ib_cm.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/in.h>
  35. #include <linux/slab.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/ratelimit.h>
  38. #include "rds_single_path.h"
  39. #include "rds.h"
  40. #include "ib.h"
  41. /*
  42. * Set the selected protocol version
  43. */
  44. static void rds_ib_set_protocol(struct rds_connection *conn, unsigned int version)
  45. {
  46. conn->c_version = version;
  47. }
  48. /*
  49. * Set up flow control
  50. */
  51. static void rds_ib_set_flow_control(struct rds_connection *conn, u32 credits)
  52. {
  53. struct rds_ib_connection *ic = conn->c_transport_data;
  54. if (rds_ib_sysctl_flow_control && credits != 0) {
  55. /* We're doing flow control */
  56. ic->i_flowctl = 1;
  57. rds_ib_send_add_credits(conn, credits);
  58. } else {
  59. ic->i_flowctl = 0;
  60. }
  61. }
  62. /*
  63. * Tune RNR behavior. Without flow control, we use a rather
  64. * low timeout, but not the absolute minimum - this should
  65. * be tunable.
  66. *
  67. * We already set the RNR retry count to 7 (which is the
  68. * smallest infinite number :-) above.
  69. * If flow control is off, we want to change this back to 0
  70. * so that we learn quickly when our credit accounting is
  71. * buggy.
  72. *
  73. * Caller passes in a qp_attr pointer - don't waste stack spacv
  74. * by allocation this twice.
  75. */
  76. static void
  77. rds_ib_tune_rnr(struct rds_ib_connection *ic, struct ib_qp_attr *attr)
  78. {
  79. int ret;
  80. attr->min_rnr_timer = IB_RNR_TIMER_000_32;
  81. ret = ib_modify_qp(ic->i_cm_id->qp, attr, IB_QP_MIN_RNR_TIMER);
  82. if (ret)
  83. printk(KERN_NOTICE "ib_modify_qp(IB_QP_MIN_RNR_TIMER): err=%d\n", -ret);
  84. }
  85. /*
  86. * Connection established.
  87. * We get here for both outgoing and incoming connection.
  88. */
  89. void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
  90. {
  91. const struct rds_ib_connect_private *dp = NULL;
  92. struct rds_ib_connection *ic = conn->c_transport_data;
  93. struct ib_qp_attr qp_attr;
  94. int err;
  95. if (event->param.conn.private_data_len >= sizeof(*dp)) {
  96. dp = event->param.conn.private_data;
  97. /* make sure it isn't empty data */
  98. if (dp->dp_protocol_major) {
  99. rds_ib_set_protocol(conn,
  100. RDS_PROTOCOL(dp->dp_protocol_major,
  101. dp->dp_protocol_minor));
  102. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  103. }
  104. }
  105. if (conn->c_version < RDS_PROTOCOL(3, 1)) {
  106. pr_notice("RDS/IB: Connection <%pI4,%pI4> version %u.%u no longer supported\n",
  107. &conn->c_laddr, &conn->c_faddr,
  108. RDS_PROTOCOL_MAJOR(conn->c_version),
  109. RDS_PROTOCOL_MINOR(conn->c_version));
  110. set_bit(RDS_DESTROY_PENDING, &conn->c_path[0].cp_flags);
  111. rds_conn_destroy(conn);
  112. return;
  113. } else {
  114. pr_notice("RDS/IB: %s conn connected <%pI4,%pI4> version %u.%u%s\n",
  115. ic->i_active_side ? "Active" : "Passive",
  116. &conn->c_laddr, &conn->c_faddr,
  117. RDS_PROTOCOL_MAJOR(conn->c_version),
  118. RDS_PROTOCOL_MINOR(conn->c_version),
  119. ic->i_flowctl ? ", flow control" : "");
  120. }
  121. atomic_set(&ic->i_cq_quiesce, 0);
  122. /* Init rings and fill recv. this needs to wait until protocol
  123. * negotiation is complete, since ring layout is different
  124. * from 3.1 to 4.1.
  125. */
  126. rds_ib_send_init_ring(ic);
  127. rds_ib_recv_init_ring(ic);
  128. /* Post receive buffers - as a side effect, this will update
  129. * the posted credit count. */
  130. rds_ib_recv_refill(conn, 1, GFP_KERNEL);
  131. /* Tune RNR behavior */
  132. rds_ib_tune_rnr(ic, &qp_attr);
  133. qp_attr.qp_state = IB_QPS_RTS;
  134. err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
  135. if (err)
  136. printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err);
  137. /* update ib_device with this local ipaddr */
  138. err = rds_ib_update_ipaddr(ic->rds_ibdev, conn->c_laddr);
  139. if (err)
  140. printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n",
  141. err);
  142. /* If the peer gave us the last packet it saw, process this as if
  143. * we had received a regular ACK. */
  144. if (dp) {
  145. /* dp structure start is not guaranteed to be 8 bytes aligned.
  146. * Since dp_ack_seq is 64-bit extended load operations can be
  147. * used so go through get_unaligned to avoid unaligned errors.
  148. */
  149. __be64 dp_ack_seq = get_unaligned(&dp->dp_ack_seq);
  150. if (dp_ack_seq)
  151. rds_send_drop_acked(conn, be64_to_cpu(dp_ack_seq),
  152. NULL);
  153. }
  154. rds_connect_complete(conn);
  155. }
  156. static void rds_ib_cm_fill_conn_param(struct rds_connection *conn,
  157. struct rdma_conn_param *conn_param,
  158. struct rds_ib_connect_private *dp,
  159. u32 protocol_version,
  160. u32 max_responder_resources,
  161. u32 max_initiator_depth)
  162. {
  163. struct rds_ib_connection *ic = conn->c_transport_data;
  164. struct rds_ib_device *rds_ibdev = ic->rds_ibdev;
  165. memset(conn_param, 0, sizeof(struct rdma_conn_param));
  166. conn_param->responder_resources =
  167. min_t(u32, rds_ibdev->max_responder_resources, max_responder_resources);
  168. conn_param->initiator_depth =
  169. min_t(u32, rds_ibdev->max_initiator_depth, max_initiator_depth);
  170. conn_param->retry_count = min_t(unsigned int, rds_ib_retry_count, 7);
  171. conn_param->rnr_retry_count = 7;
  172. if (dp) {
  173. memset(dp, 0, sizeof(*dp));
  174. dp->dp_saddr = conn->c_laddr;
  175. dp->dp_daddr = conn->c_faddr;
  176. dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version);
  177. dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version);
  178. dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS);
  179. dp->dp_ack_seq = cpu_to_be64(rds_ib_piggyb_ack(ic));
  180. /* Advertise flow control */
  181. if (ic->i_flowctl) {
  182. unsigned int credits;
  183. credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits));
  184. dp->dp_credit = cpu_to_be32(credits);
  185. atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits);
  186. }
  187. conn_param->private_data = dp;
  188. conn_param->private_data_len = sizeof(*dp);
  189. }
  190. }
  191. static void rds_ib_cq_event_handler(struct ib_event *event, void *data)
  192. {
  193. rdsdebug("event %u (%s) data %p\n",
  194. event->event, ib_event_msg(event->event), data);
  195. }
  196. /* Plucking the oldest entry from the ring can be done concurrently with
  197. * the thread refilling the ring. Each ring operation is protected by
  198. * spinlocks and the transient state of refilling doesn't change the
  199. * recording of which entry is oldest.
  200. *
  201. * This relies on IB only calling one cq comp_handler for each cq so that
  202. * there will only be one caller of rds_recv_incoming() per RDS connection.
  203. */
  204. static void rds_ib_cq_comp_handler_recv(struct ib_cq *cq, void *context)
  205. {
  206. struct rds_connection *conn = context;
  207. struct rds_ib_connection *ic = conn->c_transport_data;
  208. rdsdebug("conn %p cq %p\n", conn, cq);
  209. rds_ib_stats_inc(s_ib_evt_handler_call);
  210. tasklet_schedule(&ic->i_recv_tasklet);
  211. }
  212. static void poll_scq(struct rds_ib_connection *ic, struct ib_cq *cq,
  213. struct ib_wc *wcs)
  214. {
  215. int nr, i;
  216. struct ib_wc *wc;
  217. while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) {
  218. for (i = 0; i < nr; i++) {
  219. wc = wcs + i;
  220. rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
  221. (unsigned long long)wc->wr_id, wc->status,
  222. wc->byte_len, be32_to_cpu(wc->ex.imm_data));
  223. if (wc->wr_id <= ic->i_send_ring.w_nr ||
  224. wc->wr_id == RDS_IB_ACK_WR_ID)
  225. rds_ib_send_cqe_handler(ic, wc);
  226. else
  227. rds_ib_mr_cqe_handler(ic, wc);
  228. }
  229. }
  230. }
  231. static void rds_ib_tasklet_fn_send(unsigned long data)
  232. {
  233. struct rds_ib_connection *ic = (struct rds_ib_connection *)data;
  234. struct rds_connection *conn = ic->conn;
  235. rds_ib_stats_inc(s_ib_tasklet_call);
  236. /* if cq has been already reaped, ignore incoming cq event */
  237. if (atomic_read(&ic->i_cq_quiesce))
  238. return;
  239. poll_scq(ic, ic->i_send_cq, ic->i_send_wc);
  240. ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
  241. poll_scq(ic, ic->i_send_cq, ic->i_send_wc);
  242. if (rds_conn_up(conn) &&
  243. (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags) ||
  244. test_bit(0, &conn->c_map_queued)))
  245. rds_send_xmit(&ic->conn->c_path[0]);
  246. }
  247. static void poll_rcq(struct rds_ib_connection *ic, struct ib_cq *cq,
  248. struct ib_wc *wcs,
  249. struct rds_ib_ack_state *ack_state)
  250. {
  251. int nr, i;
  252. struct ib_wc *wc;
  253. while ((nr = ib_poll_cq(cq, RDS_IB_WC_MAX, wcs)) > 0) {
  254. for (i = 0; i < nr; i++) {
  255. wc = wcs + i;
  256. rdsdebug("wc wr_id 0x%llx status %u byte_len %u imm_data %u\n",
  257. (unsigned long long)wc->wr_id, wc->status,
  258. wc->byte_len, be32_to_cpu(wc->ex.imm_data));
  259. rds_ib_recv_cqe_handler(ic, wc, ack_state);
  260. }
  261. }
  262. }
  263. static void rds_ib_tasklet_fn_recv(unsigned long data)
  264. {
  265. struct rds_ib_connection *ic = (struct rds_ib_connection *)data;
  266. struct rds_connection *conn = ic->conn;
  267. struct rds_ib_device *rds_ibdev = ic->rds_ibdev;
  268. struct rds_ib_ack_state state;
  269. if (!rds_ibdev)
  270. rds_conn_drop(conn);
  271. rds_ib_stats_inc(s_ib_tasklet_call);
  272. /* if cq has been already reaped, ignore incoming cq event */
  273. if (atomic_read(&ic->i_cq_quiesce))
  274. return;
  275. memset(&state, 0, sizeof(state));
  276. poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state);
  277. ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
  278. poll_rcq(ic, ic->i_recv_cq, ic->i_recv_wc, &state);
  279. if (state.ack_next_valid)
  280. rds_ib_set_ack(ic, state.ack_next, state.ack_required);
  281. if (state.ack_recv_valid && state.ack_recv > ic->i_ack_recv) {
  282. rds_send_drop_acked(conn, state.ack_recv, NULL);
  283. ic->i_ack_recv = state.ack_recv;
  284. }
  285. if (rds_conn_up(conn))
  286. rds_ib_attempt_ack(ic);
  287. }
  288. static void rds_ib_qp_event_handler(struct ib_event *event, void *data)
  289. {
  290. struct rds_connection *conn = data;
  291. struct rds_ib_connection *ic = conn->c_transport_data;
  292. rdsdebug("conn %p ic %p event %u (%s)\n", conn, ic, event->event,
  293. ib_event_msg(event->event));
  294. switch (event->event) {
  295. case IB_EVENT_COMM_EST:
  296. rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST);
  297. break;
  298. default:
  299. rdsdebug("Fatal QP Event %u (%s) "
  300. "- connection %pI4->%pI4, reconnecting\n",
  301. event->event, ib_event_msg(event->event),
  302. &conn->c_laddr, &conn->c_faddr);
  303. rds_conn_drop(conn);
  304. break;
  305. }
  306. }
  307. static void rds_ib_cq_comp_handler_send(struct ib_cq *cq, void *context)
  308. {
  309. struct rds_connection *conn = context;
  310. struct rds_ib_connection *ic = conn->c_transport_data;
  311. rdsdebug("conn %p cq %p\n", conn, cq);
  312. rds_ib_stats_inc(s_ib_evt_handler_call);
  313. tasklet_schedule(&ic->i_send_tasklet);
  314. }
  315. static inline int ibdev_get_unused_vector(struct rds_ib_device *rds_ibdev)
  316. {
  317. int min = rds_ibdev->vector_load[rds_ibdev->dev->num_comp_vectors - 1];
  318. int index = rds_ibdev->dev->num_comp_vectors - 1;
  319. int i;
  320. for (i = rds_ibdev->dev->num_comp_vectors - 1; i >= 0; i--) {
  321. if (rds_ibdev->vector_load[i] < min) {
  322. index = i;
  323. min = rds_ibdev->vector_load[i];
  324. }
  325. }
  326. rds_ibdev->vector_load[index]++;
  327. return index;
  328. }
  329. static inline void ibdev_put_vector(struct rds_ib_device *rds_ibdev, int index)
  330. {
  331. rds_ibdev->vector_load[index]--;
  332. }
  333. /*
  334. * This needs to be very careful to not leave IS_ERR pointers around for
  335. * cleanup to trip over.
  336. */
  337. static int rds_ib_setup_qp(struct rds_connection *conn)
  338. {
  339. struct rds_ib_connection *ic = conn->c_transport_data;
  340. struct ib_device *dev = ic->i_cm_id->device;
  341. struct ib_qp_init_attr attr;
  342. struct ib_cq_init_attr cq_attr = {};
  343. struct rds_ib_device *rds_ibdev;
  344. int ret, fr_queue_space;
  345. /*
  346. * It's normal to see a null device if an incoming connection races
  347. * with device removal, so we don't print a warning.
  348. */
  349. rds_ibdev = rds_ib_get_client_data(dev);
  350. if (!rds_ibdev)
  351. return -EOPNOTSUPP;
  352. /* The fr_queue_space is currently set to 512, to add extra space on
  353. * completion queue and send queue. This extra space is used for FRMR
  354. * registration and invalidation work requests
  355. */
  356. fr_queue_space = rds_ibdev->use_fastreg ?
  357. (RDS_IB_DEFAULT_FR_WR + 1) +
  358. (RDS_IB_DEFAULT_FR_INV_WR + 1)
  359. : 0;
  360. /* add the conn now so that connection establishment has the dev */
  361. rds_ib_add_conn(rds_ibdev, conn);
  362. if (rds_ibdev->max_wrs < ic->i_send_ring.w_nr + 1)
  363. rds_ib_ring_resize(&ic->i_send_ring, rds_ibdev->max_wrs - 1);
  364. if (rds_ibdev->max_wrs < ic->i_recv_ring.w_nr + 1)
  365. rds_ib_ring_resize(&ic->i_recv_ring, rds_ibdev->max_wrs - 1);
  366. /* Protection domain and memory range */
  367. ic->i_pd = rds_ibdev->pd;
  368. ic->i_scq_vector = ibdev_get_unused_vector(rds_ibdev);
  369. cq_attr.cqe = ic->i_send_ring.w_nr + fr_queue_space + 1;
  370. cq_attr.comp_vector = ic->i_scq_vector;
  371. ic->i_send_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_send,
  372. rds_ib_cq_event_handler, conn,
  373. &cq_attr);
  374. if (IS_ERR(ic->i_send_cq)) {
  375. ret = PTR_ERR(ic->i_send_cq);
  376. ic->i_send_cq = NULL;
  377. ibdev_put_vector(rds_ibdev, ic->i_scq_vector);
  378. rdsdebug("ib_create_cq send failed: %d\n", ret);
  379. goto rds_ibdev_out;
  380. }
  381. ic->i_rcq_vector = ibdev_get_unused_vector(rds_ibdev);
  382. cq_attr.cqe = ic->i_recv_ring.w_nr;
  383. cq_attr.comp_vector = ic->i_rcq_vector;
  384. ic->i_recv_cq = ib_create_cq(dev, rds_ib_cq_comp_handler_recv,
  385. rds_ib_cq_event_handler, conn,
  386. &cq_attr);
  387. if (IS_ERR(ic->i_recv_cq)) {
  388. ret = PTR_ERR(ic->i_recv_cq);
  389. ic->i_recv_cq = NULL;
  390. ibdev_put_vector(rds_ibdev, ic->i_rcq_vector);
  391. rdsdebug("ib_create_cq recv failed: %d\n", ret);
  392. goto send_cq_out;
  393. }
  394. ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
  395. if (ret) {
  396. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  397. goto recv_cq_out;
  398. }
  399. ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
  400. if (ret) {
  401. rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
  402. goto recv_cq_out;
  403. }
  404. /* XXX negotiate max send/recv with remote? */
  405. memset(&attr, 0, sizeof(attr));
  406. attr.event_handler = rds_ib_qp_event_handler;
  407. attr.qp_context = conn;
  408. /* + 1 to allow for the single ack message */
  409. attr.cap.max_send_wr = ic->i_send_ring.w_nr + fr_queue_space + 1;
  410. attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1;
  411. attr.cap.max_send_sge = rds_ibdev->max_sge;
  412. attr.cap.max_recv_sge = RDS_IB_RECV_SGE;
  413. attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  414. attr.qp_type = IB_QPT_RC;
  415. attr.send_cq = ic->i_send_cq;
  416. attr.recv_cq = ic->i_recv_cq;
  417. atomic_set(&ic->i_fastreg_wrs, RDS_IB_DEFAULT_FR_WR);
  418. atomic_set(&ic->i_fastunreg_wrs, RDS_IB_DEFAULT_FR_INV_WR);
  419. /*
  420. * XXX this can fail if max_*_wr is too large? Are we supposed
  421. * to back off until we get a value that the hardware can support?
  422. */
  423. ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
  424. if (ret) {
  425. rdsdebug("rdma_create_qp failed: %d\n", ret);
  426. goto recv_cq_out;
  427. }
  428. ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
  429. ic->i_send_ring.w_nr *
  430. sizeof(struct rds_header),
  431. &ic->i_send_hdrs_dma, GFP_KERNEL);
  432. if (!ic->i_send_hdrs) {
  433. ret = -ENOMEM;
  434. rdsdebug("ib_dma_alloc_coherent send failed\n");
  435. goto qp_out;
  436. }
  437. ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
  438. ic->i_recv_ring.w_nr *
  439. sizeof(struct rds_header),
  440. &ic->i_recv_hdrs_dma, GFP_KERNEL);
  441. if (!ic->i_recv_hdrs) {
  442. ret = -ENOMEM;
  443. rdsdebug("ib_dma_alloc_coherent recv failed\n");
  444. goto send_hdrs_dma_out;
  445. }
  446. ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
  447. &ic->i_ack_dma, GFP_KERNEL);
  448. if (!ic->i_ack) {
  449. ret = -ENOMEM;
  450. rdsdebug("ib_dma_alloc_coherent ack failed\n");
  451. goto recv_hdrs_dma_out;
  452. }
  453. ic->i_sends = vzalloc_node(array_size(sizeof(struct rds_ib_send_work),
  454. ic->i_send_ring.w_nr),
  455. ibdev_to_node(dev));
  456. if (!ic->i_sends) {
  457. ret = -ENOMEM;
  458. rdsdebug("send allocation failed\n");
  459. goto ack_dma_out;
  460. }
  461. ic->i_recvs = vzalloc_node(array_size(sizeof(struct rds_ib_recv_work),
  462. ic->i_recv_ring.w_nr),
  463. ibdev_to_node(dev));
  464. if (!ic->i_recvs) {
  465. ret = -ENOMEM;
  466. rdsdebug("recv allocation failed\n");
  467. goto sends_out;
  468. }
  469. rds_ib_recv_init_ack(ic);
  470. rdsdebug("conn %p pd %p cq %p %p\n", conn, ic->i_pd,
  471. ic->i_send_cq, ic->i_recv_cq);
  472. goto out;
  473. sends_out:
  474. vfree(ic->i_sends);
  475. ack_dma_out:
  476. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  477. ic->i_ack, ic->i_ack_dma);
  478. recv_hdrs_dma_out:
  479. ib_dma_free_coherent(dev, ic->i_recv_ring.w_nr *
  480. sizeof(struct rds_header),
  481. ic->i_recv_hdrs, ic->i_recv_hdrs_dma);
  482. send_hdrs_dma_out:
  483. ib_dma_free_coherent(dev, ic->i_send_ring.w_nr *
  484. sizeof(struct rds_header),
  485. ic->i_send_hdrs, ic->i_send_hdrs_dma);
  486. qp_out:
  487. rdma_destroy_qp(ic->i_cm_id);
  488. recv_cq_out:
  489. if (!ib_destroy_cq(ic->i_recv_cq))
  490. ic->i_recv_cq = NULL;
  491. send_cq_out:
  492. if (!ib_destroy_cq(ic->i_send_cq))
  493. ic->i_send_cq = NULL;
  494. rds_ibdev_out:
  495. rds_ib_remove_conn(rds_ibdev, conn);
  496. out:
  497. rds_ib_dev_put(rds_ibdev);
  498. return ret;
  499. }
  500. static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event)
  501. {
  502. const struct rds_ib_connect_private *dp = event->param.conn.private_data;
  503. u16 common;
  504. u32 version = 0;
  505. /*
  506. * rdma_cm private data is odd - when there is any private data in the
  507. * request, we will be given a pretty large buffer without telling us the
  508. * original size. The only way to tell the difference is by looking at
  509. * the contents, which are initialized to zero.
  510. * If the protocol version fields aren't set, this is a connection attempt
  511. * from an older version. This could could be 3.0 or 2.0 - we can't tell.
  512. * We really should have changed this for OFED 1.3 :-(
  513. */
  514. /* Be paranoid. RDS always has privdata */
  515. if (!event->param.conn.private_data_len) {
  516. printk(KERN_NOTICE "RDS incoming connection has no private data, "
  517. "rejecting\n");
  518. return 0;
  519. }
  520. /* Even if len is crap *now* I still want to check it. -ASG */
  521. if (event->param.conn.private_data_len < sizeof (*dp) ||
  522. dp->dp_protocol_major == 0)
  523. return RDS_PROTOCOL_3_0;
  524. common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IB_SUPPORTED_PROTOCOLS;
  525. if (dp->dp_protocol_major == 3 && common) {
  526. version = RDS_PROTOCOL_3_0;
  527. while ((common >>= 1) != 0)
  528. version++;
  529. } else
  530. printk_ratelimited(KERN_NOTICE "RDS: Connection from %pI4 using incompatible protocol version %u.%u\n",
  531. &dp->dp_saddr,
  532. dp->dp_protocol_major,
  533. dp->dp_protocol_minor);
  534. return version;
  535. }
  536. int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
  537. struct rdma_cm_event *event)
  538. {
  539. __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id;
  540. __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id;
  541. const struct rds_ib_connect_private *dp = event->param.conn.private_data;
  542. struct rds_ib_connect_private dp_rep;
  543. struct rds_connection *conn = NULL;
  544. struct rds_ib_connection *ic = NULL;
  545. struct rdma_conn_param conn_param;
  546. u32 version;
  547. int err = 1, destroy = 1;
  548. /* Check whether the remote protocol version matches ours. */
  549. version = rds_ib_protocol_compatible(event);
  550. if (!version)
  551. goto out;
  552. rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u lguid 0x%llx fguid "
  553. "0x%llx\n", &dp->dp_saddr, &dp->dp_daddr,
  554. RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version),
  555. (unsigned long long)be64_to_cpu(lguid),
  556. (unsigned long long)be64_to_cpu(fguid));
  557. /* RDS/IB is not currently netns aware, thus init_net */
  558. conn = rds_conn_create(&init_net, dp->dp_daddr, dp->dp_saddr,
  559. &rds_ib_transport, GFP_KERNEL);
  560. if (IS_ERR(conn)) {
  561. rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
  562. conn = NULL;
  563. goto out;
  564. }
  565. /*
  566. * The connection request may occur while the
  567. * previous connection exist, e.g. in case of failover.
  568. * But as connections may be initiated simultaneously
  569. * by both hosts, we have a random backoff mechanism -
  570. * see the comment above rds_queue_reconnect()
  571. */
  572. mutex_lock(&conn->c_cm_lock);
  573. if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  574. if (rds_conn_state(conn) == RDS_CONN_UP) {
  575. rdsdebug("incoming connect while connecting\n");
  576. rds_conn_drop(conn);
  577. rds_ib_stats_inc(s_ib_listen_closed_stale);
  578. } else
  579. if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
  580. /* Wait and see - our connect may still be succeeding */
  581. rds_ib_stats_inc(s_ib_connect_raced);
  582. }
  583. goto out;
  584. }
  585. ic = conn->c_transport_data;
  586. rds_ib_set_protocol(conn, version);
  587. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  588. /* If the peer gave us the last packet it saw, process this as if
  589. * we had received a regular ACK. */
  590. if (dp->dp_ack_seq)
  591. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  592. BUG_ON(cm_id->context);
  593. BUG_ON(ic->i_cm_id);
  594. ic->i_cm_id = cm_id;
  595. cm_id->context = conn;
  596. /* We got halfway through setting up the ib_connection, if we
  597. * fail now, we have to take the long route out of this mess. */
  598. destroy = 0;
  599. err = rds_ib_setup_qp(conn);
  600. if (err) {
  601. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err);
  602. goto out;
  603. }
  604. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version,
  605. event->param.conn.responder_resources,
  606. event->param.conn.initiator_depth);
  607. /* rdma_accept() calls rdma_reject() internally if it fails */
  608. if (rdma_accept(cm_id, &conn_param))
  609. rds_ib_conn_error(conn, "rdma_accept failed\n");
  610. out:
  611. if (conn)
  612. mutex_unlock(&conn->c_cm_lock);
  613. if (err)
  614. rdma_reject(cm_id, NULL, 0);
  615. return destroy;
  616. }
  617. int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id)
  618. {
  619. struct rds_connection *conn = cm_id->context;
  620. struct rds_ib_connection *ic = conn->c_transport_data;
  621. struct rdma_conn_param conn_param;
  622. struct rds_ib_connect_private dp;
  623. int ret;
  624. /* If the peer doesn't do protocol negotiation, we must
  625. * default to RDSv3.0 */
  626. rds_ib_set_protocol(conn, RDS_PROTOCOL_3_0);
  627. ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */
  628. ret = rds_ib_setup_qp(conn);
  629. if (ret) {
  630. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret);
  631. goto out;
  632. }
  633. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION,
  634. UINT_MAX, UINT_MAX);
  635. ret = rdma_connect(cm_id, &conn_param);
  636. if (ret)
  637. rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
  638. out:
  639. /* Beware - returning non-zero tells the rdma_cm to destroy
  640. * the cm_id. We should certainly not do it as long as we still
  641. * "own" the cm_id. */
  642. if (ret) {
  643. if (ic->i_cm_id == cm_id)
  644. ret = 0;
  645. }
  646. ic->i_active_side = true;
  647. return ret;
  648. }
  649. int rds_ib_conn_path_connect(struct rds_conn_path *cp)
  650. {
  651. struct rds_connection *conn = cp->cp_conn;
  652. struct rds_ib_connection *ic = conn->c_transport_data;
  653. struct sockaddr_in src, dest;
  654. int ret;
  655. /* XXX I wonder what affect the port space has */
  656. /* delegate cm event handler to rdma_transport */
  657. ic->i_cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler, conn,
  658. RDMA_PS_TCP, IB_QPT_RC);
  659. if (IS_ERR(ic->i_cm_id)) {
  660. ret = PTR_ERR(ic->i_cm_id);
  661. ic->i_cm_id = NULL;
  662. rdsdebug("rdma_create_id() failed: %d\n", ret);
  663. goto out;
  664. }
  665. rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
  666. src.sin_family = AF_INET;
  667. src.sin_addr.s_addr = (__force u32)conn->c_laddr;
  668. src.sin_port = (__force u16)htons(0);
  669. dest.sin_family = AF_INET;
  670. dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
  671. dest.sin_port = (__force u16)htons(RDS_PORT);
  672. ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
  673. (struct sockaddr *)&dest,
  674. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  675. if (ret) {
  676. rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
  677. ret);
  678. rdma_destroy_id(ic->i_cm_id);
  679. ic->i_cm_id = NULL;
  680. }
  681. out:
  682. return ret;
  683. }
  684. /*
  685. * This is so careful about only cleaning up resources that were built up
  686. * so that it can be called at any point during startup. In fact it
  687. * can be called multiple times for a given connection.
  688. */
  689. void rds_ib_conn_path_shutdown(struct rds_conn_path *cp)
  690. {
  691. struct rds_connection *conn = cp->cp_conn;
  692. struct rds_ib_connection *ic = conn->c_transport_data;
  693. int err = 0;
  694. rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
  695. ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
  696. ic->i_cm_id ? ic->i_cm_id->qp : NULL);
  697. if (ic->i_cm_id) {
  698. struct ib_device *dev = ic->i_cm_id->device;
  699. rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
  700. err = rdma_disconnect(ic->i_cm_id);
  701. if (err) {
  702. /* Actually this may happen quite frequently, when
  703. * an outgoing connect raced with an incoming connect.
  704. */
  705. rdsdebug("failed to disconnect, cm: %p err %d\n",
  706. ic->i_cm_id, err);
  707. }
  708. /*
  709. * We want to wait for tx and rx completion to finish
  710. * before we tear down the connection, but we have to be
  711. * careful not to get stuck waiting on a send ring that
  712. * only has unsignaled sends in it. We've shutdown new
  713. * sends before getting here so by waiting for signaled
  714. * sends to complete we're ensured that there will be no
  715. * more tx processing.
  716. */
  717. wait_event(rds_ib_ring_empty_wait,
  718. rds_ib_ring_empty(&ic->i_recv_ring) &&
  719. (atomic_read(&ic->i_signaled_sends) == 0) &&
  720. (atomic_read(&ic->i_fastreg_wrs) == RDS_IB_DEFAULT_FR_WR) &&
  721. (atomic_read(&ic->i_fastunreg_wrs) == RDS_IB_DEFAULT_FR_INV_WR));
  722. tasklet_kill(&ic->i_send_tasklet);
  723. tasklet_kill(&ic->i_recv_tasklet);
  724. atomic_set(&ic->i_cq_quiesce, 1);
  725. /* first destroy the ib state that generates callbacks */
  726. if (ic->i_cm_id->qp)
  727. rdma_destroy_qp(ic->i_cm_id);
  728. if (ic->i_send_cq) {
  729. if (ic->rds_ibdev)
  730. ibdev_put_vector(ic->rds_ibdev, ic->i_scq_vector);
  731. ib_destroy_cq(ic->i_send_cq);
  732. }
  733. if (ic->i_recv_cq) {
  734. if (ic->rds_ibdev)
  735. ibdev_put_vector(ic->rds_ibdev, ic->i_rcq_vector);
  736. ib_destroy_cq(ic->i_recv_cq);
  737. }
  738. /* then free the resources that ib callbacks use */
  739. if (ic->i_send_hdrs)
  740. ib_dma_free_coherent(dev,
  741. ic->i_send_ring.w_nr *
  742. sizeof(struct rds_header),
  743. ic->i_send_hdrs,
  744. ic->i_send_hdrs_dma);
  745. if (ic->i_recv_hdrs)
  746. ib_dma_free_coherent(dev,
  747. ic->i_recv_ring.w_nr *
  748. sizeof(struct rds_header),
  749. ic->i_recv_hdrs,
  750. ic->i_recv_hdrs_dma);
  751. if (ic->i_ack)
  752. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  753. ic->i_ack, ic->i_ack_dma);
  754. if (ic->i_sends)
  755. rds_ib_send_clear_ring(ic);
  756. if (ic->i_recvs)
  757. rds_ib_recv_clear_ring(ic);
  758. rdma_destroy_id(ic->i_cm_id);
  759. /*
  760. * Move connection back to the nodev list.
  761. */
  762. if (ic->rds_ibdev)
  763. rds_ib_remove_conn(ic->rds_ibdev, conn);
  764. ic->i_cm_id = NULL;
  765. ic->i_pd = NULL;
  766. ic->i_send_cq = NULL;
  767. ic->i_recv_cq = NULL;
  768. ic->i_send_hdrs = NULL;
  769. ic->i_recv_hdrs = NULL;
  770. ic->i_ack = NULL;
  771. }
  772. BUG_ON(ic->rds_ibdev);
  773. /* Clear pending transmit */
  774. if (ic->i_data_op) {
  775. struct rds_message *rm;
  776. rm = container_of(ic->i_data_op, struct rds_message, data);
  777. rds_message_put(rm);
  778. ic->i_data_op = NULL;
  779. }
  780. /* Clear the ACK state */
  781. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  782. #ifdef KERNEL_HAS_ATOMIC64
  783. atomic64_set(&ic->i_ack_next, 0);
  784. #else
  785. ic->i_ack_next = 0;
  786. #endif
  787. ic->i_ack_recv = 0;
  788. /* Clear flow control state */
  789. ic->i_flowctl = 0;
  790. atomic_set(&ic->i_credits, 0);
  791. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  792. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  793. if (ic->i_ibinc) {
  794. rds_inc_put(&ic->i_ibinc->ii_inc);
  795. ic->i_ibinc = NULL;
  796. }
  797. vfree(ic->i_sends);
  798. ic->i_sends = NULL;
  799. vfree(ic->i_recvs);
  800. ic->i_recvs = NULL;
  801. ic->i_active_side = false;
  802. }
  803. int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp)
  804. {
  805. struct rds_ib_connection *ic;
  806. unsigned long flags;
  807. int ret;
  808. /* XXX too lazy? */
  809. ic = kzalloc(sizeof(struct rds_ib_connection), gfp);
  810. if (!ic)
  811. return -ENOMEM;
  812. ret = rds_ib_recv_alloc_caches(ic);
  813. if (ret) {
  814. kfree(ic);
  815. return ret;
  816. }
  817. INIT_LIST_HEAD(&ic->ib_node);
  818. tasklet_init(&ic->i_send_tasklet, rds_ib_tasklet_fn_send,
  819. (unsigned long)ic);
  820. tasklet_init(&ic->i_recv_tasklet, rds_ib_tasklet_fn_recv,
  821. (unsigned long)ic);
  822. mutex_init(&ic->i_recv_mutex);
  823. #ifndef KERNEL_HAS_ATOMIC64
  824. spin_lock_init(&ic->i_ack_lock);
  825. #endif
  826. atomic_set(&ic->i_signaled_sends, 0);
  827. /*
  828. * rds_ib_conn_shutdown() waits for these to be emptied so they
  829. * must be initialized before it can be called.
  830. */
  831. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  832. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  833. ic->conn = conn;
  834. conn->c_transport_data = ic;
  835. spin_lock_irqsave(&ib_nodev_conns_lock, flags);
  836. list_add_tail(&ic->ib_node, &ib_nodev_conns);
  837. spin_unlock_irqrestore(&ib_nodev_conns_lock, flags);
  838. rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
  839. return 0;
  840. }
  841. /*
  842. * Free a connection. Connection must be shut down and not set for reconnect.
  843. */
  844. void rds_ib_conn_free(void *arg)
  845. {
  846. struct rds_ib_connection *ic = arg;
  847. spinlock_t *lock_ptr;
  848. rdsdebug("ic %p\n", ic);
  849. /*
  850. * Conn is either on a dev's list or on the nodev list.
  851. * A race with shutdown() or connect() would cause problems
  852. * (since rds_ibdev would change) but that should never happen.
  853. */
  854. lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock;
  855. spin_lock_irq(lock_ptr);
  856. list_del(&ic->ib_node);
  857. spin_unlock_irq(lock_ptr);
  858. rds_ib_recv_free_caches(ic);
  859. kfree(ic);
  860. }
  861. /*
  862. * An error occurred on the connection
  863. */
  864. void
  865. __rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...)
  866. {
  867. va_list ap;
  868. rds_conn_drop(conn);
  869. va_start(ap, fmt);
  870. vprintk(fmt, ap);
  871. va_end(ap);
  872. }