ctcmain.c 83 KB

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