shpchp_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Standard 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 "shpchp.h"
  41. /* Global variables */
  42. int shpchp_debug;
  43. int shpchp_poll_mode;
  44. int shpchp_poll_time;
  45. struct controller *shpchp_ctrl_list; /* = NULL */
  46. #define DRIVER_VERSION "0.4"
  47. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  48. #define DRIVER_DESC "Standard Hot Plug PCI Controller Driver"
  49. MODULE_AUTHOR(DRIVER_AUTHOR);
  50. MODULE_DESCRIPTION(DRIVER_DESC);
  51. MODULE_LICENSE("GPL");
  52. module_param(shpchp_debug, bool, 0644);
  53. module_param(shpchp_poll_mode, bool, 0644);
  54. module_param(shpchp_poll_time, int, 0644);
  55. MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
  56. MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
  57. MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
  58. #define SHPC_MODULE_NAME "shpchp"
  59. static int shpc_start_thread (void);
  60. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  61. static int enable_slot (struct hotplug_slot *slot);
  62. static int disable_slot (struct hotplug_slot *slot);
  63. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  66. static int get_adapter_status (struct hotplug_slot *slot, u8 *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 shpchp_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_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 *new_slot;
  97. u8 number_of_slots;
  98. u8 slot_device;
  99. u32 slot_number, sun;
  100. int result = -ENOMEM;
  101. dbg("%s\n",__FUNCTION__);
  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. new_slot = (struct slot *) kmalloc(sizeof(struct slot), GFP_KERNEL);
  107. if (!new_slot)
  108. goto error;
  109. memset(new_slot, 0, sizeof(struct slot));
  110. new_slot->hotplug_slot = kmalloc (sizeof (struct hotplug_slot), GFP_KERNEL);
  111. if (!new_slot->hotplug_slot)
  112. goto error_slot;
  113. memset(new_slot->hotplug_slot, 0, sizeof (struct hotplug_slot));
  114. new_slot->hotplug_slot->info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
  115. if (!new_slot->hotplug_slot->info)
  116. goto error_hpslot;
  117. memset(new_slot->hotplug_slot->info, 0, sizeof (struct hotplug_slot_info));
  118. new_slot->hotplug_slot->name = kmalloc (SLOT_NAME_SIZE, GFP_KERNEL);
  119. if (!new_slot->hotplug_slot->name)
  120. goto error_info;
  121. new_slot->magic = SLOT_MAGIC;
  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. if (shpchprm_get_physical_slot_number(ctrl, &sun,
  127. new_slot->bus, new_slot->device))
  128. goto error_name;
  129. new_slot->number = sun;
  130. new_slot->hp_slot = slot_device - ctrl->slot_device_offset;
  131. /* register this slot with the hotplug pci core */
  132. new_slot->hotplug_slot->private = new_slot;
  133. new_slot->hotplug_slot->release = &release_slot;
  134. make_slot_name(new_slot->hotplug_slot->name, SLOT_NAME_SIZE, new_slot);
  135. new_slot->hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  136. new_slot->hpc_ops->get_power_status(new_slot, &(new_slot->hotplug_slot->info->power_status));
  137. new_slot->hpc_ops->get_attention_status(new_slot, &(new_slot->hotplug_slot->info->attention_status));
  138. new_slot->hpc_ops->get_latch_status(new_slot, &(new_slot->hotplug_slot->info->latch_status));
  139. new_slot->hpc_ops->get_adapter_status(new_slot, &(new_slot->hotplug_slot->info->adapter_status));
  140. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x slot_device_offset=%x\n", new_slot->bus,
  141. new_slot->device, new_slot->hp_slot, new_slot->number, ctrl->slot_device_offset);
  142. result = pci_hp_register (new_slot->hotplug_slot);
  143. if (result) {
  144. err ("pci_hp_register failed with error %d\n", result);
  145. goto error_name;
  146. }
  147. new_slot->next = ctrl->slot;
  148. ctrl->slot = new_slot;
  149. number_of_slots--;
  150. slot_device++;
  151. slot_number += ctrl->slot_num_inc;
  152. }
  153. return 0;
  154. error_name:
  155. kfree(new_slot->hotplug_slot->name);
  156. error_info:
  157. kfree(new_slot->hotplug_slot->info);
  158. error_hpslot:
  159. kfree(new_slot->hotplug_slot);
  160. error_slot:
  161. kfree(new_slot);
  162. error:
  163. return result;
  164. }
  165. static void cleanup_slots(struct controller *ctrl)
  166. {
  167. struct slot *old_slot, *next_slot;
  168. old_slot = ctrl->slot;
  169. ctrl->slot = NULL;
  170. while (old_slot) {
  171. next_slot = old_slot->next;
  172. pci_hp_deregister(old_slot->hotplug_slot);
  173. old_slot = next_slot;
  174. }
  175. }
  176. static int get_ctlr_slot_config(struct controller *ctrl)
  177. {
  178. int num_ctlr_slots;
  179. int first_device_num;
  180. int physical_slot_num;
  181. int updown;
  182. int rc;
  183. int flags;
  184. rc = shpc_get_ctlr_slot_config(ctrl, &num_ctlr_slots, &first_device_num, &physical_slot_num, &updown, &flags);
  185. if (rc) {
  186. err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n", __FUNCTION__, ctrl->bus, ctrl->device);
  187. return -1;
  188. }
  189. ctrl->num_slots = num_ctlr_slots;
  190. ctrl->slot_device_offset = first_device_num;
  191. ctrl->first_slot = physical_slot_num;
  192. ctrl->slot_num_inc = updown; /* either -1 or 1 */
  193. dbg("%s: num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) updown(%d) for b:d (%x:%x)\n",
  194. __FUNCTION__, num_ctlr_slots, first_device_num, physical_slot_num, updown, ctrl->bus, ctrl->device);
  195. return 0;
  196. }
  197. /*
  198. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  199. */
  200. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  201. {
  202. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  203. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  204. hotplug_slot->info->attention_status = status;
  205. slot->hpc_ops->set_attention_status(slot, status);
  206. return 0;
  207. }
  208. static int enable_slot (struct hotplug_slot *hotplug_slot)
  209. {
  210. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  211. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  212. return shpchp_enable_slot(slot);
  213. }
  214. static int disable_slot (struct hotplug_slot *hotplug_slot)
  215. {
  216. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  217. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  218. return shpchp_disable_slot(slot);
  219. }
  220. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  221. {
  222. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  223. int retval;
  224. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  225. retval = slot->hpc_ops->get_power_status(slot, value);
  226. if (retval < 0)
  227. *value = hotplug_slot->info->power_status;
  228. return 0;
  229. }
  230. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  231. {
  232. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  233. int retval;
  234. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  235. retval = slot->hpc_ops->get_attention_status(slot, value);
  236. if (retval < 0)
  237. *value = hotplug_slot->info->attention_status;
  238. return 0;
  239. }
  240. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  241. {
  242. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  243. int retval;
  244. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  245. retval = slot->hpc_ops->get_latch_status(slot, value);
  246. if (retval < 0)
  247. *value = hotplug_slot->info->latch_status;
  248. return 0;
  249. }
  250. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  251. {
  252. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  253. int retval;
  254. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  255. retval = slot->hpc_ops->get_adapter_status(slot, value);
  256. if (retval < 0)
  257. *value = hotplug_slot->info->adapter_status;
  258. return 0;
  259. }
  260. static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  261. {
  262. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  263. int retval;
  264. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  265. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  266. if (retval < 0)
  267. *value = PCI_SPEED_UNKNOWN;
  268. return 0;
  269. }
  270. static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  271. {
  272. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  273. int retval;
  274. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  275. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  276. if (retval < 0)
  277. *value = PCI_SPEED_UNKNOWN;
  278. return 0;
  279. }
  280. static int is_shpc_capable(struct pci_dev *dev)
  281. {
  282. if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device ==
  283. PCI_DEVICE_ID_AMD_GOLAM_7450))
  284. return 1;
  285. if (pci_find_capability(dev, PCI_CAP_ID_SHPC))
  286. return 1;
  287. return 0;
  288. }
  289. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  290. {
  291. int rc;
  292. struct controller *ctrl;
  293. struct slot *t_slot;
  294. int first_device_num; /* first PCI device number supported by this SHPC */
  295. int num_ctlr_slots; /* number of slots supported by this SHPC */
  296. if (!is_shpc_capable(pdev))
  297. return -ENODEV;
  298. ctrl = (struct controller *) kmalloc(sizeof(struct controller), GFP_KERNEL);
  299. if (!ctrl) {
  300. err("%s : out of memory\n", __FUNCTION__);
  301. goto err_out_none;
  302. }
  303. memset(ctrl, 0, sizeof(struct controller));
  304. dbg("DRV_thread pid = %d\n", current->pid);
  305. rc = shpc_init(ctrl, pdev,
  306. (php_intr_callback_t) shpchp_handle_attention_button,
  307. (php_intr_callback_t) shpchp_handle_switch_change,
  308. (php_intr_callback_t) shpchp_handle_presence_change,
  309. (php_intr_callback_t) shpchp_handle_power_fault);
  310. if (rc) {
  311. dbg("%s: controller initialization failed\n", SHPC_MODULE_NAME);
  312. goto err_out_free_ctrl;
  313. }
  314. dbg("%s: controller initialization success\n", __FUNCTION__);
  315. ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
  316. pci_set_drvdata(pdev, ctrl);
  317. ctrl->pci_bus = kmalloc (sizeof (*ctrl->pci_bus), GFP_KERNEL);
  318. if (!ctrl->pci_bus) {
  319. err("out of memory\n");
  320. rc = -ENOMEM;
  321. goto err_out_unmap_mmio_region;
  322. }
  323. memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
  324. ctrl->bus = pdev->bus->number;
  325. ctrl->slot_bus = pdev->subordinate->number;
  326. ctrl->device = PCI_SLOT(pdev->devfn);
  327. ctrl->function = PCI_FUNC(pdev->devfn);
  328. dbg("ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  329. /*
  330. * Save configuration headers for this and subordinate PCI buses
  331. */
  332. rc = get_ctlr_slot_config(ctrl);
  333. if (rc) {
  334. err(msg_initialization_err, rc);
  335. goto err_out_free_ctrl_bus;
  336. }
  337. first_device_num = ctrl->slot_device_offset;
  338. num_ctlr_slots = ctrl->num_slots;
  339. ctrl->add_support = 1;
  340. /* Setup the slot information structures */
  341. rc = init_slots(ctrl);
  342. if (rc) {
  343. err(msg_initialization_err, 6);
  344. goto err_out_free_ctrl_slot;
  345. }
  346. /* Now hpc_functions (slot->hpc_ops->functions) are ready */
  347. t_slot = shpchp_find_slot(ctrl, first_device_num);
  348. /* Check for operation bus speed */
  349. rc = t_slot->hpc_ops->get_cur_bus_speed(t_slot, &ctrl->speed);
  350. dbg("%s: t_slot->hp_slot %x\n", __FUNCTION__,t_slot->hp_slot);
  351. if (rc || ctrl->speed == PCI_SPEED_UNKNOWN) {
  352. err(SHPC_MODULE_NAME ": Can't get current bus speed. Set to 33MHz PCI.\n");
  353. ctrl->speed = PCI_SPEED_33MHz;
  354. }
  355. /* Finish setting up the hot plug ctrl device */
  356. ctrl->next_event = 0;
  357. if (!shpchp_ctrl_list) {
  358. shpchp_ctrl_list = ctrl;
  359. ctrl->next = NULL;
  360. } else {
  361. ctrl->next = shpchp_ctrl_list;
  362. shpchp_ctrl_list = ctrl;
  363. }
  364. shpchp_create_ctrl_files(ctrl);
  365. return 0;
  366. err_out_free_ctrl_slot:
  367. cleanup_slots(ctrl);
  368. err_out_free_ctrl_bus:
  369. kfree(ctrl->pci_bus);
  370. err_out_unmap_mmio_region:
  371. ctrl->hpc_ops->release_ctlr(ctrl);
  372. err_out_free_ctrl:
  373. kfree(ctrl);
  374. err_out_none:
  375. return -ENODEV;
  376. }
  377. static int shpc_start_thread(void)
  378. {
  379. int retval = 0;
  380. dbg("Initialize + Start the notification/polling mechanism \n");
  381. retval = shpchp_event_start_thread();
  382. if (retval) {
  383. dbg("shpchp_event_start_thread() failed\n");
  384. return retval;
  385. }
  386. return retval;
  387. }
  388. static void __exit unload_shpchpd(void)
  389. {
  390. struct controller *ctrl;
  391. struct controller *tctrl;
  392. ctrl = shpchp_ctrl_list;
  393. while (ctrl) {
  394. cleanup_slots(ctrl);
  395. kfree (ctrl->pci_bus);
  396. dbg("%s: calling release_ctlr\n", __FUNCTION__);
  397. ctrl->hpc_ops->release_ctlr(ctrl);
  398. tctrl = ctrl;
  399. ctrl = ctrl->next;
  400. kfree(tctrl);
  401. }
  402. /* Stop the notification mechanism */
  403. shpchp_event_stop_thread();
  404. }
  405. static struct pci_device_id shpcd_pci_tbl[] = {
  406. {
  407. .class = ((PCI_CLASS_BRIDGE_PCI << 8) | 0x00),
  408. .class_mask = ~0,
  409. .vendor = PCI_ANY_ID,
  410. .device = PCI_ANY_ID,
  411. .subvendor = PCI_ANY_ID,
  412. .subdevice = PCI_ANY_ID,
  413. },
  414. { /* end: all zeroes */ }
  415. };
  416. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  417. static struct pci_driver shpc_driver = {
  418. .name = SHPC_MODULE_NAME,
  419. .id_table = shpcd_pci_tbl,
  420. .probe = shpc_probe,
  421. /* remove: shpc_remove_one, */
  422. };
  423. static int __init shpcd_init(void)
  424. {
  425. int retval = 0;
  426. #ifdef CONFIG_HOTPLUG_PCI_SHPC_POLL_EVENT_MODE
  427. shpchp_poll_mode = 1;
  428. #endif
  429. retval = shpc_start_thread();
  430. if (retval)
  431. goto error_hpc_init;
  432. retval = pci_register_driver(&shpc_driver);
  433. dbg("%s: pci_register_driver = %d\n", __FUNCTION__, retval);
  434. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  435. error_hpc_init:
  436. if (retval) {
  437. shpchp_event_stop_thread();
  438. }
  439. return retval;
  440. }
  441. static void __exit shpcd_cleanup(void)
  442. {
  443. dbg("unload_shpchpd()\n");
  444. unload_shpchpd();
  445. dbg("pci_unregister_driver\n");
  446. pci_unregister_driver(&shpc_driver);
  447. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  448. }
  449. module_init(shpcd_init);
  450. module_exit(shpcd_cleanup);