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