blob: b47e20d3d1da9a2dad66d7034ce79ff218f5aad7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/char/tape_char.c
3 * character device frontend for tape device driver
4 *
5 * S390 and zSeries version
Michael Holzheucced1dd2007-02-05 21:18:26 +01006 * Copyright IBM Corp. 2001,2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Michael Holzheu <holzheu@de.ibm.com>
9 * Tuan Ngo-Anh <ngoanh@de.ibm.com>
10 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/proc_fs.h>
16#include <linux/mtio.h>
Jonathan Corbet764a4a82008-05-15 10:01:17 -060017#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/uaccess.h>
20
21#define TAPE_DBF_AREA tape_core_dbf
22
23#include "tape.h"
24#include "tape_std.h"
25#include "tape_class.h"
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define TAPECHAR_MAJOR 0 /* get dynamic major */
28
29/*
30 * file operation structure for tape character frontend
31 */
32static ssize_t tapechar_read(struct file *, char __user *, size_t, loff_t *);
33static ssize_t tapechar_write(struct file *, const char __user *, size_t, loff_t *);
34static int tapechar_open(struct inode *,struct file *);
35static int tapechar_release(struct inode *,struct file *);
36static int tapechar_ioctl(struct inode *, struct file *, unsigned int,
37 unsigned long);
Christoph Hellwigf042e0f2006-01-09 20:52:07 -080038static long tapechar_compat_ioctl(struct file *, unsigned int,
39 unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Arjan van de Vend54b1fd2007-02-12 00:55:34 -080041static const struct file_operations tape_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 .owner = THIS_MODULE,
44 .read = tapechar_read,
45 .write = tapechar_write,
46 .ioctl = tapechar_ioctl,
Christoph Hellwigf042e0f2006-01-09 20:52:07 -080047 .compat_ioctl = tapechar_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .open = tapechar_open,
49 .release = tapechar_release,
50};
51
52static int tapechar_major = TAPECHAR_MAJOR;
53
54/*
55 * This function is called for every new tapedevice
56 */
57int
58tapechar_setup_device(struct tape_device * device)
59{
60 char device_name[20];
61
62 sprintf(device_name, "ntibm%i", device->first_minor / 2);
63 device->nt = register_tape_dev(
64 &device->cdev->dev,
65 MKDEV(tapechar_major, device->first_minor),
66 &tape_fops,
67 device_name,
68 "non-rewinding"
69 );
70 device_name[0] = 'r';
71 device->rt = register_tape_dev(
72 &device->cdev->dev,
73 MKDEV(tapechar_major, device->first_minor + 1),
74 &tape_fops,
75 device_name,
76 "rewinding"
77 );
78
79 return 0;
80}
81
82void
83tapechar_cleanup_device(struct tape_device *device)
84{
Michael Holzheu92bf4352008-04-17 07:46:05 +020085 unregister_tape_dev(&device->cdev->dev, device->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 device->rt = NULL;
Michael Holzheu92bf4352008-04-17 07:46:05 +020087 unregister_tape_dev(&device->cdev->dev, device->nt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 device->nt = NULL;
89}
90
Heiko Carstens4d284ca2007-02-05 21:18:53 +010091static int
Linus Torvalds1da177e2005-04-16 15:20:36 -070092tapechar_check_idalbuffer(struct tape_device *device, size_t block_size)
93{
94 struct idal_buffer *new;
95
96 if (device->char_data.idal_buf != NULL &&
97 device->char_data.idal_buf->size == block_size)
98 return 0;
99
100 if (block_size > MAX_BLOCKSIZE) {
101 DBF_EVENT(3, "Invalid blocksize (%zd > %d)\n",
102 block_size, MAX_BLOCKSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -EINVAL;
104 }
105
106 /* The current idal buffer is not correct. Allocate a new one. */
107 new = idal_buffer_alloc(block_size, 0);
Julien Brunel0983e562008-08-21 19:46:30 +0200108 if (IS_ERR(new))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -ENOMEM;
110
111 if (device->char_data.idal_buf != NULL)
112 idal_buffer_free(device->char_data.idal_buf);
113
114 device->char_data.idal_buf = new;
115
116 return 0;
117}
118
119/*
120 * Tape device read function
121 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100122static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123tapechar_read(struct file *filp, char __user *data, size_t count, loff_t *ppos)
124{
125 struct tape_device *device;
126 struct tape_request *request;
127 size_t block_size;
128 int rc;
129
130 DBF_EVENT(6, "TCHAR:read\n");
131 device = (struct tape_device *) filp->private_data;
132
133 /*
134 * If the tape isn't terminated yet, do it now. And since we then
135 * are at the end of the tape there wouldn't be anything to read
136 * anyways. So we return immediatly.
137 */
138 if(device->required_tapemarks) {
139 return tape_std_terminate_write(device);
140 }
141
142 /* Find out block size to use */
143 if (device->char_data.block_size != 0) {
144 if (count < device->char_data.block_size) {
145 DBF_EVENT(3, "TCHAR:read smaller than block "
146 "size was requested\n");
147 return -EINVAL;
148 }
149 block_size = device->char_data.block_size;
150 } else {
151 block_size = count;
152 }
153
154 rc = tapechar_check_idalbuffer(device, block_size);
155 if (rc)
156 return rc;
157
158#ifdef CONFIG_S390_TAPE_BLOCK
159 /* Changes position. */
160 device->blk_data.medium_changed = 1;
161#endif
162
163 DBF_EVENT(6, "TCHAR:nbytes: %lx\n", block_size);
164 /* Let the discipline build the ccw chain. */
165 request = device->discipline->read_block(device, block_size);
166 if (IS_ERR(request))
167 return PTR_ERR(request);
168 /* Execute it. */
169 rc = tape_do_io(device, request);
170 if (rc == 0) {
171 rc = block_size - request->rescnt;
172 DBF_EVENT(6, "TCHAR:rbytes: %x\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* Copy data from idal buffer to user space. */
174 if (idal_buffer_to_user(device->char_data.idal_buf,
175 data, rc) != 0)
176 rc = -EFAULT;
177 }
178 tape_free_request(request);
179 return rc;
180}
181
182/*
183 * Tape device write function
184 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100185static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186tapechar_write(struct file *filp, const char __user *data, size_t count, loff_t *ppos)
187{
188 struct tape_device *device;
189 struct tape_request *request;
190 size_t block_size;
191 size_t written;
192 int nblocks;
193 int i, rc;
194
195 DBF_EVENT(6, "TCHAR:write\n");
196 device = (struct tape_device *) filp->private_data;
197 /* Find out block size and number of blocks */
198 if (device->char_data.block_size != 0) {
199 if (count < device->char_data.block_size) {
200 DBF_EVENT(3, "TCHAR:write smaller than block "
201 "size was requested\n");
202 return -EINVAL;
203 }
204 block_size = device->char_data.block_size;
205 nblocks = count / block_size;
206 } else {
207 block_size = count;
208 nblocks = 1;
209 }
210
211 rc = tapechar_check_idalbuffer(device, block_size);
212 if (rc)
213 return rc;
214
215#ifdef CONFIG_S390_TAPE_BLOCK
216 /* Changes position. */
217 device->blk_data.medium_changed = 1;
218#endif
219
220 DBF_EVENT(6,"TCHAR:nbytes: %lx\n", block_size);
221 DBF_EVENT(6, "TCHAR:nblocks: %x\n", nblocks);
222 /* Let the discipline build the ccw chain. */
223 request = device->discipline->write_block(device, block_size);
224 if (IS_ERR(request))
225 return PTR_ERR(request);
226 rc = 0;
227 written = 0;
228 for (i = 0; i < nblocks; i++) {
229 /* Copy data from user space to idal buffer. */
230 if (idal_buffer_from_user(device->char_data.idal_buf,
231 data, block_size)) {
232 rc = -EFAULT;
233 break;
234 }
235 rc = tape_do_io(device, request);
236 if (rc)
237 break;
238 DBF_EVENT(6, "TCHAR:wbytes: %lx\n",
239 block_size - request->rescnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 written += block_size - request->rescnt;
241 if (request->rescnt != 0)
242 break;
243 data += block_size;
244 }
245 tape_free_request(request);
246 if (rc == -ENOSPC) {
247 /*
248 * Ok, the device has no more space. It has NOT written
249 * the block.
250 */
251 if (device->discipline->process_eov)
252 device->discipline->process_eov(device);
253 if (written > 0)
254 rc = 0;
255
256 }
257
258 /*
259 * After doing a write we always need two tapemarks to correctly
260 * terminate the tape (one to terminate the file, the second to
261 * flag the end of recorded data.
262 * Since process_eov positions the tape in front of the written
263 * tapemark it doesn't hurt to write two marks again.
264 */
265 if (!rc)
266 device->required_tapemarks = 2;
267
268 return rc ? rc : written;
269}
270
271/*
272 * Character frontend tape device open function.
273 */
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100274static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275tapechar_open (struct inode *inode, struct file *filp)
276{
277 struct tape_device *device;
278 int minor, rc;
279
280 DBF_EVENT(6, "TCHAR:open: %i:%i\n",
Josef Sipek49522c92006-12-08 02:37:34 -0800281 imajor(filp->f_path.dentry->d_inode),
282 iminor(filp->f_path.dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Josef Sipek49522c92006-12-08 02:37:34 -0800284 if (imajor(filp->f_path.dentry->d_inode) != tapechar_major)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -ENODEV;
286
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600287 lock_kernel();
Josef Sipek49522c92006-12-08 02:37:34 -0800288 minor = iminor(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 device = tape_get_device(minor / TAPE_MINORS_PER_DEV);
290 if (IS_ERR(device)) {
291 DBF_EVENT(3, "TCHAR:open: tape_get_device() failed\n");
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600292 rc = PTR_ERR(device);
293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
296
297 rc = tape_open(device);
298 if (rc == 0) {
299 filp->private_data = device;
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600300 rc = nonseekable_open(inode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600302 else
303 tape_put_device(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Jonathan Corbet764a4a82008-05-15 10:01:17 -0600305out:
306 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return rc;
308}
309
310/*
311 * Character frontend tape device release function.
312 */
313
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100314static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315tapechar_release(struct inode *inode, struct file *filp)
316{
317 struct tape_device *device;
318
319 DBF_EVENT(6, "TCHAR:release: %x\n", iminor(inode));
320 device = (struct tape_device *) filp->private_data;
321
322 /*
323 * If this is the rewinding tape minor then rewind. In that case we
324 * write all required tapemarks. Otherwise only one to terminate the
325 * file.
326 */
327 if ((iminor(inode) & 1) != 0) {
328 if (device->required_tapemarks)
329 tape_std_terminate_write(device);
330 tape_mtop(device, MTREW, 1);
331 } else {
332 if (device->required_tapemarks > 1) {
333 if (tape_mtop(device, MTWEOF, 1) == 0)
334 device->required_tapemarks--;
335 }
336 }
337
338 if (device->char_data.idal_buf != NULL) {
339 idal_buffer_free(device->char_data.idal_buf);
340 device->char_data.idal_buf = NULL;
341 }
342 tape_release(device);
343 filp->private_data = tape_put_device(device);
344
345 return 0;
346}
347
348/*
349 * Tape device io controls.
350 */
351static int
352tapechar_ioctl(struct inode *inp, struct file *filp,
353 unsigned int no, unsigned long data)
354{
355 struct tape_device *device;
356 int rc;
357
358 DBF_EVENT(6, "TCHAR:ioct\n");
359
360 device = (struct tape_device *) filp->private_data;
361
362 if (no == MTIOCTOP) {
363 struct mtop op;
364
365 if (copy_from_user(&op, (char __user *) data, sizeof(op)) != 0)
366 return -EFAULT;
367 if (op.mt_count < 0)
368 return -EINVAL;
369
370 /*
371 * Operations that change tape position should write final
372 * tapemarks.
373 */
374 switch (op.mt_op) {
375 case MTFSF:
376 case MTBSF:
377 case MTFSR:
378 case MTBSR:
379 case MTREW:
380 case MTOFFL:
381 case MTEOM:
382 case MTRETEN:
383 case MTBSFM:
384 case MTFSFM:
385 case MTSEEK:
386#ifdef CONFIG_S390_TAPE_BLOCK
387 device->blk_data.medium_changed = 1;
388#endif
389 if (device->required_tapemarks)
390 tape_std_terminate_write(device);
391 default:
392 ;
393 }
394 rc = tape_mtop(device, op.mt_op, op.mt_count);
395
396 if (op.mt_op == MTWEOF && rc == 0) {
397 if (op.mt_count > device->required_tapemarks)
398 device->required_tapemarks = 0;
399 else
400 device->required_tapemarks -= op.mt_count;
401 }
402 return rc;
403 }
404 if (no == MTIOCPOS) {
405 /* MTIOCPOS: query the tape position. */
406 struct mtpos pos;
407
408 rc = tape_mtop(device, MTTELL, 1);
409 if (rc < 0)
410 return rc;
411 pos.mt_blkno = rc;
412 if (copy_to_user((char __user *) data, &pos, sizeof(pos)) != 0)
413 return -EFAULT;
414 return 0;
415 }
416 if (no == MTIOCGET) {
417 /* MTIOCGET: query the tape drive status. */
418 struct mtget get;
419
420 memset(&get, 0, sizeof(get));
421 get.mt_type = MT_ISUNKNOWN;
422 get.mt_resid = 0 /* device->devstat.rescnt */;
423 get.mt_dsreg = device->tape_state;
424 /* FIXME: mt_gstat, mt_erreg, mt_fileno */
425 get.mt_gstat = 0;
426 get.mt_erreg = 0;
427 get.mt_fileno = 0;
428 get.mt_gstat = device->tape_generic_status;
429
430 if (device->medium_state == MS_LOADED) {
431 rc = tape_mtop(device, MTTELL, 1);
432
433 if (rc < 0)
434 return rc;
435
436 if (rc == 0)
437 get.mt_gstat |= GMT_BOT(~0);
438
439 get.mt_blkno = rc;
440 }
441
442 if (copy_to_user((char __user *) data, &get, sizeof(get)) != 0)
443 return -EFAULT;
444
445 return 0;
446 }
447 /* Try the discipline ioctl function. */
448 if (device->discipline->ioctl_fn == NULL)
449 return -EINVAL;
450 return device->discipline->ioctl_fn(device, no, data);
451}
452
Christoph Hellwigf042e0f2006-01-09 20:52:07 -0800453static long
454tapechar_compat_ioctl(struct file *filp, unsigned int no, unsigned long data)
455{
456 struct tape_device *device = filp->private_data;
457 int rval = -ENOIOCTLCMD;
458
459 if (device->discipline->ioctl_fn) {
460 lock_kernel();
461 rval = device->discipline->ioctl_fn(device, no, data);
462 unlock_kernel();
463 if (rval == -EINVAL)
464 rval = -ENOIOCTLCMD;
465 }
466
467 return rval;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
471 * Initialize character device frontend.
472 */
473int
474tapechar_init (void)
475{
476 dev_t dev;
477
478 if (alloc_chrdev_region(&dev, 0, 256, "tape") != 0)
479 return -1;
480
481 tapechar_major = MAJOR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 return 0;
484}
485
486/*
487 * cleanup
488 */
489void
490tapechar_exit(void)
491{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unregister_chrdev_region(MKDEV(tapechar_major, 0), 256);
493}