netiucv.c 55 KB

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