lcs.c 62 KB

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