sas_host_smp.c 8.7 KB

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