shpchp_core.c 9.8 KB

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