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