usnic_ib_verbs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/errno.h>
  37. #include <rdma/ib_user_verbs.h>
  38. #include <rdma/ib_addr.h>
  39. #include "usnic_abi.h"
  40. #include "usnic_ib.h"
  41. #include "usnic_common_util.h"
  42. #include "usnic_ib_qp_grp.h"
  43. #include "usnic_ib_verbs.h"
  44. #include "usnic_fwd.h"
  45. #include "usnic_log.h"
  46. #include "usnic_uiom.h"
  47. #include "usnic_transport.h"
  48. #define USNIC_DEFAULT_TRANSPORT USNIC_TRANSPORT_ROCE_CUSTOM
  49. const struct usnic_vnic_res_spec min_transport_spec[USNIC_TRANSPORT_MAX] = {
  50. { /*USNIC_TRANSPORT_UNKNOWN*/
  51. .resources = {
  52. {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,},
  53. },
  54. },
  55. { /*USNIC_TRANSPORT_ROCE_CUSTOM*/
  56. .resources = {
  57. {.type = USNIC_VNIC_RES_TYPE_WQ, .cnt = 1,},
  58. {.type = USNIC_VNIC_RES_TYPE_RQ, .cnt = 1,},
  59. {.type = USNIC_VNIC_RES_TYPE_CQ, .cnt = 1,},
  60. {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,},
  61. },
  62. },
  63. { /*USNIC_TRANSPORT_IPV4_UDP*/
  64. .resources = {
  65. {.type = USNIC_VNIC_RES_TYPE_WQ, .cnt = 1,},
  66. {.type = USNIC_VNIC_RES_TYPE_RQ, .cnt = 1,},
  67. {.type = USNIC_VNIC_RES_TYPE_CQ, .cnt = 1,},
  68. {.type = USNIC_VNIC_RES_TYPE_EOL, .cnt = 0,},
  69. },
  70. },
  71. };
  72. static void usnic_ib_fw_string_to_u64(char *fw_ver_str, u64 *fw_ver)
  73. {
  74. *fw_ver = *((u64 *)fw_ver_str);
  75. }
  76. static int usnic_ib_fill_create_qp_resp(struct usnic_ib_qp_grp *qp_grp,
  77. struct ib_udata *udata)
  78. {
  79. struct usnic_ib_dev *us_ibdev;
  80. struct usnic_ib_create_qp_resp resp;
  81. struct pci_dev *pdev;
  82. struct vnic_dev_bar *bar;
  83. struct usnic_vnic_res_chunk *chunk;
  84. struct usnic_ib_qp_grp_flow *default_flow;
  85. int i, err;
  86. memset(&resp, 0, sizeof(resp));
  87. us_ibdev = qp_grp->vf->pf;
  88. pdev = usnic_vnic_get_pdev(qp_grp->vf->vnic);
  89. if (!pdev) {
  90. usnic_err("Failed to get pdev of qp_grp %d\n",
  91. qp_grp->grp_id);
  92. return -EFAULT;
  93. }
  94. bar = usnic_vnic_get_bar(qp_grp->vf->vnic, 0);
  95. if (!bar) {
  96. usnic_err("Failed to get bar0 of qp_grp %d vf %s",
  97. qp_grp->grp_id, pci_name(pdev));
  98. return -EFAULT;
  99. }
  100. resp.vfid = usnic_vnic_get_index(qp_grp->vf->vnic);
  101. resp.bar_bus_addr = bar->bus_addr;
  102. resp.bar_len = bar->len;
  103. chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_RQ);
  104. if (IS_ERR(chunk)) {
  105. usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
  106. usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_RQ),
  107. qp_grp->grp_id,
  108. PTR_ERR(chunk));
  109. return PTR_ERR(chunk);
  110. }
  111. WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_RQ);
  112. resp.rq_cnt = chunk->cnt;
  113. for (i = 0; i < chunk->cnt; i++)
  114. resp.rq_idx[i] = chunk->res[i]->vnic_idx;
  115. chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_WQ);
  116. if (IS_ERR(chunk)) {
  117. usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
  118. usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_WQ),
  119. qp_grp->grp_id,
  120. PTR_ERR(chunk));
  121. return PTR_ERR(chunk);
  122. }
  123. WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_WQ);
  124. resp.wq_cnt = chunk->cnt;
  125. for (i = 0; i < chunk->cnt; i++)
  126. resp.wq_idx[i] = chunk->res[i]->vnic_idx;
  127. chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_CQ);
  128. if (IS_ERR(chunk)) {
  129. usnic_err("Failed to get chunk %s for qp_grp %d with err %ld\n",
  130. usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_CQ),
  131. qp_grp->grp_id,
  132. PTR_ERR(chunk));
  133. return PTR_ERR(chunk);
  134. }
  135. WARN_ON(chunk->type != USNIC_VNIC_RES_TYPE_CQ);
  136. resp.cq_cnt = chunk->cnt;
  137. for (i = 0; i < chunk->cnt; i++)
  138. resp.cq_idx[i] = chunk->res[i]->vnic_idx;
  139. default_flow = list_first_entry(&qp_grp->flows_lst,
  140. struct usnic_ib_qp_grp_flow, link);
  141. resp.transport = default_flow->trans_type;
  142. err = ib_copy_to_udata(udata, &resp, sizeof(resp));
  143. if (err) {
  144. usnic_err("Failed to copy udata for %s", us_ibdev->ib_dev.name);
  145. return err;
  146. }
  147. return 0;
  148. }
  149. static struct usnic_ib_qp_grp*
  150. find_free_vf_and_create_qp_grp(struct usnic_ib_dev *us_ibdev,
  151. struct usnic_ib_pd *pd,
  152. struct usnic_transport_spec *trans_spec,
  153. struct usnic_vnic_res_spec *res_spec)
  154. {
  155. struct usnic_ib_vf *vf;
  156. struct usnic_vnic *vnic;
  157. struct usnic_ib_qp_grp *qp_grp;
  158. struct device *dev, **dev_list;
  159. int i;
  160. BUG_ON(!mutex_is_locked(&us_ibdev->usdev_lock));
  161. if (list_empty(&us_ibdev->vf_dev_list)) {
  162. usnic_info("No vfs to allocate\n");
  163. return NULL;
  164. }
  165. if (usnic_ib_share_vf) {
  166. /* Try to find resouces on a used vf which is in pd */
  167. dev_list = usnic_uiom_get_dev_list(pd->umem_pd);
  168. if (IS_ERR(dev_list))
  169. return ERR_CAST(dev_list);
  170. for (i = 0; dev_list[i]; i++) {
  171. dev = dev_list[i];
  172. vf = pci_get_drvdata(to_pci_dev(dev));
  173. spin_lock(&vf->lock);
  174. vnic = vf->vnic;
  175. if (!usnic_vnic_check_room(vnic, res_spec)) {
  176. usnic_dbg("Found used vnic %s from %s\n",
  177. us_ibdev->ib_dev.name,
  178. pci_name(usnic_vnic_get_pdev(
  179. vnic)));
  180. qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev,
  181. vf, pd,
  182. res_spec,
  183. trans_spec);
  184. spin_unlock(&vf->lock);
  185. goto qp_grp_check;
  186. }
  187. spin_unlock(&vf->lock);
  188. }
  189. usnic_uiom_free_dev_list(dev_list);
  190. dev_list = NULL;
  191. }
  192. /* Try to find resources on an unused vf */
  193. list_for_each_entry(vf, &us_ibdev->vf_dev_list, link) {
  194. spin_lock(&vf->lock);
  195. vnic = vf->vnic;
  196. if (vf->qp_grp_ref_cnt == 0 &&
  197. usnic_vnic_check_room(vnic, res_spec) == 0) {
  198. qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev, vf,
  199. pd, res_spec,
  200. trans_spec);
  201. spin_unlock(&vf->lock);
  202. goto qp_grp_check;
  203. }
  204. spin_unlock(&vf->lock);
  205. }
  206. usnic_info("No free qp grp found on %s\n", us_ibdev->ib_dev.name);
  207. return ERR_PTR(-ENOMEM);
  208. qp_grp_check:
  209. if (IS_ERR_OR_NULL(qp_grp)) {
  210. usnic_err("Failed to allocate qp_grp\n");
  211. if (usnic_ib_share_vf)
  212. usnic_uiom_free_dev_list(dev_list);
  213. return ERR_PTR(qp_grp ? PTR_ERR(qp_grp) : -ENOMEM);
  214. }
  215. return qp_grp;
  216. }
  217. static void qp_grp_destroy(struct usnic_ib_qp_grp *qp_grp)
  218. {
  219. struct usnic_ib_vf *vf = qp_grp->vf;
  220. WARN_ON(qp_grp->state != IB_QPS_RESET);
  221. spin_lock(&vf->lock);
  222. usnic_ib_qp_grp_destroy(qp_grp);
  223. spin_unlock(&vf->lock);
  224. }
  225. static int create_qp_validate_user_data(struct usnic_ib_create_qp_cmd cmd)
  226. {
  227. if (cmd.spec.trans_type <= USNIC_TRANSPORT_UNKNOWN ||
  228. cmd.spec.trans_type >= USNIC_TRANSPORT_MAX)
  229. return -EINVAL;
  230. return 0;
  231. }
  232. /* Start of ib callback functions */
  233. enum rdma_link_layer usnic_ib_port_link_layer(struct ib_device *device,
  234. u8 port_num)
  235. {
  236. return IB_LINK_LAYER_ETHERNET;
  237. }
  238. int usnic_ib_query_device(struct ib_device *ibdev,
  239. struct ib_device_attr *props,
  240. struct ib_udata *uhw)
  241. {
  242. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  243. union ib_gid gid;
  244. struct ethtool_drvinfo info;
  245. int qp_per_vf;
  246. usnic_dbg("\n");
  247. if (uhw->inlen || uhw->outlen)
  248. return -EINVAL;
  249. mutex_lock(&us_ibdev->usdev_lock);
  250. us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
  251. memset(props, 0, sizeof(*props));
  252. usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
  253. &gid.raw[0]);
  254. memcpy(&props->sys_image_guid, &gid.global.interface_id,
  255. sizeof(gid.global.interface_id));
  256. usnic_ib_fw_string_to_u64(&info.fw_version[0], &props->fw_ver);
  257. props->max_mr_size = USNIC_UIOM_MAX_MR_SIZE;
  258. props->page_size_cap = USNIC_UIOM_PAGE_SIZE;
  259. props->vendor_id = PCI_VENDOR_ID_CISCO;
  260. props->vendor_part_id = PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC;
  261. props->hw_ver = us_ibdev->pdev->subsystem_device;
  262. qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
  263. us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
  264. props->max_qp = qp_per_vf *
  265. kref_read(&us_ibdev->vf_cnt);
  266. props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
  267. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  268. props->max_cq = us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ] *
  269. kref_read(&us_ibdev->vf_cnt);
  270. props->max_pd = USNIC_UIOM_MAX_PD_CNT;
  271. props->max_mr = USNIC_UIOM_MAX_MR_CNT;
  272. props->local_ca_ack_delay = 0;
  273. props->max_pkeys = 0;
  274. props->atomic_cap = IB_ATOMIC_NONE;
  275. props->masked_atomic_cap = props->atomic_cap;
  276. props->max_qp_rd_atom = 0;
  277. props->max_qp_init_rd_atom = 0;
  278. props->max_res_rd_atom = 0;
  279. props->max_srq = 0;
  280. props->max_srq_wr = 0;
  281. props->max_srq_sge = 0;
  282. props->max_fast_reg_page_list_len = 0;
  283. props->max_mcast_grp = 0;
  284. props->max_mcast_qp_attach = 0;
  285. props->max_total_mcast_qp_attach = 0;
  286. props->max_map_per_fmr = 0;
  287. /* Owned by Userspace
  288. * max_qp_wr, max_sge, max_sge_rd, max_cqe */
  289. mutex_unlock(&us_ibdev->usdev_lock);
  290. return 0;
  291. }
  292. int usnic_ib_query_port(struct ib_device *ibdev, u8 port,
  293. struct ib_port_attr *props)
  294. {
  295. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  296. usnic_dbg("\n");
  297. if (ib_get_eth_speed(ibdev, port, &props->active_speed,
  298. &props->active_width))
  299. return -EINVAL;
  300. /*
  301. * usdev_lock is acquired after (and not before) ib_get_eth_speed call
  302. * because acquiring rtnl_lock in ib_get_eth_speed, while holding
  303. * usdev_lock could lead to a deadlock.
  304. */
  305. mutex_lock(&us_ibdev->usdev_lock);
  306. /* props being zeroed by the caller, avoid zeroing it here */
  307. props->lid = 0;
  308. props->lmc = 1;
  309. props->sm_lid = 0;
  310. props->sm_sl = 0;
  311. if (!us_ibdev->ufdev->link_up) {
  312. props->state = IB_PORT_DOWN;
  313. props->phys_state = 3;
  314. } else if (!us_ibdev->ufdev->inaddr) {
  315. props->state = IB_PORT_INIT;
  316. props->phys_state = 4;
  317. } else {
  318. props->state = IB_PORT_ACTIVE;
  319. props->phys_state = 5;
  320. }
  321. props->port_cap_flags = 0;
  322. props->gid_tbl_len = 1;
  323. props->pkey_tbl_len = 1;
  324. props->bad_pkey_cntr = 0;
  325. props->qkey_viol_cntr = 0;
  326. props->max_mtu = IB_MTU_4096;
  327. props->active_mtu = iboe_get_mtu(us_ibdev->ufdev->mtu);
  328. /* Userspace will adjust for hdrs */
  329. props->max_msg_sz = us_ibdev->ufdev->mtu;
  330. props->max_vl_num = 1;
  331. mutex_unlock(&us_ibdev->usdev_lock);
  332. return 0;
  333. }
  334. int usnic_ib_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
  335. int qp_attr_mask,
  336. struct ib_qp_init_attr *qp_init_attr)
  337. {
  338. struct usnic_ib_qp_grp *qp_grp;
  339. struct usnic_ib_vf *vf;
  340. int err;
  341. usnic_dbg("\n");
  342. memset(qp_attr, 0, sizeof(*qp_attr));
  343. memset(qp_init_attr, 0, sizeof(*qp_init_attr));
  344. qp_grp = to_uqp_grp(qp);
  345. vf = qp_grp->vf;
  346. mutex_lock(&vf->pf->usdev_lock);
  347. usnic_dbg("\n");
  348. qp_attr->qp_state = qp_grp->state;
  349. qp_attr->cur_qp_state = qp_grp->state;
  350. switch (qp_grp->ibqp.qp_type) {
  351. case IB_QPT_UD:
  352. qp_attr->qkey = 0;
  353. break;
  354. default:
  355. usnic_err("Unexpected qp_type %d\n", qp_grp->ibqp.qp_type);
  356. err = -EINVAL;
  357. goto err_out;
  358. }
  359. mutex_unlock(&vf->pf->usdev_lock);
  360. return 0;
  361. err_out:
  362. mutex_unlock(&vf->pf->usdev_lock);
  363. return err;
  364. }
  365. int usnic_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  366. union ib_gid *gid)
  367. {
  368. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  369. usnic_dbg("\n");
  370. if (index > 1)
  371. return -EINVAL;
  372. mutex_lock(&us_ibdev->usdev_lock);
  373. memset(&(gid->raw[0]), 0, sizeof(gid->raw));
  374. usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
  375. &gid->raw[0]);
  376. mutex_unlock(&us_ibdev->usdev_lock);
  377. return 0;
  378. }
  379. struct net_device *usnic_get_netdev(struct ib_device *device, u8 port_num)
  380. {
  381. struct usnic_ib_dev *us_ibdev = to_usdev(device);
  382. if (us_ibdev->netdev)
  383. dev_hold(us_ibdev->netdev);
  384. return us_ibdev->netdev;
  385. }
  386. int usnic_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  387. u16 *pkey)
  388. {
  389. if (index > 0)
  390. return -EINVAL;
  391. *pkey = 0xffff;
  392. return 0;
  393. }
  394. struct ib_pd *usnic_ib_alloc_pd(struct ib_device *ibdev,
  395. struct ib_ucontext *context,
  396. struct ib_udata *udata)
  397. {
  398. struct usnic_ib_pd *pd;
  399. void *umem_pd;
  400. usnic_dbg("\n");
  401. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  402. if (!pd)
  403. return ERR_PTR(-ENOMEM);
  404. umem_pd = pd->umem_pd = usnic_uiom_alloc_pd();
  405. if (IS_ERR_OR_NULL(umem_pd)) {
  406. kfree(pd);
  407. return ERR_PTR(umem_pd ? PTR_ERR(umem_pd) : -ENOMEM);
  408. }
  409. usnic_info("domain 0x%p allocated for context 0x%p and device %s\n",
  410. pd, context, ibdev->name);
  411. return &pd->ibpd;
  412. }
  413. int usnic_ib_dealloc_pd(struct ib_pd *pd)
  414. {
  415. usnic_info("freeing domain 0x%p\n", pd);
  416. usnic_uiom_dealloc_pd((to_upd(pd))->umem_pd);
  417. kfree(pd);
  418. return 0;
  419. }
  420. struct ib_qp *usnic_ib_create_qp(struct ib_pd *pd,
  421. struct ib_qp_init_attr *init_attr,
  422. struct ib_udata *udata)
  423. {
  424. int err;
  425. struct usnic_ib_dev *us_ibdev;
  426. struct usnic_ib_qp_grp *qp_grp;
  427. struct usnic_ib_ucontext *ucontext;
  428. int cq_cnt;
  429. struct usnic_vnic_res_spec res_spec;
  430. struct usnic_ib_create_qp_cmd cmd;
  431. struct usnic_transport_spec trans_spec;
  432. usnic_dbg("\n");
  433. ucontext = to_uucontext(pd->uobject->context);
  434. us_ibdev = to_usdev(pd->device);
  435. if (init_attr->create_flags)
  436. return ERR_PTR(-EINVAL);
  437. err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
  438. if (err) {
  439. usnic_err("%s: cannot copy udata for create_qp\n",
  440. us_ibdev->ib_dev.name);
  441. return ERR_PTR(-EINVAL);
  442. }
  443. err = create_qp_validate_user_data(cmd);
  444. if (err) {
  445. usnic_err("%s: Failed to validate user data\n",
  446. us_ibdev->ib_dev.name);
  447. return ERR_PTR(-EINVAL);
  448. }
  449. if (init_attr->qp_type != IB_QPT_UD) {
  450. usnic_err("%s asked to make a non-UD QP: %d\n",
  451. us_ibdev->ib_dev.name, init_attr->qp_type);
  452. return ERR_PTR(-EINVAL);
  453. }
  454. trans_spec = cmd.spec;
  455. mutex_lock(&us_ibdev->usdev_lock);
  456. cq_cnt = (init_attr->send_cq == init_attr->recv_cq) ? 1 : 2;
  457. res_spec = min_transport_spec[trans_spec.trans_type];
  458. usnic_vnic_res_spec_update(&res_spec, USNIC_VNIC_RES_TYPE_CQ, cq_cnt);
  459. qp_grp = find_free_vf_and_create_qp_grp(us_ibdev, to_upd(pd),
  460. &trans_spec,
  461. &res_spec);
  462. if (IS_ERR_OR_NULL(qp_grp)) {
  463. err = qp_grp ? PTR_ERR(qp_grp) : -ENOMEM;
  464. goto out_release_mutex;
  465. }
  466. err = usnic_ib_fill_create_qp_resp(qp_grp, udata);
  467. if (err) {
  468. err = -EBUSY;
  469. goto out_release_qp_grp;
  470. }
  471. qp_grp->ctx = ucontext;
  472. list_add_tail(&qp_grp->link, &ucontext->qp_grp_list);
  473. usnic_ib_log_vf(qp_grp->vf);
  474. mutex_unlock(&us_ibdev->usdev_lock);
  475. return &qp_grp->ibqp;
  476. out_release_qp_grp:
  477. qp_grp_destroy(qp_grp);
  478. out_release_mutex:
  479. mutex_unlock(&us_ibdev->usdev_lock);
  480. return ERR_PTR(err);
  481. }
  482. int usnic_ib_destroy_qp(struct ib_qp *qp)
  483. {
  484. struct usnic_ib_qp_grp *qp_grp;
  485. struct usnic_ib_vf *vf;
  486. usnic_dbg("\n");
  487. qp_grp = to_uqp_grp(qp);
  488. vf = qp_grp->vf;
  489. mutex_lock(&vf->pf->usdev_lock);
  490. if (usnic_ib_qp_grp_modify(qp_grp, IB_QPS_RESET, NULL)) {
  491. usnic_err("Failed to move qp grp %u to reset\n",
  492. qp_grp->grp_id);
  493. }
  494. list_del(&qp_grp->link);
  495. qp_grp_destroy(qp_grp);
  496. mutex_unlock(&vf->pf->usdev_lock);
  497. return 0;
  498. }
  499. int usnic_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  500. int attr_mask, struct ib_udata *udata)
  501. {
  502. struct usnic_ib_qp_grp *qp_grp;
  503. int status;
  504. usnic_dbg("\n");
  505. qp_grp = to_uqp_grp(ibqp);
  506. mutex_lock(&qp_grp->vf->pf->usdev_lock);
  507. if ((attr_mask & IB_QP_PORT) && attr->port_num != 1) {
  508. /* usnic devices only have one port */
  509. status = -EINVAL;
  510. goto out_unlock;
  511. }
  512. if (attr_mask & IB_QP_STATE) {
  513. status = usnic_ib_qp_grp_modify(qp_grp, attr->qp_state, NULL);
  514. } else {
  515. usnic_err("Unhandled request, attr_mask=0x%x\n", attr_mask);
  516. status = -EINVAL;
  517. }
  518. out_unlock:
  519. mutex_unlock(&qp_grp->vf->pf->usdev_lock);
  520. return status;
  521. }
  522. struct ib_cq *usnic_ib_create_cq(struct ib_device *ibdev,
  523. const struct ib_cq_init_attr *attr,
  524. struct ib_ucontext *context,
  525. struct ib_udata *udata)
  526. {
  527. struct ib_cq *cq;
  528. usnic_dbg("\n");
  529. if (attr->flags)
  530. return ERR_PTR(-EINVAL);
  531. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  532. if (!cq)
  533. return ERR_PTR(-EBUSY);
  534. return cq;
  535. }
  536. int usnic_ib_destroy_cq(struct ib_cq *cq)
  537. {
  538. usnic_dbg("\n");
  539. kfree(cq);
  540. return 0;
  541. }
  542. struct ib_mr *usnic_ib_reg_mr(struct ib_pd *pd, u64 start, u64 length,
  543. u64 virt_addr, int access_flags,
  544. struct ib_udata *udata)
  545. {
  546. struct usnic_ib_mr *mr;
  547. int err;
  548. usnic_dbg("start 0x%llx va 0x%llx length 0x%llx\n", start,
  549. virt_addr, length);
  550. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  551. if (!mr)
  552. return ERR_PTR(-ENOMEM);
  553. mr->umem = usnic_uiom_reg_get(to_upd(pd)->umem_pd, start, length,
  554. access_flags, 0);
  555. if (IS_ERR_OR_NULL(mr->umem)) {
  556. err = mr->umem ? PTR_ERR(mr->umem) : -EFAULT;
  557. goto err_free;
  558. }
  559. mr->ibmr.lkey = mr->ibmr.rkey = 0;
  560. return &mr->ibmr;
  561. err_free:
  562. kfree(mr);
  563. return ERR_PTR(err);
  564. }
  565. int usnic_ib_dereg_mr(struct ib_mr *ibmr)
  566. {
  567. struct usnic_ib_mr *mr = to_umr(ibmr);
  568. usnic_dbg("va 0x%lx length 0x%zx\n", mr->umem->va, mr->umem->length);
  569. usnic_uiom_reg_release(mr->umem, ibmr->uobject->context);
  570. kfree(mr);
  571. return 0;
  572. }
  573. struct ib_ucontext *usnic_ib_alloc_ucontext(struct ib_device *ibdev,
  574. struct ib_udata *udata)
  575. {
  576. struct usnic_ib_ucontext *context;
  577. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  578. usnic_dbg("\n");
  579. context = kmalloc(sizeof(*context), GFP_KERNEL);
  580. if (!context)
  581. return ERR_PTR(-ENOMEM);
  582. INIT_LIST_HEAD(&context->qp_grp_list);
  583. mutex_lock(&us_ibdev->usdev_lock);
  584. list_add_tail(&context->link, &us_ibdev->ctx_list);
  585. mutex_unlock(&us_ibdev->usdev_lock);
  586. return &context->ibucontext;
  587. }
  588. int usnic_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  589. {
  590. struct usnic_ib_ucontext *context = to_uucontext(ibcontext);
  591. struct usnic_ib_dev *us_ibdev = to_usdev(ibcontext->device);
  592. usnic_dbg("\n");
  593. mutex_lock(&us_ibdev->usdev_lock);
  594. BUG_ON(!list_empty(&context->qp_grp_list));
  595. list_del(&context->link);
  596. mutex_unlock(&us_ibdev->usdev_lock);
  597. kfree(context);
  598. return 0;
  599. }
  600. int usnic_ib_mmap(struct ib_ucontext *context,
  601. struct vm_area_struct *vma)
  602. {
  603. struct usnic_ib_ucontext *uctx = to_ucontext(context);
  604. struct usnic_ib_dev *us_ibdev;
  605. struct usnic_ib_qp_grp *qp_grp;
  606. struct usnic_ib_vf *vf;
  607. struct vnic_dev_bar *bar;
  608. dma_addr_t bus_addr;
  609. unsigned int len;
  610. unsigned int vfid;
  611. usnic_dbg("\n");
  612. us_ibdev = to_usdev(context->device);
  613. vma->vm_flags |= VM_IO;
  614. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  615. vfid = vma->vm_pgoff;
  616. usnic_dbg("Page Offset %lu PAGE_SHIFT %u VFID %u\n",
  617. vma->vm_pgoff, PAGE_SHIFT, vfid);
  618. mutex_lock(&us_ibdev->usdev_lock);
  619. list_for_each_entry(qp_grp, &uctx->qp_grp_list, link) {
  620. vf = qp_grp->vf;
  621. if (usnic_vnic_get_index(vf->vnic) == vfid) {
  622. bar = usnic_vnic_get_bar(vf->vnic, 0);
  623. if ((vma->vm_end - vma->vm_start) != bar->len) {
  624. usnic_err("Bar0 Len %lu - Request map %lu\n",
  625. bar->len,
  626. vma->vm_end - vma->vm_start);
  627. mutex_unlock(&us_ibdev->usdev_lock);
  628. return -EINVAL;
  629. }
  630. bus_addr = bar->bus_addr;
  631. len = bar->len;
  632. usnic_dbg("bus: %pa vaddr: %p size: %ld\n",
  633. &bus_addr, bar->vaddr, bar->len);
  634. mutex_unlock(&us_ibdev->usdev_lock);
  635. return remap_pfn_range(vma,
  636. vma->vm_start,
  637. bus_addr >> PAGE_SHIFT,
  638. len, vma->vm_page_prot);
  639. }
  640. }
  641. mutex_unlock(&us_ibdev->usdev_lock);
  642. usnic_err("No VF %u found\n", vfid);
  643. return -EINVAL;
  644. }
  645. /* In ib callbacks section - Start of stub funcs */
  646. struct ib_ah *usnic_ib_create_ah(struct ib_pd *pd,
  647. struct rdma_ah_attr *ah_attr,
  648. struct ib_udata *udata)
  649. {
  650. usnic_dbg("\n");
  651. return ERR_PTR(-EPERM);
  652. }
  653. int usnic_ib_destroy_ah(struct ib_ah *ah)
  654. {
  655. usnic_dbg("\n");
  656. return -EINVAL;
  657. }
  658. int usnic_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  659. const struct ib_send_wr **bad_wr)
  660. {
  661. usnic_dbg("\n");
  662. return -EINVAL;
  663. }
  664. int usnic_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  665. const struct ib_recv_wr **bad_wr)
  666. {
  667. usnic_dbg("\n");
  668. return -EINVAL;
  669. }
  670. int usnic_ib_poll_cq(struct ib_cq *ibcq, int num_entries,
  671. struct ib_wc *wc)
  672. {
  673. usnic_dbg("\n");
  674. return -EINVAL;
  675. }
  676. int usnic_ib_req_notify_cq(struct ib_cq *cq,
  677. enum ib_cq_notify_flags flags)
  678. {
  679. usnic_dbg("\n");
  680. return -EINVAL;
  681. }
  682. struct ib_mr *usnic_ib_get_dma_mr(struct ib_pd *pd, int acc)
  683. {
  684. usnic_dbg("\n");
  685. return ERR_PTR(-ENOMEM);
  686. }
  687. /* In ib callbacks section - End of stub funcs */
  688. /* End of ib callbacks section */