shpchp_core.c 9.9 KB

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