Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
ab640db0 | 13 | #define KMSG_COMPONENT "tape" |
bb509912 | 14 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
ab640db0 | 15 | |
1da177e4 | 16 | #include <linux/fs.h> |
1da177e4 LT |
17 | #include <linux/module.h> |
18 | #include <linux/blkdev.h> | |
2a48fc0a | 19 | #include <linux/mutex.h> |
1da177e4 LT |
20 | #include <linux/interrupt.h> |
21 | #include <linux/buffer_head.h> | |
c1637532 | 22 | #include <linux/kernel.h> |
1da177e4 LT |
23 | |
24 | #include <asm/debug.h> | |
25 | ||
26 | #define TAPE_DBF_AREA tape_core_dbf | |
27 | ||
28 | #include "tape.h" | |
29 | ||
1da177e4 LT |
30 | #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 | */ | |
2a48fc0a | 48 | static DEFINE_MUTEX(tape_block_mutex); |
4e999af9 AV |
49 | static int tapeblock_open(struct block_device *, fmode_t); |
50 | static int tapeblock_release(struct gendisk *, fmode_t); | |
ffe80cea | 51 | static unsigned int tapeblock_check_events(struct gendisk *, unsigned int); |
1da177e4 LT |
52 | static int tapeblock_revalidate_disk(struct gendisk *); |
53 | ||
83d5cde4 | 54 | static const struct block_device_operations tapeblock_fops = { |
1da177e4 | 55 | .owner = THIS_MODULE, |
4e999af9 AV |
56 | .open = tapeblock_open, |
57 | .release = tapeblock_release, | |
ffe80cea | 58 | .check_events = tapeblock_check_events, |
1da177e4 LT |
59 | .revalidate_disk = tapeblock_revalidate_disk, |
60 | }; | |
61 | ||
62 | static int tapeblock_major = 0; | |
63 | ||
64 | static void | |
65 | tapeblock_trigger_requeue(struct tape_device *device) | |
66 | { | |
67 | /* Protect against rescheduling. */ | |
973bd993 | 68 | if (atomic_cmpxchg(&device->blk_data.requeue_scheduled, 0, 1) != 0) |
1da177e4 LT |
69 | return; |
70 | schedule_work(&device->blk_data.requeue_task); | |
71 | } | |
72 | ||
73 | /* | |
74 | * Post finished request. | |
75 | */ | |
1da177e4 LT |
76 | static void |
77 | __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; | |
40cbbb78 | 86 | blk_end_request_all(req, (ccw_req->rc == 0) ? 0 : -EIO); |
1da177e4 LT |
87 | if (ccw_req->rc == 0) |
88 | /* Update position. */ | |
89 | device->blk_data.block_position = | |
83096ebf | 90 | (blk_rq_pos(req) + blk_rq_sectors(req)) >> TAPEBLOCK_HSEC_S2B; |
1da177e4 LT |
91 | 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) || | |
9934c8c0 | 96 | blk_peek_request(device->blk_data.request_queue)) |
1da177e4 LT |
97 | tapeblock_trigger_requeue(device); |
98 | } | |
99 | ||
100 | /* | |
101 | * Feed the tape device CCW queue with requests supplied in a list. | |
102 | */ | |
4d284cac | 103 | static int |
1da177e4 LT |
104 | tapeblock_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"); | |
40cbbb78 | 114 | blk_end_request_all(req, -EIO); |
1da177e4 LT |
115 | 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 | */ | |
40cbbb78 | 127 | blk_end_request_all(req, -EIO); |
1da177e4 LT |
128 | 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 | */ | |
138 | static void | |
c1637532 MS |
139 | tapeblock_requeue(struct work_struct *work) { |
140 | struct tape_blk_data * blkdat; | |
1da177e4 | 141 | struct tape_device * device; |
165125e1 | 142 | struct request_queue * queue; |
1da177e4 LT |
143 | int nr_queued; |
144 | struct request * req; | |
145 | struct list_head * l; | |
146 | int rc; | |
147 | ||
c1637532 MS |
148 | blkdat = container_of(work, struct tape_blk_data, requeue_task); |
149 | device = blkdat->device; | |
1da177e4 LT |
150 | 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 | ||
7a4a1ccd | 162 | spin_lock_irq(&device->blk_data.request_queue_lock); |
1da177e4 | 163 | while ( |
03cadd36 | 164 | blk_peek_request(queue) && |
1da177e4 LT |
165 | nr_queued < TAPEBLOCK_MIN_REQUEUE |
166 | ) { | |
03cadd36 | 167 | req = blk_fetch_request(queue); |
1da177e4 LT |
168 | if (rq_data_dir(req) == WRITE) { |
169 | DBF_EVENT(1, "TBLOCK: Rejecting write request\n"); | |
7a4a1ccd | 170 | spin_unlock_irq(&device->blk_data.request_queue_lock); |
40cbbb78 | 171 | blk_end_request_all(req, -EIO); |
7a4a1ccd | 172 | spin_lock_irq(&device->blk_data.request_queue_lock); |
1da177e4 LT |
173 | continue; |
174 | } | |
f71ad62a | 175 | nr_queued++; |
1da177e4 LT |
176 | 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); | |
1da177e4 LT |
179 | } |
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 | */ | |
187 | static void | |
165125e1 | 188 | tapeblock_request_fn(struct request_queue *queue) |
1da177e4 LT |
189 | { |
190 | struct tape_device *device; | |
191 | ||
192 | device = (struct tape_device *) queue->queuedata; | |
193 | DBF_LH(6, "tapeblock_request_fn(device=%p)\n", device); | |
3a8dc893 | 194 | BUG_ON(device == NULL); |
1da177e4 LT |
195 | tapeblock_trigger_requeue(device); |
196 | } | |
197 | ||
198 | /* | |
199 | * This function is called for every new tapedevice | |
200 | */ | |
201 | int | |
202 | tapeblock_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; | |
c1637532 | 209 | blkdat->device = device; |
1da177e4 LT |
210 | 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 | ||
52cc2eef | 220 | rc = elevator_change(blkdat->request_queue, "noop"); |
1da177e4 LT |
221 | if (rc) |
222 | goto cleanup_queue; | |
223 | ||
e1defc4f | 224 | blk_queue_logical_block_size(blkdat->request_queue, TAPEBLOCK_HSEC_SIZE); |
086fa5ff | 225 | blk_queue_max_hw_sectors(blkdat->request_queue, TAPEBLOCK_MAX_SEC); |
8a78362c | 226 | blk_queue_max_segments(blkdat->request_queue, -1L); |
1da177e4 LT |
227 | 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; | |
8fd138c3 | 239 | disk->private_data = tape_get_device(device); |
1da177e4 LT |
240 | 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; | |
8fd138c3 | 247 | blkdat->request_queue->queuedata = tape_get_device(device); |
1da177e4 LT |
248 | |
249 | add_disk(disk); | |
250 | ||
8fd138c3 | 251 | tape_get_device(device); |
c1637532 | 252 | INIT_WORK(&blkdat->requeue_task, tapeblock_requeue); |
1da177e4 LT |
253 | |
254 | return 0; | |
255 | ||
256 | cleanup_queue: | |
257 | blk_cleanup_queue(blkdat->request_queue); | |
258 | blkdat->request_queue = NULL; | |
259 | ||
260 | return rc; | |
261 | } | |
262 | ||
263 | void | |
264 | tapeblock_cleanup_device(struct tape_device *device) | |
265 | { | |
6dca467a | 266 | flush_work_sync(&device->blk_data.requeue_task); |
c1637532 | 267 | tape_put_device(device); |
1da177e4 LT |
268 | |
269 | if (!device->blk_data.disk) { | |
1da177e4 LT |
270 | goto cleanup_queue; |
271 | } | |
272 | ||
273 | del_gendisk(device->blk_data.disk); | |
8fd138c3 MS |
274 | device->blk_data.disk->private_data = NULL; |
275 | tape_put_device(device); | |
1da177e4 LT |
276 | put_disk(device->blk_data.disk); |
277 | ||
278 | device->blk_data.disk = NULL; | |
279 | cleanup_queue: | |
8fd138c3 MS |
280 | device->blk_data.request_queue->queuedata = NULL; |
281 | tape_put_device(device); | |
1da177e4 LT |
282 | |
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 | */ | |
291 | static int | |
292 | tapeblock_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; | |
3a8dc893 | 299 | BUG_ON(!device); |
1da177e4 LT |
300 | |
301 | if (!device->blk_data.medium_changed) | |
302 | return 0; | |
303 | ||
1da177e4 LT |
304 | 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 | ||
59e36927 MH |
312 | pr_info("%s: Determining the size of the recorded area...\n", |
313 | dev_name(&device->cdev->dev)); | |
1da177e4 LT |
314 | 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 | ||
59e36927 MH |
332 | pr_info("%s: The size of the recorded area is %i blocks\n", |
333 | dev_name(&device->cdev->dev), nr_of_blks); | |
1da177e4 LT |
334 | 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 | ||
ffe80cea TH |
342 | static unsigned int |
343 | tapeblock_check_events(struct gendisk *disk, unsigned int clearing) | |
1da177e4 LT |
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 | ||
ffe80cea | 351 | return device->blk_data.medium_changed ? DISK_EVENT_MEDIA_CHANGE : 0; |
1da177e4 LT |
352 | } |
353 | ||
354 | /* | |
355 | * Block frontend tape device open function. | |
356 | */ | |
357 | static int | |
4e999af9 | 358 | tapeblock_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 359 | { |
4e999af9 | 360 | struct gendisk * disk = bdev->bd_disk; |
1da177e4 LT |
361 | struct tape_device * device; |
362 | int rc; | |
363 | ||
2a48fc0a | 364 | mutex_lock(&tape_block_mutex); |
8fd138c3 | 365 | device = tape_get_device(disk->private_data); |
1da177e4 LT |
366 | |
367 | if (device->required_tapemarks) { | |
368 | DBF_EVENT(2, "TBLOCK: missing tapemarks\n"); | |
59e36927 MH |
369 | pr_warning("%s: Opening the tape failed because of missing " |
370 | "end-of-file marks\n", dev_name(&device->cdev->dev)); | |
1da177e4 LT |
371 | 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); | |
2a48fc0a | 388 | mutex_unlock(&tape_block_mutex); |
1da177e4 LT |
389 | return 0; |
390 | ||
391 | release: | |
392 | tape_release(device); | |
393 | put_device: | |
394 | tape_put_device(device); | |
2a48fc0a | 395 | mutex_unlock(&tape_block_mutex); |
1da177e4 LT |
396 | 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 | */ | |
405 | static int | |
4e999af9 | 406 | tapeblock_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 407 | { |
1da177e4 | 408 | struct tape_device *device = disk->private_data; |
6e9624b8 | 409 | |
2a48fc0a | 410 | mutex_lock(&tape_block_mutex); |
1da177e4 LT |
411 | tape_state_set(device, TS_IN_USE); |
412 | tape_release(device); | |
413 | tape_put_device(device); | |
2a48fc0a | 414 | mutex_unlock(&tape_block_mutex); |
1da177e4 LT |
415 | |
416 | return 0; | |
417 | } | |
418 | ||
1da177e4 LT |
419 | /* |
420 | * Initialize block device frontend. | |
421 | */ | |
422 | int | |
423 | tapeblock_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; | |
1da177e4 LT |
434 | return 0; |
435 | } | |
436 | ||
437 | /* | |
438 | * Deregister major for block device frontend | |
439 | */ | |
440 | void | |
441 | tapeblock_exit(void) | |
442 | { | |
443 | unregister_blkdev(tapeblock_major, "tBLK"); | |
444 | } |