shpchp_core.c 11 KB

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