usnic_ib_verbs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. }
  191. /* Try to find resources on an unused vf */
  192. list_for_each_entry(vf, &us_ibdev->vf_dev_list, link) {
  193. spin_lock(&vf->lock);
  194. vnic = vf->vnic;
  195. if (vf->qp_grp_ref_cnt == 0 &&
  196. usnic_vnic_check_room(vnic, res_spec) == 0) {
  197. qp_grp = usnic_ib_qp_grp_create(us_ibdev->ufdev, vf,
  198. pd, res_spec,
  199. trans_spec);
  200. spin_unlock(&vf->lock);
  201. goto qp_grp_check;
  202. }
  203. spin_unlock(&vf->lock);
  204. }
  205. usnic_info("No free qp grp found on %s\n", us_ibdev->ib_dev.name);
  206. return ERR_PTR(-ENOMEM);
  207. qp_grp_check:
  208. if (IS_ERR_OR_NULL(qp_grp)) {
  209. usnic_err("Failed to allocate qp_grp\n");
  210. return ERR_PTR(qp_grp ? PTR_ERR(qp_grp) : -ENOMEM);
  211. }
  212. return qp_grp;
  213. }
  214. static void qp_grp_destroy(struct usnic_ib_qp_grp *qp_grp)
  215. {
  216. struct usnic_ib_vf *vf = qp_grp->vf;
  217. WARN_ON(qp_grp->state != IB_QPS_RESET);
  218. spin_lock(&vf->lock);
  219. usnic_ib_qp_grp_destroy(qp_grp);
  220. spin_unlock(&vf->lock);
  221. }
  222. static int create_qp_validate_user_data(struct usnic_ib_create_qp_cmd cmd)
  223. {
  224. if (cmd.spec.trans_type <= USNIC_TRANSPORT_UNKNOWN ||
  225. cmd.spec.trans_type >= USNIC_TRANSPORT_MAX)
  226. return -EINVAL;
  227. return 0;
  228. }
  229. /* Start of ib callback functions */
  230. enum rdma_link_layer usnic_ib_port_link_layer(struct ib_device *device,
  231. u8 port_num)
  232. {
  233. return IB_LINK_LAYER_ETHERNET;
  234. }
  235. int usnic_ib_query_device(struct ib_device *ibdev,
  236. struct ib_device_attr *props,
  237. struct ib_udata *uhw)
  238. {
  239. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  240. union ib_gid gid;
  241. struct ethtool_drvinfo info;
  242. int qp_per_vf;
  243. usnic_dbg("\n");
  244. if (uhw->inlen || uhw->outlen)
  245. return -EINVAL;
  246. mutex_lock(&us_ibdev->usdev_lock);
  247. us_ibdev->netdev->ethtool_ops->get_drvinfo(us_ibdev->netdev, &info);
  248. memset(props, 0, sizeof(*props));
  249. usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
  250. &gid.raw[0]);
  251. memcpy(&props->sys_image_guid, &gid.global.interface_id,
  252. sizeof(gid.global.interface_id));
  253. usnic_ib_fw_string_to_u64(&info.fw_version[0], &props->fw_ver);
  254. props->max_mr_size = USNIC_UIOM_MAX_MR_SIZE;
  255. props->page_size_cap = USNIC_UIOM_PAGE_SIZE;
  256. props->vendor_id = PCI_VENDOR_ID_CISCO;
  257. props->vendor_part_id = PCI_DEVICE_ID_CISCO_VIC_USPACE_NIC;
  258. props->hw_ver = us_ibdev->pdev->subsystem_device;
  259. qp_per_vf = max(us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_WQ],
  260. us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_RQ]);
  261. props->max_qp = qp_per_vf *
  262. kref_read(&us_ibdev->vf_cnt);
  263. props->device_cap_flags = IB_DEVICE_PORT_ACTIVE_EVENT |
  264. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  265. props->max_cq = us_ibdev->vf_res_cnt[USNIC_VNIC_RES_TYPE_CQ] *
  266. kref_read(&us_ibdev->vf_cnt);
  267. props->max_pd = USNIC_UIOM_MAX_PD_CNT;
  268. props->max_mr = USNIC_UIOM_MAX_MR_CNT;
  269. props->local_ca_ack_delay = 0;
  270. props->max_pkeys = 0;
  271. props->atomic_cap = IB_ATOMIC_NONE;
  272. props->masked_atomic_cap = props->atomic_cap;
  273. props->max_qp_rd_atom = 0;
  274. props->max_qp_init_rd_atom = 0;
  275. props->max_res_rd_atom = 0;
  276. props->max_srq = 0;
  277. props->max_srq_wr = 0;
  278. props->max_srq_sge = 0;
  279. props->max_fast_reg_page_list_len = 0;
  280. props->max_mcast_grp = 0;
  281. props->max_mcast_qp_attach = 0;
  282. props->max_total_mcast_qp_attach = 0;
  283. props->max_map_per_fmr = 0;
  284. /* Owned by Userspace
  285. * max_qp_wr, max_sge, max_sge_rd, max_cqe */
  286. mutex_unlock(&us_ibdev->usdev_lock);
  287. return 0;
  288. }
  289. int usnic_ib_query_port(struct ib_device *ibdev, u8 port,
  290. struct ib_port_attr *props)
  291. {
  292. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  293. usnic_dbg("\n");
  294. mutex_lock(&us_ibdev->usdev_lock);
  295. if (ib_get_eth_speed(ibdev, port, &props->active_speed,
  296. &props->active_width)) {
  297. mutex_unlock(&us_ibdev->usdev_lock);
  298. return -EINVAL;
  299. }
  300. /* props being zeroed by the caller, avoid zeroing it here */
  301. props->lid = 0;
  302. props->lmc = 1;
  303. props->sm_lid = 0;
  304. props->sm_sl = 0;
  305. if (!us_ibdev->ufdev->link_up) {
  306. props->state = IB_PORT_DOWN;
  307. props->phys_state = 3;
  308. } else if (!us_ibdev->ufdev->inaddr) {
  309. props->state = IB_PORT_INIT;
  310. props->phys_state = 4;
  311. } else {
  312. props->state = IB_PORT_ACTIVE;
  313. props->phys_state = 5;
  314. }
  315. props->port_cap_flags = 0;
  316. props->gid_tbl_len = 1;
  317. props->pkey_tbl_len = 1;
  318. props->bad_pkey_cntr = 0;
  319. props->qkey_viol_cntr = 0;
  320. props->max_mtu = IB_MTU_4096;
  321. props->active_mtu = iboe_get_mtu(us_ibdev->ufdev->mtu);
  322. /* Userspace will adjust for hdrs */
  323. props->max_msg_sz = us_ibdev->ufdev->mtu;
  324. props->max_vl_num = 1;
  325. mutex_unlock(&us_ibdev->usdev_lock);
  326. return 0;
  327. }
  328. int usnic_ib_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
  329. int qp_attr_mask,
  330. struct ib_qp_init_attr *qp_init_attr)
  331. {
  332. struct usnic_ib_qp_grp *qp_grp;
  333. struct usnic_ib_vf *vf;
  334. int err;
  335. usnic_dbg("\n");
  336. memset(qp_attr, 0, sizeof(*qp_attr));
  337. memset(qp_init_attr, 0, sizeof(*qp_init_attr));
  338. qp_grp = to_uqp_grp(qp);
  339. vf = qp_grp->vf;
  340. mutex_lock(&vf->pf->usdev_lock);
  341. usnic_dbg("\n");
  342. qp_attr->qp_state = qp_grp->state;
  343. qp_attr->cur_qp_state = qp_grp->state;
  344. switch (qp_grp->ibqp.qp_type) {
  345. case IB_QPT_UD:
  346. qp_attr->qkey = 0;
  347. break;
  348. default:
  349. usnic_err("Unexpected qp_type %d\n", qp_grp->ibqp.qp_type);
  350. err = -EINVAL;
  351. goto err_out;
  352. }
  353. mutex_unlock(&vf->pf->usdev_lock);
  354. return 0;
  355. err_out:
  356. mutex_unlock(&vf->pf->usdev_lock);
  357. return err;
  358. }
  359. int usnic_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  360. union ib_gid *gid)
  361. {
  362. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  363. usnic_dbg("\n");
  364. if (index > 1)
  365. return -EINVAL;
  366. mutex_lock(&us_ibdev->usdev_lock);
  367. memset(&(gid->raw[0]), 0, sizeof(gid->raw));
  368. usnic_mac_ip_to_gid(us_ibdev->ufdev->mac, us_ibdev->ufdev->inaddr,
  369. &gid->raw[0]);
  370. mutex_unlock(&us_ibdev->usdev_lock);
  371. return 0;
  372. }
  373. struct net_device *usnic_get_netdev(struct ib_device *device, u8 port_num)
  374. {
  375. struct usnic_ib_dev *us_ibdev = to_usdev(device);
  376. if (us_ibdev->netdev)
  377. dev_hold(us_ibdev->netdev);
  378. return us_ibdev->netdev;
  379. }
  380. int usnic_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  381. u16 *pkey)
  382. {
  383. if (index > 1)
  384. return -EINVAL;
  385. *pkey = 0xffff;
  386. return 0;
  387. }
  388. struct ib_pd *usnic_ib_alloc_pd(struct ib_device *ibdev,
  389. struct ib_ucontext *context,
  390. struct ib_udata *udata)
  391. {
  392. struct usnic_ib_pd *pd;
  393. void *umem_pd;
  394. usnic_dbg("\n");
  395. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  396. if (!pd)
  397. return ERR_PTR(-ENOMEM);
  398. umem_pd = pd->umem_pd = usnic_uiom_alloc_pd();
  399. if (IS_ERR_OR_NULL(umem_pd)) {
  400. kfree(pd);
  401. return ERR_PTR(umem_pd ? PTR_ERR(umem_pd) : -ENOMEM);
  402. }
  403. usnic_info("domain 0x%p allocated for context 0x%p and device %s\n",
  404. pd, context, ibdev->name);
  405. return &pd->ibpd;
  406. }
  407. int usnic_ib_dealloc_pd(struct ib_pd *pd)
  408. {
  409. usnic_info("freeing domain 0x%p\n", pd);
  410. usnic_uiom_dealloc_pd((to_upd(pd))->umem_pd);
  411. kfree(pd);
  412. return 0;
  413. }
  414. struct ib_qp *usnic_ib_create_qp(struct ib_pd *pd,
  415. struct ib_qp_init_attr *init_attr,
  416. struct ib_udata *udata)
  417. {
  418. int err;
  419. struct usnic_ib_dev *us_ibdev;
  420. struct usnic_ib_qp_grp *qp_grp;
  421. struct usnic_ib_ucontext *ucontext;
  422. int cq_cnt;
  423. struct usnic_vnic_res_spec res_spec;
  424. struct usnic_ib_create_qp_cmd cmd;
  425. struct usnic_transport_spec trans_spec;
  426. usnic_dbg("\n");
  427. ucontext = to_uucontext(pd->uobject->context);
  428. us_ibdev = to_usdev(pd->device);
  429. if (init_attr->create_flags)
  430. return ERR_PTR(-EINVAL);
  431. err = ib_copy_from_udata(&cmd, udata, sizeof(cmd));
  432. if (err) {
  433. usnic_err("%s: cannot copy udata for create_qp\n",
  434. us_ibdev->ib_dev.name);
  435. return ERR_PTR(-EINVAL);
  436. }
  437. err = create_qp_validate_user_data(cmd);
  438. if (err) {
  439. usnic_err("%s: Failed to validate user data\n",
  440. us_ibdev->ib_dev.name);
  441. return ERR_PTR(-EINVAL);
  442. }
  443. if (init_attr->qp_type != IB_QPT_UD) {
  444. usnic_err("%s asked to make a non-UD QP: %d\n",
  445. us_ibdev->ib_dev.name, init_attr->qp_type);
  446. return ERR_PTR(-EINVAL);
  447. }
  448. trans_spec = cmd.spec;
  449. mutex_lock(&us_ibdev->usdev_lock);
  450. cq_cnt = (init_attr->send_cq == init_attr->recv_cq) ? 1 : 2;
  451. res_spec = min_transport_spec[trans_spec.trans_type];
  452. usnic_vnic_res_spec_update(&res_spec, USNIC_VNIC_RES_TYPE_CQ, cq_cnt);
  453. qp_grp = find_free_vf_and_create_qp_grp(us_ibdev, to_upd(pd),
  454. &trans_spec,
  455. &res_spec);
  456. if (IS_ERR_OR_NULL(qp_grp)) {
  457. err = qp_grp ? PTR_ERR(qp_grp) : -ENOMEM;
  458. goto out_release_mutex;
  459. }
  460. err = usnic_ib_fill_create_qp_resp(qp_grp, udata);
  461. if (err) {
  462. err = -EBUSY;
  463. goto out_release_qp_grp;
  464. }
  465. qp_grp->ctx = ucontext;
  466. list_add_tail(&qp_grp->link, &ucontext->qp_grp_list);
  467. usnic_ib_log_vf(qp_grp->vf);
  468. mutex_unlock(&us_ibdev->usdev_lock);
  469. return &qp_grp->ibqp;
  470. out_release_qp_grp:
  471. qp_grp_destroy(qp_grp);
  472. out_release_mutex:
  473. mutex_unlock(&us_ibdev->usdev_lock);
  474. return ERR_PTR(err);
  475. }
  476. int usnic_ib_destroy_qp(struct ib_qp *qp)
  477. {
  478. struct usnic_ib_qp_grp *qp_grp;
  479. struct usnic_ib_vf *vf;
  480. usnic_dbg("\n");
  481. qp_grp = to_uqp_grp(qp);
  482. vf = qp_grp->vf;
  483. mutex_lock(&vf->pf->usdev_lock);
  484. if (usnic_ib_qp_grp_modify(qp_grp, IB_QPS_RESET, NULL)) {
  485. usnic_err("Failed to move qp grp %u to reset\n",
  486. qp_grp->grp_id);
  487. }
  488. list_del(&qp_grp->link);
  489. qp_grp_destroy(qp_grp);
  490. mutex_unlock(&vf->pf->usdev_lock);
  491. return 0;
  492. }
  493. int usnic_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  494. int attr_mask, struct ib_udata *udata)
  495. {
  496. struct usnic_ib_qp_grp *qp_grp;
  497. int status;
  498. usnic_dbg("\n");
  499. qp_grp = to_uqp_grp(ibqp);
  500. mutex_lock(&qp_grp->vf->pf->usdev_lock);
  501. if ((attr_mask & IB_QP_PORT) && attr->port_num != 1) {
  502. /* usnic devices only have one port */
  503. status = -EINVAL;
  504. goto out_unlock;
  505. }
  506. if (attr_mask & IB_QP_STATE) {
  507. status = usnic_ib_qp_grp_modify(qp_grp, attr->qp_state, NULL);
  508. } else {
  509. usnic_err("Unhandled request, attr_mask=0x%x\n", attr_mask);
  510. status = -EINVAL;
  511. }
  512. out_unlock:
  513. mutex_unlock(&qp_grp->vf->pf->usdev_lock);
  514. return status;
  515. }
  516. struct ib_cq *usnic_ib_create_cq(struct ib_device *ibdev,
  517. const struct ib_cq_init_attr *attr,
  518. struct ib_ucontext *context,
  519. struct ib_udata *udata)
  520. {
  521. struct ib_cq *cq;
  522. usnic_dbg("\n");
  523. if (attr->flags)
  524. return ERR_PTR(-EINVAL);
  525. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  526. if (!cq)
  527. return ERR_PTR(-EBUSY);
  528. return cq;
  529. }
  530. int usnic_ib_destroy_cq(struct ib_cq *cq)
  531. {
  532. usnic_dbg("\n");
  533. kfree(cq);
  534. return 0;
  535. }
  536. struct ib_mr *usnic_ib_reg_mr(struct ib_pd *pd, u64 start, u64 length,
  537. u64 virt_addr, int access_flags,
  538. struct ib_udata *udata)
  539. {
  540. struct usnic_ib_mr *mr;
  541. int err;
  542. usnic_dbg("start 0x%llx va 0x%llx length 0x%llx\n", start,
  543. virt_addr, length);
  544. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  545. if (!mr)
  546. return ERR_PTR(-ENOMEM);
  547. mr->umem = usnic_uiom_reg_get(to_upd(pd)->umem_pd, start, length,
  548. access_flags, 0);
  549. if (IS_ERR_OR_NULL(mr->umem)) {
  550. err = mr->umem ? PTR_ERR(mr->umem) : -EFAULT;
  551. goto err_free;
  552. }
  553. mr->ibmr.lkey = mr->ibmr.rkey = 0;
  554. return &mr->ibmr;
  555. err_free:
  556. kfree(mr);
  557. return ERR_PTR(err);
  558. }
  559. int usnic_ib_dereg_mr(struct ib_mr *ibmr)
  560. {
  561. struct usnic_ib_mr *mr = to_umr(ibmr);
  562. usnic_dbg("va 0x%lx length 0x%zx\n", mr->umem->va, mr->umem->length);
  563. usnic_uiom_reg_release(mr->umem, ibmr->pd->uobject->context->closing);
  564. kfree(mr);
  565. return 0;
  566. }
  567. struct ib_ucontext *usnic_ib_alloc_ucontext(struct ib_device *ibdev,
  568. struct ib_udata *udata)
  569. {
  570. struct usnic_ib_ucontext *context;
  571. struct usnic_ib_dev *us_ibdev = to_usdev(ibdev);
  572. usnic_dbg("\n");
  573. context = kmalloc(sizeof(*context), GFP_KERNEL);
  574. if (!context)
  575. return ERR_PTR(-ENOMEM);
  576. INIT_LIST_HEAD(&context->qp_grp_list);
  577. mutex_lock(&us_ibdev->usdev_lock);
  578. list_add_tail(&context->link, &us_ibdev->ctx_list);
  579. mutex_unlock(&us_ibdev->usdev_lock);
  580. return &context->ibucontext;
  581. }
  582. int usnic_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  583. {
  584. struct usnic_ib_ucontext *context = to_uucontext(ibcontext);
  585. struct usnic_ib_dev *us_ibdev = to_usdev(ibcontext->device);
  586. usnic_dbg("\n");
  587. mutex_lock(&us_ibdev->usdev_lock);
  588. BUG_ON(!list_empty(&context->qp_grp_list));
  589. list_del(&context->link);
  590. mutex_unlock(&us_ibdev->usdev_lock);
  591. kfree(context);
  592. return 0;
  593. }
  594. int usnic_ib_mmap(struct ib_ucontext *context,
  595. struct vm_area_struct *vma)
  596. {
  597. struct usnic_ib_ucontext *uctx = to_ucontext(context);
  598. struct usnic_ib_dev *us_ibdev;
  599. struct usnic_ib_qp_grp *qp_grp;
  600. struct usnic_ib_vf *vf;
  601. struct vnic_dev_bar *bar;
  602. dma_addr_t bus_addr;
  603. unsigned int len;
  604. unsigned int vfid;
  605. usnic_dbg("\n");
  606. us_ibdev = to_usdev(context->device);
  607. vma->vm_flags |= VM_IO;
  608. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  609. vfid = vma->vm_pgoff;
  610. usnic_dbg("Page Offset %lu PAGE_SHIFT %u VFID %u\n",
  611. vma->vm_pgoff, PAGE_SHIFT, vfid);
  612. mutex_lock(&us_ibdev->usdev_lock);
  613. list_for_each_entry(qp_grp, &uctx->qp_grp_list, link) {
  614. vf = qp_grp->vf;
  615. if (usnic_vnic_get_index(vf->vnic) == vfid) {
  616. bar = usnic_vnic_get_bar(vf->vnic, 0);
  617. if ((vma->vm_end - vma->vm_start) != bar->len) {
  618. usnic_err("Bar0 Len %lu - Request map %lu\n",
  619. bar->len,
  620. vma->vm_end - vma->vm_start);
  621. mutex_unlock(&us_ibdev->usdev_lock);
  622. return -EINVAL;
  623. }
  624. bus_addr = bar->bus_addr;
  625. len = bar->len;
  626. usnic_dbg("bus: %pa vaddr: %p size: %ld\n",
  627. &bus_addr, bar->vaddr, bar->len);
  628. mutex_unlock(&us_ibdev->usdev_lock);
  629. return remap_pfn_range(vma,
  630. vma->vm_start,
  631. bus_addr >> PAGE_SHIFT,
  632. len, vma->vm_page_prot);
  633. }
  634. }
  635. mutex_unlock(&us_ibdev->usdev_lock);
  636. usnic_err("No VF %u found\n", vfid);
  637. return -EINVAL;
  638. }
  639. /* In ib callbacks section - Start of stub funcs */
  640. struct ib_ah *usnic_ib_create_ah(struct ib_pd *pd,
  641. struct rdma_ah_attr *ah_attr,
  642. struct ib_udata *udata)
  643. {
  644. usnic_dbg("\n");
  645. return ERR_PTR(-EPERM);
  646. }
  647. int usnic_ib_destroy_ah(struct ib_ah *ah)
  648. {
  649. usnic_dbg("\n");
  650. return -EINVAL;
  651. }
  652. int usnic_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  653. struct ib_send_wr **bad_wr)
  654. {
  655. usnic_dbg("\n");
  656. return -EINVAL;
  657. }
  658. int usnic_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  659. struct ib_recv_wr **bad_wr)
  660. {
  661. usnic_dbg("\n");
  662. return -EINVAL;
  663. }
  664. int usnic_ib_poll_cq(struct ib_cq *ibcq, int num_entries,
  665. struct ib_wc *wc)
  666. {
  667. usnic_dbg("\n");
  668. return -EINVAL;
  669. }
  670. int usnic_ib_req_notify_cq(struct ib_cq *cq,
  671. enum ib_cq_notify_flags flags)
  672. {
  673. usnic_dbg("\n");
  674. return -EINVAL;
  675. }
  676. struct ib_mr *usnic_ib_get_dma_mr(struct ib_pd *pd, int acc)
  677. {
  678. usnic_dbg("\n");
  679. return ERR_PTR(-ENOMEM);
  680. }
  681. /* In ib callbacks section - End of stub funcs */
  682. /* End of ib callbacks section */