blob: 8d3d720737da20879f1ac85b1f9da6a7432e99cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/tape_block.c
3 * block device frontend for tape device driver
4 *
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>
11 */
12
Carsten Otteab640db2009-03-26 15:24:38 +010013#define KMSG_COMPONENT "tape"
Michael Holzheubb509912009-12-18 17:43:21 +010014#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
Carsten Otteab640db2009-03-26 15:24:38 +010015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/blkdev.h>
19#include <linux/interrupt.h>
20#include <linux/buffer_head.h>
Martin Schwidefskyc1637532006-12-08 15:53:57 +010021#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/debug.h>
24
25#define TAPE_DBF_AREA tape_core_dbf
26
27#include "tape.h"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define TAPEBLOCK_MAX_SEC 100
30#define TAPEBLOCK_MIN_REQUEUE 3
31
32/*
33 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
34 *
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
42 */
43
44/*
45 * file operation structure for tape block frontend
46 */
Al Viro4e999af2008-03-02 10:39:59 -050047static int tapeblock_open(struct block_device *, fmode_t);
48static int tapeblock_release(struct gendisk *, fmode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int tapeblock_medium_changed(struct gendisk *);
50static int tapeblock_revalidate_disk(struct gendisk *);
51
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070052static const struct block_device_operations tapeblock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .owner = THIS_MODULE,
Al Viro4e999af2008-03-02 10:39:59 -050054 .open = tapeblock_open,
55 .release = tapeblock_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .media_changed = tapeblock_medium_changed,
57 .revalidate_disk = tapeblock_revalidate_disk,
58};
59
60static int tapeblock_major = 0;
61
62static void
63tapeblock_trigger_requeue(struct tape_device *device)
64{
65 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -080066 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return;
68 schedule_work(&device->blk_data.requeue_task);
69}
70
71/*
72 * Post finished request.
73 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010074static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070075__tapeblock_end_request(struct tape_request *ccw_req, void *data)
76{
77 struct tape_device *device;
78 struct request *req;
79
80 DBF_LH(6, "__tapeblock_end_request()\n");
81
82 device = ccw_req->device;
83 req = (struct request *) data;
Tejun Heo40cbbb72009-04-23 11:05:19 +090084 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (ccw_req->rc == 0)
86 /* Update position. */
87 device->blk_data.block_position =
Tejun Heo83096eb2009-05-07 22:24:39 +090088 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 else
90 /* We lost the position information due to an error. */
91 device->blk_data.block_position = -1;
92 device->discipline->free_bread(ccw_req);
93 if (!list_empty(&device->req_queue) ||
Tejun Heo9934c8c2009-05-08 11:54:16 +090094 blk_peek_request(device->blk_data.request_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 tapeblock_trigger_requeue(device);
96}
97
98/*
99 * Feed the tape device CCW queue with requests supplied in a list.
100 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100101static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102tapeblock_start_request(struct tape_device *device, struct request *req)
103{
104 struct tape_request * ccw_req;
105 int rc;
106
107 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
108
109 ccw_req = device->discipline->bread(device, req);
110 if (IS_ERR(ccw_req)) {
111 DBF_EVENT(1, "TBLOCK: bread failed\n");
Tejun Heo40cbbb72009-04-23 11:05:19 +0900112 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return PTR_ERR(ccw_req);
114 }
115 ccw_req->callback = __tapeblock_end_request;
116 ccw_req->callback_data = (void *) req;
117 ccw_req->retries = TAPEBLOCK_RETRIES;
118
119 rc = tape_do_io_async(device, ccw_req);
120 if (rc) {
121 /*
122 * Start/enqueueing failed. No retries in
123 * this case.
124 */
Tejun Heo40cbbb72009-04-23 11:05:19 +0900125 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 device->discipline->free_bread(ccw_req);
127 }
128
129 return rc;
130}
131
132/*
133 * Move requests from the block device request queue to the tape device ccw
134 * queue.
135 */
136static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100137tapeblock_requeue(struct work_struct *work) {
138 struct tape_blk_data * blkdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct tape_device * device;
Jens Axboe165125e2007-07-24 09:28:11 +0200140 struct request_queue * queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int nr_queued;
142 struct request * req;
143 struct list_head * l;
144 int rc;
145
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100146 blkdat = container_of(work, struct tape_blk_data, requeue_task);
147 device = blkdat->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!device)
149 return;
150
151 spin_lock_irq(get_ccwdev_lock(device->cdev));
152 queue = device->blk_data.request_queue;
153
154 /* Count number of requests on ccw queue. */
155 nr_queued = 0;
156 list_for_each(l, &device->req_queue)
157 nr_queued++;
158 spin_unlock(get_ccwdev_lock(device->cdev));
159
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100160 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 while (
162 !blk_queue_plugged(queue) &&
Michael Holzheu03cadd32009-10-14 12:43:45 +0200163 blk_peek_request(queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 nr_queued < TAPEBLOCK_MIN_REQUEUE
165 ) {
Michael Holzheu03cadd32009-10-14 12:43:45 +0200166 req = blk_fetch_request(queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (rq_data_dir(req) == WRITE) {
168 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100169 spin_unlock_irq(&device->blk_data.request_queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900170 blk_end_request_all(req, -EIO);
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100171 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 continue;
173 }
Michael Holzheuf71ad622008-05-30 10:03:25 +0200174 nr_queued++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 spin_unlock_irq(&device->blk_data.request_queue_lock);
176 rc = tapeblock_start_request(device, req);
177 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 spin_unlock_irq(&device->blk_data.request_queue_lock);
180 atomic_set(&device->blk_data.requeue_scheduled, 0);
181}
182
183/*
184 * Tape request queue function. Called from ll_rw_blk.c
185 */
186static void
Jens Axboe165125e2007-07-24 09:28:11 +0200187tapeblock_request_fn(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct tape_device *device;
190
191 device = (struct tape_device *) queue->queuedata;
192 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200193 BUG_ON(device == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 tapeblock_trigger_requeue(device);
195}
196
197/*
198 * This function is called for every new tapedevice
199 */
200int
201tapeblock_setup_device(struct tape_device * device)
202{
203 struct tape_blk_data * blkdat;
204 struct gendisk * disk;
205 int rc;
206
207 blkdat = &device->blk_data;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100208 blkdat->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 spin_lock_init(&blkdat->request_queue_lock);
210 atomic_set(&blkdat->requeue_scheduled, 0);
211
212 blkdat->request_queue = blk_init_queue(
213 tapeblock_request_fn,
214 &blkdat->request_queue_lock
215 );
216 if (!blkdat->request_queue)
217 return -ENOMEM;
218
219 elevator_exit(blkdat->request_queue->elevator);
220 rc = elevator_init(blkdat->request_queue, "noop");
221 if (rc)
222 goto cleanup_queue;
223
Martin K. Petersene1defc42009-05-22 17:17:49 -0400224 blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 blk_queue_max_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
226 blk_queue_max_phys_segments(blkdat->request_queue, -1L);
227 blk_queue_max_hw_segments(blkdat->request_queue, -1L);
228 blk_queue_max_segment_size(blkdat->request_queue, -1L);
229 blk_queue_segment_boundary(blkdat->request_queue, -1L);
230
231 disk = alloc_disk(1);
232 if (!disk) {
233 rc = -ENOMEM;
234 goto cleanup_queue;
235 }
236
237 disk->major = tapeblock_major;
238 disk->first_minor = device->first_minor;
239 disk->fops = &tapeblock_fops;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100240 disk->private_data = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 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);
245
246 blkdat->disk = disk;
247 blkdat->medium_changed = 1;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100248 blkdat->request_queue->queuedata = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 add_disk(disk);
251
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100252 tape_get_device(device);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100253 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 return 0;
256
257cleanup_queue:
258 blk_cleanup_queue(blkdat->request_queue);
259 blkdat->request_queue = NULL;
260
261 return rc;
262}
263
264void
265tapeblock_cleanup_device(struct tape_device *device)
266{
267 flush_scheduled_work();
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100268 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (!device->blk_data.disk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto cleanup_queue;
272 }
273
274 del_gendisk(device->blk_data.disk);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100275 device->blk_data.disk->private_data = NULL;
276 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 put_disk(device->blk_data.disk);
278
279 device->blk_data.disk = NULL;
280cleanup_queue:
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100281 device->blk_data.request_queue->queuedata = NULL;
282 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 blk_cleanup_queue(device->blk_data.request_queue);
285 device->blk_data.request_queue = NULL;
286}
287
288/*
289 * Detect number of blocks of the tape.
290 * FIXME: can we extent this to detect the blocks size as well ?
291 */
292static int
293tapeblock_revalidate_disk(struct gendisk *disk)
294{
295 struct tape_device * device;
296 unsigned int nr_of_blks;
297 int rc;
298
299 device = (struct tape_device *) disk->private_data;
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200300 BUG_ON(!device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (!device->blk_data.medium_changed)
303 return 0;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 rc = tape_mtop(device, MTFSFM, 1);
306 if (rc)
307 return rc;
308
309 rc = tape_mtop(device, MTTELL, 1);
310 if (rc < 0)
311 return rc;
312
Michael Holzheu59e36922009-09-11 10:29:07 +0200313 pr_info("%s: Determining the size of the recorded area...\n",
314 dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 DBF_LH(3, "Image file ends at %d\n", rc);
316 nr_of_blks = rc;
317
318 /* This will fail for the first file. Catch the error by checking the
319 * position. */
320 tape_mtop(device, MTBSF, 1);
321
322 rc = tape_mtop(device, MTTELL, 1);
323 if (rc < 0)
324 return rc;
325
326 if (rc > nr_of_blks)
327 return -EINVAL;
328
329 DBF_LH(3, "Image file starts at %d\n", rc);
330 device->bof = rc;
331 nr_of_blks -= rc;
332
Michael Holzheu59e36922009-09-11 10:29:07 +0200333 pr_info("%s: The size of the recorded area is %i blocks\n",
334 dev_name(&device->cdev->dev), nr_of_blks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 set_capacity(device->blk_data.disk,
336 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
337
338 device->blk_data.block_position = 0;
339 device->blk_data.medium_changed = 0;
340 return 0;
341}
342
343static int
344tapeblock_medium_changed(struct gendisk *disk)
345{
346 struct tape_device *device;
347
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);
351
352 return device->blk_data.medium_changed;
353}
354
355/*
356 * Block frontend tape device open function.
357 */
358static int
Al Viro4e999af2008-03-02 10:39:59 -0500359tapeblock_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Al Viro4e999af2008-03-02 10:39:59 -0500361 struct gendisk * disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct tape_device * device;
363 int rc;
364
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100365 device = tape_get_device(disk->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (device->required_tapemarks) {
368 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
Michael Holzheu59e36922009-09-11 10:29:07 +0200369 pr_warning("%s: Opening the tape failed because of missing "
370 "end-of-file marks\n", dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 rc = -EPERM;
372 goto put_device;
373 }
374
375 rc = tape_open(device);
376 if (rc)
377 goto put_device;
378
379 rc = tapeblock_revalidate_disk(disk);
380 if (rc)
381 goto release;
382
383 /*
384 * Note: The reference to <device> is hold until the release function
385 * is called.
386 */
387 tape_state_set(device, TS_BLKUSE);
388 return 0;
389
390release:
391 tape_release(device);
392 put_device:
393 tape_put_device(device);
394 return rc;
395}
396
397/*
398 * Block frontend tape device release function.
399 *
400 * Note: One reference to the tape device was made by the open function. So
401 * we just get the pointer here and release the reference.
402 */
403static int
Al Viro4e999af2008-03-02 10:39:59 -0500404tapeblock_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct tape_device *device = disk->private_data;
407
408 tape_state_set(device, TS_IN_USE);
409 tape_release(device);
410 tape_put_device(device);
411
412 return 0;
413}
414
415/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * Initialize block device frontend.
417 */
418int
419tapeblock_init(void)
420{
421 int rc;
422
423 /* Register the tape major number to the kernel */
424 rc = register_blkdev(tapeblock_major, "tBLK");
425 if (rc < 0)
426 return rc;
427
428 if (tapeblock_major == 0)
429 tapeblock_major = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431}
432
433/*
434 * Deregister major for block device frontend
435 */
436void
437tapeblock_exit(void)
438{
439 unregister_blkdev(tapeblock_major, "tBLK");
440}