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