iwch_provider.c 38 KB

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