12703d4b2STomas Winkler /* 22703d4b2STomas Winkler * 32703d4b2STomas Winkler * Intel Management Engine Interface (Intel MEI) Linux driver 42703d4b2STomas Winkler * Copyright (c) 2003-2012, Intel Corporation. 52703d4b2STomas Winkler * 62703d4b2STomas Winkler * This program is free software; you can redistribute it and/or modify it 72703d4b2STomas Winkler * under the terms and conditions of the GNU General Public License, 82703d4b2STomas Winkler * version 2, as published by the Free Software Foundation. 92703d4b2STomas Winkler * 102703d4b2STomas Winkler * This program is distributed in the hope it will be useful, but WITHOUT 112703d4b2STomas Winkler * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 122703d4b2STomas Winkler * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 132703d4b2STomas Winkler * more details. 142703d4b2STomas Winkler * 152703d4b2STomas Winkler */ 162703d4b2STomas Winkler 172703d4b2STomas Winkler #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 182703d4b2STomas Winkler 192703d4b2STomas Winkler #include <linux/module.h> 202703d4b2STomas Winkler #include <linux/moduleparam.h> 212703d4b2STomas Winkler #include <linux/kernel.h> 222703d4b2STomas Winkler #include <linux/device.h> 232703d4b2STomas Winkler #include <linux/fs.h> 242703d4b2STomas Winkler #include <linux/errno.h> 252703d4b2STomas Winkler #include <linux/types.h> 262703d4b2STomas Winkler #include <linux/fcntl.h> 272703d4b2STomas Winkler #include <linux/aio.h> 282703d4b2STomas Winkler #include <linux/pci.h> 292703d4b2STomas Winkler #include <linux/poll.h> 302703d4b2STomas Winkler #include <linux/init.h> 312703d4b2STomas Winkler #include <linux/ioctl.h> 322703d4b2STomas Winkler #include <linux/cdev.h> 332703d4b2STomas Winkler #include <linux/sched.h> 342703d4b2STomas Winkler #include <linux/uuid.h> 352703d4b2STomas Winkler #include <linux/compat.h> 362703d4b2STomas Winkler #include <linux/jiffies.h> 372703d4b2STomas Winkler #include <linux/interrupt.h> 382703d4b2STomas Winkler #include <linux/miscdevice.h> 392703d4b2STomas Winkler 402703d4b2STomas Winkler #include <linux/mei.h> 412703d4b2STomas Winkler 422703d4b2STomas Winkler #include "mei_dev.h" 432703d4b2STomas Winkler #include "hw-me.h" 442703d4b2STomas Winkler #include "client.h" 452703d4b2STomas Winkler 462703d4b2STomas Winkler /* mei_pci_tbl - PCI Device ID Table */ 47b68301e9STomas Winkler static DEFINE_PCI_DEVICE_TABLE(mei_me_pci_tbl) = { 482703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)}, 492703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)}, 502703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)}, 512703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)}, 522703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)}, 532703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)}, 542703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)}, 552703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)}, 562703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)}, 572703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)}, 582703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)}, 592703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)}, 602703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)}, 612703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)}, 622703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)}, 632703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)}, 642703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)}, 652703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)}, 662703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)}, 672703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)}, 682703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)}, 692703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)}, 702703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)}, 712703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)}, 722703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)}, 732703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)}, 742703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)}, 752703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)}, 762703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)}, 772703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)}, 782703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)}, 7976a96359STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_H)}, 80838b3a6dSTomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_W)}, 812703d4b2STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)}, 8276a96359STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_HR)}, 8376a96359STomas Winkler {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_WPT_LP)}, 842703d4b2STomas Winkler 852703d4b2STomas Winkler /* required last entry */ 862703d4b2STomas Winkler {0, } 872703d4b2STomas Winkler }; 882703d4b2STomas Winkler 89b68301e9STomas Winkler MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl); 902703d4b2STomas Winkler 912703d4b2STomas Winkler /** 922703d4b2STomas Winkler * mei_quirk_probe - probe for devices that doesn't valid ME interface 93393b148fSMasanari Iida * 942703d4b2STomas Winkler * @pdev: PCI device structure 952703d4b2STomas Winkler * @ent: entry into pci_device_table 962703d4b2STomas Winkler * 972703d4b2STomas Winkler * returns true if ME Interface is valid, false otherwise 982703d4b2STomas Winkler */ 99b68301e9STomas Winkler static bool mei_me_quirk_probe(struct pci_dev *pdev, 1002703d4b2STomas Winkler const struct pci_device_id *ent) 1012703d4b2STomas Winkler { 1022703d4b2STomas Winkler u32 reg; 1032703d4b2STomas Winkler if (ent->device == MEI_DEV_ID_PBG_1) { 1042703d4b2STomas Winkler pci_read_config_dword(pdev, 0x48, ®); 1052703d4b2STomas Winkler /* make sure that bit 9 is up and bit 10 is down */ 1062703d4b2STomas Winkler if ((reg & 0x600) == 0x200) { 1072703d4b2STomas Winkler dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n"); 1082703d4b2STomas Winkler return false; 1092703d4b2STomas Winkler } 1102703d4b2STomas Winkler } 1112703d4b2STomas Winkler return true; 1122703d4b2STomas Winkler } 1132703d4b2STomas Winkler /** 1142703d4b2STomas Winkler * mei_probe - Device Initialization Routine 1152703d4b2STomas Winkler * 1162703d4b2STomas Winkler * @pdev: PCI device structure 1172703d4b2STomas Winkler * @ent: entry in kcs_pci_tbl 1182703d4b2STomas Winkler * 1192703d4b2STomas Winkler * returns 0 on success, <0 on failure. 1202703d4b2STomas Winkler */ 121b68301e9STomas Winkler static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 1222703d4b2STomas Winkler { 1232703d4b2STomas Winkler struct mei_device *dev; 12452c34561STomas Winkler struct mei_me_hw *hw; 1252703d4b2STomas Winkler int err; 1262703d4b2STomas Winkler 1272703d4b2STomas Winkler 128b68301e9STomas Winkler if (!mei_me_quirk_probe(pdev, ent)) { 1292703d4b2STomas Winkler err = -ENODEV; 1302703d4b2STomas Winkler goto end; 1312703d4b2STomas Winkler } 1322703d4b2STomas Winkler 1332703d4b2STomas Winkler /* enable pci dev */ 1342703d4b2STomas Winkler err = pci_enable_device(pdev); 1352703d4b2STomas Winkler if (err) { 1362703d4b2STomas Winkler dev_err(&pdev->dev, "failed to enable pci device.\n"); 1372703d4b2STomas Winkler goto end; 1382703d4b2STomas Winkler } 1392703d4b2STomas Winkler /* set PCI host mastering */ 1402703d4b2STomas Winkler pci_set_master(pdev); 1412703d4b2STomas Winkler /* pci request regions for mei driver */ 1422703d4b2STomas Winkler err = pci_request_regions(pdev, KBUILD_MODNAME); 1432703d4b2STomas Winkler if (err) { 1442703d4b2STomas Winkler dev_err(&pdev->dev, "failed to get pci regions.\n"); 1452703d4b2STomas Winkler goto disable_device; 1462703d4b2STomas Winkler } 1472703d4b2STomas Winkler /* allocates and initializes the mei dev structure */ 14852c34561STomas Winkler dev = mei_me_dev_init(pdev); 1492703d4b2STomas Winkler if (!dev) { 1502703d4b2STomas Winkler err = -ENOMEM; 1512703d4b2STomas Winkler goto release_regions; 1522703d4b2STomas Winkler } 15352c34561STomas Winkler hw = to_me_hw(dev); 1542703d4b2STomas Winkler /* mapping IO device memory */ 15552c34561STomas Winkler hw->mem_addr = pci_iomap(pdev, 0, 0); 15652c34561STomas Winkler if (!hw->mem_addr) { 1572703d4b2STomas Winkler dev_err(&pdev->dev, "mapping I/O device memory failure.\n"); 1582703d4b2STomas Winkler err = -ENOMEM; 1592703d4b2STomas Winkler goto free_device; 1602703d4b2STomas Winkler } 1612703d4b2STomas Winkler pci_enable_msi(pdev); 1622703d4b2STomas Winkler 1632703d4b2STomas Winkler /* request and enable interrupt */ 1642703d4b2STomas Winkler if (pci_dev_msi_enabled(pdev)) 1652703d4b2STomas Winkler err = request_threaded_irq(pdev->irq, 1662703d4b2STomas Winkler NULL, 16706ecd645STomas Winkler mei_me_irq_thread_handler, 1682703d4b2STomas Winkler IRQF_ONESHOT, KBUILD_MODNAME, dev); 1692703d4b2STomas Winkler else 1702703d4b2STomas Winkler err = request_threaded_irq(pdev->irq, 17106ecd645STomas Winkler mei_me_irq_quick_handler, 17206ecd645STomas Winkler mei_me_irq_thread_handler, 1732703d4b2STomas Winkler IRQF_SHARED, KBUILD_MODNAME, dev); 1742703d4b2STomas Winkler 1752703d4b2STomas Winkler if (err) { 1762703d4b2STomas Winkler dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n", 1772703d4b2STomas Winkler pdev->irq); 1782703d4b2STomas Winkler goto disable_msi; 1792703d4b2STomas Winkler } 1802703d4b2STomas Winkler 181c4d589beSTomas Winkler if (mei_start(dev)) { 1822703d4b2STomas Winkler dev_err(&pdev->dev, "init hw failure.\n"); 1832703d4b2STomas Winkler err = -ENODEV; 1842703d4b2STomas Winkler goto release_irq; 1852703d4b2STomas Winkler } 1862703d4b2STomas Winkler 18730e53bb8STomas Winkler err = mei_register(dev); 1882703d4b2STomas Winkler if (err) 1892703d4b2STomas Winkler goto release_irq; 1902703d4b2STomas Winkler 1912703d4b2STomas Winkler pci_set_drvdata(pdev, dev); 1922703d4b2STomas Winkler 1932703d4b2STomas Winkler schedule_delayed_work(&dev->timer_work, HZ); 1942703d4b2STomas Winkler 195c4e87b52SAlexander Usyskin dev_dbg(&pdev->dev, "initialization successful.\n"); 1962703d4b2STomas Winkler 1972703d4b2STomas Winkler return 0; 1982703d4b2STomas Winkler 1992703d4b2STomas Winkler release_irq: 2002703d4b2STomas Winkler mei_disable_interrupts(dev); 2012703d4b2STomas Winkler flush_scheduled_work(); 2022703d4b2STomas Winkler free_irq(pdev->irq, dev); 2032703d4b2STomas Winkler disable_msi: 2042703d4b2STomas Winkler pci_disable_msi(pdev); 20552c34561STomas Winkler pci_iounmap(pdev, hw->mem_addr); 2062703d4b2STomas Winkler free_device: 2072703d4b2STomas Winkler kfree(dev); 2082703d4b2STomas Winkler release_regions: 2092703d4b2STomas Winkler pci_release_regions(pdev); 2102703d4b2STomas Winkler disable_device: 2112703d4b2STomas Winkler pci_disable_device(pdev); 2122703d4b2STomas Winkler end: 2132703d4b2STomas Winkler dev_err(&pdev->dev, "initialization failed.\n"); 2142703d4b2STomas Winkler return err; 2152703d4b2STomas Winkler } 2162703d4b2STomas Winkler 2172703d4b2STomas Winkler /** 2182703d4b2STomas Winkler * mei_remove - Device Removal Routine 2192703d4b2STomas Winkler * 2202703d4b2STomas Winkler * @pdev: PCI device structure 2212703d4b2STomas Winkler * 2222703d4b2STomas Winkler * mei_remove is called by the PCI subsystem to alert the driver 2232703d4b2STomas Winkler * that it should release a PCI device. 2242703d4b2STomas Winkler */ 225b68301e9STomas Winkler static void mei_me_remove(struct pci_dev *pdev) 2262703d4b2STomas Winkler { 2272703d4b2STomas Winkler struct mei_device *dev; 22852c34561STomas Winkler struct mei_me_hw *hw; 2292703d4b2STomas Winkler 2302703d4b2STomas Winkler dev = pci_get_drvdata(pdev); 2312703d4b2STomas Winkler if (!dev) 2322703d4b2STomas Winkler return; 2332703d4b2STomas Winkler 23452c34561STomas Winkler hw = to_me_hw(dev); 23552c34561STomas Winkler 2362703d4b2STomas Winkler 237ed6f7ac1SPaul Bolle dev_dbg(&pdev->dev, "stop\n"); 2387cb035d9STomas Winkler mei_stop(dev); 2392703d4b2STomas Winkler 2402703d4b2STomas Winkler /* disable interrupts */ 2412703d4b2STomas Winkler mei_disable_interrupts(dev); 2422703d4b2STomas Winkler 2432703d4b2STomas Winkler free_irq(pdev->irq, dev); 2442703d4b2STomas Winkler pci_disable_msi(pdev); 2452703d4b2STomas Winkler 24652c34561STomas Winkler if (hw->mem_addr) 24752c34561STomas Winkler pci_iounmap(pdev, hw->mem_addr); 2482703d4b2STomas Winkler 24930e53bb8STomas Winkler mei_deregister(dev); 25030e53bb8STomas Winkler 2512703d4b2STomas Winkler kfree(dev); 2522703d4b2STomas Winkler 2532703d4b2STomas Winkler pci_release_regions(pdev); 2542703d4b2STomas Winkler pci_disable_device(pdev); 2552703d4b2STomas Winkler 2562703d4b2STomas Winkler 2572703d4b2STomas Winkler } 2582703d4b2STomas Winkler #ifdef CONFIG_PM 259b68301e9STomas Winkler static int mei_me_pci_suspend(struct device *device) 2602703d4b2STomas Winkler { 2612703d4b2STomas Winkler struct pci_dev *pdev = to_pci_dev(device); 2622703d4b2STomas Winkler struct mei_device *dev = pci_get_drvdata(pdev); 2632703d4b2STomas Winkler 2642703d4b2STomas Winkler if (!dev) 2652703d4b2STomas Winkler return -ENODEV; 2662703d4b2STomas Winkler 267ed6f7ac1SPaul Bolle dev_dbg(&pdev->dev, "suspend\n"); 2682703d4b2STomas Winkler 2697cb035d9STomas Winkler mei_stop(dev); 2707cb035d9STomas Winkler 2717cb035d9STomas Winkler mei_disable_interrupts(dev); 2722703d4b2STomas Winkler 2732703d4b2STomas Winkler free_irq(pdev->irq, dev); 2742703d4b2STomas Winkler pci_disable_msi(pdev); 2752703d4b2STomas Winkler 2767cb035d9STomas Winkler return 0; 2772703d4b2STomas Winkler } 2782703d4b2STomas Winkler 279b68301e9STomas Winkler static int mei_me_pci_resume(struct device *device) 2802703d4b2STomas Winkler { 2812703d4b2STomas Winkler struct pci_dev *pdev = to_pci_dev(device); 2822703d4b2STomas Winkler struct mei_device *dev; 2832703d4b2STomas Winkler int err; 2842703d4b2STomas Winkler 2852703d4b2STomas Winkler dev = pci_get_drvdata(pdev); 2862703d4b2STomas Winkler if (!dev) 2872703d4b2STomas Winkler return -ENODEV; 2882703d4b2STomas Winkler 2892703d4b2STomas Winkler pci_enable_msi(pdev); 2902703d4b2STomas Winkler 2912703d4b2STomas Winkler /* request and enable interrupt */ 2922703d4b2STomas Winkler if (pci_dev_msi_enabled(pdev)) 2932703d4b2STomas Winkler err = request_threaded_irq(pdev->irq, 2942703d4b2STomas Winkler NULL, 29506ecd645STomas Winkler mei_me_irq_thread_handler, 2962703d4b2STomas Winkler IRQF_ONESHOT, KBUILD_MODNAME, dev); 2972703d4b2STomas Winkler else 2982703d4b2STomas Winkler err = request_threaded_irq(pdev->irq, 29906ecd645STomas Winkler mei_me_irq_quick_handler, 30006ecd645STomas Winkler mei_me_irq_thread_handler, 3012703d4b2STomas Winkler IRQF_SHARED, KBUILD_MODNAME, dev); 3022703d4b2STomas Winkler 3032703d4b2STomas Winkler if (err) { 3042703d4b2STomas Winkler dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n", 3052703d4b2STomas Winkler pdev->irq); 3062703d4b2STomas Winkler return err; 3072703d4b2STomas Winkler } 3082703d4b2STomas Winkler 3092703d4b2STomas Winkler mutex_lock(&dev->device_lock); 3102703d4b2STomas Winkler dev->dev_state = MEI_DEV_POWER_UP; 31142f132feSTomas Winkler mei_clear_interrupts(dev); 3122703d4b2STomas Winkler mei_reset(dev, 1); 3132703d4b2STomas Winkler mutex_unlock(&dev->device_lock); 3142703d4b2STomas Winkler 3152703d4b2STomas Winkler /* Start timer if stopped in suspend */ 3162703d4b2STomas Winkler schedule_delayed_work(&dev->timer_work, HZ); 3172703d4b2STomas Winkler 3182703d4b2STomas Winkler return err; 3192703d4b2STomas Winkler } 320b68301e9STomas Winkler static SIMPLE_DEV_PM_OPS(mei_me_pm_ops, mei_me_pci_suspend, mei_me_pci_resume); 321b68301e9STomas Winkler #define MEI_ME_PM_OPS (&mei_me_pm_ops) 3222703d4b2STomas Winkler #else 323b68301e9STomas Winkler #define MEI_ME_PM_OPS NULL 3242703d4b2STomas Winkler #endif /* CONFIG_PM */ 3252703d4b2STomas Winkler /* 3262703d4b2STomas Winkler * PCI driver structure 3272703d4b2STomas Winkler */ 328b68301e9STomas Winkler static struct pci_driver mei_me_driver = { 3292703d4b2STomas Winkler .name = KBUILD_MODNAME, 330b68301e9STomas Winkler .id_table = mei_me_pci_tbl, 331b68301e9STomas Winkler .probe = mei_me_probe, 332b68301e9STomas Winkler .remove = mei_me_remove, 333b68301e9STomas Winkler .shutdown = mei_me_remove, 334b68301e9STomas Winkler .driver.pm = MEI_ME_PM_OPS, 3352703d4b2STomas Winkler }; 3362703d4b2STomas Winkler 337b68301e9STomas Winkler module_pci_driver(mei_me_driver); 3382703d4b2STomas Winkler 3392703d4b2STomas Winkler MODULE_AUTHOR("Intel Corporation"); 3402703d4b2STomas Winkler MODULE_DESCRIPTION("Intel(R) Management Engine Interface"); 3412703d4b2STomas Winkler MODULE_LICENSE("GPL v2"); 342