blob: 42f0550d6b11ff81b2306fc135dff8f716516131 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07002/*
3 * SCSI device handler infrastruture.
4 *
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07005 * Copyright IBM Corporation, 2007
6 * Authors:
7 * Chandra Seetharaman <sekharan@us.ibm.com>
8 * Mike Anderson <andmike@linux.vnet.ibm.com>
9 */
10
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040012#include <linux/module.h>
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070013#include <scsi/scsi_dh.h>
Christoph Hellwigdaaa8582015-08-27 14:16:56 +020014#include "scsi_priv.h"
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070015
16static DEFINE_SPINLOCK(list_lock);
17static LIST_HEAD(scsi_dh_list);
18
Christoph Hellwigd95dbff2015-08-27 14:16:58 +020019struct scsi_dh_blist {
20 const char *vendor;
21 const char *model;
22 const char *driver;
23};
24
25static const struct scsi_dh_blist scsi_dh_blist[] = {
Xose Vazquez Perez0ba43a82016-10-07 18:19:57 +020026 {"DGC", "RAID", "emc" },
27 {"DGC", "DISK", "emc" },
28 {"DGC", "VRAID", "emc" },
Christoph Hellwigd95dbff2015-08-27 14:16:58 +020029
30 {"COMPAQ", "MSA1000 VOLUME", "hp_sw" },
31 {"COMPAQ", "HSV110", "hp_sw" },
32 {"HP", "HSV100", "hp_sw"},
33 {"DEC", "HSG80", "hp_sw"},
34
35 {"IBM", "1722", "rdac", },
36 {"IBM", "1724", "rdac", },
37 {"IBM", "1726", "rdac", },
38 {"IBM", "1742", "rdac", },
39 {"IBM", "1745", "rdac", },
40 {"IBM", "1746", "rdac", },
41 {"IBM", "1813", "rdac", },
42 {"IBM", "1814", "rdac", },
43 {"IBM", "1815", "rdac", },
44 {"IBM", "1818", "rdac", },
45 {"IBM", "3526", "rdac", },
Xose Vazquez Perez4b3aec22017-11-17 22:05:13 +010046 {"IBM", "3542", "rdac", },
47 {"IBM", "3552", "rdac", },
Xose Vazquez Perez37b37d22018-04-07 00:47:23 +020048 {"SGI", "TP9300", "rdac", },
49 {"SGI", "TP9400", "rdac", },
50 {"SGI", "TP9500", "rdac", },
51 {"SGI", "TP9700", "rdac", },
Christoph Hellwigd95dbff2015-08-27 14:16:58 +020052 {"SGI", "IS", "rdac", },
Xose Vazquez Perez4b3aec22017-11-17 22:05:13 +010053 {"STK", "OPENstorage", "rdac", },
Christoph Hellwigd95dbff2015-08-27 14:16:58 +020054 {"STK", "FLEXLINE 380", "rdac", },
Xose Vazquez Perez4b3aec22017-11-17 22:05:13 +010055 {"STK", "BladeCtlr", "rdac", },
Christoph Hellwigd95dbff2015-08-27 14:16:58 +020056 {"SUN", "CSM", "rdac", },
57 {"SUN", "LCSM100", "rdac", },
58 {"SUN", "STK6580_6780", "rdac", },
59 {"SUN", "SUN_6180", "rdac", },
60 {"SUN", "ArrayStorage", "rdac", },
61 {"DELL", "MD3", "rdac", },
62 {"NETAPP", "INF-01-00", "rdac", },
63 {"LSI", "INF-01-00", "rdac", },
64 {"ENGENIO", "INF-01-00", "rdac", },
Xose Vazquez Perez1cb1d2c2019-03-30 15:43:31 +010065 {"LENOVO", "DE_Series", "rdac", },
Christoph Hellwigd95dbff2015-08-27 14:16:58 +020066 {NULL, NULL, NULL },
67};
68
69static const char *
70scsi_dh_find_driver(struct scsi_device *sdev)
71{
72 const struct scsi_dh_blist *b;
73
74 if (scsi_device_tpgs(sdev))
75 return "alua";
76
77 for (b = scsi_dh_blist; b->vendor; b++) {
78 if (!strncmp(sdev->vendor, b->vendor, strlen(b->vendor)) &&
79 !strncmp(sdev->model, b->model, strlen(b->model))) {
80 return b->driver;
81 }
82 }
83 return NULL;
84}
85
86
Christoph Hellwig566079c2015-08-27 14:16:55 +020087static struct scsi_device_handler *__scsi_dh_lookup(const char *name)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070088{
89 struct scsi_device_handler *tmp, *found = NULL;
90
91 spin_lock(&list_lock);
92 list_for_each_entry(tmp, &scsi_dh_list, list) {
Hannes Reinecke765cbc62008-07-17 16:52:51 -070093 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070094 found = tmp;
95 break;
96 }
97 }
98 spin_unlock(&list_lock);
99 return found;
100}
101
Christoph Hellwig566079c2015-08-27 14:16:55 +0200102static struct scsi_device_handler *scsi_dh_lookup(const char *name)
103{
104 struct scsi_device_handler *dh;
105
Johannes Thumshirn2ee56712018-03-23 14:37:05 +0100106 if (!name || strlen(name) == 0)
107 return NULL;
108
Christoph Hellwig566079c2015-08-27 14:16:55 +0200109 dh = __scsi_dh_lookup(name);
110 if (!dh) {
Paul Mackerras13788892015-09-26 10:19:15 +1000111 request_module("scsi_dh_%s", name);
Christoph Hellwig566079c2015-08-27 14:16:55 +0200112 dh = __scsi_dh_lookup(name);
113 }
114
115 return dh;
116}
117
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200118/*
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700119 * scsi_dh_handler_attach - Attach a device handler to a device
120 * @sdev - SCSI device the device handler should attach to
121 * @scsi_dh - The device handler to attach
122 */
123static int scsi_dh_handler_attach(struct scsi_device *sdev,
124 struct scsi_device_handler *scsi_dh)
125{
Hannes Reinecke2a8f7a02017-09-20 09:18:51 +0200126 int error, ret = 0;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700127
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700128 if (!try_module_get(scsi_dh->module))
129 return -EINVAL;
Christoph Hellwig27c888f2014-09-13 19:41:16 -0700130
Christoph Hellwigee14c672015-08-27 14:16:59 +0200131 error = scsi_dh->attach(sdev);
Hannes Reinecke2a8f7a02017-09-20 09:18:51 +0200132 if (error != SCSI_DH_OK) {
133 switch (error) {
134 case SCSI_DH_NOMEM:
135 ret = -ENOMEM;
136 break;
137 case SCSI_DH_RES_TEMP_UNAVAIL:
138 ret = -EAGAIN;
139 break;
Hannes Reinecke2930f812017-09-20 09:18:52 +0200140 case SCSI_DH_DEV_UNSUPP:
141 case SCSI_DH_NOSYS:
142 ret = -ENODEV;
143 break;
Hannes Reinecke2a8f7a02017-09-20 09:18:51 +0200144 default:
145 ret = -EINVAL;
146 break;
147 }
Hannes Reinecke2930f812017-09-20 09:18:52 +0200148 if (ret != -ENODEV)
149 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%d)\n",
150 scsi_dh->name, error);
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700151 module_put(scsi_dh->module);
Christoph Hellwigee14c672015-08-27 14:16:59 +0200152 } else
153 sdev->handler = scsi_dh;
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700154
Hannes Reinecke2a8f7a02017-09-20 09:18:51 +0200155 return ret;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700156}
157
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200158/*
159 * scsi_dh_handler_detach - Detach a device handler from a device
160 * @sdev - SCSI device the device handler should be detached from
161 */
162static void scsi_dh_handler_detach(struct scsi_device *sdev)
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700163{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200164 sdev->handler->detach(sdev);
165 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", sdev->handler->name);
166 module_put(sdev->handler->module);
Chandra Seetharaman6c10db72009-06-26 19:30:06 -0700167}
168
Hannes Reinecke2930f812017-09-20 09:18:52 +0200169void scsi_dh_add_device(struct scsi_device *sdev)
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700170{
Christoph Hellwigd95dbff2015-08-27 14:16:58 +0200171 struct scsi_device_handler *devinfo = NULL;
172 const char *drv;
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700173
Christoph Hellwigd95dbff2015-08-27 14:16:58 +0200174 drv = scsi_dh_find_driver(sdev);
175 if (drv)
Christoph Hellwigd6a32b92015-10-08 09:25:32 +0100176 devinfo = __scsi_dh_lookup(drv);
Hannes Reinecke2930f812017-09-20 09:18:52 +0200177 /*
178 * device_handler is optional, so ignore errors
179 * from scsi_dh_handler_attach()
180 */
Christoph Hellwig086b91d2015-08-27 14:16:57 +0200181 if (devinfo)
Hannes Reinecke2930f812017-09-20 09:18:52 +0200182 (void)scsi_dh_handler_attach(sdev, devinfo);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700183}
184
Junichi Nomura23695e42015-10-06 04:32:57 +0000185void scsi_dh_release_device(struct scsi_device *sdev)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700186{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200187 if (sdev->handler)
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200188 scsi_dh_handler_detach(sdev);
Junichi Nomura23695e42015-10-06 04:32:57 +0000189}
190
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700191/*
192 * scsi_register_device_handler - register a device handler personality
193 * module.
194 * @scsi_dh - device handler to be registered.
195 *
196 * Returns 0 on success, -EBUSY if handler already registered.
197 */
198int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
199{
Christoph Hellwig566079c2015-08-27 14:16:55 +0200200 if (__scsi_dh_lookup(scsi_dh->name))
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700201 return -EBUSY;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700202
Christoph Hellwig1f12ffa2014-09-13 20:08:44 -0700203 if (!scsi_dh->attach || !scsi_dh->detach)
204 return -EINVAL;
205
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700206 spin_lock(&list_lock);
207 list_add(&scsi_dh->list, &scsi_dh_list);
208 spin_unlock(&list_lock);
Peter Jones940d7fa2011-01-06 15:38:24 -0500209
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700210 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700211
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700212 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700213}
214EXPORT_SYMBOL_GPL(scsi_register_device_handler);
215
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700216/*
217 * scsi_unregister_device_handler - register a device handler personality
218 * module.
219 * @scsi_dh - device handler to be unregistered.
220 *
221 * Returns 0 on success, -ENODEV if handler not registered.
222 */
223int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
224{
Christoph Hellwig566079c2015-08-27 14:16:55 +0200225 if (!__scsi_dh_lookup(scsi_dh->name))
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700226 return -ENODEV;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700227
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700228 spin_lock(&list_lock);
229 list_del(&scsi_dh->list);
230 spin_unlock(&list_lock);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700231 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700232
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700233 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700234}
235EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
236
237/*
238 * scsi_dh_activate - activate the path associated with the scsi_device
239 * corresponding to the given request queue.
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700240 * Returns immediately without waiting for activation to be completed.
241 * @q - Request queue that is associated with the scsi_device to be
242 * activated.
243 * @fn - Function to be called upon completion of the activation.
244 * Function fn is called with data (below) and the error code.
245 * Function fn may be called from the same calling context. So,
246 * do not hold the lock in the caller which may be needed in fn.
247 * @data - data passed to the function fn upon completion.
248 *
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700249 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700250int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700251{
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700252 struct scsi_device *sdev;
Christoph Hellwige959ed92015-08-27 14:17:00 +0200253 int err = SCSI_DH_NOSYS;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700254
Hannes Reinecke857de6e2017-02-17 09:02:45 +0100255 sdev = scsi_device_from_queue(q);
Moger, Babua18a9202011-10-26 14:29:38 -0400256 if (!sdev) {
Moger, Babua18a9202011-10-26 14:29:38 -0400257 if (fn)
258 fn(data, err);
259 return err;
260 }
261
Christoph Hellwige959ed92015-08-27 14:17:00 +0200262 if (!sdev->handler)
263 goto out_fn;
Hannes Reinecke710105f2015-08-27 14:17:02 +0200264 err = SCSI_DH_NOTCONN;
Christoph Hellwige959ed92015-08-27 14:17:00 +0200265 if (sdev->sdev_state == SDEV_CANCEL ||
Menny Hamburgerdb422312010-12-16 14:57:07 -0500266 sdev->sdev_state == SDEV_DEL)
Christoph Hellwige959ed92015-08-27 14:17:00 +0200267 goto out_fn;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700268
Christoph Hellwige959ed92015-08-27 14:17:00 +0200269 err = SCSI_DH_DEV_OFFLINED;
270 if (sdev->sdev_state == SDEV_OFFLINE)
271 goto out_fn;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700272
Christoph Hellwigee14c672015-08-27 14:16:59 +0200273 if (sdev->handler->activate)
274 err = sdev->handler->activate(sdev, fn, data);
Christoph Hellwige959ed92015-08-27 14:17:00 +0200275
276out_put_device:
277 put_device(&sdev->sdev_gendev);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700278 return err;
Christoph Hellwige959ed92015-08-27 14:17:00 +0200279
280out_fn:
281 if (fn)
282 fn(data, err);
283 goto out_put_device;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700284}
285EXPORT_SYMBOL_GPL(scsi_dh_activate);
286
287/*
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700288 * scsi_dh_set_params - set the parameters for the device as per the
289 * string specified in params.
290 * @q - Request queue that is associated with the scsi_device for
291 * which the parameters to be set.
292 * @params - parameters in the following format
293 * "no_of_params\0param1\0param2\0param3\0...\0"
294 * for example, string for 2 parameters with value 10 and 21
295 * is specified as "2\010\021\0".
296 */
297int scsi_dh_set_params(struct request_queue *q, const char *params)
298{
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700299 struct scsi_device *sdev;
Christoph Hellwige959ed92015-08-27 14:17:00 +0200300 int err = -SCSI_DH_NOSYS;
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700301
Hannes Reinecke857de6e2017-02-17 09:02:45 +0100302 sdev = scsi_device_from_queue(q);
Christoph Hellwige959ed92015-08-27 14:17:00 +0200303 if (!sdev)
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700304 return err;
Christoph Hellwige959ed92015-08-27 14:17:00 +0200305
306 if (sdev->handler && sdev->handler->set_params)
307 err = sdev->handler->set_params(sdev, params);
Chandra Seetharaman18ee70c2009-08-03 12:42:33 -0700308 put_device(&sdev->sdev_gendev);
309 return err;
310}
311EXPORT_SYMBOL_GPL(scsi_dh_set_params);
312
313/*
Hannes Reinecke2a9ab402011-08-24 10:51:14 +0200314 * scsi_dh_attach - Attach device handler
Mike Snitzer7e8a74b2012-06-26 14:32:03 -0400315 * @q - Request queue that is associated with the scsi_device
316 * the handler should be attached to
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700317 * @name - name of the handler to attach
318 */
319int scsi_dh_attach(struct request_queue *q, const char *name)
320{
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700321 struct scsi_device *sdev;
322 struct scsi_device_handler *scsi_dh;
323 int err = 0;
324
Hannes Reinecke857de6e2017-02-17 09:02:45 +0100325 sdev = scsi_device_from_queue(q);
Christoph Hellwige959ed92015-08-27 14:17:00 +0200326 if (!sdev)
327 return -ENODEV;
328
Christoph Hellwig566079c2015-08-27 14:16:55 +0200329 scsi_dh = scsi_dh_lookup(name);
Christoph Hellwige959ed92015-08-27 14:17:00 +0200330 if (!scsi_dh) {
331 err = -EINVAL;
332 goto out_put_device;
333 }
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200334
Christoph Hellwigee14c672015-08-27 14:16:59 +0200335 if (sdev->handler) {
336 if (sdev->handler != scsi_dh)
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200337 err = -EBUSY;
338 goto out_put_device;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700339 }
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200340
341 err = scsi_dh_handler_attach(sdev, scsi_dh);
342
343out_put_device:
344 put_device(&sdev->sdev_gendev);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700345 return err;
346}
347EXPORT_SYMBOL_GPL(scsi_dh_attach);
348
349/*
Mike Snitzer7e8a74b2012-06-26 14:32:03 -0400350 * scsi_dh_attached_handler_name - Get attached device handler's name
351 * @q - Request queue that is associated with the scsi_device
352 * that may have a device handler attached
353 * @gfp - the GFP mask used in the kmalloc() call when allocating memory
354 *
355 * Returns name of attached handler, NULL if no handler is attached.
356 * Caller must take care to free the returned string.
357 */
358const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
359{
Mike Snitzer7e8a74b2012-06-26 14:32:03 -0400360 struct scsi_device *sdev;
361 const char *handler_name = NULL;
362
Hannes Reinecke857de6e2017-02-17 09:02:45 +0100363 sdev = scsi_device_from_queue(q);
Mike Snitzer7e8a74b2012-06-26 14:32:03 -0400364 if (!sdev)
365 return NULL;
366
Christoph Hellwigee14c672015-08-27 14:16:59 +0200367 if (sdev->handler)
368 handler_name = kstrdup(sdev->handler->name, gfp);
Mike Snitzer7e8a74b2012-06-26 14:32:03 -0400369 put_device(&sdev->sdev_gendev);
370 return handler_name;
371}
372EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);