pciehp_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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. #include <linux/time.h>
  37. /* Global variables */
  38. int pciehp_debug;
  39. int pciehp_poll_mode;
  40. int pciehp_poll_time;
  41. int pciehp_force;
  42. struct controller *pciehp_ctrl_list;
  43. #define DRIVER_VERSION "0.4"
  44. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  45. #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
  46. MODULE_AUTHOR(DRIVER_AUTHOR);
  47. MODULE_DESCRIPTION(DRIVER_DESC);
  48. MODULE_LICENSE("GPL");
  49. module_param(pciehp_debug, bool, 0644);
  50. module_param(pciehp_poll_mode, bool, 0644);
  51. module_param(pciehp_poll_time, int, 0644);
  52. module_param(pciehp_force, bool, 0644);
  53. MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
  54. MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
  55. MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
  56. MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
  57. #define PCIE_MODULE_NAME "pciehp"
  58. static int pcie_start_thread (void);
  59. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  60. static int enable_slot (struct hotplug_slot *slot);
  61. static int disable_slot (struct hotplug_slot *slot);
  62. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  63. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  66. static int get_address (struct hotplug_slot *slot, u32 *value);
  67. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  68. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  69. static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
  70. .owner = THIS_MODULE,
  71. .set_attention_status = set_attention_status,
  72. .enable_slot = enable_slot,
  73. .disable_slot = disable_slot,
  74. .get_power_status = get_power_status,
  75. .get_attention_status = get_attention_status,
  76. .get_latch_status = get_latch_status,
  77. .get_adapter_status = get_adapter_status,
  78. .get_address = get_address,
  79. .get_max_bus_speed = get_max_bus_speed,
  80. .get_cur_bus_speed = get_cur_bus_speed,
  81. };
  82. /*
  83. * Check the status of the Electro Mechanical Interlock (EMI)
  84. */
  85. static int get_lock_status(struct hotplug_slot *hotplug_slot, u8 *value)
  86. {
  87. struct slot *slot = hotplug_slot->private;
  88. return (slot->hpc_ops->get_emi_status(slot, value));
  89. }
  90. /*
  91. * sysfs interface for the Electro Mechanical Interlock (EMI)
  92. * 1 == locked, 0 == unlocked
  93. */
  94. static ssize_t lock_read_file(struct hotplug_slot *slot, char *buf)
  95. {
  96. int retval;
  97. u8 value;
  98. retval = get_lock_status(slot, &value);
  99. if (retval)
  100. goto lock_read_exit;
  101. retval = sprintf (buf, "%d\n", value);
  102. lock_read_exit:
  103. return retval;
  104. }
  105. /*
  106. * Change the status of the Electro Mechanical Interlock (EMI)
  107. * This is a toggle - in addition there must be at least 1 second
  108. * in between toggles.
  109. */
  110. static int set_lock_status(struct hotplug_slot *hotplug_slot, u8 status)
  111. {
  112. struct slot *slot = hotplug_slot->private;
  113. int retval;
  114. u8 value;
  115. mutex_lock(&slot->ctrl->crit_sect);
  116. /* has it been >1 sec since our last toggle? */
  117. if ((get_seconds() - slot->last_emi_toggle) < 1)
  118. return -EINVAL;
  119. /* see what our current state is */
  120. retval = get_lock_status(hotplug_slot, &value);
  121. if (retval || (value == status))
  122. goto set_lock_exit;
  123. slot->hpc_ops->toggle_emi(slot);
  124. set_lock_exit:
  125. mutex_unlock(&slot->ctrl->crit_sect);
  126. return 0;
  127. }
  128. /*
  129. * sysfs interface which allows the user to toggle the Electro Mechanical
  130. * Interlock. Valid values are either 0 or 1. 0 == unlock, 1 == lock
  131. */
  132. static ssize_t lock_write_file(struct hotplug_slot *slot, const char *buf,
  133. size_t count)
  134. {
  135. unsigned long llock;
  136. u8 lock;
  137. int retval = 0;
  138. llock = simple_strtoul(buf, NULL, 10);
  139. lock = (u8)(llock & 0xff);
  140. switch (lock) {
  141. case 0:
  142. case 1:
  143. retval = set_lock_status(slot, lock);
  144. break;
  145. default:
  146. err ("%d is an invalid lock value\n", lock);
  147. retval = -EINVAL;
  148. }
  149. if (retval)
  150. return retval;
  151. return count;
  152. }
  153. static struct hotplug_slot_attribute hotplug_slot_attr_lock = {
  154. .attr = {.name = "lock", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  155. .show = lock_read_file,
  156. .store = lock_write_file
  157. };
  158. /**
  159. * release_slot - free up the memory used by a slot
  160. * @hotplug_slot: slot to free
  161. */
  162. static void release_slot(struct hotplug_slot *hotplug_slot)
  163. {
  164. struct slot *slot = hotplug_slot->private;
  165. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  166. kfree(slot->hotplug_slot->info);
  167. kfree(slot->hotplug_slot);
  168. kfree(slot);
  169. }
  170. static void make_slot_name(struct slot *slot)
  171. {
  172. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
  173. slot->bus, slot->number);
  174. }
  175. static int init_slots(struct controller *ctrl)
  176. {
  177. struct slot *slot;
  178. struct hotplug_slot *hotplug_slot;
  179. struct hotplug_slot_info *info;
  180. int retval = -ENOMEM;
  181. int i;
  182. for (i = 0; i < ctrl->num_slots; i++) {
  183. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  184. if (!slot)
  185. goto error;
  186. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  187. if (!hotplug_slot)
  188. goto error_slot;
  189. slot->hotplug_slot = hotplug_slot;
  190. info = kzalloc(sizeof(*info), GFP_KERNEL);
  191. if (!info)
  192. goto error_hpslot;
  193. hotplug_slot->info = info;
  194. hotplug_slot->name = slot->name;
  195. slot->hp_slot = i;
  196. slot->ctrl = ctrl;
  197. slot->bus = ctrl->pci_dev->subordinate->number;
  198. slot->device = ctrl->slot_device_offset + i;
  199. slot->hpc_ops = ctrl->hpc_ops;
  200. slot->number = ctrl->first_slot;
  201. /* register this slot with the hotplug pci core */
  202. hotplug_slot->private = slot;
  203. hotplug_slot->release = &release_slot;
  204. make_slot_name(slot);
  205. hotplug_slot->ops = &pciehp_hotplug_slot_ops;
  206. get_power_status(hotplug_slot, &info->power_status);
  207. get_attention_status(hotplug_slot, &info->attention_status);
  208. get_latch_status(hotplug_slot, &info->latch_status);
  209. get_adapter_status(hotplug_slot, &info->adapter_status);
  210. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  211. "slot_device_offset=%x\n", slot->bus, slot->device,
  212. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  213. retval = pci_hp_register(hotplug_slot);
  214. if (retval) {
  215. err ("pci_hp_register failed with error %d\n", retval);
  216. goto error_info;
  217. }
  218. /* create additional sysfs entries */
  219. if (EMI(ctrl->ctrlcap)) {
  220. retval = sysfs_create_file(&hotplug_slot->kobj,
  221. &hotplug_slot_attr_lock.attr);
  222. if (retval) {
  223. pci_hp_deregister(hotplug_slot);
  224. err("cannot create additional sysfs entries\n");
  225. goto error_info;
  226. }
  227. }
  228. list_add(&slot->slot_list, &ctrl->slot_list);
  229. }
  230. return 0;
  231. error_info:
  232. kfree(info);
  233. error_hpslot:
  234. kfree(hotplug_slot);
  235. error_slot:
  236. kfree(slot);
  237. error:
  238. return retval;
  239. }
  240. static void cleanup_slots(struct controller *ctrl)
  241. {
  242. struct list_head *tmp;
  243. struct list_head *next;
  244. struct slot *slot;
  245. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  246. slot = list_entry(tmp, struct slot, slot_list);
  247. list_del(&slot->slot_list);
  248. if (EMI(ctrl->ctrlcap))
  249. sysfs_remove_file(&slot->hotplug_slot->kobj,
  250. &hotplug_slot_attr_lock.attr);
  251. pci_hp_deregister(slot->hotplug_slot);
  252. }
  253. }
  254. /*
  255. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  256. */
  257. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  258. {
  259. struct slot *slot = hotplug_slot->private;
  260. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  261. hotplug_slot->info->attention_status = status;
  262. if (ATTN_LED(slot->ctrl->ctrlcap))
  263. slot->hpc_ops->set_attention_status(slot, status);
  264. return 0;
  265. }
  266. static int enable_slot(struct hotplug_slot *hotplug_slot)
  267. {
  268. struct slot *slot = hotplug_slot->private;
  269. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  270. return pciehp_enable_slot(slot);
  271. }
  272. static int disable_slot(struct hotplug_slot *hotplug_slot)
  273. {
  274. struct slot *slot = hotplug_slot->private;
  275. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  276. return pciehp_disable_slot(slot);
  277. }
  278. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  279. {
  280. struct slot *slot = hotplug_slot->private;
  281. int retval;
  282. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  283. retval = slot->hpc_ops->get_power_status(slot, value);
  284. if (retval < 0)
  285. *value = hotplug_slot->info->power_status;
  286. return 0;
  287. }
  288. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  289. {
  290. struct slot *slot = hotplug_slot->private;
  291. int retval;
  292. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  293. retval = slot->hpc_ops->get_attention_status(slot, value);
  294. if (retval < 0)
  295. *value = hotplug_slot->info->attention_status;
  296. return 0;
  297. }
  298. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  299. {
  300. struct slot *slot = hotplug_slot->private;
  301. int retval;
  302. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  303. retval = slot->hpc_ops->get_latch_status(slot, value);
  304. if (retval < 0)
  305. *value = hotplug_slot->info->latch_status;
  306. return 0;
  307. }
  308. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  309. {
  310. struct slot *slot = hotplug_slot->private;
  311. int retval;
  312. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  313. retval = slot->hpc_ops->get_adapter_status(slot, value);
  314. if (retval < 0)
  315. *value = hotplug_slot->info->adapter_status;
  316. return 0;
  317. }
  318. static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
  319. {
  320. struct slot *slot = hotplug_slot->private;
  321. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  322. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  323. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  324. return 0;
  325. }
  326. static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  327. {
  328. struct slot *slot = hotplug_slot->private;
  329. int retval;
  330. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  331. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  332. if (retval < 0)
  333. *value = PCI_SPEED_UNKNOWN;
  334. return 0;
  335. }
  336. static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  337. {
  338. struct slot *slot = hotplug_slot->private;
  339. int retval;
  340. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  341. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  342. if (retval < 0)
  343. *value = PCI_SPEED_UNKNOWN;
  344. return 0;
  345. }
  346. static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
  347. {
  348. int rc;
  349. struct controller *ctrl;
  350. struct slot *t_slot;
  351. u8 value;
  352. struct pci_dev *pdev;
  353. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  354. if (!ctrl) {
  355. err("%s : out of memory\n", __FUNCTION__);
  356. goto err_out_none;
  357. }
  358. INIT_LIST_HEAD(&ctrl->slot_list);
  359. pdev = dev->port;
  360. ctrl->pci_dev = pdev;
  361. rc = pcie_init(ctrl, dev);
  362. if (rc) {
  363. dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
  364. goto err_out_free_ctrl;
  365. }
  366. pci_set_drvdata(pdev, ctrl);
  367. ctrl->bus = pdev->bus->number; /* ctrl bus */
  368. ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
  369. ctrl->device = PCI_SLOT(pdev->devfn);
  370. ctrl->function = PCI_FUNC(pdev->devfn);
  371. dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
  372. ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  373. /* Setup the slot information structures */
  374. rc = init_slots(ctrl);
  375. if (rc) {
  376. err("%s: slot initialization failed\n", PCIE_MODULE_NAME);
  377. goto err_out_release_ctlr;
  378. }
  379. t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
  380. /* Finish setting up the hot plug ctrl device */
  381. ctrl->next_event = 0;
  382. if (!pciehp_ctrl_list) {
  383. pciehp_ctrl_list = ctrl;
  384. ctrl->next = NULL;
  385. } else {
  386. ctrl->next = pciehp_ctrl_list;
  387. pciehp_ctrl_list = ctrl;
  388. }
  389. t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
  390. if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
  391. rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
  392. if (rc)
  393. goto err_out_free_ctrl_slot;
  394. }
  395. return 0;
  396. err_out_free_ctrl_slot:
  397. cleanup_slots(ctrl);
  398. err_out_release_ctlr:
  399. ctrl->hpc_ops->release_ctlr(ctrl);
  400. err_out_free_ctrl:
  401. kfree(ctrl);
  402. err_out_none:
  403. return -ENODEV;
  404. }
  405. static int pcie_start_thread(void)
  406. {
  407. int retval = 0;
  408. dbg("Initialize + Start the notification/polling mechanism \n");
  409. retval = pciehp_event_start_thread();
  410. if (retval) {
  411. dbg("pciehp_event_start_thread() failed\n");
  412. return retval;
  413. }
  414. return retval;
  415. }
  416. static void __exit unload_pciehpd(void)
  417. {
  418. struct controller *ctrl;
  419. struct controller *tctrl;
  420. ctrl = pciehp_ctrl_list;
  421. while (ctrl) {
  422. cleanup_slots(ctrl);
  423. ctrl->hpc_ops->release_ctlr(ctrl);
  424. tctrl = ctrl;
  425. ctrl = ctrl->next;
  426. kfree(tctrl);
  427. }
  428. /* Stop the notification mechanism */
  429. pciehp_event_stop_thread();
  430. }
  431. static void pciehp_remove (struct pcie_device *device)
  432. {
  433. /* XXX - Needs to be adapted to device driver model */
  434. }
  435. #ifdef CONFIG_PM
  436. static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
  437. {
  438. printk("%s ENTRY\n", __FUNCTION__);
  439. return 0;
  440. }
  441. static int pciehp_resume (struct pcie_device *dev)
  442. {
  443. printk("%s ENTRY\n", __FUNCTION__);
  444. return 0;
  445. }
  446. #endif
  447. static struct pcie_port_service_id port_pci_ids[] = { {
  448. .vendor = PCI_ANY_ID,
  449. .device = PCI_ANY_ID,
  450. .port_type = PCIE_ANY_PORT,
  451. .service_type = PCIE_PORT_SERVICE_HP,
  452. .driver_data = 0,
  453. }, { /* end: all zeroes */ }
  454. };
  455. static const char device_name[] = "hpdriver";
  456. static struct pcie_port_service_driver hpdriver_portdrv = {
  457. .name = (char *)device_name,
  458. .id_table = &port_pci_ids[0],
  459. .probe = pciehp_probe,
  460. .remove = pciehp_remove,
  461. #ifdef CONFIG_PM
  462. .suspend = pciehp_suspend,
  463. .resume = pciehp_resume,
  464. #endif /* PM */
  465. };
  466. static int __init pcied_init(void)
  467. {
  468. int retval = 0;
  469. #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
  470. pciehp_poll_mode = 1;
  471. #endif
  472. retval = pcie_start_thread();
  473. if (retval)
  474. goto error_hpc_init;
  475. retval = pcie_port_service_register(&hpdriver_portdrv);
  476. dbg("pcie_port_service_register = %d\n", retval);
  477. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  478. if (retval)
  479. dbg("%s: Failure to register service\n", __FUNCTION__);
  480. error_hpc_init:
  481. if (retval) {
  482. pciehp_event_stop_thread();
  483. };
  484. return retval;
  485. }
  486. static void __exit pcied_cleanup(void)
  487. {
  488. dbg("unload_pciehpd()\n");
  489. unload_pciehpd();
  490. pcie_port_service_unregister(&hpdriver_portdrv);
  491. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  492. }
  493. module_init(pcied_init);
  494. module_exit(pcied_cleanup);