4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
31 * 1394 Services Layer FCP Support Routines
36 #include <sys/sunddi.h>
37 #include <sys/cmn_err.h>
38 #include <sys/types.h>
40 #include <sys/tnf_probe.h>
42 #include <sys/1394/t1394.h>
43 #include <sys/1394/s1394.h>
44 #include <sys/1394/h1394.h>
46 static int s1394_fcp_register_common(s1394_target_t
*target
,
47 t1394_fcp_evts_t
*evts
, s1394_fa_type_t type
, s1394_fa_descr_t
*descr
);
48 static int s1394_fcp_unregister_common(s1394_target_t
*target
,
49 s1394_fa_type_t type
);
50 static void s1394_fcp_resp_recv_write_request(cmd1394_cmd_t
*req
);
51 static void s1394_fcp_cmd_recv_write_request(cmd1394_cmd_t
*req
);
52 static void s1394_fcp_recv_write_request(cmd1394_cmd_t
*req
,
53 s1394_fa_type_t type
);
54 static void s1394_fcp_recv_write_unclaimed(s1394_hal_t
*hal
,
59 * number of retries to notify registered targets in case target list
60 * changes while the list rwlock is dropped for the time of callback
62 uint_t s1394_fcp_notify_retry_cnt
= 3;
64 s1394_fa_descr_t s1394_fcp_ctl_descr
= {
65 IEC61883_FCP_RESP_ADDR
,
66 IEC61883_FCP_RESP_SIZE
,
68 { NULL
, s1394_fcp_resp_recv_write_request
, NULL
},
72 s1394_fa_descr_t s1394_fcp_tgt_descr
= {
73 IEC61883_FCP_CMD_ADDR
,
74 IEC61883_FCP_CMD_SIZE
,
76 { NULL
, s1394_fcp_cmd_recv_write_request
, NULL
},
77 IEC61883_FCP_RESP_ADDR
82 s1394_fcp_hal_init(s1394_hal_t
*hal
)
84 int ret
= DDI_SUCCESS
;
86 if ((ddi_prop_exists(DDI_DEV_T_ANY
, hal
->halinfo
.dip
, DDI_PROP_DONTPASS
,
87 "h1394-fcp-claim-on-demand")) == 0) {
88 /* if not on-demand, claim addresses now */
89 ret
= s1394_fa_claim_addr(hal
, S1394_FA_TYPE_FCP_CTL
,
90 &s1394_fcp_ctl_descr
);
91 if (ret
== DDI_SUCCESS
) {
92 ret
= s1394_fa_claim_addr(hal
, S1394_FA_TYPE_FCP_TGT
,
93 &s1394_fcp_tgt_descr
);
94 if (ret
!= DDI_SUCCESS
) {
95 s1394_fa_free_addr(hal
, S1394_FA_TYPE_FCP_CTL
);
104 s1394_fcp_register_ctl(s1394_target_t
*target
, t1394_fcp_evts_t
*evts
)
106 return (s1394_fcp_register_common(target
, evts
, S1394_FA_TYPE_FCP_CTL
,
107 &s1394_fcp_ctl_descr
));
111 s1394_fcp_register_tgt(s1394_target_t
*target
, t1394_fcp_evts_t
*evts
)
113 return (s1394_fcp_register_common(target
, evts
, S1394_FA_TYPE_FCP_TGT
,
114 &s1394_fcp_tgt_descr
));
118 s1394_fcp_unregister_ctl(s1394_target_t
*target
)
120 return (s1394_fcp_unregister_common(target
, S1394_FA_TYPE_FCP_CTL
));
124 s1394_fcp_unregister_tgt(s1394_target_t
*target
)
126 return (s1394_fcp_unregister_common(target
, S1394_FA_TYPE_FCP_TGT
));
131 s1394_fcp_register_common(s1394_target_t
*target
, t1394_fcp_evts_t
*evts
,
132 s1394_fa_type_t type
, s1394_fa_descr_t
*descr
)
134 s1394_hal_t
*hal
= target
->on_hal
;
135 s1394_fcp_target_t
*fcp
;
137 rw_enter(&hal
->target_list_rwlock
, RW_WRITER
);
139 if (s1394_fa_list_is_empty(hal
, type
)) {
140 if (s1394_fa_claim_addr(hal
, type
, descr
) != DDI_SUCCESS
) {
141 rw_exit(&hal
->target_list_rwlock
);
142 return (DDI_FAILURE
);
146 /* Add on the target list */
147 s1394_fa_list_add(hal
, target
, type
);
149 fcp
= &target
->target_fa
[type
].fat_u
.fcp
;
150 fcp
->fc_evts
= *evts
;
152 rw_exit(&hal
->target_list_rwlock
);
154 return (DDI_SUCCESS
);
158 s1394_fcp_unregister_common(s1394_target_t
*target
, s1394_fa_type_t type
)
160 s1394_hal_t
*hal
= target
->on_hal
;
163 rw_enter(&hal
->target_list_rwlock
, RW_WRITER
);
165 result
= s1394_fa_list_remove(hal
, target
, type
);
166 if (result
== DDI_SUCCESS
) {
167 if (s1394_fa_list_is_empty(hal
, type
)) {
168 s1394_fa_free_addr(hal
, type
);
173 rw_exit(&hal
->target_list_rwlock
);
179 * s1394_fcp_write_check_cmd()
180 * Check if an FCP command is formed correctly;
181 * set cmd_result and return DDI_FAILURE if not.
184 s1394_fcp_write_check_cmd(cmd1394_cmd_t
*cmd
)
188 /* 4-byte writes must be quadlet writes */
189 if (cmd
->cmd_type
== CMD1394_ASYNCH_WR_BLOCK
) {
190 len
= cmd
->cmd_u
.b
.blk_length
;
192 cmd
->cmd_result
= CMD1394_ETYPE_ERROR
;
193 return (DDI_FAILURE
);
200 * request must be within FCP range. we avoid extra checks by
201 * using the fact that command and response are of the same size
203 if ((cmd
->cmd_addr
& IEEE1394_ADDR_OFFSET_MASK
) + len
>
204 IEC61883_FCP_CMD_SIZE
) {
205 cmd
->cmd_result
= CMD1394_EADDRESS_ERROR
;
206 return (DDI_FAILURE
);
209 /* some options don't make sense for FCP commands */
210 if (cmd
->cmd_options
& CMD1394_OVERRIDE_ADDR
) {
211 cmd
->cmd_result
= CMD1394_EINVALID_COMMAND
;
212 return (DDI_FAILURE
);
215 return (DDI_SUCCESS
);
219 s1394_fcp_resp_recv_write_request(cmd1394_cmd_t
*req
)
221 s1394_fcp_recv_write_request(req
, S1394_FA_TYPE_FCP_CTL
);
225 s1394_fcp_cmd_recv_write_request(cmd1394_cmd_t
*req
)
227 s1394_fcp_recv_write_request(req
, S1394_FA_TYPE_FCP_TGT
);
231 * s1394_fcp_recv_write_request()
232 * Common write request handler
235 s1394_fcp_recv_write_request(cmd1394_cmd_t
*req
, s1394_fa_type_t type
)
237 s1394_hal_t
*hal
= (s1394_hal_t
*)req
->cmd_callback_arg
;
238 s1394_target_t
*target
;
239 s1394_fa_target_t
*fat
;
242 int (*cb
)(cmd1394_cmd_t
*req
);
243 boolean_t restored
= B_FALSE
;
244 int ret
= T1394_REQ_UNCLAIMED
;
246 rw_enter(&hal
->target_list_rwlock
, RW_READER
);
249 target
= hal
->hal_fa
[type
].fal_head
;
252 s1394_fa_restore_cmd(hal
, req
);
255 /* Find a target that claims the request */
257 fat
= &target
->target_fa
[type
];
259 cb
= fat
->fat_u
.fcp
.fc_evts
.fcp_write_request
;
263 req
->cmd_callback_arg
= fat
->fat_u
.fcp
.fc_evts
.fcp_arg
;
265 saved_gen
= s1394_fa_list_gen(hal
, type
);
267 rw_exit(&hal
->target_list_rwlock
);
269 rw_enter(&hal
->target_list_rwlock
, RW_READER
);
271 if (ret
== T1394_REQ_CLAIMED
) {
276 * List could change while we dropped the lock. In such
277 * case, start all over again, because missing a write
278 * request can have more serious consequences for a
279 * target than receiving same request more than once
281 if (saved_gen
!= s1394_fa_list_gen(hal
, type
)) {
283 if (num_retries
<= s1394_fcp_notify_retry_cnt
) {
290 target
= fat
->fat_next
;
291 } while (target
!= NULL
);
294 rw_exit(&hal
->target_list_rwlock
);
296 if (ret
!= T1394_REQ_CLAIMED
) {
298 s1394_fa_convert_cmd(hal
, req
);
300 s1394_fcp_recv_write_unclaimed(hal
, req
);
305 * none of the targets claimed the request - send an appropriate response
308 s1394_fcp_recv_write_unclaimed(s1394_hal_t
*hal
, cmd1394_cmd_t
*req
)
310 req
->cmd_result
= IEEE1394_RESP_ADDRESS_ERROR
;
311 (void) s1394_send_response(hal
, req
);