qla_attr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/vmalloc.h>
  9. #include <scsi/scsi_transport_fc.h>
  10. /* SYSFS attributes --------------------------------------------------------- */
  11. static ssize_t
  12. qla2x00_sysfs_read_fw_dump(struct kobject *kobj, char *buf, loff_t off,
  13. size_t count)
  14. {
  15. struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
  16. struct device, kobj)));
  17. if (ha->fw_dump_reading == 0)
  18. return 0;
  19. if (off > ha->fw_dump_buffer_len)
  20. return 0;
  21. if (off + count > ha->fw_dump_buffer_len)
  22. count = ha->fw_dump_buffer_len - off;
  23. memcpy(buf, &ha->fw_dump_buffer[off], count);
  24. return (count);
  25. }
  26. static ssize_t
  27. qla2x00_sysfs_write_fw_dump(struct kobject *kobj, char *buf, loff_t off,
  28. size_t count)
  29. {
  30. struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
  31. struct device, kobj)));
  32. int reading;
  33. uint32_t dump_size;
  34. if (off != 0)
  35. return (0);
  36. reading = simple_strtol(buf, NULL, 10);
  37. switch (reading) {
  38. case 0:
  39. if (ha->fw_dump_reading == 1) {
  40. qla_printk(KERN_INFO, ha,
  41. "Firmware dump cleared on (%ld).\n",
  42. ha->host_no);
  43. vfree(ha->fw_dump_buffer);
  44. if (!IS_QLA24XX(ha) && !IS_QLA25XX(ha))
  45. free_pages((unsigned long)ha->fw_dump,
  46. ha->fw_dump_order);
  47. ha->fw_dump_reading = 0;
  48. ha->fw_dump_buffer = NULL;
  49. ha->fw_dump = NULL;
  50. ha->fw_dumped = 0;
  51. }
  52. break;
  53. case 1:
  54. if ((ha->fw_dump || ha->fw_dumped) && !ha->fw_dump_reading) {
  55. ha->fw_dump_reading = 1;
  56. if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
  57. dump_size = FW_DUMP_SIZE_24XX;
  58. else {
  59. dump_size = FW_DUMP_SIZE_1M;
  60. if (ha->fw_memory_size < 0x20000)
  61. dump_size = FW_DUMP_SIZE_128K;
  62. else if (ha->fw_memory_size < 0x80000)
  63. dump_size = FW_DUMP_SIZE_512K;
  64. }
  65. ha->fw_dump_buffer = (char *)vmalloc(dump_size);
  66. if (ha->fw_dump_buffer == NULL) {
  67. qla_printk(KERN_WARNING, ha,
  68. "Unable to allocate memory for firmware "
  69. "dump buffer (%d).\n", dump_size);
  70. ha->fw_dump_reading = 0;
  71. return (count);
  72. }
  73. qla_printk(KERN_INFO, ha,
  74. "Firmware dump ready for read on (%ld).\n",
  75. ha->host_no);
  76. memset(ha->fw_dump_buffer, 0, dump_size);
  77. ha->isp_ops.ascii_fw_dump(ha);
  78. ha->fw_dump_buffer_len = strlen(ha->fw_dump_buffer);
  79. }
  80. break;
  81. }
  82. return (count);
  83. }
  84. static struct bin_attribute sysfs_fw_dump_attr = {
  85. .attr = {
  86. .name = "fw_dump",
  87. .mode = S_IRUSR | S_IWUSR,
  88. .owner = THIS_MODULE,
  89. },
  90. .size = 0,
  91. .read = qla2x00_sysfs_read_fw_dump,
  92. .write = qla2x00_sysfs_write_fw_dump,
  93. };
  94. static ssize_t
  95. qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
  96. size_t count)
  97. {
  98. struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
  99. struct device, kobj)));
  100. unsigned long flags;
  101. if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
  102. return 0;
  103. /* Read NVRAM. */
  104. spin_lock_irqsave(&ha->hardware_lock, flags);
  105. ha->isp_ops.read_nvram(ha, (uint8_t *)buf, ha->nvram_base,
  106. ha->nvram_size);
  107. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  108. return (count);
  109. }
  110. static ssize_t
  111. qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
  112. size_t count)
  113. {
  114. struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
  115. struct device, kobj)));
  116. unsigned long flags;
  117. uint16_t cnt;
  118. if (!capable(CAP_SYS_ADMIN) || off != 0 || count != ha->nvram_size)
  119. return 0;
  120. /* Checksum NVRAM. */
  121. if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
  122. uint32_t *iter;
  123. uint32_t chksum;
  124. iter = (uint32_t *)buf;
  125. chksum = 0;
  126. for (cnt = 0; cnt < ((count >> 2) - 1); cnt++)
  127. chksum += le32_to_cpu(*iter++);
  128. chksum = ~chksum + 1;
  129. *iter = cpu_to_le32(chksum);
  130. } else {
  131. uint8_t *iter;
  132. uint8_t chksum;
  133. iter = (uint8_t *)buf;
  134. chksum = 0;
  135. for (cnt = 0; cnt < count - 1; cnt++)
  136. chksum += *iter++;
  137. chksum = ~chksum + 1;
  138. *iter = chksum;
  139. }
  140. /* Write NVRAM. */
  141. spin_lock_irqsave(&ha->hardware_lock, flags);
  142. ha->isp_ops.write_nvram(ha, (uint8_t *)buf, ha->nvram_base, count);
  143. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  144. return (count);
  145. }
  146. static struct bin_attribute sysfs_nvram_attr = {
  147. .attr = {
  148. .name = "nvram",
  149. .mode = S_IRUSR | S_IWUSR,
  150. .owner = THIS_MODULE,
  151. },
  152. .size = 0,
  153. .read = qla2x00_sysfs_read_nvram,
  154. .write = qla2x00_sysfs_write_nvram,
  155. };
  156. void
  157. qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
  158. {
  159. struct Scsi_Host *host = ha->host;
  160. sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
  161. sysfs_nvram_attr.size = ha->nvram_size;
  162. sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
  163. }
  164. void
  165. qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
  166. {
  167. struct Scsi_Host *host = ha->host;
  168. sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
  169. sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
  170. }
  171. /* Scsi_Host attributes. */
  172. static ssize_t
  173. qla2x00_drvr_version_show(struct class_device *cdev, char *buf)
  174. {
  175. return snprintf(buf, PAGE_SIZE, "%s\n", qla2x00_version_str);
  176. }
  177. static ssize_t
  178. qla2x00_fw_version_show(struct class_device *cdev, char *buf)
  179. {
  180. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  181. char fw_str[30];
  182. return snprintf(buf, PAGE_SIZE, "%s\n",
  183. ha->isp_ops.fw_version_str(ha, fw_str));
  184. }
  185. static ssize_t
  186. qla2x00_serial_num_show(struct class_device *cdev, char *buf)
  187. {
  188. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  189. uint32_t sn;
  190. sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | ha->serial1;
  191. return snprintf(buf, PAGE_SIZE, "%c%05d\n", 'A' + sn / 100000,
  192. sn % 100000);
  193. }
  194. static ssize_t
  195. qla2x00_isp_name_show(struct class_device *cdev, char *buf)
  196. {
  197. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  198. return snprintf(buf, PAGE_SIZE, "ISP%04X\n", ha->pdev->device);
  199. }
  200. static ssize_t
  201. qla2x00_isp_id_show(struct class_device *cdev, char *buf)
  202. {
  203. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  204. return snprintf(buf, PAGE_SIZE, "%04x %04x %04x %04x\n",
  205. ha->product_id[0], ha->product_id[1], ha->product_id[2],
  206. ha->product_id[3]);
  207. }
  208. static ssize_t
  209. qla2x00_model_name_show(struct class_device *cdev, char *buf)
  210. {
  211. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  212. return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_number);
  213. }
  214. static ssize_t
  215. qla2x00_model_desc_show(struct class_device *cdev, char *buf)
  216. {
  217. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  218. return snprintf(buf, PAGE_SIZE, "%s\n",
  219. ha->model_desc ? ha->model_desc: "");
  220. }
  221. static ssize_t
  222. qla2x00_pci_info_show(struct class_device *cdev, char *buf)
  223. {
  224. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  225. char pci_info[30];
  226. return snprintf(buf, PAGE_SIZE, "%s\n",
  227. ha->isp_ops.pci_info_str(ha, pci_info));
  228. }
  229. static ssize_t
  230. qla2x00_state_show(struct class_device *cdev, char *buf)
  231. {
  232. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  233. int len = 0;
  234. if (atomic_read(&ha->loop_state) == LOOP_DOWN ||
  235. atomic_read(&ha->loop_state) == LOOP_DEAD)
  236. len = snprintf(buf, PAGE_SIZE, "Link Down\n");
  237. else if (atomic_read(&ha->loop_state) != LOOP_READY ||
  238. test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
  239. test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags))
  240. len = snprintf(buf, PAGE_SIZE, "Unknown Link State\n");
  241. else {
  242. len = snprintf(buf, PAGE_SIZE, "Link Up - ");
  243. switch (ha->current_topology) {
  244. case ISP_CFG_NL:
  245. len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
  246. break;
  247. case ISP_CFG_FL:
  248. len += snprintf(buf + len, PAGE_SIZE-len, "FL_Port\n");
  249. break;
  250. case ISP_CFG_N:
  251. len += snprintf(buf + len, PAGE_SIZE-len,
  252. "N_Port to N_Port\n");
  253. break;
  254. case ISP_CFG_F:
  255. len += snprintf(buf + len, PAGE_SIZE-len, "F_Port\n");
  256. break;
  257. default:
  258. len += snprintf(buf + len, PAGE_SIZE-len, "Loop\n");
  259. break;
  260. }
  261. }
  262. return len;
  263. }
  264. static ssize_t
  265. qla2x00_zio_show(struct class_device *cdev, char *buf)
  266. {
  267. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  268. int len = 0;
  269. switch (ha->zio_mode) {
  270. case QLA_ZIO_MODE_5:
  271. len += snprintf(buf + len, PAGE_SIZE-len, "Mode 5\n");
  272. break;
  273. case QLA_ZIO_MODE_6:
  274. len += snprintf(buf + len, PAGE_SIZE-len, "Mode 6\n");
  275. break;
  276. case QLA_ZIO_DISABLED:
  277. len += snprintf(buf + len, PAGE_SIZE-len, "Disabled\n");
  278. break;
  279. }
  280. return len;
  281. }
  282. static ssize_t
  283. qla2x00_zio_store(struct class_device *cdev, const char *buf, size_t count)
  284. {
  285. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  286. int val = 0;
  287. uint16_t zio_mode;
  288. if (sscanf(buf, "%d", &val) != 1)
  289. return -EINVAL;
  290. switch (val) {
  291. case 1:
  292. zio_mode = QLA_ZIO_MODE_5;
  293. break;
  294. case 2:
  295. zio_mode = QLA_ZIO_MODE_6;
  296. break;
  297. default:
  298. zio_mode = QLA_ZIO_DISABLED;
  299. break;
  300. }
  301. /* Update per-hba values and queue a reset. */
  302. if (zio_mode != QLA_ZIO_DISABLED || ha->zio_mode != QLA_ZIO_DISABLED) {
  303. ha->zio_mode = zio_mode;
  304. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  305. }
  306. return strlen(buf);
  307. }
  308. static ssize_t
  309. qla2x00_zio_timer_show(struct class_device *cdev, char *buf)
  310. {
  311. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  312. return snprintf(buf, PAGE_SIZE, "%d us\n", ha->zio_timer * 100);
  313. }
  314. static ssize_t
  315. qla2x00_zio_timer_store(struct class_device *cdev, const char *buf,
  316. size_t count)
  317. {
  318. scsi_qla_host_t *ha = to_qla_host(class_to_shost(cdev));
  319. int val = 0;
  320. uint16_t zio_timer;
  321. if (sscanf(buf, "%d", &val) != 1)
  322. return -EINVAL;
  323. if (val > 25500 || val < 100)
  324. return -ERANGE;
  325. zio_timer = (uint16_t)(val / 100);
  326. ha->zio_timer = zio_timer;
  327. return strlen(buf);
  328. }
  329. static CLASS_DEVICE_ATTR(driver_version, S_IRUGO, qla2x00_drvr_version_show,
  330. NULL);
  331. static CLASS_DEVICE_ATTR(fw_version, S_IRUGO, qla2x00_fw_version_show, NULL);
  332. static CLASS_DEVICE_ATTR(serial_num, S_IRUGO, qla2x00_serial_num_show, NULL);
  333. static CLASS_DEVICE_ATTR(isp_name, S_IRUGO, qla2x00_isp_name_show, NULL);
  334. static CLASS_DEVICE_ATTR(isp_id, S_IRUGO, qla2x00_isp_id_show, NULL);
  335. static CLASS_DEVICE_ATTR(model_name, S_IRUGO, qla2x00_model_name_show, NULL);
  336. static CLASS_DEVICE_ATTR(model_desc, S_IRUGO, qla2x00_model_desc_show, NULL);
  337. static CLASS_DEVICE_ATTR(pci_info, S_IRUGO, qla2x00_pci_info_show, NULL);
  338. static CLASS_DEVICE_ATTR(state, S_IRUGO, qla2x00_state_show, NULL);
  339. static CLASS_DEVICE_ATTR(zio, S_IRUGO | S_IWUSR, qla2x00_zio_show,
  340. qla2x00_zio_store);
  341. static CLASS_DEVICE_ATTR(zio_timer, S_IRUGO | S_IWUSR, qla2x00_zio_timer_show,
  342. qla2x00_zio_timer_store);
  343. struct class_device_attribute *qla2x00_host_attrs[] = {
  344. &class_device_attr_driver_version,
  345. &class_device_attr_fw_version,
  346. &class_device_attr_serial_num,
  347. &class_device_attr_isp_name,
  348. &class_device_attr_isp_id,
  349. &class_device_attr_model_name,
  350. &class_device_attr_model_desc,
  351. &class_device_attr_pci_info,
  352. &class_device_attr_state,
  353. &class_device_attr_zio,
  354. &class_device_attr_zio_timer,
  355. NULL,
  356. };
  357. /* Host attributes. */
  358. static void
  359. qla2x00_get_host_port_id(struct Scsi_Host *shost)
  360. {
  361. scsi_qla_host_t *ha = to_qla_host(shost);
  362. fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
  363. ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
  364. }
  365. static void
  366. qla2x00_get_starget_node_name(struct scsi_target *starget)
  367. {
  368. struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
  369. scsi_qla_host_t *ha = to_qla_host(host);
  370. fc_port_t *fcport;
  371. u64 node_name = 0;
  372. list_for_each_entry(fcport, &ha->fcports, list) {
  373. if (starget->id == fcport->os_target_id) {
  374. node_name = wwn_to_u64(fcport->node_name);
  375. break;
  376. }
  377. }
  378. fc_starget_node_name(starget) = node_name;
  379. }
  380. static void
  381. qla2x00_get_starget_port_name(struct scsi_target *starget)
  382. {
  383. struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
  384. scsi_qla_host_t *ha = to_qla_host(host);
  385. fc_port_t *fcport;
  386. u64 port_name = 0;
  387. list_for_each_entry(fcport, &ha->fcports, list) {
  388. if (starget->id == fcport->os_target_id) {
  389. port_name = wwn_to_u64(fcport->port_name);
  390. break;
  391. }
  392. }
  393. fc_starget_port_name(starget) = port_name;
  394. }
  395. static void
  396. qla2x00_get_starget_port_id(struct scsi_target *starget)
  397. {
  398. struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
  399. scsi_qla_host_t *ha = to_qla_host(host);
  400. fc_port_t *fcport;
  401. uint32_t port_id = ~0U;
  402. list_for_each_entry(fcport, &ha->fcports, list) {
  403. if (starget->id == fcport->os_target_id) {
  404. port_id = fcport->d_id.b.domain << 16 |
  405. fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
  406. break;
  407. }
  408. }
  409. fc_starget_port_id(starget) = port_id;
  410. }
  411. static void
  412. qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
  413. {
  414. struct Scsi_Host *host = rport_to_shost(rport);
  415. scsi_qla_host_t *ha = to_qla_host(host);
  416. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  417. }
  418. static void
  419. qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
  420. {
  421. struct Scsi_Host *host = rport_to_shost(rport);
  422. scsi_qla_host_t *ha = to_qla_host(host);
  423. if (timeout)
  424. ha->port_down_retry_count = timeout;
  425. else
  426. ha->port_down_retry_count = 1;
  427. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  428. }
  429. static int
  430. qla2x00_issue_lip(struct Scsi_Host *shost)
  431. {
  432. scsi_qla_host_t *ha = to_qla_host(shost);
  433. set_bit(LOOP_RESET_NEEDED, &ha->dpc_flags);
  434. return 0;
  435. }
  436. struct fc_function_template qla2xxx_transport_functions = {
  437. .show_host_node_name = 1,
  438. .show_host_port_name = 1,
  439. .show_host_supported_classes = 1,
  440. .get_host_port_id = qla2x00_get_host_port_id,
  441. .show_host_port_id = 1,
  442. .dd_fcrport_size = sizeof(struct fc_port *),
  443. .show_rport_supported_classes = 1,
  444. .get_starget_node_name = qla2x00_get_starget_node_name,
  445. .show_starget_node_name = 1,
  446. .get_starget_port_name = qla2x00_get_starget_port_name,
  447. .show_starget_port_name = 1,
  448. .get_starget_port_id = qla2x00_get_starget_port_id,
  449. .show_starget_port_id = 1,
  450. .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
  451. .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
  452. .show_rport_dev_loss_tmo = 1,
  453. .issue_fc_host_lip = qla2x00_issue_lip,
  454. };
  455. void
  456. qla2x00_init_host_attr(scsi_qla_host_t *ha)
  457. {
  458. fc_host_node_name(ha->host) = wwn_to_u64(ha->node_name);
  459. fc_host_port_name(ha->host) = wwn_to_u64(ha->port_name);
  460. fc_host_supported_classes(ha->host) = FC_COS_CLASS3;
  461. }