sgi_hotplug.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved.
  4. *
  5. * This work was based on the 2.4/2.6 kernel development by Dick Reigner.
  6. * Work to add BIOS PROM support was completed by Mike Habeck.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci_hotplug.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/mutex.h>
  18. #include <asm/sn/addrs.h>
  19. #include <asm/sn/geo.h>
  20. #include <asm/sn/l1.h>
  21. #include <asm/sn/module.h>
  22. #include <asm/sn/pcibr_provider.h>
  23. #include <asm/sn/pcibus_provider_defs.h>
  24. #include <asm/sn/pcidev.h>
  25. #include <asm/sn/sn_feature_sets.h>
  26. #include <asm/sn/sn_sal.h>
  27. #include <asm/sn/types.h>
  28. #include <asm/sn/acpi.h>
  29. #include "../pci.h"
  30. MODULE_LICENSE("GPL");
  31. MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
  32. MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
  33. /* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
  34. #define PCI_SLOT_ALREADY_UP 2 /* slot already up */
  35. #define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
  36. #define PCI_L1_ERR 7 /* L1 console command error */
  37. #define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
  38. #define PCIIO_ASIC_TYPE_TIOCA 4
  39. #define PCI_L1_QSIZE 128 /* our L1 message buffer size */
  40. #define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
  41. #define SN_SLOT_NAME_SIZE 33 /* size of name string */
  42. /* internal list head */
  43. static struct list_head sn_hp_list;
  44. /* hotplug_slot struct's private pointer */
  45. struct slot {
  46. int device_num;
  47. struct pci_bus *pci_bus;
  48. /* this struct for glue internal only */
  49. struct hotplug_slot hotplug_slot;
  50. struct list_head hp_list;
  51. char physical_path[SN_SLOT_NAME_SIZE];
  52. };
  53. struct pcibr_slot_enable_resp {
  54. int resp_sub_errno;
  55. char resp_l1_msg[PCI_L1_QSIZE + 1];
  56. };
  57. struct pcibr_slot_disable_resp {
  58. int resp_sub_errno;
  59. char resp_l1_msg[PCI_L1_QSIZE + 1];
  60. };
  61. enum sn_pci_req_e {
  62. PCI_REQ_SLOT_ELIGIBLE,
  63. PCI_REQ_SLOT_DISABLE
  64. };
  65. static int enable_slot(struct hotplug_slot *slot);
  66. static int disable_slot(struct hotplug_slot *slot);
  67. static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
  68. static const struct hotplug_slot_ops sn_hotplug_slot_ops = {
  69. .enable_slot = enable_slot,
  70. .disable_slot = disable_slot,
  71. .get_power_status = get_power_status,
  72. };
  73. static DEFINE_MUTEX(sn_hotplug_mutex);
  74. static struct slot *to_slot(struct hotplug_slot *bss_hotplug_slot)
  75. {
  76. return container_of(bss_hotplug_slot, struct slot, hotplug_slot);
  77. }
  78. static ssize_t path_show(struct pci_slot *pci_slot, char *buf)
  79. {
  80. int retval = -ENOENT;
  81. struct slot *slot = to_slot(pci_slot->hotplug);
  82. if (!slot)
  83. return retval;
  84. retval = sprintf(buf, "%s\n", slot->physical_path);
  85. return retval;
  86. }
  87. static struct pci_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
  88. static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
  89. {
  90. struct pcibus_info *pcibus_info;
  91. u16 busnum, segment, ioboard_type;
  92. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
  93. /* Check to see if this is a valid slot on 'pci_bus' */
  94. if (!(pcibus_info->pbi_valid_devices & (1 << device)))
  95. return -EPERM;
  96. ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
  97. busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
  98. segment = pci_domain_nr(pci_bus) & 0xf;
  99. /* Do not allow hotplug operations on base I/O cards */
  100. if ((ioboard_type == L1_BRICKTYPE_IX ||
  101. ioboard_type == L1_BRICKTYPE_IA) &&
  102. (segment == 1 && busnum == 0 && device != 1))
  103. return -EPERM;
  104. return 1;
  105. }
  106. static int sn_pci_bus_valid(struct pci_bus *pci_bus)
  107. {
  108. struct pcibus_info *pcibus_info;
  109. u32 asic_type;
  110. u16 ioboard_type;
  111. /* Don't register slots hanging off the TIOCA bus */
  112. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
  113. asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
  114. if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
  115. return -EPERM;
  116. /* Only register slots in I/O Bricks that support hotplug */
  117. ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
  118. switch (ioboard_type) {
  119. case L1_BRICKTYPE_IX:
  120. case L1_BRICKTYPE_PX:
  121. case L1_BRICKTYPE_IA:
  122. case L1_BRICKTYPE_PA:
  123. case L1_BOARDTYPE_PCIX3SLOT:
  124. return 1;
  125. break;
  126. default:
  127. return -EPERM;
  128. break;
  129. }
  130. return -EIO;
  131. }
  132. static int sn_hp_slot_private_alloc(struct hotplug_slot **bss_hotplug_slot,
  133. struct pci_bus *pci_bus, int device,
  134. char *name)
  135. {
  136. struct pcibus_info *pcibus_info;
  137. struct slot *slot;
  138. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
  139. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  140. if (!slot)
  141. return -ENOMEM;
  142. slot->device_num = device;
  143. slot->pci_bus = pci_bus;
  144. sprintf(name, "%04x:%02x:%02x",
  145. pci_domain_nr(pci_bus),
  146. ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
  147. device + 1);
  148. sn_generate_path(pci_bus, slot->physical_path);
  149. list_add(&slot->hp_list, &sn_hp_list);
  150. *bss_hotplug_slot = &slot->hotplug_slot;
  151. return 0;
  152. }
  153. static struct hotplug_slot *sn_hp_destroy(void)
  154. {
  155. struct slot *slot;
  156. struct pci_slot *pci_slot;
  157. struct hotplug_slot *bss_hotplug_slot = NULL;
  158. list_for_each_entry(slot, &sn_hp_list, hp_list) {
  159. bss_hotplug_slot = &slot->hotplug_slot;
  160. pci_slot = bss_hotplug_slot->pci_slot;
  161. list_del(&slot->hp_list);
  162. sysfs_remove_file(&pci_slot->kobj,
  163. &sn_slot_path_attr.attr);
  164. break;
  165. }
  166. return bss_hotplug_slot;
  167. }
  168. static void sn_bus_free_data(struct pci_dev *dev)
  169. {
  170. struct pci_bus *subordinate_bus;
  171. struct pci_dev *child;
  172. /* Recursively clean up sn_irq_info structs */
  173. if (dev->subordinate) {
  174. subordinate_bus = dev->subordinate;
  175. list_for_each_entry(child, &subordinate_bus->devices, bus_list)
  176. sn_bus_free_data(child);
  177. }
  178. /*
  179. * Some drivers may use dma accesses during the
  180. * driver remove function. We release the sysdata
  181. * areas after the driver remove functions have
  182. * been called.
  183. */
  184. sn_bus_store_sysdata(dev);
  185. sn_pci_unfixup_slot(dev);
  186. }
  187. static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
  188. int device_num, char **ssdt)
  189. {
  190. struct slot *slot = to_slot(bss_hotplug_slot);
  191. struct pcibus_info *pcibus_info;
  192. struct pcibr_slot_enable_resp resp;
  193. int rc;
  194. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  195. /*
  196. * Power-on and initialize the slot in the SN
  197. * PCI infrastructure.
  198. */
  199. rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
  200. if (rc == PCI_SLOT_ALREADY_UP) {
  201. pci_dbg(slot->pci_bus->self, "is already active\n");
  202. return 1; /* return 1 to user */
  203. }
  204. if (rc == PCI_L1_ERR) {
  205. pci_dbg(slot->pci_bus->self, "L1 failure %d with message: %s",
  206. resp.resp_sub_errno, resp.resp_l1_msg);
  207. return -EPERM;
  208. }
  209. if (rc) {
  210. pci_dbg(slot->pci_bus->self, "insert failed with error %d sub-error %d\n",
  211. rc, resp.resp_sub_errno);
  212. return -EIO;
  213. }
  214. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  215. pcibus_info->pbi_enabled_devices |= (1 << device_num);
  216. return 0;
  217. }
  218. static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
  219. int device_num, int action)
  220. {
  221. struct slot *slot = to_slot(bss_hotplug_slot);
  222. struct pcibus_info *pcibus_info;
  223. struct pcibr_slot_disable_resp resp;
  224. int rc;
  225. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  226. rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
  227. if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
  228. (rc == PCI_SLOT_ALREADY_DOWN)) {
  229. pci_dbg(slot->pci_bus->self, "Slot %s already inactive\n", slot->physical_path);
  230. return 1; /* return 1 to user */
  231. }
  232. if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
  233. pci_dbg(slot->pci_bus->self, "Cannot remove last 33MHz card\n");
  234. return -EPERM;
  235. }
  236. if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
  237. pci_dbg(slot->pci_bus->self, "L1 failure %d with message \n%s\n",
  238. resp.resp_sub_errno, resp.resp_l1_msg);
  239. return -EPERM;
  240. }
  241. if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
  242. pci_dbg(slot->pci_bus->self, "remove failed with error %d sub-error %d\n",
  243. rc, resp.resp_sub_errno);
  244. return -EIO;
  245. }
  246. if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
  247. return 0;
  248. if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
  249. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  250. pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
  251. pci_dbg(slot->pci_bus->self, "remove successful\n");
  252. return 0;
  253. }
  254. if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
  255. pci_dbg(slot->pci_bus->self, "remove failed rc = %d\n", rc);
  256. }
  257. return rc;
  258. }
  259. /*
  260. * Power up and configure the slot via a SAL call to PROM.
  261. * Scan slot (and any children), do any platform specific fixup,
  262. * and find device driver.
  263. */
  264. static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
  265. {
  266. struct slot *slot = to_slot(bss_hotplug_slot);
  267. struct pci_bus *new_bus = NULL;
  268. struct pci_dev *dev;
  269. int num_funcs;
  270. int new_ppb = 0;
  271. int rc;
  272. char *ssdt = NULL;
  273. void pcibios_fixup_device_resources(struct pci_dev *);
  274. /* Serialize the Linux PCI infrastructure */
  275. mutex_lock(&sn_hotplug_mutex);
  276. /*
  277. * Power-on and initialize the slot in the SN
  278. * PCI infrastructure. Also, retrieve the ACPI SSDT
  279. * table for the slot (if ACPI capable PROM).
  280. */
  281. rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
  282. if (rc) {
  283. mutex_unlock(&sn_hotplug_mutex);
  284. return rc;
  285. }
  286. if (ssdt)
  287. ssdt = __va(ssdt);
  288. /* Add the new SSDT for the slot to the ACPI namespace */
  289. if (SN_ACPI_BASE_SUPPORT() && ssdt) {
  290. acpi_status ret;
  291. ret = acpi_load_table((struct acpi_table_header *)ssdt);
  292. if (ACPI_FAILURE(ret)) {
  293. printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
  294. __func__, ret);
  295. /* try to continue on */
  296. }
  297. }
  298. num_funcs = pci_scan_slot(slot->pci_bus,
  299. PCI_DEVFN(slot->device_num + 1, 0));
  300. if (!num_funcs) {
  301. pci_dbg(slot->pci_bus->self, "no device in slot\n");
  302. mutex_unlock(&sn_hotplug_mutex);
  303. return -ENODEV;
  304. }
  305. /*
  306. * Map SN resources for all functions on the card
  307. * to the Linux PCI interface and tell the drivers
  308. * about them.
  309. */
  310. list_for_each_entry(dev, &slot->pci_bus->devices, bus_list) {
  311. if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
  312. continue;
  313. /* Need to do slot fixup on PPB before fixup of children
  314. * (PPB's pcidev_info needs to be in pcidev_info list
  315. * before child's SN_PCIDEV_INFO() call to setup
  316. * pdi_host_pcidev_info).
  317. */
  318. pcibios_fixup_device_resources(dev);
  319. if (SN_ACPI_BASE_SUPPORT())
  320. sn_acpi_slot_fixup(dev);
  321. else
  322. sn_io_slot_fixup(dev);
  323. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  324. pci_hp_add_bridge(dev);
  325. if (dev->subordinate) {
  326. new_bus = dev->subordinate;
  327. new_ppb = 1;
  328. }
  329. }
  330. }
  331. /*
  332. * Add the slot's devices to the ACPI infrastructure */
  333. if (SN_ACPI_BASE_SUPPORT() && ssdt) {
  334. unsigned long long adr;
  335. struct acpi_device *pdevice;
  336. acpi_handle phandle;
  337. acpi_handle chandle = NULL;
  338. acpi_handle rethandle;
  339. acpi_status ret;
  340. phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
  341. if (acpi_bus_get_device(phandle, &pdevice)) {
  342. pci_dbg(slot->pci_bus->self, "no parent device, assuming NULL\n");
  343. pdevice = NULL;
  344. }
  345. acpi_scan_lock_acquire();
  346. /*
  347. * Walk the rootbus node's immediate children looking for
  348. * the slot's device node(s). There can be more than
  349. * one for multifunction devices.
  350. */
  351. for (;;) {
  352. rethandle = NULL;
  353. ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
  354. phandle, chandle,
  355. &rethandle);
  356. if (ret == AE_NOT_FOUND || rethandle == NULL)
  357. break;
  358. chandle = rethandle;
  359. ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
  360. NULL, &adr);
  361. if (ACPI_SUCCESS(ret) &&
  362. (adr>>16) == (slot->device_num + 1)) {
  363. ret = acpi_bus_scan(chandle);
  364. if (ACPI_FAILURE(ret)) {
  365. printk(KERN_ERR "%s: acpi_bus_scan failed (0x%x) for slot %d func %d\n",
  366. __func__, ret, (int)(adr>>16),
  367. (int)(adr&0xffff));
  368. /* try to continue on */
  369. }
  370. }
  371. }
  372. acpi_scan_lock_release();
  373. }
  374. pci_lock_rescan_remove();
  375. /* Call the driver for the new device */
  376. pci_bus_add_devices(slot->pci_bus);
  377. /* Call the drivers for the new devices subordinate to PPB */
  378. if (new_ppb)
  379. pci_bus_add_devices(new_bus);
  380. pci_unlock_rescan_remove();
  381. mutex_unlock(&sn_hotplug_mutex);
  382. if (rc == 0)
  383. pci_dbg(slot->pci_bus->self, "insert operation successful\n");
  384. else
  385. pci_dbg(slot->pci_bus->self, "insert operation failed rc = %d\n", rc);
  386. return rc;
  387. }
  388. static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
  389. {
  390. struct slot *slot = to_slot(bss_hotplug_slot);
  391. struct pci_dev *dev, *temp;
  392. int rc;
  393. acpi_handle ssdt_hdl = NULL;
  394. /* Acquire update access to the bus */
  395. mutex_lock(&sn_hotplug_mutex);
  396. /* is it okay to bring this slot down? */
  397. rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
  398. PCI_REQ_SLOT_ELIGIBLE);
  399. if (rc)
  400. goto leaving;
  401. /* free the ACPI resources for the slot */
  402. if (SN_ACPI_BASE_SUPPORT() &&
  403. PCI_CONTROLLER(slot->pci_bus)->companion) {
  404. unsigned long long adr;
  405. struct acpi_device *device;
  406. acpi_handle phandle;
  407. acpi_handle chandle = NULL;
  408. acpi_handle rethandle;
  409. acpi_status ret;
  410. /* Get the rootbus node pointer */
  411. phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
  412. acpi_scan_lock_acquire();
  413. /*
  414. * Walk the rootbus node's immediate children looking for
  415. * the slot's device node(s). There can be more than
  416. * one for multifunction devices.
  417. */
  418. for (;;) {
  419. rethandle = NULL;
  420. ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
  421. phandle, chandle,
  422. &rethandle);
  423. if (ret == AE_NOT_FOUND || rethandle == NULL)
  424. break;
  425. chandle = rethandle;
  426. ret = acpi_evaluate_integer(chandle,
  427. METHOD_NAME__ADR,
  428. NULL, &adr);
  429. if (ACPI_SUCCESS(ret) &&
  430. (adr>>16) == (slot->device_num + 1)) {
  431. /* retain the owner id */
  432. ssdt_hdl = chandle;
  433. ret = acpi_bus_get_device(chandle,
  434. &device);
  435. if (ACPI_SUCCESS(ret))
  436. acpi_bus_trim(device);
  437. }
  438. }
  439. acpi_scan_lock_release();
  440. }
  441. pci_lock_rescan_remove();
  442. /* Free the SN resources assigned to the Linux device.*/
  443. list_for_each_entry_safe(dev, temp, &slot->pci_bus->devices, bus_list) {
  444. if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
  445. continue;
  446. pci_dev_get(dev);
  447. sn_bus_free_data(dev);
  448. pci_stop_and_remove_bus_device(dev);
  449. pci_dev_put(dev);
  450. }
  451. pci_unlock_rescan_remove();
  452. /* Remove the SSDT for the slot from the ACPI namespace */
  453. if (SN_ACPI_BASE_SUPPORT() && ssdt_hdl) {
  454. acpi_status ret;
  455. ret = acpi_unload_parent_table(ssdt_hdl);
  456. if (ACPI_FAILURE(ret)) {
  457. acpi_handle_err(ssdt_hdl,
  458. "%s: acpi_unload_parent_table failed (0x%x)\n",
  459. __func__, ret);
  460. /* try to continue on */
  461. }
  462. }
  463. /* free the collected sysdata pointers */
  464. sn_bus_free_sysdata();
  465. /* Deactivate slot */
  466. rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
  467. PCI_REQ_SLOT_DISABLE);
  468. leaving:
  469. /* Release the bus lock */
  470. mutex_unlock(&sn_hotplug_mutex);
  471. return rc;
  472. }
  473. static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
  474. u8 *value)
  475. {
  476. struct slot *slot = to_slot(bss_hotplug_slot);
  477. struct pcibus_info *pcibus_info;
  478. u32 power;
  479. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  480. mutex_lock(&sn_hotplug_mutex);
  481. power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
  482. *value = power ? 1 : 0;
  483. mutex_unlock(&sn_hotplug_mutex);
  484. return 0;
  485. }
  486. static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
  487. {
  488. kfree(to_slot(bss_hotplug_slot));
  489. }
  490. static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
  491. {
  492. int device;
  493. struct pci_slot *pci_slot;
  494. struct hotplug_slot *bss_hotplug_slot;
  495. char name[SN_SLOT_NAME_SIZE];
  496. int rc = 0;
  497. /*
  498. * Currently only four devices are supported,
  499. * in the future there maybe more -- up to 32.
  500. */
  501. for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
  502. if (sn_pci_slot_valid(pci_bus, device) != 1)
  503. continue;
  504. if (sn_hp_slot_private_alloc(&bss_hotplug_slot,
  505. pci_bus, device, name)) {
  506. rc = -ENOMEM;
  507. goto alloc_err;
  508. }
  509. bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
  510. rc = pci_hp_register(bss_hotplug_slot, pci_bus, device, name);
  511. if (rc)
  512. goto register_err;
  513. pci_slot = bss_hotplug_slot->pci_slot;
  514. rc = sysfs_create_file(&pci_slot->kobj,
  515. &sn_slot_path_attr.attr);
  516. if (rc)
  517. goto alloc_err;
  518. }
  519. pci_dbg(pci_bus->self, "Registered bus with hotplug\n");
  520. return rc;
  521. register_err:
  522. pci_dbg(pci_bus->self, "bus failed to register with err = %d\n",
  523. rc);
  524. /* destroy THIS element */
  525. sn_hp_destroy();
  526. sn_release_slot(bss_hotplug_slot);
  527. alloc_err:
  528. /* destroy anything else on the list */
  529. while ((bss_hotplug_slot = sn_hp_destroy())) {
  530. pci_hp_deregister(bss_hotplug_slot);
  531. sn_release_slot(bss_hotplug_slot);
  532. }
  533. return rc;
  534. }
  535. static int __init sn_pci_hotplug_init(void)
  536. {
  537. struct pci_bus *pci_bus = NULL;
  538. int rc;
  539. int registered = 0;
  540. if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
  541. printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
  542. __func__);
  543. return -EPERM;
  544. }
  545. INIT_LIST_HEAD(&sn_hp_list);
  546. while ((pci_bus = pci_find_next_bus(pci_bus))) {
  547. if (!pci_bus->sysdata)
  548. continue;
  549. rc = sn_pci_bus_valid(pci_bus);
  550. if (rc != 1) {
  551. pci_dbg(pci_bus->self, "not a valid hotplug bus\n");
  552. continue;
  553. }
  554. pci_dbg(pci_bus->self, "valid hotplug bus\n");
  555. rc = sn_hotplug_slot_register(pci_bus);
  556. if (!rc) {
  557. registered = 1;
  558. } else {
  559. registered = 0;
  560. break;
  561. }
  562. }
  563. return registered == 1 ? 0 : -ENODEV;
  564. }
  565. static void __exit sn_pci_hotplug_exit(void)
  566. {
  567. struct hotplug_slot *bss_hotplug_slot;
  568. while ((bss_hotplug_slot = sn_hp_destroy())) {
  569. pci_hp_deregister(bss_hotplug_slot);
  570. sn_release_slot(bss_hotplug_slot);
  571. }
  572. if (!list_empty(&sn_hp_list))
  573. printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
  574. }
  575. module_init(sn_pci_hotplug_init);
  576. module_exit(sn_pci_hotplug_exit);