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/module.h>
8 #include <linux/string.h>
9 #include <linux/delay.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
16 int vio_ldc_send(struct vio_driver_state
*vio
, void *data
, int len
)
18 int err
, limit
= 1000;
22 err
= ldc_write(vio
->lp
, data
, len
);
23 if (!err
|| (err
!= -EAGAIN
))
30 EXPORT_SYMBOL(vio_ldc_send
);
32 static int send_ctrl(struct vio_driver_state
*vio
,
33 struct vio_msg_tag
*tag
, int len
)
35 tag
->sid
= vio_send_sid(vio
);
36 return vio_ldc_send(vio
, tag
, len
);
39 static void init_tag(struct vio_msg_tag
*tag
, u8 type
, u8 stype
, u16 stype_env
)
43 tag
->stype_env
= stype_env
;
46 static int send_version(struct vio_driver_state
*vio
, u16 major
, u16 minor
)
48 struct vio_ver_info pkt
;
50 vio
->_local_sid
= (u32
) sched_clock();
52 memset(&pkt
, 0, sizeof(pkt
));
53 init_tag(&pkt
.tag
, VIO_TYPE_CTRL
, VIO_SUBTYPE_INFO
, VIO_VER_INFO
);
56 pkt
.dev_class
= vio
->dev_class
;
58 viodbg(HS
, "SEND VERSION INFO maj[%u] min[%u] devclass[%u]\n",
59 major
, minor
, vio
->dev_class
);
61 return send_ctrl(vio
, &pkt
.tag
, sizeof(pkt
));
64 static int start_handshake(struct vio_driver_state
*vio
)
68 viodbg(HS
, "START HANDSHAKE\n");
70 vio
->hs_state
= VIO_HS_INVALID
;
72 err
= send_version(vio
,
73 vio
->ver_table
[0].major
,
74 vio
->ver_table
[0].minor
);
81 void vio_link_state_change(struct vio_driver_state
*vio
, int event
)
83 if (event
== LDC_EVENT_UP
) {
84 vio
->hs_state
= VIO_HS_INVALID
;
86 switch (vio
->dev_class
) {
88 case VDEV_NETWORK_SWITCH
:
89 vio
->dr_state
= (VIO_DR_STATE_TXREQ
|
94 vio
->dr_state
= VIO_DR_STATE_TXREQ
;
96 case VDEV_DISK_SERVER
:
97 vio
->dr_state
= VIO_DR_STATE_RXREQ
;
100 start_handshake(vio
);
103 EXPORT_SYMBOL(vio_link_state_change
);
105 static int handshake_failure(struct vio_driver_state
*vio
)
107 struct vio_dring_state
*dr
;
109 /* XXX Put policy here... Perhaps start a timer to fire
110 * XXX in 100 ms, which will bring the link up and retry
114 viodbg(HS
, "HANDSHAKE FAILURE\n");
116 vio
->dr_state
&= ~(VIO_DR_STATE_TXREG
|
119 dr
= &vio
->drings
[VIO_DRIVER_RX_RING
];
120 memset(dr
, 0, sizeof(*dr
));
122 kfree(vio
->desc_buf
);
123 vio
->desc_buf
= NULL
;
124 vio
->desc_buf_len
= 0;
126 vio
->hs_state
= VIO_HS_INVALID
;
131 static int process_unknown(struct vio_driver_state
*vio
, void *arg
)
133 struct vio_msg_tag
*pkt
= arg
;
135 viodbg(HS
, "UNKNOWN CONTROL [%02x:%02x:%04x:%08x]\n",
136 pkt
->type
, pkt
->stype
, pkt
->stype_env
, pkt
->sid
);
138 printk(KERN_ERR
"vio: ID[%lu] Resetting connection.\n",
141 ldc_disconnect(vio
->lp
);
146 static int send_dreg(struct vio_driver_state
*vio
)
148 struct vio_dring_state
*dr
= &vio
->drings
[VIO_DRIVER_TX_RING
];
150 struct vio_dring_register pkt
;
151 char all
[sizeof(struct vio_dring_register
) +
152 (sizeof(struct ldc_trans_cookie
) *
157 memset(&u
, 0, sizeof(u
));
158 init_tag(&u
.pkt
.tag
, VIO_TYPE_CTRL
, VIO_SUBTYPE_INFO
, VIO_DRING_REG
);
159 u
.pkt
.dring_ident
= 0;
160 u
.pkt
.num_descr
= dr
->num_entries
;
161 u
.pkt
.descr_size
= dr
->entry_size
;
162 u
.pkt
.options
= VIO_TX_DRING
;
163 u
.pkt
.num_cookies
= dr
->ncookies
;
165 viodbg(HS
, "SEND DRING_REG INFO ndesc[%u] dsz[%u] opt[0x%x] "
167 u
.pkt
.num_descr
, u
.pkt
.descr_size
, u
.pkt
.options
,
170 for (i
= 0; i
< dr
->ncookies
; i
++) {
171 u
.pkt
.cookies
[i
] = dr
->cookies
[i
];
173 viodbg(HS
, "DRING COOKIE(%d) [%016llx:%016llx]\n",
175 (unsigned long long) u
.pkt
.cookies
[i
].cookie_addr
,
176 (unsigned long long) u
.pkt
.cookies
[i
].cookie_size
);
179 return send_ctrl(vio
, &u
.pkt
.tag
, sizeof(u
));
182 static int send_rdx(struct vio_driver_state
*vio
)
186 memset(&pkt
, 0, sizeof(pkt
));
188 init_tag(&pkt
.tag
, VIO_TYPE_CTRL
, VIO_SUBTYPE_INFO
, VIO_RDX
);
190 viodbg(HS
, "SEND RDX INFO\n");
192 return send_ctrl(vio
, &pkt
.tag
, sizeof(pkt
));
195 static int send_attr(struct vio_driver_state
*vio
)
197 return vio
->ops
->send_attr(vio
);
200 static struct vio_version
*find_by_major(struct vio_driver_state
*vio
,
203 struct vio_version
*ret
= NULL
;
206 for (i
= 0; i
< vio
->ver_table_entries
; i
++) {
207 struct vio_version
*v
= &vio
->ver_table
[i
];
208 if (v
->major
<= major
) {
216 static int process_ver_info(struct vio_driver_state
*vio
,
217 struct vio_ver_info
*pkt
)
219 struct vio_version
*vap
;
222 viodbg(HS
, "GOT VERSION INFO maj[%u] min[%u] devclass[%u]\n",
223 pkt
->major
, pkt
->minor
, pkt
->dev_class
);
225 if (vio
->hs_state
!= VIO_HS_INVALID
) {
226 /* XXX Perhaps invoke start_handshake? XXX */
227 memset(&vio
->ver
, 0, sizeof(vio
->ver
));
228 vio
->hs_state
= VIO_HS_INVALID
;
231 vap
= find_by_major(vio
, pkt
->major
);
233 vio
->_peer_sid
= pkt
->tag
.sid
;
236 pkt
->tag
.stype
= VIO_SUBTYPE_NACK
;
239 viodbg(HS
, "SEND VERSION NACK maj[0] min[0]\n");
240 err
= send_ctrl(vio
, &pkt
->tag
, sizeof(*pkt
));
241 } else if (vap
->major
!= pkt
->major
) {
242 pkt
->tag
.stype
= VIO_SUBTYPE_NACK
;
243 pkt
->major
= vap
->major
;
244 pkt
->minor
= vap
->minor
;
245 viodbg(HS
, "SEND VERSION NACK maj[%u] min[%u]\n",
246 pkt
->major
, pkt
->minor
);
247 err
= send_ctrl(vio
, &pkt
->tag
, sizeof(*pkt
));
249 struct vio_version ver
= {
253 if (ver
.minor
> vap
->minor
)
254 ver
.minor
= vap
->minor
;
255 pkt
->minor
= ver
.minor
;
256 pkt
->tag
.stype
= VIO_SUBTYPE_ACK
;
257 viodbg(HS
, "SEND VERSION ACK maj[%u] min[%u]\n",
258 pkt
->major
, pkt
->minor
);
259 err
= send_ctrl(vio
, &pkt
->tag
, sizeof(*pkt
));
262 vio
->hs_state
= VIO_HS_GOTVERS
;
266 return handshake_failure(vio
);
271 static int process_ver_ack(struct vio_driver_state
*vio
,
272 struct vio_ver_info
*pkt
)
274 viodbg(HS
, "GOT VERSION ACK maj[%u] min[%u] devclass[%u]\n",
275 pkt
->major
, pkt
->minor
, pkt
->dev_class
);
277 if (vio
->hs_state
& VIO_HS_GOTVERS
) {
278 if (vio
->ver
.major
!= pkt
->major
||
279 vio
->ver
.minor
!= pkt
->minor
) {
280 pkt
->tag
.stype
= VIO_SUBTYPE_NACK
;
281 (void) send_ctrl(vio
, &pkt
->tag
, sizeof(*pkt
));
282 return handshake_failure(vio
);
285 vio
->ver
.major
= pkt
->major
;
286 vio
->ver
.minor
= pkt
->minor
;
287 vio
->hs_state
= VIO_HS_GOTVERS
;
290 switch (vio
->dev_class
) {
293 if (send_attr(vio
) < 0)
294 return handshake_failure(vio
);
304 static int process_ver_nack(struct vio_driver_state
*vio
,
305 struct vio_ver_info
*pkt
)
307 struct vio_version
*nver
;
309 viodbg(HS
, "GOT VERSION NACK maj[%u] min[%u] devclass[%u]\n",
310 pkt
->major
, pkt
->minor
, pkt
->dev_class
);
312 if ((pkt
->major
== 0 && pkt
->minor
== 0) ||
313 !(nver
= find_by_major(vio
, pkt
->major
)))
314 return handshake_failure(vio
);
316 if (send_version(vio
, nver
->major
, nver
->minor
) < 0)
317 return handshake_failure(vio
);
322 static int process_ver(struct vio_driver_state
*vio
, struct vio_ver_info
*pkt
)
324 switch (pkt
->tag
.stype
) {
325 case VIO_SUBTYPE_INFO
:
326 return process_ver_info(vio
, pkt
);
328 case VIO_SUBTYPE_ACK
:
329 return process_ver_ack(vio
, pkt
);
331 case VIO_SUBTYPE_NACK
:
332 return process_ver_nack(vio
, pkt
);
335 return handshake_failure(vio
);
339 static int process_attr(struct vio_driver_state
*vio
, void *pkt
)
343 if (!(vio
->hs_state
& VIO_HS_GOTVERS
))
344 return handshake_failure(vio
);
346 err
= vio
->ops
->handle_attr(vio
, pkt
);
348 return handshake_failure(vio
);
350 vio
->hs_state
|= VIO_HS_GOT_ATTR
;
352 if ((vio
->dr_state
& VIO_DR_STATE_TXREQ
) &&
353 !(vio
->hs_state
& VIO_HS_SENT_DREG
)) {
354 if (send_dreg(vio
) < 0)
355 return handshake_failure(vio
);
357 vio
->hs_state
|= VIO_HS_SENT_DREG
;
363 static int all_drings_registered(struct vio_driver_state
*vio
)
365 int need_rx
, need_tx
;
367 need_rx
= (vio
->dr_state
& VIO_DR_STATE_RXREQ
);
368 need_tx
= (vio
->dr_state
& VIO_DR_STATE_TXREQ
);
371 !(vio
->dr_state
& VIO_DR_STATE_RXREG
))
375 !(vio
->dr_state
& VIO_DR_STATE_TXREG
))
381 static int process_dreg_info(struct vio_driver_state
*vio
,
382 struct vio_dring_register
*pkt
)
384 struct vio_dring_state
*dr
;
387 viodbg(HS
, "GOT DRING_REG INFO ident[%llx] "
388 "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
389 (unsigned long long) pkt
->dring_ident
,
390 pkt
->num_descr
, pkt
->descr_size
, pkt
->options
,
393 if (!(vio
->dr_state
& VIO_DR_STATE_RXREQ
))
396 if (vio
->dr_state
& VIO_DR_STATE_RXREG
)
399 vio
->desc_buf
= kzalloc(pkt
->descr_size
, GFP_ATOMIC
);
403 vio
->desc_buf_len
= pkt
->descr_size
;
405 dr
= &vio
->drings
[VIO_DRIVER_RX_RING
];
407 dr
->num_entries
= pkt
->num_descr
;
408 dr
->entry_size
= pkt
->descr_size
;
409 dr
->ncookies
= pkt
->num_cookies
;
410 for (i
= 0; i
< dr
->ncookies
; i
++) {
411 dr
->cookies
[i
] = pkt
->cookies
[i
];
413 viodbg(HS
, "DRING COOKIE(%d) [%016llx:%016llx]\n",
416 pkt
->cookies
[i
].cookie_addr
,
418 pkt
->cookies
[i
].cookie_size
);
421 pkt
->tag
.stype
= VIO_SUBTYPE_ACK
;
422 pkt
->dring_ident
= ++dr
->ident
;
424 viodbg(HS
, "SEND DRING_REG ACK ident[%llx]\n",
425 (unsigned long long) pkt
->dring_ident
);
427 len
= (sizeof(*pkt
) +
428 (dr
->ncookies
* sizeof(struct ldc_trans_cookie
)));
429 if (send_ctrl(vio
, &pkt
->tag
, len
) < 0)
432 vio
->dr_state
|= VIO_DR_STATE_RXREG
;
437 pkt
->tag
.stype
= VIO_SUBTYPE_NACK
;
438 viodbg(HS
, "SEND DRING_REG NACK\n");
439 (void) send_ctrl(vio
, &pkt
->tag
, sizeof(*pkt
));
441 return handshake_failure(vio
);
444 static int process_dreg_ack(struct vio_driver_state
*vio
,
445 struct vio_dring_register
*pkt
)
447 struct vio_dring_state
*dr
;
449 viodbg(HS
, "GOT DRING_REG ACK ident[%llx] "
450 "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
451 (unsigned long long) pkt
->dring_ident
,
452 pkt
->num_descr
, pkt
->descr_size
, pkt
->options
,
455 dr
= &vio
->drings
[VIO_DRIVER_TX_RING
];
457 if (!(vio
->dr_state
& VIO_DR_STATE_TXREQ
))
458 return handshake_failure(vio
);
460 dr
->ident
= pkt
->dring_ident
;
461 vio
->dr_state
|= VIO_DR_STATE_TXREG
;
463 if (all_drings_registered(vio
)) {
464 if (send_rdx(vio
) < 0)
465 return handshake_failure(vio
);
466 vio
->hs_state
= VIO_HS_SENT_RDX
;
471 static int process_dreg_nack(struct vio_driver_state
*vio
,
472 struct vio_dring_register
*pkt
)
474 viodbg(HS
, "GOT DRING_REG NACK ident[%llx] "
475 "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
476 (unsigned long long) pkt
->dring_ident
,
477 pkt
->num_descr
, pkt
->descr_size
, pkt
->options
,
480 return handshake_failure(vio
);
483 static int process_dreg(struct vio_driver_state
*vio
,
484 struct vio_dring_register
*pkt
)
486 if (!(vio
->hs_state
& VIO_HS_GOTVERS
))
487 return handshake_failure(vio
);
489 switch (pkt
->tag
.stype
) {
490 case VIO_SUBTYPE_INFO
:
491 return process_dreg_info(vio
, pkt
);
493 case VIO_SUBTYPE_ACK
:
494 return process_dreg_ack(vio
, pkt
);
496 case VIO_SUBTYPE_NACK
:
497 return process_dreg_nack(vio
, pkt
);
500 return handshake_failure(vio
);
504 static int process_dunreg(struct vio_driver_state
*vio
,
505 struct vio_dring_unregister
*pkt
)
507 struct vio_dring_state
*dr
= &vio
->drings
[VIO_DRIVER_RX_RING
];
509 viodbg(HS
, "GOT DRING_UNREG\n");
511 if (pkt
->dring_ident
!= dr
->ident
)
514 vio
->dr_state
&= ~VIO_DR_STATE_RXREG
;
516 memset(dr
, 0, sizeof(*dr
));
518 kfree(vio
->desc_buf
);
519 vio
->desc_buf
= NULL
;
520 vio
->desc_buf_len
= 0;
525 static int process_rdx_info(struct vio_driver_state
*vio
, struct vio_rdx
*pkt
)
527 viodbg(HS
, "GOT RDX INFO\n");
529 pkt
->tag
.stype
= VIO_SUBTYPE_ACK
;
530 viodbg(HS
, "SEND RDX ACK\n");
531 if (send_ctrl(vio
, &pkt
->tag
, sizeof(*pkt
)) < 0)
532 return handshake_failure(vio
);
534 vio
->hs_state
|= VIO_HS_SENT_RDX_ACK
;
538 static int process_rdx_ack(struct vio_driver_state
*vio
, struct vio_rdx
*pkt
)
540 viodbg(HS
, "GOT RDX ACK\n");
542 if (!(vio
->hs_state
& VIO_HS_SENT_RDX
))
543 return handshake_failure(vio
);
545 vio
->hs_state
|= VIO_HS_GOT_RDX_ACK
;
549 static int process_rdx_nack(struct vio_driver_state
*vio
, struct vio_rdx
*pkt
)
551 viodbg(HS
, "GOT RDX NACK\n");
553 return handshake_failure(vio
);
556 static int process_rdx(struct vio_driver_state
*vio
, struct vio_rdx
*pkt
)
558 if (!all_drings_registered(vio
))
559 handshake_failure(vio
);
561 switch (pkt
->tag
.stype
) {
562 case VIO_SUBTYPE_INFO
:
563 return process_rdx_info(vio
, pkt
);
565 case VIO_SUBTYPE_ACK
:
566 return process_rdx_ack(vio
, pkt
);
568 case VIO_SUBTYPE_NACK
:
569 return process_rdx_nack(vio
, pkt
);
572 return handshake_failure(vio
);
576 int vio_control_pkt_engine(struct vio_driver_state
*vio
, void *pkt
)
578 struct vio_msg_tag
*tag
= pkt
;
579 u8 prev_state
= vio
->hs_state
;
582 switch (tag
->stype_env
) {
584 err
= process_ver(vio
, pkt
);
588 err
= process_attr(vio
, pkt
);
592 err
= process_dreg(vio
, pkt
);
595 case VIO_DRING_UNREG
:
596 err
= process_dunreg(vio
, pkt
);
600 err
= process_rdx(vio
, pkt
);
604 err
= process_unknown(vio
, pkt
);
608 vio
->hs_state
!= prev_state
&&
609 (vio
->hs_state
& VIO_HS_COMPLETE
))
610 vio
->ops
->handshake_complete(vio
);
614 EXPORT_SYMBOL(vio_control_pkt_engine
);
616 void vio_conn_reset(struct vio_driver_state
*vio
)
619 EXPORT_SYMBOL(vio_conn_reset
);
621 /* The issue is that the Solaris virtual disk server just mirrors the
622 * SID values it gets from the client peer. So we work around that
623 * here in vio_{validate,send}_sid() so that the drivers don't need
624 * to be aware of this crap.
626 int vio_validate_sid(struct vio_driver_state
*vio
, struct vio_msg_tag
*tp
)
630 /* Always let VERSION+INFO packets through unchecked, they
631 * define the new SID.
633 if (tp
->type
== VIO_TYPE_CTRL
&&
634 tp
->stype
== VIO_SUBTYPE_INFO
&&
635 tp
->stype_env
== VIO_VER_INFO
)
638 /* Ok, now figure out which SID to use. */
639 switch (vio
->dev_class
) {
641 case VDEV_NETWORK_SWITCH
:
642 case VDEV_DISK_SERVER
:
644 sid
= vio
->_peer_sid
;
648 sid
= vio
->_local_sid
;
654 viodbg(DATA
, "BAD SID tag->sid[%08x] peer_sid[%08x] local_sid[%08x]\n",
655 tp
->sid
, vio
->_peer_sid
, vio
->_local_sid
);
658 EXPORT_SYMBOL(vio_validate_sid
);
660 u32
vio_send_sid(struct vio_driver_state
*vio
)
662 switch (vio
->dev_class
) {
664 case VDEV_NETWORK_SWITCH
:
667 return vio
->_local_sid
;
669 case VDEV_DISK_SERVER
:
670 return vio
->_peer_sid
;
673 EXPORT_SYMBOL(vio_send_sid
);
675 extern int vio_ldc_alloc(struct vio_driver_state
*vio
,
676 struct ldc_channel_config
*base_cfg
,
679 struct ldc_channel_config cfg
= *base_cfg
;
680 struct ldc_channel
*lp
;
683 id
= md_get_property(vio
->endpoint
, "id", NULL
);
685 printk(KERN_ERR
"%s: Channel lacks id property.\n",
690 vio
->channel_id
= *id
;
692 cfg
.rx_irq
= vio
->rx_irq
;
693 cfg
.tx_irq
= vio
->tx_irq
;
695 lp
= ldc_alloc(vio
->channel_id
, &cfg
, event_arg
);
703 EXPORT_SYMBOL(vio_ldc_alloc
);
705 void vio_ldc_free(struct vio_driver_state
*vio
)
710 kfree(vio
->desc_buf
);
711 vio
->desc_buf
= NULL
;
712 vio
->desc_buf_len
= 0;
714 EXPORT_SYMBOL(vio_ldc_free
);
716 void vio_port_up(struct vio_driver_state
*vio
)
721 spin_lock_irqsave(&vio
->lock
, flags
);
723 state
= ldc_state(vio
->lp
);
726 if (state
== LDC_STATE_INIT
) {
727 err
= ldc_bind(vio
->lp
, vio
->name
);
729 printk(KERN_WARNING
"%s: Port %lu bind failed, "
731 vio
->name
, vio
->channel_id
, err
);
735 err
= ldc_connect(vio
->lp
);
737 printk(KERN_WARNING
"%s: Port %lu connect failed, "
739 vio
->name
, vio
->channel_id
, err
);
742 unsigned long expires
= jiffies
+ HZ
;
744 expires
= round_jiffies(expires
);
745 mod_timer(&vio
->timer
, expires
);
748 spin_unlock_irqrestore(&vio
->lock
, flags
);
750 EXPORT_SYMBOL(vio_port_up
);
752 static void vio_port_timer(unsigned long _arg
)
754 struct vio_driver_state
*vio
= (struct vio_driver_state
*) _arg
;
759 int vio_driver_init(struct vio_driver_state
*vio
, struct vio_dev
*vdev
,
760 u8 dev_class
, struct mdesc_node
*channel_endpoint
,
761 struct vio_version
*ver_table
, int ver_table_size
,
762 struct vio_driver_ops
*ops
, char *name
)
766 case VDEV_NETWORK_SWITCH
:
768 case VDEV_DISK_SERVER
:
775 if (!ops
->send_attr
||
777 !ops
->handshake_complete
)
780 if (!channel_endpoint
)
783 if (!ver_table
|| ver_table_size
< 0)
789 spin_lock_init(&vio
->lock
);
793 vio
->dev_class
= dev_class
;
796 vio
->endpoint
= channel_endpoint
;
797 vio
->tx_irq
= channel_endpoint
->irqs
[0];
798 vio
->rx_irq
= channel_endpoint
->irqs
[1];
800 vio
->ver_table
= ver_table
;
801 vio
->ver_table_entries
= ver_table_size
;
805 setup_timer(&vio
->timer
, vio_port_timer
, (unsigned long) vio
);
809 EXPORT_SYMBOL(vio_driver_init
);