sas_host_smp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. int sas_smp_host_handler(struct Scsi_Host *shost, struct request *req,
  191. struct request *rsp)
  192. {
  193. u8 *req_data = NULL, *resp_data = NULL, *buf;
  194. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  195. int error = -EINVAL;
  196. /* eight is the minimum size for request and response frames */
  197. if (blk_rq_bytes(req) < 8 || blk_rq_bytes(rsp) < 8)
  198. goto out;
  199. if (bio_offset(req->bio) + blk_rq_bytes(req) > PAGE_SIZE ||
  200. bio_offset(rsp->bio) + blk_rq_bytes(rsp) > PAGE_SIZE) {
  201. shost_printk(KERN_ERR, shost,
  202. "SMP request/response frame crosses page boundary");
  203. goto out;
  204. }
  205. req_data = kzalloc(blk_rq_bytes(req), GFP_KERNEL);
  206. /* make sure frame can always be built ... we copy
  207. * back only the requested length */
  208. resp_data = kzalloc(max(blk_rq_bytes(rsp), 128U), GFP_KERNEL);
  209. if (!req_data || !resp_data) {
  210. error = -ENOMEM;
  211. goto out;
  212. }
  213. local_irq_disable();
  214. buf = kmap_atomic(bio_page(req->bio));
  215. memcpy(req_data, buf, blk_rq_bytes(req));
  216. kunmap_atomic(buf - bio_offset(req->bio));
  217. local_irq_enable();
  218. if (req_data[0] != SMP_REQUEST)
  219. goto out;
  220. /* always succeeds ... even if we can't process the request
  221. * the result is in the response frame */
  222. error = 0;
  223. /* set up default don't know response */
  224. resp_data[0] = SMP_RESPONSE;
  225. resp_data[1] = req_data[1];
  226. resp_data[2] = SMP_RESP_FUNC_UNK;
  227. switch (req_data[1]) {
  228. case SMP_REPORT_GENERAL:
  229. req->resid_len -= 8;
  230. rsp->resid_len -= 32;
  231. resp_data[2] = SMP_RESP_FUNC_ACC;
  232. resp_data[9] = sas_ha->num_phys;
  233. break;
  234. case SMP_REPORT_MANUF_INFO:
  235. req->resid_len -= 8;
  236. rsp->resid_len -= 64;
  237. resp_data[2] = SMP_RESP_FUNC_ACC;
  238. memcpy(resp_data + 12, shost->hostt->name,
  239. SAS_EXPANDER_VENDOR_ID_LEN);
  240. memcpy(resp_data + 20, "libsas virt phy",
  241. SAS_EXPANDER_PRODUCT_ID_LEN);
  242. break;
  243. case SMP_READ_GPIO_REG:
  244. /* FIXME: need GPIO support in the transport class */
  245. break;
  246. case SMP_DISCOVER:
  247. req->resid_len -= 16;
  248. if ((int)req->resid_len < 0) {
  249. req->resid_len = 0;
  250. error = -EINVAL;
  251. goto out;
  252. }
  253. rsp->resid_len -= 56;
  254. sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
  255. break;
  256. case SMP_REPORT_PHY_ERR_LOG:
  257. /* FIXME: could implement this with additional
  258. * libsas callbacks providing the HW supports it */
  259. break;
  260. case SMP_REPORT_PHY_SATA:
  261. req->resid_len -= 16;
  262. if ((int)req->resid_len < 0) {
  263. req->resid_len = 0;
  264. error = -EINVAL;
  265. goto out;
  266. }
  267. rsp->resid_len -= 60;
  268. sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
  269. break;
  270. case SMP_REPORT_ROUTE_INFO:
  271. /* Can't implement; hosts have no routes */
  272. break;
  273. case SMP_WRITE_GPIO_REG: {
  274. /* SFF-8485 v0.7 */
  275. const int base_frame_size = 11;
  276. int to_write = req_data[4];
  277. if (blk_rq_bytes(req) < base_frame_size + to_write * 4 ||
  278. req->resid_len < base_frame_size + to_write * 4) {
  279. resp_data[2] = SMP_RESP_INV_FRM_LEN;
  280. break;
  281. }
  282. to_write = sas_host_smp_write_gpio(sas_ha, resp_data, req_data[2],
  283. req_data[3], to_write, &req_data[8]);
  284. req->resid_len -= base_frame_size + to_write * 4;
  285. rsp->resid_len -= 8;
  286. break;
  287. }
  288. case SMP_CONF_ROUTE_INFO:
  289. /* Can't implement; hosts have no routes */
  290. break;
  291. case SMP_PHY_CONTROL:
  292. req->resid_len -= 44;
  293. if ((int)req->resid_len < 0) {
  294. req->resid_len = 0;
  295. error = -EINVAL;
  296. goto out;
  297. }
  298. rsp->resid_len -= 8;
  299. sas_phy_control(sas_ha, req_data[9], req_data[10],
  300. req_data[32] >> 4, req_data[33] >> 4,
  301. resp_data);
  302. break;
  303. case SMP_PHY_TEST_FUNCTION:
  304. /* FIXME: should this be implemented? */
  305. break;
  306. default:
  307. /* probably a 2.0 function */
  308. break;
  309. }
  310. local_irq_disable();
  311. buf = kmap_atomic(bio_page(rsp->bio));
  312. memcpy(buf, resp_data, blk_rq_bytes(rsp));
  313. flush_kernel_dcache_page(bio_page(rsp->bio));
  314. kunmap_atomic(buf - bio_offset(rsp->bio));
  315. local_irq_enable();
  316. out:
  317. kfree(req_data);
  318. kfree(resp_data);
  319. return error;
  320. }