netiucv.c 56 KB

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