c2_mm.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "c2.h"
  34. #include "c2_vq.h"
  35. #define PBL_VIRT 1
  36. #define PBL_PHYS 2
  37. /*
  38. * Send all the PBL messages to convey the remainder of the PBL
  39. * Wait for the adapter's reply on the last one.
  40. * This is indicated by setting the MEM_PBL_COMPLETE in the flags.
  41. *
  42. * NOTE: vq_req is _not_ freed by this function. The VQ Host
  43. * Reply buffer _is_ freed by this function.
  44. */
  45. static int
  46. send_pbl_messages(struct c2_dev *c2dev, __be32 stag_index,
  47. unsigned long va, u32 pbl_depth,
  48. struct c2_vq_req *vq_req, int pbl_type)
  49. {
  50. u32 pbe_count; /* amt that fits in a PBL msg */
  51. u32 count; /* amt in this PBL MSG. */
  52. struct c2wr_nsmr_pbl_req *wr; /* PBL WR ptr */
  53. struct c2wr_nsmr_pbl_rep *reply; /* reply ptr */
  54. int err, pbl_virt, pbl_index, i;
  55. switch (pbl_type) {
  56. case PBL_VIRT:
  57. pbl_virt = 1;
  58. break;
  59. case PBL_PHYS:
  60. pbl_virt = 0;
  61. break;
  62. default:
  63. return -EINVAL;
  64. break;
  65. }
  66. pbe_count = (c2dev->req_vq.msg_size -
  67. sizeof(struct c2wr_nsmr_pbl_req)) / sizeof(u64);
  68. wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
  69. if (!wr) {
  70. return -ENOMEM;
  71. }
  72. c2_wr_set_id(wr, CCWR_NSMR_PBL);
  73. /*
  74. * Only the last PBL message will generate a reply from the verbs,
  75. * so we set the context to 0 indicating there is no kernel verbs
  76. * handler blocked awaiting this reply.
  77. */
  78. wr->hdr.context = 0;
  79. wr->rnic_handle = c2dev->adapter_handle;
  80. wr->stag_index = stag_index; /* already swapped */
  81. wr->flags = 0;
  82. pbl_index = 0;
  83. while (pbl_depth) {
  84. count = min(pbe_count, pbl_depth);
  85. wr->addrs_length = cpu_to_be32(count);
  86. /*
  87. * If this is the last message, then reference the
  88. * vq request struct cuz we're gonna wait for a reply.
  89. * also make this PBL msg as the last one.
  90. */
  91. if (count == pbl_depth) {
  92. /*
  93. * reference the request struct. dereferenced in the
  94. * int handler.
  95. */
  96. vq_req_get(c2dev, vq_req);
  97. wr->flags = cpu_to_be32(MEM_PBL_COMPLETE);
  98. /*
  99. * This is the last PBL message.
  100. * Set the context to our VQ Request Object so we can
  101. * wait for the reply.
  102. */
  103. wr->hdr.context = (unsigned long) vq_req;
  104. }
  105. /*
  106. * If pbl_virt is set then va is a virtual address
  107. * that describes a virtually contiguous memory
  108. * allocation. The wr needs the start of each virtual page
  109. * to be converted to the corresponding physical address
  110. * of the page. If pbl_virt is not set then va is an array
  111. * of physical addresses and there is no conversion to do.
  112. * Just fill in the wr with what is in the array.
  113. */
  114. for (i = 0; i < count; i++) {
  115. if (pbl_virt) {
  116. va += PAGE_SIZE;
  117. } else {
  118. wr->paddrs[i] =
  119. cpu_to_be64(((u64 *)va)[pbl_index + i]);
  120. }
  121. }
  122. /*
  123. * Send WR to adapter
  124. */
  125. err = vq_send_wr(c2dev, (union c2wr *) wr);
  126. if (err) {
  127. if (count <= pbe_count) {
  128. vq_req_put(c2dev, vq_req);
  129. }
  130. goto bail0;
  131. }
  132. pbl_depth -= count;
  133. pbl_index += count;
  134. }
  135. /*
  136. * Now wait for the reply...
  137. */
  138. err = vq_wait_for_reply(c2dev, vq_req);
  139. if (err) {
  140. goto bail0;
  141. }
  142. /*
  143. * Process reply
  144. */
  145. reply = (struct c2wr_nsmr_pbl_rep *) (unsigned long) vq_req->reply_msg;
  146. if (!reply) {
  147. err = -ENOMEM;
  148. goto bail0;
  149. }
  150. err = c2_errno(reply);
  151. vq_repbuf_free(c2dev, reply);
  152. bail0:
  153. kfree(wr);
  154. return err;
  155. }
  156. #define C2_PBL_MAX_DEPTH 131072
  157. int
  158. c2_nsmr_register_phys_kern(struct c2_dev *c2dev, u64 *addr_list,
  159. int page_size, int pbl_depth, u32 length,
  160. u32 offset, u64 *va, enum c2_acf acf,
  161. struct c2_mr *mr)
  162. {
  163. struct c2_vq_req *vq_req;
  164. struct c2wr_nsmr_register_req *wr;
  165. struct c2wr_nsmr_register_rep *reply;
  166. u16 flags;
  167. int i, pbe_count, count;
  168. int err;
  169. if (!va || !length || !addr_list || !pbl_depth)
  170. return -EINTR;
  171. /*
  172. * Verify PBL depth is within rnic max
  173. */
  174. if (pbl_depth > C2_PBL_MAX_DEPTH) {
  175. return -EINTR;
  176. }
  177. /*
  178. * allocate verbs request object
  179. */
  180. vq_req = vq_req_alloc(c2dev);
  181. if (!vq_req)
  182. return -ENOMEM;
  183. wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
  184. if (!wr) {
  185. err = -ENOMEM;
  186. goto bail0;
  187. }
  188. /*
  189. * build the WR
  190. */
  191. c2_wr_set_id(wr, CCWR_NSMR_REGISTER);
  192. wr->hdr.context = (unsigned long) vq_req;
  193. wr->rnic_handle = c2dev->adapter_handle;
  194. flags = (acf | MEM_VA_BASED | MEM_REMOTE);
  195. /*
  196. * compute how many pbes can fit in the message
  197. */
  198. pbe_count = (c2dev->req_vq.msg_size -
  199. sizeof(struct c2wr_nsmr_register_req)) / sizeof(u64);
  200. if (pbl_depth <= pbe_count) {
  201. flags |= MEM_PBL_COMPLETE;
  202. }
  203. wr->flags = cpu_to_be16(flags);
  204. wr->stag_key = 0; //stag_key;
  205. wr->va = cpu_to_be64(*va);
  206. wr->pd_id = mr->pd->pd_id;
  207. wr->pbe_size = cpu_to_be32(page_size);
  208. wr->length = cpu_to_be32(length);
  209. wr->pbl_depth = cpu_to_be32(pbl_depth);
  210. wr->fbo = cpu_to_be32(offset);
  211. count = min(pbl_depth, pbe_count);
  212. wr->addrs_length = cpu_to_be32(count);
  213. /*
  214. * fill out the PBL for this message
  215. */
  216. for (i = 0; i < count; i++) {
  217. wr->paddrs[i] = cpu_to_be64(addr_list[i]);
  218. }
  219. /*
  220. * regerence the request struct
  221. */
  222. vq_req_get(c2dev, vq_req);
  223. /*
  224. * send the WR to the adapter
  225. */
  226. err = vq_send_wr(c2dev, (union c2wr *) wr);
  227. if (err) {
  228. vq_req_put(c2dev, vq_req);
  229. goto bail1;
  230. }
  231. /*
  232. * wait for reply from adapter
  233. */
  234. err = vq_wait_for_reply(c2dev, vq_req);
  235. if (err) {
  236. goto bail1;
  237. }
  238. /*
  239. * process reply
  240. */
  241. reply =
  242. (struct c2wr_nsmr_register_rep *) (unsigned long) (vq_req->reply_msg);
  243. if (!reply) {
  244. err = -ENOMEM;
  245. goto bail1;
  246. }
  247. if ((err = c2_errno(reply))) {
  248. goto bail2;
  249. }
  250. //*p_pb_entries = be32_to_cpu(reply->pbl_depth);
  251. mr->ibmr.lkey = mr->ibmr.rkey = be32_to_cpu(reply->stag_index);
  252. vq_repbuf_free(c2dev, reply);
  253. /*
  254. * if there are still more PBEs we need to send them to
  255. * the adapter and wait for a reply on the final one.
  256. * reuse vq_req for this purpose.
  257. */
  258. pbl_depth -= count;
  259. if (pbl_depth) {
  260. vq_req->reply_msg = (unsigned long) NULL;
  261. atomic_set(&vq_req->reply_ready, 0);
  262. err = send_pbl_messages(c2dev,
  263. cpu_to_be32(mr->ibmr.lkey),
  264. (unsigned long) &addr_list[i],
  265. pbl_depth, vq_req, PBL_PHYS);
  266. if (err) {
  267. goto bail1;
  268. }
  269. }
  270. vq_req_free(c2dev, vq_req);
  271. kfree(wr);
  272. return err;
  273. bail2:
  274. vq_repbuf_free(c2dev, reply);
  275. bail1:
  276. kfree(wr);
  277. bail0:
  278. vq_req_free(c2dev, vq_req);
  279. return err;
  280. }
  281. int c2_stag_dealloc(struct c2_dev *c2dev, u32 stag_index)
  282. {
  283. struct c2_vq_req *vq_req; /* verbs request object */
  284. struct c2wr_stag_dealloc_req wr; /* work request */
  285. struct c2wr_stag_dealloc_rep *reply; /* WR reply */
  286. int err;
  287. /*
  288. * allocate verbs request object
  289. */
  290. vq_req = vq_req_alloc(c2dev);
  291. if (!vq_req) {
  292. return -ENOMEM;
  293. }
  294. /*
  295. * Build the WR
  296. */
  297. c2_wr_set_id(&wr, CCWR_STAG_DEALLOC);
  298. wr.hdr.context = (u64) (unsigned long) vq_req;
  299. wr.rnic_handle = c2dev->adapter_handle;
  300. wr.stag_index = cpu_to_be32(stag_index);
  301. /*
  302. * reference the request struct. dereferenced in the int handler.
  303. */
  304. vq_req_get(c2dev, vq_req);
  305. /*
  306. * Send WR to adapter
  307. */
  308. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  309. if (err) {
  310. vq_req_put(c2dev, vq_req);
  311. goto bail0;
  312. }
  313. /*
  314. * Wait for reply from adapter
  315. */
  316. err = vq_wait_for_reply(c2dev, vq_req);
  317. if (err) {
  318. goto bail0;
  319. }
  320. /*
  321. * Process reply
  322. */
  323. reply = (struct c2wr_stag_dealloc_rep *) (unsigned long) vq_req->reply_msg;
  324. if (!reply) {
  325. err = -ENOMEM;
  326. goto bail0;
  327. }
  328. err = c2_errno(reply);
  329. vq_repbuf_free(c2dev, reply);
  330. bail0:
  331. vq_req_free(c2dev, vq_req);
  332. return err;
  333. }