cpci_hotplug_pci.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * CompactPCI Hot Plug Driver PCI functions
  3. *
  4. * Copyright (C) 2002 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. }
  55. if(pci_bus_read_config_word(slot->bus,
  56. slot->devfn,
  57. hs_cap + 2,
  58. &hs_csr)) {
  59. return 0;
  60. }
  61. return hs_csr & 0x0008 ? 1 : 0;
  62. }
  63. int cpci_set_attention_status(struct slot* slot, int status)
  64. {
  65. int hs_cap;
  66. u16 hs_csr;
  67. hs_cap = pci_bus_find_capability(slot->bus,
  68. slot->devfn,
  69. PCI_CAP_ID_CHSWP);
  70. if(!hs_cap) {
  71. return 0;
  72. }
  73. if(pci_bus_read_config_word(slot->bus,
  74. slot->devfn,
  75. hs_cap + 2,
  76. &hs_csr)) {
  77. return 0;
  78. }
  79. if(status) {
  80. hs_csr |= HS_CSR_LOO;
  81. } else {
  82. hs_csr &= ~HS_CSR_LOO;
  83. }
  84. if(pci_bus_write_config_word(slot->bus,
  85. slot->devfn,
  86. hs_cap + 2,
  87. hs_csr)) {
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. u16 cpci_get_hs_csr(struct slot* slot)
  93. {
  94. int hs_cap;
  95. u16 hs_csr;
  96. hs_cap = pci_bus_find_capability(slot->bus,
  97. slot->devfn,
  98. PCI_CAP_ID_CHSWP);
  99. if(!hs_cap) {
  100. return 0xFFFF;
  101. }
  102. if(pci_bus_read_config_word(slot->bus,
  103. slot->devfn,
  104. hs_cap + 2,
  105. &hs_csr)) {
  106. return 0xFFFF;
  107. }
  108. return hs_csr;
  109. }
  110. int cpci_check_and_clear_ins(struct slot* slot)
  111. {
  112. int hs_cap;
  113. u16 hs_csr;
  114. int ins = 0;
  115. hs_cap = pci_bus_find_capability(slot->bus,
  116. slot->devfn,
  117. PCI_CAP_ID_CHSWP);
  118. if(!hs_cap) {
  119. return 0;
  120. }
  121. if(pci_bus_read_config_word(slot->bus,
  122. slot->devfn,
  123. hs_cap + 2,
  124. &hs_csr)) {
  125. return 0;
  126. }
  127. if(hs_csr & HS_CSR_INS) {
  128. /* Clear INS (by setting it) */
  129. if(pci_bus_write_config_word(slot->bus,
  130. slot->devfn,
  131. hs_cap + 2,
  132. hs_csr)) {
  133. ins = 0;
  134. }
  135. ins = 1;
  136. }
  137. return ins;
  138. }
  139. int cpci_check_ext(struct slot* slot)
  140. {
  141. int hs_cap;
  142. u16 hs_csr;
  143. int ext = 0;
  144. hs_cap = pci_bus_find_capability(slot->bus,
  145. slot->devfn,
  146. PCI_CAP_ID_CHSWP);
  147. if(!hs_cap) {
  148. return 0;
  149. }
  150. if(pci_bus_read_config_word(slot->bus,
  151. slot->devfn,
  152. hs_cap + 2,
  153. &hs_csr)) {
  154. return 0;
  155. }
  156. if(hs_csr & HS_CSR_EXT) {
  157. ext = 1;
  158. }
  159. return ext;
  160. }
  161. int cpci_clear_ext(struct slot* slot)
  162. {
  163. int hs_cap;
  164. u16 hs_csr;
  165. hs_cap = pci_bus_find_capability(slot->bus,
  166. slot->devfn,
  167. PCI_CAP_ID_CHSWP);
  168. if(!hs_cap) {
  169. return -ENODEV;
  170. }
  171. if(pci_bus_read_config_word(slot->bus,
  172. slot->devfn,
  173. hs_cap + 2,
  174. &hs_csr)) {
  175. return -ENODEV;
  176. }
  177. if(hs_csr & HS_CSR_EXT) {
  178. /* Clear EXT (by setting it) */
  179. if(pci_bus_write_config_word(slot->bus,
  180. slot->devfn,
  181. hs_cap + 2,
  182. hs_csr)) {
  183. return -ENODEV;
  184. }
  185. }
  186. return 0;
  187. }
  188. int cpci_led_on(struct slot* slot)
  189. {
  190. int hs_cap;
  191. u16 hs_csr;
  192. hs_cap = pci_bus_find_capability(slot->bus,
  193. slot->devfn,
  194. PCI_CAP_ID_CHSWP);
  195. if(!hs_cap) {
  196. return -ENODEV;
  197. }
  198. if(pci_bus_read_config_word(slot->bus,
  199. slot->devfn,
  200. hs_cap + 2,
  201. &hs_csr)) {
  202. return -ENODEV;
  203. }
  204. if((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
  205. hs_csr |= HS_CSR_LOO;
  206. if(pci_bus_write_config_word(slot->bus,
  207. slot->devfn,
  208. hs_cap + 2,
  209. hs_csr)) {
  210. err("Could not set LOO for slot %s",
  211. slot->hotplug_slot->name);
  212. return -ENODEV;
  213. }
  214. }
  215. return 0;
  216. }
  217. int cpci_led_off(struct slot* slot)
  218. {
  219. int hs_cap;
  220. u16 hs_csr;
  221. hs_cap = pci_bus_find_capability(slot->bus,
  222. slot->devfn,
  223. PCI_CAP_ID_CHSWP);
  224. if(!hs_cap) {
  225. return -ENODEV;
  226. }
  227. if(pci_bus_read_config_word(slot->bus,
  228. slot->devfn,
  229. hs_cap + 2,
  230. &hs_csr)) {
  231. return -ENODEV;
  232. }
  233. if(hs_csr & HS_CSR_LOO) {
  234. hs_csr &= ~HS_CSR_LOO;
  235. if(pci_bus_write_config_word(slot->bus,
  236. slot->devfn,
  237. hs_cap + 2,
  238. hs_csr)) {
  239. err("Could not clear LOO for slot %s",
  240. slot->hotplug_slot->name);
  241. return -ENODEV;
  242. }
  243. }
  244. return 0;
  245. }
  246. /*
  247. * Device configuration functions
  248. */
  249. static void cpci_enable_device(struct pci_dev *dev)
  250. {
  251. struct pci_bus *bus;
  252. pci_enable_device(dev);
  253. if(dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  254. bus = dev->subordinate;
  255. list_for_each_entry(dev, &bus->devices, bus_list) {
  256. cpci_enable_device(dev);
  257. }
  258. }
  259. }
  260. int cpci_configure_slot(struct slot* slot)
  261. {
  262. unsigned char busnr;
  263. struct pci_bus *child;
  264. dbg("%s - enter", __FUNCTION__);
  265. if(slot->dev == NULL) {
  266. dbg("pci_dev null, finding %02x:%02x:%x",
  267. slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
  268. slot->dev = pci_find_slot(slot->bus->number, slot->devfn);
  269. }
  270. /* Still NULL? Well then scan for it! */
  271. if(slot->dev == NULL) {
  272. int n;
  273. dbg("pci_dev still null");
  274. /*
  275. * This will generate pci_dev structures for all functions, but
  276. * we will only call this case when lookup fails.
  277. */
  278. n = pci_scan_slot(slot->bus, slot->devfn);
  279. dbg("%s: pci_scan_slot returned %d", __FUNCTION__, n);
  280. if(n > 0)
  281. pci_bus_add_devices(slot->bus);
  282. slot->dev = pci_find_slot(slot->bus->number, slot->devfn);
  283. if(slot->dev == NULL) {
  284. err("Could not find PCI device for slot %02x", slot->number);
  285. return 1;
  286. }
  287. }
  288. if (slot->dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  289. pci_read_config_byte(slot->dev, PCI_SECONDARY_BUS, &busnr);
  290. child = pci_add_new_bus(slot->dev->bus, slot->dev, busnr);
  291. pci_do_scan_bus(child);
  292. pci_bus_size_bridges(child);
  293. }
  294. pci_bus_assign_resources(slot->dev->bus);
  295. cpci_enable_device(slot->dev);
  296. dbg("%s - exit", __FUNCTION__);
  297. return 0;
  298. }
  299. int cpci_unconfigure_slot(struct slot* slot)
  300. {
  301. int i;
  302. struct pci_dev *dev;
  303. dbg("%s - enter", __FUNCTION__);
  304. if(!slot->dev) {
  305. err("No device for slot %02x\n", slot->number);
  306. return -ENODEV;
  307. }
  308. for (i = 0; i < 8; i++) {
  309. dev = pci_find_slot(slot->bus->number,
  310. PCI_DEVFN(PCI_SLOT(slot->devfn), i));
  311. if(dev) {
  312. pci_remove_bus_device(dev);
  313. slot->dev = NULL;
  314. }
  315. }
  316. dbg("%s - exit", __FUNCTION__);
  317. return 0;
  318. }