1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * tape device driver for 3480/3490E/3590 tapes.
4 *
5 * S390 and zSeries version
6 * Copyright IBM Corp. 2001, 2009
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
13#ifndef _TAPE_H
14#define _TAPE_H
15
16#include <asm/ccwdev.h>
17#include <asm/debug.h>
18#include <asm/idals.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/mtio.h>
22#include <linux/interrupt.h>
23#include <linux/workqueue.h>
24
25struct gendisk;
26
27/*
28 * Define DBF_LIKE_HELL for lots of messages in the debug feature.
29 */
30#define DBF_LIKE_HELL
31#ifdef DBF_LIKE_HELL
32#define DBF_LH(level, str, ...) \
33do { \
34 debug_sprintf_event(TAPE_DBF_AREA, level, str, ## __VA_ARGS__); \
35} while (0)
36#else
37#define DBF_LH(level, str, ...) do {} while(0)
38#endif
39
40/*
41 * macros s390 debug feature (dbf)
42 */
43#define DBF_EVENT(d_level, d_str...) \
44do { \
45 debug_sprintf_event(TAPE_DBF_AREA, d_level, d_str); \
46} while (0)
47
48#define DBF_EXCEPTION(d_level, d_str...) \
49do { \
50 debug_sprintf_exception(TAPE_DBF_AREA, d_level, d_str); \
51} while (0)
52
53#define TAPE_VERSION_MAJOR 2
54#define TAPE_VERSION_MINOR 0
55#define TAPE_MAGIC "tape"
56
57#define TAPE_MINORS_PER_DEV 2 /* two minors per device */
58#define TAPEBLOCK_HSEC_SIZE 2048
59#define TAPEBLOCK_HSEC_S2B 2
60#define TAPEBLOCK_RETRIES 5
61
62enum tape_medium_state {
63 MS_UNKNOWN,
64 MS_LOADED,
65 MS_UNLOADED,
66 MS_SIZE
67};
68
69enum tape_state {
70 TS_UNUSED=0,
71 TS_IN_USE,
72 TS_BLKUSE,
73 TS_INIT,
74 TS_NOT_OPER,
75 TS_SIZE
76};
77
78enum tape_op {
79 TO_BLOCK, /* Block read */
80 TO_BSB, /* Backward space block */
81 TO_BSF, /* Backward space filemark */
82 TO_DSE, /* Data security erase */
83 TO_FSB, /* Forward space block */
84 TO_FSF, /* Forward space filemark */
85 TO_LBL, /* Locate block label */
86 TO_NOP, /* No operation */
87 TO_RBA, /* Read backward */
88 TO_RBI, /* Read block information */
89 TO_RFO, /* Read forward */
90 TO_REW, /* Rewind tape */
91 TO_RUN, /* Rewind and unload tape */
92 TO_WRI, /* Write block */
93 TO_WTM, /* Write tape mark */
94 TO_MSEN, /* Medium sense */
95 TO_LOAD, /* Load tape */
96 TO_READ_CONFIG, /* Read configuration data */
97 TO_READ_ATTMSG, /* Read attention message */
98 TO_DIS, /* Tape display */
99 TO_ASSIGN, /* Assign tape to channel path */
100 TO_UNASSIGN, /* Unassign tape from channel path */
101 TO_CRYPT_ON, /* Enable encrpytion */
102 TO_CRYPT_OFF, /* Disable encrpytion */
103 TO_KEKL_SET, /* Set KEK label */
104 TO_KEKL_QUERY, /* Query KEK label */
105 TO_RDC, /* Read device characteristics */
106 TO_SIZE, /* #entries in tape_op_t */
107};
108
109/* Forward declaration */
110struct tape_device;
111
112/* tape_request->status can be: */
113enum tape_request_status {
114 TAPE_REQUEST_INIT, /* request is ready to be processed */
115 TAPE_REQUEST_QUEUED, /* request is queued to be processed */
116 TAPE_REQUEST_IN_IO, /* request is currently in IO */
117 TAPE_REQUEST_DONE, /* request is completed. */
118 TAPE_REQUEST_CANCEL, /* request should be canceled. */
119 TAPE_REQUEST_LONG_BUSY, /* request has to be restarted after long busy */
120};
121
122/* Tape CCW request */
123struct tape_request {
124 struct list_head list; /* list head for request queueing. */
125 struct tape_device *device; /* tape device of this request */
126 struct ccw1 *cpaddr; /* address of the channel program. */
127 void *cpdata; /* pointer to ccw data. */
128 enum tape_request_status status;/* status of this request */
129 int options; /* options for execution. */
130 int retries; /* retry counter for error recovery. */
131 int rescnt; /* residual count from devstat. */
132 struct timer_list timer; /* timer for std_assign_timeout(). */
133 struct irb irb; /* device status */
134
135 /* Callback for delivering final status. */
136 void (*callback)(struct tape_request *, void *);
137 void *callback_data;
138
139 enum tape_op op;
140 int rc;
141};
142
143/* Function type for magnetic tape commands */
144typedef int (*tape_mtop_fn)(struct tape_device *, int);
145
146/* Size of the array containing the mtops for a discipline */
147#define TAPE_NR_MTOPS (MTMKPART+1)
148
149/* Tape Discipline */
150struct tape_discipline {
151 struct module *owner;
152 int (*setup_device)(struct tape_device *);
153 void (*cleanup_device)(struct tape_device *);
154 int (*irq)(struct tape_device *, struct tape_request *, struct irb *);
155 struct tape_request *(*read_block)(struct tape_device *);
156 struct tape_request *(*write_block)(struct tape_device *);
157 void (*process_eov)(struct tape_device*);
158 /* ioctl function for additional ioctls. */
159 int (*ioctl_fn)(struct tape_device *, unsigned int, unsigned long);
160 /* Array of tape commands with TAPE_NR_MTOPS entries */
161 tape_mtop_fn *mtop_array;
162};
163
164/*
165 * The discipline irq function either returns an error code (<0) which
166 * means that the request has failed with an error or one of the following:
167 */
168#define TAPE_IO_SUCCESS 0 /* request successful */
169#define TAPE_IO_PENDING 1 /* request still running */
170#define TAPE_IO_RETRY 2 /* retry to current request */
171#define TAPE_IO_STOP 3 /* stop the running request */
172#define TAPE_IO_LONG_BUSY 4 /* delay the running request */
173
174/* Char Frontend Data */
175struct tape_char_data {
176 struct idal_buffer **ibs; /* idal buffer array for user char data */
177 int block_size; /* of size block_size. */
178};
179
180/* Tape Info */
181struct tape_device {
182 /* entry in tape_device_list */
183 struct list_head node;
184
185 int cdev_id;
186 struct ccw_device * cdev;
187 struct tape_class_device * nt;
188 struct tape_class_device * rt;
189
190 /* Device mutex to serialize tape commands. */
191 struct mutex mutex;
192
193 /* Device discipline information. */
194 struct tape_discipline * discipline;
195 void * discdata;
196
197 /* Generic status flags */
198 long tape_generic_status;
199
200 /* Device state information. */
201 wait_queue_head_t state_change_wq;
202 enum tape_state tape_state;
203 enum tape_medium_state medium_state;
204 unsigned char * modeset_byte;
205
206 /* Reference count. */
207 atomic_t ref_count;
208
209 /* Request queue. */
210 struct list_head req_queue;
211
212 /* Request wait queue. */
213 wait_queue_head_t wait_queue;
214
215 /* Each tape device has (currently) two minor numbers. */
216 int first_minor;
217
218 /* Number of tapemarks required for correct termination. */
219 int required_tapemarks;
220
221 /* Block ID of the BOF */
222 unsigned int bof;
223
224 /* Character device frontend data */
225 struct tape_char_data char_data;
226
227 /* Function to start or stop the next request later. */
228 struct delayed_work tape_dnr;
229
230 /* Timer for long busy */
231 struct timer_list lb_timeout;
232
233};
234
235/* Externals from tape_core.c */
236extern struct tape_request *tape_alloc_request(int cplength, int datasize);
237extern void tape_free_request(struct tape_request *);
238extern int tape_check_idalbuffer(struct tape_device *device, size_t size);
239extern int tape_do_io(struct tape_device *, struct tape_request *);
240extern int tape_do_io_async(struct tape_device *, struct tape_request *);
241extern int tape_do_io_interruptible(struct tape_device *, struct tape_request *);
242extern int tape_cancel_io(struct tape_device *, struct tape_request *);
243
244static inline int
245tape_do_io_free(struct tape_device *device, struct tape_request *request)
246{
247 int rc;
248
249 rc = tape_do_io(device, request);
250 tape_free_request(request);
251 return rc;
252}
253
254static inline void
255tape_do_io_async_free(struct tape_device *device, struct tape_request *request)
256{
257 request->callback = (void *) tape_free_request;
258 request->callback_data = NULL;
259 tape_do_io_async(device, request);
260}
261
262extern int tape_open(struct tape_device *);
263extern int tape_release(struct tape_device *);
264extern int tape_mtop(struct tape_device *, int, int);
265extern void tape_state_set(struct tape_device *, enum tape_state);
266
267extern int tape_generic_online(struct tape_device *, struct tape_discipline *);
268extern int tape_generic_offline(struct ccw_device *);
269
270/* Externals from tape_devmap.c */
271extern int tape_generic_probe(struct ccw_device *);
272extern void tape_generic_remove(struct ccw_device *);
273
274extern struct tape_device *tape_find_device(int devindex);
275extern struct tape_device *tape_get_device(struct tape_device *);
276extern void tape_put_device(struct tape_device *);
277
278/* Externals from tape_char.c */
279extern int tapechar_init(void);
280extern void tapechar_exit(void);
281extern int tapechar_setup_device(struct tape_device *);
282extern void tapechar_cleanup_device(struct tape_device *);
283
284/* tape initialisation functions */
285#ifdef CONFIG_PROC_FS
286extern void tape_proc_init (void);
287extern void tape_proc_cleanup (void);
288#else
289static inline void tape_proc_init (void) {;}
290static inline void tape_proc_cleanup (void) {;}
291#endif
292
293/* a function for dumping device sense info */
294extern void tape_dump_sense_dbf(struct tape_device *, struct tape_request *,
295 struct irb *);
296
297/* functions for handling the status of a device */
298extern void tape_med_state_set(struct tape_device *, enum tape_medium_state);
299
300/* The debug area */
301extern debug_info_t *TAPE_DBF_AREA;
302
303/* functions for building ccws */
304static inline struct ccw1 *
305tape_ccw_cc(struct ccw1 *ccw, __u8 cmd_code, __u16 memsize, void *cda)
306{
307 ccw->cmd_code = cmd_code;
308 ccw->flags = CCW_FLAG_CC;
309 ccw->count = memsize;
310 ccw->cda = 0;
311 if (cda)
312 ccw->cda = virt_to_dma32(cda);
313 return ccw + 1;
314}
315
316static inline struct ccw1 *
317tape_ccw_end(struct ccw1 *ccw, __u8 cmd_code, __u16 memsize, void *cda)
318{
319 ccw->cmd_code = cmd_code;
320 ccw->flags = 0;
321 ccw->count = memsize;
322 ccw->cda = 0;
323 if (cda)
324 ccw->cda = virt_to_dma32(cda);
325 return ccw + 1;
326}
327
328static inline struct ccw1 *
329tape_ccw_cmd(struct ccw1 *ccw, __u8 cmd_code)
330{
331 ccw->cmd_code = cmd_code;
332 ccw->flags = 0;
333 ccw->count = 0;
334 ccw->cda = virt_to_dma32(&ccw->cmd_code);
335 return ccw + 1;
336}
337
338static inline struct ccw1 *
339tape_ccw_repeat(struct ccw1 *ccw, __u8 cmd_code, int count)
340{
341 while (count-- > 0) {
342 ccw->cmd_code = cmd_code;
343 ccw->flags = CCW_FLAG_CC;
344 ccw->count = 0;
345 ccw->cda = virt_to_dma32(&ccw->cmd_code);
346 ccw++;
347 }
348 return ccw;
349}
350
351static inline struct ccw1 *
352tape_ccw_dc_idal(struct ccw1 *ccw, __u8 cmd_code, struct idal_buffer *idal)
353{
354 ccw->cmd_code = cmd_code;
355 ccw->flags = CCW_FLAG_DC;
356 idal_buffer_set_cda(idal, ccw);
357 return ccw + 1;
358}
359
360static inline struct ccw1 *
361tape_ccw_cc_idal(struct ccw1 *ccw, __u8 cmd_code, struct idal_buffer *idal)
362{
363 ccw->cmd_code = cmd_code;
364 ccw->flags = CCW_FLAG_CC;
365 idal_buffer_set_cda(idal, ccw);
366 return ccw + 1;
367}
368
369static inline struct ccw1 *
370tape_ccw_end_idal(struct ccw1 *ccw, __u8 cmd_code, struct idal_buffer *idal)
371{
372 ccw->cmd_code = cmd_code;
373 ccw->flags = 0;
374 idal_buffer_set_cda(idal, ccw);
375 return ccw + 1;
376}
377
378/* Global vars */
379extern const char *tape_state_verbose[];
380extern const char *tape_op_verbose[];
381
382#endif /* for ifdef tape.h */
383

source code of linux/drivers/s390/char/tape.h