vt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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, sizeof(*rdi->ports), GFP_KERNEL);
  89. if (!rdi->ports)
  90. ib_dealloc_device(&rdi->ibdev);
  91. return rdi;
  92. }
  93. EXPORT_SYMBOL(rvt_alloc_device);
  94. /**
  95. * rvt_dealloc_device - deallocate rdi
  96. * @rdi: structure to free
  97. *
  98. * Free a structure allocated with rvt_alloc_device()
  99. */
  100. void rvt_dealloc_device(struct rvt_dev_info *rdi)
  101. {
  102. kfree(rdi->ports);
  103. ib_dealloc_device(&rdi->ibdev);
  104. }
  105. EXPORT_SYMBOL(rvt_dealloc_device);
  106. static int rvt_query_device(struct ib_device *ibdev,
  107. struct ib_device_attr *props,
  108. struct ib_udata *uhw)
  109. {
  110. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  111. if (uhw->inlen || uhw->outlen)
  112. return -EINVAL;
  113. /*
  114. * Return rvt_dev_info.dparms.props contents
  115. */
  116. *props = rdi->dparms.props;
  117. return 0;
  118. }
  119. static int rvt_modify_device(struct ib_device *device,
  120. int device_modify_mask,
  121. struct ib_device_modify *device_modify)
  122. {
  123. /*
  124. * There is currently no need to supply this based on qib and hfi1.
  125. * Future drivers may need to implement this though.
  126. */
  127. return -EOPNOTSUPP;
  128. }
  129. /**
  130. * rvt_query_port: Passes the query port call to the driver
  131. * @ibdev: Verbs IB dev
  132. * @port_num: port number, 1 based from ib core
  133. * @props: structure to hold returned properties
  134. *
  135. * Return: 0 on success
  136. */
  137. static int rvt_query_port(struct ib_device *ibdev, u8 port_num,
  138. struct ib_port_attr *props)
  139. {
  140. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  141. struct rvt_ibport *rvp;
  142. int port_index = ibport_num_to_idx(ibdev, port_num);
  143. if (port_index < 0)
  144. return -EINVAL;
  145. rvp = rdi->ports[port_index];
  146. /* props being zeroed by the caller, avoid zeroing it here */
  147. props->sm_lid = rvp->sm_lid;
  148. props->sm_sl = rvp->sm_sl;
  149. props->port_cap_flags = rvp->port_cap_flags;
  150. props->max_msg_sz = 0x80000000;
  151. props->pkey_tbl_len = rvt_get_npkeys(rdi);
  152. props->bad_pkey_cntr = rvp->pkey_violations;
  153. props->qkey_viol_cntr = rvp->qkey_violations;
  154. props->subnet_timeout = rvp->subnet_timeout;
  155. props->init_type_reply = 0;
  156. /* Populate the remaining ib_port_attr elements */
  157. return rdi->driver_f.query_port_state(rdi, port_num, props);
  158. }
  159. /**
  160. * rvt_modify_port
  161. * @ibdev: Verbs IB dev
  162. * @port_num: Port number, 1 based from ib core
  163. * @port_modify_mask: How to change the port
  164. * @props: Structure to fill in
  165. *
  166. * Return: 0 on success
  167. */
  168. static int rvt_modify_port(struct ib_device *ibdev, u8 port_num,
  169. int port_modify_mask, struct ib_port_modify *props)
  170. {
  171. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  172. struct rvt_ibport *rvp;
  173. int ret = 0;
  174. int port_index = ibport_num_to_idx(ibdev, port_num);
  175. if (port_index < 0)
  176. return -EINVAL;
  177. rvp = rdi->ports[port_index];
  178. if (port_modify_mask & IB_PORT_OPA_MASK_CHG) {
  179. rvp->port_cap3_flags |= props->set_port_cap_mask;
  180. rvp->port_cap3_flags &= ~props->clr_port_cap_mask;
  181. } else {
  182. rvp->port_cap_flags |= props->set_port_cap_mask;
  183. rvp->port_cap_flags &= ~props->clr_port_cap_mask;
  184. }
  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. * @index: Index into pkey table
  198. * @pkey: returned pkey from the port 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. * @guid_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: Verbs IB dev
  261. * @udata: 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_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, u32 driver_id)
  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. spin_lock_init(&rdi->n_cqs_lock);
  673. /* DMA Operations */
  674. rdi->ibdev.dev.dma_ops = rdi->ibdev.dev.dma_ops ? : &dma_virt_ops;
  675. /* Protection Domain */
  676. spin_lock_init(&rdi->n_pds_lock);
  677. rdi->n_pds_allocated = 0;
  678. /*
  679. * There are some things which could be set by underlying drivers but
  680. * really should be up to rdmavt to set. For instance drivers can't know
  681. * exactly which functions rdmavt supports, nor do they know the ABI
  682. * version, so we do all of this sort of stuff here.
  683. */
  684. rdi->ibdev.uverbs_abi_ver = RVT_UVERBS_ABI_VERSION;
  685. rdi->ibdev.uverbs_cmd_mask =
  686. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  687. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  688. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  689. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  690. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  691. (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
  692. (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
  693. (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
  694. (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
  695. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  696. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  697. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  698. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  699. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  700. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  701. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  702. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  703. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  704. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  705. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  706. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  707. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  708. (1ull << IB_USER_VERBS_CMD_POST_RECV) |
  709. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  710. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  711. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  712. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  713. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  714. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  715. (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
  716. rdi->ibdev.node_type = RDMA_NODE_IB_CA;
  717. if (!rdi->ibdev.num_comp_vectors)
  718. rdi->ibdev.num_comp_vectors = 1;
  719. rdi->ibdev.driver_id = driver_id;
  720. /* We are now good to announce we exist */
  721. ret = ib_register_device(&rdi->ibdev, rdi->driver_f.port_callback);
  722. if (ret) {
  723. rvt_pr_err(rdi, "Failed to register driver with ib core.\n");
  724. goto bail_mr;
  725. }
  726. rvt_create_mad_agents(rdi);
  727. rvt_pr_info(rdi, "Registration with rdmavt done.\n");
  728. return ret;
  729. bail_mr:
  730. rvt_mr_exit(rdi);
  731. bail_no_mr:
  732. rvt_qp_exit(rdi);
  733. return ret;
  734. }
  735. EXPORT_SYMBOL(rvt_register_device);
  736. /**
  737. * rvt_unregister_device - remove a driver
  738. * @rdi: rvt dev struct
  739. */
  740. void rvt_unregister_device(struct rvt_dev_info *rdi)
  741. {
  742. trace_rvt_dbg(rdi, "Driver is unregistering.");
  743. if (!rdi)
  744. return;
  745. rvt_free_mad_agents(rdi);
  746. ib_unregister_device(&rdi->ibdev);
  747. rvt_mr_exit(rdi);
  748. rvt_qp_exit(rdi);
  749. }
  750. EXPORT_SYMBOL(rvt_unregister_device);
  751. /**
  752. * rvt_init_port - init internal data for driver port
  753. * @rdi: rvt dev strut
  754. * @port: rvt port
  755. * @port_index: 0 based index of ports, different from IB core port num
  756. *
  757. * Keep track of a list of ports. No need to have a detach port.
  758. * They persist until the driver goes away.
  759. *
  760. * Return: always 0
  761. */
  762. int rvt_init_port(struct rvt_dev_info *rdi, struct rvt_ibport *port,
  763. int port_index, u16 *pkey_table)
  764. {
  765. rdi->ports[port_index] = port;
  766. rdi->ports[port_index]->pkey_table = pkey_table;
  767. return 0;
  768. }
  769. EXPORT_SYMBOL(rvt_init_port);