2 * Intel MIC Platform Software Stack (MPSS)
4 * Copyright(c) 2013 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
18 * Intel MIC Host driver.
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/poll.h>
25 #include <linux/mic_common.h>
26 #include "../common/mic_dev.h"
27 #include "mic_device.h"
31 static const char mic_driver_name
[] = "mic";
33 static const struct pci_device_id mic_pci_tbl
[] = {
34 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2250
)},
35 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2251
)},
36 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2252
)},
37 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2253
)},
38 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2254
)},
39 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2255
)},
40 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2256
)},
41 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2257
)},
42 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2258
)},
43 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_2259
)},
44 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225a
)},
45 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225b
)},
46 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225c
)},
47 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225d
)},
48 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MIC_X100_PCI_DEVICE_225e
)},
50 /* required last entry */
54 MODULE_DEVICE_TABLE(pci
, mic_pci_tbl
);
56 /* ID allocator for MIC devices */
57 static struct ida g_mic_ida
;
59 /* Initialize the device page */
60 static int mic_dp_init(struct mic_device
*mdev
)
62 mdev
->dp
= kzalloc(MIC_DP_SIZE
, GFP_KERNEL
);
66 mdev
->dp_dma_addr
= mic_map_single(mdev
,
67 mdev
->dp
, MIC_DP_SIZE
);
68 if (mic_map_error(mdev
->dp_dma_addr
)) {
70 dev_err(&mdev
->pdev
->dev
, "%s %d err %d\n",
71 __func__
, __LINE__
, -ENOMEM
);
74 mdev
->ops
->write_spad(mdev
, MIC_DPLO_SPAD
, mdev
->dp_dma_addr
);
75 mdev
->ops
->write_spad(mdev
, MIC_DPHI_SPAD
, mdev
->dp_dma_addr
>> 32);
79 /* Uninitialize the device page */
80 static void mic_dp_uninit(struct mic_device
*mdev
)
82 mic_unmap_single(mdev
, mdev
->dp_dma_addr
, MIC_DP_SIZE
);
87 * mic_ops_init: Initialize HW specific operation tables.
89 * @mdev: pointer to mic_device instance
93 static void mic_ops_init(struct mic_device
*mdev
)
95 switch (mdev
->family
) {
97 mdev
->ops
= &mic_x100_ops
;
98 mdev
->intr_ops
= &mic_x100_intr_ops
;
99 mdev
->smpt_ops
= &mic_x100_smpt_ops
;
107 * mic_get_family - Determine hardware family to which this MIC belongs.
109 * @pdev: The pci device structure
113 static enum mic_hw_family
mic_get_family(struct pci_dev
*pdev
)
115 enum mic_hw_family family
;
117 switch (pdev
->device
) {
118 case MIC_X100_PCI_DEVICE_2250
:
119 case MIC_X100_PCI_DEVICE_2251
:
120 case MIC_X100_PCI_DEVICE_2252
:
121 case MIC_X100_PCI_DEVICE_2253
:
122 case MIC_X100_PCI_DEVICE_2254
:
123 case MIC_X100_PCI_DEVICE_2255
:
124 case MIC_X100_PCI_DEVICE_2256
:
125 case MIC_X100_PCI_DEVICE_2257
:
126 case MIC_X100_PCI_DEVICE_2258
:
127 case MIC_X100_PCI_DEVICE_2259
:
128 case MIC_X100_PCI_DEVICE_225a
:
129 case MIC_X100_PCI_DEVICE_225b
:
130 case MIC_X100_PCI_DEVICE_225c
:
131 case MIC_X100_PCI_DEVICE_225d
:
132 case MIC_X100_PCI_DEVICE_225e
:
133 family
= MIC_FAMILY_X100
;
136 family
= MIC_FAMILY_UNKNOWN
;
143 * mic_device_init - Allocates and initializes the MIC device structure
145 * @mdev: pointer to mic_device instance
146 * @pdev: The pci device structure
151 mic_device_init(struct mic_device
*mdev
, struct pci_dev
*pdev
)
154 mdev
->family
= mic_get_family(pdev
);
155 mdev
->stepping
= pdev
->revision
;
157 mutex_init(&mdev
->mic_mutex
);
158 mdev
->irq_info
.next_avail_src
= 0;
162 * mic_probe - Device Initialization Routine
164 * @pdev: PCI device structure
165 * @ent: entry in mic_pci_tbl
167 * returns 0 on success, < 0 on failure.
169 static int mic_probe(struct pci_dev
*pdev
,
170 const struct pci_device_id
*ent
)
173 struct mic_device
*mdev
;
175 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
178 dev_err(&pdev
->dev
, "mdev kmalloc failed rc %d\n", rc
);
179 goto mdev_alloc_fail
;
181 mdev
->id
= ida_simple_get(&g_mic_ida
, 0, MIC_MAX_NUM_DEVS
, GFP_KERNEL
);
184 dev_err(&pdev
->dev
, "ida_simple_get failed rc %d\n", rc
);
188 mic_device_init(mdev
, pdev
);
190 rc
= pci_enable_device(pdev
);
192 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
196 pci_set_master(pdev
);
198 rc
= pci_request_regions(pdev
, mic_driver_name
);
200 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
204 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
206 dev_err(&pdev
->dev
, "Cannot set DMA mask\n");
207 goto release_regions
;
210 mdev
->mmio
.pa
= pci_resource_start(pdev
, mdev
->ops
->mmio_bar
);
211 mdev
->mmio
.len
= pci_resource_len(pdev
, mdev
->ops
->mmio_bar
);
212 mdev
->mmio
.va
= pci_ioremap_bar(pdev
, mdev
->ops
->mmio_bar
);
213 if (!mdev
->mmio
.va
) {
214 dev_err(&pdev
->dev
, "Cannot remap MMIO BAR\n");
216 goto release_regions
;
219 mdev
->aper
.pa
= pci_resource_start(pdev
, mdev
->ops
->aper_bar
);
220 mdev
->aper
.len
= pci_resource_len(pdev
, mdev
->ops
->aper_bar
);
221 mdev
->aper
.va
= ioremap_wc(mdev
->aper
.pa
, mdev
->aper
.len
);
222 if (!mdev
->aper
.va
) {
223 dev_err(&pdev
->dev
, "Cannot remap Aperture BAR\n");
228 mdev
->intr_ops
->intr_init(mdev
);
229 rc
= mic_setup_interrupts(mdev
, pdev
);
231 dev_err(&pdev
->dev
, "mic_setup_interrupts failed %d\n", rc
);
234 rc
= mic_smpt_init(mdev
);
236 dev_err(&pdev
->dev
, "smpt_init failed %d\n", rc
);
237 goto free_interrupts
;
240 pci_set_drvdata(pdev
, mdev
);
242 rc
= mic_dp_init(mdev
);
244 dev_err(&pdev
->dev
, "mic_dp_init failed rc %d\n", rc
);
247 mic_bootparam_init(mdev
);
248 mic_create_debug_dir(mdev
);
250 mdev
->cosm_dev
= cosm_register_device(&mdev
->pdev
->dev
, &cosm_hw_ops
);
251 if (IS_ERR(mdev
->cosm_dev
)) {
252 rc
= PTR_ERR(mdev
->cosm_dev
);
253 dev_err(&pdev
->dev
, "cosm_add_device failed rc %d\n", rc
);
254 goto cleanup_debug_dir
;
258 mic_delete_debug_dir(mdev
);
261 mic_smpt_uninit(mdev
);
263 mic_free_interrupts(mdev
, pdev
);
265 iounmap(mdev
->aper
.va
);
267 iounmap(mdev
->mmio
.va
);
269 pci_release_regions(pdev
);
271 pci_disable_device(pdev
);
273 ida_simple_remove(&g_mic_ida
, mdev
->id
);
277 dev_err(&pdev
->dev
, "Probe failed rc %d\n", rc
);
282 * mic_remove - Device Removal Routine
283 * mic_remove is called by the PCI subsystem to alert the driver
284 * that it should release a PCI device.
286 * @pdev: PCI device structure
288 static void mic_remove(struct pci_dev
*pdev
)
290 struct mic_device
*mdev
;
292 mdev
= pci_get_drvdata(pdev
);
296 cosm_unregister_device(mdev
->cosm_dev
);
297 mic_delete_debug_dir(mdev
);
299 mic_smpt_uninit(mdev
);
300 mic_free_interrupts(mdev
, pdev
);
301 iounmap(mdev
->aper
.va
);
302 iounmap(mdev
->mmio
.va
);
303 pci_release_regions(pdev
);
304 pci_disable_device(pdev
);
305 ida_simple_remove(&g_mic_ida
, mdev
->id
);
309 static struct pci_driver mic_driver
= {
310 .name
= mic_driver_name
,
311 .id_table
= mic_pci_tbl
,
316 static int __init
mic_init(void)
320 request_module("mic_x100_dma");
322 ida_init(&g_mic_ida
);
323 ret
= pci_register_driver(&mic_driver
);
325 pr_err("pci_register_driver failed ret %d\n", ret
);
326 goto cleanup_debugfs
;
330 ida_destroy(&g_mic_ida
);
335 static void __exit
mic_exit(void)
337 pci_unregister_driver(&mic_driver
);
338 ida_destroy(&g_mic_ida
);
342 module_init(mic_init
);
343 module_exit(mic_exit
);
345 MODULE_AUTHOR("Intel Corporation");
346 MODULE_DESCRIPTION("Intel(R) MIC X100 Host driver");
347 MODULE_LICENSE("GPL v2");