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/module.h>
15 #include <linux/blkdev.h>
16 #include <linux/interrupt.h>
17 #include <linux/buffer_head.h>
19 #include <asm/debug.h>
21 #define TAPE_DBF_AREA tape_core_dbf
25 #define PRINTK_HEADER "TAPE_BLOCK: "
27 #define TAPEBLOCK_MAX_SEC 100
28 #define TAPEBLOCK_MIN_REQUEUE 3
31 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
33 * In 2.5/2.6 the block device request function is very likely to be called
34 * with disabled interrupts (e.g. generic_unplug_device). So the driver can't
35 * just call any function that tries to allocate CCW requests from that con-
36 * text since it might sleep. There are two choices to work around this:
37 * a) do not allocate with kmalloc but use its own memory pool
38 * b) take requests from the queue outside that context, knowing that
39 * allocation might sleep
43 * file operation structure for tape block frontend
45 static int tapeblock_open(struct inode
*, struct file
*);
46 static int tapeblock_release(struct inode
*, struct file
*);
47 static int tapeblock_ioctl(struct inode
*, struct file
*, unsigned int,
49 static int tapeblock_medium_changed(struct gendisk
*);
50 static int tapeblock_revalidate_disk(struct gendisk
*);
52 static struct block_device_operations tapeblock_fops
= {
54 .open
= tapeblock_open
,
55 .release
= tapeblock_release
,
56 .ioctl
= tapeblock_ioctl
,
57 .media_changed
= tapeblock_medium_changed
,
58 .revalidate_disk
= tapeblock_revalidate_disk
,
61 static int tapeblock_major
= 0;
64 tapeblock_trigger_requeue(struct tape_device
*device
)
66 /* Protect against rescheduling. */
67 if (atomic_cmpxchg(&device
->blk_data
.requeue_scheduled
, 0, 1) != 0)
69 schedule_work(&device
->blk_data
.requeue_task
);
73 * Post finished request.
76 tapeblock_end_request(struct request
*req
, int uptodate
)
78 if (end_that_request_first(req
, uptodate
, req
->hard_nr_sectors
))
80 end_that_request_last(req
, uptodate
);
84 __tapeblock_end_request(struct tape_request
*ccw_req
, void *data
)
86 struct tape_device
*device
;
89 DBF_LH(6, "__tapeblock_end_request()\n");
91 device
= ccw_req
->device
;
92 req
= (struct request
*) data
;
93 tapeblock_end_request(req
, ccw_req
->rc
== 0);
95 /* Update position. */
96 device
->blk_data
.block_position
=
97 (req
->sector
+ req
->nr_sectors
) >> TAPEBLOCK_HSEC_S2B
;
99 /* We lost the position information due to an error. */
100 device
->blk_data
.block_position
= -1;
101 device
->discipline
->free_bread(ccw_req
);
102 if (!list_empty(&device
->req_queue
) ||
103 elv_next_request(device
->blk_data
.request_queue
))
104 tapeblock_trigger_requeue(device
);
108 * Feed the tape device CCW queue with requests supplied in a list.
111 tapeblock_start_request(struct tape_device
*device
, struct request
*req
)
113 struct tape_request
* ccw_req
;
116 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device
, req
);
118 ccw_req
= device
->discipline
->bread(device
, req
);
119 if (IS_ERR(ccw_req
)) {
120 DBF_EVENT(1, "TBLOCK: bread failed\n");
121 tapeblock_end_request(req
, 0);
122 return PTR_ERR(ccw_req
);
124 ccw_req
->callback
= __tapeblock_end_request
;
125 ccw_req
->callback_data
= (void *) req
;
126 ccw_req
->retries
= TAPEBLOCK_RETRIES
;
128 rc
= tape_do_io_async(device
, ccw_req
);
131 * Start/enqueueing failed. No retries in
134 tapeblock_end_request(req
, 0);
135 device
->discipline
->free_bread(ccw_req
);
142 * Move requests from the block device request queue to the tape device ccw
146 tapeblock_requeue(void *data
) {
147 struct tape_device
* device
;
148 request_queue_t
* queue
;
150 struct request
* req
;
151 struct list_head
* l
;
154 device
= (struct tape_device
*) data
;
158 spin_lock_irq(get_ccwdev_lock(device
->cdev
));
159 queue
= device
->blk_data
.request_queue
;
161 /* Count number of requests on ccw queue. */
163 list_for_each(l
, &device
->req_queue
)
165 spin_unlock(get_ccwdev_lock(device
->cdev
));
167 spin_lock(&device
->blk_data
.request_queue_lock
);
169 !blk_queue_plugged(queue
) &&
170 elv_next_request(queue
) &&
171 nr_queued
< TAPEBLOCK_MIN_REQUEUE
173 req
= elv_next_request(queue
);
174 if (rq_data_dir(req
) == WRITE
) {
175 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
176 blkdev_dequeue_request(req
);
177 tapeblock_end_request(req
, 0);
180 spin_unlock_irq(&device
->blk_data
.request_queue_lock
);
181 rc
= tapeblock_start_request(device
, req
);
182 spin_lock_irq(&device
->blk_data
.request_queue_lock
);
183 blkdev_dequeue_request(req
);
186 spin_unlock_irq(&device
->blk_data
.request_queue_lock
);
187 atomic_set(&device
->blk_data
.requeue_scheduled
, 0);
191 * Tape request queue function. Called from ll_rw_blk.c
194 tapeblock_request_fn(request_queue_t
*queue
)
196 struct tape_device
*device
;
198 device
= (struct tape_device
*) queue
->queuedata
;
199 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device
);
200 BUG_ON(device
== NULL
);
201 tapeblock_trigger_requeue(device
);
205 * This function is called for every new tapedevice
208 tapeblock_setup_device(struct tape_device
* device
)
210 struct tape_blk_data
* blkdat
;
211 struct gendisk
* disk
;
214 blkdat
= &device
->blk_data
;
215 spin_lock_init(&blkdat
->request_queue_lock
);
216 atomic_set(&blkdat
->requeue_scheduled
, 0);
218 blkdat
->request_queue
= blk_init_queue(
219 tapeblock_request_fn
,
220 &blkdat
->request_queue_lock
222 if (!blkdat
->request_queue
)
225 elevator_exit(blkdat
->request_queue
->elevator
);
226 rc
= elevator_init(blkdat
->request_queue
, "noop");
230 blk_queue_hardsect_size(blkdat
->request_queue
, TAPEBLOCK_HSEC_SIZE
);
231 blk_queue_max_sectors(blkdat
->request_queue
, TAPEBLOCK_MAX_SEC
);
232 blk_queue_max_phys_segments(blkdat
->request_queue
, -1L);
233 blk_queue_max_hw_segments(blkdat
->request_queue
, -1L);
234 blk_queue_max_segment_size(blkdat
->request_queue
, -1L);
235 blk_queue_segment_boundary(blkdat
->request_queue
, -1L);
237 disk
= alloc_disk(1);
243 disk
->major
= tapeblock_major
;
244 disk
->first_minor
= device
->first_minor
;
245 disk
->fops
= &tapeblock_fops
;
246 disk
->private_data
= tape_get_device_reference(device
);
247 disk
->queue
= blkdat
->request_queue
;
248 set_capacity(disk
, 0);
249 sprintf(disk
->disk_name
, "btibm%d",
250 device
->first_minor
/ TAPE_MINORS_PER_DEV
);
253 blkdat
->medium_changed
= 1;
254 blkdat
->request_queue
->queuedata
= tape_get_device_reference(device
);
258 INIT_WORK(&blkdat
->requeue_task
, tapeblock_requeue
,
259 tape_get_device_reference(device
));
264 blk_cleanup_queue(blkdat
->request_queue
);
265 blkdat
->request_queue
= NULL
;
271 tapeblock_cleanup_device(struct tape_device
*device
)
273 flush_scheduled_work();
274 device
->blk_data
.requeue_task
.data
= tape_put_device(device
);
276 if (!device
->blk_data
.disk
) {
277 PRINT_ERR("(%s): No gendisk to clean up!\n",
278 device
->cdev
->dev
.bus_id
);
282 del_gendisk(device
->blk_data
.disk
);
283 device
->blk_data
.disk
->private_data
=
284 tape_put_device(device
->blk_data
.disk
->private_data
);
285 put_disk(device
->blk_data
.disk
);
287 device
->blk_data
.disk
= NULL
;
289 device
->blk_data
.request_queue
->queuedata
= tape_put_device(device
);
291 blk_cleanup_queue(device
->blk_data
.request_queue
);
292 device
->blk_data
.request_queue
= NULL
;
296 * Detect number of blocks of the tape.
297 * FIXME: can we extent this to detect the blocks size as well ?
300 tapeblock_revalidate_disk(struct gendisk
*disk
)
302 struct tape_device
* device
;
303 unsigned int nr_of_blks
;
306 device
= (struct tape_device
*) disk
->private_data
;
309 if (!device
->blk_data
.medium_changed
)
312 PRINT_INFO("Detecting media size...\n");
313 rc
= tape_mtop(device
, MTFSFM
, 1);
317 rc
= tape_mtop(device
, MTTELL
, 1);
321 DBF_LH(3, "Image file ends at %d\n", rc
);
324 /* This will fail for the first file. Catch the error by checking the
326 tape_mtop(device
, MTBSF
, 1);
328 rc
= tape_mtop(device
, MTTELL
, 1);
335 DBF_LH(3, "Image file starts at %d\n", rc
);
339 PRINT_INFO("Found %i blocks on media\n", nr_of_blks
);
340 set_capacity(device
->blk_data
.disk
,
341 nr_of_blks
*(TAPEBLOCK_HSEC_SIZE
/512));
343 device
->blk_data
.block_position
= 0;
344 device
->blk_data
.medium_changed
= 0;
349 tapeblock_medium_changed(struct gendisk
*disk
)
351 struct tape_device
*device
;
353 device
= (struct tape_device
*) disk
->private_data
;
354 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
355 device
, device
->blk_data
.medium_changed
);
357 return device
->blk_data
.medium_changed
;
361 * Block frontend tape device open function.
364 tapeblock_open(struct inode
*inode
, struct file
*filp
)
366 struct gendisk
* disk
;
367 struct tape_device
* device
;
370 disk
= inode
->i_bdev
->bd_disk
;
371 device
= tape_get_device_reference(disk
->private_data
);
373 if (device
->required_tapemarks
) {
374 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
375 PRINT_ERR("TBLOCK: Refusing to open tape with missing"
376 " end of file marks.\n");
381 rc
= tape_open(device
);
385 rc
= tapeblock_revalidate_disk(disk
);
390 * Note: The reference to <device> is hold until the release function
393 tape_state_set(device
, TS_BLKUSE
);
397 tape_release(device
);
399 tape_put_device(device
);
404 * Block frontend tape device release function.
406 * Note: One reference to the tape device was made by the open function. So
407 * we just get the pointer here and release the reference.
410 tapeblock_release(struct inode
*inode
, struct file
*filp
)
412 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
413 struct tape_device
*device
= disk
->private_data
;
415 tape_state_set(device
, TS_IN_USE
);
416 tape_release(device
);
417 tape_put_device(device
);
423 * Support of some generic block device IOCTLs.
427 struct inode
* inode
,
429 unsigned int command
,
434 struct gendisk
*disk
;
435 struct tape_device
*device
;
438 disk
= inode
->i_bdev
->bd_disk
;
440 device
= disk
->private_data
;
442 minor
= iminor(inode
);
444 DBF_LH(6, "tapeblock_ioctl(0x%0x)\n", command
);
445 DBF_LH(6, "device = %d:%d\n", tapeblock_major
, minor
);
448 /* Refuse some IOCTL calls without complaining (mount). */
449 case 0x5310: /* CDROMMULTISESSION */
453 PRINT_WARN("invalid ioctl 0x%x\n", command
);
461 * Initialize block device frontend.
468 /* Register the tape major number to the kernel */
469 rc
= register_blkdev(tapeblock_major
, "tBLK");
473 if (tapeblock_major
== 0)
474 tapeblock_major
= rc
;
475 PRINT_INFO("tape gets major %d for block device\n", tapeblock_major
);
480 * Deregister major for block device frontend
485 unregister_blkdev(tapeblock_major
, "tBLK");