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 || count != ha->nvram_size)
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);
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_alloc_sysfs_attr(scsi_qla_host_t *ha)
185 struct Scsi_Host *host = ha->host;
187 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
188 sysfs_nvram_attr.size = ha->nvram_size;
189 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
193 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
195 struct Scsi_Host *host = ha->host;
197 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
198 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
201 /* Scsi_Host attributes. */
204 qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
206 return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
210 qla2x00_fw_version_show(struct class_device *cdev, char *buf)
212 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
215 return snprintf(buf, PAGE_SIZE, "%s\n",
216 ha->isp_ops.fw_version_str(ha, fw_str));
220 qla2x00_serial_num_show(struct class_device *cdev, char *buf)
222 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
225 sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
226 return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
231 qla2x00_isp_name_show(struct class_device *cdev, char *buf)
233 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
234 return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
238 qla2x00_isp_id_show(struct class_device *cdev, char *buf)
240 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
241 return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
242 ha->product_id[0], ha->product_id[1], ha->product_id[2],
247 qla2x00_model_name_show(struct class_device *cdev, char *buf)
249 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
250 return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
254 qla2x00_model_desc_show(struct class_device *cdev, char *buf)
256 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
257 return snprintf(buf, PAGE_SIZE, "%s\n",
258 ha->model_desc ? ha->model_desc: "");
262 qla2x00_pci_info_show(struct class_device *cdev, char *buf)
264 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
267 return snprintf(buf, PAGE_SIZE, "%s\n",
268 ha->isp_ops.pci_info_str(ha, pci_info));
272 qla2x00_state_show(struct class_device *cdev, char *buf)
274 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
277 if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
278 atomic_read(&ha->loop_state) == LOOP_DEAD)
279 len = snprintf(buf, PAGE_SIZE, "Link Down\n");
280 else if (atomic_read(&ha->loop_state) != LOOP_READY ||
281 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
282 test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
283 len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
285 len = snprintf(buf, PAGE_SIZE, "Link Up - ");
287 switch (ha->current_topology) {
289 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
292 len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
295 len += snprintf(buf + len, PAGE_SIZE-len,
296 "N_Port to N_Port\n");
299 len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
302 len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
310 qla2x00_zio_show(struct class_device *cdev, char *buf)
312 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
315 switch (ha->zio_mode) {
317 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 5\n");
320 len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
322 case QLA_ZIO_DISABLED:
323 len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
330 qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
332 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
336 if (sscanf(buf, "%d", &val) != 1)
341 zio_mode = QLA_ZIO_MODE_5;
344 zio_mode = QLA_ZIO_MODE_6;
347 zio_mode = QLA_ZIO_DISABLED;
351 /* Update per-hba values and queue a reset. */
352 if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
353 ha->zio_mode = zio_mode;
354 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
360 qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
362 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
364 return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
368 qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
371 scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
375 if (sscanf(buf, "%d", &val) != 1)
377 if (val > 25500 || val < 100)
380 zio_timer = (uint16_t)(val / 100);
381 ha->zio_timer = zio_timer;
386 static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
388 static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
389 static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
390 static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
391 static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
392 static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
393 static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
394 static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
395 static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
396 static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
398 static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
399 qla2x00_zio_timer_store);
401 struct class_device_attribute *qla2x00_host_attrs[] = {
402 &class_device_attr_driver_version,
403 &class_device_attr_fw_version,
404 &class_device_attr_serial_num,
405 &class_device_attr_isp_name,
406 &class_device_attr_isp_id,
407 &class_device_attr_model_name,
408 &class_device_attr_model_desc,
409 &class_device_attr_pci_info,
410 &class_device_attr_state,
411 &class_device_attr_zio,
412 &class_device_attr_zio_timer,
416 /* Host attributes. */
419 qla2x00_get_host_port_id(struct Scsi_Host *shost)
421 scsi_qla_host_t *ha = to_qla_host(shost);
423 fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
424 ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
428 qla2x00_get_host_speed(struct Scsi_Host *shost)
430 scsi_qla_host_t *ha = to_qla_host(shost);
433 switch (ha->link_data_rate) {
444 fc_host_speed(shost) = speed;
448 qla2x00_get_host_port_type(struct Scsi_Host *shost)
450 scsi_qla_host_t *ha = to_qla_host(shost);
451 uint32_t port_type = FC_PORTTYPE_UNKNOWN;
453 switch (ha->current_topology) {
455 port_type = FC_PORTTYPE_LPORT;
458 port_type = FC_PORTTYPE_NLPORT;
461 port_type = FC_PORTTYPE_PTP;
464 port_type = FC_PORTTYPE_NPORT;
467 fc_host_port_type(shost) = port_type;
471 qla2x00_get_starget_node_name(struct scsi_target *starget)
473 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
474 scsi_qla_host_t *ha = to_qla_host(host);
478 list_for_each_entry(fcport, &ha->fcports, list) {
479 if (starget->id == fcport->os_target_id) {
480 node_name = wwn_to_u64(fcport->node_name);
485 fc_starget_node_name(starget) = node_name;
489 qla2x00_get_starget_port_name(struct scsi_target *starget)
491 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
492 scsi_qla_host_t *ha = to_qla_host(host);
496 list_for_each_entry(fcport, &ha->fcports, list) {
497 if (starget->id == fcport->os_target_id) {
498 port_name = wwn_to_u64(fcport->port_name);
503 fc_starget_port_name(starget) = port_name;
507 qla2x00_get_starget_port_id(struct scsi_target *starget)
509 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
510 scsi_qla_host_t *ha = to_qla_host(host);
512 uint32_t port_id = ~0U;
514 list_for_each_entry(fcport, &ha->fcports, list) {
515 if (starget->id == fcport->os_target_id) {
516 port_id = fcport->d_id.b.domain << 16 |
517 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
522 fc_starget_port_id(starget) = port_id;
526 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
528 struct Scsi_Host *host = rport_to_shost(rport);
529 scsi_qla_host_t *ha = to_qla_host(host);
531 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
535 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
537 struct Scsi_Host *host = rport_to_shost(rport);
538 scsi_qla_host_t *ha = to_qla_host(host);
541 ha->port_down_retry_count = timeout;
543 ha->port_down_retry_count = 1;
545 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
549 qla2x00_issue_lip(struct Scsi_Host *shost)
551 scsi_qla_host_t *ha = to_qla_host(shost);
553 set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
557 static struct fc_host_statistics *
558 qla2x00_get_fc_host_stats(struct Scsi_Host *shost)
560 scsi_qla_host_t *ha = to_qla_host(shost);
563 link_stat_t stat_buf;
564 struct fc_host_statistics *pfc_host_stat;
566 pfc_host_stat = &ha->fc_host_stat;
567 memset(pfc_host_stat, -1, sizeof(struct fc_host_statistics));
569 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
570 rval = qla24xx_get_isp_stats(ha, (uint32_t *)&stat_buf,
571 sizeof(stat_buf) / 4, mb_stat);
573 rval = qla2x00_get_link_status(ha, ha->loop_id, &stat_buf,
577 qla_printk(KERN_WARNING, ha,
578 "Unable to retrieve host statistics (%d).\n", mb_stat[0]);
579 return pfc_host_stat;
582 pfc_host_stat->link_failure_count = stat_buf.link_fail_cnt;
583 pfc_host_stat->loss_of_sync_count = stat_buf.loss_sync_cnt;
584 pfc_host_stat->loss_of_signal_count = stat_buf.loss_sig_cnt;
585 pfc_host_stat->prim_seq_protocol_err_count = stat_buf.prim_seq_err_cnt;
586 pfc_host_stat->invalid_tx_word_count = stat_buf.inval_xmit_word_cnt;
587 pfc_host_stat->invalid_crc_count = stat_buf.inval_crc_cnt;
589 return pfc_host_stat;
592 struct fc_function_template qla2xxx_transport_functions = {
594 .show_host_node_name = 1,
595 .show_host_port_name = 1,
596 .show_host_supported_classes = 1,
598 .get_host_port_id = qla2x00_get_host_port_id,
599 .show_host_port_id = 1,
600 .get_host_speed = qla2x00_get_host_speed,
601 .show_host_speed = 1,
602 .get_host_port_type = qla2x00_get_host_port_type,
603 .show_host_port_type = 1,
605 .dd_fcrport_size = sizeof(struct fc_port *),
606 .show_rport_supported_classes = 1,
608 .get_starget_node_name = qla2x00_get_starget_node_name,
609 .show_starget_node_name = 1,
610 .get_starget_port_name = qla2x00_get_starget_port_name,
611 .show_starget_port_name = 1,
612 .get_starget_port_id = qla2x00_get_starget_port_id,
613 .show_starget_port_id = 1,
615 .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
616 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
617 .show_rport_dev_loss_tmo = 1,
619 .issue_fc_host_lip = qla2x00_issue_lip,
620 .get_fc_host_stats = qla2x00_get_fc_host_stats,
624 qla2x00_init_host_attr(scsi_qla_host_t *ha)
626 fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
627 fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
628 fc_host_supported_classes(ha->host) = FC_COS_CLASS3;