cpci_hotplug_pci.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * CompactPCI Hot Plug Driver PCI functions
  3. *
  4. * Copyright (C) 2002,2005 by SOMA Networks, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send feedback to <scottm@somanetworks.com>
  24. */
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/pci.h>
  28. #include <linux/pci_hotplug.h>
  29. #include <linux/proc_fs.h>
  30. #include "../pci.h"
  31. #include "cpci_hotplug.h"
  32. #define MY_NAME "cpci_hotplug"
  33. extern int cpci_debug;
  34. #define dbg(format, arg...) \
  35. do { \
  36. if (cpci_debug) \
  37. printk (KERN_DEBUG "%s: " format "\n", \
  38. MY_NAME , ## arg); \
  39. } while (0)
  40. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
  41. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
  42. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
  43. #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
  44. u8 cpci_get_attention_status(struct slot* slot)
  45. {
  46. int hs_cap;
  47. u16 hs_csr;
  48. hs_cap = pci_bus_find_capability(slot->bus,
  49. slot->devfn,
  50. PCI_CAP_ID_CHSWP);
  51. if (!hs_cap)
  52. return 0;
  53. if (pci_bus_read_config_word(slot->bus,
  54. slot->devfn,
  55. hs_cap + 2,
  56. &hs_csr))
  57. return 0;
  58. return hs_csr & 0x0008 ? 1 : 0;
  59. }
  60. int cpci_set_attention_status(struct slot* slot, int status)
  61. {
  62. int hs_cap;
  63. u16 hs_csr;
  64. hs_cap = pci_bus_find_capability(slot->bus,
  65. slot->devfn,
  66. PCI_CAP_ID_CHSWP);
  67. if (!hs_cap)
  68. return 0;
  69. if (pci_bus_read_config_word(slot->bus,
  70. slot->devfn,
  71. hs_cap + 2,
  72. &hs_csr))
  73. return 0;
  74. if (status)
  75. hs_csr |= HS_CSR_LOO;
  76. else
  77. hs_csr &= ~HS_CSR_LOO;
  78. if (pci_bus_write_config_word(slot->bus,
  79. slot->devfn,
  80. hs_cap + 2,
  81. hs_csr))
  82. return 0;
  83. return 1;
  84. }
  85. u16 cpci_get_hs_csr(struct slot* slot)
  86. {
  87. int hs_cap;
  88. u16 hs_csr;
  89. hs_cap = pci_bus_find_capability(slot->bus,
  90. slot->devfn,
  91. PCI_CAP_ID_CHSWP);
  92. if (!hs_cap)
  93. return 0xFFFF;
  94. if (pci_bus_read_config_word(slot->bus,
  95. slot->devfn,
  96. hs_cap + 2,
  97. &hs_csr))
  98. return 0xFFFF;
  99. return hs_csr;
  100. }
  101. int cpci_check_and_clear_ins(struct slot* slot)
  102. {
  103. int hs_cap;
  104. u16 hs_csr;
  105. int ins = 0;
  106. hs_cap = pci_bus_find_capability(slot->bus,
  107. slot->devfn,
  108. PCI_CAP_ID_CHSWP);
  109. if (!hs_cap)
  110. return 0;
  111. if (pci_bus_read_config_word(slot->bus,
  112. slot->devfn,
  113. hs_cap + 2,
  114. &hs_csr))
  115. return 0;
  116. if (hs_csr & HS_CSR_INS) {
  117. /* Clear INS (by setting it) */
  118. if (pci_bus_write_config_word(slot->bus,
  119. slot->devfn,
  120. hs_cap + 2,
  121. hs_csr))
  122. ins = 0;
  123. else
  124. ins = 1;
  125. }
  126. return ins;
  127. }
  128. int cpci_check_ext(struct slot* slot)
  129. {
  130. int hs_cap;
  131. u16 hs_csr;
  132. int ext = 0;
  133. hs_cap = pci_bus_find_capability(slot->bus,
  134. slot->devfn,
  135. PCI_CAP_ID_CHSWP);
  136. if (!hs_cap)
  137. return 0;
  138. if (pci_bus_read_config_word(slot->bus,
  139. slot->devfn,
  140. hs_cap + 2,
  141. &hs_csr))
  142. return 0;
  143. if (hs_csr & HS_CSR_EXT)
  144. ext = 1;
  145. return ext;
  146. }
  147. int cpci_clear_ext(struct slot* slot)
  148. {
  149. int hs_cap;
  150. u16 hs_csr;
  151. hs_cap = pci_bus_find_capability(slot->bus,
  152. slot->devfn,
  153. PCI_CAP_ID_CHSWP);
  154. if (!hs_cap)
  155. return -ENODEV;
  156. if (pci_bus_read_config_word(slot->bus,
  157. slot->devfn,
  158. hs_cap + 2,
  159. &hs_csr))
  160. return -ENODEV;
  161. if (hs_csr & HS_CSR_EXT) {
  162. /* Clear EXT (by setting it) */
  163. if (pci_bus_write_config_word(slot->bus,
  164. slot->devfn,
  165. hs_cap + 2,
  166. hs_csr))
  167. return -ENODEV;
  168. }
  169. return 0;
  170. }
  171. int cpci_led_on(struct slot* slot)
  172. {
  173. int hs_cap;
  174. u16 hs_csr;
  175. hs_cap = pci_bus_find_capability(slot->bus,
  176. slot->devfn,
  177. PCI_CAP_ID_CHSWP);
  178. if (!hs_cap)
  179. return -ENODEV;
  180. if (pci_bus_read_config_word(slot->bus,
  181. slot->devfn,
  182. hs_cap + 2,
  183. &hs_csr))
  184. return -ENODEV;
  185. if ((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
  186. hs_csr |= HS_CSR_LOO;
  187. if (pci_bus_write_config_word(slot->bus,
  188. slot->devfn,
  189. hs_cap + 2,
  190. hs_csr)) {
  191. err("Could not set LOO for slot %s",
  192. slot->hotplug_slot->name);
  193. return -ENODEV;
  194. }
  195. }
  196. return 0;
  197. }
  198. int cpci_led_off(struct slot* slot)
  199. {
  200. int hs_cap;
  201. u16 hs_csr;
  202. hs_cap = pci_bus_find_capability(slot->bus,
  203. slot->devfn,
  204. PCI_CAP_ID_CHSWP);
  205. if (!hs_cap)
  206. return -ENODEV;
  207. if (pci_bus_read_config_word(slot->bus,
  208. slot->devfn,
  209. hs_cap + 2,
  210. &hs_csr))
  211. return -ENODEV;
  212. if (hs_csr & HS_CSR_LOO) {
  213. hs_csr &= ~HS_CSR_LOO;
  214. if (pci_bus_write_config_word(slot->bus,
  215. slot->devfn,
  216. hs_cap + 2,
  217. hs_csr)) {
  218. err("Could not clear LOO for slot %s",
  219. slot->hotplug_slot->name);
  220. return -ENODEV;
  221. }
  222. }
  223. return 0;
  224. }
  225. /*
  226. * Device configuration functions
  227. */
  228. int cpci_configure_slot(struct slot* slot)
  229. {
  230. struct pci_bus *parent;
  231. int fn;
  232. dbg("%s - enter", __FUNCTION__);
  233. if (slot->dev == NULL) {
  234. dbg("pci_dev null, finding %02x:%02x:%x",
  235. slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
  236. slot->dev = pci_get_slot(slot->bus, slot->devfn);
  237. }
  238. /* Still NULL? Well then scan for it! */
  239. if (slot->dev == NULL) {
  240. int n;
  241. dbg("pci_dev still null");
  242. /*
  243. * This will generate pci_dev structures for all functions, but
  244. * we will only call this case when lookup fails.
  245. */
  246. n = pci_scan_slot(slot->bus, slot->devfn);
  247. dbg("%s: pci_scan_slot returned %d", __FUNCTION__, n);
  248. slot->dev = pci_get_slot(slot->bus, slot->devfn);
  249. if (slot->dev == NULL) {
  250. err("Could not find PCI device for slot %02x", slot->number);
  251. return -ENODEV;
  252. }
  253. }
  254. parent = slot->dev->bus;
  255. for (fn = 0; fn < 8; fn++) {
  256. struct pci_dev *dev;
  257. dev = pci_get_slot(parent, PCI_DEVFN(PCI_SLOT(slot->devfn), fn));
  258. if (!dev)
  259. continue;
  260. if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) ||
  261. (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)) {
  262. /* Find an unused bus number for the new bridge */
  263. struct pci_bus *child;
  264. unsigned char busnr, start = parent->secondary;
  265. unsigned char end = parent->subordinate;
  266. for (busnr = start; busnr <= end; busnr++) {
  267. if (!pci_find_bus(pci_domain_nr(parent),
  268. busnr))
  269. break;
  270. }
  271. if (busnr >= end) {
  272. err("No free bus for hot-added bridge\n");
  273. pci_dev_put(dev);
  274. continue;
  275. }
  276. child = pci_add_new_bus(parent, dev, busnr);
  277. if (!child) {
  278. err("Cannot add new bus for %s\n",
  279. pci_name(dev));
  280. pci_dev_put(dev);
  281. continue;
  282. }
  283. child->subordinate = pci_do_scan_bus(child);
  284. pci_bus_size_bridges(child);
  285. }
  286. pci_dev_put(dev);
  287. }
  288. pci_bus_assign_resources(parent);
  289. pci_bus_add_devices(parent);
  290. pci_enable_bridges(parent);
  291. dbg("%s - exit", __FUNCTION__);
  292. return 0;
  293. }
  294. int cpci_unconfigure_slot(struct slot* slot)
  295. {
  296. int i;
  297. struct pci_dev *dev;
  298. dbg("%s - enter", __FUNCTION__);
  299. if (!slot->dev) {
  300. err("No device for slot %02x\n", slot->number);
  301. return -ENODEV;
  302. }
  303. for (i = 0; i < 8; i++) {
  304. dev = pci_get_slot(slot->bus,
  305. PCI_DEVFN(PCI_SLOT(slot->devfn), i));
  306. if (dev) {
  307. pci_remove_bus_device(dev);
  308. pci_dev_put(dev);
  309. }
  310. }
  311. pci_dev_put(slot->dev);
  312. slot->dev = NULL;
  313. dbg("%s - exit", __FUNCTION__);
  314. return 0;
  315. }