Merge branch 'upstream'
[linux-2.6] / drivers / scsi / qla2xxx / qla_attr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/vmalloc.h>
10
11 /* SYSFS attributes --------------------------------------------------------- */
12
13 static ssize_t
14 qla2x00_sysfs_read_fw_dump(struct kobject *kobj, char *buf, loff_t off,
15     size_t count)
16 {
17         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
18             struct device, kobj)));
19
20         if (ha->fw_dump_reading == 0)
21                 return 0;
22         if (off > ha->fw_dump_buffer_len)
23                 return 0;
24         if (off + count > ha->fw_dump_buffer_len)
25                 count = ha->fw_dump_buffer_len - off;
26
27         memcpy(buf, &ha->fw_dump_buffer[off], count);
28
29         return (count);
30 }
31
32 static ssize_t
33 qla2x00_sysfs_write_fw_dump(struct kobject *kobj, char *buf, loff_t off,
34     size_t count)
35 {
36         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
37             struct device, kobj)));
38         int reading;
39         uint32_t dump_size;
40
41         if (off != 0)
42                 return (0);
43
44         reading = simple_strtol(buf, NULL, 10);
45         switch (reading) {
46         case 0:
47                 if (ha->fw_dump_reading == 1) {
48                         qla_printk(KERN_INFO, ha,
49                             "Firmware dump cleared on (%ld).\n",
50                             ha->host_no);
51
52                         vfree(ha->fw_dump_buffer);
53                         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
54                                 free_pages((unsigned long)ha->fw_dump,
55                                     ha->fw_dump_order);
56
57                         ha->fw_dump_reading = 0;
58                         ha->fw_dump_buffer = NULL;
59                         ha->fw_dump = NULL;
60                         ha->fw_dumped = 0;
61                 }
62                 break;
63         case 1:
64                 if ((ha->fw_dump || ha->fw_dumped) && !ha->fw_dump_reading) {
65                         ha->fw_dump_reading = 1;
66
67                         if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
68                                 dump_size = FW_DUMP_SIZE_24XX;
69                         else {
70                                 dump_size = FW_DUMP_SIZE_1M;
71                                 if (ha->fw_memory_size < 0x20000)
72                                         dump_size = FW_DUMP_SIZE_128K;
73                                 else if (ha->fw_memory_size < 0x80000)
74                                         dump_size = FW_DUMP_SIZE_512K;
75                         }
76                         ha->fw_dump_buffer = (char *)vmalloc(dump_size);
77                         if (ha->fw_dump_buffer == NULL) {
78                                 qla_printk(KERN_WARNING, ha,
79                                     "Unable to allocate memory for firmware "
80                                     "dump buffer (%d).\n", dump_size);
81
82                                 ha->fw_dump_reading = 0;
83                                 return (count);
84                         }
85                         qla_printk(KERN_INFO, ha,
86                             "Firmware dump ready for read on (%ld).\n",
87                             ha->host_no);
88                         memset(ha->fw_dump_buffer, 0, dump_size);
89                         ha->isp_ops.ascii_fw_dump(ha);
90                         ha->fw_dump_buffer_len = strlen(ha->fw_dump_buffer);
91                 }
92                 break;
93         }
94         return (count);
95 }
96
97 static struct bin_attribute sysfs_fw_dump_attr = {
98         .attr = {
99                 .name = "fw_dump",
100                 .mode = S_IRUSR | S_IWUSR,
101                 .owner = THIS_MODULE,
102         },
103         .size = 0,
104         .read = qla2x00_sysfs_read_fw_dump,
105         .write = qla2x00_sysfs_write_fw_dump,
106 };
107
108 static ssize_t
109 qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
110     size_t count)
111 {
112         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
113             struct device, kobj)));
114         unsigned long   flags;
115
116         if (!capable(CAP_SYS_ADMIN) || off != 0)
117                 return 0;
118
119         /* Read NVRAM. */
120         spin_lock_irqsave(&ha->hardware_lock, flags);
121         ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->nvram_base,
122             ha->nvram_size);
123         spin_unlock_irqrestore(&ha->hardware_lock, flags);
124
125         return ha->nvram_size;
126 }
127
128 static ssize_t
129 qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
130     size_t count)
131 {
132         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
133             struct device, kobj)));
134         unsigned long   flags;
135         uint16_t        cnt;
136
137         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
138                 return 0;
139
140         /* Checksum NVRAM. */
141         if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
142                 uint32_t *iter;
143                 uint32_t chksum;
144
145                 iter = (uint32_t *)buf;
146                 chksum = 0;
147                 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
148                         chksum += le32_to_cpu(*iter++);
149                 chksum = ~chksum + 1;
150                 *iter = cpu_to_le32(chksum);
151         } else {
152                 uint8_t *iter;
153                 uint8_t chksum;
154
155                 iter = (uint8_t *)buf;
156                 chksum = 0;
157                 for (cnt = 0; cnt < count - 1; cnt++)
158                         chksum += *iter++;
159                 chksum = ~chksum + 1;
160                 *iter = chksum;
161         }
162
163         /* Write NVRAM. */
164         spin_lock_irqsave(&ha->hardware_lock, flags);
165         ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
166         spin_unlock_irqrestore(&ha->hardware_lock, flags);
167
168         return (count);
169 }
170
171 static struct bin_attribute sysfs_nvram_attr = {
172         .attr = {
173                 .name = "nvram",
174                 .mode = S_IRUSR | S_IWUSR,
175                 .owner = THIS_MODULE,
176         },
177         .size = 512,
178         .read = qla2x00_sysfs_read_nvram,
179         .write = qla2x00_sysfs_write_nvram,
180 };
181
182 static ssize_t
183 qla2x00_sysfs_read_optrom(struct kobject *kobj, char *buf, loff_t off,
184     size_t count)
185 {
186         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
187             struct device, kobj)));
188
189         if (ha->optrom_state != QLA_SREADING)
190                 return 0;
191         if (off > ha->optrom_size)
192                 return 0;
193         if (off + count > ha->optrom_size)
194                 count = ha->optrom_size - off;
195
196         memcpy(buf, &ha->optrom_buffer[off], count);
197
198         return count;
199 }
200
201 static ssize_t
202 qla2x00_sysfs_write_optrom(struct kobject *kobj, char *buf, loff_t off,
203     size_t count)
204 {
205         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
206             struct device, kobj)));
207
208         if (ha->optrom_state != QLA_SWRITING)
209                 return -EINVAL;
210         if (off > ha->optrom_size)
211                 return -ERANGE;
212         if (off + count > ha->optrom_size)
213                 count = ha->optrom_size - off;
214
215         memcpy(&ha->optrom_buffer[off], buf, count);
216
217         return count;
218 }
219
220 static struct bin_attribute sysfs_optrom_attr = {
221         .attr = {
222                 .name = "optrom",
223                 .mode = S_IRUSR | S_IWUSR,
224                 .owner = THIS_MODULE,
225         },
226         .size = OPTROM_SIZE_24XX,
227         .read = qla2x00_sysfs_read_optrom,
228         .write = qla2x00_sysfs_write_optrom,
229 };
230
231 static ssize_t
232 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj, char *buf, loff_t off,
233     size_t count)
234 {
235         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
236             struct device, kobj)));
237         int val;
238
239         if (off)
240                 return 0;
241
242         if (sscanf(buf, "%d", &val) != 1)
243                 return -EINVAL;
244
245         switch (val) {
246         case 0:
247                 if (ha->optrom_state != QLA_SREADING &&
248                     ha->optrom_state != QLA_SWRITING)
249                         break;
250
251                 ha->optrom_state = QLA_SWAITING;
252                 vfree(ha->optrom_buffer);
253                 ha->optrom_buffer = NULL;
254                 break;
255         case 1:
256                 if (ha->optrom_state != QLA_SWAITING)
257                         break;
258
259                 ha->optrom_state = QLA_SREADING;
260                 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
261                 if (ha->optrom_buffer == NULL) {
262                         qla_printk(KERN_WARNING, ha,
263                             "Unable to allocate memory for optrom retrieval "
264                             "(%x).\n", ha->optrom_size);
265
266                         ha->optrom_state = QLA_SWAITING;
267                         return count;
268                 }
269
270                 memset(ha->optrom_buffer, 0, ha->optrom_size);
271                 ha->isp_ops.read_optrom(ha, ha->optrom_buffer, 0,
272                     ha->optrom_size);
273                 break;
274         case 2:
275                 if (ha->optrom_state != QLA_SWAITING)
276                         break;
277
278                 ha->optrom_state = QLA_SWRITING;
279                 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
280                 if (ha->optrom_buffer == NULL) {
281                         qla_printk(KERN_WARNING, ha,
282                             "Unable to allocate memory for optrom update "
283                             "(%x).\n", ha->optrom_size);
284
285                         ha->optrom_state = QLA_SWAITING;
286                         return count;
287                 }
288                 memset(ha->optrom_buffer, 0, ha->optrom_size);
289                 break;
290         case 3:
291                 if (ha->optrom_state != QLA_SWRITING)
292                         break;
293
294                 ha->isp_ops.write_optrom(ha, ha->optrom_buffer, 0,
295                     ha->optrom_size);
296                 break;
297         }
298         return count;
299 }
300
301 static struct bin_attribute sysfs_optrom_ctl_attr = {
302         .attr = {
303                 .name = "optrom_ctl",
304                 .mode = S_IWUSR,
305                 .owner = THIS_MODULE,
306         },
307         .size = 0,
308         .write = qla2x00_sysfs_write_optrom_ctl,
309 };
310
311 static ssize_t
312 qla2x00_sysfs_read_vpd(struct kobject *kobj, char *buf, loff_t off,
313     size_t count)
314 {
315         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
316             struct device, kobj)));
317         unsigned long flags;
318
319         if (!capable(CAP_SYS_ADMIN) || off != 0)
320                 return 0;
321
322         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
323                 return -ENOTSUPP;
324
325         /* Read NVRAM. */
326         spin_lock_irqsave(&ha->hardware_lock, flags);
327         ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->vpd_base, ha->vpd_size);
328         spin_unlock_irqrestore(&ha->hardware_lock, flags);
329
330         return ha->vpd_size;
331 }
332
333 static ssize_t
334 qla2x00_sysfs_write_vpd(struct kobject *kobj, char *buf, loff_t off,
335     size_t count)
336 {
337         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
338             struct device, kobj)));
339         unsigned long flags;
340
341         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
342                 return 0;
343
344         if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
345                 return -ENOTSUPP;
346
347         /* Write NVRAM. */
348         spin_lock_irqsave(&ha->hardware_lock, flags);
349         ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->vpd_base, count);
350         spin_unlock_irqrestore(&ha->hardware_lock, flags);
351
352         return count;
353 }
354
355 static struct bin_attribute sysfs_vpd_attr = {
356         .attr = {
357                 .name = "vpd",
358                 .mode = S_IRUSR | S_IWUSR,
359                 .owner = THIS_MODULE,
360         },
361         .size = 0,
362         .read = qla2x00_sysfs_read_vpd,
363         .write = qla2x00_sysfs_write_vpd,
364 };
365
366 void
367 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
368 {
369         struct Scsi_Host *host = ha->host;
370
371         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
372         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
373         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
374         sysfs_create_bin_file(&host->shost_gendev.kobj,
375             &sysfs_optrom_ctl_attr);
376         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_vpd_attr);
377 }
378
379 void
380 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
381 {
382         struct Scsi_Host *host = ha->host;
383
384         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
385         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
386         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
387         sysfs_remove_bin_file(&host->shost_gendev.kobj,
388             &sysfs_optrom_ctl_attr);
389         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_vpd_attr);
390
391         if (ha->beacon_blink_led == 1)
392                 ha->isp_ops.beacon_off(ha);
393 }
394
395 /* Scsi_Host attributes. */
396
397 static ssize_t
398 qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
399 {
400         return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
401 }
402
403 static ssize_t
404 qla2x00_fw_version_show(struct class_device *cdev, char *buf)
405 {
406         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
407         char fw_str[30];
408
409         return snprintf(buf, PAGE_SIZE, "%s\n",
410             ha->isp_ops.fw_version_str(ha, fw_str));
411 }
412
413 static ssize_t
414 qla2x00_serial_num_show(struct class_device *cdev, char *buf)
415 {
416         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
417         uint32_t sn;
418
419         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
420         return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
421             sn % 100000);
422 }
423
424 static ssize_t
425 qla2x00_isp_name_show(struct class_device *cdev, char *buf)
426 {
427         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
428         return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
429 }
430
431 static ssize_t
432 qla2x00_isp_id_show(struct class_device *cdev, char *buf)
433 {
434         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
435         return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
436             ha->product_id[0], ha->product_id[1], ha->product_id[2],
437             ha->product_id[3]);
438 }
439
440 static ssize_t
441 qla2x00_model_name_show(struct class_device *cdev, char *buf)
442 {
443         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
444         return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
445 }
446
447 static ssize_t
448 qla2x00_model_desc_show(struct class_device *cdev, char *buf)
449 {
450         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
451         return snprintf(buf, PAGE_SIZE, "%s\n",
452             ha->model_desc ? ha->model_desc: "");
453 }
454
455 static ssize_t
456 qla2x00_pci_info_show(struct class_device *cdev, char *buf)
457 {
458         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
459         char pci_info[30];
460
461         return snprintf(buf, PAGE_SIZE, "%s\n",
462             ha->isp_ops.pci_info_str(ha, pci_info));
463 }
464
465 static ssize_t
466 qla2x00_state_show(struct class_device *cdev, char *buf)
467 {
468         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
469         int len = 0;
470
471         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
472             atomic_read(&ha->loop_state) == LOOP_DEAD)
473                 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
474         else if (atomic_read(&ha->loop_state) != LOOP_READY ||
475             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
476             test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
477                 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
478         else {
479                 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
480
481                 switch (ha->current_topology) {
482                 case ISP_CFG_NL:
483                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
484                         break;
485                 case ISP_CFG_FL:
486                         len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
487                         break;
488                 case ISP_CFG_N:
489                         len += snprintf(buf + len, PAGE_SIZE-len,
490                             "N_Port to N_Port\n");
491                         break;
492                 case ISP_CFG_F:
493                         len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
494                         break;
495                 default:
496                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
497                         break;
498                 }
499         }
500         return len;
501 }
502
503 static ssize_t
504 qla2x00_zio_show(struct class_device *cdev, char *buf)
505 {
506         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
507         int len = 0;
508
509         switch (ha->zio_mode) {
510         case QLA_ZIO_MODE_6:
511                 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
512                 break;
513         case QLA_ZIO_DISABLED:
514                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
515                 break;
516         }
517         return len;
518 }
519
520 static ssize_t
521 qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
522 {
523         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
524         int val = 0;
525         uint16_t zio_mode;
526
527         if (!IS_ZIO_SUPPORTED(ha))
528                 return -ENOTSUPP;
529
530         if (sscanf(buf, "%d", &val) != 1)
531                 return -EINVAL;
532
533         if (val)
534                 zio_mode = QLA_ZIO_MODE_6;
535         else
536                 zio_mode = QLA_ZIO_DISABLED;
537
538         /* Update per-hba values and queue a reset. */
539         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
540                 ha->zio_mode = zio_mode;
541                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
542         }
543         return strlen(buf);
544 }
545
546 static ssize_t
547 qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
548 {
549         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
550
551         return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
552 }
553
554 static ssize_t
555 qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
556     size_t count)
557 {
558         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
559         int val = 0;
560         uint16_t zio_timer;
561
562         if (sscanf(buf, "%d", &val) != 1)
563                 return -EINVAL;
564         if (val > 25500 || val < 100)
565                 return -ERANGE;
566
567         zio_timer = (uint16_t)(val / 100);
568         ha->zio_timer = zio_timer;
569
570         return strlen(buf);
571 }
572
573 static ssize_t
574 qla2x00_beacon_show(struct class_device *cdev, char *buf)
575 {
576         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
577         int len = 0;
578
579         if (ha->beacon_blink_led)
580                 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
581         else
582                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
583         return len;
584 }
585
586 static ssize_t
587 qla2x00_beacon_store(struct class_device *cdev, const char *buf,
588     size_t count)
589 {
590         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
591         int val = 0;
592         int rval;
593
594         if (IS_QLA2100(ha) || IS_QLA2200(ha))
595                 return -EPERM;
596
597         if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
598                 qla_printk(KERN_WARNING, ha,
599                     "Abort ISP active -- ignoring beacon request.\n");
600                 return -EBUSY;
601         }
602
603         if (sscanf(buf, "%d", &val) != 1)
604                 return -EINVAL;
605
606         if (val)
607                 rval = ha->isp_ops.beacon_on(ha);
608         else
609                 rval = ha->isp_ops.beacon_off(ha);
610
611         if (rval != QLA_SUCCESS)
612                 count = 0;
613
614         return count;
615 }
616
617 static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
618         NULL);
619 static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
620 static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
621 static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
622 static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
623 static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
624 static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
625 static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
626 static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
627 static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
628     qla2x00_zio_store);
629 static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
630     qla2x00_zio_timer_store);
631 static CLASS_DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
632     qla2x00_beacon_store);
633
634 struct class_device_attribute *qla2x00_host_attrs[] = {
635         &class_device_attr_driver_version,
636         &class_device_attr_fw_version,
637         &class_device_attr_serial_num,
638         &class_device_attr_isp_name,
639         &class_device_attr_isp_id,
640         &class_device_attr_model_name,
641         &class_device_attr_model_desc,
642         &class_device_attr_pci_info,
643         &class_device_attr_state,
644         &class_device_attr_zio,
645         &class_device_attr_zio_timer,
646         &class_device_attr_beacon,
647         NULL,
648 };
649
650 /* Host attributes. */
651
652 static void
653 qla2x00_get_host_port_id(struct Scsi_Host *shost)
654 {
655         scsi_qla_host_t *ha = to_qla_host(shost);
656
657         fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
658             ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
659 }
660
661 static void
662 qla2x00_get_host_speed(struct Scsi_Host *shost)
663 {
664         scsi_qla_host_t *ha = to_qla_host(shost);
665         uint32_t speed = 0;
666
667         switch (ha->link_data_rate) {
668         case LDR_1GB:
669                 speed = 1;
670                 break;
671         case LDR_2GB:
672                 speed = 2;
673                 break;
674         case LDR_4GB:
675                 speed = 4;
676                 break;
677         }
678         fc_host_speed(shost) = speed;
679 }
680
681 static void
682 qla2x00_get_host_port_type(struct Scsi_Host *shost)
683 {
684         scsi_qla_host_t *ha = to_qla_host(shost);
685         uint32_t port_type = FC_PORTTYPE_UNKNOWN;
686
687         switch (ha->current_topology) {
688         case ISP_CFG_NL:
689                 port_type = FC_PORTTYPE_LPORT;
690                 break;
691         case ISP_CFG_FL:
692                 port_type = FC_PORTTYPE_NLPORT;
693                 break;
694         case ISP_CFG_N:
695                 port_type = FC_PORTTYPE_PTP;
696                 break;
697         case ISP_CFG_F:
698                 port_type = FC_PORTTYPE_NPORT;
699                 break;
700         }
701         fc_host_port_type(shost) = port_type;
702 }
703
704 static void
705 qla2x00_get_starget_node_name(struct scsi_target *starget)
706 {
707         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
708         scsi_qla_host_t *ha = to_qla_host(host);
709         fc_port_t *fcport;
710         u64 node_name = 0;
711
712         list_for_each_entry(fcport, &ha->fcports, list) {
713                 if (starget->id == fcport->os_target_id) {
714                         node_name = wwn_to_u64(fcport->node_name);
715                         break;
716                 }
717         }
718
719         fc_starget_node_name(starget) = node_name;
720 }
721
722 static void
723 qla2x00_get_starget_port_name(struct scsi_target *starget)
724 {
725         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
726         scsi_qla_host_t *ha = to_qla_host(host);
727         fc_port_t *fcport;
728         u64 port_name = 0;
729
730         list_for_each_entry(fcport, &ha->fcports, list) {
731                 if (starget->id == fcport->os_target_id) {
732                         port_name = wwn_to_u64(fcport->port_name);
733                         break;
734                 }
735         }
736
737         fc_starget_port_name(starget) = port_name;
738 }
739
740 static void
741 qla2x00_get_starget_port_id(struct scsi_target *starget)
742 {
743         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
744         scsi_qla_host_t *ha = to_qla_host(host);
745         fc_port_t *fcport;
746         uint32_t port_id = ~0U;
747
748         list_for_each_entry(fcport, &ha->fcports, list) {
749                 if (starget->id == fcport->os_target_id) {
750                         port_id = fcport->d_id.b.domain << 16 |
751                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
752                         break;
753                 }
754         }
755
756         fc_starget_port_id(starget) = port_id;
757 }
758
759 static void
760 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
761 {
762         struct Scsi_Host *host = rport_to_shost(rport);
763         scsi_qla_host_t *ha = to_qla_host(host);
764
765         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
766 }
767
768 static void
769 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
770 {
771         struct Scsi_Host *host = rport_to_shost(rport);
772         scsi_qla_host_t *ha = to_qla_host(host);
773
774         if (timeout)
775                 ha->port_down_retry_count = timeout;
776         else
777                 ha->port_down_retry_count = 1;
778
779         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
780 }
781
782 static int
783 qla2x00_issue_lip(struct Scsi_Host *shost)
784 {
785         scsi_qla_host_t *ha = to_qla_host(shost);
786
787         set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
788         return 0;
789 }
790
791 static struct fc_host_statistics *
792 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
793 {
794         scsi_qla_host_t *ha = to_qla_host(shost);
795         int rval;
796         uint16_t mb_stat[1];
797         link_stat_t stat_buf;
798         struct fc_host_statistics *pfc_host_stat;
799
800         pfc_host_stat = &ha->fc_host_stat;
801         memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
802
803         if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
804                 rval = qla24xx_get_isp_stats(ha, (uint32_t *)&stat_buf,
805                     sizeof(stat_buf) / 4, mb_stat);
806         } else {
807                 rval = qla2x00_get_link_status(ha, ha->loop_id, &stat_buf,
808                     mb_stat);
809         }
810         if (rval != 0) {
811                 qla_printk(KERN_WARNING, ha,
812                     "Unable to retrieve host statistics (%d).\n", mb_stat[0]);
813                 return pfc_host_stat;
814         }
815
816         pfc_host_stat->link_failure_count = stat_buf.link_fail_cnt;
817         pfc_host_stat->loss_of_sync_count = stat_buf.loss_sync_cnt;
818         pfc_host_stat->loss_of_signal_count = stat_buf.loss_sig_cnt;
819         pfc_host_stat->prim_seq_protocol_err_count = stat_buf.prim_seq_err_cnt;
820         pfc_host_stat->invalid_tx_word_count = stat_buf.inval_xmit_word_cnt;
821         pfc_host_stat->invalid_crc_count = stat_buf.inval_crc_cnt;
822
823         return pfc_host_stat;
824 }
825
826 struct fc_function_template qla2xxx_transport_functions = {
827
828         .show_host_node_name = 1,
829         .show_host_port_name = 1,
830         .show_host_supported_classes = 1,
831
832         .get_host_port_id = qla2x00_get_host_port_id,
833         .show_host_port_id = 1,
834         .get_host_speed = qla2x00_get_host_speed,
835         .show_host_speed = 1,
836         .get_host_port_type = qla2x00_get_host_port_type,
837         .show_host_port_type = 1,
838
839         .dd_fcrport_size = sizeof(struct fc_port *),
840         .show_rport_supported_classes = 1,
841
842         .get_starget_node_name = qla2x00_get_starget_node_name,
843         .show_starget_node_name = 1,
844         .get_starget_port_name = qla2x00_get_starget_port_name,
845         .show_starget_port_name = 1,
846         .get_starget_port_id  = qla2x00_get_starget_port_id,
847         .show_starget_port_id = 1,
848
849         .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
850         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
851         .show_rport_dev_loss_tmo = 1,
852
853         .issue_fc_host_lip = qla2x00_issue_lip,
854         .get_fc_host_stats = qla2x00_get_fc_host_stats,
855 };
856
857 void
858 qla2x00_init_host_attr(scsi_qla_host_t *ha)
859 {
860         fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
861         fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
862         fc_host_supported_classes(ha->host) = FC_COS_CLASS3;
863 }