1 // SPDX-License-Identifier: GPL-2.0-only
3 * Intel MIC Platform Software Stack (MPSS)
5 * Copyright(c) 2013 Intel Corporation.
7 * Intel MIC Host driver.
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/poll.h>
14 #include <linux/mic_common.h>
15 #include "../common/mic_dev.h"
16 #include "mic_device.h"
20 static const char mic_driver_name
[] = "mic";
22 static const struct pci_device_id mic_pci_tbl
[] = {
23 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2250
)},
24 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2251
)},
25 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2252
)},
26 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2253
)},
27 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2254
)},
28 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2255
)},
29 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2256
)},
30 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2257
)},
31 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2258
)},
32 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2259
)},
33 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225a
)},
34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225b
)},
35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225c
)},
36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225d
)},
37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225e
)},
39 /* required last entry */
43 MODULE_DEVICE_TABLE(pci
, mic_pci_tbl
);
45 /* ID allocator for MIC devices */
46 static struct ida g_mic_ida
;
48 /* Initialize the device page */
49 static int mic_dp_init(struct mic_device
*mdev
)
51 mdev
->dp
= kzalloc(MIC_DP_SIZE
, GFP_KERNEL
);
55 mdev
->dp_dma_addr
= mic_map_single(mdev
,
56 mdev
->dp
, MIC_DP_SIZE
);
57 if (mic_map_error(mdev
->dp_dma_addr
)) {
59 dev_err(&mdev
->pdev
->dev
, "%s %d err %d\n",
60 __func__
, __LINE__
, -ENOMEM
);
63 mdev
->ops
->write_spad(mdev
, MIC_DPLO_SPAD
, mdev
->dp_dma_addr
);
64 mdev
->ops
->write_spad(mdev
, MIC_DPHI_SPAD
, mdev
->dp_dma_addr
>> 32);
68 /* Uninitialize the device page */
69 static void mic_dp_uninit(struct mic_device
*mdev
)
71 mic_unmap_single(mdev
, mdev
->dp_dma_addr
, MIC_DP_SIZE
);
76 * mic_ops_init: Initialize HW specific operation tables.
78 * @mdev: pointer to mic_device instance
82 static void mic_ops_init(struct mic_device
*mdev
)
84 switch (mdev
->family
) {
86 mdev
->ops
= &mic_x100_ops
;
87 mdev
->intr_ops
= &mic_x100_intr_ops
;
88 mdev
->smpt_ops
= &mic_x100_smpt_ops
;
96 * mic_get_family - Determine hardware family to which this MIC belongs.
98 * @pdev: The pci device structure
102 static enum mic_hw_family
mic_get_family(struct pci_dev
*pdev
)
104 enum mic_hw_family family
;
106 switch (pdev
->device
) {
107 case MIC_X100_PCI_DEVICE_2250
:
108 case MIC_X100_PCI_DEVICE_2251
:
109 case MIC_X100_PCI_DEVICE_2252
:
110 case MIC_X100_PCI_DEVICE_2253
:
111 case MIC_X100_PCI_DEVICE_2254
:
112 case MIC_X100_PCI_DEVICE_2255
:
113 case MIC_X100_PCI_DEVICE_2256
:
114 case MIC_X100_PCI_DEVICE_2257
:
115 case MIC_X100_PCI_DEVICE_2258
:
116 case MIC_X100_PCI_DEVICE_2259
:
117 case MIC_X100_PCI_DEVICE_225a
:
118 case MIC_X100_PCI_DEVICE_225b
:
119 case MIC_X100_PCI_DEVICE_225c
:
120 case MIC_X100_PCI_DEVICE_225d
:
121 case MIC_X100_PCI_DEVICE_225e
:
122 family
= MIC_FAMILY_X100
;
125 family
= MIC_FAMILY_UNKNOWN
;
132 * mic_device_init - Allocates and initializes the MIC device structure
134 * @mdev: pointer to mic_device instance
135 * @pdev: The pci device structure
140 mic_device_init(struct mic_device
*mdev
, struct pci_dev
*pdev
)
143 mdev
->family
= mic_get_family(pdev
);
144 mdev
->stepping
= pdev
->revision
;
146 mutex_init(&mdev
->mic_mutex
);
147 mdev
->irq_info
.next_avail_src
= 0;
151 * mic_probe - Device Initialization Routine
153 * @pdev: PCI device structure
154 * @ent: entry in mic_pci_tbl
156 * returns 0 on success, < 0 on failure.
158 static int mic_probe(struct pci_dev
*pdev
,
159 const struct pci_device_id
*ent
)
162 struct mic_device
*mdev
;
164 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
167 dev_err(&pdev
->dev
, "mdev kmalloc failed rc %d\n", rc
);
168 goto mdev_alloc_fail
;
170 mdev
->id
= ida_simple_get(&g_mic_ida
, 0, MIC_MAX_NUM_DEVS
, GFP_KERNEL
);
173 dev_err(&pdev
->dev
, "ida_simple_get failed rc %d\n", rc
);
177 mic_device_init(mdev
, pdev
);
179 rc
= pci_enable_device(pdev
);
181 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
185 pci_set_master(pdev
);
187 rc
= pci_request_regions(pdev
, mic_driver_name
);
189 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
193 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
195 dev_err(&pdev
->dev
, "Cannot set DMA mask\n");
196 goto release_regions
;
199 mdev
->mmio
.pa
= pci_resource_start(pdev
, mdev
->ops
->mmio_bar
);
200 mdev
->mmio
.len
= pci_resource_len(pdev
, mdev
->ops
->mmio_bar
);
201 mdev
->mmio
.va
= pci_ioremap_bar(pdev
, mdev
->ops
->mmio_bar
);
202 if (!mdev
->mmio
.va
) {
203 dev_err(&pdev
->dev
, "Cannot remap MMIO BAR\n");
205 goto release_regions
;
208 mdev
->aper
.pa
= pci_resource_start(pdev
, mdev
->ops
->aper_bar
);
209 mdev
->aper
.len
= pci_resource_len(pdev
, mdev
->ops
->aper_bar
);
210 mdev
->aper
.va
= ioremap_wc(mdev
->aper
.pa
, mdev
->aper
.len
);
211 if (!mdev
->aper
.va
) {
212 dev_err(&pdev
->dev
, "Cannot remap Aperture BAR\n");
217 mdev
->intr_ops
->intr_init(mdev
);
218 rc
= mic_setup_interrupts(mdev
, pdev
);
220 dev_err(&pdev
->dev
, "mic_setup_interrupts failed %d\n", rc
);
223 rc
= mic_smpt_init(mdev
);
225 dev_err(&pdev
->dev
, "smpt_init failed %d\n", rc
);
226 goto free_interrupts
;
229 pci_set_drvdata(pdev
, mdev
);
231 rc
= mic_dp_init(mdev
);
233 dev_err(&pdev
->dev
, "mic_dp_init failed rc %d\n", rc
);
236 mic_bootparam_init(mdev
);
237 mic_create_debug_dir(mdev
);
239 mdev
->cosm_dev
= cosm_register_device(&mdev
->pdev
->dev
, &cosm_hw_ops
);
240 if (IS_ERR(mdev
->cosm_dev
)) {
241 rc
= PTR_ERR(mdev
->cosm_dev
);
242 dev_err(&pdev
->dev
, "cosm_add_device failed rc %d\n", rc
);
243 goto cleanup_debug_dir
;
247 mic_delete_debug_dir(mdev
);
250 mic_smpt_uninit(mdev
);
252 mic_free_interrupts(mdev
, pdev
);
254 iounmap(mdev
->aper
.va
);
256 iounmap(mdev
->mmio
.va
);
258 pci_release_regions(pdev
);
260 pci_disable_device(pdev
);
262 ida_simple_remove(&g_mic_ida
, mdev
->id
);
266 dev_err(&pdev
->dev
, "Probe failed rc %d\n", rc
);
271 * mic_remove - Device Removal Routine
272 * mic_remove is called by the PCI subsystem to alert the driver
273 * that it should release a PCI device.
275 * @pdev: PCI device structure
277 static void mic_remove(struct pci_dev
*pdev
)
279 struct mic_device
*mdev
;
281 mdev
= pci_get_drvdata(pdev
);
285 cosm_unregister_device(mdev
->cosm_dev
);
286 mic_delete_debug_dir(mdev
);
288 mic_smpt_uninit(mdev
);
289 mic_free_interrupts(mdev
, pdev
);
290 iounmap(mdev
->aper
.va
);
291 iounmap(mdev
->mmio
.va
);
292 pci_release_regions(pdev
);
293 pci_disable_device(pdev
);
294 ida_simple_remove(&g_mic_ida
, mdev
->id
);
298 static struct pci_driver mic_driver
= {
299 .name
= mic_driver_name
,
300 .id_table
= mic_pci_tbl
,
305 static int __init
mic_init(void)
309 request_module("mic_x100_dma");
311 ida_init(&g_mic_ida
);
312 ret
= pci_register_driver(&mic_driver
);
314 pr_err("pci_register_driver failed ret %d\n", ret
);
315 goto cleanup_debugfs
;
319 ida_destroy(&g_mic_ida
);
324 static void __exit
mic_exit(void)
326 pci_unregister_driver(&mic_driver
);
327 ida_destroy(&g_mic_ida
);
331 module_init(mic_init
);
332 module_exit(mic_exit
);
334 MODULE_AUTHOR("Intel Corporation");
335 MODULE_DESCRIPTION("Intel(R) MIC X100 Host driver");
336 MODULE_LICENSE("GPL v2");