lcs.c 63 KB

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