1 /*
2 * Intel MIC Platform Software Stack (MPSS)
3 *
4 * Copyright(c) 2013 Intel Corporation.
5 *
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.
9 *
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.
14 *
15 * The full GNU General Public License is included in this distribution in
16 * the file called "COPYING".
17 *
18 * Intel MIC Host driver.
19 *
20 * Global TODO's across the driver to be added after initial base
21 * patches are accepted upstream:
22 * 1) Enable DMA support.
23 * 2) Enable per vring interrupt support.
24 */
25 #include <linux/fs.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/poll.h>
29 #include <linux/suspend.h>
30
31 #include <linux/mic_common.h>
32 #include "../common/mic_dev.h"
33 #include "mic_device.h"
34 #include "mic_x100.h"
35 #include "mic_smpt.h"
36 #include "mic_fops.h"
37 #include "mic_virtio.h"
38
39 static const char mic_driver_name[] = "mic";
40
41 static const struct pci_device_id mic_pci_tbl[] = {
42 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2250)},
43 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2251)},
44 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2252)},
45 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2253)},
46 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2254)},
47 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2255)},
48 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2256)},
49 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2257)},
50 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2258)},
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2259)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225a)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225b)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225c)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225d)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225e)},
57
58 /* required last entry */
59 { 0, }
60 };
61
62 MODULE_DEVICE_TABLE(pci, mic_pci_tbl);
63
64 /* ID allocator for MIC devices */
65 static struct ida g_mic_ida;
66 /* Class of MIC devices for sysfs accessibility. */
67 static struct class *g_mic_class;
68 /* Base device node number for MIC devices */
69 static dev_t g_mic_devno;
70
71 static const struct file_operations mic_fops = {
72 .open = mic_open,
73 .release = mic_release,
74 .unlocked_ioctl = mic_ioctl,
75 .poll = mic_poll,
76 .mmap = mic_mmap,
77 .owner = THIS_MODULE,
78 };
79
80 /* Initialize the device page */
mic_dp_init(struct mic_device * mdev)81 static int mic_dp_init(struct mic_device *mdev)
82 {
83 mdev->dp = kzalloc(MIC_DP_SIZE, GFP_KERNEL);
84 if (!mdev->dp) {
85 dev_err(mdev->sdev->parent, "%s %d err %d\n",
86 __func__, __LINE__, -ENOMEM);
87 return -ENOMEM;
88 }
89
90 mdev->dp_dma_addr = mic_map_single(mdev,
91 mdev->dp, MIC_DP_SIZE);
92 if (mic_map_error(mdev->dp_dma_addr)) {
93 kfree(mdev->dp);
94 dev_err(mdev->sdev->parent, "%s %d err %d\n",
95 __func__, __LINE__, -ENOMEM);
96 return -ENOMEM;
97 }
98 mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
99 mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
100 return 0;
101 }
102
103 /* Uninitialize the device page */
mic_dp_uninit(struct mic_device * mdev)104 static void mic_dp_uninit(struct mic_device *mdev)
105 {
106 mic_unmap_single(mdev, mdev->dp_dma_addr, MIC_DP_SIZE);
107 kfree(mdev->dp);
108 }
109
110 /**
111 * mic_shutdown_db - Shutdown doorbell interrupt handler.
112 */
mic_shutdown_db(int irq,void * data)113 static irqreturn_t mic_shutdown_db(int irq, void *data)
114 {
115 struct mic_device *mdev = data;
116 struct mic_bootparam *bootparam = mdev->dp;
117
118 mdev->ops->intr_workarounds(mdev);
119
120 switch (bootparam->shutdown_status) {
121 case MIC_HALTED:
122 case MIC_POWER_OFF:
123 case MIC_RESTART:
124 /* Fall through */
125 case MIC_CRASHED:
126 schedule_work(&mdev->shutdown_work);
127 break;
128 default:
129 break;
130 };
131 return IRQ_HANDLED;
132 }
133
134 /**
135 * mic_ops_init: Initialize HW specific operation tables.
136 *
137 * @mdev: pointer to mic_device instance
138 *
139 * returns none.
140 */
mic_ops_init(struct mic_device * mdev)141 static void mic_ops_init(struct mic_device *mdev)
142 {
143 switch (mdev->family) {
144 case MIC_FAMILY_X100:
145 mdev->ops = &mic_x100_ops;
146 mdev->intr_ops = &mic_x100_intr_ops;
147 mdev->smpt_ops = &mic_x100_smpt_ops;
148 break;
149 default:
150 break;
151 }
152 }
153
154 /**
155 * mic_get_family - Determine hardware family to which this MIC belongs.
156 *
157 * @pdev: The pci device structure
158 *
159 * returns family.
160 */
mic_get_family(struct pci_dev * pdev)161 static enum mic_hw_family mic_get_family(struct pci_dev *pdev)
162 {
163 enum mic_hw_family family;
164
165 switch (pdev->device) {
166 case MIC_X100_PCI_DEVICE_2250:
167 case MIC_X100_PCI_DEVICE_2251:
168 case MIC_X100_PCI_DEVICE_2252:
169 case MIC_X100_PCI_DEVICE_2253:
170 case MIC_X100_PCI_DEVICE_2254:
171 case MIC_X100_PCI_DEVICE_2255:
172 case MIC_X100_PCI_DEVICE_2256:
173 case MIC_X100_PCI_DEVICE_2257:
174 case MIC_X100_PCI_DEVICE_2258:
175 case MIC_X100_PCI_DEVICE_2259:
176 case MIC_X100_PCI_DEVICE_225a:
177 case MIC_X100_PCI_DEVICE_225b:
178 case MIC_X100_PCI_DEVICE_225c:
179 case MIC_X100_PCI_DEVICE_225d:
180 case MIC_X100_PCI_DEVICE_225e:
181 family = MIC_FAMILY_X100;
182 break;
183 default:
184 family = MIC_FAMILY_UNKNOWN;
185 break;
186 }
187 return family;
188 }
189
190 /**
191 * mic_pm_notifier: Notifier callback function that handles
192 * PM notifications.
193 *
194 * @notifier_block: The notifier structure.
195 * @pm_event: The event for which the driver was notified.
196 * @unused: Meaningless. Always NULL.
197 *
198 * returns NOTIFY_DONE
199 */
mic_pm_notifier(struct notifier_block * notifier,unsigned long pm_event,void * unused)200 static int mic_pm_notifier(struct notifier_block *notifier,
201 unsigned long pm_event, void *unused)
202 {
203 struct mic_device *mdev = container_of(notifier,
204 struct mic_device, pm_notifier);
205
206 switch (pm_event) {
207 case PM_HIBERNATION_PREPARE:
208 /* Fall through */
209 case PM_SUSPEND_PREPARE:
210 mic_prepare_suspend(mdev);
211 break;
212 case PM_POST_HIBERNATION:
213 /* Fall through */
214 case PM_POST_SUSPEND:
215 /* Fall through */
216 case PM_POST_RESTORE:
217 mic_complete_resume(mdev);
218 break;
219 case PM_RESTORE_PREPARE:
220 break;
221 default:
222 break;
223 }
224 return NOTIFY_DONE;
225 }
226
227 /**
228 * mic_device_init - Allocates and initializes the MIC device structure
229 *
230 * @mdev: pointer to mic_device instance
231 * @pdev: The pci device structure
232 *
233 * returns none.
234 */
235 static int
mic_device_init(struct mic_device * mdev,struct pci_dev * pdev)236 mic_device_init(struct mic_device *mdev, struct pci_dev *pdev)
237 {
238 int rc;
239
240 mdev->family = mic_get_family(pdev);
241 mdev->stepping = pdev->revision;
242 mic_ops_init(mdev);
243 mic_sysfs_init(mdev);
244 mutex_init(&mdev->mic_mutex);
245 mdev->irq_info.next_avail_src = 0;
246 INIT_WORK(&mdev->reset_trigger_work, mic_reset_trigger_work);
247 INIT_WORK(&mdev->shutdown_work, mic_shutdown_work);
248 init_completion(&mdev->reset_wait);
249 INIT_LIST_HEAD(&mdev->vdev_list);
250 mdev->pm_notifier.notifier_call = mic_pm_notifier;
251 rc = register_pm_notifier(&mdev->pm_notifier);
252 if (rc) {
253 dev_err(&pdev->dev, "register_pm_notifier failed rc %d\n",
254 rc);
255 goto register_pm_notifier_fail;
256 }
257 return 0;
258 register_pm_notifier_fail:
259 flush_work(&mdev->shutdown_work);
260 flush_work(&mdev->reset_trigger_work);
261 return rc;
262 }
263
264 /**
265 * mic_device_uninit - Frees resources allocated during mic_device_init(..)
266 *
267 * @mdev: pointer to mic_device instance
268 *
269 * returns none
270 */
mic_device_uninit(struct mic_device * mdev)271 static void mic_device_uninit(struct mic_device *mdev)
272 {
273 /* The cmdline sysfs entry might have allocated cmdline */
274 kfree(mdev->cmdline);
275 kfree(mdev->firmware);
276 kfree(mdev->ramdisk);
277 kfree(mdev->bootmode);
278 flush_work(&mdev->reset_trigger_work);
279 flush_work(&mdev->shutdown_work);
280 unregister_pm_notifier(&mdev->pm_notifier);
281 }
282
283 /**
284 * mic_probe - Device Initialization Routine
285 *
286 * @pdev: PCI device structure
287 * @ent: entry in mic_pci_tbl
288 *
289 * returns 0 on success, < 0 on failure.
290 */
mic_probe(struct pci_dev * pdev,const struct pci_device_id * ent)291 static int mic_probe(struct pci_dev *pdev,
292 const struct pci_device_id *ent)
293 {
294 int rc;
295 struct mic_device *mdev;
296
297 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
298 if (!mdev) {
299 rc = -ENOMEM;
300 dev_err(&pdev->dev, "mdev kmalloc failed rc %d\n", rc);
301 goto mdev_alloc_fail;
302 }
303 mdev->id = ida_simple_get(&g_mic_ida, 0, MIC_MAX_NUM_DEVS, GFP_KERNEL);
304 if (mdev->id < 0) {
305 rc = mdev->id;
306 dev_err(&pdev->dev, "ida_simple_get failed rc %d\n", rc);
307 goto ida_fail;
308 }
309
310 rc = mic_device_init(mdev, pdev);
311 if (rc) {
312 dev_err(&pdev->dev, "mic_device_init failed rc %d\n", rc);
313 goto device_init_fail;
314 }
315
316 rc = pci_enable_device(pdev);
317 if (rc) {
318 dev_err(&pdev->dev, "failed to enable pci device.\n");
319 goto uninit_device;
320 }
321
322 pci_set_master(pdev);
323
324 rc = pci_request_regions(pdev, mic_driver_name);
325 if (rc) {
326 dev_err(&pdev->dev, "failed to get pci regions.\n");
327 goto disable_device;
328 }
329
330 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
331 if (rc) {
332 dev_err(&pdev->dev, "Cannot set DMA mask\n");
333 goto release_regions;
334 }
335
336 mdev->mmio.pa = pci_resource_start(pdev, mdev->ops->mmio_bar);
337 mdev->mmio.len = pci_resource_len(pdev, mdev->ops->mmio_bar);
338 mdev->mmio.va = pci_ioremap_bar(pdev, mdev->ops->mmio_bar);
339 if (!mdev->mmio.va) {
340 dev_err(&pdev->dev, "Cannot remap MMIO BAR\n");
341 rc = -EIO;
342 goto release_regions;
343 }
344
345 mdev->aper.pa = pci_resource_start(pdev, mdev->ops->aper_bar);
346 mdev->aper.len = pci_resource_len(pdev, mdev->ops->aper_bar);
347 mdev->aper.va = ioremap_wc(mdev->aper.pa, mdev->aper.len);
348 if (!mdev->aper.va) {
349 dev_err(&pdev->dev, "Cannot remap Aperture BAR\n");
350 rc = -EIO;
351 goto unmap_mmio;
352 }
353
354 mdev->intr_ops->intr_init(mdev);
355 rc = mic_setup_interrupts(mdev, pdev);
356 if (rc) {
357 dev_err(&pdev->dev, "mic_setup_interrupts failed %d\n", rc);
358 goto unmap_aper;
359 }
360 rc = mic_smpt_init(mdev);
361 if (rc) {
362 dev_err(&pdev->dev, "smpt_init failed %d\n", rc);
363 goto free_interrupts;
364 }
365
366 pci_set_drvdata(pdev, mdev);
367
368 mdev->sdev = device_create_with_groups(g_mic_class, &pdev->dev,
369 MKDEV(MAJOR(g_mic_devno), mdev->id), NULL,
370 mdev->attr_group, "mic%d", mdev->id);
371 if (IS_ERR(mdev->sdev)) {
372 rc = PTR_ERR(mdev->sdev);
373 dev_err(&pdev->dev,
374 "device_create_with_groups failed rc %d\n", rc);
375 goto smpt_uninit;
376 }
377 mdev->state_sysfs = sysfs_get_dirent(mdev->sdev->kobj.sd, "state");
378 if (!mdev->state_sysfs) {
379 rc = -ENODEV;
380 dev_err(&pdev->dev, "sysfs_get_dirent failed rc %d\n", rc);
381 goto destroy_device;
382 }
383
384 rc = mic_dp_init(mdev);
385 if (rc) {
386 dev_err(&pdev->dev, "mic_dp_init failed rc %d\n", rc);
387 goto sysfs_put;
388 }
389 mutex_lock(&mdev->mic_mutex);
390
391 mdev->shutdown_db = mic_next_db(mdev);
392 mdev->shutdown_cookie = mic_request_threaded_irq(mdev, mic_shutdown_db,
393 NULL, "shutdown-interrupt", mdev,
394 mdev->shutdown_db, MIC_INTR_DB);
395 if (IS_ERR(mdev->shutdown_cookie)) {
396 rc = PTR_ERR(mdev->shutdown_cookie);
397 mutex_unlock(&mdev->mic_mutex);
398 goto dp_uninit;
399 }
400 mutex_unlock(&mdev->mic_mutex);
401 mic_bootparam_init(mdev);
402
403 mic_create_debug_dir(mdev);
404 cdev_init(&mdev->cdev, &mic_fops);
405 mdev->cdev.owner = THIS_MODULE;
406 rc = cdev_add(&mdev->cdev, MKDEV(MAJOR(g_mic_devno), mdev->id), 1);
407 if (rc) {
408 dev_err(&pdev->dev, "cdev_add err id %d rc %d\n", mdev->id, rc);
409 goto cleanup_debug_dir;
410 }
411 return 0;
412 cleanup_debug_dir:
413 mic_delete_debug_dir(mdev);
414 mutex_lock(&mdev->mic_mutex);
415 mic_free_irq(mdev, mdev->shutdown_cookie, mdev);
416 mutex_unlock(&mdev->mic_mutex);
417 dp_uninit:
418 mic_dp_uninit(mdev);
419 sysfs_put:
420 sysfs_put(mdev->state_sysfs);
421 destroy_device:
422 device_destroy(g_mic_class, MKDEV(MAJOR(g_mic_devno), mdev->id));
423 smpt_uninit:
424 mic_smpt_uninit(mdev);
425 free_interrupts:
426 mic_free_interrupts(mdev, pdev);
427 unmap_aper:
428 iounmap(mdev->aper.va);
429 unmap_mmio:
430 iounmap(mdev->mmio.va);
431 release_regions:
432 pci_release_regions(pdev);
433 disable_device:
434 pci_disable_device(pdev);
435 uninit_device:
436 mic_device_uninit(mdev);
437 device_init_fail:
438 ida_simple_remove(&g_mic_ida, mdev->id);
439 ida_fail:
440 kfree(mdev);
441 mdev_alloc_fail:
442 dev_err(&pdev->dev, "Probe failed rc %d\n", rc);
443 return rc;
444 }
445
446 /**
447 * mic_remove - Device Removal Routine
448 * mic_remove is called by the PCI subsystem to alert the driver
449 * that it should release a PCI device.
450 *
451 * @pdev: PCI device structure
452 */
mic_remove(struct pci_dev * pdev)453 static void mic_remove(struct pci_dev *pdev)
454 {
455 struct mic_device *mdev;
456
457 mdev = pci_get_drvdata(pdev);
458 if (!mdev)
459 return;
460
461 mic_stop(mdev, false);
462 cdev_del(&mdev->cdev);
463 mic_delete_debug_dir(mdev);
464 mutex_lock(&mdev->mic_mutex);
465 mic_free_irq(mdev, mdev->shutdown_cookie, mdev);
466 mutex_unlock(&mdev->mic_mutex);
467 flush_work(&mdev->shutdown_work);
468 mic_dp_uninit(mdev);
469 sysfs_put(mdev->state_sysfs);
470 device_destroy(g_mic_class, MKDEV(MAJOR(g_mic_devno), mdev->id));
471 mic_smpt_uninit(mdev);
472 mic_free_interrupts(mdev, pdev);
473 iounmap(mdev->mmio.va);
474 iounmap(mdev->aper.va);
475 mic_device_uninit(mdev);
476 pci_release_regions(pdev);
477 pci_disable_device(pdev);
478 ida_simple_remove(&g_mic_ida, mdev->id);
479 kfree(mdev);
480 }
481 static struct pci_driver mic_driver = {
482 .name = mic_driver_name,
483 .id_table = mic_pci_tbl,
484 .probe = mic_probe,
485 .remove = mic_remove
486 };
487
mic_init(void)488 static int __init mic_init(void)
489 {
490 int ret;
491
492 ret = alloc_chrdev_region(&g_mic_devno, 0,
493 MIC_MAX_NUM_DEVS, mic_driver_name);
494 if (ret) {
495 pr_err("alloc_chrdev_region failed ret %d\n", ret);
496 goto error;
497 }
498
499 g_mic_class = class_create(THIS_MODULE, mic_driver_name);
500 if (IS_ERR(g_mic_class)) {
501 ret = PTR_ERR(g_mic_class);
502 pr_err("class_create failed ret %d\n", ret);
503 goto cleanup_chrdev;
504 }
505
506 mic_init_debugfs();
507 ida_init(&g_mic_ida);
508 ret = pci_register_driver(&mic_driver);
509 if (ret) {
510 pr_err("pci_register_driver failed ret %d\n", ret);
511 goto cleanup_debugfs;
512 }
513 return ret;
514 cleanup_debugfs:
515 mic_exit_debugfs();
516 class_destroy(g_mic_class);
517 cleanup_chrdev:
518 unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
519 error:
520 return ret;
521 }
522
mic_exit(void)523 static void __exit mic_exit(void)
524 {
525 pci_unregister_driver(&mic_driver);
526 ida_destroy(&g_mic_ida);
527 mic_exit_debugfs();
528 class_destroy(g_mic_class);
529 unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
530 }
531
532 module_init(mic_init);
533 module_exit(mic_exit);
534
535 MODULE_AUTHOR("Intel Corporation");
536 MODULE_DESCRIPTION("Intel(R) MIC X100 Host driver");
537 MODULE_LICENSE("GPL v2");
538