scsi_dh.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * SCSI device handler infrastruture.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2007
  19. * Authors:
  20. * Chandra Seetharaman <sekharan@us.ibm.com>
  21. * Mike Anderson <andmike@linux.vnet.ibm.com>
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <scsi/scsi_dh.h>
  26. #include "scsi_priv.h"
  27. static DEFINE_SPINLOCK(list_lock);
  28. static LIST_HEAD(scsi_dh_list);
  29. struct scsi_dh_blist {
  30. const char *vendor;
  31. const char *model;
  32. const char *driver;
  33. };
  34. static const struct scsi_dh_blist scsi_dh_blist[] = {
  35. {"DGC", "RAID", "emc" },
  36. {"DGC", "DISK", "emc" },
  37. {"DGC", "VRAID", "emc" },
  38. {"COMPAQ", "MSA1000 VOLUME", "hp_sw" },
  39. {"COMPAQ", "HSV110", "hp_sw" },
  40. {"HP", "HSV100", "hp_sw"},
  41. {"DEC", "HSG80", "hp_sw"},
  42. {"IBM", "1722", "rdac", },
  43. {"IBM", "1724", "rdac", },
  44. {"IBM", "1726", "rdac", },
  45. {"IBM", "1742", "rdac", },
  46. {"IBM", "1745", "rdac", },
  47. {"IBM", "1746", "rdac", },
  48. {"IBM", "1813", "rdac", },
  49. {"IBM", "1814", "rdac", },
  50. {"IBM", "1815", "rdac", },
  51. {"IBM", "1818", "rdac", },
  52. {"IBM", "3526", "rdac", },
  53. {"IBM", "3542", "rdac", },
  54. {"IBM", "3552", "rdac", },
  55. {"SGI", "TP9300", "rdac", },
  56. {"SGI", "TP9400", "rdac", },
  57. {"SGI", "TP9500", "rdac", },
  58. {"SGI", "TP9700", "rdac", },
  59. {"SGI", "IS", "rdac", },
  60. {"STK", "OPENstorage", "rdac", },
  61. {"STK", "FLEXLINE 380", "rdac", },
  62. {"STK", "BladeCtlr", "rdac", },
  63. {"SUN", "CSM", "rdac", },
  64. {"SUN", "LCSM100", "rdac", },
  65. {"SUN", "STK6580_6780", "rdac", },
  66. {"SUN", "SUN_6180", "rdac", },
  67. {"SUN", "ArrayStorage", "rdac", },
  68. {"DELL", "MD3", "rdac", },
  69. {"NETAPP", "INF-01-00", "rdac", },
  70. {"LSI", "INF-01-00", "rdac", },
  71. {"ENGENIO", "INF-01-00", "rdac", },
  72. {NULL, NULL, NULL },
  73. };
  74. static const char *
  75. scsi_dh_find_driver(struct scsi_device *sdev)
  76. {
  77. const struct scsi_dh_blist *b;
  78. if (scsi_device_tpgs(sdev))
  79. return "alua";
  80. for (b = scsi_dh_blist; b->vendor; b++) {
  81. if (!strncmp(sdev->vendor, b->vendor, strlen(b->vendor)) &&
  82. !strncmp(sdev->model, b->model, strlen(b->model))) {
  83. return b->driver;
  84. }
  85. }
  86. return NULL;
  87. }
  88. static struct scsi_device_handler *__scsi_dh_lookup(const char *name)
  89. {
  90. struct scsi_device_handler *tmp, *found = NULL;
  91. spin_lock(&list_lock);
  92. list_for_each_entry(tmp, &scsi_dh_list, list) {
  93. if (!strncmp(tmp->name, name, strlen(tmp->name))) {
  94. found = tmp;
  95. break;
  96. }
  97. }
  98. spin_unlock(&list_lock);
  99. return found;
  100. }
  101. static struct scsi_device_handler *scsi_dh_lookup(const char *name)
  102. {
  103. struct scsi_device_handler *dh;
  104. dh = __scsi_dh_lookup(name);
  105. if (!dh) {
  106. request_module("scsi_dh_%s", name);
  107. dh = __scsi_dh_lookup(name);
  108. }
  109. return dh;
  110. }
  111. /*
  112. * scsi_dh_handler_attach - Attach a device handler to a device
  113. * @sdev - SCSI device the device handler should attach to
  114. * @scsi_dh - The device handler to attach
  115. */
  116. static int scsi_dh_handler_attach(struct scsi_device *sdev,
  117. struct scsi_device_handler *scsi_dh)
  118. {
  119. int error;
  120. if (!try_module_get(scsi_dh->module))
  121. return -EINVAL;
  122. error = scsi_dh->attach(sdev);
  123. if (error) {
  124. sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%d)\n",
  125. scsi_dh->name, error);
  126. module_put(scsi_dh->module);
  127. } else
  128. sdev->handler = scsi_dh;
  129. return error;
  130. }
  131. /*
  132. * scsi_dh_handler_detach - Detach a device handler from a device
  133. * @sdev - SCSI device the device handler should be detached from
  134. */
  135. static void scsi_dh_handler_detach(struct scsi_device *sdev)
  136. {
  137. sdev->handler->detach(sdev);
  138. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", sdev->handler->name);
  139. module_put(sdev->handler->module);
  140. }
  141. int scsi_dh_add_device(struct scsi_device *sdev)
  142. {
  143. struct scsi_device_handler *devinfo = NULL;
  144. const char *drv;
  145. int err = 0;
  146. drv = scsi_dh_find_driver(sdev);
  147. if (drv)
  148. devinfo = __scsi_dh_lookup(drv);
  149. if (devinfo)
  150. err = scsi_dh_handler_attach(sdev, devinfo);
  151. return err;
  152. }
  153. void scsi_dh_release_device(struct scsi_device *sdev)
  154. {
  155. if (sdev->handler)
  156. scsi_dh_handler_detach(sdev);
  157. }
  158. /*
  159. * scsi_register_device_handler - register a device handler personality
  160. * module.
  161. * @scsi_dh - device handler to be registered.
  162. *
  163. * Returns 0 on success, -EBUSY if handler already registered.
  164. */
  165. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  166. {
  167. if (__scsi_dh_lookup(scsi_dh->name))
  168. return -EBUSY;
  169. if (!scsi_dh->attach || !scsi_dh->detach)
  170. return -EINVAL;
  171. spin_lock(&list_lock);
  172. list_add(&scsi_dh->list, &scsi_dh_list);
  173. spin_unlock(&list_lock);
  174. printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
  175. return SCSI_DH_OK;
  176. }
  177. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  178. /*
  179. * scsi_unregister_device_handler - register a device handler personality
  180. * module.
  181. * @scsi_dh - device handler to be unregistered.
  182. *
  183. * Returns 0 on success, -ENODEV if handler not registered.
  184. */
  185. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  186. {
  187. if (!__scsi_dh_lookup(scsi_dh->name))
  188. return -ENODEV;
  189. spin_lock(&list_lock);
  190. list_del(&scsi_dh->list);
  191. spin_unlock(&list_lock);
  192. printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
  193. return SCSI_DH_OK;
  194. }
  195. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  196. /*
  197. * scsi_dh_activate - activate the path associated with the scsi_device
  198. * corresponding to the given request queue.
  199. * Returns immediately without waiting for activation to be completed.
  200. * @q - Request queue that is associated with the scsi_device to be
  201. * activated.
  202. * @fn - Function to be called upon completion of the activation.
  203. * Function fn is called with data (below) and the error code.
  204. * Function fn may be called from the same calling context. So,
  205. * do not hold the lock in the caller which may be needed in fn.
  206. * @data - data passed to the function fn upon completion.
  207. *
  208. */
  209. int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
  210. {
  211. struct scsi_device *sdev;
  212. int err = SCSI_DH_NOSYS;
  213. sdev = scsi_device_from_queue(q);
  214. if (!sdev) {
  215. if (fn)
  216. fn(data, err);
  217. return err;
  218. }
  219. if (!sdev->handler)
  220. goto out_fn;
  221. err = SCSI_DH_NOTCONN;
  222. if (sdev->sdev_state == SDEV_CANCEL ||
  223. sdev->sdev_state == SDEV_DEL)
  224. goto out_fn;
  225. err = SCSI_DH_DEV_OFFLINED;
  226. if (sdev->sdev_state == SDEV_OFFLINE)
  227. goto out_fn;
  228. if (sdev->handler->activate)
  229. err = sdev->handler->activate(sdev, fn, data);
  230. out_put_device:
  231. put_device(&sdev->sdev_gendev);
  232. return err;
  233. out_fn:
  234. if (fn)
  235. fn(data, err);
  236. goto out_put_device;
  237. }
  238. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  239. /*
  240. * scsi_dh_set_params - set the parameters for the device as per the
  241. * string specified in params.
  242. * @q - Request queue that is associated with the scsi_device for
  243. * which the parameters to be set.
  244. * @params - parameters in the following format
  245. * "no_of_params\0param1\0param2\0param3\0...\0"
  246. * for example, string for 2 parameters with value 10 and 21
  247. * is specified as "2\010\021\0".
  248. */
  249. int scsi_dh_set_params(struct request_queue *q, const char *params)
  250. {
  251. struct scsi_device *sdev;
  252. int err = -SCSI_DH_NOSYS;
  253. sdev = scsi_device_from_queue(q);
  254. if (!sdev)
  255. return err;
  256. if (sdev->handler && sdev->handler->set_params)
  257. err = sdev->handler->set_params(sdev, params);
  258. put_device(&sdev->sdev_gendev);
  259. return err;
  260. }
  261. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  262. /*
  263. * scsi_dh_attach - Attach device handler
  264. * @q - Request queue that is associated with the scsi_device
  265. * the handler should be attached to
  266. * @name - name of the handler to attach
  267. */
  268. int scsi_dh_attach(struct request_queue *q, const char *name)
  269. {
  270. struct scsi_device *sdev;
  271. struct scsi_device_handler *scsi_dh;
  272. int err = 0;
  273. sdev = scsi_device_from_queue(q);
  274. if (!sdev)
  275. return -ENODEV;
  276. scsi_dh = scsi_dh_lookup(name);
  277. if (!scsi_dh) {
  278. err = -EINVAL;
  279. goto out_put_device;
  280. }
  281. if (sdev->handler) {
  282. if (sdev->handler != scsi_dh)
  283. err = -EBUSY;
  284. goto out_put_device;
  285. }
  286. err = scsi_dh_handler_attach(sdev, scsi_dh);
  287. out_put_device:
  288. put_device(&sdev->sdev_gendev);
  289. return err;
  290. }
  291. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  292. /*
  293. * scsi_dh_attached_handler_name - Get attached device handler's name
  294. * @q - Request queue that is associated with the scsi_device
  295. * that may have a device handler attached
  296. * @gfp - the GFP mask used in the kmalloc() call when allocating memory
  297. *
  298. * Returns name of attached handler, NULL if no handler is attached.
  299. * Caller must take care to free the returned string.
  300. */
  301. const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
  302. {
  303. struct scsi_device *sdev;
  304. const char *handler_name = NULL;
  305. sdev = scsi_device_from_queue(q);
  306. if (!sdev)
  307. return NULL;
  308. if (sdev->handler)
  309. handler_name = kstrdup(sdev->handler->name, gfp);
  310. put_device(&sdev->sdev_gendev);
  311. return handler_name;
  312. }
  313. EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);