[SCSI] qla2xxx: Prevent sending mbx commands from sysfs during isp reset.
[safe/jmp/linux-2.6] / drivers / scsi / qla2xxx / qla_attr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/kthread.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
12
13 static int qla24xx_vport_disable(struct fc_vport *, bool);
14 static int qla84xx_reset(scsi_qla_host_t *, struct msg_echo_lb *, struct fc_bsg_job *);
15 int qla84xx_reset_chip(scsi_qla_host_t *, uint16_t, uint16_t *);
16 static int qla84xx_mgmt_cmd(scsi_qla_host_t *, struct msg_echo_lb *, struct fc_bsg_job *);
17 /* SYSFS attributes --------------------------------------------------------- */
18
19 static ssize_t
20 qla2x00_sysfs_read_fw_dump(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
28         if (ha->fw_dump_reading == 0)
29                 return 0;
30
31         return memory_read_from_buffer(buf, count, &off, ha->fw_dump,
32                                         ha->fw_dump_len);
33 }
34
35 static ssize_t
36 qla2x00_sysfs_write_fw_dump(struct kobject *kobj,
37                             struct bin_attribute *bin_attr,
38                             char *buf, loff_t off, size_t count)
39 {
40         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
41             struct device, kobj)));
42         struct qla_hw_data *ha = vha->hw;
43         int reading;
44
45         if (off != 0)
46                 return (0);
47
48         reading = simple_strtol(buf, NULL, 10);
49         switch (reading) {
50         case 0:
51                 if (!ha->fw_dump_reading)
52                         break;
53
54                 qla_printk(KERN_INFO, ha,
55                     "Firmware dump cleared on (%ld).\n", vha->host_no);
56
57                 ha->fw_dump_reading = 0;
58                 ha->fw_dumped = 0;
59                 break;
60         case 1:
61                 if (ha->fw_dumped && !ha->fw_dump_reading) {
62                         ha->fw_dump_reading = 1;
63
64                         qla_printk(KERN_INFO, ha,
65                             "Raw firmware dump ready for read on (%ld).\n",
66                             vha->host_no);
67                 }
68                 break;
69         case 2:
70                 qla2x00_alloc_fw_dump(vha);
71                 break;
72         case 3:
73                 qla2x00_system_error(vha);
74                 break;
75         }
76         return (count);
77 }
78
79 static struct bin_attribute sysfs_fw_dump_attr = {
80         .attr = {
81                 .name = "fw_dump",
82                 .mode = S_IRUSR | S_IWUSR,
83         },
84         .size = 0,
85         .read = qla2x00_sysfs_read_fw_dump,
86         .write = qla2x00_sysfs_write_fw_dump,
87 };
88
89 static ssize_t
90 qla2x00_sysfs_read_nvram(struct kobject *kobj,
91                          struct bin_attribute *bin_attr,
92                          char *buf, loff_t off, size_t count)
93 {
94         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
95             struct device, kobj)));
96         struct qla_hw_data *ha = vha->hw;
97
98         if (!capable(CAP_SYS_ADMIN))
99                 return 0;
100
101         if (IS_NOCACHE_VPD_TYPE(ha))
102                 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
103                     ha->nvram_size);
104         return memory_read_from_buffer(buf, count, &off, ha->nvram,
105                                         ha->nvram_size);
106 }
107
108 static ssize_t
109 qla2x00_sysfs_write_nvram(struct kobject *kobj,
110                           struct bin_attribute *bin_attr,
111                           char *buf, loff_t off, size_t count)
112 {
113         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
114             struct device, kobj)));
115         struct qla_hw_data *ha = vha->hw;
116         uint16_t        cnt;
117
118         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size ||
119             !ha->isp_ops->write_nvram)
120                 return 0;
121
122         /* Checksum NVRAM. */
123         if (IS_FWI2_CAPABLE(ha)) {
124                 uint32_t *iter;
125                 uint32_t chksum;
126
127                 iter = (uint32_t *)buf;
128                 chksum = 0;
129                 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
130                         chksum += le32_to_cpu(*iter++);
131                 chksum = ~chksum + 1;
132                 *iter = cpu_to_le32(chksum);
133         } else {
134                 uint8_t *iter;
135                 uint8_t chksum;
136
137                 iter = (uint8_t *)buf;
138                 chksum = 0;
139                 for (cnt = 0; cnt < count - 1; cnt++)
140                         chksum += *iter++;
141                 chksum = ~chksum + 1;
142                 *iter = chksum;
143         }
144
145         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
146                 qla_printk(KERN_WARNING, ha,
147                     "HBA not online, failing NVRAM update.\n");
148                 return -EAGAIN;
149         }
150
151         /* Write NVRAM. */
152         ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->nvram_base, count);
153         ha->isp_ops->read_nvram(vha, (uint8_t *)ha->nvram, ha->nvram_base,
154             count);
155
156         /* NVRAM settings take effect immediately. */
157         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
158         qla2xxx_wake_dpc(vha);
159         qla2x00_wait_for_chip_reset(vha);
160
161         return (count);
162 }
163
164 static struct bin_attribute sysfs_nvram_attr = {
165         .attr = {
166                 .name = "nvram",
167                 .mode = S_IRUSR | S_IWUSR,
168         },
169         .size = 512,
170         .read = qla2x00_sysfs_read_nvram,
171         .write = qla2x00_sysfs_write_nvram,
172 };
173
174 static ssize_t
175 qla2x00_sysfs_read_optrom(struct kobject *kobj,
176                           struct bin_attribute *bin_attr,
177                           char *buf, loff_t off, size_t count)
178 {
179         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
180             struct device, kobj)));
181         struct qla_hw_data *ha = vha->hw;
182
183         if (ha->optrom_state != QLA_SREADING)
184                 return 0;
185
186         return memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
187                                         ha->optrom_region_size);
188 }
189
190 static ssize_t
191 qla2x00_sysfs_write_optrom(struct kobject *kobj,
192                            struct bin_attribute *bin_attr,
193                            char *buf, loff_t off, size_t count)
194 {
195         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
196             struct device, kobj)));
197         struct qla_hw_data *ha = vha->hw;
198
199         if (ha->optrom_state != QLA_SWRITING)
200                 return -EINVAL;
201         if (off > ha->optrom_region_size)
202                 return -ERANGE;
203         if (off + count > ha->optrom_region_size)
204                 count = ha->optrom_region_size - off;
205
206         memcpy(&ha->optrom_buffer[off], buf, count);
207
208         return count;
209 }
210
211 static struct bin_attribute sysfs_optrom_attr = {
212         .attr = {
213                 .name = "optrom",
214                 .mode = S_IRUSR | S_IWUSR,
215         },
216         .size = 0,
217         .read = qla2x00_sysfs_read_optrom,
218         .write = qla2x00_sysfs_write_optrom,
219 };
220
221 static ssize_t
222 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj,
223                                struct bin_attribute *bin_attr,
224                                char *buf, loff_t off, size_t count)
225 {
226         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
227             struct device, kobj)));
228         struct qla_hw_data *ha = vha->hw;
229
230         uint32_t start = 0;
231         uint32_t size = ha->optrom_size;
232         int val, valid;
233
234         if (off)
235                 return 0;
236
237         if (unlikely(pci_channel_offline(ha->pdev)))
238                 return 0;
239
240         if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
241                 return -EINVAL;
242         if (start > ha->optrom_size)
243                 return -EINVAL;
244
245         switch (val) {
246         case 0:
247                 if (ha->optrom_state != QLA_SREADING &&
248                     ha->optrom_state != QLA_SWRITING)
249                         break;
250
251                 ha->optrom_state = QLA_SWAITING;
252
253                 DEBUG2(qla_printk(KERN_INFO, ha,
254                     "Freeing flash region allocation -- 0x%x bytes.\n",
255                     ha->optrom_region_size));
256
257                 vfree(ha->optrom_buffer);
258                 ha->optrom_buffer = NULL;
259                 break;
260         case 1:
261                 if (ha->optrom_state != QLA_SWAITING)
262                         break;
263
264                 ha->optrom_region_start = start;
265                 ha->optrom_region_size = start + size > ha->optrom_size ?
266                     ha->optrom_size - start : size;
267
268                 ha->optrom_state = QLA_SREADING;
269                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
270                 if (ha->optrom_buffer == NULL) {
271                         qla_printk(KERN_WARNING, ha,
272                             "Unable to allocate memory for optrom retrieval "
273                             "(%x).\n", ha->optrom_region_size);
274
275                         ha->optrom_state = QLA_SWAITING;
276                         return count;
277                 }
278
279                 DEBUG2(qla_printk(KERN_INFO, ha,
280                     "Reading flash region -- 0x%x/0x%x.\n",
281                     ha->optrom_region_start, ha->optrom_region_size));
282
283                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
284                 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
285                     ha->optrom_region_start, ha->optrom_region_size);
286                 break;
287         case 2:
288                 if (ha->optrom_state != QLA_SWAITING)
289                         break;
290
291                 /*
292                  * We need to be more restrictive on which FLASH regions are
293                  * allowed to be updated via user-space.  Regions accessible
294                  * via this method include:
295                  *
296                  * ISP21xx/ISP22xx/ISP23xx type boards:
297                  *
298                  *      0x000000 -> 0x020000 -- Boot code.
299                  *
300                  * ISP2322/ISP24xx type boards:
301                  *
302                  *      0x000000 -> 0x07ffff -- Boot code.
303                  *      0x080000 -> 0x0fffff -- Firmware.
304                  *
305                  * ISP25xx type boards:
306                  *
307                  *      0x000000 -> 0x07ffff -- Boot code.
308                  *      0x080000 -> 0x0fffff -- Firmware.
309                  *      0x120000 -> 0x12ffff -- VPD and HBA parameters.
310                  */
311                 valid = 0;
312                 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
313                         valid = 1;
314                 else if (start == (ha->flt_region_boot * 4) ||
315                     start == (ha->flt_region_fw * 4))
316                         valid = 1;
317                 else if (IS_QLA25XX(ha) || IS_QLA81XX(ha))
318                     valid = 1;
319                 if (!valid) {
320                         qla_printk(KERN_WARNING, ha,
321                             "Invalid start region 0x%x/0x%x.\n", start, size);
322                         return -EINVAL;
323                 }
324
325                 ha->optrom_region_start = start;
326                 ha->optrom_region_size = start + size > ha->optrom_size ?
327                     ha->optrom_size - start : size;
328
329                 ha->optrom_state = QLA_SWRITING;
330                 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
331                 if (ha->optrom_buffer == NULL) {
332                         qla_printk(KERN_WARNING, ha,
333                             "Unable to allocate memory for optrom update "
334                             "(%x).\n", ha->optrom_region_size);
335
336                         ha->optrom_state = QLA_SWAITING;
337                         return count;
338                 }
339
340                 DEBUG2(qla_printk(KERN_INFO, ha,
341                     "Staging flash region write -- 0x%x/0x%x.\n",
342                     ha->optrom_region_start, ha->optrom_region_size));
343
344                 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
345                 break;
346         case 3:
347                 if (ha->optrom_state != QLA_SWRITING)
348                         break;
349
350                 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
351                         qla_printk(KERN_WARNING, ha,
352                             "HBA not online, failing flash update.\n");
353                         return -EAGAIN;
354                 }
355
356                 DEBUG2(qla_printk(KERN_INFO, ha,
357                     "Writing flash region -- 0x%x/0x%x.\n",
358                     ha->optrom_region_start, ha->optrom_region_size));
359
360                 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
361                     ha->optrom_region_start, ha->optrom_region_size);
362                 break;
363         default:
364                 count = -EINVAL;
365         }
366         return count;
367 }
368
369 static struct bin_attribute sysfs_optrom_ctl_attr = {
370         .attr = {
371                 .name = "optrom_ctl",
372                 .mode = S_IWUSR,
373         },
374         .size = 0,
375         .write = qla2x00_sysfs_write_optrom_ctl,
376 };
377
378 static ssize_t
379 qla2x00_sysfs_read_vpd(struct kobject *kobj,
380                        struct bin_attribute *bin_attr,
381                        char *buf, loff_t off, size_t count)
382 {
383         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
384             struct device, kobj)));
385         struct qla_hw_data *ha = vha->hw;
386
387         if (unlikely(pci_channel_offline(ha->pdev)))
388                 return 0;
389
390         if (!capable(CAP_SYS_ADMIN))
391                 return 0;
392
393         if (IS_NOCACHE_VPD_TYPE(ha))
394                 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
395                     ha->vpd_size);
396         return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
397 }
398
399 static ssize_t
400 qla2x00_sysfs_write_vpd(struct kobject *kobj,
401                         struct bin_attribute *bin_attr,
402                         char *buf, loff_t off, size_t count)
403 {
404         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
405             struct device, kobj)));
406         struct qla_hw_data *ha = vha->hw;
407         uint8_t *tmp_data;
408
409         if (unlikely(pci_channel_offline(ha->pdev)))
410                 return 0;
411
412         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size ||
413             !ha->isp_ops->write_nvram)
414                 return 0;
415
416         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
417                 qla_printk(KERN_WARNING, ha,
418                     "HBA not online, failing VPD update.\n");
419                 return -EAGAIN;
420         }
421
422         /* Write NVRAM. */
423         ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->vpd_base, count);
424         ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd, ha->vpd_base, count);
425
426         /* Update flash version information for 4Gb & above. */
427         if (!IS_FWI2_CAPABLE(ha))
428                 goto done;
429
430         tmp_data = vmalloc(256);
431         if (!tmp_data) {
432                 qla_printk(KERN_WARNING, ha,
433                     "Unable to allocate memory for VPD information update.\n");
434                 goto done;
435         }
436         ha->isp_ops->get_flash_version(vha, tmp_data);
437         vfree(tmp_data);
438 done:
439         return count;
440 }
441
442 static struct bin_attribute sysfs_vpd_attr = {
443         .attr = {
444                 .name = "vpd",
445                 .mode = S_IRUSR | S_IWUSR,
446         },
447         .size = 0,
448         .read = qla2x00_sysfs_read_vpd,
449         .write = qla2x00_sysfs_write_vpd,
450 };
451
452 static ssize_t
453 qla2x00_sysfs_read_sfp(struct kobject *kobj,
454                        struct bin_attribute *bin_attr,
455                        char *buf, loff_t off, size_t count)
456 {
457         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
458             struct device, kobj)));
459         struct qla_hw_data *ha = vha->hw;
460         uint16_t iter, addr, offset;
461         int rval;
462
463         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
464                 return 0;
465
466         if (ha->sfp_data)
467                 goto do_read;
468
469         ha->sfp_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
470             &ha->sfp_data_dma);
471         if (!ha->sfp_data) {
472                 qla_printk(KERN_WARNING, ha,
473                     "Unable to allocate memory for SFP read-data.\n");
474                 return 0;
475         }
476
477 do_read:
478         memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
479         addr = 0xa0;
480         for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
481             iter++, offset += SFP_BLOCK_SIZE) {
482                 if (iter == 4) {
483                         /* Skip to next device address. */
484                         addr = 0xa2;
485                         offset = 0;
486                 }
487
488                 rval = qla2x00_read_sfp(vha, ha->sfp_data_dma, addr, offset,
489                     SFP_BLOCK_SIZE);
490                 if (rval != QLA_SUCCESS) {
491                         qla_printk(KERN_WARNING, ha,
492                             "Unable to read SFP data (%x/%x/%x).\n", rval,
493                             addr, offset);
494                         count = 0;
495                         break;
496                 }
497                 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
498                 buf += SFP_BLOCK_SIZE;
499         }
500
501         return count;
502 }
503
504 static struct bin_attribute sysfs_sfp_attr = {
505         .attr = {
506                 .name = "sfp",
507                 .mode = S_IRUSR | S_IWUSR,
508         },
509         .size = SFP_DEV_SIZE * 2,
510         .read = qla2x00_sysfs_read_sfp,
511 };
512
513 static ssize_t
514 qla2x00_sysfs_write_reset(struct kobject *kobj,
515                         struct bin_attribute *bin_attr,
516                         char *buf, loff_t off, size_t count)
517 {
518         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
519             struct device, kobj)));
520         struct qla_hw_data *ha = vha->hw;
521         int type;
522
523         if (off != 0)
524                 return 0;
525
526         type = simple_strtol(buf, NULL, 10);
527         switch (type) {
528         case 0x2025c:
529                 qla_printk(KERN_INFO, ha,
530                     "Issuing ISP reset on (%ld).\n", vha->host_no);
531
532                 scsi_block_requests(vha->host);
533                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
534                 qla2xxx_wake_dpc(vha);
535                 qla2x00_wait_for_chip_reset(vha);
536                 scsi_unblock_requests(vha->host);
537                 break;
538         case 0x2025d:
539                 if (!IS_QLA81XX(ha))
540                         break;
541
542                 qla_printk(KERN_INFO, ha,
543                     "Issuing MPI reset on (%ld).\n", vha->host_no);
544
545                 /* Make sure FC side is not in reset */
546                 qla2x00_wait_for_hba_online(vha);
547
548                 /* Issue MPI reset */
549                 scsi_block_requests(vha->host);
550                 if (qla81xx_restart_mpi_firmware(vha) != QLA_SUCCESS)
551                         qla_printk(KERN_WARNING, ha,
552                             "MPI reset failed on (%ld).\n", vha->host_no);
553                 scsi_unblock_requests(vha->host);
554                 break;
555         }
556         return count;
557 }
558
559 static struct bin_attribute sysfs_reset_attr = {
560         .attr = {
561                 .name = "reset",
562                 .mode = S_IWUSR,
563         },
564         .size = 0,
565         .write = qla2x00_sysfs_write_reset,
566 };
567
568 static ssize_t
569 qla2x00_sysfs_write_edc(struct kobject *kobj,
570                         struct bin_attribute *bin_attr,
571                         char *buf, loff_t off, size_t count)
572 {
573         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
574             struct device, kobj)));
575         struct qla_hw_data *ha = vha->hw;
576         uint16_t dev, adr, opt, len;
577         int rval;
578
579         ha->edc_data_len = 0;
580
581         if (!capable(CAP_SYS_ADMIN) || off != 0 || count < 8)
582                 return 0;
583
584         if (!ha->edc_data) {
585                 ha->edc_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
586                     &ha->edc_data_dma);
587                 if (!ha->edc_data) {
588                         DEBUG2(qla_printk(KERN_INFO, ha,
589                             "Unable to allocate memory for EDC write.\n"));
590                         return 0;
591                 }
592         }
593
594         dev = le16_to_cpup((void *)&buf[0]);
595         adr = le16_to_cpup((void *)&buf[2]);
596         opt = le16_to_cpup((void *)&buf[4]);
597         len = le16_to_cpup((void *)&buf[6]);
598
599         if (!(opt & BIT_0))
600                 if (len == 0 || len > DMA_POOL_SIZE || len > count - 8)
601                         return -EINVAL;
602
603         memcpy(ha->edc_data, &buf[8], len);
604
605         rval = qla2x00_write_edc(vha, dev, adr, ha->edc_data_dma,
606             ha->edc_data, len, opt);
607         if (rval != QLA_SUCCESS) {
608                 DEBUG2(qla_printk(KERN_INFO, ha,
609                     "Unable to write EDC (%x) %02x:%02x:%04x:%02x:%02x.\n",
610                     rval, dev, adr, opt, len, *buf));
611                 return 0;
612         }
613
614         return count;
615 }
616
617 static struct bin_attribute sysfs_edc_attr = {
618         .attr = {
619                 .name = "edc",
620                 .mode = S_IWUSR,
621         },
622         .size = 0,
623         .write = qla2x00_sysfs_write_edc,
624 };
625
626 static ssize_t
627 qla2x00_sysfs_write_edc_status(struct kobject *kobj,
628                         struct bin_attribute *bin_attr,
629                         char *buf, loff_t off, size_t count)
630 {
631         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
632             struct device, kobj)));
633         struct qla_hw_data *ha = vha->hw;
634         uint16_t dev, adr, opt, len;
635         int rval;
636
637         ha->edc_data_len = 0;
638
639         if (!capable(CAP_SYS_ADMIN) || off != 0 || count < 8)
640                 return 0;
641
642         if (!ha->edc_data) {
643                 ha->edc_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
644                     &ha->edc_data_dma);
645                 if (!ha->edc_data) {
646                         DEBUG2(qla_printk(KERN_INFO, ha,
647                             "Unable to allocate memory for EDC status.\n"));
648                         return 0;
649                 }
650         }
651
652         dev = le16_to_cpup((void *)&buf[0]);
653         adr = le16_to_cpup((void *)&buf[2]);
654         opt = le16_to_cpup((void *)&buf[4]);
655         len = le16_to_cpup((void *)&buf[6]);
656
657         if (!(opt & BIT_0))
658                 if (len == 0 || len > DMA_POOL_SIZE)
659                         return -EINVAL;
660
661         memset(ha->edc_data, 0, len);
662         rval = qla2x00_read_edc(vha, dev, adr, ha->edc_data_dma,
663             ha->edc_data, len, opt);
664         if (rval != QLA_SUCCESS) {
665                 DEBUG2(qla_printk(KERN_INFO, ha,
666                     "Unable to write EDC status (%x) %02x:%02x:%04x:%02x.\n",
667                     rval, dev, adr, opt, len));
668                 return 0;
669         }
670
671         ha->edc_data_len = len;
672
673         return count;
674 }
675
676 static ssize_t
677 qla2x00_sysfs_read_edc_status(struct kobject *kobj,
678                            struct bin_attribute *bin_attr,
679                            char *buf, loff_t off, size_t count)
680 {
681         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
682             struct device, kobj)));
683         struct qla_hw_data *ha = vha->hw;
684
685         if (!capable(CAP_SYS_ADMIN) || off != 0 || count == 0)
686                 return 0;
687
688         if (!ha->edc_data || ha->edc_data_len == 0 || ha->edc_data_len > count)
689                 return -EINVAL;
690
691         memcpy(buf, ha->edc_data, ha->edc_data_len);
692
693         return ha->edc_data_len;
694 }
695
696 static struct bin_attribute sysfs_edc_status_attr = {
697         .attr = {
698                 .name = "edc_status",
699                 .mode = S_IRUSR | S_IWUSR,
700         },
701         .size = 0,
702         .write = qla2x00_sysfs_write_edc_status,
703         .read = qla2x00_sysfs_read_edc_status,
704 };
705
706 static ssize_t
707 qla2x00_sysfs_read_xgmac_stats(struct kobject *kobj,
708                        struct bin_attribute *bin_attr,
709                        char *buf, loff_t off, size_t count)
710 {
711         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
712             struct device, kobj)));
713         struct qla_hw_data *ha = vha->hw;
714         int rval;
715         uint16_t actual_size;
716
717         if (!capable(CAP_SYS_ADMIN) || off != 0 || count > XGMAC_DATA_SIZE)
718                 return 0;
719
720         if (ha->xgmac_data)
721                 goto do_read;
722
723         ha->xgmac_data = dma_alloc_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
724             &ha->xgmac_data_dma, GFP_KERNEL);
725         if (!ha->xgmac_data) {
726                 qla_printk(KERN_WARNING, ha,
727                     "Unable to allocate memory for XGMAC read-data.\n");
728                 return 0;
729         }
730
731 do_read:
732         actual_size = 0;
733         memset(ha->xgmac_data, 0, XGMAC_DATA_SIZE);
734
735         rval = qla2x00_get_xgmac_stats(vha, ha->xgmac_data_dma,
736             XGMAC_DATA_SIZE, &actual_size);
737         if (rval != QLA_SUCCESS) {
738                 qla_printk(KERN_WARNING, ha,
739                     "Unable to read XGMAC data (%x).\n", rval);
740                 count = 0;
741         }
742
743         count = actual_size > count ? count: actual_size;
744         memcpy(buf, ha->xgmac_data, count);
745
746         return count;
747 }
748
749 static struct bin_attribute sysfs_xgmac_stats_attr = {
750         .attr = {
751                 .name = "xgmac_stats",
752                 .mode = S_IRUSR,
753         },
754         .size = 0,
755         .read = qla2x00_sysfs_read_xgmac_stats,
756 };
757
758 static ssize_t
759 qla2x00_sysfs_read_dcbx_tlv(struct kobject *kobj,
760                        struct bin_attribute *bin_attr,
761                        char *buf, loff_t off, size_t count)
762 {
763         struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
764             struct device, kobj)));
765         struct qla_hw_data *ha = vha->hw;
766         int rval;
767         uint16_t actual_size;
768
769         if (!capable(CAP_SYS_ADMIN) || off != 0 || count > DCBX_TLV_DATA_SIZE)
770                 return 0;
771
772         if (ha->dcbx_tlv)
773                 goto do_read;
774
775         ha->dcbx_tlv = dma_alloc_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
776             &ha->dcbx_tlv_dma, GFP_KERNEL);
777         if (!ha->dcbx_tlv) {
778                 qla_printk(KERN_WARNING, ha,
779                     "Unable to allocate memory for DCBX TLV read-data.\n");
780                 return 0;
781         }
782
783 do_read:
784         actual_size = 0;
785         memset(ha->dcbx_tlv, 0, DCBX_TLV_DATA_SIZE);
786
787         rval = qla2x00_get_dcbx_params(vha, ha->dcbx_tlv_dma,
788             DCBX_TLV_DATA_SIZE);
789         if (rval != QLA_SUCCESS) {
790                 qla_printk(KERN_WARNING, ha,
791                     "Unable to read DCBX TLV data (%x).\n", rval);
792                 count = 0;
793         }
794
795         memcpy(buf, ha->dcbx_tlv, count);
796
797         return count;
798 }
799
800 static struct bin_attribute sysfs_dcbx_tlv_attr = {
801         .attr = {
802                 .name = "dcbx_tlv",
803                 .mode = S_IRUSR,
804         },
805         .size = 0,
806         .read = qla2x00_sysfs_read_dcbx_tlv,
807 };
808
809 static struct sysfs_entry {
810         char *name;
811         struct bin_attribute *attr;
812         int is4GBp_only;
813 } bin_file_entries[] = {
814         { "fw_dump", &sysfs_fw_dump_attr, },
815         { "nvram", &sysfs_nvram_attr, },
816         { "optrom", &sysfs_optrom_attr, },
817         { "optrom_ctl", &sysfs_optrom_ctl_attr, },
818         { "vpd", &sysfs_vpd_attr, 1 },
819         { "sfp", &sysfs_sfp_attr, 1 },
820         { "reset", &sysfs_reset_attr, },
821         { "edc", &sysfs_edc_attr, 2 },
822         { "edc_status", &sysfs_edc_status_attr, 2 },
823         { "xgmac_stats", &sysfs_xgmac_stats_attr, 3 },
824         { "dcbx_tlv", &sysfs_dcbx_tlv_attr, 3 },
825         { NULL },
826 };
827
828 void
829 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *vha)
830 {
831         struct Scsi_Host *host = vha->host;
832         struct sysfs_entry *iter;
833         int ret;
834
835         for (iter = bin_file_entries; iter->name; iter++) {
836                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(vha->hw))
837                         continue;
838                 if (iter->is4GBp_only == 2 && !IS_QLA25XX(vha->hw))
839                         continue;
840                 if (iter->is4GBp_only == 3 && !IS_QLA81XX(vha->hw))
841                         continue;
842
843                 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
844                     iter->attr);
845                 if (ret)
846                         qla_printk(KERN_INFO, vha->hw,
847                             "Unable to create sysfs %s binary attribute "
848                             "(%d).\n", iter->name, ret);
849         }
850 }
851
852 void
853 qla2x00_free_sysfs_attr(scsi_qla_host_t *vha)
854 {
855         struct Scsi_Host *host = vha->host;
856         struct sysfs_entry *iter;
857         struct qla_hw_data *ha = vha->hw;
858
859         for (iter = bin_file_entries; iter->name; iter++) {
860                 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
861                         continue;
862                 if (iter->is4GBp_only == 2 && !IS_QLA25XX(ha))
863                         continue;
864                 if (iter->is4GBp_only == 3 && !IS_QLA81XX(ha))
865                         continue;
866
867                 sysfs_remove_bin_file(&host->shost_gendev.kobj,
868                     iter->attr);
869         }
870
871         if (ha->beacon_blink_led == 1)
872                 ha->isp_ops->beacon_off(vha);
873 }
874
875 /* Scsi_Host attributes. */
876
877 static ssize_t
878 qla2x00_drvr_version_show(struct device *dev,
879                           struct device_attribute *attr, char *buf)
880 {
881         return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
882 }
883
884 static ssize_t
885 qla2x00_fw_version_show(struct device *dev,
886                         struct device_attribute *attr, char *buf)
887 {
888         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
889         struct qla_hw_data *ha = vha->hw;
890         char fw_str[128];
891
892         return snprintf(buf, PAGE_SIZE, "%s\n",
893             ha->isp_ops->fw_version_str(vha, fw_str));
894 }
895
896 static ssize_t
897 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
898                         char *buf)
899 {
900         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
901         struct qla_hw_data *ha = vha->hw;
902         uint32_t sn;
903
904         if (IS_FWI2_CAPABLE(ha)) {
905                 qla2xxx_get_vpd_field(vha, "SN", buf, PAGE_SIZE);
906                 return snprintf(buf, PAGE_SIZE, "%s\n", buf);
907         }
908
909         sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
910         return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
911             sn % 100000);
912 }
913
914 static ssize_t
915 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
916                       char *buf)
917 {
918         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
919         return snprintf(buf, PAGE_SIZE, "ISP%04X\n", vha->hw->pdev->device);
920 }
921
922 static ssize_t
923 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
924                     char *buf)
925 {
926         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
927         struct qla_hw_data *ha = vha->hw;
928         return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
929             ha->product_id[0], ha->product_id[1], ha->product_id[2],
930             ha->product_id[3]);
931 }
932
933 static ssize_t
934 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
935                         char *buf)
936 {
937         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
938         return snprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_number);
939 }
940
941 static ssize_t
942 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
943                         char *buf)
944 {
945         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
946         return snprintf(buf, PAGE_SIZE, "%s\n",
947             vha->hw->model_desc ? vha->hw->model_desc : "");
948 }
949
950 static ssize_t
951 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
952                       char *buf)
953 {
954         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
955         char pci_info[30];
956
957         return snprintf(buf, PAGE_SIZE, "%s\n",
958             vha->hw->isp_ops->pci_info_str(vha, pci_info));
959 }
960
961 static ssize_t
962 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
963                         char *buf)
964 {
965         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
966         struct qla_hw_data *ha = vha->hw;
967         int len = 0;
968
969         if (atomic_read(&vha->loop_state) == LOOP_DOWN ||
970             atomic_read(&vha->loop_state) == LOOP_DEAD)
971                 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
972         else if (atomic_read(&vha->loop_state) != LOOP_READY ||
973             test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
974             test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
975                 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
976         else {
977                 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
978
979                 switch (ha->current_topology) {
980                 case ISP_CFG_NL:
981                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
982                         break;
983                 case ISP_CFG_FL:
984                         len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
985                         break;
986                 case ISP_CFG_N:
987                         len += snprintf(buf + len, PAGE_SIZE-len,
988                             "N_Port to N_Port\n");
989                         break;
990                 case ISP_CFG_F:
991                         len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
992                         break;
993                 default:
994                         len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
995                         break;
996                 }
997         }
998         return len;
999 }
1000
1001 static ssize_t
1002 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
1003                  char *buf)
1004 {
1005         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1006         int len = 0;
1007
1008         switch (vha->hw->zio_mode) {
1009         case QLA_ZIO_MODE_6:
1010                 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
1011                 break;
1012         case QLA_ZIO_DISABLED:
1013                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1014                 break;
1015         }
1016         return len;
1017 }
1018
1019 static ssize_t
1020 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
1021                   const char *buf, size_t count)
1022 {
1023         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1024         struct qla_hw_data *ha = vha->hw;
1025         int val = 0;
1026         uint16_t zio_mode;
1027
1028         if (!IS_ZIO_SUPPORTED(ha))
1029                 return -ENOTSUPP;
1030
1031         if (sscanf(buf, "%d", &val) != 1)
1032                 return -EINVAL;
1033
1034         if (val)
1035                 zio_mode = QLA_ZIO_MODE_6;
1036         else
1037                 zio_mode = QLA_ZIO_DISABLED;
1038
1039         /* Update per-hba values and queue a reset. */
1040         if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
1041                 ha->zio_mode = zio_mode;
1042                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1043         }
1044         return strlen(buf);
1045 }
1046
1047 static ssize_t
1048 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
1049                        char *buf)
1050 {
1051         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1052
1053         return snprintf(buf, PAGE_SIZE, "%d us\n", vha->hw->zio_timer * 100);
1054 }
1055
1056 static ssize_t
1057 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
1058                         const char *buf, size_t count)
1059 {
1060         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1061         int val = 0;
1062         uint16_t zio_timer;
1063
1064         if (sscanf(buf, "%d", &val) != 1)
1065                 return -EINVAL;
1066         if (val > 25500 || val < 100)
1067                 return -ERANGE;
1068
1069         zio_timer = (uint16_t)(val / 100);
1070         vha->hw->zio_timer = zio_timer;
1071
1072         return strlen(buf);
1073 }
1074
1075 static ssize_t
1076 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
1077                     char *buf)
1078 {
1079         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1080         int len = 0;
1081
1082         if (vha->hw->beacon_blink_led)
1083                 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
1084         else
1085                 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1086         return len;
1087 }
1088
1089 static ssize_t
1090 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
1091                      const char *buf, size_t count)
1092 {
1093         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1094         struct qla_hw_data *ha = vha->hw;
1095         int val = 0;
1096         int rval;
1097
1098         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1099                 return -EPERM;
1100
1101         if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
1102                 qla_printk(KERN_WARNING, ha,
1103                     "Abort ISP active -- ignoring beacon request.\n");
1104                 return -EBUSY;
1105         }
1106
1107         if (sscanf(buf, "%d", &val) != 1)
1108                 return -EINVAL;
1109
1110         if (val)
1111                 rval = ha->isp_ops->beacon_on(vha);
1112         else
1113                 rval = ha->isp_ops->beacon_off(vha);
1114
1115         if (rval != QLA_SUCCESS)
1116                 count = 0;
1117
1118         return count;
1119 }
1120
1121 static ssize_t
1122 qla2x00_optrom_bios_version_show(struct device *dev,
1123                                  struct device_attribute *attr, char *buf)
1124 {
1125         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1126         struct qla_hw_data *ha = vha->hw;
1127         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
1128             ha->bios_revision[0]);
1129 }
1130
1131 static ssize_t
1132 qla2x00_optrom_efi_version_show(struct device *dev,
1133                                 struct device_attribute *attr, char *buf)
1134 {
1135         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1136         struct qla_hw_data *ha = vha->hw;
1137         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
1138             ha->efi_revision[0]);
1139 }
1140
1141 static ssize_t
1142 qla2x00_optrom_fcode_version_show(struct device *dev,
1143                                   struct device_attribute *attr, char *buf)
1144 {
1145         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1146         struct qla_hw_data *ha = vha->hw;
1147         return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
1148             ha->fcode_revision[0]);
1149 }
1150
1151 static ssize_t
1152 qla2x00_optrom_fw_version_show(struct device *dev,
1153                                struct device_attribute *attr, char *buf)
1154 {
1155         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1156         struct qla_hw_data *ha = vha->hw;
1157         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
1158             ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
1159             ha->fw_revision[3]);
1160 }
1161
1162 static ssize_t
1163 qla2x00_total_isp_aborts_show(struct device *dev,
1164                               struct device_attribute *attr, char *buf)
1165 {
1166         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1167         struct qla_hw_data *ha = vha->hw;
1168         return snprintf(buf, PAGE_SIZE, "%d\n",
1169             ha->qla_stats.total_isp_aborts);
1170 }
1171
1172 static ssize_t
1173 qla24xx_84xx_fw_version_show(struct device *dev,
1174         struct device_attribute *attr, char *buf)
1175 {
1176         int rval = QLA_SUCCESS;
1177         uint16_t status[2] = {0, 0};
1178         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1179         struct qla_hw_data *ha = vha->hw;
1180
1181         if (IS_QLA84XX(ha) && ha->cs84xx) {
1182                 if (ha->cs84xx->op_fw_version == 0) {
1183                         rval = qla84xx_verify_chip(vha, status);
1184         }
1185
1186         if ((rval == QLA_SUCCESS) && (status[0] == 0))
1187                 return snprintf(buf, PAGE_SIZE, "%u\n",
1188                         (uint32_t)ha->cs84xx->op_fw_version);
1189         }
1190
1191         return snprintf(buf, PAGE_SIZE, "\n");
1192 }
1193
1194 static ssize_t
1195 qla2x00_mpi_version_show(struct device *dev, struct device_attribute *attr,
1196     char *buf)
1197 {
1198         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1199         struct qla_hw_data *ha = vha->hw;
1200
1201         if (!IS_QLA81XX(ha))
1202                 return snprintf(buf, PAGE_SIZE, "\n");
1203
1204         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
1205             ha->mpi_version[0], ha->mpi_version[1], ha->mpi_version[2],
1206             ha->mpi_capabilities);
1207 }
1208
1209 static ssize_t
1210 qla2x00_phy_version_show(struct device *dev, struct device_attribute *attr,
1211     char *buf)
1212 {
1213         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1214         struct qla_hw_data *ha = vha->hw;
1215
1216         if (!IS_QLA81XX(ha))
1217                 return snprintf(buf, PAGE_SIZE, "\n");
1218
1219         return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1220             ha->phy_version[0], ha->phy_version[1], ha->phy_version[2]);
1221 }
1222
1223 static ssize_t
1224 qla2x00_flash_block_size_show(struct device *dev,
1225                               struct device_attribute *attr, char *buf)
1226 {
1227         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1228         struct qla_hw_data *ha = vha->hw;
1229
1230         return snprintf(buf, PAGE_SIZE, "0x%x\n", ha->fdt_block_size);
1231 }
1232
1233 static ssize_t
1234 qla2x00_vlan_id_show(struct device *dev, struct device_attribute *attr,
1235     char *buf)
1236 {
1237         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1238
1239         if (!IS_QLA81XX(vha->hw))
1240                 return snprintf(buf, PAGE_SIZE, "\n");
1241
1242         return snprintf(buf, PAGE_SIZE, "%d\n", vha->fcoe_vlan_id);
1243 }
1244
1245 static ssize_t
1246 qla2x00_vn_port_mac_address_show(struct device *dev,
1247     struct device_attribute *attr, char *buf)
1248 {
1249         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1250
1251         if (!IS_QLA81XX(vha->hw))
1252                 return snprintf(buf, PAGE_SIZE, "\n");
1253
1254         return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x\n",
1255             vha->fcoe_vn_port_mac[5], vha->fcoe_vn_port_mac[4],
1256             vha->fcoe_vn_port_mac[3], vha->fcoe_vn_port_mac[2],
1257             vha->fcoe_vn_port_mac[1], vha->fcoe_vn_port_mac[0]);
1258 }
1259
1260 static ssize_t
1261 qla2x00_fabric_param_show(struct device *dev, struct device_attribute *attr,
1262     char *buf)
1263 {
1264         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1265
1266         return snprintf(buf, PAGE_SIZE, "%d\n", vha->hw->switch_cap);
1267 }
1268
1269 static ssize_t
1270 qla2x00_fw_state_show(struct device *dev, struct device_attribute *attr,
1271     char *buf)
1272 {
1273         scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1274         int rval = QLA_FUNCTION_FAILED;
1275         uint16_t state[5];
1276
1277         if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1278                 test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
1279                 DEBUG2_3_11(printk("%s(%ld): isp reset in progress.\n",
1280                         __func__, vha->host_no));
1281         else if (!vha->hw->flags.eeh_busy)
1282                 rval = qla2x00_get_firmware_state(vha, state);
1283         if (rval != QLA_SUCCESS)
1284                 memset(state, -1, sizeof(state));
1285
1286         return snprintf(buf, PAGE_SIZE, "0x%x 0x%x 0x%x 0x%x 0x%x\n", state[0],
1287             state[1], state[2], state[3], state[4]);
1288 }
1289
1290 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show, NULL);
1291 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
1292 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
1293 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
1294 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
1295 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
1296 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
1297 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
1298 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
1299 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
1300 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
1301                    qla2x00_zio_timer_store);
1302 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
1303                    qla2x00_beacon_store);
1304 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
1305                    qla2x00_optrom_bios_version_show, NULL);
1306 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
1307                    qla2x00_optrom_efi_version_show, NULL);
1308 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
1309                    qla2x00_optrom_fcode_version_show, NULL);
1310 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
1311                    NULL);
1312 static DEVICE_ATTR(84xx_fw_version, S_IRUGO, qla24xx_84xx_fw_version_show,
1313                    NULL);
1314 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
1315                    NULL);
1316 static DEVICE_ATTR(mpi_version, S_IRUGO, qla2x00_mpi_version_show, NULL);
1317 static DEVICE_ATTR(phy_version, S_IRUGO, qla2x00_phy_version_show, NULL);
1318 static DEVICE_ATTR(flash_block_size, S_IRUGO, qla2x00_flash_block_size_show,
1319                    NULL);
1320 static DEVICE_ATTR(vlan_id, S_IRUGO, qla2x00_vlan_id_show, NULL);
1321 static DEVICE_ATTR(vn_port_mac_address, S_IRUGO,
1322                    qla2x00_vn_port_mac_address_show, NULL);
1323 static DEVICE_ATTR(fabric_param, S_IRUGO, qla2x00_fabric_param_show, NULL);
1324 static DEVICE_ATTR(fw_state, S_IRUGO, qla2x00_fw_state_show, NULL);
1325
1326 struct device_attribute *qla2x00_host_attrs[] = {
1327         &dev_attr_driver_version,
1328         &dev_attr_fw_version,
1329         &dev_attr_serial_num,
1330         &dev_attr_isp_name,
1331         &dev_attr_isp_id,
1332         &dev_attr_model_name,
1333         &dev_attr_model_desc,
1334         &dev_attr_pci_info,
1335         &dev_attr_link_state,
1336         &dev_attr_zio,
1337         &dev_attr_zio_timer,
1338         &dev_attr_beacon,
1339         &dev_attr_optrom_bios_version,
1340         &dev_attr_optrom_efi_version,
1341         &dev_attr_optrom_fcode_version,
1342         &dev_attr_optrom_fw_version,
1343         &dev_attr_84xx_fw_version,
1344         &dev_attr_total_isp_aborts,
1345         &dev_attr_mpi_version,
1346         &dev_attr_phy_version,
1347         &dev_attr_flash_block_size,
1348         &dev_attr_vlan_id,
1349         &dev_attr_vn_port_mac_address,
1350         &dev_attr_fabric_param,
1351         &dev_attr_fw_state,
1352         NULL,
1353 };
1354
1355 /* Host attributes. */
1356
1357 static void
1358 qla2x00_get_host_port_id(struct Scsi_Host *shost)
1359 {
1360         scsi_qla_host_t *vha = shost_priv(shost);
1361
1362         fc_host_port_id(shost) = vha->d_id.b.domain << 16 |
1363             vha->d_id.b.area << 8 | vha->d_id.b.al_pa;
1364 }
1365
1366 static void
1367 qla2x00_get_host_speed(struct Scsi_Host *shost)
1368 {
1369         struct qla_hw_data *ha = ((struct scsi_qla_host *)
1370                                         (shost_priv(shost)))->hw;
1371         u32 speed = FC_PORTSPEED_UNKNOWN;
1372
1373         switch (ha->link_data_rate) {
1374         case PORT_SPEED_1GB:
1375                 speed = FC_PORTSPEED_1GBIT;
1376                 break;
1377         case PORT_SPEED_2GB:
1378                 speed = FC_PORTSPEED_2GBIT;
1379                 break;
1380         case PORT_SPEED_4GB:
1381                 speed = FC_PORTSPEED_4GBIT;
1382                 break;
1383         case PORT_SPEED_8GB:
1384                 speed = FC_PORTSPEED_8GBIT;
1385                 break;
1386         case PORT_SPEED_10GB:
1387                 speed = FC_PORTSPEED_10GBIT;
1388                 break;
1389         }
1390         fc_host_speed(shost) = speed;
1391 }
1392
1393 static void
1394 qla2x00_get_host_port_type(struct Scsi_Host *shost)
1395 {
1396         scsi_qla_host_t *vha = shost_priv(shost);
1397         uint32_t port_type = FC_PORTTYPE_UNKNOWN;
1398
1399         if (vha->vp_idx) {
1400                 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
1401                 return;
1402         }
1403         switch (vha->hw->current_topology) {
1404         case ISP_CFG_NL:
1405                 port_type = FC_PORTTYPE_LPORT;
1406                 break;
1407         case ISP_CFG_FL:
1408                 port_type = FC_PORTTYPE_NLPORT;
1409                 break;
1410         case ISP_CFG_N:
1411                 port_type = FC_PORTTYPE_PTP;
1412                 break;
1413         case ISP_CFG_F:
1414                 port_type = FC_PORTTYPE_NPORT;
1415                 break;
1416         }
1417         fc_host_port_type(shost) = port_type;
1418 }
1419
1420 static void
1421 qla2x00_get_starget_node_name(struct scsi_target *starget)
1422 {
1423         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1424         scsi_qla_host_t *vha = shost_priv(host);
1425         fc_port_t *fcport;
1426         u64 node_name = 0;
1427
1428         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1429                 if (fcport->rport &&
1430                     starget->id == fcport->rport->scsi_target_id) {
1431                         node_name = wwn_to_u64(fcport->node_name);
1432                         break;
1433                 }
1434         }
1435
1436         fc_starget_node_name(starget) = node_name;
1437 }
1438
1439 static void
1440 qla2x00_get_starget_port_name(struct scsi_target *starget)
1441 {
1442         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1443         scsi_qla_host_t *vha = shost_priv(host);
1444         fc_port_t *fcport;
1445         u64 port_name = 0;
1446
1447         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1448                 if (fcport->rport &&
1449                     starget->id == fcport->rport->scsi_target_id) {
1450                         port_name = wwn_to_u64(fcport->port_name);
1451                         break;
1452                 }
1453         }
1454
1455         fc_starget_port_name(starget) = port_name;
1456 }
1457
1458 static void
1459 qla2x00_get_starget_port_id(struct scsi_target *starget)
1460 {
1461         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1462         scsi_qla_host_t *vha = shost_priv(host);
1463         fc_port_t *fcport;
1464         uint32_t port_id = ~0U;
1465
1466         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1467                 if (fcport->rport &&
1468                     starget->id == fcport->rport->scsi_target_id) {
1469                         port_id = fcport->d_id.b.domain << 16 |
1470                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
1471                         break;
1472                 }
1473         }
1474
1475         fc_starget_port_id(starget) = port_id;
1476 }
1477
1478 static void
1479 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1480 {
1481         if (timeout)
1482                 rport->dev_loss_tmo = timeout;
1483         else
1484                 rport->dev_loss_tmo = 1;
1485 }
1486
1487 static void
1488 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
1489 {
1490         struct Scsi_Host *host = rport_to_shost(rport);
1491         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1492
1493         if (!fcport)
1494                 return;
1495
1496         if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
1497                 return;
1498
1499         if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
1500                 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
1501                 return;
1502         }
1503
1504         /*
1505          * Transport has effectively 'deleted' the rport, clear
1506          * all local references.
1507          */
1508         spin_lock_irq(host->host_lock);
1509         fcport->rport = NULL;
1510         *((fc_port_t **)rport->dd_data) = NULL;
1511         spin_unlock_irq(host->host_lock);
1512 }
1513
1514 static void
1515 qla2x00_terminate_rport_io(struct fc_rport *rport)
1516 {
1517         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1518
1519         if (!fcport)
1520                 return;
1521
1522         if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
1523                 return;
1524
1525         if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
1526                 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
1527                 return;
1528         }
1529         /*
1530          * At this point all fcport's software-states are cleared.  Perform any
1531          * final cleanup of firmware resources (PCBs and XCBs).
1532          */
1533         if (fcport->loop_id != FC_NO_LOOP_ID &&
1534             !test_bit(UNLOADING, &fcport->vha->dpc_flags))
1535                 fcport->vha->hw->isp_ops->fabric_logout(fcport->vha,
1536                         fcport->loop_id, fcport->d_id.b.domain,
1537                         fcport->d_id.b.area, fcport->d_id.b.al_pa);
1538 }
1539
1540 static int
1541 qla2x00_issue_lip(struct Scsi_Host *shost)
1542 {
1543         scsi_qla_host_t *vha = shost_priv(shost);
1544
1545         qla2x00_loop_reset(vha);
1546         return 0;
1547 }
1548
1549 static struct fc_host_statistics *
1550 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
1551 {
1552         scsi_qla_host_t *vha = shost_priv(shost);
1553         struct qla_hw_data *ha = vha->hw;
1554         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
1555         int rval;
1556         struct link_statistics *stats;
1557         dma_addr_t stats_dma;
1558         struct fc_host_statistics *pfc_host_stat;
1559
1560         pfc_host_stat = &ha->fc_host_stat;
1561         memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
1562
1563         if (test_bit(UNLOADING, &vha->dpc_flags))
1564                 goto done;
1565
1566         if (unlikely(pci_channel_offline(ha->pdev)))
1567                 goto done;
1568
1569         stats = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &stats_dma);
1570         if (stats == NULL) {
1571                 DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
1572                     __func__, base_vha->host_no));
1573                 goto done;
1574         }
1575         memset(stats, 0, DMA_POOL_SIZE);
1576
1577         rval = QLA_FUNCTION_FAILED;
1578         if (IS_FWI2_CAPABLE(ha)) {
1579                 rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma);
1580         } else if (atomic_read(&base_vha->loop_state) == LOOP_READY &&
1581                     !test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) &&
1582                     !test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
1583                     !ha->dpc_active) {
1584                 /* Must be in a 'READY' state for statistics retrieval. */
1585                 rval = qla2x00_get_link_status(base_vha, base_vha->loop_id,
1586                                                 stats, stats_dma);
1587         }
1588
1589         if (rval != QLA_SUCCESS)
1590                 goto done_free;
1591
1592         pfc_host_stat->link_failure_count = stats->link_fail_cnt;
1593         pfc_host_stat->loss_of_sync_count = stats->loss_sync_cnt;
1594         pfc_host_stat->loss_of_signal_count = stats->loss_sig_cnt;
1595         pfc_host_stat->prim_seq_protocol_err_count = stats->prim_seq_err_cnt;
1596         pfc_host_stat->invalid_tx_word_count = stats->inval_xmit_word_cnt;
1597         pfc_host_stat->invalid_crc_count = stats->inval_crc_cnt;
1598         if (IS_FWI2_CAPABLE(ha)) {
1599                 pfc_host_stat->lip_count = stats->lip_cnt;
1600                 pfc_host_stat->tx_frames = stats->tx_frames;
1601                 pfc_host_stat->rx_frames = stats->rx_frames;
1602                 pfc_host_stat->dumped_frames = stats->dumped_frames;
1603                 pfc_host_stat->nos_count = stats->nos_rcvd;
1604         }
1605         pfc_host_stat->fcp_input_megabytes = ha->qla_stats.input_bytes >> 20;
1606         pfc_host_stat->fcp_output_megabytes = ha->qla_stats.output_bytes >> 20;
1607
1608 done_free:
1609         dma_pool_free(ha->s_dma_pool, stats, stats_dma);
1610 done:
1611         return pfc_host_stat;
1612 }
1613
1614 static void
1615 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
1616 {
1617         scsi_qla_host_t *vha = shost_priv(shost);
1618
1619         qla2x00_get_sym_node_name(vha, fc_host_symbolic_name(shost));
1620 }
1621
1622 static void
1623 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
1624 {
1625         scsi_qla_host_t *vha = shost_priv(shost);
1626
1627         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
1628 }
1629
1630 static void
1631 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
1632 {
1633         scsi_qla_host_t *vha = shost_priv(shost);
1634         u64 node_name;
1635
1636         if (vha->device_flags & SWITCH_FOUND)
1637                 node_name = wwn_to_u64(vha->fabric_node_name);
1638         else
1639                 node_name = wwn_to_u64(vha->node_name);
1640
1641         fc_host_fabric_name(shost) = node_name;
1642 }
1643
1644 static void
1645 qla2x00_get_host_port_state(struct Scsi_Host *shost)
1646 {
1647         scsi_qla_host_t *vha = shost_priv(shost);
1648         struct scsi_qla_host *base_vha = pci_get_drvdata(vha->hw->pdev);
1649
1650         if (!base_vha->flags.online)
1651                 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1652         else if (atomic_read(&base_vha->loop_state) == LOOP_TIMEOUT)
1653                 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1654         else
1655                 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1656 }
1657
1658 static int
1659 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
1660 {
1661         int     ret = 0;
1662         uint8_t qos = 0;
1663         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
1664         scsi_qla_host_t *vha = NULL;
1665         struct qla_hw_data *ha = base_vha->hw;
1666         uint16_t options = 0;
1667         int     cnt;
1668         struct req_que *req = ha->req_q_map[0];
1669
1670         ret = qla24xx_vport_create_req_sanity_check(fc_vport);
1671         if (ret) {
1672                 DEBUG15(printk("qla24xx_vport_create_req_sanity_check failed, "
1673                     "status %x\n", ret));
1674                 return (ret);
1675         }
1676
1677         vha = qla24xx_create_vhost(fc_vport);
1678         if (vha == NULL) {
1679                 DEBUG15(printk ("qla24xx_create_vhost failed, vha = %p\n",
1680                     vha));
1681                 return FC_VPORT_FAILED;
1682         }
1683         if (disable) {
1684                 atomic_set(&vha->vp_state, VP_OFFLINE);
1685                 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
1686         } else
1687                 atomic_set(&vha->vp_state, VP_FAILED);
1688
1689         /* ready to create vport */
1690         qla_printk(KERN_INFO, vha->hw, "VP entry id %d assigned.\n",
1691                                                         vha->vp_idx);
1692
1693         /* initialized vport states */
1694         atomic_set(&vha->loop_state, LOOP_DOWN);
1695         vha->vp_err_state=  VP_ERR_PORTDWN;
1696         vha->vp_prev_err_state=  VP_ERR_UNKWN;
1697         /* Check if physical ha port is Up */
1698         if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
1699             atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
1700                 /* Don't retry or attempt login of this virtual port */
1701                 DEBUG15(printk ("scsi(%ld): pport loop_state is not UP.\n",
1702                     base_vha->host_no));
1703                 atomic_set(&vha->loop_state, LOOP_DEAD);
1704                 if (!disable)
1705                         fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
1706         }
1707
1708         if (scsi_add_host_with_dma(vha->host, &fc_vport->dev,
1709                                    &ha->pdev->dev)) {
1710                 DEBUG15(printk("scsi(%ld): scsi_add_host failure for VP[%d].\n",
1711                         vha->host_no, vha->vp_idx));
1712                 goto vport_create_failed_2;
1713         }
1714
1715         /* initialize attributes */
1716         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1717         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1718         fc_host_supported_classes(vha->host) =
1719                 fc_host_supported_classes(base_vha->host);
1720         fc_host_supported_speeds(vha->host) =
1721                 fc_host_supported_speeds(base_vha->host);
1722
1723         qla24xx_vport_disable(fc_vport, disable);
1724
1725         if (ha->flags.cpu_affinity_enabled) {
1726                 req = ha->req_q_map[1];
1727                 goto vport_queue;
1728         } else if (ql2xmaxqueues == 1 || !ha->npiv_info)
1729                 goto vport_queue;
1730         /* Create a request queue in QoS mode for the vport */
1731         for (cnt = 0; cnt < ha->nvram_npiv_size; cnt++) {
1732                 if (memcmp(ha->npiv_info[cnt].port_name, vha->port_name, 8) == 0
1733                         && memcmp(ha->npiv_info[cnt].node_name, vha->node_name,
1734                                         8) == 0) {
1735                         qos = ha->npiv_info[cnt].q_qos;
1736                         break;
1737                 }
1738         }
1739         if (qos) {
1740                 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, 0,
1741                         qos);
1742                 if (!ret)
1743                         qla_printk(KERN_WARNING, ha,
1744                         "Can't create request queue for vp_idx:%d\n",
1745                         vha->vp_idx);
1746                 else {
1747                         DEBUG2(qla_printk(KERN_INFO, ha,
1748                         "Request Que:%d (QoS: %d) created for vp_idx:%d\n",
1749                         ret, qos, vha->vp_idx));
1750                         req = ha->req_q_map[ret];
1751                 }
1752         }
1753
1754 vport_queue:
1755         vha->req = req;
1756         return 0;
1757
1758 vport_create_failed_2:
1759         qla24xx_disable_vp(vha);
1760         qla24xx_deallocate_vp_id(vha);
1761         scsi_host_put(vha->host);
1762         return FC_VPORT_FAILED;
1763 }
1764
1765 static int
1766 qla24xx_vport_delete(struct fc_vport *fc_vport)
1767 {
1768         scsi_qla_host_t *vha = fc_vport->dd_data;
1769         fc_port_t *fcport, *tfcport;
1770         struct qla_hw_data *ha = vha->hw;
1771         uint16_t id = vha->vp_idx;
1772
1773         while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
1774             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags))
1775                 msleep(1000);
1776
1777         qla24xx_disable_vp(vha);
1778
1779         fc_remove_host(vha->host);
1780
1781         scsi_remove_host(vha->host);
1782
1783         list_for_each_entry_safe(fcport, tfcport, &vha->vp_fcports, list) {
1784                 list_del(&fcport->list);
1785                 kfree(fcport);
1786                 fcport = NULL;
1787         }
1788
1789         qla24xx_deallocate_vp_id(vha);
1790
1791         mutex_lock(&ha->vport_lock);
1792         ha->cur_vport_count--;
1793         clear_bit(vha->vp_idx, ha->vp_idx_map);
1794         mutex_unlock(&ha->vport_lock);
1795
1796         if (vha->timer_active) {
1797                 qla2x00_vp_stop_timer(vha);
1798                 DEBUG15(printk ("scsi(%ld): timer for the vport[%d] = %p "
1799                     "has stopped\n",
1800                     vha->host_no, vha->vp_idx, vha));
1801         }
1802
1803         if (vha->req->id && !ha->flags.cpu_affinity_enabled) {
1804                 if (qla25xx_delete_req_que(vha, vha->req) != QLA_SUCCESS)
1805                         qla_printk(KERN_WARNING, ha,
1806                                 "Queue delete failed.\n");
1807         }
1808
1809         scsi_host_put(vha->host);
1810         qla_printk(KERN_INFO, ha, "vport %d deleted\n", id);
1811         return 0;
1812 }
1813
1814 static int
1815 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
1816 {
1817         scsi_qla_host_t *vha = fc_vport->dd_data;
1818
1819         if (disable)
1820                 qla24xx_disable_vp(vha);
1821         else
1822                 qla24xx_enable_vp(vha);
1823
1824         return 0;
1825 }
1826
1827 /* BSG support for ELS/CT pass through */
1828 inline srb_t *
1829 qla2x00_get_ctx_bsg_sp(scsi_qla_host_t *vha, fc_port_t *fcport, size_t size)
1830 {
1831         srb_t *sp;
1832         struct qla_hw_data *ha = vha->hw;
1833         struct srb_bsg_ctx *ctx;
1834
1835         sp = mempool_alloc(ha->srb_mempool, GFP_KERNEL);
1836         if (!sp)
1837                 goto done;
1838         ctx = kzalloc(size, GFP_KERNEL);
1839         if (!ctx) {
1840                 mempool_free(sp, ha->srb_mempool);
1841                 goto done;
1842         }
1843
1844         memset(sp, 0, sizeof(*sp));
1845         sp->fcport = fcport;
1846         sp->ctx = ctx;
1847 done:
1848         return sp;
1849 }
1850
1851 static int
1852 qla2x00_process_els(struct fc_bsg_job *bsg_job)
1853 {
1854         struct fc_rport *rport;
1855         fc_port_t *fcport;
1856         struct Scsi_Host *host;
1857         scsi_qla_host_t *vha;
1858         struct qla_hw_data *ha;
1859         srb_t *sp;
1860         const char *type;
1861         int req_sg_cnt, rsp_sg_cnt;
1862         int rval =  (DRIVER_ERROR << 16);
1863         uint16_t nextlid = 0;
1864         struct srb_bsg *els;
1865
1866         /*  Multiple SG's are not supported for ELS requests */
1867         if (bsg_job->request_payload.sg_cnt > 1 ||
1868                 bsg_job->reply_payload.sg_cnt > 1) {
1869                 DEBUG2(printk(KERN_INFO
1870                     "multiple SG's are not supported for ELS requests"
1871                     " [request_sg_cnt: %x reply_sg_cnt: %x]\n",
1872                     bsg_job->request_payload.sg_cnt,
1873                     bsg_job->reply_payload.sg_cnt));
1874                 rval = -EPERM;
1875                 goto done;
1876         }
1877
1878         /* ELS request for rport */
1879         if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
1880                 rport = bsg_job->rport;
1881                 fcport = *(fc_port_t **) rport->dd_data;
1882                 host = rport_to_shost(rport);
1883                 vha = shost_priv(host);
1884                 ha = vha->hw;
1885                 type = "FC_BSG_RPT_ELS";
1886
1887                 /* make sure the rport is logged in,
1888                  * if not perform fabric login
1889                  */
1890                 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
1891                         DEBUG2(qla_printk(KERN_WARNING, ha,
1892                             "failed to login port %06X for ELS passthru\n",
1893                             fcport->d_id.b24));
1894                         rval = -EIO;
1895                         goto done;
1896                 }
1897         } else {
1898                 host = bsg_job->shost;
1899                 vha = shost_priv(host);
1900                 ha = vha->hw;
1901                 type = "FC_BSG_HST_ELS_NOLOGIN";
1902
1903                 /* Allocate a dummy fcport structure, since functions
1904                  * preparing the IOCB and mailbox command retrieves port
1905                  * specific information from fcport structure. For Host based
1906                  * ELS commands there will be no fcport structure allocated
1907                  */
1908                 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1909                 if (!fcport) {
1910                         rval = -ENOMEM;
1911                         goto done;
1912                 }
1913
1914                 /* Initialize all required  fields of fcport */
1915                 fcport->vha = vha;
1916                 fcport->vp_idx = vha->vp_idx;
1917                 fcport->d_id.b.al_pa =
1918                     bsg_job->request->rqst_data.h_els.port_id[0];
1919                 fcport->d_id.b.area =
1920                     bsg_job->request->rqst_data.h_els.port_id[1];
1921                 fcport->d_id.b.domain =
1922                     bsg_job->request->rqst_data.h_els.port_id[2];
1923                 fcport->loop_id =
1924                     (fcport->d_id.b.al_pa == 0xFD) ?
1925                     NPH_FABRIC_CONTROLLER : NPH_F_PORT;
1926         }
1927
1928         if (!vha->flags.online) {
1929                 DEBUG2(qla_printk(KERN_WARNING, ha,
1930                     "host not online\n"));
1931                 rval = -EIO;
1932                 goto done;
1933         }
1934
1935         req_sg_cnt =
1936             dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1937             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1938         if (!req_sg_cnt) {
1939                 rval = -ENOMEM;
1940                 goto done_free_fcport;
1941         }
1942         rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1943             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1944         if (!rsp_sg_cnt) {
1945                 rval = -ENOMEM;
1946                 goto done_free_fcport;
1947         }
1948
1949         if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
1950             (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt))
1951         {
1952                 DEBUG2(printk(KERN_INFO
1953                     "dma mapping resulted in different sg counts \
1954                     [request_sg_cnt: %x dma_request_sg_cnt: %x\
1955                     reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
1956                     bsg_job->request_payload.sg_cnt, req_sg_cnt,
1957                     bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
1958                 rval = -EAGAIN;
1959                 goto done_unmap_sg;
1960         }
1961
1962         /* Alloc SRB structure */
1963         sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_bsg));
1964         if (!sp) {
1965                 rval = -ENOMEM;
1966                 goto done_unmap_sg;
1967         }
1968
1969         els = sp->ctx;
1970         els->ctx.type =
1971             (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
1972             SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
1973         els->bsg_job = bsg_job;
1974
1975         DEBUG2(qla_printk(KERN_INFO, ha,
1976             "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
1977             "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
1978             bsg_job->request->rqst_data.h_els.command_code,
1979             fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
1980             fcport->d_id.b.al_pa));
1981
1982         rval = qla2x00_start_sp(sp);
1983         if (rval != QLA_SUCCESS) {
1984                 kfree(sp->ctx);
1985                 mempool_free(sp, ha->srb_mempool);
1986                 rval = -EIO;
1987                 goto done_unmap_sg;
1988         }
1989         return rval;
1990
1991 done_unmap_sg:
1992         dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1993                 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1994         dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1995                 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1996         goto done_free_fcport;
1997
1998 done_free_fcport:
1999         if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
2000                 kfree(fcport);
2001 done:
2002         return rval;
2003 }
2004
2005 static int
2006 qla2x00_process_ct(struct fc_bsg_job *bsg_job)
2007 {
2008         srb_t *sp;
2009         struct Scsi_Host *host = bsg_job->shost;
2010         scsi_qla_host_t *vha = shost_priv(host);
2011         struct qla_hw_data *ha = vha->hw;
2012         int rval = (DRIVER_ERROR << 16);
2013         int req_sg_cnt, rsp_sg_cnt;
2014         uint16_t loop_id;
2015         struct fc_port *fcport;
2016         char  *type = "FC_BSG_HST_CT";
2017         struct srb_bsg *ct;
2018
2019         /* pass through is supported only for ISP 4Gb or higher */
2020         if (!IS_FWI2_CAPABLE(ha)) {
2021                 DEBUG2(qla_printk(KERN_INFO, ha,
2022                     "scsi(%ld):Firmware is not capable to support FC "
2023                     "CT pass thru\n", vha->host_no));
2024                 rval = -EPERM;
2025                 goto done;
2026         }
2027
2028         req_sg_cnt =
2029             dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
2030             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2031         if (!req_sg_cnt) {
2032                 rval = -ENOMEM;
2033                 goto done;
2034         }
2035
2036         rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
2037             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2038         if (!rsp_sg_cnt) {
2039                 rval = -ENOMEM;
2040                 goto done;
2041         }
2042
2043         if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
2044                 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt))
2045         {
2046                 DEBUG2(qla_printk(KERN_WARNING, ha,
2047                     "dma mapping resulted in different sg counts \
2048                     [request_sg_cnt: %x dma_request_sg_cnt: %x\
2049                     reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
2050                     bsg_job->request_payload.sg_cnt, req_sg_cnt,
2051                     bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
2052                 rval = -EAGAIN;
2053                 goto done_unmap_sg;
2054         }
2055
2056         if (!vha->flags.online) {
2057                 DEBUG2(qla_printk(KERN_WARNING, ha,
2058                     "host not online\n"));
2059                 rval = -EIO;
2060                 goto done_unmap_sg;
2061         }
2062
2063         loop_id =
2064             (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
2065             >> 24;
2066         switch (loop_id) {
2067                 case 0xFC:
2068                         loop_id = cpu_to_le16(NPH_SNS);
2069                         break;
2070                 case 0xFA:
2071                         loop_id = vha->mgmt_svr_loop_id;
2072                         break;
2073                 default:
2074                         DEBUG2(qla_printk(KERN_INFO, ha,
2075                             "Unknown loop id: %x\n", loop_id));
2076                         rval = -EINVAL;
2077                         goto done_unmap_sg;
2078         }
2079
2080         /* Allocate a dummy fcport structure, since functions preparing the
2081          * IOCB and mailbox command retrieves port specific information
2082          * from fcport structure. For Host based ELS commands there will be
2083          * no fcport structure allocated
2084          */
2085         fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2086         if (!fcport)
2087         {
2088                 rval = -ENOMEM;
2089                 goto  done_unmap_sg;
2090         }
2091
2092         /* Initialize all required  fields of fcport */
2093         fcport->vha = vha;
2094         fcport->vp_idx = vha->vp_idx;
2095         fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
2096         fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
2097         fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
2098         fcport->loop_id = loop_id;
2099
2100         /* Alloc SRB structure */
2101         sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_bsg));
2102         if (!sp) {
2103                 rval = -ENOMEM;
2104                 goto done_free_fcport;
2105         }
2106
2107         ct = sp->ctx;
2108         ct->ctx.type = SRB_CT_CMD;
2109         ct->bsg_job = bsg_job;
2110
2111         DEBUG2(qla_printk(KERN_INFO, ha,
2112             "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
2113             "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
2114             (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
2115             fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
2116             fcport->d_id.b.al_pa));
2117
2118         rval = qla2x00_start_sp(sp);
2119         if (rval != QLA_SUCCESS) {
2120                 kfree(sp->ctx);
2121                 mempool_free(sp, ha->srb_mempool);
2122                 rval = -EIO;
2123                 goto done_free_fcport;
2124         }
2125         return rval;
2126
2127 done_free_fcport:
2128         kfree(fcport);
2129 done_unmap_sg:
2130         dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
2131             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2132         dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
2133             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2134 done:
2135         return rval;
2136 }
2137
2138 static int
2139 qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
2140 {
2141         struct Scsi_Host *host = bsg_job->shost;
2142         scsi_qla_host_t *vha = shost_priv(host);
2143         struct qla_hw_data *ha = vha->hw;
2144         int rval;
2145         uint8_t command_sent;
2146         uint32_t vendor_cmd;
2147         char *type;
2148         struct msg_echo_lb elreq;
2149         uint16_t response[MAILBOX_REGISTER_COUNT];
2150         uint8_t* fw_sts_ptr;
2151         uint8_t *req_data;
2152         dma_addr_t req_data_dma;
2153         uint32_t req_data_len;
2154         uint8_t *rsp_data;
2155         dma_addr_t rsp_data_dma;
2156         uint32_t rsp_data_len;
2157
2158         if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
2159             test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
2160             test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
2161                 rval = -EBUSY;
2162                 goto done;
2163         }
2164
2165         if (!vha->flags.online) {
2166                 DEBUG2(qla_printk(KERN_WARNING, ha,
2167                     "host not online\n"));
2168                 rval = -EIO;
2169                 goto done;
2170         }
2171
2172         elreq.req_sg_cnt =
2173             dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
2174             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2175         if (!elreq.req_sg_cnt) {
2176                 rval = -ENOMEM;
2177                 goto done;
2178         }
2179         elreq.rsp_sg_cnt =
2180             dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
2181             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2182         if (!elreq.rsp_sg_cnt) {
2183                 rval = -ENOMEM;
2184                 goto done;
2185         }
2186
2187         if ((elreq.req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
2188             (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt))
2189         {
2190                 DEBUG2(printk(KERN_INFO
2191                     "dma mapping resulted in different sg counts \
2192                     [request_sg_cnt: %x dma_request_sg_cnt: %x\
2193                     reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
2194                     bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
2195                     bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt));
2196                 rval = -EAGAIN;
2197                 goto done_unmap_sg;
2198         }
2199         req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
2200         req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
2201             &req_data_dma, GFP_KERNEL);
2202
2203         rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
2204             &rsp_data_dma, GFP_KERNEL);
2205
2206         /* Copy the request buffer in req_data now */
2207         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2208             bsg_job->request_payload.sg_cnt, req_data,
2209             req_data_len);
2210
2211         elreq.send_dma = req_data_dma;
2212         elreq.rcv_dma = rsp_data_dma;
2213         elreq.transfer_size = req_data_len;
2214
2215         /* Vendor cmd : loopback or ECHO diagnostic
2216          * Options:
2217          *      Loopback : Either internal or external loopback
2218          *      ECHO: ECHO ELS or Vendor specific FC4  link data
2219          */
2220         vendor_cmd = bsg_job->request->rqst_data.h_vendor.vendor_cmd[0];
2221         elreq.options =
2222             *(((uint32_t *)bsg_job->request->rqst_data.h_vendor.vendor_cmd)
2223             + 1);
2224
2225         switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
2226         case QL_VND_LOOPBACK:
2227                 if (ha->current_topology != ISP_CFG_F) {
2228                         type = "FC_BSG_HST_VENDOR_LOOPBACK";
2229
2230                         DEBUG2(qla_printk(KERN_INFO, ha,
2231                                 "scsi(%ld) bsg rqst type: %s vendor rqst type: %x options: %x.\n",
2232                                 vha->host_no, type, vendor_cmd, elreq.options));
2233
2234                         command_sent = INT_DEF_LB_LOOPBACK_CMD;
2235                         rval = qla2x00_loopback_test(vha, &elreq, response);
2236                         if (IS_QLA81XX(ha)) {
2237                                 if (response[0] == MBS_COMMAND_ERROR && response[1] == MBS_LB_RESET) {
2238                                         DEBUG2(printk(KERN_ERR "%s(%ld): ABORTing "
2239                                                 "ISP\n", __func__, vha->host_no));
2240                                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2241                                         qla2xxx_wake_dpc(vha);
2242                                  }
2243                         }
2244                 } else {
2245                         type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
2246                         DEBUG2(qla_printk(KERN_INFO, ha,
2247                                 "scsi(%ld) bsg rqst type: %s vendor rqst type: %x options: %x.\n",
2248                                 vha->host_no, type, vendor_cmd, elreq.options));
2249
2250                         command_sent = INT_DEF_LB_ECHO_CMD;
2251                         rval = qla2x00_echo_test(vha, &elreq, response);
2252                 }
2253                 break;
2254         case QLA84_RESET:
2255                 if (!IS_QLA84XX(vha->hw)) {
2256                         rval = -EINVAL;
2257                         DEBUG16(printk(
2258                                 "%s(%ld): 8xxx exiting.\n",
2259                                 __func__, vha->host_no));
2260                         return rval;
2261                 }
2262                 rval = qla84xx_reset(vha, &elreq, bsg_job);
2263                 break;
2264         case QLA84_MGMT_CMD:
2265                 if (!IS_QLA84XX(vha->hw)) {
2266                         rval = -EINVAL;
2267                         DEBUG16(printk(
2268                                 "%s(%ld): 8xxx exiting.\n",
2269                                 __func__, vha->host_no));
2270                         return rval;
2271                 }
2272                 rval = qla84xx_mgmt_cmd(vha, &elreq, bsg_job);
2273                 break;
2274         default:
2275                 rval = -ENOSYS;
2276         }
2277
2278         if (rval != QLA_SUCCESS) {
2279                 DEBUG2(qla_printk(KERN_WARNING, ha,
2280                         "scsi(%ld) Vendor request %s failed\n", vha->host_no, type));
2281                 rval = 0;
2282                 bsg_job->reply->result = (DID_ERROR << 16);
2283                 bsg_job->reply->reply_payload_rcv_len = 0;
2284                 fw_sts_ptr = ((uint8_t*)bsg_job->req->sense) + sizeof(struct fc_bsg_reply);
2285                 memcpy( fw_sts_ptr, response, sizeof(response));
2286                 fw_sts_ptr += sizeof(response);
2287                 *fw_sts_ptr = command_sent;
2288         } else {
2289                 DEBUG2(qla_printk(KERN_WARNING, ha,
2290                         "scsi(%ld) Vendor request %s completed\n", vha->host_no, type));
2291                 rval = bsg_job->reply->result = 0;
2292                 bsg_job->reply_len = sizeof(struct fc_bsg_reply) + sizeof(response) + sizeof(uint8_t);
2293                 bsg_job->reply->reply_payload_rcv_len = bsg_job->reply_payload.payload_len;
2294                 fw_sts_ptr = ((uint8_t*)bsg_job->req->sense) + sizeof(struct fc_bsg_reply);
2295                 memcpy(fw_sts_ptr, response, sizeof(response));
2296                 fw_sts_ptr += sizeof(response);
2297                 *fw_sts_ptr = command_sent;
2298                 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2299                 bsg_job->reply_payload.sg_cnt, rsp_data,
2300                 rsp_data_len);
2301         }
2302         bsg_job->job_done(bsg_job);
2303
2304 done_unmap_sg:
2305
2306         if(req_data)
2307                 dma_free_coherent(&ha->pdev->dev, req_data_len,
2308                         req_data, req_data_dma);
2309         dma_unmap_sg(&ha->pdev->dev,
2310             bsg_job->request_payload.sg_list,
2311             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2312         dma_unmap_sg(&ha->pdev->dev,
2313             bsg_job->reply_payload.sg_list,
2314             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2315
2316 done:
2317         return rval;
2318 }
2319
2320 static int
2321 qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
2322 {
2323         int ret = -EINVAL;
2324
2325         switch (bsg_job->request->msgcode) {
2326                 case FC_BSG_RPT_ELS:
2327                 case FC_BSG_HST_ELS_NOLOGIN:
2328                         ret = qla2x00_process_els(bsg_job);
2329                         break;
2330                 case FC_BSG_HST_CT:
2331                         ret = qla2x00_process_ct(bsg_job);
2332                         break;
2333                 case FC_BSG_HST_VENDOR:
2334                         ret = qla2x00_process_vendor_specific(bsg_job);
2335                         break;
2336                 case FC_BSG_HST_ADD_RPORT:
2337                 case FC_BSG_HST_DEL_RPORT:
2338                 case FC_BSG_RPT_CT:
2339                 default:
2340                         DEBUG2(printk("qla2xxx: unsupported BSG request\n"));
2341                         break;
2342         }
2343         return ret;
2344 }
2345
2346 static int
2347 qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
2348 {
2349         scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
2350         struct qla_hw_data *ha = vha->hw;
2351         srb_t *sp;
2352         int cnt, que;
2353         unsigned long flags;
2354         struct req_que *req;
2355         struct srb_bsg *sp_bsg;
2356
2357         /* find the bsg job from the active list of commands */
2358         spin_lock_irqsave(&ha->hardware_lock, flags);
2359         for (que = 0; que < ha->max_req_queues; que++) {
2360                 req = ha->req_q_map[que];
2361                 if (!req)
2362                         continue;
2363
2364                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++ ) {
2365                         sp = req->outstanding_cmds[cnt];
2366
2367                         if (sp) {
2368                                 sp_bsg = (struct srb_bsg*)sp->ctx;
2369
2370                                 if (((sp_bsg->ctx.type == SRB_CT_CMD) ||
2371                                     (sp_bsg->ctx.type == SRB_ELS_CMD_RPT)
2372                                     || ( sp_bsg->ctx.type == SRB_ELS_CMD_HST)) &&
2373                                     (sp_bsg->bsg_job == bsg_job)) {
2374                                         if (ha->isp_ops->abort_command(sp)) {
2375                                                 DEBUG2(qla_printk(KERN_INFO, ha,
2376                                                 "scsi(%ld): mbx abort_command failed\n", vha->host_no));
2377                                                 bsg_job->req->errors = bsg_job->reply->result = -EIO;
2378                                         } else {
2379                                                 DEBUG2(qla_printk(KERN_INFO, ha,
2380                                                 "scsi(%ld): mbx abort_command success\n", vha->host_no));
2381                                                 bsg_job->req->errors = bsg_job->reply->result = 0;
2382                                         }
2383                                         goto done;
2384                                 }
2385                         }
2386                 }
2387         }
2388         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2389         DEBUG2(qla_printk(KERN_INFO, ha,
2390                 "scsi(%ld) SRB not found to abort\n", vha->host_no));
2391         bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
2392         return 0;
2393
2394 done:
2395         if (bsg_job->request->msgcode == FC_BSG_HST_CT)
2396                 kfree(sp->fcport);
2397         kfree(sp->ctx);
2398         mempool_free(sp, ha->srb_mempool);
2399         return 0;
2400 }
2401
2402 struct fc_function_template qla2xxx_transport_functions = {
2403
2404         .show_host_node_name = 1,
2405         .show_host_port_name = 1,
2406         .show_host_supported_classes = 1,
2407         .show_host_supported_speeds = 1,
2408
2409         .get_host_port_id = qla2x00_get_host_port_id,
2410         .show_host_port_id = 1,
2411         .get_host_speed = qla2x00_get_host_speed,
2412         .show_host_speed = 1,
2413         .get_host_port_type = qla2x00_get_host_port_type,
2414         .show_host_port_type = 1,
2415         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
2416         .show_host_symbolic_name = 1,
2417         .set_host_system_hostname = qla2x00_set_host_system_hostname,
2418         .show_host_system_hostname = 1,
2419         .get_host_fabric_name = qla2x00_get_host_fabric_name,
2420         .show_host_fabric_name = 1,
2421         .get_host_port_state = qla2x00_get_host_port_state,
2422         .show_host_port_state = 1,
2423
2424         .dd_fcrport_size = sizeof(struct fc_port *),
2425         .show_rport_supported_classes = 1,
2426
2427         .get_starget_node_name = qla2x00_get_starget_node_name,
2428         .show_starget_node_name = 1,
2429         .get_starget_port_name = qla2x00_get_starget_port_name,
2430         .show_starget_port_name = 1,
2431         .get_starget_port_id  = qla2x00_get_starget_port_id,
2432         .show_starget_port_id = 1,
2433
2434         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
2435         .show_rport_dev_loss_tmo = 1,
2436
2437         .issue_fc_host_lip = qla2x00_issue_lip,
2438         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
2439         .terminate_rport_io = qla2x00_terminate_rport_io,
2440         .get_fc_host_stats = qla2x00_get_fc_host_stats,
2441
2442         .vport_create = qla24xx_vport_create,
2443         .vport_disable = qla24xx_vport_disable,
2444         .vport_delete = qla24xx_vport_delete,
2445         .bsg_request = qla24xx_bsg_request,
2446         .bsg_timeout = qla24xx_bsg_timeout,
2447 };
2448
2449 struct fc_function_template qla2xxx_transport_vport_functions = {
2450
2451         .show_host_node_name = 1,
2452         .show_host_port_name = 1,
2453         .show_host_supported_classes = 1,
2454
2455         .get_host_port_id = qla2x00_get_host_port_id,
2456         .show_host_port_id = 1,
2457         .get_host_speed = qla2x00_get_host_speed,
2458         .show_host_speed = 1,
2459         .get_host_port_type = qla2x00_get_host_port_type,
2460         .show_host_port_type = 1,
2461         .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
2462         .show_host_symbolic_name = 1,
2463         .set_host_system_hostname = qla2x00_set_host_system_hostname,
2464         .show_host_system_hostname = 1,
2465         .get_host_fabric_name = qla2x00_get_host_fabric_name,
2466         .show_host_fabric_name = 1,
2467         .get_host_port_state = qla2x00_get_host_port_state,
2468         .show_host_port_state = 1,
2469
2470         .dd_fcrport_size = sizeof(struct fc_port *),
2471         .show_rport_supported_classes = 1,
2472
2473         .get_starget_node_name = qla2x00_get_starget_node_name,
2474         .show_starget_node_name = 1,
2475         .get_starget_port_name = qla2x00_get_starget_port_name,
2476         .show_starget_port_name = 1,
2477         .get_starget_port_id  = qla2x00_get_starget_port_id,
2478         .show_starget_port_id = 1,
2479
2480         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
2481         .show_rport_dev_loss_tmo = 1,
2482
2483         .issue_fc_host_lip = qla2x00_issue_lip,
2484         .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
2485         .terminate_rport_io = qla2x00_terminate_rport_io,
2486         .get_fc_host_stats = qla2x00_get_fc_host_stats,
2487         .bsg_request = qla24xx_bsg_request,
2488         .bsg_timeout = qla24xx_bsg_timeout,
2489 };
2490
2491 void
2492 qla2x00_init_host_attr(scsi_qla_host_t *vha)
2493 {
2494         struct qla_hw_data *ha = vha->hw;
2495         u32 speed = FC_PORTSPEED_UNKNOWN;
2496
2497         fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
2498         fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
2499         fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
2500         fc_host_max_npiv_vports(vha->host) = ha->max_npiv_vports;
2501         fc_host_npiv_vports_inuse(vha->host) = ha->cur_vport_count;
2502
2503         if (IS_QLA81XX(ha))
2504                 speed = FC_PORTSPEED_10GBIT;
2505         else if (IS_QLA25XX(ha))
2506                 speed = FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT |
2507                     FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
2508         else if (IS_QLA24XX_TYPE(ha))
2509                 speed = FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
2510                     FC_PORTSPEED_1GBIT;
2511         else if (IS_QLA23XX(ha))
2512                 speed = FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
2513         else
2514                 speed = FC_PORTSPEED_1GBIT;
2515         fc_host_supported_speeds(vha->host) = speed;
2516 }
2517 static int
2518 qla84xx_reset(scsi_qla_host_t *ha, struct msg_echo_lb *mreq, struct fc_bsg_job *bsg_job)
2519 {
2520         int             ret = 0;
2521         int             cmd;
2522         uint16_t        cmd_status;
2523
2524         DEBUG16(printk("%s(%ld): entered.\n", __func__, ha->host_no));
2525
2526         cmd = (*((bsg_job->request->rqst_data.h_vendor.vendor_cmd) + 2))
2527                         == A84_RESET_FLAG_ENABLE_DIAG_FW ?
2528                                 A84_ISSUE_RESET_DIAG_FW : A84_ISSUE_RESET_OP_FW;
2529         ret = qla84xx_reset_chip(ha, cmd == A84_ISSUE_RESET_DIAG_FW,
2530         &cmd_status);
2531         return ret;
2532 }
2533
2534 static int
2535 qla84xx_mgmt_cmd(scsi_qla_host_t *ha, struct msg_echo_lb *mreq, struct fc_bsg_job *bsg_job)
2536 {
2537         struct access_chip_84xx *mn;
2538         dma_addr_t mn_dma, mgmt_dma;
2539         void *mgmt_b = NULL;
2540         int ret = 0;
2541         int rsp_hdr_len, len = 0;
2542         struct qla84_msg_mgmt *ql84_mgmt;
2543
2544         ql84_mgmt = (struct qla84_msg_mgmt *) vmalloc(sizeof(struct qla84_msg_mgmt));
2545         ql84_mgmt->cmd =
2546                 *((uint16_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 2));
2547         ql84_mgmt->mgmtp.u.mem.start_addr =
2548                 *((uint32_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 3));
2549         ql84_mgmt->len =
2550                 *((uint32_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 4));
2551         ql84_mgmt->mgmtp.u.config.id =
2552                 *((uint32_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 5));
2553         ql84_mgmt->mgmtp.u.config.param0 =
2554                 *((uint32_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 6));
2555         ql84_mgmt->mgmtp.u.config.param1 =
2556                 *((uint32_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 7));
2557         ql84_mgmt->mgmtp.u.info.type =
2558                 *((uint32_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 8));
2559         ql84_mgmt->mgmtp.u.info.context =
2560                 *((uint32_t *)(bsg_job->request->rqst_data.h_vendor.vendor_cmd + 9));
2561
2562         rsp_hdr_len = bsg_job->request_payload.payload_len;
2563
2564         mn = dma_pool_alloc(ha->hw->s_dma_pool, GFP_KERNEL, &mn_dma);
2565         if (mn == NULL) {
2566                 DEBUG2(printk(KERN_ERR "%s: dma alloc for fw buffer "
2567                 "failed%lu\n", __func__, ha->host_no));
2568                 return -ENOMEM;
2569         }
2570
2571         memset(mn, 0, sizeof (struct access_chip_84xx));
2572
2573         mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
2574         mn->entry_count = 1;
2575
2576         switch (ql84_mgmt->cmd) {
2577         case QLA84_MGMT_READ_MEM:
2578                 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
2579                 mn->parameter1 = cpu_to_le32(ql84_mgmt->mgmtp.u.mem.start_addr);
2580                 break;
2581         case QLA84_MGMT_WRITE_MEM:
2582                 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
2583                 mn->parameter1 = cpu_to_le32(ql84_mgmt->mgmtp.u.mem.start_addr);
2584                 break;
2585         case QLA84_MGMT_CHNG_CONFIG:
2586                 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
2587                 mn->parameter1 = cpu_to_le32(ql84_mgmt->mgmtp.u.config.id);
2588                 mn->parameter2 = cpu_to_le32(ql84_mgmt->mgmtp.u.config.param0);
2589                 mn->parameter3 = cpu_to_le32(ql84_mgmt->mgmtp.u.config.param1);
2590                 break;
2591         case QLA84_MGMT_GET_INFO:
2592                 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
2593                 mn->parameter1 = cpu_to_le32(ql84_mgmt->mgmtp.u.info.type);
2594                 mn->parameter2 = cpu_to_le32(ql84_mgmt->mgmtp.u.info.context);
2595                 break;
2596         default:
2597                 ret = -EIO;
2598                 goto exit_mgmt0;
2599         }
2600
2601         if ((len == ql84_mgmt->len) &&
2602                 ql84_mgmt->cmd != QLA84_MGMT_CHNG_CONFIG) {
2603                 mgmt_b = dma_alloc_coherent(&ha->hw->pdev->dev, len,
2604                                 &mgmt_dma, GFP_KERNEL);
2605                 if (mgmt_b == NULL) {
2606                         DEBUG2(printk(KERN_ERR "%s: dma alloc mgmt_b "
2607                         "failed%lu\n", __func__, ha->host_no));
2608                         ret = -ENOMEM;
2609                         goto exit_mgmt0;
2610                 }
2611                 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->len);
2612                 mn->dseg_count = cpu_to_le16(1);
2613                 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
2614                 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
2615                 mn->dseg_length = cpu_to_le32(len);
2616
2617                 if (ql84_mgmt->cmd == QLA84_MGMT_WRITE_MEM) {
2618                         memcpy(mgmt_b, ql84_mgmt->payload, len);
2619                 }
2620         }
2621
2622         ret = qla2x00_issue_iocb(ha, mn, mn_dma, 0);
2623         if ((ret != QLA_SUCCESS) || (ql84_mgmt->cmd == QLA84_MGMT_WRITE_MEM)
2624                 || (ql84_mgmt->cmd == QLA84_MGMT_CHNG_CONFIG)) {
2625                         if (ret != QLA_SUCCESS)
2626                                 DEBUG2(printk(KERN_ERR "%s(%lu): failed\n",
2627                                         __func__, ha->host_no));
2628         } else if ((ql84_mgmt->cmd == QLA84_MGMT_READ_MEM) ||
2629                         (ql84_mgmt->cmd == QLA84_MGMT_GET_INFO)) {
2630         }
2631
2632         if (mgmt_b)
2633                 dma_free_coherent(&ha->hw->pdev->dev, len, mgmt_b, mgmt_dma);
2634
2635 exit_mgmt0:
2636         dma_pool_free(ha->hw->s_dma_pool, mn, mn_dma);
2637         return ret;
2638 }