iwch_provider.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. /*
  2. * Copyright (c) 2006 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/sched.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/ethtool.h>
  43. #include <linux/rtnetlink.h>
  44. #include <linux/inetdevice.h>
  45. #include <linux/slab.h>
  46. #include <asm/io.h>
  47. #include <asm/irq.h>
  48. #include <asm/byteorder.h>
  49. #include <rdma/iw_cm.h>
  50. #include <rdma/ib_verbs.h>
  51. #include <rdma/ib_smi.h>
  52. #include <rdma/ib_umem.h>
  53. #include <rdma/ib_user_verbs.h>
  54. #include "cxio_hal.h"
  55. #include "iwch.h"
  56. #include "iwch_provider.h"
  57. #include "iwch_cm.h"
  58. #include "iwch_user.h"
  59. #include "common.h"
  60. static struct ib_ah *iwch_ah_create(struct ib_pd *pd,
  61. struct ib_ah_attr *ah_attr)
  62. {
  63. return ERR_PTR(-ENOSYS);
  64. }
  65. static int iwch_ah_destroy(struct ib_ah *ah)
  66. {
  67. return -ENOSYS;
  68. }
  69. static int iwch_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  70. {
  71. return -ENOSYS;
  72. }
  73. static int iwch_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  74. {
  75. return -ENOSYS;
  76. }
  77. static int iwch_process_mad(struct ib_device *ibdev,
  78. int mad_flags,
  79. u8 port_num,
  80. struct ib_wc *in_wc,
  81. struct ib_grh *in_grh,
  82. struct ib_mad *in_mad, struct ib_mad *out_mad)
  83. {
  84. return -ENOSYS;
  85. }
  86. static int iwch_dealloc_ucontext(struct ib_ucontext *context)
  87. {
  88. struct iwch_dev *rhp = to_iwch_dev(context->device);
  89. struct iwch_ucontext *ucontext = to_iwch_ucontext(context);
  90. struct iwch_mm_entry *mm, *tmp;
  91. PDBG("%s context %p\n", __func__, context);
  92. list_for_each_entry_safe(mm, tmp, &ucontext->mmaps, entry)
  93. kfree(mm);
  94. cxio_release_ucontext(&rhp->rdev, &ucontext->uctx);
  95. kfree(ucontext);
  96. return 0;
  97. }
  98. static struct ib_ucontext *iwch_alloc_ucontext(struct ib_device *ibdev,
  99. struct ib_udata *udata)
  100. {
  101. struct iwch_ucontext *context;
  102. struct iwch_dev *rhp = to_iwch_dev(ibdev);
  103. PDBG("%s ibdev %p\n", __func__, ibdev);
  104. context = kzalloc(sizeof(*context), GFP_KERNEL);
  105. if (!context)
  106. return ERR_PTR(-ENOMEM);
  107. cxio_init_ucontext(&rhp->rdev, &context->uctx);
  108. INIT_LIST_HEAD(&context->mmaps);
  109. spin_lock_init(&context->mmap_lock);
  110. return &context->ibucontext;
  111. }
  112. static int iwch_destroy_cq(struct ib_cq *ib_cq)
  113. {
  114. struct iwch_cq *chp;
  115. PDBG("%s ib_cq %p\n", __func__, ib_cq);
  116. chp = to_iwch_cq(ib_cq);
  117. remove_handle(chp->rhp, &chp->rhp->cqidr, chp->cq.cqid);
  118. atomic_dec(&chp->refcnt);
  119. wait_event(chp->wait, !atomic_read(&chp->refcnt));
  120. cxio_destroy_cq(&chp->rhp->rdev, &chp->cq);
  121. kfree(chp);
  122. return 0;
  123. }
  124. static struct ib_cq *iwch_create_cq(struct ib_device *ibdev, int entries, int vector,
  125. struct ib_ucontext *ib_context,
  126. struct ib_udata *udata)
  127. {
  128. struct iwch_dev *rhp;
  129. struct iwch_cq *chp;
  130. struct iwch_create_cq_resp uresp;
  131. struct iwch_create_cq_req ureq;
  132. struct iwch_ucontext *ucontext = NULL;
  133. static int warned;
  134. size_t resplen;
  135. PDBG("%s ib_dev %p entries %d\n", __func__, ibdev, entries);
  136. rhp = to_iwch_dev(ibdev);
  137. chp = kzalloc(sizeof(*chp), GFP_KERNEL);
  138. if (!chp)
  139. return ERR_PTR(-ENOMEM);
  140. if (ib_context) {
  141. ucontext = to_iwch_ucontext(ib_context);
  142. if (!t3a_device(rhp)) {
  143. if (ib_copy_from_udata(&ureq, udata, sizeof (ureq))) {
  144. kfree(chp);
  145. return ERR_PTR(-EFAULT);
  146. }
  147. chp->user_rptr_addr = (u32 __user *)(unsigned long)ureq.user_rptr_addr;
  148. }
  149. }
  150. if (t3a_device(rhp)) {
  151. /*
  152. * T3A: Add some fluff to handle extra CQEs inserted
  153. * for various errors.
  154. * Additional CQE possibilities:
  155. * TERMINATE,
  156. * incoming RDMA WRITE Failures
  157. * incoming RDMA READ REQUEST FAILUREs
  158. * NOTE: We cannot ensure the CQ won't overflow.
  159. */
  160. entries += 16;
  161. }
  162. entries = roundup_pow_of_two(entries);
  163. chp->cq.size_log2 = ilog2(entries);
  164. if (cxio_create_cq(&rhp->rdev, &chp->cq, !ucontext)) {
  165. kfree(chp);
  166. return ERR_PTR(-ENOMEM);
  167. }
  168. chp->rhp = rhp;
  169. chp->ibcq.cqe = 1 << chp->cq.size_log2;
  170. spin_lock_init(&chp->lock);
  171. spin_lock_init(&chp->comp_handler_lock);
  172. atomic_set(&chp->refcnt, 1);
  173. init_waitqueue_head(&chp->wait);
  174. if (insert_handle(rhp, &rhp->cqidr, chp, chp->cq.cqid)) {
  175. cxio_destroy_cq(&chp->rhp->rdev, &chp->cq);
  176. kfree(chp);
  177. return ERR_PTR(-ENOMEM);
  178. }
  179. if (ucontext) {
  180. struct iwch_mm_entry *mm;
  181. mm = kmalloc(sizeof *mm, GFP_KERNEL);
  182. if (!mm) {
  183. iwch_destroy_cq(&chp->ibcq);
  184. return ERR_PTR(-ENOMEM);
  185. }
  186. uresp.cqid = chp->cq.cqid;
  187. uresp.size_log2 = chp->cq.size_log2;
  188. spin_lock(&ucontext->mmap_lock);
  189. uresp.key = ucontext->key;
  190. ucontext->key += PAGE_SIZE;
  191. spin_unlock(&ucontext->mmap_lock);
  192. mm->key = uresp.key;
  193. mm->addr = virt_to_phys(chp->cq.queue);
  194. if (udata->outlen < sizeof uresp) {
  195. if (!warned++)
  196. printk(KERN_WARNING MOD "Warning - "
  197. "downlevel libcxgb3 (non-fatal).\n");
  198. mm->len = PAGE_ALIGN((1UL << uresp.size_log2) *
  199. sizeof(struct t3_cqe));
  200. resplen = sizeof(struct iwch_create_cq_resp_v0);
  201. } else {
  202. mm->len = PAGE_ALIGN(((1UL << uresp.size_log2) + 1) *
  203. sizeof(struct t3_cqe));
  204. uresp.memsize = mm->len;
  205. resplen = sizeof uresp;
  206. }
  207. if (ib_copy_to_udata(udata, &uresp, resplen)) {
  208. kfree(mm);
  209. iwch_destroy_cq(&chp->ibcq);
  210. return ERR_PTR(-EFAULT);
  211. }
  212. insert_mmap(ucontext, mm);
  213. }
  214. PDBG("created cqid 0x%0x chp %p size 0x%0x, dma_addr 0x%0llx\n",
  215. chp->cq.cqid, chp, (1 << chp->cq.size_log2),
  216. (unsigned long long) chp->cq.dma_addr);
  217. return &chp->ibcq;
  218. }
  219. static int iwch_resize_cq(struct ib_cq *cq, int cqe, struct ib_udata *udata)
  220. {
  221. #ifdef notyet
  222. struct iwch_cq *chp = to_iwch_cq(cq);
  223. struct t3_cq oldcq, newcq;
  224. int ret;
  225. PDBG("%s ib_cq %p cqe %d\n", __func__, cq, cqe);
  226. /* We don't downsize... */
  227. if (cqe <= cq->cqe)
  228. return 0;
  229. /* create new t3_cq with new size */
  230. cqe = roundup_pow_of_two(cqe+1);
  231. newcq.size_log2 = ilog2(cqe);
  232. /* Dont allow resize to less than the current wce count */
  233. if (cqe < Q_COUNT(chp->cq.rptr, chp->cq.wptr)) {
  234. return -ENOMEM;
  235. }
  236. /* Quiesce all QPs using this CQ */
  237. ret = iwch_quiesce_qps(chp);
  238. if (ret) {
  239. return ret;
  240. }
  241. ret = cxio_create_cq(&chp->rhp->rdev, &newcq);
  242. if (ret) {
  243. return ret;
  244. }
  245. /* copy CQEs */
  246. memcpy(newcq.queue, chp->cq.queue, (1 << chp->cq.size_log2) *
  247. sizeof(struct t3_cqe));
  248. /* old iwch_qp gets new t3_cq but keeps old cqid */
  249. oldcq = chp->cq;
  250. chp->cq = newcq;
  251. chp->cq.cqid = oldcq.cqid;
  252. /* resize new t3_cq to update the HW context */
  253. ret = cxio_resize_cq(&chp->rhp->rdev, &chp->cq);
  254. if (ret) {
  255. chp->cq = oldcq;
  256. return ret;
  257. }
  258. chp->ibcq.cqe = (1<<chp->cq.size_log2) - 1;
  259. /* destroy old t3_cq */
  260. oldcq.cqid = newcq.cqid;
  261. ret = cxio_destroy_cq(&chp->rhp->rdev, &oldcq);
  262. if (ret) {
  263. printk(KERN_ERR MOD "%s - cxio_destroy_cq failed %d\n",
  264. __func__, ret);
  265. }
  266. /* add user hooks here */
  267. /* resume qps */
  268. ret = iwch_resume_qps(chp);
  269. return ret;
  270. #else
  271. return -ENOSYS;
  272. #endif
  273. }
  274. static int iwch_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
  275. {
  276. struct iwch_dev *rhp;
  277. struct iwch_cq *chp;
  278. enum t3_cq_opcode cq_op;
  279. int err;
  280. unsigned long flag;
  281. u32 rptr;
  282. chp = to_iwch_cq(ibcq);
  283. rhp = chp->rhp;
  284. if ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
  285. cq_op = CQ_ARM_SE;
  286. else
  287. cq_op = CQ_ARM_AN;
  288. if (chp->user_rptr_addr) {
  289. if (get_user(rptr, chp->user_rptr_addr))
  290. return -EFAULT;
  291. spin_lock_irqsave(&chp->lock, flag);
  292. chp->cq.rptr = rptr;
  293. } else
  294. spin_lock_irqsave(&chp->lock, flag);
  295. PDBG("%s rptr 0x%x\n", __func__, chp->cq.rptr);
  296. err = cxio_hal_cq_op(&rhp->rdev, &chp->cq, cq_op, 0);
  297. spin_unlock_irqrestore(&chp->lock, flag);
  298. if (err < 0)
  299. printk(KERN_ERR MOD "Error %d rearming CQID 0x%x\n", err,
  300. chp->cq.cqid);
  301. if (err > 0 && !(flags & IB_CQ_REPORT_MISSED_EVENTS))
  302. err = 0;
  303. return err;
  304. }
  305. static int iwch_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  306. {
  307. int len = vma->vm_end - vma->vm_start;
  308. u32 key = vma->vm_pgoff << PAGE_SHIFT;
  309. struct cxio_rdev *rdev_p;
  310. int ret = 0;
  311. struct iwch_mm_entry *mm;
  312. struct iwch_ucontext *ucontext;
  313. u64 addr;
  314. PDBG("%s pgoff 0x%lx key 0x%x len %d\n", __func__, vma->vm_pgoff,
  315. key, len);
  316. if (vma->vm_start & (PAGE_SIZE-1)) {
  317. return -EINVAL;
  318. }
  319. rdev_p = &(to_iwch_dev(context->device)->rdev);
  320. ucontext = to_iwch_ucontext(context);
  321. mm = remove_mmap(ucontext, key, len);
  322. if (!mm)
  323. return -EINVAL;
  324. addr = mm->addr;
  325. kfree(mm);
  326. if ((addr >= rdev_p->rnic_info.udbell_physbase) &&
  327. (addr < (rdev_p->rnic_info.udbell_physbase +
  328. rdev_p->rnic_info.udbell_len))) {
  329. /*
  330. * Map T3 DB register.
  331. */
  332. if (vma->vm_flags & VM_READ) {
  333. return -EPERM;
  334. }
  335. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  336. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
  337. vma->vm_flags &= ~VM_MAYREAD;
  338. ret = io_remap_pfn_range(vma, vma->vm_start,
  339. addr >> PAGE_SHIFT,
  340. len, vma->vm_page_prot);
  341. } else {
  342. /*
  343. * Map WQ or CQ contig dma memory...
  344. */
  345. ret = remap_pfn_range(vma, vma->vm_start,
  346. addr >> PAGE_SHIFT,
  347. len, vma->vm_page_prot);
  348. }
  349. return ret;
  350. }
  351. static int iwch_deallocate_pd(struct ib_pd *pd)
  352. {
  353. struct iwch_dev *rhp;
  354. struct iwch_pd *php;
  355. php = to_iwch_pd(pd);
  356. rhp = php->rhp;
  357. PDBG("%s ibpd %p pdid 0x%x\n", __func__, pd, php->pdid);
  358. cxio_hal_put_pdid(rhp->rdev.rscp, php->pdid);
  359. kfree(php);
  360. return 0;
  361. }
  362. static struct ib_pd *iwch_allocate_pd(struct ib_device *ibdev,
  363. struct ib_ucontext *context,
  364. struct ib_udata *udata)
  365. {
  366. struct iwch_pd *php;
  367. u32 pdid;
  368. struct iwch_dev *rhp;
  369. PDBG("%s ibdev %p\n", __func__, ibdev);
  370. rhp = (struct iwch_dev *) ibdev;
  371. pdid = cxio_hal_get_pdid(rhp->rdev.rscp);
  372. if (!pdid)
  373. return ERR_PTR(-EINVAL);
  374. php = kzalloc(sizeof(*php), GFP_KERNEL);
  375. if (!php) {
  376. cxio_hal_put_pdid(rhp->rdev.rscp, pdid);
  377. return ERR_PTR(-ENOMEM);
  378. }
  379. php->pdid = pdid;
  380. php->rhp = rhp;
  381. if (context) {
  382. if (ib_copy_to_udata(udata, &php->pdid, sizeof (__u32))) {
  383. iwch_deallocate_pd(&php->ibpd);
  384. return ERR_PTR(-EFAULT);
  385. }
  386. }
  387. PDBG("%s pdid 0x%0x ptr 0x%p\n", __func__, pdid, php);
  388. return &php->ibpd;
  389. }
  390. static int iwch_dereg_mr(struct ib_mr *ib_mr)
  391. {
  392. struct iwch_dev *rhp;
  393. struct iwch_mr *mhp;
  394. u32 mmid;
  395. PDBG("%s ib_mr %p\n", __func__, ib_mr);
  396. /* There can be no memory windows */
  397. if (atomic_read(&ib_mr->usecnt))
  398. return -EINVAL;
  399. mhp = to_iwch_mr(ib_mr);
  400. rhp = mhp->rhp;
  401. mmid = mhp->attr.stag >> 8;
  402. cxio_dereg_mem(&rhp->rdev, mhp->attr.stag, mhp->attr.pbl_size,
  403. mhp->attr.pbl_addr);
  404. iwch_free_pbl(mhp);
  405. remove_handle(rhp, &rhp->mmidr, mmid);
  406. if (mhp->kva)
  407. kfree((void *) (unsigned long) mhp->kva);
  408. if (mhp->umem)
  409. ib_umem_release(mhp->umem);
  410. PDBG("%s mmid 0x%x ptr %p\n", __func__, mmid, mhp);
  411. kfree(mhp);
  412. return 0;
  413. }
  414. static struct ib_mr *iwch_register_phys_mem(struct ib_pd *pd,
  415. struct ib_phys_buf *buffer_list,
  416. int num_phys_buf,
  417. int acc,
  418. u64 *iova_start)
  419. {
  420. __be64 *page_list;
  421. int shift;
  422. u64 total_size;
  423. int npages;
  424. struct iwch_dev *rhp;
  425. struct iwch_pd *php;
  426. struct iwch_mr *mhp;
  427. int ret;
  428. PDBG("%s ib_pd %p\n", __func__, pd);
  429. php = to_iwch_pd(pd);
  430. rhp = php->rhp;
  431. mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
  432. if (!mhp)
  433. return ERR_PTR(-ENOMEM);
  434. mhp->rhp = rhp;
  435. /* First check that we have enough alignment */
  436. if ((*iova_start & ~PAGE_MASK) != (buffer_list[0].addr & ~PAGE_MASK)) {
  437. ret = -EINVAL;
  438. goto err;
  439. }
  440. if (num_phys_buf > 1 &&
  441. ((buffer_list[0].addr + buffer_list[0].size) & ~PAGE_MASK)) {
  442. ret = -EINVAL;
  443. goto err;
  444. }
  445. ret = build_phys_page_list(buffer_list, num_phys_buf, iova_start,
  446. &total_size, &npages, &shift, &page_list);
  447. if (ret)
  448. goto err;
  449. ret = iwch_alloc_pbl(mhp, npages);
  450. if (ret) {
  451. kfree(page_list);
  452. goto err_pbl;
  453. }
  454. ret = iwch_write_pbl(mhp, page_list, npages, 0);
  455. kfree(page_list);
  456. if (ret)
  457. goto err_pbl;
  458. mhp->attr.pdid = php->pdid;
  459. mhp->attr.zbva = 0;
  460. mhp->attr.perms = iwch_ib_to_tpt_access(acc);
  461. mhp->attr.va_fbo = *iova_start;
  462. mhp->attr.page_size = shift - 12;
  463. mhp->attr.len = (u32) total_size;
  464. mhp->attr.pbl_size = npages;
  465. ret = iwch_register_mem(rhp, php, mhp, shift);
  466. if (ret)
  467. goto err_pbl;
  468. return &mhp->ibmr;
  469. err_pbl:
  470. iwch_free_pbl(mhp);
  471. err:
  472. kfree(mhp);
  473. return ERR_PTR(ret);
  474. }
  475. static int iwch_reregister_phys_mem(struct ib_mr *mr,
  476. int mr_rereg_mask,
  477. struct ib_pd *pd,
  478. struct ib_phys_buf *buffer_list,
  479. int num_phys_buf,
  480. int acc, u64 * iova_start)
  481. {
  482. struct iwch_mr mh, *mhp;
  483. struct iwch_pd *php;
  484. struct iwch_dev *rhp;
  485. __be64 *page_list = NULL;
  486. int shift = 0;
  487. u64 total_size;
  488. int npages;
  489. int ret;
  490. PDBG("%s ib_mr %p ib_pd %p\n", __func__, mr, pd);
  491. /* There can be no memory windows */
  492. if (atomic_read(&mr->usecnt))
  493. return -EINVAL;
  494. mhp = to_iwch_mr(mr);
  495. rhp = mhp->rhp;
  496. php = to_iwch_pd(mr->pd);
  497. /* make sure we are on the same adapter */
  498. if (rhp != php->rhp)
  499. return -EINVAL;
  500. memcpy(&mh, mhp, sizeof *mhp);
  501. if (mr_rereg_mask & IB_MR_REREG_PD)
  502. php = to_iwch_pd(pd);
  503. if (mr_rereg_mask & IB_MR_REREG_ACCESS)
  504. mh.attr.perms = iwch_ib_to_tpt_access(acc);
  505. if (mr_rereg_mask & IB_MR_REREG_TRANS) {
  506. ret = build_phys_page_list(buffer_list, num_phys_buf,
  507. iova_start,
  508. &total_size, &npages,
  509. &shift, &page_list);
  510. if (ret)
  511. return ret;
  512. }
  513. ret = iwch_reregister_mem(rhp, php, &mh, shift, npages);
  514. kfree(page_list);
  515. if (ret) {
  516. return ret;
  517. }
  518. if (mr_rereg_mask & IB_MR_REREG_PD)
  519. mhp->attr.pdid = php->pdid;
  520. if (mr_rereg_mask & IB_MR_REREG_ACCESS)
  521. mhp->attr.perms = iwch_ib_to_tpt_access(acc);
  522. if (mr_rereg_mask & IB_MR_REREG_TRANS) {
  523. mhp->attr.zbva = 0;
  524. mhp->attr.va_fbo = *iova_start;
  525. mhp->attr.page_size = shift - 12;
  526. mhp->attr.len = (u32) total_size;
  527. mhp->attr.pbl_size = npages;
  528. }
  529. return 0;
  530. }
  531. static struct ib_mr *iwch_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  532. u64 virt, int acc, struct ib_udata *udata)
  533. {
  534. __be64 *pages;
  535. int shift, n, len;
  536. int i, j, k;
  537. int err = 0;
  538. struct ib_umem_chunk *chunk;
  539. struct iwch_dev *rhp;
  540. struct iwch_pd *php;
  541. struct iwch_mr *mhp;
  542. struct iwch_reg_user_mr_resp uresp;
  543. PDBG("%s ib_pd %p\n", __func__, pd);
  544. php = to_iwch_pd(pd);
  545. rhp = php->rhp;
  546. mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
  547. if (!mhp)
  548. return ERR_PTR(-ENOMEM);
  549. mhp->rhp = rhp;
  550. mhp->umem = ib_umem_get(pd->uobject->context, start, length, acc, 0);
  551. if (IS_ERR(mhp->umem)) {
  552. err = PTR_ERR(mhp->umem);
  553. kfree(mhp);
  554. return ERR_PTR(err);
  555. }
  556. shift = ffs(mhp->umem->page_size) - 1;
  557. n = 0;
  558. list_for_each_entry(chunk, &mhp->umem->chunk_list, list)
  559. n += chunk->nents;
  560. err = iwch_alloc_pbl(mhp, n);
  561. if (err)
  562. goto err;
  563. pages = (__be64 *) __get_free_page(GFP_KERNEL);
  564. if (!pages) {
  565. err = -ENOMEM;
  566. goto err_pbl;
  567. }
  568. i = n = 0;
  569. list_for_each_entry(chunk, &mhp->umem->chunk_list, list)
  570. for (j = 0; j < chunk->nmap; ++j) {
  571. len = sg_dma_len(&chunk->page_list[j]) >> shift;
  572. for (k = 0; k < len; ++k) {
  573. pages[i++] = cpu_to_be64(sg_dma_address(
  574. &chunk->page_list[j]) +
  575. mhp->umem->page_size * k);
  576. if (i == PAGE_SIZE / sizeof *pages) {
  577. err = iwch_write_pbl(mhp, pages, i, n);
  578. if (err)
  579. goto pbl_done;
  580. n += i;
  581. i = 0;
  582. }
  583. }
  584. }
  585. if (i)
  586. err = iwch_write_pbl(mhp, pages, i, n);
  587. pbl_done:
  588. free_page((unsigned long) pages);
  589. if (err)
  590. goto err_pbl;
  591. mhp->attr.pdid = php->pdid;
  592. mhp->attr.zbva = 0;
  593. mhp->attr.perms = iwch_ib_to_tpt_access(acc);
  594. mhp->attr.va_fbo = virt;
  595. mhp->attr.page_size = shift - 12;
  596. mhp->attr.len = (u32) length;
  597. err = iwch_register_mem(rhp, php, mhp, shift);
  598. if (err)
  599. goto err_pbl;
  600. if (udata && !t3a_device(rhp)) {
  601. uresp.pbl_addr = (mhp->attr.pbl_addr -
  602. rhp->rdev.rnic_info.pbl_base) >> 3;
  603. PDBG("%s user resp pbl_addr 0x%x\n", __func__,
  604. uresp.pbl_addr);
  605. if (ib_copy_to_udata(udata, &uresp, sizeof (uresp))) {
  606. iwch_dereg_mr(&mhp->ibmr);
  607. err = -EFAULT;
  608. goto err;
  609. }
  610. }
  611. return &mhp->ibmr;
  612. err_pbl:
  613. iwch_free_pbl(mhp);
  614. err:
  615. ib_umem_release(mhp->umem);
  616. kfree(mhp);
  617. return ERR_PTR(err);
  618. }
  619. static struct ib_mr *iwch_get_dma_mr(struct ib_pd *pd, int acc)
  620. {
  621. struct ib_phys_buf bl;
  622. u64 kva;
  623. struct ib_mr *ibmr;
  624. PDBG("%s ib_pd %p\n", __func__, pd);
  625. /*
  626. * T3 only supports 32 bits of size.
  627. */
  628. bl.size = 0xffffffff;
  629. bl.addr = 0;
  630. kva = 0;
  631. ibmr = iwch_register_phys_mem(pd, &bl, 1, acc, &kva);
  632. return ibmr;
  633. }
  634. static struct ib_mw *iwch_alloc_mw(struct ib_pd *pd)
  635. {
  636. struct iwch_dev *rhp;
  637. struct iwch_pd *php;
  638. struct iwch_mw *mhp;
  639. u32 mmid;
  640. u32 stag = 0;
  641. int ret;
  642. php = to_iwch_pd(pd);
  643. rhp = php->rhp;
  644. mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
  645. if (!mhp)
  646. return ERR_PTR(-ENOMEM);
  647. ret = cxio_allocate_window(&rhp->rdev, &stag, php->pdid);
  648. if (ret) {
  649. kfree(mhp);
  650. return ERR_PTR(ret);
  651. }
  652. mhp->rhp = rhp;
  653. mhp->attr.pdid = php->pdid;
  654. mhp->attr.type = TPT_MW;
  655. mhp->attr.stag = stag;
  656. mmid = (stag) >> 8;
  657. mhp->ibmw.rkey = stag;
  658. if (insert_handle(rhp, &rhp->mmidr, mhp, mmid)) {
  659. cxio_deallocate_window(&rhp->rdev, mhp->attr.stag);
  660. kfree(mhp);
  661. return ERR_PTR(-ENOMEM);
  662. }
  663. PDBG("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
  664. return &(mhp->ibmw);
  665. }
  666. static int iwch_dealloc_mw(struct ib_mw *mw)
  667. {
  668. struct iwch_dev *rhp;
  669. struct iwch_mw *mhp;
  670. u32 mmid;
  671. mhp = to_iwch_mw(mw);
  672. rhp = mhp->rhp;
  673. mmid = (mw->rkey) >> 8;
  674. cxio_deallocate_window(&rhp->rdev, mhp->attr.stag);
  675. remove_handle(rhp, &rhp->mmidr, mmid);
  676. kfree(mhp);
  677. PDBG("%s ib_mw %p mmid 0x%x ptr %p\n", __func__, mw, mmid, mhp);
  678. return 0;
  679. }
  680. static struct ib_mr *iwch_alloc_fast_reg_mr(struct ib_pd *pd, int pbl_depth)
  681. {
  682. struct iwch_dev *rhp;
  683. struct iwch_pd *php;
  684. struct iwch_mr *mhp;
  685. u32 mmid;
  686. u32 stag = 0;
  687. int ret = 0;
  688. php = to_iwch_pd(pd);
  689. rhp = php->rhp;
  690. mhp = kzalloc(sizeof(*mhp), GFP_KERNEL);
  691. if (!mhp)
  692. goto err;
  693. mhp->rhp = rhp;
  694. ret = iwch_alloc_pbl(mhp, pbl_depth);
  695. if (ret)
  696. goto err1;
  697. mhp->attr.pbl_size = pbl_depth;
  698. ret = cxio_allocate_stag(&rhp->rdev, &stag, php->pdid,
  699. mhp->attr.pbl_size, mhp->attr.pbl_addr);
  700. if (ret)
  701. goto err2;
  702. mhp->attr.pdid = php->pdid;
  703. mhp->attr.type = TPT_NON_SHARED_MR;
  704. mhp->attr.stag = stag;
  705. mhp->attr.state = 1;
  706. mmid = (stag) >> 8;
  707. mhp->ibmr.rkey = mhp->ibmr.lkey = stag;
  708. if (insert_handle(rhp, &rhp->mmidr, mhp, mmid))
  709. goto err3;
  710. PDBG("%s mmid 0x%x mhp %p stag 0x%x\n", __func__, mmid, mhp, stag);
  711. return &(mhp->ibmr);
  712. err3:
  713. cxio_dereg_mem(&rhp->rdev, stag, mhp->attr.pbl_size,
  714. mhp->attr.pbl_addr);
  715. err2:
  716. iwch_free_pbl(mhp);
  717. err1:
  718. kfree(mhp);
  719. err:
  720. return ERR_PTR(ret);
  721. }
  722. static struct ib_fast_reg_page_list *iwch_alloc_fastreg_pbl(
  723. struct ib_device *device,
  724. int page_list_len)
  725. {
  726. struct ib_fast_reg_page_list *page_list;
  727. page_list = kmalloc(sizeof *page_list + page_list_len * sizeof(u64),
  728. GFP_KERNEL);
  729. if (!page_list)
  730. return ERR_PTR(-ENOMEM);
  731. page_list->page_list = (u64 *)(page_list + 1);
  732. page_list->max_page_list_len = page_list_len;
  733. return page_list;
  734. }
  735. static void iwch_free_fastreg_pbl(struct ib_fast_reg_page_list *page_list)
  736. {
  737. kfree(page_list);
  738. }
  739. static int iwch_destroy_qp(struct ib_qp *ib_qp)
  740. {
  741. struct iwch_dev *rhp;
  742. struct iwch_qp *qhp;
  743. struct iwch_qp_attributes attrs;
  744. struct iwch_ucontext *ucontext;
  745. qhp = to_iwch_qp(ib_qp);
  746. rhp = qhp->rhp;
  747. attrs.next_state = IWCH_QP_STATE_ERROR;
  748. iwch_modify_qp(rhp, qhp, IWCH_QP_ATTR_NEXT_STATE, &attrs, 0);
  749. wait_event(qhp->wait, !qhp->ep);
  750. remove_handle(rhp, &rhp->qpidr, qhp->wq.qpid);
  751. atomic_dec(&qhp->refcnt);
  752. wait_event(qhp->wait, !atomic_read(&qhp->refcnt));
  753. ucontext = ib_qp->uobject ? to_iwch_ucontext(ib_qp->uobject->context)
  754. : NULL;
  755. cxio_destroy_qp(&rhp->rdev, &qhp->wq,
  756. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  757. PDBG("%s ib_qp %p qpid 0x%0x qhp %p\n", __func__,
  758. ib_qp, qhp->wq.qpid, qhp);
  759. kfree(qhp);
  760. return 0;
  761. }
  762. static struct ib_qp *iwch_create_qp(struct ib_pd *pd,
  763. struct ib_qp_init_attr *attrs,
  764. struct ib_udata *udata)
  765. {
  766. struct iwch_dev *rhp;
  767. struct iwch_qp *qhp;
  768. struct iwch_pd *php;
  769. struct iwch_cq *schp;
  770. struct iwch_cq *rchp;
  771. struct iwch_create_qp_resp uresp;
  772. int wqsize, sqsize, rqsize;
  773. struct iwch_ucontext *ucontext;
  774. PDBG("%s ib_pd %p\n", __func__, pd);
  775. if (attrs->qp_type != IB_QPT_RC)
  776. return ERR_PTR(-EINVAL);
  777. php = to_iwch_pd(pd);
  778. rhp = php->rhp;
  779. schp = get_chp(rhp, ((struct iwch_cq *) attrs->send_cq)->cq.cqid);
  780. rchp = get_chp(rhp, ((struct iwch_cq *) attrs->recv_cq)->cq.cqid);
  781. if (!schp || !rchp)
  782. return ERR_PTR(-EINVAL);
  783. /* The RQT size must be # of entries + 1 rounded up to a power of two */
  784. rqsize = roundup_pow_of_two(attrs->cap.max_recv_wr);
  785. if (rqsize == attrs->cap.max_recv_wr)
  786. rqsize = roundup_pow_of_two(attrs->cap.max_recv_wr+1);
  787. /* T3 doesn't support RQT depth < 16 */
  788. if (rqsize < 16)
  789. rqsize = 16;
  790. if (rqsize > T3_MAX_RQ_SIZE)
  791. return ERR_PTR(-EINVAL);
  792. if (attrs->cap.max_inline_data > T3_MAX_INLINE)
  793. return ERR_PTR(-EINVAL);
  794. /*
  795. * NOTE: The SQ and total WQ sizes don't need to be
  796. * a power of two. However, all the code assumes
  797. * they are. EG: Q_FREECNT() and friends.
  798. */
  799. sqsize = roundup_pow_of_two(attrs->cap.max_send_wr);
  800. wqsize = roundup_pow_of_two(rqsize + sqsize);
  801. /*
  802. * Kernel users need more wq space for fastreg WRs which can take
  803. * 2 WR fragments.
  804. */
  805. ucontext = pd->uobject ? to_iwch_ucontext(pd->uobject->context) : NULL;
  806. if (!ucontext && wqsize < (rqsize + (2 * sqsize)))
  807. wqsize = roundup_pow_of_two(rqsize +
  808. roundup_pow_of_two(attrs->cap.max_send_wr * 2));
  809. PDBG("%s wqsize %d sqsize %d rqsize %d\n", __func__,
  810. wqsize, sqsize, rqsize);
  811. qhp = kzalloc(sizeof(*qhp), GFP_KERNEL);
  812. if (!qhp)
  813. return ERR_PTR(-ENOMEM);
  814. qhp->wq.size_log2 = ilog2(wqsize);
  815. qhp->wq.rq_size_log2 = ilog2(rqsize);
  816. qhp->wq.sq_size_log2 = ilog2(sqsize);
  817. if (cxio_create_qp(&rhp->rdev, !udata, &qhp->wq,
  818. ucontext ? &ucontext->uctx : &rhp->rdev.uctx)) {
  819. kfree(qhp);
  820. return ERR_PTR(-ENOMEM);
  821. }
  822. attrs->cap.max_recv_wr = rqsize - 1;
  823. attrs->cap.max_send_wr = sqsize;
  824. attrs->cap.max_inline_data = T3_MAX_INLINE;
  825. qhp->rhp = rhp;
  826. qhp->attr.pd = php->pdid;
  827. qhp->attr.scq = ((struct iwch_cq *) attrs->send_cq)->cq.cqid;
  828. qhp->attr.rcq = ((struct iwch_cq *) attrs->recv_cq)->cq.cqid;
  829. qhp->attr.sq_num_entries = attrs->cap.max_send_wr;
  830. qhp->attr.rq_num_entries = attrs->cap.max_recv_wr;
  831. qhp->attr.sq_max_sges = attrs->cap.max_send_sge;
  832. qhp->attr.sq_max_sges_rdma_write = attrs->cap.max_send_sge;
  833. qhp->attr.rq_max_sges = attrs->cap.max_recv_sge;
  834. qhp->attr.state = IWCH_QP_STATE_IDLE;
  835. qhp->attr.next_state = IWCH_QP_STATE_IDLE;
  836. /*
  837. * XXX - These don't get passed in from the openib user
  838. * at create time. The CM sets them via a QP modify.
  839. * Need to fix... I think the CM should
  840. */
  841. qhp->attr.enable_rdma_read = 1;
  842. qhp->attr.enable_rdma_write = 1;
  843. qhp->attr.enable_bind = 1;
  844. qhp->attr.max_ord = 1;
  845. qhp->attr.max_ird = 1;
  846. spin_lock_init(&qhp->lock);
  847. init_waitqueue_head(&qhp->wait);
  848. atomic_set(&qhp->refcnt, 1);
  849. if (insert_handle(rhp, &rhp->qpidr, qhp, qhp->wq.qpid)) {
  850. cxio_destroy_qp(&rhp->rdev, &qhp->wq,
  851. ucontext ? &ucontext->uctx : &rhp->rdev.uctx);
  852. kfree(qhp);
  853. return ERR_PTR(-ENOMEM);
  854. }
  855. if (udata) {
  856. struct iwch_mm_entry *mm1, *mm2;
  857. mm1 = kmalloc(sizeof *mm1, GFP_KERNEL);
  858. if (!mm1) {
  859. iwch_destroy_qp(&qhp->ibqp);
  860. return ERR_PTR(-ENOMEM);
  861. }
  862. mm2 = kmalloc(sizeof *mm2, GFP_KERNEL);
  863. if (!mm2) {
  864. kfree(mm1);
  865. iwch_destroy_qp(&qhp->ibqp);
  866. return ERR_PTR(-ENOMEM);
  867. }
  868. uresp.qpid = qhp->wq.qpid;
  869. uresp.size_log2 = qhp->wq.size_log2;
  870. uresp.sq_size_log2 = qhp->wq.sq_size_log2;
  871. uresp.rq_size_log2 = qhp->wq.rq_size_log2;
  872. spin_lock(&ucontext->mmap_lock);
  873. uresp.key = ucontext->key;
  874. ucontext->key += PAGE_SIZE;
  875. uresp.db_key = ucontext->key;
  876. ucontext->key += PAGE_SIZE;
  877. spin_unlock(&ucontext->mmap_lock);
  878. if (ib_copy_to_udata(udata, &uresp, sizeof (uresp))) {
  879. kfree(mm1);
  880. kfree(mm2);
  881. iwch_destroy_qp(&qhp->ibqp);
  882. return ERR_PTR(-EFAULT);
  883. }
  884. mm1->key = uresp.key;
  885. mm1->addr = virt_to_phys(qhp->wq.queue);
  886. mm1->len = PAGE_ALIGN(wqsize * sizeof (union t3_wr));
  887. insert_mmap(ucontext, mm1);
  888. mm2->key = uresp.db_key;
  889. mm2->addr = qhp->wq.udb & PAGE_MASK;
  890. mm2->len = PAGE_SIZE;
  891. insert_mmap(ucontext, mm2);
  892. }
  893. qhp->ibqp.qp_num = qhp->wq.qpid;
  894. init_timer(&(qhp->timer));
  895. PDBG("%s sq_num_entries %d, rq_num_entries %d "
  896. "qpid 0x%0x qhp %p dma_addr 0x%llx size %d rq_addr 0x%x\n",
  897. __func__, qhp->attr.sq_num_entries, qhp->attr.rq_num_entries,
  898. qhp->wq.qpid, qhp, (unsigned long long) qhp->wq.dma_addr,
  899. 1 << qhp->wq.size_log2, qhp->wq.rq_addr);
  900. return &qhp->ibqp;
  901. }
  902. static int iwch_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  903. int attr_mask, struct ib_udata *udata)
  904. {
  905. struct iwch_dev *rhp;
  906. struct iwch_qp *qhp;
  907. enum iwch_qp_attr_mask mask = 0;
  908. struct iwch_qp_attributes attrs;
  909. PDBG("%s ib_qp %p\n", __func__, ibqp);
  910. /* iwarp does not support the RTR state */
  911. if ((attr_mask & IB_QP_STATE) && (attr->qp_state == IB_QPS_RTR))
  912. attr_mask &= ~IB_QP_STATE;
  913. /* Make sure we still have something left to do */
  914. if (!attr_mask)
  915. return 0;
  916. memset(&attrs, 0, sizeof attrs);
  917. qhp = to_iwch_qp(ibqp);
  918. rhp = qhp->rhp;
  919. attrs.next_state = iwch_convert_state(attr->qp_state);
  920. attrs.enable_rdma_read = (attr->qp_access_flags &
  921. IB_ACCESS_REMOTE_READ) ? 1 : 0;
  922. attrs.enable_rdma_write = (attr->qp_access_flags &
  923. IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  924. attrs.enable_bind = (attr->qp_access_flags & IB_ACCESS_MW_BIND) ? 1 : 0;
  925. mask |= (attr_mask & IB_QP_STATE) ? IWCH_QP_ATTR_NEXT_STATE : 0;
  926. mask |= (attr_mask & IB_QP_ACCESS_FLAGS) ?
  927. (IWCH_QP_ATTR_ENABLE_RDMA_READ |
  928. IWCH_QP_ATTR_ENABLE_RDMA_WRITE |
  929. IWCH_QP_ATTR_ENABLE_RDMA_BIND) : 0;
  930. return iwch_modify_qp(rhp, qhp, mask, &attrs, 0);
  931. }
  932. void iwch_qp_add_ref(struct ib_qp *qp)
  933. {
  934. PDBG("%s ib_qp %p\n", __func__, qp);
  935. atomic_inc(&(to_iwch_qp(qp)->refcnt));
  936. }
  937. void iwch_qp_rem_ref(struct ib_qp *qp)
  938. {
  939. PDBG("%s ib_qp %p\n", __func__, qp);
  940. if (atomic_dec_and_test(&(to_iwch_qp(qp)->refcnt)))
  941. wake_up(&(to_iwch_qp(qp)->wait));
  942. }
  943. static struct ib_qp *iwch_get_qp(struct ib_device *dev, int qpn)
  944. {
  945. PDBG("%s ib_dev %p qpn 0x%x\n", __func__, dev, qpn);
  946. return (struct ib_qp *)get_qhp(to_iwch_dev(dev), qpn);
  947. }
  948. static int iwch_query_pkey(struct ib_device *ibdev,
  949. u8 port, u16 index, u16 * pkey)
  950. {
  951. PDBG("%s ibdev %p\n", __func__, ibdev);
  952. *pkey = 0;
  953. return 0;
  954. }
  955. static int iwch_query_gid(struct ib_device *ibdev, u8 port,
  956. int index, union ib_gid *gid)
  957. {
  958. struct iwch_dev *dev;
  959. PDBG("%s ibdev %p, port %d, index %d, gid %p\n",
  960. __func__, ibdev, port, index, gid);
  961. dev = to_iwch_dev(ibdev);
  962. BUG_ON(port == 0 || port > 2);
  963. memset(&(gid->raw[0]), 0, sizeof(gid->raw));
  964. memcpy(&(gid->raw[0]), dev->rdev.port_info.lldevs[port-1]->dev_addr, 6);
  965. return 0;
  966. }
  967. static u64 fw_vers_string_to_u64(struct iwch_dev *iwch_dev)
  968. {
  969. struct ethtool_drvinfo info;
  970. struct net_device *lldev = iwch_dev->rdev.t3cdev_p->lldev;
  971. char *cp, *next;
  972. unsigned fw_maj, fw_min, fw_mic;
  973. lldev->ethtool_ops->get_drvinfo(lldev, &info);
  974. next = info.fw_version + 1;
  975. cp = strsep(&next, ".");
  976. sscanf(cp, "%i", &fw_maj);
  977. cp = strsep(&next, ".");
  978. sscanf(cp, "%i", &fw_min);
  979. cp = strsep(&next, ".");
  980. sscanf(cp, "%i", &fw_mic);
  981. return (((u64)fw_maj & 0xffff) << 32) | ((fw_min & 0xffff) << 16) |
  982. (fw_mic & 0xffff);
  983. }
  984. static int iwch_query_device(struct ib_device *ibdev,
  985. struct ib_device_attr *props)
  986. {
  987. struct iwch_dev *dev;
  988. PDBG("%s ibdev %p\n", __func__, ibdev);
  989. dev = to_iwch_dev(ibdev);
  990. memset(props, 0, sizeof *props);
  991. memcpy(&props->sys_image_guid, dev->rdev.t3cdev_p->lldev->dev_addr, 6);
  992. props->hw_ver = dev->rdev.t3cdev_p->type;
  993. props->fw_ver = fw_vers_string_to_u64(dev);
  994. props->device_cap_flags = dev->device_cap_flags;
  995. props->page_size_cap = dev->attr.mem_pgsizes_bitmask;
  996. props->vendor_id = (u32)dev->rdev.rnic_info.pdev->vendor;
  997. props->vendor_part_id = (u32)dev->rdev.rnic_info.pdev->device;
  998. props->max_mr_size = dev->attr.max_mr_size;
  999. props->max_qp = dev->attr.max_qps;
  1000. props->max_qp_wr = dev->attr.max_wrs;
  1001. props->max_sge = dev->attr.max_sge_per_wr;
  1002. props->max_sge_rd = 1;
  1003. props->max_qp_rd_atom = dev->attr.max_rdma_reads_per_qp;
  1004. props->max_qp_init_rd_atom = dev->attr.max_rdma_reads_per_qp;
  1005. props->max_cq = dev->attr.max_cqs;
  1006. props->max_cqe = dev->attr.max_cqes_per_cq;
  1007. props->max_mr = dev->attr.max_mem_regs;
  1008. props->max_pd = dev->attr.max_pds;
  1009. props->local_ca_ack_delay = 0;
  1010. props->max_fast_reg_page_list_len = T3_MAX_FASTREG_DEPTH;
  1011. return 0;
  1012. }
  1013. static int iwch_query_port(struct ib_device *ibdev,
  1014. u8 port, struct ib_port_attr *props)
  1015. {
  1016. struct iwch_dev *dev;
  1017. struct net_device *netdev;
  1018. struct in_device *inetdev;
  1019. PDBG("%s ibdev %p\n", __func__, ibdev);
  1020. dev = to_iwch_dev(ibdev);
  1021. netdev = dev->rdev.port_info.lldevs[port-1];
  1022. memset(props, 0, sizeof(struct ib_port_attr));
  1023. props->max_mtu = IB_MTU_4096;
  1024. if (netdev->mtu >= 4096)
  1025. props->active_mtu = IB_MTU_4096;
  1026. else if (netdev->mtu >= 2048)
  1027. props->active_mtu = IB_MTU_2048;
  1028. else if (netdev->mtu >= 1024)
  1029. props->active_mtu = IB_MTU_1024;
  1030. else if (netdev->mtu >= 512)
  1031. props->active_mtu = IB_MTU_512;
  1032. else
  1033. props->active_mtu = IB_MTU_256;
  1034. if (!netif_carrier_ok(netdev))
  1035. props->state = IB_PORT_DOWN;
  1036. else {
  1037. inetdev = in_dev_get(netdev);
  1038. if (inetdev) {
  1039. if (inetdev->ifa_list)
  1040. props->state = IB_PORT_ACTIVE;
  1041. else
  1042. props->state = IB_PORT_INIT;
  1043. in_dev_put(inetdev);
  1044. } else
  1045. props->state = IB_PORT_INIT;
  1046. }
  1047. props->port_cap_flags =
  1048. IB_PORT_CM_SUP |
  1049. IB_PORT_SNMP_TUNNEL_SUP |
  1050. IB_PORT_REINIT_SUP |
  1051. IB_PORT_DEVICE_MGMT_SUP |
  1052. IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
  1053. props->gid_tbl_len = 1;
  1054. props->pkey_tbl_len = 1;
  1055. props->active_width = 2;
  1056. props->active_speed = 2;
  1057. props->max_msg_sz = -1;
  1058. return 0;
  1059. }
  1060. static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
  1061. char *buf)
  1062. {
  1063. struct iwch_dev *iwch_dev = container_of(dev, struct iwch_dev,
  1064. ibdev.dev);
  1065. PDBG("%s dev 0x%p\n", __func__, dev);
  1066. return sprintf(buf, "%d\n", iwch_dev->rdev.t3cdev_p->type);
  1067. }
  1068. static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr, char *buf)
  1069. {
  1070. struct iwch_dev *iwch_dev = container_of(dev, struct iwch_dev,
  1071. ibdev.dev);
  1072. struct ethtool_drvinfo info;
  1073. struct net_device *lldev = iwch_dev->rdev.t3cdev_p->lldev;
  1074. PDBG("%s dev 0x%p\n", __func__, dev);
  1075. lldev->ethtool_ops->get_drvinfo(lldev, &info);
  1076. return sprintf(buf, "%s\n", info.fw_version);
  1077. }
  1078. static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
  1079. char *buf)
  1080. {
  1081. struct iwch_dev *iwch_dev = container_of(dev, struct iwch_dev,
  1082. ibdev.dev);
  1083. struct ethtool_drvinfo info;
  1084. struct net_device *lldev = iwch_dev->rdev.t3cdev_p->lldev;
  1085. PDBG("%s dev 0x%p\n", __func__, dev);
  1086. lldev->ethtool_ops->get_drvinfo(lldev, &info);
  1087. return sprintf(buf, "%s\n", info.driver);
  1088. }
  1089. static ssize_t show_board(struct device *dev, struct device_attribute *attr,
  1090. char *buf)
  1091. {
  1092. struct iwch_dev *iwch_dev = container_of(dev, struct iwch_dev,
  1093. ibdev.dev);
  1094. PDBG("%s dev 0x%p\n", __func__, dev);
  1095. return sprintf(buf, "%x.%x\n", iwch_dev->rdev.rnic_info.pdev->vendor,
  1096. iwch_dev->rdev.rnic_info.pdev->device);
  1097. }
  1098. static int iwch_get_mib(struct ib_device *ibdev,
  1099. union rdma_protocol_stats *stats)
  1100. {
  1101. struct iwch_dev *dev;
  1102. struct tp_mib_stats m;
  1103. int ret;
  1104. PDBG("%s ibdev %p\n", __func__, ibdev);
  1105. dev = to_iwch_dev(ibdev);
  1106. ret = dev->rdev.t3cdev_p->ctl(dev->rdev.t3cdev_p, RDMA_GET_MIB, &m);
  1107. if (ret)
  1108. return -ENOSYS;
  1109. memset(stats, 0, sizeof *stats);
  1110. stats->iw.ipInReceives = ((u64) m.ipInReceive_hi << 32) +
  1111. m.ipInReceive_lo;
  1112. stats->iw.ipInHdrErrors = ((u64) m.ipInHdrErrors_hi << 32) +
  1113. m.ipInHdrErrors_lo;
  1114. stats->iw.ipInAddrErrors = ((u64) m.ipInAddrErrors_hi << 32) +
  1115. m.ipInAddrErrors_lo;
  1116. stats->iw.ipInUnknownProtos = ((u64) m.ipInUnknownProtos_hi << 32) +
  1117. m.ipInUnknownProtos_lo;
  1118. stats->iw.ipInDiscards = ((u64) m.ipInDiscards_hi << 32) +
  1119. m.ipInDiscards_lo;
  1120. stats->iw.ipInDelivers = ((u64) m.ipInDelivers_hi << 32) +
  1121. m.ipInDelivers_lo;
  1122. stats->iw.ipOutRequests = ((u64) m.ipOutRequests_hi << 32) +
  1123. m.ipOutRequests_lo;
  1124. stats->iw.ipOutDiscards = ((u64) m.ipOutDiscards_hi << 32) +
  1125. m.ipOutDiscards_lo;
  1126. stats->iw.ipOutNoRoutes = ((u64) m.ipOutNoRoutes_hi << 32) +
  1127. m.ipOutNoRoutes_lo;
  1128. stats->iw.ipReasmTimeout = (u64) m.ipReasmTimeout;
  1129. stats->iw.ipReasmReqds = (u64) m.ipReasmReqds;
  1130. stats->iw.ipReasmOKs = (u64) m.ipReasmOKs;
  1131. stats->iw.ipReasmFails = (u64) m.ipReasmFails;
  1132. stats->iw.tcpActiveOpens = (u64) m.tcpActiveOpens;
  1133. stats->iw.tcpPassiveOpens = (u64) m.tcpPassiveOpens;
  1134. stats->iw.tcpAttemptFails = (u64) m.tcpAttemptFails;
  1135. stats->iw.tcpEstabResets = (u64) m.tcpEstabResets;
  1136. stats->iw.tcpOutRsts = (u64) m.tcpOutRsts;
  1137. stats->iw.tcpCurrEstab = (u64) m.tcpCurrEstab;
  1138. stats->iw.tcpInSegs = ((u64) m.tcpInSegs_hi << 32) +
  1139. m.tcpInSegs_lo;
  1140. stats->iw.tcpOutSegs = ((u64) m.tcpOutSegs_hi << 32) +
  1141. m.tcpOutSegs_lo;
  1142. stats->iw.tcpRetransSegs = ((u64) m.tcpRetransSeg_hi << 32) +
  1143. m.tcpRetransSeg_lo;
  1144. stats->iw.tcpInErrs = ((u64) m.tcpInErrs_hi << 32) +
  1145. m.tcpInErrs_lo;
  1146. stats->iw.tcpRtoMin = (u64) m.tcpRtoMin;
  1147. stats->iw.tcpRtoMax = (u64) m.tcpRtoMax;
  1148. return 0;
  1149. }
  1150. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  1151. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  1152. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  1153. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  1154. static struct device_attribute *iwch_class_attributes[] = {
  1155. &dev_attr_hw_rev,
  1156. &dev_attr_fw_ver,
  1157. &dev_attr_hca_type,
  1158. &dev_attr_board_id,
  1159. };
  1160. int iwch_register_device(struct iwch_dev *dev)
  1161. {
  1162. int ret;
  1163. int i;
  1164. PDBG("%s iwch_dev %p\n", __func__, dev);
  1165. strlcpy(dev->ibdev.name, "cxgb3_%d", IB_DEVICE_NAME_MAX);
  1166. memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
  1167. memcpy(&dev->ibdev.node_guid, dev->rdev.t3cdev_p->lldev->dev_addr, 6);
  1168. dev->ibdev.owner = THIS_MODULE;
  1169. dev->device_cap_flags = IB_DEVICE_LOCAL_DMA_LKEY |
  1170. IB_DEVICE_MEM_WINDOW |
  1171. IB_DEVICE_MEM_MGT_EXTENSIONS;
  1172. /* cxgb3 supports STag 0. */
  1173. dev->ibdev.local_dma_lkey = 0;
  1174. dev->ibdev.uverbs_cmd_mask =
  1175. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1176. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1177. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1178. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1179. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1180. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1181. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1182. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1183. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1184. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1185. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  1186. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1187. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1188. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  1189. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1190. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  1191. (1ull << IB_USER_VERBS_CMD_POST_RECV);
  1192. dev->ibdev.node_type = RDMA_NODE_RNIC;
  1193. memcpy(dev->ibdev.node_desc, IWCH_NODE_DESC, sizeof(IWCH_NODE_DESC));
  1194. dev->ibdev.phys_port_cnt = dev->rdev.port_info.nports;
  1195. dev->ibdev.num_comp_vectors = 1;
  1196. dev->ibdev.dma_device = &(dev->rdev.rnic_info.pdev->dev);
  1197. dev->ibdev.query_device = iwch_query_device;
  1198. dev->ibdev.query_port = iwch_query_port;
  1199. dev->ibdev.query_pkey = iwch_query_pkey;
  1200. dev->ibdev.query_gid = iwch_query_gid;
  1201. dev->ibdev.alloc_ucontext = iwch_alloc_ucontext;
  1202. dev->ibdev.dealloc_ucontext = iwch_dealloc_ucontext;
  1203. dev->ibdev.mmap = iwch_mmap;
  1204. dev->ibdev.alloc_pd = iwch_allocate_pd;
  1205. dev->ibdev.dealloc_pd = iwch_deallocate_pd;
  1206. dev->ibdev.create_ah = iwch_ah_create;
  1207. dev->ibdev.destroy_ah = iwch_ah_destroy;
  1208. dev->ibdev.create_qp = iwch_create_qp;
  1209. dev->ibdev.modify_qp = iwch_ib_modify_qp;
  1210. dev->ibdev.destroy_qp = iwch_destroy_qp;
  1211. dev->ibdev.create_cq = iwch_create_cq;
  1212. dev->ibdev.destroy_cq = iwch_destroy_cq;
  1213. dev->ibdev.resize_cq = iwch_resize_cq;
  1214. dev->ibdev.poll_cq = iwch_poll_cq;
  1215. dev->ibdev.get_dma_mr = iwch_get_dma_mr;
  1216. dev->ibdev.reg_phys_mr = iwch_register_phys_mem;
  1217. dev->ibdev.rereg_phys_mr = iwch_reregister_phys_mem;
  1218. dev->ibdev.reg_user_mr = iwch_reg_user_mr;
  1219. dev->ibdev.dereg_mr = iwch_dereg_mr;
  1220. dev->ibdev.alloc_mw = iwch_alloc_mw;
  1221. dev->ibdev.bind_mw = iwch_bind_mw;
  1222. dev->ibdev.dealloc_mw = iwch_dealloc_mw;
  1223. dev->ibdev.alloc_fast_reg_mr = iwch_alloc_fast_reg_mr;
  1224. dev->ibdev.alloc_fast_reg_page_list = iwch_alloc_fastreg_pbl;
  1225. dev->ibdev.free_fast_reg_page_list = iwch_free_fastreg_pbl;
  1226. dev->ibdev.attach_mcast = iwch_multicast_attach;
  1227. dev->ibdev.detach_mcast = iwch_multicast_detach;
  1228. dev->ibdev.process_mad = iwch_process_mad;
  1229. dev->ibdev.req_notify_cq = iwch_arm_cq;
  1230. dev->ibdev.post_send = iwch_post_send;
  1231. dev->ibdev.post_recv = iwch_post_receive;
  1232. dev->ibdev.get_protocol_stats = iwch_get_mib;
  1233. dev->ibdev.uverbs_abi_ver = IWCH_UVERBS_ABI_VERSION;
  1234. dev->ibdev.iwcm = kmalloc(sizeof(struct iw_cm_verbs), GFP_KERNEL);
  1235. if (!dev->ibdev.iwcm)
  1236. return -ENOMEM;
  1237. dev->ibdev.iwcm->connect = iwch_connect;
  1238. dev->ibdev.iwcm->accept = iwch_accept_cr;
  1239. dev->ibdev.iwcm->reject = iwch_reject_cr;
  1240. dev->ibdev.iwcm->create_listen = iwch_create_listen;
  1241. dev->ibdev.iwcm->destroy_listen = iwch_destroy_listen;
  1242. dev->ibdev.iwcm->add_ref = iwch_qp_add_ref;
  1243. dev->ibdev.iwcm->rem_ref = iwch_qp_rem_ref;
  1244. dev->ibdev.iwcm->get_qp = iwch_get_qp;
  1245. ret = ib_register_device(&dev->ibdev, NULL);
  1246. if (ret)
  1247. goto bail1;
  1248. for (i = 0; i < ARRAY_SIZE(iwch_class_attributes); ++i) {
  1249. ret = device_create_file(&dev->ibdev.dev,
  1250. iwch_class_attributes[i]);
  1251. if (ret) {
  1252. goto bail2;
  1253. }
  1254. }
  1255. return 0;
  1256. bail2:
  1257. ib_unregister_device(&dev->ibdev);
  1258. bail1:
  1259. kfree(dev->ibdev.iwcm);
  1260. return ret;
  1261. }
  1262. void iwch_unregister_device(struct iwch_dev *dev)
  1263. {
  1264. int i;
  1265. PDBG("%s iwch_dev %p\n", __func__, dev);
  1266. for (i = 0; i < ARRAY_SIZE(iwch_class_attributes); ++i)
  1267. device_remove_file(&dev->ibdev.dev,
  1268. iwch_class_attributes[i]);
  1269. ib_unregister_device(&dev->ibdev);
  1270. kfree(dev->ibdev.iwcm);
  1271. return;
  1272. }