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/mutex.h>
20 #include <linux/interrupt.h>
21 #include <linux/buffer_head.h>
22 #include <linux/kernel.h>
24 #include <asm/debug.h>
26 #define TAPE_DBF_AREA tape_core_dbf
30 #define TAPEBLOCK_MAX_SEC 100
31 #define TAPEBLOCK_MIN_REQUEUE 3
34 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
36 * In 2.5/2.6 the block device request function is very likely to be called
37 * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
38 * just call any function that tries to allocate CCW requests from that con-
39 * text since it might sleep. There are two choices to work around this:
40 * a) do not allocate with kmalloc but use its own memory pool
41 * b) take requests from the queue outside that context, knowing that
42 * allocation might sleep
46 * file operation structure for tape block frontend
48 static DEFINE_MUTEX(tape_block_mutex);
49 static int tapeblock_open(struct block_device *, fmode_t);
50 static int tapeblock_release(struct gendisk *, fmode_t);
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 .media_changed = tapeblock_medium_changed,
59 .revalidate_disk = tapeblock_revalidate_disk,
62 static int tapeblock_major = 0;
65 tapeblock_trigger_requeue(struct tape_device *device)
67 /* Protect against rescheduling. */
68 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
70 schedule_work(&device->blk_data.requeue_task);
74 * Post finished request.
77 __tapeblock_end_request(struct tape_request *ccw_req, void *data)
79 struct tape_device *device;
82 DBF_LH(6, "__tapeblock_end_request()\n");
84 device = ccw_req->device;
85 req = (struct request *) data;
86 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
88 /* Update position. */
89 device->blk_data.block_position =
90 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
92 /* We lost the position information due to an error. */
93 device->blk_data.block_position = -1;
94 device->discipline->free_bread(ccw_req);
95 if (!list_empty(&device->req_queue) ||
96 blk_peek_request(device->blk_data.request_queue))
97 tapeblock_trigger_requeue(device);
101 * Feed the tape device CCW queue with requests supplied in a list.
104 tapeblock_start_request(struct tape_device *device, struct request *req)
106 struct tape_request * ccw_req;
109 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
111 ccw_req = device->discipline->bread(device, req);
112 if (IS_ERR(ccw_req)) {
113 DBF_EVENT(1, "TBLOCK: bread failed\n");
114 blk_end_request_all(req, -EIO);
115 return PTR_ERR(ccw_req);
117 ccw_req->callback = __tapeblock_end_request;
118 ccw_req->callback_data = (void *) req;
119 ccw_req->retries = TAPEBLOCK_RETRIES;
121 rc = tape_do_io_async(device, ccw_req);
124 * Start/enqueueing failed. No retries in
127 blk_end_request_all(req, -EIO);
128 device->discipline->free_bread(ccw_req);
135 * Move requests from the block device request queue to the tape device ccw
139 tapeblock_requeue(struct work_struct *work) {
140 struct tape_blk_data * blkdat;
141 struct tape_device * device;
142 struct request_queue * queue;
144 struct request * req;
145 struct list_head * l;
148 blkdat = container_of(work, struct tape_blk_data, requeue_task);
149 device = blkdat->device;
153 spin_lock_irq(get_ccwdev_lock(device->cdev));
154 queue = device->blk_data.request_queue;
156 /* Count number of requests on ccw queue. */
158 list_for_each(l, &device->req_queue)
160 spin_unlock(get_ccwdev_lock(device->cdev));
162 spin_lock_irq(&device->blk_data.request_queue_lock);
164 !blk_queue_plugged(queue) &&
165 blk_peek_request(queue) &&
166 nr_queued < TAPEBLOCK_MIN_REQUEUE
168 req = blk_fetch_request(queue);
169 if (rq_data_dir(req) == WRITE) {
170 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
171 spin_unlock_irq(&device->blk_data.request_queue_lock);
172 blk_end_request_all(req, -EIO);
173 spin_lock_irq(&device->blk_data.request_queue_lock);
177 spin_unlock_irq(&device->blk_data.request_queue_lock);
178 rc = tapeblock_start_request(device, req);
179 spin_lock_irq(&device->blk_data.request_queue_lock);
181 spin_unlock_irq(&device->blk_data.request_queue_lock);
182 atomic_set(&device->blk_data.requeue_scheduled, 0);
186 * Tape request queue function. Called from ll_rw_blk.c
189 tapeblock_request_fn(struct request_queue *queue)
191 struct tape_device *device;
193 device = (struct tape_device *) queue->queuedata;
194 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
195 BUG_ON(device == NULL);
196 tapeblock_trigger_requeue(device);
200 * This function is called for every new tapedevice
203 tapeblock_setup_device(struct tape_device * device)
205 struct tape_blk_data * blkdat;
206 struct gendisk * disk;
209 blkdat = &device->blk_data;
210 blkdat->device = device;
211 spin_lock_init(&blkdat->request_queue_lock);
212 atomic_set(&blkdat->requeue_scheduled, 0);
214 blkdat->request_queue = blk_init_queue(
215 tapeblock_request_fn,
216 &blkdat->request_queue_lock
218 if (!blkdat->request_queue)
221 rc = elevator_change(blkdat->request_queue, "noop");
225 blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
226 blk_queue_max_hw_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
227 blk_queue_max_segments(blkdat->request_queue, -1L);
228 blk_queue_max_segment_size(blkdat->request_queue, -1L);
229 blk_queue_segment_boundary(blkdat->request_queue, -1L);
231 disk = alloc_disk(1);
237 disk->major = tapeblock_major;
238 disk->first_minor = device->first_minor;
239 disk->fops = &tapeblock_fops;
240 disk->private_data = tape_get_device(device);
241 disk->queue = blkdat->request_queue;
242 set_capacity(disk, 0);
243 sprintf(disk->disk_name, "btibm%d",
244 device->first_minor / TAPE_MINORS_PER_DEV);
247 blkdat->medium_changed = 1;
248 blkdat->request_queue->queuedata = tape_get_device(device);
252 tape_get_device(device);
253 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
258 blk_cleanup_queue(blkdat->request_queue);
259 blkdat->request_queue = NULL;
265 tapeblock_cleanup_device(struct tape_device *device)
267 flush_scheduled_work();
268 tape_put_device(device);
270 if (!device->blk_data.disk) {
274 del_gendisk(device->blk_data.disk);
275 device->blk_data.disk->private_data = NULL;
276 tape_put_device(device);
277 put_disk(device->blk_data.disk);
279 device->blk_data.disk = NULL;
281 device->blk_data.request_queue->queuedata = NULL;
282 tape_put_device(device);
284 blk_cleanup_queue(device->blk_data.request_queue);
285 device->blk_data.request_queue = NULL;
289 * Detect number of blocks of the tape.
290 * FIXME: can we extent this to detect the blocks size as well ?
293 tapeblock_revalidate_disk(struct gendisk *disk)
295 struct tape_device * device;
296 unsigned int nr_of_blks;
299 device = (struct tape_device *) disk->private_data;
302 if (!device->blk_data.medium_changed)
305 rc = tape_mtop(device, MTFSFM, 1);
309 rc = tape_mtop(device, MTTELL, 1);
313 pr_info("%s: Determining the size of the recorded area...\n",
314 dev_name(&device->cdev->dev));
315 DBF_LH(3, "Image file ends at %d\n", rc);
318 /* This will fail for the first file. Catch the error by checking the
320 tape_mtop(device, MTBSF, 1);
322 rc = tape_mtop(device, MTTELL, 1);
329 DBF_LH(3, "Image file starts at %d\n", rc);
333 pr_info("%s: The size of the recorded area is %i blocks\n",
334 dev_name(&device->cdev->dev), nr_of_blks);
335 set_capacity(device->blk_data.disk,
336 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
338 device->blk_data.block_position = 0;
339 device->blk_data.medium_changed = 0;
344 tapeblock_medium_changed(struct gendisk *disk)
346 struct tape_device *device;
348 device = (struct tape_device *) disk->private_data;
349 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
350 device, device->blk_data.medium_changed);
352 return device->blk_data.medium_changed;
356 * Block frontend tape device open function.
359 tapeblock_open(struct block_device *bdev, fmode_t mode)
361 struct gendisk * disk = bdev->bd_disk;
362 struct tape_device * device;
365 mutex_lock(&tape_block_mutex);
366 device = tape_get_device(disk->private_data);
368 if (device->required_tapemarks) {
369 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
370 pr_warning("%s: Opening the tape failed because of missing "
371 "end-of-file marks\n", dev_name(&device->cdev->dev));
376 rc = tape_open(device);
380 rc = tapeblock_revalidate_disk(disk);
385 * Note: The reference to <device> is hold until the release function
388 tape_state_set(device, TS_BLKUSE);
389 mutex_unlock(&tape_block_mutex);
393 tape_release(device);
395 tape_put_device(device);
396 mutex_unlock(&tape_block_mutex);
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 mutex_lock(&tape_block_mutex);
412 tape_state_set(device, TS_IN_USE);
413 tape_release(device);
414 tape_put_device(device);
415 mutex_unlock(&tape_block_mutex);
421 * Initialize block device frontend.
428 /* Register the tape major number to the kernel */
429 rc = register_blkdev(tapeblock_major, "tBLK");
433 if (tapeblock_major == 0)
434 tapeblock_major = rc;
439 * Deregister major for block device frontend
444 unregister_blkdev(tapeblock_major, "tBLK");