vt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. * Copyright(c) 2016 - 2018 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/module.h>
  48. #include <linux/kernel.h>
  49. #include <linux/dma-mapping.h>
  50. #include "vt.h"
  51. #include "cq.h"
  52. #include "trace.h"
  53. #define RVT_UVERBS_ABI_VERSION 2
  54. MODULE_LICENSE("Dual BSD/GPL");
  55. MODULE_DESCRIPTION("RDMA Verbs Transport Library");
  56. static int rvt_init(void)
  57. {
  58. int ret = rvt_driver_cq_init();
  59. if (ret)
  60. pr_err("Error in driver CQ init.\n");
  61. return ret;
  62. }
  63. module_init(rvt_init);
  64. static void rvt_cleanup(void)
  65. {
  66. rvt_cq_exit();
  67. }
  68. module_exit(rvt_cleanup);
  69. /**
  70. * rvt_alloc_device - allocate rdi
  71. * @size: how big of a structure to allocate
  72. * @nports: number of ports to allocate array slots for
  73. *
  74. * Use IB core device alloc to allocate space for the rdi which is assumed to be
  75. * inside of the ib_device. Any extra space that drivers require should be
  76. * included in size.
  77. *
  78. * We also allocate a port array based on the number of ports.
  79. *
  80. * Return: pointer to allocated rdi
  81. */
  82. struct rvt_dev_info *rvt_alloc_device(size_t size, int nports)
  83. {
  84. struct rvt_dev_info *rdi;
  85. rdi = (struct rvt_dev_info *)ib_alloc_device(size);
  86. if (!rdi)
  87. return rdi;
  88. rdi->ports = kcalloc(nports,
  89. sizeof(struct rvt_ibport **),
  90. GFP_KERNEL);
  91. if (!rdi->ports)
  92. ib_dealloc_device(&rdi->ibdev);
  93. return rdi;
  94. }
  95. EXPORT_SYMBOL(rvt_alloc_device);
  96. /**
  97. * rvt_dealloc_device - deallocate rdi
  98. * @rdi: structure to free
  99. *
  100. * Free a structure allocated with rvt_alloc_device()
  101. */
  102. void rvt_dealloc_device(struct rvt_dev_info *rdi)
  103. {
  104. kfree(rdi->ports);
  105. ib_dealloc_device(&rdi->ibdev);
  106. }
  107. EXPORT_SYMBOL(rvt_dealloc_device);
  108. static int rvt_query_device(struct ib_device *ibdev,
  109. struct ib_device_attr *props,
  110. struct ib_udata *uhw)
  111. {
  112. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  113. if (uhw->inlen || uhw->outlen)
  114. return -EINVAL;
  115. /*
  116. * Return rvt_dev_info.dparms.props contents
  117. */
  118. *props = rdi->dparms.props;
  119. return 0;
  120. }
  121. static int rvt_modify_device(struct ib_device *device,
  122. int device_modify_mask,
  123. struct ib_device_modify *device_modify)
  124. {
  125. /*
  126. * There is currently no need to supply this based on qib and hfi1.
  127. * Future drivers may need to implement this though.
  128. */
  129. return -EOPNOTSUPP;
  130. }
  131. /**
  132. * rvt_query_port: Passes the query port call to the driver
  133. * @ibdev: Verbs IB dev
  134. * @port_num: port number, 1 based from ib core
  135. * @props: structure to hold returned properties
  136. *
  137. * Return: 0 on success
  138. */
  139. static int rvt_query_port(struct ib_device *ibdev, u8 port_num,
  140. struct ib_port_attr *props)
  141. {
  142. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  143. struct rvt_ibport *rvp;
  144. int port_index = ibport_num_to_idx(ibdev, port_num);
  145. if (port_index < 0)
  146. return -EINVAL;
  147. rvp = rdi->ports[port_index];
  148. /* props being zeroed by the caller, avoid zeroing it here */
  149. props->sm_lid = rvp->sm_lid;
  150. props->sm_sl = rvp->sm_sl;
  151. props->port_cap_flags = rvp->port_cap_flags;
  152. props->max_msg_sz = 0x80000000;
  153. props->pkey_tbl_len = rvt_get_npkeys(rdi);
  154. props->bad_pkey_cntr = rvp->pkey_violations;
  155. props->qkey_viol_cntr = rvp->qkey_violations;
  156. props->subnet_timeout = rvp->subnet_timeout;
  157. props->init_type_reply = 0;
  158. /* Populate the remaining ib_port_attr elements */
  159. return rdi->driver_f.query_port_state(rdi, port_num, props);
  160. }
  161. /**
  162. * rvt_modify_port
  163. * @ibdev: Verbs IB dev
  164. * @port_num: Port number, 1 based from ib core
  165. * @port_modify_mask: How to change the port
  166. * @props: Structure to fill in
  167. *
  168. * Return: 0 on success
  169. */
  170. static int rvt_modify_port(struct ib_device *ibdev, u8 port_num,
  171. int port_modify_mask, struct ib_port_modify *props)
  172. {
  173. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  174. struct rvt_ibport *rvp;
  175. int ret = 0;
  176. int port_index = ibport_num_to_idx(ibdev, port_num);
  177. if (port_index < 0)
  178. return -EINVAL;
  179. rvp = rdi->ports[port_index];
  180. if (port_modify_mask & IB_PORT_OPA_MASK_CHG) {
  181. rvp->port_cap3_flags |= props->set_port_cap_mask;
  182. rvp->port_cap3_flags &= ~props->clr_port_cap_mask;
  183. } else {
  184. rvp->port_cap_flags |= props->set_port_cap_mask;
  185. rvp->port_cap_flags &= ~props->clr_port_cap_mask;
  186. }
  187. if (props->set_port_cap_mask || props->clr_port_cap_mask)
  188. rdi->driver_f.cap_mask_chg(rdi, port_num);
  189. if (port_modify_mask & IB_PORT_SHUTDOWN)
  190. ret = rdi->driver_f.shut_down_port(rdi, port_num);
  191. if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
  192. rvp->qkey_violations = 0;
  193. return ret;
  194. }
  195. /**
  196. * rvt_query_pkey - Return a pkey from the table at a given index
  197. * @ibdev: Verbs IB dev
  198. * @port_num: Port number, 1 based from ib core
  199. * @index: Index into pkey table
  200. * @pkey: returned pkey from the port pkey table
  201. *
  202. * Return: 0 on failure pkey otherwise
  203. */
  204. static int rvt_query_pkey(struct ib_device *ibdev, u8 port_num, u16 index,
  205. u16 *pkey)
  206. {
  207. /*
  208. * Driver will be responsible for keeping rvt_dev_info.pkey_table up to
  209. * date. This function will just return that value. There is no need to
  210. * lock, if a stale value is read and sent to the user so be it there is
  211. * no way to protect against that anyway.
  212. */
  213. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  214. int port_index;
  215. port_index = ibport_num_to_idx(ibdev, port_num);
  216. if (port_index < 0)
  217. return -EINVAL;
  218. if (index >= rvt_get_npkeys(rdi))
  219. return -EINVAL;
  220. *pkey = rvt_get_pkey(rdi, port_index, index);
  221. return 0;
  222. }
  223. /**
  224. * rvt_query_gid - Return a gid from the table
  225. * @ibdev: Verbs IB dev
  226. * @port_num: Port number, 1 based from ib core
  227. * @guid_index: Index in table
  228. * @gid: Gid to return
  229. *
  230. * Return: 0 on success
  231. */
  232. static int rvt_query_gid(struct ib_device *ibdev, u8 port_num,
  233. int guid_index, union ib_gid *gid)
  234. {
  235. struct rvt_dev_info *rdi;
  236. struct rvt_ibport *rvp;
  237. int port_index;
  238. /*
  239. * Driver is responsible for updating the guid table. Which will be used
  240. * to craft the return value. This will work similar to how query_pkey()
  241. * is being done.
  242. */
  243. port_index = ibport_num_to_idx(ibdev, port_num);
  244. if (port_index < 0)
  245. return -EINVAL;
  246. rdi = ib_to_rvt(ibdev);
  247. rvp = rdi->ports[port_index];
  248. gid->global.subnet_prefix = rvp->gid_prefix;
  249. return rdi->driver_f.get_guid_be(rdi, rvp, guid_index,
  250. &gid->global.interface_id);
  251. }
  252. struct rvt_ucontext {
  253. struct ib_ucontext ibucontext;
  254. };
  255. static inline struct rvt_ucontext *to_iucontext(struct ib_ucontext
  256. *ibucontext)
  257. {
  258. return container_of(ibucontext, struct rvt_ucontext, ibucontext);
  259. }
  260. /**
  261. * rvt_alloc_ucontext - Allocate a user context
  262. * @ibdev: Verbs IB dev
  263. * @udata: User data allocated
  264. */
  265. static struct ib_ucontext *rvt_alloc_ucontext(struct ib_device *ibdev,
  266. struct ib_udata *udata)
  267. {
  268. struct rvt_ucontext *context;
  269. context = kmalloc(sizeof(*context), GFP_KERNEL);
  270. if (!context)
  271. return ERR_PTR(-ENOMEM);
  272. return &context->ibucontext;
  273. }
  274. /**
  275. *rvt_dealloc_ucontext - Free a user context
  276. *@context - Free this
  277. */
  278. static int rvt_dealloc_ucontext(struct ib_ucontext *context)
  279. {
  280. kfree(to_iucontext(context));
  281. return 0;
  282. }
  283. static int rvt_get_port_immutable(struct ib_device *ibdev, u8 port_num,
  284. struct ib_port_immutable *immutable)
  285. {
  286. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  287. struct ib_port_attr attr;
  288. int err, port_index;
  289. port_index = ibport_num_to_idx(ibdev, port_num);
  290. if (port_index < 0)
  291. return -EINVAL;
  292. immutable->core_cap_flags = rdi->dparms.core_cap_flags;
  293. err = ib_query_port(ibdev, port_num, &attr);
  294. if (err)
  295. return err;
  296. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  297. immutable->gid_tbl_len = attr.gid_tbl_len;
  298. immutable->max_mad_size = rdi->dparms.max_mad_size;
  299. return 0;
  300. }
  301. enum {
  302. MISC,
  303. QUERY_DEVICE,
  304. MODIFY_DEVICE,
  305. QUERY_PORT,
  306. MODIFY_PORT,
  307. QUERY_PKEY,
  308. QUERY_GID,
  309. ALLOC_UCONTEXT,
  310. DEALLOC_UCONTEXT,
  311. GET_PORT_IMMUTABLE,
  312. CREATE_QP,
  313. MODIFY_QP,
  314. DESTROY_QP,
  315. QUERY_QP,
  316. POST_SEND,
  317. POST_RECV,
  318. POST_SRQ_RECV,
  319. CREATE_AH,
  320. DESTROY_AH,
  321. MODIFY_AH,
  322. QUERY_AH,
  323. CREATE_SRQ,
  324. MODIFY_SRQ,
  325. DESTROY_SRQ,
  326. QUERY_SRQ,
  327. ATTACH_MCAST,
  328. DETACH_MCAST,
  329. GET_DMA_MR,
  330. REG_USER_MR,
  331. DEREG_MR,
  332. ALLOC_MR,
  333. MAP_MR_SG,
  334. ALLOC_FMR,
  335. MAP_PHYS_FMR,
  336. UNMAP_FMR,
  337. DEALLOC_FMR,
  338. MMAP,
  339. CREATE_CQ,
  340. DESTROY_CQ,
  341. POLL_CQ,
  342. REQ_NOTFIY_CQ,
  343. RESIZE_CQ,
  344. ALLOC_PD,
  345. DEALLOC_PD,
  346. _VERB_IDX_MAX /* Must always be last! */
  347. };
  348. static inline int check_driver_override(struct rvt_dev_info *rdi,
  349. size_t offset, void *func)
  350. {
  351. if (!*(void **)((void *)&rdi->ibdev + offset)) {
  352. *(void **)((void *)&rdi->ibdev + offset) = func;
  353. return 0;
  354. }
  355. return 1;
  356. }
  357. static noinline int check_support(struct rvt_dev_info *rdi, int verb)
  358. {
  359. switch (verb) {
  360. case MISC:
  361. /*
  362. * These functions are not part of verbs specifically but are
  363. * required for rdmavt to function.
  364. */
  365. if ((!rdi->driver_f.port_callback) ||
  366. (!rdi->driver_f.get_pci_dev))
  367. return -EINVAL;
  368. break;
  369. case QUERY_DEVICE:
  370. check_driver_override(rdi, offsetof(struct ib_device,
  371. query_device),
  372. rvt_query_device);
  373. break;
  374. case MODIFY_DEVICE:
  375. /*
  376. * rdmavt does not support modify device currently drivers must
  377. * provide.
  378. */
  379. if (!check_driver_override(rdi, offsetof(struct ib_device,
  380. modify_device),
  381. rvt_modify_device))
  382. return -EOPNOTSUPP;
  383. break;
  384. case QUERY_PORT:
  385. if (!check_driver_override(rdi, offsetof(struct ib_device,
  386. query_port),
  387. rvt_query_port))
  388. if (!rdi->driver_f.query_port_state)
  389. return -EINVAL;
  390. break;
  391. case MODIFY_PORT:
  392. if (!check_driver_override(rdi, offsetof(struct ib_device,
  393. modify_port),
  394. rvt_modify_port))
  395. if (!rdi->driver_f.cap_mask_chg ||
  396. !rdi->driver_f.shut_down_port)
  397. return -EINVAL;
  398. break;
  399. case QUERY_PKEY:
  400. check_driver_override(rdi, offsetof(struct ib_device,
  401. query_pkey),
  402. rvt_query_pkey);
  403. break;
  404. case QUERY_GID:
  405. if (!check_driver_override(rdi, offsetof(struct ib_device,
  406. query_gid),
  407. rvt_query_gid))
  408. if (!rdi->driver_f.get_guid_be)
  409. return -EINVAL;
  410. break;
  411. case ALLOC_UCONTEXT:
  412. check_driver_override(rdi, offsetof(struct ib_device,
  413. alloc_ucontext),
  414. rvt_alloc_ucontext);
  415. break;
  416. case DEALLOC_UCONTEXT:
  417. check_driver_override(rdi, offsetof(struct ib_device,
  418. dealloc_ucontext),
  419. rvt_dealloc_ucontext);
  420. break;
  421. case GET_PORT_IMMUTABLE:
  422. check_driver_override(rdi, offsetof(struct ib_device,
  423. get_port_immutable),
  424. rvt_get_port_immutable);
  425. break;
  426. case CREATE_QP:
  427. if (!check_driver_override(rdi, offsetof(struct ib_device,
  428. create_qp),
  429. rvt_create_qp))
  430. if (!rdi->driver_f.qp_priv_alloc ||
  431. !rdi->driver_f.qp_priv_free ||
  432. !rdi->driver_f.notify_qp_reset ||
  433. !rdi->driver_f.flush_qp_waiters ||
  434. !rdi->driver_f.stop_send_queue ||
  435. !rdi->driver_f.quiesce_qp)
  436. return -EINVAL;
  437. break;
  438. case MODIFY_QP:
  439. if (!check_driver_override(rdi, offsetof(struct ib_device,
  440. modify_qp),
  441. rvt_modify_qp))
  442. if (!rdi->driver_f.notify_qp_reset ||
  443. !rdi->driver_f.schedule_send ||
  444. !rdi->driver_f.get_pmtu_from_attr ||
  445. !rdi->driver_f.flush_qp_waiters ||
  446. !rdi->driver_f.stop_send_queue ||
  447. !rdi->driver_f.quiesce_qp ||
  448. !rdi->driver_f.notify_error_qp ||
  449. !rdi->driver_f.mtu_from_qp ||
  450. !rdi->driver_f.mtu_to_path_mtu)
  451. return -EINVAL;
  452. break;
  453. case DESTROY_QP:
  454. if (!check_driver_override(rdi, offsetof(struct ib_device,
  455. destroy_qp),
  456. rvt_destroy_qp))
  457. if (!rdi->driver_f.qp_priv_free ||
  458. !rdi->driver_f.notify_qp_reset ||
  459. !rdi->driver_f.flush_qp_waiters ||
  460. !rdi->driver_f.stop_send_queue ||
  461. !rdi->driver_f.quiesce_qp)
  462. return -EINVAL;
  463. break;
  464. case QUERY_QP:
  465. check_driver_override(rdi, offsetof(struct ib_device,
  466. query_qp),
  467. rvt_query_qp);
  468. break;
  469. case POST_SEND:
  470. if (!check_driver_override(rdi, offsetof(struct ib_device,
  471. post_send),
  472. rvt_post_send))
  473. if (!rdi->driver_f.schedule_send ||
  474. !rdi->driver_f.do_send ||
  475. !rdi->post_parms)
  476. return -EINVAL;
  477. break;
  478. case POST_RECV:
  479. check_driver_override(rdi, offsetof(struct ib_device,
  480. post_recv),
  481. rvt_post_recv);
  482. break;
  483. case POST_SRQ_RECV:
  484. check_driver_override(rdi, offsetof(struct ib_device,
  485. post_srq_recv),
  486. rvt_post_srq_recv);
  487. break;
  488. case CREATE_AH:
  489. check_driver_override(rdi, offsetof(struct ib_device,
  490. create_ah),
  491. rvt_create_ah);
  492. break;
  493. case DESTROY_AH:
  494. check_driver_override(rdi, offsetof(struct ib_device,
  495. destroy_ah),
  496. rvt_destroy_ah);
  497. break;
  498. case MODIFY_AH:
  499. check_driver_override(rdi, offsetof(struct ib_device,
  500. modify_ah),
  501. rvt_modify_ah);
  502. break;
  503. case QUERY_AH:
  504. check_driver_override(rdi, offsetof(struct ib_device,
  505. query_ah),
  506. rvt_query_ah);
  507. break;
  508. case CREATE_SRQ:
  509. check_driver_override(rdi, offsetof(struct ib_device,
  510. create_srq),
  511. rvt_create_srq);
  512. break;
  513. case MODIFY_SRQ:
  514. check_driver_override(rdi, offsetof(struct ib_device,
  515. modify_srq),
  516. rvt_modify_srq);
  517. break;
  518. case DESTROY_SRQ:
  519. check_driver_override(rdi, offsetof(struct ib_device,
  520. destroy_srq),
  521. rvt_destroy_srq);
  522. break;
  523. case QUERY_SRQ:
  524. check_driver_override(rdi, offsetof(struct ib_device,
  525. query_srq),
  526. rvt_query_srq);
  527. break;
  528. case ATTACH_MCAST:
  529. check_driver_override(rdi, offsetof(struct ib_device,
  530. attach_mcast),
  531. rvt_attach_mcast);
  532. break;
  533. case DETACH_MCAST:
  534. check_driver_override(rdi, offsetof(struct ib_device,
  535. detach_mcast),
  536. rvt_detach_mcast);
  537. break;
  538. case GET_DMA_MR:
  539. check_driver_override(rdi, offsetof(struct ib_device,
  540. get_dma_mr),
  541. rvt_get_dma_mr);
  542. break;
  543. case REG_USER_MR:
  544. check_driver_override(rdi, offsetof(struct ib_device,
  545. reg_user_mr),
  546. rvt_reg_user_mr);
  547. break;
  548. case DEREG_MR:
  549. check_driver_override(rdi, offsetof(struct ib_device,
  550. dereg_mr),
  551. rvt_dereg_mr);
  552. break;
  553. case ALLOC_FMR:
  554. check_driver_override(rdi, offsetof(struct ib_device,
  555. alloc_fmr),
  556. rvt_alloc_fmr);
  557. break;
  558. case ALLOC_MR:
  559. check_driver_override(rdi, offsetof(struct ib_device,
  560. alloc_mr),
  561. rvt_alloc_mr);
  562. break;
  563. case MAP_MR_SG:
  564. check_driver_override(rdi, offsetof(struct ib_device,
  565. map_mr_sg),
  566. rvt_map_mr_sg);
  567. break;
  568. case MAP_PHYS_FMR:
  569. check_driver_override(rdi, offsetof(struct ib_device,
  570. map_phys_fmr),
  571. rvt_map_phys_fmr);
  572. break;
  573. case UNMAP_FMR:
  574. check_driver_override(rdi, offsetof(struct ib_device,
  575. unmap_fmr),
  576. rvt_unmap_fmr);
  577. break;
  578. case DEALLOC_FMR:
  579. check_driver_override(rdi, offsetof(struct ib_device,
  580. dealloc_fmr),
  581. rvt_dealloc_fmr);
  582. break;
  583. case MMAP:
  584. check_driver_override(rdi, offsetof(struct ib_device,
  585. mmap),
  586. rvt_mmap);
  587. break;
  588. case CREATE_CQ:
  589. check_driver_override(rdi, offsetof(struct ib_device,
  590. create_cq),
  591. rvt_create_cq);
  592. break;
  593. case DESTROY_CQ:
  594. check_driver_override(rdi, offsetof(struct ib_device,
  595. destroy_cq),
  596. rvt_destroy_cq);
  597. break;
  598. case POLL_CQ:
  599. check_driver_override(rdi, offsetof(struct ib_device,
  600. poll_cq),
  601. rvt_poll_cq);
  602. break;
  603. case REQ_NOTFIY_CQ:
  604. check_driver_override(rdi, offsetof(struct ib_device,
  605. req_notify_cq),
  606. rvt_req_notify_cq);
  607. break;
  608. case RESIZE_CQ:
  609. check_driver_override(rdi, offsetof(struct ib_device,
  610. resize_cq),
  611. rvt_resize_cq);
  612. break;
  613. case ALLOC_PD:
  614. check_driver_override(rdi, offsetof(struct ib_device,
  615. alloc_pd),
  616. rvt_alloc_pd);
  617. break;
  618. case DEALLOC_PD:
  619. check_driver_override(rdi, offsetof(struct ib_device,
  620. dealloc_pd),
  621. rvt_dealloc_pd);
  622. break;
  623. default:
  624. return -EINVAL;
  625. }
  626. return 0;
  627. }
  628. /**
  629. * rvt_register_device - register a driver
  630. * @rdi: main dev structure for all of rdmavt operations
  631. *
  632. * It is up to drivers to allocate the rdi and fill in the appropriate
  633. * information.
  634. *
  635. * Return: 0 on success otherwise an errno.
  636. */
  637. int rvt_register_device(struct rvt_dev_info *rdi, u32 driver_id)
  638. {
  639. int ret = 0, i;
  640. if (!rdi)
  641. return -EINVAL;
  642. /*
  643. * Check to ensure drivers have setup the required helpers for the verbs
  644. * they want rdmavt to handle
  645. */
  646. for (i = 0; i < _VERB_IDX_MAX; i++)
  647. if (check_support(rdi, i)) {
  648. pr_err("Driver support req not met at %d\n", i);
  649. return -EINVAL;
  650. }
  651. /* Once we get past here we can use rvt_pr macros and tracepoints */
  652. trace_rvt_dbg(rdi, "Driver attempting registration");
  653. rvt_mmap_init(rdi);
  654. /* Queue Pairs */
  655. ret = rvt_driver_qp_init(rdi);
  656. if (ret) {
  657. pr_err("Error in driver QP init.\n");
  658. return -EINVAL;
  659. }
  660. /* Address Handle */
  661. spin_lock_init(&rdi->n_ahs_lock);
  662. rdi->n_ahs_allocated = 0;
  663. /* Shared Receive Queue */
  664. rvt_driver_srq_init(rdi);
  665. /* Multicast */
  666. rvt_driver_mcast_init(rdi);
  667. /* Mem Region */
  668. ret = rvt_driver_mr_init(rdi);
  669. if (ret) {
  670. pr_err("Error in driver MR init.\n");
  671. goto bail_no_mr;
  672. }
  673. /* Completion queues */
  674. spin_lock_init(&rdi->n_cqs_lock);
  675. /* DMA Operations */
  676. rdi->ibdev.dev.dma_ops = rdi->ibdev.dev.dma_ops ? : &dma_virt_ops;
  677. /* Protection Domain */
  678. spin_lock_init(&rdi->n_pds_lock);
  679. rdi->n_pds_allocated = 0;
  680. /*
  681. * There are some things which could be set by underlying drivers but
  682. * really should be up to rdmavt to set. For instance drivers can't know
  683. * exactly which functions rdmavt supports, nor do they know the ABI
  684. * version, so we do all of this sort of stuff here.
  685. */
  686. rdi->ibdev.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION;
  687. rdi->ibdev.uverbs_cmd_mask =
  688. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  689. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  690. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  691. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  692. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  693. (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
  694. (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
  695. (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
  696. (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
  697. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  698. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  699. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  700. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  701. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  702. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  703. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  704. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  705. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  706. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  707. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  708. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  709. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  710. (1ull << IB_USER_VERBS_CMD_POST_RECV) |
  711. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  712. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  713. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  714. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  715. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  716. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  717. (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
  718. rdi->ibdev.node_type = RDMA_NODE_IB_CA;
  719. if (!rdi->ibdev.num_comp_vectors)
  720. rdi->ibdev.num_comp_vectors = 1;
  721. rdi->ibdev.driver_id = driver_id;
  722. /* We are now good to announce we exist */
  723. ret = ib_register_device(&rdi->ibdev, rdi->driver_f.port_callback);
  724. if (ret) {
  725. rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
  726. goto bail_mr;
  727. }
  728. rvt_create_mad_agents(rdi);
  729. rvt_pr_info(rdi, "Registration with rdmavt done.\n");
  730. return ret;
  731. bail_mr:
  732. rvt_mr_exit(rdi);
  733. bail_no_mr:
  734. rvt_qp_exit(rdi);
  735. return ret;
  736. }
  737. EXPORT_SYMBOL(rvt_register_device);
  738. /**
  739. * rvt_unregister_device - remove a driver
  740. * @rdi: rvt dev struct
  741. */
  742. void rvt_unregister_device(struct rvt_dev_info *rdi)
  743. {
  744. trace_rvt_dbg(rdi, "Driver is unregistering.");
  745. if (!rdi)
  746. return;
  747. rvt_free_mad_agents(rdi);
  748. ib_unregister_device(&rdi->ibdev);
  749. rvt_mr_exit(rdi);
  750. rvt_qp_exit(rdi);
  751. }
  752. EXPORT_SYMBOL(rvt_unregister_device);
  753. /**
  754. * rvt_init_port - init internal data for driver port
  755. * @rdi: rvt dev strut
  756. * @port: rvt port
  757. * @port_index: 0 based index of ports, different from IB core port num
  758. *
  759. * Keep track of a list of ports. No need to have a detach port.
  760. * They persist until the driver goes away.
  761. *
  762. * Return: always 0
  763. */
  764. int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
  765. int port_index, u16 *pkey_table)
  766. {
  767. rdi->ports[port_index] = port;
  768. rdi->ports[port_index]->pkey_table = pkey_table;
  769. return 0;
  770. }
  771. EXPORT_SYMBOL(rvt_init_port);