sgi_hotplug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 "../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. #define PCIIO_ASIC_TYPE_TIOCA 4
  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 PCI_L1_QSIZE 128 /* our L1 message buffer size */
  39. #define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
  40. #define SGI_HOTPLUG_PROM_REV 0x0430 /* Min. required PROM version */
  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. .owner = THIS_MODULE,
  70. .enable_slot = enable_slot,
  71. .disable_slot = disable_slot,
  72. .get_power_status = get_power_status,
  73. };
  74. static DEFINE_MUTEX(sn_hotplug_mutex);
  75. static ssize_t path_show (struct hotplug_slot *bss_hotplug_slot,
  76. char *buf)
  77. {
  78. int retval = -ENOENT;
  79. struct slot *slot = bss_hotplug_slot->private;
  80. if (!slot)
  81. return retval;
  82. retval = sprintf (buf, "%s\n", slot->physical_path);
  83. return retval;
  84. }
  85. static struct hotplug_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. {
  133. struct pcibus_info *pcibus_info;
  134. struct slot *slot;
  135. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
  136. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  137. if (!slot)
  138. return -ENOMEM;
  139. bss_hotplug_slot->private = slot;
  140. bss_hotplug_slot->name = kmalloc(SN_SLOT_NAME_SIZE, GFP_KERNEL);
  141. if (!bss_hotplug_slot->name) {
  142. kfree(bss_hotplug_slot->private);
  143. return -ENOMEM;
  144. }
  145. slot->device_num = device;
  146. slot->pci_bus = pci_bus;
  147. sprintf(bss_hotplug_slot->name, "%04x:%02x:%02x",
  148. pci_domain_nr(pci_bus),
  149. ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
  150. device + 1);
  151. sn_generate_path(pci_bus, slot->physical_path);
  152. slot->hotplug_slot = bss_hotplug_slot;
  153. list_add(&slot->hp_list, &sn_hp_list);
  154. return 0;
  155. }
  156. static struct hotplug_slot * sn_hp_destroy(void)
  157. {
  158. struct slot *slot;
  159. struct hotplug_slot *bss_hotplug_slot = NULL;
  160. list_for_each_entry(slot, &sn_hp_list, hp_list) {
  161. bss_hotplug_slot = slot->hotplug_slot;
  162. list_del(&((struct slot *)bss_hotplug_slot->private)->
  163. hp_list);
  164. sysfs_remove_file(&bss_hotplug_slot->kobj,
  165. &sn_slot_path_attr.attr);
  166. break;
  167. }
  168. return bss_hotplug_slot;
  169. }
  170. static void sn_bus_alloc_data(struct pci_dev *dev)
  171. {
  172. struct pci_bus *subordinate_bus;
  173. struct pci_dev *child;
  174. sn_pci_fixup_slot(dev);
  175. /* Recursively sets up the sn_irq_info structs */
  176. if (dev->subordinate) {
  177. subordinate_bus = dev->subordinate;
  178. list_for_each_entry(child, &subordinate_bus->devices, bus_list)
  179. sn_bus_alloc_data(child);
  180. }
  181. }
  182. static void sn_bus_free_data(struct pci_dev *dev)
  183. {
  184. struct pci_bus *subordinate_bus;
  185. struct pci_dev *child;
  186. /* Recursively clean up sn_irq_info structs */
  187. if (dev->subordinate) {
  188. subordinate_bus = dev->subordinate;
  189. list_for_each_entry(child, &subordinate_bus->devices, bus_list)
  190. sn_bus_free_data(child);
  191. }
  192. /*
  193. * Some drivers may use dma accesses during the
  194. * driver remove function. We release the sysdata
  195. * areas after the driver remove functions have
  196. * been called.
  197. */
  198. sn_bus_store_sysdata(dev);
  199. sn_pci_unfixup_slot(dev);
  200. }
  201. static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
  202. int device_num)
  203. {
  204. struct slot *slot = bss_hotplug_slot->private;
  205. struct pcibus_info *pcibus_info;
  206. struct pcibr_slot_enable_resp resp;
  207. int rc;
  208. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  209. /*
  210. * Power-on and initialize the slot in the SN
  211. * PCI infrastructure.
  212. */
  213. rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp);
  214. if (rc == PCI_SLOT_ALREADY_UP) {
  215. dev_dbg(slot->pci_bus->self, "is already active\n");
  216. return 1; /* return 1 to user */
  217. }
  218. if (rc == PCI_L1_ERR) {
  219. dev_dbg(slot->pci_bus->self,
  220. "L1 failure %d with message: %s",
  221. resp.resp_sub_errno, resp.resp_l1_msg);
  222. return -EPERM;
  223. }
  224. if (rc) {
  225. dev_dbg(slot->pci_bus->self,
  226. "insert failed with error %d sub-error %d\n",
  227. rc, resp.resp_sub_errno);
  228. return -EIO;
  229. }
  230. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  231. pcibus_info->pbi_enabled_devices |= (1 << device_num);
  232. return 0;
  233. }
  234. static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
  235. int device_num, int action)
  236. {
  237. struct slot *slot = bss_hotplug_slot->private;
  238. struct pcibus_info *pcibus_info;
  239. struct pcibr_slot_disable_resp resp;
  240. int rc;
  241. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  242. rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
  243. if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
  244. (rc == PCI_SLOT_ALREADY_DOWN)) {
  245. dev_dbg(slot->pci_bus->self, "Slot %s already inactive\n");
  246. return 1; /* return 1 to user */
  247. }
  248. if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
  249. dev_dbg(slot->pci_bus->self,
  250. "Cannot remove last 33MHz card\n");
  251. return -EPERM;
  252. }
  253. if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
  254. dev_dbg(slot->pci_bus->self,
  255. "L1 failure %d with message \n%s\n",
  256. resp.resp_sub_errno, resp.resp_l1_msg);
  257. return -EPERM;
  258. }
  259. if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
  260. dev_dbg(slot->pci_bus->self,
  261. "remove failed with error %d sub-error %d\n",
  262. rc, resp.resp_sub_errno);
  263. return -EIO;
  264. }
  265. if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
  266. return 0;
  267. if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
  268. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  269. pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
  270. dev_dbg(slot->pci_bus->self, "remove successful\n");
  271. return 0;
  272. }
  273. if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
  274. dev_dbg(slot->pci_bus->self,"remove failed rc = %d\n", rc);
  275. }
  276. return rc;
  277. }
  278. static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
  279. {
  280. struct slot *slot = bss_hotplug_slot->private;
  281. struct pci_bus *new_bus = NULL;
  282. struct pci_dev *dev;
  283. int func, num_funcs;
  284. int new_ppb = 0;
  285. int rc;
  286. /* Serialize the Linux PCI infrastructure */
  287. mutex_lock(&sn_hotplug_mutex);
  288. /*
  289. * Power-on and initialize the slot in the SN
  290. * PCI infrastructure.
  291. */
  292. rc = sn_slot_enable(bss_hotplug_slot, slot->device_num);
  293. if (rc) {
  294. mutex_unlock(&sn_hotplug_mutex);
  295. return rc;
  296. }
  297. num_funcs = pci_scan_slot(slot->pci_bus,
  298. PCI_DEVFN(slot->device_num + 1, 0));
  299. if (!num_funcs) {
  300. dev_dbg(slot->pci_bus->self, "no device in slot\n");
  301. mutex_unlock(&sn_hotplug_mutex);
  302. return -ENODEV;
  303. }
  304. sn_pci_controller_fixup(pci_domain_nr(slot->pci_bus),
  305. slot->pci_bus->number,
  306. slot->pci_bus);
  307. /*
  308. * Map SN resources for all functions on the card
  309. * to the Linux PCI interface and tell the drivers
  310. * about them.
  311. */
  312. for (func = 0; func < num_funcs; func++) {
  313. dev = pci_get_slot(slot->pci_bus,
  314. PCI_DEVFN(slot->device_num + 1,
  315. PCI_FUNC(func)));
  316. if (dev) {
  317. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  318. unsigned char sec_bus;
  319. pci_read_config_byte(dev, PCI_SECONDARY_BUS,
  320. &sec_bus);
  321. new_bus = pci_add_new_bus(dev->bus, dev,
  322. sec_bus);
  323. pci_scan_child_bus(new_bus);
  324. sn_pci_controller_fixup(pci_domain_nr(new_bus),
  325. new_bus->number,
  326. new_bus);
  327. new_ppb = 1;
  328. }
  329. sn_bus_alloc_data(dev);
  330. pci_dev_put(dev);
  331. }
  332. }
  333. /* Call the driver for the new device */
  334. pci_bus_add_devices(slot->pci_bus);
  335. /* Call the drivers for the new devices subordinate to PPB */
  336. if (new_ppb)
  337. pci_bus_add_devices(new_bus);
  338. mutex_unlock(&sn_hotplug_mutex);
  339. if (rc == 0)
  340. dev_dbg(slot->pci_bus->self,
  341. "insert operation successful\n");
  342. else
  343. dev_dbg(slot->pci_bus->self,
  344. "insert operation failed rc = %d\n", rc);
  345. return rc;
  346. }
  347. static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
  348. {
  349. struct slot *slot = bss_hotplug_slot->private;
  350. struct pci_dev *dev;
  351. int func;
  352. int rc;
  353. /* Acquire update access to the bus */
  354. mutex_lock(&sn_hotplug_mutex);
  355. /* is it okay to bring this slot down? */
  356. rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
  357. PCI_REQ_SLOT_ELIGIBLE);
  358. if (rc)
  359. goto leaving;
  360. /* Free the SN resources assigned to the Linux device.*/
  361. for (func = 0; func < 8; func++) {
  362. dev = pci_get_slot(slot->pci_bus,
  363. PCI_DEVFN(slot->device_num + 1,
  364. PCI_FUNC(func)));
  365. if (dev) {
  366. sn_bus_free_data(dev);
  367. pci_remove_bus_device(dev);
  368. pci_dev_put(dev);
  369. }
  370. }
  371. /* free the collected sysdata pointers */
  372. sn_bus_free_sysdata();
  373. /* Deactivate slot */
  374. rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
  375. PCI_REQ_SLOT_DISABLE);
  376. leaving:
  377. /* Release the bus lock */
  378. mutex_unlock(&sn_hotplug_mutex);
  379. return rc;
  380. }
  381. static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
  382. u8 *value)
  383. {
  384. struct slot *slot = bss_hotplug_slot->private;
  385. struct pcibus_info *pcibus_info;
  386. u32 power;
  387. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  388. mutex_lock(&sn_hotplug_mutex);
  389. power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
  390. *value = power ? 1 : 0;
  391. mutex_unlock(&sn_hotplug_mutex);
  392. return 0;
  393. }
  394. static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
  395. {
  396. kfree(bss_hotplug_slot->info);
  397. kfree(bss_hotplug_slot->name);
  398. kfree(bss_hotplug_slot->private);
  399. kfree(bss_hotplug_slot);
  400. }
  401. static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
  402. {
  403. int device;
  404. struct hotplug_slot *bss_hotplug_slot;
  405. int rc = 0;
  406. /*
  407. * Currently only four devices are supported,
  408. * in the future there maybe more -- up to 32.
  409. */
  410. for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
  411. if (sn_pci_slot_valid(pci_bus, device) != 1)
  412. continue;
  413. bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
  414. GFP_KERNEL);
  415. if (!bss_hotplug_slot) {
  416. rc = -ENOMEM;
  417. goto alloc_err;
  418. }
  419. bss_hotplug_slot->info =
  420. kzalloc(sizeof(struct hotplug_slot_info),
  421. GFP_KERNEL);
  422. if (!bss_hotplug_slot->info) {
  423. rc = -ENOMEM;
  424. goto alloc_err;
  425. }
  426. if (sn_hp_slot_private_alloc(bss_hotplug_slot,
  427. pci_bus, device)) {
  428. rc = -ENOMEM;
  429. goto alloc_err;
  430. }
  431. bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
  432. bss_hotplug_slot->release = &sn_release_slot;
  433. rc = pci_hp_register(bss_hotplug_slot);
  434. if (rc)
  435. goto register_err;
  436. rc = sysfs_create_file(&bss_hotplug_slot->kobj,
  437. &sn_slot_path_attr.attr);
  438. if (rc)
  439. goto register_err;
  440. }
  441. dev_dbg(pci_bus->self, "Registered bus with hotplug\n");
  442. return rc;
  443. register_err:
  444. dev_dbg(pci_bus->self, "bus failed to register with err = %d\n",
  445. rc);
  446. alloc_err:
  447. if (rc == -ENOMEM)
  448. dev_dbg(pci_bus->self, "Memory allocation error\n");
  449. /* destroy THIS element */
  450. if (bss_hotplug_slot)
  451. sn_release_slot(bss_hotplug_slot);
  452. /* destroy anything else on the list */
  453. while ((bss_hotplug_slot = sn_hp_destroy()))
  454. pci_hp_deregister(bss_hotplug_slot);
  455. return rc;
  456. }
  457. static int sn_pci_hotplug_init(void)
  458. {
  459. struct pci_bus *pci_bus = NULL;
  460. int rc;
  461. int registered = 0;
  462. if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
  463. printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
  464. __FUNCTION__);
  465. return -EPERM;
  466. }
  467. INIT_LIST_HEAD(&sn_hp_list);
  468. while ((pci_bus = pci_find_next_bus(pci_bus))) {
  469. if (!pci_bus->sysdata)
  470. continue;
  471. rc = sn_pci_bus_valid(pci_bus);
  472. if (rc != 1) {
  473. dev_dbg(pci_bus->self, "not a valid hotplug bus\n");
  474. continue;
  475. }
  476. dev_dbg(pci_bus->self, "valid hotplug bus\n");
  477. rc = sn_hotplug_slot_register(pci_bus);
  478. if (!rc) {
  479. registered = 1;
  480. } else {
  481. registered = 0;
  482. break;
  483. }
  484. }
  485. return registered == 1 ? 0 : -ENODEV;
  486. }
  487. static void sn_pci_hotplug_exit(void)
  488. {
  489. struct hotplug_slot *bss_hotplug_slot;
  490. while ((bss_hotplug_slot = sn_hp_destroy()))
  491. pci_hp_deregister(bss_hotplug_slot);
  492. if (!list_empty(&sn_hp_list))
  493. printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
  494. }
  495. module_init(sn_pci_hotplug_init);
  496. module_exit(sn_pci_hotplug_exit);