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