lcs.c 62 KB

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