[SCSI] qla2xxx: Add reset capabilities for application support.
[safe/jmp/linux-2.6] / drivers / scsi / qla2xxx / qla_attr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 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 #include <linux/delay.h>
12
13 static int qla24xx_vport_disable(struct fc_vport *, bool);
14
15 /* SYSFS attributes --------------------------------------------------------- */
16
17 static ssize_t
18 qla2x00_sysfs_read_fw_dump(struct kobject *kobj,
19                            struct bin_attribute *bin_attr,
20                            char *buf, loff_t off, size_t count)
21 {
22         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
23             struct device, kobj)));
24         struct qla_hw_data *ha = vha->hw;
25
26         if (ha->fw_dump_reading == 0)
27                 return 0;
28
29         return memory_read_from_buffer(buf, count, &off, ha->fw_dump,
30                                         ha->fw_dump_len);
31 }
32
33 static ssize_t
34 qla2x00_sysfs_write_fw_dump(struct kobject *kobj,
35                             struct bin_attribute *bin_attr,
36                             char *buf, loff_t off, size_t count)
37 {
38         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
39             struct device, kobj)));
40         struct qla_hw_data *ha = vha->hw;
41         int reading;
42
43         if (off != 0)
44                 return (0);
45
46         reading = simple_strtol(buf, NULL, 10);
47         switch (reading) {
48         case 0:
49                 if (!ha->fw_dump_reading)
50                         break;
51
52                 qla_printk(KERN_INFO, ha,
53                     "Firmware dump cleared on (%ld).\n", vha->host_no);
54
55                 ha->fw_dump_reading = 0;
56                 ha->fw_dumped = 0;
57                 break;
58         case 1:
59                 if (ha->fw_dumped && !ha->fw_dump_reading) {
60                         ha->fw_dump_reading = 1;
61
62                         qla_printk(KERN_INFO, ha,
63                             "Raw firmware dump ready for read on (%ld).\n",
64                             vha->host_no);
65                 }
66                 break;
67         case 2:
68                 qla2x00_alloc_fw_dump(vha);
69                 break;
70         case 3:
71                 qla2x00_system_error(vha);
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 *vha = shost_priv(dev_to_shost(container_of(kobj,
93             struct device, kobj)));
94         struct qla_hw_data *ha = vha->hw;
95
96         if (!capable(CAP_SYS_ADMIN))
97                 return 0;
98
99         /* Read NVRAM data from cache. */
100         return memory_read_from_buffer(buf, count, &off, ha->nvram,
101                                         ha->nvram_size);
102 }
103
104 static ssize_t
105 qla2x00_sysfs_write_nvram(struct kobject *kobj,
106                           struct bin_attribute *bin_attr,
107                           char *buf, loff_t off, size_t count)
108 {
109         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
110             struct device, kobj)));
111         struct qla_hw_data *ha = vha->hw;
112         uint16_t        cnt;
113
114         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
115                 return 0;
116
117         /* Checksum NVRAM. */
118         if (IS_FWI2_CAPABLE(ha)) {
119                 uint32_t *iter;
120                 uint32_t chksum;
121
122                 iter = (uint32_t *)buf;
123                 chksum = 0;
124                 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
125                         chksum += le32_to_cpu(*iter++);
126                 chksum = ~chksum + 1;
127                 *iter = cpu_to_le32(chksum);
128         } else {
129                 uint8_t *iter;
130                 uint8_t chksum;
131
132                 iter = (uint8_t *)buf;
133                 chksum = 0;
134                 for (cnt = 0; cnt < count - 1; cnt++)
135                         chksum += *iter++;
136                 chksum = ~chksum + 1;
137                 *iter = chksum;
138         }
139
140         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
141                 qla_printk(KERN_WARNING, ha,
142                     "HBA not online, failing NVRAM update.\n");
143                 return -EAGAIN;
144         }
145
146         /* Write NVRAM. */
147         ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->nvram_base, count);
148         ha->isp_ops->read_nvram(vha, (uint8_t *)ha->nvram, ha->nvram_base,
149             count);
150
151         /* NVRAM settings take effect immediately. */
152         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
153         qla2xxx_wake_dpc(vha);
154         qla2x00_wait_for_chip_reset(vha);
155
156         return (count);
157 }
158
159 static struct bin_attribute sysfs_nvram_attr = {
160         .attr = {
161                 .name = "nvram",
162                 .mode = S_IRUSR | S_IWUSR,
163         },
164         .size = 512,
165         .read = qla2x00_sysfs_read_nvram,
166         .write = qla2x00_sysfs_write_nvram,
167 };
168
169 static ssize_t
170 qla2x00_sysfs_read_optrom(struct kobject *kobj,
171                           struct bin_attribute *bin_attr,
172                           char *buf, loff_t off, size_t count)
173 {
174         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
175             struct device, kobj)));
176         struct qla_hw_data *ha = vha->hw;
177
178         if (ha->optrom_state != QLA_SREADING)
179                 return 0;
180
181         return memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
182                                         ha->optrom_region_size);
183 }
184
185 static ssize_t
186 qla2x00_sysfs_write_optrom(struct kobject *kobj,
187                            struct bin_attribute *bin_attr,
188                            char *buf, loff_t off, size_t count)
189 {
190         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
191             struct device, kobj)));
192         struct qla_hw_data *ha = vha->hw;
193
194         if (ha->optrom_state != QLA_SWRITING)
195                 return -EINVAL;
196         if (off > ha->optrom_region_size)
197                 return -ERANGE;
198         if (off + count > ha->optrom_region_size)
199                 count = ha->optrom_region_size - off;
200
201         memcpy(&ha->optrom_buffer[off], buf, count);
202
203         return count;
204 }
205
206 static struct bin_attribute sysfs_optrom_attr = {
207         .attr = {
208                 .name = "optrom",
209                 .mode = S_IRUSR | S_IWUSR,
210         },
211         .size = 0,
212         .read = qla2x00_sysfs_read_optrom,
213         .write = qla2x00_sysfs_write_optrom,
214 };
215
216 static ssize_t
217 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj,
218                                struct bin_attribute *bin_attr,
219                                char *buf, loff_t off, size_t count)
220 {
221         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
222             struct device, kobj)));
223         struct qla_hw_data *ha = vha->hw;
224
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                 ha->optrom_region_start = start;
257                 ha->optrom_region_size = start + size > ha->optrom_size ?
258                     ha->optrom_size - start : size;
259
260                 ha->optrom_state = QLA_SREADING;
261                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
262                 if (ha->optrom_buffer == NULL) {
263                         qla_printk(KERN_WARNING, ha,
264                             "Unable to allocate memory for optrom retrieval "
265                             "(%x).\n", ha->optrom_region_size);
266
267                         ha->optrom_state = QLA_SWAITING;
268                         return count;
269                 }
270
271                 DEBUG2(qla_printk(KERN_INFO, ha,
272                     "Reading flash region -- 0x%x/0x%x.\n",
273                     ha->optrom_region_start, ha->optrom_region_size));
274
275                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
276                 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
277                     ha->optrom_region_start, ha->optrom_region_size);
278                 break;
279         case 2:
280                 if (ha->optrom_state != QLA_SWAITING)
281                         break;
282
283                 /*
284                  * We need to be more restrictive on which FLASH regions are
285                  * allowed to be updated via user-space.  Regions accessible
286                  * via this method include:
287                  *
288                  * ISP21xx/ISP22xx/ISP23xx type boards:
289                  *
290                  *      0x000000 -> 0x020000 -- Boot code.
291                  *
292                  * ISP2322/ISP24xx type boards:
293                  *
294                  *      0x000000 -> 0x07ffff -- Boot code.
295                  *      0x080000 -> 0x0fffff -- Firmware.
296                  *
297                  * ISP25xx type boards:
298                  *
299                  *      0x000000 -> 0x07ffff -- Boot code.
300                  *      0x080000 -> 0x0fffff -- Firmware.
301                  *      0x120000 -> 0x12ffff -- VPD and HBA parameters.
302                  */
303                 valid = 0;
304                 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
305                         valid = 1;
306                 else if (start == (ha->flt_region_boot * 4) ||
307                     start == (ha->flt_region_fw * 4))
308                         valid = 1;
309                 else if (IS_QLA25XX(ha) || IS_QLA81XX(ha))
310                     valid = 1;
311                 if (!valid) {
312                         qla_printk(KERN_WARNING, ha,
313                             "Invalid start region 0x%x/0x%x.\n", start, size);
314                         return -EINVAL;
315                 }
316
317                 ha->optrom_region_start = start;
318                 ha->optrom_region_size = start + size > ha->optrom_size ?
319                     ha->optrom_size - start : size;
320
321                 ha->optrom_state = QLA_SWRITING;
322                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
323                 if (ha->optrom_buffer == NULL) {
324                         qla_printk(KERN_WARNING, ha,
325                             "Unable to allocate memory for optrom update "
326                             "(%x).\n", ha->optrom_region_size);
327
328                         ha->optrom_state = QLA_SWAITING;
329                         return count;
330                 }
331
332                 DEBUG2(qla_printk(KERN_INFO, ha,
333                     "Staging flash region write -- 0x%x/0x%x.\n",
334                     ha->optrom_region_start, ha->optrom_region_size));
335
336                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
337                 break;
338         case 3:
339                 if (ha->optrom_state != QLA_SWRITING)
340                         break;
341
342                 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
343                         qla_printk(KERN_WARNING, ha,
344                             "HBA not online, failing flash update.\n");
345                         return -EAGAIN;
346                 }
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(vha, 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 *vha = shost_priv(dev_to_shost(container_of(kobj,
376             struct device, kobj)));
377         struct qla_hw_data *ha = vha->hw;
378
379         if (!capable(CAP_SYS_ADMIN))
380                 return 0;
381
382         /* Read NVRAM data from cache. */
383         return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
384 }
385
386 static ssize_t
387 qla2x00_sysfs_write_vpd(struct kobject *kobj,
388                         struct bin_attribute *bin_attr,
389                         char *buf, loff_t off, size_t count)
390 {
391         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
392             struct device, kobj)));
393         struct qla_hw_data *ha = vha->hw;
394
395         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
396                 return 0;
397
398         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
399                 qla_printk(KERN_WARNING, ha,
400                     "HBA not online, failing VPD update.\n");
401                 return -EAGAIN;
402         }
403
404         /* Write NVRAM. */
405         ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->vpd_base, count);
406         ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd, ha->vpd_base, count);
407
408         return count;
409 }
410
411 static struct bin_attribute sysfs_vpd_attr = {
412         .attr = {
413                 .name = "vpd",
414                 .mode = S_IRUSR | S_IWUSR,
415         },
416         .size = 0,
417         .read = qla2x00_sysfs_read_vpd,
418         .write = qla2x00_sysfs_write_vpd,
419 };
420
421 static ssize_t
422 qla2x00_sysfs_read_sfp(struct kobject *kobj,
423                        struct bin_attribute *bin_attr,
424                        char *buf, loff_t off, size_t count)
425 {
426         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
427             struct device, kobj)));
428         struct qla_hw_data *ha = vha->hw;
429         uint16_t iter, addr, offset;
430         int rval;
431
432         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
433                 return 0;
434
435         if (ha->sfp_data)
436                 goto do_read;
437
438         ha->sfp_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
439             &ha->sfp_data_dma);
440         if (!ha->sfp_data) {
441                 qla_printk(KERN_WARNING, ha,
442                     "Unable to allocate memory for SFP read-data.\n");
443                 return 0;
444         }
445
446 do_read:
447         memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
448         addr = 0xa0;
449         for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
450             iter++, offset += SFP_BLOCK_SIZE) {
451                 if (iter == 4) {
452                         /* Skip to next device address. */
453                         addr = 0xa2;
454                         offset = 0;
455                 }
456
457                 rval = qla2x00_read_sfp(vha, ha->sfp_data_dma, addr, offset,
458                     SFP_BLOCK_SIZE);
459                 if (rval != QLA_SUCCESS) {
460                         qla_printk(KERN_WARNING, ha,
461                             "Unable to read SFP data (%x/%x/%x).\n", rval,
462                             addr, offset);
463                         count = 0;
464                         break;
465                 }
466                 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
467                 buf += SFP_BLOCK_SIZE;
468         }
469
470         return count;
471 }
472
473 static struct bin_attribute sysfs_sfp_attr = {
474         .attr = {
475                 .name = "sfp",
476                 .mode = S_IRUSR | S_IWUSR,
477         },
478         .size = SFP_DEV_SIZE * 2,
479         .read = qla2x00_sysfs_read_sfp,
480 };
481
482 static ssize_t
483 qla2x00_sysfs_write_reset(struct kobject *kobj,
484                         struct bin_attribute *bin_attr,
485                         char *buf, loff_t off, size_t count)
486 {
487         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
488             struct device, kobj)));
489         struct qla_hw_data *ha = vha->hw;
490         int type;
491
492         if (off != 0)
493                 return 0;
494
495         type = simple_strtol(buf, NULL, 10);
496         switch (type) {
497         case 0x2025c:
498                 qla_printk(KERN_INFO, ha,
499                     "Issuing ISP reset on (%ld).\n", vha->host_no);
500
501                 scsi_block_requests(vha->host);
502                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
503                 qla2xxx_wake_dpc(vha);
504                 qla2x00_wait_for_chip_reset(vha);
505                 scsi_unblock_requests(vha->host);
506                 break;
507         case 0x2025d:
508                 if (!IS_QLA81XX(ha))
509                         break;
510
511                 qla_printk(KERN_INFO, ha,
512                     "Issuing MPI reset on (%ld).\n", vha->host_no);
513
514                 /* Make sure FC side is not in reset */
515                 qla2x00_wait_for_hba_online(vha);
516
517                 /* Issue MPI reset */
518                 scsi_block_requests(vha->host);
519                 if (qla81xx_restart_mpi_firmware(vha) != QLA_SUCCESS)
520                         qla_printk(KERN_WARNING, ha,
521                             "MPI reset failed on (%ld).\n", vha->host_no);
522                 scsi_unblock_requests(vha->host);
523                 break;
524         }
525         return count;
526 }
527
528 static struct bin_attribute sysfs_reset_attr = {
529         .attr = {
530                 .name = "reset",
531                 .mode = S_IWUSR,
532         },
533         .size = 0,
534         .write = qla2x00_sysfs_write_reset,
535 };
536
537 static struct sysfs_entry {
538         char *name;
539         struct bin_attribute *attr;
540         int is4GBp_only;
541 } bin_file_entries[] = {
542         { "fw_dump", &sysfs_fw_dump_attr, },
543         { "nvram", &sysfs_nvram_attr, },
544         { "optrom", &sysfs_optrom_attr, },
545         { "optrom_ctl", &sysfs_optrom_ctl_attr, },
546         { "vpd", &sysfs_vpd_attr, 1 },
547         { "sfp", &sysfs_sfp_attr, 1 },
548         { "reset", &sysfs_reset_attr, },
549         { NULL },
550 };
551
552 void
553 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *vha)
554 {
555         struct Scsi_Host *host = vha->host;
556         struct sysfs_entry *iter;
557         int ret;
558
559         for (iter = bin_file_entries; iter->name; iter++) {
560                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(vha->hw))
561                         continue;
562
563                 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
564                     iter->attr);
565                 if (ret)
566                         qla_printk(KERN_INFO, vha->hw,
567                             "Unable to create sysfs %s binary attribute "
568                             "(%d).\n", iter->name, ret);
569         }
570 }
571
572 void
573 qla2x00_free_sysfs_attr(scsi_qla_host_t *vha)
574 {
575         struct Scsi_Host *host = vha->host;
576         struct sysfs_entry *iter;
577         struct qla_hw_data *ha = vha->hw;
578
579         for (iter = bin_file_entries; iter->name; iter++) {
580                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
581                         continue;
582
583                 sysfs_remove_bin_file(&host->shost_gendev.kobj,
584                     iter->attr);
585         }
586
587         if (ha->beacon_blink_led == 1)
588                 ha->isp_ops->beacon_off(vha);
589 }
590
591 /* Scsi_Host attributes. */
592
593 static ssize_t
594 qla2x00_drvr_version_show(struct device *dev,
595                           struct device_attribute *attr, char *buf)
596 {
597         return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
598 }
599
600 static ssize_t
601 qla2x00_fw_version_show(struct device *dev,
602                         struct device_attribute *attr, char *buf)
603 {
604         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
605         struct qla_hw_data *ha = vha->hw;
606         char fw_str[128];
607
608         return snprintf(buf, PAGE_SIZE, "%s\n",
609             ha->isp_ops->fw_version_str(vha, fw_str));
610 }
611
612 static ssize_t
613 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
614                         char *buf)
615 {
616         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
617         struct qla_hw_data *ha = vha->hw;
618         uint32_t sn;
619
620         if (IS_FWI2_CAPABLE(ha)) {
621                 qla2xxx_get_vpd_field(vha, "SN", buf, PAGE_SIZE);
622                 return snprintf(buf, PAGE_SIZE, "%s\n", buf);
623         }
624
625         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
626         return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
627             sn % 100000);
628 }
629
630 static ssize_t
631 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
632                       char *buf)
633 {
634         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
635         return snprintf(buf, PAGE_SIZE, "ISP%04X\n", vha->hw->pdev->device);
636 }
637
638 static ssize_t
639 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
640                     char *buf)
641 {
642         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
643         struct qla_hw_data *ha = vha->hw;
644         return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
645             ha->product_id[0], ha->product_id[1], ha->product_id[2],
646             ha->product_id[3]);
647 }
648
649 static ssize_t
650 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
651                         char *buf)
652 {
653         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
654         return snprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_number);
655 }
656
657 static ssize_t
658 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
659                         char *buf)
660 {
661         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
662         return snprintf(buf, PAGE_SIZE, "%s\n",
663             vha->hw->model_desc ? vha->hw->model_desc : "");
664 }
665
666 static ssize_t
667 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
668                       char *buf)
669 {
670         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
671         char pci_info[30];
672
673         return snprintf(buf, PAGE_SIZE, "%s\n",
674             vha->hw->isp_ops->pci_info_str(vha, pci_info));
675 }
676
677 static ssize_t
678 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
679                         char *buf)
680 {
681         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
682         struct qla_hw_data *ha = vha->hw;
683         int len = 0;
684
685         if (atomic_read(&vha->loop_state) == LOOP_DOWN ||
686             atomic_read(&vha->loop_state) == LOOP_DEAD)
687                 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
688         else if (atomic_read(&vha->loop_state) != LOOP_READY ||
689             test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
690             test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
691                 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
692         else {
693                 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
694
695                 switch (ha->current_topology) {
696                 case ISP_CFG_NL:
697                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
698                         break;
699                 case ISP_CFG_FL:
700                         len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
701                         break;
702                 case ISP_CFG_N:
703                         len += snprintf(buf + len, PAGE_SIZE-len,
704                             "N_Port to N_Port\n");
705                         break;
706                 case ISP_CFG_F:
707                         len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
708                         break;
709                 default:
710                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
711                         break;
712                 }
713         }
714         return len;
715 }
716
717 static ssize_t
718 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
719                  char *buf)
720 {
721         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
722         int len = 0;
723
724         switch (vha->hw->zio_mode) {
725         case QLA_ZIO_MODE_6:
726                 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
727                 break;
728         case QLA_ZIO_DISABLED:
729                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
730                 break;
731         }
732         return len;
733 }
734
735 static ssize_t
736 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
737                   const char *buf, size_t count)
738 {
739         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
740         struct qla_hw_data *ha = vha->hw;
741         int val = 0;
742         uint16_t zio_mode;
743
744         if (!IS_ZIO_SUPPORTED(ha))
745                 return -ENOTSUPP;
746
747         if (sscanf(buf, "%d", &val) != 1)
748                 return -EINVAL;
749
750         if (val)
751                 zio_mode = QLA_ZIO_MODE_6;
752         else
753                 zio_mode = QLA_ZIO_DISABLED;
754
755         /* Update per-hba values and queue a reset. */
756         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
757                 ha->zio_mode = zio_mode;
758                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
759         }
760         return strlen(buf);
761 }
762
763 static ssize_t
764 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
765                        char *buf)
766 {
767         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
768
769         return snprintf(buf, PAGE_SIZE, "%d us\n", vha->hw->zio_timer * 100);
770 }
771
772 static ssize_t
773 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
774                         const char *buf, size_t count)
775 {
776         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
777         int val = 0;
778         uint16_t zio_timer;
779
780         if (sscanf(buf, "%d", &val) != 1)
781                 return -EINVAL;
782         if (val > 25500 || val < 100)
783                 return -ERANGE;
784
785         zio_timer = (uint16_t)(val / 100);
786         vha->hw->zio_timer = zio_timer;
787
788         return strlen(buf);
789 }
790
791 static ssize_t
792 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
793                     char *buf)
794 {
795         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
796         int len = 0;
797
798         if (vha->hw->beacon_blink_led)
799                 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
800         else
801                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
802         return len;
803 }
804
805 static ssize_t
806 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
807                      const char *buf, size_t count)
808 {
809         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
810         struct qla_hw_data *ha = vha->hw;
811         int val = 0;
812         int rval;
813
814         if (IS_QLA2100(ha) || IS_QLA2200(ha))
815                 return -EPERM;
816
817         if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
818                 qla_printk(KERN_WARNING, ha,
819                     "Abort ISP active -- ignoring beacon request.\n");
820                 return -EBUSY;
821         }
822
823         if (sscanf(buf, "%d", &val) != 1)
824                 return -EINVAL;
825
826         if (val)
827                 rval = ha->isp_ops->beacon_on(vha);
828         else
829                 rval = ha->isp_ops->beacon_off(vha);
830
831         if (rval != QLA_SUCCESS)
832                 count = 0;
833
834         return count;
835 }
836
837 static ssize_t
838 qla2x00_optrom_bios_version_show(struct device *dev,
839                                  struct device_attribute *attr, char *buf)
840 {
841         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
842         struct qla_hw_data *ha = vha->hw;
843         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
844             ha->bios_revision[0]);
845 }
846
847 static ssize_t
848 qla2x00_optrom_efi_version_show(struct device *dev,
849                                 struct device_attribute *attr, char *buf)
850 {
851         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
852         struct qla_hw_data *ha = vha->hw;
853         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
854             ha->efi_revision[0]);
855 }
856
857 static ssize_t
858 qla2x00_optrom_fcode_version_show(struct device *dev,
859                                   struct device_attribute *attr, char *buf)
860 {
861         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
862         struct qla_hw_data *ha = vha->hw;
863         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
864             ha->fcode_revision[0]);
865 }
866
867 static ssize_t
868 qla2x00_optrom_fw_version_show(struct device *dev,
869                                struct device_attribute *attr, char *buf)
870 {
871         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
872         struct qla_hw_data *ha = vha->hw;
873         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
874             ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
875             ha->fw_revision[3]);
876 }
877
878 static ssize_t
879 qla2x00_total_isp_aborts_show(struct device *dev,
880                               struct device_attribute *attr, char *buf)
881 {
882         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
883         struct qla_hw_data *ha = vha->hw;
884         return snprintf(buf, PAGE_SIZE, "%d\n",
885             ha->qla_stats.total_isp_aborts);
886 }
887
888 static ssize_t
889 qla2x00_mpi_version_show(struct device *dev, struct device_attribute *attr,
890     char *buf)
891 {
892         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
893         struct qla_hw_data *ha = vha->hw;
894
895         if (!IS_QLA81XX(ha))
896                 return snprintf(buf, PAGE_SIZE, "\n");
897
898         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
899             ha->mpi_version[0], ha->mpi_version[1], ha->mpi_version[2],
900             ha->mpi_capabilities);
901 }
902
903 static ssize_t
904 qla2x00_phy_version_show(struct device *dev, struct device_attribute *attr,
905     char *buf)
906 {
907         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
908         struct qla_hw_data *ha = vha->hw;
909
910         if (!IS_QLA81XX(ha))
911                 return snprintf(buf, PAGE_SIZE, "\n");
912
913         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
914             ha->phy_version[0], ha->phy_version[1], ha->phy_version[2]);
915 }
916
917 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show, NULL);
918 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
919 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
920 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
921 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
922 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
923 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
924 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
925 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
926 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
927 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
928                    qla2x00_zio_timer_store);
929 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
930                    qla2x00_beacon_store);
931 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
932                    qla2x00_optrom_bios_version_show, NULL);
933 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
934                    qla2x00_optrom_efi_version_show, NULL);
935 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
936                    qla2x00_optrom_fcode_version_show, NULL);
937 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
938                    NULL);
939 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
940                    NULL);
941 static DEVICE_ATTR(mpi_version, S_IRUGO, qla2x00_mpi_version_show, NULL);
942 static DEVICE_ATTR(phy_version, S_IRUGO, qla2x00_phy_version_show, NULL);
943
944 struct device_attribute *qla2x00_host_attrs[] = {
945         &dev_attr_driver_version,
946         &dev_attr_fw_version,
947         &dev_attr_serial_num,
948         &dev_attr_isp_name,
949         &dev_attr_isp_id,
950         &dev_attr_model_name,
951         &dev_attr_model_desc,
952         &dev_attr_pci_info,
953         &dev_attr_link_state,
954         &dev_attr_zio,
955         &dev_attr_zio_timer,
956         &dev_attr_beacon,
957         &dev_attr_optrom_bios_version,
958         &dev_attr_optrom_efi_version,
959         &dev_attr_optrom_fcode_version,
960         &dev_attr_optrom_fw_version,
961         &dev_attr_total_isp_aborts,
962         &dev_attr_mpi_version,
963         &dev_attr_phy_version,
964         NULL,
965 };
966
967 /* Host attributes. */
968
969 static void
970 qla2x00_get_host_port_id(struct Scsi_Host *shost)
971 {
972         scsi_qla_host_t *vha = shost_priv(shost);
973
974         fc_host_port_id(shost) = vha->d_id.b.domain << 16 |
975             vha->d_id.b.area << 8 | vha->d_id.b.al_pa;
976 }
977
978 static void
979 qla2x00_get_host_speed(struct Scsi_Host *shost)
980 {
981         struct qla_hw_data *ha = ((struct scsi_qla_host *)
982                                         (shost_priv(shost)))->hw;
983         u32 speed = FC_PORTSPEED_UNKNOWN;
984
985         switch (ha->link_data_rate) {
986         case PORT_SPEED_1GB:
987                 speed = FC_PORTSPEED_1GBIT;
988                 break;
989         case PORT_SPEED_2GB:
990                 speed = FC_PORTSPEED_2GBIT;
991                 break;
992         case PORT_SPEED_4GB:
993                 speed = FC_PORTSPEED_4GBIT;
994                 break;
995         case PORT_SPEED_8GB:
996                 speed = FC_PORTSPEED_8GBIT;
997                 break;
998         case PORT_SPEED_10GB:
999                 speed = FC_PORTSPEED_10GBIT;
1000                 break;
1001         }
1002         fc_host_speed(shost) = speed;
1003 }
1004
1005 static void
1006 qla2x00_get_host_port_type(struct Scsi_Host *shost)
1007 {
1008         scsi_qla_host_t *vha = shost_priv(shost);
1009         uint32_t port_type = FC_PORTTYPE_UNKNOWN;
1010
1011         if (vha->vp_idx) {
1012                 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
1013                 return;
1014         }
1015         switch (vha->hw->current_topology) {
1016         case ISP_CFG_NL:
1017                 port_type = FC_PORTTYPE_LPORT;
1018                 break;
1019         case ISP_CFG_FL:
1020                 port_type = FC_PORTTYPE_NLPORT;
1021                 break;
1022         case ISP_CFG_N:
1023                 port_type = FC_PORTTYPE_PTP;
1024                 break;
1025         case ISP_CFG_F:
1026                 port_type = FC_PORTTYPE_NPORT;
1027                 break;
1028         }
1029         fc_host_port_type(shost) = port_type;
1030 }
1031
1032 static void
1033 qla2x00_get_starget_node_name(struct scsi_target *starget)
1034 {
1035         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1036         scsi_qla_host_t *vha = shost_priv(host);
1037         fc_port_t *fcport;
1038         u64 node_name = 0;
1039
1040         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1041                 if (fcport->rport &&
1042                     starget->id == fcport->rport->scsi_target_id) {
1043                         node_name = wwn_to_u64(fcport->node_name);
1044                         break;
1045                 }
1046         }
1047
1048         fc_starget_node_name(starget) = node_name;
1049 }
1050
1051 static void
1052 qla2x00_get_starget_port_name(struct scsi_target *starget)
1053 {
1054         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1055         scsi_qla_host_t *vha = shost_priv(host);
1056         fc_port_t *fcport;
1057         u64 port_name = 0;
1058
1059         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1060                 if (fcport->rport &&
1061                     starget->id == fcport->rport->scsi_target_id) {
1062                         port_name = wwn_to_u64(fcport->port_name);
1063                         break;
1064                 }
1065         }
1066
1067         fc_starget_port_name(starget) = port_name;
1068 }
1069
1070 static void
1071 qla2x00_get_starget_port_id(struct scsi_target *starget)
1072 {
1073         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1074         scsi_qla_host_t *vha = shost_priv(host);
1075         fc_port_t *fcport;
1076         uint32_t port_id = ~0U;
1077
1078         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1079                 if (fcport->rport &&
1080                     starget->id == fcport->rport->scsi_target_id) {
1081                         port_id = fcport->d_id.b.domain << 16 |
1082                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
1083                         break;
1084                 }
1085         }
1086
1087         fc_starget_port_id(starget) = port_id;
1088 }
1089
1090 static void
1091 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1092 {
1093         if (timeout)
1094                 rport->dev_loss_tmo = timeout;
1095         else
1096                 rport->dev_loss_tmo = 1;
1097 }
1098
1099 static void
1100 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
1101 {
1102         struct Scsi_Host *host = rport_to_shost(rport);
1103         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1104
1105         if (!fcport)
1106                 return;
1107
1108         qla2x00_abort_fcport_cmds(fcport);
1109
1110         /*
1111          * Transport has effectively 'deleted' the rport, clear
1112          * all local references.
1113          */
1114         spin_lock_irq(host->host_lock);
1115         fcport->rport = NULL;
1116         *((fc_port_t **)rport->dd_data) = NULL;
1117         spin_unlock_irq(host->host_lock);
1118 }
1119
1120 static void
1121 qla2x00_terminate_rport_io(struct fc_rport *rport)
1122 {
1123         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1124
1125         if (!fcport)
1126                 return;
1127
1128         /*
1129          * At this point all fcport's software-states are cleared.  Perform any
1130          * final cleanup of firmware resources (PCBs and XCBs).
1131          */
1132         if (fcport->loop_id != FC_NO_LOOP_ID)
1133                 fcport->vha->hw->isp_ops->fabric_logout(fcport->vha,
1134                         fcport->loop_id, fcport->d_id.b.domain,
1135                         fcport->d_id.b.area, fcport->d_id.b.al_pa);
1136
1137         qla2x00_abort_fcport_cmds(fcport);
1138 }
1139
1140 static int
1141 qla2x00_issue_lip(struct Scsi_Host *shost)
1142 {
1143         scsi_qla_host_t *vha = shost_priv(shost);
1144
1145         qla2x00_loop_reset(vha);
1146         return 0;
1147 }
1148
1149 static struct fc_host_statistics *
1150 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
1151 {
1152         scsi_qla_host_t *vha = shost_priv(shost);
1153         struct qla_hw_data *ha = vha->hw;
1154         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
1155         int rval;
1156         struct link_statistics *stats;
1157         dma_addr_t stats_dma;
1158         struct fc_host_statistics *pfc_host_stat;
1159
1160         pfc_host_stat = &ha->fc_host_stat;
1161         memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
1162
1163         stats = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &stats_dma);
1164         if (stats == NULL) {
1165                 DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
1166                     __func__, base_vha->host_no));
1167                 goto done;
1168         }
1169         memset(stats, 0, DMA_POOL_SIZE);
1170
1171         rval = QLA_FUNCTION_FAILED;
1172         if (IS_FWI2_CAPABLE(ha)) {
1173                 rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma);
1174         } else if (atomic_read(&base_vha->loop_state) == LOOP_READY &&
1175                     !test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) &&
1176                     !test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
1177                     !ha->dpc_active) {
1178                 /* Must be in a 'READY' state for statistics retrieval. */
1179                 rval = qla2x00_get_link_status(base_vha, base_vha->loop_id,
1180                                                 stats, stats_dma);
1181         }
1182
1183         if (rval != QLA_SUCCESS)
1184                 goto done_free;
1185
1186         pfc_host_stat->link_failure_count = stats->link_fail_cnt;
1187         pfc_host_stat->loss_of_sync_count = stats->loss_sync_cnt;
1188         pfc_host_stat->loss_of_signal_count = stats->loss_sig_cnt;
1189         pfc_host_stat->prim_seq_protocol_err_count = stats->prim_seq_err_cnt;
1190         pfc_host_stat->invalid_tx_word_count = stats->inval_xmit_word_cnt;
1191         pfc_host_stat->invalid_crc_count = stats->inval_crc_cnt;
1192         if (IS_FWI2_CAPABLE(ha)) {
1193                 pfc_host_stat->lip_count = stats->lip_cnt;
1194                 pfc_host_stat->tx_frames = stats->tx_frames;
1195                 pfc_host_stat->rx_frames = stats->rx_frames;
1196                 pfc_host_stat->dumped_frames = stats->dumped_frames;
1197                 pfc_host_stat->nos_count = stats->nos_rcvd;
1198         }
1199         pfc_host_stat->fcp_input_megabytes = ha->qla_stats.input_bytes >> 20;
1200         pfc_host_stat->fcp_output_megabytes = ha->qla_stats.output_bytes >> 20;
1201
1202 done_free:
1203         dma_pool_free(ha->s_dma_pool, stats, stats_dma);
1204 done:
1205         return pfc_host_stat;
1206 }
1207
1208 static void
1209 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
1210 {
1211         scsi_qla_host_t *vha = shost_priv(shost);
1212
1213         qla2x00_get_sym_node_name(vha, fc_host_symbolic_name(shost));
1214 }
1215
1216 static void
1217 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
1218 {
1219         scsi_qla_host_t *vha = shost_priv(shost);
1220
1221         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
1222 }
1223
1224 static void
1225 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
1226 {
1227         scsi_qla_host_t *vha = shost_priv(shost);
1228         u64 node_name;
1229
1230         if (vha->device_flags & SWITCH_FOUND)
1231                 node_name = wwn_to_u64(vha->fabric_node_name);
1232         else
1233                 node_name = wwn_to_u64(vha->node_name);
1234
1235         fc_host_fabric_name(shost) = node_name;
1236 }
1237
1238 static void
1239 qla2x00_get_host_port_state(struct Scsi_Host *shost)
1240 {
1241         scsi_qla_host_t *vha = shost_priv(shost);
1242         struct scsi_qla_host *base_vha = pci_get_drvdata(vha->hw->pdev);
1243
1244         if (!base_vha->flags.online)
1245                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1246         else if (atomic_read(&base_vha->loop_state) == LOOP_TIMEOUT)
1247                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1248         else
1249                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1250 }
1251
1252 static int
1253 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
1254 {
1255         int     ret = 0;
1256         int     cnt = 0;
1257         uint8_t qos = QLA_DEFAULT_QUE_QOS;
1258         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
1259         scsi_qla_host_t *vha = NULL;
1260         struct qla_hw_data *ha = base_vha->hw;
1261
1262         ret = qla24xx_vport_create_req_sanity_check(fc_vport);
1263         if (ret) {
1264                 DEBUG15(printk("qla24xx_vport_create_req_sanity_check failed, "
1265                     "status %x\n", ret));
1266                 return (ret);
1267         }
1268
1269         vha = qla24xx_create_vhost(fc_vport);
1270         if (vha == NULL) {
1271                 DEBUG15(printk ("qla24xx_create_vhost failed, vha = %p\n",
1272                     vha));
1273                 return FC_VPORT_FAILED;
1274         }
1275         if (disable) {
1276                 atomic_set(&vha->vp_state, VP_OFFLINE);
1277                 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
1278         } else
1279                 atomic_set(&vha->vp_state, VP_FAILED);
1280
1281         /* ready to create vport */
1282         qla_printk(KERN_INFO, vha->hw, "VP entry id %d assigned.\n",
1283                                                         vha->vp_idx);
1284
1285         /* initialized vport states */
1286         atomic_set(&vha->loop_state, LOOP_DOWN);
1287         vha->vp_err_state=  VP_ERR_PORTDWN;
1288         vha->vp_prev_err_state=  VP_ERR_UNKWN;
1289         /* Check if physical ha port is Up */
1290         if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
1291             atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
1292                 /* Don't retry or attempt login of this virtual port */
1293                 DEBUG15(printk ("scsi(%ld): pport loop_state is not UP.\n",
1294                     base_vha->host_no));
1295                 atomic_set(&vha->loop_state, LOOP_DEAD);
1296                 if (!disable)
1297                         fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
1298         }
1299
1300         if (scsi_add_host(vha->host, &fc_vport->dev)) {
1301                 DEBUG15(printk("scsi(%ld): scsi_add_host failure for VP[%d].\n",
1302                         vha->host_no, vha->vp_idx));
1303                 goto vport_create_failed_2;
1304         }
1305
1306         /* initialize attributes */
1307         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1308         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1309         fc_host_supported_classes(vha->host) =
1310                 fc_host_supported_classes(base_vha->host);
1311         fc_host_supported_speeds(vha->host) =
1312                 fc_host_supported_speeds(base_vha->host);
1313
1314         qla24xx_vport_disable(fc_vport, disable);
1315
1316         /* Create a queue pair for the vport */
1317         if (ha->mqenable) {
1318                 if (ha->npiv_info) {
1319                         for (; cnt < ha->nvram_npiv_size; cnt++) {
1320                                 if (ha->npiv_info[cnt].port_name ==
1321                                         vha->port_name &&
1322                                         ha->npiv_info[cnt].node_name ==
1323                                         vha->node_name) {
1324                                         qos = ha->npiv_info[cnt].q_qos;
1325                                         break;
1326                                 }
1327                         }
1328                 }
1329                 qla25xx_create_queues(vha, qos);
1330         }
1331
1332         return 0;
1333 vport_create_failed_2:
1334         qla24xx_disable_vp(vha);
1335         qla24xx_deallocate_vp_id(vha);
1336         scsi_host_put(vha->host);
1337         return FC_VPORT_FAILED;
1338 }
1339
1340 static int
1341 qla24xx_vport_delete(struct fc_vport *fc_vport)
1342 {
1343         scsi_qla_host_t *vha = fc_vport->dd_data;
1344         fc_port_t *fcport, *tfcport;
1345         struct qla_hw_data *ha = vha->hw;
1346         uint16_t id = vha->vp_idx;
1347
1348         while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
1349             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags))
1350                 msleep(1000);
1351
1352         qla24xx_disable_vp(vha);
1353
1354         fc_remove_host(vha->host);
1355
1356         scsi_remove_host(vha->host);
1357
1358         list_for_each_entry_safe(fcport, tfcport, &vha->vp_fcports, list) {
1359                 list_del(&fcport->list);
1360                 kfree(fcport);
1361                 fcport = NULL;
1362         }
1363
1364         qla24xx_deallocate_vp_id(vha);
1365
1366         if (vha->timer_active) {
1367                 qla2x00_vp_stop_timer(vha);
1368                 DEBUG15(printk ("scsi(%ld): timer for the vport[%d] = %p "
1369                     "has stopped\n",
1370                     vha->host_no, vha->vp_idx, vha));
1371         }
1372
1373         if (ha->mqenable) {
1374                 if (qla25xx_delete_queues(vha, 0) != QLA_SUCCESS)
1375                         qla_printk(KERN_WARNING, ha,
1376                                 "Queue delete failed.\n");
1377         }
1378
1379         scsi_host_put(vha->host);
1380         qla_printk(KERN_INFO, ha, "vport %d deleted\n", id);
1381         return 0;
1382 }
1383
1384 static int
1385 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
1386 {
1387         scsi_qla_host_t *vha = fc_vport->dd_data;
1388
1389         if (disable)
1390                 qla24xx_disable_vp(vha);
1391         else
1392                 qla24xx_enable_vp(vha);
1393
1394         return 0;
1395 }
1396
1397 struct fc_function_template qla2xxx_transport_functions = {
1398
1399         .show_host_node_name = 1,
1400         .show_host_port_name = 1,
1401         .show_host_supported_classes = 1,
1402         .show_host_supported_speeds = 1,
1403
1404         .get_host_port_id = qla2x00_get_host_port_id,
1405         .show_host_port_id = 1,
1406         .get_host_speed = qla2x00_get_host_speed,
1407         .show_host_speed = 1,
1408         .get_host_port_type = qla2x00_get_host_port_type,
1409         .show_host_port_type = 1,
1410         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1411         .show_host_symbolic_name = 1,
1412         .set_host_system_hostname = qla2x00_set_host_system_hostname,
1413         .show_host_system_hostname = 1,
1414         .get_host_fabric_name = qla2x00_get_host_fabric_name,
1415         .show_host_fabric_name = 1,
1416         .get_host_port_state = qla2x00_get_host_port_state,
1417         .show_host_port_state = 1,
1418
1419         .dd_fcrport_size = sizeof(struct fc_port *),
1420         .show_rport_supported_classes = 1,
1421
1422         .get_starget_node_name = qla2x00_get_starget_node_name,
1423         .show_starget_node_name = 1,
1424         .get_starget_port_name = qla2x00_get_starget_port_name,
1425         .show_starget_port_name = 1,
1426         .get_starget_port_id  = qla2x00_get_starget_port_id,
1427         .show_starget_port_id = 1,
1428
1429         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1430         .show_rport_dev_loss_tmo = 1,
1431
1432         .issue_fc_host_lip = qla2x00_issue_lip,
1433         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
1434         .terminate_rport_io = qla2x00_terminate_rport_io,
1435         .get_fc_host_stats = qla2x00_get_fc_host_stats,
1436
1437         .vport_create = qla24xx_vport_create,
1438         .vport_disable = qla24xx_vport_disable,
1439         .vport_delete = qla24xx_vport_delete,
1440 };
1441
1442 struct fc_function_template qla2xxx_transport_vport_functions = {
1443
1444         .show_host_node_name = 1,
1445         .show_host_port_name = 1,
1446         .show_host_supported_classes = 1,
1447
1448         .get_host_port_id = qla2x00_get_host_port_id,
1449         .show_host_port_id = 1,
1450         .get_host_speed = qla2x00_get_host_speed,
1451         .show_host_speed = 1,
1452         .get_host_port_type = qla2x00_get_host_port_type,
1453         .show_host_port_type = 1,
1454         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1455         .show_host_symbolic_name = 1,
1456         .set_host_system_hostname = qla2x00_set_host_system_hostname,
1457         .show_host_system_hostname = 1,
1458         .get_host_fabric_name = qla2x00_get_host_fabric_name,
1459         .show_host_fabric_name = 1,
1460         .get_host_port_state = qla2x00_get_host_port_state,
1461         .show_host_port_state = 1,
1462
1463         .dd_fcrport_size = sizeof(struct fc_port *),
1464         .show_rport_supported_classes = 1,
1465
1466         .get_starget_node_name = qla2x00_get_starget_node_name,
1467         .show_starget_node_name = 1,
1468         .get_starget_port_name = qla2x00_get_starget_port_name,
1469         .show_starget_port_name = 1,
1470         .get_starget_port_id  = qla2x00_get_starget_port_id,
1471         .show_starget_port_id = 1,
1472
1473         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1474         .show_rport_dev_loss_tmo = 1,
1475
1476         .issue_fc_host_lip = qla2x00_issue_lip,
1477         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
1478         .terminate_rport_io = qla2x00_terminate_rport_io,
1479         .get_fc_host_stats = qla2x00_get_fc_host_stats,
1480 };
1481
1482 void
1483 qla2x00_init_host_attr(scsi_qla_host_t *vha)
1484 {
1485         struct qla_hw_data *ha = vha->hw;
1486         u32 speed = FC_PORTSPEED_UNKNOWN;
1487
1488         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1489         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1490         fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
1491         fc_host_max_npiv_vports(vha->host) = ha->max_npiv_vports;
1492         fc_host_npiv_vports_inuse(vha->host) = ha->cur_vport_count;
1493
1494         if (IS_QLA81XX(ha))
1495                 speed = FC_PORTSPEED_10GBIT;
1496         else if (IS_QLA25XX(ha))
1497                 speed = FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT |
1498                     FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1499         else if (IS_QLA24XX_TYPE(ha))
1500                 speed = FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
1501                     FC_PORTSPEED_1GBIT;
1502         else if (IS_QLA23XX(ha))
1503                 speed = FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1504         else
1505                 speed = FC_PORTSPEED_1GBIT;
1506         fc_host_supported_speeds(vha->host) = speed;
1507 }