iwch_provider.c 39 KB

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