sgi_hotplug.c 18 KB

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