iwch_provider.c 38 KB

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