sgi_hotplug.c 18 KB

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