blob: 61e8038a55ee300d3875ffcf01532624bc691dd6 [file] [log] [blame]
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001/*
2 * IUCV base infrastructure.
3 *
4 * Copyright 2001, 2006 IBM Deutschland Entwicklung GmbH, IBM Corporation
5 * Author(s):
6 * Original source:
7 * Alan Altmark (Alan_Altmark@us.ibm.com) Sept. 2000
8 * Xenia Tkatschow (xenia@us.ibm.com)
9 * 2Gb awareness and general cleanup:
10 * Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
11 * Rewritten for af_iucv:
12 * Martin Schwidefsky <schwidefsky@de.ibm.com>
13 *
14 * Documentation used:
15 * The original source
16 * CP Programming Service, IBM document # SC24-5760
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2, or (at your option)
21 * any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 */
32
Ursula Braun8f7c5022008-12-25 13:39:47 +010033#define KMSG_COMPONENT "iucv"
34#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
35
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -080036#include <linux/module.h>
37#include <linux/moduleparam.h>
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -080038#include <linux/spinlock.h>
39#include <linux/kernel.h>
40#include <linux/slab.h>
41#include <linux/init.h>
42#include <linux/interrupt.h>
43#include <linux/list.h>
44#include <linux/errno.h>
45#include <linux/err.h>
46#include <linux/device.h>
47#include <linux/cpu.h>
48#include <net/iucv/iucv.h>
49#include <asm/atomic.h>
50#include <asm/ebcdic.h>
51#include <asm/io.h>
52#include <asm/s390_ext.h>
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -080053#include <asm/smp.h>
54
55/*
56 * FLAGS:
57 * All flags are defined in the field IPFLAGS1 of each function
58 * and can be found in CP Programming Services.
59 * IPSRCCLS - Indicates you have specified a source class.
60 * IPTRGCLS - Indicates you have specified a target class.
61 * IPFGPID - Indicates you have specified a pathid.
62 * IPFGMID - Indicates you have specified a message ID.
63 * IPNORPY - Indicates a one-way message. No reply expected.
64 * IPALL - Indicates that all paths are affected.
65 */
66#define IUCV_IPSRCCLS 0x01
67#define IUCV_IPTRGCLS 0x01
68#define IUCV_IPFGPID 0x02
69#define IUCV_IPFGMID 0x04
70#define IUCV_IPNORPY 0x10
71#define IUCV_IPALL 0x80
72
Heiko Carstensda99f052007-05-04 12:23:27 -070073static int iucv_bus_match(struct device *dev, struct device_driver *drv)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -080074{
75 return 0;
76}
77
78struct bus_type iucv_bus = {
79 .name = "iucv",
80 .match = iucv_bus_match,
81};
Heiko Carstensda99f052007-05-04 12:23:27 -070082EXPORT_SYMBOL(iucv_bus);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -080083
84struct device *iucv_root;
Heiko Carstensda99f052007-05-04 12:23:27 -070085EXPORT_SYMBOL(iucv_root);
86
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -080087static int iucv_available;
88
89/* General IUCV interrupt structure */
90struct iucv_irq_data {
91 u16 ippathid;
92 u8 ipflags1;
93 u8 iptype;
94 u32 res2[8];
95};
96
Martin Schwidefsky04b090d2007-04-28 23:03:59 -070097struct iucv_irq_list {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -080098 struct list_head list;
99 struct iucv_irq_data data;
100};
101
Christoph Lameter70cf50352007-11-20 11:13:38 +0100102static struct iucv_irq_data *iucv_irq_data[NR_CPUS];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800103static cpumask_t iucv_buffer_cpumask = CPU_MASK_NONE;
104static cpumask_t iucv_irq_cpumask = CPU_MASK_NONE;
105
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700106/*
107 * Queue of interrupt buffers lock for delivery via the tasklet
108 * (fast but can't call smp_call_function).
109 */
110static LIST_HEAD(iucv_task_queue);
111
112/*
113 * The tasklet for fast delivery of iucv interrupts.
114 */
115static void iucv_tasklet_fn(unsigned long);
116static DECLARE_TASKLET(iucv_tasklet, iucv_tasklet_fn,0);
117
118/*
119 * Queue of interrupt buffers for delivery via a work queue
120 * (slower but can call smp_call_function).
121 */
122static LIST_HEAD(iucv_work_queue);
123
124/*
125 * The work element to deliver path pending interrupts.
126 */
127static void iucv_work_fn(struct work_struct *work);
128static DECLARE_WORK(iucv_work, iucv_work_fn);
129
130/*
131 * Spinlock protecting task and work queue.
132 */
133static DEFINE_SPINLOCK(iucv_queue_lock);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800134
135enum iucv_command_codes {
136 IUCV_QUERY = 0,
137 IUCV_RETRIEVE_BUFFER = 2,
138 IUCV_SEND = 4,
139 IUCV_RECEIVE = 5,
140 IUCV_REPLY = 6,
141 IUCV_REJECT = 8,
142 IUCV_PURGE = 9,
143 IUCV_ACCEPT = 10,
144 IUCV_CONNECT = 11,
145 IUCV_DECLARE_BUFFER = 12,
146 IUCV_QUIESCE = 13,
147 IUCV_RESUME = 14,
148 IUCV_SEVER = 15,
149 IUCV_SETMASK = 16,
150};
151
152/*
153 * Error messages that are used with the iucv_sever function. They get
154 * converted to EBCDIC.
155 */
156static char iucv_error_no_listener[16] = "NO LISTENER";
157static char iucv_error_no_memory[16] = "NO MEMORY";
158static char iucv_error_pathid[16] = "INVALID PATHID";
159
160/*
161 * iucv_handler_list: List of registered handlers.
162 */
163static LIST_HEAD(iucv_handler_list);
164
165/*
166 * iucv_path_table: an array of iucv_path structures.
167 */
168static struct iucv_path **iucv_path_table;
169static unsigned long iucv_max_pathid;
170
171/*
172 * iucv_lock: spinlock protecting iucv_handler_list and iucv_pathid_table
173 */
174static DEFINE_SPINLOCK(iucv_table_lock);
175
176/*
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700177 * iucv_active_cpu: contains the number of the cpu executing the tasklet
178 * or the work handler. Needed for iucv_path_sever called from tasklet.
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800179 */
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700180static int iucv_active_cpu = -1;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800181
182/*
183 * Mutex and wait queue for iucv_register/iucv_unregister.
184 */
185static DEFINE_MUTEX(iucv_register_mutex);
186
187/*
188 * Counter for number of non-smp capable handlers.
189 */
190static int iucv_nonsmp_handler;
191
192/*
193 * IUCV control data structure. Used by iucv_path_accept, iucv_path_connect,
194 * iucv_path_quiesce and iucv_path_sever.
195 */
196struct iucv_cmd_control {
197 u16 ippathid;
198 u8 ipflags1;
199 u8 iprcode;
200 u16 ipmsglim;
201 u16 res1;
202 u8 ipvmid[8];
203 u8 ipuser[16];
204 u8 iptarget[8];
205} __attribute__ ((packed,aligned(8)));
206
207/*
208 * Data in parameter list iucv structure. Used by iucv_message_send,
209 * iucv_message_send2way and iucv_message_reply.
210 */
211struct iucv_cmd_dpl {
212 u16 ippathid;
213 u8 ipflags1;
214 u8 iprcode;
215 u32 ipmsgid;
216 u32 iptrgcls;
217 u8 iprmmsg[8];
218 u32 ipsrccls;
219 u32 ipmsgtag;
220 u32 ipbfadr2;
221 u32 ipbfln2f;
222 u32 res;
223} __attribute__ ((packed,aligned(8)));
224
225/*
226 * Data in buffer iucv structure. Used by iucv_message_receive,
227 * iucv_message_reject, iucv_message_send, iucv_message_send2way
228 * and iucv_declare_cpu.
229 */
230struct iucv_cmd_db {
231 u16 ippathid;
232 u8 ipflags1;
233 u8 iprcode;
234 u32 ipmsgid;
235 u32 iptrgcls;
236 u32 ipbfadr1;
237 u32 ipbfln1f;
238 u32 ipsrccls;
239 u32 ipmsgtag;
240 u32 ipbfadr2;
241 u32 ipbfln2f;
242 u32 res;
243} __attribute__ ((packed,aligned(8)));
244
245/*
246 * Purge message iucv structure. Used by iucv_message_purge.
247 */
248struct iucv_cmd_purge {
249 u16 ippathid;
250 u8 ipflags1;
251 u8 iprcode;
252 u32 ipmsgid;
253 u8 ipaudit[3];
254 u8 res1[5];
255 u32 res2;
256 u32 ipsrccls;
257 u32 ipmsgtag;
258 u32 res3[3];
259} __attribute__ ((packed,aligned(8)));
260
261/*
262 * Set mask iucv structure. Used by iucv_enable_cpu.
263 */
264struct iucv_cmd_set_mask {
265 u8 ipmask;
266 u8 res1[2];
267 u8 iprcode;
268 u32 res2[9];
269} __attribute__ ((packed,aligned(8)));
270
271union iucv_param {
272 struct iucv_cmd_control ctrl;
273 struct iucv_cmd_dpl dpl;
274 struct iucv_cmd_db db;
275 struct iucv_cmd_purge purge;
276 struct iucv_cmd_set_mask set_mask;
277};
278
279/*
280 * Anchor for per-cpu IUCV command parameter block.
281 */
Christoph Lameter70cf50352007-11-20 11:13:38 +0100282static union iucv_param *iucv_param[NR_CPUS];
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000283static union iucv_param *iucv_param_irq[NR_CPUS];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800284
285/**
286 * iucv_call_b2f0
287 * @code: identifier of IUCV call to CP.
288 * @parm: pointer to a struct iucv_parm block
289 *
290 * Calls CP to execute IUCV commands.
291 *
292 * Returns the result of the CP IUCV call.
293 */
294static inline int iucv_call_b2f0(int command, union iucv_param *parm)
295{
296 register unsigned long reg0 asm ("0");
297 register unsigned long reg1 asm ("1");
298 int ccode;
299
300 reg0 = command;
301 reg1 = virt_to_phys(parm);
302 asm volatile(
303 " .long 0xb2f01000\n"
304 " ipm %0\n"
305 " srl %0,28\n"
306 : "=d" (ccode), "=m" (*parm), "+d" (reg0), "+a" (reg1)
307 : "m" (*parm) : "cc");
308 return (ccode == 1) ? parm->ctrl.iprcode : ccode;
309}
310
311/**
312 * iucv_query_maxconn
313 *
314 * Determines the maximum number of connections that may be established.
315 *
316 * Returns the maximum number of connections or -EPERM is IUCV is not
317 * available.
318 */
319static int iucv_query_maxconn(void)
320{
321 register unsigned long reg0 asm ("0");
322 register unsigned long reg1 asm ("1");
323 void *param;
324 int ccode;
325
326 param = kzalloc(sizeof(union iucv_param), GFP_KERNEL|GFP_DMA);
327 if (!param)
328 return -ENOMEM;
329 reg0 = IUCV_QUERY;
330 reg1 = (unsigned long) param;
331 asm volatile (
332 " .long 0xb2f01000\n"
333 " ipm %0\n"
334 " srl %0,28\n"
335 : "=d" (ccode), "+d" (reg0), "+d" (reg1) : : "cc");
336 if (ccode == 0)
337 iucv_max_pathid = reg0;
338 kfree(param);
339 return ccode ? -EPERM : 0;
340}
341
342/**
343 * iucv_allow_cpu
344 * @data: unused
345 *
346 * Allow iucv interrupts on this cpu.
347 */
348static void iucv_allow_cpu(void *data)
349{
350 int cpu = smp_processor_id();
351 union iucv_param *parm;
352
353 /*
354 * Enable all iucv interrupts.
355 * ipmask contains bits for the different interrupts
356 * 0x80 - Flag to allow nonpriority message pending interrupts
357 * 0x40 - Flag to allow priority message pending interrupts
358 * 0x20 - Flag to allow nonpriority message completion interrupts
359 * 0x10 - Flag to allow priority message completion interrupts
360 * 0x08 - Flag to allow IUCV control interrupts
361 */
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000362 parm = iucv_param_irq[cpu];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800363 memset(parm, 0, sizeof(union iucv_param));
364 parm->set_mask.ipmask = 0xf8;
365 iucv_call_b2f0(IUCV_SETMASK, parm);
366
367 /* Set indication that iucv interrupts are allowed for this cpu. */
368 cpu_set(cpu, iucv_irq_cpumask);
369}
370
371/**
372 * iucv_block_cpu
373 * @data: unused
374 *
375 * Block iucv interrupts on this cpu.
376 */
377static void iucv_block_cpu(void *data)
378{
379 int cpu = smp_processor_id();
380 union iucv_param *parm;
381
382 /* Disable all iucv interrupts. */
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000383 parm = iucv_param_irq[cpu];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800384 memset(parm, 0, sizeof(union iucv_param));
385 iucv_call_b2f0(IUCV_SETMASK, parm);
386
387 /* Clear indication that iucv interrupts are allowed for this cpu. */
388 cpu_clear(cpu, iucv_irq_cpumask);
389}
390
391/**
392 * iucv_declare_cpu
393 * @data: unused
394 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200395 * Declare a interrupt buffer on this cpu.
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800396 */
397static void iucv_declare_cpu(void *data)
398{
399 int cpu = smp_processor_id();
400 union iucv_param *parm;
401 int rc;
402
403 if (cpu_isset(cpu, iucv_buffer_cpumask))
404 return;
405
406 /* Declare interrupt buffer. */
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000407 parm = iucv_param_irq[cpu];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800408 memset(parm, 0, sizeof(union iucv_param));
Christoph Lameter70cf50352007-11-20 11:13:38 +0100409 parm->db.ipbfadr1 = virt_to_phys(iucv_irq_data[cpu]);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800410 rc = iucv_call_b2f0(IUCV_DECLARE_BUFFER, parm);
411 if (rc) {
412 char *err = "Unknown";
Heiko Carstensda99f052007-05-04 12:23:27 -0700413 switch (rc) {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800414 case 0x03:
415 err = "Directory error";
416 break;
417 case 0x0a:
418 err = "Invalid length";
419 break;
420 case 0x13:
421 err = "Buffer already exists";
422 break;
423 case 0x3e:
424 err = "Buffer overlap";
425 break;
426 case 0x5c:
427 err = "Paging or storage error";
428 break;
429 }
Ursula Braun8f7c5022008-12-25 13:39:47 +0100430 pr_warning("Defining an interrupt buffer on CPU %i"
431 " failed with 0x%02x (%s)\n", cpu, rc, err);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800432 return;
433 }
434
435 /* Set indication that an iucv buffer exists for this cpu. */
436 cpu_set(cpu, iucv_buffer_cpumask);
437
438 if (iucv_nonsmp_handler == 0 || cpus_empty(iucv_irq_cpumask))
439 /* Enable iucv interrupts on this cpu. */
440 iucv_allow_cpu(NULL);
441 else
442 /* Disable iucv interrupts on this cpu. */
443 iucv_block_cpu(NULL);
444}
445
446/**
447 * iucv_retrieve_cpu
448 * @data: unused
449 *
450 * Retrieve interrupt buffer on this cpu.
451 */
452static void iucv_retrieve_cpu(void *data)
453{
454 int cpu = smp_processor_id();
455 union iucv_param *parm;
456
457 if (!cpu_isset(cpu, iucv_buffer_cpumask))
458 return;
459
460 /* Block iucv interrupts. */
461 iucv_block_cpu(NULL);
462
463 /* Retrieve interrupt buffer. */
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000464 parm = iucv_param_irq[cpu];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800465 iucv_call_b2f0(IUCV_RETRIEVE_BUFFER, parm);
466
467 /* Clear indication that an iucv buffer exists for this cpu. */
468 cpu_clear(cpu, iucv_buffer_cpumask);
469}
470
471/**
472 * iucv_setmask_smp
473 *
474 * Allow iucv interrupts on all cpus.
475 */
476static void iucv_setmask_mp(void)
477{
478 int cpu;
479
Heiko Carstens7b9d1b22008-06-09 15:50:30 -0700480 get_online_cpus();
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800481 for_each_online_cpu(cpu)
482 /* Enable all cpus with a declared buffer. */
483 if (cpu_isset(cpu, iucv_buffer_cpumask) &&
484 !cpu_isset(cpu, iucv_irq_cpumask))
Heiko Carstens3bb447f2007-07-27 12:29:08 +0200485 smp_call_function_single(cpu, iucv_allow_cpu,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200486 NULL, 1);
Heiko Carstens7b9d1b22008-06-09 15:50:30 -0700487 put_online_cpus();
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800488}
489
490/**
491 * iucv_setmask_up
492 *
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700493 * Allow iucv interrupts on a single cpu.
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800494 */
495static void iucv_setmask_up(void)
496{
497 cpumask_t cpumask;
498 int cpu;
499
500 /* Disable all cpu but the first in cpu_irq_cpumask. */
501 cpumask = iucv_irq_cpumask;
502 cpu_clear(first_cpu(iucv_irq_cpumask), cpumask);
Mike Travis0e12f842008-05-12 21:21:13 +0200503 for_each_cpu_mask_nr(cpu, cpumask)
Jens Axboe8691e5a2008-06-06 11:18:06 +0200504 smp_call_function_single(cpu, iucv_block_cpu, NULL, 1);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800505}
506
507/**
508 * iucv_enable
509 *
510 * This function makes iucv ready for use. It allocates the pathid
511 * table, declares an iucv interrupt buffer and enables the iucv
512 * interrupts. Called when the first user has registered an iucv
513 * handler.
514 */
515static int iucv_enable(void)
516{
517 size_t alloc_size;
518 int cpu, rc;
519
Heiko Carstensf1d3e4d2009-01-05 18:09:02 -0800520 get_online_cpus();
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800521 rc = -ENOMEM;
522 alloc_size = iucv_max_pathid * sizeof(struct iucv_path);
523 iucv_path_table = kzalloc(alloc_size, GFP_KERNEL);
524 if (!iucv_path_table)
525 goto out;
526 /* Declare per cpu buffers. */
527 rc = -EIO;
528 for_each_online_cpu(cpu)
Jens Axboe8691e5a2008-06-06 11:18:06 +0200529 smp_call_function_single(cpu, iucv_declare_cpu, NULL, 1);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800530 if (cpus_empty(iucv_buffer_cpumask))
531 /* No cpu could declare an iucv buffer. */
Heiko Carstensf1d3e4d2009-01-05 18:09:02 -0800532 goto out;
Heiko Carstens7b9d1b22008-06-09 15:50:30 -0700533 put_online_cpus();
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800534 return 0;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800535out:
Heiko Carstensf1d3e4d2009-01-05 18:09:02 -0800536 kfree(iucv_path_table);
537 iucv_path_table = NULL;
538 put_online_cpus();
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800539 return rc;
540}
541
542/**
543 * iucv_disable
544 *
545 * This function shuts down iucv. It disables iucv interrupts, retrieves
546 * the iucv interrupt buffer and frees the pathid table. Called after the
547 * last user unregister its iucv handler.
548 */
549static void iucv_disable(void)
550{
Heiko Carstens8b122ef2008-09-30 03:03:35 -0700551 get_online_cpus();
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200552 on_each_cpu(iucv_retrieve_cpu, NULL, 1);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800553 kfree(iucv_path_table);
Heiko Carstensf1d3e4d2009-01-05 18:09:02 -0800554 iucv_path_table = NULL;
555 put_online_cpus();
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800556}
557
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800558static int __cpuinit iucv_cpu_notify(struct notifier_block *self,
559 unsigned long action, void *hcpu)
560{
561 cpumask_t cpumask;
562 long cpu = (long) hcpu;
563
564 switch (action) {
565 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700566 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter70cf50352007-11-20 11:13:38 +0100567 iucv_irq_data[cpu] = kmalloc_node(sizeof(struct iucv_irq_data),
568 GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
569 if (!iucv_irq_data[cpu])
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800570 return NOTIFY_BAD;
Christoph Lameter70cf50352007-11-20 11:13:38 +0100571 iucv_param[cpu] = kmalloc_node(sizeof(union iucv_param),
572 GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
Akinobu Mitad0236f82008-07-15 02:09:53 -0700573 if (!iucv_param[cpu]) {
574 kfree(iucv_irq_data[cpu]);
575 iucv_irq_data[cpu] = NULL;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800576 return NOTIFY_BAD;
Akinobu Mitad0236f82008-07-15 02:09:53 -0700577 }
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000578 iucv_param_irq[cpu] = kmalloc_node(sizeof(union iucv_param),
579 GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
580 if (!iucv_param_irq[cpu]) {
581 kfree(iucv_param[cpu]);
582 iucv_param[cpu] = NULL;
583 kfree(iucv_irq_data[cpu]);
584 iucv_irq_data[cpu] = NULL;
585 return NOTIFY_BAD;
586 }
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800587 break;
588 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700589 case CPU_UP_CANCELED_FROZEN:
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800590 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700591 case CPU_DEAD_FROZEN:
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000592 kfree(iucv_param_irq[cpu]);
593 iucv_param_irq[cpu] = NULL;
Christoph Lameter70cf50352007-11-20 11:13:38 +0100594 kfree(iucv_param[cpu]);
595 iucv_param[cpu] = NULL;
596 kfree(iucv_irq_data[cpu]);
597 iucv_irq_data[cpu] = NULL;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800598 break;
599 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700600 case CPU_ONLINE_FROZEN:
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800601 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700602 case CPU_DOWN_FAILED_FROZEN:
Heiko Carstensf1d3e4d2009-01-05 18:09:02 -0800603 if (!iucv_path_table)
604 break;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200605 smp_call_function_single(cpu, iucv_declare_cpu, NULL, 1);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800606 break;
607 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700608 case CPU_DOWN_PREPARE_FROZEN:
Heiko Carstensf1d3e4d2009-01-05 18:09:02 -0800609 if (!iucv_path_table)
610 break;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800611 cpumask = iucv_buffer_cpumask;
612 cpu_clear(cpu, cpumask);
613 if (cpus_empty(cpumask))
614 /* Can't offline last IUCV enabled cpu. */
615 return NOTIFY_BAD;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200616 smp_call_function_single(cpu, iucv_retrieve_cpu, NULL, 1);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800617 if (cpus_empty(iucv_irq_cpumask))
Heiko Carstens3bb447f2007-07-27 12:29:08 +0200618 smp_call_function_single(first_cpu(iucv_buffer_cpumask),
Jens Axboe8691e5a2008-06-06 11:18:06 +0200619 iucv_allow_cpu, NULL, 1);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800620 break;
621 }
622 return NOTIFY_OK;
623}
624
Heiko Carstensf1494ed2008-06-09 15:49:57 -0700625static struct notifier_block __refdata iucv_cpu_notifier = {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800626 .notifier_call = iucv_cpu_notify,
627};
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800628
629/**
630 * iucv_sever_pathid
631 * @pathid: path identification number.
632 * @userdata: 16-bytes of user data.
633 *
634 * Sever an iucv path to free up the pathid. Used internally.
635 */
636static int iucv_sever_pathid(u16 pathid, u8 userdata[16])
637{
638 union iucv_param *parm;
639
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000640 parm = iucv_param_irq[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800641 memset(parm, 0, sizeof(union iucv_param));
642 if (userdata)
643 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
644 parm->ctrl.ippathid = pathid;
645 return iucv_call_b2f0(IUCV_SEVER, parm);
646}
647
648/**
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700649 * __iucv_cleanup_queue
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800650 * @dummy: unused dummy argument
651 *
652 * Nop function called via smp_call_function to force work items from
653 * pending external iucv interrupts to the work queue.
654 */
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700655static void __iucv_cleanup_queue(void *dummy)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800656{
657}
658
659/**
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700660 * iucv_cleanup_queue
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800661 *
662 * Function called after a path has been severed to find all remaining
663 * work items for the now stale pathid. The caller needs to hold the
664 * iucv_table_lock.
665 */
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700666static void iucv_cleanup_queue(void)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800667{
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700668 struct iucv_irq_list *p, *n;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800669
670 /*
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700671 * When a path is severed, the pathid can be reused immediatly
672 * on a iucv connect or a connection pending interrupt. Remove
673 * all entries from the task queue that refer to a stale pathid
674 * (iucv_path_table[ix] == NULL). Only then do the iucv connect
675 * or deliver the connection pending interrupt. To get all the
676 * pending interrupts force them to the work queue by calling
677 * an empty function on all cpus.
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800678 */
Jens Axboe8691e5a2008-06-06 11:18:06 +0200679 smp_call_function(__iucv_cleanup_queue, NULL, 1);
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700680 spin_lock_irq(&iucv_queue_lock);
681 list_for_each_entry_safe(p, n, &iucv_task_queue, list) {
682 /* Remove stale work items from the task queue. */
683 if (iucv_path_table[p->data.ippathid] == NULL) {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800684 list_del(&p->list);
685 kfree(p);
686 }
687 }
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700688 spin_unlock_irq(&iucv_queue_lock);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800689}
690
691/**
692 * iucv_register:
693 * @handler: address of iucv handler structure
694 * @smp: != 0 indicates that the handler can deal with out of order messages
695 *
696 * Registers a driver with IUCV.
697 *
698 * Returns 0 on success, -ENOMEM if the memory allocation for the pathid
699 * table failed, or -EIO if IUCV_DECLARE_BUFFER failed on all cpus.
700 */
701int iucv_register(struct iucv_handler *handler, int smp)
702{
703 int rc;
704
705 if (!iucv_available)
706 return -ENOSYS;
707 mutex_lock(&iucv_register_mutex);
708 if (!smp)
709 iucv_nonsmp_handler++;
710 if (list_empty(&iucv_handler_list)) {
711 rc = iucv_enable();
712 if (rc)
713 goto out_mutex;
714 } else if (!smp && iucv_nonsmp_handler == 1)
715 iucv_setmask_up();
716 INIT_LIST_HEAD(&handler->paths);
717
Ursula Braun435bc9d2008-02-07 18:06:52 -0800718 spin_lock_bh(&iucv_table_lock);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800719 list_add_tail(&handler->list, &iucv_handler_list);
Ursula Braun435bc9d2008-02-07 18:06:52 -0800720 spin_unlock_bh(&iucv_table_lock);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800721 rc = 0;
722out_mutex:
723 mutex_unlock(&iucv_register_mutex);
724 return rc;
725}
Heiko Carstensda99f052007-05-04 12:23:27 -0700726EXPORT_SYMBOL(iucv_register);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800727
728/**
729 * iucv_unregister
730 * @handler: address of iucv handler structure
731 * @smp: != 0 indicates that the handler can deal with out of order messages
732 *
733 * Unregister driver from IUCV.
734 */
735void iucv_unregister(struct iucv_handler *handler, int smp)
736{
737 struct iucv_path *p, *n;
738
739 mutex_lock(&iucv_register_mutex);
740 spin_lock_bh(&iucv_table_lock);
741 /* Remove handler from the iucv_handler_list. */
742 list_del_init(&handler->list);
743 /* Sever all pathids still refering to the handler. */
744 list_for_each_entry_safe(p, n, &handler->paths, list) {
745 iucv_sever_pathid(p->pathid, NULL);
746 iucv_path_table[p->pathid] = NULL;
747 list_del(&p->list);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800748 iucv_path_free(p);
749 }
750 spin_unlock_bh(&iucv_table_lock);
751 if (!smp)
752 iucv_nonsmp_handler--;
753 if (list_empty(&iucv_handler_list))
754 iucv_disable();
755 else if (!smp && iucv_nonsmp_handler == 0)
756 iucv_setmask_mp();
757 mutex_unlock(&iucv_register_mutex);
758}
Heiko Carstensda99f052007-05-04 12:23:27 -0700759EXPORT_SYMBOL(iucv_unregister);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800760
761/**
762 * iucv_path_accept
763 * @path: address of iucv path structure
764 * @handler: address of iucv handler structure
765 * @userdata: 16 bytes of data reflected to the communication partner
766 * @private: private data passed to interrupt handlers for this path
767 *
768 * This function is issued after the user received a connection pending
769 * external interrupt and now wishes to complete the IUCV communication path.
770 *
771 * Returns the result of the CP IUCV call.
772 */
773int iucv_path_accept(struct iucv_path *path, struct iucv_handler *handler,
774 u8 userdata[16], void *private)
775{
776 union iucv_param *parm;
777 int rc;
778
779 local_bh_disable();
780 /* Prepare parameter block. */
Christoph Lameter70cf50352007-11-20 11:13:38 +0100781 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800782 memset(parm, 0, sizeof(union iucv_param));
783 parm->ctrl.ippathid = path->pathid;
784 parm->ctrl.ipmsglim = path->msglim;
785 if (userdata)
786 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
787 parm->ctrl.ipflags1 = path->flags;
788
789 rc = iucv_call_b2f0(IUCV_ACCEPT, parm);
790 if (!rc) {
791 path->private = private;
792 path->msglim = parm->ctrl.ipmsglim;
793 path->flags = parm->ctrl.ipflags1;
794 }
795 local_bh_enable();
796 return rc;
797}
Heiko Carstensda99f052007-05-04 12:23:27 -0700798EXPORT_SYMBOL(iucv_path_accept);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800799
800/**
801 * iucv_path_connect
802 * @path: address of iucv path structure
803 * @handler: address of iucv handler structure
804 * @userid: 8-byte user identification
805 * @system: 8-byte target system identification
806 * @userdata: 16 bytes of data reflected to the communication partner
807 * @private: private data passed to interrupt handlers for this path
808 *
809 * This function establishes an IUCV path. Although the connect may complete
810 * successfully, you are not able to use the path until you receive an IUCV
811 * Connection Complete external interrupt.
812 *
813 * Returns the result of the CP IUCV call.
814 */
815int iucv_path_connect(struct iucv_path *path, struct iucv_handler *handler,
816 u8 userid[8], u8 system[8], u8 userdata[16],
817 void *private)
818{
819 union iucv_param *parm;
820 int rc;
821
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700822 spin_lock_bh(&iucv_table_lock);
823 iucv_cleanup_queue();
Christoph Lameter70cf50352007-11-20 11:13:38 +0100824 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800825 memset(parm, 0, sizeof(union iucv_param));
826 parm->ctrl.ipmsglim = path->msglim;
827 parm->ctrl.ipflags1 = path->flags;
828 if (userid) {
829 memcpy(parm->ctrl.ipvmid, userid, sizeof(parm->ctrl.ipvmid));
830 ASCEBC(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
831 EBC_TOUPPER(parm->ctrl.ipvmid, sizeof(parm->ctrl.ipvmid));
832 }
833 if (system) {
834 memcpy(parm->ctrl.iptarget, system,
835 sizeof(parm->ctrl.iptarget));
836 ASCEBC(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
837 EBC_TOUPPER(parm->ctrl.iptarget, sizeof(parm->ctrl.iptarget));
838 }
839 if (userdata)
840 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
841
842 rc = iucv_call_b2f0(IUCV_CONNECT, parm);
843 if (!rc) {
844 if (parm->ctrl.ippathid < iucv_max_pathid) {
845 path->pathid = parm->ctrl.ippathid;
846 path->msglim = parm->ctrl.ipmsglim;
847 path->flags = parm->ctrl.ipflags1;
848 path->handler = handler;
849 path->private = private;
850 list_add_tail(&path->list, &handler->paths);
851 iucv_path_table[path->pathid] = path;
852 } else {
853 iucv_sever_pathid(parm->ctrl.ippathid,
854 iucv_error_pathid);
855 rc = -EIO;
856 }
857 }
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700858 spin_unlock_bh(&iucv_table_lock);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800859 return rc;
860}
Heiko Carstensda99f052007-05-04 12:23:27 -0700861EXPORT_SYMBOL(iucv_path_connect);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800862
863/**
864 * iucv_path_quiesce:
865 * @path: address of iucv path structure
866 * @userdata: 16 bytes of data reflected to the communication partner
867 *
868 * This function temporarily suspends incoming messages on an IUCV path.
869 * You can later reactivate the path by invoking the iucv_resume function.
870 *
871 * Returns the result from the CP IUCV call.
872 */
873int iucv_path_quiesce(struct iucv_path *path, u8 userdata[16])
874{
875 union iucv_param *parm;
876 int rc;
877
878 local_bh_disable();
Christoph Lameter70cf50352007-11-20 11:13:38 +0100879 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800880 memset(parm, 0, sizeof(union iucv_param));
881 if (userdata)
882 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
883 parm->ctrl.ippathid = path->pathid;
884 rc = iucv_call_b2f0(IUCV_QUIESCE, parm);
885 local_bh_enable();
886 return rc;
887}
Heiko Carstensda99f052007-05-04 12:23:27 -0700888EXPORT_SYMBOL(iucv_path_quiesce);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800889
890/**
891 * iucv_path_resume:
892 * @path: address of iucv path structure
893 * @userdata: 16 bytes of data reflected to the communication partner
894 *
895 * This function resumes incoming messages on an IUCV path that has
896 * been stopped with iucv_path_quiesce.
897 *
898 * Returns the result from the CP IUCV call.
899 */
900int iucv_path_resume(struct iucv_path *path, u8 userdata[16])
901{
902 union iucv_param *parm;
903 int rc;
904
905 local_bh_disable();
Christoph Lameter70cf50352007-11-20 11:13:38 +0100906 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800907 memset(parm, 0, sizeof(union iucv_param));
908 if (userdata)
909 memcpy(parm->ctrl.ipuser, userdata, sizeof(parm->ctrl.ipuser));
910 parm->ctrl.ippathid = path->pathid;
911 rc = iucv_call_b2f0(IUCV_RESUME, parm);
912 local_bh_enable();
913 return rc;
914}
915
916/**
917 * iucv_path_sever
918 * @path: address of iucv path structure
919 * @userdata: 16 bytes of data reflected to the communication partner
920 *
921 * This function terminates an IUCV path.
922 *
923 * Returns the result from the CP IUCV call.
924 */
925int iucv_path_sever(struct iucv_path *path, u8 userdata[16])
926{
927 int rc;
928
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800929 preempt_disable();
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700930 if (iucv_active_cpu != smp_processor_id())
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800931 spin_lock_bh(&iucv_table_lock);
932 rc = iucv_sever_pathid(path->pathid, userdata);
Ursula Braun42e1b4c2009-04-21 23:26:20 +0000933 iucv_path_table[path->pathid] = NULL;
934 list_del_init(&path->list);
Martin Schwidefsky04b090d2007-04-28 23:03:59 -0700935 if (iucv_active_cpu != smp_processor_id())
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800936 spin_unlock_bh(&iucv_table_lock);
937 preempt_enable();
938 return rc;
939}
Heiko Carstensda99f052007-05-04 12:23:27 -0700940EXPORT_SYMBOL(iucv_path_sever);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800941
942/**
943 * iucv_message_purge
944 * @path: address of iucv path structure
945 * @msg: address of iucv msg structure
946 * @srccls: source class of message
947 *
948 * Cancels a message you have sent.
949 *
950 * Returns the result from the CP IUCV call.
951 */
952int iucv_message_purge(struct iucv_path *path, struct iucv_message *msg,
953 u32 srccls)
954{
955 union iucv_param *parm;
956 int rc;
957
958 local_bh_disable();
Christoph Lameter70cf50352007-11-20 11:13:38 +0100959 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800960 memset(parm, 0, sizeof(union iucv_param));
961 parm->purge.ippathid = path->pathid;
962 parm->purge.ipmsgid = msg->id;
963 parm->purge.ipsrccls = srccls;
964 parm->purge.ipflags1 = IUCV_IPSRCCLS | IUCV_IPFGMID | IUCV_IPFGPID;
965 rc = iucv_call_b2f0(IUCV_PURGE, parm);
966 if (!rc) {
967 msg->audit = (*(u32 *) &parm->purge.ipaudit) >> 8;
968 msg->tag = parm->purge.ipmsgtag;
969 }
970 local_bh_enable();
971 return rc;
972}
Heiko Carstensda99f052007-05-04 12:23:27 -0700973EXPORT_SYMBOL(iucv_message_purge);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -0800974
975/**
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +0100976 * iucv_message_receive_iprmdata
977 * @path: address of iucv path structure
978 * @msg: address of iucv msg structure
979 * @flags: how the message is received (IUCV_IPBUFLST)
980 * @buffer: address of data buffer or address of struct iucv_array
981 * @size: length of data buffer
982 * @residual:
983 *
984 * Internal function used by iucv_message_receive and __iucv_message_receive
985 * to receive RMDATA data stored in struct iucv_message.
986 */
987static int iucv_message_receive_iprmdata(struct iucv_path *path,
988 struct iucv_message *msg,
989 u8 flags, void *buffer,
990 size_t size, size_t *residual)
991{
992 struct iucv_array *array;
993 u8 *rmmsg;
994 size_t copy;
995
996 /*
997 * Message is 8 bytes long and has been stored to the
998 * message descriptor itself.
999 */
1000 if (residual)
1001 *residual = abs(size - 8);
1002 rmmsg = msg->rmmsg;
1003 if (flags & IUCV_IPBUFLST) {
1004 /* Copy to struct iucv_array. */
1005 size = (size < 8) ? size : 8;
1006 for (array = buffer; size > 0; array++) {
1007 copy = min_t(size_t, size, array->length);
1008 memcpy((u8 *)(addr_t) array->address,
1009 rmmsg, copy);
1010 rmmsg += copy;
1011 size -= copy;
1012 }
1013 } else {
1014 /* Copy to direct buffer. */
1015 memcpy(buffer, rmmsg, min_t(size_t, size, 8));
1016 }
1017 return 0;
1018}
1019
1020/**
1021 * __iucv_message_receive
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001022 * @path: address of iucv path structure
1023 * @msg: address of iucv msg structure
1024 * @flags: how the message is received (IUCV_IPBUFLST)
1025 * @buffer: address of data buffer or address of struct iucv_array
1026 * @size: length of data buffer
1027 * @residual:
1028 *
1029 * This function receives messages that are being sent to you over
1030 * established paths. This function will deal with RMDATA messages
1031 * embedded in struct iucv_message as well.
1032 *
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001033 * Locking: no locking
1034 *
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001035 * Returns the result from the CP IUCV call.
1036 */
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001037int __iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
1038 u8 flags, void *buffer, size_t size, size_t *residual)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001039{
1040 union iucv_param *parm;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001041 int rc;
1042
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001043 if (msg->flags & IUCV_IPRMDATA)
1044 return iucv_message_receive_iprmdata(path, msg, flags,
1045 buffer, size, residual);
Christoph Lameter70cf50352007-11-20 11:13:38 +01001046 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001047 memset(parm, 0, sizeof(union iucv_param));
1048 parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1049 parm->db.ipbfln1f = (u32) size;
1050 parm->db.ipmsgid = msg->id;
1051 parm->db.ippathid = path->pathid;
1052 parm->db.iptrgcls = msg->class;
1053 parm->db.ipflags1 = (flags | IUCV_IPFGPID |
1054 IUCV_IPFGMID | IUCV_IPTRGCLS);
1055 rc = iucv_call_b2f0(IUCV_RECEIVE, parm);
1056 if (!rc || rc == 5) {
1057 msg->flags = parm->db.ipflags1;
1058 if (residual)
1059 *residual = parm->db.ipbfln1f;
1060 }
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001061 return rc;
1062}
1063EXPORT_SYMBOL(__iucv_message_receive);
1064
1065/**
1066 * iucv_message_receive
1067 * @path: address of iucv path structure
1068 * @msg: address of iucv msg structure
1069 * @flags: how the message is received (IUCV_IPBUFLST)
1070 * @buffer: address of data buffer or address of struct iucv_array
1071 * @size: length of data buffer
1072 * @residual:
1073 *
1074 * This function receives messages that are being sent to you over
1075 * established paths. This function will deal with RMDATA messages
1076 * embedded in struct iucv_message as well.
1077 *
1078 * Locking: local_bh_enable/local_bh_disable
1079 *
1080 * Returns the result from the CP IUCV call.
1081 */
1082int iucv_message_receive(struct iucv_path *path, struct iucv_message *msg,
1083 u8 flags, void *buffer, size_t size, size_t *residual)
1084{
1085 int rc;
1086
1087 if (msg->flags & IUCV_IPRMDATA)
1088 return iucv_message_receive_iprmdata(path, msg, flags,
1089 buffer, size, residual);
1090 local_bh_disable();
1091 rc = __iucv_message_receive(path, msg, flags, buffer, size, residual);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001092 local_bh_enable();
1093 return rc;
1094}
Heiko Carstensda99f052007-05-04 12:23:27 -07001095EXPORT_SYMBOL(iucv_message_receive);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001096
1097/**
1098 * iucv_message_reject
1099 * @path: address of iucv path structure
1100 * @msg: address of iucv msg structure
1101 *
1102 * The reject function refuses a specified message. Between the time you
1103 * are notified of a message and the time that you complete the message,
1104 * the message may be rejected.
1105 *
1106 * Returns the result from the CP IUCV call.
1107 */
1108int iucv_message_reject(struct iucv_path *path, struct iucv_message *msg)
1109{
1110 union iucv_param *parm;
1111 int rc;
1112
1113 local_bh_disable();
Christoph Lameter70cf50352007-11-20 11:13:38 +01001114 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001115 memset(parm, 0, sizeof(union iucv_param));
1116 parm->db.ippathid = path->pathid;
1117 parm->db.ipmsgid = msg->id;
1118 parm->db.iptrgcls = msg->class;
1119 parm->db.ipflags1 = (IUCV_IPTRGCLS | IUCV_IPFGMID | IUCV_IPFGPID);
1120 rc = iucv_call_b2f0(IUCV_REJECT, parm);
1121 local_bh_enable();
1122 return rc;
1123}
Heiko Carstensda99f052007-05-04 12:23:27 -07001124EXPORT_SYMBOL(iucv_message_reject);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001125
1126/**
1127 * iucv_message_reply
1128 * @path: address of iucv path structure
1129 * @msg: address of iucv msg structure
1130 * @flags: how the reply is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1131 * @reply: address of reply data buffer or address of struct iucv_array
1132 * @size: length of reply data buffer
1133 *
1134 * This function responds to the two-way messages that you receive. You
1135 * must identify completely the message to which you wish to reply. ie,
1136 * pathid, msgid, and trgcls. Prmmsg signifies the data is moved into
1137 * the parameter list.
1138 *
1139 * Returns the result from the CP IUCV call.
1140 */
1141int iucv_message_reply(struct iucv_path *path, struct iucv_message *msg,
1142 u8 flags, void *reply, size_t size)
1143{
1144 union iucv_param *parm;
1145 int rc;
1146
1147 local_bh_disable();
Christoph Lameter70cf50352007-11-20 11:13:38 +01001148 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001149 memset(parm, 0, sizeof(union iucv_param));
1150 if (flags & IUCV_IPRMDATA) {
1151 parm->dpl.ippathid = path->pathid;
1152 parm->dpl.ipflags1 = flags;
1153 parm->dpl.ipmsgid = msg->id;
1154 parm->dpl.iptrgcls = msg->class;
1155 memcpy(parm->dpl.iprmmsg, reply, min_t(size_t, size, 8));
1156 } else {
1157 parm->db.ipbfadr1 = (u32)(addr_t) reply;
1158 parm->db.ipbfln1f = (u32) size;
1159 parm->db.ippathid = path->pathid;
1160 parm->db.ipflags1 = flags;
1161 parm->db.ipmsgid = msg->id;
1162 parm->db.iptrgcls = msg->class;
1163 }
1164 rc = iucv_call_b2f0(IUCV_REPLY, parm);
1165 local_bh_enable();
1166 return rc;
1167}
Heiko Carstensda99f052007-05-04 12:23:27 -07001168EXPORT_SYMBOL(iucv_message_reply);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001169
1170/**
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001171 * __iucv_message_send
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001172 * @path: address of iucv path structure
1173 * @msg: address of iucv msg structure
1174 * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1175 * @srccls: source class of message
1176 * @buffer: address of send buffer or address of struct iucv_array
1177 * @size: length of send buffer
1178 *
1179 * This function transmits data to another application. Data to be
1180 * transmitted is in a buffer and this is a one-way message and the
1181 * receiver will not reply to the message.
1182 *
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001183 * Locking: no locking
1184 *
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001185 * Returns the result from the CP IUCV call.
1186 */
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001187int __iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001188 u8 flags, u32 srccls, void *buffer, size_t size)
1189{
1190 union iucv_param *parm;
1191 int rc;
1192
Christoph Lameter70cf50352007-11-20 11:13:38 +01001193 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001194 memset(parm, 0, sizeof(union iucv_param));
1195 if (flags & IUCV_IPRMDATA) {
1196 /* Message of 8 bytes can be placed into the parameter list. */
1197 parm->dpl.ippathid = path->pathid;
1198 parm->dpl.ipflags1 = flags | IUCV_IPNORPY;
1199 parm->dpl.iptrgcls = msg->class;
1200 parm->dpl.ipsrccls = srccls;
1201 parm->dpl.ipmsgtag = msg->tag;
1202 memcpy(parm->dpl.iprmmsg, buffer, 8);
1203 } else {
1204 parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1205 parm->db.ipbfln1f = (u32) size;
1206 parm->db.ippathid = path->pathid;
1207 parm->db.ipflags1 = flags | IUCV_IPNORPY;
1208 parm->db.iptrgcls = msg->class;
1209 parm->db.ipsrccls = srccls;
1210 parm->db.ipmsgtag = msg->tag;
1211 }
1212 rc = iucv_call_b2f0(IUCV_SEND, parm);
1213 if (!rc)
1214 msg->id = parm->db.ipmsgid;
Hendrik Brueckner91d5d45e2008-12-25 13:38:58 +01001215 return rc;
1216}
1217EXPORT_SYMBOL(__iucv_message_send);
1218
1219/**
1220 * iucv_message_send
1221 * @path: address of iucv path structure
1222 * @msg: address of iucv msg structure
1223 * @flags: how the message is sent (IUCV_IPRMDATA, IUCV_IPPRTY, IUCV_IPBUFLST)
1224 * @srccls: source class of message
1225 * @buffer: address of send buffer or address of struct iucv_array
1226 * @size: length of send buffer
1227 *
1228 * This function transmits data to another application. Data to be
1229 * transmitted is in a buffer and this is a one-way message and the
1230 * receiver will not reply to the message.
1231 *
1232 * Locking: local_bh_enable/local_bh_disable
1233 *
1234 * Returns the result from the CP IUCV call.
1235 */
1236int iucv_message_send(struct iucv_path *path, struct iucv_message *msg,
1237 u8 flags, u32 srccls, void *buffer, size_t size)
1238{
1239 int rc;
1240
1241 local_bh_disable();
1242 rc = __iucv_message_send(path, msg, flags, srccls, buffer, size);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001243 local_bh_enable();
1244 return rc;
1245}
Heiko Carstensda99f052007-05-04 12:23:27 -07001246EXPORT_SYMBOL(iucv_message_send);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001247
1248/**
1249 * iucv_message_send2way
1250 * @path: address of iucv path structure
1251 * @msg: address of iucv msg structure
1252 * @flags: how the message is sent and the reply is received
1253 * (IUCV_IPRMDATA, IUCV_IPBUFLST, IUCV_IPPRTY, IUCV_ANSLST)
1254 * @srccls: source class of message
1255 * @buffer: address of send buffer or address of struct iucv_array
1256 * @size: length of send buffer
1257 * @ansbuf: address of answer buffer or address of struct iucv_array
1258 * @asize: size of reply buffer
1259 *
1260 * This function transmits data to another application. Data to be
1261 * transmitted is in a buffer. The receiver of the send is expected to
1262 * reply to the message and a buffer is provided into which IUCV moves
1263 * the reply to this message.
1264 *
1265 * Returns the result from the CP IUCV call.
1266 */
1267int iucv_message_send2way(struct iucv_path *path, struct iucv_message *msg,
1268 u8 flags, u32 srccls, void *buffer, size_t size,
1269 void *answer, size_t asize, size_t *residual)
1270{
1271 union iucv_param *parm;
1272 int rc;
1273
1274 local_bh_disable();
Christoph Lameter70cf50352007-11-20 11:13:38 +01001275 parm = iucv_param[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001276 memset(parm, 0, sizeof(union iucv_param));
1277 if (flags & IUCV_IPRMDATA) {
1278 parm->dpl.ippathid = path->pathid;
1279 parm->dpl.ipflags1 = path->flags; /* priority message */
1280 parm->dpl.iptrgcls = msg->class;
1281 parm->dpl.ipsrccls = srccls;
1282 parm->dpl.ipmsgtag = msg->tag;
1283 parm->dpl.ipbfadr2 = (u32)(addr_t) answer;
1284 parm->dpl.ipbfln2f = (u32) asize;
1285 memcpy(parm->dpl.iprmmsg, buffer, 8);
1286 } else {
1287 parm->db.ippathid = path->pathid;
1288 parm->db.ipflags1 = path->flags; /* priority message */
1289 parm->db.iptrgcls = msg->class;
1290 parm->db.ipsrccls = srccls;
1291 parm->db.ipmsgtag = msg->tag;
1292 parm->db.ipbfadr1 = (u32)(addr_t) buffer;
1293 parm->db.ipbfln1f = (u32) size;
1294 parm->db.ipbfadr2 = (u32)(addr_t) answer;
1295 parm->db.ipbfln2f = (u32) asize;
1296 }
1297 rc = iucv_call_b2f0(IUCV_SEND, parm);
1298 if (!rc)
1299 msg->id = parm->db.ipmsgid;
1300 local_bh_enable();
1301 return rc;
1302}
Heiko Carstensda99f052007-05-04 12:23:27 -07001303EXPORT_SYMBOL(iucv_message_send2way);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001304
1305/**
1306 * iucv_path_pending
1307 * @data: Pointer to external interrupt buffer
1308 *
1309 * Process connection pending work item. Called from tasklet while holding
1310 * iucv_table_lock.
1311 */
1312struct iucv_path_pending {
1313 u16 ippathid;
1314 u8 ipflags1;
1315 u8 iptype;
1316 u16 ipmsglim;
1317 u16 res1;
1318 u8 ipvmid[8];
1319 u8 ipuser[16];
1320 u32 res3;
1321 u8 ippollfg;
1322 u8 res4[3];
1323} __attribute__ ((packed));
1324
1325static void iucv_path_pending(struct iucv_irq_data *data)
1326{
1327 struct iucv_path_pending *ipp = (void *) data;
1328 struct iucv_handler *handler;
1329 struct iucv_path *path;
1330 char *error;
1331
1332 BUG_ON(iucv_path_table[ipp->ippathid]);
1333 /* New pathid, handler found. Create a new path struct. */
1334 error = iucv_error_no_memory;
1335 path = iucv_path_alloc(ipp->ipmsglim, ipp->ipflags1, GFP_ATOMIC);
1336 if (!path)
1337 goto out_sever;
1338 path->pathid = ipp->ippathid;
1339 iucv_path_table[path->pathid] = path;
1340 EBCASC(ipp->ipvmid, 8);
1341
1342 /* Call registered handler until one is found that wants the path. */
1343 list_for_each_entry(handler, &iucv_handler_list, list) {
1344 if (!handler->path_pending)
1345 continue;
1346 /*
1347 * Add path to handler to allow a call to iucv_path_sever
1348 * inside the path_pending function. If the handler returns
1349 * an error remove the path from the handler again.
1350 */
1351 list_add(&path->list, &handler->paths);
1352 path->handler = handler;
1353 if (!handler->path_pending(path, ipp->ipvmid, ipp->ipuser))
1354 return;
1355 list_del(&path->list);
1356 path->handler = NULL;
1357 }
1358 /* No handler wanted the path. */
1359 iucv_path_table[path->pathid] = NULL;
1360 iucv_path_free(path);
1361 error = iucv_error_no_listener;
1362out_sever:
1363 iucv_sever_pathid(ipp->ippathid, error);
1364}
1365
1366/**
1367 * iucv_path_complete
1368 * @data: Pointer to external interrupt buffer
1369 *
1370 * Process connection complete work item. Called from tasklet while holding
1371 * iucv_table_lock.
1372 */
1373struct iucv_path_complete {
1374 u16 ippathid;
1375 u8 ipflags1;
1376 u8 iptype;
1377 u16 ipmsglim;
1378 u16 res1;
1379 u8 res2[8];
1380 u8 ipuser[16];
1381 u32 res3;
1382 u8 ippollfg;
1383 u8 res4[3];
1384} __attribute__ ((packed));
1385
1386static void iucv_path_complete(struct iucv_irq_data *data)
1387{
1388 struct iucv_path_complete *ipc = (void *) data;
1389 struct iucv_path *path = iucv_path_table[ipc->ippathid];
1390
Hendrik Bruecknerb8942e32009-04-21 23:26:23 +00001391 if (path)
1392 path->flags = ipc->ipflags1;
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001393 if (path && path->handler && path->handler->path_complete)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001394 path->handler->path_complete(path, ipc->ipuser);
1395}
1396
1397/**
1398 * iucv_path_severed
1399 * @data: Pointer to external interrupt buffer
1400 *
1401 * Process connection severed work item. Called from tasklet while holding
1402 * iucv_table_lock.
1403 */
1404struct iucv_path_severed {
1405 u16 ippathid;
1406 u8 res1;
1407 u8 iptype;
1408 u32 res2;
1409 u8 res3[8];
1410 u8 ipuser[16];
1411 u32 res4;
1412 u8 ippollfg;
1413 u8 res5[3];
1414} __attribute__ ((packed));
1415
1416static void iucv_path_severed(struct iucv_irq_data *data)
1417{
1418 struct iucv_path_severed *ips = (void *) data;
1419 struct iucv_path *path = iucv_path_table[ips->ippathid];
1420
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001421 if (!path || !path->handler) /* Already severed */
1422 return;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001423 if (path->handler->path_severed)
1424 path->handler->path_severed(path, ips->ipuser);
1425 else {
1426 iucv_sever_pathid(path->pathid, NULL);
1427 iucv_path_table[path->pathid] = NULL;
Ursula Braun42e1b4c2009-04-21 23:26:20 +00001428 list_del(&path->list);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001429 iucv_path_free(path);
1430 }
1431}
1432
1433/**
1434 * iucv_path_quiesced
1435 * @data: Pointer to external interrupt buffer
1436 *
1437 * Process connection quiesced work item. Called from tasklet while holding
1438 * iucv_table_lock.
1439 */
1440struct iucv_path_quiesced {
1441 u16 ippathid;
1442 u8 res1;
1443 u8 iptype;
1444 u32 res2;
1445 u8 res3[8];
1446 u8 ipuser[16];
1447 u32 res4;
1448 u8 ippollfg;
1449 u8 res5[3];
1450} __attribute__ ((packed));
1451
1452static void iucv_path_quiesced(struct iucv_irq_data *data)
1453{
1454 struct iucv_path_quiesced *ipq = (void *) data;
1455 struct iucv_path *path = iucv_path_table[ipq->ippathid];
1456
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001457 if (path && path->handler && path->handler->path_quiesced)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001458 path->handler->path_quiesced(path, ipq->ipuser);
1459}
1460
1461/**
1462 * iucv_path_resumed
1463 * @data: Pointer to external interrupt buffer
1464 *
1465 * Process connection resumed work item. Called from tasklet while holding
1466 * iucv_table_lock.
1467 */
1468struct iucv_path_resumed {
1469 u16 ippathid;
1470 u8 res1;
1471 u8 iptype;
1472 u32 res2;
1473 u8 res3[8];
1474 u8 ipuser[16];
1475 u32 res4;
1476 u8 ippollfg;
1477 u8 res5[3];
1478} __attribute__ ((packed));
1479
1480static void iucv_path_resumed(struct iucv_irq_data *data)
1481{
1482 struct iucv_path_resumed *ipr = (void *) data;
1483 struct iucv_path *path = iucv_path_table[ipr->ippathid];
1484
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001485 if (path && path->handler && path->handler->path_resumed)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001486 path->handler->path_resumed(path, ipr->ipuser);
1487}
1488
1489/**
1490 * iucv_message_complete
1491 * @data: Pointer to external interrupt buffer
1492 *
1493 * Process message complete work item. Called from tasklet while holding
1494 * iucv_table_lock.
1495 */
1496struct iucv_message_complete {
1497 u16 ippathid;
1498 u8 ipflags1;
1499 u8 iptype;
1500 u32 ipmsgid;
1501 u32 ipaudit;
1502 u8 iprmmsg[8];
1503 u32 ipsrccls;
1504 u32 ipmsgtag;
1505 u32 res;
1506 u32 ipbfln2f;
1507 u8 ippollfg;
1508 u8 res2[3];
1509} __attribute__ ((packed));
1510
1511static void iucv_message_complete(struct iucv_irq_data *data)
1512{
1513 struct iucv_message_complete *imc = (void *) data;
1514 struct iucv_path *path = iucv_path_table[imc->ippathid];
1515 struct iucv_message msg;
1516
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001517 if (path && path->handler && path->handler->message_complete) {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001518 msg.flags = imc->ipflags1;
1519 msg.id = imc->ipmsgid;
1520 msg.audit = imc->ipaudit;
1521 memcpy(msg.rmmsg, imc->iprmmsg, 8);
1522 msg.class = imc->ipsrccls;
1523 msg.tag = imc->ipmsgtag;
1524 msg.length = imc->ipbfln2f;
1525 path->handler->message_complete(path, &msg);
1526 }
1527}
1528
1529/**
1530 * iucv_message_pending
1531 * @data: Pointer to external interrupt buffer
1532 *
1533 * Process message pending work item. Called from tasklet while holding
1534 * iucv_table_lock.
1535 */
1536struct iucv_message_pending {
1537 u16 ippathid;
1538 u8 ipflags1;
1539 u8 iptype;
1540 u32 ipmsgid;
1541 u32 iptrgcls;
1542 union {
1543 u32 iprmmsg1_u32;
1544 u8 iprmmsg1[4];
1545 } ln1msg1;
1546 union {
1547 u32 ipbfln1f;
1548 u8 iprmmsg2[4];
1549 } ln1msg2;
1550 u32 res1[3];
1551 u32 ipbfln2f;
1552 u8 ippollfg;
1553 u8 res2[3];
1554} __attribute__ ((packed));
1555
1556static void iucv_message_pending(struct iucv_irq_data *data)
1557{
1558 struct iucv_message_pending *imp = (void *) data;
1559 struct iucv_path *path = iucv_path_table[imp->ippathid];
1560 struct iucv_message msg;
1561
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001562 if (path && path->handler && path->handler->message_pending) {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001563 msg.flags = imp->ipflags1;
1564 msg.id = imp->ipmsgid;
1565 msg.class = imp->iptrgcls;
1566 if (imp->ipflags1 & IUCV_IPRMDATA) {
1567 memcpy(msg.rmmsg, imp->ln1msg1.iprmmsg1, 8);
1568 msg.length = 8;
1569 } else
1570 msg.length = imp->ln1msg2.ipbfln1f;
1571 msg.reply_size = imp->ipbfln2f;
1572 path->handler->message_pending(path, &msg);
1573 }
1574}
1575
1576/**
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001577 * iucv_tasklet_fn:
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001578 *
1579 * This tasklet loops over the queue of irq buffers created by
1580 * iucv_external_interrupt, calls the appropriate action handler
1581 * and then frees the buffer.
1582 */
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001583static void iucv_tasklet_fn(unsigned long ignored)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001584{
1585 typedef void iucv_irq_fn(struct iucv_irq_data *);
1586 static iucv_irq_fn *irq_fn[] = {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001587 [0x02] = iucv_path_complete,
1588 [0x03] = iucv_path_severed,
1589 [0x04] = iucv_path_quiesced,
1590 [0x05] = iucv_path_resumed,
1591 [0x06] = iucv_message_complete,
1592 [0x07] = iucv_message_complete,
1593 [0x08] = iucv_message_pending,
1594 [0x09] = iucv_message_pending,
1595 };
Denis Chengb5e78332007-12-07 00:51:45 -08001596 LIST_HEAD(task_queue);
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001597 struct iucv_irq_list *p, *n;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001598
1599 /* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
Ursula Braun13fdc9a2007-07-14 19:03:41 -07001600 if (!spin_trylock(&iucv_table_lock)) {
1601 tasklet_schedule(&iucv_tasklet);
1602 return;
1603 }
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001604 iucv_active_cpu = smp_processor_id();
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001605
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001606 spin_lock_irq(&iucv_queue_lock);
1607 list_splice_init(&iucv_task_queue, &task_queue);
1608 spin_unlock_irq(&iucv_queue_lock);
1609
1610 list_for_each_entry_safe(p, n, &task_queue, list) {
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001611 list_del_init(&p->list);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001612 irq_fn[p->data.iptype](&p->data);
1613 kfree(p);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001614 }
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001615
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001616 iucv_active_cpu = -1;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001617 spin_unlock(&iucv_table_lock);
1618}
1619
1620/**
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001621 * iucv_work_fn:
1622 *
1623 * This work function loops over the queue of path pending irq blocks
1624 * created by iucv_external_interrupt, calls the appropriate action
1625 * handler and then frees the buffer.
1626 */
1627static void iucv_work_fn(struct work_struct *work)
1628{
1629 typedef void iucv_irq_fn(struct iucv_irq_data *);
Denis Chengb5e78332007-12-07 00:51:45 -08001630 LIST_HEAD(work_queue);
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001631 struct iucv_irq_list *p, *n;
1632
1633 /* Serialize tasklet, iucv_path_sever and iucv_path_connect. */
1634 spin_lock_bh(&iucv_table_lock);
1635 iucv_active_cpu = smp_processor_id();
1636
1637 spin_lock_irq(&iucv_queue_lock);
1638 list_splice_init(&iucv_work_queue, &work_queue);
1639 spin_unlock_irq(&iucv_queue_lock);
1640
1641 iucv_cleanup_queue();
1642 list_for_each_entry_safe(p, n, &work_queue, list) {
1643 list_del_init(&p->list);
1644 iucv_path_pending(&p->data);
1645 kfree(p);
1646 }
1647
1648 iucv_active_cpu = -1;
1649 spin_unlock_bh(&iucv_table_lock);
1650}
1651
1652/**
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001653 * iucv_external_interrupt
1654 * @code: irq code
1655 *
1656 * Handles external interrupts coming in from CP.
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001657 * Places the interrupt buffer on a queue and schedules iucv_tasklet_fn().
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001658 */
1659static void iucv_external_interrupt(u16 code)
1660{
1661 struct iucv_irq_data *p;
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001662 struct iucv_irq_list *work;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001663
Christoph Lameter70cf50352007-11-20 11:13:38 +01001664 p = iucv_irq_data[smp_processor_id()];
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001665 if (p->ippathid >= iucv_max_pathid) {
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001666 WARN_ON(p->ippathid >= iucv_max_pathid);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001667 iucv_sever_pathid(p->ippathid, iucv_error_no_listener);
1668 return;
1669 }
Ursula Braunc2b4afd2008-07-14 09:59:29 +02001670 BUG_ON(p->iptype < 0x01 || p->iptype > 0x09);
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001671 work = kmalloc(sizeof(struct iucv_irq_list), GFP_ATOMIC);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001672 if (!work) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01001673 pr_warning("iucv_external_interrupt: out of memory\n");
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001674 return;
1675 }
1676 memcpy(&work->data, p, sizeof(work->data));
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001677 spin_lock(&iucv_queue_lock);
1678 if (p->iptype == 0x01) {
1679 /* Path pending interrupt. */
1680 list_add_tail(&work->list, &iucv_work_queue);
1681 schedule_work(&iucv_work);
1682 } else {
1683 /* The other interrupts. */
1684 list_add_tail(&work->list, &iucv_task_queue);
1685 tasklet_schedule(&iucv_tasklet);
1686 }
1687 spin_unlock(&iucv_queue_lock);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001688}
1689
1690/**
1691 * iucv_init
1692 *
1693 * Allocates and initializes various data structures.
1694 */
Heiko Carstensda99f052007-05-04 12:23:27 -07001695static int __init iucv_init(void)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001696{
1697 int rc;
Christoph Lameter70cf50352007-11-20 11:13:38 +01001698 int cpu;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001699
1700 if (!MACHINE_IS_VM) {
1701 rc = -EPROTONOSUPPORT;
1702 goto out;
1703 }
1704 rc = iucv_query_maxconn();
1705 if (rc)
1706 goto out;
Heiko Carstensda99f052007-05-04 12:23:27 -07001707 rc = register_external_interrupt(0x4000, iucv_external_interrupt);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001708 if (rc)
1709 goto out;
Mark McLoughlin035da162008-12-15 12:58:29 +00001710 iucv_root = root_device_register("iucv");
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001711 if (IS_ERR(iucv_root)) {
1712 rc = PTR_ERR(iucv_root);
Cornelia Huck2d7bf362008-04-10 02:12:45 -07001713 goto out_int;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001714 }
Christoph Lameter70cf50352007-11-20 11:13:38 +01001715
1716 for_each_online_cpu(cpu) {
1717 /* Note: GFP_DMA used to get memory below 2G */
1718 iucv_irq_data[cpu] = kmalloc_node(sizeof(struct iucv_irq_data),
1719 GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
1720 if (!iucv_irq_data[cpu]) {
1721 rc = -ENOMEM;
1722 goto out_free;
1723 }
1724
1725 /* Allocate parameter blocks. */
1726 iucv_param[cpu] = kmalloc_node(sizeof(union iucv_param),
1727 GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
1728 if (!iucv_param[cpu]) {
1729 rc = -ENOMEM;
1730 goto out_free;
1731 }
Ursula Braun42e1b4c2009-04-21 23:26:20 +00001732 iucv_param_irq[cpu] = kmalloc_node(sizeof(union iucv_param),
1733 GFP_KERNEL|GFP_DMA, cpu_to_node(cpu));
1734 if (!iucv_param_irq[cpu]) {
1735 rc = -ENOMEM;
1736 goto out_free;
1737 }
1738
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001739 }
Cornelia Huck2d7bf362008-04-10 02:12:45 -07001740 rc = register_hotcpu_notifier(&iucv_cpu_notifier);
1741 if (rc)
1742 goto out_free;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001743 ASCEBC(iucv_error_no_listener, 16);
1744 ASCEBC(iucv_error_no_memory, 16);
1745 ASCEBC(iucv_error_pathid, 16);
1746 iucv_available = 1;
Cornelia Huck2d7bf362008-04-10 02:12:45 -07001747 rc = bus_register(&iucv_bus);
1748 if (rc)
1749 goto out_cpu;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001750 return 0;
1751
Cornelia Huck2d7bf362008-04-10 02:12:45 -07001752out_cpu:
1753 unregister_hotcpu_notifier(&iucv_cpu_notifier);
Christoph Lameter70cf50352007-11-20 11:13:38 +01001754out_free:
1755 for_each_possible_cpu(cpu) {
Ursula Braun42e1b4c2009-04-21 23:26:20 +00001756 kfree(iucv_param_irq[cpu]);
1757 iucv_param_irq[cpu] = NULL;
Christoph Lameter70cf50352007-11-20 11:13:38 +01001758 kfree(iucv_param[cpu]);
1759 iucv_param[cpu] = NULL;
1760 kfree(iucv_irq_data[cpu]);
1761 iucv_irq_data[cpu] = NULL;
1762 }
Mark McLoughlin035da162008-12-15 12:58:29 +00001763 root_device_unregister(iucv_root);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001764out_int:
1765 unregister_external_interrupt(0x4000, iucv_external_interrupt);
1766out:
1767 return rc;
1768}
1769
1770/**
1771 * iucv_exit
1772 *
1773 * Frees everything allocated from iucv_init.
1774 */
Heiko Carstensda99f052007-05-04 12:23:27 -07001775static void __exit iucv_exit(void)
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001776{
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001777 struct iucv_irq_list *p, *n;
Christoph Lameter70cf50352007-11-20 11:13:38 +01001778 int cpu;
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001779
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001780 spin_lock_irq(&iucv_queue_lock);
1781 list_for_each_entry_safe(p, n, &iucv_task_queue, list)
1782 kfree(p);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001783 list_for_each_entry_safe(p, n, &iucv_work_queue, list)
1784 kfree(p);
Martin Schwidefsky04b090d2007-04-28 23:03:59 -07001785 spin_unlock_irq(&iucv_queue_lock);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001786 unregister_hotcpu_notifier(&iucv_cpu_notifier);
Christoph Lameter70cf50352007-11-20 11:13:38 +01001787 for_each_possible_cpu(cpu) {
Ursula Braun42e1b4c2009-04-21 23:26:20 +00001788 kfree(iucv_param_irq[cpu]);
1789 iucv_param_irq[cpu] = NULL;
Christoph Lameter70cf50352007-11-20 11:13:38 +01001790 kfree(iucv_param[cpu]);
1791 iucv_param[cpu] = NULL;
1792 kfree(iucv_irq_data[cpu]);
1793 iucv_irq_data[cpu] = NULL;
1794 }
Mark McLoughlin035da162008-12-15 12:58:29 +00001795 root_device_unregister(iucv_root);
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001796 bus_unregister(&iucv_bus);
1797 unregister_external_interrupt(0x4000, iucv_external_interrupt);
1798}
1799
1800subsys_initcall(iucv_init);
1801module_exit(iucv_exit);
1802
Martin Schwidefsky2356f4c2007-02-08 13:37:42 -08001803MODULE_AUTHOR("(C) 2001 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
1804MODULE_DESCRIPTION("Linux for S/390 IUCV lowlevel driver");
1805MODULE_LICENSE("GPL");