netiucv.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246
  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 <asm/uaccess.h>
  60. #include <net/iucv/iucv.h>
  61. #include "fsm.h"
  62. MODULE_AUTHOR
  63. ("(C) 2001 IBM Corporation by Fritz Elfert (felfert@millenux.com)");
  64. MODULE_DESCRIPTION ("Linux for S/390 IUCV network driver");
  65. /**
  66. * Debug Facility stuff
  67. */
  68. #define IUCV_DBF_SETUP_NAME "iucv_setup"
  69. #define IUCV_DBF_SETUP_LEN 32
  70. #define IUCV_DBF_SETUP_PAGES 2
  71. #define IUCV_DBF_SETUP_NR_AREAS 1
  72. #define IUCV_DBF_SETUP_LEVEL 3
  73. #define IUCV_DBF_DATA_NAME "iucv_data"
  74. #define IUCV_DBF_DATA_LEN 128
  75. #define IUCV_DBF_DATA_PAGES 2
  76. #define IUCV_DBF_DATA_NR_AREAS 1
  77. #define IUCV_DBF_DATA_LEVEL 2
  78. #define IUCV_DBF_TRACE_NAME "iucv_trace"
  79. #define IUCV_DBF_TRACE_LEN 16
  80. #define IUCV_DBF_TRACE_PAGES 4
  81. #define IUCV_DBF_TRACE_NR_AREAS 1
  82. #define IUCV_DBF_TRACE_LEVEL 3
  83. #define IUCV_DBF_TEXT(name,level,text) \
  84. do { \
  85. debug_text_event(iucv_dbf_##name,level,text); \
  86. } while (0)
  87. #define IUCV_DBF_HEX(name,level,addr,len) \
  88. do { \
  89. debug_event(iucv_dbf_##name,level,(void*)(addr),len); \
  90. } while (0)
  91. DECLARE_PER_CPU(char[256], iucv_dbf_txt_buf);
  92. /* Allow to sort out low debug levels early to avoid wasted sprints */
  93. static inline int iucv_dbf_passes(debug_info_t *dbf_grp, int level)
  94. {
  95. return (level <= dbf_grp->level);
  96. }
  97. #define IUCV_DBF_TEXT_(name, level, text...) \
  98. do { \
  99. if (iucv_dbf_passes(iucv_dbf_##name, level)) { \
  100. char* iucv_dbf_txt_buf = \
  101. get_cpu_var(iucv_dbf_txt_buf); \
  102. sprintf(iucv_dbf_txt_buf, text); \
  103. debug_text_event(iucv_dbf_##name, level, \
  104. iucv_dbf_txt_buf); \
  105. put_cpu_var(iucv_dbf_txt_buf); \
  106. } \
  107. } while (0)
  108. #define IUCV_DBF_SPRINTF(name,level,text...) \
  109. do { \
  110. debug_sprintf_event(iucv_dbf_trace, level, ##text ); \
  111. debug_sprintf_event(iucv_dbf_trace, level, text ); \
  112. } while (0)
  113. /**
  114. * some more debug stuff
  115. */
  116. #define IUCV_HEXDUMP16(importance,header,ptr) \
  117. PRINT_##importance(header "%02x %02x %02x %02x %02x %02x %02x %02x " \
  118. "%02x %02x %02x %02x %02x %02x %02x %02x\n", \
  119. *(((char*)ptr)),*(((char*)ptr)+1),*(((char*)ptr)+2), \
  120. *(((char*)ptr)+3),*(((char*)ptr)+4),*(((char*)ptr)+5), \
  121. *(((char*)ptr)+6),*(((char*)ptr)+7),*(((char*)ptr)+8), \
  122. *(((char*)ptr)+9),*(((char*)ptr)+10),*(((char*)ptr)+11), \
  123. *(((char*)ptr)+12),*(((char*)ptr)+13), \
  124. *(((char*)ptr)+14),*(((char*)ptr)+15)); \
  125. PRINT_##importance(header "%02x %02x %02x %02x %02x %02x %02x %02x " \
  126. "%02x %02x %02x %02x %02x %02x %02x %02x\n", \
  127. *(((char*)ptr)+16),*(((char*)ptr)+17), \
  128. *(((char*)ptr)+18),*(((char*)ptr)+19), \
  129. *(((char*)ptr)+20),*(((char*)ptr)+21), \
  130. *(((char*)ptr)+22),*(((char*)ptr)+23), \
  131. *(((char*)ptr)+24),*(((char*)ptr)+25), \
  132. *(((char*)ptr)+26),*(((char*)ptr)+27), \
  133. *(((char*)ptr)+28),*(((char*)ptr)+29), \
  134. *(((char*)ptr)+30),*(((char*)ptr)+31));
  135. #define PRINTK_HEADER " iucv: " /* for debugging */
  136. /* dummy device to make sure netiucv_pm functions are called */
  137. static struct device *netiucv_dev;
  138. static int netiucv_pm_prepare(struct device *);
  139. static void netiucv_pm_complete(struct device *);
  140. static int netiucv_pm_freeze(struct device *);
  141. static int netiucv_pm_restore_thaw(struct device *);
  142. static struct dev_pm_ops netiucv_pm_ops = {
  143. .prepare = netiucv_pm_prepare,
  144. .complete = netiucv_pm_complete,
  145. .freeze = netiucv_pm_freeze,
  146. .thaw = netiucv_pm_restore_thaw,
  147. .restore = netiucv_pm_restore_thaw,
  148. };
  149. static struct device_driver netiucv_driver = {
  150. .owner = THIS_MODULE,
  151. .name = "netiucv",
  152. .bus = &iucv_bus,
  153. .pm = &netiucv_pm_ops,
  154. };
  155. static int netiucv_callback_connreq(struct iucv_path *,
  156. u8 ipvmid[8], u8 ipuser[16]);
  157. static void netiucv_callback_connack(struct iucv_path *, u8 ipuser[16]);
  158. static void netiucv_callback_connrej(struct iucv_path *, u8 ipuser[16]);
  159. static void netiucv_callback_connsusp(struct iucv_path *, u8 ipuser[16]);
  160. static void netiucv_callback_connres(struct iucv_path *, u8 ipuser[16]);
  161. static void netiucv_callback_rx(struct iucv_path *, struct iucv_message *);
  162. static void netiucv_callback_txdone(struct iucv_path *, struct iucv_message *);
  163. static struct iucv_handler netiucv_handler = {
  164. .path_pending = netiucv_callback_connreq,
  165. .path_complete = netiucv_callback_connack,
  166. .path_severed = netiucv_callback_connrej,
  167. .path_quiesced = netiucv_callback_connsusp,
  168. .path_resumed = netiucv_callback_connres,
  169. .message_pending = netiucv_callback_rx,
  170. .message_complete = netiucv_callback_txdone
  171. };
  172. /**
  173. * Per connection profiling data
  174. */
  175. struct connection_profile {
  176. unsigned long maxmulti;
  177. unsigned long maxcqueue;
  178. unsigned long doios_single;
  179. unsigned long doios_multi;
  180. unsigned long txlen;
  181. unsigned long tx_time;
  182. struct timespec send_stamp;
  183. unsigned long tx_pending;
  184. unsigned long tx_max_pending;
  185. };
  186. /**
  187. * Representation of one iucv connection
  188. */
  189. struct iucv_connection {
  190. struct list_head list;
  191. struct iucv_path *path;
  192. struct sk_buff *rx_buff;
  193. struct sk_buff *tx_buff;
  194. struct sk_buff_head collect_queue;
  195. struct sk_buff_head commit_queue;
  196. spinlock_t collect_lock;
  197. int collect_len;
  198. int max_buffsize;
  199. fsm_timer timer;
  200. fsm_instance *fsm;
  201. struct net_device *netdev;
  202. struct connection_profile prof;
  203. char userid[9];
  204. };
  205. /**
  206. * Linked list of all connection structs.
  207. */
  208. static LIST_HEAD(iucv_connection_list);
  209. static DEFINE_RWLOCK(iucv_connection_rwlock);
  210. /**
  211. * Representation of event-data for the
  212. * connection state machine.
  213. */
  214. struct iucv_event {
  215. struct iucv_connection *conn;
  216. void *data;
  217. };
  218. /**
  219. * Private part of the network device structure
  220. */
  221. struct netiucv_priv {
  222. struct net_device_stats stats;
  223. unsigned long tbusy;
  224. fsm_instance *fsm;
  225. struct iucv_connection *conn;
  226. struct device *dev;
  227. int pm_state;
  228. };
  229. /**
  230. * Link level header for a packet.
  231. */
  232. struct ll_header {
  233. u16 next;
  234. };
  235. #define NETIUCV_HDRLEN (sizeof(struct ll_header))
  236. #define NETIUCV_BUFSIZE_MAX 32768
  237. #define NETIUCV_BUFSIZE_DEFAULT NETIUCV_BUFSIZE_MAX
  238. #define NETIUCV_MTU_MAX (NETIUCV_BUFSIZE_MAX - NETIUCV_HDRLEN)
  239. #define NETIUCV_MTU_DEFAULT 9216
  240. #define NETIUCV_QUEUELEN_DEFAULT 50
  241. #define NETIUCV_TIMEOUT_5SEC 5000
  242. /**
  243. * Compatibility macros for busy handling
  244. * of network devices.
  245. */
  246. static inline void netiucv_clear_busy(struct net_device *dev)
  247. {
  248. struct netiucv_priv *priv = netdev_priv(dev);
  249. clear_bit(0, &priv->tbusy);
  250. netif_wake_queue(dev);
  251. }
  252. static inline int netiucv_test_and_set_busy(struct net_device *dev)
  253. {
  254. struct netiucv_priv *priv = netdev_priv(dev);
  255. netif_stop_queue(dev);
  256. return test_and_set_bit(0, &priv->tbusy);
  257. }
  258. static u8 iucvMagic[16] = {
  259. 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
  260. 0xF0, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40
  261. };
  262. /**
  263. * Convert an iucv userId to its printable
  264. * form (strip whitespace at end).
  265. *
  266. * @param An iucv userId
  267. *
  268. * @returns The printable string (static data!!)
  269. */
  270. static char *netiucv_printname(char *name)
  271. {
  272. static char tmp[9];
  273. char *p = tmp;
  274. memcpy(tmp, name, 8);
  275. tmp[8] = '\0';
  276. while (*p && (!isspace(*p)))
  277. p++;
  278. *p = '\0';
  279. return tmp;
  280. }
  281. /**
  282. * States of the interface statemachine.
  283. */
  284. enum dev_states {
  285. DEV_STATE_STOPPED,
  286. DEV_STATE_STARTWAIT,
  287. DEV_STATE_STOPWAIT,
  288. DEV_STATE_RUNNING,
  289. /**
  290. * MUST be always the last element!!
  291. */
  292. NR_DEV_STATES
  293. };
  294. static const char *dev_state_names[] = {
  295. "Stopped",
  296. "StartWait",
  297. "StopWait",
  298. "Running",
  299. };
  300. /**
  301. * Events of the interface statemachine.
  302. */
  303. enum dev_events {
  304. DEV_EVENT_START,
  305. DEV_EVENT_STOP,
  306. DEV_EVENT_CONUP,
  307. DEV_EVENT_CONDOWN,
  308. /**
  309. * MUST be always the last element!!
  310. */
  311. NR_DEV_EVENTS
  312. };
  313. static const char *dev_event_names[] = {
  314. "Start",
  315. "Stop",
  316. "Connection up",
  317. "Connection down",
  318. };
  319. /**
  320. * Events of the connection statemachine
  321. */
  322. enum conn_events {
  323. /**
  324. * Events, representing callbacks from
  325. * lowlevel iucv layer)
  326. */
  327. CONN_EVENT_CONN_REQ,
  328. CONN_EVENT_CONN_ACK,
  329. CONN_EVENT_CONN_REJ,
  330. CONN_EVENT_CONN_SUS,
  331. CONN_EVENT_CONN_RES,
  332. CONN_EVENT_RX,
  333. CONN_EVENT_TXDONE,
  334. /**
  335. * Events, representing errors return codes from
  336. * calls to lowlevel iucv layer
  337. */
  338. /**
  339. * Event, representing timer expiry.
  340. */
  341. CONN_EVENT_TIMER,
  342. /**
  343. * Events, representing commands from upper levels.
  344. */
  345. CONN_EVENT_START,
  346. CONN_EVENT_STOP,
  347. /**
  348. * MUST be always the last element!!
  349. */
  350. NR_CONN_EVENTS,
  351. };
  352. static const char *conn_event_names[] = {
  353. "Remote connection request",
  354. "Remote connection acknowledge",
  355. "Remote connection reject",
  356. "Connection suspended",
  357. "Connection resumed",
  358. "Data received",
  359. "Data sent",
  360. "Timer",
  361. "Start",
  362. "Stop",
  363. };
  364. /**
  365. * States of the connection statemachine.
  366. */
  367. enum conn_states {
  368. /**
  369. * Connection not assigned to any device,
  370. * initial state, invalid
  371. */
  372. CONN_STATE_INVALID,
  373. /**
  374. * Userid assigned but not operating
  375. */
  376. CONN_STATE_STOPPED,
  377. /**
  378. * Connection registered,
  379. * no connection request sent yet,
  380. * no connection request received
  381. */
  382. CONN_STATE_STARTWAIT,
  383. /**
  384. * Connection registered and connection request sent,
  385. * no acknowledge and no connection request received yet.
  386. */
  387. CONN_STATE_SETUPWAIT,
  388. /**
  389. * Connection up and running idle
  390. */
  391. CONN_STATE_IDLE,
  392. /**
  393. * Data sent, awaiting CONN_EVENT_TXDONE
  394. */
  395. CONN_STATE_TX,
  396. /**
  397. * Error during registration.
  398. */
  399. CONN_STATE_REGERR,
  400. /**
  401. * Error during registration.
  402. */
  403. CONN_STATE_CONNERR,
  404. /**
  405. * MUST be always the last element!!
  406. */
  407. NR_CONN_STATES,
  408. };
  409. static const char *conn_state_names[] = {
  410. "Invalid",
  411. "Stopped",
  412. "StartWait",
  413. "SetupWait",
  414. "Idle",
  415. "TX",
  416. "Terminating",
  417. "Registration error",
  418. "Connect error",
  419. };
  420. /**
  421. * Debug Facility Stuff
  422. */
  423. static debug_info_t *iucv_dbf_setup = NULL;
  424. static debug_info_t *iucv_dbf_data = NULL;
  425. static debug_info_t *iucv_dbf_trace = NULL;
  426. DEFINE_PER_CPU(char[256], iucv_dbf_txt_buf);
  427. static void iucv_unregister_dbf_views(void)
  428. {
  429. if (iucv_dbf_setup)
  430. debug_unregister(iucv_dbf_setup);
  431. if (iucv_dbf_data)
  432. debug_unregister(iucv_dbf_data);
  433. if (iucv_dbf_trace)
  434. debug_unregister(iucv_dbf_trace);
  435. }
  436. static int iucv_register_dbf_views(void)
  437. {
  438. iucv_dbf_setup = debug_register(IUCV_DBF_SETUP_NAME,
  439. IUCV_DBF_SETUP_PAGES,
  440. IUCV_DBF_SETUP_NR_AREAS,
  441. IUCV_DBF_SETUP_LEN);
  442. iucv_dbf_data = debug_register(IUCV_DBF_DATA_NAME,
  443. IUCV_DBF_DATA_PAGES,
  444. IUCV_DBF_DATA_NR_AREAS,
  445. IUCV_DBF_DATA_LEN);
  446. iucv_dbf_trace = debug_register(IUCV_DBF_TRACE_NAME,
  447. IUCV_DBF_TRACE_PAGES,
  448. IUCV_DBF_TRACE_NR_AREAS,
  449. IUCV_DBF_TRACE_LEN);
  450. if ((iucv_dbf_setup == NULL) || (iucv_dbf_data == NULL) ||
  451. (iucv_dbf_trace == NULL)) {
  452. iucv_unregister_dbf_views();
  453. return -ENOMEM;
  454. }
  455. debug_register_view(iucv_dbf_setup, &debug_hex_ascii_view);
  456. debug_set_level(iucv_dbf_setup, IUCV_DBF_SETUP_LEVEL);
  457. debug_register_view(iucv_dbf_data, &debug_hex_ascii_view);
  458. debug_set_level(iucv_dbf_data, IUCV_DBF_DATA_LEVEL);
  459. debug_register_view(iucv_dbf_trace, &debug_hex_ascii_view);
  460. debug_set_level(iucv_dbf_trace, IUCV_DBF_TRACE_LEVEL);
  461. return 0;
  462. }
  463. /*
  464. * Callback-wrappers, called from lowlevel iucv layer.
  465. */
  466. static void netiucv_callback_rx(struct iucv_path *path,
  467. struct iucv_message *msg)
  468. {
  469. struct iucv_connection *conn = path->private;
  470. struct iucv_event ev;
  471. ev.conn = conn;
  472. ev.data = msg;
  473. fsm_event(conn->fsm, CONN_EVENT_RX, &ev);
  474. }
  475. static void netiucv_callback_txdone(struct iucv_path *path,
  476. struct iucv_message *msg)
  477. {
  478. struct iucv_connection *conn = path->private;
  479. struct iucv_event ev;
  480. ev.conn = conn;
  481. ev.data = msg;
  482. fsm_event(conn->fsm, CONN_EVENT_TXDONE, &ev);
  483. }
  484. static void netiucv_callback_connack(struct iucv_path *path, u8 ipuser[16])
  485. {
  486. struct iucv_connection *conn = path->private;
  487. fsm_event(conn->fsm, CONN_EVENT_CONN_ACK, conn);
  488. }
  489. static int netiucv_callback_connreq(struct iucv_path *path,
  490. u8 ipvmid[8], u8 ipuser[16])
  491. {
  492. struct iucv_connection *conn = path->private;
  493. struct iucv_event ev;
  494. int rc;
  495. if (memcmp(iucvMagic, ipuser, sizeof(ipuser)))
  496. /* ipuser must match iucvMagic. */
  497. return -EINVAL;
  498. rc = -EINVAL;
  499. read_lock_bh(&iucv_connection_rwlock);
  500. list_for_each_entry(conn, &iucv_connection_list, list) {
  501. if (strncmp(ipvmid, conn->userid, 8))
  502. continue;
  503. /* Found a matching connection for this path. */
  504. conn->path = path;
  505. ev.conn = conn;
  506. ev.data = path;
  507. fsm_event(conn->fsm, CONN_EVENT_CONN_REQ, &ev);
  508. rc = 0;
  509. }
  510. read_unlock_bh(&iucv_connection_rwlock);
  511. return rc;
  512. }
  513. static void netiucv_callback_connrej(struct iucv_path *path, u8 ipuser[16])
  514. {
  515. struct iucv_connection *conn = path->private;
  516. fsm_event(conn->fsm, CONN_EVENT_CONN_REJ, conn);
  517. }
  518. static void netiucv_callback_connsusp(struct iucv_path *path, u8 ipuser[16])
  519. {
  520. struct iucv_connection *conn = path->private;
  521. fsm_event(conn->fsm, CONN_EVENT_CONN_SUS, conn);
  522. }
  523. static void netiucv_callback_connres(struct iucv_path *path, u8 ipuser[16])
  524. {
  525. struct iucv_connection *conn = path->private;
  526. fsm_event(conn->fsm, CONN_EVENT_CONN_RES, conn);
  527. }
  528. /**
  529. * NOP action for statemachines
  530. */
  531. static void netiucv_action_nop(fsm_instance *fi, int event, void *arg)
  532. {
  533. }
  534. /*
  535. * Actions of the connection statemachine
  536. */
  537. /**
  538. * netiucv_unpack_skb
  539. * @conn: The connection where this skb has been received.
  540. * @pskb: The received skb.
  541. *
  542. * Unpack a just received skb and hand it over to upper layers.
  543. * Helper function for conn_action_rx.
  544. */
  545. static void netiucv_unpack_skb(struct iucv_connection *conn,
  546. struct sk_buff *pskb)
  547. {
  548. struct net_device *dev = conn->netdev;
  549. struct netiucv_priv *privptr = netdev_priv(dev);
  550. u16 offset = 0;
  551. skb_put(pskb, NETIUCV_HDRLEN);
  552. pskb->dev = dev;
  553. pskb->ip_summed = CHECKSUM_NONE;
  554. pskb->protocol = ntohs(ETH_P_IP);
  555. while (1) {
  556. struct sk_buff *skb;
  557. struct ll_header *header = (struct ll_header *) pskb->data;
  558. if (!header->next)
  559. break;
  560. skb_pull(pskb, NETIUCV_HDRLEN);
  561. header->next -= offset;
  562. offset += header->next;
  563. header->next -= NETIUCV_HDRLEN;
  564. if (skb_tailroom(pskb) < header->next) {
  565. IUCV_DBF_TEXT_(data, 2, "Illegal next field: %d > %d\n",
  566. header->next, skb_tailroom(pskb));
  567. return;
  568. }
  569. skb_put(pskb, header->next);
  570. skb_reset_mac_header(pskb);
  571. skb = dev_alloc_skb(pskb->len);
  572. if (!skb) {
  573. IUCV_DBF_TEXT(data, 2,
  574. "Out of memory in netiucv_unpack_skb\n");
  575. privptr->stats.rx_dropped++;
  576. return;
  577. }
  578. skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
  579. pskb->len);
  580. skb_reset_mac_header(skb);
  581. skb->dev = pskb->dev;
  582. skb->protocol = pskb->protocol;
  583. pskb->ip_summed = CHECKSUM_UNNECESSARY;
  584. privptr->stats.rx_packets++;
  585. privptr->stats.rx_bytes += skb->len;
  586. /*
  587. * Since receiving is always initiated from a tasklet (in iucv.c),
  588. * we must use netif_rx_ni() instead of netif_rx()
  589. */
  590. netif_rx_ni(skb);
  591. dev->last_rx = jiffies;
  592. skb_pull(pskb, header->next);
  593. skb_put(pskb, NETIUCV_HDRLEN);
  594. }
  595. }
  596. static void conn_action_rx(fsm_instance *fi, int event, void *arg)
  597. {
  598. struct iucv_event *ev = arg;
  599. struct iucv_connection *conn = ev->conn;
  600. struct iucv_message *msg = ev->data;
  601. struct netiucv_priv *privptr = netdev_priv(conn->netdev);
  602. int rc;
  603. IUCV_DBF_TEXT(trace, 4, __func__);
  604. if (!conn->netdev) {
  605. iucv_message_reject(conn->path, msg);
  606. IUCV_DBF_TEXT(data, 2,
  607. "Received data for unlinked connection\n");
  608. return;
  609. }
  610. if (msg->length > conn->max_buffsize) {
  611. iucv_message_reject(conn->path, msg);
  612. privptr->stats.rx_dropped++;
  613. IUCV_DBF_TEXT_(data, 2, "msglen %d > max_buffsize %d\n",
  614. msg->length, conn->max_buffsize);
  615. return;
  616. }
  617. conn->rx_buff->data = conn->rx_buff->head;
  618. skb_reset_tail_pointer(conn->rx_buff);
  619. conn->rx_buff->len = 0;
  620. rc = iucv_message_receive(conn->path, msg, 0, conn->rx_buff->data,
  621. msg->length, NULL);
  622. if (rc || msg->length < 5) {
  623. privptr->stats.rx_errors++;
  624. IUCV_DBF_TEXT_(data, 2, "rc %d from iucv_receive\n", rc);
  625. return;
  626. }
  627. netiucv_unpack_skb(conn, conn->rx_buff);
  628. }
  629. static void conn_action_txdone(fsm_instance *fi, int event, void *arg)
  630. {
  631. struct iucv_event *ev = arg;
  632. struct iucv_connection *conn = ev->conn;
  633. struct iucv_message *msg = ev->data;
  634. struct iucv_message txmsg;
  635. struct netiucv_priv *privptr = NULL;
  636. u32 single_flag = msg->tag;
  637. u32 txbytes = 0;
  638. u32 txpackets = 0;
  639. u32 stat_maxcq = 0;
  640. struct sk_buff *skb;
  641. unsigned long saveflags;
  642. struct ll_header header;
  643. int rc;
  644. IUCV_DBF_TEXT(trace, 4, __func__);
  645. if (conn && conn->netdev)
  646. privptr = netdev_priv(conn->netdev);
  647. conn->prof.tx_pending--;
  648. if (single_flag) {
  649. if ((skb = skb_dequeue(&conn->commit_queue))) {
  650. atomic_dec(&skb->users);
  651. dev_kfree_skb_any(skb);
  652. if (privptr) {
  653. privptr->stats.tx_packets++;
  654. privptr->stats.tx_bytes +=
  655. (skb->len - NETIUCV_HDRLEN
  656. - NETIUCV_HDRLEN);
  657. }
  658. }
  659. }
  660. conn->tx_buff->data = conn->tx_buff->head;
  661. skb_reset_tail_pointer(conn->tx_buff);
  662. conn->tx_buff->len = 0;
  663. spin_lock_irqsave(&conn->collect_lock, saveflags);
  664. while ((skb = skb_dequeue(&conn->collect_queue))) {
  665. header.next = conn->tx_buff->len + skb->len + NETIUCV_HDRLEN;
  666. memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header,
  667. 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. atomic_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. memcpy(skb_put(conn->tx_buff, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
  687. conn->prof.send_stamp = current_kernel_time();
  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, NULL, 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, NULL);
  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, NULL);
  767. dev_info(privptr->dev, "The peer interface of the IUCV device"
  768. " has closed the connection\n");
  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. IUCV_DBF_TEXT_(setup, 2, "%s('%s'): connecting ...\n",
  783. netdev->name, conn->userid);
  784. /*
  785. * We must set the state before calling iucv_connect because the
  786. * callback handler could be called at any point after the connection
  787. * request is sent
  788. */
  789. fsm_newstate(fi, CONN_STATE_SETUPWAIT);
  790. conn->path = iucv_path_alloc(NETIUCV_QUEUELEN_DEFAULT, 0, GFP_KERNEL);
  791. rc = iucv_path_connect(conn->path, &netiucv_handler, conn->userid,
  792. NULL, iucvMagic, 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));
  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));
  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));
  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. atomic_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, iucvMagic);
  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", privptr->conn->userid);
  958. IUCV_DBF_TEXT(setup, 3,
  959. "connection is up and running\n");
  960. break;
  961. case DEV_STATE_STOPWAIT:
  962. IUCV_DBF_TEXT(data, 2,
  963. "dev_action_connup: in DEV_STATE_STOPWAIT\n");
  964. break;
  965. }
  966. }
  967. /**
  968. * Called from connection statemachine
  969. * when a connection has been shutdown.
  970. *
  971. * @param fi An instance of an interface statemachine.
  972. * @param event The event, just happened.
  973. * @param arg Generic pointer, casted from struct net_device * upon call.
  974. */
  975. static void
  976. dev_action_conndown(fsm_instance *fi, int event, void *arg)
  977. {
  978. IUCV_DBF_TEXT(trace, 3, __func__);
  979. switch (fsm_getstate(fi)) {
  980. case DEV_STATE_RUNNING:
  981. fsm_newstate(fi, DEV_STATE_STARTWAIT);
  982. break;
  983. case DEV_STATE_STOPWAIT:
  984. fsm_newstate(fi, DEV_STATE_STOPPED);
  985. IUCV_DBF_TEXT(setup, 3, "connection is down\n");
  986. break;
  987. }
  988. }
  989. static const fsm_node dev_fsm[] = {
  990. { DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start },
  991. { DEV_STATE_STOPWAIT, DEV_EVENT_START, dev_action_start },
  992. { DEV_STATE_STOPWAIT, DEV_EVENT_CONDOWN, dev_action_conndown },
  993. { DEV_STATE_STARTWAIT, DEV_EVENT_STOP, dev_action_stop },
  994. { DEV_STATE_STARTWAIT, DEV_EVENT_CONUP, dev_action_connup },
  995. { DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
  996. { DEV_STATE_RUNNING, DEV_EVENT_CONDOWN, dev_action_conndown },
  997. { DEV_STATE_RUNNING, DEV_EVENT_CONUP, netiucv_action_nop },
  998. };
  999. static const int DEV_FSM_LEN = sizeof(dev_fsm) / sizeof(fsm_node);
  1000. /**
  1001. * Transmit a packet.
  1002. * This is a helper function for netiucv_tx().
  1003. *
  1004. * @param conn Connection to be used for sending.
  1005. * @param skb Pointer to struct sk_buff of packet to send.
  1006. * The linklevel header has already been set up
  1007. * by netiucv_tx().
  1008. *
  1009. * @return 0 on success, -ERRNO on failure. (Never fails.)
  1010. */
  1011. static int netiucv_transmit_skb(struct iucv_connection *conn,
  1012. struct sk_buff *skb)
  1013. {
  1014. struct iucv_message msg;
  1015. unsigned long saveflags;
  1016. struct ll_header header;
  1017. int rc;
  1018. if (fsm_getstate(conn->fsm) != CONN_STATE_IDLE) {
  1019. int l = skb->len + NETIUCV_HDRLEN;
  1020. spin_lock_irqsave(&conn->collect_lock, saveflags);
  1021. if (conn->collect_len + l >
  1022. (conn->max_buffsize - NETIUCV_HDRLEN)) {
  1023. rc = -EBUSY;
  1024. IUCV_DBF_TEXT(data, 2,
  1025. "EBUSY from netiucv_transmit_skb\n");
  1026. } else {
  1027. atomic_inc(&skb->users);
  1028. skb_queue_tail(&conn->collect_queue, skb);
  1029. conn->collect_len += l;
  1030. rc = 0;
  1031. }
  1032. spin_unlock_irqrestore(&conn->collect_lock, saveflags);
  1033. } else {
  1034. struct sk_buff *nskb = skb;
  1035. /**
  1036. * Copy the skb to a new allocated skb in lowmem only if the
  1037. * data is located above 2G in memory or tailroom is < 2.
  1038. */
  1039. unsigned long hi = ((unsigned long)(skb_tail_pointer(skb) +
  1040. NETIUCV_HDRLEN)) >> 31;
  1041. int copied = 0;
  1042. if (hi || (skb_tailroom(skb) < 2)) {
  1043. nskb = alloc_skb(skb->len + NETIUCV_HDRLEN +
  1044. NETIUCV_HDRLEN, GFP_ATOMIC | GFP_DMA);
  1045. if (!nskb) {
  1046. IUCV_DBF_TEXT(data, 2, "alloc_skb failed\n");
  1047. rc = -ENOMEM;
  1048. return rc;
  1049. } else {
  1050. skb_reserve(nskb, NETIUCV_HDRLEN);
  1051. memcpy(skb_put(nskb, skb->len),
  1052. 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. memcpy(skb_put(nskb, NETIUCV_HDRLEN), &header, NETIUCV_HDRLEN);
  1063. fsm_newstate(conn->fsm, CONN_STATE_TX);
  1064. conn->prof.send_stamp = current_kernel_time();
  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. atomic_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 0;
  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 0;
  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 0;
  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. dev->trans_start = jiffies;
  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. * netiucv_change_mtu
  1259. * @dev: Pointer to interface struct.
  1260. * @new_mtu: The new MTU to use for this interface.
  1261. *
  1262. * Sets MTU of an interface.
  1263. *
  1264. * Returns 0 on success, -EINVAL if MTU is out of valid range.
  1265. * (valid range is 576 .. NETIUCV_MTU_MAX).
  1266. */
  1267. static int netiucv_change_mtu(struct net_device * dev, int new_mtu)
  1268. {
  1269. IUCV_DBF_TEXT(trace, 3, __func__);
  1270. if (new_mtu < 576 || new_mtu > NETIUCV_MTU_MAX) {
  1271. IUCV_DBF_TEXT(setup, 2, "given MTU out of valid range\n");
  1272. return -EINVAL;
  1273. }
  1274. dev->mtu = new_mtu;
  1275. return 0;
  1276. }
  1277. /*
  1278. * attributes in sysfs
  1279. */
  1280. static ssize_t user_show(struct device *dev, struct device_attribute *attr,
  1281. char *buf)
  1282. {
  1283. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1284. IUCV_DBF_TEXT(trace, 5, __func__);
  1285. return sprintf(buf, "%s\n", netiucv_printname(priv->conn->userid));
  1286. }
  1287. static ssize_t user_write(struct device *dev, struct device_attribute *attr,
  1288. const char *buf, size_t count)
  1289. {
  1290. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1291. struct net_device *ndev = priv->conn->netdev;
  1292. char *p;
  1293. char *tmp;
  1294. char username[9];
  1295. int i;
  1296. struct iucv_connection *cp;
  1297. IUCV_DBF_TEXT(trace, 3, __func__);
  1298. if (count > 9) {
  1299. IUCV_DBF_TEXT_(setup, 2,
  1300. "%d is length of username\n", (int) count);
  1301. return -EINVAL;
  1302. }
  1303. tmp = strsep((char **) &buf, "\n");
  1304. for (i = 0, p = tmp; i < 8 && *p; i++, p++) {
  1305. if (isalnum(*p) || (*p == '$')) {
  1306. username[i]= toupper(*p);
  1307. continue;
  1308. }
  1309. if (*p == '\n') {
  1310. /* trailing lf, grr */
  1311. break;
  1312. }
  1313. IUCV_DBF_TEXT_(setup, 2,
  1314. "username: invalid character %c\n", *p);
  1315. return -EINVAL;
  1316. }
  1317. while (i < 8)
  1318. username[i++] = ' ';
  1319. username[8] = '\0';
  1320. if (memcmp(username, priv->conn->userid, 9) &&
  1321. (ndev->flags & (IFF_UP | IFF_RUNNING))) {
  1322. /* username changed while the interface is active. */
  1323. IUCV_DBF_TEXT(setup, 2, "user_write: device active\n");
  1324. return -EPERM;
  1325. }
  1326. read_lock_bh(&iucv_connection_rwlock);
  1327. list_for_each_entry(cp, &iucv_connection_list, list) {
  1328. if (!strncmp(username, cp->userid, 9) && cp->netdev != ndev) {
  1329. read_unlock_bh(&iucv_connection_rwlock);
  1330. IUCV_DBF_TEXT_(setup, 2, "user_write: Connection "
  1331. "to %s already exists\n", username);
  1332. return -EEXIST;
  1333. }
  1334. }
  1335. read_unlock_bh(&iucv_connection_rwlock);
  1336. memcpy(priv->conn->userid, username, 9);
  1337. return count;
  1338. }
  1339. static DEVICE_ATTR(user, 0644, user_show, user_write);
  1340. static ssize_t buffer_show (struct device *dev, struct device_attribute *attr,
  1341. char *buf)
  1342. {
  1343. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1344. IUCV_DBF_TEXT(trace, 5, __func__);
  1345. return sprintf(buf, "%d\n", priv->conn->max_buffsize);
  1346. }
  1347. static ssize_t buffer_write (struct device *dev, struct device_attribute *attr,
  1348. const char *buf, size_t count)
  1349. {
  1350. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1351. struct net_device *ndev = priv->conn->netdev;
  1352. char *e;
  1353. int bs1;
  1354. IUCV_DBF_TEXT(trace, 3, __func__);
  1355. if (count >= 39)
  1356. return -EINVAL;
  1357. bs1 = simple_strtoul(buf, &e, 0);
  1358. if (e && (!isspace(*e))) {
  1359. IUCV_DBF_TEXT_(setup, 2, "buffer_write: invalid char %c\n", *e);
  1360. return -EINVAL;
  1361. }
  1362. if (bs1 > NETIUCV_BUFSIZE_MAX) {
  1363. IUCV_DBF_TEXT_(setup, 2,
  1364. "buffer_write: buffer size %d too large\n",
  1365. bs1);
  1366. return -EINVAL;
  1367. }
  1368. if ((ndev->flags & IFF_RUNNING) &&
  1369. (bs1 < (ndev->mtu + NETIUCV_HDRLEN + 2))) {
  1370. IUCV_DBF_TEXT_(setup, 2,
  1371. "buffer_write: buffer size %d too small\n",
  1372. bs1);
  1373. return -EINVAL;
  1374. }
  1375. if (bs1 < (576 + NETIUCV_HDRLEN + NETIUCV_HDRLEN)) {
  1376. IUCV_DBF_TEXT_(setup, 2,
  1377. "buffer_write: buffer size %d too small\n",
  1378. bs1);
  1379. return -EINVAL;
  1380. }
  1381. priv->conn->max_buffsize = bs1;
  1382. if (!(ndev->flags & IFF_RUNNING))
  1383. ndev->mtu = bs1 - NETIUCV_HDRLEN - NETIUCV_HDRLEN;
  1384. return count;
  1385. }
  1386. static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
  1387. static ssize_t dev_fsm_show (struct device *dev, struct device_attribute *attr,
  1388. 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->fsm));
  1393. }
  1394. static DEVICE_ATTR(device_fsm_state, 0444, dev_fsm_show, NULL);
  1395. static ssize_t conn_fsm_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, "%s\n", fsm_getstate_str(priv->conn->fsm));
  1401. }
  1402. static DEVICE_ATTR(connection_fsm_state, 0444, conn_fsm_show, NULL);
  1403. static ssize_t maxmulti_show (struct device *dev,
  1404. struct device_attribute *attr, char *buf)
  1405. {
  1406. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1407. IUCV_DBF_TEXT(trace, 5, __func__);
  1408. return sprintf(buf, "%ld\n", priv->conn->prof.maxmulti);
  1409. }
  1410. static ssize_t maxmulti_write (struct device *dev,
  1411. struct device_attribute *attr,
  1412. const char *buf, size_t count)
  1413. {
  1414. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1415. IUCV_DBF_TEXT(trace, 4, __func__);
  1416. priv->conn->prof.maxmulti = 0;
  1417. return count;
  1418. }
  1419. static DEVICE_ATTR(max_tx_buffer_used, 0644, maxmulti_show, maxmulti_write);
  1420. static ssize_t maxcq_show (struct device *dev, struct device_attribute *attr,
  1421. char *buf)
  1422. {
  1423. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1424. IUCV_DBF_TEXT(trace, 5, __func__);
  1425. return sprintf(buf, "%ld\n", priv->conn->prof.maxcqueue);
  1426. }
  1427. static ssize_t maxcq_write (struct device *dev, struct device_attribute *attr,
  1428. const char *buf, size_t count)
  1429. {
  1430. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1431. IUCV_DBF_TEXT(trace, 4, __func__);
  1432. priv->conn->prof.maxcqueue = 0;
  1433. return count;
  1434. }
  1435. static DEVICE_ATTR(max_chained_skbs, 0644, maxcq_show, maxcq_write);
  1436. static ssize_t sdoio_show (struct device *dev, struct device_attribute *attr,
  1437. char *buf)
  1438. {
  1439. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1440. IUCV_DBF_TEXT(trace, 5, __func__);
  1441. return sprintf(buf, "%ld\n", priv->conn->prof.doios_single);
  1442. }
  1443. static ssize_t sdoio_write (struct device *dev, struct device_attribute *attr,
  1444. const char *buf, size_t count)
  1445. {
  1446. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1447. IUCV_DBF_TEXT(trace, 4, __func__);
  1448. priv->conn->prof.doios_single = 0;
  1449. return count;
  1450. }
  1451. static DEVICE_ATTR(tx_single_write_ops, 0644, sdoio_show, sdoio_write);
  1452. static ssize_t mdoio_show (struct device *dev, struct device_attribute *attr,
  1453. char *buf)
  1454. {
  1455. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1456. IUCV_DBF_TEXT(trace, 5, __func__);
  1457. return sprintf(buf, "%ld\n", priv->conn->prof.doios_multi);
  1458. }
  1459. static ssize_t mdoio_write (struct device *dev, struct device_attribute *attr,
  1460. const char *buf, size_t count)
  1461. {
  1462. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1463. IUCV_DBF_TEXT(trace, 5, __func__);
  1464. priv->conn->prof.doios_multi = 0;
  1465. return count;
  1466. }
  1467. static DEVICE_ATTR(tx_multi_write_ops, 0644, mdoio_show, mdoio_write);
  1468. static ssize_t txlen_show (struct device *dev, struct device_attribute *attr,
  1469. char *buf)
  1470. {
  1471. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1472. IUCV_DBF_TEXT(trace, 5, __func__);
  1473. return sprintf(buf, "%ld\n", priv->conn->prof.txlen);
  1474. }
  1475. static ssize_t txlen_write (struct device *dev, struct device_attribute *attr,
  1476. const char *buf, size_t count)
  1477. {
  1478. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1479. IUCV_DBF_TEXT(trace, 4, __func__);
  1480. priv->conn->prof.txlen = 0;
  1481. return count;
  1482. }
  1483. static DEVICE_ATTR(netto_bytes, 0644, txlen_show, txlen_write);
  1484. static ssize_t txtime_show (struct device *dev, struct device_attribute *attr,
  1485. char *buf)
  1486. {
  1487. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1488. IUCV_DBF_TEXT(trace, 5, __func__);
  1489. return sprintf(buf, "%ld\n", priv->conn->prof.tx_time);
  1490. }
  1491. static ssize_t txtime_write (struct device *dev, struct device_attribute *attr,
  1492. const char *buf, size_t count)
  1493. {
  1494. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1495. IUCV_DBF_TEXT(trace, 4, __func__);
  1496. priv->conn->prof.tx_time = 0;
  1497. return count;
  1498. }
  1499. static DEVICE_ATTR(max_tx_io_time, 0644, txtime_show, txtime_write);
  1500. static ssize_t txpend_show (struct device *dev, struct device_attribute *attr,
  1501. char *buf)
  1502. {
  1503. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1504. IUCV_DBF_TEXT(trace, 5, __func__);
  1505. return sprintf(buf, "%ld\n", priv->conn->prof.tx_pending);
  1506. }
  1507. static ssize_t txpend_write (struct device *dev, struct device_attribute *attr,
  1508. const char *buf, size_t count)
  1509. {
  1510. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1511. IUCV_DBF_TEXT(trace, 4, __func__);
  1512. priv->conn->prof.tx_pending = 0;
  1513. return count;
  1514. }
  1515. static DEVICE_ATTR(tx_pending, 0644, txpend_show, txpend_write);
  1516. static ssize_t txmpnd_show (struct device *dev, struct device_attribute *attr,
  1517. char *buf)
  1518. {
  1519. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1520. IUCV_DBF_TEXT(trace, 5, __func__);
  1521. return sprintf(buf, "%ld\n", priv->conn->prof.tx_max_pending);
  1522. }
  1523. static ssize_t txmpnd_write (struct device *dev, struct device_attribute *attr,
  1524. const char *buf, size_t count)
  1525. {
  1526. struct netiucv_priv *priv = dev_get_drvdata(dev);
  1527. IUCV_DBF_TEXT(trace, 4, __func__);
  1528. priv->conn->prof.tx_max_pending = 0;
  1529. return count;
  1530. }
  1531. static DEVICE_ATTR(tx_max_pending, 0644, txmpnd_show, txmpnd_write);
  1532. static struct attribute *netiucv_attrs[] = {
  1533. &dev_attr_buffer.attr,
  1534. &dev_attr_user.attr,
  1535. NULL,
  1536. };
  1537. static struct attribute_group netiucv_attr_group = {
  1538. .attrs = netiucv_attrs,
  1539. };
  1540. static struct attribute *netiucv_stat_attrs[] = {
  1541. &dev_attr_device_fsm_state.attr,
  1542. &dev_attr_connection_fsm_state.attr,
  1543. &dev_attr_max_tx_buffer_used.attr,
  1544. &dev_attr_max_chained_skbs.attr,
  1545. &dev_attr_tx_single_write_ops.attr,
  1546. &dev_attr_tx_multi_write_ops.attr,
  1547. &dev_attr_netto_bytes.attr,
  1548. &dev_attr_max_tx_io_time.attr,
  1549. &dev_attr_tx_pending.attr,
  1550. &dev_attr_tx_max_pending.attr,
  1551. NULL,
  1552. };
  1553. static struct attribute_group netiucv_stat_attr_group = {
  1554. .name = "stats",
  1555. .attrs = netiucv_stat_attrs,
  1556. };
  1557. static int netiucv_add_files(struct device *dev)
  1558. {
  1559. int ret;
  1560. IUCV_DBF_TEXT(trace, 3, __func__);
  1561. ret = sysfs_create_group(&dev->kobj, &netiucv_attr_group);
  1562. if (ret)
  1563. return ret;
  1564. ret = sysfs_create_group(&dev->kobj, &netiucv_stat_attr_group);
  1565. if (ret)
  1566. sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
  1567. return ret;
  1568. }
  1569. static void netiucv_remove_files(struct device *dev)
  1570. {
  1571. IUCV_DBF_TEXT(trace, 3, __func__);
  1572. sysfs_remove_group(&dev->kobj, &netiucv_stat_attr_group);
  1573. sysfs_remove_group(&dev->kobj, &netiucv_attr_group);
  1574. }
  1575. static int netiucv_register_device(struct net_device *ndev)
  1576. {
  1577. struct netiucv_priv *priv = netdev_priv(ndev);
  1578. struct device *dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1579. int ret;
  1580. IUCV_DBF_TEXT(trace, 3, __func__);
  1581. if (dev) {
  1582. dev_set_name(dev, "net%s", ndev->name);
  1583. dev->bus = &iucv_bus;
  1584. dev->parent = iucv_root;
  1585. /*
  1586. * The release function could be called after the
  1587. * module has been unloaded. It's _only_ task is to
  1588. * free the struct. Therefore, we specify kfree()
  1589. * directly here. (Probably a little bit obfuscating
  1590. * but legitime ...).
  1591. */
  1592. dev->release = (void (*)(struct device *))kfree;
  1593. dev->driver = &netiucv_driver;
  1594. } else
  1595. return -ENOMEM;
  1596. ret = device_register(dev);
  1597. if (ret)
  1598. return ret;
  1599. ret = netiucv_add_files(dev);
  1600. if (ret)
  1601. goto out_unreg;
  1602. priv->dev = dev;
  1603. dev_set_drvdata(dev, priv);
  1604. return 0;
  1605. out_unreg:
  1606. device_unregister(dev);
  1607. return ret;
  1608. }
  1609. static void netiucv_unregister_device(struct device *dev)
  1610. {
  1611. IUCV_DBF_TEXT(trace, 3, __func__);
  1612. netiucv_remove_files(dev);
  1613. device_unregister(dev);
  1614. }
  1615. /**
  1616. * Allocate and initialize a new connection structure.
  1617. * Add it to the list of netiucv connections;
  1618. */
  1619. static struct iucv_connection *netiucv_new_connection(struct net_device *dev,
  1620. char *username)
  1621. {
  1622. struct iucv_connection *conn;
  1623. conn = kzalloc(sizeof(*conn), GFP_KERNEL);
  1624. if (!conn)
  1625. goto out;
  1626. skb_queue_head_init(&conn->collect_queue);
  1627. skb_queue_head_init(&conn->commit_queue);
  1628. spin_lock_init(&conn->collect_lock);
  1629. conn->max_buffsize = NETIUCV_BUFSIZE_DEFAULT;
  1630. conn->netdev = dev;
  1631. conn->rx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
  1632. if (!conn->rx_buff)
  1633. goto out_conn;
  1634. conn->tx_buff = alloc_skb(conn->max_buffsize, GFP_KERNEL | GFP_DMA);
  1635. if (!conn->tx_buff)
  1636. goto out_rx;
  1637. conn->fsm = init_fsm("netiucvconn", conn_state_names,
  1638. conn_event_names, NR_CONN_STATES,
  1639. NR_CONN_EVENTS, conn_fsm, CONN_FSM_LEN,
  1640. GFP_KERNEL);
  1641. if (!conn->fsm)
  1642. goto out_tx;
  1643. fsm_settimer(conn->fsm, &conn->timer);
  1644. fsm_newstate(conn->fsm, CONN_STATE_INVALID);
  1645. if (username) {
  1646. memcpy(conn->userid, username, 9);
  1647. fsm_newstate(conn->fsm, CONN_STATE_STOPPED);
  1648. }
  1649. write_lock_bh(&iucv_connection_rwlock);
  1650. list_add_tail(&conn->list, &iucv_connection_list);
  1651. write_unlock_bh(&iucv_connection_rwlock);
  1652. return conn;
  1653. out_tx:
  1654. kfree_skb(conn->tx_buff);
  1655. out_rx:
  1656. kfree_skb(conn->rx_buff);
  1657. out_conn:
  1658. kfree(conn);
  1659. out:
  1660. return NULL;
  1661. }
  1662. /**
  1663. * Release a connection structure and remove it from the
  1664. * list of netiucv connections.
  1665. */
  1666. static void netiucv_remove_connection(struct iucv_connection *conn)
  1667. {
  1668. IUCV_DBF_TEXT(trace, 3, __func__);
  1669. write_lock_bh(&iucv_connection_rwlock);
  1670. list_del_init(&conn->list);
  1671. write_unlock_bh(&iucv_connection_rwlock);
  1672. fsm_deltimer(&conn->timer);
  1673. netiucv_purge_skb_queue(&conn->collect_queue);
  1674. if (conn->path) {
  1675. iucv_path_sever(conn->path, iucvMagic);
  1676. kfree(conn->path);
  1677. conn->path = NULL;
  1678. }
  1679. netiucv_purge_skb_queue(&conn->commit_queue);
  1680. kfree_fsm(conn->fsm);
  1681. kfree_skb(conn->rx_buff);
  1682. kfree_skb(conn->tx_buff);
  1683. }
  1684. /**
  1685. * Release everything of a net device.
  1686. */
  1687. static void netiucv_free_netdevice(struct net_device *dev)
  1688. {
  1689. struct netiucv_priv *privptr = netdev_priv(dev);
  1690. IUCV_DBF_TEXT(trace, 3, __func__);
  1691. if (!dev)
  1692. return;
  1693. if (privptr) {
  1694. if (privptr->conn)
  1695. netiucv_remove_connection(privptr->conn);
  1696. if (privptr->fsm)
  1697. kfree_fsm(privptr->fsm);
  1698. privptr->conn = NULL; privptr->fsm = NULL;
  1699. /* privptr gets freed by free_netdev() */
  1700. }
  1701. free_netdev(dev);
  1702. }
  1703. /**
  1704. * Initialize a net device. (Called from kernel in alloc_netdev())
  1705. */
  1706. static const struct net_device_ops netiucv_netdev_ops = {
  1707. .ndo_open = netiucv_open,
  1708. .ndo_stop = netiucv_close,
  1709. .ndo_get_stats = netiucv_stats,
  1710. .ndo_start_xmit = netiucv_tx,
  1711. .ndo_change_mtu = netiucv_change_mtu,
  1712. };
  1713. static void netiucv_setup_netdevice(struct net_device *dev)
  1714. {
  1715. dev->mtu = NETIUCV_MTU_DEFAULT;
  1716. dev->destructor = netiucv_free_netdevice;
  1717. dev->hard_header_len = NETIUCV_HDRLEN;
  1718. dev->addr_len = 0;
  1719. dev->type = ARPHRD_SLIP;
  1720. dev->tx_queue_len = NETIUCV_QUEUELEN_DEFAULT;
  1721. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  1722. dev->netdev_ops = &netiucv_netdev_ops;
  1723. }
  1724. /**
  1725. * Allocate and initialize everything of a net device.
  1726. */
  1727. static struct net_device *netiucv_init_netdevice(char *username)
  1728. {
  1729. struct netiucv_priv *privptr;
  1730. struct net_device *dev;
  1731. dev = alloc_netdev(sizeof(struct netiucv_priv), "iucv%d",
  1732. netiucv_setup_netdevice);
  1733. if (!dev)
  1734. return NULL;
  1735. if (dev_alloc_name(dev, dev->name) < 0)
  1736. goto out_netdev;
  1737. privptr = netdev_priv(dev);
  1738. privptr->fsm = init_fsm("netiucvdev", dev_state_names,
  1739. dev_event_names, NR_DEV_STATES, NR_DEV_EVENTS,
  1740. dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
  1741. if (!privptr->fsm)
  1742. goto out_netdev;
  1743. privptr->conn = netiucv_new_connection(dev, username);
  1744. if (!privptr->conn) {
  1745. IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_new_connection\n");
  1746. goto out_fsm;
  1747. }
  1748. fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
  1749. return dev;
  1750. out_fsm:
  1751. kfree_fsm(privptr->fsm);
  1752. out_netdev:
  1753. free_netdev(dev);
  1754. return NULL;
  1755. }
  1756. static ssize_t conn_write(struct device_driver *drv,
  1757. const char *buf, size_t count)
  1758. {
  1759. const char *p;
  1760. char username[9];
  1761. int i, rc;
  1762. struct net_device *dev;
  1763. struct netiucv_priv *priv;
  1764. struct iucv_connection *cp;
  1765. IUCV_DBF_TEXT(trace, 3, __func__);
  1766. if (count>9) {
  1767. IUCV_DBF_TEXT(setup, 2, "conn_write: too long\n");
  1768. return -EINVAL;
  1769. }
  1770. for (i = 0, p = buf; i < 8 && *p; i++, p++) {
  1771. if (isalnum(*p) || *p == '$') {
  1772. username[i] = toupper(*p);
  1773. continue;
  1774. }
  1775. if (*p == '\n')
  1776. /* trailing lf, grr */
  1777. break;
  1778. IUCV_DBF_TEXT_(setup, 2,
  1779. "conn_write: invalid character %c\n", *p);
  1780. return -EINVAL;
  1781. }
  1782. while (i < 8)
  1783. username[i++] = ' ';
  1784. username[8] = '\0';
  1785. read_lock_bh(&iucv_connection_rwlock);
  1786. list_for_each_entry(cp, &iucv_connection_list, list) {
  1787. if (!strncmp(username, cp->userid, 9)) {
  1788. read_unlock_bh(&iucv_connection_rwlock);
  1789. IUCV_DBF_TEXT_(setup, 2, "conn_write: Connection "
  1790. "to %s already exists\n", username);
  1791. return -EEXIST;
  1792. }
  1793. }
  1794. read_unlock_bh(&iucv_connection_rwlock);
  1795. dev = netiucv_init_netdevice(username);
  1796. if (!dev) {
  1797. IUCV_DBF_TEXT(setup, 2, "NULL from netiucv_init_netdevice\n");
  1798. return -ENODEV;
  1799. }
  1800. rc = netiucv_register_device(dev);
  1801. if (rc) {
  1802. IUCV_DBF_TEXT_(setup, 2,
  1803. "ret %d from netiucv_register_device\n", rc);
  1804. goto out_free_ndev;
  1805. }
  1806. /* sysfs magic */
  1807. priv = netdev_priv(dev);
  1808. SET_NETDEV_DEV(dev, priv->dev);
  1809. rc = register_netdev(dev);
  1810. if (rc)
  1811. goto out_unreg;
  1812. dev_info(priv->dev, "The IUCV interface to %s has been"
  1813. " established successfully\n", netiucv_printname(username));
  1814. return count;
  1815. out_unreg:
  1816. netiucv_unregister_device(priv->dev);
  1817. out_free_ndev:
  1818. netiucv_free_netdevice(dev);
  1819. return rc;
  1820. }
  1821. static DRIVER_ATTR(connection, 0200, NULL, conn_write);
  1822. static ssize_t remove_write (struct device_driver *drv,
  1823. const char *buf, size_t count)
  1824. {
  1825. struct iucv_connection *cp;
  1826. struct net_device *ndev;
  1827. struct netiucv_priv *priv;
  1828. struct device *dev;
  1829. char name[IFNAMSIZ];
  1830. const char *p;
  1831. int i;
  1832. IUCV_DBF_TEXT(trace, 3, __func__);
  1833. if (count >= IFNAMSIZ)
  1834. count = IFNAMSIZ - 1;;
  1835. for (i = 0, p = buf; i < count && *p; i++, p++) {
  1836. if (*p == '\n' || *p == ' ')
  1837. /* trailing lf, grr */
  1838. break;
  1839. name[i] = *p;
  1840. }
  1841. name[i] = '\0';
  1842. read_lock_bh(&iucv_connection_rwlock);
  1843. list_for_each_entry(cp, &iucv_connection_list, list) {
  1844. ndev = cp->netdev;
  1845. priv = netdev_priv(ndev);
  1846. dev = priv->dev;
  1847. if (strncmp(name, ndev->name, count))
  1848. continue;
  1849. read_unlock_bh(&iucv_connection_rwlock);
  1850. if (ndev->flags & (IFF_UP | IFF_RUNNING)) {
  1851. dev_warn(dev, "The IUCV device is connected"
  1852. " to %s and cannot be removed\n",
  1853. priv->conn->userid);
  1854. IUCV_DBF_TEXT(data, 2, "remove_write: still active\n");
  1855. return -EPERM;
  1856. }
  1857. unregister_netdev(ndev);
  1858. netiucv_unregister_device(dev);
  1859. return count;
  1860. }
  1861. read_unlock_bh(&iucv_connection_rwlock);
  1862. IUCV_DBF_TEXT(data, 2, "remove_write: unknown device\n");
  1863. return -EINVAL;
  1864. }
  1865. static DRIVER_ATTR(remove, 0200, NULL, remove_write);
  1866. static struct attribute * netiucv_drv_attrs[] = {
  1867. &driver_attr_connection.attr,
  1868. &driver_attr_remove.attr,
  1869. NULL,
  1870. };
  1871. static struct attribute_group netiucv_drv_attr_group = {
  1872. .attrs = netiucv_drv_attrs,
  1873. };
  1874. static struct attribute_group *netiucv_drv_attr_groups[] = {
  1875. &netiucv_drv_attr_group,
  1876. NULL,
  1877. };
  1878. static void netiucv_banner(void)
  1879. {
  1880. pr_info("driver initialized\n");
  1881. }
  1882. static void __exit netiucv_exit(void)
  1883. {
  1884. struct iucv_connection *cp;
  1885. struct net_device *ndev;
  1886. struct netiucv_priv *priv;
  1887. struct device *dev;
  1888. IUCV_DBF_TEXT(trace, 3, __func__);
  1889. while (!list_empty(&iucv_connection_list)) {
  1890. cp = list_entry(iucv_connection_list.next,
  1891. struct iucv_connection, list);
  1892. ndev = cp->netdev;
  1893. priv = netdev_priv(ndev);
  1894. dev = priv->dev;
  1895. unregister_netdev(ndev);
  1896. netiucv_unregister_device(dev);
  1897. }
  1898. device_unregister(netiucv_dev);
  1899. driver_unregister(&netiucv_driver);
  1900. iucv_unregister(&netiucv_handler, 1);
  1901. iucv_unregister_dbf_views();
  1902. pr_info("driver unloaded\n");
  1903. return;
  1904. }
  1905. static int __init netiucv_init(void)
  1906. {
  1907. int rc;
  1908. rc = iucv_register_dbf_views();
  1909. if (rc)
  1910. goto out;
  1911. rc = iucv_register(&netiucv_handler, 1);
  1912. if (rc)
  1913. goto out_dbf;
  1914. IUCV_DBF_TEXT(trace, 3, __func__);
  1915. netiucv_driver.groups = netiucv_drv_attr_groups;
  1916. rc = driver_register(&netiucv_driver);
  1917. if (rc) {
  1918. IUCV_DBF_TEXT_(setup, 2, "ret %d from driver_register\n", rc);
  1919. goto out_iucv;
  1920. }
  1921. /* establish dummy device */
  1922. netiucv_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1923. if (!netiucv_dev) {
  1924. rc = -ENOMEM;
  1925. goto out_driver;
  1926. }
  1927. dev_set_name(netiucv_dev, "netiucv");
  1928. netiucv_dev->bus = &iucv_bus;
  1929. netiucv_dev->parent = iucv_root;
  1930. netiucv_dev->release = (void (*)(struct device *))kfree;
  1931. netiucv_dev->driver = &netiucv_driver;
  1932. rc = device_register(netiucv_dev);
  1933. if (rc)
  1934. goto out_driver;
  1935. netiucv_banner();
  1936. return rc;
  1937. out_driver:
  1938. driver_unregister(&netiucv_driver);
  1939. out_iucv:
  1940. iucv_unregister(&netiucv_handler, 1);
  1941. out_dbf:
  1942. iucv_unregister_dbf_views();
  1943. out:
  1944. return rc;
  1945. }
  1946. module_init(netiucv_init);
  1947. module_exit(netiucv_exit);
  1948. MODULE_LICENSE("GPL");