1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SCSI device handler infrastruture.
5 * Copyright IBM Corporation, 2007
7 * Chandra Seetharaman <sekharan@us.ibm.com>
8 * Mike Anderson <andmike@linux.vnet.ibm.com>
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
{
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", },
70 scsi_dh_find_driver(struct scsi_device
*sdev
)
72 const struct scsi_dh_blist
*b
;
74 if (scsi_device_tpgs(sdev
))
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
))) {
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
))) {
98 spin_unlock(&list_lock
);
102 static struct scsi_device_handler
*scsi_dh_lookup(const char *name
)
104 struct scsi_device_handler
*dh
;
106 if (!name
|| strlen(name
) == 0)
109 dh
= __scsi_dh_lookup(name
);
111 request_module("scsi_dh_%s", name
);
112 dh
= __scsi_dh_lookup(name
);
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
)
128 if (!try_module_get(scsi_dh
->module
))
131 error
= scsi_dh
->attach(sdev
);
132 if (error
!= SCSI_DH_OK
) {
137 case SCSI_DH_RES_TEMP_UNAVAIL
:
140 case SCSI_DH_DEV_UNSUPP
:
149 sdev_printk(KERN_ERR
, sdev
, "%s: Attach failed (%d)\n",
150 scsi_dh
->name
, error
);
151 module_put(scsi_dh
->module
);
153 sdev
->handler
= scsi_dh
;
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
;
174 drv
= scsi_dh_find_driver(sdev
);
176 devinfo
= __scsi_dh_lookup(drv
);
178 * device_handler is optional, so ignore errors
179 * from scsi_dh_handler_attach()
182 (void)scsi_dh_handler_attach(sdev
, devinfo
);
185 void scsi_dh_release_device(struct scsi_device
*sdev
)
188 scsi_dh_handler_detach(sdev
);
192 * scsi_register_device_handler - register a device handler personality
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
))
203 if (!scsi_dh
->attach
|| !scsi_dh
->detach
)
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
);
214 EXPORT_SYMBOL_GPL(scsi_register_device_handler
);
217 * scsi_unregister_device_handler - register a device handler personality
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
))
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
);
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
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
);
264 err
= SCSI_DH_NOTCONN
;
265 if (sdev
->sdev_state
== SDEV_CANCEL
||
266 sdev
->sdev_state
== SDEV_DEL
)
269 err
= SCSI_DH_DEV_OFFLINED
;
270 if (sdev
->sdev_state
== SDEV_OFFLINE
)
273 if (sdev
->handler
->activate
)
274 err
= sdev
->handler
->activate(sdev
, fn
, data
);
277 put_device(&sdev
->sdev_gendev
);
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
);
306 if (sdev
->handler
&& sdev
->handler
->set_params
)
307 err
= sdev
->handler
->set_params(sdev
, params
);
308 put_device(&sdev
->sdev_gendev
);
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
;
325 sdev
= scsi_device_from_queue(q
);
329 scsi_dh
= scsi_dh_lookup(name
);
336 if (sdev
->handler
!= scsi_dh
)
341 err
= scsi_dh_handler_attach(sdev
, scsi_dh
);
344 put_device(&sdev
->sdev_gendev
);
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
);
368 handler_name
= kstrdup(sdev
->handler
->name
, gfp
);
369 put_device(&sdev
->sdev_gendev
);
372 EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name
);