ARM: fix put_user() for gcc-8
[linux/fpc-iii.git] / drivers / scsi / qla2xxx / qla_attr.c
blobac12ee844bfc828ea4892226d8aedbc27e21c4e4
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2014 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
8 #include "qla_target.h"
10 #include <linux/kthread.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
15 static int qla24xx_vport_disable(struct fc_vport *, bool);
17 /* SYSFS attributes --------------------------------------------------------- */
19 static ssize_t
20 qla2x00_sysfs_read_fw_dump(struct file *filp, struct kobject *kobj,
21 struct bin_attribute *bin_attr,
22 char *buf, loff_t off, size_t count)
24 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
25 struct device, kobj)));
26 struct qla_hw_data *ha = vha->hw;
27 int rval = 0;
29 if (!(ha->fw_dump_reading || ha->mctp_dump_reading))
30 return 0;
32 if (IS_P3P_TYPE(ha)) {
33 if (off < ha->md_template_size) {
34 rval = memory_read_from_buffer(buf, count,
35 &off, ha->md_tmplt_hdr, ha->md_template_size);
36 return rval;
38 off -= ha->md_template_size;
39 rval = memory_read_from_buffer(buf, count,
40 &off, ha->md_dump, ha->md_dump_size);
41 return rval;
42 } else if (ha->mctp_dumped && ha->mctp_dump_reading)
43 return memory_read_from_buffer(buf, count, &off, ha->mctp_dump,
44 MCTP_DUMP_SIZE);
45 else if (ha->fw_dump_reading)
46 return memory_read_from_buffer(buf, count, &off, ha->fw_dump,
47 ha->fw_dump_len);
48 else
49 return 0;
52 static ssize_t
53 qla2x00_sysfs_write_fw_dump(struct file *filp, struct kobject *kobj,
54 struct bin_attribute *bin_attr,
55 char *buf, loff_t off, size_t count)
57 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
58 struct device, kobj)));
59 struct qla_hw_data *ha = vha->hw;
60 int reading;
62 if (off != 0)
63 return (0);
65 reading = simple_strtol(buf, NULL, 10);
66 switch (reading) {
67 case 0:
68 if (!ha->fw_dump_reading)
69 break;
71 ql_log(ql_log_info, vha, 0x705d,
72 "Firmware dump cleared on (%ld).\n", vha->host_no);
74 if (IS_P3P_TYPE(ha)) {
75 qla82xx_md_free(vha);
76 qla82xx_md_prep(vha);
78 ha->fw_dump_reading = 0;
79 ha->fw_dumped = 0;
80 break;
81 case 1:
82 if (ha->fw_dumped && !ha->fw_dump_reading) {
83 ha->fw_dump_reading = 1;
85 ql_log(ql_log_info, vha, 0x705e,
86 "Raw firmware dump ready for read on (%ld).\n",
87 vha->host_no);
89 break;
90 case 2:
91 qla2x00_alloc_fw_dump(vha);
92 break;
93 case 3:
94 if (IS_QLA82XX(ha)) {
95 qla82xx_idc_lock(ha);
96 qla82xx_set_reset_owner(vha);
97 qla82xx_idc_unlock(ha);
98 } else if (IS_QLA8044(ha)) {
99 qla8044_idc_lock(ha);
100 qla82xx_set_reset_owner(vha);
101 qla8044_idc_unlock(ha);
102 } else
103 qla2x00_system_error(vha);
104 break;
105 case 4:
106 if (IS_P3P_TYPE(ha)) {
107 if (ha->md_tmplt_hdr)
108 ql_dbg(ql_dbg_user, vha, 0x705b,
109 "MiniDump supported with this firmware.\n");
110 else
111 ql_dbg(ql_dbg_user, vha, 0x709d,
112 "MiniDump not supported with this firmware.\n");
114 break;
115 case 5:
116 if (IS_P3P_TYPE(ha))
117 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
118 break;
119 case 6:
120 if (!ha->mctp_dump_reading)
121 break;
122 ql_log(ql_log_info, vha, 0x70c1,
123 "MCTP dump cleared on (%ld).\n", vha->host_no);
124 ha->mctp_dump_reading = 0;
125 ha->mctp_dumped = 0;
126 break;
127 case 7:
128 if (ha->mctp_dumped && !ha->mctp_dump_reading) {
129 ha->mctp_dump_reading = 1;
130 ql_log(ql_log_info, vha, 0x70c2,
131 "Raw mctp dump ready for read on (%ld).\n",
132 vha->host_no);
134 break;
136 return count;
139 static struct bin_attribute sysfs_fw_dump_attr = {
140 .attr = {
141 .name = "fw_dump",
142 .mode = S_IRUSR | S_IWUSR,
144 .size = 0,
145 .read = qla2x00_sysfs_read_fw_dump,
146 .write = qla2x00_sysfs_write_fw_dump,
149 static ssize_t
150 qla2x00_sysfs_read_fw_dump_template(struct file *filp, struct kobject *kobj,
151 struct bin_attribute *bin_attr,
152 char *buf, loff_t off, size_t count)
154 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
155 struct device, kobj)));
156 struct qla_hw_data *ha = vha->hw;
158 if (!ha->fw_dump_template || !ha->fw_dump_template_len)
159 return 0;
161 ql_dbg(ql_dbg_user, vha, 0x70e2,
162 "chunk <- off=%llx count=%zx\n", off, count);
163 return memory_read_from_buffer(buf, count, &off,
164 ha->fw_dump_template, ha->fw_dump_template_len);
167 static ssize_t
168 qla2x00_sysfs_write_fw_dump_template(struct file *filp, struct kobject *kobj,
169 struct bin_attribute *bin_attr,
170 char *buf, loff_t off, size_t count)
172 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
173 struct device, kobj)));
174 struct qla_hw_data *ha = vha->hw;
175 uint32_t size;
177 if (off == 0) {
178 if (ha->fw_dump)
179 vfree(ha->fw_dump);
180 if (ha->fw_dump_template)
181 vfree(ha->fw_dump_template);
183 ha->fw_dump = NULL;
184 ha->fw_dump_len = 0;
185 ha->fw_dump_template = NULL;
186 ha->fw_dump_template_len = 0;
188 size = qla27xx_fwdt_template_size(buf);
189 ql_dbg(ql_dbg_user, vha, 0x70d1,
190 "-> allocating fwdt (%x bytes)...\n", size);
191 ha->fw_dump_template = vmalloc(size);
192 if (!ha->fw_dump_template) {
193 ql_log(ql_log_warn, vha, 0x70d2,
194 "Failed allocate fwdt (%x bytes).\n", size);
195 return -ENOMEM;
197 ha->fw_dump_template_len = size;
200 if (off + count > ha->fw_dump_template_len) {
201 count = ha->fw_dump_template_len - off;
202 ql_dbg(ql_dbg_user, vha, 0x70d3,
203 "chunk -> truncating to %zx bytes.\n", count);
206 ql_dbg(ql_dbg_user, vha, 0x70d4,
207 "chunk -> off=%llx count=%zx\n", off, count);
208 memcpy(ha->fw_dump_template + off, buf, count);
210 if (off + count == ha->fw_dump_template_len) {
211 size = qla27xx_fwdt_calculate_dump_size(vha);
212 ql_dbg(ql_dbg_user, vha, 0x70d5,
213 "-> allocating fwdump (%x bytes)...\n", size);
214 ha->fw_dump = vmalloc(size);
215 if (!ha->fw_dump) {
216 ql_log(ql_log_warn, vha, 0x70d6,
217 "Failed allocate fwdump (%x bytes).\n", size);
218 return -ENOMEM;
220 ha->fw_dump_len = size;
223 return count;
225 static struct bin_attribute sysfs_fw_dump_template_attr = {
226 .attr = {
227 .name = "fw_dump_template",
228 .mode = S_IRUSR | S_IWUSR,
230 .size = 0,
231 .read = qla2x00_sysfs_read_fw_dump_template,
232 .write = qla2x00_sysfs_write_fw_dump_template,
235 static ssize_t
236 qla2x00_sysfs_read_nvram(struct file *filp, struct kobject *kobj,
237 struct bin_attribute *bin_attr,
238 char *buf, loff_t off, size_t count)
240 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
241 struct device, kobj)));
242 struct qla_hw_data *ha = vha->hw;
244 if (!capable(CAP_SYS_ADMIN))
245 return 0;
247 if (IS_NOCACHE_VPD_TYPE(ha))
248 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
249 ha->nvram_size);
250 return memory_read_from_buffer(buf, count, &off, ha->nvram,
251 ha->nvram_size);
254 static ssize_t
255 qla2x00_sysfs_write_nvram(struct file *filp, struct kobject *kobj,
256 struct bin_attribute *bin_attr,
257 char *buf, loff_t off, size_t count)
259 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
260 struct device, kobj)));
261 struct qla_hw_data *ha = vha->hw;
262 uint16_t cnt;
264 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size ||
265 !ha->isp_ops->write_nvram)
266 return -EINVAL;
268 /* Checksum NVRAM. */
269 if (IS_FWI2_CAPABLE(ha)) {
270 uint32_t *iter;
271 uint32_t chksum;
273 iter = (uint32_t *)buf;
274 chksum = 0;
275 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
276 chksum += le32_to_cpu(*iter++);
277 chksum = ~chksum + 1;
278 *iter = cpu_to_le32(chksum);
279 } else {
280 uint8_t *iter;
281 uint8_t chksum;
283 iter = (uint8_t *)buf;
284 chksum = 0;
285 for (cnt = 0; cnt < count - 1; cnt++)
286 chksum += *iter++;
287 chksum = ~chksum + 1;
288 *iter = chksum;
291 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
292 ql_log(ql_log_warn, vha, 0x705f,
293 "HBA not online, failing NVRAM update.\n");
294 return -EAGAIN;
297 /* Write NVRAM. */
298 ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->nvram_base, count);
299 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->nvram, ha->nvram_base,
300 count);
302 ql_dbg(ql_dbg_user, vha, 0x7060,
303 "Setting ISP_ABORT_NEEDED\n");
304 /* NVRAM settings take effect immediately. */
305 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
306 qla2xxx_wake_dpc(vha);
307 qla2x00_wait_for_chip_reset(vha);
309 return count;
312 static struct bin_attribute sysfs_nvram_attr = {
313 .attr = {
314 .name = "nvram",
315 .mode = S_IRUSR | S_IWUSR,
317 .size = 512,
318 .read = qla2x00_sysfs_read_nvram,
319 .write = qla2x00_sysfs_write_nvram,
322 static ssize_t
323 qla2x00_sysfs_read_optrom(struct file *filp, struct kobject *kobj,
324 struct bin_attribute *bin_attr,
325 char *buf, loff_t off, size_t count)
327 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
328 struct device, kobj)));
329 struct qla_hw_data *ha = vha->hw;
330 ssize_t rval = 0;
332 mutex_lock(&ha->optrom_mutex);
334 if (ha->optrom_state != QLA_SREADING)
335 goto out;
337 rval = memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
338 ha->optrom_region_size);
340 out:
341 mutex_unlock(&ha->optrom_mutex);
343 return rval;
346 static ssize_t
347 qla2x00_sysfs_write_optrom(struct file *filp, struct kobject *kobj,
348 struct bin_attribute *bin_attr,
349 char *buf, loff_t off, size_t count)
351 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
352 struct device, kobj)));
353 struct qla_hw_data *ha = vha->hw;
355 mutex_lock(&ha->optrom_mutex);
357 if (ha->optrom_state != QLA_SWRITING) {
358 mutex_unlock(&ha->optrom_mutex);
359 return -EINVAL;
361 if (off > ha->optrom_region_size) {
362 mutex_unlock(&ha->optrom_mutex);
363 return -ERANGE;
365 if (off + count > ha->optrom_region_size)
366 count = ha->optrom_region_size - off;
368 memcpy(&ha->optrom_buffer[off], buf, count);
369 mutex_unlock(&ha->optrom_mutex);
371 return count;
374 static struct bin_attribute sysfs_optrom_attr = {
375 .attr = {
376 .name = "optrom",
377 .mode = S_IRUSR | S_IWUSR,
379 .size = 0,
380 .read = qla2x00_sysfs_read_optrom,
381 .write = qla2x00_sysfs_write_optrom,
384 static ssize_t
385 qla2x00_sysfs_write_optrom_ctl(struct file *filp, struct kobject *kobj,
386 struct bin_attribute *bin_attr,
387 char *buf, loff_t off, size_t count)
389 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
390 struct device, kobj)));
391 struct qla_hw_data *ha = vha->hw;
392 uint32_t start = 0;
393 uint32_t size = ha->optrom_size;
394 int val, valid;
395 ssize_t rval = count;
397 if (off)
398 return -EINVAL;
400 if (unlikely(pci_channel_offline(ha->pdev)))
401 return -EAGAIN;
403 if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
404 return -EINVAL;
405 if (start > ha->optrom_size)
406 return -EINVAL;
407 if (size > ha->optrom_size - start)
408 size = ha->optrom_size - start;
410 mutex_lock(&ha->optrom_mutex);
411 switch (val) {
412 case 0:
413 if (ha->optrom_state != QLA_SREADING &&
414 ha->optrom_state != QLA_SWRITING) {
415 rval = -EINVAL;
416 goto out;
418 ha->optrom_state = QLA_SWAITING;
420 ql_dbg(ql_dbg_user, vha, 0x7061,
421 "Freeing flash region allocation -- 0x%x bytes.\n",
422 ha->optrom_region_size);
424 vfree(ha->optrom_buffer);
425 ha->optrom_buffer = NULL;
426 break;
427 case 1:
428 if (ha->optrom_state != QLA_SWAITING) {
429 rval = -EINVAL;
430 goto out;
433 ha->optrom_region_start = start;
434 ha->optrom_region_size = start + size;
436 ha->optrom_state = QLA_SREADING;
437 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
438 if (ha->optrom_buffer == NULL) {
439 ql_log(ql_log_warn, vha, 0x7062,
440 "Unable to allocate memory for optrom retrieval "
441 "(%x).\n", ha->optrom_region_size);
443 ha->optrom_state = QLA_SWAITING;
444 rval = -ENOMEM;
445 goto out;
448 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
449 ql_log(ql_log_warn, vha, 0x7063,
450 "HBA not online, failing NVRAM update.\n");
451 rval = -EAGAIN;
452 goto out;
455 ql_dbg(ql_dbg_user, vha, 0x7064,
456 "Reading flash region -- 0x%x/0x%x.\n",
457 ha->optrom_region_start, ha->optrom_region_size);
459 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
460 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
461 ha->optrom_region_start, ha->optrom_region_size);
462 break;
463 case 2:
464 if (ha->optrom_state != QLA_SWAITING) {
465 rval = -EINVAL;
466 goto out;
470 * We need to be more restrictive on which FLASH regions are
471 * allowed to be updated via user-space. Regions accessible
472 * via this method include:
474 * ISP21xx/ISP22xx/ISP23xx type boards:
476 * 0x000000 -> 0x020000 -- Boot code.
478 * ISP2322/ISP24xx type boards:
480 * 0x000000 -> 0x07ffff -- Boot code.
481 * 0x080000 -> 0x0fffff -- Firmware.
483 * ISP25xx type boards:
485 * 0x000000 -> 0x07ffff -- Boot code.
486 * 0x080000 -> 0x0fffff -- Firmware.
487 * 0x120000 -> 0x12ffff -- VPD and HBA parameters.
489 valid = 0;
490 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
491 valid = 1;
492 else if (start == (ha->flt_region_boot * 4) ||
493 start == (ha->flt_region_fw * 4))
494 valid = 1;
495 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha)
496 || IS_CNA_CAPABLE(ha) || IS_QLA2031(ha)
497 || IS_QLA27XX(ha))
498 valid = 1;
499 if (!valid) {
500 ql_log(ql_log_warn, vha, 0x7065,
501 "Invalid start region 0x%x/0x%x.\n", start, size);
502 rval = -EINVAL;
503 goto out;
506 ha->optrom_region_start = start;
507 ha->optrom_region_size = start + size;
509 ha->optrom_state = QLA_SWRITING;
510 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
511 if (ha->optrom_buffer == NULL) {
512 ql_log(ql_log_warn, vha, 0x7066,
513 "Unable to allocate memory for optrom update "
514 "(%x)\n", ha->optrom_region_size);
516 ha->optrom_state = QLA_SWAITING;
517 rval = -ENOMEM;
518 goto out;
521 ql_dbg(ql_dbg_user, vha, 0x7067,
522 "Staging flash region write -- 0x%x/0x%x.\n",
523 ha->optrom_region_start, ha->optrom_region_size);
525 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
526 break;
527 case 3:
528 if (ha->optrom_state != QLA_SWRITING) {
529 rval = -EINVAL;
530 goto out;
533 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
534 ql_log(ql_log_warn, vha, 0x7068,
535 "HBA not online, failing flash update.\n");
536 rval = -EAGAIN;
537 goto out;
540 ql_dbg(ql_dbg_user, vha, 0x7069,
541 "Writing flash region -- 0x%x/0x%x.\n",
542 ha->optrom_region_start, ha->optrom_region_size);
544 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
545 ha->optrom_region_start, ha->optrom_region_size);
546 break;
547 default:
548 rval = -EINVAL;
551 out:
552 mutex_unlock(&ha->optrom_mutex);
553 return rval;
556 static struct bin_attribute sysfs_optrom_ctl_attr = {
557 .attr = {
558 .name = "optrom_ctl",
559 .mode = S_IWUSR,
561 .size = 0,
562 .write = qla2x00_sysfs_write_optrom_ctl,
565 static ssize_t
566 qla2x00_sysfs_read_vpd(struct file *filp, struct kobject *kobj,
567 struct bin_attribute *bin_attr,
568 char *buf, loff_t off, size_t count)
570 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
571 struct device, kobj)));
572 struct qla_hw_data *ha = vha->hw;
574 if (unlikely(pci_channel_offline(ha->pdev)))
575 return -EAGAIN;
577 if (!capable(CAP_SYS_ADMIN))
578 return -EINVAL;
580 if (IS_NOCACHE_VPD_TYPE(ha))
581 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
582 ha->vpd_size);
583 return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
586 static ssize_t
587 qla2x00_sysfs_write_vpd(struct file *filp, struct kobject *kobj,
588 struct bin_attribute *bin_attr,
589 char *buf, loff_t off, size_t count)
591 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
592 struct device, kobj)));
593 struct qla_hw_data *ha = vha->hw;
594 uint8_t *tmp_data;
596 if (unlikely(pci_channel_offline(ha->pdev)))
597 return 0;
599 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size ||
600 !ha->isp_ops->write_nvram)
601 return 0;
603 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
604 ql_log(ql_log_warn, vha, 0x706a,
605 "HBA not online, failing VPD update.\n");
606 return -EAGAIN;
609 /* Write NVRAM. */
610 ha->isp_ops->write_nvram(vha, (uint8_t *)buf, ha->vpd_base, count);
611 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd, ha->vpd_base, count);
613 /* Update flash version information for 4Gb & above. */
614 if (!IS_FWI2_CAPABLE(ha))
615 return -EINVAL;
617 tmp_data = vmalloc(256);
618 if (!tmp_data) {
619 ql_log(ql_log_warn, vha, 0x706b,
620 "Unable to allocate memory for VPD information update.\n");
621 return -ENOMEM;
623 ha->isp_ops->get_flash_version(vha, tmp_data);
624 vfree(tmp_data);
626 return count;
629 static struct bin_attribute sysfs_vpd_attr = {
630 .attr = {
631 .name = "vpd",
632 .mode = S_IRUSR | S_IWUSR,
634 .size = 0,
635 .read = qla2x00_sysfs_read_vpd,
636 .write = qla2x00_sysfs_write_vpd,
639 static ssize_t
640 qla2x00_sysfs_read_sfp(struct file *filp, struct kobject *kobj,
641 struct bin_attribute *bin_attr,
642 char *buf, loff_t off, size_t count)
644 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
645 struct device, kobj)));
646 struct qla_hw_data *ha = vha->hw;
647 uint16_t iter, addr, offset;
648 int rval;
650 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
651 return 0;
653 if (ha->sfp_data)
654 goto do_read;
656 ha->sfp_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
657 &ha->sfp_data_dma);
658 if (!ha->sfp_data) {
659 ql_log(ql_log_warn, vha, 0x706c,
660 "Unable to allocate memory for SFP read-data.\n");
661 return 0;
664 do_read:
665 memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
666 addr = 0xa0;
667 for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
668 iter++, offset += SFP_BLOCK_SIZE) {
669 if (iter == 4) {
670 /* Skip to next device address. */
671 addr = 0xa2;
672 offset = 0;
675 rval = qla2x00_read_sfp(vha, ha->sfp_data_dma, ha->sfp_data,
676 addr, offset, SFP_BLOCK_SIZE, BIT_1);
677 if (rval != QLA_SUCCESS) {
678 ql_log(ql_log_warn, vha, 0x706d,
679 "Unable to read SFP data (%x/%x/%x).\n", rval,
680 addr, offset);
682 return -EIO;
684 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
685 buf += SFP_BLOCK_SIZE;
688 return count;
691 static struct bin_attribute sysfs_sfp_attr = {
692 .attr = {
693 .name = "sfp",
694 .mode = S_IRUSR | S_IWUSR,
696 .size = SFP_DEV_SIZE * 2,
697 .read = qla2x00_sysfs_read_sfp,
700 static ssize_t
701 qla2x00_sysfs_write_reset(struct file *filp, struct kobject *kobj,
702 struct bin_attribute *bin_attr,
703 char *buf, loff_t off, size_t count)
705 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
706 struct device, kobj)));
707 struct qla_hw_data *ha = vha->hw;
708 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
709 int type;
710 uint32_t idc_control;
711 uint8_t *tmp_data = NULL;
712 if (off != 0)
713 return -EINVAL;
715 type = simple_strtol(buf, NULL, 10);
716 switch (type) {
717 case 0x2025c:
718 ql_log(ql_log_info, vha, 0x706e,
719 "Issuing ISP reset.\n");
721 scsi_block_requests(vha->host);
722 if (IS_QLA82XX(ha)) {
723 ha->flags.isp82xx_no_md_cap = 1;
724 qla82xx_idc_lock(ha);
725 qla82xx_set_reset_owner(vha);
726 qla82xx_idc_unlock(ha);
727 } else if (IS_QLA8044(ha)) {
728 qla8044_idc_lock(ha);
729 idc_control = qla8044_rd_reg(ha,
730 QLA8044_IDC_DRV_CTRL);
731 qla8044_wr_reg(ha, QLA8044_IDC_DRV_CTRL,
732 (idc_control | GRACEFUL_RESET_BIT1));
733 qla82xx_set_reset_owner(vha);
734 qla8044_idc_unlock(ha);
735 } else {
736 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
737 qla2xxx_wake_dpc(vha);
739 qla2x00_wait_for_chip_reset(vha);
740 scsi_unblock_requests(vha->host);
741 break;
742 case 0x2025d:
743 if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha))
744 return -EPERM;
746 ql_log(ql_log_info, vha, 0x706f,
747 "Issuing MPI reset.\n");
749 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
750 uint32_t idc_control;
752 qla83xx_idc_lock(vha, 0);
753 __qla83xx_get_idc_control(vha, &idc_control);
754 idc_control |= QLA83XX_IDC_GRACEFUL_RESET;
755 __qla83xx_set_idc_control(vha, idc_control);
756 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
757 QLA8XXX_DEV_NEED_RESET);
758 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
759 qla83xx_idc_unlock(vha, 0);
760 break;
761 } else {
762 /* Make sure FC side is not in reset */
763 qla2x00_wait_for_hba_online(vha);
765 /* Issue MPI reset */
766 scsi_block_requests(vha->host);
767 if (qla81xx_restart_mpi_firmware(vha) != QLA_SUCCESS)
768 ql_log(ql_log_warn, vha, 0x7070,
769 "MPI reset failed.\n");
770 scsi_unblock_requests(vha->host);
771 break;
773 case 0x2025e:
774 if (!IS_P3P_TYPE(ha) || vha != base_vha) {
775 ql_log(ql_log_info, vha, 0x7071,
776 "FCoE ctx reset no supported.\n");
777 return -EPERM;
780 ql_log(ql_log_info, vha, 0x7072,
781 "Issuing FCoE ctx reset.\n");
782 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
783 qla2xxx_wake_dpc(vha);
784 qla2x00_wait_for_fcoe_ctx_reset(vha);
785 break;
786 case 0x2025f:
787 if (!IS_QLA8031(ha))
788 return -EPERM;
789 ql_log(ql_log_info, vha, 0x70bc,
790 "Disabling Reset by IDC control\n");
791 qla83xx_idc_lock(vha, 0);
792 __qla83xx_get_idc_control(vha, &idc_control);
793 idc_control |= QLA83XX_IDC_RESET_DISABLED;
794 __qla83xx_set_idc_control(vha, idc_control);
795 qla83xx_idc_unlock(vha, 0);
796 break;
797 case 0x20260:
798 if (!IS_QLA8031(ha))
799 return -EPERM;
800 ql_log(ql_log_info, vha, 0x70bd,
801 "Enabling Reset by IDC control\n");
802 qla83xx_idc_lock(vha, 0);
803 __qla83xx_get_idc_control(vha, &idc_control);
804 idc_control &= ~QLA83XX_IDC_RESET_DISABLED;
805 __qla83xx_set_idc_control(vha, idc_control);
806 qla83xx_idc_unlock(vha, 0);
807 break;
808 case 0x20261:
809 ql_dbg(ql_dbg_user, vha, 0x70e0,
810 "Updating cache versions without reset ");
812 tmp_data = vmalloc(256);
813 if (!tmp_data) {
814 ql_log(ql_log_warn, vha, 0x70e1,
815 "Unable to allocate memory for VPD information update.\n");
816 return -ENOMEM;
818 ha->isp_ops->get_flash_version(vha, tmp_data);
819 vfree(tmp_data);
820 break;
822 return count;
825 static struct bin_attribute sysfs_reset_attr = {
826 .attr = {
827 .name = "reset",
828 .mode = S_IWUSR,
830 .size = 0,
831 .write = qla2x00_sysfs_write_reset,
834 static ssize_t
835 qla2x00_sysfs_read_xgmac_stats(struct file *filp, struct kobject *kobj,
836 struct bin_attribute *bin_attr,
837 char *buf, loff_t off, size_t count)
839 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
840 struct device, kobj)));
841 struct qla_hw_data *ha = vha->hw;
842 int rval;
843 uint16_t actual_size;
845 if (!capable(CAP_SYS_ADMIN) || off != 0 || count > XGMAC_DATA_SIZE)
846 return 0;
848 if (ha->xgmac_data)
849 goto do_read;
851 ha->xgmac_data = dma_alloc_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
852 &ha->xgmac_data_dma, GFP_KERNEL);
853 if (!ha->xgmac_data) {
854 ql_log(ql_log_warn, vha, 0x7076,
855 "Unable to allocate memory for XGMAC read-data.\n");
856 return 0;
859 do_read:
860 actual_size = 0;
861 memset(ha->xgmac_data, 0, XGMAC_DATA_SIZE);
863 rval = qla2x00_get_xgmac_stats(vha, ha->xgmac_data_dma,
864 XGMAC_DATA_SIZE, &actual_size);
865 if (rval != QLA_SUCCESS) {
866 ql_log(ql_log_warn, vha, 0x7077,
867 "Unable to read XGMAC data (%x).\n", rval);
868 count = 0;
871 count = actual_size > count ? count: actual_size;
872 memcpy(buf, ha->xgmac_data, count);
874 return count;
877 static struct bin_attribute sysfs_xgmac_stats_attr = {
878 .attr = {
879 .name = "xgmac_stats",
880 .mode = S_IRUSR,
882 .size = 0,
883 .read = qla2x00_sysfs_read_xgmac_stats,
886 static ssize_t
887 qla2x00_sysfs_read_dcbx_tlv(struct file *filp, struct kobject *kobj,
888 struct bin_attribute *bin_attr,
889 char *buf, loff_t off, size_t count)
891 struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
892 struct device, kobj)));
893 struct qla_hw_data *ha = vha->hw;
894 int rval;
896 if (!capable(CAP_SYS_ADMIN) || off != 0 || count > DCBX_TLV_DATA_SIZE)
897 return 0;
899 if (ha->dcbx_tlv)
900 goto do_read;
902 ha->dcbx_tlv = dma_alloc_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
903 &ha->dcbx_tlv_dma, GFP_KERNEL);
904 if (!ha->dcbx_tlv) {
905 ql_log(ql_log_warn, vha, 0x7078,
906 "Unable to allocate memory for DCBX TLV read-data.\n");
907 return -ENOMEM;
910 do_read:
911 memset(ha->dcbx_tlv, 0, DCBX_TLV_DATA_SIZE);
913 rval = qla2x00_get_dcbx_params(vha, ha->dcbx_tlv_dma,
914 DCBX_TLV_DATA_SIZE);
915 if (rval != QLA_SUCCESS) {
916 ql_log(ql_log_warn, vha, 0x7079,
917 "Unable to read DCBX TLV (%x).\n", rval);
918 return -EIO;
921 memcpy(buf, ha->dcbx_tlv, count);
923 return count;
926 static struct bin_attribute sysfs_dcbx_tlv_attr = {
927 .attr = {
928 .name = "dcbx_tlv",
929 .mode = S_IRUSR,
931 .size = 0,
932 .read = qla2x00_sysfs_read_dcbx_tlv,
935 static struct sysfs_entry {
936 char *name;
937 struct bin_attribute *attr;
938 int is4GBp_only;
939 } bin_file_entries[] = {
940 { "fw_dump", &sysfs_fw_dump_attr, },
941 { "fw_dump_template", &sysfs_fw_dump_template_attr, 0x27 },
942 { "nvram", &sysfs_nvram_attr, },
943 { "optrom", &sysfs_optrom_attr, },
944 { "optrom_ctl", &sysfs_optrom_ctl_attr, },
945 { "vpd", &sysfs_vpd_attr, 1 },
946 { "sfp", &sysfs_sfp_attr, 1 },
947 { "reset", &sysfs_reset_attr, },
948 { "xgmac_stats", &sysfs_xgmac_stats_attr, 3 },
949 { "dcbx_tlv", &sysfs_dcbx_tlv_attr, 3 },
950 { NULL },
953 void
954 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *vha)
956 struct Scsi_Host *host = vha->host;
957 struct sysfs_entry *iter;
958 int ret;
960 for (iter = bin_file_entries; iter->name; iter++) {
961 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(vha->hw))
962 continue;
963 if (iter->is4GBp_only == 2 && !IS_QLA25XX(vha->hw))
964 continue;
965 if (iter->is4GBp_only == 3 && !(IS_CNA_CAPABLE(vha->hw)))
966 continue;
967 if (iter->is4GBp_only == 0x27 && !IS_QLA27XX(vha->hw))
968 continue;
970 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
971 iter->attr);
972 if (ret)
973 ql_log(ql_log_warn, vha, 0x00f3,
974 "Unable to create sysfs %s binary attribute (%d).\n",
975 iter->name, ret);
976 else
977 ql_dbg(ql_dbg_init, vha, 0x00f4,
978 "Successfully created sysfs %s binary attribure.\n",
979 iter->name);
983 void
984 qla2x00_free_sysfs_attr(scsi_qla_host_t *vha, bool stop_beacon)
986 struct Scsi_Host *host = vha->host;
987 struct sysfs_entry *iter;
988 struct qla_hw_data *ha = vha->hw;
990 for (iter = bin_file_entries; iter->name; iter++) {
991 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
992 continue;
993 if (iter->is4GBp_only == 2 && !IS_QLA25XX(ha))
994 continue;
995 if (iter->is4GBp_only == 3 && !(IS_CNA_CAPABLE(vha->hw)))
996 continue;
997 if (iter->is4GBp_only == 0x27 && !IS_QLA27XX(vha->hw))
998 continue;
1000 sysfs_remove_bin_file(&host->shost_gendev.kobj,
1001 iter->attr);
1004 if (stop_beacon && ha->beacon_blink_led == 1)
1005 ha->isp_ops->beacon_off(vha);
1008 /* Scsi_Host attributes. */
1010 static ssize_t
1011 qla2x00_drvr_version_show(struct device *dev,
1012 struct device_attribute *attr, char *buf)
1014 return scnprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
1017 static ssize_t
1018 qla2x00_fw_version_show(struct device *dev,
1019 struct device_attribute *attr, char *buf)
1021 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1022 struct qla_hw_data *ha = vha->hw;
1023 char fw_str[128];
1025 return scnprintf(buf, PAGE_SIZE, "%s\n",
1026 ha->isp_ops->fw_version_str(vha, fw_str, sizeof(fw_str)));
1029 static ssize_t
1030 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
1031 char *buf)
1033 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1034 struct qla_hw_data *ha = vha->hw;
1035 uint32_t sn;
1037 if (IS_QLAFX00(vha->hw)) {
1038 return scnprintf(buf, PAGE_SIZE, "%s\n",
1039 vha->hw->mr.serial_num);
1040 } else if (IS_FWI2_CAPABLE(ha)) {
1041 qla2xxx_get_vpd_field(vha, "SN", buf, PAGE_SIZE - 1);
1042 return strlen(strcat(buf, "\n"));
1045 sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
1046 return scnprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
1047 sn % 100000);
1050 static ssize_t
1051 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
1052 char *buf)
1054 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1055 return scnprintf(buf, PAGE_SIZE, "ISP%04X\n", vha->hw->pdev->device);
1058 static ssize_t
1059 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
1060 char *buf)
1062 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1063 struct qla_hw_data *ha = vha->hw;
1065 if (IS_QLAFX00(vha->hw))
1066 return scnprintf(buf, PAGE_SIZE, "%s\n",
1067 vha->hw->mr.hw_version);
1069 return scnprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
1070 ha->product_id[0], ha->product_id[1], ha->product_id[2],
1071 ha->product_id[3]);
1074 static ssize_t
1075 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
1076 char *buf)
1078 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1080 return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_number);
1083 static ssize_t
1084 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
1085 char *buf)
1087 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1088 return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_desc);
1091 static ssize_t
1092 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
1093 char *buf)
1095 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1096 char pci_info[30];
1098 return scnprintf(buf, PAGE_SIZE, "%s\n",
1099 vha->hw->isp_ops->pci_info_str(vha, pci_info));
1102 static ssize_t
1103 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
1104 char *buf)
1106 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1107 struct qla_hw_data *ha = vha->hw;
1108 int len = 0;
1110 if (atomic_read(&vha->loop_state) == LOOP_DOWN ||
1111 atomic_read(&vha->loop_state) == LOOP_DEAD ||
1112 vha->device_flags & DFLG_NO_CABLE)
1113 len = scnprintf(buf, PAGE_SIZE, "Link Down\n");
1114 else if (atomic_read(&vha->loop_state) != LOOP_READY ||
1115 qla2x00_reset_active(vha))
1116 len = scnprintf(buf, PAGE_SIZE, "Unknown Link State\n");
1117 else {
1118 len = scnprintf(buf, PAGE_SIZE, "Link Up - ");
1120 switch (ha->current_topology) {
1121 case ISP_CFG_NL:
1122 len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1123 break;
1124 case ISP_CFG_FL:
1125 len += scnprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
1126 break;
1127 case ISP_CFG_N:
1128 len += scnprintf(buf + len, PAGE_SIZE-len,
1129 "N_Port to N_Port\n");
1130 break;
1131 case ISP_CFG_F:
1132 len += scnprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
1133 break;
1134 default:
1135 len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1136 break;
1139 return len;
1142 static ssize_t
1143 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
1144 char *buf)
1146 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1147 int len = 0;
1149 switch (vha->hw->zio_mode) {
1150 case QLA_ZIO_MODE_6:
1151 len += scnprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
1152 break;
1153 case QLA_ZIO_DISABLED:
1154 len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1155 break;
1157 return len;
1160 static ssize_t
1161 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
1162 const char *buf, size_t count)
1164 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1165 struct qla_hw_data *ha = vha->hw;
1166 int val = 0;
1167 uint16_t zio_mode;
1169 if (!IS_ZIO_SUPPORTED(ha))
1170 return -ENOTSUPP;
1172 if (sscanf(buf, "%d", &val) != 1)
1173 return -EINVAL;
1175 if (val)
1176 zio_mode = QLA_ZIO_MODE_6;
1177 else
1178 zio_mode = QLA_ZIO_DISABLED;
1180 /* Update per-hba values and queue a reset. */
1181 if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
1182 ha->zio_mode = zio_mode;
1183 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1185 return strlen(buf);
1188 static ssize_t
1189 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
1190 char *buf)
1192 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1194 return scnprintf(buf, PAGE_SIZE, "%d us\n", vha->hw->zio_timer * 100);
1197 static ssize_t
1198 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
1199 const char *buf, size_t count)
1201 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1202 int val = 0;
1203 uint16_t zio_timer;
1205 if (sscanf(buf, "%d", &val) != 1)
1206 return -EINVAL;
1207 if (val > 25500 || val < 100)
1208 return -ERANGE;
1210 zio_timer = (uint16_t)(val / 100);
1211 vha->hw->zio_timer = zio_timer;
1213 return strlen(buf);
1216 static ssize_t
1217 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
1218 char *buf)
1220 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1221 int len = 0;
1223 if (vha->hw->beacon_blink_led)
1224 len += scnprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
1225 else
1226 len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1227 return len;
1230 static ssize_t
1231 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
1232 const char *buf, size_t count)
1234 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1235 struct qla_hw_data *ha = vha->hw;
1236 int val = 0;
1237 int rval;
1239 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1240 return -EPERM;
1242 if (test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
1243 ql_log(ql_log_warn, vha, 0x707a,
1244 "Abort ISP active -- ignoring beacon request.\n");
1245 return -EBUSY;
1248 if (sscanf(buf, "%d", &val) != 1)
1249 return -EINVAL;
1251 if (val)
1252 rval = ha->isp_ops->beacon_on(vha);
1253 else
1254 rval = ha->isp_ops->beacon_off(vha);
1256 if (rval != QLA_SUCCESS)
1257 count = 0;
1259 return count;
1262 static ssize_t
1263 qla2x00_optrom_bios_version_show(struct device *dev,
1264 struct device_attribute *attr, char *buf)
1266 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1267 struct qla_hw_data *ha = vha->hw;
1268 return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
1269 ha->bios_revision[0]);
1272 static ssize_t
1273 qla2x00_optrom_efi_version_show(struct device *dev,
1274 struct device_attribute *attr, char *buf)
1276 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1277 struct qla_hw_data *ha = vha->hw;
1278 return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
1279 ha->efi_revision[0]);
1282 static ssize_t
1283 qla2x00_optrom_fcode_version_show(struct device *dev,
1284 struct device_attribute *attr, char *buf)
1286 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1287 struct qla_hw_data *ha = vha->hw;
1288 return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
1289 ha->fcode_revision[0]);
1292 static ssize_t
1293 qla2x00_optrom_fw_version_show(struct device *dev,
1294 struct device_attribute *attr, char *buf)
1296 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1297 struct qla_hw_data *ha = vha->hw;
1298 return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
1299 ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
1300 ha->fw_revision[3]);
1303 static ssize_t
1304 qla2x00_optrom_gold_fw_version_show(struct device *dev,
1305 struct device_attribute *attr, char *buf)
1307 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1308 struct qla_hw_data *ha = vha->hw;
1310 if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha) && !IS_QLA27XX(ha))
1311 return scnprintf(buf, PAGE_SIZE, "\n");
1313 return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%d)\n",
1314 ha->gold_fw_version[0], ha->gold_fw_version[1],
1315 ha->gold_fw_version[2], ha->gold_fw_version[3]);
1318 static ssize_t
1319 qla2x00_total_isp_aborts_show(struct device *dev,
1320 struct device_attribute *attr, char *buf)
1322 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1323 return scnprintf(buf, PAGE_SIZE, "%d\n",
1324 vha->qla_stats.total_isp_aborts);
1327 static ssize_t
1328 qla24xx_84xx_fw_version_show(struct device *dev,
1329 struct device_attribute *attr, char *buf)
1331 int rval = QLA_SUCCESS;
1332 uint16_t status[2] = {0, 0};
1333 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1334 struct qla_hw_data *ha = vha->hw;
1336 if (!IS_QLA84XX(ha))
1337 return scnprintf(buf, PAGE_SIZE, "\n");
1339 if (ha->cs84xx->op_fw_version == 0)
1340 rval = qla84xx_verify_chip(vha, status);
1342 if ((rval == QLA_SUCCESS) && (status[0] == 0))
1343 return scnprintf(buf, PAGE_SIZE, "%u\n",
1344 (uint32_t)ha->cs84xx->op_fw_version);
1346 return scnprintf(buf, PAGE_SIZE, "\n");
1349 static ssize_t
1350 qla2x00_mpi_version_show(struct device *dev, struct device_attribute *attr,
1351 char *buf)
1353 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1354 struct qla_hw_data *ha = vha->hw;
1356 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha) &&
1357 !IS_QLA27XX(ha))
1358 return scnprintf(buf, PAGE_SIZE, "\n");
1360 return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
1361 ha->mpi_version[0], ha->mpi_version[1], ha->mpi_version[2],
1362 ha->mpi_capabilities);
1365 static ssize_t
1366 qla2x00_phy_version_show(struct device *dev, struct device_attribute *attr,
1367 char *buf)
1369 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1370 struct qla_hw_data *ha = vha->hw;
1372 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
1373 return scnprintf(buf, PAGE_SIZE, "\n");
1375 return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1376 ha->phy_version[0], ha->phy_version[1], ha->phy_version[2]);
1379 static ssize_t
1380 qla2x00_flash_block_size_show(struct device *dev,
1381 struct device_attribute *attr, char *buf)
1383 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1384 struct qla_hw_data *ha = vha->hw;
1386 return scnprintf(buf, PAGE_SIZE, "0x%x\n", ha->fdt_block_size);
1389 static ssize_t
1390 qla2x00_vlan_id_show(struct device *dev, struct device_attribute *attr,
1391 char *buf)
1393 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1395 if (!IS_CNA_CAPABLE(vha->hw))
1396 return scnprintf(buf, PAGE_SIZE, "\n");
1398 return scnprintf(buf, PAGE_SIZE, "%d\n", vha->fcoe_vlan_id);
1401 static ssize_t
1402 qla2x00_vn_port_mac_address_show(struct device *dev,
1403 struct device_attribute *attr, char *buf)
1405 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1407 if (!IS_CNA_CAPABLE(vha->hw))
1408 return scnprintf(buf, PAGE_SIZE, "\n");
1410 return scnprintf(buf, PAGE_SIZE, "%pMR\n", vha->fcoe_vn_port_mac);
1413 static ssize_t
1414 qla2x00_fabric_param_show(struct device *dev, struct device_attribute *attr,
1415 char *buf)
1417 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1419 return scnprintf(buf, PAGE_SIZE, "%d\n", vha->hw->switch_cap);
1422 static ssize_t
1423 qla2x00_thermal_temp_show(struct device *dev,
1424 struct device_attribute *attr, char *buf)
1426 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1427 uint16_t temp = 0;
1429 if (qla2x00_reset_active(vha)) {
1430 ql_log(ql_log_warn, vha, 0x70dc, "ISP reset active.\n");
1431 goto done;
1434 if (vha->hw->flags.eeh_busy) {
1435 ql_log(ql_log_warn, vha, 0x70dd, "PCI EEH busy.\n");
1436 goto done;
1439 if (qla2x00_get_thermal_temp(vha, &temp) == QLA_SUCCESS)
1440 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
1442 done:
1443 return scnprintf(buf, PAGE_SIZE, "\n");
1446 static ssize_t
1447 qla2x00_fw_state_show(struct device *dev, struct device_attribute *attr,
1448 char *buf)
1450 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1451 int rval = QLA_FUNCTION_FAILED;
1452 uint16_t state[6];
1453 uint32_t pstate;
1455 if (IS_QLAFX00(vha->hw)) {
1456 pstate = qlafx00_fw_state_show(dev, attr, buf);
1457 return scnprintf(buf, PAGE_SIZE, "0x%x\n", pstate);
1460 if (qla2x00_reset_active(vha))
1461 ql_log(ql_log_warn, vha, 0x707c,
1462 "ISP reset active.\n");
1463 else if (!vha->hw->flags.eeh_busy)
1464 rval = qla2x00_get_firmware_state(vha, state);
1465 if (rval != QLA_SUCCESS)
1466 memset(state, -1, sizeof(state));
1468 return scnprintf(buf, PAGE_SIZE, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1469 state[0], state[1], state[2], state[3], state[4], state[5]);
1472 static ssize_t
1473 qla2x00_diag_requests_show(struct device *dev,
1474 struct device_attribute *attr, char *buf)
1476 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1478 if (!IS_BIDI_CAPABLE(vha->hw))
1479 return scnprintf(buf, PAGE_SIZE, "\n");
1481 return scnprintf(buf, PAGE_SIZE, "%llu\n", vha->bidi_stats.io_count);
1484 static ssize_t
1485 qla2x00_diag_megabytes_show(struct device *dev,
1486 struct device_attribute *attr, char *buf)
1488 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1490 if (!IS_BIDI_CAPABLE(vha->hw))
1491 return scnprintf(buf, PAGE_SIZE, "\n");
1493 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1494 vha->bidi_stats.transfer_bytes >> 20);
1497 static ssize_t
1498 qla2x00_fw_dump_size_show(struct device *dev, struct device_attribute *attr,
1499 char *buf)
1501 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1502 struct qla_hw_data *ha = vha->hw;
1503 uint32_t size;
1505 if (!ha->fw_dumped)
1506 size = 0;
1507 else if (IS_P3P_TYPE(ha))
1508 size = ha->md_template_size + ha->md_dump_size;
1509 else
1510 size = ha->fw_dump_len;
1512 return scnprintf(buf, PAGE_SIZE, "%d\n", size);
1515 static ssize_t
1516 qla2x00_allow_cna_fw_dump_show(struct device *dev,
1517 struct device_attribute *attr, char *buf)
1519 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1521 if (!IS_P3P_TYPE(vha->hw))
1522 return scnprintf(buf, PAGE_SIZE, "\n");
1523 else
1524 return scnprintf(buf, PAGE_SIZE, "%s\n",
1525 vha->hw->allow_cna_fw_dump ? "true" : "false");
1528 static ssize_t
1529 qla2x00_allow_cna_fw_dump_store(struct device *dev,
1530 struct device_attribute *attr, const char *buf, size_t count)
1532 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1533 int val = 0;
1535 if (!IS_P3P_TYPE(vha->hw))
1536 return -EINVAL;
1538 if (sscanf(buf, "%d", &val) != 1)
1539 return -EINVAL;
1541 vha->hw->allow_cna_fw_dump = val != 0;
1543 return strlen(buf);
1546 static ssize_t
1547 qla2x00_pep_version_show(struct device *dev, struct device_attribute *attr,
1548 char *buf)
1550 scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1551 struct qla_hw_data *ha = vha->hw;
1553 if (!IS_QLA27XX(ha))
1554 return scnprintf(buf, PAGE_SIZE, "\n");
1556 return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1557 ha->pep_version[0], ha->pep_version[1], ha->pep_version[2]);
1560 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show, NULL);
1561 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
1562 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
1563 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
1564 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
1565 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
1566 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
1567 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
1568 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
1569 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
1570 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
1571 qla2x00_zio_timer_store);
1572 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
1573 qla2x00_beacon_store);
1574 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
1575 qla2x00_optrom_bios_version_show, NULL);
1576 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
1577 qla2x00_optrom_efi_version_show, NULL);
1578 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
1579 qla2x00_optrom_fcode_version_show, NULL);
1580 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
1581 NULL);
1582 static DEVICE_ATTR(optrom_gold_fw_version, S_IRUGO,
1583 qla2x00_optrom_gold_fw_version_show, NULL);
1584 static DEVICE_ATTR(84xx_fw_version, S_IRUGO, qla24xx_84xx_fw_version_show,
1585 NULL);
1586 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
1587 NULL);
1588 static DEVICE_ATTR(mpi_version, S_IRUGO, qla2x00_mpi_version_show, NULL);
1589 static DEVICE_ATTR(phy_version, S_IRUGO, qla2x00_phy_version_show, NULL);
1590 static DEVICE_ATTR(flash_block_size, S_IRUGO, qla2x00_flash_block_size_show,
1591 NULL);
1592 static DEVICE_ATTR(vlan_id, S_IRUGO, qla2x00_vlan_id_show, NULL);
1593 static DEVICE_ATTR(vn_port_mac_address, S_IRUGO,
1594 qla2x00_vn_port_mac_address_show, NULL);
1595 static DEVICE_ATTR(fabric_param, S_IRUGO, qla2x00_fabric_param_show, NULL);
1596 static DEVICE_ATTR(fw_state, S_IRUGO, qla2x00_fw_state_show, NULL);
1597 static DEVICE_ATTR(thermal_temp, S_IRUGO, qla2x00_thermal_temp_show, NULL);
1598 static DEVICE_ATTR(diag_requests, S_IRUGO, qla2x00_diag_requests_show, NULL);
1599 static DEVICE_ATTR(diag_megabytes, S_IRUGO, qla2x00_diag_megabytes_show, NULL);
1600 static DEVICE_ATTR(fw_dump_size, S_IRUGO, qla2x00_fw_dump_size_show, NULL);
1601 static DEVICE_ATTR(allow_cna_fw_dump, S_IRUGO | S_IWUSR,
1602 qla2x00_allow_cna_fw_dump_show,
1603 qla2x00_allow_cna_fw_dump_store);
1604 static DEVICE_ATTR(pep_version, S_IRUGO, qla2x00_pep_version_show, NULL);
1606 struct device_attribute *qla2x00_host_attrs[] = {
1607 &dev_attr_driver_version,
1608 &dev_attr_fw_version,
1609 &dev_attr_serial_num,
1610 &dev_attr_isp_name,
1611 &dev_attr_isp_id,
1612 &dev_attr_model_name,
1613 &dev_attr_model_desc,
1614 &dev_attr_pci_info,
1615 &dev_attr_link_state,
1616 &dev_attr_zio,
1617 &dev_attr_zio_timer,
1618 &dev_attr_beacon,
1619 &dev_attr_optrom_bios_version,
1620 &dev_attr_optrom_efi_version,
1621 &dev_attr_optrom_fcode_version,
1622 &dev_attr_optrom_fw_version,
1623 &dev_attr_84xx_fw_version,
1624 &dev_attr_total_isp_aborts,
1625 &dev_attr_mpi_version,
1626 &dev_attr_phy_version,
1627 &dev_attr_flash_block_size,
1628 &dev_attr_vlan_id,
1629 &dev_attr_vn_port_mac_address,
1630 &dev_attr_fabric_param,
1631 &dev_attr_fw_state,
1632 &dev_attr_optrom_gold_fw_version,
1633 &dev_attr_thermal_temp,
1634 &dev_attr_diag_requests,
1635 &dev_attr_diag_megabytes,
1636 &dev_attr_fw_dump_size,
1637 &dev_attr_allow_cna_fw_dump,
1638 &dev_attr_pep_version,
1639 NULL,
1642 /* Host attributes. */
1644 static void
1645 qla2x00_get_host_port_id(struct Scsi_Host *shost)
1647 scsi_qla_host_t *vha = shost_priv(shost);
1649 fc_host_port_id(shost) = vha->d_id.b.domain << 16 |
1650 vha->d_id.b.area << 8 | vha->d_id.b.al_pa;
1653 static void
1654 qla2x00_get_host_speed(struct Scsi_Host *shost)
1656 struct qla_hw_data *ha = ((struct scsi_qla_host *)
1657 (shost_priv(shost)))->hw;
1658 u32 speed = FC_PORTSPEED_UNKNOWN;
1660 if (IS_QLAFX00(ha)) {
1661 qlafx00_get_host_speed(shost);
1662 return;
1665 switch (ha->link_data_rate) {
1666 case PORT_SPEED_1GB:
1667 speed = FC_PORTSPEED_1GBIT;
1668 break;
1669 case PORT_SPEED_2GB:
1670 speed = FC_PORTSPEED_2GBIT;
1671 break;
1672 case PORT_SPEED_4GB:
1673 speed = FC_PORTSPEED_4GBIT;
1674 break;
1675 case PORT_SPEED_8GB:
1676 speed = FC_PORTSPEED_8GBIT;
1677 break;
1678 case PORT_SPEED_10GB:
1679 speed = FC_PORTSPEED_10GBIT;
1680 break;
1681 case PORT_SPEED_16GB:
1682 speed = FC_PORTSPEED_16GBIT;
1683 break;
1684 case PORT_SPEED_32GB:
1685 speed = FC_PORTSPEED_32GBIT;
1686 break;
1688 fc_host_speed(shost) = speed;
1691 static void
1692 qla2x00_get_host_port_type(struct Scsi_Host *shost)
1694 scsi_qla_host_t *vha = shost_priv(shost);
1695 uint32_t port_type = FC_PORTTYPE_UNKNOWN;
1697 if (vha->vp_idx) {
1698 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
1699 return;
1701 switch (vha->hw->current_topology) {
1702 case ISP_CFG_NL:
1703 port_type = FC_PORTTYPE_LPORT;
1704 break;
1705 case ISP_CFG_FL:
1706 port_type = FC_PORTTYPE_NLPORT;
1707 break;
1708 case ISP_CFG_N:
1709 port_type = FC_PORTTYPE_PTP;
1710 break;
1711 case ISP_CFG_F:
1712 port_type = FC_PORTTYPE_NPORT;
1713 break;
1715 fc_host_port_type(shost) = port_type;
1718 static void
1719 qla2x00_get_starget_node_name(struct scsi_target *starget)
1721 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1722 scsi_qla_host_t *vha = shost_priv(host);
1723 fc_port_t *fcport;
1724 u64 node_name = 0;
1726 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1727 if (fcport->rport &&
1728 starget->id == fcport->rport->scsi_target_id) {
1729 node_name = wwn_to_u64(fcport->node_name);
1730 break;
1734 fc_starget_node_name(starget) = node_name;
1737 static void
1738 qla2x00_get_starget_port_name(struct scsi_target *starget)
1740 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1741 scsi_qla_host_t *vha = shost_priv(host);
1742 fc_port_t *fcport;
1743 u64 port_name = 0;
1745 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1746 if (fcport->rport &&
1747 starget->id == fcport->rport->scsi_target_id) {
1748 port_name = wwn_to_u64(fcport->port_name);
1749 break;
1753 fc_starget_port_name(starget) = port_name;
1756 static void
1757 qla2x00_get_starget_port_id(struct scsi_target *starget)
1759 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
1760 scsi_qla_host_t *vha = shost_priv(host);
1761 fc_port_t *fcport;
1762 uint32_t port_id = ~0U;
1764 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1765 if (fcport->rport &&
1766 starget->id == fcport->rport->scsi_target_id) {
1767 port_id = fcport->d_id.b.domain << 16 |
1768 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
1769 break;
1773 fc_starget_port_id(starget) = port_id;
1776 static void
1777 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
1779 if (timeout)
1780 rport->dev_loss_tmo = timeout;
1781 else
1782 rport->dev_loss_tmo = 1;
1785 static void
1786 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
1788 struct Scsi_Host *host = rport_to_shost(rport);
1789 fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1790 unsigned long flags;
1792 if (!fcport)
1793 return;
1795 /* Now that the rport has been deleted, set the fcport state to
1796 FCS_DEVICE_DEAD */
1797 qla2x00_set_fcport_state(fcport, FCS_DEVICE_DEAD);
1800 * Transport has effectively 'deleted' the rport, clear
1801 * all local references.
1803 spin_lock_irqsave(host->host_lock, flags);
1804 fcport->rport = fcport->drport = NULL;
1805 *((fc_port_t **)rport->dd_data) = NULL;
1806 spin_unlock_irqrestore(host->host_lock, flags);
1808 if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
1809 return;
1811 if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
1812 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
1813 return;
1817 static void
1818 qla2x00_terminate_rport_io(struct fc_rport *rport)
1820 fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
1822 if (!fcport)
1823 return;
1825 if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
1826 return;
1828 if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
1829 qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
1830 return;
1833 * At this point all fcport's software-states are cleared. Perform any
1834 * final cleanup of firmware resources (PCBs and XCBs).
1836 if (fcport->loop_id != FC_NO_LOOP_ID) {
1837 if (IS_FWI2_CAPABLE(fcport->vha->hw))
1838 fcport->vha->hw->isp_ops->fabric_logout(fcport->vha,
1839 fcport->loop_id, fcport->d_id.b.domain,
1840 fcport->d_id.b.area, fcport->d_id.b.al_pa);
1841 else
1842 qla2x00_port_logout(fcport->vha, fcport);
1846 static int
1847 qla2x00_issue_lip(struct Scsi_Host *shost)
1849 scsi_qla_host_t *vha = shost_priv(shost);
1851 if (IS_QLAFX00(vha->hw))
1852 return 0;
1854 qla2x00_loop_reset(vha);
1855 return 0;
1858 static struct fc_host_statistics *
1859 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
1861 scsi_qla_host_t *vha = shost_priv(shost);
1862 struct qla_hw_data *ha = vha->hw;
1863 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
1864 int rval;
1865 struct link_statistics *stats;
1866 dma_addr_t stats_dma;
1867 struct fc_host_statistics *pfc_host_stat;
1869 pfc_host_stat = &vha->fc_host_stat;
1870 memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
1872 if (IS_QLAFX00(vha->hw))
1873 goto done;
1875 if (test_bit(UNLOADING, &vha->dpc_flags))
1876 goto done;
1878 if (unlikely(pci_channel_offline(ha->pdev)))
1879 goto done;
1881 if (qla2x00_reset_active(vha))
1882 goto done;
1884 stats = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &stats_dma);
1885 if (stats == NULL) {
1886 ql_log(ql_log_warn, vha, 0x707d,
1887 "Failed to allocate memory for stats.\n");
1888 goto done;
1890 memset(stats, 0, DMA_POOL_SIZE);
1892 rval = QLA_FUNCTION_FAILED;
1893 if (IS_FWI2_CAPABLE(ha)) {
1894 rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma);
1895 } else if (atomic_read(&base_vha->loop_state) == LOOP_READY &&
1896 !ha->dpc_active) {
1897 /* Must be in a 'READY' state for statistics retrieval. */
1898 rval = qla2x00_get_link_status(base_vha, base_vha->loop_id,
1899 stats, stats_dma);
1902 if (rval != QLA_SUCCESS)
1903 goto done_free;
1905 pfc_host_stat->link_failure_count = stats->link_fail_cnt;
1906 pfc_host_stat->loss_of_sync_count = stats->loss_sync_cnt;
1907 pfc_host_stat->loss_of_signal_count = stats->loss_sig_cnt;
1908 pfc_host_stat->prim_seq_protocol_err_count = stats->prim_seq_err_cnt;
1909 pfc_host_stat->invalid_tx_word_count = stats->inval_xmit_word_cnt;
1910 pfc_host_stat->invalid_crc_count = stats->inval_crc_cnt;
1911 if (IS_FWI2_CAPABLE(ha)) {
1912 pfc_host_stat->lip_count = stats->lip_cnt;
1913 pfc_host_stat->tx_frames = stats->tx_frames;
1914 pfc_host_stat->rx_frames = stats->rx_frames;
1915 pfc_host_stat->dumped_frames = stats->discarded_frames;
1916 pfc_host_stat->nos_count = stats->nos_rcvd;
1917 pfc_host_stat->error_frames =
1918 stats->dropped_frames + stats->discarded_frames;
1919 pfc_host_stat->rx_words = vha->qla_stats.input_bytes;
1920 pfc_host_stat->tx_words = vha->qla_stats.output_bytes;
1922 pfc_host_stat->fcp_control_requests = vha->qla_stats.control_requests;
1923 pfc_host_stat->fcp_input_requests = vha->qla_stats.input_requests;
1924 pfc_host_stat->fcp_output_requests = vha->qla_stats.output_requests;
1925 pfc_host_stat->fcp_input_megabytes = vha->qla_stats.input_bytes >> 20;
1926 pfc_host_stat->fcp_output_megabytes = vha->qla_stats.output_bytes >> 20;
1927 pfc_host_stat->seconds_since_last_reset =
1928 get_jiffies_64() - vha->qla_stats.jiffies_at_last_reset;
1929 do_div(pfc_host_stat->seconds_since_last_reset, HZ);
1931 done_free:
1932 dma_pool_free(ha->s_dma_pool, stats, stats_dma);
1933 done:
1934 return pfc_host_stat;
1937 static void
1938 qla2x00_reset_host_stats(struct Scsi_Host *shost)
1940 scsi_qla_host_t *vha = shost_priv(shost);
1942 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1944 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
1947 static void
1948 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
1950 scsi_qla_host_t *vha = shost_priv(shost);
1952 qla2x00_get_sym_node_name(vha, fc_host_symbolic_name(shost),
1953 sizeof(fc_host_symbolic_name(shost)));
1956 static void
1957 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
1959 scsi_qla_host_t *vha = shost_priv(shost);
1961 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
1964 static void
1965 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
1967 scsi_qla_host_t *vha = shost_priv(shost);
1968 uint8_t node_name[WWN_SIZE] = { 0xFF, 0xFF, 0xFF, 0xFF, \
1969 0xFF, 0xFF, 0xFF, 0xFF};
1970 u64 fabric_name = wwn_to_u64(node_name);
1972 if (vha->device_flags & SWITCH_FOUND)
1973 fabric_name = wwn_to_u64(vha->fabric_node_name);
1975 fc_host_fabric_name(shost) = fabric_name;
1978 static void
1979 qla2x00_get_host_port_state(struct Scsi_Host *shost)
1981 scsi_qla_host_t *vha = shost_priv(shost);
1982 struct scsi_qla_host *base_vha = pci_get_drvdata(vha->hw->pdev);
1984 if (!base_vha->flags.online) {
1985 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1986 return;
1989 switch (atomic_read(&base_vha->loop_state)) {
1990 case LOOP_UPDATE:
1991 fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
1992 break;
1993 case LOOP_DOWN:
1994 if (test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags))
1995 fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
1996 else
1997 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
1998 break;
1999 case LOOP_DEAD:
2000 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
2001 break;
2002 case LOOP_READY:
2003 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
2004 break;
2005 default:
2006 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
2007 break;
2011 static int
2012 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
2014 int ret = 0;
2015 uint8_t qos = 0;
2016 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
2017 scsi_qla_host_t *vha = NULL;
2018 struct qla_hw_data *ha = base_vha->hw;
2019 uint16_t options = 0;
2020 int cnt;
2021 struct req_que *req = ha->req_q_map[0];
2023 ret = qla24xx_vport_create_req_sanity_check(fc_vport);
2024 if (ret) {
2025 ql_log(ql_log_warn, vha, 0x707e,
2026 "Vport sanity check failed, status %x\n", ret);
2027 return (ret);
2030 vha = qla24xx_create_vhost(fc_vport);
2031 if (vha == NULL) {
2032 ql_log(ql_log_warn, vha, 0x707f, "Vport create host failed.\n");
2033 return FC_VPORT_FAILED;
2035 if (disable) {
2036 atomic_set(&vha->vp_state, VP_OFFLINE);
2037 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
2038 } else
2039 atomic_set(&vha->vp_state, VP_FAILED);
2041 /* ready to create vport */
2042 ql_log(ql_log_info, vha, 0x7080,
2043 "VP entry id %d assigned.\n", vha->vp_idx);
2045 /* initialized vport states */
2046 atomic_set(&vha->loop_state, LOOP_DOWN);
2047 vha->vp_err_state= VP_ERR_PORTDWN;
2048 vha->vp_prev_err_state= VP_ERR_UNKWN;
2049 /* Check if physical ha port is Up */
2050 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
2051 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
2052 /* Don't retry or attempt login of this virtual port */
2053 ql_dbg(ql_dbg_user, vha, 0x7081,
2054 "Vport loop state is not UP.\n");
2055 atomic_set(&vha->loop_state, LOOP_DEAD);
2056 if (!disable)
2057 fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
2060 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
2061 if (ha->fw_attributes & BIT_4) {
2062 int prot = 0, guard;
2063 vha->flags.difdix_supported = 1;
2064 ql_dbg(ql_dbg_user, vha, 0x7082,
2065 "Registered for DIF/DIX type 1 and 3 protection.\n");
2066 if (ql2xenabledif == 1)
2067 prot = SHOST_DIX_TYPE0_PROTECTION;
2068 scsi_host_set_prot(vha->host,
2069 prot | SHOST_DIF_TYPE1_PROTECTION
2070 | SHOST_DIF_TYPE2_PROTECTION
2071 | SHOST_DIF_TYPE3_PROTECTION
2072 | SHOST_DIX_TYPE1_PROTECTION
2073 | SHOST_DIX_TYPE2_PROTECTION
2074 | SHOST_DIX_TYPE3_PROTECTION);
2076 guard = SHOST_DIX_GUARD_CRC;
2078 if (IS_PI_IPGUARD_CAPABLE(ha) &&
2079 (ql2xenabledif > 1 || IS_PI_DIFB_DIX0_CAPABLE(ha)))
2080 guard |= SHOST_DIX_GUARD_IP;
2082 scsi_host_set_guard(vha->host, guard);
2083 } else
2084 vha->flags.difdix_supported = 0;
2087 if (scsi_add_host_with_dma(vha->host, &fc_vport->dev,
2088 &ha->pdev->dev)) {
2089 ql_dbg(ql_dbg_user, vha, 0x7083,
2090 "scsi_add_host failure for VP[%d].\n", vha->vp_idx);
2091 goto vport_create_failed_2;
2094 /* initialize attributes */
2095 fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
2096 fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
2097 fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
2098 fc_host_supported_classes(vha->host) =
2099 fc_host_supported_classes(base_vha->host);
2100 fc_host_supported_speeds(vha->host) =
2101 fc_host_supported_speeds(base_vha->host);
2103 qlt_vport_create(vha, ha);
2104 qla24xx_vport_disable(fc_vport, disable);
2106 if (ha->flags.cpu_affinity_enabled) {
2107 req = ha->req_q_map[1];
2108 ql_dbg(ql_dbg_multiq, vha, 0xc000,
2109 "Request queue %p attached with "
2110 "VP[%d], cpu affinity =%d\n",
2111 req, vha->vp_idx, ha->flags.cpu_affinity_enabled);
2112 goto vport_queue;
2113 } else if (ql2xmaxqueues == 1 || !ha->npiv_info)
2114 goto vport_queue;
2115 /* Create a request queue in QoS mode for the vport */
2116 for (cnt = 0; cnt < ha->nvram_npiv_size; cnt++) {
2117 if (memcmp(ha->npiv_info[cnt].port_name, vha->port_name, 8) == 0
2118 && memcmp(ha->npiv_info[cnt].node_name, vha->node_name,
2119 8) == 0) {
2120 qos = ha->npiv_info[cnt].q_qos;
2121 break;
2125 if (qos) {
2126 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, 0,
2127 qos);
2128 if (!ret)
2129 ql_log(ql_log_warn, vha, 0x7084,
2130 "Can't create request queue for VP[%d]\n",
2131 vha->vp_idx);
2132 else {
2133 ql_dbg(ql_dbg_multiq, vha, 0xc001,
2134 "Request Que:%d Q0s: %d) created for VP[%d]\n",
2135 ret, qos, vha->vp_idx);
2136 ql_dbg(ql_dbg_user, vha, 0x7085,
2137 "Request Que:%d Q0s: %d) created for VP[%d]\n",
2138 ret, qos, vha->vp_idx);
2139 req = ha->req_q_map[ret];
2143 vport_queue:
2144 vha->req = req;
2145 return 0;
2147 vport_create_failed_2:
2148 qla24xx_disable_vp(vha);
2149 qla24xx_deallocate_vp_id(vha);
2150 scsi_host_put(vha->host);
2151 return FC_VPORT_FAILED;
2154 static int
2155 qla24xx_vport_delete(struct fc_vport *fc_vport)
2157 scsi_qla_host_t *vha = fc_vport->dd_data;
2158 struct qla_hw_data *ha = vha->hw;
2159 uint16_t id = vha->vp_idx;
2161 while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
2162 test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags))
2163 msleep(1000);
2165 qla24xx_disable_vp(vha);
2167 vha->flags.delete_progress = 1;
2169 qlt_remove_target(ha, vha);
2171 fc_remove_host(vha->host);
2173 scsi_remove_host(vha->host);
2175 /* Allow timer to run to drain queued items, when removing vp */
2176 qla24xx_deallocate_vp_id(vha);
2178 if (vha->timer_active) {
2179 qla2x00_vp_stop_timer(vha);
2180 ql_dbg(ql_dbg_user, vha, 0x7086,
2181 "Timer for the VP[%d] has stopped\n", vha->vp_idx);
2184 BUG_ON(atomic_read(&vha->vref_count));
2186 qla2x00_free_fcports(vha);
2188 mutex_lock(&ha->vport_lock);
2189 ha->cur_vport_count--;
2190 clear_bit(vha->vp_idx, ha->vp_idx_map);
2191 mutex_unlock(&ha->vport_lock);
2193 if (vha->req->id && !ha->flags.cpu_affinity_enabled) {
2194 if (qla25xx_delete_req_que(vha, vha->req) != QLA_SUCCESS)
2195 ql_log(ql_log_warn, vha, 0x7087,
2196 "Queue delete failed.\n");
2199 ql_log(ql_log_info, vha, 0x7088, "VP[%d] deleted.\n", id);
2200 scsi_host_put(vha->host);
2201 return 0;
2204 static int
2205 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
2207 scsi_qla_host_t *vha = fc_vport->dd_data;
2209 if (disable)
2210 qla24xx_disable_vp(vha);
2211 else
2212 qla24xx_enable_vp(vha);
2214 return 0;
2217 struct fc_function_template qla2xxx_transport_functions = {
2219 .show_host_node_name = 1,
2220 .show_host_port_name = 1,
2221 .show_host_supported_classes = 1,
2222 .show_host_supported_speeds = 1,
2224 .get_host_port_id = qla2x00_get_host_port_id,
2225 .show_host_port_id = 1,
2226 .get_host_speed = qla2x00_get_host_speed,
2227 .show_host_speed = 1,
2228 .get_host_port_type = qla2x00_get_host_port_type,
2229 .show_host_port_type = 1,
2230 .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
2231 .show_host_symbolic_name = 1,
2232 .set_host_system_hostname = qla2x00_set_host_system_hostname,
2233 .show_host_system_hostname = 1,
2234 .get_host_fabric_name = qla2x00_get_host_fabric_name,
2235 .show_host_fabric_name = 1,
2236 .get_host_port_state = qla2x00_get_host_port_state,
2237 .show_host_port_state = 1,
2239 .dd_fcrport_size = sizeof(struct fc_port *),
2240 .show_rport_supported_classes = 1,
2242 .get_starget_node_name = qla2x00_get_starget_node_name,
2243 .show_starget_node_name = 1,
2244 .get_starget_port_name = qla2x00_get_starget_port_name,
2245 .show_starget_port_name = 1,
2246 .get_starget_port_id = qla2x00_get_starget_port_id,
2247 .show_starget_port_id = 1,
2249 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
2250 .show_rport_dev_loss_tmo = 1,
2252 .issue_fc_host_lip = qla2x00_issue_lip,
2253 .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
2254 .terminate_rport_io = qla2x00_terminate_rport_io,
2255 .get_fc_host_stats = qla2x00_get_fc_host_stats,
2256 .reset_fc_host_stats = qla2x00_reset_host_stats,
2258 .vport_create = qla24xx_vport_create,
2259 .vport_disable = qla24xx_vport_disable,
2260 .vport_delete = qla24xx_vport_delete,
2261 .bsg_request = qla24xx_bsg_request,
2262 .bsg_timeout = qla24xx_bsg_timeout,
2265 struct fc_function_template qla2xxx_transport_vport_functions = {
2267 .show_host_node_name = 1,
2268 .show_host_port_name = 1,
2269 .show_host_supported_classes = 1,
2271 .get_host_port_id = qla2x00_get_host_port_id,
2272 .show_host_port_id = 1,
2273 .get_host_speed = qla2x00_get_host_speed,
2274 .show_host_speed = 1,
2275 .get_host_port_type = qla2x00_get_host_port_type,
2276 .show_host_port_type = 1,
2277 .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
2278 .show_host_symbolic_name = 1,
2279 .set_host_system_hostname = qla2x00_set_host_system_hostname,
2280 .show_host_system_hostname = 1,
2281 .get_host_fabric_name = qla2x00_get_host_fabric_name,
2282 .show_host_fabric_name = 1,
2283 .get_host_port_state = qla2x00_get_host_port_state,
2284 .show_host_port_state = 1,
2286 .dd_fcrport_size = sizeof(struct fc_port *),
2287 .show_rport_supported_classes = 1,
2289 .get_starget_node_name = qla2x00_get_starget_node_name,
2290 .show_starget_node_name = 1,
2291 .get_starget_port_name = qla2x00_get_starget_port_name,
2292 .show_starget_port_name = 1,
2293 .get_starget_port_id = qla2x00_get_starget_port_id,
2294 .show_starget_port_id = 1,
2296 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
2297 .show_rport_dev_loss_tmo = 1,
2299 .issue_fc_host_lip = qla2x00_issue_lip,
2300 .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
2301 .terminate_rport_io = qla2x00_terminate_rport_io,
2302 .get_fc_host_stats = qla2x00_get_fc_host_stats,
2303 .reset_fc_host_stats = qla2x00_reset_host_stats,
2305 .bsg_request = qla24xx_bsg_request,
2306 .bsg_timeout = qla24xx_bsg_timeout,
2309 void
2310 qla2x00_init_host_attr(scsi_qla_host_t *vha)
2312 struct qla_hw_data *ha = vha->hw;
2313 u32 speed = FC_PORTSPEED_UNKNOWN;
2315 fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
2316 fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
2317 fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
2318 fc_host_supported_classes(vha->host) = ha->tgt.enable_class_2 ?
2319 (FC_COS_CLASS2|FC_COS_CLASS3) : FC_COS_CLASS3;
2320 fc_host_max_npiv_vports(vha->host) = ha->max_npiv_vports;
2321 fc_host_npiv_vports_inuse(vha->host) = ha->cur_vport_count;
2323 if (IS_CNA_CAPABLE(ha))
2324 speed = FC_PORTSPEED_10GBIT;
2325 else if (IS_QLA2031(ha))
2326 speed = FC_PORTSPEED_16GBIT | FC_PORTSPEED_8GBIT |
2327 FC_PORTSPEED_4GBIT;
2328 else if (IS_QLA25XX(ha))
2329 speed = FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT |
2330 FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
2331 else if (IS_QLA24XX_TYPE(ha))
2332 speed = FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
2333 FC_PORTSPEED_1GBIT;
2334 else if (IS_QLA23XX(ha))
2335 speed = FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
2336 else if (IS_QLAFX00(ha))
2337 speed = FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT |
2338 FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
2339 else if (IS_QLA27XX(ha))
2340 speed = FC_PORTSPEED_32GBIT | FC_PORTSPEED_16GBIT |
2341 FC_PORTSPEED_8GBIT;
2342 else
2343 speed = FC_PORTSPEED_1GBIT;
2344 fc_host_supported_speeds(vha->host) = speed;