qed: Add module with basic common support
[linux-2.6-microblaze.git] / drivers / net / ethernet / qlogic / qed / qed_main.c
1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015 QLogic Corporation
3  *
4  * This software is available under the terms of the GNU General Public License
5  * (GPL) Version 2, available from the file COPYING in the main directory of
6  * this source tree.
7  */
8
9 #include <linux/stddef.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/version.h>
14 #include <linux/delay.h>
15 #include <asm/byteorder.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/string.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/workqueue.h>
21 #include <linux/ethtool.h>
22 #include <linux/etherdevice.h>
23 #include <linux/vmalloc.h>
24 #include <linux/qed/qed_if.h>
25
26 #include "qed.h"
27 #include "qed_sp.h"
28 #include "qed_dev_api.h"
29 #include "qed_mcp.h"
30 #include "qed_hw.h"
31
32 static const char version[] =
33         "QLogic QL4xxx 40G/100G Ethernet Driver qed " DRV_MODULE_VERSION "\n";
34
35 MODULE_DESCRIPTION("QLogic 25G/40G/50G/100G Core Module");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION(DRV_MODULE_VERSION);
38
39 #define FW_FILE_VERSION                         \
40         __stringify(FW_MAJOR_VERSION) "."       \
41         __stringify(FW_MINOR_VERSION) "."       \
42         __stringify(FW_REVISION_VERSION) "."    \
43         __stringify(FW_ENGINEERING_VERSION)
44
45 #define QED_FW_FILE_NAME        \
46         "qed/qed_init_values_zipped-" FW_FILE_VERSION ".bin"
47
48 static int __init qed_init(void)
49 {
50         pr_notice("qed_init called\n");
51
52         pr_info("%s", version);
53
54         return 0;
55 }
56
57 static void __exit qed_cleanup(void)
58 {
59         pr_notice("qed_cleanup called\n");
60 }
61
62 module_init(qed_init);
63 module_exit(qed_cleanup);
64
65 /* Check if the DMA controller on the machine can properly handle the DMA
66  * addressing required by the device.
67 */
68 static int qed_set_coherency_mask(struct qed_dev *cdev)
69 {
70         struct device *dev = &cdev->pdev->dev;
71
72         if (dma_set_mask(dev, DMA_BIT_MASK(64)) == 0) {
73                 if (dma_set_coherent_mask(dev, DMA_BIT_MASK(64)) != 0) {
74                         DP_NOTICE(cdev,
75                                   "Can't request 64-bit consistent allocations\n");
76                         return -EIO;
77                 }
78         } else if (dma_set_mask(dev, DMA_BIT_MASK(32)) != 0) {
79                 DP_NOTICE(cdev, "Can't request 64b/32b DMA addresses\n");
80                 return -EIO;
81         }
82
83         return 0;
84 }
85
86 static void qed_free_pci(struct qed_dev *cdev)
87 {
88         struct pci_dev *pdev = cdev->pdev;
89
90         if (cdev->doorbells)
91                 iounmap(cdev->doorbells);
92         if (cdev->regview)
93                 iounmap(cdev->regview);
94         if (atomic_read(&pdev->enable_cnt) == 1)
95                 pci_release_regions(pdev);
96
97         pci_disable_device(pdev);
98 }
99
100 /* Performs PCI initializations as well as initializing PCI-related parameters
101  * in the device structrue. Returns 0 in case of success.
102  */
103 static int qed_init_pci(struct qed_dev *cdev,
104                         struct pci_dev *pdev)
105 {
106         int rc;
107
108         cdev->pdev = pdev;
109
110         rc = pci_enable_device(pdev);
111         if (rc) {
112                 DP_NOTICE(cdev, "Cannot enable PCI device\n");
113                 goto err0;
114         }
115
116         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
117                 DP_NOTICE(cdev, "No memory region found in bar #0\n");
118                 rc = -EIO;
119                 goto err1;
120         }
121
122         if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
123                 DP_NOTICE(cdev, "No memory region found in bar #2\n");
124                 rc = -EIO;
125                 goto err1;
126         }
127
128         if (atomic_read(&pdev->enable_cnt) == 1) {
129                 rc = pci_request_regions(pdev, "qed");
130                 if (rc) {
131                         DP_NOTICE(cdev,
132                                   "Failed to request PCI memory resources\n");
133                         goto err1;
134                 }
135                 pci_set_master(pdev);
136                 pci_save_state(pdev);
137         }
138
139         if (!pci_is_pcie(pdev)) {
140                 DP_NOTICE(cdev, "The bus is not PCI Express\n");
141                 rc = -EIO;
142                 goto err2;
143         }
144
145         cdev->pci_params.pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
146         if (cdev->pci_params.pm_cap == 0)
147                 DP_NOTICE(cdev, "Cannot find power management capability\n");
148
149         rc = qed_set_coherency_mask(cdev);
150         if (rc)
151                 goto err2;
152
153         cdev->pci_params.mem_start = pci_resource_start(pdev, 0);
154         cdev->pci_params.mem_end = pci_resource_end(pdev, 0);
155         cdev->pci_params.irq = pdev->irq;
156
157         cdev->regview = pci_ioremap_bar(pdev, 0);
158         if (!cdev->regview) {
159                 DP_NOTICE(cdev, "Cannot map register space, aborting\n");
160                 rc = -ENOMEM;
161                 goto err2;
162         }
163
164         cdev->db_phys_addr = pci_resource_start(cdev->pdev, 2);
165         cdev->db_size = pci_resource_len(cdev->pdev, 2);
166         cdev->doorbells = ioremap_wc(cdev->db_phys_addr, cdev->db_size);
167         if (!cdev->doorbells) {
168                 DP_NOTICE(cdev, "Cannot map doorbell space\n");
169                 return -ENOMEM;
170         }
171
172         return 0;
173
174 err2:
175         pci_release_regions(pdev);
176 err1:
177         pci_disable_device(pdev);
178 err0:
179         return rc;
180 }
181
182 int qed_fill_dev_info(struct qed_dev *cdev,
183                       struct qed_dev_info *dev_info)
184 {
185         memset(dev_info, 0, sizeof(struct qed_dev_info));
186
187         dev_info->num_hwfns = cdev->num_hwfns;
188         dev_info->pci_mem_start = cdev->pci_params.mem_start;
189         dev_info->pci_mem_end = cdev->pci_params.mem_end;
190         dev_info->pci_irq = cdev->pci_params.irq;
191         dev_info->is_mf = IS_MF(&cdev->hwfns[0]);
192         ether_addr_copy(dev_info->hw_mac, cdev->hwfns[0].hw_info.hw_mac_addr);
193
194         dev_info->fw_major = FW_MAJOR_VERSION;
195         dev_info->fw_minor = FW_MINOR_VERSION;
196         dev_info->fw_rev = FW_REVISION_VERSION;
197         dev_info->fw_eng = FW_ENGINEERING_VERSION;
198         dev_info->mf_mode = cdev->mf_mode;
199
200         qed_mcp_get_mfw_ver(cdev, &dev_info->mfw_rev);
201
202         return 0;
203 }
204
205 static void qed_free_cdev(struct qed_dev *cdev)
206 {
207         kfree((void *)cdev);
208 }
209
210 static struct qed_dev *qed_alloc_cdev(struct pci_dev *pdev)
211 {
212         struct qed_dev *cdev;
213
214         cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
215         if (!cdev)
216                 return cdev;
217
218         qed_init_struct(cdev);
219
220         return cdev;
221 }
222
223 /* Sets the requested power state */
224 static int qed_set_power_state(struct qed_dev *cdev,
225                                pci_power_t state)
226 {
227         if (!cdev)
228                 return -ENODEV;
229
230         DP_VERBOSE(cdev, NETIF_MSG_DRV, "Omitting Power state change\n");
231         return 0;
232 }
233
234 /* probing */
235 static struct qed_dev *qed_probe(struct pci_dev *pdev,
236                                  enum qed_protocol protocol,
237                                  u32 dp_module,
238                                  u8 dp_level)
239 {
240         struct qed_dev *cdev;
241         int rc;
242
243         cdev = qed_alloc_cdev(pdev);
244         if (!cdev)
245                 goto err0;
246
247         cdev->protocol = protocol;
248
249         qed_init_dp(cdev, dp_module, dp_level);
250
251         rc = qed_init_pci(cdev, pdev);
252         if (rc) {
253                 DP_ERR(cdev, "init pci failed\n");
254                 goto err1;
255         }
256         DP_INFO(cdev, "PCI init completed successfully\n");
257
258         rc = qed_hw_prepare(cdev, QED_PCI_DEFAULT);
259         if (rc) {
260                 DP_ERR(cdev, "hw prepare failed\n");
261                 goto err2;
262         }
263
264         DP_INFO(cdev, "qed_probe completed successffuly\n");
265
266         return cdev;
267
268 err2:
269         qed_free_pci(cdev);
270 err1:
271         qed_free_cdev(cdev);
272 err0:
273         return NULL;
274 }
275
276 static void qed_remove(struct qed_dev *cdev)
277 {
278         if (!cdev)
279                 return;
280
281         qed_hw_remove(cdev);
282
283         qed_free_pci(cdev);
284
285         qed_set_power_state(cdev, PCI_D3hot);
286
287         qed_free_cdev(cdev);
288 }
289
290 static void qed_disable_msix(struct qed_dev *cdev)
291 {
292         if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
293                 pci_disable_msix(cdev->pdev);
294                 kfree(cdev->int_params.msix_table);
295         } else if (cdev->int_params.out.int_mode == QED_INT_MODE_MSI) {
296                 pci_disable_msi(cdev->pdev);
297         }
298
299         memset(&cdev->int_params.out, 0, sizeof(struct qed_int_param));
300 }
301
302 static int qed_enable_msix(struct qed_dev *cdev,
303                            struct qed_int_params *int_params)
304 {
305         int i, rc, cnt;
306
307         cnt = int_params->in.num_vectors;
308
309         for (i = 0; i < cnt; i++)
310                 int_params->msix_table[i].entry = i;
311
312         rc = pci_enable_msix_range(cdev->pdev, int_params->msix_table,
313                                    int_params->in.min_msix_cnt, cnt);
314         if (rc < cnt && rc >= int_params->in.min_msix_cnt &&
315             (rc % cdev->num_hwfns)) {
316                 pci_disable_msix(cdev->pdev);
317
318                 /* If fastpath is initialized, we need at least one interrupt
319                  * per hwfn [and the slow path interrupts]. New requested number
320                  * should be a multiple of the number of hwfns.
321                  */
322                 cnt = (rc / cdev->num_hwfns) * cdev->num_hwfns;
323                 DP_NOTICE(cdev,
324                           "Trying to enable MSI-X with less vectors (%d out of %d)\n",
325                           cnt, int_params->in.num_vectors);
326                 rc = pci_enable_msix_exact(cdev->pdev,
327                                            int_params->msix_table, cnt);
328                 if (!rc)
329                         rc = cnt;
330         }
331
332         if (rc > 0) {
333                 /* MSI-x configuration was achieved */
334                 int_params->out.int_mode = QED_INT_MODE_MSIX;
335                 int_params->out.num_vectors = rc;
336                 rc = 0;
337         } else {
338                 DP_NOTICE(cdev,
339                           "Failed to enable MSI-X [Requested %d vectors][rc %d]\n",
340                           cnt, rc);
341         }
342
343         return rc;
344 }
345
346 /* This function outputs the int mode and the number of enabled msix vector */
347 static int qed_set_int_mode(struct qed_dev *cdev, bool force_mode)
348 {
349         struct qed_int_params *int_params = &cdev->int_params;
350         struct msix_entry *tbl;
351         int rc = 0, cnt;
352
353         switch (int_params->in.int_mode) {
354         case QED_INT_MODE_MSIX:
355                 /* Allocate MSIX table */
356                 cnt = int_params->in.num_vectors;
357                 int_params->msix_table = kcalloc(cnt, sizeof(*tbl), GFP_KERNEL);
358                 if (!int_params->msix_table) {
359                         rc = -ENOMEM;
360                         goto out;
361                 }
362
363                 /* Enable MSIX */
364                 rc = qed_enable_msix(cdev, int_params);
365                 if (!rc)
366                         goto out;
367
368                 DP_NOTICE(cdev, "Failed to enable MSI-X\n");
369                 kfree(int_params->msix_table);
370                 if (force_mode)
371                         goto out;
372                 /* Fallthrough */
373
374         case QED_INT_MODE_MSI:
375                 rc = pci_enable_msi(cdev->pdev);
376                 if (!rc) {
377                         int_params->out.int_mode = QED_INT_MODE_MSI;
378                         goto out;
379                 }
380
381                 DP_NOTICE(cdev, "Failed to enable MSI\n");
382                 if (force_mode)
383                         goto out;
384                 /* Fallthrough */
385
386         case QED_INT_MODE_INTA:
387                         int_params->out.int_mode = QED_INT_MODE_INTA;
388                         rc = 0;
389                         goto out;
390         default:
391                 DP_NOTICE(cdev, "Unknown int_mode value %d\n",
392                           int_params->in.int_mode);
393                 rc = -EINVAL;
394         }
395
396 out:
397         cdev->int_coalescing_mode = QED_COAL_MODE_ENABLE;
398
399         return rc;
400 }
401
402 static void qed_simd_handler_config(struct qed_dev *cdev, void *token,
403                                     int index, void(*handler)(void *))
404 {
405         struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
406         int relative_idx = index / cdev->num_hwfns;
407
408         hwfn->simd_proto_handler[relative_idx].func = handler;
409         hwfn->simd_proto_handler[relative_idx].token = token;
410 }
411
412 static void qed_simd_handler_clean(struct qed_dev *cdev, int index)
413 {
414         struct qed_hwfn *hwfn = &cdev->hwfns[index % cdev->num_hwfns];
415         int relative_idx = index / cdev->num_hwfns;
416
417         memset(&hwfn->simd_proto_handler[relative_idx], 0,
418                sizeof(struct qed_simd_fp_handler));
419 }
420
421 static irqreturn_t qed_msix_sp_int(int irq, void *tasklet)
422 {
423         tasklet_schedule((struct tasklet_struct *)tasklet);
424         return IRQ_HANDLED;
425 }
426
427 static irqreturn_t qed_single_int(int irq, void *dev_instance)
428 {
429         struct qed_dev *cdev = (struct qed_dev *)dev_instance;
430         struct qed_hwfn *hwfn;
431         irqreturn_t rc = IRQ_NONE;
432         u64 status;
433         int i, j;
434
435         for (i = 0; i < cdev->num_hwfns; i++) {
436                 status = qed_int_igu_read_sisr_reg(&cdev->hwfns[i]);
437
438                 if (!status)
439                         continue;
440
441                 hwfn = &cdev->hwfns[i];
442
443                 /* Slowpath interrupt */
444                 if (unlikely(status & 0x1)) {
445                         tasklet_schedule(hwfn->sp_dpc);
446                         status &= ~0x1;
447                         rc = IRQ_HANDLED;
448                 }
449
450                 /* Fastpath interrupts */
451                 for (j = 0; j < 64; j++) {
452                         if ((0x2ULL << j) & status) {
453                                 hwfn->simd_proto_handler[j].func(
454                                         hwfn->simd_proto_handler[j].token);
455                                 status &= ~(0x2ULL << j);
456                                 rc = IRQ_HANDLED;
457                         }
458                 }
459
460                 if (unlikely(status))
461                         DP_VERBOSE(hwfn, NETIF_MSG_INTR,
462                                    "got an unknown interrupt status 0x%llx\n",
463                                    status);
464         }
465
466         return rc;
467 }
468
469 static int qed_slowpath_irq_req(struct qed_dev *cdev)
470 {
471         int i = 0, rc = 0;
472
473         if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
474                 /* Request all the slowpath MSI-X vectors */
475                 for (i = 0; i < cdev->num_hwfns; i++) {
476                         snprintf(cdev->hwfns[i].name, NAME_SIZE,
477                                  "sp-%d-%02x:%02x.%02x",
478                                  i, cdev->pdev->bus->number,
479                                  PCI_SLOT(cdev->pdev->devfn),
480                                  cdev->hwfns[i].abs_pf_id);
481
482                         rc = request_irq(cdev->int_params.msix_table[i].vector,
483                                          qed_msix_sp_int, 0,
484                                          cdev->hwfns[i].name,
485                                          cdev->hwfns[i].sp_dpc);
486                         if (rc)
487                                 break;
488
489                         DP_VERBOSE(&cdev->hwfns[i],
490                                    (NETIF_MSG_INTR | QED_MSG_SP),
491                                    "Requested slowpath MSI-X\n");
492                 }
493
494                 if (i != cdev->num_hwfns) {
495                         /* Free already request MSI-X vectors */
496                         for (i--; i >= 0; i--) {
497                                 unsigned int vec =
498                                         cdev->int_params.msix_table[i].vector;
499                                 synchronize_irq(vec);
500                                 free_irq(cdev->int_params.msix_table[i].vector,
501                                          cdev->hwfns[i].sp_dpc);
502                         }
503                 }
504         } else {
505                 unsigned long flags = 0;
506
507                 snprintf(cdev->name, NAME_SIZE, "%02x:%02x.%02x",
508                          cdev->pdev->bus->number, PCI_SLOT(cdev->pdev->devfn),
509                          PCI_FUNC(cdev->pdev->devfn));
510
511                 if (cdev->int_params.out.int_mode == QED_INT_MODE_INTA)
512                         flags |= IRQF_SHARED;
513
514                 rc = request_irq(cdev->pdev->irq, qed_single_int,
515                                  flags, cdev->name, cdev);
516         }
517
518         return rc;
519 }
520
521 static void qed_slowpath_irq_free(struct qed_dev *cdev)
522 {
523         int i;
524
525         if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
526                 for_each_hwfn(cdev, i) {
527                         synchronize_irq(cdev->int_params.msix_table[i].vector);
528                         free_irq(cdev->int_params.msix_table[i].vector,
529                                  cdev->hwfns[i].sp_dpc);
530                 }
531         } else {
532                 free_irq(cdev->pdev->irq, cdev);
533         }
534 }
535
536 static int qed_nic_stop(struct qed_dev *cdev)
537 {
538         int i, rc;
539
540         rc = qed_hw_stop(cdev);
541
542         for (i = 0; i < cdev->num_hwfns; i++) {
543                 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
544
545                 if (p_hwfn->b_sp_dpc_enabled) {
546                         tasklet_disable(p_hwfn->sp_dpc);
547                         p_hwfn->b_sp_dpc_enabled = false;
548                         DP_VERBOSE(cdev, NETIF_MSG_IFDOWN,
549                                    "Disabled sp taskelt [hwfn %d] at %p\n",
550                                    i, p_hwfn->sp_dpc);
551                 }
552         }
553
554         return rc;
555 }
556
557 static int qed_nic_reset(struct qed_dev *cdev)
558 {
559         int rc;
560
561         rc = qed_hw_reset(cdev);
562         if (rc)
563                 return rc;
564
565         qed_resc_free(cdev);
566
567         return 0;
568 }
569
570 static int qed_nic_setup(struct qed_dev *cdev)
571 {
572         int rc;
573
574         rc = qed_resc_alloc(cdev);
575         if (rc)
576                 return rc;
577
578         DP_INFO(cdev, "Allocated qed resources\n");
579
580         qed_resc_setup(cdev);
581
582         return rc;
583 }
584
585 static int qed_set_int_fp(struct qed_dev *cdev, u16 cnt)
586 {
587         int limit = 0;
588
589         /* Mark the fastpath as free/used */
590         cdev->int_params.fp_initialized = cnt ? true : false;
591
592         if (cdev->int_params.out.int_mode != QED_INT_MODE_MSIX)
593                 limit = cdev->num_hwfns * 63;
594         else if (cdev->int_params.fp_msix_cnt)
595                 limit = cdev->int_params.fp_msix_cnt;
596
597         if (!limit)
598                 return -ENOMEM;
599
600         return min_t(int, cnt, limit);
601 }
602
603 static int qed_get_int_fp(struct qed_dev *cdev, struct qed_int_info *info)
604 {
605         memset(info, 0, sizeof(struct qed_int_info));
606
607         if (!cdev->int_params.fp_initialized) {
608                 DP_INFO(cdev,
609                         "Protocol driver requested interrupt information, but its support is not yet configured\n");
610                 return -EINVAL;
611         }
612
613         /* Need to expose only MSI-X information; Single IRQ is handled solely
614          * by qed.
615          */
616         if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
617                 int msix_base = cdev->int_params.fp_msix_base;
618
619                 info->msix_cnt = cdev->int_params.fp_msix_cnt;
620                 info->msix = &cdev->int_params.msix_table[msix_base];
621         }
622
623         return 0;
624 }
625
626 static int qed_slowpath_setup_int(struct qed_dev *cdev,
627                                   enum qed_int_mode int_mode)
628 {
629         int rc, i;
630         u8 num_vectors = 0;
631
632         memset(&cdev->int_params, 0, sizeof(struct qed_int_params));
633
634         cdev->int_params.in.int_mode = int_mode;
635         for_each_hwfn(cdev, i)
636                 num_vectors +=  qed_int_get_num_sbs(&cdev->hwfns[i], NULL) + 1;
637         cdev->int_params.in.num_vectors = num_vectors;
638
639         /* We want a minimum of one slowpath and one fastpath vector per hwfn */
640         cdev->int_params.in.min_msix_cnt = cdev->num_hwfns * 2;
641
642         rc = qed_set_int_mode(cdev, false);
643         if (rc)  {
644                 DP_ERR(cdev, "qed_slowpath_setup_int ERR\n");
645                 return rc;
646         }
647
648         cdev->int_params.fp_msix_base = cdev->num_hwfns;
649         cdev->int_params.fp_msix_cnt = cdev->int_params.out.num_vectors -
650                                        cdev->num_hwfns;
651
652         return 0;
653 }
654
655 u32 qed_unzip_data(struct qed_hwfn *p_hwfn, u32 input_len,
656                    u8 *input_buf, u32 max_size, u8 *unzip_buf)
657 {
658         int rc;
659
660         p_hwfn->stream->next_in = input_buf;
661         p_hwfn->stream->avail_in = input_len;
662         p_hwfn->stream->next_out = unzip_buf;
663         p_hwfn->stream->avail_out = max_size;
664
665         rc = zlib_inflateInit2(p_hwfn->stream, MAX_WBITS);
666
667         if (rc != Z_OK) {
668                 DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "zlib init failed, rc = %d\n",
669                            rc);
670                 return 0;
671         }
672
673         rc = zlib_inflate(p_hwfn->stream, Z_FINISH);
674         zlib_inflateEnd(p_hwfn->stream);
675
676         if (rc != Z_OK && rc != Z_STREAM_END) {
677                 DP_VERBOSE(p_hwfn, NETIF_MSG_DRV, "FW unzip error: %s, rc=%d\n",
678                            p_hwfn->stream->msg, rc);
679                 return 0;
680         }
681
682         return p_hwfn->stream->total_out / 4;
683 }
684
685 static int qed_alloc_stream_mem(struct qed_dev *cdev)
686 {
687         int i;
688         void *workspace;
689
690         for_each_hwfn(cdev, i) {
691                 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
692
693                 p_hwfn->stream = kzalloc(sizeof(*p_hwfn->stream), GFP_KERNEL);
694                 if (!p_hwfn->stream)
695                         return -ENOMEM;
696
697                 workspace = vzalloc(zlib_inflate_workspacesize());
698                 if (!workspace)
699                         return -ENOMEM;
700                 p_hwfn->stream->workspace = workspace;
701         }
702
703         return 0;
704 }
705
706 static void qed_free_stream_mem(struct qed_dev *cdev)
707 {
708         int i;
709
710         for_each_hwfn(cdev, i) {
711                 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
712
713                 if (!p_hwfn->stream)
714                         return;
715
716                 vfree(p_hwfn->stream->workspace);
717                 kfree(p_hwfn->stream);
718         }
719 }
720
721 static void qed_update_pf_params(struct qed_dev *cdev,
722                                  struct qed_pf_params *params)
723 {
724         int i;
725
726         for (i = 0; i < cdev->num_hwfns; i++) {
727                 struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
728
729                 p_hwfn->pf_params = *params;
730         }
731 }
732
733 static int qed_slowpath_start(struct qed_dev *cdev,
734                               struct qed_slowpath_params *params)
735 {
736         struct qed_mcp_drv_version drv_version;
737         const u8 *data = NULL;
738         struct qed_hwfn *hwfn;
739         int rc;
740
741         rc = request_firmware(&cdev->firmware, QED_FW_FILE_NAME,
742                               &cdev->pdev->dev);
743         if (rc) {
744                 DP_NOTICE(cdev,
745                           "Failed to find fw file - /lib/firmware/%s\n",
746                           QED_FW_FILE_NAME);
747                 goto err;
748         }
749
750         rc = qed_nic_setup(cdev);
751         if (rc)
752                 goto err;
753
754         rc = qed_slowpath_setup_int(cdev, params->int_mode);
755         if (rc)
756                 goto err1;
757
758         /* Request the slowpath IRQ */
759         rc = qed_slowpath_irq_req(cdev);
760         if (rc)
761                 goto err2;
762
763         /* Allocate stream for unzipping */
764         rc = qed_alloc_stream_mem(cdev);
765         if (rc) {
766                 DP_NOTICE(cdev, "Failed to allocate stream memory\n");
767                 goto err3;
768         }
769
770         /* Start the slowpath */
771         data = cdev->firmware->data;
772
773         rc = qed_hw_init(cdev, true, cdev->int_params.out.int_mode,
774                          true, data);
775         if (rc)
776                 goto err3;
777
778         DP_INFO(cdev,
779                 "HW initialization and function start completed successfully\n");
780
781         hwfn = QED_LEADING_HWFN(cdev);
782         drv_version.version = (params->drv_major << 24) |
783                               (params->drv_minor << 16) |
784                               (params->drv_rev << 8) |
785                               (params->drv_eng);
786         strlcpy(drv_version.name, params->name,
787                 MCP_DRV_VER_STR_SIZE - 4);
788         rc = qed_mcp_send_drv_version(hwfn, hwfn->p_main_ptt,
789                                       &drv_version);
790         if (rc) {
791                 DP_NOTICE(cdev, "Failed sending drv version command\n");
792                 return rc;
793         }
794
795         return 0;
796
797 err3:
798         qed_free_stream_mem(cdev);
799         qed_slowpath_irq_free(cdev);
800 err2:
801         qed_disable_msix(cdev);
802 err1:
803         qed_resc_free(cdev);
804 err:
805         release_firmware(cdev->firmware);
806
807         return rc;
808 }
809
810 static int qed_slowpath_stop(struct qed_dev *cdev)
811 {
812         if (!cdev)
813                 return -ENODEV;
814
815         qed_free_stream_mem(cdev);
816
817         qed_nic_stop(cdev);
818         qed_slowpath_irq_free(cdev);
819
820         qed_disable_msix(cdev);
821         qed_nic_reset(cdev);
822
823         release_firmware(cdev->firmware);
824
825         return 0;
826 }
827
828 static void qed_set_id(struct qed_dev *cdev, char name[NAME_SIZE],
829                        char ver_str[VER_SIZE])
830 {
831         int i;
832
833         memcpy(cdev->name, name, NAME_SIZE);
834         for_each_hwfn(cdev, i)
835                 snprintf(cdev->hwfns[i].name, NAME_SIZE, "%s-%d", name, i);
836
837         memcpy(cdev->ver_str, ver_str, VER_SIZE);
838         cdev->drv_type = DRV_ID_DRV_TYPE_LINUX;
839 }
840
841 static u32 qed_sb_init(struct qed_dev *cdev,
842                        struct qed_sb_info *sb_info,
843                        void *sb_virt_addr,
844                        dma_addr_t sb_phy_addr, u16 sb_id,
845                        enum qed_sb_type type)
846 {
847         struct qed_hwfn *p_hwfn;
848         int hwfn_index;
849         u16 rel_sb_id;
850         u8 n_hwfns;
851         u32 rc;
852
853         /* RoCE uses single engine and CMT uses two engines. When using both
854          * we force only a single engine. Storage uses only engine 0 too.
855          */
856         if (type == QED_SB_TYPE_L2_QUEUE)
857                 n_hwfns = cdev->num_hwfns;
858         else
859                 n_hwfns = 1;
860
861         hwfn_index = sb_id % n_hwfns;
862         p_hwfn = &cdev->hwfns[hwfn_index];
863         rel_sb_id = sb_id / n_hwfns;
864
865         DP_VERBOSE(cdev, NETIF_MSG_INTR,
866                    "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
867                    hwfn_index, rel_sb_id, sb_id);
868
869         rc = qed_int_sb_init(p_hwfn, p_hwfn->p_main_ptt, sb_info,
870                              sb_virt_addr, sb_phy_addr, rel_sb_id);
871
872         return rc;
873 }
874
875 static u32 qed_sb_release(struct qed_dev *cdev,
876                           struct qed_sb_info *sb_info,
877                           u16 sb_id)
878 {
879         struct qed_hwfn *p_hwfn;
880         int hwfn_index;
881         u16 rel_sb_id;
882         u32 rc;
883
884         hwfn_index = sb_id % cdev->num_hwfns;
885         p_hwfn = &cdev->hwfns[hwfn_index];
886         rel_sb_id = sb_id / cdev->num_hwfns;
887
888         DP_VERBOSE(cdev, NETIF_MSG_INTR,
889                    "hwfn [%d] <--[init]-- SB %04x [0x%04x upper]\n",
890                    hwfn_index, rel_sb_id, sb_id);
891
892         rc = qed_int_sb_release(p_hwfn, sb_info, rel_sb_id);
893
894         return rc;
895 }
896
897 static int qed_drain(struct qed_dev *cdev)
898 {
899         struct qed_hwfn *hwfn;
900         struct qed_ptt *ptt;
901         int i, rc;
902
903         for_each_hwfn(cdev, i) {
904                 hwfn = &cdev->hwfns[i];
905                 ptt = qed_ptt_acquire(hwfn);
906                 if (!ptt) {
907                         DP_NOTICE(hwfn, "Failed to drain NIG; No PTT\n");
908                         return -EBUSY;
909                 }
910                 rc = qed_mcp_drain(hwfn, ptt);
911                 if (rc)
912                         return rc;
913                 qed_ptt_release(hwfn, ptt);
914         }
915
916         return 0;
917 }
918
919 const struct qed_common_ops qed_common_ops_pass = {
920         .probe = &qed_probe,
921         .remove = &qed_remove,
922         .set_power_state = &qed_set_power_state,
923         .set_id = &qed_set_id,
924         .update_pf_params = &qed_update_pf_params,
925         .slowpath_start = &qed_slowpath_start,
926         .slowpath_stop = &qed_slowpath_stop,
927         .set_fp_int = &qed_set_int_fp,
928         .get_fp_int = &qed_get_int_fp,
929         .sb_init = &qed_sb_init,
930         .sb_release = &qed_sb_release,
931         .simd_handler_config = &qed_simd_handler_config,
932         .simd_handler_clean = &qed_simd_handler_clean,
933         .drain = &qed_drain,
934         .update_msglvl = &qed_init_dp,
935         .chain_alloc = &qed_chain_alloc,
936         .chain_free = &qed_chain_free,
937 };
938
939 u32 qed_get_protocol_version(enum qed_protocol protocol)
940 {
941         switch (protocol) {
942         case QED_PROTOCOL_ETH:
943                 return QED_ETH_INTERFACE_VERSION;
944         default:
945                 return 0;
946         }
947 }
948 EXPORT_SYMBOL(qed_get_protocol_version);