xref: /linux/drivers/s390/char/raw3270.c (revision 3f0a50f345f78183f6e9b39c2f45ca5dcaa511ca)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * IBM/3270 Driver - core functions.
4  *
5  * Author(s):
6  *   Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
7  *   Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
8  *     Copyright IBM Corp. 2003, 2009
9  */
10 
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/wait.h>
19 
20 #include <asm/ccwdev.h>
21 #include <asm/cio.h>
22 #include <asm/ebcdic.h>
23 #include <asm/diag.h>
24 
25 #include "raw3270.h"
26 
27 #include <linux/major.h>
28 #include <linux/kdev_t.h>
29 #include <linux/device.h>
30 #include <linux/mutex.h>
31 
32 struct class *class3270;
33 
34 /* The main 3270 data structure. */
35 struct raw3270 {
36 	struct list_head list;
37 	struct ccw_device *cdev;
38 	int minor;
39 
40 	short model, rows, cols;
41 	unsigned int state;
42 	unsigned long flags;
43 
44 	struct list_head req_queue;	/* Request queue. */
45 	struct list_head view_list;	/* List of available views. */
46 	struct raw3270_view *view;	/* Active view. */
47 
48 	struct timer_list timer;	/* Device timer. */
49 
50 	unsigned char *ascebc;		/* ascii -> ebcdic table */
51 
52 	struct raw3270_view init_view;
53 	struct raw3270_request init_reset;
54 	struct raw3270_request init_readpart;
55 	struct raw3270_request init_readmod;
56 	unsigned char init_data[256];
57 };
58 
59 /* raw3270->state */
60 #define RAW3270_STATE_INIT	0	/* Initial state */
61 #define RAW3270_STATE_RESET	1	/* Reset command is pending */
62 #define RAW3270_STATE_W4ATTN	2	/* Wait for attention interrupt */
63 #define RAW3270_STATE_READMOD	3	/* Read partition is pending */
64 #define RAW3270_STATE_READY	4	/* Device is usable by views */
65 
66 /* raw3270->flags */
67 #define RAW3270_FLAGS_14BITADDR	0	/* 14-bit buffer addresses */
68 #define RAW3270_FLAGS_BUSY	1	/* Device busy, leave it alone */
69 #define RAW3270_FLAGS_CONSOLE	2	/* Device is the console. */
70 
71 /* Semaphore to protect global data of raw3270 (devices, views, etc). */
72 static DEFINE_MUTEX(raw3270_mutex);
73 
74 /* List of 3270 devices. */
75 static LIST_HEAD(raw3270_devices);
76 
77 /*
78  * Flag to indicate if the driver has been registered. Some operations
79  * like waiting for the end of i/o need to be done differently as long
80  * as the kernel is still starting up (console support).
81  */
82 static int raw3270_registered;
83 
84 /* Module parameters */
85 static bool tubxcorrect;
86 module_param(tubxcorrect, bool, 0);
87 
88 /*
89  * Wait queue for device init/delete, view delete.
90  */
91 DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
92 
93 static void __raw3270_disconnect(struct raw3270 *rp);
94 
95 /*
96  * Encode array for 12 bit 3270 addresses.
97  */
98 static unsigned char raw3270_ebcgraf[64] =	{
99 	0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
100 	0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
101 	0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
102 	0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
103 	0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
104 	0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
105 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
106 	0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
107 };
108 
109 static inline int raw3270_state_ready(struct raw3270 *rp)
110 {
111 	return rp->state == RAW3270_STATE_READY;
112 }
113 
114 static inline int raw3270_state_final(struct raw3270 *rp)
115 {
116 	return rp->state == RAW3270_STATE_INIT ||
117 		rp->state == RAW3270_STATE_READY;
118 }
119 
120 void
121 raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
122 {
123 	if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
124 		cp[0] = (addr >> 8) & 0x3f;
125 		cp[1] = addr & 0xff;
126 	} else {
127 		cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
128 		cp[1] = raw3270_ebcgraf[addr & 0x3f];
129 	}
130 }
131 
132 /*
133  * Allocate a new 3270 ccw request
134  */
135 struct raw3270_request *
136 raw3270_request_alloc(size_t size)
137 {
138 	struct raw3270_request *rq;
139 
140 	/* Allocate request structure */
141 	rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
142 	if (!rq)
143 		return ERR_PTR(-ENOMEM);
144 
145 	/* alloc output buffer. */
146 	if (size > 0) {
147 		rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
148 		if (!rq->buffer) {
149 			kfree(rq);
150 			return ERR_PTR(-ENOMEM);
151 		}
152 	}
153 	rq->size = size;
154 	INIT_LIST_HEAD(&rq->list);
155 
156 	/*
157 	 * Setup ccw.
158 	 */
159 	rq->ccw.cda = __pa(rq->buffer);
160 	rq->ccw.flags = CCW_FLAG_SLI;
161 
162 	return rq;
163 }
164 
165 /*
166  * Free 3270 ccw request
167  */
168 void
169 raw3270_request_free (struct raw3270_request *rq)
170 {
171 	kfree(rq->buffer);
172 	kfree(rq);
173 }
174 
175 /*
176  * Reset request to initial state.
177  */
178 void
179 raw3270_request_reset(struct raw3270_request *rq)
180 {
181 	BUG_ON(!list_empty(&rq->list));
182 	rq->ccw.cmd_code = 0;
183 	rq->ccw.count = 0;
184 	rq->ccw.cda = __pa(rq->buffer);
185 	rq->ccw.flags = CCW_FLAG_SLI;
186 	rq->rescnt = 0;
187 	rq->rc = 0;
188 }
189 
190 /*
191  * Set command code to ccw of a request.
192  */
193 void
194 raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
195 {
196 	rq->ccw.cmd_code = cmd;
197 }
198 
199 /*
200  * Add data fragment to output buffer.
201  */
202 int
203 raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
204 {
205 	if (size + rq->ccw.count > rq->size)
206 		return -E2BIG;
207 	memcpy(rq->buffer + rq->ccw.count, data, size);
208 	rq->ccw.count += size;
209 	return 0;
210 }
211 
212 /*
213  * Set address/length pair to ccw of a request.
214  */
215 void
216 raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
217 {
218 	rq->ccw.cda = __pa(data);
219 	rq->ccw.count = size;
220 }
221 
222 /*
223  * Set idal buffer to ccw of a request.
224  */
225 void
226 raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
227 {
228 	rq->ccw.cda = __pa(ib->data);
229 	rq->ccw.count = ib->size;
230 	rq->ccw.flags |= CCW_FLAG_IDA;
231 }
232 
233 /*
234  * Add the request to the request queue, try to start it if the
235  * 3270 device is idle. Return without waiting for end of i/o.
236  */
237 static int
238 __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
239 		struct raw3270_request *rq)
240 {
241 	rq->view = view;
242 	raw3270_get_view(view);
243 	if (list_empty(&rp->req_queue) &&
244 	    !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
245 		/* No other requests are on the queue. Start this one. */
246 		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
247 					       (unsigned long) rq, 0, 0);
248 		if (rq->rc) {
249 			raw3270_put_view(view);
250 			return rq->rc;
251 		}
252 	}
253 	list_add_tail(&rq->list, &rp->req_queue);
254 	return 0;
255 }
256 
257 int
258 raw3270_view_active(struct raw3270_view *view)
259 {
260 	struct raw3270 *rp = view->dev;
261 
262 	return rp && rp->view == view;
263 }
264 
265 int
266 raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
267 {
268 	unsigned long flags;
269 	struct raw3270 *rp;
270 	int rc;
271 
272 	spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
273 	rp = view->dev;
274 	if (!rp || rp->view != view)
275 		rc = -EACCES;
276 	else if (!raw3270_state_ready(rp))
277 		rc = -EBUSY;
278 	else
279 		rc =  __raw3270_start(rp, view, rq);
280 	spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
281 	return rc;
282 }
283 
284 int
285 raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
286 {
287 	struct raw3270 *rp;
288 	int rc;
289 
290 	rp = view->dev;
291 	if (!rp || rp->view != view)
292 		rc = -EACCES;
293 	else if (!raw3270_state_ready(rp))
294 		rc = -EBUSY;
295 	else
296 		rc =  __raw3270_start(rp, view, rq);
297 	return rc;
298 }
299 
300 int
301 raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
302 {
303 	struct raw3270 *rp;
304 
305 	rp = view->dev;
306 	rq->view = view;
307 	raw3270_get_view(view);
308 	list_add_tail(&rq->list, &rp->req_queue);
309 	return 0;
310 }
311 
312 /*
313  * 3270 interrupt routine, called from the ccw_device layer
314  */
315 static void
316 raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
317 {
318 	struct raw3270 *rp;
319 	struct raw3270_view *view;
320 	struct raw3270_request *rq;
321 
322 	rp = dev_get_drvdata(&cdev->dev);
323 	if (!rp)
324 		return;
325 	rq = (struct raw3270_request *) intparm;
326 	view = rq ? rq->view : rp->view;
327 
328 	if (!IS_ERR(irb)) {
329 		/* Handle CE-DE-UE and subsequent UDE */
330 		if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END)
331 			clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
332 		if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END |
333 					    DEV_STAT_DEV_END |
334 					    DEV_STAT_UNIT_EXCEP))
335 			set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
336 		/* Handle disconnected devices */
337 		if ((irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
338 		    (irb->ecw[0] & SNS0_INTERVENTION_REQ)) {
339 			set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
340 			if (rp->state > RAW3270_STATE_RESET)
341 				__raw3270_disconnect(rp);
342 		}
343 		/* Call interrupt handler of the view */
344 		if (view)
345 			view->fn->intv(view, rq, irb);
346 	}
347 
348 	if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags))
349 		/* Device busy, do not start I/O */
350 		return;
351 
352 	if (rq && !list_empty(&rq->list)) {
353 		/* The request completed, remove from queue and do callback. */
354 		list_del_init(&rq->list);
355 		if (rq->callback)
356 			rq->callback(rq, rq->callback_data);
357 		/* Do put_device for get_device in raw3270_start. */
358 		raw3270_put_view(view);
359 	}
360 
361 	/*
362 	 * Try to start each request on request queue until one is
363 	 * started successful.
364 	 */
365 	while (!list_empty(&rp->req_queue)) {
366 		rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
367 		rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
368 					  (unsigned long) rq, 0, 0);
369 		if (rq->rc == 0)
370 			break;
371 		/* Start failed. Remove request and do callback. */
372 		list_del_init(&rq->list);
373 		if (rq->callback)
374 			rq->callback(rq, rq->callback_data);
375 		/* Do put_device for get_device in raw3270_start. */
376 		raw3270_put_view(view);
377 	}
378 }
379 
380 /*
381  * To determine the size of the 3270 device we need to do:
382  * 1) send a 'read partition' data stream to the device
383  * 2) wait for the attn interrupt that precedes the query reply
384  * 3) do a read modified to get the query reply
385  * To make things worse we have to cope with intervention
386  * required (3270 device switched to 'stand-by') and command
387  * rejects (old devices that can't do 'read partition').
388  */
389 struct raw3270_ua {	/* Query Reply structure for Usable Area */
390 	struct {	/* Usable Area Query Reply Base */
391 		short l;	/* Length of this structured field */
392 		char  sfid;	/* 0x81 if Query Reply */
393 		char  qcode;	/* 0x81 if Usable Area */
394 		char  flags0;
395 		char  flags1;
396 		short w;	/* Width of usable area */
397 		short h;	/* Heigth of usavle area */
398 		char  units;	/* 0x00:in; 0x01:mm */
399 		int   xr;
400 		int   yr;
401 		char  aw;
402 		char  ah;
403 		short buffsz;	/* Character buffer size, bytes */
404 		char  xmin;
405 		char  ymin;
406 		char  xmax;
407 		char  ymax;
408 	} __attribute__ ((packed)) uab;
409 	struct {	/* Alternate Usable Area Self-Defining Parameter */
410 		char  l;	/* Length of this Self-Defining Parm */
411 		char  sdpid;	/* 0x02 if Alternate Usable Area */
412 		char  res;
413 		char  auaid;	/* 0x01 is Id for the A U A */
414 		short wauai;	/* Width of AUAi */
415 		short hauai;	/* Height of AUAi */
416 		char  auaunits;	/* 0x00:in, 0x01:mm */
417 		int   auaxr;
418 		int   auayr;
419 		char  awauai;
420 		char  ahauai;
421 	} __attribute__ ((packed)) aua;
422 } __attribute__ ((packed));
423 
424 static void
425 raw3270_size_device_vm(struct raw3270 *rp)
426 {
427 	int rc, model;
428 	struct ccw_dev_id dev_id;
429 	struct diag210 diag_data;
430 
431 	ccw_device_get_id(rp->cdev, &dev_id);
432 	diag_data.vrdcdvno = dev_id.devno;
433 	diag_data.vrdclen = sizeof(struct diag210);
434 	rc = diag210(&diag_data);
435 	model = diag_data.vrdccrmd;
436 	/* Use default model 2 if the size could not be detected */
437 	if (rc || model < 2 || model > 5)
438 		model = 2;
439 	switch (model) {
440 	case 2:
441 		rp->model = model;
442 		rp->rows = 24;
443 		rp->cols = 80;
444 		break;
445 	case 3:
446 		rp->model = model;
447 		rp->rows = 32;
448 		rp->cols = 80;
449 		break;
450 	case 4:
451 		rp->model = model;
452 		rp->rows = 43;
453 		rp->cols = 80;
454 		break;
455 	case 5:
456 		rp->model = model;
457 		rp->rows = 27;
458 		rp->cols = 132;
459 		break;
460 	}
461 }
462 
463 static void
464 raw3270_size_device(struct raw3270 *rp)
465 {
466 	struct raw3270_ua *uap;
467 
468 	/* Got a Query Reply */
469 	uap = (struct raw3270_ua *) (rp->init_data + 1);
470 	/* Paranoia check. */
471 	if (rp->init_readmod.rc || rp->init_data[0] != 0x88 ||
472 	    uap->uab.qcode != 0x81) {
473 		/* Couldn't detect size. Use default model 2. */
474 		rp->model = 2;
475 		rp->rows = 24;
476 		rp->cols = 80;
477 		return;
478 	}
479 	/* Copy rows/columns of default Usable Area */
480 	rp->rows = uap->uab.h;
481 	rp->cols = uap->uab.w;
482 	/* Check for 14 bit addressing */
483 	if ((uap->uab.flags0 & 0x0d) == 0x01)
484 		set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
485 	/* Check for Alternate Usable Area */
486 	if (uap->uab.l == sizeof(struct raw3270_ua) &&
487 	    uap->aua.sdpid == 0x02) {
488 		rp->rows = uap->aua.hauai;
489 		rp->cols = uap->aua.wauai;
490 	}
491 	/* Try to find a model. */
492 	rp->model = 0;
493 	if (rp->rows == 24 && rp->cols == 80)
494 		rp->model = 2;
495 	if (rp->rows == 32 && rp->cols == 80)
496 		rp->model = 3;
497 	if (rp->rows == 43 && rp->cols == 80)
498 		rp->model = 4;
499 	if (rp->rows == 27 && rp->cols == 132)
500 		rp->model = 5;
501 }
502 
503 static void
504 raw3270_size_device_done(struct raw3270 *rp)
505 {
506 	struct raw3270_view *view;
507 
508 	rp->view = NULL;
509 	rp->state = RAW3270_STATE_READY;
510 	/* Notify views about new size */
511 	list_for_each_entry(view, &rp->view_list, list)
512 		if (view->fn->resize)
513 			view->fn->resize(view, rp->model, rp->rows, rp->cols);
514 	/* Setup processing done, now activate a view */
515 	list_for_each_entry(view, &rp->view_list, list) {
516 		rp->view = view;
517 		if (view->fn->activate(view) == 0)
518 			break;
519 		rp->view = NULL;
520 	}
521 }
522 
523 static void
524 raw3270_read_modified_cb(struct raw3270_request *rq, void *data)
525 {
526 	struct raw3270 *rp = rq->view->dev;
527 
528 	raw3270_size_device(rp);
529 	raw3270_size_device_done(rp);
530 }
531 
532 static void
533 raw3270_read_modified(struct raw3270 *rp)
534 {
535 	if (rp->state != RAW3270_STATE_W4ATTN)
536 		return;
537 	/* Use 'read modified' to get the result of a read partition. */
538 	memset(&rp->init_readmod, 0, sizeof(rp->init_readmod));
539 	memset(&rp->init_data, 0, sizeof(rp->init_data));
540 	rp->init_readmod.ccw.cmd_code = TC_READMOD;
541 	rp->init_readmod.ccw.flags = CCW_FLAG_SLI;
542 	rp->init_readmod.ccw.count = sizeof(rp->init_data);
543 	rp->init_readmod.ccw.cda = (__u32) __pa(rp->init_data);
544 	rp->init_readmod.callback = raw3270_read_modified_cb;
545 	rp->state = RAW3270_STATE_READMOD;
546 	raw3270_start_irq(&rp->init_view, &rp->init_readmod);
547 }
548 
549 static void
550 raw3270_writesf_readpart(struct raw3270 *rp)
551 {
552 	static const unsigned char wbuf[] =
553 		{ 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
554 
555 	/* Store 'read partition' data stream to init_data */
556 	memset(&rp->init_readpart, 0, sizeof(rp->init_readpart));
557 	memset(&rp->init_data, 0, sizeof(rp->init_data));
558 	memcpy(&rp->init_data, wbuf, sizeof(wbuf));
559 	rp->init_readpart.ccw.cmd_code = TC_WRITESF;
560 	rp->init_readpart.ccw.flags = CCW_FLAG_SLI;
561 	rp->init_readpart.ccw.count = sizeof(wbuf);
562 	rp->init_readpart.ccw.cda = (__u32) __pa(&rp->init_data);
563 	rp->state = RAW3270_STATE_W4ATTN;
564 	raw3270_start_irq(&rp->init_view, &rp->init_readpart);
565 }
566 
567 /*
568  * Device reset
569  */
570 static void
571 raw3270_reset_device_cb(struct raw3270_request *rq, void *data)
572 {
573 	struct raw3270 *rp = rq->view->dev;
574 
575 	if (rp->state != RAW3270_STATE_RESET)
576 		return;
577 	if (rq->rc) {
578 		/* Reset command failed. */
579 		rp->state = RAW3270_STATE_INIT;
580 	} else if (MACHINE_IS_VM) {
581 		raw3270_size_device_vm(rp);
582 		raw3270_size_device_done(rp);
583 	} else
584 		raw3270_writesf_readpart(rp);
585 	memset(&rp->init_reset, 0, sizeof(rp->init_reset));
586 }
587 
588 static int
589 __raw3270_reset_device(struct raw3270 *rp)
590 {
591 	int rc;
592 
593 	/* Check if reset is already pending */
594 	if (rp->init_reset.view)
595 		return -EBUSY;
596 	/* Store reset data stream to init_data/init_reset */
597 	rp->init_data[0] = TW_KR;
598 	rp->init_reset.ccw.cmd_code = TC_EWRITEA;
599 	rp->init_reset.ccw.flags = CCW_FLAG_SLI;
600 	rp->init_reset.ccw.count = 1;
601 	rp->init_reset.ccw.cda = (__u32) __pa(rp->init_data);
602 	rp->init_reset.callback = raw3270_reset_device_cb;
603 	rc = __raw3270_start(rp, &rp->init_view, &rp->init_reset);
604 	if (rc == 0 && rp->state == RAW3270_STATE_INIT)
605 		rp->state = RAW3270_STATE_RESET;
606 	return rc;
607 }
608 
609 static int
610 raw3270_reset_device(struct raw3270 *rp)
611 {
612 	unsigned long flags;
613 	int rc;
614 
615 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
616 	rc = __raw3270_reset_device(rp);
617 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
618 	return rc;
619 }
620 
621 int
622 raw3270_reset(struct raw3270_view *view)
623 {
624 	struct raw3270 *rp;
625 	int rc;
626 
627 	rp = view->dev;
628 	if (!rp || rp->view != view)
629 		rc = -EACCES;
630 	else if (!raw3270_state_ready(rp))
631 		rc = -EBUSY;
632 	else
633 		rc = raw3270_reset_device(view->dev);
634 	return rc;
635 }
636 
637 static void
638 __raw3270_disconnect(struct raw3270 *rp)
639 {
640 	struct raw3270_request *rq;
641 	struct raw3270_view *view;
642 
643 	rp->state = RAW3270_STATE_INIT;
644 	rp->view = &rp->init_view;
645 	/* Cancel all queued requests */
646 	while (!list_empty(&rp->req_queue)) {
647 		rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
648 		view = rq->view;
649 		rq->rc = -EACCES;
650 		list_del_init(&rq->list);
651 		if (rq->callback)
652 			rq->callback(rq, rq->callback_data);
653 		raw3270_put_view(view);
654 	}
655 	/* Start from scratch */
656 	__raw3270_reset_device(rp);
657 }
658 
659 static void
660 raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
661 		 struct irb *irb)
662 {
663 	struct raw3270 *rp;
664 
665 	if (rq) {
666 		if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
667 			if (irb->ecw[0] & SNS0_CMD_REJECT)
668 				rq->rc = -EOPNOTSUPP;
669 			else
670 				rq->rc = -EIO;
671 		}
672 	}
673 	if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
674 		/* Queue read modified after attention interrupt */
675 		rp = view->dev;
676 		raw3270_read_modified(rp);
677 	}
678 }
679 
680 static struct raw3270_fn raw3270_init_fn = {
681 	.intv = raw3270_init_irq
682 };
683 
684 /*
685  * Setup new 3270 device.
686  */
687 static int
688 raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
689 {
690 	struct list_head *l;
691 	struct raw3270 *tmp;
692 	int minor;
693 
694 	memset(rp, 0, sizeof(struct raw3270));
695 	/* Copy ebcdic -> ascii translation table. */
696 	memcpy(ascebc, _ascebc, 256);
697 	if (tubxcorrect) {
698 		/* correct brackets and circumflex */
699 		ascebc['['] = 0xad;
700 		ascebc[']'] = 0xbd;
701 		ascebc['^'] = 0xb0;
702 	}
703 	rp->ascebc = ascebc;
704 
705 	/* Set defaults. */
706 	rp->rows = 24;
707 	rp->cols = 80;
708 
709 	INIT_LIST_HEAD(&rp->req_queue);
710 	INIT_LIST_HEAD(&rp->view_list);
711 
712 	rp->init_view.dev = rp;
713 	rp->init_view.fn = &raw3270_init_fn;
714 	rp->view = &rp->init_view;
715 
716 	/*
717 	 * Add device to list and find the smallest unused minor
718 	 * number for it. Note: there is no device with minor 0,
719 	 * see special case for fs3270.c:fs3270_open().
720 	 */
721 	mutex_lock(&raw3270_mutex);
722 	/* Keep the list sorted. */
723 	minor = RAW3270_FIRSTMINOR;
724 	rp->minor = -1;
725 	list_for_each(l, &raw3270_devices) {
726 		tmp = list_entry(l, struct raw3270, list);
727 		if (tmp->minor > minor) {
728 			rp->minor = minor;
729 			__list_add(&rp->list, l->prev, l);
730 			break;
731 		}
732 		minor++;
733 	}
734 	if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
735 		rp->minor = minor;
736 		list_add_tail(&rp->list, &raw3270_devices);
737 	}
738 	mutex_unlock(&raw3270_mutex);
739 	/* No free minor number? Then give up. */
740 	if (rp->minor == -1)
741 		return -EUSERS;
742 	rp->cdev = cdev;
743 	dev_set_drvdata(&cdev->dev, rp);
744 	cdev->handler = raw3270_irq;
745 	return 0;
746 }
747 
748 #ifdef CONFIG_TN3270_CONSOLE
749 /* Tentative definition - see below for actual definition. */
750 static struct ccw_driver raw3270_ccw_driver;
751 
752 /*
753  * Setup 3270 device configured as console.
754  */
755 struct raw3270 __init *raw3270_setup_console(void)
756 {
757 	struct ccw_device *cdev;
758 	unsigned long flags;
759 	struct raw3270 *rp;
760 	char *ascebc;
761 	int rc;
762 
763 	cdev = ccw_device_create_console(&raw3270_ccw_driver);
764 	if (IS_ERR(cdev))
765 		return ERR_CAST(cdev);
766 
767 	rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
768 	ascebc = kzalloc(256, GFP_KERNEL);
769 	rc = raw3270_setup_device(cdev, rp, ascebc);
770 	if (rc)
771 		return ERR_PTR(rc);
772 	set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
773 
774 	rc = ccw_device_enable_console(cdev);
775 	if (rc) {
776 		ccw_device_destroy_console(cdev);
777 		return ERR_PTR(rc);
778 	}
779 
780 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
781 	do {
782 		__raw3270_reset_device(rp);
783 		while (!raw3270_state_final(rp)) {
784 			ccw_device_wait_idle(rp->cdev);
785 			barrier();
786 		}
787 	} while (rp->state != RAW3270_STATE_READY);
788 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
789 	return rp;
790 }
791 
792 void
793 raw3270_wait_cons_dev(struct raw3270 *rp)
794 {
795 	unsigned long flags;
796 
797 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
798 	ccw_device_wait_idle(rp->cdev);
799 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
800 }
801 
802 #endif
803 
804 /*
805  * Create a 3270 device structure.
806  */
807 static struct raw3270 *
808 raw3270_create_device(struct ccw_device *cdev)
809 {
810 	struct raw3270 *rp;
811 	char *ascebc;
812 	int rc;
813 
814 	rp = kzalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
815 	if (!rp)
816 		return ERR_PTR(-ENOMEM);
817 	ascebc = kmalloc(256, GFP_KERNEL);
818 	if (!ascebc) {
819 		kfree(rp);
820 		return ERR_PTR(-ENOMEM);
821 	}
822 	rc = raw3270_setup_device(cdev, rp, ascebc);
823 	if (rc) {
824 		kfree(rp->ascebc);
825 		kfree(rp);
826 		rp = ERR_PTR(rc);
827 	}
828 	/* Get reference to ccw_device structure. */
829 	get_device(&cdev->dev);
830 	return rp;
831 }
832 
833 /*
834  * Activate a view.
835  */
836 int
837 raw3270_activate_view(struct raw3270_view *view)
838 {
839 	struct raw3270 *rp;
840 	struct raw3270_view *oldview, *nv;
841 	unsigned long flags;
842 	int rc;
843 
844 	rp = view->dev;
845 	if (!rp)
846 		return -ENODEV;
847 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
848 	if (rp->view == view)
849 		rc = 0;
850 	else if (!raw3270_state_ready(rp))
851 		rc = -EBUSY;
852 	else {
853 		oldview = NULL;
854 		if (rp->view && rp->view->fn->deactivate) {
855 			oldview = rp->view;
856 			oldview->fn->deactivate(oldview);
857 		}
858 		rp->view = view;
859 		rc = view->fn->activate(view);
860 		if (rc) {
861 			/* Didn't work. Try to reactivate the old view. */
862 			rp->view = oldview;
863 			if (!oldview || oldview->fn->activate(oldview) != 0) {
864 				/* Didn't work as well. Try any other view. */
865 				list_for_each_entry(nv, &rp->view_list, list)
866 					if (nv != view && nv != oldview) {
867 						rp->view = nv;
868 						if (nv->fn->activate(nv) == 0)
869 							break;
870 						rp->view = NULL;
871 					}
872 			}
873 		}
874 	}
875 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
876 	return rc;
877 }
878 
879 /*
880  * Deactivate current view.
881  */
882 void
883 raw3270_deactivate_view(struct raw3270_view *view)
884 {
885 	unsigned long flags;
886 	struct raw3270 *rp;
887 
888 	rp = view->dev;
889 	if (!rp)
890 		return;
891 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
892 	if (rp->view == view) {
893 		view->fn->deactivate(view);
894 		rp->view = NULL;
895 		/* Move deactivated view to end of list. */
896 		list_del_init(&view->list);
897 		list_add_tail(&view->list, &rp->view_list);
898 		/* Try to activate another view. */
899 		if (raw3270_state_ready(rp)) {
900 			list_for_each_entry(view, &rp->view_list, list) {
901 				rp->view = view;
902 				if (view->fn->activate(view) == 0)
903 					break;
904 				rp->view = NULL;
905 			}
906 		}
907 	}
908 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
909 }
910 
911 /*
912  * Add view to device with minor "minor".
913  */
914 int
915 raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor, int subclass)
916 {
917 	unsigned long flags;
918 	struct raw3270 *rp;
919 	int rc;
920 
921 	if (minor <= 0)
922 		return -ENODEV;
923 	mutex_lock(&raw3270_mutex);
924 	rc = -ENODEV;
925 	list_for_each_entry(rp, &raw3270_devices, list) {
926 		if (rp->minor != minor)
927 			continue;
928 		spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
929 		atomic_set(&view->ref_count, 2);
930 		view->dev = rp;
931 		view->fn = fn;
932 		view->model = rp->model;
933 		view->rows = rp->rows;
934 		view->cols = rp->cols;
935 		view->ascebc = rp->ascebc;
936 		spin_lock_init(&view->lock);
937 		lockdep_set_subclass(&view->lock, subclass);
938 		list_add(&view->list, &rp->view_list);
939 		rc = 0;
940 		spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
941 		break;
942 	}
943 	mutex_unlock(&raw3270_mutex);
944 	return rc;
945 }
946 
947 /*
948  * Find specific view of device with minor "minor".
949  */
950 struct raw3270_view *
951 raw3270_find_view(struct raw3270_fn *fn, int minor)
952 {
953 	struct raw3270 *rp;
954 	struct raw3270_view *view, *tmp;
955 	unsigned long flags;
956 
957 	mutex_lock(&raw3270_mutex);
958 	view = ERR_PTR(-ENODEV);
959 	list_for_each_entry(rp, &raw3270_devices, list) {
960 		if (rp->minor != minor)
961 			continue;
962 		spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
963 		list_for_each_entry(tmp, &rp->view_list, list) {
964 			if (tmp->fn == fn) {
965 				raw3270_get_view(tmp);
966 				view = tmp;
967 				break;
968 			}
969 		}
970 		spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
971 		break;
972 	}
973 	mutex_unlock(&raw3270_mutex);
974 	return view;
975 }
976 
977 /*
978  * Remove view from device and free view structure via call to view->fn->free.
979  */
980 void
981 raw3270_del_view(struct raw3270_view *view)
982 {
983 	unsigned long flags;
984 	struct raw3270 *rp;
985 	struct raw3270_view *nv;
986 
987 	rp = view->dev;
988 	spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
989 	if (rp->view == view) {
990 		view->fn->deactivate(view);
991 		rp->view = NULL;
992 	}
993 	list_del_init(&view->list);
994 	if (!rp->view && raw3270_state_ready(rp)) {
995 		/* Try to activate another view. */
996 		list_for_each_entry(nv, &rp->view_list, list) {
997 			if (nv->fn->activate(nv) == 0) {
998 				rp->view = nv;
999 				break;
1000 			}
1001 		}
1002 	}
1003 	spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
1004 	/* Wait for reference counter to drop to zero. */
1005 	atomic_dec(&view->ref_count);
1006 	wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
1007 	if (view->fn->free)
1008 		view->fn->free(view);
1009 }
1010 
1011 /*
1012  * Remove a 3270 device structure.
1013  */
1014 static void
1015 raw3270_delete_device(struct raw3270 *rp)
1016 {
1017 	struct ccw_device *cdev;
1018 
1019 	/* Remove from device chain. */
1020 	mutex_lock(&raw3270_mutex);
1021 	list_del_init(&rp->list);
1022 	mutex_unlock(&raw3270_mutex);
1023 
1024 	/* Disconnect from ccw_device. */
1025 	cdev = rp->cdev;
1026 	rp->cdev = NULL;
1027 	dev_set_drvdata(&cdev->dev, NULL);
1028 	cdev->handler = NULL;
1029 
1030 	/* Put ccw_device structure. */
1031 	put_device(&cdev->dev);
1032 
1033 	/* Now free raw3270 structure. */
1034 	kfree(rp->ascebc);
1035 	kfree(rp);
1036 }
1037 
1038 static int
1039 raw3270_probe (struct ccw_device *cdev)
1040 {
1041 	return 0;
1042 }
1043 
1044 /*
1045  * Additional attributes for a 3270 device
1046  */
1047 static ssize_t
1048 raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
1049 {
1050 	return sysfs_emit(buf, "%i\n",
1051 			  ((struct raw3270 *)dev_get_drvdata(dev))->model);
1052 }
1053 static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
1054 
1055 static ssize_t
1056 raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
1057 {
1058 	return sysfs_emit(buf, "%i\n",
1059 			  ((struct raw3270 *)dev_get_drvdata(dev))->rows);
1060 }
1061 static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
1062 
1063 static ssize_t
1064 raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
1065 {
1066 	return sysfs_emit(buf, "%i\n",
1067 			  ((struct raw3270 *)dev_get_drvdata(dev))->cols);
1068 }
1069 static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
1070 
1071 static struct attribute * raw3270_attrs[] = {
1072 	&dev_attr_model.attr,
1073 	&dev_attr_rows.attr,
1074 	&dev_attr_columns.attr,
1075 	NULL,
1076 };
1077 
1078 static const struct attribute_group raw3270_attr_group = {
1079 	.attrs = raw3270_attrs,
1080 };
1081 
1082 static int raw3270_create_attributes(struct raw3270 *rp)
1083 {
1084 	return sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
1085 }
1086 
1087 /*
1088  * Notifier for device addition/removal
1089  */
1090 static LIST_HEAD(raw3270_notifier);
1091 
1092 int raw3270_register_notifier(struct raw3270_notifier *notifier)
1093 {
1094 	struct raw3270 *rp;
1095 
1096 	mutex_lock(&raw3270_mutex);
1097 	list_add_tail(&notifier->list, &raw3270_notifier);
1098 	list_for_each_entry(rp, &raw3270_devices, list)
1099 		notifier->create(rp->minor);
1100 	mutex_unlock(&raw3270_mutex);
1101 	return 0;
1102 }
1103 
1104 void raw3270_unregister_notifier(struct raw3270_notifier *notifier)
1105 {
1106 	struct raw3270 *rp;
1107 
1108 	mutex_lock(&raw3270_mutex);
1109 	list_for_each_entry(rp, &raw3270_devices, list)
1110 		notifier->destroy(rp->minor);
1111 	list_del(&notifier->list);
1112 	mutex_unlock(&raw3270_mutex);
1113 }
1114 
1115 /*
1116  * Set 3270 device online.
1117  */
1118 static int
1119 raw3270_set_online (struct ccw_device *cdev)
1120 {
1121 	struct raw3270_notifier *np;
1122 	struct raw3270 *rp;
1123 	int rc;
1124 
1125 	rp = raw3270_create_device(cdev);
1126 	if (IS_ERR(rp))
1127 		return PTR_ERR(rp);
1128 	rc = raw3270_create_attributes(rp);
1129 	if (rc)
1130 		goto failure;
1131 	raw3270_reset_device(rp);
1132 	mutex_lock(&raw3270_mutex);
1133 	list_for_each_entry(np, &raw3270_notifier, list)
1134 		np->create(rp->minor);
1135 	mutex_unlock(&raw3270_mutex);
1136 	return 0;
1137 
1138 failure:
1139 	raw3270_delete_device(rp);
1140 	return rc;
1141 }
1142 
1143 /*
1144  * Remove 3270 device structure.
1145  */
1146 static void
1147 raw3270_remove (struct ccw_device *cdev)
1148 {
1149 	unsigned long flags;
1150 	struct raw3270 *rp;
1151 	struct raw3270_view *v;
1152 	struct raw3270_notifier *np;
1153 
1154 	rp = dev_get_drvdata(&cdev->dev);
1155 	/*
1156 	 * _remove is the opposite of _probe; it's probe that
1157 	 * should set up rp.  raw3270_remove gets entered for
1158 	 * devices even if they haven't been varied online.
1159 	 * Thus, rp may validly be NULL here.
1160 	 */
1161 	if (rp == NULL)
1162 		return;
1163 
1164 	sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
1165 
1166 	/* Deactivate current view and remove all views. */
1167 	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1168 	if (rp->view) {
1169 		if (rp->view->fn->deactivate)
1170 			rp->view->fn->deactivate(rp->view);
1171 		rp->view = NULL;
1172 	}
1173 	while (!list_empty(&rp->view_list)) {
1174 		v = list_entry(rp->view_list.next, struct raw3270_view, list);
1175 		if (v->fn->release)
1176 			v->fn->release(v);
1177 		spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1178 		raw3270_del_view(v);
1179 		spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1180 	}
1181 	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1182 
1183 	mutex_lock(&raw3270_mutex);
1184 	list_for_each_entry(np, &raw3270_notifier, list)
1185 		np->destroy(rp->minor);
1186 	mutex_unlock(&raw3270_mutex);
1187 
1188 	/* Reset 3270 device. */
1189 	raw3270_reset_device(rp);
1190 	/* And finally remove it. */
1191 	raw3270_delete_device(rp);
1192 }
1193 
1194 /*
1195  * Set 3270 device offline.
1196  */
1197 static int
1198 raw3270_set_offline (struct ccw_device *cdev)
1199 {
1200 	struct raw3270 *rp;
1201 
1202 	rp = dev_get_drvdata(&cdev->dev);
1203 	if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
1204 		return -EBUSY;
1205 	raw3270_remove(cdev);
1206 	return 0;
1207 }
1208 
1209 static struct ccw_device_id raw3270_id[] = {
1210 	{ CCW_DEVICE(0x3270, 0) },
1211 	{ CCW_DEVICE(0x3271, 0) },
1212 	{ CCW_DEVICE(0x3272, 0) },
1213 	{ CCW_DEVICE(0x3273, 0) },
1214 	{ CCW_DEVICE(0x3274, 0) },
1215 	{ CCW_DEVICE(0x3275, 0) },
1216 	{ CCW_DEVICE(0x3276, 0) },
1217 	{ CCW_DEVICE(0x3277, 0) },
1218 	{ CCW_DEVICE(0x3278, 0) },
1219 	{ CCW_DEVICE(0x3279, 0) },
1220 	{ CCW_DEVICE(0x3174, 0) },
1221 	{ /* end of list */ },
1222 };
1223 
1224 static struct ccw_driver raw3270_ccw_driver = {
1225 	.driver = {
1226 		.name	= "3270",
1227 		.owner	= THIS_MODULE,
1228 	},
1229 	.ids		= raw3270_id,
1230 	.probe		= &raw3270_probe,
1231 	.remove		= &raw3270_remove,
1232 	.set_online	= &raw3270_set_online,
1233 	.set_offline	= &raw3270_set_offline,
1234 	.int_class	= IRQIO_C70,
1235 };
1236 
1237 static int
1238 raw3270_init(void)
1239 {
1240 	struct raw3270 *rp;
1241 	int rc;
1242 
1243 	if (raw3270_registered)
1244 		return 0;
1245 	raw3270_registered = 1;
1246 	rc = ccw_driver_register(&raw3270_ccw_driver);
1247 	if (rc == 0) {
1248 		/* Create attributes for early (= console) device. */
1249 		mutex_lock(&raw3270_mutex);
1250 		class3270 = class_create(THIS_MODULE, "3270");
1251 		list_for_each_entry(rp, &raw3270_devices, list) {
1252 			get_device(&rp->cdev->dev);
1253 			raw3270_create_attributes(rp);
1254 		}
1255 		mutex_unlock(&raw3270_mutex);
1256 	}
1257 	return rc;
1258 }
1259 
1260 static void
1261 raw3270_exit(void)
1262 {
1263 	ccw_driver_unregister(&raw3270_ccw_driver);
1264 	class_destroy(class3270);
1265 }
1266 
1267 MODULE_LICENSE("GPL");
1268 
1269 module_init(raw3270_init);
1270 module_exit(raw3270_exit);
1271 
1272 EXPORT_SYMBOL(class3270);
1273 EXPORT_SYMBOL(raw3270_request_alloc);
1274 EXPORT_SYMBOL(raw3270_request_free);
1275 EXPORT_SYMBOL(raw3270_request_reset);
1276 EXPORT_SYMBOL(raw3270_request_set_cmd);
1277 EXPORT_SYMBOL(raw3270_request_add_data);
1278 EXPORT_SYMBOL(raw3270_request_set_data);
1279 EXPORT_SYMBOL(raw3270_request_set_idal);
1280 EXPORT_SYMBOL(raw3270_buffer_address);
1281 EXPORT_SYMBOL(raw3270_add_view);
1282 EXPORT_SYMBOL(raw3270_del_view);
1283 EXPORT_SYMBOL(raw3270_find_view);
1284 EXPORT_SYMBOL(raw3270_activate_view);
1285 EXPORT_SYMBOL(raw3270_deactivate_view);
1286 EXPORT_SYMBOL(raw3270_start);
1287 EXPORT_SYMBOL(raw3270_start_locked);
1288 EXPORT_SYMBOL(raw3270_start_irq);
1289 EXPORT_SYMBOL(raw3270_reset);
1290 EXPORT_SYMBOL(raw3270_register_notifier);
1291 EXPORT_SYMBOL(raw3270_unregister_notifier);
1292 EXPORT_SYMBOL(raw3270_wait_queue);
1293