pciehp_core.c 16 KB

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