scsi_dh.c 9.1 KB

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