pciehp_core.c 15 KB

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