c2_mm.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
  3. * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/slab.h>
  34. #include "c2.h"
  35. #include "c2_vq.h"
  36. #define PBL_VIRT 1
  37. #define PBL_PHYS 2
  38. /*
  39. * Send all the PBL messages to convey the remainder of the PBL
  40. * Wait for the adapter's reply on the last one.
  41. * This is indicated by setting the MEM_PBL_COMPLETE in the flags.
  42. *
  43. * NOTE: vq_req is _not_ freed by this function. The VQ Host
  44. * Reply buffer _is_ freed by this function.
  45. */
  46. static int
  47. send_pbl_messages(struct c2_dev *c2dev, __be32 stag_index,
  48. unsigned long va, u32 pbl_depth,
  49. struct c2_vq_req *vq_req, int pbl_type)
  50. {
  51. u32 pbe_count; /* amt that fits in a PBL msg */
  52. u32 count; /* amt in this PBL MSG. */
  53. struct c2wr_nsmr_pbl_req *wr; /* PBL WR ptr */
  54. struct c2wr_nsmr_pbl_rep *reply; /* reply ptr */
  55. int err, pbl_virt, pbl_index, i;
  56. switch (pbl_type) {
  57. case PBL_VIRT:
  58. pbl_virt = 1;
  59. break;
  60. case PBL_PHYS:
  61. pbl_virt = 0;
  62. break;
  63. default:
  64. return -EINVAL;
  65. break;
  66. }
  67. pbe_count = (c2dev->req_vq.msg_size -
  68. sizeof(struct c2wr_nsmr_pbl_req)) / sizeof(u64);
  69. wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
  70. if (!wr) {
  71. return -ENOMEM;
  72. }
  73. c2_wr_set_id(wr, CCWR_NSMR_PBL);
  74. /*
  75. * Only the last PBL message will generate a reply from the verbs,
  76. * so we set the context to 0 indicating there is no kernel verbs
  77. * handler blocked awaiting this reply.
  78. */
  79. wr->hdr.context = 0;
  80. wr->rnic_handle = c2dev->adapter_handle;
  81. wr->stag_index = stag_index; /* already swapped */
  82. wr->flags = 0;
  83. pbl_index = 0;
  84. while (pbl_depth) {
  85. count = min(pbe_count, pbl_depth);
  86. wr->addrs_length = cpu_to_be32(count);
  87. /*
  88. * If this is the last message, then reference the
  89. * vq request struct cuz we're gonna wait for a reply.
  90. * also make this PBL msg as the last one.
  91. */
  92. if (count == pbl_depth) {
  93. /*
  94. * reference the request struct. dereferenced in the
  95. * int handler.
  96. */
  97. vq_req_get(c2dev, vq_req);
  98. wr->flags = cpu_to_be32(MEM_PBL_COMPLETE);
  99. /*
  100. * This is the last PBL message.
  101. * Set the context to our VQ Request Object so we can
  102. * wait for the reply.
  103. */
  104. wr->hdr.context = (unsigned long) vq_req;
  105. }
  106. /*
  107. * If pbl_virt is set then va is a virtual address
  108. * that describes a virtually contiguous memory
  109. * allocation. The wr needs the start of each virtual page
  110. * to be converted to the corresponding physical address
  111. * of the page. If pbl_virt is not set then va is an array
  112. * of physical addresses and there is no conversion to do.
  113. * Just fill in the wr with what is in the array.
  114. */
  115. for (i = 0; i < count; i++) {
  116. if (pbl_virt) {
  117. va += PAGE_SIZE;
  118. } else {
  119. wr->paddrs[i] =
  120. cpu_to_be64(((u64 *)va)[pbl_index + i]);
  121. }
  122. }
  123. /*
  124. * Send WR to adapter
  125. */
  126. err = vq_send_wr(c2dev, (union c2wr *) wr);
  127. if (err) {
  128. if (count <= pbe_count) {
  129. vq_req_put(c2dev, vq_req);
  130. }
  131. goto bail0;
  132. }
  133. pbl_depth -= count;
  134. pbl_index += count;
  135. }
  136. /*
  137. * Now wait for the reply...
  138. */
  139. err = vq_wait_for_reply(c2dev, vq_req);
  140. if (err) {
  141. goto bail0;
  142. }
  143. /*
  144. * Process reply
  145. */
  146. reply = (struct c2wr_nsmr_pbl_rep *) (unsigned long) vq_req->reply_msg;
  147. if (!reply) {
  148. err = -ENOMEM;
  149. goto bail0;
  150. }
  151. err = c2_errno(reply);
  152. vq_repbuf_free(c2dev, reply);
  153. bail0:
  154. kfree(wr);
  155. return err;
  156. }
  157. #define C2_PBL_MAX_DEPTH 131072
  158. int
  159. c2_nsmr_register_phys_kern(struct c2_dev *c2dev, u64 *addr_list,
  160. int page_size, int pbl_depth, u32 length,
  161. u32 offset, u64 *va, enum c2_acf acf,
  162. struct c2_mr *mr)
  163. {
  164. struct c2_vq_req *vq_req;
  165. struct c2wr_nsmr_register_req *wr;
  166. struct c2wr_nsmr_register_rep *reply;
  167. u16 flags;
  168. int i, pbe_count, count;
  169. int err;
  170. if (!va || !length || !addr_list || !pbl_depth)
  171. return -EINTR;
  172. /*
  173. * Verify PBL depth is within rnic max
  174. */
  175. if (pbl_depth > C2_PBL_MAX_DEPTH) {
  176. return -EINTR;
  177. }
  178. /*
  179. * allocate verbs request object
  180. */
  181. vq_req = vq_req_alloc(c2dev);
  182. if (!vq_req)
  183. return -ENOMEM;
  184. wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
  185. if (!wr) {
  186. err = -ENOMEM;
  187. goto bail0;
  188. }
  189. /*
  190. * build the WR
  191. */
  192. c2_wr_set_id(wr, CCWR_NSMR_REGISTER);
  193. wr->hdr.context = (unsigned long) vq_req;
  194. wr->rnic_handle = c2dev->adapter_handle;
  195. flags = (acf | MEM_VA_BASED | MEM_REMOTE);
  196. /*
  197. * compute how many pbes can fit in the message
  198. */
  199. pbe_count = (c2dev->req_vq.msg_size -
  200. sizeof(struct c2wr_nsmr_register_req)) / sizeof(u64);
  201. if (pbl_depth <= pbe_count) {
  202. flags |= MEM_PBL_COMPLETE;
  203. }
  204. wr->flags = cpu_to_be16(flags);
  205. wr->stag_key = 0; //stag_key;
  206. wr->va = cpu_to_be64(*va);
  207. wr->pd_id = mr->pd->pd_id;
  208. wr->pbe_size = cpu_to_be32(page_size);
  209. wr->length = cpu_to_be32(length);
  210. wr->pbl_depth = cpu_to_be32(pbl_depth);
  211. wr->fbo = cpu_to_be32(offset);
  212. count = min(pbl_depth, pbe_count);
  213. wr->addrs_length = cpu_to_be32(count);
  214. /*
  215. * fill out the PBL for this message
  216. */
  217. for (i = 0; i < count; i++) {
  218. wr->paddrs[i] = cpu_to_be64(addr_list[i]);
  219. }
  220. /*
  221. * regerence the request struct
  222. */
  223. vq_req_get(c2dev, vq_req);
  224. /*
  225. * send the WR to the adapter
  226. */
  227. err = vq_send_wr(c2dev, (union c2wr *) wr);
  228. if (err) {
  229. vq_req_put(c2dev, vq_req);
  230. goto bail1;
  231. }
  232. /*
  233. * wait for reply from adapter
  234. */
  235. err = vq_wait_for_reply(c2dev, vq_req);
  236. if (err) {
  237. goto bail1;
  238. }
  239. /*
  240. * process reply
  241. */
  242. reply =
  243. (struct c2wr_nsmr_register_rep *) (unsigned long) (vq_req->reply_msg);
  244. if (!reply) {
  245. err = -ENOMEM;
  246. goto bail1;
  247. }
  248. if ((err = c2_errno(reply))) {
  249. goto bail2;
  250. }
  251. //*p_pb_entries = be32_to_cpu(reply->pbl_depth);
  252. mr->ibmr.lkey = mr->ibmr.rkey = be32_to_cpu(reply->stag_index);
  253. vq_repbuf_free(c2dev, reply);
  254. /*
  255. * if there are still more PBEs we need to send them to
  256. * the adapter and wait for a reply on the final one.
  257. * reuse vq_req for this purpose.
  258. */
  259. pbl_depth -= count;
  260. if (pbl_depth) {
  261. vq_req->reply_msg = (unsigned long) NULL;
  262. atomic_set(&vq_req->reply_ready, 0);
  263. err = send_pbl_messages(c2dev,
  264. cpu_to_be32(mr->ibmr.lkey),
  265. (unsigned long) &addr_list[i],
  266. pbl_depth, vq_req, PBL_PHYS);
  267. if (err) {
  268. goto bail1;
  269. }
  270. }
  271. vq_req_free(c2dev, vq_req);
  272. kfree(wr);
  273. return err;
  274. bail2:
  275. vq_repbuf_free(c2dev, reply);
  276. bail1:
  277. kfree(wr);
  278. bail0:
  279. vq_req_free(c2dev, vq_req);
  280. return err;
  281. }
  282. int c2_stag_dealloc(struct c2_dev *c2dev, u32 stag_index)
  283. {
  284. struct c2_vq_req *vq_req; /* verbs request object */
  285. struct c2wr_stag_dealloc_req wr; /* work request */
  286. struct c2wr_stag_dealloc_rep *reply; /* WR reply */
  287. int err;
  288. /*
  289. * allocate verbs request object
  290. */
  291. vq_req = vq_req_alloc(c2dev);
  292. if (!vq_req) {
  293. return -ENOMEM;
  294. }
  295. /*
  296. * Build the WR
  297. */
  298. c2_wr_set_id(&wr, CCWR_STAG_DEALLOC);
  299. wr.hdr.context = (u64) (unsigned long) vq_req;
  300. wr.rnic_handle = c2dev->adapter_handle;
  301. wr.stag_index = cpu_to_be32(stag_index);
  302. /*
  303. * reference the request struct. dereferenced in the int handler.
  304. */
  305. vq_req_get(c2dev, vq_req);
  306. /*
  307. * Send WR to adapter
  308. */
  309. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  310. if (err) {
  311. vq_req_put(c2dev, vq_req);
  312. goto bail0;
  313. }
  314. /*
  315. * Wait for reply from adapter
  316. */
  317. err = vq_wait_for_reply(c2dev, vq_req);
  318. if (err) {
  319. goto bail0;
  320. }
  321. /*
  322. * Process reply
  323. */
  324. reply = (struct c2wr_stag_dealloc_rep *) (unsigned long) vq_req->reply_msg;
  325. if (!reply) {
  326. err = -ENOMEM;
  327. goto bail0;
  328. }
  329. err = c2_errno(reply);
  330. vq_repbuf_free(c2dev, reply);
  331. bail0:
  332. vq_req_free(c2dev, vq_req);
  333. return err;
  334. }