pciehp_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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->name);
  91. kfree(slot->hotplug_slot);
  92. kfree(slot);
  93. }
  94. static int init_slots(struct controller *ctrl)
  95. {
  96. struct slot *slot;
  97. struct hpc_ops *hpc_ops;
  98. struct hotplug_slot *hotplug_slot;
  99. struct hotplug_slot_info *hotplug_slot_info;
  100. u8 number_of_slots;
  101. u8 slot_device;
  102. u32 slot_number;
  103. int result = -ENOMEM;
  104. number_of_slots = ctrl->num_slots;
  105. slot_device = ctrl->slot_device_offset;
  106. slot_number = ctrl->first_slot;
  107. while (number_of_slots) {
  108. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  109. if (!slot)
  110. goto error;
  111. slot->hotplug_slot =
  112. kzalloc(sizeof(*(slot->hotplug_slot)),
  113. GFP_KERNEL);
  114. if (!slot->hotplug_slot)
  115. goto error_slot;
  116. hotplug_slot = slot->hotplug_slot;
  117. hotplug_slot->info =
  118. kzalloc(sizeof(*(hotplug_slot->info)),
  119. GFP_KERNEL);
  120. if (!hotplug_slot->info)
  121. goto error_hpslot;
  122. hotplug_slot_info = hotplug_slot->info;
  123. hotplug_slot->name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
  124. if (!hotplug_slot->name)
  125. goto error_info;
  126. slot->ctrl = ctrl;
  127. slot->bus = ctrl->slot_bus;
  128. slot->device = slot_device;
  129. slot->hpc_ops = hpc_ops = ctrl->hpc_ops;
  130. slot->number = ctrl->first_slot;
  131. slot->hp_slot = slot_device - ctrl->slot_device_offset;
  132. /* register this slot with the hotplug pci core */
  133. hotplug_slot->private = slot;
  134. hotplug_slot->release = &release_slot;
  135. make_slot_name(hotplug_slot->name, SLOT_NAME_SIZE, slot);
  136. hotplug_slot->ops = &pciehp_hotplug_slot_ops;
  137. hpc_ops->get_power_status(slot,
  138. &(hotplug_slot_info->power_status));
  139. hpc_ops->get_attention_status(slot,
  140. &(hotplug_slot_info->attention_status));
  141. hpc_ops->get_latch_status(slot,
  142. &(hotplug_slot_info->latch_status));
  143. hpc_ops->get_adapter_status(slot,
  144. &(hotplug_slot_info->adapter_status));
  145. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  146. "slot_device_offset=%x\n",
  147. slot->bus, slot->device, slot->hp_slot, slot->number,
  148. ctrl->slot_device_offset);
  149. result = pci_hp_register(hotplug_slot);
  150. if (result) {
  151. err ("pci_hp_register failed with error %d\n", result);
  152. goto error_name;
  153. }
  154. slot->next = ctrl->slot;
  155. ctrl->slot = slot;
  156. number_of_slots--;
  157. slot_device++;
  158. slot_number += ctrl->slot_num_inc;
  159. }
  160. return 0;
  161. error_name:
  162. kfree(hotplug_slot->name);
  163. error_info:
  164. kfree(hotplug_slot_info);
  165. error_hpslot:
  166. kfree(hotplug_slot);
  167. error_slot:
  168. kfree(slot);
  169. error:
  170. return result;
  171. }
  172. static int cleanup_slots (struct controller * ctrl)
  173. {
  174. struct slot *old_slot, *next_slot;
  175. old_slot = ctrl->slot;
  176. ctrl->slot = NULL;
  177. while (old_slot) {
  178. next_slot = old_slot->next;
  179. pci_hp_deregister (old_slot->hotplug_slot);
  180. old_slot = next_slot;
  181. }
  182. return(0);
  183. }
  184. static int get_ctlr_slot_config(struct controller *ctrl)
  185. {
  186. int num_ctlr_slots; /* Not needed; PCI Express has 1 slot per port*/
  187. int first_device_num; /* Not needed */
  188. int physical_slot_num;
  189. u8 ctrlcap;
  190. int rc;
  191. rc = pcie_get_ctlr_slot_config(ctrl, &num_ctlr_slots, &first_device_num, &physical_slot_num, &ctrlcap);
  192. if (rc) {
  193. err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n", __FUNCTION__, ctrl->bus, ctrl->device);
  194. return (-1);
  195. }
  196. ctrl->num_slots = num_ctlr_slots; /* PCI Express has 1 slot per port */
  197. ctrl->slot_device_offset = first_device_num;
  198. ctrl->first_slot = physical_slot_num;
  199. ctrl->ctrlcap = ctrlcap;
  200. dbg("%s: bus(0x%x) num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) ctrlcap(%x) for b:d (%x:%x)\n",
  201. __FUNCTION__, ctrl->slot_bus, num_ctlr_slots, first_device_num, physical_slot_num, ctrlcap,
  202. ctrl->bus, ctrl->device);
  203. return (0);
  204. }
  205. /*
  206. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  207. */
  208. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  209. {
  210. struct slot *slot = hotplug_slot->private;
  211. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  212. hotplug_slot->info->attention_status = status;
  213. if (ATTN_LED(slot->ctrl->ctrlcap))
  214. slot->hpc_ops->set_attention_status(slot, status);
  215. return 0;
  216. }
  217. static int enable_slot(struct hotplug_slot *hotplug_slot)
  218. {
  219. struct slot *slot = hotplug_slot->private;
  220. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  221. return pciehp_enable_slot(slot);
  222. }
  223. static int disable_slot(struct hotplug_slot *hotplug_slot)
  224. {
  225. struct slot *slot = hotplug_slot->private;
  226. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  227. return pciehp_disable_slot(slot);
  228. }
  229. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  230. {
  231. struct slot *slot = hotplug_slot->private;
  232. int retval;
  233. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  234. retval = slot->hpc_ops->get_power_status(slot, value);
  235. if (retval < 0)
  236. *value = hotplug_slot->info->power_status;
  237. return 0;
  238. }
  239. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  240. {
  241. struct slot *slot = hotplug_slot->private;
  242. int retval;
  243. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  244. retval = slot->hpc_ops->get_attention_status(slot, value);
  245. if (retval < 0)
  246. *value = hotplug_slot->info->attention_status;
  247. return 0;
  248. }
  249. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  250. {
  251. struct slot *slot = hotplug_slot->private;
  252. int retval;
  253. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  254. retval = slot->hpc_ops->get_latch_status(slot, value);
  255. if (retval < 0)
  256. *value = hotplug_slot->info->latch_status;
  257. return 0;
  258. }
  259. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  260. {
  261. struct slot *slot = hotplug_slot->private;
  262. int retval;
  263. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  264. retval = slot->hpc_ops->get_adapter_status(slot, value);
  265. if (retval < 0)
  266. *value = hotplug_slot->info->adapter_status;
  267. return 0;
  268. }
  269. static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
  270. {
  271. struct slot *slot = hotplug_slot->private;
  272. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  273. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  274. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  275. return 0;
  276. }
  277. static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  278. {
  279. struct slot *slot = hotplug_slot->private;
  280. int retval;
  281. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  282. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  283. if (retval < 0)
  284. *value = PCI_SPEED_UNKNOWN;
  285. return 0;
  286. }
  287. static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  288. {
  289. struct slot *slot = hotplug_slot->private;
  290. int retval;
  291. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  292. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  293. if (retval < 0)
  294. *value = PCI_SPEED_UNKNOWN;
  295. return 0;
  296. }
  297. static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
  298. {
  299. int rc;
  300. struct controller *ctrl;
  301. struct slot *t_slot;
  302. int first_device_num = 0 ; /* first PCI device number supported by this PCIE */
  303. int num_ctlr_slots; /* number of slots supported by this HPC */
  304. u8 value;
  305. struct pci_dev *pdev;
  306. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  307. if (!ctrl) {
  308. err("%s : out of memory\n", __FUNCTION__);
  309. goto err_out_none;
  310. }
  311. pdev = dev->port;
  312. ctrl->pci_dev = pdev;
  313. rc = pcie_init(ctrl, dev);
  314. if (rc) {
  315. dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
  316. goto err_out_free_ctrl;
  317. }
  318. pci_set_drvdata(pdev, ctrl);
  319. ctrl->pci_bus = kmalloc(sizeof(*ctrl->pci_bus), GFP_KERNEL);
  320. if (!ctrl->pci_bus) {
  321. err("%s: out of memory\n", __FUNCTION__);
  322. rc = -ENOMEM;
  323. goto err_out_unmap_mmio_region;
  324. }
  325. memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
  326. ctrl->bus = pdev->bus->number; /* ctrl bus */
  327. ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
  328. ctrl->device = PCI_SLOT(pdev->devfn);
  329. ctrl->function = PCI_FUNC(pdev->devfn);
  330. dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
  331. ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  332. /*
  333. * Save configuration headers for this and subordinate PCI buses
  334. */
  335. rc = get_ctlr_slot_config(ctrl);
  336. if (rc) {
  337. err(msg_initialization_err, rc);
  338. goto err_out_free_ctrl_bus;
  339. }
  340. first_device_num = ctrl->slot_device_offset;
  341. num_ctlr_slots = ctrl->num_slots;
  342. /* Setup the slot information structures */
  343. rc = init_slots(ctrl);
  344. if (rc) {
  345. err(msg_initialization_err, 6);
  346. goto err_out_free_ctrl_slot;
  347. }
  348. t_slot = pciehp_find_slot(ctrl, first_device_num);
  349. /* Finish setting up the hot plug ctrl device */
  350. ctrl->next_event = 0;
  351. if (!pciehp_ctrl_list) {
  352. pciehp_ctrl_list = ctrl;
  353. ctrl->next = NULL;
  354. } else {
  355. ctrl->next = pciehp_ctrl_list;
  356. pciehp_ctrl_list = ctrl;
  357. }
  358. /* Wait for exclusive access to hardware */
  359. mutex_lock(&ctrl->ctrl_lock);
  360. t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
  361. if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
  362. rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
  363. if (rc) {
  364. /* Done with exclusive hardware access */
  365. mutex_unlock(&ctrl->ctrl_lock);
  366. goto err_out_free_ctrl_slot;
  367. } else
  368. /* Wait for the command to complete */
  369. wait_for_ctrl_irq (ctrl);
  370. }
  371. /* Done with exclusive hardware access */
  372. mutex_unlock(&ctrl->ctrl_lock);
  373. return 0;
  374. err_out_free_ctrl_slot:
  375. cleanup_slots(ctrl);
  376. err_out_free_ctrl_bus:
  377. kfree(ctrl->pci_bus);
  378. err_out_unmap_mmio_region:
  379. ctrl->hpc_ops->release_ctlr(ctrl);
  380. err_out_free_ctrl:
  381. kfree(ctrl);
  382. err_out_none:
  383. return -ENODEV;
  384. }
  385. static int pcie_start_thread(void)
  386. {
  387. int retval = 0;
  388. dbg("Initialize + Start the notification/polling mechanism \n");
  389. retval = pciehp_event_start_thread();
  390. if (retval) {
  391. dbg("pciehp_event_start_thread() failed\n");
  392. return retval;
  393. }
  394. return retval;
  395. }
  396. static void __exit unload_pciehpd(void)
  397. {
  398. struct controller *ctrl;
  399. struct controller *tctrl;
  400. ctrl = pciehp_ctrl_list;
  401. while (ctrl) {
  402. cleanup_slots(ctrl);
  403. kfree (ctrl->pci_bus);
  404. ctrl->hpc_ops->release_ctlr(ctrl);
  405. tctrl = ctrl;
  406. ctrl = ctrl->next;
  407. kfree(tctrl);
  408. }
  409. /* Stop the notification mechanism */
  410. pciehp_event_stop_thread();
  411. }
  412. static int hpdriver_context = 0;
  413. static void pciehp_remove (struct pcie_device *device)
  414. {
  415. printk("%s ENTRY\n", __FUNCTION__);
  416. printk("%s -> Call free_irq for irq = %d\n",
  417. __FUNCTION__, device->irq);
  418. free_irq(device->irq, &hpdriver_context);
  419. }
  420. #ifdef CONFIG_PM
  421. static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
  422. {
  423. printk("%s ENTRY\n", __FUNCTION__);
  424. return 0;
  425. }
  426. static int pciehp_resume (struct pcie_device *dev)
  427. {
  428. printk("%s ENTRY\n", __FUNCTION__);
  429. return 0;
  430. }
  431. #endif
  432. static struct pcie_port_service_id port_pci_ids[] = { {
  433. .vendor = PCI_ANY_ID,
  434. .device = PCI_ANY_ID,
  435. .port_type = PCIE_ANY_PORT,
  436. .service_type = PCIE_PORT_SERVICE_HP,
  437. .driver_data = 0,
  438. }, { /* end: all zeroes */ }
  439. };
  440. static const char device_name[] = "hpdriver";
  441. static struct pcie_port_service_driver hpdriver_portdrv = {
  442. .name = (char *)device_name,
  443. .id_table = &port_pci_ids[0],
  444. .probe = pciehp_probe,
  445. .remove = pciehp_remove,
  446. #ifdef CONFIG_PM
  447. .suspend = pciehp_suspend,
  448. .resume = pciehp_resume,
  449. #endif /* PM */
  450. };
  451. static int __init pcied_init(void)
  452. {
  453. int retval = 0;
  454. #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
  455. pciehp_poll_mode = 1;
  456. #endif
  457. retval = pcie_start_thread();
  458. if (retval)
  459. goto error_hpc_init;
  460. retval = pcie_port_service_register(&hpdriver_portdrv);
  461. dbg("pcie_port_service_register = %d\n", retval);
  462. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  463. if (retval)
  464. dbg("%s: Failure to register service\n", __FUNCTION__);
  465. error_hpc_init:
  466. if (retval) {
  467. pciehp_event_stop_thread();
  468. };
  469. return retval;
  470. }
  471. static void __exit pcied_cleanup(void)
  472. {
  473. dbg("unload_pciehpd()\n");
  474. unload_pciehpd();
  475. pcie_port_service_unregister(&hpdriver_portdrv);
  476. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  477. }
  478. module_init(pcied_init);
  479. module_exit(pcied_cleanup);