smc_core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  4. *
  5. * Basic Transport Functions exploiting Infiniband API
  6. *
  7. * Copyright IBM Corp. 2016
  8. *
  9. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  10. */
  11. #include <linux/socket.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/random.h>
  14. #include <linux/workqueue.h>
  15. #include <net/tcp.h>
  16. #include <net/sock.h>
  17. #include <rdma/ib_verbs.h>
  18. #include <rdma/ib_cache.h>
  19. #include "smc.h"
  20. #include "smc_clc.h"
  21. #include "smc_core.h"
  22. #include "smc_ib.h"
  23. #include "smc_wr.h"
  24. #include "smc_llc.h"
  25. #include "smc_cdc.h"
  26. #include "smc_close.h"
  27. #include "smc_ism.h"
  28. #define SMC_LGR_NUM_INCR 256
  29. #define SMC_LGR_FREE_DELAY_SERV (600 * HZ)
  30. #define SMC_LGR_FREE_DELAY_CLNT (SMC_LGR_FREE_DELAY_SERV + 10 * HZ)
  31. #define SMC_LGR_FREE_DELAY_FAST (8 * HZ)
  32. static struct smc_lgr_list smc_lgr_list = { /* established link groups */
  33. .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
  34. .list = LIST_HEAD_INIT(smc_lgr_list.list),
  35. .num = 0,
  36. };
  37. static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
  38. struct smc_buf_desc *buf_desc);
  39. static void smc_lgr_schedule_free_work(struct smc_link_group *lgr)
  40. {
  41. /* client link group creation always follows the server link group
  42. * creation. For client use a somewhat higher removal delay time,
  43. * otherwise there is a risk of out-of-sync link groups.
  44. */
  45. mod_delayed_work(system_wq, &lgr->free_work,
  46. (!lgr->is_smcd && lgr->role == SMC_CLNT) ?
  47. SMC_LGR_FREE_DELAY_CLNT : SMC_LGR_FREE_DELAY_SERV);
  48. }
  49. void smc_lgr_schedule_free_work_fast(struct smc_link_group *lgr)
  50. {
  51. mod_delayed_work(system_wq, &lgr->free_work, SMC_LGR_FREE_DELAY_FAST);
  52. }
  53. /* Register connection's alert token in our lookup structure.
  54. * To use rbtrees we have to implement our own insert core.
  55. * Requires @conns_lock
  56. * @smc connection to register
  57. * Returns 0 on success, != otherwise.
  58. */
  59. static void smc_lgr_add_alert_token(struct smc_connection *conn)
  60. {
  61. struct rb_node **link, *parent = NULL;
  62. u32 token = conn->alert_token_local;
  63. link = &conn->lgr->conns_all.rb_node;
  64. while (*link) {
  65. struct smc_connection *cur = rb_entry(*link,
  66. struct smc_connection, alert_node);
  67. parent = *link;
  68. if (cur->alert_token_local > token)
  69. link = &parent->rb_left;
  70. else
  71. link = &parent->rb_right;
  72. }
  73. /* Put the new node there */
  74. rb_link_node(&conn->alert_node, parent, link);
  75. rb_insert_color(&conn->alert_node, &conn->lgr->conns_all);
  76. }
  77. /* Register connection in link group by assigning an alert token
  78. * registered in a search tree.
  79. * Requires @conns_lock
  80. * Note that '0' is a reserved value and not assigned.
  81. */
  82. static void smc_lgr_register_conn(struct smc_connection *conn)
  83. {
  84. struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
  85. static atomic_t nexttoken = ATOMIC_INIT(0);
  86. /* find a new alert_token_local value not yet used by some connection
  87. * in this link group
  88. */
  89. sock_hold(&smc->sk); /* sock_put in smc_lgr_unregister_conn() */
  90. while (!conn->alert_token_local) {
  91. conn->alert_token_local = atomic_inc_return(&nexttoken);
  92. if (smc_lgr_find_conn(conn->alert_token_local, conn->lgr))
  93. conn->alert_token_local = 0;
  94. }
  95. smc_lgr_add_alert_token(conn);
  96. conn->lgr->conns_num++;
  97. }
  98. /* Unregister connection and reset the alert token of the given connection<
  99. */
  100. static void __smc_lgr_unregister_conn(struct smc_connection *conn)
  101. {
  102. struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
  103. struct smc_link_group *lgr = conn->lgr;
  104. rb_erase(&conn->alert_node, &lgr->conns_all);
  105. lgr->conns_num--;
  106. conn->alert_token_local = 0;
  107. conn->lgr = NULL;
  108. sock_put(&smc->sk); /* sock_hold in smc_lgr_register_conn() */
  109. }
  110. /* Unregister connection from lgr
  111. */
  112. static void smc_lgr_unregister_conn(struct smc_connection *conn)
  113. {
  114. struct smc_link_group *lgr = conn->lgr;
  115. if (!lgr)
  116. return;
  117. write_lock_bh(&lgr->conns_lock);
  118. if (conn->alert_token_local) {
  119. __smc_lgr_unregister_conn(conn);
  120. }
  121. write_unlock_bh(&lgr->conns_lock);
  122. }
  123. /* Send delete link, either as client to request the initiation
  124. * of the DELETE LINK sequence from server; or as server to
  125. * initiate the delete processing. See smc_llc_rx_delete_link().
  126. */
  127. static int smc_link_send_delete(struct smc_link *lnk)
  128. {
  129. if (lnk->state == SMC_LNK_ACTIVE &&
  130. !smc_llc_send_delete_link(lnk, SMC_LLC_REQ, true)) {
  131. smc_llc_link_deleting(lnk);
  132. return 0;
  133. }
  134. return -ENOTCONN;
  135. }
  136. static void smc_lgr_free_work(struct work_struct *work)
  137. {
  138. struct smc_link_group *lgr = container_of(to_delayed_work(work),
  139. struct smc_link_group,
  140. free_work);
  141. bool conns;
  142. spin_lock_bh(&smc_lgr_list.lock);
  143. if (list_empty(&lgr->list))
  144. goto free;
  145. read_lock_bh(&lgr->conns_lock);
  146. conns = RB_EMPTY_ROOT(&lgr->conns_all);
  147. read_unlock_bh(&lgr->conns_lock);
  148. if (!conns) { /* number of lgr connections is no longer zero */
  149. spin_unlock_bh(&smc_lgr_list.lock);
  150. return;
  151. }
  152. list_del_init(&lgr->list); /* remove from smc_lgr_list */
  153. free:
  154. spin_unlock_bh(&smc_lgr_list.lock);
  155. if (!lgr->is_smcd && !lgr->terminating) {
  156. /* try to send del link msg, on error free lgr immediately */
  157. if (!smc_link_send_delete(&lgr->lnk[SMC_SINGLE_LINK])) {
  158. /* reschedule in case we never receive a response */
  159. smc_lgr_schedule_free_work(lgr);
  160. return;
  161. }
  162. }
  163. if (!delayed_work_pending(&lgr->free_work)) {
  164. struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
  165. if (!lgr->is_smcd && lnk->state != SMC_LNK_INACTIVE)
  166. smc_llc_link_inactive(lnk);
  167. smc_lgr_free(lgr);
  168. }
  169. }
  170. /* create a new SMC link group */
  171. static int smc_lgr_create(struct smc_sock *smc, bool is_smcd,
  172. struct smc_ib_device *smcibdev, u8 ibport,
  173. char *peer_systemid, unsigned short vlan_id,
  174. struct smcd_dev *smcismdev, u64 peer_gid)
  175. {
  176. struct smc_link_group *lgr;
  177. struct smc_link *lnk;
  178. u8 rndvec[3];
  179. int rc = 0;
  180. int i;
  181. if (is_smcd && vlan_id) {
  182. rc = smc_ism_get_vlan(smcismdev, vlan_id);
  183. if (rc)
  184. goto out;
  185. }
  186. lgr = kzalloc(sizeof(*lgr), GFP_KERNEL);
  187. if (!lgr) {
  188. rc = -ENOMEM;
  189. goto out;
  190. }
  191. lgr->is_smcd = is_smcd;
  192. lgr->sync_err = 0;
  193. lgr->vlan_id = vlan_id;
  194. rwlock_init(&lgr->sndbufs_lock);
  195. rwlock_init(&lgr->rmbs_lock);
  196. rwlock_init(&lgr->conns_lock);
  197. for (i = 0; i < SMC_RMBE_SIZES; i++) {
  198. INIT_LIST_HEAD(&lgr->sndbufs[i]);
  199. INIT_LIST_HEAD(&lgr->rmbs[i]);
  200. }
  201. smc_lgr_list.num += SMC_LGR_NUM_INCR;
  202. memcpy(&lgr->id, (u8 *)&smc_lgr_list.num, SMC_LGR_ID_SIZE);
  203. INIT_DELAYED_WORK(&lgr->free_work, smc_lgr_free_work);
  204. lgr->conns_all = RB_ROOT;
  205. if (is_smcd) {
  206. /* SMC-D specific settings */
  207. lgr->peer_gid = peer_gid;
  208. lgr->smcd = smcismdev;
  209. } else {
  210. /* SMC-R specific settings */
  211. lgr->role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
  212. memcpy(lgr->peer_systemid, peer_systemid, SMC_SYSTEMID_LEN);
  213. lnk = &lgr->lnk[SMC_SINGLE_LINK];
  214. /* initialize link */
  215. lnk->state = SMC_LNK_ACTIVATING;
  216. lnk->link_id = SMC_SINGLE_LINK;
  217. lnk->smcibdev = smcibdev;
  218. lnk->ibport = ibport;
  219. lnk->path_mtu = smcibdev->pattr[ibport - 1].active_mtu;
  220. if (!smcibdev->initialized)
  221. smc_ib_setup_per_ibdev(smcibdev);
  222. get_random_bytes(rndvec, sizeof(rndvec));
  223. lnk->psn_initial = rndvec[0] + (rndvec[1] << 8) +
  224. (rndvec[2] << 16);
  225. rc = smc_ib_determine_gid(lnk->smcibdev, lnk->ibport,
  226. vlan_id, lnk->gid, &lnk->sgid_index);
  227. if (rc)
  228. goto free_lgr;
  229. rc = smc_llc_link_init(lnk);
  230. if (rc)
  231. goto free_lgr;
  232. rc = smc_wr_alloc_link_mem(lnk);
  233. if (rc)
  234. goto clear_llc_lnk;
  235. rc = smc_ib_create_protection_domain(lnk);
  236. if (rc)
  237. goto free_link_mem;
  238. rc = smc_ib_create_queue_pair(lnk);
  239. if (rc)
  240. goto dealloc_pd;
  241. rc = smc_wr_create_link(lnk);
  242. if (rc)
  243. goto destroy_qp;
  244. }
  245. smc->conn.lgr = lgr;
  246. spin_lock_bh(&smc_lgr_list.lock);
  247. list_add(&lgr->list, &smc_lgr_list.list);
  248. spin_unlock_bh(&smc_lgr_list.lock);
  249. return 0;
  250. destroy_qp:
  251. smc_ib_destroy_queue_pair(lnk);
  252. dealloc_pd:
  253. smc_ib_dealloc_protection_domain(lnk);
  254. free_link_mem:
  255. smc_wr_free_link_mem(lnk);
  256. clear_llc_lnk:
  257. smc_llc_link_clear(lnk);
  258. free_lgr:
  259. kfree(lgr);
  260. out:
  261. return rc;
  262. }
  263. static void smc_buf_unuse(struct smc_connection *conn,
  264. struct smc_link_group *lgr)
  265. {
  266. if (conn->sndbuf_desc)
  267. conn->sndbuf_desc->used = 0;
  268. if (conn->rmb_desc) {
  269. if (!conn->rmb_desc->regerr) {
  270. conn->rmb_desc->reused = 1;
  271. conn->rmb_desc->used = 0;
  272. } else {
  273. /* buf registration failed, reuse not possible */
  274. write_lock_bh(&lgr->rmbs_lock);
  275. list_del(&conn->rmb_desc->list);
  276. write_unlock_bh(&lgr->rmbs_lock);
  277. smc_buf_free(lgr, true, conn->rmb_desc);
  278. }
  279. }
  280. }
  281. /* remove a finished connection from its link group */
  282. void smc_conn_free(struct smc_connection *conn)
  283. {
  284. struct smc_link_group *lgr = conn->lgr;
  285. if (!lgr)
  286. return;
  287. if (lgr->is_smcd) {
  288. smc_ism_unset_conn(conn);
  289. tasklet_kill(&conn->rx_tsklet);
  290. } else {
  291. smc_cdc_tx_dismiss_slots(conn);
  292. }
  293. smc_lgr_unregister_conn(conn); /* unsets conn->lgr */
  294. smc_buf_unuse(conn, lgr); /* allow buffer reuse */
  295. if (!lgr->conns_num)
  296. smc_lgr_schedule_free_work(lgr);
  297. }
  298. static void smc_link_clear(struct smc_link *lnk)
  299. {
  300. lnk->peer_qpn = 0;
  301. smc_llc_link_clear(lnk);
  302. smc_ib_modify_qp_reset(lnk);
  303. smc_wr_free_link(lnk);
  304. smc_ib_destroy_queue_pair(lnk);
  305. smc_ib_dealloc_protection_domain(lnk);
  306. smc_wr_free_link_mem(lnk);
  307. }
  308. static void smcr_buf_free(struct smc_link_group *lgr, bool is_rmb,
  309. struct smc_buf_desc *buf_desc)
  310. {
  311. struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
  312. if (is_rmb) {
  313. if (buf_desc->mr_rx[SMC_SINGLE_LINK])
  314. smc_ib_put_memory_region(
  315. buf_desc->mr_rx[SMC_SINGLE_LINK]);
  316. smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
  317. DMA_FROM_DEVICE);
  318. } else {
  319. smc_ib_buf_unmap_sg(lnk->smcibdev, buf_desc,
  320. DMA_TO_DEVICE);
  321. }
  322. sg_free_table(&buf_desc->sgt[SMC_SINGLE_LINK]);
  323. if (buf_desc->pages)
  324. __free_pages(buf_desc->pages, buf_desc->order);
  325. kfree(buf_desc);
  326. }
  327. static void smcd_buf_free(struct smc_link_group *lgr, bool is_dmb,
  328. struct smc_buf_desc *buf_desc)
  329. {
  330. if (is_dmb) {
  331. /* restore original buf len */
  332. buf_desc->len += sizeof(struct smcd_cdc_msg);
  333. smc_ism_unregister_dmb(lgr->smcd, buf_desc);
  334. } else {
  335. kfree(buf_desc->cpu_addr);
  336. }
  337. kfree(buf_desc);
  338. }
  339. static void smc_buf_free(struct smc_link_group *lgr, bool is_rmb,
  340. struct smc_buf_desc *buf_desc)
  341. {
  342. if (lgr->is_smcd)
  343. smcd_buf_free(lgr, is_rmb, buf_desc);
  344. else
  345. smcr_buf_free(lgr, is_rmb, buf_desc);
  346. }
  347. static void __smc_lgr_free_bufs(struct smc_link_group *lgr, bool is_rmb)
  348. {
  349. struct smc_buf_desc *buf_desc, *bf_desc;
  350. struct list_head *buf_list;
  351. int i;
  352. for (i = 0; i < SMC_RMBE_SIZES; i++) {
  353. if (is_rmb)
  354. buf_list = &lgr->rmbs[i];
  355. else
  356. buf_list = &lgr->sndbufs[i];
  357. list_for_each_entry_safe(buf_desc, bf_desc, buf_list,
  358. list) {
  359. list_del(&buf_desc->list);
  360. smc_buf_free(lgr, is_rmb, buf_desc);
  361. }
  362. }
  363. }
  364. static void smc_lgr_free_bufs(struct smc_link_group *lgr)
  365. {
  366. /* free send buffers */
  367. __smc_lgr_free_bufs(lgr, false);
  368. /* free rmbs */
  369. __smc_lgr_free_bufs(lgr, true);
  370. }
  371. /* remove a link group */
  372. void smc_lgr_free(struct smc_link_group *lgr)
  373. {
  374. smc_lgr_free_bufs(lgr);
  375. if (lgr->is_smcd)
  376. smc_ism_put_vlan(lgr->smcd, lgr->vlan_id);
  377. else
  378. smc_link_clear(&lgr->lnk[SMC_SINGLE_LINK]);
  379. kfree(lgr);
  380. }
  381. void smc_lgr_forget(struct smc_link_group *lgr)
  382. {
  383. spin_lock_bh(&smc_lgr_list.lock);
  384. /* do not use this link group for new connections */
  385. if (!list_empty(&lgr->list))
  386. list_del_init(&lgr->list);
  387. spin_unlock_bh(&smc_lgr_list.lock);
  388. }
  389. /* terminate linkgroup abnormally */
  390. static void __smc_lgr_terminate(struct smc_link_group *lgr)
  391. {
  392. struct smc_connection *conn;
  393. struct smc_sock *smc;
  394. struct rb_node *node;
  395. if (lgr->terminating)
  396. return; /* lgr already terminating */
  397. lgr->terminating = 1;
  398. if (!list_empty(&lgr->list)) /* forget lgr */
  399. list_del_init(&lgr->list);
  400. if (!lgr->is_smcd)
  401. smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]);
  402. write_lock_bh(&lgr->conns_lock);
  403. node = rb_first(&lgr->conns_all);
  404. while (node) {
  405. conn = rb_entry(node, struct smc_connection, alert_node);
  406. smc = container_of(conn, struct smc_sock, conn);
  407. sock_hold(&smc->sk); /* sock_put in close work */
  408. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  409. __smc_lgr_unregister_conn(conn);
  410. write_unlock_bh(&lgr->conns_lock);
  411. if (!schedule_work(&conn->close_work))
  412. sock_put(&smc->sk);
  413. write_lock_bh(&lgr->conns_lock);
  414. node = rb_first(&lgr->conns_all);
  415. }
  416. write_unlock_bh(&lgr->conns_lock);
  417. if (!lgr->is_smcd)
  418. wake_up(&lgr->lnk[SMC_SINGLE_LINK].wr_reg_wait);
  419. smc_lgr_schedule_free_work(lgr);
  420. }
  421. void smc_lgr_terminate(struct smc_link_group *lgr)
  422. {
  423. spin_lock_bh(&smc_lgr_list.lock);
  424. __smc_lgr_terminate(lgr);
  425. spin_unlock_bh(&smc_lgr_list.lock);
  426. }
  427. /* Called when IB port is terminated */
  428. void smc_port_terminate(struct smc_ib_device *smcibdev, u8 ibport)
  429. {
  430. struct smc_link_group *lgr, *l;
  431. spin_lock_bh(&smc_lgr_list.lock);
  432. list_for_each_entry_safe(lgr, l, &smc_lgr_list.list, list) {
  433. if (!lgr->is_smcd &&
  434. lgr->lnk[SMC_SINGLE_LINK].smcibdev == smcibdev &&
  435. lgr->lnk[SMC_SINGLE_LINK].ibport == ibport)
  436. __smc_lgr_terminate(lgr);
  437. }
  438. spin_unlock_bh(&smc_lgr_list.lock);
  439. }
  440. /* Called when SMC-D device is terminated or peer is lost */
  441. void smc_smcd_terminate(struct smcd_dev *dev, u64 peer_gid)
  442. {
  443. struct smc_link_group *lgr, *l;
  444. LIST_HEAD(lgr_free_list);
  445. /* run common cleanup function and build free list */
  446. spin_lock_bh(&smc_lgr_list.lock);
  447. list_for_each_entry_safe(lgr, l, &smc_lgr_list.list, list) {
  448. if (lgr->is_smcd && lgr->smcd == dev &&
  449. (!peer_gid || lgr->peer_gid == peer_gid) &&
  450. !list_empty(&lgr->list)) {
  451. __smc_lgr_terminate(lgr);
  452. list_move(&lgr->list, &lgr_free_list);
  453. }
  454. }
  455. spin_unlock_bh(&smc_lgr_list.lock);
  456. /* cancel the regular free workers and actually free lgrs */
  457. list_for_each_entry_safe(lgr, l, &lgr_free_list, list) {
  458. list_del_init(&lgr->list);
  459. cancel_delayed_work_sync(&lgr->free_work);
  460. smc_lgr_free(lgr);
  461. }
  462. }
  463. /* Determine vlan of internal TCP socket.
  464. * @vlan_id: address to store the determined vlan id into
  465. */
  466. int smc_vlan_by_tcpsk(struct socket *clcsock, unsigned short *vlan_id)
  467. {
  468. struct dst_entry *dst = sk_dst_get(clcsock->sk);
  469. struct net_device *ndev;
  470. int i, nest_lvl, rc = 0;
  471. *vlan_id = 0;
  472. if (!dst) {
  473. rc = -ENOTCONN;
  474. goto out;
  475. }
  476. if (!dst->dev) {
  477. rc = -ENODEV;
  478. goto out_rel;
  479. }
  480. ndev = dst->dev;
  481. if (is_vlan_dev(ndev)) {
  482. *vlan_id = vlan_dev_vlan_id(ndev);
  483. goto out_rel;
  484. }
  485. rtnl_lock();
  486. nest_lvl = dev_get_nest_level(ndev);
  487. for (i = 0; i < nest_lvl; i++) {
  488. struct list_head *lower = &ndev->adj_list.lower;
  489. if (list_empty(lower))
  490. break;
  491. lower = lower->next;
  492. ndev = (struct net_device *)netdev_lower_get_next(ndev, &lower);
  493. if (is_vlan_dev(ndev)) {
  494. *vlan_id = vlan_dev_vlan_id(ndev);
  495. break;
  496. }
  497. }
  498. rtnl_unlock();
  499. out_rel:
  500. dst_release(dst);
  501. out:
  502. return rc;
  503. }
  504. static bool smcr_lgr_match(struct smc_link_group *lgr,
  505. struct smc_clc_msg_local *lcl,
  506. enum smc_lgr_role role)
  507. {
  508. return !memcmp(lgr->peer_systemid, lcl->id_for_peer,
  509. SMC_SYSTEMID_LEN) &&
  510. !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_gid, &lcl->gid,
  511. SMC_GID_SIZE) &&
  512. !memcmp(lgr->lnk[SMC_SINGLE_LINK].peer_mac, lcl->mac,
  513. sizeof(lcl->mac)) &&
  514. lgr->role == role;
  515. }
  516. static bool smcd_lgr_match(struct smc_link_group *lgr,
  517. struct smcd_dev *smcismdev, u64 peer_gid)
  518. {
  519. return lgr->peer_gid == peer_gid && lgr->smcd == smcismdev;
  520. }
  521. /* create a new SMC connection (and a new link group if necessary) */
  522. int smc_conn_create(struct smc_sock *smc, bool is_smcd, int srv_first_contact,
  523. struct smc_ib_device *smcibdev, u8 ibport,
  524. struct smc_clc_msg_local *lcl, struct smcd_dev *smcd,
  525. u64 peer_gid)
  526. {
  527. struct smc_connection *conn = &smc->conn;
  528. int local_contact = SMC_FIRST_CONTACT;
  529. struct smc_link_group *lgr;
  530. unsigned short vlan_id;
  531. enum smc_lgr_role role;
  532. int rc = 0;
  533. role = smc->listen_smc ? SMC_SERV : SMC_CLNT;
  534. rc = smc_vlan_by_tcpsk(smc->clcsock, &vlan_id);
  535. if (rc)
  536. return rc;
  537. if ((role == SMC_CLNT) && srv_first_contact)
  538. /* create new link group as well */
  539. goto create;
  540. /* determine if an existing link group can be reused */
  541. spin_lock_bh(&smc_lgr_list.lock);
  542. list_for_each_entry(lgr, &smc_lgr_list.list, list) {
  543. write_lock_bh(&lgr->conns_lock);
  544. if ((is_smcd ? smcd_lgr_match(lgr, smcd, peer_gid) :
  545. smcr_lgr_match(lgr, lcl, role)) &&
  546. !lgr->sync_err &&
  547. lgr->vlan_id == vlan_id &&
  548. (role == SMC_CLNT ||
  549. lgr->conns_num < SMC_RMBS_PER_LGR_MAX)) {
  550. /* link group found */
  551. local_contact = SMC_REUSE_CONTACT;
  552. conn->lgr = lgr;
  553. smc_lgr_register_conn(conn); /* add smc conn to lgr */
  554. if (delayed_work_pending(&lgr->free_work))
  555. cancel_delayed_work(&lgr->free_work);
  556. write_unlock_bh(&lgr->conns_lock);
  557. break;
  558. }
  559. write_unlock_bh(&lgr->conns_lock);
  560. }
  561. spin_unlock_bh(&smc_lgr_list.lock);
  562. if (role == SMC_CLNT && !srv_first_contact &&
  563. (local_contact == SMC_FIRST_CONTACT)) {
  564. /* Server reuses a link group, but Client wants to start
  565. * a new one
  566. * send out_of_sync decline, reason synchr. error
  567. */
  568. return -ENOLINK;
  569. }
  570. create:
  571. if (local_contact == SMC_FIRST_CONTACT) {
  572. rc = smc_lgr_create(smc, is_smcd, smcibdev, ibport,
  573. lcl->id_for_peer, vlan_id, smcd, peer_gid);
  574. if (rc)
  575. goto out;
  576. smc_lgr_register_conn(conn); /* add smc conn to lgr */
  577. }
  578. conn->local_tx_ctrl.common.type = SMC_CDC_MSG_TYPE;
  579. conn->local_tx_ctrl.len = SMC_WR_TX_SIZE;
  580. conn->urg_state = SMC_URG_READ;
  581. if (is_smcd) {
  582. conn->rx_off = sizeof(struct smcd_cdc_msg);
  583. smcd_cdc_rx_init(conn); /* init tasklet for this conn */
  584. }
  585. #ifndef KERNEL_HAS_ATOMIC64
  586. spin_lock_init(&conn->acurs_lock);
  587. #endif
  588. out:
  589. return rc ? rc : local_contact;
  590. }
  591. /* convert the RMB size into the compressed notation - minimum 16K.
  592. * In contrast to plain ilog2, this rounds towards the next power of 2,
  593. * so the socket application gets at least its desired sndbuf / rcvbuf size.
  594. */
  595. static u8 smc_compress_bufsize(int size)
  596. {
  597. u8 compressed;
  598. if (size <= SMC_BUF_MIN_SIZE)
  599. return 0;
  600. size = (size - 1) >> 14;
  601. compressed = ilog2(size) + 1;
  602. if (compressed >= SMC_RMBE_SIZES)
  603. compressed = SMC_RMBE_SIZES - 1;
  604. return compressed;
  605. }
  606. /* convert the RMB size from compressed notation into integer */
  607. int smc_uncompress_bufsize(u8 compressed)
  608. {
  609. u32 size;
  610. size = 0x00000001 << (((int)compressed) + 14);
  611. return (int)size;
  612. }
  613. /* try to reuse a sndbuf or rmb description slot for a certain
  614. * buffer size; if not available, return NULL
  615. */
  616. static struct smc_buf_desc *smc_buf_get_slot(int compressed_bufsize,
  617. rwlock_t *lock,
  618. struct list_head *buf_list)
  619. {
  620. struct smc_buf_desc *buf_slot;
  621. read_lock_bh(lock);
  622. list_for_each_entry(buf_slot, buf_list, list) {
  623. if (cmpxchg(&buf_slot->used, 0, 1) == 0) {
  624. read_unlock_bh(lock);
  625. return buf_slot;
  626. }
  627. }
  628. read_unlock_bh(lock);
  629. return NULL;
  630. }
  631. /* one of the conditions for announcing a receiver's current window size is
  632. * that it "results in a minimum increase in the window size of 10% of the
  633. * receive buffer space" [RFC7609]
  634. */
  635. static inline int smc_rmb_wnd_update_limit(int rmbe_size)
  636. {
  637. return min_t(int, rmbe_size / 10, SOCK_MIN_SNDBUF / 2);
  638. }
  639. static struct smc_buf_desc *smcr_new_buf_create(struct smc_link_group *lgr,
  640. bool is_rmb, int bufsize)
  641. {
  642. struct smc_buf_desc *buf_desc;
  643. struct smc_link *lnk;
  644. int rc;
  645. /* try to alloc a new buffer */
  646. buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
  647. if (!buf_desc)
  648. return ERR_PTR(-ENOMEM);
  649. buf_desc->order = get_order(bufsize);
  650. buf_desc->pages = alloc_pages(GFP_KERNEL | __GFP_NOWARN |
  651. __GFP_NOMEMALLOC | __GFP_COMP |
  652. __GFP_NORETRY | __GFP_ZERO,
  653. buf_desc->order);
  654. if (!buf_desc->pages) {
  655. kfree(buf_desc);
  656. return ERR_PTR(-EAGAIN);
  657. }
  658. buf_desc->cpu_addr = (void *)page_address(buf_desc->pages);
  659. /* build the sg table from the pages */
  660. lnk = &lgr->lnk[SMC_SINGLE_LINK];
  661. rc = sg_alloc_table(&buf_desc->sgt[SMC_SINGLE_LINK], 1,
  662. GFP_KERNEL);
  663. if (rc) {
  664. smc_buf_free(lgr, is_rmb, buf_desc);
  665. return ERR_PTR(rc);
  666. }
  667. sg_set_buf(buf_desc->sgt[SMC_SINGLE_LINK].sgl,
  668. buf_desc->cpu_addr, bufsize);
  669. /* map sg table to DMA address */
  670. rc = smc_ib_buf_map_sg(lnk->smcibdev, buf_desc,
  671. is_rmb ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  672. /* SMC protocol depends on mapping to one DMA address only */
  673. if (rc != 1) {
  674. smc_buf_free(lgr, is_rmb, buf_desc);
  675. return ERR_PTR(-EAGAIN);
  676. }
  677. /* create a new memory region for the RMB */
  678. if (is_rmb) {
  679. rc = smc_ib_get_memory_region(lnk->roce_pd,
  680. IB_ACCESS_REMOTE_WRITE |
  681. IB_ACCESS_LOCAL_WRITE,
  682. buf_desc);
  683. if (rc) {
  684. smc_buf_free(lgr, is_rmb, buf_desc);
  685. return ERR_PTR(rc);
  686. }
  687. }
  688. buf_desc->len = bufsize;
  689. return buf_desc;
  690. }
  691. #define SMCD_DMBE_SIZES 7 /* 0 -> 16KB, 1 -> 32KB, .. 6 -> 1MB */
  692. static struct smc_buf_desc *smcd_new_buf_create(struct smc_link_group *lgr,
  693. bool is_dmb, int bufsize)
  694. {
  695. struct smc_buf_desc *buf_desc;
  696. int rc;
  697. if (smc_compress_bufsize(bufsize) > SMCD_DMBE_SIZES)
  698. return ERR_PTR(-EAGAIN);
  699. /* try to alloc a new DMB */
  700. buf_desc = kzalloc(sizeof(*buf_desc), GFP_KERNEL);
  701. if (!buf_desc)
  702. return ERR_PTR(-ENOMEM);
  703. if (is_dmb) {
  704. rc = smc_ism_register_dmb(lgr, bufsize, buf_desc);
  705. if (rc) {
  706. kfree(buf_desc);
  707. return ERR_PTR(-EAGAIN);
  708. }
  709. buf_desc->pages = virt_to_page(buf_desc->cpu_addr);
  710. /* CDC header stored in buf. So, pretend it was smaller */
  711. buf_desc->len = bufsize - sizeof(struct smcd_cdc_msg);
  712. } else {
  713. buf_desc->cpu_addr = kzalloc(bufsize, GFP_KERNEL |
  714. __GFP_NOWARN | __GFP_NORETRY |
  715. __GFP_NOMEMALLOC);
  716. if (!buf_desc->cpu_addr) {
  717. kfree(buf_desc);
  718. return ERR_PTR(-EAGAIN);
  719. }
  720. buf_desc->len = bufsize;
  721. }
  722. return buf_desc;
  723. }
  724. static int __smc_buf_create(struct smc_sock *smc, bool is_smcd, bool is_rmb)
  725. {
  726. struct smc_buf_desc *buf_desc = ERR_PTR(-ENOMEM);
  727. struct smc_connection *conn = &smc->conn;
  728. struct smc_link_group *lgr = conn->lgr;
  729. struct list_head *buf_list;
  730. int bufsize, bufsize_short;
  731. int sk_buf_size;
  732. rwlock_t *lock;
  733. if (is_rmb)
  734. /* use socket recv buffer size (w/o overhead) as start value */
  735. sk_buf_size = smc->sk.sk_rcvbuf / 2;
  736. else
  737. /* use socket send buffer size (w/o overhead) as start value */
  738. sk_buf_size = smc->sk.sk_sndbuf / 2;
  739. for (bufsize_short = smc_compress_bufsize(sk_buf_size);
  740. bufsize_short >= 0; bufsize_short--) {
  741. if (is_rmb) {
  742. lock = &lgr->rmbs_lock;
  743. buf_list = &lgr->rmbs[bufsize_short];
  744. } else {
  745. lock = &lgr->sndbufs_lock;
  746. buf_list = &lgr->sndbufs[bufsize_short];
  747. }
  748. bufsize = smc_uncompress_bufsize(bufsize_short);
  749. if ((1 << get_order(bufsize)) > SG_MAX_SINGLE_ALLOC)
  750. continue;
  751. /* check for reusable slot in the link group */
  752. buf_desc = smc_buf_get_slot(bufsize_short, lock, buf_list);
  753. if (buf_desc) {
  754. memset(buf_desc->cpu_addr, 0, bufsize);
  755. break; /* found reusable slot */
  756. }
  757. if (is_smcd)
  758. buf_desc = smcd_new_buf_create(lgr, is_rmb, bufsize);
  759. else
  760. buf_desc = smcr_new_buf_create(lgr, is_rmb, bufsize);
  761. if (PTR_ERR(buf_desc) == -ENOMEM)
  762. break;
  763. if (IS_ERR(buf_desc))
  764. continue;
  765. buf_desc->used = 1;
  766. write_lock_bh(lock);
  767. list_add(&buf_desc->list, buf_list);
  768. write_unlock_bh(lock);
  769. break; /* found */
  770. }
  771. if (IS_ERR(buf_desc))
  772. return -ENOMEM;
  773. if (is_rmb) {
  774. conn->rmb_desc = buf_desc;
  775. conn->rmbe_size_short = bufsize_short;
  776. smc->sk.sk_rcvbuf = bufsize * 2;
  777. atomic_set(&conn->bytes_to_rcv, 0);
  778. conn->rmbe_update_limit =
  779. smc_rmb_wnd_update_limit(buf_desc->len);
  780. if (is_smcd)
  781. smc_ism_set_conn(conn); /* map RMB/smcd_dev to conn */
  782. } else {
  783. conn->sndbuf_desc = buf_desc;
  784. smc->sk.sk_sndbuf = bufsize * 2;
  785. atomic_set(&conn->sndbuf_space, bufsize);
  786. }
  787. return 0;
  788. }
  789. void smc_sndbuf_sync_sg_for_cpu(struct smc_connection *conn)
  790. {
  791. struct smc_link_group *lgr = conn->lgr;
  792. if (!conn->lgr || conn->lgr->is_smcd)
  793. return;
  794. smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
  795. conn->sndbuf_desc, DMA_TO_DEVICE);
  796. }
  797. void smc_sndbuf_sync_sg_for_device(struct smc_connection *conn)
  798. {
  799. struct smc_link_group *lgr = conn->lgr;
  800. if (!conn->lgr || conn->lgr->is_smcd)
  801. return;
  802. smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
  803. conn->sndbuf_desc, DMA_TO_DEVICE);
  804. }
  805. void smc_rmb_sync_sg_for_cpu(struct smc_connection *conn)
  806. {
  807. struct smc_link_group *lgr = conn->lgr;
  808. if (!conn->lgr || conn->lgr->is_smcd)
  809. return;
  810. smc_ib_sync_sg_for_cpu(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
  811. conn->rmb_desc, DMA_FROM_DEVICE);
  812. }
  813. void smc_rmb_sync_sg_for_device(struct smc_connection *conn)
  814. {
  815. struct smc_link_group *lgr = conn->lgr;
  816. if (!conn->lgr || conn->lgr->is_smcd)
  817. return;
  818. smc_ib_sync_sg_for_device(lgr->lnk[SMC_SINGLE_LINK].smcibdev,
  819. conn->rmb_desc, DMA_FROM_DEVICE);
  820. }
  821. /* create the send and receive buffer for an SMC socket;
  822. * receive buffers are called RMBs;
  823. * (even though the SMC protocol allows more than one RMB-element per RMB,
  824. * the Linux implementation uses just one RMB-element per RMB, i.e. uses an
  825. * extra RMB for every connection in a link group
  826. */
  827. int smc_buf_create(struct smc_sock *smc, bool is_smcd)
  828. {
  829. int rc;
  830. /* create send buffer */
  831. rc = __smc_buf_create(smc, is_smcd, false);
  832. if (rc)
  833. return rc;
  834. /* create rmb */
  835. rc = __smc_buf_create(smc, is_smcd, true);
  836. if (rc)
  837. smc_buf_free(smc->conn.lgr, false, smc->conn.sndbuf_desc);
  838. return rc;
  839. }
  840. static inline int smc_rmb_reserve_rtoken_idx(struct smc_link_group *lgr)
  841. {
  842. int i;
  843. for_each_clear_bit(i, lgr->rtokens_used_mask, SMC_RMBS_PER_LGR_MAX) {
  844. if (!test_and_set_bit(i, lgr->rtokens_used_mask))
  845. return i;
  846. }
  847. return -ENOSPC;
  848. }
  849. /* add a new rtoken from peer */
  850. int smc_rtoken_add(struct smc_link_group *lgr, __be64 nw_vaddr, __be32 nw_rkey)
  851. {
  852. u64 dma_addr = be64_to_cpu(nw_vaddr);
  853. u32 rkey = ntohl(nw_rkey);
  854. int i;
  855. for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
  856. if ((lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey) &&
  857. (lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr == dma_addr) &&
  858. test_bit(i, lgr->rtokens_used_mask)) {
  859. /* already in list */
  860. return i;
  861. }
  862. }
  863. i = smc_rmb_reserve_rtoken_idx(lgr);
  864. if (i < 0)
  865. return i;
  866. lgr->rtokens[i][SMC_SINGLE_LINK].rkey = rkey;
  867. lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = dma_addr;
  868. return i;
  869. }
  870. /* delete an rtoken */
  871. int smc_rtoken_delete(struct smc_link_group *lgr, __be32 nw_rkey)
  872. {
  873. u32 rkey = ntohl(nw_rkey);
  874. int i;
  875. for (i = 0; i < SMC_RMBS_PER_LGR_MAX; i++) {
  876. if (lgr->rtokens[i][SMC_SINGLE_LINK].rkey == rkey &&
  877. test_bit(i, lgr->rtokens_used_mask)) {
  878. lgr->rtokens[i][SMC_SINGLE_LINK].rkey = 0;
  879. lgr->rtokens[i][SMC_SINGLE_LINK].dma_addr = 0;
  880. clear_bit(i, lgr->rtokens_used_mask);
  881. return 0;
  882. }
  883. }
  884. return -ENOENT;
  885. }
  886. /* save rkey and dma_addr received from peer during clc handshake */
  887. int smc_rmb_rtoken_handling(struct smc_connection *conn,
  888. struct smc_clc_msg_accept_confirm *clc)
  889. {
  890. conn->rtoken_idx = smc_rtoken_add(conn->lgr, clc->rmb_dma_addr,
  891. clc->rmb_rkey);
  892. if (conn->rtoken_idx < 0)
  893. return conn->rtoken_idx;
  894. return 0;
  895. }
  896. /* Called (from smc_exit) when module is removed */
  897. void smc_core_exit(void)
  898. {
  899. struct smc_link_group *lgr, *lg;
  900. LIST_HEAD(lgr_freeing_list);
  901. spin_lock_bh(&smc_lgr_list.lock);
  902. if (!list_empty(&smc_lgr_list.list))
  903. list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
  904. spin_unlock_bh(&smc_lgr_list.lock);
  905. list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
  906. list_del_init(&lgr->list);
  907. if (!lgr->is_smcd) {
  908. struct smc_link *lnk = &lgr->lnk[SMC_SINGLE_LINK];
  909. if (lnk->state == SMC_LNK_ACTIVE)
  910. smc_llc_send_delete_link(lnk, SMC_LLC_REQ,
  911. false);
  912. smc_llc_link_inactive(lnk);
  913. }
  914. cancel_delayed_work_sync(&lgr->free_work);
  915. smc_lgr_free(lgr); /* free link group */
  916. }
  917. }