sgi_hotplug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 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/proc_fs.h>
  16. #include <linux/types.h>
  17. #include <asm/sn/addrs.h>
  18. #include <asm/sn/l1.h>
  19. #include <asm/sn/module.h>
  20. #include <asm/sn/pcibr_provider.h>
  21. #include <asm/sn/pcibus_provider_defs.h>
  22. #include <asm/sn/pcidev.h>
  23. #include <asm/sn/sn_sal.h>
  24. #include <asm/sn/types.h>
  25. #include "../pci.h"
  26. #include "pci_hotplug.h"
  27. MODULE_LICENSE("GPL");
  28. MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
  29. MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
  30. #define PCIIO_ASIC_TYPE_TIOCA 4
  31. #define PCI_SLOT_ALREADY_UP 2 /* slot already up */
  32. #define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
  33. #define PCI_L1_ERR 7 /* L1 console command error */
  34. #define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
  35. #define PCI_L1_QSIZE 128 /* our L1 message buffer size */
  36. #define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
  37. #define SGI_HOTPLUG_PROM_REV 0x0430 /* Min. required PROM version */
  38. #define SN_SLOT_NAME_SIZE 33 /* size of name string */
  39. /* internal list head */
  40. static struct list_head sn_hp_list;
  41. /* hotplug_slot struct's private pointer */
  42. struct slot {
  43. int device_num;
  44. struct pci_bus *pci_bus;
  45. /* this struct for glue internal only */
  46. struct hotplug_slot *hotplug_slot;
  47. struct list_head hp_list;
  48. char physical_path[SN_SLOT_NAME_SIZE];
  49. };
  50. struct pcibr_slot_enable_resp {
  51. int resp_sub_errno;
  52. char resp_l1_msg[PCI_L1_QSIZE + 1];
  53. };
  54. struct pcibr_slot_disable_resp {
  55. int resp_sub_errno;
  56. char resp_l1_msg[PCI_L1_QSIZE + 1];
  57. };
  58. enum sn_pci_req_e {
  59. PCI_REQ_SLOT_ELIGIBLE,
  60. PCI_REQ_SLOT_DISABLE
  61. };
  62. static int enable_slot(struct hotplug_slot *slot);
  63. static int disable_slot(struct hotplug_slot *slot);
  64. static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
  65. static struct hotplug_slot_ops sn_hotplug_slot_ops = {
  66. .owner = THIS_MODULE,
  67. .enable_slot = enable_slot,
  68. .disable_slot = disable_slot,
  69. .get_power_status = get_power_status,
  70. };
  71. static DECLARE_MUTEX(sn_hotplug_sem);
  72. static ssize_t path_show (struct hotplug_slot *bss_hotplug_slot,
  73. char *buf)
  74. {
  75. int retval = -ENOENT;
  76. struct slot *slot = bss_hotplug_slot->private;
  77. if (!slot)
  78. return retval;
  79. retval = sprintf (buf, "%s\n", slot->physical_path);
  80. return retval;
  81. }
  82. static struct hotplug_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
  83. static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
  84. {
  85. struct pcibus_info *pcibus_info;
  86. int bricktype;
  87. int bus_num;
  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. bricktype = MODULE_GET_BTYPE(pcibus_info->pbi_moduleid);
  93. bus_num = pcibus_info->pbi_buscommon.bs_persist_busnum & 0xf;
  94. /* Do not allow hotplug operations on base I/O cards */
  95. if ((bricktype == L1_BRICKTYPE_IX || bricktype == L1_BRICKTYPE_IA) &&
  96. (bus_num == 1 && device != 1))
  97. return -EPERM;
  98. return 1;
  99. }
  100. static int sn_pci_bus_valid(struct pci_bus *pci_bus)
  101. {
  102. struct pcibus_info *pcibus_info;
  103. int asic_type;
  104. int bricktype;
  105. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
  106. /* Don't register slots hanging off the TIOCA bus */
  107. asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
  108. if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
  109. return -EPERM;
  110. /* Only register slots in I/O Bricks that support hotplug */
  111. bricktype = MODULE_GET_BTYPE(pcibus_info->pbi_moduleid);
  112. switch (bricktype) {
  113. case L1_BRICKTYPE_IX:
  114. case L1_BRICKTYPE_PX:
  115. case L1_BRICKTYPE_IA:
  116. case L1_BRICKTYPE_PA:
  117. return 1;
  118. break;
  119. default:
  120. return -EPERM;
  121. break;
  122. }
  123. return -EIO;
  124. }
  125. static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
  126. struct pci_bus *pci_bus, int device)
  127. {
  128. struct pcibus_info *pcibus_info;
  129. struct slot *slot;
  130. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
  131. slot = kcalloc(1, sizeof(*slot), GFP_KERNEL);
  132. if (!slot)
  133. return -ENOMEM;
  134. bss_hotplug_slot->private = slot;
  135. bss_hotplug_slot->name = kmalloc(SN_SLOT_NAME_SIZE, GFP_KERNEL);
  136. if (!bss_hotplug_slot->name) {
  137. kfree(bss_hotplug_slot->private);
  138. return -ENOMEM;
  139. }
  140. slot->device_num = device;
  141. slot->pci_bus = pci_bus;
  142. sprintf(bss_hotplug_slot->name, "%04x:%02x:%02x",
  143. pci_domain_nr(pci_bus),
  144. ((int)pcibus_info->pbi_buscommon.bs_persist_busnum) & 0xf,
  145. device + 1);
  146. sprintf(slot->physical_path, "module_%c%c%c%c%.2d",
  147. '0'+RACK_GET_CLASS(MODULE_GET_RACK(pcibus_info->pbi_moduleid)),
  148. '0'+RACK_GET_GROUP(MODULE_GET_RACK(pcibus_info->pbi_moduleid)),
  149. '0'+RACK_GET_NUM(MODULE_GET_RACK(pcibus_info->pbi_moduleid)),
  150. MODULE_GET_BTCHAR(pcibus_info->pbi_moduleid),
  151. MODULE_GET_BPOS(pcibus_info->pbi_moduleid));
  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. sn_pci_unfixup_slot(dev);
  193. }
  194. static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
  195. int device_num)
  196. {
  197. struct slot *slot = bss_hotplug_slot->private;
  198. struct pcibus_info *pcibus_info;
  199. struct pcibr_slot_enable_resp resp;
  200. int rc;
  201. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  202. /*
  203. * Power-on and initialize the slot in the SN
  204. * PCI infrastructure.
  205. */
  206. rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp);
  207. if (rc == PCI_SLOT_ALREADY_UP) {
  208. dev_dbg(slot->pci_bus->self, "is already active\n");
  209. return 1; /* return 1 to user */
  210. }
  211. if (rc == PCI_L1_ERR) {
  212. dev_dbg(slot->pci_bus->self,
  213. "L1 failure %d with message: %s",
  214. resp.resp_sub_errno, resp.resp_l1_msg);
  215. return -EPERM;
  216. }
  217. if (rc) {
  218. dev_dbg(slot->pci_bus->self,
  219. "insert failed with error %d sub-error %d\n",
  220. rc, resp.resp_sub_errno);
  221. return -EIO;
  222. }
  223. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  224. pcibus_info->pbi_enabled_devices |= (1 << device_num);
  225. return 0;
  226. }
  227. static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
  228. int device_num, int action)
  229. {
  230. struct slot *slot = bss_hotplug_slot->private;
  231. struct pcibus_info *pcibus_info;
  232. struct pcibr_slot_disable_resp resp;
  233. int rc;
  234. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  235. rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
  236. if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
  237. (rc == PCI_SLOT_ALREADY_DOWN)) {
  238. dev_dbg(slot->pci_bus->self, "Slot %s already inactive\n");
  239. return 1; /* return 1 to user */
  240. }
  241. if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
  242. dev_dbg(slot->pci_bus->self,
  243. "Cannot remove last 33MHz card\n");
  244. return -EPERM;
  245. }
  246. if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
  247. dev_dbg(slot->pci_bus->self,
  248. "L1 failure %d with message \n%s\n",
  249. resp.resp_sub_errno, resp.resp_l1_msg);
  250. return -EPERM;
  251. }
  252. if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
  253. dev_dbg(slot->pci_bus->self,
  254. "remove failed with error %d sub-error %d\n",
  255. rc, resp.resp_sub_errno);
  256. return -EIO;
  257. }
  258. if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
  259. return 0;
  260. if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
  261. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  262. pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
  263. dev_dbg(slot->pci_bus->self, "remove successful\n");
  264. return 0;
  265. }
  266. if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
  267. dev_dbg(slot->pci_bus->self,"remove failed rc = %d\n", rc);
  268. }
  269. return rc;
  270. }
  271. static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
  272. {
  273. struct slot *slot = bss_hotplug_slot->private;
  274. struct pci_bus *new_bus = NULL;
  275. struct pci_dev *dev;
  276. int func, num_funcs;
  277. int new_ppb = 0;
  278. int rc;
  279. /* Serialize the Linux PCI infrastructure */
  280. down(&sn_hotplug_sem);
  281. /*
  282. * Power-on and initialize the slot in the SN
  283. * PCI infrastructure.
  284. */
  285. rc = sn_slot_enable(bss_hotplug_slot, slot->device_num);
  286. if (rc) {
  287. up(&sn_hotplug_sem);
  288. return rc;
  289. }
  290. num_funcs = pci_scan_slot(slot->pci_bus,
  291. PCI_DEVFN(slot->device_num + 1, 0));
  292. if (!num_funcs) {
  293. dev_dbg(slot->pci_bus->self, "no device in slot\n");
  294. up(&sn_hotplug_sem);
  295. return -ENODEV;
  296. }
  297. sn_pci_controller_fixup(pci_domain_nr(slot->pci_bus),
  298. slot->pci_bus->number,
  299. slot->pci_bus);
  300. /*
  301. * Map SN resources for all functions on the card
  302. * to the Linux PCI interface and tell the drivers
  303. * about them.
  304. */
  305. for (func = 0; func < num_funcs; func++) {
  306. dev = pci_get_slot(slot->pci_bus,
  307. PCI_DEVFN(slot->device_num + 1,
  308. PCI_FUNC(func)));
  309. if (dev) {
  310. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  311. unsigned char sec_bus;
  312. pci_read_config_byte(dev, PCI_SECONDARY_BUS,
  313. &sec_bus);
  314. new_bus = pci_add_new_bus(dev->bus, dev,
  315. sec_bus);
  316. pci_scan_child_bus(new_bus);
  317. sn_pci_controller_fixup(pci_domain_nr(new_bus),
  318. new_bus->number,
  319. new_bus);
  320. new_ppb = 1;
  321. }
  322. sn_bus_alloc_data(dev);
  323. pci_dev_put(dev);
  324. }
  325. }
  326. /* Call the driver for the new device */
  327. pci_bus_add_devices(slot->pci_bus);
  328. /* Call the drivers for the new devices subordinate to PPB */
  329. if (new_ppb)
  330. pci_bus_add_devices(new_bus);
  331. up(&sn_hotplug_sem);
  332. if (rc == 0)
  333. dev_dbg(slot->pci_bus->self,
  334. "insert operation successful\n");
  335. else
  336. dev_dbg(slot->pci_bus->self,
  337. "insert operation failed rc = %d\n", rc);
  338. return rc;
  339. }
  340. static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
  341. {
  342. struct slot *slot = bss_hotplug_slot->private;
  343. struct pci_dev *dev;
  344. int func;
  345. int rc;
  346. /* Acquire update access to the bus */
  347. down(&sn_hotplug_sem);
  348. /* is it okay to bring this slot down? */
  349. rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
  350. PCI_REQ_SLOT_ELIGIBLE);
  351. if (rc)
  352. goto leaving;
  353. /* Free the SN resources assigned to the Linux device.*/
  354. for (func = 0; func < 8; func++) {
  355. dev = pci_get_slot(slot->pci_bus,
  356. PCI_DEVFN(slot->device_num + 1,
  357. PCI_FUNC(func)));
  358. if (dev) {
  359. /*
  360. * Some drivers may use dma accesses during the
  361. * driver remove function. We release the sysdata
  362. * areas after the driver remove functions have
  363. * been called.
  364. */
  365. sn_bus_store_sysdata(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. up(&sn_hotplug_sem);
  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. pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
  387. down(&sn_hotplug_sem);
  388. *value = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
  389. up(&sn_hotplug_sem);
  390. return 0;
  391. }
  392. static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
  393. {
  394. kfree(bss_hotplug_slot->info);
  395. kfree(bss_hotplug_slot->name);
  396. kfree(bss_hotplug_slot->private);
  397. kfree(bss_hotplug_slot);
  398. }
  399. static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
  400. {
  401. int device;
  402. struct hotplug_slot *bss_hotplug_slot;
  403. int rc = 0;
  404. /*
  405. * Currently only four devices are supported,
  406. * in the future there maybe more -- up to 32.
  407. */
  408. for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
  409. if (sn_pci_slot_valid(pci_bus, device) != 1)
  410. continue;
  411. bss_hotplug_slot = kcalloc(1, sizeof(*bss_hotplug_slot),
  412. GFP_KERNEL);
  413. if (!bss_hotplug_slot) {
  414. rc = -ENOMEM;
  415. goto alloc_err;
  416. }
  417. bss_hotplug_slot->info =
  418. kcalloc(1, sizeof(struct hotplug_slot_info),
  419. GFP_KERNEL);
  420. if (!bss_hotplug_slot->info) {
  421. rc = -ENOMEM;
  422. goto alloc_err;
  423. }
  424. if (sn_hp_slot_private_alloc(bss_hotplug_slot,
  425. pci_bus, device)) {
  426. rc = -ENOMEM;
  427. goto alloc_err;
  428. }
  429. bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
  430. bss_hotplug_slot->release = &sn_release_slot;
  431. rc = pci_hp_register(bss_hotplug_slot);
  432. if (rc)
  433. goto register_err;
  434. rc = sysfs_create_file(&bss_hotplug_slot->kobj,
  435. &sn_slot_path_attr.attr);
  436. if (rc)
  437. goto register_err;
  438. }
  439. dev_dbg(pci_bus->self, "Registered bus with hotplug\n");
  440. return rc;
  441. register_err:
  442. dev_dbg(pci_bus->self, "bus failed to register with err = %d\n",
  443. rc);
  444. alloc_err:
  445. if (rc == -ENOMEM)
  446. dev_dbg(pci_bus->self, "Memory allocation error\n");
  447. /* destroy THIS element */
  448. if (bss_hotplug_slot)
  449. sn_release_slot(bss_hotplug_slot);
  450. /* destroy anything else on the list */
  451. while ((bss_hotplug_slot = sn_hp_destroy()))
  452. pci_hp_deregister(bss_hotplug_slot);
  453. return rc;
  454. }
  455. static int sn_pci_hotplug_init(void)
  456. {
  457. struct pci_bus *pci_bus = NULL;
  458. int rc;
  459. int registered = 0;
  460. if (sn_sal_rev() < SGI_HOTPLUG_PROM_REV) {
  461. printk(KERN_ERR "%s: PROM version must be greater than 4.30\n",
  462. __FUNCTION__);
  463. return -EPERM;
  464. }
  465. INIT_LIST_HEAD(&sn_hp_list);
  466. while ((pci_bus = pci_find_next_bus(pci_bus))) {
  467. if (!pci_bus->sysdata)
  468. continue;
  469. rc = sn_pci_bus_valid(pci_bus);
  470. if (rc != 1) {
  471. dev_dbg(pci_bus->self, "not a valid hotplug bus\n");
  472. continue;
  473. }
  474. dev_dbg(pci_bus->self, "valid hotplug bus\n");
  475. rc = sn_hotplug_slot_register(pci_bus);
  476. if (!rc) {
  477. registered = 1;
  478. } else {
  479. registered = 0;
  480. break;
  481. }
  482. }
  483. return registered == 1 ? 0 : -ENODEV;
  484. }
  485. static void sn_pci_hotplug_exit(void)
  486. {
  487. struct hotplug_slot *bss_hotplug_slot;
  488. while ((bss_hotplug_slot = sn_hp_destroy()))
  489. pci_hp_deregister(bss_hotplug_slot);
  490. if (!list_empty(&sn_hp_list))
  491. printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
  492. }
  493. module_init(sn_pci_hotplug_init);
  494. module_exit(sn_pci_hotplug_exit);