blob: 85cf607fc78f62d243ae41b20f2df018710be2e0 [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 Bergmann6e9624b2010-08-07 18:25:34 +020019#include <linux/smp_lock.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 */
Al Viro4e999af2008-03-02 10:39:59 -050048static int tapeblock_open(struct block_device *, fmode_t);
49static int tapeblock_release(struct gendisk *, fmode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static int tapeblock_medium_changed(struct gendisk *);
51static int tapeblock_revalidate_disk(struct gendisk *);
52
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070053static const struct block_device_operations tapeblock_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .owner = THIS_MODULE,
Al Viro4e999af2008-03-02 10:39:59 -050055 .open = tapeblock_open,
56 .release = tapeblock_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .media_changed = tapeblock_medium_changed,
58 .revalidate_disk = tapeblock_revalidate_disk,
59};
60
61static int tapeblock_major = 0;
62
63static void
64tapeblock_trigger_requeue(struct tape_device *device)
65{
66 /* Protect against rescheduling. */
Martin Schwidefsky973bd992006-01-06 00:19:07 -080067 if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return;
69 schedule_work(&device->blk_data.requeue_task);
70}
71
72/*
73 * Post finished request.
74 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +010075static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070076__tapeblock_end_request(struct tape_request *ccw_req, void *data)
77{
78 struct tape_device *device;
79 struct request *req;
80
81 DBF_LH(6, "__tapeblock_end_request()\n");
82
83 device = ccw_req->device;
84 req = (struct request *) data;
Tejun Heo40cbbb72009-04-23 11:05:19 +090085 blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (ccw_req->rc == 0)
87 /* Update position. */
88 device->blk_data.block_position =
Tejun Heo83096eb2009-05-07 22:24:39 +090089 (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 else
91 /* We lost the position information due to an error. */
92 device->blk_data.block_position = -1;
93 device->discipline->free_bread(ccw_req);
94 if (!list_empty(&device->req_queue) ||
Tejun Heo9934c8c2009-05-08 11:54:16 +090095 blk_peek_request(device->blk_data.request_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 tapeblock_trigger_requeue(device);
97}
98
99/*
100 * Feed the tape device CCW queue with requests supplied in a list.
101 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100102static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103tapeblock_start_request(struct tape_device *device, struct request *req)
104{
105 struct tape_request * ccw_req;
106 int rc;
107
108 DBF_LH(6, "tapeblock_start_request(%p, %p)\n", device, req);
109
110 ccw_req = device->discipline->bread(device, req);
111 if (IS_ERR(ccw_req)) {
112 DBF_EVENT(1, "TBLOCK: bread failed\n");
Tejun Heo40cbbb72009-04-23 11:05:19 +0900113 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return PTR_ERR(ccw_req);
115 }
116 ccw_req->callback = __tapeblock_end_request;
117 ccw_req->callback_data = (void *) req;
118 ccw_req->retries = TAPEBLOCK_RETRIES;
119
120 rc = tape_do_io_async(device, ccw_req);
121 if (rc) {
122 /*
123 * Start/enqueueing failed. No retries in
124 * this case.
125 */
Tejun Heo40cbbb72009-04-23 11:05:19 +0900126 blk_end_request_all(req, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 device->discipline->free_bread(ccw_req);
128 }
129
130 return rc;
131}
132
133/*
134 * Move requests from the block device request queue to the tape device ccw
135 * queue.
136 */
137static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100138tapeblock_requeue(struct work_struct *work) {
139 struct tape_blk_data * blkdat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct tape_device * device;
Jens Axboe165125e2007-07-24 09:28:11 +0200141 struct request_queue * queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int nr_queued;
143 struct request * req;
144 struct list_head * l;
145 int rc;
146
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100147 blkdat = container_of(work, struct tape_blk_data, requeue_task);
148 device = blkdat->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!device)
150 return;
151
152 spin_lock_irq(get_ccwdev_lock(device->cdev));
153 queue = device->blk_data.request_queue;
154
155 /* Count number of requests on ccw queue. */
156 nr_queued = 0;
157 list_for_each(l, &device->req_queue)
158 nr_queued++;
159 spin_unlock(get_ccwdev_lock(device->cdev));
160
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100161 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 while (
163 !blk_queue_plugged(queue) &&
Michael Holzheu03cadd32009-10-14 12:43:45 +0200164 blk_peek_request(queue) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 nr_queued < TAPEBLOCK_MIN_REQUEUE
166 ) {
Michael Holzheu03cadd32009-10-14 12:43:45 +0200167 req = blk_fetch_request(queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (rq_data_dir(req) == WRITE) {
169 DBF_EVENT(1, "TBLOCK: Rejecting write request\n");
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100170 spin_unlock_irq(&device->blk_data.request_queue_lock);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900171 blk_end_request_all(req, -EIO);
Frank Munzert7a4a1cc2008-10-28 11:10:17 +0100172 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 continue;
174 }
Michael Holzheuf71ad622008-05-30 10:03:25 +0200175 nr_queued++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 spin_unlock_irq(&device->blk_data.request_queue_lock);
177 rc = tapeblock_start_request(device, req);
178 spin_lock_irq(&device->blk_data.request_queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 spin_unlock_irq(&device->blk_data.request_queue_lock);
181 atomic_set(&device->blk_data.requeue_scheduled, 0);
182}
183
184/*
185 * Tape request queue function. Called from ll_rw_blk.c
186 */
187static void
Jens Axboe165125e2007-07-24 09:28:11 +0200188tapeblock_request_fn(struct request_queue *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct tape_device *device;
191
192 device = (struct tape_device *) queue->queuedata;
193 DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device);
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200194 BUG_ON(device == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 tapeblock_trigger_requeue(device);
196}
197
198/*
199 * This function is called for every new tapedevice
200 */
201int
202tapeblock_setup_device(struct tape_device * device)
203{
204 struct tape_blk_data * blkdat;
205 struct gendisk * disk;
206 int rc;
207
208 blkdat = &device->blk_data;
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100209 blkdat->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 spin_lock_init(&blkdat->request_queue_lock);
211 atomic_set(&blkdat->requeue_scheduled, 0);
212
213 blkdat->request_queue = blk_init_queue(
214 tapeblock_request_fn,
215 &blkdat->request_queue_lock
216 );
217 if (!blkdat->request_queue)
218 return -ENOMEM;
219
Jens Axboe52cc2ee2010-08-23 14:02:44 +0200220 rc = elevator_change(blkdat->request_queue, "noop");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 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);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500225 blk_queue_max_hw_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500226 blk_queue_max_segments(blkdat->request_queue, -1L);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 blk_queue_max_segment_size(blkdat->request_queue, -1L);
228 blk_queue_segment_boundary(blkdat->request_queue, -1L);
229
230 disk = alloc_disk(1);
231 if (!disk) {
232 rc = -ENOMEM;
233 goto cleanup_queue;
234 }
235
236 disk->major = tapeblock_major;
237 disk->first_minor = device->first_minor;
238 disk->fops = &tapeblock_fops;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100239 disk->private_data = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 disk->queue = blkdat->request_queue;
241 set_capacity(disk, 0);
242 sprintf(disk->disk_name, "btibm%d",
243 device->first_minor / TAPE_MINORS_PER_DEV);
244
245 blkdat->disk = disk;
246 blkdat->medium_changed = 1;
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100247 blkdat->request_queue->queuedata = tape_get_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 add_disk(disk);
250
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100251 tape_get_device(device);
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100252 INIT_WORK(&blkdat->requeue_task, tapeblock_requeue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return 0;
255
256cleanup_queue:
257 blk_cleanup_queue(blkdat->request_queue);
258 blkdat->request_queue = NULL;
259
260 return rc;
261}
262
263void
264tapeblock_cleanup_device(struct tape_device *device)
265{
266 flush_scheduled_work();
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100267 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (!device->blk_data.disk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto cleanup_queue;
271 }
272
273 del_gendisk(device->blk_data.disk);
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100274 device->blk_data.disk->private_data = NULL;
275 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 put_disk(device->blk_data.disk);
277
278 device->blk_data.disk = NULL;
279cleanup_queue:
Martin Schwidefsky8fd138c2009-12-07 12:52:03 +0100280 device->blk_data.request_queue->queuedata = NULL;
281 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 blk_cleanup_queue(device->blk_data.request_queue);
284 device->blk_data.request_queue = NULL;
285}
286
287/*
288 * Detect number of blocks of the tape.
289 * FIXME: can we extent this to detect the blocks size as well ?
290 */
291static int
292tapeblock_revalidate_disk(struct gendisk *disk)
293{
294 struct tape_device * device;
295 unsigned int nr_of_blks;
296 int rc;
297
298 device = (struct tape_device *) disk->private_data;
Eric Sesterhenn3a8dc892006-04-01 01:28:11 +0200299 BUG_ON(!device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (!device->blk_data.medium_changed)
302 return 0;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 rc = tape_mtop(device, MTFSFM, 1);
305 if (rc)
306 return rc;
307
308 rc = tape_mtop(device, MTTELL, 1);
309 if (rc < 0)
310 return rc;
311
Michael Holzheu59e36922009-09-11 10:29:07 +0200312 pr_info("%s: Determining the size of the recorded area...\n",
313 dev_name(&device->cdev->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 DBF_LH(3, "Image file ends at %d\n", rc);
315 nr_of_blks = rc;
316
317 /* This will fail for the first file. Catch the error by checking the
318 * position. */
319 tape_mtop(device, MTBSF, 1);
320
321 rc = tape_mtop(device, MTTELL, 1);
322 if (rc < 0)
323 return rc;
324
325 if (rc > nr_of_blks)
326 return -EINVAL;
327
328 DBF_LH(3, "Image file starts at %d\n", rc);
329 device->bof = rc;
330 nr_of_blks -= rc;
331
Michael Holzheu59e36922009-09-11 10:29:07 +0200332 pr_info("%s: The size of the recorded area is %i blocks\n",
333 dev_name(&device->cdev->dev), nr_of_blks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 set_capacity(device->blk_data.disk,
335 nr_of_blks*(TAPEBLOCK_HSEC_SIZE/512));
336
337 device->blk_data.block_position = 0;
338 device->blk_data.medium_changed = 0;
339 return 0;
340}
341
342static int
343tapeblock_medium_changed(struct gendisk *disk)
344{
345 struct tape_device *device;
346
347 device = (struct tape_device *) disk->private_data;
348 DBF_LH(6, "tapeblock_medium_changed(%p) = %d\n",
349 device, device->blk_data.medium_changed);
350
351 return device->blk_data.medium_changed;
352}
353
354/*
355 * Block frontend tape device open function.
356 */
357static int
Al Viro4e999af2008-03-02 10:39:59 -0500358tapeblock_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Al Viro4e999af2008-03-02 10:39:59 -0500360 struct gendisk * disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 struct tape_device * device;
362 int rc;
363
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200364 lock_kernel();
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);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200388 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390
391release:
392 tape_release(device);
393 put_device:
394 tape_put_device(device);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200395 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return rc;
397}
398
399/*
400 * Block frontend tape device release function.
401 *
402 * Note: One reference to the tape device was made by the open function. So
403 * we just get the pointer here and release the reference.
404 */
405static int
Al Viro4e999af2008-03-02 10:39:59 -0500406tapeblock_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct tape_device *device = disk->private_data;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200409
410 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 tape_state_set(device, TS_IN_USE);
412 tape_release(device);
413 tape_put_device(device);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200414 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return 0;
417}
418
419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * Initialize block device frontend.
421 */
422int
423tapeblock_init(void)
424{
425 int rc;
426
427 /* Register the tape major number to the kernel */
428 rc = register_blkdev(tapeblock_major, "tBLK");
429 if (rc < 0)
430 return rc;
431
432 if (tapeblock_major == 0)
433 tapeblock_major = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
437/*
438 * Deregister major for block device frontend
439 */
440void
441tapeblock_exit(void)
442{
443 unregister_blkdev(tapeblock_major, "tBLK");
444}