1 /* viohs.c: LDOM Virtual I/O handshake helper layer.
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
6 #include <linux/kernel.h>
7 #include <linux/export.h>
8 #include <linux/string.h>
9 #include <linux/delay.h>
10 #include <linux/sched.h>
11 #include <linux/sched/clock.h>
12 #include <linux/slab.h>
17 int vio_ldc_send(struct vio_driver_state *vio, void *data, int len)
19 int err, limit = 1000;
23 err = ldc_write(vio->lp, data, len);
24 if (!err || (err != -EAGAIN))
31 EXPORT_SYMBOL(vio_ldc_send);
33 static int send_ctrl(struct vio_driver_state *vio,
34 struct vio_msg_tag *tag, int len)
36 tag->sid = vio_send_sid(vio);
37 return vio_ldc_send(vio, tag, len);
40 static void init_tag(struct vio_msg_tag *tag, u8 type, u8 stype, u16 stype_env)
44 tag->stype_env = stype_env;
47 static int send_version(struct vio_driver_state *vio, u16 major, u16 minor)
49 struct vio_ver_info pkt;
51 vio->_local_sid = (u32) sched_clock();
53 memset(&pkt, 0, sizeof(pkt));
54 init_tag(&pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_VER_INFO);
57 pkt.dev_class = vio->dev_class;
59 viodbg(HS, "SEND VERSION INFO maj[%u] min[%u] devclass[%u]\n",
60 major, minor, vio->dev_class);
62 return send_ctrl(vio, &pkt.tag, sizeof(pkt));
65 static int start_handshake(struct vio_driver_state *vio)
69 viodbg(HS, "START HANDSHAKE\n");
71 vio->hs_state = VIO_HS_INVALID;
73 err = send_version(vio,
74 vio->ver_table[0].major,
75 vio->ver_table[0].minor);
82 static void flush_rx_dring(struct vio_driver_state *vio)
84 struct vio_dring_state *dr;
87 BUG_ON(!(vio->dr_state & VIO_DR_STATE_RXREG));
89 dr = &vio->drings[VIO_DRIVER_RX_RING];
92 BUG_ON(!vio->desc_buf);
96 memset(dr, 0, sizeof(*dr));
100 void vio_link_state_change(struct vio_driver_state *vio, int event)
102 if (event == LDC_EVENT_UP) {
103 vio->hs_state = VIO_HS_INVALID;
105 switch (vio->dev_class) {
107 case VDEV_NETWORK_SWITCH:
108 vio->dr_state = (VIO_DR_STATE_TXREQ |
113 vio->dr_state = VIO_DR_STATE_TXREQ;
115 case VDEV_DISK_SERVER:
116 vio->dr_state = VIO_DR_STATE_RXREQ;
119 start_handshake(vio);
120 } else if (event == LDC_EVENT_RESET) {
121 vio->hs_state = VIO_HS_INVALID;
123 if (vio->dr_state & VIO_DR_STATE_RXREG)
126 vio->dr_state = 0x00;
127 memset(&vio->ver, 0, sizeof(vio->ver));
129 ldc_disconnect(vio->lp);
132 EXPORT_SYMBOL(vio_link_state_change);
134 static int handshake_failure(struct vio_driver_state *vio)
136 struct vio_dring_state *dr;
138 /* XXX Put policy here... Perhaps start a timer to fire
139 * XXX in 100 ms, which will bring the link up and retry
143 viodbg(HS, "HANDSHAKE FAILURE\n");
145 vio->dr_state &= ~(VIO_DR_STATE_TXREG |
148 dr = &vio->drings[VIO_DRIVER_RX_RING];
149 memset(dr, 0, sizeof(*dr));
151 kfree(vio->desc_buf);
152 vio->desc_buf = NULL;
153 vio->desc_buf_len = 0;
155 vio->hs_state = VIO_HS_INVALID;
160 static int process_unknown(struct vio_driver_state *vio, void *arg)
162 struct vio_msg_tag *pkt = arg;
164 viodbg(HS, "UNKNOWN CONTROL [%02x:%02x:%04x:%08x]\n",
165 pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
167 printk(KERN_ERR "vio: ID[%lu] Resetting connection.\n",
168 vio->vdev->channel_id);
170 ldc_disconnect(vio->lp);
175 static int send_dreg(struct vio_driver_state *vio)
177 struct vio_dring_state *dr = &vio->drings[VIO_DRIVER_TX_RING];
179 struct vio_dring_register pkt;
180 char all[sizeof(struct vio_dring_register) +
181 (sizeof(struct ldc_trans_cookie) *
186 memset(&u, 0, sizeof(u));
187 init_tag(&u.pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_DRING_REG);
188 u.pkt.dring_ident = 0;
189 u.pkt.num_descr = dr->num_entries;
190 u.pkt.descr_size = dr->entry_size;
191 u.pkt.options = VIO_TX_DRING;
192 u.pkt.num_cookies = dr->ncookies;
194 viodbg(HS, "SEND DRING_REG INFO ndesc[%u] dsz[%u] opt[0x%x] "
196 u.pkt.num_descr, u.pkt.descr_size, u.pkt.options,
199 for (i = 0; i < dr->ncookies; i++) {
200 u.pkt.cookies[i] = dr->cookies[i];
202 viodbg(HS, "DRING COOKIE(%d) [%016llx:%016llx]\n",
204 (unsigned long long) u.pkt.cookies[i].cookie_addr,
205 (unsigned long long) u.pkt.cookies[i].cookie_size);
208 return send_ctrl(vio, &u.pkt.tag, sizeof(u));
211 static int send_rdx(struct vio_driver_state *vio)
215 memset(&pkt, 0, sizeof(pkt));
217 init_tag(&pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX);
219 viodbg(HS, "SEND RDX INFO\n");
221 return send_ctrl(vio, &pkt.tag, sizeof(pkt));
224 static int send_attr(struct vio_driver_state *vio)
229 return vio->ops->send_attr(vio);
232 static struct vio_version *find_by_major(struct vio_driver_state *vio,
235 struct vio_version *ret = NULL;
238 for (i = 0; i < vio->ver_table_entries; i++) {
239 struct vio_version *v = &vio->ver_table[i];
240 if (v->major <= major) {
248 static int process_ver_info(struct vio_driver_state *vio,
249 struct vio_ver_info *pkt)
251 struct vio_version *vap;
254 viodbg(HS, "GOT VERSION INFO maj[%u] min[%u] devclass[%u]\n",
255 pkt->major, pkt->minor, pkt->dev_class);
257 if (vio->hs_state != VIO_HS_INVALID) {
258 /* XXX Perhaps invoke start_handshake? XXX */
259 memset(&vio->ver, 0, sizeof(vio->ver));
260 vio->hs_state = VIO_HS_INVALID;
263 vap = find_by_major(vio, pkt->major);
265 vio->_peer_sid = pkt->tag.sid;
268 pkt->tag.stype = VIO_SUBTYPE_NACK;
271 viodbg(HS, "SEND VERSION NACK maj[0] min[0]\n");
272 err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
273 } else if (vap->major != pkt->major) {
274 pkt->tag.stype = VIO_SUBTYPE_NACK;
275 pkt->major = vap->major;
276 pkt->minor = vap->minor;
277 viodbg(HS, "SEND VERSION NACK maj[%u] min[%u]\n",
278 pkt->major, pkt->minor);
279 err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
281 struct vio_version ver = {
285 if (ver.minor > vap->minor)
286 ver.minor = vap->minor;
287 pkt->minor = ver.minor;
288 pkt->tag.stype = VIO_SUBTYPE_ACK;
289 pkt->dev_class = vio->dev_class;
290 viodbg(HS, "SEND VERSION ACK maj[%u] min[%u]\n",
291 pkt->major, pkt->minor);
292 err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
295 vio->hs_state = VIO_HS_GOTVERS;
299 return handshake_failure(vio);
304 static int process_ver_ack(struct vio_driver_state *vio,
305 struct vio_ver_info *pkt)
307 viodbg(HS, "GOT VERSION ACK maj[%u] min[%u] devclass[%u]\n",
308 pkt->major, pkt->minor, pkt->dev_class);
310 if (vio->hs_state & VIO_HS_GOTVERS) {
311 if (vio->ver.major != pkt->major ||
312 vio->ver.minor != pkt->minor) {
313 pkt->tag.stype = VIO_SUBTYPE_NACK;
314 (void) send_ctrl(vio, &pkt->tag, sizeof(*pkt));
315 return handshake_failure(vio);
318 vio->ver.major = pkt->major;
319 vio->ver.minor = pkt->minor;
320 vio->hs_state = VIO_HS_GOTVERS;
323 switch (vio->dev_class) {
326 if (send_attr(vio) < 0)
327 return handshake_failure(vio);
337 static int process_ver_nack(struct vio_driver_state *vio,
338 struct vio_ver_info *pkt)
340 struct vio_version *nver;
342 viodbg(HS, "GOT VERSION NACK maj[%u] min[%u] devclass[%u]\n",
343 pkt->major, pkt->minor, pkt->dev_class);
345 if (pkt->major == 0 && pkt->minor == 0)
346 return handshake_failure(vio);
347 nver = find_by_major(vio, pkt->major);
349 return handshake_failure(vio);
351 if (send_version(vio, nver->major, nver->minor) < 0)
352 return handshake_failure(vio);
357 static int process_ver(struct vio_driver_state *vio, struct vio_ver_info *pkt)
359 switch (pkt->tag.stype) {
360 case VIO_SUBTYPE_INFO:
361 return process_ver_info(vio, pkt);
363 case VIO_SUBTYPE_ACK:
364 return process_ver_ack(vio, pkt);
366 case VIO_SUBTYPE_NACK:
367 return process_ver_nack(vio, pkt);
370 return handshake_failure(vio);
374 static int process_attr(struct vio_driver_state *vio, void *pkt)
378 if (!(vio->hs_state & VIO_HS_GOTVERS))
379 return handshake_failure(vio);
384 err = vio->ops->handle_attr(vio, pkt);
386 return handshake_failure(vio);
388 vio->hs_state |= VIO_HS_GOT_ATTR;
390 if ((vio->dr_state & VIO_DR_STATE_TXREQ) &&
391 !(vio->hs_state & VIO_HS_SENT_DREG)) {
392 if (send_dreg(vio) < 0)
393 return handshake_failure(vio);
395 vio->hs_state |= VIO_HS_SENT_DREG;
402 static int all_drings_registered(struct vio_driver_state *vio)
404 int need_rx, need_tx;
406 need_rx = (vio->dr_state & VIO_DR_STATE_RXREQ);
407 need_tx = (vio->dr_state & VIO_DR_STATE_TXREQ);
410 !(vio->dr_state & VIO_DR_STATE_RXREG))
414 !(vio->dr_state & VIO_DR_STATE_TXREG))
420 static int process_dreg_info(struct vio_driver_state *vio,
421 struct vio_dring_register *pkt)
423 struct vio_dring_state *dr;
426 viodbg(HS, "GOT DRING_REG INFO ident[%llx] "
427 "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
428 (unsigned long long) pkt->dring_ident,
429 pkt->num_descr, pkt->descr_size, pkt->options,
432 if (!(vio->dr_state & VIO_DR_STATE_RXREQ))
435 if (vio->dr_state & VIO_DR_STATE_RXREG)
438 /* v1.6 and higher, ACK with desired, supported mode, or NACK */
439 if (vio_version_after_eq(vio, 1, 6)) {
440 if (!(pkt->options & VIO_TX_DRING))
442 pkt->options = VIO_TX_DRING;
445 BUG_ON(vio->desc_buf);
447 vio->desc_buf = kzalloc(pkt->descr_size, GFP_ATOMIC);
451 vio->desc_buf_len = pkt->descr_size;
453 dr = &vio->drings[VIO_DRIVER_RX_RING];
455 dr->num_entries = pkt->num_descr;
456 dr->entry_size = pkt->descr_size;
457 dr->ncookies = pkt->num_cookies;
458 for (i = 0; i < dr->ncookies; i++) {
459 dr->cookies[i] = pkt->cookies[i];
461 viodbg(HS, "DRING COOKIE(%d) [%016llx:%016llx]\n",
464 pkt->cookies[i].cookie_addr,
466 pkt->cookies[i].cookie_size);
469 pkt->tag.stype = VIO_SUBTYPE_ACK;
470 pkt->dring_ident = ++dr->ident;
472 viodbg(HS, "SEND DRING_REG ACK ident[%llx] "
473 "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
474 (unsigned long long) pkt->dring_ident,
475 pkt->num_descr, pkt->descr_size, pkt->options,
478 len = (sizeof(*pkt) +
479 (dr->ncookies * sizeof(struct ldc_trans_cookie)));
480 if (send_ctrl(vio, &pkt->tag, len) < 0)
483 vio->dr_state |= VIO_DR_STATE_RXREG;
488 pkt->tag.stype = VIO_SUBTYPE_NACK;
489 viodbg(HS, "SEND DRING_REG NACK\n");
490 (void) send_ctrl(vio, &pkt->tag, sizeof(*pkt));
492 return handshake_failure(vio);
495 static int process_dreg_ack(struct vio_driver_state *vio,
496 struct vio_dring_register *pkt)
498 struct vio_dring_state *dr;
500 viodbg(HS, "GOT DRING_REG ACK ident[%llx] "
501 "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
502 (unsigned long long) pkt->dring_ident,
503 pkt->num_descr, pkt->descr_size, pkt->options,
506 dr = &vio->drings[VIO_DRIVER_TX_RING];
508 if (!(vio->dr_state & VIO_DR_STATE_TXREQ))
509 return handshake_failure(vio);
511 dr->ident = pkt->dring_ident;
512 vio->dr_state |= VIO_DR_STATE_TXREG;
514 if (all_drings_registered(vio)) {
515 if (send_rdx(vio) < 0)
516 return handshake_failure(vio);
517 vio->hs_state = VIO_HS_SENT_RDX;
522 static int process_dreg_nack(struct vio_driver_state *vio,
523 struct vio_dring_register *pkt)
525 viodbg(HS, "GOT DRING_REG NACK ident[%llx] "
526 "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
527 (unsigned long long) pkt->dring_ident,
528 pkt->num_descr, pkt->descr_size, pkt->options,
531 return handshake_failure(vio);
534 static int process_dreg(struct vio_driver_state *vio,
535 struct vio_dring_register *pkt)
537 if (!(vio->hs_state & VIO_HS_GOTVERS))
538 return handshake_failure(vio);
540 switch (pkt->tag.stype) {
541 case VIO_SUBTYPE_INFO:
542 return process_dreg_info(vio, pkt);
544 case VIO_SUBTYPE_ACK:
545 return process_dreg_ack(vio, pkt);
547 case VIO_SUBTYPE_NACK:
548 return process_dreg_nack(vio, pkt);
551 return handshake_failure(vio);
555 static int process_dunreg(struct vio_driver_state *vio,
556 struct vio_dring_unregister *pkt)
558 struct vio_dring_state *dr = &vio->drings[VIO_DRIVER_RX_RING];
560 viodbg(HS, "GOT DRING_UNREG\n");
562 if (pkt->dring_ident != dr->ident)
565 vio->dr_state &= ~VIO_DR_STATE_RXREG;
567 memset(dr, 0, sizeof(*dr));
569 kfree(vio->desc_buf);
570 vio->desc_buf = NULL;
571 vio->desc_buf_len = 0;
576 static int process_rdx_info(struct vio_driver_state *vio, struct vio_rdx *pkt)
578 viodbg(HS, "GOT RDX INFO\n");
580 pkt->tag.stype = VIO_SUBTYPE_ACK;
581 viodbg(HS, "SEND RDX ACK\n");
582 if (send_ctrl(vio, &pkt->tag, sizeof(*pkt)) < 0)
583 return handshake_failure(vio);
585 vio->hs_state |= VIO_HS_SENT_RDX_ACK;
589 static int process_rdx_ack(struct vio_driver_state *vio, struct vio_rdx *pkt)
591 viodbg(HS, "GOT RDX ACK\n");
593 if (!(vio->hs_state & VIO_HS_SENT_RDX))
594 return handshake_failure(vio);
596 vio->hs_state |= VIO_HS_GOT_RDX_ACK;
600 static int process_rdx_nack(struct vio_driver_state *vio, struct vio_rdx *pkt)
602 viodbg(HS, "GOT RDX NACK\n");
604 return handshake_failure(vio);
607 static int process_rdx(struct vio_driver_state *vio, struct vio_rdx *pkt)
609 if (!all_drings_registered(vio))
610 handshake_failure(vio);
612 switch (pkt->tag.stype) {
613 case VIO_SUBTYPE_INFO:
614 return process_rdx_info(vio, pkt);
616 case VIO_SUBTYPE_ACK:
617 return process_rdx_ack(vio, pkt);
619 case VIO_SUBTYPE_NACK:
620 return process_rdx_nack(vio, pkt);
623 return handshake_failure(vio);
627 int vio_control_pkt_engine(struct vio_driver_state *vio, void *pkt)
629 struct vio_msg_tag *tag = pkt;
630 u8 prev_state = vio->hs_state;
633 switch (tag->stype_env) {
635 err = process_ver(vio, pkt);
639 err = process_attr(vio, pkt);
643 err = process_dreg(vio, pkt);
646 case VIO_DRING_UNREG:
647 err = process_dunreg(vio, pkt);
651 err = process_rdx(vio, pkt);
655 err = process_unknown(vio, pkt);
660 vio->hs_state != prev_state &&
661 (vio->hs_state & VIO_HS_COMPLETE)) {
663 vio->ops->handshake_complete(vio);
668 EXPORT_SYMBOL(vio_control_pkt_engine);
670 void vio_conn_reset(struct vio_driver_state *vio)
673 EXPORT_SYMBOL(vio_conn_reset);
675 /* The issue is that the Solaris virtual disk server just mirrors the
676 * SID values it gets from the client peer. So we work around that
677 * here in vio_{validate,send}_sid() so that the drivers don't need
678 * to be aware of this crap.
680 int vio_validate_sid(struct vio_driver_state *vio, struct vio_msg_tag *tp)
684 /* Always let VERSION+INFO packets through unchecked, they
685 * define the new SID.
687 if (tp->type == VIO_TYPE_CTRL &&
688 tp->stype == VIO_SUBTYPE_INFO &&
689 tp->stype_env == VIO_VER_INFO)
692 /* Ok, now figure out which SID to use. */
693 switch (vio->dev_class) {
695 case VDEV_NETWORK_SWITCH:
696 case VDEV_DISK_SERVER:
698 sid = vio->_peer_sid;
702 sid = vio->_local_sid;
708 viodbg(DATA, "BAD SID tag->sid[%08x] peer_sid[%08x] local_sid[%08x]\n",
709 tp->sid, vio->_peer_sid, vio->_local_sid);
712 EXPORT_SYMBOL(vio_validate_sid);
714 u32 vio_send_sid(struct vio_driver_state *vio)
716 switch (vio->dev_class) {
718 case VDEV_NETWORK_SWITCH:
721 return vio->_local_sid;
723 case VDEV_DISK_SERVER:
724 return vio->_peer_sid;
727 EXPORT_SYMBOL(vio_send_sid);
729 int vio_ldc_alloc(struct vio_driver_state *vio,
730 struct ldc_channel_config *base_cfg,
733 struct ldc_channel_config cfg = *base_cfg;
734 struct ldc_channel *lp;
736 cfg.tx_irq = vio->vdev->tx_irq;
737 cfg.rx_irq = vio->vdev->rx_irq;
739 lp = ldc_alloc(vio->vdev->channel_id, &cfg, event_arg, vio->name);
747 EXPORT_SYMBOL(vio_ldc_alloc);
749 void vio_ldc_free(struct vio_driver_state *vio)
754 kfree(vio->desc_buf);
755 vio->desc_buf = NULL;
756 vio->desc_buf_len = 0;
758 EXPORT_SYMBOL(vio_ldc_free);
760 void vio_port_up(struct vio_driver_state *vio)
765 spin_lock_irqsave(&vio->lock, flags);
767 state = ldc_state(vio->lp);
770 if (state == LDC_STATE_INIT) {
771 err = ldc_bind(vio->lp);
773 printk(KERN_WARNING "%s: Port %lu bind failed, "
775 vio->name, vio->vdev->channel_id, err);
779 if (ldc_mode(vio->lp) == LDC_MODE_RAW)
780 ldc_set_state(vio->lp, LDC_STATE_CONNECTED);
782 err = ldc_connect(vio->lp);
785 printk(KERN_WARNING "%s: Port %lu connect failed, "
787 vio->name, vio->vdev->channel_id, err);
790 unsigned long expires = jiffies + HZ;
792 expires = round_jiffies(expires);
793 mod_timer(&vio->timer, expires);
796 spin_unlock_irqrestore(&vio->lock, flags);
798 EXPORT_SYMBOL(vio_port_up);
800 static void vio_port_timer(unsigned long _arg)
802 struct vio_driver_state *vio = (struct vio_driver_state *) _arg;
807 int vio_driver_init(struct vio_driver_state *vio, struct vio_dev *vdev,
808 u8 dev_class, struct vio_version *ver_table,
809 int ver_table_size, struct vio_driver_ops *ops,
814 case VDEV_NETWORK_SWITCH:
816 case VDEV_DISK_SERVER:
817 case VDEV_CONSOLE_CON:
824 if (dev_class == VDEV_NETWORK ||
825 dev_class == VDEV_NETWORK_SWITCH ||
826 dev_class == VDEV_DISK ||
827 dev_class == VDEV_DISK_SERVER) {
828 if (!ops || !ops->send_attr || !ops->handle_attr ||
829 !ops->handshake_complete)
833 if (!ver_table || ver_table_size < 0)
839 spin_lock_init(&vio->lock);
843 vio->dev_class = dev_class;
846 vio->ver_table = ver_table;
847 vio->ver_table_entries = ver_table_size;
851 setup_timer(&vio->timer, vio_port_timer, (unsigned long) vio);
855 EXPORT_SYMBOL(vio_driver_init);