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