Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / 1394 / s1394_fcp.c
blob2ab91fd0ba8045ca47672420d5752e544cbd79a2
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * s1394_fcp.c
31 * 1394 Services Layer FCP Support Routines
34 #include <sys/conf.h>
35 #include <sys/ddi.h>
36 #include <sys/sunddi.h>
37 #include <sys/cmn_err.h>
38 #include <sys/types.h>
39 #include <sys/kmem.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,
55 cmd1394_cmd_t *req);
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,
67 T1394_ADDR_WRENBL,
68 { NULL, s1394_fcp_resp_recv_write_request, NULL },
69 IEC61883_FCP_CMD_ADDR
72 s1394_fa_descr_t s1394_fcp_tgt_descr = {
73 IEC61883_FCP_CMD_ADDR,
74 IEC61883_FCP_CMD_SIZE,
75 T1394_ADDR_WRENBL,
76 { NULL, s1394_fcp_cmd_recv_write_request, NULL },
77 IEC61883_FCP_RESP_ADDR
81 int
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);
100 return (ret);
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));
130 static int
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);
157 static int
158 s1394_fcp_unregister_common(s1394_target_t *target, s1394_fa_type_t type)
160 s1394_hal_t *hal = target->on_hal;
161 int result;
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);
170 } else {
173 rw_exit(&hal->target_list_rwlock);
175 return (result);
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)
186 int len;
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;
191 if (len == 4) {
192 cmd->cmd_result = CMD1394_ETYPE_ERROR;
193 return (DDI_FAILURE);
195 } else {
196 len = 4;
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);
218 static void
219 s1394_fcp_resp_recv_write_request(cmd1394_cmd_t *req)
221 s1394_fcp_recv_write_request(req, S1394_FA_TYPE_FCP_CTL);
224 static void
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
234 static void
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;
240 uint_t saved_gen;
241 int num_retries = 0;
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);
248 start:
249 target = hal->hal_fa[type].fal_head;
251 if (target) {
252 s1394_fa_restore_cmd(hal, req);
253 restored = B_TRUE;
255 /* Find a target that claims the request */
256 do {
257 fat = &target->target_fa[type];
259 cb = fat->fat_u.fcp.fc_evts.fcp_write_request;
260 if (cb == NULL) {
261 continue;
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);
268 ret = cb(req);
269 rw_enter(&hal->target_list_rwlock, RW_READER);
271 if (ret == T1394_REQ_CLAIMED) {
272 break;
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)) {
282 num_retries++;
283 if (num_retries <= s1394_fcp_notify_retry_cnt) {
284 goto start;
285 } else {
286 break;
290 target = fat->fat_next;
291 } while (target != NULL);
294 rw_exit(&hal->target_list_rwlock);
296 if (ret != T1394_REQ_CLAIMED) {
297 if (restored) {
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
307 static void
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);