lcs.c 62 KB

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