Linux-libre 4.4.228-gnu
[librecmc/linux-libre.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_reg_phys_mr(struct ib_pd *ib_pd,
341                                     struct ib_phys_buf *buffer_list,
342                                     int num_phys_buf, int acc, u64 * iova_start)
343 {
344         struct c2_mr *mr;
345         u64 *page_list;
346         u32 total_len;
347         int err, i, j, k, page_shift, pbl_depth;
348
349         pbl_depth = 0;
350         total_len = 0;
351
352         page_shift = PAGE_SHIFT;
353         /*
354          * If there is only 1 buffer we assume this could
355          * be a map of all phy mem...use a 32k page_shift.
356          */
357         if (num_phys_buf == 1)
358                 page_shift += 3;
359
360         for (i = 0; i < num_phys_buf; i++) {
361
362                 if (offset_in_page(buffer_list[i].addr)) {
363                         pr_debug("Unaligned Memory Buffer: 0x%x\n",
364                                 (unsigned int) buffer_list[i].addr);
365                         return ERR_PTR(-EINVAL);
366                 }
367
368                 if (!buffer_list[i].size) {
369                         pr_debug("Invalid Buffer Size\n");
370                         return ERR_PTR(-EINVAL);
371                 }
372
373                 total_len += buffer_list[i].size;
374                 pbl_depth += ALIGN(buffer_list[i].size,
375                                    BIT(page_shift)) >> page_shift;
376         }
377
378         page_list = vmalloc(sizeof(u64) * pbl_depth);
379         if (!page_list) {
380                 pr_debug("couldn't vmalloc page_list of size %zd\n",
381                         (sizeof(u64) * pbl_depth));
382                 return ERR_PTR(-ENOMEM);
383         }
384
385         for (i = 0, j = 0; i < num_phys_buf; i++) {
386
387                 int naddrs;
388
389                 naddrs = ALIGN(buffer_list[i].size,
390                                BIT(page_shift)) >> page_shift;
391                 for (k = 0; k < naddrs; k++)
392                         page_list[j++] = (buffer_list[i].addr +
393                                                      (k << page_shift));
394         }
395
396         mr = kmalloc(sizeof(*mr), GFP_KERNEL);
397         if (!mr) {
398                 vfree(page_list);
399                 return ERR_PTR(-ENOMEM);
400         }
401
402         mr->pd = to_c2pd(ib_pd);
403         mr->umem = NULL;
404         pr_debug("%s - page shift %d, pbl_depth %d, total_len %u, "
405                 "*iova_start %llx, first pa %llx, last pa %llx\n",
406                 __func__, page_shift, pbl_depth, total_len,
407                 (unsigned long long) *iova_start,
408                 (unsigned long long) page_list[0],
409                 (unsigned long long) page_list[pbl_depth-1]);
410         err = c2_nsmr_register_phys_kern(to_c2dev(ib_pd->device), page_list,
411                                          BIT(page_shift), pbl_depth,
412                                          total_len, 0, iova_start,
413                                          c2_convert_access(acc), mr);
414         vfree(page_list);
415         if (err) {
416                 kfree(mr);
417                 return ERR_PTR(err);
418         }
419
420         return &mr->ibmr;
421 }
422
423 static struct ib_mr *c2_get_dma_mr(struct ib_pd *pd, int acc)
424 {
425         struct ib_phys_buf bl;
426         u64 kva = 0;
427
428         pr_debug("%s:%u\n", __func__, __LINE__);
429
430         /* AMSO1100 limit */
431         bl.size = 0xffffffff;
432         bl.addr = 0;
433         return c2_reg_phys_mr(pd, &bl, 1, acc, &kva);
434 }
435
436 static struct ib_mr *c2_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
437                                     u64 virt, int acc, struct ib_udata *udata)
438 {
439         u64 *pages;
440         u64 kva = 0;
441         int shift, n, len;
442         int i, k, entry;
443         int err = 0;
444         struct scatterlist *sg;
445         struct c2_pd *c2pd = to_c2pd(pd);
446         struct c2_mr *c2mr;
447
448         pr_debug("%s:%u\n", __func__, __LINE__);
449
450         c2mr = kmalloc(sizeof(*c2mr), GFP_KERNEL);
451         if (!c2mr)
452                 return ERR_PTR(-ENOMEM);
453         c2mr->pd = c2pd;
454
455         c2mr->umem = ib_umem_get(pd->uobject->context, start, length, acc, 0);
456         if (IS_ERR(c2mr->umem)) {
457                 err = PTR_ERR(c2mr->umem);
458                 kfree(c2mr);
459                 return ERR_PTR(err);
460         }
461
462         shift = ffs(c2mr->umem->page_size) - 1;
463         n = c2mr->umem->nmap;
464
465         pages = kmalloc_array(n, sizeof(u64), GFP_KERNEL);
466         if (!pages) {
467                 err = -ENOMEM;
468                 goto err;
469         }
470
471         i = 0;
472         for_each_sg(c2mr->umem->sg_head.sgl, sg, c2mr->umem->nmap, entry) {
473                 len = sg_dma_len(sg) >> shift;
474                 for (k = 0; k < len; ++k) {
475                         pages[i++] =
476                                 sg_dma_address(sg) +
477                                 (c2mr->umem->page_size * k);
478                 }
479         }
480
481         kva = virt;
482         err = c2_nsmr_register_phys_kern(to_c2dev(pd->device),
483                                          pages,
484                                          c2mr->umem->page_size,
485                                          i,
486                                          length,
487                                          ib_umem_offset(c2mr->umem),
488                                          &kva,
489                                          c2_convert_access(acc),
490                                          c2mr);
491         kfree(pages);
492         if (err)
493                 goto err;
494         return &c2mr->ibmr;
495
496 err:
497         ib_umem_release(c2mr->umem);
498         kfree(c2mr);
499         return ERR_PTR(err);
500 }
501
502 static int c2_dereg_mr(struct ib_mr *ib_mr)
503 {
504         struct c2_mr *mr = to_c2mr(ib_mr);
505         int err;
506
507         pr_debug("%s:%u\n", __func__, __LINE__);
508
509         err = c2_stag_dealloc(to_c2dev(ib_mr->device), ib_mr->lkey);
510         if (err)
511                 pr_debug("c2_stag_dealloc failed: %d\n", err);
512         else {
513                 if (mr->umem)
514                         ib_umem_release(mr->umem);
515                 kfree(mr);
516         }
517
518         return err;
519 }
520
521 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
522                         char *buf)
523 {
524         struct c2_dev *c2dev = container_of(dev, struct c2_dev, ibdev.dev);
525         pr_debug("%s:%u\n", __func__, __LINE__);
526         return sprintf(buf, "%x\n", c2dev->props.hw_ver);
527 }
528
529 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
530                            char *buf)
531 {
532         struct c2_dev *c2dev = container_of(dev, struct c2_dev, ibdev.dev);
533         pr_debug("%s:%u\n", __func__, __LINE__);
534         return sprintf(buf, "%x.%x.%x\n",
535                        (int) (c2dev->props.fw_ver >> 32),
536                        (int) (c2dev->props.fw_ver >> 16) & 0xffff,
537                        (int) (c2dev->props.fw_ver & 0xffff));
538 }
539
540 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
541                         char *buf)
542 {
543         pr_debug("%s:%u\n", __func__, __LINE__);
544         return sprintf(buf, "AMSO1100\n");
545 }
546
547 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
548                           char *buf)
549 {
550         pr_debug("%s:%u\n", __func__, __LINE__);
551         return sprintf(buf, "%.*s\n", 32, "AMSO1100 Board ID");
552 }
553
554 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
555 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
556 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
557 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
558
559 static struct device_attribute *c2_dev_attributes[] = {
560         &dev_attr_hw_rev,
561         &dev_attr_fw_ver,
562         &dev_attr_hca_type,
563         &dev_attr_board_id
564 };
565
566 static int c2_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
567                         int attr_mask, struct ib_udata *udata)
568 {
569         int err;
570
571         err =
572             c2_qp_modify(to_c2dev(ibqp->device), to_c2qp(ibqp), attr,
573                          attr_mask);
574
575         return err;
576 }
577
578 static int c2_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
579 {
580         pr_debug("%s:%u\n", __func__, __LINE__);
581         return -ENOSYS;
582 }
583
584 static int c2_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
585 {
586         pr_debug("%s:%u\n", __func__, __LINE__);
587         return -ENOSYS;
588 }
589
590 static int c2_process_mad(struct ib_device *ibdev,
591                           int mad_flags,
592                           u8 port_num,
593                           const struct ib_wc *in_wc,
594                           const struct ib_grh *in_grh,
595                           const struct ib_mad_hdr *in_mad,
596                           size_t in_mad_size,
597                           struct ib_mad_hdr *out_mad,
598                           size_t *out_mad_size,
599                           u16 *out_mad_pkey_index)
600 {
601         pr_debug("%s:%u\n", __func__, __LINE__);
602         return -ENOSYS;
603 }
604
605 static int c2_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
606 {
607         pr_debug("%s:%u\n", __func__, __LINE__);
608
609         /* Request a connection */
610         return c2_llp_connect(cm_id, iw_param);
611 }
612
613 static int c2_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
614 {
615         pr_debug("%s:%u\n", __func__, __LINE__);
616
617         /* Accept the new connection */
618         return c2_llp_accept(cm_id, iw_param);
619 }
620
621 static int c2_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
622 {
623         int err;
624
625         pr_debug("%s:%u\n", __func__, __LINE__);
626
627         err = c2_llp_reject(cm_id, pdata, pdata_len);
628         return err;
629 }
630
631 static int c2_service_create(struct iw_cm_id *cm_id, int backlog)
632 {
633         int err;
634
635         pr_debug("%s:%u\n", __func__, __LINE__);
636         err = c2_llp_service_create(cm_id, backlog);
637         pr_debug("%s:%u err=%d\n",
638                 __func__, __LINE__,
639                 err);
640         return err;
641 }
642
643 static int c2_service_destroy(struct iw_cm_id *cm_id)
644 {
645         int err;
646         pr_debug("%s:%u\n", __func__, __LINE__);
647
648         err = c2_llp_service_destroy(cm_id);
649
650         return err;
651 }
652
653 static int c2_pseudo_up(struct net_device *netdev)
654 {
655         struct in_device *ind;
656         struct c2_dev *c2dev = netdev->ml_priv;
657
658         ind = in_dev_get(netdev);
659         if (!ind)
660                 return 0;
661
662         pr_debug("adding...\n");
663         for_ifa(ind) {
664 #ifdef DEBUG
665                 u8 *ip = (u8 *) & ifa->ifa_address;
666
667                 pr_debug("%s: %d.%d.%d.%d\n",
668                        ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]);
669 #endif
670                 c2_add_addr(c2dev, ifa->ifa_address, ifa->ifa_mask);
671         }
672         endfor_ifa(ind);
673         in_dev_put(ind);
674
675         return 0;
676 }
677
678 static int c2_pseudo_down(struct net_device *netdev)
679 {
680         struct in_device *ind;
681         struct c2_dev *c2dev = netdev->ml_priv;
682
683         ind = in_dev_get(netdev);
684         if (!ind)
685                 return 0;
686
687         pr_debug("deleting...\n");
688         for_ifa(ind) {
689 #ifdef DEBUG
690                 u8 *ip = (u8 *) & ifa->ifa_address;
691
692                 pr_debug("%s: %d.%d.%d.%d\n",
693                        ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]);
694 #endif
695                 c2_del_addr(c2dev, ifa->ifa_address, ifa->ifa_mask);
696         }
697         endfor_ifa(ind);
698         in_dev_put(ind);
699
700         return 0;
701 }
702
703 static int c2_pseudo_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
704 {
705         kfree_skb(skb);
706         return NETDEV_TX_OK;
707 }
708
709 static int c2_pseudo_change_mtu(struct net_device *netdev, int new_mtu)
710 {
711         if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
712                 return -EINVAL;
713
714         netdev->mtu = new_mtu;
715
716         /* TODO: Tell rnic about new rmda interface mtu */
717         return 0;
718 }
719
720 static const struct net_device_ops c2_pseudo_netdev_ops = {
721         .ndo_open               = c2_pseudo_up,
722         .ndo_stop               = c2_pseudo_down,
723         .ndo_start_xmit         = c2_pseudo_xmit_frame,
724         .ndo_change_mtu         = c2_pseudo_change_mtu,
725         .ndo_validate_addr      = eth_validate_addr,
726 };
727
728 static void setup(struct net_device *netdev)
729 {
730         netdev->netdev_ops = &c2_pseudo_netdev_ops;
731
732         netdev->watchdog_timeo = 0;
733         netdev->type = ARPHRD_ETHER;
734         netdev->mtu = 1500;
735         netdev->hard_header_len = ETH_HLEN;
736         netdev->addr_len = ETH_ALEN;
737         netdev->tx_queue_len = 0;
738         netdev->flags |= IFF_NOARP;
739 }
740
741 static struct net_device *c2_pseudo_netdev_init(struct c2_dev *c2dev)
742 {
743         char name[IFNAMSIZ];
744         struct net_device *netdev;
745
746         /* change ethxxx to iwxxx */
747         strcpy(name, "iw");
748         strcat(name, &c2dev->netdev->name[3]);
749         netdev = alloc_netdev(0, name, NET_NAME_UNKNOWN, setup);
750         if (!netdev) {
751                 printk(KERN_ERR PFX "%s -  etherdev alloc failed",
752                         __func__);
753                 return NULL;
754         }
755
756         netdev->ml_priv = c2dev;
757
758         SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev);
759
760         memcpy_fromio(netdev->dev_addr, c2dev->kva + C2_REGS_RDMA_ENADDR, 6);
761
762         /* Print out the MAC address */
763         pr_debug("%s: MAC %pM\n", netdev->name, netdev->dev_addr);
764
765 #if 0
766         /* Disable network packets */
767         netif_stop_queue(netdev);
768 #endif
769         return netdev;
770 }
771
772 static int c2_port_immutable(struct ib_device *ibdev, u8 port_num,
773                              struct ib_port_immutable *immutable)
774 {
775         struct ib_port_attr attr;
776         int err;
777
778         err = c2_query_port(ibdev, port_num, &attr);
779         if (err)
780                 return err;
781
782         immutable->pkey_tbl_len = attr.pkey_tbl_len;
783         immutable->gid_tbl_len = attr.gid_tbl_len;
784         immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
785
786         return 0;
787 }
788
789 int c2_register_device(struct c2_dev *dev)
790 {
791         int ret = -ENOMEM;
792         int i;
793
794         /* Register pseudo network device */
795         dev->pseudo_netdev = c2_pseudo_netdev_init(dev);
796         if (!dev->pseudo_netdev)
797                 goto out;
798
799         ret = register_netdev(dev->pseudo_netdev);
800         if (ret)
801                 goto out_free_netdev;
802
803         pr_debug("%s:%u\n", __func__, __LINE__);
804         strlcpy(dev->ibdev.name, "amso%d", IB_DEVICE_NAME_MAX);
805         dev->ibdev.owner = THIS_MODULE;
806         dev->ibdev.uverbs_cmd_mask =
807             (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
808             (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
809             (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
810             (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
811             (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
812             (1ull << IB_USER_VERBS_CMD_REG_MR) |
813             (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
814             (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
815             (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
816             (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
817             (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
818             (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
819             (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
820             (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
821             (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
822             (1ull << IB_USER_VERBS_CMD_POST_SEND) |
823             (1ull << IB_USER_VERBS_CMD_POST_RECV);
824
825         dev->ibdev.node_type = RDMA_NODE_RNIC;
826         memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid));
827         memcpy(&dev->ibdev.node_guid, dev->pseudo_netdev->dev_addr, 6);
828         dev->ibdev.phys_port_cnt = 1;
829         dev->ibdev.num_comp_vectors = 1;
830         dev->ibdev.dma_device = &dev->pcidev->dev;
831         dev->ibdev.query_device = c2_query_device;
832         dev->ibdev.query_port = c2_query_port;
833         dev->ibdev.query_pkey = c2_query_pkey;
834         dev->ibdev.query_gid = c2_query_gid;
835         dev->ibdev.alloc_ucontext = c2_alloc_ucontext;
836         dev->ibdev.dealloc_ucontext = c2_dealloc_ucontext;
837         dev->ibdev.mmap = c2_mmap_uar;
838         dev->ibdev.alloc_pd = c2_alloc_pd;
839         dev->ibdev.dealloc_pd = c2_dealloc_pd;
840         dev->ibdev.create_ah = c2_ah_create;
841         dev->ibdev.destroy_ah = c2_ah_destroy;
842         dev->ibdev.create_qp = c2_create_qp;
843         dev->ibdev.modify_qp = c2_modify_qp;
844         dev->ibdev.destroy_qp = c2_destroy_qp;
845         dev->ibdev.create_cq = c2_create_cq;
846         dev->ibdev.destroy_cq = c2_destroy_cq;
847         dev->ibdev.poll_cq = c2_poll_cq;
848         dev->ibdev.get_dma_mr = c2_get_dma_mr;
849         dev->ibdev.reg_phys_mr = c2_reg_phys_mr;
850         dev->ibdev.reg_user_mr = c2_reg_user_mr;
851         dev->ibdev.dereg_mr = c2_dereg_mr;
852         dev->ibdev.get_port_immutable = c2_port_immutable;
853
854         dev->ibdev.alloc_fmr = NULL;
855         dev->ibdev.unmap_fmr = NULL;
856         dev->ibdev.dealloc_fmr = NULL;
857         dev->ibdev.map_phys_fmr = NULL;
858
859         dev->ibdev.attach_mcast = c2_multicast_attach;
860         dev->ibdev.detach_mcast = c2_multicast_detach;
861         dev->ibdev.process_mad = c2_process_mad;
862
863         dev->ibdev.req_notify_cq = c2_arm_cq;
864         dev->ibdev.post_send = c2_post_send;
865         dev->ibdev.post_recv = c2_post_receive;
866
867         dev->ibdev.iwcm = kmalloc(sizeof(*dev->ibdev.iwcm), GFP_KERNEL);
868         if (dev->ibdev.iwcm == NULL) {
869                 ret = -ENOMEM;
870                 goto out_unregister_netdev;
871         }
872         dev->ibdev.iwcm->add_ref = c2_add_ref;
873         dev->ibdev.iwcm->rem_ref = c2_rem_ref;
874         dev->ibdev.iwcm->get_qp = c2_get_qp;
875         dev->ibdev.iwcm->connect = c2_connect;
876         dev->ibdev.iwcm->accept = c2_accept;
877         dev->ibdev.iwcm->reject = c2_reject;
878         dev->ibdev.iwcm->create_listen = c2_service_create;
879         dev->ibdev.iwcm->destroy_listen = c2_service_destroy;
880
881         ret = ib_register_device(&dev->ibdev, NULL);
882         if (ret)
883                 goto out_free_iwcm;
884
885         for (i = 0; i < ARRAY_SIZE(c2_dev_attributes); ++i) {
886                 ret = device_create_file(&dev->ibdev.dev,
887                                                c2_dev_attributes[i]);
888                 if (ret)
889                         goto out_unregister_ibdev;
890         }
891         goto out;
892
893 out_unregister_ibdev:
894         ib_unregister_device(&dev->ibdev);
895 out_free_iwcm:
896         kfree(dev->ibdev.iwcm);
897 out_unregister_netdev:
898         unregister_netdev(dev->pseudo_netdev);
899 out_free_netdev:
900         free_netdev(dev->pseudo_netdev);
901 out:
902         pr_debug("%s:%u ret=%d\n", __func__, __LINE__, ret);
903         return ret;
904 }
905
906 void c2_unregister_device(struct c2_dev *dev)
907 {
908         pr_debug("%s:%u\n", __func__, __LINE__);
909         unregister_netdev(dev->pseudo_netdev);
910         free_netdev(dev->pseudo_netdev);
911         ib_unregister_device(&dev->ibdev);
912 }