qib_ruc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/spinlock.h>
  34. #include <rdma/ib_smi.h>
  35. #include "qib.h"
  36. #include "qib_mad.h"
  37. /*
  38. * Switch to alternate path.
  39. * The QP s_lock should be held and interrupts disabled.
  40. */
  41. void qib_migrate_qp(struct rvt_qp *qp)
  42. {
  43. struct ib_event ev;
  44. qp->s_mig_state = IB_MIG_MIGRATED;
  45. qp->remote_ah_attr = qp->alt_ah_attr;
  46. qp->port_num = rdma_ah_get_port_num(&qp->alt_ah_attr);
  47. qp->s_pkey_index = qp->s_alt_pkey_index;
  48. ev.device = qp->ibqp.device;
  49. ev.element.qp = &qp->ibqp;
  50. ev.event = IB_EVENT_PATH_MIG;
  51. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  52. }
  53. static __be64 get_sguid(struct qib_ibport *ibp, unsigned index)
  54. {
  55. if (!index) {
  56. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  57. return ppd->guid;
  58. }
  59. return ibp->guids[index - 1];
  60. }
  61. static int gid_ok(union ib_gid *gid, __be64 gid_prefix, __be64 id)
  62. {
  63. return (gid->global.interface_id == id &&
  64. (gid->global.subnet_prefix == gid_prefix ||
  65. gid->global.subnet_prefix == IB_DEFAULT_GID_PREFIX));
  66. }
  67. /*
  68. *
  69. * This should be called with the QP r_lock held.
  70. *
  71. * The s_lock will be acquired around the qib_migrate_qp() call.
  72. */
  73. int qib_ruc_check_hdr(struct qib_ibport *ibp, struct ib_header *hdr,
  74. int has_grh, struct rvt_qp *qp, u32 bth0)
  75. {
  76. __be64 guid;
  77. unsigned long flags;
  78. if (qp->s_mig_state == IB_MIG_ARMED && (bth0 & IB_BTH_MIG_REQ)) {
  79. if (!has_grh) {
  80. if (rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  81. IB_AH_GRH)
  82. goto err;
  83. } else {
  84. const struct ib_global_route *grh;
  85. if (!(rdma_ah_get_ah_flags(&qp->alt_ah_attr) &
  86. IB_AH_GRH))
  87. goto err;
  88. grh = rdma_ah_read_grh(&qp->alt_ah_attr);
  89. guid = get_sguid(ibp, grh->sgid_index);
  90. if (!gid_ok(&hdr->u.l.grh.dgid,
  91. ibp->rvp.gid_prefix, guid))
  92. goto err;
  93. if (!gid_ok(&hdr->u.l.grh.sgid,
  94. grh->dgid.global.subnet_prefix,
  95. grh->dgid.global.interface_id))
  96. goto err;
  97. }
  98. if (!qib_pkey_ok((u16)bth0,
  99. qib_get_pkey(ibp, qp->s_alt_pkey_index))) {
  100. qib_bad_pkey(ibp,
  101. (u16)bth0,
  102. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  103. 0, qp->ibqp.qp_num,
  104. hdr->lrh[3], hdr->lrh[1]);
  105. goto err;
  106. }
  107. /* Validate the SLID. See Ch. 9.6.1.5 and 17.2.8 */
  108. if ((be16_to_cpu(hdr->lrh[3]) !=
  109. rdma_ah_get_dlid(&qp->alt_ah_attr)) ||
  110. ppd_from_ibp(ibp)->port !=
  111. rdma_ah_get_port_num(&qp->alt_ah_attr))
  112. goto err;
  113. spin_lock_irqsave(&qp->s_lock, flags);
  114. qib_migrate_qp(qp);
  115. spin_unlock_irqrestore(&qp->s_lock, flags);
  116. } else {
  117. if (!has_grh) {
  118. if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  119. IB_AH_GRH)
  120. goto err;
  121. } else {
  122. const struct ib_global_route *grh;
  123. if (!(rdma_ah_get_ah_flags(&qp->remote_ah_attr) &
  124. IB_AH_GRH))
  125. goto err;
  126. grh = rdma_ah_read_grh(&qp->remote_ah_attr);
  127. guid = get_sguid(ibp, grh->sgid_index);
  128. if (!gid_ok(&hdr->u.l.grh.dgid,
  129. ibp->rvp.gid_prefix, guid))
  130. goto err;
  131. if (!gid_ok(&hdr->u.l.grh.sgid,
  132. grh->dgid.global.subnet_prefix,
  133. grh->dgid.global.interface_id))
  134. goto err;
  135. }
  136. if (!qib_pkey_ok((u16)bth0,
  137. qib_get_pkey(ibp, qp->s_pkey_index))) {
  138. qib_bad_pkey(ibp,
  139. (u16)bth0,
  140. (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
  141. 0, qp->ibqp.qp_num,
  142. hdr->lrh[3], hdr->lrh[1]);
  143. goto err;
  144. }
  145. /* Validate the SLID. See Ch. 9.6.1.5 */
  146. if (be16_to_cpu(hdr->lrh[3]) !=
  147. rdma_ah_get_dlid(&qp->remote_ah_attr) ||
  148. ppd_from_ibp(ibp)->port != qp->port_num)
  149. goto err;
  150. if (qp->s_mig_state == IB_MIG_REARM &&
  151. !(bth0 & IB_BTH_MIG_REQ))
  152. qp->s_mig_state = IB_MIG_ARMED;
  153. }
  154. return 0;
  155. err:
  156. return 1;
  157. }
  158. /**
  159. * qib_make_grh - construct a GRH header
  160. * @ibp: a pointer to the IB port
  161. * @hdr: a pointer to the GRH header being constructed
  162. * @grh: the global route address to send to
  163. * @hwords: the number of 32 bit words of header being sent
  164. * @nwords: the number of 32 bit words of data being sent
  165. *
  166. * Return the size of the header in 32 bit words.
  167. */
  168. u32 qib_make_grh(struct qib_ibport *ibp, struct ib_grh *hdr,
  169. const struct ib_global_route *grh, u32 hwords, u32 nwords)
  170. {
  171. hdr->version_tclass_flow =
  172. cpu_to_be32((IB_GRH_VERSION << IB_GRH_VERSION_SHIFT) |
  173. (grh->traffic_class << IB_GRH_TCLASS_SHIFT) |
  174. (grh->flow_label << IB_GRH_FLOW_SHIFT));
  175. hdr->paylen = cpu_to_be16((hwords - 2 + nwords + SIZE_OF_CRC) << 2);
  176. /* next_hdr is defined by C8-7 in ch. 8.4.1 */
  177. hdr->next_hdr = IB_GRH_NEXT_HDR;
  178. hdr->hop_limit = grh->hop_limit;
  179. /* The SGID is 32-bit aligned. */
  180. hdr->sgid.global.subnet_prefix = ibp->rvp.gid_prefix;
  181. if (!grh->sgid_index)
  182. hdr->sgid.global.interface_id = ppd_from_ibp(ibp)->guid;
  183. else if (grh->sgid_index < QIB_GUIDS_PER_PORT)
  184. hdr->sgid.global.interface_id = ibp->guids[grh->sgid_index - 1];
  185. hdr->dgid = grh->dgid;
  186. /* GRH header size in 32-bit words. */
  187. return sizeof(struct ib_grh) / sizeof(u32);
  188. }
  189. void qib_make_ruc_header(struct rvt_qp *qp, struct ib_other_headers *ohdr,
  190. u32 bth0, u32 bth2)
  191. {
  192. struct qib_qp_priv *priv = qp->priv;
  193. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  194. u16 lrh0;
  195. u32 nwords;
  196. u32 extra_bytes;
  197. /* Construct the header. */
  198. extra_bytes = -qp->s_cur_size & 3;
  199. nwords = (qp->s_cur_size + extra_bytes) >> 2;
  200. lrh0 = QIB_LRH_BTH;
  201. if (unlikely(rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)) {
  202. qp->s_hdrwords +=
  203. qib_make_grh(ibp, &priv->s_hdr->u.l.grh,
  204. rdma_ah_read_grh(&qp->remote_ah_attr),
  205. qp->s_hdrwords, nwords);
  206. lrh0 = QIB_LRH_GRH;
  207. }
  208. lrh0 |= ibp->sl_to_vl[rdma_ah_get_sl(&qp->remote_ah_attr)] << 12 |
  209. rdma_ah_get_sl(&qp->remote_ah_attr) << 4;
  210. priv->s_hdr->lrh[0] = cpu_to_be16(lrh0);
  211. priv->s_hdr->lrh[1] =
  212. cpu_to_be16(rdma_ah_get_dlid(&qp->remote_ah_attr));
  213. priv->s_hdr->lrh[2] =
  214. cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
  215. priv->s_hdr->lrh[3] =
  216. cpu_to_be16(ppd_from_ibp(ibp)->lid |
  217. rdma_ah_get_path_bits(&qp->remote_ah_attr));
  218. bth0 |= qib_get_pkey(ibp, qp->s_pkey_index);
  219. bth0 |= extra_bytes << 20;
  220. if (qp->s_mig_state == IB_MIG_MIGRATED)
  221. bth0 |= IB_BTH_MIG_REQ;
  222. ohdr->bth[0] = cpu_to_be32(bth0);
  223. ohdr->bth[1] = cpu_to_be32(qp->remote_qpn);
  224. ohdr->bth[2] = cpu_to_be32(bth2);
  225. this_cpu_inc(ibp->pmastats->n_unicast_xmit);
  226. }
  227. void _qib_do_send(struct work_struct *work)
  228. {
  229. struct qib_qp_priv *priv = container_of(work, struct qib_qp_priv,
  230. s_work);
  231. struct rvt_qp *qp = priv->owner;
  232. qib_do_send(qp);
  233. }
  234. /**
  235. * qib_do_send - perform a send on a QP
  236. * @qp: pointer to the QP
  237. *
  238. * Process entries in the send work queue until credit or queue is
  239. * exhausted. Only allow one CPU to send a packet per QP (tasklet).
  240. * Otherwise, two threads could send packets out of order.
  241. */
  242. void qib_do_send(struct rvt_qp *qp)
  243. {
  244. struct qib_qp_priv *priv = qp->priv;
  245. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  246. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  247. int (*make_req)(struct rvt_qp *qp, unsigned long *flags);
  248. unsigned long flags;
  249. if ((qp->ibqp.qp_type == IB_QPT_RC ||
  250. qp->ibqp.qp_type == IB_QPT_UC) &&
  251. (rdma_ah_get_dlid(&qp->remote_ah_attr) &
  252. ~((1 << ppd->lmc) - 1)) == ppd->lid) {
  253. rvt_ruc_loopback(qp);
  254. return;
  255. }
  256. if (qp->ibqp.qp_type == IB_QPT_RC)
  257. make_req = qib_make_rc_req;
  258. else if (qp->ibqp.qp_type == IB_QPT_UC)
  259. make_req = qib_make_uc_req;
  260. else
  261. make_req = qib_make_ud_req;
  262. spin_lock_irqsave(&qp->s_lock, flags);
  263. /* Return if we are already busy processing a work request. */
  264. if (!qib_send_ok(qp)) {
  265. spin_unlock_irqrestore(&qp->s_lock, flags);
  266. return;
  267. }
  268. qp->s_flags |= RVT_S_BUSY;
  269. do {
  270. /* Check for a constructed packet to be sent. */
  271. if (qp->s_hdrwords != 0) {
  272. spin_unlock_irqrestore(&qp->s_lock, flags);
  273. /*
  274. * If the packet cannot be sent now, return and
  275. * the send tasklet will be woken up later.
  276. */
  277. if (qib_verbs_send(qp, priv->s_hdr, qp->s_hdrwords,
  278. qp->s_cur_sge, qp->s_cur_size))
  279. return;
  280. /* Record that s_hdr is empty. */
  281. qp->s_hdrwords = 0;
  282. spin_lock_irqsave(&qp->s_lock, flags);
  283. }
  284. } while (make_req(qp, &flags));
  285. spin_unlock_irqrestore(&qp->s_lock, flags);
  286. }