shpchp_core.c 10 KB

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