netiucv.c 56 KB

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