iwch_provider.c 38 KB

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