shpchp_core.c 11 KB

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