GNU Linux-libre 5.4.200-gnu1
[releases.git] / drivers / scsi / qla2xxx / qla_attr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_target.h"
9
10 #include <linux/kthread.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14
15 static int qla24xx_vport_disable(struct fc_vport *, bool);
16
17 /* SYSFS attributes --------------------------------------------------------- */
18
19 static ssize_t
20 qla2x00_sysfs_read_fw_dump(struct file *filp, struct kobject *kobj,
21                            struct bin_attribute *bin_attr,
22                            char *buf, loff_t off, size_t count)
23 {
24         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
25             struct device, kobj)));
26         struct qla_hw_data *ha = vha->hw;
27         int rval = 0;
28
29         if (!(ha->fw_dump_reading || ha->mctp_dump_reading))
30                 return 0;
31
32         mutex_lock(&ha->optrom_mutex);
33         if (IS_P3P_TYPE(ha)) {
34                 if (off < ha->md_template_size) {
35                         rval = memory_read_from_buffer(buf, count,
36                             &off, ha->md_tmplt_hdr, ha->md_template_size);
37                 } else {
38                         off -= ha->md_template_size;
39                         rval = memory_read_from_buffer(buf, count,
40                             &off, ha->md_dump, ha->md_dump_size);
41                 }
42         } else if (ha->mctp_dumped && ha->mctp_dump_reading) {
43                 rval = memory_read_from_buffer(buf, count, &off, ha->mctp_dump,
44                     MCTP_DUMP_SIZE);
45         } else if (ha->fw_dump_reading) {
46                 rval = memory_read_from_buffer(buf, count, &off, ha->fw_dump,
47                                         ha->fw_dump_len);
48         } else {
49                 rval = 0;
50         }
51         mutex_unlock(&ha->optrom_mutex);
52         return rval;
53 }
54
55 static ssize_t
56 qla2x00_sysfs_write_fw_dump(struct file *filp, struct kobject *kobj,
57                             struct bin_attribute *bin_attr,
58                             char *buf, loff_t off, size_t count)
59 {
60         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
61             struct device, kobj)));
62         struct qla_hw_data *ha = vha->hw;
63         int reading;
64
65         if (off != 0)
66                 return (0);
67
68         reading = simple_strtol(buf, NULL, 10);
69         switch (reading) {
70         case 0:
71                 if (!ha->fw_dump_reading)
72                         break;
73
74                 ql_log(ql_log_info, vha, 0x705d,
75                     "Firmware dump cleared on (%ld).\n", vha->host_no);
76
77                 if (IS_P3P_TYPE(ha)) {
78                         qla82xx_md_free(vha);
79                         qla82xx_md_prep(vha);
80                 }
81                 ha->fw_dump_reading = 0;
82                 ha->fw_dumped = 0;
83                 break;
84         case 1:
85                 if (ha->fw_dumped && !ha->fw_dump_reading) {
86                         ha->fw_dump_reading = 1;
87
88                         ql_log(ql_log_info, vha, 0x705e,
89                             "Raw firmware dump ready for read on (%ld).\n",
90                             vha->host_no);
91                 }
92                 break;
93         case 2:
94                 qla2x00_alloc_fw_dump(vha);
95                 break;
96         case 3:
97                 if (IS_QLA82XX(ha)) {
98                         qla82xx_idc_lock(ha);
99                         qla82xx_set_reset_owner(vha);
100                         qla82xx_idc_unlock(ha);
101                 } else if (IS_QLA8044(ha)) {
102                         qla8044_idc_lock(ha);
103                         qla82xx_set_reset_owner(vha);
104                         qla8044_idc_unlock(ha);
105                 } else
106                         qla2x00_system_error(vha);
107                 break;
108         case 4:
109                 if (IS_P3P_TYPE(ha)) {
110                         if (ha->md_tmplt_hdr)
111                                 ql_dbg(ql_dbg_user, vha, 0x705b,
112                                     "MiniDump supported with this firmware.\n");
113                         else
114                                 ql_dbg(ql_dbg_user, vha, 0x709d,
115                                     "MiniDump not supported with this firmware.\n");
116                 }
117                 break;
118         case 5:
119                 if (IS_P3P_TYPE(ha))
120                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
121                 break;
122         case 6:
123                 if (!ha->mctp_dump_reading)
124                         break;
125                 ql_log(ql_log_info, vha, 0x70c1,
126                     "MCTP dump cleared on (%ld).\n", vha->host_no);
127                 ha->mctp_dump_reading = 0;
128                 ha->mctp_dumped = 0;
129                 break;
130         case 7:
131                 if (ha->mctp_dumped && !ha->mctp_dump_reading) {
132                         ha->mctp_dump_reading = 1;
133                         ql_log(ql_log_info, vha, 0x70c2,
134                             "Raw mctp dump ready for read on (%ld).\n",
135                             vha->host_no);
136                 }
137                 break;
138         }
139         return count;
140 }
141
142 static struct bin_attribute sysfs_fw_dump_attr = {
143         .attr = {
144                 .name = "fw_dump",
145                 .mode = S_IRUSR | S_IWUSR,
146         },
147         .size = 0,
148         .read = qla2x00_sysfs_read_fw_dump,
149         .write = qla2x00_sysfs_write_fw_dump,
150 };
151
152 static ssize_t
153 qla2x00_sysfs_read_nvram(struct file *filp, struct kobject *kobj,
154                          struct bin_attribute *bin_attr,
155                          char *buf, loff_t off, size_t count)
156 {
157         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
158             struct device, kobj)));
159         struct qla_hw_data *ha = vha->hw;
160         uint32_t faddr;
161         struct active_regions active_regions = { };
162
163         if (!capable(CAP_SYS_ADMIN))
164                 return 0;
165
166         mutex_lock(&ha->optrom_mutex);
167         if (qla2x00_chip_is_down(vha)) {
168                 mutex_unlock(&ha->optrom_mutex);
169                 return -EAGAIN;
170         }
171
172         if (!IS_NOCACHE_VPD_TYPE(ha)) {
173                 mutex_unlock(&ha->optrom_mutex);
174                 goto skip;
175         }
176
177         faddr = ha->flt_region_nvram;
178         if (IS_QLA28XX(ha)) {
179                 qla28xx_get_aux_images(vha, &active_regions);
180                 if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
181                         faddr = ha->flt_region_nvram_sec;
182         }
183         ha->isp_ops->read_optrom(vha, ha->nvram, faddr << 2, ha->nvram_size);
184
185         mutex_unlock(&ha->optrom_mutex);
186
187 skip:
188         return memory_read_from_buffer(buf, count, &off, ha->nvram,
189                                         ha->nvram_size);
190 }
191
192 static ssize_t
193 qla2x00_sysfs_write_nvram(struct file *filp, struct kobject *kobj,
194                           struct bin_attribute *bin_attr,
195                           char *buf, loff_t off, size_t count)
196 {
197         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
198             struct device, kobj)));
199         struct qla_hw_data *ha = vha->hw;
200         uint16_t        cnt;
201
202         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size ||
203             !ha->isp_ops->write_nvram)
204                 return -EINVAL;
205
206         /* Checksum NVRAM. */
207         if (IS_FWI2_CAPABLE(ha)) {
208                 uint32_t *iter;
209                 uint32_t chksum;
210
211                 iter = (uint32_t *)buf;
212                 chksum = 0;
213                 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++, iter++)
214                         chksum += le32_to_cpu(*iter);
215                 chksum = ~chksum + 1;
216                 *iter = cpu_to_le32(chksum);
217         } else {
218                 uint8_t *iter;
219                 uint8_t chksum;
220
221                 iter = (uint8_t *)buf;
222                 chksum = 0;
223                 for (cnt = 0; cnt < count - 1; cnt++)
224                         chksum += *iter++;
225                 chksum = ~chksum + 1;
226                 *iter = chksum;
227         }
228
229         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
230                 ql_log(ql_log_warn, vha, 0x705f,
231                     "HBA not online, failing NVRAM update.\n");
232                 return -EAGAIN;
233         }
234
235         mutex_lock(&ha->optrom_mutex);
236         if (qla2x00_chip_is_down(vha)) {
237                 mutex_unlock(&ha->optrom_mutex);
238                 return -EAGAIN;
239         }
240
241         /* Write NVRAM. */
242         ha->isp_ops->write_nvram(vha, buf, ha->nvram_base, count);
243         ha->isp_ops->read_nvram(vha, ha->nvram, ha->nvram_base,
244             count);
245         mutex_unlock(&ha->optrom_mutex);
246
247         ql_dbg(ql_dbg_user, vha, 0x7060,
248             "Setting ISP_ABORT_NEEDED\n");
249         /* NVRAM settings take effect immediately. */
250         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
251         qla2xxx_wake_dpc(vha);
252         qla2x00_wait_for_chip_reset(vha);
253
254         return count;
255 }
256
257 static struct bin_attribute sysfs_nvram_attr = {
258         .attr = {
259                 .name = "nvram",
260                 .mode = S_IRUSR | S_IWUSR,
261         },
262         .size = 512,
263         .read = qla2x00_sysfs_read_nvram,
264         .write = qla2x00_sysfs_write_nvram,
265 };
266
267 static ssize_t
268 qla2x00_sysfs_read_optrom(struct file *filp, struct kobject *kobj,
269                           struct bin_attribute *bin_attr,
270                           char *buf, loff_t off, size_t count)
271 {
272         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
273             struct device, kobj)));
274         struct qla_hw_data *ha = vha->hw;
275         ssize_t rval = 0;
276
277         mutex_lock(&ha->optrom_mutex);
278
279         if (ha->optrom_state != QLA_SREADING)
280                 goto out;
281
282         rval = memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
283             ha->optrom_region_size);
284
285 out:
286         mutex_unlock(&ha->optrom_mutex);
287
288         return rval;
289 }
290
291 static ssize_t
292 qla2x00_sysfs_write_optrom(struct file *filp, struct kobject *kobj,
293                            struct bin_attribute *bin_attr,
294                            char *buf, loff_t off, size_t count)
295 {
296         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
297             struct device, kobj)));
298         struct qla_hw_data *ha = vha->hw;
299
300         mutex_lock(&ha->optrom_mutex);
301
302         if (ha->optrom_state != QLA_SWRITING) {
303                 mutex_unlock(&ha->optrom_mutex);
304                 return -EINVAL;
305         }
306         if (off > ha->optrom_region_size) {
307                 mutex_unlock(&ha->optrom_mutex);
308                 return -ERANGE;
309         }
310         if (off + count > ha->optrom_region_size)
311                 count = ha->optrom_region_size - off;
312
313         memcpy(&ha->optrom_buffer[off], buf, count);
314         mutex_unlock(&ha->optrom_mutex);
315
316         return count;
317 }
318
319 static struct bin_attribute sysfs_optrom_attr = {
320         .attr = {
321                 .name = "optrom",
322                 .mode = S_IRUSR | S_IWUSR,
323         },
324         .size = 0,
325         .read = qla2x00_sysfs_read_optrom,
326         .write = qla2x00_sysfs_write_optrom,
327 };
328
329 static ssize_t
330 qla2x00_sysfs_write_optrom_ctl(struct file *filp, struct kobject *kobj,
331                                struct bin_attribute *bin_attr,
332                                char *buf, loff_t off, size_t count)
333 {
334         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
335             struct device, kobj)));
336         struct qla_hw_data *ha = vha->hw;
337         uint32_t start = 0;
338         uint32_t size = ha->optrom_size;
339         int val, valid;
340         ssize_t rval = count;
341
342         if (off)
343                 return -EINVAL;
344
345         if (unlikely(pci_channel_offline(ha->pdev)))
346                 return -EAGAIN;
347
348         if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
349                 return -EINVAL;
350         if (start > ha->optrom_size)
351                 return -EINVAL;
352         if (size > ha->optrom_size - start)
353                 size = ha->optrom_size - start;
354
355         mutex_lock(&ha->optrom_mutex);
356         if (qla2x00_chip_is_down(vha)) {
357                 mutex_unlock(&ha->optrom_mutex);
358                 return -EAGAIN;
359         }
360         switch (val) {
361         case 0:
362                 if (ha->optrom_state != QLA_SREADING &&
363                     ha->optrom_state != QLA_SWRITING) {
364                         rval =  -EINVAL;
365                         goto out;
366                 }
367                 ha->optrom_state = QLA_SWAITING;
368
369                 ql_dbg(ql_dbg_user, vha, 0x7061,
370                     "Freeing flash region allocation -- 0x%x bytes.\n",
371                     ha->optrom_region_size);
372
373                 vfree(ha->optrom_buffer);
374                 ha->optrom_buffer = NULL;
375                 break;
376         case 1:
377                 if (ha->optrom_state != QLA_SWAITING) {
378                         rval = -EINVAL;
379                         goto out;
380                 }
381
382                 ha->optrom_region_start = start;
383                 ha->optrom_region_size = size;
384
385                 ha->optrom_state = QLA_SREADING;
386                 ha->optrom_buffer = vzalloc(ha->optrom_region_size);
387                 if (ha->optrom_buffer == NULL) {
388                         ql_log(ql_log_warn, vha, 0x7062,
389                             "Unable to allocate memory for optrom retrieval "
390                             "(%x).\n", ha->optrom_region_size);
391
392                         ha->optrom_state = QLA_SWAITING;
393                         rval = -ENOMEM;
394                         goto out;
395                 }
396
397                 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
398                         ql_log(ql_log_warn, vha, 0x7063,
399                             "HBA not online, failing NVRAM update.\n");
400                         rval = -EAGAIN;
401                         goto out;
402                 }
403
404                 ql_dbg(ql_dbg_user, vha, 0x7064,
405                     "Reading flash region -- 0x%x/0x%x.\n",
406                     ha->optrom_region_start, ha->optrom_region_size);
407
408                 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
409                     ha->optrom_region_start, ha->optrom_region_size);
410                 break;
411         case 2:
412                 if (ha->optrom_state != QLA_SWAITING) {
413                         rval = -EINVAL;
414                         goto out;
415                 }
416
417                 /*
418                  * We need to be more restrictive on which FLASH regions are
419                  * allowed to be updated via user-space.  Regions accessible
420                  * via this method include:
421                  *
422                  * ISP21xx/ISP22xx/ISP23xx type boards:
423                  *
424                  *      0x000000 -> 0x020000 -- Boot code.
425                  *
426                  * ISP2322/ISP24xx type boards:
427                  *
428                  *      0x000000 -> 0x07ffff -- Boot code.
429                  *      0x080000 -> 0x0fffff -- Firmware.
430                  *
431                  * ISP25xx type boards:
432                  *
433                  *      0x000000 -> 0x07ffff -- Boot code.
434                  *      0x080000 -> 0x0fffff -- Firmware.
435                  *      0x120000 -> 0x12ffff -- VPD and HBA parameters.
436                  *
437                  * > ISP25xx type boards:
438                  *
439                  *      None -- should go through BSG.
440                  */
441                 valid = 0;
442                 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
443                         valid = 1;
444                 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
445                         valid = 1;
446                 if (!valid) {
447                         ql_log(ql_log_warn, vha, 0x7065,
448                             "Invalid start region 0x%x/0x%x.\n", start, size);
449                         rval = -EINVAL;
450                         goto out;
451                 }
452
453                 ha->optrom_region_start = start;
454                 ha->optrom_region_size = size;
455
456                 ha->optrom_state = QLA_SWRITING;
457                 ha->optrom_buffer = vzalloc(ha->optrom_region_size);
458                 if (ha->optrom_buffer == NULL) {
459                         ql_log(ql_log_warn, vha, 0x7066,
460                             "Unable to allocate memory for optrom update "
461                             "(%x)\n", ha->optrom_region_size);
462
463                         ha->optrom_state = QLA_SWAITING;
464                         rval = -ENOMEM;
465                         goto out;
466                 }
467
468                 ql_dbg(ql_dbg_user, vha, 0x7067,
469                     "Staging flash region write -- 0x%x/0x%x.\n",
470                     ha->optrom_region_start, ha->optrom_region_size);
471
472                 break;
473         case 3:
474                 if (ha->optrom_state != QLA_SWRITING) {
475                         rval = -EINVAL;
476                         goto out;
477                 }
478
479                 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
480                         ql_log(ql_log_warn, vha, 0x7068,
481                             "HBA not online, failing flash update.\n");
482                         rval = -EAGAIN;
483                         goto out;
484                 }
485
486                 ql_dbg(ql_dbg_user, vha, 0x7069,
487                     "Writing flash region -- 0x%x/0x%x.\n",
488                     ha->optrom_region_start, ha->optrom_region_size);
489
490                 rval = ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
491                     ha->optrom_region_start, ha->optrom_region_size);
492                 if (rval)
493                         rval = -EIO;
494                 break;
495         default:
496                 rval = -EINVAL;
497         }
498
499 out:
500         mutex_unlock(&ha->optrom_mutex);
501         return rval;
502 }
503
504 static struct bin_attribute sysfs_optrom_ctl_attr = {
505         .attr = {
506                 .name = "optrom_ctl",
507                 .mode = S_IWUSR,
508         },
509         .size = 0,
510         .write = qla2x00_sysfs_write_optrom_ctl,
511 };
512
513 static ssize_t
514 qla2x00_sysfs_read_vpd(struct file *filp, struct kobject *kobj,
515                        struct bin_attribute *bin_attr,
516                        char *buf, loff_t off, size_t count)
517 {
518         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
519             struct device, kobj)));
520         struct qla_hw_data *ha = vha->hw;
521         uint32_t faddr;
522         struct active_regions active_regions = { };
523
524         if (unlikely(pci_channel_offline(ha->pdev)))
525                 return -EAGAIN;
526
527         if (!capable(CAP_SYS_ADMIN))
528                 return -EINVAL;
529
530         if (!IS_NOCACHE_VPD_TYPE(ha))
531                 goto skip;
532
533         faddr = ha->flt_region_vpd << 2;
534
535         if (IS_QLA28XX(ha)) {
536                 qla28xx_get_aux_images(vha, &active_regions);
537                 if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
538                         faddr = ha->flt_region_vpd_sec << 2;
539
540                 ql_dbg(ql_dbg_init, vha, 0x7070,
541                     "Loading %s nvram image.\n",
542                     active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
543                     "primary" : "secondary");
544         }
545
546         mutex_lock(&ha->optrom_mutex);
547         if (qla2x00_chip_is_down(vha)) {
548                 mutex_unlock(&ha->optrom_mutex);
549                 return -EAGAIN;
550         }
551
552         ha->isp_ops->read_optrom(vha, ha->vpd, faddr, ha->vpd_size);
553         mutex_unlock(&ha->optrom_mutex);
554
555         ha->isp_ops->read_optrom(vha, ha->vpd, faddr, ha->vpd_size);
556 skip:
557         return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
558 }
559
560 static ssize_t
561 qla2x00_sysfs_write_vpd(struct file *filp, struct kobject *kobj,
562                         struct bin_attribute *bin_attr,
563                         char *buf, loff_t off, size_t count)
564 {
565         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
566             struct device, kobj)));
567         struct qla_hw_data *ha = vha->hw;
568         uint8_t *tmp_data;
569
570         if (unlikely(pci_channel_offline(ha->pdev)))
571                 return 0;
572
573         if (qla2x00_chip_is_down(vha))
574                 return 0;
575
576         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size ||
577             !ha->isp_ops->write_nvram)
578                 return 0;
579
580         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
581                 ql_log(ql_log_warn, vha, 0x706a,
582                     "HBA not online, failing VPD update.\n");
583                 return -EAGAIN;
584         }
585
586         mutex_lock(&ha->optrom_mutex);
587         if (qla2x00_chip_is_down(vha)) {
588                 mutex_unlock(&ha->optrom_mutex);
589                 return -EAGAIN;
590         }
591
592         /* Write NVRAM. */
593         ha->isp_ops->write_nvram(vha, buf, ha->vpd_base, count);
594         ha->isp_ops->read_nvram(vha, ha->vpd, ha->vpd_base, count);
595
596         /* Update flash version information for 4Gb & above. */
597         if (!IS_FWI2_CAPABLE(ha)) {
598                 mutex_unlock(&ha->optrom_mutex);
599                 return -EINVAL;
600         }
601
602         tmp_data = vmalloc(256);
603         if (!tmp_data) {
604                 mutex_unlock(&ha->optrom_mutex);
605                 ql_log(ql_log_warn, vha, 0x706b,
606                     "Unable to allocate memory for VPD information update.\n");
607                 return -ENOMEM;
608         }
609         ha->isp_ops->get_flash_version(vha, tmp_data);
610         vfree(tmp_data);
611
612         mutex_unlock(&ha->optrom_mutex);
613
614         return count;
615 }
616
617 static struct bin_attribute sysfs_vpd_attr = {
618         .attr = {
619                 .name = "vpd",
620                 .mode = S_IRUSR | S_IWUSR,
621         },
622         .size = 0,
623         .read = qla2x00_sysfs_read_vpd,
624         .write = qla2x00_sysfs_write_vpd,
625 };
626
627 static ssize_t
628 qla2x00_sysfs_read_sfp(struct file *filp, struct kobject *kobj,
629                        struct bin_attribute *bin_attr,
630                        char *buf, loff_t off, size_t count)
631 {
632         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
633             struct device, kobj)));
634         int rval;
635
636         if (!capable(CAP_SYS_ADMIN) || off != 0 || count < SFP_DEV_SIZE)
637                 return 0;
638
639         mutex_lock(&vha->hw->optrom_mutex);
640         if (qla2x00_chip_is_down(vha)) {
641                 mutex_unlock(&vha->hw->optrom_mutex);
642                 return 0;
643         }
644
645         rval = qla2x00_read_sfp_dev(vha, buf, count);
646         mutex_unlock(&vha->hw->optrom_mutex);
647
648         if (rval)
649                 return -EIO;
650
651         return count;
652 }
653
654 static struct bin_attribute sysfs_sfp_attr = {
655         .attr = {
656                 .name = "sfp",
657                 .mode = S_IRUSR | S_IWUSR,
658         },
659         .size = SFP_DEV_SIZE,
660         .read = qla2x00_sysfs_read_sfp,
661 };
662
663 static ssize_t
664 qla2x00_sysfs_write_reset(struct file *filp, struct kobject *kobj,
665                         struct bin_attribute *bin_attr,
666                         char *buf, loff_t off, size_t count)
667 {
668         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
669             struct device, kobj)));
670         struct qla_hw_data *ha = vha->hw;
671         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
672         int type;
673         uint32_t idc_control;
674         uint8_t *tmp_data = NULL;
675
676         if (off != 0)
677                 return -EINVAL;
678
679         type = simple_strtol(buf, NULL, 10);
680         switch (type) {
681         case 0x2025c:
682                 ql_log(ql_log_info, vha, 0x706e,
683                     "Issuing ISP reset.\n");
684
685                 scsi_block_requests(vha->host);
686                 if (IS_QLA82XX(ha)) {
687                         ha->flags.isp82xx_no_md_cap = 1;
688                         qla82xx_idc_lock(ha);
689                         qla82xx_set_reset_owner(vha);
690                         qla82xx_idc_unlock(ha);
691                 } else if (IS_QLA8044(ha)) {
692                         qla8044_idc_lock(ha);
693                         idc_control = qla8044_rd_reg(ha,
694                             QLA8044_IDC_DRV_CTRL);
695                         qla8044_wr_reg(ha, QLA8044_IDC_DRV_CTRL,
696                             (idc_control | GRACEFUL_RESET_BIT1));
697                         qla82xx_set_reset_owner(vha);
698                         qla8044_idc_unlock(ha);
699                 } else {
700                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
701                         qla2xxx_wake_dpc(vha);
702                 }
703                 qla2x00_wait_for_chip_reset(vha);
704                 scsi_unblock_requests(vha->host);
705                 break;
706         case 0x2025d:
707                 if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha))
708                         return -EPERM;
709
710                 ql_log(ql_log_info, vha, 0x706f,
711                     "Issuing MPI reset.\n");
712
713                 if (IS_QLA83XX(ha)) {
714                         uint32_t idc_control;
715
716                         qla83xx_idc_lock(vha, 0);
717                         __qla83xx_get_idc_control(vha, &idc_control);
718                         idc_control |= QLA83XX_IDC_GRACEFUL_RESET;
719                         __qla83xx_set_idc_control(vha, idc_control);
720                         qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
721                             QLA8XXX_DEV_NEED_RESET);
722                         qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
723                         qla83xx_idc_unlock(vha, 0);
724                         break;
725                 } else {
726                         /* Make sure FC side is not in reset */
727                         WARN_ON_ONCE(qla2x00_wait_for_hba_online(vha) !=
728                                      QLA_SUCCESS);
729
730                         /* Issue MPI reset */
731                         scsi_block_requests(vha->host);
732                         if (qla81xx_restart_mpi_firmware(vha) != QLA_SUCCESS)
733                                 ql_log(ql_log_warn, vha, 0x7070,
734                                     "MPI reset failed.\n");
735                         scsi_unblock_requests(vha->host);
736                         break;
737                 }
738         case 0x2025e:
739                 if (!IS_P3P_TYPE(ha) || vha != base_vha) {
740                         ql_log(ql_log_info, vha, 0x7071,
741                             "FCoE ctx reset not supported.\n");
742                         return -EPERM;
743                 }
744
745                 ql_log(ql_log_info, vha, 0x7072,
746                     "Issuing FCoE ctx reset.\n");
747                 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
748                 qla2xxx_wake_dpc(vha);
749                 qla2x00_wait_for_fcoe_ctx_reset(vha);
750                 break;
751         case 0x2025f:
752                 if (!IS_QLA8031(ha))
753                         return -EPERM;
754                 ql_log(ql_log_info, vha, 0x70bc,
755                     "Disabling Reset by IDC control\n");
756                 qla83xx_idc_lock(vha, 0);
757                 __qla83xx_get_idc_control(vha, &idc_control);
758                 idc_control |= QLA83XX_IDC_RESET_DISABLED;
759                 __qla83xx_set_idc_control(vha, idc_control);
760                 qla83xx_idc_unlock(vha, 0);
761                 break;
762         case 0x20260:
763                 if (!IS_QLA8031(ha))
764                         return -EPERM;
765                 ql_log(ql_log_info, vha, 0x70bd,
766                     "Enabling Reset by IDC control\n");
767                 qla83xx_idc_lock(vha, 0);
768                 __qla83xx_get_idc_control(vha, &idc_control);
769                 idc_control &= ~QLA83XX_IDC_RESET_DISABLED;
770                 __qla83xx_set_idc_control(vha, idc_control);
771                 qla83xx_idc_unlock(vha, 0);
772                 break;
773         case 0x20261:
774                 ql_dbg(ql_dbg_user, vha, 0x70e0,
775                     "Updating cache versions without reset ");
776
777                 tmp_data = vmalloc(256);
778                 if (!tmp_data) {
779                         ql_log(ql_log_warn, vha, 0x70e1,
780                             "Unable to allocate memory for VPD information update.\n");
781                         return -ENOMEM;
782                 }
783                 ha->isp_ops->get_flash_version(vha, tmp_data);
784                 vfree(tmp_data);
785                 break;
786         }
787         return count;
788 }
789
790 static struct bin_attribute sysfs_reset_attr = {
791         .attr = {
792                 .name = "reset",
793                 .mode = S_IWUSR,
794         },
795         .size = 0,
796         .write = qla2x00_sysfs_write_reset,
797 };
798
799 static ssize_t
800 qla2x00_issue_logo(struct file *filp, struct kobject *kobj,
801                         struct bin_attribute *bin_attr,
802                         char *buf, loff_t off, size_t count)
803 {
804         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
805             struct device, kobj)));
806         int type;
807         port_id_t did;
808
809         if (!capable(CAP_SYS_ADMIN))
810                 return 0;
811
812         if (unlikely(pci_channel_offline(vha->hw->pdev)))
813                 return 0;
814
815         if (qla2x00_chip_is_down(vha))
816                 return 0;
817
818         type = simple_strtol(buf, NULL, 10);
819
820         did.b.domain = (type & 0x00ff0000) >> 16;
821         did.b.area = (type & 0x0000ff00) >> 8;
822         did.b.al_pa = (type & 0x000000ff);
823
824         ql_log(ql_log_info, vha, 0xd04d, "portid=%02x%02x%02x done\n",
825             did.b.domain, did.b.area, did.b.al_pa);
826
827         ql_log(ql_log_info, vha, 0x70e4, "%s: %d\n", __func__, type);
828
829         qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, did);
830         return count;
831 }
832
833 static struct bin_attribute sysfs_issue_logo_attr = {
834         .attr = {
835                 .name = "issue_logo",
836                 .mode = S_IWUSR,
837         },
838         .size = 0,
839         .write = qla2x00_issue_logo,
840 };
841
842 static ssize_t
843 qla2x00_sysfs_read_xgmac_stats(struct file *filp, struct kobject *kobj,
844                        struct bin_attribute *bin_attr,
845                        char *buf, loff_t off, size_t count)
846 {
847         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
848             struct device, kobj)));
849         struct qla_hw_data *ha = vha->hw;
850         int rval;
851         uint16_t actual_size;
852
853         if (!capable(CAP_SYS_ADMIN) || off != 0 || count > XGMAC_DATA_SIZE)
854                 return 0;
855
856         if (unlikely(pci_channel_offline(ha->pdev)))
857                 return 0;
858         mutex_lock(&vha->hw->optrom_mutex);
859         if (qla2x00_chip_is_down(vha)) {
860                 mutex_unlock(&vha->hw->optrom_mutex);
861                 return 0;
862         }
863
864         if (ha->xgmac_data)
865                 goto do_read;
866
867         ha->xgmac_data = dma_alloc_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
868             &ha->xgmac_data_dma, GFP_KERNEL);
869         if (!ha->xgmac_data) {
870                 mutex_unlock(&vha->hw->optrom_mutex);
871                 ql_log(ql_log_warn, vha, 0x7076,
872                     "Unable to allocate memory for XGMAC read-data.\n");
873                 return 0;
874         }
875
876 do_read:
877         actual_size = 0;
878         memset(ha->xgmac_data, 0, XGMAC_DATA_SIZE);
879
880         rval = qla2x00_get_xgmac_stats(vha, ha->xgmac_data_dma,
881             XGMAC_DATA_SIZE, &actual_size);
882
883         mutex_unlock(&vha->hw->optrom_mutex);
884         if (rval != QLA_SUCCESS) {
885                 ql_log(ql_log_warn, vha, 0x7077,
886                     "Unable to read XGMAC data (%x).\n", rval);
887                 count = 0;
888         }
889
890         count = actual_size > count ? count : actual_size;
891         memcpy(buf, ha->xgmac_data, count);
892
893         return count;
894 }
895
896 static struct bin_attribute sysfs_xgmac_stats_attr = {
897         .attr = {
898                 .name = "xgmac_stats",
899                 .mode = S_IRUSR,
900         },
901         .size = 0,
902         .read = qla2x00_sysfs_read_xgmac_stats,
903 };
904
905 static ssize_t
906 qla2x00_sysfs_read_dcbx_tlv(struct file *filp, struct kobject *kobj,
907                        struct bin_attribute *bin_attr,
908                        char *buf, loff_t off, size_t count)
909 {
910         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
911             struct device, kobj)));
912         struct qla_hw_data *ha = vha->hw;
913         int rval;
914
915         if (!capable(CAP_SYS_ADMIN) || off != 0 || count > DCBX_TLV_DATA_SIZE)
916                 return 0;
917
918         if (ha->dcbx_tlv)
919                 goto do_read;
920         mutex_lock(&vha->hw->optrom_mutex);
921         if (qla2x00_chip_is_down(vha)) {
922                 mutex_unlock(&vha->hw->optrom_mutex);
923                 return 0;
924         }
925
926         ha->dcbx_tlv = dma_alloc_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
927             &ha->dcbx_tlv_dma, GFP_KERNEL);
928         if (!ha->dcbx_tlv) {
929                 mutex_unlock(&vha->hw->optrom_mutex);
930                 ql_log(ql_log_warn, vha, 0x7078,
931                     "Unable to allocate memory for DCBX TLV read-data.\n");
932                 return -ENOMEM;
933         }
934
935 do_read:
936         memset(ha->dcbx_tlv, 0, DCBX_TLV_DATA_SIZE);
937
938         rval = qla2x00_get_dcbx_params(vha, ha->dcbx_tlv_dma,
939             DCBX_TLV_DATA_SIZE);
940
941         mutex_unlock(&vha->hw->optrom_mutex);
942
943         if (rval != QLA_SUCCESS) {
944                 ql_log(ql_log_warn, vha, 0x7079,
945                     "Unable to read DCBX TLV (%x).\n", rval);
946                 return -EIO;
947         }
948
949         memcpy(buf, ha->dcbx_tlv, count);
950
951         return count;
952 }
953
954 static struct bin_attribute sysfs_dcbx_tlv_attr = {
955         .attr = {
956                 .name = "dcbx_tlv",
957                 .mode = S_IRUSR,
958         },
959         .size = 0,
960         .read = qla2x00_sysfs_read_dcbx_tlv,
961 };
962
963 static struct sysfs_entry {
964         char *name;
965         struct bin_attribute *attr;
966         int type;
967 } bin_file_entries[] = {
968         { "fw_dump", &sysfs_fw_dump_attr, },
969         { "nvram", &sysfs_nvram_attr, },
970         { "optrom", &sysfs_optrom_attr, },
971         { "optrom_ctl", &sysfs_optrom_ctl_attr, },
972         { "vpd", &sysfs_vpd_attr, 1 },
973         { "sfp", &sysfs_sfp_attr, 1 },
974         { "reset", &sysfs_reset_attr, },
975         { "issue_logo", &sysfs_issue_logo_attr, },
976         { "xgmac_stats", &sysfs_xgmac_stats_attr, 3 },
977         { "dcbx_tlv", &sysfs_dcbx_tlv_attr, 3 },
978         { NULL },
979 };
980
981 void
982 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *vha)
983 {
984         struct Scsi_Host *host = vha->host;
985         struct sysfs_entry *iter;
986         int ret;
987
988         for (iter = bin_file_entries; iter->name; iter++) {
989                 if (iter->type && !IS_FWI2_CAPABLE(vha->hw))
990                         continue;
991                 if (iter->type == 2 && !IS_QLA25XX(vha->hw))
992                         continue;
993                 if (iter->type == 3 && !(IS_CNA_CAPABLE(vha->hw)))
994                         continue;
995
996                 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
997                     iter->attr);
998                 if (ret)
999                         ql_log(ql_log_warn, vha, 0x00f3,
1000                             "Unable to create sysfs %s binary attribute (%d).\n",
1001                             iter->name, ret);
1002                 else
1003                         ql_dbg(ql_dbg_init, vha, 0x00f4,
1004                             "Successfully created sysfs %s binary attribute.\n",
1005                             iter->name);
1006         }
1007 }
1008
1009 void
1010 qla2x00_free_sysfs_attr(scsi_qla_host_t *vha, bool stop_beacon)
1011 {
1012         struct Scsi_Host *host = vha->host;
1013         struct sysfs_entry *iter;
1014         struct qla_hw_data *ha = vha->hw;
1015
1016         for (iter = bin_file_entries; iter->name; iter++) {
1017                 if (iter->type && !IS_FWI2_CAPABLE(ha))
1018                         continue;
1019                 if (iter->type == 2 && !IS_QLA25XX(ha))
1020                         continue;
1021                 if (iter->type == 3 && !(IS_CNA_CAPABLE(ha)))
1022                         continue;
1023
1024                 sysfs_remove_bin_file(&host->shost_gendev.kobj,
1025                     iter->attr);
1026         }
1027
1028         if (stop_beacon && ha->beacon_blink_led == 1)
1029                 ha->isp_ops->beacon_off(vha);
1030 }
1031
1032 /* Scsi_Host attributes. */
1033
1034 static ssize_t
1035 qla2x00_driver_version_show(struct device *dev,
1036                           struct device_attribute *attr, char *buf)
1037 {
1038         return scnprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
1039 }
1040
1041 static ssize_t
1042 qla2x00_fw_version_show(struct device *dev,
1043                         struct device_attribute *attr, char *buf)
1044 {
1045         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1046         struct qla_hw_data *ha = vha->hw;
1047         char fw_str[128];
1048
1049         return scnprintf(buf, PAGE_SIZE, "%s\n",
1050             ha->isp_ops->fw_version_str(vha, fw_str, sizeof(fw_str)));
1051 }
1052
1053 static ssize_t
1054 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
1055                         char *buf)
1056 {
1057         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1058         struct qla_hw_data *ha = vha->hw;
1059         uint32_t sn;
1060
1061         if (IS_QLAFX00(vha->hw)) {
1062                 return scnprintf(buf, PAGE_SIZE, "%s\n",
1063                     vha->hw->mr.serial_num);
1064         } else if (IS_FWI2_CAPABLE(ha)) {
1065                 qla2xxx_get_vpd_field(vha, "SN", buf, PAGE_SIZE - 1);
1066                 return strlen(strcat(buf, "\n"));
1067         }
1068
1069         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
1070         return scnprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
1071             sn % 100000);
1072 }
1073
1074 static ssize_t
1075 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
1076                       char *buf)
1077 {
1078         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1079
1080         return scnprintf(buf, PAGE_SIZE, "ISP%04X\n", vha->hw->pdev->device);
1081 }
1082
1083 static ssize_t
1084 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
1085                     char *buf)
1086 {
1087         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1088         struct qla_hw_data *ha = vha->hw;
1089
1090         if (IS_QLAFX00(vha->hw))
1091                 return scnprintf(buf, PAGE_SIZE, "%s\n",
1092                     vha->hw->mr.hw_version);
1093
1094         return scnprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
1095             ha->product_id[0], ha->product_id[1], ha->product_id[2],
1096             ha->product_id[3]);
1097 }
1098
1099 static ssize_t
1100 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
1101                         char *buf)
1102 {
1103         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1104
1105         return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_number);
1106 }
1107
1108 static ssize_t
1109 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
1110                         char *buf)
1111 {
1112         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1113
1114         return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_desc);
1115 }
1116
1117 static ssize_t
1118 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
1119                       char *buf)
1120 {
1121         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1122         char pci_info[30];
1123
1124         return scnprintf(buf, PAGE_SIZE, "%s\n",
1125                          vha->hw->isp_ops->pci_info_str(vha, pci_info,
1126                                                         sizeof(pci_info)));
1127 }
1128
1129 static ssize_t
1130 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
1131                         char *buf)
1132 {
1133         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1134         struct qla_hw_data *ha = vha->hw;
1135         int len = 0;
1136
1137         if (atomic_read(&vha->loop_state) == LOOP_DOWN ||
1138             atomic_read(&vha->loop_state) == LOOP_DEAD ||
1139             vha->device_flags & DFLG_NO_CABLE)
1140                 len = scnprintf(buf, PAGE_SIZE, "Link Down\n");
1141         else if (atomic_read(&vha->loop_state) != LOOP_READY ||
1142             qla2x00_chip_is_down(vha))
1143                 len = scnprintf(buf, PAGE_SIZE, "Unknown Link State\n");
1144         else {
1145                 len = scnprintf(buf, PAGE_SIZE, "Link Up - ");
1146
1147                 switch (ha->current_topology) {
1148                 case ISP_CFG_NL:
1149                         len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1150                         break;
1151                 case ISP_CFG_FL:
1152                         len += scnprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
1153                         break;
1154                 case ISP_CFG_N:
1155                         len += scnprintf(buf + len, PAGE_SIZE-len,
1156                             "N_Port to N_Port\n");
1157                         break;
1158                 case ISP_CFG_F:
1159                         len += scnprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
1160                         break;
1161                 default:
1162                         len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1163                         break;
1164                 }
1165         }
1166         return len;
1167 }
1168
1169 static ssize_t
1170 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
1171                  char *buf)
1172 {
1173         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1174         int len = 0;
1175
1176         switch (vha->hw->zio_mode) {
1177         case QLA_ZIO_MODE_6:
1178                 len += scnprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
1179                 break;
1180         case QLA_ZIO_DISABLED:
1181                 len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1182                 break;
1183         }
1184         return len;
1185 }
1186
1187 static ssize_t
1188 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
1189                   const char *buf, size_t count)
1190 {
1191         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1192         struct qla_hw_data *ha = vha->hw;
1193         int val = 0;
1194         uint16_t zio_mode;
1195
1196         if (!IS_ZIO_SUPPORTED(ha))
1197                 return -ENOTSUPP;
1198
1199         if (sscanf(buf, "%d", &val) != 1)
1200                 return -EINVAL;
1201
1202         if (val)
1203                 zio_mode = QLA_ZIO_MODE_6;
1204         else
1205                 zio_mode = QLA_ZIO_DISABLED;
1206
1207         /* Update per-hba values and queue a reset. */
1208         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
1209                 ha->zio_mode = zio_mode;
1210                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1211         }
1212         return strlen(buf);
1213 }
1214
1215 static ssize_t
1216 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
1217                        char *buf)
1218 {
1219         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1220
1221         return scnprintf(buf, PAGE_SIZE, "%d us\n", vha->hw->zio_timer * 100);
1222 }
1223
1224 static ssize_t
1225 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
1226                         const char *buf, size_t count)
1227 {
1228         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1229         int val = 0;
1230         uint16_t zio_timer;
1231
1232         if (sscanf(buf, "%d", &val) != 1)
1233                 return -EINVAL;
1234         if (val > 25500 || val < 100)
1235                 return -ERANGE;
1236
1237         zio_timer = (uint16_t)(val / 100);
1238         vha->hw->zio_timer = zio_timer;
1239
1240         return strlen(buf);
1241 }
1242
1243 static ssize_t
1244 qla_zio_threshold_show(struct device *dev, struct device_attribute *attr,
1245                        char *buf)
1246 {
1247         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1248
1249         return scnprintf(buf, PAGE_SIZE, "%d exchanges\n",
1250             vha->hw->last_zio_threshold);
1251 }
1252
1253 static ssize_t
1254 qla_zio_threshold_store(struct device *dev, struct device_attribute *attr,
1255     const char *buf, size_t count)
1256 {
1257         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1258         int val = 0;
1259
1260         if (vha->hw->zio_mode != QLA_ZIO_MODE_6)
1261                 return -EINVAL;
1262         if (sscanf(buf, "%d", &val) != 1)
1263                 return -EINVAL;
1264         if (val < 0 || val > 256)
1265                 return -ERANGE;
1266
1267         atomic_set(&vha->hw->zio_threshold, val);
1268         return strlen(buf);
1269 }
1270
1271 static ssize_t
1272 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
1273                     char *buf)
1274 {
1275         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1276         int len = 0;
1277
1278         if (vha->hw->beacon_blink_led)
1279                 len += scnprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
1280         else
1281                 len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1282         return len;
1283 }
1284
1285 static ssize_t
1286 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
1287                      const char *buf, size_t count)
1288 {
1289         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1290         struct qla_hw_data *ha = vha->hw;
1291         int val = 0;
1292         int rval;
1293
1294         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1295                 return -EPERM;
1296
1297         if (sscanf(buf, "%d", &val) != 1)
1298                 return -EINVAL;
1299
1300         mutex_lock(&vha->hw->optrom_mutex);
1301         if (qla2x00_chip_is_down(vha)) {
1302                 mutex_unlock(&vha->hw->optrom_mutex);
1303                 ql_log(ql_log_warn, vha, 0x707a,
1304                     "Abort ISP active -- ignoring beacon request.\n");
1305                 return -EBUSY;
1306         }
1307
1308         if (val)
1309                 rval = ha->isp_ops->beacon_on(vha);
1310         else
1311                 rval = ha->isp_ops->beacon_off(vha);
1312
1313         if (rval != QLA_SUCCESS)
1314                 count = 0;
1315
1316         mutex_unlock(&vha->hw->optrom_mutex);
1317
1318         return count;
1319 }
1320
1321 static ssize_t
1322 qla2x00_optrom_bios_version_show(struct device *dev,
1323                                  struct device_attribute *attr, char *buf)
1324 {
1325         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1326         struct qla_hw_data *ha = vha->hw;
1327
1328         return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
1329             ha->bios_revision[0]);
1330 }
1331
1332 static ssize_t
1333 qla2x00_optrom_efi_version_show(struct device *dev,
1334                                 struct device_attribute *attr, char *buf)
1335 {
1336         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1337         struct qla_hw_data *ha = vha->hw;
1338
1339         return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
1340             ha->efi_revision[0]);
1341 }
1342
1343 static ssize_t
1344 qla2x00_optrom_fcode_version_show(struct device *dev,
1345                                   struct device_attribute *attr, char *buf)
1346 {
1347         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1348         struct qla_hw_data *ha = vha->hw;
1349
1350         return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
1351             ha->fcode_revision[0]);
1352 }
1353
1354 static ssize_t
1355 qla2x00_optrom_fw_version_show(struct device *dev,
1356                                struct device_attribute *attr, char *buf)
1357 {
1358         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1359         struct qla_hw_data *ha = vha->hw;
1360
1361         return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
1362             ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
1363             ha->fw_revision[3]);
1364 }
1365
1366 static ssize_t
1367 qla2x00_optrom_gold_fw_version_show(struct device *dev,
1368     struct device_attribute *attr, char *buf)
1369 {
1370         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1371         struct qla_hw_data *ha = vha->hw;
1372
1373         if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
1374             !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1375                 return scnprintf(buf, PAGE_SIZE, "\n");
1376
1377         return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%d)\n",
1378             ha->gold_fw_version[0], ha->gold_fw_version[1],
1379             ha->gold_fw_version[2], ha->gold_fw_version[3]);
1380 }
1381
1382 static ssize_t
1383 qla2x00_total_isp_aborts_show(struct device *dev,
1384                               struct device_attribute *attr, char *buf)
1385 {
1386         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1387
1388         return scnprintf(buf, PAGE_SIZE, "%d\n",
1389             vha->qla_stats.total_isp_aborts);
1390 }
1391
1392 static ssize_t
1393 qla24xx_84xx_fw_version_show(struct device *dev,
1394         struct device_attribute *attr, char *buf)
1395 {
1396         int rval = QLA_SUCCESS;
1397         uint16_t status[2] = { 0 };
1398         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1399         struct qla_hw_data *ha = vha->hw;
1400
1401         if (!IS_QLA84XX(ha))
1402                 return scnprintf(buf, PAGE_SIZE, "\n");
1403
1404         if (!ha->cs84xx->op_fw_version) {
1405                 rval = qla84xx_verify_chip(vha, status);
1406
1407                 if (!rval && !status[0])
1408                         return scnprintf(buf, PAGE_SIZE, "%u\n",
1409                             (uint32_t)ha->cs84xx->op_fw_version);
1410         }
1411
1412         return scnprintf(buf, PAGE_SIZE, "\n");
1413 }
1414
1415 static ssize_t
1416 qla2x00_serdes_version_show(struct device *dev, struct device_attribute *attr,
1417     char *buf)
1418 {
1419         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1420         struct qla_hw_data *ha = vha->hw;
1421
1422         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1423                 return scnprintf(buf, PAGE_SIZE, "\n");
1424
1425         return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1426             ha->serdes_version[0], ha->serdes_version[1],
1427             ha->serdes_version[2]);
1428 }
1429
1430 static ssize_t
1431 qla2x00_mpi_version_show(struct device *dev, struct device_attribute *attr,
1432     char *buf)
1433 {
1434         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1435         struct qla_hw_data *ha = vha->hw;
1436
1437         if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha) &&
1438             !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1439                 return scnprintf(buf, PAGE_SIZE, "\n");
1440
1441         return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
1442             ha->mpi_version[0], ha->mpi_version[1], ha->mpi_version[2],
1443             ha->mpi_capabilities);
1444 }
1445
1446 static ssize_t
1447 qla2x00_phy_version_show(struct device *dev, struct device_attribute *attr,
1448     char *buf)
1449 {
1450         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1451         struct qla_hw_data *ha = vha->hw;
1452
1453         if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
1454                 return scnprintf(buf, PAGE_SIZE, "\n");
1455
1456         return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1457             ha->phy_version[0], ha->phy_version[1], ha->phy_version[2]);
1458 }
1459
1460 static ssize_t
1461 qla2x00_flash_block_size_show(struct device *dev,
1462                               struct device_attribute *attr, char *buf)
1463 {
1464         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1465         struct qla_hw_data *ha = vha->hw;
1466
1467         return scnprintf(buf, PAGE_SIZE, "0x%x\n", ha->fdt_block_size);
1468 }
1469
1470 static ssize_t
1471 qla2x00_vlan_id_show(struct device *dev, struct device_attribute *attr,
1472     char *buf)
1473 {
1474         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1475
1476         if (!IS_CNA_CAPABLE(vha->hw))
1477                 return scnprintf(buf, PAGE_SIZE, "\n");
1478
1479         return scnprintf(buf, PAGE_SIZE, "%d\n", vha->fcoe_vlan_id);
1480 }
1481
1482 static ssize_t
1483 qla2x00_vn_port_mac_address_show(struct device *dev,
1484     struct device_attribute *attr, char *buf)
1485 {
1486         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1487
1488         if (!IS_CNA_CAPABLE(vha->hw))
1489                 return scnprintf(buf, PAGE_SIZE, "\n");
1490
1491         return scnprintf(buf, PAGE_SIZE, "%pMR\n", vha->fcoe_vn_port_mac);
1492 }
1493
1494 static ssize_t
1495 qla2x00_fabric_param_show(struct device *dev, struct device_attribute *attr,
1496     char *buf)
1497 {
1498         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1499
1500         return scnprintf(buf, PAGE_SIZE, "%d\n", vha->hw->switch_cap);
1501 }
1502
1503 static ssize_t
1504 qla2x00_thermal_temp_show(struct device *dev,
1505         struct device_attribute *attr, char *buf)
1506 {
1507         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1508         uint16_t temp = 0;
1509         int rc;
1510
1511         mutex_lock(&vha->hw->optrom_mutex);
1512         if (qla2x00_chip_is_down(vha)) {
1513                 mutex_unlock(&vha->hw->optrom_mutex);
1514                 ql_log(ql_log_warn, vha, 0x70dc, "ISP reset active.\n");
1515                 goto done;
1516         }
1517
1518         if (vha->hw->flags.eeh_busy) {
1519                 mutex_unlock(&vha->hw->optrom_mutex);
1520                 ql_log(ql_log_warn, vha, 0x70dd, "PCI EEH busy.\n");
1521                 goto done;
1522         }
1523
1524         rc = qla2x00_get_thermal_temp(vha, &temp);
1525         mutex_unlock(&vha->hw->optrom_mutex);
1526         if (rc == QLA_SUCCESS)
1527                 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
1528
1529 done:
1530         return scnprintf(buf, PAGE_SIZE, "\n");
1531 }
1532
1533 static ssize_t
1534 qla2x00_fw_state_show(struct device *dev, struct device_attribute *attr,
1535     char *buf)
1536 {
1537         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1538         int rval = QLA_FUNCTION_FAILED;
1539         uint16_t state[6];
1540         uint32_t pstate;
1541
1542         if (IS_QLAFX00(vha->hw)) {
1543                 pstate = qlafx00_fw_state_show(dev, attr, buf);
1544                 return scnprintf(buf, PAGE_SIZE, "0x%x\n", pstate);
1545         }
1546
1547         mutex_lock(&vha->hw->optrom_mutex);
1548         if (qla2x00_chip_is_down(vha)) {
1549                 mutex_unlock(&vha->hw->optrom_mutex);
1550                 ql_log(ql_log_warn, vha, 0x707c,
1551                     "ISP reset active.\n");
1552                 goto out;
1553         } else if (vha->hw->flags.eeh_busy) {
1554                 mutex_unlock(&vha->hw->optrom_mutex);
1555                 goto out;
1556         }
1557
1558         rval = qla2x00_get_firmware_state(vha, state);
1559         mutex_unlock(&vha->hw->optrom_mutex);
1560 out:
1561         if (rval != QLA_SUCCESS) {
1562                 memset(state, -1, sizeof(state));
1563                 rval = qla2x00_get_firmware_state(vha, state);
1564         }
1565
1566         return scnprintf(buf, PAGE_SIZE, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1567             state[0], state[1], state[2], state[3], state[4], state[5]);
1568 }
1569
1570 static ssize_t
1571 qla2x00_diag_requests_show(struct device *dev,
1572         struct device_attribute *attr, char *buf)
1573 {
1574         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1575
1576         if (!IS_BIDI_CAPABLE(vha->hw))
1577                 return scnprintf(buf, PAGE_SIZE, "\n");
1578
1579         return scnprintf(buf, PAGE_SIZE, "%llu\n", vha->bidi_stats.io_count);
1580 }
1581
1582 static ssize_t
1583 qla2x00_diag_megabytes_show(struct device *dev,
1584         struct device_attribute *attr, char *buf)
1585 {
1586         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1587
1588         if (!IS_BIDI_CAPABLE(vha->hw))
1589                 return scnprintf(buf, PAGE_SIZE, "\n");
1590
1591         return scnprintf(buf, PAGE_SIZE, "%llu\n",
1592             vha->bidi_stats.transfer_bytes >> 20);
1593 }
1594
1595 static ssize_t
1596 qla2x00_fw_dump_size_show(struct device *dev, struct device_attribute *attr,
1597         char *buf)
1598 {
1599         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1600         struct qla_hw_data *ha = vha->hw;
1601         uint32_t size;
1602
1603         if (!ha->fw_dumped)
1604                 size = 0;
1605         else if (IS_P3P_TYPE(ha))
1606                 size = ha->md_template_size + ha->md_dump_size;
1607         else
1608                 size = ha->fw_dump_len;
1609
1610         return scnprintf(buf, PAGE_SIZE, "%d\n", size);
1611 }
1612
1613 static ssize_t
1614 qla2x00_allow_cna_fw_dump_show(struct device *dev,
1615         struct device_attribute *attr, char *buf)
1616 {
1617         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1618
1619         if (!IS_P3P_TYPE(vha->hw))
1620                 return scnprintf(buf, PAGE_SIZE, "\n");
1621         else
1622                 return scnprintf(buf, PAGE_SIZE, "%s\n",
1623                     vha->hw->allow_cna_fw_dump ? "true" : "false");
1624 }
1625
1626 static ssize_t
1627 qla2x00_allow_cna_fw_dump_store(struct device *dev,
1628         struct device_attribute *attr, const char *buf, size_t count)
1629 {
1630         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1631         int val = 0;
1632
1633         if (!IS_P3P_TYPE(vha->hw))
1634                 return -EINVAL;
1635
1636         if (sscanf(buf, "%d", &val) != 1)
1637                 return -EINVAL;
1638
1639         vha->hw->allow_cna_fw_dump = val != 0;
1640
1641         return strlen(buf);
1642 }
1643
1644 static ssize_t
1645 qla2x00_pep_version_show(struct device *dev, struct device_attribute *attr,
1646         char *buf)
1647 {
1648         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1649         struct qla_hw_data *ha = vha->hw;
1650
1651         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1652                 return scnprintf(buf, PAGE_SIZE, "\n");
1653
1654         return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1655             ha->pep_version[0], ha->pep_version[1], ha->pep_version[2]);
1656 }
1657
1658 static ssize_t
1659 qla2x00_min_supported_speed_show(struct device *dev,
1660     struct device_attribute *attr, char *buf)
1661 {
1662         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1663         struct qla_hw_data *ha = vha->hw;
1664
1665         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1666                 return scnprintf(buf, PAGE_SIZE, "\n");
1667
1668         return scnprintf(buf, PAGE_SIZE, "%s\n",
1669             ha->min_supported_speed == 6 ? "64Gps" :
1670             ha->min_supported_speed == 5 ? "32Gps" :
1671             ha->min_supported_speed == 4 ? "16Gps" :
1672             ha->min_supported_speed == 3 ? "8Gps" :
1673             ha->min_supported_speed == 2 ? "4Gps" :
1674             ha->min_supported_speed != 0 ? "unknown" : "");
1675 }
1676
1677 static ssize_t
1678 qla2x00_max_supported_speed_show(struct device *dev,
1679     struct device_attribute *attr, char *buf)
1680 {
1681         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1682         struct qla_hw_data *ha = vha->hw;
1683
1684         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1685                 return scnprintf(buf, PAGE_SIZE, "\n");
1686
1687         return scnprintf(buf, PAGE_SIZE, "%s\n",
1688             ha->max_supported_speed  == 2 ? "64Gps" :
1689             ha->max_supported_speed  == 1 ? "32Gps" :
1690             ha->max_supported_speed  == 0 ? "16Gps" : "unknown");
1691 }
1692
1693 static ssize_t
1694 qla2x00_port_speed_store(struct device *dev, struct device_attribute *attr,
1695     const char *buf, size_t count)
1696 {
1697         struct scsi_qla_host *vha = shost_priv(dev_to_shost(dev));
1698         ulong type, speed;
1699         int oldspeed, rval;
1700         int mode = QLA_SET_DATA_RATE_LR;
1701         struct qla_hw_data *ha = vha->hw;
1702
1703         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha)) {
1704                 ql_log(ql_log_warn, vha, 0x70d8,
1705                     "Speed setting not supported \n");
1706                 return -EINVAL;
1707         }
1708
1709         rval = kstrtol(buf, 10, &type);
1710         if (rval)
1711                 return rval;
1712         speed = type;
1713         if (type == 40 || type == 80 || type == 160 ||
1714             type == 320) {
1715                 ql_dbg(ql_dbg_user, vha, 0x70d9,
1716                     "Setting will be affected after a loss of sync\n");
1717                 type = type/10;
1718                 mode = QLA_SET_DATA_RATE_NOLR;
1719         }
1720
1721         oldspeed = ha->set_data_rate;
1722
1723         switch (type) {
1724         case 0:
1725                 ha->set_data_rate = PORT_SPEED_AUTO;
1726                 break;
1727         case 4:
1728                 ha->set_data_rate = PORT_SPEED_4GB;
1729                 break;
1730         case 8:
1731                 ha->set_data_rate = PORT_SPEED_8GB;
1732                 break;
1733         case 16:
1734                 ha->set_data_rate = PORT_SPEED_16GB;
1735                 break;
1736         case 32:
1737                 ha->set_data_rate = PORT_SPEED_32GB;
1738                 break;
1739         default:
1740                 ql_log(ql_log_warn, vha, 0x1199,
1741                     "Unrecognized speed setting:%lx. Setting Autoneg\n",
1742                     speed);
1743                 ha->set_data_rate = PORT_SPEED_AUTO;
1744         }
1745
1746         if (qla2x00_chip_is_down(vha) || (oldspeed == ha->set_data_rate))
1747                 return -EINVAL;
1748
1749         ql_log(ql_log_info, vha, 0x70da,
1750             "Setting speed to %lx Gbps \n", type);
1751
1752         rval = qla2x00_set_data_rate(vha, mode);
1753         if (rval != QLA_SUCCESS)
1754                 return -EIO;
1755
1756         return strlen(buf);
1757 }
1758
1759 static const struct {
1760         u16 rate;
1761         char *str;
1762 } port_speed_str[] = {
1763         { PORT_SPEED_4GB, "4" },
1764         { PORT_SPEED_8GB, "8" },
1765         { PORT_SPEED_16GB, "16" },
1766         { PORT_SPEED_32GB, "32" },
1767         { PORT_SPEED_64GB, "64" },
1768         { PORT_SPEED_10GB, "10" },
1769 };
1770
1771 static ssize_t
1772 qla2x00_port_speed_show(struct device *dev, struct device_attribute *attr,
1773     char *buf)
1774 {
1775         struct scsi_qla_host *vha = shost_priv(dev_to_shost(dev));
1776         struct qla_hw_data *ha = vha->hw;
1777         ssize_t rval;
1778         u16 i;
1779         char *speed = "Unknown";
1780
1781         rval = qla2x00_get_data_rate(vha);
1782         if (rval != QLA_SUCCESS) {
1783                 ql_log(ql_log_warn, vha, 0x70db,
1784                     "Unable to get port speed rval:%zd\n", rval);
1785                 return -EINVAL;
1786         }
1787
1788         for (i = 0; i < ARRAY_SIZE(port_speed_str); i++) {
1789                 if (port_speed_str[i].rate != ha->link_data_rate)
1790                         continue;
1791                 speed = port_speed_str[i].str;
1792                 break;
1793         }
1794
1795         return scnprintf(buf, PAGE_SIZE, "%s\n", speed);
1796 }
1797
1798 /* ----- */
1799
1800 static ssize_t
1801 qlini_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1802 {
1803         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1804         int len = 0;
1805
1806         len += scnprintf(buf + len, PAGE_SIZE-len,
1807             "Supported options: enabled | disabled | dual | exclusive\n");
1808
1809         /* --- */
1810         len += scnprintf(buf + len, PAGE_SIZE-len, "Current selection: ");
1811
1812         switch (vha->qlini_mode) {
1813         case QLA2XXX_INI_MODE_EXCLUSIVE:
1814                 len += scnprintf(buf + len, PAGE_SIZE-len,
1815                     QLA2XXX_INI_MODE_STR_EXCLUSIVE);
1816                 break;
1817         case QLA2XXX_INI_MODE_DISABLED:
1818                 len += scnprintf(buf + len, PAGE_SIZE-len,
1819                     QLA2XXX_INI_MODE_STR_DISABLED);
1820                 break;
1821         case QLA2XXX_INI_MODE_ENABLED:
1822                 len += scnprintf(buf + len, PAGE_SIZE-len,
1823                     QLA2XXX_INI_MODE_STR_ENABLED);
1824                 break;
1825         case QLA2XXX_INI_MODE_DUAL:
1826                 len += scnprintf(buf + len, PAGE_SIZE-len,
1827                     QLA2XXX_INI_MODE_STR_DUAL);
1828                 break;
1829         }
1830         len += scnprintf(buf + len, PAGE_SIZE-len, "\n");
1831
1832         return len;
1833 }
1834
1835 static char *mode_to_str[] = {
1836         "exclusive",
1837         "disabled",
1838         "enabled",
1839         "dual",
1840 };
1841
1842 #define NEED_EXCH_OFFLOAD(_exchg) ((_exchg) > FW_DEF_EXCHANGES_CNT)
1843 static int qla_set_ini_mode(scsi_qla_host_t *vha, int op)
1844 {
1845         int rc = 0;
1846         enum {
1847                 NO_ACTION,
1848                 MODE_CHANGE_ACCEPT,
1849                 MODE_CHANGE_NO_ACTION,
1850                 TARGET_STILL_ACTIVE,
1851         };
1852         int action = NO_ACTION;
1853         int set_mode = 0;
1854         u8  eo_toggle = 0;      /* exchange offload flipped */
1855
1856         switch (vha->qlini_mode) {
1857         case QLA2XXX_INI_MODE_DISABLED:
1858                 switch (op) {
1859                 case QLA2XXX_INI_MODE_DISABLED:
1860                         if (qla_tgt_mode_enabled(vha)) {
1861                                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
1862                                     vha->hw->flags.exchoffld_enabled)
1863                                         eo_toggle = 1;
1864                                 if (((vha->ql2xexchoffld !=
1865                                     vha->u_ql2xexchoffld) &&
1866                                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
1867                                     eo_toggle) {
1868                                         /*
1869                                          * The number of exchange to be offload
1870                                          * was tweaked or offload option was
1871                                          * flipped
1872                                          */
1873                                         action = MODE_CHANGE_ACCEPT;
1874                                 } else {
1875                                         action = MODE_CHANGE_NO_ACTION;
1876                                 }
1877                         } else {
1878                                 action = MODE_CHANGE_NO_ACTION;
1879                         }
1880                         break;
1881                 case QLA2XXX_INI_MODE_EXCLUSIVE:
1882                         if (qla_tgt_mode_enabled(vha)) {
1883                                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
1884                                     vha->hw->flags.exchoffld_enabled)
1885                                         eo_toggle = 1;
1886                                 if (((vha->ql2xexchoffld !=
1887                                     vha->u_ql2xexchoffld) &&
1888                                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
1889                                     eo_toggle) {
1890                                         /*
1891                                          * The number of exchange to be offload
1892                                          * was tweaked or offload option was
1893                                          * flipped
1894                                          */
1895                                         action = MODE_CHANGE_ACCEPT;
1896                                 } else {
1897                                         action = MODE_CHANGE_NO_ACTION;
1898                                 }
1899                         } else {
1900                                 action = MODE_CHANGE_ACCEPT;
1901                         }
1902                         break;
1903                 case QLA2XXX_INI_MODE_DUAL:
1904                         action = MODE_CHANGE_ACCEPT;
1905                         /* active_mode is target only, reset it to dual */
1906                         if (qla_tgt_mode_enabled(vha)) {
1907                                 set_mode = 1;
1908                                 action = MODE_CHANGE_ACCEPT;
1909                         } else {
1910                                 action = MODE_CHANGE_NO_ACTION;
1911                         }
1912                         break;
1913
1914                 case QLA2XXX_INI_MODE_ENABLED:
1915                         if (qla_tgt_mode_enabled(vha))
1916                                 action = TARGET_STILL_ACTIVE;
1917                         else {
1918                                 action = MODE_CHANGE_ACCEPT;
1919                                 set_mode = 1;
1920                         }
1921                         break;
1922                 }
1923                 break;
1924
1925         case QLA2XXX_INI_MODE_EXCLUSIVE:
1926                 switch (op) {
1927                 case QLA2XXX_INI_MODE_EXCLUSIVE:
1928                         if (qla_tgt_mode_enabled(vha)) {
1929                                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
1930                                     vha->hw->flags.exchoffld_enabled)
1931                                         eo_toggle = 1;
1932                                 if (((vha->ql2xexchoffld !=
1933                                     vha->u_ql2xexchoffld) &&
1934                                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
1935                                     eo_toggle)
1936                                         /*
1937                                          * The number of exchange to be offload
1938                                          * was tweaked or offload option was
1939                                          * flipped
1940                                          */
1941                                         action = MODE_CHANGE_ACCEPT;
1942                                 else
1943                                         action = NO_ACTION;
1944                         } else
1945                                 action = NO_ACTION;
1946
1947                         break;
1948
1949                 case QLA2XXX_INI_MODE_DISABLED:
1950                         if (qla_tgt_mode_enabled(vha)) {
1951                                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
1952                                     vha->hw->flags.exchoffld_enabled)
1953                                         eo_toggle = 1;
1954                                 if (((vha->ql2xexchoffld !=
1955                                       vha->u_ql2xexchoffld) &&
1956                                     NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
1957                                     eo_toggle)
1958                                         action = MODE_CHANGE_ACCEPT;
1959                                 else
1960                                         action = MODE_CHANGE_NO_ACTION;
1961                         } else
1962                                 action = MODE_CHANGE_NO_ACTION;
1963                         break;
1964
1965                 case QLA2XXX_INI_MODE_DUAL: /* exclusive -> dual */
1966                         if (qla_tgt_mode_enabled(vha)) {
1967                                 action = MODE_CHANGE_ACCEPT;
1968                                 set_mode = 1;
1969                         } else
1970                                 action = MODE_CHANGE_ACCEPT;
1971                         break;
1972
1973                 case QLA2XXX_INI_MODE_ENABLED:
1974                         if (qla_tgt_mode_enabled(vha))
1975                                 action = TARGET_STILL_ACTIVE;
1976                         else {
1977                                 if (vha->hw->flags.fw_started)
1978                                         action = MODE_CHANGE_NO_ACTION;
1979                                 else
1980                                         action = MODE_CHANGE_ACCEPT;
1981                         }
1982                         break;
1983                 }
1984                 break;
1985
1986         case QLA2XXX_INI_MODE_ENABLED:
1987                 switch (op) {
1988                 case QLA2XXX_INI_MODE_ENABLED:
1989                         if (NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg) !=
1990                             vha->hw->flags.exchoffld_enabled)
1991                                 eo_toggle = 1;
1992                         if (((vha->ql2xiniexchg != vha->u_ql2xiniexchg) &&
1993                                 NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg)) ||
1994                             eo_toggle)
1995                                 action = MODE_CHANGE_ACCEPT;
1996                         else
1997                                 action = NO_ACTION;
1998                         break;
1999                 case QLA2XXX_INI_MODE_DUAL:
2000                 case QLA2XXX_INI_MODE_DISABLED:
2001                         action = MODE_CHANGE_ACCEPT;
2002                         break;
2003                 default:
2004                         action = MODE_CHANGE_NO_ACTION;
2005                         break;
2006                 }
2007                 break;
2008
2009         case QLA2XXX_INI_MODE_DUAL:
2010                 switch (op) {
2011                 case QLA2XXX_INI_MODE_DUAL:
2012                         if (qla_tgt_mode_enabled(vha) ||
2013                             qla_dual_mode_enabled(vha)) {
2014                                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld +
2015                                         vha->u_ql2xiniexchg) !=
2016                                     vha->hw->flags.exchoffld_enabled)
2017                                         eo_toggle = 1;
2018
2019                                 if ((((vha->ql2xexchoffld +
2020                                        vha->ql2xiniexchg) !=
2021                                     (vha->u_ql2xiniexchg +
2022                                      vha->u_ql2xexchoffld)) &&
2023                                     NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg +
2024                                         vha->u_ql2xexchoffld)) || eo_toggle)
2025                                         action = MODE_CHANGE_ACCEPT;
2026                                 else
2027                                         action = NO_ACTION;
2028                         } else {
2029                                 if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld +
2030                                         vha->u_ql2xiniexchg) !=
2031                                     vha->hw->flags.exchoffld_enabled)
2032                                         eo_toggle = 1;
2033
2034                                 if ((((vha->ql2xexchoffld + vha->ql2xiniexchg)
2035                                     != (vha->u_ql2xiniexchg +
2036                                         vha->u_ql2xexchoffld)) &&
2037                                     NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg +
2038                                         vha->u_ql2xexchoffld)) || eo_toggle)
2039                                         action = MODE_CHANGE_NO_ACTION;
2040                                 else
2041                                         action = NO_ACTION;
2042                         }
2043                         break;
2044
2045                 case QLA2XXX_INI_MODE_DISABLED:
2046                         if (qla_tgt_mode_enabled(vha) ||
2047                             qla_dual_mode_enabled(vha)) {
2048                                 /* turning off initiator mode */
2049                                 set_mode = 1;
2050                                 action = MODE_CHANGE_ACCEPT;
2051                         } else {
2052                                 action = MODE_CHANGE_NO_ACTION;
2053                         }
2054                         break;
2055
2056                 case QLA2XXX_INI_MODE_EXCLUSIVE:
2057                         if (qla_tgt_mode_enabled(vha) ||
2058                             qla_dual_mode_enabled(vha)) {
2059                                 set_mode = 1;
2060                                 action = MODE_CHANGE_ACCEPT;
2061                         } else {
2062                                 action = MODE_CHANGE_ACCEPT;
2063                         }
2064                         break;
2065
2066                 case QLA2XXX_INI_MODE_ENABLED:
2067                         if (qla_tgt_mode_enabled(vha) ||
2068                             qla_dual_mode_enabled(vha)) {
2069                                 action = TARGET_STILL_ACTIVE;
2070                         } else {
2071                                 action = MODE_CHANGE_ACCEPT;
2072                         }
2073                 }
2074                 break;
2075         }
2076
2077         switch (action) {
2078         case MODE_CHANGE_ACCEPT:
2079                 ql_log(ql_log_warn, vha, 0xffff,
2080                     "Mode change accepted. From %s to %s, Tgt exchg %d|%d. ini exchg %d|%d\n",
2081                     mode_to_str[vha->qlini_mode], mode_to_str[op],
2082                     vha->ql2xexchoffld, vha->u_ql2xexchoffld,
2083                     vha->ql2xiniexchg, vha->u_ql2xiniexchg);
2084
2085                 vha->qlini_mode = op;
2086                 vha->ql2xexchoffld = vha->u_ql2xexchoffld;
2087                 vha->ql2xiniexchg = vha->u_ql2xiniexchg;
2088                 if (set_mode)
2089                         qlt_set_mode(vha);
2090                 vha->flags.online = 1;
2091                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2092                 break;
2093
2094         case MODE_CHANGE_NO_ACTION:
2095                 ql_log(ql_log_warn, vha, 0xffff,
2096                     "Mode is set. No action taken. From %s to %s, Tgt exchg %d|%d. ini exchg %d|%d\n",
2097                     mode_to_str[vha->qlini_mode], mode_to_str[op],
2098                     vha->ql2xexchoffld, vha->u_ql2xexchoffld,
2099                     vha->ql2xiniexchg, vha->u_ql2xiniexchg);
2100                 vha->qlini_mode = op;
2101                 vha->ql2xexchoffld = vha->u_ql2xexchoffld;
2102                 vha->ql2xiniexchg = vha->u_ql2xiniexchg;
2103                 break;
2104
2105         case TARGET_STILL_ACTIVE:
2106                 ql_log(ql_log_warn, vha, 0xffff,
2107                     "Target Mode is active. Unable to change Mode.\n");
2108                 break;
2109
2110         case NO_ACTION:
2111         default:
2112                 ql_log(ql_log_warn, vha, 0xffff,
2113                     "Mode unchange. No action taken. %d|%d pct %d|%d.\n",
2114                     vha->qlini_mode, op,
2115                     vha->ql2xexchoffld, vha->u_ql2xexchoffld);
2116                 break;
2117         }
2118
2119         return rc;
2120 }
2121
2122 static ssize_t
2123 qlini_mode_store(struct device *dev, struct device_attribute *attr,
2124     const char *buf, size_t count)
2125 {
2126         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2127         int ini;
2128
2129         if (!buf)
2130                 return -EINVAL;
2131
2132         if (strncasecmp(QLA2XXX_INI_MODE_STR_EXCLUSIVE, buf,
2133                 strlen(QLA2XXX_INI_MODE_STR_EXCLUSIVE)) == 0)
2134                 ini = QLA2XXX_INI_MODE_EXCLUSIVE;
2135         else if (strncasecmp(QLA2XXX_INI_MODE_STR_DISABLED, buf,
2136                 strlen(QLA2XXX_INI_MODE_STR_DISABLED)) == 0)
2137                 ini = QLA2XXX_INI_MODE_DISABLED;
2138         else if (strncasecmp(QLA2XXX_INI_MODE_STR_ENABLED, buf,
2139                   strlen(QLA2XXX_INI_MODE_STR_ENABLED)) == 0)
2140                 ini = QLA2XXX_INI_MODE_ENABLED;
2141         else if (strncasecmp(QLA2XXX_INI_MODE_STR_DUAL, buf,
2142                 strlen(QLA2XXX_INI_MODE_STR_DUAL)) == 0)
2143                 ini = QLA2XXX_INI_MODE_DUAL;
2144         else
2145                 return -EINVAL;
2146
2147         qla_set_ini_mode(vha, ini);
2148         return strlen(buf);
2149 }
2150
2151 static ssize_t
2152 ql2xexchoffld_show(struct device *dev, struct device_attribute *attr,
2153     char *buf)
2154 {
2155         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2156         int len = 0;
2157
2158         len += scnprintf(buf + len, PAGE_SIZE-len,
2159                 "target exchange: new %d : current: %d\n\n",
2160                 vha->u_ql2xexchoffld, vha->ql2xexchoffld);
2161
2162         len += scnprintf(buf + len, PAGE_SIZE-len,
2163             "Please (re)set operating mode via \"/sys/class/scsi_host/host%ld/qlini_mode\" to load new setting.\n",
2164             vha->host_no);
2165
2166         return len;
2167 }
2168
2169 static ssize_t
2170 ql2xexchoffld_store(struct device *dev, struct device_attribute *attr,
2171     const char *buf, size_t count)
2172 {
2173         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2174         int val = 0;
2175
2176         if (sscanf(buf, "%d", &val) != 1)
2177                 return -EINVAL;
2178
2179         if (val > FW_MAX_EXCHANGES_CNT)
2180                 val = FW_MAX_EXCHANGES_CNT;
2181         else if (val < 0)
2182                 val = 0;
2183
2184         vha->u_ql2xexchoffld = val;
2185         return strlen(buf);
2186 }
2187
2188 static ssize_t
2189 ql2xiniexchg_show(struct device *dev, struct device_attribute *attr,
2190     char *buf)
2191 {
2192         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2193         int len = 0;
2194
2195         len += scnprintf(buf + len, PAGE_SIZE-len,
2196                 "target exchange: new %d : current: %d\n\n",
2197                 vha->u_ql2xiniexchg, vha->ql2xiniexchg);
2198
2199         len += scnprintf(buf + len, PAGE_SIZE-len,
2200             "Please (re)set operating mode via \"/sys/class/scsi_host/host%ld/qlini_mode\" to load new setting.\n",
2201             vha->host_no);
2202
2203         return len;
2204 }
2205
2206 static ssize_t
2207 ql2xiniexchg_store(struct device *dev, struct device_attribute *attr,
2208     const char *buf, size_t count)
2209 {
2210         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2211         int val = 0;
2212
2213         if (sscanf(buf, "%d", &val) != 1)
2214                 return -EINVAL;
2215
2216         if (val > FW_MAX_EXCHANGES_CNT)
2217                 val = FW_MAX_EXCHANGES_CNT;
2218         else if (val < 0)
2219                 val = 0;
2220
2221         vha->u_ql2xiniexchg = val;
2222         return strlen(buf);
2223 }
2224
2225 static ssize_t
2226 qla2x00_dif_bundle_statistics_show(struct device *dev,
2227     struct device_attribute *attr, char *buf)
2228 {
2229         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2230         struct qla_hw_data *ha = vha->hw;
2231
2232         return scnprintf(buf, PAGE_SIZE,
2233             "cross=%llu read=%llu write=%llu kalloc=%llu dma_alloc=%llu unusable=%u\n",
2234             ha->dif_bundle_crossed_pages, ha->dif_bundle_reads,
2235             ha->dif_bundle_writes, ha->dif_bundle_kallocs,
2236             ha->dif_bundle_dma_allocs, ha->pool.unusable.count);
2237 }
2238
2239 static ssize_t
2240 qla2x00_fw_attr_show(struct device *dev,
2241     struct device_attribute *attr, char *buf)
2242 {
2243         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2244         struct qla_hw_data *ha = vha->hw;
2245
2246         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
2247                 return scnprintf(buf, PAGE_SIZE, "\n");
2248
2249         return scnprintf(buf, PAGE_SIZE, "%llx\n",
2250             (uint64_t)ha->fw_attributes_ext[1] << 48 |
2251             (uint64_t)ha->fw_attributes_ext[0] << 32 |
2252             (uint64_t)ha->fw_attributes_h << 16 |
2253             (uint64_t)ha->fw_attributes);
2254 }
2255
2256 static ssize_t
2257 qla2x00_port_no_show(struct device *dev, struct device_attribute *attr,
2258     char *buf)
2259 {
2260         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2261
2262         return scnprintf(buf, PAGE_SIZE, "%u\n", vha->hw->port_no);
2263 }
2264
2265 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_driver_version_show, NULL);
2266 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
2267 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
2268 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
2269 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
2270 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
2271 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
2272 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
2273 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
2274 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
2275 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
2276                    qla2x00_zio_timer_store);
2277 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
2278                    qla2x00_beacon_store);
2279 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
2280                    qla2x00_optrom_bios_version_show, NULL);
2281 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
2282                    qla2x00_optrom_efi_version_show, NULL);
2283 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
2284                    qla2x00_optrom_fcode_version_show, NULL);
2285 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
2286                    NULL);
2287 static DEVICE_ATTR(optrom_gold_fw_version, S_IRUGO,
2288     qla2x00_optrom_gold_fw_version_show, NULL);
2289 static DEVICE_ATTR(84xx_fw_version, S_IRUGO, qla24xx_84xx_fw_version_show,
2290                    NULL);
2291 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
2292                    NULL);
2293 static DEVICE_ATTR(serdes_version, 0444, qla2x00_serdes_version_show, NULL);
2294 static DEVICE_ATTR(mpi_version, S_IRUGO, qla2x00_mpi_version_show, NULL);
2295 static DEVICE_ATTR(phy_version, S_IRUGO, qla2x00_phy_version_show, NULL);
2296 static DEVICE_ATTR(flash_block_size, S_IRUGO, qla2x00_flash_block_size_show,
2297                    NULL);
2298 static DEVICE_ATTR(vlan_id, S_IRUGO, qla2x00_vlan_id_show, NULL);
2299 static DEVICE_ATTR(vn_port_mac_address, S_IRUGO,
2300                    qla2x00_vn_port_mac_address_show, NULL);
2301 static DEVICE_ATTR(fabric_param, S_IRUGO, qla2x00_fabric_param_show, NULL);
2302 static DEVICE_ATTR(fw_state, S_IRUGO, qla2x00_fw_state_show, NULL);
2303 static DEVICE_ATTR(thermal_temp, S_IRUGO, qla2x00_thermal_temp_show, NULL);
2304 static DEVICE_ATTR(diag_requests, S_IRUGO, qla2x00_diag_requests_show, NULL);
2305 static DEVICE_ATTR(diag_megabytes, S_IRUGO, qla2x00_diag_megabytes_show, NULL);
2306 static DEVICE_ATTR(fw_dump_size, S_IRUGO, qla2x00_fw_dump_size_show, NULL);
2307 static DEVICE_ATTR(allow_cna_fw_dump, S_IRUGO | S_IWUSR,
2308                    qla2x00_allow_cna_fw_dump_show,
2309                    qla2x00_allow_cna_fw_dump_store);
2310 static DEVICE_ATTR(pep_version, S_IRUGO, qla2x00_pep_version_show, NULL);
2311 static DEVICE_ATTR(min_supported_speed, 0444,
2312                    qla2x00_min_supported_speed_show, NULL);
2313 static DEVICE_ATTR(max_supported_speed, 0444,
2314                    qla2x00_max_supported_speed_show, NULL);
2315 static DEVICE_ATTR(zio_threshold, 0644,
2316     qla_zio_threshold_show,
2317     qla_zio_threshold_store);
2318 static DEVICE_ATTR_RW(qlini_mode);
2319 static DEVICE_ATTR_RW(ql2xexchoffld);
2320 static DEVICE_ATTR_RW(ql2xiniexchg);
2321 static DEVICE_ATTR(dif_bundle_statistics, 0444,
2322     qla2x00_dif_bundle_statistics_show, NULL);
2323 static DEVICE_ATTR(port_speed, 0644, qla2x00_port_speed_show,
2324     qla2x00_port_speed_store);
2325 static DEVICE_ATTR(port_no, 0444, qla2x00_port_no_show, NULL);
2326 static DEVICE_ATTR(fw_attr, 0444, qla2x00_fw_attr_show, NULL);
2327
2328
2329 struct device_attribute *qla2x00_host_attrs[] = {
2330         &dev_attr_driver_version,
2331         &dev_attr_fw_version,
2332         &dev_attr_serial_num,
2333         &dev_attr_isp_name,
2334         &dev_attr_isp_id,
2335         &dev_attr_model_name,
2336         &dev_attr_model_desc,
2337         &dev_attr_pci_info,
2338         &dev_attr_link_state,
2339         &dev_attr_zio,
2340         &dev_attr_zio_timer,
2341         &dev_attr_beacon,
2342         &dev_attr_optrom_bios_version,
2343         &dev_attr_optrom_efi_version,
2344         &dev_attr_optrom_fcode_version,
2345         &dev_attr_optrom_fw_version,
2346         &dev_attr_84xx_fw_version,
2347         &dev_attr_total_isp_aborts,
2348         &dev_attr_serdes_version,
2349         &dev_attr_mpi_version,
2350         &dev_attr_phy_version,
2351         &dev_attr_flash_block_size,
2352         &dev_attr_vlan_id,
2353         &dev_attr_vn_port_mac_address,
2354         &dev_attr_fabric_param,
2355         &dev_attr_fw_state,
2356         &dev_attr_optrom_gold_fw_version,
2357         &dev_attr_thermal_temp,
2358         &dev_attr_diag_requests,
2359         &dev_attr_diag_megabytes,
2360         &dev_attr_fw_dump_size,
2361         &dev_attr_allow_cna_fw_dump,
2362         &dev_attr_pep_version,
2363         &dev_attr_min_supported_speed,
2364         &dev_attr_max_supported_speed,
2365         &dev_attr_zio_threshold,
2366         &dev_attr_dif_bundle_statistics,
2367         &dev_attr_port_speed,
2368         &dev_attr_port_no,
2369         &dev_attr_fw_attr,
2370         NULL, /* reserve for qlini_mode */
2371         NULL, /* reserve for ql2xiniexchg */
2372         NULL, /* reserve for ql2xexchoffld */
2373         NULL,
2374 };
2375
2376 void qla_insert_tgt_attrs(void)
2377 {
2378         struct device_attribute **attr;
2379
2380         /* advance to empty slot */
2381         for (attr = &qla2x00_host_attrs[0]; *attr; ++attr)
2382                 continue;
2383
2384         *attr = &dev_attr_qlini_mode;
2385         attr++;
2386         *attr = &dev_attr_ql2xiniexchg;
2387         attr++;
2388         *attr = &dev_attr_ql2xexchoffld;
2389 }
2390
2391 /* Host attributes. */
2392
2393 static void
2394 qla2x00_get_host_port_id(struct Scsi_Host *shost)
2395 {
2396         scsi_qla_host_t *vha = shost_priv(shost);
2397
2398         fc_host_port_id(shost) = vha->d_id.b.domain << 16 |
2399             vha->d_id.b.area << 8 | vha->d_id.b.al_pa;
2400 }
2401
2402 static void
2403 qla2x00_get_host_speed(struct Scsi_Host *shost)
2404 {
2405         scsi_qla_host_t *vha = shost_priv(shost);
2406         u32 speed;
2407
2408         if (IS_QLAFX00(vha->hw)) {
2409                 qlafx00_get_host_speed(shost);
2410                 return;
2411         }
2412
2413         switch (vha->hw->link_data_rate) {
2414         case PORT_SPEED_1GB:
2415                 speed = FC_PORTSPEED_1GBIT;
2416                 break;
2417         case PORT_SPEED_2GB:
2418                 speed = FC_PORTSPEED_2GBIT;
2419                 break;
2420         case PORT_SPEED_4GB:
2421                 speed = FC_PORTSPEED_4GBIT;
2422                 break;
2423         case PORT_SPEED_8GB:
2424                 speed = FC_PORTSPEED_8GBIT;
2425                 break;
2426         case PORT_SPEED_10GB:
2427                 speed = FC_PORTSPEED_10GBIT;
2428                 break;
2429         case PORT_SPEED_16GB:
2430                 speed = FC_PORTSPEED_16GBIT;
2431                 break;
2432         case PORT_SPEED_32GB:
2433                 speed = FC_PORTSPEED_32GBIT;
2434                 break;
2435         case PORT_SPEED_64GB:
2436                 speed = FC_PORTSPEED_64GBIT;
2437                 break;
2438         default:
2439                 speed = FC_PORTSPEED_UNKNOWN;
2440                 break;
2441         }
2442
2443         fc_host_speed(shost) = speed;
2444 }
2445
2446 static void
2447 qla2x00_get_host_port_type(struct Scsi_Host *shost)
2448 {
2449         scsi_qla_host_t *vha = shost_priv(shost);
2450         uint32_t port_type;
2451
2452         if (vha->vp_idx) {
2453                 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
2454                 return;
2455         }
2456         switch (vha->hw->current_topology) {
2457         case ISP_CFG_NL:
2458                 port_type = FC_PORTTYPE_LPORT;
2459                 break;
2460         case ISP_CFG_FL:
2461                 port_type = FC_PORTTYPE_NLPORT;
2462                 break;
2463         case ISP_CFG_N:
2464                 port_type = FC_PORTTYPE_PTP;
2465                 break;
2466         case ISP_CFG_F:
2467                 port_type = FC_PORTTYPE_NPORT;
2468                 break;
2469         default:
2470                 port_type = FC_PORTTYPE_UNKNOWN;
2471                 break;
2472         }
2473
2474         fc_host_port_type(shost) = port_type;
2475 }
2476
2477 static void
2478 qla2x00_get_starget_node_name(struct scsi_target *starget)
2479 {
2480         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2481         scsi_qla_host_t *vha = shost_priv(host);
2482         fc_port_t *fcport;
2483         u64 node_name = 0;
2484
2485         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2486                 if (fcport->rport &&
2487                     starget->id == fcport->rport->scsi_target_id) {
2488                         node_name = wwn_to_u64(fcport->node_name);
2489                         break;
2490                 }
2491         }
2492
2493         fc_starget_node_name(starget) = node_name;
2494 }
2495
2496 static void
2497 qla2x00_get_starget_port_name(struct scsi_target *starget)
2498 {
2499         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2500         scsi_qla_host_t *vha = shost_priv(host);
2501         fc_port_t *fcport;
2502         u64 port_name = 0;
2503
2504         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2505                 if (fcport->rport &&
2506                     starget->id == fcport->rport->scsi_target_id) {
2507                         port_name = wwn_to_u64(fcport->port_name);
2508                         break;
2509                 }
2510         }
2511
2512         fc_starget_port_name(starget) = port_name;
2513 }
2514
2515 static void
2516 qla2x00_get_starget_port_id(struct scsi_target *starget)
2517 {
2518         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2519         scsi_qla_host_t *vha = shost_priv(host);
2520         fc_port_t *fcport;
2521         uint32_t port_id = ~0U;
2522
2523         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2524                 if (fcport->rport &&
2525                     starget->id == fcport->rport->scsi_target_id) {
2526                         port_id = fcport->d_id.b.domain << 16 |
2527                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2528                         break;
2529                 }
2530         }
2531
2532         fc_starget_port_id(starget) = port_id;
2533 }
2534
2535 static inline void
2536 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
2537 {
2538         rport->dev_loss_tmo = timeout ? timeout : 1;
2539 }
2540
2541 static void
2542 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
2543 {
2544         struct Scsi_Host *host = rport_to_shost(rport);
2545         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2546         unsigned long flags;
2547
2548         if (!fcport)
2549                 return;
2550
2551         /* Now that the rport has been deleted, set the fcport state to
2552            FCS_DEVICE_DEAD */
2553         qla2x00_set_fcport_state(fcport, FCS_DEVICE_DEAD);
2554
2555         /*
2556          * Transport has effectively 'deleted' the rport, clear
2557          * all local references.
2558          */
2559         spin_lock_irqsave(host->host_lock, flags);
2560         fcport->rport = fcport->drport = NULL;
2561         *((fc_port_t **)rport->dd_data) = NULL;
2562         spin_unlock_irqrestore(host->host_lock, flags);
2563
2564         if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
2565                 return;
2566
2567         if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
2568                 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
2569                 return;
2570         }
2571 }
2572
2573 static void
2574 qla2x00_terminate_rport_io(struct fc_rport *rport)
2575 {
2576         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2577
2578         if (!fcport)
2579                 return;
2580
2581         if (test_bit(UNLOADING, &fcport->vha->dpc_flags))
2582                 return;
2583
2584         if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
2585                 return;
2586
2587         if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
2588                 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
2589                 return;
2590         }
2591         /*
2592          * At this point all fcport's software-states are cleared.  Perform any
2593          * final cleanup of firmware resources (PCBs and XCBs).
2594          */
2595         if (fcport->loop_id != FC_NO_LOOP_ID) {
2596                 if (IS_FWI2_CAPABLE(fcport->vha->hw))
2597                         fcport->vha->hw->isp_ops->fabric_logout(fcport->vha,
2598                             fcport->loop_id, fcport->d_id.b.domain,
2599                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
2600                 else
2601                         qla2x00_port_logout(fcport->vha, fcport);
2602         }
2603 }
2604
2605 static int
2606 qla2x00_issue_lip(struct Scsi_Host *shost)
2607 {
2608         scsi_qla_host_t *vha = shost_priv(shost);
2609
2610         if (IS_QLAFX00(vha->hw))
2611                 return 0;
2612
2613         qla2x00_loop_reset(vha);
2614         return 0;
2615 }
2616
2617 static struct fc_host_statistics *
2618 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
2619 {
2620         scsi_qla_host_t *vha = shost_priv(shost);
2621         struct qla_hw_data *ha = vha->hw;
2622         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2623         int rval;
2624         struct link_statistics *stats;
2625         dma_addr_t stats_dma;
2626         struct fc_host_statistics *p = &vha->fc_host_stat;
2627
2628         memset(p, -1, sizeof(*p));
2629
2630         if (IS_QLAFX00(vha->hw))
2631                 goto done;
2632
2633         if (test_bit(UNLOADING, &vha->dpc_flags))
2634                 goto done;
2635
2636         if (unlikely(pci_channel_offline(ha->pdev)))
2637                 goto done;
2638
2639         if (qla2x00_chip_is_down(vha))
2640                 goto done;
2641
2642         stats = dma_alloc_coherent(&ha->pdev->dev, sizeof(*stats), &stats_dma,
2643                                    GFP_KERNEL);
2644         if (!stats) {
2645                 ql_log(ql_log_warn, vha, 0x707d,
2646                     "Failed to allocate memory for stats.\n");
2647                 goto done;
2648         }
2649
2650         rval = QLA_FUNCTION_FAILED;
2651         if (IS_FWI2_CAPABLE(ha)) {
2652                 rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, 0);
2653         } else if (atomic_read(&base_vha->loop_state) == LOOP_READY &&
2654             !ha->dpc_active) {
2655                 /* Must be in a 'READY' state for statistics retrieval. */
2656                 rval = qla2x00_get_link_status(base_vha, base_vha->loop_id,
2657                                                 stats, stats_dma);
2658         }
2659
2660         if (rval != QLA_SUCCESS)
2661                 goto done_free;
2662
2663         p->link_failure_count = stats->link_fail_cnt;
2664         p->loss_of_sync_count = stats->loss_sync_cnt;
2665         p->loss_of_signal_count = stats->loss_sig_cnt;
2666         p->prim_seq_protocol_err_count = stats->prim_seq_err_cnt;
2667         p->invalid_tx_word_count = stats->inval_xmit_word_cnt;
2668         p->invalid_crc_count = stats->inval_crc_cnt;
2669         if (IS_FWI2_CAPABLE(ha)) {
2670                 p->lip_count = stats->lip_cnt;
2671                 p->tx_frames = stats->tx_frames;
2672                 p->rx_frames = stats->rx_frames;
2673                 p->dumped_frames = stats->discarded_frames;
2674                 p->nos_count = stats->nos_rcvd;
2675                 p->error_frames =
2676                         stats->dropped_frames + stats->discarded_frames;
2677                 p->rx_words = vha->qla_stats.input_bytes;
2678                 p->tx_words = vha->qla_stats.output_bytes;
2679         }
2680         p->fcp_control_requests = vha->qla_stats.control_requests;
2681         p->fcp_input_requests = vha->qla_stats.input_requests;
2682         p->fcp_output_requests = vha->qla_stats.output_requests;
2683         p->fcp_input_megabytes = vha->qla_stats.input_bytes >> 20;
2684         p->fcp_output_megabytes = vha->qla_stats.output_bytes >> 20;
2685         p->seconds_since_last_reset =
2686                 get_jiffies_64() - vha->qla_stats.jiffies_at_last_reset;
2687         do_div(p->seconds_since_last_reset, HZ);
2688
2689 done_free:
2690         dma_free_coherent(&ha->pdev->dev, sizeof(struct link_statistics),
2691             stats, stats_dma);
2692 done:
2693         return p;
2694 }
2695
2696 static void
2697 qla2x00_reset_host_stats(struct Scsi_Host *shost)
2698 {
2699         scsi_qla_host_t *vha = shost_priv(shost);
2700         struct qla_hw_data *ha = vha->hw;
2701         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2702         struct link_statistics *stats;
2703         dma_addr_t stats_dma;
2704
2705         memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
2706         memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
2707
2708         vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2709
2710         if (IS_FWI2_CAPABLE(ha)) {
2711                 int rval;
2712
2713                 stats = dma_alloc_coherent(&ha->pdev->dev,
2714                     sizeof(*stats), &stats_dma, GFP_KERNEL);
2715                 if (!stats) {
2716                         ql_log(ql_log_warn, vha, 0x70d7,
2717                             "Failed to allocate memory for stats.\n");
2718                         return;
2719                 }
2720
2721                 /* reset firmware statistics */
2722                 rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, BIT_0);
2723                 if (rval != QLA_SUCCESS)
2724                         ql_log(ql_log_warn, vha, 0x70de,
2725                                "Resetting ISP statistics failed: rval = %d\n",
2726                                rval);
2727
2728                 dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
2729                     stats, stats_dma);
2730         }
2731 }
2732
2733 static void
2734 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
2735 {
2736         scsi_qla_host_t *vha = shost_priv(shost);
2737
2738         qla2x00_get_sym_node_name(vha, fc_host_symbolic_name(shost),
2739             sizeof(fc_host_symbolic_name(shost)));
2740 }
2741
2742 static void
2743 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
2744 {
2745         scsi_qla_host_t *vha = shost_priv(shost);
2746
2747         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
2748 }
2749
2750 static void
2751 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
2752 {
2753         scsi_qla_host_t *vha = shost_priv(shost);
2754         static const uint8_t node_name[WWN_SIZE] = {
2755                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
2756         };
2757         u64 fabric_name = wwn_to_u64(node_name);
2758
2759         if (vha->device_flags & SWITCH_FOUND)
2760                 fabric_name = wwn_to_u64(vha->fabric_node_name);
2761
2762         fc_host_fabric_name(shost) = fabric_name;
2763 }
2764
2765 static void
2766 qla2x00_get_host_port_state(struct Scsi_Host *shost)
2767 {
2768         scsi_qla_host_t *vha = shost_priv(shost);
2769         struct scsi_qla_host *base_vha = pci_get_drvdata(vha->hw->pdev);
2770
2771         if (!base_vha->flags.online) {
2772                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
2773                 return;
2774         }
2775
2776         switch (atomic_read(&base_vha->loop_state)) {
2777         case LOOP_UPDATE:
2778                 fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
2779                 break;
2780         case LOOP_DOWN:
2781                 if (test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags))
2782                         fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
2783                 else
2784                         fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
2785                 break;
2786         case LOOP_DEAD:
2787                 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
2788                 break;
2789         case LOOP_READY:
2790                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
2791                 break;
2792         default:
2793                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
2794                 break;
2795         }
2796 }
2797
2798 static int
2799 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
2800 {
2801         int     ret = 0;
2802         uint8_t qos = 0;
2803         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
2804         scsi_qla_host_t *vha = NULL;
2805         struct qla_hw_data *ha = base_vha->hw;
2806         int     cnt;
2807         struct req_que *req = ha->req_q_map[0];
2808         struct qla_qpair *qpair;
2809
2810         ret = qla24xx_vport_create_req_sanity_check(fc_vport);
2811         if (ret) {
2812                 ql_log(ql_log_warn, vha, 0x707e,
2813                     "Vport sanity check failed, status %x\n", ret);
2814                 return (ret);
2815         }
2816
2817         vha = qla24xx_create_vhost(fc_vport);
2818         if (vha == NULL) {
2819                 ql_log(ql_log_warn, vha, 0x707f, "Vport create host failed.\n");
2820                 return FC_VPORT_FAILED;
2821         }
2822         if (disable) {
2823                 atomic_set(&vha->vp_state, VP_OFFLINE);
2824                 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
2825         } else
2826                 atomic_set(&vha->vp_state, VP_FAILED);
2827
2828         /* ready to create vport */
2829         ql_log(ql_log_info, vha, 0x7080,
2830             "VP entry id %d assigned.\n", vha->vp_idx);
2831
2832         /* initialized vport states */
2833         atomic_set(&vha->loop_state, LOOP_DOWN);
2834         vha->vp_err_state = VP_ERR_PORTDWN;
2835         vha->vp_prev_err_state = VP_ERR_UNKWN;
2836         /* Check if physical ha port is Up */
2837         if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
2838             atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
2839                 /* Don't retry or attempt login of this virtual port */
2840                 ql_dbg(ql_dbg_user, vha, 0x7081,
2841                     "Vport loop state is not UP.\n");
2842                 atomic_set(&vha->loop_state, LOOP_DEAD);
2843                 if (!disable)
2844                         fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
2845         }
2846
2847         if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
2848                 if (ha->fw_attributes & BIT_4) {
2849                         int prot = 0, guard;
2850
2851                         vha->flags.difdix_supported = 1;
2852                         ql_dbg(ql_dbg_user, vha, 0x7082,
2853                             "Registered for DIF/DIX type 1 and 3 protection.\n");
2854                         if (ql2xenabledif == 1)
2855                                 prot = SHOST_DIX_TYPE0_PROTECTION;
2856                         scsi_host_set_prot(vha->host,
2857                             prot | SHOST_DIF_TYPE1_PROTECTION
2858                             | SHOST_DIF_TYPE2_PROTECTION
2859                             | SHOST_DIF_TYPE3_PROTECTION
2860                             | SHOST_DIX_TYPE1_PROTECTION
2861                             | SHOST_DIX_TYPE2_PROTECTION
2862                             | SHOST_DIX_TYPE3_PROTECTION);
2863
2864                         guard = SHOST_DIX_GUARD_CRC;
2865
2866                         if (IS_PI_IPGUARD_CAPABLE(ha) &&
2867                             (ql2xenabledif > 1 || IS_PI_DIFB_DIX0_CAPABLE(ha)))
2868                                 guard |= SHOST_DIX_GUARD_IP;
2869
2870                         scsi_host_set_guard(vha->host, guard);
2871                 } else
2872                         vha->flags.difdix_supported = 0;
2873         }
2874
2875         if (scsi_add_host_with_dma(vha->host, &fc_vport->dev,
2876                                    &ha->pdev->dev)) {
2877                 ql_dbg(ql_dbg_user, vha, 0x7083,
2878                     "scsi_add_host failure for VP[%d].\n", vha->vp_idx);
2879                 goto vport_create_failed_2;
2880         }
2881
2882         /* initialize attributes */
2883         fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
2884         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
2885         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
2886         fc_host_supported_classes(vha->host) =
2887                 fc_host_supported_classes(base_vha->host);
2888         fc_host_supported_speeds(vha->host) =
2889                 fc_host_supported_speeds(base_vha->host);
2890
2891         qlt_vport_create(vha, ha);
2892         qla24xx_vport_disable(fc_vport, disable);
2893
2894         if (!ql2xmqsupport || !ha->npiv_info)
2895                 goto vport_queue;
2896
2897         /* Create a request queue in QoS mode for the vport */
2898         for (cnt = 0; cnt < ha->nvram_npiv_size; cnt++) {
2899                 if (memcmp(ha->npiv_info[cnt].port_name, vha->port_name, 8) == 0
2900                         && memcmp(ha->npiv_info[cnt].node_name, vha->node_name,
2901                                         8) == 0) {
2902                         qos = ha->npiv_info[cnt].q_qos;
2903                         break;
2904                 }
2905         }
2906
2907         if (qos) {
2908                 qpair = qla2xxx_create_qpair(vha, qos, vha->vp_idx, true);
2909                 if (!qpair)
2910                         ql_log(ql_log_warn, vha, 0x7084,
2911                             "Can't create qpair for VP[%d]\n",
2912                             vha->vp_idx);
2913                 else {
2914                         ql_dbg(ql_dbg_multiq, vha, 0xc001,
2915                             "Queue pair: %d Qos: %d) created for VP[%d]\n",
2916                             qpair->id, qos, vha->vp_idx);
2917                         ql_dbg(ql_dbg_user, vha, 0x7085,
2918                             "Queue Pair: %d Qos: %d) created for VP[%d]\n",
2919                             qpair->id, qos, vha->vp_idx);
2920                         req = qpair->req;
2921                         vha->qpair = qpair;
2922                 }
2923         }
2924
2925 vport_queue:
2926         vha->req = req;
2927         return 0;
2928
2929 vport_create_failed_2:
2930         qla24xx_disable_vp(vha);
2931         qla24xx_deallocate_vp_id(vha);
2932         scsi_host_put(vha->host);
2933         return FC_VPORT_FAILED;
2934 }
2935
2936 static int
2937 qla24xx_vport_delete(struct fc_vport *fc_vport)
2938 {
2939         scsi_qla_host_t *vha = fc_vport->dd_data;
2940         struct qla_hw_data *ha = vha->hw;
2941         uint16_t id = vha->vp_idx;
2942
2943         set_bit(VPORT_DELETE, &vha->dpc_flags);
2944
2945         while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
2946             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags))
2947                 msleep(1000);
2948
2949
2950         qla24xx_disable_vp(vha);
2951         qla2x00_wait_for_sess_deletion(vha);
2952
2953         qla_nvme_delete(vha);
2954         vha->flags.delete_progress = 1;
2955
2956         qlt_remove_target(ha, vha);
2957
2958         fc_remove_host(vha->host);
2959
2960         scsi_remove_host(vha->host);
2961
2962         /* Allow timer to run to drain queued items, when removing vp */
2963         qla24xx_deallocate_vp_id(vha);
2964
2965         if (vha->timer_active) {
2966                 qla2x00_vp_stop_timer(vha);
2967                 ql_dbg(ql_dbg_user, vha, 0x7086,
2968                     "Timer for the VP[%d] has stopped\n", vha->vp_idx);
2969         }
2970
2971         qla2x00_free_fcports(vha);
2972
2973         mutex_lock(&ha->vport_lock);
2974         ha->cur_vport_count--;
2975         clear_bit(vha->vp_idx, ha->vp_idx_map);
2976         mutex_unlock(&ha->vport_lock);
2977
2978         dma_free_coherent(&ha->pdev->dev, vha->gnl.size, vha->gnl.l,
2979             vha->gnl.ldma);
2980
2981         vha->gnl.l = NULL;
2982
2983         vfree(vha->scan.l);
2984
2985         if (vha->qpair && vha->qpair->vp_idx == vha->vp_idx) {
2986                 if (qla2xxx_delete_qpair(vha, vha->qpair) != QLA_SUCCESS)
2987                         ql_log(ql_log_warn, vha, 0x7087,
2988                             "Queue Pair delete failed.\n");
2989         }
2990
2991         ql_log(ql_log_info, vha, 0x7088, "VP[%d] deleted.\n", id);
2992         scsi_host_put(vha->host);
2993         return 0;
2994 }
2995
2996 static int
2997 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
2998 {
2999         scsi_qla_host_t *vha = fc_vport->dd_data;
3000
3001         if (disable)
3002                 qla24xx_disable_vp(vha);
3003         else
3004                 qla24xx_enable_vp(vha);
3005
3006         return 0;
3007 }
3008
3009 struct fc_function_template qla2xxx_transport_functions = {
3010
3011         .show_host_node_name = 1,
3012         .show_host_port_name = 1,
3013         .show_host_supported_classes = 1,
3014         .show_host_supported_speeds = 1,
3015
3016         .get_host_port_id = qla2x00_get_host_port_id,
3017         .show_host_port_id = 1,
3018         .get_host_speed = qla2x00_get_host_speed,
3019         .show_host_speed = 1,
3020         .get_host_port_type = qla2x00_get_host_port_type,
3021         .show_host_port_type = 1,
3022         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
3023         .show_host_symbolic_name = 1,
3024         .set_host_system_hostname = qla2x00_set_host_system_hostname,
3025         .show_host_system_hostname = 1,
3026         .get_host_fabric_name = qla2x00_get_host_fabric_name,
3027         .show_host_fabric_name = 1,
3028         .get_host_port_state = qla2x00_get_host_port_state,
3029         .show_host_port_state = 1,
3030
3031         .dd_fcrport_size = sizeof(struct fc_port *),
3032         .show_rport_supported_classes = 1,
3033
3034         .get_starget_node_name = qla2x00_get_starget_node_name,
3035         .show_starget_node_name = 1,
3036         .get_starget_port_name = qla2x00_get_starget_port_name,
3037         .show_starget_port_name = 1,
3038         .get_starget_port_id  = qla2x00_get_starget_port_id,
3039         .show_starget_port_id = 1,
3040
3041         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
3042         .show_rport_dev_loss_tmo = 1,
3043
3044         .issue_fc_host_lip = qla2x00_issue_lip,
3045         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
3046         .terminate_rport_io = qla2x00_terminate_rport_io,
3047         .get_fc_host_stats = qla2x00_get_fc_host_stats,
3048         .reset_fc_host_stats = qla2x00_reset_host_stats,
3049
3050         .vport_create = qla24xx_vport_create,
3051         .vport_disable = qla24xx_vport_disable,
3052         .vport_delete = qla24xx_vport_delete,
3053         .bsg_request = qla24xx_bsg_request,
3054         .bsg_timeout = qla24xx_bsg_timeout,
3055 };
3056
3057 struct fc_function_template qla2xxx_transport_vport_functions = {
3058
3059         .show_host_node_name = 1,
3060         .show_host_port_name = 1,
3061         .show_host_supported_classes = 1,
3062
3063         .get_host_port_id = qla2x00_get_host_port_id,
3064         .show_host_port_id = 1,
3065         .get_host_speed = qla2x00_get_host_speed,
3066         .show_host_speed = 1,
3067         .get_host_port_type = qla2x00_get_host_port_type,
3068         .show_host_port_type = 1,
3069         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
3070         .show_host_symbolic_name = 1,
3071         .set_host_system_hostname = qla2x00_set_host_system_hostname,
3072         .show_host_system_hostname = 1,
3073         .get_host_fabric_name = qla2x00_get_host_fabric_name,
3074         .show_host_fabric_name = 1,
3075         .get_host_port_state = qla2x00_get_host_port_state,
3076         .show_host_port_state = 1,
3077
3078         .dd_fcrport_size = sizeof(struct fc_port *),
3079         .show_rport_supported_classes = 1,
3080
3081         .get_starget_node_name = qla2x00_get_starget_node_name,
3082         .show_starget_node_name = 1,
3083         .get_starget_port_name = qla2x00_get_starget_port_name,
3084         .show_starget_port_name = 1,
3085         .get_starget_port_id  = qla2x00_get_starget_port_id,
3086         .show_starget_port_id = 1,
3087
3088         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
3089         .show_rport_dev_loss_tmo = 1,
3090
3091         .issue_fc_host_lip = qla2x00_issue_lip,
3092         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
3093         .terminate_rport_io = qla2x00_terminate_rport_io,
3094         .get_fc_host_stats = qla2x00_get_fc_host_stats,
3095         .reset_fc_host_stats = qla2x00_reset_host_stats,
3096
3097         .bsg_request = qla24xx_bsg_request,
3098         .bsg_timeout = qla24xx_bsg_timeout,
3099 };
3100
3101 void
3102 qla2x00_init_host_attr(scsi_qla_host_t *vha)
3103 {
3104         struct qla_hw_data *ha = vha->hw;
3105         u32 speeds = FC_PORTSPEED_UNKNOWN;
3106
3107         fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
3108         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
3109         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
3110         fc_host_supported_classes(vha->host) = ha->base_qpair->enable_class_2 ?
3111                         (FC_COS_CLASS2|FC_COS_CLASS3) : FC_COS_CLASS3;
3112         fc_host_max_npiv_vports(vha->host) = ha->max_npiv_vports;
3113         fc_host_npiv_vports_inuse(vha->host) = ha->cur_vport_count;
3114
3115         if (IS_CNA_CAPABLE(ha))
3116                 speeds = FC_PORTSPEED_10GBIT;
3117         else if (IS_QLA28XX(ha) || IS_QLA27XX(ha)) {
3118                 if (ha->max_supported_speed == 2) {
3119                         if (ha->min_supported_speed <= 6)
3120                                 speeds |= FC_PORTSPEED_64GBIT;
3121                 }
3122                 if (ha->max_supported_speed == 2 ||
3123                     ha->max_supported_speed == 1) {
3124                         if (ha->min_supported_speed <= 5)
3125                                 speeds |= FC_PORTSPEED_32GBIT;
3126                 }
3127                 if (ha->max_supported_speed == 2 ||
3128                     ha->max_supported_speed == 1 ||
3129                     ha->max_supported_speed == 0) {
3130                         if (ha->min_supported_speed <= 4)
3131                                 speeds |= FC_PORTSPEED_16GBIT;
3132                 }
3133                 if (ha->max_supported_speed == 1 ||
3134                     ha->max_supported_speed == 0) {
3135                         if (ha->min_supported_speed <= 3)
3136                                 speeds |= FC_PORTSPEED_8GBIT;
3137                 }
3138                 if (ha->max_supported_speed == 0) {
3139                         if (ha->min_supported_speed <= 2)
3140                                 speeds |= FC_PORTSPEED_4GBIT;
3141                 }
3142         } else if (IS_QLA2031(ha))
3143                 speeds = FC_PORTSPEED_16GBIT|FC_PORTSPEED_8GBIT|
3144                         FC_PORTSPEED_4GBIT;
3145         else if (IS_QLA25XX(ha) || IS_QLAFX00(ha))
3146                 speeds = FC_PORTSPEED_8GBIT|FC_PORTSPEED_4GBIT|
3147                         FC_PORTSPEED_2GBIT|FC_PORTSPEED_1GBIT;
3148         else if (IS_QLA24XX_TYPE(ha))
3149                 speeds = FC_PORTSPEED_4GBIT|FC_PORTSPEED_2GBIT|
3150                         FC_PORTSPEED_1GBIT;
3151         else if (IS_QLA23XX(ha))
3152                 speeds = FC_PORTSPEED_2GBIT|FC_PORTSPEED_1GBIT;
3153         else
3154                 speeds = FC_PORTSPEED_1GBIT;
3155
3156         fc_host_supported_speeds(vha->host) = speeds;
3157 }