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)));
20 if (ha->fw_dump_reading == 0)
22 if (off > ha->fw_dump_buffer_len)
24 if (off + count > ha->fw_dump_buffer_len)
25 count = ha->fw_dump_buffer_len - off;
27 memcpy(buf, &ha->fw_dump_buffer[off], count);
33 qla2x00_sysfs_write_fw_dump(struct kobject *kobj, char *buf, loff_t off,
36 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
37 struct device, kobj)));
44 reading = simple_strtol(buf, NULL, 10);
47 if (ha->fw_dump_reading == 1) {
48 qla_printk(KERN_INFO, ha,
49 "Firmware dump cleared on (%ld).\n",
52 vfree(ha->fw_dump_buffer);
53 if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha))
54 free_pages((unsigned long)ha->fw_dump,
57 ha->fw_dump_reading = 0;
58 ha->fw_dump_buffer = NULL;
64 if ((ha->fw_dump || ha->fw_dumped) && !ha->fw_dump_reading) {
65 ha->fw_dump_reading = 1;
67 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
68 dump_size = FW_DUMP_SIZE_24XX;
70 dump_size = FW_DUMP_SIZE_1M;
71 if (ha->fw_memory_size < 0x20000)
72 dump_size = FW_DUMP_SIZE_128K;
73 else if (ha->fw_memory_size < 0x80000)
74 dump_size = FW_DUMP_SIZE_512K;
76 ha->fw_dump_buffer = (char *)vmalloc(dump_size);
77 if (ha->fw_dump_buffer == NULL) {
78 qla_printk(KERN_WARNING, ha,
79 "Unable to allocate memory for firmware "
80 "dump buffer (%d).\n", dump_size);
82 ha->fw_dump_reading = 0;
85 qla_printk(KERN_INFO, ha,
86 "Firmware dump ready for read on (%ld).\n",
88 memset(ha->fw_dump_buffer, 0, dump_size);
89 ha->isp_ops.ascii_fw_dump(ha);
90 ha->fw_dump_buffer_len = strlen(ha->fw_dump_buffer);
97 static struct bin_attribute sysfs_fw_dump_attr = {
100 .mode = S_IRUSR | S_IWUSR,
101 .owner = THIS_MODULE,
104 .read = qla2x00_sysfs_read_fw_dump,
105 .write = qla2x00_sysfs_write_fw_dump,
109 qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
112 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
113 struct device, kobj)));
116 if (!capable(CAP_SYS_ADMIN) || off != 0)
120 spin_lock_irqsave(&ha->hardware_lock, flags);
121 ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->nvram_base,
123 spin_unlock_irqrestore(&ha->hardware_lock, flags);
125 return ha->nvram_size;
129 qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
132 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
133 struct device, kobj)));
137 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
140 /* Checksum NVRAM. */
141 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
145 iter = (uint32_t *)buf;
147 for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
148 chksum += le32_to_cpu(*iter++);
149 chksum = ~chksum + 1;
150 *iter = cpu_to_le32(chksum);
155 iter = (uint8_t *)buf;
157 for (cnt = 0; cnt < count - 1; cnt++)
159 chksum = ~chksum + 1;
164 spin_lock_irqsave(&ha->hardware_lock, flags);
165 ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
166 spin_unlock_irqrestore(&ha->hardware_lock, flags);
171 static struct bin_attribute sysfs_nvram_attr = {
174 .mode = S_IRUSR | S_IWUSR,
175 .owner = THIS_MODULE,
178 .read = qla2x00_sysfs_read_nvram,
179 .write = qla2x00_sysfs_write_nvram,
183 qla2x00_sysfs_read_optrom(struct kobject *kobj, char *buf, loff_t off,
186 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
187 struct device, kobj)));
189 if (ha->optrom_state != QLA_SREADING)
191 if (off > ha->optrom_size)
193 if (off + count > ha->optrom_size)
194 count = ha->optrom_size - off;
196 memcpy(buf, &ha->optrom_buffer[off], count);
202 qla2x00_sysfs_write_optrom(struct kobject *kobj, char *buf, loff_t off,
205 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
206 struct device, kobj)));
208 if (ha->optrom_state != QLA_SWRITING)
210 if (off > ha->optrom_size)
212 if (off + count > ha->optrom_size)
213 count = ha->optrom_size - off;
215 memcpy(&ha->optrom_buffer[off], buf, count);
220 static struct bin_attribute sysfs_optrom_attr = {
223 .mode = S_IRUSR | S_IWUSR,
224 .owner = THIS_MODULE,
226 .size = OPTROM_SIZE_24XX,
227 .read = qla2x00_sysfs_read_optrom,
228 .write = qla2x00_sysfs_write_optrom,
232 qla2x00_sysfs_write_optrom_ctl(struct kobject *kobj, char *buf, loff_t off,
235 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
236 struct device, kobj)));
242 if (sscanf(buf, "%d", &val) != 1)
247 if (ha->optrom_state != QLA_SREADING &&
248 ha->optrom_state != QLA_SWRITING)
251 ha->optrom_state = QLA_SWAITING;
252 vfree(ha->optrom_buffer);
253 ha->optrom_buffer = NULL;
256 if (ha->optrom_state != QLA_SWAITING)
259 ha->optrom_state = QLA_SREADING;
260 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
261 if (ha->optrom_buffer == NULL) {
262 qla_printk(KERN_WARNING, ha,
263 "Unable to allocate memory for optrom retrieval "
264 "(%x).\n", ha->optrom_size);
266 ha->optrom_state = QLA_SWAITING;
270 memset(ha->optrom_buffer, 0, ha->optrom_size);
271 ha->isp_ops.read_optrom(ha, ha->optrom_buffer, 0,
275 if (ha->optrom_state != QLA_SWAITING)
278 ha->optrom_state = QLA_SWRITING;
279 ha->optrom_buffer = (uint8_t *)vmalloc(ha->optrom_size);
280 if (ha->optrom_buffer == NULL) {
281 qla_printk(KERN_WARNING, ha,
282 "Unable to allocate memory for optrom update "
283 "(%x).\n", ha->optrom_size);
285 ha->optrom_state = QLA_SWAITING;
288 memset(ha->optrom_buffer, 0, ha->optrom_size);
291 if (ha->optrom_state != QLA_SWRITING)
294 ha->isp_ops.write_optrom(ha, ha->optrom_buffer, 0,
301 static struct bin_attribute sysfs_optrom_ctl_attr = {
303 .name = "optrom_ctl",
305 .owner = THIS_MODULE,
308 .write = qla2x00_sysfs_write_optrom_ctl,
312 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
314 struct Scsi_Host *host = ha->host;
316 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
317 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
318 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
319 sysfs_create_bin_file(&host->shost_gendev.kobj,
320 &sysfs_optrom_ctl_attr);
324 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
326 struct Scsi_Host *host = ha->host;
328 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
329 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
330 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_optrom_attr);
331 sysfs_remove_bin_file(&host->shost_gendev.kobj,
332 &sysfs_optrom_ctl_attr);
334 if (ha->beacon_blink_led == 1)
335 ha->isp_ops.beacon_off(ha);
338 /* Scsi_Host attributes. */
341 qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
343 return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
347 qla2x00_fw_version_show(struct class_device *cdev, char *buf)
349 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
352 return snprintf(buf, PAGE_SIZE, "%s\n",
353 ha->isp_ops.fw_version_str(ha, fw_str));
357 qla2x00_serial_num_show(struct class_device *cdev, char *buf)
359 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
362 sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
363 return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
368 qla2x00_isp_name_show(struct class_device *cdev, char *buf)
370 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
371 return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
375 qla2x00_isp_id_show(struct class_device *cdev, char *buf)
377 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
378 return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
379 ha->product_id[0], ha->product_id[1], ha->product_id[2],
384 qla2x00_model_name_show(struct class_device *cdev, char *buf)
386 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
387 return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
391 qla2x00_model_desc_show(struct class_device *cdev, char *buf)
393 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
394 return snprintf(buf, PAGE_SIZE, "%s\n",
395 ha->model_desc ? ha->model_desc: "");
399 qla2x00_pci_info_show(struct class_device *cdev, char *buf)
401 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
404 return snprintf(buf, PAGE_SIZE, "%s\n",
405 ha->isp_ops.pci_info_str(ha, pci_info));
409 qla2x00_state_show(struct class_device *cdev, char *buf)
411 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
414 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
415 atomic_read(&ha->loop_state) == LOOP_DEAD)
416 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
417 else if (atomic_read(&ha->loop_state) != LOOP_READY ||
418 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
419 test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
420 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
422 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
424 switch (ha->current_topology) {
426 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
429 len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
432 len += snprintf(buf + len, PAGE_SIZE-len,
433 "N_Port to N_Port\n");
436 len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
439 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
447 qla2x00_zio_show(struct class_device *cdev, char *buf)
449 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
452 switch (ha->zio_mode) {
454 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 5\n");
457 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
459 case QLA_ZIO_DISABLED:
460 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
467 qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
469 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
473 if (sscanf(buf, "%d", &val) != 1)
478 zio_mode = QLA_ZIO_MODE_5;
481 zio_mode = QLA_ZIO_MODE_6;
484 zio_mode = QLA_ZIO_DISABLED;
488 /* Update per-hba values and queue a reset. */
489 if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
490 ha->zio_mode = zio_mode;
491 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
497 qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
499 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
501 return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
505 qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
508 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
512 if (sscanf(buf, "%d", &val) != 1)
514 if (val > 25500 || val < 100)
517 zio_timer = (uint16_t)(val / 100);
518 ha->zio_timer = zio_timer;
524 qla2x00_beacon_show(struct class_device *cdev, char *buf)
526 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
529 if (ha->beacon_blink_led)
530 len += snprintf(buf + len, PAGE_SIZE-len, "Enabled\n");
532 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
537 qla2x00_beacon_store(struct class_device *cdev, const char *buf,
540 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
544 if (IS_QLA2100(ha) || IS_QLA2200(ha))
547 if (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) {
548 qla_printk(KERN_WARNING, ha,
549 "Abort ISP active -- ignoring beacon request.\n");
553 if (sscanf(buf, "%d", &val) != 1)
557 rval = ha->isp_ops.beacon_on(ha);
559 rval = ha->isp_ops.beacon_off(ha);
561 if (rval != QLA_SUCCESS)
567 static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
569 static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
570 static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
571 static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
572 static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
573 static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
574 static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
575 static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
576 static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
577 static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
579 static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
580 qla2x00_zio_timer_store);
581 static CLASS_DEVICE_ATTR(beacon, S_IRUGO | S_IWUSR, qla2x00_beacon_show,
582 qla2x00_beacon_store);
584 struct class_device_attribute *qla2x00_host_attrs[] = {
585 &class_device_attr_driver_version,
586 &class_device_attr_fw_version,
587 &class_device_attr_serial_num,
588 &class_device_attr_isp_name,
589 &class_device_attr_isp_id,
590 &class_device_attr_model_name,
591 &class_device_attr_model_desc,
592 &class_device_attr_pci_info,
593 &class_device_attr_state,
594 &class_device_attr_zio,
595 &class_device_attr_zio_timer,
596 &class_device_attr_beacon,
600 /* Host attributes. */
603 qla2x00_get_host_port_id(struct Scsi_Host *shost)
605 scsi_qla_host_t *ha = to_qla_host(shost);
607 fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
608 ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
612 qla2x00_get_host_speed(struct Scsi_Host *shost)
614 scsi_qla_host_t *ha = to_qla_host(shost);
617 switch (ha->link_data_rate) {
628 fc_host_speed(shost) = speed;
632 qla2x00_get_host_port_type(struct Scsi_Host *shost)
634 scsi_qla_host_t *ha = to_qla_host(shost);
635 uint32_t port_type = FC_PORTTYPE_UNKNOWN;
637 switch (ha->current_topology) {
639 port_type = FC_PORTTYPE_LPORT;
642 port_type = FC_PORTTYPE_NLPORT;
645 port_type = FC_PORTTYPE_PTP;
648 port_type = FC_PORTTYPE_NPORT;
651 fc_host_port_type(shost) = port_type;
655 qla2x00_get_starget_node_name(struct scsi_target *starget)
657 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
658 scsi_qla_host_t *ha = to_qla_host(host);
662 list_for_each_entry(fcport, &ha->fcports, list) {
663 if (starget->id == fcport->os_target_id) {
664 node_name = wwn_to_u64(fcport->node_name);
669 fc_starget_node_name(starget) = node_name;
673 qla2x00_get_starget_port_name(struct scsi_target *starget)
675 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
676 scsi_qla_host_t *ha = to_qla_host(host);
680 list_for_each_entry(fcport, &ha->fcports, list) {
681 if (starget->id == fcport->os_target_id) {
682 port_name = wwn_to_u64(fcport->port_name);
687 fc_starget_port_name(starget) = port_name;
691 qla2x00_get_starget_port_id(struct scsi_target *starget)
693 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
694 scsi_qla_host_t *ha = to_qla_host(host);
696 uint32_t port_id = ~0U;
698 list_for_each_entry(fcport, &ha->fcports, list) {
699 if (starget->id == fcport->os_target_id) {
700 port_id = fcport->d_id.b.domain << 16 |
701 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
706 fc_starget_port_id(starget) = port_id;
710 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
712 struct Scsi_Host *host = rport_to_shost(rport);
713 scsi_qla_host_t *ha = to_qla_host(host);
715 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
719 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
721 struct Scsi_Host *host = rport_to_shost(rport);
722 scsi_qla_host_t *ha = to_qla_host(host);
725 ha->port_down_retry_count = timeout;
727 ha->port_down_retry_count = 1;
729 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
733 qla2x00_issue_lip(struct Scsi_Host *shost)
735 scsi_qla_host_t *ha = to_qla_host(shost);
737 set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
741 static struct fc_host_statistics *
742 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
744 scsi_qla_host_t *ha = to_qla_host(shost);
747 link_stat_t stat_buf;
748 struct fc_host_statistics *pfc_host_stat;
750 pfc_host_stat = &ha->fc_host_stat;
751 memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
753 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
754 rval = qla24xx_get_isp_stats(ha, (uint32_t *)&stat_buf,
755 sizeof(stat_buf) / 4, mb_stat);
757 rval = qla2x00_get_link_status(ha, ha->loop_id, &stat_buf,
761 qla_printk(KERN_WARNING, ha,
762 "Unable to retrieve host statistics (%d).\n", mb_stat[0]);
763 return pfc_host_stat;
766 pfc_host_stat->link_failure_count = stat_buf.link_fail_cnt;
767 pfc_host_stat->loss_of_sync_count = stat_buf.loss_sync_cnt;
768 pfc_host_stat->loss_of_signal_count = stat_buf.loss_sig_cnt;
769 pfc_host_stat->prim_seq_protocol_err_count = stat_buf.prim_seq_err_cnt;
770 pfc_host_stat->invalid_tx_word_count = stat_buf.inval_xmit_word_cnt;
771 pfc_host_stat->invalid_crc_count = stat_buf.inval_crc_cnt;
773 return pfc_host_stat;
776 struct fc_function_template qla2xxx_transport_functions = {
778 .show_host_node_name = 1,
779 .show_host_port_name = 1,
780 .show_host_supported_classes = 1,
782 .get_host_port_id = qla2x00_get_host_port_id,
783 .show_host_port_id = 1,
784 .get_host_speed = qla2x00_get_host_speed,
785 .show_host_speed = 1,
786 .get_host_port_type = qla2x00_get_host_port_type,
787 .show_host_port_type = 1,
789 .dd_fcrport_size = sizeof(struct fc_port *),
790 .show_rport_supported_classes = 1,
792 .get_starget_node_name = qla2x00_get_starget_node_name,
793 .show_starget_node_name = 1,
794 .get_starget_port_name = qla2x00_get_starget_port_name,
795 .show_starget_port_name = 1,
796 .get_starget_port_id = qla2x00_get_starget_port_id,
797 .show_starget_port_id = 1,
799 .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
800 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
801 .show_rport_dev_loss_tmo = 1,
803 .issue_fc_host_lip = qla2x00_issue_lip,
804 .get_fc_host_stats = qla2x00_get_fc_host_stats,
808 qla2x00_init_host_attr(scsi_qla_host_t *ha)
810 fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
811 fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
812 fc_host_supported_classes(ha->host) = FC_COS_CLASS3;