netiucv.c 57 KB

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