sas_host_smp.c 6.8 KB

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