blob: e90df9dd3e6525ed0db89c60525428b91ee329a3 [file] [log] [blame]
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -07001/*
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 <scsi/scsi_dh.h>
25#include "../scsi_priv.h"
26
27static DEFINE_SPINLOCK(list_lock);
28static LIST_HEAD(scsi_dh_list);
29
30static struct scsi_device_handler *get_device_handler(const char *name)
31{
32 struct scsi_device_handler *tmp, *found = NULL;
33
34 spin_lock(&list_lock);
35 list_for_each_entry(tmp, &scsi_dh_list, list) {
Hannes Reinecke765cbc62008-07-17 16:52:51 -070036 if (!strncmp(tmp->name, name, strlen(tmp->name))) {
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -070037 found = tmp;
38 break;
39 }
40 }
41 spin_unlock(&list_lock);
42 return found;
43}
44
Hannes Reinecke765cbc62008-07-17 16:52:51 -070045static int device_handler_match(struct scsi_device_handler *tmp,
46 struct scsi_device *sdev)
47{
48 int i;
49
50 for(i = 0; tmp->devlist[i].vendor; i++) {
51 if (!strncmp(sdev->vendor, tmp->devlist[i].vendor,
52 strlen(tmp->devlist[i].vendor)) &&
53 !strncmp(sdev->model, tmp->devlist[i].model,
54 strlen(tmp->devlist[i].model))) {
55 return 1;
56 }
57 }
58
59 return 0;
60}
61
62/*
63 * scsi_dh_handler_attach - Attach a device handler to a device
64 * @sdev - SCSI device the device handler should attach to
65 * @scsi_dh - The device handler to attach
66 */
67static int scsi_dh_handler_attach(struct scsi_device *sdev,
68 struct scsi_device_handler *scsi_dh)
69{
70 int err = 0;
71
72 if (sdev->scsi_dh_data) {
73 if (sdev->scsi_dh_data->scsi_dh != scsi_dh)
74 err = -EBUSY;
75 } else if (scsi_dh->attach)
76 err = scsi_dh->attach(sdev);
77
78 return err;
79}
80
81/*
82 * scsi_dh_handler_detach - Detach a device handler from a device
83 * @sdev - SCSI device the device handler should be detached from
84 * @scsi_dh - Device handler to be detached
85 *
86 * Detach from a device handler. If a device handler is specified,
Hannes Reinecke4c05ae52008-07-17 16:52:57 -070087 * only detach if the currently attached handler matches @scsi_dh.
Hannes Reinecke765cbc62008-07-17 16:52:51 -070088 */
89static void scsi_dh_handler_detach(struct scsi_device *sdev,
90 struct scsi_device_handler *scsi_dh)
91{
92 if (!sdev->scsi_dh_data)
93 return;
94
95 if (scsi_dh && scsi_dh != sdev->scsi_dh_data->scsi_dh)
96 return;
97
98 if (!scsi_dh)
99 scsi_dh = sdev->scsi_dh_data->scsi_dh;
100
101 if (scsi_dh && scsi_dh->detach)
102 scsi_dh->detach(sdev);
103}
104
105/*
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700106 * Functions for sysfs attribute 'dh_state'
107 */
108static ssize_t
109store_dh_state(struct device *dev, struct device_attribute *attr,
110 const char *buf, size_t count)
111{
112 struct scsi_device *sdev = to_scsi_device(dev);
113 struct scsi_device_handler *scsi_dh;
114 int err = -EINVAL;
115
116 if (!sdev->scsi_dh_data) {
117 /*
118 * Attach to a device handler
119 */
120 if (!(scsi_dh = get_device_handler(buf)))
121 return err;
122 err = scsi_dh_handler_attach(sdev, scsi_dh);
123 } else {
124 scsi_dh = sdev->scsi_dh_data->scsi_dh;
125 if (!strncmp(buf, "detach", 6)) {
126 /*
127 * Detach from a device handler
128 */
129 scsi_dh_handler_detach(sdev, scsi_dh);
130 err = 0;
131 } else if (!strncmp(buf, "activate", 8)) {
132 /*
133 * Activate a device handler
134 */
135 if (scsi_dh->activate)
136 err = scsi_dh->activate(sdev);
137 else
138 err = 0;
139 }
140 }
141
142 return err<0?err:count;
143}
144
145static ssize_t
146show_dh_state(struct device *dev, struct device_attribute *attr, char *buf)
147{
148 struct scsi_device *sdev = to_scsi_device(dev);
149
150 if (!sdev->scsi_dh_data)
151 return snprintf(buf, 20, "detached\n");
152
153 return snprintf(buf, 20, "%s\n", sdev->scsi_dh_data->scsi_dh->name);
154}
155
156static struct device_attribute scsi_dh_state_attr =
157 __ATTR(dh_state, S_IRUGO | S_IWUSR, show_dh_state,
158 store_dh_state);
159
160/*
161 * scsi_dh_sysfs_attr_add - Callback for scsi_init_dh
162 */
163static int scsi_dh_sysfs_attr_add(struct device *dev, void *data)
164{
165 struct scsi_device *sdev;
166 int err;
167
168 if (!scsi_is_sdev_device(dev))
169 return 0;
170
171 sdev = to_scsi_device(dev);
172
173 err = device_create_file(&sdev->sdev_gendev,
174 &scsi_dh_state_attr);
175
176 return 0;
177}
178
179/*
180 * scsi_dh_sysfs_attr_remove - Callback for scsi_exit_dh
181 */
182static int scsi_dh_sysfs_attr_remove(struct device *dev, void *data)
183{
184 struct scsi_device *sdev;
185
186 if (!scsi_is_sdev_device(dev))
187 return 0;
188
189 sdev = to_scsi_device(dev);
190
191 device_remove_file(&sdev->sdev_gendev,
192 &scsi_dh_state_attr);
193
194 return 0;
195}
196
197/*
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700198 * scsi_dh_notifier - notifier chain callback
199 */
200static int scsi_dh_notifier(struct notifier_block *nb,
201 unsigned long action, void *data)
202{
203 struct device *dev = data;
204 struct scsi_device *sdev;
205 int err = 0;
206 struct scsi_device_handler *tmp, *devinfo = NULL;
207
208 if (!scsi_is_sdev_device(dev))
209 return 0;
210
211 sdev = to_scsi_device(dev);
212
213 spin_lock(&list_lock);
214 list_for_each_entry(tmp, &scsi_dh_list, list) {
215 if (device_handler_match(tmp, sdev)) {
216 devinfo = tmp;
217 break;
218 }
219 }
220 spin_unlock(&list_lock);
221
222 if (!devinfo)
223 goto out;
224
225 if (action == BUS_NOTIFY_ADD_DEVICE) {
226 err = scsi_dh_handler_attach(sdev, devinfo);
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700227 if (!err)
228 err = device_create_file(dev, &scsi_dh_state_attr);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700229 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700230 device_remove_file(dev, &scsi_dh_state_attr);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700231 scsi_dh_handler_detach(sdev, NULL);
232 }
233out:
234 return err;
235}
236
237/*
238 * scsi_dh_notifier_add - Callback for scsi_register_device_handler
239 */
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700240static int scsi_dh_notifier_add(struct device *dev, void *data)
241{
242 struct scsi_device_handler *scsi_dh = data;
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700243 struct scsi_device *sdev;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700244
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700245 if (!scsi_is_sdev_device(dev))
246 return 0;
247
248 if (!get_device(dev))
249 return 0;
250
251 sdev = to_scsi_device(dev);
252
253 if (device_handler_match(scsi_dh, sdev))
254 scsi_dh_handler_attach(sdev, scsi_dh);
255
256 put_device(dev);
257
258 return 0;
259}
260
261/*
262 * scsi_dh_notifier_remove - Callback for scsi_unregister_device_handler
263 */
264static int scsi_dh_notifier_remove(struct device *dev, void *data)
265{
266 struct scsi_device_handler *scsi_dh = data;
267 struct scsi_device *sdev;
268
269 if (!scsi_is_sdev_device(dev))
270 return 0;
271
272 if (!get_device(dev))
273 return 0;
274
275 sdev = to_scsi_device(dev);
276
277 scsi_dh_handler_detach(sdev, scsi_dh);
278
279 put_device(dev);
280
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700281 return 0;
282}
283
284/*
285 * scsi_register_device_handler - register a device handler personality
286 * module.
287 * @scsi_dh - device handler to be registered.
288 *
289 * Returns 0 on success, -EBUSY if handler already registered.
290 */
291int scsi_register_device_handler(struct scsi_device_handler *scsi_dh)
292{
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700293 if (get_device_handler(scsi_dh->name))
294 return -EBUSY;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700295
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700296 spin_lock(&list_lock);
297 list_add(&scsi_dh->list, &scsi_dh_list);
298 spin_unlock(&list_lock);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700299 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh, scsi_dh_notifier_add);
300 printk(KERN_INFO "%s: device handler registered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700301
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700302 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700303}
304EXPORT_SYMBOL_GPL(scsi_register_device_handler);
305
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700306/*
307 * scsi_unregister_device_handler - register a device handler personality
308 * module.
309 * @scsi_dh - device handler to be unregistered.
310 *
311 * Returns 0 on success, -ENODEV if handler not registered.
312 */
313int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh)
314{
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700315 if (!get_device_handler(scsi_dh->name))
316 return -ENODEV;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700317
318 bus_for_each_dev(&scsi_bus_type, NULL, scsi_dh,
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700319 scsi_dh_notifier_remove);
320
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700321 spin_lock(&list_lock);
322 list_del(&scsi_dh->list);
323 spin_unlock(&list_lock);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700324 printk(KERN_INFO "%s: device handler unregistered\n", scsi_dh->name);
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700325
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700326 return SCSI_DH_OK;
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700327}
328EXPORT_SYMBOL_GPL(scsi_unregister_device_handler);
329
330/*
331 * scsi_dh_activate - activate the path associated with the scsi_device
332 * corresponding to the given request queue.
333 * @q - Request queue that is associated with the scsi_device to be
334 * activated.
335 */
336int scsi_dh_activate(struct request_queue *q)
337{
338 int err = 0;
339 unsigned long flags;
340 struct scsi_device *sdev;
341 struct scsi_device_handler *scsi_dh = NULL;
342
343 spin_lock_irqsave(q->queue_lock, flags);
344 sdev = q->queuedata;
345 if (sdev && sdev->scsi_dh_data)
346 scsi_dh = sdev->scsi_dh_data->scsi_dh;
347 if (!scsi_dh || !get_device(&sdev->sdev_gendev))
348 err = SCSI_DH_NOSYS;
349 spin_unlock_irqrestore(q->queue_lock, flags);
350
351 if (err)
352 return err;
353
354 if (scsi_dh->activate)
355 err = scsi_dh->activate(sdev);
356 put_device(&sdev->sdev_gendev);
357 return err;
358}
359EXPORT_SYMBOL_GPL(scsi_dh_activate);
360
361/*
362 * scsi_dh_handler_exist - Return TRUE(1) if a device handler exists for
363 * the given name. FALSE(0) otherwise.
364 * @name - name of the device handler.
365 */
366int scsi_dh_handler_exist(const char *name)
367{
368 return (get_device_handler(name) != NULL);
369}
370EXPORT_SYMBOL_GPL(scsi_dh_handler_exist);
371
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700372static struct notifier_block scsi_dh_nb = {
373 .notifier_call = scsi_dh_notifier
374};
375
376static int __init scsi_dh_init(void)
377{
378 int r;
379
380 r = bus_register_notifier(&scsi_bus_type, &scsi_dh_nb);
381
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700382 if (!r)
383 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
384 scsi_dh_sysfs_attr_add);
385
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700386 return r;
387}
388
389static void __exit scsi_dh_exit(void)
390{
Hannes Reinecke4c05ae52008-07-17 16:52:57 -0700391 bus_for_each_dev(&scsi_bus_type, NULL, NULL,
392 scsi_dh_sysfs_attr_remove);
Hannes Reinecke765cbc62008-07-17 16:52:51 -0700393 bus_unregister_notifier(&scsi_bus_type, &scsi_dh_nb);
394}
395
396module_init(scsi_dh_init);
397module_exit(scsi_dh_exit);
398
Chandra Seetharamana6a8d9f2008-05-01 14:49:46 -0700399MODULE_DESCRIPTION("SCSI device handler");
400MODULE_AUTHOR("Chandra Seetharaman <sekharan@us.ibm.com>");
401MODULE_LICENSE("GPL");