netiucv.c 56 KB

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