x86: mce: Move code in mce.c
[linux-2.6/linux-2.6-openrd.git] / drivers / scsi / osd / osd_uld.c
blob0bdef339090288d409e23557bf3c0956f400f241
1 /*
2 * osd_uld.c - OSD Upper Layer Driver
4 * A Linux driver module that registers as a SCSI ULD and probes
5 * for OSD type SCSI devices.
6 * It's main function is to export osd devices to in-kernel users like
7 * osdfs and pNFS-objects-LD. It also provides one ioctl for running
8 * in Kernel tests.
10 * Copyright (C) 2008 Panasas Inc. All rights reserved.
12 * Authors:
13 * Boaz Harrosh <bharrosh@panasas.com>
14 * Benny Halevy <bhalevy@panasas.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the Panasas company nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #include <linux/namei.h>
46 #include <linux/cdev.h>
47 #include <linux/fs.h>
48 #include <linux/module.h>
49 #include <linux/device.h>
50 #include <linux/idr.h>
51 #include <linux/major.h>
52 #include <linux/file.h>
54 #include <scsi/scsi.h>
55 #include <scsi/scsi_driver.h>
56 #include <scsi/scsi_device.h>
57 #include <scsi/scsi_ioctl.h>
59 #include <scsi/osd_initiator.h>
60 #include <scsi/osd_sec.h>
62 #include "osd_debug.h"
64 #ifndef TYPE_OSD
65 # define TYPE_OSD 0x11
66 #endif
68 #ifndef SCSI_OSD_MAJOR
69 # define SCSI_OSD_MAJOR 260
70 #endif
71 #define SCSI_OSD_MAX_MINOR 64
73 static const char osd_name[] = "osd";
74 static const char *osd_version_string = "open-osd 0.1.0";
75 const char osd_symlink[] = "scsi_osd";
77 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
78 MODULE_DESCRIPTION("open-osd Upper-Layer-Driver osd.ko");
79 MODULE_LICENSE("GPL");
80 MODULE_ALIAS_CHARDEV_MAJOR(SCSI_OSD_MAJOR);
81 MODULE_ALIAS_SCSI_DEVICE(TYPE_OSD);
83 struct osd_uld_device {
84 int minor;
85 struct kref kref;
86 struct cdev cdev;
87 struct osd_dev od;
88 struct gendisk *disk;
89 struct device *class_member;
92 static void __uld_get(struct osd_uld_device *oud);
93 static void __uld_put(struct osd_uld_device *oud);
96 * Char Device operations
99 static int osd_uld_open(struct inode *inode, struct file *file)
101 struct osd_uld_device *oud = container_of(inode->i_cdev,
102 struct osd_uld_device, cdev);
104 __uld_get(oud);
105 /* cache osd_uld_device on file handle */
106 file->private_data = oud;
107 OSD_DEBUG("osd_uld_open %p\n", oud);
108 return 0;
111 static int osd_uld_release(struct inode *inode, struct file *file)
113 struct osd_uld_device *oud = file->private_data;
115 OSD_DEBUG("osd_uld_release %p\n", file->private_data);
116 file->private_data = NULL;
117 __uld_put(oud);
118 return 0;
121 /* FIXME: Only one vector for now */
122 unsigned g_test_ioctl;
123 do_test_fn *g_do_test;
125 int osduld_register_test(unsigned ioctl, do_test_fn *do_test)
127 if (g_test_ioctl)
128 return -EINVAL;
130 g_test_ioctl = ioctl;
131 g_do_test = do_test;
132 return 0;
134 EXPORT_SYMBOL(osduld_register_test);
136 void osduld_unregister_test(unsigned ioctl)
138 if (ioctl == g_test_ioctl) {
139 g_test_ioctl = 0;
140 g_do_test = NULL;
143 EXPORT_SYMBOL(osduld_unregister_test);
145 static do_test_fn *_find_ioctl(unsigned cmd)
147 if (g_test_ioctl == cmd)
148 return g_do_test;
149 else
150 return NULL;
153 static long osd_uld_ioctl(struct file *file, unsigned int cmd,
154 unsigned long arg)
156 struct osd_uld_device *oud = file->private_data;
157 int ret;
158 do_test_fn *do_test;
160 do_test = _find_ioctl(cmd);
161 if (do_test)
162 ret = do_test(&oud->od, cmd, arg);
163 else {
164 OSD_ERR("Unknown ioctl %d: osd_uld_device=%p\n", cmd, oud);
165 ret = -ENOIOCTLCMD;
167 return ret;
170 static const struct file_operations osd_fops = {
171 .owner = THIS_MODULE,
172 .open = osd_uld_open,
173 .release = osd_uld_release,
174 .unlocked_ioctl = osd_uld_ioctl,
177 struct osd_dev *osduld_path_lookup(const char *name)
179 struct osd_uld_device *oud;
180 struct osd_dev *od;
181 struct file *file;
182 int error;
184 if (!name || !*name) {
185 OSD_ERR("Mount with !path || !*path\n");
186 return ERR_PTR(-EINVAL);
189 od = kzalloc(sizeof(*od), GFP_KERNEL);
190 if (!od)
191 return ERR_PTR(-ENOMEM);
193 file = filp_open(name, O_RDWR, 0);
194 if (IS_ERR(file)) {
195 error = PTR_ERR(file);
196 goto free_od;
199 if (file->f_op != &osd_fops){
200 error = -EINVAL;
201 goto close_file;
204 oud = file->private_data;
206 *od = oud->od;
207 od->file = file;
209 return od;
211 close_file:
212 fput(file);
213 free_od:
214 kfree(od);
215 return ERR_PTR(error);
217 EXPORT_SYMBOL(osduld_path_lookup);
219 void osduld_put_device(struct osd_dev *od)
222 if (od && !IS_ERR(od)) {
223 struct osd_uld_device *oud = od->file->private_data;
225 BUG_ON(od->scsi_device != oud->od.scsi_device);
227 fput(od->file);
228 kfree(od);
231 EXPORT_SYMBOL(osduld_put_device);
234 * Scsi Device operations
237 static int __detect_osd(struct osd_uld_device *oud)
239 struct scsi_device *scsi_device = oud->od.scsi_device;
240 char caps[OSD_CAP_LEN];
241 int error;
243 /* sending a test_unit_ready as first command seems to be needed
244 * by some targets
246 OSD_DEBUG("start scsi_test_unit_ready %p %p %p\n",
247 oud, scsi_device, scsi_device->request_queue);
248 error = scsi_test_unit_ready(scsi_device, 10*HZ, 5, NULL);
249 if (error)
250 OSD_ERR("warning: scsi_test_unit_ready failed\n");
252 osd_sec_init_nosec_doall_caps(caps, &osd_root_object, false, true);
253 if (osd_auto_detect_ver(&oud->od, caps))
254 return -ENODEV;
256 return 0;
259 static struct class *osd_sysfs_class;
260 static DEFINE_IDA(osd_minor_ida);
262 static int osd_probe(struct device *dev)
264 struct scsi_device *scsi_device = to_scsi_device(dev);
265 struct gendisk *disk;
266 struct osd_uld_device *oud;
267 int minor;
268 int error;
270 if (scsi_device->type != TYPE_OSD)
271 return -ENODEV;
273 do {
274 if (!ida_pre_get(&osd_minor_ida, GFP_KERNEL))
275 return -ENODEV;
277 error = ida_get_new(&osd_minor_ida, &minor);
278 } while (error == -EAGAIN);
280 if (error)
281 return error;
282 if (minor >= SCSI_OSD_MAX_MINOR) {
283 error = -EBUSY;
284 goto err_retract_minor;
287 error = -ENOMEM;
288 oud = kzalloc(sizeof(*oud), GFP_KERNEL);
289 if (NULL == oud)
290 goto err_retract_minor;
292 kref_init(&oud->kref);
293 dev_set_drvdata(dev, oud);
294 oud->minor = minor;
296 /* allocate a disk and set it up */
297 /* FIXME: do we need this since sg has already done that */
298 disk = alloc_disk(1);
299 if (!disk) {
300 OSD_ERR("alloc_disk failed\n");
301 goto err_free_osd;
303 disk->major = SCSI_OSD_MAJOR;
304 disk->first_minor = oud->minor;
305 sprintf(disk->disk_name, "osd%d", oud->minor);
306 oud->disk = disk;
308 /* hold one more reference to the scsi_device that will get released
309 * in __release, in case a logout is happening while fs is mounted
311 scsi_device_get(scsi_device);
312 osd_dev_init(&oud->od, scsi_device);
314 /* Detect the OSD Version */
315 error = __detect_osd(oud);
316 if (error) {
317 OSD_ERR("osd detection failed, non-compatible OSD device\n");
318 goto err_put_disk;
321 /* init the char-device for communication with user-mode */
322 cdev_init(&oud->cdev, &osd_fops);
323 oud->cdev.owner = THIS_MODULE;
324 error = cdev_add(&oud->cdev,
325 MKDEV(SCSI_OSD_MAJOR, oud->minor), 1);
326 if (error) {
327 OSD_ERR("cdev_add failed\n");
328 goto err_put_disk;
330 kobject_get(&oud->cdev.kobj); /* 2nd ref see osd_remove() */
332 /* class_member */
333 oud->class_member = device_create(osd_sysfs_class, dev,
334 MKDEV(SCSI_OSD_MAJOR, oud->minor), "%s", disk->disk_name);
335 if (IS_ERR(oud->class_member)) {
336 OSD_ERR("class_device_create failed\n");
337 error = PTR_ERR(oud->class_member);
338 goto err_put_cdev;
341 dev_set_drvdata(oud->class_member, oud);
343 OSD_INFO("osd_probe %s\n", disk->disk_name);
344 return 0;
346 err_put_cdev:
347 cdev_del(&oud->cdev);
348 err_put_disk:
349 scsi_device_put(scsi_device);
350 put_disk(disk);
351 err_free_osd:
352 dev_set_drvdata(dev, NULL);
353 kfree(oud);
354 err_retract_minor:
355 ida_remove(&osd_minor_ida, minor);
356 return error;
359 static int osd_remove(struct device *dev)
361 struct scsi_device *scsi_device = to_scsi_device(dev);
362 struct osd_uld_device *oud = dev_get_drvdata(dev);
364 if (!oud || (oud->od.scsi_device != scsi_device)) {
365 OSD_ERR("Half cooked osd-device %p,%p || %p!=%p",
366 dev, oud, oud ? oud->od.scsi_device : NULL,
367 scsi_device);
370 if (oud->class_member)
371 device_destroy(osd_sysfs_class,
372 MKDEV(SCSI_OSD_MAJOR, oud->minor));
374 /* We have 2 references to the cdev. One is released here
375 * and also takes down the /dev/osdX mapping. The second
376 * Will be released in __remove() after all users have released
377 * the osd_uld_device.
379 if (oud->cdev.owner)
380 cdev_del(&oud->cdev);
382 __uld_put(oud);
383 return 0;
386 static void __remove(struct kref *kref)
388 struct osd_uld_device *oud = container_of(kref,
389 struct osd_uld_device, kref);
390 struct scsi_device *scsi_device = oud->od.scsi_device;
392 /* now let delete the char_dev */
393 kobject_put(&oud->cdev.kobj);
395 osd_dev_fini(&oud->od);
396 scsi_device_put(scsi_device);
398 OSD_INFO("osd_remove %s\n",
399 oud->disk ? oud->disk->disk_name : NULL);
401 if (oud->disk)
402 put_disk(oud->disk);
404 ida_remove(&osd_minor_ida, oud->minor);
405 kfree(oud);
408 static void __uld_get(struct osd_uld_device *oud)
410 kref_get(&oud->kref);
413 static void __uld_put(struct osd_uld_device *oud)
415 kref_put(&oud->kref, __remove);
419 * Global driver and scsi registration
422 static struct scsi_driver osd_driver = {
423 .owner = THIS_MODULE,
424 .gendrv = {
425 .name = osd_name,
426 .probe = osd_probe,
427 .remove = osd_remove,
431 static int __init osd_uld_init(void)
433 int err;
435 osd_sysfs_class = class_create(THIS_MODULE, osd_symlink);
436 if (IS_ERR(osd_sysfs_class)) {
437 OSD_ERR("Unable to register sysfs class => %ld\n",
438 PTR_ERR(osd_sysfs_class));
439 return PTR_ERR(osd_sysfs_class);
442 err = register_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0),
443 SCSI_OSD_MAX_MINOR, osd_name);
444 if (err) {
445 OSD_ERR("Unable to register major %d for osd ULD => %d\n",
446 SCSI_OSD_MAJOR, err);
447 goto err_out;
450 err = scsi_register_driver(&osd_driver.gendrv);
451 if (err) {
452 OSD_ERR("scsi_register_driver failed => %d\n", err);
453 goto err_out_chrdev;
456 OSD_INFO("LOADED %s\n", osd_version_string);
457 return 0;
459 err_out_chrdev:
460 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
461 err_out:
462 class_destroy(osd_sysfs_class);
463 return err;
466 static void __exit osd_uld_exit(void)
468 scsi_unregister_driver(&osd_driver.gendrv);
469 unregister_chrdev_region(MKDEV(SCSI_OSD_MAJOR, 0), SCSI_OSD_MAX_MINOR);
470 class_destroy(osd_sysfs_class);
471 OSD_INFO("UNLOADED %s\n", osd_version_string);
474 module_init(osd_uld_init);
475 module_exit(osd_uld_exit);