pciehp_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. int pciehp_slot_with_bus;
  43. struct workqueue_struct *pciehp_wq;
  44. #define DRIVER_VERSION "0.4"
  45. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  46. #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
  47. MODULE_AUTHOR(DRIVER_AUTHOR);
  48. MODULE_DESCRIPTION(DRIVER_DESC);
  49. MODULE_LICENSE("GPL");
  50. module_param(pciehp_debug, bool, 0644);
  51. module_param(pciehp_poll_mode, bool, 0644);
  52. module_param(pciehp_poll_time, int, 0644);
  53. module_param(pciehp_force, bool, 0644);
  54. module_param(pciehp_slot_with_bus, bool, 0644);
  55. MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
  56. MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
  57. MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
  58. MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
  59. MODULE_PARM_DESC(pciehp_slot_with_bus, "Use bus number in the slot name");
  60. #define PCIE_MODULE_NAME "pciehp"
  61. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  62. static int enable_slot (struct hotplug_slot *slot);
  63. static int disable_slot (struct hotplug_slot *slot);
  64. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  66. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  67. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  68. static int get_address (struct hotplug_slot *slot, u32 *value);
  69. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  70. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  71. static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
  72. .owner = THIS_MODULE,
  73. .set_attention_status = set_attention_status,
  74. .enable_slot = enable_slot,
  75. .disable_slot = disable_slot,
  76. .get_power_status = get_power_status,
  77. .get_attention_status = get_attention_status,
  78. .get_latch_status = get_latch_status,
  79. .get_adapter_status = get_adapter_status,
  80. .get_address = get_address,
  81. .get_max_bus_speed = get_max_bus_speed,
  82. .get_cur_bus_speed = get_cur_bus_speed,
  83. };
  84. /*
  85. * Check the status of the Electro Mechanical Interlock (EMI)
  86. */
  87. static int get_lock_status(struct hotplug_slot *hotplug_slot, u8 *value)
  88. {
  89. struct slot *slot = hotplug_slot->private;
  90. return (slot->hpc_ops->get_emi_status(slot, value));
  91. }
  92. /*
  93. * sysfs interface for the Electro Mechanical Interlock (EMI)
  94. * 1 == locked, 0 == unlocked
  95. */
  96. static ssize_t lock_read_file(struct hotplug_slot *slot, char *buf)
  97. {
  98. int retval;
  99. u8 value;
  100. retval = get_lock_status(slot, &value);
  101. if (retval)
  102. goto lock_read_exit;
  103. retval = sprintf (buf, "%d\n", value);
  104. lock_read_exit:
  105. return retval;
  106. }
  107. /*
  108. * Change the status of the Electro Mechanical Interlock (EMI)
  109. * This is a toggle - in addition there must be at least 1 second
  110. * in between toggles.
  111. */
  112. static int set_lock_status(struct hotplug_slot *hotplug_slot, u8 status)
  113. {
  114. struct slot *slot = hotplug_slot->private;
  115. int retval;
  116. u8 value;
  117. mutex_lock(&slot->ctrl->crit_sect);
  118. /* has it been >1 sec since our last toggle? */
  119. if ((get_seconds() - slot->last_emi_toggle) < 1)
  120. return -EINVAL;
  121. /* see what our current state is */
  122. retval = get_lock_status(hotplug_slot, &value);
  123. if (retval || (value == status))
  124. goto set_lock_exit;
  125. slot->hpc_ops->toggle_emi(slot);
  126. set_lock_exit:
  127. mutex_unlock(&slot->ctrl->crit_sect);
  128. return 0;
  129. }
  130. /*
  131. * sysfs interface which allows the user to toggle the Electro Mechanical
  132. * Interlock. Valid values are either 0 or 1. 0 == unlock, 1 == lock
  133. */
  134. static ssize_t lock_write_file(struct hotplug_slot *slot, const char *buf,
  135. size_t count)
  136. {
  137. unsigned long llock;
  138. u8 lock;
  139. int retval = 0;
  140. llock = simple_strtoul(buf, NULL, 10);
  141. lock = (u8)(llock & 0xff);
  142. switch (lock) {
  143. case 0:
  144. case 1:
  145. retval = set_lock_status(slot, lock);
  146. break;
  147. default:
  148. err ("%d is an invalid lock value\n", lock);
  149. retval = -EINVAL;
  150. }
  151. if (retval)
  152. return retval;
  153. return count;
  154. }
  155. static struct hotplug_slot_attribute hotplug_slot_attr_lock = {
  156. .attr = {.name = "lock", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  157. .show = lock_read_file,
  158. .store = lock_write_file
  159. };
  160. /**
  161. * release_slot - free up the memory used by a slot
  162. * @hotplug_slot: slot to free
  163. */
  164. static void release_slot(struct hotplug_slot *hotplug_slot)
  165. {
  166. struct slot *slot = hotplug_slot->private;
  167. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  168. kfree(slot->hotplug_slot->info);
  169. kfree(slot->hotplug_slot);
  170. kfree(slot);
  171. }
  172. static void make_slot_name(struct slot *slot)
  173. {
  174. if (pciehp_slot_with_bus)
  175. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
  176. slot->bus, slot->number);
  177. else
  178. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d",
  179. slot->number);
  180. }
  181. static int init_slots(struct controller *ctrl)
  182. {
  183. struct slot *slot;
  184. struct hotplug_slot *hotplug_slot;
  185. struct hotplug_slot_info *info;
  186. int retval = -ENOMEM;
  187. int i;
  188. for (i = 0; i < ctrl->num_slots; i++) {
  189. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  190. if (!slot)
  191. goto error;
  192. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  193. if (!hotplug_slot)
  194. goto error_slot;
  195. slot->hotplug_slot = hotplug_slot;
  196. info = kzalloc(sizeof(*info), GFP_KERNEL);
  197. if (!info)
  198. goto error_hpslot;
  199. hotplug_slot->info = info;
  200. hotplug_slot->name = slot->name;
  201. slot->hp_slot = i;
  202. slot->ctrl = ctrl;
  203. slot->bus = ctrl->pci_dev->subordinate->number;
  204. slot->device = ctrl->slot_device_offset + i;
  205. slot->hpc_ops = ctrl->hpc_ops;
  206. slot->number = ctrl->first_slot;
  207. mutex_init(&slot->lock);
  208. INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
  209. /* register this slot with the hotplug pci core */
  210. hotplug_slot->private = slot;
  211. hotplug_slot->release = &release_slot;
  212. make_slot_name(slot);
  213. hotplug_slot->ops = &pciehp_hotplug_slot_ops;
  214. get_power_status(hotplug_slot, &info->power_status);
  215. get_attention_status(hotplug_slot, &info->attention_status);
  216. get_latch_status(hotplug_slot, &info->latch_status);
  217. get_adapter_status(hotplug_slot, &info->adapter_status);
  218. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  219. "slot_device_offset=%x\n", slot->bus, slot->device,
  220. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  221. retval = pci_hp_register(hotplug_slot);
  222. if (retval) {
  223. err ("pci_hp_register failed with error %d\n", retval);
  224. goto error_info;
  225. }
  226. /* create additional sysfs entries */
  227. if (EMI(ctrl)) {
  228. retval = sysfs_create_file(&hotplug_slot->kobj,
  229. &hotplug_slot_attr_lock.attr);
  230. if (retval) {
  231. pci_hp_deregister(hotplug_slot);
  232. err("cannot create additional sysfs entries\n");
  233. goto error_info;
  234. }
  235. }
  236. list_add(&slot->slot_list, &ctrl->slot_list);
  237. }
  238. return 0;
  239. error_info:
  240. kfree(info);
  241. error_hpslot:
  242. kfree(hotplug_slot);
  243. error_slot:
  244. kfree(slot);
  245. error:
  246. return retval;
  247. }
  248. static void cleanup_slots(struct controller *ctrl)
  249. {
  250. struct list_head *tmp;
  251. struct list_head *next;
  252. struct slot *slot;
  253. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  254. slot = list_entry(tmp, struct slot, slot_list);
  255. list_del(&slot->slot_list);
  256. if (EMI(ctrl))
  257. sysfs_remove_file(&slot->hotplug_slot->kobj,
  258. &hotplug_slot_attr_lock.attr);
  259. cancel_delayed_work(&slot->work);
  260. flush_scheduled_work();
  261. flush_workqueue(pciehp_wq);
  262. pci_hp_deregister(slot->hotplug_slot);
  263. }
  264. }
  265. /*
  266. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  267. */
  268. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  269. {
  270. struct slot *slot = hotplug_slot->private;
  271. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  272. hotplug_slot->info->attention_status = status;
  273. if (ATTN_LED(slot->ctrl))
  274. slot->hpc_ops->set_attention_status(slot, status);
  275. return 0;
  276. }
  277. static int enable_slot(struct hotplug_slot *hotplug_slot)
  278. {
  279. struct slot *slot = hotplug_slot->private;
  280. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  281. return pciehp_sysfs_enable_slot(slot);
  282. }
  283. static int disable_slot(struct hotplug_slot *hotplug_slot)
  284. {
  285. struct slot *slot = hotplug_slot->private;
  286. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  287. return pciehp_sysfs_disable_slot(slot);
  288. }
  289. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  290. {
  291. struct slot *slot = hotplug_slot->private;
  292. int retval;
  293. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  294. retval = slot->hpc_ops->get_power_status(slot, value);
  295. if (retval < 0)
  296. *value = hotplug_slot->info->power_status;
  297. return 0;
  298. }
  299. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  300. {
  301. struct slot *slot = hotplug_slot->private;
  302. int retval;
  303. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  304. retval = slot->hpc_ops->get_attention_status(slot, value);
  305. if (retval < 0)
  306. *value = hotplug_slot->info->attention_status;
  307. return 0;
  308. }
  309. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  310. {
  311. struct slot *slot = hotplug_slot->private;
  312. int retval;
  313. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  314. retval = slot->hpc_ops->get_latch_status(slot, value);
  315. if (retval < 0)
  316. *value = hotplug_slot->info->latch_status;
  317. return 0;
  318. }
  319. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  320. {
  321. struct slot *slot = hotplug_slot->private;
  322. int retval;
  323. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  324. retval = slot->hpc_ops->get_adapter_status(slot, value);
  325. if (retval < 0)
  326. *value = hotplug_slot->info->adapter_status;
  327. return 0;
  328. }
  329. static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
  330. {
  331. struct slot *slot = hotplug_slot->private;
  332. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  333. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  334. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  335. return 0;
  336. }
  337. static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  338. {
  339. struct slot *slot = hotplug_slot->private;
  340. int retval;
  341. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  342. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  343. if (retval < 0)
  344. *value = PCI_SPEED_UNKNOWN;
  345. return 0;
  346. }
  347. static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  348. {
  349. struct slot *slot = hotplug_slot->private;
  350. int retval;
  351. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  352. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  353. if (retval < 0)
  354. *value = PCI_SPEED_UNKNOWN;
  355. return 0;
  356. }
  357. static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
  358. {
  359. int rc;
  360. struct controller *ctrl;
  361. struct slot *t_slot;
  362. u8 value;
  363. struct pci_dev *pdev;
  364. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  365. if (!ctrl) {
  366. err("%s : out of memory\n", __func__);
  367. goto err_out_none;
  368. }
  369. INIT_LIST_HEAD(&ctrl->slot_list);
  370. pdev = dev->port;
  371. ctrl->pci_dev = pdev;
  372. rc = pcie_init(ctrl, dev);
  373. if (rc) {
  374. dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
  375. goto err_out_free_ctrl;
  376. }
  377. pci_set_drvdata(pdev, ctrl);
  378. dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n",
  379. __func__, pdev->bus->number, PCI_SLOT(pdev->devfn),
  380. PCI_FUNC(pdev->devfn), pdev->irq);
  381. /* Setup the slot information structures */
  382. rc = init_slots(ctrl);
  383. if (rc) {
  384. err("%s: slot initialization failed\n", PCIE_MODULE_NAME);
  385. goto err_out_release_ctlr;
  386. }
  387. t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  388. t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
  389. if (value && pciehp_force) {
  390. rc = pciehp_enable_slot(t_slot);
  391. if (rc) /* -ENODEV: shouldn't happen, but deal with it */
  392. value = 0;
  393. }
  394. if ((POWER_CTRL(ctrl)) && !value) {
  395. rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
  396. if (rc)
  397. goto err_out_free_ctrl_slot;
  398. }
  399. return 0;
  400. err_out_free_ctrl_slot:
  401. cleanup_slots(ctrl);
  402. err_out_release_ctlr:
  403. ctrl->hpc_ops->release_ctlr(ctrl);
  404. err_out_free_ctrl:
  405. kfree(ctrl);
  406. err_out_none:
  407. return -ENODEV;
  408. }
  409. static void pciehp_remove (struct pcie_device *dev)
  410. {
  411. struct pci_dev *pdev = dev->port;
  412. struct controller *ctrl = pci_get_drvdata(pdev);
  413. cleanup_slots(ctrl);
  414. ctrl->hpc_ops->release_ctlr(ctrl);
  415. kfree(ctrl);
  416. }
  417. #ifdef CONFIG_PM
  418. static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
  419. {
  420. printk("%s ENTRY\n", __func__);
  421. return 0;
  422. }
  423. static int pciehp_resume (struct pcie_device *dev)
  424. {
  425. printk("%s ENTRY\n", __func__);
  426. if (pciehp_force) {
  427. struct pci_dev *pdev = dev->port;
  428. struct controller *ctrl = pci_get_drvdata(pdev);
  429. struct slot *t_slot;
  430. u8 status;
  431. /* reinitialize the chipset's event detection logic */
  432. pcie_init_hardware_part2(ctrl, dev);
  433. t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  434. /* Check if slot is occupied */
  435. t_slot->hpc_ops->get_adapter_status(t_slot, &status);
  436. if (status)
  437. pciehp_enable_slot(t_slot);
  438. else
  439. pciehp_disable_slot(t_slot);
  440. }
  441. return 0;
  442. }
  443. #endif
  444. static struct pcie_port_service_id port_pci_ids[] = { {
  445. .vendor = PCI_ANY_ID,
  446. .device = PCI_ANY_ID,
  447. .port_type = PCIE_ANY_PORT,
  448. .service_type = PCIE_PORT_SERVICE_HP,
  449. .driver_data = 0,
  450. }, { /* end: all zeroes */ }
  451. };
  452. static const char device_name[] = "hpdriver";
  453. static struct pcie_port_service_driver hpdriver_portdrv = {
  454. .name = (char *)device_name,
  455. .id_table = &port_pci_ids[0],
  456. .probe = pciehp_probe,
  457. .remove = pciehp_remove,
  458. #ifdef CONFIG_PM
  459. .suspend = pciehp_suspend,
  460. .resume = pciehp_resume,
  461. #endif /* PM */
  462. };
  463. static int __init pcied_init(void)
  464. {
  465. int retval = 0;
  466. retval = pcie_port_service_register(&hpdriver_portdrv);
  467. dbg("pcie_port_service_register = %d\n", retval);
  468. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  469. if (retval)
  470. dbg("%s: Failure to register service\n", __func__);
  471. return retval;
  472. }
  473. static void __exit pcied_cleanup(void)
  474. {
  475. dbg("unload_pciehpd()\n");
  476. pcie_port_service_unregister(&hpdriver_portdrv);
  477. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  478. }
  479. module_init(pcied_init);
  480. module_exit(pcied_cleanup);