ctcm_main.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /*
  2. * Copyright IBM Corp. 2001, 2009
  3. * Author(s):
  4. * Original CTC driver(s):
  5. * Fritz Elfert (felfert@millenux.com)
  6. * Dieter Wellerdiek (wel@de.ibm.com)
  7. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  8. * Denis Joseph Barrow (barrow_dj@yahoo.com)
  9. * Jochen Roehrig (roehrig@de.ibm.com)
  10. * Cornelia Huck <cornelia.huck@de.ibm.com>
  11. * MPC additions:
  12. * Belinda Thompson (belindat@us.ibm.com)
  13. * Andy Richter (richtera@us.ibm.com)
  14. * Revived by:
  15. * Peter Tiedemann (ptiedem@de.ibm.com)
  16. */
  17. #undef DEBUG
  18. #undef DEBUGDATA
  19. #undef DEBUGCCW
  20. #define KMSG_COMPONENT "ctcm"
  21. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/errno.h>
  27. #include <linux/types.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/timer.h>
  30. #include <linux/bitops.h>
  31. #include <linux/signal.h>
  32. #include <linux/string.h>
  33. #include <linux/ip.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/tcp.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/ctype.h>
  38. #include <net/dst.h>
  39. #include <linux/io.h>
  40. #include <asm/ccwdev.h>
  41. #include <asm/ccwgroup.h>
  42. #include <linux/uaccess.h>
  43. #include <asm/idals.h>
  44. #include "ctcm_fsms.h"
  45. #include "ctcm_main.h"
  46. /* Some common global variables */
  47. /**
  48. * The root device for ctcm group devices
  49. */
  50. static struct device *ctcm_root_dev;
  51. /*
  52. * Linked list of all detected channels.
  53. */
  54. struct channel *channels;
  55. /**
  56. * Unpack a just received skb and hand it over to
  57. * upper layers.
  58. *
  59. * ch The channel where this skb has been received.
  60. * pskb The received skb.
  61. */
  62. void ctcm_unpack_skb(struct channel *ch, struct sk_buff *pskb)
  63. {
  64. struct net_device *dev = ch->netdev;
  65. struct ctcm_priv *priv = dev->ml_priv;
  66. __u16 len = *((__u16 *) pskb->data);
  67. skb_put(pskb, 2 + LL_HEADER_LENGTH);
  68. skb_pull(pskb, 2);
  69. pskb->dev = dev;
  70. pskb->ip_summed = CHECKSUM_UNNECESSARY;
  71. while (len > 0) {
  72. struct sk_buff *skb;
  73. int skblen;
  74. struct ll_header *header = (struct ll_header *)pskb->data;
  75. skb_pull(pskb, LL_HEADER_LENGTH);
  76. if ((ch->protocol == CTCM_PROTO_S390) &&
  77. (header->type != ETH_P_IP)) {
  78. if (!(ch->logflags & LOG_FLAG_ILLEGALPKT)) {
  79. ch->logflags |= LOG_FLAG_ILLEGALPKT;
  80. /*
  81. * Check packet type only if we stick strictly
  82. * to S/390's protocol of OS390. This only
  83. * supports IP. Otherwise allow any packet
  84. * type.
  85. */
  86. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  87. "%s(%s): Illegal packet type 0x%04x"
  88. " - dropping",
  89. CTCM_FUNTAIL, dev->name, header->type);
  90. }
  91. priv->stats.rx_dropped++;
  92. priv->stats.rx_frame_errors++;
  93. return;
  94. }
  95. pskb->protocol = ntohs(header->type);
  96. if ((header->length <= LL_HEADER_LENGTH) ||
  97. (len <= LL_HEADER_LENGTH)) {
  98. if (!(ch->logflags & LOG_FLAG_ILLEGALSIZE)) {
  99. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  100. "%s(%s): Illegal packet size %d(%d,%d)"
  101. "- dropping",
  102. CTCM_FUNTAIL, dev->name,
  103. header->length, dev->mtu, len);
  104. ch->logflags |= LOG_FLAG_ILLEGALSIZE;
  105. }
  106. priv->stats.rx_dropped++;
  107. priv->stats.rx_length_errors++;
  108. return;
  109. }
  110. header->length -= LL_HEADER_LENGTH;
  111. len -= LL_HEADER_LENGTH;
  112. if ((header->length > skb_tailroom(pskb)) ||
  113. (header->length > len)) {
  114. if (!(ch->logflags & LOG_FLAG_OVERRUN)) {
  115. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  116. "%s(%s): Packet size %d (overrun)"
  117. " - dropping", CTCM_FUNTAIL,
  118. dev->name, header->length);
  119. ch->logflags |= LOG_FLAG_OVERRUN;
  120. }
  121. priv->stats.rx_dropped++;
  122. priv->stats.rx_length_errors++;
  123. return;
  124. }
  125. skb_put(pskb, header->length);
  126. skb_reset_mac_header(pskb);
  127. len -= header->length;
  128. skb = dev_alloc_skb(pskb->len);
  129. if (!skb) {
  130. if (!(ch->logflags & LOG_FLAG_NOMEM)) {
  131. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  132. "%s(%s): MEMORY allocation error",
  133. CTCM_FUNTAIL, dev->name);
  134. ch->logflags |= LOG_FLAG_NOMEM;
  135. }
  136. priv->stats.rx_dropped++;
  137. return;
  138. }
  139. skb_copy_from_linear_data(pskb, skb_put(skb, pskb->len),
  140. pskb->len);
  141. skb_reset_mac_header(skb);
  142. skb->dev = pskb->dev;
  143. skb->protocol = pskb->protocol;
  144. pskb->ip_summed = CHECKSUM_UNNECESSARY;
  145. skblen = skb->len;
  146. /*
  147. * reset logflags
  148. */
  149. ch->logflags = 0;
  150. priv->stats.rx_packets++;
  151. priv->stats.rx_bytes += skblen;
  152. netif_rx_ni(skb);
  153. if (len > 0) {
  154. skb_pull(pskb, header->length);
  155. if (skb_tailroom(pskb) < LL_HEADER_LENGTH) {
  156. CTCM_DBF_DEV_NAME(TRACE, dev,
  157. "Overrun in ctcm_unpack_skb");
  158. ch->logflags |= LOG_FLAG_OVERRUN;
  159. return;
  160. }
  161. skb_put(pskb, LL_HEADER_LENGTH);
  162. }
  163. }
  164. }
  165. /**
  166. * Release a specific channel in the channel list.
  167. *
  168. * ch Pointer to channel struct to be released.
  169. */
  170. static void channel_free(struct channel *ch)
  171. {
  172. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s)", CTCM_FUNTAIL, ch->id);
  173. ch->flags &= ~CHANNEL_FLAGS_INUSE;
  174. fsm_newstate(ch->fsm, CTC_STATE_IDLE);
  175. }
  176. /**
  177. * Remove a specific channel in the channel list.
  178. *
  179. * ch Pointer to channel struct to be released.
  180. */
  181. static void channel_remove(struct channel *ch)
  182. {
  183. struct channel **c = &channels;
  184. char chid[CTCM_ID_SIZE+1];
  185. int ok = 0;
  186. if (ch == NULL)
  187. return;
  188. else
  189. strncpy(chid, ch->id, CTCM_ID_SIZE);
  190. channel_free(ch);
  191. while (*c) {
  192. if (*c == ch) {
  193. *c = ch->next;
  194. fsm_deltimer(&ch->timer);
  195. if (IS_MPC(ch))
  196. fsm_deltimer(&ch->sweep_timer);
  197. kfree_fsm(ch->fsm);
  198. clear_normalized_cda(&ch->ccw[4]);
  199. if (ch->trans_skb != NULL) {
  200. clear_normalized_cda(&ch->ccw[1]);
  201. dev_kfree_skb_any(ch->trans_skb);
  202. }
  203. if (IS_MPC(ch)) {
  204. tasklet_kill(&ch->ch_tasklet);
  205. tasklet_kill(&ch->ch_disc_tasklet);
  206. kfree(ch->discontact_th);
  207. }
  208. kfree(ch->ccw);
  209. kfree(ch->irb);
  210. kfree(ch);
  211. ok = 1;
  212. break;
  213. }
  214. c = &((*c)->next);
  215. }
  216. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO, "%s(%s) %s", CTCM_FUNTAIL,
  217. chid, ok ? "OK" : "failed");
  218. }
  219. /**
  220. * Get a specific channel from the channel list.
  221. *
  222. * type Type of channel we are interested in.
  223. * id Id of channel we are interested in.
  224. * direction Direction we want to use this channel for.
  225. *
  226. * returns Pointer to a channel or NULL if no matching channel available.
  227. */
  228. static struct channel *channel_get(enum ctcm_channel_types type,
  229. char *id, int direction)
  230. {
  231. struct channel *ch = channels;
  232. while (ch && (strncmp(ch->id, id, CTCM_ID_SIZE) || (ch->type != type)))
  233. ch = ch->next;
  234. if (!ch) {
  235. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  236. "%s(%d, %s, %d) not found in channel list\n",
  237. CTCM_FUNTAIL, type, id, direction);
  238. } else {
  239. if (ch->flags & CHANNEL_FLAGS_INUSE)
  240. ch = NULL;
  241. else {
  242. ch->flags |= CHANNEL_FLAGS_INUSE;
  243. ch->flags &= ~CHANNEL_FLAGS_RWMASK;
  244. ch->flags |= (direction == CTCM_WRITE)
  245. ? CHANNEL_FLAGS_WRITE : CHANNEL_FLAGS_READ;
  246. fsm_newstate(ch->fsm, CTC_STATE_STOPPED);
  247. }
  248. }
  249. return ch;
  250. }
  251. static long ctcm_check_irb_error(struct ccw_device *cdev, struct irb *irb)
  252. {
  253. if (!IS_ERR(irb))
  254. return 0;
  255. CTCM_DBF_TEXT_(ERROR, CTC_DBF_WARN,
  256. "irb error %ld on device %s\n",
  257. PTR_ERR(irb), dev_name(&cdev->dev));
  258. switch (PTR_ERR(irb)) {
  259. case -EIO:
  260. dev_err(&cdev->dev,
  261. "An I/O-error occurred on the CTCM device\n");
  262. break;
  263. case -ETIMEDOUT:
  264. dev_err(&cdev->dev,
  265. "An adapter hardware operation timed out\n");
  266. break;
  267. default:
  268. dev_err(&cdev->dev,
  269. "An error occurred on the adapter hardware\n");
  270. }
  271. return PTR_ERR(irb);
  272. }
  273. /**
  274. * Check sense of a unit check.
  275. *
  276. * ch The channel, the sense code belongs to.
  277. * sense The sense code to inspect.
  278. */
  279. static inline void ccw_unit_check(struct channel *ch, __u8 sense)
  280. {
  281. CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG,
  282. "%s(%s): %02x",
  283. CTCM_FUNTAIL, ch->id, sense);
  284. if (sense & SNS0_INTERVENTION_REQ) {
  285. if (sense & 0x01) {
  286. if (ch->sense_rc != 0x01) {
  287. pr_notice(
  288. "%s: The communication peer has "
  289. "disconnected\n", ch->id);
  290. ch->sense_rc = 0x01;
  291. }
  292. fsm_event(ch->fsm, CTC_EVENT_UC_RCRESET, ch);
  293. } else {
  294. if (ch->sense_rc != SNS0_INTERVENTION_REQ) {
  295. pr_notice(
  296. "%s: The remote operating system is "
  297. "not available\n", ch->id);
  298. ch->sense_rc = SNS0_INTERVENTION_REQ;
  299. }
  300. fsm_event(ch->fsm, CTC_EVENT_UC_RSRESET, ch);
  301. }
  302. } else if (sense & SNS0_EQUIPMENT_CHECK) {
  303. if (sense & SNS0_BUS_OUT_CHECK) {
  304. if (ch->sense_rc != SNS0_BUS_OUT_CHECK) {
  305. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  306. "%s(%s): remote HW error %02x",
  307. CTCM_FUNTAIL, ch->id, sense);
  308. ch->sense_rc = SNS0_BUS_OUT_CHECK;
  309. }
  310. fsm_event(ch->fsm, CTC_EVENT_UC_HWFAIL, ch);
  311. } else {
  312. if (ch->sense_rc != SNS0_EQUIPMENT_CHECK) {
  313. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  314. "%s(%s): remote read parity error %02x",
  315. CTCM_FUNTAIL, ch->id, sense);
  316. ch->sense_rc = SNS0_EQUIPMENT_CHECK;
  317. }
  318. fsm_event(ch->fsm, CTC_EVENT_UC_RXPARITY, ch);
  319. }
  320. } else if (sense & SNS0_BUS_OUT_CHECK) {
  321. if (ch->sense_rc != SNS0_BUS_OUT_CHECK) {
  322. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  323. "%s(%s): BUS OUT error %02x",
  324. CTCM_FUNTAIL, ch->id, sense);
  325. ch->sense_rc = SNS0_BUS_OUT_CHECK;
  326. }
  327. if (sense & 0x04) /* data-streaming timeout */
  328. fsm_event(ch->fsm, CTC_EVENT_UC_TXTIMEOUT, ch);
  329. else /* Data-transfer parity error */
  330. fsm_event(ch->fsm, CTC_EVENT_UC_TXPARITY, ch);
  331. } else if (sense & SNS0_CMD_REJECT) {
  332. if (ch->sense_rc != SNS0_CMD_REJECT) {
  333. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  334. "%s(%s): Command rejected",
  335. CTCM_FUNTAIL, ch->id);
  336. ch->sense_rc = SNS0_CMD_REJECT;
  337. }
  338. } else if (sense == 0) {
  339. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  340. "%s(%s): Unit check ZERO",
  341. CTCM_FUNTAIL, ch->id);
  342. fsm_event(ch->fsm, CTC_EVENT_UC_ZERO, ch);
  343. } else {
  344. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  345. "%s(%s): Unit check code %02x unknown",
  346. CTCM_FUNTAIL, ch->id, sense);
  347. fsm_event(ch->fsm, CTC_EVENT_UC_UNKNOWN, ch);
  348. }
  349. }
  350. int ctcm_ch_alloc_buffer(struct channel *ch)
  351. {
  352. clear_normalized_cda(&ch->ccw[1]);
  353. ch->trans_skb = __dev_alloc_skb(ch->max_bufsize, GFP_ATOMIC | GFP_DMA);
  354. if (ch->trans_skb == NULL) {
  355. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  356. "%s(%s): %s trans_skb allocation error",
  357. CTCM_FUNTAIL, ch->id,
  358. (CHANNEL_DIRECTION(ch->flags) == CTCM_READ) ?
  359. "RX" : "TX");
  360. return -ENOMEM;
  361. }
  362. ch->ccw[1].count = ch->max_bufsize;
  363. if (set_normalized_cda(&ch->ccw[1], ch->trans_skb->data)) {
  364. dev_kfree_skb(ch->trans_skb);
  365. ch->trans_skb = NULL;
  366. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  367. "%s(%s): %s set norm_cda failed",
  368. CTCM_FUNTAIL, ch->id,
  369. (CHANNEL_DIRECTION(ch->flags) == CTCM_READ) ?
  370. "RX" : "TX");
  371. return -ENOMEM;
  372. }
  373. ch->ccw[1].count = 0;
  374. ch->trans_skb_data = ch->trans_skb->data;
  375. ch->flags &= ~CHANNEL_FLAGS_BUFSIZE_CHANGED;
  376. return 0;
  377. }
  378. /*
  379. * Interface API for upper network layers
  380. */
  381. /**
  382. * Open an interface.
  383. * Called from generic network layer when ifconfig up is run.
  384. *
  385. * dev Pointer to interface struct.
  386. *
  387. * returns 0 on success, -ERRNO on failure. (Never fails.)
  388. */
  389. int ctcm_open(struct net_device *dev)
  390. {
  391. struct ctcm_priv *priv = dev->ml_priv;
  392. CTCMY_DBF_DEV_NAME(SETUP, dev, "");
  393. if (!IS_MPC(priv))
  394. fsm_event(priv->fsm, DEV_EVENT_START, dev);
  395. return 0;
  396. }
  397. /**
  398. * Close an interface.
  399. * Called from generic network layer when ifconfig down is run.
  400. *
  401. * dev Pointer to interface struct.
  402. *
  403. * returns 0 on success, -ERRNO on failure. (Never fails.)
  404. */
  405. int ctcm_close(struct net_device *dev)
  406. {
  407. struct ctcm_priv *priv = dev->ml_priv;
  408. CTCMY_DBF_DEV_NAME(SETUP, dev, "");
  409. if (!IS_MPC(priv))
  410. fsm_event(priv->fsm, DEV_EVENT_STOP, dev);
  411. return 0;
  412. }
  413. /**
  414. * Transmit a packet.
  415. * This is a helper function for ctcm_tx().
  416. *
  417. * ch Channel to be used for sending.
  418. * skb Pointer to struct sk_buff of packet to send.
  419. * The linklevel header has already been set up
  420. * by ctcm_tx().
  421. *
  422. * returns 0 on success, -ERRNO on failure. (Never fails.)
  423. */
  424. static int ctcm_transmit_skb(struct channel *ch, struct sk_buff *skb)
  425. {
  426. unsigned long saveflags;
  427. struct ll_header header;
  428. int rc = 0;
  429. __u16 block_len;
  430. int ccw_idx;
  431. struct sk_buff *nskb;
  432. unsigned long hi;
  433. /* we need to acquire the lock for testing the state
  434. * otherwise we can have an IRQ changing the state to
  435. * TXIDLE after the test but before acquiring the lock.
  436. */
  437. spin_lock_irqsave(&ch->collect_lock, saveflags);
  438. if (fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) {
  439. int l = skb->len + LL_HEADER_LENGTH;
  440. if (ch->collect_len + l > ch->max_bufsize - 2) {
  441. spin_unlock_irqrestore(&ch->collect_lock, saveflags);
  442. return -EBUSY;
  443. } else {
  444. atomic_inc(&skb->users);
  445. header.length = l;
  446. header.type = skb->protocol;
  447. header.unused = 0;
  448. memcpy(skb_push(skb, LL_HEADER_LENGTH), &header,
  449. LL_HEADER_LENGTH);
  450. skb_queue_tail(&ch->collect_queue, skb);
  451. ch->collect_len += l;
  452. }
  453. spin_unlock_irqrestore(&ch->collect_lock, saveflags);
  454. goto done;
  455. }
  456. spin_unlock_irqrestore(&ch->collect_lock, saveflags);
  457. /*
  458. * Protect skb against beeing free'd by upper
  459. * layers.
  460. */
  461. atomic_inc(&skb->users);
  462. ch->prof.txlen += skb->len;
  463. header.length = skb->len + LL_HEADER_LENGTH;
  464. header.type = skb->protocol;
  465. header.unused = 0;
  466. memcpy(skb_push(skb, LL_HEADER_LENGTH), &header, LL_HEADER_LENGTH);
  467. block_len = skb->len + 2;
  468. *((__u16 *)skb_push(skb, 2)) = block_len;
  469. /*
  470. * IDAL support in CTCM is broken, so we have to
  471. * care about skb's above 2G ourselves.
  472. */
  473. hi = ((unsigned long)skb_tail_pointer(skb) + LL_HEADER_LENGTH) >> 31;
  474. if (hi) {
  475. nskb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
  476. if (!nskb) {
  477. atomic_dec(&skb->users);
  478. skb_pull(skb, LL_HEADER_LENGTH + 2);
  479. ctcm_clear_busy(ch->netdev);
  480. return -ENOMEM;
  481. } else {
  482. memcpy(skb_put(nskb, skb->len), skb->data, skb->len);
  483. atomic_inc(&nskb->users);
  484. atomic_dec(&skb->users);
  485. dev_kfree_skb_irq(skb);
  486. skb = nskb;
  487. }
  488. }
  489. ch->ccw[4].count = block_len;
  490. if (set_normalized_cda(&ch->ccw[4], skb->data)) {
  491. /*
  492. * idal allocation failed, try via copying to
  493. * trans_skb. trans_skb usually has a pre-allocated
  494. * idal.
  495. */
  496. if (ctcm_checkalloc_buffer(ch)) {
  497. /*
  498. * Remove our header. It gets added
  499. * again on retransmit.
  500. */
  501. atomic_dec(&skb->users);
  502. skb_pull(skb, LL_HEADER_LENGTH + 2);
  503. ctcm_clear_busy(ch->netdev);
  504. return -ENOMEM;
  505. }
  506. skb_reset_tail_pointer(ch->trans_skb);
  507. ch->trans_skb->len = 0;
  508. ch->ccw[1].count = skb->len;
  509. skb_copy_from_linear_data(skb,
  510. skb_put(ch->trans_skb, skb->len), skb->len);
  511. atomic_dec(&skb->users);
  512. dev_kfree_skb_irq(skb);
  513. ccw_idx = 0;
  514. } else {
  515. skb_queue_tail(&ch->io_queue, skb);
  516. ccw_idx = 3;
  517. }
  518. if (do_debug_ccw)
  519. ctcmpc_dumpit((char *)&ch->ccw[ccw_idx],
  520. sizeof(struct ccw1) * 3);
  521. ch->retry = 0;
  522. fsm_newstate(ch->fsm, CTC_STATE_TX);
  523. fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch);
  524. spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
  525. ch->prof.send_stamp = jiffies;
  526. rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
  527. (unsigned long)ch, 0xff, 0);
  528. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
  529. if (ccw_idx == 3)
  530. ch->prof.doios_single++;
  531. if (rc != 0) {
  532. fsm_deltimer(&ch->timer);
  533. ctcm_ccw_check_rc(ch, rc, "single skb TX");
  534. if (ccw_idx == 3)
  535. skb_dequeue_tail(&ch->io_queue);
  536. /*
  537. * Remove our header. It gets added
  538. * again on retransmit.
  539. */
  540. skb_pull(skb, LL_HEADER_LENGTH + 2);
  541. } else if (ccw_idx == 0) {
  542. struct net_device *dev = ch->netdev;
  543. struct ctcm_priv *priv = dev->ml_priv;
  544. priv->stats.tx_packets++;
  545. priv->stats.tx_bytes += skb->len - LL_HEADER_LENGTH;
  546. }
  547. done:
  548. ctcm_clear_busy(ch->netdev);
  549. return rc;
  550. }
  551. static void ctcmpc_send_sweep_req(struct channel *rch)
  552. {
  553. struct net_device *dev = rch->netdev;
  554. struct ctcm_priv *priv;
  555. struct mpc_group *grp;
  556. struct th_sweep *header;
  557. struct sk_buff *sweep_skb;
  558. struct channel *ch;
  559. /* int rc = 0; */
  560. priv = dev->ml_priv;
  561. grp = priv->mpcg;
  562. ch = priv->channel[CTCM_WRITE];
  563. /* sweep processing is not complete until response and request */
  564. /* has completed for all read channels in group */
  565. if (grp->in_sweep == 0) {
  566. grp->in_sweep = 1;
  567. grp->sweep_rsp_pend_num = grp->active_channels[CTCM_READ];
  568. grp->sweep_req_pend_num = grp->active_channels[CTCM_READ];
  569. }
  570. sweep_skb = __dev_alloc_skb(MPC_BUFSIZE_DEFAULT, GFP_ATOMIC|GFP_DMA);
  571. if (sweep_skb == NULL) {
  572. /* rc = -ENOMEM; */
  573. goto nomem;
  574. }
  575. header = kmalloc(TH_SWEEP_LENGTH, gfp_type());
  576. if (!header) {
  577. dev_kfree_skb_any(sweep_skb);
  578. /* rc = -ENOMEM; */
  579. goto nomem;
  580. }
  581. header->th.th_seg = 0x00 ;
  582. header->th.th_ch_flag = TH_SWEEP_REQ; /* 0x0f */
  583. header->th.th_blk_flag = 0x00;
  584. header->th.th_is_xid = 0x00;
  585. header->th.th_seq_num = 0x00;
  586. header->sw.th_last_seq = ch->th_seq_num;
  587. memcpy(skb_put(sweep_skb, TH_SWEEP_LENGTH), header, TH_SWEEP_LENGTH);
  588. kfree(header);
  589. dev->trans_start = jiffies;
  590. skb_queue_tail(&ch->sweep_queue, sweep_skb);
  591. fsm_addtimer(&ch->sweep_timer, 100, CTC_EVENT_RSWEEP_TIMER, ch);
  592. return;
  593. nomem:
  594. grp->in_sweep = 0;
  595. ctcm_clear_busy(dev);
  596. fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
  597. return;
  598. }
  599. /*
  600. * MPC mode version of transmit_skb
  601. */
  602. static int ctcmpc_transmit_skb(struct channel *ch, struct sk_buff *skb)
  603. {
  604. struct pdu *p_header;
  605. struct net_device *dev = ch->netdev;
  606. struct ctcm_priv *priv = dev->ml_priv;
  607. struct mpc_group *grp = priv->mpcg;
  608. struct th_header *header;
  609. struct sk_buff *nskb;
  610. int rc = 0;
  611. int ccw_idx;
  612. unsigned long hi;
  613. unsigned long saveflags = 0; /* avoids compiler warning */
  614. CTCM_PR_DEBUG("Enter %s: %s, cp=%i ch=0x%p id=%s state=%s\n",
  615. __func__, dev->name, smp_processor_id(), ch,
  616. ch->id, fsm_getstate_str(ch->fsm));
  617. if ((fsm_getstate(ch->fsm) != CTC_STATE_TXIDLE) || grp->in_sweep) {
  618. spin_lock_irqsave(&ch->collect_lock, saveflags);
  619. atomic_inc(&skb->users);
  620. p_header = kmalloc(PDU_HEADER_LENGTH, gfp_type());
  621. if (!p_header) {
  622. spin_unlock_irqrestore(&ch->collect_lock, saveflags);
  623. goto nomem_exit;
  624. }
  625. p_header->pdu_offset = skb->len;
  626. p_header->pdu_proto = 0x01;
  627. p_header->pdu_flag = 0x00;
  628. if (skb->protocol == ntohs(ETH_P_SNAP)) {
  629. p_header->pdu_flag |= PDU_FIRST | PDU_CNTL;
  630. } else {
  631. p_header->pdu_flag |= PDU_FIRST;
  632. }
  633. p_header->pdu_seq = 0;
  634. memcpy(skb_push(skb, PDU_HEADER_LENGTH), p_header,
  635. PDU_HEADER_LENGTH);
  636. CTCM_PR_DEBUG("%s(%s): Put on collect_q - skb len: %04x \n"
  637. "pdu header and data for up to 32 bytes:\n",
  638. __func__, dev->name, skb->len);
  639. CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
  640. skb_queue_tail(&ch->collect_queue, skb);
  641. ch->collect_len += skb->len;
  642. kfree(p_header);
  643. spin_unlock_irqrestore(&ch->collect_lock, saveflags);
  644. goto done;
  645. }
  646. /*
  647. * Protect skb against beeing free'd by upper
  648. * layers.
  649. */
  650. atomic_inc(&skb->users);
  651. /*
  652. * IDAL support in CTCM is broken, so we have to
  653. * care about skb's above 2G ourselves.
  654. */
  655. hi = ((unsigned long)skb->tail + TH_HEADER_LENGTH) >> 31;
  656. if (hi) {
  657. nskb = __dev_alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
  658. if (!nskb) {
  659. goto nomem_exit;
  660. } else {
  661. memcpy(skb_put(nskb, skb->len), skb->data, skb->len);
  662. atomic_inc(&nskb->users);
  663. atomic_dec(&skb->users);
  664. dev_kfree_skb_irq(skb);
  665. skb = nskb;
  666. }
  667. }
  668. p_header = kmalloc(PDU_HEADER_LENGTH, gfp_type());
  669. if (!p_header)
  670. goto nomem_exit;
  671. p_header->pdu_offset = skb->len;
  672. p_header->pdu_proto = 0x01;
  673. p_header->pdu_flag = 0x00;
  674. p_header->pdu_seq = 0;
  675. if (skb->protocol == ntohs(ETH_P_SNAP)) {
  676. p_header->pdu_flag |= PDU_FIRST | PDU_CNTL;
  677. } else {
  678. p_header->pdu_flag |= PDU_FIRST;
  679. }
  680. memcpy(skb_push(skb, PDU_HEADER_LENGTH), p_header, PDU_HEADER_LENGTH);
  681. kfree(p_header);
  682. if (ch->collect_len > 0) {
  683. spin_lock_irqsave(&ch->collect_lock, saveflags);
  684. skb_queue_tail(&ch->collect_queue, skb);
  685. ch->collect_len += skb->len;
  686. skb = skb_dequeue(&ch->collect_queue);
  687. ch->collect_len -= skb->len;
  688. spin_unlock_irqrestore(&ch->collect_lock, saveflags);
  689. }
  690. p_header = (struct pdu *)skb->data;
  691. p_header->pdu_flag |= PDU_LAST;
  692. ch->prof.txlen += skb->len - PDU_HEADER_LENGTH;
  693. header = kmalloc(TH_HEADER_LENGTH, gfp_type());
  694. if (!header)
  695. goto nomem_exit;
  696. header->th_seg = 0x00;
  697. header->th_ch_flag = TH_HAS_PDU; /* Normal data */
  698. header->th_blk_flag = 0x00;
  699. header->th_is_xid = 0x00; /* Just data here */
  700. ch->th_seq_num++;
  701. header->th_seq_num = ch->th_seq_num;
  702. CTCM_PR_DBGDATA("%s(%s) ToVTAM_th_seq= %08x\n" ,
  703. __func__, dev->name, ch->th_seq_num);
  704. /* put the TH on the packet */
  705. memcpy(skb_push(skb, TH_HEADER_LENGTH), header, TH_HEADER_LENGTH);
  706. kfree(header);
  707. CTCM_PR_DBGDATA("%s(%s): skb len: %04x\n - pdu header and data for "
  708. "up to 32 bytes sent to vtam:\n",
  709. __func__, dev->name, skb->len);
  710. CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
  711. ch->ccw[4].count = skb->len;
  712. if (set_normalized_cda(&ch->ccw[4], skb->data)) {
  713. /*
  714. * idal allocation failed, try via copying to trans_skb.
  715. * trans_skb usually has a pre-allocated idal.
  716. */
  717. if (ctcm_checkalloc_buffer(ch)) {
  718. /*
  719. * Remove our header.
  720. * It gets added again on retransmit.
  721. */
  722. goto nomem_exit;
  723. }
  724. skb_reset_tail_pointer(ch->trans_skb);
  725. ch->trans_skb->len = 0;
  726. ch->ccw[1].count = skb->len;
  727. memcpy(skb_put(ch->trans_skb, skb->len), skb->data, skb->len);
  728. atomic_dec(&skb->users);
  729. dev_kfree_skb_irq(skb);
  730. ccw_idx = 0;
  731. CTCM_PR_DBGDATA("%s(%s): trans_skb len: %04x\n"
  732. "up to 32 bytes sent to vtam:\n",
  733. __func__, dev->name, ch->trans_skb->len);
  734. CTCM_D3_DUMP((char *)ch->trans_skb->data,
  735. min_t(int, 32, ch->trans_skb->len));
  736. } else {
  737. skb_queue_tail(&ch->io_queue, skb);
  738. ccw_idx = 3;
  739. }
  740. ch->retry = 0;
  741. fsm_newstate(ch->fsm, CTC_STATE_TX);
  742. fsm_addtimer(&ch->timer, CTCM_TIME_5_SEC, CTC_EVENT_TIMER, ch);
  743. if (do_debug_ccw)
  744. ctcmpc_dumpit((char *)&ch->ccw[ccw_idx],
  745. sizeof(struct ccw1) * 3);
  746. spin_lock_irqsave(get_ccwdev_lock(ch->cdev), saveflags);
  747. ch->prof.send_stamp = jiffies;
  748. rc = ccw_device_start(ch->cdev, &ch->ccw[ccw_idx],
  749. (unsigned long)ch, 0xff, 0);
  750. spin_unlock_irqrestore(get_ccwdev_lock(ch->cdev), saveflags);
  751. if (ccw_idx == 3)
  752. ch->prof.doios_single++;
  753. if (rc != 0) {
  754. fsm_deltimer(&ch->timer);
  755. ctcm_ccw_check_rc(ch, rc, "single skb TX");
  756. if (ccw_idx == 3)
  757. skb_dequeue_tail(&ch->io_queue);
  758. } else if (ccw_idx == 0) {
  759. priv->stats.tx_packets++;
  760. priv->stats.tx_bytes += skb->len - TH_HEADER_LENGTH;
  761. }
  762. if (ch->th_seq_num > 0xf0000000) /* Chose at random. */
  763. ctcmpc_send_sweep_req(ch);
  764. goto done;
  765. nomem_exit:
  766. CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_CRIT,
  767. "%s(%s): MEMORY allocation ERROR\n",
  768. CTCM_FUNTAIL, ch->id);
  769. rc = -ENOMEM;
  770. atomic_dec(&skb->users);
  771. dev_kfree_skb_any(skb);
  772. fsm_event(priv->mpcg->fsm, MPCG_EVENT_INOP, dev);
  773. done:
  774. CTCM_PR_DEBUG("Exit %s(%s)\n", __func__, dev->name);
  775. return rc;
  776. }
  777. /**
  778. * Start transmission of a packet.
  779. * Called from generic network device layer.
  780. *
  781. * skb Pointer to buffer containing the packet.
  782. * dev Pointer to interface struct.
  783. *
  784. * returns 0 if packet consumed, !0 if packet rejected.
  785. * Note: If we return !0, then the packet is free'd by
  786. * the generic network layer.
  787. */
  788. /* first merge version - leaving both functions separated */
  789. static int ctcm_tx(struct sk_buff *skb, struct net_device *dev)
  790. {
  791. struct ctcm_priv *priv = dev->ml_priv;
  792. if (skb == NULL) {
  793. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  794. "%s(%s): NULL sk_buff passed",
  795. CTCM_FUNTAIL, dev->name);
  796. priv->stats.tx_dropped++;
  797. return NETDEV_TX_OK;
  798. }
  799. if (skb_headroom(skb) < (LL_HEADER_LENGTH + 2)) {
  800. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  801. "%s(%s): Got sk_buff with head room < %ld bytes",
  802. CTCM_FUNTAIL, dev->name, LL_HEADER_LENGTH + 2);
  803. dev_kfree_skb(skb);
  804. priv->stats.tx_dropped++;
  805. return NETDEV_TX_OK;
  806. }
  807. /*
  808. * If channels are not running, try to restart them
  809. * and throw away packet.
  810. */
  811. if (fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) {
  812. fsm_event(priv->fsm, DEV_EVENT_START, dev);
  813. dev_kfree_skb(skb);
  814. priv->stats.tx_dropped++;
  815. priv->stats.tx_errors++;
  816. priv->stats.tx_carrier_errors++;
  817. return NETDEV_TX_OK;
  818. }
  819. if (ctcm_test_and_set_busy(dev))
  820. return NETDEV_TX_BUSY;
  821. dev->trans_start = jiffies;
  822. if (ctcm_transmit_skb(priv->channel[CTCM_WRITE], skb) != 0)
  823. return NETDEV_TX_BUSY;
  824. return NETDEV_TX_OK;
  825. }
  826. /* unmerged MPC variant of ctcm_tx */
  827. static int ctcmpc_tx(struct sk_buff *skb, struct net_device *dev)
  828. {
  829. int len = 0;
  830. struct ctcm_priv *priv = dev->ml_priv;
  831. struct mpc_group *grp = priv->mpcg;
  832. struct sk_buff *newskb = NULL;
  833. /*
  834. * Some sanity checks ...
  835. */
  836. if (skb == NULL) {
  837. CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
  838. "%s(%s): NULL sk_buff passed",
  839. CTCM_FUNTAIL, dev->name);
  840. priv->stats.tx_dropped++;
  841. goto done;
  842. }
  843. if (skb_headroom(skb) < (TH_HEADER_LENGTH + PDU_HEADER_LENGTH)) {
  844. CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR,
  845. "%s(%s): Got sk_buff with head room < %ld bytes",
  846. CTCM_FUNTAIL, dev->name,
  847. TH_HEADER_LENGTH + PDU_HEADER_LENGTH);
  848. CTCM_D3_DUMP((char *)skb->data, min_t(int, 32, skb->len));
  849. len = skb->len + TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
  850. newskb = __dev_alloc_skb(len, gfp_type() | GFP_DMA);
  851. if (!newskb) {
  852. CTCM_DBF_TEXT_(MPC_TRACE, CTC_DBF_ERROR,
  853. "%s: %s: __dev_alloc_skb failed",
  854. __func__, dev->name);
  855. dev_kfree_skb_any(skb);
  856. priv->stats.tx_dropped++;
  857. priv->stats.tx_errors++;
  858. priv->stats.tx_carrier_errors++;
  859. fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
  860. goto done;
  861. }
  862. newskb->protocol = skb->protocol;
  863. skb_reserve(newskb, TH_HEADER_LENGTH + PDU_HEADER_LENGTH);
  864. memcpy(skb_put(newskb, skb->len), skb->data, skb->len);
  865. dev_kfree_skb_any(skb);
  866. skb = newskb;
  867. }
  868. /*
  869. * If channels are not running,
  870. * notify anybody about a link failure and throw
  871. * away packet.
  872. */
  873. if ((fsm_getstate(priv->fsm) != DEV_STATE_RUNNING) ||
  874. (fsm_getstate(grp->fsm) < MPCG_STATE_XID2INITW)) {
  875. dev_kfree_skb_any(skb);
  876. CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
  877. "%s(%s): inactive MPCGROUP - dropped",
  878. CTCM_FUNTAIL, dev->name);
  879. priv->stats.tx_dropped++;
  880. priv->stats.tx_errors++;
  881. priv->stats.tx_carrier_errors++;
  882. goto done;
  883. }
  884. if (ctcm_test_and_set_busy(dev)) {
  885. CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
  886. "%s(%s): device busy - dropped",
  887. CTCM_FUNTAIL, dev->name);
  888. dev_kfree_skb_any(skb);
  889. priv->stats.tx_dropped++;
  890. priv->stats.tx_errors++;
  891. priv->stats.tx_carrier_errors++;
  892. fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
  893. goto done;
  894. }
  895. dev->trans_start = jiffies;
  896. if (ctcmpc_transmit_skb(priv->channel[CTCM_WRITE], skb) != 0) {
  897. CTCM_DBF_TEXT_(MPC_ERROR, CTC_DBF_ERROR,
  898. "%s(%s): device error - dropped",
  899. CTCM_FUNTAIL, dev->name);
  900. dev_kfree_skb_any(skb);
  901. priv->stats.tx_dropped++;
  902. priv->stats.tx_errors++;
  903. priv->stats.tx_carrier_errors++;
  904. ctcm_clear_busy(dev);
  905. fsm_event(grp->fsm, MPCG_EVENT_INOP, dev);
  906. goto done;
  907. }
  908. ctcm_clear_busy(dev);
  909. done:
  910. if (do_debug)
  911. MPC_DBF_DEV_NAME(TRACE, dev, "exit");
  912. return NETDEV_TX_OK; /* handle freeing of skb here */
  913. }
  914. /**
  915. * Sets MTU of an interface.
  916. *
  917. * dev Pointer to interface struct.
  918. * new_mtu The new MTU to use for this interface.
  919. *
  920. * returns 0 on success, -EINVAL if MTU is out of valid range.
  921. * (valid range is 576 .. 65527). If VM is on the
  922. * remote side, maximum MTU is 32760, however this is
  923. * not checked here.
  924. */
  925. static int ctcm_change_mtu(struct net_device *dev, int new_mtu)
  926. {
  927. struct ctcm_priv *priv;
  928. int max_bufsize;
  929. if (new_mtu < 576 || new_mtu > 65527)
  930. return -EINVAL;
  931. priv = dev->ml_priv;
  932. max_bufsize = priv->channel[CTCM_READ]->max_bufsize;
  933. if (IS_MPC(priv)) {
  934. if (new_mtu > max_bufsize - TH_HEADER_LENGTH)
  935. return -EINVAL;
  936. dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
  937. } else {
  938. if (new_mtu > max_bufsize - LL_HEADER_LENGTH - 2)
  939. return -EINVAL;
  940. dev->hard_header_len = LL_HEADER_LENGTH + 2;
  941. }
  942. dev->mtu = new_mtu;
  943. return 0;
  944. }
  945. /**
  946. * Returns interface statistics of a device.
  947. *
  948. * dev Pointer to interface struct.
  949. *
  950. * returns Pointer to stats struct of this interface.
  951. */
  952. static struct net_device_stats *ctcm_stats(struct net_device *dev)
  953. {
  954. return &((struct ctcm_priv *)dev->ml_priv)->stats;
  955. }
  956. static void ctcm_free_netdevice(struct net_device *dev)
  957. {
  958. struct ctcm_priv *priv;
  959. struct mpc_group *grp;
  960. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
  961. "%s(%s)", CTCM_FUNTAIL, dev->name);
  962. priv = dev->ml_priv;
  963. if (priv) {
  964. grp = priv->mpcg;
  965. if (grp) {
  966. if (grp->fsm)
  967. kfree_fsm(grp->fsm);
  968. if (grp->xid_skb)
  969. dev_kfree_skb(grp->xid_skb);
  970. if (grp->rcvd_xid_skb)
  971. dev_kfree_skb(grp->rcvd_xid_skb);
  972. tasklet_kill(&grp->mpc_tasklet2);
  973. kfree(grp);
  974. priv->mpcg = NULL;
  975. }
  976. if (priv->fsm) {
  977. kfree_fsm(priv->fsm);
  978. priv->fsm = NULL;
  979. }
  980. kfree(priv->xid);
  981. priv->xid = NULL;
  982. /*
  983. * Note: kfree(priv); is done in "opposite" function of
  984. * allocator function probe_device which is remove_device.
  985. */
  986. }
  987. #ifdef MODULE
  988. free_netdev(dev);
  989. #endif
  990. }
  991. struct mpc_group *ctcmpc_init_mpc_group(struct ctcm_priv *priv);
  992. static const struct net_device_ops ctcm_netdev_ops = {
  993. .ndo_open = ctcm_open,
  994. .ndo_stop = ctcm_close,
  995. .ndo_get_stats = ctcm_stats,
  996. .ndo_change_mtu = ctcm_change_mtu,
  997. .ndo_start_xmit = ctcm_tx,
  998. };
  999. static const struct net_device_ops ctcm_mpc_netdev_ops = {
  1000. .ndo_open = ctcm_open,
  1001. .ndo_stop = ctcm_close,
  1002. .ndo_get_stats = ctcm_stats,
  1003. .ndo_change_mtu = ctcm_change_mtu,
  1004. .ndo_start_xmit = ctcmpc_tx,
  1005. };
  1006. void static ctcm_dev_setup(struct net_device *dev)
  1007. {
  1008. dev->type = ARPHRD_SLIP;
  1009. dev->tx_queue_len = 100;
  1010. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  1011. }
  1012. /*
  1013. * Initialize everything of the net device except the name and the
  1014. * channel structs.
  1015. */
  1016. static struct net_device *ctcm_init_netdevice(struct ctcm_priv *priv)
  1017. {
  1018. struct net_device *dev;
  1019. struct mpc_group *grp;
  1020. if (!priv)
  1021. return NULL;
  1022. if (IS_MPC(priv))
  1023. dev = alloc_netdev(0, MPC_DEVICE_GENE, NET_NAME_UNKNOWN,
  1024. ctcm_dev_setup);
  1025. else
  1026. dev = alloc_netdev(0, CTC_DEVICE_GENE, NET_NAME_UNKNOWN,
  1027. ctcm_dev_setup);
  1028. if (!dev) {
  1029. CTCM_DBF_TEXT_(ERROR, CTC_DBF_CRIT,
  1030. "%s: MEMORY allocation ERROR",
  1031. CTCM_FUNTAIL);
  1032. return NULL;
  1033. }
  1034. dev->ml_priv = priv;
  1035. priv->fsm = init_fsm("ctcmdev", dev_state_names, dev_event_names,
  1036. CTCM_NR_DEV_STATES, CTCM_NR_DEV_EVENTS,
  1037. dev_fsm, dev_fsm_len, GFP_KERNEL);
  1038. if (priv->fsm == NULL) {
  1039. CTCMY_DBF_DEV(SETUP, dev, "init_fsm error");
  1040. free_netdev(dev);
  1041. return NULL;
  1042. }
  1043. fsm_newstate(priv->fsm, DEV_STATE_STOPPED);
  1044. fsm_settimer(priv->fsm, &priv->restart_timer);
  1045. if (IS_MPC(priv)) {
  1046. /* MPC Group Initializations */
  1047. grp = ctcmpc_init_mpc_group(priv);
  1048. if (grp == NULL) {
  1049. MPC_DBF_DEV(SETUP, dev, "init_mpc_group error");
  1050. free_netdev(dev);
  1051. return NULL;
  1052. }
  1053. tasklet_init(&grp->mpc_tasklet2,
  1054. mpc_group_ready, (unsigned long)dev);
  1055. dev->mtu = MPC_BUFSIZE_DEFAULT -
  1056. TH_HEADER_LENGTH - PDU_HEADER_LENGTH;
  1057. dev->netdev_ops = &ctcm_mpc_netdev_ops;
  1058. dev->hard_header_len = TH_HEADER_LENGTH + PDU_HEADER_LENGTH;
  1059. priv->buffer_size = MPC_BUFSIZE_DEFAULT;
  1060. } else {
  1061. dev->mtu = CTCM_BUFSIZE_DEFAULT - LL_HEADER_LENGTH - 2;
  1062. dev->netdev_ops = &ctcm_netdev_ops;
  1063. dev->hard_header_len = LL_HEADER_LENGTH + 2;
  1064. }
  1065. CTCMY_DBF_DEV(SETUP, dev, "finished");
  1066. return dev;
  1067. }
  1068. /**
  1069. * Main IRQ handler.
  1070. *
  1071. * cdev The ccw_device the interrupt is for.
  1072. * intparm interruption parameter.
  1073. * irb interruption response block.
  1074. */
  1075. static void ctcm_irq_handler(struct ccw_device *cdev,
  1076. unsigned long intparm, struct irb *irb)
  1077. {
  1078. struct channel *ch;
  1079. struct net_device *dev;
  1080. struct ctcm_priv *priv;
  1081. struct ccwgroup_device *cgdev;
  1082. int cstat;
  1083. int dstat;
  1084. CTCM_DBF_TEXT_(TRACE, CTC_DBF_DEBUG,
  1085. "Enter %s(%s)", CTCM_FUNTAIL, dev_name(&cdev->dev));
  1086. if (ctcm_check_irb_error(cdev, irb))
  1087. return;
  1088. cgdev = dev_get_drvdata(&cdev->dev);
  1089. cstat = irb->scsw.cmd.cstat;
  1090. dstat = irb->scsw.cmd.dstat;
  1091. /* Check for unsolicited interrupts. */
  1092. if (cgdev == NULL) {
  1093. CTCM_DBF_TEXT_(TRACE, CTC_DBF_ERROR,
  1094. "%s(%s) unsolicited irq: c-%02x d-%02x\n",
  1095. CTCM_FUNTAIL, dev_name(&cdev->dev), cstat, dstat);
  1096. dev_warn(&cdev->dev,
  1097. "The adapter received a non-specific IRQ\n");
  1098. return;
  1099. }
  1100. priv = dev_get_drvdata(&cgdev->dev);
  1101. /* Try to extract channel from driver data. */
  1102. if (priv->channel[CTCM_READ]->cdev == cdev)
  1103. ch = priv->channel[CTCM_READ];
  1104. else if (priv->channel[CTCM_WRITE]->cdev == cdev)
  1105. ch = priv->channel[CTCM_WRITE];
  1106. else {
  1107. dev_err(&cdev->dev,
  1108. "%s: Internal error: Can't determine channel for "
  1109. "interrupt device %s\n",
  1110. __func__, dev_name(&cdev->dev));
  1111. /* Explain: inconsistent internal structures */
  1112. return;
  1113. }
  1114. dev = ch->netdev;
  1115. if (dev == NULL) {
  1116. dev_err(&cdev->dev,
  1117. "%s Internal error: net_device is NULL, ch = 0x%p\n",
  1118. __func__, ch);
  1119. /* Explain: inconsistent internal structures */
  1120. return;
  1121. }
  1122. /* Copy interruption response block. */
  1123. memcpy(ch->irb, irb, sizeof(struct irb));
  1124. /* Issue error message and return on subchannel error code */
  1125. if (irb->scsw.cmd.cstat) {
  1126. fsm_event(ch->fsm, CTC_EVENT_SC_UNKNOWN, ch);
  1127. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  1128. "%s(%s): sub-ch check %s: cs=%02x ds=%02x",
  1129. CTCM_FUNTAIL, dev->name, ch->id, cstat, dstat);
  1130. dev_warn(&cdev->dev,
  1131. "A check occurred on the subchannel\n");
  1132. return;
  1133. }
  1134. /* Check the reason-code of a unit check */
  1135. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  1136. if ((irb->ecw[0] & ch->sense_rc) == 0)
  1137. /* print it only once */
  1138. CTCM_DBF_TEXT_(TRACE, CTC_DBF_WARN,
  1139. "%s(%s): sense=%02x, ds=%02x",
  1140. CTCM_FUNTAIL, ch->id, irb->ecw[0], dstat);
  1141. ccw_unit_check(ch, irb->ecw[0]);
  1142. return;
  1143. }
  1144. if (irb->scsw.cmd.dstat & DEV_STAT_BUSY) {
  1145. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION)
  1146. fsm_event(ch->fsm, CTC_EVENT_ATTNBUSY, ch);
  1147. else
  1148. fsm_event(ch->fsm, CTC_EVENT_BUSY, ch);
  1149. return;
  1150. }
  1151. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  1152. fsm_event(ch->fsm, CTC_EVENT_ATTN, ch);
  1153. return;
  1154. }
  1155. if ((irb->scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS) ||
  1156. (irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND) ||
  1157. (irb->scsw.cmd.stctl ==
  1158. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND)))
  1159. fsm_event(ch->fsm, CTC_EVENT_FINSTAT, ch);
  1160. else
  1161. fsm_event(ch->fsm, CTC_EVENT_IRQ, ch);
  1162. }
  1163. static const struct device_type ctcm_devtype = {
  1164. .name = "ctcm",
  1165. .groups = ctcm_attr_groups,
  1166. };
  1167. /**
  1168. * Add ctcm specific attributes.
  1169. * Add ctcm private data.
  1170. *
  1171. * cgdev pointer to ccwgroup_device just added
  1172. *
  1173. * returns 0 on success, !0 on failure.
  1174. */
  1175. static int ctcm_probe_device(struct ccwgroup_device *cgdev)
  1176. {
  1177. struct ctcm_priv *priv;
  1178. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
  1179. "%s %p",
  1180. __func__, cgdev);
  1181. if (!get_device(&cgdev->dev))
  1182. return -ENODEV;
  1183. priv = kzalloc(sizeof(struct ctcm_priv), GFP_KERNEL);
  1184. if (!priv) {
  1185. CTCM_DBF_TEXT_(ERROR, CTC_DBF_ERROR,
  1186. "%s: memory allocation failure",
  1187. CTCM_FUNTAIL);
  1188. put_device(&cgdev->dev);
  1189. return -ENOMEM;
  1190. }
  1191. priv->buffer_size = CTCM_BUFSIZE_DEFAULT;
  1192. cgdev->cdev[0]->handler = ctcm_irq_handler;
  1193. cgdev->cdev[1]->handler = ctcm_irq_handler;
  1194. dev_set_drvdata(&cgdev->dev, priv);
  1195. cgdev->dev.type = &ctcm_devtype;
  1196. return 0;
  1197. }
  1198. /**
  1199. * Add a new channel to the list of channels.
  1200. * Keeps the channel list sorted.
  1201. *
  1202. * cdev The ccw_device to be added.
  1203. * type The type class of the new channel.
  1204. * priv Points to the private data of the ccwgroup_device.
  1205. *
  1206. * returns 0 on success, !0 on error.
  1207. */
  1208. static int add_channel(struct ccw_device *cdev, enum ctcm_channel_types type,
  1209. struct ctcm_priv *priv)
  1210. {
  1211. struct channel **c = &channels;
  1212. struct channel *ch;
  1213. int ccw_num;
  1214. int rc = 0;
  1215. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
  1216. "%s(%s), type %d, proto %d",
  1217. __func__, dev_name(&cdev->dev), type, priv->protocol);
  1218. ch = kzalloc(sizeof(struct channel), GFP_KERNEL);
  1219. if (ch == NULL)
  1220. return -ENOMEM;
  1221. ch->protocol = priv->protocol;
  1222. if (IS_MPC(priv)) {
  1223. ch->discontact_th = kzalloc(TH_HEADER_LENGTH, gfp_type());
  1224. if (ch->discontact_th == NULL)
  1225. goto nomem_return;
  1226. ch->discontact_th->th_blk_flag = TH_DISCONTACT;
  1227. tasklet_init(&ch->ch_disc_tasklet,
  1228. mpc_action_send_discontact, (unsigned long)ch);
  1229. tasklet_init(&ch->ch_tasklet, ctcmpc_bh, (unsigned long)ch);
  1230. ch->max_bufsize = (MPC_BUFSIZE_DEFAULT - 35);
  1231. ccw_num = 17;
  1232. } else
  1233. ccw_num = 8;
  1234. ch->ccw = kzalloc(ccw_num * sizeof(struct ccw1), GFP_KERNEL | GFP_DMA);
  1235. if (ch->ccw == NULL)
  1236. goto nomem_return;
  1237. ch->cdev = cdev;
  1238. snprintf(ch->id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev->dev));
  1239. ch->type = type;
  1240. /**
  1241. * "static" ccws are used in the following way:
  1242. *
  1243. * ccw[0..2] (Channel program for generic I/O):
  1244. * 0: prepare
  1245. * 1: read or write (depending on direction) with fixed
  1246. * buffer (idal allocated once when buffer is allocated)
  1247. * 2: nop
  1248. * ccw[3..5] (Channel program for direct write of packets)
  1249. * 3: prepare
  1250. * 4: write (idal allocated on every write).
  1251. * 5: nop
  1252. * ccw[6..7] (Channel program for initial channel setup):
  1253. * 6: set extended mode
  1254. * 7: nop
  1255. *
  1256. * ch->ccw[0..5] are initialized in ch_action_start because
  1257. * the channel's direction is yet unknown here.
  1258. *
  1259. * ccws used for xid2 negotiations
  1260. * ch-ccw[8-14] need to be used for the XID exchange either
  1261. * X side XID2 Processing
  1262. * 8: write control
  1263. * 9: write th
  1264. * 10: write XID
  1265. * 11: read th from secondary
  1266. * 12: read XID from secondary
  1267. * 13: read 4 byte ID
  1268. * 14: nop
  1269. * Y side XID Processing
  1270. * 8: sense
  1271. * 9: read th
  1272. * 10: read XID
  1273. * 11: write th
  1274. * 12: write XID
  1275. * 13: write 4 byte ID
  1276. * 14: nop
  1277. *
  1278. * ccws used for double noop due to VM timing issues
  1279. * which result in unrecoverable Busy on channel
  1280. * 15: nop
  1281. * 16: nop
  1282. */
  1283. ch->ccw[6].cmd_code = CCW_CMD_SET_EXTENDED;
  1284. ch->ccw[6].flags = CCW_FLAG_SLI;
  1285. ch->ccw[7].cmd_code = CCW_CMD_NOOP;
  1286. ch->ccw[7].flags = CCW_FLAG_SLI;
  1287. if (IS_MPC(priv)) {
  1288. ch->ccw[15].cmd_code = CCW_CMD_WRITE;
  1289. ch->ccw[15].flags = CCW_FLAG_SLI | CCW_FLAG_CC;
  1290. ch->ccw[15].count = TH_HEADER_LENGTH;
  1291. ch->ccw[15].cda = virt_to_phys(ch->discontact_th);
  1292. ch->ccw[16].cmd_code = CCW_CMD_NOOP;
  1293. ch->ccw[16].flags = CCW_FLAG_SLI;
  1294. ch->fsm = init_fsm(ch->id, ctc_ch_state_names,
  1295. ctc_ch_event_names, CTC_MPC_NR_STATES,
  1296. CTC_MPC_NR_EVENTS, ctcmpc_ch_fsm,
  1297. mpc_ch_fsm_len, GFP_KERNEL);
  1298. } else {
  1299. ch->fsm = init_fsm(ch->id, ctc_ch_state_names,
  1300. ctc_ch_event_names, CTC_NR_STATES,
  1301. CTC_NR_EVENTS, ch_fsm,
  1302. ch_fsm_len, GFP_KERNEL);
  1303. }
  1304. if (ch->fsm == NULL)
  1305. goto nomem_return;
  1306. fsm_newstate(ch->fsm, CTC_STATE_IDLE);
  1307. ch->irb = kzalloc(sizeof(struct irb), GFP_KERNEL);
  1308. if (ch->irb == NULL)
  1309. goto nomem_return;
  1310. while (*c && ctcm_less_than((*c)->id, ch->id))
  1311. c = &(*c)->next;
  1312. if (*c && (!strncmp((*c)->id, ch->id, CTCM_ID_SIZE))) {
  1313. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
  1314. "%s (%s) already in list, using old entry",
  1315. __func__, (*c)->id);
  1316. goto free_return;
  1317. }
  1318. spin_lock_init(&ch->collect_lock);
  1319. fsm_settimer(ch->fsm, &ch->timer);
  1320. skb_queue_head_init(&ch->io_queue);
  1321. skb_queue_head_init(&ch->collect_queue);
  1322. if (IS_MPC(priv)) {
  1323. fsm_settimer(ch->fsm, &ch->sweep_timer);
  1324. skb_queue_head_init(&ch->sweep_queue);
  1325. }
  1326. ch->next = *c;
  1327. *c = ch;
  1328. return 0;
  1329. nomem_return:
  1330. rc = -ENOMEM;
  1331. free_return: /* note that all channel pointers are 0 or valid */
  1332. kfree(ch->ccw);
  1333. kfree(ch->discontact_th);
  1334. kfree_fsm(ch->fsm);
  1335. kfree(ch->irb);
  1336. kfree(ch);
  1337. return rc;
  1338. }
  1339. /*
  1340. * Return type of a detected device.
  1341. */
  1342. static enum ctcm_channel_types get_channel_type(struct ccw_device_id *id)
  1343. {
  1344. enum ctcm_channel_types type;
  1345. type = (enum ctcm_channel_types)id->driver_info;
  1346. if (type == ctcm_channel_type_ficon)
  1347. type = ctcm_channel_type_escon;
  1348. return type;
  1349. }
  1350. /**
  1351. *
  1352. * Setup an interface.
  1353. *
  1354. * cgdev Device to be setup.
  1355. *
  1356. * returns 0 on success, !0 on failure.
  1357. */
  1358. static int ctcm_new_device(struct ccwgroup_device *cgdev)
  1359. {
  1360. char read_id[CTCM_ID_SIZE];
  1361. char write_id[CTCM_ID_SIZE];
  1362. int direction;
  1363. enum ctcm_channel_types type;
  1364. struct ctcm_priv *priv;
  1365. struct net_device *dev;
  1366. struct ccw_device *cdev0;
  1367. struct ccw_device *cdev1;
  1368. struct channel *readc;
  1369. struct channel *writec;
  1370. int ret;
  1371. int result;
  1372. priv = dev_get_drvdata(&cgdev->dev);
  1373. if (!priv) {
  1374. result = -ENODEV;
  1375. goto out_err_result;
  1376. }
  1377. cdev0 = cgdev->cdev[0];
  1378. cdev1 = cgdev->cdev[1];
  1379. type = get_channel_type(&cdev0->id);
  1380. snprintf(read_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev0->dev));
  1381. snprintf(write_id, CTCM_ID_SIZE, "ch-%s", dev_name(&cdev1->dev));
  1382. ret = add_channel(cdev0, type, priv);
  1383. if (ret) {
  1384. result = ret;
  1385. goto out_err_result;
  1386. }
  1387. ret = add_channel(cdev1, type, priv);
  1388. if (ret) {
  1389. result = ret;
  1390. goto out_remove_channel1;
  1391. }
  1392. ret = ccw_device_set_online(cdev0);
  1393. if (ret != 0) {
  1394. CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE,
  1395. "%s(%s) set_online rc=%d",
  1396. CTCM_FUNTAIL, read_id, ret);
  1397. result = -EIO;
  1398. goto out_remove_channel2;
  1399. }
  1400. ret = ccw_device_set_online(cdev1);
  1401. if (ret != 0) {
  1402. CTCM_DBF_TEXT_(TRACE, CTC_DBF_NOTICE,
  1403. "%s(%s) set_online rc=%d",
  1404. CTCM_FUNTAIL, write_id, ret);
  1405. result = -EIO;
  1406. goto out_ccw1;
  1407. }
  1408. dev = ctcm_init_netdevice(priv);
  1409. if (dev == NULL) {
  1410. result = -ENODEV;
  1411. goto out_ccw2;
  1412. }
  1413. for (direction = CTCM_READ; direction <= CTCM_WRITE; direction++) {
  1414. priv->channel[direction] =
  1415. channel_get(type, direction == CTCM_READ ?
  1416. read_id : write_id, direction);
  1417. if (priv->channel[direction] == NULL) {
  1418. if (direction == CTCM_WRITE)
  1419. channel_free(priv->channel[CTCM_READ]);
  1420. goto out_dev;
  1421. }
  1422. priv->channel[direction]->netdev = dev;
  1423. priv->channel[direction]->protocol = priv->protocol;
  1424. priv->channel[direction]->max_bufsize = priv->buffer_size;
  1425. }
  1426. /* sysfs magic */
  1427. SET_NETDEV_DEV(dev, &cgdev->dev);
  1428. if (register_netdev(dev)) {
  1429. result = -ENODEV;
  1430. goto out_dev;
  1431. }
  1432. strlcpy(priv->fsm->name, dev->name, sizeof(priv->fsm->name));
  1433. dev_info(&dev->dev,
  1434. "setup OK : r/w = %s/%s, protocol : %d\n",
  1435. priv->channel[CTCM_READ]->id,
  1436. priv->channel[CTCM_WRITE]->id, priv->protocol);
  1437. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
  1438. "setup(%s) OK : r/w = %s/%s, protocol : %d", dev->name,
  1439. priv->channel[CTCM_READ]->id,
  1440. priv->channel[CTCM_WRITE]->id, priv->protocol);
  1441. return 0;
  1442. out_dev:
  1443. ctcm_free_netdevice(dev);
  1444. out_ccw2:
  1445. ccw_device_set_offline(cgdev->cdev[1]);
  1446. out_ccw1:
  1447. ccw_device_set_offline(cgdev->cdev[0]);
  1448. out_remove_channel2:
  1449. readc = channel_get(type, read_id, CTCM_READ);
  1450. channel_remove(readc);
  1451. out_remove_channel1:
  1452. writec = channel_get(type, write_id, CTCM_WRITE);
  1453. channel_remove(writec);
  1454. out_err_result:
  1455. return result;
  1456. }
  1457. /**
  1458. * Shutdown an interface.
  1459. *
  1460. * cgdev Device to be shut down.
  1461. *
  1462. * returns 0 on success, !0 on failure.
  1463. */
  1464. static int ctcm_shutdown_device(struct ccwgroup_device *cgdev)
  1465. {
  1466. struct ctcm_priv *priv;
  1467. struct net_device *dev;
  1468. priv = dev_get_drvdata(&cgdev->dev);
  1469. if (!priv)
  1470. return -ENODEV;
  1471. if (priv->channel[CTCM_READ]) {
  1472. dev = priv->channel[CTCM_READ]->netdev;
  1473. CTCM_DBF_DEV(SETUP, dev, "");
  1474. /* Close the device */
  1475. ctcm_close(dev);
  1476. dev->flags &= ~IFF_RUNNING;
  1477. channel_free(priv->channel[CTCM_READ]);
  1478. } else
  1479. dev = NULL;
  1480. if (priv->channel[CTCM_WRITE])
  1481. channel_free(priv->channel[CTCM_WRITE]);
  1482. if (dev) {
  1483. unregister_netdev(dev);
  1484. ctcm_free_netdevice(dev);
  1485. }
  1486. if (priv->fsm)
  1487. kfree_fsm(priv->fsm);
  1488. ccw_device_set_offline(cgdev->cdev[1]);
  1489. ccw_device_set_offline(cgdev->cdev[0]);
  1490. if (priv->channel[CTCM_READ])
  1491. channel_remove(priv->channel[CTCM_READ]);
  1492. if (priv->channel[CTCM_WRITE])
  1493. channel_remove(priv->channel[CTCM_WRITE]);
  1494. priv->channel[CTCM_READ] = priv->channel[CTCM_WRITE] = NULL;
  1495. return 0;
  1496. }
  1497. static void ctcm_remove_device(struct ccwgroup_device *cgdev)
  1498. {
  1499. struct ctcm_priv *priv = dev_get_drvdata(&cgdev->dev);
  1500. CTCM_DBF_TEXT_(SETUP, CTC_DBF_INFO,
  1501. "removing device %p, proto : %d",
  1502. cgdev, priv->protocol);
  1503. if (cgdev->state == CCWGROUP_ONLINE)
  1504. ctcm_shutdown_device(cgdev);
  1505. dev_set_drvdata(&cgdev->dev, NULL);
  1506. kfree(priv);
  1507. put_device(&cgdev->dev);
  1508. }
  1509. static int ctcm_pm_suspend(struct ccwgroup_device *gdev)
  1510. {
  1511. struct ctcm_priv *priv = dev_get_drvdata(&gdev->dev);
  1512. if (gdev->state == CCWGROUP_OFFLINE)
  1513. return 0;
  1514. netif_device_detach(priv->channel[CTCM_READ]->netdev);
  1515. ctcm_close(priv->channel[CTCM_READ]->netdev);
  1516. if (!wait_event_timeout(priv->fsm->wait_q,
  1517. fsm_getstate(priv->fsm) == DEV_STATE_STOPPED, CTCM_TIME_5_SEC)) {
  1518. netif_device_attach(priv->channel[CTCM_READ]->netdev);
  1519. return -EBUSY;
  1520. }
  1521. ccw_device_set_offline(gdev->cdev[1]);
  1522. ccw_device_set_offline(gdev->cdev[0]);
  1523. return 0;
  1524. }
  1525. static int ctcm_pm_resume(struct ccwgroup_device *gdev)
  1526. {
  1527. struct ctcm_priv *priv = dev_get_drvdata(&gdev->dev);
  1528. int rc;
  1529. if (gdev->state == CCWGROUP_OFFLINE)
  1530. return 0;
  1531. rc = ccw_device_set_online(gdev->cdev[1]);
  1532. if (rc)
  1533. goto err_out;
  1534. rc = ccw_device_set_online(gdev->cdev[0]);
  1535. if (rc)
  1536. goto err_out;
  1537. ctcm_open(priv->channel[CTCM_READ]->netdev);
  1538. err_out:
  1539. netif_device_attach(priv->channel[CTCM_READ]->netdev);
  1540. return rc;
  1541. }
  1542. static struct ccw_device_id ctcm_ids[] = {
  1543. {CCW_DEVICE(0x3088, 0x08), .driver_info = ctcm_channel_type_parallel},
  1544. {CCW_DEVICE(0x3088, 0x1e), .driver_info = ctcm_channel_type_ficon},
  1545. {CCW_DEVICE(0x3088, 0x1f), .driver_info = ctcm_channel_type_escon},
  1546. {},
  1547. };
  1548. MODULE_DEVICE_TABLE(ccw, ctcm_ids);
  1549. static struct ccw_driver ctcm_ccw_driver = {
  1550. .driver = {
  1551. .owner = THIS_MODULE,
  1552. .name = "ctcm",
  1553. },
  1554. .ids = ctcm_ids,
  1555. .probe = ccwgroup_probe_ccwdev,
  1556. .remove = ccwgroup_remove_ccwdev,
  1557. .int_class = IRQIO_CTC,
  1558. };
  1559. static struct ccwgroup_driver ctcm_group_driver = {
  1560. .driver = {
  1561. .owner = THIS_MODULE,
  1562. .name = CTC_DRIVER_NAME,
  1563. },
  1564. .setup = ctcm_probe_device,
  1565. .remove = ctcm_remove_device,
  1566. .set_online = ctcm_new_device,
  1567. .set_offline = ctcm_shutdown_device,
  1568. .freeze = ctcm_pm_suspend,
  1569. .thaw = ctcm_pm_resume,
  1570. .restore = ctcm_pm_resume,
  1571. };
  1572. static ssize_t ctcm_driver_group_store(struct device_driver *ddrv,
  1573. const char *buf, size_t count)
  1574. {
  1575. int err;
  1576. err = ccwgroup_create_dev(ctcm_root_dev, &ctcm_group_driver, 2, buf);
  1577. return err ? err : count;
  1578. }
  1579. static DRIVER_ATTR(group, 0200, NULL, ctcm_driver_group_store);
  1580. static struct attribute *ctcm_drv_attrs[] = {
  1581. &driver_attr_group.attr,
  1582. NULL,
  1583. };
  1584. static struct attribute_group ctcm_drv_attr_group = {
  1585. .attrs = ctcm_drv_attrs,
  1586. };
  1587. static const struct attribute_group *ctcm_drv_attr_groups[] = {
  1588. &ctcm_drv_attr_group,
  1589. NULL,
  1590. };
  1591. /*
  1592. * Module related routines
  1593. */
  1594. /*
  1595. * Prepare to be unloaded. Free IRQ's and release all resources.
  1596. * This is called just before this module is unloaded. It is
  1597. * not called, if the usage count is !0, so we don't need to check
  1598. * for that.
  1599. */
  1600. static void __exit ctcm_exit(void)
  1601. {
  1602. ccwgroup_driver_unregister(&ctcm_group_driver);
  1603. ccw_driver_unregister(&ctcm_ccw_driver);
  1604. root_device_unregister(ctcm_root_dev);
  1605. ctcm_unregister_dbf_views();
  1606. pr_info("CTCM driver unloaded\n");
  1607. }
  1608. /*
  1609. * Print Banner.
  1610. */
  1611. static void print_banner(void)
  1612. {
  1613. pr_info("CTCM driver initialized\n");
  1614. }
  1615. /**
  1616. * Initialize module.
  1617. * This is called just after the module is loaded.
  1618. *
  1619. * returns 0 on success, !0 on error.
  1620. */
  1621. static int __init ctcm_init(void)
  1622. {
  1623. int ret;
  1624. channels = NULL;
  1625. ret = ctcm_register_dbf_views();
  1626. if (ret)
  1627. goto out_err;
  1628. ctcm_root_dev = root_device_register("ctcm");
  1629. ret = PTR_ERR_OR_ZERO(ctcm_root_dev);
  1630. if (ret)
  1631. goto register_err;
  1632. ret = ccw_driver_register(&ctcm_ccw_driver);
  1633. if (ret)
  1634. goto ccw_err;
  1635. ctcm_group_driver.driver.groups = ctcm_drv_attr_groups;
  1636. ret = ccwgroup_driver_register(&ctcm_group_driver);
  1637. if (ret)
  1638. goto ccwgroup_err;
  1639. print_banner();
  1640. return 0;
  1641. ccwgroup_err:
  1642. ccw_driver_unregister(&ctcm_ccw_driver);
  1643. ccw_err:
  1644. root_device_unregister(ctcm_root_dev);
  1645. register_err:
  1646. ctcm_unregister_dbf_views();
  1647. out_err:
  1648. pr_err("%s / Initializing the ctcm device driver failed, ret = %d\n",
  1649. __func__, ret);
  1650. return ret;
  1651. }
  1652. module_init(ctcm_init);
  1653. module_exit(ctcm_exit);
  1654. MODULE_AUTHOR("Peter Tiedemann <ptiedem@de.ibm.com>");
  1655. MODULE_DESCRIPTION("Network driver for S/390 CTC + CTCMPC (SNA)");
  1656. MODULE_LICENSE("GPL");