scsi_dh.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. {"SGI", "TP9", "rdac", },
  54. {"SGI", "IS", "rdac", },
  55. {"STK", "OPENstorage D280", "rdac", },
  56. {"STK", "FLEXLINE 380", "rdac", },
  57. {"SUN", "CSM", "rdac", },
  58. {"SUN", "LCSM100", "rdac", },
  59. {"SUN", "STK6580_6780", "rdac", },
  60. {"SUN", "SUN_6180", "rdac", },
  61. {"SUN", "ArrayStorage", "rdac", },
  62. {"DELL", "MD3", "rdac", },
  63. {"NETAPP", "INF-01-00", "rdac", },
  64. {"LSI", "INF-01-00", "rdac", },
  65. {"ENGENIO", "INF-01-00", "rdac", },
  66. {NULL, NULL, NULL },
  67. };
  68. static const char *
  69. scsi_dh_find_driver(struct scsi_device *sdev)
  70. {
  71. const struct scsi_dh_blist *b;
  72. if (scsi_device_tpgs(sdev))
  73. return "alua";
  74. for (b = scsi_dh_blist; b->vendor; b++) {
  75. if (!strncmp(sdev->vendor, b->vendor, strlen(b->vendor)) &&
  76. !strncmp(sdev->model, b->model, strlen(b->model))) {
  77. return b->driver;
  78. }
  79. }
  80. return NULL;
  81. }
  82. static struct scsi_device_handler *__scsi_dh_lookup(const char *name)
  83. {
  84. struct scsi_device_handler *tmp, *found = NULL;
  85. spin_lock(&list_lock);
  86. list_for_each_entry(tmp, &scsi_dh_list, list) {
  87. if (!strncmp(tmp->name, name, strlen(tmp->name))) {
  88. found = tmp;
  89. break;
  90. }
  91. }
  92. spin_unlock(&list_lock);
  93. return found;
  94. }
  95. static struct scsi_device_handler *scsi_dh_lookup(const char *name)
  96. {
  97. struct scsi_device_handler *dh;
  98. dh = __scsi_dh_lookup(name);
  99. if (!dh) {
  100. request_module("scsi_dh_%s", name);
  101. dh = __scsi_dh_lookup(name);
  102. }
  103. return dh;
  104. }
  105. /*
  106. * scsi_dh_handler_attach - Attach a device handler to a device
  107. * @sdev - SCSI device the device handler should attach to
  108. * @scsi_dh - The device handler to attach
  109. */
  110. static int scsi_dh_handler_attach(struct scsi_device *sdev,
  111. struct scsi_device_handler *scsi_dh)
  112. {
  113. int error;
  114. if (!try_module_get(scsi_dh->module))
  115. return -EINVAL;
  116. error = scsi_dh->attach(sdev);
  117. if (error) {
  118. sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%d)\n",
  119. scsi_dh->name, error);
  120. module_put(scsi_dh->module);
  121. } else
  122. sdev->handler = scsi_dh;
  123. return error;
  124. }
  125. /*
  126. * scsi_dh_handler_detach - Detach a device handler from a device
  127. * @sdev - SCSI device the device handler should be detached from
  128. */
  129. static void scsi_dh_handler_detach(struct scsi_device *sdev)
  130. {
  131. sdev->handler->detach(sdev);
  132. sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", sdev->handler->name);
  133. module_put(sdev->handler->module);
  134. }
  135. int scsi_dh_add_device(struct scsi_device *sdev)
  136. {
  137. struct scsi_device_handler *devinfo = NULL;
  138. const char *drv;
  139. int err = 0;
  140. drv = scsi_dh_find_driver(sdev);
  141. if (drv)
  142. devinfo = __scsi_dh_lookup(drv);
  143. if (devinfo)
  144. err = scsi_dh_handler_attach(sdev, devinfo);
  145. return err;
  146. }
  147. void scsi_dh_release_device(struct scsi_device *sdev)
  148. {
  149. if (sdev->handler)
  150. scsi_dh_handler_detach(sdev);
  151. }
  152. /*
  153. * scsi_register_device_handler - register a device handler personality
  154. * module.
  155. * @scsi_dh - device handler to be registered.
  156. *
  157. * Returns 0 on success, -EBUSY if handler already registered.
  158. */
  159. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  160. {
  161. if (__scsi_dh_lookup(scsi_dh->name))
  162. return -EBUSY;
  163. if (!scsi_dh->attach || !scsi_dh->detach)
  164. return -EINVAL;
  165. spin_lock(&list_lock);
  166. list_add(&scsi_dh->list, &scsi_dh_list);
  167. spin_unlock(&list_lock);
  168. printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
  169. return SCSI_DH_OK;
  170. }
  171. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  172. /*
  173. * scsi_unregister_device_handler - register a device handler personality
  174. * module.
  175. * @scsi_dh - device handler to be unregistered.
  176. *
  177. * Returns 0 on success, -ENODEV if handler not registered.
  178. */
  179. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  180. {
  181. if (!__scsi_dh_lookup(scsi_dh->name))
  182. return -ENODEV;
  183. spin_lock(&list_lock);
  184. list_del(&scsi_dh->list);
  185. spin_unlock(&list_lock);
  186. printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
  187. return SCSI_DH_OK;
  188. }
  189. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  190. /*
  191. * scsi_dh_activate - activate the path associated with the scsi_device
  192. * corresponding to the given request queue.
  193. * Returns immediately without waiting for activation to be completed.
  194. * @q - Request queue that is associated with the scsi_device to be
  195. * activated.
  196. * @fn - Function to be called upon completion of the activation.
  197. * Function fn is called with data (below) and the error code.
  198. * Function fn may be called from the same calling context. So,
  199. * do not hold the lock in the caller which may be needed in fn.
  200. * @data - data passed to the function fn upon completion.
  201. *
  202. */
  203. int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
  204. {
  205. struct scsi_device *sdev;
  206. int err = SCSI_DH_NOSYS;
  207. sdev = scsi_device_from_queue(q);
  208. if (!sdev) {
  209. if (fn)
  210. fn(data, err);
  211. return err;
  212. }
  213. if (!sdev->handler)
  214. goto out_fn;
  215. err = SCSI_DH_NOTCONN;
  216. if (sdev->sdev_state == SDEV_CANCEL ||
  217. sdev->sdev_state == SDEV_DEL)
  218. goto out_fn;
  219. err = SCSI_DH_DEV_OFFLINED;
  220. if (sdev->sdev_state == SDEV_OFFLINE)
  221. goto out_fn;
  222. if (sdev->handler->activate)
  223. err = sdev->handler->activate(sdev, fn, data);
  224. out_put_device:
  225. put_device(&sdev->sdev_gendev);
  226. return err;
  227. out_fn:
  228. if (fn)
  229. fn(data, err);
  230. goto out_put_device;
  231. }
  232. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  233. /*
  234. * scsi_dh_set_params - set the parameters for the device as per the
  235. * string specified in params.
  236. * @q - Request queue that is associated with the scsi_device for
  237. * which the parameters to be set.
  238. * @params - parameters in the following format
  239. * "no_of_params\0param1\0param2\0param3\0...\0"
  240. * for example, string for 2 parameters with value 10 and 21
  241. * is specified as "2\010\021\0".
  242. */
  243. int scsi_dh_set_params(struct request_queue *q, const char *params)
  244. {
  245. struct scsi_device *sdev;
  246. int err = -SCSI_DH_NOSYS;
  247. sdev = scsi_device_from_queue(q);
  248. if (!sdev)
  249. return err;
  250. if (sdev->handler && sdev->handler->set_params)
  251. err = sdev->handler->set_params(sdev, params);
  252. put_device(&sdev->sdev_gendev);
  253. return err;
  254. }
  255. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  256. /*
  257. * scsi_dh_attach - Attach device handler
  258. * @q - Request queue that is associated with the scsi_device
  259. * the handler should be attached to
  260. * @name - name of the handler to attach
  261. */
  262. int scsi_dh_attach(struct request_queue *q, const char *name)
  263. {
  264. struct scsi_device *sdev;
  265. struct scsi_device_handler *scsi_dh;
  266. int err = 0;
  267. sdev = scsi_device_from_queue(q);
  268. if (!sdev)
  269. return -ENODEV;
  270. scsi_dh = scsi_dh_lookup(name);
  271. if (!scsi_dh) {
  272. err = -EINVAL;
  273. goto out_put_device;
  274. }
  275. if (sdev->handler) {
  276. if (sdev->handler != scsi_dh)
  277. err = -EBUSY;
  278. goto out_put_device;
  279. }
  280. err = scsi_dh_handler_attach(sdev, scsi_dh);
  281. out_put_device:
  282. put_device(&sdev->sdev_gendev);
  283. return err;
  284. }
  285. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  286. /*
  287. * scsi_dh_attached_handler_name - Get attached device handler's name
  288. * @q - Request queue that is associated with the scsi_device
  289. * that may have a device handler attached
  290. * @gfp - the GFP mask used in the kmalloc() call when allocating memory
  291. *
  292. * Returns name of attached handler, NULL if no handler is attached.
  293. * Caller must take care to free the returned string.
  294. */
  295. const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
  296. {
  297. struct scsi_device *sdev;
  298. const char *handler_name = NULL;
  299. sdev = scsi_device_from_queue(q);
  300. if (!sdev)
  301. return NULL;
  302. if (sdev->handler)
  303. handler_name = kstrdup(sdev->handler->name, gfp);
  304. put_device(&sdev->sdev_gendev);
  305. return handler_name;
  306. }
  307. EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);