blob: ad8431636012619f16e3ca313d317d52d235d44c [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>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/interrupt.h>
21#include <linux/buffer_head.h>
Martin Schwidefskyc1637532006-12-08 15:53:57 +010022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/debug.h>
25
26#define TAPE_DBF_AREA tape_core_dbf
27
28#include "tape.h"
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define TAPEBLOCK_MAX_SEC 100
31#define TAPEBLOCK_MIN_REQUEUE 3
32
33/*
34 * 2003/11/25 Stefan Bader <shbader@de.ibm.com>
35 *
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
43 */
44
45/*
46 * file operation structure for tape block frontend
47 */
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020048static DEFINE_MUTEX(tape_block_mutex);
Al Viro4e999af2008-03-02 10:39:59 -050049static int tapeblock_open(struct block_device *, fmode_t);
50static int tapeblock_release(struct gendisk *, fmode_t);
Tejun Heoffe80ce2011-03-09 19:54:28 +010051static unsigned int tapeblock_check_events(struct gendisk *, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int tapeblock_revalidate_disk(struct gendisk *);
53
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070054static const struct block_device_operations tapeblock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .owner = THIS_MODULE,
Al Viro4e999af2008-03-02 10:39:59 -050056 .open = tapeblock_open,
57 .release = tapeblock_release,
Tejun Heoffe80ce2011-03-09 19:54:28 +010058 .check_events = tapeblock_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 .revalidate_disk = tapeblock_revalidate_disk,
60};
61
62static int tapeblock_major = 0;
63
64static void
65tapeblock_trigger_requeue(struct tape_device *device)
66{
67 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -080068 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return;
70 schedule_work(&device->blk_data.requeue_task);
71}
72
73/*
74 * Post finished request.
75 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010076static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070077__tapeblock_end_request(struct tape_request *ccw_req, void *data)
78{
79 struct tape_device *device;
80 struct request *req;
81
82 DBF_LH(6, "__tapeblock_end_request()\n");
83
84 device = ccw_req->device;
85 req = (struct request *) data;
Tejun Heo40cbbb72009-04-23 11:05:19 +090086 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (ccw_req->rc == 0)
88 /* Update position. */
89 device->blk_data.block_position =
Tejun Heo83096eb2009-05-07 22:24:39 +090090 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 else
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) ||
Tejun Heo9934c8c2009-05-08 11:54:16 +090096 blk_peek_request(device->blk_data.request_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 tapeblock_trigger_requeue(device);
98}
99
100/*
101 * Feed the tape device CCW queue with requests supplied in a list.
102 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100103static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104tapeblock_start_request(struct tape_device *device, struct request *req)
105{
106 struct tape_request * ccw_req;
107 int rc;
108
109 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
110
111 ccw_req = device->discipline->bread(device, req);
112 if (IS_ERR(ccw_req)) {
113 DBF_EVENT(1, "TBLOCK: bread failed\n");
Tejun Heo40cbbb72009-04-23 11:05:19 +0900114 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return PTR_ERR(ccw_req);
116 }
117 ccw_req->callback = __tapeblock_end_request;
118 ccw_req->callback_data = (void *) req;
119 ccw_req->retries = TAPEBLOCK_RETRIES;
120
121 rc = tape_do_io_async(device, ccw_req);
122 if (rc) {
123 /*
124 * Start/enqueueing failed. No retries in
125 * this case.
126 */
Tejun Heo40cbbb72009-04-23 11:05:19 +0900127 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 device->discipline->free_bread(ccw_req);
129 }
130
131 return rc;
132}
133
134/*
135 * Move requests from the block device request queue to the tape device ccw
136 * queue.
137 */
138static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100139tapeblock_requeue(struct work_struct *work) {
140 struct tape_blk_data * blkdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct tape_device * device;
Jens Axboe165125e2007-07-24 09:28:11 +0200142 struct request_queue * queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int nr_queued;
144 struct request * req;
145 struct list_head * l;
146 int rc;
147
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100148 blkdat = container_of(work, struct tape_blk_data, requeue_task);
149 device = blkdat->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (!device)
151 return;
152
153 spin_lock_irq(get_ccwdev_lock(device->cdev));
154 queue = device->blk_data.request_queue;
155
156 /* Count number of requests on ccw queue. */
157 nr_queued = 0;
158 list_for_each(l, &device->req_queue)
159 nr_queued++;
160 spin_unlock(get_ccwdev_lock(device->cdev));
161
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100162 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 while (
164 !blk_queue_plugged(queue) &&
Michael Holzheu03cadd32009-10-14 12:43:45 +0200165 blk_peek_request(queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 nr_queued < TAPEBLOCK_MIN_REQUEUE
167 ) {
Michael Holzheu03cadd32009-10-14 12:43:45 +0200168 req = blk_fetch_request(queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (rq_data_dir(req) == WRITE) {
170 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100171 spin_unlock_irq(&device->blk_data.request_queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900172 blk_end_request_all(req, -EIO);
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100173 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 continue;
175 }
Michael Holzheuf71ad622008-05-30 10:03:25 +0200176 nr_queued++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 spin_unlock_irq(&device->blk_data.request_queue_lock);
182 atomic_set(&device->blk_data.requeue_scheduled, 0);
183}
184
185/*
186 * Tape request queue function. Called from ll_rw_blk.c
187 */
188static void
Jens Axboe165125e2007-07-24 09:28:11 +0200189tapeblock_request_fn(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct tape_device *device;
192
193 device = (struct tape_device *) queue->queuedata;
194 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200195 BUG_ON(device == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 tapeblock_trigger_requeue(device);
197}
198
199/*
200 * This function is called for every new tapedevice
201 */
202int
203tapeblock_setup_device(struct tape_device * device)
204{
205 struct tape_blk_data * blkdat;
206 struct gendisk * disk;
207 int rc;
208
209 blkdat = &device->blk_data;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100210 blkdat->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 spin_lock_init(&blkdat->request_queue_lock);
212 atomic_set(&blkdat->requeue_scheduled, 0);
213
214 blkdat->request_queue = blk_init_queue(
215 tapeblock_request_fn,
216 &blkdat->request_queue_lock
217 );
218 if (!blkdat->request_queue)
219 return -ENOMEM;
220
Jens Axboe52cc2ee2010-08-23 14:02:44 +0200221 rc = elevator_change(blkdat->request_queue, "noop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (rc)
223 goto cleanup_queue;
224
Martin K. Petersene1defc42009-05-22 17:17:49 -0400225 blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500226 blk_queue_max_hw_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500227 blk_queue_max_segments(blkdat->request_queue, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 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;
Tejun Heoffe80ce2011-03-09 19:54:28 +0100240 disk->events = DISK_EVENT_MEDIA_CHANGE;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100241 disk->private_data = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 disk->queue = blkdat->request_queue;
243 set_capacity(disk, 0);
244 sprintf(disk->disk_name, "btibm%d",
245 device->first_minor / TAPE_MINORS_PER_DEV);
246
247 blkdat->disk = disk;
248 blkdat->medium_changed = 1;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100249 blkdat->request_queue->queuedata = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 add_disk(disk);
252
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100253 tape_get_device(device);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100254 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 return 0;
257
258cleanup_queue:
259 blk_cleanup_queue(blkdat->request_queue);
260 blkdat->request_queue = NULL;
261
262 return rc;
263}
264
265void
266tapeblock_cleanup_device(struct tape_device *device)
267{
Tejun Heo6dca4672010-12-24 16:00:18 +0100268 flush_work_sync(&device->blk_data.requeue_task);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100269 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (!device->blk_data.disk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto cleanup_queue;
273 }
274
275 del_gendisk(device->blk_data.disk);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100276 device->blk_data.disk->private_data = NULL;
277 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 put_disk(device->blk_data.disk);
279
280 device->blk_data.disk = NULL;
281cleanup_queue:
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100282 device->blk_data.request_queue->queuedata = NULL;
283 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 blk_cleanup_queue(device->blk_data.request_queue);
286 device->blk_data.request_queue = NULL;
287}
288
289/*
290 * Detect number of blocks of the tape.
291 * FIXME: can we extent this to detect the blocks size as well ?
292 */
293static int
294tapeblock_revalidate_disk(struct gendisk *disk)
295{
296 struct tape_device * device;
297 unsigned int nr_of_blks;
298 int rc;
299
300 device = (struct tape_device *) disk->private_data;
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200301 BUG_ON(!device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (!device->blk_data.medium_changed)
304 return 0;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 rc = tape_mtop(device, MTFSFM, 1);
307 if (rc)
308 return rc;
309
310 rc = tape_mtop(device, MTTELL, 1);
311 if (rc < 0)
312 return rc;
313
Michael Holzheu59e36922009-09-11 10:29:07 +0200314 pr_info("%s: Determining the size of the recorded area...\n",
315 dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 DBF_LH(3, "Image file ends at %d\n", rc);
317 nr_of_blks = rc;
318
319 /* This will fail for the first file. Catch the error by checking the
320 * position. */
321 tape_mtop(device, MTBSF, 1);
322
323 rc = tape_mtop(device, MTTELL, 1);
324 if (rc < 0)
325 return rc;
326
327 if (rc > nr_of_blks)
328 return -EINVAL;
329
330 DBF_LH(3, "Image file starts at %d\n", rc);
331 device->bof = rc;
332 nr_of_blks -= rc;
333
Michael Holzheu59e36922009-09-11 10:29:07 +0200334 pr_info("%s: The size of the recorded area is %i blocks\n",
335 dev_name(&device->cdev->dev), nr_of_blks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 set_capacity(device->blk_data.disk,
337 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
338
339 device->blk_data.block_position = 0;
340 device->blk_data.medium_changed = 0;
341 return 0;
342}
343
Tejun Heoffe80ce2011-03-09 19:54:28 +0100344static unsigned int
345tapeblock_check_events(struct gendisk *disk, unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct tape_device *device;
348
349 device = (struct tape_device *) disk->private_data;
350 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
351 device, device->blk_data.medium_changed);
352
Tejun Heoffe80ce2011-03-09 19:54:28 +0100353 return device->blk_data.medium_changed ? DISK_EVENT_MEDIA_CHANGE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/*
357 * Block frontend tape device open function.
358 */
359static int
Al Viro4e999af2008-03-02 10:39:59 -0500360tapeblock_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Al Viro4e999af2008-03-02 10:39:59 -0500362 struct gendisk * disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct tape_device * device;
364 int rc;
365
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200366 mutex_lock(&tape_block_mutex);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100367 device = tape_get_device(disk->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 if (device->required_tapemarks) {
370 DBF_EVENT(2, "TBLOCK: missing tapemarks\n");
Michael Holzheu59e36922009-09-11 10:29:07 +0200371 pr_warning("%s: Opening the tape failed because of missing "
372 "end-of-file marks\n", dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 rc = -EPERM;
374 goto put_device;
375 }
376
377 rc = tape_open(device);
378 if (rc)
379 goto put_device;
380
381 rc = tapeblock_revalidate_disk(disk);
382 if (rc)
383 goto release;
384
385 /*
386 * Note: The reference to <device> is hold until the release function
387 * is called.
388 */
389 tape_state_set(device, TS_BLKUSE);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200390 mutex_unlock(&tape_block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392
393release:
394 tape_release(device);
395 put_device:
396 tape_put_device(device);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200397 mutex_unlock(&tape_block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return rc;
399}
400
401/*
402 * Block frontend tape device release function.
403 *
404 * Note: One reference to the tape device was made by the open function. So
405 * we just get the pointer here and release the reference.
406 */
407static int
Al Viro4e999af2008-03-02 10:39:59 -0500408tapeblock_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct tape_device *device = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200411
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200412 mutex_lock(&tape_block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 tape_state_set(device, TS_IN_USE);
414 tape_release(device);
415 tape_put_device(device);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200416 mutex_unlock(&tape_block_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 return 0;
419}
420
421/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * Initialize block device frontend.
423 */
424int
425tapeblock_init(void)
426{
427 int rc;
428
429 /* Register the tape major number to the kernel */
430 rc = register_blkdev(tapeblock_major, "tBLK");
431 if (rc < 0)
432 return rc;
433
434 if (tapeblock_major == 0)
435 tapeblock_major = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}
438
439/*
440 * Deregister major for block device frontend
441 */
442void
443tapeblock_exit(void)
444{
445 unregister_blkdev(tapeblock_major, "tBLK");
446}