ctcmain.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100
  1. /*
  2. * $Id: ctcmain.c,v 1.74 2005/03/24 09:04:17 mschwide Exp $
  3. *
  4. * CTC / ESCON network driver
  5. *
  6. * Copyright (C) 2001 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Fritz Elfert (elfert@de.ibm.com, felfert@millenux.com)
  8. * Fixes by : Jochen Röhrig (roehrig@de.ibm.com)
  9. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  10. Peter Tiedemann (ptiedem@de.ibm.com)
  11. * Driver Model stuff by : Cornelia Huck <cohuck@de.ibm.com>
  12. *
  13. * Documentation used:
  14. * - Principles of Operation (IBM doc#: SA22-7201-06)
  15. * - Common IO/-Device Commands and Self Description (IBM doc#: SA22-7204-02)
  16. * - Common IO/-Device Commands and Self Description (IBM doc#: SN22-5535)
  17. * - ESCON Channel-to-Channel Adapter (IBM doc#: SA22-7203-00)
  18. * - ESCON I/O Interface (IBM doc#: SA22-7202-029
  19. *
  20. * and the source of the original CTC driver by:
  21. * Dieter Wellerdiek (wel@de.ibm.com)
  22. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  23. * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  24. * Jochen Röhrig (roehrig@de.ibm.com)
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2, or (at your option)
  29. * any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  39. *
  40. * RELEASE-TAG: CTC/ESCON network driver $Revision: 1.74 $
  41. *
  42. */
  43. #undef DEBUG
  44. #include <linux/module.h>
  45. #include <linux/init.h>
  46. #include <linux/kernel.h>
  47. #include <linux/slab.h>
  48. #include <linux/errno.h>
  49. #include <linux/types.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/timer.h>
  52. #include <linux/sched.h>
  53. #include <linux/bitops.h>
  54. #include <linux/signal.h>
  55. #include <linux/string.h>
  56. #include <linux/ip.h>
  57. #include <linux/if_arp.h>
  58. #include <linux/tcp.h>
  59. #include <linux/skbuff.h>
  60. #include <linux/ctype.h>
  61. #include <net/dst.h>
  62. #include <asm/io.h>
  63. #include <asm/ccwdev.h>
  64. #include <asm/ccwgroup.h>
  65. #include <asm/uaccess.h>
  66. #include <asm/idals.h>
  67. #include "ctctty.h"
  68. #include "fsm.h"
  69. #include "cu3088.h"
  70. #include "ctcdbug.h"
  71. #include "ctcmain.h"
  72. MODULE_AUTHOR("(C) 2000 IBM Corp. by Fritz Elfert (felfert@millenux.com)");
  73. MODULE_DESCRIPTION("Linux for S/390 CTC/Escon Driver");
  74. MODULE_LICENSE("GPL");
  75. /**
  76. * States of the interface statemachine.
  77. */
  78. enum dev_states {
  79. DEV_STATE_STOPPED,
  80. DEV_STATE_STARTWAIT_RXTX,
  81. DEV_STATE_STARTWAIT_RX,
  82. DEV_STATE_STARTWAIT_TX,
  83. DEV_STATE_STOPWAIT_RXTX,
  84. DEV_STATE_STOPWAIT_RX,
  85. DEV_STATE_STOPWAIT_TX,
  86. DEV_STATE_RUNNING,
  87. /**
  88. * MUST be always the last element!!
  89. */
  90. CTC_NR_DEV_STATES
  91. };
  92. static const char *dev_state_names[] = {
  93. "Stopped",
  94. "StartWait RXTX",
  95. "StartWait RX",
  96. "StartWait TX",
  97. "StopWait RXTX",
  98. "StopWait RX",
  99. "StopWait TX",
  100. "Running",
  101. };
  102. /**
  103. * Events of the interface statemachine.
  104. */
  105. enum dev_events {
  106. DEV_EVENT_START,
  107. DEV_EVENT_STOP,
  108. DEV_EVENT_RXUP,
  109. DEV_EVENT_TXUP,
  110. DEV_EVENT_RXDOWN,
  111. DEV_EVENT_TXDOWN,
  112. DEV_EVENT_RESTART,
  113. /**
  114. * MUST be always the last element!!
  115. */
  116. CTC_NR_DEV_EVENTS
  117. };
  118. static const char *dev_event_names[] = {
  119. "Start",
  120. "Stop",
  121. "RX up",
  122. "TX up",
  123. "RX down",
  124. "TX down",
  125. "Restart",
  126. };
  127. /**
  128. * Events of the channel statemachine
  129. */
  130. enum ch_events {
  131. /**
  132. * Events, representing return code of
  133. * I/O operations (ccw_device_start, ccw_device_halt et al.)
  134. */
  135. CH_EVENT_IO_SUCCESS,
  136. CH_EVENT_IO_EBUSY,
  137. CH_EVENT_IO_ENODEV,
  138. CH_EVENT_IO_EIO,
  139. CH_EVENT_IO_UNKNOWN,
  140. CH_EVENT_ATTNBUSY,
  141. CH_EVENT_ATTN,
  142. CH_EVENT_BUSY,
  143. /**
  144. * Events, representing unit-check
  145. */
  146. CH_EVENT_UC_RCRESET,
  147. CH_EVENT_UC_RSRESET,
  148. CH_EVENT_UC_TXTIMEOUT,
  149. CH_EVENT_UC_TXPARITY,
  150. CH_EVENT_UC_HWFAIL,
  151. CH_EVENT_UC_RXPARITY,
  152. CH_EVENT_UC_ZERO,
  153. CH_EVENT_UC_UNKNOWN,
  154. /**
  155. * Events, representing subchannel-check
  156. */
  157. CH_EVENT_SC_UNKNOWN,
  158. /**
  159. * Events, representing machine checks
  160. */
  161. CH_EVENT_MC_FAIL,
  162. CH_EVENT_MC_GOOD,
  163. /**
  164. * Event, representing normal IRQ
  165. */
  166. CH_EVENT_IRQ,
  167. CH_EVENT_FINSTAT,
  168. /**
  169. * Event, representing timer expiry.
  170. */
  171. CH_EVENT_TIMER,
  172. /**
  173. * Events, representing commands from upper levels.
  174. */
  175. CH_EVENT_START,
  176. CH_EVENT_STOP,
  177. /**
  178. * MUST be always the last element!!
  179. */
  180. NR_CH_EVENTS,
  181. };
  182. /**
  183. * States of the channel statemachine.
  184. */
  185. enum ch_states {
  186. /**
  187. * Channel not assigned to any device,
  188. * initial state, direction invalid
  189. */
  190. CH_STATE_IDLE,
  191. /**
  192. * Channel assigned but not operating
  193. */
  194. CH_STATE_STOPPED,
  195. CH_STATE_STARTWAIT,
  196. CH_STATE_STARTRETRY,
  197. CH_STATE_SETUPWAIT,
  198. CH_STATE_RXINIT,
  199. CH_STATE_TXINIT,
  200. CH_STATE_RX,
  201. CH_STATE_TX,
  202. CH_STATE_RXIDLE,
  203. CH_STATE_TXIDLE,
  204. CH_STATE_RXERR,
  205. CH_STATE_TXERR,
  206. CH_STATE_TERM,
  207. CH_STATE_DTERM,
  208. CH_STATE_NOTOP,
  209. /**
  210. * MUST be always the last element!!
  211. */
  212. NR_CH_STATES,
  213. };
  214. static int loglevel = CTC_LOGLEVEL_DEFAULT;
  215. /**
  216. * Linked list of all detected channels.
  217. */
  218. static struct channel *channels = NULL;
  219. /**
  220. * Print Banner.
  221. */
  222. static void
  223. print_banner(void)
  224. {
  225. static int printed = 0;
  226. char vbuf[] = "$Revision: 1.74 $";
  227. char *version = vbuf;
  228. if (printed)
  229. return;
  230. if ((version = strchr(version, ':'))) {
  231. char *p = strchr(version + 1, '$');
  232. if (p)
  233. *p = '\0';
  234. } else
  235. version = " ??? ";
  236. printk(KERN_INFO "CTC driver Version%s"
  237. #ifdef DEBUG
  238. " (DEBUG-VERSION, " __DATE__ __TIME__ ")"
  239. #endif
  240. " initialized\n", version);
  241. printed = 1;
  242. }
  243. /**
  244. * Return type of a detected device.
  245. */
  246. static enum channel_types
  247. get_channel_type(struct ccw_device_id *id)
  248. {
  249. enum channel_types type = (enum channel_types) id->driver_info;
  250. if (type == channel_type_ficon)
  251. type = channel_type_escon;
  252. return type;
  253. }
  254. static const char *ch_event_names[] = {
  255. "ccw_device success",
  256. "ccw_device busy",
  257. "ccw_device enodev",
  258. "ccw_device ioerr",
  259. "ccw_device unknown",
  260. "Status ATTN & BUSY",
  261. "Status ATTN",
  262. "Status BUSY",
  263. "Unit check remote reset",
  264. "Unit check remote system reset",
  265. "Unit check TX timeout",
  266. "Unit check TX parity",
  267. "Unit check Hardware failure",
  268. "Unit check RX parity",
  269. "Unit check ZERO",
  270. "Unit check Unknown",
  271. "SubChannel check Unknown",
  272. "Machine check failure",
  273. "Machine check operational",
  274. "IRQ normal",
  275. "IRQ final",
  276. "Timer",
  277. "Start",
  278. "Stop",
  279. };
  280. static const char *ch_state_names[] = {
  281. "Idle",
  282. "Stopped",
  283. "StartWait",
  284. "StartRetry",
  285. "SetupWait",
  286. "RX init",
  287. "TX init",
  288. "RX",
  289. "TX",
  290. "RX idle",
  291. "TX idle",
  292. "RX error",
  293. "TX error",
  294. "Terminating",
  295. "Restarting",
  296. "Not operational",
  297. };
  298. #ifdef DEBUG
  299. /**
  300. * Dump header and first 16 bytes of an sk_buff for debugging purposes.
  301. *
  302. * @param skb The sk_buff to dump.
  303. * @param offset Offset relative to skb-data, where to start the dump.
  304. */
  305. static void
  306. ctc_dump_skb(struct sk_buff *skb, int offset)
  307. {
  308. unsigned char *p = skb->data;
  309. __u16 bl;
  310. struct ll_header *header;
  311. int i;
  312. if (!(loglevel & CTC_LOGLEVEL_DEBUG))
  313. return;
  314. p += offset;
  315. bl = *((__u16 *) p);
  316. p += 2;
  317. header = (struct ll_header *) p;
  318. p -= 2;
  319. printk(KERN_DEBUG "dump:\n");
  320. printk(KERN_DEBUG "blocklen=%d %04x\n", bl, bl);
  321. printk(KERN_DEBUG "h->length=%d %04x\n", header->length,
  322. header->length);
  323. printk(KERN_DEBUG "h->type=%04x\n", header->type);
  324. printk(KERN_DEBUG "h->unused=%04x\n", header->unused);
  325. if (bl > 16)
  326. bl = 16;
  327. printk(KERN_DEBUG "data: ");
  328. for (i = 0; i < bl; i++)
  329. printk("%02x%s", *p++, (i % 16) ? " " : "\n<7>");
  330. printk("\n");
  331. }
  332. #else
  333. static inline void
  334. ctc_dump_skb(struct sk_buff *skb, int offset)
  335. {
  336. }
  337. #endif
  338. /**
  339. * Unpack a just received skb and hand it over to
  340. * upper layers.
  341. *
  342. * @param ch The channel where this skb has been received.
  343. * @param pskb The received skb.
  344. */
  345. static __inline__ void
  346. ctc_unpack_skb(struct channel *ch, struct sk_buff *pskb)
  347. {
  348. struct net_device *dev = ch->netdev;
  349. struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
  350. __u16 len = *((__u16 *) pskb->data);
  351. DBF_TEXT(trace, 4, __FUNCTION__);
  352. skb_put(pskb, 2 + LL_HEADER_LENGTH);
  353. skb_pull(pskb, 2);
  354. pskb->dev = dev;
  355. pskb->ip_summed = CHECKSUM_UNNECESSARY;
  356. while (len > 0) {
  357. struct sk_buff *skb;
  358. struct ll_header *header = (struct ll_header *) pskb->data;
  359. skb_pull(pskb, LL_HEADER_LENGTH);
  360. if ((ch->protocol == CTC_PROTO_S390) &&
  361. (header->type != ETH_P_IP)) {
  362. #ifndef DEBUG
  363. if (!(ch->logflags & LOG_FLAG_ILLEGALPKT)) {
  364. #endif
  365. /**
  366. * Check packet type only if we stick strictly
  367. * to S/390's protocol of OS390. This only
  368. * supports IP. Otherwise allow any packet
  369. * type.
  370. */
  371. ctc_pr_warn(
  372. "%s Illegal packet type 0x%04x received, dropping\n",
  373. dev->name, header->type);
  374. ch->logflags |= LOG_FLAG_ILLEGALPKT;
  375. #ifndef DEBUG
  376. }
  377. #endif
  378. #ifdef DEBUG
  379. ctc_dump_skb(pskb, -6);
  380. #endif
  381. privptr->stats.rx_dropped++;
  382. privptr->stats.rx_frame_errors++;
  383. return;
  384. }
  385. pskb->protocol = ntohs(header->type);
  386. if (header->length <= LL_HEADER_LENGTH) {
  387. #ifndef DEBUG
  388. if (!(ch->logflags & LOG_FLAG_ILLEGALSIZE)) {
  389. #endif
  390. ctc_pr_warn(
  391. "%s Illegal packet size %d "
  392. "received (MTU=%d blocklen=%d), "
  393. "dropping\n", dev->name, header->length,
  394. dev->mtu, len);
  395. ch->logflags |= LOG_FLAG_ILLEGALSIZE;
  396. #ifndef DEBUG
  397. }
  398. #endif
  399. #ifdef DEBUG
  400. ctc_dump_skb(pskb, -6);
  401. #endif
  402. privptr->stats.rx_dropped++;
  403. privptr->stats.rx_length_errors++;
  404. return;
  405. }
  406. header->length -= LL_HEADER_LENGTH;
  407. len -= LL_HEADER_LENGTH;
  408. if ((header->length > skb_tailroom(pskb)) ||
  409. (header->length > len)) {
  410. #ifndef DEBUG
  411. if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
  412. #endif
  413. ctc_pr_warn(
  414. "%s Illegal packet size %d "
  415. "(beyond the end of received data), "
  416. "dropping\n", dev->name, header->length);
  417. ch->logflags |= LOG_FLAG_OVERRUN;
  418. #ifndef DEBUG
  419. }
  420. #endif
  421. #ifdef DEBUG
  422. ctc_dump_skb(pskb, -6);
  423. #endif
  424. privptr->stats.rx_dropped++;
  425. privptr->stats.rx_length_errors++;
  426. return;
  427. }
  428. skb_put(pskb, header->length);
  429. pskb->mac.raw = pskb->data;
  430. len -= header->length;
  431. skb = dev_alloc_skb(pskb->len);
  432. if (!skb) {
  433. #ifndef DEBUG
  434. if (!(ch->logflags & LOG_FLAG_NOMEM)) {
  435. #endif
  436. ctc_pr_warn(
  437. "%s Out of memory in ctc_unpack_skb\n",
  438. dev->name);
  439. ch->logflags |= LOG_FLAG_NOMEM;
  440. #ifndef DEBUG
  441. }
  442. #endif
  443. privptr->stats.rx_dropped++;
  444. return;
  445. }
  446. memcpy(skb_put(skb, pskb->len), pskb->data, pskb->len);
  447. skb->mac.raw = skb->data;
  448. skb->dev = pskb->dev;
  449. skb->protocol = pskb->protocol;
  450. pskb->ip_summed = CHECKSUM_UNNECESSARY;
  451. if (ch->protocol == CTC_PROTO_LINUX_TTY)
  452. ctc_tty_netif_rx(skb);
  453. else
  454. netif_rx_ni(skb);
  455. /**
  456. * Successful rx; reset logflags
  457. */
  458. ch->logflags = 0;
  459. dev->last_rx = jiffies;
  460. privptr->stats.rx_packets++;
  461. privptr->stats.rx_bytes += skb->len;
  462. if (len > 0) {
  463. skb_pull(pskb, header->length);
  464. if (skb_tailroom(pskb) < LL_HEADER_LENGTH) {
  465. #ifndef DEBUG
  466. if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
  467. #endif
  468. ctc_pr_warn(
  469. "%s Overrun in ctc_unpack_skb\n",
  470. dev->name);
  471. ch->logflags |= LOG_FLAG_OVERRUN;
  472. #ifndef DEBUG
  473. }
  474. #endif
  475. return;
  476. }
  477. skb_put(pskb, LL_HEADER_LENGTH);
  478. }
  479. }
  480. }
  481. /**
  482. * Check return code of a preceeding ccw_device call, halt_IO etc...
  483. *
  484. * @param ch The channel, the error belongs to.
  485. * @param return_code The error code to inspect.
  486. */
  487. static void inline
  488. ccw_check_return_code(struct channel *ch, int return_code, char *msg)
  489. {
  490. DBF_TEXT(trace, 5, __FUNCTION__);
  491. switch (return_code) {
  492. case 0:
  493. fsm_event(ch->fsm, CH_EVENT_IO_SUCCESS, ch);
  494. break;
  495. case -EBUSY:
  496. ctc_pr_warn("%s (%s): Busy !\n", ch->id, msg);
  497. fsm_event(ch->fsm, CH_EVENT_IO_EBUSY, ch);
  498. break;
  499. case -ENODEV:
  500. ctc_pr_emerg("%s (%s): Invalid device called for IO\n",
  501. ch->id, msg);
  502. fsm_event(ch->fsm, CH_EVENT_IO_ENODEV, ch);
  503. break;
  504. case -EIO:
  505. ctc_pr_emerg("%s (%s): Status pending... \n",
  506. ch->id, msg);
  507. fsm_event(ch->fsm, CH_EVENT_IO_EIO, ch);
  508. break;
  509. default:
  510. ctc_pr_emerg("%s (%s): Unknown error in do_IO %04x\n",
  511. ch->id, msg, return_code);
  512. fsm_event(ch->fsm, CH_EVENT_IO_UNKNOWN, ch);
  513. }
  514. }
  515. /**
  516. * Check sense of a unit check.
  517. *
  518. * @param ch The channel, the sense code belongs to.
  519. * @param sense The sense code to inspect.
  520. */
  521. static void inline
  522. ccw_unit_check(struct channel *ch, unsigned char sense)
  523. {
  524. DBF_TEXT(trace, 5, __FUNCTION__);
  525. if (sense & SNS0_INTERVENTION_REQ) {
  526. if (sense & 0x01) {
  527. if (ch->protocol != CTC_PROTO_LINUX_TTY)
  528. ctc_pr_debug("%s: Interface disc. or Sel. reset "
  529. "(remote)\n", ch->id);
  530. fsm_event(ch->fsm, CH_EVENT_UC_RCRESET, ch);
  531. } else {
  532. ctc_pr_debug("%s: System reset (remote)\n", ch->id);
  533. fsm_event(ch->fsm, CH_EVENT_UC_RSRESET, ch);
  534. }
  535. } else if (sense & SNS0_EQUIPMENT_CHECK) {
  536. if (sense & SNS0_BUS_OUT_CHECK) {
  537. ctc_pr_warn("%s: Hardware malfunction (remote)\n",
  538. ch->id);
  539. fsm_event(ch->fsm, CH_EVENT_UC_HWFAIL, ch);
  540. } else {
  541. ctc_pr_warn("%s: Read-data parity error (remote)\n",
  542. ch->id);
  543. fsm_event(ch->fsm, CH_EVENT_UC_RXPARITY, ch);
  544. }
  545. } else if (sense & SNS0_BUS_OUT_CHECK) {
  546. if (sense & 0x04) {
  547. ctc_pr_warn("%s: Data-streaming timeout)\n", ch->id);
  548. fsm_event(ch->fsm, CH_EVENT_UC_TXTIMEOUT, ch);
  549. } else {
  550. ctc_pr_warn("%s: Data-transfer parity error\n", ch->id);
  551. fsm_event(ch->fsm, CH_EVENT_UC_TXPARITY, ch);
  552. }
  553. } else if (sense & SNS0_CMD_REJECT) {
  554. ctc_pr_warn("%s: Command reject\n", ch->id);
  555. } else if (sense == 0) {
  556. ctc_pr_debug("%s: Unit check ZERO\n", ch->id);
  557. fsm_event(ch->fsm, CH_EVENT_UC_ZERO, ch);
  558. } else {
  559. ctc_pr_warn("%s: Unit Check with sense code: %02x\n",
  560. ch->id, sense);
  561. fsm_event(ch->fsm, CH_EVENT_UC_UNKNOWN, ch);
  562. }
  563. }
  564. static void
  565. ctc_purge_skb_queue(struct sk_buff_head *q)
  566. {
  567. struct sk_buff *skb;
  568. DBF_TEXT(trace, 5, __FUNCTION__);
  569. while ((skb = skb_dequeue(q))) {
  570. atomic_dec(&skb->users);
  571. dev_kfree_skb_irq(skb);
  572. }
  573. }
  574. static __inline__ int
  575. ctc_checkalloc_buffer(struct channel *ch, int warn)
  576. {
  577. DBF_TEXT(trace, 5, __FUNCTION__);
  578. if ((ch->trans_skb == NULL) ||
  579. (ch->flags & CHANNEL_FLAGS_BUFSIZE_CHANGED)) {
  580. if (ch->trans_skb != NULL)
  581. dev_kfree_skb(ch->trans_skb);
  582. clear_normalized_cda(&ch->ccw[1]);
  583. ch->trans_skb = __dev_alloc_skb(ch->max_bufsize,
  584. GFP_ATOMIC | GFP_DMA);
  585. if (ch->trans_skb == NULL) {
  586. if (warn)
  587. ctc_pr_warn(
  588. "%s: Couldn't alloc %s trans_skb\n",
  589. ch->id,
  590. (CHANNEL_DIRECTION(ch->flags) == READ) ?
  591. "RX" : "TX");
  592. return -ENOMEM;
  593. }
  594. ch->ccw[1].count = ch->max_bufsize;
  595. if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
  596. dev_kfree_skb(ch->trans_skb);
  597. ch->trans_skb = NULL;
  598. if (warn)
  599. ctc_pr_warn(
  600. "%s: set_normalized_cda for %s "
  601. "trans_skb failed, dropping packets\n",
  602. ch->id,
  603. (CHANNEL_DIRECTION(ch->flags) == READ) ?
  604. "RX" : "TX");
  605. return -ENOMEM;
  606. }
  607. ch->ccw[1].count = 0;
  608. ch->trans_skb_data = ch->trans_skb->data;
  609. ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
  610. }
  611. return 0;
  612. }
  613. /**
  614. * Dummy NOP action for statemachines
  615. */
  616. static void
  617. fsm_action_nop(fsm_instance * fi, int event, void *arg)
  618. {
  619. }
  620. /**
  621. * Actions for channel - statemachines.
  622. *****************************************************************************/
  623. /**
  624. * Normal data has been send. Free the corresponding
  625. * skb (it's in io_queue), reset dev->tbusy and
  626. * revert to idle state.
  627. *
  628. * @param fi An instance of a channel statemachine.
  629. * @param event The event, just happened.
  630. * @param arg Generic pointer, casted from channel * upon call.
  631. */
  632. static void
  633. ch_action_txdone(fsm_instance * fi, int event, void *arg)
  634. {
  635. struct channel *ch = (struct channel *) arg;
  636. struct net_device *dev = ch->netdev;
  637. struct ctc_priv *privptr = dev->priv;
  638. struct sk_buff *skb;
  639. int first = 1;
  640. int i;
  641. unsigned long duration;
  642. struct timespec done_stamp = xtime;
  643. DBF_TEXT(trace, 4, __FUNCTION__);
  644. duration =
  645. (done_stamp.tv_sec - ch->prof.send_stamp.tv_sec) * 1000000 +
  646. (done_stamp.tv_nsec - ch->prof.send_stamp.tv_nsec) / 1000;
  647. if (duration > ch->prof.tx_time)
  648. ch->prof.tx_time = duration;
  649. if (ch->irb->scsw.count != 0)
  650. ctc_pr_debug("%s: TX not complete, remaining %d bytes\n",
  651. dev->name, ch->irb->scsw.count);
  652. fsm_deltimer(&ch->timer);
  653. while ((skb = skb_dequeue(&ch->io_queue))) {
  654. privptr->stats.tx_packets++;
  655. privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
  656. if (first) {
  657. privptr->stats.tx_bytes += 2;
  658. first = 0;
  659. }
  660. atomic_dec(&skb->users);
  661. dev_kfree_skb_irq(skb);
  662. }
  663. spin_lock(&ch->collect_lock);
  664. clear_normalized_cda(&ch->ccw[4]);
  665. if (ch->collect_len > 0) {
  666. int rc;
  667. if (ctc_checkalloc_buffer(ch, 1)) {
  668. spin_unlock(&ch->collect_lock);
  669. return;
  670. }
  671. ch->trans_skb->tail = ch->trans_skb->data = ch->trans_skb_data;
  672. ch->trans_skb->len = 0;
  673. if (ch->prof.maxmulti < (ch->collect_len + 2))
  674. ch->prof.maxmulti = ch->collect_len + 2;
  675. if (ch->prof.maxcqueue < skb_queue_len(&ch->collect_queue))
  676. ch->prof.maxcqueue = skb_queue_len(&ch->collect_queue);
  677. *((__u16 *) skb_put(ch->trans_skb, 2)) = ch->collect_len + 2;
  678. i = 0;
  679. while ((skb = skb_dequeue(&ch->collect_queue))) {
  680. memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
  681. skb->len);
  682. privptr->stats.tx_packets++;
  683. privptr->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
  684. atomic_dec(&skb->users);
  685. dev_kfree_skb_irq(skb);
  686. i++;
  687. }
  688. ch->collect_len = 0;
  689. spin_unlock(&ch->collect_lock);
  690. ch->ccw[1].count = ch->trans_skb->len;
  691. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
  692. ch->prof.send_stamp = xtime;
  693. rc = ccw_device_start(ch->cdev, &ch->ccw[0],
  694. (unsigned long) ch, 0xff, 0);
  695. ch->prof.doios_multi++;
  696. if (rc != 0) {
  697. privptr->stats.tx_dropped += i;
  698. privptr->stats.tx_errors += i;
  699. fsm_deltimer(&ch->timer);
  700. ccw_check_return_code(ch, rc, "chained TX");
  701. }
  702. } else {
  703. spin_unlock(&ch->collect_lock);
  704. fsm_newstate(fi, CH_STATE_TXIDLE);
  705. }
  706. ctc_clear_busy(dev);
  707. }
  708. /**
  709. * Initial data is sent.
  710. * Notify device statemachine that we are up and
  711. * running.
  712. *
  713. * @param fi An instance of a channel statemachine.
  714. * @param event The event, just happened.
  715. * @param arg Generic pointer, casted from channel * upon call.
  716. */
  717. static void
  718. ch_action_txidle(fsm_instance * fi, int event, void *arg)
  719. {
  720. struct channel *ch = (struct channel *) arg;
  721. DBF_TEXT(trace, 4, __FUNCTION__);
  722. fsm_deltimer(&ch->timer);
  723. fsm_newstate(fi, CH_STATE_TXIDLE);
  724. fsm_event(((struct ctc_priv *) ch->netdev->priv)->fsm, DEV_EVENT_TXUP,
  725. ch->netdev);
  726. }
  727. /**
  728. * Got normal data, check for sanity, queue it up, allocate new buffer
  729. * trigger bottom half, and initiate next read.
  730. *
  731. * @param fi An instance of a channel statemachine.
  732. * @param event The event, just happened.
  733. * @param arg Generic pointer, casted from channel * upon call.
  734. */
  735. static void
  736. ch_action_rx(fsm_instance * fi, int event, void *arg)
  737. {
  738. struct channel *ch = (struct channel *) arg;
  739. struct net_device *dev = ch->netdev;
  740. struct ctc_priv *privptr = dev->priv;
  741. int len = ch->max_bufsize - ch->irb->scsw.count;
  742. struct sk_buff *skb = ch->trans_skb;
  743. __u16 block_len = *((__u16 *) skb->data);
  744. int check_len;
  745. int rc;
  746. DBF_TEXT(trace, 4, __FUNCTION__);
  747. fsm_deltimer(&ch->timer);
  748. if (len < 8) {
  749. ctc_pr_debug("%s: got packet with length %d < 8\n",
  750. dev->name, len);
  751. privptr->stats.rx_dropped++;
  752. privptr->stats.rx_length_errors++;
  753. goto again;
  754. }
  755. if (len > ch->max_bufsize) {
  756. ctc_pr_debug("%s: got packet with length %d > %d\n",
  757. dev->name, len, ch->max_bufsize);
  758. privptr->stats.rx_dropped++;
  759. privptr->stats.rx_length_errors++;
  760. goto again;
  761. }
  762. /**
  763. * VM TCP seems to have a bug sending 2 trailing bytes of garbage.
  764. */
  765. switch (ch->protocol) {
  766. case CTC_PROTO_S390:
  767. case CTC_PROTO_OS390:
  768. check_len = block_len + 2;
  769. break;
  770. default:
  771. check_len = block_len;
  772. break;
  773. }
  774. if ((len < block_len) || (len > check_len)) {
  775. ctc_pr_debug("%s: got block length %d != rx length %d\n",
  776. dev->name, block_len, len);
  777. #ifdef DEBUG
  778. ctc_dump_skb(skb, 0);
  779. #endif
  780. *((__u16 *) skb->data) = len;
  781. privptr->stats.rx_dropped++;
  782. privptr->stats.rx_length_errors++;
  783. goto again;
  784. }
  785. block_len -= 2;
  786. if (block_len > 0) {
  787. *((__u16 *) skb->data) = block_len;
  788. ctc_unpack_skb(ch, skb);
  789. }
  790. again:
  791. skb->data = skb->tail = ch->trans_skb_data;
  792. skb->len = 0;
  793. if (ctc_checkalloc_buffer(ch, 1))
  794. return;
  795. ch->ccw[1].count = ch->max_bufsize;
  796. rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
  797. if (rc != 0)
  798. ccw_check_return_code(ch, rc, "normal RX");
  799. }
  800. static void ch_action_rxidle(fsm_instance * fi, int event, void *arg);
  801. /**
  802. * Initialize connection by sending a __u16 of value 0.
  803. *
  804. * @param fi An instance of a channel statemachine.
  805. * @param event The event, just happened.
  806. * @param arg Generic pointer, casted from channel * upon call.
  807. */
  808. static void
  809. ch_action_firstio(fsm_instance * fi, int event, void *arg)
  810. {
  811. struct channel *ch = (struct channel *) arg;
  812. int rc;
  813. DBF_TEXT(trace, 4, __FUNCTION__);
  814. if (fsm_getstate(fi) == CH_STATE_TXIDLE)
  815. ctc_pr_debug("%s: remote side issued READ?, init ...\n", ch->id);
  816. fsm_deltimer(&ch->timer);
  817. if (ctc_checkalloc_buffer(ch, 1))
  818. return;
  819. if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
  820. (ch->protocol == CTC_PROTO_OS390)) {
  821. /* OS/390 resp. z/OS */
  822. if (CHANNEL_DIRECTION(ch->flags) == READ) {
  823. *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
  824. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC,
  825. CH_EVENT_TIMER, ch);
  826. ch_action_rxidle(fi, event, arg);
  827. } else {
  828. struct net_device *dev = ch->netdev;
  829. fsm_newstate(fi, CH_STATE_TXIDLE);
  830. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  831. DEV_EVENT_TXUP, dev);
  832. }
  833. return;
  834. }
  835. /**
  836. * Don´t setup a timer for receiving the initial RX frame
  837. * if in compatibility mode, since VM TCP delays the initial
  838. * frame until it has some data to send.
  839. */
  840. if ((CHANNEL_DIRECTION(ch->flags) == WRITE) ||
  841. (ch->protocol != CTC_PROTO_S390))
  842. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
  843. *((__u16 *) ch->trans_skb->data) = CTC_INITIAL_BLOCKLEN;
  844. ch->ccw[1].count = 2; /* Transfer only length */
  845. fsm_newstate(fi, (CHANNEL_DIRECTION(ch->flags) == READ)
  846. ? CH_STATE_RXINIT : CH_STATE_TXINIT);
  847. rc = ccw_device_start(ch->cdev, &ch->ccw[0], (unsigned long) ch, 0xff, 0);
  848. if (rc != 0) {
  849. fsm_deltimer(&ch->timer);
  850. fsm_newstate(fi, CH_STATE_SETUPWAIT);
  851. ccw_check_return_code(ch, rc, "init IO");
  852. }
  853. /**
  854. * If in compatibility mode since we don´t setup a timer, we
  855. * also signal RX channel up immediately. This enables us
  856. * to send packets early which in turn usually triggers some
  857. * reply from VM TCP which brings up the RX channel to it´s
  858. * final state.
  859. */
  860. if ((CHANNEL_DIRECTION(ch->flags) == READ) &&
  861. (ch->protocol == CTC_PROTO_S390)) {
  862. struct net_device *dev = ch->netdev;
  863. fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXUP,
  864. dev);
  865. }
  866. }
  867. /**
  868. * Got initial data, check it. If OK,
  869. * notify device statemachine that we are up and
  870. * running.
  871. *
  872. * @param fi An instance of a channel statemachine.
  873. * @param event The event, just happened.
  874. * @param arg Generic pointer, casted from channel * upon call.
  875. */
  876. static void
  877. ch_action_rxidle(fsm_instance * fi, int event, void *arg)
  878. {
  879. struct channel *ch = (struct channel *) arg;
  880. struct net_device *dev = ch->netdev;
  881. __u16 buflen;
  882. int rc;
  883. DBF_TEXT(trace, 4, __FUNCTION__);
  884. fsm_deltimer(&ch->timer);
  885. buflen = *((__u16 *) ch->trans_skb->data);
  886. #ifdef DEBUG
  887. ctc_pr_debug("%s: Initial RX count %d\n", dev->name, buflen);
  888. #endif
  889. if (buflen >= CTC_INITIAL_BLOCKLEN) {
  890. if (ctc_checkalloc_buffer(ch, 1))
  891. return;
  892. ch->ccw[1].count = ch->max_bufsize;
  893. fsm_newstate(fi, CH_STATE_RXIDLE);
  894. rc = ccw_device_start(ch->cdev, &ch->ccw[0],
  895. (unsigned long) ch, 0xff, 0);
  896. if (rc != 0) {
  897. fsm_newstate(fi, CH_STATE_RXINIT);
  898. ccw_check_return_code(ch, rc, "initial RX");
  899. } else
  900. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  901. DEV_EVENT_RXUP, dev);
  902. } else {
  903. ctc_pr_debug("%s: Initial RX count %d not %d\n",
  904. dev->name, buflen, CTC_INITIAL_BLOCKLEN);
  905. ch_action_firstio(fi, event, arg);
  906. }
  907. }
  908. /**
  909. * Set channel into extended mode.
  910. *
  911. * @param fi An instance of a channel statemachine.
  912. * @param event The event, just happened.
  913. * @param arg Generic pointer, casted from channel * upon call.
  914. */
  915. static void
  916. ch_action_setmode(fsm_instance * fi, int event, void *arg)
  917. {
  918. struct channel *ch = (struct channel *) arg;
  919. int rc;
  920. unsigned long saveflags;
  921. DBF_TEXT(trace, 4, __FUNCTION__);
  922. fsm_deltimer(&ch->timer);
  923. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
  924. fsm_newstate(fi, CH_STATE_SETUPWAIT);
  925. saveflags = 0; /* avoids compiler warning with
  926. spin_unlock_irqrestore */
  927. if (event == CH_EVENT_TIMER) // only for timer not yet locked
  928. spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
  929. rc = ccw_device_start(ch->cdev, &ch->ccw[6], (unsigned long) ch, 0xff, 0);
  930. if (event == CH_EVENT_TIMER)
  931. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
  932. if (rc != 0) {
  933. fsm_deltimer(&ch->timer);
  934. fsm_newstate(fi, CH_STATE_STARTWAIT);
  935. ccw_check_return_code(ch, rc, "set Mode");
  936. } else
  937. ch->retry = 0;
  938. }
  939. /**
  940. * Setup channel.
  941. *
  942. * @param fi An instance of a channel statemachine.
  943. * @param event The event, just happened.
  944. * @param arg Generic pointer, casted from channel * upon call.
  945. */
  946. static void
  947. ch_action_start(fsm_instance * fi, int event, void *arg)
  948. {
  949. struct channel *ch = (struct channel *) arg;
  950. unsigned long saveflags;
  951. int rc;
  952. struct net_device *dev;
  953. DBF_TEXT(trace, 4, __FUNCTION__);
  954. if (ch == NULL) {
  955. ctc_pr_warn("ch_action_start ch=NULL\n");
  956. return;
  957. }
  958. if (ch->netdev == NULL) {
  959. ctc_pr_warn("ch_action_start dev=NULL, id=%s\n", ch->id);
  960. return;
  961. }
  962. dev = ch->netdev;
  963. #ifdef DEBUG
  964. ctc_pr_debug("%s: %s channel start\n", dev->name,
  965. (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
  966. #endif
  967. if (ch->trans_skb != NULL) {
  968. clear_normalized_cda(&ch->ccw[1]);
  969. dev_kfree_skb(ch->trans_skb);
  970. ch->trans_skb = NULL;
  971. }
  972. if (CHANNEL_DIRECTION(ch->flags) == READ) {
  973. ch->ccw[1].cmd_code = CCW_CMD_READ;
  974. ch->ccw[1].flags = CCW_FLAG_SLI;
  975. ch->ccw[1].count = 0;
  976. } else {
  977. ch->ccw[1].cmd_code = CCW_CMD_WRITE;
  978. ch->ccw[1].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
  979. ch->ccw[1].count = 0;
  980. }
  981. if (ctc_checkalloc_buffer(ch, 0)) {
  982. ctc_pr_notice(
  983. "%s: Could not allocate %s trans_skb, delaying "
  984. "allocation until first transfer\n",
  985. dev->name,
  986. (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
  987. }
  988. ch->ccw[0].cmd_code = CCW_CMD_PREPARE;
  989. ch->ccw[0].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
  990. ch->ccw[0].count = 0;
  991. ch->ccw[0].cda = 0;
  992. ch->ccw[2].cmd_code = CCW_CMD_NOOP; /* jointed CE + DE */
  993. ch->ccw[2].flags = CCW_FLAG_SLI;
  994. ch->ccw[2].count = 0;
  995. ch->ccw[2].cda = 0;
  996. memcpy(&ch->ccw[3], &ch->ccw[0], sizeof (struct ccw1) * 3);
  997. ch->ccw[4].cda = 0;
  998. ch->ccw[4].flags &= ~CCW_FLAG_IDA;
  999. fsm_newstate(fi, CH_STATE_STARTWAIT);
  1000. fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
  1001. spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
  1002. rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
  1003. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
  1004. if (rc != 0) {
  1005. if (rc != -EBUSY)
  1006. fsm_deltimer(&ch->timer);
  1007. ccw_check_return_code(ch, rc, "initial HaltIO");
  1008. }
  1009. #ifdef DEBUG
  1010. ctc_pr_debug("ctc: %s(): leaving\n", __func__);
  1011. #endif
  1012. }
  1013. /**
  1014. * Shutdown a channel.
  1015. *
  1016. * @param fi An instance of a channel statemachine.
  1017. * @param event The event, just happened.
  1018. * @param arg Generic pointer, casted from channel * upon call.
  1019. */
  1020. static void
  1021. ch_action_haltio(fsm_instance * fi, int event, void *arg)
  1022. {
  1023. struct channel *ch = (struct channel *) arg;
  1024. unsigned long saveflags;
  1025. int rc;
  1026. int oldstate;
  1027. DBF_TEXT(trace, 3, __FUNCTION__);
  1028. fsm_deltimer(&ch->timer);
  1029. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
  1030. saveflags = 0; /* avoids comp warning with
  1031. spin_unlock_irqrestore */
  1032. if (event == CH_EVENT_STOP) // only for STOP not yet locked
  1033. spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
  1034. oldstate = fsm_getstate(fi);
  1035. fsm_newstate(fi, CH_STATE_TERM);
  1036. rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
  1037. if (event == CH_EVENT_STOP)
  1038. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
  1039. if (rc != 0) {
  1040. if (rc != -EBUSY) {
  1041. fsm_deltimer(&ch->timer);
  1042. fsm_newstate(fi, oldstate);
  1043. }
  1044. ccw_check_return_code(ch, rc, "HaltIO in ch_action_haltio");
  1045. }
  1046. }
  1047. /**
  1048. * A channel has successfully been halted.
  1049. * Cleanup it's queue and notify interface statemachine.
  1050. *
  1051. * @param fi An instance of a channel statemachine.
  1052. * @param event The event, just happened.
  1053. * @param arg Generic pointer, casted from channel * upon call.
  1054. */
  1055. static void
  1056. ch_action_stopped(fsm_instance * fi, int event, void *arg)
  1057. {
  1058. struct channel *ch = (struct channel *) arg;
  1059. struct net_device *dev = ch->netdev;
  1060. DBF_TEXT(trace, 3, __FUNCTION__);
  1061. fsm_deltimer(&ch->timer);
  1062. fsm_newstate(fi, CH_STATE_STOPPED);
  1063. if (ch->trans_skb != NULL) {
  1064. clear_normalized_cda(&ch->ccw[1]);
  1065. dev_kfree_skb(ch->trans_skb);
  1066. ch->trans_skb = NULL;
  1067. }
  1068. if (CHANNEL_DIRECTION(ch->flags) == READ) {
  1069. skb_queue_purge(&ch->io_queue);
  1070. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1071. DEV_EVENT_RXDOWN, dev);
  1072. } else {
  1073. ctc_purge_skb_queue(&ch->io_queue);
  1074. spin_lock(&ch->collect_lock);
  1075. ctc_purge_skb_queue(&ch->collect_queue);
  1076. ch->collect_len = 0;
  1077. spin_unlock(&ch->collect_lock);
  1078. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1079. DEV_EVENT_TXDOWN, dev);
  1080. }
  1081. }
  1082. /**
  1083. * A stop command from device statemachine arrived and we are in
  1084. * not operational mode. Set state to stopped.
  1085. *
  1086. * @param fi An instance of a channel statemachine.
  1087. * @param event The event, just happened.
  1088. * @param arg Generic pointer, casted from channel * upon call.
  1089. */
  1090. static void
  1091. ch_action_stop(fsm_instance * fi, int event, void *arg)
  1092. {
  1093. fsm_newstate(fi, CH_STATE_STOPPED);
  1094. }
  1095. /**
  1096. * A machine check for no path, not operational status or gone device has
  1097. * happened.
  1098. * Cleanup queue and notify interface statemachine.
  1099. *
  1100. * @param fi An instance of a channel statemachine.
  1101. * @param event The event, just happened.
  1102. * @param arg Generic pointer, casted from channel * upon call.
  1103. */
  1104. static void
  1105. ch_action_fail(fsm_instance * fi, int event, void *arg)
  1106. {
  1107. struct channel *ch = (struct channel *) arg;
  1108. struct net_device *dev = ch->netdev;
  1109. DBF_TEXT(trace, 3, __FUNCTION__);
  1110. fsm_deltimer(&ch->timer);
  1111. fsm_newstate(fi, CH_STATE_NOTOP);
  1112. if (CHANNEL_DIRECTION(ch->flags) == READ) {
  1113. skb_queue_purge(&ch->io_queue);
  1114. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1115. DEV_EVENT_RXDOWN, dev);
  1116. } else {
  1117. ctc_purge_skb_queue(&ch->io_queue);
  1118. spin_lock(&ch->collect_lock);
  1119. ctc_purge_skb_queue(&ch->collect_queue);
  1120. ch->collect_len = 0;
  1121. spin_unlock(&ch->collect_lock);
  1122. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1123. DEV_EVENT_TXDOWN, dev);
  1124. }
  1125. }
  1126. /**
  1127. * Handle error during setup of channel.
  1128. *
  1129. * @param fi An instance of a channel statemachine.
  1130. * @param event The event, just happened.
  1131. * @param arg Generic pointer, casted from channel * upon call.
  1132. */
  1133. static void
  1134. ch_action_setuperr(fsm_instance * fi, int event, void *arg)
  1135. {
  1136. struct channel *ch = (struct channel *) arg;
  1137. struct net_device *dev = ch->netdev;
  1138. DBF_TEXT(setup, 3, __FUNCTION__);
  1139. /**
  1140. * Special case: Got UC_RCRESET on setmode.
  1141. * This means that remote side isn't setup. In this case
  1142. * simply retry after some 10 secs...
  1143. */
  1144. if ((fsm_getstate(fi) == CH_STATE_SETUPWAIT) &&
  1145. ((event == CH_EVENT_UC_RCRESET) ||
  1146. (event == CH_EVENT_UC_RSRESET))) {
  1147. fsm_newstate(fi, CH_STATE_STARTRETRY);
  1148. fsm_deltimer(&ch->timer);
  1149. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
  1150. if (CHANNEL_DIRECTION(ch->flags) == READ) {
  1151. int rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
  1152. if (rc != 0)
  1153. ccw_check_return_code(
  1154. ch, rc, "HaltIO in ch_action_setuperr");
  1155. }
  1156. return;
  1157. }
  1158. ctc_pr_debug("%s: Error %s during %s channel setup state=%s\n",
  1159. dev->name, ch_event_names[event],
  1160. (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX",
  1161. fsm_getstate_str(fi));
  1162. if (CHANNEL_DIRECTION(ch->flags) == READ) {
  1163. fsm_newstate(fi, CH_STATE_RXERR);
  1164. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1165. DEV_EVENT_RXDOWN, dev);
  1166. } else {
  1167. fsm_newstate(fi, CH_STATE_TXERR);
  1168. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1169. DEV_EVENT_TXDOWN, dev);
  1170. }
  1171. }
  1172. /**
  1173. * Restart a channel after an error.
  1174. *
  1175. * @param fi An instance of a channel statemachine.
  1176. * @param event The event, just happened.
  1177. * @param arg Generic pointer, casted from channel * upon call.
  1178. */
  1179. static void
  1180. ch_action_restart(fsm_instance * fi, int event, void *arg)
  1181. {
  1182. unsigned long saveflags;
  1183. int oldstate;
  1184. int rc;
  1185. struct channel *ch = (struct channel *) arg;
  1186. struct net_device *dev = ch->netdev;
  1187. DBF_TEXT(trace, 3, __FUNCTION__);
  1188. fsm_deltimer(&ch->timer);
  1189. ctc_pr_debug("%s: %s channel restart\n", dev->name,
  1190. (CHANNEL_DIRECTION(ch->flags) == READ) ? "RX" : "TX");
  1191. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
  1192. oldstate = fsm_getstate(fi);
  1193. fsm_newstate(fi, CH_STATE_STARTWAIT);
  1194. saveflags = 0; /* avoids compiler warning with
  1195. spin_unlock_irqrestore */
  1196. if (event == CH_EVENT_TIMER) // only for timer not yet locked
  1197. spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
  1198. rc = ccw_device_halt(ch->cdev, (unsigned long) ch);
  1199. if (event == CH_EVENT_TIMER)
  1200. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
  1201. if (rc != 0) {
  1202. if (rc != -EBUSY) {
  1203. fsm_deltimer(&ch->timer);
  1204. fsm_newstate(fi, oldstate);
  1205. }
  1206. ccw_check_return_code(ch, rc, "HaltIO in ch_action_restart");
  1207. }
  1208. }
  1209. /**
  1210. * Handle error during RX initial handshake (exchange of
  1211. * 0-length block header)
  1212. *
  1213. * @param fi An instance of a channel statemachine.
  1214. * @param event The event, just happened.
  1215. * @param arg Generic pointer, casted from channel * upon call.
  1216. */
  1217. static void
  1218. ch_action_rxiniterr(fsm_instance * fi, int event, void *arg)
  1219. {
  1220. struct channel *ch = (struct channel *) arg;
  1221. struct net_device *dev = ch->netdev;
  1222. DBF_TEXT(setup, 3, __FUNCTION__);
  1223. if (event == CH_EVENT_TIMER) {
  1224. fsm_deltimer(&ch->timer);
  1225. ctc_pr_debug("%s: Timeout during RX init handshake\n", dev->name);
  1226. if (ch->retry++ < 3)
  1227. ch_action_restart(fi, event, arg);
  1228. else {
  1229. fsm_newstate(fi, CH_STATE_RXERR);
  1230. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1231. DEV_EVENT_RXDOWN, dev);
  1232. }
  1233. } else
  1234. ctc_pr_warn("%s: Error during RX init handshake\n", dev->name);
  1235. }
  1236. /**
  1237. * Notify device statemachine if we gave up initialization
  1238. * of RX channel.
  1239. *
  1240. * @param fi An instance of a channel statemachine.
  1241. * @param event The event, just happened.
  1242. * @param arg Generic pointer, casted from channel * upon call.
  1243. */
  1244. static void
  1245. ch_action_rxinitfail(fsm_instance * fi, int event, void *arg)
  1246. {
  1247. struct channel *ch = (struct channel *) arg;
  1248. struct net_device *dev = ch->netdev;
  1249. DBF_TEXT(setup, 3, __FUNCTION__);
  1250. fsm_newstate(fi, CH_STATE_RXERR);
  1251. ctc_pr_warn("%s: RX initialization failed\n", dev->name);
  1252. ctc_pr_warn("%s: RX <-> RX connection detected\n", dev->name);
  1253. fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
  1254. }
  1255. /**
  1256. * Handle RX Unit check remote reset (remote disconnected)
  1257. *
  1258. * @param fi An instance of a channel statemachine.
  1259. * @param event The event, just happened.
  1260. * @param arg Generic pointer, casted from channel * upon call.
  1261. */
  1262. static void
  1263. ch_action_rxdisc(fsm_instance * fi, int event, void *arg)
  1264. {
  1265. struct channel *ch = (struct channel *) arg;
  1266. struct channel *ch2;
  1267. struct net_device *dev = ch->netdev;
  1268. DBF_TEXT(trace, 3, __FUNCTION__);
  1269. fsm_deltimer(&ch->timer);
  1270. ctc_pr_debug("%s: Got remote disconnect, re-initializing ...\n",
  1271. dev->name);
  1272. /**
  1273. * Notify device statemachine
  1274. */
  1275. fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_RXDOWN, dev);
  1276. fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_TXDOWN, dev);
  1277. fsm_newstate(fi, CH_STATE_DTERM);
  1278. ch2 = ((struct ctc_priv *) dev->priv)->channel[WRITE];
  1279. fsm_newstate(ch2->fsm, CH_STATE_DTERM);
  1280. ccw_device_halt(ch->cdev, (unsigned long) ch);
  1281. ccw_device_halt(ch2->cdev, (unsigned long) ch2);
  1282. }
  1283. /**
  1284. * Handle error during TX channel initialization.
  1285. *
  1286. * @param fi An instance of a channel statemachine.
  1287. * @param event The event, just happened.
  1288. * @param arg Generic pointer, casted from channel * upon call.
  1289. */
  1290. static void
  1291. ch_action_txiniterr(fsm_instance * fi, int event, void *arg)
  1292. {
  1293. struct channel *ch = (struct channel *) arg;
  1294. struct net_device *dev = ch->netdev;
  1295. DBF_TEXT(setup, 2, __FUNCTION__);
  1296. if (event == CH_EVENT_TIMER) {
  1297. fsm_deltimer(&ch->timer);
  1298. ctc_pr_debug("%s: Timeout during TX init handshake\n", dev->name);
  1299. if (ch->retry++ < 3)
  1300. ch_action_restart(fi, event, arg);
  1301. else {
  1302. fsm_newstate(fi, CH_STATE_TXERR);
  1303. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1304. DEV_EVENT_TXDOWN, dev);
  1305. }
  1306. } else
  1307. ctc_pr_warn("%s: Error during TX init handshake\n", dev->name);
  1308. }
  1309. /**
  1310. * Handle TX timeout by retrying operation.
  1311. *
  1312. * @param fi An instance of a channel statemachine.
  1313. * @param event The event, just happened.
  1314. * @param arg Generic pointer, casted from channel * upon call.
  1315. */
  1316. static void
  1317. ch_action_txretry(fsm_instance * fi, int event, void *arg)
  1318. {
  1319. struct channel *ch = (struct channel *) arg;
  1320. struct net_device *dev = ch->netdev;
  1321. unsigned long saveflags;
  1322. DBF_TEXT(trace, 4, __FUNCTION__);
  1323. fsm_deltimer(&ch->timer);
  1324. if (ch->retry++ > 3) {
  1325. ctc_pr_debug("%s: TX retry failed, restarting channel\n",
  1326. dev->name);
  1327. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1328. DEV_EVENT_TXDOWN, dev);
  1329. ch_action_restart(fi, event, arg);
  1330. } else {
  1331. struct sk_buff *skb;
  1332. ctc_pr_debug("%s: TX retry %d\n", dev->name, ch->retry);
  1333. if ((skb = skb_peek(&ch->io_queue))) {
  1334. int rc = 0;
  1335. clear_normalized_cda(&ch->ccw[4]);
  1336. ch->ccw[4].count = skb->len;
  1337. if (set_normalized_cda(&ch->ccw[4], skb->data)) {
  1338. ctc_pr_debug(
  1339. "%s: IDAL alloc failed, chan restart\n",
  1340. dev->name);
  1341. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1342. DEV_EVENT_TXDOWN, dev);
  1343. ch_action_restart(fi, event, arg);
  1344. return;
  1345. }
  1346. fsm_addtimer(&ch->timer, 1000, CH_EVENT_TIMER, ch);
  1347. saveflags = 0; /* avoids compiler warning with
  1348. spin_unlock_irqrestore */
  1349. if (event == CH_EVENT_TIMER) // only for TIMER not yet locked
  1350. spin_lock_irqsave(get_ccwdev_lock(ch->cdev),
  1351. saveflags);
  1352. rc = ccw_device_start(ch->cdev, &ch->ccw[3],
  1353. (unsigned long) ch, 0xff, 0);
  1354. if (event == CH_EVENT_TIMER)
  1355. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev),
  1356. saveflags);
  1357. if (rc != 0) {
  1358. fsm_deltimer(&ch->timer);
  1359. ccw_check_return_code(ch, rc, "TX in ch_action_txretry");
  1360. ctc_purge_skb_queue(&ch->io_queue);
  1361. }
  1362. }
  1363. }
  1364. }
  1365. /**
  1366. * Handle fatal errors during an I/O command.
  1367. *
  1368. * @param fi An instance of a channel statemachine.
  1369. * @param event The event, just happened.
  1370. * @param arg Generic pointer, casted from channel * upon call.
  1371. */
  1372. static void
  1373. ch_action_iofatal(fsm_instance * fi, int event, void *arg)
  1374. {
  1375. struct channel *ch = (struct channel *) arg;
  1376. struct net_device *dev = ch->netdev;
  1377. DBF_TEXT(trace, 3, __FUNCTION__);
  1378. fsm_deltimer(&ch->timer);
  1379. if (CHANNEL_DIRECTION(ch->flags) == READ) {
  1380. ctc_pr_debug("%s: RX I/O error\n", dev->name);
  1381. fsm_newstate(fi, CH_STATE_RXERR);
  1382. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1383. DEV_EVENT_RXDOWN, dev);
  1384. } else {
  1385. ctc_pr_debug("%s: TX I/O error\n", dev->name);
  1386. fsm_newstate(fi, CH_STATE_TXERR);
  1387. fsm_event(((struct ctc_priv *) dev->priv)->fsm,
  1388. DEV_EVENT_TXDOWN, dev);
  1389. }
  1390. }
  1391. static void
  1392. ch_action_reinit(fsm_instance *fi, int event, void *arg)
  1393. {
  1394. struct channel *ch = (struct channel *)arg;
  1395. struct net_device *dev = ch->netdev;
  1396. struct ctc_priv *privptr = dev->priv;
  1397. DBF_TEXT(trace, 4, __FUNCTION__);
  1398. ch_action_iofatal(fi, event, arg);
  1399. fsm_addtimer(&privptr->restart_timer, 1000, DEV_EVENT_RESTART, dev);
  1400. }
  1401. /**
  1402. * The statemachine for a channel.
  1403. */
  1404. static const fsm_node ch_fsm[] = {
  1405. {CH_STATE_STOPPED, CH_EVENT_STOP, fsm_action_nop },
  1406. {CH_STATE_STOPPED, CH_EVENT_START, ch_action_start },
  1407. {CH_STATE_STOPPED, CH_EVENT_FINSTAT, fsm_action_nop },
  1408. {CH_STATE_STOPPED, CH_EVENT_MC_FAIL, fsm_action_nop },
  1409. {CH_STATE_NOTOP, CH_EVENT_STOP, ch_action_stop },
  1410. {CH_STATE_NOTOP, CH_EVENT_START, fsm_action_nop },
  1411. {CH_STATE_NOTOP, CH_EVENT_FINSTAT, fsm_action_nop },
  1412. {CH_STATE_NOTOP, CH_EVENT_MC_FAIL, fsm_action_nop },
  1413. {CH_STATE_NOTOP, CH_EVENT_MC_GOOD, ch_action_start },
  1414. {CH_STATE_STARTWAIT, CH_EVENT_STOP, ch_action_haltio },
  1415. {CH_STATE_STARTWAIT, CH_EVENT_START, fsm_action_nop },
  1416. {CH_STATE_STARTWAIT, CH_EVENT_FINSTAT, ch_action_setmode },
  1417. {CH_STATE_STARTWAIT, CH_EVENT_TIMER, ch_action_setuperr },
  1418. {CH_STATE_STARTWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
  1419. {CH_STATE_STARTWAIT, CH_EVENT_IO_EIO, ch_action_reinit },
  1420. {CH_STATE_STARTWAIT, CH_EVENT_MC_FAIL, ch_action_fail },
  1421. {CH_STATE_STARTRETRY, CH_EVENT_STOP, ch_action_haltio },
  1422. {CH_STATE_STARTRETRY, CH_EVENT_TIMER, ch_action_setmode },
  1423. {CH_STATE_STARTRETRY, CH_EVENT_FINSTAT, fsm_action_nop },
  1424. {CH_STATE_STARTRETRY, CH_EVENT_MC_FAIL, ch_action_fail },
  1425. {CH_STATE_SETUPWAIT, CH_EVENT_STOP, ch_action_haltio },
  1426. {CH_STATE_SETUPWAIT, CH_EVENT_START, fsm_action_nop },
  1427. {CH_STATE_SETUPWAIT, CH_EVENT_FINSTAT, ch_action_firstio },
  1428. {CH_STATE_SETUPWAIT, CH_EVENT_UC_RCRESET, ch_action_setuperr },
  1429. {CH_STATE_SETUPWAIT, CH_EVENT_UC_RSRESET, ch_action_setuperr },
  1430. {CH_STATE_SETUPWAIT, CH_EVENT_TIMER, ch_action_setmode },
  1431. {CH_STATE_SETUPWAIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
  1432. {CH_STATE_SETUPWAIT, CH_EVENT_IO_EIO, ch_action_reinit },
  1433. {CH_STATE_SETUPWAIT, CH_EVENT_MC_FAIL, ch_action_fail },
  1434. {CH_STATE_RXINIT, CH_EVENT_STOP, ch_action_haltio },
  1435. {CH_STATE_RXINIT, CH_EVENT_START, fsm_action_nop },
  1436. {CH_STATE_RXINIT, CH_EVENT_FINSTAT, ch_action_rxidle },
  1437. {CH_STATE_RXINIT, CH_EVENT_UC_RCRESET, ch_action_rxiniterr },
  1438. {CH_STATE_RXINIT, CH_EVENT_UC_RSRESET, ch_action_rxiniterr },
  1439. {CH_STATE_RXINIT, CH_EVENT_TIMER, ch_action_rxiniterr },
  1440. {CH_STATE_RXINIT, CH_EVENT_ATTNBUSY, ch_action_rxinitfail },
  1441. {CH_STATE_RXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
  1442. {CH_STATE_RXINIT, CH_EVENT_IO_EIO, ch_action_reinit },
  1443. {CH_STATE_RXINIT, CH_EVENT_UC_ZERO, ch_action_firstio },
  1444. {CH_STATE_RXINIT, CH_EVENT_MC_FAIL, ch_action_fail },
  1445. {CH_STATE_RXIDLE, CH_EVENT_STOP, ch_action_haltio },
  1446. {CH_STATE_RXIDLE, CH_EVENT_START, fsm_action_nop },
  1447. {CH_STATE_RXIDLE, CH_EVENT_FINSTAT, ch_action_rx },
  1448. {CH_STATE_RXIDLE, CH_EVENT_UC_RCRESET, ch_action_rxdisc },
  1449. // {CH_STATE_RXIDLE, CH_EVENT_UC_RSRESET, ch_action_rxretry },
  1450. {CH_STATE_RXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal },
  1451. {CH_STATE_RXIDLE, CH_EVENT_IO_EIO, ch_action_reinit },
  1452. {CH_STATE_RXIDLE, CH_EVENT_MC_FAIL, ch_action_fail },
  1453. {CH_STATE_RXIDLE, CH_EVENT_UC_ZERO, ch_action_rx },
  1454. {CH_STATE_TXINIT, CH_EVENT_STOP, ch_action_haltio },
  1455. {CH_STATE_TXINIT, CH_EVENT_START, fsm_action_nop },
  1456. {CH_STATE_TXINIT, CH_EVENT_FINSTAT, ch_action_txidle },
  1457. {CH_STATE_TXINIT, CH_EVENT_UC_RCRESET, ch_action_txiniterr },
  1458. {CH_STATE_TXINIT, CH_EVENT_UC_RSRESET, ch_action_txiniterr },
  1459. {CH_STATE_TXINIT, CH_EVENT_TIMER, ch_action_txiniterr },
  1460. {CH_STATE_TXINIT, CH_EVENT_IO_ENODEV, ch_action_iofatal },
  1461. {CH_STATE_TXINIT, CH_EVENT_IO_EIO, ch_action_reinit },
  1462. {CH_STATE_TXINIT, CH_EVENT_MC_FAIL, ch_action_fail },
  1463. {CH_STATE_TXIDLE, CH_EVENT_STOP, ch_action_haltio },
  1464. {CH_STATE_TXIDLE, CH_EVENT_START, fsm_action_nop },
  1465. {CH_STATE_TXIDLE, CH_EVENT_FINSTAT, ch_action_firstio },
  1466. {CH_STATE_TXIDLE, CH_EVENT_UC_RCRESET, fsm_action_nop },
  1467. {CH_STATE_TXIDLE, CH_EVENT_UC_RSRESET, fsm_action_nop },
  1468. {CH_STATE_TXIDLE, CH_EVENT_IO_ENODEV, ch_action_iofatal },
  1469. {CH_STATE_TXIDLE, CH_EVENT_IO_EIO, ch_action_reinit },
  1470. {CH_STATE_TXIDLE, CH_EVENT_MC_FAIL, ch_action_fail },
  1471. {CH_STATE_TERM, CH_EVENT_STOP, fsm_action_nop },
  1472. {CH_STATE_TERM, CH_EVENT_START, ch_action_restart },
  1473. {CH_STATE_TERM, CH_EVENT_FINSTAT, ch_action_stopped },
  1474. {CH_STATE_TERM, CH_EVENT_UC_RCRESET, fsm_action_nop },
  1475. {CH_STATE_TERM, CH_EVENT_UC_RSRESET, fsm_action_nop },
  1476. {CH_STATE_TERM, CH_EVENT_MC_FAIL, ch_action_fail },
  1477. {CH_STATE_DTERM, CH_EVENT_STOP, ch_action_haltio },
  1478. {CH_STATE_DTERM, CH_EVENT_START, ch_action_restart },
  1479. {CH_STATE_DTERM, CH_EVENT_FINSTAT, ch_action_setmode },
  1480. {CH_STATE_DTERM, CH_EVENT_UC_RCRESET, fsm_action_nop },
  1481. {CH_STATE_DTERM, CH_EVENT_UC_RSRESET, fsm_action_nop },
  1482. {CH_STATE_DTERM, CH_EVENT_MC_FAIL, ch_action_fail },
  1483. {CH_STATE_TX, CH_EVENT_STOP, ch_action_haltio },
  1484. {CH_STATE_TX, CH_EVENT_START, fsm_action_nop },
  1485. {CH_STATE_TX, CH_EVENT_FINSTAT, ch_action_txdone },
  1486. {CH_STATE_TX, CH_EVENT_UC_RCRESET, ch_action_txretry },
  1487. {CH_STATE_TX, CH_EVENT_UC_RSRESET, ch_action_txretry },
  1488. {CH_STATE_TX, CH_EVENT_TIMER, ch_action_txretry },
  1489. {CH_STATE_TX, CH_EVENT_IO_ENODEV, ch_action_iofatal },
  1490. {CH_STATE_TX, CH_EVENT_IO_EIO, ch_action_reinit },
  1491. {CH_STATE_TX, CH_EVENT_MC_FAIL, ch_action_fail },
  1492. {CH_STATE_RXERR, CH_EVENT_STOP, ch_action_haltio },
  1493. {CH_STATE_TXERR, CH_EVENT_STOP, ch_action_haltio },
  1494. {CH_STATE_TXERR, CH_EVENT_MC_FAIL, ch_action_fail },
  1495. {CH_STATE_RXERR, CH_EVENT_MC_FAIL, ch_action_fail },
  1496. };
  1497. static const int CH_FSM_LEN = sizeof (ch_fsm) / sizeof (fsm_node);
  1498. /**
  1499. * Functions related to setup and device detection.
  1500. *****************************************************************************/
  1501. static inline int
  1502. less_than(char *id1, char *id2)
  1503. {
  1504. int dev1, dev2, i;
  1505. for (i = 0; i < 5; i++) {
  1506. id1++;
  1507. id2++;
  1508. }
  1509. dev1 = simple_strtoul(id1, &id1, 16);
  1510. dev2 = simple_strtoul(id2, &id2, 16);
  1511. return (dev1 < dev2);
  1512. }
  1513. /**
  1514. * Add a new channel to the list of channels.
  1515. * Keeps the channel list sorted.
  1516. *
  1517. * @param cdev The ccw_device to be added.
  1518. * @param type The type class of the new channel.
  1519. *
  1520. * @return 0 on success, !0 on error.
  1521. */
  1522. static int
  1523. add_channel(struct ccw_device *cdev, enum channel_types type)
  1524. {
  1525. struct channel **c = &channels;
  1526. struct channel *ch;
  1527. DBF_TEXT(trace, 2, __FUNCTION__);
  1528. if ((ch =
  1529. (struct channel *) kmalloc(sizeof (struct channel),
  1530. GFP_KERNEL)) == NULL) {
  1531. ctc_pr_warn("ctc: Out of memory in add_channel\n");
  1532. return -1;
  1533. }
  1534. memset(ch, 0, sizeof (struct channel));
  1535. if ((ch->ccw = (struct ccw1 *) kmalloc(8*sizeof(struct ccw1),
  1536. GFP_KERNEL | GFP_DMA)) == NULL) {
  1537. kfree(ch);
  1538. ctc_pr_warn("ctc: Out of memory in add_channel\n");
  1539. return -1;
  1540. }
  1541. memset(ch->ccw, 0, 8*sizeof(struct ccw1)); // assure all flags and counters are reset
  1542. /**
  1543. * "static" ccws are used in the following way:
  1544. *
  1545. * ccw[0..2] (Channel program for generic I/O):
  1546. * 0: prepare
  1547. * 1: read or write (depending on direction) with fixed
  1548. * buffer (idal allocated once when buffer is allocated)
  1549. * 2: nop
  1550. * ccw[3..5] (Channel program for direct write of packets)
  1551. * 3: prepare
  1552. * 4: write (idal allocated on every write).
  1553. * 5: nop
  1554. * ccw[6..7] (Channel program for initial channel setup):
  1555. * 6: set extended mode
  1556. * 7: nop
  1557. *
  1558. * ch->ccw[0..5] are initialized in ch_action_start because
  1559. * the channel's direction is yet unknown here.
  1560. */
  1561. ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
  1562. ch->ccw[6].flags = CCW_FLAG_SLI;
  1563. ch->ccw[7].cmd_code = CCW_CMD_NOOP;
  1564. ch->ccw[7].flags = CCW_FLAG_SLI;
  1565. ch->cdev = cdev;
  1566. snprintf(ch->id, CTC_ID_SIZE, "ch-%s", cdev->dev.bus_id);
  1567. ch->type = type;
  1568. ch->fsm = init_fsm(ch->id, ch_state_names,
  1569. ch_event_names, NR_CH_STATES, NR_CH_EVENTS,
  1570. ch_fsm, CH_FSM_LEN, GFP_KERNEL);
  1571. if (ch->fsm == NULL) {
  1572. ctc_pr_warn("ctc: Could not create FSM in add_channel\n");
  1573. kfree(ch->ccw);
  1574. kfree(ch);
  1575. return -1;
  1576. }
  1577. fsm_newstate(ch->fsm, CH_STATE_IDLE);
  1578. if ((ch->irb = (struct irb *) kmalloc(sizeof (struct irb),
  1579. GFP_KERNEL)) == NULL) {
  1580. ctc_pr_warn("ctc: Out of memory in add_channel\n");
  1581. kfree_fsm(ch->fsm);
  1582. kfree(ch->ccw);
  1583. kfree(ch);
  1584. return -1;
  1585. }
  1586. memset(ch->irb, 0, sizeof (struct irb));
  1587. while (*c && less_than((*c)->id, ch->id))
  1588. c = &(*c)->next;
  1589. if (*c && (!strncmp((*c)->id, ch->id, CTC_ID_SIZE))) {
  1590. ctc_pr_debug(
  1591. "ctc: add_channel: device %s already in list, "
  1592. "using old entry\n", (*c)->id);
  1593. kfree(ch->irb);
  1594. kfree_fsm(ch->fsm);
  1595. kfree(ch->ccw);
  1596. kfree(ch);
  1597. return 0;
  1598. }
  1599. fsm_settimer(ch->fsm, &ch->timer);
  1600. skb_queue_head_init(&ch->io_queue);
  1601. skb_queue_head_init(&ch->collect_queue);
  1602. ch->next = *c;
  1603. *c = ch;
  1604. return 0;
  1605. }
  1606. /**
  1607. * Release a specific channel in the channel list.
  1608. *
  1609. * @param ch Pointer to channel struct to be released.
  1610. */
  1611. static void
  1612. channel_free(struct channel *ch)
  1613. {
  1614. ch->flags &= ~CHANNEL_FLAGS_INUSE;
  1615. fsm_newstate(ch->fsm, CH_STATE_IDLE);
  1616. }
  1617. /**
  1618. * Remove a specific channel in the channel list.
  1619. *
  1620. * @param ch Pointer to channel struct to be released.
  1621. */
  1622. static void
  1623. channel_remove(struct channel *ch)
  1624. {
  1625. struct channel **c = &channels;
  1626. DBF_TEXT(trace, 2, __FUNCTION__);
  1627. if (ch == NULL)
  1628. return;
  1629. channel_free(ch);
  1630. while (*c) {
  1631. if (*c == ch) {
  1632. *c = ch->next;
  1633. fsm_deltimer(&ch->timer);
  1634. kfree_fsm(ch->fsm);
  1635. clear_normalized_cda(&ch->ccw[4]);
  1636. if (ch->trans_skb != NULL) {
  1637. clear_normalized_cda(&ch->ccw[1]);
  1638. dev_kfree_skb(ch->trans_skb);
  1639. }
  1640. kfree(ch->ccw);
  1641. kfree(ch->irb);
  1642. kfree(ch);
  1643. return;
  1644. }
  1645. c = &((*c)->next);
  1646. }
  1647. }
  1648. /**
  1649. * Get a specific channel from the channel list.
  1650. *
  1651. * @param type Type of channel we are interested in.
  1652. * @param id Id of channel we are interested in.
  1653. * @param direction Direction we want to use this channel for.
  1654. *
  1655. * @return Pointer to a channel or NULL if no matching channel available.
  1656. */
  1657. static struct channel
  1658. *
  1659. channel_get(enum channel_types type, char *id, int direction)
  1660. {
  1661. struct channel *ch = channels;
  1662. DBF_TEXT(trace, 3, __FUNCTION__);
  1663. #ifdef DEBUG
  1664. ctc_pr_debug("ctc: %s(): searching for ch with id %s and type %d\n",
  1665. __func__, id, type);
  1666. #endif
  1667. while (ch && ((strncmp(ch->id, id, CTC_ID_SIZE)) || (ch->type != type))) {
  1668. #ifdef DEBUG
  1669. ctc_pr_debug("ctc: %s(): ch=0x%p (id=%s, type=%d\n",
  1670. __func__, ch, ch->id, ch->type);
  1671. #endif
  1672. ch = ch->next;
  1673. }
  1674. #ifdef DEBUG
  1675. ctc_pr_debug("ctc: %s(): ch=0x%pq (id=%s, type=%d\n",
  1676. __func__, ch, ch->id, ch->type);
  1677. #endif
  1678. if (!ch) {
  1679. ctc_pr_warn("ctc: %s(): channel with id %s "
  1680. "and type %d not found in channel list\n",
  1681. __func__, id, type);
  1682. } else {
  1683. if (ch->flags & CHANNEL_FLAGS_INUSE)
  1684. ch = NULL;
  1685. else {
  1686. ch->flags |= CHANNEL_FLAGS_INUSE;
  1687. ch->flags &= ~CHANNEL_FLAGS_RWMASK;
  1688. ch->flags |= (direction == WRITE)
  1689. ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
  1690. fsm_newstate(ch->fsm, CH_STATE_STOPPED);
  1691. }
  1692. }
  1693. return ch;
  1694. }
  1695. /**
  1696. * Return the channel type by name.
  1697. *
  1698. * @param name Name of network interface.
  1699. *
  1700. * @return Type class of channel to be used for that interface.
  1701. */
  1702. static enum channel_types inline
  1703. extract_channel_media(char *name)
  1704. {
  1705. enum channel_types ret = channel_type_unknown;
  1706. if (name != NULL) {
  1707. if (strncmp(name, "ctc", 3) == 0)
  1708. ret = channel_type_parallel;
  1709. if (strncmp(name, "escon", 5) == 0)
  1710. ret = channel_type_escon;
  1711. }
  1712. return ret;
  1713. }
  1714. static long
  1715. __ctc_check_irb_error(struct ccw_device *cdev, struct irb *irb)
  1716. {
  1717. if (!IS_ERR(irb))
  1718. return 0;
  1719. switch (PTR_ERR(irb)) {
  1720. case -EIO:
  1721. ctc_pr_warn("i/o-error on device %s\n", cdev->dev.bus_id);
  1722. // CTC_DBF_TEXT(trace, 2, "ckirberr");
  1723. // CTC_DBF_TEXT_(trace, 2, " rc%d", -EIO);
  1724. break;
  1725. case -ETIMEDOUT:
  1726. ctc_pr_warn("timeout on device %s\n", cdev->dev.bus_id);
  1727. // CTC_DBF_TEXT(trace, 2, "ckirberr");
  1728. // CTC_DBF_TEXT_(trace, 2, " rc%d", -ETIMEDOUT);
  1729. break;
  1730. default:
  1731. ctc_pr_warn("unknown error %ld on device %s\n", PTR_ERR(irb),
  1732. cdev->dev.bus_id);
  1733. // CTC_DBF_TEXT(trace, 2, "ckirberr");
  1734. // CTC_DBF_TEXT(trace, 2, " rc???");
  1735. }
  1736. return PTR_ERR(irb);
  1737. }
  1738. /**
  1739. * Main IRQ handler.
  1740. *
  1741. * @param cdev The ccw_device the interrupt is for.
  1742. * @param intparm interruption parameter.
  1743. * @param irb interruption response block.
  1744. */
  1745. static void
  1746. ctc_irq_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  1747. {
  1748. struct channel *ch;
  1749. struct net_device *dev;
  1750. struct ctc_priv *priv;
  1751. DBF_TEXT(trace, 5, __FUNCTION__);
  1752. if (__ctc_check_irb_error(cdev, irb))
  1753. return;
  1754. /* Check for unsolicited interrupts. */
  1755. if (!cdev->dev.driver_data) {
  1756. ctc_pr_warn("ctc: Got unsolicited irq: %s c-%02x d-%02x\n",
  1757. cdev->dev.bus_id, irb->scsw.cstat,
  1758. irb->scsw.dstat);
  1759. return;
  1760. }
  1761. priv = ((struct ccwgroup_device *)cdev->dev.driver_data)
  1762. ->dev.driver_data;
  1763. /* Try to extract channel from driver data. */
  1764. if (priv->channel[READ]->cdev == cdev)
  1765. ch = priv->channel[READ];
  1766. else if (priv->channel[WRITE]->cdev == cdev)
  1767. ch = priv->channel[WRITE];
  1768. else {
  1769. ctc_pr_err("ctc: Can't determine channel for interrupt, "
  1770. "device %s\n", cdev->dev.bus_id);
  1771. return;
  1772. }
  1773. dev = (struct net_device *) (ch->netdev);
  1774. if (dev == NULL) {
  1775. ctc_pr_crit("ctc: ctc_irq_handler dev=NULL bus_id=%s, ch=0x%p\n",
  1776. cdev->dev.bus_id, ch);
  1777. return;
  1778. }
  1779. #ifdef DEBUG
  1780. ctc_pr_debug("%s: interrupt for device: %s received c-%02x d-%02x\n",
  1781. dev->name, ch->id, irb->scsw.cstat, irb->scsw.dstat);
  1782. #endif
  1783. /* Copy interruption response block. */
  1784. memcpy(ch->irb, irb, sizeof(struct irb));
  1785. /* Check for good subchannel return code, otherwise error message */
  1786. if (ch->irb->scsw.cstat) {
  1787. fsm_event(ch->fsm, CH_EVENT_SC_UNKNOWN, ch);
  1788. ctc_pr_warn("%s: subchannel check for device: %s - %02x %02x\n",
  1789. dev->name, ch->id, ch->irb->scsw.cstat,
  1790. ch->irb->scsw.dstat);
  1791. return;
  1792. }
  1793. /* Check the reason-code of a unit check */
  1794. if (ch->irb->scsw.dstat & DEV_STAT_UNIT_CHECK) {
  1795. ccw_unit_check(ch, ch->irb->ecw[0]);
  1796. return;
  1797. }
  1798. if (ch->irb->scsw.dstat & DEV_STAT_BUSY) {
  1799. if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION)
  1800. fsm_event(ch->fsm, CH_EVENT_ATTNBUSY, ch);
  1801. else
  1802. fsm_event(ch->fsm, CH_EVENT_BUSY, ch);
  1803. return;
  1804. }
  1805. if (ch->irb->scsw.dstat & DEV_STAT_ATTENTION) {
  1806. fsm_event(ch->fsm, CH_EVENT_ATTN, ch);
  1807. return;
  1808. }
  1809. if ((ch->irb->scsw.stctl & SCSW_STCTL_SEC_STATUS) ||
  1810. (ch->irb->scsw.stctl == SCSW_STCTL_STATUS_PEND) ||
  1811. (ch->irb->scsw.stctl ==
  1812. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
  1813. fsm_event(ch->fsm, CH_EVENT_FINSTAT, ch);
  1814. else
  1815. fsm_event(ch->fsm, CH_EVENT_IRQ, ch);
  1816. }
  1817. /**
  1818. * Actions for interface - statemachine.
  1819. *****************************************************************************/
  1820. /**
  1821. * Startup channels by sending CH_EVENT_START to each channel.
  1822. *
  1823. * @param fi An instance of an interface statemachine.
  1824. * @param event The event, just happened.
  1825. * @param arg Generic pointer, casted from struct net_device * upon call.
  1826. */
  1827. static void
  1828. dev_action_start(fsm_instance * fi, int event, void *arg)
  1829. {
  1830. struct net_device *dev = (struct net_device *) arg;
  1831. struct ctc_priv *privptr = dev->priv;
  1832. int direction;
  1833. DBF_TEXT(setup, 3, __FUNCTION__);
  1834. fsm_deltimer(&privptr->restart_timer);
  1835. fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
  1836. for (direction = READ; direction <= WRITE; direction++) {
  1837. struct channel *ch = privptr->channel[direction];
  1838. fsm_event(ch->fsm, CH_EVENT_START, ch);
  1839. }
  1840. }
  1841. /**
  1842. * Shutdown channels by sending CH_EVENT_STOP to each channel.
  1843. *
  1844. * @param fi An instance of an interface statemachine.
  1845. * @param event The event, just happened.
  1846. * @param arg Generic pointer, casted from struct net_device * upon call.
  1847. */
  1848. static void
  1849. dev_action_stop(fsm_instance * fi, int event, void *arg)
  1850. {
  1851. struct net_device *dev = (struct net_device *) arg;
  1852. struct ctc_priv *privptr = dev->priv;
  1853. int direction;
  1854. DBF_TEXT(trace, 3, __FUNCTION__);
  1855. fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
  1856. for (direction = READ; direction <= WRITE; direction++) {
  1857. struct channel *ch = privptr->channel[direction];
  1858. fsm_event(ch->fsm, CH_EVENT_STOP, ch);
  1859. }
  1860. }
  1861. static void
  1862. dev_action_restart(fsm_instance *fi, int event, void *arg)
  1863. {
  1864. struct net_device *dev = (struct net_device *)arg;
  1865. struct ctc_priv *privptr = dev->priv;
  1866. DBF_TEXT(trace, 3, __FUNCTION__);
  1867. ctc_pr_debug("%s: Restarting\n", dev->name);
  1868. dev_action_stop(fi, event, arg);
  1869. fsm_event(privptr->fsm, DEV_EVENT_STOP, dev);
  1870. fsm_addtimer(&privptr->restart_timer, CTC_TIMEOUT_5SEC,
  1871. DEV_EVENT_START, dev);
  1872. }
  1873. /**
  1874. * Called from channel statemachine
  1875. * when a channel is up and running.
  1876. *
  1877. * @param fi An instance of an interface statemachine.
  1878. * @param event The event, just happened.
  1879. * @param arg Generic pointer, casted from struct net_device * upon call.
  1880. */
  1881. static void
  1882. dev_action_chup(fsm_instance * fi, int event, void *arg)
  1883. {
  1884. struct net_device *dev = (struct net_device *) arg;
  1885. struct ctc_priv *privptr = dev->priv;
  1886. DBF_TEXT(trace, 3, __FUNCTION__);
  1887. switch (fsm_getstate(fi)) {
  1888. case DEV_STATE_STARTWAIT_RXTX:
  1889. if (event == DEV_EVENT_RXUP)
  1890. fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
  1891. else
  1892. fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
  1893. break;
  1894. case DEV_STATE_STARTWAIT_RX:
  1895. if (event == DEV_EVENT_RXUP) {
  1896. fsm_newstate(fi, DEV_STATE_RUNNING);
  1897. ctc_pr_info("%s: connected with remote side\n",
  1898. dev->name);
  1899. if (privptr->protocol == CTC_PROTO_LINUX_TTY)
  1900. ctc_tty_setcarrier(dev, 1);
  1901. ctc_clear_busy(dev);
  1902. }
  1903. break;
  1904. case DEV_STATE_STARTWAIT_TX:
  1905. if (event == DEV_EVENT_TXUP) {
  1906. fsm_newstate(fi, DEV_STATE_RUNNING);
  1907. ctc_pr_info("%s: connected with remote side\n",
  1908. dev->name);
  1909. if (privptr->protocol == CTC_PROTO_LINUX_TTY)
  1910. ctc_tty_setcarrier(dev, 1);
  1911. ctc_clear_busy(dev);
  1912. }
  1913. break;
  1914. case DEV_STATE_STOPWAIT_TX:
  1915. if (event == DEV_EVENT_RXUP)
  1916. fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
  1917. break;
  1918. case DEV_STATE_STOPWAIT_RX:
  1919. if (event == DEV_EVENT_TXUP)
  1920. fsm_newstate(fi, DEV_STATE_STOPWAIT_RXTX);
  1921. break;
  1922. }
  1923. }
  1924. /**
  1925. * Called from channel statemachine
  1926. * when a channel has been shutdown.
  1927. *
  1928. * @param fi An instance of an interface statemachine.
  1929. * @param event The event, just happened.
  1930. * @param arg Generic pointer, casted from struct net_device * upon call.
  1931. */
  1932. static void
  1933. dev_action_chdown(fsm_instance * fi, int event, void *arg)
  1934. {
  1935. struct net_device *dev = (struct net_device *) arg;
  1936. struct ctc_priv *privptr = dev->priv;
  1937. DBF_TEXT(trace, 3, __FUNCTION__);
  1938. switch (fsm_getstate(fi)) {
  1939. case DEV_STATE_RUNNING:
  1940. if (privptr->protocol == CTC_PROTO_LINUX_TTY)
  1941. ctc_tty_setcarrier(dev, 0);
  1942. if (event == DEV_EVENT_TXDOWN)
  1943. fsm_newstate(fi, DEV_STATE_STARTWAIT_TX);
  1944. else
  1945. fsm_newstate(fi, DEV_STATE_STARTWAIT_RX);
  1946. break;
  1947. case DEV_STATE_STARTWAIT_RX:
  1948. if (event == DEV_EVENT_TXDOWN)
  1949. fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
  1950. break;
  1951. case DEV_STATE_STARTWAIT_TX:
  1952. if (event == DEV_EVENT_RXDOWN)
  1953. fsm_newstate(fi, DEV_STATE_STARTWAIT_RXTX);
  1954. break;
  1955. case DEV_STATE_STOPWAIT_RXTX:
  1956. if (event == DEV_EVENT_TXDOWN)
  1957. fsm_newstate(fi, DEV_STATE_STOPWAIT_RX);
  1958. else
  1959. fsm_newstate(fi, DEV_STATE_STOPWAIT_TX);
  1960. break;
  1961. case DEV_STATE_STOPWAIT_RX:
  1962. if (event == DEV_EVENT_RXDOWN)
  1963. fsm_newstate(fi, DEV_STATE_STOPPED);
  1964. break;
  1965. case DEV_STATE_STOPWAIT_TX:
  1966. if (event == DEV_EVENT_TXDOWN)
  1967. fsm_newstate(fi, DEV_STATE_STOPPED);
  1968. break;
  1969. }
  1970. }
  1971. static const fsm_node dev_fsm[] = {
  1972. {DEV_STATE_STOPPED, DEV_EVENT_START, dev_action_start},
  1973. {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_START, dev_action_start },
  1974. {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown },
  1975. {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown },
  1976. {DEV_STATE_STOPWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
  1977. {DEV_STATE_STOPWAIT_RX, DEV_EVENT_START, dev_action_start },
  1978. {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXUP, dev_action_chup },
  1979. {DEV_STATE_STOPWAIT_RX, DEV_EVENT_TXUP, dev_action_chup },
  1980. {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RXDOWN, dev_action_chdown },
  1981. {DEV_STATE_STOPWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
  1982. {DEV_STATE_STOPWAIT_TX, DEV_EVENT_START, dev_action_start },
  1983. {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RXUP, dev_action_chup },
  1984. {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXUP, dev_action_chup },
  1985. {DEV_STATE_STOPWAIT_TX, DEV_EVENT_TXDOWN, dev_action_chdown },
  1986. {DEV_STATE_STOPWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
  1987. {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_STOP, dev_action_stop },
  1988. {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXUP, dev_action_chup },
  1989. {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXUP, dev_action_chup },
  1990. {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RXDOWN, dev_action_chdown },
  1991. {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_TXDOWN, dev_action_chdown },
  1992. {DEV_STATE_STARTWAIT_RXTX, DEV_EVENT_RESTART, dev_action_restart },
  1993. {DEV_STATE_STARTWAIT_TX, DEV_EVENT_STOP, dev_action_stop },
  1994. {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXUP, dev_action_chup },
  1995. {DEV_STATE_STARTWAIT_TX, DEV_EVENT_TXUP, dev_action_chup },
  1996. {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RXDOWN, dev_action_chdown },
  1997. {DEV_STATE_STARTWAIT_TX, DEV_EVENT_RESTART, dev_action_restart },
  1998. {DEV_STATE_STARTWAIT_RX, DEV_EVENT_STOP, dev_action_stop },
  1999. {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RXUP, dev_action_chup },
  2000. {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXUP, dev_action_chup },
  2001. {DEV_STATE_STARTWAIT_RX, DEV_EVENT_TXDOWN, dev_action_chdown },
  2002. {DEV_STATE_STARTWAIT_RX, DEV_EVENT_RESTART, dev_action_restart },
  2003. {DEV_STATE_RUNNING, DEV_EVENT_STOP, dev_action_stop },
  2004. {DEV_STATE_RUNNING, DEV_EVENT_RXDOWN, dev_action_chdown },
  2005. {DEV_STATE_RUNNING, DEV_EVENT_TXDOWN, dev_action_chdown },
  2006. {DEV_STATE_RUNNING, DEV_EVENT_TXUP, fsm_action_nop },
  2007. {DEV_STATE_RUNNING, DEV_EVENT_RXUP, fsm_action_nop },
  2008. {DEV_STATE_RUNNING, DEV_EVENT_RESTART, dev_action_restart },
  2009. };
  2010. static const int DEV_FSM_LEN = sizeof (dev_fsm) / sizeof (fsm_node);
  2011. /**
  2012. * Transmit a packet.
  2013. * This is a helper function for ctc_tx().
  2014. *
  2015. * @param ch Channel to be used for sending.
  2016. * @param skb Pointer to struct sk_buff of packet to send.
  2017. * The linklevel header has already been set up
  2018. * by ctc_tx().
  2019. *
  2020. * @return 0 on success, -ERRNO on failure. (Never fails.)
  2021. */
  2022. static int
  2023. transmit_skb(struct channel *ch, struct sk_buff *skb)
  2024. {
  2025. unsigned long saveflags;
  2026. struct ll_header header;
  2027. int rc = 0;
  2028. DBF_TEXT(trace, 5, __FUNCTION__);
  2029. if (fsm_getstate(ch->fsm) != CH_STATE_TXIDLE) {
  2030. int l = skb->len + LL_HEADER_LENGTH;
  2031. spin_lock_irqsave(&ch->collect_lock, saveflags);
  2032. if (ch->collect_len + l > ch->max_bufsize - 2)
  2033. rc = -EBUSY;
  2034. else {
  2035. atomic_inc(&skb->users);
  2036. header.length = l;
  2037. header.type = skb->protocol;
  2038. header.unused = 0;
  2039. memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
  2040. LL_HEADER_LENGTH);
  2041. skb_queue_tail(&ch->collect_queue, skb);
  2042. ch->collect_len += l;
  2043. }
  2044. spin_unlock_irqrestore(&ch->collect_lock, saveflags);
  2045. } else {
  2046. __u16 block_len;
  2047. int ccw_idx;
  2048. struct sk_buff *nskb;
  2049. unsigned long hi;
  2050. /**
  2051. * Protect skb against beeing free'd by upper
  2052. * layers.
  2053. */
  2054. atomic_inc(&skb->users);
  2055. ch->prof.txlen += skb->len;
  2056. header.length = skb->len + LL_HEADER_LENGTH;
  2057. header.type = skb->protocol;
  2058. header.unused = 0;
  2059. memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
  2060. LL_HEADER_LENGTH);
  2061. block_len = skb->len + 2;
  2062. *((__u16 *) skb_push(skb, 2)) = block_len;
  2063. /**
  2064. * IDAL support in CTC is broken, so we have to
  2065. * care about skb's above 2G ourselves.
  2066. */
  2067. hi = ((unsigned long) skb->tail + LL_HEADER_LENGTH) >> 31;
  2068. if (hi) {
  2069. nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
  2070. if (!nskb) {
  2071. atomic_dec(&skb->users);
  2072. skb_pull(skb, LL_HEADER_LENGTH + 2);
  2073. return -ENOMEM;
  2074. } else {
  2075. memcpy(skb_put(nskb, skb->len),
  2076. skb->data, skb->len);
  2077. atomic_inc(&nskb->users);
  2078. atomic_dec(&skb->users);
  2079. dev_kfree_skb_irq(skb);
  2080. skb = nskb;
  2081. }
  2082. }
  2083. ch->ccw[4].count = block_len;
  2084. if (set_normalized_cda(&ch->ccw[4], skb->data)) {
  2085. /**
  2086. * idal allocation failed, try via copying to
  2087. * trans_skb. trans_skb usually has a pre-allocated
  2088. * idal.
  2089. */
  2090. if (ctc_checkalloc_buffer(ch, 1)) {
  2091. /**
  2092. * Remove our header. It gets added
  2093. * again on retransmit.
  2094. */
  2095. atomic_dec(&skb->users);
  2096. skb_pull(skb, LL_HEADER_LENGTH + 2);
  2097. return -EBUSY;
  2098. }
  2099. ch->trans_skb->tail = ch->trans_skb->data;
  2100. ch->trans_skb->len = 0;
  2101. ch->ccw[1].count = skb->len;
  2102. memcpy(skb_put(ch->trans_skb, skb->len), skb->data,
  2103. skb->len);
  2104. atomic_dec(&skb->users);
  2105. dev_kfree_skb_irq(skb);
  2106. ccw_idx = 0;
  2107. } else {
  2108. skb_queue_tail(&ch->io_queue, skb);
  2109. ccw_idx = 3;
  2110. }
  2111. ch->retry = 0;
  2112. fsm_newstate(ch->fsm, CH_STATE_TX);
  2113. fsm_addtimer(&ch->timer, CTC_TIMEOUT_5SEC, CH_EVENT_TIMER, ch);
  2114. spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
  2115. ch->prof.send_stamp = xtime;
  2116. rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
  2117. (unsigned long) ch, 0xff, 0);
  2118. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
  2119. if (ccw_idx == 3)
  2120. ch->prof.doios_single++;
  2121. if (rc != 0) {
  2122. fsm_deltimer(&ch->timer);
  2123. ccw_check_return_code(ch, rc, "single skb TX");
  2124. if (ccw_idx == 3)
  2125. skb_dequeue_tail(&ch->io_queue);
  2126. /**
  2127. * Remove our header. It gets added
  2128. * again on retransmit.
  2129. */
  2130. skb_pull(skb, LL_HEADER_LENGTH + 2);
  2131. } else {
  2132. if (ccw_idx == 0) {
  2133. struct net_device *dev = ch->netdev;
  2134. struct ctc_priv *privptr = dev->priv;
  2135. privptr->stats.tx_packets++;
  2136. privptr->stats.tx_bytes +=
  2137. skb->len - LL_HEADER_LENGTH;
  2138. }
  2139. }
  2140. }
  2141. return rc;
  2142. }
  2143. /**
  2144. * Interface API for upper network layers
  2145. *****************************************************************************/
  2146. /**
  2147. * Open an interface.
  2148. * Called from generic network layer when ifconfig up is run.
  2149. *
  2150. * @param dev Pointer to interface struct.
  2151. *
  2152. * @return 0 on success, -ERRNO on failure. (Never fails.)
  2153. */
  2154. static int
  2155. ctc_open(struct net_device * dev)
  2156. {
  2157. DBF_TEXT(trace, 5, __FUNCTION__);
  2158. fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_START, dev);
  2159. return 0;
  2160. }
  2161. /**
  2162. * Close an interface.
  2163. * Called from generic network layer when ifconfig down is run.
  2164. *
  2165. * @param dev Pointer to interface struct.
  2166. *
  2167. * @return 0 on success, -ERRNO on failure. (Never fails.)
  2168. */
  2169. static int
  2170. ctc_close(struct net_device * dev)
  2171. {
  2172. DBF_TEXT(trace, 5, __FUNCTION__);
  2173. fsm_event(((struct ctc_priv *) dev->priv)->fsm, DEV_EVENT_STOP, dev);
  2174. return 0;
  2175. }
  2176. /**
  2177. * Start transmission of a packet.
  2178. * Called from generic network device layer.
  2179. *
  2180. * @param skb Pointer to buffer containing the packet.
  2181. * @param dev Pointer to interface struct.
  2182. *
  2183. * @return 0 if packet consumed, !0 if packet rejected.
  2184. * Note: If we return !0, then the packet is free'd by
  2185. * the generic network layer.
  2186. */
  2187. static int
  2188. ctc_tx(struct sk_buff *skb, struct net_device * dev)
  2189. {
  2190. int rc = 0;
  2191. struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
  2192. DBF_TEXT(trace, 5, __FUNCTION__);
  2193. /**
  2194. * Some sanity checks ...
  2195. */
  2196. if (skb == NULL) {
  2197. ctc_pr_warn("%s: NULL sk_buff passed\n", dev->name);
  2198. privptr->stats.tx_dropped++;
  2199. return 0;
  2200. }
  2201. if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
  2202. ctc_pr_warn("%s: Got sk_buff with head room < %ld bytes\n",
  2203. dev->name, LL_HEADER_LENGTH + 2);
  2204. dev_kfree_skb(skb);
  2205. privptr->stats.tx_dropped++;
  2206. return 0;
  2207. }
  2208. /**
  2209. * If channels are not running, try to restart them
  2210. * and throw away packet.
  2211. */
  2212. if (fsm_getstate(privptr->fsm) != DEV_STATE_RUNNING) {
  2213. fsm_event(privptr->fsm, DEV_EVENT_START, dev);
  2214. if (privptr->protocol == CTC_PROTO_LINUX_TTY)
  2215. return -EBUSY;
  2216. dev_kfree_skb(skb);
  2217. privptr->stats.tx_dropped++;
  2218. privptr->stats.tx_errors++;
  2219. privptr->stats.tx_carrier_errors++;
  2220. return 0;
  2221. }
  2222. if (ctc_test_and_set_busy(dev))
  2223. return -EBUSY;
  2224. dev->trans_start = jiffies;
  2225. if (transmit_skb(privptr->channel[WRITE], skb) != 0)
  2226. rc = 1;
  2227. ctc_clear_busy(dev);
  2228. return rc;
  2229. }
  2230. /**
  2231. * Sets MTU of an interface.
  2232. *
  2233. * @param dev Pointer to interface struct.
  2234. * @param new_mtu The new MTU to use for this interface.
  2235. *
  2236. * @return 0 on success, -EINVAL if MTU is out of valid range.
  2237. * (valid range is 576 .. 65527). If VM is on the
  2238. * remote side, maximum MTU is 32760, however this is
  2239. * <em>not</em> checked here.
  2240. */
  2241. static int
  2242. ctc_change_mtu(struct net_device * dev, int new_mtu)
  2243. {
  2244. struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
  2245. DBF_TEXT(trace, 3, __FUNCTION__);
  2246. if ((new_mtu < 576) || (new_mtu > 65527) ||
  2247. (new_mtu > (privptr->channel[READ]->max_bufsize -
  2248. LL_HEADER_LENGTH - 2)))
  2249. return -EINVAL;
  2250. dev->mtu = new_mtu;
  2251. dev->hard_header_len = LL_HEADER_LENGTH + 2;
  2252. return 0;
  2253. }
  2254. /**
  2255. * Returns interface statistics of a device.
  2256. *
  2257. * @param dev Pointer to interface struct.
  2258. *
  2259. * @return Pointer to stats struct of this interface.
  2260. */
  2261. static struct net_device_stats *
  2262. ctc_stats(struct net_device * dev)
  2263. {
  2264. return &((struct ctc_priv *) dev->priv)->stats;
  2265. }
  2266. /*
  2267. * sysfs attributes
  2268. */
  2269. static ssize_t
  2270. buffer_show(struct device *dev, struct device_attribute *attr, char *buf)
  2271. {
  2272. struct ctc_priv *priv;
  2273. priv = dev->driver_data;
  2274. if (!priv)
  2275. return -ENODEV;
  2276. return sprintf(buf, "%d\n",
  2277. priv->buffer_size);
  2278. }
  2279. static ssize_t
  2280. buffer_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  2281. {
  2282. struct ctc_priv *priv;
  2283. struct net_device *ndev;
  2284. int bs1;
  2285. char buffer[16];
  2286. DBF_TEXT(trace, 3, __FUNCTION__);
  2287. DBF_TEXT(trace, 3, buf);
  2288. priv = dev->driver_data;
  2289. if (!priv) {
  2290. DBF_TEXT(trace, 3, "bfnopriv");
  2291. return -ENODEV;
  2292. }
  2293. sscanf(buf, "%u", &bs1);
  2294. if (bs1 > CTC_BUFSIZE_LIMIT)
  2295. goto einval;
  2296. if (bs1 < (576 + LL_HEADER_LENGTH + 2))
  2297. goto einval;
  2298. priv->buffer_size = bs1; // just to overwrite the default
  2299. ndev = priv->channel[READ]->netdev;
  2300. if (!ndev) {
  2301. DBF_TEXT(trace, 3, "bfnondev");
  2302. return -ENODEV;
  2303. }
  2304. if ((ndev->flags & IFF_RUNNING) &&
  2305. (bs1 < (ndev->mtu + LL_HEADER_LENGTH + 2)))
  2306. goto einval;
  2307. priv->channel[READ]->max_bufsize = bs1;
  2308. priv->channel[WRITE]->max_bufsize = bs1;
  2309. if (!(ndev->flags & IFF_RUNNING))
  2310. ndev->mtu = bs1 - LL_HEADER_LENGTH - 2;
  2311. priv->channel[READ]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
  2312. priv->channel[WRITE]->flags |= CHANNEL_FLAGS_BUFSIZE_CHANGED;
  2313. sprintf(buffer, "%d",priv->buffer_size);
  2314. DBF_TEXT(trace, 3, buffer);
  2315. return count;
  2316. einval:
  2317. DBF_TEXT(trace, 3, "buff_err");
  2318. return -EINVAL;
  2319. }
  2320. static ssize_t
  2321. loglevel_show(struct device *dev, struct device_attribute *attr, char *buf)
  2322. {
  2323. return sprintf(buf, "%d\n", loglevel);
  2324. }
  2325. static ssize_t
  2326. loglevel_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  2327. {
  2328. int ll1;
  2329. DBF_TEXT(trace, 5, __FUNCTION__);
  2330. sscanf(buf, "%i", &ll1);
  2331. if ((ll1 > CTC_LOGLEVEL_MAX) || (ll1 < 0))
  2332. return -EINVAL;
  2333. loglevel = ll1;
  2334. return count;
  2335. }
  2336. static void
  2337. ctc_print_statistics(struct ctc_priv *priv)
  2338. {
  2339. char *sbuf;
  2340. char *p;
  2341. DBF_TEXT(trace, 4, __FUNCTION__);
  2342. if (!priv)
  2343. return;
  2344. sbuf = (char *)kmalloc(2048, GFP_KERNEL);
  2345. if (sbuf == NULL)
  2346. return;
  2347. p = sbuf;
  2348. p += sprintf(p, " Device FSM state: %s\n",
  2349. fsm_getstate_str(priv->fsm));
  2350. p += sprintf(p, " RX channel FSM state: %s\n",
  2351. fsm_getstate_str(priv->channel[READ]->fsm));
  2352. p += sprintf(p, " TX channel FSM state: %s\n",
  2353. fsm_getstate_str(priv->channel[WRITE]->fsm));
  2354. p += sprintf(p, " Max. TX buffer used: %ld\n",
  2355. priv->channel[WRITE]->prof.maxmulti);
  2356. p += sprintf(p, " Max. chained SKBs: %ld\n",
  2357. priv->channel[WRITE]->prof.maxcqueue);
  2358. p += sprintf(p, " TX single write ops: %ld\n",
  2359. priv->channel[WRITE]->prof.doios_single);
  2360. p += sprintf(p, " TX multi write ops: %ld\n",
  2361. priv->channel[WRITE]->prof.doios_multi);
  2362. p += sprintf(p, " Netto bytes written: %ld\n",
  2363. priv->channel[WRITE]->prof.txlen);
  2364. p += sprintf(p, " Max. TX IO-time: %ld\n",
  2365. priv->channel[WRITE]->prof.tx_time);
  2366. ctc_pr_debug("Statistics for %s:\n%s",
  2367. priv->channel[WRITE]->netdev->name, sbuf);
  2368. kfree(sbuf);
  2369. return;
  2370. }
  2371. static ssize_t
  2372. stats_show(struct device *dev, struct device_attribute *attr, char *buf)
  2373. {
  2374. struct ctc_priv *priv = dev->driver_data;
  2375. if (!priv)
  2376. return -ENODEV;
  2377. ctc_print_statistics(priv);
  2378. return sprintf(buf, "0\n");
  2379. }
  2380. static ssize_t
  2381. stats_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  2382. {
  2383. struct ctc_priv *priv = dev->driver_data;
  2384. if (!priv)
  2385. return -ENODEV;
  2386. /* Reset statistics */
  2387. memset(&priv->channel[WRITE]->prof, 0,
  2388. sizeof(priv->channel[WRITE]->prof));
  2389. return count;
  2390. }
  2391. static void
  2392. ctc_netdev_unregister(struct net_device * dev)
  2393. {
  2394. struct ctc_priv *privptr;
  2395. if (!dev)
  2396. return;
  2397. privptr = (struct ctc_priv *) dev->priv;
  2398. if (privptr->protocol != CTC_PROTO_LINUX_TTY)
  2399. unregister_netdev(dev);
  2400. else
  2401. ctc_tty_unregister_netdev(dev);
  2402. }
  2403. static int
  2404. ctc_netdev_register(struct net_device * dev)
  2405. {
  2406. struct ctc_priv *privptr = (struct ctc_priv *) dev->priv;
  2407. if (privptr->protocol != CTC_PROTO_LINUX_TTY)
  2408. return register_netdev(dev);
  2409. else
  2410. return ctc_tty_register_netdev(dev);
  2411. }
  2412. static void
  2413. ctc_free_netdevice(struct net_device * dev, int free_dev)
  2414. {
  2415. struct ctc_priv *privptr;
  2416. if (!dev)
  2417. return;
  2418. privptr = dev->priv;
  2419. if (privptr) {
  2420. if (privptr->fsm)
  2421. kfree_fsm(privptr->fsm);
  2422. kfree(privptr);
  2423. }
  2424. #ifdef MODULE
  2425. if (free_dev)
  2426. free_netdev(dev);
  2427. #endif
  2428. }
  2429. static ssize_t
  2430. ctc_proto_show(struct device *dev, struct device_attribute *attr, char *buf)
  2431. {
  2432. struct ctc_priv *priv;
  2433. priv = dev->driver_data;
  2434. if (!priv)
  2435. return -ENODEV;
  2436. return sprintf(buf, "%d\n", priv->protocol);
  2437. }
  2438. static ssize_t
  2439. ctc_proto_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  2440. {
  2441. struct ctc_priv *priv;
  2442. int value;
  2443. DBF_TEXT(trace, 3, __FUNCTION__);
  2444. pr_debug("%s() called\n", __FUNCTION__);
  2445. priv = dev->driver_data;
  2446. if (!priv)
  2447. return -ENODEV;
  2448. sscanf(buf, "%u", &value);
  2449. if ((value < 0) || (value > CTC_PROTO_MAX))
  2450. return -EINVAL;
  2451. priv->protocol = value;
  2452. return count;
  2453. }
  2454. static ssize_t
  2455. ctc_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  2456. {
  2457. struct ccwgroup_device *cgdev;
  2458. cgdev = to_ccwgroupdev(dev);
  2459. if (!cgdev)
  2460. return -ENODEV;
  2461. return sprintf(buf, "%s\n", cu3088_type[cgdev->cdev[0]->id.driver_info]);
  2462. }
  2463. static DEVICE_ATTR(buffer, 0644, buffer_show, buffer_write);
  2464. static DEVICE_ATTR(protocol, 0644, ctc_proto_show, ctc_proto_store);
  2465. static DEVICE_ATTR(type, 0444, ctc_type_show, NULL);
  2466. static DEVICE_ATTR(loglevel, 0644, loglevel_show, loglevel_write);
  2467. static DEVICE_ATTR(stats, 0644, stats_show, stats_write);
  2468. static struct attribute *ctc_attr[] = {
  2469. &dev_attr_protocol.attr,
  2470. &dev_attr_type.attr,
  2471. &dev_attr_buffer.attr,
  2472. NULL,
  2473. };
  2474. static struct attribute_group ctc_attr_group = {
  2475. .attrs = ctc_attr,
  2476. };
  2477. static int
  2478. ctc_add_attributes(struct device *dev)
  2479. {
  2480. device_create_file(dev, &dev_attr_loglevel);
  2481. device_create_file(dev, &dev_attr_stats);
  2482. return 0;
  2483. }
  2484. static void
  2485. ctc_remove_attributes(struct device *dev)
  2486. {
  2487. device_remove_file(dev, &dev_attr_stats);
  2488. device_remove_file(dev, &dev_attr_loglevel);
  2489. }
  2490. static int
  2491. ctc_add_files(struct device *dev)
  2492. {
  2493. pr_debug("%s() called\n", __FUNCTION__);
  2494. return sysfs_create_group(&dev->kobj, &ctc_attr_group);
  2495. }
  2496. static void
  2497. ctc_remove_files(struct device *dev)
  2498. {
  2499. pr_debug("%s() called\n", __FUNCTION__);
  2500. sysfs_remove_group(&dev->kobj, &ctc_attr_group);
  2501. }
  2502. /**
  2503. * Add ctc specific attributes.
  2504. * Add ctc private data.
  2505. *
  2506. * @param cgdev pointer to ccwgroup_device just added
  2507. *
  2508. * @returns 0 on success, !0 on failure.
  2509. */
  2510. static int
  2511. ctc_probe_device(struct ccwgroup_device *cgdev)
  2512. {
  2513. struct ctc_priv *priv;
  2514. int rc;
  2515. char buffer[16];
  2516. pr_debug("%s() called\n", __FUNCTION__);
  2517. DBF_TEXT(setup, 3, __FUNCTION__);
  2518. if (!get_device(&cgdev->dev))
  2519. return -ENODEV;
  2520. priv = kmalloc(sizeof (struct ctc_priv), GFP_KERNEL);
  2521. if (!priv) {
  2522. ctc_pr_err("%s: Out of memory\n", __func__);
  2523. put_device(&cgdev->dev);
  2524. return -ENOMEM;
  2525. }
  2526. memset(priv, 0, sizeof (struct ctc_priv));
  2527. rc = ctc_add_files(&cgdev->dev);
  2528. if (rc) {
  2529. kfree(priv);
  2530. put_device(&cgdev->dev);
  2531. return rc;
  2532. }
  2533. priv->buffer_size = CTC_BUFSIZE_DEFAULT;
  2534. cgdev->cdev[0]->handler = ctc_irq_handler;
  2535. cgdev->cdev[1]->handler = ctc_irq_handler;
  2536. cgdev->dev.driver_data = priv;
  2537. sprintf(buffer, "%p", priv);
  2538. DBF_TEXT(data, 3, buffer);
  2539. sprintf(buffer, "%u", (unsigned int)sizeof(struct ctc_priv));
  2540. DBF_TEXT(data, 3, buffer);
  2541. sprintf(buffer, "%p", &channels);
  2542. DBF_TEXT(data, 3, buffer);
  2543. sprintf(buffer, "%u", (unsigned int)sizeof(struct channel));
  2544. DBF_TEXT(data, 3, buffer);
  2545. return 0;
  2546. }
  2547. /**
  2548. * Initialize everything of the net device except the name and the
  2549. * channel structs.
  2550. */
  2551. static struct net_device *
  2552. ctc_init_netdevice(struct net_device * dev, int alloc_device,
  2553. struct ctc_priv *privptr)
  2554. {
  2555. if (!privptr)
  2556. return NULL;
  2557. DBF_TEXT(setup, 3, __FUNCTION__);
  2558. if (alloc_device) {
  2559. dev = kmalloc(sizeof (struct net_device), GFP_KERNEL);
  2560. if (!dev)
  2561. return NULL;
  2562. memset(dev, 0, sizeof (struct net_device));
  2563. }
  2564. dev->priv = privptr;
  2565. privptr->fsm = init_fsm("ctcdev", dev_state_names,
  2566. dev_event_names, CTC_NR_DEV_STATES, CTC_NR_DEV_EVENTS,
  2567. dev_fsm, DEV_FSM_LEN, GFP_KERNEL);
  2568. if (privptr->fsm == NULL) {
  2569. if (alloc_device)
  2570. kfree(dev);
  2571. return NULL;
  2572. }
  2573. fsm_newstate(privptr->fsm, DEV_STATE_STOPPED);
  2574. fsm_settimer(privptr->fsm, &privptr->restart_timer);
  2575. if (dev->mtu == 0)
  2576. dev->mtu = CTC_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
  2577. dev->hard_start_xmit = ctc_tx;
  2578. dev->open = ctc_open;
  2579. dev->stop = ctc_close;
  2580. dev->get_stats = ctc_stats;
  2581. dev->change_mtu = ctc_change_mtu;
  2582. dev->hard_header_len = LL_HEADER_LENGTH + 2;
  2583. dev->addr_len = 0;
  2584. dev->type = ARPHRD_SLIP;
  2585. dev->tx_queue_len = 100;
  2586. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  2587. SET_MODULE_OWNER(dev);
  2588. return dev;
  2589. }
  2590. /**
  2591. *
  2592. * Setup an interface.
  2593. *
  2594. * @param cgdev Device to be setup.
  2595. *
  2596. * @returns 0 on success, !0 on failure.
  2597. */
  2598. static int
  2599. ctc_new_device(struct ccwgroup_device *cgdev)
  2600. {
  2601. char read_id[CTC_ID_SIZE];
  2602. char write_id[CTC_ID_SIZE];
  2603. int direction;
  2604. enum channel_types type;
  2605. struct ctc_priv *privptr;
  2606. struct net_device *dev;
  2607. int ret;
  2608. char buffer[16];
  2609. pr_debug("%s() called\n", __FUNCTION__);
  2610. DBF_TEXT(setup, 3, __FUNCTION__);
  2611. privptr = cgdev->dev.driver_data;
  2612. if (!privptr)
  2613. return -ENODEV;
  2614. sprintf(buffer, "%d", privptr->buffer_size);
  2615. DBF_TEXT(setup, 3, buffer);
  2616. type = get_channel_type(&cgdev->cdev[0]->id);
  2617. snprintf(read_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[0]->dev.bus_id);
  2618. snprintf(write_id, CTC_ID_SIZE, "ch-%s", cgdev->cdev[1]->dev.bus_id);
  2619. if (add_channel(cgdev->cdev[0], type))
  2620. return -ENOMEM;
  2621. if (add_channel(cgdev->cdev[1], type))
  2622. return -ENOMEM;
  2623. ret = ccw_device_set_online(cgdev->cdev[0]);
  2624. if (ret != 0) {
  2625. printk(KERN_WARNING
  2626. "ccw_device_set_online (cdev[0]) failed with ret = %d\n", ret);
  2627. }
  2628. ret = ccw_device_set_online(cgdev->cdev[1]);
  2629. if (ret != 0) {
  2630. printk(KERN_WARNING
  2631. "ccw_device_set_online (cdev[1]) failed with ret = %d\n", ret);
  2632. }
  2633. dev = ctc_init_netdevice(NULL, 1, privptr);
  2634. if (!dev) {
  2635. ctc_pr_warn("ctc_init_netdevice failed\n");
  2636. goto out;
  2637. }
  2638. if (privptr->protocol == CTC_PROTO_LINUX_TTY)
  2639. strlcpy(dev->name, "ctctty%d", IFNAMSIZ);
  2640. else
  2641. strlcpy(dev->name, "ctc%d", IFNAMSIZ);
  2642. for (direction = READ; direction <= WRITE; direction++) {
  2643. privptr->channel[direction] =
  2644. channel_get(type, direction == READ ? read_id : write_id,
  2645. direction);
  2646. if (privptr->channel[direction] == NULL) {
  2647. if (direction == WRITE)
  2648. channel_free(privptr->channel[READ]);
  2649. ctc_free_netdevice(dev, 1);
  2650. goto out;
  2651. }
  2652. privptr->channel[direction]->netdev = dev;
  2653. privptr->channel[direction]->protocol = privptr->protocol;
  2654. privptr->channel[direction]->max_bufsize = privptr->buffer_size;
  2655. }
  2656. /* sysfs magic */
  2657. SET_NETDEV_DEV(dev, &cgdev->dev);
  2658. if (ctc_netdev_register(dev) != 0) {
  2659. ctc_free_netdevice(dev, 1);
  2660. goto out;
  2661. }
  2662. ctc_add_attributes(&cgdev->dev);
  2663. strlcpy(privptr->fsm->name, dev->name, sizeof (privptr->fsm->name));
  2664. print_banner();
  2665. ctc_pr_info("%s: read: %s, write: %s, proto: %d\n",
  2666. dev->name, privptr->channel[READ]->id,
  2667. privptr->channel[WRITE]->id, privptr->protocol);
  2668. return 0;
  2669. out:
  2670. ccw_device_set_offline(cgdev->cdev[1]);
  2671. ccw_device_set_offline(cgdev->cdev[0]);
  2672. return -ENODEV;
  2673. }
  2674. /**
  2675. * Shutdown an interface.
  2676. *
  2677. * @param cgdev Device to be shut down.
  2678. *
  2679. * @returns 0 on success, !0 on failure.
  2680. */
  2681. static int
  2682. ctc_shutdown_device(struct ccwgroup_device *cgdev)
  2683. {
  2684. struct ctc_priv *priv;
  2685. struct net_device *ndev;
  2686. DBF_TEXT(setup, 3, __FUNCTION__);
  2687. pr_debug("%s() called\n", __FUNCTION__);
  2688. priv = cgdev->dev.driver_data;
  2689. ndev = NULL;
  2690. if (!priv)
  2691. return -ENODEV;
  2692. if (priv->channel[READ]) {
  2693. ndev = priv->channel[READ]->netdev;
  2694. /* Close the device */
  2695. ctc_close(ndev);
  2696. ndev->flags &=~IFF_RUNNING;
  2697. ctc_remove_attributes(&cgdev->dev);
  2698. channel_free(priv->channel[READ]);
  2699. }
  2700. if (priv->channel[WRITE])
  2701. channel_free(priv->channel[WRITE]);
  2702. if (ndev) {
  2703. ctc_netdev_unregister(ndev);
  2704. ndev->priv = NULL;
  2705. ctc_free_netdevice(ndev, 1);
  2706. }
  2707. if (priv->fsm)
  2708. kfree_fsm(priv->fsm);
  2709. ccw_device_set_offline(cgdev->cdev[1]);
  2710. ccw_device_set_offline(cgdev->cdev[0]);
  2711. if (priv->channel[READ])
  2712. channel_remove(priv->channel[READ]);
  2713. if (priv->channel[WRITE])
  2714. channel_remove(priv->channel[WRITE]);
  2715. priv->channel[READ] = priv->channel[WRITE] = NULL;
  2716. return 0;
  2717. }
  2718. static void
  2719. ctc_remove_device(struct ccwgroup_device *cgdev)
  2720. {
  2721. struct ctc_priv *priv;
  2722. pr_debug("%s() called\n", __FUNCTION__);
  2723. DBF_TEXT(setup, 3, __FUNCTION__);
  2724. priv = cgdev->dev.driver_data;
  2725. if (!priv)
  2726. return;
  2727. if (cgdev->state == CCWGROUP_ONLINE)
  2728. ctc_shutdown_device(cgdev);
  2729. ctc_remove_files(&cgdev->dev);
  2730. cgdev->dev.driver_data = NULL;
  2731. kfree(priv);
  2732. put_device(&cgdev->dev);
  2733. }
  2734. static struct ccwgroup_driver ctc_group_driver = {
  2735. .owner = THIS_MODULE,
  2736. .name = "ctc",
  2737. .max_slaves = 2,
  2738. .driver_id = 0xC3E3C3,
  2739. .probe = ctc_probe_device,
  2740. .remove = ctc_remove_device,
  2741. .set_online = ctc_new_device,
  2742. .set_offline = ctc_shutdown_device,
  2743. };
  2744. /**
  2745. * Module related routines
  2746. *****************************************************************************/
  2747. /**
  2748. * Prepare to be unloaded. Free IRQ's and release all resources.
  2749. * This is called just before this module is unloaded. It is
  2750. * <em>not</em> called, if the usage count is !0, so we don't need to check
  2751. * for that.
  2752. */
  2753. static void __exit
  2754. ctc_exit(void)
  2755. {
  2756. DBF_TEXT(setup, 3, __FUNCTION__);
  2757. unregister_cu3088_discipline(&ctc_group_driver);
  2758. ctc_tty_cleanup();
  2759. ctc_unregister_dbf_views();
  2760. ctc_pr_info("CTC driver unloaded\n");
  2761. }
  2762. /**
  2763. * Initialize module.
  2764. * This is called just after the module is loaded.
  2765. *
  2766. * @return 0 on success, !0 on error.
  2767. */
  2768. static int __init
  2769. ctc_init(void)
  2770. {
  2771. int ret = 0;
  2772. loglevel = CTC_LOGLEVEL_DEFAULT;
  2773. DBF_TEXT(setup, 3, __FUNCTION__);
  2774. print_banner();
  2775. ret = ctc_register_dbf_views();
  2776. if (ret){
  2777. ctc_pr_crit("ctc_init failed with ctc_register_dbf_views rc = %d\n", ret);
  2778. return ret;
  2779. }
  2780. ctc_tty_init();
  2781. ret = register_cu3088_discipline(&ctc_group_driver);
  2782. if (ret) {
  2783. ctc_tty_cleanup();
  2784. ctc_unregister_dbf_views();
  2785. }
  2786. return ret;
  2787. }
  2788. module_init(ctc_init);
  2789. module_exit(ctc_exit);
  2790. /* --- This is the END my friend --- */