sas_host_smp.c 9.3 KB

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