[SCSI] qla2xxx: Re-factor isp_operations to static structures.
[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/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_FWI2_CAPABLE(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,
309             ha->vpd_size);
310         spin_unlock_irqrestore(&ha->hardware_lock, flags);
311
312         return ha->vpd_size;
313 }
314
315 static ssize_t
316 qla2x00_sysfs_write_vpd(struct kobject *kobj,
317                         struct bin_attribute *bin_attr,
318                         char *buf, loff_t off, size_t count)
319 {
320         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
321             struct device, kobj)));
322         unsigned long flags;
323
324         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
325                 return 0;
326
327         /* Write NVRAM. */
328         spin_lock_irqsave(&ha->hardware_lock, flags);
329         ha->isp_ops->write_nvram(ha, (uint8_t *)buf, ha->vpd_base, count);
330         spin_unlock_irqrestore(&ha->hardware_lock, flags);
331
332         return count;
333 }
334
335 static struct bin_attribute sysfs_vpd_attr = {
336         .attr = {
337                 .name = "vpd",
338                 .mode = S_IRUSR | S_IWUSR,
339         },
340         .size = 0,
341         .read = qla2x00_sysfs_read_vpd,
342         .write = qla2x00_sysfs_write_vpd,
343 };
344
345 static ssize_t
346 qla2x00_sysfs_read_sfp(struct kobject *kobj,
347                        struct bin_attribute *bin_attr,
348                        char *buf, loff_t off, size_t count)
349 {
350         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
351             struct device, kobj)));
352         uint16_t iter, addr, offset;
353         int rval;
354
355         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
356                 return 0;
357
358         addr = 0xa0;
359         for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
360             iter++, offset += SFP_BLOCK_SIZE) {
361                 if (iter == 4) {
362                         /* Skip to next device address. */
363                         addr = 0xa2;
364                         offset = 0;
365                 }
366
367                 rval = qla2x00_read_sfp(ha, ha->sfp_data_dma, addr, offset,
368                     SFP_BLOCK_SIZE);
369                 if (rval != QLA_SUCCESS) {
370                         qla_printk(KERN_WARNING, ha,
371                             "Unable to read SFP data (%x/%x/%x).\n", rval,
372                             addr, offset);
373                         count = 0;
374                         break;
375                 }
376                 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
377                 buf += SFP_BLOCK_SIZE;
378         }
379
380         return count;
381 }
382
383 static struct bin_attribute sysfs_sfp_attr = {
384         .attr = {
385                 .name = "sfp",
386                 .mode = S_IRUSR | S_IWUSR,
387         },
388         .size = SFP_DEV_SIZE * 2,
389         .read = qla2x00_sysfs_read_sfp,
390 };
391
392 static struct sysfs_entry {
393         char *name;
394         struct bin_attribute *attr;
395         int is4GBp_only;
396 } bin_file_entries[] = {
397         { "fw_dump", &sysfs_fw_dump_attr, },
398         { "nvram", &sysfs_nvram_attr, },
399         { "optrom", &sysfs_optrom_attr, },
400         { "optrom_ctl", &sysfs_optrom_ctl_attr, },
401         { "vpd", &sysfs_vpd_attr, 1 },
402         { "sfp", &sysfs_sfp_attr, 1 },
403         { NULL },
404 };
405
406 void
407 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
408 {
409         struct Scsi_Host *host = ha->host;
410         struct sysfs_entry *iter;
411         int ret;
412
413         for (iter = bin_file_entries; iter->name; iter++) {
414                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
415                         continue;
416
417                 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
418                     iter->attr);
419                 if (ret)
420                         qla_printk(KERN_INFO, ha,
421                             "Unable to create sysfs %s binary attribute "
422                             "(%d).\n", iter->name, ret);
423         }
424 }
425
426 void
427 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
428 {
429         struct Scsi_Host *host = ha->host;
430         struct sysfs_entry *iter;
431
432         for (iter = bin_file_entries; iter->name; iter++) {
433                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
434                         continue;
435
436                 sysfs_remove_bin_file(&host->shost_gendev.kobj,
437                     iter->attr);
438         }
439
440         if (ha->beacon_blink_led == 1)
441                 ha->isp_ops->beacon_off(ha);
442 }
443
444 /* Scsi_Host attributes. */
445
446 static ssize_t
447 qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
448 {
449         return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
450 }
451
452 static ssize_t
453 qla2x00_fw_version_show(struct class_device *cdev, char *buf)
454 {
455         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
456         char fw_str[30];
457
458         return snprintf(buf, PAGE_SIZE, "%s\n",
459             ha->isp_ops->fw_version_str(ha, fw_str));
460 }
461
462 static ssize_t
463 qla2x00_serial_num_show(struct class_device *cdev, char *buf)
464 {
465         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
466         uint32_t sn;
467
468         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
469         return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
470             sn % 100000);
471 }
472
473 static ssize_t
474 qla2x00_isp_name_show(struct class_device *cdev, char *buf)
475 {
476         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
477         return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
478 }
479
480 static ssize_t
481 qla2x00_isp_id_show(struct class_device *cdev, char *buf)
482 {
483         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
484         return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
485             ha->product_id[0], ha->product_id[1], ha->product_id[2],
486             ha->product_id[3]);
487 }
488
489 static ssize_t
490 qla2x00_model_name_show(struct class_device *cdev, char *buf)
491 {
492         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
493         return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
494 }
495
496 static ssize_t
497 qla2x00_model_desc_show(struct class_device *cdev, char *buf)
498 {
499         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
500         return snprintf(buf, PAGE_SIZE, "%s\n",
501             ha->model_desc ? ha->model_desc: "");
502 }
503
504 static ssize_t
505 qla2x00_pci_info_show(struct class_device *cdev, char *buf)
506 {
507         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
508         char pci_info[30];
509
510         return snprintf(buf, PAGE_SIZE, "%s\n",
511             ha->isp_ops->pci_info_str(ha, pci_info));
512 }
513
514 static ssize_t
515 qla2x00_state_show(struct class_device *cdev, char *buf)
516 {
517         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
518         int len = 0;
519
520         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
521             atomic_read(&ha->loop_state) == LOOP_DEAD)
522                 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
523         else if (atomic_read(&ha->loop_state) != LOOP_READY ||
524             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
525             test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
526                 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
527         else {
528                 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
529
530                 switch (ha->current_topology) {
531                 case ISP_CFG_NL:
532                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
533                         break;
534                 case ISP_CFG_FL:
535                         len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
536                         break;
537                 case ISP_CFG_N:
538                         len += snprintf(buf + len, PAGE_SIZE-len,
539                             "N_Port to N_Port\n");
540                         break;
541                 case ISP_CFG_F:
542                         len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
543                         break;
544                 default:
545                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
546                         break;
547                 }
548         }
549         return len;
550 }
551
552 static ssize_t
553 qla2x00_zio_show(struct class_device *cdev, char *buf)
554 {
555         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
556         int len = 0;
557
558         switch (ha->zio_mode) {
559         case QLA_ZIO_MODE_6:
560                 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
561                 break;
562         case QLA_ZIO_DISABLED:
563                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
564                 break;
565         }
566         return len;
567 }
568
569 static ssize_t
570 qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
571 {
572         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
573         int val = 0;
574         uint16_t zio_mode;
575
576         if (!IS_ZIO_SUPPORTED(ha))
577                 return -ENOTSUPP;
578
579         if (sscanf(buf, "%d", &val) != 1)
580                 return -EINVAL;
581
582         if (val)
583                 zio_mode = QLA_ZIO_MODE_6;
584         else
585                 zio_mode = QLA_ZIO_DISABLED;
586
587         /* Update per-hba values and queue a reset. */
588         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
589                 ha->zio_mode = zio_mode;
590                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
591         }
592         return strlen(buf);
593 }
594
595 static ssize_t
596 qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
597 {
598         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
599
600         return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
601 }
602
603 static ssize_t
604 qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
605     size_t count)
606 {
607         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
608         int val = 0;
609         uint16_t zio_timer;
610
611         if (sscanf(buf, "%d", &val) != 1)
612                 return -EINVAL;
613         if (val > 25500 || val < 100)
614                 return -ERANGE;
615
616         zio_timer = (uint16_t)(val / 100);
617         ha->zio_timer = zio_timer;
618
619         return strlen(buf);
620 }
621
622 static ssize_t
623 qla2x00_beacon_show(struct class_device *cdev, char *buf)
624 {
625         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
626         int len = 0;
627
628         if (ha->beacon_blink_led)
629                 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
630         else
631                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
632         return len;
633 }
634
635 static ssize_t
636 qla2x00_beacon_store(struct class_device *cdev, const char *buf,
637     size_t count)
638 {
639         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
640         int val = 0;
641         int rval;
642
643         if (IS_QLA2100(ha) || IS_QLA2200(ha))
644                 return -EPERM;
645
646         if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
647                 qla_printk(KERN_WARNING, ha,
648                     "Abort ISP active -- ignoring beacon request.\n");
649                 return -EBUSY;
650         }
651
652         if (sscanf(buf, "%d", &val) != 1)
653                 return -EINVAL;
654
655         if (val)
656                 rval = ha->isp_ops->beacon_on(ha);
657         else
658                 rval = ha->isp_ops->beacon_off(ha);
659
660         if (rval != QLA_SUCCESS)
661                 count = 0;
662
663         return count;
664 }
665
666 static ssize_t
667 qla2x00_optrom_bios_version_show(struct class_device *cdev, char *buf)
668 {
669         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
670
671         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
672             ha->bios_revision[0]);
673 }
674
675 static ssize_t
676 qla2x00_optrom_efi_version_show(struct class_device *cdev, char *buf)
677 {
678         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
679
680         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
681             ha->efi_revision[0]);
682 }
683
684 static ssize_t
685 qla2x00_optrom_fcode_version_show(struct class_device *cdev, char *buf)
686 {
687         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
688
689         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
690             ha->fcode_revision[0]);
691 }
692
693 static ssize_t
694 qla2x00_optrom_fw_version_show(struct class_device *cdev, char *buf)
695 {
696         scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
697
698         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
699             ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
700             ha->fw_revision[3]);
701 }
702
703 static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
704         NULL);
705 static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
706 static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
707 static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
708 static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
709 static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
710 static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
711 static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
712 static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
713 static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
714     qla2x00_zio_store);
715 static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
716     qla2x00_zio_timer_store);
717 static CLASS_DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
718     qla2x00_beacon_store);
719 static CLASS_DEVICE_ATTR(optrom_bios_version, S_IRUGO,
720     qla2x00_optrom_bios_version_show, NULL);
721 static CLASS_DEVICE_ATTR(optrom_efi_version, S_IRUGO,
722     qla2x00_optrom_efi_version_show, NULL);
723 static CLASS_DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
724     qla2x00_optrom_fcode_version_show, NULL);
725 static CLASS_DEVICE_ATTR(optrom_fw_version, S_IRUGO,
726     qla2x00_optrom_fw_version_show, NULL);
727
728 struct class_device_attribute *qla2x00_host_attrs[] = {
729         &class_device_attr_driver_version,
730         &class_device_attr_fw_version,
731         &class_device_attr_serial_num,
732         &class_device_attr_isp_name,
733         &class_device_attr_isp_id,
734         &class_device_attr_model_name,
735         &class_device_attr_model_desc,
736         &class_device_attr_pci_info,
737         &class_device_attr_state,
738         &class_device_attr_zio,
739         &class_device_attr_zio_timer,
740         &class_device_attr_beacon,
741         &class_device_attr_optrom_bios_version,
742         &class_device_attr_optrom_efi_version,
743         &class_device_attr_optrom_fcode_version,
744         &class_device_attr_optrom_fw_version,
745         NULL,
746 };
747
748 /* Host attributes. */
749
750 static void
751 qla2x00_get_host_port_id(struct Scsi_Host *shost)
752 {
753         scsi_qla_host_t *ha = to_qla_host(shost);
754
755         fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
756             ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
757 }
758
759 static void
760 qla2x00_get_host_speed(struct Scsi_Host *shost)
761 {
762         scsi_qla_host_t *ha = to_qla_host(shost);
763         uint32_t speed = 0;
764
765         switch (ha->link_data_rate) {
766         case PORT_SPEED_1GB:
767                 speed = 1;
768                 break;
769         case PORT_SPEED_2GB:
770                 speed = 2;
771                 break;
772         case PORT_SPEED_4GB:
773                 speed = 4;
774                 break;
775         }
776         fc_host_speed(shost) = speed;
777 }
778
779 static void
780 qla2x00_get_host_port_type(struct Scsi_Host *shost)
781 {
782         scsi_qla_host_t *ha = to_qla_host(shost);
783         uint32_t port_type = FC_PORTTYPE_UNKNOWN;
784
785         switch (ha->current_topology) {
786         case ISP_CFG_NL:
787                 port_type = FC_PORTTYPE_LPORT;
788                 break;
789         case ISP_CFG_FL:
790                 port_type = FC_PORTTYPE_NLPORT;
791                 break;
792         case ISP_CFG_N:
793                 port_type = FC_PORTTYPE_PTP;
794                 break;
795         case ISP_CFG_F:
796                 port_type = FC_PORTTYPE_NPORT;
797                 break;
798         }
799         fc_host_port_type(shost) = port_type;
800 }
801
802 static void
803 qla2x00_get_starget_node_name(struct scsi_target *starget)
804 {
805         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
806         scsi_qla_host_t *ha = to_qla_host(host);
807         fc_port_t *fcport;
808         u64 node_name = 0;
809
810         list_for_each_entry(fcport, &ha->fcports, list) {
811                 if (starget->id == fcport->os_target_id) {
812                         node_name = wwn_to_u64(fcport->node_name);
813                         break;
814                 }
815         }
816
817         fc_starget_node_name(starget) = node_name;
818 }
819
820 static void
821 qla2x00_get_starget_port_name(struct scsi_target *starget)
822 {
823         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
824         scsi_qla_host_t *ha = to_qla_host(host);
825         fc_port_t *fcport;
826         u64 port_name = 0;
827
828         list_for_each_entry(fcport, &ha->fcports, list) {
829                 if (starget->id == fcport->os_target_id) {
830                         port_name = wwn_to_u64(fcport->port_name);
831                         break;
832                 }
833         }
834
835         fc_starget_port_name(starget) = port_name;
836 }
837
838 static void
839 qla2x00_get_starget_port_id(struct scsi_target *starget)
840 {
841         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
842         scsi_qla_host_t *ha = to_qla_host(host);
843         fc_port_t *fcport;
844         uint32_t port_id = ~0U;
845
846         list_for_each_entry(fcport, &ha->fcports, list) {
847                 if (starget->id == fcport->os_target_id) {
848                         port_id = fcport->d_id.b.domain << 16 |
849                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
850                         break;
851                 }
852         }
853
854         fc_starget_port_id(starget) = port_id;
855 }
856
857 static void
858 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
859 {
860         struct Scsi_Host *host = rport_to_shost(rport);
861         scsi_qla_host_t *ha = to_qla_host(host);
862
863         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
864 }
865
866 static void
867 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
868 {
869         struct Scsi_Host *host = rport_to_shost(rport);
870         scsi_qla_host_t *ha = to_qla_host(host);
871
872         if (timeout)
873                 ha->port_down_retry_count = timeout;
874         else
875                 ha->port_down_retry_count = 1;
876
877         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
878 }
879
880 static int
881 qla2x00_issue_lip(struct Scsi_Host *shost)
882 {
883         scsi_qla_host_t *ha = to_qla_host(shost);
884
885         set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
886         return 0;
887 }
888
889 static struct fc_host_statistics *
890 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
891 {
892         scsi_qla_host_t *ha = to_qla_host(shost);
893         int rval;
894         uint16_t mb_stat[1];
895         link_stat_t stat_buf;
896         struct fc_host_statistics *pfc_host_stat;
897
898         rval = QLA_FUNCTION_FAILED;
899         pfc_host_stat = &ha->fc_host_stat;
900         memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
901
902         if (IS_FWI2_CAPABLE(ha)) {
903                 rval = qla24xx_get_isp_stats(ha, (uint32_t *)&stat_buf,
904                     sizeof(stat_buf) / 4, mb_stat);
905         } else if (atomic_read(&ha->loop_state) == LOOP_READY &&
906                     !test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) &&
907                     !test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) &&
908                     !ha->dpc_active) {
909                 /* Must be in a 'READY' state for statistics retrieval. */
910                 rval = qla2x00_get_link_status(ha, ha->loop_id, &stat_buf,
911                     mb_stat);
912         }
913
914         if (rval != QLA_SUCCESS)
915                 goto done;
916
917         pfc_host_stat->link_failure_count = stat_buf.link_fail_cnt;
918         pfc_host_stat->loss_of_sync_count = stat_buf.loss_sync_cnt;
919         pfc_host_stat->loss_of_signal_count = stat_buf.loss_sig_cnt;
920         pfc_host_stat->prim_seq_protocol_err_count = stat_buf.prim_seq_err_cnt;
921         pfc_host_stat->invalid_tx_word_count = stat_buf.inval_xmit_word_cnt;
922         pfc_host_stat->invalid_crc_count = stat_buf.inval_crc_cnt;
923 done:
924         return pfc_host_stat;
925 }
926
927 static void
928 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
929 {
930         scsi_qla_host_t *ha = to_qla_host(shost);
931
932         qla2x00_get_sym_node_name(ha, fc_host_symbolic_name(shost));
933 }
934
935 static void
936 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
937 {
938         scsi_qla_host_t *ha = to_qla_host(shost);
939
940         set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
941 }
942
943 static void
944 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
945 {
946         scsi_qla_host_t *ha = to_qla_host(shost);
947         u64 node_name;
948
949         if (ha->device_flags & SWITCH_FOUND)
950                 node_name = wwn_to_u64(ha->fabric_node_name);
951         else
952                 node_name = wwn_to_u64(ha->node_name);
953
954         fc_host_fabric_name(shost) = node_name;
955 }
956
957 static void
958 qla2x00_get_host_port_state(struct Scsi_Host *shost)
959 {
960         scsi_qla_host_t *ha = to_qla_host(shost);
961
962         if (!ha->flags.online)
963                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
964         else if (atomic_read(&ha->loop_state) == LOOP_TIMEOUT)
965                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
966         else
967                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
968 }
969
970 static int
971 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
972 {
973         int     ret = 0;
974         scsi_qla_host_t *ha = (scsi_qla_host_t *) fc_vport->shost->hostdata;
975         scsi_qla_host_t *vha;
976
977         ret = qla24xx_vport_create_req_sanity_check(fc_vport);
978         if (ret) {
979                 DEBUG15(printk("qla24xx_vport_create_req_sanity_check failed, "
980                     "status %x\n", ret));
981                 return (ret);
982         }
983
984         vha = qla24xx_create_vhost(fc_vport);
985         if (vha == NULL) {
986                 DEBUG15(printk ("qla24xx_create_vhost failed, vha = %p\n",
987                     vha));
988                 return FC_VPORT_FAILED;
989         }
990         if (disable) {
991                 atomic_set(&vha->vp_state, VP_OFFLINE);
992                 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
993         } else
994                 atomic_set(&vha->vp_state, VP_FAILED);
995
996         /* ready to create vport */
997         qla_printk(KERN_INFO, vha, "VP entry id %d assigned.\n", vha->vp_idx);
998
999         /* initialized vport states */
1000         atomic_set(&vha->loop_state, LOOP_DOWN);
1001         vha->vp_err_state=  VP_ERR_PORTDWN;
1002         vha->vp_prev_err_state=  VP_ERR_UNKWN;
1003         /* Check if physical ha port is Up */
1004         if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
1005             atomic_read(&ha->loop_state) == LOOP_DEAD) {
1006                 /* Don't retry or attempt login of this virtual port */
1007                 DEBUG15(printk ("scsi(%ld): pport loop_state is not UP.\n",
1008                     vha->host_no));
1009                 atomic_set(&vha->loop_state, LOOP_DEAD);
1010                 if (!disable)
1011                         fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
1012         }
1013
1014         if (scsi_add_host(vha->host, &fc_vport->dev)) {
1015                 DEBUG15(printk("scsi(%ld): scsi_add_host failure for VP[%d].\n",
1016                         vha->host_no, vha->vp_idx));
1017                 goto vport_create_failed_2;
1018         }
1019
1020         /* initialize attributes */
1021         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1022         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1023         fc_host_supported_classes(vha->host) =
1024                 fc_host_supported_classes(ha->host);
1025         fc_host_supported_speeds(vha->host) =
1026                 fc_host_supported_speeds(ha->host);
1027
1028         qla24xx_vport_disable(fc_vport, disable);
1029
1030         return 0;
1031 vport_create_failed_2:
1032         qla24xx_disable_vp(vha);
1033         qla24xx_deallocate_vp_id(vha);
1034         kfree(vha->port_name);
1035         kfree(vha->node_name);
1036         scsi_host_put(vha->host);
1037         return FC_VPORT_FAILED;
1038 }
1039
1040 int
1041 qla24xx_vport_delete(struct fc_vport *fc_vport)
1042 {
1043         scsi_qla_host_t *ha = (scsi_qla_host_t *) fc_vport->shost->hostdata;
1044         scsi_qla_host_t *vha = fc_vport->dd_data;
1045
1046         qla24xx_disable_vp(vha);
1047         qla24xx_deallocate_vp_id(vha);
1048
1049         down(&ha->vport_sem);
1050         ha->cur_vport_count--;
1051         clear_bit(vha->vp_idx, (unsigned long *)ha->vp_idx_map);
1052         up(&ha->vport_sem);
1053
1054         kfree(vha->node_name);
1055         kfree(vha->port_name);
1056
1057         if (vha->timer_active) {
1058                 qla2x00_vp_stop_timer(vha);
1059                 DEBUG15(printk ("scsi(%ld): timer for the vport[%d] = %p "
1060                     "has stopped\n",
1061                     vha->host_no, vha->vp_idx, vha));
1062         }
1063
1064         fc_remove_host(vha->host);
1065
1066         scsi_remove_host(vha->host);
1067
1068         scsi_host_put(vha->host);
1069
1070         return 0;
1071 }
1072
1073 int
1074 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
1075 {
1076         scsi_qla_host_t *vha = fc_vport->dd_data;
1077
1078         if (disable)
1079                 qla24xx_disable_vp(vha);
1080         else
1081                 qla24xx_enable_vp(vha);
1082
1083         return 0;
1084 }
1085
1086 struct fc_function_template qla2xxx_transport_functions = {
1087
1088         .show_host_node_name = 1,
1089         .show_host_port_name = 1,
1090         .show_host_supported_classes = 1,
1091
1092         .get_host_port_id = qla2x00_get_host_port_id,
1093         .show_host_port_id = 1,
1094         .get_host_speed = qla2x00_get_host_speed,
1095         .show_host_speed = 1,
1096         .get_host_port_type = qla2x00_get_host_port_type,
1097         .show_host_port_type = 1,
1098         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1099         .show_host_symbolic_name = 1,
1100         .set_host_system_hostname = qla2x00_set_host_system_hostname,
1101         .show_host_system_hostname = 1,
1102         .get_host_fabric_name = qla2x00_get_host_fabric_name,
1103         .show_host_fabric_name = 1,
1104         .get_host_port_state = qla2x00_get_host_port_state,
1105         .show_host_port_state = 1,
1106
1107         .dd_fcrport_size = sizeof(struct fc_port *),
1108         .show_rport_supported_classes = 1,
1109
1110         .get_starget_node_name = qla2x00_get_starget_node_name,
1111         .show_starget_node_name = 1,
1112         .get_starget_port_name = qla2x00_get_starget_port_name,
1113         .show_starget_port_name = 1,
1114         .get_starget_port_id  = qla2x00_get_starget_port_id,
1115         .show_starget_port_id = 1,
1116
1117         .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
1118         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1119         .show_rport_dev_loss_tmo = 1,
1120
1121         .issue_fc_host_lip = qla2x00_issue_lip,
1122         .get_fc_host_stats = qla2x00_get_fc_host_stats,
1123
1124         .vport_create = qla24xx_vport_create,
1125         .vport_disable = qla24xx_vport_disable,
1126         .vport_delete = qla24xx_vport_delete,
1127 };
1128
1129 struct fc_function_template qla2xxx_transport_vport_functions = {
1130
1131         .show_host_node_name = 1,
1132         .show_host_port_name = 1,
1133         .show_host_supported_classes = 1,
1134
1135         .get_host_port_id = qla2x00_get_host_port_id,
1136         .show_host_port_id = 1,
1137         .get_host_speed = qla2x00_get_host_speed,
1138         .show_host_speed = 1,
1139         .get_host_port_type = qla2x00_get_host_port_type,
1140         .show_host_port_type = 1,
1141         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1142         .show_host_symbolic_name = 1,
1143         .set_host_system_hostname = qla2x00_set_host_system_hostname,
1144         .show_host_system_hostname = 1,
1145         .get_host_fabric_name = qla2x00_get_host_fabric_name,
1146         .show_host_fabric_name = 1,
1147         .get_host_port_state = qla2x00_get_host_port_state,
1148         .show_host_port_state = 1,
1149
1150         .dd_fcrport_size = sizeof(struct fc_port *),
1151         .show_rport_supported_classes = 1,
1152
1153         .get_starget_node_name = qla2x00_get_starget_node_name,
1154         .show_starget_node_name = 1,
1155         .get_starget_port_name = qla2x00_get_starget_port_name,
1156         .show_starget_port_name = 1,
1157         .get_starget_port_id  = qla2x00_get_starget_port_id,
1158         .show_starget_port_id = 1,
1159
1160         .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
1161         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1162         .show_rport_dev_loss_tmo = 1,
1163
1164         .issue_fc_host_lip = qla2x00_issue_lip,
1165         .get_fc_host_stats = qla2x00_get_fc_host_stats,
1166 };
1167
1168 void
1169 qla2x00_init_host_attr(scsi_qla_host_t *ha)
1170 {
1171         fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
1172         fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
1173         fc_host_supported_classes(ha->host) = FC_COS_CLASS3;
1174         fc_host_max_npiv_vports(ha->host) = MAX_NUM_VPORT_FABRIC;
1175         fc_host_npiv_vports_inuse(ha->host) = ha->cur_vport_count;
1176 }