qla_attr.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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/version.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. free_pages((unsigned long)ha->fw_dump,
  57. ha->fw_dump_order);
  58. ha->fw_dump_reading = 0;
  59. ha->fw_dump_buffer = NULL;
  60. ha->fw_dump = NULL;
  61. }
  62. break;
  63. case 1:
  64. if (ha->fw_dump != NULL && !ha->fw_dump_reading) {
  65. ha->fw_dump_reading = 1;
  66. dump_size = FW_DUMP_SIZE_1M;
  67. if (ha->fw_memory_size < 0x20000)
  68. dump_size = FW_DUMP_SIZE_128K;
  69. else if (ha->fw_memory_size < 0x80000)
  70. dump_size = FW_DUMP_SIZE_512K;
  71. ha->fw_dump_buffer = (char *)vmalloc(dump_size);
  72. if (ha->fw_dump_buffer == NULL) {
  73. qla_printk(KERN_WARNING, ha,
  74. "Unable to allocate memory for firmware "
  75. "dump buffer (%d).\n", dump_size);
  76. ha->fw_dump_reading = 0;
  77. return (count);
  78. }
  79. qla_printk(KERN_INFO, ha,
  80. "Firmware dump ready for read on (%ld).\n",
  81. ha->host_no);
  82. memset(ha->fw_dump_buffer, 0, dump_size);
  83. if (IS_QLA2100(ha) || IS_QLA2200(ha))
  84. qla2100_ascii_fw_dump(ha);
  85. else
  86. qla2300_ascii_fw_dump(ha);
  87. ha->fw_dump_buffer_len = strlen(ha->fw_dump_buffer);
  88. }
  89. break;
  90. }
  91. return (count);
  92. }
  93. static struct bin_attribute sysfs_fw_dump_attr = {
  94. .attr = {
  95. .name = "fw_dump",
  96. .mode = S_IRUSR | S_IWUSR,
  97. .owner = THIS_MODULE,
  98. },
  99. .size = 0,
  100. .read = qla2x00_sysfs_read_fw_dump,
  101. .write = qla2x00_sysfs_write_fw_dump,
  102. };
  103. static ssize_t
  104. qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
  105. size_t count)
  106. {
  107. struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
  108. struct device, kobj)));
  109. uint16_t *witer;
  110. unsigned long flags;
  111. uint16_t cnt;
  112. if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
  113. return 0;
  114. /* Read NVRAM. */
  115. spin_lock_irqsave(&ha->hardware_lock, flags);
  116. qla2x00_lock_nvram_access(ha);
  117. witer = (uint16_t *)buf;
  118. for (cnt = 0; cnt < count / 2; cnt++) {
  119. *witer = cpu_to_le16(qla2x00_get_nvram_word(ha,
  120. cnt+ha->nvram_base));
  121. witer++;
  122. }
  123. qla2x00_unlock_nvram_access(ha);
  124. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  125. return (count);
  126. }
  127. static ssize_t
  128. qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
  129. size_t count)
  130. {
  131. struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
  132. struct device, kobj)));
  133. uint8_t *iter;
  134. uint16_t *witer;
  135. unsigned long flags;
  136. uint16_t cnt;
  137. uint8_t chksum;
  138. if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
  139. return 0;
  140. /* Checksum NVRAM. */
  141. iter = (uint8_t *)buf;
  142. chksum = 0;
  143. for (cnt = 0; cnt < count - 1; cnt++)
  144. chksum += *iter++;
  145. chksum = ~chksum + 1;
  146. *iter = chksum;
  147. /* Write NVRAM. */
  148. spin_lock_irqsave(&ha->hardware_lock, flags);
  149. qla2x00_lock_nvram_access(ha);
  150. qla2x00_release_nvram_protection(ha);
  151. witer = (uint16_t *)buf;
  152. for (cnt = 0; cnt < count / 2; cnt++) {
  153. qla2x00_write_nvram_word(ha, cnt+ha->nvram_base,
  154. cpu_to_le16(*witer));
  155. witer++;
  156. }
  157. qla2x00_unlock_nvram_access(ha);
  158. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  159. return (count);
  160. }
  161. static struct bin_attribute sysfs_nvram_attr = {
  162. .attr = {
  163. .name = "nvram",
  164. .mode = S_IRUSR | S_IWUSR,
  165. .owner = THIS_MODULE,
  166. },
  167. .size = sizeof(nvram_t),
  168. .read = qla2x00_sysfs_read_nvram,
  169. .write = qla2x00_sysfs_write_nvram,
  170. };
  171. void
  172. qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
  173. {
  174. struct Scsi_Host *host = ha->host;
  175. sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
  176. sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
  177. }
  178. void
  179. qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
  180. {
  181. struct Scsi_Host *host = ha->host;
  182. sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
  183. sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
  184. }
  185. /* Host attributes. */
  186. static void
  187. qla2x00_get_host_port_id(struct Scsi_Host *shost)
  188. {
  189. scsi_qla_host_t *ha = to_qla_host(shost);
  190. fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
  191. ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
  192. }
  193. static void
  194. qla2x00_get_starget_node_name(struct scsi_target *starget)
  195. {
  196. struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
  197. scsi_qla_host_t *ha = to_qla_host(host);
  198. os_tgt_t *tq = (os_tgt_t *) TGT_Q(ha, starget->id);
  199. uint64_t node_name = 0;
  200. if (tq->fcport)
  201. node_name = be64_to_cpu(*(uint64_t *)tq->fcport->node_name);
  202. fc_starget_node_name(starget) = node_name;
  203. }
  204. static void
  205. qla2x00_get_starget_port_name(struct scsi_target *starget)
  206. {
  207. struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
  208. scsi_qla_host_t *ha = to_qla_host(host);
  209. os_tgt_t *tq = (os_tgt_t *) TGT_Q(ha, starget->id);
  210. uint64_t port_name = 0;
  211. if (tq->fcport)
  212. port_name = be64_to_cpu(*(uint64_t *)tq->fcport->port_name);
  213. fc_starget_port_name(starget) = port_name;
  214. }
  215. static void
  216. qla2x00_get_starget_port_id(struct scsi_target *starget)
  217. {
  218. struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
  219. scsi_qla_host_t *ha = to_qla_host(host);
  220. os_tgt_t *tq = (os_tgt_t *) TGT_Q(ha, starget->id);
  221. uint32_t port_id = 0;
  222. if (tq->fcport)
  223. port_id = tq->fcport->d_id.b.domain << 16 |
  224. tq->fcport->d_id.b.area << 8 | tq->fcport->d_id.b.al_pa;
  225. fc_starget_port_id(starget) = port_id;
  226. }
  227. static void
  228. qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
  229. {
  230. os_tgt_t *tq = rport->dd_data;
  231. scsi_qla_host_t *ha = tq->ha;
  232. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  233. }
  234. static void
  235. qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
  236. {
  237. os_tgt_t *tq = rport->dd_data;
  238. scsi_qla_host_t *ha = tq->ha;
  239. if (timeout)
  240. ha->port_down_retry_count = timeout;
  241. else
  242. ha->port_down_retry_count = 1;
  243. rport->dev_loss_tmo = ha->port_down_retry_count + 5;
  244. }
  245. static struct fc_function_template qla2xxx_transport_functions = {
  246. .show_host_node_name = 1,
  247. .show_host_port_name = 1,
  248. .get_host_port_id = qla2x00_get_host_port_id,
  249. .show_host_port_id = 1,
  250. .dd_fcrport_size = sizeof(os_tgt_t *),
  251. .get_starget_node_name = qla2x00_get_starget_node_name,
  252. .show_starget_node_name = 1,
  253. .get_starget_port_name = qla2x00_get_starget_port_name,
  254. .show_starget_port_name = 1,
  255. .get_starget_port_id = qla2x00_get_starget_port_id,
  256. .show_starget_port_id = 1,
  257. .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
  258. .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
  259. .show_rport_dev_loss_tmo = 1,
  260. };
  261. struct scsi_transport_template *
  262. qla2x00_alloc_transport_tmpl(void)
  263. {
  264. return (fc_attach_transport(&qla2xxx_transport_functions));
  265. }
  266. void
  267. qla2x00_init_host_attr(scsi_qla_host_t *ha)
  268. {
  269. fc_host_node_name(ha->host) =
  270. be64_to_cpu(*(uint64_t *)ha->init_cb->node_name);
  271. fc_host_port_name(ha->host) =
  272. be64_to_cpu(*(uint64_t *)ha->init_cb->port_name);
  273. }