sgi_hotplug.c 15 KB

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