shpchp_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 "
  127. "hp_slot=%x sun=%x slot_device_offset=%x\n",
  128. pci_domain_nr(ctrl->pci_dev->subordinate),
  129. slot->bus, slot->device, slot->hp_slot, slot->number,
  130. ctrl->slot_device_offset);
  131. retval = pci_hp_register(slot->hotplug_slot,
  132. ctrl->pci_dev->subordinate, slot->device, name);
  133. if (retval) {
  134. ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
  135. retval);
  136. goto error_slotwq;
  137. }
  138. get_power_status(hotplug_slot, &info->power_status);
  139. get_attention_status(hotplug_slot, &info->attention_status);
  140. get_latch_status(hotplug_slot, &info->latch_status);
  141. get_adapter_status(hotplug_slot, &info->adapter_status);
  142. list_add(&slot->slot_list, &ctrl->slot_list);
  143. }
  144. return 0;
  145. error_slotwq:
  146. destroy_workqueue(slot->wq);
  147. error_info:
  148. kfree(info);
  149. error_hpslot:
  150. kfree(hotplug_slot);
  151. error_slot:
  152. kfree(slot);
  153. error:
  154. return retval;
  155. }
  156. void cleanup_slots(struct controller *ctrl)
  157. {
  158. struct list_head *tmp;
  159. struct list_head *next;
  160. struct slot *slot;
  161. list_for_each_safe(tmp, next, &ctrl->slot_list) {
  162. slot = list_entry(tmp, struct slot, slot_list);
  163. list_del(&slot->slot_list);
  164. cancel_delayed_work(&slot->work);
  165. destroy_workqueue(slot->wq);
  166. pci_hp_deregister(slot->hotplug_slot);
  167. }
  168. }
  169. /*
  170. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  171. */
  172. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  173. {
  174. struct slot *slot = get_slot(hotplug_slot);
  175. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  176. __func__, slot_name(slot));
  177. hotplug_slot->info->attention_status = status;
  178. slot->hpc_ops->set_attention_status(slot, status);
  179. return 0;
  180. }
  181. static int enable_slot (struct hotplug_slot *hotplug_slot)
  182. {
  183. struct slot *slot = get_slot(hotplug_slot);
  184. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  185. __func__, slot_name(slot));
  186. return shpchp_sysfs_enable_slot(slot);
  187. }
  188. static int disable_slot (struct hotplug_slot *hotplug_slot)
  189. {
  190. struct slot *slot = get_slot(hotplug_slot);
  191. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  192. __func__, slot_name(slot));
  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. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  200. __func__, slot_name(slot));
  201. retval = slot->hpc_ops->get_power_status(slot, value);
  202. if (retval < 0)
  203. *value = hotplug_slot->info->power_status;
  204. return 0;
  205. }
  206. static int get_attention_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_attention_status(slot, value);
  213. if (retval < 0)
  214. *value = hotplug_slot->info->attention_status;
  215. return 0;
  216. }
  217. static int get_latch_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_latch_status(slot, value);
  224. if (retval < 0)
  225. *value = hotplug_slot->info->latch_status;
  226. return 0;
  227. }
  228. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  229. {
  230. struct slot *slot = get_slot(hotplug_slot);
  231. int retval;
  232. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  233. __func__, slot_name(slot));
  234. retval = slot->hpc_ops->get_adapter_status(slot, value);
  235. if (retval < 0)
  236. *value = hotplug_slot->info->adapter_status;
  237. return 0;
  238. }
  239. static int is_shpc_capable(struct pci_dev *dev)
  240. {
  241. if (dev->vendor == PCI_VENDOR_ID_AMD &&
  242. dev->device == PCI_DEVICE_ID_AMD_GOLAM_7450)
  243. return 1;
  244. if (!pci_find_capability(dev, PCI_CAP_ID_SHPC))
  245. return 0;
  246. if (get_hp_hw_control_from_firmware(dev))
  247. return 0;
  248. return 1;
  249. }
  250. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  251. {
  252. int rc;
  253. struct controller *ctrl;
  254. if (!is_shpc_capable(pdev))
  255. return -ENODEV;
  256. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  257. if (!ctrl) {
  258. dev_err(&pdev->dev, "%s: Out of memory\n", __func__);
  259. goto err_out_none;
  260. }
  261. INIT_LIST_HEAD(&ctrl->slot_list);
  262. rc = shpc_init(ctrl, pdev);
  263. if (rc) {
  264. ctrl_dbg(ctrl, "Controller initialization failed\n");
  265. goto err_out_free_ctrl;
  266. }
  267. pci_set_drvdata(pdev, ctrl);
  268. /* Setup the slot information structures */
  269. rc = init_slots(ctrl);
  270. if (rc) {
  271. ctrl_err(ctrl, "Slot initialization failed\n");
  272. goto err_out_release_ctlr;
  273. }
  274. rc = shpchp_create_ctrl_files(ctrl);
  275. if (rc)
  276. goto err_cleanup_slots;
  277. return 0;
  278. err_cleanup_slots:
  279. cleanup_slots(ctrl);
  280. err_out_release_ctlr:
  281. ctrl->hpc_ops->release_ctlr(ctrl);
  282. err_out_free_ctrl:
  283. kfree(ctrl);
  284. err_out_none:
  285. return -ENODEV;
  286. }
  287. static void shpc_remove(struct pci_dev *dev)
  288. {
  289. struct controller *ctrl = pci_get_drvdata(dev);
  290. shpchp_remove_ctrl_files(ctrl);
  291. ctrl->hpc_ops->release_ctlr(ctrl);
  292. kfree(ctrl);
  293. }
  294. static struct pci_device_id shpcd_pci_tbl[] = {
  295. {PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0)},
  296. { /* end: all zeroes */ }
  297. };
  298. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  299. static struct pci_driver shpc_driver = {
  300. .name = SHPC_MODULE_NAME,
  301. .id_table = shpcd_pci_tbl,
  302. .probe = shpc_probe,
  303. .remove = shpc_remove,
  304. };
  305. static int __init shpcd_init(void)
  306. {
  307. int retval;
  308. retval = pci_register_driver(&shpc_driver);
  309. dbg("%s: pci_register_driver = %d\n", __func__, retval);
  310. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  311. return retval;
  312. }
  313. static void __exit shpcd_cleanup(void)
  314. {
  315. dbg("unload_shpchpd()\n");
  316. pci_unregister_driver(&shpc_driver);
  317. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  318. }
  319. module_init(shpcd_init);
  320. module_exit(shpcd_cleanup);