netiucv.c 56 KB

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