cpcihp_generic.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * cpcihp_generic.c
  3. *
  4. * Generic port I/O CompactPCI driver
  5. *
  6. * Copyright 2002 SOMA Networks, Inc.
  7. * Copyright 2001 Intel San Luis Obispo
  8. * Copyright 2000,2001 MontaVista Software Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  18. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. * This generic CompactPCI hotplug driver should allow using the PCI hotplug
  31. * mechanism on any CompactPCI board that exposes the #ENUM signal as a bit
  32. * in a system register that can be read through standard port I/O.
  33. *
  34. * Send feedback to <scottm@somanetworks.com>
  35. */
  36. #include <linux/config.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/errno.h>
  40. #include <linux/pci.h>
  41. #include <linux/string.h>
  42. #include "cpci_hotplug.h"
  43. #define DRIVER_VERSION "0.1"
  44. #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
  45. #define DRIVER_DESC "Generic port I/O CompactPCI Hot Plug Driver"
  46. #if !defined(MODULE)
  47. #define MY_NAME "cpcihp_generic"
  48. #else
  49. #define MY_NAME THIS_MODULE->name
  50. #endif
  51. #define dbg(format, arg...) \
  52. do { \
  53. if(debug) \
  54. printk (KERN_DEBUG "%s: " format "\n", \
  55. MY_NAME , ## arg); \
  56. } while(0)
  57. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
  58. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
  59. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
  60. /* local variables */
  61. static int debug;
  62. static char *bridge;
  63. static u8 bridge_busnr;
  64. static u8 bridge_slot;
  65. static struct pci_bus *bus;
  66. static u8 first_slot;
  67. static u8 last_slot;
  68. static u16 port;
  69. static unsigned int enum_bit;
  70. static u8 enum_mask;
  71. static struct cpci_hp_controller_ops generic_hpc_ops;
  72. static struct cpci_hp_controller generic_hpc;
  73. static int __init validate_parameters(void)
  74. {
  75. char* str;
  76. char* p;
  77. unsigned long tmp;
  78. if(!bridge) {
  79. info("not configured, disabling.");
  80. return 1;
  81. }
  82. str = bridge;
  83. if(!*str)
  84. return -EINVAL;
  85. tmp = simple_strtoul(str, &p, 16);
  86. if(p == str || tmp > 0xff) {
  87. err("Invalid hotplug bus bridge device bus number");
  88. return -EINVAL;
  89. }
  90. bridge_busnr = (u8) tmp;
  91. dbg("bridge_busnr = 0x%02x", bridge_busnr);
  92. if(*p != ':') {
  93. err("Invalid hotplug bus bridge device");
  94. return -EINVAL;
  95. }
  96. str = p + 1;
  97. tmp = simple_strtoul(str, &p, 16);
  98. if(p == str || tmp > 0x1f) {
  99. err("Invalid hotplug bus bridge device slot number");
  100. return -EINVAL;
  101. }
  102. bridge_slot = (u8) tmp;
  103. dbg("bridge_slot = 0x%02x", bridge_slot);
  104. dbg("first_slot = 0x%02x", first_slot);
  105. dbg("last_slot = 0x%02x", last_slot);
  106. if(!(first_slot && last_slot)) {
  107. err("Need to specify first_slot and last_slot");
  108. return -EINVAL;
  109. }
  110. if(last_slot < first_slot) {
  111. err("first_slot must be less than last_slot");
  112. return -EINVAL;
  113. }
  114. dbg("port = 0x%04x", port);
  115. dbg("enum_bit = 0x%02x", enum_bit);
  116. if(enum_bit > 7) {
  117. err("Invalid #ENUM bit");
  118. return -EINVAL;
  119. }
  120. enum_mask = 1 << enum_bit;
  121. return 0;
  122. }
  123. static int query_enum(void)
  124. {
  125. u8 value;
  126. value = inb_p(port);
  127. return ((value & enum_mask) == enum_mask);
  128. }
  129. static int __init cpcihp_generic_init(void)
  130. {
  131. int status;
  132. struct resource* r;
  133. struct pci_dev* dev;
  134. info(DRIVER_DESC " version: " DRIVER_VERSION);
  135. status = validate_parameters();
  136. if(status != 0)
  137. return status;
  138. r = request_region(port, 1, "#ENUM hotswap signal register");
  139. if(!r)
  140. return -EBUSY;
  141. dev = pci_find_slot(bridge_busnr, PCI_DEVFN(bridge_slot, 0));
  142. if(!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  143. err("Invalid bridge device %s", bridge);
  144. return -EINVAL;
  145. }
  146. bus = dev->subordinate;
  147. memset(&generic_hpc, 0, sizeof (struct cpci_hp_controller));
  148. generic_hpc_ops.query_enum = query_enum;
  149. generic_hpc.ops = &generic_hpc_ops;
  150. status = cpci_hp_register_controller(&generic_hpc);
  151. if(status != 0) {
  152. err("Could not register cPCI hotplug controller");
  153. return -ENODEV;
  154. }
  155. dbg("registered controller");
  156. status = cpci_hp_register_bus(bus, first_slot, last_slot);
  157. if(status != 0) {
  158. err("Could not register cPCI hotplug bus");
  159. goto init_bus_register_error;
  160. }
  161. dbg("registered bus");
  162. status = cpci_hp_start();
  163. if(status != 0) {
  164. err("Could not started cPCI hotplug system");
  165. goto init_start_error;
  166. }
  167. dbg("started cpci hp system");
  168. return 0;
  169. init_start_error:
  170. cpci_hp_unregister_bus(bus);
  171. init_bus_register_error:
  172. cpci_hp_unregister_controller(&generic_hpc);
  173. err("status = %d", status);
  174. return status;
  175. }
  176. static void __exit cpcihp_generic_exit(void)
  177. {
  178. cpci_hp_stop();
  179. cpci_hp_unregister_bus(bus);
  180. cpci_hp_unregister_controller(&generic_hpc);
  181. release_region(port, 1);
  182. }
  183. module_init(cpcihp_generic_init);
  184. module_exit(cpcihp_generic_exit);
  185. MODULE_AUTHOR(DRIVER_AUTHOR);
  186. MODULE_DESCRIPTION(DRIVER_DESC);
  187. MODULE_LICENSE("GPL");
  188. module_param(debug, bool, S_IRUGO | S_IWUSR);
  189. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  190. module_param(bridge, charp, 0);
  191. MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)");
  192. module_param(first_slot, byte, 0);
  193. MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number");
  194. module_param(last_slot, byte, 0);
  195. MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number");
  196. module_param(port, ushort, 0);
  197. MODULE_PARM_DESC(port, "#ENUM signal I/O port");
  198. module_param(enum_bit, uint, 0);
  199. MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)");