sgi_hotplug.c 18 KB

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