pciehp_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. /* Global variables */
  37. int pciehp_debug;
  38. int pciehp_poll_mode;
  39. int pciehp_poll_time;
  40. int pciehp_force;
  41. struct controller *pciehp_ctrl_list;
  42. #define DRIVER_VERSION "0.4"
  43. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  44. #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
  45. MODULE_AUTHOR(DRIVER_AUTHOR);
  46. MODULE_DESCRIPTION(DRIVER_DESC);
  47. MODULE_LICENSE("GPL");
  48. module_param(pciehp_debug, bool, 0644);
  49. module_param(pciehp_poll_mode, bool, 0644);
  50. module_param(pciehp_poll_time, int, 0644);
  51. module_param(pciehp_force, bool, 0644);
  52. MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
  53. MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
  54. MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
  55. MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
  56. #define PCIE_MODULE_NAME "pciehp"
  57. static int pcie_start_thread (void);
  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_address (struct hotplug_slot *slot, u32 *value);
  66. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  67. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  68. static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
  69. .owner = THIS_MODULE,
  70. .set_attention_status = set_attention_status,
  71. .enable_slot = enable_slot,
  72. .disable_slot = disable_slot,
  73. .get_power_status = get_power_status,
  74. .get_attention_status = get_attention_status,
  75. .get_latch_status = get_latch_status,
  76. .get_adapter_status = get_adapter_status,
  77. .get_address = get_address,
  78. .get_max_bus_speed = get_max_bus_speed,
  79. .get_cur_bus_speed = get_cur_bus_speed,
  80. };
  81. /**
  82. * release_slot - free up the memory used by a slot
  83. * @hotplug_slot: slot to free
  84. */
  85. static void release_slot(struct hotplug_slot *hotplug_slot)
  86. {
  87. struct slot *slot = hotplug_slot->private;
  88. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  89. kfree(slot->hotplug_slot->info);
  90. kfree(slot->hotplug_slot);
  91. kfree(slot);
  92. }
  93. static void make_slot_name(struct slot *slot)
  94. {
  95. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
  96. slot->bus, slot->number);
  97. }
  98. static int init_slots(struct controller *ctrl)
  99. {
  100. struct slot *slot;
  101. struct hotplug_slot *hotplug_slot;
  102. struct hotplug_slot_info *info;
  103. int retval = -ENOMEM;
  104. int i;
  105. for (i = 0; i < ctrl->num_slots; i++) {
  106. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  107. if (!slot)
  108. goto error;
  109. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  110. if (!hotplug_slot)
  111. goto error_slot;
  112. slot->hotplug_slot = hotplug_slot;
  113. info = kzalloc(sizeof(*info), GFP_KERNEL);
  114. if (!info)
  115. goto error_hpslot;
  116. hotplug_slot->info = info;
  117. hotplug_slot->name = slot->name;
  118. slot->hp_slot = i;
  119. slot->ctrl = ctrl;
  120. slot->bus = ctrl->pci_dev->subordinate->number;
  121. slot->device = ctrl->slot_device_offset + i;
  122. slot->hpc_ops = ctrl->hpc_ops;
  123. slot->number = ctrl->first_slot;
  124. /* register this slot with the hotplug pci core */
  125. hotplug_slot->private = slot;
  126. hotplug_slot->release = &release_slot;
  127. make_slot_name(slot);
  128. hotplug_slot->ops = &pciehp_hotplug_slot_ops;
  129. get_power_status(hotplug_slot, &info->power_status);
  130. get_attention_status(hotplug_slot, &info->attention_status);
  131. get_latch_status(hotplug_slot, &info->latch_status);
  132. get_adapter_status(hotplug_slot, &info->adapter_status);
  133. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  134. "slot_device_offset=%x\n", slot->bus, slot->device,
  135. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  136. retval = pci_hp_register(hotplug_slot);
  137. if (retval) {
  138. err ("pci_hp_register failed with error %d\n", retval);
  139. goto error_info;
  140. }
  141. list_add(&slot->slot_list, &ctrl->slot_list);
  142. }
  143. return 0;
  144. error_info:
  145. kfree(info);
  146. error_hpslot:
  147. kfree(hotplug_slot);
  148. error_slot:
  149. kfree(slot);
  150. error:
  151. return retval;
  152. }
  153. static void cleanup_slots(struct controller *ctrl)
  154. {
  155. struct list_head *tmp;
  156. struct list_head *next;
  157. struct slot *slot;
  158. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  159. slot = list_entry(tmp, struct slot, slot_list);
  160. list_del(&slot->slot_list);
  161. pci_hp_deregister(slot->hotplug_slot);
  162. }
  163. }
  164. /*
  165. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  166. */
  167. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  168. {
  169. struct slot *slot = hotplug_slot->private;
  170. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  171. hotplug_slot->info->attention_status = status;
  172. if (ATTN_LED(slot->ctrl->ctrlcap))
  173. slot->hpc_ops->set_attention_status(slot, status);
  174. return 0;
  175. }
  176. static int enable_slot(struct hotplug_slot *hotplug_slot)
  177. {
  178. struct slot *slot = hotplug_slot->private;
  179. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  180. return pciehp_enable_slot(slot);
  181. }
  182. static int disable_slot(struct hotplug_slot *hotplug_slot)
  183. {
  184. struct slot *slot = hotplug_slot->private;
  185. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  186. return pciehp_disable_slot(slot);
  187. }
  188. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  189. {
  190. struct slot *slot = hotplug_slot->private;
  191. int retval;
  192. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  193. retval = slot->hpc_ops->get_power_status(slot, value);
  194. if (retval < 0)
  195. *value = hotplug_slot->info->power_status;
  196. return 0;
  197. }
  198. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  199. {
  200. struct slot *slot = hotplug_slot->private;
  201. int retval;
  202. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  203. retval = slot->hpc_ops->get_attention_status(slot, value);
  204. if (retval < 0)
  205. *value = hotplug_slot->info->attention_status;
  206. return 0;
  207. }
  208. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  209. {
  210. struct slot *slot = hotplug_slot->private;
  211. int retval;
  212. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  213. retval = slot->hpc_ops->get_latch_status(slot, value);
  214. if (retval < 0)
  215. *value = hotplug_slot->info->latch_status;
  216. return 0;
  217. }
  218. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  219. {
  220. struct slot *slot = hotplug_slot->private;
  221. int retval;
  222. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  223. retval = slot->hpc_ops->get_adapter_status(slot, value);
  224. if (retval < 0)
  225. *value = hotplug_slot->info->adapter_status;
  226. return 0;
  227. }
  228. static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
  229. {
  230. struct slot *slot = hotplug_slot->private;
  231. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  232. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  233. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  234. return 0;
  235. }
  236. static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  237. {
  238. struct slot *slot = hotplug_slot->private;
  239. int retval;
  240. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  241. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  242. if (retval < 0)
  243. *value = PCI_SPEED_UNKNOWN;
  244. return 0;
  245. }
  246. static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  247. {
  248. struct slot *slot = hotplug_slot->private;
  249. int retval;
  250. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  251. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  252. if (retval < 0)
  253. *value = PCI_SPEED_UNKNOWN;
  254. return 0;
  255. }
  256. static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
  257. {
  258. int rc;
  259. struct controller *ctrl;
  260. struct slot *t_slot;
  261. u8 value;
  262. struct pci_dev *pdev;
  263. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  264. if (!ctrl) {
  265. err("%s : out of memory\n", __FUNCTION__);
  266. goto err_out_none;
  267. }
  268. INIT_LIST_HEAD(&ctrl->slot_list);
  269. pdev = dev->port;
  270. ctrl->pci_dev = pdev;
  271. rc = pcie_init(ctrl, dev);
  272. if (rc) {
  273. dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
  274. goto err_out_free_ctrl;
  275. }
  276. pci_set_drvdata(pdev, ctrl);
  277. ctrl->bus = pdev->bus->number; /* ctrl bus */
  278. ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
  279. ctrl->device = PCI_SLOT(pdev->devfn);
  280. ctrl->function = PCI_FUNC(pdev->devfn);
  281. dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
  282. ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  283. /* Setup the slot information structures */
  284. rc = init_slots(ctrl);
  285. if (rc) {
  286. err("%s: slot initialization failed\n", PCIE_MODULE_NAME);
  287. goto err_out_release_ctlr;
  288. }
  289. t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  290. /* Finish setting up the hot plug ctrl device */
  291. ctrl->next_event = 0;
  292. if (!pciehp_ctrl_list) {
  293. pciehp_ctrl_list = ctrl;
  294. ctrl->next = NULL;
  295. } else {
  296. ctrl->next = pciehp_ctrl_list;
  297. pciehp_ctrl_list = ctrl;
  298. }
  299. /* Wait for exclusive access to hardware */
  300. mutex_lock(&ctrl->ctrl_lock);
  301. t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
  302. if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
  303. rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
  304. if (rc) {
  305. /* Done with exclusive hardware access */
  306. mutex_unlock(&ctrl->ctrl_lock);
  307. goto err_out_free_ctrl_slot;
  308. } else
  309. /* Wait for the command to complete */
  310. wait_for_ctrl_irq (ctrl);
  311. }
  312. /* Done with exclusive hardware access */
  313. mutex_unlock(&ctrl->ctrl_lock);
  314. return 0;
  315. err_out_free_ctrl_slot:
  316. cleanup_slots(ctrl);
  317. err_out_release_ctlr:
  318. ctrl->hpc_ops->release_ctlr(ctrl);
  319. err_out_free_ctrl:
  320. kfree(ctrl);
  321. err_out_none:
  322. return -ENODEV;
  323. }
  324. static int pcie_start_thread(void)
  325. {
  326. int retval = 0;
  327. dbg("Initialize + Start the notification/polling mechanism \n");
  328. retval = pciehp_event_start_thread();
  329. if (retval) {
  330. dbg("pciehp_event_start_thread() failed\n");
  331. return retval;
  332. }
  333. return retval;
  334. }
  335. static void __exit unload_pciehpd(void)
  336. {
  337. struct controller *ctrl;
  338. struct controller *tctrl;
  339. ctrl = pciehp_ctrl_list;
  340. while (ctrl) {
  341. cleanup_slots(ctrl);
  342. ctrl->hpc_ops->release_ctlr(ctrl);
  343. tctrl = ctrl;
  344. ctrl = ctrl->next;
  345. kfree(tctrl);
  346. }
  347. /* Stop the notification mechanism */
  348. pciehp_event_stop_thread();
  349. }
  350. static void pciehp_remove (struct pcie_device *device)
  351. {
  352. /* XXX - Needs to be adapted to device driver model */
  353. }
  354. #ifdef CONFIG_PM
  355. static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
  356. {
  357. printk("%s ENTRY\n", __FUNCTION__);
  358. return 0;
  359. }
  360. static int pciehp_resume (struct pcie_device *dev)
  361. {
  362. printk("%s ENTRY\n", __FUNCTION__);
  363. return 0;
  364. }
  365. #endif
  366. static struct pcie_port_service_id port_pci_ids[] = { {
  367. .vendor = PCI_ANY_ID,
  368. .device = PCI_ANY_ID,
  369. .port_type = PCIE_ANY_PORT,
  370. .service_type = PCIE_PORT_SERVICE_HP,
  371. .driver_data = 0,
  372. }, { /* end: all zeroes */ }
  373. };
  374. static const char device_name[] = "hpdriver";
  375. static struct pcie_port_service_driver hpdriver_portdrv = {
  376. .name = (char *)device_name,
  377. .id_table = &port_pci_ids[0],
  378. .probe = pciehp_probe,
  379. .remove = pciehp_remove,
  380. #ifdef CONFIG_PM
  381. .suspend = pciehp_suspend,
  382. .resume = pciehp_resume,
  383. #endif /* PM */
  384. };
  385. static int __init pcied_init(void)
  386. {
  387. int retval = 0;
  388. #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
  389. pciehp_poll_mode = 1;
  390. #endif
  391. retval = pcie_start_thread();
  392. if (retval)
  393. goto error_hpc_init;
  394. retval = pcie_port_service_register(&hpdriver_portdrv);
  395. dbg("pcie_port_service_register = %d\n", retval);
  396. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  397. if (retval)
  398. dbg("%s: Failure to register service\n", __FUNCTION__);
  399. error_hpc_init:
  400. if (retval) {
  401. pciehp_event_stop_thread();
  402. };
  403. return retval;
  404. }
  405. static void __exit pcied_cleanup(void)
  406. {
  407. dbg("unload_pciehpd()\n");
  408. unload_pciehpd();
  409. pcie_port_service_unregister(&hpdriver_portdrv);
  410. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  411. }
  412. module_init(pcied_init);
  413. module_exit(pcied_cleanup);