scsi_dh.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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", "clariion" },
  36. {"DGC", "DISK", "clariion" },
  37. {"DGC", "VRAID", "clariion" },
  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. /*
  142. * Functions for sysfs attribute 'dh_state'
  143. */
  144. static ssize_t
  145. store_dh_state(struct device *dev, struct device_attribute *attr,
  146. const char *buf, size_t count)
  147. {
  148. struct scsi_device *sdev = to_scsi_device(dev);
  149. struct scsi_device_handler *scsi_dh;
  150. int err = -EINVAL;
  151. if (sdev->sdev_state == SDEV_CANCEL ||
  152. sdev->sdev_state == SDEV_DEL)
  153. return -ENODEV;
  154. if (!sdev->handler) {
  155. /*
  156. * Attach to a device handler
  157. */
  158. scsi_dh = scsi_dh_lookup(buf);
  159. if (!scsi_dh)
  160. return err;
  161. err = scsi_dh_handler_attach(sdev, scsi_dh);
  162. } else {
  163. if (!strncmp(buf, "detach", 6)) {
  164. /*
  165. * Detach from a device handler
  166. */
  167. sdev_printk(KERN_WARNING, sdev,
  168. "can't detach handler %s.\n",
  169. sdev->handler->name);
  170. err = -EINVAL;
  171. } else if (!strncmp(buf, "activate", 8)) {
  172. /*
  173. * Activate a device handler
  174. */
  175. if (sdev->handler->activate)
  176. err = sdev->handler->activate(sdev, NULL, NULL);
  177. else
  178. err = 0;
  179. }
  180. }
  181. return err<0?err:count;
  182. }
  183. static ssize_t
  184. show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
  185. {
  186. struct scsi_device *sdev = to_scsi_device(dev);
  187. if (!sdev->handler)
  188. return snprintf(buf, 20, "detached\n");
  189. return snprintf(buf, 20, "%s\n", sdev->handler->name);
  190. }
  191. static struct device_attribute scsi_dh_state_attr =
  192. __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
  193. store_dh_state);
  194. int scsi_dh_add_device(struct scsi_device *sdev)
  195. {
  196. struct scsi_device_handler *devinfo = NULL;
  197. const char *drv;
  198. int err;
  199. err = device_create_file(&sdev->sdev_gendev, &scsi_dh_state_attr);
  200. if (err)
  201. return err;
  202. drv = scsi_dh_find_driver(sdev);
  203. if (drv)
  204. devinfo = __scsi_dh_lookup(drv);
  205. if (devinfo)
  206. err = scsi_dh_handler_attach(sdev, devinfo);
  207. return err;
  208. }
  209. void scsi_dh_release_device(struct scsi_device *sdev)
  210. {
  211. if (sdev->handler)
  212. scsi_dh_handler_detach(sdev);
  213. }
  214. void scsi_dh_remove_device(struct scsi_device *sdev)
  215. {
  216. device_remove_file(&sdev->sdev_gendev, &scsi_dh_state_attr);
  217. }
  218. /*
  219. * scsi_register_device_handler - register a device handler personality
  220. * module.
  221. * @scsi_dh - device handler to be registered.
  222. *
  223. * Returns 0 on success, -EBUSY if handler already registered.
  224. */
  225. int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
  226. {
  227. if (__scsi_dh_lookup(scsi_dh->name))
  228. return -EBUSY;
  229. if (!scsi_dh->attach || !scsi_dh->detach)
  230. return -EINVAL;
  231. spin_lock(&list_lock);
  232. list_add(&scsi_dh->list, &scsi_dh_list);
  233. spin_unlock(&list_lock);
  234. printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
  235. return SCSI_DH_OK;
  236. }
  237. EXPORT_SYMBOL_GPL(scsi_register_device_handler);
  238. /*
  239. * scsi_unregister_device_handler - register a device handler personality
  240. * module.
  241. * @scsi_dh - device handler to be unregistered.
  242. *
  243. * Returns 0 on success, -ENODEV if handler not registered.
  244. */
  245. int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
  246. {
  247. if (!__scsi_dh_lookup(scsi_dh->name))
  248. return -ENODEV;
  249. spin_lock(&list_lock);
  250. list_del(&scsi_dh->list);
  251. spin_unlock(&list_lock);
  252. printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
  253. return SCSI_DH_OK;
  254. }
  255. EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
  256. /*
  257. * scsi_dh_activate - activate the path associated with the scsi_device
  258. * corresponding to the given request queue.
  259. * Returns immediately without waiting for activation to be completed.
  260. * @q - Request queue that is associated with the scsi_device to be
  261. * activated.
  262. * @fn - Function to be called upon completion of the activation.
  263. * Function fn is called with data (below) and the error code.
  264. * Function fn may be called from the same calling context. So,
  265. * do not hold the lock in the caller which may be needed in fn.
  266. * @data - data passed to the function fn upon completion.
  267. *
  268. */
  269. int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
  270. {
  271. struct scsi_device *sdev;
  272. int err = SCSI_DH_NOSYS;
  273. sdev = scsi_device_from_queue(q);
  274. if (!sdev) {
  275. if (fn)
  276. fn(data, err);
  277. return err;
  278. }
  279. if (!sdev->handler)
  280. goto out_fn;
  281. err = SCSI_DH_NOTCONN;
  282. if (sdev->sdev_state == SDEV_CANCEL ||
  283. sdev->sdev_state == SDEV_DEL)
  284. goto out_fn;
  285. err = SCSI_DH_DEV_OFFLINED;
  286. if (sdev->sdev_state == SDEV_OFFLINE)
  287. goto out_fn;
  288. if (sdev->handler->activate)
  289. err = sdev->handler->activate(sdev, fn, data);
  290. out_put_device:
  291. put_device(&sdev->sdev_gendev);
  292. return err;
  293. out_fn:
  294. if (fn)
  295. fn(data, err);
  296. goto out_put_device;
  297. }
  298. EXPORT_SYMBOL_GPL(scsi_dh_activate);
  299. /*
  300. * scsi_dh_set_params - set the parameters for the device as per the
  301. * string specified in params.
  302. * @q - Request queue that is associated with the scsi_device for
  303. * which the parameters to be set.
  304. * @params - parameters in the following format
  305. * "no_of_params\0param1\0param2\0param3\0...\0"
  306. * for example, string for 2 parameters with value 10 and 21
  307. * is specified as "2\010\021\0".
  308. */
  309. int scsi_dh_set_params(struct request_queue *q, const char *params)
  310. {
  311. struct scsi_device *sdev;
  312. int err = -SCSI_DH_NOSYS;
  313. sdev = scsi_device_from_queue(q);
  314. if (!sdev)
  315. return err;
  316. if (sdev->handler && sdev->handler->set_params)
  317. err = sdev->handler->set_params(sdev, params);
  318. put_device(&sdev->sdev_gendev);
  319. return err;
  320. }
  321. EXPORT_SYMBOL_GPL(scsi_dh_set_params);
  322. /*
  323. * scsi_dh_attach - Attach device handler
  324. * @q - Request queue that is associated with the scsi_device
  325. * the handler should be attached to
  326. * @name - name of the handler to attach
  327. */
  328. int scsi_dh_attach(struct request_queue *q, const char *name)
  329. {
  330. struct scsi_device *sdev;
  331. struct scsi_device_handler *scsi_dh;
  332. int err = 0;
  333. sdev = scsi_device_from_queue(q);
  334. if (!sdev)
  335. return -ENODEV;
  336. scsi_dh = scsi_dh_lookup(name);
  337. if (!scsi_dh) {
  338. err = -EINVAL;
  339. goto out_put_device;
  340. }
  341. if (sdev->handler) {
  342. if (sdev->handler != scsi_dh)
  343. err = -EBUSY;
  344. goto out_put_device;
  345. }
  346. err = scsi_dh_handler_attach(sdev, scsi_dh);
  347. out_put_device:
  348. put_device(&sdev->sdev_gendev);
  349. return err;
  350. }
  351. EXPORT_SYMBOL_GPL(scsi_dh_attach);
  352. /*
  353. * scsi_dh_attached_handler_name - Get attached device handler's name
  354. * @q - Request queue that is associated with the scsi_device
  355. * that may have a device handler attached
  356. * @gfp - the GFP mask used in the kmalloc() call when allocating memory
  357. *
  358. * Returns name of attached handler, NULL if no handler is attached.
  359. * Caller must take care to free the returned string.
  360. */
  361. const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
  362. {
  363. struct scsi_device *sdev;
  364. const char *handler_name = NULL;
  365. sdev = scsi_device_from_queue(q);
  366. if (!sdev)
  367. return NULL;
  368. if (sdev->handler)
  369. handler_name = kstrdup(sdev->handler->name, gfp);
  370. put_device(&sdev->sdev_gendev);
  371. return handler_name;
  372. }
  373. EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);