shpchp_core.c 10 KB

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