lcs.c 61 KB

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