netiucv.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /*
  2. * $Id: netiucv.c,v 1.66 2005/05/11 08:10:17 holzheu Exp $
  3. *
  4. * IUCV network driver
  5. *
  6. * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
  8. *
  9. * Driverfs integration and all bugs therein by Cornelia Huck(cohuck@de.ibm.com)
  10. *
  11. * Documentation used:
  12. * the source of the original IUCV driver by:
  13. * Stefan Hegewald <hegewald@de.ibm.com>
  14. * Hartmut Penner <hpenner@de.ibm.com>
  15. * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  16. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  17. * Alan Altmark (Alan_Altmark@us.ibm.com) Sept. 2000
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2, or (at your option)
  22. * any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  32. *
  33. * RELEASE-TAG: IUCV network driver $Revision: 1.66 $
  34. *
  35. */
  36. #undef DEBUG
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/kernel.h>
  40. #include <linux/slab.h>
  41. #include <linux/errno.h>
  42. #include <linux/types.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/timer.h>
  45. #include <linux/sched.h>
  46. #include <linux/bitops.h>
  47. #include <linux/signal.h>
  48. #include <linux/string.h>
  49. #include <linux/device.h>
  50. #include <linux/ip.h>
  51. #include <linux/if_arp.h>
  52. #include <linux/tcp.h>
  53. #include <linux/skbuff.h>
  54. #include <linux/ctype.h>
  55. #include <net/dst.h>
  56. #include <asm/io.h>
  57. #include <asm/uaccess.h>
  58. #include "iucv.h"
  59. #include "fsm.h"
  60. MODULE_AUTHOR
  61. ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
  62. MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
  63. #define PRINTK_HEADER " iucv: " /* for debugging */
  64. static struct device_driver netiucv_driver = {
  65. .name = "netiucv",
  66. .bus = &iucv_bus,
  67. };
  68. /**
  69. * Per connection profiling data
  70. */
  71. struct connection_profile {
  72. unsigned long maxmulti;
  73. unsigned long maxcqueue;
  74. unsigned long doios_single;
  75. unsigned long doios_multi;
  76. unsigned long txlen;
  77. unsigned long tx_time;
  78. struct timespec send_stamp;
  79. unsigned long tx_pending;
  80. unsigned long tx_max_pending;
  81. };
  82. /**
  83. * Representation of one iucv connection
  84. */
  85. struct iucv_connection {
  86. struct iucv_connection *next;
  87. iucv_handle_t handle;
  88. __u16 pathid;
  89. struct sk_buff *rx_buff;
  90. struct sk_buff *tx_buff;
  91. struct sk_buff_head collect_queue;
  92. struct sk_buff_head commit_queue;
  93. spinlock_t collect_lock;
  94. int collect_len;
  95. int max_buffsize;
  96. fsm_timer timer;
  97. fsm_instance *fsm;
  98. struct net_device *netdev;
  99. struct connection_profile prof;
  100. char userid[9];
  101. };
  102. /**
  103. * Linked list of all connection structs.
  104. */
  105. static struct iucv_connection *iucv_connections;
  106. /**
  107. * Representation of event-data for the
  108. * connection state machine.
  109. */
  110. struct iucv_event {
  111. struct iucv_connection *conn;
  112. void *data;
  113. };
  114. /**
  115. * Private part of the network device structure
  116. */
  117. struct netiucv_priv {
  118. struct net_device_stats stats;
  119. unsigned long tbusy;
  120. fsm_instance *fsm;
  121. struct iucv_connection *conn;
  122. struct device *dev;
  123. };
  124. /**
  125. * Link level header for a packet.
  126. */
  127. typedef struct ll_header_t {
  128. __u16 next;
  129. } ll_header;
  130. #define NETIUCV_HDRLEN (sizeof(ll_header))
  131. #define NETIUCV_BUFSIZE_MAX 32768
  132. #define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX
  133. #define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
  134. #define NETIUCV_MTU_DEFAULT 9216
  135. #define NETIUCV_QUEUELEN_DEFAULT 50
  136. #define NETIUCV_TIMEOUT_5SEC 5000
  137. /**
  138. * Compatibility macros for busy handling
  139. * of network devices.
  140. */
  141. static __inline__ void netiucv_clear_busy(struct net_device *dev)
  142. {
  143. clear_bit(0, &(((struct netiucv_priv *)dev->priv)->tbusy));
  144. netif_wake_queue(dev);
  145. }
  146. static __inline__ int netiucv_test_and_set_busy(struct net_device *dev)
  147. {
  148. netif_stop_queue(dev);
  149. return test_and_set_bit(0, &((struct netiucv_priv *)dev->priv)->tbusy);
  150. }
  151. static __u8 iucv_host[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  152. static __u8 iucvMagic[16] = {
  153. 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
  154. 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
  155. };
  156. /**
  157. * This mask means the 16-byte IUCV "magic" and the origin userid must
  158. * match exactly as specified in order to give connection_pending()
  159. * control.
  160. */
  161. static __u8 netiucv_mask[] = {
  162. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  163. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  164. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  165. };
  166. /**
  167. * Convert an iucv userId to its printable
  168. * form (strip whitespace at end).
  169. *
  170. * @param An iucv userId
  171. *
  172. * @returns The printable string (static data!!)
  173. */
  174. static __inline__ char *
  175. netiucv_printname(char *name)
  176. {
  177. static char tmp[9];
  178. char *p = tmp;
  179. memcpy(tmp, name, 8);
  180. tmp[8] = '\0';
  181. while (*p && (!isspace(*p)))
  182. p++;
  183. *p = '\0';
  184. return tmp;
  185. }
  186. /**
  187. * States of the interface statemachine.
  188. */
  189. enum dev_states {
  190. DEV_STATE_STOPPED,
  191. DEV_STATE_STARTWAIT,
  192. DEV_STATE_STOPWAIT,
  193. DEV_STATE_RUNNING,
  194. /**
  195. * MUST be always the last element!!
  196. */
  197. NR_DEV_STATES
  198. };
  199. static const char *dev_state_names[] = {
  200. "Stopped",
  201. "StartWait",
  202. "StopWait",
  203. "Running",
  204. };
  205. /**
  206. * Events of the interface statemachine.
  207. */
  208. enum dev_events {
  209. DEV_EVENT_START,
  210. DEV_EVENT_STOP,
  211. DEV_EVENT_CONUP,
  212. DEV_EVENT_CONDOWN,
  213. /**
  214. * MUST be always the last element!!
  215. */
  216. NR_DEV_EVENTS
  217. };
  218. static const char *dev_event_names[] = {
  219. "Start",
  220. "Stop",
  221. "Connection up",
  222. "Connection down",
  223. };
  224. /**
  225. * Events of the connection statemachine
  226. */
  227. enum conn_events {
  228. /**
  229. * Events, representing callbacks from
  230. * lowlevel iucv layer)
  231. */
  232. CONN_EVENT_CONN_REQ,
  233. CONN_EVENT_CONN_ACK,
  234. CONN_EVENT_CONN_REJ,
  235. CONN_EVENT_CONN_SUS,
  236. CONN_EVENT_CONN_RES,
  237. CONN_EVENT_RX,
  238. CONN_EVENT_TXDONE,
  239. /**
  240. * Events, representing errors return codes from
  241. * calls to lowlevel iucv layer
  242. */
  243. /**
  244. * Event, representing timer expiry.
  245. */
  246. CONN_EVENT_TIMER,
  247. /**
  248. * Events, representing commands from upper levels.
  249. */
  250. CONN_EVENT_START,
  251. CONN_EVENT_STOP,
  252. /**
  253. * MUST be always the last element!!
  254. */
  255. NR_CONN_EVENTS,
  256. };
  257. static const char *conn_event_names[] = {
  258. "Remote connection request",
  259. "Remote connection acknowledge",
  260. "Remote connection reject",
  261. "Connection suspended",
  262. "Connection resumed",
  263. "Data received",
  264. "Data sent",
  265. "Timer",
  266. "Start",
  267. "Stop",
  268. };
  269. /**
  270. * States of the connection statemachine.
  271. */
  272. enum conn_states {
  273. /**
  274. * Connection not assigned to any device,
  275. * initial state, invalid
  276. */
  277. CONN_STATE_INVALID,
  278. /**
  279. * Userid assigned but not operating
  280. */
  281. CONN_STATE_STOPPED,
  282. /**
  283. * Connection registered,
  284. * no connection request sent yet,
  285. * no connection request received
  286. */
  287. CONN_STATE_STARTWAIT,
  288. /**
  289. * Connection registered and connection request sent,
  290. * no acknowledge and no connection request received yet.
  291. */
  292. CONN_STATE_SETUPWAIT,
  293. /**
  294. * Connection up and running idle
  295. */
  296. CONN_STATE_IDLE,
  297. /**
  298. * Data sent, awaiting CONN_EVENT_TXDONE
  299. */
  300. CONN_STATE_TX,
  301. /**
  302. * Error during registration.
  303. */
  304. CONN_STATE_REGERR,
  305. /**
  306. * Error during registration.
  307. */
  308. CONN_STATE_CONNERR,
  309. /**
  310. * MUST be always the last element!!
  311. */
  312. NR_CONN_STATES,
  313. };
  314. static const char *conn_state_names[] = {
  315. "Invalid",
  316. "Stopped",
  317. "StartWait",
  318. "SetupWait",
  319. "Idle",
  320. "TX",
  321. "Terminating",
  322. "Registration error",
  323. "Connect error",
  324. };
  325. /**
  326. * Debug Facility Stuff
  327. */
  328. static debug_info_t *iucv_dbf_setup = NULL;
  329. static debug_info_t *iucv_dbf_data = NULL;
  330. static debug_info_t *iucv_dbf_trace = NULL;
  331. DEFINE_PER_CPU(char[256], iucv_dbf_txt_buf);
  332. static void
  333. iucv_unregister_dbf_views(void)
  334. {
  335. if (iucv_dbf_setup)
  336. debug_unregister(iucv_dbf_setup);
  337. if (iucv_dbf_data)
  338. debug_unregister(iucv_dbf_data);
  339. if (iucv_dbf_trace)
  340. debug_unregister(iucv_dbf_trace);
  341. }
  342. static int
  343. iucv_register_dbf_views(void)
  344. {
  345. iucv_dbf_setup = debug_register(IUCV_DBF_SETUP_NAME,
  346. IUCV_DBF_SETUP_PAGES,
  347. IUCV_DBF_SETUP_NR_AREAS,
  348. IUCV_DBF_SETUP_LEN);
  349. iucv_dbf_data = debug_register(IUCV_DBF_DATA_NAME,
  350. IUCV_DBF_DATA_PAGES,
  351. IUCV_DBF_DATA_NR_AREAS,
  352. IUCV_DBF_DATA_LEN);
  353. iucv_dbf_trace = debug_register(IUCV_DBF_TRACE_NAME,
  354. IUCV_DBF_TRACE_PAGES,
  355. IUCV_DBF_TRACE_NR_AREAS,
  356. IUCV_DBF_TRACE_LEN);
  357. if ((iucv_dbf_setup == NULL) || (iucv_dbf_data == NULL) ||
  358. (iucv_dbf_trace == NULL)) {
  359. iucv_unregister_dbf_views();
  360. return -ENOMEM;
  361. }
  362. debug_register_view(iucv_dbf_setup, &debug_hex_ascii_view);
  363. debug_set_level(iucv_dbf_setup, IUCV_DBF_SETUP_LEVEL);
  364. debug_register_view(iucv_dbf_data, &debug_hex_ascii_view);
  365. debug_set_level(iucv_dbf_data, IUCV_DBF_DATA_LEVEL);
  366. debug_register_view(iucv_dbf_trace, &debug_hex_ascii_view);
  367. debug_set_level(iucv_dbf_trace, IUCV_DBF_TRACE_LEVEL);
  368. return 0;
  369. }
  370. /**
  371. * Callback-wrappers, called from lowlevel iucv layer.
  372. *****************************************************************************/
  373. static void
  374. netiucv_callback_rx(iucv_MessagePending *eib, void *pgm_data)
  375. {
  376. struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
  377. struct iucv_event ev;
  378. ev.conn = conn;
  379. ev.data = (void *)eib;
  380. fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
  381. }
  382. static void
  383. netiucv_callback_txdone(iucv_MessageComplete *eib, void *pgm_data)
  384. {
  385. struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
  386. struct iucv_event ev;
  387. ev.conn = conn;
  388. ev.data = (void *)eib;
  389. fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
  390. }
  391. static void
  392. netiucv_callback_connack(iucv_ConnectionComplete *eib, void *pgm_data)
  393. {
  394. struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
  395. struct iucv_event ev;
  396. ev.conn = conn;
  397. ev.data = (void *)eib;
  398. fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, &ev);
  399. }
  400. static void
  401. netiucv_callback_connreq(iucv_ConnectionPending *eib, void *pgm_data)
  402. {
  403. struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
  404. struct iucv_event ev;
  405. ev.conn = conn;
  406. ev.data = (void *)eib;
  407. fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
  408. }
  409. static void
  410. netiucv_callback_connrej(iucv_ConnectionSevered *eib, void *pgm_data)
  411. {
  412. struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
  413. struct iucv_event ev;
  414. ev.conn = conn;
  415. ev.data = (void *)eib;
  416. fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, &ev);
  417. }
  418. static void
  419. netiucv_callback_connsusp(iucv_ConnectionQuiesced *eib, void *pgm_data)
  420. {
  421. struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
  422. struct iucv_event ev;
  423. ev.conn = conn;
  424. ev.data = (void *)eib;
  425. fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, &ev);
  426. }
  427. static void
  428. netiucv_callback_connres(iucv_ConnectionResumed *eib, void *pgm_data)
  429. {
  430. struct iucv_connection *conn = (struct iucv_connection *)pgm_data;
  431. struct iucv_event ev;
  432. ev.conn = conn;
  433. ev.data = (void *)eib;
  434. fsm_event(conn->fsm, CONN_EVENT_CONN_RES, &ev);
  435. }
  436. static iucv_interrupt_ops_t netiucv_ops = {
  437. .ConnectionPending = netiucv_callback_connreq,
  438. .ConnectionComplete = netiucv_callback_connack,
  439. .ConnectionSevered = netiucv_callback_connrej,
  440. .ConnectionQuiesced = netiucv_callback_connsusp,
  441. .ConnectionResumed = netiucv_callback_connres,
  442. .MessagePending = netiucv_callback_rx,
  443. .MessageComplete = netiucv_callback_txdone
  444. };
  445. /**
  446. * Dummy NOP action for all statemachines
  447. */
  448. static void
  449. fsm_action_nop(fsm_instance *fi, int event, void *arg)
  450. {
  451. }
  452. /**
  453. * Actions of the connection statemachine
  454. *****************************************************************************/
  455. /**
  456. * Helper function for conn_action_rx()
  457. * Unpack a just received skb and hand it over to
  458. * upper layers.
  459. *
  460. * @param conn The connection where this skb has been received.
  461. * @param pskb The received skb.
  462. */
  463. //static __inline__ void
  464. static void
  465. netiucv_unpack_skb(struct iucv_connection *conn, struct sk_buff *pskb)
  466. {
  467. struct net_device *dev = conn->netdev;
  468. struct netiucv_priv *privptr = dev->priv;
  469. __u16 offset = 0;
  470. skb_put(pskb, NETIUCV_HDRLEN);
  471. pskb->dev = dev;
  472. pskb->ip_summed = CHECKSUM_NONE;
  473. pskb->protocol = ntohs(ETH_P_IP);
  474. while (1) {
  475. struct sk_buff *skb;
  476. ll_header *header = (ll_header *)pskb->data;
  477. if (!header->next)
  478. break;
  479. skb_pull(pskb, NETIUCV_HDRLEN);
  480. header->next -= offset;
  481. offset += header->next;
  482. header->next -= NETIUCV_HDRLEN;
  483. if (skb_tailroom(pskb) < header->next) {
  484. PRINT_WARN("%s: Illegal next field in iucv header: "
  485. "%d > %d\n",
  486. dev->name, header->next, skb_tailroom(pskb));
  487. IUCV_DBF_TEXT_(data, 2, "Illegal next field: %d > %d\n",
  488. header->next, skb_tailroom(pskb));
  489. return;
  490. }
  491. skb_put(pskb, header->next);
  492. pskb->mac.raw = pskb->data;
  493. skb = dev_alloc_skb(pskb->len);
  494. if (!skb) {
  495. PRINT_WARN("%s Out of memory in netiucv_unpack_skb\n",
  496. dev->name);
  497. IUCV_DBF_TEXT(data, 2,
  498. "Out of memory in netiucv_unpack_skb\n");
  499. privptr->stats.rx_dropped++;
  500. return;
  501. }
  502. memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
  503. skb->mac.raw = skb->data;
  504. skb->dev = pskb->dev;
  505. skb->protocol = pskb->protocol;
  506. pskb->ip_summed = CHECKSUM_UNNECESSARY;
  507. /*
  508. * Since receiving is always initiated from a tasklet (in iucv.c),
  509. * we must use netif_rx_ni() instead of netif_rx()
  510. */
  511. netif_rx_ni(skb);
  512. dev->last_rx = jiffies;
  513. privptr->stats.rx_packets++;
  514. privptr->stats.rx_bytes += skb->len;
  515. skb_pull(pskb, header->next);
  516. skb_put(pskb, NETIUCV_HDRLEN);
  517. }
  518. }
  519. static void
  520. conn_action_rx(fsm_instance *fi, int event, void *arg)
  521. {
  522. struct iucv_event *ev = (struct iucv_event *)arg;
  523. struct iucv_connection *conn = ev->conn;
  524. iucv_MessagePending *eib = (iucv_MessagePending *)ev->data;
  525. struct netiucv_priv *privptr =(struct netiucv_priv *)conn->netdev->priv;
  526. __u32 msglen = eib->ln1msg2.ipbfln1f;
  527. int rc;
  528. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  529. if (!conn->netdev) {
  530. /* FRITZ: How to tell iucv LL to drop the msg? */
  531. PRINT_WARN("Received data for unlinked connection\n");
  532. IUCV_DBF_TEXT(data, 2,
  533. "Received data for unlinked connection\n");
  534. return;
  535. }
  536. if (msglen > conn->max_buffsize) {
  537. /* FRITZ: How to tell iucv LL to drop the msg? */
  538. privptr->stats.rx_dropped++;
  539. PRINT_WARN("msglen %d > max_buffsize %d\n",
  540. msglen, conn->max_buffsize);
  541. IUCV_DBF_TEXT_(data, 2, "msglen %d > max_buffsize %d\n",
  542. msglen, conn->max_buffsize);
  543. return;
  544. }
  545. conn->rx_buff->data = conn->rx_buff->tail = conn->rx_buff->head;
  546. conn->rx_buff->len = 0;
  547. rc = iucv_receive(conn->pathid, eib->ipmsgid, eib->iptrgcls,
  548. conn->rx_buff->data, msglen, NULL, NULL, NULL);
  549. if (rc || msglen < 5) {
  550. privptr->stats.rx_errors++;
  551. PRINT_WARN("iucv_receive returned %08x\n", rc);
  552. IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_receive\n", rc);
  553. return;
  554. }
  555. netiucv_unpack_skb(conn, conn->rx_buff);
  556. }
  557. static void
  558. conn_action_txdone(fsm_instance *fi, int event, void *arg)
  559. {
  560. struct iucv_event *ev = (struct iucv_event *)arg;
  561. struct iucv_connection *conn = ev->conn;
  562. iucv_MessageComplete *eib = (iucv_MessageComplete *)ev->data;
  563. struct netiucv_priv *privptr = NULL;
  564. /* Shut up, gcc! skb is always below 2G. */
  565. __u32 single_flag = eib->ipmsgtag;
  566. __u32 txbytes = 0;
  567. __u32 txpackets = 0;
  568. __u32 stat_maxcq = 0;
  569. struct sk_buff *skb;
  570. unsigned long saveflags;
  571. ll_header header;
  572. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  573. if (conn && conn->netdev && conn->netdev->priv)
  574. privptr = (struct netiucv_priv *)conn->netdev->priv;
  575. conn->prof.tx_pending--;
  576. if (single_flag) {
  577. if ((skb = skb_dequeue(&conn->commit_queue))) {
  578. atomic_dec(&skb->users);
  579. dev_kfree_skb_any(skb);
  580. if (privptr) {
  581. privptr->stats.tx_packets++;
  582. privptr->stats.tx_bytes +=
  583. (skb->len - NETIUCV_HDRLEN
  584. - NETIUCV_HDRLEN);
  585. }
  586. }
  587. }
  588. conn->tx_buff->data = conn->tx_buff->tail = conn->tx_buff->head;
  589. conn->tx_buff->len = 0;
  590. spin_lock_irqsave(&conn->collect_lock, saveflags);
  591. while ((skb = skb_dequeue(&conn->collect_queue))) {
  592. header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
  593. memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
  594. NETIUCV_HDRLEN);
  595. memcpy(skb_put(conn->tx_buff, skb->len), skb->data, skb->len);
  596. txbytes += skb->len;
  597. txpackets++;
  598. stat_maxcq++;
  599. atomic_dec(&skb->users);
  600. dev_kfree_skb_any(skb);
  601. }
  602. if (conn->collect_len > conn->prof.maxmulti)
  603. conn->prof.maxmulti = conn->collect_len;
  604. conn->collect_len = 0;
  605. spin_unlock_irqrestore(&conn->collect_lock, saveflags);
  606. if (conn->tx_buff->len) {
  607. int rc;
  608. header.next = 0;
  609. memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
  610. NETIUCV_HDRLEN);
  611. conn->prof.send_stamp = xtime;
  612. rc = iucv_send(conn->pathid, NULL, 0, 0, 0, 0,
  613. conn->tx_buff->data, conn->tx_buff->len);
  614. conn->prof.doios_multi++;
  615. conn->prof.txlen += conn->tx_buff->len;
  616. conn->prof.tx_pending++;
  617. if (conn->prof.tx_pending > conn->prof.tx_max_pending)
  618. conn->prof.tx_max_pending = conn->prof.tx_pending;
  619. if (rc) {
  620. conn->prof.tx_pending--;
  621. fsm_newstate(fi, CONN_STATE_IDLE);
  622. if (privptr)
  623. privptr->stats.tx_errors += txpackets;
  624. PRINT_WARN("iucv_send returned %08x\n", rc);
  625. IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
  626. } else {
  627. if (privptr) {
  628. privptr->stats.tx_packets += txpackets;
  629. privptr->stats.tx_bytes += txbytes;
  630. }
  631. if (stat_maxcq > conn->prof.maxcqueue)
  632. conn->prof.maxcqueue = stat_maxcq;
  633. }
  634. } else
  635. fsm_newstate(fi, CONN_STATE_IDLE);
  636. }
  637. static void
  638. conn_action_connaccept(fsm_instance *fi, int event, void *arg)
  639. {
  640. struct iucv_event *ev = (struct iucv_event *)arg;
  641. struct iucv_connection *conn = ev->conn;
  642. iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
  643. struct net_device *netdev = conn->netdev;
  644. struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
  645. int rc;
  646. __u16 msglimit;
  647. __u8 udata[16];
  648. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  649. rc = iucv_accept(eib->ippathid, NETIUCV_QUEUELEN_DEFAULT, udata, 0,
  650. conn->handle, conn, NULL, &msglimit);
  651. if (rc) {
  652. PRINT_WARN("%s: IUCV accept failed with error %d\n",
  653. netdev->name, rc);
  654. IUCV_DBF_TEXT_(setup, 2, "rc %d from iucv_accept", rc);
  655. return;
  656. }
  657. fsm_newstate(fi, CONN_STATE_IDLE);
  658. conn->pathid = eib->ippathid;
  659. netdev->tx_queue_len = msglimit;
  660. fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
  661. }
  662. static void
  663. conn_action_connreject(fsm_instance *fi, int event, void *arg)
  664. {
  665. struct iucv_event *ev = (struct iucv_event *)arg;
  666. struct iucv_connection *conn = ev->conn;
  667. struct net_device *netdev = conn->netdev;
  668. iucv_ConnectionPending *eib = (iucv_ConnectionPending *)ev->data;
  669. __u8 udata[16];
  670. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  671. iucv_sever(eib->ippathid, udata);
  672. if (eib->ippathid != conn->pathid) {
  673. PRINT_INFO("%s: IR Connection Pending; "
  674. "pathid %d does not match original pathid %d\n",
  675. netdev->name, eib->ippathid, conn->pathid);
  676. IUCV_DBF_TEXT_(data, 2,
  677. "connreject: IR pathid %d, conn. pathid %d\n",
  678. eib->ippathid, conn->pathid);
  679. iucv_sever(conn->pathid, udata);
  680. }
  681. }
  682. static void
  683. conn_action_connack(fsm_instance *fi, int event, void *arg)
  684. {
  685. struct iucv_event *ev = (struct iucv_event *)arg;
  686. struct iucv_connection *conn = ev->conn;
  687. iucv_ConnectionComplete *eib = (iucv_ConnectionComplete *)ev->data;
  688. struct net_device *netdev = conn->netdev;
  689. struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
  690. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  691. fsm_deltimer(&conn->timer);
  692. fsm_newstate(fi, CONN_STATE_IDLE);
  693. if (eib->ippathid != conn->pathid) {
  694. PRINT_INFO("%s: IR Connection Complete; "
  695. "pathid %d does not match original pathid %d\n",
  696. netdev->name, eib->ippathid, conn->pathid);
  697. IUCV_DBF_TEXT_(data, 2,
  698. "connack: IR pathid %d, conn. pathid %d\n",
  699. eib->ippathid, conn->pathid);
  700. conn->pathid = eib->ippathid;
  701. }
  702. netdev->tx_queue_len = eib->ipmsglim;
  703. fsm_event(privptr->fsm, DEV_EVENT_CONUP, netdev);
  704. }
  705. static void
  706. conn_action_conntimsev(fsm_instance *fi, int event, void *arg)
  707. {
  708. struct iucv_connection *conn = (struct iucv_connection *)arg;
  709. __u8 udata[16];
  710. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  711. fsm_deltimer(&conn->timer);
  712. iucv_sever(conn->pathid, udata);
  713. fsm_newstate(fi, CONN_STATE_STARTWAIT);
  714. }
  715. static void
  716. conn_action_connsever(fsm_instance *fi, int event, void *arg)
  717. {
  718. struct iucv_event *ev = (struct iucv_event *)arg;
  719. struct iucv_connection *conn = ev->conn;
  720. struct net_device *netdev = conn->netdev;
  721. struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
  722. __u8 udata[16];
  723. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  724. fsm_deltimer(&conn->timer);
  725. iucv_sever(conn->pathid, udata);
  726. PRINT_INFO("%s: Remote dropped connection\n", netdev->name);
  727. IUCV_DBF_TEXT(data, 2,
  728. "conn_action_connsever: Remote dropped connection\n");
  729. fsm_newstate(fi, CONN_STATE_STARTWAIT);
  730. fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
  731. }
  732. static void
  733. conn_action_start(fsm_instance *fi, int event, void *arg)
  734. {
  735. struct iucv_event *ev = (struct iucv_event *)arg;
  736. struct iucv_connection *conn = ev->conn;
  737. __u16 msglimit;
  738. int rc;
  739. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  740. if (!conn->handle) {
  741. IUCV_DBF_TEXT(trace, 5, "calling iucv_register_program\n");
  742. conn->handle =
  743. iucv_register_program(iucvMagic, conn->userid,
  744. netiucv_mask,
  745. &netiucv_ops, conn);
  746. fsm_newstate(fi, CONN_STATE_STARTWAIT);
  747. if (!conn->handle) {
  748. fsm_newstate(fi, CONN_STATE_REGERR);
  749. conn->handle = NULL;
  750. IUCV_DBF_TEXT(setup, 2,
  751. "NULL from iucv_register_program\n");
  752. return;
  753. }
  754. PRINT_DEBUG("%s('%s'): registered successfully\n",
  755. conn->netdev->name, conn->userid);
  756. }
  757. PRINT_DEBUG("%s('%s'): connecting ...\n",
  758. conn->netdev->name, conn->userid);
  759. /* We must set the state before calling iucv_connect because the callback
  760. * handler could be called at any point after the connection request is
  761. * sent */
  762. fsm_newstate(fi, CONN_STATE_SETUPWAIT);
  763. rc = iucv_connect(&(conn->pathid), NETIUCV_QUEUELEN_DEFAULT, iucvMagic,
  764. conn->userid, iucv_host, 0, NULL, &msglimit,
  765. conn->handle, conn);
  766. switch (rc) {
  767. case 0:
  768. conn->netdev->tx_queue_len = msglimit;
  769. fsm_addtimer(&conn->timer, NETIUCV_TIMEOUT_5SEC,
  770. CONN_EVENT_TIMER, conn);
  771. return;
  772. case 11:
  773. PRINT_INFO("%s: User %s is currently not available.\n",
  774. conn->netdev->name,
  775. netiucv_printname(conn->userid));
  776. fsm_newstate(fi, CONN_STATE_STARTWAIT);
  777. return;
  778. case 12:
  779. PRINT_INFO("%s: User %s is currently not ready.\n",
  780. conn->netdev->name,
  781. netiucv_printname(conn->userid));
  782. fsm_newstate(fi, CONN_STATE_STARTWAIT);
  783. return;
  784. case 13:
  785. PRINT_WARN("%s: Too many IUCV connections.\n",
  786. conn->netdev->name);
  787. fsm_newstate(fi, CONN_STATE_CONNERR);
  788. break;
  789. case 14:
  790. PRINT_WARN(
  791. "%s: User %s has too many IUCV connections.\n",
  792. conn->netdev->name,
  793. netiucv_printname(conn->userid));
  794. fsm_newstate(fi, CONN_STATE_CONNERR);
  795. break;
  796. case 15:
  797. PRINT_WARN(
  798. "%s: No IUCV authorization in CP directory.\n",
  799. conn->netdev->name);
  800. fsm_newstate(fi, CONN_STATE_CONNERR);
  801. break;
  802. default:
  803. PRINT_WARN("%s: iucv_connect returned error %d\n",
  804. conn->netdev->name, rc);
  805. fsm_newstate(fi, CONN_STATE_CONNERR);
  806. break;
  807. }
  808. IUCV_DBF_TEXT_(setup, 5, "iucv_connect rc is %d\n", rc);
  809. IUCV_DBF_TEXT(trace, 5, "calling iucv_unregister_program\n");
  810. iucv_unregister_program(conn->handle);
  811. conn->handle = NULL;
  812. }
  813. static void
  814. netiucv_purge_skb_queue(struct sk_buff_head *q)
  815. {
  816. struct sk_buff *skb;
  817. while ((skb = skb_dequeue(q))) {
  818. atomic_dec(&skb->users);
  819. dev_kfree_skb_any(skb);
  820. }
  821. }
  822. static void
  823. conn_action_stop(fsm_instance *fi, int event, void *arg)
  824. {
  825. struct iucv_event *ev = (struct iucv_event *)arg;
  826. struct iucv_connection *conn = ev->conn;
  827. struct net_device *netdev = conn->netdev;
  828. struct netiucv_priv *privptr = (struct netiucv_priv *)netdev->priv;
  829. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  830. fsm_deltimer(&conn->timer);
  831. fsm_newstate(fi, CONN_STATE_STOPPED);
  832. netiucv_purge_skb_queue(&conn->collect_queue);
  833. if (conn->handle)
  834. IUCV_DBF_TEXT(trace, 5, "calling iucv_unregister_program\n");
  835. iucv_unregister_program(conn->handle);
  836. conn->handle = NULL;
  837. netiucv_purge_skb_queue(&conn->commit_queue);
  838. fsm_event(privptr->fsm, DEV_EVENT_CONDOWN, netdev);
  839. }
  840. static void
  841. conn_action_inval(fsm_instance *fi, int event, void *arg)
  842. {
  843. struct iucv_event *ev = (struct iucv_event *)arg;
  844. struct iucv_connection *conn = ev->conn;
  845. struct net_device *netdev = conn->netdev;
  846. PRINT_WARN("%s: Cannot connect without username\n",
  847. netdev->name);
  848. IUCV_DBF_TEXT(data, 2, "conn_action_inval called\n");
  849. }
  850. static const fsm_node conn_fsm[] = {
  851. { CONN_STATE_INVALID, CONN_EVENT_START, conn_action_inval },
  852. { CONN_STATE_STOPPED, CONN_EVENT_START, conn_action_start },
  853. { CONN_STATE_STOPPED, CONN_EVENT_STOP, conn_action_stop },
  854. { CONN_STATE_STARTWAIT, CONN_EVENT_STOP, conn_action_stop },
  855. { CONN_STATE_SETUPWAIT, CONN_EVENT_STOP, conn_action_stop },
  856. { CONN_STATE_IDLE, CONN_EVENT_STOP, conn_action_stop },
  857. { CONN_STATE_TX, CONN_EVENT_STOP, conn_action_stop },
  858. { CONN_STATE_REGERR, CONN_EVENT_STOP, conn_action_stop },
  859. { CONN_STATE_CONNERR, CONN_EVENT_STOP, conn_action_stop },
  860. { CONN_STATE_STOPPED, CONN_EVENT_CONN_REQ, conn_action_connreject },
  861. { CONN_STATE_STARTWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
  862. { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REQ, conn_action_connaccept },
  863. { CONN_STATE_IDLE, CONN_EVENT_CONN_REQ, conn_action_connreject },
  864. { CONN_STATE_TX, CONN_EVENT_CONN_REQ, conn_action_connreject },
  865. { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_ACK, conn_action_connack },
  866. { CONN_STATE_SETUPWAIT, CONN_EVENT_TIMER, conn_action_conntimsev },
  867. { CONN_STATE_SETUPWAIT, CONN_EVENT_CONN_REJ, conn_action_connsever },
  868. { CONN_STATE_IDLE, CONN_EVENT_CONN_REJ, conn_action_connsever },
  869. { CONN_STATE_TX, CONN_EVENT_CONN_REJ, conn_action_connsever },
  870. { CONN_STATE_IDLE, CONN_EVENT_RX, conn_action_rx },
  871. { CONN_STATE_TX, CONN_EVENT_RX, conn_action_rx },
  872. { CONN_STATE_TX, CONN_EVENT_TXDONE, conn_action_txdone },
  873. { CONN_STATE_IDLE, CONN_EVENT_TXDONE, conn_action_txdone },
  874. };
  875. static const int CONN_FSM_LEN = sizeof(conn_fsm) / sizeof(fsm_node);
  876. /**
  877. * Actions for interface - statemachine.
  878. *****************************************************************************/
  879. /**
  880. * Startup connection by sending CONN_EVENT_START to it.
  881. *
  882. * @param fi An instance of an interface statemachine.
  883. * @param event The event, just happened.
  884. * @param arg Generic pointer, casted from struct net_device * upon call.
  885. */
  886. static void
  887. dev_action_start(fsm_instance *fi, int event, void *arg)
  888. {
  889. struct net_device *dev = (struct net_device *)arg;
  890. struct netiucv_priv *privptr = dev->priv;
  891. struct iucv_event ev;
  892. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  893. ev.conn = privptr->conn;
  894. fsm_newstate(fi, DEV_STATE_STARTWAIT);
  895. fsm_event(privptr->conn->fsm, CONN_EVENT_START, &ev);
  896. }
  897. /**
  898. * Shutdown connection by sending CONN_EVENT_STOP to it.
  899. *
  900. * @param fi An instance of an interface statemachine.
  901. * @param event The event, just happened.
  902. * @param arg Generic pointer, casted from struct net_device * upon call.
  903. */
  904. static void
  905. dev_action_stop(fsm_instance *fi, int event, void *arg)
  906. {
  907. struct net_device *dev = (struct net_device *)arg;
  908. struct netiucv_priv *privptr = dev->priv;
  909. struct iucv_event ev;
  910. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  911. ev.conn = privptr->conn;
  912. fsm_newstate(fi, DEV_STATE_STOPWAIT);
  913. fsm_event(privptr->conn->fsm, CONN_EVENT_STOP, &ev);
  914. }
  915. /**
  916. * Called from connection statemachine
  917. * when a connection is up and running.
  918. *
  919. * @param fi An instance of an interface statemachine.
  920. * @param event The event, just happened.
  921. * @param arg Generic pointer, casted from struct net_device * upon call.
  922. */
  923. static void
  924. dev_action_connup(fsm_instance *fi, int event, void *arg)
  925. {
  926. struct net_device *dev = (struct net_device *)arg;
  927. struct netiucv_priv *privptr = dev->priv;
  928. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  929. switch (fsm_getstate(fi)) {
  930. case DEV_STATE_STARTWAIT:
  931. fsm_newstate(fi, DEV_STATE_RUNNING);
  932. PRINT_INFO("%s: connected with remote side %s\n",
  933. dev->name, privptr->conn->userid);
  934. IUCV_DBF_TEXT(setup, 3,
  935. "connection is up and running\n");
  936. break;
  937. case DEV_STATE_STOPWAIT:
  938. PRINT_INFO(
  939. "%s: got connection UP event during shutdown!\n",
  940. dev->name);
  941. IUCV_DBF_TEXT(data, 2,
  942. "dev_action_connup: in DEV_STATE_STOPWAIT\n");
  943. break;
  944. }
  945. }
  946. /**
  947. * Called from connection statemachine
  948. * when a connection has been shutdown.
  949. *
  950. * @param fi An instance of an interface statemachine.
  951. * @param event The event, just happened.
  952. * @param arg Generic pointer, casted from struct net_device * upon call.
  953. */
  954. static void
  955. dev_action_conndown(fsm_instance *fi, int event, void *arg)
  956. {
  957. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  958. switch (fsm_getstate(fi)) {
  959. case DEV_STATE_RUNNING:
  960. fsm_newstate(fi, DEV_STATE_STARTWAIT);
  961. break;
  962. case DEV_STATE_STOPWAIT:
  963. fsm_newstate(fi, DEV_STATE_STOPPED);
  964. IUCV_DBF_TEXT(setup, 3, "connection is down\n");
  965. break;
  966. }
  967. }
  968. static const fsm_node dev_fsm[] = {
  969. { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start },
  970. { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start },
  971. { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
  972. { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop },
  973. { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup },
  974. { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
  975. { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown },
  976. { DEV_STATE_RUNNING, DEV_EVENT_CONUP, fsm_action_nop },
  977. };
  978. static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
  979. /**
  980. * Transmit a packet.
  981. * This is a helper function for netiucv_tx().
  982. *
  983. * @param conn Connection to be used for sending.
  984. * @param skb Pointer to struct sk_buff of packet to send.
  985. * The linklevel header has already been set up
  986. * by netiucv_tx().
  987. *
  988. * @return 0 on success, -ERRNO on failure. (Never fails.)
  989. */
  990. static int
  991. netiucv_transmit_skb(struct iucv_connection *conn, struct sk_buff *skb) {
  992. unsigned long saveflags;
  993. ll_header header;
  994. int rc = 0;
  995. if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
  996. int l = skb->len + NETIUCV_HDRLEN;
  997. spin_lock_irqsave(&conn->collect_lock, saveflags);
  998. if (conn->collect_len + l >
  999. (conn->max_buffsize - NETIUCV_HDRLEN)) {
  1000. rc = -EBUSY;
  1001. IUCV_DBF_TEXT(data, 2,
  1002. "EBUSY from netiucv_transmit_skb\n");
  1003. } else {
  1004. atomic_inc(&skb->users);
  1005. skb_queue_tail(&conn->collect_queue, skb);
  1006. conn->collect_len += l;
  1007. }
  1008. spin_unlock_irqrestore(&conn->collect_lock, saveflags);
  1009. } else {
  1010. struct sk_buff *nskb = skb;
  1011. /**
  1012. * Copy the skb to a new allocated skb in lowmem only if the
  1013. * data is located above 2G in memory or tailroom is < 2.
  1014. */
  1015. unsigned long hi =
  1016. ((unsigned long)(skb->tail + NETIUCV_HDRLEN)) >> 31;
  1017. int copied = 0;
  1018. if (hi || (skb_tailroom(skb) < 2)) {
  1019. nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
  1020. NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
  1021. if (!nskb) {
  1022. PRINT_WARN("%s: Could not allocate tx_skb\n",
  1023. conn->netdev->name);
  1024. IUCV_DBF_TEXT(data, 2, "alloc_skb failed\n");
  1025. rc = -ENOMEM;
  1026. return rc;
  1027. } else {
  1028. skb_reserve(nskb, NETIUCV_HDRLEN);
  1029. memcpy(skb_put(nskb, skb->len),
  1030. skb->data, skb->len);
  1031. }
  1032. copied = 1;
  1033. }
  1034. /**
  1035. * skb now is below 2G and has enough room. Add headers.
  1036. */
  1037. header.next = nskb->len + NETIUCV_HDRLEN;
  1038. memcpy(skb_push(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
  1039. header.next = 0;
  1040. memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
  1041. fsm_newstate(conn->fsm, CONN_STATE_TX);
  1042. conn->prof.send_stamp = xtime;
  1043. rc = iucv_send(conn->pathid, NULL, 0, 0, 1 /* single_flag */,
  1044. 0, nskb->data, nskb->len);
  1045. /* Shut up, gcc! nskb is always below 2G. */
  1046. conn->prof.doios_single++;
  1047. conn->prof.txlen += skb->len;
  1048. conn->prof.tx_pending++;
  1049. if (conn->prof.tx_pending > conn->prof.tx_max_pending)
  1050. conn->prof.tx_max_pending = conn->prof.tx_pending;
  1051. if (rc) {
  1052. struct netiucv_priv *privptr;
  1053. fsm_newstate(conn->fsm, CONN_STATE_IDLE);
  1054. conn->prof.tx_pending--;
  1055. privptr = (struct netiucv_priv *)conn->netdev->priv;
  1056. if (privptr)
  1057. privptr->stats.tx_errors++;
  1058. if (copied)
  1059. dev_kfree_skb(nskb);
  1060. else {
  1061. /**
  1062. * Remove our headers. They get added
  1063. * again on retransmit.
  1064. */
  1065. skb_pull(skb, NETIUCV_HDRLEN);
  1066. skb_trim(skb, skb->len - NETIUCV_HDRLEN);
  1067. }
  1068. PRINT_WARN("iucv_send returned %08x\n", rc);
  1069. IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_send\n", rc);
  1070. } else {
  1071. if (copied)
  1072. dev_kfree_skb(skb);
  1073. atomic_inc(&nskb->users);
  1074. skb_queue_tail(&conn->commit_queue, nskb);
  1075. }
  1076. }
  1077. return rc;
  1078. }
  1079. /**
  1080. * Interface API for upper network layers
  1081. *****************************************************************************/
  1082. /**
  1083. * Open an interface.
  1084. * Called from generic network layer when ifconfig up is run.
  1085. *
  1086. * @param dev Pointer to interface struct.
  1087. *
  1088. * @return 0 on success, -ERRNO on failure. (Never fails.)
  1089. */
  1090. static int
  1091. netiucv_open(struct net_device *dev) {
  1092. fsm_event(((struct netiucv_priv *)dev->priv)->fsm, DEV_EVENT_START,dev);
  1093. return 0;
  1094. }
  1095. /**
  1096. * Close an interface.
  1097. * Called from generic network layer when ifconfig down is run.
  1098. *
  1099. * @param dev Pointer to interface struct.
  1100. *
  1101. * @return 0 on success, -ERRNO on failure. (Never fails.)
  1102. */
  1103. static int
  1104. netiucv_close(struct net_device *dev) {
  1105. fsm_event(((struct netiucv_priv *)dev->priv)->fsm, DEV_EVENT_STOP, dev);
  1106. return 0;
  1107. }
  1108. /**
  1109. * Start transmission of a packet.
  1110. * Called from generic network device layer.
  1111. *
  1112. * @param skb Pointer to buffer containing the packet.
  1113. * @param dev Pointer to interface struct.
  1114. *
  1115. * @return 0 if packet consumed, !0 if packet rejected.
  1116. * Note: If we return !0, then the packet is free'd by
  1117. * the generic network layer.
  1118. */
  1119. static int netiucv_tx(struct sk_buff *skb, struct net_device *dev)
  1120. {
  1121. int rc = 0;
  1122. struct netiucv_priv *privptr = dev->priv;
  1123. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1124. /**
  1125. * Some sanity checks ...
  1126. */
  1127. if (skb == NULL) {
  1128. PRINT_WARN("%s: NULL sk_buff passed\n", dev->name);
  1129. IUCV_DBF_TEXT(data, 2, "netiucv_tx: skb is NULL\n");
  1130. privptr->stats.tx_dropped++;
  1131. return 0;
  1132. }
  1133. if (skb_headroom(skb) < NETIUCV_HDRLEN) {
  1134. PRINT_WARN("%s: Got sk_buff with head room < %ld bytes\n",
  1135. dev->name, NETIUCV_HDRLEN);
  1136. IUCV_DBF_TEXT(data, 2,
  1137. "netiucv_tx: skb_headroom < NETIUCV_HDRLEN\n");
  1138. dev_kfree_skb(skb);
  1139. privptr->stats.tx_dropped++;
  1140. return 0;
  1141. }
  1142. /**
  1143. * If connection is not running, try to restart it
  1144. * and throw away packet.
  1145. */
  1146. if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
  1147. fsm_event(privptr->fsm, DEV_EVENT_START, dev);
  1148. dev_kfree_skb(skb);
  1149. privptr->stats.tx_dropped++;
  1150. privptr->stats.tx_errors++;
  1151. privptr->stats.tx_carrier_errors++;
  1152. return 0;
  1153. }
  1154. if (netiucv_test_and_set_busy(dev)) {
  1155. IUCV_DBF_TEXT(data, 2, "EBUSY from netiucv_tx\n");
  1156. return -EBUSY;
  1157. }
  1158. dev->trans_start = jiffies;
  1159. if (netiucv_transmit_skb(privptr->conn, skb))
  1160. rc = 1;
  1161. netiucv_clear_busy(dev);
  1162. return rc;
  1163. }
  1164. /**
  1165. * Returns interface statistics of a device.
  1166. *
  1167. * @param dev Pointer to interface struct.
  1168. *
  1169. * @return Pointer to stats struct of this interface.
  1170. */
  1171. static struct net_device_stats *
  1172. netiucv_stats (struct net_device * dev)
  1173. {
  1174. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1175. return &((struct netiucv_priv *)dev->priv)->stats;
  1176. }
  1177. /**
  1178. * Sets MTU of an interface.
  1179. *
  1180. * @param dev Pointer to interface struct.
  1181. * @param new_mtu The new MTU to use for this interface.
  1182. *
  1183. * @return 0 on success, -EINVAL if MTU is out of valid range.
  1184. * (valid range is 576 .. NETIUCV_MTU_MAX).
  1185. */
  1186. static int
  1187. netiucv_change_mtu (struct net_device * dev, int new_mtu)
  1188. {
  1189. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1190. if ((new_mtu < 576) || (new_mtu > NETIUCV_MTU_MAX)) {
  1191. IUCV_DBF_TEXT(setup, 2, "given MTU out of valid range\n");
  1192. return -EINVAL;
  1193. }
  1194. dev->mtu = new_mtu;
  1195. return 0;
  1196. }
  1197. /**
  1198. * attributes in sysfs
  1199. *****************************************************************************/
  1200. static ssize_t
  1201. user_show (struct device *dev, struct device_attribute *attr, char *buf)
  1202. {
  1203. struct netiucv_priv *priv = dev->driver_data;
  1204. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1205. return sprintf(buf, "%s\n", netiucv_printname(priv->conn->userid));
  1206. }
  1207. static ssize_t
  1208. user_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1209. {
  1210. struct netiucv_priv *priv = dev->driver_data;
  1211. struct net_device *ndev = priv->conn->netdev;
  1212. char *p;
  1213. char *tmp;
  1214. char username[10];
  1215. int i;
  1216. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1217. if (count>9) {
  1218. PRINT_WARN("netiucv: username too long (%d)!\n", (int)count);
  1219. IUCV_DBF_TEXT_(setup, 2,
  1220. "%d is length of username\n", (int)count);
  1221. return -EINVAL;
  1222. }
  1223. tmp = strsep((char **) &buf, "\n");
  1224. for (i=0, p=tmp; i<8 && *p; i++, p++) {
  1225. if (isalnum(*p) || (*p == '$'))
  1226. username[i]= *p;
  1227. else if (*p == '\n') {
  1228. /* trailing lf, grr */
  1229. break;
  1230. } else {
  1231. PRINT_WARN("netiucv: Invalid char %c in username!\n",
  1232. *p);
  1233. IUCV_DBF_TEXT_(setup, 2,
  1234. "username: invalid character %c\n",
  1235. *p);
  1236. return -EINVAL;
  1237. }
  1238. }
  1239. while (i<9)
  1240. username[i++] = ' ';
  1241. username[9] = '\0';
  1242. if (memcmp(username, priv->conn->userid, 8)) {
  1243. /* username changed */
  1244. if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
  1245. PRINT_WARN(
  1246. "netiucv: device %s active, connected to %s\n",
  1247. dev->bus_id, priv->conn->userid);
  1248. PRINT_WARN("netiucv: user cannot be updated\n");
  1249. IUCV_DBF_TEXT(setup, 2, "user_write: device active\n");
  1250. return -EBUSY;
  1251. }
  1252. }
  1253. memcpy(priv->conn->userid, username, 9);
  1254. return count;
  1255. }
  1256. static DEVICE_ATTR(user, 0644, user_show, user_write);
  1257. static ssize_t
  1258. buffer_show (struct device *dev, struct device_attribute *attr, char *buf)
  1259. {
  1260. struct netiucv_priv *priv = dev->driver_data;
  1261. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1262. return sprintf(buf, "%d\n", priv->conn->max_buffsize);
  1263. }
  1264. static ssize_t
  1265. buffer_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1266. {
  1267. struct netiucv_priv *priv = dev->driver_data;
  1268. struct net_device *ndev = priv->conn->netdev;
  1269. char *e;
  1270. int bs1;
  1271. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1272. if (count >= 39)
  1273. return -EINVAL;
  1274. bs1 = simple_strtoul(buf, &e, 0);
  1275. if (e && (!isspace(*e))) {
  1276. PRINT_WARN("netiucv: Invalid character in buffer!\n");
  1277. IUCV_DBF_TEXT_(setup, 2, "buffer_write: invalid char %c\n", *e);
  1278. return -EINVAL;
  1279. }
  1280. if (bs1 > NETIUCV_BUFSIZE_MAX) {
  1281. PRINT_WARN("netiucv: Given buffer size %d too large.\n",
  1282. bs1);
  1283. IUCV_DBF_TEXT_(setup, 2,
  1284. "buffer_write: buffer size %d too large\n",
  1285. bs1);
  1286. return -EINVAL;
  1287. }
  1288. if ((ndev->flags & IFF_RUNNING) &&
  1289. (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2))) {
  1290. PRINT_WARN("netiucv: Given buffer size %d too small.\n",
  1291. bs1);
  1292. IUCV_DBF_TEXT_(setup, 2,
  1293. "buffer_write: buffer size %d too small\n",
  1294. bs1);
  1295. return -EINVAL;
  1296. }
  1297. if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN)) {
  1298. PRINT_WARN("netiucv: Given buffer size %d too small.\n",
  1299. bs1);
  1300. IUCV_DBF_TEXT_(setup, 2,
  1301. "buffer_write: buffer size %d too small\n",
  1302. bs1);
  1303. return -EINVAL;
  1304. }
  1305. priv->conn->max_buffsize = bs1;
  1306. if (!(ndev->flags & IFF_RUNNING))
  1307. ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
  1308. return count;
  1309. }
  1310. static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
  1311. static ssize_t
  1312. dev_fsm_show (struct device *dev, struct device_attribute *attr, char *buf)
  1313. {
  1314. struct netiucv_priv *priv = dev->driver_data;
  1315. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1316. return sprintf(buf, "%s\n", fsm_getstate_str(priv->fsm));
  1317. }
  1318. static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
  1319. static ssize_t
  1320. conn_fsm_show (struct device *dev, struct device_attribute *attr, char *buf)
  1321. {
  1322. struct netiucv_priv *priv = dev->driver_data;
  1323. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1324. return sprintf(buf, "%s\n", fsm_getstate_str(priv->conn->fsm));
  1325. }
  1326. static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
  1327. static ssize_t
  1328. maxmulti_show (struct device *dev, struct device_attribute *attr, char *buf)
  1329. {
  1330. struct netiucv_priv *priv = dev->driver_data;
  1331. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1332. return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
  1333. }
  1334. static ssize_t
  1335. maxmulti_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1336. {
  1337. struct netiucv_priv *priv = dev->driver_data;
  1338. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1339. priv->conn->prof.maxmulti = 0;
  1340. return count;
  1341. }
  1342. static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
  1343. static ssize_t
  1344. maxcq_show (struct device *dev, struct device_attribute *attr, char *buf)
  1345. {
  1346. struct netiucv_priv *priv = dev->driver_data;
  1347. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1348. return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
  1349. }
  1350. static ssize_t
  1351. maxcq_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1352. {
  1353. struct netiucv_priv *priv = dev->driver_data;
  1354. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1355. priv->conn->prof.maxcqueue = 0;
  1356. return count;
  1357. }
  1358. static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
  1359. static ssize_t
  1360. sdoio_show (struct device *dev, struct device_attribute *attr, char *buf)
  1361. {
  1362. struct netiucv_priv *priv = dev->driver_data;
  1363. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1364. return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
  1365. }
  1366. static ssize_t
  1367. sdoio_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1368. {
  1369. struct netiucv_priv *priv = dev->driver_data;
  1370. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1371. priv->conn->prof.doios_single = 0;
  1372. return count;
  1373. }
  1374. static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
  1375. static ssize_t
  1376. mdoio_show (struct device *dev, struct device_attribute *attr, char *buf)
  1377. {
  1378. struct netiucv_priv *priv = dev->driver_data;
  1379. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1380. return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
  1381. }
  1382. static ssize_t
  1383. mdoio_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1384. {
  1385. struct netiucv_priv *priv = dev->driver_data;
  1386. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1387. priv->conn->prof.doios_multi = 0;
  1388. return count;
  1389. }
  1390. static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
  1391. static ssize_t
  1392. txlen_show (struct device *dev, struct device_attribute *attr, char *buf)
  1393. {
  1394. struct netiucv_priv *priv = dev->driver_data;
  1395. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1396. return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
  1397. }
  1398. static ssize_t
  1399. txlen_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1400. {
  1401. struct netiucv_priv *priv = dev->driver_data;
  1402. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1403. priv->conn->prof.txlen = 0;
  1404. return count;
  1405. }
  1406. static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
  1407. static ssize_t
  1408. txtime_show (struct device *dev, struct device_attribute *attr, char *buf)
  1409. {
  1410. struct netiucv_priv *priv = dev->driver_data;
  1411. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1412. return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
  1413. }
  1414. static ssize_t
  1415. txtime_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1416. {
  1417. struct netiucv_priv *priv = dev->driver_data;
  1418. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1419. priv->conn->prof.tx_time = 0;
  1420. return count;
  1421. }
  1422. static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
  1423. static ssize_t
  1424. txpend_show (struct device *dev, struct device_attribute *attr, char *buf)
  1425. {
  1426. struct netiucv_priv *priv = dev->driver_data;
  1427. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1428. return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending);
  1429. }
  1430. static ssize_t
  1431. txpend_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1432. {
  1433. struct netiucv_priv *priv = dev->driver_data;
  1434. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1435. priv->conn->prof.tx_pending = 0;
  1436. return count;
  1437. }
  1438. static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write);
  1439. static ssize_t
  1440. txmpnd_show (struct device *dev, struct device_attribute *attr, char *buf)
  1441. {
  1442. struct netiucv_priv *priv = dev->driver_data;
  1443. IUCV_DBF_TEXT(trace, 5, __FUNCTION__);
  1444. return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending);
  1445. }
  1446. static ssize_t
  1447. txmpnd_write (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1448. {
  1449. struct netiucv_priv *priv = dev->driver_data;
  1450. IUCV_DBF_TEXT(trace, 4, __FUNCTION__);
  1451. priv->conn->prof.tx_max_pending = 0;
  1452. return count;
  1453. }
  1454. static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write);
  1455. static struct attribute *netiucv_attrs[] = {
  1456. &dev_attr_buffer.attr,
  1457. &dev_attr_user.attr,
  1458. NULL,
  1459. };
  1460. static struct attribute_group netiucv_attr_group = {
  1461. .attrs = netiucv_attrs,
  1462. };
  1463. static struct attribute *netiucv_stat_attrs[] = {
  1464. &dev_attr_device_fsm_state.attr,
  1465. &dev_attr_connection_fsm_state.attr,
  1466. &dev_attr_max_tx_buffer_used.attr,
  1467. &dev_attr_max_chained_skbs.attr,
  1468. &dev_attr_tx_single_write_ops.attr,
  1469. &dev_attr_tx_multi_write_ops.attr,
  1470. &dev_attr_netto_bytes.attr,
  1471. &dev_attr_max_tx_io_time.attr,
  1472. &dev_attr_tx_pending.attr,
  1473. &dev_attr_tx_max_pending.attr,
  1474. NULL,
  1475. };
  1476. static struct attribute_group netiucv_stat_attr_group = {
  1477. .name = "stats",
  1478. .attrs = netiucv_stat_attrs,
  1479. };
  1480. static inline int
  1481. netiucv_add_files(struct device *dev)
  1482. {
  1483. int ret;
  1484. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1485. ret = sysfs_create_group(&dev->kobj, &netiucv_attr_group);
  1486. if (ret)
  1487. return ret;
  1488. ret = sysfs_create_group(&dev->kobj, &netiucv_stat_attr_group);
  1489. if (ret)
  1490. sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
  1491. return ret;
  1492. }
  1493. static inline void
  1494. netiucv_remove_files(struct device *dev)
  1495. {
  1496. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1497. sysfs_remove_group(&dev->kobj, &netiucv_stat_attr_group);
  1498. sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
  1499. }
  1500. static int
  1501. netiucv_register_device(struct net_device *ndev)
  1502. {
  1503. struct netiucv_priv *priv = ndev->priv;
  1504. struct device *dev = kmalloc(sizeof(struct device), GFP_KERNEL);
  1505. int ret;
  1506. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1507. if (dev) {
  1508. memset(dev, 0, sizeof(struct device));
  1509. snprintf(dev->bus_id, BUS_ID_SIZE, "net%s", ndev->name);
  1510. dev->bus = &iucv_bus;
  1511. dev->parent = iucv_root;
  1512. /*
  1513. * The release function could be called after the
  1514. * module has been unloaded. It's _only_ task is to
  1515. * free the struct. Therefore, we specify kfree()
  1516. * directly here. (Probably a little bit obfuscating
  1517. * but legitime ...).
  1518. */
  1519. dev->release = (void (*)(struct device *))kfree;
  1520. dev->driver = &netiucv_driver;
  1521. } else
  1522. return -ENOMEM;
  1523. ret = device_register(dev);
  1524. if (ret)
  1525. return ret;
  1526. ret = netiucv_add_files(dev);
  1527. if (ret)
  1528. goto out_unreg;
  1529. priv->dev = dev;
  1530. dev->driver_data = priv;
  1531. return 0;
  1532. out_unreg:
  1533. device_unregister(dev);
  1534. return ret;
  1535. }
  1536. static void
  1537. netiucv_unregister_device(struct device *dev)
  1538. {
  1539. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1540. netiucv_remove_files(dev);
  1541. device_unregister(dev);
  1542. }
  1543. /**
  1544. * Allocate and initialize a new connection structure.
  1545. * Add it to the list of netiucv connections;
  1546. */
  1547. static struct iucv_connection *
  1548. netiucv_new_connection(struct net_device *dev, char *username)
  1549. {
  1550. struct iucv_connection **clist = &iucv_connections;
  1551. struct iucv_connection *conn =
  1552. (struct iucv_connection *)
  1553. kmalloc(sizeof(struct iucv_connection), GFP_KERNEL);
  1554. if (conn) {
  1555. memset(conn, 0, sizeof(struct iucv_connection));
  1556. skb_queue_head_init(&conn->collect_queue);
  1557. skb_queue_head_init(&conn->commit_queue);
  1558. conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
  1559. conn->netdev = dev;
  1560. conn->rx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT,
  1561. GFP_KERNEL | GFP_DMA);
  1562. if (!conn->rx_buff) {
  1563. kfree(conn);
  1564. return NULL;
  1565. }
  1566. conn->tx_buff = alloc_skb(NETIUCV_BUFSIZE_DEFAULT,
  1567. GFP_KERNEL | GFP_DMA);
  1568. if (!conn->tx_buff) {
  1569. kfree_skb(conn->rx_buff);
  1570. kfree(conn);
  1571. return NULL;
  1572. }
  1573. conn->fsm = init_fsm("netiucvconn", conn_state_names,
  1574. conn_event_names, NR_CONN_STATES,
  1575. NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
  1576. GFP_KERNEL);
  1577. if (!conn->fsm) {
  1578. kfree_skb(conn->tx_buff);
  1579. kfree_skb(conn->rx_buff);
  1580. kfree(conn);
  1581. return NULL;
  1582. }
  1583. fsm_settimer(conn->fsm, &conn->timer);
  1584. fsm_newstate(conn->fsm, CONN_STATE_INVALID);
  1585. if (username) {
  1586. memcpy(conn->userid, username, 9);
  1587. fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
  1588. }
  1589. conn->next = *clist;
  1590. *clist = conn;
  1591. }
  1592. return conn;
  1593. }
  1594. /**
  1595. * Release a connection structure and remove it from the
  1596. * list of netiucv connections.
  1597. */
  1598. static void
  1599. netiucv_remove_connection(struct iucv_connection *conn)
  1600. {
  1601. struct iucv_connection **clist = &iucv_connections;
  1602. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1603. if (conn == NULL)
  1604. return;
  1605. while (*clist) {
  1606. if (*clist == conn) {
  1607. *clist = conn->next;
  1608. if (conn->handle) {
  1609. iucv_unregister_program(conn->handle);
  1610. conn->handle = NULL;
  1611. }
  1612. fsm_deltimer(&conn->timer);
  1613. kfree_fsm(conn->fsm);
  1614. kfree_skb(conn->rx_buff);
  1615. kfree_skb(conn->tx_buff);
  1616. return;
  1617. }
  1618. clist = &((*clist)->next);
  1619. }
  1620. }
  1621. /**
  1622. * Release everything of a net device.
  1623. */
  1624. static void
  1625. netiucv_free_netdevice(struct net_device *dev)
  1626. {
  1627. struct netiucv_priv *privptr;
  1628. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1629. if (!dev)
  1630. return;
  1631. privptr = (struct netiucv_priv *)dev->priv;
  1632. if (privptr) {
  1633. if (privptr->conn)
  1634. netiucv_remove_connection(privptr->conn);
  1635. if (privptr->fsm)
  1636. kfree_fsm(privptr->fsm);
  1637. privptr->conn = NULL; privptr->fsm = NULL;
  1638. /* privptr gets freed by free_netdev() */
  1639. }
  1640. free_netdev(dev);
  1641. }
  1642. /**
  1643. * Initialize a net device. (Called from kernel in alloc_netdev())
  1644. */
  1645. static void
  1646. netiucv_setup_netdevice(struct net_device *dev)
  1647. {
  1648. memset(dev->priv, 0, sizeof(struct netiucv_priv));
  1649. dev->mtu = NETIUCV_MTU_DEFAULT;
  1650. dev->hard_start_xmit = netiucv_tx;
  1651. dev->open = netiucv_open;
  1652. dev->stop = netiucv_close;
  1653. dev->get_stats = netiucv_stats;
  1654. dev->change_mtu = netiucv_change_mtu;
  1655. dev->destructor = netiucv_free_netdevice;
  1656. dev->hard_header_len = NETIUCV_HDRLEN;
  1657. dev->addr_len = 0;
  1658. dev->type = ARPHRD_SLIP;
  1659. dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT;
  1660. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  1661. SET_MODULE_OWNER(dev);
  1662. }
  1663. /**
  1664. * Allocate and initialize everything of a net device.
  1665. */
  1666. static struct net_device *
  1667. netiucv_init_netdevice(char *username)
  1668. {
  1669. struct netiucv_priv *privptr;
  1670. struct net_device *dev;
  1671. dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d",
  1672. netiucv_setup_netdevice);
  1673. if (!dev)
  1674. return NULL;
  1675. if (dev_alloc_name(dev, dev->name) < 0) {
  1676. free_netdev(dev);
  1677. return NULL;
  1678. }
  1679. privptr = (struct netiucv_priv *)dev->priv;
  1680. privptr->fsm = init_fsm("netiucvdev", dev_state_names,
  1681. dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
  1682. dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
  1683. if (!privptr->fsm) {
  1684. free_netdev(dev);
  1685. return NULL;
  1686. }
  1687. privptr->conn = netiucv_new_connection(dev, username);
  1688. if (!privptr->conn) {
  1689. kfree_fsm(privptr->fsm);
  1690. free_netdev(dev);
  1691. IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_new_connection\n");
  1692. return NULL;
  1693. }
  1694. fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
  1695. return dev;
  1696. }
  1697. static ssize_t
  1698. conn_write(struct device_driver *drv, const char *buf, size_t count)
  1699. {
  1700. char *p;
  1701. char username[10];
  1702. int i, ret;
  1703. struct net_device *dev;
  1704. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1705. if (count>9) {
  1706. PRINT_WARN("netiucv: username too long (%d)!\n", (int)count);
  1707. IUCV_DBF_TEXT(setup, 2, "conn_write: too long\n");
  1708. return -EINVAL;
  1709. }
  1710. for (i=0, p=(char *)buf; i<8 && *p; i++, p++) {
  1711. if (isalnum(*p) || (*p == '$'))
  1712. username[i]= *p;
  1713. else if (*p == '\n') {
  1714. /* trailing lf, grr */
  1715. break;
  1716. } else {
  1717. PRINT_WARN("netiucv: Invalid character in username!\n");
  1718. IUCV_DBF_TEXT_(setup, 2,
  1719. "conn_write: invalid character %c\n", *p);
  1720. return -EINVAL;
  1721. }
  1722. }
  1723. while (i<9)
  1724. username[i++] = ' ';
  1725. username[9] = '\0';
  1726. dev = netiucv_init_netdevice(username);
  1727. if (!dev) {
  1728. PRINT_WARN(
  1729. "netiucv: Could not allocate network device structure "
  1730. "for user '%s'\n", netiucv_printname(username));
  1731. IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_init_netdevice\n");
  1732. return -ENODEV;
  1733. }
  1734. if ((ret = netiucv_register_device(dev))) {
  1735. IUCV_DBF_TEXT_(setup, 2,
  1736. "ret %d from netiucv_register_device\n", ret);
  1737. goto out_free_ndev;
  1738. }
  1739. /* sysfs magic */
  1740. SET_NETDEV_DEV(dev,
  1741. (struct device*)((struct netiucv_priv*)dev->priv)->dev);
  1742. if ((ret = register_netdev(dev))) {
  1743. netiucv_unregister_device((struct device*)
  1744. ((struct netiucv_priv*)dev->priv)->dev);
  1745. goto out_free_ndev;
  1746. }
  1747. PRINT_INFO("%s: '%s'\n", dev->name, netiucv_printname(username));
  1748. return count;
  1749. out_free_ndev:
  1750. PRINT_WARN("netiucv: Could not register '%s'\n", dev->name);
  1751. IUCV_DBF_TEXT(setup, 2, "conn_write: could not register\n");
  1752. netiucv_free_netdevice(dev);
  1753. return ret;
  1754. }
  1755. DRIVER_ATTR(connection, 0200, NULL, conn_write);
  1756. static ssize_t
  1757. remove_write (struct device_driver *drv, const char *buf, size_t count)
  1758. {
  1759. struct iucv_connection **clist = &iucv_connections;
  1760. struct net_device *ndev;
  1761. struct netiucv_priv *priv;
  1762. struct device *dev;
  1763. char name[IFNAMSIZ];
  1764. char *p;
  1765. int i;
  1766. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1767. if (count >= IFNAMSIZ)
  1768. count = IFNAMSIZ-1;
  1769. for (i=0, p=(char *)buf; i<count && *p; i++, p++) {
  1770. if ((*p == '\n') | (*p == ' ')) {
  1771. /* trailing lf, grr */
  1772. break;
  1773. } else {
  1774. name[i]=*p;
  1775. }
  1776. }
  1777. name[i] = '\0';
  1778. while (*clist) {
  1779. ndev = (*clist)->netdev;
  1780. priv = (struct netiucv_priv*)ndev->priv;
  1781. dev = priv->dev;
  1782. if (strncmp(name, ndev->name, count)) {
  1783. clist = &((*clist)->next);
  1784. continue;
  1785. }
  1786. if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
  1787. PRINT_WARN(
  1788. "netiucv: net device %s active with peer %s\n",
  1789. ndev->name, priv->conn->userid);
  1790. PRINT_WARN("netiucv: %s cannot be removed\n",
  1791. ndev->name);
  1792. IUCV_DBF_TEXT(data, 2, "remove_write: still active\n");
  1793. return -EBUSY;
  1794. }
  1795. unregister_netdev(ndev);
  1796. netiucv_unregister_device(dev);
  1797. return count;
  1798. }
  1799. PRINT_WARN("netiucv: net device %s unknown\n", name);
  1800. IUCV_DBF_TEXT(data, 2, "remove_write: unknown device\n");
  1801. return -EINVAL;
  1802. }
  1803. DRIVER_ATTR(remove, 0200, NULL, remove_write);
  1804. static void
  1805. netiucv_banner(void)
  1806. {
  1807. char vbuf[] = "$Revision: 1.66 $";
  1808. char *version = vbuf;
  1809. if ((version = strchr(version, ':'))) {
  1810. char *p = strchr(version + 1, '$');
  1811. if (p)
  1812. *p = '\0';
  1813. } else
  1814. version = " ??? ";
  1815. PRINT_INFO("NETIUCV driver Version%s initialized\n", version);
  1816. }
  1817. static void __exit
  1818. netiucv_exit(void)
  1819. {
  1820. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1821. while (iucv_connections) {
  1822. struct net_device *ndev = iucv_connections->netdev;
  1823. struct netiucv_priv *priv = (struct netiucv_priv*)ndev->priv;
  1824. struct device *dev = priv->dev;
  1825. unregister_netdev(ndev);
  1826. netiucv_unregister_device(dev);
  1827. }
  1828. driver_remove_file(&netiucv_driver, &driver_attr_connection);
  1829. driver_remove_file(&netiucv_driver, &driver_attr_remove);
  1830. driver_unregister(&netiucv_driver);
  1831. iucv_unregister_dbf_views();
  1832. PRINT_INFO("NETIUCV driver unloaded\n");
  1833. return;
  1834. }
  1835. static int __init
  1836. netiucv_init(void)
  1837. {
  1838. int ret;
  1839. ret = iucv_register_dbf_views();
  1840. if (ret) {
  1841. PRINT_WARN("netiucv_init failed, "
  1842. "iucv_register_dbf_views rc = %d\n", ret);
  1843. return ret;
  1844. }
  1845. IUCV_DBF_TEXT(trace, 3, __FUNCTION__);
  1846. ret = driver_register(&netiucv_driver);
  1847. if (ret) {
  1848. PRINT_ERR("NETIUCV: failed to register driver.\n");
  1849. IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_register\n", ret);
  1850. iucv_unregister_dbf_views();
  1851. return ret;
  1852. }
  1853. /* Add entry for specifying connections. */
  1854. ret = driver_create_file(&netiucv_driver, &driver_attr_connection);
  1855. if (!ret) {
  1856. ret = driver_create_file(&netiucv_driver, &driver_attr_remove);
  1857. netiucv_banner();
  1858. } else {
  1859. PRINT_ERR("NETIUCV: failed to add driver attribute.\n");
  1860. IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_create_file\n", ret);
  1861. driver_unregister(&netiucv_driver);
  1862. iucv_unregister_dbf_views();
  1863. }
  1864. return ret;
  1865. }
  1866. module_init(netiucv_init);
  1867. module_exit(netiucv_exit);
  1868. MODULE_LICENSE("GPL");