netiucv.c 54 KB

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