qla_attr.c 14 KB

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