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