pciehp_core.c 15 KB

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