blob: 9215fbbccc0816ca83ab5901c95247c48bd93909 [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>
66
Martin Schwidefskyeebce382007-02-08 13:50:33 -080067#include <net/iucv/iucv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "fsm.h"
69
70MODULE_AUTHOR
71 ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
72MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
73
Martin Schwidefskyeebce382007-02-08 13:50:33 -080074/**
75 * Debug Facility stuff
76 */
77#define IUCV_DBF_SETUP_NAME "iucv_setup"
78#define IUCV_DBF_SETUP_LEN 32
79#define IUCV_DBF_SETUP_PAGES 2
80#define IUCV_DBF_SETUP_NR_AREAS 1
81#define IUCV_DBF_SETUP_LEVEL 3
82
83#define IUCV_DBF_DATA_NAME "iucv_data"
84#define IUCV_DBF_DATA_LEN 128
85#define IUCV_DBF_DATA_PAGES 2
86#define IUCV_DBF_DATA_NR_AREAS 1
87#define IUCV_DBF_DATA_LEVEL 2
88
89#define IUCV_DBF_TRACE_NAME "iucv_trace"
90#define IUCV_DBF_TRACE_LEN 16
91#define IUCV_DBF_TRACE_PAGES 4
92#define IUCV_DBF_TRACE_NR_AREAS 1
93#define IUCV_DBF_TRACE_LEVEL 3
94
95#define IUCV_DBF_TEXT(name,level,text) \
96 do { \
97 debug_text_event(iucv_dbf_##name,level,text); \
98 } while (0)
99
100#define IUCV_DBF_HEX(name,level,addr,len) \
101 do { \
102 debug_event(iucv_dbf_##name,level,(void*)(addr),len); \
103 } while (0)
104
105DECLARE_PER_CPU(char[256], iucv_dbf_txt_buf);
106
Peter Tiedemannf33780d2008-02-08 13:09:05 +0100107/* Allow to sort out low debug levels early to avoid wasted sprints */
108static inline int iucv_dbf_passes(debug_info_t *dbf_grp, int level)
109{
110 return (level <= dbf_grp->level);
111}
112
113#define IUCV_DBF_TEXT_(name, level, text...) \
114 do { \
115 if (iucv_dbf_passes(iucv_dbf_##name, level)) { \
116 char* iucv_dbf_txt_buf = \
117 get_cpu_var(iucv_dbf_txt_buf); \
118 sprintf(iucv_dbf_txt_buf, text); \
119 debug_text_event(iucv_dbf_##name, level, \
120 iucv_dbf_txt_buf); \
121 put_cpu_var(iucv_dbf_txt_buf); \
122 } \
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800123 } while (0)
124
125#define IUCV_DBF_SPRINTF(name,level,text...) \
126 do { \
127 debug_sprintf_event(iucv_dbf_trace, level, ##text ); \
128 debug_sprintf_event(iucv_dbf_trace, level, text ); \
129 } while (0)
130
131/**
132 * some more debug stuff
133 */
134#define IUCV_HEXDUMP16(importance,header,ptr) \
135PRINT_##importance(header "%02x %02x %02x %02x %02x %02x %02x %02x " \
136 "%02x %02x %02x %02x %02x %02x %02x %02x\n", \
137 *(((char*)ptr)),*(((char*)ptr)+1),*(((char*)ptr)+2), \
138 *(((char*)ptr)+3),*(((char*)ptr)+4),*(((char*)ptr)+5), \
139 *(((char*)ptr)+6),*(((char*)ptr)+7),*(((char*)ptr)+8), \
140 *(((char*)ptr)+9),*(((char*)ptr)+10),*(((char*)ptr)+11), \
141 *(((char*)ptr)+12),*(((char*)ptr)+13), \
142 *(((char*)ptr)+14),*(((char*)ptr)+15)); \
143PRINT_##importance(header "%02x %02x %02x %02x %02x %02x %02x %02x " \
144 "%02x %02x %02x %02x %02x %02x %02x %02x\n", \
145 *(((char*)ptr)+16),*(((char*)ptr)+17), \
146 *(((char*)ptr)+18),*(((char*)ptr)+19), \
147 *(((char*)ptr)+20),*(((char*)ptr)+21), \
148 *(((char*)ptr)+22),*(((char*)ptr)+23), \
149 *(((char*)ptr)+24),*(((char*)ptr)+25), \
150 *(((char*)ptr)+26),*(((char*)ptr)+27), \
151 *(((char*)ptr)+28),*(((char*)ptr)+29), \
152 *(((char*)ptr)+30),*(((char*)ptr)+31));
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define PRINTK_HEADER " iucv: " /* for debugging */
155
Ursula Braun1175b252009-06-16 10:30:43 +0200156/* dummy device to make sure netiucv_pm functions are called */
157static struct device *netiucv_dev;
158
159static int netiucv_pm_prepare(struct device *);
160static void netiucv_pm_complete(struct device *);
161static int netiucv_pm_freeze(struct device *);
162static int netiucv_pm_restore_thaw(struct device *);
163
164static struct dev_pm_ops netiucv_pm_ops = {
165 .prepare = netiucv_pm_prepare,
166 .complete = netiucv_pm_complete,
167 .freeze = netiucv_pm_freeze,
168 .thaw = netiucv_pm_restore_thaw,
169 .restore = netiucv_pm_restore_thaw,
170};
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static struct device_driver netiucv_driver = {
Cornelia Huck22195102008-02-08 13:09:02 +0100173 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .name = "netiucv",
175 .bus = &iucv_bus,
Ursula Braun1175b252009-06-16 10:30:43 +0200176 .pm = &netiucv_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800179static int netiucv_callback_connreq(struct iucv_path *,
180 u8 ipvmid[8], u8 ipuser[16]);
181static void netiucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
182static void netiucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
183static void netiucv_callback_connsusp(struct iucv_path *, u8 ipuser[16]);
184static void netiucv_callback_connres(struct iucv_path *, u8 ipuser[16]);
185static void netiucv_callback_rx(struct iucv_path *, struct iucv_message *);
186static void netiucv_callback_txdone(struct iucv_path *, struct iucv_message *);
187
188static struct iucv_handler netiucv_handler = {
189 .path_pending = netiucv_callback_connreq,
190 .path_complete = netiucv_callback_connack,
191 .path_severed = netiucv_callback_connrej,
192 .path_quiesced = netiucv_callback_connsusp,
193 .path_resumed = netiucv_callback_connres,
194 .message_pending = netiucv_callback_rx,
195 .message_complete = netiucv_callback_txdone
196};
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/**
199 * Per connection profiling data
200 */
201struct connection_profile {
202 unsigned long maxmulti;
203 unsigned long maxcqueue;
204 unsigned long doios_single;
205 unsigned long doios_multi;
206 unsigned long txlen;
207 unsigned long tx_time;
208 struct timespec send_stamp;
209 unsigned long tx_pending;
210 unsigned long tx_max_pending;
211};
212
213/**
214 * Representation of one iucv connection
215 */
216struct iucv_connection {
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800217 struct list_head list;
218 struct iucv_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct sk_buff *rx_buff;
220 struct sk_buff *tx_buff;
221 struct sk_buff_head collect_queue;
222 struct sk_buff_head commit_queue;
223 spinlock_t collect_lock;
224 int collect_len;
225 int max_buffsize;
226 fsm_timer timer;
227 fsm_instance *fsm;
228 struct net_device *netdev;
229 struct connection_profile prof;
230 char userid[9];
231};
232
233/**
234 * Linked list of all connection structs.
235 */
Denis Chengc11ca972008-01-26 14:11:13 +0100236static LIST_HEAD(iucv_connection_list);
Thomas Gleixnerbfac0d02007-06-20 13:02:55 +0200237static DEFINE_RWLOCK(iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/**
240 * Representation of event-data for the
241 * connection state machine.
242 */
243struct iucv_event {
244 struct iucv_connection *conn;
245 void *data;
246};
247
248/**
249 * Private part of the network device structure
250 */
251struct netiucv_priv {
252 struct net_device_stats stats;
253 unsigned long tbusy;
254 fsm_instance *fsm;
255 struct iucv_connection *conn;
256 struct device *dev;
Ursula Braun1175b252009-06-16 10:30:43 +0200257 int pm_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
260/**
261 * Link level header for a packet.
262 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800263struct ll_header {
264 u16 next;
265};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800267#define NETIUCV_HDRLEN (sizeof(struct ll_header))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#define NETIUCV_BUFSIZE_MAX 32768
269#define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX
270#define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
271#define NETIUCV_MTU_DEFAULT 9216
272#define NETIUCV_QUEUELEN_DEFAULT 50
273#define NETIUCV_TIMEOUT_5SEC 5000
274
275/**
276 * Compatibility macros for busy handling
277 * of network devices.
278 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800279static inline void netiucv_clear_busy(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800281 struct netiucv_priv *priv = netdev_priv(dev);
282 clear_bit(0, &priv->tbusy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 netif_wake_queue(dev);
284}
285
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800286static inline int netiucv_test_and_set_busy(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800288 struct netiucv_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 netif_stop_queue(dev);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800290 return test_and_set_bit(0, &priv->tbusy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800293static u8 iucvMagic[16] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
295 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
296};
297
298/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * Convert an iucv userId to its printable
300 * form (strip whitespace at end).
301 *
302 * @param An iucv userId
303 *
304 * @returns The printable string (static data!!)
305 */
Martin Schwidefskyd4614622007-06-20 13:03:57 +0200306static char *netiucv_printname(char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 static char tmp[9];
309 char *p = tmp;
310 memcpy(tmp, name, 8);
311 tmp[8] = '\0';
312 while (*p && (!isspace(*p)))
313 p++;
314 *p = '\0';
315 return tmp;
316}
Jeff Garzike82b0f22006-05-26 21:58:38 -0400317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/**
319 * States of the interface statemachine.
320 */
321enum dev_states {
322 DEV_STATE_STOPPED,
323 DEV_STATE_STARTWAIT,
324 DEV_STATE_STOPWAIT,
325 DEV_STATE_RUNNING,
326 /**
327 * MUST be always the last element!!
328 */
329 NR_DEV_STATES
330};
331
332static const char *dev_state_names[] = {
333 "Stopped",
334 "StartWait",
335 "StopWait",
336 "Running",
337};
338
339/**
340 * Events of the interface statemachine.
341 */
342enum dev_events {
343 DEV_EVENT_START,
344 DEV_EVENT_STOP,
345 DEV_EVENT_CONUP,
346 DEV_EVENT_CONDOWN,
347 /**
348 * MUST be always the last element!!
349 */
350 NR_DEV_EVENTS
351};
352
353static const char *dev_event_names[] = {
354 "Start",
355 "Stop",
356 "Connection up",
357 "Connection down",
358};
Jeff Garzike82b0f22006-05-26 21:58:38 -0400359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/**
361 * Events of the connection statemachine
362 */
363enum conn_events {
364 /**
365 * Events, representing callbacks from
366 * lowlevel iucv layer)
367 */
368 CONN_EVENT_CONN_REQ,
369 CONN_EVENT_CONN_ACK,
370 CONN_EVENT_CONN_REJ,
371 CONN_EVENT_CONN_SUS,
372 CONN_EVENT_CONN_RES,
373 CONN_EVENT_RX,
374 CONN_EVENT_TXDONE,
375
376 /**
377 * Events, representing errors return codes from
378 * calls to lowlevel iucv layer
379 */
380
381 /**
382 * Event, representing timer expiry.
383 */
384 CONN_EVENT_TIMER,
385
386 /**
387 * Events, representing commands from upper levels.
388 */
389 CONN_EVENT_START,
390 CONN_EVENT_STOP,
391
392 /**
393 * MUST be always the last element!!
394 */
395 NR_CONN_EVENTS,
396};
397
398static const char *conn_event_names[] = {
399 "Remote connection request",
400 "Remote connection acknowledge",
401 "Remote connection reject",
402 "Connection suspended",
403 "Connection resumed",
404 "Data received",
405 "Data sent",
406
407 "Timer",
408
409 "Start",
410 "Stop",
411};
412
413/**
414 * States of the connection statemachine.
415 */
416enum conn_states {
417 /**
418 * Connection not assigned to any device,
419 * initial state, invalid
420 */
421 CONN_STATE_INVALID,
422
423 /**
424 * Userid assigned but not operating
425 */
426 CONN_STATE_STOPPED,
427
428 /**
429 * Connection registered,
430 * no connection request sent yet,
431 * no connection request received
432 */
433 CONN_STATE_STARTWAIT,
434
435 /**
436 * Connection registered and connection request sent,
437 * no acknowledge and no connection request received yet.
438 */
439 CONN_STATE_SETUPWAIT,
440
441 /**
442 * Connection up and running idle
443 */
444 CONN_STATE_IDLE,
445
446 /**
447 * Data sent, awaiting CONN_EVENT_TXDONE
448 */
449 CONN_STATE_TX,
450
451 /**
452 * Error during registration.
453 */
454 CONN_STATE_REGERR,
455
456 /**
457 * Error during registration.
458 */
459 CONN_STATE_CONNERR,
460
461 /**
462 * MUST be always the last element!!
463 */
464 NR_CONN_STATES,
465};
466
467static const char *conn_state_names[] = {
468 "Invalid",
469 "Stopped",
470 "StartWait",
471 "SetupWait",
472 "Idle",
473 "TX",
474 "Terminating",
475 "Registration error",
476 "Connect error",
477};
478
Jeff Garzike82b0f22006-05-26 21:58:38 -0400479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/**
481 * Debug Facility Stuff
482 */
483static debug_info_t *iucv_dbf_setup = NULL;
484static debug_info_t *iucv_dbf_data = NULL;
485static debug_info_t *iucv_dbf_trace = NULL;
486
487DEFINE_PER_CPU(char[256], iucv_dbf_txt_buf);
488
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800489static void iucv_unregister_dbf_views(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 if (iucv_dbf_setup)
492 debug_unregister(iucv_dbf_setup);
493 if (iucv_dbf_data)
494 debug_unregister(iucv_dbf_data);
495 if (iucv_dbf_trace)
496 debug_unregister(iucv_dbf_trace);
497}
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800498static int iucv_register_dbf_views(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 iucv_dbf_setup = debug_register(IUCV_DBF_SETUP_NAME,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700501 IUCV_DBF_SETUP_PAGES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 IUCV_DBF_SETUP_NR_AREAS,
503 IUCV_DBF_SETUP_LEN);
504 iucv_dbf_data = debug_register(IUCV_DBF_DATA_NAME,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700505 IUCV_DBF_DATA_PAGES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 IUCV_DBF_DATA_NR_AREAS,
507 IUCV_DBF_DATA_LEN);
508 iucv_dbf_trace = debug_register(IUCV_DBF_TRACE_NAME,
Michael Holzheu66a464d2005-06-25 14:55:33 -0700509 IUCV_DBF_TRACE_PAGES,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 IUCV_DBF_TRACE_NR_AREAS,
511 IUCV_DBF_TRACE_LEN);
512
513 if ((iucv_dbf_setup == NULL) || (iucv_dbf_data == NULL) ||
514 (iucv_dbf_trace == NULL)) {
515 iucv_unregister_dbf_views();
516 return -ENOMEM;
517 }
518 debug_register_view(iucv_dbf_setup, &debug_hex_ascii_view);
519 debug_set_level(iucv_dbf_setup, IUCV_DBF_SETUP_LEVEL);
520
521 debug_register_view(iucv_dbf_data, &debug_hex_ascii_view);
522 debug_set_level(iucv_dbf_data, IUCV_DBF_DATA_LEVEL);
523
524 debug_register_view(iucv_dbf_trace, &debug_hex_ascii_view);
525 debug_set_level(iucv_dbf_trace, IUCV_DBF_TRACE_LEVEL);
526
527 return 0;
528}
529
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800530/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * Callback-wrappers, called from lowlevel iucv layer.
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800534static void netiucv_callback_rx(struct iucv_path *path,
535 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800537 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct iucv_event ev;
539
540 ev.conn = conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800541 ev.data = msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
543}
544
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800545static void netiucv_callback_txdone(struct iucv_path *path,
546 struct iucv_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800548 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct iucv_event ev;
550
551 ev.conn = conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800552 ev.data = msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
554}
555
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800556static void netiucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800558 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800560 fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800563static int netiucv_callback_connreq(struct iucv_path *path,
564 u8 ipvmid[8], u8 ipuser[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800566 struct iucv_connection *conn = path->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct iucv_event ev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800568 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800570 if (memcmp(iucvMagic, ipuser, sizeof(ipuser)))
571 /* ipuser must match iucvMagic. */
572 return -EINVAL;
573 rc = -EINVAL;
574 read_lock_bh(&iucv_connection_rwlock);
575 list_for_each_entry(conn, &iucv_connection_list, list) {
576 if (strncmp(ipvmid, conn->userid, 8))
577 continue;
578 /* Found a matching connection for this path. */
579 conn->path = path;
580 ev.conn = conn;
581 ev.data = path;
582 fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
583 rc = 0;
584 }
585 read_unlock_bh(&iucv_connection_rwlock);
586 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800589static void netiucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
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
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800596static void netiucv_callback_connsusp(struct iucv_path *path, u8 ipuser[16])
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
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800603static void netiucv_callback_connres(struct iucv_path *path, u8 ipuser[16])
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
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800738 if (conn && conn->netdev)
739 privptr = netdev_priv(conn->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 conn->prof.tx_pending--;
741 if (single_flag) {
742 if ((skb = skb_dequeue(&conn->commit_queue))) {
743 atomic_dec(&skb->users);
744 dev_kfree_skb_any(skb);
745 if (privptr) {
746 privptr->stats.tx_packets++;
747 privptr->stats.tx_bytes +=
748 (skb->len - NETIUCV_HDRLEN
749 - NETIUCV_HDRLEN);
750 }
751 }
752 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700753 conn->tx_buff->data = conn->tx_buff->head;
754 skb_reset_tail_pointer(conn->tx_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 conn->tx_buff->len = 0;
756 spin_lock_irqsave(&conn->collect_lock, saveflags);
757 while ((skb = skb_dequeue(&conn->collect_queue))) {
758 header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
759 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
760 NETIUCV_HDRLEN);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300761 skb_copy_from_linear_data(skb,
762 skb_put(conn->tx_buff, skb->len),
763 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 txbytes += skb->len;
765 txpackets++;
766 stat_maxcq++;
767 atomic_dec(&skb->users);
768 dev_kfree_skb_any(skb);
769 }
770 if (conn->collect_len > conn->prof.maxmulti)
771 conn->prof.maxmulti = conn->collect_len;
772 conn->collect_len = 0;
773 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800774 if (conn->tx_buff->len == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 fsm_newstate(fi, CONN_STATE_IDLE);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800776 return;
777 }
778
779 header.next = 0;
780 memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
john stultz2c6b47d2007-07-24 17:47:43 -0700781 conn->prof.send_stamp = current_kernel_time();
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800782 txmsg.class = 0;
783 txmsg.tag = 0;
784 rc = iucv_message_send(conn->path, &txmsg, 0, 0,
785 conn->tx_buff->data, conn->tx_buff->len);
786 conn->prof.doios_multi++;
787 conn->prof.txlen += conn->tx_buff->len;
788 conn->prof.tx_pending++;
789 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
790 conn->prof.tx_max_pending = conn->prof.tx_pending;
791 if (rc) {
792 conn->prof.tx_pending--;
793 fsm_newstate(fi, CONN_STATE_IDLE);
794 if (privptr)
795 privptr->stats.tx_errors += txpackets;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800796 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
797 } else {
798 if (privptr) {
799 privptr->stats.tx_packets += txpackets;
800 privptr->stats.tx_bytes += txbytes;
801 }
802 if (stat_maxcq > conn->prof.maxcqueue)
803 conn->prof.maxcqueue = stat_maxcq;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800807static void conn_action_connaccept(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800809 struct iucv_event *ev = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct iucv_connection *conn = ev->conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800811 struct iucv_path *path = ev->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800813 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200816 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800818 conn->path = path;
819 path->msglim = NETIUCV_QUEUELEN_DEFAULT;
820 path->flags = 0;
821 rc = iucv_path_accept(path, &netiucv_handler, NULL, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 IUCV_DBF_TEXT_(setup, 2, "rc %d from iucv_accept", rc);
824 return;
825 }
826 fsm_newstate(fi, CONN_STATE_IDLE);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800827 netdev->tx_queue_len = conn->path->msglim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
829}
830
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800831static void conn_action_connreject(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800833 struct iucv_event *ev = arg;
834 struct iucv_path *path = ev->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200836 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800837 iucv_path_sever(path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800840static void conn_action_connack(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800842 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800844 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200846 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 fsm_deltimer(&conn->timer);
848 fsm_newstate(fi, CONN_STATE_IDLE);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800849 netdev->tx_queue_len = conn->path->msglim;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
851}
852
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800853static void conn_action_conntimsev(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800855 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200857 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 fsm_deltimer(&conn->timer);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800859 iucv_path_sever(conn->path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 fsm_newstate(fi, CONN_STATE_STARTWAIT);
861}
862
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800863static void conn_action_connsever(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800865 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800867 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200869 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 fsm_deltimer(&conn->timer);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800872 iucv_path_sever(conn->path, NULL);
Ursula Braun8f7c5022008-12-25 13:39:47 +0100873 dev_info(privptr->dev, "The peer interface of the IUCV device"
874 " has closed the connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 IUCV_DBF_TEXT(data, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800876 "conn_action_connsever: Remote dropped connection\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 fsm_newstate(fi, CONN_STATE_STARTWAIT);
878 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
879}
880
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800881static void conn_action_start(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800883 struct iucv_connection *conn = arg;
Ursula Braun8f7c5022008-12-25 13:39:47 +0100884 struct net_device *netdev = conn->netdev;
885 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int rc;
887
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200888 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800890 fsm_newstate(fi, CONN_STATE_STARTWAIT);
Ursula Braunf082bca2008-07-14 09:59:30 +0200891 IUCV_DBF_TEXT_(setup, 2, "%s('%s'): connecting ...\n",
Ursula Braun8f7c5022008-12-25 13:39:47 +0100892 netdev->name, conn->userid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800894 /*
895 * We must set the state before calling iucv_connect because the
896 * callback handler could be called at any point after the connection
897 * request is sent
898 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 fsm_newstate(fi, CONN_STATE_SETUPWAIT);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800901 conn->path = iucv_path_alloc(NETIUCV_QUEUELEN_DEFAULT, 0, GFP_KERNEL);
902 rc = iucv_path_connect(conn->path, &netiucv_handler, conn->userid,
903 NULL, iucvMagic, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 switch (rc) {
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800905 case 0:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100906 netdev->tx_queue_len = conn->path->msglim;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800907 fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
908 CONN_EVENT_TIMER, conn);
909 return;
910 case 11:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100911 dev_warn(privptr->dev,
912 "The IUCV device failed to connect to z/VM guest %s\n",
913 netiucv_printname(conn->userid));
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800914 fsm_newstate(fi, CONN_STATE_STARTWAIT);
915 break;
916 case 12:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100917 dev_warn(privptr->dev,
918 "The IUCV device failed to connect to the peer on z/VM"
919 " guest %s\n", netiucv_printname(conn->userid));
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800920 fsm_newstate(fi, CONN_STATE_STARTWAIT);
921 break;
922 case 13:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100923 dev_err(privptr->dev,
924 "Connecting the IUCV device would exceed the maximum"
925 " number of IUCV connections\n");
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800926 fsm_newstate(fi, CONN_STATE_CONNERR);
927 break;
928 case 14:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100929 dev_err(privptr->dev,
930 "z/VM guest %s has too many IUCV connections"
931 " to connect with the IUCV device\n",
932 netiucv_printname(conn->userid));
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800933 fsm_newstate(fi, CONN_STATE_CONNERR);
934 break;
935 case 15:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100936 dev_err(privptr->dev,
937 "The IUCV device cannot connect to a z/VM guest with no"
938 " IUCV authorization\n");
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800939 fsm_newstate(fi, CONN_STATE_CONNERR);
940 break;
941 default:
Ursula Braun8f7c5022008-12-25 13:39:47 +0100942 dev_err(privptr->dev,
943 "Connecting the IUCV device failed with error %d\n",
944 rc);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800945 fsm_newstate(fi, CONN_STATE_CONNERR);
946 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 IUCV_DBF_TEXT_(setup, 5, "iucv_connect rc is %d\n", rc);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800949 kfree(conn->path);
950 conn->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800953static void netiucv_purge_skb_queue(struct sk_buff_head *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 struct sk_buff *skb;
956
957 while ((skb = skb_dequeue(q))) {
958 atomic_dec(&skb->users);
959 dev_kfree_skb_any(skb);
960 }
961}
962
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800963static void conn_action_stop(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800965 struct iucv_event *ev = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct iucv_connection *conn = ev->conn;
967 struct net_device *netdev = conn->netdev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800968 struct netiucv_priv *privptr = netdev_priv(netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +0200970 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 fsm_deltimer(&conn->timer);
973 fsm_newstate(fi, CONN_STATE_STOPPED);
974 netiucv_purge_skb_queue(&conn->collect_queue);
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800975 if (conn->path) {
976 IUCV_DBF_TEXT(trace, 5, "calling iucv_path_sever\n");
977 iucv_path_sever(conn->path, iucvMagic);
978 kfree(conn->path);
979 conn->path = NULL;
980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 netiucv_purge_skb_queue(&conn->commit_queue);
982 fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
983}
984
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800985static void conn_action_inval(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Martin Schwidefskyeebce382007-02-08 13:50:33 -0800987 struct iucv_connection *conn = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 struct net_device *netdev = conn->netdev;
989
Ursula Braunf082bca2008-07-14 09:59:30 +0200990 IUCV_DBF_TEXT_(data, 2, "%s('%s'): conn_action_inval called\n",
991 netdev->name, conn->userid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static const fsm_node conn_fsm[] = {
995 { CONN_STATE_INVALID, CONN_EVENT_START, conn_action_inval },
996 { CONN_STATE_STOPPED, CONN_EVENT_START, conn_action_start },
997
998 { CONN_STATE_STOPPED, CONN_EVENT_STOP, conn_action_stop },
999 { CONN_STATE_STARTWAIT, CONN_EVENT_STOP, conn_action_stop },
1000 { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP, conn_action_stop },
1001 { CONN_STATE_IDLE, CONN_EVENT_STOP, conn_action_stop },
1002 { CONN_STATE_TX, CONN_EVENT_STOP, conn_action_stop },
1003 { CONN_STATE_REGERR, CONN_EVENT_STOP, conn_action_stop },
1004 { CONN_STATE_CONNERR, CONN_EVENT_STOP, conn_action_stop },
1005
1006 { CONN_STATE_STOPPED, CONN_EVENT_CONN_REQ, conn_action_connreject },
1007 { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
1008 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
1009 { CONN_STATE_IDLE, CONN_EVENT_CONN_REQ, conn_action_connreject },
1010 { CONN_STATE_TX, CONN_EVENT_CONN_REQ, conn_action_connreject },
1011
1012 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack },
1013 { CONN_STATE_SETUPWAIT, CONN_EVENT_TIMER, conn_action_conntimsev },
1014
1015 { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever },
1016 { CONN_STATE_IDLE, CONN_EVENT_CONN_REJ, conn_action_connsever },
1017 { CONN_STATE_TX, CONN_EVENT_CONN_REJ, conn_action_connsever },
1018
1019 { CONN_STATE_IDLE, CONN_EVENT_RX, conn_action_rx },
1020 { CONN_STATE_TX, CONN_EVENT_RX, conn_action_rx },
1021
1022 { CONN_STATE_TX, CONN_EVENT_TXDONE, conn_action_txdone },
1023 { CONN_STATE_IDLE, CONN_EVENT_TXDONE, conn_action_txdone },
1024};
1025
1026static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
1027
Jeff Garzike82b0f22006-05-26 21:58:38 -04001028
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001029/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * Actions for interface - statemachine.
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001031 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033/**
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001034 * dev_action_start
1035 * @fi: An instance of an interface statemachine.
1036 * @event: The event, just happened.
1037 * @arg: Generic pointer, casted from struct net_device * upon call.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001039 * Startup connection by sending CONN_EVENT_START to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001041static void dev_action_start(fsm_instance *fi, int event, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001043 struct net_device *dev = arg;
1044 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001046 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 fsm_newstate(fi, DEV_STATE_STARTWAIT);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001049 fsm_event(privptr->conn->fsm, CONN_EVENT_START, privptr->conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052/**
1053 * Shutdown connection by sending CONN_EVENT_STOP to it.
1054 *
1055 * @param fi An instance of an interface statemachine.
1056 * @param event The event, just happened.
1057 * @param arg Generic pointer, casted from struct net_device * upon call.
1058 */
1059static void
1060dev_action_stop(fsm_instance *fi, int event, void *arg)
1061{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001062 struct net_device *dev = arg;
1063 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct iucv_event ev;
1065
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001066 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 ev.conn = privptr->conn;
1069
1070 fsm_newstate(fi, DEV_STATE_STOPWAIT);
1071 fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev);
1072}
1073
1074/**
1075 * Called from connection statemachine
1076 * when a connection is up and running.
1077 *
1078 * @param fi An instance of an interface statemachine.
1079 * @param event The event, just happened.
1080 * @param arg Generic pointer, casted from struct net_device * upon call.
1081 */
1082static void
1083dev_action_connup(fsm_instance *fi, int event, void *arg)
1084{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001085 struct net_device *dev = arg;
1086 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001088 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 switch (fsm_getstate(fi)) {
1091 case DEV_STATE_STARTWAIT:
1092 fsm_newstate(fi, DEV_STATE_RUNNING);
Ursula Braun8f7c5022008-12-25 13:39:47 +01001093 dev_info(privptr->dev,
1094 "The IUCV device has been connected"
1095 " successfully to %s\n", privptr->conn->userid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 IUCV_DBF_TEXT(setup, 3,
1097 "connection is up and running\n");
1098 break;
1099 case DEV_STATE_STOPWAIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 IUCV_DBF_TEXT(data, 2,
1101 "dev_action_connup: in DEV_STATE_STOPWAIT\n");
1102 break;
1103 }
1104}
1105
1106/**
1107 * Called from connection statemachine
1108 * when a connection has been shutdown.
1109 *
1110 * @param fi An instance of an interface statemachine.
1111 * @param event The event, just happened.
1112 * @param arg Generic pointer, casted from struct net_device * upon call.
1113 */
1114static void
1115dev_action_conndown(fsm_instance *fi, int event, void *arg)
1116{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001117 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 switch (fsm_getstate(fi)) {
1120 case DEV_STATE_RUNNING:
1121 fsm_newstate(fi, DEV_STATE_STARTWAIT);
1122 break;
1123 case DEV_STATE_STOPWAIT:
1124 fsm_newstate(fi, DEV_STATE_STOPPED);
1125 IUCV_DBF_TEXT(setup, 3, "connection is down\n");
1126 break;
1127 }
1128}
1129
1130static const fsm_node dev_fsm[] = {
1131 { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start },
1132
1133 { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start },
1134 { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
1135
1136 { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop },
1137 { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup },
1138
1139 { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
1140 { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown },
Ursula Braun21b26f2f2008-02-08 13:09:03 +01001141 { DEV_STATE_RUNNING, DEV_EVENT_CONUP, netiucv_action_nop },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};
1143
1144static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
1145
1146/**
1147 * Transmit a packet.
1148 * This is a helper function for netiucv_tx().
1149 *
1150 * @param conn Connection to be used for sending.
1151 * @param skb Pointer to struct sk_buff of packet to send.
1152 * The linklevel header has already been set up
1153 * by netiucv_tx().
1154 *
1155 * @return 0 on success, -ERRNO on failure. (Never fails.)
1156 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001157static int netiucv_transmit_skb(struct iucv_connection *conn,
1158 struct sk_buff *skb)
1159{
1160 struct iucv_message msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned long saveflags;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001162 struct ll_header header;
1163 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
1166 int l = skb->len + NETIUCV_HDRLEN;
1167
1168 spin_lock_irqsave(&conn->collect_lock, saveflags);
1169 if (conn->collect_len + l >
1170 (conn->max_buffsize - NETIUCV_HDRLEN)) {
1171 rc = -EBUSY;
1172 IUCV_DBF_TEXT(data, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001173 "EBUSY from netiucv_transmit_skb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 } else {
1175 atomic_inc(&skb->users);
1176 skb_queue_tail(&conn->collect_queue, skb);
1177 conn->collect_len += l;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001178 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180 spin_unlock_irqrestore(&conn->collect_lock, saveflags);
1181 } else {
1182 struct sk_buff *nskb = skb;
1183 /**
1184 * Copy the skb to a new allocated skb in lowmem only if the
1185 * data is located above 2G in memory or tailroom is < 2.
1186 */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001187 unsigned long hi = ((unsigned long)(skb_tail_pointer(skb) +
1188 NETIUCV_HDRLEN)) >> 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int copied = 0;
1190 if (hi || (skb_tailroom(skb) < 2)) {
1191 nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
1192 NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
1193 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 IUCV_DBF_TEXT(data, 2, "alloc_skb failed\n");
1195 rc = -ENOMEM;
1196 return rc;
1197 } else {
1198 skb_reserve(nskb, NETIUCV_HDRLEN);
1199 memcpy(skb_put(nskb, skb->len),
1200 skb->data, skb->len);
1201 }
1202 copied = 1;
1203 }
1204 /**
1205 * skb now is below 2G and has enough room. Add headers.
1206 */
1207 header.next = nskb->len + NETIUCV_HDRLEN;
1208 memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1209 header.next = 0;
1210 memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
1211
1212 fsm_newstate(conn->fsm, CONN_STATE_TX);
john stultz2c6b47d2007-07-24 17:47:43 -07001213 conn->prof.send_stamp = current_kernel_time();
Jeff Garzike82b0f22006-05-26 21:58:38 -04001214
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001215 msg.tag = 1;
1216 msg.class = 0;
1217 rc = iucv_message_send(conn->path, &msg, 0, 0,
1218 nskb->data, nskb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 conn->prof.doios_single++;
1220 conn->prof.txlen += skb->len;
1221 conn->prof.tx_pending++;
1222 if (conn->prof.tx_pending > conn->prof.tx_max_pending)
1223 conn->prof.tx_max_pending = conn->prof.tx_pending;
1224 if (rc) {
1225 struct netiucv_priv *privptr;
1226 fsm_newstate(conn->fsm, CONN_STATE_IDLE);
1227 conn->prof.tx_pending--;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001228 privptr = netdev_priv(conn->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (privptr)
1230 privptr->stats.tx_errors++;
1231 if (copied)
1232 dev_kfree_skb(nskb);
1233 else {
1234 /**
1235 * Remove our headers. They get added
1236 * again on retransmit.
1237 */
1238 skb_pull(skb, NETIUCV_HDRLEN);
1239 skb_trim(skb, skb->len - NETIUCV_HDRLEN);
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
1242 } else {
1243 if (copied)
1244 dev_kfree_skb(skb);
1245 atomic_inc(&nskb->users);
1246 skb_queue_tail(&conn->commit_queue, nskb);
1247 }
1248 }
1249
1250 return rc;
1251}
Jeff Garzike82b0f22006-05-26 21:58:38 -04001252
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001253/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * Interface API for upper network layers
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001255 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257/**
1258 * Open an interface.
1259 * Called from generic network layer when ifconfig up is run.
1260 *
1261 * @param dev Pointer to interface struct.
1262 *
1263 * @return 0 on success, -ERRNO on failure. (Never fails.)
1264 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001265static int netiucv_open(struct net_device *dev)
1266{
1267 struct netiucv_priv *priv = netdev_priv(dev);
1268
1269 fsm_event(priv->fsm, DEV_EVENT_START, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return 0;
1271}
1272
1273/**
1274 * Close an interface.
1275 * Called from generic network layer when ifconfig down is run.
1276 *
1277 * @param dev Pointer to interface struct.
1278 *
1279 * @return 0 on success, -ERRNO on failure. (Never fails.)
1280 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001281static int netiucv_close(struct net_device *dev)
1282{
1283 struct netiucv_priv *priv = netdev_priv(dev);
1284
1285 fsm_event(priv->fsm, DEV_EVENT_STOP, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287}
1288
Ursula Braun1175b252009-06-16 10:30:43 +02001289static int netiucv_pm_prepare(struct device *dev)
1290{
1291 IUCV_DBF_TEXT(trace, 3, __func__);
1292 return 0;
1293}
1294
1295static void netiucv_pm_complete(struct device *dev)
1296{
1297 IUCV_DBF_TEXT(trace, 3, __func__);
1298 return;
1299}
1300
1301/**
1302 * netiucv_pm_freeze() - Freeze PM callback
1303 * @dev: netiucv device
1304 *
1305 * close open netiucv interfaces
1306 */
1307static int netiucv_pm_freeze(struct device *dev)
1308{
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001309 struct netiucv_priv *priv = dev_get_drvdata(dev);
Ursula Braun1175b252009-06-16 10:30:43 +02001310 struct net_device *ndev = NULL;
1311 int rc = 0;
1312
1313 IUCV_DBF_TEXT(trace, 3, __func__);
1314 if (priv && priv->conn)
1315 ndev = priv->conn->netdev;
1316 if (!ndev)
1317 goto out;
1318 netif_device_detach(ndev);
1319 priv->pm_state = fsm_getstate(priv->fsm);
1320 rc = netiucv_close(ndev);
1321out:
1322 return rc;
1323}
1324
1325/**
1326 * netiucv_pm_restore_thaw() - Thaw and restore PM callback
1327 * @dev: netiucv device
1328 *
1329 * re-open netiucv interfaces closed during freeze
1330 */
1331static int netiucv_pm_restore_thaw(struct device *dev)
1332{
Martin Schwidefsky4f0076f2009-06-22 12:08:19 +02001333 struct netiucv_priv *priv = dev_get_drvdata(dev);
Ursula Braun1175b252009-06-16 10:30:43 +02001334 struct net_device *ndev = NULL;
1335 int rc = 0;
1336
1337 IUCV_DBF_TEXT(trace, 3, __func__);
1338 if (priv && priv->conn)
1339 ndev = priv->conn->netdev;
1340 if (!ndev)
1341 goto out;
1342 switch (priv->pm_state) {
1343 case DEV_STATE_RUNNING:
1344 case DEV_STATE_STARTWAIT:
1345 rc = netiucv_open(ndev);
1346 break;
1347 default:
1348 break;
1349 }
1350 netif_device_attach(ndev);
1351out:
1352 return rc;
1353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355/**
1356 * Start transmission of a packet.
1357 * Called from generic network device layer.
1358 *
1359 * @param skb Pointer to buffer containing the packet.
1360 * @param dev Pointer to interface struct.
1361 *
1362 * @return 0 if packet consumed, !0 if packet rejected.
1363 * Note: If we return !0, then the packet is free'd by
1364 * the generic network layer.
1365 */
1366static int netiucv_tx(struct sk_buff *skb, struct net_device *dev)
1367{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001368 struct netiucv_priv *privptr = netdev_priv(dev);
1369 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001371 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /**
1373 * Some sanity checks ...
1374 */
1375 if (skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 IUCV_DBF_TEXT(data, 2, "netiucv_tx: skb is NULL\n");
1377 privptr->stats.tx_dropped++;
Patrick McHardyec634fe2009-07-05 19:23:38 -07001378 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 if (skb_headroom(skb) < NETIUCV_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 IUCV_DBF_TEXT(data, 2,
1382 "netiucv_tx: skb_headroom < NETIUCV_HDRLEN\n");
1383 dev_kfree_skb(skb);
1384 privptr->stats.tx_dropped++;
Patrick McHardyec634fe2009-07-05 19:23:38 -07001385 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
1388 /**
1389 * If connection is not running, try to restart it
Jeff Garzike82b0f22006-05-26 21:58:38 -04001390 * and throw away packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
1392 if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 dev_kfree_skb(skb);
1394 privptr->stats.tx_dropped++;
1395 privptr->stats.tx_errors++;
1396 privptr->stats.tx_carrier_errors++;
Patrick McHardyec634fe2009-07-05 19:23:38 -07001397 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
1400 if (netiucv_test_and_set_busy(dev)) {
1401 IUCV_DBF_TEXT(data, 2, "EBUSY from netiucv_tx\n");
Ursula Braun4e584d62009-03-24 03:27:45 +00001402 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 dev->trans_start = jiffies;
Patrick McHardy5b548142009-06-12 06:22:29 +00001405 rc = netiucv_transmit_skb(privptr->conn, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 netiucv_clear_busy(dev);
Patrick McHardy5b548142009-06-12 06:22:29 +00001407 return rc ? NETDEV_TX_BUSY : NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410/**
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001411 * netiucv_stats
1412 * @dev: Pointer to interface struct.
1413 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * Returns interface statistics of a device.
1415 *
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001416 * Returns pointer to stats struct of this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001418static struct net_device_stats *netiucv_stats (struct net_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001420 struct netiucv_priv *priv = netdev_priv(dev);
1421
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001422 IUCV_DBF_TEXT(trace, 5, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001423 return &priv->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426/**
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001427 * netiucv_change_mtu
1428 * @dev: Pointer to interface struct.
1429 * @new_mtu: The new MTU to use for this interface.
1430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 * Sets MTU of an interface.
1432 *
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001433 * Returns 0 on success, -EINVAL if MTU is out of valid range.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 * (valid range is 576 .. NETIUCV_MTU_MAX).
1435 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001436static int netiucv_change_mtu(struct net_device * dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001438 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001439 if (new_mtu < 576 || new_mtu > NETIUCV_MTU_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 IUCV_DBF_TEXT(setup, 2, "given MTU out of valid range\n");
1441 return -EINVAL;
1442 }
1443 dev->mtu = new_mtu;
1444 return 0;
1445}
1446
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001447/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 * attributes in sysfs
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001451static ssize_t user_show(struct device *dev, struct device_attribute *attr,
1452 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001454 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001456 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return sprintf(buf, "%s\n", netiucv_printname(priv->conn->userid));
1458}
1459
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001460static ssize_t user_write(struct device *dev, struct device_attribute *attr,
1461 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001463 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct net_device *ndev = priv->conn->netdev;
1465 char *p;
1466 char *tmp;
Frank Pavlic16a83b32006-09-15 16:25:19 +02001467 char username[9];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int i;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001469 struct iucv_connection *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001471 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001472 if (count > 9) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 IUCV_DBF_TEXT_(setup, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001474 "%d is length of username\n", (int) count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return -EINVAL;
1476 }
1477
1478 tmp = strsep((char **) &buf, "\n");
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001479 for (i = 0, p = tmp; i < 8 && *p; i++, p++) {
1480 if (isalnum(*p) || (*p == '$')) {
Frank Pavlic16a83b32006-09-15 16:25:19 +02001481 username[i]= toupper(*p);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001482 continue;
1483 }
1484 if (*p == '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* trailing lf, grr */
1486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001488 IUCV_DBF_TEXT_(setup, 2,
1489 "username: invalid character %c\n", *p);
1490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001492 while (i < 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 username[i++] = ' ';
Frank Pavlic16a83b32006-09-15 16:25:19 +02001494 username[8] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001496 if (memcmp(username, priv->conn->userid, 9) &&
1497 (ndev->flags & (IFF_UP | IFF_RUNNING))) {
1498 /* username changed while the interface is active. */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001499 IUCV_DBF_TEXT(setup, 2, "user_write: device active\n");
Ursula Braunf082bca2008-07-14 09:59:30 +02001500 return -EPERM;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001501 }
1502 read_lock_bh(&iucv_connection_rwlock);
1503 list_for_each_entry(cp, &iucv_connection_list, list) {
1504 if (!strncmp(username, cp->userid, 9) && cp->netdev != ndev) {
1505 read_unlock_bh(&iucv_connection_rwlock);
Ursula Braunf082bca2008-07-14 09:59:30 +02001506 IUCV_DBF_TEXT_(setup, 2, "user_write: Connection "
1507 "to %s already exists\n", username);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001508 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001511 read_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 memcpy(priv->conn->userid, username, 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516static DEVICE_ATTR(user, 0644, user_show, user_write);
1517
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001518static ssize_t buffer_show (struct device *dev, struct device_attribute *attr,
1519 char *buf)
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001520{
1521 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001523 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return sprintf(buf, "%d\n", priv->conn->max_buffsize);
1525}
1526
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001527static ssize_t buffer_write (struct device *dev, struct device_attribute *attr,
1528 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001530 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct net_device *ndev = priv->conn->netdev;
1532 char *e;
1533 int bs1;
1534
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001535 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (count >= 39)
1537 return -EINVAL;
1538
1539 bs1 = simple_strtoul(buf, &e, 0);
1540
1541 if (e && (!isspace(*e))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 IUCV_DBF_TEXT_(setup, 2, "buffer_write: invalid char %c\n", *e);
1543 return -EINVAL;
1544 }
1545 if (bs1 > NETIUCV_BUFSIZE_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 IUCV_DBF_TEXT_(setup, 2,
1547 "buffer_write: buffer size %d too large\n",
1548 bs1);
1549 return -EINVAL;
1550 }
1551 if ((ndev->flags & IFF_RUNNING) &&
1552 (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 IUCV_DBF_TEXT_(setup, 2,
1554 "buffer_write: buffer size %d too small\n",
1555 bs1);
1556 return -EINVAL;
1557 }
1558 if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 IUCV_DBF_TEXT_(setup, 2,
1560 "buffer_write: buffer size %d too small\n",
1561 bs1);
1562 return -EINVAL;
1563 }
1564
1565 priv->conn->max_buffsize = bs1;
1566 if (!(ndev->flags & IFF_RUNNING))
1567 ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
1568
1569 return count;
1570
1571}
1572
1573static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
1574
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001575static ssize_t dev_fsm_show (struct device *dev, struct device_attribute *attr,
1576 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001578 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001580 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm));
1582}
1583
1584static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
1585
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001586static ssize_t conn_fsm_show (struct device *dev,
1587 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001589 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001591 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm));
1593}
1594
1595static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
1596
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001597static ssize_t maxmulti_show (struct device *dev,
1598 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001600 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001602 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
1604}
1605
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001606static ssize_t maxmulti_write (struct device *dev,
1607 struct device_attribute *attr,
1608 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001610 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001612 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 priv->conn->prof.maxmulti = 0;
1614 return count;
1615}
1616
1617static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
1618
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001619static ssize_t maxcq_show (struct device *dev, struct device_attribute *attr,
1620 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001622 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001624 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
1626}
1627
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001628static ssize_t maxcq_write (struct device *dev, struct device_attribute *attr,
1629 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001631 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001632
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001633 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 priv->conn->prof.maxcqueue = 0;
1635 return count;
1636}
1637
1638static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
1639
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001640static ssize_t sdoio_show (struct device *dev, struct device_attribute *attr,
1641 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001643 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001645 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
1647}
1648
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001649static ssize_t sdoio_write (struct device *dev, struct device_attribute *attr,
1650 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001652 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001653
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001654 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 priv->conn->prof.doios_single = 0;
1656 return count;
1657}
1658
1659static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
1660
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001661static ssize_t mdoio_show (struct device *dev, struct device_attribute *attr,
1662 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001664 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001666 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
1668}
1669
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001670static ssize_t mdoio_write (struct device *dev, struct device_attribute *attr,
1671 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001673 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001674
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001675 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 priv->conn->prof.doios_multi = 0;
1677 return count;
1678}
1679
1680static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
1681
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001682static ssize_t txlen_show (struct device *dev, struct device_attribute *attr,
1683 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001685 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001687 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
1689}
1690
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001691static ssize_t txlen_write (struct device *dev, struct device_attribute *attr,
1692 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001694 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001695
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001696 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 priv->conn->prof.txlen = 0;
1698 return count;
1699}
1700
1701static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
1702
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001703static ssize_t txtime_show (struct device *dev, struct device_attribute *attr,
1704 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001706 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001708 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
1710}
1711
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001712static ssize_t txtime_write (struct device *dev, struct device_attribute *attr,
1713 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001715 struct netiucv_priv *priv = dev_get_drvdata(dev);
Jeff Garzike82b0f22006-05-26 21:58:38 -04001716
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001717 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 priv->conn->prof.tx_time = 0;
1719 return count;
1720}
1721
1722static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
1723
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001724static ssize_t txpend_show (struct device *dev, struct device_attribute *attr,
1725 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001727 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001729 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending);
1731}
1732
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001733static ssize_t txpend_write (struct device *dev, struct device_attribute *attr,
1734 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001736 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001738 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 priv->conn->prof.tx_pending = 0;
1740 return count;
1741}
1742
1743static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write);
1744
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001745static ssize_t txmpnd_show (struct device *dev, struct device_attribute *attr,
1746 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001748 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001750 IUCV_DBF_TEXT(trace, 5, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending);
1752}
1753
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001754static ssize_t txmpnd_write (struct device *dev, struct device_attribute *attr,
1755 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001757 struct netiucv_priv *priv = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001759 IUCV_DBF_TEXT(trace, 4, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 priv->conn->prof.tx_max_pending = 0;
1761 return count;
1762}
1763
1764static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write);
1765
1766static struct attribute *netiucv_attrs[] = {
1767 &dev_attr_buffer.attr,
1768 &dev_attr_user.attr,
1769 NULL,
1770};
1771
1772static struct attribute_group netiucv_attr_group = {
1773 .attrs = netiucv_attrs,
1774};
1775
1776static struct attribute *netiucv_stat_attrs[] = {
1777 &dev_attr_device_fsm_state.attr,
1778 &dev_attr_connection_fsm_state.attr,
1779 &dev_attr_max_tx_buffer_used.attr,
1780 &dev_attr_max_chained_skbs.attr,
1781 &dev_attr_tx_single_write_ops.attr,
1782 &dev_attr_tx_multi_write_ops.attr,
1783 &dev_attr_netto_bytes.attr,
1784 &dev_attr_max_tx_io_time.attr,
1785 &dev_attr_tx_pending.attr,
1786 &dev_attr_tx_max_pending.attr,
1787 NULL,
1788};
1789
1790static struct attribute_group netiucv_stat_attr_group = {
1791 .name = "stats",
1792 .attrs = netiucv_stat_attrs,
1793};
1794
Martin Schwidefskyd4614622007-06-20 13:03:57 +02001795static int netiucv_add_files(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 int ret;
1798
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001799 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 ret = sysfs_create_group(&dev->kobj, &netiucv_attr_group);
1801 if (ret)
1802 return ret;
1803 ret = sysfs_create_group(&dev->kobj, &netiucv_stat_attr_group);
1804 if (ret)
1805 sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
1806 return ret;
1807}
1808
Martin Schwidefskyd4614622007-06-20 13:03:57 +02001809static void netiucv_remove_files(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001811 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 sysfs_remove_group(&dev->kobj, &netiucv_stat_attr_group);
1813 sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
1814}
1815
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001816static int netiucv_register_device(struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001818 struct netiucv_priv *priv = netdev_priv(ndev);
Eric Sesterhenn88abaab2006-03-24 03:15:31 -08001819 struct device *dev = kzalloc(sizeof(struct device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 int ret;
1821
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001822 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (dev) {
Cornelia Huck1bf5b282008-10-10 21:33:10 +02001825 dev_set_name(dev, "net%s", ndev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 dev->bus = &iucv_bus;
1827 dev->parent = iucv_root;
1828 /*
1829 * The release function could be called after the
1830 * module has been unloaded. It's _only_ task is to
1831 * free the struct. Therefore, we specify kfree()
1832 * directly here. (Probably a little bit obfuscating
1833 * but legitime ...).
1834 */
1835 dev->release = (void (*)(struct device *))kfree;
1836 dev->driver = &netiucv_driver;
1837 } else
1838 return -ENOMEM;
1839
1840 ret = device_register(dev);
Sebastian Ottc6304932009-09-11 10:28:38 +02001841 if (ret) {
1842 put_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return ret;
Sebastian Ottc6304932009-09-11 10:28:38 +02001844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 ret = netiucv_add_files(dev);
1846 if (ret)
1847 goto out_unreg;
1848 priv->dev = dev;
Greg Kroah-Hartmandff59b62009-05-04 12:40:54 -07001849 dev_set_drvdata(dev, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return 0;
1851
1852out_unreg:
1853 device_unregister(dev);
1854 return ret;
1855}
1856
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001857static void netiucv_unregister_device(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001859 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 netiucv_remove_files(dev);
1861 device_unregister(dev);
1862}
1863
1864/**
1865 * Allocate and initialize a new connection structure.
1866 * Add it to the list of netiucv connections;
1867 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001868static struct iucv_connection *netiucv_new_connection(struct net_device *dev,
1869 char *username)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001871 struct iucv_connection *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001873 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
1874 if (!conn)
1875 goto out;
1876 skb_queue_head_init(&conn->collect_queue);
1877 skb_queue_head_init(&conn->commit_queue);
1878 spin_lock_init(&conn->collect_lock);
1879 conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
1880 conn->netdev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001882 conn->rx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1883 if (!conn->rx_buff)
1884 goto out_conn;
1885 conn->tx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
1886 if (!conn->tx_buff)
1887 goto out_rx;
1888 conn->fsm = init_fsm("netiucvconn", conn_state_names,
1889 conn_event_names, NR_CONN_STATES,
1890 NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
1891 GFP_KERNEL);
1892 if (!conn->fsm)
1893 goto out_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001895 fsm_settimer(conn->fsm, &conn->timer);
1896 fsm_newstate(conn->fsm, CONN_STATE_INVALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001898 if (username) {
1899 memcpy(conn->userid, username, 9);
1900 fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001902
1903 write_lock_bh(&iucv_connection_rwlock);
1904 list_add_tail(&conn->list, &iucv_connection_list);
1905 write_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return conn;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001907
1908out_tx:
1909 kfree_skb(conn->tx_buff);
1910out_rx:
1911 kfree_skb(conn->rx_buff);
1912out_conn:
1913 kfree(conn);
1914out:
1915 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
1918/**
1919 * Release a connection structure and remove it from the
1920 * list of netiucv connections.
1921 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001922static void netiucv_remove_connection(struct iucv_connection *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001924 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001925 write_lock_bh(&iucv_connection_rwlock);
1926 list_del_init(&conn->list);
1927 write_unlock_bh(&iucv_connection_rwlock);
Ursula Braun0be4ace2007-05-02 15:18:44 +02001928 fsm_deltimer(&conn->timer);
1929 netiucv_purge_skb_queue(&conn->collect_queue);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001930 if (conn->path) {
1931 iucv_path_sever(conn->path, iucvMagic);
1932 kfree(conn->path);
1933 conn->path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Ursula Braun0be4ace2007-05-02 15:18:44 +02001935 netiucv_purge_skb_queue(&conn->commit_queue);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001936 kfree_fsm(conn->fsm);
1937 kfree_skb(conn->rx_buff);
1938 kfree_skb(conn->tx_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
1941/**
1942 * Release everything of a net device.
1943 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001944static void netiucv_free_netdevice(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001946 struct netiucv_priv *privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02001948 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 if (!dev)
1951 return;
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (privptr) {
1954 if (privptr->conn)
1955 netiucv_remove_connection(privptr->conn);
1956 if (privptr->fsm)
1957 kfree_fsm(privptr->fsm);
1958 privptr->conn = NULL; privptr->fsm = NULL;
1959 /* privptr gets freed by free_netdev() */
1960 }
1961 free_netdev(dev);
1962}
1963
1964/**
1965 * Initialize a net device. (Called from kernel in alloc_netdev())
1966 */
Frank Blaschka4edd73b2009-01-09 03:43:58 +00001967static const struct net_device_ops netiucv_netdev_ops = {
1968 .ndo_open = netiucv_open,
1969 .ndo_stop = netiucv_close,
1970 .ndo_get_stats = netiucv_stats,
1971 .ndo_start_xmit = netiucv_tx,
1972 .ndo_change_mtu = netiucv_change_mtu,
1973};
1974
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001975static void netiucv_setup_netdevice(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 dev->mtu = NETIUCV_MTU_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 dev->destructor = netiucv_free_netdevice;
1979 dev->hard_header_len = NETIUCV_HDRLEN;
1980 dev->addr_len = 0;
1981 dev->type = ARPHRD_SLIP;
1982 dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT;
1983 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
Frank Blaschka4edd73b2009-01-09 03:43:58 +00001984 dev->netdev_ops = &netiucv_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
1987/**
1988 * Allocate and initialize everything of a net device.
1989 */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001990static struct net_device *netiucv_init_netdevice(char *username)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
1992 struct netiucv_priv *privptr;
1993 struct net_device *dev;
1994
1995 dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d",
1996 netiucv_setup_netdevice);
1997 if (!dev)
1998 return NULL;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08001999 if (dev_alloc_name(dev, dev->name) < 0)
2000 goto out_netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002002 privptr = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 privptr->fsm = init_fsm("netiucvdev", dev_state_names,
2004 dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
2005 dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002006 if (!privptr->fsm)
2007 goto out_netdev;
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 privptr->conn = netiucv_new_connection(dev, username);
2010 if (!privptr->conn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_new_connection\n");
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002012 goto out_fsm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014 fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return dev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002016
2017out_fsm:
2018 kfree_fsm(privptr->fsm);
2019out_netdev:
2020 free_netdev(dev);
2021 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002024static ssize_t conn_write(struct device_driver *drv,
2025 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002027 const char *p;
Frank Pavlic16a83b32006-09-15 16:25:19 +02002028 char username[9];
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002029 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct net_device *dev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002031 struct netiucv_priv *priv;
2032 struct iucv_connection *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002034 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (count>9) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 IUCV_DBF_TEXT(setup, 2, "conn_write: too long\n");
2037 return -EINVAL;
2038 }
2039
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002040 for (i = 0, p = buf; i < 8 && *p; i++, p++) {
2041 if (isalnum(*p) || *p == '$') {
2042 username[i] = toupper(*p);
2043 continue;
2044 }
2045 if (*p == '\n')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /* trailing lf, grr */
2047 break;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002048 IUCV_DBF_TEXT_(setup, 2,
2049 "conn_write: invalid character %c\n", *p);
2050 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002052 while (i < 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 username[i++] = ' ';
Frank Pavlic16a83b32006-09-15 16:25:19 +02002054 username[8] = '\0';
2055
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002056 read_lock_bh(&iucv_connection_rwlock);
2057 list_for_each_entry(cp, &iucv_connection_list, list) {
2058 if (!strncmp(username, cp->userid, 9)) {
2059 read_unlock_bh(&iucv_connection_rwlock);
Ursula Braunf082bca2008-07-14 09:59:30 +02002060 IUCV_DBF_TEXT_(setup, 2, "conn_write: Connection "
2061 "to %s already exists\n", username);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002062 return -EEXIST;
2063 }
Frank Pavlic16a83b32006-09-15 16:25:19 +02002064 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002065 read_unlock_bh(&iucv_connection_rwlock);
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 dev = netiucv_init_netdevice(username);
2068 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_init_netdevice\n");
2070 return -ENODEV;
2071 }
2072
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002073 rc = netiucv_register_device(dev);
2074 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 IUCV_DBF_TEXT_(setup, 2,
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002076 "ret %d from netiucv_register_device\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto out_free_ndev;
2078 }
2079
2080 /* sysfs magic */
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002081 priv = netdev_priv(dev);
2082 SET_NETDEV_DEV(dev, priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002084 rc = register_netdev(dev);
2085 if (rc)
2086 goto out_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Ursula Braun8f7c5022008-12-25 13:39:47 +01002088 dev_info(priv->dev, "The IUCV interface to %s has been"
2089 " established successfully\n", netiucv_printname(username));
Jeff Garzike82b0f22006-05-26 21:58:38 -04002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return count;
2092
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002093out_unreg:
2094 netiucv_unregister_device(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095out_free_ndev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 netiucv_free_netdevice(dev);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002097 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Heiko Carstens2b67fc42007-02-05 21:16:47 +01002100static DRIVER_ATTR(connection, 0200, NULL, conn_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002102static ssize_t remove_write (struct device_driver *drv,
2103 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002105 struct iucv_connection *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct net_device *ndev;
2107 struct netiucv_priv *priv;
2108 struct device *dev;
2109 char name[IFNAMSIZ];
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002110 const char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 int i;
2112
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002113 IUCV_DBF_TEXT(trace, 3, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 if (count >= IFNAMSIZ)
Frank Pavlic16a83b32006-09-15 16:25:19 +02002116 count = IFNAMSIZ - 1;;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002118 for (i = 0, p = buf; i < count && *p; i++, p++) {
2119 if (*p == '\n' || *p == ' ')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 /* trailing lf, grr */
2121 break;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002122 name[i] = *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
2124 name[i] = '\0';
2125
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002126 read_lock_bh(&iucv_connection_rwlock);
2127 list_for_each_entry(cp, &iucv_connection_list, list) {
2128 ndev = cp->netdev;
2129 priv = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 dev = priv->dev;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002131 if (strncmp(name, ndev->name, count))
2132 continue;
2133 read_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
Ursula Braun8f7c5022008-12-25 13:39:47 +01002135 dev_warn(dev, "The IUCV device is connected"
2136 " to %s and cannot be removed\n",
2137 priv->conn->userid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 IUCV_DBF_TEXT(data, 2, "remove_write: still active\n");
Ursula Braunf082bca2008-07-14 09:59:30 +02002139 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 unregister_netdev(ndev);
2142 netiucv_unregister_device(dev);
2143 return count;
2144 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002145 read_unlock_bh(&iucv_connection_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 IUCV_DBF_TEXT(data, 2, "remove_write: unknown device\n");
2147 return -EINVAL;
2148}
2149
Heiko Carstens2b67fc42007-02-05 21:16:47 +01002150static DRIVER_ATTR(remove, 0200, NULL, remove_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002152static struct attribute * netiucv_drv_attrs[] = {
2153 &driver_attr_connection.attr,
2154 &driver_attr_remove.attr,
2155 NULL,
2156};
2157
2158static struct attribute_group netiucv_drv_attr_group = {
2159 .attrs = netiucv_drv_attrs,
2160};
2161
Cornelia Huck5b88feb2007-12-05 12:50:28 +01002162static struct attribute_group *netiucv_drv_attr_groups[] = {
2163 &netiucv_drv_attr_group,
2164 NULL,
2165};
2166
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002167static void netiucv_banner(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Ursula Braun8f7c5022008-12-25 13:39:47 +01002169 pr_info("driver initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002172static void __exit netiucv_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002174 struct iucv_connection *cp;
2175 struct net_device *ndev;
2176 struct netiucv_priv *priv;
2177 struct device *dev;
2178
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002179 IUCV_DBF_TEXT(trace, 3, __func__);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002180 while (!list_empty(&iucv_connection_list)) {
2181 cp = list_entry(iucv_connection_list.next,
2182 struct iucv_connection, list);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002183 ndev = cp->netdev;
2184 priv = netdev_priv(ndev);
2185 dev = priv->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 unregister_netdev(ndev);
2188 netiucv_unregister_device(dev);
2189 }
2190
Ursula Braun1175b252009-06-16 10:30:43 +02002191 device_unregister(netiucv_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 driver_unregister(&netiucv_driver);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002193 iucv_unregister(&netiucv_handler, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 iucv_unregister_dbf_views();
2195
Ursula Braun8f7c5022008-12-25 13:39:47 +01002196 pr_info("driver unloaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return;
2198}
2199
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002200static int __init netiucv_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002202 int rc;
Jeff Garzike82b0f22006-05-26 21:58:38 -04002203
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002204 rc = iucv_register_dbf_views();
2205 if (rc)
2206 goto out;
2207 rc = iucv_register(&netiucv_handler, 1);
2208 if (rc)
2209 goto out_dbf;
Harvey Harrison2a2cf6b2008-04-17 07:46:21 +02002210 IUCV_DBF_TEXT(trace, 3, __func__);
Cornelia Huck0a0a8312008-04-24 10:15:28 +02002211 netiucv_driver.groups = netiucv_drv_attr_groups;
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002212 rc = driver_register(&netiucv_driver);
2213 if (rc) {
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002214 IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_register\n", rc);
2215 goto out_iucv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
Ursula Braun1175b252009-06-16 10:30:43 +02002217 /* establish dummy device */
2218 netiucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
2219 if (!netiucv_dev) {
2220 rc = -ENOMEM;
2221 goto out_driver;
2222 }
2223 dev_set_name(netiucv_dev, "netiucv");
2224 netiucv_dev->bus = &iucv_bus;
2225 netiucv_dev->parent = iucv_root;
2226 netiucv_dev->release = (void (*)(struct device *))kfree;
2227 netiucv_dev->driver = &netiucv_driver;
2228 rc = device_register(netiucv_dev);
Sebastian Ottc6304932009-09-11 10:28:38 +02002229 if (rc) {
2230 put_device(netiucv_dev);
Ursula Braun1175b252009-06-16 10:30:43 +02002231 goto out_driver;
Sebastian Ottc6304932009-09-11 10:28:38 +02002232 }
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002233 netiucv_banner();
2234 return rc;
2235
Ursula Braun1175b252009-06-16 10:30:43 +02002236out_driver:
2237 driver_unregister(&netiucv_driver);
Martin Schwidefskyeebce382007-02-08 13:50:33 -08002238out_iucv:
2239 iucv_unregister(&netiucv_handler, 1);
2240out_dbf:
2241 iucv_unregister_dbf_views();
2242out:
2243 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
Jeff Garzike82b0f22006-05-26 21:58:38 -04002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246module_init(netiucv_init);
2247module_exit(netiucv_exit);
2248MODULE_LICENSE("GPL");