xref: /openbmc/linux/drivers/scsi/qla2xxx/qla_attr.c (revision 92cc94ad)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic Fibre Channel HBA Driver
4  * Copyright (c)  2003-2014 QLogic Corporation
5  */
6 #include "qla_def.h"
7 #include "qla_target.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 	int rval = 0;
27 
28 	if (!(ha->fw_dump_reading || ha->mctp_dump_reading ||
29 	      ha->mpi_fw_dump_reading))
30 		return 0;
31 
32 	mutex_lock(&ha->optrom_mutex);
33 	if (IS_P3P_TYPE(ha)) {
34 		if (off < ha->md_template_size) {
35 			rval = memory_read_from_buffer(buf, count,
36 			    &off, ha->md_tmplt_hdr, ha->md_template_size);
37 		} else {
38 			off -= ha->md_template_size;
39 			rval = memory_read_from_buffer(buf, count,
40 			    &off, ha->md_dump, ha->md_dump_size);
41 		}
42 	} else if (ha->mctp_dumped && ha->mctp_dump_reading) {
43 		rval = memory_read_from_buffer(buf, count, &off, ha->mctp_dump,
44 		    MCTP_DUMP_SIZE);
45 	} else if (ha->mpi_fw_dumped && ha->mpi_fw_dump_reading) {
46 		rval = memory_read_from_buffer(buf, count, &off,
47 					       ha->mpi_fw_dump,
48 					       ha->mpi_fw_dump_len);
49 	} else if (ha->fw_dump_reading) {
50 		rval = memory_read_from_buffer(buf, count, &off, ha->fw_dump,
51 					ha->fw_dump_len);
52 	} else {
53 		rval = 0;
54 	}
55 	mutex_unlock(&ha->optrom_mutex);
56 	return rval;
57 }
58 
59 static ssize_t
60 qla2x00_sysfs_write_fw_dump(struct file *filp, struct kobject *kobj,
61 			    struct bin_attribute *bin_attr,
62 			    char *buf, loff_t off, size_t count)
63 {
64 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
65 	    struct device, kobj)));
66 	struct qla_hw_data *ha = vha->hw;
67 	int reading;
68 
69 	if (off != 0)
70 		return (0);
71 
72 	reading = simple_strtol(buf, NULL, 10);
73 	switch (reading) {
74 	case 0:
75 		if (!ha->fw_dump_reading)
76 			break;
77 
78 		ql_log(ql_log_info, vha, 0x705d,
79 		    "Firmware dump cleared on (%ld).\n", vha->host_no);
80 
81 		if (IS_P3P_TYPE(ha)) {
82 			qla82xx_md_free(vha);
83 			qla82xx_md_prep(vha);
84 		}
85 		ha->fw_dump_reading = 0;
86 		ha->fw_dumped = false;
87 		break;
88 	case 1:
89 		if (ha->fw_dumped && !ha->fw_dump_reading) {
90 			ha->fw_dump_reading = 1;
91 
92 			ql_log(ql_log_info, vha, 0x705e,
93 			    "Raw firmware dump ready for read on (%ld).\n",
94 			    vha->host_no);
95 		}
96 		break;
97 	case 2:
98 		qla2x00_alloc_fw_dump(vha);
99 		break;
100 	case 3:
101 		if (IS_QLA82XX(ha)) {
102 			qla82xx_idc_lock(ha);
103 			qla82xx_set_reset_owner(vha);
104 			qla82xx_idc_unlock(ha);
105 		} else if (IS_QLA8044(ha)) {
106 			qla8044_idc_lock(ha);
107 			qla82xx_set_reset_owner(vha);
108 			qla8044_idc_unlock(ha);
109 		} else {
110 			qla2x00_system_error(vha);
111 		}
112 		break;
113 	case 4:
114 		if (IS_P3P_TYPE(ha)) {
115 			if (ha->md_tmplt_hdr)
116 				ql_dbg(ql_dbg_user, vha, 0x705b,
117 				    "MiniDump supported with this firmware.\n");
118 			else
119 				ql_dbg(ql_dbg_user, vha, 0x709d,
120 				    "MiniDump not supported with this firmware.\n");
121 		}
122 		break;
123 	case 5:
124 		if (IS_P3P_TYPE(ha))
125 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
126 		break;
127 	case 6:
128 		if (!ha->mctp_dump_reading)
129 			break;
130 		ql_log(ql_log_info, vha, 0x70c1,
131 		    "MCTP dump cleared on (%ld).\n", vha->host_no);
132 		ha->mctp_dump_reading = 0;
133 		ha->mctp_dumped = 0;
134 		break;
135 	case 7:
136 		if (ha->mctp_dumped && !ha->mctp_dump_reading) {
137 			ha->mctp_dump_reading = 1;
138 			ql_log(ql_log_info, vha, 0x70c2,
139 			    "Raw mctp dump ready for read on (%ld).\n",
140 			    vha->host_no);
141 		}
142 		break;
143 	case 8:
144 		if (!ha->mpi_fw_dump_reading)
145 			break;
146 		ql_log(ql_log_info, vha, 0x70e7,
147 		       "MPI firmware dump cleared on (%ld).\n", vha->host_no);
148 		ha->mpi_fw_dump_reading = 0;
149 		ha->mpi_fw_dumped = 0;
150 		break;
151 	case 9:
152 		if (ha->mpi_fw_dumped && !ha->mpi_fw_dump_reading) {
153 			ha->mpi_fw_dump_reading = 1;
154 			ql_log(ql_log_info, vha, 0x70e8,
155 			       "Raw MPI firmware dump ready for read on (%ld).\n",
156 			       vha->host_no);
157 		}
158 		break;
159 	case 10:
160 		if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
161 			ql_log(ql_log_info, vha, 0x70e9,
162 			       "Issuing MPI firmware dump on host#%ld.\n",
163 			       vha->host_no);
164 			ha->isp_ops->mpi_fw_dump(vha, 0);
165 		}
166 		break;
167 	}
168 	return count;
169 }
170 
171 static struct bin_attribute sysfs_fw_dump_attr = {
172 	.attr = {
173 		.name = "fw_dump",
174 		.mode = S_IRUSR | S_IWUSR,
175 	},
176 	.size = 0,
177 	.read = qla2x00_sysfs_read_fw_dump,
178 	.write = qla2x00_sysfs_write_fw_dump,
179 };
180 
181 static ssize_t
182 qla2x00_sysfs_read_nvram(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 	uint32_t faddr;
190 	struct active_regions active_regions = { };
191 
192 	if (!capable(CAP_SYS_ADMIN))
193 		return 0;
194 
195 	mutex_lock(&ha->optrom_mutex);
196 	if (qla2x00_chip_is_down(vha)) {
197 		mutex_unlock(&ha->optrom_mutex);
198 		return -EAGAIN;
199 	}
200 
201 	if (!IS_NOCACHE_VPD_TYPE(ha)) {
202 		mutex_unlock(&ha->optrom_mutex);
203 		goto skip;
204 	}
205 
206 	faddr = ha->flt_region_nvram;
207 	if (IS_QLA28XX(ha)) {
208 		qla28xx_get_aux_images(vha, &active_regions);
209 		if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
210 			faddr = ha->flt_region_nvram_sec;
211 	}
212 	ha->isp_ops->read_optrom(vha, ha->nvram, faddr << 2, ha->nvram_size);
213 
214 	mutex_unlock(&ha->optrom_mutex);
215 
216 skip:
217 	return memory_read_from_buffer(buf, count, &off, ha->nvram,
218 					ha->nvram_size);
219 }
220 
221 static ssize_t
222 qla2x00_sysfs_write_nvram(struct file *filp, struct kobject *kobj,
223 			  struct bin_attribute *bin_attr,
224 			  char *buf, loff_t off, size_t count)
225 {
226 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
227 	    struct device, kobj)));
228 	struct qla_hw_data *ha = vha->hw;
229 	uint16_t	cnt;
230 
231 	if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size ||
232 	    !ha->isp_ops->write_nvram)
233 		return -EINVAL;
234 
235 	/* Checksum NVRAM. */
236 	if (IS_FWI2_CAPABLE(ha)) {
237 		__le32 *iter = (__force __le32 *)buf;
238 		uint32_t chksum;
239 
240 		chksum = 0;
241 		for (cnt = 0; cnt < ((count >> 2) - 1); cnt++, iter++)
242 			chksum += le32_to_cpu(*iter);
243 		chksum = ~chksum + 1;
244 		*iter = cpu_to_le32(chksum);
245 	} else {
246 		uint8_t *iter;
247 		uint8_t chksum;
248 
249 		iter = (uint8_t *)buf;
250 		chksum = 0;
251 		for (cnt = 0; cnt < count - 1; cnt++)
252 			chksum += *iter++;
253 		chksum = ~chksum + 1;
254 		*iter = chksum;
255 	}
256 
257 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
258 		ql_log(ql_log_warn, vha, 0x705f,
259 		    "HBA not online, failing NVRAM update.\n");
260 		return -EAGAIN;
261 	}
262 
263 	mutex_lock(&ha->optrom_mutex);
264 	if (qla2x00_chip_is_down(vha)) {
265 		mutex_unlock(&ha->optrom_mutex);
266 		return -EAGAIN;
267 	}
268 
269 	/* Write NVRAM. */
270 	ha->isp_ops->write_nvram(vha, buf, ha->nvram_base, count);
271 	ha->isp_ops->read_nvram(vha, ha->nvram, ha->nvram_base,
272 	    count);
273 	mutex_unlock(&ha->optrom_mutex);
274 
275 	ql_dbg(ql_dbg_user, vha, 0x7060,
276 	    "Setting ISP_ABORT_NEEDED\n");
277 	/* NVRAM settings take effect immediately. */
278 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
279 	qla2xxx_wake_dpc(vha);
280 	qla2x00_wait_for_chip_reset(vha);
281 
282 	return count;
283 }
284 
285 static struct bin_attribute sysfs_nvram_attr = {
286 	.attr = {
287 		.name = "nvram",
288 		.mode = S_IRUSR | S_IWUSR,
289 	},
290 	.size = 512,
291 	.read = qla2x00_sysfs_read_nvram,
292 	.write = qla2x00_sysfs_write_nvram,
293 };
294 
295 static ssize_t
296 qla2x00_sysfs_read_optrom(struct file *filp, struct kobject *kobj,
297 			  struct bin_attribute *bin_attr,
298 			  char *buf, loff_t off, size_t count)
299 {
300 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
301 	    struct device, kobj)));
302 	struct qla_hw_data *ha = vha->hw;
303 	ssize_t rval = 0;
304 
305 	mutex_lock(&ha->optrom_mutex);
306 
307 	if (ha->optrom_state != QLA_SREADING)
308 		goto out;
309 
310 	rval = memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
311 	    ha->optrom_region_size);
312 
313 out:
314 	mutex_unlock(&ha->optrom_mutex);
315 
316 	return rval;
317 }
318 
319 static ssize_t
320 qla2x00_sysfs_write_optrom(struct file *filp, struct kobject *kobj,
321 			   struct bin_attribute *bin_attr,
322 			   char *buf, loff_t off, size_t count)
323 {
324 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
325 	    struct device, kobj)));
326 	struct qla_hw_data *ha = vha->hw;
327 
328 	mutex_lock(&ha->optrom_mutex);
329 
330 	if (ha->optrom_state != QLA_SWRITING) {
331 		mutex_unlock(&ha->optrom_mutex);
332 		return -EINVAL;
333 	}
334 	if (off > ha->optrom_region_size) {
335 		mutex_unlock(&ha->optrom_mutex);
336 		return -ERANGE;
337 	}
338 	if (off + count > ha->optrom_region_size)
339 		count = ha->optrom_region_size - off;
340 
341 	memcpy(&ha->optrom_buffer[off], buf, count);
342 	mutex_unlock(&ha->optrom_mutex);
343 
344 	return count;
345 }
346 
347 static struct bin_attribute sysfs_optrom_attr = {
348 	.attr = {
349 		.name = "optrom",
350 		.mode = S_IRUSR | S_IWUSR,
351 	},
352 	.size = 0,
353 	.read = qla2x00_sysfs_read_optrom,
354 	.write = qla2x00_sysfs_write_optrom,
355 };
356 
357 static ssize_t
358 qla2x00_sysfs_write_optrom_ctl(struct file *filp, struct kobject *kobj,
359 			       struct bin_attribute *bin_attr,
360 			       char *buf, loff_t off, size_t count)
361 {
362 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
363 	    struct device, kobj)));
364 	struct qla_hw_data *ha = vha->hw;
365 	uint32_t start = 0;
366 	uint32_t size = ha->optrom_size;
367 	int val, valid;
368 	ssize_t rval = count;
369 
370 	if (off)
371 		return -EINVAL;
372 
373 	if (unlikely(pci_channel_offline(ha->pdev)))
374 		return -EAGAIN;
375 
376 	if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
377 		return -EINVAL;
378 	if (start > ha->optrom_size)
379 		return -EINVAL;
380 	if (size > ha->optrom_size - start)
381 		size = ha->optrom_size - start;
382 
383 	mutex_lock(&ha->optrom_mutex);
384 	if (qla2x00_chip_is_down(vha)) {
385 		mutex_unlock(&ha->optrom_mutex);
386 		return -EAGAIN;
387 	}
388 	switch (val) {
389 	case 0:
390 		if (ha->optrom_state != QLA_SREADING &&
391 		    ha->optrom_state != QLA_SWRITING) {
392 			rval =  -EINVAL;
393 			goto out;
394 		}
395 		ha->optrom_state = QLA_SWAITING;
396 
397 		ql_dbg(ql_dbg_user, vha, 0x7061,
398 		    "Freeing flash region allocation -- 0x%x bytes.\n",
399 		    ha->optrom_region_size);
400 
401 		vfree(ha->optrom_buffer);
402 		ha->optrom_buffer = NULL;
403 		break;
404 	case 1:
405 		if (ha->optrom_state != QLA_SWAITING) {
406 			rval = -EINVAL;
407 			goto out;
408 		}
409 
410 		ha->optrom_region_start = start;
411 		ha->optrom_region_size = size;
412 
413 		ha->optrom_state = QLA_SREADING;
414 		ha->optrom_buffer = vzalloc(ha->optrom_region_size);
415 		if (ha->optrom_buffer == NULL) {
416 			ql_log(ql_log_warn, vha, 0x7062,
417 			    "Unable to allocate memory for optrom retrieval "
418 			    "(%x).\n", ha->optrom_region_size);
419 
420 			ha->optrom_state = QLA_SWAITING;
421 			rval = -ENOMEM;
422 			goto out;
423 		}
424 
425 		if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
426 			ql_log(ql_log_warn, vha, 0x7063,
427 			    "HBA not online, failing NVRAM update.\n");
428 			rval = -EAGAIN;
429 			goto out;
430 		}
431 
432 		ql_dbg(ql_dbg_user, vha, 0x7064,
433 		    "Reading flash region -- 0x%x/0x%x.\n",
434 		    ha->optrom_region_start, ha->optrom_region_size);
435 
436 		ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
437 		    ha->optrom_region_start, ha->optrom_region_size);
438 		break;
439 	case 2:
440 		if (ha->optrom_state != QLA_SWAITING) {
441 			rval = -EINVAL;
442 			goto out;
443 		}
444 
445 		/*
446 		 * We need to be more restrictive on which FLASH regions are
447 		 * allowed to be updated via user-space.  Regions accessible
448 		 * via this method include:
449 		 *
450 		 * ISP21xx/ISP22xx/ISP23xx type boards:
451 		 *
452 		 * 	0x000000 -> 0x020000 -- Boot code.
453 		 *
454 		 * ISP2322/ISP24xx type boards:
455 		 *
456 		 * 	0x000000 -> 0x07ffff -- Boot code.
457 		 * 	0x080000 -> 0x0fffff -- Firmware.
458 		 *
459 		 * ISP25xx type boards:
460 		 *
461 		 * 	0x000000 -> 0x07ffff -- Boot code.
462 		 * 	0x080000 -> 0x0fffff -- Firmware.
463 		 * 	0x120000 -> 0x12ffff -- VPD and HBA parameters.
464 		 *
465 		 * > ISP25xx type boards:
466 		 *
467 		 *      None -- should go through BSG.
468 		 */
469 		valid = 0;
470 		if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
471 			valid = 1;
472 		else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
473 			valid = 1;
474 		if (!valid) {
475 			ql_log(ql_log_warn, vha, 0x7065,
476 			    "Invalid start region 0x%x/0x%x.\n", start, size);
477 			rval = -EINVAL;
478 			goto out;
479 		}
480 
481 		ha->optrom_region_start = start;
482 		ha->optrom_region_size = size;
483 
484 		ha->optrom_state = QLA_SWRITING;
485 		ha->optrom_buffer = vzalloc(ha->optrom_region_size);
486 		if (ha->optrom_buffer == NULL) {
487 			ql_log(ql_log_warn, vha, 0x7066,
488 			    "Unable to allocate memory for optrom update "
489 			    "(%x)\n", ha->optrom_region_size);
490 
491 			ha->optrom_state = QLA_SWAITING;
492 			rval = -ENOMEM;
493 			goto out;
494 		}
495 
496 		ql_dbg(ql_dbg_user, vha, 0x7067,
497 		    "Staging flash region write -- 0x%x/0x%x.\n",
498 		    ha->optrom_region_start, ha->optrom_region_size);
499 
500 		break;
501 	case 3:
502 		if (ha->optrom_state != QLA_SWRITING) {
503 			rval = -EINVAL;
504 			goto out;
505 		}
506 
507 		if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
508 			ql_log(ql_log_warn, vha, 0x7068,
509 			    "HBA not online, failing flash update.\n");
510 			rval = -EAGAIN;
511 			goto out;
512 		}
513 
514 		ql_dbg(ql_dbg_user, vha, 0x7069,
515 		    "Writing flash region -- 0x%x/0x%x.\n",
516 		    ha->optrom_region_start, ha->optrom_region_size);
517 
518 		rval = ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
519 		    ha->optrom_region_start, ha->optrom_region_size);
520 		if (rval)
521 			rval = -EIO;
522 		break;
523 	default:
524 		rval = -EINVAL;
525 	}
526 
527 out:
528 	mutex_unlock(&ha->optrom_mutex);
529 	return rval;
530 }
531 
532 static struct bin_attribute sysfs_optrom_ctl_attr = {
533 	.attr = {
534 		.name = "optrom_ctl",
535 		.mode = S_IWUSR,
536 	},
537 	.size = 0,
538 	.write = qla2x00_sysfs_write_optrom_ctl,
539 };
540 
541 static ssize_t
542 qla2x00_sysfs_read_vpd(struct file *filp, struct kobject *kobj,
543 		       struct bin_attribute *bin_attr,
544 		       char *buf, loff_t off, size_t count)
545 {
546 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
547 	    struct device, kobj)));
548 	struct qla_hw_data *ha = vha->hw;
549 	uint32_t faddr;
550 	struct active_regions active_regions = { };
551 
552 	if (unlikely(pci_channel_offline(ha->pdev)))
553 		return -EAGAIN;
554 
555 	if (!capable(CAP_SYS_ADMIN))
556 		return -EINVAL;
557 
558 	if (IS_NOCACHE_VPD_TYPE(ha))
559 		goto skip;
560 
561 	faddr = ha->flt_region_vpd << 2;
562 
563 	if (IS_QLA28XX(ha)) {
564 		qla28xx_get_aux_images(vha, &active_regions);
565 		if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
566 			faddr = ha->flt_region_vpd_sec << 2;
567 
568 		ql_dbg(ql_dbg_init, vha, 0x7070,
569 		    "Loading %s nvram image.\n",
570 		    active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
571 		    "primary" : "secondary");
572 	}
573 
574 	mutex_lock(&ha->optrom_mutex);
575 	if (qla2x00_chip_is_down(vha)) {
576 		mutex_unlock(&ha->optrom_mutex);
577 		return -EAGAIN;
578 	}
579 
580 	ha->isp_ops->read_optrom(vha, ha->vpd, faddr, ha->vpd_size);
581 	mutex_unlock(&ha->optrom_mutex);
582 
583 	ha->isp_ops->read_optrom(vha, ha->vpd, faddr, ha->vpd_size);
584 skip:
585 	return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
586 }
587 
588 static ssize_t
589 qla2x00_sysfs_write_vpd(struct file *filp, struct kobject *kobj,
590 			struct bin_attribute *bin_attr,
591 			char *buf, loff_t off, size_t count)
592 {
593 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
594 	    struct device, kobj)));
595 	struct qla_hw_data *ha = vha->hw;
596 	uint8_t *tmp_data;
597 
598 	if (unlikely(pci_channel_offline(ha->pdev)))
599 		return 0;
600 
601 	if (qla2x00_chip_is_down(vha))
602 		return 0;
603 
604 	if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size ||
605 	    !ha->isp_ops->write_nvram)
606 		return 0;
607 
608 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
609 		ql_log(ql_log_warn, vha, 0x706a,
610 		    "HBA not online, failing VPD update.\n");
611 		return -EAGAIN;
612 	}
613 
614 	mutex_lock(&ha->optrom_mutex);
615 	if (qla2x00_chip_is_down(vha)) {
616 		mutex_unlock(&ha->optrom_mutex);
617 		return -EAGAIN;
618 	}
619 
620 	/* Write NVRAM. */
621 	ha->isp_ops->write_nvram(vha, buf, ha->vpd_base, count);
622 	ha->isp_ops->read_nvram(vha, ha->vpd, ha->vpd_base, count);
623 
624 	/* Update flash version information for 4Gb & above. */
625 	if (!IS_FWI2_CAPABLE(ha)) {
626 		mutex_unlock(&ha->optrom_mutex);
627 		return -EINVAL;
628 	}
629 
630 	tmp_data = vmalloc(256);
631 	if (!tmp_data) {
632 		mutex_unlock(&ha->optrom_mutex);
633 		ql_log(ql_log_warn, vha, 0x706b,
634 		    "Unable to allocate memory for VPD information update.\n");
635 		return -ENOMEM;
636 	}
637 	ha->isp_ops->get_flash_version(vha, tmp_data);
638 	vfree(tmp_data);
639 
640 	mutex_unlock(&ha->optrom_mutex);
641 
642 	return count;
643 }
644 
645 static struct bin_attribute sysfs_vpd_attr = {
646 	.attr = {
647 		.name = "vpd",
648 		.mode = S_IRUSR | S_IWUSR,
649 	},
650 	.size = 0,
651 	.read = qla2x00_sysfs_read_vpd,
652 	.write = qla2x00_sysfs_write_vpd,
653 };
654 
655 static ssize_t
656 qla2x00_sysfs_read_sfp(struct file *filp, struct kobject *kobj,
657 		       struct bin_attribute *bin_attr,
658 		       char *buf, loff_t off, size_t count)
659 {
660 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
661 	    struct device, kobj)));
662 	int rval;
663 
664 	if (!capable(CAP_SYS_ADMIN) || off != 0 || count < SFP_DEV_SIZE)
665 		return 0;
666 
667 	mutex_lock(&vha->hw->optrom_mutex);
668 	if (qla2x00_chip_is_down(vha)) {
669 		mutex_unlock(&vha->hw->optrom_mutex);
670 		return 0;
671 	}
672 
673 	rval = qla2x00_read_sfp_dev(vha, buf, count);
674 	mutex_unlock(&vha->hw->optrom_mutex);
675 
676 	if (rval)
677 		return -EIO;
678 
679 	return count;
680 }
681 
682 static struct bin_attribute sysfs_sfp_attr = {
683 	.attr = {
684 		.name = "sfp",
685 		.mode = S_IRUSR | S_IWUSR,
686 	},
687 	.size = SFP_DEV_SIZE,
688 	.read = qla2x00_sysfs_read_sfp,
689 };
690 
691 static ssize_t
692 qla2x00_sysfs_write_reset(struct file *filp, struct kobject *kobj,
693 			struct bin_attribute *bin_attr,
694 			char *buf, loff_t off, size_t count)
695 {
696 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
697 	    struct device, kobj)));
698 	struct qla_hw_data *ha = vha->hw;
699 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
700 	int type;
701 	uint32_t idc_control;
702 	uint8_t *tmp_data = NULL;
703 
704 	if (off != 0)
705 		return -EINVAL;
706 
707 	type = simple_strtol(buf, NULL, 10);
708 	switch (type) {
709 	case 0x2025c:
710 		ql_log(ql_log_info, vha, 0x706e,
711 		    "Issuing ISP reset.\n");
712 
713 		if (vha->hw->flags.port_isolated) {
714 			ql_log(ql_log_info, vha, 0x706e,
715 			       "Port is isolated, returning.\n");
716 			return -EINVAL;
717 		}
718 
719 		scsi_block_requests(vha->host);
720 		if (IS_QLA82XX(ha)) {
721 			ha->flags.isp82xx_no_md_cap = 1;
722 			qla82xx_idc_lock(ha);
723 			qla82xx_set_reset_owner(vha);
724 			qla82xx_idc_unlock(ha);
725 		} else if (IS_QLA8044(ha)) {
726 			qla8044_idc_lock(ha);
727 			idc_control = qla8044_rd_reg(ha,
728 			    QLA8044_IDC_DRV_CTRL);
729 			qla8044_wr_reg(ha, QLA8044_IDC_DRV_CTRL,
730 			    (idc_control | GRACEFUL_RESET_BIT1));
731 			qla82xx_set_reset_owner(vha);
732 			qla8044_idc_unlock(ha);
733 		} else {
734 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
735 			qla2xxx_wake_dpc(vha);
736 		}
737 		qla2x00_wait_for_chip_reset(vha);
738 		scsi_unblock_requests(vha->host);
739 		break;
740 	case 0x2025d:
741 		if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
742 		    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
743 			return -EPERM;
744 
745 		ql_log(ql_log_info, vha, 0x706f,
746 		    "Issuing MPI reset.\n");
747 
748 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
749 			uint32_t idc_control;
750 
751 			qla83xx_idc_lock(vha, 0);
752 			__qla83xx_get_idc_control(vha, &idc_control);
753 			idc_control |= QLA83XX_IDC_GRACEFUL_RESET;
754 			__qla83xx_set_idc_control(vha, idc_control);
755 			qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
756 			    QLA8XXX_DEV_NEED_RESET);
757 			qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
758 			qla83xx_idc_unlock(vha, 0);
759 			break;
760 		} else {
761 			/* Make sure FC side is not in reset */
762 			WARN_ON_ONCE(qla2x00_wait_for_hba_online(vha) !=
763 				     QLA_SUCCESS);
764 
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;
772 		}
773 		break;
774 	case 0x2025e:
775 		if (!IS_P3P_TYPE(ha) || vha != base_vha) {
776 			ql_log(ql_log_info, vha, 0x7071,
777 			    "FCoE ctx reset not supported.\n");
778 			return -EPERM;
779 		}
780 
781 		ql_log(ql_log_info, vha, 0x7072,
782 		    "Issuing FCoE ctx reset.\n");
783 		set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
784 		qla2xxx_wake_dpc(vha);
785 		qla2x00_wait_for_fcoe_ctx_reset(vha);
786 		break;
787 	case 0x2025f:
788 		if (!IS_QLA8031(ha))
789 			return -EPERM;
790 		ql_log(ql_log_info, vha, 0x70bc,
791 		    "Disabling Reset by IDC control\n");
792 		qla83xx_idc_lock(vha, 0);
793 		__qla83xx_get_idc_control(vha, &idc_control);
794 		idc_control |= QLA83XX_IDC_RESET_DISABLED;
795 		__qla83xx_set_idc_control(vha, idc_control);
796 		qla83xx_idc_unlock(vha, 0);
797 		break;
798 	case 0x20260:
799 		if (!IS_QLA8031(ha))
800 			return -EPERM;
801 		ql_log(ql_log_info, vha, 0x70bd,
802 		    "Enabling Reset by IDC control\n");
803 		qla83xx_idc_lock(vha, 0);
804 		__qla83xx_get_idc_control(vha, &idc_control);
805 		idc_control &= ~QLA83XX_IDC_RESET_DISABLED;
806 		__qla83xx_set_idc_control(vha, idc_control);
807 		qla83xx_idc_unlock(vha, 0);
808 		break;
809 	case 0x20261:
810 		ql_dbg(ql_dbg_user, vha, 0x70e0,
811 		    "Updating cache versions without reset ");
812 
813 		tmp_data = vmalloc(256);
814 		if (!tmp_data) {
815 			ql_log(ql_log_warn, vha, 0x70e1,
816 			    "Unable to allocate memory for VPD information update.\n");
817 			return -ENOMEM;
818 		}
819 		ha->isp_ops->get_flash_version(vha, tmp_data);
820 		vfree(tmp_data);
821 		break;
822 	}
823 	return count;
824 }
825 
826 static struct bin_attribute sysfs_reset_attr = {
827 	.attr = {
828 		.name = "reset",
829 		.mode = S_IWUSR,
830 	},
831 	.size = 0,
832 	.write = qla2x00_sysfs_write_reset,
833 };
834 
835 static ssize_t
836 qla2x00_issue_logo(struct file *filp, struct kobject *kobj,
837 			struct bin_attribute *bin_attr,
838 			char *buf, loff_t off, size_t count)
839 {
840 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
841 	    struct device, kobj)));
842 	int type;
843 	port_id_t did;
844 
845 	if (!capable(CAP_SYS_ADMIN))
846 		return 0;
847 
848 	if (unlikely(pci_channel_offline(vha->hw->pdev)))
849 		return 0;
850 
851 	if (qla2x00_chip_is_down(vha))
852 		return 0;
853 
854 	type = simple_strtol(buf, NULL, 10);
855 
856 	did.b.domain = (type & 0x00ff0000) >> 16;
857 	did.b.area = (type & 0x0000ff00) >> 8;
858 	did.b.al_pa = (type & 0x000000ff);
859 
860 	ql_log(ql_log_info, vha, 0xd04d, "portid=%02x%02x%02x done\n",
861 	    did.b.domain, did.b.area, did.b.al_pa);
862 
863 	ql_log(ql_log_info, vha, 0x70e4, "%s: %d\n", __func__, type);
864 
865 	qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, did);
866 	return count;
867 }
868 
869 static struct bin_attribute sysfs_issue_logo_attr = {
870 	.attr = {
871 		.name = "issue_logo",
872 		.mode = S_IWUSR,
873 	},
874 	.size = 0,
875 	.write = qla2x00_issue_logo,
876 };
877 
878 static ssize_t
879 qla2x00_sysfs_read_xgmac_stats(struct file *filp, struct kobject *kobj,
880 		       struct bin_attribute *bin_attr,
881 		       char *buf, loff_t off, size_t count)
882 {
883 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
884 	    struct device, kobj)));
885 	struct qla_hw_data *ha = vha->hw;
886 	int rval;
887 	uint16_t actual_size;
888 
889 	if (!capable(CAP_SYS_ADMIN) || off != 0 || count > XGMAC_DATA_SIZE)
890 		return 0;
891 
892 	if (unlikely(pci_channel_offline(ha->pdev)))
893 		return 0;
894 	mutex_lock(&vha->hw->optrom_mutex);
895 	if (qla2x00_chip_is_down(vha)) {
896 		mutex_unlock(&vha->hw->optrom_mutex);
897 		return 0;
898 	}
899 
900 	if (ha->xgmac_data)
901 		goto do_read;
902 
903 	ha->xgmac_data = dma_alloc_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
904 	    &ha->xgmac_data_dma, GFP_KERNEL);
905 	if (!ha->xgmac_data) {
906 		mutex_unlock(&vha->hw->optrom_mutex);
907 		ql_log(ql_log_warn, vha, 0x7076,
908 		    "Unable to allocate memory for XGMAC read-data.\n");
909 		return 0;
910 	}
911 
912 do_read:
913 	actual_size = 0;
914 	memset(ha->xgmac_data, 0, XGMAC_DATA_SIZE);
915 
916 	rval = qla2x00_get_xgmac_stats(vha, ha->xgmac_data_dma,
917 	    XGMAC_DATA_SIZE, &actual_size);
918 
919 	mutex_unlock(&vha->hw->optrom_mutex);
920 	if (rval != QLA_SUCCESS) {
921 		ql_log(ql_log_warn, vha, 0x7077,
922 		    "Unable to read XGMAC data (%x).\n", rval);
923 		count = 0;
924 	}
925 
926 	count = actual_size > count ? count : actual_size;
927 	memcpy(buf, ha->xgmac_data, count);
928 
929 	return count;
930 }
931 
932 static struct bin_attribute sysfs_xgmac_stats_attr = {
933 	.attr = {
934 		.name = "xgmac_stats",
935 		.mode = S_IRUSR,
936 	},
937 	.size = 0,
938 	.read = qla2x00_sysfs_read_xgmac_stats,
939 };
940 
941 static ssize_t
942 qla2x00_sysfs_read_dcbx_tlv(struct file *filp, struct kobject *kobj,
943 		       struct bin_attribute *bin_attr,
944 		       char *buf, loff_t off, size_t count)
945 {
946 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(container_of(kobj,
947 	    struct device, kobj)));
948 	struct qla_hw_data *ha = vha->hw;
949 	int rval;
950 
951 	if (!capable(CAP_SYS_ADMIN) || off != 0 || count > DCBX_TLV_DATA_SIZE)
952 		return 0;
953 
954 	if (ha->dcbx_tlv)
955 		goto do_read;
956 	mutex_lock(&vha->hw->optrom_mutex);
957 	if (qla2x00_chip_is_down(vha)) {
958 		mutex_unlock(&vha->hw->optrom_mutex);
959 		return 0;
960 	}
961 
962 	ha->dcbx_tlv = dma_alloc_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
963 	    &ha->dcbx_tlv_dma, GFP_KERNEL);
964 	if (!ha->dcbx_tlv) {
965 		mutex_unlock(&vha->hw->optrom_mutex);
966 		ql_log(ql_log_warn, vha, 0x7078,
967 		    "Unable to allocate memory for DCBX TLV read-data.\n");
968 		return -ENOMEM;
969 	}
970 
971 do_read:
972 	memset(ha->dcbx_tlv, 0, DCBX_TLV_DATA_SIZE);
973 
974 	rval = qla2x00_get_dcbx_params(vha, ha->dcbx_tlv_dma,
975 	    DCBX_TLV_DATA_SIZE);
976 
977 	mutex_unlock(&vha->hw->optrom_mutex);
978 
979 	if (rval != QLA_SUCCESS) {
980 		ql_log(ql_log_warn, vha, 0x7079,
981 		    "Unable to read DCBX TLV (%x).\n", rval);
982 		return -EIO;
983 	}
984 
985 	memcpy(buf, ha->dcbx_tlv, count);
986 
987 	return count;
988 }
989 
990 static struct bin_attribute sysfs_dcbx_tlv_attr = {
991 	.attr = {
992 		.name = "dcbx_tlv",
993 		.mode = S_IRUSR,
994 	},
995 	.size = 0,
996 	.read = qla2x00_sysfs_read_dcbx_tlv,
997 };
998 
999 static struct sysfs_entry {
1000 	char *name;
1001 	struct bin_attribute *attr;
1002 	int type;
1003 } bin_file_entries[] = {
1004 	{ "fw_dump", &sysfs_fw_dump_attr, },
1005 	{ "nvram", &sysfs_nvram_attr, },
1006 	{ "optrom", &sysfs_optrom_attr, },
1007 	{ "optrom_ctl", &sysfs_optrom_ctl_attr, },
1008 	{ "vpd", &sysfs_vpd_attr, 1 },
1009 	{ "sfp", &sysfs_sfp_attr, 1 },
1010 	{ "reset", &sysfs_reset_attr, },
1011 	{ "issue_logo", &sysfs_issue_logo_attr, },
1012 	{ "xgmac_stats", &sysfs_xgmac_stats_attr, 3 },
1013 	{ "dcbx_tlv", &sysfs_dcbx_tlv_attr, 3 },
1014 	{ NULL },
1015 };
1016 
1017 void
1018 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *vha)
1019 {
1020 	struct Scsi_Host *host = vha->host;
1021 	struct sysfs_entry *iter;
1022 	int ret;
1023 
1024 	for (iter = bin_file_entries; iter->name; iter++) {
1025 		if (iter->type && !IS_FWI2_CAPABLE(vha->hw))
1026 			continue;
1027 		if (iter->type == 2 && !IS_QLA25XX(vha->hw))
1028 			continue;
1029 		if (iter->type == 3 && !(IS_CNA_CAPABLE(vha->hw)))
1030 			continue;
1031 
1032 		ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
1033 		    iter->attr);
1034 		if (ret)
1035 			ql_log(ql_log_warn, vha, 0x00f3,
1036 			    "Unable to create sysfs %s binary attribute (%d).\n",
1037 			    iter->name, ret);
1038 		else
1039 			ql_dbg(ql_dbg_init, vha, 0x00f4,
1040 			    "Successfully created sysfs %s binary attribute.\n",
1041 			    iter->name);
1042 	}
1043 }
1044 
1045 void
1046 qla2x00_free_sysfs_attr(scsi_qla_host_t *vha, bool stop_beacon)
1047 {
1048 	struct Scsi_Host *host = vha->host;
1049 	struct sysfs_entry *iter;
1050 	struct qla_hw_data *ha = vha->hw;
1051 
1052 	for (iter = bin_file_entries; iter->name; iter++) {
1053 		if (iter->type && !IS_FWI2_CAPABLE(ha))
1054 			continue;
1055 		if (iter->type == 2 && !IS_QLA25XX(ha))
1056 			continue;
1057 		if (iter->type == 3 && !(IS_CNA_CAPABLE(ha)))
1058 			continue;
1059 		if (iter->type == 0x27 &&
1060 		    (!IS_QLA27XX(ha) || !IS_QLA28XX(ha)))
1061 			continue;
1062 
1063 		sysfs_remove_bin_file(&host->shost_gendev.kobj,
1064 		    iter->attr);
1065 	}
1066 
1067 	if (stop_beacon && ha->beacon_blink_led == 1)
1068 		ha->isp_ops->beacon_off(vha);
1069 }
1070 
1071 /* Scsi_Host attributes. */
1072 
1073 static ssize_t
1074 qla2x00_driver_version_show(struct device *dev,
1075 			  struct device_attribute *attr, char *buf)
1076 {
1077 	return scnprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
1078 }
1079 
1080 static ssize_t
1081 qla2x00_fw_version_show(struct device *dev,
1082 			struct device_attribute *attr, char *buf)
1083 {
1084 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1085 	struct qla_hw_data *ha = vha->hw;
1086 	char fw_str[128];
1087 
1088 	return scnprintf(buf, PAGE_SIZE, "%s\n",
1089 	    ha->isp_ops->fw_version_str(vha, fw_str, sizeof(fw_str)));
1090 }
1091 
1092 static ssize_t
1093 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
1094 			char *buf)
1095 {
1096 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1097 	struct qla_hw_data *ha = vha->hw;
1098 	uint32_t sn;
1099 
1100 	if (IS_QLAFX00(vha->hw)) {
1101 		return scnprintf(buf, PAGE_SIZE, "%s\n",
1102 		    vha->hw->mr.serial_num);
1103 	} else if (IS_FWI2_CAPABLE(ha)) {
1104 		qla2xxx_get_vpd_field(vha, "SN", buf, PAGE_SIZE - 1);
1105 		return strlen(strcat(buf, "\n"));
1106 	}
1107 
1108 	sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
1109 	return scnprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
1110 	    sn % 100000);
1111 }
1112 
1113 static ssize_t
1114 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
1115 		      char *buf)
1116 {
1117 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1118 
1119 	return scnprintf(buf, PAGE_SIZE, "ISP%04X\n", vha->hw->pdev->device);
1120 }
1121 
1122 static ssize_t
1123 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
1124 		    char *buf)
1125 {
1126 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1127 	struct qla_hw_data *ha = vha->hw;
1128 
1129 	if (IS_QLAFX00(vha->hw))
1130 		return scnprintf(buf, PAGE_SIZE, "%s\n",
1131 		    vha->hw->mr.hw_version);
1132 
1133 	return scnprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
1134 	    ha->product_id[0], ha->product_id[1], ha->product_id[2],
1135 	    ha->product_id[3]);
1136 }
1137 
1138 static ssize_t
1139 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
1140 			char *buf)
1141 {
1142 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1143 
1144 	return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_number);
1145 }
1146 
1147 static ssize_t
1148 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
1149 			char *buf)
1150 {
1151 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1152 
1153 	return scnprintf(buf, PAGE_SIZE, "%s\n", vha->hw->model_desc);
1154 }
1155 
1156 static ssize_t
1157 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
1158 		      char *buf)
1159 {
1160 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1161 	char pci_info[30];
1162 
1163 	return scnprintf(buf, PAGE_SIZE, "%s\n",
1164 			 vha->hw->isp_ops->pci_info_str(vha, pci_info,
1165 							sizeof(pci_info)));
1166 }
1167 
1168 static ssize_t
1169 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
1170 			char *buf)
1171 {
1172 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1173 	struct qla_hw_data *ha = vha->hw;
1174 	int len = 0;
1175 
1176 	if (atomic_read(&vha->loop_state) == LOOP_DOWN ||
1177 	    atomic_read(&vha->loop_state) == LOOP_DEAD ||
1178 	    vha->device_flags & DFLG_NO_CABLE)
1179 		len = scnprintf(buf, PAGE_SIZE, "Link Down\n");
1180 	else if (atomic_read(&vha->loop_state) != LOOP_READY ||
1181 	    qla2x00_chip_is_down(vha))
1182 		len = scnprintf(buf, PAGE_SIZE, "Unknown Link State\n");
1183 	else {
1184 		len = scnprintf(buf, PAGE_SIZE, "Link Up - ");
1185 
1186 		switch (ha->current_topology) {
1187 		case ISP_CFG_NL:
1188 			len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1189 			break;
1190 		case ISP_CFG_FL:
1191 			len += scnprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
1192 			break;
1193 		case ISP_CFG_N:
1194 			len += scnprintf(buf + len, PAGE_SIZE-len,
1195 			    "N_Port to N_Port\n");
1196 			break;
1197 		case ISP_CFG_F:
1198 			len += scnprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
1199 			break;
1200 		default:
1201 			len += scnprintf(buf + len, PAGE_SIZE-len, "Loop\n");
1202 			break;
1203 		}
1204 	}
1205 	return len;
1206 }
1207 
1208 static ssize_t
1209 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
1210 		 char *buf)
1211 {
1212 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1213 	int len = 0;
1214 
1215 	switch (vha->hw->zio_mode) {
1216 	case QLA_ZIO_MODE_6:
1217 		len += scnprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
1218 		break;
1219 	case QLA_ZIO_DISABLED:
1220 		len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1221 		break;
1222 	}
1223 	return len;
1224 }
1225 
1226 static ssize_t
1227 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
1228 		  const char *buf, size_t count)
1229 {
1230 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1231 	struct qla_hw_data *ha = vha->hw;
1232 	int val = 0;
1233 	uint16_t zio_mode;
1234 
1235 	if (!IS_ZIO_SUPPORTED(ha))
1236 		return -ENOTSUPP;
1237 
1238 	if (sscanf(buf, "%d", &val) != 1)
1239 		return -EINVAL;
1240 
1241 	if (val)
1242 		zio_mode = QLA_ZIO_MODE_6;
1243 	else
1244 		zio_mode = QLA_ZIO_DISABLED;
1245 
1246 	/* Update per-hba values and queue a reset. */
1247 	if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
1248 		ha->zio_mode = zio_mode;
1249 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1250 	}
1251 	return strlen(buf);
1252 }
1253 
1254 static ssize_t
1255 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
1256 		       char *buf)
1257 {
1258 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1259 
1260 	return scnprintf(buf, PAGE_SIZE, "%d us\n", vha->hw->zio_timer * 100);
1261 }
1262 
1263 static ssize_t
1264 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
1265 			const char *buf, size_t count)
1266 {
1267 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1268 	int val = 0;
1269 	uint16_t zio_timer;
1270 
1271 	if (sscanf(buf, "%d", &val) != 1)
1272 		return -EINVAL;
1273 	if (val > 25500 || val < 100)
1274 		return -ERANGE;
1275 
1276 	zio_timer = (uint16_t)(val / 100);
1277 	vha->hw->zio_timer = zio_timer;
1278 
1279 	return strlen(buf);
1280 }
1281 
1282 static ssize_t
1283 qla_zio_threshold_show(struct device *dev, struct device_attribute *attr,
1284 		       char *buf)
1285 {
1286 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1287 
1288 	return scnprintf(buf, PAGE_SIZE, "%d exchanges\n",
1289 	    vha->hw->last_zio_threshold);
1290 }
1291 
1292 static ssize_t
1293 qla_zio_threshold_store(struct device *dev, struct device_attribute *attr,
1294     const char *buf, size_t count)
1295 {
1296 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1297 	int val = 0;
1298 
1299 	if (vha->hw->zio_mode != QLA_ZIO_MODE_6)
1300 		return -EINVAL;
1301 	if (sscanf(buf, "%d", &val) != 1)
1302 		return -EINVAL;
1303 	if (val < 0 || val > 256)
1304 		return -ERANGE;
1305 
1306 	atomic_set(&vha->hw->zio_threshold, val);
1307 	return strlen(buf);
1308 }
1309 
1310 static ssize_t
1311 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
1312 		    char *buf)
1313 {
1314 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1315 	int len = 0;
1316 
1317 	if (vha->hw->beacon_blink_led)
1318 		len += scnprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
1319 	else
1320 		len += scnprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
1321 	return len;
1322 }
1323 
1324 static ssize_t
1325 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
1326 		     const char *buf, size_t count)
1327 {
1328 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1329 	struct qla_hw_data *ha = vha->hw;
1330 	int val = 0;
1331 	int rval;
1332 
1333 	if (IS_QLA2100(ha) || IS_QLA2200(ha))
1334 		return -EPERM;
1335 
1336 	if (sscanf(buf, "%d", &val) != 1)
1337 		return -EINVAL;
1338 
1339 	mutex_lock(&vha->hw->optrom_mutex);
1340 	if (qla2x00_chip_is_down(vha)) {
1341 		mutex_unlock(&vha->hw->optrom_mutex);
1342 		ql_log(ql_log_warn, vha, 0x707a,
1343 		    "Abort ISP active -- ignoring beacon request.\n");
1344 		return -EBUSY;
1345 	}
1346 
1347 	if (val)
1348 		rval = ha->isp_ops->beacon_on(vha);
1349 	else
1350 		rval = ha->isp_ops->beacon_off(vha);
1351 
1352 	if (rval != QLA_SUCCESS)
1353 		count = 0;
1354 
1355 	mutex_unlock(&vha->hw->optrom_mutex);
1356 
1357 	return count;
1358 }
1359 
1360 static ssize_t
1361 qla2x00_beacon_config_show(struct device *dev, struct device_attribute *attr,
1362 	char *buf)
1363 {
1364 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1365 	struct qla_hw_data *ha = vha->hw;
1366 	uint16_t led[3] = { 0 };
1367 
1368 	if (!IS_QLA2031(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1369 		return -EPERM;
1370 
1371 	if (ql26xx_led_config(vha, 0, led))
1372 		return scnprintf(buf, PAGE_SIZE, "\n");
1373 
1374 	return scnprintf(buf, PAGE_SIZE, "%#04hx %#04hx %#04hx\n",
1375 	    led[0], led[1], led[2]);
1376 }
1377 
1378 static ssize_t
1379 qla2x00_beacon_config_store(struct device *dev, struct device_attribute *attr,
1380 	const char *buf, size_t count)
1381 {
1382 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1383 	struct qla_hw_data *ha = vha->hw;
1384 	uint16_t options = BIT_0;
1385 	uint16_t led[3] = { 0 };
1386 	uint16_t word[4];
1387 	int n;
1388 
1389 	if (!IS_QLA2031(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1390 		return -EPERM;
1391 
1392 	n = sscanf(buf, "%hx %hx %hx %hx", word+0, word+1, word+2, word+3);
1393 	if (n == 4) {
1394 		if (word[0] == 3) {
1395 			options |= BIT_3|BIT_2|BIT_1;
1396 			led[0] = word[1];
1397 			led[1] = word[2];
1398 			led[2] = word[3];
1399 			goto write;
1400 		}
1401 		return -EINVAL;
1402 	}
1403 
1404 	if (n == 2) {
1405 		/* check led index */
1406 		if (word[0] == 0) {
1407 			options |= BIT_2;
1408 			led[0] = word[1];
1409 			goto write;
1410 		}
1411 		if (word[0] == 1) {
1412 			options |= BIT_3;
1413 			led[1] = word[1];
1414 			goto write;
1415 		}
1416 		if (word[0] == 2) {
1417 			options |= BIT_1;
1418 			led[2] = word[1];
1419 			goto write;
1420 		}
1421 		return -EINVAL;
1422 	}
1423 
1424 	return -EINVAL;
1425 
1426 write:
1427 	if (ql26xx_led_config(vha, options, led))
1428 		return -EFAULT;
1429 
1430 	return count;
1431 }
1432 
1433 static ssize_t
1434 qla2x00_optrom_bios_version_show(struct device *dev,
1435 				 struct device_attribute *attr, char *buf)
1436 {
1437 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1438 	struct qla_hw_data *ha = vha->hw;
1439 
1440 	return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
1441 	    ha->bios_revision[0]);
1442 }
1443 
1444 static ssize_t
1445 qla2x00_optrom_efi_version_show(struct device *dev,
1446 				struct device_attribute *attr, char *buf)
1447 {
1448 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1449 	struct qla_hw_data *ha = vha->hw;
1450 
1451 	return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
1452 	    ha->efi_revision[0]);
1453 }
1454 
1455 static ssize_t
1456 qla2x00_optrom_fcode_version_show(struct device *dev,
1457 				  struct device_attribute *attr, char *buf)
1458 {
1459 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1460 	struct qla_hw_data *ha = vha->hw;
1461 
1462 	return scnprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
1463 	    ha->fcode_revision[0]);
1464 }
1465 
1466 static ssize_t
1467 qla2x00_optrom_fw_version_show(struct device *dev,
1468 			       struct device_attribute *attr, char *buf)
1469 {
1470 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1471 	struct qla_hw_data *ha = vha->hw;
1472 
1473 	return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
1474 	    ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
1475 	    ha->fw_revision[3]);
1476 }
1477 
1478 static ssize_t
1479 qla2x00_optrom_gold_fw_version_show(struct device *dev,
1480     struct device_attribute *attr, char *buf)
1481 {
1482 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1483 	struct qla_hw_data *ha = vha->hw;
1484 
1485 	if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
1486 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1487 		return scnprintf(buf, PAGE_SIZE, "\n");
1488 
1489 	return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%d)\n",
1490 	    ha->gold_fw_version[0], ha->gold_fw_version[1],
1491 	    ha->gold_fw_version[2], ha->gold_fw_version[3]);
1492 }
1493 
1494 static ssize_t
1495 qla2x00_total_isp_aborts_show(struct device *dev,
1496 			      struct device_attribute *attr, char *buf)
1497 {
1498 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1499 
1500 	return scnprintf(buf, PAGE_SIZE, "%d\n",
1501 	    vha->qla_stats.total_isp_aborts);
1502 }
1503 
1504 static ssize_t
1505 qla24xx_84xx_fw_version_show(struct device *dev,
1506 	struct device_attribute *attr, char *buf)
1507 {
1508 	int rval = QLA_SUCCESS;
1509 	uint16_t status[2] = { 0 };
1510 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1511 	struct qla_hw_data *ha = vha->hw;
1512 
1513 	if (!IS_QLA84XX(ha))
1514 		return scnprintf(buf, PAGE_SIZE, "\n");
1515 
1516 	if (!ha->cs84xx->op_fw_version) {
1517 		rval = qla84xx_verify_chip(vha, status);
1518 
1519 		if (!rval && !status[0])
1520 			return scnprintf(buf, PAGE_SIZE, "%u\n",
1521 			    (uint32_t)ha->cs84xx->op_fw_version);
1522 	}
1523 
1524 	return scnprintf(buf, PAGE_SIZE, "\n");
1525 }
1526 
1527 static ssize_t
1528 qla2x00_serdes_version_show(struct device *dev, struct device_attribute *attr,
1529     char *buf)
1530 {
1531 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1532 	struct qla_hw_data *ha = vha->hw;
1533 
1534 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1535 		return scnprintf(buf, PAGE_SIZE, "\n");
1536 
1537 	return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1538 	    ha->serdes_version[0], ha->serdes_version[1],
1539 	    ha->serdes_version[2]);
1540 }
1541 
1542 static ssize_t
1543 qla2x00_mpi_version_show(struct device *dev, struct device_attribute *attr,
1544     char *buf)
1545 {
1546 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1547 	struct qla_hw_data *ha = vha->hw;
1548 
1549 	if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha) &&
1550 	    !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1551 		return scnprintf(buf, PAGE_SIZE, "\n");
1552 
1553 	return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
1554 	    ha->mpi_version[0], ha->mpi_version[1], ha->mpi_version[2],
1555 	    ha->mpi_capabilities);
1556 }
1557 
1558 static ssize_t
1559 qla2x00_phy_version_show(struct device *dev, struct device_attribute *attr,
1560     char *buf)
1561 {
1562 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1563 	struct qla_hw_data *ha = vha->hw;
1564 
1565 	if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
1566 		return scnprintf(buf, PAGE_SIZE, "\n");
1567 
1568 	return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1569 	    ha->phy_version[0], ha->phy_version[1], ha->phy_version[2]);
1570 }
1571 
1572 static ssize_t
1573 qla2x00_flash_block_size_show(struct device *dev,
1574 			      struct device_attribute *attr, char *buf)
1575 {
1576 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1577 	struct qla_hw_data *ha = vha->hw;
1578 
1579 	return scnprintf(buf, PAGE_SIZE, "0x%x\n", ha->fdt_block_size);
1580 }
1581 
1582 static ssize_t
1583 qla2x00_vlan_id_show(struct device *dev, struct device_attribute *attr,
1584     char *buf)
1585 {
1586 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1587 
1588 	if (!IS_CNA_CAPABLE(vha->hw))
1589 		return scnprintf(buf, PAGE_SIZE, "\n");
1590 
1591 	return scnprintf(buf, PAGE_SIZE, "%d\n", vha->fcoe_vlan_id);
1592 }
1593 
1594 static ssize_t
1595 qla2x00_vn_port_mac_address_show(struct device *dev,
1596     struct device_attribute *attr, char *buf)
1597 {
1598 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1599 
1600 	if (!IS_CNA_CAPABLE(vha->hw))
1601 		return scnprintf(buf, PAGE_SIZE, "\n");
1602 
1603 	return scnprintf(buf, PAGE_SIZE, "%pMR\n", vha->fcoe_vn_port_mac);
1604 }
1605 
1606 static ssize_t
1607 qla2x00_fabric_param_show(struct device *dev, struct device_attribute *attr,
1608     char *buf)
1609 {
1610 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1611 
1612 	return scnprintf(buf, PAGE_SIZE, "%d\n", vha->hw->switch_cap);
1613 }
1614 
1615 static ssize_t
1616 qla2x00_thermal_temp_show(struct device *dev,
1617 	struct device_attribute *attr, char *buf)
1618 {
1619 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1620 	uint16_t temp = 0;
1621 	int rc;
1622 
1623 	mutex_lock(&vha->hw->optrom_mutex);
1624 	if (qla2x00_chip_is_down(vha)) {
1625 		mutex_unlock(&vha->hw->optrom_mutex);
1626 		ql_log(ql_log_warn, vha, 0x70dc, "ISP reset active.\n");
1627 		goto done;
1628 	}
1629 
1630 	if (vha->hw->flags.eeh_busy) {
1631 		mutex_unlock(&vha->hw->optrom_mutex);
1632 		ql_log(ql_log_warn, vha, 0x70dd, "PCI EEH busy.\n");
1633 		goto done;
1634 	}
1635 
1636 	rc = qla2x00_get_thermal_temp(vha, &temp);
1637 	mutex_unlock(&vha->hw->optrom_mutex);
1638 	if (rc == QLA_SUCCESS)
1639 		return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
1640 
1641 done:
1642 	return scnprintf(buf, PAGE_SIZE, "\n");
1643 }
1644 
1645 static ssize_t
1646 qla2x00_fw_state_show(struct device *dev, struct device_attribute *attr,
1647     char *buf)
1648 {
1649 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1650 	int rval = QLA_FUNCTION_FAILED;
1651 	uint16_t state[6];
1652 	uint32_t pstate;
1653 
1654 	if (IS_QLAFX00(vha->hw)) {
1655 		pstate = qlafx00_fw_state_show(dev, attr, buf);
1656 		return scnprintf(buf, PAGE_SIZE, "0x%x\n", pstate);
1657 	}
1658 
1659 	mutex_lock(&vha->hw->optrom_mutex);
1660 	if (qla2x00_chip_is_down(vha)) {
1661 		mutex_unlock(&vha->hw->optrom_mutex);
1662 		ql_log(ql_log_warn, vha, 0x707c,
1663 		    "ISP reset active.\n");
1664 		goto out;
1665 	} else if (vha->hw->flags.eeh_busy) {
1666 		mutex_unlock(&vha->hw->optrom_mutex);
1667 		goto out;
1668 	}
1669 
1670 	rval = qla2x00_get_firmware_state(vha, state);
1671 	mutex_unlock(&vha->hw->optrom_mutex);
1672 out:
1673 	if (rval != QLA_SUCCESS) {
1674 		memset(state, -1, sizeof(state));
1675 		rval = qla2x00_get_firmware_state(vha, state);
1676 	}
1677 
1678 	return scnprintf(buf, PAGE_SIZE, "0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1679 	    state[0], state[1], state[2], state[3], state[4], state[5]);
1680 }
1681 
1682 static ssize_t
1683 qla2x00_diag_requests_show(struct device *dev,
1684 	struct device_attribute *attr, char *buf)
1685 {
1686 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1687 
1688 	if (!IS_BIDI_CAPABLE(vha->hw))
1689 		return scnprintf(buf, PAGE_SIZE, "\n");
1690 
1691 	return scnprintf(buf, PAGE_SIZE, "%llu\n", vha->bidi_stats.io_count);
1692 }
1693 
1694 static ssize_t
1695 qla2x00_diag_megabytes_show(struct device *dev,
1696 	struct device_attribute *attr, char *buf)
1697 {
1698 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1699 
1700 	if (!IS_BIDI_CAPABLE(vha->hw))
1701 		return scnprintf(buf, PAGE_SIZE, "\n");
1702 
1703 	return scnprintf(buf, PAGE_SIZE, "%llu\n",
1704 	    vha->bidi_stats.transfer_bytes >> 20);
1705 }
1706 
1707 static ssize_t
1708 qla2x00_fw_dump_size_show(struct device *dev, struct device_attribute *attr,
1709 	char *buf)
1710 {
1711 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1712 	struct qla_hw_data *ha = vha->hw;
1713 	uint32_t size;
1714 
1715 	if (!ha->fw_dumped)
1716 		size = 0;
1717 	else if (IS_P3P_TYPE(ha))
1718 		size = ha->md_template_size + ha->md_dump_size;
1719 	else
1720 		size = ha->fw_dump_len;
1721 
1722 	return scnprintf(buf, PAGE_SIZE, "%d\n", size);
1723 }
1724 
1725 static ssize_t
1726 qla2x00_allow_cna_fw_dump_show(struct device *dev,
1727 	struct device_attribute *attr, char *buf)
1728 {
1729 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1730 
1731 	if (!IS_P3P_TYPE(vha->hw))
1732 		return scnprintf(buf, PAGE_SIZE, "\n");
1733 	else
1734 		return scnprintf(buf, PAGE_SIZE, "%s\n",
1735 		    vha->hw->allow_cna_fw_dump ? "true" : "false");
1736 }
1737 
1738 static ssize_t
1739 qla2x00_allow_cna_fw_dump_store(struct device *dev,
1740 	struct device_attribute *attr, const char *buf, size_t count)
1741 {
1742 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1743 	int val = 0;
1744 
1745 	if (!IS_P3P_TYPE(vha->hw))
1746 		return -EINVAL;
1747 
1748 	if (sscanf(buf, "%d", &val) != 1)
1749 		return -EINVAL;
1750 
1751 	vha->hw->allow_cna_fw_dump = val != 0;
1752 
1753 	return strlen(buf);
1754 }
1755 
1756 static ssize_t
1757 qla2x00_pep_version_show(struct device *dev, struct device_attribute *attr,
1758 	char *buf)
1759 {
1760 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1761 	struct qla_hw_data *ha = vha->hw;
1762 
1763 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1764 		return scnprintf(buf, PAGE_SIZE, "\n");
1765 
1766 	return scnprintf(buf, PAGE_SIZE, "%d.%02d.%02d\n",
1767 	    ha->pep_version[0], ha->pep_version[1], ha->pep_version[2]);
1768 }
1769 
1770 static ssize_t
1771 qla2x00_min_supported_speed_show(struct device *dev,
1772     struct device_attribute *attr, char *buf)
1773 {
1774 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1775 	struct qla_hw_data *ha = vha->hw;
1776 
1777 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1778 		return scnprintf(buf, PAGE_SIZE, "\n");
1779 
1780 	return scnprintf(buf, PAGE_SIZE, "%s\n",
1781 	    ha->min_supported_speed == 6 ? "64Gps" :
1782 	    ha->min_supported_speed == 5 ? "32Gps" :
1783 	    ha->min_supported_speed == 4 ? "16Gps" :
1784 	    ha->min_supported_speed == 3 ? "8Gps" :
1785 	    ha->min_supported_speed == 2 ? "4Gps" :
1786 	    ha->min_supported_speed != 0 ? "unknown" : "");
1787 }
1788 
1789 static ssize_t
1790 qla2x00_max_supported_speed_show(struct device *dev,
1791     struct device_attribute *attr, char *buf)
1792 {
1793 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1794 	struct qla_hw_data *ha = vha->hw;
1795 
1796 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
1797 		return scnprintf(buf, PAGE_SIZE, "\n");
1798 
1799 	return scnprintf(buf, PAGE_SIZE, "%s\n",
1800 	    ha->max_supported_speed  == 2 ? "64Gps" :
1801 	    ha->max_supported_speed  == 1 ? "32Gps" :
1802 	    ha->max_supported_speed  == 0 ? "16Gps" : "unknown");
1803 }
1804 
1805 static ssize_t
1806 qla2x00_port_speed_store(struct device *dev, struct device_attribute *attr,
1807     const char *buf, size_t count)
1808 {
1809 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(dev));
1810 	ulong type, speed;
1811 	int oldspeed, rval;
1812 	int mode = QLA_SET_DATA_RATE_LR;
1813 	struct qla_hw_data *ha = vha->hw;
1814 
1815 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha)) {
1816 		ql_log(ql_log_warn, vha, 0x70d8,
1817 		    "Speed setting not supported \n");
1818 		return -EINVAL;
1819 	}
1820 
1821 	rval = kstrtol(buf, 10, &type);
1822 	if (rval)
1823 		return rval;
1824 	speed = type;
1825 	if (type == 40 || type == 80 || type == 160 ||
1826 	    type == 320) {
1827 		ql_dbg(ql_dbg_user, vha, 0x70d9,
1828 		    "Setting will be affected after a loss of sync\n");
1829 		type = type/10;
1830 		mode = QLA_SET_DATA_RATE_NOLR;
1831 	}
1832 
1833 	oldspeed = ha->set_data_rate;
1834 
1835 	switch (type) {
1836 	case 0:
1837 		ha->set_data_rate = PORT_SPEED_AUTO;
1838 		break;
1839 	case 4:
1840 		ha->set_data_rate = PORT_SPEED_4GB;
1841 		break;
1842 	case 8:
1843 		ha->set_data_rate = PORT_SPEED_8GB;
1844 		break;
1845 	case 16:
1846 		ha->set_data_rate = PORT_SPEED_16GB;
1847 		break;
1848 	case 32:
1849 		ha->set_data_rate = PORT_SPEED_32GB;
1850 		break;
1851 	default:
1852 		ql_log(ql_log_warn, vha, 0x1199,
1853 		    "Unrecognized speed setting:%lx. Setting Autoneg\n",
1854 		    speed);
1855 		ha->set_data_rate = PORT_SPEED_AUTO;
1856 	}
1857 
1858 	if (qla2x00_chip_is_down(vha) || (oldspeed == ha->set_data_rate))
1859 		return -EINVAL;
1860 
1861 	ql_log(ql_log_info, vha, 0x70da,
1862 	    "Setting speed to %lx Gbps \n", type);
1863 
1864 	rval = qla2x00_set_data_rate(vha, mode);
1865 	if (rval != QLA_SUCCESS)
1866 		return -EIO;
1867 
1868 	return strlen(buf);
1869 }
1870 
1871 static ssize_t
1872 qla2x00_port_speed_show(struct device *dev, struct device_attribute *attr,
1873     char *buf)
1874 {
1875 	struct scsi_qla_host *vha = shost_priv(dev_to_shost(dev));
1876 	struct qla_hw_data *ha = vha->hw;
1877 	ssize_t rval;
1878 	char *spd[7] = {"0", "0", "0", "4", "8", "16", "32"};
1879 
1880 	rval = qla2x00_get_data_rate(vha);
1881 	if (rval != QLA_SUCCESS) {
1882 		ql_log(ql_log_warn, vha, 0x70db,
1883 		    "Unable to get port speed rval:%zd\n", rval);
1884 		return -EINVAL;
1885 	}
1886 
1887 	return scnprintf(buf, PAGE_SIZE, "%s\n", spd[ha->link_data_rate]);
1888 }
1889 
1890 static ssize_t
1891 qla2x00_mpi_pause_store(struct device *dev,
1892 	struct device_attribute *attr, const char *buf, size_t count)
1893 {
1894 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1895 	int rval = 0;
1896 
1897 	if (sscanf(buf, "%d", &rval) != 1)
1898 		return -EINVAL;
1899 
1900 	ql_log(ql_log_warn, vha, 0x7089, "Pausing MPI...\n");
1901 
1902 	rval = qla83xx_wr_reg(vha, 0x002012d4, 0x30000001);
1903 
1904 	if (rval != QLA_SUCCESS) {
1905 		ql_log(ql_log_warn, vha, 0x708a, "Unable to pause MPI.\n");
1906 		count = 0;
1907 	}
1908 
1909 	return count;
1910 }
1911 
1912 static DEVICE_ATTR(mpi_pause, S_IWUSR, NULL, qla2x00_mpi_pause_store);
1913 
1914 /* ----- */
1915 
1916 static ssize_t
1917 qlini_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1918 {
1919 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
1920 	int len = 0;
1921 
1922 	len += scnprintf(buf + len, PAGE_SIZE-len,
1923 	    "Supported options: enabled | disabled | dual | exclusive\n");
1924 
1925 	/* --- */
1926 	len += scnprintf(buf + len, PAGE_SIZE-len, "Current selection: ");
1927 
1928 	switch (vha->qlini_mode) {
1929 	case QLA2XXX_INI_MODE_EXCLUSIVE:
1930 		len += scnprintf(buf + len, PAGE_SIZE-len,
1931 		    QLA2XXX_INI_MODE_STR_EXCLUSIVE);
1932 		break;
1933 	case QLA2XXX_INI_MODE_DISABLED:
1934 		len += scnprintf(buf + len, PAGE_SIZE-len,
1935 		    QLA2XXX_INI_MODE_STR_DISABLED);
1936 		break;
1937 	case QLA2XXX_INI_MODE_ENABLED:
1938 		len += scnprintf(buf + len, PAGE_SIZE-len,
1939 		    QLA2XXX_INI_MODE_STR_ENABLED);
1940 		break;
1941 	case QLA2XXX_INI_MODE_DUAL:
1942 		len += scnprintf(buf + len, PAGE_SIZE-len,
1943 		    QLA2XXX_INI_MODE_STR_DUAL);
1944 		break;
1945 	}
1946 	len += scnprintf(buf + len, PAGE_SIZE-len, "\n");
1947 
1948 	return len;
1949 }
1950 
1951 static char *mode_to_str[] = {
1952 	"exclusive",
1953 	"disabled",
1954 	"enabled",
1955 	"dual",
1956 };
1957 
1958 #define NEED_EXCH_OFFLOAD(_exchg) ((_exchg) > FW_DEF_EXCHANGES_CNT)
1959 static void qla_set_ini_mode(scsi_qla_host_t *vha, int op)
1960 {
1961 	enum {
1962 		NO_ACTION,
1963 		MODE_CHANGE_ACCEPT,
1964 		MODE_CHANGE_NO_ACTION,
1965 		TARGET_STILL_ACTIVE,
1966 	};
1967 	int action = NO_ACTION;
1968 	int set_mode = 0;
1969 	u8  eo_toggle = 0;	/* exchange offload flipped */
1970 
1971 	switch (vha->qlini_mode) {
1972 	case QLA2XXX_INI_MODE_DISABLED:
1973 		switch (op) {
1974 		case QLA2XXX_INI_MODE_DISABLED:
1975 			if (qla_tgt_mode_enabled(vha)) {
1976 				if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
1977 				    vha->hw->flags.exchoffld_enabled)
1978 					eo_toggle = 1;
1979 				if (((vha->ql2xexchoffld !=
1980 				    vha->u_ql2xexchoffld) &&
1981 				    NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
1982 				    eo_toggle) {
1983 					/*
1984 					 * The number of exchange to be offload
1985 					 * was tweaked or offload option was
1986 					 * flipped
1987 					 */
1988 					action = MODE_CHANGE_ACCEPT;
1989 				} else {
1990 					action = MODE_CHANGE_NO_ACTION;
1991 				}
1992 			} else {
1993 				action = MODE_CHANGE_NO_ACTION;
1994 			}
1995 			break;
1996 		case QLA2XXX_INI_MODE_EXCLUSIVE:
1997 			if (qla_tgt_mode_enabled(vha)) {
1998 				if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
1999 				    vha->hw->flags.exchoffld_enabled)
2000 					eo_toggle = 1;
2001 				if (((vha->ql2xexchoffld !=
2002 				    vha->u_ql2xexchoffld) &&
2003 				    NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
2004 				    eo_toggle) {
2005 					/*
2006 					 * The number of exchange to be offload
2007 					 * was tweaked or offload option was
2008 					 * flipped
2009 					 */
2010 					action = MODE_CHANGE_ACCEPT;
2011 				} else {
2012 					action = MODE_CHANGE_NO_ACTION;
2013 				}
2014 			} else {
2015 				action = MODE_CHANGE_ACCEPT;
2016 			}
2017 			break;
2018 		case QLA2XXX_INI_MODE_DUAL:
2019 			action = MODE_CHANGE_ACCEPT;
2020 			/* active_mode is target only, reset it to dual */
2021 			if (qla_tgt_mode_enabled(vha)) {
2022 				set_mode = 1;
2023 				action = MODE_CHANGE_ACCEPT;
2024 			} else {
2025 				action = MODE_CHANGE_NO_ACTION;
2026 			}
2027 			break;
2028 
2029 		case QLA2XXX_INI_MODE_ENABLED:
2030 			if (qla_tgt_mode_enabled(vha))
2031 				action = TARGET_STILL_ACTIVE;
2032 			else {
2033 				action = MODE_CHANGE_ACCEPT;
2034 				set_mode = 1;
2035 			}
2036 			break;
2037 		}
2038 		break;
2039 
2040 	case QLA2XXX_INI_MODE_EXCLUSIVE:
2041 		switch (op) {
2042 		case QLA2XXX_INI_MODE_EXCLUSIVE:
2043 			if (qla_tgt_mode_enabled(vha)) {
2044 				if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
2045 				    vha->hw->flags.exchoffld_enabled)
2046 					eo_toggle = 1;
2047 				if (((vha->ql2xexchoffld !=
2048 				    vha->u_ql2xexchoffld) &&
2049 				    NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
2050 				    eo_toggle)
2051 					/*
2052 					 * The number of exchange to be offload
2053 					 * was tweaked or offload option was
2054 					 * flipped
2055 					 */
2056 					action = MODE_CHANGE_ACCEPT;
2057 				else
2058 					action = NO_ACTION;
2059 			} else
2060 				action = NO_ACTION;
2061 
2062 			break;
2063 
2064 		case QLA2XXX_INI_MODE_DISABLED:
2065 			if (qla_tgt_mode_enabled(vha)) {
2066 				if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld) !=
2067 				    vha->hw->flags.exchoffld_enabled)
2068 					eo_toggle = 1;
2069 				if (((vha->ql2xexchoffld !=
2070 				      vha->u_ql2xexchoffld) &&
2071 				    NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld)) ||
2072 				    eo_toggle)
2073 					action = MODE_CHANGE_ACCEPT;
2074 				else
2075 					action = MODE_CHANGE_NO_ACTION;
2076 			} else
2077 				action = MODE_CHANGE_NO_ACTION;
2078 			break;
2079 
2080 		case QLA2XXX_INI_MODE_DUAL: /* exclusive -> dual */
2081 			if (qla_tgt_mode_enabled(vha)) {
2082 				action = MODE_CHANGE_ACCEPT;
2083 				set_mode = 1;
2084 			} else
2085 				action = MODE_CHANGE_ACCEPT;
2086 			break;
2087 
2088 		case QLA2XXX_INI_MODE_ENABLED:
2089 			if (qla_tgt_mode_enabled(vha))
2090 				action = TARGET_STILL_ACTIVE;
2091 			else {
2092 				if (vha->hw->flags.fw_started)
2093 					action = MODE_CHANGE_NO_ACTION;
2094 				else
2095 					action = MODE_CHANGE_ACCEPT;
2096 			}
2097 			break;
2098 		}
2099 		break;
2100 
2101 	case QLA2XXX_INI_MODE_ENABLED:
2102 		switch (op) {
2103 		case QLA2XXX_INI_MODE_ENABLED:
2104 			if (NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg) !=
2105 			    vha->hw->flags.exchoffld_enabled)
2106 				eo_toggle = 1;
2107 			if (((vha->ql2xiniexchg != vha->u_ql2xiniexchg) &&
2108 				NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg)) ||
2109 			    eo_toggle)
2110 				action = MODE_CHANGE_ACCEPT;
2111 			else
2112 				action = NO_ACTION;
2113 			break;
2114 		case QLA2XXX_INI_MODE_DUAL:
2115 		case QLA2XXX_INI_MODE_DISABLED:
2116 			action = MODE_CHANGE_ACCEPT;
2117 			break;
2118 		default:
2119 			action = MODE_CHANGE_NO_ACTION;
2120 			break;
2121 		}
2122 		break;
2123 
2124 	case QLA2XXX_INI_MODE_DUAL:
2125 		switch (op) {
2126 		case QLA2XXX_INI_MODE_DUAL:
2127 			if (qla_tgt_mode_enabled(vha) ||
2128 			    qla_dual_mode_enabled(vha)) {
2129 				if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld +
2130 					vha->u_ql2xiniexchg) !=
2131 				    vha->hw->flags.exchoffld_enabled)
2132 					eo_toggle = 1;
2133 
2134 				if ((((vha->ql2xexchoffld +
2135 				       vha->ql2xiniexchg) !=
2136 				    (vha->u_ql2xiniexchg +
2137 				     vha->u_ql2xexchoffld)) &&
2138 				    NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg +
2139 					vha->u_ql2xexchoffld)) || eo_toggle)
2140 					action = MODE_CHANGE_ACCEPT;
2141 				else
2142 					action = NO_ACTION;
2143 			} else {
2144 				if (NEED_EXCH_OFFLOAD(vha->u_ql2xexchoffld +
2145 					vha->u_ql2xiniexchg) !=
2146 				    vha->hw->flags.exchoffld_enabled)
2147 					eo_toggle = 1;
2148 
2149 				if ((((vha->ql2xexchoffld + vha->ql2xiniexchg)
2150 				    != (vha->u_ql2xiniexchg +
2151 					vha->u_ql2xexchoffld)) &&
2152 				    NEED_EXCH_OFFLOAD(vha->u_ql2xiniexchg +
2153 					vha->u_ql2xexchoffld)) || eo_toggle)
2154 					action = MODE_CHANGE_NO_ACTION;
2155 				else
2156 					action = NO_ACTION;
2157 			}
2158 			break;
2159 
2160 		case QLA2XXX_INI_MODE_DISABLED:
2161 			if (qla_tgt_mode_enabled(vha) ||
2162 			    qla_dual_mode_enabled(vha)) {
2163 				/* turning off initiator mode */
2164 				set_mode = 1;
2165 				action = MODE_CHANGE_ACCEPT;
2166 			} else {
2167 				action = MODE_CHANGE_NO_ACTION;
2168 			}
2169 			break;
2170 
2171 		case QLA2XXX_INI_MODE_EXCLUSIVE:
2172 			if (qla_tgt_mode_enabled(vha) ||
2173 			    qla_dual_mode_enabled(vha)) {
2174 				set_mode = 1;
2175 				action = MODE_CHANGE_ACCEPT;
2176 			} else {
2177 				action = MODE_CHANGE_ACCEPT;
2178 			}
2179 			break;
2180 
2181 		case QLA2XXX_INI_MODE_ENABLED:
2182 			if (qla_tgt_mode_enabled(vha) ||
2183 			    qla_dual_mode_enabled(vha)) {
2184 				action = TARGET_STILL_ACTIVE;
2185 			} else {
2186 				action = MODE_CHANGE_ACCEPT;
2187 			}
2188 		}
2189 		break;
2190 	}
2191 
2192 	switch (action) {
2193 	case MODE_CHANGE_ACCEPT:
2194 		ql_log(ql_log_warn, vha, 0xffff,
2195 		    "Mode change accepted. From %s to %s, Tgt exchg %d|%d. ini exchg %d|%d\n",
2196 		    mode_to_str[vha->qlini_mode], mode_to_str[op],
2197 		    vha->ql2xexchoffld, vha->u_ql2xexchoffld,
2198 		    vha->ql2xiniexchg, vha->u_ql2xiniexchg);
2199 
2200 		vha->qlini_mode = op;
2201 		vha->ql2xexchoffld = vha->u_ql2xexchoffld;
2202 		vha->ql2xiniexchg = vha->u_ql2xiniexchg;
2203 		if (set_mode)
2204 			qlt_set_mode(vha);
2205 		vha->flags.online = 1;
2206 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2207 		break;
2208 
2209 	case MODE_CHANGE_NO_ACTION:
2210 		ql_log(ql_log_warn, vha, 0xffff,
2211 		    "Mode is set. No action taken. From %s to %s, Tgt exchg %d|%d. ini exchg %d|%d\n",
2212 		    mode_to_str[vha->qlini_mode], mode_to_str[op],
2213 		    vha->ql2xexchoffld, vha->u_ql2xexchoffld,
2214 		    vha->ql2xiniexchg, vha->u_ql2xiniexchg);
2215 		vha->qlini_mode = op;
2216 		vha->ql2xexchoffld = vha->u_ql2xexchoffld;
2217 		vha->ql2xiniexchg = vha->u_ql2xiniexchg;
2218 		break;
2219 
2220 	case TARGET_STILL_ACTIVE:
2221 		ql_log(ql_log_warn, vha, 0xffff,
2222 		    "Target Mode is active. Unable to change Mode.\n");
2223 		break;
2224 
2225 	case NO_ACTION:
2226 	default:
2227 		ql_log(ql_log_warn, vha, 0xffff,
2228 		    "Mode unchange. No action taken. %d|%d pct %d|%d.\n",
2229 		    vha->qlini_mode, op,
2230 		    vha->ql2xexchoffld, vha->u_ql2xexchoffld);
2231 		break;
2232 	}
2233 }
2234 
2235 static ssize_t
2236 qlini_mode_store(struct device *dev, struct device_attribute *attr,
2237     const char *buf, size_t count)
2238 {
2239 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2240 	int ini;
2241 
2242 	if (!buf)
2243 		return -EINVAL;
2244 
2245 	if (strncasecmp(QLA2XXX_INI_MODE_STR_EXCLUSIVE, buf,
2246 		strlen(QLA2XXX_INI_MODE_STR_EXCLUSIVE)) == 0)
2247 		ini = QLA2XXX_INI_MODE_EXCLUSIVE;
2248 	else if (strncasecmp(QLA2XXX_INI_MODE_STR_DISABLED, buf,
2249 		strlen(QLA2XXX_INI_MODE_STR_DISABLED)) == 0)
2250 		ini = QLA2XXX_INI_MODE_DISABLED;
2251 	else if (strncasecmp(QLA2XXX_INI_MODE_STR_ENABLED, buf,
2252 		  strlen(QLA2XXX_INI_MODE_STR_ENABLED)) == 0)
2253 		ini = QLA2XXX_INI_MODE_ENABLED;
2254 	else if (strncasecmp(QLA2XXX_INI_MODE_STR_DUAL, buf,
2255 		strlen(QLA2XXX_INI_MODE_STR_DUAL)) == 0)
2256 		ini = QLA2XXX_INI_MODE_DUAL;
2257 	else
2258 		return -EINVAL;
2259 
2260 	qla_set_ini_mode(vha, ini);
2261 	return strlen(buf);
2262 }
2263 
2264 static ssize_t
2265 ql2xexchoffld_show(struct device *dev, struct device_attribute *attr,
2266     char *buf)
2267 {
2268 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2269 	int len = 0;
2270 
2271 	len += scnprintf(buf + len, PAGE_SIZE-len,
2272 		"target exchange: new %d : current: %d\n\n",
2273 		vha->u_ql2xexchoffld, vha->ql2xexchoffld);
2274 
2275 	len += scnprintf(buf + len, PAGE_SIZE-len,
2276 	    "Please (re)set operating mode via \"/sys/class/scsi_host/host%ld/qlini_mode\" to load new setting.\n",
2277 	    vha->host_no);
2278 
2279 	return len;
2280 }
2281 
2282 static ssize_t
2283 ql2xexchoffld_store(struct device *dev, struct device_attribute *attr,
2284     const char *buf, size_t count)
2285 {
2286 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2287 	int val = 0;
2288 
2289 	if (sscanf(buf, "%d", &val) != 1)
2290 		return -EINVAL;
2291 
2292 	if (val > FW_MAX_EXCHANGES_CNT)
2293 		val = FW_MAX_EXCHANGES_CNT;
2294 	else if (val < 0)
2295 		val = 0;
2296 
2297 	vha->u_ql2xexchoffld = val;
2298 	return strlen(buf);
2299 }
2300 
2301 static ssize_t
2302 ql2xiniexchg_show(struct device *dev, struct device_attribute *attr,
2303     char *buf)
2304 {
2305 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2306 	int len = 0;
2307 
2308 	len += scnprintf(buf + len, PAGE_SIZE-len,
2309 		"target exchange: new %d : current: %d\n\n",
2310 		vha->u_ql2xiniexchg, vha->ql2xiniexchg);
2311 
2312 	len += scnprintf(buf + len, PAGE_SIZE-len,
2313 	    "Please (re)set operating mode via \"/sys/class/scsi_host/host%ld/qlini_mode\" to load new setting.\n",
2314 	    vha->host_no);
2315 
2316 	return len;
2317 }
2318 
2319 static ssize_t
2320 ql2xiniexchg_store(struct device *dev, struct device_attribute *attr,
2321     const char *buf, size_t count)
2322 {
2323 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2324 	int val = 0;
2325 
2326 	if (sscanf(buf, "%d", &val) != 1)
2327 		return -EINVAL;
2328 
2329 	if (val > FW_MAX_EXCHANGES_CNT)
2330 		val = FW_MAX_EXCHANGES_CNT;
2331 	else if (val < 0)
2332 		val = 0;
2333 
2334 	vha->u_ql2xiniexchg = val;
2335 	return strlen(buf);
2336 }
2337 
2338 static ssize_t
2339 qla2x00_dif_bundle_statistics_show(struct device *dev,
2340     struct device_attribute *attr, char *buf)
2341 {
2342 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2343 	struct qla_hw_data *ha = vha->hw;
2344 
2345 	return scnprintf(buf, PAGE_SIZE,
2346 	    "cross=%llu read=%llu write=%llu kalloc=%llu dma_alloc=%llu unusable=%u\n",
2347 	    ha->dif_bundle_crossed_pages, ha->dif_bundle_reads,
2348 	    ha->dif_bundle_writes, ha->dif_bundle_kallocs,
2349 	    ha->dif_bundle_dma_allocs, ha->pool.unusable.count);
2350 }
2351 
2352 static ssize_t
2353 qla2x00_fw_attr_show(struct device *dev,
2354     struct device_attribute *attr, char *buf)
2355 {
2356 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2357 	struct qla_hw_data *ha = vha->hw;
2358 
2359 	if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
2360 		return scnprintf(buf, PAGE_SIZE, "\n");
2361 
2362 	return scnprintf(buf, PAGE_SIZE, "%llx\n",
2363 	    (uint64_t)ha->fw_attributes_ext[1] << 48 |
2364 	    (uint64_t)ha->fw_attributes_ext[0] << 32 |
2365 	    (uint64_t)ha->fw_attributes_h << 16 |
2366 	    (uint64_t)ha->fw_attributes);
2367 }
2368 
2369 static ssize_t
2370 qla2x00_port_no_show(struct device *dev, struct device_attribute *attr,
2371     char *buf)
2372 {
2373 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2374 
2375 	return scnprintf(buf, PAGE_SIZE, "%u\n", vha->hw->port_no);
2376 }
2377 
2378 static ssize_t
2379 qla2x00_dport_diagnostics_show(struct device *dev,
2380 	struct device_attribute *attr, char *buf)
2381 {
2382 	scsi_qla_host_t *vha = shost_priv(class_to_shost(dev));
2383 
2384 	if (!IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw) &&
2385 	    !IS_QLA28XX(vha->hw))
2386 		return scnprintf(buf, PAGE_SIZE, "\n");
2387 
2388 	if (!*vha->dport_data)
2389 		return scnprintf(buf, PAGE_SIZE, "\n");
2390 
2391 	return scnprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
2392 	    vha->dport_data[0], vha->dport_data[1],
2393 	    vha->dport_data[2], vha->dport_data[3]);
2394 }
2395 static DEVICE_ATTR(dport_diagnostics, 0444,
2396 	   qla2x00_dport_diagnostics_show, NULL);
2397 
2398 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_driver_version_show, NULL);
2399 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
2400 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
2401 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
2402 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
2403 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
2404 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
2405 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
2406 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
2407 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
2408 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
2409 		   qla2x00_zio_timer_store);
2410 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
2411 		   qla2x00_beacon_store);
2412 static DEVICE_ATTR(beacon_config, 0644, qla2x00_beacon_config_show,
2413 		   qla2x00_beacon_config_store);
2414 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
2415 		   qla2x00_optrom_bios_version_show, NULL);
2416 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
2417 		   qla2x00_optrom_efi_version_show, NULL);
2418 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
2419 		   qla2x00_optrom_fcode_version_show, NULL);
2420 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
2421 		   NULL);
2422 static DEVICE_ATTR(optrom_gold_fw_version, S_IRUGO,
2423     qla2x00_optrom_gold_fw_version_show, NULL);
2424 static DEVICE_ATTR(84xx_fw_version, S_IRUGO, qla24xx_84xx_fw_version_show,
2425 		   NULL);
2426 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
2427 		   NULL);
2428 static DEVICE_ATTR(serdes_version, 0444, qla2x00_serdes_version_show, NULL);
2429 static DEVICE_ATTR(mpi_version, S_IRUGO, qla2x00_mpi_version_show, NULL);
2430 static DEVICE_ATTR(phy_version, S_IRUGO, qla2x00_phy_version_show, NULL);
2431 static DEVICE_ATTR(flash_block_size, S_IRUGO, qla2x00_flash_block_size_show,
2432 		   NULL);
2433 static DEVICE_ATTR(vlan_id, S_IRUGO, qla2x00_vlan_id_show, NULL);
2434 static DEVICE_ATTR(vn_port_mac_address, S_IRUGO,
2435 		   qla2x00_vn_port_mac_address_show, NULL);
2436 static DEVICE_ATTR(fabric_param, S_IRUGO, qla2x00_fabric_param_show, NULL);
2437 static DEVICE_ATTR(fw_state, S_IRUGO, qla2x00_fw_state_show, NULL);
2438 static DEVICE_ATTR(thermal_temp, S_IRUGO, qla2x00_thermal_temp_show, NULL);
2439 static DEVICE_ATTR(diag_requests, S_IRUGO, qla2x00_diag_requests_show, NULL);
2440 static DEVICE_ATTR(diag_megabytes, S_IRUGO, qla2x00_diag_megabytes_show, NULL);
2441 static DEVICE_ATTR(fw_dump_size, S_IRUGO, qla2x00_fw_dump_size_show, NULL);
2442 static DEVICE_ATTR(allow_cna_fw_dump, S_IRUGO | S_IWUSR,
2443 		   qla2x00_allow_cna_fw_dump_show,
2444 		   qla2x00_allow_cna_fw_dump_store);
2445 static DEVICE_ATTR(pep_version, S_IRUGO, qla2x00_pep_version_show, NULL);
2446 static DEVICE_ATTR(min_supported_speed, 0444,
2447 		   qla2x00_min_supported_speed_show, NULL);
2448 static DEVICE_ATTR(max_supported_speed, 0444,
2449 		   qla2x00_max_supported_speed_show, NULL);
2450 static DEVICE_ATTR(zio_threshold, 0644,
2451     qla_zio_threshold_show,
2452     qla_zio_threshold_store);
2453 static DEVICE_ATTR_RW(qlini_mode);
2454 static DEVICE_ATTR_RW(ql2xexchoffld);
2455 static DEVICE_ATTR_RW(ql2xiniexchg);
2456 static DEVICE_ATTR(dif_bundle_statistics, 0444,
2457     qla2x00_dif_bundle_statistics_show, NULL);
2458 static DEVICE_ATTR(port_speed, 0644, qla2x00_port_speed_show,
2459     qla2x00_port_speed_store);
2460 static DEVICE_ATTR(port_no, 0444, qla2x00_port_no_show, NULL);
2461 static DEVICE_ATTR(fw_attr, 0444, qla2x00_fw_attr_show, NULL);
2462 static DEVICE_ATTR_RO(edif_doorbell);
2463 
2464 
2465 struct device_attribute *qla2x00_host_attrs[] = {
2466 	&dev_attr_driver_version,
2467 	&dev_attr_fw_version,
2468 	&dev_attr_serial_num,
2469 	&dev_attr_isp_name,
2470 	&dev_attr_isp_id,
2471 	&dev_attr_model_name,
2472 	&dev_attr_model_desc,
2473 	&dev_attr_pci_info,
2474 	&dev_attr_link_state,
2475 	&dev_attr_zio,
2476 	&dev_attr_zio_timer,
2477 	&dev_attr_beacon,
2478 	&dev_attr_beacon_config,
2479 	&dev_attr_optrom_bios_version,
2480 	&dev_attr_optrom_efi_version,
2481 	&dev_attr_optrom_fcode_version,
2482 	&dev_attr_optrom_fw_version,
2483 	&dev_attr_84xx_fw_version,
2484 	&dev_attr_total_isp_aborts,
2485 	&dev_attr_serdes_version,
2486 	&dev_attr_mpi_version,
2487 	&dev_attr_phy_version,
2488 	&dev_attr_flash_block_size,
2489 	&dev_attr_vlan_id,
2490 	&dev_attr_vn_port_mac_address,
2491 	&dev_attr_fabric_param,
2492 	&dev_attr_fw_state,
2493 	&dev_attr_optrom_gold_fw_version,
2494 	&dev_attr_thermal_temp,
2495 	&dev_attr_diag_requests,
2496 	&dev_attr_diag_megabytes,
2497 	&dev_attr_fw_dump_size,
2498 	&dev_attr_allow_cna_fw_dump,
2499 	&dev_attr_pep_version,
2500 	&dev_attr_min_supported_speed,
2501 	&dev_attr_max_supported_speed,
2502 	&dev_attr_zio_threshold,
2503 	&dev_attr_dif_bundle_statistics,
2504 	&dev_attr_port_speed,
2505 	&dev_attr_port_no,
2506 	&dev_attr_fw_attr,
2507 	&dev_attr_dport_diagnostics,
2508 	&dev_attr_edif_doorbell,
2509 	&dev_attr_mpi_pause,
2510 	NULL, /* reserve for qlini_mode */
2511 	NULL, /* reserve for ql2xiniexchg */
2512 	NULL, /* reserve for ql2xexchoffld */
2513 	NULL,
2514 };
2515 
2516 void qla_insert_tgt_attrs(void)
2517 {
2518 	struct device_attribute **attr;
2519 
2520 	/* advance to empty slot */
2521 	for (attr = &qla2x00_host_attrs[0]; *attr; ++attr)
2522 		continue;
2523 
2524 	*attr = &dev_attr_qlini_mode;
2525 	attr++;
2526 	*attr = &dev_attr_ql2xiniexchg;
2527 	attr++;
2528 	*attr = &dev_attr_ql2xexchoffld;
2529 }
2530 
2531 /* Host attributes. */
2532 
2533 static void
2534 qla2x00_get_host_port_id(struct Scsi_Host *shost)
2535 {
2536 	scsi_qla_host_t *vha = shost_priv(shost);
2537 
2538 	fc_host_port_id(shost) = vha->d_id.b.domain << 16 |
2539 	    vha->d_id.b.area << 8 | vha->d_id.b.al_pa;
2540 }
2541 
2542 static void
2543 qla2x00_get_host_speed(struct Scsi_Host *shost)
2544 {
2545 	scsi_qla_host_t *vha = shost_priv(shost);
2546 	u32 speed;
2547 
2548 	if (IS_QLAFX00(vha->hw)) {
2549 		qlafx00_get_host_speed(shost);
2550 		return;
2551 	}
2552 
2553 	switch (vha->hw->link_data_rate) {
2554 	case PORT_SPEED_1GB:
2555 		speed = FC_PORTSPEED_1GBIT;
2556 		break;
2557 	case PORT_SPEED_2GB:
2558 		speed = FC_PORTSPEED_2GBIT;
2559 		break;
2560 	case PORT_SPEED_4GB:
2561 		speed = FC_PORTSPEED_4GBIT;
2562 		break;
2563 	case PORT_SPEED_8GB:
2564 		speed = FC_PORTSPEED_8GBIT;
2565 		break;
2566 	case PORT_SPEED_10GB:
2567 		speed = FC_PORTSPEED_10GBIT;
2568 		break;
2569 	case PORT_SPEED_16GB:
2570 		speed = FC_PORTSPEED_16GBIT;
2571 		break;
2572 	case PORT_SPEED_32GB:
2573 		speed = FC_PORTSPEED_32GBIT;
2574 		break;
2575 	case PORT_SPEED_64GB:
2576 		speed = FC_PORTSPEED_64GBIT;
2577 		break;
2578 	default:
2579 		speed = FC_PORTSPEED_UNKNOWN;
2580 		break;
2581 	}
2582 
2583 	fc_host_speed(shost) = speed;
2584 }
2585 
2586 static void
2587 qla2x00_get_host_port_type(struct Scsi_Host *shost)
2588 {
2589 	scsi_qla_host_t *vha = shost_priv(shost);
2590 	uint32_t port_type;
2591 
2592 	if (vha->vp_idx) {
2593 		fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
2594 		return;
2595 	}
2596 	switch (vha->hw->current_topology) {
2597 	case ISP_CFG_NL:
2598 		port_type = FC_PORTTYPE_LPORT;
2599 		break;
2600 	case ISP_CFG_FL:
2601 		port_type = FC_PORTTYPE_NLPORT;
2602 		break;
2603 	case ISP_CFG_N:
2604 		port_type = FC_PORTTYPE_PTP;
2605 		break;
2606 	case ISP_CFG_F:
2607 		port_type = FC_PORTTYPE_NPORT;
2608 		break;
2609 	default:
2610 		port_type = FC_PORTTYPE_UNKNOWN;
2611 		break;
2612 	}
2613 
2614 	fc_host_port_type(shost) = port_type;
2615 }
2616 
2617 static void
2618 qla2x00_get_starget_node_name(struct scsi_target *starget)
2619 {
2620 	struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2621 	scsi_qla_host_t *vha = shost_priv(host);
2622 	fc_port_t *fcport;
2623 	u64 node_name = 0;
2624 
2625 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
2626 		if (fcport->rport &&
2627 		    starget->id == fcport->rport->scsi_target_id) {
2628 			node_name = wwn_to_u64(fcport->node_name);
2629 			break;
2630 		}
2631 	}
2632 
2633 	fc_starget_node_name(starget) = node_name;
2634 }
2635 
2636 static void
2637 qla2x00_get_starget_port_name(struct scsi_target *starget)
2638 {
2639 	struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2640 	scsi_qla_host_t *vha = shost_priv(host);
2641 	fc_port_t *fcport;
2642 	u64 port_name = 0;
2643 
2644 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
2645 		if (fcport->rport &&
2646 		    starget->id == fcport->rport->scsi_target_id) {
2647 			port_name = wwn_to_u64(fcport->port_name);
2648 			break;
2649 		}
2650 	}
2651 
2652 	fc_starget_port_name(starget) = port_name;
2653 }
2654 
2655 static void
2656 qla2x00_get_starget_port_id(struct scsi_target *starget)
2657 {
2658 	struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
2659 	scsi_qla_host_t *vha = shost_priv(host);
2660 	fc_port_t *fcport;
2661 	uint32_t port_id = ~0U;
2662 
2663 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
2664 		if (fcport->rport &&
2665 		    starget->id == fcport->rport->scsi_target_id) {
2666 			port_id = fcport->d_id.b.domain << 16 |
2667 			    fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
2668 			break;
2669 		}
2670 	}
2671 
2672 	fc_starget_port_id(starget) = port_id;
2673 }
2674 
2675 static inline void
2676 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
2677 {
2678 	rport->dev_loss_tmo = timeout ? timeout : 1;
2679 }
2680 
2681 static void
2682 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
2683 {
2684 	struct Scsi_Host *host = rport_to_shost(rport);
2685 	fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2686 	unsigned long flags;
2687 
2688 	if (!fcport)
2689 		return;
2690 
2691 	/* Now that the rport has been deleted, set the fcport state to
2692 	   FCS_DEVICE_DEAD */
2693 	qla2x00_set_fcport_state(fcport, FCS_DEVICE_DEAD);
2694 
2695 	/*
2696 	 * Transport has effectively 'deleted' the rport, clear
2697 	 * all local references.
2698 	 */
2699 	spin_lock_irqsave(host->host_lock, flags);
2700 	fcport->rport = fcport->drport = NULL;
2701 	*((fc_port_t **)rport->dd_data) = NULL;
2702 	spin_unlock_irqrestore(host->host_lock, flags);
2703 
2704 	if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
2705 		return;
2706 
2707 	if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
2708 		qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
2709 		return;
2710 	}
2711 }
2712 
2713 static void
2714 qla2x00_terminate_rport_io(struct fc_rport *rport)
2715 {
2716 	fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
2717 
2718 	if (!fcport)
2719 		return;
2720 
2721 	if (test_bit(UNLOADING, &fcport->vha->dpc_flags))
2722 		return;
2723 
2724 	if (test_bit(ABORT_ISP_ACTIVE, &fcport->vha->dpc_flags))
2725 		return;
2726 
2727 	if (unlikely(pci_channel_offline(fcport->vha->hw->pdev))) {
2728 		qla2x00_abort_all_cmds(fcport->vha, DID_NO_CONNECT << 16);
2729 		return;
2730 	}
2731 	/*
2732 	 * At this point all fcport's software-states are cleared.  Perform any
2733 	 * final cleanup of firmware resources (PCBs and XCBs).
2734 	 */
2735 	if (fcport->loop_id != FC_NO_LOOP_ID) {
2736 		if (IS_FWI2_CAPABLE(fcport->vha->hw))
2737 			fcport->vha->hw->isp_ops->fabric_logout(fcport->vha,
2738 			    fcport->loop_id, fcport->d_id.b.domain,
2739 			    fcport->d_id.b.area, fcport->d_id.b.al_pa);
2740 		else
2741 			qla2x00_port_logout(fcport->vha, fcport);
2742 	}
2743 }
2744 
2745 static int
2746 qla2x00_issue_lip(struct Scsi_Host *shost)
2747 {
2748 	scsi_qla_host_t *vha = shost_priv(shost);
2749 
2750 	if (IS_QLAFX00(vha->hw))
2751 		return 0;
2752 
2753 	if (vha->hw->flags.port_isolated)
2754 		return 0;
2755 
2756 	qla2x00_loop_reset(vha);
2757 	return 0;
2758 }
2759 
2760 static struct fc_host_statistics *
2761 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
2762 {
2763 	scsi_qla_host_t *vha = shost_priv(shost);
2764 	struct qla_hw_data *ha = vha->hw;
2765 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2766 	int rval;
2767 	struct link_statistics *stats;
2768 	dma_addr_t stats_dma;
2769 	struct fc_host_statistics *p = &vha->fc_host_stat;
2770 	struct qla_qpair *qpair;
2771 	int i;
2772 	u64 ib = 0, ob = 0, ir = 0, or = 0;
2773 
2774 	memset(p, -1, sizeof(*p));
2775 
2776 	if (IS_QLAFX00(vha->hw))
2777 		goto done;
2778 
2779 	if (test_bit(UNLOADING, &vha->dpc_flags))
2780 		goto done;
2781 
2782 	if (unlikely(pci_channel_offline(ha->pdev)))
2783 		goto done;
2784 
2785 	if (qla2x00_chip_is_down(vha))
2786 		goto done;
2787 
2788 	stats = dma_alloc_coherent(&ha->pdev->dev, sizeof(*stats), &stats_dma,
2789 				   GFP_KERNEL);
2790 	if (!stats) {
2791 		ql_log(ql_log_warn, vha, 0x707d,
2792 		    "Failed to allocate memory for stats.\n");
2793 		goto done;
2794 	}
2795 
2796 	rval = QLA_FUNCTION_FAILED;
2797 	if (IS_FWI2_CAPABLE(ha)) {
2798 		rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, 0);
2799 	} else if (atomic_read(&base_vha->loop_state) == LOOP_READY &&
2800 	    !ha->dpc_active) {
2801 		/* Must be in a 'READY' state for statistics retrieval. */
2802 		rval = qla2x00_get_link_status(base_vha, base_vha->loop_id,
2803 						stats, stats_dma);
2804 	}
2805 
2806 	if (rval != QLA_SUCCESS)
2807 		goto done_free;
2808 
2809 	/* --- */
2810 	for (i = 0; i < vha->hw->max_qpairs; i++) {
2811 		qpair = vha->hw->queue_pair_map[i];
2812 		if (!qpair)
2813 			continue;
2814 		ir += qpair->counters.input_requests;
2815 		or += qpair->counters.output_requests;
2816 		ib += qpair->counters.input_bytes;
2817 		ob += qpair->counters.output_bytes;
2818 	}
2819 	ir += ha->base_qpair->counters.input_requests;
2820 	or += ha->base_qpair->counters.output_requests;
2821 	ib += ha->base_qpair->counters.input_bytes;
2822 	ob += ha->base_qpair->counters.output_bytes;
2823 
2824 	ir += vha->qla_stats.input_requests;
2825 	or += vha->qla_stats.output_requests;
2826 	ib += vha->qla_stats.input_bytes;
2827 	ob += vha->qla_stats.output_bytes;
2828 	/* --- */
2829 
2830 	p->link_failure_count = le32_to_cpu(stats->link_fail_cnt);
2831 	p->loss_of_sync_count = le32_to_cpu(stats->loss_sync_cnt);
2832 	p->loss_of_signal_count = le32_to_cpu(stats->loss_sig_cnt);
2833 	p->prim_seq_protocol_err_count = le32_to_cpu(stats->prim_seq_err_cnt);
2834 	p->invalid_tx_word_count = le32_to_cpu(stats->inval_xmit_word_cnt);
2835 	p->invalid_crc_count = le32_to_cpu(stats->inval_crc_cnt);
2836 	if (IS_FWI2_CAPABLE(ha)) {
2837 		p->lip_count = le32_to_cpu(stats->lip_cnt);
2838 		p->tx_frames = le32_to_cpu(stats->tx_frames);
2839 		p->rx_frames = le32_to_cpu(stats->rx_frames);
2840 		p->dumped_frames = le32_to_cpu(stats->discarded_frames);
2841 		p->nos_count = le32_to_cpu(stats->nos_rcvd);
2842 		p->error_frames =
2843 		    le32_to_cpu(stats->dropped_frames) +
2844 		    le32_to_cpu(stats->discarded_frames);
2845 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
2846 			p->rx_words = le64_to_cpu(stats->fpm_recv_word_cnt);
2847 			p->tx_words = le64_to_cpu(stats->fpm_xmit_word_cnt);
2848 		} else {
2849 			p->rx_words = ib >> 2;
2850 			p->tx_words = ob >> 2;
2851 		}
2852 	}
2853 
2854 	p->fcp_control_requests = vha->qla_stats.control_requests;
2855 	p->fcp_input_requests = ir;
2856 	p->fcp_output_requests = or;
2857 	p->fcp_input_megabytes  = ib >> 20;
2858 	p->fcp_output_megabytes = ob >> 20;
2859 	p->seconds_since_last_reset =
2860 	    get_jiffies_64() - vha->qla_stats.jiffies_at_last_reset;
2861 	do_div(p->seconds_since_last_reset, HZ);
2862 
2863 done_free:
2864 	dma_free_coherent(&ha->pdev->dev, sizeof(struct link_statistics),
2865 	    stats, stats_dma);
2866 done:
2867 	return p;
2868 }
2869 
2870 static void
2871 qla2x00_reset_host_stats(struct Scsi_Host *shost)
2872 {
2873 	scsi_qla_host_t *vha = shost_priv(shost);
2874 	struct qla_hw_data *ha = vha->hw;
2875 	struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2876 	struct link_statistics *stats;
2877 	dma_addr_t stats_dma;
2878 	int i;
2879 	struct qla_qpair *qpair;
2880 
2881 	memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
2882 	memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
2883 	for (i = 0; i < vha->hw->max_qpairs; i++) {
2884 		qpair = vha->hw->queue_pair_map[i];
2885 		if (!qpair)
2886 			continue;
2887 		memset(&qpair->counters, 0, sizeof(qpair->counters));
2888 	}
2889 	memset(&ha->base_qpair->counters, 0, sizeof(qpair->counters));
2890 
2891 	vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2892 
2893 	if (IS_FWI2_CAPABLE(ha)) {
2894 		int rval;
2895 
2896 		stats = dma_alloc_coherent(&ha->pdev->dev,
2897 		    sizeof(*stats), &stats_dma, GFP_KERNEL);
2898 		if (!stats) {
2899 			ql_log(ql_log_warn, vha, 0x70d7,
2900 			    "Failed to allocate memory for stats.\n");
2901 			return;
2902 		}
2903 
2904 		/* reset firmware statistics */
2905 		rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, BIT_0);
2906 		if (rval != QLA_SUCCESS)
2907 			ql_log(ql_log_warn, vha, 0x70de,
2908 			       "Resetting ISP statistics failed: rval = %d\n",
2909 			       rval);
2910 
2911 		dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
2912 		    stats, stats_dma);
2913 	}
2914 }
2915 
2916 static void
2917 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
2918 {
2919 	scsi_qla_host_t *vha = shost_priv(shost);
2920 
2921 	qla2x00_get_sym_node_name(vha, fc_host_symbolic_name(shost),
2922 	    sizeof(fc_host_symbolic_name(shost)));
2923 }
2924 
2925 static void
2926 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
2927 {
2928 	scsi_qla_host_t *vha = shost_priv(shost);
2929 
2930 	set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
2931 }
2932 
2933 static void
2934 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
2935 {
2936 	scsi_qla_host_t *vha = shost_priv(shost);
2937 	static const uint8_t node_name[WWN_SIZE] = {
2938 		0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
2939 	};
2940 	u64 fabric_name = wwn_to_u64(node_name);
2941 
2942 	if (vha->device_flags & SWITCH_FOUND)
2943 		fabric_name = wwn_to_u64(vha->fabric_node_name);
2944 
2945 	fc_host_fabric_name(shost) = fabric_name;
2946 }
2947 
2948 static void
2949 qla2x00_get_host_port_state(struct Scsi_Host *shost)
2950 {
2951 	scsi_qla_host_t *vha = shost_priv(shost);
2952 	struct scsi_qla_host *base_vha = pci_get_drvdata(vha->hw->pdev);
2953 
2954 	if (!base_vha->flags.online) {
2955 		fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
2956 		return;
2957 	}
2958 
2959 	switch (atomic_read(&base_vha->loop_state)) {
2960 	case LOOP_UPDATE:
2961 		fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
2962 		break;
2963 	case LOOP_DOWN:
2964 		if (test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags))
2965 			fc_host_port_state(shost) = FC_PORTSTATE_DIAGNOSTICS;
2966 		else
2967 			fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
2968 		break;
2969 	case LOOP_DEAD:
2970 		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
2971 		break;
2972 	case LOOP_READY:
2973 		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
2974 		break;
2975 	default:
2976 		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
2977 		break;
2978 	}
2979 }
2980 
2981 static int
2982 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
2983 {
2984 	int	ret = 0;
2985 	uint8_t	qos = 0;
2986 	scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
2987 	scsi_qla_host_t *vha = NULL;
2988 	struct qla_hw_data *ha = base_vha->hw;
2989 	int	cnt;
2990 	struct req_que *req = ha->req_q_map[0];
2991 	struct qla_qpair *qpair;
2992 
2993 	ret = qla24xx_vport_create_req_sanity_check(fc_vport);
2994 	if (ret) {
2995 		ql_log(ql_log_warn, vha, 0x707e,
2996 		    "Vport sanity check failed, status %x\n", ret);
2997 		return (ret);
2998 	}
2999 
3000 	vha = qla24xx_create_vhost(fc_vport);
3001 	if (vha == NULL) {
3002 		ql_log(ql_log_warn, vha, 0x707f, "Vport create host failed.\n");
3003 		return FC_VPORT_FAILED;
3004 	}
3005 	if (disable) {
3006 		atomic_set(&vha->vp_state, VP_OFFLINE);
3007 		fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
3008 	} else
3009 		atomic_set(&vha->vp_state, VP_FAILED);
3010 
3011 	/* ready to create vport */
3012 	ql_log(ql_log_info, vha, 0x7080,
3013 	    "VP entry id %d assigned.\n", vha->vp_idx);
3014 
3015 	/* initialized vport states */
3016 	atomic_set(&vha->loop_state, LOOP_DOWN);
3017 	vha->vp_err_state = VP_ERR_PORTDWN;
3018 	vha->vp_prev_err_state = VP_ERR_UNKWN;
3019 	/* Check if physical ha port is Up */
3020 	if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
3021 	    atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
3022 		/* Don't retry or attempt login of this virtual port */
3023 		ql_dbg(ql_dbg_user, vha, 0x7081,
3024 		    "Vport loop state is not UP.\n");
3025 		atomic_set(&vha->loop_state, LOOP_DEAD);
3026 		if (!disable)
3027 			fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
3028 	}
3029 
3030 	if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
3031 		if (ha->fw_attributes & BIT_4) {
3032 			int prot = 0, guard;
3033 
3034 			vha->flags.difdix_supported = 1;
3035 			ql_dbg(ql_dbg_user, vha, 0x7082,
3036 			    "Registered for DIF/DIX type 1 and 3 protection.\n");
3037 			if (ql2xenabledif == 1)
3038 				prot = SHOST_DIX_TYPE0_PROTECTION;
3039 			scsi_host_set_prot(vha->host,
3040 			    prot | SHOST_DIF_TYPE1_PROTECTION
3041 			    | SHOST_DIF_TYPE2_PROTECTION
3042 			    | SHOST_DIF_TYPE3_PROTECTION
3043 			    | SHOST_DIX_TYPE1_PROTECTION
3044 			    | SHOST_DIX_TYPE2_PROTECTION
3045 			    | SHOST_DIX_TYPE3_PROTECTION);
3046 
3047 			guard = SHOST_DIX_GUARD_CRC;
3048 
3049 			if (IS_PI_IPGUARD_CAPABLE(ha) &&
3050 			    (ql2xenabledif > 1 || IS_PI_DIFB_DIX0_CAPABLE(ha)))
3051 				guard |= SHOST_DIX_GUARD_IP;
3052 
3053 			scsi_host_set_guard(vha->host, guard);
3054 		} else
3055 			vha->flags.difdix_supported = 0;
3056 	}
3057 
3058 	if (scsi_add_host_with_dma(vha->host, &fc_vport->dev,
3059 				   &ha->pdev->dev)) {
3060 		ql_dbg(ql_dbg_user, vha, 0x7083,
3061 		    "scsi_add_host failure for VP[%d].\n", vha->vp_idx);
3062 		goto vport_create_failed_2;
3063 	}
3064 
3065 	/* initialize attributes */
3066 	fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
3067 	fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
3068 	fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
3069 	fc_host_supported_classes(vha->host) =
3070 		fc_host_supported_classes(base_vha->host);
3071 	fc_host_supported_speeds(vha->host) =
3072 		fc_host_supported_speeds(base_vha->host);
3073 
3074 	qlt_vport_create(vha, ha);
3075 	qla24xx_vport_disable(fc_vport, disable);
3076 
3077 	if (!ql2xmqsupport || !ha->npiv_info)
3078 		goto vport_queue;
3079 
3080 	/* Create a request queue in QoS mode for the vport */
3081 	for (cnt = 0; cnt < ha->nvram_npiv_size; cnt++) {
3082 		if (memcmp(ha->npiv_info[cnt].port_name, vha->port_name, 8) == 0
3083 			&& memcmp(ha->npiv_info[cnt].node_name, vha->node_name,
3084 					8) == 0) {
3085 			qos = ha->npiv_info[cnt].q_qos;
3086 			break;
3087 		}
3088 	}
3089 
3090 	if (qos) {
3091 		qpair = qla2xxx_create_qpair(vha, qos, vha->vp_idx, true);
3092 		if (!qpair)
3093 			ql_log(ql_log_warn, vha, 0x7084,
3094 			    "Can't create qpair for VP[%d]\n",
3095 			    vha->vp_idx);
3096 		else {
3097 			ql_dbg(ql_dbg_multiq, vha, 0xc001,
3098 			    "Queue pair: %d Qos: %d) created for VP[%d]\n",
3099 			    qpair->id, qos, vha->vp_idx);
3100 			ql_dbg(ql_dbg_user, vha, 0x7085,
3101 			    "Queue Pair: %d Qos: %d) created for VP[%d]\n",
3102 			    qpair->id, qos, vha->vp_idx);
3103 			req = qpair->req;
3104 			vha->qpair = qpair;
3105 		}
3106 	}
3107 
3108 vport_queue:
3109 	vha->req = req;
3110 	return 0;
3111 
3112 vport_create_failed_2:
3113 	qla24xx_disable_vp(vha);
3114 	qla24xx_deallocate_vp_id(vha);
3115 	scsi_host_put(vha->host);
3116 	return FC_VPORT_FAILED;
3117 }
3118 
3119 static int
3120 qla24xx_vport_delete(struct fc_vport *fc_vport)
3121 {
3122 	scsi_qla_host_t *vha = fc_vport->dd_data;
3123 	struct qla_hw_data *ha = vha->hw;
3124 	uint16_t id = vha->vp_idx;
3125 
3126 	set_bit(VPORT_DELETE, &vha->dpc_flags);
3127 
3128 	while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
3129 	    test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags))
3130 		msleep(1000);
3131 
3132 
3133 	qla24xx_disable_vp(vha);
3134 	qla2x00_wait_for_sess_deletion(vha);
3135 
3136 	qla_nvme_delete(vha);
3137 	qla_enode_stop(vha);
3138 	qla_edb_stop(vha);
3139 
3140 	vha->flags.delete_progress = 1;
3141 
3142 	qlt_remove_target(ha, vha);
3143 
3144 	fc_remove_host(vha->host);
3145 
3146 	scsi_remove_host(vha->host);
3147 
3148 	/* Allow timer to run to drain queued items, when removing vp */
3149 	qla24xx_deallocate_vp_id(vha);
3150 
3151 	if (vha->timer_active) {
3152 		qla2x00_vp_stop_timer(vha);
3153 		ql_dbg(ql_dbg_user, vha, 0x7086,
3154 		    "Timer for the VP[%d] has stopped\n", vha->vp_idx);
3155 	}
3156 
3157 	qla2x00_free_fcports(vha);
3158 
3159 	mutex_lock(&ha->vport_lock);
3160 	ha->cur_vport_count--;
3161 	clear_bit(vha->vp_idx, ha->vp_idx_map);
3162 	mutex_unlock(&ha->vport_lock);
3163 
3164 	dma_free_coherent(&ha->pdev->dev, vha->gnl.size, vha->gnl.l,
3165 	    vha->gnl.ldma);
3166 
3167 	vha->gnl.l = NULL;
3168 
3169 	vfree(vha->scan.l);
3170 
3171 	if (vha->qpair && vha->qpair->vp_idx == vha->vp_idx) {
3172 		if (qla2xxx_delete_qpair(vha, vha->qpair) != QLA_SUCCESS)
3173 			ql_log(ql_log_warn, vha, 0x7087,
3174 			    "Queue Pair delete failed.\n");
3175 	}
3176 
3177 	ql_log(ql_log_info, vha, 0x7088, "VP[%d] deleted.\n", id);
3178 	scsi_host_put(vha->host);
3179 	return 0;
3180 }
3181 
3182 static int
3183 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
3184 {
3185 	scsi_qla_host_t *vha = fc_vport->dd_data;
3186 
3187 	if (disable)
3188 		qla24xx_disable_vp(vha);
3189 	else
3190 		qla24xx_enable_vp(vha);
3191 
3192 	return 0;
3193 }
3194 
3195 struct fc_function_template qla2xxx_transport_functions = {
3196 
3197 	.show_host_node_name = 1,
3198 	.show_host_port_name = 1,
3199 	.show_host_supported_classes = 1,
3200 	.show_host_supported_speeds = 1,
3201 
3202 	.get_host_port_id = qla2x00_get_host_port_id,
3203 	.show_host_port_id = 1,
3204 	.get_host_speed = qla2x00_get_host_speed,
3205 	.show_host_speed = 1,
3206 	.get_host_port_type = qla2x00_get_host_port_type,
3207 	.show_host_port_type = 1,
3208 	.get_host_symbolic_name = qla2x00_get_host_symbolic_name,
3209 	.show_host_symbolic_name = 1,
3210 	.set_host_system_hostname = qla2x00_set_host_system_hostname,
3211 	.show_host_system_hostname = 1,
3212 	.get_host_fabric_name = qla2x00_get_host_fabric_name,
3213 	.show_host_fabric_name = 1,
3214 	.get_host_port_state = qla2x00_get_host_port_state,
3215 	.show_host_port_state = 1,
3216 
3217 	.dd_fcrport_size = sizeof(struct fc_port *),
3218 	.show_rport_supported_classes = 1,
3219 
3220 	.get_starget_node_name = qla2x00_get_starget_node_name,
3221 	.show_starget_node_name = 1,
3222 	.get_starget_port_name = qla2x00_get_starget_port_name,
3223 	.show_starget_port_name = 1,
3224 	.get_starget_port_id  = qla2x00_get_starget_port_id,
3225 	.show_starget_port_id = 1,
3226 
3227 	.set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
3228 	.show_rport_dev_loss_tmo = 1,
3229 
3230 	.issue_fc_host_lip = qla2x00_issue_lip,
3231 	.dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
3232 	.terminate_rport_io = qla2x00_terminate_rport_io,
3233 	.get_fc_host_stats = qla2x00_get_fc_host_stats,
3234 	.reset_fc_host_stats = qla2x00_reset_host_stats,
3235 
3236 	.vport_create = qla24xx_vport_create,
3237 	.vport_disable = qla24xx_vport_disable,
3238 	.vport_delete = qla24xx_vport_delete,
3239 	.bsg_request = qla24xx_bsg_request,
3240 	.bsg_timeout = qla24xx_bsg_timeout,
3241 };
3242 
3243 struct fc_function_template qla2xxx_transport_vport_functions = {
3244 
3245 	.show_host_node_name = 1,
3246 	.show_host_port_name = 1,
3247 	.show_host_supported_classes = 1,
3248 
3249 	.get_host_port_id = qla2x00_get_host_port_id,
3250 	.show_host_port_id = 1,
3251 	.get_host_speed = qla2x00_get_host_speed,
3252 	.show_host_speed = 1,
3253 	.get_host_port_type = qla2x00_get_host_port_type,
3254 	.show_host_port_type = 1,
3255 	.get_host_symbolic_name = qla2x00_get_host_symbolic_name,
3256 	.show_host_symbolic_name = 1,
3257 	.set_host_system_hostname = qla2x00_set_host_system_hostname,
3258 	.show_host_system_hostname = 1,
3259 	.get_host_fabric_name = qla2x00_get_host_fabric_name,
3260 	.show_host_fabric_name = 1,
3261 	.get_host_port_state = qla2x00_get_host_port_state,
3262 	.show_host_port_state = 1,
3263 
3264 	.dd_fcrport_size = sizeof(struct fc_port *),
3265 	.show_rport_supported_classes = 1,
3266 
3267 	.get_starget_node_name = qla2x00_get_starget_node_name,
3268 	.show_starget_node_name = 1,
3269 	.get_starget_port_name = qla2x00_get_starget_port_name,
3270 	.show_starget_port_name = 1,
3271 	.get_starget_port_id  = qla2x00_get_starget_port_id,
3272 	.show_starget_port_id = 1,
3273 
3274 	.set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
3275 	.show_rport_dev_loss_tmo = 1,
3276 
3277 	.issue_fc_host_lip = qla2x00_issue_lip,
3278 	.dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
3279 	.terminate_rport_io = qla2x00_terminate_rport_io,
3280 	.get_fc_host_stats = qla2x00_get_fc_host_stats,
3281 	.reset_fc_host_stats = qla2x00_reset_host_stats,
3282 
3283 	.bsg_request = qla24xx_bsg_request,
3284 	.bsg_timeout = qla24xx_bsg_timeout,
3285 };
3286 
3287 void
3288 qla2x00_init_host_attr(scsi_qla_host_t *vha)
3289 {
3290 	struct qla_hw_data *ha = vha->hw;
3291 	u32 speeds = FC_PORTSPEED_UNKNOWN;
3292 
3293 	fc_host_dev_loss_tmo(vha->host) = ha->port_down_retry_count;
3294 	fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
3295 	fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
3296 	fc_host_supported_classes(vha->host) = ha->base_qpair->enable_class_2 ?
3297 			(FC_COS_CLASS2|FC_COS_CLASS3) : FC_COS_CLASS3;
3298 	fc_host_max_npiv_vports(vha->host) = ha->max_npiv_vports;
3299 	fc_host_npiv_vports_inuse(vha->host) = ha->cur_vport_count;
3300 
3301 	speeds = qla25xx_fdmi_port_speed_capability(ha);
3302 
3303 	fc_host_supported_speeds(vha->host) = speeds;
3304 }
3305