netiucv.c 59 KB

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