vt.c 22 KB

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