shpchp_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. int shpchp_slot_with_bus;
  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_param(shpchp_slot_with_bus, bool, 0644);
  52. MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
  53. MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
  54. MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
  55. MODULE_PARM_DESC(shpchp_slot_with_bus, "Use bus number in the slot name");
  56. #define SHPC_MODULE_NAME "shpchp"
  57. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  58. static int enable_slot (struct hotplug_slot *slot);
  59. static int disable_slot (struct hotplug_slot *slot);
  60. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  61. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  62. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  63. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_address (struct hotplug_slot *slot, u32 *value);
  65. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  66. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  67. static struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
  68. .owner = THIS_MODULE,
  69. .set_attention_status = set_attention_status,
  70. .enable_slot = enable_slot,
  71. .disable_slot = disable_slot,
  72. .get_power_status = get_power_status,
  73. .get_attention_status = get_attention_status,
  74. .get_latch_status = get_latch_status,
  75. .get_adapter_status = get_adapter_status,
  76. .get_address = get_address,
  77. .get_max_bus_speed = get_max_bus_speed,
  78. .get_cur_bus_speed = get_cur_bus_speed,
  79. };
  80. /**
  81. * release_slot - free up the memory used by a slot
  82. * @hotplug_slot: slot to free
  83. */
  84. static void release_slot(struct hotplug_slot *hotplug_slot)
  85. {
  86. struct slot *slot = hotplug_slot->private;
  87. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  88. kfree(slot->hotplug_slot->info);
  89. kfree(slot->hotplug_slot);
  90. kfree(slot);
  91. }
  92. static void make_slot_name(struct slot *slot)
  93. {
  94. if (shpchp_slot_with_bus)
  95. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
  96. slot->bus, slot->number);
  97. else
  98. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%d",
  99. slot->number);
  100. }
  101. static int init_slots(struct controller *ctrl)
  102. {
  103. struct slot *slot;
  104. struct hotplug_slot *hotplug_slot;
  105. struct hotplug_slot_info *info;
  106. int retval = -ENOMEM;
  107. int i;
  108. for (i = 0; i < ctrl->num_slots; i++) {
  109. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  110. if (!slot)
  111. goto error;
  112. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  113. if (!hotplug_slot)
  114. goto error_slot;
  115. slot->hotplug_slot = hotplug_slot;
  116. info = kzalloc(sizeof(*info), GFP_KERNEL);
  117. if (!info)
  118. goto error_hpslot;
  119. hotplug_slot->info = info;
  120. hotplug_slot->name = slot->name;
  121. slot->hp_slot = i;
  122. slot->ctrl = ctrl;
  123. slot->bus = ctrl->pci_dev->subordinate->number;
  124. slot->device = ctrl->slot_device_offset + i;
  125. slot->hpc_ops = ctrl->hpc_ops;
  126. slot->number = ctrl->first_slot + (ctrl->slot_num_inc * i);
  127. mutex_init(&slot->lock);
  128. INIT_DELAYED_WORK(&slot->work, shpchp_queue_pushbutton_work);
  129. /* register this slot with the hotplug pci core */
  130. hotplug_slot->private = slot;
  131. hotplug_slot->release = &release_slot;
  132. make_slot_name(slot);
  133. hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  134. get_power_status(hotplug_slot, &info->power_status);
  135. get_attention_status(hotplug_slot, &info->attention_status);
  136. get_latch_status(hotplug_slot, &info->latch_status);
  137. get_adapter_status(hotplug_slot, &info->adapter_status);
  138. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
  139. "slot_device_offset=%x\n", slot->bus, slot->device,
  140. slot->hp_slot, slot->number, ctrl->slot_device_offset);
  141. retval = pci_hp_register(slot->hotplug_slot);
  142. if (retval) {
  143. err("pci_hp_register failed with error %d\n", retval);
  144. goto error_info;
  145. }
  146. list_add(&slot->slot_list, &ctrl->slot_list);
  147. }
  148. return 0;
  149. error_info:
  150. kfree(info);
  151. error_hpslot:
  152. kfree(hotplug_slot);
  153. error_slot:
  154. kfree(slot);
  155. error:
  156. return retval;
  157. }
  158. void cleanup_slots(struct controller *ctrl)
  159. {
  160. struct list_head *tmp;
  161. struct list_head *next;
  162. struct slot *slot;
  163. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  164. slot = list_entry(tmp, struct slot, slot_list);
  165. list_del(&slot->slot_list);
  166. cancel_delayed_work(&slot->work);
  167. flush_scheduled_work();
  168. flush_workqueue(shpchp_wq);
  169. pci_hp_deregister(slot->hotplug_slot);
  170. }
  171. }
  172. /*
  173. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  174. */
  175. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  176. {
  177. struct slot *slot = get_slot(hotplug_slot);
  178. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  179. hotplug_slot->info->attention_status = status;
  180. slot->hpc_ops->set_attention_status(slot, status);
  181. return 0;
  182. }
  183. static int enable_slot (struct hotplug_slot *hotplug_slot)
  184. {
  185. struct slot *slot = get_slot(hotplug_slot);
  186. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  187. return shpchp_sysfs_enable_slot(slot);
  188. }
  189. static int disable_slot (struct hotplug_slot *hotplug_slot)
  190. {
  191. struct slot *slot = get_slot(hotplug_slot);
  192. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  193. return shpchp_sysfs_disable_slot(slot);
  194. }
  195. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  196. {
  197. struct slot *slot = get_slot(hotplug_slot);
  198. int retval;
  199. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  200. retval = slot->hpc_ops->get_power_status(slot, value);
  201. if (retval < 0)
  202. *value = hotplug_slot->info->power_status;
  203. return 0;
  204. }
  205. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  206. {
  207. struct slot *slot = get_slot(hotplug_slot);
  208. int retval;
  209. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  210. retval = slot->hpc_ops->get_attention_status(slot, value);
  211. if (retval < 0)
  212. *value = hotplug_slot->info->attention_status;
  213. return 0;
  214. }
  215. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  216. {
  217. struct slot *slot = get_slot(hotplug_slot);
  218. int retval;
  219. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  220. retval = slot->hpc_ops->get_latch_status(slot, value);
  221. if (retval < 0)
  222. *value = hotplug_slot->info->latch_status;
  223. return 0;
  224. }
  225. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  226. {
  227. struct slot *slot = get_slot(hotplug_slot);
  228. int retval;
  229. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  230. retval = slot->hpc_ops->get_adapter_status(slot, value);
  231. if (retval < 0)
  232. *value = hotplug_slot->info->adapter_status;
  233. return 0;
  234. }
  235. static int get_address (struct hotplug_slot *hotplug_slot, u32 *value)
  236. {
  237. struct slot *slot = get_slot(hotplug_slot);
  238. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  239. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  240. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  241. return 0;
  242. }
  243. static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  244. {
  245. struct slot *slot = get_slot(hotplug_slot);
  246. int retval;
  247. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  248. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  249. if (retval < 0)
  250. *value = PCI_SPEED_UNKNOWN;
  251. return 0;
  252. }
  253. static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  254. {
  255. struct slot *slot = get_slot(hotplug_slot);
  256. int retval;
  257. dbg("%s - physical_slot = %s\n", __func__, hotplug_slot->name);
  258. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  259. if (retval < 0)
  260. *value = PCI_SPEED_UNKNOWN;
  261. return 0;
  262. }
  263. static int is_shpc_capable(struct pci_dev *dev)
  264. {
  265. if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device ==
  266. PCI_DEVICE_ID_AMD_GOLAM_7450))
  267. return 1;
  268. if (pci_find_capability(dev, PCI_CAP_ID_SHPC))
  269. return 1;
  270. return 0;
  271. }
  272. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  273. {
  274. int rc;
  275. struct controller *ctrl;
  276. if (!is_shpc_capable(pdev))
  277. return -ENODEV;
  278. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  279. if (!ctrl) {
  280. err("%s : out of memory\n", __func__);
  281. goto err_out_none;
  282. }
  283. INIT_LIST_HEAD(&ctrl->slot_list);
  284. rc = shpc_init(ctrl, pdev);
  285. if (rc) {
  286. dbg("%s: controller initialization failed\n",
  287. SHPC_MODULE_NAME);
  288. goto err_out_free_ctrl;
  289. }
  290. pci_set_drvdata(pdev, ctrl);
  291. /* Setup the slot information structures */
  292. rc = init_slots(ctrl);
  293. if (rc) {
  294. err("%s: slot initialization failed\n", SHPC_MODULE_NAME);
  295. goto err_out_release_ctlr;
  296. }
  297. rc = shpchp_create_ctrl_files(ctrl);
  298. if (rc)
  299. goto err_cleanup_slots;
  300. return 0;
  301. err_cleanup_slots:
  302. cleanup_slots(ctrl);
  303. err_out_release_ctlr:
  304. ctrl->hpc_ops->release_ctlr(ctrl);
  305. err_out_free_ctrl:
  306. kfree(ctrl);
  307. err_out_none:
  308. return -ENODEV;
  309. }
  310. static void shpc_remove(struct pci_dev *dev)
  311. {
  312. struct controller *ctrl = pci_get_drvdata(dev);
  313. shpchp_remove_ctrl_files(ctrl);
  314. ctrl->hpc_ops->release_ctlr(ctrl);
  315. kfree(ctrl);
  316. }
  317. static struct pci_device_id shpcd_pci_tbl[] = {
  318. {PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0)},
  319. { /* end: all zeroes */ }
  320. };
  321. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  322. static struct pci_driver shpc_driver = {
  323. .name = SHPC_MODULE_NAME,
  324. .id_table = shpcd_pci_tbl,
  325. .probe = shpc_probe,
  326. .remove = shpc_remove,
  327. };
  328. static int __init shpcd_init(void)
  329. {
  330. int retval = 0;
  331. retval = pci_register_driver(&shpc_driver);
  332. dbg("%s: pci_register_driver = %d\n", __func__, retval);
  333. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  334. return retval;
  335. }
  336. static void __exit shpcd_cleanup(void)
  337. {
  338. dbg("unload_shpchpd()\n");
  339. pci_unregister_driver(&shpc_driver);
  340. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  341. }
  342. module_init(shpcd_init);
  343. module_exit(shpcd_cleanup);