Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Linux network driver for Brocade Converged Network Adapter. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License (GPL) Version 2 as |
| 6 | * published by the Free Software Foundation |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | /* |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 14 | * Copyright (c) 2005-2011 Brocade Communications Systems, Inc. |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 15 | * All rights reserved |
| 16 | * www.brocade.com |
| 17 | */ |
| 18 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 19 | /* BFA common services */ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 20 | |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 21 | #ifndef __BFA_CS_H__ |
| 22 | #define __BFA_CS_H__ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 23 | |
| 24 | #include "cna.h" |
| 25 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 26 | /* BFA state machine interfaces */ |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 27 | |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 28 | typedef void (*bfa_sm_t)(void *sm, int event); |
| 29 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 30 | /* oc - object class eg. bfa_ioc |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 31 | * st - state, eg. reset |
| 32 | * otype - object type, eg. struct bfa_ioc |
| 33 | * etype - object type, eg. enum ioc_event |
| 34 | */ |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 35 | #define bfa_sm_state_decl(oc, st, otype, etype) \ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 36 | static void oc ## _sm_ ## st(otype * fsm, etype event) |
| 37 | |
| 38 | #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state)) |
| 39 | #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event))) |
| 40 | #define bfa_sm_get_state(_sm) ((_sm)->sm) |
| 41 | #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state)) |
| 42 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 43 | /* For converting from state machine function to state encoding. */ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 44 | struct bfa_sm_table { |
| 45 | bfa_sm_t sm; /*!< state machine function */ |
| 46 | int state; /*!< state machine encoding */ |
| 47 | char *name; /*!< state name for display */ |
| 48 | }; |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 49 | #define BFA_SM(_sm) ((bfa_sm_t)(_sm)) |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 50 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 51 | /* State machine with entry actions. */ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 52 | typedef void (*bfa_fsm_t)(void *fsm, int event); |
| 53 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 54 | /* oc - object class eg. bfa_ioc |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 55 | * st - state, eg. reset |
| 56 | * otype - object type, eg. struct bfa_ioc |
| 57 | * etype - object type, eg. enum ioc_event |
| 58 | */ |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 59 | #define bfa_fsm_state_decl(oc, st, otype, etype) \ |
| 60 | static void oc ## _sm_ ## st(otype * fsm, etype event); \ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 61 | static void oc ## _sm_ ## st ## _entry(otype * fsm) |
| 62 | |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 63 | #define bfa_fsm_set_state(_fsm, _state) do { \ |
| 64 | (_fsm)->fsm = (bfa_fsm_t)(_state); \ |
| 65 | _state ## _entry(_fsm); \ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 66 | } while (0) |
| 67 | |
| 68 | #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event))) |
| 69 | #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm) |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 70 | #define bfa_fsm_cmp_state(_fsm, _state) \ |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 71 | ((_fsm)->fsm == (bfa_fsm_t)(_state)) |
| 72 | |
| 73 | static inline int |
Rasesh Mody | b7ee31c5 | 2010-10-05 15:46:05 +0000 | [diff] [blame] | 74 | bfa_sm_to_state(const struct bfa_sm_table *smt, bfa_sm_t sm) |
Rasesh Mody | 8b230ed | 2010-08-23 20:24:12 -0700 | [diff] [blame] | 75 | { |
| 76 | int i = 0; |
| 77 | |
| 78 | while (smt[i].sm && smt[i].sm != sm) |
| 79 | i++; |
| 80 | return smt[i].state; |
| 81 | } |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 82 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 83 | /* Generic wait counter. */ |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 84 | |
| 85 | typedef void (*bfa_wc_resume_t) (void *cbarg); |
| 86 | |
| 87 | struct bfa_wc { |
| 88 | bfa_wc_resume_t wc_resume; |
| 89 | void *wc_cbarg; |
| 90 | int wc_count; |
| 91 | }; |
| 92 | |
| 93 | static inline void |
| 94 | bfa_wc_up(struct bfa_wc *wc) |
| 95 | { |
| 96 | wc->wc_count++; |
| 97 | } |
| 98 | |
| 99 | static inline void |
| 100 | bfa_wc_down(struct bfa_wc *wc) |
| 101 | { |
| 102 | wc->wc_count--; |
| 103 | if (wc->wc_count == 0) |
| 104 | wc->wc_resume(wc->wc_cbarg); |
| 105 | } |
| 106 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 107 | /* Initialize a waiting counter. */ |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 108 | static inline void |
| 109 | bfa_wc_init(struct bfa_wc *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg) |
| 110 | { |
| 111 | wc->wc_resume = wc_resume; |
| 112 | wc->wc_cbarg = wc_cbarg; |
| 113 | wc->wc_count = 0; |
| 114 | bfa_wc_up(wc); |
| 115 | } |
| 116 | |
Ben Hutchings | 1aa8b47 | 2012-07-10 10:56:59 +0000 | [diff] [blame] | 117 | /* Wait for counter to reach zero */ |
Rasesh Mody | 758ccc3 | 2011-07-22 08:07:49 +0000 | [diff] [blame] | 118 | static inline void |
| 119 | bfa_wc_wait(struct bfa_wc *wc) |
| 120 | { |
| 121 | bfa_wc_down(wc); |
| 122 | } |
| 123 | |
| 124 | #endif /* __BFA_CS_H__ */ |