shpchp_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/pci.h>
  34. #include "shpchp.h"
  35. /* Global variables */
  36. int shpchp_debug;
  37. int shpchp_poll_mode;
  38. int shpchp_poll_time;
  39. LIST_HEAD(shpchp_ctrl_list);
  40. #define DRIVER_VERSION "0.4"
  41. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  42. #define DRIVER_DESC "Standard Hot Plug PCI Controller Driver"
  43. MODULE_AUTHOR(DRIVER_AUTHOR);
  44. MODULE_DESCRIPTION(DRIVER_DESC);
  45. MODULE_LICENSE("GPL");
  46. module_param(shpchp_debug, bool, 0644);
  47. module_param(shpchp_poll_mode, bool, 0644);
  48. module_param(shpchp_poll_time, int, 0644);
  49. MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
  50. MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
  51. MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
  52. #define SHPC_MODULE_NAME "shpchp"
  53. static int shpc_start_thread (void);
  54. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  55. static int enable_slot (struct hotplug_slot *slot);
  56. static int disable_slot (struct hotplug_slot *slot);
  57. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  58. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  59. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  60. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  61. static int get_address (struct hotplug_slot *slot, u32 *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 shpchp_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_address = get_address,
  74. .get_max_bus_speed = get_max_bus_speed,
  75. .get_cur_bus_speed = get_cur_bus_speed,
  76. };
  77. /**
  78. * release_slot - free up the memory used by a slot
  79. * @hotplug_slot: slot to free
  80. */
  81. static void release_slot(struct hotplug_slot *hotplug_slot)
  82. {
  83. struct slot *slot = hotplug_slot->private;
  84. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  85. kfree(slot->hotplug_slot->info);
  86. kfree(slot->hotplug_slot);
  87. kfree(slot);
  88. }
  89. static void make_slot_name(struct slot *slot)
  90. {
  91. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
  92. slot->bus, slot->number);
  93. }
  94. static int init_slots(struct controller *ctrl)
  95. {
  96. struct slot *slot;
  97. struct hotplug_slot *hotplug_slot;
  98. struct hotplug_slot_info *info;
  99. int retval = -ENOMEM;
  100. int i;
  101. u32 sun;
  102. for (i = 0; i < ctrl->num_slots; i++) {
  103. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  104. if (!slot)
  105. goto error;
  106. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  107. if (!hotplug_slot)
  108. goto error_slot;
  109. slot->hotplug_slot = hotplug_slot;
  110. info = kzalloc(sizeof(*info), GFP_KERNEL);
  111. if (!info)
  112. goto error_hpslot;
  113. hotplug_slot->info = info;
  114. hotplug_slot->name = slot->name;
  115. slot->hp_slot = i;
  116. slot->ctrl = ctrl;
  117. slot->bus = ctrl->slot_bus;
  118. slot->device = ctrl->slot_device_offset + i;
  119. slot->hpc_ops = ctrl->hpc_ops;
  120. if (shpchprm_get_physical_slot_number(ctrl, &sun,
  121. slot->bus, slot->device))
  122. goto error_info;
  123. slot->number = sun;
  124. /* register this slot with the hotplug pci core */
  125. hotplug_slot->private = slot;
  126. hotplug_slot->release = &release_slot;
  127. make_slot_name(slot);
  128. hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  129. get_power_status(hotplug_slot, &info->power_status);
  130. get_attention_status(hotplug_slot, &info->attention_status);
  131. get_latch_status(hotplug_slot, &info->latch_status);
  132. get_adapter_status(hotplug_slot, &info->adapter_status);
  133. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  134. "slot_device_offset=%x\n", slot->bus, slot->device,
  135. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  136. retval = pci_hp_register(slot->hotplug_slot);
  137. if (retval) {
  138. err("pci_hp_register failed with error %d\n", retval);
  139. goto error_info;
  140. }
  141. list_add(&slot->slot_list, &ctrl->slot_list);
  142. }
  143. return 0;
  144. error_info:
  145. kfree(info);
  146. error_hpslot:
  147. kfree(hotplug_slot);
  148. error_slot:
  149. kfree(slot);
  150. error:
  151. return retval;
  152. }
  153. static void cleanup_slots(struct controller *ctrl)
  154. {
  155. struct list_head *tmp;
  156. struct list_head *next;
  157. struct slot *slot;
  158. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  159. slot = list_entry(tmp, struct slot, slot_list);
  160. list_del(&slot->slot_list);
  161. pci_hp_deregister(slot->hotplug_slot);
  162. }
  163. }
  164. static int get_ctlr_slot_config(struct controller *ctrl)
  165. {
  166. int num_ctlr_slots;
  167. int first_device_num;
  168. int physical_slot_num;
  169. int updown;
  170. int rc;
  171. int flags;
  172. rc = shpc_get_ctlr_slot_config(ctrl, &num_ctlr_slots,
  173. &first_device_num, &physical_slot_num,
  174. &updown, &flags);
  175. if (rc) {
  176. err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n",
  177. __FUNCTION__, ctrl->bus, ctrl->device);
  178. return -1;
  179. }
  180. ctrl->num_slots = num_ctlr_slots;
  181. ctrl->slot_device_offset = first_device_num;
  182. ctrl->first_slot = physical_slot_num;
  183. ctrl->slot_num_inc = updown; /* either -1 or 1 */
  184. dbg("%s: num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) updown(%d) for b:d "
  185. "(%x:%x)\n", __FUNCTION__, num_ctlr_slots, first_device_num,
  186. physical_slot_num, updown, ctrl->bus, ctrl->device);
  187. return 0;
  188. }
  189. /*
  190. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  191. */
  192. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  193. {
  194. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  195. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  196. hotplug_slot->info->attention_status = status;
  197. slot->hpc_ops->set_attention_status(slot, status);
  198. return 0;
  199. }
  200. static int enable_slot (struct hotplug_slot *hotplug_slot)
  201. {
  202. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  203. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  204. return shpchp_enable_slot(slot);
  205. }
  206. static int disable_slot (struct hotplug_slot *hotplug_slot)
  207. {
  208. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  209. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  210. return shpchp_disable_slot(slot);
  211. }
  212. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  213. {
  214. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  215. int retval;
  216. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  217. retval = slot->hpc_ops->get_power_status(slot, value);
  218. if (retval < 0)
  219. *value = hotplug_slot->info->power_status;
  220. return 0;
  221. }
  222. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  223. {
  224. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  225. int retval;
  226. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  227. retval = slot->hpc_ops->get_attention_status(slot, value);
  228. if (retval < 0)
  229. *value = hotplug_slot->info->attention_status;
  230. return 0;
  231. }
  232. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  233. {
  234. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  235. int retval;
  236. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  237. retval = slot->hpc_ops->get_latch_status(slot, value);
  238. if (retval < 0)
  239. *value = hotplug_slot->info->latch_status;
  240. return 0;
  241. }
  242. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  243. {
  244. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  245. int retval;
  246. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  247. retval = slot->hpc_ops->get_adapter_status(slot, value);
  248. if (retval < 0)
  249. *value = hotplug_slot->info->adapter_status;
  250. return 0;
  251. }
  252. static int get_address (struct hotplug_slot *hotplug_slot, u32 *value)
  253. {
  254. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  255. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  256. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  257. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  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 */
  295. int num_ctlr_slots; /* number of slots implemented */
  296. if (!is_shpc_capable(pdev))
  297. return -ENODEV;
  298. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  299. if (!ctrl) {
  300. err("%s : out of memory\n", __FUNCTION__);
  301. goto err_out_none;
  302. }
  303. INIT_LIST_HEAD(&ctrl->slot_list);
  304. rc = shpc_init(ctrl, pdev);
  305. if (rc) {
  306. dbg("%s: controller initialization failed\n",
  307. SHPC_MODULE_NAME);
  308. goto err_out_free_ctrl;
  309. }
  310. pci_set_drvdata(pdev, ctrl);
  311. ctrl->pci_bus = kmalloc(sizeof(*ctrl->pci_bus), GFP_KERNEL);
  312. if (!ctrl->pci_bus) {
  313. err("out of memory\n");
  314. rc = -ENOMEM;
  315. goto err_out_unmap_mmio_region;
  316. }
  317. memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
  318. ctrl->bus = pdev->bus->number;
  319. ctrl->slot_bus = pdev->subordinate->number;
  320. ctrl->device = PCI_SLOT(pdev->devfn);
  321. ctrl->function = PCI_FUNC(pdev->devfn);
  322. dbg("ctrl bus=0x%x, device=%x, function=%x, irq=%x\n",
  323. ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  324. /*
  325. * Save configuration headers for this and subordinate PCI buses
  326. */
  327. rc = get_ctlr_slot_config(ctrl);
  328. if (rc) {
  329. err(msg_initialization_err, rc);
  330. goto err_out_free_ctrl_bus;
  331. }
  332. first_device_num = ctrl->slot_device_offset;
  333. num_ctlr_slots = ctrl->num_slots;
  334. ctrl->add_support = 1;
  335. /* Setup the slot information structures */
  336. rc = init_slots(ctrl);
  337. if (rc) {
  338. err(msg_initialization_err, 6);
  339. goto err_out_free_ctrl_slot;
  340. }
  341. /* Now hpc_functions (slot->hpc_ops->functions) are ready */
  342. t_slot = shpchp_find_slot(ctrl, first_device_num);
  343. /* Check for operation bus speed */
  344. rc = t_slot->hpc_ops->get_cur_bus_speed(t_slot, &ctrl->speed);
  345. dbg("%s: t_slot->hp_slot %x\n", __FUNCTION__,t_slot->hp_slot);
  346. if (rc || ctrl->speed == PCI_SPEED_UNKNOWN) {
  347. err(SHPC_MODULE_NAME ": Can't get current bus speed. "
  348. "Set to 33MHz PCI.\n");
  349. ctrl->speed = PCI_SPEED_33MHz;
  350. }
  351. /* Finish setting up the hot plug ctrl device */
  352. ctrl->next_event = 0;
  353. list_add(&ctrl->ctrl_list, &shpchp_ctrl_list);
  354. shpchp_create_ctrl_files(ctrl);
  355. return 0;
  356. err_out_free_ctrl_slot:
  357. cleanup_slots(ctrl);
  358. err_out_free_ctrl_bus:
  359. kfree(ctrl->pci_bus);
  360. err_out_unmap_mmio_region:
  361. ctrl->hpc_ops->release_ctlr(ctrl);
  362. err_out_free_ctrl:
  363. kfree(ctrl);
  364. err_out_none:
  365. return -ENODEV;
  366. }
  367. static int shpc_start_thread(void)
  368. {
  369. int retval = 0;
  370. dbg("Initialize + Start the notification/polling mechanism \n");
  371. retval = shpchp_event_start_thread();
  372. if (retval) {
  373. dbg("shpchp_event_start_thread() failed\n");
  374. return retval;
  375. }
  376. return retval;
  377. }
  378. static void __exit unload_shpchpd(void)
  379. {
  380. struct list_head *tmp;
  381. struct list_head *next;
  382. struct controller *ctrl;
  383. list_for_each_safe(tmp, next, &shpchp_ctrl_list) {
  384. ctrl = list_entry(tmp, struct controller, ctrl_list);
  385. shpchp_remove_ctrl_files(ctrl);
  386. cleanup_slots(ctrl);
  387. kfree (ctrl->pci_bus);
  388. ctrl->hpc_ops->release_ctlr(ctrl);
  389. kfree(ctrl);
  390. }
  391. /* Stop the notification mechanism */
  392. shpchp_event_stop_thread();
  393. }
  394. static struct pci_device_id shpcd_pci_tbl[] = {
  395. {PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0)},
  396. { /* end: all zeroes */ }
  397. };
  398. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  399. static struct pci_driver shpc_driver = {
  400. .name = SHPC_MODULE_NAME,
  401. .id_table = shpcd_pci_tbl,
  402. .probe = shpc_probe,
  403. /* remove: shpc_remove_one, */
  404. };
  405. static int __init shpcd_init(void)
  406. {
  407. int retval = 0;
  408. #ifdef CONFIG_HOTPLUG_PCI_SHPC_POLL_EVENT_MODE
  409. shpchp_poll_mode = 1;
  410. #endif
  411. retval = shpc_start_thread();
  412. if (retval)
  413. goto error_hpc_init;
  414. retval = pci_register_driver(&shpc_driver);
  415. dbg("%s: pci_register_driver = %d\n", __FUNCTION__, retval);
  416. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  417. error_hpc_init:
  418. if (retval) {
  419. shpchp_event_stop_thread();
  420. }
  421. return retval;
  422. }
  423. static void __exit shpcd_cleanup(void)
  424. {
  425. dbg("unload_shpchpd()\n");
  426. unload_shpchpd();
  427. pci_unregister_driver(&shpc_driver);
  428. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  429. }
  430. module_init(shpcd_init);
  431. module_exit(shpcd_cleanup);