summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/1394/s1394_fcp.c
blob: 51617043db62c75c1b49801e54ffe02a3cbf1b6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * s1394_fcp.c
 *    1394 Services Layer FCP Support Routines
 */

#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/tnf_probe.h>

#include <sys/1394/t1394.h>
#include <sys/1394/s1394.h>
#include <sys/1394/h1394.h>

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);
static int s1394_fcp_unregister_common(s1394_target_t *target,
    s1394_fa_type_t type);
static void s1394_fcp_resp_recv_write_request(cmd1394_cmd_t *req);
static void s1394_fcp_cmd_recv_write_request(cmd1394_cmd_t *req);
static void s1394_fcp_recv_write_request(cmd1394_cmd_t *req,
    s1394_fa_type_t type);
static void s1394_fcp_recv_write_unclaimed(s1394_hal_t *hal,
    cmd1394_cmd_t *req);


/*
 * number of retries to notify registered targets in case target list
 * changes while the list rwlock is dropped for the time of callback
 */
uint_t s1394_fcp_notify_retry_cnt = 3;

s1394_fa_descr_t s1394_fcp_ctl_descr = {
	IEC61883_FCP_RESP_ADDR,
	IEC61883_FCP_RESP_SIZE,
	T1394_ADDR_WRENBL,
	{ NULL, s1394_fcp_resp_recv_write_request, NULL },
	IEC61883_FCP_CMD_ADDR
};

s1394_fa_descr_t s1394_fcp_tgt_descr = {
	IEC61883_FCP_CMD_ADDR,
	IEC61883_FCP_CMD_SIZE,
	T1394_ADDR_WRENBL,
	{ NULL, s1394_fcp_cmd_recv_write_request, NULL },
	IEC61883_FCP_RESP_ADDR
};


int
s1394_fcp_hal_init(s1394_hal_t *hal)
{
	int	ret = DDI_SUCCESS;

	TNF_PROBE_0_DEBUG(s1394_fcp_hal_init_enter, S1394_TNF_SL_FCP_STACK, "");

	if ((ddi_prop_exists(DDI_DEV_T_ANY, hal->halinfo.dip, DDI_PROP_DONTPASS,
	    "h1394-fcp-claim-on-demand")) == 0) {
		/* if not on-demand, claim addresses now */
		ret = s1394_fa_claim_addr(hal, S1394_FA_TYPE_FCP_CTL,
					&s1394_fcp_ctl_descr);
		if (ret == DDI_SUCCESS) {
			ret = s1394_fa_claim_addr(hal, S1394_FA_TYPE_FCP_TGT,
						&s1394_fcp_tgt_descr);
			if (ret != DDI_SUCCESS) {
				s1394_fa_free_addr(hal, S1394_FA_TYPE_FCP_CTL);
			}
		}
	}

	TNF_PROBE_0_DEBUG(s1394_fcp_hal_init_exit, S1394_TNF_SL_FCP_STACK, "");
	return (ret);
}

int
s1394_fcp_register_ctl(s1394_target_t *target, t1394_fcp_evts_t *evts)
{
	return (s1394_fcp_register_common(target, evts, S1394_FA_TYPE_FCP_CTL,
			&s1394_fcp_ctl_descr));
}

int
s1394_fcp_register_tgt(s1394_target_t *target, t1394_fcp_evts_t *evts)
{
	return (s1394_fcp_register_common(target, evts, S1394_FA_TYPE_FCP_TGT,
			&s1394_fcp_tgt_descr));
}

int
s1394_fcp_unregister_ctl(s1394_target_t *target)
{
	return (s1394_fcp_unregister_common(target, S1394_FA_TYPE_FCP_CTL));
}

int
s1394_fcp_unregister_tgt(s1394_target_t *target)
{
	return (s1394_fcp_unregister_common(target, S1394_FA_TYPE_FCP_TGT));
}


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)
{
	s1394_hal_t	*hal = target->on_hal;
	s1394_fcp_target_t *fcp;

	rw_enter(&hal->target_list_rwlock, RW_WRITER);

	if (s1394_fa_list_is_empty(hal, type)) {
		if (s1394_fa_claim_addr(hal, type, descr) != DDI_SUCCESS) {
			rw_exit(&hal->target_list_rwlock);
			return (DDI_FAILURE);
		}
	}

	/* Add on the target list */
	s1394_fa_list_add(hal, target, type);

	fcp = &target->target_fa[type].fat_u.fcp;
	fcp->fc_evts = *evts;

	rw_exit(&hal->target_list_rwlock);

	return (DDI_SUCCESS);
}

static int
s1394_fcp_unregister_common(s1394_target_t *target, s1394_fa_type_t type)
{
	s1394_hal_t	*hal = target->on_hal;
	int		result;

	rw_enter(&hal->target_list_rwlock, RW_WRITER);

	result = s1394_fa_list_remove(hal, target, type);
	if (result == DDI_SUCCESS) {
		if (s1394_fa_list_is_empty(hal, type)) {
			s1394_fa_free_addr(hal, type);
		}
	} else {
		TNF_PROBE_0(s1394_fcp_unregister_common_error_list,
		    S1394_TNF_SL_FCP_ERROR, "");
	}

	rw_exit(&hal->target_list_rwlock);

	return (result);
}

