Linux-libre 4.4.148-gnu
[librecmc/linux-libre.git] / drivers / scsi / scsi_dh.c
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
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <scsi/scsi_dh.h>
27 #include "scsi_priv.h"
28
29 static DEFINE_SPINLOCK(list_lock);
30 static LIST_HEAD(scsi_dh_list);
31
32 struct scsi_dh_blist {
33         const char *vendor;
34         const char *model;
35         const char *driver;
36 };
37
38 static const struct scsi_dh_blist scsi_dh_blist[] = {
39         {"DGC", "RAID",                 "clariion" },
40         {"DGC", "DISK",                 "clariion" },
41         {"DGC", "VRAID",                "clariion" },
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         {"IBM", "3542",                 "rdac", },
60         {"IBM", "3552",                 "rdac", },
61         {"SGI", "TP9300",               "rdac", },
62         {"SGI", "TP9400",               "rdac", },
63         {"SGI", "TP9500",               "rdac", },
64         {"SGI", "TP9700",               "rdac", },
65         {"SGI", "IS",                   "rdac", },
66         {"STK", "OPENstorage",          "rdac", },
67         {"STK", "FLEXLINE 380",         "rdac", },
68         {"STK", "BladeCtlr",            "rdac", },
69         {"SUN", "CSM",                  "rdac", },
70         {"SUN", "LCSM100",              "rdac", },
71         {"SUN", "STK6580_6780",         "rdac", },
72         {"SUN", "SUN_6180",             "rdac", },
73         {"SUN", "ArrayStorage",         "rdac", },
74         {"DELL", "MD3",                 "rdac", },
75         {"NETAPP", "INF-01-00",         "rdac", },
76         {"LSI", "INF-01-00",            "rdac", },
77         {"ENGENIO", "INF-01-00",        "rdac", },
78         {NULL, NULL,                    NULL },
79 };
80
81 static const char *
82 scsi_dh_find_driver(struct scsi_device *sdev)
83 {
84         const struct scsi_dh_blist *b;
85
86         if (scsi_device_tpgs(sdev))
87                 return "alua";
88
89         for (b = scsi_dh_blist; b->vendor; b++) {
90                 if (!strncmp(sdev->vendor, b->vendor, strlen(b->vendor)) &&
91                     !strncmp(sdev->model, b->model, strlen(b->model))) {
92                         return b->driver;
93                 }
94         }
95         return NULL;
96 }
97
98
99 static struct scsi_device_handler *__scsi_dh_lookup(const char *name)
100 {
101         struct scsi_device_handler *tmp, *found = NULL;
102
103         spin_lock(&list_lock);
104         list_for_each_entry(tmp, &scsi_dh_list, list) {
105                 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
106                         found = tmp;
107                         break;
108                 }
109         }
110         spin_unlock(&list_lock);
111         return found;
112 }
113
114 static struct scsi_device_handler *scsi_dh_lookup(const char *name)
115 {
116         struct scsi_device_handler *dh;
117
118         dh = __scsi_dh_lookup(name);
119         if (!dh) {
120                 request_module("scsi_dh_%s", name);
121                 dh = __scsi_dh_lookup(name);
122         }
123
124         return dh;
125 }
126
127 /*
128  * scsi_dh_handler_attach - Attach a device handler to a device
129  * @sdev - SCSI device the device handler should attach to
130  * @scsi_dh - The device handler to attach
131  */
132 static int scsi_dh_handler_attach(struct scsi_device *sdev,
133                                   struct scsi_device_handler *scsi_dh)
134 {
135         int error;
136
137         if (!try_module_get(scsi_dh->module))
138                 return -EINVAL;
139
140         error = scsi_dh->attach(sdev);
141         if (error) {
142                 sdev_printk(KERN_ERR, sdev, "%s: Attach failed (%d)\n",
143                             scsi_dh->name, error);
144                 module_put(scsi_dh->module);
145         } else
146                 sdev->handler = scsi_dh;
147
148         return error;
149 }
150
151 /*
152  * scsi_dh_handler_detach - Detach a device handler from a device
153  * @sdev - SCSI device the device handler should be detached from
154  */
155 static void scsi_dh_handler_detach(struct scsi_device *sdev)
156 {
157         sdev->handler->detach(sdev);
158         sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", sdev->handler->name);
159         module_put(sdev->handler->module);
160 }
161
162 /*
163  * Functions for sysfs attribute 'dh_state'
164  */
165 static ssize_t
166 store_dh_state(struct device *dev, struct device_attribute *attr,
167                const char *buf, size_t count)
168 {
169         struct scsi_device *sdev = to_scsi_device(dev);
170         struct scsi_device_handler *scsi_dh;
171         int err = -EINVAL;
172
173         if (sdev->sdev_state == SDEV_CANCEL ||
174             sdev->sdev_state == SDEV_DEL)
175                 return -ENODEV;
176
177         if (!sdev->handler) {
178                 /*
179                  * Attach to a device handler
180                  */
181                 scsi_dh = scsi_dh_lookup(buf);
182                 if (!scsi_dh)
183                         return err;
184                 err = scsi_dh_handler_attach(sdev, scsi_dh);
185         } else {
186                 if (!strncmp(buf, "detach", 6)) {
187                         /*
188                          * Detach from a device handler
189                          */
190                         sdev_printk(KERN_WARNING, sdev,
191                                     "can't detach handler %s.\n",
192                                     sdev->handler->name);
193                         err = -EINVAL;
194                 } else if (!strncmp(buf, "activate", 8)) {
195                         /*
196                          * Activate a device handler
197                          */
198                         if (sdev->handler->activate)
199                                 err = sdev->handler->activate(sdev, NULL, NULL);
200                         else
201                                 err = 0;
202                 }
203         }
204
205         return err<0?err:count;
206 }
207
208 static ssize_t
209 show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
210 {
211         struct scsi_device *sdev = to_scsi_device(dev);
212
213         if (!sdev->handler)
214                 return snprintf(buf, 20, "detached\n");
215
216         return snprintf(buf, 20, "%s\n", sdev->handler->name);
217 }
218
219 static struct device_attribute scsi_dh_state_attr =
220         __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
221                store_dh_state);
222
223 int scsi_dh_add_device(struct scsi_device *sdev)
224 {
225         struct scsi_device_handler *devinfo = NULL;
226         const char *drv;
227         int err;
228
229         err = device_create_file(&sdev->sdev_gendev, &scsi_dh_state_attr);
230         if (err)
231                 return err;
232
233         drv = scsi_dh_find_driver(sdev);
234         if (drv)
235                 devinfo = __scsi_dh_lookup(drv);
236         if (devinfo)
237                 err = scsi_dh_handler_attach(sdev, devinfo);
238         return err;
239 }
240
241 void scsi_dh_release_device(struct scsi_device *sdev)
242 {
243         if (sdev->handler)
244                 scsi_dh_handler_detach(sdev);
245 }
246
247 void scsi_dh_remove_device(struct scsi_device *sdev)
248 {
249         device_remove_file(&sdev->sdev_gendev, &scsi_dh_state_attr);
250 }
251
252 /*
253  * scsi_register_device_handler - register a device handler personality
254  *      module.
255  * @scsi_dh - device handler to be registered.
256  *
257  * Returns 0 on success, -EBUSY if handler already registered.
258  */
259 int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
260 {
261         if (__scsi_dh_lookup(scsi_dh->name))
262                 return -EBUSY;
263
264         if (!scsi_dh->attach || !scsi_dh->detach)
265                 return -EINVAL;
266
267         spin_lock(&list_lock);
268         list_add(&scsi_dh->list, &scsi_dh_list);
269         spin_unlock(&list_lock);
270
271         printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
272
273         return SCSI_DH_OK;
274 }
275 EXPORT_SYMBOL_GPL(scsi_register_device_handler);
276
277 /*
278  * scsi_unregister_device_handler - register a device handler personality
279  *      module.
280  * @scsi_dh - device handler to be unregistered.
281  *
282  * Returns 0 on success, -ENODEV if handler not registered.
283  */
284 int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
285 {
286         if (!__scsi_dh_lookup(scsi_dh->name))
287                 return -ENODEV;
288
289         spin_lock(&list_lock);
290         list_del(&scsi_dh->list);
291         spin_unlock(&list_lock);
292         printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
293
294         return SCSI_DH_OK;
295 }
296 EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
297
298 /*
299  * scsi_dh_activate - activate the path associated with the scsi_device
300  *      corresponding to the given request queue.
301  *     Returns immediately without waiting for activation to be completed.
302  * @q    - Request queue that is associated with the scsi_device to be
303  *         activated.
304  * @fn   - Function to be called upon completion of the activation.
305  *         Function fn is called with data (below) and the error code.
306  *         Function fn may be called from the same calling context. So,
307  *         do not hold the lock in the caller which may be needed in fn.
308  * @data - data passed to the function fn upon completion.
309  *
310  */
311 int scsi_dh_activate(struct request_queue *q, activate_complete fn, void *data)
312 {
313         struct scsi_device *sdev;
314         int err = SCSI_DH_NOSYS;
315
316         sdev = scsi_device_from_queue(q);
317         if (!sdev) {
318                 if (fn)
319                         fn(data, err);
320                 return err;
321         }
322
323         if (!sdev->handler)
324                 goto out_fn;
325         err = SCSI_DH_NOTCONN;
326         if (sdev->sdev_state == SDEV_CANCEL ||
327             sdev->sdev_state == SDEV_DEL)
328                 goto out_fn;
329
330         err = SCSI_DH_DEV_OFFLINED;
331         if (sdev->sdev_state == SDEV_OFFLINE)
332                 goto out_fn;
333
334         if (sdev->handler->activate)
335                 err = sdev->handler->activate(sdev, fn, data);
336
337 out_put_device:
338         put_device(&sdev->sdev_gendev);
339         return err;
340
341 out_fn:
342         if (fn)
343                 fn(data, err);
344         goto out_put_device;
345 }
346 EXPORT_SYMBOL_GPL(scsi_dh_activate);
347
348 /*
349  * scsi_dh_set_params - set the parameters for the device as per the
350  *      string specified in params.
351  * @q - Request queue that is associated with the scsi_device for
352  *      which the parameters to be set.
353  * @params - parameters in the following format
354  *      "no_of_params\0param1\0param2\0param3\0...\0"
355  *      for example, string for 2 parameters with value 10 and 21
356  *      is specified as "2\010\021\0".
357  */
358 int scsi_dh_set_params(struct request_queue *q, const char *params)
359 {
360         struct scsi_device *sdev;
361         int err = -SCSI_DH_NOSYS;
362
363         sdev = scsi_device_from_queue(q);
364         if (!sdev)
365                 return err;
366
367         if (sdev->handler && sdev->handler->set_params)
368                 err = sdev->handler->set_params(sdev, params);
369         put_device(&sdev->sdev_gendev);
370         return err;
371 }
372 EXPORT_SYMBOL_GPL(scsi_dh_set_params);
373
374 /*
375  * scsi_dh_attach - Attach device handler
376  * @q - Request queue that is associated with the scsi_device
377  *      the handler should be attached to
378  * @name - name of the handler to attach
379  */
380 int scsi_dh_attach(struct request_queue *q, const char *name)
381 {
382         struct scsi_device *sdev;
383         struct scsi_device_handler *scsi_dh;
384         int err = 0;
385
386         sdev = scsi_device_from_queue(q);
387         if (!sdev)
388                 return -ENODEV;
389
390         scsi_dh = scsi_dh_lookup(name);
391         if (!scsi_dh) {
392                 err = -EINVAL;
393                 goto out_put_device;
394         }
395
396         if (sdev->handler) {
397                 if (sdev->handler != scsi_dh)
398                         err = -EBUSY;
399                 goto out_put_device;
400         }
401
402         err = scsi_dh_handler_attach(sdev, scsi_dh);
403
404 out_put_device:
405         put_device(&sdev->sdev_gendev);
406         return err;
407 }
408 EXPORT_SYMBOL_GPL(scsi_dh_attach);
409
410 /*
411  * scsi_dh_attached_handler_name - Get attached device handler's name
412  * @q - Request queue that is associated with the scsi_device
413  *      that may have a device handler attached
414  * @gfp - the GFP mask used in the kmalloc() call when allocating memory
415  *
416  * Returns name of attached handler, NULL if no handler is attached.
417  * Caller must take care to free the returned string.
418  */
419 const char *scsi_dh_attached_handler_name(struct request_queue *q, gfp_t gfp)
420 {
421         struct scsi_device *sdev;
422         const char *handler_name = NULL;
423
424         sdev = scsi_device_from_queue(q);
425         if (!sdev)
426                 return NULL;
427
428         if (sdev->handler)
429                 handler_name = kstrdup(sdev->handler->name, gfp);
430         put_device(&sdev->sdev_gendev);
431         return handler_name;
432 }
433 EXPORT_SYMBOL_GPL(scsi_dh_attached_handler_name);