blob: b0e8ffdf864b049c1d72797c039b91a6e29d373e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * IUCV network driver
3 *
Ursula Braun1175b252009-06-16 10:30:43 +02004 * Copyright IBM Corp. 2001, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Ursula Braun1175b252009-06-16 10:30:43 +02006 * Author(s):
7 * Original netiucv driver:
8 * Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
9 * Sysfs integration and all bugs therein:
10 * Cornelia Huck (cornelia.huck@de.ibm.com)
11 * PM functions:
12 * Ursula Braun (ursula.braun@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * Documentation used:
15 * the source of the original IUCV driver by:
16 * Stefan Hegewald <hegewald@de.ibm.com>
17 * Hartmut Penner <hpenner@de.ibm.com>
18 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
19 * Martin Schwidefsky (schwidefsky@de.ibm.com)
20 * Alan Altmark (Alan_Altmark@us.ibm.com) Sept. 2000
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2, or (at your option)
25 * any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Jeff Garzike82b0f22006-05-26 21:58:38 -040037
Ursula Braun8f7c5022008-12-25 13:39:47 +010038#define KMSG_COMPONENT "netiucv"
39#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#undef DEBUG
42
43#include <linux/module.h>
44#include <linux/init.h>
45#include <linux/kernel.h>
46#include <linux/slab.h>
47#include <linux/errno.h>
48#include <linux/types.h>
49#include <linux/interrupt.h>
50#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/bitops.h>
52
53#include <linux/signal.h>
54#include <linux/string.h>
55#include <linux/device.h>
56
57#include <linux/ip.h>
58#include <linux/if_arp.h>
59#include <linux/tcp.h>
60#include <linux/skbuff.h>
61#include <linux/ctype.h>
62#include <net/dst.h>
63
64#include <asm/io.h>
65#include <asm/uaccess.h>
Ursula Braun08e33562011-12-19 22:56:34 +000066#include <asm/ebcdic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Martin Schwidefskyeebce382007-02-08 13:50:33 -080068#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include "fsm.h"
70
71MODULE_AUTHOR
72 ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
73MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
74
Martin Schwidefskyeebce382007-02-08 13:50:33 -080075/**
76 * Debug Facility stuff
77 */
78#define IUCV_DBF_SETUP_NAME "iucv_setup"
Ursula Braun08e33562011-12-19 22:56:34 +000079#define IUCV_DBF_SETUP_LEN 64
Martin Schwidefskyeebce382007-02-08 13:50:33 -080080#define IUCV_DBF_SETUP_PAGES 2
81#define IUCV_DBF_SETUP_NR_AREAS 1
82#define IUCV_DBF_SETUP_LEVEL 3
83
84#define IUCV_DBF_DATA_NAME "iucv_data"
85#define IUCV_DBF_DATA_LEN 128
86#define IUCV_DBF_DATA_PAGES 2
87#define IUCV_DBF_DATA_NR_AREAS 1
88#define IUCV_DBF_DATA_LEVEL 2
89
90#define IUCV_DBF_TRACE_NAME "iucv_trace"
91#define IUCV_DBF_TRACE_LEN 16
92#define IUCV_DBF_TRACE_PAGES 4
93#define IUCV_DBF_TRACE_NR_AREAS 1
94#define IUCV_DBF_TRACE_LEVEL 3
95
96#define IUCV_DBF_TEXT(name,level,text) \
97 do { \
98 debug_text_event(iucv_dbf_##name,level,text); \
99 } while (0)
100
101#define IUCV_DBF_HEX(name,level,addr,len) \
102 do { \
103 debug_event(iucv_dbf_##name,level,(void*)(addr),len); \
104 } while (0)
105
106DECLARE_PER_CPU(char[256], iucv_dbf_txt_buf);
107
Peter Tiedemannf33780d2008-02-08 13:09:05 +0100108#define IUCV_DBF_TEXT_(name, level, text...) \
109 do { \
Hendrik Brueckner8e6a8282013-09-18 17:21:34 +0200110 if (debug_level_enabled(iucv_dbf_##name, level)) { \
Tejun Heo390dfd92009-10-29 22:34:14 +0900111 char* __buf = get_cpu_var(iucv_dbf_txt_buf); \
112 sprintf(__buf, text); \
113 debug_text_event(iucv_dbf_##name, level, __buf); \
Peter Tiedemannf33780d2008-02-08 13:09:05 +0100114 put_cpu_var(iucv_dbf_txt_buf); \
115 } \
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800116 } while (0)
117
118#define IUCV_DBF_SPRINTF(name,level,text...) \
119 do { \
120 debug_sprintf_event(iucv_dbf_trace, level, ##text ); \
121 debug_sprintf_event(iucv_dbf_trace, level, text ); \
122 } while (0)
123
124/**
125 * some more debug stuff
126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define PRINTK_HEADER " iucv: " /* for debugging */
128
Ursula Braun1175b252009-06-16 10:30:43 +0200129/* dummy device to make sure netiucv_pm functions are called */
130static struct device *netiucv_dev;
131
132static int netiucv_pm_prepare(struct device *);
133static void netiucv_pm_complete(struct device *);
134static int netiucv_pm_freeze(struct device *);
135static int netiucv_pm_restore_thaw(struct device *);
136
Alexey Dobriyan47145212009-12-14 18:00:08 -0800137static const struct dev_pm_ops netiucv_pm_ops = {
Ursula Braun1175b252009-06-16 10:30:43 +0200138 .prepare = netiucv_pm_prepare,
139 .complete = netiucv_pm_complete,
140 .freeze = netiucv_pm_freeze,
141 .thaw = netiucv_pm_restore_thaw,
142 .restore = netiucv_pm_restore_thaw,
143};
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static struct device_driver netiucv_driver = {
Cornelia Huck22195102008-02-08 13:09:02 +0100146 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .name = "netiucv",
148 .bus = &iucv_bus,
Ursula Braun1175b252009-06-16 10:30:43 +0200149 .pm = &netiucv_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Ursula Braun91e60eb2015-09-18 16:06:52 +0200152static int netiucv_callback_connreq(struct iucv_path *, u8 *, u8 *);
153static void netiucv_callback_connack(struct iucv_path *, u8 *);
154static void netiucv_callback_connrej(struct iucv_path *, u8 *);
155static void netiucv_callback_connsusp(struct iucv_path *, u8 *);
156static void netiucv_callback_connres(struct iucv_path *, u8 *);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800157static void netiucv_callback_rx(struct iucv_path *, struct iucv_message *);
158static void netiucv_callback_txdone(struct iucv_path *, struct iucv_message *);
159
160static struct iucv_handler netiucv_handler = {
161 .path_pending = netiucv_callback_connreq,
162 .path_complete = netiucv_callback_connack,
163 .path_severed = netiucv_callback_connrej,
164 .path_quiesced = netiucv_callback_connsusp,
165 .path_resumed = netiucv_callback_connres,
166 .message_pending = netiucv_callback_rx,
167 .message_complete = netiucv_callback_txdone
168};
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/**
171 * Per connection profiling data
172 */
173struct connection_profile {
174 unsigned long maxmulti;
175 unsigned long maxcqueue;
176 unsigned long doios_single;
177 unsigned long doios_multi;
178 unsigned long txlen;
179 unsigned long tx_time;
Aya Mahfouzee6edb92015-01-16 14:05:45 +0100180 unsigned long send_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned long tx_pending;
182 unsigned long tx_max_pending;
183};
184
185/**
186 * Representation of one iucv connection
187 */
188struct iucv_connection {
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800189 struct list_head list;
190 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct sk_buff *rx_buff;
192 struct sk_buff *tx_buff;
193 struct sk_buff_head collect_queue;
194 struct sk_buff_head commit_queue;
195 spinlock_t collect_lock;
196 int collect_len;
197 int max_buffsize;
198 fsm_timer timer;
199 fsm_instance *fsm;
200 struct net_device *netdev;
201 struct connection_profile prof;
202 char userid[9];
Ursula Braun08e33562011-12-19 22:56:34 +0000203 char userdata[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206/**
207 * Linked list of all connection structs.
208 */
Denis Chengc11ca972008-01-26 14:11:13 +0100209static LIST_HEAD(iucv_connection_list);
Thomas Gleixnerbfac0d02007-06-20 13:02:55 +0200210static DEFINE_RWLOCK(iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/**
213 * Representation of event-data for the
214 * connection state machine.
215 */
216struct iucv_event {
217 struct iucv_connection *conn;
218 void *data;
219};
220
221/**
222 * Private part of the network device structure
223 */
224struct netiucv_priv {
225 struct net_device_stats stats;
226 unsigned long tbusy;
227 fsm_instance *fsm;
228 struct iucv_connection *conn;
229 struct device *dev;
Ursula Braun1175b252009-06-16 10:30:43 +0200230 int pm_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
233/**
234 * Link level header for a packet.
235 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800236struct ll_header {
237 u16 next;
238};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800240#define NETIUCV_HDRLEN (sizeof(struct ll_header))
Ursula Braun08e33562011-12-19 22:56:34 +0000241#define NETIUCV_BUFSIZE_MAX 65537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX
243#define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
244#define NETIUCV_MTU_DEFAULT 9216
245#define NETIUCV_QUEUELEN_DEFAULT 50
246#define NETIUCV_TIMEOUT_5SEC 5000
247
248/**
249 * Compatibility macros for busy handling
250 * of network devices.
251 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800252static inline void netiucv_clear_busy(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800254 struct netiucv_priv *priv = netdev_priv(dev);
255 clear_bit(0, &priv->tbusy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 netif_wake_queue(dev);
257}
258
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800259static inline int netiucv_test_and_set_busy(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800261 struct netiucv_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 netif_stop_queue(dev);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800263 return test_and_set_bit(0, &priv->tbusy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Ursula Braun08e33562011-12-19 22:56:34 +0000266static u8 iucvMagic_ascii[16] = {
267 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
268 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
269};
270
271static u8 iucvMagic_ebcdic[16] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
273 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
274};
275
276/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Convert an iucv userId to its printable
278 * form (strip whitespace at end).
279 *
280 * @param An iucv userId
281 *
282 * @returns The printable string (static data!!)
283 */
Ursula Braun08e33562011-12-19 22:56:34 +0000284static char *netiucv_printname(char *name, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Ursula Braun08e33562011-12-19 22:56:34 +0000286 static char tmp[17];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 char *p = tmp;
Ursula Braun08e33562011-12-19 22:56:34 +0000288 memcpy(tmp, name, len);
289 tmp[len] = '\0';
290 while (*p && ((p - tmp) < len) && (!isspace(*p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 p++;
292 *p = '\0';
293 return tmp;
294}
Jeff Garzike82b0f22006-05-26 21:58:38 -0400295
Ursula Braun08e33562011-12-19 22:56:34 +0000296static char *netiucv_printuser(struct iucv_connection *conn)
297{
298 static char tmp_uid[9];
299 static char tmp_udat[17];
300 static char buf[100];
301
302 if (memcmp(conn->userdata, iucvMagic_ebcdic, 16)) {
303 tmp_uid[8] = '\0';
304 tmp_udat[16] = '\0';
305 memcpy(tmp_uid, conn->userid, 8);
306 memcpy(tmp_uid, netiucv_printname(tmp_uid, 8), 8);
307 memcpy(tmp_udat, conn->userdata, 16);
308 EBCASC(tmp_udat, 16);
309 memcpy(tmp_udat, netiucv_printname(tmp_udat, 16), 16);
310 sprintf(buf, "%s.%s", tmp_uid, tmp_udat);
311 return buf;
312 } else
313 return netiucv_printname(conn->userid, 8);
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/**
317 * States of the interface statemachine.
318 */
319enum dev_states {
320 DEV_STATE_STOPPED,
321 DEV_STATE_STARTWAIT,
322 DEV_STATE_STOPWAIT,
323 DEV_STATE_RUNNING,
324 /**
325 * MUST be always the last element!!
326 */
327 NR_DEV_STATES
328};
329
330static const char *dev_state_names[] = {
331 "Stopped",
332 "StartWait",
333 "StopWait",
334 "Running",
335};
336
337/**
338 * Events of the interface statemachine.
339 */
340enum dev_events {
341 DEV_EVENT_START,
342 DEV_EVENT_STOP,
343 DEV_EVENT_CONUP,
344 DEV_EVENT_CONDOWN,
345 /**
346 * MUST be always the last element!!
347 */
348 NR_DEV_EVENTS
349};
350
351static const char *dev_event_names[] = {
352 "Start",
353 "Stop",
354 "Connection up",
355 "Connection down",
356};
Jeff Garzike82b0f22006-05-26 21:58:38 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
359 * Events of the connection statemachine
360 */
361enum conn_events {
362 /**
363 * Events, representing callbacks from
364 * lowlevel iucv layer)
365 */
366 CONN_EVENT_CONN_REQ,
367 CONN_EVENT_CONN_ACK,
368 CONN_EVENT_CONN_REJ,
369 CONN_EVENT_CONN_SUS,
370 CONN_EVENT_CONN_RES,
371 CONN_EVENT_RX,
372 CONN_EVENT_TXDONE,
373
374 /**
375 * Events, representing errors return codes from
376 * calls to lowlevel iucv layer
377 */
378
379 /**
380 * Event, representing timer expiry.
381 */
382 CONN_EVENT_TIMER,
383
384 /**
385 * Events, representing commands from upper levels.
386 */
387 CONN_EVENT_START,
388 CONN_EVENT_STOP,
389
390 /**
391 * MUST be always the last element!!
392 */
393 NR_CONN_EVENTS,
394};
395
396static const char *conn_event_names[] = {
397 "Remote connection request",
398 "Remote connection acknowledge",
399 "Remote connection reject",
400 "Connection suspended",
401 "Connection resumed",
402 "Data received",
403 "Data sent",
404
405 "Timer",
406
407 "Start",
408 "Stop",
409};
410
411/**
412 * States of the connection statemachine.
413 */
414enum conn_states {
415 /**
416 * Connection not assigned to any device,
417 * initial state, invalid
418 */
419 CONN_STATE_INVALID,
420
421 /**
422 * Userid assigned but not operating
423 */
424 CONN_STATE_STOPPED,
425
426 /**
427 * Connection registered,
428 * no connection request sent yet,
429 * no connection request received
430 */
431 CONN_STATE_STARTWAIT,
432
433 /**
434 * Connection registered and connection request sent,
435 * no acknowledge and no connection request received yet.
436 */
437 CONN_STATE_SETUPWAIT,
438
439 /**
440 * Connection up and running idle
441 */
442 CONN_STATE_IDLE,
443
444 /**
445 * Data sent, awaiting CONN_EVENT_TXDONE
446 */
447 CONN_STATE_TX,
448
449 /**
450 * Error during registration.
451 */
452 CONN_STATE_REGERR,
453
454 /**
455 * Error during registration.
456 */
457 CONN_STATE_CONNERR,
458
459 /**
460 * MUST be always the last element!!
461 */
462 NR_CONN_STATES,
463};
464
465static const char *conn_state_names[] = {
466 "Invalid",
467 "Stopped",
468 "StartWait",
469 "SetupWait",
470 "Idle",
471 "TX",
472 "Terminating",
473 "Registration error",
474 "Connect error",
475};
476
Jeff Garzike82b0f22006-05-26 21:58:38 -0400477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/**
479 * Debug Facility Stuff
480 */
481static debug_info_t *iucv_dbf_setup = NULL;
482static debug_info_t *iucv_dbf_data = NULL;
483static debug_info_t *iucv_dbf_trace = NULL;
484
485DEFINE_PER_CPU(char[256], iucv_dbf_txt_buf);
486
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800487static void iucv_unregister_dbf_views(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Markus Elfringb646c082015-01-16 14:05:46 +0100489 debug_unregister(iucv_dbf_setup);
490 debug_unregister(iucv_dbf_data);
491 debug_unregister(iucv_dbf_trace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800493static int iucv_register_dbf_views(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 iucv_dbf_setup = debug_register(IUCV_DBF_SETUP_NAME,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700496 IUCV_DBF_SETUP_PAGES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 IUCV_DBF_SETUP_NR_AREAS,
498 IUCV_DBF_SETUP_LEN);
499 iucv_dbf_data = debug_register(IUCV_DBF_DATA_NAME,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700500 IUCV_DBF_DATA_PAGES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 IUCV_DBF_DATA_NR_AREAS,
502 IUCV_DBF_DATA_LEN);
503 iucv_dbf_trace = debug_register(IUCV_DBF_TRACE_NAME,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700504 IUCV_DBF_TRACE_PAGES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 IUCV_DBF_TRACE_NR_AREAS,
506 IUCV_DBF_TRACE_LEN);
507
508 if ((iucv_dbf_setup == NULL) || (iucv_dbf_data == NULL) ||
509 (iucv_dbf_trace == NULL)) {
510 iucv_unregister_dbf_views();
511 return -ENOMEM;
512 }
513 debug_register_view(iucv_dbf_setup, &debug_hex_ascii_view);
514 debug_set_level(iucv_dbf_setup, IUCV_DBF_SETUP_LEVEL);
515
516 debug_register_view(iucv_dbf_data, &debug_hex_ascii_view);
517 debug_set_level(iucv_dbf_data, IUCV_DBF_DATA_LEVEL);
518
519 debug_register_view(iucv_dbf_trace, &debug_hex_ascii_view);
520 debug_set_level(iucv_dbf_trace, IUCV_DBF_TRACE_LEVEL);
521
522 return 0;
523}
524
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800525/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * Callback-wrappers, called from lowlevel iucv layer.
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800529static void netiucv_callback_rx(struct iucv_path *path,
530 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800532 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct iucv_event ev;
534
535 ev.conn = conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800536 ev.data = msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
538}
539
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800540static void netiucv_callback_txdone(struct iucv_path *path,
541 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800543 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct iucv_event ev;
545
546 ev.conn = conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800547 ev.data = msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
549}
550
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800551static void netiucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800553 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800555 fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Ursula Braun91e60eb2015-09-18 16:06:52 +0200558static int netiucv_callback_connreq(struct iucv_path *path, u8 *ipvmid,
559 u8 *ipuser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800561 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct iucv_event ev;
Ursula Braun08e33562011-12-19 22:56:34 +0000563 static char tmp_user[9];
564 static char tmp_udat[17];
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800565 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800567 rc = -EINVAL;
Ursula Braun08e33562011-12-19 22:56:34 +0000568 memcpy(tmp_user, netiucv_printname(ipvmid, 8), 8);
569 memcpy(tmp_udat, ipuser, 16);
570 EBCASC(tmp_udat, 16);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800571 read_lock_bh(&iucv_connection_rwlock);
572 list_for_each_entry(conn, &iucv_connection_list, list) {
Ursula Braun08e33562011-12-19 22:56:34 +0000573 if (strncmp(ipvmid, conn->userid, 8) ||
574 strncmp(ipuser, conn->userdata, 16))
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800575 continue;
576 /* Found a matching connection for this path. */
577 conn->path = path;
578 ev.conn = conn;
579 ev.data = path;
580 fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
581 rc = 0;
582 }
Ursula Braun08e33562011-12-19 22:56:34 +0000583 IUCV_DBF_TEXT_(setup, 2, "Connection requested for %s.%s\n",
584 tmp_user, netiucv_printname(tmp_udat, 16));
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800585 read_unlock_bh(&iucv_connection_rwlock);
586 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Ursula Braun91e60eb2015-09-18 16:06:52 +0200589static void netiucv_callback_connrej(struct iucv_path *path, u8 *ipuser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800591 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800593 fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Ursula Braun91e60eb2015-09-18 16:06:52 +0200596static void netiucv_callback_connsusp(struct iucv_path *path, u8 *ipuser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800598 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800600 fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Ursula Braun91e60eb2015-09-18 16:06:52 +0200603static void netiucv_callback_connres(struct iucv_path *path, u8 *ipuser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800605 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800607 fsm_event(conn->fsm, CONN_EVENT_CONN_RES, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/**
Ursula Braun21b26f2f2008-02-08 13:09:03 +0100611 * NOP action for statemachines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Ursula Braun21b26f2f2008-02-08 13:09:03 +0100613static void netiucv_action_nop(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615}
Jeff Garzike82b0f22006-05-26 21:58:38 -0400616
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800617/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * Actions of the connection statemachine
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800619 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621/**
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800622 * netiucv_unpack_skb
623 * @conn: The connection where this skb has been received.
624 * @pskb: The received skb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800626 * Unpack a just received skb and hand it over to upper layers.
627 * Helper function for conn_action_rx.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800629static void netiucv_unpack_skb(struct iucv_connection *conn,
630 struct sk_buff *pskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct net_device *dev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800633 struct netiucv_priv *privptr = netdev_priv(dev);
634 u16 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 skb_put(pskb, NETIUCV_HDRLEN);
637 pskb->dev = dev;
638 pskb->ip_summed = CHECKSUM_NONE;
639 pskb->protocol = ntohs(ETH_P_IP);
640
641 while (1) {
642 struct sk_buff *skb;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800643 struct ll_header *header = (struct ll_header *) pskb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (!header->next)
646 break;
647
648 skb_pull(pskb, NETIUCV_HDRLEN);
649 header->next -= offset;
650 offset += header->next;
651 header->next -= NETIUCV_HDRLEN;
652 if (skb_tailroom(pskb) < header->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 IUCV_DBF_TEXT_(data, 2, "Illegal next field: %d > %d\n",
654 header->next, skb_tailroom(pskb));
655 return;
656 }
657 skb_put(pskb, header->next);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700658 skb_reset_mac_header(pskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 skb = dev_alloc_skb(pskb->len);
660 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 IUCV_DBF_TEXT(data, 2,
662 "Out of memory in netiucv_unpack_skb\n");
663 privptr->stats.rx_dropped++;
664 return;
665 }
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300666 skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
667 pskb->len);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700668 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 skb->dev = pskb->dev;
670 skb->protocol = pskb->protocol;
671 pskb->ip_summed = CHECKSUM_UNNECESSARY;
Julia Lawall9b3efc02007-12-10 17:17:37 -0800672 privptr->stats.rx_packets++;
673 privptr->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /*
675 * Since receiving is always initiated from a tasklet (in iucv.c),
676 * we must use netif_rx_ni() instead of netif_rx()
677 */
678 netif_rx_ni(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 skb_pull(pskb, header->next);
680 skb_put(pskb, NETIUCV_HDRLEN);
681 }
682}
683
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800684static void conn_action_rx(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800686 struct iucv_event *ev = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct iucv_connection *conn = ev->conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800688 struct iucv_message *msg = ev->data;
689 struct netiucv_priv *privptr = netdev_priv(conn->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int rc;
691
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200692 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (!conn->netdev) {
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800695 iucv_message_reject(conn->path, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 IUCV_DBF_TEXT(data, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800697 "Received data for unlinked connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return;
699 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800700 if (msg->length > conn->max_buffsize) {
701 iucv_message_reject(conn->path, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 privptr->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 IUCV_DBF_TEXT_(data, 2, "msglen %d > max_buffsize %d\n",
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800704 msg->length, conn->max_buffsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return;
706 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700707 conn->rx_buff->data = conn->rx_buff->head;
708 skb_reset_tail_pointer(conn->rx_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 conn->rx_buff->len = 0;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800710 rc = iucv_message_receive(conn->path, msg, 0, conn->rx_buff->data,
711 msg->length, NULL);
712 if (rc || msg->length < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 privptr->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_receive\n", rc);
715 return;
716 }
717 netiucv_unpack_skb(conn, conn->rx_buff);
718}
719
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800720static void conn_action_txdone(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800722 struct iucv_event *ev = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct iucv_connection *conn = ev->conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800724 struct iucv_message *msg = ev->data;
725 struct iucv_message txmsg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct netiucv_priv *privptr = NULL;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800727 u32 single_flag = msg->tag;
728 u32 txbytes = 0;
729 u32 txpackets = 0;
730 u32 stat_maxcq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct sk_buff *skb;
732 unsigned long saveflags;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800733 struct ll_header header;
734 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200736 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Ursula Braund239ae32013-12-16 09:44:51 +0100738 if (!conn || !conn->netdev) {
739 IUCV_DBF_TEXT(data, 2,
740 "Send confirmation for unlinked connection\n");
741 return;
742 }
743 privptr = netdev_priv(conn->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 conn->prof.tx_pending--;
745 if (single_flag) {
746 if ((skb = skb_dequeue(&conn->commit_queue))) {
747 atomic_dec(&skb->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (privptr) {
749 privptr->stats.tx_packets++;
750 privptr->stats.tx_bytes +=
751 (skb->len - NETIUCV_HDRLEN
Ursula Braun998221c2009-11-12 21:46:30 +0000752 - NETIUCV_HDRLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Ursula Braun998221c2009-11-12 21:46:30 +0000754 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700757 conn->tx_buff->data = conn->tx_buff->head;
758 skb_reset_tail_pointer(conn->tx_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 conn->tx_buff->len = 0;
760 spin_lock_irqsave(&conn->collect_lock, saveflags);
761 while ((skb = skb_dequeue(&conn->collect_queue))) {
762 header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
763 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
764 NETIUCV_HDRLEN);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300765 skb_copy_from_linear_data(skb,
766 skb_put(conn->tx_buff, skb->len),
767 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 txbytes += skb->len;
769 txpackets++;
770 stat_maxcq++;
771 atomic_dec(&skb->users);
772 dev_kfree_skb_any(skb);
773 }
774 if (conn->collect_len > conn->prof.maxmulti)
775 conn->prof.maxmulti = conn->collect_len;
776 conn->collect_len = 0;
777 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800778 if (conn->tx_buff->len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 fsm_newstate(fi, CONN_STATE_IDLE);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800780 return;
781 }
782
783 header.next = 0;
784 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
Aya Mahfouzee6edb92015-01-16 14:05:45 +0100785 conn->prof.send_stamp = jiffies;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800786 txmsg.class = 0;
787 txmsg.tag = 0;
788 rc = iucv_message_send(conn->path, &txmsg, 0, 0,
789 conn->tx_buff->data, conn->tx_buff->len);
790 conn->prof.doios_multi++;
791 conn->prof.txlen += conn->tx_buff->len;
792 conn->prof.tx_pending++;
793 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
794 conn->prof.tx_max_pending = conn->prof.tx_pending;
795 if (rc) {
796 conn->prof.tx_pending--;
797 fsm_newstate(fi, CONN_STATE_IDLE);
798 if (privptr)
799 privptr->stats.tx_errors += txpackets;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800800 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
801 } else {
802 if (privptr) {
803 privptr->stats.tx_packets += txpackets;
804 privptr->stats.tx_bytes += txbytes;
805 }
806 if (stat_maxcq > conn->prof.maxcqueue)
807 conn->prof.maxcqueue = stat_maxcq;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800811static void conn_action_connaccept(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800813 struct iucv_event *ev = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct iucv_connection *conn = ev->conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800815 struct iucv_path *path = ev->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800817 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200820 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800822 conn->path = path;
823 path->msglim = NETIUCV_QUEUELEN_DEFAULT;
824 path->flags = 0;
Ursula Braun08e33562011-12-19 22:56:34 +0000825 rc = iucv_path_accept(path, &netiucv_handler, conn->userdata , conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 IUCV_DBF_TEXT_(setup, 2, "rc %d from iucv_accept", rc);
828 return;
829 }
830 fsm_newstate(fi, CONN_STATE_IDLE);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800831 netdev->tx_queue_len = conn->path->msglim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
833}
834
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800835static void conn_action_connreject(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800837 struct iucv_event *ev = arg;
838 struct iucv_path *path = ev->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200840 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800841 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800844static void conn_action_connack(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800846 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800848 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200850 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 fsm_deltimer(&conn->timer);
852 fsm_newstate(fi, CONN_STATE_IDLE);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800853 netdev->tx_queue_len = conn->path->msglim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
855}
856
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800857static void conn_action_conntimsev(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800859 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200861 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 fsm_deltimer(&conn->timer);
Ursula Braun08e33562011-12-19 22:56:34 +0000863 iucv_path_sever(conn->path, conn->userdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 fsm_newstate(fi, CONN_STATE_STARTWAIT);
865}
866
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800867static void conn_action_connsever(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800869 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800871 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200873 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 fsm_deltimer(&conn->timer);
Ursula Braun08e33562011-12-19 22:56:34 +0000876 iucv_path_sever(conn->path, conn->userdata);
877 dev_info(privptr->dev, "The peer z/VM guest %s has closed the "
878 "connection\n", netiucv_printuser(conn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 IUCV_DBF_TEXT(data, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800880 "conn_action_connsever: Remote dropped connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 fsm_newstate(fi, CONN_STATE_STARTWAIT);
882 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
883}
884
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800885static void conn_action_start(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800887 struct iucv_connection *conn = arg;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100888 struct net_device *netdev = conn->netdev;
889 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int rc;
891
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200892 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800894 fsm_newstate(fi, CONN_STATE_STARTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800896 /*
897 * We must set the state before calling iucv_connect because the
898 * callback handler could be called at any point after the connection
899 * request is sent
900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 fsm_newstate(fi, CONN_STATE_SETUPWAIT);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800903 conn->path = iucv_path_alloc(NETIUCV_QUEUELEN_DEFAULT, 0, GFP_KERNEL);
Ursula Braun08e33562011-12-19 22:56:34 +0000904 IUCV_DBF_TEXT_(setup, 2, "%s: connecting to %s ...\n",
905 netdev->name, netiucv_printuser(conn));
906
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800907 rc = iucv_path_connect(conn->path, &netiucv_handler, conn->userid,
Ursula Braun08e33562011-12-19 22:56:34 +0000908 NULL, conn->userdata, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 switch (rc) {
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800910 case 0:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100911 netdev->tx_queue_len = conn->path->msglim;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800912 fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
913 CONN_EVENT_TIMER, conn);
914 return;
915 case 11:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100916 dev_warn(privptr->dev,
917 "The IUCV device failed to connect to z/VM guest %s\n",
Ursula Braun08e33562011-12-19 22:56:34 +0000918 netiucv_printname(conn->userid, 8));
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800919 fsm_newstate(fi, CONN_STATE_STARTWAIT);
920 break;
921 case 12:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100922 dev_warn(privptr->dev,
923 "The IUCV device failed to connect to the peer on z/VM"
Ursula Braun08e33562011-12-19 22:56:34 +0000924 " guest %s\n", netiucv_printname(conn->userid, 8));
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800925 fsm_newstate(fi, CONN_STATE_STARTWAIT);
926 break;
927 case 13:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100928 dev_err(privptr->dev,
929 "Connecting the IUCV device would exceed the maximum"
930 " number of IUCV connections\n");
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800931 fsm_newstate(fi, CONN_STATE_CONNERR);
932 break;
933 case 14:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100934 dev_err(privptr->dev,
935 "z/VM guest %s has too many IUCV connections"
936 " to connect with the IUCV device\n",
Ursula Braun08e33562011-12-19 22:56:34 +0000937 netiucv_printname(conn->userid, 8));
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800938 fsm_newstate(fi, CONN_STATE_CONNERR);
939 break;
940 case 15:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100941 dev_err(privptr->dev,
942 "The IUCV device cannot connect to a z/VM guest with no"
943 " IUCV authorization\n");
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800944 fsm_newstate(fi, CONN_STATE_CONNERR);
945 break;
946 default:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100947 dev_err(privptr->dev,
948 "Connecting the IUCV device failed with error %d\n",
949 rc);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800950 fsm_newstate(fi, CONN_STATE_CONNERR);
951 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953 IUCV_DBF_TEXT_(setup, 5, "iucv_connect rc is %d\n", rc);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800954 kfree(conn->path);
955 conn->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800958static void netiucv_purge_skb_queue(struct sk_buff_head *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct sk_buff *skb;
961
962 while ((skb = skb_dequeue(q))) {
963 atomic_dec(&skb->users);
964 dev_kfree_skb_any(skb);
965 }
966}
967
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800968static void conn_action_stop(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800970 struct iucv_event *ev = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 struct iucv_connection *conn = ev->conn;
972 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800973 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200975 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 fsm_deltimer(&conn->timer);
978 fsm_newstate(fi, CONN_STATE_STOPPED);
979 netiucv_purge_skb_queue(&conn->collect_queue);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800980 if (conn->path) {
981 IUCV_DBF_TEXT(trace, 5, "calling iucv_path_sever\n");
Ursula Braun08e33562011-12-19 22:56:34 +0000982 iucv_path_sever(conn->path, conn->userdata);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800983 kfree(conn->path);
984 conn->path = NULL;
985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 netiucv_purge_skb_queue(&conn->commit_queue);
987 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
988}
989
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800990static void conn_action_inval(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800992 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct net_device *netdev = conn->netdev;
994
Ursula Braunf082bca2008-07-14 09:59:30 +0200995 IUCV_DBF_TEXT_(data, 2, "%s('%s'): conn_action_inval called\n",
996 netdev->name, conn->userid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static const fsm_node conn_fsm[] = {
1000 { CONN_STATE_INVALID, CONN_EVENT_START, conn_action_inval },
1001 { CONN_STATE_STOPPED, CONN_EVENT_START, conn_action_start },
1002
1003 { CONN_STATE_STOPPED, CONN_EVENT_STOP, conn_action_stop },
1004 { CONN_STATE_STARTWAIT, CONN_EVENT_STOP, conn_action_stop },
1005 { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP, conn_action_stop },
1006 { CONN_STATE_IDLE, CONN_EVENT_STOP, conn_action_stop },
1007 { CONN_STATE_TX, CONN_EVENT_STOP, conn_action_stop },
1008 { CONN_STATE_REGERR, CONN_EVENT_STOP, conn_action_stop },
1009 { CONN_STATE_CONNERR, CONN_EVENT_STOP, conn_action_stop },
1010
1011 { CONN_STATE_STOPPED, CONN_EVENT_CONN_REQ, conn_action_connreject },
1012 { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
1013 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
1014 { CONN_STATE_IDLE, CONN_EVENT_CONN_REQ, conn_action_connreject },
1015 { CONN_STATE_TX, CONN_EVENT_CONN_REQ, conn_action_connreject },
1016
1017 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack },
1018 { CONN_STATE_SETUPWAIT, CONN_EVENT_TIMER, conn_action_conntimsev },
1019
1020 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever },
1021 { CONN_STATE_IDLE, CONN_EVENT_CONN_REJ, conn_action_connsever },
1022 { CONN_STATE_TX, CONN_EVENT_CONN_REJ, conn_action_connsever },
1023
1024 { CONN_STATE_IDLE, CONN_EVENT_RX, conn_action_rx },
1025 { CONN_STATE_TX, CONN_EVENT_RX, conn_action_rx },
1026
1027 { CONN_STATE_TX, CONN_EVENT_TXDONE, conn_action_txdone },
1028 { CONN_STATE_IDLE, CONN_EVENT_TXDONE, conn_action_txdone },
1029};
1030
1031static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
1032
Jeff Garzike82b0f22006-05-26 21:58:38 -04001033
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001034/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * Actions for interface - statemachine.
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038/**
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001039 * dev_action_start
1040 * @fi: An instance of an interface statemachine.
1041 * @event: The event, just happened.
1042 * @arg: Generic pointer, casted from struct net_device * upon call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 *
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001044 * Startup connection by sending CONN_EVENT_START to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001046static void dev_action_start(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001048 struct net_device *dev = arg;
1049 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001051 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 fsm_newstate(fi, DEV_STATE_STARTWAIT);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001054 fsm_event(privptr->conn->fsm, CONN_EVENT_START, privptr->conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057/**
1058 * Shutdown connection by sending CONN_EVENT_STOP to it.
1059 *
1060 * @param fi An instance of an interface statemachine.
1061 * @param event The event, just happened.
1062 * @param arg Generic pointer, casted from struct net_device * upon call.
1063 */
1064static void
1065dev_action_stop(fsm_instance *fi, int event, void *arg)
1066{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001067 struct net_device *dev = arg;
1068 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct iucv_event ev;
1070
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001071 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 ev.conn = privptr->conn;
1074
1075 fsm_newstate(fi, DEV_STATE_STOPWAIT);
1076 fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev);
1077}
1078
1079/**
1080 * Called from connection statemachine
1081 * when a connection is up and running.
1082 *
1083 * @param fi An instance of an interface statemachine.
1084 * @param event The event, just happened.
1085 * @param arg Generic pointer, casted from struct net_device * upon call.
1086 */
1087static void
1088dev_action_connup(fsm_instance *fi, int event, void *arg)
1089{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001090 struct net_device *dev = arg;
1091 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001093 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 switch (fsm_getstate(fi)) {
1096 case DEV_STATE_STARTWAIT:
1097 fsm_newstate(fi, DEV_STATE_RUNNING);
Ursula Braun8f7c5022008-12-25 13:39:47 +01001098 dev_info(privptr->dev,
1099 "The IUCV device has been connected"
Ursula Braun08e33562011-12-19 22:56:34 +00001100 " successfully to %s\n",
1101 netiucv_printuser(privptr->conn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 IUCV_DBF_TEXT(setup, 3,
1103 "connection is up and running\n");
1104 break;
1105 case DEV_STATE_STOPWAIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 IUCV_DBF_TEXT(data, 2,
1107 "dev_action_connup: in DEV_STATE_STOPWAIT\n");
1108 break;
1109 }
1110}
1111
1112/**
1113 * Called from connection statemachine
1114 * when a connection has been shutdown.
1115 *
1116 * @param fi An instance of an interface statemachine.
1117 * @param event The event, just happened.
1118 * @param arg Generic pointer, casted from struct net_device * upon call.
1119 */
1120static void
1121dev_action_conndown(fsm_instance *fi, int event, void *arg)
1122{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001123 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 switch (fsm_getstate(fi)) {
1126 case DEV_STATE_RUNNING:
1127 fsm_newstate(fi, DEV_STATE_STARTWAIT);
1128 break;
1129 case DEV_STATE_STOPWAIT:
1130 fsm_newstate(fi, DEV_STATE_STOPPED);
1131 IUCV_DBF_TEXT(setup, 3, "connection is down\n");
1132 break;
1133 }
1134}
1135
1136static const fsm_node dev_fsm[] = {
1137 { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start },
1138
1139 { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start },
1140 { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
1141
1142 { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop },
1143 { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup },
1144
1145 { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
1146 { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown },
Ursula Braun21b26f2f2008-02-08 13:09:03 +01001147 { DEV_STATE_RUNNING, DEV_EVENT_CONUP, netiucv_action_nop },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148};
1149
1150static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
1151
1152/**
1153 * Transmit a packet.
1154 * This is a helper function for netiucv_tx().
1155 *
1156 * @param conn Connection to be used for sending.
1157 * @param skb Pointer to struct sk_buff of packet to send.
1158 * The linklevel header has already been set up
1159 * by netiucv_tx().
1160 *
1161 * @return 0 on success, -ERRNO on failure. (Never fails.)
1162 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001163static int netiucv_transmit_skb(struct iucv_connection *conn,
1164 struct sk_buff *skb)
1165{
1166 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 unsigned long saveflags;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001168 struct ll_header header;
1169 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
1172 int l = skb->len + NETIUCV_HDRLEN;
1173
1174 spin_lock_irqsave(&conn->collect_lock, saveflags);
1175 if (conn->collect_len + l >
1176 (conn->max_buffsize - NETIUCV_HDRLEN)) {
1177 rc = -EBUSY;
1178 IUCV_DBF_TEXT(data, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001179 "EBUSY from netiucv_transmit_skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 } else {
1181 atomic_inc(&skb->users);
1182 skb_queue_tail(&conn->collect_queue, skb);
1183 conn->collect_len += l;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001184 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
1187 } else {
1188 struct sk_buff *nskb = skb;
1189 /**
1190 * Copy the skb to a new allocated skb in lowmem only if the
1191 * data is located above 2G in memory or tailroom is < 2.
1192 */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001193 unsigned long hi = ((unsigned long)(skb_tail_pointer(skb) +
1194 NETIUCV_HDRLEN)) >> 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int copied = 0;
1196 if (hi || (skb_tailroom(skb) < 2)) {
1197 nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
1198 NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
1199 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 IUCV_DBF_TEXT(data, 2, "alloc_skb failed\n");
1201 rc = -ENOMEM;
1202 return rc;
1203 } else {
1204 skb_reserve(nskb, NETIUCV_HDRLEN);
1205 memcpy(skb_put(nskb, skb->len),
1206 skb->data, skb->len);
1207 }
1208 copied = 1;
1209 }
1210 /**
1211 * skb now is below 2G and has enough room. Add headers.
1212 */
1213 header.next = nskb->len + NETIUCV_HDRLEN;
1214 memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1215 header.next = 0;
1216 memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1217
1218 fsm_newstate(conn->fsm, CONN_STATE_TX);
Aya Mahfouzee6edb92015-01-16 14:05:45 +01001219 conn->prof.send_stamp = jiffies;
Jeff Garzike82b0f22006-05-26 21:58:38 -04001220
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001221 msg.tag = 1;
1222 msg.class = 0;
1223 rc = iucv_message_send(conn->path, &msg, 0, 0,
1224 nskb->data, nskb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 conn->prof.doios_single++;
1226 conn->prof.txlen += skb->len;
1227 conn->prof.tx_pending++;
1228 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
1229 conn->prof.tx_max_pending = conn->prof.tx_pending;
1230 if (rc) {
1231 struct netiucv_priv *privptr;
1232 fsm_newstate(conn->fsm, CONN_STATE_IDLE);
1233 conn->prof.tx_pending--;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001234 privptr = netdev_priv(conn->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (privptr)
1236 privptr->stats.tx_errors++;
1237 if (copied)
1238 dev_kfree_skb(nskb);
1239 else {
1240 /**
1241 * Remove our headers. They get added
1242 * again on retransmit.
1243 */
1244 skb_pull(skb, NETIUCV_HDRLEN);
1245 skb_trim(skb, skb->len - NETIUCV_HDRLEN);
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
1248 } else {
1249 if (copied)
1250 dev_kfree_skb(skb);
1251 atomic_inc(&nskb->users);
1252 skb_queue_tail(&conn->commit_queue, nskb);
1253 }
1254 }
1255
1256 return rc;
1257}
Jeff Garzike82b0f22006-05-26 21:58:38 -04001258
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001259/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * Interface API for upper network layers
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263/**
1264 * Open an interface.
1265 * Called from generic network layer when ifconfig up is run.
1266 *
1267 * @param dev Pointer to interface struct.
1268 *
1269 * @return 0 on success, -ERRNO on failure. (Never fails.)
1270 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001271static int netiucv_open(struct net_device *dev)
1272{
1273 struct netiucv_priv *priv = netdev_priv(dev);
1274
1275 fsm_event(priv->fsm, DEV_EVENT_START, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
1277}
1278
1279/**
1280 * Close an interface.
1281 * Called from generic network layer when ifconfig down is run.
1282 *
1283 * @param dev Pointer to interface struct.
1284 *
1285 * @return 0 on success, -ERRNO on failure. (Never fails.)
1286 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001287static int netiucv_close(struct net_device *dev)
1288{
1289 struct netiucv_priv *priv = netdev_priv(dev);
1290
1291 fsm_event(priv->fsm, DEV_EVENT_STOP, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return 0;
1293}
1294
Ursula Braun1175b252009-06-16 10:30:43 +02001295static int netiucv_pm_prepare(struct device *dev)
1296{
1297 IUCV_DBF_TEXT(trace, 3, __func__);
1298 return 0;
1299}
1300
1301static void netiucv_pm_complete(struct device *dev)
1302{
1303 IUCV_DBF_TEXT(trace, 3, __func__);
1304 return;
1305}
1306
1307/**
1308 * netiucv_pm_freeze() - Freeze PM callback
1309 * @dev: netiucv device
1310 *
1311 * close open netiucv interfaces
1312 */
1313static int netiucv_pm_freeze(struct device *dev)
1314{
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001315 struct netiucv_priv *priv = dev_get_drvdata(dev);
Ursula Braun1175b252009-06-16 10:30:43 +02001316 struct net_device *ndev = NULL;
1317 int rc = 0;
1318
1319 IUCV_DBF_TEXT(trace, 3, __func__);
1320 if (priv && priv->conn)
1321 ndev = priv->conn->netdev;
1322 if (!ndev)
1323 goto out;
1324 netif_device_detach(ndev);
1325 priv->pm_state = fsm_getstate(priv->fsm);
1326 rc = netiucv_close(ndev);
1327out:
1328 return rc;
1329}
1330
1331/**
1332 * netiucv_pm_restore_thaw() - Thaw and restore PM callback
1333 * @dev: netiucv device
1334 *
1335 * re-open netiucv interfaces closed during freeze
1336 */
1337static int netiucv_pm_restore_thaw(struct device *dev)
1338{
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001339 struct netiucv_priv *priv = dev_get_drvdata(dev);
Ursula Braun1175b252009-06-16 10:30:43 +02001340 struct net_device *ndev = NULL;
1341 int rc = 0;
1342
1343 IUCV_DBF_TEXT(trace, 3, __func__);
1344 if (priv && priv->conn)
1345 ndev = priv->conn->netdev;
1346 if (!ndev)
1347 goto out;
1348 switch (priv->pm_state) {
1349 case DEV_STATE_RUNNING:
1350 case DEV_STATE_STARTWAIT:
1351 rc = netiucv_open(ndev);
1352 break;
1353 default:
1354 break;
1355 }
1356 netif_device_attach(ndev);
1357out:
1358 return rc;
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/**
1362 * Start transmission of a packet.
1363 * Called from generic network device layer.
1364 *
1365 * @param skb Pointer to buffer containing the packet.
1366 * @param dev Pointer to interface struct.
1367 *
1368 * @return 0 if packet consumed, !0 if packet rejected.
1369 * Note: If we return !0, then the packet is free'd by
1370 * the generic network layer.
1371 */
1372static int netiucv_tx(struct sk_buff *skb, struct net_device *dev)
1373{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001374 struct netiucv_priv *privptr = netdev_priv(dev);
1375 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001377 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /**
1379 * Some sanity checks ...
1380 */
1381 if (skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 IUCV_DBF_TEXT(data, 2, "netiucv_tx: skb is NULL\n");
1383 privptr->stats.tx_dropped++;
Patrick McHardyec634fe2009-07-05 19:23:38 -07001384 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386 if (skb_headroom(skb) < NETIUCV_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 IUCV_DBF_TEXT(data, 2,
1388 "netiucv_tx: skb_headroom < NETIUCV_HDRLEN\n");
1389 dev_kfree_skb(skb);
1390 privptr->stats.tx_dropped++;
Patrick McHardyec634fe2009-07-05 19:23:38 -07001391 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
1394 /**
1395 * If connection is not running, try to restart it
Jeff Garzike82b0f22006-05-26 21:58:38 -04001396 * and throw away packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
1398 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 dev_kfree_skb(skb);
1400 privptr->stats.tx_dropped++;
1401 privptr->stats.tx_errors++;
1402 privptr->stats.tx_carrier_errors++;
Patrick McHardyec634fe2009-07-05 19:23:38 -07001403 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405
1406 if (netiucv_test_and_set_busy(dev)) {
1407 IUCV_DBF_TEXT(data, 2, "EBUSY from netiucv_tx\n");
Ursula Braun4e584d62009-03-24 03:27:45 +00001408 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
Florian Westphal860e9532016-05-03 16:33:13 +02001410 netif_trans_update(dev);
Patrick McHardy5b548142009-06-12 06:22:29 +00001411 rc = netiucv_transmit_skb(privptr->conn, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 netiucv_clear_busy(dev);
Patrick McHardy5b548142009-06-12 06:22:29 +00001413 return rc ? NETDEV_TX_BUSY : NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/**
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001417 * netiucv_stats
1418 * @dev: Pointer to interface struct.
1419 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * Returns interface statistics of a device.
1421 *
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001422 * Returns pointer to stats struct of this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001424static struct net_device_stats *netiucv_stats (struct net_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001426 struct netiucv_priv *priv = netdev_priv(dev);
1427
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001428 IUCV_DBF_TEXT(trace, 5, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001429 return &priv->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432/**
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001433 * netiucv_change_mtu
1434 * @dev: Pointer to interface struct.
1435 * @new_mtu: The new MTU to use for this interface.
1436 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 * Sets MTU of an interface.
1438 *
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001439 * Returns 0 on success, -EINVAL if MTU is out of valid range.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * (valid range is 576 .. NETIUCV_MTU_MAX).
1441 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001442static int netiucv_change_mtu(struct net_device * dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001444 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001445 if (new_mtu < 576 || new_mtu > NETIUCV_MTU_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 IUCV_DBF_TEXT(setup, 2, "given MTU out of valid range\n");
1447 return -EINVAL;
1448 }
1449 dev->mtu = new_mtu;
1450 return 0;
1451}
1452
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001453/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 * attributes in sysfs
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001457static ssize_t user_show(struct device *dev, struct device_attribute *attr,
1458 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001460 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001462 IUCV_DBF_TEXT(trace, 5, __func__);
Ursula Braun08e33562011-12-19 22:56:34 +00001463 return sprintf(buf, "%s\n", netiucv_printuser(priv->conn));
1464}
1465
1466static int netiucv_check_user(const char *buf, size_t count, char *username,
1467 char *userdata)
1468{
1469 const char *p;
1470 int i;
1471
1472 p = strchr(buf, '.');
1473 if ((p && ((count > 26) ||
1474 ((p - buf) > 8) ||
1475 (buf + count - p > 18))) ||
1476 (!p && (count > 9))) {
1477 IUCV_DBF_TEXT(setup, 2, "conn_write: too long\n");
1478 return -EINVAL;
1479 }
1480
1481 for (i = 0, p = buf; i < 8 && *p && *p != '.'; i++, p++) {
1482 if (isalnum(*p) || *p == '$') {
1483 username[i] = toupper(*p);
1484 continue;
1485 }
1486 if (*p == '\n')
1487 /* trailing lf, grr */
1488 break;
1489 IUCV_DBF_TEXT_(setup, 2,
1490 "conn_write: invalid character %02x\n", *p);
1491 return -EINVAL;
1492 }
1493 while (i < 8)
1494 username[i++] = ' ';
1495 username[8] = '\0';
1496
1497 if (*p == '.') {
1498 p++;
1499 for (i = 0; i < 16 && *p; i++, p++) {
1500 if (*p == '\n')
1501 break;
1502 userdata[i] = toupper(*p);
1503 }
1504 while (i > 0 && i < 16)
1505 userdata[i++] = ' ';
1506 } else
1507 memcpy(userdata, iucvMagic_ascii, 16);
1508 userdata[16] = '\0';
1509 ASCEBC(userdata, 16);
1510
1511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001514static ssize_t user_write(struct device *dev, struct device_attribute *attr,
1515 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001517 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 struct net_device *ndev = priv->conn->netdev;
Ursula Braun08e33562011-12-19 22:56:34 +00001519 char username[9];
1520 char userdata[17];
1521 int rc;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001522 struct iucv_connection *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001524 IUCV_DBF_TEXT(trace, 3, __func__);
Ursula Braun08e33562011-12-19 22:56:34 +00001525 rc = netiucv_check_user(buf, count, username, userdata);
1526 if (rc)
1527 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001529 if (memcmp(username, priv->conn->userid, 9) &&
1530 (ndev->flags & (IFF_UP | IFF_RUNNING))) {
1531 /* username changed while the interface is active. */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001532 IUCV_DBF_TEXT(setup, 2, "user_write: device active\n");
Ursula Braunf082bca2008-07-14 09:59:30 +02001533 return -EPERM;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001534 }
1535 read_lock_bh(&iucv_connection_rwlock);
1536 list_for_each_entry(cp, &iucv_connection_list, list) {
Ursula Braun08e33562011-12-19 22:56:34 +00001537 if (!strncmp(username, cp->userid, 9) &&
1538 !strncmp(userdata, cp->userdata, 17) && cp->netdev != ndev) {
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001539 read_unlock_bh(&iucv_connection_rwlock);
Ursula Braun08e33562011-12-19 22:56:34 +00001540 IUCV_DBF_TEXT_(setup, 2, "user_write: Connection to %s "
1541 "already exists\n", netiucv_printuser(cp));
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001542 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 }
1544 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001545 read_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 memcpy(priv->conn->userid, username, 9);
Ursula Braun08e33562011-12-19 22:56:34 +00001547 memcpy(priv->conn->userdata, userdata, 17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
1551static DEVICE_ATTR(user, 0644, user_show, user_write);
1552
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001553static ssize_t buffer_show (struct device *dev, struct device_attribute *attr,
1554 char *buf)
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001555{
1556 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001558 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return sprintf(buf, "%d\n", priv->conn->max_buffsize);
1560}
1561
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001562static ssize_t buffer_write (struct device *dev, struct device_attribute *attr,
1563 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001565 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 struct net_device *ndev = priv->conn->netdev;
1567 char *e;
1568 int bs1;
1569
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001570 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (count >= 39)
1572 return -EINVAL;
1573
1574 bs1 = simple_strtoul(buf, &e, 0);
1575
1576 if (e && (!isspace(*e))) {
Ursula Braun08e33562011-12-19 22:56:34 +00001577 IUCV_DBF_TEXT_(setup, 2, "buffer_write: invalid char %02x\n",
1578 *e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return -EINVAL;
1580 }
1581 if (bs1 > NETIUCV_BUFSIZE_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 IUCV_DBF_TEXT_(setup, 2,
1583 "buffer_write: buffer size %d too large\n",
1584 bs1);
1585 return -EINVAL;
1586 }
1587 if ((ndev->flags & IFF_RUNNING) &&
1588 (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 IUCV_DBF_TEXT_(setup, 2,
1590 "buffer_write: buffer size %d too small\n",
1591 bs1);
1592 return -EINVAL;
1593 }
1594 if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 IUCV_DBF_TEXT_(setup, 2,
1596 "buffer_write: buffer size %d too small\n",
1597 bs1);
1598 return -EINVAL;
1599 }
1600
1601 priv->conn->max_buffsize = bs1;
1602 if (!(ndev->flags & IFF_RUNNING))
1603 ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
1604
1605 return count;
1606
1607}
1608
1609static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
1610
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001611static ssize_t dev_fsm_show (struct device *dev, struct device_attribute *attr,
1612 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001614 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001616 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm));
1618}
1619
1620static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
1621
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001622static ssize_t conn_fsm_show (struct device *dev,
1623 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001625 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001627 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm));
1629}
1630
1631static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
1632
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001633static ssize_t maxmulti_show (struct device *dev,
1634 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001636 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001638 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
1640}
1641
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001642static ssize_t maxmulti_write (struct device *dev,
1643 struct device_attribute *attr,
1644 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001646 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001648 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 priv->conn->prof.maxmulti = 0;
1650 return count;
1651}
1652
1653static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
1654
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001655static ssize_t maxcq_show (struct device *dev, struct device_attribute *attr,
1656 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001658 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001660 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
1662}
1663
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001664static ssize_t maxcq_write (struct device *dev, struct device_attribute *attr,
1665 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001667 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001668
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001669 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 priv->conn->prof.maxcqueue = 0;
1671 return count;
1672}
1673
1674static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
1675
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001676static ssize_t sdoio_show (struct device *dev, struct device_attribute *attr,
1677 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001679 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001681 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
1683}
1684
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001685static ssize_t sdoio_write (struct device *dev, struct device_attribute *attr,
1686 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001688 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001689
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001690 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 priv->conn->prof.doios_single = 0;
1692 return count;
1693}
1694
1695static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
1696
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001697static ssize_t mdoio_show (struct device *dev, struct device_attribute *attr,
1698 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001700 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001702 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
1704}
1705
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001706static ssize_t mdoio_write (struct device *dev, struct device_attribute *attr,
1707 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001709 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001710
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001711 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 priv->conn->prof.doios_multi = 0;
1713 return count;
1714}
1715
1716static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
1717
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001718static ssize_t txlen_show (struct device *dev, struct device_attribute *attr,
1719 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001721 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001723 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
1725}
1726
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001727static ssize_t txlen_write (struct device *dev, struct device_attribute *attr,
1728 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001730 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001731
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001732 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 priv->conn->prof.txlen = 0;
1734 return count;
1735}
1736
1737static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
1738
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001739static ssize_t txtime_show (struct device *dev, struct device_attribute *attr,
1740 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001742 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001744 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
1746}
1747
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001748static ssize_t txtime_write (struct device *dev, struct device_attribute *attr,
1749 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001751 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001752
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001753 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 priv->conn->prof.tx_time = 0;
1755 return count;
1756}
1757
1758static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
1759
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001760static ssize_t txpend_show (struct device *dev, struct device_attribute *attr,
1761 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001763 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001765 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending);
1767}
1768
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001769static ssize_t txpend_write (struct device *dev, struct device_attribute *attr,
1770 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001772 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001774 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 priv->conn->prof.tx_pending = 0;
1776 return count;
1777}
1778
1779static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write);
1780
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001781static ssize_t txmpnd_show (struct device *dev, struct device_attribute *attr,
1782 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001784 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001786 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending);
1788}
1789
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001790static ssize_t txmpnd_write (struct device *dev, struct device_attribute *attr,
1791 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001793 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001795 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 priv->conn->prof.tx_max_pending = 0;
1797 return count;
1798}
1799
1800static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write);
1801
1802static struct attribute *netiucv_attrs[] = {
1803 &dev_attr_buffer.attr,
1804 &dev_attr_user.attr,
1805 NULL,
1806};
1807
1808static struct attribute_group netiucv_attr_group = {
1809 .attrs = netiucv_attrs,
1810};
1811
1812static struct attribute *netiucv_stat_attrs[] = {
1813 &dev_attr_device_fsm_state.attr,
1814 &dev_attr_connection_fsm_state.attr,
1815 &dev_attr_max_tx_buffer_used.attr,
1816 &dev_attr_max_chained_skbs.attr,
1817 &dev_attr_tx_single_write_ops.attr,
1818 &dev_attr_tx_multi_write_ops.attr,
1819 &dev_attr_netto_bytes.attr,
1820 &dev_attr_max_tx_io_time.attr,
1821 &dev_attr_tx_pending.attr,
1822 &dev_attr_tx_max_pending.attr,
1823 NULL,
1824};
1825
1826static struct attribute_group netiucv_stat_attr_group = {
1827 .name = "stats",
1828 .attrs = netiucv_stat_attrs,
1829};
1830
frank.blaschka@de.ibm.com0b945292012-07-24 22:34:28 +00001831static const struct attribute_group *netiucv_attr_groups[] = {
1832 &netiucv_stat_attr_group,
1833 &netiucv_attr_group,
1834 NULL,
1835};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001837static int netiucv_register_device(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001839 struct netiucv_priv *priv = netdev_priv(ndev);
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001840 struct device *dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 int ret;
1842
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001843 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 if (dev) {
Cornelia Huck1bf5b282008-10-10 21:33:10 +02001846 dev_set_name(dev, "net%s", ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 dev->bus = &iucv_bus;
1848 dev->parent = iucv_root;
frank.blaschka@de.ibm.com0b945292012-07-24 22:34:28 +00001849 dev->groups = netiucv_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /*
1851 * The release function could be called after the
1852 * module has been unloaded. It's _only_ task is to
1853 * free the struct. Therefore, we specify kfree()
1854 * directly here. (Probably a little bit obfuscating
1855 * but legitime ...).
1856 */
1857 dev->release = (void (*)(struct device *))kfree;
1858 dev->driver = &netiucv_driver;
1859 } else
1860 return -ENOMEM;
1861
1862 ret = device_register(dev);
Sebastian Ottc6304932009-09-11 10:28:38 +02001863 if (ret) {
1864 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return ret;
Sebastian Ottc6304932009-09-11 10:28:38 +02001866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 priv->dev = dev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001868 dev_set_drvdata(dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001872static void netiucv_unregister_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001874 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 device_unregister(dev);
1876}
1877
1878/**
1879 * Allocate and initialize a new connection structure.
1880 * Add it to the list of netiucv connections;
1881 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001882static struct iucv_connection *netiucv_new_connection(struct net_device *dev,
Ursula Braun08e33562011-12-19 22:56:34 +00001883 char *username,
1884 char *userdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001886 struct iucv_connection *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001888 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1889 if (!conn)
1890 goto out;
1891 skb_queue_head_init(&conn->collect_queue);
1892 skb_queue_head_init(&conn->commit_queue);
1893 spin_lock_init(&conn->collect_lock);
1894 conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
1895 conn->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001897 conn->rx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1898 if (!conn->rx_buff)
1899 goto out_conn;
1900 conn->tx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1901 if (!conn->tx_buff)
1902 goto out_rx;
1903 conn->fsm = init_fsm("netiucvconn", conn_state_names,
1904 conn_event_names, NR_CONN_STATES,
1905 NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
1906 GFP_KERNEL);
1907 if (!conn->fsm)
1908 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001910 fsm_settimer(conn->fsm, &conn->timer);
1911 fsm_newstate(conn->fsm, CONN_STATE_INVALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Ursula Braun08e33562011-12-19 22:56:34 +00001913 if (userdata)
1914 memcpy(conn->userdata, userdata, 17);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001915 if (username) {
1916 memcpy(conn->userid, username, 9);
1917 fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001919
1920 write_lock_bh(&iucv_connection_rwlock);
1921 list_add_tail(&conn->list, &iucv_connection_list);
1922 write_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001924
1925out_tx:
1926 kfree_skb(conn->tx_buff);
1927out_rx:
1928 kfree_skb(conn->rx_buff);
1929out_conn:
1930 kfree(conn);
1931out:
1932 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935/**
1936 * Release a connection structure and remove it from the
1937 * list of netiucv connections.
1938 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001939static void netiucv_remove_connection(struct iucv_connection *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Ursula Braun08e33562011-12-19 22:56:34 +00001941
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001942 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001943 write_lock_bh(&iucv_connection_rwlock);
1944 list_del_init(&conn->list);
1945 write_unlock_bh(&iucv_connection_rwlock);
Ursula Braun0be4ace2007-05-02 15:18:44 +02001946 fsm_deltimer(&conn->timer);
1947 netiucv_purge_skb_queue(&conn->collect_queue);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001948 if (conn->path) {
Ursula Braun08e33562011-12-19 22:56:34 +00001949 iucv_path_sever(conn->path, conn->userdata);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001950 kfree(conn->path);
1951 conn->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Ursula Braun0be4ace2007-05-02 15:18:44 +02001953 netiucv_purge_skb_queue(&conn->commit_queue);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001954 kfree_fsm(conn->fsm);
1955 kfree_skb(conn->rx_buff);
1956 kfree_skb(conn->tx_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
1959/**
1960 * Release everything of a net device.
1961 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001962static void netiucv_free_netdevice(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001964 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001966 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 if (!dev)
1969 return;
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (privptr) {
1972 if (privptr->conn)
1973 netiucv_remove_connection(privptr->conn);
1974 if (privptr->fsm)
1975 kfree_fsm(privptr->fsm);
1976 privptr->conn = NULL; privptr->fsm = NULL;
1977 /* privptr gets freed by free_netdev() */
1978 }
1979 free_netdev(dev);
1980}
1981
1982/**
1983 * Initialize a net device. (Called from kernel in alloc_netdev())
1984 */
Frank Blaschka4edd73b2009-01-09 03:43:58 +00001985static const struct net_device_ops netiucv_netdev_ops = {
1986 .ndo_open = netiucv_open,
1987 .ndo_stop = netiucv_close,
1988 .ndo_get_stats = netiucv_stats,
1989 .ndo_start_xmit = netiucv_tx,
1990 .ndo_change_mtu = netiucv_change_mtu,
1991};
1992
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001993static void netiucv_setup_netdevice(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 dev->mtu = NETIUCV_MTU_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 dev->destructor = netiucv_free_netdevice;
1997 dev->hard_header_len = NETIUCV_HDRLEN;
1998 dev->addr_len = 0;
1999 dev->type = ARPHRD_SLIP;
2000 dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT;
2001 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
Frank Blaschka4edd73b2009-01-09 03:43:58 +00002002 dev->netdev_ops = &netiucv_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
2005/**
2006 * Allocate and initialize everything of a net device.
2007 */
Ursula Braun08e33562011-12-19 22:56:34 +00002008static struct net_device *netiucv_init_netdevice(char *username, char *userdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct netiucv_priv *privptr;
2011 struct net_device *dev;
2012
2013 dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d",
Tom Gundersenc835a672014-07-14 16:37:24 +02002014 NET_NAME_UNKNOWN, netiucv_setup_netdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!dev)
2016 return NULL;
Benjamin Poirieraaf95222013-06-13 09:09:47 -04002017 rtnl_lock();
Ursula Braun1d503562011-11-15 02:31:14 +00002018 if (dev_alloc_name(dev, dev->name) < 0)
2019 goto out_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002021 privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 privptr->fsm = init_fsm("netiucvdev", dev_state_names,
2023 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
2024 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002025 if (!privptr->fsm)
2026 goto out_netdev;
2027
Ursula Braun08e33562011-12-19 22:56:34 +00002028 privptr->conn = netiucv_new_connection(dev, username, userdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (!privptr->conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_new_connection\n");
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002031 goto out_fsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return dev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002035
2036out_fsm:
2037 kfree_fsm(privptr->fsm);
2038out_netdev:
Benjamin Poirieraaf95222013-06-13 09:09:47 -04002039 rtnl_unlock();
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002040 free_netdev(dev);
2041 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002044static ssize_t conn_write(struct device_driver *drv,
2045 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Frank Pavlic16a83b32006-09-15 16:25:19 +02002047 char username[9];
Ursula Braun08e33562011-12-19 22:56:34 +00002048 char userdata[17];
2049 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 struct net_device *dev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002051 struct netiucv_priv *priv;
2052 struct iucv_connection *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002054 IUCV_DBF_TEXT(trace, 3, __func__);
Ursula Braun08e33562011-12-19 22:56:34 +00002055 rc = netiucv_check_user(buf, count, username, userdata);
2056 if (rc)
2057 return rc;
Frank Pavlic16a83b32006-09-15 16:25:19 +02002058
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002059 read_lock_bh(&iucv_connection_rwlock);
2060 list_for_each_entry(cp, &iucv_connection_list, list) {
Ursula Braun08e33562011-12-19 22:56:34 +00002061 if (!strncmp(username, cp->userid, 9) &&
2062 !strncmp(userdata, cp->userdata, 17)) {
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002063 read_unlock_bh(&iucv_connection_rwlock);
Ursula Braun08e33562011-12-19 22:56:34 +00002064 IUCV_DBF_TEXT_(setup, 2, "conn_write: Connection to %s "
2065 "already exists\n", netiucv_printuser(cp));
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002066 return -EEXIST;
2067 }
Frank Pavlic16a83b32006-09-15 16:25:19 +02002068 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002069 read_unlock_bh(&iucv_connection_rwlock);
2070
Ursula Braun08e33562011-12-19 22:56:34 +00002071 dev = netiucv_init_netdevice(username, userdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_init_netdevice\n");
2074 return -ENODEV;
2075 }
2076
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002077 rc = netiucv_register_device(dev);
2078 if (rc) {
Benjamin Poirieraaf95222013-06-13 09:09:47 -04002079 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 IUCV_DBF_TEXT_(setup, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002081 "ret %d from netiucv_register_device\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto out_free_ndev;
2083 }
2084
2085 /* sysfs magic */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002086 priv = netdev_priv(dev);
2087 SET_NETDEV_DEV(dev, priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Benjamin Poirieraaf95222013-06-13 09:09:47 -04002089 rc = register_netdevice(dev);
2090 rtnl_unlock();
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002091 if (rc)
2092 goto out_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Ursula Braun08e33562011-12-19 22:56:34 +00002094 dev_info(priv->dev, "The IUCV interface to %s has been established "
2095 "successfully\n",
2096 netiucv_printuser(priv->conn));
Jeff Garzike82b0f22006-05-26 21:58:38 -04002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 return count;
2099
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002100out_unreg:
2101 netiucv_unregister_device(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102out_free_ndev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 netiucv_free_netdevice(dev);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002104 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Heiko Carstens2b67fc42007-02-05 21:16:47 +01002107static DRIVER_ATTR(connection, 0200, NULL, conn_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002109static ssize_t remove_write (struct device_driver *drv,
2110 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002112 struct iucv_connection *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 struct net_device *ndev;
2114 struct netiucv_priv *priv;
2115 struct device *dev;
2116 char name[IFNAMSIZ];
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002117 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 int i;
2119
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002120 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 if (count >= IFNAMSIZ)
Joe Perchesa419aef2009-08-18 11:18:35 -07002123 count = IFNAMSIZ - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002125 for (i = 0, p = buf; i < count && *p; i++, p++) {
2126 if (*p == '\n' || *p == ' ')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 /* trailing lf, grr */
2128 break;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002129 name[i] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131 name[i] = '\0';
2132
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002133 read_lock_bh(&iucv_connection_rwlock);
2134 list_for_each_entry(cp, &iucv_connection_list, list) {
2135 ndev = cp->netdev;
2136 priv = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 dev = priv->dev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002138 if (strncmp(name, ndev->name, count))
2139 continue;
2140 read_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01002142 dev_warn(dev, "The IUCV device is connected"
2143 " to %s and cannot be removed\n",
2144 priv->conn->userid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 IUCV_DBF_TEXT(data, 2, "remove_write: still active\n");
Ursula Braunf082bca2008-07-14 09:59:30 +02002146 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 unregister_netdev(ndev);
2149 netiucv_unregister_device(dev);
2150 return count;
2151 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002152 read_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 IUCV_DBF_TEXT(data, 2, "remove_write: unknown device\n");
2154 return -EINVAL;
2155}
2156
Heiko Carstens2b67fc42007-02-05 21:16:47 +01002157static DRIVER_ATTR(remove, 0200, NULL, remove_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002159static struct attribute * netiucv_drv_attrs[] = {
2160 &driver_attr_connection.attr,
2161 &driver_attr_remove.attr,
2162 NULL,
2163};
2164
2165static struct attribute_group netiucv_drv_attr_group = {
2166 .attrs = netiucv_drv_attrs,
2167};
2168
David Brownella4dbd672009-06-24 10:06:31 -07002169static const struct attribute_group *netiucv_drv_attr_groups[] = {
Cornelia Huck5b88feb2007-12-05 12:50:28 +01002170 &netiucv_drv_attr_group,
2171 NULL,
2172};
2173
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002174static void netiucv_banner(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Ursula Braun8f7c5022008-12-25 13:39:47 +01002176 pr_info("driver initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002179static void __exit netiucv_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002181 struct iucv_connection *cp;
2182 struct net_device *ndev;
2183 struct netiucv_priv *priv;
2184 struct device *dev;
2185
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002186 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002187 while (!list_empty(&iucv_connection_list)) {
2188 cp = list_entry(iucv_connection_list.next,
2189 struct iucv_connection, list);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002190 ndev = cp->netdev;
2191 priv = netdev_priv(ndev);
2192 dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 unregister_netdev(ndev);
2195 netiucv_unregister_device(dev);
2196 }
2197
Ursula Braun1175b252009-06-16 10:30:43 +02002198 device_unregister(netiucv_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 driver_unregister(&netiucv_driver);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002200 iucv_unregister(&netiucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 iucv_unregister_dbf_views();
2202
Ursula Braun8f7c5022008-12-25 13:39:47 +01002203 pr_info("driver unloaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return;
2205}
2206
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002207static int __init netiucv_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002209 int rc;
Jeff Garzike82b0f22006-05-26 21:58:38 -04002210
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002211 rc = iucv_register_dbf_views();
2212 if (rc)
2213 goto out;
2214 rc = iucv_register(&netiucv_handler, 1);
2215 if (rc)
2216 goto out_dbf;
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002217 IUCV_DBF_TEXT(trace, 3, __func__);
Cornelia Huck0a0a8312008-04-24 10:15:28 +02002218 netiucv_driver.groups = netiucv_drv_attr_groups;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002219 rc = driver_register(&netiucv_driver);
2220 if (rc) {
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002221 IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_register\n", rc);
2222 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
Ursula Braun1175b252009-06-16 10:30:43 +02002224 /* establish dummy device */
2225 netiucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2226 if (!netiucv_dev) {
2227 rc = -ENOMEM;
2228 goto out_driver;
2229 }
2230 dev_set_name(netiucv_dev, "netiucv");
2231 netiucv_dev->bus = &iucv_bus;
2232 netiucv_dev->parent = iucv_root;
2233 netiucv_dev->release = (void (*)(struct device *))kfree;
2234 netiucv_dev->driver = &netiucv_driver;
2235 rc = device_register(netiucv_dev);
Sebastian Ottc6304932009-09-11 10:28:38 +02002236 if (rc) {
2237 put_device(netiucv_dev);
Ursula Braun1175b252009-06-16 10:30:43 +02002238 goto out_driver;
Sebastian Ottc6304932009-09-11 10:28:38 +02002239 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002240 netiucv_banner();
2241 return rc;
2242
Ursula Braun1175b252009-06-16 10:30:43 +02002243out_driver:
2244 driver_unregister(&netiucv_driver);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002245out_iucv:
2246 iucv_unregister(&netiucv_handler, 1);
2247out_dbf:
2248 iucv_unregister_dbf_views();
2249out:
2250 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
Jeff Garzike82b0f22006-05-26 21:58:38 -04002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253module_init(netiucv_init);
2254module_exit(netiucv_exit);
2255MODULE_LICENSE("GPL");