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