0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #undef DEBUG
0020
0021 #include <linux/console.h>
0022 #include <linux/ctype.h>
0023 #include <linux/delay.h>
0024 #include <linux/init.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/module.h>
0027 #include <linux/major.h>
0028 #include <linux/kernel.h>
0029 #include <linux/of_irq.h>
0030 #include <linux/spinlock.h>
0031 #include <linux/sysrq.h>
0032 #include <linux/tty.h>
0033 #include <linux/tty_flip.h>
0034 #include <asm/hvcall.h>
0035 #include <asm/hvconsole.h>
0036 #include <linux/uaccess.h>
0037 #include <asm/vio.h>
0038 #include <asm/param.h>
0039 #include <asm/hvsi.h>
0040
0041 #define HVSI_MAJOR 229
0042 #define HVSI_MINOR 128
0043 #define MAX_NR_HVSI_CONSOLES 4
0044
0045 #define HVSI_TIMEOUT (5*HZ)
0046 #define HVSI_VERSION 1
0047 #define HVSI_MAX_PACKET 256
0048 #define HVSI_MAX_READ 16
0049 #define HVSI_MAX_OUTGOING_DATA 12
0050 #define N_OUTBUF 12
0051
0052
0053
0054
0055
0056 #define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
0057
0058 struct hvsi_struct {
0059 struct tty_port port;
0060 struct delayed_work writer;
0061 struct work_struct handshaker;
0062 wait_queue_head_t emptyq;
0063 wait_queue_head_t stateq;
0064 spinlock_t lock;
0065 int index;
0066 uint8_t throttle_buf[128];
0067 uint8_t outbuf[N_OUTBUF];
0068
0069 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
0070 uint8_t *inbuf_end;
0071 int n_throttle;
0072 int n_outbuf;
0073 uint32_t vtermno;
0074 uint32_t virq;
0075 atomic_t seqno;
0076 uint16_t mctrl;
0077 uint8_t state;
0078 uint8_t flags;
0079 #ifdef CONFIG_MAGIC_SYSRQ
0080 uint8_t sysrq;
0081 #endif
0082 };
0083 static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
0084
0085 static struct tty_driver *hvsi_driver;
0086 static int hvsi_count;
0087 static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
0088
0089 enum HVSI_PROTOCOL_STATE {
0090 HVSI_CLOSED,
0091 HVSI_WAIT_FOR_VER_RESPONSE,
0092 HVSI_WAIT_FOR_VER_QUERY,
0093 HVSI_OPEN,
0094 HVSI_WAIT_FOR_MCTRL_RESPONSE,
0095 HVSI_FSP_DIED,
0096 };
0097 #define HVSI_CONSOLE 0x1
0098
0099 static inline int is_console(struct hvsi_struct *hp)
0100 {
0101 return hp->flags & HVSI_CONSOLE;
0102 }
0103
0104 static inline int is_open(struct hvsi_struct *hp)
0105 {
0106
0107 return (hp->state == HVSI_OPEN)
0108 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
0109 }
0110
0111 static inline void print_state(struct hvsi_struct *hp)
0112 {
0113 #ifdef DEBUG
0114 static const char *state_names[] = {
0115 "HVSI_CLOSED",
0116 "HVSI_WAIT_FOR_VER_RESPONSE",
0117 "HVSI_WAIT_FOR_VER_QUERY",
0118 "HVSI_OPEN",
0119 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
0120 "HVSI_FSP_DIED",
0121 };
0122 const char *name = (hp->state < ARRAY_SIZE(state_names))
0123 ? state_names[hp->state] : "UNKNOWN";
0124
0125 pr_debug("hvsi%i: state = %s\n", hp->index, name);
0126 #endif
0127 }
0128
0129 static inline void __set_state(struct hvsi_struct *hp, int state)
0130 {
0131 hp->state = state;
0132 print_state(hp);
0133 wake_up_all(&hp->stateq);
0134 }
0135
0136 static inline void set_state(struct hvsi_struct *hp, int state)
0137 {
0138 unsigned long flags;
0139
0140 spin_lock_irqsave(&hp->lock, flags);
0141 __set_state(hp, state);
0142 spin_unlock_irqrestore(&hp->lock, flags);
0143 }
0144
0145 static inline int len_packet(const uint8_t *packet)
0146 {
0147 return (int)((struct hvsi_header *)packet)->len;
0148 }
0149
0150 static inline int is_header(const uint8_t *packet)
0151 {
0152 struct hvsi_header *header = (struct hvsi_header *)packet;
0153 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
0154 }
0155
0156 static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
0157 {
0158 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
0159 return 0;
0160
0161 if (hp->inbuf_end < (packet + len_packet(packet)))
0162 return 0;
0163
0164 return 1;
0165 }
0166
0167
0168 static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
0169 {
0170 int remaining = (int)(hp->inbuf_end - read_to);
0171
0172 pr_debug("%s: %i chars remain\n", __func__, remaining);
0173
0174 if (read_to != hp->inbuf)
0175 memmove(hp->inbuf, read_to, remaining);
0176
0177 hp->inbuf_end = hp->inbuf + remaining;
0178 }
0179
0180 #ifdef DEBUG
0181 #define dbg_dump_packet(packet) dump_packet(packet)
0182 #define dbg_dump_hex(data, len) dump_hex(data, len)
0183 #else
0184 #define dbg_dump_packet(packet) do { } while (0)
0185 #define dbg_dump_hex(data, len) do { } while (0)
0186 #endif
0187
0188 static void dump_hex(const uint8_t *data, int len)
0189 {
0190 int i;
0191
0192 printk(" ");
0193 for (i=0; i < len; i++)
0194 printk("%.2x", data[i]);
0195
0196 printk("\n ");
0197 for (i=0; i < len; i++) {
0198 if (isprint(data[i]))
0199 printk("%c", data[i]);
0200 else
0201 printk(".");
0202 }
0203 printk("\n");
0204 }
0205
0206 static void dump_packet(uint8_t *packet)
0207 {
0208 struct hvsi_header *header = (struct hvsi_header *)packet;
0209
0210 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
0211 header->seqno);
0212
0213 dump_hex(packet, header->len);
0214 }
0215
0216 static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
0217 {
0218 unsigned long got;
0219
0220 got = hvc_get_chars(hp->vtermno, buf, count);
0221
0222 return got;
0223 }
0224
0225 static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
0226 struct tty_struct *tty, struct hvsi_struct **to_handshake)
0227 {
0228 struct hvsi_control *header = (struct hvsi_control *)packet;
0229
0230 switch (be16_to_cpu(header->verb)) {
0231 case VSV_MODEM_CTL_UPDATE:
0232 if ((be32_to_cpu(header->word) & HVSI_TSCD) == 0) {
0233
0234 pr_debug("hvsi%i: CD dropped\n", hp->index);
0235 hp->mctrl &= TIOCM_CD;
0236 if (tty && !C_CLOCAL(tty))
0237 tty_hangup(tty);
0238 }
0239 break;
0240 case VSV_CLOSE_PROTOCOL:
0241 pr_debug("hvsi%i: service processor came back\n", hp->index);
0242 if (hp->state != HVSI_CLOSED) {
0243 *to_handshake = hp;
0244 }
0245 break;
0246 default:
0247 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
0248 hp->index);
0249 dump_packet(packet);
0250 break;
0251 }
0252 }
0253
0254 static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
0255 {
0256 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
0257 uint32_t mctrl_word;
0258
0259 switch (hp->state) {
0260 case HVSI_WAIT_FOR_VER_RESPONSE:
0261 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
0262 break;
0263 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
0264 hp->mctrl = 0;
0265 mctrl_word = be32_to_cpu(resp->u.mctrl_word);
0266 if (mctrl_word & HVSI_TSDTR)
0267 hp->mctrl |= TIOCM_DTR;
0268 if (mctrl_word & HVSI_TSCD)
0269 hp->mctrl |= TIOCM_CD;
0270 __set_state(hp, HVSI_OPEN);
0271 break;
0272 default:
0273 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
0274 dump_packet(packet);
0275 break;
0276 }
0277 }
0278
0279
0280 static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
0281 {
0282 struct hvsi_query_response packet __ALIGNED__;
0283 int wrote;
0284
0285 packet.hdr.type = VS_QUERY_RESPONSE_PACKET_HEADER;
0286 packet.hdr.len = sizeof(struct hvsi_query_response);
0287 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
0288 packet.verb = cpu_to_be16(VSV_SEND_VERSION_NUMBER);
0289 packet.u.version = HVSI_VERSION;
0290 packet.query_seqno = cpu_to_be16(query_seqno+1);
0291
0292 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
0293 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
0294
0295 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
0296 if (wrote != packet.hdr.len) {
0297 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
0298 hp->index);
0299 return -EIO;
0300 }
0301
0302 return 0;
0303 }
0304
0305 static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
0306 {
0307 struct hvsi_query *query = (struct hvsi_query *)packet;
0308
0309 switch (hp->state) {
0310 case HVSI_WAIT_FOR_VER_QUERY:
0311 hvsi_version_respond(hp, be16_to_cpu(query->hdr.seqno));
0312 __set_state(hp, HVSI_OPEN);
0313 break;
0314 default:
0315 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
0316 dump_packet(packet);
0317 break;
0318 }
0319 }
0320
0321 static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
0322 {
0323 int i;
0324
0325 for (i=0; i < len; i++) {
0326 char c = buf[i];
0327 #ifdef CONFIG_MAGIC_SYSRQ
0328 if (c == '\0') {
0329 hp->sysrq = 1;
0330 continue;
0331 } else if (hp->sysrq) {
0332 handle_sysrq(c);
0333 hp->sysrq = 0;
0334 continue;
0335 }
0336 #endif
0337 tty_insert_flip_char(&hp->port, c, 0);
0338 }
0339 }
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349 #define TTY_THRESHOLD_THROTTLE 128
0350 static bool hvsi_recv_data(struct hvsi_struct *hp, const uint8_t *packet)
0351 {
0352 const struct hvsi_header *header = (const struct hvsi_header *)packet;
0353 const uint8_t *data = packet + sizeof(struct hvsi_header);
0354 int datalen = header->len - sizeof(struct hvsi_header);
0355 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
0356
0357 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
0358
0359 if (datalen == 0)
0360 return false;
0361
0362 if (overflow > 0) {
0363 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
0364 datalen = TTY_THRESHOLD_THROTTLE;
0365 }
0366
0367 hvsi_insert_chars(hp, data, datalen);
0368
0369 if (overflow > 0) {
0370
0371
0372
0373
0374 pr_debug("%s: deferring overflow\n", __func__);
0375 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
0376 hp->n_throttle = overflow;
0377 }
0378
0379 return true;
0380 }
0381
0382
0383
0384
0385
0386
0387
0388 static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct *tty,
0389 struct hvsi_struct **handshake)
0390 {
0391 uint8_t *packet = hp->inbuf;
0392 int chunklen;
0393 bool flip = false;
0394
0395 *handshake = NULL;
0396
0397 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
0398 if (chunklen == 0) {
0399 pr_debug("%s: 0-length read\n", __func__);
0400 return 0;
0401 }
0402
0403 pr_debug("%s: got %i bytes\n", __func__, chunklen);
0404 dbg_dump_hex(hp->inbuf_end, chunklen);
0405
0406 hp->inbuf_end += chunklen;
0407
0408
0409 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
0410 struct hvsi_header *header = (struct hvsi_header *)packet;
0411
0412 if (!is_header(packet)) {
0413 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
0414
0415 while ((packet < hp->inbuf_end) && (!is_header(packet)))
0416 packet++;
0417 continue;
0418 }
0419
0420 pr_debug("%s: handling %i-byte packet\n", __func__,
0421 len_packet(packet));
0422 dbg_dump_packet(packet);
0423
0424 switch (header->type) {
0425 case VS_DATA_PACKET_HEADER:
0426 if (!is_open(hp))
0427 break;
0428 flip = hvsi_recv_data(hp, packet);
0429 break;
0430 case VS_CONTROL_PACKET_HEADER:
0431 hvsi_recv_control(hp, packet, tty, handshake);
0432 break;
0433 case VS_QUERY_RESPONSE_PACKET_HEADER:
0434 hvsi_recv_response(hp, packet);
0435 break;
0436 case VS_QUERY_PACKET_HEADER:
0437 hvsi_recv_query(hp, packet);
0438 break;
0439 default:
0440 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
0441 hp->index, header->type);
0442 dump_packet(packet);
0443 break;
0444 }
0445
0446 packet += len_packet(packet);
0447
0448 if (*handshake) {
0449 pr_debug("%s: handshake\n", __func__);
0450 break;
0451 }
0452 }
0453
0454 compact_inbuf(hp, packet);
0455
0456 if (flip)
0457 tty_flip_buffer_push(&hp->port);
0458
0459 return 1;
0460 }
0461
0462 static void hvsi_send_overflow(struct hvsi_struct *hp)
0463 {
0464 pr_debug("%s: delivering %i bytes overflow\n", __func__,
0465 hp->n_throttle);
0466
0467 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
0468 hp->n_throttle = 0;
0469 }
0470
0471
0472
0473
0474
0475 static irqreturn_t hvsi_interrupt(int irq, void *arg)
0476 {
0477 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
0478 struct hvsi_struct *handshake;
0479 struct tty_struct *tty;
0480 unsigned long flags;
0481 int again = 1;
0482
0483 pr_debug("%s\n", __func__);
0484
0485 tty = tty_port_tty_get(&hp->port);
0486
0487 while (again) {
0488 spin_lock_irqsave(&hp->lock, flags);
0489 again = hvsi_load_chunk(hp, tty, &handshake);
0490 spin_unlock_irqrestore(&hp->lock, flags);
0491
0492 if (handshake) {
0493 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
0494 schedule_work(&handshake->handshaker);
0495 }
0496 }
0497
0498 spin_lock_irqsave(&hp->lock, flags);
0499 if (tty && hp->n_throttle && !tty_throttled(tty)) {
0500
0501
0502 hvsi_send_overflow(hp);
0503 tty_flip_buffer_push(&hp->port);
0504 }
0505 spin_unlock_irqrestore(&hp->lock, flags);
0506
0507 tty_kref_put(tty);
0508
0509 return IRQ_HANDLED;
0510 }
0511
0512
0513 static int __init poll_for_state(struct hvsi_struct *hp, int state)
0514 {
0515 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
0516
0517 for (;;) {
0518 hvsi_interrupt(hp->virq, (void *)hp);
0519
0520 if (hp->state == state)
0521 return 0;
0522
0523 mdelay(5);
0524 if (time_after(jiffies, end_jiffies))
0525 return -EIO;
0526 }
0527 }
0528
0529
0530 static int wait_for_state(struct hvsi_struct *hp, int state)
0531 {
0532 int ret = 0;
0533
0534 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
0535 ret = -EIO;
0536
0537 return ret;
0538 }
0539
0540 static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
0541 {
0542 struct hvsi_query packet __ALIGNED__;
0543 int wrote;
0544
0545 packet.hdr.type = VS_QUERY_PACKET_HEADER;
0546 packet.hdr.len = sizeof(struct hvsi_query);
0547 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
0548 packet.verb = cpu_to_be16(verb);
0549
0550 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
0551 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
0552
0553 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
0554 if (wrote != packet.hdr.len) {
0555 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
0556 wrote);
0557 return -EIO;
0558 }
0559
0560 return 0;
0561 }
0562
0563 static int hvsi_get_mctrl(struct hvsi_struct *hp)
0564 {
0565 int ret;
0566
0567 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
0568 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
0569
0570 ret = hvsi_wait(hp, HVSI_OPEN);
0571 if (ret < 0) {
0572 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
0573 set_state(hp, HVSI_OPEN);
0574 return ret;
0575 }
0576
0577 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
0578
0579 return 0;
0580 }
0581
0582
0583 static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
0584 {
0585 struct hvsi_control packet __ALIGNED__;
0586 int wrote;
0587
0588 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
0589 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
0590 packet.hdr.len = sizeof(struct hvsi_control);
0591 packet.verb = cpu_to_be16(VSV_SET_MODEM_CTL);
0592 packet.mask = cpu_to_be32(HVSI_TSDTR);
0593
0594 if (mctrl & TIOCM_DTR)
0595 packet.word = cpu_to_be32(HVSI_TSDTR);
0596
0597 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
0598 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
0599
0600 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
0601 if (wrote != packet.hdr.len) {
0602 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
0603 return -EIO;
0604 }
0605
0606 return 0;
0607 }
0608
0609 static void hvsi_drain_input(struct hvsi_struct *hp)
0610 {
0611 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
0612 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
0613
0614 while (time_before(end_jiffies, jiffies))
0615 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
0616 break;
0617 }
0618
0619 static int hvsi_handshake(struct hvsi_struct *hp)
0620 {
0621 int ret;
0622
0623
0624
0625
0626
0627
0628
0629
0630 hvsi_drain_input(hp);
0631
0632 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
0633 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
0634 if (ret < 0) {
0635 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
0636 return ret;
0637 }
0638
0639 ret = hvsi_wait(hp, HVSI_OPEN);
0640 if (ret < 0)
0641 return ret;
0642
0643 return 0;
0644 }
0645
0646 static void hvsi_handshaker(struct work_struct *work)
0647 {
0648 struct hvsi_struct *hp =
0649 container_of(work, struct hvsi_struct, handshaker);
0650
0651 if (hvsi_handshake(hp) >= 0)
0652 return;
0653
0654 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
0655 if (is_console(hp)) {
0656
0657
0658
0659
0660 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
0661 }
0662 }
0663
0664 static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
0665 {
0666 struct hvsi_data packet __ALIGNED__;
0667 int ret;
0668
0669 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
0670
0671 packet.hdr.type = VS_DATA_PACKET_HEADER;
0672 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
0673 packet.hdr.len = count + sizeof(struct hvsi_header);
0674 memcpy(&packet.data, buf, count);
0675
0676 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
0677 if (ret == packet.hdr.len) {
0678
0679 return count;
0680 }
0681 return ret;
0682 }
0683
0684 static void hvsi_close_protocol(struct hvsi_struct *hp)
0685 {
0686 struct hvsi_control packet __ALIGNED__;
0687
0688 packet.hdr.type = VS_CONTROL_PACKET_HEADER;
0689 packet.hdr.seqno = cpu_to_be16(atomic_inc_return(&hp->seqno));
0690 packet.hdr.len = 6;
0691 packet.verb = cpu_to_be16(VSV_CLOSE_PROTOCOL);
0692
0693 pr_debug("%s: sending %i bytes\n", __func__, packet.hdr.len);
0694 dbg_dump_hex((uint8_t*)&packet, packet.hdr.len);
0695
0696 hvc_put_chars(hp->vtermno, (char *)&packet, packet.hdr.len);
0697 }
0698
0699 static int hvsi_open(struct tty_struct *tty, struct file *filp)
0700 {
0701 struct hvsi_struct *hp;
0702 unsigned long flags;
0703 int ret;
0704
0705 pr_debug("%s\n", __func__);
0706
0707 hp = &hvsi_ports[tty->index];
0708
0709 tty->driver_data = hp;
0710
0711 mb();
0712 if (hp->state == HVSI_FSP_DIED)
0713 return -EIO;
0714
0715 tty_port_tty_set(&hp->port, tty);
0716 spin_lock_irqsave(&hp->lock, flags);
0717 hp->port.count++;
0718 atomic_set(&hp->seqno, 0);
0719 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
0720 spin_unlock_irqrestore(&hp->lock, flags);
0721
0722 if (is_console(hp))
0723 return 0;
0724
0725 ret = hvsi_handshake(hp);
0726 if (ret < 0) {
0727 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
0728 return ret;
0729 }
0730
0731 ret = hvsi_get_mctrl(hp);
0732 if (ret < 0) {
0733 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
0734 return ret;
0735 }
0736
0737 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
0738 if (ret < 0) {
0739 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
0740 return ret;
0741 }
0742
0743 return 0;
0744 }
0745
0746
0747 static void hvsi_flush_output(struct hvsi_struct *hp)
0748 {
0749 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
0750
0751
0752 cancel_delayed_work_sync(&hp->writer);
0753 flush_work(&hp->handshaker);
0754
0755
0756
0757
0758
0759 hp->n_outbuf = 0;
0760 }
0761
0762 static void hvsi_close(struct tty_struct *tty, struct file *filp)
0763 {
0764 struct hvsi_struct *hp = tty->driver_data;
0765 unsigned long flags;
0766
0767 pr_debug("%s\n", __func__);
0768
0769 if (tty_hung_up_p(filp))
0770 return;
0771
0772 spin_lock_irqsave(&hp->lock, flags);
0773
0774 if (--hp->port.count == 0) {
0775 tty_port_tty_set(&hp->port, NULL);
0776 hp->inbuf_end = hp->inbuf;
0777
0778
0779 if (!is_console(hp)) {
0780 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
0781 __set_state(hp, HVSI_CLOSED);
0782
0783
0784
0785
0786 tty->closing = 1;
0787
0788 spin_unlock_irqrestore(&hp->lock, flags);
0789
0790
0791 synchronize_irq(hp->virq);
0792
0793
0794 hvsi_flush_output(hp);
0795
0796
0797 hvsi_close_protocol(hp);
0798
0799
0800
0801
0802
0803 hvsi_drain_input(hp);
0804
0805 spin_lock_irqsave(&hp->lock, flags);
0806 }
0807 } else if (hp->port.count < 0)
0808 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
0809 hp - hvsi_ports, hp->port.count);
0810
0811 spin_unlock_irqrestore(&hp->lock, flags);
0812 }
0813
0814 static void hvsi_hangup(struct tty_struct *tty)
0815 {
0816 struct hvsi_struct *hp = tty->driver_data;
0817 unsigned long flags;
0818
0819 pr_debug("%s\n", __func__);
0820
0821 tty_port_tty_set(&hp->port, NULL);
0822
0823 spin_lock_irqsave(&hp->lock, flags);
0824 hp->port.count = 0;
0825 hp->n_outbuf = 0;
0826 spin_unlock_irqrestore(&hp->lock, flags);
0827 }
0828
0829
0830 static void hvsi_push(struct hvsi_struct *hp)
0831 {
0832 int n;
0833
0834 if (hp->n_outbuf <= 0)
0835 return;
0836
0837 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
0838 if (n > 0) {
0839
0840 pr_debug("%s: wrote %i chars\n", __func__, n);
0841 hp->n_outbuf = 0;
0842 } else if (n == -EIO) {
0843 __set_state(hp, HVSI_FSP_DIED);
0844 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
0845 }
0846 }
0847
0848
0849 static void hvsi_write_worker(struct work_struct *work)
0850 {
0851 struct hvsi_struct *hp =
0852 container_of(work, struct hvsi_struct, writer.work);
0853 unsigned long flags;
0854 #ifdef DEBUG
0855 static long start_j = 0;
0856
0857 if (start_j == 0)
0858 start_j = jiffies;
0859 #endif
0860
0861 spin_lock_irqsave(&hp->lock, flags);
0862
0863 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
0864
0865 if (!is_open(hp)) {
0866
0867
0868
0869
0870
0871
0872 schedule_delayed_work(&hp->writer, HZ);
0873 goto out;
0874 }
0875
0876 hvsi_push(hp);
0877 if (hp->n_outbuf > 0)
0878 schedule_delayed_work(&hp->writer, 10);
0879 else {
0880 #ifdef DEBUG
0881 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
0882 jiffies - start_j);
0883 start_j = 0;
0884 #endif
0885 wake_up_all(&hp->emptyq);
0886 tty_port_tty_wakeup(&hp->port);
0887 }
0888
0889 out:
0890 spin_unlock_irqrestore(&hp->lock, flags);
0891 }
0892
0893 static unsigned int hvsi_write_room(struct tty_struct *tty)
0894 {
0895 struct hvsi_struct *hp = tty->driver_data;
0896
0897 return N_OUTBUF - hp->n_outbuf;
0898 }
0899
0900 static unsigned int hvsi_chars_in_buffer(struct tty_struct *tty)
0901 {
0902 struct hvsi_struct *hp = tty->driver_data;
0903
0904 return hp->n_outbuf;
0905 }
0906
0907 static int hvsi_write(struct tty_struct *tty,
0908 const unsigned char *buf, int count)
0909 {
0910 struct hvsi_struct *hp = tty->driver_data;
0911 const char *source = buf;
0912 unsigned long flags;
0913 int total = 0;
0914 int origcount = count;
0915
0916 spin_lock_irqsave(&hp->lock, flags);
0917
0918 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
0919
0920 if (!is_open(hp)) {
0921
0922 pr_debug("%s: not open\n", __func__);
0923 goto out;
0924 }
0925
0926
0927
0928
0929
0930
0931 while ((count > 0) && (hvsi_write_room(tty) > 0)) {
0932 int chunksize = min_t(int, count, hvsi_write_room(tty));
0933
0934 BUG_ON(hp->n_outbuf < 0);
0935 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
0936 hp->n_outbuf += chunksize;
0937
0938 total += chunksize;
0939 source += chunksize;
0940 count -= chunksize;
0941 hvsi_push(hp);
0942 }
0943
0944 if (hp->n_outbuf > 0) {
0945
0946
0947
0948
0949 schedule_delayed_work(&hp->writer, 10);
0950 }
0951
0952 out:
0953 spin_unlock_irqrestore(&hp->lock, flags);
0954
0955 if (total != origcount)
0956 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
0957 total);
0958
0959 return total;
0960 }
0961
0962
0963
0964
0965
0966 static void hvsi_throttle(struct tty_struct *tty)
0967 {
0968 struct hvsi_struct *hp = tty->driver_data;
0969
0970 pr_debug("%s\n", __func__);
0971
0972 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
0973 }
0974
0975 static void hvsi_unthrottle(struct tty_struct *tty)
0976 {
0977 struct hvsi_struct *hp = tty->driver_data;
0978 unsigned long flags;
0979
0980 pr_debug("%s\n", __func__);
0981
0982 spin_lock_irqsave(&hp->lock, flags);
0983 if (hp->n_throttle) {
0984 hvsi_send_overflow(hp);
0985 tty_flip_buffer_push(&hp->port);
0986 }
0987 spin_unlock_irqrestore(&hp->lock, flags);
0988
0989
0990 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
0991 }
0992
0993 static int hvsi_tiocmget(struct tty_struct *tty)
0994 {
0995 struct hvsi_struct *hp = tty->driver_data;
0996
0997 hvsi_get_mctrl(hp);
0998 return hp->mctrl;
0999 }
1000
1001 static int hvsi_tiocmset(struct tty_struct *tty,
1002 unsigned int set, unsigned int clear)
1003 {
1004 struct hvsi_struct *hp = tty->driver_data;
1005 unsigned long flags;
1006 uint16_t new_mctrl;
1007
1008
1009 clear &= TIOCM_DTR;
1010 set &= TIOCM_DTR;
1011
1012 spin_lock_irqsave(&hp->lock, flags);
1013
1014 new_mctrl = (hp->mctrl & ~clear) | set;
1015
1016 if (hp->mctrl != new_mctrl) {
1017 hvsi_set_mctrl(hp, new_mctrl);
1018 hp->mctrl = new_mctrl;
1019 }
1020 spin_unlock_irqrestore(&hp->lock, flags);
1021
1022 return 0;
1023 }
1024
1025
1026 static const struct tty_operations hvsi_ops = {
1027 .open = hvsi_open,
1028 .close = hvsi_close,
1029 .write = hvsi_write,
1030 .hangup = hvsi_hangup,
1031 .write_room = hvsi_write_room,
1032 .chars_in_buffer = hvsi_chars_in_buffer,
1033 .throttle = hvsi_throttle,
1034 .unthrottle = hvsi_unthrottle,
1035 .tiocmget = hvsi_tiocmget,
1036 .tiocmset = hvsi_tiocmset,
1037 };
1038
1039 static int __init hvsi_init(void)
1040 {
1041 struct tty_driver *driver;
1042 int i, ret;
1043
1044 driver = tty_alloc_driver(hvsi_count, TTY_DRIVER_REAL_RAW);
1045 if (IS_ERR(driver))
1046 return PTR_ERR(driver);
1047
1048 driver->driver_name = "hvsi";
1049 driver->name = "hvsi";
1050 driver->major = HVSI_MAJOR;
1051 driver->minor_start = HVSI_MINOR;
1052 driver->type = TTY_DRIVER_TYPE_SYSTEM;
1053 driver->init_termios = tty_std_termios;
1054 driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1055 driver->init_termios.c_ispeed = 9600;
1056 driver->init_termios.c_ospeed = 9600;
1057 tty_set_operations(driver, &hvsi_ops);
1058
1059 for (i=0; i < hvsi_count; i++) {
1060 struct hvsi_struct *hp = &hvsi_ports[i];
1061 int ret = 1;
1062
1063 tty_port_link_device(&hp->port, driver, i);
1064
1065 ret = request_irq(hp->virq, hvsi_interrupt, 0, "hvsi", hp);
1066 if (ret)
1067 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1068 hp->virq, ret);
1069 }
1070 hvsi_wait = wait_for_state;
1071
1072 ret = tty_register_driver(driver);
1073 if (ret) {
1074 pr_err("Couldn't register hvsi console driver\n");
1075 goto err_free_irq;
1076 }
1077
1078 hvsi_driver = driver;
1079
1080 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1081
1082 return 0;
1083 err_free_irq:
1084 hvsi_wait = poll_for_state;
1085 for (i = 0; i < hvsi_count; i++) {
1086 struct hvsi_struct *hp = &hvsi_ports[i];
1087
1088 free_irq(hp->virq, hp);
1089 }
1090 tty_driver_kref_put(driver);
1091
1092 return ret;
1093 }
1094 device_initcall(hvsi_init);
1095
1096
1097
1098 static void hvsi_console_print(struct console *console, const char *buf,
1099 unsigned int count)
1100 {
1101 struct hvsi_struct *hp = &hvsi_ports[console->index];
1102 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1103 unsigned int i = 0, n = 0;
1104 int ret, donecr = 0;
1105
1106 mb();
1107 if (!is_open(hp))
1108 return;
1109
1110
1111
1112
1113
1114 while (count > 0 || i > 0) {
1115 if (count > 0 && i < sizeof(c)) {
1116 if (buf[n] == '\n' && !donecr) {
1117 c[i++] = '\r';
1118 donecr = 1;
1119 } else {
1120 c[i++] = buf[n++];
1121 donecr = 0;
1122 --count;
1123 }
1124 } else {
1125 ret = hvsi_put_chars(hp, c, i);
1126 if (ret < 0)
1127 i = 0;
1128 i -= ret;
1129 }
1130 }
1131 }
1132
1133 static struct tty_driver *hvsi_console_device(struct console *console,
1134 int *index)
1135 {
1136 *index = console->index;
1137 return hvsi_driver;
1138 }
1139
1140 static int __init hvsi_console_setup(struct console *console, char *options)
1141 {
1142 struct hvsi_struct *hp;
1143 int ret;
1144
1145 if (console->index < 0 || console->index >= hvsi_count)
1146 return -EINVAL;
1147 hp = &hvsi_ports[console->index];
1148
1149
1150 hvsi_close_protocol(hp);
1151
1152 ret = hvsi_handshake(hp);
1153 if (ret < 0)
1154 return ret;
1155
1156 ret = hvsi_get_mctrl(hp);
1157 if (ret < 0)
1158 return ret;
1159
1160 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1161 if (ret < 0)
1162 return ret;
1163
1164 hp->flags |= HVSI_CONSOLE;
1165
1166 return 0;
1167 }
1168
1169 static struct console hvsi_console = {
1170 .name = "hvsi",
1171 .write = hvsi_console_print,
1172 .device = hvsi_console_device,
1173 .setup = hvsi_console_setup,
1174 .flags = CON_PRINTBUFFER,
1175 .index = -1,
1176 };
1177
1178 static int __init hvsi_console_init(void)
1179 {
1180 struct device_node *vty;
1181
1182 hvsi_wait = poll_for_state;
1183
1184
1185 for_each_compatible_node(vty, "serial", "hvterm-protocol") {
1186 struct hvsi_struct *hp;
1187 const __be32 *vtermno, *irq;
1188
1189 vtermno = of_get_property(vty, "reg", NULL);
1190 irq = of_get_property(vty, "interrupts", NULL);
1191 if (!vtermno || !irq)
1192 continue;
1193
1194 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1195 of_node_put(vty);
1196 break;
1197 }
1198
1199 hp = &hvsi_ports[hvsi_count];
1200 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1201 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1202 init_waitqueue_head(&hp->emptyq);
1203 init_waitqueue_head(&hp->stateq);
1204 spin_lock_init(&hp->lock);
1205 tty_port_init(&hp->port);
1206 hp->index = hvsi_count;
1207 hp->inbuf_end = hp->inbuf;
1208 hp->state = HVSI_CLOSED;
1209 hp->vtermno = be32_to_cpup(vtermno);
1210 hp->virq = irq_create_mapping(NULL, be32_to_cpup(irq));
1211 if (hp->virq == 0) {
1212 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1213 __func__, be32_to_cpup(irq));
1214 tty_port_destroy(&hp->port);
1215 continue;
1216 }
1217
1218 hvsi_count++;
1219 }
1220
1221 if (hvsi_count)
1222 register_console(&hvsi_console);
1223 return 0;
1224 }
1225 console_initcall(hvsi_console_init);