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