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>
13 #define KMSG_COMPONENT "tape"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 #include <linux/module.h>
18 #include <linux/blkdev.h>
19 #include <linux/interrupt.h>
20 #include <linux/buffer_head.h>
21 #include <linux/kernel.h>
23 #include <asm/debug.h>
25 #define TAPE_DBF_AREA tape_core_dbf
29 #define TAPEBLOCK_MAX_SEC 100
30 #define TAPEBLOCK_MIN_REQUEUE 3
33 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
35 * In 2.5/2.6 the block device request function is very likely to be called
36 * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
37 * just call any function that tries to allocate CCW requests from that con-
38 * text since it might sleep. There are two choices to work around this:
39 * a) do not allocate with kmalloc but use its own memory pool
40 * b) take requests from the queue outside that context, knowing that
41 * allocation might sleep
45 * file operation structure for tape block frontend
47 static int tapeblock_open(struct block_device *, fmode_t);
48 static int tapeblock_release(struct gendisk *, fmode_t);
49 static int tapeblock_ioctl(struct block_device *, fmode_t, unsigned int,
51 static int tapeblock_medium_changed(struct gendisk *);
52 static int tapeblock_revalidate_disk(struct gendisk *);
54 static const struct block_device_operations tapeblock_fops = {
56 .open = tapeblock_open,
57 .release = tapeblock_release,
58 .ioctl = tapeblock_ioctl,
59 .media_changed = tapeblock_medium_changed,
60 .revalidate_disk = tapeblock_revalidate_disk,
63 static int tapeblock_major = 0;
66 tapeblock_trigger_requeue(struct tape_device *device)
68 /* Protect against rescheduling. */
69 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
71 schedule_work(&device->blk_data.requeue_task);
75 * Post finished request.
78 __tapeblock_end_request(struct tape_request *ccw_req, void *data)
80 struct tape_device *device;
83 DBF_LH(6, "__tapeblock_end_request()\n");
85 device = ccw_req->device;
86 req = (struct request *) data;
87 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
89 /* Update position. */
90 device->blk_data.block_position =
91 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
93 /* We lost the position information due to an error. */
94 device->blk_data.block_position = -1;
95 device->discipline->free_bread(ccw_req);
96 if (!list_empty(&device->req_queue) ||
97 blk_peek_request(device->blk_data.request_queue))
98 tapeblock_trigger_requeue(device);
102 * Feed the tape device CCW queue with requests supplied in a list.
105 tapeblock_start_request(struct tape_device *device, struct request *req)
107 struct tape_request * ccw_req;
110 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
112 ccw_req = device->discipline->bread(device, req);
113 if (IS_ERR(ccw_req)) {
114 DBF_EVENT(1, "TBLOCK: bread failed\n");
115 blk_end_request_all(req, -EIO);
116 return PTR_ERR(ccw_req);
118 ccw_req->callback = __tapeblock_end_request;
119 ccw_req->callback_data = (void *) req;
120 ccw_req->retries = TAPEBLOCK_RETRIES;
122 rc = tape_do_io_async(device, ccw_req);
125 * Start/enqueueing failed. No retries in
128 blk_end_request_all(req, -EIO);
129 device->discipline->free_bread(ccw_req);
136 * Move requests from the block device request queue to the tape device ccw
140 tapeblock_requeue(struct work_struct *work) {
141 struct tape_blk_data * blkdat;
142 struct tape_device * device;
143 struct request_queue * queue;
145 struct request * req;
146 struct list_head * l;
149 blkdat = container_of(work, struct tape_blk_data, requeue_task);
150 device = blkdat->device;
154 spin_lock_irq(get_ccwdev_lock(device->cdev));
155 queue = device->blk_data.request_queue;
157 /* Count number of requests on ccw queue. */
159 list_for_each(l, &device->req_queue)
161 spin_unlock(get_ccwdev_lock(device->cdev));
163 spin_lock_irq(&device->blk_data.request_queue_lock);
165 !blk_queue_plugged(queue) &&
166 blk_peek_request(queue) &&
167 nr_queued < TAPEBLOCK_MIN_REQUEUE
169 req = blk_fetch_request(queue);
170 if (rq_data_dir(req) == WRITE) {
171 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
172 spin_unlock_irq(&device->blk_data.request_queue_lock);
173 blk_end_request_all(req, -EIO);
174 spin_lock_irq(&device->blk_data.request_queue_lock);
178 spin_unlock_irq(&device->blk_data.request_queue_lock);
179 rc = tapeblock_start_request(device, req);
180 spin_lock_irq(&device->blk_data.request_queue_lock);
182 spin_unlock_irq(&device->blk_data.request_queue_lock);
183 atomic_set(&device->blk_data.requeue_scheduled, 0);
187 * Tape request queue function. Called from ll_rw_blk.c
190 tapeblock_request_fn(struct request_queue *queue)
192 struct tape_device *device;
194 device = (struct tape_device *) queue->queuedata;
195 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
196 BUG_ON(device == NULL);
197 tapeblock_trigger_requeue(device);
201 * This function is called for every new tapedevice
204 tapeblock_setup_device(struct tape_device * device)
206 struct tape_blk_data * blkdat;
207 struct gendisk * disk;
210 blkdat = &device->blk_data;
211 blkdat->device = device;
212 spin_lock_init(&blkdat->request_queue_lock);
213 atomic_set(&blkdat->requeue_scheduled, 0);
215 blkdat->request_queue = blk_init_queue(
216 tapeblock_request_fn,
217 &blkdat->request_queue_lock
219 if (!blkdat->request_queue)
222 elevator_exit(blkdat->request_queue->elevator);
223 rc = elevator_init(blkdat->request_queue, "noop");
227 blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
228 blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
229 blk_queue_max_phys_segments(blkdat->request_queue, -1L);
230 blk_queue_max_hw_segments(blkdat->request_queue, -1L);
231 blk_queue_max_segment_size(blkdat->request_queue, -1L);
232 blk_queue_segment_boundary(blkdat->request_queue, -1L);
234 disk = alloc_disk(1);
240 disk->major = tapeblock_major;
241 disk->first_minor = device->first_minor;
242 disk->fops = &tapeblock_fops;
243 disk->private_data = tape_get_device(device);
244 disk->queue = blkdat->request_queue;
245 set_capacity(disk, 0);
246 sprintf(disk->disk_name, "btibm%d",
247 device->first_minor / TAPE_MINORS_PER_DEV);
250 blkdat->medium_changed = 1;
251 blkdat->request_queue->queuedata = tape_get_device(device);
255 tape_get_device(device);
256 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
261 blk_cleanup_queue(blkdat->request_queue);
262 blkdat->request_queue = NULL;
268 tapeblock_cleanup_device(struct tape_device *device)
270 flush_scheduled_work();
271 tape_put_device(device);
273 if (!device->blk_data.disk) {
277 del_gendisk(device->blk_data.disk);
278 device->blk_data.disk->private_data = NULL;
279 tape_put_device(device);
280 put_disk(device->blk_data.disk);
282 device->blk_data.disk = NULL;
284 device->blk_data.request_queue->queuedata = NULL;
285 tape_put_device(device);
287 blk_cleanup_queue(device->blk_data.request_queue);
288 device->blk_data.request_queue = NULL;
292 * Detect number of blocks of the tape.
293 * FIXME: can we extent this to detect the blocks size as well ?
296 tapeblock_revalidate_disk(struct gendisk *disk)
298 struct tape_device * device;
299 unsigned int nr_of_blks;
302 device = (struct tape_device *) disk->private_data;
305 if (!device->blk_data.medium_changed)
308 rc = tape_mtop(device, MTFSFM, 1);
312 rc = tape_mtop(device, MTTELL, 1);
316 pr_info("%s: Determining the size of the recorded area...\n",
317 dev_name(&device->cdev->dev));
318 DBF_LH(3, "Image file ends at %d\n", rc);
321 /* This will fail for the first file. Catch the error by checking the
323 tape_mtop(device, MTBSF, 1);
325 rc = tape_mtop(device, MTTELL, 1);
332 DBF_LH(3, "Image file starts at %d\n", rc);
336 pr_info("%s: The size of the recorded area is %i blocks\n",
337 dev_name(&device->cdev->dev), nr_of_blks);
338 set_capacity(device->blk_data.disk,
339 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
341 device->blk_data.block_position = 0;
342 device->blk_data.medium_changed = 0;
347 tapeblock_medium_changed(struct gendisk *disk)
349 struct tape_device *device;
351 device = (struct tape_device *) disk->private_data;
352 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
353 device, device->blk_data.medium_changed);
355 return device->blk_data.medium_changed;
359 * Block frontend tape device open function.
362 tapeblock_open(struct block_device *bdev, fmode_t mode)
364 struct gendisk * disk = bdev->bd_disk;
365 struct tape_device * device;
368 device = tape_get_device(disk->private_data);
370 if (device->required_tapemarks) {
371 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
372 pr_warning("%s: Opening the tape failed because of missing "
373 "end-of-file marks\n", dev_name(&device->cdev->dev));
378 rc = tape_open(device);
382 rc = tapeblock_revalidate_disk(disk);
387 * Note: The reference to <device> is hold until the release function
390 tape_state_set(device, TS_BLKUSE);
394 tape_release(device);
396 tape_put_device(device);
401 * Block frontend tape device release function.
403 * Note: One reference to the tape device was made by the open function. So
404 * we just get the pointer here and release the reference.
407 tapeblock_release(struct gendisk *disk, fmode_t mode)
409 struct tape_device *device = disk->private_data;
411 tape_state_set(device, TS_IN_USE);
412 tape_release(device);
413 tape_put_device(device);
419 * Support of some generic block device IOCTLs.
423 struct block_device * bdev,
425 unsigned int command,
430 struct gendisk *disk = bdev->bd_disk;
431 struct tape_device *device;
435 device = disk->private_data;
437 minor = MINOR(bdev->bd_dev);
439 DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command);
440 DBF_LH(6, "device = %d:%d\n", tapeblock_major, minor);
443 /* Refuse some IOCTL calls without complaining (mount). */
444 case 0x5310: /* CDROMMULTISESSION */
455 * Initialize block device frontend.
462 /* Register the tape major number to the kernel */
463 rc = register_blkdev(tapeblock_major, "tBLK");
467 if (tapeblock_major == 0)
468 tapeblock_major = rc;
473 * Deregister major for block device frontend
478 unregister_blkdev(tapeblock_major, "tBLK");