netiucv.c 57 KB

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