1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/slab.h>
25#include <linux/module.h>
26#include <scsi/scsi_dh.h>
27#include "scsi_priv.h"
28
29static DEFINE_SPINLOCK(list_lock);
30static LIST_HEAD(scsi_dh_list);
31
32struct scsi_dh_blist {
33 const char *vendor;
34 const char *model;
35 const char *driver;
36};
37
38static const struct scsi_dh_blist scsi_dh_blist[] = {
39 {"DGC", "RAID", "emc" },
40 {"DGC", "DISK", "emc" },
41 {"DGC", "VRAID", "emc" },
42
43 {"COMPAQ", "MSA1000 VOLUME", "hp_sw" },
44 {"COMPAQ", "HSV110", "hp_sw" },
45 {"HP", "HSV100", "hp_sw"},
46 {"DEC", "HSG80", "hp_sw"},
47
48 {"IBM", "1722", "rdac", },
49 {"IBM", "1724", "rdac", },
50 {"IBM", "1726", "rdac", },
51 {"IBM", "1742", "rdac", },
52 {"IBM", "1745", "rdac", },
53 {"IBM", "1746", "rdac", },
54 {"IBM", "1813", "rdac", },
55 {"IBM", "1814", "rdac", },
56 {"IBM", "1815", "rdac", },
57 {"IBM", "1818", "rdac", },
58 {"IBM", "3526", "rdac", },
59 {"SGI", "TP9", "rdac", },
60 {"SGI", "IS", "rdac", },
61 {"STK", "OPENstorage D280", "rdac", },
62 {"STK", "FLEXLINE 380", "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
75static const char *
76scsi_dh_find_driver(struct scsi_device *sdev)
77{
78 const struct scsi_dh_blist *b;
79
80 if (scsi_device_tpgs(sdev))
81 return "alua";
82
83 for (b = scsi_dh_blist; b->vendor; b++) {
84 if (!strncmp(sdev->vendor, b->vendor, strlen(b->vendor)) &&
85 !strncmp(sdev->model, b->model, strlen(b->model))) {
86 return b->driver;
87 }
88 }
89 return NULL;
90}
91
92
93static struct scsi_device_handler *__scsi_dh_lookup(const char *name)
94{
95 struct scsi_device_handler *tmp, *found = NULL;
96
97 spin_lock(&list_lock);
98 list_for_each_entry(tmp, &scsi_dh_list, list) {
99 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
100 found = tmp;
101 break;
102 }
103 }
104 spin_unlock(&list_lock);
105 return found;
106}
107
108static struct scsi_device_handler *scsi_dh_lookup(const char *name)
109{
110 struct scsi_device_handler *dh;
111
112 dh = __scsi_dh_lookup(name);
113 if (!dh) {
114 request_module("scsi_dh_%s", name);
115 dh = __scsi_dh_lookup(name);
116 }
117
118 return dh;
119}
120
121
122
123
124
125
126static int scsi_dh_handler_attach(struct scsi_device *sdev,
127 struct scsi_device_handler *scsi_dh)
128{
129 int error;
130
131 if (!try_module_get(scsi_dh->module))
132 return -EINVAL;
133
134 error = scsi_dh->attach(sdev);
135 if (error) {
136 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%d)\n",
137 scsi_dh->name, error);
138 module_put(scsi_dh->module);
139 } else
140 sdev->handler = scsi_dh;
141
142 return error;
143}
144
145
146
147
148
149static void scsi_dh_handler_detach(struct scsi_device *sdev)
150{
151 sdev->handler->detach(sdev);
152 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", sdev->handler->name);
153 module_put(sdev->handler->module);
154}
155
156int scsi_dh_add_device(struct scsi_device *sdev)
157{
158 struct scsi_device_handler *devinfo = NULL;
159 const char *drv;
160 int err = 0;
161
162 drv = scsi_dh_find_driver(sdev);
163 if (drv)
164 devinfo = __scsi_dh_lookup(drv);
165 if (devinfo)
166 err = scsi_dh_handler_attach(sdev, devinfo);
167 return err;
168}
169
170void scsi_dh_release_device(struct scsi_device *sdev)
171{
172 if (sdev->handler)
173 scsi_dh_handler_detach(sdev);
174}
175
176
177
178
179
180
181
182
183int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
184{
185 if (__scsi_dh_lookup(scsi_dh->name))
186 return -EBUSY;
187
188 if (!scsi_dh->attach || !scsi_dh->detach)
189 return -EINVAL;
190
191 spin_lock(&list_lock);
192 list_add(&scsi_dh->list, &scsi_dh_list);
193 spin_unlock(&list_lock);
194
195 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
196
197 return SCSI_DH_OK;
198}
199EXPORT_SYMBOL_GPL(scsi_register_device_handler);
200
201
202
203
204
205
206
207
208int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
209{
210 if (!__scsi_dh_lookup(scsi_dh->name))
211 return -ENODEV;
212
213 spin_lock(&list_lock);
214 list_del(&scsi_dh->list);
215 spin_unlock(&list_lock);
216 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
217
218 return SCSI_DH_OK;
219}
220EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
236{
237 struct scsi_device *sdev;
238 int err = SCSI_DH_NOSYS;
239
240 sdev = scsi_device_from_queue(q);
241 if (!sdev) {
242 if (fn)
243 fn(data, err);
244 return err;
245 }
246
247 if (!sdev->handler)
248 goto out_fn;
249 err = SCSI_DH_NOTCONN;
250 if (sdev->sdev_state == SDEV_CANCEL ||
251 sdev->sdev_state == SDEV_DEL)
252 goto out_fn;
253
254 err = SCSI_DH_DEV_OFFLINED;
255 if (sdev->sdev_state == SDEV_OFFLINE)
256 goto out_fn;
257
258 if (sdev->handler->activate)
259 err = sdev->handler->activate(sdev, fn, data);
260
261out_put_device:
262 put_device(&sdev->sdev_gendev);
263 return err;
264
265out_fn:
266 if (fn)
267 fn(data, err);
268 goto out_put_device;
269}
270EXPORT_SYMBOL_GPL(scsi_dh_activate);
271
272
273
274
275
276
277
278
279
280
281
282int scsi_dh_set_params(struct request_queue *q, const char *params)
283{
284 struct scsi_device *sdev;
285 int err = -SCSI_DH_NOSYS;
286
287 sdev = scsi_device_from_queue(q);
288 if (!sdev)
289 return err;
290
291 if (sdev->handler && sdev->handler->set_params)
292 err = sdev->handler->set_params(sdev, params);
293 put_device(&sdev->sdev_gendev);
294 return err;
295}
296EXPORT_SYMBOL_GPL(scsi_dh_set_params);
297
298
299
300
301
302
303
304int scsi_dh_attach(struct request_queue *q, const char *name)
305{
306 struct scsi_device *sdev;
307 struct scsi_device_handler *scsi_dh;
308 int err = 0;
309
310 sdev = scsi_device_from_queue(q);
311 if (!sdev)
312 return -ENODEV;
313
314 scsi_dh = scsi_dh_lookup(name);
315 if (!scsi_dh) {
316 err = -EINVAL;
317 goto out_put_device;
318 }
319
320 if (sdev->handler) {
321 if (sdev->handler != scsi_dh)
322 err = -EBUSY;
323 goto out_put_device;
324 }
325
326 err = scsi_dh_handler_attach(sdev, scsi_dh);
327
328out_put_device:
329 put_device(&sdev->sdev_gendev);
330 return err;
331}
332EXPORT_SYMBOL_GPL(scsi_dh_attach);
333
334
335
336
337
338
339
340
341
342
343const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
344{
345 struct scsi_device *sdev;
346 const char *handler_name = NULL;
347
348 sdev = scsi_device_from_queue(q);
349 if (!sdev)
350 return NULL;
351
352 if (sdev->handler)
353 handler_name = kstrdup(sdev->handler->name, gfp);
354 put_device(&sdev->sdev_gendev);
355 return handler_name;
356}
357EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);
358