shpchp_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. struct workqueue_struct *shpchp_wq;
  41. #define DRIVER_VERSION "0.4"
  42. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  43. #define DRIVER_DESC "Standard Hot Plug PCI Controller Driver"
  44. MODULE_AUTHOR(DRIVER_AUTHOR);
  45. MODULE_DESCRIPTION(DRIVER_DESC);
  46. MODULE_LICENSE("GPL");
  47. module_param(shpchp_debug, bool, 0644);
  48. module_param(shpchp_poll_mode, bool, 0644);
  49. module_param(shpchp_poll_time, int, 0644);
  50. MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
  51. MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
  52. MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
  53. #define SHPC_MODULE_NAME "shpchp"
  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. for (i = 0; i < ctrl->num_slots; i++) {
  102. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  103. if (!slot)
  104. goto error;
  105. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  106. if (!hotplug_slot)
  107. goto error_slot;
  108. slot->hotplug_slot = hotplug_slot;
  109. info = kzalloc(sizeof(*info), GFP_KERNEL);
  110. if (!info)
  111. goto error_hpslot;
  112. hotplug_slot->info = info;
  113. hotplug_slot->name = slot->name;
  114. slot->hp_slot = i;
  115. slot->ctrl = ctrl;
  116. slot->bus = ctrl->pci_dev->subordinate->number;
  117. slot->device = ctrl->slot_device_offset + i;
  118. slot->hpc_ops = ctrl->hpc_ops;
  119. slot->number = ctrl->first_slot + (ctrl->slot_num_inc * i);
  120. mutex_init(&slot->lock);
  121. INIT_DELAYED_WORK(&slot->work, shpchp_queue_pushbutton_work);
  122. /* register this slot with the hotplug pci core */
  123. hotplug_slot->private = slot;
  124. hotplug_slot->release = &release_slot;
  125. make_slot_name(slot);
  126. hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  127. get_power_status(hotplug_slot, &info->power_status);
  128. get_attention_status(hotplug_slot, &info->attention_status);
  129. get_latch_status(hotplug_slot, &info->latch_status);
  130. get_adapter_status(hotplug_slot, &info->adapter_status);
  131. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  132. "slot_device_offset=%x\n", slot->bus, slot->device,
  133. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  134. retval = pci_hp_register(slot->hotplug_slot);
  135. if (retval) {
  136. err("pci_hp_register failed with error %d\n", retval);
  137. goto error_info;
  138. }
  139. list_add(&slot->slot_list, &ctrl->slot_list);
  140. }
  141. return 0;
  142. error_info:
  143. kfree(info);
  144. error_hpslot:
  145. kfree(hotplug_slot);
  146. error_slot:
  147. kfree(slot);
  148. error:
  149. return retval;
  150. }
  151. void cleanup_slots(struct controller *ctrl)
  152. {
  153. struct list_head *tmp;
  154. struct list_head *next;
  155. struct slot *slot;
  156. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  157. slot = list_entry(tmp, struct slot, slot_list);
  158. list_del(&slot->slot_list);
  159. cancel_delayed_work(&slot->work);
  160. flush_scheduled_work();
  161. flush_workqueue(shpchp_wq);
  162. pci_hp_deregister(slot->hotplug_slot);
  163. }
  164. }
  165. /*
  166. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  167. */
  168. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  169. {
  170. struct slot *slot = get_slot(hotplug_slot);
  171. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  172. hotplug_slot->info->attention_status = status;
  173. slot->hpc_ops->set_attention_status(slot, status);
  174. return 0;
  175. }
  176. static int enable_slot (struct hotplug_slot *hotplug_slot)
  177. {
  178. struct slot *slot = get_slot(hotplug_slot);
  179. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  180. return shpchp_sysfs_enable_slot(slot);
  181. }
  182. static int disable_slot (struct hotplug_slot *hotplug_slot)
  183. {
  184. struct slot *slot = get_slot(hotplug_slot);
  185. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  186. return shpchp_sysfs_disable_slot(slot);
  187. }
  188. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  189. {
  190. struct slot *slot = get_slot(hotplug_slot);
  191. int retval;
  192. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  193. retval = slot->hpc_ops->get_power_status(slot, value);
  194. if (retval < 0)
  195. *value = hotplug_slot->info->power_status;
  196. return 0;
  197. }
  198. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  199. {
  200. struct slot *slot = get_slot(hotplug_slot);
  201. int retval;
  202. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  203. retval = slot->hpc_ops->get_attention_status(slot, value);
  204. if (retval < 0)
  205. *value = hotplug_slot->info->attention_status;
  206. return 0;
  207. }
  208. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  209. {
  210. struct slot *slot = get_slot(hotplug_slot);
  211. int retval;
  212. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  213. retval = slot->hpc_ops->get_latch_status(slot, value);
  214. if (retval < 0)
  215. *value = hotplug_slot->info->latch_status;
  216. return 0;
  217. }
  218. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  219. {
  220. struct slot *slot = get_slot(hotplug_slot);
  221. int retval;
  222. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  223. retval = slot->hpc_ops->get_adapter_status(slot, value);
  224. if (retval < 0)
  225. *value = hotplug_slot->info->adapter_status;
  226. return 0;
  227. }
  228. static int get_address (struct hotplug_slot *hotplug_slot, u32 *value)
  229. {
  230. struct slot *slot = get_slot(hotplug_slot);
  231. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  232. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  233. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  234. return 0;
  235. }
  236. static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  237. {
  238. struct slot *slot = get_slot(hotplug_slot);
  239. int retval;
  240. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  241. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  242. if (retval < 0)
  243. *value = PCI_SPEED_UNKNOWN;
  244. return 0;
  245. }
  246. static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  247. {
  248. struct slot *slot = get_slot(hotplug_slot);
  249. int retval;
  250. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  251. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  252. if (retval < 0)
  253. *value = PCI_SPEED_UNKNOWN;
  254. return 0;
  255. }
  256. static int is_shpc_capable(struct pci_dev *dev)
  257. {
  258. if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device ==
  259. PCI_DEVICE_ID_AMD_GOLAM_7450))
  260. return 1;
  261. if (pci_find_capability(dev, PCI_CAP_ID_SHPC))
  262. return 1;
  263. return 0;
  264. }
  265. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  266. {
  267. int rc;
  268. struct controller *ctrl;
  269. if (!is_shpc_capable(pdev))
  270. return -ENODEV;
  271. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  272. if (!ctrl) {
  273. err("%s : out of memory\n", __FUNCTION__);
  274. goto err_out_none;
  275. }
  276. INIT_LIST_HEAD(&ctrl->slot_list);
  277. rc = shpc_init(ctrl, pdev);
  278. if (rc) {
  279. dbg("%s: controller initialization failed\n",
  280. SHPC_MODULE_NAME);
  281. goto err_out_free_ctrl;
  282. }
  283. pci_set_drvdata(pdev, ctrl);
  284. /* Setup the slot information structures */
  285. rc = init_slots(ctrl);
  286. if (rc) {
  287. err("%s: slot initialization failed\n", SHPC_MODULE_NAME);
  288. goto err_out_release_ctlr;
  289. }
  290. rc = shpchp_create_ctrl_files(ctrl);
  291. if (rc)
  292. goto err_cleanup_slots;
  293. return 0;
  294. err_cleanup_slots:
  295. cleanup_slots(ctrl);
  296. err_out_release_ctlr:
  297. ctrl->hpc_ops->release_ctlr(ctrl);
  298. err_out_free_ctrl:
  299. kfree(ctrl);
  300. err_out_none:
  301. return -ENODEV;
  302. }
  303. static void shpc_remove(struct pci_dev *dev)
  304. {
  305. struct controller *ctrl = pci_get_drvdata(dev);
  306. shpchp_remove_ctrl_files(ctrl);
  307. ctrl->hpc_ops->release_ctlr(ctrl);
  308. kfree(ctrl);
  309. }
  310. static struct pci_device_id shpcd_pci_tbl[] = {
  311. {PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0)},
  312. { /* end: all zeroes */ }
  313. };
  314. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  315. static struct pci_driver shpc_driver = {
  316. .name = SHPC_MODULE_NAME,
  317. .id_table = shpcd_pci_tbl,
  318. .probe = shpc_probe,
  319. .remove = shpc_remove,
  320. };
  321. static int __init shpcd_init(void)
  322. {
  323. int retval = 0;
  324. retval = pci_register_driver(&shpc_driver);
  325. dbg("%s: pci_register_driver = %d\n", __FUNCTION__, retval);
  326. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  327. return retval;
  328. }
  329. static void __exit shpcd_cleanup(void)
  330. {
  331. dbg("unload_shpchpd()\n");
  332. pci_unregister_driver(&shpc_driver);
  333. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  334. }
  335. module_init(shpcd_init);
  336. module_exit(shpcd_cleanup);