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. 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. destroy_timer_on_stack(&timer);
  808. LCS_DBF_TEXT_(4, trace, "rc:%d",reply->rc);
  809. rc = reply->rc;
  810. lcs_put_reply(reply);
  811. return rc ? -EIO : 0;
  812. }
  813. /**
  814. * LCS startup command
  815. */
  816. static int
  817. lcs_send_startup(struct lcs_card *card, __u8 initiator)
  818. {
  819. struct lcs_buffer *buffer;
  820. struct lcs_cmd *cmd;
  821. LCS_DBF_TEXT(2, trace, "startup");
  822. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  823. cmd = (struct lcs_cmd *) buffer->data;
  824. cmd->cmd_code = LCS_CMD_STARTUP;
  825. cmd->initiator = initiator;
  826. cmd->cmd.lcs_startup.buff_size = LCS_IOBUFFERSIZE;
  827. return lcs_send_lancmd(card, buffer, NULL);
  828. }
  829. /**
  830. * LCS shutdown command
  831. */
  832. static int
  833. lcs_send_shutdown(struct lcs_card *card)
  834. {
  835. struct lcs_buffer *buffer;
  836. struct lcs_cmd *cmd;
  837. LCS_DBF_TEXT(2, trace, "shutdown");
  838. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  839. cmd = (struct lcs_cmd *) buffer->data;
  840. cmd->cmd_code = LCS_CMD_SHUTDOWN;
  841. cmd->initiator = LCS_INITIATOR_TCPIP;
  842. return lcs_send_lancmd(card, buffer, NULL);
  843. }
  844. /**
  845. * LCS lanstat command
  846. */
  847. static void
  848. __lcs_lanstat_cb(struct lcs_card *card, struct lcs_cmd *cmd)
  849. {
  850. LCS_DBF_TEXT(2, trace, "statcb");
  851. memcpy(card->mac, cmd->cmd.lcs_lanstat_cmd.mac_addr, LCS_MAC_LENGTH);
  852. }
  853. static int
  854. lcs_send_lanstat(struct lcs_card *card)
  855. {
  856. struct lcs_buffer *buffer;
  857. struct lcs_cmd *cmd;
  858. LCS_DBF_TEXT(2,trace, "cmdstat");
  859. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  860. cmd = (struct lcs_cmd *) buffer->data;
  861. /* Setup lanstat command. */
  862. cmd->cmd_code = LCS_CMD_LANSTAT;
  863. cmd->initiator = LCS_INITIATOR_TCPIP;
  864. cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
  865. cmd->cmd.lcs_std_cmd.portno = card->portno;
  866. return lcs_send_lancmd(card, buffer, __lcs_lanstat_cb);
  867. }
  868. /**
  869. * send stoplan command
  870. */
  871. static int
  872. lcs_send_stoplan(struct lcs_card *card, __u8 initiator)
  873. {
  874. struct lcs_buffer *buffer;
  875. struct lcs_cmd *cmd;
  876. LCS_DBF_TEXT(2, trace, "cmdstpln");
  877. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  878. cmd = (struct lcs_cmd *) buffer->data;
  879. cmd->cmd_code = LCS_CMD_STOPLAN;
  880. cmd->initiator = initiator;
  881. cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
  882. cmd->cmd.lcs_std_cmd.portno = card->portno;
  883. return lcs_send_lancmd(card, buffer, NULL);
  884. }
  885. /**
  886. * send startlan command
  887. */
  888. static void
  889. __lcs_send_startlan_cb(struct lcs_card *card, struct lcs_cmd *cmd)
  890. {
  891. LCS_DBF_TEXT(2, trace, "srtlancb");
  892. card->lan_type = cmd->cmd.lcs_std_cmd.lan_type;
  893. card->portno = cmd->cmd.lcs_std_cmd.portno;
  894. }
  895. static int
  896. lcs_send_startlan(struct lcs_card *card, __u8 initiator)
  897. {
  898. struct lcs_buffer *buffer;
  899. struct lcs_cmd *cmd;
  900. LCS_DBF_TEXT(2, trace, "cmdstaln");
  901. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  902. cmd = (struct lcs_cmd *) buffer->data;
  903. cmd->cmd_code = LCS_CMD_STARTLAN;
  904. cmd->initiator = initiator;
  905. cmd->cmd.lcs_std_cmd.lan_type = card->lan_type;
  906. cmd->cmd.lcs_std_cmd.portno = card->portno;
  907. return lcs_send_lancmd(card, buffer, __lcs_send_startlan_cb);
  908. }
  909. #ifdef CONFIG_IP_MULTICAST
  910. /**
  911. * send setipm command (Multicast)
  912. */
  913. static int
  914. lcs_send_setipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
  915. {
  916. struct lcs_buffer *buffer;
  917. struct lcs_cmd *cmd;
  918. LCS_DBF_TEXT(2, trace, "cmdsetim");
  919. buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
  920. cmd = (struct lcs_cmd *) buffer->data;
  921. cmd->cmd_code = LCS_CMD_SETIPM;
  922. cmd->initiator = LCS_INITIATOR_TCPIP;
  923. cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
  924. cmd->cmd.lcs_qipassist.portno = card->portno;
  925. cmd->cmd.lcs_qipassist.version = 4;
  926. cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
  927. memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
  928. &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
  929. LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
  930. return lcs_send_lancmd(card, buffer, NULL);
  931. }
  932. /**
  933. * send delipm command (Multicast)
  934. */
  935. static int
  936. lcs_send_delipm(struct lcs_card *card,struct lcs_ipm_list *ipm_list)
  937. {
  938. struct lcs_buffer *buffer;
  939. struct lcs_cmd *cmd;
  940. LCS_DBF_TEXT(2, trace, "cmddelim");
  941. buffer = lcs_get_lancmd(card, LCS_MULTICAST_CMD_SIZE);
  942. cmd = (struct lcs_cmd *) buffer->data;
  943. cmd->cmd_code = LCS_CMD_DELIPM;
  944. cmd->initiator = LCS_INITIATOR_TCPIP;
  945. cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
  946. cmd->cmd.lcs_qipassist.portno = card->portno;
  947. cmd->cmd.lcs_qipassist.version = 4;
  948. cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
  949. memcpy(cmd->cmd.lcs_qipassist.lcs_ipass_ctlmsg.ip_mac_pair,
  950. &ipm_list->ipm, sizeof (struct lcs_ip_mac_pair));
  951. LCS_DBF_TEXT_(2, trace, "%x",ipm_list->ipm.ip_addr);
  952. return lcs_send_lancmd(card, buffer, NULL);
  953. }
  954. /**
  955. * check if multicast is supported by LCS
  956. */
  957. static void
  958. __lcs_check_multicast_cb(struct lcs_card *card, struct lcs_cmd *cmd)
  959. {
  960. LCS_DBF_TEXT(2, trace, "chkmccb");
  961. card->ip_assists_supported =
  962. cmd->cmd.lcs_qipassist.ip_assists_supported;
  963. card->ip_assists_enabled =
  964. cmd->cmd.lcs_qipassist.ip_assists_enabled;
  965. }
  966. static int
  967. lcs_check_multicast_support(struct lcs_card *card)
  968. {
  969. struct lcs_buffer *buffer;
  970. struct lcs_cmd *cmd;
  971. int rc;
  972. LCS_DBF_TEXT(2, trace, "cmdqipa");
  973. /* Send query ipassist. */
  974. buffer = lcs_get_lancmd(card, LCS_STD_CMD_SIZE);
  975. cmd = (struct lcs_cmd *) buffer->data;
  976. cmd->cmd_code = LCS_CMD_QIPASSIST;
  977. cmd->initiator = LCS_INITIATOR_TCPIP;
  978. cmd->cmd.lcs_qipassist.lan_type = card->lan_type;
  979. cmd->cmd.lcs_qipassist.portno = card->portno;
  980. cmd->cmd.lcs_qipassist.version = 4;
  981. cmd->cmd.lcs_qipassist.num_ip_pairs = 1;
  982. rc = lcs_send_lancmd(card, buffer, __lcs_check_multicast_cb);
  983. if (rc != 0) {
  984. pr_err("Query IPAssist failed. Assuming unsupported!\n");
  985. return -EOPNOTSUPP;
  986. }
  987. if (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT)
  988. return 0;
  989. return -EOPNOTSUPP;
  990. }
  991. /**
  992. * set or del multicast address on LCS card
  993. */
  994. static void
  995. lcs_fix_multicast_list(struct lcs_card *card)
  996. {
  997. struct list_head failed_list;
  998. struct lcs_ipm_list *ipm, *tmp;
  999. unsigned long flags;
  1000. int rc;
  1001. LCS_DBF_TEXT(4,trace, "fixipm");
  1002. INIT_LIST_HEAD(&failed_list);
  1003. spin_lock_irqsave(&card->ipm_lock, flags);
  1004. list_modified:
  1005. list_for_each_entry_safe(ipm, tmp, &card->ipm_list, list){
  1006. switch (ipm->ipm_state) {
  1007. case LCS_IPM_STATE_SET_REQUIRED:
  1008. /* del from ipm_list so no one else can tamper with
  1009. * this entry */
  1010. list_del_init(&ipm->list);
  1011. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1012. rc = lcs_send_setipm(card, ipm);
  1013. spin_lock_irqsave(&card->ipm_lock, flags);
  1014. if (rc) {
  1015. pr_info("Adding multicast address failed."
  1016. " Table possibly full!\n");
  1017. /* store ipm in failed list -> will be added
  1018. * to ipm_list again, so a retry will be done
  1019. * during the next call of this function */
  1020. list_add_tail(&ipm->list, &failed_list);
  1021. } else {
  1022. ipm->ipm_state = LCS_IPM_STATE_ON_CARD;
  1023. /* re-insert into ipm_list */
  1024. list_add_tail(&ipm->list, &card->ipm_list);
  1025. }
  1026. goto list_modified;
  1027. case LCS_IPM_STATE_DEL_REQUIRED:
  1028. list_del(&ipm->list);
  1029. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1030. lcs_send_delipm(card, ipm);
  1031. spin_lock_irqsave(&card->ipm_lock, flags);
  1032. kfree(ipm);
  1033. goto list_modified;
  1034. case LCS_IPM_STATE_ON_CARD:
  1035. break;
  1036. }
  1037. }
  1038. /* re-insert all entries from the failed_list into ipm_list */
  1039. list_for_each_entry_safe(ipm, tmp, &failed_list, list)
  1040. list_move_tail(&ipm->list, &card->ipm_list);
  1041. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1042. }
  1043. /**
  1044. * get mac address for the relevant Multicast address
  1045. */
  1046. static void
  1047. lcs_get_mac_for_ipm(__be32 ipm, char *mac, struct net_device *dev)
  1048. {
  1049. LCS_DBF_TEXT(4,trace, "getmac");
  1050. ip_eth_mc_map(ipm, mac);
  1051. }
  1052. /**
  1053. * function called by net device to handle multicast address relevant things
  1054. */
  1055. static inline void
  1056. lcs_remove_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
  1057. {
  1058. struct ip_mc_list *im4;
  1059. struct list_head *l;
  1060. struct lcs_ipm_list *ipm;
  1061. unsigned long flags;
  1062. char buf[MAX_ADDR_LEN];
  1063. LCS_DBF_TEXT(4, trace, "remmclst");
  1064. spin_lock_irqsave(&card->ipm_lock, flags);
  1065. list_for_each(l, &card->ipm_list) {
  1066. ipm = list_entry(l, struct lcs_ipm_list, list);
  1067. for (im4 = rcu_dereference(in4_dev->mc_list);
  1068. im4 != NULL; im4 = rcu_dereference(im4->next_rcu)) {
  1069. lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
  1070. if ( (ipm->ipm.ip_addr == im4->multiaddr) &&
  1071. (memcmp(buf, &ipm->ipm.mac_addr,
  1072. LCS_MAC_LENGTH) == 0) )
  1073. break;
  1074. }
  1075. if (im4 == NULL)
  1076. ipm->ipm_state = LCS_IPM_STATE_DEL_REQUIRED;
  1077. }
  1078. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1079. }
  1080. static inline struct lcs_ipm_list *
  1081. lcs_check_addr_entry(struct lcs_card *card, struct ip_mc_list *im4, char *buf)
  1082. {
  1083. struct lcs_ipm_list *tmp, *ipm = NULL;
  1084. struct list_head *l;
  1085. unsigned long flags;
  1086. LCS_DBF_TEXT(4, trace, "chkmcent");
  1087. spin_lock_irqsave(&card->ipm_lock, flags);
  1088. list_for_each(l, &card->ipm_list) {
  1089. tmp = list_entry(l, struct lcs_ipm_list, list);
  1090. if ( (tmp->ipm.ip_addr == im4->multiaddr) &&
  1091. (memcmp(buf, &tmp->ipm.mac_addr,
  1092. LCS_MAC_LENGTH) == 0) ) {
  1093. ipm = tmp;
  1094. break;
  1095. }
  1096. }
  1097. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1098. return ipm;
  1099. }
  1100. static inline void
  1101. lcs_set_mc_addresses(struct lcs_card *card, struct in_device *in4_dev)
  1102. {
  1103. struct ip_mc_list *im4;
  1104. struct lcs_ipm_list *ipm;
  1105. char buf[MAX_ADDR_LEN];
  1106. unsigned long flags;
  1107. LCS_DBF_TEXT(4, trace, "setmclst");
  1108. for (im4 = rcu_dereference(in4_dev->mc_list); im4 != NULL;
  1109. im4 = rcu_dereference(im4->next_rcu)) {
  1110. lcs_get_mac_for_ipm(im4->multiaddr, buf, card->dev);
  1111. ipm = lcs_check_addr_entry(card, im4, buf);
  1112. if (ipm != NULL)
  1113. continue; /* Address already in list. */
  1114. ipm = kzalloc(sizeof(struct lcs_ipm_list), GFP_ATOMIC);
  1115. if (ipm == NULL) {
  1116. pr_info("Not enough memory to add"
  1117. " new multicast entry!\n");
  1118. break;
  1119. }
  1120. memcpy(&ipm->ipm.mac_addr, buf, LCS_MAC_LENGTH);
  1121. ipm->ipm.ip_addr = im4->multiaddr;
  1122. ipm->ipm_state = LCS_IPM_STATE_SET_REQUIRED;
  1123. spin_lock_irqsave(&card->ipm_lock, flags);
  1124. LCS_DBF_HEX(2,trace,&ipm->ipm.ip_addr,4);
  1125. list_add(&ipm->list, &card->ipm_list);
  1126. spin_unlock_irqrestore(&card->ipm_lock, flags);
  1127. }
  1128. }
  1129. static int
  1130. lcs_register_mc_addresses(void *data)
  1131. {
  1132. struct lcs_card *card;
  1133. struct in_device *in4_dev;
  1134. card = (struct lcs_card *) data;
  1135. if (!lcs_do_run_thread(card, LCS_SET_MC_THREAD))
  1136. return 0;
  1137. LCS_DBF_TEXT(4, trace, "regmulti");
  1138. in4_dev = in_dev_get(card->dev);
  1139. if (in4_dev == NULL)
  1140. goto out;
  1141. rcu_read_lock();
  1142. lcs_remove_mc_addresses(card,in4_dev);
  1143. lcs_set_mc_addresses(card, in4_dev);
  1144. rcu_read_unlock();
  1145. in_dev_put(in4_dev);
  1146. netif_carrier_off(card->dev);
  1147. netif_tx_disable(card->dev);
  1148. wait_event(card->write.wait_q,
  1149. (card->write.state != LCS_CH_STATE_RUNNING));
  1150. lcs_fix_multicast_list(card);
  1151. if (card->state == DEV_STATE_UP) {
  1152. netif_carrier_on(card->dev);
  1153. netif_wake_queue(card->dev);
  1154. }
  1155. out:
  1156. lcs_clear_thread_running_bit(card, LCS_SET_MC_THREAD);
  1157. return 0;
  1158. }
  1159. #endif /* CONFIG_IP_MULTICAST */
  1160. /**
  1161. * function called by net device to
  1162. * handle multicast address relevant things
  1163. */
  1164. static void
  1165. lcs_set_multicast_list(struct net_device *dev)
  1166. {
  1167. #ifdef CONFIG_IP_MULTICAST
  1168. struct lcs_card *card;
  1169. LCS_DBF_TEXT(4, trace, "setmulti");
  1170. card = (struct lcs_card *) dev->ml_priv;
  1171. if (!lcs_set_thread_start_bit(card, LCS_SET_MC_THREAD))
  1172. schedule_work(&card->kernel_thread_starter);
  1173. #endif /* CONFIG_IP_MULTICAST */
  1174. }
  1175. static long
  1176. lcs_check_irb_error(struct ccw_device *cdev, struct irb *irb)
  1177. {
  1178. if (!IS_ERR(irb))
  1179. return 0;
  1180. switch (PTR_ERR(irb)) {
  1181. case -EIO:
  1182. dev_warn(&cdev->dev,
  1183. "An I/O-error occurred on the LCS device\n");
  1184. LCS_DBF_TEXT(2, trace, "ckirberr");
  1185. LCS_DBF_TEXT_(2, trace, " rc%d", -EIO);
  1186. break;
  1187. case -ETIMEDOUT:
  1188. dev_warn(&cdev->dev,
  1189. "A command timed out on the LCS device\n");
  1190. LCS_DBF_TEXT(2, trace, "ckirberr");
  1191. LCS_DBF_TEXT_(2, trace, " rc%d", -ETIMEDOUT);
  1192. break;
  1193. default:
  1194. dev_warn(&cdev->dev,
  1195. "An error occurred on the LCS device, rc=%ld\n",
  1196. PTR_ERR(irb));
  1197. LCS_DBF_TEXT(2, trace, "ckirberr");
  1198. LCS_DBF_TEXT(2, trace, " rc???");
  1199. }
  1200. return PTR_ERR(irb);
  1201. }
  1202. static int
  1203. lcs_get_problem(struct ccw_device *cdev, struct irb *irb)
  1204. {
  1205. int dstat, cstat;
  1206. char *sense;
  1207. sense = (char *) irb->ecw;
  1208. cstat = irb->scsw.cmd.cstat;
  1209. dstat = irb->scsw.cmd.dstat;
  1210. if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
  1211. SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
  1212. SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
  1213. LCS_DBF_TEXT(2, trace, "CGENCHK");
  1214. return 1;
  1215. }
  1216. if (dstat & DEV_STAT_UNIT_CHECK) {
  1217. if (sense[LCS_SENSE_BYTE_1] &
  1218. LCS_SENSE_RESETTING_EVENT) {
  1219. LCS_DBF_TEXT(2, trace, "REVIND");
  1220. return 1;
  1221. }
  1222. if (sense[LCS_SENSE_BYTE_0] &
  1223. LCS_SENSE_CMD_REJECT) {
  1224. LCS_DBF_TEXT(2, trace, "CMDREJ");
  1225. return 0;
  1226. }
  1227. if ((!sense[LCS_SENSE_BYTE_0]) &&
  1228. (!sense[LCS_SENSE_BYTE_1]) &&
  1229. (!sense[LCS_SENSE_BYTE_2]) &&
  1230. (!sense[LCS_SENSE_BYTE_3])) {
  1231. LCS_DBF_TEXT(2, trace, "ZEROSEN");
  1232. return 0;
  1233. }
  1234. LCS_DBF_TEXT(2, trace, "DGENCHK");
  1235. return 1;
  1236. }
  1237. return 0;
  1238. }
  1239. static void
  1240. lcs_schedule_recovery(struct lcs_card *card)
  1241. {
  1242. LCS_DBF_TEXT(2, trace, "startrec");
  1243. if (!lcs_set_thread_start_bit(card, LCS_RECOVERY_THREAD))
  1244. schedule_work(&card->kernel_thread_starter);
  1245. }
  1246. /**
  1247. * IRQ Handler for LCS channels
  1248. */
  1249. static void
  1250. lcs_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  1251. {
  1252. struct lcs_card *card;
  1253. struct lcs_channel *channel;
  1254. int rc, index;
  1255. int cstat, dstat;
  1256. if (lcs_check_irb_error(cdev, irb))
  1257. return;
  1258. card = CARD_FROM_DEV(cdev);
  1259. if (card->read.ccwdev == cdev)
  1260. channel = &card->read;
  1261. else
  1262. channel = &card->write;
  1263. cstat = irb->scsw.cmd.cstat;
  1264. dstat = irb->scsw.cmd.dstat;
  1265. LCS_DBF_TEXT_(5, trace, "Rint%s", dev_name(&cdev->dev));
  1266. LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.cstat,
  1267. irb->scsw.cmd.dstat);
  1268. LCS_DBF_TEXT_(5, trace, "%4x%4x", irb->scsw.cmd.fctl,
  1269. irb->scsw.cmd.actl);
  1270. /* Check for channel and device errors presented */
  1271. rc = lcs_get_problem(cdev, irb);
  1272. if (rc || (dstat & DEV_STAT_UNIT_EXCEP)) {
  1273. dev_warn(&cdev->dev,
  1274. "The LCS device stopped because of an error,"
  1275. " dstat=0x%X, cstat=0x%X \n",
  1276. dstat, cstat);
  1277. if (rc) {
  1278. channel->state = LCS_CH_STATE_ERROR;
  1279. }
  1280. }
  1281. if (channel->state == LCS_CH_STATE_ERROR) {
  1282. lcs_schedule_recovery(card);
  1283. wake_up(&card->wait_q);
  1284. return;
  1285. }
  1286. /* How far in the ccw chain have we processed? */
  1287. if ((channel->state != LCS_CH_STATE_INIT) &&
  1288. (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) &&
  1289. (irb->scsw.cmd.cpa != 0)) {
  1290. index = (struct ccw1 *) __va((addr_t) irb->scsw.cmd.cpa)
  1291. - channel->ccws;
  1292. if ((irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED) ||
  1293. (irb->scsw.cmd.cstat & SCHN_STAT_PCI))
  1294. /* Bloody io subsystem tells us lies about cpa... */
  1295. index = (index - 1) & (LCS_NUM_BUFFS - 1);
  1296. while (channel->io_idx != index) {
  1297. __lcs_processed_buffer(channel,
  1298. channel->iob + channel->io_idx);
  1299. channel->io_idx =
  1300. (channel->io_idx + 1) & (LCS_NUM_BUFFS - 1);
  1301. }
  1302. }
  1303. if ((irb->scsw.cmd.dstat & DEV_STAT_DEV_END) ||
  1304. (irb->scsw.cmd.dstat & DEV_STAT_CHN_END) ||
  1305. (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK))
  1306. /* Mark channel as stopped. */
  1307. channel->state = LCS_CH_STATE_STOPPED;
  1308. else if (irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED)
  1309. /* CCW execution stopped on a suspend bit. */
  1310. channel->state = LCS_CH_STATE_SUSPENDED;
  1311. if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
  1312. if (irb->scsw.cmd.cc != 0) {
  1313. ccw_device_halt(channel->ccwdev, (addr_t) channel);
  1314. return;
  1315. }
  1316. /* The channel has been stopped by halt_IO. */
  1317. channel->state = LCS_CH_STATE_HALTED;
  1318. }
  1319. if (irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC)
  1320. channel->state = LCS_CH_STATE_CLEARED;
  1321. /* Do the rest in the tasklet. */
  1322. tasklet_schedule(&channel->irq_tasklet);
  1323. }
  1324. /**
  1325. * Tasklet for IRQ handler
  1326. */
  1327. static void
  1328. lcs_tasklet(unsigned long data)
  1329. {
  1330. unsigned long flags;
  1331. struct lcs_channel *channel;
  1332. struct lcs_buffer *iob;
  1333. int buf_idx;
  1334. channel = (struct lcs_channel *) data;
  1335. LCS_DBF_TEXT_(5, trace, "tlet%s", dev_name(&channel->ccwdev->dev));
  1336. /* Check for processed buffers. */
  1337. iob = channel->iob;
  1338. buf_idx = channel->buf_idx;
  1339. while (iob[buf_idx].state == LCS_BUF_STATE_PROCESSED) {
  1340. /* Do the callback thing. */
  1341. if (iob[buf_idx].callback != NULL)
  1342. iob[buf_idx].callback(channel, iob + buf_idx);
  1343. buf_idx = (buf_idx + 1) & (LCS_NUM_BUFFS - 1);
  1344. }
  1345. channel->buf_idx = buf_idx;
  1346. if (channel->state == LCS_CH_STATE_STOPPED)
  1347. lcs_start_channel(channel);
  1348. spin_lock_irqsave(get_ccwdev_lock(channel->ccwdev), flags);
  1349. if (channel->state == LCS_CH_STATE_SUSPENDED &&
  1350. channel->iob[channel->io_idx].state == LCS_BUF_STATE_READY)
  1351. __lcs_resume_channel(channel);
  1352. spin_unlock_irqrestore(get_ccwdev_lock(channel->ccwdev), flags);
  1353. /* Something happened on the channel. Wake up waiters. */
  1354. wake_up(&channel->wait_q);
  1355. }
  1356. /**
  1357. * Finish current tx buffer and make it ready for transmit.
  1358. */
  1359. static void
  1360. __lcs_emit_txbuffer(struct lcs_card *card)
  1361. {
  1362. LCS_DBF_TEXT(5, trace, "emittx");
  1363. *(__u16 *)(card->tx_buffer->data + card->tx_buffer->count) = 0;
  1364. card->tx_buffer->count += 2;
  1365. lcs_ready_buffer(&card->write, card->tx_buffer);
  1366. card->tx_buffer = NULL;
  1367. card->tx_emitted++;
  1368. }
  1369. /**
  1370. * Callback for finished tx buffers.
  1371. */
  1372. static void
  1373. lcs_txbuffer_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
  1374. {
  1375. struct lcs_card *card;
  1376. LCS_DBF_TEXT(5, trace, "txbuffcb");
  1377. /* Put buffer back to pool. */
  1378. lcs_release_buffer(channel, buffer);
  1379. card = container_of(channel, struct lcs_card, write);
  1380. if (netif_queue_stopped(card->dev) && netif_carrier_ok(card->dev))
  1381. netif_wake_queue(card->dev);
  1382. spin_lock(&card->lock);
  1383. card->tx_emitted--;
  1384. if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
  1385. /*
  1386. * Last running tx buffer has finished. Submit partially
  1387. * filled current buffer.
  1388. */
  1389. __lcs_emit_txbuffer(card);
  1390. spin_unlock(&card->lock);
  1391. }
  1392. /**
  1393. * Packet transmit function called by network stack
  1394. */
  1395. static int
  1396. __lcs_start_xmit(struct lcs_card *card, struct sk_buff *skb,
  1397. struct net_device *dev)
  1398. {
  1399. struct lcs_header *header;
  1400. int rc = NETDEV_TX_OK;
  1401. LCS_DBF_TEXT(5, trace, "hardxmit");
  1402. if (skb == NULL) {
  1403. card->stats.tx_dropped++;
  1404. card->stats.tx_errors++;
  1405. return NETDEV_TX_OK;
  1406. }
  1407. if (card->state != DEV_STATE_UP) {
  1408. dev_kfree_skb(skb);
  1409. card->stats.tx_dropped++;
  1410. card->stats.tx_errors++;
  1411. card->stats.tx_carrier_errors++;
  1412. return NETDEV_TX_OK;
  1413. }
  1414. if (skb->protocol == htons(ETH_P_IPV6)) {
  1415. dev_kfree_skb(skb);
  1416. return NETDEV_TX_OK;
  1417. }
  1418. netif_stop_queue(card->dev);
  1419. spin_lock(&card->lock);
  1420. if (card->tx_buffer != NULL &&
  1421. card->tx_buffer->count + sizeof(struct lcs_header) +
  1422. skb->len + sizeof(u16) > LCS_IOBUFFERSIZE)
  1423. /* skb too big for current tx buffer. */
  1424. __lcs_emit_txbuffer(card);
  1425. if (card->tx_buffer == NULL) {
  1426. /* Get new tx buffer */
  1427. card->tx_buffer = lcs_get_buffer(&card->write);
  1428. if (card->tx_buffer == NULL) {
  1429. card->stats.tx_dropped++;
  1430. rc = NETDEV_TX_BUSY;
  1431. goto out;
  1432. }
  1433. card->tx_buffer->callback = lcs_txbuffer_cb;
  1434. card->tx_buffer->count = 0;
  1435. }
  1436. header = (struct lcs_header *)
  1437. (card->tx_buffer->data + card->tx_buffer->count);
  1438. card->tx_buffer->count += skb->len + sizeof(struct lcs_header);
  1439. header->offset = card->tx_buffer->count;
  1440. header->type = card->lan_type;
  1441. header->slot = card->portno;
  1442. skb_copy_from_linear_data(skb, header + 1, skb->len);
  1443. spin_unlock(&card->lock);
  1444. card->stats.tx_bytes += skb->len;
  1445. card->stats.tx_packets++;
  1446. dev_kfree_skb(skb);
  1447. netif_wake_queue(card->dev);
  1448. spin_lock(&card->lock);
  1449. if (card->tx_emitted <= 0 && card->tx_buffer != NULL)
  1450. /* If this is the first tx buffer emit it immediately. */
  1451. __lcs_emit_txbuffer(card);
  1452. out:
  1453. spin_unlock(&card->lock);
  1454. return rc;
  1455. }
  1456. static int
  1457. lcs_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1458. {
  1459. struct lcs_card *card;
  1460. int rc;
  1461. LCS_DBF_TEXT(5, trace, "pktxmit");
  1462. card = (struct lcs_card *) dev->ml_priv;
  1463. rc = __lcs_start_xmit(card, skb, dev);
  1464. return rc;
  1465. }
  1466. /**
  1467. * send startlan and lanstat command to make LCS device ready
  1468. */
  1469. static int
  1470. lcs_startlan_auto(struct lcs_card *card)
  1471. {
  1472. int rc;
  1473. LCS_DBF_TEXT(2, trace, "strtauto");
  1474. #ifdef CONFIG_ETHERNET
  1475. card->lan_type = LCS_FRAME_TYPE_ENET;
  1476. rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
  1477. if (rc == 0)
  1478. return 0;
  1479. #endif
  1480. #ifdef CONFIG_FDDI
  1481. card->lan_type = LCS_FRAME_TYPE_FDDI;
  1482. rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
  1483. if (rc == 0)
  1484. return 0;
  1485. #endif
  1486. return -EIO;
  1487. }
  1488. static int
  1489. lcs_startlan(struct lcs_card *card)
  1490. {
  1491. int rc, i;
  1492. LCS_DBF_TEXT(2, trace, "startlan");
  1493. rc = 0;
  1494. if (card->portno != LCS_INVALID_PORT_NO) {
  1495. if (card->lan_type == LCS_FRAME_TYPE_AUTO)
  1496. rc = lcs_startlan_auto(card);
  1497. else
  1498. rc = lcs_send_startlan(card, LCS_INITIATOR_TCPIP);
  1499. } else {
  1500. for (i = 0; i <= 16; i++) {
  1501. card->portno = i;
  1502. if (card->lan_type != LCS_FRAME_TYPE_AUTO)
  1503. rc = lcs_send_startlan(card,
  1504. LCS_INITIATOR_TCPIP);
  1505. else
  1506. /* autodetecting lan type */
  1507. rc = lcs_startlan_auto(card);
  1508. if (rc == 0)
  1509. break;
  1510. }
  1511. }
  1512. if (rc == 0)
  1513. return lcs_send_lanstat(card);
  1514. return rc;
  1515. }
  1516. /**
  1517. * LCS detect function
  1518. * setup channels and make them I/O ready
  1519. */
  1520. static int
  1521. lcs_detect(struct lcs_card *card)
  1522. {
  1523. int rc = 0;
  1524. LCS_DBF_TEXT(2, setup, "lcsdetct");
  1525. /* start/reset card */
  1526. if (card->dev)
  1527. netif_stop_queue(card->dev);
  1528. rc = lcs_stop_channels(card);
  1529. if (rc == 0) {
  1530. rc = lcs_start_channels(card);
  1531. if (rc == 0) {
  1532. rc = lcs_send_startup(card, LCS_INITIATOR_TCPIP);
  1533. if (rc == 0)
  1534. rc = lcs_startlan(card);
  1535. }
  1536. }
  1537. if (rc == 0) {
  1538. card->state = DEV_STATE_UP;
  1539. } else {
  1540. card->state = DEV_STATE_DOWN;
  1541. card->write.state = LCS_CH_STATE_INIT;
  1542. card->read.state = LCS_CH_STATE_INIT;
  1543. }
  1544. return rc;
  1545. }
  1546. /**
  1547. * LCS Stop card
  1548. */
  1549. static int
  1550. lcs_stopcard(struct lcs_card *card)
  1551. {
  1552. int rc;
  1553. LCS_DBF_TEXT(3, setup, "stopcard");
  1554. if (card->read.state != LCS_CH_STATE_STOPPED &&
  1555. card->write.state != LCS_CH_STATE_STOPPED &&
  1556. card->read.state != LCS_CH_STATE_ERROR &&
  1557. card->write.state != LCS_CH_STATE_ERROR &&
  1558. card->state == DEV_STATE_UP) {
  1559. lcs_clear_multicast_list(card);
  1560. rc = lcs_send_stoplan(card,LCS_INITIATOR_TCPIP);
  1561. rc = lcs_send_shutdown(card);
  1562. }
  1563. rc = lcs_stop_channels(card);
  1564. card->state = DEV_STATE_DOWN;
  1565. return rc;
  1566. }
  1567. /**
  1568. * Kernel Thread helper functions for LGW initiated commands
  1569. */
  1570. static void
  1571. lcs_start_kernel_thread(struct work_struct *work)
  1572. {
  1573. struct lcs_card *card = container_of(work, struct lcs_card, kernel_thread_starter);
  1574. LCS_DBF_TEXT(5, trace, "krnthrd");
  1575. if (lcs_do_start_thread(card, LCS_RECOVERY_THREAD))
  1576. kthread_run(lcs_recovery, card, "lcs_recover");
  1577. #ifdef CONFIG_IP_MULTICAST
  1578. if (lcs_do_start_thread(card, LCS_SET_MC_THREAD))
  1579. kthread_run(lcs_register_mc_addresses, card, "regipm");
  1580. #endif
  1581. }
  1582. /**
  1583. * Process control frames.
  1584. */
  1585. static void
  1586. lcs_get_control(struct lcs_card *card, struct lcs_cmd *cmd)
  1587. {
  1588. LCS_DBF_TEXT(5, trace, "getctrl");
  1589. if (cmd->initiator == LCS_INITIATOR_LGW) {
  1590. switch(cmd->cmd_code) {
  1591. case LCS_CMD_STARTUP:
  1592. case LCS_CMD_STARTLAN:
  1593. lcs_schedule_recovery(card);
  1594. break;
  1595. case LCS_CMD_STOPLAN:
  1596. pr_warning("Stoplan for %s initiated by LGW.\n",
  1597. card->dev->name);
  1598. if (card->dev)
  1599. netif_carrier_off(card->dev);
  1600. break;
  1601. default:
  1602. LCS_DBF_TEXT(5, trace, "noLGWcmd");
  1603. break;
  1604. }
  1605. } else
  1606. lcs_notify_lancmd_waiters(card, cmd);
  1607. }
  1608. /**
  1609. * Unpack network packet.
  1610. */
  1611. static void
  1612. lcs_get_skb(struct lcs_card *card, char *skb_data, unsigned int skb_len)
  1613. {
  1614. struct sk_buff *skb;
  1615. LCS_DBF_TEXT(5, trace, "getskb");
  1616. if (card->dev == NULL ||
  1617. card->state != DEV_STATE_UP)
  1618. /* The card isn't up. Ignore the packet. */
  1619. return;
  1620. skb = dev_alloc_skb(skb_len);
  1621. if (skb == NULL) {
  1622. dev_err(&card->dev->dev,
  1623. " Allocating a socket buffer to interface %s failed\n",
  1624. card->dev->name);
  1625. card->stats.rx_dropped++;
  1626. return;
  1627. }
  1628. memcpy(skb_put(skb, skb_len), skb_data, skb_len);
  1629. skb->protocol = card->lan_type_trans(skb, card->dev);
  1630. card->stats.rx_bytes += skb_len;
  1631. card->stats.rx_packets++;
  1632. if (skb->protocol == htons(ETH_P_802_2))
  1633. *((__u32 *)skb->cb) = ++card->pkt_seq;
  1634. netif_rx(skb);
  1635. }
  1636. /**
  1637. * LCS main routine to get packets and lancmd replies from the buffers
  1638. */
  1639. static void
  1640. lcs_get_frames_cb(struct lcs_channel *channel, struct lcs_buffer *buffer)
  1641. {
  1642. struct lcs_card *card;
  1643. struct lcs_header *lcs_hdr;
  1644. __u16 offset;
  1645. LCS_DBF_TEXT(5, trace, "lcsgtpkt");
  1646. lcs_hdr = (struct lcs_header *) buffer->data;
  1647. if (lcs_hdr->offset == LCS_ILLEGAL_OFFSET) {
  1648. LCS_DBF_TEXT(4, trace, "-eiogpkt");
  1649. return;
  1650. }
  1651. card = container_of(channel, struct lcs_card, read);
  1652. offset = 0;
  1653. while (lcs_hdr->offset != 0) {
  1654. if (lcs_hdr->offset <= 0 ||
  1655. lcs_hdr->offset > LCS_IOBUFFERSIZE ||
  1656. lcs_hdr->offset < offset) {
  1657. /* Offset invalid. */
  1658. card->stats.rx_length_errors++;
  1659. card->stats.rx_errors++;
  1660. return;
  1661. }
  1662. /* What kind of frame is it? */
  1663. if (lcs_hdr->type == LCS_FRAME_TYPE_CONTROL)
  1664. /* Control frame. */
  1665. lcs_get_control(card, (struct lcs_cmd *) lcs_hdr);
  1666. else if (lcs_hdr->type == LCS_FRAME_TYPE_ENET ||
  1667. lcs_hdr->type == LCS_FRAME_TYPE_TR ||
  1668. lcs_hdr->type == LCS_FRAME_TYPE_FDDI)
  1669. /* Normal network packet. */
  1670. lcs_get_skb(card, (char *)(lcs_hdr + 1),
  1671. lcs_hdr->offset - offset -
  1672. sizeof(struct lcs_header));
  1673. else
  1674. /* Unknown frame type. */
  1675. ; // FIXME: error message ?
  1676. /* Proceed to next frame. */
  1677. offset = lcs_hdr->offset;
  1678. lcs_hdr->offset = LCS_ILLEGAL_OFFSET;
  1679. lcs_hdr = (struct lcs_header *) (buffer->data + offset);
  1680. }
  1681. /* The buffer is now empty. Make it ready again. */
  1682. lcs_ready_buffer(&card->read, buffer);
  1683. }
  1684. /**
  1685. * get network statistics for ifconfig and other user programs
  1686. */
  1687. static struct net_device_stats *
  1688. lcs_getstats(struct net_device *dev)
  1689. {
  1690. struct lcs_card *card;
  1691. LCS_DBF_TEXT(4, trace, "netstats");
  1692. card = (struct lcs_card *) dev->ml_priv;
  1693. return &card->stats;
  1694. }
  1695. /**
  1696. * stop lcs device
  1697. * This function will be called by user doing ifconfig xxx down
  1698. */
  1699. static int
  1700. lcs_stop_device(struct net_device *dev)
  1701. {
  1702. struct lcs_card *card;
  1703. int rc;
  1704. LCS_DBF_TEXT(2, trace, "stopdev");
  1705. card = (struct lcs_card *) dev->ml_priv;
  1706. netif_carrier_off(dev);
  1707. netif_tx_disable(dev);
  1708. dev->flags &= ~IFF_UP;
  1709. wait_event(card->write.wait_q,
  1710. (card->write.state != LCS_CH_STATE_RUNNING));
  1711. rc = lcs_stopcard(card);
  1712. if (rc)
  1713. dev_err(&card->dev->dev,
  1714. " Shutting down the LCS device failed\n ");
  1715. return rc;
  1716. }
  1717. /**
  1718. * start lcs device and make it runnable
  1719. * This function will be called by user doing ifconfig xxx up
  1720. */
  1721. static int
  1722. lcs_open_device(struct net_device *dev)
  1723. {
  1724. struct lcs_card *card;
  1725. int rc;
  1726. LCS_DBF_TEXT(2, trace, "opendev");
  1727. card = (struct lcs_card *) dev->ml_priv;
  1728. /* initialize statistics */
  1729. rc = lcs_detect(card);
  1730. if (rc) {
  1731. pr_err("Error in opening device!\n");
  1732. } else {
  1733. dev->flags |= IFF_UP;
  1734. netif_carrier_on(dev);
  1735. netif_wake_queue(dev);
  1736. card->state = DEV_STATE_UP;
  1737. }
  1738. return rc;
  1739. }
  1740. /**
  1741. * show function for portno called by cat or similar things
  1742. */
  1743. static ssize_t
  1744. lcs_portno_show (struct device *dev, struct device_attribute *attr, char *buf)
  1745. {
  1746. struct lcs_card *card;
  1747. card = dev_get_drvdata(dev);
  1748. if (!card)
  1749. return 0;
  1750. return sprintf(buf, "%d\n", card->portno);
  1751. }
  1752. /**
  1753. * store the value which is piped to file portno
  1754. */
  1755. static ssize_t
  1756. lcs_portno_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1757. {
  1758. struct lcs_card *card;
  1759. int value, rc;
  1760. card = dev_get_drvdata(dev);
  1761. if (!card)
  1762. return 0;
  1763. rc = sscanf(buf, "%d", &value);
  1764. if (rc != 1)
  1765. return -EINVAL;
  1766. /* TODO: sanity checks */
  1767. card->portno = value;
  1768. return count;
  1769. }
  1770. static DEVICE_ATTR(portno, 0644, lcs_portno_show, lcs_portno_store);
  1771. static const char *lcs_type[] = {
  1772. "not a channel",
  1773. "2216 parallel",
  1774. "2216 channel",
  1775. "OSA LCS card",
  1776. "unknown channel type",
  1777. "unsupported channel type",
  1778. };
  1779. static ssize_t
  1780. lcs_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  1781. {
  1782. struct ccwgroup_device *cgdev;
  1783. cgdev = to_ccwgroupdev(dev);
  1784. if (!cgdev)
  1785. return -ENODEV;
  1786. return sprintf(buf, "%s\n", lcs_type[cgdev->cdev[0]->id.driver_info]);
  1787. }
  1788. static DEVICE_ATTR(type, 0444, lcs_type_show, NULL);
  1789. static ssize_t
  1790. lcs_timeout_show(struct device *dev, struct device_attribute *attr, char *buf)
  1791. {
  1792. struct lcs_card *card;
  1793. card = dev_get_drvdata(dev);
  1794. return card ? sprintf(buf, "%u\n", card->lancmd_timeout) : 0;
  1795. }
  1796. static ssize_t
  1797. lcs_timeout_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1798. {
  1799. struct lcs_card *card;
  1800. unsigned int value;
  1801. int rc;
  1802. card = dev_get_drvdata(dev);
  1803. if (!card)
  1804. return 0;
  1805. rc = sscanf(buf, "%u", &value);
  1806. if (rc != 1)
  1807. return -EINVAL;
  1808. /* TODO: sanity checks */
  1809. card->lancmd_timeout = value;
  1810. return count;
  1811. }
  1812. static DEVICE_ATTR(lancmd_timeout, 0644, lcs_timeout_show, lcs_timeout_store);
  1813. static ssize_t
  1814. lcs_dev_recover_store(struct device *dev, struct device_attribute *attr,
  1815. const char *buf, size_t count)
  1816. {
  1817. struct lcs_card *card = dev_get_drvdata(dev);
  1818. char *tmp;
  1819. int i;
  1820. if (!card)
  1821. return -EINVAL;
  1822. if (card->state != DEV_STATE_UP)
  1823. return -EPERM;
  1824. i = simple_strtoul(buf, &tmp, 16);
  1825. if (i == 1)
  1826. lcs_schedule_recovery(card);
  1827. return count;
  1828. }
  1829. static DEVICE_ATTR(recover, 0200, NULL, lcs_dev_recover_store);
  1830. static struct attribute * lcs_attrs[] = {
  1831. &dev_attr_portno.attr,
  1832. &dev_attr_type.attr,
  1833. &dev_attr_lancmd_timeout.attr,
  1834. &dev_attr_recover.attr,
  1835. NULL,
  1836. };
  1837. static struct attribute_group lcs_attr_group = {
  1838. .attrs = lcs_attrs,
  1839. };
  1840. static const struct attribute_group *lcs_attr_groups[] = {
  1841. &lcs_attr_group,
  1842. NULL,
  1843. };
  1844. static const struct device_type lcs_devtype = {
  1845. .name = "lcs",
  1846. .groups = lcs_attr_groups,
  1847. };
  1848. /**
  1849. * lcs_probe_device is called on establishing a new ccwgroup_device.
  1850. */
  1851. static int
  1852. lcs_probe_device(struct ccwgroup_device *ccwgdev)
  1853. {
  1854. struct lcs_card *card;
  1855. if (!get_device(&ccwgdev->dev))
  1856. return -ENODEV;
  1857. LCS_DBF_TEXT(2, setup, "add_dev");
  1858. card = lcs_alloc_card();
  1859. if (!card) {
  1860. LCS_DBF_TEXT_(2, setup, " rc%d", -ENOMEM);
  1861. put_device(&ccwgdev->dev);
  1862. return -ENOMEM;
  1863. }
  1864. dev_set_drvdata(&ccwgdev->dev, card);
  1865. ccwgdev->cdev[0]->handler = lcs_irq;
  1866. ccwgdev->cdev[1]->handler = lcs_irq;
  1867. card->gdev = ccwgdev;
  1868. INIT_WORK(&card->kernel_thread_starter, lcs_start_kernel_thread);
  1869. card->thread_start_mask = 0;
  1870. card->thread_allowed_mask = 0;
  1871. card->thread_running_mask = 0;
  1872. ccwgdev->dev.type = &lcs_devtype;
  1873. return 0;
  1874. }
  1875. static int
  1876. lcs_register_netdev(struct ccwgroup_device *ccwgdev)
  1877. {
  1878. struct lcs_card *card;
  1879. LCS_DBF_TEXT(2, setup, "regnetdv");
  1880. card = dev_get_drvdata(&ccwgdev->dev);
  1881. if (card->dev->reg_state != NETREG_UNINITIALIZED)
  1882. return 0;
  1883. SET_NETDEV_DEV(card->dev, &ccwgdev->dev);
  1884. return register_netdev(card->dev);
  1885. }
  1886. /**
  1887. * lcs_new_device will be called by setting the group device online.
  1888. */
  1889. static const struct net_device_ops lcs_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. };
  1895. static const struct net_device_ops lcs_mc_netdev_ops = {
  1896. .ndo_open = lcs_open_device,
  1897. .ndo_stop = lcs_stop_device,
  1898. .ndo_get_stats = lcs_getstats,
  1899. .ndo_start_xmit = lcs_start_xmit,
  1900. .ndo_set_rx_mode = lcs_set_multicast_list,
  1901. };
  1902. static int
  1903. lcs_new_device(struct ccwgroup_device *ccwgdev)
  1904. {
  1905. struct lcs_card *card;
  1906. struct net_device *dev=NULL;
  1907. enum lcs_dev_states recover_state;
  1908. int rc;
  1909. card = dev_get_drvdata(&ccwgdev->dev);
  1910. if (!card)
  1911. return -ENODEV;
  1912. LCS_DBF_TEXT(2, setup, "newdev");
  1913. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  1914. card->read.ccwdev = ccwgdev->cdev[0];
  1915. card->write.ccwdev = ccwgdev->cdev[1];
  1916. recover_state = card->state;
  1917. rc = ccw_device_set_online(card->read.ccwdev);
  1918. if (rc)
  1919. goto out_err;
  1920. rc = ccw_device_set_online(card->write.ccwdev);
  1921. if (rc)
  1922. goto out_werr;
  1923. LCS_DBF_TEXT(3, setup, "lcsnewdv");
  1924. lcs_setup_card(card);
  1925. rc = lcs_detect(card);
  1926. if (rc) {
  1927. LCS_DBF_TEXT(2, setup, "dtctfail");
  1928. dev_err(&card->dev->dev,
  1929. "Detecting a network adapter for LCS devices"
  1930. " failed with rc=%d (0x%x)\n", rc, rc);
  1931. lcs_stopcard(card);
  1932. goto out;
  1933. }
  1934. if (card->dev) {
  1935. LCS_DBF_TEXT(2, setup, "samedev");
  1936. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  1937. goto netdev_out;
  1938. }
  1939. switch (card->lan_type) {
  1940. #ifdef CONFIG_ETHERNET
  1941. case LCS_FRAME_TYPE_ENET:
  1942. card->lan_type_trans = eth_type_trans;
  1943. dev = alloc_etherdev(0);
  1944. break;
  1945. #endif
  1946. #ifdef CONFIG_FDDI
  1947. case LCS_FRAME_TYPE_FDDI:
  1948. card->lan_type_trans = fddi_type_trans;
  1949. dev = alloc_fddidev(0);
  1950. break;
  1951. #endif
  1952. default:
  1953. LCS_DBF_TEXT(3, setup, "errinit");
  1954. pr_err(" Initialization failed\n");
  1955. goto out;
  1956. }
  1957. if (!dev)
  1958. goto out;
  1959. card->dev = dev;
  1960. card->dev->ml_priv = card;
  1961. card->dev->netdev_ops = &lcs_netdev_ops;
  1962. memcpy(card->dev->dev_addr, card->mac, LCS_MAC_LENGTH);
  1963. #ifdef CONFIG_IP_MULTICAST
  1964. if (!lcs_check_multicast_support(card))
  1965. card->dev->netdev_ops = &lcs_mc_netdev_ops;
  1966. #endif
  1967. netdev_out:
  1968. lcs_set_allowed_threads(card,0xffffffff);
  1969. if (recover_state == DEV_STATE_RECOVER) {
  1970. lcs_set_multicast_list(card->dev);
  1971. card->dev->flags |= IFF_UP;
  1972. netif_carrier_on(card->dev);
  1973. netif_wake_queue(card->dev);
  1974. card->state = DEV_STATE_UP;
  1975. } else {
  1976. lcs_stopcard(card);
  1977. }
  1978. if (lcs_register_netdev(ccwgdev) != 0)
  1979. goto out;
  1980. /* Print out supported assists: IPv6 */
  1981. pr_info("LCS device %s %s IPv6 support\n", card->dev->name,
  1982. (card->ip_assists_supported & LCS_IPASS_IPV6_SUPPORT) ?
  1983. "with" : "without");
  1984. /* Print out supported assist: Multicast */
  1985. pr_info("LCS device %s %s Multicast support\n", card->dev->name,
  1986. (card->ip_assists_supported & LCS_IPASS_MULTICAST_SUPPORT) ?
  1987. "with" : "without");
  1988. return 0;
  1989. out:
  1990. ccw_device_set_offline(card->write.ccwdev);
  1991. out_werr:
  1992. ccw_device_set_offline(card->read.ccwdev);
  1993. out_err:
  1994. return -ENODEV;
  1995. }
  1996. /**
  1997. * lcs_shutdown_device, called when setting the group device offline.
  1998. */
  1999. static int
  2000. __lcs_shutdown_device(struct ccwgroup_device *ccwgdev, int recovery_mode)
  2001. {
  2002. struct lcs_card *card;
  2003. enum lcs_dev_states recover_state;
  2004. int ret = 0, ret2 = 0, ret3 = 0;
  2005. LCS_DBF_TEXT(3, setup, "shtdndev");
  2006. card = dev_get_drvdata(&ccwgdev->dev);
  2007. if (!card)
  2008. return -ENODEV;
  2009. if (recovery_mode == 0) {
  2010. lcs_set_allowed_threads(card, 0);
  2011. if (lcs_wait_for_threads(card, LCS_SET_MC_THREAD))
  2012. return -ERESTARTSYS;
  2013. }
  2014. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  2015. recover_state = card->state;
  2016. ret = lcs_stop_device(card->dev);
  2017. ret2 = ccw_device_set_offline(card->read.ccwdev);
  2018. ret3 = ccw_device_set_offline(card->write.ccwdev);
  2019. if (!ret)
  2020. ret = (ret2) ? ret2 : ret3;
  2021. if (ret)
  2022. LCS_DBF_TEXT_(3, setup, "1err:%d", ret);
  2023. if (recover_state == DEV_STATE_UP) {
  2024. card->state = DEV_STATE_RECOVER;
  2025. }
  2026. return 0;
  2027. }
  2028. static int
  2029. lcs_shutdown_device(struct ccwgroup_device *ccwgdev)
  2030. {
  2031. return __lcs_shutdown_device(ccwgdev, 0);
  2032. }
  2033. /**
  2034. * drive lcs recovery after startup and startlan initiated by Lan Gateway
  2035. */
  2036. static int
  2037. lcs_recovery(void *ptr)
  2038. {
  2039. struct lcs_card *card;
  2040. struct ccwgroup_device *gdev;
  2041. int rc;
  2042. card = (struct lcs_card *) ptr;
  2043. LCS_DBF_TEXT(4, trace, "recover1");
  2044. if (!lcs_do_run_thread(card, LCS_RECOVERY_THREAD))
  2045. return 0;
  2046. LCS_DBF_TEXT(4, trace, "recover2");
  2047. gdev = card->gdev;
  2048. dev_warn(&gdev->dev,
  2049. "A recovery process has been started for the LCS device\n");
  2050. rc = __lcs_shutdown_device(gdev, 1);
  2051. rc = lcs_new_device(gdev);
  2052. if (!rc)
  2053. pr_info("Device %s successfully recovered!\n",
  2054. card->dev->name);
  2055. else
  2056. pr_info("Device %s could not be recovered!\n",
  2057. card->dev->name);
  2058. lcs_clear_thread_running_bit(card, LCS_RECOVERY_THREAD);
  2059. return 0;
  2060. }
  2061. /**
  2062. * lcs_remove_device, free buffers and card
  2063. */
  2064. static void
  2065. lcs_remove_device(struct ccwgroup_device *ccwgdev)
  2066. {
  2067. struct lcs_card *card;
  2068. card = dev_get_drvdata(&ccwgdev->dev);
  2069. if (!card)
  2070. return;
  2071. LCS_DBF_TEXT(3, setup, "remdev");
  2072. LCS_DBF_HEX(3, setup, &card, sizeof(void*));
  2073. if (ccwgdev->state == CCWGROUP_ONLINE) {
  2074. lcs_shutdown_device(ccwgdev);
  2075. }
  2076. if (card->dev)
  2077. unregister_netdev(card->dev);
  2078. lcs_cleanup_card(card);
  2079. lcs_free_card(card);
  2080. dev_set_drvdata(&ccwgdev->dev, NULL);
  2081. put_device(&ccwgdev->dev);
  2082. }
  2083. static int lcs_pm_suspend(struct lcs_card *card)
  2084. {
  2085. if (card->dev)
  2086. netif_device_detach(card->dev);
  2087. lcs_set_allowed_threads(card, 0);
  2088. lcs_wait_for_threads(card, 0xffffffff);
  2089. if (card->state != DEV_STATE_DOWN)
  2090. __lcs_shutdown_device(card->gdev, 1);
  2091. return 0;
  2092. }
  2093. static int lcs_pm_resume(struct lcs_card *card)
  2094. {
  2095. int rc = 0;
  2096. if (card->state == DEV_STATE_RECOVER)
  2097. rc = lcs_new_device(card->gdev);
  2098. if (card->dev)
  2099. netif_device_attach(card->dev);
  2100. if (rc) {
  2101. dev_warn(&card->gdev->dev, "The lcs device driver "
  2102. "failed to recover the device\n");
  2103. }
  2104. return rc;
  2105. }
  2106. static int lcs_prepare(struct ccwgroup_device *gdev)
  2107. {
  2108. return 0;
  2109. }
  2110. static void lcs_complete(struct ccwgroup_device *gdev)
  2111. {
  2112. return;
  2113. }
  2114. static int lcs_freeze(struct ccwgroup_device *gdev)
  2115. {
  2116. struct lcs_card *card = dev_get_drvdata(&gdev->dev);
  2117. return lcs_pm_suspend(card);
  2118. }
  2119. static int lcs_thaw(struct ccwgroup_device *gdev)
  2120. {
  2121. struct lcs_card *card = dev_get_drvdata(&gdev->dev);
  2122. return lcs_pm_resume(card);
  2123. }
  2124. static int lcs_restore(struct ccwgroup_device *gdev)
  2125. {
  2126. struct lcs_card *card = dev_get_drvdata(&gdev->dev);
  2127. return lcs_pm_resume(card);
  2128. }
  2129. static struct ccw_device_id lcs_ids[] = {
  2130. {CCW_DEVICE(0x3088, 0x08), .driver_info = lcs_channel_type_parallel},
  2131. {CCW_DEVICE(0x3088, 0x1f), .driver_info = lcs_channel_type_2216},
  2132. {CCW_DEVICE(0x3088, 0x60), .driver_info = lcs_channel_type_osa2},
  2133. {},
  2134. };
  2135. MODULE_DEVICE_TABLE(ccw, lcs_ids);
  2136. static struct ccw_driver lcs_ccw_driver = {
  2137. .driver = {
  2138. .owner = THIS_MODULE,
  2139. .name = "lcs",
  2140. },
  2141. .ids = lcs_ids,
  2142. .probe = ccwgroup_probe_ccwdev,
  2143. .remove = ccwgroup_remove_ccwdev,
  2144. .int_class = IRQIO_LCS,
  2145. };
  2146. /**
  2147. * LCS ccwgroup driver registration
  2148. */
  2149. static struct ccwgroup_driver lcs_group_driver = {
  2150. .driver = {
  2151. .owner = THIS_MODULE,
  2152. .name = "lcs",
  2153. },
  2154. .setup = lcs_probe_device,
  2155. .remove = lcs_remove_device,
  2156. .set_online = lcs_new_device,
  2157. .set_offline = lcs_shutdown_device,
  2158. .prepare = lcs_prepare,
  2159. .complete = lcs_complete,
  2160. .freeze = lcs_freeze,
  2161. .thaw = lcs_thaw,
  2162. .restore = lcs_restore,
  2163. };
  2164. static ssize_t lcs_driver_group_store(struct device_driver *ddrv,
  2165. const char *buf, size_t count)
  2166. {
  2167. int err;
  2168. err = ccwgroup_create_dev(lcs_root_dev, &lcs_group_driver, 2, buf);
  2169. return err ? err : count;
  2170. }
  2171. static DRIVER_ATTR(group, 0200, NULL, lcs_driver_group_store);
  2172. static struct attribute *lcs_drv_attrs[] = {
  2173. &driver_attr_group.attr,
  2174. NULL,
  2175. };
  2176. static struct attribute_group lcs_drv_attr_group = {
  2177. .attrs = lcs_drv_attrs,
  2178. };
  2179. static const struct attribute_group *lcs_drv_attr_groups[] = {
  2180. &lcs_drv_attr_group,
  2181. NULL,
  2182. };
  2183. /**
  2184. * LCS Module/Kernel initialization function
  2185. */
  2186. static int
  2187. __init lcs_init_module(void)
  2188. {
  2189. int rc;
  2190. pr_info("Loading %s\n", version);
  2191. rc = lcs_register_debug_facility();
  2192. LCS_DBF_TEXT(0, setup, "lcsinit");
  2193. if (rc)
  2194. goto out_err;
  2195. lcs_root_dev = root_device_register("lcs");
  2196. rc = PTR_ERR_OR_ZERO(lcs_root_dev);
  2197. if (rc)
  2198. goto register_err;
  2199. rc = ccw_driver_register(&lcs_ccw_driver);
  2200. if (rc)
  2201. goto ccw_err;
  2202. lcs_group_driver.driver.groups = lcs_drv_attr_groups;
  2203. rc = ccwgroup_driver_register(&lcs_group_driver);
  2204. if (rc)
  2205. goto ccwgroup_err;
  2206. return 0;
  2207. ccwgroup_err:
  2208. ccw_driver_unregister(&lcs_ccw_driver);
  2209. ccw_err:
  2210. root_device_unregister(lcs_root_dev);
  2211. register_err:
  2212. lcs_unregister_debug_facility();
  2213. out_err:
  2214. pr_err("Initializing the lcs device driver failed\n");
  2215. return rc;
  2216. }
  2217. /**
  2218. * LCS module cleanup function
  2219. */
  2220. static void
  2221. __exit lcs_cleanup_module(void)
  2222. {
  2223. pr_info("Terminating lcs module.\n");
  2224. LCS_DBF_TEXT(0, trace, "cleanup");
  2225. ccwgroup_driver_unregister(&lcs_group_driver);
  2226. ccw_driver_unregister(&lcs_ccw_driver);
  2227. root_device_unregister(lcs_root_dev);
  2228. lcs_unregister_debug_facility();
  2229. }
  2230. module_init(lcs_init_module);
  2231. module_exit(lcs_cleanup_module);
  2232. MODULE_AUTHOR("Frank Pavlic <fpavlic@de.ibm.com>");
  2233. MODULE_LICENSE("GPL");