]> Pileus Git - ~andy/linux/blob - drivers/infiniband/hw/ocrdma/ocrdma_main.c
Merge branch 'upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/floppy...
[~andy/linux] / drivers / infiniband / hw / ocrdma / ocrdma_main.c
1 /*******************************************************************
2  * This file is part of the Emulex RoCE Device Driver for          *
3  * RoCE (RDMA over Converged Ethernet) adapters.                   *
4  * Copyright (C) 2008-2012 Emulex. All rights reserved.            *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  *                                                                 *
8  * This program is free software; you can redistribute it and/or   *
9  * modify it under the terms of version 2 of the GNU General       *
10  * Public License as published by the Free Software Foundation.    *
11  * This program is distributed in the hope that it will be useful. *
12  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17  * more details, a copy of which can be found in the file COPYING  *
18  * included with this package.                                     *
19  *
20  * Contact Information:
21  * linux-drivers@emulex.com
22  *
23  * Emulex
24  * 3333 Susan Street
25  * Costa Mesa, CA 92626
26  *******************************************************************/
27
28 #include <linux/module.h>
29 #include <linux/version.h>
30 #include <linux/idr.h>
31 #include <rdma/ib_verbs.h>
32 #include <rdma/ib_user_verbs.h>
33 #include <rdma/ib_addr.h>
34
35 #include <linux/netdevice.h>
36 #include <net/addrconf.h>
37
38 #include "ocrdma.h"
39 #include "ocrdma_verbs.h"
40 #include "ocrdma_ah.h"
41 #include "be_roce.h"
42 #include "ocrdma_hw.h"
43
44 MODULE_VERSION(OCRDMA_ROCE_DEV_VERSION);
45 MODULE_DESCRIPTION("Emulex RoCE HCA Driver");
46 MODULE_AUTHOR("Emulex Corporation");
47 MODULE_LICENSE("GPL");
48
49 static LIST_HEAD(ocrdma_dev_list);
50 static DEFINE_SPINLOCK(ocrdma_devlist_lock);
51 static DEFINE_IDR(ocrdma_dev_id);
52
53 static union ib_gid ocrdma_zero_sgid;
54
55 static int ocrdma_get_instance(void)
56 {
57         int instance = 0;
58
59         /* Assign an unused number */
60         if (!idr_pre_get(&ocrdma_dev_id, GFP_KERNEL))
61                 return -1;
62         if (idr_get_new(&ocrdma_dev_id, NULL, &instance))
63                 return -1;
64         return instance;
65 }
66
67 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
68 {
69         u8 mac_addr[6];
70
71         memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
72         guid[0] = mac_addr[0] ^ 2;
73         guid[1] = mac_addr[1];
74         guid[2] = mac_addr[2];
75         guid[3] = 0xff;
76         guid[4] = 0xfe;
77         guid[5] = mac_addr[3];
78         guid[6] = mac_addr[4];
79         guid[7] = mac_addr[5];
80 }
81
82 static void ocrdma_build_sgid_mac(union ib_gid *sgid, unsigned char *mac_addr,
83                                   bool is_vlan, u16 vlan_id)
84 {
85         sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
86         sgid->raw[8] = mac_addr[0] ^ 2;
87         sgid->raw[9] = mac_addr[1];
88         sgid->raw[10] = mac_addr[2];
89         if (is_vlan) {
90                 sgid->raw[11] = vlan_id >> 8;
91                 sgid->raw[12] = vlan_id & 0xff;
92         } else {
93                 sgid->raw[11] = 0xff;
94                 sgid->raw[12] = 0xfe;
95         }
96         sgid->raw[13] = mac_addr[3];
97         sgid->raw[14] = mac_addr[4];
98         sgid->raw[15] = mac_addr[5];
99 }
100
101 static void ocrdma_add_sgid(struct ocrdma_dev *dev, unsigned char *mac_addr,
102                             bool is_vlan, u16 vlan_id)
103 {
104         int i;
105         bool found = false;
106         union ib_gid new_sgid;
107         int free_idx = OCRDMA_MAX_SGID;
108         unsigned long flags;
109
110         memset(&ocrdma_zero_sgid, 0, sizeof(union ib_gid));
111
112         ocrdma_build_sgid_mac(&new_sgid, mac_addr, is_vlan, vlan_id);
113
114         spin_lock_irqsave(&dev->sgid_lock, flags);
115         for (i = 0; i < OCRDMA_MAX_SGID; i++) {
116                 if (!memcmp(&dev->sgid_tbl[i], &ocrdma_zero_sgid,
117                             sizeof(union ib_gid))) {
118                         /* found free entry */
119                         if (!found) {
120                                 free_idx = i;
121                                 found = true;
122                                 break;
123                         }
124                 } else if (!memcmp(&dev->sgid_tbl[i], &new_sgid,
125                                    sizeof(union ib_gid))) {
126                         /* entry already present, no addition is required. */
127                         spin_unlock_irqrestore(&dev->sgid_lock, flags);
128                         return;
129                 }
130         }
131         /* if entry doesn't exist and if table has some space, add entry */
132         if (found)
133                 memcpy(&dev->sgid_tbl[free_idx], &new_sgid,
134                        sizeof(union ib_gid));
135         spin_unlock_irqrestore(&dev->sgid_lock, flags);
136 }
137
138 static bool ocrdma_del_sgid(struct ocrdma_dev *dev, unsigned char *mac_addr,
139                             bool is_vlan, u16 vlan_id)
140 {
141         int found = false;
142         int i;
143         union ib_gid sgid;
144         unsigned long flags;
145
146         ocrdma_build_sgid_mac(&sgid, mac_addr, is_vlan, vlan_id);
147
148         spin_lock_irqsave(&dev->sgid_lock, flags);
149         /* first is default sgid, which cannot be deleted. */
150         for (i = 1; i < OCRDMA_MAX_SGID; i++) {
151                 if (!memcmp(&dev->sgid_tbl[i], &sgid, sizeof(union ib_gid))) {
152                         /* found matching entry */
153                         memset(&dev->sgid_tbl[i], 0, sizeof(union ib_gid));
154                         found = true;
155                         break;
156                 }
157         }
158         spin_unlock_irqrestore(&dev->sgid_lock, flags);
159         return found;
160 }
161
162 static void ocrdma_add_default_sgid(struct ocrdma_dev *dev)
163 {
164         /* GID Index 0 - Invariant manufacturer-assigned EUI-64 */
165         union ib_gid *sgid = &dev->sgid_tbl[0];
166
167         sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
168         ocrdma_get_guid(dev, &sgid->raw[8]);
169 }
170
171 static int ocrdma_build_sgid_tbl(struct ocrdma_dev *dev)
172 {
173         struct net_device *netdev, *tmp;
174         u16 vlan_id;
175         bool is_vlan;
176
177         netdev = dev->nic_info.netdev;
178
179         ocrdma_add_default_sgid(dev);
180
181         rcu_read_lock();
182         for_each_netdev_rcu(&init_net, tmp) {
183                 if (netdev == tmp || vlan_dev_real_dev(tmp) == netdev) {
184                         if (!netif_running(tmp) || !netif_oper_up(tmp))
185                                 continue;
186                         if (netdev != tmp) {
187                                 vlan_id = vlan_dev_vlan_id(tmp);
188                                 is_vlan = true;
189                         } else {
190                                 is_vlan = false;
191                                 vlan_id = 0;
192                                 tmp = netdev;
193                         }
194                         ocrdma_add_sgid(dev, tmp->dev_addr, is_vlan, vlan_id);
195                 }
196         }
197         rcu_read_unlock();
198         return 0;
199 }
200
201 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
202
203 static int ocrdma_inet6addr_event(struct notifier_block *notifier,
204                                   unsigned long event, void *ptr)
205 {
206         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
207         struct net_device *event_netdev = ifa->idev->dev;
208         struct net_device *netdev = NULL;
209         struct ib_event gid_event;
210         struct ocrdma_dev *dev;
211         bool found = false;
212         bool is_vlan = false;
213         u16 vid = 0;
214
215         netdev = vlan_dev_real_dev(event_netdev);
216         if (netdev != event_netdev) {
217                 is_vlan = true;
218                 vid = vlan_dev_vlan_id(event_netdev);
219         }
220         rcu_read_lock();
221         list_for_each_entry_rcu(dev, &ocrdma_dev_list, entry) {
222                 if (dev->nic_info.netdev == netdev) {
223                         found = true;
224                         break;
225                 }
226         }
227         rcu_read_unlock();
228
229         if (!found)
230                 return NOTIFY_DONE;
231         if (!rdma_link_local_addr((struct in6_addr *)&ifa->addr))
232                 return NOTIFY_DONE;
233
234         mutex_lock(&dev->dev_lock);
235         switch (event) {
236         case NETDEV_UP:
237                 ocrdma_add_sgid(dev, netdev->dev_addr, is_vlan, vid);
238                 break;
239         case NETDEV_DOWN:
240                 found = ocrdma_del_sgid(dev, netdev->dev_addr, is_vlan, vid);
241                 if (found) {
242                         /* found the matching entry, notify
243                          * the consumers about it
244                          */
245                         gid_event.device = &dev->ibdev;
246                         gid_event.element.port_num = 1;
247                         gid_event.event = IB_EVENT_GID_CHANGE;
248                         ib_dispatch_event(&gid_event);
249                 }
250                 break;
251         default:
252                 break;
253         }
254         mutex_unlock(&dev->dev_lock);
255         return NOTIFY_OK;
256 }
257
258 static struct notifier_block ocrdma_inet6addr_notifier = {
259         .notifier_call = ocrdma_inet6addr_event
260 };
261
262 #endif /* IPV6 */
263
264 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
265                                               u8 port_num)
266 {
267         return IB_LINK_LAYER_ETHERNET;
268 }
269
270 static int ocrdma_register_device(struct ocrdma_dev *dev)
271 {
272         strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
273         ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
274         memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
275                sizeof(OCRDMA_NODE_DESC));
276         dev->ibdev.owner = THIS_MODULE;
277         dev->ibdev.uverbs_cmd_mask =
278             OCRDMA_UVERBS(GET_CONTEXT) |
279             OCRDMA_UVERBS(QUERY_DEVICE) |
280             OCRDMA_UVERBS(QUERY_PORT) |
281             OCRDMA_UVERBS(ALLOC_PD) |
282             OCRDMA_UVERBS(DEALLOC_PD) |
283             OCRDMA_UVERBS(REG_MR) |
284             OCRDMA_UVERBS(DEREG_MR) |
285             OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
286             OCRDMA_UVERBS(CREATE_CQ) |
287             OCRDMA_UVERBS(RESIZE_CQ) |
288             OCRDMA_UVERBS(DESTROY_CQ) |
289             OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
290             OCRDMA_UVERBS(CREATE_QP) |
291             OCRDMA_UVERBS(MODIFY_QP) |
292             OCRDMA_UVERBS(QUERY_QP) |
293             OCRDMA_UVERBS(DESTROY_QP) |
294             OCRDMA_UVERBS(POLL_CQ) |
295             OCRDMA_UVERBS(POST_SEND) |
296             OCRDMA_UVERBS(POST_RECV);
297
298         dev->ibdev.uverbs_cmd_mask |=
299             OCRDMA_UVERBS(CREATE_AH) |
300              OCRDMA_UVERBS(MODIFY_AH) |
301              OCRDMA_UVERBS(QUERY_AH) |
302              OCRDMA_UVERBS(DESTROY_AH);
303
304         dev->ibdev.node_type = RDMA_NODE_IB_CA;
305         dev->ibdev.phys_port_cnt = 1;
306         dev->ibdev.num_comp_vectors = 1;
307
308         /* mandatory verbs. */
309         dev->ibdev.query_device = ocrdma_query_device;
310         dev->ibdev.query_port = ocrdma_query_port;
311         dev->ibdev.modify_port = ocrdma_modify_port;
312         dev->ibdev.query_gid = ocrdma_query_gid;
313         dev->ibdev.get_link_layer = ocrdma_link_layer;
314         dev->ibdev.alloc_pd = ocrdma_alloc_pd;
315         dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
316
317         dev->ibdev.create_cq = ocrdma_create_cq;
318         dev->ibdev.destroy_cq = ocrdma_destroy_cq;
319         dev->ibdev.resize_cq = ocrdma_resize_cq;
320
321         dev->ibdev.create_qp = ocrdma_create_qp;
322         dev->ibdev.modify_qp = ocrdma_modify_qp;
323         dev->ibdev.query_qp = ocrdma_query_qp;
324         dev->ibdev.destroy_qp = ocrdma_destroy_qp;
325
326         dev->ibdev.query_pkey = ocrdma_query_pkey;
327         dev->ibdev.create_ah = ocrdma_create_ah;
328         dev->ibdev.destroy_ah = ocrdma_destroy_ah;
329         dev->ibdev.query_ah = ocrdma_query_ah;
330         dev->ibdev.modify_ah = ocrdma_modify_ah;
331
332         dev->ibdev.poll_cq = ocrdma_poll_cq;
333         dev->ibdev.post_send = ocrdma_post_send;
334         dev->ibdev.post_recv = ocrdma_post_recv;
335         dev->ibdev.req_notify_cq = ocrdma_arm_cq;
336
337         dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
338         dev->ibdev.dereg_mr = ocrdma_dereg_mr;
339         dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
340
341         /* mandatory to support user space verbs consumer. */
342         dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
343         dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
344         dev->ibdev.mmap = ocrdma_mmap;
345         dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
346
347         dev->ibdev.process_mad = ocrdma_process_mad;
348
349         if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
350                 dev->ibdev.uverbs_cmd_mask |=
351                      OCRDMA_UVERBS(CREATE_SRQ) |
352                      OCRDMA_UVERBS(MODIFY_SRQ) |
353                      OCRDMA_UVERBS(QUERY_SRQ) |
354                      OCRDMA_UVERBS(DESTROY_SRQ) |
355                      OCRDMA_UVERBS(POST_SRQ_RECV);
356
357                 dev->ibdev.create_srq = ocrdma_create_srq;
358                 dev->ibdev.modify_srq = ocrdma_modify_srq;
359                 dev->ibdev.query_srq = ocrdma_query_srq;
360                 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
361                 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
362         }
363         return ib_register_device(&dev->ibdev, NULL);
364 }
365
366 static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
367 {
368         mutex_init(&dev->dev_lock);
369         dev->sgid_tbl = kzalloc(sizeof(union ib_gid) *
370                                 OCRDMA_MAX_SGID, GFP_KERNEL);
371         if (!dev->sgid_tbl)
372                 goto alloc_err;
373         spin_lock_init(&dev->sgid_lock);
374
375         dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
376                               OCRDMA_MAX_CQ, GFP_KERNEL);
377         if (!dev->cq_tbl)
378                 goto alloc_err;
379
380         if (dev->attr.max_qp) {
381                 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
382                                       OCRDMA_MAX_QP, GFP_KERNEL);
383                 if (!dev->qp_tbl)
384                         goto alloc_err;
385         }
386         spin_lock_init(&dev->av_tbl.lock);
387         spin_lock_init(&dev->flush_q_lock);
388         return 0;
389 alloc_err:
390         ocrdma_err("%s(%d) error.\n", __func__, dev->id);
391         return -ENOMEM;
392 }
393
394 static void ocrdma_free_resources(struct ocrdma_dev *dev)
395 {
396         kfree(dev->qp_tbl);
397         kfree(dev->cq_tbl);
398         kfree(dev->sgid_tbl);
399 }
400
401 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
402 {
403         int status = 0;
404         struct ocrdma_dev *dev;
405
406         dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
407         if (!dev) {
408                 ocrdma_err("Unable to allocate ib device\n");
409                 return NULL;
410         }
411         dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
412         if (!dev->mbx_cmd)
413                 goto idr_err;
414
415         memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
416         dev->id = ocrdma_get_instance();
417         if (dev->id < 0)
418                 goto idr_err;
419
420         status = ocrdma_init_hw(dev);
421         if (status)
422                 goto init_err;
423
424         status = ocrdma_alloc_resources(dev);
425         if (status)
426                 goto alloc_err;
427
428         status = ocrdma_build_sgid_tbl(dev);
429         if (status)
430                 goto alloc_err;
431
432         status = ocrdma_register_device(dev);
433         if (status)
434                 goto alloc_err;
435
436         spin_lock(&ocrdma_devlist_lock);
437         list_add_tail_rcu(&dev->entry, &ocrdma_dev_list);
438         spin_unlock(&ocrdma_devlist_lock);
439         return dev;
440
441 alloc_err:
442         ocrdma_free_resources(dev);
443         ocrdma_cleanup_hw(dev);
444 init_err:
445         idr_remove(&ocrdma_dev_id, dev->id);
446 idr_err:
447         kfree(dev->mbx_cmd);
448         ib_dealloc_device(&dev->ibdev);
449         ocrdma_err("%s() leaving. ret=%d\n", __func__, status);
450         return NULL;
451 }
452
453 static void ocrdma_remove_free(struct rcu_head *rcu)
454 {
455         struct ocrdma_dev *dev = container_of(rcu, struct ocrdma_dev, rcu);
456
457         ocrdma_free_resources(dev);
458         ocrdma_cleanup_hw(dev);
459
460         idr_remove(&ocrdma_dev_id, dev->id);
461         kfree(dev->mbx_cmd);
462         ib_dealloc_device(&dev->ibdev);
463 }
464
465 static void ocrdma_remove(struct ocrdma_dev *dev)
466 {
467         /* first unregister with stack to stop all the active traffic
468          * of the registered clients.
469          */
470         ib_unregister_device(&dev->ibdev);
471
472         spin_lock(&ocrdma_devlist_lock);
473         list_del_rcu(&dev->entry);
474         spin_unlock(&ocrdma_devlist_lock);
475         call_rcu(&dev->rcu, ocrdma_remove_free);
476 }
477
478 static int ocrdma_open(struct ocrdma_dev *dev)
479 {
480         struct ib_event port_event;
481
482         port_event.event = IB_EVENT_PORT_ACTIVE;
483         port_event.element.port_num = 1;
484         port_event.device = &dev->ibdev;
485         ib_dispatch_event(&port_event);
486         return 0;
487 }
488
489 static int ocrdma_close(struct ocrdma_dev *dev)
490 {
491         int i;
492         struct ocrdma_qp *qp, **cur_qp;
493         struct ib_event err_event;
494         struct ib_qp_attr attrs;
495         int attr_mask = IB_QP_STATE;
496
497         attrs.qp_state = IB_QPS_ERR;
498         mutex_lock(&dev->dev_lock);
499         if (dev->qp_tbl) {
500                 cur_qp = dev->qp_tbl;
501                 for (i = 0; i < OCRDMA_MAX_QP; i++) {
502                         qp = cur_qp[i];
503                         if (qp) {
504                                 /* change the QP state to ERROR */
505                                 _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
506
507                                 err_event.event = IB_EVENT_QP_FATAL;
508                                 err_event.element.qp = &qp->ibqp;
509                                 err_event.device = &dev->ibdev;
510                                 ib_dispatch_event(&err_event);
511                         }
512                 }
513         }
514         mutex_unlock(&dev->dev_lock);
515
516         err_event.event = IB_EVENT_PORT_ERR;
517         err_event.element.port_num = 1;
518         err_event.device = &dev->ibdev;
519         ib_dispatch_event(&err_event);
520         return 0;
521 }
522
523 /* event handling via NIC driver ensures that all the NIC specific
524  * initialization done before RoCE driver notifies
525  * event to stack.
526  */
527 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
528 {
529         switch (event) {
530         case BE_DEV_UP:
531                 ocrdma_open(dev);
532                 break;
533         case BE_DEV_DOWN:
534                 ocrdma_close(dev);
535                 break;
536         };
537 }
538
539 static struct ocrdma_driver ocrdma_drv = {
540         .name                   = "ocrdma_driver",
541         .add                    = ocrdma_add,
542         .remove                 = ocrdma_remove,
543         .state_change_handler   = ocrdma_event_handler,
544 };
545
546 static void ocrdma_unregister_inet6addr_notifier(void)
547 {
548 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
549         unregister_inet6addr_notifier(&ocrdma_inet6addr_notifier);
550 #endif
551 }
552
553 static int __init ocrdma_init_module(void)
554 {
555         int status;
556
557 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
558         status = register_inet6addr_notifier(&ocrdma_inet6addr_notifier);
559         if (status)
560                 return status;
561 #endif
562
563         status = be_roce_register_driver(&ocrdma_drv);
564         if (status)
565                 ocrdma_unregister_inet6addr_notifier();
566
567         return status;
568 }
569
570 static void __exit ocrdma_exit_module(void)
571 {
572         be_roce_unregister_driver(&ocrdma_drv);
573         ocrdma_unregister_inet6addr_notifier();
574 }
575
576 module_init(ocrdma_init_module);
577 module_exit(ocrdma_exit_module);