iw_rdma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 "rds.h"
  35. #include "rdma.h"
  36. #include "iw.h"
  37. /*
  38. * This is stored as mr->r_trans_private.
  39. */
  40. struct rds_iw_mr {
  41. struct rds_iw_device *device;
  42. struct rds_iw_mr_pool *pool;
  43. struct rdma_cm_id *cm_id;
  44. struct ib_mr *mr;
  45. struct ib_fast_reg_page_list *page_list;
  46. struct rds_iw_mapping mapping;
  47. unsigned char remap_count;
  48. };
  49. /*
  50. * Our own little MR pool
  51. */
  52. struct rds_iw_mr_pool {
  53. struct rds_iw_device *device; /* back ptr to the device that owns us */
  54. struct mutex flush_lock; /* serialize fmr invalidate */
  55. struct work_struct flush_worker; /* flush worker */
  56. spinlock_t list_lock; /* protect variables below */
  57. atomic_t item_count; /* total # of MRs */
  58. atomic_t dirty_count; /* # dirty of MRs */
  59. struct list_head dirty_list; /* dirty mappings */
  60. struct list_head clean_list; /* unused & unamapped MRs */
  61. atomic_t free_pinned; /* memory pinned by free MRs */
  62. unsigned long max_message_size; /* in pages */
  63. unsigned long max_items;
  64. unsigned long max_items_soft;
  65. unsigned long max_free_pinned;
  66. int max_pages;
  67. };
  68. static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all);
  69. static void rds_iw_mr_pool_flush_worker(struct work_struct *work);
  70. static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
  71. static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
  72. struct rds_iw_mr *ibmr,
  73. struct scatterlist *sg, unsigned int nents);
  74. static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
  75. static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
  76. struct list_head *unmap_list,
  77. struct list_head *kill_list);
  78. static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool, struct rds_iw_mr *ibmr);
  79. static int rds_iw_get_device(struct rds_sock *rs, struct rds_iw_device **rds_iwdev, struct rdma_cm_id **cm_id)
  80. {
  81. struct rds_iw_device *iwdev;
  82. struct rds_iw_cm_id *i_cm_id;
  83. *rds_iwdev = NULL;
  84. *cm_id = NULL;
  85. list_for_each_entry(iwdev, &rds_iw_devices, list) {
  86. spin_lock_irq(&iwdev->spinlock);
  87. list_for_each_entry(i_cm_id, &iwdev->cm_id_list, list) {
  88. struct sockaddr_in *src_addr, *dst_addr;
  89. src_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.src_addr;
  90. dst_addr = (struct sockaddr_in *)&i_cm_id->cm_id->route.addr.dst_addr;
  91. rdsdebug("local ipaddr = %x port %d, "
  92. "remote ipaddr = %x port %d"
  93. "..looking for %x port %d, "
  94. "remote ipaddr = %x port %d\n",
  95. src_addr->sin_addr.s_addr,
  96. src_addr->sin_port,
  97. dst_addr->sin_addr.s_addr,
  98. dst_addr->sin_port,
  99. rs->rs_bound_addr,
  100. rs->rs_bound_port,
  101. rs->rs_conn_addr,
  102. rs->rs_conn_port);
  103. #ifdef WORKING_TUPLE_DETECTION
  104. if (src_addr->sin_addr.s_addr == rs->rs_bound_addr &&
  105. src_addr->sin_port == rs->rs_bound_port &&
  106. dst_addr->sin_addr.s_addr == rs->rs_conn_addr &&
  107. dst_addr->sin_port == rs->rs_conn_port) {
  108. #else
  109. /* FIXME - needs to compare the local and remote
  110. * ipaddr/port tuple, but the ipaddr is the only
  111. * available infomation in the rds_sock (as the rest are
  112. * zero'ed. It doesn't appear to be properly populated
  113. * during connection setup...
  114. */
  115. if (src_addr->sin_addr.s_addr == rs->rs_bound_addr) {
  116. #endif
  117. spin_unlock_irq(&iwdev->spinlock);
  118. *rds_iwdev = iwdev;
  119. *cm_id = i_cm_id->cm_id;
  120. return 0;
  121. }
  122. }
  123. spin_unlock_irq(&iwdev->spinlock);
  124. }
  125. return 1;
  126. }
  127. static int rds_iw_add_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
  128. {
  129. struct rds_iw_cm_id *i_cm_id;
  130. i_cm_id = kmalloc(sizeof *i_cm_id, GFP_KERNEL);
  131. if (!i_cm_id)
  132. return -ENOMEM;
  133. i_cm_id->cm_id = cm_id;
  134. spin_lock_irq(&rds_iwdev->spinlock);
  135. list_add_tail(&i_cm_id->list, &rds_iwdev->cm_id_list);
  136. spin_unlock_irq(&rds_iwdev->spinlock);
  137. return 0;
  138. }
  139. void rds_iw_remove_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
  140. {
  141. struct rds_iw_cm_id *i_cm_id;
  142. spin_lock_irq(&rds_iwdev->spinlock);
  143. list_for_each_entry(i_cm_id, &rds_iwdev->cm_id_list, list) {
  144. if (i_cm_id->cm_id == cm_id) {
  145. list_del(&i_cm_id->list);
  146. kfree(i_cm_id);
  147. break;
  148. }
  149. }
  150. spin_unlock_irq(&rds_iwdev->spinlock);
  151. }
  152. int rds_iw_update_cm_id(struct rds_iw_device *rds_iwdev, struct rdma_cm_id *cm_id)
  153. {
  154. struct sockaddr_in *src_addr, *dst_addr;
  155. struct rds_iw_device *rds_iwdev_old;
  156. struct rds_sock rs;
  157. struct rdma_cm_id *pcm_id;
  158. int rc;
  159. src_addr = (struct sockaddr_in *)&cm_id->route.addr.src_addr;
  160. dst_addr = (struct sockaddr_in *)&cm_id->route.addr.dst_addr;
  161. rs.rs_bound_addr = src_addr->sin_addr.s_addr;
  162. rs.rs_bound_port = src_addr->sin_port;
  163. rs.rs_conn_addr = dst_addr->sin_addr.s_addr;
  164. rs.rs_conn_port = dst_addr->sin_port;
  165. rc = rds_iw_get_device(&rs, &rds_iwdev_old, &pcm_id);
  166. if (rc)
  167. rds_iw_remove_cm_id(rds_iwdev, cm_id);
  168. return rds_iw_add_cm_id(rds_iwdev, cm_id);
  169. }
  170. int rds_iw_add_conn(struct rds_iw_device *rds_iwdev, struct rds_connection *conn)
  171. {
  172. struct rds_iw_connection *ic = conn->c_transport_data;
  173. /* conn was previously on the nodev_conns_list */
  174. spin_lock_irq(&iw_nodev_conns_lock);
  175. BUG_ON(list_empty(&iw_nodev_conns));
  176. BUG_ON(list_empty(&ic->iw_node));
  177. list_del(&ic->iw_node);
  178. spin_unlock_irq(&iw_nodev_conns_lock);
  179. spin_lock_irq(&rds_iwdev->spinlock);
  180. list_add_tail(&ic->iw_node, &rds_iwdev->conn_list);
  181. spin_unlock_irq(&rds_iwdev->spinlock);
  182. ic->rds_iwdev = rds_iwdev;
  183. return 0;
  184. }
  185. void rds_iw_remove_nodev_conns(void)
  186. {
  187. struct rds_iw_connection *ic, *_ic;
  188. LIST_HEAD(tmp_list);
  189. /* avoid calling conn_destroy with irqs off */
  190. spin_lock_irq(&iw_nodev_conns_lock);
  191. list_splice(&iw_nodev_conns, &tmp_list);
  192. INIT_LIST_HEAD(&iw_nodev_conns);
  193. spin_unlock_irq(&iw_nodev_conns_lock);
  194. list_for_each_entry_safe(ic, _ic, &tmp_list, iw_node) {
  195. if (ic->conn->c_passive)
  196. rds_conn_destroy(ic->conn->c_passive);
  197. rds_conn_destroy(ic->conn);
  198. }
  199. }
  200. void rds_iw_remove_conns(struct rds_iw_device *rds_iwdev)
  201. {
  202. struct rds_iw_connection *ic, *_ic;
  203. LIST_HEAD(tmp_list);
  204. /* avoid calling conn_destroy with irqs off */
  205. spin_lock_irq(&rds_iwdev->spinlock);
  206. list_splice(&rds_iwdev->conn_list, &tmp_list);
  207. INIT_LIST_HEAD(&rds_iwdev->conn_list);
  208. spin_unlock_irq(&rds_iwdev->spinlock);
  209. list_for_each_entry_safe(ic, _ic, &tmp_list, iw_node) {
  210. if (ic->conn->c_passive)
  211. rds_conn_destroy(ic->conn->c_passive);
  212. rds_conn_destroy(ic->conn);
  213. }
  214. }
  215. static void rds_iw_set_scatterlist(struct rds_iw_scatterlist *sg,
  216. struct scatterlist *list, unsigned int sg_len)
  217. {
  218. sg->list = list;
  219. sg->len = sg_len;
  220. sg->dma_len = 0;
  221. sg->dma_npages = 0;
  222. sg->bytes = 0;
  223. }
  224. static u64 *rds_iw_map_scatterlist(struct rds_iw_device *rds_iwdev,
  225. struct rds_iw_scatterlist *sg,
  226. unsigned int dma_page_shift)
  227. {
  228. struct ib_device *dev = rds_iwdev->dev;
  229. u64 *dma_pages = NULL;
  230. u64 dma_mask;
  231. unsigned int dma_page_size;
  232. int i, j, ret;
  233. dma_page_size = 1 << dma_page_shift;
  234. dma_mask = dma_page_size - 1;
  235. WARN_ON(sg->dma_len);
  236. sg->dma_len = ib_dma_map_sg(dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
  237. if (unlikely(!sg->dma_len)) {
  238. printk(KERN_WARNING "RDS/IW: dma_map_sg failed!\n");
  239. return ERR_PTR(-EBUSY);
  240. }
  241. sg->bytes = 0;
  242. sg->dma_npages = 0;
  243. ret = -EINVAL;
  244. for (i = 0; i < sg->dma_len; ++i) {
  245. unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
  246. u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
  247. u64 end_addr;
  248. sg->bytes += dma_len;
  249. end_addr = dma_addr + dma_len;
  250. if (dma_addr & dma_mask) {
  251. if (i > 0)
  252. goto out_unmap;
  253. dma_addr &= ~dma_mask;
  254. }
  255. if (end_addr & dma_mask) {
  256. if (i < sg->dma_len - 1)
  257. goto out_unmap;
  258. end_addr = (end_addr + dma_mask) & ~dma_mask;
  259. }
  260. sg->dma_npages += (end_addr - dma_addr) >> dma_page_shift;
  261. }
  262. /* Now gather the dma addrs into one list */
  263. if (sg->dma_npages > fastreg_message_size)
  264. goto out_unmap;
  265. dma_pages = kmalloc(sizeof(u64) * sg->dma_npages, GFP_ATOMIC);
  266. if (!dma_pages) {
  267. ret = -ENOMEM;
  268. goto out_unmap;
  269. }
  270. for (i = j = 0; i < sg->dma_len; ++i) {
  271. unsigned int dma_len = ib_sg_dma_len(dev, &sg->list[i]);
  272. u64 dma_addr = ib_sg_dma_address(dev, &sg->list[i]);
  273. u64 end_addr;
  274. end_addr = dma_addr + dma_len;
  275. dma_addr &= ~dma_mask;
  276. for (; dma_addr < end_addr; dma_addr += dma_page_size)
  277. dma_pages[j++] = dma_addr;
  278. BUG_ON(j > sg->dma_npages);
  279. }
  280. return dma_pages;
  281. out_unmap:
  282. ib_dma_unmap_sg(rds_iwdev->dev, sg->list, sg->len, DMA_BIDIRECTIONAL);
  283. sg->dma_len = 0;
  284. kfree(dma_pages);
  285. return ERR_PTR(ret);
  286. }
  287. struct rds_iw_mr_pool *rds_iw_create_mr_pool(struct rds_iw_device *rds_iwdev)
  288. {
  289. struct rds_iw_mr_pool *pool;
  290. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  291. if (!pool) {
  292. printk(KERN_WARNING "RDS/IW: rds_iw_create_mr_pool alloc error\n");
  293. return ERR_PTR(-ENOMEM);
  294. }
  295. pool->device = rds_iwdev;
  296. INIT_LIST_HEAD(&pool->dirty_list);
  297. INIT_LIST_HEAD(&pool->clean_list);
  298. mutex_init(&pool->flush_lock);
  299. spin_lock_init(&pool->list_lock);
  300. INIT_WORK(&pool->flush_worker, rds_iw_mr_pool_flush_worker);
  301. pool->max_message_size = fastreg_message_size;
  302. pool->max_items = fastreg_pool_size;
  303. pool->max_free_pinned = pool->max_items * pool->max_message_size / 4;
  304. pool->max_pages = fastreg_message_size;
  305. /* We never allow more than max_items MRs to be allocated.
  306. * When we exceed more than max_items_soft, we start freeing
  307. * items more aggressively.
  308. * Make sure that max_items > max_items_soft > max_items / 2
  309. */
  310. pool->max_items_soft = pool->max_items * 3 / 4;
  311. return pool;
  312. }
  313. void rds_iw_get_mr_info(struct rds_iw_device *rds_iwdev, struct rds_info_rdma_connection *iinfo)
  314. {
  315. struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
  316. iinfo->rdma_mr_max = pool->max_items;
  317. iinfo->rdma_mr_size = pool->max_pages;
  318. }
  319. void rds_iw_destroy_mr_pool(struct rds_iw_mr_pool *pool)
  320. {
  321. flush_workqueue(rds_wq);
  322. rds_iw_flush_mr_pool(pool, 1);
  323. BUG_ON(atomic_read(&pool->item_count));
  324. BUG_ON(atomic_read(&pool->free_pinned));
  325. kfree(pool);
  326. }
  327. static inline struct rds_iw_mr *rds_iw_reuse_fmr(struct rds_iw_mr_pool *pool)
  328. {
  329. struct rds_iw_mr *ibmr = NULL;
  330. unsigned long flags;
  331. spin_lock_irqsave(&pool->list_lock, flags);
  332. if (!list_empty(&pool->clean_list)) {
  333. ibmr = list_entry(pool->clean_list.next, struct rds_iw_mr, mapping.m_list);
  334. list_del_init(&ibmr->mapping.m_list);
  335. }
  336. spin_unlock_irqrestore(&pool->list_lock, flags);
  337. return ibmr;
  338. }
  339. static struct rds_iw_mr *rds_iw_alloc_mr(struct rds_iw_device *rds_iwdev)
  340. {
  341. struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
  342. struct rds_iw_mr *ibmr = NULL;
  343. int err = 0, iter = 0;
  344. while (1) {
  345. ibmr = rds_iw_reuse_fmr(pool);
  346. if (ibmr)
  347. return ibmr;
  348. /* No clean MRs - now we have the choice of either
  349. * allocating a fresh MR up to the limit imposed by the
  350. * driver, or flush any dirty unused MRs.
  351. * We try to avoid stalling in the send path if possible,
  352. * so we allocate as long as we're allowed to.
  353. *
  354. * We're fussy with enforcing the FMR limit, though. If the driver
  355. * tells us we can't use more than N fmrs, we shouldn't start
  356. * arguing with it */
  357. if (atomic_inc_return(&pool->item_count) <= pool->max_items)
  358. break;
  359. atomic_dec(&pool->item_count);
  360. if (++iter > 2) {
  361. rds_iw_stats_inc(s_iw_rdma_mr_pool_depleted);
  362. return ERR_PTR(-EAGAIN);
  363. }
  364. /* We do have some empty MRs. Flush them out. */
  365. rds_iw_stats_inc(s_iw_rdma_mr_pool_wait);
  366. rds_iw_flush_mr_pool(pool, 0);
  367. }
  368. ibmr = kzalloc(sizeof(*ibmr), GFP_KERNEL);
  369. if (!ibmr) {
  370. err = -ENOMEM;
  371. goto out_no_cigar;
  372. }
  373. spin_lock_init(&ibmr->mapping.m_lock);
  374. INIT_LIST_HEAD(&ibmr->mapping.m_list);
  375. ibmr->mapping.m_mr = ibmr;
  376. err = rds_iw_init_fastreg(pool, ibmr);
  377. if (err)
  378. goto out_no_cigar;
  379. rds_iw_stats_inc(s_iw_rdma_mr_alloc);
  380. return ibmr;
  381. out_no_cigar:
  382. if (ibmr) {
  383. rds_iw_destroy_fastreg(pool, ibmr);
  384. kfree(ibmr);
  385. }
  386. atomic_dec(&pool->item_count);
  387. return ERR_PTR(err);
  388. }
  389. void rds_iw_sync_mr(void *trans_private, int direction)
  390. {
  391. struct rds_iw_mr *ibmr = trans_private;
  392. struct rds_iw_device *rds_iwdev = ibmr->device;
  393. switch (direction) {
  394. case DMA_FROM_DEVICE:
  395. ib_dma_sync_sg_for_cpu(rds_iwdev->dev, ibmr->mapping.m_sg.list,
  396. ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
  397. break;
  398. case DMA_TO_DEVICE:
  399. ib_dma_sync_sg_for_device(rds_iwdev->dev, ibmr->mapping.m_sg.list,
  400. ibmr->mapping.m_sg.dma_len, DMA_BIDIRECTIONAL);
  401. break;
  402. }
  403. }
  404. static inline unsigned int rds_iw_flush_goal(struct rds_iw_mr_pool *pool, int free_all)
  405. {
  406. unsigned int item_count;
  407. item_count = atomic_read(&pool->item_count);
  408. if (free_all)
  409. return item_count;
  410. return 0;
  411. }
  412. /*
  413. * Flush our pool of MRs.
  414. * At a minimum, all currently unused MRs are unmapped.
  415. * If the number of MRs allocated exceeds the limit, we also try
  416. * to free as many MRs as needed to get back to this limit.
  417. */
  418. static int rds_iw_flush_mr_pool(struct rds_iw_mr_pool *pool, int free_all)
  419. {
  420. struct rds_iw_mr *ibmr, *next;
  421. LIST_HEAD(unmap_list);
  422. LIST_HEAD(kill_list);
  423. unsigned long flags;
  424. unsigned int nfreed = 0, ncleaned = 0, free_goal;
  425. int ret = 0;
  426. rds_iw_stats_inc(s_iw_rdma_mr_pool_flush);
  427. mutex_lock(&pool->flush_lock);
  428. spin_lock_irqsave(&pool->list_lock, flags);
  429. /* Get the list of all mappings to be destroyed */
  430. list_splice_init(&pool->dirty_list, &unmap_list);
  431. if (free_all)
  432. list_splice_init(&pool->clean_list, &kill_list);
  433. spin_unlock_irqrestore(&pool->list_lock, flags);
  434. free_goal = rds_iw_flush_goal(pool, free_all);
  435. /* Batched invalidate of dirty MRs.
  436. * For FMR based MRs, the mappings on the unmap list are
  437. * actually members of an ibmr (ibmr->mapping). They either
  438. * migrate to the kill_list, or have been cleaned and should be
  439. * moved to the clean_list.
  440. * For fastregs, they will be dynamically allocated, and
  441. * will be destroyed by the unmap function.
  442. */
  443. if (!list_empty(&unmap_list)) {
  444. ncleaned = rds_iw_unmap_fastreg_list(pool, &unmap_list, &kill_list);
  445. /* If we've been asked to destroy all MRs, move those
  446. * that were simply cleaned to the kill list */
  447. if (free_all)
  448. list_splice_init(&unmap_list, &kill_list);
  449. }
  450. /* Destroy any MRs that are past their best before date */
  451. list_for_each_entry_safe(ibmr, next, &kill_list, mapping.m_list) {
  452. rds_iw_stats_inc(s_iw_rdma_mr_free);
  453. list_del(&ibmr->mapping.m_list);
  454. rds_iw_destroy_fastreg(pool, ibmr);
  455. kfree(ibmr);
  456. nfreed++;
  457. }
  458. /* Anything that remains are laundered ibmrs, which we can add
  459. * back to the clean list. */
  460. if (!list_empty(&unmap_list)) {
  461. spin_lock_irqsave(&pool->list_lock, flags);
  462. list_splice(&unmap_list, &pool->clean_list);
  463. spin_unlock_irqrestore(&pool->list_lock, flags);
  464. }
  465. atomic_sub(ncleaned, &pool->dirty_count);
  466. atomic_sub(nfreed, &pool->item_count);
  467. mutex_unlock(&pool->flush_lock);
  468. return ret;
  469. }
  470. static void rds_iw_mr_pool_flush_worker(struct work_struct *work)
  471. {
  472. struct rds_iw_mr_pool *pool = container_of(work, struct rds_iw_mr_pool, flush_worker);
  473. rds_iw_flush_mr_pool(pool, 0);
  474. }
  475. void rds_iw_free_mr(void *trans_private, int invalidate)
  476. {
  477. struct rds_iw_mr *ibmr = trans_private;
  478. struct rds_iw_mr_pool *pool = ibmr->device->mr_pool;
  479. rdsdebug("RDS/IW: free_mr nents %u\n", ibmr->mapping.m_sg.len);
  480. if (!pool)
  481. return;
  482. /* Return it to the pool's free list */
  483. rds_iw_free_fastreg(pool, ibmr);
  484. /* If we've pinned too many pages, request a flush */
  485. if (atomic_read(&pool->free_pinned) >= pool->max_free_pinned
  486. || atomic_read(&pool->dirty_count) >= pool->max_items / 10)
  487. queue_work(rds_wq, &pool->flush_worker);
  488. if (invalidate) {
  489. if (likely(!in_interrupt())) {
  490. rds_iw_flush_mr_pool(pool, 0);
  491. } else {
  492. /* We get here if the user created a MR marked
  493. * as use_once and invalidate at the same time. */
  494. queue_work(rds_wq, &pool->flush_worker);
  495. }
  496. }
  497. }
  498. void rds_iw_flush_mrs(void)
  499. {
  500. struct rds_iw_device *rds_iwdev;
  501. list_for_each_entry(rds_iwdev, &rds_iw_devices, list) {
  502. struct rds_iw_mr_pool *pool = rds_iwdev->mr_pool;
  503. if (pool)
  504. rds_iw_flush_mr_pool(pool, 0);
  505. }
  506. }
  507. void *rds_iw_get_mr(struct scatterlist *sg, unsigned long nents,
  508. struct rds_sock *rs, u32 *key_ret)
  509. {
  510. struct rds_iw_device *rds_iwdev;
  511. struct rds_iw_mr *ibmr = NULL;
  512. struct rdma_cm_id *cm_id;
  513. int ret;
  514. ret = rds_iw_get_device(rs, &rds_iwdev, &cm_id);
  515. if (ret || !cm_id) {
  516. ret = -ENODEV;
  517. goto out;
  518. }
  519. if (!rds_iwdev->mr_pool) {
  520. ret = -ENODEV;
  521. goto out;
  522. }
  523. ibmr = rds_iw_alloc_mr(rds_iwdev);
  524. if (IS_ERR(ibmr))
  525. return ibmr;
  526. ibmr->cm_id = cm_id;
  527. ibmr->device = rds_iwdev;
  528. ret = rds_iw_map_fastreg(rds_iwdev->mr_pool, ibmr, sg, nents);
  529. if (ret == 0)
  530. *key_ret = ibmr->mr->rkey;
  531. else
  532. printk(KERN_WARNING "RDS/IW: failed to map mr (errno=%d)\n", ret);
  533. out:
  534. if (ret) {
  535. if (ibmr)
  536. rds_iw_free_mr(ibmr, 0);
  537. ibmr = ERR_PTR(ret);
  538. }
  539. return ibmr;
  540. }
  541. /*
  542. * iWARP fastreg handling
  543. *
  544. * The life cycle of a fastreg registration is a bit different from
  545. * FMRs.
  546. * The idea behind fastreg is to have one MR, to which we bind different
  547. * mappings over time. To avoid stalling on the expensive map and invalidate
  548. * operations, these operations are pipelined on the same send queue on
  549. * which we want to send the message containing the r_key.
  550. *
  551. * This creates a bit of a problem for us, as we do not have the destination
  552. * IP in GET_MR, so the connection must be setup prior to the GET_MR call for
  553. * RDMA to be correctly setup. If a fastreg request is present, rds_iw_xmit
  554. * will try to queue a LOCAL_INV (if needed) and a FAST_REG_MR work request
  555. * before queuing the SEND. When completions for these arrive, they are
  556. * dispatched to the MR has a bit set showing that RDMa can be performed.
  557. *
  558. * There is another interesting aspect that's related to invalidation.
  559. * The application can request that a mapping is invalidated in FREE_MR.
  560. * The expectation there is that this invalidation step includes ALL
  561. * PREVIOUSLY FREED MRs.
  562. */
  563. static int rds_iw_init_fastreg(struct rds_iw_mr_pool *pool,
  564. struct rds_iw_mr *ibmr)
  565. {
  566. struct rds_iw_device *rds_iwdev = pool->device;
  567. struct ib_fast_reg_page_list *page_list = NULL;
  568. struct ib_mr *mr;
  569. int err;
  570. mr = ib_alloc_fast_reg_mr(rds_iwdev->pd, pool->max_message_size);
  571. if (IS_ERR(mr)) {
  572. err = PTR_ERR(mr);
  573. printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_mr failed (err=%d)\n", err);
  574. return err;
  575. }
  576. /* FIXME - this is overkill, but mapping->m_sg.dma_len/mapping->m_sg.dma_npages
  577. * is not filled in.
  578. */
  579. page_list = ib_alloc_fast_reg_page_list(rds_iwdev->dev, pool->max_message_size);
  580. if (IS_ERR(page_list)) {
  581. err = PTR_ERR(page_list);
  582. printk(KERN_WARNING "RDS/IW: ib_alloc_fast_reg_page_list failed (err=%d)\n", err);
  583. ib_dereg_mr(mr);
  584. return err;
  585. }
  586. ibmr->page_list = page_list;
  587. ibmr->mr = mr;
  588. return 0;
  589. }
  590. static int rds_iw_rdma_build_fastreg(struct rds_iw_mapping *mapping)
  591. {
  592. struct rds_iw_mr *ibmr = mapping->m_mr;
  593. struct ib_send_wr f_wr, *failed_wr;
  594. int ret;
  595. /*
  596. * Perform a WR for the fast_reg_mr. Each individual page
  597. * in the sg list is added to the fast reg page list and placed
  598. * inside the fast_reg_mr WR. The key used is a rolling 8bit
  599. * counter, which should guarantee uniqueness.
  600. */
  601. ib_update_fast_reg_key(ibmr->mr, ibmr->remap_count++);
  602. mapping->m_rkey = ibmr->mr->rkey;
  603. memset(&f_wr, 0, sizeof(f_wr));
  604. f_wr.wr_id = RDS_IW_FAST_REG_WR_ID;
  605. f_wr.opcode = IB_WR_FAST_REG_MR;
  606. f_wr.wr.fast_reg.length = mapping->m_sg.bytes;
  607. f_wr.wr.fast_reg.rkey = mapping->m_rkey;
  608. f_wr.wr.fast_reg.page_list = ibmr->page_list;
  609. f_wr.wr.fast_reg.page_list_len = mapping->m_sg.dma_len;
  610. f_wr.wr.fast_reg.page_shift = ibmr->device->page_shift;
  611. f_wr.wr.fast_reg.access_flags = IB_ACCESS_LOCAL_WRITE |
  612. IB_ACCESS_REMOTE_READ |
  613. IB_ACCESS_REMOTE_WRITE;
  614. f_wr.wr.fast_reg.iova_start = 0;
  615. f_wr.send_flags = IB_SEND_SIGNALED;
  616. failed_wr = &f_wr;
  617. ret = ib_post_send(ibmr->cm_id->qp, &f_wr, &failed_wr);
  618. BUG_ON(failed_wr != &f_wr);
  619. if (ret && printk_ratelimit())
  620. printk(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
  621. __func__, __LINE__, ret);
  622. return ret;
  623. }
  624. static int rds_iw_rdma_fastreg_inv(struct rds_iw_mr *ibmr)
  625. {
  626. struct ib_send_wr s_wr, *failed_wr;
  627. int ret = 0;
  628. if (!ibmr->cm_id->qp || !ibmr->mr)
  629. goto out;
  630. memset(&s_wr, 0, sizeof(s_wr));
  631. s_wr.wr_id = RDS_IW_LOCAL_INV_WR_ID;
  632. s_wr.opcode = IB_WR_LOCAL_INV;
  633. s_wr.ex.invalidate_rkey = ibmr->mr->rkey;
  634. s_wr.send_flags = IB_SEND_SIGNALED;
  635. failed_wr = &s_wr;
  636. ret = ib_post_send(ibmr->cm_id->qp, &s_wr, &failed_wr);
  637. if (ret && printk_ratelimit()) {
  638. printk(KERN_WARNING "RDS/IW: %s:%d ib_post_send returned %d\n",
  639. __func__, __LINE__, ret);
  640. goto out;
  641. }
  642. out:
  643. return ret;
  644. }
  645. static int rds_iw_map_fastreg(struct rds_iw_mr_pool *pool,
  646. struct rds_iw_mr *ibmr,
  647. struct scatterlist *sg,
  648. unsigned int sg_len)
  649. {
  650. struct rds_iw_device *rds_iwdev = pool->device;
  651. struct rds_iw_mapping *mapping = &ibmr->mapping;
  652. u64 *dma_pages;
  653. int i, ret = 0;
  654. rds_iw_set_scatterlist(&mapping->m_sg, sg, sg_len);
  655. dma_pages = rds_iw_map_scatterlist(rds_iwdev,
  656. &mapping->m_sg,
  657. rds_iwdev->page_shift);
  658. if (IS_ERR(dma_pages)) {
  659. ret = PTR_ERR(dma_pages);
  660. dma_pages = NULL;
  661. goto out;
  662. }
  663. if (mapping->m_sg.dma_len > pool->max_message_size) {
  664. ret = -EMSGSIZE;
  665. goto out;
  666. }
  667. for (i = 0; i < mapping->m_sg.dma_npages; ++i)
  668. ibmr->page_list->page_list[i] = dma_pages[i];
  669. ret = rds_iw_rdma_build_fastreg(mapping);
  670. if (ret)
  671. goto out;
  672. rds_iw_stats_inc(s_iw_rdma_mr_used);
  673. out:
  674. kfree(dma_pages);
  675. return ret;
  676. }
  677. /*
  678. * "Free" a fastreg MR.
  679. */
  680. static void rds_iw_free_fastreg(struct rds_iw_mr_pool *pool,
  681. struct rds_iw_mr *ibmr)
  682. {
  683. unsigned long flags;
  684. int ret;
  685. if (!ibmr->mapping.m_sg.dma_len)
  686. return;
  687. ret = rds_iw_rdma_fastreg_inv(ibmr);
  688. if (ret)
  689. return;
  690. /* Try to post the LOCAL_INV WR to the queue. */
  691. spin_lock_irqsave(&pool->list_lock, flags);
  692. list_add_tail(&ibmr->mapping.m_list, &pool->dirty_list);
  693. atomic_add(ibmr->mapping.m_sg.len, &pool->free_pinned);
  694. atomic_inc(&pool->dirty_count);
  695. spin_unlock_irqrestore(&pool->list_lock, flags);
  696. }
  697. static unsigned int rds_iw_unmap_fastreg_list(struct rds_iw_mr_pool *pool,
  698. struct list_head *unmap_list,
  699. struct list_head *kill_list)
  700. {
  701. struct rds_iw_mapping *mapping, *next;
  702. unsigned int ncleaned = 0;
  703. LIST_HEAD(laundered);
  704. /* Batched invalidation of fastreg MRs.
  705. * Why do we do it this way, even though we could pipeline unmap
  706. * and remap? The reason is the application semantics - when the
  707. * application requests an invalidation of MRs, it expects all
  708. * previously released R_Keys to become invalid.
  709. *
  710. * If we implement MR reuse naively, we risk memory corruption
  711. * (this has actually been observed). So the default behavior
  712. * requires that a MR goes through an explicit unmap operation before
  713. * we can reuse it again.
  714. *
  715. * We could probably improve on this a little, by allowing immediate
  716. * reuse of a MR on the same socket (eg you could add small
  717. * cache of unused MRs to strct rds_socket - GET_MR could grab one
  718. * of these without requiring an explicit invalidate).
  719. */
  720. while (!list_empty(unmap_list)) {
  721. unsigned long flags;
  722. spin_lock_irqsave(&pool->list_lock, flags);
  723. list_for_each_entry_safe(mapping, next, unmap_list, m_list) {
  724. list_move(&mapping->m_list, &laundered);
  725. ncleaned++;
  726. }
  727. spin_unlock_irqrestore(&pool->list_lock, flags);
  728. }
  729. /* Move all laundered mappings back to the unmap list.
  730. * We do not kill any WRs right now - it doesn't seem the
  731. * fastreg API has a max_remap limit. */
  732. list_splice_init(&laundered, unmap_list);
  733. return ncleaned;
  734. }
  735. static void rds_iw_destroy_fastreg(struct rds_iw_mr_pool *pool,
  736. struct rds_iw_mr *ibmr)
  737. {
  738. if (ibmr->page_list)
  739. ib_free_fast_reg_page_list(ibmr->page_list);
  740. if (ibmr->mr)
  741. ib_dereg_mr(ibmr->mr);
  742. }