lcs.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511
  1. /*
  2. * Linux for S/390 Lan Channel Station Network Driver
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. * Author(s): Original Code written by
  6. * DJ Barrow <djbarrow@de.ibm.com,barrow_dj@yahoo.com>
  7. * Rewritten by
  8. * Frank Pavlic <fpavlic@de.ibm.com> and
  9. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #define KMSG_COMPONENT "lcs"
  26. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  27. #include <linux/kernel_stat.h>
  28. #include <linux/module.h>
  29. #include <linux/if.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/trdevice.h>
  33. #include <linux/fddidevice.h>
  34. #include <linux/inetdevice.h>
  35. #include <linux/in.h>
  36. #include <linux/igmp.h>
  37. #include <linux/delay.h>
  38. #include <linux/kthread.h>
  39. #include <linux/slab.h>
  40. #include <net/arp.h>
  41. #include <net/ip.h>
  42. #include <asm/debug.h>
  43. #include <asm/idals.h>
  44. #include <asm/timex.h>
  45. #include <linux/device.h>
  46. #include <asm/ccwgroup.h>
  47. #include "lcs.h"
  48. #if !defined(CONFIG_NET_ETHERNET) && \
  49. !defined(CONFIG_TR) && !defined(CONFIG_FDDI)
  50. #error Cannot compile lcs.c without some net devices switched on.
  51. #endif
  52. /**
  53. * initialization string for output
  54. */
  55. static char version[] __initdata = "LCS driver";
  56. /**
  57. * the root device for lcs group devices
  58. */
  59. static struct device *lcs_root_dev;
  60. /**
  61. * Some prototypes.
  62. */
  63. static void lcs_tasklet(unsigned long);
  64. static void lcs_start_kernel_thread(struct work_struct *);
  65. static void lcs_get_frames_cb(struct lcs_channel *, struct lcs_buffer *);
  66. #ifdef CONFIG_IP_MULTICAST
  67. static int lcs_send_delipm(struct lcs_card *, struct lcs_ipm_list *);
  68. #endif /* CONFIG_IP_MULTICAST */
  69. static int lcs_recovery(void *ptr);
  70. /**
  71. * Debug Facility Stuff
  72. */
  73. static char debug_buffer[255];
  74. static debug_info_t *lcs_dbf_setup;
  75. static debug_info_t *lcs_dbf_trace;
  76. /**
  77. * LCS Debug Facility functions
  78. */
  79. static void
  80. lcs_unregister_debug_facility(void)
  81. {
  82. if (lcs_dbf_setup)
  83. debug_unregister(lcs_dbf_setup);
  84. if (lcs_dbf_trace)
  85. debug_unregister(lcs_dbf_trace);
  86. }
  87. static int
  88. lcs_register_debug_facility(void)
  89. {
  90. lcs_dbf_setup = debug_register("lcs_setup", 2, 1, 8);
  91. lcs_dbf_trace = debug_register("lcs_trace", 4, 1, 8);
  92. if (lcs_dbf_setup == NULL || lcs_dbf_trace == NULL) {
  93. pr_err("Not enough memory for debug facility.\n");
  94. lcs_unregister_debug_facility();
  95. return -ENOMEM;
  96. }
  97. debug_register_view(lcs_dbf_setup, &debug_hex_ascii_view);
  98. debug_set_level(lcs_dbf_setup, 2);
  99. debug_register_view(lcs_dbf_trace, &debug_hex_ascii_view);
  100. debug_set_level(lcs_dbf_trace, 2);
  101. return 0;
  102. }
  103. /**
  104. * Allocate io buffers.
  105. */
  106. static int
  107. lcs_alloc_channel(struct lcs_channel *channel)
  108. {
  109. int cnt;
  110. LCS_DBF_TEXT(2, setup, "ichalloc");
  111. for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
  112. /* alloc memory fo iobuffer */
  113. channel->iob[cnt].data =
  114. kzalloc(LCS_IOBUFFERSIZE, GFP_DMA | GFP_KERNEL);
  115. if (channel->iob[cnt].data == NULL)
  116. break;
  117. channel->iob[cnt].state = LCS_BUF_STATE_EMPTY;
  118. }
  119. if (cnt < LCS_NUM_BUFFS) {
  120. /* Not all io buffers could be allocated. */
  121. LCS_DBF_TEXT(2, setup, "echalloc");
  122. while (cnt-- > 0)
  123. kfree(channel->iob[cnt].data);
  124. return -ENOMEM;
  125. }
  126. return 0;
  127. }
  128. /**
  129. * Free io buffers.
  130. */
  131. static void
  132. lcs_free_channel(struct lcs_channel *channel)
  133. {
  134. int cnt;
  135. LCS_DBF_TEXT(2, setup, "ichfree");
  136. for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
  137. kfree(channel->iob[cnt].data);
  138. channel->iob[cnt].data = NULL;
  139. }
  140. }
  141. /*
  142. * Cleanup channel.
  143. */
  144. static void
  145. lcs_cleanup_channel(struct lcs_channel *channel)
  146. {
  147. LCS_DBF_TEXT(3, setup, "cleanch");
  148. /* Kill write channel tasklets. */
  149. tasklet_kill(&channel->irq_tasklet);
  150. /* Free channel buffers. */
  151. lcs_free_channel(channel);
  152. }
  153. /**
  154. * LCS free memory for card and channels.
  155. */
  156. static void
  157. lcs_free_card(struct lcs_card *card)
  158. {
  159. LCS_DBF_TEXT(2, setup, "remcard");
  160. LCS_DBF_HEX(2, setup, &card, sizeof(void*));
  161. kfree(card);
  162. }
  163. /**
  164. * LCS alloc memory for card and channels
  165. */
  166. static struct lcs_card *
  167. lcs_alloc_card(void)
  168. {
  169. struct lcs_card *card;
  170. int rc;
  171. LCS_DBF_TEXT(2, setup, "alloclcs");
  172. card = kzalloc(sizeof(struct lcs_card), GFP_KERNEL | GFP_DMA);
  173. if (card == NULL)
  174. return NULL;
  175. card->lan_type = LCS_FRAME_TYPE_AUTO;
  176. card->pkt_seq = 0;
  177. card->lancmd_timeout = LCS_LANCMD_TIMEOUT_DEFAULT;
  178. /* Allocate io buffers for the read channel. */
  179. rc = lcs_alloc_channel(&card->read);
  180. if (rc){
  181. LCS_DBF_TEXT(2, setup, "iccwerr");
  182. lcs_free_card(card);
  183. return NULL;
  184. }
  185. /* Allocate io buffers for the write channel. */
  186. rc = lcs_alloc_channel(&card->write);
  187. if (rc) {
  188. LCS_DBF_TEXT(2, setup, "iccwerr");
  189. lcs_cleanup_channel(&card->read);
  190. lcs_free_card(card);
  191. return NULL;
  192. }
  193. #ifdef CONFIG_IP_MULTICAST
  194. INIT_LIST_HEAD(&card->ipm_list);
  195. #endif
  196. LCS_DBF_HEX(2, setup, &card, sizeof(void*));
  197. return card;
  198. }
  199. /*
  200. * Setup read channel.
  201. */
  202. static void
  203. lcs_setup_read_ccws(struct lcs_card *card)
  204. {
  205. int cnt;
  206. LCS_DBF_TEXT(2, setup, "ireadccw");
  207. /* Setup read ccws. */
  208. memset(card->read.ccws, 0, sizeof (struct ccw1) * (LCS_NUM_BUFFS + 1));
  209. for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
  210. card->read.ccws[cnt].cmd_code = LCS_CCW_READ;
  211. card->read.ccws[cnt].count = LCS_IOBUFFERSIZE;
  212. card->read.ccws[cnt].flags =
  213. CCW_FLAG_CC | CCW_FLAG_SLI | CCW_FLAG_PCI;
  214. /*
  215. * Note: we have allocated the buffer with GFP_DMA, so
  216. * we do not need to do set_normalized_cda.
  217. */
  218. card->read.ccws[cnt].cda =
  219. (__u32) __pa(card->read.iob[cnt].data);
  220. ((struct lcs_header *)
  221. card->read.iob[cnt].data)->offset = LCS_ILLEGAL_OFFSET;
  222. card->read.iob[cnt].callback = lcs_get_frames_cb;
  223. card->read.iob[cnt].state = LCS_BUF_STATE_READY;
  224. card->read.iob[cnt].count = LCS_IOBUFFERSIZE;
  225. }
  226. card->read.ccws[0].flags &= ~CCW_FLAG_PCI;
  227. card->read.ccws[LCS_NUM_BUFFS - 1].flags &= ~CCW_FLAG_PCI;
  228. card->read.ccws[LCS_NUM_BUFFS - 1].flags |= CCW_FLAG_SUSPEND;
  229. /* Last ccw is a tic (transfer in channel). */
  230. card->read.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
  231. card->read.ccws[LCS_NUM_BUFFS].cda =
  232. (__u32) __pa(card->read.ccws);
  233. /* Setg initial state of the read channel. */
  234. card->read.state = LCS_CH_STATE_INIT;
  235. card->read.io_idx = 0;
  236. card->read.buf_idx = 0;
  237. }
  238. static void
  239. lcs_setup_read(struct lcs_card *card)
  240. {
  241. LCS_DBF_TEXT(3, setup, "initread");
  242. lcs_setup_read_ccws(card);
  243. /* Initialize read channel tasklet. */
  244. card->read.irq_tasklet.data = (unsigned long) &card->read;
  245. card->read.irq_tasklet.func = lcs_tasklet;
  246. /* Initialize waitqueue. */
  247. init_waitqueue_head(&card->read.wait_q);
  248. }
  249. /*
  250. * Setup write channel.
  251. */
  252. static void
  253. lcs_setup_write_ccws(struct lcs_card *card)
  254. {
  255. int cnt;
  256. LCS_DBF_TEXT(3, setup, "iwritccw");
  257. /* Setup write ccws. */
  258. memset(card->write.ccws, 0, sizeof(struct ccw1) * LCS_NUM_BUFFS + 1);
  259. for (cnt = 0; cnt < LCS_NUM_BUFFS; cnt++) {
  260. card->write.ccws[cnt].cmd_code = LCS_CCW_WRITE;
  261. card->write.ccws[cnt].count = 0;
  262. card->write.ccws[cnt].flags =
  263. CCW_FLAG_SUSPEND | CCW_FLAG_CC | CCW_FLAG_SLI;
  264. /*
  265. * Note: we have allocated the buffer with GFP_DMA, so
  266. * we do not need to do set_normalized_cda.
  267. */
  268. card->write.ccws[cnt].cda =
  269. (__u32) __pa(card->write.iob[cnt].data);
  270. }
  271. /* Last ccw is a tic (transfer in channel). */
  272. card->write.ccws[LCS_NUM_BUFFS].cmd_code = LCS_CCW_TRANSFER;
  273. card->write.ccws[LCS_NUM_BUFFS].cda =
  274. (__u32) __pa(card->write.ccws);
  275. /* Set initial state of the write channel. */
  276. card->read.state = LCS_CH_STATE_INIT;
  277. card->write.io_idx = 0;
  278. card->write.buf_idx = 0;
  279. }
  280. static void
  281. lcs_setup_write(struct lcs_card *card)
  282. {
  283. LCS_DBF_TEXT(3, setup, "initwrit");
  284. lcs_setup_write_ccws(card);
  285. /* Initialize write channel tasklet. */
  286. card->write.irq_tasklet.data = (unsigned long) &card->write;
  287. card->write.irq_tasklet.func = lcs_tasklet;
  288. /* Initialize waitqueue. */
  289. init_waitqueue_head(&card->write.wait_q);
  290. }
  291. static void
  292. lcs_set_allowed_threads(struct lcs_card *card, unsigned long threads)
  293. {
  294. unsigned long flags;
  295. spin_lock_irqsave(&card->mask_lock, flags);
  296. card->thread_allowed_mask = threads;
  297. spin_unlock_irqrestore(&card->mask_lock, flags);
  298. wake_up(&card->wait_q);
  299. }
  300. static inline int
  301. lcs_threads_running(struct lcs_card *card, unsigned long threads)
  302. {
  303. unsigned long flags;
  304. int rc = 0;
  305. spin_lock_irqsave(&card->mask_lock, flags);
  306. rc = (card->thread_running_mask & threads);
  307. spin_unlock_irqrestore(&card->mask_lock, flags);
  308. return rc;
  309. }
  310. static int
  311. lcs_wait_for_threads(struct lcs_card *card, unsigned long threads)
  312. {
  313. return wait_event_interruptible(card->wait_q,
  314. lcs_threads_running(card, threads) == 0);
  315. }
  316. static inline int
  317. lcs_set_thread_start_bit(struct lcs_card *card, unsigned long thread)
  318. {
  319. unsigned long flags;
  320. spin_lock_irqsave(&card->mask_lock, flags);
  321. if ( !(card->thread_allowed_mask & thread) ||
  322. (card->thread_start_mask & thread) ) {
  323. spin_unlock_irqrestore(&card->mask_lock, flags);
  324. return -EPERM;
  325. }
  326. card->thread_start_mask |= thread;
  327. spin_unlock_irqrestore(&card->mask_lock, flags);
  328. return 0;
  329. }
  330. static void
  331. lcs_clear_thread_running_bit(struct lcs_card *card, unsigned long thread)
  332. {
  333. unsigned long flags;
  334. spin_lock_irqsave(&card->mask_lock, flags);
  335. card->thread_running_mask &= ~thread;
  336. spin_unlock_irqrestore(&card->mask_lock, flags);
  337. wake_up(&card->wait_q);
  338. }
  339. static inline int
  340. __lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
  341. {
  342. unsigned long flags;
  343. int rc = 0;
  344. spin_lock_irqsave(&card->mask_lock, flags);
  345. if (card->thread_start_mask & thread){
  346. if ((card->thread_allowed_mask & thread) &&
  347. !(card->thread_running_mask & thread)){
  348. rc = 1;
  349. card->thread_start_mask &= ~thread;
  350. card->thread_running_mask |= thread;
  351. } else
  352. rc = -EPERM;
  353. }
  354. spin_unlock_irqrestore(&card->mask_lock, flags);
  355. return rc;
  356. }
  357. static int
  358. lcs_do_run_thread(struct lcs_card *card, unsigned long thread)
  359. {
  360. int rc = 0;
  361. wait_event(card->wait_q,
  362. (rc = __lcs_do_run_thread(card, thread)) >= 0);
  363. return rc;
  364. }
  365. static int
  366. lcs_do_start_thread(struct lcs_card *card, unsigned long thread)
  367. {
  368. unsigned long flags;
  369. int rc = 0;
  370. spin_lock_irqsave(&card->mask_lock, flags);
  371. LCS_DBF_TEXT_(4, trace, " %02x%02x%02x",
  372. (u8) card->thread_start_mask,
  373. (u8) card->thread_allowed_mask,
  374. (u8) card->thread_running_mask);
  375. rc = (card->thread_start_mask & thread);
  376. spin_unlock_irqrestore(&card->mask_lock, flags);
  377. return rc;
  378. }
  379. /**
  380. * Initialize channels,card and state machines.
  381. */
  382. static void
  383. lcs_setup_card(struct lcs_card *card)
  384. {
  385. LCS_DBF_TEXT(2, setup, "initcard");
  386. LCS_DBF_HEX(2, setup, &card, sizeof(void*));
  387. lcs_setup_read(card);
  388. lcs_setup_write(card);
  389. /* Set cards initial state. */
  390. card->state = DEV_STATE_DOWN;
  391. card->tx_buffer = NULL;
  392. card->tx_emitted = 0;
  393. init_waitqueue_head(&card->wait_q);
  394. spin_lock_init(&card->lock);
  395. spin_lock_init(&card->ipm_lock);
  396. spin_lock_init(&card->mask_lock);
  397. #ifdef CONFIG_IP_MULTICAST
  398. INIT_LIST_HEAD(&card->ipm_list);
  399. #endif
  400. INIT_LIST_HEAD(&card->lancmd_waiters);
  401. }
  402. static inline void
  403. lcs_clear_multicast_list(struct lcs_card *card)
  404. {
  405. #ifdef CONFIG_IP_MULTICAST
  406. struct lcs_ipm_list *ipm;
  407. unsigned long flags;
  408. /* Free multicast list. */
  409. LCS_DBF_TEXT(3, setup, "clmclist");
  410. spin_lock_irqsave(&card->ipm_lock, flags);
  411. while (!list_empty(&card->ipm_list)){
  412. ipm = list_entry(card->ipm_list.next,
  413. struct lcs_ipm_list, list);
  414. list_del(&ipm->list);
  415. if (ipm->ipm_state != LCS_IPM_STATE_SET_REQUIRED){
  416. spin_unlock_irqrestore(&card->ipm_lock, flags);
  417. lcs_send_delipm(card, ipm);
  418. spin_lock_irqsave(&card->ipm_lock, flags);
  419. }
  420. kfree(ipm);
  421. }
  422. spin_unlock_irqrestore(&card->ipm_lock, flags);
  423. #endif
  424. }
  425. /**
  426. * Cleanup channels,card and state machines.
  427. */
  428. static void
  429. lcs_cleanup_card(struct lcs_card *card)
  430. {
  431. LCS_DBF_TEXT(3, setup, "cleancrd");
  432. LCS_DBF_HEX(2,setup,&card,sizeof(void*));
  433. if (card->dev != NULL)
  434. free_netdev(card->dev);
  435. /* Cleanup channels. */
  436. lcs_cleanup_channel(&card->write);
  437. lcs_cleanup_channel(&card->read);
  438. }
  439. /**
  440. * Start channel.
  441. */
  442. static int
  443. lcs_start_channel(struct lcs_channel *channel)
  444. {
  445. unsigned long flags;
  446. int rc;
  447. LCS_DBF_TEXT_(4, trace,"ssch%s", dev_name(&channel->ccwdev->dev));
  448. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  449. rc = ccw_device_start(channel->ccwdev,
  450. channel->ccws + channel->io_idx, 0, 0,
  451. DOIO_DENY_PREFETCH | DOIO_ALLOW_SUSPEND);
  452. if (rc == 0)
  453. channel->state = LCS_CH_STATE_RUNNING;
  454. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  455. if (rc) {
  456. LCS_DBF_TEXT_(4,trace,"essh%s",
  457. dev_name(&channel->ccwdev->dev));
  458. dev_err(&channel->ccwdev->dev,
  459. "Starting an LCS device resulted in an error,"
  460. " rc=%d!\n", rc);
  461. }
  462. return rc;
  463. }
  464. static int
  465. lcs_clear_channel(struct lcs_channel *channel)
  466. {
  467. unsigned long flags;
  468. int rc;
  469. LCS_DBF_TEXT(4,trace,"clearch");
  470. LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
  471. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  472. rc = ccw_device_clear(channel->ccwdev, (addr_t) channel);
  473. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  474. if (rc) {
  475. LCS_DBF_TEXT_(4, trace, "ecsc%s",
  476. dev_name(&channel->ccwdev->dev));
  477. return rc;
  478. }
  479. wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_CLEARED));
  480. channel->state = LCS_CH_STATE_STOPPED;
  481. return rc;
  482. }
  483. /**
  484. * Stop channel.
  485. */
  486. static int
  487. lcs_stop_channel(struct lcs_channel *channel)
  488. {
  489. unsigned long flags;
  490. int rc;
  491. if (channel->state == LCS_CH_STATE_STOPPED)
  492. return 0;
  493. LCS_DBF_TEXT(4,trace,"haltsch");
  494. LCS_DBF_TEXT_(4, trace, "%s", dev_name(&channel->ccwdev->dev));
  495. channel->state = LCS_CH_STATE_INIT;
  496. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  497. rc = ccw_device_halt(channel->ccwdev, (addr_t) channel);
  498. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  499. if (rc) {
  500. LCS_DBF_TEXT_(4, trace, "ehsc%s",
  501. dev_name(&channel->ccwdev->dev));
  502. return rc;
  503. }
  504. /* Asynchronous halt initialted. Wait for its completion. */
  505. wait_event(channel->wait_q, (channel->state == LCS_CH_STATE_HALTED));
  506. lcs_clear_channel(channel);
  507. return 0;
  508. }
  509. /**
  510. * start read and write channel
  511. */
  512. static int
  513. lcs_start_channels(struct lcs_card *card)
  514. {
  515. int rc;
  516. LCS_DBF_TEXT(2, trace, "chstart");
  517. /* start read channel */
  518. rc = lcs_start_channel(&card->read);
  519. if (rc)
  520. return rc;
  521. /* start write channel */
  522. rc = lcs_start_channel(&card->write);
  523. if (rc)
  524. lcs_stop_channel(&card->read);
  525. return rc;
  526. }
  527. /**
  528. * stop read and write channel
  529. */
  530. static int
  531. lcs_stop_channels(struct lcs_card *card)
  532. {
  533. LCS_DBF_TEXT(2, trace, "chhalt");
  534. lcs_stop_channel(&card->read);
  535. lcs_stop_channel(&card->write);
  536. return 0;
  537. }
  538. /**
  539. * Get empty buffer.
  540. */
  541. static struct lcs_buffer *
  542. __lcs_get_buffer(struct lcs_channel *channel)
  543. {
  544. int index;
  545. LCS_DBF_TEXT(5, trace, "_getbuff");
  546. index = channel->io_idx;
  547. do {
  548. if (channel->iob[index].state == LCS_BUF_STATE_EMPTY) {
  549. channel->iob[index].state = LCS_BUF_STATE_LOCKED;
  550. return channel->iob + index;
  551. }
  552. index = (index + 1) & (LCS_NUM_BUFFS - 1);
  553. } while (index != channel->io_idx);
  554. return NULL;
  555. }
  556. static struct lcs_buffer *
  557. lcs_get_buffer(struct lcs_channel *channel)
  558. {
  559. struct lcs_buffer *buffer;
  560. unsigned long flags;
  561. LCS_DBF_TEXT(5, trace, "getbuff");
  562. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  563. buffer = __lcs_get_buffer(channel);
  564. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  565. return buffer;
  566. }
  567. /**
  568. * Resume channel program if the channel is suspended.
  569. */
  570. static int
  571. __lcs_resume_channel(struct lcs_channel *channel)
  572. {
  573. int rc;
  574. if (channel->state != LCS_CH_STATE_SUSPENDED)
  575. return 0;
  576. if (channel->ccws[channel->io_idx].flags & CCW_FLAG_SUSPEND)
  577. return 0;
  578. LCS_DBF_TEXT_(5, trace, "rsch%s", dev_name(&channel->ccwdev->dev));
  579. rc = ccw_device_resume(channel->ccwdev);
  580. if (rc) {
  581. LCS_DBF_TEXT_(4, trace, "ersc%s",
  582. dev_name(&channel->ccwdev->dev));
  583. dev_err(&channel->ccwdev->dev,
  584. "Sending data from the LCS device to the LAN failed"
  585. " with rc=%d\n",rc);
  586. } else
  587. channel->state = LCS_CH_STATE_RUNNING;
  588. return rc;
  589. }
  590. /**
  591. * Make a buffer ready for processing.
  592. */
  593. static inline void
  594. __lcs_ready_buffer_bits(struct lcs_channel *channel, int index)
  595. {
  596. int prev, next;
  597. LCS_DBF_TEXT(5, trace, "rdybits");
  598. prev = (index - 1) & (LCS_NUM_BUFFS - 1);
  599. next = (index + 1) & (LCS_NUM_BUFFS - 1);
  600. /* Check if we may clear the suspend bit of this buffer. */
  601. if (channel->ccws[next].flags & CCW_FLAG_SUSPEND) {
  602. /* Check if we have to set the PCI bit. */
  603. if (!(channel->ccws[prev].flags & CCW_FLAG_SUSPEND))
  604. /* Suspend bit of the previous buffer is not set. */
  605. channel->ccws[index].flags |= CCW_FLAG_PCI;
  606. /* Suspend bit of the next buffer is set. */
  607. channel->ccws[index].flags &= ~CCW_FLAG_SUSPEND;
  608. }
  609. }
  610. static int
  611. lcs_ready_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
  612. {
  613. unsigned long flags;
  614. int index, rc;
  615. LCS_DBF_TEXT(5, trace, "rdybuff");
  616. BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
  617. buffer->state != LCS_BUF_STATE_PROCESSED);
  618. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  619. buffer->state = LCS_BUF_STATE_READY;
  620. index = buffer - channel->iob;
  621. /* Set length. */
  622. channel->ccws[index].count = buffer->count;
  623. /* Check relevant PCI/suspend bits. */
  624. __lcs_ready_buffer_bits(channel, index);
  625. rc = __lcs_resume_channel(channel);
  626. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  627. return rc;
  628. }
  629. /**
  630. * Mark the buffer as processed. Take care of the suspend bit
  631. * of the previous buffer. This function is called from
  632. * interrupt context, so the lock must not be taken.
  633. */
  634. static int
  635. __lcs_processed_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
  636. {
  637. int index, prev, next;
  638. LCS_DBF_TEXT(5, trace, "prcsbuff");
  639. BUG_ON(buffer->state != LCS_BUF_STATE_READY);
  640. buffer->state = LCS_BUF_STATE_PROCESSED;
  641. index = buffer - channel->iob;
  642. prev = (index - 1) & (LCS_NUM_BUFFS - 1);
  643. next = (index + 1) & (LCS_NUM_BUFFS - 1);
  644. /* Set the suspend bit and clear the PCI bit of this buffer. */
  645. channel->ccws[index].flags |= CCW_FLAG_SUSPEND;
  646. channel->ccws[index].flags &= ~CCW_FLAG_PCI;
  647. /* Check the suspend bit of the previous buffer. */
  648. if (channel->iob[prev].state == LCS_BUF_STATE_READY) {
  649. /*
  650. * Previous buffer is in state ready. It might have
  651. * happened in lcs_ready_buffer that the suspend bit
  652. * has not been cleared to avoid an endless loop.
  653. * Do it now.
  654. */
  655. __lcs_ready_buffer_bits(channel, prev);
  656. }
  657. /* Clear PCI bit of next buffer. */
  658. channel->ccws[next].flags &= ~CCW_FLAG_PCI;
  659. return __lcs_resume_channel(channel);
  660. }
  661. /**
  662. * Put a processed buffer back to state empty.
  663. */
  664. static void
  665. lcs_release_buffer(struct lcs_channel *channel, struct lcs_buffer *buffer)
  666. {
  667. unsigned long flags;
  668. LCS_DBF_TEXT(5, trace, "relbuff");
  669. BUG_ON(buffer->state != LCS_BUF_STATE_LOCKED &&
  670. buffer->state != LCS_BUF_STATE_PROCESSED);
  671. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  672. buffer->state = LCS_BUF_STATE_EMPTY;
  673. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  674. }
  675. /**
  676. * Get buffer for a lan command.
  677. */
  678. static struct lcs_buffer *
  679. lcs_get_lancmd(struct lcs_card *card, int count)
  680. {
  681. struct lcs_buffer *buffer;
  682. struct lcs_cmd *cmd;
  683. LCS_DBF_TEXT(4, trace, "getlncmd");
  684. /* Get buffer and wait if none is available. */
  685. wait_event(card->write.wait_q,
  686. ((buffer = lcs_get_buffer(&card->write)) != NULL));
  687. count += sizeof(struct lcs_header);
  688. *(__u16 *)(buffer->data + count) = 0;
  689. buffer->count = count + sizeof(__u16);
  690. buffer->callback = lcs_release_buffer;
  691. cmd = (struct lcs_cmd *) buffer->data;
  692. cmd->offset = count;
  693. cmd->type = LCS_FRAME_TYPE_CONTROL;
  694. cmd->slot = 0;
  695. return buffer;
  696. }
  697. static void
  698. lcs_get_reply(struct lcs_reply *reply)
  699. {
  700. WARN_ON(atomic_read(&reply->refcnt) <= 0);
  701. atomic_inc(&reply->refcnt);
  702. }
  703. static void
  704. lcs_put_reply(struct lcs_reply *reply)
  705. {
  706. WARN_ON(atomic_read(&reply->refcnt) <= 0);
  707. if (atomic_dec_and_test(&reply->refcnt)) {
  708. kfree(reply);
  709. }
  710. }
  711. static struct lcs_reply *
  712. lcs_alloc_reply(struct lcs_cmd *cmd)
  713. {
  714. struct lcs_reply *reply;
  715. LCS_DBF_TEXT(4, trace, "getreply");
  716. reply = kzalloc(sizeof(struct lcs_reply), GFP_ATOMIC);
  717. if (!reply)
  718. return NULL;
  719. atomic_set(&reply->refcnt,1);
  720. reply->sequence_no = cmd->sequence_no;
  721. reply->received = 0;
  722. reply->rc = 0;
  723. init_waitqueue_head(&reply->wait_q);
  724. return reply;
  725. }
  726. /**
  727. * Notifier function for lancmd replies. Called from read irq.
  728. */
  729. static void
  730. lcs_notify_lancmd_waiters(struct lcs_card *card, struct lcs_cmd *cmd)
  731. {
  732. struct list_head *l, *n;
  733. struct lcs_reply *reply;
  734. LCS_DBF_TEXT(4, trace, "notiwait");
  735. spin_lock(&card->lock);
  736. list_for_each_safe(l, n, &card->lancmd_waiters) {
  737. reply = list_entry(l, struct lcs_reply, list);
  738. if (reply->sequence_no == cmd->sequence_no) {
  739. lcs_get_reply(reply);
  740. list_del_init(&reply->list);
  741. if (reply->callback != NULL)
  742. reply->callback(card, cmd);
  743. reply->received = 1;
  744. reply->rc = cmd->return_code;
  745. wake_up(&reply->wait_q);
  746. lcs_put_reply(reply);
  747. break;
  748. }
  749. }
  750. spin_unlock(&card->lock);
  751. }
  752. /**
  753. * Emit buffer of a lan comand.
  754. */
  755. static void
  756. lcs_lancmd_timeout(unsigned long data)
  757. {
  758. struct lcs_reply *reply, *list_reply, *r;
  759. unsigned long flags;
  760. LCS_DBF_TEXT(4, trace, "timeout");
  761. reply = (struct lcs_reply *) data;
  762. spin_lock_irqsave(&reply->card->lock, flags);
  763. list_for_each_entry_safe(list_reply, r,
  764. &reply->card->lancmd_waiters,list) {
  765. if (reply == list_reply) {
  766. lcs_get_reply(reply);
  767. list_del_init(&reply->list);
  768. spin_unlock_irqrestore(&reply->card->lock, flags);
  769. reply->received = 1;
  770. reply->rc = -ETIME;
  771. wake_up(&reply->wait_q);
  772. lcs_put_reply(reply);
  773. return;
  774. }
  775. }
  776. spin_unlock_irqrestore(&reply->card->lock, flags);
  777. }
  778. static int
  779. lcs_send_lancmd(struct lcs_card *card, struct lcs_buffer *buffer,
  780. void (*reply_callback)(struct lcs_card *, struct lcs_cmd *))
  781. {
  782. struct lcs_reply *reply;
  783. struct lcs_cmd *cmd;
  784. struct timer_list timer;
  785. unsigned long flags;
  786. int rc;
  787. LCS_DBF_TEXT(4, trace, "sendcmd");
  788. cmd = (struct lcs_cmd *) buffer->data;
  789. cmd->return_code = 0;
  790. cmd->sequence_no = card->sequence_no++;
  791. reply = lcs_alloc_reply(cmd);
  792. if (!reply)
  793. return -ENOMEM;
  794. reply->callback = reply_callback;
  795. reply->card = card;
  796. spin_lock_irqsave(&card->lock, flags);
  797. list_add_tail(&reply->list, &card->lancmd_waiters);
  798. spin_unlock_irqrestore(&card->lock, flags);
  799. buffer->callback = lcs_release_buffer;
  800. rc = lcs_ready_buffer(&card->write, buffer);
  801. if (rc)
  802. return rc;
  803. init_timer_on_stack(&timer);
  804. timer.function = lcs_lancmd_timeout;
  805. timer.data = (unsigned long) reply;
  806. timer.expires = jiffies + HZ*card->lancmd_timeout;
  807. add_timer(&timer);
  808. wait_event(reply->wait_q, reply->received);
  809. del_timer_sync(&timer);
  810. LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
  811. rc = reply->rc;
  812. lcs_put_reply(reply);
  813. return rc ? -EIO : 0;
  814. }
  815. /**
  816. * LCS startup command
  817. */
  818. static int
  819. lcs_send_startup(struct lcs_card *card, __u8 initiator)
  820. {
  821. struct lcs_buffer *buffer;
  822. struct lcs_cmd *cmd;
  823. LCS_DBF_TEXT(2, trace, "startup");
  824. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  825. cmd = (struct lcs_cmd *) buffer->data;
  826. cmd->cmd_code = LCS_CMD_STARTUP;
  827. cmd->initiator = initiator;
  828. cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
  829. return lcs_send_lancmd(card, buffer, NULL);
  830. }
  831. /**
  832. * LCS shutdown command
  833. */
  834. static int
  835. lcs_send_shutdown(struct lcs_card *card)
  836. {
  837. struct lcs_buffer *buffer;
  838. struct lcs_cmd *cmd;
  839. LCS_DBF_TEXT(2, trace, "shutdown");
  840. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  841. cmd = (struct lcs_cmd *) buffer->data;
  842. cmd->cmd_code = LCS_CMD_SHUTDOWN;
  843. cmd->initiator = LCS_INITIATOR_TCPIP;
  844. return lcs_send_lancmd(card, buffer, NULL);
  845. }
  846. /**
  847. * LCS lanstat command
  848. */
  849. static void
  850. __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
  851. {
  852. LCS_DBF_TEXT(2, trace, "statcb");
  853. memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
  854. }
  855. static int
  856. lcs_send_lanstat(struct lcs_card *card)
  857. {
  858. struct lcs_buffer *buffer;
  859. struct lcs_cmd *cmd;
  860. LCS_DBF_TEXT(2,trace, "cmdstat");
  861. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  862. cmd = (struct lcs_cmd *) buffer->data;
  863. /* Setup lanstat command. */
  864. cmd->cmd_code = LCS_CMD_LANSTAT;
  865. cmd->initiator = LCS_INITIATOR_TCPIP;
  866. cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
  867. cmd->cmd.lcs_std_cmd.portno = card->portno;
  868. return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
  869. }
  870. /**
  871. * send stoplan command
  872. */
  873. static int
  874. lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
  875. {
  876. struct lcs_buffer *buffer;
  877. struct lcs_cmd *cmd;
  878. LCS_DBF_TEXT(2, trace, "cmdstpln");
  879. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  880. cmd = (struct lcs_cmd *) buffer->data;
  881. cmd->cmd_code = LCS_CMD_STOPLAN;
  882. cmd->initiator = initiator;
  883. cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
  884. cmd->cmd.lcs_std_cmd.portno = card->portno;
  885. return lcs_send_lancmd(card, buffer, NULL);
  886. }
  887. /**
  888. * send startlan command
  889. */
  890. static void
  891. __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
  892. {
  893. LCS_DBF_TEXT(2, trace, "srtlancb");
  894. card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
  895. card->portno = cmd->cmd.lcs_std_cmd.portno;
  896. }
  897. static int
  898. lcs_send_startlan(struct lcs_card *card, __u8 initiator)
  899. {
  900. struct lcs_buffer *buffer;
  901. struct lcs_cmd *cmd;
  902. LCS_DBF_TEXT(2, trace, "cmdstaln");
  903. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  904. cmd = (struct lcs_cmd *) buffer->data;
  905. cmd->cmd_code = LCS_CMD_STARTLAN;
  906. cmd->initiator = initiator;
  907. cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
  908. cmd->cmd.lcs_std_cmd.portno = card->portno;
  909. return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
  910. }
  911. #ifdef CONFIG_IP_MULTICAST
  912. /**
  913. * send setipm command (Multicast)
  914. */
  915. static int
  916. lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
  917. {
  918. struct lcs_buffer *buffer;
  919. struct lcs_cmd *cmd;
  920. LCS_DBF_TEXT(2, trace, "cmdsetim");
  921. buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
  922. cmd = (struct lcs_cmd *) buffer->data;
  923. cmd->cmd_code = LCS_CMD_SETIPM;
  924. cmd->initiator = LCS_INITIATOR_TCPIP;
  925. cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
  926. cmd->cmd.lcs_qipassist.portno = card->portno;
  927. cmd->cmd.lcs_qipassist.version = 4;
  928. cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
  929. memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
  930. &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
  931. LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
  932. return lcs_send_lancmd(card, buffer, NULL);
  933. }
  934. /**
  935. * send delipm command (Multicast)
  936. */
  937. static int
  938. lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
  939. {
  940. struct lcs_buffer *buffer;
  941. struct lcs_cmd *cmd;
  942. LCS_DBF_TEXT(2, trace, "cmddelim");
  943. buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
  944. cmd = (struct lcs_cmd *) buffer->data;
  945. cmd->cmd_code = LCS_CMD_DELIPM;
  946. cmd->initiator = LCS_INITIATOR_TCPIP;
  947. cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
  948. cmd->cmd.lcs_qipassist.portno = card->portno;
  949. cmd->cmd.lcs_qipassist.version = 4;
  950. cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
  951. memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
  952. &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
  953. LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
  954. return lcs_send_lancmd(card, buffer, NULL);
  955. }
  956. /**
  957. * check if multicast is supported by LCS
  958. */
  959. static void
  960. __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
  961. {
  962. LCS_DBF_TEXT(2, trace, "chkmccb");
  963. card->ip_assists_supported =
  964. cmd->cmd.lcs_qipassist.ip_assists_supported;
  965. card->ip_assists_enabled =
  966. cmd->cmd.lcs_qipassist.ip_assists_enabled;
  967. }
  968. static int
  969. lcs_check_multicast_support(struct lcs_card *card)
  970. {
  971. struct lcs_buffer *buffer;
  972. struct lcs_cmd *cmd;
  973. int rc;
  974. LCS_DBF_TEXT(2, trace, "cmdqipa");
  975. /* Send query ipassist. */
  976. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  977. cmd = (struct lcs_cmd *) buffer->data;
  978. cmd->cmd_code = LCS_CMD_QIPASSIST;
  979. cmd->initiator = LCS_INITIATOR_TCPIP;
  980. cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
  981. cmd->cmd.lcs_qipassist.portno = card->portno;
  982. cmd->cmd.lcs_qipassist.version = 4;
  983. cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
  984. rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
  985. if (rc != 0) {
  986. pr_err("Query IPAssist failed. Assuming unsupported!\n");
  987. return -EOPNOTSUPP;
  988. }
  989. if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
  990. return 0;
  991. return -EOPNOTSUPP;
  992. }
  993. /**
  994. * set or del multicast address on LCS card
  995. */
  996. static void
  997. lcs_fix_multicast_list(struct lcs_card *card)
  998. {
  999. struct list_head failed_list;
  1000. struct lcs_ipm_list *ipm, *tmp;
  1001. unsigned long flags;
  1002. int rc;
  1003. LCS_DBF_TEXT(4,trace, "fixipm");
  1004. INIT_LIST_HEAD(&failed_list);
  1005. spin_lock_irqsave(&card->ipm_lock, flags);
  1006. list_modified:
  1007. list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
  1008. switch (ipm->ipm_state) {
  1009. case LCS_IPM_STATE_SET_REQUIRED:
  1010. /* del from ipm_list so noone else can tamper with
  1011. * this entry */
  1012. list_del_init(&ipm->list);
  1013. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1014. rc = lcs_send_setipm(card, ipm);
  1015. spin_lock_irqsave(&card->ipm_lock, flags);
  1016. if (rc) {
  1017. pr_info("Adding multicast address failed."
  1018. " Table possibly full!\n");
  1019. /* store ipm in failed list -> will be added
  1020. * to ipm_list again, so a retry will be done
  1021. * during the next call of this function */
  1022. list_add_tail(&ipm->list, &failed_list);
  1023. } else {
  1024. ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
  1025. /* re-insert into ipm_list */
  1026. list_add_tail(&ipm->list, &card->ipm_list);
  1027. }
  1028. goto list_modified;
  1029. case LCS_IPM_STATE_DEL_REQUIRED:
  1030. list_del(&ipm->list);
  1031. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1032. lcs_send_delipm(card, ipm);
  1033. spin_lock_irqsave(&card->ipm_lock, flags);
  1034. kfree(ipm);
  1035. goto list_modified;
  1036. case LCS_IPM_STATE_ON_CARD:
  1037. break;
  1038. }
  1039. }
  1040. /* re-insert all entries from the failed_list into ipm_list */
  1041. list_for_each_entry_safe(ipm, tmp, &failed_list, list)
  1042. list_move_tail(&ipm->list, &card->ipm_list);
  1043. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1044. }
  1045. /**
  1046. * get mac address for the relevant Multicast address
  1047. */
  1048. static void
  1049. lcs_get_mac_for_ipm(__be32 ipm, char *mac, struct net_device *dev)
  1050. {
  1051. LCS_DBF_TEXT(4,trace, "getmac");
  1052. if (dev->type == ARPHRD_IEEE802_TR)
  1053. ip_tr_mc_map(ipm, mac);
  1054. else
  1055. ip_eth_mc_map(ipm, mac);
  1056. }
  1057. /**
  1058. * function called by net device to handle multicast address relevant things
  1059. */
  1060. static inline void
  1061. lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
  1062. {
  1063. struct ip_mc_list *im4;
  1064. struct list_head *l;
  1065. struct lcs_ipm_list *ipm;
  1066. unsigned long flags;
  1067. char buf[MAX_ADDR_LEN];
  1068. LCS_DBF_TEXT(4, trace, "remmclst");
  1069. spin_lock_irqsave(&card->ipm_lock, flags);
  1070. list_for_each(l, &card->ipm_list) {
  1071. ipm = list_entry(l, struct lcs_ipm_list, list);
  1072. for (im4 = rcu_dereference(in4_dev->mc_list);
  1073. im4 != NULL; im4 = rcu_dereference(im4->next_rcu)) {
  1074. lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
  1075. if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
  1076. (memcmp(buf, &ipm->ipm.mac_addr,
  1077. LCS_MAC_LENGTH) == 0) )
  1078. break;
  1079. }
  1080. if (im4 == NULL)
  1081. ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
  1082. }
  1083. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1084. }
  1085. static inline struct lcs_ipm_list *
  1086. lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
  1087. {
  1088. struct lcs_ipm_list *tmp, *ipm = NULL;
  1089. struct list_head *l;
  1090. unsigned long flags;
  1091. LCS_DBF_TEXT(4, trace, "chkmcent");
  1092. spin_lock_irqsave(&card->ipm_lock, flags);
  1093. list_for_each(l, &card->ipm_list) {
  1094. tmp = list_entry(l, struct lcs_ipm_list, list);
  1095. if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
  1096. (memcmp(buf, &tmp->ipm.mac_addr,
  1097. LCS_MAC_LENGTH) == 0) ) {
  1098. ipm = tmp;
  1099. break;
  1100. }
  1101. }
  1102. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1103. return ipm;
  1104. }
  1105. static inline void
  1106. lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
  1107. {
  1108. struct ip_mc_list *im4;
  1109. struct lcs_ipm_list *ipm;
  1110. char buf[MAX_ADDR_LEN];
  1111. unsigned long flags;
  1112. LCS_DBF_TEXT(4, trace, "setmclst");
  1113. for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
  1114. im4 = rcu_dereference(im4->next_rcu)) {
  1115. lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
  1116. ipm = lcs_check_addr_entry(card, im4, buf);
  1117. if (ipm != NULL)
  1118. continue; /* Address already in list. */
  1119. ipm = kzalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
  1120. if (ipm == NULL) {
  1121. pr_info("Not enough memory to add"
  1122. " new multicast entry!\n");
  1123. break;
  1124. }
  1125. memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
  1126. ipm->ipm.ip_addr = im4->multiaddr;
  1127. ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
  1128. spin_lock_irqsave(&card->ipm_lock, flags);
  1129. LCS_DBF_HEX(2,trace,&ipm->ipm.ip_addr,4);
  1130. list_add(&ipm->list, &card->ipm_list);
  1131. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1132. }
  1133. }
  1134. static int
  1135. lcs_register_mc_addresses(void *data)
  1136. {
  1137. struct lcs_card *card;
  1138. struct in_device *in4_dev;
  1139. card = (struct lcs_card *) data;
  1140. if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
  1141. return 0;
  1142. LCS_DBF_TEXT(4, trace, "regmulti");
  1143. in4_dev = in_dev_get(card->dev);
  1144. if (in4_dev == NULL)
  1145. goto out;
  1146. rcu_read_lock();
  1147. lcs_remove_mc_addresses(card,in4_dev);
  1148. lcs_set_mc_addresses(card, in4_dev);
  1149. rcu_read_unlock();
  1150. in_dev_put(in4_dev);
  1151. netif_carrier_off(card->dev);
  1152. netif_tx_disable(card->dev);
  1153. wait_event(card->write.wait_q,
  1154. (card->write.state != LCS_CH_STATE_RUNNING));
  1155. lcs_fix_multicast_list(card);
  1156. if (card->state == DEV_STATE_UP) {
  1157. netif_carrier_on(card->dev);
  1158. netif_wake_queue(card->dev);
  1159. }
  1160. out:
  1161. lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
  1162. return 0;
  1163. }
  1164. #endif /* CONFIG_IP_MULTICAST */
  1165. /**
  1166. * function called by net device to
  1167. * handle multicast address relevant things
  1168. */
  1169. static void
  1170. lcs_set_multicast_list(struct net_device *dev)
  1171. {
  1172. #ifdef CONFIG_IP_MULTICAST
  1173. struct lcs_card *card;
  1174. LCS_DBF_TEXT(4, trace, "setmulti");
  1175. card = (struct lcs_card *) dev->ml_priv;
  1176. if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD))
  1177. schedule_work(&card->kernel_thread_starter);
  1178. #endif /* CONFIG_IP_MULTICAST */
  1179. }
  1180. static long
  1181. lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
  1182. {
  1183. if (!IS_ERR(irb))
  1184. return 0;
  1185. switch (PTR_ERR(irb)) {
  1186. case -EIO:
  1187. dev_warn(&cdev->dev,
  1188. "An I/O-error occurred on the LCS device\n");
  1189. LCS_DBF_TEXT(2, trace, "ckirberr");
  1190. LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
  1191. break;
  1192. case -ETIMEDOUT:
  1193. dev_warn(&cdev->dev,
  1194. "A command timed out on the LCS device\n");
  1195. LCS_DBF_TEXT(2, trace, "ckirberr");
  1196. LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
  1197. break;
  1198. default:
  1199. dev_warn(&cdev->dev,
  1200. "An error occurred on the LCS device, rc=%ld\n",
  1201. PTR_ERR(irb));
  1202. LCS_DBF_TEXT(2, trace, "ckirberr");
  1203. LCS_DBF_TEXT(2, trace, " rc???");
  1204. }
  1205. return PTR_ERR(irb);
  1206. }
  1207. static int
  1208. lcs_get_problem(struct ccw_device *cdev, struct irb *irb)
  1209. {
  1210. int dstat, cstat;
  1211. char *sense;
  1212. sense = (char *) irb->ecw;
  1213. cstat = irb->scsw.cmd.cstat;
  1214. dstat = irb->scsw.cmd.dstat;
  1215. if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
  1216. SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
  1217. SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
  1218. LCS_DBF_TEXT(2, trace, "CGENCHK");
  1219. return 1;
  1220. }
  1221. if (dstat & DEV_STAT_UNIT_CHECK) {
  1222. if (sense[LCS_SENSE_BYTE_1] &
  1223. LCS_SENSE_RESETTING_EVENT) {
  1224. LCS_DBF_TEXT(2, trace, "REVIND");
  1225. return 1;
  1226. }
  1227. if (sense[LCS_SENSE_BYTE_0] &
  1228. LCS_SENSE_CMD_REJECT) {
  1229. LCS_DBF_TEXT(2, trace, "CMDREJ");
  1230. return 0;
  1231. }
  1232. if ((!sense[LCS_SENSE_BYTE_0]) &&
  1233. (!sense[LCS_SENSE_BYTE_1]) &&
  1234. (!sense[LCS_SENSE_BYTE_2]) &&
  1235. (!sense[LCS_SENSE_BYTE_3])) {
  1236. LCS_DBF_TEXT(2, trace, "ZEROSEN");
  1237. return 0;
  1238. }
  1239. LCS_DBF_TEXT(2, trace, "DGENCHK");
  1240. return 1;
  1241. }
  1242. return 0;
  1243. }
  1244. static void
  1245. lcs_schedule_recovery(struct lcs_card *card)
  1246. {
  1247. LCS_DBF_TEXT(2, trace, "startrec");
  1248. if (!lcs_set_thread_start_bit(card, LCS_RECOVERY_THREAD))
  1249. schedule_work(&card->kernel_thread_starter);
  1250. }
  1251. /**
  1252. * IRQ Handler for LCS channels
  1253. */
  1254. static void
  1255. lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  1256. {
  1257. struct lcs_card *card;
  1258. struct lcs_channel *channel;
  1259. int rc, index;
  1260. int cstat, dstat;
  1261. kstat_cpu(smp_processor_id()).irqs[IOINT_LCS]++;
  1262. if (lcs_check_irb_error(cdev, irb))
  1263. return;
  1264. card = CARD_FROM_DEV(cdev);
  1265. if (card->read.ccwdev == cdev)
  1266. channel = &card->read;
  1267. else
  1268. channel = &card->write;
  1269. cstat = irb->scsw.cmd.cstat;
  1270. dstat = irb->scsw.cmd.dstat;
  1271. LCS_DBF_TEXT_(5, trace, "Rint%s", dev_name(&cdev->dev));
  1272. LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.cstat,
  1273. irb->scsw.cmd.dstat);
  1274. LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.fctl,
  1275. irb->scsw.cmd.actl);
  1276. /* Check for channel and device errors presented */
  1277. rc = lcs_get_problem(cdev, irb);
  1278. if (rc || (dstat & DEV_STAT_UNIT_EXCEP)) {
  1279. dev_warn(&cdev->dev,
  1280. "The LCS device stopped because of an error,"
  1281. " dstat=0x%X, cstat=0x%X \n",
  1282. dstat, cstat);
  1283. if (rc) {
  1284. channel->state = LCS_CH_STATE_ERROR;
  1285. }
  1286. }
  1287. if (channel->state == LCS_CH_STATE_ERROR) {
  1288. lcs_schedule_recovery(card);
  1289. wake_up(&card->wait_q);
  1290. return;
  1291. }
  1292. /* How far in the ccw chain have we processed? */
  1293. if ((channel->state != LCS_CH_STATE_INIT) &&
  1294. (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
  1295. (irb->scsw.cmd.cpa != 0)) {
  1296. index = (struct ccw1 *) __va((addr_t) irb->scsw.cmd.cpa)
  1297. - channel->ccws;
  1298. if ((irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED) ||
  1299. (irb->scsw.cmd.cstat & SCHN_STAT_PCI))
  1300. /* Bloody io subsystem tells us lies about cpa... */
  1301. index = (index - 1) & (LCS_NUM_BUFFS - 1);
  1302. while (channel->io_idx != index) {
  1303. __lcs_processed_buffer(channel,
  1304. channel->iob + channel->io_idx);
  1305. channel->io_idx =
  1306. (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
  1307. }
  1308. }
  1309. if ((irb->scsw.cmd.dstat & DEV_STAT_DEV_END) ||
  1310. (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) ||
  1311. (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK))
  1312. /* Mark channel as stopped. */
  1313. channel->state = LCS_CH_STATE_STOPPED;
  1314. else if (irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED)
  1315. /* CCW execution stopped on a suspend bit. */
  1316. channel->state = LCS_CH_STATE_SUSPENDED;
  1317. if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
  1318. if (irb->scsw.cmd.cc != 0) {
  1319. ccw_device_halt(channel->ccwdev, (addr_t) channel);
  1320. return;
  1321. }
  1322. /* The channel has been stopped by halt_IO. */
  1323. channel->state = LCS_CH_STATE_HALTED;
  1324. }
  1325. if (irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC)
  1326. channel->state = LCS_CH_STATE_CLEARED;
  1327. /* Do the rest in the tasklet. */
  1328. tasklet_schedule(&channel->irq_tasklet);
  1329. }
  1330. /**
  1331. * Tasklet for IRQ handler
  1332. */
  1333. static void
  1334. lcs_tasklet(unsigned long data)
  1335. {
  1336. unsigned long flags;
  1337. struct lcs_channel *channel;
  1338. struct lcs_buffer *iob;
  1339. int buf_idx;
  1340. int rc;
  1341. channel = (struct lcs_channel *) data;
  1342. LCS_DBF_TEXT_(5, trace, "tlet%s", dev_name(&channel->ccwdev->dev));
  1343. /* Check for processed buffers. */
  1344. iob = channel->iob;
  1345. buf_idx = channel->buf_idx;
  1346. while (iob[buf_idx].state == LCS_BUF_STATE_PROCESSED) {
  1347. /* Do the callback thing. */
  1348. if (iob[buf_idx].callback != NULL)
  1349. iob[buf_idx].callback(channel, iob + buf_idx);
  1350. buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
  1351. }
  1352. channel->buf_idx = buf_idx;
  1353. if (channel->state == LCS_CH_STATE_STOPPED)
  1354. // FIXME: what if rc != 0 ??
  1355. rc = lcs_start_channel(channel);
  1356. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  1357. if (channel->state == LCS_CH_STATE_SUSPENDED &&
  1358. channel->iob[channel->io_idx].state == LCS_BUF_STATE_READY) {
  1359. // FIXME: what if rc != 0 ??
  1360. rc = __lcs_resume_channel(channel);
  1361. }
  1362. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  1363. /* Something happened on the channel. Wake up waiters. */
  1364. wake_up(&channel->wait_q);
  1365. }
  1366. /**
  1367. * Finish current tx buffer and make it ready for transmit.
  1368. */
  1369. static void
  1370. __lcs_emit_txbuffer(struct lcs_card *card)
  1371. {
  1372. LCS_DBF_TEXT(5, trace, "emittx");
  1373. *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
  1374. card->tx_buffer->count += 2;
  1375. lcs_ready_buffer(&card->write, card->tx_buffer);
  1376. card->tx_buffer = NULL;
  1377. card->tx_emitted++;
  1378. }
  1379. /**
  1380. * Callback for finished tx buffers.
  1381. */
  1382. static void
  1383. lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
  1384. {
  1385. struct lcs_card *card;
  1386. LCS_DBF_TEXT(5, trace, "txbuffcb");
  1387. /* Put buffer back to pool. */
  1388. lcs_release_buffer(channel, buffer);
  1389. card = container_of(channel, struct lcs_card, write);
  1390. if (netif_queue_stopped(card->dev) && netif_carrier_ok(card->dev))
  1391. netif_wake_queue(card->dev);
  1392. spin_lock(&card->lock);
  1393. card->tx_emitted--;
  1394. if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
  1395. /*
  1396. * Last running tx buffer has finished. Submit partially
  1397. * filled current buffer.
  1398. */
  1399. __lcs_emit_txbuffer(card);
  1400. spin_unlock(&card->lock);
  1401. }
  1402. /**
  1403. * Packet transmit function called by network stack
  1404. */
  1405. static int
  1406. __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
  1407. struct net_device *dev)
  1408. {
  1409. struct lcs_header *header;
  1410. int rc = NETDEV_TX_OK;
  1411. LCS_DBF_TEXT(5, trace, "hardxmit");
  1412. if (skb == NULL) {
  1413. card->stats.tx_dropped++;
  1414. card->stats.tx_errors++;
  1415. return NETDEV_TX_OK;
  1416. }
  1417. if (card->state != DEV_STATE_UP) {
  1418. dev_kfree_skb(skb);
  1419. card->stats.tx_dropped++;
  1420. card->stats.tx_errors++;
  1421. card->stats.tx_carrier_errors++;
  1422. return NETDEV_TX_OK;
  1423. }
  1424. if (skb->protocol == htons(ETH_P_IPV6)) {
  1425. dev_kfree_skb(skb);
  1426. return NETDEV_TX_OK;
  1427. }
  1428. netif_stop_queue(card->dev);
  1429. spin_lock(&card->lock);
  1430. if (card->tx_buffer != NULL &&
  1431. card->tx_buffer->count + sizeof(struct lcs_header) +
  1432. skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
  1433. /* skb too big for current tx buffer. */
  1434. __lcs_emit_txbuffer(card);
  1435. if (card->tx_buffer == NULL) {
  1436. /* Get new tx buffer */
  1437. card->tx_buffer = lcs_get_buffer(&card->write);
  1438. if (card->tx_buffer == NULL) {
  1439. card->stats.tx_dropped++;
  1440. rc = NETDEV_TX_BUSY;
  1441. goto out;
  1442. }
  1443. card->tx_buffer->callback = lcs_txbuffer_cb;
  1444. card->tx_buffer->count = 0;
  1445. }
  1446. header = (struct lcs_header *)
  1447. (card->tx_buffer->data + card->tx_buffer->count);
  1448. card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
  1449. header->offset = card->tx_buffer->count;
  1450. header->type = card->lan_type;
  1451. header->slot = card->portno;
  1452. skb_copy_from_linear_data(skb, header + 1, skb->len);
  1453. spin_unlock(&card->lock);
  1454. card->stats.tx_bytes += skb->len;
  1455. card->stats.tx_packets++;
  1456. dev_kfree_skb(skb);
  1457. netif_wake_queue(card->dev);
  1458. spin_lock(&card->lock);
  1459. if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
  1460. /* If this is the first tx buffer emit it immediately. */
  1461. __lcs_emit_txbuffer(card);
  1462. out:
  1463. spin_unlock(&card->lock);
  1464. return rc;
  1465. }
  1466. static int
  1467. lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1468. {
  1469. struct lcs_card *card;
  1470. int rc;
  1471. LCS_DBF_TEXT(5, trace, "pktxmit");
  1472. card = (struct lcs_card *) dev->ml_priv;
  1473. rc = __lcs_start_xmit(card, skb, dev);
  1474. return rc;
  1475. }
  1476. /**
  1477. * send startlan and lanstat command to make LCS device ready
  1478. */
  1479. static int
  1480. lcs_startlan_auto(struct lcs_card *card)
  1481. {
  1482. int rc;
  1483. LCS_DBF_TEXT(2, trace, "strtauto");
  1484. #ifdef CONFIG_NET_ETHERNET
  1485. card->lan_type = LCS_FRAME_TYPE_ENET;
  1486. rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
  1487. if (rc == 0)
  1488. return 0;
  1489. #endif
  1490. #ifdef CONFIG_TR
  1491. card->lan_type = LCS_FRAME_TYPE_TR;
  1492. rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
  1493. if (rc == 0)
  1494. return 0;
  1495. #endif
  1496. #ifdef CONFIG_FDDI
  1497. card->lan_type = LCS_FRAME_TYPE_FDDI;
  1498. rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
  1499. if (rc == 0)
  1500. return 0;
  1501. #endif
  1502. return -EIO;
  1503. }
  1504. static int
  1505. lcs_startlan(struct lcs_card *card)
  1506. {
  1507. int rc, i;
  1508. LCS_DBF_TEXT(2, trace, "startlan");
  1509. rc = 0;
  1510. if (card->portno != LCS_INVALID_PORT_NO) {
  1511. if (card->lan_type == LCS_FRAME_TYPE_AUTO)
  1512. rc = lcs_startlan_auto(card);
  1513. else
  1514. rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
  1515. } else {
  1516. for (i = 0; i <= 16; i++) {
  1517. card->portno = i;
  1518. if (card->lan_type != LCS_FRAME_TYPE_AUTO)
  1519. rc = lcs_send_startlan(card,
  1520. LCS_INITIATOR_TCPIP);
  1521. else
  1522. /* autodetecting lan type */
  1523. rc = lcs_startlan_auto(card);
  1524. if (rc == 0)
  1525. break;
  1526. }
  1527. }
  1528. if (rc == 0)
  1529. return lcs_send_lanstat(card);
  1530. return rc;
  1531. }
  1532. /**
  1533. * LCS detect function
  1534. * setup channels and make them I/O ready
  1535. */
  1536. static int
  1537. lcs_detect(struct lcs_card *card)
  1538. {
  1539. int rc = 0;
  1540. LCS_DBF_TEXT(2, setup, "lcsdetct");
  1541. /* start/reset card */
  1542. if (card->dev)
  1543. netif_stop_queue(card->dev);
  1544. rc = lcs_stop_channels(card);
  1545. if (rc == 0) {
  1546. rc = lcs_start_channels(card);
  1547. if (rc == 0) {
  1548. rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
  1549. if (rc == 0)
  1550. rc = lcs_startlan(card);
  1551. }
  1552. }
  1553. if (rc == 0) {
  1554. card->state = DEV_STATE_UP;
  1555. } else {
  1556. card->state = DEV_STATE_DOWN;
  1557. card->write.state = LCS_CH_STATE_INIT;
  1558. card->read.state = LCS_CH_STATE_INIT;
  1559. }
  1560. return rc;
  1561. }
  1562. /**
  1563. * LCS Stop card
  1564. */
  1565. static int
  1566. lcs_stopcard(struct lcs_card *card)
  1567. {
  1568. int rc;
  1569. LCS_DBF_TEXT(3, setup, "stopcard");
  1570. if (card->read.state != LCS_CH_STATE_STOPPED &&
  1571. card->write.state != LCS_CH_STATE_STOPPED &&
  1572. card->read.state != LCS_CH_STATE_ERROR &&
  1573. card->write.state != LCS_CH_STATE_ERROR &&
  1574. card->state == DEV_STATE_UP) {
  1575. lcs_clear_multicast_list(card);
  1576. rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
  1577. rc = lcs_send_shutdown(card);
  1578. }
  1579. rc = lcs_stop_channels(card);
  1580. card->state = DEV_STATE_DOWN;
  1581. return rc;
  1582. }
  1583. /**
  1584. * Kernel Thread helper functions for LGW initiated commands
  1585. */
  1586. static void
  1587. lcs_start_kernel_thread(struct work_struct *work)
  1588. {
  1589. struct lcs_card *card = container_of(work, struct lcs_card, kernel_thread_starter);
  1590. LCS_DBF_TEXT(5, trace, "krnthrd");
  1591. if (lcs_do_start_thread(card, LCS_RECOVERY_THREAD))
  1592. kthread_run(lcs_recovery, card, "lcs_recover");
  1593. #ifdef CONFIG_IP_MULTICAST
  1594. if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
  1595. kthread_run(lcs_register_mc_addresses, card, "regipm");
  1596. #endif
  1597. }
  1598. /**
  1599. * Process control frames.
  1600. */
  1601. static void
  1602. lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
  1603. {
  1604. LCS_DBF_TEXT(5, trace, "getctrl");
  1605. if (cmd->initiator == LCS_INITIATOR_LGW) {
  1606. switch(cmd->cmd_code) {
  1607. case LCS_CMD_STARTUP:
  1608. case LCS_CMD_STARTLAN:
  1609. lcs_schedule_recovery(card);
  1610. break;
  1611. case LCS_CMD_STOPLAN:
  1612. pr_warning("Stoplan for %s initiated by LGW.\n",
  1613. card->dev->name);
  1614. if (card->dev)
  1615. netif_carrier_off(card->dev);
  1616. break;
  1617. default:
  1618. LCS_DBF_TEXT(5, trace, "noLGWcmd");
  1619. break;
  1620. }
  1621. } else
  1622. lcs_notify_lancmd_waiters(card, cmd);
  1623. }
  1624. /**
  1625. * Unpack network packet.
  1626. */
  1627. static void
  1628. lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
  1629. {
  1630. struct sk_buff *skb;
  1631. LCS_DBF_TEXT(5, trace, "getskb");
  1632. if (card->dev == NULL ||
  1633. card->state != DEV_STATE_UP)
  1634. /* The card isn't up. Ignore the packet. */
  1635. return;
  1636. skb = dev_alloc_skb(skb_len);
  1637. if (skb == NULL) {
  1638. dev_err(&card->dev->dev,
  1639. " Allocating a socket buffer to interface %s failed\n",
  1640. card->dev->name);
  1641. card->stats.rx_dropped++;
  1642. return;
  1643. }
  1644. memcpy(skb_put(skb, skb_len), skb_data, skb_len);
  1645. skb->protocol = card->lan_type_trans(skb, card->dev);
  1646. card->stats.rx_bytes += skb_len;
  1647. card->stats.rx_packets++;
  1648. if (skb->protocol == htons(ETH_P_802_2))
  1649. *((__u32 *)skb->cb) = ++card->pkt_seq;
  1650. netif_rx(skb);
  1651. }
  1652. /**
  1653. * LCS main routine to get packets and lancmd replies from the buffers
  1654. */
  1655. static void
  1656. lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
  1657. {
  1658. struct lcs_card *card;
  1659. struct lcs_header *lcs_hdr;
  1660. __u16 offset;
  1661. LCS_DBF_TEXT(5, trace, "lcsgtpkt");
  1662. lcs_hdr = (struct lcs_header *) buffer->data;
  1663. if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
  1664. LCS_DBF_TEXT(4, trace, "-eiogpkt");
  1665. return;
  1666. }
  1667. card = container_of(channel, struct lcs_card, read);
  1668. offset = 0;
  1669. while (lcs_hdr->offset != 0) {
  1670. if (lcs_hdr->offset <= 0 ||
  1671. lcs_hdr->offset > LCS_IOBUFFERSIZE ||
  1672. lcs_hdr->offset < offset) {
  1673. /* Offset invalid. */
  1674. card->stats.rx_length_errors++;
  1675. card->stats.rx_errors++;
  1676. return;
  1677. }
  1678. /* What kind of frame is it? */
  1679. if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
  1680. /* Control frame. */
  1681. lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
  1682. else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
  1683. lcs_hdr->type == LCS_FRAME_TYPE_TR ||
  1684. lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
  1685. /* Normal network packet. */
  1686. lcs_get_skb(card, (char *)(lcs_hdr + 1),
  1687. lcs_hdr->offset - offset -
  1688. sizeof(struct lcs_header));
  1689. else
  1690. /* Unknown frame type. */
  1691. ; // FIXME: error message ?
  1692. /* Proceed to next frame. */
  1693. offset = lcs_hdr->offset;
  1694. lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
  1695. lcs_hdr = (struct lcs_header *) (buffer->data + offset);
  1696. }
  1697. /* The buffer is now empty. Make it ready again. */
  1698. lcs_ready_buffer(&card->read, buffer);
  1699. }
  1700. /**
  1701. * get network statistics for ifconfig and other user programs
  1702. */
  1703. static struct net_device_stats *
  1704. lcs_getstats(struct net_device *dev)
  1705. {
  1706. struct lcs_card *card;
  1707. LCS_DBF_TEXT(4, trace, "netstats");
  1708. card = (struct lcs_card *) dev->ml_priv;
  1709. return &card->stats;
  1710. }
  1711. /**
  1712. * stop lcs device
  1713. * This function will be called by user doing ifconfig xxx down
  1714. */
  1715. static int
  1716. lcs_stop_device(struct net_device *dev)
  1717. {
  1718. struct lcs_card *card;
  1719. int rc;
  1720. LCS_DBF_TEXT(2, trace, "stopdev");
  1721. card = (struct lcs_card *) dev->ml_priv;
  1722. netif_carrier_off(dev);
  1723. netif_tx_disable(dev);
  1724. dev->flags &= ~IFF_UP;
  1725. wait_event(card->write.wait_q,
  1726. (card->write.state != LCS_CH_STATE_RUNNING));
  1727. rc = lcs_stopcard(card);
  1728. if (rc)
  1729. dev_err(&card->dev->dev,
  1730. " Shutting down the LCS device failed\n ");
  1731. return rc;
  1732. }
  1733. /**
  1734. * start lcs device and make it runnable
  1735. * This function will be called by user doing ifconfig xxx up
  1736. */
  1737. static int
  1738. lcs_open_device(struct net_device *dev)
  1739. {
  1740. struct lcs_card *card;
  1741. int rc;
  1742. LCS_DBF_TEXT(2, trace, "opendev");
  1743. card = (struct lcs_card *) dev->ml_priv;
  1744. /* initialize statistics */
  1745. rc = lcs_detect(card);
  1746. if (rc) {
  1747. pr_err("Error in opening device!\n");
  1748. } else {
  1749. dev->flags |= IFF_UP;
  1750. netif_carrier_on(dev);
  1751. netif_wake_queue(dev);
  1752. card->state = DEV_STATE_UP;
  1753. }
  1754. return rc;
  1755. }
  1756. /**
  1757. * show function for portno called by cat or similar things
  1758. */
  1759. static ssize_t
  1760. lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
  1761. {
  1762. struct lcs_card *card;
  1763. card = dev_get_drvdata(dev);
  1764. if (!card)
  1765. return 0;
  1766. return sprintf(buf, "%d\n", card->portno);
  1767. }
  1768. /**
  1769. * store the value which is piped to file portno
  1770. */
  1771. static ssize_t
  1772. lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1773. {
  1774. struct lcs_card *card;
  1775. int value;
  1776. card = dev_get_drvdata(dev);
  1777. if (!card)
  1778. return 0;
  1779. sscanf(buf, "%u", &value);
  1780. /* TODO: sanity checks */
  1781. card->portno = value;
  1782. return count;
  1783. }
  1784. static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
  1785. const char *lcs_type[] = {
  1786. "not a channel",
  1787. "2216 parallel",
  1788. "2216 channel",
  1789. "OSA LCS card",
  1790. "unknown channel type",
  1791. "unsupported channel type",
  1792. };
  1793. static ssize_t
  1794. lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  1795. {
  1796. struct ccwgroup_device *cgdev;
  1797. cgdev = to_ccwgroupdev(dev);
  1798. if (!cgdev)
  1799. return -ENODEV;
  1800. return sprintf(buf, "%s\n", lcs_type[cgdev->cdev[0]->id.driver_info]);
  1801. }
  1802. static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
  1803. static ssize_t
  1804. lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
  1805. {
  1806. struct lcs_card *card;
  1807. card = dev_get_drvdata(dev);
  1808. return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
  1809. }
  1810. static ssize_t
  1811. lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1812. {
  1813. struct lcs_card *card;
  1814. int value;
  1815. card = dev_get_drvdata(dev);
  1816. if (!card)
  1817. return 0;
  1818. sscanf(buf, "%u", &value);
  1819. /* TODO: sanity checks */
  1820. card->lancmd_timeout = value;
  1821. return count;
  1822. }
  1823. static DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
  1824. static ssize_t
  1825. lcs_dev_recover_store(struct device *dev, struct device_attribute *attr,
  1826. const char *buf, size_t count)
  1827. {
  1828. struct lcs_card *card = dev_get_drvdata(dev);
  1829. char *tmp;
  1830. int i;
  1831. if (!card)
  1832. return -EINVAL;
  1833. if (card->state != DEV_STATE_UP)
  1834. return -EPERM;
  1835. i = simple_strtoul(buf, &tmp, 16);
  1836. if (i == 1)
  1837. lcs_schedule_recovery(card);
  1838. return count;
  1839. }
  1840. static DEVICE_ATTR(recover, 0200, NULL, lcs_dev_recover_store);
  1841. static struct attribute * lcs_attrs[] = {
  1842. &dev_attr_portno.attr,
  1843. &dev_attr_type.attr,
  1844. &dev_attr_lancmd_timeout.attr,
  1845. &dev_attr_recover.attr,
  1846. NULL,
  1847. };
  1848. static struct attribute_group lcs_attr_group = {
  1849. .attrs = lcs_attrs,
  1850. };
  1851. /**
  1852. * lcs_probe_device is called on establishing a new ccwgroup_device.
  1853. */
  1854. static int
  1855. lcs_probe_device(struct ccwgroup_device *ccwgdev)
  1856. {
  1857. struct lcs_card *card;
  1858. int ret;
  1859. if (!get_device(&ccwgdev->dev))
  1860. return -ENODEV;
  1861. LCS_DBF_TEXT(2, setup, "add_dev");
  1862. card = lcs_alloc_card();
  1863. if (!card) {
  1864. LCS_DBF_TEXT_(2, setup, " rc%d", -ENOMEM);
  1865. put_device(&ccwgdev->dev);
  1866. return -ENOMEM;
  1867. }
  1868. ret = sysfs_create_group(&ccwgdev->dev.kobj, &lcs_attr_group);
  1869. if (ret) {
  1870. lcs_free_card(card);
  1871. put_device(&ccwgdev->dev);
  1872. return ret;
  1873. }
  1874. dev_set_drvdata(&ccwgdev->dev, card);
  1875. ccwgdev->cdev[0]->handler = lcs_irq;
  1876. ccwgdev->cdev[1]->handler = lcs_irq;
  1877. card->gdev = ccwgdev;
  1878. INIT_WORK(&card->kernel_thread_starter, lcs_start_kernel_thread);
  1879. card->thread_start_mask = 0;
  1880. card->thread_allowed_mask = 0;
  1881. card->thread_running_mask = 0;
  1882. return 0;
  1883. }
  1884. static int
  1885. lcs_register_netdev(struct ccwgroup_device *ccwgdev)
  1886. {
  1887. struct lcs_card *card;
  1888. LCS_DBF_TEXT(2, setup, "regnetdv");
  1889. card = dev_get_drvdata(&ccwgdev->dev);
  1890. if (card->dev->reg_state != NETREG_UNINITIALIZED)
  1891. return 0;
  1892. SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
  1893. return register_netdev(card->dev);
  1894. }
  1895. /**
  1896. * lcs_new_device will be called by setting the group device online.
  1897. */
  1898. static const struct net_device_ops lcs_netdev_ops = {
  1899. .ndo_open = lcs_open_device,
  1900. .ndo_stop = lcs_stop_device,
  1901. .ndo_get_stats = lcs_getstats,
  1902. .ndo_start_xmit = lcs_start_xmit,
  1903. };
  1904. static const struct net_device_ops lcs_mc_netdev_ops = {
  1905. .ndo_open = lcs_open_device,
  1906. .ndo_stop = lcs_stop_device,
  1907. .ndo_get_stats = lcs_getstats,
  1908. .ndo_start_xmit = lcs_start_xmit,
  1909. .ndo_set_multicast_list = lcs_set_multicast_list,
  1910. };
  1911. static int
  1912. lcs_new_device(struct ccwgroup_device *ccwgdev)
  1913. {
  1914. struct lcs_card *card;
  1915. struct net_device *dev=NULL;
  1916. enum lcs_dev_states recover_state;
  1917. int rc;
  1918. card = dev_get_drvdata(&ccwgdev->dev);
  1919. if (!card)
  1920. return -ENODEV;
  1921. LCS_DBF_TEXT(2, setup, "newdev");
  1922. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  1923. card->read.ccwdev = ccwgdev->cdev[0];
  1924. card->write.ccwdev = ccwgdev->cdev[1];
  1925. recover_state = card->state;
  1926. rc = ccw_device_set_online(card->read.ccwdev);
  1927. if (rc)
  1928. goto out_err;
  1929. rc = ccw_device_set_online(card->write.ccwdev);
  1930. if (rc)
  1931. goto out_werr;
  1932. LCS_DBF_TEXT(3, setup, "lcsnewdv");
  1933. lcs_setup_card(card);
  1934. rc = lcs_detect(card);
  1935. if (rc) {
  1936. LCS_DBF_TEXT(2, setup, "dtctfail");
  1937. dev_err(&card->dev->dev,
  1938. "Detecting a network adapter for LCS devices"
  1939. " failed with rc=%d (0x%x)\n", rc, rc);
  1940. lcs_stopcard(card);
  1941. goto out;
  1942. }
  1943. if (card->dev) {
  1944. LCS_DBF_TEXT(2, setup, "samedev");
  1945. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  1946. goto netdev_out;
  1947. }
  1948. switch (card->lan_type) {
  1949. #ifdef CONFIG_NET_ETHERNET
  1950. case LCS_FRAME_TYPE_ENET:
  1951. card->lan_type_trans = eth_type_trans;
  1952. dev = alloc_etherdev(0);
  1953. break;
  1954. #endif
  1955. #ifdef CONFIG_TR
  1956. case LCS_FRAME_TYPE_TR:
  1957. card->lan_type_trans = tr_type_trans;
  1958. dev = alloc_trdev(0);
  1959. break;
  1960. #endif
  1961. #ifdef CONFIG_FDDI
  1962. case LCS_FRAME_TYPE_FDDI:
  1963. card->lan_type_trans = fddi_type_trans;
  1964. dev = alloc_fddidev(0);
  1965. break;
  1966. #endif
  1967. default:
  1968. LCS_DBF_TEXT(3, setup, "errinit");
  1969. pr_err(" Initialization failed\n");
  1970. goto out;
  1971. }
  1972. if (!dev)
  1973. goto out;
  1974. card->dev = dev;
  1975. card->dev->ml_priv = card;
  1976. card->dev->netdev_ops = &lcs_netdev_ops;
  1977. memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
  1978. #ifdef CONFIG_IP_MULTICAST
  1979. if (!lcs_check_multicast_support(card))
  1980. card->dev->netdev_ops = &lcs_mc_netdev_ops;
  1981. #endif
  1982. netdev_out:
  1983. lcs_set_allowed_threads(card,0xffffffff);
  1984. if (recover_state == DEV_STATE_RECOVER) {
  1985. lcs_set_multicast_list(card->dev);
  1986. card->dev->flags |= IFF_UP;
  1987. netif_carrier_on(card->dev);
  1988. netif_wake_queue(card->dev);
  1989. card->state = DEV_STATE_UP;
  1990. } else {
  1991. lcs_stopcard(card);
  1992. }
  1993. if (lcs_register_netdev(ccwgdev) != 0)
  1994. goto out;
  1995. /* Print out supported assists: IPv6 */
  1996. pr_info("LCS device %s %s IPv6 support\n", card->dev->name,
  1997. (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
  1998. "with" : "without");
  1999. /* Print out supported assist: Multicast */
  2000. pr_info("LCS device %s %s Multicast support\n", card->dev->name,
  2001. (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
  2002. "with" : "without");
  2003. return 0;
  2004. out:
  2005. ccw_device_set_offline(card->write.ccwdev);
  2006. out_werr:
  2007. ccw_device_set_offline(card->read.ccwdev);
  2008. out_err:
  2009. return -ENODEV;
  2010. }
  2011. /**
  2012. * lcs_shutdown_device, called when setting the group device offline.
  2013. */
  2014. static int
  2015. __lcs_shutdown_device(struct ccwgroup_device *ccwgdev, int recovery_mode)
  2016. {
  2017. struct lcs_card *card;
  2018. enum lcs_dev_states recover_state;
  2019. int ret;
  2020. LCS_DBF_TEXT(3, setup, "shtdndev");
  2021. card = dev_get_drvdata(&ccwgdev->dev);
  2022. if (!card)
  2023. return -ENODEV;
  2024. if (recovery_mode == 0) {
  2025. lcs_set_allowed_threads(card, 0);
  2026. if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
  2027. return -ERESTARTSYS;
  2028. }
  2029. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  2030. recover_state = card->state;
  2031. ret = lcs_stop_device(card->dev);
  2032. ret = ccw_device_set_offline(card->read.ccwdev);
  2033. ret = ccw_device_set_offline(card->write.ccwdev);
  2034. if (recover_state == DEV_STATE_UP) {
  2035. card->state = DEV_STATE_RECOVER;
  2036. }
  2037. if (ret)
  2038. return ret;
  2039. return 0;
  2040. }
  2041. static int
  2042. lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
  2043. {
  2044. return __lcs_shutdown_device(ccwgdev, 0);
  2045. }
  2046. /**
  2047. * drive lcs recovery after startup and startlan initiated by Lan Gateway
  2048. */
  2049. static int
  2050. lcs_recovery(void *ptr)
  2051. {
  2052. struct lcs_card *card;
  2053. struct ccwgroup_device *gdev;
  2054. int rc;
  2055. card = (struct lcs_card *) ptr;
  2056. LCS_DBF_TEXT(4, trace, "recover1");
  2057. if (!lcs_do_run_thread(card, LCS_RECOVERY_THREAD))
  2058. return 0;
  2059. LCS_DBF_TEXT(4, trace, "recover2");
  2060. gdev = card->gdev;
  2061. dev_warn(&gdev->dev,
  2062. "A recovery process has been started for the LCS device\n");
  2063. rc = __lcs_shutdown_device(gdev, 1);
  2064. rc = lcs_new_device(gdev);
  2065. if (!rc)
  2066. pr_info("Device %s successfully recovered!\n",
  2067. card->dev->name);
  2068. else
  2069. pr_info("Device %s could not be recovered!\n",
  2070. card->dev->name);
  2071. lcs_clear_thread_running_bit(card, LCS_RECOVERY_THREAD);
  2072. return 0;
  2073. }
  2074. /**
  2075. * lcs_remove_device, free buffers and card
  2076. */
  2077. static void
  2078. lcs_remove_device(struct ccwgroup_device *ccwgdev)
  2079. {
  2080. struct lcs_card *card;
  2081. card = dev_get_drvdata(&ccwgdev->dev);
  2082. if (!card)
  2083. return;
  2084. LCS_DBF_TEXT(3, setup, "remdev");
  2085. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  2086. if (ccwgdev->state == CCWGROUP_ONLINE) {
  2087. lcs_shutdown_device(ccwgdev);
  2088. }
  2089. if (card->dev)
  2090. unregister_netdev(card->dev);
  2091. sysfs_remove_group(&ccwgdev->dev.kobj, &lcs_attr_group);
  2092. lcs_cleanup_card(card);
  2093. lcs_free_card(card);
  2094. put_device(&ccwgdev->dev);
  2095. }
  2096. static int lcs_pm_suspend(struct lcs_card *card)
  2097. {
  2098. if (card->dev)
  2099. netif_device_detach(card->dev);
  2100. lcs_set_allowed_threads(card, 0);
  2101. lcs_wait_for_threads(card, 0xffffffff);
  2102. if (card->state != DEV_STATE_DOWN)
  2103. __lcs_shutdown_device(card->gdev, 1);
  2104. return 0;
  2105. }
  2106. static int lcs_pm_resume(struct lcs_card *card)
  2107. {
  2108. int rc = 0;
  2109. if (card->state == DEV_STATE_RECOVER)
  2110. rc = lcs_new_device(card->gdev);
  2111. if (card->dev)
  2112. netif_device_attach(card->dev);
  2113. if (rc) {
  2114. dev_warn(&card->gdev->dev, "The lcs device driver "
  2115. "failed to recover the device\n");
  2116. }
  2117. return rc;
  2118. }
  2119. static int lcs_prepare(struct ccwgroup_device *gdev)
  2120. {
  2121. return 0;
  2122. }
  2123. static void lcs_complete(struct ccwgroup_device *gdev)
  2124. {
  2125. return;
  2126. }
  2127. static int lcs_freeze(struct ccwgroup_device *gdev)
  2128. {
  2129. struct lcs_card *card = dev_get_drvdata(&gdev->dev);
  2130. return lcs_pm_suspend(card);
  2131. }
  2132. static int lcs_thaw(struct ccwgroup_device *gdev)
  2133. {
  2134. struct lcs_card *card = dev_get_drvdata(&gdev->dev);
  2135. return lcs_pm_resume(card);
  2136. }
  2137. static int lcs_restore(struct ccwgroup_device *gdev)
  2138. {
  2139. struct lcs_card *card = dev_get_drvdata(&gdev->dev);
  2140. return lcs_pm_resume(card);
  2141. }
  2142. static struct ccw_device_id lcs_ids[] = {
  2143. {CCW_DEVICE(0x3088, 0x08), .driver_info = lcs_channel_type_parallel},
  2144. {CCW_DEVICE(0x3088, 0x1f), .driver_info = lcs_channel_type_2216},
  2145. {CCW_DEVICE(0x3088, 0x60), .driver_info = lcs_channel_type_osa2},
  2146. {},
  2147. };
  2148. MODULE_DEVICE_TABLE(ccw, lcs_ids);
  2149. static struct ccw_driver lcs_ccw_driver = {
  2150. .owner = THIS_MODULE,
  2151. .name = "lcs",
  2152. .ids = lcs_ids,
  2153. .probe = ccwgroup_probe_ccwdev,
  2154. .remove = ccwgroup_remove_ccwdev,
  2155. };
  2156. /**
  2157. * LCS ccwgroup driver registration
  2158. */
  2159. static struct ccwgroup_driver lcs_group_driver = {
  2160. .owner = THIS_MODULE,
  2161. .name = "lcs",
  2162. .max_slaves = 2,
  2163. .driver_id = 0xD3C3E2,
  2164. .probe = lcs_probe_device,
  2165. .remove = lcs_remove_device,
  2166. .set_online = lcs_new_device,
  2167. .set_offline = lcs_shutdown_device,
  2168. .prepare = lcs_prepare,
  2169. .complete = lcs_complete,
  2170. .freeze = lcs_freeze,
  2171. .thaw = lcs_thaw,
  2172. .restore = lcs_restore,
  2173. };
  2174. static ssize_t
  2175. lcs_driver_group_store(struct device_driver *ddrv, const char *buf,
  2176. size_t count)
  2177. {
  2178. int err;
  2179. err = ccwgroup_create_from_string(lcs_root_dev,
  2180. lcs_group_driver.driver_id,
  2181. &lcs_ccw_driver, 2, buf);
  2182. return err ? err : count;
  2183. }
  2184. static DRIVER_ATTR(group, 0200, NULL, lcs_driver_group_store);
  2185. static struct attribute *lcs_group_attrs[] = {
  2186. &driver_attr_group.attr,
  2187. NULL,
  2188. };
  2189. static struct attribute_group lcs_group_attr_group = {
  2190. .attrs = lcs_group_attrs,
  2191. };
  2192. static const struct attribute_group *lcs_group_attr_groups[] = {
  2193. &lcs_group_attr_group,
  2194. NULL,
  2195. };
  2196. /**
  2197. * LCS Module/Kernel initialization function
  2198. */
  2199. static int
  2200. __init lcs_init_module(void)
  2201. {
  2202. int rc;
  2203. pr_info("Loading %s\n", version);
  2204. rc = lcs_register_debug_facility();
  2205. LCS_DBF_TEXT(0, setup, "lcsinit");
  2206. if (rc)
  2207. goto out_err;
  2208. lcs_root_dev = root_device_register("lcs");
  2209. rc = IS_ERR(lcs_root_dev) ? PTR_ERR(lcs_root_dev) : 0;
  2210. if (rc)
  2211. goto register_err;
  2212. rc = ccw_driver_register(&lcs_ccw_driver);
  2213. if (rc)
  2214. goto ccw_err;
  2215. lcs_group_driver.driver.groups = lcs_group_attr_groups;
  2216. rc = ccwgroup_driver_register(&lcs_group_driver);
  2217. if (rc)
  2218. goto ccwgroup_err;
  2219. return 0;
  2220. ccwgroup_err:
  2221. ccw_driver_unregister(&lcs_ccw_driver);
  2222. ccw_err:
  2223. root_device_unregister(lcs_root_dev);
  2224. register_err:
  2225. lcs_unregister_debug_facility();
  2226. out_err:
  2227. pr_err("Initializing the lcs device driver failed\n");
  2228. return rc;
  2229. }
  2230. /**
  2231. * LCS module cleanup function
  2232. */
  2233. static void
  2234. __exit lcs_cleanup_module(void)
  2235. {
  2236. pr_info("Terminating lcs module.\n");
  2237. LCS_DBF_TEXT(0, trace, "cleanup");
  2238. driver_remove_file(&lcs_group_driver.driver,
  2239. &driver_attr_group);
  2240. ccwgroup_driver_unregister(&lcs_group_driver);
  2241. ccw_driver_unregister(&lcs_ccw_driver);
  2242. root_device_unregister(lcs_root_dev);
  2243. lcs_unregister_debug_facility();
  2244. }
  2245. module_init(lcs_init_module);
  2246. module_exit(lcs_cleanup_module);
  2247. MODULE_AUTHOR("Frank Pavlic <fpavlic@de.ibm.com>");
  2248. MODULE_LICENSE("GPL");