10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7492SZhigang.Lu@Sun.COM * Common Development and Distribution License (the "License"). 6*7492SZhigang.Lu@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*7492SZhigang.Lu@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _SYS_USB_USBSER_USBSER_RSEQ_H 270Sstevel@tonic-gate #define _SYS_USB_USBSER_USBSER_RSEQ_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * Reversible sequence (rseq) is a data-driven mechanism to execute several 320Sstevel@tonic-gate * subfunctions, called steps, and subsequently execute them in the reverse 330Sstevel@tonic-gate * order - these opposite actions are further referred to as 'do' and 'undo'. 340Sstevel@tonic-gate * If one of the intermediate steps fails, the previously executed steps are 350Sstevel@tonic-gate * undone in reverse order. Debugging facilities are also provided. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * rseq is primarily aimed to simplify multistep driver attach()/detach() 380Sstevel@tonic-gate * implementations, where each step can potentially fail and undoing previous 390Sstevel@tonic-gate * ones typically involve either goto's or bit-fields (indicating what has been 400Sstevel@tonic-gate * done so far). 410Sstevel@tonic-gate */ 420Sstevel@tonic-gate 430Sstevel@tonic-gate #include <sys/types.h> 440Sstevel@tonic-gate #include <sys/note.h> 450Sstevel@tonic-gate 460Sstevel@tonic-gate #ifdef __cplusplus 470Sstevel@tonic-gate extern "C" { 480Sstevel@tonic-gate #endif 490Sstevel@tonic-gate 500Sstevel@tonic-gate typedef struct rseq rseq_t; 510Sstevel@tonic-gate 520Sstevel@tonic-gate /* 530Sstevel@tonic-gate * rseq function type 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * uintptr_t is used to accomodate both integer and pointer argument types 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate typedef uintptr_t (*rseq_func_t)(uintptr_t); 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* step callback is called after each step */ 600Sstevel@tonic-gate typedef int (*rseq_cb_t)(rseq_t *rseq, int num, uintptr_t arg); 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* values returned by step callback */ 630Sstevel@tonic-gate enum { 640Sstevel@tonic-gate RSEQ_OK = 0, /* continue to execute steps */ 650Sstevel@tonic-gate RSEQ_UNDO = 1, /* rseq_do() only: step failed, undo all */ 660Sstevel@tonic-gate RSEQ_ABORT = 2 /* stop rseq execution and return immediately */ 670Sstevel@tonic-gate }; 680Sstevel@tonic-gate 690Sstevel@tonic-gate /* 700Sstevel@tonic-gate * rseq step 710Sstevel@tonic-gate */ 720Sstevel@tonic-gate typedef struct rseq_step { 730Sstevel@tonic-gate rseq_func_t s_func; /* step function; ignored if NULL */ 740Sstevel@tonic-gate char *s_name; /* step name string */ 750Sstevel@tonic-gate rseq_cb_t s_cb; /* step callback; NULL is equivalent */ 760Sstevel@tonic-gate /* to a callback returning RSEQ_OK */ 770Sstevel@tonic-gate uintptr_t s_rval; /* s_func's return value */ 780Sstevel@tonic-gate } rseq_step_t; 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* 810Sstevel@tonic-gate * rseq entry 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate struct rseq { 840Sstevel@tonic-gate rseq_step_t r_do; /* do step */ 850Sstevel@tonic-gate rseq_step_t r_undo; /* undo step */ 860Sstevel@tonic-gate }; 870Sstevel@tonic-gate 880Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("one per call", rseq rseq_step)) 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* 910Sstevel@tonic-gate * rseq_do(), rseq_undo() 920Sstevel@tonic-gate * 930Sstevel@tonic-gate * Arguments: 940Sstevel@tonic-gate * rseq - array of rseq entries; 950Sstevel@tonic-gate * num - number of entries in the array; 960Sstevel@tonic-gate * arg - argument passed to the step functions; 970Sstevel@tonic-gate * flags - should be 0, no flags defined yet; 980Sstevel@tonic-gate * 990Sstevel@tonic-gate * Return values: 1000Sstevel@tonic-gate * If an intermediate step failed, value returned by respective callback. 1010Sstevel@tonic-gate * Otherwise RSEQ_OK. 1020Sstevel@tonic-gate */ 1030Sstevel@tonic-gate int rseq_do(rseq_t *rseq, int num, uintptr_t arg, int flags); 1040Sstevel@tonic-gate int rseq_undo(rseq_t *rseq, int num, uintptr_t arg, int flags); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate /* 1080Sstevel@tonic-gate * To use rseq debugging, rseq_do_debug() and rseq_undo_debug() are provided. 1090Sstevel@tonic-gate * They are similar to their non-debug counterparts, except for additional 1100Sstevel@tonic-gate * arguments: scenario type and scenario arguments. 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate int rseq_do_debug(rseq_t *rseq, int num, uintptr_t arg, int flags, 1130Sstevel@tonic-gate int scenario, uintptr_t sarg1, uintptr_t sarg2); 1140Sstevel@tonic-gate int rseq_undo_debug(rseq_t *rseq, int num, uintptr_t arg, int flags, 1150Sstevel@tonic-gate int scenario, uintptr_t sarg1, uintptr_t sarg2); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * Debug scenarios 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate enum { 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * simulate step failure: instead of executing step number sarg2, 1230Sstevel@tonic-gate * rseq will set s_rval to sarg1 and invoke the step callback. 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate RSEQ_DBG_FAIL_ONE, 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * same as RSEQ_DBG_FAIL_ONE, but step number is chosen randomly. 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate RSEQ_DBG_FAIL_ONE_RANDOM, 1300Sstevel@tonic-gate /* 1310Sstevel@tonic-gate * simulate each step failure one-by-one, to cover all failure paths. 1320Sstevel@tonic-gate * in pseudo code: 1330Sstevel@tonic-gate * 1340Sstevel@tonic-gate * for i = 0..num 1350Sstevel@tonic-gate * RSEQ_DBG_FAIL_ONE of the i-th step; 1360Sstevel@tonic-gate * 1370Sstevel@tonic-gate */ 1380Sstevel@tonic-gate RSEQ_DBG_FAIL_ONEBYONE 1390Sstevel@tonic-gate }; 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * convenience macros for rseq definition 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate #define RSEQT(func, cb) { (rseq_func_t)(func), #func, (rseq_cb_t)(cb), 0 } 1460Sstevel@tonic-gate #define RSEQE(f1, cb1, f2, cb2) { RSEQT(f1, cb1), RSEQT(f2, cb2) } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * Example: 1500Sstevel@tonic-gate * 1510Sstevel@tonic-gate * #define MY_RSEQ(f1, f2) RSEQE(f1, my_do_cb, f2, my_undo_cb) 1520Sstevel@tonic-gate * 1530Sstevel@tonic-gate * rseq_t my_rseq[] = { 1540Sstevel@tonic-gate * MY_RSEQ(my_first_do, my_first_undo), 1550Sstevel@tonic-gate * MY_RSEQ(my_second_do, my_second_undo), 1560Sstevel@tonic-gate * ... 1570Sstevel@tonic-gate * }; 1580Sstevel@tonic-gate * 1590Sstevel@tonic-gate * int my_do_cb(rseq_t *rseq, int num) 1600Sstevel@tonic-gate * { return (rseq[num].rval == 0) ? RSEQ_OK : RSEQ_UNDO; } 1610Sstevel@tonic-gate * 1620Sstevel@tonic-gate * int my_undo_cb(rseq_t *rseq, int num) 1630Sstevel@tonic-gate * { return RSEQ_OK; } 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate #ifdef __cplusplus 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate #endif 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate #endif /* _SYS_USB_USBSER_USBSER_RSEQ_H */ 171