ctcm_main.c 47 KB

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