netiucv.c 57 KB

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