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