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