shpchp_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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/config.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/kernel.h>
  33. #include <linux/types.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/pci.h>
  38. #include <linux/init.h>
  39. #include <asm/uaccess.h>
  40. #include "shpchp.h"
  41. /* Global variables */
  42. int shpchp_debug;
  43. int shpchp_poll_mode;
  44. int shpchp_poll_time;
  45. struct controller *shpchp_ctrl_list; /* = NULL */
  46. struct pci_func *shpchp_slot_list[256];
  47. #define DRIVER_VERSION "0.4"
  48. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  49. #define DRIVER_DESC "Standard Hot Plug PCI Controller Driver"
  50. MODULE_AUTHOR(DRIVER_AUTHOR);
  51. MODULE_DESCRIPTION(DRIVER_DESC);
  52. MODULE_LICENSE("GPL");
  53. module_param(shpchp_debug, bool, 0644);
  54. module_param(shpchp_poll_mode, bool, 0644);
  55. module_param(shpchp_poll_time, int, 0644);
  56. MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
  57. MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
  58. MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
  59. #define SHPC_MODULE_NAME "shpchp"
  60. static int shpc_start_thread (void);
  61. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  62. static int enable_slot (struct hotplug_slot *slot);
  63. static int disable_slot (struct hotplug_slot *slot);
  64. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  66. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  67. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  68. static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  69. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  70. static struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
  71. .owner = THIS_MODULE,
  72. .set_attention_status = set_attention_status,
  73. .enable_slot = enable_slot,
  74. .disable_slot = disable_slot,
  75. .get_power_status = get_power_status,
  76. .get_attention_status = get_attention_status,
  77. .get_latch_status = get_latch_status,
  78. .get_adapter_status = get_adapter_status,
  79. .get_max_bus_speed = get_max_bus_speed,
  80. .get_cur_bus_speed = get_cur_bus_speed,
  81. };
  82. /**
  83. * release_slot - free up the memory used by a slot
  84. * @hotplug_slot: slot to free
  85. */
  86. static void release_slot(struct hotplug_slot *hotplug_slot)
  87. {
  88. struct slot *slot = hotplug_slot->private;
  89. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  90. kfree(slot->hotplug_slot->info);
  91. kfree(slot->hotplug_slot->name);
  92. kfree(slot->hotplug_slot);
  93. kfree(slot);
  94. }
  95. static int init_slots(struct controller *ctrl)
  96. {
  97. struct slot *new_slot;
  98. u8 number_of_slots;
  99. u8 slot_device;
  100. u32 slot_number, sun;
  101. int result = -ENOMEM;
  102. dbg("%s\n",__FUNCTION__);
  103. number_of_slots = ctrl->num_slots;
  104. slot_device = ctrl->slot_device_offset;
  105. slot_number = ctrl->first_slot;
  106. while (number_of_slots) {
  107. new_slot = (struct slot *) kmalloc(sizeof(struct slot), GFP_KERNEL);
  108. if (!new_slot)
  109. goto error;
  110. memset(new_slot, 0, sizeof(struct slot));
  111. new_slot->hotplug_slot = kmalloc (sizeof (struct hotplug_slot), GFP_KERNEL);
  112. if (!new_slot->hotplug_slot)
  113. goto error_slot;
  114. memset(new_slot->hotplug_slot, 0, sizeof (struct hotplug_slot));
  115. new_slot->hotplug_slot->info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
  116. if (!new_slot->hotplug_slot->info)
  117. goto error_hpslot;
  118. memset(new_slot->hotplug_slot->info, 0, sizeof (struct hotplug_slot_info));
  119. new_slot->hotplug_slot->name = kmalloc (SLOT_NAME_SIZE, GFP_KERNEL);
  120. if (!new_slot->hotplug_slot->name)
  121. goto error_info;
  122. new_slot->magic = SLOT_MAGIC;
  123. new_slot->ctrl = ctrl;
  124. new_slot->bus = ctrl->slot_bus;
  125. new_slot->device = slot_device;
  126. new_slot->hpc_ops = ctrl->hpc_ops;
  127. if (shpchprm_get_physical_slot_number(ctrl, &sun,
  128. new_slot->bus, new_slot->device))
  129. goto error_name;
  130. new_slot->number = sun;
  131. new_slot->hp_slot = slot_device - ctrl->slot_device_offset;
  132. /* register this slot with the hotplug pci core */
  133. new_slot->hotplug_slot->private = new_slot;
  134. new_slot->hotplug_slot->release = &release_slot;
  135. make_slot_name(new_slot->hotplug_slot->name, SLOT_NAME_SIZE, new_slot);
  136. new_slot->hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  137. new_slot->hpc_ops->get_power_status(new_slot, &(new_slot->hotplug_slot->info->power_status));
  138. new_slot->hpc_ops->get_attention_status(new_slot, &(new_slot->hotplug_slot->info->attention_status));
  139. new_slot->hpc_ops->get_latch_status(new_slot, &(new_slot->hotplug_slot->info->latch_status));
  140. new_slot->hpc_ops->get_adapter_status(new_slot, &(new_slot->hotplug_slot->info->adapter_status));
  141. dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x slot_device_offset=%x\n", new_slot->bus,
  142. new_slot->device, new_slot->hp_slot, new_slot->number, ctrl->slot_device_offset);
  143. result = pci_hp_register (new_slot->hotplug_slot);
  144. if (result) {
  145. err ("pci_hp_register failed with error %d\n", result);
  146. goto error_name;
  147. }
  148. new_slot->next = ctrl->slot;
  149. ctrl->slot = new_slot;
  150. number_of_slots--;
  151. slot_device++;
  152. slot_number += ctrl->slot_num_inc;
  153. }
  154. return 0;
  155. error_name:
  156. kfree(new_slot->hotplug_slot->name);
  157. error_info:
  158. kfree(new_slot->hotplug_slot->info);
  159. error_hpslot:
  160. kfree(new_slot->hotplug_slot);
  161. error_slot:
  162. kfree(new_slot);
  163. error:
  164. return result;
  165. }
  166. static void cleanup_slots(struct controller *ctrl)
  167. {
  168. struct slot *old_slot, *next_slot;
  169. old_slot = ctrl->slot;
  170. ctrl->slot = NULL;
  171. while (old_slot) {
  172. next_slot = old_slot->next;
  173. pci_hp_deregister(old_slot->hotplug_slot);
  174. old_slot = next_slot;
  175. }
  176. }
  177. static int get_ctlr_slot_config(struct controller *ctrl)
  178. {
  179. int num_ctlr_slots;
  180. int first_device_num;
  181. int physical_slot_num;
  182. int updown;
  183. int rc;
  184. int flags;
  185. rc = shpc_get_ctlr_slot_config(ctrl, &num_ctlr_slots, &first_device_num, &physical_slot_num, &updown, &flags);
  186. if (rc) {
  187. err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n", __FUNCTION__, ctrl->bus, ctrl->device);
  188. return -1;
  189. }
  190. ctrl->num_slots = num_ctlr_slots;
  191. ctrl->slot_device_offset = first_device_num;
  192. ctrl->first_slot = physical_slot_num;
  193. ctrl->slot_num_inc = updown; /* either -1 or 1 */
  194. dbg("%s: num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) updown(%d) for b:d (%x:%x)\n",
  195. __FUNCTION__, num_ctlr_slots, first_device_num, physical_slot_num, updown, ctrl->bus, ctrl->device);
  196. return 0;
  197. }
  198. /*
  199. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  200. */
  201. static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
  202. {
  203. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  204. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  205. hotplug_slot->info->attention_status = status;
  206. slot->hpc_ops->set_attention_status(slot, status);
  207. return 0;
  208. }
  209. static int enable_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_enable_slot(slot);
  214. }
  215. static int disable_slot (struct hotplug_slot *hotplug_slot)
  216. {
  217. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  218. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  219. return shpchp_disable_slot(slot);
  220. }
  221. static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
  222. {
  223. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  224. int retval;
  225. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  226. retval = slot->hpc_ops->get_power_status(slot, value);
  227. if (retval < 0)
  228. *value = hotplug_slot->info->power_status;
  229. return 0;
  230. }
  231. static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
  232. {
  233. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  234. int retval;
  235. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  236. retval = slot->hpc_ops->get_attention_status(slot, value);
  237. if (retval < 0)
  238. *value = hotplug_slot->info->attention_status;
  239. return 0;
  240. }
  241. static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
  242. {
  243. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  244. int retval;
  245. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  246. retval = slot->hpc_ops->get_latch_status(slot, value);
  247. if (retval < 0)
  248. *value = hotplug_slot->info->latch_status;
  249. return 0;
  250. }
  251. static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
  252. {
  253. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  254. int retval;
  255. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  256. retval = slot->hpc_ops->get_adapter_status(slot, value);
  257. if (retval < 0)
  258. *value = hotplug_slot->info->adapter_status;
  259. return 0;
  260. }
  261. static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  262. {
  263. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  264. int retval;
  265. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  266. retval = slot->hpc_ops->get_max_bus_speed(slot, value);
  267. if (retval < 0)
  268. *value = PCI_SPEED_UNKNOWN;
  269. return 0;
  270. }
  271. static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  272. {
  273. struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
  274. int retval;
  275. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  276. retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
  277. if (retval < 0)
  278. *value = PCI_SPEED_UNKNOWN;
  279. return 0;
  280. }
  281. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  282. {
  283. int rc;
  284. struct controller *ctrl;
  285. struct slot *t_slot;
  286. int first_device_num; /* first PCI device number supported by this SHPC */
  287. int num_ctlr_slots; /* number of slots supported by this SHPC */
  288. ctrl = (struct controller *) kmalloc(sizeof(struct controller), GFP_KERNEL);
  289. if (!ctrl) {
  290. err("%s : out of memory\n", __FUNCTION__);
  291. goto err_out_none;
  292. }
  293. memset(ctrl, 0, sizeof(struct controller));
  294. dbg("DRV_thread pid = %d\n", current->pid);
  295. rc = shpc_init(ctrl, pdev,
  296. (php_intr_callback_t) shpchp_handle_attention_button,
  297. (php_intr_callback_t) shpchp_handle_switch_change,
  298. (php_intr_callback_t) shpchp_handle_presence_change,
  299. (php_intr_callback_t) shpchp_handle_power_fault);
  300. if (rc) {
  301. dbg("%s: controller initialization failed\n", SHPC_MODULE_NAME);
  302. goto err_out_free_ctrl;
  303. }
  304. dbg("%s: controller initialization success\n", __FUNCTION__);
  305. ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
  306. pci_set_drvdata(pdev, ctrl);
  307. ctrl->pci_bus = kmalloc (sizeof (*ctrl->pci_bus), GFP_KERNEL);
  308. if (!ctrl->pci_bus) {
  309. err("out of memory\n");
  310. rc = -ENOMEM;
  311. goto err_out_unmap_mmio_region;
  312. }
  313. memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
  314. ctrl->bus = pdev->bus->number;
  315. ctrl->slot_bus = pdev->subordinate->number;
  316. ctrl->device = PCI_SLOT(pdev->devfn);
  317. ctrl->function = PCI_FUNC(pdev->devfn);
  318. dbg("ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
  319. /*
  320. * Save configuration headers for this and subordinate PCI buses
  321. */
  322. rc = get_ctlr_slot_config(ctrl);
  323. if (rc) {
  324. err(msg_initialization_err, rc);
  325. goto err_out_free_ctrl_bus;
  326. }
  327. first_device_num = ctrl->slot_device_offset;
  328. num_ctlr_slots = ctrl->num_slots;
  329. /* Store PCI Config Space for all devices on this bus */
  330. rc = shpchp_save_config(ctrl, ctrl->slot_bus, num_ctlr_slots, first_device_num);
  331. if (rc) {
  332. err("%s: unable to save PCI configuration data, error %d\n", __FUNCTION__, rc);
  333. goto err_out_free_ctrl_bus;
  334. }
  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 loop;
  376. int retval = 0;
  377. dbg("Initialize + Start the notification/polling mechanism \n");
  378. retval = shpchp_event_start_thread();
  379. if (retval) {
  380. dbg("shpchp_event_start_thread() failed\n");
  381. return retval;
  382. }
  383. dbg("Initialize slot lists\n");
  384. /* One slot list for each bus in the system */
  385. for (loop = 0; loop < 256; loop++) {
  386. shpchp_slot_list[loop] = NULL;
  387. }
  388. return retval;
  389. }
  390. static void __exit unload_shpchpd(void)
  391. {
  392. struct pci_func *next;
  393. struct pci_func *TempSlot;
  394. int loop;
  395. struct controller *ctrl;
  396. struct controller *tctrl;
  397. ctrl = shpchp_ctrl_list;
  398. while (ctrl) {
  399. cleanup_slots(ctrl);
  400. kfree (ctrl->pci_bus);
  401. dbg("%s: calling release_ctlr\n", __FUNCTION__);
  402. ctrl->hpc_ops->release_ctlr(ctrl);
  403. tctrl = ctrl;
  404. ctrl = ctrl->next;
  405. kfree(tctrl);
  406. }
  407. for (loop = 0; loop < 256; loop++) {
  408. next = shpchp_slot_list[loop];
  409. while (next != NULL) {
  410. TempSlot = next;
  411. next = next->next;
  412. kfree(TempSlot);
  413. }
  414. }
  415. /* Stop the notification mechanism */
  416. shpchp_event_stop_thread();
  417. }
  418. static struct pci_device_id shpcd_pci_tbl[] = {
  419. {
  420. .class = ((PCI_CLASS_BRIDGE_PCI << 8) | 0x00),
  421. .class_mask = ~0,
  422. .vendor = PCI_ANY_ID,
  423. .device = PCI_ANY_ID,
  424. .subvendor = PCI_ANY_ID,
  425. .subdevice = PCI_ANY_ID,
  426. },
  427. { /* end: all zeroes */ }
  428. };
  429. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  430. static struct pci_driver shpc_driver = {
  431. .name = SHPC_MODULE_NAME,
  432. .id_table = shpcd_pci_tbl,
  433. .probe = shpc_probe,
  434. /* remove: shpc_remove_one, */
  435. };
  436. static int __init shpcd_init(void)
  437. {
  438. int retval = 0;
  439. #ifdef CONFIG_HOTPLUG_PCI_SHPC_POLL_EVENT_MODE
  440. shpchp_poll_mode = 1;
  441. #endif
  442. retval = shpc_start_thread();
  443. if (retval)
  444. goto error_hpc_init;
  445. retval = pci_register_driver(&shpc_driver);
  446. dbg("%s: pci_register_driver = %d\n", __FUNCTION__, retval);
  447. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  448. error_hpc_init:
  449. if (retval) {
  450. shpchp_event_stop_thread();
  451. }
  452. return retval;
  453. }
  454. static void __exit shpcd_cleanup(void)
  455. {
  456. dbg("unload_shpchpd()\n");
  457. unload_shpchpd();
  458. dbg("pci_unregister_driver\n");
  459. pci_unregister_driver(&shpc_driver);
  460. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  461. }
  462. module_init(shpcd_init);
  463. module_exit(shpcd_cleanup);