shpchp_core.c 10.0 KB

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