cpci_hotplug_pci.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/config.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/pci.h>
  29. #include <linux/proc_fs.h>
  30. #include "../pci.h"
  31. #include "pci_hotplug.h"
  32. #include "cpci_hotplug.h"
  33. #define MY_NAME "cpci_hotplug"
  34. extern int cpci_debug;
  35. #define dbg(format, arg...) \
  36. do { \
  37. if (cpci_debug) \
  38. printk (KERN_DEBUG "%s: " format "\n", \
  39. MY_NAME , ## arg); \
  40. } while (0)
  41. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
  42. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
  43. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
  44. #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
  45. u8 cpci_get_attention_status(struct slot* slot)
  46. {
  47. int hs_cap;
  48. u16 hs_csr;
  49. hs_cap = pci_bus_find_capability(slot->bus,
  50. slot->devfn,
  51. PCI_CAP_ID_CHSWP);
  52. if (!hs_cap)
  53. return 0;
  54. if (pci_bus_read_config_word(slot->bus,
  55. slot->devfn,
  56. hs_cap + 2,
  57. &hs_csr))
  58. return 0;
  59. return hs_csr & 0x0008 ? 1 : 0;
  60. }
  61. int cpci_set_attention_status(struct slot* slot, int status)
  62. {
  63. int hs_cap;
  64. u16 hs_csr;
  65. hs_cap = pci_bus_find_capability(slot->bus,
  66. slot->devfn,
  67. PCI_CAP_ID_CHSWP);
  68. if (!hs_cap)
  69. return 0;
  70. if (pci_bus_read_config_word(slot->bus,
  71. slot->devfn,
  72. hs_cap + 2,
  73. &hs_csr))
  74. return 0;
  75. if (status)
  76. hs_csr |= HS_CSR_LOO;
  77. else
  78. hs_csr &= ~HS_CSR_LOO;
  79. if (pci_bus_write_config_word(slot->bus,
  80. slot->devfn,
  81. hs_cap + 2,
  82. hs_csr))
  83. return 0;
  84. return 1;
  85. }
  86. u16 cpci_get_hs_csr(struct slot* slot)
  87. {
  88. int hs_cap;
  89. u16 hs_csr;
  90. hs_cap = pci_bus_find_capability(slot->bus,
  91. slot->devfn,
  92. PCI_CAP_ID_CHSWP);
  93. if (!hs_cap)
  94. return 0xFFFF;
  95. if (pci_bus_read_config_word(slot->bus,
  96. slot->devfn,
  97. hs_cap + 2,
  98. &hs_csr))
  99. return 0xFFFF;
  100. return hs_csr;
  101. }
  102. int cpci_check_and_clear_ins(struct slot* slot)
  103. {
  104. int hs_cap;
  105. u16 hs_csr;
  106. int ins = 0;
  107. hs_cap = pci_bus_find_capability(slot->bus,
  108. slot->devfn,
  109. PCI_CAP_ID_CHSWP);
  110. if (!hs_cap)
  111. return 0;
  112. if (pci_bus_read_config_word(slot->bus,
  113. slot->devfn,
  114. hs_cap + 2,
  115. &hs_csr))
  116. return 0;
  117. if (hs_csr & HS_CSR_INS) {
  118. /* Clear INS (by setting it) */
  119. if (pci_bus_write_config_word(slot->bus,
  120. slot->devfn,
  121. hs_cap + 2,
  122. hs_csr))
  123. ins = 0;
  124. else
  125. ins = 1;
  126. }
  127. return ins;
  128. }
  129. int cpci_check_ext(struct slot* slot)
  130. {
  131. int hs_cap;
  132. u16 hs_csr;
  133. int ext = 0;
  134. hs_cap = pci_bus_find_capability(slot->bus,
  135. slot->devfn,
  136. PCI_CAP_ID_CHSWP);
  137. if (!hs_cap)
  138. return 0;
  139. if (pci_bus_read_config_word(slot->bus,
  140. slot->devfn,
  141. hs_cap + 2,
  142. &hs_csr))
  143. return 0;
  144. if (hs_csr & HS_CSR_EXT)
  145. ext = 1;
  146. return ext;
  147. }
  148. int cpci_clear_ext(struct slot* slot)
  149. {
  150. int hs_cap;
  151. u16 hs_csr;
  152. hs_cap = pci_bus_find_capability(slot->bus,
  153. slot->devfn,
  154. PCI_CAP_ID_CHSWP);
  155. if (!hs_cap)
  156. return -ENODEV;
  157. if (pci_bus_read_config_word(slot->bus,
  158. slot->devfn,
  159. hs_cap + 2,
  160. &hs_csr))
  161. return -ENODEV;
  162. if (hs_csr & HS_CSR_EXT) {
  163. /* Clear EXT (by setting it) */
  164. if (pci_bus_write_config_word(slot->bus,
  165. slot->devfn,
  166. hs_cap + 2,
  167. hs_csr))
  168. return -ENODEV;
  169. }
  170. return 0;
  171. }
  172. int cpci_led_on(struct slot* slot)
  173. {
  174. int hs_cap;
  175. u16 hs_csr;
  176. hs_cap = pci_bus_find_capability(slot->bus,
  177. slot->devfn,
  178. PCI_CAP_ID_CHSWP);
  179. if (!hs_cap)
  180. return -ENODEV;
  181. if (pci_bus_read_config_word(slot->bus,
  182. slot->devfn,
  183. hs_cap + 2,
  184. &hs_csr))
  185. return -ENODEV;
  186. if ((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
  187. hs_csr |= HS_CSR_LOO;
  188. if (pci_bus_write_config_word(slot->bus,
  189. slot->devfn,
  190. hs_cap + 2,
  191. hs_csr)) {
  192. err("Could not set LOO for slot %s",
  193. slot->hotplug_slot->name);
  194. return -ENODEV;
  195. }
  196. }
  197. return 0;
  198. }
  199. int cpci_led_off(struct slot* slot)
  200. {
  201. int hs_cap;
  202. u16 hs_csr;
  203. hs_cap = pci_bus_find_capability(slot->bus,
  204. slot->devfn,
  205. PCI_CAP_ID_CHSWP);
  206. if (!hs_cap)
  207. return -ENODEV;
  208. if (pci_bus_read_config_word(slot->bus,
  209. slot->devfn,
  210. hs_cap + 2,
  211. &hs_csr))
  212. return -ENODEV;
  213. if (hs_csr & HS_CSR_LOO) {
  214. hs_csr &= ~HS_CSR_LOO;
  215. if (pci_bus_write_config_word(slot->bus,
  216. slot->devfn,
  217. hs_cap + 2,
  218. hs_csr)) {
  219. err("Could not clear LOO for slot %s",
  220. slot->hotplug_slot->name);
  221. return -ENODEV;
  222. }
  223. }
  224. return 0;
  225. }
  226. /*
  227. * Device configuration functions
  228. */
  229. int cpci_configure_slot(struct slot* slot)
  230. {
  231. unsigned char busnr;
  232. struct pci_bus *child;
  233. dbg("%s - enter", __FUNCTION__);
  234. if (slot->dev == NULL) {
  235. dbg("pci_dev null, finding %02x:%02x:%x",
  236. slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
  237. slot->dev = pci_get_slot(slot->bus, slot->devfn);
  238. }
  239. /* Still NULL? Well then scan for it! */
  240. if (slot->dev == NULL) {
  241. int n;
  242. dbg("pci_dev still null");
  243. /*
  244. * This will generate pci_dev structures for all functions, but
  245. * we will only call this case when lookup fails.
  246. */
  247. n = pci_scan_slot(slot->bus, slot->devfn);
  248. dbg("%s: pci_scan_slot returned %d", __FUNCTION__, n);
  249. if (n > 0)
  250. pci_bus_add_devices(slot->bus);
  251. slot->dev = pci_get_slot(slot->bus, slot->devfn);
  252. if (slot->dev == NULL) {
  253. err("Could not find PCI device for slot %02x", slot->number);
  254. return 1;
  255. }
  256. }
  257. if (slot->dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  258. pci_read_config_byte(slot->dev, PCI_SECONDARY_BUS, &busnr);
  259. child = pci_add_new_bus(slot->dev->bus, slot->dev, busnr);
  260. pci_do_scan_bus(child);
  261. pci_bus_size_bridges(child);
  262. }
  263. pci_bus_assign_resources(slot->dev->bus);
  264. dbg("%s - exit", __FUNCTION__);
  265. return 0;
  266. }
  267. int cpci_unconfigure_slot(struct slot* slot)
  268. {
  269. int i;
  270. struct pci_dev *dev;
  271. dbg("%s - enter", __FUNCTION__);
  272. if (!slot->dev) {
  273. err("No device for slot %02x\n", slot->number);
  274. return -ENODEV;
  275. }
  276. for (i = 0; i < 8; i++) {
  277. dev = pci_get_slot(slot->bus,
  278. PCI_DEVFN(PCI_SLOT(slot->devfn), i));
  279. if (dev) {
  280. pci_remove_bus_device(dev);
  281. pci_dev_put(dev);
  282. }
  283. }
  284. pci_dev_put(slot->dev);
  285. slot->dev = NULL;
  286. dbg("%s - exit", __FUNCTION__);
  287. return 0;
  288. }