scsi_dh.c 9.7 KB

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