driver.c 9.4 KB

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