[SCSI] qla2xxx: Correct synchronization of software/firmware fcport states.
[linux-2.6/mini2440.git] / drivers / scsi / qla2xxx / qla_attr.c
blob45e7dcb4b34d8b276edae17cf56a13c749317fa3
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
9 #include <linux/kthread.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
13 static int qla24xx_vport_disable(struct fc_vport *, bool);
15 /* SYSFS attributes --------------------------------------------------------- */
17 static ssize_t
18 qla2x00_sysfs_read_fw_dump(struct kobject *kobj,
19 struct bin_attribute *bin_attr,
20 char *buf, loff_t off, size_t count)
22 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
23 struct device, kobj)));
25 if (ha->fw_dump_reading == 0)
26 return 0;
28 return memory_read_from_buffer(buf, count, &off, ha->fw_dump,
29 ha->fw_dump_len);
32 static ssize_t
33 qla2x00_sysfs_write_fw_dump(struct kobject *kobj,
34 struct bin_attribute *bin_attr,
35 char *buf, loff_t off, size_t count)
37 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
38 struct device, kobj)));
39 int reading;
41 if (off != 0)
42 return (0);
44 reading = simple_strtol(buf, NULL, 10);
45 switch (reading) {
46 case 0:
47 if (!ha->fw_dump_reading)
48 break;
50 qla_printk(KERN_INFO, ha,
51 "Firmware dump cleared on (%ld).\n", ha->host_no);
53 ha->fw_dump_reading = 0;
54 ha->fw_dumped = 0;
55 break;
56 case 1:
57 if (ha->fw_dumped && !ha->fw_dump_reading) {
58 ha->fw_dump_reading = 1;
60 qla_printk(KERN_INFO, ha,
61 "Raw firmware dump ready for read on (%ld).\n",
62 ha->host_no);
64 break;
65 case 2:
66 qla2x00_alloc_fw_dump(ha);
67 break;
68 case 3:
69 qla2x00_system_error(ha);
70 break;
72 return (count);
75 static struct bin_attribute sysfs_fw_dump_attr = {
76 .attr = {
77 .name = "fw_dump",
78 .mode = S_IRUSR | S_IWUSR,
80 .size = 0,
81 .read = qla2x00_sysfs_read_fw_dump,
82 .write = qla2x00_sysfs_write_fw_dump,
85 static ssize_t
86 qla2x00_sysfs_read_nvram(struct kobject *kobj,
87 struct bin_attribute *bin_attr,
88 char *buf, loff_t off, size_t count)
90 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
91 struct device, kobj)));
93 if (!capable(CAP_SYS_ADMIN))
94 return 0;
96 /* Read NVRAM data from cache. */
97 return memory_read_from_buffer(buf, count, &off, ha->nvram,
98 ha->nvram_size);
101 static ssize_t
102 qla2x00_sysfs_write_nvram(struct kobject *kobj,
103 struct bin_attribute *bin_attr,
104 char *buf, loff_t off, size_t count)
106 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
107 struct device, kobj)));
108 uint16_t cnt;
110 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
111 return 0;
113 /* Checksum NVRAM. */
114 if (IS_FWI2_CAPABLE(ha)) {
115 uint32_t *iter;
116 uint32_t chksum;
118 iter = (uint32_t *)buf;
119 chksum = 0;
120 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
121 chksum += le32_to_cpu(*iter++);
122 chksum = ~chksum + 1;
123 *iter = cpu_to_le32(chksum);
124 } else {
125 uint8_t *iter;
126 uint8_t chksum;
128 iter = (uint8_t *)buf;
129 chksum = 0;
130 for (cnt = 0; cnt < count - 1; cnt++)
131 chksum += *iter++;
132 chksum = ~chksum + 1;
133 *iter = chksum;
136 /* Write NVRAM. */
137 ha->isp_ops->write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
138 ha->isp_ops->read_nvram(ha, (uint8_t *)ha->nvram, ha->nvram_base,
139 count);
141 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
143 return (count);
146 static struct bin_attribute sysfs_nvram_attr = {
147 .attr = {
148 .name = "nvram",
149 .mode = S_IRUSR | S_IWUSR,
151 .size = 512,
152 .read = qla2x00_sysfs_read_nvram,
153 .write = qla2x00_sysfs_write_nvram,
156 static ssize_t
157 qla2x00_sysfs_read_optrom(struct kobject *kobj,
158 struct bin_attribute *bin_attr,
159 char *buf, loff_t off, size_t count)
161 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
162 struct device, kobj)));
164 if (ha->optrom_state != QLA_SREADING)
165 return 0;
167 return memory_read_from_buffer(buf, count, &off, ha->optrom_buffer,
168 ha->optrom_region_size);
171 static ssize_t
172 qla2x00_sysfs_write_optrom(struct kobject *kobj,
173 struct bin_attribute *bin_attr,
174 char *buf, loff_t off, size_t count)
176 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
177 struct device, kobj)));
179 if (ha->optrom_state != QLA_SWRITING)
180 return -EINVAL;
181 if (off > ha->optrom_region_size)
182 return -ERANGE;
183 if (off + count > ha->optrom_region_size)
184 count = ha->optrom_region_size - off;
186 memcpy(&ha->optrom_buffer[off], buf, count);
188 return count;
191 static struct bin_attribute sysfs_optrom_attr = {
192 .attr = {
193 .name = "optrom",
194 .mode = S_IRUSR | S_IWUSR,
196 .size = 0,
197 .read = qla2x00_sysfs_read_optrom,
198 .write = qla2x00_sysfs_write_optrom,
201 static ssize_t
202 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj,
203 struct bin_attribute *bin_attr,
204 char *buf, loff_t off, size_t count)
206 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
207 struct device, kobj)));
208 uint32_t start = 0;
209 uint32_t size = ha->optrom_size;
210 int val, valid;
212 if (off)
213 return 0;
215 if (sscanf(buf, "%d:%x:%x", &val, &start, &size) < 1)
216 return -EINVAL;
217 if (start > ha->optrom_size)
218 return -EINVAL;
220 switch (val) {
221 case 0:
222 if (ha->optrom_state != QLA_SREADING &&
223 ha->optrom_state != QLA_SWRITING)
224 break;
226 ha->optrom_state = QLA_SWAITING;
228 DEBUG2(qla_printk(KERN_INFO, ha,
229 "Freeing flash region allocation -- 0x%x bytes.\n",
230 ha->optrom_region_size));
232 vfree(ha->optrom_buffer);
233 ha->optrom_buffer = NULL;
234 break;
235 case 1:
236 if (ha->optrom_state != QLA_SWAITING)
237 break;
239 if (start & 0xfff) {
240 qla_printk(KERN_WARNING, ha,
241 "Invalid start region 0x%x/0x%x.\n", start, size);
242 return -EINVAL;
245 ha->optrom_region_start = start;
246 ha->optrom_region_size = start + size > ha->optrom_size ?
247 ha->optrom_size - start : size;
249 ha->optrom_state = QLA_SREADING;
250 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
251 if (ha->optrom_buffer == NULL) {
252 qla_printk(KERN_WARNING, ha,
253 "Unable to allocate memory for optrom retrieval "
254 "(%x).\n", ha->optrom_region_size);
256 ha->optrom_state = QLA_SWAITING;
257 return count;
260 DEBUG2(qla_printk(KERN_INFO, ha,
261 "Reading flash region -- 0x%x/0x%x.\n",
262 ha->optrom_region_start, ha->optrom_region_size));
264 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
265 ha->isp_ops->read_optrom(ha, ha->optrom_buffer,
266 ha->optrom_region_start, ha->optrom_region_size);
267 break;
268 case 2:
269 if (ha->optrom_state != QLA_SWAITING)
270 break;
273 * We need to be more restrictive on which FLASH regions are
274 * allowed to be updated via user-space. Regions accessible
275 * via this method include:
277 * ISP21xx/ISP22xx/ISP23xx type boards:
279 * 0x000000 -> 0x020000 -- Boot code.
281 * ISP2322/ISP24xx type boards:
283 * 0x000000 -> 0x07ffff -- Boot code.
284 * 0x080000 -> 0x0fffff -- Firmware.
286 * ISP25xx type boards:
288 * 0x000000 -> 0x07ffff -- Boot code.
289 * 0x080000 -> 0x0fffff -- Firmware.
290 * 0x120000 -> 0x12ffff -- VPD and HBA parameters.
292 valid = 0;
293 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
294 valid = 1;
295 else if (start == (FA_BOOT_CODE_ADDR*4) ||
296 start == (FA_RISC_CODE_ADDR*4))
297 valid = 1;
298 else if (IS_QLA25XX(ha) && start == (FA_VPD_NVRAM_ADDR*4))
299 valid = 1;
300 if (!valid) {
301 qla_printk(KERN_WARNING, ha,
302 "Invalid start region 0x%x/0x%x.\n", start, size);
303 return -EINVAL;
306 ha->optrom_region_start = start;
307 ha->optrom_region_size = start + size > ha->optrom_size ?
308 ha->optrom_size - start : size;
310 ha->optrom_state = QLA_SWRITING;
311 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
312 if (ha->optrom_buffer == NULL) {
313 qla_printk(KERN_WARNING, ha,
314 "Unable to allocate memory for optrom update "
315 "(%x).\n", ha->optrom_region_size);
317 ha->optrom_state = QLA_SWAITING;
318 return count;
321 DEBUG2(qla_printk(KERN_INFO, ha,
322 "Staging flash region write -- 0x%x/0x%x.\n",
323 ha->optrom_region_start, ha->optrom_region_size));
325 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
326 break;
327 case 3:
328 if (ha->optrom_state != QLA_SWRITING)
329 break;
331 DEBUG2(qla_printk(KERN_INFO, ha,
332 "Writing flash region -- 0x%x/0x%x.\n",
333 ha->optrom_region_start, ha->optrom_region_size));
335 ha->isp_ops->write_optrom(ha, ha->optrom_buffer,
336 ha->optrom_region_start, ha->optrom_region_size);
337 break;
338 default:
339 count = -EINVAL;
341 return count;
344 static struct bin_attribute sysfs_optrom_ctl_attr = {
345 .attr = {
346 .name = "optrom_ctl",
347 .mode = S_IWUSR,
349 .size = 0,
350 .write = qla2x00_sysfs_write_optrom_ctl,
353 static ssize_t
354 qla2x00_sysfs_read_vpd(struct kobject *kobj,
355 struct bin_attribute *bin_attr,
356 char *buf, loff_t off, size_t count)
358 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
359 struct device, kobj)));
361 if (!capable(CAP_SYS_ADMIN))
362 return 0;
364 /* Read NVRAM data from cache. */
365 return memory_read_from_buffer(buf, count, &off, ha->vpd, ha->vpd_size);
368 static ssize_t
369 qla2x00_sysfs_write_vpd(struct kobject *kobj,
370 struct bin_attribute *bin_attr,
371 char *buf, loff_t off, size_t count)
373 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
374 struct device, kobj)));
376 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
377 return 0;
379 /* Write NVRAM. */
380 ha->isp_ops->write_nvram(ha, (uint8_t *)buf, ha->vpd_base, count);
381 ha->isp_ops->read_nvram(ha, (uint8_t *)ha->vpd, ha->vpd_base, count);
383 return count;
386 static struct bin_attribute sysfs_vpd_attr = {
387 .attr = {
388 .name = "vpd",
389 .mode = S_IRUSR | S_IWUSR,
391 .size = 0,
392 .read = qla2x00_sysfs_read_vpd,
393 .write = qla2x00_sysfs_write_vpd,
396 static ssize_t
397 qla2x00_sysfs_read_sfp(struct kobject *kobj,
398 struct bin_attribute *bin_attr,
399 char *buf, loff_t off, size_t count)
401 struct scsi_qla_host *ha = shost_priv(dev_to_shost(container_of(kobj,
402 struct device, kobj)));
403 uint16_t iter, addr, offset;
404 int rval;
406 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != SFP_DEV_SIZE * 2)
407 return 0;
409 if (ha->sfp_data)
410 goto do_read;
412 ha->sfp_data = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
413 &ha->sfp_data_dma);
414 if (!ha->sfp_data) {
415 qla_printk(KERN_WARNING, ha,
416 "Unable to allocate memory for SFP read-data.\n");
417 return 0;
420 do_read:
421 memset(ha->sfp_data, 0, SFP_BLOCK_SIZE);
422 addr = 0xa0;
423 for (iter = 0, offset = 0; iter < (SFP_DEV_SIZE * 2) / SFP_BLOCK_SIZE;
424 iter++, offset += SFP_BLOCK_SIZE) {
425 if (iter == 4) {
426 /* Skip to next device address. */
427 addr = 0xa2;
428 offset = 0;
431 rval = qla2x00_read_sfp(ha, ha->sfp_data_dma, addr, offset,
432 SFP_BLOCK_SIZE);
433 if (rval != QLA_SUCCESS) {
434 qla_printk(KERN_WARNING, ha,
435 "Unable to read SFP data (%x/%x/%x).\n", rval,
436 addr, offset);
437 count = 0;
438 break;
440 memcpy(buf, ha->sfp_data, SFP_BLOCK_SIZE);
441 buf += SFP_BLOCK_SIZE;
444 return count;
447 static struct bin_attribute sysfs_sfp_attr = {
448 .attr = {
449 .name = "sfp",
450 .mode = S_IRUSR | S_IWUSR,
452 .size = SFP_DEV_SIZE * 2,
453 .read = qla2x00_sysfs_read_sfp,
456 static struct sysfs_entry {
457 char *name;
458 struct bin_attribute *attr;
459 int is4GBp_only;
460 } bin_file_entries[] = {
461 { "fw_dump", &sysfs_fw_dump_attr, },
462 { "nvram", &sysfs_nvram_attr, },
463 { "optrom", &sysfs_optrom_attr, },
464 { "optrom_ctl", &sysfs_optrom_ctl_attr, },
465 { "vpd", &sysfs_vpd_attr, 1 },
466 { "sfp", &sysfs_sfp_attr, 1 },
467 { NULL },
470 void
471 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
473 struct Scsi_Host *host = ha->host;
474 struct sysfs_entry *iter;
475 int ret;
477 for (iter = bin_file_entries; iter->name; iter++) {
478 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
479 continue;
481 ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
482 iter->attr);
483 if (ret)
484 qla_printk(KERN_INFO, ha,
485 "Unable to create sysfs %s binary attribute "
486 "(%d).\n", iter->name, ret);
490 void
491 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
493 struct Scsi_Host *host = ha->host;
494 struct sysfs_entry *iter;
496 for (iter = bin_file_entries; iter->name; iter++) {
497 if (iter->is4GBp_only && !IS_FWI2_CAPABLE(ha))
498 continue;
500 sysfs_remove_bin_file(&host->shost_gendev.kobj,
501 iter->attr);
504 if (ha->beacon_blink_led == 1)
505 ha->isp_ops->beacon_off(ha);
508 /* Scsi_Host attributes. */
510 static ssize_t
511 qla2x00_drvr_version_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
514 return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
517 static ssize_t
518 qla2x00_fw_version_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
521 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
522 char fw_str[30];
524 return snprintf(buf, PAGE_SIZE, "%s\n",
525 ha->isp_ops->fw_version_str(ha, fw_str));
528 static ssize_t
529 qla2x00_serial_num_show(struct device *dev, struct device_attribute *attr,
530 char *buf)
532 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
533 uint32_t sn;
535 if (IS_FWI2_CAPABLE(ha)) {
536 qla2xxx_get_vpd_field(ha, "SN", buf, PAGE_SIZE);
537 return snprintf(buf, PAGE_SIZE, "%s\n", buf);
540 sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
541 return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
542 sn % 100000);
545 static ssize_t
546 qla2x00_isp_name_show(struct device *dev, struct device_attribute *attr,
547 char *buf)
549 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
550 return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
553 static ssize_t
554 qla2x00_isp_id_show(struct device *dev, struct device_attribute *attr,
555 char *buf)
557 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
558 return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
559 ha->product_id[0], ha->product_id[1], ha->product_id[2],
560 ha->product_id[3]);
563 static ssize_t
564 qla2x00_model_name_show(struct device *dev, struct device_attribute *attr,
565 char *buf)
567 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
568 return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
571 static ssize_t
572 qla2x00_model_desc_show(struct device *dev, struct device_attribute *attr,
573 char *buf)
575 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
576 return snprintf(buf, PAGE_SIZE, "%s\n",
577 ha->model_desc ? ha->model_desc: "");
580 static ssize_t
581 qla2x00_pci_info_show(struct device *dev, struct device_attribute *attr,
582 char *buf)
584 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
585 char pci_info[30];
587 return snprintf(buf, PAGE_SIZE, "%s\n",
588 ha->isp_ops->pci_info_str(ha, pci_info));
591 static ssize_t
592 qla2x00_link_state_show(struct device *dev, struct device_attribute *attr,
593 char *buf)
595 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
596 int len = 0;
598 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
599 atomic_read(&ha->loop_state) == LOOP_DEAD)
600 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
601 else if (atomic_read(&ha->loop_state) != LOOP_READY ||
602 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
603 test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
604 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
605 else {
606 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
608 switch (ha->current_topology) {
609 case ISP_CFG_NL:
610 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
611 break;
612 case ISP_CFG_FL:
613 len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
614 break;
615 case ISP_CFG_N:
616 len += snprintf(buf + len, PAGE_SIZE-len,
617 "N_Port to N_Port\n");
618 break;
619 case ISP_CFG_F:
620 len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
621 break;
622 default:
623 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
624 break;
627 return len;
630 static ssize_t
631 qla2x00_zio_show(struct device *dev, struct device_attribute *attr,
632 char *buf)
634 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
635 int len = 0;
637 switch (ha->zio_mode) {
638 case QLA_ZIO_MODE_6:
639 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
640 break;
641 case QLA_ZIO_DISABLED:
642 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
643 break;
645 return len;
648 static ssize_t
649 qla2x00_zio_store(struct device *dev, struct device_attribute *attr,
650 const char *buf, size_t count)
652 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
653 int val = 0;
654 uint16_t zio_mode;
656 if (!IS_ZIO_SUPPORTED(ha))
657 return -ENOTSUPP;
659 if (sscanf(buf, "%d", &val) != 1)
660 return -EINVAL;
662 if (val)
663 zio_mode = QLA_ZIO_MODE_6;
664 else
665 zio_mode = QLA_ZIO_DISABLED;
667 /* Update per-hba values and queue a reset. */
668 if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
669 ha->zio_mode = zio_mode;
670 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
672 return strlen(buf);
675 static ssize_t
676 qla2x00_zio_timer_show(struct device *dev, struct device_attribute *attr,
677 char *buf)
679 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
681 return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
684 static ssize_t
685 qla2x00_zio_timer_store(struct device *dev, struct device_attribute *attr,
686 const char *buf, size_t count)
688 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
689 int val = 0;
690 uint16_t zio_timer;
692 if (sscanf(buf, "%d", &val) != 1)
693 return -EINVAL;
694 if (val > 25500 || val < 100)
695 return -ERANGE;
697 zio_timer = (uint16_t)(val / 100);
698 ha->zio_timer = zio_timer;
700 return strlen(buf);
703 static ssize_t
704 qla2x00_beacon_show(struct device *dev, struct device_attribute *attr,
705 char *buf)
707 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
708 int len = 0;
710 if (ha->beacon_blink_led)
711 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
712 else
713 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
714 return len;
717 static ssize_t
718 qla2x00_beacon_store(struct device *dev, struct device_attribute *attr,
719 const char *buf, size_t count)
721 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
722 int val = 0;
723 int rval;
725 if (IS_QLA2100(ha) || IS_QLA2200(ha))
726 return -EPERM;
728 if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
729 qla_printk(KERN_WARNING, ha,
730 "Abort ISP active -- ignoring beacon request.\n");
731 return -EBUSY;
734 if (sscanf(buf, "%d", &val) != 1)
735 return -EINVAL;
737 if (val)
738 rval = ha->isp_ops->beacon_on(ha);
739 else
740 rval = ha->isp_ops->beacon_off(ha);
742 if (rval != QLA_SUCCESS)
743 count = 0;
745 return count;
748 static ssize_t
749 qla2x00_optrom_bios_version_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
752 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
754 return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->bios_revision[1],
755 ha->bios_revision[0]);
758 static ssize_t
759 qla2x00_optrom_efi_version_show(struct device *dev,
760 struct device_attribute *attr, char *buf)
762 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
764 return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->efi_revision[1],
765 ha->efi_revision[0]);
768 static ssize_t
769 qla2x00_optrom_fcode_version_show(struct device *dev,
770 struct device_attribute *attr, char *buf)
772 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
774 return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fcode_revision[1],
775 ha->fcode_revision[0]);
778 static ssize_t
779 qla2x00_optrom_fw_version_show(struct device *dev,
780 struct device_attribute *attr, char *buf)
782 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
784 return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d %d\n",
785 ha->fw_revision[0], ha->fw_revision[1], ha->fw_revision[2],
786 ha->fw_revision[3]);
789 static ssize_t
790 qla2x00_total_isp_aborts_show(struct device *dev,
791 struct device_attribute *attr, char *buf)
793 scsi_qla_host_t *ha = shost_priv(class_to_shost(dev));
795 return snprintf(buf, PAGE_SIZE, "%d\n",
796 ha->qla_stats.total_isp_aborts);
799 static DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show, NULL);
800 static DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
801 static DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
802 static DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
803 static DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
804 static DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
805 static DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
806 static DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
807 static DEVICE_ATTR(link_state, S_IRUGO, qla2x00_link_state_show, NULL);
808 static DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show, qla2x00_zio_store);
809 static DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
810 qla2x00_zio_timer_store);
811 static DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
812 qla2x00_beacon_store);
813 static DEVICE_ATTR(optrom_bios_version, S_IRUGO,
814 qla2x00_optrom_bios_version_show, NULL);
815 static DEVICE_ATTR(optrom_efi_version, S_IRUGO,
816 qla2x00_optrom_efi_version_show, NULL);
817 static DEVICE_ATTR(optrom_fcode_version, S_IRUGO,
818 qla2x00_optrom_fcode_version_show, NULL);
819 static DEVICE_ATTR(optrom_fw_version, S_IRUGO, qla2x00_optrom_fw_version_show,
820 NULL);
821 static DEVICE_ATTR(total_isp_aborts, S_IRUGO, qla2x00_total_isp_aborts_show,
822 NULL);
824 struct device_attribute *qla2x00_host_attrs[] = {
825 &dev_attr_driver_version,
826 &dev_attr_fw_version,
827 &dev_attr_serial_num,
828 &dev_attr_isp_name,
829 &dev_attr_isp_id,
830 &dev_attr_model_name,
831 &dev_attr_model_desc,
832 &dev_attr_pci_info,
833 &dev_attr_link_state,
834 &dev_attr_zio,
835 &dev_attr_zio_timer,
836 &dev_attr_beacon,
837 &dev_attr_optrom_bios_version,
838 &dev_attr_optrom_efi_version,
839 &dev_attr_optrom_fcode_version,
840 &dev_attr_optrom_fw_version,
841 &dev_attr_total_isp_aborts,
842 NULL,
845 /* Host attributes. */
847 static void
848 qla2x00_get_host_port_id(struct Scsi_Host *shost)
850 scsi_qla_host_t *ha = shost_priv(shost);
852 fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
853 ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
856 static void
857 qla2x00_get_host_speed(struct Scsi_Host *shost)
859 scsi_qla_host_t *ha = to_qla_parent(shost_priv(shost));
860 u32 speed = FC_PORTSPEED_UNKNOWN;
862 switch (ha->link_data_rate) {
863 case PORT_SPEED_1GB:
864 speed = FC_PORTSPEED_1GBIT;
865 break;
866 case PORT_SPEED_2GB:
867 speed = FC_PORTSPEED_2GBIT;
868 break;
869 case PORT_SPEED_4GB:
870 speed = FC_PORTSPEED_4GBIT;
871 break;
872 case PORT_SPEED_8GB:
873 speed = FC_PORTSPEED_8GBIT;
874 break;
876 fc_host_speed(shost) = speed;
879 static void
880 qla2x00_get_host_port_type(struct Scsi_Host *shost)
882 scsi_qla_host_t *ha = shost_priv(shost);
883 uint32_t port_type = FC_PORTTYPE_UNKNOWN;
885 if (ha->parent) {
886 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
887 return;
889 switch (ha->current_topology) {
890 case ISP_CFG_NL:
891 port_type = FC_PORTTYPE_LPORT;
892 break;
893 case ISP_CFG_FL:
894 port_type = FC_PORTTYPE_NLPORT;
895 break;
896 case ISP_CFG_N:
897 port_type = FC_PORTTYPE_PTP;
898 break;
899 case ISP_CFG_F:
900 port_type = FC_PORTTYPE_NPORT;
901 break;
903 fc_host_port_type(shost) = port_type;
906 static void
907 qla2x00_get_starget_node_name(struct scsi_target *starget)
909 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
910 scsi_qla_host_t *ha = shost_priv(host);
911 fc_port_t *fcport;
912 u64 node_name = 0;
914 list_for_each_entry(fcport, &ha->fcports, list) {
915 if (fcport->rport &&
916 starget->id == fcport->rport->scsi_target_id) {
917 node_name = wwn_to_u64(fcport->node_name);
918 break;
922 fc_starget_node_name(starget) = node_name;
925 static void
926 qla2x00_get_starget_port_name(struct scsi_target *starget)
928 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
929 scsi_qla_host_t *ha = shost_priv(host);
930 fc_port_t *fcport;
931 u64 port_name = 0;
933 list_for_each_entry(fcport, &ha->fcports, list) {
934 if (fcport->rport &&
935 starget->id == fcport->rport->scsi_target_id) {
936 port_name = wwn_to_u64(fcport->port_name);
937 break;
941 fc_starget_port_name(starget) = port_name;
944 static void
945 qla2x00_get_starget_port_id(struct scsi_target *starget)
947 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
948 scsi_qla_host_t *ha = shost_priv(host);
949 fc_port_t *fcport;
950 uint32_t port_id = ~0U;
952 list_for_each_entry(fcport, &ha->fcports, list) {
953 if (fcport->rport &&
954 starget->id == fcport->rport->scsi_target_id) {
955 port_id = fcport->d_id.b.domain << 16 |
956 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
957 break;
961 fc_starget_port_id(starget) = port_id;
964 static void
965 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
967 if (timeout)
968 rport->dev_loss_tmo = timeout;
969 else
970 rport->dev_loss_tmo = 1;
973 static void
974 qla2x00_dev_loss_tmo_callbk(struct fc_rport *rport)
976 struct Scsi_Host *host = rport_to_shost(rport);
977 fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
979 qla2x00_abort_fcport_cmds(fcport);
982 * Transport has effectively 'deleted' the rport, clear
983 * all local references.
985 spin_lock_irq(host->host_lock);
986 fcport->rport = NULL;
987 *((fc_port_t **)rport->dd_data) = NULL;
988 spin_unlock_irq(host->host_lock);
991 static void
992 qla2x00_terminate_rport_io(struct fc_rport *rport)
994 fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
997 * At this point all fcport's software-states are cleared. Perform any
998 * final cleanup of firmware resources (PCBs and XCBs).
1000 if (fcport->loop_id != FC_NO_LOOP_ID) {
1001 fcport->ha->isp_ops->fabric_logout(fcport->ha, fcport->loop_id,
1002 fcport->d_id.b.domain, fcport->d_id.b.area,
1003 fcport->d_id.b.al_pa);
1004 fcport->loop_id = FC_NO_LOOP_ID;
1007 qla2x00_abort_fcport_cmds(fcport);
1008 scsi_target_unblock(&rport->dev);
1011 static int
1012 qla2x00_issue_lip(struct Scsi_Host *shost)
1014 scsi_qla_host_t *ha = shost_priv(shost);
1016 qla2x00_loop_reset(ha);
1017 return 0;
1020 static struct fc_host_statistics *
1021 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
1023 scsi_qla_host_t *ha = to_qla_parent(shost_priv(shost));
1024 int rval;
1025 struct link_statistics *stats;
1026 dma_addr_t stats_dma;
1027 struct fc_host_statistics *pfc_host_stat;
1029 pfc_host_stat = &ha->fc_host_stat;
1030 memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
1032 stats = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &stats_dma);
1033 if (stats == NULL) {
1034 DEBUG2_3_11(printk("%s(%ld): Failed to allocate memory.\n",
1035 __func__, ha->host_no));
1036 goto done;
1038 memset(stats, 0, DMA_POOL_SIZE);
1040 rval = QLA_FUNCTION_FAILED;
1041 if (IS_FWI2_CAPABLE(ha)) {
1042 rval = qla24xx_get_isp_stats(ha, stats, stats_dma);
1043 } else if (atomic_read(&ha->loop_state) == LOOP_READY &&
1044 !test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) &&
1045 !test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) &&
1046 !ha->dpc_active) {
1047 /* Must be in a 'READY' state for statistics retrieval. */
1048 rval = qla2x00_get_link_status(ha, ha->loop_id, stats,
1049 stats_dma);
1052 if (rval != QLA_SUCCESS)
1053 goto done_free;
1055 pfc_host_stat->link_failure_count = stats->link_fail_cnt;
1056 pfc_host_stat->loss_of_sync_count = stats->loss_sync_cnt;
1057 pfc_host_stat->loss_of_signal_count = stats->loss_sig_cnt;
1058 pfc_host_stat->prim_seq_protocol_err_count = stats->prim_seq_err_cnt;
1059 pfc_host_stat->invalid_tx_word_count = stats->inval_xmit_word_cnt;
1060 pfc_host_stat->invalid_crc_count = stats->inval_crc_cnt;
1061 if (IS_FWI2_CAPABLE(ha)) {
1062 pfc_host_stat->lip_count = stats->lip_cnt;
1063 pfc_host_stat->tx_frames = stats->tx_frames;
1064 pfc_host_stat->rx_frames = stats->rx_frames;
1065 pfc_host_stat->dumped_frames = stats->dumped_frames;
1066 pfc_host_stat->nos_count = stats->nos_rcvd;
1069 done_free:
1070 dma_pool_free(ha->s_dma_pool, stats, stats_dma);
1071 done:
1072 return pfc_host_stat;
1075 static void
1076 qla2x00_get_host_symbolic_name(struct Scsi_Host *shost)
1078 scsi_qla_host_t *ha = shost_priv(shost);
1080 qla2x00_get_sym_node_name(ha, fc_host_symbolic_name(shost));
1083 static void
1084 qla2x00_set_host_system_hostname(struct Scsi_Host *shost)
1086 scsi_qla_host_t *ha = shost_priv(shost);
1088 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
1091 static void
1092 qla2x00_get_host_fabric_name(struct Scsi_Host *shost)
1094 scsi_qla_host_t *ha = shost_priv(shost);
1095 u64 node_name;
1097 if (ha->device_flags & SWITCH_FOUND)
1098 node_name = wwn_to_u64(ha->fabric_node_name);
1099 else
1100 node_name = wwn_to_u64(ha->node_name);
1102 fc_host_fabric_name(shost) = node_name;
1105 static void
1106 qla2x00_get_host_port_state(struct Scsi_Host *shost)
1108 scsi_qla_host_t *ha = to_qla_parent(shost_priv(shost));
1110 if (!ha->flags.online)
1111 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
1112 else if (atomic_read(&ha->loop_state) == LOOP_TIMEOUT)
1113 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
1114 else
1115 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
1118 static int
1119 qla24xx_vport_create(struct fc_vport *fc_vport, bool disable)
1121 int ret = 0;
1122 scsi_qla_host_t *ha = shost_priv(fc_vport->shost);
1123 scsi_qla_host_t *vha;
1125 ret = qla24xx_vport_create_req_sanity_check(fc_vport);
1126 if (ret) {
1127 DEBUG15(printk("qla24xx_vport_create_req_sanity_check failed, "
1128 "status %x\n", ret));
1129 return (ret);
1132 vha = qla24xx_create_vhost(fc_vport);
1133 if (vha == NULL) {
1134 DEBUG15(printk ("qla24xx_create_vhost failed, vha = %p\n",
1135 vha));
1136 return FC_VPORT_FAILED;
1138 if (disable) {
1139 atomic_set(&vha->vp_state, VP_OFFLINE);
1140 fc_vport_set_state(fc_vport, FC_VPORT_DISABLED);
1141 } else
1142 atomic_set(&vha->vp_state, VP_FAILED);
1144 /* ready to create vport */
1145 qla_printk(KERN_INFO, vha, "VP entry id %d assigned.\n", vha->vp_idx);
1147 /* initialized vport states */
1148 atomic_set(&vha->loop_state, LOOP_DOWN);
1149 vha->vp_err_state= VP_ERR_PORTDWN;
1150 vha->vp_prev_err_state= VP_ERR_UNKWN;
1151 /* Check if physical ha port is Up */
1152 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
1153 atomic_read(&ha->loop_state) == LOOP_DEAD) {
1154 /* Don't retry or attempt login of this virtual port */
1155 DEBUG15(printk ("scsi(%ld): pport loop_state is not UP.\n",
1156 vha->host_no));
1157 atomic_set(&vha->loop_state, LOOP_DEAD);
1158 if (!disable)
1159 fc_vport_set_state(fc_vport, FC_VPORT_LINKDOWN);
1162 if (scsi_add_host(vha->host, &fc_vport->dev)) {
1163 DEBUG15(printk("scsi(%ld): scsi_add_host failure for VP[%d].\n",
1164 vha->host_no, vha->vp_idx));
1165 goto vport_create_failed_2;
1168 /* initialize attributes */
1169 fc_host_node_name(vha->host) = wwn_to_u64(vha->node_name);
1170 fc_host_port_name(vha->host) = wwn_to_u64(vha->port_name);
1171 fc_host_supported_classes(vha->host) =
1172 fc_host_supported_classes(ha->host);
1173 fc_host_supported_speeds(vha->host) =
1174 fc_host_supported_speeds(ha->host);
1176 qla24xx_vport_disable(fc_vport, disable);
1178 return 0;
1179 vport_create_failed_2:
1180 qla24xx_disable_vp(vha);
1181 qla24xx_deallocate_vp_id(vha);
1182 kfree(vha->port_name);
1183 kfree(vha->node_name);
1184 scsi_host_put(vha->host);
1185 return FC_VPORT_FAILED;
1188 static int
1189 qla24xx_vport_delete(struct fc_vport *fc_vport)
1191 scsi_qla_host_t *vha = fc_vport->dd_data;
1192 scsi_qla_host_t *pha = to_qla_parent(vha);
1194 while (test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags) ||
1195 test_bit(FCPORT_UPDATE_NEEDED, &pha->dpc_flags))
1196 msleep(1000);
1198 qla24xx_disable_vp(vha);
1199 qla24xx_deallocate_vp_id(vha);
1201 kfree(vha->node_name);
1202 kfree(vha->port_name);
1204 if (vha->timer_active) {
1205 qla2x00_vp_stop_timer(vha);
1206 DEBUG15(printk ("scsi(%ld): timer for the vport[%d] = %p "
1207 "has stopped\n",
1208 vha->host_no, vha->vp_idx, vha));
1211 fc_remove_host(vha->host);
1213 scsi_remove_host(vha->host);
1215 scsi_host_put(vha->host);
1217 return 0;
1220 static int
1221 qla24xx_vport_disable(struct fc_vport *fc_vport, bool disable)
1223 scsi_qla_host_t *vha = fc_vport->dd_data;
1225 if (disable)
1226 qla24xx_disable_vp(vha);
1227 else
1228 qla24xx_enable_vp(vha);
1230 return 0;
1233 struct fc_function_template qla2xxx_transport_functions = {
1235 .show_host_node_name = 1,
1236 .show_host_port_name = 1,
1237 .show_host_supported_classes = 1,
1238 .show_host_supported_speeds = 1,
1240 .get_host_port_id = qla2x00_get_host_port_id,
1241 .show_host_port_id = 1,
1242 .get_host_speed = qla2x00_get_host_speed,
1243 .show_host_speed = 1,
1244 .get_host_port_type = qla2x00_get_host_port_type,
1245 .show_host_port_type = 1,
1246 .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1247 .show_host_symbolic_name = 1,
1248 .set_host_system_hostname = qla2x00_set_host_system_hostname,
1249 .show_host_system_hostname = 1,
1250 .get_host_fabric_name = qla2x00_get_host_fabric_name,
1251 .show_host_fabric_name = 1,
1252 .get_host_port_state = qla2x00_get_host_port_state,
1253 .show_host_port_state = 1,
1255 .dd_fcrport_size = sizeof(struct fc_port *),
1256 .show_rport_supported_classes = 1,
1258 .get_starget_node_name = qla2x00_get_starget_node_name,
1259 .show_starget_node_name = 1,
1260 .get_starget_port_name = qla2x00_get_starget_port_name,
1261 .show_starget_port_name = 1,
1262 .get_starget_port_id = qla2x00_get_starget_port_id,
1263 .show_starget_port_id = 1,
1265 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1266 .show_rport_dev_loss_tmo = 1,
1268 .issue_fc_host_lip = qla2x00_issue_lip,
1269 .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
1270 .terminate_rport_io = qla2x00_terminate_rport_io,
1271 .get_fc_host_stats = qla2x00_get_fc_host_stats,
1273 .vport_create = qla24xx_vport_create,
1274 .vport_disable = qla24xx_vport_disable,
1275 .vport_delete = qla24xx_vport_delete,
1278 struct fc_function_template qla2xxx_transport_vport_functions = {
1280 .show_host_node_name = 1,
1281 .show_host_port_name = 1,
1282 .show_host_supported_classes = 1,
1284 .get_host_port_id = qla2x00_get_host_port_id,
1285 .show_host_port_id = 1,
1286 .get_host_speed = qla2x00_get_host_speed,
1287 .show_host_speed = 1,
1288 .get_host_port_type = qla2x00_get_host_port_type,
1289 .show_host_port_type = 1,
1290 .get_host_symbolic_name = qla2x00_get_host_symbolic_name,
1291 .show_host_symbolic_name = 1,
1292 .set_host_system_hostname = qla2x00_set_host_system_hostname,
1293 .show_host_system_hostname = 1,
1294 .get_host_fabric_name = qla2x00_get_host_fabric_name,
1295 .show_host_fabric_name = 1,
1296 .get_host_port_state = qla2x00_get_host_port_state,
1297 .show_host_port_state = 1,
1299 .dd_fcrport_size = sizeof(struct fc_port *),
1300 .show_rport_supported_classes = 1,
1302 .get_starget_node_name = qla2x00_get_starget_node_name,
1303 .show_starget_node_name = 1,
1304 .get_starget_port_name = qla2x00_get_starget_port_name,
1305 .show_starget_port_name = 1,
1306 .get_starget_port_id = qla2x00_get_starget_port_id,
1307 .show_starget_port_id = 1,
1309 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
1310 .show_rport_dev_loss_tmo = 1,
1312 .issue_fc_host_lip = qla2x00_issue_lip,
1313 .dev_loss_tmo_callbk = qla2x00_dev_loss_tmo_callbk,
1314 .terminate_rport_io = qla2x00_terminate_rport_io,
1315 .get_fc_host_stats = qla2x00_get_fc_host_stats,
1318 void
1319 qla2x00_init_host_attr(scsi_qla_host_t *ha)
1321 u32 speed = FC_PORTSPEED_UNKNOWN;
1323 fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
1324 fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
1325 fc_host_supported_classes(ha->host) = FC_COS_CLASS3;
1326 fc_host_max_npiv_vports(ha->host) = ha->max_npiv_vports;;
1327 fc_host_npiv_vports_inuse(ha->host) = ha->cur_vport_count;
1329 if (IS_QLA25XX(ha))
1330 speed = FC_PORTSPEED_8GBIT | FC_PORTSPEED_4GBIT |
1331 FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1332 else if (IS_QLA24XX_TYPE(ha))
1333 speed = FC_PORTSPEED_4GBIT | FC_PORTSPEED_2GBIT |
1334 FC_PORTSPEED_1GBIT;
1335 else if (IS_QLA23XX(ha))
1336 speed = FC_PORTSPEED_2GBIT | FC_PORTSPEED_1GBIT;
1337 else
1338 speed = FC_PORTSPEED_1GBIT;
1339 fc_host_supported_speeds(ha->host) = speed;