]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * IUCV network driver |
3 | * | |
1175b257 | 4 | * Copyright IBM Corp. 2001, 2009 |
1da177e4 | 5 | * |
1175b257 UB |
6 | * 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) | |
1da177e4 LT |
13 | * |
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 | * | |
1da177e4 | 36 | */ |
e82b0f2c | 37 | |
8f7c502c UB |
38 | #define KMSG_COMPONENT "netiucv" |
39 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
40 | ||
1da177e4 LT |
41 | #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> | |
1da177e4 LT |
51 | #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> | |
08e3356c | 66 | #include <asm/ebcdic.h> |
1da177e4 | 67 | |
eebce385 | 68 | #include <net/iucv/iucv.h> |
1da177e4 LT |
69 | #include "fsm.h" |
70 | ||
71 | MODULE_AUTHOR | |
72 | ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)"); | |
73 | MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver"); | |
74 | ||
eebce385 MS |
75 | /** |
76 | * Debug Facility stuff | |
77 | */ | |
78 | #define IUCV_DBF_SETUP_NAME "iucv_setup" | |
08e3356c | 79 | #define IUCV_DBF_SETUP_LEN 64 |
eebce385 MS |
80 | #define IUCV_DBF_SETUP_PAGES 2 |
81 | #define IUCV_DBF_SETUP_NR_AREAS 1 | |
82 | #define IUCV_DBF_SETUP_LEVEL 3 | |
83 | ||
84 | #define IUCV_DBF_DATA_NAME "iucv_data" | |
85 | #define IUCV_DBF_DATA_LEN 128 | |
86 | #define IUCV_DBF_DATA_PAGES 2 | |
87 | #define IUCV_DBF_DATA_NR_AREAS 1 | |
88 | #define IUCV_DBF_DATA_LEVEL 2 | |
89 | ||
90 | #define IUCV_DBF_TRACE_NAME "iucv_trace" | |
91 | #define IUCV_DBF_TRACE_LEN 16 | |
92 | #define IUCV_DBF_TRACE_PAGES 4 | |
93 | #define IUCV_DBF_TRACE_NR_AREAS 1 | |
94 | #define IUCV_DBF_TRACE_LEVEL 3 | |
95 | ||
96 | #define IUCV_DBF_TEXT(name,level,text) \ | |
97 | do { \ | |
98 | debug_text_event(iucv_dbf_##name,level,text); \ | |
99 | } while (0) | |
100 | ||
101 | #define IUCV_DBF_HEX(name,level,addr,len) \ | |
102 | do { \ | |
103 | debug_event(iucv_dbf_##name,level,(void*)(addr),len); \ | |
104 | } while (0) | |
105 | ||
106 | DECLARE_PER_CPU(char[256], iucv_dbf_txt_buf); | |
107 | ||
f33780d3 PT |
108 | #define IUCV_DBF_TEXT_(name, level, text...) \ |
109 | do { \ | |
8e6a8285 | 110 | if (debug_level_enabled(iucv_dbf_##name, level)) { \ |
390dfd95 TH |
111 | char* __buf = get_cpu_var(iucv_dbf_txt_buf); \ |
112 | sprintf(__buf, text); \ | |
113 | debug_text_event(iucv_dbf_##name, level, __buf); \ | |
f33780d3 PT |
114 | put_cpu_var(iucv_dbf_txt_buf); \ |
115 | } \ | |
eebce385 MS |
116 | } while (0) |
117 | ||
118 | #define IUCV_DBF_SPRINTF(name,level,text...) \ | |
119 | do { \ | |
120 | debug_sprintf_event(iucv_dbf_trace, level, ##text ); \ | |
121 | debug_sprintf_event(iucv_dbf_trace, level, text ); \ | |
122 | } while (0) | |
123 | ||
124 | /** | |
125 | * some more debug stuff | |
126 | */ | |
1da177e4 LT |
127 | #define PRINTK_HEADER " iucv: " /* for debugging */ |
128 | ||
1175b257 UB |
129 | /* dummy device to make sure netiucv_pm functions are called */ |
130 | static struct device *netiucv_dev; | |
131 | ||
132 | static int netiucv_pm_prepare(struct device *); | |
133 | static void netiucv_pm_complete(struct device *); | |
134 | static int netiucv_pm_freeze(struct device *); | |
135 | static int netiucv_pm_restore_thaw(struct device *); | |
136 | ||
47145210 | 137 | static const struct dev_pm_ops netiucv_pm_ops = { |
1175b257 UB |
138 | .prepare = netiucv_pm_prepare, |
139 | .complete = netiucv_pm_complete, | |
140 | .freeze = netiucv_pm_freeze, | |
141 | .thaw = netiucv_pm_restore_thaw, | |
142 | .restore = netiucv_pm_restore_thaw, | |
143 | }; | |
144 | ||
1da177e4 | 145 | static struct device_driver netiucv_driver = { |
2219510f | 146 | .owner = THIS_MODULE, |
1da177e4 LT |
147 | .name = "netiucv", |
148 | .bus = &iucv_bus, | |
1175b257 | 149 | .pm = &netiucv_pm_ops, |
1da177e4 LT |
150 | }; |
151 | ||
91e60eb6 UB |
152 | static int netiucv_callback_connreq(struct iucv_path *, u8 *, u8 *); |
153 | static void netiucv_callback_connack(struct iucv_path *, u8 *); | |
154 | static void netiucv_callback_connrej(struct iucv_path *, u8 *); | |
155 | static void netiucv_callback_connsusp(struct iucv_path *, u8 *); | |
156 | static void netiucv_callback_connres(struct iucv_path *, u8 *); | |
eebce385 MS |
157 | static void netiucv_callback_rx(struct iucv_path *, struct iucv_message *); |
158 | static void netiucv_callback_txdone(struct iucv_path *, struct iucv_message *); | |
159 | ||
160 | static struct iucv_handler netiucv_handler = { | |
161 | .path_pending = netiucv_callback_connreq, | |
162 | .path_complete = netiucv_callback_connack, | |
163 | .path_severed = netiucv_callback_connrej, | |
164 | .path_quiesced = netiucv_callback_connsusp, | |
165 | .path_resumed = netiucv_callback_connres, | |
166 | .message_pending = netiucv_callback_rx, | |
167 | .message_complete = netiucv_callback_txdone | |
168 | }; | |
169 | ||
1da177e4 LT |
170 | /** |
171 | * Per connection profiling data | |
172 | */ | |
173 | struct connection_profile { | |
174 | unsigned long maxmulti; | |
175 | unsigned long maxcqueue; | |
176 | unsigned long doios_single; | |
177 | unsigned long doios_multi; | |
178 | unsigned long txlen; | |
179 | unsigned long tx_time; | |
ee6edb97 | 180 | unsigned long send_stamp; |
1da177e4 LT |
181 | unsigned long tx_pending; |
182 | unsigned long tx_max_pending; | |
183 | }; | |
184 | ||
185 | /** | |
186 | * Representation of one iucv connection | |
187 | */ | |
188 | struct iucv_connection { | |
eebce385 MS |
189 | struct list_head list; |
190 | struct iucv_path *path; | |
1da177e4 LT |
191 | struct sk_buff *rx_buff; |
192 | struct sk_buff *tx_buff; | |
193 | struct sk_buff_head collect_queue; | |
194 | struct sk_buff_head commit_queue; | |
195 | spinlock_t collect_lock; | |
196 | int collect_len; | |
197 | int max_buffsize; | |
198 | fsm_timer timer; | |
199 | fsm_instance *fsm; | |
200 | struct net_device *netdev; | |
201 | struct connection_profile prof; | |
202 | char userid[9]; | |
08e3356c | 203 | char userdata[17]; |
1da177e4 LT |
204 | }; |
205 | ||
206 | /** | |
207 | * Linked list of all connection structs. | |
208 | */ | |
c11ca97e | 209 | static LIST_HEAD(iucv_connection_list); |
bfac0d0b | 210 | static DEFINE_RWLOCK(iucv_connection_rwlock); |
1da177e4 LT |
211 | |
212 | /** | |
213 | * Representation of event-data for the | |
214 | * connection state machine. | |
215 | */ | |
216 | struct iucv_event { | |
217 | struct iucv_connection *conn; | |
218 | void *data; | |
219 | }; | |
220 | ||
221 | /** | |
222 | * Private part of the network device structure | |
223 | */ | |
224 | struct netiucv_priv { | |
225 | struct net_device_stats stats; | |
226 | unsigned long tbusy; | |
227 | fsm_instance *fsm; | |
228 | struct iucv_connection *conn; | |
229 | struct device *dev; | |
1175b257 | 230 | int pm_state; |
1da177e4 LT |
231 | }; |
232 | ||
233 | /** | |
234 | * Link level header for a packet. | |
235 | */ | |
eebce385 MS |
236 | struct ll_header { |
237 | u16 next; | |
238 | }; | |
1da177e4 | 239 | |
eebce385 | 240 | #define NETIUCV_HDRLEN (sizeof(struct ll_header)) |
08e3356c | 241 | #define NETIUCV_BUFSIZE_MAX 65537 |
1da177e4 LT |
242 | #define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX |
243 | #define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN) | |
244 | #define NETIUCV_MTU_DEFAULT 9216 | |
245 | #define NETIUCV_QUEUELEN_DEFAULT 50 | |
246 | #define NETIUCV_TIMEOUT_5SEC 5000 | |
247 | ||
248 | /** | |
249 | * Compatibility macros for busy handling | |
250 | * of network devices. | |
251 | */ | |
eebce385 | 252 | static inline void netiucv_clear_busy(struct net_device *dev) |
1da177e4 | 253 | { |
eebce385 MS |
254 | struct netiucv_priv *priv = netdev_priv(dev); |
255 | clear_bit(0, &priv->tbusy); | |
1da177e4 LT |
256 | netif_wake_queue(dev); |
257 | } | |
258 | ||
eebce385 | 259 | static inline int netiucv_test_and_set_busy(struct net_device *dev) |
1da177e4 | 260 | { |
eebce385 | 261 | struct netiucv_priv *priv = netdev_priv(dev); |
1da177e4 | 262 | netif_stop_queue(dev); |
eebce385 | 263 | return test_and_set_bit(0, &priv->tbusy); |
1da177e4 LT |
264 | } |
265 | ||
08e3356c UB |
266 | static u8 iucvMagic_ascii[16] = { |
267 | 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, | |
268 | 0x30, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 | |
269 | }; | |
270 | ||
271 | static u8 iucvMagic_ebcdic[16] = { | |
1da177e4 LT |
272 | 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, |
273 | 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 | |
274 | }; | |
275 | ||
1da177e4 LT |
276 | /** |
277 | * Convert an iucv userId to its printable | |
278 | * form (strip whitespace at end). | |
279 | * | |
280 | * @param An iucv userId | |
281 | * | |
282 | * @returns The printable string (static data!!) | |
283 | */ | |
08e3356c | 284 | static char *netiucv_printname(char *name, int len) |
1da177e4 | 285 | { |
08e3356c | 286 | static char tmp[17]; |
1da177e4 | 287 | char *p = tmp; |
08e3356c UB |
288 | memcpy(tmp, name, len); |
289 | tmp[len] = '\0'; | |
290 | while (*p && ((p - tmp) < len) && (!isspace(*p))) | |
1da177e4 LT |
291 | p++; |
292 | *p = '\0'; | |
293 | return tmp; | |
294 | } | |
e82b0f2c | 295 | |
08e3356c UB |
296 | static char *netiucv_printuser(struct iucv_connection *conn) |
297 | { | |
298 | static char tmp_uid[9]; | |
299 | static char tmp_udat[17]; | |
300 | static char buf[100]; | |
301 | ||
302 | if (memcmp(conn->userdata, iucvMagic_ebcdic, 16)) { | |
303 | tmp_uid[8] = '\0'; | |
304 | tmp_udat[16] = '\0'; | |
baac7898 | 305 | memcpy(tmp_uid, netiucv_printname(conn->userid, 8), 8); |
08e3356c UB |
306 | memcpy(tmp_udat, conn->userdata, 16); |
307 | EBCASC(tmp_udat, 16); | |
308 | memcpy(tmp_udat, netiucv_printname(tmp_udat, 16), 16); | |
309 | sprintf(buf, "%s.%s", tmp_uid, tmp_udat); | |
310 | return buf; | |
311 | } else | |
312 | return netiucv_printname(conn->userid, 8); | |
313 | } | |
314 | ||
1da177e4 LT |
315 | /** |
316 | * States of the interface statemachine. | |
317 | */ | |
318 | enum dev_states { | |
319 | DEV_STATE_STOPPED, | |
320 | DEV_STATE_STARTWAIT, | |
321 | DEV_STATE_STOPWAIT, | |
322 | DEV_STATE_RUNNING, | |
323 | /** | |
324 | * MUST be always the last element!! | |
325 | */ | |
326 | NR_DEV_STATES | |
327 | }; | |
328 | ||
329 | static const char *dev_state_names[] = { | |
330 | "Stopped", | |
331 | "StartWait", | |
332 | "StopWait", | |
333 | "Running", | |
334 | }; | |
335 | ||
336 | /** | |
337 | * Events of the interface statemachine. | |
338 | */ | |
339 | enum dev_events { | |
340 | DEV_EVENT_START, | |
341 | DEV_EVENT_STOP, | |
342 | DEV_EVENT_CONUP, | |
343 | DEV_EVENT_CONDOWN, | |
344 | /** | |
345 | * MUST be always the last element!! | |
346 | */ | |
347 | NR_DEV_EVENTS | |
348 | }; | |
349 | ||
350 | static const char *dev_event_names[] = { | |
351 | "Start", | |
352 | "Stop", | |
353 | "Connection up", | |
354 | "Connection down", | |
355 | }; | |
e82b0f2c | 356 | |
1da177e4 LT |
357 | /** |
358 | * Events of the connection statemachine | |
359 | */ | |
360 | enum conn_events { | |
361 | /** | |
362 | * Events, representing callbacks from | |
363 | * lowlevel iucv layer) | |
364 | */ | |
365 | CONN_EVENT_CONN_REQ, | |
366 | CONN_EVENT_CONN_ACK, | |
367 | CONN_EVENT_CONN_REJ, | |
368 | CONN_EVENT_CONN_SUS, | |
369 | CONN_EVENT_CONN_RES, | |
370 | CONN_EVENT_RX, | |
371 | CONN_EVENT_TXDONE, | |
372 | ||
373 | /** | |
374 | * Events, representing errors return codes from | |
375 | * calls to lowlevel iucv layer | |
376 | */ | |
377 | ||
378 | /** | |
379 | * Event, representing timer expiry. | |
380 | */ | |
381 | CONN_EVENT_TIMER, | |
382 | ||
383 | /** | |
384 | * Events, representing commands from upper levels. | |
385 | */ | |
386 | CONN_EVENT_START, | |
387 | CONN_EVENT_STOP, | |
388 | ||
389 | /** | |
390 | * MUST be always the last element!! | |
391 | */ | |
392 | NR_CONN_EVENTS, | |
393 | }; | |
394 | ||
395 | static const char *conn_event_names[] = { | |
396 | "Remote connection request", | |
397 | "Remote connection acknowledge", | |
398 | "Remote connection reject", | |
399 | "Connection suspended", | |
400 | "Connection resumed", | |
401 | "Data received", | |
402 | "Data sent", | |
403 | ||
404 | "Timer", | |
405 | ||
406 | "Start", | |
407 | "Stop", | |
408 | }; | |
409 | ||
410 | /** | |
411 | * States of the connection statemachine. | |
412 | */ | |
413 | enum conn_states { | |
414 | /** | |
415 | * Connection not assigned to any device, | |
416 | * initial state, invalid | |
417 | */ | |
418 | CONN_STATE_INVALID, | |
419 | ||
420 | /** | |
421 | * Userid assigned but not operating | |
422 | */ | |
423 | CONN_STATE_STOPPED, | |
424 | ||
425 | /** | |
426 | * Connection registered, | |
427 | * no connection request sent yet, | |
428 | * no connection request received | |
429 | */ | |
430 | CONN_STATE_STARTWAIT, | |
431 | ||
432 | /** | |
433 | * Connection registered and connection request sent, | |
434 | * no acknowledge and no connection request received yet. | |
435 | */ | |
436 | CONN_STATE_SETUPWAIT, | |
437 | ||
438 | /** | |
439 | * Connection up and running idle | |
440 | */ | |
441 | CONN_STATE_IDLE, | |
442 | ||
443 | /** | |
444 | * Data sent, awaiting CONN_EVENT_TXDONE | |
445 | */ | |
446 | CONN_STATE_TX, | |
447 | ||
448 | /** | |
449 | * Error during registration. | |
450 | */ | |
451 | CONN_STATE_REGERR, | |
452 | ||
453 | /** | |
454 | * Error during registration. | |
455 | */ | |
456 | CONN_STATE_CONNERR, | |
457 | ||
458 | /** | |
459 | * MUST be always the last element!! | |
460 | */ | |
461 | NR_CONN_STATES, | |
462 | }; | |
463 | ||
464 | static const char *conn_state_names[] = { | |
465 | "Invalid", | |
466 | "Stopped", | |
467 | "StartWait", | |
468 | "SetupWait", | |
469 | "Idle", | |
470 | "TX", | |
471 | "Terminating", | |
472 | "Registration error", | |
473 | "Connect error", | |
474 | }; | |
475 | ||
e82b0f2c | 476 | |
1da177e4 LT |
477 | /** |
478 | * Debug Facility Stuff | |
479 | */ | |
480 | static debug_info_t *iucv_dbf_setup = NULL; | |
481 | static debug_info_t *iucv_dbf_data = NULL; | |
482 | static debug_info_t *iucv_dbf_trace = NULL; | |
483 | ||
484 | DEFINE_PER_CPU(char[256], iucv_dbf_txt_buf); | |
485 | ||
eebce385 | 486 | static void iucv_unregister_dbf_views(void) |
1da177e4 | 487 | { |
b646c08e ME |
488 | debug_unregister(iucv_dbf_setup); |
489 | debug_unregister(iucv_dbf_data); | |
490 | debug_unregister(iucv_dbf_trace); | |
1da177e4 | 491 | } |
eebce385 | 492 | static int iucv_register_dbf_views(void) |
1da177e4 LT |
493 | { |
494 | iucv_dbf_setup = debug_register(IUCV_DBF_SETUP_NAME, | |
66a464db | 495 | IUCV_DBF_SETUP_PAGES, |
1da177e4 LT |
496 | IUCV_DBF_SETUP_NR_AREAS, |
497 | IUCV_DBF_SETUP_LEN); | |
498 | iucv_dbf_data = debug_register(IUCV_DBF_DATA_NAME, | |
66a464db | 499 | IUCV_DBF_DATA_PAGES, |
1da177e4 LT |
500 | IUCV_DBF_DATA_NR_AREAS, |
501 | IUCV_DBF_DATA_LEN); | |
502 | iucv_dbf_trace = debug_register(IUCV_DBF_TRACE_NAME, | |
66a464db | 503 | IUCV_DBF_TRACE_PAGES, |
1da177e4 LT |
504 | IUCV_DBF_TRACE_NR_AREAS, |
505 | IUCV_DBF_TRACE_LEN); | |
506 | ||
507 | if ((iucv_dbf_setup == NULL) || (iucv_dbf_data == NULL) || | |
508 | (iucv_dbf_trace == NULL)) { | |
509 | iucv_unregister_dbf_views(); | |
510 | return -ENOMEM; | |
511 | } | |
512 | debug_register_view(iucv_dbf_setup, &debug_hex_ascii_view); | |
513 | debug_set_level(iucv_dbf_setup, IUCV_DBF_SETUP_LEVEL); | |
514 | ||
515 | debug_register_view(iucv_dbf_data, &debug_hex_ascii_view); | |
516 | debug_set_level(iucv_dbf_data, IUCV_DBF_DATA_LEVEL); | |
517 | ||
518 | debug_register_view(iucv_dbf_trace, &debug_hex_ascii_view); | |
519 | debug_set_level(iucv_dbf_trace, IUCV_DBF_TRACE_LEVEL); | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
eebce385 | 524 | /* |
1da177e4 | 525 | * Callback-wrappers, called from lowlevel iucv layer. |
eebce385 | 526 | */ |
1da177e4 | 527 | |
eebce385 MS |
528 | static void netiucv_callback_rx(struct iucv_path *path, |
529 | struct iucv_message *msg) | |
1da177e4 | 530 | { |
eebce385 | 531 | struct iucv_connection *conn = path->private; |
1da177e4 LT |
532 | struct iucv_event ev; |
533 | ||
534 | ev.conn = conn; | |
eebce385 | 535 | ev.data = msg; |
1da177e4 LT |
536 | fsm_event(conn->fsm, CONN_EVENT_RX, &ev); |
537 | } | |
538 | ||
eebce385 MS |
539 | static void netiucv_callback_txdone(struct iucv_path *path, |
540 | struct iucv_message *msg) | |
1da177e4 | 541 | { |
eebce385 | 542 | struct iucv_connection *conn = path->private; |
1da177e4 LT |
543 | struct iucv_event ev; |
544 | ||
545 | ev.conn = conn; | |
eebce385 | 546 | ev.data = msg; |
1da177e4 LT |
547 | fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev); |
548 | } | |
549 | ||
eebce385 | 550 | static void netiucv_callback_connack(struct iucv_path *path, u8 ipuser[16]) |
1da177e4 | 551 | { |
eebce385 | 552 | struct iucv_connection *conn = path->private; |
1da177e4 | 553 | |
eebce385 | 554 | fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, conn); |
1da177e4 LT |
555 | } |
556 | ||
91e60eb6 UB |
557 | static int netiucv_callback_connreq(struct iucv_path *path, u8 *ipvmid, |
558 | u8 *ipuser) | |
1da177e4 | 559 | { |
eebce385 | 560 | struct iucv_connection *conn = path->private; |
1da177e4 | 561 | struct iucv_event ev; |
08e3356c UB |
562 | static char tmp_user[9]; |
563 | static char tmp_udat[17]; | |
eebce385 | 564 | int rc; |
1da177e4 | 565 | |
eebce385 | 566 | rc = -EINVAL; |
08e3356c UB |
567 | memcpy(tmp_user, netiucv_printname(ipvmid, 8), 8); |
568 | memcpy(tmp_udat, ipuser, 16); | |
569 | EBCASC(tmp_udat, 16); | |
eebce385 MS |
570 | read_lock_bh(&iucv_connection_rwlock); |
571 | list_for_each_entry(conn, &iucv_connection_list, list) { | |
08e3356c UB |
572 | if (strncmp(ipvmid, conn->userid, 8) || |
573 | strncmp(ipuser, conn->userdata, 16)) | |
eebce385 MS |
574 | continue; |
575 | /* Found a matching connection for this path. */ | |
576 | conn->path = path; | |
577 | ev.conn = conn; | |
578 | ev.data = path; | |
579 | fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev); | |
580 | rc = 0; | |
581 | } | |
08e3356c UB |
582 | IUCV_DBF_TEXT_(setup, 2, "Connection requested for %s.%s\n", |
583 | tmp_user, netiucv_printname(tmp_udat, 16)); | |
eebce385 MS |
584 | read_unlock_bh(&iucv_connection_rwlock); |
585 | return rc; | |
1da177e4 LT |
586 | } |
587 | ||
91e60eb6 | 588 | static void netiucv_callback_connrej(struct iucv_path *path, u8 *ipuser) |
1da177e4 | 589 | { |
eebce385 | 590 | struct iucv_connection *conn = path->private; |
1da177e4 | 591 | |
eebce385 | 592 | fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, conn); |
1da177e4 LT |
593 | } |
594 | ||
91e60eb6 | 595 | static void netiucv_callback_connsusp(struct iucv_path *path, u8 *ipuser) |
1da177e4 | 596 | { |
eebce385 | 597 | struct iucv_connection *conn = path->private; |
1da177e4 | 598 | |
eebce385 | 599 | fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, conn); |
1da177e4 LT |
600 | } |
601 | ||
91e60eb6 | 602 | static void netiucv_callback_connres(struct iucv_path *path, u8 *ipuser) |
1da177e4 | 603 | { |
eebce385 | 604 | struct iucv_connection *conn = path->private; |
1da177e4 | 605 | |
eebce385 MS |
606 | fsm_event(conn->fsm, CONN_EVENT_CONN_RES, conn); |
607 | } | |
1da177e4 LT |
608 | |
609 | /** | |
21b26f2f | 610 | * NOP action for statemachines |
1da177e4 | 611 | */ |
21b26f2f | 612 | static void netiucv_action_nop(fsm_instance *fi, int event, void *arg) |
1da177e4 LT |
613 | { |
614 | } | |
e82b0f2c | 615 | |
eebce385 | 616 | /* |
1da177e4 | 617 | * Actions of the connection statemachine |
eebce385 | 618 | */ |
1da177e4 LT |
619 | |
620 | /** | |
eebce385 MS |
621 | * netiucv_unpack_skb |
622 | * @conn: The connection where this skb has been received. | |
623 | * @pskb: The received skb. | |
1da177e4 | 624 | * |
eebce385 MS |
625 | * Unpack a just received skb and hand it over to upper layers. |
626 | * Helper function for conn_action_rx. | |
1da177e4 | 627 | */ |
eebce385 MS |
628 | static void netiucv_unpack_skb(struct iucv_connection *conn, |
629 | struct sk_buff *pskb) | |
1da177e4 LT |
630 | { |
631 | struct net_device *dev = conn->netdev; | |
eebce385 MS |
632 | struct netiucv_priv *privptr = netdev_priv(dev); |
633 | u16 offset = 0; | |
1da177e4 LT |
634 | |
635 | skb_put(pskb, NETIUCV_HDRLEN); | |
636 | pskb->dev = dev; | |
637 | pskb->ip_summed = CHECKSUM_NONE; | |
638 | pskb->protocol = ntohs(ETH_P_IP); | |
639 | ||
640 | while (1) { | |
641 | struct sk_buff *skb; | |
eebce385 | 642 | struct ll_header *header = (struct ll_header *) pskb->data; |
1da177e4 LT |
643 | |
644 | if (!header->next) | |
645 | break; | |
646 | ||
647 | skb_pull(pskb, NETIUCV_HDRLEN); | |
648 | header->next -= offset; | |
649 | offset += header->next; | |
650 | header->next -= NETIUCV_HDRLEN; | |
651 | if (skb_tailroom(pskb) < header->next) { | |
1da177e4 LT |
652 | IUCV_DBF_TEXT_(data, 2, "Illegal next field: %d > %d\n", |
653 | header->next, skb_tailroom(pskb)); | |
654 | return; | |
655 | } | |
656 | skb_put(pskb, header->next); | |
459a98ed | 657 | skb_reset_mac_header(pskb); |
1da177e4 LT |
658 | skb = dev_alloc_skb(pskb->len); |
659 | if (!skb) { | |
1da177e4 LT |
660 | IUCV_DBF_TEXT(data, 2, |
661 | "Out of memory in netiucv_unpack_skb\n"); | |
662 | privptr->stats.rx_dropped++; | |
663 | return; | |
664 | } | |
d626f62b ACM |
665 | skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len), |
666 | pskb->len); | |
459a98ed | 667 | skb_reset_mac_header(skb); |
1da177e4 LT |
668 | skb->dev = pskb->dev; |
669 | skb->protocol = pskb->protocol; | |
670 | pskb->ip_summed = CHECKSUM_UNNECESSARY; | |
9b3efc01 JL |
671 | privptr->stats.rx_packets++; |
672 | privptr->stats.rx_bytes += skb->len; | |
1da177e4 LT |
673 | /* |
674 | * Since receiving is always initiated from a tasklet (in iucv.c), | |
675 | * we must use netif_rx_ni() instead of netif_rx() | |
676 | */ | |
677 | netif_rx_ni(skb); | |
1da177e4 LT |
678 | skb_pull(pskb, header->next); |
679 | skb_put(pskb, NETIUCV_HDRLEN); | |
680 | } | |
681 | } | |
682 | ||
eebce385 | 683 | static void conn_action_rx(fsm_instance *fi, int event, void *arg) |
1da177e4 | 684 | { |
eebce385 | 685 | struct iucv_event *ev = arg; |
1da177e4 | 686 | struct iucv_connection *conn = ev->conn; |
eebce385 MS |
687 | struct iucv_message *msg = ev->data; |
688 | struct netiucv_priv *privptr = netdev_priv(conn->netdev); | |
1da177e4 LT |
689 | int rc; |
690 | ||
2a2cf6b1 | 691 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
692 | |
693 | if (!conn->netdev) { | |
eebce385 | 694 | iucv_message_reject(conn->path, msg); |
1da177e4 | 695 | IUCV_DBF_TEXT(data, 2, |
eebce385 | 696 | "Received data for unlinked connection\n"); |
1da177e4 LT |
697 | return; |
698 | } | |
eebce385 MS |
699 | if (msg->length > conn->max_buffsize) { |
700 | iucv_message_reject(conn->path, msg); | |
1da177e4 | 701 | privptr->stats.rx_dropped++; |
1da177e4 | 702 | IUCV_DBF_TEXT_(data, 2, "msglen %d > max_buffsize %d\n", |
eebce385 | 703 | msg->length, conn->max_buffsize); |
1da177e4 LT |
704 | return; |
705 | } | |
27a884dc ACM |
706 | conn->rx_buff->data = conn->rx_buff->head; |
707 | skb_reset_tail_pointer(conn->rx_buff); | |
1da177e4 | 708 | conn->rx_buff->len = 0; |
eebce385 MS |
709 | rc = iucv_message_receive(conn->path, msg, 0, conn->rx_buff->data, |
710 | msg->length, NULL); | |
711 | if (rc || msg->length < 5) { | |
1da177e4 | 712 | privptr->stats.rx_errors++; |
1da177e4 LT |
713 | IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_receive\n", rc); |
714 | return; | |
715 | } | |
716 | netiucv_unpack_skb(conn, conn->rx_buff); | |
717 | } | |
718 | ||
eebce385 | 719 | static void conn_action_txdone(fsm_instance *fi, int event, void *arg) |
1da177e4 | 720 | { |
eebce385 | 721 | struct iucv_event *ev = arg; |
1da177e4 | 722 | struct iucv_connection *conn = ev->conn; |
eebce385 MS |
723 | struct iucv_message *msg = ev->data; |
724 | struct iucv_message txmsg; | |
1da177e4 | 725 | struct netiucv_priv *privptr = NULL; |
eebce385 MS |
726 | u32 single_flag = msg->tag; |
727 | u32 txbytes = 0; | |
728 | u32 txpackets = 0; | |
729 | u32 stat_maxcq = 0; | |
1da177e4 LT |
730 | struct sk_buff *skb; |
731 | unsigned long saveflags; | |
eebce385 MS |
732 | struct ll_header header; |
733 | int rc; | |
1da177e4 | 734 | |
2a2cf6b1 | 735 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 | 736 | |
d239ae33 UB |
737 | if (!conn || !conn->netdev) { |
738 | IUCV_DBF_TEXT(data, 2, | |
739 | "Send confirmation for unlinked connection\n"); | |
740 | return; | |
741 | } | |
742 | privptr = netdev_priv(conn->netdev); | |
1da177e4 LT |
743 | conn->prof.tx_pending--; |
744 | if (single_flag) { | |
745 | if ((skb = skb_dequeue(&conn->commit_queue))) { | |
746 | atomic_dec(&skb->users); | |
1da177e4 LT |
747 | if (privptr) { |
748 | privptr->stats.tx_packets++; | |
749 | privptr->stats.tx_bytes += | |
750 | (skb->len - NETIUCV_HDRLEN | |
998221c2 | 751 | - NETIUCV_HDRLEN); |
1da177e4 | 752 | } |
998221c2 | 753 | dev_kfree_skb_any(skb); |
1da177e4 LT |
754 | } |
755 | } | |
27a884dc ACM |
756 | conn->tx_buff->data = conn->tx_buff->head; |
757 | skb_reset_tail_pointer(conn->tx_buff); | |
1da177e4 LT |
758 | conn->tx_buff->len = 0; |
759 | spin_lock_irqsave(&conn->collect_lock, saveflags); | |
760 | while ((skb = skb_dequeue(&conn->collect_queue))) { | |
761 | header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN; | |
762 | memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header, | |
763 | NETIUCV_HDRLEN); | |
d626f62b ACM |
764 | skb_copy_from_linear_data(skb, |
765 | skb_put(conn->tx_buff, skb->len), | |
766 | skb->len); | |
1da177e4 LT |
767 | txbytes += skb->len; |
768 | txpackets++; | |
769 | stat_maxcq++; | |
770 | atomic_dec(&skb->users); | |
771 | dev_kfree_skb_any(skb); | |
772 | } | |
773 | if (conn->collect_len > conn->prof.maxmulti) | |
774 | conn->prof.maxmulti = conn->collect_len; | |
775 | conn->collect_len = 0; | |
776 | spin_unlock_irqrestore(&conn->collect_lock, saveflags); | |
eebce385 MS |
777 | if (conn->tx_buff->len == 0) { |
778 | fsm_newstate(fi, CONN_STATE_IDLE); | |
779 | return; | |
780 | } | |
1da177e4 | 781 | |
eebce385 MS |
782 | header.next = 0; |
783 | memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN); | |
ee6edb97 | 784 | conn->prof.send_stamp = jiffies; |
eebce385 MS |
785 | txmsg.class = 0; |
786 | txmsg.tag = 0; | |
787 | rc = iucv_message_send(conn->path, &txmsg, 0, 0, | |
1da177e4 | 788 | conn->tx_buff->data, conn->tx_buff->len); |
eebce385 MS |
789 | conn->prof.doios_multi++; |
790 | conn->prof.txlen += conn->tx_buff->len; | |
791 | conn->prof.tx_pending++; | |
792 | if (conn->prof.tx_pending > conn->prof.tx_max_pending) | |
793 | conn->prof.tx_max_pending = conn->prof.tx_pending; | |
794 | if (rc) { | |
795 | conn->prof.tx_pending--; | |
1da177e4 | 796 | fsm_newstate(fi, CONN_STATE_IDLE); |
eebce385 MS |
797 | if (privptr) |
798 | privptr->stats.tx_errors += txpackets; | |
eebce385 MS |
799 | IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc); |
800 | } else { | |
801 | if (privptr) { | |
802 | privptr->stats.tx_packets += txpackets; | |
803 | privptr->stats.tx_bytes += txbytes; | |
804 | } | |
805 | if (stat_maxcq > conn->prof.maxcqueue) | |
806 | conn->prof.maxcqueue = stat_maxcq; | |
807 | } | |
1da177e4 LT |
808 | } |
809 | ||
eebce385 | 810 | static void conn_action_connaccept(fsm_instance *fi, int event, void *arg) |
1da177e4 | 811 | { |
eebce385 | 812 | struct iucv_event *ev = arg; |
1da177e4 | 813 | struct iucv_connection *conn = ev->conn; |
eebce385 | 814 | struct iucv_path *path = ev->data; |
1da177e4 | 815 | struct net_device *netdev = conn->netdev; |
eebce385 | 816 | struct netiucv_priv *privptr = netdev_priv(netdev); |
1da177e4 | 817 | int rc; |
1da177e4 | 818 | |
2a2cf6b1 | 819 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 | 820 | |
eebce385 MS |
821 | conn->path = path; |
822 | path->msglim = NETIUCV_QUEUELEN_DEFAULT; | |
823 | path->flags = 0; | |
08e3356c | 824 | rc = iucv_path_accept(path, &netiucv_handler, conn->userdata , conn); |
1da177e4 | 825 | if (rc) { |
1da177e4 LT |
826 | IUCV_DBF_TEXT_(setup, 2, "rc %d from iucv_accept", rc); |
827 | return; | |
828 | } | |
829 | fsm_newstate(fi, CONN_STATE_IDLE); | |
eebce385 | 830 | netdev->tx_queue_len = conn->path->msglim; |
1da177e4 LT |
831 | fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev); |
832 | } | |
833 | ||
eebce385 | 834 | static void conn_action_connreject(fsm_instance *fi, int event, void *arg) |
1da177e4 | 835 | { |
eebce385 MS |
836 | struct iucv_event *ev = arg; |
837 | struct iucv_path *path = ev->data; | |
1da177e4 | 838 | |
2a2cf6b1 | 839 | IUCV_DBF_TEXT(trace, 3, __func__); |
eebce385 | 840 | iucv_path_sever(path, NULL); |
1da177e4 LT |
841 | } |
842 | ||
eebce385 | 843 | static void conn_action_connack(fsm_instance *fi, int event, void *arg) |
1da177e4 | 844 | { |
eebce385 | 845 | struct iucv_connection *conn = arg; |
1da177e4 | 846 | struct net_device *netdev = conn->netdev; |
eebce385 | 847 | struct netiucv_priv *privptr = netdev_priv(netdev); |
1da177e4 | 848 | |
2a2cf6b1 | 849 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
850 | fsm_deltimer(&conn->timer); |
851 | fsm_newstate(fi, CONN_STATE_IDLE); | |
eebce385 | 852 | netdev->tx_queue_len = conn->path->msglim; |
1da177e4 LT |
853 | fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev); |
854 | } | |
855 | ||
eebce385 | 856 | static void conn_action_conntimsev(fsm_instance *fi, int event, void *arg) |
1da177e4 | 857 | { |
eebce385 | 858 | struct iucv_connection *conn = arg; |
1da177e4 | 859 | |
2a2cf6b1 | 860 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 | 861 | fsm_deltimer(&conn->timer); |
08e3356c | 862 | iucv_path_sever(conn->path, conn->userdata); |
1da177e4 LT |
863 | fsm_newstate(fi, CONN_STATE_STARTWAIT); |
864 | } | |
865 | ||
eebce385 | 866 | static void conn_action_connsever(fsm_instance *fi, int event, void *arg) |
1da177e4 | 867 | { |
eebce385 | 868 | struct iucv_connection *conn = arg; |
1da177e4 | 869 | struct net_device *netdev = conn->netdev; |
eebce385 | 870 | struct netiucv_priv *privptr = netdev_priv(netdev); |
1da177e4 | 871 | |
2a2cf6b1 | 872 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
873 | |
874 | fsm_deltimer(&conn->timer); | |
08e3356c UB |
875 | iucv_path_sever(conn->path, conn->userdata); |
876 | dev_info(privptr->dev, "The peer z/VM guest %s has closed the " | |
877 | "connection\n", netiucv_printuser(conn)); | |
1da177e4 | 878 | IUCV_DBF_TEXT(data, 2, |
eebce385 | 879 | "conn_action_connsever: Remote dropped connection\n"); |
1da177e4 LT |
880 | fsm_newstate(fi, CONN_STATE_STARTWAIT); |
881 | fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev); | |
882 | } | |
883 | ||
eebce385 | 884 | static void conn_action_start(fsm_instance *fi, int event, void *arg) |
1da177e4 | 885 | { |
eebce385 | 886 | struct iucv_connection *conn = arg; |
8f7c502c UB |
887 | struct net_device *netdev = conn->netdev; |
888 | struct netiucv_priv *privptr = netdev_priv(netdev); | |
1da177e4 LT |
889 | int rc; |
890 | ||
2a2cf6b1 | 891 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 | 892 | |
eebce385 | 893 | fsm_newstate(fi, CONN_STATE_STARTWAIT); |
1da177e4 | 894 | |
eebce385 MS |
895 | /* |
896 | * We must set the state before calling iucv_connect because the | |
897 | * callback handler could be called at any point after the connection | |
898 | * request is sent | |
899 | */ | |
1da177e4 LT |
900 | |
901 | fsm_newstate(fi, CONN_STATE_SETUPWAIT); | |
eebce385 | 902 | conn->path = iucv_path_alloc(NETIUCV_QUEUELEN_DEFAULT, 0, GFP_KERNEL); |
08e3356c UB |
903 | IUCV_DBF_TEXT_(setup, 2, "%s: connecting to %s ...\n", |
904 | netdev->name, netiucv_printuser(conn)); | |
905 | ||
eebce385 | 906 | rc = iucv_path_connect(conn->path, &netiucv_handler, conn->userid, |
08e3356c | 907 | NULL, conn->userdata, conn); |
1da177e4 | 908 | switch (rc) { |
eebce385 | 909 | case 0: |
8f7c502c | 910 | netdev->tx_queue_len = conn->path->msglim; |
eebce385 MS |
911 | fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC, |
912 | CONN_EVENT_TIMER, conn); | |
913 | return; | |
914 | case 11: | |
8f7c502c UB |
915 | dev_warn(privptr->dev, |
916 | "The IUCV device failed to connect to z/VM guest %s\n", | |
08e3356c | 917 | netiucv_printname(conn->userid, 8)); |
eebce385 MS |
918 | fsm_newstate(fi, CONN_STATE_STARTWAIT); |
919 | break; | |
920 | case 12: | |
8f7c502c UB |
921 | dev_warn(privptr->dev, |
922 | "The IUCV device failed to connect to the peer on z/VM" | |
08e3356c | 923 | " guest %s\n", netiucv_printname(conn->userid, 8)); |
eebce385 MS |
924 | fsm_newstate(fi, CONN_STATE_STARTWAIT); |
925 | break; | |
926 | case 13: | |
8f7c502c UB |
927 | dev_err(privptr->dev, |
928 | "Connecting the IUCV device would exceed the maximum" | |
929 | " number of IUCV connections\n"); | |
eebce385 MS |
930 | fsm_newstate(fi, CONN_STATE_CONNERR); |
931 | break; | |
932 | case 14: | |
8f7c502c UB |
933 | dev_err(privptr->dev, |
934 | "z/VM guest %s has too many IUCV connections" | |
935 | " to connect with the IUCV device\n", | |
08e3356c | 936 | netiucv_printname(conn->userid, 8)); |
eebce385 MS |
937 | fsm_newstate(fi, CONN_STATE_CONNERR); |
938 | break; | |
939 | case 15: | |
8f7c502c UB |
940 | dev_err(privptr->dev, |
941 | "The IUCV device cannot connect to a z/VM guest with no" | |
942 | " IUCV authorization\n"); | |
eebce385 MS |
943 | fsm_newstate(fi, CONN_STATE_CONNERR); |
944 | break; | |
945 | default: | |
8f7c502c UB |
946 | dev_err(privptr->dev, |
947 | "Connecting the IUCV device failed with error %d\n", | |
948 | rc); | |
eebce385 MS |
949 | fsm_newstate(fi, CONN_STATE_CONNERR); |
950 | break; | |
1da177e4 LT |
951 | } |
952 | IUCV_DBF_TEXT_(setup, 5, "iucv_connect rc is %d\n", rc); | |
eebce385 MS |
953 | kfree(conn->path); |
954 | conn->path = NULL; | |
1da177e4 LT |
955 | } |
956 | ||
eebce385 | 957 | static void netiucv_purge_skb_queue(struct sk_buff_head *q) |
1da177e4 LT |
958 | { |
959 | struct sk_buff *skb; | |
960 | ||
961 | while ((skb = skb_dequeue(q))) { | |
962 | atomic_dec(&skb->users); | |
963 | dev_kfree_skb_any(skb); | |
964 | } | |
965 | } | |
966 | ||
eebce385 | 967 | static void conn_action_stop(fsm_instance *fi, int event, void *arg) |
1da177e4 | 968 | { |
eebce385 | 969 | struct iucv_event *ev = arg; |
1da177e4 LT |
970 | struct iucv_connection *conn = ev->conn; |
971 | struct net_device *netdev = conn->netdev; | |
eebce385 | 972 | struct netiucv_priv *privptr = netdev_priv(netdev); |
1da177e4 | 973 | |
2a2cf6b1 | 974 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
975 | |
976 | fsm_deltimer(&conn->timer); | |
977 | fsm_newstate(fi, CONN_STATE_STOPPED); | |
978 | netiucv_purge_skb_queue(&conn->collect_queue); | |
eebce385 MS |
979 | if (conn->path) { |
980 | IUCV_DBF_TEXT(trace, 5, "calling iucv_path_sever\n"); | |
08e3356c | 981 | iucv_path_sever(conn->path, conn->userdata); |
eebce385 MS |
982 | kfree(conn->path); |
983 | conn->path = NULL; | |
984 | } | |
1da177e4 LT |
985 | netiucv_purge_skb_queue(&conn->commit_queue); |
986 | fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev); | |
987 | } | |
988 | ||
eebce385 | 989 | static void conn_action_inval(fsm_instance *fi, int event, void *arg) |
1da177e4 | 990 | { |
eebce385 | 991 | struct iucv_connection *conn = arg; |
1da177e4 LT |
992 | struct net_device *netdev = conn->netdev; |
993 | ||
f082bcae UB |
994 | IUCV_DBF_TEXT_(data, 2, "%s('%s'): conn_action_inval called\n", |
995 | netdev->name, conn->userid); | |
1da177e4 LT |
996 | } |
997 | ||
998 | static const fsm_node conn_fsm[] = { | |
999 | { CONN_STATE_INVALID, CONN_EVENT_START, conn_action_inval }, | |
1000 | { CONN_STATE_STOPPED, CONN_EVENT_START, conn_action_start }, | |
1001 | ||
1002 | { CONN_STATE_STOPPED, CONN_EVENT_STOP, conn_action_stop }, | |
1003 | { CONN_STATE_STARTWAIT, CONN_EVENT_STOP, conn_action_stop }, | |
1004 | { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP, conn_action_stop }, | |
1005 | { CONN_STATE_IDLE, CONN_EVENT_STOP, conn_action_stop }, | |
1006 | { CONN_STATE_TX, CONN_EVENT_STOP, conn_action_stop }, | |
1007 | { CONN_STATE_REGERR, CONN_EVENT_STOP, conn_action_stop }, | |
1008 | { CONN_STATE_CONNERR, CONN_EVENT_STOP, conn_action_stop }, | |
1009 | ||
1010 | { CONN_STATE_STOPPED, CONN_EVENT_CONN_REQ, conn_action_connreject }, | |
1011 | { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept }, | |
1012 | { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept }, | |
1013 | { CONN_STATE_IDLE, CONN_EVENT_CONN_REQ, conn_action_connreject }, | |
1014 | { CONN_STATE_TX, CONN_EVENT_CONN_REQ, conn_action_connreject }, | |
1015 | ||
1016 | { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack }, | |
1017 | { CONN_STATE_SETUPWAIT, CONN_EVENT_TIMER, conn_action_conntimsev }, | |
1018 | ||
1019 | { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever }, | |
1020 | { CONN_STATE_IDLE, CONN_EVENT_CONN_REJ, conn_action_connsever }, | |
1021 | { CONN_STATE_TX, CONN_EVENT_CONN_REJ, conn_action_connsever }, | |
1022 | ||
1023 | { CONN_STATE_IDLE, CONN_EVENT_RX, conn_action_rx }, | |
1024 | { CONN_STATE_TX, CONN_EVENT_RX, conn_action_rx }, | |
1025 | ||
1026 | { CONN_STATE_TX, CONN_EVENT_TXDONE, conn_action_txdone }, | |
1027 | { CONN_STATE_IDLE, CONN_EVENT_TXDONE, conn_action_txdone }, | |
1028 | }; | |
1029 | ||
1030 | static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node); | |
1031 | ||
e82b0f2c | 1032 | |
eebce385 | 1033 | /* |
1da177e4 | 1034 | * Actions for interface - statemachine. |
eebce385 | 1035 | */ |
1da177e4 LT |
1036 | |
1037 | /** | |
eebce385 MS |
1038 | * dev_action_start |
1039 | * @fi: An instance of an interface statemachine. | |
1040 | * @event: The event, just happened. | |
1041 | * @arg: Generic pointer, casted from struct net_device * upon call. | |
1da177e4 | 1042 | * |
eebce385 | 1043 | * Startup connection by sending CONN_EVENT_START to it. |
1da177e4 | 1044 | */ |
eebce385 | 1045 | static void dev_action_start(fsm_instance *fi, int event, void *arg) |
1da177e4 | 1046 | { |
eebce385 MS |
1047 | struct net_device *dev = arg; |
1048 | struct netiucv_priv *privptr = netdev_priv(dev); | |
1da177e4 | 1049 | |
2a2cf6b1 | 1050 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 | 1051 | |
1da177e4 | 1052 | fsm_newstate(fi, DEV_STATE_STARTWAIT); |
eebce385 | 1053 | fsm_event(privptr->conn->fsm, CONN_EVENT_START, privptr->conn); |
1da177e4 LT |
1054 | } |
1055 | ||
1056 | /** | |
1057 | * Shutdown connection by sending CONN_EVENT_STOP to it. | |
1058 | * | |
1059 | * @param fi An instance of an interface statemachine. | |
1060 | * @param event The event, just happened. | |
1061 | * @param arg Generic pointer, casted from struct net_device * upon call. | |
1062 | */ | |
1063 | static void | |
1064 | dev_action_stop(fsm_instance *fi, int event, void *arg) | |
1065 | { | |
eebce385 MS |
1066 | struct net_device *dev = arg; |
1067 | struct netiucv_priv *privptr = netdev_priv(dev); | |
1da177e4 LT |
1068 | struct iucv_event ev; |
1069 | ||
2a2cf6b1 | 1070 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
1071 | |
1072 | ev.conn = privptr->conn; | |
1073 | ||
1074 | fsm_newstate(fi, DEV_STATE_STOPWAIT); | |
1075 | fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev); | |
1076 | } | |
1077 | ||
1078 | /** | |
1079 | * Called from connection statemachine | |
1080 | * when a connection is up and running. | |
1081 | * | |
1082 | * @param fi An instance of an interface statemachine. | |
1083 | * @param event The event, just happened. | |
1084 | * @param arg Generic pointer, casted from struct net_device * upon call. | |
1085 | */ | |
1086 | static void | |
1087 | dev_action_connup(fsm_instance *fi, int event, void *arg) | |
1088 | { | |
eebce385 MS |
1089 | struct net_device *dev = arg; |
1090 | struct netiucv_priv *privptr = netdev_priv(dev); | |
1da177e4 | 1091 | |
2a2cf6b1 | 1092 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
1093 | |
1094 | switch (fsm_getstate(fi)) { | |
1095 | case DEV_STATE_STARTWAIT: | |
1096 | fsm_newstate(fi, DEV_STATE_RUNNING); | |
8f7c502c UB |
1097 | dev_info(privptr->dev, |
1098 | "The IUCV device has been connected" | |
08e3356c UB |
1099 | " successfully to %s\n", |
1100 | netiucv_printuser(privptr->conn)); | |
1da177e4 LT |
1101 | IUCV_DBF_TEXT(setup, 3, |
1102 | "connection is up and running\n"); | |
1103 | break; | |
1104 | case DEV_STATE_STOPWAIT: | |
1da177e4 LT |
1105 | IUCV_DBF_TEXT(data, 2, |
1106 | "dev_action_connup: in DEV_STATE_STOPWAIT\n"); | |
1107 | break; | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * Called from connection statemachine | |
1113 | * when a connection has been shutdown. | |
1114 | * | |
1115 | * @param fi An instance of an interface statemachine. | |
1116 | * @param event The event, just happened. | |
1117 | * @param arg Generic pointer, casted from struct net_device * upon call. | |
1118 | */ | |
1119 | static void | |
1120 | dev_action_conndown(fsm_instance *fi, int event, void *arg) | |
1121 | { | |
2a2cf6b1 | 1122 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
1123 | |
1124 | switch (fsm_getstate(fi)) { | |
1125 | case DEV_STATE_RUNNING: | |
1126 | fsm_newstate(fi, DEV_STATE_STARTWAIT); | |
1127 | break; | |
1128 | case DEV_STATE_STOPWAIT: | |
1129 | fsm_newstate(fi, DEV_STATE_STOPPED); | |
1130 | IUCV_DBF_TEXT(setup, 3, "connection is down\n"); | |
1131 | break; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | static const fsm_node dev_fsm[] = { | |
1136 | { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start }, | |
1137 | ||
1138 | { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start }, | |
1139 | { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown }, | |
1140 | ||
1141 | { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop }, | |
1142 | { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup }, | |
1143 | ||
1144 | { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop }, | |
1145 | { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown }, | |
21b26f2f | 1146 | { DEV_STATE_RUNNING, DEV_EVENT_CONUP, netiucv_action_nop }, |
1da177e4 LT |
1147 | }; |
1148 | ||
1149 | static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node); | |
1150 | ||
1151 | /** | |
1152 | * Transmit a packet. | |
1153 | * This is a helper function for netiucv_tx(). | |
1154 | * | |
1155 | * @param conn Connection to be used for sending. | |
1156 | * @param skb Pointer to struct sk_buff of packet to send. | |
1157 | * The linklevel header has already been set up | |
1158 | * by netiucv_tx(). | |
1159 | * | |
1160 | * @return 0 on success, -ERRNO on failure. (Never fails.) | |
1161 | */ | |
eebce385 MS |
1162 | static int netiucv_transmit_skb(struct iucv_connection *conn, |
1163 | struct sk_buff *skb) | |
1164 | { | |
1165 | struct iucv_message msg; | |
1da177e4 | 1166 | unsigned long saveflags; |
eebce385 MS |
1167 | struct ll_header header; |
1168 | int rc; | |
1da177e4 LT |
1169 | |
1170 | if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) { | |
1171 | int l = skb->len + NETIUCV_HDRLEN; | |
1172 | ||
1173 | spin_lock_irqsave(&conn->collect_lock, saveflags); | |
1174 | if (conn->collect_len + l > | |
1175 | (conn->max_buffsize - NETIUCV_HDRLEN)) { | |
1176 | rc = -EBUSY; | |
1177 | IUCV_DBF_TEXT(data, 2, | |
eebce385 | 1178 | "EBUSY from netiucv_transmit_skb\n"); |
1da177e4 LT |
1179 | } else { |
1180 | atomic_inc(&skb->users); | |
1181 | skb_queue_tail(&conn->collect_queue, skb); | |
1182 | conn->collect_len += l; | |
eebce385 | 1183 | rc = 0; |
1da177e4 LT |
1184 | } |
1185 | spin_unlock_irqrestore(&conn->collect_lock, saveflags); | |
1186 | } else { | |
1187 | struct sk_buff *nskb = skb; | |
1188 | /** | |
1189 | * Copy the skb to a new allocated skb in lowmem only if the | |
1190 | * data is located above 2G in memory or tailroom is < 2. | |
1191 | */ | |
27a884dc ACM |
1192 | unsigned long hi = ((unsigned long)(skb_tail_pointer(skb) + |
1193 | NETIUCV_HDRLEN)) >> 31; | |
1da177e4 LT |
1194 | int copied = 0; |
1195 | if (hi || (skb_tailroom(skb) < 2)) { | |
1196 | nskb = alloc_skb(skb->len + NETIUCV_HDRLEN + | |
1197 | NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA); | |
1198 | if (!nskb) { | |
1da177e4 LT |
1199 | IUCV_DBF_TEXT(data, 2, "alloc_skb failed\n"); |
1200 | rc = -ENOMEM; | |
1201 | return rc; | |
1202 | } else { | |
1203 | skb_reserve(nskb, NETIUCV_HDRLEN); | |
1204 | memcpy(skb_put(nskb, skb->len), | |
1205 | skb->data, skb->len); | |
1206 | } | |
1207 | copied = 1; | |
1208 | } | |
1209 | /** | |
1210 | * skb now is below 2G and has enough room. Add headers. | |
1211 | */ | |
1212 | header.next = nskb->len + NETIUCV_HDRLEN; | |
1213 | memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN); | |
1214 | header.next = 0; | |
1215 | memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN); | |
1216 | ||
1217 | fsm_newstate(conn->fsm, CONN_STATE_TX); | |
ee6edb97 | 1218 | conn->prof.send_stamp = jiffies; |
e82b0f2c | 1219 | |
eebce385 MS |
1220 | msg.tag = 1; |
1221 | msg.class = 0; | |
1222 | rc = iucv_message_send(conn->path, &msg, 0, 0, | |
1223 | nskb->data, nskb->len); | |
1da177e4 LT |
1224 | conn->prof.doios_single++; |
1225 | conn->prof.txlen += skb->len; | |
1226 | conn->prof.tx_pending++; | |
1227 | if (conn->prof.tx_pending > conn->prof.tx_max_pending) | |
1228 | conn->prof.tx_max_pending = conn->prof.tx_pending; | |
1229 | if (rc) { | |
1230 | struct netiucv_priv *privptr; | |
1231 | fsm_newstate(conn->fsm, CONN_STATE_IDLE); | |
1232 | conn->prof.tx_pending--; | |
eebce385 | 1233 | privptr = netdev_priv(conn->netdev); |
1da177e4 LT |
1234 | if (privptr) |
1235 | privptr->stats.tx_errors++; | |
1236 | if (copied) | |
1237 | dev_kfree_skb(nskb); | |
1238 | else { | |
1239 | /** | |
1240 | * Remove our headers. They get added | |
1241 | * again on retransmit. | |
1242 | */ | |
1243 | skb_pull(skb, NETIUCV_HDRLEN); | |
1244 | skb_trim(skb, skb->len - NETIUCV_HDRLEN); | |
1245 | } | |
1da177e4 LT |
1246 | IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc); |
1247 | } else { | |
1248 | if (copied) | |
1249 | dev_kfree_skb(skb); | |
1250 | atomic_inc(&nskb->users); | |
1251 | skb_queue_tail(&conn->commit_queue, nskb); | |
1252 | } | |
1253 | } | |
1254 | ||
1255 | return rc; | |
1256 | } | |
e82b0f2c | 1257 | |
eebce385 | 1258 | /* |
1da177e4 | 1259 | * Interface API for upper network layers |
eebce385 | 1260 | */ |
1da177e4 LT |
1261 | |
1262 | /** | |
1263 | * Open an interface. | |
1264 | * Called from generic network layer when ifconfig up is run. | |
1265 | * | |
1266 | * @param dev Pointer to interface struct. | |
1267 | * | |
1268 | * @return 0 on success, -ERRNO on failure. (Never fails.) | |
1269 | */ | |
eebce385 MS |
1270 | static int netiucv_open(struct net_device *dev) |
1271 | { | |
1272 | struct netiucv_priv *priv = netdev_priv(dev); | |
1273 | ||
1274 | fsm_event(priv->fsm, DEV_EVENT_START, dev); | |
1da177e4 LT |
1275 | return 0; |
1276 | } | |
1277 | ||
1278 | /** | |
1279 | * Close an interface. | |
1280 | * Called from generic network layer when ifconfig down is run. | |
1281 | * | |
1282 | * @param dev Pointer to interface struct. | |
1283 | * | |
1284 | * @return 0 on success, -ERRNO on failure. (Never fails.) | |
1285 | */ | |
eebce385 MS |
1286 | static int netiucv_close(struct net_device *dev) |
1287 | { | |
1288 | struct netiucv_priv *priv = netdev_priv(dev); | |
1289 | ||
1290 | fsm_event(priv->fsm, DEV_EVENT_STOP, dev); | |
1da177e4 LT |
1291 | return 0; |
1292 | } | |
1293 | ||
1175b257 UB |
1294 | static int netiucv_pm_prepare(struct device *dev) |
1295 | { | |
1296 | IUCV_DBF_TEXT(trace, 3, __func__); | |
1297 | return 0; | |
1298 | } | |
1299 | ||
1300 | static void netiucv_pm_complete(struct device *dev) | |
1301 | { | |
1302 | IUCV_DBF_TEXT(trace, 3, __func__); | |
1303 | return; | |
1304 | } | |
1305 | ||
1306 | /** | |
1307 | * netiucv_pm_freeze() - Freeze PM callback | |
1308 | * @dev: netiucv device | |
1309 | * | |
1310 | * close open netiucv interfaces | |
1311 | */ | |
1312 | static int netiucv_pm_freeze(struct device *dev) | |
1313 | { | |
4f0076f7 | 1314 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1175b257 UB |
1315 | struct net_device *ndev = NULL; |
1316 | int rc = 0; | |
1317 | ||
1318 | IUCV_DBF_TEXT(trace, 3, __func__); | |
1319 | if (priv && priv->conn) | |
1320 | ndev = priv->conn->netdev; | |
1321 | if (!ndev) | |
1322 | goto out; | |
1323 | netif_device_detach(ndev); | |
1324 | priv->pm_state = fsm_getstate(priv->fsm); | |
1325 | rc = netiucv_close(ndev); | |
1326 | out: | |
1327 | return rc; | |
1328 | } | |
1329 | ||
1330 | /** | |
1331 | * netiucv_pm_restore_thaw() - Thaw and restore PM callback | |
1332 | * @dev: netiucv device | |
1333 | * | |
1334 | * re-open netiucv interfaces closed during freeze | |
1335 | */ | |
1336 | static int netiucv_pm_restore_thaw(struct device *dev) | |
1337 | { | |
4f0076f7 | 1338 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1175b257 UB |
1339 | struct net_device *ndev = NULL; |
1340 | int rc = 0; | |
1341 | ||
1342 | IUCV_DBF_TEXT(trace, 3, __func__); | |
1343 | if (priv && priv->conn) | |
1344 | ndev = priv->conn->netdev; | |
1345 | if (!ndev) | |
1346 | goto out; | |
1347 | switch (priv->pm_state) { | |
1348 | case DEV_STATE_RUNNING: | |
1349 | case DEV_STATE_STARTWAIT: | |
1350 | rc = netiucv_open(ndev); | |
1351 | break; | |
1352 | default: | |
1353 | break; | |
1354 | } | |
1355 | netif_device_attach(ndev); | |
1356 | out: | |
1357 | return rc; | |
1358 | } | |
1359 | ||
1da177e4 LT |
1360 | /** |
1361 | * Start transmission of a packet. | |
1362 | * Called from generic network device layer. | |
1363 | * | |
1364 | * @param skb Pointer to buffer containing the packet. | |
1365 | * @param dev Pointer to interface struct. | |
1366 | * | |
1367 | * @return 0 if packet consumed, !0 if packet rejected. | |
1368 | * Note: If we return !0, then the packet is free'd by | |
1369 | * the generic network layer. | |
1370 | */ | |
1371 | static int netiucv_tx(struct sk_buff *skb, struct net_device *dev) | |
1372 | { | |
eebce385 MS |
1373 | struct netiucv_priv *privptr = netdev_priv(dev); |
1374 | int rc; | |
1da177e4 | 1375 | |
2a2cf6b1 | 1376 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1377 | /** |
1378 | * Some sanity checks ... | |
1379 | */ | |
1380 | if (skb == NULL) { | |
1da177e4 LT |
1381 | IUCV_DBF_TEXT(data, 2, "netiucv_tx: skb is NULL\n"); |
1382 | privptr->stats.tx_dropped++; | |
ec634fe3 | 1383 | return NETDEV_TX_OK; |
1da177e4 LT |
1384 | } |
1385 | if (skb_headroom(skb) < NETIUCV_HDRLEN) { | |
1da177e4 LT |
1386 | IUCV_DBF_TEXT(data, 2, |
1387 | "netiucv_tx: skb_headroom < NETIUCV_HDRLEN\n"); | |
1388 | dev_kfree_skb(skb); | |
1389 | privptr->stats.tx_dropped++; | |
ec634fe3 | 1390 | return NETDEV_TX_OK; |
1da177e4 LT |
1391 | } |
1392 | ||
1393 | /** | |
1394 | * If connection is not running, try to restart it | |
e82b0f2c | 1395 | * and throw away packet. |
1da177e4 LT |
1396 | */ |
1397 | if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) { | |
1da177e4 LT |
1398 | dev_kfree_skb(skb); |
1399 | privptr->stats.tx_dropped++; | |
1400 | privptr->stats.tx_errors++; | |
1401 | privptr->stats.tx_carrier_errors++; | |
ec634fe3 | 1402 | return NETDEV_TX_OK; |
1da177e4 LT |
1403 | } |
1404 | ||
1405 | if (netiucv_test_and_set_busy(dev)) { | |
1406 | IUCV_DBF_TEXT(data, 2, "EBUSY from netiucv_tx\n"); | |
4e584d66 | 1407 | return NETDEV_TX_BUSY; |
1da177e4 | 1408 | } |
860e9538 | 1409 | netif_trans_update(dev); |
5b548140 | 1410 | rc = netiucv_transmit_skb(privptr->conn, skb); |
1da177e4 | 1411 | netiucv_clear_busy(dev); |
5b548140 | 1412 | return rc ? NETDEV_TX_BUSY : NETDEV_TX_OK; |
1da177e4 LT |
1413 | } |
1414 | ||
1415 | /** | |
eebce385 MS |
1416 | * netiucv_stats |
1417 | * @dev: Pointer to interface struct. | |
1da177e4 | 1418 | * |
eebce385 | 1419 | * Returns interface statistics of a device. |
1da177e4 | 1420 | * |
eebce385 | 1421 | * Returns pointer to stats struct of this interface. |
1da177e4 | 1422 | */ |
eebce385 | 1423 | static struct net_device_stats *netiucv_stats (struct net_device * dev) |
1da177e4 | 1424 | { |
eebce385 MS |
1425 | struct netiucv_priv *priv = netdev_priv(dev); |
1426 | ||
2a2cf6b1 | 1427 | IUCV_DBF_TEXT(trace, 5, __func__); |
eebce385 | 1428 | return &priv->stats; |
1da177e4 LT |
1429 | } |
1430 | ||
1431 | /** | |
eebce385 MS |
1432 | * netiucv_change_mtu |
1433 | * @dev: Pointer to interface struct. | |
1434 | * @new_mtu: The new MTU to use for this interface. | |
1da177e4 | 1435 | * |
eebce385 | 1436 | * Sets MTU of an interface. |
1da177e4 | 1437 | * |
eebce385 | 1438 | * Returns 0 on success, -EINVAL if MTU is out of valid range. |
1da177e4 LT |
1439 | * (valid range is 576 .. NETIUCV_MTU_MAX). |
1440 | */ | |
eebce385 | 1441 | static int netiucv_change_mtu(struct net_device * dev, int new_mtu) |
1da177e4 | 1442 | { |
2a2cf6b1 | 1443 | IUCV_DBF_TEXT(trace, 3, __func__); |
eebce385 | 1444 | if (new_mtu < 576 || new_mtu > NETIUCV_MTU_MAX) { |
1da177e4 LT |
1445 | IUCV_DBF_TEXT(setup, 2, "given MTU out of valid range\n"); |
1446 | return -EINVAL; | |
1447 | } | |
1448 | dev->mtu = new_mtu; | |
1449 | return 0; | |
1450 | } | |
1451 | ||
eebce385 | 1452 | /* |
1da177e4 | 1453 | * attributes in sysfs |
eebce385 | 1454 | */ |
1da177e4 | 1455 | |
eebce385 MS |
1456 | static ssize_t user_show(struct device *dev, struct device_attribute *attr, |
1457 | char *buf) | |
1da177e4 | 1458 | { |
dff59b64 | 1459 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1460 | |
2a2cf6b1 | 1461 | IUCV_DBF_TEXT(trace, 5, __func__); |
08e3356c | 1462 | return sprintf(buf, "%s\n", netiucv_printuser(priv->conn)); |
1da177e4 LT |
1463 | } |
1464 | ||
08e3356c UB |
1465 | static int netiucv_check_user(const char *buf, size_t count, char *username, |
1466 | char *userdata) | |
1da177e4 | 1467 | { |
08e3356c UB |
1468 | const char *p; |
1469 | int i; | |
1da177e4 | 1470 | |
08e3356c UB |
1471 | p = strchr(buf, '.'); |
1472 | if ((p && ((count > 26) || | |
1473 | ((p - buf) > 8) || | |
1474 | (buf + count - p > 18))) || | |
1475 | (!p && (count > 9))) { | |
1476 | IUCV_DBF_TEXT(setup, 2, "conn_write: too long\n"); | |
1da177e4 LT |
1477 | return -EINVAL; |
1478 | } | |
1479 | ||
08e3356c UB |
1480 | for (i = 0, p = buf; i < 8 && *p && *p != '.'; i++, p++) { |
1481 | if (isalnum(*p) || *p == '$') { | |
1482 | username[i] = toupper(*p); | |
eebce385 MS |
1483 | continue; |
1484 | } | |
08e3356c | 1485 | if (*p == '\n') |
1da177e4 LT |
1486 | /* trailing lf, grr */ |
1487 | break; | |
eebce385 | 1488 | IUCV_DBF_TEXT_(setup, 2, |
08e3356c | 1489 | "conn_write: invalid character %02x\n", *p); |
eebce385 | 1490 | return -EINVAL; |
1da177e4 | 1491 | } |
eebce385 | 1492 | while (i < 8) |
1da177e4 | 1493 | username[i++] = ' '; |
16a83b30 | 1494 | username[8] = '\0'; |
1da177e4 | 1495 | |
08e3356c UB |
1496 | if (*p == '.') { |
1497 | p++; | |
1498 | for (i = 0; i < 16 && *p; i++, p++) { | |
1499 | if (*p == '\n') | |
1500 | break; | |
1501 | userdata[i] = toupper(*p); | |
1502 | } | |
1503 | while (i > 0 && i < 16) | |
1504 | userdata[i++] = ' '; | |
1505 | } else | |
1506 | memcpy(userdata, iucvMagic_ascii, 16); | |
1507 | userdata[16] = '\0'; | |
1508 | ASCEBC(userdata, 16); | |
1509 | ||
1510 | return 0; | |
1511 | } | |
1512 | ||
1513 | static ssize_t user_write(struct device *dev, struct device_attribute *attr, | |
1514 | const char *buf, size_t count) | |
1515 | { | |
1516 | struct netiucv_priv *priv = dev_get_drvdata(dev); | |
1517 | struct net_device *ndev = priv->conn->netdev; | |
1518 | char username[9]; | |
1519 | char userdata[17]; | |
1520 | int rc; | |
1521 | struct iucv_connection *cp; | |
1522 | ||
1523 | IUCV_DBF_TEXT(trace, 3, __func__); | |
1524 | rc = netiucv_check_user(buf, count, username, userdata); | |
1525 | if (rc) | |
1526 | return rc; | |
1527 | ||
eebce385 MS |
1528 | if (memcmp(username, priv->conn->userid, 9) && |
1529 | (ndev->flags & (IFF_UP | IFF_RUNNING))) { | |
1530 | /* username changed while the interface is active. */ | |
eebce385 | 1531 | IUCV_DBF_TEXT(setup, 2, "user_write: device active\n"); |
f082bcae | 1532 | return -EPERM; |
eebce385 MS |
1533 | } |
1534 | read_lock_bh(&iucv_connection_rwlock); | |
1535 | list_for_each_entry(cp, &iucv_connection_list, list) { | |
08e3356c UB |
1536 | if (!strncmp(username, cp->userid, 9) && |
1537 | !strncmp(userdata, cp->userdata, 17) && cp->netdev != ndev) { | |
eebce385 | 1538 | read_unlock_bh(&iucv_connection_rwlock); |
08e3356c UB |
1539 | IUCV_DBF_TEXT_(setup, 2, "user_write: Connection to %s " |
1540 | "already exists\n", netiucv_printuser(cp)); | |
eebce385 | 1541 | return -EEXIST; |
1da177e4 LT |
1542 | } |
1543 | } | |
eebce385 | 1544 | read_unlock_bh(&iucv_connection_rwlock); |
1da177e4 | 1545 | memcpy(priv->conn->userid, username, 9); |
08e3356c | 1546 | memcpy(priv->conn->userdata, userdata, 17); |
1da177e4 | 1547 | return count; |
1da177e4 LT |
1548 | } |
1549 | ||
1550 | static DEVICE_ATTR(user, 0644, user_show, user_write); | |
1551 | ||
eebce385 MS |
1552 | static ssize_t buffer_show (struct device *dev, struct device_attribute *attr, |
1553 | char *buf) | |
dff59b64 GKH |
1554 | { |
1555 | struct netiucv_priv *priv = dev_get_drvdata(dev); | |
1da177e4 | 1556 | |
2a2cf6b1 | 1557 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1558 | return sprintf(buf, "%d\n", priv->conn->max_buffsize); |
1559 | } | |
1560 | ||
eebce385 MS |
1561 | static ssize_t buffer_write (struct device *dev, struct device_attribute *attr, |
1562 | const char *buf, size_t count) | |
1da177e4 | 1563 | { |
dff59b64 | 1564 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 LT |
1565 | struct net_device *ndev = priv->conn->netdev; |
1566 | char *e; | |
1567 | int bs1; | |
1568 | ||
2a2cf6b1 | 1569 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
1570 | if (count >= 39) |
1571 | return -EINVAL; | |
1572 | ||
1573 | bs1 = simple_strtoul(buf, &e, 0); | |
1574 | ||
1575 | if (e && (!isspace(*e))) { | |
08e3356c UB |
1576 | IUCV_DBF_TEXT_(setup, 2, "buffer_write: invalid char %02x\n", |
1577 | *e); | |
1da177e4 LT |
1578 | return -EINVAL; |
1579 | } | |
1580 | if (bs1 > NETIUCV_BUFSIZE_MAX) { | |
1da177e4 LT |
1581 | IUCV_DBF_TEXT_(setup, 2, |
1582 | "buffer_write: buffer size %d too large\n", | |
1583 | bs1); | |
1584 | return -EINVAL; | |
1585 | } | |
1586 | if ((ndev->flags & IFF_RUNNING) && | |
1587 | (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2))) { | |
1da177e4 LT |
1588 | IUCV_DBF_TEXT_(setup, 2, |
1589 | "buffer_write: buffer size %d too small\n", | |
1590 | bs1); | |
1591 | return -EINVAL; | |
1592 | } | |
1593 | if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN)) { | |
1da177e4 LT |
1594 | IUCV_DBF_TEXT_(setup, 2, |
1595 | "buffer_write: buffer size %d too small\n", | |
1596 | bs1); | |
1597 | return -EINVAL; | |
1598 | } | |
1599 | ||
1600 | priv->conn->max_buffsize = bs1; | |
1601 | if (!(ndev->flags & IFF_RUNNING)) | |
1602 | ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN; | |
1603 | ||
1604 | return count; | |
1605 | ||
1606 | } | |
1607 | ||
1608 | static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write); | |
1609 | ||
eebce385 MS |
1610 | static ssize_t dev_fsm_show (struct device *dev, struct device_attribute *attr, |
1611 | char *buf) | |
1da177e4 | 1612 | { |
dff59b64 | 1613 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1614 | |
2a2cf6b1 | 1615 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1616 | return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm)); |
1617 | } | |
1618 | ||
1619 | static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL); | |
1620 | ||
eebce385 MS |
1621 | static ssize_t conn_fsm_show (struct device *dev, |
1622 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1623 | { |
dff59b64 | 1624 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1625 | |
2a2cf6b1 | 1626 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1627 | return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm)); |
1628 | } | |
1629 | ||
1630 | static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL); | |
1631 | ||
eebce385 MS |
1632 | static ssize_t maxmulti_show (struct device *dev, |
1633 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1634 | { |
dff59b64 | 1635 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1636 | |
2a2cf6b1 | 1637 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1638 | return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti); |
1639 | } | |
1640 | ||
eebce385 MS |
1641 | static ssize_t maxmulti_write (struct device *dev, |
1642 | struct device_attribute *attr, | |
1643 | const char *buf, size_t count) | |
1da177e4 | 1644 | { |
dff59b64 | 1645 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1646 | |
2a2cf6b1 | 1647 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1648 | priv->conn->prof.maxmulti = 0; |
1649 | return count; | |
1650 | } | |
1651 | ||
1652 | static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write); | |
1653 | ||
eebce385 MS |
1654 | static ssize_t maxcq_show (struct device *dev, struct device_attribute *attr, |
1655 | char *buf) | |
1da177e4 | 1656 | { |
dff59b64 | 1657 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1658 | |
2a2cf6b1 | 1659 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1660 | return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue); |
1661 | } | |
1662 | ||
eebce385 MS |
1663 | static ssize_t maxcq_write (struct device *dev, struct device_attribute *attr, |
1664 | const char *buf, size_t count) | |
1da177e4 | 1665 | { |
dff59b64 | 1666 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
e82b0f2c | 1667 | |
2a2cf6b1 | 1668 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1669 | priv->conn->prof.maxcqueue = 0; |
1670 | return count; | |
1671 | } | |
1672 | ||
1673 | static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write); | |
1674 | ||
eebce385 MS |
1675 | static ssize_t sdoio_show (struct device *dev, struct device_attribute *attr, |
1676 | char *buf) | |
1da177e4 | 1677 | { |
dff59b64 | 1678 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1679 | |
2a2cf6b1 | 1680 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1681 | return sprintf(buf, "%ld\n", priv->conn->prof.doios_single); |
1682 | } | |
1683 | ||
eebce385 MS |
1684 | static ssize_t sdoio_write (struct device *dev, struct device_attribute *attr, |
1685 | const char *buf, size_t count) | |
1da177e4 | 1686 | { |
dff59b64 | 1687 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
e82b0f2c | 1688 | |
2a2cf6b1 | 1689 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1690 | priv->conn->prof.doios_single = 0; |
1691 | return count; | |
1692 | } | |
1693 | ||
1694 | static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write); | |
1695 | ||
eebce385 MS |
1696 | static ssize_t mdoio_show (struct device *dev, struct device_attribute *attr, |
1697 | char *buf) | |
1da177e4 | 1698 | { |
dff59b64 | 1699 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1700 | |
2a2cf6b1 | 1701 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1702 | return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi); |
1703 | } | |
1704 | ||
eebce385 MS |
1705 | static ssize_t mdoio_write (struct device *dev, struct device_attribute *attr, |
1706 | const char *buf, size_t count) | |
1da177e4 | 1707 | { |
dff59b64 | 1708 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
e82b0f2c | 1709 | |
2a2cf6b1 | 1710 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1711 | priv->conn->prof.doios_multi = 0; |
1712 | return count; | |
1713 | } | |
1714 | ||
1715 | static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write); | |
1716 | ||
eebce385 MS |
1717 | static ssize_t txlen_show (struct device *dev, struct device_attribute *attr, |
1718 | char *buf) | |
1da177e4 | 1719 | { |
dff59b64 | 1720 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1721 | |
2a2cf6b1 | 1722 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1723 | return sprintf(buf, "%ld\n", priv->conn->prof.txlen); |
1724 | } | |
1725 | ||
eebce385 MS |
1726 | static ssize_t txlen_write (struct device *dev, struct device_attribute *attr, |
1727 | const char *buf, size_t count) | |
1da177e4 | 1728 | { |
dff59b64 | 1729 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
e82b0f2c | 1730 | |
2a2cf6b1 | 1731 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1732 | priv->conn->prof.txlen = 0; |
1733 | return count; | |
1734 | } | |
1735 | ||
1736 | static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write); | |
1737 | ||
eebce385 MS |
1738 | static ssize_t txtime_show (struct device *dev, struct device_attribute *attr, |
1739 | char *buf) | |
1da177e4 | 1740 | { |
dff59b64 | 1741 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1742 | |
2a2cf6b1 | 1743 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1744 | return sprintf(buf, "%ld\n", priv->conn->prof.tx_time); |
1745 | } | |
1746 | ||
eebce385 MS |
1747 | static ssize_t txtime_write (struct device *dev, struct device_attribute *attr, |
1748 | const char *buf, size_t count) | |
1da177e4 | 1749 | { |
dff59b64 | 1750 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
e82b0f2c | 1751 | |
2a2cf6b1 | 1752 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1753 | priv->conn->prof.tx_time = 0; |
1754 | return count; | |
1755 | } | |
1756 | ||
1757 | static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write); | |
1758 | ||
eebce385 MS |
1759 | static ssize_t txpend_show (struct device *dev, struct device_attribute *attr, |
1760 | char *buf) | |
1da177e4 | 1761 | { |
dff59b64 | 1762 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1763 | |
2a2cf6b1 | 1764 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1765 | return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending); |
1766 | } | |
1767 | ||
eebce385 MS |
1768 | static ssize_t txpend_write (struct device *dev, struct device_attribute *attr, |
1769 | const char *buf, size_t count) | |
1da177e4 | 1770 | { |
dff59b64 | 1771 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1772 | |
2a2cf6b1 | 1773 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1774 | priv->conn->prof.tx_pending = 0; |
1775 | return count; | |
1776 | } | |
1777 | ||
1778 | static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write); | |
1779 | ||
eebce385 MS |
1780 | static ssize_t txmpnd_show (struct device *dev, struct device_attribute *attr, |
1781 | char *buf) | |
1da177e4 | 1782 | { |
dff59b64 | 1783 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1784 | |
2a2cf6b1 | 1785 | IUCV_DBF_TEXT(trace, 5, __func__); |
1da177e4 LT |
1786 | return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending); |
1787 | } | |
1788 | ||
eebce385 MS |
1789 | static ssize_t txmpnd_write (struct device *dev, struct device_attribute *attr, |
1790 | const char *buf, size_t count) | |
1da177e4 | 1791 | { |
dff59b64 | 1792 | struct netiucv_priv *priv = dev_get_drvdata(dev); |
1da177e4 | 1793 | |
2a2cf6b1 | 1794 | IUCV_DBF_TEXT(trace, 4, __func__); |
1da177e4 LT |
1795 | priv->conn->prof.tx_max_pending = 0; |
1796 | return count; | |
1797 | } | |
1798 | ||
1799 | static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write); | |
1800 | ||
1801 | static struct attribute *netiucv_attrs[] = { | |
1802 | &dev_attr_buffer.attr, | |
1803 | &dev_attr_user.attr, | |
1804 | NULL, | |
1805 | }; | |
1806 | ||
1807 | static struct attribute_group netiucv_attr_group = { | |
1808 | .attrs = netiucv_attrs, | |
1809 | }; | |
1810 | ||
1811 | static struct attribute *netiucv_stat_attrs[] = { | |
1812 | &dev_attr_device_fsm_state.attr, | |
1813 | &dev_attr_connection_fsm_state.attr, | |
1814 | &dev_attr_max_tx_buffer_used.attr, | |
1815 | &dev_attr_max_chained_skbs.attr, | |
1816 | &dev_attr_tx_single_write_ops.attr, | |
1817 | &dev_attr_tx_multi_write_ops.attr, | |
1818 | &dev_attr_netto_bytes.attr, | |
1819 | &dev_attr_max_tx_io_time.attr, | |
1820 | &dev_attr_tx_pending.attr, | |
1821 | &dev_attr_tx_max_pending.attr, | |
1822 | NULL, | |
1823 | }; | |
1824 | ||
1825 | static struct attribute_group netiucv_stat_attr_group = { | |
1826 | .name = "stats", | |
1827 | .attrs = netiucv_stat_attrs, | |
1828 | }; | |
1829 | ||
0b945293 | 1830 | static const struct attribute_group *netiucv_attr_groups[] = { |
1831 | &netiucv_stat_attr_group, | |
1832 | &netiucv_attr_group, | |
1833 | NULL, | |
1834 | }; | |
1da177e4 | 1835 | |
eebce385 | 1836 | static int netiucv_register_device(struct net_device *ndev) |
1da177e4 | 1837 | { |
eebce385 | 1838 | struct netiucv_priv *priv = netdev_priv(ndev); |
88abaab4 | 1839 | struct device *dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
1da177e4 LT |
1840 | int ret; |
1841 | ||
2a2cf6b1 | 1842 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
1843 | |
1844 | if (dev) { | |
1bf5b285 | 1845 | dev_set_name(dev, "net%s", ndev->name); |
1da177e4 LT |
1846 | dev->bus = &iucv_bus; |
1847 | dev->parent = iucv_root; | |
0b945293 | 1848 | dev->groups = netiucv_attr_groups; |
1da177e4 LT |
1849 | /* |
1850 | * The release function could be called after the | |
1851 | * module has been unloaded. It's _only_ task is to | |
1852 | * free the struct. Therefore, we specify kfree() | |
1853 | * directly here. (Probably a little bit obfuscating | |
1854 | * but legitime ...). | |
1855 | */ | |
1856 | dev->release = (void (*)(struct device *))kfree; | |
1857 | dev->driver = &netiucv_driver; | |
1858 | } else | |
1859 | return -ENOMEM; | |
1860 | ||
1861 | ret = device_register(dev); | |
c6304933 SO |
1862 | if (ret) { |
1863 | put_device(dev); | |
1da177e4 | 1864 | return ret; |
c6304933 | 1865 | } |
1da177e4 | 1866 | priv->dev = dev; |
dff59b64 | 1867 | dev_set_drvdata(dev, priv); |
1da177e4 | 1868 | return 0; |
1da177e4 LT |
1869 | } |
1870 | ||
eebce385 | 1871 | static void netiucv_unregister_device(struct device *dev) |
1da177e4 | 1872 | { |
2a2cf6b1 | 1873 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
1874 | device_unregister(dev); |
1875 | } | |
1876 | ||
1877 | /** | |
1878 | * Allocate and initialize a new connection structure. | |
1879 | * Add it to the list of netiucv connections; | |
1880 | */ | |
eebce385 | 1881 | static struct iucv_connection *netiucv_new_connection(struct net_device *dev, |
08e3356c UB |
1882 | char *username, |
1883 | char *userdata) | |
eebce385 MS |
1884 | { |
1885 | struct iucv_connection *conn; | |
1da177e4 | 1886 | |
eebce385 MS |
1887 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); |
1888 | if (!conn) | |
1889 | goto out; | |
1890 | skb_queue_head_init(&conn->collect_queue); | |
1891 | skb_queue_head_init(&conn->commit_queue); | |
1892 | spin_lock_init(&conn->collect_lock); | |
1893 | conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT; | |
1894 | conn->netdev = dev; | |
1895 | ||
1896 | conn->rx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA); | |
1897 | if (!conn->rx_buff) | |
1898 | goto out_conn; | |
1899 | conn->tx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA); | |
1900 | if (!conn->tx_buff) | |
1901 | goto out_rx; | |
1902 | conn->fsm = init_fsm("netiucvconn", conn_state_names, | |
1903 | conn_event_names, NR_CONN_STATES, | |
1904 | NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN, | |
1905 | GFP_KERNEL); | |
1906 | if (!conn->fsm) | |
1907 | goto out_tx; | |
1908 | ||
1909 | fsm_settimer(conn->fsm, &conn->timer); | |
1910 | fsm_newstate(conn->fsm, CONN_STATE_INVALID); | |
1911 | ||
08e3356c UB |
1912 | if (userdata) |
1913 | memcpy(conn->userdata, userdata, 17); | |
eebce385 MS |
1914 | if (username) { |
1915 | memcpy(conn->userid, username, 9); | |
1916 | fsm_newstate(conn->fsm, CONN_STATE_STOPPED); | |
1da177e4 | 1917 | } |
eebce385 MS |
1918 | |
1919 | write_lock_bh(&iucv_connection_rwlock); | |
1920 | list_add_tail(&conn->list, &iucv_connection_list); | |
1921 | write_unlock_bh(&iucv_connection_rwlock); | |
1da177e4 | 1922 | return conn; |
eebce385 MS |
1923 | |
1924 | out_tx: | |
1925 | kfree_skb(conn->tx_buff); | |
1926 | out_rx: | |
1927 | kfree_skb(conn->rx_buff); | |
1928 | out_conn: | |
1929 | kfree(conn); | |
1930 | out: | |
1931 | return NULL; | |
1da177e4 LT |
1932 | } |
1933 | ||
1934 | /** | |
1935 | * Release a connection structure and remove it from the | |
1936 | * list of netiucv connections. | |
1937 | */ | |
eebce385 | 1938 | static void netiucv_remove_connection(struct iucv_connection *conn) |
1da177e4 | 1939 | { |
08e3356c | 1940 | |
2a2cf6b1 | 1941 | IUCV_DBF_TEXT(trace, 3, __func__); |
eebce385 MS |
1942 | write_lock_bh(&iucv_connection_rwlock); |
1943 | list_del_init(&conn->list); | |
1944 | write_unlock_bh(&iucv_connection_rwlock); | |
0be4acec UB |
1945 | fsm_deltimer(&conn->timer); |
1946 | netiucv_purge_skb_queue(&conn->collect_queue); | |
eebce385 | 1947 | if (conn->path) { |
08e3356c | 1948 | iucv_path_sever(conn->path, conn->userdata); |
eebce385 MS |
1949 | kfree(conn->path); |
1950 | conn->path = NULL; | |
1da177e4 | 1951 | } |
0be4acec | 1952 | netiucv_purge_skb_queue(&conn->commit_queue); |
eebce385 MS |
1953 | kfree_fsm(conn->fsm); |
1954 | kfree_skb(conn->rx_buff); | |
1955 | kfree_skb(conn->tx_buff); | |
1da177e4 LT |
1956 | } |
1957 | ||
1958 | /** | |
1959 | * Release everything of a net device. | |
1960 | */ | |
eebce385 | 1961 | static void netiucv_free_netdevice(struct net_device *dev) |
1da177e4 | 1962 | { |
eebce385 | 1963 | struct netiucv_priv *privptr = netdev_priv(dev); |
1da177e4 | 1964 | |
2a2cf6b1 | 1965 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
1966 | |
1967 | if (!dev) | |
1968 | return; | |
1969 | ||
1da177e4 LT |
1970 | if (privptr) { |
1971 | if (privptr->conn) | |
1972 | netiucv_remove_connection(privptr->conn); | |
1973 | if (privptr->fsm) | |
1974 | kfree_fsm(privptr->fsm); | |
1975 | privptr->conn = NULL; privptr->fsm = NULL; | |
1976 | /* privptr gets freed by free_netdev() */ | |
1977 | } | |
1978 | free_netdev(dev); | |
1979 | } | |
1980 | ||
1981 | /** | |
1982 | * Initialize a net device. (Called from kernel in alloc_netdev()) | |
1983 | */ | |
4edd73b5 FB |
1984 | static const struct net_device_ops netiucv_netdev_ops = { |
1985 | .ndo_open = netiucv_open, | |
1986 | .ndo_stop = netiucv_close, | |
1987 | .ndo_get_stats = netiucv_stats, | |
1988 | .ndo_start_xmit = netiucv_tx, | |
1989 | .ndo_change_mtu = netiucv_change_mtu, | |
1990 | }; | |
1991 | ||
eebce385 | 1992 | static void netiucv_setup_netdevice(struct net_device *dev) |
1da177e4 | 1993 | { |
1da177e4 | 1994 | dev->mtu = NETIUCV_MTU_DEFAULT; |
1da177e4 LT |
1995 | dev->destructor = netiucv_free_netdevice; |
1996 | dev->hard_header_len = NETIUCV_HDRLEN; | |
1997 | dev->addr_len = 0; | |
1998 | dev->type = ARPHRD_SLIP; | |
1999 | dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT; | |
2000 | dev->flags = IFF_POINTOPOINT | IFF_NOARP; | |
4edd73b5 | 2001 | dev->netdev_ops = &netiucv_netdev_ops; |
1da177e4 LT |
2002 | } |
2003 | ||
2004 | /** | |
2005 | * Allocate and initialize everything of a net device. | |
2006 | */ | |
08e3356c | 2007 | static struct net_device *netiucv_init_netdevice(char *username, char *userdata) |
1da177e4 LT |
2008 | { |
2009 | struct netiucv_priv *privptr; | |
2010 | struct net_device *dev; | |
2011 | ||
2012 | dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d", | |
c835a677 | 2013 | NET_NAME_UNKNOWN, netiucv_setup_netdevice); |
1da177e4 LT |
2014 | if (!dev) |
2015 | return NULL; | |
aaf9522d | 2016 | rtnl_lock(); |
1d503563 UB |
2017 | if (dev_alloc_name(dev, dev->name) < 0) |
2018 | goto out_netdev; | |
1da177e4 | 2019 | |
eebce385 | 2020 | privptr = netdev_priv(dev); |
1da177e4 LT |
2021 | privptr->fsm = init_fsm("netiucvdev", dev_state_names, |
2022 | dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS, | |
2023 | dev_fsm, DEV_FSM_LEN, GFP_KERNEL); | |
eebce385 MS |
2024 | if (!privptr->fsm) |
2025 | goto out_netdev; | |
2026 | ||
08e3356c | 2027 | privptr->conn = netiucv_new_connection(dev, username, userdata); |
1da177e4 | 2028 | if (!privptr->conn) { |
1da177e4 | 2029 | IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_new_connection\n"); |
eebce385 | 2030 | goto out_fsm; |
1da177e4 LT |
2031 | } |
2032 | fsm_newstate(privptr->fsm, DEV_STATE_STOPPED); | |
1da177e4 | 2033 | return dev; |
eebce385 MS |
2034 | |
2035 | out_fsm: | |
2036 | kfree_fsm(privptr->fsm); | |
2037 | out_netdev: | |
aaf9522d | 2038 | rtnl_unlock(); |
eebce385 MS |
2039 | free_netdev(dev); |
2040 | return NULL; | |
1da177e4 LT |
2041 | } |
2042 | ||
eebce385 MS |
2043 | static ssize_t conn_write(struct device_driver *drv, |
2044 | const char *buf, size_t count) | |
1da177e4 | 2045 | { |
16a83b30 | 2046 | char username[9]; |
08e3356c UB |
2047 | char userdata[17]; |
2048 | int rc; | |
1da177e4 | 2049 | struct net_device *dev; |
eebce385 MS |
2050 | struct netiucv_priv *priv; |
2051 | struct iucv_connection *cp; | |
1da177e4 | 2052 | |
2a2cf6b1 | 2053 | IUCV_DBF_TEXT(trace, 3, __func__); |
08e3356c UB |
2054 | rc = netiucv_check_user(buf, count, username, userdata); |
2055 | if (rc) | |
2056 | return rc; | |
16a83b30 | 2057 | |
eebce385 MS |
2058 | read_lock_bh(&iucv_connection_rwlock); |
2059 | list_for_each_entry(cp, &iucv_connection_list, list) { | |
08e3356c UB |
2060 | if (!strncmp(username, cp->userid, 9) && |
2061 | !strncmp(userdata, cp->userdata, 17)) { | |
eebce385 | 2062 | read_unlock_bh(&iucv_connection_rwlock); |
08e3356c UB |
2063 | IUCV_DBF_TEXT_(setup, 2, "conn_write: Connection to %s " |
2064 | "already exists\n", netiucv_printuser(cp)); | |
eebce385 MS |
2065 | return -EEXIST; |
2066 | } | |
16a83b30 | 2067 | } |
eebce385 MS |
2068 | read_unlock_bh(&iucv_connection_rwlock); |
2069 | ||
08e3356c | 2070 | dev = netiucv_init_netdevice(username, userdata); |
1da177e4 | 2071 | if (!dev) { |
1da177e4 LT |
2072 | IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_init_netdevice\n"); |
2073 | return -ENODEV; | |
2074 | } | |
2075 | ||
eebce385 MS |
2076 | rc = netiucv_register_device(dev); |
2077 | if (rc) { | |
aaf9522d | 2078 | rtnl_unlock(); |
1da177e4 | 2079 | IUCV_DBF_TEXT_(setup, 2, |
eebce385 | 2080 | "ret %d from netiucv_register_device\n", rc); |
1da177e4 LT |
2081 | goto out_free_ndev; |
2082 | } | |
2083 | ||
2084 | /* sysfs magic */ | |
eebce385 MS |
2085 | priv = netdev_priv(dev); |
2086 | SET_NETDEV_DEV(dev, priv->dev); | |
1da177e4 | 2087 | |
aaf9522d BP |
2088 | rc = register_netdevice(dev); |
2089 | rtnl_unlock(); | |
eebce385 MS |
2090 | if (rc) |
2091 | goto out_unreg; | |
1da177e4 | 2092 | |
08e3356c UB |
2093 | dev_info(priv->dev, "The IUCV interface to %s has been established " |
2094 | "successfully\n", | |
2095 | netiucv_printuser(priv->conn)); | |
e82b0f2c | 2096 | |
1da177e4 LT |
2097 | return count; |
2098 | ||
eebce385 MS |
2099 | out_unreg: |
2100 | netiucv_unregister_device(priv->dev); | |
1da177e4 | 2101 | out_free_ndev: |
1da177e4 | 2102 | netiucv_free_netdevice(dev); |
eebce385 | 2103 | return rc; |
1da177e4 LT |
2104 | } |
2105 | ||
2b67fc46 | 2106 | static DRIVER_ATTR(connection, 0200, NULL, conn_write); |
1da177e4 | 2107 | |
eebce385 MS |
2108 | static ssize_t remove_write (struct device_driver *drv, |
2109 | const char *buf, size_t count) | |
1da177e4 | 2110 | { |
eebce385 | 2111 | struct iucv_connection *cp; |
1da177e4 LT |
2112 | struct net_device *ndev; |
2113 | struct netiucv_priv *priv; | |
2114 | struct device *dev; | |
2115 | char name[IFNAMSIZ]; | |
eebce385 | 2116 | const char *p; |
1da177e4 LT |
2117 | int i; |
2118 | ||
2a2cf6b1 | 2119 | IUCV_DBF_TEXT(trace, 3, __func__); |
1da177e4 LT |
2120 | |
2121 | if (count >= IFNAMSIZ) | |
a419aef8 | 2122 | count = IFNAMSIZ - 1; |
1da177e4 | 2123 | |
eebce385 MS |
2124 | for (i = 0, p = buf; i < count && *p; i++, p++) { |
2125 | if (*p == '\n' || *p == ' ') | |
1da177e4 LT |
2126 | /* trailing lf, grr */ |
2127 | break; | |
eebce385 | 2128 | name[i] = *p; |
1da177e4 LT |
2129 | } |
2130 | name[i] = '\0'; | |
2131 | ||
eebce385 MS |
2132 | read_lock_bh(&iucv_connection_rwlock); |
2133 | list_for_each_entry(cp, &iucv_connection_list, list) { | |
2134 | ndev = cp->netdev; | |
2135 | priv = netdev_priv(ndev); | |
1da177e4 | 2136 | dev = priv->dev; |
eebce385 MS |
2137 | if (strncmp(name, ndev->name, count)) |
2138 | continue; | |
2139 | read_unlock_bh(&iucv_connection_rwlock); | |
1da177e4 | 2140 | if (ndev->flags & (IFF_UP | IFF_RUNNING)) { |
8f7c502c UB |
2141 | dev_warn(dev, "The IUCV device is connected" |
2142 | " to %s and cannot be removed\n", | |
2143 | priv->conn->userid); | |
1da177e4 | 2144 | IUCV_DBF_TEXT(data, 2, "remove_write: still active\n"); |
f082bcae | 2145 | return -EPERM; |
1da177e4 LT |
2146 | } |
2147 | unregister_netdev(ndev); | |
2148 | netiucv_unregister_device(dev); | |
2149 | return count; | |
2150 | } | |
eebce385 | 2151 | read_unlock_bh(&iucv_connection_rwlock); |
1da177e4 LT |
2152 | IUCV_DBF_TEXT(data, 2, "remove_write: unknown device\n"); |
2153 | return -EINVAL; | |
2154 | } | |
2155 | ||
2b67fc46 | 2156 | static DRIVER_ATTR(remove, 0200, NULL, remove_write); |
1da177e4 | 2157 | |
eebce385 MS |
2158 | static struct attribute * netiucv_drv_attrs[] = { |
2159 | &driver_attr_connection.attr, | |
2160 | &driver_attr_remove.attr, | |
2161 | NULL, | |
2162 | }; | |
2163 | ||
2164 | static struct attribute_group netiucv_drv_attr_group = { | |
2165 | .attrs = netiucv_drv_attrs, | |
2166 | }; | |
2167 | ||
a4dbd674 | 2168 | static const struct attribute_group *netiucv_drv_attr_groups[] = { |
5b88feb1 CH |
2169 | &netiucv_drv_attr_group, |
2170 | NULL, | |
2171 | }; | |
2172 | ||
eebce385 | 2173 | static void netiucv_banner(void) |
1da177e4 | 2174 | { |
8f7c502c | 2175 | pr_info("driver initialized\n"); |
1da177e4 LT |
2176 | } |
2177 | ||
eebce385 | 2178 | static void __exit netiucv_exit(void) |
1da177e4 | 2179 | { |
eebce385 MS |
2180 | struct iucv_connection *cp; |
2181 | struct net_device *ndev; | |
2182 | struct netiucv_priv *priv; | |
2183 | struct device *dev; | |
2184 | ||
2a2cf6b1 | 2185 | IUCV_DBF_TEXT(trace, 3, __func__); |
eebce385 MS |
2186 | while (!list_empty(&iucv_connection_list)) { |
2187 | cp = list_entry(iucv_connection_list.next, | |
2188 | struct iucv_connection, list); | |
eebce385 MS |
2189 | ndev = cp->netdev; |
2190 | priv = netdev_priv(ndev); | |
2191 | dev = priv->dev; | |
1da177e4 LT |
2192 | |
2193 | unregister_netdev(ndev); | |
2194 | netiucv_unregister_device(dev); | |
2195 | } | |
2196 | ||
1175b257 | 2197 | device_unregister(netiucv_dev); |
1da177e4 | 2198 | driver_unregister(&netiucv_driver); |
eebce385 | 2199 | iucv_unregister(&netiucv_handler, 1); |
1da177e4 LT |
2200 | iucv_unregister_dbf_views(); |
2201 | ||
8f7c502c | 2202 | pr_info("driver unloaded\n"); |
1da177e4 LT |
2203 | return; |
2204 | } | |
2205 | ||
eebce385 | 2206 | static int __init netiucv_init(void) |
1da177e4 | 2207 | { |
eebce385 | 2208 | int rc; |
e82b0f2c | 2209 | |
eebce385 MS |
2210 | rc = iucv_register_dbf_views(); |
2211 | if (rc) | |
2212 | goto out; | |
2213 | rc = iucv_register(&netiucv_handler, 1); | |
2214 | if (rc) | |
2215 | goto out_dbf; | |
2a2cf6b1 | 2216 | IUCV_DBF_TEXT(trace, 3, __func__); |
0a0a8310 | 2217 | netiucv_driver.groups = netiucv_drv_attr_groups; |
eebce385 MS |
2218 | rc = driver_register(&netiucv_driver); |
2219 | if (rc) { | |
eebce385 MS |
2220 | IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_register\n", rc); |
2221 | goto out_iucv; | |
1da177e4 | 2222 | } |
1175b257 UB |
2223 | /* establish dummy device */ |
2224 | netiucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
2225 | if (!netiucv_dev) { | |
2226 | rc = -ENOMEM; | |
2227 | goto out_driver; | |
2228 | } | |
2229 | dev_set_name(netiucv_dev, "netiucv"); | |
2230 | netiucv_dev->bus = &iucv_bus; | |
2231 | netiucv_dev->parent = iucv_root; | |
2232 | netiucv_dev->release = (void (*)(struct device *))kfree; | |
2233 | netiucv_dev->driver = &netiucv_driver; | |
2234 | rc = device_register(netiucv_dev); | |
c6304933 SO |
2235 | if (rc) { |
2236 | put_device(netiucv_dev); | |
1175b257 | 2237 | goto out_driver; |
c6304933 | 2238 | } |
eebce385 MS |
2239 | netiucv_banner(); |
2240 | return rc; | |
2241 | ||
1175b257 UB |
2242 | out_driver: |
2243 | driver_unregister(&netiucv_driver); | |
eebce385 MS |
2244 | out_iucv: |
2245 | iucv_unregister(&netiucv_handler, 1); | |
2246 | out_dbf: | |
2247 | iucv_unregister_dbf_views(); | |
2248 | out: | |
2249 | return rc; | |
1da177e4 | 2250 | } |
e82b0f2c | 2251 | |
1da177e4 LT |
2252 | module_init(netiucv_init); |
2253 | module_exit(netiucv_exit); | |
2254 | MODULE_LICENSE("GPL"); |