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