2 * drivers/s390/char/tape_block.c
3 * block device frontend for tape device driver
5 * S390 and zSeries version
6 * Copyright (C) 2001,2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
9 * Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Stefan Bader <shbader@de.ibm.com>
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/blkdev.h>
17 #include <linux/interrupt.h>
18 #include <linux/buffer_head.h>
20 #include <asm/debug.h>
24 #define PRINTK_HEADER "TAPE_BLOCK: "
26 #define TAPEBLOCK_MAX_SEC 100
27 #define TAPEBLOCK_MIN_REQUEUE 3
30 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
32 * In 2.5/2.6 the block device request function is very likely to be called
33 * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
34 * just call any function that tries to allocate CCW requests from that con-
35 * text since it might sleep. There are two choices to work around this:
36 * a) do not allocate with kmalloc but use its own memory pool
37 * b) take requests from the queue outside that context, knowing that
38 * allocation might sleep
42 * file operation structure for tape block frontend
44 static int tapeblock_open(struct inode *, struct file *);
45 static int tapeblock_release(struct inode *, struct file *);
46 static int tapeblock_ioctl(struct inode *, struct file *, unsigned int,
48 static int tapeblock_medium_changed(struct gendisk *);
49 static int tapeblock_revalidate_disk(struct gendisk *);
51 static struct block_device_operations tapeblock_fops = {
53 .open = tapeblock_open,
54 .release = tapeblock_release,
55 .ioctl = tapeblock_ioctl,
56 .media_changed = tapeblock_medium_changed,
57 .revalidate_disk = tapeblock_revalidate_disk,
60 static int tapeblock_major = 0;
63 tapeblock_trigger_requeue(struct tape_device *device)
65 /* Protect against rescheduling. */
66 if (atomic_compare_and_swap(0, 1, &device->blk_data.requeue_scheduled))
68 schedule_work(&device->blk_data.requeue_task);
72 * Post finished request.
75 tapeblock_end_request(struct request *req, int uptodate)
77 if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
79 end_that_request_last(req);
83 __tapeblock_end_request(struct tape_request *ccw_req, void *data)
85 struct tape_device *device;
88 DBF_LH(6, "__tapeblock_end_request()\n");
90 device = ccw_req->device;
91 req = (struct request *) data;
92 tapeblock_end_request(req, ccw_req->rc == 0);
94 /* Update position. */
95 device->blk_data.block_position =
96 (req->sector + req->nr_sectors) >> TAPEBLOCK_HSEC_S2B;
98 /* We lost the position information due to an error. */
99 device->blk_data.block_position = -1;
100 device->discipline->free_bread(ccw_req);
101 if (!list_empty(&device->req_queue) ||
102 elv_next_request(device->blk_data.request_queue))
103 tapeblock_trigger_requeue(device);
107 * Feed the tape device CCW queue with requests supplied in a list.
110 tapeblock_start_request(struct tape_device *device, struct request *req)
112 struct tape_request * ccw_req;
115 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
117 ccw_req = device->discipline->bread(device, req);
118 if (IS_ERR(ccw_req)) {
119 DBF_EVENT(1, "TBLOCK: bread failed\n");
120 tapeblock_end_request(req, 0);
121 return PTR_ERR(ccw_req);
123 ccw_req->callback = __tapeblock_end_request;
124 ccw_req->callback_data = (void *) req;
125 ccw_req->retries = TAPEBLOCK_RETRIES;
127 rc = tape_do_io_async(device, ccw_req);
130 * Start/enqueueing failed. No retries in
133 tapeblock_end_request(req, 0);
134 device->discipline->free_bread(ccw_req);
141 * Move requests from the block device request queue to the tape device ccw
145 tapeblock_requeue(void *data) {
146 struct tape_device * device;
147 request_queue_t * queue;
149 struct request * req;
150 struct list_head * l;
153 device = (struct tape_device *) data;
157 spin_lock_irq(get_ccwdev_lock(device->cdev));
158 queue = device->blk_data.request_queue;
160 /* Count number of requests on ccw queue. */
162 list_for_each(l, &device->req_queue)
164 spin_unlock(get_ccwdev_lock(device->cdev));
166 spin_lock(&device->blk_data.request_queue_lock);
168 !blk_queue_plugged(queue) &&
169 elv_next_request(queue) &&
170 nr_queued < TAPEBLOCK_MIN_REQUEUE
172 req = elv_next_request(queue);
173 if (rq_data_dir(req) == WRITE) {
174 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
175 blkdev_dequeue_request(req);
176 tapeblock_end_request(req, 0);
179 spin_unlock_irq(&device->blk_data.request_queue_lock);
180 rc = tapeblock_start_request(device, req);
181 spin_lock_irq(&device->blk_data.request_queue_lock);
182 blkdev_dequeue_request(req);
185 spin_unlock_irq(&device->blk_data.request_queue_lock);
186 atomic_set(&device->blk_data.requeue_scheduled, 0);
190 * Tape request queue function. Called from ll_rw_blk.c
193 tapeblock_request_fn(request_queue_t *queue)
195 struct tape_device *device;
197 device = (struct tape_device *) queue->queuedata;
198 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
202 tapeblock_trigger_requeue(device);
206 * This function is called for every new tapedevice
209 tapeblock_setup_device(struct tape_device * device)
211 struct tape_blk_data * blkdat;
212 struct gendisk * disk;
215 blkdat = &device->blk_data;
216 spin_lock_init(&blkdat->request_queue_lock);
217 atomic_set(&blkdat->requeue_scheduled, 0);
219 blkdat->request_queue = blk_init_queue(
220 tapeblock_request_fn,
221 &blkdat->request_queue_lock
223 if (!blkdat->request_queue)
226 elevator_exit(blkdat->request_queue);
227 rc = elevator_init(blkdat->request_queue, &elevator_noop);
231 blk_queue_hardsect_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
232 blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
233 blk_queue_max_phys_segments(blkdat->request_queue, -1L);
234 blk_queue_max_hw_segments(blkdat->request_queue, -1L);
235 blk_queue_max_segment_size(blkdat->request_queue, -1L);
236 blk_queue_segment_boundary(blkdat->request_queue, -1L);
238 disk = alloc_disk(1);
244 disk->major = tapeblock_major;
245 disk->first_minor = device->first_minor;
246 disk->fops = &tapeblock_fops;
247 disk->private_data = tape_get_device_reference(device);
248 disk->queue = blkdat->request_queue;
249 set_capacity(disk, 0);
250 sprintf(disk->disk_name, "btibm%d",
251 device->first_minor / TAPE_MINORS_PER_DEV);
254 blkdat->medium_changed = 1;
255 blkdat->request_queue->queuedata = tape_get_device_reference(device);
259 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue,
260 tape_get_device_reference(device));
265 blk_cleanup_queue(blkdat->request_queue);
266 blkdat->request_queue = NULL;
272 tapeblock_cleanup_device(struct tape_device *device)
274 flush_scheduled_work();
275 device->blk_data.requeue_task.data = tape_put_device(device);
277 if (!device->blk_data.disk) {
278 PRINT_ERR("(%s): No gendisk to clean up!\n",
279 device->cdev->dev.bus_id);
283 del_gendisk(device->blk_data.disk);
284 device->blk_data.disk->private_data =
285 tape_put_device(device->blk_data.disk->private_data);
286 put_disk(device->blk_data.disk);
288 device->blk_data.disk = NULL;
290 device->blk_data.request_queue->queuedata = tape_put_device(device);
292 blk_cleanup_queue(device->blk_data.request_queue);
293 device->blk_data.request_queue = NULL;
297 * Detect number of blocks of the tape.
298 * FIXME: can we extent this to detect the blocks size as well ?
301 tapeblock_revalidate_disk(struct gendisk *disk)
303 struct tape_device * device;
304 unsigned int nr_of_blks;
307 device = (struct tape_device *) disk->private_data;
311 if (!device->blk_data.medium_changed)
314 PRINT_INFO("Detecting media size...\n");
315 rc = tape_mtop(device, MTFSFM, 1);
319 rc = tape_mtop(device, MTTELL, 1);
323 DBF_LH(3, "Image file ends at %d\n", rc);
326 /* This will fail for the first file. Catch the error by checking the
328 tape_mtop(device, MTBSF, 1);
330 rc = tape_mtop(device, MTTELL, 1);
337 DBF_LH(3, "Image file starts at %d\n", rc);
341 PRINT_INFO("Found %i blocks on media\n", nr_of_blks);
342 set_capacity(device->blk_data.disk,
343 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
345 device->blk_data.block_position = 0;
346 device->blk_data.medium_changed = 0;
351 tapeblock_medium_changed(struct gendisk *disk)
353 struct tape_device *device;
355 device = (struct tape_device *) disk->private_data;
356 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
357 device, device->blk_data.medium_changed);
359 return device->blk_data.medium_changed;
363 * Block frontend tape device open function.
366 tapeblock_open(struct inode *inode, struct file *filp)
368 struct gendisk * disk;
369 struct tape_device * device;
372 disk = inode->i_bdev->bd_disk;
373 device = tape_get_device_reference(disk->private_data);
375 if (device->required_tapemarks) {
376 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
377 PRINT_ERR("TBLOCK: Refusing to open tape with missing"
378 " end of file marks.\n");
383 rc = tape_open(device);
387 rc = tapeblock_revalidate_disk(disk);
392 * Note: The reference to <device> is hold until the release function
395 tape_state_set(device, TS_BLKUSE);
399 tape_release(device);
401 tape_put_device(device);
406 * Block frontend tape device release function.
408 * Note: One reference to the tape device was made by the open function. So
409 * we just get the pointer here and release the reference.
412 tapeblock_release(struct inode *inode, struct file *filp)
414 struct gendisk *disk = inode->i_bdev->bd_disk;
415 struct tape_device *device = disk->private_data;
417 tape_state_set(device, TS_IN_USE);
418 tape_release(device);
419 tape_put_device(device);
425 * Support of some generic block device IOCTLs.
429 struct inode * inode,
431 unsigned int command,
436 struct gendisk *disk = inode->i_bdev->bd_disk;
437 struct tape_device *device = disk->private_data;
440 disk = inode->i_bdev->bd_disk;
443 device = disk->private_data;
446 minor = iminor(inode);
448 DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command);
449 DBF_LH(6, "device = %d:%d\n", tapeblock_major, minor);
452 /* Refuse some IOCTL calls without complaining (mount). */
453 case 0x5310: /* CDROMMULTISESSION */
457 PRINT_WARN("invalid ioctl 0x%x\n", command);
465 * Initialize block device frontend.
472 /* Register the tape major number to the kernel */
473 rc = register_blkdev(tapeblock_major, "tBLK");
477 if (tapeblock_major == 0)
478 tapeblock_major = rc;
479 PRINT_INFO("tape gets major %d for block device\n", tapeblock_major);
484 * Deregister major for block device frontend
489 unregister_blkdev(tapeblock_major, "tBLK");