treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / scsi / scsi_dh.c
blob42f0550d6b11ff81b2306fc135dff8f716516131
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * SCSI device handler infrastruture.
5 * Copyright IBM Corporation, 2007
6 * Authors:
7 * Chandra Seetharaman <sekharan@us.ibm.com>
8 * Mike Anderson <andmike@linux.vnet.ibm.com>
9 */
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <scsi/scsi_dh.h>
14 #include "scsi_priv.h"
16 static DEFINE_SPINLOCK(list_lock);
17 static LIST_HEAD(scsi_dh_list);
19 struct scsi_dh_blist {
20 const char *vendor;
21 const char *model;
22 const char *driver;
25 static const struct scsi_dh_blist scsi_dh_blist[] = {
26 {"DGC", "RAID", "emc" },
27 {"DGC", "DISK", "emc" },
28 {"DGC", "VRAID", "emc" },
30 {"COMPAQ", "MSA1000 VOLUME", "hp_sw" },
31 {"COMPAQ", "HSV110", "hp_sw" },
32 {"HP", "HSV100", "hp_sw"},
33 {"DEC", "HSG80", "hp_sw"},
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", },
46 {"IBM", "3542", "rdac", },
47 {"IBM", "3552", "rdac", },
48 {"SGI", "TP9300", "rdac", },
49 {"SGI", "TP9400", "rdac", },
50 {"SGI", "TP9500", "rdac", },
51 {"SGI", "TP9700", "rdac", },
52 {"SGI", "IS", "rdac", },
53 {"STK", "OPENstorage", "rdac", },
54 {"STK", "FLEXLINE 380", "rdac", },
55 {"STK", "BladeCtlr", "rdac", },
56 {"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", },
65 {"LENOVO", "DE_Series", "rdac", },
66 {NULL, NULL, NULL },
69 static const char *
70 scsi_dh_find_driver(struct scsi_device *sdev)
72 const struct scsi_dh_blist *b;
74 if (scsi_device_tpgs(sdev))
75 return "alua";
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;
83 return NULL;
87 static struct scsi_device_handler *__scsi_dh_lookup(const char *name)
89 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;
98 spin_unlock(&list_lock);
99 return found;
102 static struct scsi_device_handler *scsi_dh_lookup(const char *name)
104 struct scsi_device_handler *dh;
106 if (!name || strlen(name) == 0)
107 return NULL;
109 dh = __scsi_dh_lookup(name);
110 if (!dh) {
111 request_module("scsi_dh_%s", name);
112 dh = __scsi_dh_lookup(name);
115 return dh;
119 * 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
123 static int scsi_dh_handler_attach(struct scsi_device *sdev,
124 struct scsi_device_handler *scsi_dh)
126 int error, ret = 0;
128 if (!try_module_get(scsi_dh->module))
129 return -EINVAL;
131 error = scsi_dh->attach(sdev);
132 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;
140 case SCSI_DH_DEV_UNSUPP:
141 case SCSI_DH_NOSYS:
142 ret = -ENODEV;
143 break;
144 default:
145 ret = -EINVAL;
146 break;
148 if (ret != -ENODEV)
149 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%d)\n",
150 scsi_dh->name, error);
151 module_put(scsi_dh->module);
152 } else
153 sdev->handler = scsi_dh;
155 return ret;
159 * scsi_dh_handler_detach - Detach a device handler from a device
160 * @sdev - SCSI device the device handler should be detached from
162 static void scsi_dh_handler_detach(struct scsi_device *sdev)
164 sdev->handler->detach(sdev);
165 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", sdev->handler->name);
166 module_put(sdev->handler->module);
169 void scsi_dh_add_device(struct scsi_device *sdev)
171 struct scsi_device_handler *devinfo = NULL;
172 const char *drv;
174 drv = scsi_dh_find_driver(sdev);
175 if (drv)
176 devinfo = __scsi_dh_lookup(drv);
178 * device_handler is optional, so ignore errors
179 * from scsi_dh_handler_attach()
181 if (devinfo)
182 (void)scsi_dh_handler_attach(sdev, devinfo);
185 void scsi_dh_release_device(struct scsi_device *sdev)
187 if (sdev->handler)
188 scsi_dh_handler_detach(sdev);
192 * scsi_register_device_handler - register a device handler personality
193 * module.
194 * @scsi_dh - device handler to be registered.
196 * Returns 0 on success, -EBUSY if handler already registered.
198 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
200 if (__scsi_dh_lookup(scsi_dh->name))
201 return -EBUSY;
203 if (!scsi_dh->attach || !scsi_dh->detach)
204 return -EINVAL;
206 spin_lock(&list_lock);
207 list_add(&scsi_dh->list, &scsi_dh_list);
208 spin_unlock(&list_lock);
210 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
212 return SCSI_DH_OK;
214 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
217 * scsi_unregister_device_handler - register a device handler personality
218 * module.
219 * @scsi_dh - device handler to be unregistered.
221 * Returns 0 on success, -ENODEV if handler not registered.
223 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
225 if (!__scsi_dh_lookup(scsi_dh->name))
226 return -ENODEV;
228 spin_lock(&list_lock);
229 list_del(&scsi_dh->list);
230 spin_unlock(&list_lock);
231 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
233 return SCSI_DH_OK;
235 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
238 * scsi_dh_activate - activate the path associated with the scsi_device
239 * corresponding to the given request queue.
240 * 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.
250 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
252 struct scsi_device *sdev;
253 int err = SCSI_DH_NOSYS;
255 sdev = scsi_device_from_queue(q);
256 if (!sdev) {
257 if (fn)
258 fn(data, err);
259 return err;
262 if (!sdev->handler)
263 goto out_fn;
264 err = SCSI_DH_NOTCONN;
265 if (sdev->sdev_state == SDEV_CANCEL ||
266 sdev->sdev_state == SDEV_DEL)
267 goto out_fn;
269 err = SCSI_DH_DEV_OFFLINED;
270 if (sdev->sdev_state == SDEV_OFFLINE)
271 goto out_fn;
273 if (sdev->handler->activate)
274 err = sdev->handler->activate(sdev, fn, data);
276 out_put_device:
277 put_device(&sdev->sdev_gendev);
278 return err;
280 out_fn:
281 if (fn)
282 fn(data, err);
283 goto out_put_device;
285 EXPORT_SYMBOL_GPL(scsi_dh_activate);
288 * 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".
297 int scsi_dh_set_params(struct request_queue *q, const char *params)
299 struct scsi_device *sdev;
300 int err = -SCSI_DH_NOSYS;
302 sdev = scsi_device_from_queue(q);
303 if (!sdev)
304 return err;
306 if (sdev->handler && sdev->handler->set_params)
307 err = sdev->handler->set_params(sdev, params);
308 put_device(&sdev->sdev_gendev);
309 return err;
311 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
314 * scsi_dh_attach - Attach device handler
315 * @q - Request queue that is associated with the scsi_device
316 * the handler should be attached to
317 * @name - name of the handler to attach
319 int scsi_dh_attach(struct request_queue *q, const char *name)
321 struct scsi_device *sdev;
322 struct scsi_device_handler *scsi_dh;
323 int err = 0;
325 sdev = scsi_device_from_queue(q);
326 if (!sdev)
327 return -ENODEV;
329 scsi_dh = scsi_dh_lookup(name);
330 if (!scsi_dh) {
331 err = -EINVAL;
332 goto out_put_device;
335 if (sdev->handler) {
336 if (sdev->handler != scsi_dh)
337 err = -EBUSY;
338 goto out_put_device;
341 err = scsi_dh_handler_attach(sdev, scsi_dh);
343 out_put_device:
344 put_device(&sdev->sdev_gendev);
345 return err;
347 EXPORT_SYMBOL_GPL(scsi_dh_attach);
350 * 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
355 * Returns name of attached handler, NULL if no handler is attached.
356 * Caller must take care to free the returned string.
358 const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
360 struct scsi_device *sdev;
361 const char *handler_name = NULL;
363 sdev = scsi_device_from_queue(q);
364 if (!sdev)
365 return NULL;
367 if (sdev->handler)
368 handler_name = kstrdup(sdev->handler->name, gfp);
369 put_device(&sdev->sdev_gendev);
370 return handler_name;
372 EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);