shpchp_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 "shpchp.h"
  35. /* Global variables */
  36. int shpchp_debug;
  37. int shpchp_poll_mode;
  38. int shpchp_poll_time;
  39. struct controller *shpchp_ctrl_list; /* = NULL */
  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 shpc_start_thread (void);
  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_address (struct hotplug_slot *slot, u32 *value);
  62. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  63. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  64. static struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
  65. .owner = THIS_MODULE,
  66. .set_attention_status = set_attention_status,
  67. .enable_slot = enable_slot,
  68. .disable_slot = disable_slot,
  69. .get_power_status = get_power_status,
  70. .get_attention_status = get_attention_status,
  71. .get_latch_status = get_latch_status,
  72. .get_adapter_status = get_adapter_status,
  73. .get_address = get_address,
  74. .get_max_bus_speed = get_max_bus_speed,
  75. .get_cur_bus_speed = get_cur_bus_speed,
  76. };
  77. /**
  78. * release_slot - free up the memory used by a slot
  79. * @hotplug_slot: slot to free
  80. */
  81. static void release_slot(struct hotplug_slot *hotplug_slot)
  82. {
  83. struct slot *slot = hotplug_slot->private;
  84. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  85. kfree(slot->hotplug_slot->info);
  86. kfree(slot->hotplug_slot->name);
  87. kfree(slot->hotplug_slot);
  88. kfree(slot);
  89. }
  90. static int init_slots(struct controller *ctrl)
  91. {
  92. struct slot *new_slot;
  93. u8 number_of_slots;
  94. u8 slot_device;
  95. u32 slot_number, sun;
  96. int result = -ENOMEM;
  97. number_of_slots = ctrl->num_slots;
  98. slot_device = ctrl->slot_device_offset;
  99. slot_number = ctrl->first_slot;
  100. while (number_of_slots) {
  101. new_slot = (struct slot *) kmalloc(sizeof(struct slot), GFP_KERNEL);
  102. if (!new_slot)
  103. goto error;
  104. memset(new_slot, 0, sizeof(struct slot));
  105. new_slot->hotplug_slot = kmalloc (sizeof (struct hotplug_slot), GFP_KERNEL);
  106. if (!new_slot->hotplug_slot)
  107. goto error_slot;
  108. memset(new_slot->hotplug_slot, 0, sizeof (struct hotplug_slot));
  109. new_slot->hotplug_slot->info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
  110. if (!new_slot->hotplug_slot->info)
  111. goto error_hpslot;
  112. memset(new_slot->hotplug_slot->info, 0, sizeof (struct hotplug_slot_info));
  113. new_slot->hotplug_slot->name = kmalloc (SLOT_NAME_SIZE, GFP_KERNEL);
  114. if (!new_slot->hotplug_slot->name)
  115. goto error_info;
  116. new_slot->magic = SLOT_MAGIC;
  117. new_slot->ctrl = ctrl;
  118. new_slot->bus = ctrl->slot_bus;
  119. new_slot->device = slot_device;
  120. new_slot->hpc_ops = ctrl->hpc_ops;
  121. if (shpchprm_get_physical_slot_number(ctrl, &sun,
  122. new_slot->bus, new_slot->device))
  123. goto error_name;
  124. new_slot->number = sun;
  125. new_slot->hp_slot = slot_device - ctrl->slot_device_offset;
  126. /* register this slot with the hotplug pci core */
  127. new_slot->hotplug_slot->private = new_slot;
  128. new_slot->hotplug_slot->release = &release_slot;
  129. make_slot_name(new_slot->hotplug_slot->name, SLOT_NAME_SIZE, new_slot);
  130. new_slot->hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  131. new_slot->hpc_ops->get_power_status(new_slot, &(new_slot->hotplug_slot->info->power_status));
  132. new_slot->hpc_ops->get_attention_status(new_slot, &(new_slot->hotplug_slot->info->attention_status));
  133. new_slot->hpc_ops->get_latch_status(new_slot, &(new_slot->hotplug_slot->info->latch_status));
  134. new_slot->hpc_ops->get_adapter_status(new_slot, &(new_slot->hotplug_slot->info->adapter_status));
  135. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x slot_device_offset=%x\n", new_slot->bus,
  136. new_slot->device, new_slot->hp_slot, new_slot->number, ctrl->slot_device_offset);
  137. result = pci_hp_register (new_slot->hotplug_slot);
  138. if (result) {
  139. err ("pci_hp_register failed with error %d\n", result);
  140. goto error_name;
  141. }
  142. new_slot->next = ctrl->slot;
  143. ctrl->slot = new_slot;
  144. number_of_slots--;
  145. slot_device++;
  146. slot_number += ctrl->slot_num_inc;
  147. }
  148. return 0;
  149. error_name:
  150. kfree(new_slot->hotplug_slot->name);
  151. error_info:
  152. kfree(new_slot->hotplug_slot->info);
  153. error_hpslot:
  154. kfree(new_slot->hotplug_slot);
  155. error_slot:
  156. kfree(new_slot);
  157. error:
  158. return result;
  159. }
  160. static void cleanup_slots(struct controller *ctrl)
  161. {
  162. struct slot *old_slot, *next_slot;
  163. old_slot = ctrl->slot;
  164. ctrl->slot = NULL;
  165. while (old_slot) {
  166. next_slot = old_slot->next;
  167. pci_hp_deregister(old_slot->hotplug_slot);
  168. old_slot = next_slot;
  169. }
  170. }
  171. static int get_ctlr_slot_config(struct controller *ctrl)
  172. {
  173. int num_ctlr_slots;
  174. int first_device_num;
  175. int physical_slot_num;
  176. int updown;
  177. int rc;
  178. int flags;
  179. rc = shpc_get_ctlr_slot_config(ctrl, &num_ctlr_slots, &first_device_num, &physical_slot_num, &updown, &flags);
  180. if (rc) {
  181. err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n", __FUNCTION__, ctrl->bus, ctrl->device);
  182. return -1;
  183. }
  184. ctrl->num_slots = num_ctlr_slots;
  185. ctrl->slot_device_offset = first_device_num;
  186. ctrl->first_slot = physical_slot_num;
  187. ctrl->slot_num_inc = updown; /* either -1 or 1 */
  188. dbg("%s: num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) updown(%d) for b:d (%x:%x)\n",
  189. __FUNCTION__, num_ctlr_slots, first_device_num, physical_slot_num, updown, ctrl->bus, ctrl->device);
  190. return 0;
  191. }
  192. /*
  193. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  194. */
  195. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  196. {
  197. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  198. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  199. hotplug_slot->info->attention_status = status;
  200. slot->hpc_ops->set_attention_status(slot, status);
  201. return 0;
  202. }
  203. static int enable_slot (struct hotplug_slot *hotplug_slot)
  204. {
  205. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  206. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  207. return shpchp_enable_slot(slot);
  208. }
  209. static int disable_slot (struct hotplug_slot *hotplug_slot)
  210. {
  211. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  212. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  213. return shpchp_disable_slot(slot);
  214. }
  215. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  216. {
  217. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  218. int retval;
  219. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  220. retval = slot->hpc_ops->get_power_status(slot, value);
  221. if (retval < 0)
  222. *value = hotplug_slot->info->power_status;
  223. return 0;
  224. }
  225. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  226. {
  227. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  228. int retval;
  229. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  230. retval = slot->hpc_ops->get_attention_status(slot, value);
  231. if (retval < 0)
  232. *value = hotplug_slot->info->attention_status;
  233. return 0;
  234. }
  235. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  236. {
  237. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  238. int retval;
  239. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  240. retval = slot->hpc_ops->get_latch_status(slot, value);
  241. if (retval < 0)
  242. *value = hotplug_slot->info->latch_status;
  243. return 0;
  244. }
  245. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  246. {
  247. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  248. int retval;
  249. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  250. retval = slot->hpc_ops->get_adapter_status(slot, value);
  251. if (retval < 0)
  252. *value = hotplug_slot->info->adapter_status;
  253. return 0;
  254. }
  255. static int get_address (struct hotplug_slot *hotplug_slot, u32 *value)
  256. {
  257. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  258. struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
  259. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  260. *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
  261. return 0;
  262. }
  263. static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  264. {
  265. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  266. int retval;
  267. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  268. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  269. if (retval < 0)
  270. *value = PCI_SPEED_UNKNOWN;
  271. return 0;
  272. }
  273. static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  274. {
  275. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  276. int retval;
  277. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  278. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  279. if (retval < 0)
  280. *value = PCI_SPEED_UNKNOWN;
  281. return 0;
  282. }
  283. static int is_shpc_capable(struct pci_dev *dev)
  284. {
  285. if ((dev->vendor == PCI_VENDOR_ID_AMD) || (dev->device ==
  286. PCI_DEVICE_ID_AMD_GOLAM_7450))
  287. return 1;
  288. if (pci_find_capability(dev, PCI_CAP_ID_SHPC))
  289. return 1;
  290. return 0;
  291. }
  292. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  293. {
  294. int rc;
  295. struct controller *ctrl;
  296. struct slot *t_slot;
  297. int first_device_num; /* first PCI device number supported by this SHPC */
  298. int num_ctlr_slots; /* number of slots supported by this SHPC */
  299. if (!is_shpc_capable(pdev))
  300. return -ENODEV;
  301. ctrl = (struct controller *) kmalloc(sizeof(struct controller), GFP_KERNEL);
  302. if (!ctrl) {
  303. err("%s : out of memory\n", __FUNCTION__);
  304. goto err_out_none;
  305. }
  306. memset(ctrl, 0, sizeof(struct controller));
  307. rc = shpc_init(ctrl, pdev);
  308. if (rc) {
  309. dbg("%s: controller initialization failed\n", SHPC_MODULE_NAME);
  310. goto err_out_free_ctrl;
  311. }
  312. pci_set_drvdata(pdev, ctrl);
  313. ctrl->pci_bus = kmalloc (sizeof (*ctrl->pci_bus), GFP_KERNEL);
  314. if (!ctrl->pci_bus) {
  315. err("out of memory\n");
  316. rc = -ENOMEM;
  317. goto err_out_unmap_mmio_region;
  318. }
  319. memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
  320. ctrl->bus = pdev->bus->number;
  321. ctrl->slot_bus = pdev->subordinate->number;
  322. ctrl->device = PCI_SLOT(pdev->devfn);
  323. ctrl->function = PCI_FUNC(pdev->devfn);
  324. dbg("ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  325. /*
  326. * Save configuration headers for this and subordinate PCI buses
  327. */
  328. rc = get_ctlr_slot_config(ctrl);
  329. if (rc) {
  330. err(msg_initialization_err, rc);
  331. goto err_out_free_ctrl_bus;
  332. }
  333. first_device_num = ctrl->slot_device_offset;
  334. num_ctlr_slots = ctrl->num_slots;
  335. ctrl->add_support = 1;
  336. /* Setup the slot information structures */
  337. rc = init_slots(ctrl);
  338. if (rc) {
  339. err(msg_initialization_err, 6);
  340. goto err_out_free_ctrl_slot;
  341. }
  342. /* Now hpc_functions (slot->hpc_ops->functions) are ready */
  343. t_slot = shpchp_find_slot(ctrl, first_device_num);
  344. /* Check for operation bus speed */
  345. rc = t_slot->hpc_ops->get_cur_bus_speed(t_slot, &ctrl->speed);
  346. dbg("%s: t_slot->hp_slot %x\n", __FUNCTION__,t_slot->hp_slot);
  347. if (rc || ctrl->speed == PCI_SPEED_UNKNOWN) {
  348. err(SHPC_MODULE_NAME ": Can't get current bus speed. Set to 33MHz PCI.\n");
  349. ctrl->speed = PCI_SPEED_33MHz;
  350. }
  351. /* Finish setting up the hot plug ctrl device */
  352. ctrl->next_event = 0;
  353. if (!shpchp_ctrl_list) {
  354. shpchp_ctrl_list = ctrl;
  355. ctrl->next = NULL;
  356. } else {
  357. ctrl->next = shpchp_ctrl_list;
  358. shpchp_ctrl_list = ctrl;
  359. }
  360. shpchp_create_ctrl_files(ctrl);
  361. return 0;
  362. err_out_free_ctrl_slot:
  363. cleanup_slots(ctrl);
  364. err_out_free_ctrl_bus:
  365. kfree(ctrl->pci_bus);
  366. err_out_unmap_mmio_region:
  367. ctrl->hpc_ops->release_ctlr(ctrl);
  368. err_out_free_ctrl:
  369. kfree(ctrl);
  370. err_out_none:
  371. return -ENODEV;
  372. }
  373. static int shpc_start_thread(void)
  374. {
  375. int retval = 0;
  376. dbg("Initialize + Start the notification/polling mechanism \n");
  377. retval = shpchp_event_start_thread();
  378. if (retval) {
  379. dbg("shpchp_event_start_thread() failed\n");
  380. return retval;
  381. }
  382. return retval;
  383. }
  384. static void __exit unload_shpchpd(void)
  385. {
  386. struct controller *ctrl;
  387. struct controller *tctrl;
  388. ctrl = shpchp_ctrl_list;
  389. while (ctrl) {
  390. shpchp_remove_ctrl_files(ctrl);
  391. cleanup_slots(ctrl);
  392. kfree (ctrl->pci_bus);
  393. ctrl->hpc_ops->release_ctlr(ctrl);
  394. tctrl = ctrl;
  395. ctrl = ctrl->next;
  396. kfree(tctrl);
  397. }
  398. /* Stop the notification mechanism */
  399. shpchp_event_stop_thread();
  400. }
  401. static struct pci_device_id shpcd_pci_tbl[] = {
  402. {
  403. .class = ((PCI_CLASS_BRIDGE_PCI << 8) | 0x00),
  404. .class_mask = ~0,
  405. .vendor = PCI_ANY_ID,
  406. .device = PCI_ANY_ID,
  407. .subvendor = PCI_ANY_ID,
  408. .subdevice = PCI_ANY_ID,
  409. },
  410. { /* end: all zeroes */ }
  411. };
  412. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  413. static struct pci_driver shpc_driver = {
  414. .name = SHPC_MODULE_NAME,
  415. .id_table = shpcd_pci_tbl,
  416. .probe = shpc_probe,
  417. /* remove: shpc_remove_one, */
  418. };
  419. static int __init shpcd_init(void)
  420. {
  421. int retval = 0;
  422. #ifdef CONFIG_HOTPLUG_PCI_SHPC_POLL_EVENT_MODE
  423. shpchp_poll_mode = 1;
  424. #endif
  425. retval = shpc_start_thread();
  426. if (retval)
  427. goto error_hpc_init;
  428. retval = pci_register_driver(&shpc_driver);
  429. dbg("%s: pci_register_driver = %d\n", __FUNCTION__, retval);
  430. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  431. error_hpc_init:
  432. if (retval) {
  433. shpchp_event_stop_thread();
  434. }
  435. return retval;
  436. }
  437. static void __exit shpcd_cleanup(void)
  438. {
  439. dbg("unload_shpchpd()\n");
  440. unload_shpchpd();
  441. pci_unregister_driver(&shpc_driver);
  442. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  443. }
  444. module_init(shpcd_init);
  445. module_exit(shpcd_cleanup);