driver.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Functions to handle I2O drivers (OSMs) and I2O bus type for sysfs
  3. *
  4. * Copyright (C) 2004 Markus Lidel <Markus.Lidel@shadowconnect.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Fixes/additions:
  12. * Markus Lidel <Markus.Lidel@shadowconnect.com>
  13. * initial version.
  14. */
  15. #include <linux/device.h>
  16. #include <linux/module.h>
  17. #include <linux/rwsem.h>
  18. #include <linux/i2o.h>
  19. #define OSM_NAME "i2o"
  20. /* max_drivers - Maximum I2O drivers (OSMs) which could be registered */
  21. unsigned int i2o_max_drivers = I2O_MAX_DRIVERS;
  22. module_param_named(max_drivers, i2o_max_drivers, uint, 0);
  23. MODULE_PARM_DESC(max_drivers, "maximum number of OSM's to support");
  24. /* I2O drivers lock and array */
  25. static spinlock_t i2o_drivers_lock;
  26. static struct i2o_driver **i2o_drivers;
  27. /**
  28. * i2o_bus_match - Tell if a I2O device class id match the class ids of
  29. * the I2O driver (OSM)
  30. *
  31. * @dev: device which should be verified
  32. * @drv: the driver to match against
  33. *
  34. * Used by the bus to check if the driver wants to handle the device.
  35. *
  36. * Returns 1 if the class ids of the driver match the class id of the
  37. * device, otherwise 0.
  38. */
  39. static int i2o_bus_match(struct device *dev, struct device_driver *drv)
  40. {
  41. struct i2o_device *i2o_dev = to_i2o_device(dev);
  42. struct i2o_driver *i2o_drv = to_i2o_driver(drv);
  43. struct i2o_class_id *ids = i2o_drv->classes;
  44. if (ids)
  45. while (ids->class_id != I2O_CLASS_END) {
  46. if (ids->class_id == i2o_dev->lct_data.class_id)
  47. return 1;
  48. ids++;
  49. }
  50. return 0;
  51. };
  52. /* I2O bus type */
  53. struct bus_type i2o_bus_type = {
  54. .name = "i2o",
  55. .match = i2o_bus_match,
  56. };
  57. /**
  58. * i2o_driver_register - Register a I2O driver (OSM) in the I2O core
  59. * @drv: I2O driver which should be registered
  60. *
  61. * Registers the OSM drv in the I2O core and creates an event queues if
  62. * necessary.
  63. *
  64. * Returns 0 on success or negative error code on failure.
  65. */
  66. int i2o_driver_register(struct i2o_driver *drv)
  67. {
  68. struct i2o_controller *c;
  69. int i;
  70. int rc = 0;
  71. unsigned long flags;
  72. osm_debug("Register driver %s\n", drv->name);
  73. if (drv->event) {
  74. drv->event_queue = create_workqueue(drv->name);
  75. if (!drv->event_queue) {
  76. osm_err("Could not initialize event queue for driver "
  77. "%s\n", drv->name);
  78. return -EFAULT;
  79. }
  80. osm_debug("Event queue initialized for driver %s\n", drv->name);
  81. } else
  82. drv->event_queue = NULL;
  83. drv->driver.name = drv->name;
  84. drv->driver.bus = &i2o_bus_type;
  85. spin_lock_irqsave(&i2o_drivers_lock, flags);
  86. for (i = 0; i2o_drivers[i]; i++)
  87. if (i >= i2o_max_drivers) {
  88. osm_err("too many drivers registered, increase "
  89. "max_drivers\n");
  90. spin_unlock_irqrestore(&i2o_drivers_lock, flags);
  91. return -EFAULT;
  92. }
  93. drv->context = i;
  94. i2o_drivers[i] = drv;
  95. spin_unlock_irqrestore(&i2o_drivers_lock, flags);
  96. osm_debug("driver %s gets context id %d\n", drv->name, drv->context);
  97. list_for_each_entry(c, &i2o_controllers, list) {
  98. struct i2o_device *i2o_dev;
  99. i2o_driver_notify_controller_add(drv, c);
  100. list_for_each_entry(i2o_dev, &c->devices, list)
  101. i2o_driver_notify_device_add(drv, i2o_dev);
  102. }
  103. rc = driver_register(&drv->driver);
  104. if (rc)
  105. destroy_workqueue(drv->event_queue);
  106. return rc;
  107. };
  108. /**
  109. * i2o_driver_unregister - Unregister a I2O driver (OSM) from the I2O core
  110. * @drv: I2O driver which should be unregistered
  111. *
  112. * Unregisters the OSM drv from the I2O core and cleanup event queues if
  113. * necessary.
  114. */
  115. void i2o_driver_unregister(struct i2o_driver *drv)
  116. {
  117. struct i2o_controller *c;
  118. unsigned long flags;
  119. osm_debug("unregister driver %s\n", drv->name);
  120. driver_unregister(&drv->driver);
  121. list_for_each_entry(c, &i2o_controllers, list) {
  122. struct i2o_device *i2o_dev;
  123. list_for_each_entry(i2o_dev, &c->devices, list)
  124. i2o_driver_notify_device_remove(drv, i2o_dev);
  125. i2o_driver_notify_controller_remove(drv, c);
  126. }
  127. spin_lock_irqsave(&i2o_drivers_lock, flags);
  128. i2o_drivers[drv->context] = NULL;
  129. spin_unlock_irqrestore(&i2o_drivers_lock, flags);
  130. if (drv->event_queue) {
  131. destroy_workqueue(drv->event_queue);
  132. drv->event_queue = NULL;
  133. osm_debug("event queue removed for %s\n", drv->name);
  134. }
  135. };
  136. /**
  137. * i2o_driver_dispatch - dispatch an I2O reply message
  138. * @c: I2O controller of the message
  139. * @m: I2O message number
  140. * @msg: I2O message to be delivered
  141. *
  142. * The reply is delivered to the driver from which the original message
  143. * was. This function is only called from interrupt context.
  144. *
  145. * Returns 0 on success and the message should not be flushed. Returns > 0
  146. * on success and if the message should be flushed afterwords. Returns
  147. * negative error code on failure (the message will be flushed too).
  148. */
  149. int i2o_driver_dispatch(struct i2o_controller *c, u32 m)
  150. {
  151. struct i2o_driver *drv;
  152. struct i2o_message __iomem *msg = i2o_msg_out_to_virt(c, m);
  153. u32 context;
  154. unsigned long flags;
  155. if(unlikely(!msg))
  156. return -EIO;
  157. context = readl(&msg->u.s.icntxt);
  158. if (unlikely(context >= i2o_max_drivers)) {
  159. osm_warn("%s: Spurious reply to unknown driver %d\n", c->name,
  160. context);
  161. return -EIO;
  162. }
  163. spin_lock_irqsave(&i2o_drivers_lock, flags);
  164. drv = i2o_drivers[context];
  165. spin_unlock_irqrestore(&i2o_drivers_lock, flags);
  166. if (unlikely(!drv)) {
  167. osm_warn("%s: Spurious reply to unknown driver %d\n", c->name,
  168. context);
  169. return -EIO;
  170. }
  171. if ((readl(&msg->u.head[1]) >> 24) == I2O_CMD_UTIL_EVT_REGISTER) {
  172. struct i2o_device *dev, *tmp;
  173. struct i2o_event *evt;
  174. u16 size;
  175. u16 tid = readl(&msg->u.head[1]) & 0xfff;
  176. osm_debug("event received from device %d\n", tid);
  177. if (!drv->event)
  178. return -EIO;
  179. /* cut of header from message size (in 32-bit words) */
  180. size = (readl(&msg->u.head[0]) >> 16) - 5;
  181. evt = kmalloc(size * 4 + sizeof(*evt), GFP_ATOMIC | __GFP_ZERO);
  182. if (!evt)
  183. return -ENOMEM;
  184. evt->size = size;
  185. evt->tcntxt = readl(&msg->u.s.tcntxt);
  186. evt->event_indicator = readl(&msg->body[0]);
  187. memcpy_fromio(&evt->tcntxt, &msg->u.s.tcntxt, size * 4);
  188. list_for_each_entry_safe(dev, tmp, &c->devices, list)
  189. if (dev->lct_data.tid == tid) {
  190. evt->i2o_dev = dev;
  191. break;
  192. }
  193. INIT_WORK(&evt->work, (void (*)(void *))drv->event, evt);
  194. queue_work(drv->event_queue, &evt->work);
  195. return 1;
  196. }
  197. if (unlikely(!drv->reply)) {
  198. osm_debug("%s: Reply to driver %s, but no reply function"
  199. " defined!\n", c->name, drv->name);
  200. return -EIO;
  201. }
  202. return drv->reply(c, m, msg);
  203. }
  204. /**
  205. * i2o_driver_notify_controller_add_all - Send notify of added controller
  206. * to all I2O drivers
  207. *
  208. * Send notifications to all registered drivers that a new controller was
  209. * added.
  210. */
  211. void i2o_driver_notify_controller_add_all(struct i2o_controller *c)
  212. {
  213. int i;
  214. struct i2o_driver *drv;
  215. for (i = 0; i < I2O_MAX_DRIVERS; i++) {
  216. drv = i2o_drivers[i];
  217. if (drv)
  218. i2o_driver_notify_controller_add(drv, c);
  219. }
  220. }
  221. /**
  222. * i2o_driver_notify_controller_remove_all - Send notify of removed
  223. * controller to all I2O drivers
  224. *
  225. * Send notifications to all registered drivers that a controller was
  226. * removed.
  227. */
  228. void i2o_driver_notify_controller_remove_all(struct i2o_controller *c)
  229. {
  230. int i;
  231. struct i2o_driver *drv;
  232. for (i = 0; i < I2O_MAX_DRIVERS; i++) {
  233. drv = i2o_drivers[i];
  234. if (drv)
  235. i2o_driver_notify_controller_remove(drv, c);
  236. }
  237. }
  238. /**
  239. * i2o_driver_notify_device_add_all - Send notify of added device to all
  240. * I2O drivers
  241. *
  242. * Send notifications to all registered drivers that a device was added.
  243. */
  244. void i2o_driver_notify_device_add_all(struct i2o_device *i2o_dev)
  245. {
  246. int i;
  247. struct i2o_driver *drv;
  248. for (i = 0; i < I2O_MAX_DRIVERS; i++) {
  249. drv = i2o_drivers[i];
  250. if (drv)
  251. i2o_driver_notify_device_add(drv, i2o_dev);
  252. }
  253. }
  254. /**
  255. * i2o_driver_notify_device_remove_all - Send notify of removed device to
  256. * all I2O drivers
  257. *
  258. * Send notifications to all registered drivers that a device was removed.
  259. */
  260. void i2o_driver_notify_device_remove_all(struct i2o_device *i2o_dev)
  261. {
  262. int i;
  263. struct i2o_driver *drv;
  264. for (i = 0; i < I2O_MAX_DRIVERS; i++) {
  265. drv = i2o_drivers[i];
  266. if (drv)
  267. i2o_driver_notify_device_remove(drv, i2o_dev);
  268. }
  269. }
  270. /**
  271. * i2o_driver_init - initialize I2O drivers (OSMs)
  272. *
  273. * Registers the I2O bus and allocate memory for the array of OSMs.
  274. *
  275. * Returns 0 on success or negative error code on failure.
  276. */
  277. int __init i2o_driver_init(void)
  278. {
  279. int rc = 0;
  280. spin_lock_init(&i2o_drivers_lock);
  281. if ((i2o_max_drivers < 2) || (i2o_max_drivers > 64) ||
  282. ((i2o_max_drivers ^ (i2o_max_drivers - 1)) !=
  283. (2 * i2o_max_drivers - 1))) {
  284. osm_warn("max_drivers set to %d, but must be >=2 and <= 64 and "
  285. "a power of 2\n", i2o_max_drivers);
  286. i2o_max_drivers = I2O_MAX_DRIVERS;
  287. }
  288. osm_info("max drivers = %d\n", i2o_max_drivers);
  289. i2o_drivers =
  290. kmalloc(i2o_max_drivers * sizeof(*i2o_drivers), GFP_KERNEL);
  291. if (!i2o_drivers)
  292. return -ENOMEM;
  293. memset(i2o_drivers, 0, i2o_max_drivers * sizeof(*i2o_drivers));
  294. rc = bus_register(&i2o_bus_type);
  295. if (rc < 0)
  296. kfree(i2o_drivers);
  297. return rc;
  298. };
  299. /**
  300. * i2o_driver_exit - clean up I2O drivers (OSMs)
  301. *
  302. * Unregisters the I2O bus and free driver array.
  303. */
  304. void __exit i2o_driver_exit(void)
  305. {
  306. bus_unregister(&i2o_bus_type);
  307. kfree(i2o_drivers);
  308. };
  309. EXPORT_SYMBOL(i2o_driver_register);
  310. EXPORT_SYMBOL(i2o_driver_unregister);
  311. EXPORT_SYMBOL(i2o_driver_notify_controller_add_all);
  312. EXPORT_SYMBOL(i2o_driver_notify_controller_remove_all);
  313. EXPORT_SYMBOL(i2o_driver_notify_device_add_all);
  314. EXPORT_SYMBOL(i2o_driver_notify_device_remove_all);