sgi_hotplug.c 18 KB

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