netiucv.c 57 KB

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