62b7009daa6c18a4d21002636637b5a2d258b231
[cascardo/linux.git] / drivers / infiniband / hw / ocrdma / ocrdma_main.c
1 /* This file is part of the Emulex RoCE Device Driver for
2  * RoCE (RDMA over Converged Ethernet) adapters.
3  * Copyright (C) 2012-2015 Emulex. All rights reserved.
4  * EMULEX and SLI are trademarks of Emulex.
5  * www.emulex.com
6  *
7  * This software is available to you under a choice of one of two licenses.
8  * You may choose to be licensed under the terms of the GNU General Public
9  * License (GPL) Version 2, available from the file COPYING in the main
10  * directory of this source tree, or the BSD license below:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * - Redistributions of source code must retain the above copyright notice,
17  *   this list of conditions and the following disclaimer.
18  *
19  * - Redistributions in binary form must reproduce the above copyright
20  *   notice, this list of conditions and the following disclaimer in
21  *   the documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Contact Information:
36  * linux-drivers@emulex.com
37  *
38  * Emulex
39  * 3333 Susan Street
40  * Costa Mesa, CA 92626
41  */
42
43 #include <linux/module.h>
44 #include <linux/idr.h>
45 #include <rdma/ib_verbs.h>
46 #include <rdma/ib_user_verbs.h>
47 #include <rdma/ib_addr.h>
48 #include <rdma/ib_mad.h>
49
50 #include <linux/netdevice.h>
51 #include <net/addrconf.h>
52
53 #include "ocrdma.h"
54 #include "ocrdma_verbs.h"
55 #include "ocrdma_ah.h"
56 #include "be_roce.h"
57 #include "ocrdma_hw.h"
58 #include "ocrdma_stats.h"
59 #include "ocrdma_abi.h"
60
61 MODULE_VERSION(OCRDMA_ROCE_DRV_VERSION);
62 MODULE_DESCRIPTION(OCRDMA_ROCE_DRV_DESC " " OCRDMA_ROCE_DRV_VERSION);
63 MODULE_AUTHOR("Emulex Corporation");
64 MODULE_LICENSE("Dual BSD/GPL");
65
66 static DEFINE_IDR(ocrdma_dev_id);
67
68 void ocrdma_get_guid(struct ocrdma_dev *dev, u8 *guid)
69 {
70         u8 mac_addr[6];
71
72         memcpy(&mac_addr[0], &dev->nic_info.mac_addr[0], ETH_ALEN);
73         guid[0] = mac_addr[0] ^ 2;
74         guid[1] = mac_addr[1];
75         guid[2] = mac_addr[2];
76         guid[3] = 0xff;
77         guid[4] = 0xfe;
78         guid[5] = mac_addr[3];
79         guid[6] = mac_addr[4];
80         guid[7] = mac_addr[5];
81 }
82 static enum rdma_link_layer ocrdma_link_layer(struct ib_device *device,
83                                               u8 port_num)
84 {
85         return IB_LINK_LAYER_ETHERNET;
86 }
87
88 static int ocrdma_port_immutable(struct ib_device *ibdev, u8 port_num,
89                                  struct ib_port_immutable *immutable)
90 {
91         struct ib_port_attr attr;
92         int err;
93
94         err = ocrdma_query_port(ibdev, port_num, &attr);
95         if (err)
96                 return err;
97
98         immutable->pkey_tbl_len = attr.pkey_tbl_len;
99         immutable->gid_tbl_len = attr.gid_tbl_len;
100         immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
101         immutable->max_mad_size = IB_MGMT_MAD_SIZE;
102
103         return 0;
104 }
105
106 static int ocrdma_register_device(struct ocrdma_dev *dev)
107 {
108         strlcpy(dev->ibdev.name, "ocrdma%d", IB_DEVICE_NAME_MAX);
109         ocrdma_get_guid(dev, (u8 *)&dev->ibdev.node_guid);
110         memcpy(dev->ibdev.node_desc, OCRDMA_NODE_DESC,
111                sizeof(OCRDMA_NODE_DESC));
112         dev->ibdev.owner = THIS_MODULE;
113         dev->ibdev.uverbs_abi_ver = OCRDMA_ABI_VERSION;
114         dev->ibdev.uverbs_cmd_mask =
115             OCRDMA_UVERBS(GET_CONTEXT) |
116             OCRDMA_UVERBS(QUERY_DEVICE) |
117             OCRDMA_UVERBS(QUERY_PORT) |
118             OCRDMA_UVERBS(ALLOC_PD) |
119             OCRDMA_UVERBS(DEALLOC_PD) |
120             OCRDMA_UVERBS(REG_MR) |
121             OCRDMA_UVERBS(DEREG_MR) |
122             OCRDMA_UVERBS(CREATE_COMP_CHANNEL) |
123             OCRDMA_UVERBS(CREATE_CQ) |
124             OCRDMA_UVERBS(RESIZE_CQ) |
125             OCRDMA_UVERBS(DESTROY_CQ) |
126             OCRDMA_UVERBS(REQ_NOTIFY_CQ) |
127             OCRDMA_UVERBS(CREATE_QP) |
128             OCRDMA_UVERBS(MODIFY_QP) |
129             OCRDMA_UVERBS(QUERY_QP) |
130             OCRDMA_UVERBS(DESTROY_QP) |
131             OCRDMA_UVERBS(POLL_CQ) |
132             OCRDMA_UVERBS(POST_SEND) |
133             OCRDMA_UVERBS(POST_RECV);
134
135         dev->ibdev.uverbs_cmd_mask |=
136             OCRDMA_UVERBS(CREATE_AH) |
137              OCRDMA_UVERBS(MODIFY_AH) |
138              OCRDMA_UVERBS(QUERY_AH) |
139              OCRDMA_UVERBS(DESTROY_AH);
140
141         dev->ibdev.node_type = RDMA_NODE_IB_CA;
142         dev->ibdev.phys_port_cnt = 1;
143         dev->ibdev.num_comp_vectors = dev->eq_cnt;
144
145         /* mandatory verbs. */
146         dev->ibdev.query_device = ocrdma_query_device;
147         dev->ibdev.query_port = ocrdma_query_port;
148         dev->ibdev.modify_port = ocrdma_modify_port;
149         dev->ibdev.query_gid = ocrdma_query_gid;
150         dev->ibdev.get_netdev = ocrdma_get_netdev;
151         dev->ibdev.add_gid = ocrdma_add_gid;
152         dev->ibdev.del_gid = ocrdma_del_gid;
153         dev->ibdev.get_link_layer = ocrdma_link_layer;
154         dev->ibdev.alloc_pd = ocrdma_alloc_pd;
155         dev->ibdev.dealloc_pd = ocrdma_dealloc_pd;
156
157         dev->ibdev.create_cq = ocrdma_create_cq;
158         dev->ibdev.destroy_cq = ocrdma_destroy_cq;
159         dev->ibdev.resize_cq = ocrdma_resize_cq;
160
161         dev->ibdev.create_qp = ocrdma_create_qp;
162         dev->ibdev.modify_qp = ocrdma_modify_qp;
163         dev->ibdev.query_qp = ocrdma_query_qp;
164         dev->ibdev.destroy_qp = ocrdma_destroy_qp;
165
166         dev->ibdev.query_pkey = ocrdma_query_pkey;
167         dev->ibdev.create_ah = ocrdma_create_ah;
168         dev->ibdev.destroy_ah = ocrdma_destroy_ah;
169         dev->ibdev.query_ah = ocrdma_query_ah;
170         dev->ibdev.modify_ah = ocrdma_modify_ah;
171
172         dev->ibdev.poll_cq = ocrdma_poll_cq;
173         dev->ibdev.post_send = ocrdma_post_send;
174         dev->ibdev.post_recv = ocrdma_post_recv;
175         dev->ibdev.req_notify_cq = ocrdma_arm_cq;
176
177         dev->ibdev.get_dma_mr = ocrdma_get_dma_mr;
178         dev->ibdev.reg_phys_mr = ocrdma_reg_kernel_mr;
179         dev->ibdev.dereg_mr = ocrdma_dereg_mr;
180         dev->ibdev.reg_user_mr = ocrdma_reg_user_mr;
181
182         dev->ibdev.alloc_mr = ocrdma_alloc_mr;
183         dev->ibdev.map_mr_sg = ocrdma_map_mr_sg;
184
185         /* mandatory to support user space verbs consumer. */
186         dev->ibdev.alloc_ucontext = ocrdma_alloc_ucontext;
187         dev->ibdev.dealloc_ucontext = ocrdma_dealloc_ucontext;
188         dev->ibdev.mmap = ocrdma_mmap;
189         dev->ibdev.dma_device = &dev->nic_info.pdev->dev;
190
191         dev->ibdev.process_mad = ocrdma_process_mad;
192         dev->ibdev.get_port_immutable = ocrdma_port_immutable;
193
194         if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
195                 dev->ibdev.uverbs_cmd_mask |=
196                      OCRDMA_UVERBS(CREATE_SRQ) |
197                      OCRDMA_UVERBS(MODIFY_SRQ) |
198                      OCRDMA_UVERBS(QUERY_SRQ) |
199                      OCRDMA_UVERBS(DESTROY_SRQ) |
200                      OCRDMA_UVERBS(POST_SRQ_RECV);
201
202                 dev->ibdev.create_srq = ocrdma_create_srq;
203                 dev->ibdev.modify_srq = ocrdma_modify_srq;
204                 dev->ibdev.query_srq = ocrdma_query_srq;
205                 dev->ibdev.destroy_srq = ocrdma_destroy_srq;
206                 dev->ibdev.post_srq_recv = ocrdma_post_srq_recv;
207         }
208         return ib_register_device(&dev->ibdev, NULL);
209 }
210
211 static int ocrdma_alloc_resources(struct ocrdma_dev *dev)
212 {
213         mutex_init(&dev->dev_lock);
214         dev->cq_tbl = kzalloc(sizeof(struct ocrdma_cq *) *
215                               OCRDMA_MAX_CQ, GFP_KERNEL);
216         if (!dev->cq_tbl)
217                 goto alloc_err;
218
219         if (dev->attr.max_qp) {
220                 dev->qp_tbl = kzalloc(sizeof(struct ocrdma_qp *) *
221                                       OCRDMA_MAX_QP, GFP_KERNEL);
222                 if (!dev->qp_tbl)
223                         goto alloc_err;
224         }
225
226         dev->stag_arr = kzalloc(sizeof(u64) * OCRDMA_MAX_STAG, GFP_KERNEL);
227         if (dev->stag_arr == NULL)
228                 goto alloc_err;
229
230         ocrdma_alloc_pd_pool(dev);
231
232         spin_lock_init(&dev->av_tbl.lock);
233         spin_lock_init(&dev->flush_q_lock);
234         return 0;
235 alloc_err:
236         pr_err("%s(%d) error.\n", __func__, dev->id);
237         return -ENOMEM;
238 }
239
240 static void ocrdma_free_resources(struct ocrdma_dev *dev)
241 {
242         kfree(dev->stag_arr);
243         kfree(dev->qp_tbl);
244         kfree(dev->cq_tbl);
245 }
246
247 /* OCRDMA sysfs interface */
248 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
249                         char *buf)
250 {
251         struct ocrdma_dev *dev = dev_get_drvdata(device);
252
253         return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->nic_info.pdev->vendor);
254 }
255
256 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
257                         char *buf)
258 {
259         struct ocrdma_dev *dev = dev_get_drvdata(device);
260
261         return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->attr.fw_ver[0]);
262 }
263
264 static ssize_t show_hca_type(struct device *device,
265                              struct device_attribute *attr, char *buf)
266 {
267         struct ocrdma_dev *dev = dev_get_drvdata(device);
268
269         return scnprintf(buf, PAGE_SIZE, "%s\n", &dev->model_number[0]);
270 }
271
272 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
273 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
274 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca_type, NULL);
275
276 static struct device_attribute *ocrdma_attributes[] = {
277         &dev_attr_hw_rev,
278         &dev_attr_fw_ver,
279         &dev_attr_hca_type
280 };
281
282 static void ocrdma_remove_sysfiles(struct ocrdma_dev *dev)
283 {
284         int i;
285
286         for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
287                 device_remove_file(&dev->ibdev.dev, ocrdma_attributes[i]);
288 }
289
290 static struct ocrdma_dev *ocrdma_add(struct be_dev_info *dev_info)
291 {
292         int status = 0, i;
293         struct ocrdma_dev *dev;
294
295         dev = (struct ocrdma_dev *)ib_alloc_device(sizeof(struct ocrdma_dev));
296         if (!dev) {
297                 pr_err("Unable to allocate ib device\n");
298                 return NULL;
299         }
300         dev->mbx_cmd = kzalloc(sizeof(struct ocrdma_mqe_emb_cmd), GFP_KERNEL);
301         if (!dev->mbx_cmd)
302                 goto idr_err;
303
304         memcpy(&dev->nic_info, dev_info, sizeof(*dev_info));
305         dev->id = idr_alloc(&ocrdma_dev_id, NULL, 0, 0, GFP_KERNEL);
306         if (dev->id < 0)
307                 goto idr_err;
308
309         status = ocrdma_init_hw(dev);
310         if (status)
311                 goto init_err;
312
313         status = ocrdma_alloc_resources(dev);
314         if (status)
315                 goto alloc_err;
316
317         ocrdma_init_service_level(dev);
318         status = ocrdma_register_device(dev);
319         if (status)
320                 goto alloc_err;
321
322         for (i = 0; i < ARRAY_SIZE(ocrdma_attributes); i++)
323                 if (device_create_file(&dev->ibdev.dev, ocrdma_attributes[i]))
324                         goto sysfs_err;
325         /* Init stats */
326         ocrdma_add_port_stats(dev);
327         /* Interrupt Moderation */
328         INIT_DELAYED_WORK(&dev->eqd_work, ocrdma_eqd_set_task);
329         schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
330
331         pr_info("%s %s: %s \"%s\" port %d\n",
332                 dev_name(&dev->nic_info.pdev->dev), hca_name(dev),
333                 port_speed_string(dev), dev->model_number,
334                 dev->hba_port_num);
335         pr_info("%s ocrdma%d driver loaded successfully\n",
336                 dev_name(&dev->nic_info.pdev->dev), dev->id);
337         return dev;
338
339 sysfs_err:
340         ocrdma_remove_sysfiles(dev);
341 alloc_err:
342         ocrdma_free_resources(dev);
343         ocrdma_cleanup_hw(dev);
344 init_err:
345         idr_remove(&ocrdma_dev_id, dev->id);
346 idr_err:
347         kfree(dev->mbx_cmd);
348         ib_dealloc_device(&dev->ibdev);
349         pr_err("%s() leaving. ret=%d\n", __func__, status);
350         return NULL;
351 }
352
353 static void ocrdma_remove_free(struct ocrdma_dev *dev)
354 {
355
356         idr_remove(&ocrdma_dev_id, dev->id);
357         kfree(dev->mbx_cmd);
358         ib_dealloc_device(&dev->ibdev);
359 }
360
361 static void ocrdma_remove(struct ocrdma_dev *dev)
362 {
363         /* first unregister with stack to stop all the active traffic
364          * of the registered clients.
365          */
366         cancel_delayed_work_sync(&dev->eqd_work);
367         ocrdma_remove_sysfiles(dev);
368         ib_unregister_device(&dev->ibdev);
369
370         ocrdma_rem_port_stats(dev);
371         ocrdma_free_resources(dev);
372         ocrdma_cleanup_hw(dev);
373         ocrdma_remove_free(dev);
374 }
375
376 static int ocrdma_open(struct ocrdma_dev *dev)
377 {
378         struct ib_event port_event;
379
380         port_event.event = IB_EVENT_PORT_ACTIVE;
381         port_event.element.port_num = 1;
382         port_event.device = &dev->ibdev;
383         ib_dispatch_event(&port_event);
384         return 0;
385 }
386
387 static int ocrdma_close(struct ocrdma_dev *dev)
388 {
389         int i;
390         struct ocrdma_qp *qp, **cur_qp;
391         struct ib_event err_event;
392         struct ib_qp_attr attrs;
393         int attr_mask = IB_QP_STATE;
394
395         attrs.qp_state = IB_QPS_ERR;
396         mutex_lock(&dev->dev_lock);
397         if (dev->qp_tbl) {
398                 cur_qp = dev->qp_tbl;
399                 for (i = 0; i < OCRDMA_MAX_QP; i++) {
400                         qp = cur_qp[i];
401                         if (qp && qp->ibqp.qp_type != IB_QPT_GSI) {
402                                 /* change the QP state to ERROR */
403                                 _ocrdma_modify_qp(&qp->ibqp, &attrs, attr_mask);
404
405                                 err_event.event = IB_EVENT_QP_FATAL;
406                                 err_event.element.qp = &qp->ibqp;
407                                 err_event.device = &dev->ibdev;
408                                 ib_dispatch_event(&err_event);
409                         }
410                 }
411         }
412         mutex_unlock(&dev->dev_lock);
413
414         err_event.event = IB_EVENT_PORT_ERR;
415         err_event.element.port_num = 1;
416         err_event.device = &dev->ibdev;
417         ib_dispatch_event(&err_event);
418         return 0;
419 }
420
421 static void ocrdma_shutdown(struct ocrdma_dev *dev)
422 {
423         ocrdma_close(dev);
424         ocrdma_remove(dev);
425 }
426
427 /* event handling via NIC driver ensures that all the NIC specific
428  * initialization done before RoCE driver notifies
429  * event to stack.
430  */
431 static void ocrdma_event_handler(struct ocrdma_dev *dev, u32 event)
432 {
433         switch (event) {
434         case BE_DEV_UP:
435                 ocrdma_open(dev);
436                 break;
437         case BE_DEV_DOWN:
438                 ocrdma_close(dev);
439                 break;
440         case BE_DEV_SHUTDOWN:
441                 ocrdma_shutdown(dev);
442                 break;
443         }
444 }
445
446 static struct ocrdma_driver ocrdma_drv = {
447         .name                   = "ocrdma_driver",
448         .add                    = ocrdma_add,
449         .remove                 = ocrdma_remove,
450         .state_change_handler   = ocrdma_event_handler,
451         .be_abi_version         = OCRDMA_BE_ROCE_ABI_VERSION,
452 };
453
454 static int __init ocrdma_init_module(void)
455 {
456         int status;
457
458         ocrdma_init_debugfs();
459
460         status = be_roce_register_driver(&ocrdma_drv);
461         if (status)
462                 goto err_be_reg;
463
464         return 0;
465
466 err_be_reg:
467
468         return status;
469 }
470
471 static void __exit ocrdma_exit_module(void)
472 {
473         be_roce_unregister_driver(&ocrdma_drv);
474         ocrdma_rem_debugfs();
475         idr_destroy(&ocrdma_dev_id);
476 }
477
478 module_init(ocrdma_init_module);
479 module_exit(ocrdma_exit_module);