pciehp_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * PCI Express Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/pci.h>
  34. #include "pciehp.h"
  35. #include <linux/interrupt.h>
  36. #include <linux/time.h>
  37. /* Global variables */
  38. int pciehp_debug;
  39. int pciehp_poll_mode;
  40. int pciehp_poll_time;
  41. int pciehp_force;
  42. struct workqueue_struct *pciehp_wq;
  43. #define DRIVER_VERSION "0.4"
  44. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  45. #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
  46. MODULE_AUTHOR(DRIVER_AUTHOR);
  47. MODULE_DESCRIPTION(DRIVER_DESC);
  48. MODULE_LICENSE("GPL");
  49. module_param(pciehp_debug, bool, 0644);
  50. module_param(pciehp_poll_mode, bool, 0644);
  51. module_param(pciehp_poll_time, int, 0644);
  52. module_param(pciehp_force, bool, 0644);
  53. MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
  54. MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
  55. MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
  56. MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
  57. #define PCIE_MODULE_NAME "pciehp"
  58. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  59. static int enable_slot (struct hotplug_slot *slot);
  60. static int disable_slot (struct hotplug_slot *slot);
  61. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  62. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  63. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  66. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  67. static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
  68. .owner = THIS_MODULE,
  69. .set_attention_status = set_attention_status,
  70. .enable_slot = enable_slot,
  71. .disable_slot = disable_slot,
  72. .get_power_status = get_power_status,
  73. .get_attention_status = get_attention_status,
  74. .get_latch_status = get_latch_status,
  75. .get_adapter_status = get_adapter_status,
  76. .get_max_bus_speed = get_max_bus_speed,
  77. .get_cur_bus_speed = get_cur_bus_speed,
  78. };
  79. /*
  80. * Check the status of the Electro Mechanical Interlock (EMI)
  81. */
  82. static int get_lock_status(struct hotplug_slot *hotplug_slot, u8 *value)
  83. {
  84. struct slot *slot = hotplug_slot->private;
  85. return (slot->hpc_ops->get_emi_status(slot, value));
  86. }
  87. /*
  88. * sysfs interface for the Electro Mechanical Interlock (EMI)
  89. * 1 == locked, 0 == unlocked
  90. */
  91. static ssize_t lock_read_file(struct hotplug_slot *slot, char *buf)
  92. {
  93. int retval;
  94. u8 value;
  95. retval = get_lock_status(slot, &value);
  96. if (retval)
  97. goto lock_read_exit;
  98. retval = sprintf (buf, "%d\n", value);
  99. lock_read_exit:
  100. return retval;
  101. }
  102. /*
  103. * Change the status of the Electro Mechanical Interlock (EMI)
  104. * This is a toggle - in addition there must be at least 1 second
  105. * in between toggles.
  106. */
  107. static int set_lock_status(struct hotplug_slot *hotplug_slot, u8 status)
  108. {
  109. struct slot *slot = hotplug_slot->private;
  110. int retval;
  111. u8 value;
  112. mutex_lock(&slot->ctrl->crit_sect);
  113. /* has it been >1 sec since our last toggle? */
  114. if ((get_seconds() - slot->last_emi_toggle) < 1)
  115. return -EINVAL;
  116. /* see what our current state is */
  117. retval = get_lock_status(hotplug_slot, &value);
  118. if (retval || (value == status))
  119. goto set_lock_exit;
  120. slot->hpc_ops->toggle_emi(slot);
  121. set_lock_exit:
  122. mutex_unlock(&slot->ctrl->crit_sect);
  123. return 0;
  124. }
  125. /*
  126. * sysfs interface which allows the user to toggle the Electro Mechanical
  127. * Interlock. Valid values are either 0 or 1. 0 == unlock, 1 == lock
  128. */
  129. static ssize_t lock_write_file(struct hotplug_slot *hotplug_slot,
  130. const char *buf, size_t count)
  131. {
  132. struct slot *slot = hotplug_slot->private;
  133. unsigned long llock;
  134. u8 lock;
  135. int retval = 0;
  136. llock = simple_strtoul(buf, NULL, 10);
  137. lock = (u8)(llock & 0xff);
  138. switch (lock) {
  139. case 0:
  140. case 1:
  141. retval = set_lock_status(hotplug_slot, lock);
  142. break;
  143. default:
  144. ctrl_err(slot->ctrl, "%d is an invalid lock value\n",
  145. lock);
  146. retval = -EINVAL;
  147. }
  148. if (retval)
  149. return retval;
  150. return count;
  151. }
  152. static struct hotplug_slot_attribute hotplug_slot_attr_lock = {
  153. .attr = {.name = "lock", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  154. .show = lock_read_file,
  155. .store = lock_write_file
  156. };
  157. /**
  158. * release_slot - free up the memory used by a slot
  159. * @hotplug_slot: slot to free
  160. */
  161. static void release_slot(struct hotplug_slot *hotplug_slot)
  162. {
  163. struct slot *slot = hotplug_slot->private;
  164. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  165. __func__, hotplug_slot->name);
  166. kfree(hotplug_slot->info);
  167. kfree(hotplug_slot);
  168. }
  169. static int init_slots(struct controller *ctrl)
  170. {
  171. struct slot *slot;
  172. struct hotplug_slot *hotplug_slot;
  173. struct hotplug_slot_info *info;
  174. int len, dup = 1;
  175. int retval = -ENOMEM;
  176. list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
  177. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  178. if (!hotplug_slot)
  179. goto error;
  180. info = kzalloc(sizeof(*info), GFP_KERNEL);
  181. if (!info)
  182. goto error_hpslot;
  183. /* register this slot with the hotplug pci core */
  184. hotplug_slot->info = info;
  185. hotplug_slot->name = slot->name;
  186. hotplug_slot->private = slot;
  187. hotplug_slot->release = &release_slot;
  188. hotplug_slot->ops = &pciehp_hotplug_slot_ops;
  189. get_power_status(hotplug_slot, &info->power_status);
  190. get_attention_status(hotplug_slot, &info->attention_status);
  191. get_latch_status(hotplug_slot, &info->latch_status);
  192. get_adapter_status(hotplug_slot, &info->adapter_status);
  193. slot->hotplug_slot = hotplug_slot;
  194. ctrl_dbg(ctrl, "Registering bus=%x dev=%x hp_slot=%x sun=%x "
  195. "slot_device_offset=%x\n", slot->bus, slot->device,
  196. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  197. duplicate_name:
  198. retval = pci_hp_register(hotplug_slot,
  199. ctrl->pci_dev->subordinate,
  200. slot->device,
  201. slot->name);
  202. if (retval) {
  203. /*
  204. * If slot N already exists, we'll try to create
  205. * slot N-1, N-2 ... N-M, until we overflow.
  206. */
  207. if (retval == -EEXIST) {
  208. len = snprintf(slot->name, SLOT_NAME_SIZE,
  209. "%d-%d", slot->number, dup++);
  210. if (len < SLOT_NAME_SIZE)
  211. goto duplicate_name;
  212. else
  213. ctrl_err(ctrl, "duplicate slot name "
  214. "overflow\n");
  215. }
  216. ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
  217. retval);
  218. goto error_info;
  219. }
  220. /* create additional sysfs entries */
  221. if (EMI(ctrl)) {
  222. retval = sysfs_create_file(&hotplug_slot->pci_slot->kobj,
  223. &hotplug_slot_attr_lock.attr);
  224. if (retval) {
  225. pci_hp_deregister(hotplug_slot);
  226. ctrl_err(ctrl, "cannot create additional sysfs "
  227. "entries\n");
  228. goto error_info;
  229. }
  230. }
  231. }
  232. return 0;
  233. error_info:
  234. kfree(info);
  235. error_hpslot:
  236. kfree(hotplug_slot);
  237. error:
  238. return retval;
  239. }
  240. static void cleanup_slots(struct controller *ctrl)
  241. {
  242. struct slot *slot;
  243. list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
  244. if (EMI(ctrl))
  245. sysfs_remove_file(&slot->hotplug_slot->pci_slot->kobj,
  246. &hotplug_slot_attr_lock.attr);
  247. pci_hp_deregister(slot->hotplug_slot);
  248. }
  249. }
  250. /*
  251. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  252. */
  253. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  254. {
  255. struct slot *slot = hotplug_slot->private;
  256. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  257. __func__, hotplug_slot->name);
  258. hotplug_slot->info->attention_status = status;
  259. if (ATTN_LED(slot->ctrl))
  260. slot->hpc_ops->set_attention_status(slot, status);
  261. return 0;
  262. }
  263. static int enable_slot(struct hotplug_slot *hotplug_slot)
  264. {
  265. struct slot *slot = hotplug_slot->private;
  266. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  267. __func__, hotplug_slot->name);
  268. return pciehp_sysfs_enable_slot(slot);
  269. }
  270. static int disable_slot(struct hotplug_slot *hotplug_slot)
  271. {
  272. struct slot *slot = hotplug_slot->private;
  273. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  274. __func__, hotplug_slot->name);
  275. return pciehp_sysfs_disable_slot(slot);
  276. }
  277. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  278. {
  279. struct slot *slot = hotplug_slot->private;
  280. int retval;
  281. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  282. __func__, hotplug_slot->name);
  283. retval = slot->hpc_ops->get_power_status(slot, value);
  284. if (retval < 0)
  285. *value = hotplug_slot->info->power_status;
  286. return 0;
  287. }
  288. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  289. {
  290. struct slot *slot = hotplug_slot->private;
  291. int retval;
  292. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  293. __func__, hotplug_slot->name);
  294. retval = slot->hpc_ops->get_attention_status(slot, value);
  295. if (retval < 0)
  296. *value = hotplug_slot->info->attention_status;
  297. return 0;
  298. }
  299. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  300. {
  301. struct slot *slot = hotplug_slot->private;
  302. int retval;
  303. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  304. __func__, hotplug_slot->name);
  305. retval = slot->hpc_ops->get_latch_status(slot, value);
  306. if (retval < 0)
  307. *value = hotplug_slot->info->latch_status;
  308. return 0;
  309. }
  310. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  311. {
  312. struct slot *slot = hotplug_slot->private;
  313. int retval;
  314. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  315. __func__, hotplug_slot->name);
  316. retval = slot->hpc_ops->get_adapter_status(slot, value);
  317. if (retval < 0)
  318. *value = hotplug_slot->info->adapter_status;
  319. return 0;
  320. }
  321. static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
  322. enum pci_bus_speed *value)
  323. {
  324. struct slot *slot = hotplug_slot->private;
  325. int retval;
  326. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  327. __func__, hotplug_slot->name);
  328. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  329. if (retval < 0)
  330. *value = PCI_SPEED_UNKNOWN;
  331. return 0;
  332. }
  333. static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  334. {
  335. struct slot *slot = hotplug_slot->private;
  336. int retval;
  337. ctrl_dbg(slot->ctrl, "%s - physical_slot = %s\n",
  338. __func__, hotplug_slot->name);
  339. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  340. if (retval < 0)
  341. *value = PCI_SPEED_UNKNOWN;
  342. return 0;
  343. }
  344. static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
  345. {
  346. int rc;
  347. struct controller *ctrl;
  348. struct slot *t_slot;
  349. u8 value;
  350. struct pci_dev *pdev = dev->port;
  351. if (pciehp_force)
  352. dev_info(&dev->device,
  353. "Bypassing BIOS check for pciehp use on %s\n",
  354. pci_name(pdev));
  355. else if (pciehp_get_hp_hw_control_from_firmware(pdev))
  356. goto err_out_none;
  357. ctrl = pcie_init(dev);
  358. if (!ctrl) {
  359. dev_err(&dev->device, "controller initialization failed\n");
  360. goto err_out_none;
  361. }
  362. set_service_data(dev, ctrl);
  363. /* Setup the slot information structures */
  364. rc = init_slots(ctrl);
  365. if (rc) {
  366. if (rc == -EBUSY)
  367. ctrl_warn(ctrl, "slot already registered by another "
  368. "hotplug driver\n");
  369. else
  370. ctrl_err(ctrl, "slot initialization failed\n");
  371. goto err_out_release_ctlr;
  372. }
  373. t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  374. t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
  375. if (value && pciehp_force) {
  376. rc = pciehp_enable_slot(t_slot);
  377. if (rc) /* -ENODEV: shouldn't happen, but deal with it */
  378. value = 0;
  379. }
  380. if ((POWER_CTRL(ctrl)) && !value) {
  381. rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
  382. if (rc)
  383. goto err_out_free_ctrl_slot;
  384. }
  385. return 0;
  386. err_out_free_ctrl_slot:
  387. cleanup_slots(ctrl);
  388. err_out_release_ctlr:
  389. ctrl->hpc_ops->release_ctlr(ctrl);
  390. err_out_none:
  391. return -ENODEV;
  392. }
  393. static void pciehp_remove (struct pcie_device *dev)
  394. {
  395. struct controller *ctrl = get_service_data(dev);
  396. cleanup_slots(ctrl);
  397. ctrl->hpc_ops->release_ctlr(ctrl);
  398. }
  399. #ifdef CONFIG_PM
  400. static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
  401. {
  402. dev_info(&dev->device, "%s ENTRY\n", __func__);
  403. return 0;
  404. }
  405. static int pciehp_resume (struct pcie_device *dev)
  406. {
  407. dev_info(&dev->device, "%s ENTRY\n", __func__);
  408. if (pciehp_force) {
  409. struct controller *ctrl = get_service_data(dev);
  410. struct slot *t_slot;
  411. u8 status;
  412. /* reinitialize the chipset's event detection logic */
  413. pcie_enable_notification(ctrl);
  414. t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  415. /* Check if slot is occupied */
  416. t_slot->hpc_ops->get_adapter_status(t_slot, &status);
  417. if (status)
  418. pciehp_enable_slot(t_slot);
  419. else
  420. pciehp_disable_slot(t_slot);
  421. }
  422. return 0;
  423. }
  424. #endif
  425. static struct pcie_port_service_id port_pci_ids[] = { {
  426. .vendor = PCI_ANY_ID,
  427. .device = PCI_ANY_ID,
  428. .port_type = PCIE_ANY_PORT,
  429. .service_type = PCIE_PORT_SERVICE_HP,
  430. .driver_data = 0,
  431. }, { /* end: all zeroes */ }
  432. };
  433. static struct pcie_port_service_driver hpdriver_portdrv = {
  434. .name = PCIE_MODULE_NAME,
  435. .id_table = &port_pci_ids[0],
  436. .probe = pciehp_probe,
  437. .remove = pciehp_remove,
  438. #ifdef CONFIG_PM
  439. .suspend = pciehp_suspend,
  440. .resume = pciehp_resume,
  441. #endif /* PM */
  442. };
  443. static int __init pcied_init(void)
  444. {
  445. int retval = 0;
  446. retval = pcie_port_service_register(&hpdriver_portdrv);
  447. dbg("pcie_port_service_register = %d\n", retval);
  448. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  449. if (retval)
  450. dbg("%s: Failure to register service\n", __func__);
  451. return retval;
  452. }
  453. static void __exit pcied_cleanup(void)
  454. {
  455. dbg("unload_pciehpd()\n");
  456. pcie_port_service_unregister(&hpdriver_portdrv);
  457. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  458. }
  459. module_init(pcied_init);
  460. module_exit(pcied_cleanup);