usnic_ib_verbs.c 20 KB

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