netiucv.c 57 KB

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