lcs.c 63 KB

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