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