netiucv.c 56 KB

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