[SCSI] qla2xxx: Perform implicit logout during rport tear-down.
[linux-2.6.git] / drivers / scsi / qla2xxx / qla_attr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2011 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/slab.h>
12 #include <linux/delay.h>
13
14 static int qla24xx_vport_disable(struct fc_vport *, bool);
15
16 /* SYSFS attributes --------------------------------------------------------- */
17
18 static ssize_t
19 qla2x00_sysfs_read_fw_dump(struct file *filp, struct kobject *kobj,
20                            struct bin_attribute *bin_attr,
21                            char *buf, loff_t off, size_t count)
22 {
23         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
24             struct device, kobj)));
25         struct qla_hw_data *ha = vha->hw;
26         int rval = 0;
27
28         if (ha->fw_dump_reading == 0)
29                 return 0;
30
31         if (IS_QLA82XX(ha)) {
32                 if (off < ha->md_template_size) {
33                         rval = memory_read_from_buffer(buf, count,
34                             &off, ha->md_tmplt_hdr, ha->md_template_size);
35                         return rval;
36                 }
37                 off -= ha->md_template_size;
38                 rval = memory_read_from_buffer(buf, count,
39                     &off, ha->md_dump, ha->md_dump_size);
40                 return rval;
41         } else
42                 return memory_read_from_buffer(buf, count, &off, ha->fw_dump,
43                                         ha->fw_dump_len);
44 }
45
46 static ssize_t
47 qla2x00_sysfs_write_fw_dump(struct file *filp, struct kobject *kobj,
48                             struct bin_attribute *bin_attr,
49                             char *buf, loff_t off, size_t count)
50 {
51         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
52             struct device, kobj)));
53         struct qla_hw_data *ha = vha->hw;
54         int reading;
55
56         if (off != 0)
57                 return (0);
58
59         reading = simple_strtol(buf, NULL, 10);
60         switch (reading) {
61         case 0:
62                 if (!ha->fw_dump_reading)
63                         break;
64
65                 ql_log(ql_log_info, vha, 0x705d,
66                     "Firmware dump cleared on (%ld).\n", vha->host_no);
67
68                 if (IS_QLA82XX(vha->hw)) {
69                         qla82xx_md_free(vha);
70                         qla82xx_md_prep(vha);
71                 }
72                 ha->fw_dump_reading = 0;
73                 ha->fw_dumped = 0;
74                 break;
75         case 1:
76                 if (ha->fw_dumped && !ha->fw_dump_reading) {
77                         ha->fw_dump_reading = 1;
78
79                         ql_log(ql_log_info, vha, 0x705e,
80                             "Raw firmware dump ready for read on (%ld).\n",
81                             vha->host_no);
82                 }
83                 break;
84         case 2:
85                 qla2x00_alloc_fw_dump(vha);
86                 break;
87         case 3:
88                 if (IS_QLA82XX(ha)) {
89                         qla82xx_idc_lock(ha);
90                         qla82xx_set_reset_owner(vha);
91                         qla82xx_idc_unlock(ha);
92                 } else
93                         qla2x00_system_error(vha);
94                 break;
95         case 4:
96                 if (IS_QLA82XX(ha)) {
97                         if (ha->md_tmplt_hdr)
98                                 ql_dbg(ql_dbg_user, vha, 0x705b,
99                                     "MiniDump supported with this firmware.\n");
100                         else
101                                 ql_dbg(ql_dbg_user, vha, 0x709d,
102                                     "MiniDump not supported with this firmware.\n");
103                 }
104                 break;
105         case 5:
106                 if (IS_QLA82XX(ha))
107                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
108                 break;
109         }
110         return count;
111 }
112
113 static struct bin_attribute sysfs_fw_dump_attr = {
114         .attr = {
115                 .name = "fw_dump",
116                 .mode = S_IRUSR | S_IWUSR,
117         },
118         .size = 0,
119         .read = qla2x00_sysfs_read_fw_dump,
120         .write = qla2x00_sysfs_write_fw_dump,
121 };
122
123 static ssize_t
124 qla2x00_sysfs_read_nvram(struct file *filp, struct kobject *kobj,
125                          struct bin_attribute *bin_attr,
126                          char *buf, loff_t off, size_t count)
127 {
128         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
129             struct device, kobj)));
130         struct qla_hw_data *ha = vha->hw;
131
132         if (!capable(CAP_SYS_ADMIN))
133                 return 0;
134
135         if (IS_NOCACHE_VPD_TYPE(ha))
136                 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
137                     ha->nvram_size);
138         return memory_read_from_buffer(buf, count, &off, ha->nvram,
139                                         ha->nvram_size);
140 }
141
142 static ssize_t
143 qla2x00_sysfs_write_nvram(struct file *filp, struct kobject *kobj,
144                           struct bin_attribute *bin_attr,
145                           char *buf, loff_t off, size_t count)
146 {
147         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
148             struct device, kobj)));
149         struct qla_hw_data *ha = vha->hw;
150         uint16_t        cnt;
151
152         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size ||
153             !ha->isp_ops->write_nvram)
154                 return -EINVAL;
155
156         /* Checksum NVRAM. */
157         if (IS_FWI2_CAPABLE(ha)) {
158                 uint32_t *iter;
159                 uint32_t chksum;
160
161                 iter = (uint32_t *)buf;
162                 chksum = 0;
163                 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
164                         chksum += le32_to_cpu(*iter++);
165                 chksum = ~chksum + 1;
166                 *iter = cpu_to_le32(chksum);
167         } else {
168                 uint8_t *iter;
169                 uint8_t chksum;
170
171                 iter = (uint8_t *)buf;
172                 chksum = 0;
173                 for (cnt = 0; cnt < count - 1; cnt++)
174                         chksum += *iter++;
175                 chksum = ~chksum + 1;
176                 *iter = chksum;
177         }
178
179         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
180                 ql_log(ql_log_warn, vha, 0x705f,
181                     "HBA not online, failing NVRAM update.\n");
182                 return -EAGAIN;
183         }
184
185         /* Write NVRAM. */
186         ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->nvram_base, count);
187         ha->isp_ops->read_nvram(vha, (uint8_t *)ha->nvram, ha->nvram_base,
188             count);
189
190         ql_dbg(ql_dbg_user, vha, 0x7060,
191             "Setting ISP_ABORT_NEEDED\n");
192         /* NVRAM settings take effect immediately. */
193         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
194         qla2xxx_wake_dpc(vha);
195         qla2x00_wait_for_chip_reset(vha);
196
197         return count;
198 }
199
200 static struct bin_attribute sysfs_nvram_attr = {
201         .attr = {
202                 .name = "nvram",
203                 .mode = S_IRUSR | S_IWUSR,
204         },
205         .size = 512,
206         .read = qla2x00_sysfs_read_nvram,
207         .write = qla2x00_sysfs_write_nvram,
208 };
209
210 static ssize_t
211 qla2x00_sysfs_read_optrom(struct file *filp, struct kobject *kobj,
212                           struct bin_attribute *bin_attr,
213                           char *buf, loff_t off, size_t count)
214 {
215         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
216             struct device, kobj)));
217         struct qla_hw_data *ha = vha->hw;
218
219         if (ha->optrom_state != QLA_SREADING)
220                 return 0;
221
222         return memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
223                                         ha->optrom_region_size);
224 }
225
226 static ssize_t
227 qla2x00_sysfs_write_optrom(struct file *filp, struct kobject *kobj,
228                            struct bin_attribute *bin_attr,
229                            char *buf, loff_t off, size_t count)
230 {
231         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
232             struct device, kobj)));
233         struct qla_hw_data *ha = vha->hw;
234
235         if (ha->optrom_state != QLA_SWRITING)
236                 return -EINVAL;
237         if (off > ha->optrom_region_size)
238                 return -ERANGE;
239         if (off + count > ha->optrom_region_size)
240                 count = ha->optrom_region_size - off;
241
242         memcpy(&ha->optrom_buffer[off], buf, count);
243
244         return count;
245 }
246
247 static struct bin_attribute sysfs_optrom_attr = {
248         .attr = {
249                 .name = "optrom",
250                 .mode = S_IRUSR | S_IWUSR,
251         },
252         .size = 0,
253         .read = qla2x00_sysfs_read_optrom,
254         .write = qla2x00_sysfs_write_optrom,
255 };
256
257 static ssize_t
258 qla2x00_sysfs_write_optrom_ctl(struct file *filp, struct kobject *kobj,
259                                struct bin_attribute *bin_attr,
260                                char *buf, loff_t off, size_t count)
261 {
262         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
263             struct device, kobj)));
264         struct qla_hw_data *ha = vha->hw;
265
266         uint32_t start = 0;
267         uint32_t size = ha->optrom_size;
268         int val, valid;
269
270         if (off)
271                 return -EINVAL;
272
273         if (unlikely(pci_channel_offline(ha->pdev)))
274                 return -EAGAIN;
275
276         if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
277                 return -EINVAL;
278         if (start > ha->optrom_size)
279                 return -EINVAL;
280
281         switch (val) {
282         case 0:
283                 if (ha->optrom_state != QLA_SREADING &&
284                     ha->optrom_state != QLA_SWRITING)
285                         return -EINVAL;
286
287                 ha->optrom_state = QLA_SWAITING;
288
289                 ql_dbg(ql_dbg_user, vha, 0x7061,
290                     "Freeing flash region allocation -- 0x%x bytes.\n",
291                     ha->optrom_region_size);
292
293                 vfree(ha->optrom_buffer);
294                 ha->optrom_buffer = NULL;
295                 break;
296         case 1:
297                 if (ha->optrom_state != QLA_SWAITING)
298                         return -EINVAL;
299
300                 ha->optrom_region_start = start;
301                 ha->optrom_region_size = start + size > ha->optrom_size ?
302                     ha->optrom_size - start : size;
303
304                 ha->optrom_state = QLA_SREADING;
305                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
306                 if (ha->optrom_buffer == NULL) {
307                         ql_log(ql_log_warn, vha, 0x7062,
308                             "Unable to allocate memory for optrom retrieval "
309                             "(%x).\n", ha->optrom_region_size);
310
311                         ha->optrom_state = QLA_SWAITING;
312                         return -ENOMEM;
313                 }
314
315                 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
316                         ql_log(ql_log_warn, vha, 0x7063,
317                             "HBA not online, failing NVRAM update.\n");
318                         return -EAGAIN;
319                 }
320
321                 ql_dbg(ql_dbg_user, vha, 0x7064,
322                     "Reading flash region -- 0x%x/0x%x.\n",
323                     ha->optrom_region_start, ha->optrom_region_size);
324
325                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
326                 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
327                     ha->optrom_region_start, ha->optrom_region_size);
328                 break;
329         case 2:
330                 if (ha->optrom_state != QLA_SWAITING)
331                         return -EINVAL;
332
333                 /*
334                  * We need to be more restrictive on which FLASH regions are
335                  * allowed to be updated via user-space.  Regions accessible
336                  * via this method include:
337                  *
338                  * ISP21xx/ISP22xx/ISP23xx type boards:
339                  *
340                  *      0x000000 -> 0x020000 -- Boot code.
341                  *
342                  * ISP2322/ISP24xx type boards:
343                  *
344                  *      0x000000 -> 0x07ffff -- Boot code.
345                  *      0x080000 -> 0x0fffff -- Firmware.
346                  *
347                  * ISP25xx type boards:
348                  *
349                  *      0x000000 -> 0x07ffff -- Boot code.
350                  *      0x080000 -> 0x0fffff -- Firmware.
351                  *      0x120000 -> 0x12ffff -- VPD and HBA parameters.
352                  */
353                 valid = 0;
354                 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
355                         valid = 1;
356                 else if (start == (ha->flt_region_boot * 4) ||
357                     start == (ha->flt_region_fw * 4))
358                         valid = 1;
359                 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha)
360                         || IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
361                         valid = 1;
362                 if (!valid) {
363                         ql_log(ql_log_warn, vha, 0x7065,
364                             "Invalid start region 0x%x/0x%x.\n", start, size);
365                         return -EINVAL;
366                 }
367
368                 ha->optrom_region_start = start;
369                 ha->optrom_region_size = start + size > ha->optrom_size ?
370                     ha->optrom_size - start : size;
371
372                 ha->optrom_state = QLA_SWRITING;
373                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
374                 if (ha->optrom_buffer == NULL) {
375                         ql_log(ql_log_warn, vha, 0x7066,
376                             "Unable to allocate memory for optrom update "
377                             "(%x)\n", ha->optrom_region_size);
378
379                         ha->optrom_state = QLA_SWAITING;
380                         return -ENOMEM;
381                 }
382
383                 ql_dbg(ql_dbg_user, vha, 0x7067,
384                     "Staging flash region write -- 0x%x/0x%x.\n",
385                     ha->optrom_region_start, ha->optrom_region_size);
386
387                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
388                 break;
389         case 3:
390                 if (ha->optrom_state != QLA_SWRITING)
391                         return -EINVAL;
392
393                 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
394                         ql_log(ql_log_warn, vha, 0x7068,
395                             "HBA not online, failing flash update.\n");
396                         return -EAGAIN;
397                 }
398
399                 ql_dbg(ql_dbg_user, vha, 0x7069,
400                     "Writing flash region -- 0x%x/0x%x.\n",
401                     ha->optrom_region_start, ha->optrom_region_size);
402
403                 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
404                     ha->optrom_region_start, ha->optrom_region_size);
405                 break;
406         default:
407                 return -EINVAL;
408         }
409         return count;
410 }
411
412 static struct bin_attribute sysfs_optrom_ctl_attr = {
413         .attr = {
414                 .name = "optrom_ctl",
415                 .mode = S_IWUSR,
416         },
417         .size = 0,
418         .write = qla2x00_sysfs_write_optrom_ctl,
419 };
420
421 static ssize_t
422 qla2x00_sysfs_read_vpd(struct file *filp, 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
430         if (unlikely(pci_channel_offline(ha->pdev)))
431                 return -EAGAIN;
432
433         if (!capable(CAP_SYS_ADMIN))
434                 return -EINVAL;
435
436         if (IS_NOCACHE_VPD_TYPE(ha))
437                 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
438                     ha->vpd_size);
439         return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
440 }
441
442 static ssize_t
443 qla2x00_sysfs_write_vpd(struct file *filp, struct kobject *kobj,
444                         struct bin_attribute *bin_attr,
445                         char *buf, loff_t off, size_t count)
446 {
447         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
448             struct device, kobj)));
449         struct qla_hw_data *ha = vha->hw;
450         uint8_t *tmp_data;
451
452         if (unlikely(pci_channel_offline(ha->pdev)))
453                 return 0;
454
455         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size ||
456             !ha->isp_ops->write_nvram)
457                 return 0;
458
459         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
460                 ql_log(ql_log_warn, vha, 0x706a,
461                     "HBA not online, failing VPD update.\n");
462                 return -EAGAIN;
463         }
464
465         /* Write NVRAM. */
466         ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->vpd_base, count);
467         ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd, ha->vpd_base, count);
468
469         /* Update flash version information for 4Gb & above. */
470         if (!IS_FWI2_CAPABLE(ha))
471                 return -EINVAL;
472
473         tmp_data = vmalloc(256);
474         if (!tmp_data) {
475                 ql_log(ql_log_warn, vha, 0x706b,
476                     "Unable to allocate memory for VPD information update.\n");
477                 return -ENOMEM;
478         }
479         ha->isp_ops->get_flash_version(vha, tmp_data);
480         vfree(tmp_data);
481
482         return count;
483 }
484
485 static struct bin_attribute sysfs_vpd_attr = {
486         .attr = {
487                 .name = "vpd",
488                 .mode = S_IRUSR | S_IWUSR,
489         },
490         .size = 0,
491         .read = qla2x00_sysfs_read_vpd,
492         .write = qla2x00_sysfs_write_vpd,
493 };
494
495 static ssize_t
496 qla2x00_sysfs_read_sfp(struct file *filp, struct kobject *kobj,
497                        struct bin_attribute *bin_attr,
498                        char *buf, loff_t off, size_t count)
499 {
500         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
501             struct device, kobj)));
502         struct qla_hw_data *ha = vha->hw;
503         uint16_t iter, addr, offset;
504         int rval;
505
506         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
507                 return 0;
508
509         if (ha->sfp_data)
510                 goto do_read;
511
512         ha->sfp_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
513             &ha->sfp_data_dma);
514         if (!ha->sfp_data) {
515                 ql_log(ql_log_warn, vha, 0x706c,
516                     "Unable to allocate memory for SFP read-data.\n");
517                 return 0;
518         }
519
520 do_read:
521         memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
522         addr = 0xa0;
523         for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
524             iter++, offset += SFP_BLOCK_SIZE) {
525                 if (iter == 4) {
526                         /* Skip to next device address. */
527                         addr = 0xa2;
528                         offset = 0;
529                 }
530
531                 rval = qla2x00_read_sfp(vha, ha->sfp_data_dma, ha->sfp_data,
532                     addr, offset, SFP_BLOCK_SIZE, 0);
533                 if (rval != QLA_SUCCESS) {
534                         ql_log(ql_log_warn, vha, 0x706d,
535                             "Unable to read SFP data (%x/%x/%x).\n", rval,
536                             addr, offset);
537
538                         return -EIO;
539                 }
540                 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
541                 buf += SFP_BLOCK_SIZE;
542         }
543
544         return count;
545 }
546
547 static struct bin_attribute sysfs_sfp_attr = {
548         .attr = {
549                 .name = "sfp",
550                 .mode = S_IRUSR | S_IWUSR,
551         },
552         .size = SFP_DEV_SIZE * 2,
553         .read = qla2x00_sysfs_read_sfp,
554 };
555
556 static ssize_t
557 qla2x00_sysfs_write_reset(struct file *filp, struct kobject *kobj,
558                         struct bin_attribute *bin_attr,
559                         char *buf, loff_t off, size_t count)
560 {
561         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
562             struct device, kobj)));
563         struct qla_hw_data *ha = vha->hw;
564         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
565         int type;
566
567         if (off != 0)
568                 return -EINVAL;
569
570         type = simple_strtol(buf, NULL, 10);
571         switch (type) {
572         case 0x2025c:
573                 ql_log(ql_log_info, vha, 0x706e,
574                     "Issuing ISP reset.\n");
575
576                 scsi_block_requests(vha->host);
577                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
578                 if (IS_QLA82XX(ha)) {
579                         qla82xx_idc_lock(ha);
580                         qla82xx_set_reset_owner(vha);
581                         qla82xx_idc_unlock(ha);
582                 }
583                 qla2xxx_wake_dpc(vha);
584                 qla2x00_wait_for_chip_reset(vha);
585                 scsi_unblock_requests(vha->host);
586                 break;
587         case 0x2025d:
588                 if (!IS_QLA81XX(ha))
589                         return -EPERM;
590
591                 ql_log(ql_log_info, vha, 0x706f,
592                     "Issuing MPI reset.\n");
593
594                 /* Make sure FC side is not in reset */
595                 qla2x00_wait_for_hba_online(vha);
596
597                 /* Issue MPI reset */
598                 scsi_block_requests(vha->host);
599                 if (qla81xx_restart_mpi_firmware(vha) != QLA_SUCCESS)
600                         ql_log(ql_log_warn, vha, 0x7070,
601                             "MPI reset failed.\n");
602                 scsi_unblock_requests(vha->host);
603                 break;
604         case 0x2025e:
605                 if (!IS_QLA82XX(ha) || vha != base_vha) {
606                         ql_log(ql_log_info, vha, 0x7071,
607                             "FCoE ctx reset no supported.\n");
608                         return -EPERM;
609                 }
610
611                 ql_log(ql_log_info, vha, 0x7072,
612                     "Issuing FCoE ctx reset.\n");
613                 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
614                 qla2xxx_wake_dpc(vha);
615                 qla2x00_wait_for_fcoe_ctx_reset(vha);
616                 break;
617         }
618         return count;
619 }
620
621 static struct bin_attribute sysfs_reset_attr = {
622         .attr = {
623                 .name = "reset",
624                 .mode = S_IWUSR,
625         },
626         .size = 0,
627         .write = qla2x00_sysfs_write_reset,
628 };
629
630 static ssize_t
631 qla2x00_sysfs_write_edc(struct file *filp, struct kobject *kobj,
632                         struct bin_attribute *bin_attr,
633                         char *buf, loff_t off, size_t count)
634 {
635         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
636             struct device, kobj)));
637         struct qla_hw_data *ha = vha->hw;
638         uint16_t dev, adr, opt, len;
639         int rval;
640
641         ha->edc_data_len = 0;
642
643         if (!capable(CAP_SYS_ADMIN) || off != 0 || count < 8)
644                 return -EINVAL;
645
646         if (!ha->edc_data) {
647                 ha->edc_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
648                     &ha->edc_data_dma);
649                 if (!ha->edc_data) {
650                         ql_log(ql_log_warn, vha, 0x7073,
651                             "Unable to allocate memory for EDC write.\n");
652                         return -ENOMEM;
653                 }
654         }
655
656         dev = le16_to_cpup((void *)&buf[0]);
657         adr = le16_to_cpup((void *)&buf[2]);
658         opt = le16_to_cpup((void *)&buf[4]);
659         len = le16_to_cpup((void *)&buf[6]);
660
661         if (!(opt & BIT_0))
662                 if (len == 0 || len > DMA_POOL_SIZE || len > count - 8)
663                         return -EINVAL;
664
665         memcpy(ha->edc_data, &buf[8], len);
666
667         rval = qla2x00_write_sfp(vha, ha->edc_data_dma, ha->edc_data,
668             dev, adr, len, opt);
669         if (rval != QLA_SUCCESS) {
670                 ql_log(ql_log_warn, vha, 0x7074,
671                     "Unable to write EDC (%x) %02x:%02x:%04x:%02x:%02hhx\n",
672                     rval, dev, adr, opt, len, buf[8]);
673                 return -EIO;
674         }
675
676         return count;
677 }
678
679 static struct bin_attribute sysfs_edc_attr = {
680         .attr = {
681                 .name = "edc",
682                 .mode = S_IWUSR,
683         },
684         .size = 0,
685         .write = qla2x00_sysfs_write_edc,
686 };
687
688 static ssize_t
689 qla2x00_sysfs_write_edc_status(struct file *filp, struct kobject *kobj,
690                         struct bin_attribute *bin_attr,
691                         char *buf, loff_t off, size_t count)
692 {
693         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
694             struct device, kobj)));
695         struct qla_hw_data *ha = vha->hw;
696         uint16_t dev, adr, opt, len;
697         int rval;
698
699         ha->edc_data_len = 0;
700
701         if (!capable(CAP_SYS_ADMIN) || off != 0 || count < 8)
702                 return -EINVAL;
703
704         if (!ha->edc_data) {
705                 ha->edc_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
706                     &ha->edc_data_dma);
707                 if (!ha->edc_data) {
708                         ql_log(ql_log_warn, vha, 0x708c,
709                             "Unable to allocate memory for EDC status.\n");
710                         return -ENOMEM;
711                 }
712         }
713
714         dev = le16_to_cpup((void *)&buf[0]);
715         adr = le16_to_cpup((void *)&buf[2]);
716         opt = le16_to_cpup((void *)&buf[4]);
717         len = le16_to_cpup((void *)&buf[6]);
718
719         if (!(opt & BIT_0))
720                 if (len == 0 || len > DMA_POOL_SIZE)
721                         return -EINVAL;
722
723         memset(ha->edc_data, 0, len);
724         rval = qla2x00_read_sfp(vha, ha->edc_data_dma, ha->edc_data,
725                         dev, adr, len, opt);
726         if (rval != QLA_SUCCESS) {
727                 ql_log(ql_log_info, vha, 0x7075,
728                     "Unable to write EDC status (%x) %02x:%02x:%04x:%02x.\n",
729                     rval, dev, adr, opt, len);
730                 return -EIO;
731         }
732
733         ha->edc_data_len = len;
734
735         return count;
736 }
737
738 static ssize_t
739 qla2x00_sysfs_read_edc_status(struct file *filp, struct kobject *kobj,
740                            struct bin_attribute *bin_attr,
741                            char *buf, loff_t off, size_t count)
742 {
743         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
744             struct device, kobj)));
745         struct qla_hw_data *ha = vha->hw;
746
747         if (!capable(CAP_SYS_ADMIN) || off != 0 || count == 0)
748                 return 0;
749
750         if (!ha->edc_data || ha->edc_data_len == 0 || ha->edc_data_len > count)
751                 return -EINVAL;
752
753         memcpy(buf, ha->edc_data, ha->edc_data_len);
754
755         return ha->edc_data_len;
756 }
757
758 static struct bin_attribute sysfs_edc_status_attr = {
759         .attr = {
760                 .name = "edc_status",
761                 .mode = S_IRUSR | S_IWUSR,
762         },
763         .size = 0,
764         .write = qla2x00_sysfs_write_edc_status,
765         .read = qla2x00_sysfs_read_edc_status,
766 };
767
768 static ssize_t
769 qla2x00_sysfs_read_xgmac_stats(struct file *filp, struct kobject *kobj,
770                        struct bin_attribute *bin_attr,
771                        char *buf, loff_t off, size_t count)
772 {
773         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
774             struct device, kobj)));
775         struct qla_hw_data *ha = vha->hw;
776         int rval;
777         uint16_t actual_size;
778
779         if (!capable(CAP_SYS_ADMIN) || off != 0 || count > XGMAC_DATA_SIZE)
780                 return 0;
781
782         if (ha->xgmac_data)
783                 goto do_read;
784
785         ha->xgmac_data = dma_alloc_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
786             &ha->xgmac_data_dma, GFP_KERNEL);
787         if (!ha->xgmac_data) {
788                 ql_log(ql_log_warn, vha, 0x7076,
789                     "Unable to allocate memory for XGMAC read-data.\n");
790                 return 0;
791         }
792
793 do_read:
794         actual_size = 0;
795         memset(ha->xgmac_data, 0, XGMAC_DATA_SIZE);
796
797         rval = qla2x00_get_xgmac_stats(vha, ha->xgmac_data_dma,
798             XGMAC_DATA_SIZE, &actual_size);
799         if (rval != QLA_SUCCESS) {
800                 ql_log(ql_log_warn, vha, 0x7077,
801                     "Unable to read XGMAC data (%x).\n", rval);
802                 count = 0;
803         }
804
805         count = actual_size > count ? count: actual_size;
806         memcpy(buf, ha->xgmac_data, count);
807
808         return count;
809 }
810
811 static struct bin_attribute sysfs_xgmac_stats_attr = {
812         .attr = {
813                 .name = "xgmac_stats",
814                 .mode = S_IRUSR,
815         },
816         .size = 0,
817         .read = qla2x00_sysfs_read_xgmac_stats,
818 };
819
820 static ssize_t
821 qla2x00_sysfs_read_dcbx_tlv(struct file *filp, struct kobject *kobj,
822                        struct bin_attribute *bin_attr,
823                        char *buf, loff_t off, size_t count)
824 {
825         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
826             struct device, kobj)));
827         struct qla_hw_data *ha = vha->hw;
828         int rval;
829         uint16_t actual_size;
830
831         if (!capable(CAP_SYS_ADMIN) || off != 0 || count > DCBX_TLV_DATA_SIZE)
832                 return 0;
833
834         if (ha->dcbx_tlv)
835                 goto do_read;
836
837         ha->dcbx_tlv = dma_alloc_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
838             &ha->dcbx_tlv_dma, GFP_KERNEL);
839         if (!ha->dcbx_tlv) {
840                 ql_log(ql_log_warn, vha, 0x7078,
841                     "Unable to allocate memory for DCBX TLV read-data.\n");
842                 return -ENOMEM;
843         }
844
845 do_read:
846         actual_size = 0;
847         memset(ha->dcbx_tlv, 0, DCBX_TLV_DATA_SIZE);
848
849         rval = qla2x00_get_dcbx_params(vha, ha->dcbx_tlv_dma,
850             DCBX_TLV_DATA_SIZE);
851         if (rval != QLA_SUCCESS) {
852                 ql_log(ql_log_warn, vha, 0x7079,
853                     "Unable to read DCBX TLV (%x).\n", rval);
854                 return -EIO;
855         }
856
857         memcpy(buf, ha->dcbx_tlv, count);
858
859         return count;
860 }
861
862 static struct bin_attribute sysfs_dcbx_tlv_attr = {
863         .attr = {
864                 .name = "dcbx_tlv",
865                 .mode = S_IRUSR,
866         },
867         .size = 0,
868         .read = qla2x00_sysfs_read_dcbx_tlv,
869 };
870
871 static struct sysfs_entry {
872         char *name;
873         struct bin_attribute *attr;
874         int is4GBp_only;
875 } bin_file_entries[] = {
876         { "fw_dump", &sysfs_fw_dump_attr, },
877         { "nvram", &sysfs_nvram_attr, },
878         { "optrom", &sysfs_optrom_attr, },
879         { "optrom_ctl", &sysfs_optrom_ctl_attr, },
880         { "vpd", &sysfs_vpd_attr, 1 },
881         { "sfp", &sysfs_sfp_attr, 1 },
882         { "reset", &sysfs_reset_attr, },
883         { "edc", &sysfs_edc_attr, 2 },
884         { "edc_status", &sysfs_edc_status_attr, 2 },
885         { "xgmac_stats", &sysfs_xgmac_stats_attr, 3 },
886         { "dcbx_tlv", &sysfs_dcbx_tlv_attr, 3 },
887         { NULL },
888 };
889
890 void
891 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *vha)
892 {
893         struct Scsi_Host *host = vha->host;
894         struct sysfs_entry *iter;
895         int ret;
896
897         for (iter = bin_file_entries; iter->name; iter++) {
898                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(vha->hw))
899                         continue;
900                 if (iter->is4GBp_only == 2 && !IS_QLA25XX(vha->hw))
901                         continue;
902                 if (iter->is4GBp_only == 3 && !(IS_CNA_CAPABLE(vha->hw)))
903                         continue;
904
905                 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
906                     iter->attr);
907                 if (ret)
908                         ql_log(ql_log_warn, vha, 0x00f3,
909                             "Unable to create sysfs %s binary attribute (%d).\n",
910                             iter->name, ret);
911                 else
912                         ql_dbg(ql_dbg_init, vha, 0x00f4,
913                             "Successfully created sysfs %s binary attribure.\n",
914                             iter->name);
915         }
916 }
917
918 void
919 qla2x00_free_sysfs_attr(scsi_qla_host_t *vha)
920 {
921         struct Scsi_Host *host = vha->host;
922         struct sysfs_entry *iter;
923         struct qla_hw_data *ha = vha->hw;
924
925         for (iter = bin_file_entries; iter->name; iter++) {
926                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
927                         continue;
928                 if (iter->is4GBp_only == 2 && !IS_QLA25XX(ha))
929                         continue;
930                 if (iter->is4GBp_only == 3 && !(IS_CNA_CAPABLE(vha->hw)))
931                         continue;
932
933                 sysfs_remove_bin_file(&host->shost_gendev.kobj,
934                     iter->attr);
935         }
936
937         if (ha->beacon_blink_led == 1)
938                 ha->isp_ops->beacon_off(vha);
939 }
940
941 /* Scsi_Host attributes. */
942
943 static ssize_t
944 qla2x00_drvr_version_show(struct device *dev,
945                           struct device_attribute *attr, char *buf)
946 {
947         return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
948 }
949
950 static ssize_t
951 qla2x00_fw_version_show(struct device *dev,
952                         struct device_attribute *attr, char *buf)
953 {
954         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
955         struct qla_hw_data *ha = vha->hw;
956         char fw_str[128];
957
958         return snprintf(buf, PAGE_SIZE, "%s\n",
959             ha->isp_ops->fw_version_str(vha, fw_str));
960 }
961
962 static ssize_t
963 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
964                         char *buf)
965 {
966         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
967         struct qla_hw_data *ha = vha->hw;
968         uint32_t sn;
969
970         if (IS_FWI2_CAPABLE(ha)) {
971                 qla2xxx_get_vpd_field(vha, "SN", buf, PAGE_SIZE);
972                 return snprintf(buf, PAGE_SIZE, "%s\n", buf);
973         }
974
975         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
976         return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
977             sn % 100000);
978 }
979
980 static ssize_t
981 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
982                       char *buf)
983 {
984         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
985         return snprintf(buf, PAGE_SIZE, "ISP%04X\n", vha->hw->pdev->device);
986 }
987
988 static ssize_t
989 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
990                     char *buf)
991 {
992         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
993         struct qla_hw_data *ha = vha->hw;
994         return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
995             ha->product_id[0], ha->product_id[1], ha->product_id[2],
996             ha->product_id[3]);
997 }
998
999 static ssize_t
1000 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
1001                         char *buf)
1002 {
1003         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1004         return snprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_number);
1005 }
1006
1007 static ssize_t
1008 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
1009                         char *buf)
1010 {
1011         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1012         return snprintf(buf, PAGE_SIZE, "%s\n",
1013             vha->hw->model_desc ? vha->hw->model_desc : "");
1014 }
1015
1016 static ssize_t
1017 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
1018                       char *buf)
1019 {
1020         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1021         char pci_info[30];
1022
1023         return snprintf(buf, PAGE_SIZE, "%s\n",
1024             vha->hw->isp_ops->pci_info_str(vha, pci_info));
1025 }
1026
1027 static ssize_t
1028 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
1029                         char *buf)
1030 {
1031         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1032         struct qla_hw_data *ha = vha->hw;
1033         int len = 0;
1034
1035         if (atomic_read(&vha->loop_state) == LOOP_DOWN ||
1036             atomic_read(&vha->loop_state) == LOOP_DEAD ||
1037             vha->device_flags & DFLG_NO_CABLE)
1038                 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
1039         else if (atomic_read(&vha->loop_state) != LOOP_READY ||
1040             qla2x00_reset_active(vha))
1041                 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
1042         else {
1043                 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
1044
1045                 switch (ha->current_topology) {
1046                 case ISP_CFG_NL:
1047                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1048                         break;
1049                 case ISP_CFG_FL:
1050                         len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
1051                         break;
1052                 case ISP_CFG_N:
1053                         len += snprintf(buf + len, PAGE_SIZE-len,
1054                             "N_Port to N_Port\n");
1055                         break;
1056                 case ISP_CFG_F:
1057                         len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
1058                         break;
1059                 default:
1060                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1061                         break;
1062                 }
1063         }
1064         return len;
1065 }
1066
1067 static ssize_t
1068 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
1069                  char *buf)
1070 {
1071         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1072         int len = 0;
1073
1074         switch (vha->hw->zio_mode) {
1075         case QLA_ZIO_MODE_6:
1076                 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
1077                 break;
1078         case QLA_ZIO_DISABLED:
1079                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1080                 break;
1081         }
1082         return len;
1083 }
1084
1085 static ssize_t
1086 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
1087                   const char *buf, size_t count)
1088 {
1089         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1090         struct qla_hw_data *ha = vha->hw;
1091         int val = 0;
1092         uint16_t zio_mode;
1093
1094         if (!IS_ZIO_SUPPORTED(ha))
1095                 return -ENOTSUPP;
1096
1097         if (sscanf(buf, "%d", &val) != 1)
1098                 return -EINVAL;
1099
1100         if (val)
1101                 zio_mode = QLA_ZIO_MODE_6;
1102         else
1103                 zio_mode = QLA_ZIO_DISABLED;
1104
1105         /* Update per-hba values and queue a reset. */
1106         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
1107                 ha->zio_mode = zio_mode;
1108                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1109         }
1110         return strlen(buf);
1111 }
1112
1113 static ssize_t
1114 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
1115                        char *buf)
1116 {
1117         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1118
1119         return snprintf(buf, PAGE_SIZE, "%d us\n", vha->hw->zio_timer * 100);
1120 }
1121
1122 static ssize_t
1123 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
1124                         const char *buf, size_t count)
1125 {
1126         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1127         int val = 0;
1128         uint16_t zio_timer;
1129
1130         if (sscanf(buf, "%d", &val) != 1)
1131                 return -EINVAL;
1132         if (val > 25500 || val < 100)
1133                 return -ERANGE;
1134
1135         zio_timer = (uint16_t)(val / 100);
1136         vha->hw->zio_timer = zio_timer;
1137
1138         return strlen(buf);
1139 }
1140
1141 static ssize_t
1142 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
1143                     char *buf)
1144 {
1145         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1146         int len = 0;
1147
1148         if (vha->hw->beacon_blink_led)
1149                 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
1150         else
1151                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1152         return len;
1153 }
1154
1155 static ssize_t
1156 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
1157                      const char *buf, size_t count)
1158 {
1159         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1160         struct qla_hw_data *ha = vha->hw;
1161         int val = 0;
1162         int rval;
1163
1164         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1165                 return -EPERM;
1166
1167         if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
1168                 ql_log(ql_log_warn, vha, 0x707a,
1169                     "Abort ISP active -- ignoring beacon request.\n");
1170                 return -EBUSY;
1171         }
1172
1173         if (sscanf(buf, "%d", &val) != 1)
1174                 return -EINVAL;
1175
1176         if (val)
1177                 rval = ha->isp_ops->beacon_on(vha);
1178         else
1179                 rval = ha->isp_ops->beacon_off(vha);
1180
1181         if (rval != QLA_SUCCESS)
1182                 count = 0;
1183
1184         return count;
1185 }
1186
1187 static ssize_t
1188 qla2x00_optrom_bios_version_show(struct device *dev,
1189                                  struct device_attribute *attr, char *buf)
1190 {
1191         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1192         struct qla_hw_data *ha = vha->hw;
1193         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
1194             ha->bios_revision[0]);
1195 }
1196
1197 static ssize_t
1198 qla2x00_optrom_efi_version_show(struct device *dev,
1199                                 struct device_attribute *attr, char *buf)
1200 {
1201         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1202         struct qla_hw_data *ha = vha->hw;
1203         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
1204             ha->efi_revision[0]);
1205 }
1206
1207 static ssize_t
1208 qla2x00_optrom_fcode_version_show(struct device *dev,
1209                                   struct device_attribute *attr, char *buf)
1210 {
1211         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1212         struct qla_hw_data *ha = vha->hw;
1213         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
1214             ha->fcode_revision[0]);
1215 }
1216
1217 static ssize_t
1218 qla2x00_optrom_fw_version_show(struct device *dev,
1219                                struct device_attribute *attr, char *buf)
1220 {
1221         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1222         struct qla_hw_data *ha = vha->hw;
1223         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
1224             ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
1225             ha->fw_revision[3]);
1226 }
1227
1228 static ssize_t
1229 qla2x00_optrom_gold_fw_version_show(struct device *dev,
1230     struct device_attribute *attr, char *buf)
1231 {
1232         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1233         struct qla_hw_data *ha = vha->hw;
1234
1235         if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha))
1236                 return snprintf(buf, PAGE_SIZE, "\n");
1237
1238         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%d)\n",
1239             ha->gold_fw_version[0], ha->gold_fw_version[1],
1240             ha->gold_fw_version[2], ha->gold_fw_version[3]);
1241 }
1242
1243 static ssize_t
1244 qla2x00_total_isp_aborts_show(struct device *dev,
1245                               struct device_attribute *attr, char *buf)
1246 {
1247         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1248         struct qla_hw_data *ha = vha->hw;
1249         return snprintf(buf, PAGE_SIZE, "%d\n",
1250             ha->qla_stats.total_isp_aborts);
1251 }
1252
1253 static ssize_t
1254 qla24xx_84xx_fw_version_show(struct device *dev,
1255         struct device_attribute *attr, char *buf)
1256 {
1257         int rval = QLA_SUCCESS;
1258         uint16_t status[2] = {0, 0};
1259         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1260         struct qla_hw_data *ha = vha->hw;
1261
1262         if (!IS_QLA84XX(ha))
1263                 return snprintf(buf, PAGE_SIZE, "\n");
1264
1265         if (ha->cs84xx->op_fw_version == 0)
1266                 rval = qla84xx_verify_chip(vha, status);
1267
1268         if ((rval == QLA_SUCCESS) && (status[0] == 0))
1269                 return snprintf(buf, PAGE_SIZE, "%u\n",
1270                         (uint32_t)ha->cs84xx->op_fw_version);
1271
1272         return snprintf(buf, PAGE_SIZE, "\n");
1273 }
1274
1275 static ssize_t
1276 qla2x00_mpi_version_show(struct device *dev, struct device_attribute *attr,
1277     char *buf)
1278 {
1279         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1280         struct qla_hw_data *ha = vha->hw;
1281
1282         if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
1283                 return snprintf(buf, PAGE_SIZE, "\n");
1284
1285         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
1286             ha->mpi_version[0], ha->mpi_version[1], ha->mpi_version[2],
1287             ha->mpi_capabilities);
1288 }
1289
1290 static ssize_t
1291 qla2x00_phy_version_show(struct device *dev, struct device_attribute *attr,
1292     char *buf)
1293 {
1294         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1295         struct qla_hw_data *ha = vha->hw;
1296
1297         if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha))
1298                 return snprintf(buf, PAGE_SIZE, "\n");
1299
1300         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1301             ha->phy_version[0], ha->phy_version[1], ha->phy_version[2]);
1302 }
1303
1304 static ssize_t
1305 qla2x00_flash_block_size_show(struct device *dev,
1306                               struct device_attribute *attr, char *buf)
1307 {
1308         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1309         struct qla_hw_data *ha = vha->hw;
1310
1311         return snprintf(buf, PAGE_SIZE, "0x%x\n", ha->fdt_block_size);
1312 }
1313
1314 static ssize_t
1315 qla2x00_vlan_id_show(struct device *dev, struct device_attribute *attr,
1316     char *buf)
1317 {
1318         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1319
1320         if (!IS_CNA_CAPABLE(vha->hw))
1321                 return snprintf(buf, PAGE_SIZE, "\n");
1322
1323         return snprintf(buf, PAGE_SIZE, "%d\n", vha->fcoe_vlan_id);
1324 }
1325
1326 static ssize_t
1327 qla2x00_vn_port_mac_address_show(struct device *dev,
1328     struct device_attribute *attr, char *buf)
1329 {
1330         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1331
1332         if (!IS_CNA_CAPABLE(vha->hw))
1333                 return snprintf(buf, PAGE_SIZE, "\n");
1334
1335         return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1336             vha->fcoe_vn_port_mac[5], vha->fcoe_vn_port_mac[4],
1337             vha->fcoe_vn_port_mac[3], vha->fcoe_vn_port_mac[2],
1338             vha->fcoe_vn_port_mac[1], vha->fcoe_vn_port_mac[0]);
1339 }
1340
1341 static ssize_t
1342 qla2x00_fabric_param_show(struct device *dev, struct device_attribute *attr,
1343     char *buf)
1344 {
1345         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1346
1347         return snprintf(buf, PAGE_SIZE, "%d\n", vha->hw->switch_cap);
1348 }
1349
1350 static ssize_t
1351 qla2x00_thermal_temp_show(struct device *dev,
1352         struct device_attribute *attr, char *buf)
1353 {
1354         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1355         int rval = QLA_FUNCTION_FAILED;
1356         uint16_t temp, frac;
1357
1358         if (!vha->hw->flags.thermal_supported)
1359                 return snprintf(buf, PAGE_SIZE, "\n");
1360
1361         temp = frac = 0;
1362         if (qla2x00_reset_active(vha))
1363                 ql_log(ql_log_warn, vha, 0x707b,
1364                     "ISP reset active.\n");
1365         else if (!vha->hw->flags.eeh_busy)
1366                 rval = qla2x00_get_thermal_temp(vha, &temp, &frac);
1367         if (rval != QLA_SUCCESS)
1368                 return snprintf(buf, PAGE_SIZE, "\n");
1369
1370         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", temp, frac);
1371 }
1372
1373 static ssize_t
1374 qla2x00_fw_state_show(struct device *dev, struct device_attribute *attr,
1375     char *buf)
1376 {
1377         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1378         int rval = QLA_FUNCTION_FAILED;
1379         uint16_t state[5];
1380
1381         if (qla2x00_reset_active(vha))
1382                 ql_log(ql_log_warn, vha, 0x707c,
1383                     "ISP reset active.\n");
1384         else if (!vha->hw->flags.eeh_busy)
1385                 rval = qla2x00_get_firmware_state(vha, state);
1386         if (rval != QLA_SUCCESS)
1387                 memset(state, -1, sizeof(state));
1388
1389         return snprintf(buf, PAGE_SIZE, "0x%x 0x%x 0x%x 0x%x 0x%x\n", state[0],
1390             state[1], state[2], state[3], state[4]);
1391 }
1392
1393 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show, NULL);
1394 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
1395 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
1396 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
1397 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
1398 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
1399 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
1400 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
1401 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
1402 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
1403 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
1404                    qla2x00_zio_timer_store);
1405 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
1406                    qla2x00_beacon_store);
1407 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
1408                    qla2x00_optrom_bios_version_show, NULL);
1409 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
1410                    qla2x00_optrom_efi_version_show, NULL);
1411 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
1412                    qla2x00_optrom_fcode_version_show, NULL);
1413 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
1414                    NULL);
1415 static DEVICE_ATTR(optrom_gold_fw_version, S_IRUGO,
1416     qla2x00_optrom_gold_fw_version_show, NULL);
1417 static DEVICE_ATTR(84xx_fw_version, S_IRUGO, qla24xx_84xx_fw_version_show,
1418                    NULL);
1419 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
1420                    NULL);
1421 static DEVICE_ATTR(mpi_version, S_IRUGO, qla2x00_mpi_version_show, NULL);
1422 static DEVICE_ATTR(phy_version, S_IRUGO, qla2x00_phy_version_show, NULL);
1423 static DEVICE_ATTR(flash_block_size, S_IRUGO, qla2x00_flash_block_size_show,
1424                    NULL);
1425 static DEVICE_ATTR(vlan_id, S_IRUGO, qla2x00_vlan_id_show, NULL);
1426 static DEVICE_ATTR(vn_port_mac_address, S_IRUGO,
1427                    qla2x00_vn_port_mac_address_show, NULL);
1428 static DEVICE_ATTR(fabric_param, S_IRUGO, qla2x00_fabric_param_show, NULL);
1429 static DEVICE_ATTR(fw_state, S_IRUGO, qla2x00_fw_state_show, NULL);
1430 static DEVICE_ATTR(thermal_temp, S_IRUGO, qla2x00_thermal_temp_show, NULL);
1431
1432 struct device_attribute *qla2x00_host_attrs[] = {
1433         &dev_attr_driver_version,
1434         &dev_attr_fw_version,
1435         &dev_attr_serial_num,
1436         &dev_attr_isp_name,
1437         &dev_attr_isp_id,
1438         &dev_attr_model_name,
1439         &dev_attr_model_desc,
1440         &dev_attr_pci_info,
1441         &dev_attr_link_state,
1442         &dev_attr_zio,
1443         &dev_attr_zio_timer,
1444         &dev_attr_beacon,
1445         &dev_attr_optrom_bios_version,
1446         &dev_attr_optrom_efi_version,
1447         &dev_attr_optrom_fcode_version,
1448         &dev_attr_optrom_fw_version,
1449         &dev_attr_84xx_fw_version,
1450         &dev_attr_total_isp_aborts,
1451         &dev_attr_mpi_version,
1452         &dev_attr_phy_version,
1453         &dev_attr_flash_block_size,
1454         &dev_attr_vlan_id,
1455         &dev_attr_vn_port_mac_address,
1456         &dev_attr_fabric_param,
1457         &dev_attr_fw_state,
1458         &dev_attr_optrom_gold_fw_version,
1459         &dev_attr_thermal_temp,
1460         NULL,
1461 };
1462
1463 /* Host attributes. */
1464
1465 static void
1466 qla2x00_get_host_port_id(struct Scsi_Host *shost)
1467 {
1468         scsi_qla_host_t *vha = shost_priv(shost);
1469
1470         fc_host_port_id(shost) = vha->d_id.b.domain << 16 |
1471             vha->d_id.b.area << 8 | vha->d_id.b.al_pa;
1472 }
1473
1474 static void
1475 qla2x00_get_host_speed(struct Scsi_Host *shost)
1476 {
1477         struct qla_hw_data *ha = ((struct scsi_qla_host *)
1478                                         (shost_priv(shost)))->hw;
1479         u32 speed = FC_PORTSPEED_UNKNOWN;
1480
1481         switch (ha->link_data_rate) {
1482         case PORT_SPEED_1GB:
1483                 speed = FC_PORTSPEED_1GBIT;
1484                 break;
1485         case PORT_SPEED_2GB:
1486                 speed = FC_PORTSPEED_2GBIT;
1487                 break;
1488         case PORT_SPEED_4GB:
1489                 speed = FC_PORTSPEED_4GBIT;
1490                 break;
1491         case PORT_SPEED_8GB:
1492                 speed = FC_PORTSPEED_8GBIT;
1493                 break;
1494         case PORT_SPEED_10GB:
1495                 speed = FC_PORTSPEED_10GBIT;
1496                 break;
1497         case PORT_SPEED_16GB:
1498                 speed = FC_PORTSPEED_16GBIT;
1499                 break;
1500         }
1501         fc_host_speed(shost) = speed;
1502 }
1503
1504 static void
1505 qla2x00_get_host_port_type(struct Scsi_Host *shost)
1506 {
1507         scsi_qla_host_t *vha = shost_priv(shost);
1508         uint32_t port_type = FC_PORTTYPE_UNKNOWN;
1509
1510         if (vha->vp_idx) {
1511                 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
1512                 return;
1513         }
1514         switch (vha->hw->current_topology) {
1515         case ISP_CFG_NL:
1516                 port_type = FC_PORTTYPE_LPORT;
1517                 break;
1518         case ISP_CFG_FL:
1519                 port_type = FC_PORTTYPE_NLPORT;
1520                 break;
1521         case ISP_CFG_N:
1522                 port_type = FC_PORTTYPE_PTP;
1523                 break;
1524         case ISP_CFG_F:
1525                 port_type = FC_PORTTYPE_NPORT;
1526                 break;
1527         }
1528         fc_host_port_type(shost) = port_type;
1529 }
1530
1531 static void
1532 qla2x00_get_starget_node_name(struct scsi_target *starget)
1533 {
1534         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1535         scsi_qla_host_t *vha = shost_priv(host);
1536         fc_port_t *fcport;
1537         u64 node_name = 0;
1538
1539         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1540                 if (fcport->rport &&
1541                     starget->id == fcport->rport->scsi_target_id) {
1542                         node_name = wwn_to_u64(fcport->node_name);
1543                         break;
1544                 }
1545         }
1546
1547         fc_starget_node_name(starget) = node_name;
1548 }
1549
1550 static void
1551 qla2x00_get_starget_port_name(struct scsi_target *starget)
1552 {
1553         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1554         scsi_qla_host_t *vha = shost_priv(host);
1555         fc_port_t *fcport;
1556         u64 port_name = 0;
1557
1558         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1559                 if (fcport->rport &&
1560                     starget->id == fcport->rport->scsi_target_id) {
1561                         port_name = wwn_to_u64(fcport->port_name);
1562                         break;
1563                 }
1564         }
1565
1566         fc_starget_port_name(starget) = port_name;
1567 }
1568
1569 static void
1570 qla2x00_get_starget_port_id(struct scsi_target *starget)
1571 {
1572         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1573         scsi_qla_host_t *vha = shost_priv(host);
1574         fc_port_t *fcport;
1575         uint32_t port_id = ~0U;
1576
1577         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1578                 if (fcport->rport &&
1579                     starget->id == fcport->rport->scsi_target_id) {
1580                         port_id = fcport->d_id.b.domain << 16 |
1581                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
1582                         break;
1583                 }
1584         }
1585
1586         fc_starget_port_id(starget) = port_id;
1587 }
1588
1589 static void
1590 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1591 {
1592         if (timeout)
1593                 rport->dev_loss_tmo = timeout;
1594         else
1595                 rport->dev_loss_tmo = 1;
1596 }
1597
1598 static void
1599 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
1600 {
1601         struct Scsi_Host *host = rport_to_shost(rport);
1602         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1603         unsigned long flags;
1604
1605         if (!fcport)
1606                 return;
1607
1608         /* Now that the rport has been deleted, set the fcport state to
1609            FCS_DEVICE_DEAD */
1610         qla2x00_set_fcport_state(fcport, FCS_DEVICE_DEAD);
1611
1612         /*
1613          * Transport has effectively 'deleted' the rport, clear
1614          * all local references.
1615          */
1616         spin_lock_irqsave(host->host_lock, flags);
1617         fcport->rport = fcport->drport = NULL;
1618         *((fc_port_t **)rport->dd_data) = NULL;
1619         spin_unlock_irqrestore(host->host_lock, flags);
1620
1621         if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
1622                 return;
1623
1624         if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
1625                 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
1626                 return;
1627         }
1628 }
1629
1630 static void
1631 qla2x00_terminate_rport_io(struct fc_rport *rport)
1632 {
1633         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1634
1635         if (!fcport)
1636                 return;
1637
1638         if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
1639                 return;
1640
1641         if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
1642                 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
1643                 return;
1644         }
1645         /*
1646          * At this point all fcport's software-states are cleared.  Perform any
1647          * final cleanup of firmware resources (PCBs and XCBs).
1648          */
1649         if (fcport->loop_id != FC_NO_LOOP_ID &&
1650             !test_bit(UNLOADING, &fcport->vha->dpc_flags)) {
1651                 if (IS_FWI2_CAPABLE(fcport->vha->hw))
1652                         fcport->vha->hw->isp_ops->fabric_logout(fcport->vha,
1653                             fcport->loop_id, fcport->d_id.b.domain,
1654                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
1655                 else
1656                         qla2x00_port_logout(fcport->vha, fcport);
1657         }
1658 }
1659
1660 static int
1661 qla2x00_issue_lip(struct Scsi_Host *shost)
1662 {
1663         scsi_qla_host_t *vha = shost_priv(shost);
1664
1665         qla2x00_loop_reset(vha);
1666         return 0;
1667 }
1668
1669 static struct fc_host_statistics *
1670 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
1671 {
1672         scsi_qla_host_t *vha = shost_priv(shost);
1673         struct qla_hw_data *ha = vha->hw;
1674         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
1675         int rval;
1676         struct link_statistics *stats;
1677         dma_addr_t stats_dma;
1678         struct fc_host_statistics *pfc_host_stat;
1679
1680         pfc_host_stat = &ha->fc_host_stat;
1681         memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
1682
1683         if (test_bit(UNLOADING, &vha->dpc_flags))
1684                 goto done;
1685
1686         if (unlikely(pci_channel_offline(ha->pdev)))
1687                 goto done;
1688
1689         stats = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &stats_dma);
1690         if (stats == NULL) {
1691                 ql_log(ql_log_warn, vha, 0x707d,
1692                     "Failed to allocate memory for stats.\n");
1693                 goto done;
1694         }
1695         memset(stats, 0, DMA_POOL_SIZE);
1696
1697         rval = QLA_FUNCTION_FAILED;
1698         if (IS_FWI2_CAPABLE(ha)) {
1699                 rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma);
1700         } else if (atomic_read(&base_vha->loop_state) == LOOP_READY &&
1701             !qla2x00_reset_active(vha) && !ha->dpc_active) {
1702                 /* Must be in a 'READY' state for statistics retrieval. */
1703                 rval = qla2x00_get_link_status(base_vha, base_vha->loop_id,
1704                                                 stats, stats_dma);
1705         }
1706
1707         if (rval != QLA_SUCCESS)
1708                 goto done_free;
1709
1710         pfc_host_stat->link_failure_count = stats->link_fail_cnt;
1711         pfc_host_stat->loss_of_sync_count = stats->loss_sync_cnt;
1712         pfc_host_stat->loss_of_signal_count = stats->loss_sig_cnt;
1713         pfc_host_stat->prim_seq_protocol_err_count = stats->prim_seq_err_cnt;
1714         pfc_host_stat->invalid_tx_word_count = stats->inval_xmit_word_cnt;
1715         pfc_host_stat->invalid_crc_count = stats->inval_crc_cnt;
1716         if (IS_FWI2_CAPABLE(ha)) {
1717                 pfc_host_stat->lip_count = stats->lip_cnt;
1718                 pfc_host_stat->tx_frames = stats->tx_frames;
1719                 pfc_host_stat->rx_frames = stats->rx_frames;
1720                 pfc_host_stat->dumped_frames = stats->dumped_frames;
1721                 pfc_host_stat->nos_count = stats->nos_rcvd;
1722         }
1723         pfc_host_stat->fcp_input_megabytes = ha->qla_stats.input_bytes >> 20;
1724         pfc_host_stat->fcp_output_megabytes = ha->qla_stats.output_bytes >> 20;
1725
1726 done_free:
1727         dma_pool_free(ha->s_dma_pool, stats, stats_dma);
1728 done:
1729         return pfc_host_stat;
1730 }
1731
1732 static void
1733 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
1734 {
1735         scsi_qla_host_t *vha = shost_priv(shost);
1736
1737         qla2x00_get_sym_node_name(vha, fc_host_symbolic_name(shost));
1738 }
1739
1740 static void
1741 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
1742 {
1743         scsi_qla_host_t *vha = shost_priv(shost);
1744
1745         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
1746 }
1747
1748 static void
1749 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
1750 {
1751         scsi_qla_host_t *vha = shost_priv(shost);
1752         uint8_t node_name[WWN_SIZE] = { 0xFF, 0xFF, 0xFF, 0xFF, \
1753                 0xFF, 0xFF, 0xFF, 0xFF};
1754         u64 fabric_name = wwn_to_u64(node_name);
1755
1756         if (vha->device_flags & SWITCH_FOUND)
1757                 fabric_name = wwn_to_u64(vha->fabric_node_name);
1758
1759         fc_host_fabric_name(shost) = fabric_name;
1760 }
1761
1762 static void
1763 qla2x00_get_host_port_state(struct Scsi_Host *shost)
1764 {
1765         scsi_qla_host_t *vha = shost_priv(shost);
1766         struct scsi_qla_host *base_vha = pci_get_drvdata(vha->hw->pdev);
1767
1768         if (!base_vha->flags.online) {
1769                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1770                 return;
1771         }
1772
1773         switch (atomic_read(&base_vha->loop_state)) {
1774         case LOOP_UPDATE:
1775                 fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
1776                 break;
1777         case LOOP_DOWN:
1778                 if (test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags))
1779                         fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
1780                 else
1781                         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1782                 break;
1783         case LOOP_DEAD:
1784                 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1785                 break;
1786         case LOOP_READY:
1787                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1788                 break;
1789         default:
1790                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1791                 break;
1792         }
1793 }
1794
1795 static int
1796 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
1797 {
1798         int     ret = 0;
1799         uint8_t qos = 0;
1800         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
1801         scsi_qla_host_t *vha = NULL;
1802         struct qla_hw_data *ha = base_vha->hw;
1803         uint16_t options = 0;
1804         int     cnt;
1805         struct req_que *req = ha->req_q_map[0];
1806
1807         ret = qla24xx_vport_create_req_sanity_check(fc_vport);
1808         if (ret) {
1809                 ql_log(ql_log_warn, vha, 0x707e,
1810                     "Vport sanity check failed, status %x\n", ret);
1811                 return (ret);
1812         }
1813
1814         vha = qla24xx_create_vhost(fc_vport);
1815         if (vha == NULL) {
1816                 ql_log(ql_log_warn, vha, 0x707f, "Vport create host failed.\n");
1817                 return FC_VPORT_FAILED;
1818         }
1819         if (disable) {
1820                 atomic_set(&vha->vp_state, VP_OFFLINE);
1821                 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
1822         } else
1823                 atomic_set(&vha->vp_state, VP_FAILED);
1824
1825         /* ready to create vport */
1826         ql_log(ql_log_info, vha, 0x7080,
1827             "VP entry id %d assigned.\n", vha->vp_idx);
1828
1829         /* initialized vport states */
1830         atomic_set(&vha->loop_state, LOOP_DOWN);
1831         vha->vp_err_state=  VP_ERR_PORTDWN;
1832         vha->vp_prev_err_state=  VP_ERR_UNKWN;
1833         /* Check if physical ha port is Up */
1834         if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
1835             atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
1836                 /* Don't retry or attempt login of this virtual port */
1837                 ql_dbg(ql_dbg_user, vha, 0x7081,
1838                     "Vport loop state is not UP.\n");
1839                 atomic_set(&vha->loop_state, LOOP_DEAD);
1840                 if (!disable)
1841                         fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
1842         }
1843
1844         if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
1845                 if (ha->fw_attributes & BIT_4) {
1846                         int prot = 0;
1847                         vha->flags.difdix_supported = 1;
1848                         ql_dbg(ql_dbg_user, vha, 0x7082,
1849                             "Registered for DIF/DIX type 1 and 3 protection.\n");
1850                         if (ql2xenabledif == 1)
1851                                 prot = SHOST_DIX_TYPE0_PROTECTION;
1852                         scsi_host_set_prot(vha->host,
1853                             prot | SHOST_DIF_TYPE1_PROTECTION
1854                             | SHOST_DIF_TYPE2_PROTECTION
1855                             | SHOST_DIF_TYPE3_PROTECTION
1856                             | SHOST_DIX_TYPE1_PROTECTION
1857                             | SHOST_DIX_TYPE2_PROTECTION
1858                             | SHOST_DIX_TYPE3_PROTECTION);
1859                         scsi_host_set_guard(vha->host, SHOST_DIX_GUARD_CRC);
1860                 } else
1861                         vha->flags.difdix_supported = 0;
1862         }
1863
1864         if (scsi_add_host_with_dma(vha->host, &fc_vport->dev,
1865                                    &ha->pdev->dev)) {
1866                 ql_dbg(ql_dbg_user, vha, 0x7083,
1867                     "scsi_add_host failure for VP[%d].\n", vha->vp_idx);
1868                 goto vport_create_failed_2;
1869         }
1870
1871         /* initialize attributes */
1872         fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
1873         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1874         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1875         fc_host_supported_classes(vha->host) =
1876                 fc_host_supported_classes(base_vha->host);
1877         fc_host_supported_speeds(vha->host) =
1878                 fc_host_supported_speeds(base_vha->host);
1879
1880         qla24xx_vport_disable(fc_vport, disable);
1881
1882         if (ha->flags.cpu_affinity_enabled) {
1883                 req = ha->req_q_map[1];
1884                 ql_dbg(ql_dbg_multiq, vha, 0xc000,
1885                     "Request queue %p attached with "
1886                     "VP[%d], cpu affinity =%d\n",
1887                     req, vha->vp_idx, ha->flags.cpu_affinity_enabled);
1888                 goto vport_queue;
1889         } else if (ql2xmaxqueues == 1 || !ha->npiv_info)
1890                 goto vport_queue;
1891         /* Create a request queue in QoS mode for the vport */
1892         for (cnt = 0; cnt < ha->nvram_npiv_size; cnt++) {
1893                 if (memcmp(ha->npiv_info[cnt].port_name, vha->port_name, 8) == 0
1894                         && memcmp(ha->npiv_info[cnt].node_name, vha->node_name,
1895                                         8) == 0) {
1896                         qos = ha->npiv_info[cnt].q_qos;
1897                         break;
1898                 }
1899         }
1900
1901         if (qos) {
1902                 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, 0,
1903                         qos);
1904                 if (!ret)
1905                         ql_log(ql_log_warn, vha, 0x7084,
1906                             "Can't create request queue for VP[%d]\n",
1907                             vha->vp_idx);
1908                 else {
1909                         ql_dbg(ql_dbg_multiq, vha, 0xc001,
1910                             "Request Que:%d Q0s: %d) created for VP[%d]\n",
1911                             ret, qos, vha->vp_idx);
1912                         ql_dbg(ql_dbg_user, vha, 0x7085,
1913                             "Request Que:%d Q0s: %d) created for VP[%d]\n",
1914                             ret, qos, vha->vp_idx);
1915                         req = ha->req_q_map[ret];
1916                 }
1917         }
1918
1919 vport_queue:
1920         vha->req = req;
1921         return 0;
1922
1923 vport_create_failed_2:
1924         qla24xx_disable_vp(vha);
1925         qla24xx_deallocate_vp_id(vha);
1926         scsi_host_put(vha->host);
1927         return FC_VPORT_FAILED;
1928 }
1929
1930 static int
1931 qla24xx_vport_delete(struct fc_vport *fc_vport)
1932 {
1933         scsi_qla_host_t *vha = fc_vport->dd_data;
1934         struct qla_hw_data *ha = vha->hw;
1935         uint16_t id = vha->vp_idx;
1936
1937         while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
1938             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags))
1939                 msleep(1000);
1940
1941         qla24xx_disable_vp(vha);
1942
1943         vha->flags.delete_progress = 1;
1944
1945         fc_remove_host(vha->host);
1946
1947         scsi_remove_host(vha->host);
1948
1949         /* Allow timer to run to drain queued items, when removing vp */
1950         qla24xx_deallocate_vp_id(vha);
1951
1952         if (vha->timer_active) {
1953                 qla2x00_vp_stop_timer(vha);
1954                 ql_dbg(ql_dbg_user, vha, 0x7086,
1955                     "Timer for the VP[%d] has stopped\n", vha->vp_idx);
1956         }
1957
1958         /* No pending activities shall be there on the vha now */
1959         if (ql2xextended_error_logging & ql_dbg_user)
1960                 msleep(random32()%10);  /* Just to see if something falls on
1961                                         * the net we have placed below */
1962
1963         BUG_ON(atomic_read(&vha->vref_count));
1964
1965         qla2x00_free_fcports(vha);
1966
1967         mutex_lock(&ha->vport_lock);
1968         ha->cur_vport_count--;
1969         clear_bit(vha->vp_idx, ha->vp_idx_map);
1970         mutex_unlock(&ha->vport_lock);
1971
1972         if (vha->req->id && !ha->flags.cpu_affinity_enabled) {
1973                 if (qla25xx_delete_req_que(vha, vha->req) != QLA_SUCCESS)
1974                         ql_log(ql_log_warn, vha, 0x7087,
1975                             "Queue delete failed.\n");
1976         }
1977
1978         ql_log(ql_log_info, vha, 0x7088, "VP[%d] deleted.\n", id);
1979         scsi_host_put(vha->host);
1980         return 0;
1981 }
1982
1983 static int
1984 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
1985 {
1986         scsi_qla_host_t *vha = fc_vport->dd_data;
1987
1988         if (disable)
1989                 qla24xx_disable_vp(vha);
1990         else
1991                 qla24xx_enable_vp(vha);
1992
1993         return 0;
1994 }
1995
1996 struct fc_function_template qla2xxx_transport_functions = {
1997
1998         .show_host_node_name = 1,
1999         .show_host_port_name = 1,
2000         .show_host_supported_classes = 1,
2001         .show_host_supported_speeds = 1,
2002
2003         .get_host_port_id = qla2x00_get_host_port_id,
2004         .show_host_port_id = 1,
2005         .get_host_speed = qla2x00_get_host_speed,
2006         .show_host_speed = 1,
2007         .get_host_port_type = qla2x00_get_host_port_type,
2008         .show_host_port_type = 1,
2009         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
2010         .show_host_symbolic_name = 1,
2011         .set_host_system_hostname = qla2x00_set_host_system_hostname,
2012         .show_host_system_hostname = 1,
2013         .get_host_fabric_name = qla2x00_get_host_fabric_name,
2014         .show_host_fabric_name = 1,
2015         .get_host_port_state = qla2x00_get_host_port_state,
2016         .show_host_port_state = 1,
2017
2018         .dd_fcrport_size = sizeof(struct fc_port *),
2019         .show_rport_supported_classes = 1,
2020
2021         .get_starget_node_name = qla2x00_get_starget_node_name,
2022         .show_starget_node_name = 1,
2023         .get_starget_port_name = qla2x00_get_starget_port_name,
2024         .show_starget_port_name = 1,
2025         .get_starget_port_id  = qla2x00_get_starget_port_id,
2026         .show_starget_port_id = 1,
2027
2028         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
2029         .show_rport_dev_loss_tmo = 1,
2030
2031         .issue_fc_host_lip = qla2x00_issue_lip,
2032         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
2033         .terminate_rport_io = qla2x00_terminate_rport_io,
2034         .get_fc_host_stats = qla2x00_get_fc_host_stats,
2035
2036         .vport_create = qla24xx_vport_create,
2037         .vport_disable = qla24xx_vport_disable,
2038         .vport_delete = qla24xx_vport_delete,
2039         .bsg_request = qla24xx_bsg_request,
2040         .bsg_timeout = qla24xx_bsg_timeout,
2041 };
2042
2043 struct fc_function_template qla2xxx_transport_vport_functions = {
2044
2045         .show_host_node_name = 1,
2046         .show_host_port_name = 1,
2047         .show_host_supported_classes = 1,
2048
2049         .get_host_port_id = qla2x00_get_host_port_id,
2050         .show_host_port_id = 1,
2051         .get_host_speed = qla2x00_get_host_speed,
2052         .show_host_speed = 1,
2053         .get_host_port_type = qla2x00_get_host_port_type,
2054         .show_host_port_type = 1,
2055         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
2056         .show_host_symbolic_name = 1,
2057         .set_host_system_hostname = qla2x00_set_host_system_hostname,
2058         .show_host_system_hostname = 1,
2059         .get_host_fabric_name = qla2x00_get_host_fabric_name,
2060         .show_host_fabric_name = 1,
2061         .get_host_port_state = qla2x00_get_host_port_state,
2062         .show_host_port_state = 1,
2063
2064         .dd_fcrport_size = sizeof(struct fc_port *),
2065         .show_rport_supported_classes = 1,
2066
2067         .get_starget_node_name = qla2x00_get_starget_node_name,
2068         .show_starget_node_name = 1,
2069         .get_starget_port_name = qla2x00_get_starget_port_name,
2070         .show_starget_port_name = 1,
2071         .get_starget_port_id  = qla2x00_get_starget_port_id,
2072         .show_starget_port_id = 1,
2073
2074         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
2075         .show_rport_dev_loss_tmo = 1,
2076
2077         .issue_fc_host_lip = qla2x00_issue_lip,
2078         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
2079         .terminate_rport_io = qla2x00_terminate_rport_io,
2080         .get_fc_host_stats = qla2x00_get_fc_host_stats,
2081         .bsg_request = qla24xx_bsg_request,
2082         .bsg_timeout = qla24xx_bsg_timeout,
2083 };
2084
2085 void
2086 qla2x00_init_host_attr(scsi_qla_host_t *vha)
2087 {
2088         struct qla_hw_data *ha = vha->hw;
2089         u32 speed = FC_PORTSPEED_UNKNOWN;
2090
2091         fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
2092         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
2093         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
2094         fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
2095         fc_host_max_npiv_vports(vha->host) = ha->max_npiv_vports;
2096         fc_host_npiv_vports_inuse(vha->host) = ha->cur_vport_count;
2097
2098         if (IS_CNA_CAPABLE(ha))
2099                 speed = FC_PORTSPEED_10GBIT;
2100         else if (IS_QLA25XX(ha))
2101                 speed = FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT |
2102                     FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
2103         else if (IS_QLA24XX_TYPE(ha))
2104                 speed = FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
2105                     FC_PORTSPEED_1GBIT;
2106         else if (IS_QLA23XX(ha))
2107                 speed = FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
2108         else
2109                 speed = FC_PORTSPEED_1GBIT;
2110         fc_host_supported_speeds(vha->host) = speed;
2111 }