2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
9 #include <linux/vmalloc.h>
11 /* SYSFS attributes --------------------------------------------------------- */
14 qla2x00_sysfs_read_fw_dump(struct kobject *kobj, char *buf, loff_t off,
17 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
18 struct device, kobj)));
19 char *rbuf = (char *)ha->fw_dump;
21 if (ha->fw_dump_reading == 0)
23 if (off > ha->fw_dump_len)
25 if (off + count > ha->fw_dump_len)
26 count = ha->fw_dump_len - off;
28 memcpy(buf, &rbuf[off], count);
34 qla2x00_sysfs_write_fw_dump(struct kobject *kobj, char *buf, loff_t off,
37 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
38 struct device, kobj)));
44 reading = simple_strtol(buf, NULL, 10);
47 if (!ha->fw_dump_reading)
50 qla_printk(KERN_INFO, ha,
51 "Firmware dump cleared on (%ld).\n", ha->host_no);
53 ha->fw_dump_reading = 0;
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",
66 qla2x00_alloc_fw_dump(ha);
72 static struct bin_attribute sysfs_fw_dump_attr = {
75 .mode = S_IRUSR | S_IWUSR,
79 .read = qla2x00_sysfs_read_fw_dump,
80 .write = qla2x00_sysfs_write_fw_dump,
84 qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
87 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
88 struct device, kobj)));
91 if (!capable(CAP_SYS_ADMIN) || off != 0)
95 spin_lock_irqsave(&ha->hardware_lock, flags);
96 ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->nvram_base,
98 spin_unlock_irqrestore(&ha->hardware_lock, flags);
100 return ha->nvram_size;
104 qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
107 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
108 struct device, kobj)));
112 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
115 /* Checksum NVRAM. */
116 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
120 iter = (uint32_t *)buf;
122 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
123 chksum += le32_to_cpu(*iter++);
124 chksum = ~chksum + 1;
125 *iter = cpu_to_le32(chksum);
130 iter = (uint8_t *)buf;
132 for (cnt = 0; cnt < count - 1; cnt++)
134 chksum = ~chksum + 1;
139 spin_lock_irqsave(&ha->hardware_lock, flags);
140 ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
141 spin_unlock_irqrestore(&ha->hardware_lock, flags);
146 static struct bin_attribute sysfs_nvram_attr = {
149 .mode = S_IRUSR | S_IWUSR,
150 .owner = THIS_MODULE,
153 .read = qla2x00_sysfs_read_nvram,
154 .write = qla2x00_sysfs_write_nvram,
158 qla2x00_sysfs_read_optrom(struct kobject *kobj, char *buf, loff_t off,
161 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
162 struct device, kobj)));
164 if (ha->optrom_state != QLA_SREADING)
166 if (off > ha->optrom_size)
168 if (off + count > ha->optrom_size)
169 count = ha->optrom_size - off;
171 memcpy(buf, &ha->optrom_buffer[off], count);
177 qla2x00_sysfs_write_optrom(struct kobject *kobj, char *buf, loff_t off,
180 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
181 struct device, kobj)));
183 if (ha->optrom_state != QLA_SWRITING)
185 if (off > ha->optrom_size)
187 if (off + count > ha->optrom_size)
188 count = ha->optrom_size - off;
190 memcpy(&ha->optrom_buffer[off], buf, count);
195 static struct bin_attribute sysfs_optrom_attr = {
198 .mode = S_IRUSR | S_IWUSR,
199 .owner = THIS_MODULE,
201 .size = OPTROM_SIZE_24XX,
202 .read = qla2x00_sysfs_read_optrom,
203 .write = qla2x00_sysfs_write_optrom,
207 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj, char *buf, loff_t off,
210 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
211 struct device, kobj)));
217 if (sscanf(buf, "%d", &val) != 1)
222 if (ha->optrom_state != QLA_SREADING &&
223 ha->optrom_state != QLA_SWRITING)
226 ha->optrom_state = QLA_SWAITING;
227 vfree(ha->optrom_buffer);
228 ha->optrom_buffer = NULL;
231 if (ha->optrom_state != QLA_SWAITING)
234 ha->optrom_state = QLA_SREADING;
235 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
236 if (ha->optrom_buffer == NULL) {
237 qla_printk(KERN_WARNING, ha,
238 "Unable to allocate memory for optrom retrieval "
239 "(%x).\n", ha->optrom_size);
241 ha->optrom_state = QLA_SWAITING;
245 memset(ha->optrom_buffer, 0, ha->optrom_size);
246 ha->isp_ops.read_optrom(ha, ha->optrom_buffer, 0,
250 if (ha->optrom_state != QLA_SWAITING)
253 ha->optrom_state = QLA_SWRITING;
254 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
255 if (ha->optrom_buffer == NULL) {
256 qla_printk(KERN_WARNING, ha,
257 "Unable to allocate memory for optrom update "
258 "(%x).\n", ha->optrom_size);
260 ha->optrom_state = QLA_SWAITING;
263 memset(ha->optrom_buffer, 0, ha->optrom_size);
266 if (ha->optrom_state != QLA_SWRITING)
269 ha->isp_ops.write_optrom(ha, ha->optrom_buffer, 0,
276 static struct bin_attribute sysfs_optrom_ctl_attr = {
278 .name = "optrom_ctl",
280 .owner = THIS_MODULE,
283 .write = qla2x00_sysfs_write_optrom_ctl,
287 qla2x00_sysfs_read_vpd(struct kobject *kobj, char *buf, loff_t off,
290 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
291 struct device, kobj)));
294 if (!capable(CAP_SYS_ADMIN) || off != 0)
297 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
301 spin_lock_irqsave(&ha->hardware_lock, flags);
302 ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->vpd_base, ha->vpd_size);
303 spin_unlock_irqrestore(&ha->hardware_lock, flags);
309 qla2x00_sysfs_write_vpd(struct kobject *kobj, char *buf, loff_t off,
312 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
313 struct device, kobj)));
316 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->vpd_size)
319 if (!IS_QLA24XX(ha) && !IS_QLA54XX(ha))
323 spin_lock_irqsave(&ha->hardware_lock, flags);
324 ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->vpd_base, count);
325 spin_unlock_irqrestore(&ha->hardware_lock, flags);
330 static struct bin_attribute sysfs_vpd_attr = {
333 .mode = S_IRUSR | S_IWUSR,
334 .owner = THIS_MODULE,
337 .read = qla2x00_sysfs_read_vpd,
338 .write = qla2x00_sysfs_write_vpd,
342 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
344 struct Scsi_Host *host = ha->host;
346 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
347 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
348 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
349 sysfs_create_bin_file(&host->shost_gendev.kobj,
350 &sysfs_optrom_ctl_attr);
351 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_vpd_attr);
355 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
357 struct Scsi_Host *host = ha->host;
359 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
360 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
361 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
362 sysfs_remove_bin_file(&host->shost_gendev.kobj,
363 &sysfs_optrom_ctl_attr);
364 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_vpd_attr);
366 if (ha->beacon_blink_led == 1)
367 ha->isp_ops.beacon_off(ha);
370 /* Scsi_Host attributes. */
373 qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
375 return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
379 qla2x00_fw_version_show(struct class_device *cdev, char *buf)
381 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
384 return snprintf(buf, PAGE_SIZE, "%s\n",
385 ha->isp_ops.fw_version_str(ha, fw_str));
389 qla2x00_serial_num_show(struct class_device *cdev, char *buf)
391 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
394 sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
395 return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
400 qla2x00_isp_name_show(struct class_device *cdev, char *buf)
402 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
403 return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
407 qla2x00_isp_id_show(struct class_device *cdev, char *buf)
409 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
410 return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
411 ha->product_id[0], ha->product_id[1], ha->product_id[2],
416 qla2x00_model_name_show(struct class_device *cdev, char *buf)
418 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
419 return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
423 qla2x00_model_desc_show(struct class_device *cdev, char *buf)
425 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
426 return snprintf(buf, PAGE_SIZE, "%s\n",
427 ha->model_desc ? ha->model_desc: "");
431 qla2x00_pci_info_show(struct class_device *cdev, char *buf)
433 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
436 return snprintf(buf, PAGE_SIZE, "%s\n",
437 ha->isp_ops.pci_info_str(ha, pci_info));
441 qla2x00_state_show(struct class_device *cdev, char *buf)
443 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
446 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
447 atomic_read(&ha->loop_state) == LOOP_DEAD)
448 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
449 else if (atomic_read(&ha->loop_state) != LOOP_READY ||
450 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
451 test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
452 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
454 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
456 switch (ha->current_topology) {
458 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
461 len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
464 len += snprintf(buf + len, PAGE_SIZE-len,
465 "N_Port to N_Port\n");
468 len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
471 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
479 qla2x00_zio_show(struct class_device *cdev, char *buf)
481 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
484 switch (ha->zio_mode) {
486 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
488 case QLA_ZIO_DISABLED:
489 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
496 qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
498 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
502 if (!IS_ZIO_SUPPORTED(ha))
505 if (sscanf(buf, "%d", &val) != 1)
509 zio_mode = QLA_ZIO_MODE_6;
511 zio_mode = QLA_ZIO_DISABLED;
513 /* Update per-hba values and queue a reset. */
514 if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
515 ha->zio_mode = zio_mode;
516 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
522 qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
524 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
526 return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
530 qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
533 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
537 if (sscanf(buf, "%d", &val) != 1)
539 if (val > 25500 || val < 100)
542 zio_timer = (uint16_t)(val / 100);
543 ha->zio_timer = zio_timer;
549 qla2x00_beacon_show(struct class_device *cdev, char *buf)
551 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
554 if (ha->beacon_blink_led)
555 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
557 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
562 qla2x00_beacon_store(struct class_device *cdev, const char *buf,
565 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
569 if (IS_QLA2100(ha) || IS_QLA2200(ha))
572 if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
573 qla_printk(KERN_WARNING, ha,
574 "Abort ISP active -- ignoring beacon request.\n");
578 if (sscanf(buf, "%d", &val) != 1)
582 rval = ha->isp_ops.beacon_on(ha);
584 rval = ha->isp_ops.beacon_off(ha);
586 if (rval != QLA_SUCCESS)
592 static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
594 static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
595 static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
596 static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
597 static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
598 static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
599 static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
600 static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
601 static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
602 static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
604 static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
605 qla2x00_zio_timer_store);
606 static CLASS_DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
607 qla2x00_beacon_store);
609 struct class_device_attribute *qla2x00_host_attrs[] = {
610 &class_device_attr_driver_version,
611 &class_device_attr_fw_version,
612 &class_device_attr_serial_num,
613 &class_device_attr_isp_name,
614 &class_device_attr_isp_id,
615 &class_device_attr_model_name,
616 &class_device_attr_model_desc,
617 &class_device_attr_pci_info,
618 &class_device_attr_state,
619 &class_device_attr_zio,
620 &class_device_attr_zio_timer,
621 &class_device_attr_beacon,
625 /* Host attributes. */
628 qla2x00_get_host_port_id(struct Scsi_Host *shost)
630 scsi_qla_host_t *ha = to_qla_host(shost);
632 fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
633 ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
637 qla2x00_get_host_speed(struct Scsi_Host *shost)
639 scsi_qla_host_t *ha = to_qla_host(shost);
642 switch (ha->link_data_rate) {
653 fc_host_speed(shost) = speed;
657 qla2x00_get_host_port_type(struct Scsi_Host *shost)
659 scsi_qla_host_t *ha = to_qla_host(shost);
660 uint32_t port_type = FC_PORTTYPE_UNKNOWN;
662 switch (ha->current_topology) {
664 port_type = FC_PORTTYPE_LPORT;
667 port_type = FC_PORTTYPE_NLPORT;
670 port_type = FC_PORTTYPE_PTP;
673 port_type = FC_PORTTYPE_NPORT;
676 fc_host_port_type(shost) = port_type;
680 qla2x00_get_starget_node_name(struct scsi_target *starget)
682 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
683 scsi_qla_host_t *ha = to_qla_host(host);
687 list_for_each_entry(fcport, &ha->fcports, list) {
688 if (starget->id == fcport->os_target_id) {
689 node_name = wwn_to_u64(fcport->node_name);
694 fc_starget_node_name(starget) = node_name;
698 qla2x00_get_starget_port_name(struct scsi_target *starget)
700 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
701 scsi_qla_host_t *ha = to_qla_host(host);
705 list_for_each_entry(fcport, &ha->fcports, list) {
706 if (starget->id == fcport->os_target_id) {
707 port_name = wwn_to_u64(fcport->port_name);
712 fc_starget_port_name(starget) = port_name;
716 qla2x00_get_starget_port_id(struct scsi_target *starget)
718 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
719 scsi_qla_host_t *ha = to_qla_host(host);
721 uint32_t port_id = ~0U;
723 list_for_each_entry(fcport, &ha->fcports, list) {
724 if (starget->id == fcport->os_target_id) {
725 port_id = fcport->d_id.b.domain << 16 |
726 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
731 fc_starget_port_id(starget) = port_id;
735 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
737 struct Scsi_Host *host = rport_to_shost(rport);
738 scsi_qla_host_t *ha = to_qla_host(host);
740 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
744 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
746 struct Scsi_Host *host = rport_to_shost(rport);
747 scsi_qla_host_t *ha = to_qla_host(host);
750 ha->port_down_retry_count = timeout;
752 ha->port_down_retry_count = 1;
754 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
758 qla2x00_issue_lip(struct Scsi_Host *shost)
760 scsi_qla_host_t *ha = to_qla_host(shost);
762 set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
766 static struct fc_host_statistics *
767 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
769 scsi_qla_host_t *ha = to_qla_host(shost);
772 link_stat_t stat_buf;
773 struct fc_host_statistics *pfc_host_stat;
775 pfc_host_stat = &ha->fc_host_stat;
776 memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
778 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
779 rval = qla24xx_get_isp_stats(ha, (uint32_t *)&stat_buf,
780 sizeof(stat_buf) / 4, mb_stat);
782 rval = qla2x00_get_link_status(ha, ha->loop_id, &stat_buf,
786 qla_printk(KERN_WARNING, ha,
787 "Unable to retrieve host statistics (%d).\n", mb_stat[0]);
788 return pfc_host_stat;
791 pfc_host_stat->link_failure_count = stat_buf.link_fail_cnt;
792 pfc_host_stat->loss_of_sync_count = stat_buf.loss_sync_cnt;
793 pfc_host_stat->loss_of_signal_count = stat_buf.loss_sig_cnt;
794 pfc_host_stat->prim_seq_protocol_err_count = stat_buf.prim_seq_err_cnt;
795 pfc_host_stat->invalid_tx_word_count = stat_buf.inval_xmit_word_cnt;
796 pfc_host_stat->invalid_crc_count = stat_buf.inval_crc_cnt;
798 return pfc_host_stat;
801 struct fc_function_template qla2xxx_transport_functions = {
803 .show_host_node_name = 1,
804 .show_host_port_name = 1,
805 .show_host_supported_classes = 1,
807 .get_host_port_id = qla2x00_get_host_port_id,
808 .show_host_port_id = 1,
809 .get_host_speed = qla2x00_get_host_speed,
810 .show_host_speed = 1,
811 .get_host_port_type = qla2x00_get_host_port_type,
812 .show_host_port_type = 1,
814 .dd_fcrport_size = sizeof(struct fc_port *),
815 .show_rport_supported_classes = 1,
817 .get_starget_node_name = qla2x00_get_starget_node_name,
818 .show_starget_node_name = 1,
819 .get_starget_port_name = qla2x00_get_starget_port_name,
820 .show_starget_port_name = 1,
821 .get_starget_port_id = qla2x00_get_starget_port_id,
822 .show_starget_port_id = 1,
824 .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
825 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
826 .show_rport_dev_loss_tmo = 1,
828 .issue_fc_host_lip = qla2x00_issue_lip,
829 .get_fc_host_stats = qla2x00_get_fc_host_stats,
833 qla2x00_init_host_attr(scsi_qla_host_t *ha)
835 fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
836 fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
837 fc_host_supported_classes(ha->host) = FC_COS_CLASS3;