driver.c 9.4 KB

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