provider.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Copyright (c) 2009-2010 Chelsio, 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. * OpenIB.org 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. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/device.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/delay.h>
  38. #include <linux/errno.h>
  39. #include <linux/list.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/ethtool.h>
  42. #include <linux/rtnetlink.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/io.h>
  45. #include <asm/irq.h>
  46. #include <asm/byteorder.h>
  47. #include <rdma/iw_cm.h>
  48. #include <rdma/ib_verbs.h>
  49. #include <rdma/ib_smi.h>
  50. #include <rdma/ib_umem.h>
  51. #include <rdma/ib_user_verbs.h>
  52. #include "iw_cxgb4.h"
  53. static int fastreg_support = 1;
  54. module_param(fastreg_support, int, 0644);
  55. MODULE_PARM_DESC(fastreg_support, "Advertise fastreg support (default=1)");
  56. static struct ib_ah *c4iw_ah_create(struct ib_pd *pd,
  57. struct rdma_ah_attr *ah_attr,
  58. struct ib_udata *udata)
  59. {
  60. return ERR_PTR(-ENOSYS);
  61. }
  62. static int c4iw_ah_destroy(struct ib_ah *ah)
  63. {
  64. return -ENOSYS;
  65. }
  66. static int c4iw_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  67. {
  68. return -ENOSYS;
  69. }
  70. static int c4iw_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  71. {
  72. return -ENOSYS;
  73. }
  74. static int c4iw_process_mad(struct ib_device *ibdev, int mad_flags,
  75. u8 port_num, const struct ib_wc *in_wc,
  76. const struct ib_grh *in_grh,
  77. const struct ib_mad_hdr *in_mad,
  78. size_t in_mad_size,
  79. struct ib_mad_hdr *out_mad,
  80. size_t *out_mad_size,
  81. u16 *out_mad_pkey_index)
  82. {
  83. return -ENOSYS;
  84. }
  85. void _c4iw_free_ucontext(struct kref *kref)
  86. {
  87. struct c4iw_ucontext *ucontext;
  88. struct c4iw_dev *rhp;
  89. struct c4iw_mm_entry *mm, *tmp;
  90. ucontext = container_of(kref, struct c4iw_ucontext, kref);
  91. rhp = to_c4iw_dev(ucontext->ibucontext.device);
  92. pr_debug("ucontext %p\n", ucontext);
  93. list_for_each_entry_safe(mm, tmp, &ucontext->mmaps, entry)
  94. kfree(mm);
  95. c4iw_release_dev_ucontext(&rhp->rdev, &ucontext->uctx);
  96. kfree(ucontext);
  97. }
  98. static int c4iw_dealloc_ucontext(struct ib_ucontext *context)
  99. {
  100. struct c4iw_ucontext *ucontext = to_c4iw_ucontext(context);
  101. pr_debug("context %p\n", context);
  102. c4iw_put_ucontext(ucontext);
  103. return 0;
  104. }
  105. static struct ib_ucontext *c4iw_alloc_ucontext(struct ib_device *ibdev,
  106. struct ib_udata *udata)
  107. {
  108. struct c4iw_ucontext *context;
  109. struct c4iw_dev *rhp = to_c4iw_dev(ibdev);
  110. struct c4iw_alloc_ucontext_resp uresp;
  111. int ret = 0;
  112. struct c4iw_mm_entry *mm = NULL;
  113. pr_debug("ibdev %p\n", ibdev);
  114. context = kzalloc(sizeof(*context), GFP_KERNEL);
  115. if (!context) {
  116. ret = -ENOMEM;
  117. goto err;
  118. }
  119. c4iw_init_dev_ucontext(&rhp->rdev, &context->uctx);
  120. INIT_LIST_HEAD(&context->mmaps);
  121. spin_lock_init(&context->mmap_lock);
  122. kref_init(&context->kref);
  123. if (udata->outlen < sizeof(uresp) - sizeof(uresp.reserved)) {
  124. pr_err_once("Warning - downlevel libcxgb4 (non-fatal), device status page disabled\n");
  125. rhp->rdev.flags |= T4_STATUS_PAGE_DISABLED;
  126. } else {
  127. mm = kmalloc(sizeof(*mm), GFP_KERNEL);
  128. if (!mm) {
  129. ret = -ENOMEM;
  130. goto err_free;
  131. }
  132. uresp.status_page_size = PAGE_SIZE;
  133. spin_lock(&context->mmap_lock);
  134. uresp.status_page_key = context->key;
  135. context->key += PAGE_SIZE;
  136. spin_unlock(&context->mmap_lock);
  137. ret = ib_copy_to_udata(udata, &uresp,
  138. sizeof(uresp) - sizeof(uresp.reserved));
  139. if (ret)
  140. goto err_mm;
  141. mm->key = uresp.status_page_key;
  142. mm->addr = virt_to_phys(rhp->rdev.status_page);
  143. mm->len = PAGE_SIZE;
  144. insert_mmap(context, mm);
  145. }
  146. return &context->ibucontext;
  147. err_mm:
  148. kfree(mm);
  149. err_free:
  150. kfree(context);
  151. err:
  152. return ERR_PTR(ret);
  153. }
  154. static int c4iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  155. {
  156. int len = vma->vm_end - vma->vm_start;
  157. u32 key = vma->vm_pgoff << PAGE_SHIFT;
  158. struct c4iw_rdev *rdev;
  159. int ret = 0;
  160. struct c4iw_mm_entry *mm;
  161. struct c4iw_ucontext *ucontext;
  162. u64 addr;
  163. pr_debug("pgoff 0x%lx key 0x%x len %d\n", vma->vm_pgoff,
  164. key, len);
  165. if (vma->vm_start & (PAGE_SIZE-1))
  166. return -EINVAL;
  167. rdev = &(to_c4iw_dev(context->device)->rdev);
  168. ucontext = to_c4iw_ucontext(context);
  169. mm = remove_mmap(ucontext, key, len);
  170. if (!mm)
  171. return -EINVAL;
  172. addr = mm->addr;
  173. kfree(mm);
  174. if ((addr >= pci_resource_start(rdev->lldi.pdev, 0)) &&
  175. (addr < (pci_resource_start(rdev->lldi.pdev, 0) +
  176. pci_resource_len(rdev->lldi.pdev, 0)))) {
  177. /*
  178. * MA_SYNC register...
  179. */
  180. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  181. ret = io_remap_pfn_range(vma, vma->vm_start,
  182. addr >> PAGE_SHIFT,
  183. len, vma->vm_page_prot);
  184. } else if ((addr >= pci_resource_start(rdev->lldi.pdev, 2)) &&
  185. (addr < (pci_resource_start(rdev->lldi.pdev, 2) +
  186. pci_resource_len(rdev->lldi.pdev, 2)))) {
  187. /*
  188. * Map user DB or OCQP memory...
  189. */
  190. if (addr >= rdev->oc_mw_pa)
  191. vma->vm_page_prot = t4_pgprot_wc(vma->vm_page_prot);
  192. else {
  193. if (!is_t4(rdev->lldi.adapter_type))
  194. vma->vm_page_prot =
  195. t4_pgprot_wc(vma->vm_page_prot);
  196. else
  197. vma->vm_page_prot =
  198. pgprot_noncached(vma->vm_page_prot);
  199. }
  200. ret = io_remap_pfn_range(vma, vma->vm_start,
  201. addr >> PAGE_SHIFT,
  202. len, vma->vm_page_prot);
  203. } else {
  204. /*
  205. * Map WQ or CQ contig dma memory...
  206. */
  207. ret = remap_pfn_range(vma, vma->vm_start,
  208. addr >> PAGE_SHIFT,
  209. len, vma->vm_page_prot);
  210. }
  211. return ret;
  212. }
  213. static int c4iw_deallocate_pd(struct ib_pd *pd)
  214. {
  215. struct c4iw_dev *rhp;
  216. struct c4iw_pd *php;
  217. php = to_c4iw_pd(pd);
  218. rhp = php->rhp;
  219. pr_debug("ibpd %p pdid 0x%x\n", pd, php->pdid);
  220. c4iw_put_resource(&rhp->rdev.resource.pdid_table, php->pdid);
  221. mutex_lock(&rhp->rdev.stats.lock);
  222. rhp->rdev.stats.pd.cur--;
  223. mutex_unlock(&rhp->rdev.stats.lock);
  224. kfree(php);
  225. return 0;
  226. }
  227. static struct ib_pd *c4iw_allocate_pd(struct ib_device *ibdev,
  228. struct ib_ucontext *context,
  229. struct ib_udata *udata)
  230. {
  231. struct c4iw_pd *php;
  232. u32 pdid;
  233. struct c4iw_dev *rhp;
  234. pr_debug("ibdev %p\n", ibdev);
  235. rhp = (struct c4iw_dev *) ibdev;
  236. pdid = c4iw_get_resource(&rhp->rdev.resource.pdid_table);
  237. if (!pdid)
  238. return ERR_PTR(-EINVAL);
  239. php = kzalloc(sizeof(*php), GFP_KERNEL);
  240. if (!php) {
  241. c4iw_put_resource(&rhp->rdev.resource.pdid_table, pdid);
  242. return ERR_PTR(-ENOMEM);
  243. }
  244. php->pdid = pdid;
  245. php->rhp = rhp;
  246. if (context) {
  247. struct c4iw_alloc_pd_resp uresp = {.pdid = php->pdid};
  248. if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
  249. c4iw_deallocate_pd(&php->ibpd);
  250. return ERR_PTR(-EFAULT);
  251. }
  252. }
  253. mutex_lock(&rhp->rdev.stats.lock);
  254. rhp->rdev.stats.pd.cur++;
  255. if (rhp->rdev.stats.pd.cur > rhp->rdev.stats.pd.max)
  256. rhp->rdev.stats.pd.max = rhp->rdev.stats.pd.cur;
  257. mutex_unlock(&rhp->rdev.stats.lock);
  258. pr_debug("pdid 0x%0x ptr 0x%p\n", pdid, php);
  259. return &php->ibpd;
  260. }
  261. static int c4iw_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  262. u16 *pkey)
  263. {
  264. pr_debug("ibdev %p\n", ibdev);
  265. *pkey = 0;
  266. return 0;
  267. }
  268. static int c4iw_query_gid(struct ib_device *ibdev, u8 port, int index,
  269. union ib_gid *gid)
  270. {
  271. struct c4iw_dev *dev;
  272. pr_debug("ibdev %p, port %d, index %d, gid %p\n",
  273. ibdev, port, index, gid);
  274. if (!port)
  275. return -EINVAL;
  276. dev = to_c4iw_dev(ibdev);
  277. memset(&(gid->raw[0]), 0, sizeof(gid->raw));
  278. memcpy(&(gid->raw[0]), dev->rdev.lldi.ports[port-1]->dev_addr, 6);
  279. return 0;
  280. }
  281. static int c4iw_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
  282. struct ib_udata *uhw)
  283. {
  284. struct c4iw_dev *dev;
  285. pr_debug("ibdev %p\n", ibdev);
  286. if (uhw->inlen || uhw->outlen)
  287. return -EINVAL;
  288. dev = to_c4iw_dev(ibdev);
  289. memset(props, 0, sizeof *props);
  290. memcpy(&props->sys_image_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);
  291. props->hw_ver = CHELSIO_CHIP_RELEASE(dev->rdev.lldi.adapter_type);
  292. props->fw_ver = dev->rdev.lldi.fw_vers;
  293. props->device_cap_flags = dev->device_cap_flags;
  294. props->page_size_cap = T4_PAGESIZE_MASK;
  295. props->vendor_id = (u32)dev->rdev.lldi.pdev->vendor;
  296. props->vendor_part_id = (u32)dev->rdev.lldi.pdev->device;
  297. props->max_mr_size = T4_MAX_MR_SIZE;
  298. props->max_qp = dev->rdev.lldi.vr->qp.size / 2;
  299. props->max_qp_wr = dev->rdev.hw_queue.t4_max_qp_depth;
  300. props->max_sge = T4_MAX_RECV_SGE;
  301. props->max_sge_rd = 1;
  302. props->max_res_rd_atom = dev->rdev.lldi.max_ird_adapter;
  303. props->max_qp_rd_atom = min(dev->rdev.lldi.max_ordird_qp,
  304. c4iw_max_read_depth);
  305. props->max_qp_init_rd_atom = props->max_qp_rd_atom;
  306. props->max_cq = dev->rdev.lldi.vr->qp.size;
  307. props->max_cqe = dev->rdev.hw_queue.t4_max_cq_depth;
  308. props->max_mr = c4iw_num_stags(&dev->rdev);
  309. props->max_pd = T4_MAX_NUM_PD;
  310. props->local_ca_ack_delay = 0;
  311. props->max_fast_reg_page_list_len =
  312. t4_max_fr_depth(dev->rdev.lldi.ulptx_memwrite_dsgl && use_dsgl);
  313. return 0;
  314. }
  315. static int c4iw_query_port(struct ib_device *ibdev, u8 port,
  316. struct ib_port_attr *props)
  317. {
  318. struct c4iw_dev *dev;
  319. struct net_device *netdev;
  320. struct in_device *inetdev;
  321. pr_debug("ibdev %p\n", ibdev);
  322. dev = to_c4iw_dev(ibdev);
  323. netdev = dev->rdev.lldi.ports[port-1];
  324. /* props being zeroed by the caller, avoid zeroing it here */
  325. props->max_mtu = IB_MTU_4096;
  326. props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
  327. if (!netif_carrier_ok(netdev))
  328. props->state = IB_PORT_DOWN;
  329. else {
  330. inetdev = in_dev_get(netdev);
  331. if (inetdev) {
  332. if (inetdev->ifa_list)
  333. props->state = IB_PORT_ACTIVE;
  334. else
  335. props->state = IB_PORT_INIT;
  336. in_dev_put(inetdev);
  337. } else
  338. props->state = IB_PORT_INIT;
  339. }
  340. props->port_cap_flags =
  341. IB_PORT_CM_SUP |
  342. IB_PORT_SNMP_TUNNEL_SUP |
  343. IB_PORT_REINIT_SUP |
  344. IB_PORT_DEVICE_MGMT_SUP |
  345. IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
  346. props->gid_tbl_len = 1;
  347. props->pkey_tbl_len = 1;
  348. props->active_width = 2;
  349. props->active_speed = IB_SPEED_DDR;
  350. props->max_msg_sz = -1;
  351. return 0;
  352. }
  353. static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
  354. char *buf)
  355. {
  356. struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
  357. ibdev.dev);
  358. pr_debug("dev 0x%p\n", dev);
  359. return sprintf(buf, "%d\n",
  360. CHELSIO_CHIP_RELEASE(c4iw_dev->rdev.lldi.adapter_type));
  361. }
  362. static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
  363. char *buf)
  364. {
  365. struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
  366. ibdev.dev);
  367. struct ethtool_drvinfo info;
  368. struct net_device *lldev = c4iw_dev->rdev.lldi.ports[0];
  369. pr_debug("dev 0x%p\n", dev);
  370. lldev->ethtool_ops->get_drvinfo(lldev, &info);
  371. return sprintf(buf, "%s\n", info.driver);
  372. }
  373. static ssize_t show_board(struct device *dev, struct device_attribute *attr,
  374. char *buf)
  375. {
  376. struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
  377. ibdev.dev);
  378. pr_debug("dev 0x%p\n", dev);
  379. return sprintf(buf, "%x.%x\n", c4iw_dev->rdev.lldi.pdev->vendor,
  380. c4iw_dev->rdev.lldi.pdev->device);
  381. }
  382. enum counters {
  383. IP4INSEGS,
  384. IP4OUTSEGS,
  385. IP4RETRANSSEGS,
  386. IP4OUTRSTS,
  387. IP6INSEGS,
  388. IP6OUTSEGS,
  389. IP6RETRANSSEGS,
  390. IP6OUTRSTS,
  391. NR_COUNTERS
  392. };
  393. static const char * const names[] = {
  394. [IP4INSEGS] = "ip4InSegs",
  395. [IP4OUTSEGS] = "ip4OutSegs",
  396. [IP4RETRANSSEGS] = "ip4RetransSegs",
  397. [IP4OUTRSTS] = "ip4OutRsts",
  398. [IP6INSEGS] = "ip6InSegs",
  399. [IP6OUTSEGS] = "ip6OutSegs",
  400. [IP6RETRANSSEGS] = "ip6RetransSegs",
  401. [IP6OUTRSTS] = "ip6OutRsts"
  402. };
  403. static struct rdma_hw_stats *c4iw_alloc_stats(struct ib_device *ibdev,
  404. u8 port_num)
  405. {
  406. BUILD_BUG_ON(ARRAY_SIZE(names) != NR_COUNTERS);
  407. if (port_num != 0)
  408. return NULL;
  409. return rdma_alloc_hw_stats_struct(names, NR_COUNTERS,
  410. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  411. }
  412. static int c4iw_get_mib(struct ib_device *ibdev,
  413. struct rdma_hw_stats *stats,
  414. u8 port, int index)
  415. {
  416. struct tp_tcp_stats v4, v6;
  417. struct c4iw_dev *c4iw_dev = to_c4iw_dev(ibdev);
  418. cxgb4_get_tcp_stats(c4iw_dev->rdev.lldi.pdev, &v4, &v6);
  419. stats->value[IP4INSEGS] = v4.tcp_in_segs;
  420. stats->value[IP4OUTSEGS] = v4.tcp_out_segs;
  421. stats->value[IP4RETRANSSEGS] = v4.tcp_retrans_segs;
  422. stats->value[IP4OUTRSTS] = v4.tcp_out_rsts;
  423. stats->value[IP6INSEGS] = v6.tcp_in_segs;
  424. stats->value[IP6OUTSEGS] = v6.tcp_out_segs;
  425. stats->value[IP6RETRANSSEGS] = v6.tcp_retrans_segs;
  426. stats->value[IP6OUTRSTS] = v6.tcp_out_rsts;
  427. return stats->num_counters;
  428. }
  429. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  430. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  431. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  432. static struct device_attribute *c4iw_class_attributes[] = {
  433. &dev_attr_hw_rev,
  434. &dev_attr_hca_type,
  435. &dev_attr_board_id,
  436. };
  437. static int c4iw_port_immutable(struct ib_device *ibdev, u8 port_num,
  438. struct ib_port_immutable *immutable)
  439. {
  440. struct ib_port_attr attr;
  441. int err;
  442. immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
  443. err = ib_query_port(ibdev, port_num, &attr);
  444. if (err)
  445. return err;
  446. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  447. immutable->gid_tbl_len = attr.gid_tbl_len;
  448. return 0;
  449. }
  450. static void get_dev_fw_str(struct ib_device *dev, char *str)
  451. {
  452. struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev,
  453. ibdev);
  454. pr_debug("dev 0x%p\n", dev);
  455. snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u.%u.%u",
  456. FW_HDR_FW_VER_MAJOR_G(c4iw_dev->rdev.lldi.fw_vers),
  457. FW_HDR_FW_VER_MINOR_G(c4iw_dev->rdev.lldi.fw_vers),
  458. FW_HDR_FW_VER_MICRO_G(c4iw_dev->rdev.lldi.fw_vers),
  459. FW_HDR_FW_VER_BUILD_G(c4iw_dev->rdev.lldi.fw_vers));
  460. }
  461. static struct net_device *get_netdev(struct ib_device *dev, u8 port)
  462. {
  463. struct c4iw_dev *c4iw_dev = container_of(dev, struct c4iw_dev, ibdev);
  464. struct c4iw_rdev *rdev = &c4iw_dev->rdev;
  465. struct net_device *ndev;
  466. if (!port || port > rdev->lldi.nports)
  467. return NULL;
  468. rcu_read_lock();
  469. ndev = rdev->lldi.ports[port - 1];
  470. if (ndev)
  471. dev_hold(ndev);
  472. rcu_read_unlock();
  473. return ndev;
  474. }
  475. void c4iw_register_device(struct work_struct *work)
  476. {
  477. int ret;
  478. int i;
  479. struct uld_ctx *ctx = container_of(work, struct uld_ctx, reg_work);
  480. struct c4iw_dev *dev = ctx->dev;
  481. pr_debug("c4iw_dev %p\n", dev);
  482. strlcpy(dev->ibdev.name, "cxgb4_%d", IB_DEVICE_NAME_MAX);
  483. memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
  484. memcpy(&dev->ibdev.node_guid, dev->rdev.lldi.ports[0]->dev_addr, 6);
  485. dev->ibdev.owner = THIS_MODULE;
  486. dev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY | IB_DEVICE_MEM_WINDOW;
  487. if (fastreg_support)
  488. dev->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  489. dev->ibdev.local_dma_lkey = 0;
  490. dev->ibdev.uverbs_cmd_mask =
  491. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  492. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  493. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  494. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  495. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  496. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  497. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  498. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  499. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  500. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  501. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  502. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  503. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  504. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  505. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  506. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  507. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  508. (1ull << IB_USER_VERBS_CMD_POST_RECV);
  509. dev->ibdev.node_type = RDMA_NODE_RNIC;
  510. BUILD_BUG_ON(sizeof(C4IW_NODE_DESC) > IB_DEVICE_NODE_DESC_MAX);
  511. memcpy(dev->ibdev.node_desc, C4IW_NODE_DESC, sizeof(C4IW_NODE_DESC));
  512. dev->ibdev.phys_port_cnt = dev->rdev.lldi.nports;
  513. dev->ibdev.num_comp_vectors = dev->rdev.lldi.nciq;
  514. dev->ibdev.dev.parent = &dev->rdev.lldi.pdev->dev;
  515. dev->ibdev.query_device = c4iw_query_device;
  516. dev->ibdev.query_port = c4iw_query_port;
  517. dev->ibdev.query_pkey = c4iw_query_pkey;
  518. dev->ibdev.query_gid = c4iw_query_gid;
  519. dev->ibdev.alloc_ucontext = c4iw_alloc_ucontext;
  520. dev->ibdev.dealloc_ucontext = c4iw_dealloc_ucontext;
  521. dev->ibdev.mmap = c4iw_mmap;
  522. dev->ibdev.alloc_pd = c4iw_allocate_pd;
  523. dev->ibdev.dealloc_pd = c4iw_deallocate_pd;
  524. dev->ibdev.create_ah = c4iw_ah_create;
  525. dev->ibdev.destroy_ah = c4iw_ah_destroy;
  526. dev->ibdev.create_qp = c4iw_create_qp;
  527. dev->ibdev.modify_qp = c4iw_ib_modify_qp;
  528. dev->ibdev.query_qp = c4iw_ib_query_qp;
  529. dev->ibdev.destroy_qp = c4iw_destroy_qp;
  530. dev->ibdev.create_cq = c4iw_create_cq;
  531. dev->ibdev.destroy_cq = c4iw_destroy_cq;
  532. dev->ibdev.resize_cq = c4iw_resize_cq;
  533. dev->ibdev.poll_cq = c4iw_poll_cq;
  534. dev->ibdev.get_dma_mr = c4iw_get_dma_mr;
  535. dev->ibdev.reg_user_mr = c4iw_reg_user_mr;
  536. dev->ibdev.dereg_mr = c4iw_dereg_mr;
  537. dev->ibdev.alloc_mw = c4iw_alloc_mw;
  538. dev->ibdev.dealloc_mw = c4iw_dealloc_mw;
  539. dev->ibdev.alloc_mr = c4iw_alloc_mr;
  540. dev->ibdev.map_mr_sg = c4iw_map_mr_sg;
  541. dev->ibdev.attach_mcast = c4iw_multicast_attach;
  542. dev->ibdev.detach_mcast = c4iw_multicast_detach;
  543. dev->ibdev.process_mad = c4iw_process_mad;
  544. dev->ibdev.req_notify_cq = c4iw_arm_cq;
  545. dev->ibdev.post_send = c4iw_post_send;
  546. dev->ibdev.post_recv = c4iw_post_receive;
  547. dev->ibdev.alloc_hw_stats = c4iw_alloc_stats;
  548. dev->ibdev.get_hw_stats = c4iw_get_mib;
  549. dev->ibdev.uverbs_abi_ver = C4IW_UVERBS_ABI_VERSION;
  550. dev->ibdev.get_port_immutable = c4iw_port_immutable;
  551. dev->ibdev.get_dev_fw_str = get_dev_fw_str;
  552. dev->ibdev.get_netdev = get_netdev;
  553. dev->ibdev.iwcm = kmalloc(sizeof(struct iw_cm_verbs), GFP_KERNEL);
  554. if (!dev->ibdev.iwcm) {
  555. ret = -ENOMEM;
  556. goto err_dealloc_ctx;
  557. }
  558. dev->ibdev.iwcm->connect = c4iw_connect;
  559. dev->ibdev.iwcm->accept = c4iw_accept_cr;
  560. dev->ibdev.iwcm->reject = c4iw_reject_cr;
  561. dev->ibdev.iwcm->create_listen = c4iw_create_listen;
  562. dev->ibdev.iwcm->destroy_listen = c4iw_destroy_listen;
  563. dev->ibdev.iwcm->add_ref = c4iw_qp_add_ref;
  564. dev->ibdev.iwcm->rem_ref = c4iw_qp_rem_ref;
  565. dev->ibdev.iwcm->get_qp = c4iw_get_qp;
  566. memcpy(dev->ibdev.iwcm->ifname, dev->rdev.lldi.ports[0]->name,
  567. sizeof(dev->ibdev.iwcm->ifname));
  568. dev->ibdev.driver_id = RDMA_DRIVER_CXGB4;
  569. ret = ib_register_device(&dev->ibdev, NULL);
  570. if (ret)
  571. goto err_kfree_iwcm;
  572. for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i) {
  573. ret = device_create_file(&dev->ibdev.dev,
  574. c4iw_class_attributes[i]);
  575. if (ret)
  576. goto err_unregister_device;
  577. }
  578. return;
  579. err_unregister_device:
  580. ib_unregister_device(&dev->ibdev);
  581. err_kfree_iwcm:
  582. kfree(dev->ibdev.iwcm);
  583. err_dealloc_ctx:
  584. pr_err("%s - Failed registering iwarp device: %d\n",
  585. pci_name(ctx->lldi.pdev), ret);
  586. c4iw_dealloc(ctx);
  587. return;
  588. }
  589. void c4iw_unregister_device(struct c4iw_dev *dev)
  590. {
  591. int i;
  592. pr_debug("c4iw_dev %p\n", dev);
  593. for (i = 0; i < ARRAY_SIZE(c4iw_class_attributes); ++i)
  594. device_remove_file(&dev->ibdev.dev,
  595. c4iw_class_attributes[i]);
  596. ib_unregister_device(&dev->ibdev);
  597. kfree(dev->ibdev.iwcm);
  598. return;
  599. }