1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate * s1394_cmp.c
29*7c478bd9Sstevel@tonic-gate * 1394 Services Layer Connection Management Procedures Support Routines
30*7c478bd9Sstevel@tonic-gate */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/1394/t1394.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/1394/s1394.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/1394/h1394.h>
41*7c478bd9Sstevel@tonic-gate
42*7c478bd9Sstevel@tonic-gate static void s1394_cmp_init(s1394_hal_t *hal);
43*7c478bd9Sstevel@tonic-gate static void s1394_cmp_fini(s1394_hal_t *hal);
44*7c478bd9Sstevel@tonic-gate static void s1394_cmp_ompr_recv_read_request(cmd1394_cmd_t *req);
45*7c478bd9Sstevel@tonic-gate static void s1394_cmp_impr_recv_read_request(cmd1394_cmd_t *req);
46*7c478bd9Sstevel@tonic-gate static void s1394_cmp_ompr_recv_lock_request(cmd1394_cmd_t *req);
47*7c478bd9Sstevel@tonic-gate static void s1394_cmp_impr_recv_lock_request(cmd1394_cmd_t *req);
48*7c478bd9Sstevel@tonic-gate static void s1394_cmp_notify_reg_change(s1394_hal_t *hal, t1394_cmp_reg_t reg,
49*7c478bd9Sstevel@tonic-gate s1394_target_t *self);
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate * number of retries to notify registered targets in case target list
54*7c478bd9Sstevel@tonic-gate * changes while the list rwlock is dropped for the time of callback
55*7c478bd9Sstevel@tonic-gate */
56*7c478bd9Sstevel@tonic-gate uint_t s1394_cmp_notify_retry_cnt = 3;
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate s1394_fa_descr_t s1394_cmp_ompr_descr = {
59*7c478bd9Sstevel@tonic-gate IEC61883_CMP_OMPR_ADDR,
60*7c478bd9Sstevel@tonic-gate 4,
61*7c478bd9Sstevel@tonic-gate T1394_ADDR_RDENBL | T1394_ADDR_LKENBL,
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate s1394_cmp_ompr_recv_read_request,
64*7c478bd9Sstevel@tonic-gate NULL,
65*7c478bd9Sstevel@tonic-gate s1394_cmp_ompr_recv_lock_request
66*7c478bd9Sstevel@tonic-gate },
67*7c478bd9Sstevel@tonic-gate 0
68*7c478bd9Sstevel@tonic-gate };
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate s1394_fa_descr_t s1394_cmp_impr_descr = {
71*7c478bd9Sstevel@tonic-gate IEC61883_CMP_IMPR_ADDR,
72*7c478bd9Sstevel@tonic-gate 4,
73*7c478bd9Sstevel@tonic-gate T1394_ADDR_RDENBL | T1394_ADDR_LKENBL,
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate s1394_cmp_impr_recv_read_request,
76*7c478bd9Sstevel@tonic-gate NULL,
77*7c478bd9Sstevel@tonic-gate s1394_cmp_impr_recv_lock_request
78*7c478bd9Sstevel@tonic-gate },
79*7c478bd9Sstevel@tonic-gate 0
80*7c478bd9Sstevel@tonic-gate };
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate
83*7c478bd9Sstevel@tonic-gate int
s1394_cmp_register(s1394_target_t * target,t1394_cmp_evts_t * evts)84*7c478bd9Sstevel@tonic-gate s1394_cmp_register(s1394_target_t *target, t1394_cmp_evts_t *evts)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = target->on_hal;
87*7c478bd9Sstevel@tonic-gate static t1394_cmp_evts_t default_evts = { NULL, NULL };
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_WRITER);
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate * if registering the first target, claim and initialize addresses
92*7c478bd9Sstevel@tonic-gate */
93*7c478bd9Sstevel@tonic-gate if (s1394_fa_list_is_empty(hal, S1394_FA_TYPE_CMP)) {
94*7c478bd9Sstevel@tonic-gate if (s1394_fa_claim_addr(hal, S1394_FA_TYPE_CMP_OMPR,
95*7c478bd9Sstevel@tonic-gate &s1394_cmp_ompr_descr) != DDI_SUCCESS) {
96*7c478bd9Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
97*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate if (s1394_fa_claim_addr(hal, S1394_FA_TYPE_CMP_IMPR,
101*7c478bd9Sstevel@tonic-gate &s1394_cmp_impr_descr) != DDI_SUCCESS) {
102*7c478bd9Sstevel@tonic-gate s1394_fa_free_addr(hal, S1394_FA_TYPE_CMP_OMPR);
103*7c478bd9Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
104*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate s1394_cmp_init(hal);
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate /* Add on the target list (we only use one list) */
111*7c478bd9Sstevel@tonic-gate s1394_fa_list_add(hal, target, S1394_FA_TYPE_CMP);
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate if (evts == NULL) {
114*7c478bd9Sstevel@tonic-gate evts = &default_evts;
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate target->target_fa[S1394_FA_TYPE_CMP].fat_u.cmp.cm_evts = *evts;
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate int
s1394_cmp_unregister(s1394_target_t * target)124*7c478bd9Sstevel@tonic-gate s1394_cmp_unregister(s1394_target_t *target)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = target->on_hal;
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_WRITER);
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate if (s1394_fa_list_remove(hal, target,
131*7c478bd9Sstevel@tonic-gate S1394_FA_TYPE_CMP) == DDI_SUCCESS) {
132*7c478bd9Sstevel@tonic-gate if (s1394_fa_list_is_empty(hal, S1394_FA_TYPE_CMP)) {
133*7c478bd9Sstevel@tonic-gate s1394_fa_free_addr(hal, S1394_FA_TYPE_CMP_OMPR);
134*7c478bd9Sstevel@tonic-gate s1394_fa_free_addr(hal, S1394_FA_TYPE_CMP_IMPR);
135*7c478bd9Sstevel@tonic-gate s1394_cmp_fini(hal);
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate
139*7c478bd9Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate int
s1394_cmp_read(s1394_target_t * target,t1394_cmp_reg_t reg,uint32_t * valp)145*7c478bd9Sstevel@tonic-gate s1394_cmp_read(s1394_target_t *target, t1394_cmp_reg_t reg, uint32_t *valp)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = target->on_hal;
148*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
149*7c478bd9Sstevel@tonic-gate int ret = DDI_FAILURE;
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate if (reg == T1394_CMP_OMPR) {
152*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_ompr_rwlock, RW_READER);
153*7c478bd9Sstevel@tonic-gate *valp = cmp->cmp_ompr_val;
154*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_ompr_rwlock);
155*7c478bd9Sstevel@tonic-gate ret = DDI_SUCCESS;
156*7c478bd9Sstevel@tonic-gate } else if (reg == T1394_CMP_IMPR) {
157*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_impr_rwlock, RW_READER);
158*7c478bd9Sstevel@tonic-gate *valp = cmp->cmp_impr_val;
159*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_impr_rwlock);
160*7c478bd9Sstevel@tonic-gate ret = DDI_SUCCESS;
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate return (ret);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate int
s1394_cmp_cas(s1394_target_t * target,t1394_cmp_reg_t reg,uint32_t arg_val,uint32_t new_val,uint32_t * old_valp)167*7c478bd9Sstevel@tonic-gate s1394_cmp_cas(s1394_target_t *target, t1394_cmp_reg_t reg, uint32_t arg_val,
168*7c478bd9Sstevel@tonic-gate uint32_t new_val, uint32_t *old_valp)
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = target->on_hal;
171*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
172*7c478bd9Sstevel@tonic-gate int ret = DDI_SUCCESS;
173*7c478bd9Sstevel@tonic-gate
174*7c478bd9Sstevel@tonic-gate if (reg == T1394_CMP_OMPR) {
175*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_ompr_rwlock, RW_WRITER);
176*7c478bd9Sstevel@tonic-gate *old_valp = cmp->cmp_ompr_val;
177*7c478bd9Sstevel@tonic-gate if (cmp->cmp_ompr_val == arg_val) {
178*7c478bd9Sstevel@tonic-gate cmp->cmp_ompr_val = new_val;
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_ompr_rwlock);
181*7c478bd9Sstevel@tonic-gate } else if (reg == T1394_CMP_IMPR) {
182*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_impr_rwlock, RW_WRITER);
183*7c478bd9Sstevel@tonic-gate *old_valp = cmp->cmp_impr_val;
184*7c478bd9Sstevel@tonic-gate if (cmp->cmp_impr_val == arg_val) {
185*7c478bd9Sstevel@tonic-gate cmp->cmp_impr_val = new_val;
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_impr_rwlock);
188*7c478bd9Sstevel@tonic-gate } else {
189*7c478bd9Sstevel@tonic-gate ret = DDI_FAILURE;
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate /* notify other targets */
193*7c478bd9Sstevel@tonic-gate if (ret == DDI_SUCCESS) {
194*7c478bd9Sstevel@tonic-gate s1394_cmp_notify_reg_change(hal, reg, target);
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate return (ret);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_init(s1394_hal_t * hal)201*7c478bd9Sstevel@tonic-gate s1394_cmp_init(s1394_hal_t *hal)
202*7c478bd9Sstevel@tonic-gate {
203*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate rw_init(&cmp->cmp_ompr_rwlock, NULL, RW_DRIVER, NULL);
206*7c478bd9Sstevel@tonic-gate rw_init(&cmp->cmp_impr_rwlock, NULL, RW_DRIVER, NULL);
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate cmp->cmp_ompr_val = IEC61883_CMP_OMPR_INIT_VAL;
209*7c478bd9Sstevel@tonic-gate cmp->cmp_impr_val = IEC61883_CMP_IMPR_INIT_VAL;
210*7c478bd9Sstevel@tonic-gate }
211*7c478bd9Sstevel@tonic-gate
212*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_fini(s1394_hal_t * hal)213*7c478bd9Sstevel@tonic-gate s1394_cmp_fini(s1394_hal_t *hal)
214*7c478bd9Sstevel@tonic-gate {
215*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
216*7c478bd9Sstevel@tonic-gate
217*7c478bd9Sstevel@tonic-gate rw_destroy(&cmp->cmp_ompr_rwlock);
218*7c478bd9Sstevel@tonic-gate rw_destroy(&cmp->cmp_impr_rwlock);
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate
221*7c478bd9Sstevel@tonic-gate /*
222*7c478bd9Sstevel@tonic-gate * iMPR/oMPR read/lock requests
223*7c478bd9Sstevel@tonic-gate */
224*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_ompr_recv_read_request(cmd1394_cmd_t * req)225*7c478bd9Sstevel@tonic-gate s1394_cmp_ompr_recv_read_request(cmd1394_cmd_t *req)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = req->cmd_callback_arg;
228*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate if (req->cmd_type != CMD1394_ASYNCH_RD_QUAD) {
231*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
232*7c478bd9Sstevel@tonic-gate } else {
233*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_ompr_rwlock, RW_READER);
234*7c478bd9Sstevel@tonic-gate req->cmd_u.q.quadlet_data = cmp->cmp_ompr_val;
235*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_ompr_rwlock);
236*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_COMPLETE;
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate (void) s1394_send_response(hal, req);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_impr_recv_read_request(cmd1394_cmd_t * req)243*7c478bd9Sstevel@tonic-gate s1394_cmp_impr_recv_read_request(cmd1394_cmd_t *req)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = req->cmd_callback_arg;
246*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate if (req->cmd_type != CMD1394_ASYNCH_RD_QUAD) {
249*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
250*7c478bd9Sstevel@tonic-gate } else {
251*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_impr_rwlock, RW_READER);
252*7c478bd9Sstevel@tonic-gate req->cmd_u.q.quadlet_data = cmp->cmp_impr_val;
253*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_impr_rwlock);
254*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_COMPLETE;
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate
257*7c478bd9Sstevel@tonic-gate (void) s1394_send_response(hal, req);
258*7c478bd9Sstevel@tonic-gate }
259*7c478bd9Sstevel@tonic-gate
260*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_ompr_recv_lock_request(cmd1394_cmd_t * req)261*7c478bd9Sstevel@tonic-gate s1394_cmp_ompr_recv_lock_request(cmd1394_cmd_t *req)
262*7c478bd9Sstevel@tonic-gate {
263*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = req->cmd_callback_arg;
264*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
265*7c478bd9Sstevel@tonic-gate boolean_t notify = B_TRUE;
266*7c478bd9Sstevel@tonic-gate
267*7c478bd9Sstevel@tonic-gate if ((req->cmd_type != CMD1394_ASYNCH_LOCK_32) ||
268*7c478bd9Sstevel@tonic-gate (req->cmd_u.l32.lock_type != CMD1394_LOCK_COMPARE_SWAP)) {
269*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
270*7c478bd9Sstevel@tonic-gate notify = B_FALSE;
271*7c478bd9Sstevel@tonic-gate } else {
272*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_ompr_rwlock, RW_WRITER);
273*7c478bd9Sstevel@tonic-gate req->cmd_u.l32.old_value = cmp->cmp_ompr_val;
274*7c478bd9Sstevel@tonic-gate if (cmp->cmp_ompr_val == req->cmd_u.l32.arg_value) {
275*7c478bd9Sstevel@tonic-gate /* write only allowed bits */
276*7c478bd9Sstevel@tonic-gate cmp->cmp_ompr_val = (req->cmd_u.l32.data_value &
277*7c478bd9Sstevel@tonic-gate IEC61883_CMP_OMPR_LOCK_MASK) |
278*7c478bd9Sstevel@tonic-gate (cmp->cmp_ompr_val & ~IEC61883_CMP_OMPR_LOCK_MASK);
279*7c478bd9Sstevel@tonic-gate }
280*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_ompr_rwlock);
281*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_COMPLETE;
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate
284*7c478bd9Sstevel@tonic-gate (void) s1394_send_response(hal, req);
285*7c478bd9Sstevel@tonic-gate
286*7c478bd9Sstevel@tonic-gate /* notify all targets */
287*7c478bd9Sstevel@tonic-gate if (notify) {
288*7c478bd9Sstevel@tonic-gate s1394_cmp_notify_reg_change(hal, T1394_CMP_OMPR, NULL);
289*7c478bd9Sstevel@tonic-gate }
290*7c478bd9Sstevel@tonic-gate }
291*7c478bd9Sstevel@tonic-gate
292*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_impr_recv_lock_request(cmd1394_cmd_t * req)293*7c478bd9Sstevel@tonic-gate s1394_cmp_impr_recv_lock_request(cmd1394_cmd_t *req)
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate s1394_hal_t *hal = req->cmd_callback_arg;
296*7c478bd9Sstevel@tonic-gate s1394_cmp_hal_t *cmp = &hal->hal_cmp;
297*7c478bd9Sstevel@tonic-gate boolean_t notify = B_TRUE;
298*7c478bd9Sstevel@tonic-gate
299*7c478bd9Sstevel@tonic-gate if ((req->cmd_type != CMD1394_ASYNCH_LOCK_32) ||
300*7c478bd9Sstevel@tonic-gate (req->cmd_u.l32.lock_type != CMD1394_LOCK_COMPARE_SWAP)) {
301*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_TYPE_ERROR;
302*7c478bd9Sstevel@tonic-gate notify = B_FALSE;
303*7c478bd9Sstevel@tonic-gate } else {
304*7c478bd9Sstevel@tonic-gate rw_enter(&cmp->cmp_impr_rwlock, RW_WRITER);
305*7c478bd9Sstevel@tonic-gate req->cmd_u.l32.old_value = cmp->cmp_impr_val;
306*7c478bd9Sstevel@tonic-gate if (cmp->cmp_impr_val == req->cmd_u.l32.arg_value) {
307*7c478bd9Sstevel@tonic-gate /* write only allowed bits */
308*7c478bd9Sstevel@tonic-gate cmp->cmp_impr_val = (req->cmd_u.l32.data_value &
309*7c478bd9Sstevel@tonic-gate IEC61883_CMP_IMPR_LOCK_MASK) |
310*7c478bd9Sstevel@tonic-gate (cmp->cmp_impr_val & ~IEC61883_CMP_IMPR_LOCK_MASK);
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate rw_exit(&cmp->cmp_impr_rwlock);
313*7c478bd9Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_COMPLETE;
314*7c478bd9Sstevel@tonic-gate }
315*7c478bd9Sstevel@tonic-gate
316*7c478bd9Sstevel@tonic-gate (void) s1394_send_response(hal, req);
317*7c478bd9Sstevel@tonic-gate
318*7c478bd9Sstevel@tonic-gate /* notify all targets */
319*7c478bd9Sstevel@tonic-gate if (notify) {
320*7c478bd9Sstevel@tonic-gate s1394_cmp_notify_reg_change(hal, T1394_CMP_IMPR, NULL);
321*7c478bd9Sstevel@tonic-gate }
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate
324*7c478bd9Sstevel@tonic-gate /*
325*7c478bd9Sstevel@tonic-gate * Notify registered targets except 'self' about register value change
326*7c478bd9Sstevel@tonic-gate */
327*7c478bd9Sstevel@tonic-gate static void
s1394_cmp_notify_reg_change(s1394_hal_t * hal,t1394_cmp_reg_t reg,s1394_target_t * self)328*7c478bd9Sstevel@tonic-gate s1394_cmp_notify_reg_change(s1394_hal_t *hal, t1394_cmp_reg_t reg,
329*7c478bd9Sstevel@tonic-gate s1394_target_t *self)
330*7c478bd9Sstevel@tonic-gate {
331*7c478bd9Sstevel@tonic-gate s1394_target_t *target;
332*7c478bd9Sstevel@tonic-gate s1394_fa_target_t *fat;
333*7c478bd9Sstevel@tonic-gate uint_t saved_gen;
334*7c478bd9Sstevel@tonic-gate int num_retries = 0;
335*7c478bd9Sstevel@tonic-gate void (*cb)(opaque_t, t1394_cmp_reg_t);
336*7c478bd9Sstevel@tonic-gate opaque_t arg;
337*7c478bd9Sstevel@tonic-gate
338*7c478bd9Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_READER);
339*7c478bd9Sstevel@tonic-gate
340*7c478bd9Sstevel@tonic-gate start:
341*7c478bd9Sstevel@tonic-gate target = hal->hal_fa[S1394_FA_TYPE_CMP].fal_head;
342*7c478bd9Sstevel@tonic-gate
343*7c478bd9Sstevel@tonic-gate for (; target; target = fat->fat_next) {
344*7c478bd9Sstevel@tonic-gate fat = &target->target_fa[S1394_FA_TYPE_CMP];
345*7c478bd9Sstevel@tonic-gate
346*7c478bd9Sstevel@tonic-gate /*
347*7c478bd9Sstevel@tonic-gate * even if the target list changes when the lock is dropped,
348*7c478bd9Sstevel@tonic-gate * comparing with self is safe because the target should
349*7c478bd9Sstevel@tonic-gate * not unregister until all CMP operations are completed
350*7c478bd9Sstevel@tonic-gate */
351*7c478bd9Sstevel@tonic-gate if (target == self) {
352*7c478bd9Sstevel@tonic-gate continue;
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate
355*7c478bd9Sstevel@tonic-gate cb = fat->fat_u.cmp.cm_evts.cmp_reg_change;
356*7c478bd9Sstevel@tonic-gate if (cb == NULL) {
357*7c478bd9Sstevel@tonic-gate continue;
358*7c478bd9Sstevel@tonic-gate }
359*7c478bd9Sstevel@tonic-gate arg = fat->fat_u.cmp.cm_evts.cmp_arg;
360*7c478bd9Sstevel@tonic-gate
361*7c478bd9Sstevel@tonic-gate saved_gen = s1394_fa_list_gen(hal, S1394_FA_TYPE_CMP);
362*7c478bd9Sstevel@tonic-gate
363*7c478bd9Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
364*7c478bd9Sstevel@tonic-gate cb(arg, reg);
365*7c478bd9Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_READER);
366*7c478bd9Sstevel@tonic-gate
367*7c478bd9Sstevel@tonic-gate /*
368*7c478bd9Sstevel@tonic-gate * List could change while we dropped the lock. In such
369*7c478bd9Sstevel@tonic-gate * case, start all over again, because missing a register
370*7c478bd9Sstevel@tonic-gate * change can have more serious consequences for a
371*7c478bd9Sstevel@tonic-gate * target than receiving same notification more than once
372*7c478bd9Sstevel@tonic-gate */
373*7c478bd9Sstevel@tonic-gate if (saved_gen != s1394_fa_list_gen(hal, S1394_FA_TYPE_CMP)) {
374*7c478bd9Sstevel@tonic-gate if (++num_retries <= s1394_cmp_notify_retry_cnt) {
375*7c478bd9Sstevel@tonic-gate goto start;
376*7c478bd9Sstevel@tonic-gate } else {
377*7c478bd9Sstevel@tonic-gate break;
378*7c478bd9Sstevel@tonic-gate }
379*7c478bd9Sstevel@tonic-gate }
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate
382*7c478bd9Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
383*7c478bd9Sstevel@tonic-gate }
384