netiucv.c 57 KB

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