1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * s1394_fcp.c
31*0Sstevel@tonic-gate * 1394 Services Layer FCP Support Routines
32*0Sstevel@tonic-gate */
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include <sys/conf.h>
35*0Sstevel@tonic-gate #include <sys/ddi.h>
36*0Sstevel@tonic-gate #include <sys/sunddi.h>
37*0Sstevel@tonic-gate #include <sys/cmn_err.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <sys/kmem.h>
40*0Sstevel@tonic-gate #include <sys/tnf_probe.h>
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate #include <sys/1394/t1394.h>
43*0Sstevel@tonic-gate #include <sys/1394/s1394.h>
44*0Sstevel@tonic-gate #include <sys/1394/h1394.h>
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate static int s1394_fcp_register_common(s1394_target_t *target,
47*0Sstevel@tonic-gate t1394_fcp_evts_t *evts, s1394_fa_type_t type, s1394_fa_descr_t *descr);
48*0Sstevel@tonic-gate static int s1394_fcp_unregister_common(s1394_target_t *target,
49*0Sstevel@tonic-gate s1394_fa_type_t type);
50*0Sstevel@tonic-gate static void s1394_fcp_resp_recv_write_request(cmd1394_cmd_t *req);
51*0Sstevel@tonic-gate static void s1394_fcp_cmd_recv_write_request(cmd1394_cmd_t *req);
52*0Sstevel@tonic-gate static void s1394_fcp_recv_write_request(cmd1394_cmd_t *req,
53*0Sstevel@tonic-gate s1394_fa_type_t type);
54*0Sstevel@tonic-gate static void s1394_fcp_recv_write_unclaimed(s1394_hal_t *hal,
55*0Sstevel@tonic-gate cmd1394_cmd_t *req);
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate * number of retries to notify registered targets in case target list
60*0Sstevel@tonic-gate * changes while the list rwlock is dropped for the time of callback
61*0Sstevel@tonic-gate */
62*0Sstevel@tonic-gate uint_t s1394_fcp_notify_retry_cnt = 3;
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate s1394_fa_descr_t s1394_fcp_ctl_descr = {
65*0Sstevel@tonic-gate IEC61883_FCP_RESP_ADDR,
66*0Sstevel@tonic-gate IEC61883_FCP_RESP_SIZE,
67*0Sstevel@tonic-gate T1394_ADDR_WRENBL,
68*0Sstevel@tonic-gate { NULL, s1394_fcp_resp_recv_write_request, NULL },
69*0Sstevel@tonic-gate IEC61883_FCP_CMD_ADDR
70*0Sstevel@tonic-gate };
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate s1394_fa_descr_t s1394_fcp_tgt_descr = {
73*0Sstevel@tonic-gate IEC61883_FCP_CMD_ADDR,
74*0Sstevel@tonic-gate IEC61883_FCP_CMD_SIZE,
75*0Sstevel@tonic-gate T1394_ADDR_WRENBL,
76*0Sstevel@tonic-gate { NULL, s1394_fcp_cmd_recv_write_request, NULL },
77*0Sstevel@tonic-gate IEC61883_FCP_RESP_ADDR
78*0Sstevel@tonic-gate };
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate int
s1394_fcp_hal_init(s1394_hal_t * hal)82*0Sstevel@tonic-gate s1394_fcp_hal_init(s1394_hal_t *hal)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate int ret = DDI_SUCCESS;
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate TNF_PROBE_0_DEBUG(s1394_fcp_hal_init_enter, S1394_TNF_SL_FCP_STACK, "");
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate if ((ddi_prop_exists(DDI_DEV_T_ANY, hal->halinfo.dip, DDI_PROP_DONTPASS,
89*0Sstevel@tonic-gate "h1394-fcp-claim-on-demand")) == 0) {
90*0Sstevel@tonic-gate /* if not on-demand, claim addresses now */
91*0Sstevel@tonic-gate ret = s1394_fa_claim_addr(hal, S1394_FA_TYPE_FCP_CTL,
92*0Sstevel@tonic-gate &s1394_fcp_ctl_descr);
93*0Sstevel@tonic-gate if (ret == DDI_SUCCESS) {
94*0Sstevel@tonic-gate ret = s1394_fa_claim_addr(hal, S1394_FA_TYPE_FCP_TGT,
95*0Sstevel@tonic-gate &s1394_fcp_tgt_descr);
96*0Sstevel@tonic-gate if (ret != DDI_SUCCESS) {
97*0Sstevel@tonic-gate s1394_fa_free_addr(hal, S1394_FA_TYPE_FCP_CTL);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate TNF_PROBE_0_DEBUG(s1394_fcp_hal_init_exit, S1394_TNF_SL_FCP_STACK, "");
103*0Sstevel@tonic-gate return (ret);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate int
s1394_fcp_register_ctl(s1394_target_t * target,t1394_fcp_evts_t * evts)107*0Sstevel@tonic-gate s1394_fcp_register_ctl(s1394_target_t *target, t1394_fcp_evts_t *evts)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate return (s1394_fcp_register_common(target, evts, S1394_FA_TYPE_FCP_CTL,
110*0Sstevel@tonic-gate &s1394_fcp_ctl_descr));
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate int
s1394_fcp_register_tgt(s1394_target_t * target,t1394_fcp_evts_t * evts)114*0Sstevel@tonic-gate s1394_fcp_register_tgt(s1394_target_t *target, t1394_fcp_evts_t *evts)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate return (s1394_fcp_register_common(target, evts, S1394_FA_TYPE_FCP_TGT,
117*0Sstevel@tonic-gate &s1394_fcp_tgt_descr));
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate int
s1394_fcp_unregister_ctl(s1394_target_t * target)121*0Sstevel@tonic-gate s1394_fcp_unregister_ctl(s1394_target_t *target)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate return (s1394_fcp_unregister_common(target, S1394_FA_TYPE_FCP_CTL));
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate int
s1394_fcp_unregister_tgt(s1394_target_t * target)127*0Sstevel@tonic-gate s1394_fcp_unregister_tgt(s1394_target_t *target)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate return (s1394_fcp_unregister_common(target, S1394_FA_TYPE_FCP_TGT));
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate static int
s1394_fcp_register_common(s1394_target_t * target,t1394_fcp_evts_t * evts,s1394_fa_type_t type,s1394_fa_descr_t * descr)134*0Sstevel@tonic-gate s1394_fcp_register_common(s1394_target_t *target, t1394_fcp_evts_t *evts,
135*0Sstevel@tonic-gate s1394_fa_type_t type, s1394_fa_descr_t *descr)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate s1394_hal_t *hal = target->on_hal;
138*0Sstevel@tonic-gate s1394_fcp_target_t *fcp;
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_WRITER);
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate if (s1394_fa_list_is_empty(hal, type)) {
143*0Sstevel@tonic-gate if (s1394_fa_claim_addr(hal, type, descr) != DDI_SUCCESS) {
144*0Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
145*0Sstevel@tonic-gate return (DDI_FAILURE);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate /* Add on the target list */
150*0Sstevel@tonic-gate s1394_fa_list_add(hal, target, type);
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate fcp = &target->target_fa[type].fat_u.fcp;
153*0Sstevel@tonic-gate fcp->fc_evts = *evts;
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate return (DDI_SUCCESS);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate static int
s1394_fcp_unregister_common(s1394_target_t * target,s1394_fa_type_t type)161*0Sstevel@tonic-gate s1394_fcp_unregister_common(s1394_target_t *target, s1394_fa_type_t type)
162*0Sstevel@tonic-gate {
163*0Sstevel@tonic-gate s1394_hal_t *hal = target->on_hal;
164*0Sstevel@tonic-gate int result;
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_WRITER);
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate result = s1394_fa_list_remove(hal, target, type);
169*0Sstevel@tonic-gate if (result == DDI_SUCCESS) {
170*0Sstevel@tonic-gate if (s1394_fa_list_is_empty(hal, type)) {
171*0Sstevel@tonic-gate s1394_fa_free_addr(hal, type);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate } else {
174*0Sstevel@tonic-gate TNF_PROBE_0(s1394_fcp_unregister_common_error_list,
175*0Sstevel@tonic-gate S1394_TNF_SL_FCP_ERROR, "");
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate return (result);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate /*
184*0Sstevel@tonic-gate * s1394_fcp_write_check_cmd()
185*0Sstevel@tonic-gate * Check if an FCP command is formed correctly;
186*0Sstevel@tonic-gate * set cmd_result and return DDI_FAILURE if not.
187*0Sstevel@tonic-gate */
188*0Sstevel@tonic-gate int
s1394_fcp_write_check_cmd(cmd1394_cmd_t * cmd)189*0Sstevel@tonic-gate s1394_fcp_write_check_cmd(cmd1394_cmd_t *cmd)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate int len;
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate /* 4-byte writes must be quadlet writes */
194*0Sstevel@tonic-gate if (cmd->cmd_type == CMD1394_ASYNCH_WR_BLOCK) {
195*0Sstevel@tonic-gate len = cmd->cmd_u.b.blk_length;
196*0Sstevel@tonic-gate if (len == 4) {
197*0Sstevel@tonic-gate cmd->cmd_result = CMD1394_ETYPE_ERROR;
198*0Sstevel@tonic-gate TNF_PROBE_0(t1394_write_error_type,
199*0Sstevel@tonic-gate S1394_TNF_SL_FCP_ERROR, "");
200*0Sstevel@tonic-gate return (DDI_FAILURE);
201*0Sstevel@tonic-gate }
202*0Sstevel@tonic-gate } else {
203*0Sstevel@tonic-gate len = 4;
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate * request must be within FCP range. we avoid extra checks by
208*0Sstevel@tonic-gate * using the fact that command and response are of the same size
209*0Sstevel@tonic-gate */
210*0Sstevel@tonic-gate if ((cmd->cmd_addr & IEEE1394_ADDR_OFFSET_MASK) + len >
211*0Sstevel@tonic-gate IEC61883_FCP_CMD_SIZE) {
212*0Sstevel@tonic-gate cmd->cmd_result = CMD1394_EADDRESS_ERROR;
213*0Sstevel@tonic-gate TNF_PROBE_0(t1394_write_error_addr, S1394_TNF_SL_FCP_ERROR, "");
214*0Sstevel@tonic-gate return (DDI_FAILURE);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate /* some options don't make sense for FCP commands */
218*0Sstevel@tonic-gate if (cmd->cmd_options & CMD1394_OVERRIDE_ADDR) {
219*0Sstevel@tonic-gate cmd->cmd_result = CMD1394_EINVALID_COMMAND;
220*0Sstevel@tonic-gate TNF_PROBE_0(t1394_write_error_opt, S1394_TNF_SL_FCP_ERROR, "");
221*0Sstevel@tonic-gate return (DDI_FAILURE);
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate return (DDI_SUCCESS);
225*0Sstevel@tonic-gate }
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gate static void
s1394_fcp_resp_recv_write_request(cmd1394_cmd_t * req)228*0Sstevel@tonic-gate s1394_fcp_resp_recv_write_request(cmd1394_cmd_t *req)
229*0Sstevel@tonic-gate {
230*0Sstevel@tonic-gate s1394_fcp_recv_write_request(req, S1394_FA_TYPE_FCP_CTL);
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate static void
s1394_fcp_cmd_recv_write_request(cmd1394_cmd_t * req)234*0Sstevel@tonic-gate s1394_fcp_cmd_recv_write_request(cmd1394_cmd_t *req)
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate s1394_fcp_recv_write_request(req, S1394_FA_TYPE_FCP_TGT);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate * s1394_fcp_recv_write_request()
241*0Sstevel@tonic-gate * Common write request handler
242*0Sstevel@tonic-gate */
243*0Sstevel@tonic-gate static void
s1394_fcp_recv_write_request(cmd1394_cmd_t * req,s1394_fa_type_t type)244*0Sstevel@tonic-gate s1394_fcp_recv_write_request(cmd1394_cmd_t *req, s1394_fa_type_t type)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate s1394_hal_t *hal = (s1394_hal_t *)req->cmd_callback_arg;
247*0Sstevel@tonic-gate s1394_target_t *target;
248*0Sstevel@tonic-gate s1394_fa_target_t *fat;
249*0Sstevel@tonic-gate uint_t saved_gen;
250*0Sstevel@tonic-gate int num_retries = 0;
251*0Sstevel@tonic-gate int (*cb)(cmd1394_cmd_t *req);
252*0Sstevel@tonic-gate boolean_t restored = B_FALSE;
253*0Sstevel@tonic-gate int ret = T1394_REQ_UNCLAIMED;
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate TNF_PROBE_0_DEBUG(s1394_fcp_recv_write_request_enter,
256*0Sstevel@tonic-gate S1394_TNF_SL_FCP_STACK, "");
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_READER);
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate start:
261*0Sstevel@tonic-gate target = hal->hal_fa[type].fal_head;
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate if (target) {
264*0Sstevel@tonic-gate s1394_fa_restore_cmd(hal, req);
265*0Sstevel@tonic-gate restored = B_TRUE;
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate /* Find a target that claims the request */
268*0Sstevel@tonic-gate do {
269*0Sstevel@tonic-gate fat = &target->target_fa[type];
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate cb = fat->fat_u.fcp.fc_evts.fcp_write_request;
272*0Sstevel@tonic-gate if (cb == NULL) {
273*0Sstevel@tonic-gate continue;
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate req->cmd_callback_arg = fat->fat_u.fcp.fc_evts.fcp_arg;
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate saved_gen = s1394_fa_list_gen(hal, type);
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
280*0Sstevel@tonic-gate ret = cb(req);
281*0Sstevel@tonic-gate rw_enter(&hal->target_list_rwlock, RW_READER);
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate if (ret == T1394_REQ_CLAIMED) {
284*0Sstevel@tonic-gate break;
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate * List could change while we dropped the lock. In such
289*0Sstevel@tonic-gate * case, start all over again, because missing a write
290*0Sstevel@tonic-gate * request can have more serious consequences for a
291*0Sstevel@tonic-gate * target than receiving same request more than once
292*0Sstevel@tonic-gate */
293*0Sstevel@tonic-gate if (saved_gen != s1394_fa_list_gen(hal, type)) {
294*0Sstevel@tonic-gate TNF_PROBE_2(s1394_fcp_recv_write_request_error,
295*0Sstevel@tonic-gate S1394_TNF_SL_FCP_ERROR, "",
296*0Sstevel@tonic-gate tnf_string, msg, "list gen changed",
297*0Sstevel@tonic-gate tnf_opaque, num_retries, num_retries);
298*0Sstevel@tonic-gate num_retries++;
299*0Sstevel@tonic-gate if (num_retries <= s1394_fcp_notify_retry_cnt) {
300*0Sstevel@tonic-gate goto start;
301*0Sstevel@tonic-gate } else {
302*0Sstevel@tonic-gate break;
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate
306*0Sstevel@tonic-gate target = fat->fat_next;
307*0Sstevel@tonic-gate } while (target != NULL);
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate rw_exit(&hal->target_list_rwlock);
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate if (ret != T1394_REQ_CLAIMED) {
313*0Sstevel@tonic-gate TNF_PROBE_0(s1394_fcp_recv_write_request_error_unclaimed,
314*0Sstevel@tonic-gate S1394_TNF_SL_FCP_ERROR, "");
315*0Sstevel@tonic-gate if (restored) {
316*0Sstevel@tonic-gate s1394_fa_convert_cmd(hal, req);
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate s1394_fcp_recv_write_unclaimed(hal, req);
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate TNF_PROBE_0_DEBUG(s1394_fcp_recv_write_request_exit,
322*0Sstevel@tonic-gate S1394_TNF_SL_FCP_STACK, "");
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate * none of the targets claimed the request - send an appropriate response
327*0Sstevel@tonic-gate */
328*0Sstevel@tonic-gate static void
s1394_fcp_recv_write_unclaimed(s1394_hal_t * hal,cmd1394_cmd_t * req)329*0Sstevel@tonic-gate s1394_fcp_recv_write_unclaimed(s1394_hal_t *hal, cmd1394_cmd_t *req)
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
332*0Sstevel@tonic-gate (void) s1394_send_response(hal, req);
333*0Sstevel@tonic-gate }
334