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