iwch_provider.c 39 KB

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