lcs.c 62 KB

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