shpchp_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 <linux/workqueue.h>
  35. #include "shpchp.h"
  36. /* Global variables */
  37. int shpchp_debug;
  38. int shpchp_poll_mode;
  39. int shpchp_poll_time;
  40. LIST_HEAD(shpchp_ctrl_list);
  41. struct workqueue_struct *shpchp_wq;
  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 "Standard Hot Plug PCI Controller Driver"
  45. MODULE_AUTHOR(DRIVER_AUTHOR);
  46. MODULE_DESCRIPTION(DRIVER_DESC);
  47. MODULE_LICENSE("GPL");
  48. module_param(shpchp_debug, bool, 0644);
  49. module_param(shpchp_poll_mode, bool, 0644);
  50. module_param(shpchp_poll_time, int, 0644);
  51. MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
  52. MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
  53. MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
  54. #define SHPC_MODULE_NAME "shpchp"
  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_address (struct hotplug_slot *slot, u32 *value);
  63. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  64. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  65. static struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
  66. .owner = THIS_MODULE,
  67. .set_attention_status = set_attention_status,
  68. .enable_slot = enable_slot,
  69. .disable_slot = disable_slot,
  70. .get_power_status = get_power_status,
  71. .get_attention_status = get_attention_status,
  72. .get_latch_status = get_latch_status,
  73. .get_adapter_status = get_adapter_status,
  74. .get_address = get_address,
  75. .get_max_bus_speed = get_max_bus_speed,
  76. .get_cur_bus_speed = get_cur_bus_speed,
  77. };
  78. /**
  79. * release_slot - free up the memory used by a slot
  80. * @hotplug_slot: slot to free
  81. */
  82. static void release_slot(struct hotplug_slot *hotplug_slot)
  83. {
  84. struct slot *slot = hotplug_slot->private;
  85. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  86. kfree(slot->hotplug_slot->info);
  87. kfree(slot->hotplug_slot);
  88. kfree(slot);
  89. }
  90. static void make_slot_name(struct slot *slot)
  91. {
  92. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
  93. slot->bus, slot->number);
  94. }
  95. static int init_slots(struct controller *ctrl)
  96. {
  97. struct slot *slot;
  98. struct hotplug_slot *hotplug_slot;
  99. struct hotplug_slot_info *info;
  100. int retval = -ENOMEM;
  101. int i;
  102. u32 sun;
  103. for (i = 0; i < ctrl->num_slots; i++) {
  104. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  105. if (!slot)
  106. goto error;
  107. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  108. if (!hotplug_slot)
  109. goto error_slot;
  110. slot->hotplug_slot = hotplug_slot;
  111. info = kzalloc(sizeof(*info), GFP_KERNEL);
  112. if (!info)
  113. goto error_hpslot;
  114. hotplug_slot->info = info;
  115. hotplug_slot->name = slot->name;
  116. slot->hp_slot = i;
  117. slot->ctrl = ctrl;
  118. slot->bus = ctrl->slot_bus;
  119. slot->device = ctrl->slot_device_offset + i;
  120. slot->hpc_ops = ctrl->hpc_ops;
  121. mutex_init(&slot->lock);
  122. if (shpchprm_get_physical_slot_number(ctrl, &sun,
  123. slot->bus, slot->device))
  124. goto error_info;
  125. slot->number = sun;
  126. INIT_WORK(&slot->work, queue_pushbutton_work, slot);
  127. /* register this slot with the hotplug pci core */
  128. hotplug_slot->private = slot;
  129. hotplug_slot->release = &release_slot;
  130. make_slot_name(slot);
  131. hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  132. get_power_status(hotplug_slot, &info->power_status);
  133. get_attention_status(hotplug_slot, &info->attention_status);
  134. get_latch_status(hotplug_slot, &info->latch_status);
  135. get_adapter_status(hotplug_slot, &info->adapter_status);
  136. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  137. "slot_device_offset=%x\n", slot->bus, slot->device,
  138. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  139. retval = pci_hp_register(slot->hotplug_slot);
  140. if (retval) {
  141. err("pci_hp_register failed with error %d\n", retval);
  142. goto error_info;
  143. }
  144. list_add(&slot->slot_list, &ctrl->slot_list);
  145. }
  146. return 0;
  147. error_info:
  148. kfree(info);
  149. error_hpslot:
  150. kfree(hotplug_slot);
  151. error_slot:
  152. kfree(slot);
  153. error:
  154. return retval;
  155. }
  156. void cleanup_slots(struct controller *ctrl)
  157. {
  158. struct list_head *tmp;
  159. struct list_head *next;
  160. struct slot *slot;
  161. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  162. slot = list_entry(tmp, struct slot, slot_list);
  163. list_del(&slot->slot_list);
  164. cancel_delayed_work(&slot->work);
  165. flush_scheduled_work();
  166. flush_workqueue(shpchp_wq);
  167. pci_hp_deregister(slot->hotplug_slot);
  168. }
  169. }
  170. static int get_ctlr_slot_config(struct controller *ctrl)
  171. {
  172. int num_ctlr_slots;
  173. int first_device_num;
  174. int physical_slot_num;
  175. int updown;
  176. int rc;
  177. int flags;
  178. rc = shpc_get_ctlr_slot_config(ctrl, &num_ctlr_slots,
  179. &first_device_num, &physical_slot_num,
  180. &updown, &flags);
  181. if (rc) {
  182. err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n",
  183. __FUNCTION__, ctrl->bus, ctrl->device);
  184. return -1;
  185. }
  186. ctrl->num_slots = num_ctlr_slots;
  187. ctrl->slot_device_offset = first_device_num;
  188. ctrl->first_slot = physical_slot_num;
  189. ctrl->slot_num_inc = updown; /* either -1 or 1 */
  190. dbg("%s: num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) updown(%d) for b:d "
  191. "(%x:%x)\n", __FUNCTION__, num_ctlr_slots, first_device_num,
  192. physical_slot_num, updown, 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 = get_slot(hotplug_slot, __FUNCTION__);
  201. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  202. hotplug_slot->info->attention_status = status;
  203. slot->hpc_ops->set_attention_status(slot, status);
  204. return 0;
  205. }
  206. static int enable_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_sysfs_enable_slot(slot);
  211. }
  212. static int disable_slot (struct hotplug_slot *hotplug_slot)
  213. {
  214. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  215. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  216. return shpchp_sysfs_disable_slot(slot);
  217. }
  218. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  219. {
  220. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  221. int retval;
  222. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  223. retval = slot->hpc_ops->get_power_status(slot, value);
  224. if (retval < 0)
  225. *value = hotplug_slot->info->power_status;
  226. return 0;
  227. }
  228. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  229. {
  230. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  231. int retval;
  232. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  233. retval = slot->hpc_ops->get_attention_status(slot, value);
  234. if (retval < 0)
  235. *value = hotplug_slot->info->attention_status;
  236. return 0;
  237. }
  238. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  239. {
  240. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  241. int retval;
  242. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  243. retval = slot->hpc_ops->get_latch_status(slot, value);
  244. if (retval < 0)
  245. *value = hotplug_slot->info->latch_status;
  246. return 0;
  247. }
  248. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  249. {
  250. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  251. int retval;
  252. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  253. retval = slot->hpc_ops->get_adapter_status(slot, value);
  254. if (retval < 0)
  255. *value = hotplug_slot->info->adapter_status;
  256. return 0;
  257. }
  258. static int get_address (struct hotplug_slot *hotplug_slot, u32 *value)
  259. {
  260. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  261. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  262. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  263. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  264. return 0;
  265. }
  266. static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  267. {
  268. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  269. int retval;
  270. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  271. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  272. if (retval < 0)
  273. *value = PCI_SPEED_UNKNOWN;
  274. return 0;
  275. }
  276. static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  277. {
  278. struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
  279. int retval;
  280. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  281. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  282. if (retval < 0)
  283. *value = PCI_SPEED_UNKNOWN;
  284. return 0;
  285. }
  286. static int is_shpc_capable(struct pci_dev *dev)
  287. {
  288. if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device ==
  289. PCI_DEVICE_ID_AMD_GOLAM_7450))
  290. return 1;
  291. if (pci_find_capability(dev, PCI_CAP_ID_SHPC))
  292. return 1;
  293. return 0;
  294. }
  295. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  296. {
  297. int rc;
  298. struct controller *ctrl;
  299. struct slot *t_slot;
  300. int first_device_num; /* first PCI device number */
  301. int num_ctlr_slots; /* number of slots implemented */
  302. if (!is_shpc_capable(pdev))
  303. return -ENODEV;
  304. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  305. if (!ctrl) {
  306. err("%s : out of memory\n", __FUNCTION__);
  307. goto err_out_none;
  308. }
  309. INIT_LIST_HEAD(&ctrl->slot_list);
  310. rc = shpc_init(ctrl, pdev);
  311. if (rc) {
  312. dbg("%s: controller initialization failed\n",
  313. SHPC_MODULE_NAME);
  314. goto err_out_free_ctrl;
  315. }
  316. pci_set_drvdata(pdev, ctrl);
  317. ctrl->bus = pdev->bus->number;
  318. ctrl->slot_bus = pdev->subordinate->number;
  319. ctrl->device = PCI_SLOT(pdev->devfn);
  320. ctrl->function = PCI_FUNC(pdev->devfn);
  321. dbg("ctrl bus=0x%x, device=%x, function=%x, irq=%x\n",
  322. ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  323. /*
  324. * Save configuration headers for this and subordinate PCI buses
  325. */
  326. rc = get_ctlr_slot_config(ctrl);
  327. if (rc) {
  328. err(msg_initialization_err, rc);
  329. goto err_out_release_ctlr;
  330. }
  331. first_device_num = ctrl->slot_device_offset;
  332. num_ctlr_slots = ctrl->num_slots;
  333. ctrl->add_support = 1;
  334. /* Setup the slot information structures */
  335. rc = init_slots(ctrl);
  336. if (rc) {
  337. err(msg_initialization_err, 6);
  338. goto err_out_release_ctlr;
  339. }
  340. /* Now hpc_functions (slot->hpc_ops->functions) are ready */
  341. t_slot = shpchp_find_slot(ctrl, first_device_num);
  342. /* Check for operation bus speed */
  343. rc = t_slot->hpc_ops->get_cur_bus_speed(t_slot, &ctrl->speed);
  344. dbg("%s: t_slot->hp_slot %x\n", __FUNCTION__,t_slot->hp_slot);
  345. if (rc || ctrl->speed == PCI_SPEED_UNKNOWN) {
  346. err(SHPC_MODULE_NAME ": Can't get current bus speed. "
  347. "Set to 33MHz PCI.\n");
  348. ctrl->speed = PCI_SPEED_33MHz;
  349. }
  350. list_add(&ctrl->ctrl_list, &shpchp_ctrl_list);
  351. shpchp_create_ctrl_files(ctrl);
  352. return 0;
  353. err_out_release_ctlr:
  354. ctrl->hpc_ops->release_ctlr(ctrl);
  355. err_out_free_ctrl:
  356. kfree(ctrl);
  357. err_out_none:
  358. return -ENODEV;
  359. }
  360. static void __exit unload_shpchpd(void)
  361. {
  362. struct list_head *tmp;
  363. struct list_head *next;
  364. struct controller *ctrl;
  365. list_for_each_safe(tmp, next, &shpchp_ctrl_list) {
  366. ctrl = list_entry(tmp, struct controller, ctrl_list);
  367. shpchp_remove_ctrl_files(ctrl);
  368. ctrl->hpc_ops->release_ctlr(ctrl);
  369. kfree(ctrl);
  370. }
  371. destroy_workqueue(shpchp_wq);
  372. }
  373. static struct pci_device_id shpcd_pci_tbl[] = {
  374. {PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0)},
  375. { /* end: all zeroes */ }
  376. };
  377. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  378. static struct pci_driver shpc_driver = {
  379. .name = SHPC_MODULE_NAME,
  380. .id_table = shpcd_pci_tbl,
  381. .probe = shpc_probe,
  382. /* remove: shpc_remove_one, */
  383. };
  384. static int __init shpcd_init(void)
  385. {
  386. int retval = 0;
  387. #ifdef CONFIG_HOTPLUG_PCI_SHPC_POLL_EVENT_MODE
  388. shpchp_poll_mode = 1;
  389. #endif
  390. shpchp_wq = create_singlethread_workqueue("shpchpd");
  391. if (!shpchp_wq)
  392. return -ENOMEM;
  393. retval = pci_register_driver(&shpc_driver);
  394. dbg("%s: pci_register_driver = %d\n", __FUNCTION__, retval);
  395. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  396. if (retval) {
  397. destroy_workqueue(shpchp_wq);
  398. }
  399. return retval;
  400. }
  401. static void __exit shpcd_cleanup(void)
  402. {
  403. dbg("unload_shpchpd()\n");
  404. unload_shpchpd();
  405. pci_unregister_driver(&shpc_driver);
  406. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  407. }
  408. module_init(shpcd_init);
  409. module_exit(shpcd_cleanup);