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