shpchp_core.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 = -ENOMEM;
  89. int i;
  90. for (i = 0; i < ctrl->num_slots; i++) {
  91. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  92. if (!slot)
  93. goto error;
  94. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  95. if (!hotplug_slot)
  96. goto error_slot;
  97. slot->hotplug_slot = hotplug_slot;
  98. info = kzalloc(sizeof(*info), GFP_KERNEL);
  99. if (!info)
  100. goto error_hpslot;
  101. hotplug_slot->info = info;
  102. slot->hp_slot = i;
  103. slot->ctrl = ctrl;
  104. slot->bus = ctrl->pci_dev->subordinate->number;
  105. slot->device = ctrl->slot_device_offset + i;
  106. slot->hpc_ops = ctrl->hpc_ops;
  107. slot->number = ctrl->first_slot + (ctrl->slot_num_inc * i);
  108. snprintf(name, sizeof(name), "shpchp-%d", slot->number);
  109. slot->wq = alloc_workqueue(name, 0, 0);
  110. if (!slot->wq) {
  111. retval = -ENOMEM;
  112. goto error_info;
  113. }
  114. mutex_init(&slot->lock);
  115. INIT_DELAYED_WORK(&slot->work, shpchp_queue_pushbutton_work);
  116. /* register this slot with the hotplug pci core */
  117. hotplug_slot->private = slot;
  118. hotplug_slot->release = &release_slot;
  119. snprintf(name, SLOT_NAME_SIZE, "%d", slot->number);
  120. hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  121. ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x "
  122. "hp_slot=%x sun=%x slot_device_offset=%x\n",
  123. pci_domain_nr(ctrl->pci_dev->subordinate),
  124. slot->bus, slot->device, slot->hp_slot, slot->number,
  125. ctrl->slot_device_offset);
  126. retval = pci_hp_register(slot->hotplug_slot,
  127. ctrl->pci_dev->subordinate, slot->device, name);
  128. if (retval) {
  129. ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
  130. retval);
  131. goto error_slotwq;
  132. }
  133. get_power_status(hotplug_slot, &info->power_status);
  134. get_attention_status(hotplug_slot, &info->attention_status);
  135. get_latch_status(hotplug_slot, &info->latch_status);
  136. get_adapter_status(hotplug_slot, &info->adapter_status);
  137. list_add(&slot->slot_list, &ctrl->slot_list);
  138. }
  139. return 0;
  140. error_slotwq:
  141. destroy_workqueue(slot->wq);
  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. destroy_workqueue(slot->wq);
  161. pci_hp_deregister(slot->hotplug_slot);
  162. }
  163. }
  164. /*
  165. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  166. */
  167. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  168. {
  169. struct slot *slot = get_slot(hotplug_slot);
  170. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  171. __func__, slot_name(slot));
  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. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  180. __func__, slot_name(slot));
  181. return shpchp_sysfs_enable_slot(slot);
  182. }
  183. static int disable_slot (struct hotplug_slot *hotplug_slot)
  184. {
  185. struct slot *slot = get_slot(hotplug_slot);
  186. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  187. __func__, slot_name(slot));
  188. return shpchp_sysfs_disable_slot(slot);
  189. }
  190. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  191. {
  192. struct slot *slot = get_slot(hotplug_slot);
  193. int retval;
  194. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  195. __func__, slot_name(slot));
  196. retval = slot->hpc_ops->get_power_status(slot, value);
  197. if (retval < 0)
  198. *value = hotplug_slot->info->power_status;
  199. return 0;
  200. }
  201. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  202. {
  203. struct slot *slot = get_slot(hotplug_slot);
  204. int retval;
  205. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  206. __func__, slot_name(slot));
  207. retval = slot->hpc_ops->get_attention_status(slot, value);
  208. if (retval < 0)
  209. *value = hotplug_slot->info->attention_status;
  210. return 0;
  211. }
  212. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  213. {
  214. struct slot *slot = get_slot(hotplug_slot);
  215. int retval;
  216. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  217. __func__, slot_name(slot));
  218. retval = slot->hpc_ops->get_latch_status(slot, value);
  219. if (retval < 0)
  220. *value = hotplug_slot->info->latch_status;
  221. return 0;
  222. }
  223. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  224. {
  225. struct slot *slot = get_slot(hotplug_slot);
  226. int retval;
  227. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  228. __func__, slot_name(slot));
  229. retval = slot->hpc_ops->get_adapter_status(slot, value);
  230. if (retval < 0)
  231. *value = hotplug_slot->info->adapter_status;
  232. return 0;
  233. }
  234. static int is_shpc_capable(struct pci_dev *dev)
  235. {
  236. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  237. dev->device == PCI_DEVICE_ID_AMD_GOLAM_7450)
  238. return 1;
  239. if (!pci_find_capability(dev, PCI_CAP_ID_SHPC))
  240. return 0;
  241. if (get_hp_hw_control_from_firmware(dev))
  242. return 0;
  243. return 1;
  244. }
  245. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  246. {
  247. int rc;
  248. struct controller *ctrl;
  249. if (!is_shpc_capable(pdev))
  250. return -ENODEV;
  251. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  252. if (!ctrl) {
  253. dev_err(&pdev->dev, "%s: Out of memory\n", __func__);
  254. goto err_out_none;
  255. }
  256. INIT_LIST_HEAD(&ctrl->slot_list);
  257. rc = shpc_init(ctrl, pdev);
  258. if (rc) {
  259. ctrl_dbg(ctrl, "Controller initialization failed\n");
  260. goto err_out_free_ctrl;
  261. }
  262. pci_set_drvdata(pdev, ctrl);
  263. /* Setup the slot information structures */
  264. rc = init_slots(ctrl);
  265. if (rc) {
  266. ctrl_err(ctrl, "Slot initialization failed\n");
  267. goto err_out_release_ctlr;
  268. }
  269. rc = shpchp_create_ctrl_files(ctrl);
  270. if (rc)
  271. goto err_cleanup_slots;
  272. return 0;
  273. err_cleanup_slots:
  274. cleanup_slots(ctrl);
  275. err_out_release_ctlr:
  276. ctrl->hpc_ops->release_ctlr(ctrl);
  277. err_out_free_ctrl:
  278. kfree(ctrl);
  279. err_out_none:
  280. return -ENODEV;
  281. }
  282. static void shpc_remove(struct pci_dev *dev)
  283. {
  284. struct controller *ctrl = pci_get_drvdata(dev);
  285. shpchp_remove_ctrl_files(ctrl);
  286. ctrl->hpc_ops->release_ctlr(ctrl);
  287. kfree(ctrl);
  288. }
  289. static struct pci_device_id shpcd_pci_tbl[] = {
  290. {PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0)},
  291. { /* end: all zeroes */ }
  292. };
  293. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  294. static struct pci_driver shpc_driver = {
  295. .name = SHPC_MODULE_NAME,
  296. .id_table = shpcd_pci_tbl,
  297. .probe = shpc_probe,
  298. .remove = shpc_remove,
  299. };
  300. static int __init shpcd_init(void)
  301. {
  302. int retval;
  303. retval = pci_register_driver(&shpc_driver);
  304. dbg("%s: pci_register_driver = %d\n", __func__, retval);
  305. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  306. return retval;
  307. }
  308. static void __exit shpcd_cleanup(void)
  309. {
  310. dbg("unload_shpchpd()\n");
  311. pci_unregister_driver(&shpc_driver);
  312. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  313. }
  314. module_init(shpcd_init);
  315. module_exit(shpcd_cleanup);