ctcmain.c 87 KB

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