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