c2_provider.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
  3. * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/pci.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/inetdevice.h>
  40. #include <linux/delay.h>
  41. #include <linux/ethtool.h>
  42. #include <linux/mii.h>
  43. #include <linux/if_vlan.h>
  44. #include <linux/crc32.h>
  45. #include <linux/in.h>
  46. #include <linux/ip.h>
  47. #include <linux/tcp.h>
  48. #include <linux/init.h>
  49. #include <linux/dma-mapping.h>
  50. #include <linux/if_arp.h>
  51. #include <linux/vmalloc.h>
  52. #include <linux/slab.h>
  53. #include <asm/io.h>
  54. #include <asm/irq.h>
  55. #include <asm/byteorder.h>
  56. #include <rdma/ib_smi.h>
  57. #include <rdma/ib_umem.h>
  58. #include <rdma/ib_user_verbs.h>
  59. #include "c2.h"
  60. #include "c2_provider.h"
  61. #include "c2_user.h"
  62. static int c2_query_device(struct ib_device *ibdev,
  63. struct ib_device_attr *props)
  64. {
  65. struct c2_dev *c2dev = to_c2dev(ibdev);
  66. pr_debug("%s:%u\n", __func__, __LINE__);
  67. *props = c2dev->props;
  68. return 0;
  69. }
  70. static int c2_query_port(struct ib_device *ibdev,
  71. u8 port, struct ib_port_attr *props)
  72. {
  73. pr_debug("%s:%u\n", __func__, __LINE__);
  74. props->max_mtu = IB_MTU_4096;
  75. props->lid = 0;
  76. props->lmc = 0;
  77. props->sm_lid = 0;
  78. props->sm_sl = 0;
  79. props->state = IB_PORT_ACTIVE;
  80. props->phys_state = 0;
  81. props->port_cap_flags =
  82. IB_PORT_CM_SUP |
  83. IB_PORT_REINIT_SUP |
  84. IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
  85. props->gid_tbl_len = 1;
  86. props->pkey_tbl_len = 1;
  87. props->qkey_viol_cntr = 0;
  88. props->active_width = 1;
  89. props->active_speed = IB_SPEED_SDR;
  90. return 0;
  91. }
  92. static int c2_query_pkey(struct ib_device *ibdev,
  93. u8 port, u16 index, u16 * pkey)
  94. {
  95. pr_debug("%s:%u\n", __func__, __LINE__);
  96. *pkey = 0;
  97. return 0;
  98. }
  99. static int c2_query_gid(struct ib_device *ibdev, u8 port,
  100. int index, union ib_gid *gid)
  101. {
  102. struct c2_dev *c2dev = to_c2dev(ibdev);
  103. pr_debug("%s:%u\n", __func__, __LINE__);
  104. memset(&(gid->raw[0]), 0, sizeof(gid->raw));
  105. memcpy(&(gid->raw[0]), c2dev->pseudo_netdev->dev_addr, 6);
  106. return 0;
  107. }
  108. /* Allocate the user context data structure. This keeps track
  109. * of all objects associated with a particular user-mode client.
  110. */
  111. static struct ib_ucontext *c2_alloc_ucontext(struct ib_device *ibdev,
  112. struct ib_udata *udata)
  113. {
  114. struct c2_ucontext *context;
  115. pr_debug("%s:%u\n", __func__, __LINE__);
  116. context = kmalloc(sizeof(*context), GFP_KERNEL);
  117. if (!context)
  118. return ERR_PTR(-ENOMEM);
  119. return &context->ibucontext;
  120. }
  121. static int c2_dealloc_ucontext(struct ib_ucontext *context)
  122. {
  123. pr_debug("%s:%u\n", __func__, __LINE__);
  124. kfree(context);
  125. return 0;
  126. }
  127. static int c2_mmap_uar(struct ib_ucontext *context, struct vm_area_struct *vma)
  128. {
  129. pr_debug("%s:%u\n", __func__, __LINE__);
  130. return -ENOSYS;
  131. }
  132. static struct ib_pd *c2_alloc_pd(struct ib_device *ibdev,
  133. struct ib_ucontext *context,
  134. struct ib_udata *udata)
  135. {
  136. struct c2_pd *pd;
  137. int err;
  138. pr_debug("%s:%u\n", __func__, __LINE__);
  139. pd = kmalloc(sizeof(*pd), GFP_KERNEL);
  140. if (!pd)
  141. return ERR_PTR(-ENOMEM);
  142. err = c2_pd_alloc(to_c2dev(ibdev), !context, pd);
  143. if (err) {
  144. kfree(pd);
  145. return ERR_PTR(err);
  146. }
  147. if (context) {
  148. if (ib_copy_to_udata(udata, &pd->pd_id, sizeof(__u32))) {
  149. c2_pd_free(to_c2dev(ibdev), pd);
  150. kfree(pd);
  151. return ERR_PTR(-EFAULT);
  152. }
  153. }
  154. return &pd->ibpd;
  155. }
  156. static int c2_dealloc_pd(struct ib_pd *pd)
  157. {
  158. pr_debug("%s:%u\n", __func__, __LINE__);
  159. c2_pd_free(to_c2dev(pd->device), to_c2pd(pd));
  160. kfree(pd);
  161. return 0;
  162. }
  163. static struct ib_ah *c2_ah_create(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
  164. {
  165. pr_debug("%s:%u\n", __func__, __LINE__);
  166. return ERR_PTR(-ENOSYS);
  167. }
  168. static int c2_ah_destroy(struct ib_ah *ah)
  169. {
  170. pr_debug("%s:%u\n", __func__, __LINE__);
  171. return -ENOSYS;
  172. }
  173. static void c2_add_ref(struct ib_qp *ibqp)
  174. {
  175. struct c2_qp *qp;
  176. BUG_ON(!ibqp);
  177. qp = to_c2qp(ibqp);
  178. atomic_inc(&qp->refcount);
  179. }
  180. static void c2_rem_ref(struct ib_qp *ibqp)
  181. {
  182. struct c2_qp *qp;
  183. BUG_ON(!ibqp);
  184. qp = to_c2qp(ibqp);
  185. if (atomic_dec_and_test(&qp->refcount))
  186. wake_up(&qp->wait);
  187. }
  188. struct ib_qp *c2_get_qp(struct ib_device *device, int qpn)
  189. {
  190. struct c2_dev* c2dev = to_c2dev(device);
  191. struct c2_qp *qp;
  192. qp = c2_find_qpn(c2dev, qpn);
  193. pr_debug("%s Returning QP=%p for QPN=%d, device=%p, refcount=%d\n",
  194. __func__, qp, qpn, device,
  195. (qp?atomic_read(&qp->refcount):0));
  196. return (qp?&qp->ibqp:NULL);
  197. }
  198. static struct ib_qp *c2_create_qp(struct ib_pd *pd,
  199. struct ib_qp_init_attr *init_attr,
  200. struct ib_udata *udata)
  201. {
  202. struct c2_qp *qp;
  203. int err;
  204. pr_debug("%s:%u\n", __func__, __LINE__);
  205. if (init_attr->create_flags)
  206. return ERR_PTR(-EINVAL);
  207. switch (init_attr->qp_type) {
  208. case IB_QPT_RC:
  209. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  210. if (!qp) {
  211. pr_debug("%s: Unable to allocate QP\n", __func__);
  212. return ERR_PTR(-ENOMEM);
  213. }
  214. spin_lock_init(&qp->lock);
  215. if (pd->uobject) {
  216. /* userspace specific */
  217. }
  218. err = c2_alloc_qp(to_c2dev(pd->device),
  219. to_c2pd(pd), init_attr, qp);
  220. if (err && pd->uobject) {
  221. /* userspace specific */
  222. }
  223. break;
  224. default:
  225. pr_debug("%s: Invalid QP type: %d\n", __func__,
  226. init_attr->qp_type);
  227. return ERR_PTR(-EINVAL);
  228. }
  229. if (err) {
  230. kfree(qp);
  231. return ERR_PTR(err);
  232. }
  233. return &qp->ibqp;
  234. }
  235. static int c2_destroy_qp(struct ib_qp *ib_qp)
  236. {
  237. struct c2_qp *qp = to_c2qp(ib_qp);
  238. pr_debug("%s:%u qp=%p,qp->state=%d\n",
  239. __func__, __LINE__, ib_qp, qp->state);
  240. c2_free_qp(to_c2dev(ib_qp->device), qp);
  241. kfree(qp);
  242. return 0;
  243. }
  244. static struct ib_cq *c2_create_cq(struct ib_device *ibdev, int entries, int vector,
  245. struct ib_ucontext *context,
  246. struct ib_udata *udata)
  247. {
  248. struct c2_cq *cq;
  249. int err;
  250. cq = kmalloc(sizeof(*cq), GFP_KERNEL);
  251. if (!cq) {
  252. pr_debug("%s: Unable to allocate CQ\n", __func__);
  253. return ERR_PTR(-ENOMEM);
  254. }
  255. err = c2_init_cq(to_c2dev(ibdev), entries, NULL, cq);
  256. if (err) {
  257. pr_debug("%s: error initializing CQ\n", __func__);
  258. kfree(cq);
  259. return ERR_PTR(err);
  260. }
  261. return &cq->ibcq;
  262. }
  263. static int c2_destroy_cq(struct ib_cq *ib_cq)
  264. {
  265. struct c2_cq *cq = to_c2cq(ib_cq);
  266. pr_debug("%s:%u\n", __func__, __LINE__);
  267. c2_free_cq(to_c2dev(ib_cq->device), cq);
  268. kfree(cq);
  269. return 0;
  270. }
  271. static inline u32 c2_convert_access(int acc)
  272. {
  273. return (acc & IB_ACCESS_REMOTE_WRITE ? C2_ACF_REMOTE_WRITE : 0) |
  274. (acc & IB_ACCESS_REMOTE_READ ? C2_ACF_REMOTE_READ : 0) |
  275. (acc & IB_ACCESS_LOCAL_WRITE ? C2_ACF_LOCAL_WRITE : 0) |
  276. C2_ACF_LOCAL_READ | C2_ACF_WINDOW_BIND;
  277. }
  278. static struct ib_mr *c2_reg_phys_mr(struct ib_pd *ib_pd,
  279. struct ib_phys_buf *buffer_list,
  280. int num_phys_buf, int acc, u64 * iova_start)
  281. {
  282. struct c2_mr *mr;
  283. u64 *page_list;
  284. u32 total_len;
  285. int err, i, j, k, page_shift, pbl_depth;
  286. pbl_depth = 0;
  287. total_len = 0;
  288. page_shift = PAGE_SHIFT;
  289. /*
  290. * If there is only 1 buffer we assume this could
  291. * be a map of all phy mem...use a 32k page_shift.
  292. */
  293. if (num_phys_buf == 1)
  294. page_shift += 3;
  295. for (i = 0; i < num_phys_buf; i++) {
  296. if (buffer_list[i].addr & ~PAGE_MASK) {
  297. pr_debug("Unaligned Memory Buffer: 0x%x\n",
  298. (unsigned int) buffer_list[i].addr);
  299. return ERR_PTR(-EINVAL);
  300. }
  301. if (!buffer_list[i].size) {
  302. pr_debug("Invalid Buffer Size\n");
  303. return ERR_PTR(-EINVAL);
  304. }
  305. total_len += buffer_list[i].size;
  306. pbl_depth += ALIGN(buffer_list[i].size,
  307. (1 << page_shift)) >> page_shift;
  308. }
  309. page_list = vmalloc(sizeof(u64) * pbl_depth);
  310. if (!page_list) {
  311. pr_debug("couldn't vmalloc page_list of size %zd\n",
  312. (sizeof(u64) * pbl_depth));
  313. return ERR_PTR(-ENOMEM);
  314. }
  315. for (i = 0, j = 0; i < num_phys_buf; i++) {
  316. int naddrs;
  317. naddrs = ALIGN(buffer_list[i].size,
  318. (1 << page_shift)) >> page_shift;
  319. for (k = 0; k < naddrs; k++)
  320. page_list[j++] = (buffer_list[i].addr +
  321. (k << page_shift));
  322. }
  323. mr = kmalloc(sizeof(*mr), GFP_KERNEL);
  324. if (!mr) {
  325. vfree(page_list);
  326. return ERR_PTR(-ENOMEM);
  327. }
  328. mr->pd = to_c2pd(ib_pd);
  329. mr->umem = NULL;
  330. pr_debug("%s - page shift %d, pbl_depth %d, total_len %u, "
  331. "*iova_start %llx, first pa %llx, last pa %llx\n",
  332. __func__, page_shift, pbl_depth, total_len,
  333. (unsigned long long) *iova_start,
  334. (unsigned long long) page_list[0],
  335. (unsigned long long) page_list[pbl_depth-1]);
  336. err = c2_nsmr_register_phys_kern(to_c2dev(ib_pd->device), page_list,
  337. (1 << page_shift), pbl_depth,
  338. total_len, 0, iova_start,
  339. c2_convert_access(acc), mr);
  340. vfree(page_list);
  341. if (err) {
  342. kfree(mr);
  343. return ERR_PTR(err);
  344. }
  345. return &mr->ibmr;
  346. }
  347. static struct ib_mr *c2_get_dma_mr(struct ib_pd *pd, int acc)
  348. {
  349. struct ib_phys_buf bl;
  350. u64 kva = 0;
  351. pr_debug("%s:%u\n", __func__, __LINE__);
  352. /* AMSO1100 limit */
  353. bl.size = 0xffffffff;
  354. bl.addr = 0;
  355. return c2_reg_phys_mr(pd, &bl, 1, acc, &kva);
  356. }
  357. static struct ib_mr *c2_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  358. u64 virt, int acc, struct ib_udata *udata)
  359. {
  360. u64 *pages;
  361. u64 kva = 0;
  362. int shift, n, len;
  363. int i, j, k;
  364. int err = 0;
  365. struct ib_umem_chunk *chunk;
  366. struct c2_pd *c2pd = to_c2pd(pd);
  367. struct c2_mr *c2mr;
  368. pr_debug("%s:%u\n", __func__, __LINE__);
  369. c2mr = kmalloc(sizeof(*c2mr), GFP_KERNEL);
  370. if (!c2mr)
  371. return ERR_PTR(-ENOMEM);
  372. c2mr->pd = c2pd;
  373. c2mr->umem = ib_umem_get(pd->uobject->context, start, length, acc, 0);
  374. if (IS_ERR(c2mr->umem)) {
  375. err = PTR_ERR(c2mr->umem);
  376. kfree(c2mr);
  377. return ERR_PTR(err);
  378. }
  379. shift = ffs(c2mr->umem->page_size) - 1;
  380. n = 0;
  381. list_for_each_entry(chunk, &c2mr->umem->chunk_list, list)
  382. n += chunk->nents;
  383. pages = kmalloc(n * sizeof(u64), GFP_KERNEL);
  384. if (!pages) {
  385. err = -ENOMEM;
  386. goto err;
  387. }
  388. i = 0;
  389. list_for_each_entry(chunk, &c2mr->umem->chunk_list, list) {
  390. for (j = 0; j < chunk->nmap; ++j) {
  391. len = sg_dma_len(&chunk->page_list[j]) >> shift;
  392. for (k = 0; k < len; ++k) {
  393. pages[i++] =
  394. sg_dma_address(&chunk->page_list[j]) +
  395. (c2mr->umem->page_size * k);
  396. }
  397. }
  398. }
  399. kva = virt;
  400. err = c2_nsmr_register_phys_kern(to_c2dev(pd->device),
  401. pages,
  402. c2mr->umem->page_size,
  403. i,
  404. length,
  405. c2mr->umem->offset,
  406. &kva,
  407. c2_convert_access(acc),
  408. c2mr);
  409. kfree(pages);
  410. if (err)
  411. goto err;
  412. return &c2mr->ibmr;
  413. err:
  414. ib_umem_release(c2mr->umem);
  415. kfree(c2mr);
  416. return ERR_PTR(err);
  417. }
  418. static int c2_dereg_mr(struct ib_mr *ib_mr)
  419. {
  420. struct c2_mr *mr = to_c2mr(ib_mr);
  421. int err;
  422. pr_debug("%s:%u\n", __func__, __LINE__);
  423. err = c2_stag_dealloc(to_c2dev(ib_mr->device), ib_mr->lkey);
  424. if (err)
  425. pr_debug("c2_stag_dealloc failed: %d\n", err);
  426. else {
  427. if (mr->umem)
  428. ib_umem_release(mr->umem);
  429. kfree(mr);
  430. }
  431. return err;
  432. }
  433. static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
  434. char *buf)
  435. {
  436. struct c2_dev *c2dev = container_of(dev, struct c2_dev, ibdev.dev);
  437. pr_debug("%s:%u\n", __func__, __LINE__);
  438. return sprintf(buf, "%x\n", c2dev->props.hw_ver);
  439. }
  440. static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
  441. char *buf)
  442. {
  443. struct c2_dev *c2dev = container_of(dev, struct c2_dev, ibdev.dev);
  444. pr_debug("%s:%u\n", __func__, __LINE__);
  445. return sprintf(buf, "%x.%x.%x\n",
  446. (int) (c2dev->props.fw_ver >> 32),
  447. (int) (c2dev->props.fw_ver >> 16) & 0xffff,
  448. (int) (c2dev->props.fw_ver & 0xffff));
  449. }
  450. static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
  451. char *buf)
  452. {
  453. pr_debug("%s:%u\n", __func__, __LINE__);
  454. return sprintf(buf, "AMSO1100\n");
  455. }
  456. static ssize_t show_board(struct device *dev, struct device_attribute *attr,
  457. char *buf)
  458. {
  459. pr_debug("%s:%u\n", __func__, __LINE__);
  460. return sprintf(buf, "%.*s\n", 32, "AMSO1100 Board ID");
  461. }
  462. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  463. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  464. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  465. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  466. static struct device_attribute *c2_dev_attributes[] = {
  467. &dev_attr_hw_rev,
  468. &dev_attr_fw_ver,
  469. &dev_attr_hca_type,
  470. &dev_attr_board_id
  471. };
  472. static int c2_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  473. int attr_mask, struct ib_udata *udata)
  474. {
  475. int err;
  476. err =
  477. c2_qp_modify(to_c2dev(ibqp->device), to_c2qp(ibqp), attr,
  478. attr_mask);
  479. return err;
  480. }
  481. static int c2_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  482. {
  483. pr_debug("%s:%u\n", __func__, __LINE__);
  484. return -ENOSYS;
  485. }
  486. static int c2_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  487. {
  488. pr_debug("%s:%u\n", __func__, __LINE__);
  489. return -ENOSYS;
  490. }
  491. static int c2_process_mad(struct ib_device *ibdev,
  492. int mad_flags,
  493. u8 port_num,
  494. struct ib_wc *in_wc,
  495. struct ib_grh *in_grh,
  496. struct ib_mad *in_mad, struct ib_mad *out_mad)
  497. {
  498. pr_debug("%s:%u\n", __func__, __LINE__);
  499. return -ENOSYS;
  500. }
  501. static int c2_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
  502. {
  503. pr_debug("%s:%u\n", __func__, __LINE__);
  504. /* Request a connection */
  505. return c2_llp_connect(cm_id, iw_param);
  506. }
  507. static int c2_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
  508. {
  509. pr_debug("%s:%u\n", __func__, __LINE__);
  510. /* Accept the new connection */
  511. return c2_llp_accept(cm_id, iw_param);
  512. }
  513. static int c2_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
  514. {
  515. int err;
  516. pr_debug("%s:%u\n", __func__, __LINE__);
  517. err = c2_llp_reject(cm_id, pdata, pdata_len);
  518. return err;
  519. }
  520. static int c2_service_create(struct iw_cm_id *cm_id, int backlog)
  521. {
  522. int err;
  523. pr_debug("%s:%u\n", __func__, __LINE__);
  524. err = c2_llp_service_create(cm_id, backlog);
  525. pr_debug("%s:%u err=%d\n",
  526. __func__, __LINE__,
  527. err);
  528. return err;
  529. }
  530. static int c2_service_destroy(struct iw_cm_id *cm_id)
  531. {
  532. int err;
  533. pr_debug("%s:%u\n", __func__, __LINE__);
  534. err = c2_llp_service_destroy(cm_id);
  535. return err;
  536. }
  537. static int c2_pseudo_up(struct net_device *netdev)
  538. {
  539. struct in_device *ind;
  540. struct c2_dev *c2dev = netdev->ml_priv;
  541. ind = in_dev_get(netdev);
  542. if (!ind)
  543. return 0;
  544. pr_debug("adding...\n");
  545. for_ifa(ind) {
  546. #ifdef DEBUG
  547. u8 *ip = (u8 *) & ifa->ifa_address;
  548. pr_debug("%s: %d.%d.%d.%d\n",
  549. ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]);
  550. #endif
  551. c2_add_addr(c2dev, ifa->ifa_address, ifa->ifa_mask);
  552. }
  553. endfor_ifa(ind);
  554. in_dev_put(ind);
  555. return 0;
  556. }
  557. static int c2_pseudo_down(struct net_device *netdev)
  558. {
  559. struct in_device *ind;
  560. struct c2_dev *c2dev = netdev->ml_priv;
  561. ind = in_dev_get(netdev);
  562. if (!ind)
  563. return 0;
  564. pr_debug("deleting...\n");
  565. for_ifa(ind) {
  566. #ifdef DEBUG
  567. u8 *ip = (u8 *) & ifa->ifa_address;
  568. pr_debug("%s: %d.%d.%d.%d\n",
  569. ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]);
  570. #endif
  571. c2_del_addr(c2dev, ifa->ifa_address, ifa->ifa_mask);
  572. }
  573. endfor_ifa(ind);
  574. in_dev_put(ind);
  575. return 0;
  576. }
  577. static int c2_pseudo_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  578. {
  579. kfree_skb(skb);
  580. return NETDEV_TX_OK;
  581. }
  582. static int c2_pseudo_change_mtu(struct net_device *netdev, int new_mtu)
  583. {
  584. if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
  585. return -EINVAL;
  586. netdev->mtu = new_mtu;
  587. /* TODO: Tell rnic about new rmda interface mtu */
  588. return 0;
  589. }
  590. static const struct net_device_ops c2_pseudo_netdev_ops = {
  591. .ndo_open = c2_pseudo_up,
  592. .ndo_stop = c2_pseudo_down,
  593. .ndo_start_xmit = c2_pseudo_xmit_frame,
  594. .ndo_change_mtu = c2_pseudo_change_mtu,
  595. .ndo_validate_addr = eth_validate_addr,
  596. };
  597. static void setup(struct net_device *netdev)
  598. {
  599. netdev->netdev_ops = &c2_pseudo_netdev_ops;
  600. netdev->watchdog_timeo = 0;
  601. netdev->type = ARPHRD_ETHER;
  602. netdev->mtu = 1500;
  603. netdev->hard_header_len = ETH_HLEN;
  604. netdev->addr_len = ETH_ALEN;
  605. netdev->tx_queue_len = 0;
  606. netdev->flags |= IFF_NOARP;
  607. }
  608. static struct net_device *c2_pseudo_netdev_init(struct c2_dev *c2dev)
  609. {
  610. char name[IFNAMSIZ];
  611. struct net_device *netdev;
  612. /* change ethxxx to iwxxx */
  613. strcpy(name, "iw");
  614. strcat(name, &c2dev->netdev->name[3]);
  615. netdev = alloc_netdev(0, name, setup);
  616. if (!netdev) {
  617. printk(KERN_ERR PFX "%s - etherdev alloc failed",
  618. __func__);
  619. return NULL;
  620. }
  621. netdev->ml_priv = c2dev;
  622. SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev);
  623. memcpy_fromio(netdev->dev_addr, c2dev->kva + C2_REGS_RDMA_ENADDR, 6);
  624. /* Print out the MAC address */
  625. pr_debug("%s: MAC %pM\n", netdev->name, netdev->dev_addr);
  626. #if 0
  627. /* Disable network packets */
  628. netif_stop_queue(netdev);
  629. #endif
  630. return netdev;
  631. }
  632. int c2_register_device(struct c2_dev *dev)
  633. {
  634. int ret = -ENOMEM;
  635. int i;
  636. /* Register pseudo network device */
  637. dev->pseudo_netdev = c2_pseudo_netdev_init(dev);
  638. if (!dev->pseudo_netdev)
  639. goto out;
  640. ret = register_netdev(dev->pseudo_netdev);
  641. if (ret)
  642. goto out_free_netdev;
  643. pr_debug("%s:%u\n", __func__, __LINE__);
  644. strlcpy(dev->ibdev.name, "amso%d", IB_DEVICE_NAME_MAX);
  645. dev->ibdev.owner = THIS_MODULE;
  646. dev->ibdev.uverbs_cmd_mask =
  647. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  648. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  649. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  650. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  651. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  652. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  653. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  654. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  655. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  656. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  657. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  658. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  659. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  660. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  661. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  662. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  663. (1ull << IB_USER_VERBS_CMD_POST_RECV);
  664. dev->ibdev.node_type = RDMA_NODE_RNIC;
  665. memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
  666. memcpy(&dev->ibdev.node_guid, dev->pseudo_netdev->dev_addr, 6);
  667. dev->ibdev.phys_port_cnt = 1;
  668. dev->ibdev.num_comp_vectors = 1;
  669. dev->ibdev.dma_device = &dev->pcidev->dev;
  670. dev->ibdev.query_device = c2_query_device;
  671. dev->ibdev.query_port = c2_query_port;
  672. dev->ibdev.query_pkey = c2_query_pkey;
  673. dev->ibdev.query_gid = c2_query_gid;
  674. dev->ibdev.alloc_ucontext = c2_alloc_ucontext;
  675. dev->ibdev.dealloc_ucontext = c2_dealloc_ucontext;
  676. dev->ibdev.mmap = c2_mmap_uar;
  677. dev->ibdev.alloc_pd = c2_alloc_pd;
  678. dev->ibdev.dealloc_pd = c2_dealloc_pd;
  679. dev->ibdev.create_ah = c2_ah_create;
  680. dev->ibdev.destroy_ah = c2_ah_destroy;
  681. dev->ibdev.create_qp = c2_create_qp;
  682. dev->ibdev.modify_qp = c2_modify_qp;
  683. dev->ibdev.destroy_qp = c2_destroy_qp;
  684. dev->ibdev.create_cq = c2_create_cq;
  685. dev->ibdev.destroy_cq = c2_destroy_cq;
  686. dev->ibdev.poll_cq = c2_poll_cq;
  687. dev->ibdev.get_dma_mr = c2_get_dma_mr;
  688. dev->ibdev.reg_phys_mr = c2_reg_phys_mr;
  689. dev->ibdev.reg_user_mr = c2_reg_user_mr;
  690. dev->ibdev.dereg_mr = c2_dereg_mr;
  691. dev->ibdev.alloc_fmr = NULL;
  692. dev->ibdev.unmap_fmr = NULL;
  693. dev->ibdev.dealloc_fmr = NULL;
  694. dev->ibdev.map_phys_fmr = NULL;
  695. dev->ibdev.attach_mcast = c2_multicast_attach;
  696. dev->ibdev.detach_mcast = c2_multicast_detach;
  697. dev->ibdev.process_mad = c2_process_mad;
  698. dev->ibdev.req_notify_cq = c2_arm_cq;
  699. dev->ibdev.post_send = c2_post_send;
  700. dev->ibdev.post_recv = c2_post_receive;
  701. dev->ibdev.iwcm = kmalloc(sizeof(*dev->ibdev.iwcm), GFP_KERNEL);
  702. if (dev->ibdev.iwcm == NULL) {
  703. ret = -ENOMEM;
  704. goto out_unregister_netdev;
  705. }
  706. dev->ibdev.iwcm->add_ref = c2_add_ref;
  707. dev->ibdev.iwcm->rem_ref = c2_rem_ref;
  708. dev->ibdev.iwcm->get_qp = c2_get_qp;
  709. dev->ibdev.iwcm->connect = c2_connect;
  710. dev->ibdev.iwcm->accept = c2_accept;
  711. dev->ibdev.iwcm->reject = c2_reject;
  712. dev->ibdev.iwcm->create_listen = c2_service_create;
  713. dev->ibdev.iwcm->destroy_listen = c2_service_destroy;
  714. ret = ib_register_device(&dev->ibdev, NULL);
  715. if (ret)
  716. goto out_free_iwcm;
  717. for (i = 0; i < ARRAY_SIZE(c2_dev_attributes); ++i) {
  718. ret = device_create_file(&dev->ibdev.dev,
  719. c2_dev_attributes[i]);
  720. if (ret)
  721. goto out_unregister_ibdev;
  722. }
  723. goto out;
  724. out_unregister_ibdev:
  725. ib_unregister_device(&dev->ibdev);
  726. out_free_iwcm:
  727. kfree(dev->ibdev.iwcm);
  728. out_unregister_netdev:
  729. unregister_netdev(dev->pseudo_netdev);
  730. out_free_netdev:
  731. free_netdev(dev->pseudo_netdev);
  732. out:
  733. pr_debug("%s:%u ret=%d\n", __func__, __LINE__, ret);
  734. return ret;
  735. }
  736. void c2_unregister_device(struct c2_dev *dev)
  737. {
  738. pr_debug("%s:%u\n", __func__, __LINE__);
  739. unregister_netdev(dev->pseudo_netdev);
  740. free_netdev(dev->pseudo_netdev);
  741. ib_unregister_device(&dev->ibdev);
  742. }