sas_host_smp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Serial Attached SCSI (SAS) Expander discovery and configuration
  3. *
  4. * Copyright (C) 2007 James E.J. Bottomley
  5. * <James.Bottomley@HansenPartnership.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; version 2 only.
  10. */
  11. #include <linux/scatterlist.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/slab.h>
  14. #include <linux/export.h>
  15. #include "sas_internal.h"
  16. #include <scsi/scsi_transport.h>
  17. #include <scsi/scsi_transport_sas.h>
  18. #include "../scsi_sas_internal.h"
  19. static void sas_host_smp_discover(struct sas_ha_struct *sas_ha, u8 *resp_data,
  20. u8 phy_id)
  21. {
  22. struct sas_phy *phy;
  23. struct sas_rphy *rphy;
  24. if (phy_id >= sas_ha->num_phys) {
  25. resp_data[2] = SMP_RESP_NO_PHY;
  26. return;
  27. }
  28. resp_data[2] = SMP_RESP_FUNC_ACC;
  29. phy = sas_ha->sas_phy[phy_id]->phy;
  30. resp_data[9] = phy_id;
  31. resp_data[13] = phy->negotiated_linkrate;
  32. memcpy(resp_data + 16, sas_ha->sas_addr, SAS_ADDR_SIZE);
  33. memcpy(resp_data + 24, sas_ha->sas_phy[phy_id]->attached_sas_addr,
  34. SAS_ADDR_SIZE);
  35. resp_data[40] = (phy->minimum_linkrate << 4) |
  36. phy->minimum_linkrate_hw;
  37. resp_data[41] = (phy->maximum_linkrate << 4) |
  38. phy->maximum_linkrate_hw;
  39. if (!sas_ha->sas_phy[phy_id]->port ||
  40. !sas_ha->sas_phy[phy_id]->port->port_dev)
  41. return;
  42. rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
  43. resp_data[12] = rphy->identify.device_type << 4;
  44. resp_data[14] = rphy->identify.initiator_port_protocols;
  45. resp_data[15] = rphy->identify.target_port_protocols;
  46. }
  47. /**
  48. * to_sas_gpio_gp_bit - given the gpio frame data find the byte/bit position of 'od'
  49. * @od: od bit to find
  50. * @data: incoming bitstream (from frame)
  51. * @index: requested data register index (from frame)
  52. * @count: total number of registers in the bitstream (from frame)
  53. * @bit: bit position of 'od' in the returned byte
  54. *
  55. * returns NULL if 'od' is not in 'data'
  56. *
  57. * From SFF-8485 v0.7:
  58. * "In GPIO_TX[1], bit 0 of byte 3 contains the first bit (i.e., OD0.0)
  59. * and bit 7 of byte 0 contains the 32nd bit (i.e., OD10.1).
  60. *
  61. * In GPIO_TX[2], bit 0 of byte 3 contains the 33rd bit (i.e., OD10.2)
  62. * and bit 7 of byte 0 contains the 64th bit (i.e., OD21.0)."
  63. *
  64. * The general-purpose (raw-bitstream) RX registers have the same layout
  65. * although 'od' is renamed 'id' for 'input data'.
  66. *
  67. * SFF-8489 defines the behavior of the LEDs in response to the 'od' values.
  68. */
  69. static u8 *to_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count, u8 *bit)
  70. {
  71. unsigned int reg;
  72. u8 byte;
  73. /* gp registers start at index 1 */
  74. if (index == 0)
  75. return NULL;
  76. index--; /* make index 0-based */
  77. if (od < index * 32)
  78. return NULL;
  79. od -= index * 32;
  80. reg = od >> 5;
  81. if (reg >= count)
  82. return NULL;
  83. od &= (1 << 5) - 1;
  84. byte = 3 - (od >> 3);
  85. *bit = od & ((1 << 3) - 1);
  86. return &data[reg * 4 + byte];
  87. }
  88. int try_test_sas_gpio_gp_bit(unsigned int od, u8 *data, u8 index, u8 count)
  89. {
  90. u8 *byte;
  91. u8 bit;
  92. byte = to_sas_gpio_gp_bit(od, data, index, count, &bit);
  93. if (!byte)
  94. return -1;
  95. return (*byte >> bit) & 1;
  96. }
  97. EXPORT_SYMBOL(try_test_sas_gpio_gp_bit);
  98. static int sas_host_smp_write_gpio(struct sas_ha_struct *sas_ha, u8 *resp_data,
  99. u8 reg_type, u8 reg_index, u8 reg_count,
  100. u8 *req_data)
  101. {
  102. struct sas_internal *i = to_sas_internal(sas_ha->core.shost->transportt);
  103. int written;
  104. if (i->dft->lldd_write_gpio == NULL) {
  105. resp_data[2] = SMP_RESP_FUNC_UNK;
  106. return 0;
  107. }
  108. written = i->dft->lldd_write_gpio(sas_ha, reg_type, reg_index,
  109. reg_count, req_data);
  110. if (written < 0) {
  111. resp_data[2] = SMP_RESP_FUNC_FAILED;
  112. written = 0;
  113. } else
  114. resp_data[2] = SMP_RESP_FUNC_ACC;
  115. return written;
  116. }
  117. static void sas_report_phy_sata(struct sas_ha_struct *sas_ha, u8 *resp_data,
  118. u8 phy_id)
  119. {
  120. struct sas_rphy *rphy;
  121. struct dev_to_host_fis *fis;
  122. int i;
  123. if (phy_id >= sas_ha->num_phys) {
  124. resp_data[2] = SMP_RESP_NO_PHY;
  125. return;
  126. }
  127. resp_data[2] = SMP_RESP_PHY_NO_SATA;
  128. if (!sas_ha->sas_phy[phy_id]->port)
  129. return;
  130. rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
  131. fis = (struct dev_to_host_fis *)
  132. sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd;
  133. if (rphy->identify.target_port_protocols != SAS_PROTOCOL_SATA)
  134. return;
  135. resp_data[2] = SMP_RESP_FUNC_ACC;
  136. resp_data[9] = phy_id;
  137. memcpy(resp_data + 16, sas_ha->sas_phy[phy_id]->attached_sas_addr,
  138. SAS_ADDR_SIZE);
  139. /* check to see if we have a valid d2h fis */
  140. if (fis->fis_type != 0x34)
  141. return;
  142. /* the d2h fis is required by the standard to be in LE format */
  143. for (i = 0; i < 20; i += 4) {
  144. u8 *dst = resp_data + 24 + i, *src =
  145. &sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd[i];
  146. dst[0] = src[3];
  147. dst[1] = src[2];
  148. dst[2] = src[1];
  149. dst[3] = src[0];
  150. }
  151. }
  152. static void sas_phy_control(struct sas_ha_struct *sas_ha, u8 phy_id,
  153. u8 phy_op, enum sas_linkrate min,
  154. enum sas_linkrate max, u8 *resp_data)
  155. {
  156. struct sas_internal *i =
  157. to_sas_internal(sas_ha->core.shost->transportt);
  158. struct sas_phy_linkrates rates;
  159. struct asd_sas_phy *asd_phy;
  160. if (phy_id >= sas_ha->num_phys) {
  161. resp_data[2] = SMP_RESP_NO_PHY;
  162. return;
  163. }
  164. asd_phy = sas_ha->sas_phy[phy_id];
  165. switch (phy_op) {
  166. case PHY_FUNC_NOP:
  167. case PHY_FUNC_LINK_RESET:
  168. case PHY_FUNC_HARD_RESET:
  169. case PHY_FUNC_DISABLE:
  170. case PHY_FUNC_CLEAR_ERROR_LOG:
  171. case PHY_FUNC_CLEAR_AFFIL:
  172. case PHY_FUNC_TX_SATA_PS_SIGNAL:
  173. break;
  174. default:
  175. resp_data[2] = SMP_RESP_PHY_UNK_OP;
  176. return;
  177. }
  178. rates.minimum_linkrate = min;
  179. rates.maximum_linkrate = max;
  180. /* filter reset requests through libata eh */
  181. if (phy_op == PHY_FUNC_LINK_RESET && sas_try_ata_reset(asd_phy) == 0) {
  182. resp_data[2] = SMP_RESP_FUNC_ACC;
  183. return;
  184. }
  185. if (i->dft->lldd_control_phy(asd_phy, phy_op, &rates))
  186. resp_data[2] = SMP_RESP_FUNC_FAILED;
  187. else
  188. resp_data[2] = SMP_RESP_FUNC_ACC;
  189. }
  190. void sas_smp_host_handler(struct bsg_job *job, struct Scsi_Host *shost)
  191. {
  192. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  193. u8 *req_data, *resp_data;
  194. unsigned int reslen = 0;
  195. int error = -EINVAL;
  196. /* eight is the minimum size for request and response frames */
  197. if (job->request_payload.payload_len < 8 ||
  198. job->reply_payload.payload_len < 8)
  199. goto out;
  200. error = -ENOMEM;
  201. req_data = kzalloc(job->request_payload.payload_len, GFP_KERNEL);
  202. if (!req_data)
  203. goto out;
  204. sg_copy_to_buffer(job->request_payload.sg_list,
  205. job->request_payload.sg_cnt, req_data,
  206. job->request_payload.payload_len);
  207. /* make sure frame can always be built ... we copy
  208. * back only the requested length */
  209. resp_data = kzalloc(max(job->reply_payload.payload_len, 128U),
  210. GFP_KERNEL);
  211. if (!resp_data)
  212. goto out_free_req;
  213. error = -EINVAL;
  214. if (req_data[0] != SMP_REQUEST)
  215. goto out_free_resp;
  216. /* set up default don't know response */
  217. resp_data[0] = SMP_RESPONSE;
  218. resp_data[1] = req_data[1];
  219. resp_data[2] = SMP_RESP_FUNC_UNK;
  220. switch (req_data[1]) {
  221. case SMP_REPORT_GENERAL:
  222. resp_data[2] = SMP_RESP_FUNC_ACC;
  223. resp_data[9] = sas_ha->num_phys;
  224. reslen = 32;
  225. break;
  226. case SMP_REPORT_MANUF_INFO:
  227. resp_data[2] = SMP_RESP_FUNC_ACC;
  228. memcpy(resp_data + 12, shost->hostt->name,
  229. SAS_EXPANDER_VENDOR_ID_LEN);
  230. memcpy(resp_data + 20, "libsas virt phy",
  231. SAS_EXPANDER_PRODUCT_ID_LEN);
  232. reslen = 64;
  233. break;
  234. case SMP_READ_GPIO_REG:
  235. /* FIXME: need GPIO support in the transport class */
  236. break;
  237. case SMP_DISCOVER:
  238. if (job->request_payload.payload_len < 16)
  239. goto out_free_resp;
  240. sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
  241. reslen = 56;
  242. break;
  243. case SMP_REPORT_PHY_ERR_LOG:
  244. /* FIXME: could implement this with additional
  245. * libsas callbacks providing the HW supports it */
  246. break;
  247. case SMP_REPORT_PHY_SATA:
  248. if (job->request_payload.payload_len < 16)
  249. goto out_free_resp;
  250. sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
  251. reslen = 60;
  252. break;
  253. case SMP_REPORT_ROUTE_INFO:
  254. /* Can't implement; hosts have no routes */
  255. break;
  256. case SMP_WRITE_GPIO_REG: {
  257. /* SFF-8485 v0.7 */
  258. const int base_frame_size = 11;
  259. int to_write = req_data[4];
  260. if (job->request_payload.payload_len <
  261. base_frame_size + to_write * 4) {
  262. resp_data[2] = SMP_RESP_INV_FRM_LEN;
  263. break;
  264. }
  265. to_write = sas_host_smp_write_gpio(sas_ha, resp_data, req_data[2],
  266. req_data[3], to_write, &req_data[8]);
  267. reslen = 8;
  268. break;
  269. }
  270. case SMP_CONF_ROUTE_INFO:
  271. /* Can't implement; hosts have no routes */
  272. break;
  273. case SMP_PHY_CONTROL:
  274. if (job->request_payload.payload_len < 44)
  275. goto out_free_resp;
  276. sas_phy_control(sas_ha, req_data[9], req_data[10],
  277. req_data[32] >> 4, req_data[33] >> 4,
  278. resp_data);
  279. reslen = 8;
  280. break;
  281. case SMP_PHY_TEST_FUNCTION:
  282. /* FIXME: should this be implemented? */
  283. break;
  284. default:
  285. /* probably a 2.0 function */
  286. break;
  287. }
  288. sg_copy_from_buffer(job->reply_payload.sg_list,
  289. job->reply_payload.sg_cnt, resp_data,
  290. job->reply_payload.payload_len);
  291. error = 0;
  292. out_free_resp:
  293. kfree(resp_data);
  294. out_free_req:
  295. kfree(req_data);
  296. out:
  297. bsg_job_done(job, error, reslen);
  298. }