netiucv.c 52 KB

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