iwch_provider.c 38 KB

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