/*
 * s1394_fcp_write_check_cmd()
 *    Check if an FCP command is formed correctly;
 *    set cmd_result and return DDI_FAILURE if not.
 */
int
s1394_fcp_write_check_cmd(cmd1394_cmd_t *cmd)
{
	int	len;

	/* 4-byte writes must be quadlet writes */
	if (cmd->cmd_type == CMD1394_ASYNCH_WR_BLOCK) {
		len = cmd->cmd_u.b.blk_length;
		if (len == 4) {
			cmd->cmd_result = CMD1394_ETYPE_ERROR;
			TNF_PROBE_0(t1394_write_error_type,
			    S1394_TNF_SL_FCP_ERROR, "");
			return (DDI_FAILURE);
		}
	} else {
		len = 4;
	}

	/*
	 * request must be within FCP range. we avoid extra checks by
	 * using the fact that command and response are of the same size
	 */
	if ((cmd->cmd_addr & IEEE1394_ADDR_OFFSET_MASK) + len >
	    IEC61883_FCP_CMD_SIZE) {
		cmd->cmd_result = CMD1394_EADDRESS_ERROR;
		TNF_PROBE_0(t1394_write_error_addr, S1394_TNF_SL_FCP_ERROR, "");
		return (DDI_FAILURE);
	}

	/* some options don't make sense for FCP commands */
	if (cmd->cmd_options & CMD1394_OVERRIDE_ADDR) {
		cmd->cmd_result = CMD1394_EINVALID_COMMAND;
		TNF_PROBE_0(t1394_write_error_opt, S1394_TNF_SL_FCP_ERROR, "");
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

static void
s1394_fcp_resp_recv_write_request(cmd1394_cmd_t *req)
{
	s1394_fcp_recv_write_request(req, S1394_FA_TYPE_FCP_CTL);
}

static void
s1394_fcp_cmd_recv_write_request(cmd1394_cmd_t *req)
{
	s1394_fcp_recv_write_request(req, S1394_FA_TYPE_FCP_TGT);
}

/*
 * s1394_fcp_recv_write_request()
 *    Common write request handler
 */
static void
s1394_fcp_recv_write_request(cmd1394_cmd_t *req, s1394_fa_type_t type)
{
	s1394_hal_t	*hal = (s1394_hal_t *)req->cmd_callback_arg;
	s1394_target_t	*target;
	s1394_fa_target_t *fat;
	uint_t		saved_gen;
	int		num_retries = 0;
	int		(*cb)(cmd1394_cmd_t *req);
	boolean_t	restored = B_FALSE;
	int		ret = T1394_REQ_UNCLAIMED;

	TNF_PROBE_0_DEBUG(s1394_fcp_recv_write_request_enter,
	    S1394_TNF_SL_FCP_STACK, "");

	rw_enter(&hal->target_list_rwlock, RW_READER);

start:
	target = hal->hal_fa[type].fal_head;

	if (target) {
		s1394_fa_restore_cmd(hal, req);
		restored = B_TRUE;

		/* Find a target that claims the request */
		do {
			fat = &target->target_fa[type];

			cb = fat->fat_u.fcp.fc_evts.fcp_write_request;
			if (cb == NULL) {
				continue;
			}
			req->cmd_callback_arg = fat->fat_u.fcp.fc_evts.fcp_arg;

			saved_gen = s1394_fa_list_gen(hal, type);

			rw_exit(&hal->target_list_rwlock);
			ret = cb(req);
			rw_enter(&hal->target_list_rwlock, RW_READER);

			if (ret == T1394_REQ_CLAIMED) {
				break;
			}

			/*
			 * List could change while we dropped the lock. In such
			 * case, start all over again, because missing a write
			 * request can have more serious consequences for a
			 * target than receiving same request more than once
			 */
			if (saved_gen != s1394_fa_list_gen(hal, type)) {
				TNF_PROBE_2(s1394_fcp_recv_write_request_error,
				    S1394_TNF_SL_FCP_ERROR, "",
				    tnf_string, msg, "list gen changed",
				    tnf_opaque, num_retries, num_retries);
				num_retries++;
				if (num_retries <= s1394_fcp_notify_retry_cnt) {
					goto start;
				} else {
					break;
				}
			}

			target = fat->fat_next;
		} while (target != NULL);
	}

	rw_exit(&hal->target_list_rwlock);

	if (ret != T1394_REQ_CLAIMED) {
		TNF_PROBE_0(s1394_fcp_recv_write_request_error_unclaimed,
		    S1394_TNF_SL_FCP_ERROR, "");
		if (restored) {
			s1394_fa_convert_cmd(hal, req);
		}
		s1394_fcp_recv_write_unclaimed(hal, req);
	}

	TNF_PROBE_0_DEBUG(s1394_fcp_recv_write_request_exit,
	    S1394_TNF_SL_FCP_STACK, "");
}

/*
 * none of the targets claimed the request - send an appropriate response
 */
static void
s1394_fcp_recv_write_unclaimed(s1394_hal_t *hal, cmd1394_cmd_t *req)
{
	req->cmd_result = IEEE1394_RESP_ADDRESS_ERROR;
	(void) s1394_send_response(hal, req);
}