iwch_provider.c 38 KB

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