sas_host_smp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. static void sas_report_phy_sata(struct sas_ha_struct *sas_ha, u8 *resp_data,
  47. u8 phy_id)
  48. {
  49. struct sas_rphy *rphy;
  50. struct dev_to_host_fis *fis;
  51. int i;
  52. if (phy_id >= sas_ha->num_phys) {
  53. resp_data[2] = SMP_RESP_NO_PHY;
  54. return;
  55. }
  56. resp_data[2] = SMP_RESP_PHY_NO_SATA;
  57. if (!sas_ha->sas_phy[phy_id]->port)
  58. return;
  59. rphy = sas_ha->sas_phy[phy_id]->port->port_dev->rphy;
  60. fis = (struct dev_to_host_fis *)
  61. sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd;
  62. if (rphy->identify.target_port_protocols != SAS_PROTOCOL_SATA)
  63. return;
  64. resp_data[2] = SMP_RESP_FUNC_ACC;
  65. resp_data[9] = phy_id;
  66. memcpy(resp_data + 16, sas_ha->sas_phy[phy_id]->attached_sas_addr,
  67. SAS_ADDR_SIZE);
  68. /* check to see if we have a valid d2h fis */
  69. if (fis->fis_type != 0x34)
  70. return;
  71. /* the d2h fis is required by the standard to be in LE format */
  72. for (i = 0; i < 20; i += 4) {
  73. u8 *dst = resp_data + 24 + i, *src =
  74. &sas_ha->sas_phy[phy_id]->port->port_dev->frame_rcvd[i];
  75. dst[0] = src[3];
  76. dst[1] = src[2];
  77. dst[2] = src[1];
  78. dst[3] = src[0];
  79. }
  80. }
  81. static void sas_phy_control(struct sas_ha_struct *sas_ha, u8 phy_id,
  82. u8 phy_op, enum sas_linkrate min,
  83. enum sas_linkrate max, u8 *resp_data)
  84. {
  85. struct sas_internal *i =
  86. to_sas_internal(sas_ha->core.shost->transportt);
  87. struct sas_phy_linkrates rates;
  88. if (phy_id >= sas_ha->num_phys) {
  89. resp_data[2] = SMP_RESP_NO_PHY;
  90. return;
  91. }
  92. switch (phy_op) {
  93. case PHY_FUNC_NOP:
  94. case PHY_FUNC_LINK_RESET:
  95. case PHY_FUNC_HARD_RESET:
  96. case PHY_FUNC_DISABLE:
  97. case PHY_FUNC_CLEAR_ERROR_LOG:
  98. case PHY_FUNC_CLEAR_AFFIL:
  99. case PHY_FUNC_TX_SATA_PS_SIGNAL:
  100. break;
  101. default:
  102. resp_data[2] = SMP_RESP_PHY_UNK_OP;
  103. return;
  104. }
  105. rates.minimum_linkrate = min;
  106. rates.maximum_linkrate = max;
  107. if (i->dft->lldd_control_phy(sas_ha->sas_phy[phy_id], phy_op, &rates))
  108. resp_data[2] = SMP_RESP_FUNC_FAILED;
  109. else
  110. resp_data[2] = SMP_RESP_FUNC_ACC;
  111. }
  112. int sas_smp_host_handler(struct Scsi_Host *shost, struct request *req,
  113. struct request *rsp)
  114. {
  115. u8 *req_data = NULL, *resp_data = NULL, *buf;
  116. struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost);
  117. int error = -EINVAL;
  118. /* eight is the minimum size for request and response frames */
  119. if (blk_rq_bytes(req) < 8 || blk_rq_bytes(rsp) < 8)
  120. goto out;
  121. if (bio_offset(req->bio) + blk_rq_bytes(req) > PAGE_SIZE ||
  122. bio_offset(rsp->bio) + blk_rq_bytes(rsp) > PAGE_SIZE) {
  123. shost_printk(KERN_ERR, shost,
  124. "SMP request/response frame crosses page boundary");
  125. goto out;
  126. }
  127. req_data = kzalloc(blk_rq_bytes(req), GFP_KERNEL);
  128. /* make sure frame can always be built ... we copy
  129. * back only the requested length */
  130. resp_data = kzalloc(max(blk_rq_bytes(rsp), 128U), GFP_KERNEL);
  131. if (!req_data || !resp_data) {
  132. error = -ENOMEM;
  133. goto out;
  134. }
  135. local_irq_disable();
  136. buf = kmap_atomic(bio_page(req->bio), KM_USER0) + bio_offset(req->bio);
  137. memcpy(req_data, buf, blk_rq_bytes(req));
  138. kunmap_atomic(buf - bio_offset(req->bio), KM_USER0);
  139. local_irq_enable();
  140. if (req_data[0] != SMP_REQUEST)
  141. goto out;
  142. /* always succeeds ... even if we can't process the request
  143. * the result is in the response frame */
  144. error = 0;
  145. /* set up default don't know response */
  146. resp_data[0] = SMP_RESPONSE;
  147. resp_data[1] = req_data[1];
  148. resp_data[2] = SMP_RESP_FUNC_UNK;
  149. switch (req_data[1]) {
  150. case SMP_REPORT_GENERAL:
  151. req->resid_len -= 8;
  152. rsp->resid_len -= 32;
  153. resp_data[2] = SMP_RESP_FUNC_ACC;
  154. resp_data[9] = sas_ha->num_phys;
  155. break;
  156. case SMP_REPORT_MANUF_INFO:
  157. req->resid_len -= 8;
  158. rsp->resid_len -= 64;
  159. resp_data[2] = SMP_RESP_FUNC_ACC;
  160. memcpy(resp_data + 12, shost->hostt->name,
  161. SAS_EXPANDER_VENDOR_ID_LEN);
  162. memcpy(resp_data + 20, "libsas virt phy",
  163. SAS_EXPANDER_PRODUCT_ID_LEN);
  164. break;
  165. case SMP_READ_GPIO_REG:
  166. /* FIXME: need GPIO support in the transport class */
  167. break;
  168. case SMP_DISCOVER:
  169. req->resid_len -= 16;
  170. if ((int)req->resid_len < 0) {
  171. req->resid_len = 0;
  172. error = -EINVAL;
  173. goto out;
  174. }
  175. rsp->resid_len -= 56;
  176. sas_host_smp_discover(sas_ha, resp_data, req_data[9]);
  177. break;
  178. case SMP_REPORT_PHY_ERR_LOG:
  179. /* FIXME: could implement this with additional
  180. * libsas callbacks providing the HW supports it */
  181. break;
  182. case SMP_REPORT_PHY_SATA:
  183. req->resid_len -= 16;
  184. if ((int)req->resid_len < 0) {
  185. req->resid_len = 0;
  186. error = -EINVAL;
  187. goto out;
  188. }
  189. rsp->resid_len -= 60;
  190. sas_report_phy_sata(sas_ha, resp_data, req_data[9]);
  191. break;
  192. case SMP_REPORT_ROUTE_INFO:
  193. /* Can't implement; hosts have no routes */
  194. break;
  195. case SMP_WRITE_GPIO_REG:
  196. /* FIXME: need GPIO support in the transport class */
  197. break;
  198. case SMP_CONF_ROUTE_INFO:
  199. /* Can't implement; hosts have no routes */
  200. break;
  201. case SMP_PHY_CONTROL:
  202. req->resid_len -= 44;
  203. if ((int)req->resid_len < 0) {
  204. req->resid_len = 0;
  205. error = -EINVAL;
  206. goto out;
  207. }
  208. rsp->resid_len -= 8;
  209. sas_phy_control(sas_ha, req_data[9], req_data[10],
  210. req_data[32] >> 4, req_data[33] >> 4,
  211. resp_data);
  212. break;
  213. case SMP_PHY_TEST_FUNCTION:
  214. /* FIXME: should this be implemented? */
  215. break;
  216. default:
  217. /* probably a 2.0 function */
  218. break;
  219. }
  220. local_irq_disable();
  221. buf = kmap_atomic(bio_page(rsp->bio), KM_USER0) + bio_offset(rsp->bio);
  222. memcpy(buf, resp_data, blk_rq_bytes(rsp));
  223. flush_kernel_dcache_page(bio_page(rsp->bio));
  224. kunmap_atomic(buf - bio_offset(rsp->bio), KM_USER0);
  225. local_irq_enable();
  226. out:
  227. kfree(req_data);
  228. kfree(resp_data);
  229. return error;
  230. }