lcs.c 62 KB

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