RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / s390 / char / tape_block.c
blobb136e75019493790fdcb7514319f356499a1e2ed
1 /*
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
16 #include <linux/fs.h>
17 #include <linux/module.h>
18 #include <linux/blkdev.h>
19 #include <linux/smp_lock.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
28 #include "tape.h"
30 #define TAPEBLOCK_MAX_SEC 100
31 #define TAPEBLOCK_MIN_REQUEUE 3
35 * file operation structure for tape block frontend
37 static int tapeblock_open(struct block_device *, fmode_t);
38 static int tapeblock_release(struct gendisk *, fmode_t);
39 static int tapeblock_medium_changed(struct gendisk *);
40 static int tapeblock_revalidate_disk(struct gendisk *);
42 static const struct block_device_operations tapeblock_fops = {
43 .owner = THIS_MODULE,
44 .open = tapeblock_open,
45 .release = tapeblock_release,
46 .media_changed = tapeblock_medium_changed,
47 .revalidate_disk = tapeblock_revalidate_disk,
50 static int tapeblock_major = 0;
52 static void
53 tapeblock_trigger_requeue(struct tape_device *device)
55 /* Protect against rescheduling. */
56 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
57 return;
58 schedule_work(&device->blk_data.requeue_task);
62 * Post finished request.
64 static void
65 __tapeblock_end_request(struct tape_request *ccw_req, void *data)
67 struct tape_device *device;
68 struct request *req;
70 DBF_LH(6, "__tapeblock_end_request()\n");
72 device = ccw_req->device;
73 req = (struct request *) data;
74 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
75 if (ccw_req->rc == 0)
76 /* Update position. */
77 device->blk_data.block_position =
78 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
79 else
80 /* We lost the position information due to an error. */
81 device->blk_data.block_position = -1;
82 device->discipline->free_bread(ccw_req);
83 if (!list_empty(&device->req_queue) ||
84 blk_peek_request(device->blk_data.request_queue))
85 tapeblock_trigger_requeue(device);
89 * Feed the tape device CCW queue with requests supplied in a list.
91 static int
92 tapeblock_start_request(struct tape_device *device, struct request *req)
94 struct tape_request * ccw_req;
95 int rc;
97 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
99 ccw_req = device->discipline->bread(device, req);
100 if (IS_ERR(ccw_req)) {
101 DBF_EVENT(1, "TBLOCK: bread failed\n");
102 blk_end_request_all(req, -EIO);
103 return PTR_ERR(ccw_req);
105 ccw_req->callback = __tapeblock_end_request;
106 ccw_req->callback_data = (void *) req;
107 ccw_req->retries = TAPEBLOCK_RETRIES;
109 rc = tape_do_io_async(device, ccw_req);
110 if (rc) {
112 * Start/enqueueing failed. No retries in
113 * this case.
115 blk_end_request_all(req, -EIO);
116 device->discipline->free_bread(ccw_req);
119 return rc;
123 * Move requests from the block device request queue to the tape device ccw
124 * queue.
126 static void
127 tapeblock_requeue(struct work_struct *work) {
128 struct tape_blk_data * blkdat;
129 struct tape_device * device;
130 struct request_queue * queue;
131 int nr_queued;
132 struct request * req;
133 struct list_head * l;
134 int rc;
136 blkdat = container_of(work, struct tape_blk_data, requeue_task);
137 device = blkdat->device;
138 if (!device)
139 return;
141 spin_lock_irq(get_ccwdev_lock(device->cdev));
142 queue = device->blk_data.request_queue;
144 /* Count number of requests on ccw queue. */
145 nr_queued = 0;
146 list_for_each(l, &device->req_queue)
147 nr_queued++;
148 spin_unlock(get_ccwdev_lock(device->cdev));
150 spin_lock_irq(&device->blk_data.request_queue_lock);
151 while (
152 !blk_queue_plugged(queue) &&
153 blk_peek_request(queue) &&
154 nr_queued < TAPEBLOCK_MIN_REQUEUE
156 req = blk_fetch_request(queue);
157 if (rq_data_dir(req) == WRITE) {
158 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
159 spin_unlock_irq(&device->blk_data.request_queue_lock);
160 blk_end_request_all(req, -EIO);
161 spin_lock_irq(&device->blk_data.request_queue_lock);
162 continue;
164 nr_queued++;
165 spin_unlock_irq(&device->blk_data.request_queue_lock);
166 rc = tapeblock_start_request(device, req);
167 spin_lock_irq(&device->blk_data.request_queue_lock);
169 spin_unlock_irq(&device->blk_data.request_queue_lock);
170 atomic_set(&device->blk_data.requeue_scheduled, 0);
174 * Tape request queue function. Called from ll_rw_blk.c
176 static void
177 tapeblock_request_fn(struct request_queue *queue)
179 struct tape_device *device;
181 device = (struct tape_device *) queue->queuedata;
182 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
183 BUG_ON(device == NULL);
184 tapeblock_trigger_requeue(device);
188 * This function is called for every new tapedevice
191 tapeblock_setup_device(struct tape_device * device)
193 struct tape_blk_data * blkdat;
194 struct gendisk * disk;
195 int rc;
197 blkdat = &device->blk_data;
198 blkdat->device = device;
199 spin_lock_init(&blkdat->request_queue_lock);
200 atomic_set(&blkdat->requeue_scheduled, 0);
202 blkdat->request_queue = blk_init_queue(
203 tapeblock_request_fn,
204 &blkdat->request_queue_lock
206 if (!blkdat->request_queue)
207 return -ENOMEM;
209 rc = elevator_change(blkdat->request_queue, "noop");
210 if (rc)
211 goto cleanup_queue;
213 blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
214 blk_queue_max_hw_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
215 blk_queue_max_segments(blkdat->request_queue, -1L);
216 blk_queue_max_segment_size(blkdat->request_queue, -1L);
217 blk_queue_segment_boundary(blkdat->request_queue, -1L);
219 disk = alloc_disk(1);
220 if (!disk) {
221 rc = -ENOMEM;
222 goto cleanup_queue;
225 disk->major = tapeblock_major;
226 disk->first_minor = device->first_minor;
227 disk->fops = &tapeblock_fops;
228 disk->private_data = tape_get_device(device);
229 disk->queue = blkdat->request_queue;
230 set_capacity(disk, 0);
231 sprintf(disk->disk_name, "btibm%d",
232 device->first_minor / TAPE_MINORS_PER_DEV);
234 blkdat->disk = disk;
235 blkdat->medium_changed = 1;
236 blkdat->request_queue->queuedata = tape_get_device(device);
238 add_disk(disk);
240 tape_get_device(device);
241 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
243 return 0;
245 cleanup_queue:
246 blk_cleanup_queue(blkdat->request_queue);
247 blkdat->request_queue = NULL;
249 return rc;
252 void
253 tapeblock_cleanup_device(struct tape_device *device)
255 flush_scheduled_work();
256 tape_put_device(device);
258 if (!device->blk_data.disk) {
259 goto cleanup_queue;
262 del_gendisk(device->blk_data.disk);
263 device->blk_data.disk->private_data = NULL;
264 tape_put_device(device);
265 put_disk(device->blk_data.disk);
267 device->blk_data.disk = NULL;
268 cleanup_queue:
269 device->blk_data.request_queue->queuedata = NULL;
270 tape_put_device(device);
272 blk_cleanup_queue(device->blk_data.request_queue);
273 device->blk_data.request_queue = NULL;
276 static int
277 tapeblock_revalidate_disk(struct gendisk *disk)
279 struct tape_device * device;
280 unsigned int nr_of_blks;
281 int rc;
283 device = (struct tape_device *) disk->private_data;
284 BUG_ON(!device);
286 if (!device->blk_data.medium_changed)
287 return 0;
289 rc = tape_mtop(device, MTFSFM, 1);
290 if (rc)
291 return rc;
293 rc = tape_mtop(device, MTTELL, 1);
294 if (rc < 0)
295 return rc;
297 pr_info("%s: Determining the size of the recorded area...\n",
298 dev_name(&device->cdev->dev));
299 DBF_LH(3, "Image file ends at %d\n", rc);
300 nr_of_blks = rc;
302 /* This will fail for the first file. Catch the error by checking the
303 * position. */
304 tape_mtop(device, MTBSF, 1);
306 rc = tape_mtop(device, MTTELL, 1);
307 if (rc < 0)
308 return rc;
310 if (rc > nr_of_blks)
311 return -EINVAL;
313 DBF_LH(3, "Image file starts at %d\n", rc);
314 device->bof = rc;
315 nr_of_blks -= rc;
317 pr_info("%s: The size of the recorded area is %i blocks\n",
318 dev_name(&device->cdev->dev), nr_of_blks);
319 set_capacity(device->blk_data.disk,
320 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
322 device->blk_data.block_position = 0;
323 device->blk_data.medium_changed = 0;
324 return 0;
327 static int
328 tapeblock_medium_changed(struct gendisk *disk)
330 struct tape_device *device;
332 device = (struct tape_device *) disk->private_data;
333 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
334 device, device->blk_data.medium_changed);
336 return device->blk_data.medium_changed;
340 * Block frontend tape device open function.
342 static int
343 tapeblock_open(struct block_device *bdev, fmode_t mode)
345 struct gendisk * disk = bdev->bd_disk;
346 struct tape_device * device;
347 int rc;
349 lock_kernel();
350 device = tape_get_device(disk->private_data);
352 if (device->required_tapemarks) {
353 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
354 pr_warning("%s: Opening the tape failed because of missing "
355 "end-of-file marks\n", dev_name(&device->cdev->dev));
356 rc = -EPERM;
357 goto put_device;
360 rc = tape_open(device);
361 if (rc)
362 goto put_device;
364 rc = tapeblock_revalidate_disk(disk);
365 if (rc)
366 goto release;
369 * Note: The reference to <device> is hold until the release function
370 * is called.
372 tape_state_set(device, TS_BLKUSE);
373 unlock_kernel();
374 return 0;
376 release:
377 tape_release(device);
378 put_device:
379 tape_put_device(device);
380 unlock_kernel();
381 return rc;
385 * Block frontend tape device release function.
387 * Note: One reference to the tape device was made by the open function. So
388 * we just get the pointer here and release the reference.
390 static int
391 tapeblock_release(struct gendisk *disk, fmode_t mode)
393 struct tape_device *device = disk->private_data;
395 lock_kernel();
396 tape_state_set(device, TS_IN_USE);
397 tape_release(device);
398 tape_put_device(device);
399 unlock_kernel();
401 return 0;
405 * Initialize block device frontend.
408 tapeblock_init(void)
410 int rc;
412 /* Register the tape major number to the kernel */
413 rc = register_blkdev(tapeblock_major, "tBLK");
414 if (rc < 0)
415 return rc;
417 if (tapeblock_major == 0)
418 tapeblock_major = rc;
419 return 0;
423 * Deregister major for block device frontend
425 void
426 tapeblock_exit(void)
428 unregister_blkdev(tapeblock_major, "tBLK");