driver.c 9.3 KB

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