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