hwmon: (acpi_power_meter) Fix acpi_bus_get_device() return value check
[linux-drm-fsl-dcu.git] / drivers / net / ethernet / mellanox / mlx5 / core / main.c
1 /*
2  * Copyright (c) 2013, Mellanox Technologies inc.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <asm-generic/kmap_types.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/errno.h>
37 #include <linux/pci.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/slab.h>
40 #include <linux/io-mapping.h>
41 #include <linux/mlx5/driver.h>
42 #include <linux/mlx5/cq.h>
43 #include <linux/mlx5/qp.h>
44 #include <linux/mlx5/srq.h>
45 #include <linux/debugfs.h>
46 #include "mlx5_core.h"
47
48 #define DRIVER_NAME "mlx5_core"
49 #define DRIVER_VERSION "1.0"
50 #define DRIVER_RELDATE  "June 2013"
51
52 MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
53 MODULE_DESCRIPTION("Mellanox ConnectX-IB HCA core library");
54 MODULE_LICENSE("Dual BSD/GPL");
55 MODULE_VERSION(DRIVER_VERSION);
56
57 int mlx5_core_debug_mask;
58 module_param_named(debug_mask, mlx5_core_debug_mask, int, 0644);
59 MODULE_PARM_DESC(debug_mask, "debug mask: 1 = dump cmd data, 2 = dump cmd exec time, 3 = both. Default=0");
60
61 struct workqueue_struct *mlx5_core_wq;
62
63 static int set_dma_caps(struct pci_dev *pdev)
64 {
65         int err;
66
67         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
68         if (err) {
69                 dev_warn(&pdev->dev, "Warning: couldn't set 64-bit PCI DMA mask.\n");
70                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
71                 if (err) {
72                         dev_err(&pdev->dev, "Can't set PCI DMA mask, aborting.\n");
73                         return err;
74                 }
75         }
76
77         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
78         if (err) {
79                 dev_warn(&pdev->dev,
80                          "Warning: couldn't set 64-bit consistent PCI DMA mask.\n");
81                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
82                 if (err) {
83                         dev_err(&pdev->dev,
84                                 "Can't set consistent PCI DMA mask, aborting.\n");
85                         return err;
86                 }
87         }
88
89         dma_set_max_seg_size(&pdev->dev, 2u * 1024 * 1024 * 1024);
90         return err;
91 }
92
93 static int request_bar(struct pci_dev *pdev)
94 {
95         int err = 0;
96
97         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
98                 dev_err(&pdev->dev, "Missing registers BAR, aborting.\n");
99                 return -ENODEV;
100         }
101
102         err = pci_request_regions(pdev, DRIVER_NAME);
103         if (err)
104                 dev_err(&pdev->dev, "Couldn't get PCI resources, aborting\n");
105
106         return err;
107 }
108
109 static void release_bar(struct pci_dev *pdev)
110 {
111         pci_release_regions(pdev);
112 }
113
114 static int mlx5_enable_msix(struct mlx5_core_dev *dev)
115 {
116         struct mlx5_eq_table *table = &dev->priv.eq_table;
117         int num_eqs = 1 << dev->caps.log_max_eq;
118         int nvec;
119         int err;
120         int i;
121
122         nvec = dev->caps.num_ports * num_online_cpus() + MLX5_EQ_VEC_COMP_BASE;
123         nvec = min_t(int, nvec, num_eqs);
124         if (nvec <= MLX5_EQ_VEC_COMP_BASE)
125                 return -ENOMEM;
126
127         table->msix_arr = kzalloc(nvec * sizeof(*table->msix_arr), GFP_KERNEL);
128         if (!table->msix_arr)
129                 return -ENOMEM;
130
131         for (i = 0; i < nvec; i++)
132                 table->msix_arr[i].entry = i;
133
134 retry:
135         table->num_comp_vectors = nvec - MLX5_EQ_VEC_COMP_BASE;
136         err = pci_enable_msix(dev->pdev, table->msix_arr, nvec);
137         if (err <= 0) {
138                 return err;
139         } else if (err > 2) {
140                 nvec = err;
141                 goto retry;
142         }
143
144         mlx5_core_dbg(dev, "received %d MSI vectors out of %d requested\n", err, nvec);
145
146         return 0;
147 }
148
149 static void mlx5_disable_msix(struct mlx5_core_dev *dev)
150 {
151         struct mlx5_eq_table *table = &dev->priv.eq_table;
152
153         pci_disable_msix(dev->pdev);
154         kfree(table->msix_arr);
155 }
156
157 struct mlx5_reg_host_endianess {
158         u8      he;
159         u8      rsvd[15];
160 };
161
162 static int handle_hca_cap(struct mlx5_core_dev *dev)
163 {
164         struct mlx5_cmd_query_hca_cap_mbox_out *query_out = NULL;
165         struct mlx5_cmd_set_hca_cap_mbox_in *set_ctx = NULL;
166         struct mlx5_cmd_query_hca_cap_mbox_in query_ctx;
167         struct mlx5_cmd_set_hca_cap_mbox_out set_out;
168         u64 flags;
169         int err;
170
171         memset(&query_ctx, 0, sizeof(query_ctx));
172         query_out = kzalloc(sizeof(*query_out), GFP_KERNEL);
173         if (!query_out)
174                 return -ENOMEM;
175
176         set_ctx = kzalloc(sizeof(*set_ctx), GFP_KERNEL);
177         if (!set_ctx) {
178                 err = -ENOMEM;
179                 goto query_ex;
180         }
181
182         query_ctx.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_HCA_CAP);
183         query_ctx.hdr.opmod  = cpu_to_be16(0x1);
184         err = mlx5_cmd_exec(dev, &query_ctx, sizeof(query_ctx),
185                                  query_out, sizeof(*query_out));
186         if (err)
187                 goto query_ex;
188
189         err = mlx5_cmd_status_to_err(&query_out->hdr);
190         if (err) {
191                 mlx5_core_warn(dev, "query hca cap failed, %d\n", err);
192                 goto query_ex;
193         }
194
195         memcpy(&set_ctx->hca_cap, &query_out->hca_cap,
196                sizeof(set_ctx->hca_cap));
197
198         if (dev->profile->mask & MLX5_PROF_MASK_QP_SIZE)
199                 set_ctx->hca_cap.log_max_qp = dev->profile->log_max_qp;
200
201         flags = be64_to_cpu(query_out->hca_cap.flags);
202         /* disable checksum */
203         flags &= ~MLX5_DEV_CAP_FLAG_CMDIF_CSUM;
204
205         set_ctx->hca_cap.flags = cpu_to_be64(flags);
206         memset(&set_out, 0, sizeof(set_out));
207         set_ctx->hca_cap.log_uar_page_sz = cpu_to_be16(PAGE_SHIFT - 12);
208         set_ctx->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_SET_HCA_CAP);
209         err = mlx5_cmd_exec(dev, set_ctx, sizeof(*set_ctx),
210                                  &set_out, sizeof(set_out));
211         if (err) {
212                 mlx5_core_warn(dev, "set hca cap failed, %d\n", err);
213                 goto query_ex;
214         }
215
216         err = mlx5_cmd_status_to_err(&set_out.hdr);
217         if (err)
218                 goto query_ex;
219
220 query_ex:
221         kfree(query_out);
222         kfree(set_ctx);
223
224         return err;
225 }
226
227 static int set_hca_ctrl(struct mlx5_core_dev *dev)
228 {
229         struct mlx5_reg_host_endianess he_in;
230         struct mlx5_reg_host_endianess he_out;
231         int err;
232
233         memset(&he_in, 0, sizeof(he_in));
234         he_in.he = MLX5_SET_HOST_ENDIANNESS;
235         err = mlx5_core_access_reg(dev, &he_in,  sizeof(he_in),
236                                         &he_out, sizeof(he_out),
237                                         MLX5_REG_HOST_ENDIANNESS, 0, 1);
238         return err;
239 }
240
241 static int mlx5_core_enable_hca(struct mlx5_core_dev *dev)
242 {
243         int err;
244         struct mlx5_enable_hca_mbox_in in;
245         struct mlx5_enable_hca_mbox_out out;
246
247         memset(&in, 0, sizeof(in));
248         memset(&out, 0, sizeof(out));
249         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_ENABLE_HCA);
250         err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
251         if (err)
252                 return err;
253
254         if (out.hdr.status)
255                 return mlx5_cmd_status_to_err(&out.hdr);
256
257         return 0;
258 }
259
260 static int mlx5_core_disable_hca(struct mlx5_core_dev *dev)
261 {
262         int err;
263         struct mlx5_disable_hca_mbox_in in;
264         struct mlx5_disable_hca_mbox_out out;
265
266         memset(&in, 0, sizeof(in));
267         memset(&out, 0, sizeof(out));
268         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DISABLE_HCA);
269         err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
270         if (err)
271                 return err;
272
273         if (out.hdr.status)
274                 return mlx5_cmd_status_to_err(&out.hdr);
275
276         return 0;
277 }
278
279 int mlx5_dev_init(struct mlx5_core_dev *dev, struct pci_dev *pdev)
280 {
281         struct mlx5_priv *priv = &dev->priv;
282         int err;
283
284         dev->pdev = pdev;
285         pci_set_drvdata(dev->pdev, dev);
286         strncpy(priv->name, dev_name(&pdev->dev), MLX5_MAX_NAME_LEN);
287         priv->name[MLX5_MAX_NAME_LEN - 1] = 0;
288
289         mutex_init(&priv->pgdir_mutex);
290         INIT_LIST_HEAD(&priv->pgdir_list);
291         spin_lock_init(&priv->mkey_lock);
292
293         priv->dbg_root = debugfs_create_dir(dev_name(&pdev->dev), mlx5_debugfs_root);
294         if (!priv->dbg_root)
295                 return -ENOMEM;
296
297         err = pci_enable_device(pdev);
298         if (err) {
299                 dev_err(&pdev->dev, "Cannot enable PCI device, aborting.\n");
300                 goto err_dbg;
301         }
302
303         err = request_bar(pdev);
304         if (err) {
305                 dev_err(&pdev->dev, "error requesting BARs, aborting.\n");
306                 goto err_disable;
307         }
308
309         pci_set_master(pdev);
310
311         err = set_dma_caps(pdev);
312         if (err) {
313                 dev_err(&pdev->dev, "Failed setting DMA capabilities mask, aborting\n");
314                 goto err_clr_master;
315         }
316
317         dev->iseg_base = pci_resource_start(dev->pdev, 0);
318         dev->iseg = ioremap(dev->iseg_base, sizeof(*dev->iseg));
319         if (!dev->iseg) {
320                 err = -ENOMEM;
321                 dev_err(&pdev->dev, "Failed mapping initialization segment, aborting\n");
322                 goto err_clr_master;
323         }
324         dev_info(&pdev->dev, "firmware version: %d.%d.%d\n", fw_rev_maj(dev),
325                  fw_rev_min(dev), fw_rev_sub(dev));
326
327         err = mlx5_cmd_init(dev);
328         if (err) {
329                 dev_err(&pdev->dev, "Failed initializing command interface, aborting\n");
330                 goto err_unmap;
331         }
332
333         mlx5_pagealloc_init(dev);
334
335         err = mlx5_core_enable_hca(dev);
336         if (err) {
337                 dev_err(&pdev->dev, "enable hca failed\n");
338                 goto err_pagealloc_cleanup;
339         }
340
341         err = mlx5_satisfy_startup_pages(dev, 1);
342         if (err) {
343                 dev_err(&pdev->dev, "failed to allocate boot pages\n");
344                 goto err_disable_hca;
345         }
346
347         err = set_hca_ctrl(dev);
348         if (err) {
349                 dev_err(&pdev->dev, "set_hca_ctrl failed\n");
350                 goto reclaim_boot_pages;
351         }
352
353         err = handle_hca_cap(dev);
354         if (err) {
355                 dev_err(&pdev->dev, "handle_hca_cap failed\n");
356                 goto reclaim_boot_pages;
357         }
358
359         err = mlx5_satisfy_startup_pages(dev, 0);
360         if (err) {
361                 dev_err(&pdev->dev, "failed to allocate init pages\n");
362                 goto reclaim_boot_pages;
363         }
364
365         err = mlx5_pagealloc_start(dev);
366         if (err) {
367                 dev_err(&pdev->dev, "mlx5_pagealloc_start failed\n");
368                 goto reclaim_boot_pages;
369         }
370
371         err = mlx5_cmd_init_hca(dev);
372         if (err) {
373                 dev_err(&pdev->dev, "init hca failed\n");
374                 goto err_pagealloc_stop;
375         }
376
377         mlx5_start_health_poll(dev);
378
379         err = mlx5_cmd_query_hca_cap(dev, &dev->caps);
380         if (err) {
381                 dev_err(&pdev->dev, "query hca failed\n");
382                 goto err_stop_poll;
383         }
384
385         err = mlx5_cmd_query_adapter(dev);
386         if (err) {
387                 dev_err(&pdev->dev, "query adapter failed\n");
388                 goto err_stop_poll;
389         }
390
391         err = mlx5_enable_msix(dev);
392         if (err) {
393                 dev_err(&pdev->dev, "enable msix failed\n");
394                 goto err_stop_poll;
395         }
396
397         err = mlx5_eq_init(dev);
398         if (err) {
399                 dev_err(&pdev->dev, "failed to initialize eq\n");
400                 goto disable_msix;
401         }
402
403         err = mlx5_alloc_uuars(dev, &priv->uuari);
404         if (err) {
405                 dev_err(&pdev->dev, "Failed allocating uar, aborting\n");
406                 goto err_eq_cleanup;
407         }
408
409         err = mlx5_start_eqs(dev);
410         if (err) {
411                 dev_err(&pdev->dev, "Failed to start pages and async EQs\n");
412                 goto err_free_uar;
413         }
414
415         MLX5_INIT_DOORBELL_LOCK(&priv->cq_uar_lock);
416
417         mlx5_init_cq_table(dev);
418         mlx5_init_qp_table(dev);
419         mlx5_init_srq_table(dev);
420
421         return 0;
422
423 err_free_uar:
424         mlx5_free_uuars(dev, &priv->uuari);
425
426 err_eq_cleanup:
427         mlx5_eq_cleanup(dev);
428
429 disable_msix:
430         mlx5_disable_msix(dev);
431
432 err_stop_poll:
433         mlx5_stop_health_poll(dev);
434         mlx5_cmd_teardown_hca(dev);
435
436 err_pagealloc_stop:
437         mlx5_pagealloc_stop(dev);
438
439 reclaim_boot_pages:
440         mlx5_reclaim_startup_pages(dev);
441
442 err_disable_hca:
443         mlx5_core_disable_hca(dev);
444
445 err_pagealloc_cleanup:
446         mlx5_pagealloc_cleanup(dev);
447         mlx5_cmd_cleanup(dev);
448
449 err_unmap:
450         iounmap(dev->iseg);
451
452 err_clr_master:
453         pci_clear_master(dev->pdev);
454         release_bar(dev->pdev);
455
456 err_disable:
457         pci_disable_device(dev->pdev);
458
459 err_dbg:
460         debugfs_remove(priv->dbg_root);
461         return err;
462 }
463 EXPORT_SYMBOL(mlx5_dev_init);
464
465 void mlx5_dev_cleanup(struct mlx5_core_dev *dev)
466 {
467         struct mlx5_priv *priv = &dev->priv;
468
469         mlx5_cleanup_srq_table(dev);
470         mlx5_cleanup_qp_table(dev);
471         mlx5_cleanup_cq_table(dev);
472         mlx5_stop_eqs(dev);
473         mlx5_free_uuars(dev, &priv->uuari);
474         mlx5_eq_cleanup(dev);
475         mlx5_disable_msix(dev);
476         mlx5_stop_health_poll(dev);
477         mlx5_cmd_teardown_hca(dev);
478         mlx5_pagealloc_stop(dev);
479         mlx5_reclaim_startup_pages(dev);
480         mlx5_core_disable_hca(dev);
481         mlx5_pagealloc_cleanup(dev);
482         mlx5_cmd_cleanup(dev);
483         iounmap(dev->iseg);
484         pci_clear_master(dev->pdev);
485         release_bar(dev->pdev);
486         pci_disable_device(dev->pdev);
487         debugfs_remove(priv->dbg_root);
488 }
489 EXPORT_SYMBOL(mlx5_dev_cleanup);
490
491 static int __init init(void)
492 {
493         int err;
494
495         mlx5_register_debugfs();
496         mlx5_core_wq = create_singlethread_workqueue("mlx5_core_wq");
497         if (!mlx5_core_wq) {
498                 err = -ENOMEM;
499                 goto err_debug;
500         }
501         mlx5_health_init();
502
503         return 0;
504
505         mlx5_health_cleanup();
506 err_debug:
507         mlx5_unregister_debugfs();
508         return err;
509 }
510
511 static void __exit cleanup(void)
512 {
513         mlx5_health_cleanup();
514         destroy_workqueue(mlx5_core_wq);
515         mlx5_unregister_debugfs();
516 }
517
518 module_init(init);
519 module_exit(cleanup);