xref: /freebsd/sys/dev/firewire/sbp_targ.c (revision df57947f083046d50552e99b91074927d2458708)
1098ca2bdSWarner Losh /*-
2*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni  *
4e9e688e2SHidetoshi Shimokawa  * Copyright (C) 2003
5e9e688e2SHidetoshi Shimokawa  * 	Hidetoshi Shimokawa. All rights reserved.
6e9e688e2SHidetoshi Shimokawa  *
7e9e688e2SHidetoshi Shimokawa  * Redistribution and use in source and binary forms, with or without
8e9e688e2SHidetoshi Shimokawa  * modification, are permitted provided that the following conditions
9e9e688e2SHidetoshi Shimokawa  * are met:
10e9e688e2SHidetoshi Shimokawa  * 1. Redistributions of source code must retain the above copyright
11e9e688e2SHidetoshi Shimokawa  *    notice, this list of conditions and the following disclaimer.
12e9e688e2SHidetoshi Shimokawa  * 2. Redistributions in binary form must reproduce the above copyright
13e9e688e2SHidetoshi Shimokawa  *    notice, this list of conditions and the following disclaimer in the
14e9e688e2SHidetoshi Shimokawa  *    documentation and/or other materials provided with the distribution.
15e9e688e2SHidetoshi Shimokawa  * 3. All advertising materials mentioning features or use of this software
16e9e688e2SHidetoshi Shimokawa  *    must display the following acknowledgement:
17e9e688e2SHidetoshi Shimokawa  *
18e9e688e2SHidetoshi Shimokawa  *	This product includes software developed by Hidetoshi Shimokawa.
19e9e688e2SHidetoshi Shimokawa  *
20e9e688e2SHidetoshi Shimokawa  * 4. Neither the name of the author nor the names of its contributors
21e9e688e2SHidetoshi Shimokawa  *    may be used to endorse or promote products derived from this software
22e9e688e2SHidetoshi Shimokawa  *    without specific prior written permission.
23e9e688e2SHidetoshi Shimokawa  *
24e9e688e2SHidetoshi Shimokawa  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25e9e688e2SHidetoshi Shimokawa  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26e9e688e2SHidetoshi Shimokawa  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27e9e688e2SHidetoshi Shimokawa  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28e9e688e2SHidetoshi Shimokawa  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29e9e688e2SHidetoshi Shimokawa  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30e9e688e2SHidetoshi Shimokawa  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31e9e688e2SHidetoshi Shimokawa  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32e9e688e2SHidetoshi Shimokawa  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33e9e688e2SHidetoshi Shimokawa  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34e9e688e2SHidetoshi Shimokawa  * SUCH DAMAGE.
35e9e688e2SHidetoshi Shimokawa  *
36e9e688e2SHidetoshi Shimokawa  * $FreeBSD$
37e9e688e2SHidetoshi Shimokawa  */
38e9e688e2SHidetoshi Shimokawa 
39e9e688e2SHidetoshi Shimokawa #include <sys/param.h>
40e9e688e2SHidetoshi Shimokawa #include <sys/kernel.h>
41e9e688e2SHidetoshi Shimokawa #include <sys/systm.h>
42e9e688e2SHidetoshi Shimokawa #include <sys/sysctl.h>
43e9e688e2SHidetoshi Shimokawa #include <sys/types.h>
44e9e688e2SHidetoshi Shimokawa #include <sys/conf.h>
45e9e688e2SHidetoshi Shimokawa #include <sys/malloc.h>
461cc052e8SKenneth D. Merry #include <sys/endian.h>
47e9e688e2SHidetoshi Shimokawa 
48e9e688e2SHidetoshi Shimokawa #include <sys/bus.h>
49e9e688e2SHidetoshi Shimokawa #include <machine/bus.h>
50e9e688e2SHidetoshi Shimokawa 
51e9e688e2SHidetoshi Shimokawa #include <dev/firewire/firewire.h>
52e9e688e2SHidetoshi Shimokawa #include <dev/firewire/firewirereg.h>
53e9e688e2SHidetoshi Shimokawa #include <dev/firewire/iec13213.h>
54e9e688e2SHidetoshi Shimokawa #include <dev/firewire/sbp.h>
55e9e688e2SHidetoshi Shimokawa #include <dev/firewire/fwmem.h>
56e9e688e2SHidetoshi Shimokawa 
57e9e688e2SHidetoshi Shimokawa #include <cam/cam.h>
58e9e688e2SHidetoshi Shimokawa #include <cam/cam_ccb.h>
59e9e688e2SHidetoshi Shimokawa #include <cam/cam_sim.h>
60e9e688e2SHidetoshi Shimokawa #include <cam/cam_xpt_sim.h>
61e9e688e2SHidetoshi Shimokawa #include <cam/cam_debug.h>
62e9e688e2SHidetoshi Shimokawa #include <cam/cam_periph.h>
63e9e688e2SHidetoshi Shimokawa #include <cam/scsi/scsi_all.h>
644636d244SSean Bruno #include <cam/scsi/scsi_message.h>
65e9e688e2SHidetoshi Shimokawa 
66a73ff510SHidetoshi Shimokawa #define SBP_TARG_RECV_LEN	8
67a73ff510SHidetoshi Shimokawa #define MAX_INITIATORS		8
68e9e688e2SHidetoshi Shimokawa #define MAX_LUN			63
69a73ff510SHidetoshi Shimokawa #define MAX_LOGINS		63
70a73ff510SHidetoshi Shimokawa #define MAX_NODES		63
71e9e688e2SHidetoshi Shimokawa /*
72e9e688e2SHidetoshi Shimokawa  * management/command block agent registers
73e9e688e2SHidetoshi Shimokawa  *
74e9e688e2SHidetoshi Shimokawa  * BASE 0xffff f001 0000 management port
75a73ff510SHidetoshi Shimokawa  * BASE 0xffff f001 0020 command port for login id 0
76a73ff510SHidetoshi Shimokawa  * BASE 0xffff f001 0040 command port for login id 1
77e9e688e2SHidetoshi Shimokawa  *
78e9e688e2SHidetoshi Shimokawa  */
79e9e688e2SHidetoshi Shimokawa #define SBP_TARG_MGM	 0x10000	/* offset from 0xffff f000 000 */
80e9e688e2SHidetoshi Shimokawa #define SBP_TARG_BIND_HI	0xffff
81e9e688e2SHidetoshi Shimokawa #define SBP_TARG_BIND_LO(l)	(0xf0000000 + SBP_TARG_MGM + 0x20 * ((l) + 1))
82e9e688e2SHidetoshi Shimokawa #define SBP_TARG_BIND_START	(((u_int64_t)SBP_TARG_BIND_HI << 32) | \
83e9e688e2SHidetoshi Shimokawa 				    SBP_TARG_BIND_LO(-1))
84e9e688e2SHidetoshi Shimokawa #define SBP_TARG_BIND_END	(((u_int64_t)SBP_TARG_BIND_HI << 32) | \
85a73ff510SHidetoshi Shimokawa 				    SBP_TARG_BIND_LO(MAX_LOGINS))
86a73ff510SHidetoshi Shimokawa #define SBP_TARG_LOGIN_ID(lo)	(((lo) - SBP_TARG_BIND_LO(0))/0x20)
87e9e688e2SHidetoshi Shimokawa 
88e9e688e2SHidetoshi Shimokawa #define FETCH_MGM	0
89e9e688e2SHidetoshi Shimokawa #define FETCH_CMD	1
90e9e688e2SHidetoshi Shimokawa #define FETCH_POINTER	2
91e9e688e2SHidetoshi Shimokawa 
92a73ff510SHidetoshi Shimokawa #define F_LINK_ACTIVE	(1 << 0)
93a73ff510SHidetoshi Shimokawa #define F_ATIO_STARVED	(1 << 1)
94a73ff510SHidetoshi Shimokawa #define F_LOGIN		(1 << 2)
95a73ff510SHidetoshi Shimokawa #define F_HOLD		(1 << 3)
96a73ff510SHidetoshi Shimokawa #define F_FREEZED	(1 << 4)
97a73ff510SHidetoshi Shimokawa 
98d745c852SEd Schouten static MALLOC_DEFINE(M_SBP_TARG, "sbp_targ", "SBP-II/FireWire target mode");
99e9e688e2SHidetoshi Shimokawa 
100e9e688e2SHidetoshi Shimokawa static int debug = 0;
101e9e688e2SHidetoshi Shimokawa 
102e9e688e2SHidetoshi Shimokawa SYSCTL_INT(_debug, OID_AUTO, sbp_targ_debug, CTLFLAG_RW, &debug, 0,
103e9e688e2SHidetoshi Shimokawa         "SBP target mode debug flag");
104e9e688e2SHidetoshi Shimokawa 
105a73ff510SHidetoshi Shimokawa struct sbp_targ_login {
106a73ff510SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
107a73ff510SHidetoshi Shimokawa 	struct fw_device *fwdev;
108a73ff510SHidetoshi Shimokawa 	struct sbp_login_res loginres;
10903161bbcSDoug Rabson 	uint16_t fifo_hi;
11003161bbcSDoug Rabson 	uint16_t last_hi;
11103161bbcSDoug Rabson 	uint32_t fifo_lo;
11203161bbcSDoug Rabson 	uint32_t last_lo;
113a73ff510SHidetoshi Shimokawa 	STAILQ_HEAD(, orb_info) orbs;
114a73ff510SHidetoshi Shimokawa 	STAILQ_ENTRY(sbp_targ_login) link;
11503161bbcSDoug Rabson 	uint16_t hold_sec;
11603161bbcSDoug Rabson 	uint16_t id;
11703161bbcSDoug Rabson 	uint8_t flags;
11803161bbcSDoug Rabson 	uint8_t spd;
119a73ff510SHidetoshi Shimokawa 	struct callout hold_callout;
120a73ff510SHidetoshi Shimokawa };
121a73ff510SHidetoshi Shimokawa 
122a73ff510SHidetoshi Shimokawa struct sbp_targ_lstate {
12303161bbcSDoug Rabson 	uint16_t lun;
124a73ff510SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
125a73ff510SHidetoshi Shimokawa 	struct cam_path *path;
126a73ff510SHidetoshi Shimokawa 	struct ccb_hdr_slist accept_tios;
127a73ff510SHidetoshi Shimokawa 	struct ccb_hdr_slist immed_notifies;
128a73ff510SHidetoshi Shimokawa 	struct crom_chunk model;
12903161bbcSDoug Rabson 	uint32_t flags;
130a73ff510SHidetoshi Shimokawa 	STAILQ_HEAD(, sbp_targ_login) logins;
131a73ff510SHidetoshi Shimokawa };
132a73ff510SHidetoshi Shimokawa 
133e9e688e2SHidetoshi Shimokawa struct sbp_targ_softc {
134e9e688e2SHidetoshi Shimokawa         struct firewire_dev_comm fd;
135e9e688e2SHidetoshi Shimokawa 	struct cam_sim *sim;
136e9e688e2SHidetoshi Shimokawa 	struct cam_path *path;
137e9e688e2SHidetoshi Shimokawa 	struct fw_bind fwb;
138e9e688e2SHidetoshi Shimokawa 	int ndevs;
139a73ff510SHidetoshi Shimokawa 	int flags;
140e9e688e2SHidetoshi Shimokawa 	struct crom_chunk unit;
141e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate[MAX_LUN];
142e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *black_hole;
143a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *logins[MAX_LOGINS];
1449950b741SHidetoshi Shimokawa 	struct mtx mtx;
145e9e688e2SHidetoshi Shimokawa };
1469950b741SHidetoshi Shimokawa #define SBP_LOCK(sc) mtx_lock(&(sc)->mtx)
1479950b741SHidetoshi Shimokawa #define SBP_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
148e9e688e2SHidetoshi Shimokawa 
149e9e688e2SHidetoshi Shimokawa struct corb4 {
150e9e688e2SHidetoshi Shimokawa #if BYTE_ORDER == BIG_ENDIAN
15103161bbcSDoug Rabson 	uint32_t n:1,
152e9e688e2SHidetoshi Shimokawa 		  rq_fmt:2,
153e9e688e2SHidetoshi Shimokawa 		  :1,
154e9e688e2SHidetoshi Shimokawa 		  dir:1,
155e9e688e2SHidetoshi Shimokawa 		  spd:3,
156e9e688e2SHidetoshi Shimokawa 		  max_payload:4,
157e9e688e2SHidetoshi Shimokawa 		  page_table_present:1,
158e9e688e2SHidetoshi Shimokawa 		  page_size:3,
159e9e688e2SHidetoshi Shimokawa 		  data_size:16;
160e9e688e2SHidetoshi Shimokawa #else
16103161bbcSDoug Rabson 	uint32_t data_size:16,
162e9e688e2SHidetoshi Shimokawa 		  page_size:3,
163e9e688e2SHidetoshi Shimokawa 		  page_table_present:1,
164e9e688e2SHidetoshi Shimokawa 		  max_payload:4,
165e9e688e2SHidetoshi Shimokawa 		  spd:3,
166e9e688e2SHidetoshi Shimokawa 		  dir:1,
167e9e688e2SHidetoshi Shimokawa 		  :1,
168e9e688e2SHidetoshi Shimokawa 		  rq_fmt:2,
169e9e688e2SHidetoshi Shimokawa 		  n:1;
170e9e688e2SHidetoshi Shimokawa #endif
171e9e688e2SHidetoshi Shimokawa };
172e9e688e2SHidetoshi Shimokawa 
173e9e688e2SHidetoshi Shimokawa struct morb4 {
174e9e688e2SHidetoshi Shimokawa #if BYTE_ORDER == BIG_ENDIAN
17503161bbcSDoug Rabson 	uint32_t n:1,
176e9e688e2SHidetoshi Shimokawa 		  rq_fmt:2,
177e9e688e2SHidetoshi Shimokawa 		  :9,
178e9e688e2SHidetoshi Shimokawa 		  fun:4,
179e9e688e2SHidetoshi Shimokawa 		  id:16;
180e9e688e2SHidetoshi Shimokawa #else
18103161bbcSDoug Rabson 	uint32_t id:16,
182e9e688e2SHidetoshi Shimokawa 		  fun:4,
183e9e688e2SHidetoshi Shimokawa 		  :9,
184e9e688e2SHidetoshi Shimokawa 		  rq_fmt:2,
185e9e688e2SHidetoshi Shimokawa 		  n:1;
186e9e688e2SHidetoshi Shimokawa #endif
187e9e688e2SHidetoshi Shimokawa };
188e9e688e2SHidetoshi Shimokawa 
1894636d244SSean Bruno 
1904636d244SSean Bruno /*
1914636d244SSean Bruno  * Urestricted page table format
1924636d244SSean Bruno  * states that the segment length
1934636d244SSean Bruno  * and high base addr are in the first
1944636d244SSean Bruno  * 32 bits and the base low is in
1954636d244SSean Bruno  * the second
1964636d244SSean Bruno  */
1974636d244SSean Bruno struct unrestricted_page_table_fmt {
1984636d244SSean Bruno 	uint16_t segment_len;
1994636d244SSean Bruno 	uint16_t segment_base_high;
2004636d244SSean Bruno 	uint32_t segment_base_low;
2014636d244SSean Bruno };
2024636d244SSean Bruno 
2034636d244SSean Bruno 
204e9e688e2SHidetoshi Shimokawa struct orb_info {
205e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
206e9e688e2SHidetoshi Shimokawa 	struct fw_device *fwdev;
207a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *login;
208e9e688e2SHidetoshi Shimokawa 	union ccb *ccb;
209e9e688e2SHidetoshi Shimokawa 	struct ccb_accept_tio *atio;
21003161bbcSDoug Rabson 	uint8_t state;
211e9e688e2SHidetoshi Shimokawa #define ORBI_STATUS_NONE	0
212e9e688e2SHidetoshi Shimokawa #define ORBI_STATUS_FETCH	1
213e9e688e2SHidetoshi Shimokawa #define ORBI_STATUS_ATIO	2
214e9e688e2SHidetoshi Shimokawa #define ORBI_STATUS_CTIO	3
215e9e688e2SHidetoshi Shimokawa #define ORBI_STATUS_STATUS	4
216e9e688e2SHidetoshi Shimokawa #define ORBI_STATUS_POINTER	5
217e9e688e2SHidetoshi Shimokawa #define ORBI_STATUS_ABORTED	7
21803161bbcSDoug Rabson 	uint8_t refcount;
21903161bbcSDoug Rabson 	uint16_t orb_hi;
22003161bbcSDoug Rabson 	uint32_t orb_lo;
22103161bbcSDoug Rabson 	uint32_t data_hi;
22203161bbcSDoug Rabson 	uint32_t data_lo;
223e9e688e2SHidetoshi Shimokawa 	struct corb4 orb4;
224e9e688e2SHidetoshi Shimokawa 	STAILQ_ENTRY(orb_info) link;
22503161bbcSDoug Rabson 	uint32_t orb[8];
2264636d244SSean Bruno 	struct unrestricted_page_table_fmt *page_table;
2274636d244SSean Bruno 	struct unrestricted_page_table_fmt *cur_pte;
2284636d244SSean Bruno 	struct unrestricted_page_table_fmt *last_pte;
2294636d244SSean Bruno 	uint32_t  last_block_read;
230e9e688e2SHidetoshi Shimokawa 	struct sbp_status status;
231e9e688e2SHidetoshi Shimokawa };
232e9e688e2SHidetoshi Shimokawa 
233e9e688e2SHidetoshi Shimokawa static char *orb_fun_name[] = {
234e9e688e2SHidetoshi Shimokawa 	ORB_FUN_NAMES
235e9e688e2SHidetoshi Shimokawa };
236e9e688e2SHidetoshi Shimokawa 
237e9e688e2SHidetoshi Shimokawa static void sbp_targ_recv(struct fw_xfer *);
238e9e688e2SHidetoshi Shimokawa static void sbp_targ_fetch_orb(struct sbp_targ_softc *, struct fw_device *,
23903161bbcSDoug Rabson     uint16_t, uint32_t, struct sbp_targ_login *, int);
2404636d244SSean Bruno static void sbp_targ_xfer_pt(struct orb_info *);
2419950b741SHidetoshi Shimokawa static void sbp_targ_abort(struct sbp_targ_softc *, struct orb_info *);
242e9e688e2SHidetoshi Shimokawa 
243e9e688e2SHidetoshi Shimokawa static void
244e9e688e2SHidetoshi Shimokawa sbp_targ_identify(driver_t *driver, device_t parent)
245e9e688e2SHidetoshi Shimokawa {
246e9e688e2SHidetoshi Shimokawa 	BUS_ADD_CHILD(parent, 0, "sbp_targ", device_get_unit(parent));
247e9e688e2SHidetoshi Shimokawa }
248e9e688e2SHidetoshi Shimokawa 
249e9e688e2SHidetoshi Shimokawa static int
250e9e688e2SHidetoshi Shimokawa sbp_targ_probe(device_t dev)
251e9e688e2SHidetoshi Shimokawa {
252e9e688e2SHidetoshi Shimokawa 	device_t pa;
253e9e688e2SHidetoshi Shimokawa 
254e9e688e2SHidetoshi Shimokawa 	pa = device_get_parent(dev);
255e9e688e2SHidetoshi Shimokawa 	if (device_get_unit(dev) != device_get_unit(pa)) {
256e9e688e2SHidetoshi Shimokawa 		return (ENXIO);
257e9e688e2SHidetoshi Shimokawa 	}
258e9e688e2SHidetoshi Shimokawa 
259e9e688e2SHidetoshi Shimokawa 	device_set_desc(dev, "SBP-2/SCSI over FireWire target mode");
260e9e688e2SHidetoshi Shimokawa 	return (0);
261e9e688e2SHidetoshi Shimokawa }
262e9e688e2SHidetoshi Shimokawa 
263a73ff510SHidetoshi Shimokawa static void
264a73ff510SHidetoshi Shimokawa sbp_targ_dealloc_login(struct sbp_targ_login *login)
265a73ff510SHidetoshi Shimokawa {
266a73ff510SHidetoshi Shimokawa 	struct orb_info *orbi, *next;
267a73ff510SHidetoshi Shimokawa 
268a73ff510SHidetoshi Shimokawa 	if (login == NULL) {
26910d3ed64SHidetoshi Shimokawa 		printf("%s: login = NULL\n", __func__);
270a73ff510SHidetoshi Shimokawa 		return;
271a73ff510SHidetoshi Shimokawa 	}
272a73ff510SHidetoshi Shimokawa 	for (orbi = STAILQ_FIRST(&login->orbs); orbi != NULL; orbi = next) {
273a73ff510SHidetoshi Shimokawa 		next = STAILQ_NEXT(orbi, link);
2744636d244SSean Bruno 		if (debug)
2754636d244SSean Bruno 			printf("%s: free orbi %p\n", __func__, orbi);
276a73ff510SHidetoshi Shimokawa 		free(orbi, M_SBP_TARG);
2774636d244SSean Bruno 		orbi = NULL;
278a73ff510SHidetoshi Shimokawa 	}
279a73ff510SHidetoshi Shimokawa 	callout_stop(&login->hold_callout);
280a73ff510SHidetoshi Shimokawa 
281a73ff510SHidetoshi Shimokawa 	STAILQ_REMOVE(&login->lstate->logins, login, sbp_targ_login, link);
282a73ff510SHidetoshi Shimokawa 	login->lstate->sc->logins[login->id] = NULL;
2834636d244SSean Bruno 	if (debug)
2844636d244SSean Bruno 		printf("%s: free login %p\n", __func__, login);
285a73ff510SHidetoshi Shimokawa 	free((void *)login, M_SBP_TARG);
2864636d244SSean Bruno 	login = NULL;
287a73ff510SHidetoshi Shimokawa }
288a73ff510SHidetoshi Shimokawa 
289a73ff510SHidetoshi Shimokawa static void
290a73ff510SHidetoshi Shimokawa sbp_targ_hold_expire(void *arg)
291a73ff510SHidetoshi Shimokawa {
292a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *login;
293a73ff510SHidetoshi Shimokawa 
294a73ff510SHidetoshi Shimokawa 	login = (struct sbp_targ_login *)arg;
295a73ff510SHidetoshi Shimokawa 
296a73ff510SHidetoshi Shimokawa 	if (login->flags & F_HOLD) {
29710d3ed64SHidetoshi Shimokawa 		printf("%s: login_id=%d expired\n", __func__, login->id);
298a73ff510SHidetoshi Shimokawa 		sbp_targ_dealloc_login(login);
299a73ff510SHidetoshi Shimokawa 	} else {
30010d3ed64SHidetoshi Shimokawa 		printf("%s: login_id=%d not hold\n", __func__, login->id);
301a73ff510SHidetoshi Shimokawa 	}
302a73ff510SHidetoshi Shimokawa }
303a73ff510SHidetoshi Shimokawa 
304e9e688e2SHidetoshi Shimokawa static void
305e9e688e2SHidetoshi Shimokawa sbp_targ_post_busreset(void *arg)
306e9e688e2SHidetoshi Shimokawa {
307e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
308e9e688e2SHidetoshi Shimokawa 	struct crom_src *src;
309e9e688e2SHidetoshi Shimokawa 	struct crom_chunk *root;
310e9e688e2SHidetoshi Shimokawa 	struct crom_chunk *unit;
311e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
312a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *login;
313e9e688e2SHidetoshi Shimokawa 	int i;
314e9e688e2SHidetoshi Shimokawa 
315e9e688e2SHidetoshi Shimokawa 	sc = (struct sbp_targ_softc *)arg;
316e9e688e2SHidetoshi Shimokawa 	src = sc->fd.fc->crom_src;
317e9e688e2SHidetoshi Shimokawa 	root = sc->fd.fc->crom_root;
318e9e688e2SHidetoshi Shimokawa 
319e9e688e2SHidetoshi Shimokawa 	unit = &sc->unit;
320e9e688e2SHidetoshi Shimokawa 
321a73ff510SHidetoshi Shimokawa 	if ((sc->flags & F_FREEZED) == 0) {
322a73ff510SHidetoshi Shimokawa 		sc->flags |= F_FREEZED;
323a73ff510SHidetoshi Shimokawa 		xpt_freeze_simq(sc->sim, /*count*/1);
324a73ff510SHidetoshi Shimokawa 	} else {
32510d3ed64SHidetoshi Shimokawa 		printf("%s: already freezed\n", __func__);
326a73ff510SHidetoshi Shimokawa 	}
327a73ff510SHidetoshi Shimokawa 
328e9e688e2SHidetoshi Shimokawa 	bzero(unit, sizeof(struct crom_chunk));
329e9e688e2SHidetoshi Shimokawa 
330e9e688e2SHidetoshi Shimokawa 	crom_add_chunk(src, root, unit, CROM_UDIR);
331e9e688e2SHidetoshi Shimokawa 	crom_add_entry(unit, CSRKEY_SPEC, CSRVAL_ANSIT10);
332e9e688e2SHidetoshi Shimokawa 	crom_add_entry(unit, CSRKEY_VER, CSRVAL_T10SBP2);
333e9e688e2SHidetoshi Shimokawa 	crom_add_entry(unit, CSRKEY_COM_SPEC, CSRVAL_ANSIT10);
334e9e688e2SHidetoshi Shimokawa 	crom_add_entry(unit, CSRKEY_COM_SET, CSRVAL_SCSI);
335e9e688e2SHidetoshi Shimokawa 
336e9e688e2SHidetoshi Shimokawa 	crom_add_entry(unit, CROM_MGM, SBP_TARG_MGM >> 2);
337e9e688e2SHidetoshi Shimokawa 	crom_add_entry(unit, CSRKEY_UNIT_CH, (10<<8) | 8);
338e9e688e2SHidetoshi Shimokawa 
339e9e688e2SHidetoshi Shimokawa 	for (i = 0; i < MAX_LUN; i++) {
340e9e688e2SHidetoshi Shimokawa 		lstate = sc->lstate[i];
341e9e688e2SHidetoshi Shimokawa 		if (lstate == NULL)
342e9e688e2SHidetoshi Shimokawa 			continue;
343e9e688e2SHidetoshi Shimokawa 		crom_add_entry(unit, CSRKEY_FIRM_VER, 1);
344e9e688e2SHidetoshi Shimokawa 		crom_add_entry(unit, CROM_LUN, i);
345e9e688e2SHidetoshi Shimokawa 		crom_add_entry(unit, CSRKEY_MODEL, 1);
346e9e688e2SHidetoshi Shimokawa 		crom_add_simple_text(src, unit, &lstate->model, "TargetMode");
347e9e688e2SHidetoshi Shimokawa 	}
348a73ff510SHidetoshi Shimokawa 
349a73ff510SHidetoshi Shimokawa 	/* Process for reconnection hold time */
350a73ff510SHidetoshi Shimokawa 	for (i = 0; i < MAX_LOGINS; i++) {
351a73ff510SHidetoshi Shimokawa 		login = sc->logins[i];
352a73ff510SHidetoshi Shimokawa 		if (login == NULL)
353a73ff510SHidetoshi Shimokawa 			continue;
3549950b741SHidetoshi Shimokawa 		sbp_targ_abort(sc, STAILQ_FIRST(&login->orbs));
355a73ff510SHidetoshi Shimokawa 		if (login->flags & F_LOGIN) {
356a73ff510SHidetoshi Shimokawa 			login->flags |= F_HOLD;
357a73ff510SHidetoshi Shimokawa 			callout_reset(&login->hold_callout,
358a73ff510SHidetoshi Shimokawa 			    hz * login->hold_sec,
359a73ff510SHidetoshi Shimokawa 			    sbp_targ_hold_expire, (void *)login);
360a73ff510SHidetoshi Shimokawa 		}
361a73ff510SHidetoshi Shimokawa 	}
362a73ff510SHidetoshi Shimokawa }
363a73ff510SHidetoshi Shimokawa 
364a73ff510SHidetoshi Shimokawa static void
365a73ff510SHidetoshi Shimokawa sbp_targ_post_explore(void *arg)
366a73ff510SHidetoshi Shimokawa {
367a73ff510SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
368a73ff510SHidetoshi Shimokawa 
369a73ff510SHidetoshi Shimokawa 	sc = (struct sbp_targ_softc *)arg;
370a73ff510SHidetoshi Shimokawa 	sc->flags &= ~F_FREEZED;
371a73ff510SHidetoshi Shimokawa 	xpt_release_simq(sc->sim, /*run queue*/TRUE);
372a73ff510SHidetoshi Shimokawa 	return;
373e9e688e2SHidetoshi Shimokawa }
374e9e688e2SHidetoshi Shimokawa 
375e9e688e2SHidetoshi Shimokawa static cam_status
376e9e688e2SHidetoshi Shimokawa sbp_targ_find_devs(struct sbp_targ_softc *sc, union ccb *ccb,
377e9e688e2SHidetoshi Shimokawa     struct sbp_targ_lstate **lstate, int notfound_failure)
378e9e688e2SHidetoshi Shimokawa {
379e9e688e2SHidetoshi Shimokawa 	u_int lun;
380e9e688e2SHidetoshi Shimokawa 
381e9e688e2SHidetoshi Shimokawa 	/* XXX 0 is the only vaild target_id */
382e9e688e2SHidetoshi Shimokawa 	if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD &&
383e9e688e2SHidetoshi Shimokawa 	    ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
384e9e688e2SHidetoshi Shimokawa 		*lstate = sc->black_hole;
3854636d244SSean Bruno 		if (debug)
3864636d244SSean Bruno 			printf("setting black hole for this target id(%d)\n", ccb->ccb_h.target_id);
387e9e688e2SHidetoshi Shimokawa 		return (CAM_REQ_CMP);
388e9e688e2SHidetoshi Shimokawa 	}
389e9e688e2SHidetoshi Shimokawa 
390e9e688e2SHidetoshi Shimokawa 	lun = ccb->ccb_h.target_lun;
391e9e688e2SHidetoshi Shimokawa 	if (lun >= MAX_LUN)
392e9e688e2SHidetoshi Shimokawa 		return (CAM_LUN_INVALID);
393e9e688e2SHidetoshi Shimokawa 
394e9e688e2SHidetoshi Shimokawa 	*lstate = sc->lstate[lun];
395e9e688e2SHidetoshi Shimokawa 
3964636d244SSean Bruno 	if (notfound_failure != 0 && *lstate == NULL) {
3974636d244SSean Bruno 		if (debug)
3984636d244SSean Bruno 			printf("%s: lstate for lun is invalid, target(%d), lun(%d)\n",
3994636d244SSean Bruno 				__func__, ccb->ccb_h.target_id, lun);
400e9e688e2SHidetoshi Shimokawa 		return (CAM_PATH_INVALID);
4014636d244SSean Bruno 	} else
4024636d244SSean Bruno 		if (debug)
4034636d244SSean Bruno 			printf("%s: setting lstate for tgt(%d) lun(%d)\n",
4044636d244SSean Bruno 				__func__,ccb->ccb_h.target_id, lun);
405e9e688e2SHidetoshi Shimokawa 
406e9e688e2SHidetoshi Shimokawa 	return (CAM_REQ_CMP);
407e9e688e2SHidetoshi Shimokawa }
408e9e688e2SHidetoshi Shimokawa 
409e9e688e2SHidetoshi Shimokawa static void
410e9e688e2SHidetoshi Shimokawa sbp_targ_en_lun(struct sbp_targ_softc *sc, union ccb *ccb)
411e9e688e2SHidetoshi Shimokawa {
412e9e688e2SHidetoshi Shimokawa 	struct ccb_en_lun *cel = &ccb->cel;
413e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
414e9e688e2SHidetoshi Shimokawa 	cam_status status;
415e9e688e2SHidetoshi Shimokawa 
416e9e688e2SHidetoshi Shimokawa 	status = sbp_targ_find_devs(sc, ccb, &lstate, 0);
417e9e688e2SHidetoshi Shimokawa 	if (status != CAM_REQ_CMP) {
418e9e688e2SHidetoshi Shimokawa 		ccb->ccb_h.status = status;
419e9e688e2SHidetoshi Shimokawa 		return;
420e9e688e2SHidetoshi Shimokawa 	}
421e9e688e2SHidetoshi Shimokawa 
422e9e688e2SHidetoshi Shimokawa 	if (cel->enable != 0) {
423e9e688e2SHidetoshi Shimokawa 		if (lstate != NULL) {
424e9e688e2SHidetoshi Shimokawa 			xpt_print_path(ccb->ccb_h.path);
425e9e688e2SHidetoshi Shimokawa 			printf("Lun already enabled\n");
426e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
427e9e688e2SHidetoshi Shimokawa 			return;
428e9e688e2SHidetoshi Shimokawa 		}
429e9e688e2SHidetoshi Shimokawa 		if (cel->grp6_len != 0 || cel->grp7_len != 0) {
430e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_INVALID;
431e9e688e2SHidetoshi Shimokawa 			printf("Non-zero Group Codes\n");
432e9e688e2SHidetoshi Shimokawa 			return;
433e9e688e2SHidetoshi Shimokawa 		}
434e9e688e2SHidetoshi Shimokawa 		lstate = (struct sbp_targ_lstate *)
435e9e688e2SHidetoshi Shimokawa 		    malloc(sizeof(*lstate), M_SBP_TARG, M_NOWAIT | M_ZERO);
436e9e688e2SHidetoshi Shimokawa 		if (lstate == NULL) {
437e9e688e2SHidetoshi Shimokawa 			xpt_print_path(ccb->ccb_h.path);
438e9e688e2SHidetoshi Shimokawa 			printf("Couldn't allocate lstate\n");
439e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
440e9e688e2SHidetoshi Shimokawa 			return;
4414636d244SSean Bruno 		} else {
4424636d244SSean Bruno 			if (debug)
4434636d244SSean Bruno 				printf("%s: malloc'd lstate %p\n",__func__, lstate);
444e9e688e2SHidetoshi Shimokawa 		}
4454636d244SSean Bruno 		if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD) {
446e9e688e2SHidetoshi Shimokawa 			sc->black_hole = lstate;
4474636d244SSean Bruno 			if (debug)
4484636d244SSean Bruno 				printf("Blackhole set due to target id == %d\n",
4494636d244SSean Bruno 					ccb->ccb_h.target_id);
4504636d244SSean Bruno 		} else
451e9e688e2SHidetoshi Shimokawa 			sc->lstate[ccb->ccb_h.target_lun] = lstate;
4524636d244SSean Bruno 
453e9e688e2SHidetoshi Shimokawa 		memset(lstate, 0, sizeof(*lstate));
454e9e688e2SHidetoshi Shimokawa 		lstate->sc = sc;
455e9e688e2SHidetoshi Shimokawa 		status = xpt_create_path(&lstate->path, /*periph*/NULL,
456e9e688e2SHidetoshi Shimokawa 					 xpt_path_path_id(ccb->ccb_h.path),
457e9e688e2SHidetoshi Shimokawa 					 xpt_path_target_id(ccb->ccb_h.path),
458e9e688e2SHidetoshi Shimokawa 					 xpt_path_lun_id(ccb->ccb_h.path));
459e9e688e2SHidetoshi Shimokawa 		if (status != CAM_REQ_CMP) {
460e9e688e2SHidetoshi Shimokawa 			free(lstate, M_SBP_TARG);
4614636d244SSean Bruno 			lstate = NULL;
462e9e688e2SHidetoshi Shimokawa 			xpt_print_path(ccb->ccb_h.path);
463e9e688e2SHidetoshi Shimokawa 			printf("Couldn't allocate path\n");
464e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
465e9e688e2SHidetoshi Shimokawa 			return;
466e9e688e2SHidetoshi Shimokawa 		}
467e9e688e2SHidetoshi Shimokawa 		SLIST_INIT(&lstate->accept_tios);
468e9e688e2SHidetoshi Shimokawa 		SLIST_INIT(&lstate->immed_notifies);
469a73ff510SHidetoshi Shimokawa 		STAILQ_INIT(&lstate->logins);
470e9e688e2SHidetoshi Shimokawa 
471e9e688e2SHidetoshi Shimokawa 		ccb->ccb_h.status = CAM_REQ_CMP;
472e9e688e2SHidetoshi Shimokawa 		xpt_print_path(ccb->ccb_h.path);
473e9e688e2SHidetoshi Shimokawa 		printf("Lun now enabled for target mode\n");
474e9e688e2SHidetoshi Shimokawa 		/* bus reset */
475e9e688e2SHidetoshi Shimokawa 		sc->fd.fc->ibr(sc->fd.fc);
476e9e688e2SHidetoshi Shimokawa 	} else {
477a73ff510SHidetoshi Shimokawa 		struct sbp_targ_login *login, *next;
478a73ff510SHidetoshi Shimokawa 
479e9e688e2SHidetoshi Shimokawa 		if (lstate == NULL) {
480e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_LUN_INVALID;
4814636d244SSean Bruno 			printf("Invalid lstate for this target\n");
482e9e688e2SHidetoshi Shimokawa 			return;
483e9e688e2SHidetoshi Shimokawa 		}
484e9e688e2SHidetoshi Shimokawa 		ccb->ccb_h.status = CAM_REQ_CMP;
485e9e688e2SHidetoshi Shimokawa 
486e9e688e2SHidetoshi Shimokawa 		if (SLIST_FIRST(&lstate->accept_tios) != NULL) {
487e9e688e2SHidetoshi Shimokawa 			printf("ATIOs pending\n");
488e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_INVALID;
489e9e688e2SHidetoshi Shimokawa 		}
490e9e688e2SHidetoshi Shimokawa 
491e9e688e2SHidetoshi Shimokawa 		if (SLIST_FIRST(&lstate->immed_notifies) != NULL) {
492e9e688e2SHidetoshi Shimokawa 			printf("INOTs pending\n");
493e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_INVALID;
494e9e688e2SHidetoshi Shimokawa 		}
495e9e688e2SHidetoshi Shimokawa 
496e9e688e2SHidetoshi Shimokawa 		if (ccb->ccb_h.status != CAM_REQ_CMP) {
4974636d244SSean Bruno 			printf("status != CAM_REQ_CMP\n");
498e9e688e2SHidetoshi Shimokawa 			return;
499e9e688e2SHidetoshi Shimokawa 		}
500e9e688e2SHidetoshi Shimokawa 
501e9e688e2SHidetoshi Shimokawa 		xpt_print_path(ccb->ccb_h.path);
502e9e688e2SHidetoshi Shimokawa 		printf("Target mode disabled\n");
503e9e688e2SHidetoshi Shimokawa 		xpt_free_path(lstate->path);
504e9e688e2SHidetoshi Shimokawa 
505a73ff510SHidetoshi Shimokawa 		for (login = STAILQ_FIRST(&lstate->logins); login != NULL;
506a73ff510SHidetoshi Shimokawa 		    login = next) {
507a73ff510SHidetoshi Shimokawa 			next = STAILQ_NEXT(login, link);
508a73ff510SHidetoshi Shimokawa 			sbp_targ_dealloc_login(login);
509e9e688e2SHidetoshi Shimokawa 		}
510e9e688e2SHidetoshi Shimokawa 
511e9e688e2SHidetoshi Shimokawa 		if (ccb->ccb_h.target_id == CAM_TARGET_WILDCARD)
512e9e688e2SHidetoshi Shimokawa 			sc->black_hole = NULL;
513e9e688e2SHidetoshi Shimokawa 		else
514e9e688e2SHidetoshi Shimokawa 			sc->lstate[ccb->ccb_h.target_lun] = NULL;
5154636d244SSean Bruno 		if (debug)
5164636d244SSean Bruno 			printf("%s: free lstate %p\n", __func__, lstate);
517e9e688e2SHidetoshi Shimokawa 		free(lstate, M_SBP_TARG);
5184636d244SSean Bruno 		lstate = NULL;
519e9e688e2SHidetoshi Shimokawa 
520e9e688e2SHidetoshi Shimokawa 		/* bus reset */
521e9e688e2SHidetoshi Shimokawa 		sc->fd.fc->ibr(sc->fd.fc);
522e9e688e2SHidetoshi Shimokawa 	}
523e9e688e2SHidetoshi Shimokawa }
524e9e688e2SHidetoshi Shimokawa 
525e9e688e2SHidetoshi Shimokawa static void
526e9e688e2SHidetoshi Shimokawa sbp_targ_send_lstate_events(struct sbp_targ_softc *sc,
527e9e688e2SHidetoshi Shimokawa     struct sbp_targ_lstate *lstate)
528e9e688e2SHidetoshi Shimokawa {
529e9e688e2SHidetoshi Shimokawa #if 0
530e9e688e2SHidetoshi Shimokawa 	struct ccb_hdr *ccbh;
531b79dc8a8SKenneth D. Merry 	struct ccb_immediate_notify *inot;
532e9e688e2SHidetoshi Shimokawa 
53310d3ed64SHidetoshi Shimokawa 	printf("%s: not implemented yet\n", __func__);
534e9e688e2SHidetoshi Shimokawa #endif
535e9e688e2SHidetoshi Shimokawa }
536e9e688e2SHidetoshi Shimokawa 
5379950b741SHidetoshi Shimokawa 
5389950b741SHidetoshi Shimokawa static __inline void
5399950b741SHidetoshi Shimokawa sbp_targ_remove_orb_info_locked(struct sbp_targ_login *login, struct orb_info *orbi)
5409950b741SHidetoshi Shimokawa {
5419950b741SHidetoshi Shimokawa 	STAILQ_REMOVE(&login->orbs, orbi, orb_info, link);
5429950b741SHidetoshi Shimokawa }
5439950b741SHidetoshi Shimokawa 
544e9e688e2SHidetoshi Shimokawa static __inline void
545a73ff510SHidetoshi Shimokawa sbp_targ_remove_orb_info(struct sbp_targ_login *login, struct orb_info *orbi)
546e9e688e2SHidetoshi Shimokawa {
5479950b741SHidetoshi Shimokawa 	SBP_LOCK(orbi->sc);
548a73ff510SHidetoshi Shimokawa 	STAILQ_REMOVE(&login->orbs, orbi, orb_info, link);
5499950b741SHidetoshi Shimokawa 	SBP_UNLOCK(orbi->sc);
550e9e688e2SHidetoshi Shimokawa }
551e9e688e2SHidetoshi Shimokawa 
552e9e688e2SHidetoshi Shimokawa /*
553e9e688e2SHidetoshi Shimokawa  * tag_id/init_id encoding
554e9e688e2SHidetoshi Shimokawa  *
555e9e688e2SHidetoshi Shimokawa  * tag_id and init_id has only 32bit for each.
556e9e688e2SHidetoshi Shimokawa  * scsi_target can handle very limited number(up to 15) of init_id.
557e9e688e2SHidetoshi Shimokawa  * we have to encode 48bit orb and 64bit EUI64 into these
558e9e688e2SHidetoshi Shimokawa  * variables.
559e9e688e2SHidetoshi Shimokawa  *
560e9e688e2SHidetoshi Shimokawa  * tag_id represents lower 32bit of ORB address.
561a73ff510SHidetoshi Shimokawa  * init_id represents login_id.
562e9e688e2SHidetoshi Shimokawa  *
563e9e688e2SHidetoshi Shimokawa  */
564e9e688e2SHidetoshi Shimokawa 
565e9e688e2SHidetoshi Shimokawa static struct orb_info *
566e9e688e2SHidetoshi Shimokawa sbp_targ_get_orb_info(struct sbp_targ_lstate *lstate,
567e9e688e2SHidetoshi Shimokawa     u_int tag_id, u_int init_id)
568e9e688e2SHidetoshi Shimokawa {
569a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *login;
570e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
571e9e688e2SHidetoshi Shimokawa 
572a73ff510SHidetoshi Shimokawa 	login = lstate->sc->logins[init_id];
573a73ff510SHidetoshi Shimokawa 	if (login == NULL) {
57410d3ed64SHidetoshi Shimokawa 		printf("%s: no such login\n", __func__);
575a73ff510SHidetoshi Shimokawa 		return (NULL);
576a73ff510SHidetoshi Shimokawa 	}
577a73ff510SHidetoshi Shimokawa 	STAILQ_FOREACH(orbi, &login->orbs, link)
578a73ff510SHidetoshi Shimokawa 		if (orbi->orb_lo == tag_id)
579e9e688e2SHidetoshi Shimokawa 			goto found;
5809950b741SHidetoshi Shimokawa 	printf("%s: orb not found tag_id=0x%08x init_id=%d\n",
5819950b741SHidetoshi Shimokawa 			 __func__, tag_id, init_id);
582e9e688e2SHidetoshi Shimokawa 	return (NULL);
583e9e688e2SHidetoshi Shimokawa found:
584e9e688e2SHidetoshi Shimokawa 	return (orbi);
585e9e688e2SHidetoshi Shimokawa }
586e9e688e2SHidetoshi Shimokawa 
587e9e688e2SHidetoshi Shimokawa static void
5889950b741SHidetoshi Shimokawa sbp_targ_abort(struct sbp_targ_softc *sc, struct orb_info *orbi)
589e9e688e2SHidetoshi Shimokawa {
590e9e688e2SHidetoshi Shimokawa 	struct orb_info *norbi;
591e9e688e2SHidetoshi Shimokawa 
5929950b741SHidetoshi Shimokawa 	SBP_LOCK(sc);
593e9e688e2SHidetoshi Shimokawa 	for (; orbi != NULL; orbi = norbi) {
5949950b741SHidetoshi Shimokawa 		printf("%s: status=%d ccb=%p\n", __func__, orbi->state, orbi->ccb);
595e9e688e2SHidetoshi Shimokawa 		norbi = STAILQ_NEXT(orbi, link);
596e9e688e2SHidetoshi Shimokawa 		if (orbi->state != ORBI_STATUS_ABORTED) {
597e9e688e2SHidetoshi Shimokawa 			if (orbi->ccb != NULL) {
598e9e688e2SHidetoshi Shimokawa 				orbi->ccb->ccb_h.status = CAM_REQ_ABORTED;
599e9e688e2SHidetoshi Shimokawa 				xpt_done(orbi->ccb);
600e9e688e2SHidetoshi Shimokawa 				orbi->ccb = NULL;
601e9e688e2SHidetoshi Shimokawa 			}
602e9e688e2SHidetoshi Shimokawa 			if (orbi->state <= ORBI_STATUS_ATIO) {
6039950b741SHidetoshi Shimokawa 				sbp_targ_remove_orb_info_locked(orbi->login, orbi);
6044636d244SSean Bruno 				if (debug)
6054636d244SSean Bruno 					printf("%s: free orbi %p\n", __func__, orbi);
606e9e688e2SHidetoshi Shimokawa 				free(orbi, M_SBP_TARG);
6074636d244SSean Bruno 				orbi = NULL;
608e9e688e2SHidetoshi Shimokawa 			} else
609e9e688e2SHidetoshi Shimokawa 				orbi->state = ORBI_STATUS_ABORTED;
610e9e688e2SHidetoshi Shimokawa 		}
611e9e688e2SHidetoshi Shimokawa 	}
6129950b741SHidetoshi Shimokawa 	SBP_UNLOCK(sc);
613e9e688e2SHidetoshi Shimokawa }
614e9e688e2SHidetoshi Shimokawa 
615e9e688e2SHidetoshi Shimokawa static void
616e9e688e2SHidetoshi Shimokawa sbp_targ_free_orbi(struct fw_xfer *xfer)
617e9e688e2SHidetoshi Shimokawa {
618e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
619e9e688e2SHidetoshi Shimokawa 
620e9e688e2SHidetoshi Shimokawa 	if (xfer->resp != 0) {
621e9e688e2SHidetoshi Shimokawa 		/* XXX */
62210d3ed64SHidetoshi Shimokawa 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
623e9e688e2SHidetoshi Shimokawa 	}
6244636d244SSean Bruno 	orbi = (struct orb_info *)xfer->sc;
6254636d244SSean Bruno 	if ( orbi->page_table != NULL ) {
6264636d244SSean Bruno 		if (debug)
6274636d244SSean Bruno 			printf("%s:  free orbi->page_table %p\n", __func__, orbi->page_table);
6284636d244SSean Bruno 		free(orbi->page_table, M_SBP_TARG);
6294636d244SSean Bruno 		orbi->page_table = NULL;
6304636d244SSean Bruno 	}
6314636d244SSean Bruno 	if (debug)
6324636d244SSean Bruno 		printf("%s: free orbi %p\n", __func__, orbi);
633e9e688e2SHidetoshi Shimokawa 	free(orbi, M_SBP_TARG);
6344636d244SSean Bruno 	orbi = NULL;
635e9e688e2SHidetoshi Shimokawa 	fw_xfer_free(xfer);
636e9e688e2SHidetoshi Shimokawa }
637e9e688e2SHidetoshi Shimokawa 
638e9e688e2SHidetoshi Shimokawa static void
639e9e688e2SHidetoshi Shimokawa sbp_targ_status_FIFO(struct orb_info *orbi,
64003161bbcSDoug Rabson     uint32_t fifo_hi, uint32_t fifo_lo, int dequeue)
641e9e688e2SHidetoshi Shimokawa {
642e9e688e2SHidetoshi Shimokawa 	struct fw_xfer *xfer;
643e9e688e2SHidetoshi Shimokawa 
644e9e688e2SHidetoshi Shimokawa 	if (dequeue)
645a73ff510SHidetoshi Shimokawa 		sbp_targ_remove_orb_info(orbi->login, orbi);
646e9e688e2SHidetoshi Shimokawa 
647e9e688e2SHidetoshi Shimokawa 	xfer = fwmem_write_block(orbi->fwdev, (void *)orbi,
6484636d244SSean Bruno 	    /*spd*/FWSPD_S400, fifo_hi, fifo_lo,
64903161bbcSDoug Rabson 	    sizeof(uint32_t) * (orbi->status.len + 1), (char *)&orbi->status,
650e9e688e2SHidetoshi Shimokawa 	    sbp_targ_free_orbi);
651e9e688e2SHidetoshi Shimokawa 
652e9e688e2SHidetoshi Shimokawa 	if (xfer == NULL) {
653e9e688e2SHidetoshi Shimokawa 		/* XXX */
65410d3ed64SHidetoshi Shimokawa 		printf("%s: xfer == NULL\n", __func__);
655e9e688e2SHidetoshi Shimokawa 	}
656e9e688e2SHidetoshi Shimokawa }
657e9e688e2SHidetoshi Shimokawa 
6584636d244SSean Bruno /*
6594636d244SSean Bruno  * Generate the appropriate CAM status for the
6604636d244SSean Bruno  * target.
6614636d244SSean Bruno  */
662e9e688e2SHidetoshi Shimokawa static void
663e9e688e2SHidetoshi Shimokawa sbp_targ_send_status(struct orb_info *orbi, union ccb *ccb)
664e9e688e2SHidetoshi Shimokawa {
665e9e688e2SHidetoshi Shimokawa 	struct sbp_status *sbp_status;
666ea47b6c8SMatt Jacob #if	0
6679950b741SHidetoshi Shimokawa 	struct orb_info *norbi;
668ea47b6c8SMatt Jacob #endif
669e9e688e2SHidetoshi Shimokawa 
670e9e688e2SHidetoshi Shimokawa 	sbp_status = &orbi->status;
671e9e688e2SHidetoshi Shimokawa 
672e9e688e2SHidetoshi Shimokawa 	orbi->state = ORBI_STATUS_STATUS;
673e9e688e2SHidetoshi Shimokawa 
674e9e688e2SHidetoshi Shimokawa 	sbp_status->resp = 0; /* XXX */
675e9e688e2SHidetoshi Shimokawa 	sbp_status->status = 0; /* XXX */
676e9e688e2SHidetoshi Shimokawa 	sbp_status->dead = 0; /* XXX */
677e9e688e2SHidetoshi Shimokawa 
6784636d244SSean Bruno 	ccb->ccb_h.status= CAM_REQ_CMP;
6794636d244SSean Bruno 
680e9e688e2SHidetoshi Shimokawa 	switch (ccb->csio.scsi_status) {
681e9e688e2SHidetoshi Shimokawa 	case SCSI_STATUS_OK:
682e9e688e2SHidetoshi Shimokawa 		if (debug)
68310d3ed64SHidetoshi Shimokawa 			printf("%s: STATUS_OK\n", __func__);
684e9e688e2SHidetoshi Shimokawa 		sbp_status->len = 1;
685e9e688e2SHidetoshi Shimokawa 		break;
686e9e688e2SHidetoshi Shimokawa 	case SCSI_STATUS_CHECK_COND:
6874636d244SSean Bruno 		if (debug)
6884636d244SSean Bruno 			printf("%s: STATUS SCSI_STATUS_CHECK_COND\n", __func__);
6894636d244SSean Bruno 		goto process_scsi_status;
690e9e688e2SHidetoshi Shimokawa 	case SCSI_STATUS_BUSY:
6914636d244SSean Bruno 		if (debug)
6924636d244SSean Bruno 			printf("%s: STATUS SCSI_STATUS_BUSY\n", __func__);
6934636d244SSean Bruno 		goto process_scsi_status;
694e9e688e2SHidetoshi Shimokawa 	case SCSI_STATUS_CMD_TERMINATED:
6954636d244SSean Bruno process_scsi_status:
696e9e688e2SHidetoshi Shimokawa 	{
697e9e688e2SHidetoshi Shimokawa 		struct sbp_cmd_status *sbp_cmd_status;
698e9e688e2SHidetoshi Shimokawa 		struct scsi_sense_data *sense;
6991cc052e8SKenneth D. Merry 		int error_code, sense_key, asc, ascq;
7001cc052e8SKenneth D. Merry 		uint8_t stream_bits;
7011cc052e8SKenneth D. Merry 		uint8_t sks[3];
7021cc052e8SKenneth D. Merry 		uint64_t info;
7031cc052e8SKenneth D. Merry 		int64_t sinfo;
7041cc052e8SKenneth D. Merry 		int sense_len;
705e9e688e2SHidetoshi Shimokawa 
706e9e688e2SHidetoshi Shimokawa 		sbp_cmd_status = (struct sbp_cmd_status *)&sbp_status->data[0];
707e9e688e2SHidetoshi Shimokawa 		sbp_cmd_status->status = ccb->csio.scsi_status;
708e9e688e2SHidetoshi Shimokawa 		sense = &ccb->csio.sense_data;
709e9e688e2SHidetoshi Shimokawa 
7109950b741SHidetoshi Shimokawa #if 0		/* XXX What we should do? */
7119950b741SHidetoshi Shimokawa #if 0
7129950b741SHidetoshi Shimokawa 		sbp_targ_abort(orbi->sc, STAILQ_NEXT(orbi, link));
7139950b741SHidetoshi Shimokawa #else
7149950b741SHidetoshi Shimokawa 		norbi = STAILQ_NEXT(orbi, link);
7159950b741SHidetoshi Shimokawa 		while (norbi) {
7169950b741SHidetoshi Shimokawa 			printf("%s: status=%d\n", __func__, norbi->state);
7179950b741SHidetoshi Shimokawa 			if (norbi->ccb != NULL) {
7189950b741SHidetoshi Shimokawa 				norbi->ccb->ccb_h.status = CAM_REQ_ABORTED;
7199950b741SHidetoshi Shimokawa 				xpt_done(norbi->ccb);
7209950b741SHidetoshi Shimokawa 				norbi->ccb = NULL;
7219950b741SHidetoshi Shimokawa 			}
7229950b741SHidetoshi Shimokawa 			sbp_targ_remove_orb_info_locked(orbi->login, norbi);
7239950b741SHidetoshi Shimokawa 			norbi = STAILQ_NEXT(norbi, link);
7249950b741SHidetoshi Shimokawa 			free(norbi, M_SBP_TARG);
7259950b741SHidetoshi Shimokawa 		}
7269950b741SHidetoshi Shimokawa #endif
7279950b741SHidetoshi Shimokawa #endif
728e9e688e2SHidetoshi Shimokawa 
7291cc052e8SKenneth D. Merry 		sense_len = ccb->csio.sense_len - ccb->csio.sense_resid;
7301cc052e8SKenneth D. Merry 		scsi_extract_sense_len(sense, sense_len, &error_code,
7311cc052e8SKenneth D. Merry 		    &sense_key, &asc, &ascq, /*show_errors*/ 0);
7321cc052e8SKenneth D. Merry 
7331cc052e8SKenneth D. Merry 		switch (error_code) {
7341cc052e8SKenneth D. Merry 		case SSD_CURRENT_ERROR:
7351cc052e8SKenneth D. Merry 		case SSD_DESC_CURRENT_ERROR:
736e9e688e2SHidetoshi Shimokawa 			sbp_cmd_status->sfmt = SBP_SFMT_CURR;
7371cc052e8SKenneth D. Merry 			break;
7381cc052e8SKenneth D. Merry 		default:
739e9e688e2SHidetoshi Shimokawa 			sbp_cmd_status->sfmt = SBP_SFMT_DEFER;
7401cc052e8SKenneth D. Merry 			break;
7411cc052e8SKenneth D. Merry 		}
742e9e688e2SHidetoshi Shimokawa 
7431cc052e8SKenneth D. Merry 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &info,
7441cc052e8SKenneth D. Merry 					&sinfo) == 0) {
7451cc052e8SKenneth D. Merry 			uint32_t info_trunc;
7461cc052e8SKenneth D. Merry 			sbp_cmd_status->valid = 1;
7471cc052e8SKenneth D. Merry 			info_trunc = info;
748e9e688e2SHidetoshi Shimokawa 
7491cc052e8SKenneth D. Merry 			sbp_cmd_status->info = htobe32(info_trunc);
7501cc052e8SKenneth D. Merry 		} else {
7511cc052e8SKenneth D. Merry 			sbp_cmd_status->valid = 0;
7521cc052e8SKenneth D. Merry 		}
753e9e688e2SHidetoshi Shimokawa 
7541cc052e8SKenneth D. Merry 		sbp_cmd_status->s_key = sense_key;
7551cc052e8SKenneth D. Merry 
7561cc052e8SKenneth D. Merry 		if (scsi_get_stream_info(sense, sense_len, NULL,
7571cc052e8SKenneth D. Merry 					 &stream_bits) == 0) {
7581cc052e8SKenneth D. Merry 			sbp_cmd_status->mark =
7591cc052e8SKenneth D. Merry 			    (stream_bits & SSD_FILEMARK) ? 1 : 0;
7601cc052e8SKenneth D. Merry 			sbp_cmd_status->eom =
7611cc052e8SKenneth D. Merry 			    (stream_bits & SSD_EOM) ? 1 : 0;
7621cc052e8SKenneth D. Merry 			sbp_cmd_status->ill_len =
7631cc052e8SKenneth D. Merry 			    (stream_bits & SSD_ILI) ? 1 : 0;
7641cc052e8SKenneth D. Merry 		} else {
7651cc052e8SKenneth D. Merry 			sbp_cmd_status->mark = 0;
7661cc052e8SKenneth D. Merry 			sbp_cmd_status->eom = 0;
7671cc052e8SKenneth D. Merry 			sbp_cmd_status->ill_len = 0;
7681cc052e8SKenneth D. Merry 		}
7691cc052e8SKenneth D. Merry 
7701cc052e8SKenneth D. Merry 
771e9e688e2SHidetoshi Shimokawa 		/* add_sense_code(_qual), info, cmd_spec_info */
772e9e688e2SHidetoshi Shimokawa 		sbp_status->len = 4;
7731cc052e8SKenneth D. Merry 
7741cc052e8SKenneth D. Merry 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_COMMAND,
7751cc052e8SKenneth D. Merry 					&info, &sinfo) == 0) {
7761cc052e8SKenneth D. Merry 			uint32_t cmdspec_trunc;
7771cc052e8SKenneth D. Merry 
7781cc052e8SKenneth D. Merry 			cmdspec_trunc = info;
7791cc052e8SKenneth D. Merry 
7801cc052e8SKenneth D. Merry 			sbp_cmd_status->cdb = htobe32(cmdspec_trunc);
7811cc052e8SKenneth D. Merry 		}
7821cc052e8SKenneth D. Merry 
7831cc052e8SKenneth D. Merry 		sbp_cmd_status->s_code = asc;
7841cc052e8SKenneth D. Merry 		sbp_cmd_status->s_qlfr = ascq;
7851cc052e8SKenneth D. Merry 
7861cc052e8SKenneth D. Merry 		if (scsi_get_sense_info(sense, sense_len, SSD_DESC_FRU, &info,
7871cc052e8SKenneth D. Merry 					&sinfo) == 0) {
7881cc052e8SKenneth D. Merry 			sbp_cmd_status->fru = (uint8_t)info;
789e9e688e2SHidetoshi Shimokawa 			sbp_status->len = 5;
7901cc052e8SKenneth D. Merry 		} else {
7911cc052e8SKenneth D. Merry 			sbp_cmd_status->fru = 0;
7921cc052e8SKenneth D. Merry 		}
793e9e688e2SHidetoshi Shimokawa 
7941cc052e8SKenneth D. Merry 		if (scsi_get_sks(sense, sense_len, sks) == 0) {
7951cc052e8SKenneth D. Merry 			bcopy(sks, &sbp_cmd_status->s_keydep[0], sizeof(sks));
7961cc052e8SKenneth D. Merry 			sbp_status->len = 5;
7974636d244SSean Bruno 			ccb->ccb_h.status |= CAM_SENT_SENSE;
7981cc052e8SKenneth D. Merry 		}
799e9e688e2SHidetoshi Shimokawa 
800e9e688e2SHidetoshi Shimokawa 		break;
801e9e688e2SHidetoshi Shimokawa 	}
802e9e688e2SHidetoshi Shimokawa 	default:
80310d3ed64SHidetoshi Shimokawa 		printf("%s: unknown scsi status 0x%x\n", __func__,
804e9e688e2SHidetoshi Shimokawa 		    sbp_status->status);
805e9e688e2SHidetoshi Shimokawa 	}
806e9e688e2SHidetoshi Shimokawa 
8079950b741SHidetoshi Shimokawa 
8089950b741SHidetoshi Shimokawa 	sbp_targ_status_FIFO(orbi,
8099950b741SHidetoshi Shimokawa 	    orbi->login->fifo_hi, orbi->login->fifo_lo, /*dequeue*/1);
810e9e688e2SHidetoshi Shimokawa }
811e9e688e2SHidetoshi Shimokawa 
8124636d244SSean Bruno /*
8134636d244SSean Bruno  * Invoked as a callback handler from fwmem_read/write_block
8144636d244SSean Bruno  *
8154636d244SSean Bruno  * Process read/write of initiator address space
8164636d244SSean Bruno  * completion and pass status onto the backend target.
8174636d244SSean Bruno  * If this is a partial read/write for a CCB then
8184636d244SSean Bruno  * we decrement the orbi's refcount to indicate
8194636d244SSean Bruno  * the status of the read/write is complete
8204636d244SSean Bruno  */
821e9e688e2SHidetoshi Shimokawa static void
822e9e688e2SHidetoshi Shimokawa sbp_targ_cam_done(struct fw_xfer *xfer)
823e9e688e2SHidetoshi Shimokawa {
824e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
825e9e688e2SHidetoshi Shimokawa 	union ccb *ccb;
826e9e688e2SHidetoshi Shimokawa 
827e9e688e2SHidetoshi Shimokawa 	orbi = (struct orb_info *)xfer->sc;
828e9e688e2SHidetoshi Shimokawa 
8294636d244SSean Bruno 	if (debug)
83010d3ed64SHidetoshi Shimokawa 		printf("%s: resp=%d refcount=%d\n", __func__,
831e9e688e2SHidetoshi Shimokawa 			xfer->resp, orbi->refcount);
832e9e688e2SHidetoshi Shimokawa 
833e9e688e2SHidetoshi Shimokawa 	if (xfer->resp != 0) {
83410d3ed64SHidetoshi Shimokawa 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
835e9e688e2SHidetoshi Shimokawa 		orbi->status.resp = SBP_TRANS_FAIL;
8361398a889SHidetoshi Shimokawa 		orbi->status.status = OBJ_DATA | SBE_TIMEOUT/*XXX*/;
837e9e688e2SHidetoshi Shimokawa 		orbi->status.dead = 1;
8389950b741SHidetoshi Shimokawa 		sbp_targ_abort(orbi->sc, STAILQ_NEXT(orbi, link));
839e9e688e2SHidetoshi Shimokawa 	}
840e9e688e2SHidetoshi Shimokawa 
841e9e688e2SHidetoshi Shimokawa 	orbi->refcount--;
842e9e688e2SHidetoshi Shimokawa 
843e9e688e2SHidetoshi Shimokawa 	ccb = orbi->ccb;
844e9e688e2SHidetoshi Shimokawa 	if (orbi->refcount == 0) {
8459950b741SHidetoshi Shimokawa 		orbi->ccb = NULL;
846e9e688e2SHidetoshi Shimokawa 		if (orbi->state == ORBI_STATUS_ABORTED) {
847e9e688e2SHidetoshi Shimokawa 			if (debug)
84810d3ed64SHidetoshi Shimokawa 				printf("%s: orbi aborted\n", __func__);
849a73ff510SHidetoshi Shimokawa 			sbp_targ_remove_orb_info(orbi->login, orbi);
8504636d244SSean Bruno 			if (orbi->page_table != NULL) {
8514636d244SSean Bruno 				if (debug)
8524636d244SSean Bruno 					printf("%s: free orbi->page_table %p\n",
8534636d244SSean Bruno 						__func__, orbi->page_table);
854e9e688e2SHidetoshi Shimokawa 				free(orbi->page_table, M_SBP_TARG);
8554636d244SSean Bruno 			}
8564636d244SSean Bruno 			if (debug)
8574636d244SSean Bruno 				printf("%s: free orbi %p\n", __func__, orbi);
858e9e688e2SHidetoshi Shimokawa 			free(orbi, M_SBP_TARG);
8594636d244SSean Bruno 			orbi = NULL;
8604636d244SSean Bruno 		} else if (orbi->status.resp == ORBI_STATUS_NONE) {
8614636d244SSean Bruno 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
8624636d244SSean Bruno 				if (debug)
8634636d244SSean Bruno 					printf("%s: CAM_SEND_STATUS set %0x\n", __func__, ccb->ccb_h.flags);
864e9e688e2SHidetoshi Shimokawa 				sbp_targ_send_status(orbi, ccb);
8654636d244SSean Bruno 			} else {
8664636d244SSean Bruno 				if (debug)
8674636d244SSean Bruno 					printf("%s: CAM_SEND_STATUS not set %0x\n", __func__, ccb->ccb_h.flags);
868e9e688e2SHidetoshi Shimokawa 				ccb->ccb_h.status = CAM_REQ_CMP;
8694636d244SSean Bruno 			}
870e9e688e2SHidetoshi Shimokawa 			xpt_done(ccb);
871e9e688e2SHidetoshi Shimokawa 		} else {
872e9e688e2SHidetoshi Shimokawa 			orbi->status.len = 1;
873e9e688e2SHidetoshi Shimokawa 			sbp_targ_status_FIFO(orbi,
874a73ff510SHidetoshi Shimokawa 		    	    orbi->login->fifo_hi, orbi->login->fifo_lo,
875e9e688e2SHidetoshi Shimokawa 			    /*dequeue*/1);
876e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_ABORTED;
877e9e688e2SHidetoshi Shimokawa 			xpt_done(ccb);
878e9e688e2SHidetoshi Shimokawa 		}
879e9e688e2SHidetoshi Shimokawa 	}
880e9e688e2SHidetoshi Shimokawa 
881e9e688e2SHidetoshi Shimokawa 	fw_xfer_free(xfer);
882e9e688e2SHidetoshi Shimokawa }
883e9e688e2SHidetoshi Shimokawa 
884e9e688e2SHidetoshi Shimokawa static cam_status
885e9e688e2SHidetoshi Shimokawa sbp_targ_abort_ccb(struct sbp_targ_softc *sc, union ccb *ccb)
886e9e688e2SHidetoshi Shimokawa {
887e9e688e2SHidetoshi Shimokawa 	union ccb *accb;
888e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
889e9e688e2SHidetoshi Shimokawa 	struct ccb_hdr_slist *list;
890e9e688e2SHidetoshi Shimokawa 	struct ccb_hdr *curelm;
891e9e688e2SHidetoshi Shimokawa 	int found;
892e9e688e2SHidetoshi Shimokawa 	cam_status status;
893e9e688e2SHidetoshi Shimokawa 
894e9e688e2SHidetoshi Shimokawa 	status = sbp_targ_find_devs(sc, ccb, &lstate, 0);
895e9e688e2SHidetoshi Shimokawa 	if (status != CAM_REQ_CMP)
896e9e688e2SHidetoshi Shimokawa 		return (status);
897e9e688e2SHidetoshi Shimokawa 
898e9e688e2SHidetoshi Shimokawa 	accb = ccb->cab.abort_ccb;
899e9e688e2SHidetoshi Shimokawa 
900e9e688e2SHidetoshi Shimokawa 	if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
901e9e688e2SHidetoshi Shimokawa 		list = &lstate->accept_tios;
902b79dc8a8SKenneth D. Merry 	else if (accb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
903e9e688e2SHidetoshi Shimokawa 		list = &lstate->immed_notifies;
904e9e688e2SHidetoshi Shimokawa 	else
905e9e688e2SHidetoshi Shimokawa 		return (CAM_UA_ABORT);
906e9e688e2SHidetoshi Shimokawa 
907e9e688e2SHidetoshi Shimokawa 	curelm = SLIST_FIRST(list);
908e9e688e2SHidetoshi Shimokawa 	found = 0;
909e9e688e2SHidetoshi Shimokawa 	if (curelm == &accb->ccb_h) {
910e9e688e2SHidetoshi Shimokawa 		found = 1;
911e9e688e2SHidetoshi Shimokawa 		SLIST_REMOVE_HEAD(list, sim_links.sle);
912e9e688e2SHidetoshi Shimokawa 	} else {
913e9e688e2SHidetoshi Shimokawa 		while (curelm != NULL) {
914e9e688e2SHidetoshi Shimokawa 			struct ccb_hdr *nextelm;
915e9e688e2SHidetoshi Shimokawa 
916e9e688e2SHidetoshi Shimokawa 			nextelm = SLIST_NEXT(curelm, sim_links.sle);
917e9e688e2SHidetoshi Shimokawa 			if (nextelm == &accb->ccb_h) {
918e9e688e2SHidetoshi Shimokawa 				found = 1;
919e9e688e2SHidetoshi Shimokawa 				SLIST_NEXT(curelm, sim_links.sle) =
920e9e688e2SHidetoshi Shimokawa 				    SLIST_NEXT(nextelm, sim_links.sle);
921e9e688e2SHidetoshi Shimokawa 				break;
922e9e688e2SHidetoshi Shimokawa 			}
923e9e688e2SHidetoshi Shimokawa 			curelm = nextelm;
924e9e688e2SHidetoshi Shimokawa 		}
925e9e688e2SHidetoshi Shimokawa 	}
926e9e688e2SHidetoshi Shimokawa 	if (found) {
927e9e688e2SHidetoshi Shimokawa 		accb->ccb_h.status = CAM_REQ_ABORTED;
928e9e688e2SHidetoshi Shimokawa 		xpt_done(accb);
929e9e688e2SHidetoshi Shimokawa 		return (CAM_REQ_CMP);
930e9e688e2SHidetoshi Shimokawa 	}
93110d3ed64SHidetoshi Shimokawa 	printf("%s: not found\n", __func__);
932e9e688e2SHidetoshi Shimokawa 	return (CAM_PATH_INVALID);
933e9e688e2SHidetoshi Shimokawa }
934e9e688e2SHidetoshi Shimokawa 
9354636d244SSean Bruno /*
9364636d244SSean Bruno  * directly execute a read or write to the initiator
9374636d244SSean Bruno  * address space and set hand(sbp_targ_cam_done) to
9384636d244SSean Bruno  * process the completion from the SIM to the target.
9394636d244SSean Bruno  * set orbi->refcount to inidicate that a read/write
9404636d244SSean Bruno  * is inflight to/from the initiator.
9414636d244SSean Bruno  */
942e9e688e2SHidetoshi Shimokawa static void
943e9e688e2SHidetoshi Shimokawa sbp_targ_xfer_buf(struct orb_info *orbi, u_int offset,
94403161bbcSDoug Rabson     uint16_t dst_hi, uint32_t dst_lo, u_int size,
945e9e688e2SHidetoshi Shimokawa     void (*hand)(struct fw_xfer *))
946e9e688e2SHidetoshi Shimokawa {
947e9e688e2SHidetoshi Shimokawa 	struct fw_xfer *xfer;
948e9e688e2SHidetoshi Shimokawa 	u_int len, ccb_dir, off = 0;
949e9e688e2SHidetoshi Shimokawa 	char *ptr;
950e9e688e2SHidetoshi Shimokawa 
951a73ff510SHidetoshi Shimokawa 	if (debug > 1)
95210d3ed64SHidetoshi Shimokawa 		printf("%s: offset=%d size=%d\n", __func__, offset, size);
953e9e688e2SHidetoshi Shimokawa 	ccb_dir = orbi->ccb->ccb_h.flags & CAM_DIR_MASK;
954e9e688e2SHidetoshi Shimokawa 	ptr = (char *)orbi->ccb->csio.data_ptr + offset;
955e9e688e2SHidetoshi Shimokawa 
956e9e688e2SHidetoshi Shimokawa 	while (size > 0) {
957e9e688e2SHidetoshi Shimokawa 		/* XXX assume dst_lo + off doesn't overflow */
958e9e688e2SHidetoshi Shimokawa 		len = MIN(size, 2048 /* XXX */);
959e9e688e2SHidetoshi Shimokawa 		size -= len;
960e9e688e2SHidetoshi Shimokawa 		orbi->refcount ++;
9614636d244SSean Bruno 		if (ccb_dir == CAM_DIR_OUT) {
9624636d244SSean Bruno 			if (debug)
9634636d244SSean Bruno 				printf("%s: CAM_DIR_OUT --> read block in?\n",__func__);
964e9e688e2SHidetoshi Shimokawa 			xfer = fwmem_read_block(orbi->fwdev,
9654636d244SSean Bruno 			   (void *)orbi, /*spd*/FWSPD_S400,
966e9e688e2SHidetoshi Shimokawa 			    dst_hi, dst_lo + off, len,
967e9e688e2SHidetoshi Shimokawa 			    ptr + off, hand);
9684636d244SSean Bruno 		} else {
9694636d244SSean Bruno 			if (debug)
9704636d244SSean Bruno 				printf("%s: CAM_DIR_IN --> write block out?\n",__func__);
971e9e688e2SHidetoshi Shimokawa 			xfer = fwmem_write_block(orbi->fwdev,
9724636d244SSean Bruno 			   (void *)orbi, /*spd*/FWSPD_S400,
973e9e688e2SHidetoshi Shimokawa 			    dst_hi, dst_lo + off, len,
974e9e688e2SHidetoshi Shimokawa 			    ptr + off, hand);
9754636d244SSean Bruno 		}
976e9e688e2SHidetoshi Shimokawa 		if (xfer == NULL) {
97710d3ed64SHidetoshi Shimokawa 			printf("%s: xfer == NULL", __func__);
978e9e688e2SHidetoshi Shimokawa 			/* XXX what should we do?? */
979e9e688e2SHidetoshi Shimokawa 			orbi->refcount--;
980e9e688e2SHidetoshi Shimokawa 		}
981e9e688e2SHidetoshi Shimokawa 		off += len;
982e9e688e2SHidetoshi Shimokawa 	}
983e9e688e2SHidetoshi Shimokawa }
984e9e688e2SHidetoshi Shimokawa 
985e9e688e2SHidetoshi Shimokawa static void
986e9e688e2SHidetoshi Shimokawa sbp_targ_pt_done(struct fw_xfer *xfer)
987e9e688e2SHidetoshi Shimokawa {
988e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
9894636d244SSean Bruno 	struct unrestricted_page_table_fmt *pt;
9904636d244SSean Bruno 	uint32_t i;
991e9e688e2SHidetoshi Shimokawa 
992e9e688e2SHidetoshi Shimokawa 	orbi = (struct orb_info *)xfer->sc;
9934636d244SSean Bruno 
994e9e688e2SHidetoshi Shimokawa 	if (orbi->state == ORBI_STATUS_ABORTED) {
995e9e688e2SHidetoshi Shimokawa 		if (debug)
99610d3ed64SHidetoshi Shimokawa 			printf("%s: orbi aborted\n", __func__);
997a73ff510SHidetoshi Shimokawa 		sbp_targ_remove_orb_info(orbi->login, orbi);
9984636d244SSean Bruno 		if (debug) {
9994636d244SSean Bruno 			printf("%s: free orbi->page_table %p\n", __func__, orbi->page_table);
10004636d244SSean Bruno 			printf("%s: free orbi %p\n", __func__, orbi);
10014636d244SSean Bruno 		}
1002e9e688e2SHidetoshi Shimokawa 		free(orbi->page_table, M_SBP_TARG);
1003e9e688e2SHidetoshi Shimokawa 		free(orbi, M_SBP_TARG);
10044636d244SSean Bruno 		orbi = NULL;
1005e9e688e2SHidetoshi Shimokawa 		fw_xfer_free(xfer);
1006e9e688e2SHidetoshi Shimokawa 		return;
1007e9e688e2SHidetoshi Shimokawa 	}
1008e9e688e2SHidetoshi Shimokawa 	if (xfer->resp != 0) {
100910d3ed64SHidetoshi Shimokawa 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1010e9e688e2SHidetoshi Shimokawa 		orbi->status.resp = SBP_TRANS_FAIL;
10111398a889SHidetoshi Shimokawa 		orbi->status.status = OBJ_PT | SBE_TIMEOUT/*XXX*/;
1012e9e688e2SHidetoshi Shimokawa 		orbi->status.dead = 1;
1013e9e688e2SHidetoshi Shimokawa 		orbi->status.len = 1;
10149950b741SHidetoshi Shimokawa 		sbp_targ_abort(orbi->sc, STAILQ_NEXT(orbi, link));
1015e9e688e2SHidetoshi Shimokawa 
10164636d244SSean Bruno 		if (debug)
10174636d244SSean Bruno 			printf("%s: free orbi->page_table %p\n", __func__, orbi->page_table);
10184636d244SSean Bruno 
1019e9e688e2SHidetoshi Shimokawa 		sbp_targ_status_FIFO(orbi,
1020a73ff510SHidetoshi Shimokawa 		    orbi->login->fifo_hi, orbi->login->fifo_lo, /*dequeue*/1);
1021e9e688e2SHidetoshi Shimokawa 		free(orbi->page_table, M_SBP_TARG);
10224636d244SSean Bruno 		orbi->page_table = NULL;
1023e9e688e2SHidetoshi Shimokawa 		fw_xfer_free(xfer);
1024e9e688e2SHidetoshi Shimokawa 		return;
1025e9e688e2SHidetoshi Shimokawa 	}
1026e9e688e2SHidetoshi Shimokawa 	orbi->refcount++;
10274636d244SSean Bruno /*
1028453130d9SPedro F. Giffuni  * Set endianness here so we don't have
10294636d244SSean Bruno  * to deal with is later
10304636d244SSean Bruno  */
10314636d244SSean Bruno 	for (i = 0, pt = orbi->page_table; i < orbi->orb4.data_size; i++, pt++) {
10324636d244SSean Bruno 		pt->segment_len = ntohs(pt->segment_len);
10334636d244SSean Bruno 		if (debug)
10344636d244SSean Bruno 			printf("%s:segment_len = %u\n", __func__,pt->segment_len);
10354636d244SSean Bruno 		pt->segment_base_high = ntohs(pt->segment_base_high);
10364636d244SSean Bruno 		pt->segment_base_low = ntohl(pt->segment_base_low);
1037e9e688e2SHidetoshi Shimokawa 	}
10384636d244SSean Bruno 
10394636d244SSean Bruno 	sbp_targ_xfer_pt(orbi);
10404636d244SSean Bruno 
1041e9e688e2SHidetoshi Shimokawa 	orbi->refcount--;
1042e9e688e2SHidetoshi Shimokawa 	if (orbi->refcount == 0)
104310d3ed64SHidetoshi Shimokawa 		printf("%s: refcount == 0\n", __func__);
1044e9e688e2SHidetoshi Shimokawa 
1045e9e688e2SHidetoshi Shimokawa 	fw_xfer_free(xfer);
1046e9e688e2SHidetoshi Shimokawa 	return;
1047e9e688e2SHidetoshi Shimokawa }
1048e9e688e2SHidetoshi Shimokawa 
10494636d244SSean Bruno static void sbp_targ_xfer_pt(struct orb_info *orbi)
10504636d244SSean Bruno {
10514636d244SSean Bruno 	union ccb *ccb;
10524636d244SSean Bruno 	uint32_t res, offset, len;
10534636d244SSean Bruno 
10544636d244SSean Bruno 	ccb = orbi->ccb;
10554636d244SSean Bruno 	if (debug)
10564636d244SSean Bruno 		printf("%s: dxfer_len=%d\n", __func__, ccb->csio.dxfer_len);
10574636d244SSean Bruno 	res = ccb->csio.dxfer_len;
10584636d244SSean Bruno 	/*
10594636d244SSean Bruno 	 * If the page table required multiple CTIO's to
10604636d244SSean Bruno 	 * complete, then cur_pte is non NULL
10614636d244SSean Bruno 	 * and we need to start from the last position
10624636d244SSean Bruno 	 * If this is the first pass over a page table
10634636d244SSean Bruno 	 * then we just start at the beginning of the page
10644636d244SSean Bruno 	 * table.
10654636d244SSean Bruno 	 *
10664636d244SSean Bruno 	 * Parse the unrestricted page table and figure out where we need
10674636d244SSean Bruno 	 * to shove the data from this read request.
10684636d244SSean Bruno 	 */
10694636d244SSean Bruno 	for (offset = 0, len = 0; (res != 0) && (orbi->cur_pte < orbi->last_pte); offset += len) {
10704636d244SSean Bruno 		len = MIN(orbi->cur_pte->segment_len, res);
10714636d244SSean Bruno 		res -= len;
10724636d244SSean Bruno 		if (debug)
10734636d244SSean Bruno 			printf("%s:page_table: %04x:%08x segment_len(%u) res(%u) len(%u)\n",
10744636d244SSean Bruno 				__func__, orbi->cur_pte->segment_base_high,
10754636d244SSean Bruno 				orbi->cur_pte->segment_base_low,
10764636d244SSean Bruno 				orbi->cur_pte->segment_len,
10774636d244SSean Bruno 				res, len);
10784636d244SSean Bruno 		sbp_targ_xfer_buf(orbi, offset,
10794636d244SSean Bruno 				orbi->cur_pte->segment_base_high,
10804636d244SSean Bruno 				orbi->cur_pte->segment_base_low,
10814636d244SSean Bruno 				len, sbp_targ_cam_done);
10824636d244SSean Bruno 		/*
10834636d244SSean Bruno 		 * If we have only written partially to
10844636d244SSean Bruno 		 * this page table, then we need to save
10854636d244SSean Bruno 		 * our position for the next CTIO.  If we
10864636d244SSean Bruno 		 * have completed the page table, then we
10874636d244SSean Bruno 		 * are safe to move on to the next entry.
10884636d244SSean Bruno 		 */
10894636d244SSean Bruno 		if (len == orbi->cur_pte->segment_len) {
10904636d244SSean Bruno 			orbi->cur_pte++;
10914636d244SSean Bruno 		} else {
10924636d244SSean Bruno 			uint32_t saved_base_low;
10934636d244SSean Bruno 
10944636d244SSean Bruno 			/* Handle transfers that cross a 4GB boundary. */
10954636d244SSean Bruno 			saved_base_low = orbi->cur_pte->segment_base_low;
10964636d244SSean Bruno 			orbi->cur_pte->segment_base_low += len;
10974636d244SSean Bruno 			if (orbi->cur_pte->segment_base_low < saved_base_low)
10984636d244SSean Bruno 				orbi->cur_pte->segment_base_high++;
10994636d244SSean Bruno 
11004636d244SSean Bruno 			orbi->cur_pte->segment_len -= len;
11014636d244SSean Bruno 		}
11024636d244SSean Bruno 	}
11034636d244SSean Bruno 	if (debug) {
11044636d244SSean Bruno 		printf("%s: base_low(%08x) page_table_off(%p) last_block(%u)\n",
11054636d244SSean Bruno 			__func__, orbi->cur_pte->segment_base_low,
11064636d244SSean Bruno 			orbi->cur_pte, orbi->last_block_read);
11074636d244SSean Bruno 	}
11084636d244SSean Bruno 	if (res != 0)
11094636d244SSean Bruno 		printf("Warning - short pt encountered.  "
11104636d244SSean Bruno 			"Could not transfer all data.\n");
11114636d244SSean Bruno 	return;
11124636d244SSean Bruno }
11134636d244SSean Bruno 
11144636d244SSean Bruno /*
11154636d244SSean Bruno  * Create page table in local memory
11164636d244SSean Bruno  * and transfer it from the initiator
11174636d244SSean Bruno  * in order to know where we are supposed
11184636d244SSean Bruno  * to put the data.
11194636d244SSean Bruno  */
11204636d244SSean Bruno 
1121e9e688e2SHidetoshi Shimokawa static void
1122e9e688e2SHidetoshi Shimokawa sbp_targ_fetch_pt(struct orb_info *orbi)
1123e9e688e2SHidetoshi Shimokawa {
1124e9e688e2SHidetoshi Shimokawa 	struct fw_xfer *xfer;
1125e9e688e2SHidetoshi Shimokawa 
11264636d244SSean Bruno 	/*
11274636d244SSean Bruno 	 * Pull in page table from initiator
11284636d244SSean Bruno 	 * and setup for data from our
11294636d244SSean Bruno 	 * backend device.
11304636d244SSean Bruno 	 */
11314636d244SSean Bruno 	if (orbi->page_table == NULL) {
11324636d244SSean Bruno 		orbi->page_table = malloc(orbi->orb4.data_size*
11334636d244SSean Bruno 					  sizeof(struct unrestricted_page_table_fmt),
11344636d244SSean Bruno 					  M_SBP_TARG, M_NOWAIT|M_ZERO);
1135e9e688e2SHidetoshi Shimokawa 		if (orbi->page_table == NULL)
1136e9e688e2SHidetoshi Shimokawa 			goto error;
11374636d244SSean Bruno 		orbi->cur_pte = orbi->page_table;
11384636d244SSean Bruno 		orbi->last_pte = orbi->page_table + orbi->orb4.data_size;
11394636d244SSean Bruno 		orbi->last_block_read = orbi->orb4.data_size;
11404636d244SSean Bruno 		if (debug && orbi->page_table != NULL)
11414636d244SSean Bruno 			printf("%s: malloc'd orbi->page_table(%p), orb4.data_size(%u)\n",
11424636d244SSean Bruno  				__func__, orbi->page_table, orbi->orb4.data_size);
11434636d244SSean Bruno 
11444636d244SSean Bruno 		xfer = fwmem_read_block(orbi->fwdev, (void *)orbi, /*spd*/FWSPD_S400,
11454636d244SSean Bruno 					orbi->data_hi, orbi->data_lo, orbi->orb4.data_size*
11464636d244SSean Bruno 					sizeof(struct unrestricted_page_table_fmt),
1147e9e688e2SHidetoshi Shimokawa 					(void *)orbi->page_table, sbp_targ_pt_done);
11484636d244SSean Bruno 
1149e9e688e2SHidetoshi Shimokawa 		if (xfer != NULL)
1150e9e688e2SHidetoshi Shimokawa 			return;
11514636d244SSean Bruno 	} else {
11524636d244SSean Bruno 		/*
11534636d244SSean Bruno 		 * This is a CTIO for a page table we have
11544636d244SSean Bruno 		 * already malloc'd, so just directly invoke
11554636d244SSean Bruno 		 * the xfer function on the orbi.
11564636d244SSean Bruno 		 */
11574636d244SSean Bruno 		sbp_targ_xfer_pt(orbi);
11584636d244SSean Bruno 		return;
11594636d244SSean Bruno 	}
1160e9e688e2SHidetoshi Shimokawa error:
1161e9e688e2SHidetoshi Shimokawa 	orbi->ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
11624636d244SSean Bruno 	if (debug)
11634636d244SSean Bruno 		printf("%s: free orbi->page_table %p due to xfer == NULL\n", __func__, orbi->page_table);
11644636d244SSean Bruno 	if (orbi->page_table != NULL) {
11654636d244SSean Bruno 		free(orbi->page_table, M_SBP_TARG);
11664636d244SSean Bruno 		orbi->page_table = NULL;
11674636d244SSean Bruno 	}
1168e9e688e2SHidetoshi Shimokawa 	xpt_done(orbi->ccb);
1169e9e688e2SHidetoshi Shimokawa 	return;
1170e9e688e2SHidetoshi Shimokawa }
1171e9e688e2SHidetoshi Shimokawa 
1172e9e688e2SHidetoshi Shimokawa static void
1173e9e688e2SHidetoshi Shimokawa sbp_targ_action1(struct cam_sim *sim, union ccb *ccb)
1174e9e688e2SHidetoshi Shimokawa {
1175e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
1176e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
1177e9e688e2SHidetoshi Shimokawa 	cam_status status;
1178e9e688e2SHidetoshi Shimokawa 	u_int ccb_dir;
1179e9e688e2SHidetoshi Shimokawa 
1180e9e688e2SHidetoshi Shimokawa 	sc =  (struct sbp_targ_softc *)cam_sim_softc(sim);
1181e9e688e2SHidetoshi Shimokawa 
1182e9e688e2SHidetoshi Shimokawa 	status = sbp_targ_find_devs(sc, ccb, &lstate, TRUE);
1183e9e688e2SHidetoshi Shimokawa 
1184e9e688e2SHidetoshi Shimokawa 	switch (ccb->ccb_h.func_code) {
1185e9e688e2SHidetoshi Shimokawa 	case XPT_CONT_TARGET_IO:
1186e9e688e2SHidetoshi Shimokawa 	{
1187e9e688e2SHidetoshi Shimokawa 		struct orb_info *orbi;
1188e9e688e2SHidetoshi Shimokawa 
1189e9e688e2SHidetoshi Shimokawa 		if (debug)
11909950b741SHidetoshi Shimokawa 			printf("%s: XPT_CONT_TARGET_IO (0x%08x)\n",
11919950b741SHidetoshi Shimokawa 					 __func__, ccb->csio.tag_id);
1192e9e688e2SHidetoshi Shimokawa 
1193e9e688e2SHidetoshi Shimokawa 		if (status != CAM_REQ_CMP) {
1194e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = status;
1195e9e688e2SHidetoshi Shimokawa 			xpt_done(ccb);
1196e9e688e2SHidetoshi Shimokawa 			break;
1197e9e688e2SHidetoshi Shimokawa 		}
1198e9e688e2SHidetoshi Shimokawa 		/* XXX transfer from/to initiator */
1199e9e688e2SHidetoshi Shimokawa 		orbi = sbp_targ_get_orb_info(lstate,
1200e9e688e2SHidetoshi Shimokawa 		    ccb->csio.tag_id, ccb->csio.init_id);
1201e9e688e2SHidetoshi Shimokawa 		if (orbi == NULL) {
1202e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_ABORTED; /* XXX */
1203e9e688e2SHidetoshi Shimokawa 			xpt_done(ccb);
1204e9e688e2SHidetoshi Shimokawa 			break;
1205e9e688e2SHidetoshi Shimokawa 		}
1206e9e688e2SHidetoshi Shimokawa 		if (orbi->state == ORBI_STATUS_ABORTED) {
1207e9e688e2SHidetoshi Shimokawa 			if (debug)
120810d3ed64SHidetoshi Shimokawa 				printf("%s: ctio aborted\n", __func__);
12099950b741SHidetoshi Shimokawa 			sbp_targ_remove_orb_info_locked(orbi->login, orbi);
12104636d244SSean Bruno 			if (debug)
12114636d244SSean Bruno 				printf("%s: free orbi %p\n", __func__, orbi);
1212e9e688e2SHidetoshi Shimokawa 			free(orbi, M_SBP_TARG);
12139950b741SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_ABORTED;
12149950b741SHidetoshi Shimokawa 			xpt_done(ccb);
1215e9e688e2SHidetoshi Shimokawa 			break;
1216e9e688e2SHidetoshi Shimokawa 		}
1217e9e688e2SHidetoshi Shimokawa 		orbi->state = ORBI_STATUS_CTIO;
1218e9e688e2SHidetoshi Shimokawa 
1219e9e688e2SHidetoshi Shimokawa 		orbi->ccb = ccb;
1220e9e688e2SHidetoshi Shimokawa 		ccb_dir = ccb->ccb_h.flags & CAM_DIR_MASK;
1221e9e688e2SHidetoshi Shimokawa 
1222e9e688e2SHidetoshi Shimokawa 		/* XXX */
1223e9e688e2SHidetoshi Shimokawa 		if (ccb->csio.dxfer_len == 0)
1224e9e688e2SHidetoshi Shimokawa 			ccb_dir = CAM_DIR_NONE;
1225e9e688e2SHidetoshi Shimokawa 
1226e9e688e2SHidetoshi Shimokawa 		/* Sanity check */
1227e9e688e2SHidetoshi Shimokawa 		if (ccb_dir == CAM_DIR_IN && orbi->orb4.dir == 0)
122810d3ed64SHidetoshi Shimokawa 			printf("%s: direction mismatch\n", __func__);
1229e9e688e2SHidetoshi Shimokawa 
1230e9e688e2SHidetoshi Shimokawa 		/* check page table */
1231e9e688e2SHidetoshi Shimokawa 		if (ccb_dir != CAM_DIR_NONE && orbi->orb4.page_table_present) {
1232e9e688e2SHidetoshi Shimokawa 			if (debug)
1233e9e688e2SHidetoshi Shimokawa 				printf("%s: page_table_present\n",
123410d3ed64SHidetoshi Shimokawa 				    __func__);
1235e9e688e2SHidetoshi Shimokawa 			if (orbi->orb4.page_size != 0) {
1236e9e688e2SHidetoshi Shimokawa 				printf("%s: unsupported pagesize %d != 0\n",
123710d3ed64SHidetoshi Shimokawa 			 	    __func__, orbi->orb4.page_size);
1238e9e688e2SHidetoshi Shimokawa 				ccb->ccb_h.status = CAM_REQ_INVALID;
1239e9e688e2SHidetoshi Shimokawa 				xpt_done(ccb);
1240e9e688e2SHidetoshi Shimokawa 				break;
1241e9e688e2SHidetoshi Shimokawa 			}
1242e9e688e2SHidetoshi Shimokawa 			sbp_targ_fetch_pt(orbi);
1243e9e688e2SHidetoshi Shimokawa 			break;
1244e9e688e2SHidetoshi Shimokawa 		}
1245e9e688e2SHidetoshi Shimokawa 
1246e9e688e2SHidetoshi Shimokawa 		/* Sanity check */
12474636d244SSean Bruno 		if (ccb_dir != CAM_DIR_NONE) {
1248e9e688e2SHidetoshi Shimokawa 			sbp_targ_xfer_buf(orbi, 0, orbi->data_hi,
1249e9e688e2SHidetoshi Shimokawa 			    orbi->data_lo,
1250e9e688e2SHidetoshi Shimokawa 			    MIN(orbi->orb4.data_size, ccb->csio.dxfer_len),
1251e9e688e2SHidetoshi Shimokawa 			    sbp_targ_cam_done);
12524636d244SSean Bruno 			if ( orbi->orb4.data_size > ccb->csio.dxfer_len ) {
12534636d244SSean Bruno 				orbi->data_lo += ccb->csio.dxfer_len;
12544636d244SSean Bruno 				orbi->orb4.data_size -= ccb->csio.dxfer_len;
12554636d244SSean Bruno 			}
12564636d244SSean Bruno 		}
1257e9e688e2SHidetoshi Shimokawa 
1258e9e688e2SHidetoshi Shimokawa 		if (ccb_dir == CAM_DIR_NONE) {
12599950b741SHidetoshi Shimokawa 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
12609950b741SHidetoshi Shimokawa 				/* XXX */
12619950b741SHidetoshi Shimokawa 				SBP_UNLOCK(sc);
1262e9e688e2SHidetoshi Shimokawa 				sbp_targ_send_status(orbi, ccb);
12639950b741SHidetoshi Shimokawa 				SBP_LOCK(sc);
12649950b741SHidetoshi Shimokawa 			}
1265e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_CMP;
1266e9e688e2SHidetoshi Shimokawa 			xpt_done(ccb);
1267e9e688e2SHidetoshi Shimokawa 		}
1268e9e688e2SHidetoshi Shimokawa 		break;
1269e9e688e2SHidetoshi Shimokawa 	}
1270e9e688e2SHidetoshi Shimokawa 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
1271e9e688e2SHidetoshi Shimokawa 		if (status != CAM_REQ_CMP) {
1272e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = status;
1273e9e688e2SHidetoshi Shimokawa 			xpt_done(ccb);
1274e9e688e2SHidetoshi Shimokawa 			break;
1275e9e688e2SHidetoshi Shimokawa 		}
1276e9e688e2SHidetoshi Shimokawa 		SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
1277e9e688e2SHidetoshi Shimokawa 		    sim_links.sle);
1278e9e688e2SHidetoshi Shimokawa 		ccb->ccb_h.status = CAM_REQ_INPROG;
1279a73ff510SHidetoshi Shimokawa 		if ((lstate->flags & F_ATIO_STARVED) != 0) {
1280a73ff510SHidetoshi Shimokawa 			struct sbp_targ_login *login;
1281a73ff510SHidetoshi Shimokawa 
1282e9e688e2SHidetoshi Shimokawa 			if (debug)
128310d3ed64SHidetoshi Shimokawa 				printf("%s: new atio arrived\n", __func__);
1284a73ff510SHidetoshi Shimokawa 			lstate->flags &= ~F_ATIO_STARVED;
1285a73ff510SHidetoshi Shimokawa 			STAILQ_FOREACH(login, &lstate->logins, link)
1286a73ff510SHidetoshi Shimokawa 				if ((login->flags & F_ATIO_STARVED) != 0) {
1287a73ff510SHidetoshi Shimokawa 					login->flags &= ~F_ATIO_STARVED;
1288a73ff510SHidetoshi Shimokawa 					sbp_targ_fetch_orb(lstate->sc,
1289a73ff510SHidetoshi Shimokawa 					    login->fwdev,
1290a73ff510SHidetoshi Shimokawa 					    login->last_hi, login->last_lo,
1291a73ff510SHidetoshi Shimokawa 					    login, FETCH_CMD);
1292a73ff510SHidetoshi Shimokawa 				}
1293e9e688e2SHidetoshi Shimokawa 		}
1294e9e688e2SHidetoshi Shimokawa 		break;
1295b79dc8a8SKenneth D. Merry 	case XPT_NOTIFY_ACKNOWLEDGE:	/* recycle notify ack */
1296b79dc8a8SKenneth D. Merry 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
1297e9e688e2SHidetoshi Shimokawa 		if (status != CAM_REQ_CMP) {
1298e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = status;
1299e9e688e2SHidetoshi Shimokawa 			xpt_done(ccb);
1300e9e688e2SHidetoshi Shimokawa 			break;
1301e9e688e2SHidetoshi Shimokawa 		}
1302e9e688e2SHidetoshi Shimokawa 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
1303e9e688e2SHidetoshi Shimokawa 		    sim_links.sle);
1304e9e688e2SHidetoshi Shimokawa 		ccb->ccb_h.status = CAM_REQ_INPROG;
1305e9e688e2SHidetoshi Shimokawa 		sbp_targ_send_lstate_events(sc, lstate);
1306e9e688e2SHidetoshi Shimokawa 		break;
1307e9e688e2SHidetoshi Shimokawa 	case XPT_EN_LUN:
1308e9e688e2SHidetoshi Shimokawa 		sbp_targ_en_lun(sc, ccb);
1309e9e688e2SHidetoshi Shimokawa 		xpt_done(ccb);
1310e9e688e2SHidetoshi Shimokawa 		break;
1311e9e688e2SHidetoshi Shimokawa 	case XPT_PATH_INQ:
1312e9e688e2SHidetoshi Shimokawa 	{
1313e9e688e2SHidetoshi Shimokawa 		struct ccb_pathinq *cpi = &ccb->cpi;
1314e9e688e2SHidetoshi Shimokawa 
1315e9e688e2SHidetoshi Shimokawa 		cpi->version_num = 1; /* XXX??? */
1316e9e688e2SHidetoshi Shimokawa 		cpi->hba_inquiry = PI_TAG_ABLE;
1317e9e688e2SHidetoshi Shimokawa 		cpi->target_sprt = PIT_PROCESSOR
1318e9e688e2SHidetoshi Shimokawa 				 | PIT_DISCONNECT
1319e9e688e2SHidetoshi Shimokawa 				 | PIT_TERM_IO;
13204636d244SSean Bruno 		cpi->transport = XPORT_SPI; /* FIXME add XPORT_FW type to cam */
132164e574c2SAlexander Motin 		cpi->hba_misc = PIM_NOINITIATOR | PIM_NOBUSRESET |
132264e574c2SAlexander Motin 		    PIM_NO_6_BYTE;
1323e9e688e2SHidetoshi Shimokawa 		cpi->hba_eng_cnt = 0;
1324e9e688e2SHidetoshi Shimokawa 		cpi->max_target = 7; /* XXX */
1325e9e688e2SHidetoshi Shimokawa 		cpi->max_lun = MAX_LUN - 1;
1326e9e688e2SHidetoshi Shimokawa 		cpi->initiator_id = 7; /* XXX */
1327e9e688e2SHidetoshi Shimokawa 		cpi->bus_id = sim->bus_id;
1328e9e688e2SHidetoshi Shimokawa 		cpi->base_transfer_speed = 400 * 1000 / 8;
13294195c7deSAlan Somers 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
13304195c7deSAlan Somers 		strlcpy(cpi->hba_vid, "SBP_TARG", HBA_IDLEN);
13314195c7deSAlan Somers 		strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
1332e9e688e2SHidetoshi Shimokawa 		cpi->unit_number = sim->unit_number;
1333e9e688e2SHidetoshi Shimokawa 
1334e9e688e2SHidetoshi Shimokawa 		cpi->ccb_h.status = CAM_REQ_CMP;
1335e9e688e2SHidetoshi Shimokawa 		xpt_done(ccb);
1336e9e688e2SHidetoshi Shimokawa 		break;
1337e9e688e2SHidetoshi Shimokawa 	}
1338e9e688e2SHidetoshi Shimokawa 	case XPT_ABORT:
1339e9e688e2SHidetoshi Shimokawa 	{
1340e9e688e2SHidetoshi Shimokawa 		union ccb *accb = ccb->cab.abort_ccb;
1341e9e688e2SHidetoshi Shimokawa 
1342e9e688e2SHidetoshi Shimokawa 		switch (accb->ccb_h.func_code) {
1343e9e688e2SHidetoshi Shimokawa 		case XPT_ACCEPT_TARGET_IO:
1344b79dc8a8SKenneth D. Merry 		case XPT_IMMEDIATE_NOTIFY:
1345e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = sbp_targ_abort_ccb(sc, ccb);
1346e9e688e2SHidetoshi Shimokawa 			break;
1347e9e688e2SHidetoshi Shimokawa 		case XPT_CONT_TARGET_IO:
1348e9e688e2SHidetoshi Shimokawa 			/* XXX */
1349e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_UA_ABORT;
1350e9e688e2SHidetoshi Shimokawa 			break;
1351e9e688e2SHidetoshi Shimokawa 		default:
1352e9e688e2SHidetoshi Shimokawa 			printf("%s: aborting unknown function %d\n",
135310d3ed64SHidetoshi Shimokawa 				__func__, accb->ccb_h.func_code);
1354e9e688e2SHidetoshi Shimokawa 			ccb->ccb_h.status = CAM_REQ_INVALID;
1355e9e688e2SHidetoshi Shimokawa 			break;
1356e9e688e2SHidetoshi Shimokawa 		}
1357e9e688e2SHidetoshi Shimokawa 		xpt_done(ccb);
1358e9e688e2SHidetoshi Shimokawa 		break;
1359e9e688e2SHidetoshi Shimokawa 	}
13604636d244SSean Bruno #ifdef CAM_NEW_TRAN_CODE
13614636d244SSean Bruno 	case XPT_SET_TRAN_SETTINGS:
1362e9e688e2SHidetoshi Shimokawa 		ccb->ccb_h.status = CAM_REQ_INVALID;
1363e9e688e2SHidetoshi Shimokawa 		xpt_done(ccb);
1364e9e688e2SHidetoshi Shimokawa 		break;
13654636d244SSean Bruno 	case XPT_GET_TRAN_SETTINGS:
13664636d244SSean Bruno 	{
13674636d244SSean Bruno 		struct ccb_trans_settings *cts = &ccb->cts;
13684636d244SSean Bruno 		struct ccb_trans_settings_scsi *scsi =
13694636d244SSean Bruno 			&cts->proto_specific.scsi;
13704636d244SSean Bruno 		struct ccb_trans_settings_spi *spi =
13714636d244SSean Bruno 			&cts->xport_specific.spi;
13724636d244SSean Bruno 
13734636d244SSean Bruno 		cts->protocol = PROTO_SCSI;
13744636d244SSean Bruno 		cts->protocol_version = SCSI_REV_2;
13754636d244SSean Bruno 		cts->transport = XPORT_FW;     /* should have a FireWire */
13764636d244SSean Bruno 		cts->transport_version = 2;
13774636d244SSean Bruno 		spi->valid = CTS_SPI_VALID_DISC;
13784636d244SSean Bruno 		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
13794636d244SSean Bruno 		scsi->valid = CTS_SCSI_VALID_TQ;
13804636d244SSean Bruno 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
13814636d244SSean Bruno #if 0
13824636d244SSean Bruno 		printf("%s:%d:%d XPT_GET_TRAN_SETTINGS:\n",
13834636d244SSean Bruno 			device_get_nameunit(sc->fd.dev),
13844636d244SSean Bruno 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
13854636d244SSean Bruno #endif
13864636d244SSean Bruno 		cts->ccb_h.status = CAM_REQ_CMP;
13874636d244SSean Bruno 		xpt_done(ccb);
13884636d244SSean Bruno 		break;
13894636d244SSean Bruno 	}
13904636d244SSean Bruno #endif
13914636d244SSean Bruno 
13924636d244SSean Bruno 	default:
13934636d244SSean Bruno 		printf("%s: unknown function 0x%x\n",
13944636d244SSean Bruno 		    __func__, ccb->ccb_h.func_code);
13954636d244SSean Bruno 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
13964636d244SSean Bruno 		xpt_done(ccb);
13974636d244SSean Bruno 		break;
1398e9e688e2SHidetoshi Shimokawa 	}
1399e9e688e2SHidetoshi Shimokawa 	return;
1400e9e688e2SHidetoshi Shimokawa }
1401e9e688e2SHidetoshi Shimokawa 
1402e9e688e2SHidetoshi Shimokawa static void
1403e9e688e2SHidetoshi Shimokawa sbp_targ_action(struct cam_sim *sim, union ccb *ccb)
1404e9e688e2SHidetoshi Shimokawa {
1405e9e688e2SHidetoshi Shimokawa 	int s;
1406e9e688e2SHidetoshi Shimokawa 
1407e9e688e2SHidetoshi Shimokawa 	s = splfw();
1408e9e688e2SHidetoshi Shimokawa 	sbp_targ_action1(sim, ccb);
1409e9e688e2SHidetoshi Shimokawa 	splx(s);
1410e9e688e2SHidetoshi Shimokawa }
1411e9e688e2SHidetoshi Shimokawa 
1412e9e688e2SHidetoshi Shimokawa static void
1413e9e688e2SHidetoshi Shimokawa sbp_targ_poll(struct cam_sim *sim)
1414e9e688e2SHidetoshi Shimokawa {
1415e9e688e2SHidetoshi Shimokawa 	/* XXX */
1416e9e688e2SHidetoshi Shimokawa 	return;
1417e9e688e2SHidetoshi Shimokawa }
1418e9e688e2SHidetoshi Shimokawa 
1419e9e688e2SHidetoshi Shimokawa static void
1420e9e688e2SHidetoshi Shimokawa sbp_targ_cmd_handler(struct fw_xfer *xfer)
1421e9e688e2SHidetoshi Shimokawa {
1422e9e688e2SHidetoshi Shimokawa 	struct fw_pkt *fp;
142303161bbcSDoug Rabson 	uint32_t *orb;
1424e9e688e2SHidetoshi Shimokawa 	struct corb4 *orb4;
1425e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
1426e9e688e2SHidetoshi Shimokawa 	struct ccb_accept_tio *atio;
1427e9e688e2SHidetoshi Shimokawa 	u_char *bytes;
1428e9e688e2SHidetoshi Shimokawa 	int i;
1429e9e688e2SHidetoshi Shimokawa 
1430e9e688e2SHidetoshi Shimokawa 	orbi = (struct orb_info *)xfer->sc;
1431e9e688e2SHidetoshi Shimokawa 	if (xfer->resp != 0) {
143210d3ed64SHidetoshi Shimokawa 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1433e9e688e2SHidetoshi Shimokawa 		orbi->status.resp = SBP_TRANS_FAIL;
14341398a889SHidetoshi Shimokawa 		orbi->status.status = OBJ_ORB | SBE_TIMEOUT/*XXX*/;
1435e9e688e2SHidetoshi Shimokawa 		orbi->status.dead = 1;
1436e9e688e2SHidetoshi Shimokawa 		orbi->status.len = 1;
14379950b741SHidetoshi Shimokawa 		sbp_targ_abort(orbi->sc, STAILQ_NEXT(orbi, link));
1438e9e688e2SHidetoshi Shimokawa 
1439e9e688e2SHidetoshi Shimokawa 		sbp_targ_status_FIFO(orbi,
1440a73ff510SHidetoshi Shimokawa 		    orbi->login->fifo_hi, orbi->login->fifo_lo, /*dequeue*/1);
1441e9e688e2SHidetoshi Shimokawa 		fw_xfer_free(xfer);
1442e9e688e2SHidetoshi Shimokawa 		return;
1443e9e688e2SHidetoshi Shimokawa 	}
1444e9e688e2SHidetoshi Shimokawa 	fp = &xfer->recv.hdr;
1445e9e688e2SHidetoshi Shimokawa 
14469950b741SHidetoshi Shimokawa 	atio = orbi->atio;
14479950b741SHidetoshi Shimokawa 
1448e9e688e2SHidetoshi Shimokawa 	if (orbi->state == ORBI_STATUS_ABORTED) {
144910d3ed64SHidetoshi Shimokawa 		printf("%s: aborted\n", __func__);
1450a73ff510SHidetoshi Shimokawa 		sbp_targ_remove_orb_info(orbi->login, orbi);
1451e9e688e2SHidetoshi Shimokawa 		free(orbi, M_SBP_TARG);
14529950b741SHidetoshi Shimokawa 		atio->ccb_h.status = CAM_REQ_ABORTED;
14539950b741SHidetoshi Shimokawa 		xpt_done((union ccb*)atio);
1454e9e688e2SHidetoshi Shimokawa 		goto done0;
1455e9e688e2SHidetoshi Shimokawa 	}
1456e9e688e2SHidetoshi Shimokawa 	orbi->state = ORBI_STATUS_ATIO;
1457e9e688e2SHidetoshi Shimokawa 
1458e9e688e2SHidetoshi Shimokawa 	orb = orbi->orb;
1459e9e688e2SHidetoshi Shimokawa 	/* swap payload except SCSI command */
1460e9e688e2SHidetoshi Shimokawa 	for (i = 0; i < 5; i++)
1461e9e688e2SHidetoshi Shimokawa 		orb[i] = ntohl(orb[i]);
1462e9e688e2SHidetoshi Shimokawa 
1463e9e688e2SHidetoshi Shimokawa 	orb4 = (struct corb4 *)&orb[4];
1464e9e688e2SHidetoshi Shimokawa 	if (orb4->rq_fmt != 0) {
1465e9e688e2SHidetoshi Shimokawa 		/* XXX */
146610d3ed64SHidetoshi Shimokawa 		printf("%s: rq_fmt(%d) != 0\n", __func__, orb4->rq_fmt);
1467e9e688e2SHidetoshi Shimokawa 	}
1468e9e688e2SHidetoshi Shimokawa 
1469e9e688e2SHidetoshi Shimokawa 	atio->ccb_h.target_id = 0; /* XXX */
1470a73ff510SHidetoshi Shimokawa 	atio->ccb_h.target_lun = orbi->login->lstate->lun;
1471e9e688e2SHidetoshi Shimokawa 	atio->sense_len = 0;
14724636d244SSean Bruno 	atio->tag_action = MSG_SIMPLE_TASK;
1473e9e688e2SHidetoshi Shimokawa 	atio->tag_id = orbi->orb_lo;
1474a73ff510SHidetoshi Shimokawa 	atio->init_id = orbi->login->id;
1475a73ff510SHidetoshi Shimokawa 
14765e63cdb4SAlexander Motin 	atio->ccb_h.flags |= CAM_TAG_ACTION_VALID;
14779950b741SHidetoshi Shimokawa 	bytes = (u_char *)&orb[5];
1478e9e688e2SHidetoshi Shimokawa 	if (debug)
14799950b741SHidetoshi Shimokawa 		printf("%s: %p %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
14809950b741SHidetoshi Shimokawa 		    __func__, (void *)atio,
1481e9e688e2SHidetoshi Shimokawa 		    bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
1482e9e688e2SHidetoshi Shimokawa 		    bytes[5], bytes[6], bytes[7], bytes[8], bytes[9]);
1483e9e688e2SHidetoshi Shimokawa 	switch (bytes[0] >> 5) {
1484e9e688e2SHidetoshi Shimokawa 	case 0:
1485e9e688e2SHidetoshi Shimokawa 		atio->cdb_len = 6;
1486e9e688e2SHidetoshi Shimokawa 		break;
1487e9e688e2SHidetoshi Shimokawa 	case 1:
1488e9e688e2SHidetoshi Shimokawa 	case 2:
1489e9e688e2SHidetoshi Shimokawa 		atio->cdb_len = 10;
1490e9e688e2SHidetoshi Shimokawa 		break;
1491e9e688e2SHidetoshi Shimokawa 	case 4:
1492e9e688e2SHidetoshi Shimokawa 		atio->cdb_len = 16;
1493e9e688e2SHidetoshi Shimokawa 		break;
1494e9e688e2SHidetoshi Shimokawa 	case 5:
1495e9e688e2SHidetoshi Shimokawa 		atio->cdb_len = 12;
1496e9e688e2SHidetoshi Shimokawa 		break;
1497e9e688e2SHidetoshi Shimokawa 	case 3:
1498e9e688e2SHidetoshi Shimokawa 	default:
1499e9e688e2SHidetoshi Shimokawa 		/* Only copy the opcode. */
1500e9e688e2SHidetoshi Shimokawa 		atio->cdb_len = 1;
1501e9e688e2SHidetoshi Shimokawa 		printf("Reserved or VU command code type encountered\n");
1502e9e688e2SHidetoshi Shimokawa 		break;
1503e9e688e2SHidetoshi Shimokawa 	}
1504e9e688e2SHidetoshi Shimokawa 
1505e9e688e2SHidetoshi Shimokawa 	memcpy(atio->cdb_io.cdb_bytes, bytes, atio->cdb_len);
1506e9e688e2SHidetoshi Shimokawa 
1507e9e688e2SHidetoshi Shimokawa 	atio->ccb_h.status |= CAM_CDB_RECVD;
1508e9e688e2SHidetoshi Shimokawa 
1509e9e688e2SHidetoshi Shimokawa 	/* next ORB */
1510e9e688e2SHidetoshi Shimokawa 	if ((orb[0] & (1<<31)) == 0) {
1511e9e688e2SHidetoshi Shimokawa 		if (debug)
151210d3ed64SHidetoshi Shimokawa 			printf("%s: fetch next orb\n", __func__);
1513e9e688e2SHidetoshi Shimokawa 		orbi->status.src = SRC_NEXT_EXISTS;
1514e9e688e2SHidetoshi Shimokawa 		sbp_targ_fetch_orb(orbi->sc, orbi->fwdev,
1515a73ff510SHidetoshi Shimokawa 		    orb[0], orb[1], orbi->login, FETCH_CMD);
1516e9e688e2SHidetoshi Shimokawa 	} else {
1517e9e688e2SHidetoshi Shimokawa 		orbi->status.src = SRC_NO_NEXT;
1518a73ff510SHidetoshi Shimokawa 		orbi->login->flags &= ~F_LINK_ACTIVE;
1519e9e688e2SHidetoshi Shimokawa 	}
1520e9e688e2SHidetoshi Shimokawa 
1521e9e688e2SHidetoshi Shimokawa 	orbi->data_hi = orb[2];
1522e9e688e2SHidetoshi Shimokawa 	orbi->data_lo = orb[3];
1523e9e688e2SHidetoshi Shimokawa 	orbi->orb4 = *orb4;
1524e9e688e2SHidetoshi Shimokawa 
1525e9e688e2SHidetoshi Shimokawa 	xpt_done((union ccb*)atio);
1526e9e688e2SHidetoshi Shimokawa done0:
1527e9e688e2SHidetoshi Shimokawa 	fw_xfer_free(xfer);
1528e9e688e2SHidetoshi Shimokawa 	return;
1529e9e688e2SHidetoshi Shimokawa }
1530e9e688e2SHidetoshi Shimokawa 
1531a73ff510SHidetoshi Shimokawa static struct sbp_targ_login *
1532a73ff510SHidetoshi Shimokawa sbp_targ_get_login(struct sbp_targ_softc *sc, struct fw_device *fwdev, int lun)
1533a73ff510SHidetoshi Shimokawa {
1534a73ff510SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
1535a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *login;
1536a73ff510SHidetoshi Shimokawa 	int i;
1537a73ff510SHidetoshi Shimokawa 
1538a73ff510SHidetoshi Shimokawa 	lstate = sc->lstate[lun];
1539a73ff510SHidetoshi Shimokawa 
1540a73ff510SHidetoshi Shimokawa 	STAILQ_FOREACH(login, &lstate->logins, link)
1541a73ff510SHidetoshi Shimokawa 		if (login->fwdev == fwdev)
1542a73ff510SHidetoshi Shimokawa 			return (login);
1543a73ff510SHidetoshi Shimokawa 
1544a73ff510SHidetoshi Shimokawa 	for (i = 0; i < MAX_LOGINS; i++)
1545a73ff510SHidetoshi Shimokawa 		if (sc->logins[i] == NULL)
1546a73ff510SHidetoshi Shimokawa 			goto found;
1547a73ff510SHidetoshi Shimokawa 
154810d3ed64SHidetoshi Shimokawa 	printf("%s: increase MAX_LOGIN\n", __func__);
1549a73ff510SHidetoshi Shimokawa 	return (NULL);
1550a73ff510SHidetoshi Shimokawa 
1551a73ff510SHidetoshi Shimokawa found:
1552a73ff510SHidetoshi Shimokawa 	login = (struct sbp_targ_login *)malloc(
1553a73ff510SHidetoshi Shimokawa 	    sizeof(struct sbp_targ_login), M_SBP_TARG, M_NOWAIT | M_ZERO);
1554a73ff510SHidetoshi Shimokawa 
1555a73ff510SHidetoshi Shimokawa 	if (login == NULL) {
155610d3ed64SHidetoshi Shimokawa 		printf("%s: malloc failed\n", __func__);
1557a73ff510SHidetoshi Shimokawa 		return (NULL);
1558a73ff510SHidetoshi Shimokawa 	}
1559a73ff510SHidetoshi Shimokawa 
1560c6cf3e20SHidetoshi Shimokawa 	login->id = i;
1561a73ff510SHidetoshi Shimokawa 	login->fwdev = fwdev;
1562a73ff510SHidetoshi Shimokawa 	login->lstate = lstate;
1563a73ff510SHidetoshi Shimokawa 	login->last_hi = 0xffff;
1564a73ff510SHidetoshi Shimokawa 	login->last_lo = 0xffffffff;
1565a73ff510SHidetoshi Shimokawa 	login->hold_sec = 1;
1566a73ff510SHidetoshi Shimokawa 	STAILQ_INIT(&login->orbs);
1567a73ff510SHidetoshi Shimokawa 	CALLOUT_INIT(&login->hold_callout);
1568a73ff510SHidetoshi Shimokawa 	sc->logins[i] = login;
1569a73ff510SHidetoshi Shimokawa 	return (login);
1570a73ff510SHidetoshi Shimokawa }
1571a73ff510SHidetoshi Shimokawa 
1572e9e688e2SHidetoshi Shimokawa static void
1573e9e688e2SHidetoshi Shimokawa sbp_targ_mgm_handler(struct fw_xfer *xfer)
1574e9e688e2SHidetoshi Shimokawa {
1575e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
1576a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *login;
1577e9e688e2SHidetoshi Shimokawa 	struct fw_pkt *fp;
157803161bbcSDoug Rabson 	uint32_t *orb;
1579e9e688e2SHidetoshi Shimokawa 	struct morb4 *orb4;
1580e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
1581e9e688e2SHidetoshi Shimokawa 	int i;
1582e9e688e2SHidetoshi Shimokawa 
1583e9e688e2SHidetoshi Shimokawa 	orbi = (struct orb_info *)xfer->sc;
1584e9e688e2SHidetoshi Shimokawa 	if (xfer->resp != 0) {
158510d3ed64SHidetoshi Shimokawa 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1586e9e688e2SHidetoshi Shimokawa 		orbi->status.resp = SBP_TRANS_FAIL;
15871398a889SHidetoshi Shimokawa 		orbi->status.status = OBJ_ORB | SBE_TIMEOUT/*XXX*/;
1588e9e688e2SHidetoshi Shimokawa 		orbi->status.dead = 1;
1589e9e688e2SHidetoshi Shimokawa 		orbi->status.len = 1;
15909950b741SHidetoshi Shimokawa 		sbp_targ_abort(orbi->sc, STAILQ_NEXT(orbi, link));
1591e9e688e2SHidetoshi Shimokawa 
1592e9e688e2SHidetoshi Shimokawa 		sbp_targ_status_FIFO(orbi,
1593a73ff510SHidetoshi Shimokawa 		    orbi->login->fifo_hi, orbi->login->fifo_lo, /*dequeue*/0);
1594e9e688e2SHidetoshi Shimokawa 		fw_xfer_free(xfer);
1595e9e688e2SHidetoshi Shimokawa 		return;
1596e9e688e2SHidetoshi Shimokawa 	}
1597e9e688e2SHidetoshi Shimokawa 	fp = &xfer->recv.hdr;
1598e9e688e2SHidetoshi Shimokawa 
1599e9e688e2SHidetoshi Shimokawa 	orb = orbi->orb;
1600e9e688e2SHidetoshi Shimokawa 	/* swap payload */
1601e9e688e2SHidetoshi Shimokawa 	for (i = 0; i < 8; i++) {
1602e9e688e2SHidetoshi Shimokawa 		orb[i] = ntohl(orb[i]);
1603e9e688e2SHidetoshi Shimokawa 	}
1604e9e688e2SHidetoshi Shimokawa 	orb4 = (struct morb4 *)&orb[4];
1605e9e688e2SHidetoshi Shimokawa 	if (debug)
160610d3ed64SHidetoshi Shimokawa 		printf("%s: %s\n", __func__, orb_fun_name[orb4->fun]);
1607e9e688e2SHidetoshi Shimokawa 
1608e9e688e2SHidetoshi Shimokawa 	orbi->status.src = SRC_NO_NEXT;
1609e9e688e2SHidetoshi Shimokawa 
1610e9e688e2SHidetoshi Shimokawa 	switch (orb4->fun << 16) {
1611e9e688e2SHidetoshi Shimokawa 	case ORB_FUN_LGI:
1612e9e688e2SHidetoshi Shimokawa 	{
1613a73ff510SHidetoshi Shimokawa 		int exclusive = 0, lun;
1614e9e688e2SHidetoshi Shimokawa 
1615a73ff510SHidetoshi Shimokawa 		if (orb[4] & ORB_EXV)
1616a73ff510SHidetoshi Shimokawa 			exclusive = 1;
1617a73ff510SHidetoshi Shimokawa 
1618a73ff510SHidetoshi Shimokawa 		lun = orb4->id;
1619a73ff510SHidetoshi Shimokawa 		lstate = orbi->sc->lstate[lun];
1620a73ff510SHidetoshi Shimokawa 
1621a73ff510SHidetoshi Shimokawa 		if (lun >= MAX_LUN || lstate == NULL ||
1622a73ff510SHidetoshi Shimokawa 		    (exclusive &&
1623a73ff510SHidetoshi Shimokawa 		    STAILQ_FIRST(&lstate->logins) != NULL &&
1624a73ff510SHidetoshi Shimokawa 		    STAILQ_FIRST(&lstate->logins)->fwdev != orbi->fwdev)
1625a73ff510SHidetoshi Shimokawa 		   ) {
1626e9e688e2SHidetoshi Shimokawa 			/* error */
1627e9e688e2SHidetoshi Shimokawa 			orbi->status.dead = 1;
1628e9e688e2SHidetoshi Shimokawa 			orbi->status.status = STATUS_ACCESS_DENY;
1629e9e688e2SHidetoshi Shimokawa 			orbi->status.len = 1;
1630e9e688e2SHidetoshi Shimokawa 			break;
1631e9e688e2SHidetoshi Shimokawa 		}
1632a73ff510SHidetoshi Shimokawa 
1633a73ff510SHidetoshi Shimokawa 		/* allocate login */
1634a73ff510SHidetoshi Shimokawa 		login = sbp_targ_get_login(orbi->sc, orbi->fwdev, lun);
1635a73ff510SHidetoshi Shimokawa 		if (login == NULL) {
1636a73ff510SHidetoshi Shimokawa 			printf("%s: sbp_targ_get_login failed\n",
163710d3ed64SHidetoshi Shimokawa 			    __func__);
1638a73ff510SHidetoshi Shimokawa 			orbi->status.dead = 1;
1639a73ff510SHidetoshi Shimokawa 			orbi->status.status = STATUS_RES_UNAVAIL;
1640a73ff510SHidetoshi Shimokawa 			orbi->status.len = 1;
1641a73ff510SHidetoshi Shimokawa 			break;
1642a73ff510SHidetoshi Shimokawa 		}
16439950b741SHidetoshi Shimokawa 		printf("%s: login id=%d\n", __func__, login->id);
1644a73ff510SHidetoshi Shimokawa 
1645a73ff510SHidetoshi Shimokawa 		login->fifo_hi = orb[6];
1646a73ff510SHidetoshi Shimokawa 		login->fifo_lo = orb[7];
164703161bbcSDoug Rabson 		login->loginres.len = htons(sizeof(uint32_t) * 4);
1648a73ff510SHidetoshi Shimokawa 		login->loginres.id = htons(login->id);
1649a73ff510SHidetoshi Shimokawa 		login->loginres.cmd_hi = htons(SBP_TARG_BIND_HI);
1650a73ff510SHidetoshi Shimokawa 		login->loginres.cmd_lo = htonl(SBP_TARG_BIND_LO(login->id));
1651a73ff510SHidetoshi Shimokawa 		login->loginres.recon_hold = htons(login->hold_sec);
1652a73ff510SHidetoshi Shimokawa 
16539950b741SHidetoshi Shimokawa 		STAILQ_INSERT_TAIL(&lstate->logins, login, link);
16544636d244SSean Bruno 		fwmem_write_block(orbi->fwdev, NULL, /*spd*/FWSPD_S400, orb[2], orb[3],
1655a73ff510SHidetoshi Shimokawa 		    sizeof(struct sbp_login_res), (void *)&login->loginres,
1656e9e688e2SHidetoshi Shimokawa 		    fw_asy_callback_free);
1657c54d1fe2SHidetoshi Shimokawa 		/* XXX return status after loginres is successfully written */
1658e9e688e2SHidetoshi Shimokawa 		break;
1659e9e688e2SHidetoshi Shimokawa 	}
1660e9e688e2SHidetoshi Shimokawa 	case ORB_FUN_RCN:
1661a73ff510SHidetoshi Shimokawa 		login = orbi->sc->logins[orb4->id];
1662a73ff510SHidetoshi Shimokawa 		if (login != NULL && login->fwdev == orbi->fwdev) {
1663a73ff510SHidetoshi Shimokawa 			login->flags &= ~F_HOLD;
1664a73ff510SHidetoshi Shimokawa 			callout_stop(&login->hold_callout);
1665a73ff510SHidetoshi Shimokawa 			printf("%s: reconnected id=%d\n",
166610d3ed64SHidetoshi Shimokawa 			    __func__, login->id);
1667a73ff510SHidetoshi Shimokawa 		} else {
1668e9e688e2SHidetoshi Shimokawa 			orbi->status.dead = 1;
1669e9e688e2SHidetoshi Shimokawa 			orbi->status.status = STATUS_ACCESS_DENY;
1670a73ff510SHidetoshi Shimokawa 			printf("%s: reconnection faild id=%d\n",
167110d3ed64SHidetoshi Shimokawa 			    __func__, orb4->id);
1672a73ff510SHidetoshi Shimokawa 		}
1673a73ff510SHidetoshi Shimokawa 		break;
1674a73ff510SHidetoshi Shimokawa 	case ORB_FUN_LGO:
1675a73ff510SHidetoshi Shimokawa 		login = orbi->sc->logins[orb4->id];
1676a73ff510SHidetoshi Shimokawa 		if (login->fwdev != orbi->fwdev) {
167710d3ed64SHidetoshi Shimokawa 			printf("%s: wrong initiator\n", __func__);
1678a73ff510SHidetoshi Shimokawa 			break;
1679a73ff510SHidetoshi Shimokawa 		}
1680a73ff510SHidetoshi Shimokawa 		sbp_targ_dealloc_login(login);
1681e9e688e2SHidetoshi Shimokawa 		break;
1682e9e688e2SHidetoshi Shimokawa 	default:
1683e9e688e2SHidetoshi Shimokawa 		printf("%s: %s not implemented yet\n",
168410d3ed64SHidetoshi Shimokawa 		    __func__, orb_fun_name[orb4->fun]);
1685e9e688e2SHidetoshi Shimokawa 		break;
1686e9e688e2SHidetoshi Shimokawa 	}
1687e9e688e2SHidetoshi Shimokawa 	orbi->status.len = 1;
1688e9e688e2SHidetoshi Shimokawa 	sbp_targ_status_FIFO(orbi, orb[6], orb[7], /*dequeue*/0);
1689e9e688e2SHidetoshi Shimokawa 	fw_xfer_free(xfer);
1690e9e688e2SHidetoshi Shimokawa 	return;
1691e9e688e2SHidetoshi Shimokawa }
1692e9e688e2SHidetoshi Shimokawa 
1693e9e688e2SHidetoshi Shimokawa static void
1694e9e688e2SHidetoshi Shimokawa sbp_targ_pointer_handler(struct fw_xfer *xfer)
1695e9e688e2SHidetoshi Shimokawa {
1696e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
169703161bbcSDoug Rabson 	uint32_t orb0, orb1;
1698e9e688e2SHidetoshi Shimokawa 
1699e9e688e2SHidetoshi Shimokawa 	orbi = (struct orb_info *)xfer->sc;
1700e9e688e2SHidetoshi Shimokawa 	if (xfer->resp != 0) {
170110d3ed64SHidetoshi Shimokawa 		printf("%s: xfer->resp = %d\n", __func__, xfer->resp);
1702e9e688e2SHidetoshi Shimokawa 		goto done;
1703e9e688e2SHidetoshi Shimokawa 	}
1704e9e688e2SHidetoshi Shimokawa 
1705e9e688e2SHidetoshi Shimokawa 	orb0 = ntohl(orbi->orb[0]);
1706e9e688e2SHidetoshi Shimokawa 	orb1 = ntohl(orbi->orb[1]);
17077a22215cSEitan Adler 	if ((orb0 & (1U << 31)) != 0) {
170810d3ed64SHidetoshi Shimokawa 		printf("%s: invalid pointer\n", __func__);
1709e9e688e2SHidetoshi Shimokawa 		goto done;
1710e9e688e2SHidetoshi Shimokawa 	}
1711a73ff510SHidetoshi Shimokawa 	sbp_targ_fetch_orb(orbi->login->lstate->sc, orbi->fwdev,
171203161bbcSDoug Rabson 	    (uint16_t)orb0, orb1, orbi->login, FETCH_CMD);
1713e9e688e2SHidetoshi Shimokawa done:
1714e9e688e2SHidetoshi Shimokawa 	free(orbi, M_SBP_TARG);
1715e9e688e2SHidetoshi Shimokawa 	fw_xfer_free(xfer);
1716e9e688e2SHidetoshi Shimokawa 	return;
1717e9e688e2SHidetoshi Shimokawa }
1718e9e688e2SHidetoshi Shimokawa 
1719e9e688e2SHidetoshi Shimokawa static void
1720e9e688e2SHidetoshi Shimokawa sbp_targ_fetch_orb(struct sbp_targ_softc *sc, struct fw_device *fwdev,
172103161bbcSDoug Rabson     uint16_t orb_hi, uint32_t orb_lo, struct sbp_targ_login *login,
1722e9e688e2SHidetoshi Shimokawa     int mode)
1723e9e688e2SHidetoshi Shimokawa {
1724e9e688e2SHidetoshi Shimokawa 	struct orb_info *orbi;
1725e9e688e2SHidetoshi Shimokawa 
1726e9e688e2SHidetoshi Shimokawa 	if (debug)
172710d3ed64SHidetoshi Shimokawa 		printf("%s: fetch orb %04x:%08x\n", __func__, orb_hi, orb_lo);
1728e9e688e2SHidetoshi Shimokawa 	orbi = malloc(sizeof(struct orb_info), M_SBP_TARG, M_NOWAIT | M_ZERO);
1729e9e688e2SHidetoshi Shimokawa 	if (orbi == NULL) {
173010d3ed64SHidetoshi Shimokawa 		printf("%s: malloc failed\n", __func__);
1731e9e688e2SHidetoshi Shimokawa 		return;
1732e9e688e2SHidetoshi Shimokawa 	}
1733e9e688e2SHidetoshi Shimokawa 	orbi->sc = sc;
1734e9e688e2SHidetoshi Shimokawa 	orbi->fwdev = fwdev;
1735a73ff510SHidetoshi Shimokawa 	orbi->login = login;
1736e9e688e2SHidetoshi Shimokawa 	orbi->orb_hi = orb_hi;
1737e9e688e2SHidetoshi Shimokawa 	orbi->orb_lo = orb_lo;
1738e9e688e2SHidetoshi Shimokawa 	orbi->status.orb_hi = htons(orb_hi);
1739e9e688e2SHidetoshi Shimokawa 	orbi->status.orb_lo = htonl(orb_lo);
17404636d244SSean Bruno 	orbi->page_table = NULL;
1741e9e688e2SHidetoshi Shimokawa 
1742e9e688e2SHidetoshi Shimokawa 	switch (mode) {
1743e9e688e2SHidetoshi Shimokawa 	case FETCH_MGM:
17444636d244SSean Bruno 		fwmem_read_block(fwdev, (void *)orbi, /*spd*/FWSPD_S400, orb_hi, orb_lo,
174503161bbcSDoug Rabson 		    sizeof(uint32_t) * 8, &orbi->orb[0],
1746e9e688e2SHidetoshi Shimokawa 		    sbp_targ_mgm_handler);
1747e9e688e2SHidetoshi Shimokawa 		break;
1748e9e688e2SHidetoshi Shimokawa 	case FETCH_CMD:
1749e9e688e2SHidetoshi Shimokawa 		orbi->state = ORBI_STATUS_FETCH;
1750a73ff510SHidetoshi Shimokawa 		login->last_hi = orb_hi;
1751a73ff510SHidetoshi Shimokawa 		login->last_lo = orb_lo;
1752a73ff510SHidetoshi Shimokawa 		login->flags |= F_LINK_ACTIVE;
1753e9e688e2SHidetoshi Shimokawa 		/* dequeue */
17549950b741SHidetoshi Shimokawa 		SBP_LOCK(sc);
1755e9e688e2SHidetoshi Shimokawa 		orbi->atio = (struct ccb_accept_tio *)
1756a73ff510SHidetoshi Shimokawa 		    SLIST_FIRST(&login->lstate->accept_tios);
1757e9e688e2SHidetoshi Shimokawa 		if (orbi->atio == NULL) {
17589950b741SHidetoshi Shimokawa 			SBP_UNLOCK(sc);
175910d3ed64SHidetoshi Shimokawa 			printf("%s: no free atio\n", __func__);
1760a73ff510SHidetoshi Shimokawa 			login->lstate->flags |= F_ATIO_STARVED;
1761a73ff510SHidetoshi Shimokawa 			login->flags |= F_ATIO_STARVED;
1762a73ff510SHidetoshi Shimokawa #if 0
1763a73ff510SHidetoshi Shimokawa 			/* XXX ?? */
1764a73ff510SHidetoshi Shimokawa 			login->fwdev = fwdev;
1765a73ff510SHidetoshi Shimokawa #endif
1766e9e688e2SHidetoshi Shimokawa 			break;
1767e9e688e2SHidetoshi Shimokawa 		}
1768a73ff510SHidetoshi Shimokawa 		SLIST_REMOVE_HEAD(&login->lstate->accept_tios, sim_links.sle);
17699950b741SHidetoshi Shimokawa 		STAILQ_INSERT_TAIL(&login->orbs, orbi, link);
17709950b741SHidetoshi Shimokawa 		SBP_UNLOCK(sc);
17714636d244SSean Bruno 		fwmem_read_block(fwdev, (void *)orbi, /*spd*/FWSPD_S400, orb_hi, orb_lo,
177203161bbcSDoug Rabson 		    sizeof(uint32_t) * 8, &orbi->orb[0],
1773e9e688e2SHidetoshi Shimokawa 		    sbp_targ_cmd_handler);
1774e9e688e2SHidetoshi Shimokawa 		break;
1775e9e688e2SHidetoshi Shimokawa 	case FETCH_POINTER:
1776e9e688e2SHidetoshi Shimokawa 		orbi->state = ORBI_STATUS_POINTER;
1777a73ff510SHidetoshi Shimokawa 		login->flags |= F_LINK_ACTIVE;
17784636d244SSean Bruno 		fwmem_read_block(fwdev, (void *)orbi, /*spd*/FWSPD_S400, orb_hi, orb_lo,
177903161bbcSDoug Rabson 		    sizeof(uint32_t) * 2, &orbi->orb[0],
1780e9e688e2SHidetoshi Shimokawa 		    sbp_targ_pointer_handler);
1781e9e688e2SHidetoshi Shimokawa 		break;
1782e9e688e2SHidetoshi Shimokawa 	default:
178310d3ed64SHidetoshi Shimokawa 		printf("%s: invalid mode %d\n", __func__, mode);
1784e9e688e2SHidetoshi Shimokawa 	}
1785e9e688e2SHidetoshi Shimokawa }
1786e9e688e2SHidetoshi Shimokawa 
1787e9e688e2SHidetoshi Shimokawa static void
1788e9e688e2SHidetoshi Shimokawa sbp_targ_resp_callback(struct fw_xfer *xfer)
1789e9e688e2SHidetoshi Shimokawa {
1790e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
1791e9e688e2SHidetoshi Shimokawa 	int s;
1792e9e688e2SHidetoshi Shimokawa 
1793e9e688e2SHidetoshi Shimokawa 	if (debug)
179410d3ed64SHidetoshi Shimokawa 		printf("%s: xfer=%p\n", __func__, xfer);
1795e9e688e2SHidetoshi Shimokawa 	sc = (struct sbp_targ_softc *)xfer->sc;
1796e9e688e2SHidetoshi Shimokawa 	fw_xfer_unload(xfer);
1797e9e688e2SHidetoshi Shimokawa 	xfer->recv.pay_len = SBP_TARG_RECV_LEN;
1798801167a8SHidetoshi Shimokawa 	xfer->hand = sbp_targ_recv;
1799e9e688e2SHidetoshi Shimokawa 	s = splfw();
1800e9e688e2SHidetoshi Shimokawa 	STAILQ_INSERT_TAIL(&sc->fwb.xferlist, xfer, link);
1801e9e688e2SHidetoshi Shimokawa 	splx(s);
1802e9e688e2SHidetoshi Shimokawa }
1803e9e688e2SHidetoshi Shimokawa 
1804e9e688e2SHidetoshi Shimokawa static int
1805a73ff510SHidetoshi Shimokawa sbp_targ_cmd(struct fw_xfer *xfer, struct fw_device *fwdev, int login_id,
1806a73ff510SHidetoshi Shimokawa     int reg)
1807e9e688e2SHidetoshi Shimokawa {
1808a73ff510SHidetoshi Shimokawa 	struct sbp_targ_login *login;
1809e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
1810e9e688e2SHidetoshi Shimokawa 	int rtcode = 0;
1811e9e688e2SHidetoshi Shimokawa 
1812a73ff510SHidetoshi Shimokawa 	if (login_id < 0 || login_id >= MAX_LOGINS)
1813e9e688e2SHidetoshi Shimokawa 		return (RESP_ADDRESS_ERROR);
1814e9e688e2SHidetoshi Shimokawa 
1815e9e688e2SHidetoshi Shimokawa 	sc = (struct sbp_targ_softc *)xfer->sc;
1816a73ff510SHidetoshi Shimokawa 	login = sc->logins[login_id];
1817a73ff510SHidetoshi Shimokawa 	if (login == NULL)
1818e9e688e2SHidetoshi Shimokawa 		return (RESP_ADDRESS_ERROR);
1819e9e688e2SHidetoshi Shimokawa 
1820a73ff510SHidetoshi Shimokawa 	if (login->fwdev != fwdev) {
1821a73ff510SHidetoshi Shimokawa 		/* XXX */
1822a73ff510SHidetoshi Shimokawa 		return (RESP_ADDRESS_ERROR);
1823a73ff510SHidetoshi Shimokawa 	}
1824a73ff510SHidetoshi Shimokawa 
1825e9e688e2SHidetoshi Shimokawa 	switch (reg) {
1826e9e688e2SHidetoshi Shimokawa 	case 0x08:	/* ORB_POINTER */
1827e9e688e2SHidetoshi Shimokawa 		if (debug)
18289950b741SHidetoshi Shimokawa 			printf("%s: ORB_POINTER(%d)\n", __func__, login_id);
1829a73ff510SHidetoshi Shimokawa 		if ((login->flags & F_LINK_ACTIVE) != 0) {
1830a73ff510SHidetoshi Shimokawa 			if (debug)
1831a73ff510SHidetoshi Shimokawa 				printf("link active (ORB_POINTER)\n");
1832a73ff510SHidetoshi Shimokawa 			break;
1833a73ff510SHidetoshi Shimokawa 		}
1834a73ff510SHidetoshi Shimokawa 		sbp_targ_fetch_orb(sc, fwdev,
1835e9e688e2SHidetoshi Shimokawa 		    ntohl(xfer->recv.payload[0]),
1836e9e688e2SHidetoshi Shimokawa 		    ntohl(xfer->recv.payload[1]),
1837a73ff510SHidetoshi Shimokawa 		    login, FETCH_CMD);
1838e9e688e2SHidetoshi Shimokawa 		break;
1839e9e688e2SHidetoshi Shimokawa 	case 0x04:	/* AGENT_RESET */
1840e9e688e2SHidetoshi Shimokawa 		if (debug)
18419950b741SHidetoshi Shimokawa 			printf("%s: AGENT RESET(%d)\n", __func__, login_id);
1842a73ff510SHidetoshi Shimokawa 		login->last_hi = 0xffff;
1843a73ff510SHidetoshi Shimokawa 		login->last_lo = 0xffffffff;
18449950b741SHidetoshi Shimokawa 		sbp_targ_abort(sc, STAILQ_FIRST(&login->orbs));
1845e9e688e2SHidetoshi Shimokawa 		break;
1846e9e688e2SHidetoshi Shimokawa 	case 0x10:	/* DOORBELL */
1847e9e688e2SHidetoshi Shimokawa 		if (debug)
18489950b741SHidetoshi Shimokawa 			printf("%s: DOORBELL(%d)\n", __func__, login_id);
1849a73ff510SHidetoshi Shimokawa 		if (login->last_hi == 0xffff &&
1850a73ff510SHidetoshi Shimokawa 		    login->last_lo == 0xffffffff) {
1851e9e688e2SHidetoshi Shimokawa 			printf("%s: no previous pointer(DOORBELL)\n",
185210d3ed64SHidetoshi Shimokawa 			    __func__);
1853e9e688e2SHidetoshi Shimokawa 			break;
1854e9e688e2SHidetoshi Shimokawa 		}
1855a73ff510SHidetoshi Shimokawa 		if ((login->flags & F_LINK_ACTIVE) != 0) {
1856e9e688e2SHidetoshi Shimokawa 			if (debug)
1857e9e688e2SHidetoshi Shimokawa 				printf("link active (DOORBELL)\n");
1858e9e688e2SHidetoshi Shimokawa 			break;
1859e9e688e2SHidetoshi Shimokawa 		}
1860a73ff510SHidetoshi Shimokawa 		sbp_targ_fetch_orb(sc, fwdev,
1861a73ff510SHidetoshi Shimokawa 		    login->last_hi, login->last_lo,
1862a73ff510SHidetoshi Shimokawa 		    login, FETCH_POINTER);
1863e9e688e2SHidetoshi Shimokawa 		break;
1864e9e688e2SHidetoshi Shimokawa 	case 0x00:	/* AGENT_STATE */
18659950b741SHidetoshi Shimokawa 		printf("%s: AGENT_STATE (%d:ignore)\n", __func__, login_id);
1866e9e688e2SHidetoshi Shimokawa 		break;
1867e9e688e2SHidetoshi Shimokawa 	case 0x14:	/* UNSOLICITED_STATE_ENABLE */
18689950b741SHidetoshi Shimokawa 		printf("%s: UNSOLICITED_STATE_ENABLE (%d:ignore)\n",
18699950b741SHidetoshi Shimokawa 							 __func__, login_id);
1870e9e688e2SHidetoshi Shimokawa 		break;
1871e9e688e2SHidetoshi Shimokawa 	default:
18729950b741SHidetoshi Shimokawa 		printf("%s: invalid register %d(%d)\n",
18739950b741SHidetoshi Shimokawa 						 __func__, reg, login_id);
1874e9e688e2SHidetoshi Shimokawa 		rtcode = RESP_ADDRESS_ERROR;
1875e9e688e2SHidetoshi Shimokawa 	}
1876e9e688e2SHidetoshi Shimokawa 
1877e9e688e2SHidetoshi Shimokawa 	return (rtcode);
1878e9e688e2SHidetoshi Shimokawa }
1879e9e688e2SHidetoshi Shimokawa 
1880e9e688e2SHidetoshi Shimokawa static int
1881e9e688e2SHidetoshi Shimokawa sbp_targ_mgm(struct fw_xfer *xfer, struct fw_device *fwdev)
1882e9e688e2SHidetoshi Shimokawa {
1883e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
1884e9e688e2SHidetoshi Shimokawa 	struct fw_pkt *fp;
1885e9e688e2SHidetoshi Shimokawa 
1886e9e688e2SHidetoshi Shimokawa 	sc = (struct sbp_targ_softc *)xfer->sc;
1887e9e688e2SHidetoshi Shimokawa 
1888e9e688e2SHidetoshi Shimokawa 	fp = &xfer->recv.hdr;
1889e9e688e2SHidetoshi Shimokawa 	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
189010d3ed64SHidetoshi Shimokawa 		printf("%s: tcode = %d\n", __func__, fp->mode.wreqb.tcode);
1891e9e688e2SHidetoshi Shimokawa 		return (RESP_TYPE_ERROR);
1892e9e688e2SHidetoshi Shimokawa         }
1893e9e688e2SHidetoshi Shimokawa 
1894e9e688e2SHidetoshi Shimokawa 	sbp_targ_fetch_orb(sc, fwdev,
1895e9e688e2SHidetoshi Shimokawa 	    ntohl(xfer->recv.payload[0]),
1896e9e688e2SHidetoshi Shimokawa 	    ntohl(xfer->recv.payload[1]),
1897e9e688e2SHidetoshi Shimokawa 	    NULL, FETCH_MGM);
1898e9e688e2SHidetoshi Shimokawa 
1899e9e688e2SHidetoshi Shimokawa 	return (0);
1900e9e688e2SHidetoshi Shimokawa }
1901e9e688e2SHidetoshi Shimokawa 
1902e9e688e2SHidetoshi Shimokawa static void
1903e9e688e2SHidetoshi Shimokawa sbp_targ_recv(struct fw_xfer *xfer)
1904e9e688e2SHidetoshi Shimokawa {
1905e9e688e2SHidetoshi Shimokawa 	struct fw_pkt *fp, *sfp;
1906e9e688e2SHidetoshi Shimokawa 	struct fw_device *fwdev;
190703161bbcSDoug Rabson 	uint32_t lo;
1908e9e688e2SHidetoshi Shimokawa 	int s, rtcode;
1909e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
1910e9e688e2SHidetoshi Shimokawa 
1911e9e688e2SHidetoshi Shimokawa 	s = splfw();
1912e9e688e2SHidetoshi Shimokawa 	sc = (struct sbp_targ_softc *)xfer->sc;
1913e9e688e2SHidetoshi Shimokawa 	fp = &xfer->recv.hdr;
1914e9e688e2SHidetoshi Shimokawa 	fwdev = fw_noderesolve_nodeid(sc->fd.fc, fp->mode.wreqb.src & 0x3f);
1915e9e688e2SHidetoshi Shimokawa 	if (fwdev == NULL) {
1916e9e688e2SHidetoshi Shimokawa 		printf("%s: cannot resolve nodeid=%d\n",
191710d3ed64SHidetoshi Shimokawa 		    __func__, fp->mode.wreqb.src & 0x3f);
1918e9e688e2SHidetoshi Shimokawa 		rtcode = RESP_TYPE_ERROR; /* XXX */
1919e9e688e2SHidetoshi Shimokawa 		goto done;
1920e9e688e2SHidetoshi Shimokawa 	}
1921e9e688e2SHidetoshi Shimokawa 	lo = fp->mode.wreqb.dest_lo;
19229950b741SHidetoshi Shimokawa 
1923e9e688e2SHidetoshi Shimokawa 	if (lo == SBP_TARG_BIND_LO(-1))
1924e9e688e2SHidetoshi Shimokawa 		rtcode = sbp_targ_mgm(xfer, fwdev);
1925e9e688e2SHidetoshi Shimokawa 	else if (lo >= SBP_TARG_BIND_LO(0))
1926a73ff510SHidetoshi Shimokawa 		rtcode = sbp_targ_cmd(xfer, fwdev, SBP_TARG_LOGIN_ID(lo),
1927a73ff510SHidetoshi Shimokawa 		    lo % 0x20);
1928e9e688e2SHidetoshi Shimokawa 	else
1929e9e688e2SHidetoshi Shimokawa 		rtcode = RESP_ADDRESS_ERROR;
1930e9e688e2SHidetoshi Shimokawa 
1931e9e688e2SHidetoshi Shimokawa done:
1932e9e688e2SHidetoshi Shimokawa 	if (rtcode != 0)
193310d3ed64SHidetoshi Shimokawa 		printf("%s: rtcode = %d\n", __func__, rtcode);
1934e9e688e2SHidetoshi Shimokawa 	sfp = &xfer->send.hdr;
19354636d244SSean Bruno 	xfer->send.spd = FWSPD_S400;
1936801167a8SHidetoshi Shimokawa 	xfer->hand = sbp_targ_resp_callback;
1937e9e688e2SHidetoshi Shimokawa 	sfp->mode.wres.dst = fp->mode.wreqb.src;
1938e9e688e2SHidetoshi Shimokawa 	sfp->mode.wres.tlrt = fp->mode.wreqb.tlrt;
1939e9e688e2SHidetoshi Shimokawa 	sfp->mode.wres.tcode = FWTCODE_WRES;
1940e9e688e2SHidetoshi Shimokawa 	sfp->mode.wres.rtcode = rtcode;
1941e9e688e2SHidetoshi Shimokawa 	sfp->mode.wres.pri = 0;
1942e9e688e2SHidetoshi Shimokawa 
1943e9e688e2SHidetoshi Shimokawa 	fw_asyreq(xfer->fc, -1, xfer);
1944e9e688e2SHidetoshi Shimokawa 	splx(s);
1945e9e688e2SHidetoshi Shimokawa }
1946e9e688e2SHidetoshi Shimokawa 
1947e9e688e2SHidetoshi Shimokawa static int
1948e9e688e2SHidetoshi Shimokawa sbp_targ_attach(device_t dev)
1949e9e688e2SHidetoshi Shimokawa {
1950e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
1951e9e688e2SHidetoshi Shimokawa 	struct cam_devq *devq;
19529950b741SHidetoshi Shimokawa 	struct firewire_comm *fc;
1953e9e688e2SHidetoshi Shimokawa 
1954e9e688e2SHidetoshi Shimokawa         sc = (struct sbp_targ_softc *) device_get_softc(dev);
1955e9e688e2SHidetoshi Shimokawa 	bzero((void *)sc, sizeof(struct sbp_targ_softc));
1956e9e688e2SHidetoshi Shimokawa 
19579950b741SHidetoshi Shimokawa 	mtx_init(&sc->mtx, "sbp_targ", NULL, MTX_DEF);
19589950b741SHidetoshi Shimokawa 	sc->fd.fc = fc = device_get_ivars(dev);
1959e9e688e2SHidetoshi Shimokawa 	sc->fd.dev = dev;
1960a73ff510SHidetoshi Shimokawa 	sc->fd.post_explore = (void *) sbp_targ_post_explore;
1961e9e688e2SHidetoshi Shimokawa 	sc->fd.post_busreset = (void *) sbp_targ_post_busreset;
1962e9e688e2SHidetoshi Shimokawa 
1963c6cf3e20SHidetoshi Shimokawa         devq = cam_simq_alloc(/*maxopenings*/MAX_LUN*MAX_INITIATORS);
1964e9e688e2SHidetoshi Shimokawa 	if (devq == NULL)
1965e9e688e2SHidetoshi Shimokawa 		return (ENXIO);
1966e9e688e2SHidetoshi Shimokawa 
1967e9e688e2SHidetoshi Shimokawa 	sc->sim = cam_sim_alloc(sbp_targ_action, sbp_targ_poll,
19689950b741SHidetoshi Shimokawa 	    "sbp_targ", sc, device_get_unit(dev), &sc->mtx,
1969e9e688e2SHidetoshi Shimokawa 	    /*untagged*/ 1, /*tagged*/ 1, devq);
1970e9e688e2SHidetoshi Shimokawa 	if (sc->sim == NULL) {
1971e9e688e2SHidetoshi Shimokawa 		cam_simq_free(devq);
1972e9e688e2SHidetoshi Shimokawa 		return (ENXIO);
1973e9e688e2SHidetoshi Shimokawa 	}
1974e9e688e2SHidetoshi Shimokawa 
19759950b741SHidetoshi Shimokawa 	SBP_LOCK(sc);
1976b50569b7SScott Long 	if (xpt_bus_register(sc->sim, dev, /*bus*/0) != CAM_SUCCESS)
1977e9e688e2SHidetoshi Shimokawa 		goto fail;
1978e9e688e2SHidetoshi Shimokawa 
1979e9e688e2SHidetoshi Shimokawa 	if (xpt_create_path(&sc->path, /*periph*/ NULL, cam_sim_path(sc->sim),
1980e9e688e2SHidetoshi Shimokawa 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1981e9e688e2SHidetoshi Shimokawa 		xpt_bus_deregister(cam_sim_path(sc->sim));
1982e9e688e2SHidetoshi Shimokawa 		goto fail;
1983e9e688e2SHidetoshi Shimokawa 	}
19849950b741SHidetoshi Shimokawa 	SBP_UNLOCK(sc);
1985e9e688e2SHidetoshi Shimokawa 
1986e9e688e2SHidetoshi Shimokawa 	sc->fwb.start = SBP_TARG_BIND_START;
1987e9e688e2SHidetoshi Shimokawa 	sc->fwb.end = SBP_TARG_BIND_END;
1988e9e688e2SHidetoshi Shimokawa 
1989e9e688e2SHidetoshi Shimokawa 	/* pre-allocate xfer */
1990e9e688e2SHidetoshi Shimokawa 	STAILQ_INIT(&sc->fwb.xferlist);
19910892f4c5SHidetoshi Shimokawa 	fw_xferlist_add(&sc->fwb.xferlist, M_SBP_TARG,
19920892f4c5SHidetoshi Shimokawa 	    /*send*/ 0, /*recv*/ SBP_TARG_RECV_LEN, MAX_LUN /* XXX */,
19939950b741SHidetoshi Shimokawa 	    fc, (void *)sc, sbp_targ_recv);
19949950b741SHidetoshi Shimokawa 	fw_bindadd(fc, &sc->fwb);
1995e9e688e2SHidetoshi Shimokawa 	return 0;
1996e9e688e2SHidetoshi Shimokawa 
1997e9e688e2SHidetoshi Shimokawa fail:
19989950b741SHidetoshi Shimokawa 	SBP_UNLOCK(sc);
1999e9e688e2SHidetoshi Shimokawa 	cam_sim_free(sc->sim, /*free_devq*/TRUE);
2000e9e688e2SHidetoshi Shimokawa 	return (ENXIO);
2001e9e688e2SHidetoshi Shimokawa }
2002e9e688e2SHidetoshi Shimokawa 
2003e9e688e2SHidetoshi Shimokawa static int
2004e9e688e2SHidetoshi Shimokawa sbp_targ_detach(device_t dev)
2005e9e688e2SHidetoshi Shimokawa {
2006e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_softc *sc;
2007e9e688e2SHidetoshi Shimokawa 	struct sbp_targ_lstate *lstate;
2008e9e688e2SHidetoshi Shimokawa 	int i;
2009e9e688e2SHidetoshi Shimokawa 
2010e9e688e2SHidetoshi Shimokawa 	sc = (struct sbp_targ_softc *)device_get_softc(dev);
2011e9e688e2SHidetoshi Shimokawa 	sc->fd.post_busreset = NULL;
2012e9e688e2SHidetoshi Shimokawa 
20139950b741SHidetoshi Shimokawa 	SBP_LOCK(sc);
2014e9e688e2SHidetoshi Shimokawa 	xpt_free_path(sc->path);
2015e9e688e2SHidetoshi Shimokawa 	xpt_bus_deregister(cam_sim_path(sc->sim));
2016e9e688e2SHidetoshi Shimokawa 	cam_sim_free(sc->sim, /*free_devq*/TRUE);
201794792a2cSAlexander Motin 	SBP_UNLOCK(sc);
2018e9e688e2SHidetoshi Shimokawa 
2019e9e688e2SHidetoshi Shimokawa 	for (i = 0; i < MAX_LUN; i++) {
2020e9e688e2SHidetoshi Shimokawa 		lstate = sc->lstate[i];
2021e9e688e2SHidetoshi Shimokawa 		if (lstate != NULL) {
2022e9e688e2SHidetoshi Shimokawa 			xpt_free_path(lstate->path);
2023e9e688e2SHidetoshi Shimokawa 			free(lstate, M_SBP_TARG);
2024e9e688e2SHidetoshi Shimokawa 		}
2025e9e688e2SHidetoshi Shimokawa 	}
2026e9e688e2SHidetoshi Shimokawa 	if (sc->black_hole != NULL) {
2027e9e688e2SHidetoshi Shimokawa 		xpt_free_path(sc->black_hole->path);
2028e9e688e2SHidetoshi Shimokawa 		free(sc->black_hole, M_SBP_TARG);
2029e9e688e2SHidetoshi Shimokawa 	}
2030e9e688e2SHidetoshi Shimokawa 
2031e9e688e2SHidetoshi Shimokawa 	fw_bindremove(sc->fd.fc, &sc->fwb);
20320892f4c5SHidetoshi Shimokawa 	fw_xferlist_remove(&sc->fwb.xferlist);
2033e9e688e2SHidetoshi Shimokawa 
20349950b741SHidetoshi Shimokawa 	mtx_destroy(&sc->mtx);
20359950b741SHidetoshi Shimokawa 
2036e9e688e2SHidetoshi Shimokawa 	return 0;
2037e9e688e2SHidetoshi Shimokawa }
2038e9e688e2SHidetoshi Shimokawa 
2039e9e688e2SHidetoshi Shimokawa static devclass_t sbp_targ_devclass;
2040e9e688e2SHidetoshi Shimokawa 
2041e9e688e2SHidetoshi Shimokawa static device_method_t sbp_targ_methods[] = {
2042e9e688e2SHidetoshi Shimokawa 	/* device interface */
2043e9e688e2SHidetoshi Shimokawa 	DEVMETHOD(device_identify,	sbp_targ_identify),
2044e9e688e2SHidetoshi Shimokawa 	DEVMETHOD(device_probe,		sbp_targ_probe),
2045e9e688e2SHidetoshi Shimokawa 	DEVMETHOD(device_attach,	sbp_targ_attach),
2046e9e688e2SHidetoshi Shimokawa 	DEVMETHOD(device_detach,	sbp_targ_detach),
2047e9e688e2SHidetoshi Shimokawa 	{ 0, 0 }
2048e9e688e2SHidetoshi Shimokawa };
2049e9e688e2SHidetoshi Shimokawa 
2050e9e688e2SHidetoshi Shimokawa static driver_t sbp_targ_driver = {
2051e9e688e2SHidetoshi Shimokawa 	"sbp_targ",
2052e9e688e2SHidetoshi Shimokawa 	sbp_targ_methods,
2053e9e688e2SHidetoshi Shimokawa 	sizeof(struct sbp_targ_softc),
2054e9e688e2SHidetoshi Shimokawa };
2055e9e688e2SHidetoshi Shimokawa 
2056e9e688e2SHidetoshi Shimokawa DRIVER_MODULE(sbp_targ, firewire, sbp_targ_driver, sbp_targ_devclass, 0, 0);
2057e9e688e2SHidetoshi Shimokawa MODULE_VERSION(sbp_targ, 1);
2058e9e688e2SHidetoshi Shimokawa MODULE_DEPEND(sbp_targ, firewire, 1, 1, 1);
2059e9e688e2SHidetoshi Shimokawa MODULE_DEPEND(sbp_targ, cam, 1, 1, 1);
2060