netiucv.c 56 KB

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