OSDN Git Service

Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[android-x86/kernel.git] / drivers / staging / rdma / amso1100 / c2_provider.c
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
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/pci.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/inetdevice.h>
41 #include <linux/delay.h>
42 #include <linux/ethtool.h>
43 #include <linux/mii.h>
44 #include <linux/if_vlan.h>
45 #include <linux/crc32.h>
46 #include <linux/in.h>
47 #include <linux/ip.h>
48 #include <linux/tcp.h>
49 #include <linux/init.h>
50 #include <linux/dma-mapping.h>
51 #include <linux/if_arp.h>
52 #include <linux/vmalloc.h>
53 #include <linux/slab.h>
54
55 #include <asm/io.h>
56 #include <asm/irq.h>
57 #include <asm/byteorder.h>
58
59 #include <rdma/ib_smi.h>
60 #include <rdma/ib_umem.h>
61 #include <rdma/ib_user_verbs.h>
62 #include "c2.h"
63 #include "c2_provider.h"
64 #include "c2_user.h"
65
66 static int c2_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
67                            struct ib_udata *uhw)
68 {
69         struct c2_dev *c2dev = to_c2dev(ibdev);
70
71         pr_debug("%s:%u\n", __func__, __LINE__);
72
73         if (uhw->inlen || uhw->outlen)
74                 return -EINVAL;
75
76         *props = c2dev->props;
77         return 0;
78 }
79
80 static int c2_query_port(struct ib_device *ibdev,
81                          u8 port, struct ib_port_attr *props)
82 {
83         pr_debug("%s:%u\n", __func__, __LINE__);
84
85         props->max_mtu = IB_MTU_4096;
86         props->lid = 0;
87         props->lmc = 0;
88         props->sm_lid = 0;
89         props->sm_sl = 0;
90         props->state = IB_PORT_ACTIVE;
91         props->phys_state = 0;
92         props->port_cap_flags =
93             IB_PORT_CM_SUP |
94             IB_PORT_REINIT_SUP |
95             IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
96         props->gid_tbl_len = 1;
97         props->pkey_tbl_len = 1;
98         props->qkey_viol_cntr = 0;
99         props->active_width = 1;
100         props->active_speed = IB_SPEED_SDR;
101
102         return 0;
103 }
104
105 static int c2_query_pkey(struct ib_device *ibdev,
106                          u8 port, u16 index, u16 * pkey)
107 {
108         pr_debug("%s:%u\n", __func__, __LINE__);
109         *pkey = 0;
110         return 0;
111 }
112
113 static int c2_query_gid(struct ib_device *ibdev, u8 port,
114                         int index, union ib_gid *gid)
115 {
116         struct c2_dev *c2dev = to_c2dev(ibdev);
117
118         pr_debug("%s:%u\n", __func__, __LINE__);
119         memset(&(gid->raw[0]), 0, sizeof(gid->raw));
120         memcpy(&(gid->raw[0]), c2dev->pseudo_netdev->dev_addr, 6);
121
122         return 0;
123 }
124
125 /* Allocate the user context data structure. This keeps track
126  * of all objects associated with a particular user-mode client.
127  */
128 static struct ib_ucontext *c2_alloc_ucontext(struct ib_device *ibdev,
129                                              struct ib_udata *udata)
130 {
131         struct c2_ucontext *context;
132
133         pr_debug("%s:%u\n", __func__, __LINE__);
134         context = kmalloc(sizeof(*context), GFP_KERNEL);
135         if (!context)
136                 return ERR_PTR(-ENOMEM);
137
138         return &context->ibucontext;
139 }
140
141 static int c2_dealloc_ucontext(struct ib_ucontext *context)
142 {
143         pr_debug("%s:%u\n", __func__, __LINE__);
144         kfree(context);
145         return 0;
146 }
147
148 static int c2_mmap_uar(struct ib_ucontext *context, struct vm_area_struct *vma)
149 {
150         pr_debug("%s:%u\n", __func__, __LINE__);
151         return -ENOSYS;
152 }
153
154 static struct ib_pd *c2_alloc_pd(struct ib_device *ibdev,
155                                  struct ib_ucontext *context,
156                                  struct ib_udata *udata)
157 {
158         struct c2_pd *pd;
159         int err;
160
161         pr_debug("%s:%u\n", __func__, __LINE__);
162
163         pd = kmalloc(sizeof(*pd), GFP_KERNEL);
164         if (!pd)
165                 return ERR_PTR(-ENOMEM);
166
167         err = c2_pd_alloc(to_c2dev(ibdev), !context, pd);
168         if (err) {
169                 kfree(pd);
170                 return ERR_PTR(err);
171         }
172
173         if (context) {
174                 if (ib_copy_to_udata(udata, &pd->pd_id, sizeof(__u32))) {
175                         c2_pd_free(to_c2dev(ibdev), pd);
176                         kfree(pd);
177                         return ERR_PTR(-EFAULT);
178                 }
179         }
180
181         return &pd->ibpd;
182 }
183
184 static int c2_dealloc_pd(struct ib_pd *pd)
185 {
186         pr_debug("%s:%u\n", __func__, __LINE__);
187         c2_pd_free(to_c2dev(pd->device), to_c2pd(pd));
188         kfree(pd);
189
190         return 0;
191 }
192
193 static struct ib_ah *c2_ah_create(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
194 {
195         pr_debug("%s:%u\n", __func__, __LINE__);
196         return ERR_PTR(-ENOSYS);
197 }
198
199 static int c2_ah_destroy(struct ib_ah *ah)
200 {
201         pr_debug("%s:%u\n", __func__, __LINE__);
202         return -ENOSYS;
203 }
204
205 static void c2_add_ref(struct ib_qp *ibqp)
206 {
207         struct c2_qp *qp;
208         BUG_ON(!ibqp);
209         qp = to_c2qp(ibqp);
210         atomic_inc(&qp->refcount);
211 }
212
213 static void c2_rem_ref(struct ib_qp *ibqp)
214 {
215         struct c2_qp *qp;
216         BUG_ON(!ibqp);
217         qp = to_c2qp(ibqp);
218         if (atomic_dec_and_test(&qp->refcount))
219                 wake_up(&qp->wait);
220 }
221
222 struct ib_qp *c2_get_qp(struct ib_device *device, int qpn)
223 {
224         struct c2_dev* c2dev = to_c2dev(device);
225         struct c2_qp *qp;
226
227         qp = c2_find_qpn(c2dev, qpn);
228         pr_debug("%s Returning QP=%p for QPN=%d, device=%p, refcount=%d\n",
229                 __func__, qp, qpn, device,
230                 (qp?atomic_read(&qp->refcount):0));
231
232         return (qp?&qp->ibqp:NULL);
233 }
234
235 static struct ib_qp *c2_create_qp(struct ib_pd *pd,
236                                   struct ib_qp_init_attr *init_attr,
237                                   struct ib_udata *udata)
238 {
239         struct c2_qp *qp;
240         int err;
241
242         pr_debug("%s:%u\n", __func__, __LINE__);
243
244         if (init_attr->create_flags)
245                 return ERR_PTR(-EINVAL);
246
247         switch (init_attr->qp_type) {
248         case IB_QPT_RC:
249                 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
250                 if (!qp) {
251                         pr_debug("%s: Unable to allocate QP\n", __func__);
252                         return ERR_PTR(-ENOMEM);
253                 }
254                 spin_lock_init(&qp->lock);
255                 if (pd->uobject) {
256                         /* userspace specific */
257                 }
258
259                 err = c2_alloc_qp(to_c2dev(pd->device),
260                                   to_c2pd(pd), init_attr, qp);
261
262                 if (err && pd->uobject) {
263                         /* userspace specific */
264                 }
265
266                 break;
267         default:
268                 pr_debug("%s: Invalid QP type: %d\n", __func__,
269                         init_attr->qp_type);
270                 return ERR_PTR(-EINVAL);
271         }
272
273         if (err) {
274                 kfree(qp);
275                 return ERR_PTR(err);
276         }
277
278         return &qp->ibqp;
279 }
280
281 static int c2_destroy_qp(struct ib_qp *ib_qp)
282 {
283         struct c2_qp *qp = to_c2qp(ib_qp);
284
285         pr_debug("%s:%u qp=%p,qp->state=%d\n",
286                 __func__, __LINE__, ib_qp, qp->state);
287         c2_free_qp(to_c2dev(ib_qp->device), qp);
288         kfree(qp);
289         return 0;
290 }
291
292 static struct ib_cq *c2_create_cq(struct ib_device *ibdev,
293                                   const struct ib_cq_init_attr *attr,
294                                   struct ib_ucontext *context,
295                                   struct ib_udata *udata)
296 {
297         int entries = attr->cqe;
298         struct c2_cq *cq;
299         int err;
300
301         if (attr->flags)
302                 return ERR_PTR(-EINVAL);
303
304         cq = kmalloc(sizeof(*cq), GFP_KERNEL);
305         if (!cq) {
306                 pr_debug("%s: Unable to allocate CQ\n", __func__);
307                 return ERR_PTR(-ENOMEM);
308         }
309
310         err = c2_init_cq(to_c2dev(ibdev), entries, NULL, cq);
311         if (err) {
312                 pr_debug("%s: error initializing CQ\n", __func__);
313                 kfree(cq);
314                 return ERR_PTR(err);
315         }
316
317         return &cq->ibcq;
318 }
319
320 static int c2_destroy_cq(struct ib_cq *ib_cq)
321 {
322         struct c2_cq *cq = to_c2cq(ib_cq);
323
324         pr_debug("%s:%u\n", __func__, __LINE__);
325
326         c2_free_cq(to_c2dev(ib_cq->device), cq);
327         kfree(cq);
328
329         return 0;
330 }
331
332 static inline u32 c2_convert_access(int acc)
333 {
334         return (acc & IB_ACCESS_REMOTE_WRITE ? C2_ACF_REMOTE_WRITE : 0) |
335             (acc & IB_ACCESS_REMOTE_READ ? C2_ACF_REMOTE_READ : 0) |
336             (acc & IB_ACCESS_LOCAL_WRITE ? C2_ACF_LOCAL_WRITE : 0) |
337             C2_ACF_LOCAL_READ | C2_ACF_WINDOW_BIND;
338 }
339
340 static struct ib_mr *c2_get_dma_mr(struct ib_pd *pd, int acc)
341 {
342         struct c2_mr *mr;
343         u64 *page_list;
344         const u32 total_len = 0xffffffff;       /* AMSO1100 limit */
345         int err, page_shift, pbl_depth, i;
346         u64 kva = 0;
347
348         pr_debug("%s:%u\n", __func__, __LINE__);
349
350         /*
351          * This is a map of all phy mem...use a 32k page_shift.
352          */
353         page_shift = PAGE_SHIFT + 3;
354         pbl_depth = ALIGN(total_len, BIT(page_shift)) >> page_shift;
355
356         page_list = vmalloc(sizeof(u64) * pbl_depth);
357         if (!page_list) {
358                 pr_debug("couldn't vmalloc page_list of size %zd\n",
359                         (sizeof(u64) * pbl_depth));
360                 return ERR_PTR(-ENOMEM);
361         }
362
363         for (i = 0; i < pbl_depth; i++)
364                 page_list[i] = (i << page_shift);
365
366         mr = kmalloc(sizeof(*mr), GFP_KERNEL);
367         if (!mr) {
368                 vfree(page_list);
369                 return ERR_PTR(-ENOMEM);
370         }
371
372         mr->pd = to_c2pd(pd);
373         mr->umem = NULL;
374         pr_debug("%s - page shift %d, pbl_depth %d, total_len %u, "
375                 "*iova_start %llx, first pa %llx, last pa %llx\n",
376                 __func__, page_shift, pbl_depth, total_len,
377                 (unsigned long long) kva,
378                 (unsigned long long) page_list[0],
379                 (unsigned long long) page_list[pbl_depth-1]);
380         err = c2_nsmr_register_phys_kern(to_c2dev(pd->device), page_list,
381                                          BIT(page_shift), pbl_depth,
382                                          total_len, 0, &kva,
383                                          c2_convert_access(acc), mr);
384         vfree(page_list);
385         if (err) {
386                 kfree(mr);
387                 return ERR_PTR(err);
388         }
389
390         return &mr->ibmr;
391 }
392
393 static struct ib_mr *c2_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
394                                     u64 virt, int acc, struct ib_udata *udata)
395 {
396         u64 *pages;
397         u64 kva = 0;
398         int shift, n, len;
399         int i, k, entry;
400         int err = 0;
401         struct scatterlist *sg;
402         struct c2_pd *c2pd = to_c2pd(pd);
403         struct c2_mr *c2mr;
404
405         pr_debug("%s:%u\n", __func__, __LINE__);
406
407         c2mr = kmalloc(sizeof(*c2mr), GFP_KERNEL);
408         if (!c2mr)
409                 return ERR_PTR(-ENOMEM);
410         c2mr->pd = c2pd;
411
412         c2mr->umem = ib_umem_get(pd->uobject->context, start, length, acc, 0);
413         if (IS_ERR(c2mr->umem)) {
414                 err = PTR_ERR(c2mr->umem);
415                 kfree(c2mr);
416                 return ERR_PTR(err);
417         }
418
419         shift = ffs(c2mr->umem->page_size) - 1;
420         n = c2mr->umem->nmap;
421
422         pages = kmalloc_array(n, sizeof(u64), GFP_KERNEL);
423         if (!pages) {
424                 err = -ENOMEM;
425                 goto err;
426         }
427
428         i = 0;
429         for_each_sg(c2mr->umem->sg_head.sgl, sg, c2mr->umem->nmap, entry) {
430                 len = sg_dma_len(sg) >> shift;
431                 for (k = 0; k < len; ++k) {
432                         pages[i++] =
433                                 sg_dma_address(sg) +
434                                 (c2mr->umem->page_size * k);
435                 }
436         }
437
438         kva = virt;
439         err = c2_nsmr_register_phys_kern(to_c2dev(pd->device),
440                                          pages,
441                                          c2mr->umem->page_size,
442                                          i,
443                                          length,
444                                          ib_umem_offset(c2mr->umem),
445                                          &kva,
446                                          c2_convert_access(acc),
447                                          c2mr);
448         kfree(pages);
449         if (err)
450                 goto err;
451         return &c2mr->ibmr;
452
453 err:
454         ib_umem_release(c2mr->umem);
455         kfree(c2mr);
456         return ERR_PTR(err);
457 }
458
459 static int c2_dereg_mr(struct ib_mr *ib_mr)
460 {
461         struct c2_mr *mr = to_c2mr(ib_mr);
462         int err;
463
464         pr_debug("%s:%u\n", __func__, __LINE__);
465
466         err = c2_stag_dealloc(to_c2dev(ib_mr->device), ib_mr->lkey);
467         if (err)
468                 pr_debug("c2_stag_dealloc failed: %d\n", err);
469         else {
470                 if (mr->umem)
471                         ib_umem_release(mr->umem);
472                 kfree(mr);
473         }
474
475         return err;
476 }
477
478 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
479                         char *buf)
480 {
481         struct c2_dev *c2dev = container_of(dev, struct c2_dev, ibdev.dev);
482         pr_debug("%s:%u\n", __func__, __LINE__);
483         return sprintf(buf, "%x\n", c2dev->props.hw_ver);
484 }
485
486 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
487                            char *buf)
488 {
489         struct c2_dev *c2dev = container_of(dev, struct c2_dev, ibdev.dev);
490         pr_debug("%s:%u\n", __func__, __LINE__);
491         return sprintf(buf, "%x.%x.%x\n",
492                        (int) (c2dev->props.fw_ver >> 32),
493                        (int) (c2dev->props.fw_ver >> 16) & 0xffff,
494                        (int) (c2dev->props.fw_ver & 0xffff));
495 }
496
497 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
498                         char *buf)
499 {
500         pr_debug("%s:%u\n", __func__, __LINE__);
501         return sprintf(buf, "AMSO1100\n");
502 }
503
504 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
505                           char *buf)
506 {
507         pr_debug("%s:%u\n", __func__, __LINE__);
508         return sprintf(buf, "%.*s\n", 32, "AMSO1100 Board ID");
509 }
510
511 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
512 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
513 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
514 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
515
516 static struct device_attribute *c2_dev_attributes[] = {
517         &dev_attr_hw_rev,
518         &dev_attr_fw_ver,
519         &dev_attr_hca_type,
520         &dev_attr_board_id
521 };
522
523 static int c2_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
524                         int attr_mask, struct ib_udata *udata)
525 {
526         int err;
527
528         err =
529             c2_qp_modify(to_c2dev(ibqp->device), to_c2qp(ibqp), attr,
530                          attr_mask);
531
532         return err;
533 }
534
535 static int c2_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
536 {
537         pr_debug("%s:%u\n", __func__, __LINE__);
538         return -ENOSYS;
539 }
540
541 static int c2_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
542 {
543         pr_debug("%s:%u\n", __func__, __LINE__);
544         return -ENOSYS;
545 }
546
547 static int c2_process_mad(struct ib_device *ibdev,
548                           int mad_flags,
549                           u8 port_num,
550                           const struct ib_wc *in_wc,
551                           const struct ib_grh *in_grh,
552                           const struct ib_mad_hdr *in_mad,
553                           size_t in_mad_size,
554                           struct ib_mad_hdr *out_mad,
555                           size_t *out_mad_size,
556                           u16 *out_mad_pkey_index)
557 {
558         pr_debug("%s:%u\n", __func__, __LINE__);
559         return -ENOSYS;
560 }
561
562 static int c2_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
563 {
564         pr_debug("%s:%u\n", __func__, __LINE__);
565
566         /* Request a connection */
567         return c2_llp_connect(cm_id, iw_param);
568 }
569
570 static int c2_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
571 {
572         pr_debug("%s:%u\n", __func__, __LINE__);
573
574         /* Accept the new connection */
575         return c2_llp_accept(cm_id, iw_param);
576 }
577
578 static int c2_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
579 {
580         pr_debug("%s:%u\n", __func__, __LINE__);
581
582         return c2_llp_reject(cm_id, pdata, pdata_len);
583 }
584
585 static int c2_service_create(struct iw_cm_id *cm_id, int backlog)
586 {
587         int err;
588
589         pr_debug("%s:%u\n", __func__, __LINE__);
590         err = c2_llp_service_create(cm_id, backlog);
591         pr_debug("%s:%u err=%d\n",
592                 __func__, __LINE__,
593                 err);
594         return err;
595 }
596
597 static int c2_service_destroy(struct iw_cm_id *cm_id)
598 {
599         pr_debug("%s:%u\n", __func__, __LINE__);
600
601         return c2_llp_service_destroy(cm_id);
602 }
603
604 static int c2_pseudo_up(struct net_device *netdev)
605 {
606         struct in_device *ind;
607         struct c2_dev *c2dev = netdev->ml_priv;
608
609         ind = in_dev_get(netdev);
610         if (!ind)
611                 return 0;
612
613         pr_debug("adding...\n");
614         for_ifa(ind) {
615 #ifdef DEBUG
616                 u8 *ip = (u8 *) & ifa->ifa_address;
617
618                 pr_debug("%s: %d.%d.%d.%d\n",
619                        ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]);
620 #endif
621                 c2_add_addr(c2dev, ifa->ifa_address, ifa->ifa_mask);
622         }
623         endfor_ifa(ind);
624         in_dev_put(ind);
625
626         return 0;
627 }
628
629 static int c2_pseudo_down(struct net_device *netdev)
630 {
631         struct in_device *ind;
632         struct c2_dev *c2dev = netdev->ml_priv;
633
634         ind = in_dev_get(netdev);
635         if (!ind)
636                 return 0;
637
638         pr_debug("deleting...\n");
639         for_ifa(ind) {
640 #ifdef DEBUG
641                 u8 *ip = (u8 *) & ifa->ifa_address;
642
643                 pr_debug("%s: %d.%d.%d.%d\n",
644                        ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]);
645 #endif
646                 c2_del_addr(c2dev, ifa->ifa_address, ifa->ifa_mask);
647         }
648         endfor_ifa(ind);
649         in_dev_put(ind);
650
651         return 0;
652 }
653
654 static int c2_pseudo_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
655 {
656         kfree_skb(skb);
657         return NETDEV_TX_OK;
658 }
659
660 static int c2_pseudo_change_mtu(struct net_device *netdev, int new_mtu)
661 {
662         if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
663                 return -EINVAL;
664
665         netdev->mtu = new_mtu;
666
667         /* TODO: Tell rnic about new rmda interface mtu */
668         return 0;
669 }
670
671 static const struct net_device_ops c2_pseudo_netdev_ops = {
672         .ndo_open               = c2_pseudo_up,
673         .ndo_stop               = c2_pseudo_down,
674         .ndo_start_xmit         = c2_pseudo_xmit_frame,
675         .ndo_change_mtu         = c2_pseudo_change_mtu,
676         .ndo_validate_addr      = eth_validate_addr,
677 };
678
679 static void setup(struct net_device *netdev)
680 {
681         netdev->netdev_ops = &c2_pseudo_netdev_ops;
682
683         netdev->watchdog_timeo = 0;
684         netdev->type = ARPHRD_ETHER;
685         netdev->mtu = 1500;
686         netdev->hard_header_len = ETH_HLEN;
687         netdev->addr_len = ETH_ALEN;
688         netdev->tx_queue_len = 0;
689         netdev->flags |= IFF_NOARP;
690 }
691
692 static struct net_device *c2_pseudo_netdev_init(struct c2_dev *c2dev)
693 {
694         char name[IFNAMSIZ];
695         struct net_device *netdev;
696
697         /* change ethxxx to iwxxx */
698         strcpy(name, "iw");
699         strcat(name, &c2dev->netdev->name[3]);
700         netdev = alloc_netdev(0, name, NET_NAME_UNKNOWN, setup);
701         if (!netdev) {
702                 printk(KERN_ERR PFX "%s -  etherdev alloc failed",
703                         __func__);
704                 return NULL;
705         }
706
707         netdev->ml_priv = c2dev;
708
709         SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev);
710
711         memcpy_fromio(netdev->dev_addr, c2dev->kva + C2_REGS_RDMA_ENADDR, 6);
712
713         /* Print out the MAC address */
714         pr_debug("%s: MAC %pM\n", netdev->name, netdev->dev_addr);
715
716 #if 0
717         /* Disable network packets */
718         netif_stop_queue(netdev);
719 #endif
720         return netdev;
721 }
722
723 static int c2_port_immutable(struct ib_device *ibdev, u8 port_num,
724                              struct ib_port_immutable *immutable)
725 {
726         struct ib_port_attr attr;
727         int err;
728
729         err = c2_query_port(ibdev, port_num, &attr);
730         if (err)
731                 return err;
732
733         immutable->pkey_tbl_len = attr.pkey_tbl_len;
734         immutable->gid_tbl_len = attr.gid_tbl_len;
735         immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
736
737         return 0;
738 }
739
740 int c2_register_device(struct c2_dev *dev)
741 {
742         int ret = -ENOMEM;
743         int i;
744
745         /* Register pseudo network device */
746         dev->pseudo_netdev = c2_pseudo_netdev_init(dev);
747         if (!dev->pseudo_netdev)
748                 goto out;
749
750         ret = register_netdev(dev->pseudo_netdev);
751         if (ret)
752                 goto out_free_netdev;
753
754         pr_debug("%s:%u\n", __func__, __LINE__);
755         strlcpy(dev->ibdev.name, "amso%d", IB_DEVICE_NAME_MAX);
756         dev->ibdev.owner = THIS_MODULE;
757         dev->ibdev.uverbs_cmd_mask =
758             (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
759             (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
760             (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
761             (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
762             (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
763             (1ull << IB_USER_VERBS_CMD_REG_MR) |
764             (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
765             (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
766             (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
767             (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
768             (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
769             (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
770             (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
771             (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
772             (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
773             (1ull << IB_USER_VERBS_CMD_POST_SEND) |
774             (1ull << IB_USER_VERBS_CMD_POST_RECV);
775
776         dev->ibdev.node_type = RDMA_NODE_RNIC;
777         memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
778         memcpy(&dev->ibdev.node_guid, dev->pseudo_netdev->dev_addr, 6);
779         dev->ibdev.phys_port_cnt = 1;
780         dev->ibdev.num_comp_vectors = 1;
781         dev->ibdev.dma_device = &dev->pcidev->dev;
782         dev->ibdev.query_device = c2_query_device;
783         dev->ibdev.query_port = c2_query_port;
784         dev->ibdev.query_pkey = c2_query_pkey;
785         dev->ibdev.query_gid = c2_query_gid;
786         dev->ibdev.alloc_ucontext = c2_alloc_ucontext;
787         dev->ibdev.dealloc_ucontext = c2_dealloc_ucontext;
788         dev->ibdev.mmap = c2_mmap_uar;
789         dev->ibdev.alloc_pd = c2_alloc_pd;
790         dev->ibdev.dealloc_pd = c2_dealloc_pd;
791         dev->ibdev.create_ah = c2_ah_create;
792         dev->ibdev.destroy_ah = c2_ah_destroy;
793         dev->ibdev.create_qp = c2_create_qp;
794         dev->ibdev.modify_qp = c2_modify_qp;
795         dev->ibdev.destroy_qp = c2_destroy_qp;
796         dev->ibdev.create_cq = c2_create_cq;
797         dev->ibdev.destroy_cq = c2_destroy_cq;
798         dev->ibdev.poll_cq = c2_poll_cq;
799         dev->ibdev.get_dma_mr = c2_get_dma_mr;
800         dev->ibdev.reg_user_mr = c2_reg_user_mr;
801         dev->ibdev.dereg_mr = c2_dereg_mr;
802         dev->ibdev.get_port_immutable = c2_port_immutable;
803
804         dev->ibdev.alloc_fmr = NULL;
805         dev->ibdev.unmap_fmr = NULL;
806         dev->ibdev.dealloc_fmr = NULL;
807         dev->ibdev.map_phys_fmr = NULL;
808
809         dev->ibdev.attach_mcast = c2_multicast_attach;
810         dev->ibdev.detach_mcast = c2_multicast_detach;
811         dev->ibdev.process_mad = c2_process_mad;
812
813         dev->ibdev.req_notify_cq = c2_arm_cq;
814         dev->ibdev.post_send = c2_post_send;
815         dev->ibdev.post_recv = c2_post_receive;
816
817         dev->ibdev.iwcm = kmalloc(sizeof(*dev->ibdev.iwcm), GFP_KERNEL);
818         if (dev->ibdev.iwcm == NULL) {
819                 ret = -ENOMEM;
820                 goto out_unregister_netdev;
821         }
822         dev->ibdev.iwcm->add_ref = c2_add_ref;
823         dev->ibdev.iwcm->rem_ref = c2_rem_ref;
824         dev->ibdev.iwcm->get_qp = c2_get_qp;
825         dev->ibdev.iwcm->connect = c2_connect;
826         dev->ibdev.iwcm->accept = c2_accept;
827         dev->ibdev.iwcm->reject = c2_reject;
828         dev->ibdev.iwcm->create_listen = c2_service_create;
829         dev->ibdev.iwcm->destroy_listen = c2_service_destroy;
830
831         ret = ib_register_device(&dev->ibdev, NULL);
832         if (ret)
833                 goto out_free_iwcm;
834
835         for (i = 0; i < ARRAY_SIZE(c2_dev_attributes); ++i) {
836                 ret = device_create_file(&dev->ibdev.dev,
837                                                c2_dev_attributes[i]);
838                 if (ret)
839                         goto out_unregister_ibdev;
840         }
841         goto out;
842
843 out_unregister_ibdev:
844         ib_unregister_device(&dev->ibdev);
845 out_free_iwcm:
846         kfree(dev->ibdev.iwcm);
847 out_unregister_netdev:
848         unregister_netdev(dev->pseudo_netdev);
849 out_free_netdev:
850         free_netdev(dev->pseudo_netdev);
851 out:
852         pr_debug("%s:%u ret=%d\n", __func__, __LINE__, ret);
853         return ret;
854 }
855
856 void c2_unregister_device(struct c2_dev *dev)
857 {
858         pr_debug("%s:%u\n", __func__, __LINE__);
859         unregister_netdev(dev->pseudo_netdev);
860         free_netdev(dev->pseudo_netdev);
861         ib_unregister_device(&dev->ibdev);
862 }