xref: /freebsd-13-stable/sys/dev/mpt/mpt_cam.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * FreeBSD/CAM specific routines for LSI '909 FC  adapters.
3  * FreeBSD Version.
4  *
5  * SPDX-License-Identifier: BSD-2-Clause AND BSD-3-Clause
6  *
7  * Copyright (c)  2000, 2001 by Greg Ansley
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice immediately at the beginning of the file, without modification,
14  *    this list of conditions, and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 /*-
31  * Copyright (c) 2002, 2006 by Matthew Jacob
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions are
36  * met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
40  *    substantially similar to the "NO WARRANTY" disclaimer below
41  *    ("Disclaimer") and any redistribution must be conditioned upon including
42  *    a substantially similar Disclaimer requirement for further binary
43  *    redistribution.
44  * 3. Neither the names of the above listed copyright holders nor the names
45  *    of any contributors may be used to endorse or promote products derived
46  *    from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
58  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  *
60  * Support from Chris Ellsworth in order to make SAS adapters work
61  * is gratefully acknowledged.
62  *
63  * Support from LSI-Logic has also gone a great deal toward making this a
64  * workable subsystem and is gratefully acknowledged.
65  */
66 /*-
67  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
68  * Copyright (c) 2005, WHEEL Sp. z o.o.
69  * Copyright (c) 2004, 2005 Justin T. Gibbs
70  * All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions are
74  * met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
78  *    substantially similar to the "NO WARRANTY" disclaimer below
79  *    ("Disclaimer") and any redistribution must be conditioned upon including
80  *    a substantially similar Disclaimer requirement for further binary
81  *    redistribution.
82  * 3. Neither the names of the above listed copyright holders nor the names
83  *    of any contributors may be used to endorse or promote products derived
84  *    from this software without specific prior written permission.
85  *
86  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
90  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
91  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
92  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
94  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
95  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
96  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  */
98 #include <sys/cdefs.h>
99 #include <dev/mpt/mpt.h>
100 #include <dev/mpt/mpt_cam.h>
101 #include <dev/mpt/mpt_raid.h>
102 
103 #include "dev/mpt/mpilib/mpi_ioc.h" /* XXX Fix Event Handling!!! */
104 #include "dev/mpt/mpilib/mpi_init.h"
105 #include "dev/mpt/mpilib/mpi_targ.h"
106 #include "dev/mpt/mpilib/mpi_fc.h"
107 #include "dev/mpt/mpilib/mpi_sas.h"
108 
109 #include <sys/callout.h>
110 #include <sys/kthread.h>
111 #include <sys/sysctl.h>
112 
113 static void mpt_poll(struct cam_sim *);
114 static callout_func_t mpt_timeout;
115 static void mpt_action(struct cam_sim *, union ccb *);
116 static int
117 mpt_get_spi_settings(struct mpt_softc *, struct ccb_trans_settings *);
118 static void mpt_setwidth(struct mpt_softc *, int, int);
119 static void mpt_setsync(struct mpt_softc *, int, int, int);
120 static int mpt_update_spi_config(struct mpt_softc *, int);
121 
122 static mpt_reply_handler_t mpt_scsi_reply_handler;
123 static mpt_reply_handler_t mpt_scsi_tmf_reply_handler;
124 static mpt_reply_handler_t mpt_fc_els_reply_handler;
125 static int mpt_scsi_reply_frame_handler(struct mpt_softc *, request_t *,
126 					MSG_DEFAULT_REPLY *);
127 static int mpt_bus_reset(struct mpt_softc *, target_id_t, lun_id_t, int);
128 static int mpt_fc_reset_link(struct mpt_softc *, int);
129 
130 static int mpt_spawn_recovery_thread(struct mpt_softc *mpt);
131 static void mpt_terminate_recovery_thread(struct mpt_softc *mpt);
132 static void mpt_recovery_thread(void *arg);
133 static void mpt_recover_commands(struct mpt_softc *mpt);
134 
135 static int mpt_scsi_send_tmf(struct mpt_softc *, u_int, u_int, u_int,
136     target_id_t, lun_id_t, u_int, int);
137 
138 static void mpt_fc_post_els(struct mpt_softc *mpt, request_t *, int);
139 static void mpt_post_target_command(struct mpt_softc *, request_t *, int);
140 static int mpt_add_els_buffers(struct mpt_softc *mpt);
141 static int mpt_add_target_commands(struct mpt_softc *mpt);
142 static int mpt_enable_lun(struct mpt_softc *, target_id_t, lun_id_t);
143 static int mpt_disable_lun(struct mpt_softc *, target_id_t, lun_id_t);
144 static void mpt_target_start_io(struct mpt_softc *, union ccb *);
145 static cam_status mpt_abort_target_ccb(struct mpt_softc *, union ccb *);
146 static int mpt_abort_target_cmd(struct mpt_softc *, request_t *);
147 static void mpt_scsi_tgt_status(struct mpt_softc *, union ccb *, request_t *,
148     uint8_t, uint8_t const *, u_int);
149 static void
150 mpt_scsi_tgt_tsk_mgmt(struct mpt_softc *, request_t *, mpt_task_mgmt_t,
151     tgt_resource_t *, int);
152 static void mpt_tgt_dump_tgt_state(struct mpt_softc *, request_t *);
153 static void mpt_tgt_dump_req_state(struct mpt_softc *, request_t *);
154 static mpt_reply_handler_t mpt_scsi_tgt_reply_handler;
155 static mpt_reply_handler_t mpt_sata_pass_reply_handler;
156 
157 static uint32_t scsi_io_handler_id = MPT_HANDLER_ID_NONE;
158 static uint32_t scsi_tmf_handler_id = MPT_HANDLER_ID_NONE;
159 static uint32_t fc_els_handler_id = MPT_HANDLER_ID_NONE;
160 static uint32_t sata_pass_handler_id = MPT_HANDLER_ID_NONE;
161 
162 static mpt_probe_handler_t	mpt_cam_probe;
163 static mpt_attach_handler_t	mpt_cam_attach;
164 static mpt_enable_handler_t	mpt_cam_enable;
165 static mpt_ready_handler_t	mpt_cam_ready;
166 static mpt_event_handler_t	mpt_cam_event;
167 static mpt_reset_handler_t	mpt_cam_ioc_reset;
168 static mpt_detach_handler_t	mpt_cam_detach;
169 
170 static struct mpt_personality mpt_cam_personality =
171 {
172 	.name		= "mpt_cam",
173 	.probe		= mpt_cam_probe,
174 	.attach		= mpt_cam_attach,
175 	.enable		= mpt_cam_enable,
176 	.ready		= mpt_cam_ready,
177 	.event		= mpt_cam_event,
178 	.reset		= mpt_cam_ioc_reset,
179 	.detach		= mpt_cam_detach,
180 };
181 
182 DECLARE_MPT_PERSONALITY(mpt_cam, SI_ORDER_SECOND);
183 MODULE_DEPEND(mpt_cam, cam, 1, 1, 1);
184 
185 int mpt_enable_sata_wc = -1;
186 TUNABLE_INT("hw.mpt.enable_sata_wc", &mpt_enable_sata_wc);
187 
188 static int
mpt_cam_probe(struct mpt_softc * mpt)189 mpt_cam_probe(struct mpt_softc *mpt)
190 {
191 	int role;
192 
193 	/*
194 	 * Only attach to nodes that support the initiator or target role
195 	 * (or want to) or have RAID physical devices that need CAM pass-thru
196 	 * support.
197 	 */
198 	if (mpt->do_cfg_role) {
199 		role = mpt->cfg_role;
200 	} else {
201 		role = mpt->role;
202 	}
203 	if ((role & (MPT_ROLE_TARGET|MPT_ROLE_INITIATOR)) != 0 ||
204 	    (mpt->ioc_page2 != NULL && mpt->ioc_page2->MaxPhysDisks != 0)) {
205 		return (0);
206 	}
207 	return (ENODEV);
208 }
209 
210 static int
mpt_cam_attach(struct mpt_softc * mpt)211 mpt_cam_attach(struct mpt_softc *mpt)
212 {
213 	struct cam_devq *devq;
214 	mpt_handler_t	 handler;
215 	int		 maxq;
216 	int		 error;
217 
218 	MPT_LOCK(mpt);
219 	TAILQ_INIT(&mpt->request_timeout_list);
220 	maxq = (mpt->ioc_facts.GlobalCredits < MPT_MAX_REQUESTS(mpt))?
221 	    mpt->ioc_facts.GlobalCredits : MPT_MAX_REQUESTS(mpt);
222 
223 	handler.reply_handler = mpt_scsi_reply_handler;
224 	error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
225 				     &scsi_io_handler_id);
226 	if (error != 0) {
227 		MPT_UNLOCK(mpt);
228 		goto cleanup;
229 	}
230 
231 	handler.reply_handler = mpt_scsi_tmf_reply_handler;
232 	error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
233 				     &scsi_tmf_handler_id);
234 	if (error != 0) {
235 		MPT_UNLOCK(mpt);
236 		goto cleanup;
237 	}
238 
239 	/*
240 	 * If we're fibre channel and could support target mode, we register
241 	 * an ELS reply handler and give it resources.
242 	 */
243 	if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET) != 0) {
244 		handler.reply_handler = mpt_fc_els_reply_handler;
245 		error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
246 		    &fc_els_handler_id);
247 		if (error != 0) {
248 			MPT_UNLOCK(mpt);
249 			goto cleanup;
250 		}
251 		if (mpt_add_els_buffers(mpt) == FALSE) {
252 			error = ENOMEM;
253 			MPT_UNLOCK(mpt);
254 			goto cleanup;
255 		}
256 		maxq -= mpt->els_cmds_allocated;
257 	}
258 
259 	/*
260 	 * If we support target mode, we register a reply handler for it,
261 	 * but don't add command resources until we actually enable target
262 	 * mode.
263 	 */
264 	if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET) != 0) {
265 		handler.reply_handler = mpt_scsi_tgt_reply_handler;
266 		error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
267 		    &mpt->scsi_tgt_handler_id);
268 		if (error != 0) {
269 			MPT_UNLOCK(mpt);
270 			goto cleanup;
271 		}
272 	}
273 
274 	if (mpt->is_sas) {
275 		handler.reply_handler = mpt_sata_pass_reply_handler;
276 		error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
277 		    &sata_pass_handler_id);
278 		if (error != 0) {
279 			MPT_UNLOCK(mpt);
280 			goto cleanup;
281 		}
282 	}
283 
284 	/*
285 	 * We keep one request reserved for timeout TMF requests.
286 	 */
287 	mpt->tmf_req = mpt_get_request(mpt, FALSE);
288 	if (mpt->tmf_req == NULL) {
289 		mpt_prt(mpt, "Unable to allocate dedicated TMF request!\n");
290 		error = ENOMEM;
291 		MPT_UNLOCK(mpt);
292 		goto cleanup;
293 	}
294 
295 	/*
296 	 * Mark the request as free even though not on the free list.
297 	 * There is only one TMF request allowed to be outstanding at
298 	 * a time and the TMF routines perform their own allocation
299 	 * tracking using the standard state flags.
300 	 */
301 	mpt->tmf_req->state = REQ_STATE_FREE;
302 	maxq--;
303 
304 	/*
305 	 * The rest of this is CAM foo, for which we need to drop our lock
306 	 */
307 	MPT_UNLOCK(mpt);
308 
309 	if (mpt_spawn_recovery_thread(mpt) != 0) {
310 		mpt_prt(mpt, "Unable to spawn recovery thread!\n");
311 		error = ENOMEM;
312 		goto cleanup;
313 	}
314 
315 	/*
316 	 * Create the device queue for our SIM(s).
317 	 */
318 	devq = cam_simq_alloc(maxq);
319 	if (devq == NULL) {
320 		mpt_prt(mpt, "Unable to allocate CAM SIMQ!\n");
321 		error = ENOMEM;
322 		goto cleanup;
323 	}
324 
325 	/*
326 	 * Construct our SIM entry.
327 	 */
328 	mpt->sim =
329 	    mpt_sim_alloc(mpt_action, mpt_poll, "mpt", mpt, 1, maxq, devq);
330 	if (mpt->sim == NULL) {
331 		mpt_prt(mpt, "Unable to allocate CAM SIM!\n");
332 		cam_simq_free(devq);
333 		error = ENOMEM;
334 		goto cleanup;
335 	}
336 
337 	/*
338 	 * Register exactly this bus.
339 	 */
340 	MPT_LOCK(mpt);
341 	if (xpt_bus_register(mpt->sim, mpt->dev, 0) != CAM_SUCCESS) {
342 		mpt_prt(mpt, "Bus registration Failed!\n");
343 		error = ENOMEM;
344 		MPT_UNLOCK(mpt);
345 		goto cleanup;
346 	}
347 
348 	if (xpt_create_path(&mpt->path, NULL, cam_sim_path(mpt->sim),
349 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
350 		mpt_prt(mpt, "Unable to allocate Path!\n");
351 		error = ENOMEM;
352 		MPT_UNLOCK(mpt);
353 		goto cleanup;
354 	}
355 	MPT_UNLOCK(mpt);
356 
357 	/*
358 	 * Only register a second bus for RAID physical
359 	 * devices if the controller supports RAID.
360 	 */
361 	if (mpt->ioc_page2 == NULL || mpt->ioc_page2->MaxPhysDisks == 0) {
362 		return (0);
363 	}
364 
365 	/*
366 	 * Create a "bus" to export all hidden disks to CAM.
367 	 */
368 	mpt->phydisk_sim =
369 	    mpt_sim_alloc(mpt_action, mpt_poll, "mpt", mpt, 1, maxq, devq);
370 	if (mpt->phydisk_sim == NULL) {
371 		mpt_prt(mpt, "Unable to allocate Physical Disk CAM SIM!\n");
372 		error = ENOMEM;
373 		goto cleanup;
374 	}
375 
376 	/*
377 	 * Register this bus.
378 	 */
379 	MPT_LOCK(mpt);
380 	if (xpt_bus_register(mpt->phydisk_sim, mpt->dev, 1) !=
381 	    CAM_SUCCESS) {
382 		mpt_prt(mpt, "Physical Disk Bus registration Failed!\n");
383 		error = ENOMEM;
384 		MPT_UNLOCK(mpt);
385 		goto cleanup;
386 	}
387 
388 	if (xpt_create_path(&mpt->phydisk_path, NULL,
389 	    cam_sim_path(mpt->phydisk_sim),
390 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
391 		mpt_prt(mpt, "Unable to allocate Physical Disk Path!\n");
392 		error = ENOMEM;
393 		MPT_UNLOCK(mpt);
394 		goto cleanup;
395 	}
396 	MPT_UNLOCK(mpt);
397 	mpt_lprt(mpt, MPT_PRT_DEBUG, "attached cam\n");
398 	return (0);
399 
400 cleanup:
401 	mpt_cam_detach(mpt);
402 	return (error);
403 }
404 
405 /*
406  * Read FC configuration information
407  */
408 static int
mpt_read_config_info_fc(struct mpt_softc * mpt)409 mpt_read_config_info_fc(struct mpt_softc *mpt)
410 {
411 	struct sysctl_ctx_list *ctx;
412 	struct sysctl_oid *tree;
413 	char *topology = NULL;
414 	int rv;
415 
416 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_FC_PORT, 0,
417 	    0, &mpt->mpt_fcport_page0.Header, FALSE, 5000);
418 	if (rv) {
419 		return (-1);
420 	}
421 	mpt_lprt(mpt, MPT_PRT_DEBUG, "FC Port Page 0 Header: %x %x %x %x\n",
422 		 mpt->mpt_fcport_page0.Header.PageVersion,
423 		 mpt->mpt_fcport_page0.Header.PageLength,
424 		 mpt->mpt_fcport_page0.Header.PageNumber,
425 		 mpt->mpt_fcport_page0.Header.PageType);
426 
427 	rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_fcport_page0.Header,
428 	    sizeof(mpt->mpt_fcport_page0), FALSE, 5000);
429 	if (rv) {
430 		mpt_prt(mpt, "failed to read FC Port Page 0\n");
431 		return (-1);
432 	}
433 	mpt2host_config_page_fc_port_0(&mpt->mpt_fcport_page0);
434 
435 	switch (mpt->mpt_fcport_page0.CurrentSpeed) {
436 	case MPI_FCPORTPAGE0_CURRENT_SPEED_1GBIT:
437 		mpt->mpt_fcport_speed = 1;
438 		break;
439 	case MPI_FCPORTPAGE0_CURRENT_SPEED_2GBIT:
440 		mpt->mpt_fcport_speed = 2;
441 		break;
442 	case MPI_FCPORTPAGE0_CURRENT_SPEED_10GBIT:
443 		mpt->mpt_fcport_speed = 10;
444 		break;
445 	case MPI_FCPORTPAGE0_CURRENT_SPEED_4GBIT:
446 		mpt->mpt_fcport_speed = 4;
447 		break;
448 	default:
449 		mpt->mpt_fcport_speed = 0;
450 		break;
451 	}
452 
453 	switch (mpt->mpt_fcport_page0.Flags &
454 	    MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK) {
455 	case MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT:
456 		mpt->mpt_fcport_speed = 0;
457 		topology = "<NO LOOP>";
458 		break;
459 	case MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT:
460 		topology = "N-Port";
461 		break;
462 	case MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP:
463 		topology = "NL-Port";
464 		break;
465 	case MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT:
466 		topology = "F-Port";
467 		break;
468 	case MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP:
469 		topology = "FL-Port";
470 		break;
471 	default:
472 		mpt->mpt_fcport_speed = 0;
473 		topology = "?";
474 		break;
475 	}
476 
477 	mpt->scinfo.fc.wwnn = ((uint64_t)mpt->mpt_fcport_page0.WWNN.High << 32)
478 	    | mpt->mpt_fcport_page0.WWNN.Low;
479 	mpt->scinfo.fc.wwpn = ((uint64_t)mpt->mpt_fcport_page0.WWPN.High << 32)
480 	    | mpt->mpt_fcport_page0.WWPN.Low;
481 	mpt->scinfo.fc.portid = mpt->mpt_fcport_page0.PortIdentifier;
482 
483 	mpt_lprt(mpt, MPT_PRT_INFO,
484 	    "FC Port Page 0: Topology <%s> WWNN 0x%16jx WWPN 0x%16jx "
485 	    "Speed %u-Gbit\n", topology,
486 	    (uintmax_t)mpt->scinfo.fc.wwnn, (uintmax_t)mpt->scinfo.fc.wwpn,
487 	    mpt->mpt_fcport_speed);
488 	MPT_UNLOCK(mpt);
489 	ctx = device_get_sysctl_ctx(mpt->dev);
490 	tree = device_get_sysctl_tree(mpt->dev);
491 
492 	SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
493 	    "wwnn", CTLFLAG_RD, &mpt->scinfo.fc.wwnn,
494 	    "World Wide Node Name");
495 
496 	SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
497 	     "wwpn", CTLFLAG_RD, &mpt->scinfo.fc.wwpn,
498 	     "World Wide Port Name");
499 
500 	MPT_LOCK(mpt);
501 	return (0);
502 }
503 
504 /*
505  * Set FC configuration information.
506  */
507 static int
mpt_set_initial_config_fc(struct mpt_softc * mpt)508 mpt_set_initial_config_fc(struct mpt_softc *mpt)
509 {
510 	CONFIG_PAGE_FC_PORT_1 fc;
511 	U32 fl;
512 	int r, doit = 0;
513 	int role;
514 
515 	r = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_FC_PORT, 1, 0,
516 	    &fc.Header, FALSE, 5000);
517 	if (r) {
518 		mpt_prt(mpt, "failed to read FC page 1 header\n");
519 		return (mpt_fc_reset_link(mpt, 1));
520 	}
521 
522 	r = mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_NVRAM, 0,
523 	    &fc.Header, sizeof (fc), FALSE, 5000);
524 	if (r) {
525 		mpt_prt(mpt, "failed to read FC page 1\n");
526 		return (mpt_fc_reset_link(mpt, 1));
527 	}
528 	mpt2host_config_page_fc_port_1(&fc);
529 
530 	/*
531 	 * Check our flags to make sure we support the role we want.
532 	 */
533 	doit = 0;
534 	role = 0;
535 	fl = fc.Flags;
536 
537 	if (fl & MPI_FCPORTPAGE1_FLAGS_PROT_FCP_INIT) {
538 		role |= MPT_ROLE_INITIATOR;
539 	}
540 	if (fl & MPI_FCPORTPAGE1_FLAGS_PROT_FCP_TARG) {
541 		role |= MPT_ROLE_TARGET;
542 	}
543 
544 	fl &= ~MPI_FCPORTPAGE1_FLAGS_PROT_MASK;
545 
546 	if (mpt->do_cfg_role == 0) {
547 		role = mpt->cfg_role;
548 	} else {
549 		mpt->do_cfg_role = 0;
550 	}
551 
552 	if (role != mpt->cfg_role) {
553 		if (mpt->cfg_role & MPT_ROLE_INITIATOR) {
554 			if ((role & MPT_ROLE_INITIATOR) == 0) {
555 				mpt_prt(mpt, "adding initiator role\n");
556 				fl |= MPI_FCPORTPAGE1_FLAGS_PROT_FCP_INIT;
557 				doit++;
558 			} else {
559 				mpt_prt(mpt, "keeping initiator role\n");
560 			}
561 		} else if (role & MPT_ROLE_INITIATOR) {
562 			mpt_prt(mpt, "removing initiator role\n");
563 			doit++;
564 		}
565 		if (mpt->cfg_role & MPT_ROLE_TARGET) {
566 			if ((role & MPT_ROLE_TARGET) == 0) {
567 				mpt_prt(mpt, "adding target role\n");
568 				fl |= MPI_FCPORTPAGE1_FLAGS_PROT_FCP_TARG;
569 				doit++;
570 			} else {
571 				mpt_prt(mpt, "keeping target role\n");
572 			}
573 		} else if (role & MPT_ROLE_TARGET) {
574 			mpt_prt(mpt, "removing target role\n");
575 			doit++;
576 		}
577 		mpt->role = mpt->cfg_role;
578 	}
579 
580 	if (fl & MPI_FCPORTPAGE1_FLAGS_PROT_FCP_TARG) {
581 		if ((fl & MPI_FCPORTPAGE1_FLAGS_TARGET_MODE_OXID) == 0) {
582 			mpt_prt(mpt, "adding OXID option\n");
583 			fl |= MPI_FCPORTPAGE1_FLAGS_TARGET_MODE_OXID;
584 			doit++;
585 		}
586 	}
587 
588 	if (doit) {
589 		fc.Flags = fl;
590 		host2mpt_config_page_fc_port_1(&fc);
591 		r = mpt_write_cfg_page(mpt,
592 		    MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM, 0, &fc.Header,
593 		    sizeof(fc), FALSE, 5000);
594 		if (r != 0) {
595 			mpt_prt(mpt, "failed to update NVRAM with changes\n");
596 			return (0);
597 		}
598 		mpt_prt(mpt, "NOTE: NVRAM changes will not take "
599 		    "effect until next reboot or IOC reset\n");
600 	}
601 	return (0);
602 }
603 
604 static int
mptsas_sas_io_unit_pg0(struct mpt_softc * mpt,struct mptsas_portinfo * portinfo)605 mptsas_sas_io_unit_pg0(struct mpt_softc *mpt, struct mptsas_portinfo *portinfo)
606 {
607 	ConfigExtendedPageHeader_t hdr;
608 	struct mptsas_phyinfo *phyinfo;
609 	SasIOUnitPage0_t *buffer;
610 	int error, len, i;
611 
612 	error = mpt_read_extcfg_header(mpt, MPI_SASIOUNITPAGE0_PAGEVERSION,
613 				       0, 0, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
614 				       &hdr, 0, 10000);
615 	if (error)
616 		goto out;
617 	if (hdr.ExtPageLength == 0) {
618 		error = ENXIO;
619 		goto out;
620 	}
621 
622 	len = hdr.ExtPageLength * 4;
623 	buffer = malloc(len, M_DEVBUF, M_NOWAIT|M_ZERO);
624 	if (buffer == NULL) {
625 		error = ENOMEM;
626 		goto out;
627 	}
628 
629 	error = mpt_read_extcfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
630 				     0, &hdr, buffer, len, 0, 10000);
631 	if (error) {
632 		free(buffer, M_DEVBUF);
633 		goto out;
634 	}
635 
636 	portinfo->num_phys = buffer->NumPhys;
637 	portinfo->phy_info = malloc(sizeof(*portinfo->phy_info) *
638 	    portinfo->num_phys, M_DEVBUF, M_NOWAIT|M_ZERO);
639 	if (portinfo->phy_info == NULL) {
640 		free(buffer, M_DEVBUF);
641 		error = ENOMEM;
642 		goto out;
643 	}
644 
645 	for (i = 0; i < portinfo->num_phys; i++) {
646 		phyinfo = &portinfo->phy_info[i];
647 		phyinfo->phy_num = i;
648 		phyinfo->port_id = buffer->PhyData[i].Port;
649 		phyinfo->negotiated_link_rate =
650 		    buffer->PhyData[i].NegotiatedLinkRate;
651 		phyinfo->handle =
652 		    le16toh(buffer->PhyData[i].ControllerDevHandle);
653 	}
654 
655 	free(buffer, M_DEVBUF);
656 out:
657 	return (error);
658 }
659 
660 static int
mptsas_sas_phy_pg0(struct mpt_softc * mpt,struct mptsas_phyinfo * phy_info,uint32_t form,uint32_t form_specific)661 mptsas_sas_phy_pg0(struct mpt_softc *mpt, struct mptsas_phyinfo *phy_info,
662 	uint32_t form, uint32_t form_specific)
663 {
664 	ConfigExtendedPageHeader_t hdr;
665 	SasPhyPage0_t *buffer;
666 	int error;
667 
668 	error = mpt_read_extcfg_header(mpt, MPI_SASPHY0_PAGEVERSION, 0, 0,
669 				       MPI_CONFIG_EXTPAGETYPE_SAS_PHY, &hdr,
670 				       0, 10000);
671 	if (error)
672 		goto out;
673 	if (hdr.ExtPageLength == 0) {
674 		error = ENXIO;
675 		goto out;
676 	}
677 
678 	buffer = malloc(sizeof(SasPhyPage0_t), M_DEVBUF, M_NOWAIT|M_ZERO);
679 	if (buffer == NULL) {
680 		error = ENOMEM;
681 		goto out;
682 	}
683 
684 	error = mpt_read_extcfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
685 				     form + form_specific, &hdr, buffer,
686 				     sizeof(SasPhyPage0_t), 0, 10000);
687 	if (error) {
688 		free(buffer, M_DEVBUF);
689 		goto out;
690 	}
691 
692 	phy_info->hw_link_rate = buffer->HwLinkRate;
693 	phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
694 	phy_info->identify.dev_handle = le16toh(buffer->OwnerDevHandle);
695 	phy_info->attached.dev_handle = le16toh(buffer->AttachedDevHandle);
696 
697 	free(buffer, M_DEVBUF);
698 out:
699 	return (error);
700 }
701 
702 static int
mptsas_sas_device_pg0(struct mpt_softc * mpt,struct mptsas_devinfo * device_info,uint32_t form,uint32_t form_specific)703 mptsas_sas_device_pg0(struct mpt_softc *mpt, struct mptsas_devinfo *device_info,
704 	uint32_t form, uint32_t form_specific)
705 {
706 	ConfigExtendedPageHeader_t hdr;
707 	SasDevicePage0_t *buffer;
708 	uint64_t sas_address;
709 	int error = 0;
710 
711 	bzero(device_info, sizeof(*device_info));
712 	error = mpt_read_extcfg_header(mpt, MPI_SASDEVICE0_PAGEVERSION, 0, 0,
713 				       MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE,
714 				       &hdr, 0, 10000);
715 	if (error)
716 		goto out;
717 	if (hdr.ExtPageLength == 0) {
718 		error = ENXIO;
719 		goto out;
720 	}
721 
722 	buffer = malloc(sizeof(SasDevicePage0_t), M_DEVBUF, M_NOWAIT|M_ZERO);
723 	if (buffer == NULL) {
724 		error = ENOMEM;
725 		goto out;
726 	}
727 
728 	error = mpt_read_extcfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
729 				     form + form_specific, &hdr, buffer,
730 				     sizeof(SasDevicePage0_t), 0, 10000);
731 	if (error) {
732 		free(buffer, M_DEVBUF);
733 		goto out;
734 	}
735 
736 	device_info->dev_handle = le16toh(buffer->DevHandle);
737 	device_info->parent_dev_handle = le16toh(buffer->ParentDevHandle);
738 	device_info->enclosure_handle = le16toh(buffer->EnclosureHandle);
739 	device_info->slot = le16toh(buffer->Slot);
740 	device_info->phy_num = buffer->PhyNum;
741 	device_info->physical_port = buffer->PhysicalPort;
742 	device_info->target_id = buffer->TargetID;
743 	device_info->bus = buffer->Bus;
744 	bcopy(&buffer->SASAddress, &sas_address, sizeof(uint64_t));
745 	device_info->sas_address = le64toh(sas_address);
746 	device_info->device_info = le32toh(buffer->DeviceInfo);
747 
748 	free(buffer, M_DEVBUF);
749 out:
750 	return (error);
751 }
752 
753 /*
754  * Read SAS configuration information. Nothing to do yet.
755  */
756 static int
mpt_read_config_info_sas(struct mpt_softc * mpt)757 mpt_read_config_info_sas(struct mpt_softc *mpt)
758 {
759 	struct mptsas_portinfo *portinfo;
760 	struct mptsas_phyinfo *phyinfo;
761 	int error, i;
762 
763 	portinfo = malloc(sizeof(*portinfo), M_DEVBUF, M_NOWAIT|M_ZERO);
764 	if (portinfo == NULL)
765 		return (ENOMEM);
766 
767 	error = mptsas_sas_io_unit_pg0(mpt, portinfo);
768 	if (error) {
769 		free(portinfo, M_DEVBUF);
770 		return (0);
771 	}
772 
773 	for (i = 0; i < portinfo->num_phys; i++) {
774 		phyinfo = &portinfo->phy_info[i];
775 		error = mptsas_sas_phy_pg0(mpt, phyinfo,
776 		    (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
777 		    MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
778 		if (error)
779 			break;
780 		error = mptsas_sas_device_pg0(mpt, &phyinfo->identify,
781 		    (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
782 		    MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
783 		    phyinfo->handle);
784 		if (error)
785 			break;
786 		phyinfo->identify.phy_num = phyinfo->phy_num = i;
787 		if (phyinfo->attached.dev_handle)
788 			error = mptsas_sas_device_pg0(mpt,
789 			    &phyinfo->attached,
790 			    (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
791 			    MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
792 			    phyinfo->attached.dev_handle);
793 		if (error)
794 			break;
795 	}
796 	mpt->sas_portinfo = portinfo;
797 	return (0);
798 }
799 
800 static void
mptsas_set_sata_wc(struct mpt_softc * mpt,struct mptsas_devinfo * devinfo,int enabled)801 mptsas_set_sata_wc(struct mpt_softc *mpt, struct mptsas_devinfo *devinfo,
802 	int enabled)
803 {
804 	SataPassthroughRequest_t	*pass;
805 	request_t *req;
806 	int error, status;
807 
808 	req = mpt_get_request(mpt, 0);
809 	if (req == NULL)
810 		return;
811 
812 	pass = req->req_vbuf;
813 	bzero(pass, sizeof(SataPassthroughRequest_t));
814 	pass->Function = MPI_FUNCTION_SATA_PASSTHROUGH;
815 	pass->TargetID = devinfo->target_id;
816 	pass->Bus = devinfo->bus;
817 	pass->PassthroughFlags = 0;
818 	pass->ConnectionRate = MPI_SATA_PT_REQ_CONNECT_RATE_NEGOTIATED;
819 	pass->DataLength = 0;
820 	pass->MsgContext = htole32(req->index | sata_pass_handler_id);
821 	pass->CommandFIS[0] = 0x27;
822 	pass->CommandFIS[1] = 0x80;
823 	pass->CommandFIS[2] = 0xef;
824 	pass->CommandFIS[3] = (enabled) ? 0x02 : 0x82;
825 	pass->CommandFIS[7] = 0x40;
826 	pass->CommandFIS[15] = 0x08;
827 
828 	mpt_check_doorbell(mpt);
829 	mpt_send_cmd(mpt, req);
830 	error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 0,
831 			     10 * 1000);
832 	if (error) {
833 		mpt_free_request(mpt, req);
834 		printf("error %d sending passthrough\n", error);
835 		return;
836 	}
837 
838 	status = le16toh(req->IOCStatus);
839 	if (status != MPI_IOCSTATUS_SUCCESS) {
840 		mpt_free_request(mpt, req);
841 		printf("IOCSTATUS %d\n", status);
842 		return;
843 	}
844 
845 	mpt_free_request(mpt, req);
846 }
847 
848 /*
849  * Set SAS configuration information. Nothing to do yet.
850  */
851 static int
mpt_set_initial_config_sas(struct mpt_softc * mpt)852 mpt_set_initial_config_sas(struct mpt_softc *mpt)
853 {
854 	struct mptsas_phyinfo *phyinfo;
855 	int i;
856 
857 	if ((mpt_enable_sata_wc != -1) && (mpt->sas_portinfo != NULL)) {
858 		for (i = 0; i < mpt->sas_portinfo->num_phys; i++) {
859 			phyinfo = &mpt->sas_portinfo->phy_info[i];
860 			if (phyinfo->attached.dev_handle == 0)
861 				continue;
862 			if ((phyinfo->attached.device_info &
863 			    MPI_SAS_DEVICE_INFO_SATA_DEVICE) == 0)
864 				continue;
865 			if (bootverbose)
866 				device_printf(mpt->dev,
867 				    "%sabling SATA WC on phy %d\n",
868 				    (mpt_enable_sata_wc) ? "En" : "Dis", i);
869 			mptsas_set_sata_wc(mpt, &phyinfo->attached,
870 					   mpt_enable_sata_wc);
871 		}
872 	}
873 
874 	return (0);
875 }
876 
877 static int
mpt_sata_pass_reply_handler(struct mpt_softc * mpt,request_t * req,uint32_t reply_desc,MSG_DEFAULT_REPLY * reply_frame)878 mpt_sata_pass_reply_handler(struct mpt_softc *mpt, request_t *req,
879  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
880 {
881 
882 	if (req != NULL) {
883 		if (reply_frame != NULL) {
884 			req->IOCStatus = le16toh(reply_frame->IOCStatus);
885 		}
886 		req->state &= ~REQ_STATE_QUEUED;
887 		req->state |= REQ_STATE_DONE;
888 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
889 		if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
890 			wakeup(req);
891 		} else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
892 			/*
893 			 * Whew- we can free this request (late completion)
894 			 */
895 			mpt_free_request(mpt, req);
896 		}
897 	}
898 
899 	return (TRUE);
900 }
901 
902 /*
903  * Read SCSI configuration information
904  */
905 static int
mpt_read_config_info_spi(struct mpt_softc * mpt)906 mpt_read_config_info_spi(struct mpt_softc *mpt)
907 {
908 	int rv, i;
909 
910 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 0, 0,
911 	    &mpt->mpt_port_page0.Header, FALSE, 5000);
912 	if (rv) {
913 		return (-1);
914 	}
915 	mpt_lprt(mpt, MPT_PRT_DEBUG, "SPI Port Page 0 Header: %x %x %x %x\n",
916 	    mpt->mpt_port_page0.Header.PageVersion,
917 	    mpt->mpt_port_page0.Header.PageLength,
918 	    mpt->mpt_port_page0.Header.PageNumber,
919 	    mpt->mpt_port_page0.Header.PageType);
920 
921 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 1, 0,
922 	    &mpt->mpt_port_page1.Header, FALSE, 5000);
923 	if (rv) {
924 		return (-1);
925 	}
926 	mpt_lprt(mpt, MPT_PRT_DEBUG, "SPI Port Page 1 Header: %x %x %x %x\n",
927 	    mpt->mpt_port_page1.Header.PageVersion,
928 	    mpt->mpt_port_page1.Header.PageLength,
929 	    mpt->mpt_port_page1.Header.PageNumber,
930 	    mpt->mpt_port_page1.Header.PageType);
931 
932 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 2, 0,
933 	    &mpt->mpt_port_page2.Header, FALSE, 5000);
934 	if (rv) {
935 		return (-1);
936 	}
937 	mpt_lprt(mpt, MPT_PRT_DEBUG, "SPI Port Page 2 Header: %x %x %x %x\n",
938 	    mpt->mpt_port_page2.Header.PageVersion,
939 	    mpt->mpt_port_page2.Header.PageLength,
940 	    mpt->mpt_port_page2.Header.PageNumber,
941 	    mpt->mpt_port_page2.Header.PageType);
942 
943 	for (i = 0; i < 16; i++) {
944 		rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_DEVICE,
945 		    0, i, &mpt->mpt_dev_page0[i].Header, FALSE, 5000);
946 		if (rv) {
947 			return (-1);
948 		}
949 		mpt_lprt(mpt, MPT_PRT_DEBUG,
950 		    "SPI Target %d Device Page 0 Header: %x %x %x %x\n", i,
951 		    mpt->mpt_dev_page0[i].Header.PageVersion,
952 		    mpt->mpt_dev_page0[i].Header.PageLength,
953 		    mpt->mpt_dev_page0[i].Header.PageNumber,
954 		    mpt->mpt_dev_page0[i].Header.PageType);
955 
956 		rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_DEVICE,
957 		    1, i, &mpt->mpt_dev_page1[i].Header, FALSE, 5000);
958 		if (rv) {
959 			return (-1);
960 		}
961 		mpt_lprt(mpt, MPT_PRT_DEBUG,
962 		    "SPI Target %d Device Page 1 Header: %x %x %x %x\n", i,
963 		    mpt->mpt_dev_page1[i].Header.PageVersion,
964 		    mpt->mpt_dev_page1[i].Header.PageLength,
965 		    mpt->mpt_dev_page1[i].Header.PageNumber,
966 		    mpt->mpt_dev_page1[i].Header.PageType);
967 	}
968 
969 	/*
970 	 * At this point, we don't *have* to fail. As long as we have
971 	 * valid config header information, we can (barely) lurch
972 	 * along.
973 	 */
974 
975 	rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_port_page0.Header,
976 	    sizeof(mpt->mpt_port_page0), FALSE, 5000);
977 	if (rv) {
978 		mpt_prt(mpt, "failed to read SPI Port Page 0\n");
979 	} else {
980 		mpt2host_config_page_scsi_port_0(&mpt->mpt_port_page0);
981 		mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
982 		    "SPI Port Page 0: Capabilities %x PhysicalInterface %x\n",
983 		    mpt->mpt_port_page0.Capabilities,
984 		    mpt->mpt_port_page0.PhysicalInterface);
985 	}
986 
987 	rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_port_page1.Header,
988 	    sizeof(mpt->mpt_port_page1), FALSE, 5000);
989 	if (rv) {
990 		mpt_prt(mpt, "failed to read SPI Port Page 1\n");
991 	} else {
992 		mpt2host_config_page_scsi_port_1(&mpt->mpt_port_page1);
993 		mpt_lprt(mpt, MPT_PRT_DEBUG,
994 		    "SPI Port Page 1: Configuration %x OnBusTimerValue %x\n",
995 		    mpt->mpt_port_page1.Configuration,
996 		    mpt->mpt_port_page1.OnBusTimerValue);
997 	}
998 
999 	rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_port_page2.Header,
1000 	    sizeof(mpt->mpt_port_page2), FALSE, 5000);
1001 	if (rv) {
1002 		mpt_prt(mpt, "failed to read SPI Port Page 2\n");
1003 	} else {
1004 		mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1005 		    "Port Page 2: Flags %x Settings %x\n",
1006 		    mpt->mpt_port_page2.PortFlags,
1007 		    mpt->mpt_port_page2.PortSettings);
1008 		mpt2host_config_page_scsi_port_2(&mpt->mpt_port_page2);
1009 		for (i = 0; i < 16; i++) {
1010 			mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1011 		  	    " Port Page 2 Tgt %d: timo %x SF %x Flags %x\n",
1012 			    i, mpt->mpt_port_page2.DeviceSettings[i].Timeout,
1013 			    mpt->mpt_port_page2.DeviceSettings[i].SyncFactor,
1014 			    mpt->mpt_port_page2.DeviceSettings[i].DeviceFlags);
1015 		}
1016 	}
1017 
1018 	for (i = 0; i < 16; i++) {
1019 		rv = mpt_read_cur_cfg_page(mpt, i,
1020 		    &mpt->mpt_dev_page0[i].Header, sizeof(*mpt->mpt_dev_page0),
1021 		    FALSE, 5000);
1022 		if (rv) {
1023 			mpt_prt(mpt,
1024 			    "cannot read SPI Target %d Device Page 0\n", i);
1025 			continue;
1026 		}
1027 		mpt2host_config_page_scsi_device_0(&mpt->mpt_dev_page0[i]);
1028 		mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1029 		    "target %d page 0: Negotiated Params %x Information %x\n",
1030 		    i, mpt->mpt_dev_page0[i].NegotiatedParameters,
1031 		    mpt->mpt_dev_page0[i].Information);
1032 
1033 		rv = mpt_read_cur_cfg_page(mpt, i,
1034 		    &mpt->mpt_dev_page1[i].Header, sizeof(*mpt->mpt_dev_page1),
1035 		    FALSE, 5000);
1036 		if (rv) {
1037 			mpt_prt(mpt,
1038 			    "cannot read SPI Target %d Device Page 1\n", i);
1039 			continue;
1040 		}
1041 		mpt2host_config_page_scsi_device_1(&mpt->mpt_dev_page1[i]);
1042 		mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1043 		    "target %d page 1: Requested Params %x Configuration %x\n",
1044 		    i, mpt->mpt_dev_page1[i].RequestedParameters,
1045 		    mpt->mpt_dev_page1[i].Configuration);
1046 	}
1047 	return (0);
1048 }
1049 
1050 /*
1051  * Validate SPI configuration information.
1052  *
1053  * In particular, validate SPI Port Page 1.
1054  */
1055 static int
mpt_set_initial_config_spi(struct mpt_softc * mpt)1056 mpt_set_initial_config_spi(struct mpt_softc *mpt)
1057 {
1058 	int error, i, pp1val;
1059 
1060 	mpt->mpt_disc_enable = 0xff;
1061 	mpt->mpt_tag_enable = 0;
1062 
1063 	pp1val = ((1 << mpt->mpt_ini_id) <<
1064 	    MPI_SCSIPORTPAGE1_CFG_SHIFT_PORT_RESPONSE_ID) | mpt->mpt_ini_id;
1065 	if (mpt->mpt_port_page1.Configuration != pp1val) {
1066 		CONFIG_PAGE_SCSI_PORT_1 tmp;
1067 
1068 		mpt_prt(mpt, "SPI Port Page 1 Config value bad (%x)- should "
1069 		    "be %x\n", mpt->mpt_port_page1.Configuration, pp1val);
1070 		tmp = mpt->mpt_port_page1;
1071 		tmp.Configuration = pp1val;
1072 		host2mpt_config_page_scsi_port_1(&tmp);
1073 		error = mpt_write_cur_cfg_page(mpt, 0,
1074 		    &tmp.Header, sizeof(tmp), FALSE, 5000);
1075 		if (error) {
1076 			return (-1);
1077 		}
1078 		error = mpt_read_cur_cfg_page(mpt, 0,
1079 		    &tmp.Header, sizeof(tmp), FALSE, 5000);
1080 		if (error) {
1081 			return (-1);
1082 		}
1083 		mpt2host_config_page_scsi_port_1(&tmp);
1084 		if (tmp.Configuration != pp1val) {
1085 			mpt_prt(mpt,
1086 			    "failed to reset SPI Port Page 1 Config value\n");
1087 			return (-1);
1088 		}
1089 		mpt->mpt_port_page1 = tmp;
1090 	}
1091 
1092 	/*
1093 	 * The purpose of this exercise is to get
1094 	 * all targets back to async/narrow.
1095 	 *
1096 	 * We skip this step if the BIOS has already negotiated
1097 	 * speeds with the targets.
1098 	 */
1099 	i = mpt->mpt_port_page2.PortSettings &
1100 	    MPI_SCSIPORTPAGE2_PORT_MASK_NEGO_MASTER_SETTINGS;
1101 	if (i == MPI_SCSIPORTPAGE2_PORT_ALL_MASTER_SETTINGS) {
1102 		mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1103 		    "honoring BIOS transfer negotiations\n");
1104 	} else {
1105 		for (i = 0; i < 16; i++) {
1106 			mpt->mpt_dev_page1[i].RequestedParameters = 0;
1107 			mpt->mpt_dev_page1[i].Configuration = 0;
1108 			(void) mpt_update_spi_config(mpt, i);
1109 		}
1110 	}
1111 	return (0);
1112 }
1113 
1114 static int
mpt_cam_enable(struct mpt_softc * mpt)1115 mpt_cam_enable(struct mpt_softc *mpt)
1116 {
1117 	int error;
1118 
1119 	MPT_LOCK(mpt);
1120 
1121 	error = EIO;
1122 	if (mpt->is_fc) {
1123 		if (mpt_read_config_info_fc(mpt)) {
1124 			goto out;
1125 		}
1126 		if (mpt_set_initial_config_fc(mpt)) {
1127 			goto out;
1128 		}
1129 	} else if (mpt->is_sas) {
1130 		if (mpt_read_config_info_sas(mpt)) {
1131 			goto out;
1132 		}
1133 		if (mpt_set_initial_config_sas(mpt)) {
1134 			goto out;
1135 		}
1136 	} else if (mpt->is_spi) {
1137 		if (mpt_read_config_info_spi(mpt)) {
1138 			goto out;
1139 		}
1140 		if (mpt_set_initial_config_spi(mpt)) {
1141 			goto out;
1142 		}
1143 	}
1144 	error = 0;
1145 
1146 out:
1147 	MPT_UNLOCK(mpt);
1148 	return (error);
1149 }
1150 
1151 static void
mpt_cam_ready(struct mpt_softc * mpt)1152 mpt_cam_ready(struct mpt_softc *mpt)
1153 {
1154 
1155 	/*
1156 	 * If we're in target mode, hang out resources now
1157 	 * so we don't cause the world to hang talking to us.
1158 	 */
1159 	if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET)) {
1160 		/*
1161 		 * Try to add some target command resources
1162 		 */
1163 		MPT_LOCK(mpt);
1164 		if (mpt_add_target_commands(mpt) == FALSE) {
1165 			mpt_prt(mpt, "failed to add target commands\n");
1166 		}
1167 		MPT_UNLOCK(mpt);
1168 	}
1169 	mpt->ready = 1;
1170 }
1171 
1172 static void
mpt_cam_detach(struct mpt_softc * mpt)1173 mpt_cam_detach(struct mpt_softc *mpt)
1174 {
1175 	mpt_handler_t handler;
1176 
1177 	MPT_LOCK(mpt);
1178 	mpt->ready = 0;
1179 	mpt_terminate_recovery_thread(mpt);
1180 
1181 	handler.reply_handler = mpt_scsi_reply_handler;
1182 	mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1183 			       scsi_io_handler_id);
1184 	handler.reply_handler = mpt_scsi_tmf_reply_handler;
1185 	mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1186 			       scsi_tmf_handler_id);
1187 	handler.reply_handler = mpt_fc_els_reply_handler;
1188 	mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1189 			       fc_els_handler_id);
1190 	handler.reply_handler = mpt_scsi_tgt_reply_handler;
1191 	mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1192 			       mpt->scsi_tgt_handler_id);
1193 	handler.reply_handler = mpt_sata_pass_reply_handler;
1194 	mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1195 			       sata_pass_handler_id);
1196 
1197 	if (mpt->tmf_req != NULL) {
1198 		mpt->tmf_req->state = REQ_STATE_ALLOCATED;
1199 		mpt_free_request(mpt, mpt->tmf_req);
1200 		mpt->tmf_req = NULL;
1201 	}
1202 	if (mpt->sas_portinfo != NULL) {
1203 		free(mpt->sas_portinfo, M_DEVBUF);
1204 		mpt->sas_portinfo = NULL;
1205 	}
1206 
1207 	if (mpt->sim != NULL) {
1208 		xpt_free_path(mpt->path);
1209 		xpt_bus_deregister(cam_sim_path(mpt->sim));
1210 		cam_sim_free(mpt->sim, TRUE);
1211 		mpt->sim = NULL;
1212 	}
1213 
1214 	if (mpt->phydisk_sim != NULL) {
1215 		xpt_free_path(mpt->phydisk_path);
1216 		xpt_bus_deregister(cam_sim_path(mpt->phydisk_sim));
1217 		cam_sim_free(mpt->phydisk_sim, TRUE);
1218 		mpt->phydisk_sim = NULL;
1219 	}
1220 	MPT_UNLOCK(mpt);
1221 }
1222 
1223 /* This routine is used after a system crash to dump core onto the swap device.
1224  */
1225 static void
mpt_poll(struct cam_sim * sim)1226 mpt_poll(struct cam_sim *sim)
1227 {
1228 	struct mpt_softc *mpt;
1229 
1230 	mpt = (struct mpt_softc *)cam_sim_softc(sim);
1231 	mpt_intr(mpt);
1232 }
1233 
1234 /*
1235  * Watchdog timeout routine for SCSI requests.
1236  */
1237 static void
mpt_timeout(void * arg)1238 mpt_timeout(void *arg)
1239 {
1240 	union ccb	 *ccb;
1241 	struct mpt_softc *mpt;
1242 	request_t	 *req;
1243 
1244 	ccb = (union ccb *)arg;
1245 	mpt = ccb->ccb_h.ccb_mpt_ptr;
1246 
1247 	MPT_LOCK_ASSERT(mpt);
1248 	req = ccb->ccb_h.ccb_req_ptr;
1249 	mpt_prt(mpt, "request %p:%u timed out for ccb %p (req->ccb %p)\n", req,
1250 	    req->serno, ccb, req->ccb);
1251 /* XXX: WHAT ARE WE TRYING TO DO HERE? */
1252 	if ((req->state & REQ_STATE_QUEUED) == REQ_STATE_QUEUED) {
1253 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
1254 		TAILQ_INSERT_TAIL(&mpt->request_timeout_list, req, links);
1255 		req->state |= REQ_STATE_TIMEDOUT;
1256 		mpt_wakeup_recovery_thread(mpt);
1257 	}
1258 }
1259 
1260 /*
1261  * Callback routine from bus_dmamap_load_ccb(9) or, in simple cases, called
1262  * directly.
1263  *
1264  * Takes a list of physical segments and builds the SGL for SCSI IO command
1265  * and forwards the commard to the IOC after one last check that CAM has not
1266  * aborted the transaction.
1267  */
1268 static void
mpt_execute_req_a64(void * arg,bus_dma_segment_t * dm_segs,int nseg,int error)1269 mpt_execute_req_a64(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1270 {
1271 	request_t *req, *trq;
1272 	char *mpt_off;
1273 	union ccb *ccb;
1274 	struct mpt_softc *mpt;
1275 	bus_addr_t chain_list_addr;
1276 	int first_lim, seg, this_seg_lim;
1277 	uint32_t addr, cur_off, flags, nxt_off, tf;
1278 	void *sglp = NULL;
1279 	MSG_REQUEST_HEADER *hdrp;
1280 	SGE_SIMPLE64 *se;
1281 	SGE_CHAIN64 *ce;
1282 	int istgt = 0;
1283 
1284 	req = (request_t *)arg;
1285 	ccb = req->ccb;
1286 
1287 	mpt = ccb->ccb_h.ccb_mpt_ptr;
1288 	req = ccb->ccb_h.ccb_req_ptr;
1289 
1290 	hdrp = req->req_vbuf;
1291 	mpt_off = req->req_vbuf;
1292 
1293 	if (error == 0) {
1294 		switch (hdrp->Function) {
1295 		case MPI_FUNCTION_SCSI_IO_REQUEST:
1296 		case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1297 			istgt = 0;
1298 			sglp = &((PTR_MSG_SCSI_IO_REQUEST)hdrp)->SGL;
1299 			break;
1300 		case MPI_FUNCTION_TARGET_ASSIST:
1301 			istgt = 1;
1302 			sglp = &((PTR_MSG_TARGET_ASSIST_REQUEST)hdrp)->SGL;
1303 			break;
1304 		default:
1305 			mpt_prt(mpt, "bad fct 0x%x in mpt_execute_req_a64\n",
1306 			    hdrp->Function);
1307 			error = EINVAL;
1308 			break;
1309 		}
1310 	}
1311 
1312 bad:
1313 	if (error != 0) {
1314 		if (error != EFBIG && error != ENOMEM) {
1315 			mpt_prt(mpt, "mpt_execute_req_a64: err %d\n", error);
1316 		}
1317 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1318 			cam_status status;
1319 			mpt_freeze_ccb(ccb);
1320 			if (error == EFBIG) {
1321 				status = CAM_REQ_TOO_BIG;
1322 			} else if (error == ENOMEM) {
1323 				if (mpt->outofbeer == 0) {
1324 					mpt->outofbeer = 1;
1325 					xpt_freeze_simq(mpt->sim, 1);
1326 					mpt_lprt(mpt, MPT_PRT_DEBUG,
1327 					    "FREEZEQ\n");
1328 				}
1329 				status = CAM_REQUEUE_REQ;
1330 			} else {
1331 				status = CAM_REQ_CMP_ERR;
1332 			}
1333 			mpt_set_ccb_status(ccb, status);
1334 		}
1335 		if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1336 			request_t *cmd_req =
1337 				MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1338 			MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
1339 			MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
1340 			MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
1341 		}
1342 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1343 		KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d", __LINE__));
1344 		xpt_done(ccb);
1345 		mpt_free_request(mpt, req);
1346 		return;
1347 	}
1348 
1349 	/*
1350 	 * No data to transfer?
1351 	 * Just make a single simple SGL with zero length.
1352 	 */
1353 
1354 	if (mpt->verbose >= MPT_PRT_DEBUG) {
1355 		int tidx = ((char *)sglp) - mpt_off;
1356 		memset(&mpt_off[tidx], 0xff, MPT_REQUEST_AREA - tidx);
1357 	}
1358 
1359 	if (nseg == 0) {
1360 		SGE_SIMPLE32 *se1 = (SGE_SIMPLE32 *) sglp;
1361 		MPI_pSGE_SET_FLAGS(se1,
1362 		    (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1363 		    MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_END_OF_LIST));
1364 		se1->FlagsLength = htole32(se1->FlagsLength);
1365 		goto out;
1366 	}
1367 
1368 	flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_64_BIT_ADDRESSING;
1369 	if (istgt == 0) {
1370 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1371 			flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1372 		}
1373 	} else {
1374 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1375 			flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1376 		}
1377 	}
1378 
1379 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1380 		bus_dmasync_op_t op;
1381 		if (istgt == 0) {
1382 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1383 				op = BUS_DMASYNC_PREREAD;
1384 			} else {
1385 				op = BUS_DMASYNC_PREWRITE;
1386 			}
1387 		} else {
1388 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1389 				op = BUS_DMASYNC_PREWRITE;
1390 			} else {
1391 				op = BUS_DMASYNC_PREREAD;
1392 			}
1393 		}
1394 		bus_dmamap_sync(mpt->buffer_dmat, req->dmap, op);
1395 	}
1396 
1397 	/*
1398 	 * Okay, fill in what we can at the end of the command frame.
1399 	 * If we have up to MPT_NSGL_FIRST, we can fit them all into
1400 	 * the command frame.
1401 	 *
1402 	 * Otherwise, we fill up through MPT_NSGL_FIRST less one
1403 	 * SIMPLE64 pointers and start doing CHAIN64 entries after
1404 	 * that.
1405 	 */
1406 
1407 	if (nseg < MPT_NSGL_FIRST(mpt)) {
1408 		first_lim = nseg;
1409 	} else {
1410 		/*
1411 		 * Leave room for CHAIN element
1412 		 */
1413 		first_lim = MPT_NSGL_FIRST(mpt) - 1;
1414 	}
1415 
1416 	se = (SGE_SIMPLE64 *) sglp;
1417 	for (seg = 0; seg < first_lim; seg++, se++, dm_segs++) {
1418 		tf = flags;
1419 		memset(se, 0, sizeof (*se));
1420 		MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1421 		se->Address.Low = htole32(dm_segs->ds_addr & 0xffffffff);
1422 		if (sizeof(bus_addr_t) > 4) {
1423 			addr = ((uint64_t)dm_segs->ds_addr) >> 32;
1424 			/* SAS1078 36GB limitation WAR */
1425 			if (mpt->is_1078 && (((uint64_t)dm_segs->ds_addr +
1426 			    MPI_SGE_LENGTH(se->FlagsLength)) >> 32) == 9) {
1427 				addr |= (1U << 31);
1428 				tf |= MPI_SGE_FLAGS_LOCAL_ADDRESS;
1429 			}
1430 			se->Address.High = htole32(addr);
1431 		}
1432 		if (seg == first_lim - 1) {
1433 			tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
1434 		}
1435 		if (seg == nseg - 1) {
1436 			tf |=	MPI_SGE_FLAGS_END_OF_LIST |
1437 				MPI_SGE_FLAGS_END_OF_BUFFER;
1438 		}
1439 		MPI_pSGE_SET_FLAGS(se, tf);
1440 		se->FlagsLength = htole32(se->FlagsLength);
1441 	}
1442 
1443 	if (seg == nseg) {
1444 		goto out;
1445 	}
1446 
1447 	/*
1448 	 * Tell the IOC where to find the first chain element.
1449 	 */
1450 	hdrp->ChainOffset = ((char *)se - (char *)hdrp) >> 2;
1451 	nxt_off = MPT_RQSL(mpt);
1452 	trq = req;
1453 
1454 	/*
1455 	 * Make up the rest of the data segments out of a chain element
1456 	 * (contained in the current request frame) which points to
1457 	 * SIMPLE64 elements in the next request frame, possibly ending
1458 	 * with *another* chain element (if there's more).
1459 	 */
1460 	while (seg < nseg) {
1461 		/*
1462 		 * Point to the chain descriptor. Note that the chain
1463 		 * descriptor is at the end of the *previous* list (whether
1464 		 * chain or simple).
1465 		 */
1466 		ce = (SGE_CHAIN64 *) se;
1467 
1468 		/*
1469 		 * Before we change our current pointer, make  sure we won't
1470 		 * overflow the request area with this frame. Note that we
1471 		 * test against 'greater than' here as it's okay in this case
1472 		 * to have next offset be just outside the request area.
1473 		 */
1474 		if ((nxt_off + MPT_RQSL(mpt)) > MPT_REQUEST_AREA) {
1475 			nxt_off = MPT_REQUEST_AREA;
1476 			goto next_chain;
1477 		}
1478 
1479 		/*
1480 		 * Set our SGE element pointer to the beginning of the chain
1481 		 * list and update our next chain list offset.
1482 		 */
1483 		se = (SGE_SIMPLE64 *) &mpt_off[nxt_off];
1484 		cur_off = nxt_off;
1485 		nxt_off += MPT_RQSL(mpt);
1486 
1487 		/*
1488 		 * Now initialize the chain descriptor.
1489 		 */
1490 		memset(ce, 0, sizeof (*ce));
1491 
1492 		/*
1493 		 * Get the physical address of the chain list.
1494 		 */
1495 		chain_list_addr = trq->req_pbuf;
1496 		chain_list_addr += cur_off;
1497 		if (sizeof (bus_addr_t) > 4) {
1498 			ce->Address.High =
1499 			    htole32(((uint64_t)chain_list_addr) >> 32);
1500 		}
1501 		ce->Address.Low = htole32(chain_list_addr & 0xffffffff);
1502 		ce->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT |
1503 			    MPI_SGE_FLAGS_64_BIT_ADDRESSING;
1504 
1505 		/*
1506 		 * If we have more than a frame's worth of segments left,
1507 		 * set up the chain list to have the last element be another
1508 		 * chain descriptor.
1509 		 */
1510 		if ((nseg - seg) > MPT_NSGL(mpt)) {
1511 			this_seg_lim = seg + MPT_NSGL(mpt) - 1;
1512 			/*
1513 			 * The length of the chain is the length in bytes of the
1514 			 * number of segments plus the next chain element.
1515 			 *
1516 			 * The next chain descriptor offset is the length,
1517 			 * in words, of the number of segments.
1518 			 */
1519 			ce->Length = (this_seg_lim - seg) *
1520 			    sizeof (SGE_SIMPLE64);
1521 			ce->NextChainOffset = ce->Length >> 2;
1522 			ce->Length += sizeof (SGE_CHAIN64);
1523 		} else {
1524 			this_seg_lim = nseg;
1525 			ce->Length = (this_seg_lim - seg) *
1526 			    sizeof (SGE_SIMPLE64);
1527 		}
1528 		ce->Length = htole16(ce->Length);
1529 
1530 		/*
1531 		 * Fill in the chain list SGE elements with our segment data.
1532 		 *
1533 		 * If we're the last element in this chain list, set the last
1534 		 * element flag. If we're the completely last element period,
1535 		 * set the end of list and end of buffer flags.
1536 		 */
1537 		while (seg < this_seg_lim) {
1538 			tf = flags;
1539 			memset(se, 0, sizeof (*se));
1540 			MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1541 			se->Address.Low = htole32(dm_segs->ds_addr &
1542 			    0xffffffff);
1543 			if (sizeof (bus_addr_t) > 4) {
1544 				addr = ((uint64_t)dm_segs->ds_addr) >> 32;
1545 				/* SAS1078 36GB limitation WAR */
1546 				if (mpt->is_1078 &&
1547 				    (((uint64_t)dm_segs->ds_addr +
1548 				    MPI_SGE_LENGTH(se->FlagsLength)) >>
1549 				    32) == 9) {
1550 					addr |= (1U << 31);
1551 					tf |= MPI_SGE_FLAGS_LOCAL_ADDRESS;
1552 				}
1553 				se->Address.High = htole32(addr);
1554 			}
1555 			if (seg == this_seg_lim - 1) {
1556 				tf |=	MPI_SGE_FLAGS_LAST_ELEMENT;
1557 			}
1558 			if (seg == nseg - 1) {
1559 				tf |=	MPI_SGE_FLAGS_END_OF_LIST |
1560 					MPI_SGE_FLAGS_END_OF_BUFFER;
1561 			}
1562 			MPI_pSGE_SET_FLAGS(se, tf);
1563 			se->FlagsLength = htole32(se->FlagsLength);
1564 			se++;
1565 			seg++;
1566 			dm_segs++;
1567 		}
1568 
1569     next_chain:
1570 		/*
1571 		 * If we have more segments to do and we've used up all of
1572 		 * the space in a request area, go allocate another one
1573 		 * and chain to that.
1574 		 */
1575 		if (seg < nseg && nxt_off >= MPT_REQUEST_AREA) {
1576 			request_t *nrq;
1577 
1578 			nrq = mpt_get_request(mpt, FALSE);
1579 
1580 			if (nrq == NULL) {
1581 				error = ENOMEM;
1582 				goto bad;
1583 			}
1584 
1585 			/*
1586 			 * Append the new request area on the tail of our list.
1587 			 */
1588 			if ((trq = req->chain) == NULL) {
1589 				req->chain = nrq;
1590 			} else {
1591 				while (trq->chain != NULL) {
1592 					trq = trq->chain;
1593 				}
1594 				trq->chain = nrq;
1595 			}
1596 			trq = nrq;
1597 			mpt_off = trq->req_vbuf;
1598 			if (mpt->verbose >= MPT_PRT_DEBUG) {
1599 				memset(mpt_off, 0xff, MPT_REQUEST_AREA);
1600 			}
1601 			nxt_off = 0;
1602 		}
1603 	}
1604 out:
1605 
1606 	/*
1607 	 * Last time we need to check if this CCB needs to be aborted.
1608 	 */
1609 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1610 		if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1611 			request_t *cmd_req =
1612 				MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1613 			MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
1614 			MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
1615 			MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
1616 		}
1617 		mpt_prt(mpt,
1618 		    "mpt_execute_req_a64: I/O cancelled (status 0x%x)\n",
1619 		    ccb->ccb_h.status & CAM_STATUS_MASK);
1620 		if (nseg) {
1621 			bus_dmamap_unload(mpt->buffer_dmat, req->dmap);
1622 		}
1623 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1624 		KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d", __LINE__));
1625 		xpt_done(ccb);
1626 		mpt_free_request(mpt, req);
1627 		return;
1628 	}
1629 
1630 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1631 	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1632 		mpt_req_timeout(req, SBT_1MS * ccb->ccb_h.timeout,
1633 		    mpt_timeout, ccb);
1634 	}
1635 	if (mpt->verbose > MPT_PRT_DEBUG) {
1636 		int nc = 0;
1637 		mpt_print_request(req->req_vbuf);
1638 		for (trq = req->chain; trq; trq = trq->chain) {
1639 			printf("  Additional Chain Area %d\n", nc++);
1640 			mpt_dump_sgl(trq->req_vbuf, 0);
1641 		}
1642 	}
1643 
1644 	if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1645 		request_t *cmd_req = MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1646 		mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, cmd_req);
1647 #ifdef	WE_TRUST_AUTO_GOOD_STATUS
1648 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1649 		    csio->scsi_status == SCSI_STATUS_OK && tgt->resid == 0) {
1650 			tgt->state = TGT_STATE_MOVING_DATA_AND_STATUS;
1651 		} else {
1652 			tgt->state = TGT_STATE_MOVING_DATA;
1653 		}
1654 #else
1655 		tgt->state = TGT_STATE_MOVING_DATA;
1656 #endif
1657 	}
1658 	mpt_send_cmd(mpt, req);
1659 }
1660 
1661 static void
mpt_execute_req(void * arg,bus_dma_segment_t * dm_segs,int nseg,int error)1662 mpt_execute_req(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1663 {
1664 	request_t *req, *trq;
1665 	char *mpt_off;
1666 	union ccb *ccb;
1667 	struct mpt_softc *mpt;
1668 	int seg, first_lim;
1669 	uint32_t flags, nxt_off;
1670 	void *sglp = NULL;
1671 	MSG_REQUEST_HEADER *hdrp;
1672 	SGE_SIMPLE32 *se;
1673 	SGE_CHAIN32 *ce;
1674 	int istgt = 0;
1675 
1676 	req = (request_t *)arg;
1677 	ccb = req->ccb;
1678 
1679 	mpt = ccb->ccb_h.ccb_mpt_ptr;
1680 	req = ccb->ccb_h.ccb_req_ptr;
1681 
1682 	hdrp = req->req_vbuf;
1683 	mpt_off = req->req_vbuf;
1684 
1685 	if (error == 0) {
1686 		switch (hdrp->Function) {
1687 		case MPI_FUNCTION_SCSI_IO_REQUEST:
1688 		case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1689 			sglp = &((PTR_MSG_SCSI_IO_REQUEST)hdrp)->SGL;
1690 			break;
1691 		case MPI_FUNCTION_TARGET_ASSIST:
1692 			istgt = 1;
1693 			sglp = &((PTR_MSG_TARGET_ASSIST_REQUEST)hdrp)->SGL;
1694 			break;
1695 		default:
1696 			mpt_prt(mpt, "bad fct 0x%x in mpt_execute_req\n",
1697 			    hdrp->Function);
1698 			error = EINVAL;
1699 			break;
1700 		}
1701 	}
1702 
1703 bad:
1704 	if (error != 0) {
1705 		if (error != EFBIG && error != ENOMEM) {
1706 			mpt_prt(mpt, "mpt_execute_req: err %d\n", error);
1707 		}
1708 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1709 			cam_status status;
1710 			mpt_freeze_ccb(ccb);
1711 			if (error == EFBIG) {
1712 				status = CAM_REQ_TOO_BIG;
1713 			} else if (error == ENOMEM) {
1714 				if (mpt->outofbeer == 0) {
1715 					mpt->outofbeer = 1;
1716 					xpt_freeze_simq(mpt->sim, 1);
1717 					mpt_lprt(mpt, MPT_PRT_DEBUG,
1718 					    "FREEZEQ\n");
1719 				}
1720 				status = CAM_REQUEUE_REQ;
1721 			} else {
1722 				status = CAM_REQ_CMP_ERR;
1723 			}
1724 			mpt_set_ccb_status(ccb, status);
1725 		}
1726 		if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1727 			request_t *cmd_req =
1728 				MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1729 			MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
1730 			MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
1731 			MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
1732 		}
1733 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1734 		KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d", __LINE__));
1735 		xpt_done(ccb);
1736 		mpt_free_request(mpt, req);
1737 		return;
1738 	}
1739 
1740 	/*
1741 	 * No data to transfer?
1742 	 * Just make a single simple SGL with zero length.
1743 	 */
1744 
1745 	if (mpt->verbose >= MPT_PRT_DEBUG) {
1746 		int tidx = ((char *)sglp) - mpt_off;
1747 		memset(&mpt_off[tidx], 0xff, MPT_REQUEST_AREA - tidx);
1748 	}
1749 
1750 	if (nseg == 0) {
1751 		SGE_SIMPLE32 *se1 = (SGE_SIMPLE32 *) sglp;
1752 		MPI_pSGE_SET_FLAGS(se1,
1753 		    (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1754 		    MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_END_OF_LIST));
1755 		se1->FlagsLength = htole32(se1->FlagsLength);
1756 		goto out;
1757 	}
1758 
1759 	flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
1760 	if (istgt == 0) {
1761 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1762 			flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1763 		}
1764 	} else {
1765 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1766 			flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1767 		}
1768 	}
1769 
1770 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1771 		bus_dmasync_op_t op;
1772 		if (istgt) {
1773 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1774 				op = BUS_DMASYNC_PREREAD;
1775 			} else {
1776 				op = BUS_DMASYNC_PREWRITE;
1777 			}
1778 		} else {
1779 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1780 				op = BUS_DMASYNC_PREWRITE;
1781 			} else {
1782 				op = BUS_DMASYNC_PREREAD;
1783 			}
1784 		}
1785 		bus_dmamap_sync(mpt->buffer_dmat, req->dmap, op);
1786 	}
1787 
1788 	/*
1789 	 * Okay, fill in what we can at the end of the command frame.
1790 	 * If we have up to MPT_NSGL_FIRST, we can fit them all into
1791 	 * the command frame.
1792 	 *
1793 	 * Otherwise, we fill up through MPT_NSGL_FIRST less one
1794 	 * SIMPLE32 pointers and start doing CHAIN32 entries after
1795 	 * that.
1796 	 */
1797 
1798 	if (nseg < MPT_NSGL_FIRST(mpt)) {
1799 		first_lim = nseg;
1800 	} else {
1801 		/*
1802 		 * Leave room for CHAIN element
1803 		 */
1804 		first_lim = MPT_NSGL_FIRST(mpt) - 1;
1805 	}
1806 
1807 	se = (SGE_SIMPLE32 *) sglp;
1808 	for (seg = 0; seg < first_lim; seg++, se++, dm_segs++) {
1809 		uint32_t tf;
1810 
1811 		memset(se, 0,sizeof (*se));
1812 		se->Address = htole32(dm_segs->ds_addr);
1813 
1814 		MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1815 		tf = flags;
1816 		if (seg == first_lim - 1) {
1817 			tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
1818 		}
1819 		if (seg == nseg - 1) {
1820 			tf |=	MPI_SGE_FLAGS_END_OF_LIST |
1821 				MPI_SGE_FLAGS_END_OF_BUFFER;
1822 		}
1823 		MPI_pSGE_SET_FLAGS(se, tf);
1824 		se->FlagsLength = htole32(se->FlagsLength);
1825 	}
1826 
1827 	if (seg == nseg) {
1828 		goto out;
1829 	}
1830 
1831 	/*
1832 	 * Tell the IOC where to find the first chain element.
1833 	 */
1834 	hdrp->ChainOffset = ((char *)se - (char *)hdrp) >> 2;
1835 	nxt_off = MPT_RQSL(mpt);
1836 	trq = req;
1837 
1838 	/*
1839 	 * Make up the rest of the data segments out of a chain element
1840 	 * (contained in the current request frame) which points to
1841 	 * SIMPLE32 elements in the next request frame, possibly ending
1842 	 * with *another* chain element (if there's more).
1843 	 */
1844 	while (seg < nseg) {
1845 		int this_seg_lim;
1846 		uint32_t tf, cur_off;
1847 		bus_addr_t chain_list_addr;
1848 
1849 		/*
1850 		 * Point to the chain descriptor. Note that the chain
1851 		 * descriptor is at the end of the *previous* list (whether
1852 		 * chain or simple).
1853 		 */
1854 		ce = (SGE_CHAIN32 *) se;
1855 
1856 		/*
1857 		 * Before we change our current pointer, make  sure we won't
1858 		 * overflow the request area with this frame. Note that we
1859 		 * test against 'greater than' here as it's okay in this case
1860 		 * to have next offset be just outside the request area.
1861 		 */
1862 		if ((nxt_off + MPT_RQSL(mpt)) > MPT_REQUEST_AREA) {
1863 			nxt_off = MPT_REQUEST_AREA;
1864 			goto next_chain;
1865 		}
1866 
1867 		/*
1868 		 * Set our SGE element pointer to the beginning of the chain
1869 		 * list and update our next chain list offset.
1870 		 */
1871 		se = (SGE_SIMPLE32 *) &mpt_off[nxt_off];
1872 		cur_off = nxt_off;
1873 		nxt_off += MPT_RQSL(mpt);
1874 
1875 		/*
1876 		 * Now initialize the chain descriptor.
1877 		 */
1878 		memset(ce, 0, sizeof (*ce));
1879 
1880 		/*
1881 		 * Get the physical address of the chain list.
1882 		 */
1883 		chain_list_addr = trq->req_pbuf;
1884 		chain_list_addr += cur_off;
1885 
1886 		ce->Address = htole32(chain_list_addr);
1887 		ce->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT;
1888 
1889 		/*
1890 		 * If we have more than a frame's worth of segments left,
1891 		 * set up the chain list to have the last element be another
1892 		 * chain descriptor.
1893 		 */
1894 		if ((nseg - seg) > MPT_NSGL(mpt)) {
1895 			this_seg_lim = seg + MPT_NSGL(mpt) - 1;
1896 			/*
1897 			 * The length of the chain is the length in bytes of the
1898 			 * number of segments plus the next chain element.
1899 			 *
1900 			 * The next chain descriptor offset is the length,
1901 			 * in words, of the number of segments.
1902 			 */
1903 			ce->Length = (this_seg_lim - seg) *
1904 			    sizeof (SGE_SIMPLE32);
1905 			ce->NextChainOffset = ce->Length >> 2;
1906 			ce->Length += sizeof (SGE_CHAIN32);
1907 		} else {
1908 			this_seg_lim = nseg;
1909 			ce->Length = (this_seg_lim - seg) *
1910 			    sizeof (SGE_SIMPLE32);
1911 		}
1912 		ce->Length = htole16(ce->Length);
1913 
1914 		/*
1915 		 * Fill in the chain list SGE elements with our segment data.
1916 		 *
1917 		 * If we're the last element in this chain list, set the last
1918 		 * element flag. If we're the completely last element period,
1919 		 * set the end of list and end of buffer flags.
1920 		 */
1921 		while (seg < this_seg_lim) {
1922 			memset(se, 0, sizeof (*se));
1923 			se->Address = htole32(dm_segs->ds_addr);
1924 
1925 			MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1926 			tf = flags;
1927 			if (seg == this_seg_lim - 1) {
1928 				tf |=	MPI_SGE_FLAGS_LAST_ELEMENT;
1929 			}
1930 			if (seg == nseg - 1) {
1931 				tf |=	MPI_SGE_FLAGS_END_OF_LIST |
1932 					MPI_SGE_FLAGS_END_OF_BUFFER;
1933 			}
1934 			MPI_pSGE_SET_FLAGS(se, tf);
1935 			se->FlagsLength = htole32(se->FlagsLength);
1936 			se++;
1937 			seg++;
1938 			dm_segs++;
1939 		}
1940 
1941     next_chain:
1942 		/*
1943 		 * If we have more segments to do and we've used up all of
1944 		 * the space in a request area, go allocate another one
1945 		 * and chain to that.
1946 		 */
1947 		if (seg < nseg && nxt_off >= MPT_REQUEST_AREA) {
1948 			request_t *nrq;
1949 
1950 			nrq = mpt_get_request(mpt, FALSE);
1951 
1952 			if (nrq == NULL) {
1953 				error = ENOMEM;
1954 				goto bad;
1955 			}
1956 
1957 			/*
1958 			 * Append the new request area on the tail of our list.
1959 			 */
1960 			if ((trq = req->chain) == NULL) {
1961 				req->chain = nrq;
1962 			} else {
1963 				while (trq->chain != NULL) {
1964 					trq = trq->chain;
1965 				}
1966 				trq->chain = nrq;
1967 			}
1968 			trq = nrq;
1969 			mpt_off = trq->req_vbuf;
1970 			if (mpt->verbose >= MPT_PRT_DEBUG) {
1971 				memset(mpt_off, 0xff, MPT_REQUEST_AREA);
1972 			}
1973 			nxt_off = 0;
1974 		}
1975 	}
1976 out:
1977 
1978 	/*
1979 	 * Last time we need to check if this CCB needs to be aborted.
1980 	 */
1981 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1982 		if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1983 			request_t *cmd_req =
1984 				MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1985 			MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
1986 			MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
1987 			MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
1988 		}
1989 		mpt_prt(mpt,
1990 		    "mpt_execute_req: I/O cancelled (status 0x%x)\n",
1991 		    ccb->ccb_h.status & CAM_STATUS_MASK);
1992 		if (nseg) {
1993 			bus_dmamap_unload(mpt->buffer_dmat, req->dmap);
1994 		}
1995 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1996 		KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d", __LINE__));
1997 		xpt_done(ccb);
1998 		mpt_free_request(mpt, req);
1999 		return;
2000 	}
2001 
2002 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
2003 	if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2004 		mpt_req_timeout(req, SBT_1MS * ccb->ccb_h.timeout,
2005 		    mpt_timeout, ccb);
2006 	}
2007 	if (mpt->verbose > MPT_PRT_DEBUG) {
2008 		int nc = 0;
2009 		mpt_print_request(req->req_vbuf);
2010 		for (trq = req->chain; trq; trq = trq->chain) {
2011 			printf("  Additional Chain Area %d\n", nc++);
2012 			mpt_dump_sgl(trq->req_vbuf, 0);
2013 		}
2014 	}
2015 
2016 	if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
2017 		request_t *cmd_req = MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
2018 		mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, cmd_req);
2019 #ifdef	WE_TRUST_AUTO_GOOD_STATUS
2020 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2021 		    csio->scsi_status == SCSI_STATUS_OK && tgt->resid == 0) {
2022 			tgt->state = TGT_STATE_MOVING_DATA_AND_STATUS;
2023 		} else {
2024 			tgt->state = TGT_STATE_MOVING_DATA;
2025 		}
2026 #else
2027 		tgt->state = TGT_STATE_MOVING_DATA;
2028 #endif
2029 	}
2030 	mpt_send_cmd(mpt, req);
2031 }
2032 
2033 static void
mpt_start(struct cam_sim * sim,union ccb * ccb)2034 mpt_start(struct cam_sim *sim, union ccb *ccb)
2035 {
2036 	request_t *req;
2037 	struct mpt_softc *mpt;
2038 	MSG_SCSI_IO_REQUEST *mpt_req;
2039 	struct ccb_scsiio *csio = &ccb->csio;
2040 	struct ccb_hdr *ccbh = &ccb->ccb_h;
2041 	bus_dmamap_callback_t *cb;
2042 	target_id_t tgt;
2043 	int raid_passthru;
2044 	int error;
2045 
2046 	/* Get the pointer for the physical addapter */
2047 	mpt = ccb->ccb_h.ccb_mpt_ptr;
2048 	raid_passthru = (sim == mpt->phydisk_sim);
2049 
2050 	if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
2051 		if (mpt->outofbeer == 0) {
2052 			mpt->outofbeer = 1;
2053 			xpt_freeze_simq(mpt->sim, 1);
2054 			mpt_lprt(mpt, MPT_PRT_DEBUG, "FREEZEQ\n");
2055 		}
2056 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2057 		mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
2058 		xpt_done(ccb);
2059 		return;
2060 	}
2061 #ifdef	INVARIANTS
2062 	mpt_req_not_spcl(mpt, req, "mpt_start", __LINE__);
2063 #endif
2064 
2065 	if (sizeof (bus_addr_t) > 4) {
2066 		cb = mpt_execute_req_a64;
2067 	} else {
2068 		cb = mpt_execute_req;
2069 	}
2070 
2071 	/*
2072 	 * Link the ccb and the request structure so we can find
2073 	 * the other knowing either the request or the ccb
2074 	 */
2075 	req->ccb = ccb;
2076 	ccb->ccb_h.ccb_req_ptr = req;
2077 
2078 	/* Now we build the command for the IOC */
2079 	mpt_req = req->req_vbuf;
2080 	memset(mpt_req, 0, sizeof (MSG_SCSI_IO_REQUEST));
2081 
2082 	mpt_req->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
2083 	if (raid_passthru) {
2084 		mpt_req->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2085 		if (mpt_map_physdisk(mpt, ccb, &tgt) != 0) {
2086 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2087 			mpt_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2088 			xpt_done(ccb);
2089 			return;
2090 		}
2091 		mpt_req->Bus = 0;	/* we never set bus here */
2092 	} else {
2093 		tgt = ccb->ccb_h.target_id;
2094 		mpt_req->Bus = 0;	/* XXX */
2095 
2096 	}
2097 	mpt_req->SenseBufferLength =
2098 		(csio->sense_len < MPT_SENSE_SIZE) ?
2099 		 csio->sense_len : MPT_SENSE_SIZE;
2100 
2101 	/*
2102 	 * We use the message context to find the request structure when we
2103 	 * Get the command completion interrupt from the IOC.
2104 	 */
2105 	mpt_req->MsgContext = htole32(req->index | scsi_io_handler_id);
2106 
2107 	/* Which physical device to do the I/O on */
2108 	mpt_req->TargetID = tgt;
2109 
2110 	be64enc(mpt_req->LUN, CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
2111 
2112 	/* Set the direction of the transfer */
2113 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2114 		mpt_req->Control = MPI_SCSIIO_CONTROL_READ;
2115 	} else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2116 		mpt_req->Control = MPI_SCSIIO_CONTROL_WRITE;
2117 	} else {
2118 		mpt_req->Control = MPI_SCSIIO_CONTROL_NODATATRANSFER;
2119 	}
2120 
2121 	if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2122 		switch(ccb->csio.tag_action) {
2123 		case MSG_HEAD_OF_Q_TAG:
2124 			mpt_req->Control |= MPI_SCSIIO_CONTROL_HEADOFQ;
2125 			break;
2126 		case MSG_ACA_TASK:
2127 			mpt_req->Control |= MPI_SCSIIO_CONTROL_ACAQ;
2128 			break;
2129 		case MSG_ORDERED_Q_TAG:
2130 			mpt_req->Control |= MPI_SCSIIO_CONTROL_ORDEREDQ;
2131 			break;
2132 		case MSG_SIMPLE_Q_TAG:
2133 		default:
2134 			mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
2135 			break;
2136 		}
2137 	} else {
2138 		if (mpt->is_fc || mpt->is_sas) {
2139 			mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
2140 		} else {
2141 			/* XXX No such thing for a target doing packetized. */
2142 			mpt_req->Control |= MPI_SCSIIO_CONTROL_UNTAGGED;
2143 		}
2144 	}
2145 
2146 	if (mpt->is_spi) {
2147 		if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
2148 			mpt_req->Control |= MPI_SCSIIO_CONTROL_NO_DISCONNECT;
2149 		}
2150 	}
2151 	mpt_req->Control = htole32(mpt_req->Control);
2152 
2153 	/* Copy the scsi command block into place */
2154 	if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2155 		bcopy(csio->cdb_io.cdb_ptr, mpt_req->CDB, csio->cdb_len);
2156 	} else {
2157 		bcopy(csio->cdb_io.cdb_bytes, mpt_req->CDB, csio->cdb_len);
2158 	}
2159 
2160 	mpt_req->CDBLength = csio->cdb_len;
2161 	mpt_req->DataLength = htole32(csio->dxfer_len);
2162 	mpt_req->SenseBufferLowAddr = htole32(req->sense_pbuf);
2163 
2164 	/*
2165 	 * Do a *short* print here if we're set to MPT_PRT_DEBUG
2166 	 */
2167 	if (mpt->verbose == MPT_PRT_DEBUG) {
2168 		U32 df;
2169 		mpt_prt(mpt, "mpt_start: %s op 0x%x ",
2170 		    (mpt_req->Function == MPI_FUNCTION_SCSI_IO_REQUEST)?
2171 		    "SCSI_IO_REQUEST" : "SCSI_IO_PASSTHRU", mpt_req->CDB[0]);
2172 		df = mpt_req->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK;
2173 		if (df != MPI_SCSIIO_CONTROL_NODATATRANSFER) {
2174 			mpt_prtc(mpt, "(%s %u byte%s ",
2175 			    (df == MPI_SCSIIO_CONTROL_READ)?
2176 			    "read" : "write",  csio->dxfer_len,
2177 			    (csio->dxfer_len == 1)? ")" : "s)");
2178 		}
2179 		mpt_prtc(mpt, "tgt %u lun %jx req %p:%u\n", tgt,
2180 		    (uintmax_t)ccb->ccb_h.target_lun, req, req->serno);
2181 	}
2182 
2183 	error = bus_dmamap_load_ccb(mpt->buffer_dmat, req->dmap, ccb, cb,
2184 	    req, 0);
2185 	if (error == EINPROGRESS) {
2186 		/*
2187 		 * So as to maintain ordering, freeze the controller queue
2188 		 * until our mapping is returned.
2189 		 */
2190 		xpt_freeze_simq(mpt->sim, 1);
2191 		ccbh->status |= CAM_RELEASE_SIMQ;
2192 	}
2193 }
2194 
2195 static int
mpt_bus_reset(struct mpt_softc * mpt,target_id_t tgt,lun_id_t lun,int sleep_ok)2196 mpt_bus_reset(struct mpt_softc *mpt, target_id_t tgt, lun_id_t lun,
2197     int sleep_ok)
2198 {
2199 	int   error;
2200 	uint16_t status;
2201 	uint8_t response;
2202 
2203 	error = mpt_scsi_send_tmf(mpt,
2204 	    (tgt != CAM_TARGET_WILDCARD || lun != CAM_LUN_WILDCARD) ?
2205 	    MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET :
2206 	    MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
2207 	    mpt->is_fc ? MPI_SCSITASKMGMT_MSGFLAGS_LIP_RESET_OPTION : 0,
2208 	    0,	/* XXX How do I get the channel ID? */
2209 	    tgt != CAM_TARGET_WILDCARD ? tgt : 0,
2210 	    lun != CAM_LUN_WILDCARD ? lun : 0,
2211 	    0, sleep_ok);
2212 
2213 	if (error != 0) {
2214 		/*
2215 		 * mpt_scsi_send_tmf hard resets on failure, so no
2216 		 * need to do so here.
2217 		 */
2218 		mpt_prt(mpt,
2219 		    "mpt_bus_reset: mpt_scsi_send_tmf returned %d\n", error);
2220 		return (EIO);
2221 	}
2222 
2223 	/* Wait for bus reset to be processed by the IOC. */
2224 	error = mpt_wait_req(mpt, mpt->tmf_req, REQ_STATE_DONE,
2225 	    REQ_STATE_DONE, sleep_ok, 5000);
2226 
2227 	status = le16toh(mpt->tmf_req->IOCStatus);
2228 	response = mpt->tmf_req->ResponseCode;
2229 	mpt->tmf_req->state = REQ_STATE_FREE;
2230 
2231 	if (error) {
2232 		mpt_prt(mpt, "mpt_bus_reset: Reset timed-out. "
2233 		    "Resetting controller.\n");
2234 		mpt_reset(mpt, TRUE);
2235 		return (ETIMEDOUT);
2236 	}
2237 
2238 	if ((status & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
2239 		mpt_prt(mpt, "mpt_bus_reset: TMF IOC Status 0x%x. "
2240 		    "Resetting controller.\n", status);
2241 		mpt_reset(mpt, TRUE);
2242 		return (EIO);
2243 	}
2244 
2245 	if (response != MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED &&
2246 	    response != MPI_SCSITASKMGMT_RSP_TM_COMPLETE) {
2247 		mpt_prt(mpt, "mpt_bus_reset: TMF Response 0x%x. "
2248 		    "Resetting controller.\n", response);
2249 		mpt_reset(mpt, TRUE);
2250 		return (EIO);
2251 	}
2252 	return (0);
2253 }
2254 
2255 static int
mpt_fc_reset_link(struct mpt_softc * mpt,int dowait)2256 mpt_fc_reset_link(struct mpt_softc *mpt, int dowait)
2257 {
2258 	int r = 0;
2259 	request_t *req;
2260 	PTR_MSG_FC_PRIMITIVE_SEND_REQUEST fc;
2261 
2262  	req = mpt_get_request(mpt, FALSE);
2263 	if (req == NULL) {
2264 		return (ENOMEM);
2265 	}
2266 	fc = req->req_vbuf;
2267 	memset(fc, 0, sizeof(*fc));
2268 	fc->SendFlags = MPI_FC_PRIM_SEND_FLAGS_RESET_LINK;
2269 	fc->Function = MPI_FUNCTION_FC_PRIMITIVE_SEND;
2270 	fc->MsgContext = htole32(req->index | fc_els_handler_id);
2271 	mpt_send_cmd(mpt, req);
2272 	if (dowait) {
2273 		r = mpt_wait_req(mpt, req, REQ_STATE_DONE,
2274 		    REQ_STATE_DONE, FALSE, 60 * 1000);
2275 		if (r == 0) {
2276 			mpt_free_request(mpt, req);
2277 		}
2278 	}
2279 	return (r);
2280 }
2281 
2282 static int
mpt_cam_event(struct mpt_softc * mpt,request_t * req,MSG_EVENT_NOTIFY_REPLY * msg)2283 mpt_cam_event(struct mpt_softc *mpt, request_t *req,
2284 	      MSG_EVENT_NOTIFY_REPLY *msg)
2285 {
2286 	uint32_t data0, data1;
2287 
2288 	data0 = le32toh(msg->Data[0]);
2289 	data1 = le32toh(msg->Data[1]);
2290 	switch(msg->Event & 0xFF) {
2291 	case MPI_EVENT_UNIT_ATTENTION:
2292 		mpt_prt(mpt, "UNIT ATTENTION: Bus: 0x%02x TargetID: 0x%02x\n",
2293 		    (data0 >> 8) & 0xff, data0 & 0xff);
2294 		break;
2295 
2296 	case MPI_EVENT_IOC_BUS_RESET:
2297 		/* We generated a bus reset */
2298 		mpt_prt(mpt, "IOC Generated Bus Reset Port: %d\n",
2299 		    (data0 >> 8) & 0xff);
2300 		xpt_async(AC_BUS_RESET, mpt->path, NULL);
2301 		break;
2302 
2303 	case MPI_EVENT_EXT_BUS_RESET:
2304 		/* Someone else generated a bus reset */
2305 		mpt_prt(mpt, "External Bus Reset Detected\n");
2306 		/*
2307 		 * These replies don't return EventData like the MPI
2308 		 * spec says they do
2309 		 */
2310 		xpt_async(AC_BUS_RESET, mpt->path, NULL);
2311 		break;
2312 
2313 	case MPI_EVENT_RESCAN:
2314 	{
2315 		union ccb *ccb;
2316 		uint32_t pathid;
2317 		/*
2318 		 * In general this means a device has been added to the loop.
2319 		 */
2320 		mpt_prt(mpt, "Rescan Port: %d\n", (data0 >> 8) & 0xff);
2321 		if (mpt->ready == 0) {
2322 			break;
2323 		}
2324 		if (mpt->phydisk_sim) {
2325 			pathid = cam_sim_path(mpt->phydisk_sim);
2326 		} else {
2327 			pathid = cam_sim_path(mpt->sim);
2328 		}
2329 		/*
2330 		 * Allocate a CCB, create a wildcard path for this bus,
2331 		 * and schedule a rescan.
2332 		 */
2333 		ccb = xpt_alloc_ccb_nowait();
2334 		if (ccb == NULL) {
2335 			mpt_prt(mpt, "unable to alloc CCB for rescan\n");
2336 			break;
2337 		}
2338 
2339 		if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
2340 		    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2341 			mpt_prt(mpt, "unable to create path for rescan\n");
2342 			xpt_free_ccb(ccb);
2343 			break;
2344 		}
2345 		xpt_rescan(ccb);
2346 		break;
2347 	}
2348 
2349 	case MPI_EVENT_LINK_STATUS_CHANGE:
2350 		mpt_prt(mpt, "Port %d: LinkState: %s\n",
2351 		    (data1 >> 8) & 0xff,
2352 		    ((data0 & 0xff) == 0)?  "Failed" : "Active");
2353 		break;
2354 
2355 	case MPI_EVENT_LOOP_STATE_CHANGE:
2356 		switch ((data0 >> 16) & 0xff) {
2357 		case 0x01:
2358 			mpt_prt(mpt,
2359 			    "Port 0x%x: FC LinkEvent: LIP(%02x,%02x) "
2360 			    "(Loop Initialization)\n",
2361 			    (data1 >> 8) & 0xff,
2362 			    (data0 >> 8) & 0xff,
2363 			    (data0     ) & 0xff);
2364 			switch ((data0 >> 8) & 0xff) {
2365 			case 0xF7:
2366 				if ((data0 & 0xff) == 0xF7) {
2367 					mpt_prt(mpt, "Device needs AL_PA\n");
2368 				} else {
2369 					mpt_prt(mpt, "Device %02x doesn't like "
2370 					    "FC performance\n",
2371 					    data0 & 0xFF);
2372 				}
2373 				break;
2374 			case 0xF8:
2375 				if ((data0 & 0xff) == 0xF7) {
2376 					mpt_prt(mpt, "Device had loop failure "
2377 					    "at its receiver prior to acquiring"
2378 					    " AL_PA\n");
2379 				} else {
2380 					mpt_prt(mpt, "Device %02x detected loop"
2381 					    " failure at its receiver\n",
2382 					    data0 & 0xFF);
2383 				}
2384 				break;
2385 			default:
2386 				mpt_prt(mpt, "Device %02x requests that device "
2387 				    "%02x reset itself\n",
2388 				    data0 & 0xFF,
2389 				    (data0 >> 8) & 0xFF);
2390 				break;
2391 			}
2392 			break;
2393 		case 0x02:
2394 			mpt_prt(mpt, "Port 0x%x: FC LinkEvent: "
2395 			    "LPE(%02x,%02x) (Loop Port Enable)\n",
2396 			    (data1 >> 8) & 0xff, /* Port */
2397 			    (data0 >>  8) & 0xff, /* Character 3 */
2398 			    (data0      ) & 0xff  /* Character 4 */);
2399 			break;
2400 		case 0x03:
2401 			mpt_prt(mpt, "Port 0x%x: FC LinkEvent: "
2402 			    "LPB(%02x,%02x) (Loop Port Bypass)\n",
2403 			    (data1 >> 8) & 0xff, /* Port */
2404 			    (data0 >> 8) & 0xff, /* Character 3 */
2405 			    (data0     ) & 0xff  /* Character 4 */);
2406 			break;
2407 		default:
2408 			mpt_prt(mpt, "Port 0x%x: FC LinkEvent: Unknown "
2409 			    "FC event (%02x %02x %02x)\n",
2410 			    (data1 >> 8) & 0xff, /* Port */
2411 			    (data0 >> 16) & 0xff, /* Event */
2412 			    (data0 >>  8) & 0xff, /* Character 3 */
2413 			    (data0      ) & 0xff  /* Character 4 */);
2414 		}
2415 		break;
2416 
2417 	case MPI_EVENT_LOGOUT:
2418 		mpt_prt(mpt, "FC Logout Port: %d N_PortID: %02x\n",
2419 		    (data1 >> 8) & 0xff, data0);
2420 		break;
2421 	case MPI_EVENT_QUEUE_FULL:
2422 	{
2423 		struct cam_sim *sim;
2424 		struct cam_path *tmppath;
2425 		struct ccb_relsim crs;
2426 		PTR_EVENT_DATA_QUEUE_FULL pqf;
2427 		lun_id_t lun_id;
2428 
2429 		pqf = (PTR_EVENT_DATA_QUEUE_FULL)msg->Data;
2430 		pqf->CurrentDepth = le16toh(pqf->CurrentDepth);
2431 		if (bootverbose) {
2432 		    mpt_prt(mpt, "QUEUE FULL EVENT: Bus 0x%02x Target 0x%02x "
2433 			"Depth %d\n",
2434 			pqf->Bus, pqf->TargetID, pqf->CurrentDepth);
2435 		}
2436 		if (mpt->phydisk_sim && mpt_is_raid_member(mpt,
2437 		    pqf->TargetID) != 0) {
2438 			sim = mpt->phydisk_sim;
2439 		} else {
2440 			sim = mpt->sim;
2441 		}
2442 		for (lun_id = 0; lun_id < MPT_MAX_LUNS; lun_id++) {
2443 			if (xpt_create_path(&tmppath, NULL, cam_sim_path(sim),
2444 			    pqf->TargetID, lun_id) != CAM_REQ_CMP) {
2445 				mpt_prt(mpt, "unable to create a path to send "
2446 				    "XPT_REL_SIMQ");
2447 				break;
2448 			}
2449 			xpt_setup_ccb(&crs.ccb_h, tmppath, 5);
2450 			crs.ccb_h.func_code = XPT_REL_SIMQ;
2451 			crs.ccb_h.flags = CAM_DEV_QFREEZE;
2452 			crs.release_flags = RELSIM_ADJUST_OPENINGS;
2453 			crs.openings = pqf->CurrentDepth - 1;
2454 			xpt_action((union ccb *)&crs);
2455 			if (crs.ccb_h.status != CAM_REQ_CMP) {
2456 				mpt_prt(mpt, "XPT_REL_SIMQ failed\n");
2457 			}
2458 			xpt_free_path(tmppath);
2459 		}
2460 		break;
2461 	}
2462 	case MPI_EVENT_IR_RESYNC_UPDATE:
2463 		mpt_prt(mpt, "IR resync update %d completed\n",
2464 		    (data0 >> 16) & 0xff);
2465 		break;
2466 	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
2467 	{
2468 		union ccb *ccb;
2469 		struct cam_sim *sim;
2470 		struct cam_path *tmppath;
2471 		PTR_EVENT_DATA_SAS_DEVICE_STATUS_CHANGE psdsc;
2472 
2473 		psdsc = (PTR_EVENT_DATA_SAS_DEVICE_STATUS_CHANGE)msg->Data;
2474 		if (mpt->phydisk_sim && mpt_is_raid_member(mpt,
2475 		    psdsc->TargetID) != 0)
2476 			sim = mpt->phydisk_sim;
2477 		else
2478 			sim = mpt->sim;
2479 		switch(psdsc->ReasonCode) {
2480 		case MPI_EVENT_SAS_DEV_STAT_RC_ADDED:
2481 			ccb = xpt_alloc_ccb_nowait();
2482 			if (ccb == NULL) {
2483 				mpt_prt(mpt,
2484 				    "unable to alloc CCB for rescan\n");
2485 				break;
2486 			}
2487 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
2488 			    cam_sim_path(sim), psdsc->TargetID,
2489 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2490 				mpt_prt(mpt,
2491 				    "unable to create path for rescan\n");
2492 				xpt_free_ccb(ccb);
2493 				break;
2494 			}
2495 			xpt_rescan(ccb);
2496 			break;
2497 		case MPI_EVENT_SAS_DEV_STAT_RC_NOT_RESPONDING:
2498 			if (xpt_create_path(&tmppath, NULL, cam_sim_path(sim),
2499 			    psdsc->TargetID, CAM_LUN_WILDCARD) !=
2500 			    CAM_REQ_CMP) {
2501 				mpt_prt(mpt,
2502 				    "unable to create path for async event");
2503 				break;
2504 			}
2505 			xpt_async(AC_LOST_DEVICE, tmppath, NULL);
2506 			xpt_free_path(tmppath);
2507 			break;
2508 		case MPI_EVENT_SAS_DEV_STAT_RC_CMPL_INTERNAL_DEV_RESET:
2509 		case MPI_EVENT_SAS_DEV_STAT_RC_CMPL_TASK_ABORT_INTERNAL:
2510 		case MPI_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
2511 			break;
2512 		default:
2513 			mpt_lprt(mpt, MPT_PRT_WARN,
2514 			    "SAS device status change: Bus: 0x%02x TargetID: "
2515 			    "0x%02x ReasonCode: 0x%02x\n", psdsc->Bus,
2516 			    psdsc->TargetID, psdsc->ReasonCode);
2517 			break;
2518 		}
2519 		break;
2520 	}
2521 	case MPI_EVENT_SAS_DISCOVERY_ERROR:
2522 	{
2523 		PTR_EVENT_DATA_DISCOVERY_ERROR pde;
2524 
2525 		pde = (PTR_EVENT_DATA_DISCOVERY_ERROR)msg->Data;
2526 		pde->DiscoveryStatus = le32toh(pde->DiscoveryStatus);
2527 		mpt_lprt(mpt, MPT_PRT_WARN,
2528 		    "SAS discovery error: Port: 0x%02x Status: 0x%08x\n",
2529 		    pde->Port, pde->DiscoveryStatus);
2530 		break;
2531 	}
2532 	case MPI_EVENT_EVENT_CHANGE:
2533 	case MPI_EVENT_INTEGRATED_RAID:
2534 	case MPI_EVENT_IR2:
2535 	case MPI_EVENT_LOG_ENTRY_ADDED:
2536 	case MPI_EVENT_SAS_DISCOVERY:
2537 	case MPI_EVENT_SAS_PHY_LINK_STATUS:
2538 	case MPI_EVENT_SAS_SES:
2539 		break;
2540 	default:
2541 		mpt_lprt(mpt, MPT_PRT_WARN, "mpt_cam_event: 0x%x\n",
2542 		    msg->Event & 0xFF);
2543 		return (0);
2544 	}
2545 	return (1);
2546 }
2547 
2548 /*
2549  * Reply path for all SCSI I/O requests, called from our
2550  * interrupt handler by extracting our handler index from
2551  * the MsgContext field of the reply from the IOC.
2552  *
2553  * This routine is optimized for the common case of a
2554  * completion without error.  All exception handling is
2555  * offloaded to non-inlined helper routines to minimize
2556  * cache footprint.
2557  */
2558 static int
mpt_scsi_reply_handler(struct mpt_softc * mpt,request_t * req,uint32_t reply_desc,MSG_DEFAULT_REPLY * reply_frame)2559 mpt_scsi_reply_handler(struct mpt_softc *mpt, request_t *req,
2560     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
2561 {
2562 	MSG_SCSI_IO_REQUEST *scsi_req;
2563 	union ccb *ccb;
2564 
2565 	if (req->state == REQ_STATE_FREE) {
2566 		mpt_prt(mpt, "mpt_scsi_reply_handler: req already free\n");
2567 		return (TRUE);
2568 	}
2569 
2570 	scsi_req = (MSG_SCSI_IO_REQUEST *)req->req_vbuf;
2571 	ccb = req->ccb;
2572 	if (ccb == NULL) {
2573 		mpt_prt(mpt, "mpt_scsi_reply_handler: req %p:%u with no ccb\n",
2574 		    req, req->serno);
2575 		return (TRUE);
2576 	}
2577 
2578 	mpt_req_untimeout(req, mpt_timeout, ccb);
2579 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2580 
2581 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2582 		bus_dmasync_op_t op;
2583 
2584 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
2585 			op = BUS_DMASYNC_POSTREAD;
2586 		else
2587 			op = BUS_DMASYNC_POSTWRITE;
2588 		bus_dmamap_sync(mpt->buffer_dmat, req->dmap, op);
2589 		bus_dmamap_unload(mpt->buffer_dmat, req->dmap);
2590 	}
2591 
2592 	if (reply_frame == NULL) {
2593 		/*
2594 		 * Context only reply, completion without error status.
2595 		 */
2596 		ccb->csio.resid = 0;
2597 		mpt_set_ccb_status(ccb, CAM_REQ_CMP);
2598 		ccb->csio.scsi_status = SCSI_STATUS_OK;
2599 	} else {
2600 		mpt_scsi_reply_frame_handler(mpt, req, reply_frame);
2601 	}
2602 
2603 	if (mpt->outofbeer) {
2604 		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2605 		mpt->outofbeer = 0;
2606 		mpt_lprt(mpt, MPT_PRT_DEBUG, "THAWQ\n");
2607 	}
2608 	if (scsi_req->CDB[0] == INQUIRY && (scsi_req->CDB[1] & SI_EVPD) == 0) {
2609 		struct scsi_inquiry_data *iq =
2610 		    (struct scsi_inquiry_data *)ccb->csio.data_ptr;
2611 		if (scsi_req->Function ==
2612 		    MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
2613 			/*
2614 			 * Fake out the device type so that only the
2615 			 * pass-thru device will attach.
2616 			 */
2617 			iq->device &= ~0x1F;
2618 			iq->device |= T_NODEVICE;
2619 		}
2620 	}
2621 	if (mpt->verbose == MPT_PRT_DEBUG) {
2622 		mpt_prt(mpt, "mpt_scsi_reply_handler: %p:%u complete\n",
2623 		    req, req->serno);
2624 	}
2625 	KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d", __LINE__));
2626 	xpt_done(ccb);
2627 	if ((req->state & REQ_STATE_TIMEDOUT) == 0) {
2628 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2629 	} else {
2630 		mpt_prt(mpt, "completing timedout/aborted req %p:%u\n",
2631 		    req, req->serno);
2632 		TAILQ_REMOVE(&mpt->request_timeout_list, req, links);
2633 	}
2634 	KASSERT((req->state & REQ_STATE_NEED_WAKEUP) == 0,
2635 	    ("CCB req needed wakeup"));
2636 #ifdef	INVARIANTS
2637 	mpt_req_not_spcl(mpt, req, "mpt_scsi_reply_handler", __LINE__);
2638 #endif
2639 	mpt_free_request(mpt, req);
2640 	return (TRUE);
2641 }
2642 
2643 static int
mpt_scsi_tmf_reply_handler(struct mpt_softc * mpt,request_t * req,uint32_t reply_desc,MSG_DEFAULT_REPLY * reply_frame)2644 mpt_scsi_tmf_reply_handler(struct mpt_softc *mpt, request_t *req,
2645     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
2646 {
2647 	MSG_SCSI_TASK_MGMT_REPLY *tmf_reply;
2648 
2649 	KASSERT(req == mpt->tmf_req, ("TMF Reply not using mpt->tmf_req"));
2650 #ifdef	INVARIANTS
2651 	mpt_req_not_spcl(mpt, req, "mpt_scsi_tmf_reply_handler", __LINE__);
2652 #endif
2653 	tmf_reply = (MSG_SCSI_TASK_MGMT_REPLY *)reply_frame;
2654 	/* Record IOC Status and Response Code of TMF for any waiters. */
2655 	req->IOCStatus = le16toh(tmf_reply->IOCStatus);
2656 	req->ResponseCode = tmf_reply->ResponseCode;
2657 
2658 	mpt_lprt(mpt, MPT_PRT_DEBUG, "TMF complete: req %p:%u status 0x%x\n",
2659 	    req, req->serno, le16toh(tmf_reply->IOCStatus));
2660 	TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2661 	if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
2662 		req->state |= REQ_STATE_DONE;
2663 		wakeup(req);
2664 	} else {
2665 		mpt->tmf_req->state = REQ_STATE_FREE;
2666 	}
2667 	return (TRUE);
2668 }
2669 
2670 /*
2671  * XXX: Move to definitions file
2672  */
2673 #define	ELS	0x22
2674 #define	FC4LS	0x32
2675 #define	ABTS	0x81
2676 #define	BA_ACC	0x84
2677 
2678 #define	LS_RJT	0x01
2679 #define	LS_ACC	0x02
2680 #define	PLOGI	0x03
2681 #define	LOGO	0x05
2682 #define SRR	0x14
2683 #define PRLI	0x20
2684 #define PRLO	0x21
2685 #define ADISC	0x52
2686 #define RSCN	0x61
2687 
2688 static void
mpt_fc_els_send_response(struct mpt_softc * mpt,request_t * req,PTR_MSG_LINK_SERVICE_BUFFER_POST_REPLY rp,U8 length)2689 mpt_fc_els_send_response(struct mpt_softc *mpt, request_t *req,
2690     PTR_MSG_LINK_SERVICE_BUFFER_POST_REPLY rp, U8 length)
2691 {
2692 	uint32_t fl;
2693 	MSG_LINK_SERVICE_RSP_REQUEST tmp;
2694 	PTR_MSG_LINK_SERVICE_RSP_REQUEST rsp;
2695 
2696 	/*
2697 	 * We are going to reuse the ELS request to send this response back.
2698 	 */
2699 	rsp = &tmp;
2700 	memset(rsp, 0, sizeof(*rsp));
2701 
2702 #ifdef	USE_IMMEDIATE_LINK_DATA
2703 	/*
2704 	 * Apparently the IMMEDIATE stuff doesn't seem to work.
2705 	 */
2706 	rsp->RspFlags = LINK_SERVICE_RSP_FLAGS_IMMEDIATE;
2707 #endif
2708 	rsp->RspLength = length;
2709 	rsp->Function = MPI_FUNCTION_FC_LINK_SRVC_RSP;
2710 	rsp->MsgContext = htole32(req->index | fc_els_handler_id);
2711 
2712 	/*
2713 	 * Copy over information from the original reply frame to
2714 	 * it's correct place in the response.
2715 	 */
2716 	memcpy((U8 *)rsp + 0x0c, (U8 *)rp + 0x1c, 24);
2717 
2718 	/*
2719 	 * And now copy back the temporary area to the original frame.
2720 	 */
2721 	memcpy(req->req_vbuf, rsp, sizeof (MSG_LINK_SERVICE_RSP_REQUEST));
2722 	rsp = req->req_vbuf;
2723 
2724 #ifdef	USE_IMMEDIATE_LINK_DATA
2725 	memcpy((U8 *)&rsp->SGL, &((U8 *)req->req_vbuf)[MPT_RQSL(mpt)], length);
2726 #else
2727 {
2728 	PTR_SGE_SIMPLE32 se = (PTR_SGE_SIMPLE32) &rsp->SGL;
2729 	bus_addr_t paddr = req->req_pbuf;
2730 	paddr += MPT_RQSL(mpt);
2731 
2732 	fl =
2733 		MPI_SGE_FLAGS_HOST_TO_IOC	|
2734 		MPI_SGE_FLAGS_SIMPLE_ELEMENT	|
2735 		MPI_SGE_FLAGS_LAST_ELEMENT	|
2736 		MPI_SGE_FLAGS_END_OF_LIST	|
2737 		MPI_SGE_FLAGS_END_OF_BUFFER;
2738 	fl <<= MPI_SGE_FLAGS_SHIFT;
2739 	fl |= (length);
2740 	se->FlagsLength = htole32(fl);
2741 	se->Address = htole32((uint32_t) paddr);
2742 }
2743 #endif
2744 
2745 	/*
2746 	 * Send it on...
2747 	 */
2748 	mpt_send_cmd(mpt, req);
2749 }
2750 
2751 static int
mpt_fc_els_reply_handler(struct mpt_softc * mpt,request_t * req,uint32_t reply_desc,MSG_DEFAULT_REPLY * reply_frame)2752 mpt_fc_els_reply_handler(struct mpt_softc *mpt, request_t *req,
2753     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
2754 {
2755 	PTR_MSG_LINK_SERVICE_BUFFER_POST_REPLY rp =
2756 	    (PTR_MSG_LINK_SERVICE_BUFFER_POST_REPLY) reply_frame;
2757 	U8 rctl;
2758 	U8 type;
2759 	U8 cmd;
2760 	U16 status = le16toh(reply_frame->IOCStatus);
2761 	U32 *elsbuf;
2762 	int ioindex;
2763 	int do_refresh = TRUE;
2764 
2765 #ifdef	INVARIANTS
2766 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
2767 	    ("fc_els_reply_handler: req %p:%u for function %x on freelist!",
2768 	    req, req->serno, rp->Function));
2769 	if (rp->Function != MPI_FUNCTION_FC_PRIMITIVE_SEND) {
2770 		mpt_req_spcl(mpt, req, "fc_els_reply_handler", __LINE__);
2771 	} else {
2772 		mpt_req_not_spcl(mpt, req, "fc_els_reply_handler", __LINE__);
2773 	}
2774 #endif
2775 	mpt_lprt(mpt, MPT_PRT_DEBUG,
2776 	    "FC_ELS Complete: req %p:%u, reply %p function %x\n",
2777 	    req, req->serno, reply_frame, reply_frame->Function);
2778 
2779 	if  (status != MPI_IOCSTATUS_SUCCESS) {
2780 		mpt_prt(mpt, "ELS REPLY STATUS 0x%x for Function %x\n",
2781 		    status, reply_frame->Function);
2782 		if (status == MPI_IOCSTATUS_INVALID_STATE) {
2783 			/*
2784 			 * XXX: to get around shutdown issue
2785 			 */
2786 			mpt->disabled = 1;
2787 			return (TRUE);
2788 		}
2789 		return (TRUE);
2790 	}
2791 
2792 	/*
2793 	 * If the function of a link service response, we recycle the
2794 	 * response to be a refresh for a new link service request.
2795 	 *
2796 	 * The request pointer is bogus in this case and we have to fetch
2797 	 * it based upon the TransactionContext.
2798 	 */
2799 	if (rp->Function == MPI_FUNCTION_FC_LINK_SRVC_RSP) {
2800 		/* Freddie Uncle Charlie Katie */
2801 		/* We don't get the IOINDEX as part of the Link Svc Rsp */
2802 		for (ioindex = 0; ioindex < mpt->els_cmds_allocated; ioindex++)
2803 			if (mpt->els_cmd_ptrs[ioindex] == req) {
2804 				break;
2805 			}
2806 
2807 		KASSERT(ioindex < mpt->els_cmds_allocated,
2808 		    ("can't find my mommie!"));
2809 
2810 		/* remove from active list as we're going to re-post it */
2811 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2812 		req->state &= ~REQ_STATE_QUEUED;
2813 		req->state |= REQ_STATE_DONE;
2814 		mpt_fc_post_els(mpt, req, ioindex);
2815 		return (TRUE);
2816 	}
2817 
2818 	if (rp->Function == MPI_FUNCTION_FC_PRIMITIVE_SEND) {
2819 		/* remove from active list as we're done */
2820 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2821 		req->state &= ~REQ_STATE_QUEUED;
2822 		req->state |= REQ_STATE_DONE;
2823 		if (req->state & REQ_STATE_TIMEDOUT) {
2824 			mpt_lprt(mpt, MPT_PRT_DEBUG,
2825 			    "Sync Primitive Send Completed After Timeout\n");
2826 			mpt_free_request(mpt, req);
2827 		} else if ((req->state & REQ_STATE_NEED_WAKEUP) == 0) {
2828 			mpt_lprt(mpt, MPT_PRT_DEBUG,
2829 			    "Async Primitive Send Complete\n");
2830 			mpt_free_request(mpt, req);
2831 		} else {
2832 			mpt_lprt(mpt, MPT_PRT_DEBUG,
2833 			    "Sync Primitive Send Complete- Waking Waiter\n");
2834 			wakeup(req);
2835 		}
2836 		return (TRUE);
2837 	}
2838 
2839 	if (rp->Function != MPI_FUNCTION_FC_LINK_SRVC_BUF_POST) {
2840 		mpt_prt(mpt, "unexpected ELS_REPLY: Function 0x%x Flags %x "
2841 		    "Length %d Message Flags %x\n", rp->Function, rp->Flags,
2842 		    rp->MsgLength, rp->MsgFlags);
2843 		return (TRUE);
2844 	}
2845 
2846 	if (rp->MsgLength <= 5) {
2847 		/*
2848 		 * This is just a ack of an original ELS buffer post
2849 		 */
2850 		mpt_lprt(mpt, MPT_PRT_DEBUG,
2851 		    "RECV'd ACK of FC_ELS buf post %p:%u\n", req, req->serno);
2852 		return (TRUE);
2853 	}
2854 
2855 	rctl = (le32toh(rp->Rctl_Did) & MPI_FC_RCTL_MASK) >> MPI_FC_RCTL_SHIFT;
2856 	type = (le32toh(rp->Type_Fctl) & MPI_FC_TYPE_MASK) >> MPI_FC_TYPE_SHIFT;
2857 
2858 	elsbuf = &((U32 *)req->req_vbuf)[MPT_RQSL(mpt)/sizeof (U32)];
2859 	cmd = be32toh(elsbuf[0]) >> 24;
2860 
2861 	if (rp->Flags & MPI_LS_BUF_POST_REPLY_FLAG_NO_RSP_NEEDED) {
2862 		mpt_lprt(mpt, MPT_PRT_ALWAYS, "ELS_REPLY: response unneeded\n");
2863 		return (TRUE);
2864 	}
2865 
2866 	ioindex = le32toh(rp->TransactionContext);
2867 	req = mpt->els_cmd_ptrs[ioindex];
2868 
2869 	if (rctl == ELS && type == 1) {
2870 		switch (cmd) {
2871 		case PRLI:
2872 			/*
2873 			 * Send back a PRLI ACC
2874 			 */
2875 			mpt_prt(mpt, "PRLI from 0x%08x%08x\n",
2876 			    le32toh(rp->Wwn.PortNameHigh),
2877 			    le32toh(rp->Wwn.PortNameLow));
2878 			elsbuf[0] = htobe32(0x02100014);
2879 			elsbuf[1] |= htobe32(0x00000100);
2880 			elsbuf[4] = htobe32(0x00000002);
2881 			if (mpt->role & MPT_ROLE_TARGET)
2882 				elsbuf[4] |= htobe32(0x00000010);
2883 			if (mpt->role & MPT_ROLE_INITIATOR)
2884 				elsbuf[4] |= htobe32(0x00000020);
2885 			/* remove from active list as we're done */
2886 			TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2887 			req->state &= ~REQ_STATE_QUEUED;
2888 			req->state |= REQ_STATE_DONE;
2889 			mpt_fc_els_send_response(mpt, req, rp, 20);
2890 			do_refresh = FALSE;
2891 			break;
2892 		case PRLO:
2893 			memset(elsbuf, 0, 5 * (sizeof (U32)));
2894 			elsbuf[0] = htobe32(0x02100014);
2895 			elsbuf[1] = htobe32(0x08000100);
2896 			mpt_prt(mpt, "PRLO from 0x%08x%08x\n",
2897 			    le32toh(rp->Wwn.PortNameHigh),
2898 			    le32toh(rp->Wwn.PortNameLow));
2899 			/* remove from active list as we're done */
2900 			TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2901 			req->state &= ~REQ_STATE_QUEUED;
2902 			req->state |= REQ_STATE_DONE;
2903 			mpt_fc_els_send_response(mpt, req, rp, 20);
2904 			do_refresh = FALSE;
2905 			break;
2906 		default:
2907 			mpt_prt(mpt, "ELS TYPE 1 COMMAND: %x\n", cmd);
2908 			break;
2909 		}
2910 	} else if (rctl == ABTS && type == 0) {
2911 		uint16_t rx_id = le16toh(rp->Rxid);
2912 		uint16_t ox_id = le16toh(rp->Oxid);
2913 		mpt_tgt_state_t *tgt;
2914 		request_t *tgt_req = NULL;
2915 		union ccb *ccb;
2916 		uint32_t ct_id;
2917 
2918 		mpt_prt(mpt,
2919 		    "ELS: ABTS OX_ID 0x%x RX_ID 0x%x from 0x%08x%08x\n",
2920 		    ox_id, rx_id, le32toh(rp->Wwn.PortNameHigh),
2921 		    le32toh(rp->Wwn.PortNameLow));
2922 		if (rx_id >= mpt->mpt_max_tgtcmds) {
2923 			mpt_prt(mpt, "Bad RX_ID 0x%x\n", rx_id);
2924 		} else if (mpt->tgt_cmd_ptrs == NULL) {
2925 			mpt_prt(mpt, "No TGT CMD PTRS\n");
2926 		} else {
2927 			tgt_req = mpt->tgt_cmd_ptrs[rx_id];
2928 		}
2929 		if (tgt_req == NULL) {
2930 			mpt_prt(mpt, "no back pointer for RX_ID 0x%x\n", rx_id);
2931 			goto skip;
2932 		}
2933 		tgt = MPT_TGT_STATE(mpt, tgt_req);
2934 
2935 		/* Check to make sure we have the correct command. */
2936 		ct_id = GET_IO_INDEX(tgt->reply_desc);
2937 		if (ct_id != rx_id) {
2938 			mpt_lprt(mpt, MPT_PRT_ERROR, "ABORT Mismatch: "
2939 			    "RX_ID received=0x%x, in cmd=0x%x\n", rx_id, ct_id);
2940 			goto skip;
2941 		}
2942 		if (tgt->itag != ox_id) {
2943 			mpt_lprt(mpt, MPT_PRT_ERROR, "ABORT Mismatch: "
2944 			    "OX_ID received=0x%x, in cmd=0x%x\n", ox_id, tgt->itag);
2945 			goto skip;
2946 		}
2947 
2948 		if ((ccb = tgt->ccb) != NULL) {
2949 			mpt_prt(mpt, "CCB (%p): lun %jx flags %x status %x\n",
2950 			    ccb, (uintmax_t)ccb->ccb_h.target_lun,
2951 			    ccb->ccb_h.flags, ccb->ccb_h.status);
2952 		}
2953 		mpt_prt(mpt, "target state 0x%x resid %u xfrd %u rpwrd "
2954 		    "%x nxfers %x\n", tgt->state, tgt->resid,
2955 		    tgt->bytes_xfered, tgt->reply_desc, tgt->nxfers);
2956 		if (mpt_abort_target_cmd(mpt, tgt_req))
2957 			mpt_prt(mpt, "unable to start TargetAbort\n");
2958 
2959 skip:
2960 		memset(elsbuf, 0, 5 * (sizeof (U32)));
2961 		elsbuf[0] = htobe32(0);
2962 		elsbuf[1] = htobe32((ox_id << 16) | rx_id);
2963 		elsbuf[2] = htobe32(0x000ffff);
2964 		/*
2965 		 * Dork with the reply frame so that the response to it
2966 		 * will be correct.
2967 		 */
2968 		rp->Rctl_Did += ((BA_ACC - ABTS) << MPI_FC_RCTL_SHIFT);
2969 		/* remove from active list as we're done */
2970 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2971 		req->state &= ~REQ_STATE_QUEUED;
2972 		req->state |= REQ_STATE_DONE;
2973 		mpt_fc_els_send_response(mpt, req, rp, 12);
2974 		do_refresh = FALSE;
2975 	} else {
2976 		mpt_prt(mpt, "ELS: RCTL %x TYPE %x CMD %x\n", rctl, type, cmd);
2977 	}
2978 	if (do_refresh == TRUE) {
2979 		/* remove from active list as we're done */
2980 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2981 		req->state &= ~REQ_STATE_QUEUED;
2982 		req->state |= REQ_STATE_DONE;
2983 		mpt_fc_post_els(mpt, req, ioindex);
2984 	}
2985 	return (TRUE);
2986 }
2987 
2988 /*
2989  * Clean up all SCSI Initiator personality state in response
2990  * to a controller reset.
2991  */
2992 static void
mpt_cam_ioc_reset(struct mpt_softc * mpt,int type)2993 mpt_cam_ioc_reset(struct mpt_softc *mpt, int type)
2994 {
2995 
2996 	/*
2997 	 * The pending list is already run down by
2998 	 * the generic handler.  Perform the same
2999 	 * operation on the timed out request list.
3000 	 */
3001 	mpt_complete_request_chain(mpt, &mpt->request_timeout_list,
3002 				   MPI_IOCSTATUS_INVALID_STATE);
3003 
3004 	/*
3005 	 * XXX: We need to repost ELS and Target Command Buffers?
3006 	 */
3007 
3008 	/*
3009 	 * Inform the XPT that a bus reset has occurred.
3010 	 */
3011 	xpt_async(AC_BUS_RESET, mpt->path, NULL);
3012 }
3013 
3014 /*
3015  * Parse additional completion information in the reply
3016  * frame for SCSI I/O requests.
3017  */
3018 static int
mpt_scsi_reply_frame_handler(struct mpt_softc * mpt,request_t * req,MSG_DEFAULT_REPLY * reply_frame)3019 mpt_scsi_reply_frame_handler(struct mpt_softc *mpt, request_t *req,
3020 			     MSG_DEFAULT_REPLY *reply_frame)
3021 {
3022 	union ccb *ccb;
3023 	MSG_SCSI_IO_REPLY *scsi_io_reply;
3024 	u_int ioc_status;
3025 	u_int sstate;
3026 
3027 	MPT_DUMP_REPLY_FRAME(mpt, reply_frame);
3028 	KASSERT(reply_frame->Function == MPI_FUNCTION_SCSI_IO_REQUEST
3029 	     || reply_frame->Function == MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH,
3030 		("MPT SCSI I/O Handler called with incorrect reply type"));
3031 	KASSERT((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0,
3032 		("MPT SCSI I/O Handler called with continuation reply"));
3033 
3034 	scsi_io_reply = (MSG_SCSI_IO_REPLY *)reply_frame;
3035 	ioc_status = le16toh(scsi_io_reply->IOCStatus);
3036 	ioc_status &= MPI_IOCSTATUS_MASK;
3037 	sstate = scsi_io_reply->SCSIState;
3038 
3039 	ccb = req->ccb;
3040 	ccb->csio.resid =
3041 	    ccb->csio.dxfer_len - le32toh(scsi_io_reply->TransferCount);
3042 
3043 	if ((sstate & MPI_SCSI_STATE_AUTOSENSE_VALID) != 0
3044 	 && (ccb->ccb_h.flags & (CAM_SENSE_PHYS | CAM_SENSE_PTR)) == 0) {
3045 		uint32_t sense_returned;
3046 
3047 		ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
3048 
3049 		sense_returned = le32toh(scsi_io_reply->SenseCount);
3050 		if (sense_returned < ccb->csio.sense_len)
3051 			ccb->csio.sense_resid = ccb->csio.sense_len -
3052 						sense_returned;
3053 		else
3054 			ccb->csio.sense_resid = 0;
3055 
3056 		bzero(&ccb->csio.sense_data, sizeof(ccb->csio.sense_data));
3057 		bcopy(req->sense_vbuf, &ccb->csio.sense_data,
3058 		    min(ccb->csio.sense_len, sense_returned));
3059 	}
3060 
3061 	if ((sstate & MPI_SCSI_STATE_QUEUE_TAG_REJECTED) != 0) {
3062 		/*
3063 		 * Tag messages rejected, but non-tagged retry
3064 		 * was successful.
3065 XXXX
3066 		mpt_set_tags(mpt, devinfo, MPT_QUEUE_NONE);
3067 		 */
3068 	}
3069 
3070 	switch(ioc_status) {
3071 	case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3072 		/*
3073 		 * XXX
3074 		 * Linux driver indicates that a zero
3075 		 * transfer length with this error code
3076 		 * indicates a CRC error.
3077 		 *
3078 		 * No need to swap the bytes for checking
3079 		 * against zero.
3080 		 */
3081 		if (scsi_io_reply->TransferCount == 0) {
3082 			mpt_set_ccb_status(ccb, CAM_UNCOR_PARITY);
3083 			break;
3084 		}
3085 		/* FALLTHROUGH */
3086 	case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:
3087 	case MPI_IOCSTATUS_SUCCESS:
3088 	case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:
3089 		if ((sstate & MPI_SCSI_STATE_NO_SCSI_STATUS) != 0) {
3090 			/*
3091 			 * Status was never returned for this transaction.
3092 			 */
3093 			mpt_set_ccb_status(ccb, CAM_UNEXP_BUSFREE);
3094 		} else if (scsi_io_reply->SCSIStatus != SCSI_STATUS_OK) {
3095 			ccb->csio.scsi_status = scsi_io_reply->SCSIStatus;
3096 			mpt_set_ccb_status(ccb, CAM_SCSI_STATUS_ERROR);
3097 			if ((sstate & MPI_SCSI_STATE_AUTOSENSE_FAILED) != 0)
3098 				mpt_set_ccb_status(ccb, CAM_AUTOSENSE_FAIL);
3099 		} else if ((sstate & MPI_SCSI_STATE_RESPONSE_INFO_VALID) != 0) {
3100 			/* XXX Handle SPI-Packet and FCP-2 response info. */
3101 			mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3102 		} else
3103 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3104 		break;
3105 	case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:
3106 		mpt_set_ccb_status(ccb, CAM_DATA_RUN_ERR);
3107 		break;
3108 	case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:
3109 		mpt_set_ccb_status(ccb, CAM_UNCOR_PARITY);
3110 		break;
3111 	case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3112 		/*
3113 		 * Since selection timeouts and "device really not
3114 		 * there" are grouped into this error code, report
3115 		 * selection timeout.  Selection timeouts are
3116 		 * typically retried before giving up on the device
3117 		 * whereas "device not there" errors are considered
3118 		 * unretryable.
3119 		 */
3120 		mpt_set_ccb_status(ccb, CAM_SEL_TIMEOUT);
3121 		break;
3122 	case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3123 		mpt_set_ccb_status(ccb, CAM_SEQUENCE_FAIL);
3124 		break;
3125 	case MPI_IOCSTATUS_SCSI_INVALID_BUS:
3126 		mpt_set_ccb_status(ccb, CAM_PATH_INVALID);
3127 		break;
3128 	case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:
3129 		mpt_set_ccb_status(ccb, CAM_TID_INVALID);
3130 		break;
3131 	case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3132 		ccb->ccb_h.status = CAM_UA_TERMIO;
3133 		break;
3134 	case MPI_IOCSTATUS_INVALID_STATE:
3135 		/*
3136 		 * The IOC has been reset.  Emulate a bus reset.
3137 		 */
3138 		/* FALLTHROUGH */
3139 	case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:
3140 		ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
3141 		break;
3142 	case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:
3143 	case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:
3144 		/*
3145 		 * Don't clobber any timeout status that has
3146 		 * already been set for this transaction.  We
3147 		 * want the SCSI layer to be able to differentiate
3148 		 * between the command we aborted due to timeout
3149 		 * and any innocent bystanders.
3150 		 */
3151 		if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG)
3152 			break;
3153 		mpt_set_ccb_status(ccb, CAM_REQ_TERMIO);
3154 		break;
3155 
3156 	case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:
3157 		mpt_set_ccb_status(ccb, CAM_RESRC_UNAVAIL);
3158 		break;
3159 	case MPI_IOCSTATUS_BUSY:
3160 		mpt_set_ccb_status(ccb, CAM_BUSY);
3161 		break;
3162 	case MPI_IOCSTATUS_INVALID_FUNCTION:
3163 	case MPI_IOCSTATUS_INVALID_SGL:
3164 	case MPI_IOCSTATUS_INTERNAL_ERROR:
3165 	case MPI_IOCSTATUS_INVALID_FIELD:
3166 	default:
3167 		/* XXX
3168 		 * Some of the above may need to kick
3169 		 * of a recovery action!!!!
3170 		 */
3171 		ccb->ccb_h.status = CAM_UNREC_HBA_ERROR;
3172 		break;
3173 	}
3174 
3175 	if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3176 		mpt_freeze_ccb(ccb);
3177 	}
3178 
3179 	return (TRUE);
3180 }
3181 
3182 static void
mpt_action(struct cam_sim * sim,union ccb * ccb)3183 mpt_action(struct cam_sim *sim, union ccb *ccb)
3184 {
3185 	struct mpt_softc *mpt;
3186 	struct ccb_trans_settings *cts;
3187 	target_id_t tgt;
3188 	lun_id_t lun;
3189 	int raid_passthru;
3190 
3191 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("mpt_action\n"));
3192 
3193 	mpt = (struct mpt_softc *)cam_sim_softc(sim);
3194 	raid_passthru = (sim == mpt->phydisk_sim);
3195 	MPT_LOCK_ASSERT(mpt);
3196 
3197 	tgt = ccb->ccb_h.target_id;
3198 	lun = ccb->ccb_h.target_lun;
3199 	if (raid_passthru &&
3200 	    ccb->ccb_h.func_code != XPT_PATH_INQ &&
3201 	    ccb->ccb_h.func_code != XPT_RESET_BUS &&
3202 	    ccb->ccb_h.func_code != XPT_RESET_DEV) {
3203 		if (mpt_map_physdisk(mpt, ccb, &tgt) != 0) {
3204 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3205 			mpt_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
3206 			xpt_done(ccb);
3207 			return;
3208 		}
3209 	}
3210 	ccb->ccb_h.ccb_mpt_ptr = mpt;
3211 
3212 	switch (ccb->ccb_h.func_code) {
3213 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
3214 		/*
3215 		 * Do a couple of preliminary checks...
3216 		 */
3217 		if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3218 			if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3219 				ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3220 				mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3221 				break;
3222 			}
3223 		}
3224 		/* Max supported CDB length is 16 bytes */
3225 		/* XXX Unless we implement the new 32byte message type */
3226 		if (ccb->csio.cdb_len >
3227 		    sizeof (((PTR_MSG_SCSI_IO_REQUEST)0)->CDB)) {
3228 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3229 			mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3230 			break;
3231 		}
3232 #ifdef	MPT_TEST_MULTIPATH
3233 		if (mpt->failure_id == ccb->ccb_h.target_id) {
3234 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3235 			mpt_set_ccb_status(ccb, CAM_SEL_TIMEOUT);
3236 			break;
3237 		}
3238 #endif
3239 		ccb->csio.scsi_status = SCSI_STATUS_OK;
3240 		mpt_start(sim, ccb);
3241 		return;
3242 
3243 	case XPT_RESET_BUS:
3244 		if (raid_passthru) {
3245 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3246 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3247 			break;
3248 		}
3249 	case XPT_RESET_DEV:
3250 		if (ccb->ccb_h.func_code == XPT_RESET_BUS) {
3251 			if (bootverbose) {
3252 				xpt_print(ccb->ccb_h.path, "reset bus\n");
3253 			}
3254 		} else {
3255 			xpt_print(ccb->ccb_h.path, "reset device\n");
3256 		}
3257 		(void) mpt_bus_reset(mpt, tgt, lun, FALSE);
3258 
3259 		/*
3260 		 * mpt_bus_reset is always successful in that it
3261 		 * will fall back to a hard reset should a bus
3262 		 * reset attempt fail.
3263 		 */
3264 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3265 		mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3266 		break;
3267 
3268 	case XPT_ABORT:
3269 	{
3270 		union ccb *accb = ccb->cab.abort_ccb;
3271 		switch (accb->ccb_h.func_code) {
3272 		case XPT_ACCEPT_TARGET_IO:
3273 		case XPT_IMMEDIATE_NOTIFY:
3274 			ccb->ccb_h.status = mpt_abort_target_ccb(mpt, ccb);
3275 			break;
3276 		case XPT_CONT_TARGET_IO:
3277 			mpt_prt(mpt, "cannot abort active CTIOs yet\n");
3278 			ccb->ccb_h.status = CAM_UA_ABORT;
3279 			break;
3280 		case XPT_SCSI_IO:
3281 			ccb->ccb_h.status = CAM_UA_ABORT;
3282 			break;
3283 		default:
3284 			ccb->ccb_h.status = CAM_REQ_INVALID;
3285 			break;
3286 		}
3287 		break;
3288 	}
3289 
3290 #define	IS_CURRENT_SETTINGS(c)	((c)->type == CTS_TYPE_CURRENT_SETTINGS)
3291 
3292 #define	DP_DISC_ENABLE	0x1
3293 #define	DP_DISC_DISABL	0x2
3294 #define	DP_DISC		(DP_DISC_ENABLE|DP_DISC_DISABL)
3295 
3296 #define	DP_TQING_ENABLE	0x4
3297 #define	DP_TQING_DISABL	0x8
3298 #define	DP_TQING	(DP_TQING_ENABLE|DP_TQING_DISABL)
3299 
3300 #define	DP_WIDE		0x10
3301 #define	DP_NARROW	0x20
3302 #define	DP_WIDTH	(DP_WIDE|DP_NARROW)
3303 
3304 #define	DP_SYNC		0x40
3305 
3306 	case XPT_SET_TRAN_SETTINGS:	/* Nexus Settings */
3307 	{
3308 		struct ccb_trans_settings_scsi *scsi;
3309 		struct ccb_trans_settings_spi *spi;
3310 		uint8_t dval;
3311 		u_int period;
3312 		u_int offset;
3313 		int i, j;
3314 
3315 		cts = &ccb->cts;
3316 
3317 		if (mpt->is_fc || mpt->is_sas) {
3318 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3319 			break;
3320 		}
3321 
3322 		scsi = &cts->proto_specific.scsi;
3323 		spi = &cts->xport_specific.spi;
3324 
3325 		/*
3326 		 * We can be called just to valid transport and proto versions
3327 		 */
3328 		if (scsi->valid == 0 && spi->valid == 0) {
3329 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3330 			break;
3331 		}
3332 
3333 		/*
3334 		 * Skip attempting settings on RAID volume disks.
3335 		 * Other devices on the bus get the normal treatment.
3336 		 */
3337 		if (mpt->phydisk_sim && raid_passthru == 0 &&
3338 		    mpt_is_raid_volume(mpt, tgt) != 0) {
3339 			mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3340 			    "no transfer settings for RAID vols\n");
3341 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3342 			break;
3343 		}
3344 
3345 		i = mpt->mpt_port_page2.PortSettings &
3346 		    MPI_SCSIPORTPAGE2_PORT_MASK_NEGO_MASTER_SETTINGS;
3347 		j = mpt->mpt_port_page2.PortFlags &
3348 		    MPI_SCSIPORTPAGE2_PORT_FLAGS_DV_MASK;
3349 		if (i == MPI_SCSIPORTPAGE2_PORT_ALL_MASTER_SETTINGS &&
3350 		    j == MPI_SCSIPORTPAGE2_PORT_FLAGS_OFF_DV) {
3351 			mpt_lprt(mpt, MPT_PRT_ALWAYS,
3352 			    "honoring BIOS transfer negotiations\n");
3353 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3354 			break;
3355 		}
3356 
3357 		dval = 0;
3358 		period = 0;
3359 		offset = 0;
3360 
3361 		if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3362 			dval |= ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0) ?
3363 			    DP_DISC_ENABLE : DP_DISC_DISABL;
3364 		}
3365 
3366 		if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3367 			dval |= ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) ?
3368 			    DP_TQING_ENABLE : DP_TQING_DISABL;
3369 		}
3370 
3371 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3372 			dval |= (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT) ?
3373 			    DP_WIDE : DP_NARROW;
3374 		}
3375 
3376 		if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
3377 			dval |= DP_SYNC;
3378 			offset = spi->sync_offset;
3379 		} else {
3380 			PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr =
3381 			    &mpt->mpt_dev_page1[tgt];
3382 			offset = ptr->RequestedParameters;
3383 			offset &= MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
3384 	    		offset >>= MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET;
3385 		}
3386 		if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
3387 			dval |= DP_SYNC;
3388 			period = spi->sync_period;
3389 		} else {
3390 			PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr =
3391 			    &mpt->mpt_dev_page1[tgt];
3392 			period = ptr->RequestedParameters;
3393 			period &= MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
3394 	    		period >>= MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD;
3395 		}
3396 
3397 		if (dval & DP_DISC_ENABLE) {
3398 			mpt->mpt_disc_enable |= (1 << tgt);
3399 		} else if (dval & DP_DISC_DISABL) {
3400 			mpt->mpt_disc_enable &= ~(1 << tgt);
3401 		}
3402 		if (dval & DP_TQING_ENABLE) {
3403 			mpt->mpt_tag_enable |= (1 << tgt);
3404 		} else if (dval & DP_TQING_DISABL) {
3405 			mpt->mpt_tag_enable &= ~(1 << tgt);
3406 		}
3407 		if (dval & DP_WIDTH) {
3408 			mpt_setwidth(mpt, tgt, 1);
3409 		}
3410 		if (dval & DP_SYNC) {
3411 			mpt_setsync(mpt, tgt, period, offset);
3412 		}
3413 		if (dval == 0) {
3414 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3415 			break;
3416 		}
3417 		mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3418 		    "set [%d]: 0x%x period 0x%x offset %d\n",
3419 		    tgt, dval, period, offset);
3420 		if (mpt_update_spi_config(mpt, tgt)) {
3421 			mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3422 		} else {
3423 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3424 		}
3425 		break;
3426 	}
3427 	case XPT_GET_TRAN_SETTINGS:
3428 	{
3429 		struct ccb_trans_settings_scsi *scsi;
3430 		cts = &ccb->cts;
3431 		cts->protocol = PROTO_SCSI;
3432 		if (mpt->is_fc) {
3433 			struct ccb_trans_settings_fc *fc =
3434 			    &cts->xport_specific.fc;
3435 			cts->protocol_version = SCSI_REV_SPC;
3436 			cts->transport = XPORT_FC;
3437 			cts->transport_version = 0;
3438 			if (mpt->mpt_fcport_speed != 0) {
3439 				fc->valid = CTS_FC_VALID_SPEED;
3440 				fc->bitrate = 100000 * mpt->mpt_fcport_speed;
3441 			}
3442 		} else if (mpt->is_sas) {
3443 			struct ccb_trans_settings_sas *sas =
3444 			    &cts->xport_specific.sas;
3445 			cts->protocol_version = SCSI_REV_SPC2;
3446 			cts->transport = XPORT_SAS;
3447 			cts->transport_version = 0;
3448 			sas->valid = CTS_SAS_VALID_SPEED;
3449 			sas->bitrate = 300000;
3450 		} else {
3451 			cts->protocol_version = SCSI_REV_2;
3452 			cts->transport = XPORT_SPI;
3453 			cts->transport_version = 2;
3454 			if (mpt_get_spi_settings(mpt, cts) != 0) {
3455 				mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3456 				break;
3457 			}
3458 		}
3459 		scsi = &cts->proto_specific.scsi;
3460 		scsi->valid = CTS_SCSI_VALID_TQ;
3461 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3462 		mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3463 		break;
3464 	}
3465 	case XPT_CALC_GEOMETRY:
3466 	{
3467 		struct ccb_calc_geometry *ccg;
3468 
3469 		ccg = &ccb->ccg;
3470 		if (ccg->block_size == 0) {
3471 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3472 			mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3473 			break;
3474 		}
3475 		cam_calc_geometry(ccg, /* extended */ 1);
3476 		KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d", __LINE__));
3477 		break;
3478 	}
3479 	case XPT_GET_SIM_KNOB:
3480 	{
3481 		struct ccb_sim_knob *kp = &ccb->knob;
3482 
3483 		if (mpt->is_fc) {
3484 			kp->xport_specific.fc.wwnn = mpt->scinfo.fc.wwnn;
3485 			kp->xport_specific.fc.wwpn = mpt->scinfo.fc.wwpn;
3486 			switch (mpt->role) {
3487 			case MPT_ROLE_NONE:
3488 				kp->xport_specific.fc.role = KNOB_ROLE_NONE;
3489 				break;
3490 			case MPT_ROLE_INITIATOR:
3491 				kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
3492 				break;
3493 			case MPT_ROLE_TARGET:
3494 				kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
3495 				break;
3496 			case MPT_ROLE_BOTH:
3497 				kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
3498 				break;
3499 			}
3500 			kp->xport_specific.fc.valid =
3501 			    KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
3502 			ccb->ccb_h.status = CAM_REQ_CMP;
3503 		} else {
3504 			ccb->ccb_h.status = CAM_REQ_INVALID;
3505 		}
3506 		xpt_done(ccb);
3507 		break;
3508 	}
3509 	case XPT_PATH_INQ:		/* Path routing inquiry */
3510 	{
3511 		struct ccb_pathinq *cpi = &ccb->cpi;
3512 
3513 		cpi->version_num = 1;
3514 		cpi->target_sprt = 0;
3515 		cpi->hba_eng_cnt = 0;
3516 		cpi->max_target = mpt->port_facts[0].MaxDevices - 1;
3517 		cpi->maxio = (mpt->max_cam_seg_cnt - 1) * PAGE_SIZE;
3518 		/*
3519 		 * FC cards report MAX_DEVICES of 512, but
3520 		 * the MSG_SCSI_IO_REQUEST target id field
3521 		 * is only 8 bits. Until we fix the driver
3522 		 * to support 'channels' for bus overflow,
3523 		 * just limit it.
3524 		 */
3525 		if (cpi->max_target > 255) {
3526 			cpi->max_target = 255;
3527 		}
3528 
3529 		/*
3530 		 * VMware ESX reports > 16 devices and then dies when we probe.
3531 		 */
3532 		if (mpt->is_spi && cpi->max_target > 15) {
3533 			cpi->max_target = 15;
3534 		}
3535 		if (mpt->is_spi)
3536 			cpi->max_lun = 7;
3537 		else
3538 			cpi->max_lun = MPT_MAX_LUNS;
3539 		cpi->initiator_id = mpt->mpt_ini_id;
3540 		cpi->bus_id = cam_sim_bus(sim);
3541 
3542 		/*
3543 		 * The base speed is the speed of the underlying connection.
3544 		 */
3545 		cpi->protocol = PROTO_SCSI;
3546 		if (mpt->is_fc) {
3547 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED |
3548 			    PIM_EXTLUNS;
3549 			cpi->base_transfer_speed = 100000;
3550 			cpi->hba_inquiry = PI_TAG_ABLE;
3551 			cpi->transport = XPORT_FC;
3552 			cpi->transport_version = 0;
3553 			cpi->protocol_version = SCSI_REV_SPC;
3554 			cpi->xport_specific.fc.wwnn = mpt->scinfo.fc.wwnn;
3555 			cpi->xport_specific.fc.wwpn = mpt->scinfo.fc.wwpn;
3556 			cpi->xport_specific.fc.port = mpt->scinfo.fc.portid;
3557 			cpi->xport_specific.fc.bitrate =
3558 			    100000 * mpt->mpt_fcport_speed;
3559 		} else if (mpt->is_sas) {
3560 			cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED |
3561 			    PIM_EXTLUNS;
3562 			cpi->base_transfer_speed = 300000;
3563 			cpi->hba_inquiry = PI_TAG_ABLE;
3564 			cpi->transport = XPORT_SAS;
3565 			cpi->transport_version = 0;
3566 			cpi->protocol_version = SCSI_REV_SPC2;
3567 		} else {
3568 			cpi->hba_misc = PIM_SEQSCAN | PIM_UNMAPPED |
3569 			    PIM_EXTLUNS;
3570 			cpi->base_transfer_speed = 3300;
3571 			cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3572 			cpi->transport = XPORT_SPI;
3573 			cpi->transport_version = 2;
3574 			cpi->protocol_version = SCSI_REV_2;
3575 		}
3576 
3577 		/*
3578 		 * We give our fake RAID passhtru bus a width that is MaxVolumes
3579 		 * wide and restrict it to one lun.
3580 		 */
3581 		if (raid_passthru) {
3582 			cpi->max_target = mpt->ioc_page2->MaxPhysDisks - 1;
3583 			cpi->initiator_id = cpi->max_target + 1;
3584 			cpi->max_lun = 0;
3585 		}
3586 
3587 		if ((mpt->role & MPT_ROLE_INITIATOR) == 0) {
3588 			cpi->hba_misc |= PIM_NOINITIATOR;
3589 		}
3590 		if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET)) {
3591 			cpi->target_sprt =
3592 			    PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
3593 		} else {
3594 			cpi->target_sprt = 0;
3595 		}
3596 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3597 		strlcpy(cpi->hba_vid, "LSI", HBA_IDLEN);
3598 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3599 		cpi->unit_number = cam_sim_unit(sim);
3600 		cpi->ccb_h.status = CAM_REQ_CMP;
3601 		break;
3602 	}
3603 	case XPT_EN_LUN:		/* Enable LUN as a target */
3604 	{
3605 		int result;
3606 
3607 		if (ccb->cel.enable)
3608 			result = mpt_enable_lun(mpt,
3609 			    ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3610 		else
3611 			result = mpt_disable_lun(mpt,
3612 			    ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3613 		if (result == 0) {
3614 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3615 		} else {
3616 			mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3617 		}
3618 		break;
3619 	}
3620 	case XPT_IMMEDIATE_NOTIFY:	/* Add Immediate Notify Resource */
3621 	case XPT_ACCEPT_TARGET_IO:	/* Add Accept Target IO Resource */
3622 	{
3623 		tgt_resource_t *trtp;
3624 		lun_id_t lun = ccb->ccb_h.target_lun;
3625 		ccb->ccb_h.sim_priv.entries[0].field = 0;
3626 		ccb->ccb_h.sim_priv.entries[1].ptr = mpt;
3627 
3628 		if (lun == CAM_LUN_WILDCARD) {
3629 			if (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
3630 				mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3631 				break;
3632 			}
3633 			trtp = &mpt->trt_wildcard;
3634 		} else if (lun >= MPT_MAX_LUNS) {
3635 			mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3636 			break;
3637 		} else {
3638 			trtp = &mpt->trt[lun];
3639 		}
3640 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
3641 			mpt_lprt(mpt, MPT_PRT_DEBUG1,
3642 			    "Put FREE ATIO %p lun %jx\n", ccb, (uintmax_t)lun);
3643 			STAILQ_INSERT_TAIL(&trtp->atios, &ccb->ccb_h,
3644 			    sim_links.stqe);
3645 		} else {
3646 			mpt_lprt(mpt, MPT_PRT_DEBUG1,
3647 			    "Put FREE INOT lun %jx\n", (uintmax_t)lun);
3648 			STAILQ_INSERT_TAIL(&trtp->inots, &ccb->ccb_h,
3649 			    sim_links.stqe);
3650 		}
3651 		mpt_set_ccb_status(ccb, CAM_REQ_INPROG);
3652 		return;
3653 	}
3654 	case XPT_NOTIFY_ACKNOWLEDGE:	/* Task management request done. */
3655 	{
3656 		request_t *req = MPT_TAG_2_REQ(mpt, ccb->cna2.tag_id);
3657 
3658 		mpt_lprt(mpt, MPT_PRT_DEBUG, "Got Notify ACK\n");
3659 		mpt_scsi_tgt_status(mpt, NULL, req, 0, NULL, 0);
3660 		mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3661 		break;
3662 	}
3663 	case XPT_CONT_TARGET_IO:
3664 		mpt_target_start_io(mpt, ccb);
3665 		return;
3666 
3667 	default:
3668 		ccb->ccb_h.status = CAM_REQ_INVALID;
3669 		break;
3670 	}
3671 	xpt_done(ccb);
3672 }
3673 
3674 static int
mpt_get_spi_settings(struct mpt_softc * mpt,struct ccb_trans_settings * cts)3675 mpt_get_spi_settings(struct mpt_softc *mpt, struct ccb_trans_settings *cts)
3676 {
3677 	struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3678 	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3679 	target_id_t tgt;
3680 	uint32_t dval, pval, oval;
3681 	int rv;
3682 
3683 	if (IS_CURRENT_SETTINGS(cts) == 0) {
3684 		tgt = cts->ccb_h.target_id;
3685 	} else if (xpt_path_sim(cts->ccb_h.path) == mpt->phydisk_sim) {
3686 		if (mpt_map_physdisk(mpt, (union ccb *)cts, &tgt)) {
3687 			return (-1);
3688 		}
3689 	} else {
3690 		tgt = cts->ccb_h.target_id;
3691 	}
3692 
3693 	/*
3694 	 * We aren't looking at Port Page 2 BIOS settings here-
3695 	 * sometimes these have been known to be bogus XXX.
3696 	 *
3697 	 * For user settings, we pick the max from port page 0
3698 	 *
3699 	 * For current settings we read the current settings out from
3700 	 * device page 0 for that target.
3701 	 */
3702 	if (IS_CURRENT_SETTINGS(cts)) {
3703 		CONFIG_PAGE_SCSI_DEVICE_0 tmp;
3704 		dval = 0;
3705 
3706 		tmp = mpt->mpt_dev_page0[tgt];
3707 		rv = mpt_read_cur_cfg_page(mpt, tgt, &tmp.Header,
3708 		    sizeof(tmp), FALSE, 5000);
3709 		if (rv) {
3710 			mpt_prt(mpt, "can't get tgt %d config page 0\n", tgt);
3711 			return (rv);
3712 		}
3713 		mpt2host_config_page_scsi_device_0(&tmp);
3714 
3715 		mpt_lprt(mpt, MPT_PRT_DEBUG,
3716 		    "mpt_get_spi_settings[%d]: current NP %x Info %x\n", tgt,
3717 		    tmp.NegotiatedParameters, tmp.Information);
3718 		dval |= (tmp.NegotiatedParameters & MPI_SCSIDEVPAGE0_NP_WIDE) ?
3719 		    DP_WIDE : DP_NARROW;
3720 		dval |= (mpt->mpt_disc_enable & (1 << tgt)) ?
3721 		    DP_DISC_ENABLE : DP_DISC_DISABL;
3722 		dval |= (mpt->mpt_tag_enable & (1 << tgt)) ?
3723 		    DP_TQING_ENABLE : DP_TQING_DISABL;
3724 		oval = tmp.NegotiatedParameters;
3725 		oval &= MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK;
3726 		oval >>= MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_OFFSET;
3727 		pval = tmp.NegotiatedParameters;
3728 		pval &= MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK;
3729 		pval >>= MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_PERIOD;
3730 		mpt->mpt_dev_page0[tgt] = tmp;
3731 	} else {
3732 		dval = DP_WIDE|DP_DISC_ENABLE|DP_TQING_ENABLE|DP_SYNC;
3733 		oval = mpt->mpt_port_page0.Capabilities;
3734 		oval = MPI_SCSIPORTPAGE0_CAP_GET_MAX_SYNC_OFFSET(oval);
3735 		pval = mpt->mpt_port_page0.Capabilities;
3736 		pval = MPI_SCSIPORTPAGE0_CAP_GET_MIN_SYNC_PERIOD(pval);
3737 	}
3738 
3739 	spi->valid = 0;
3740 	scsi->valid = 0;
3741 	spi->flags = 0;
3742 	scsi->flags = 0;
3743 	spi->sync_offset = oval;
3744 	spi->sync_period = pval;
3745 	spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3746 	spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3747 	spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3748 	if (dval & DP_WIDE) {
3749 		spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3750 	} else {
3751 		spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3752 	}
3753 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3754 		scsi->valid = CTS_SCSI_VALID_TQ;
3755 		if (dval & DP_TQING_ENABLE) {
3756 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3757 		}
3758 		spi->valid |= CTS_SPI_VALID_DISC;
3759 		if (dval & DP_DISC_ENABLE) {
3760 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3761 		}
3762 	}
3763 
3764 	mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3765 	    "mpt_get_spi_settings[%d]: %s flags 0x%x per 0x%x off=%d\n", tgt,
3766 	    IS_CURRENT_SETTINGS(cts) ? "ACTIVE" : "NVRAM ", dval, pval, oval);
3767 	return (0);
3768 }
3769 
3770 static void
mpt_setwidth(struct mpt_softc * mpt,int tgt,int onoff)3771 mpt_setwidth(struct mpt_softc *mpt, int tgt, int onoff)
3772 {
3773 	PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr;
3774 
3775 	ptr = &mpt->mpt_dev_page1[tgt];
3776 	if (onoff) {
3777 		ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_WIDE;
3778 	} else {
3779 		ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_WIDE;
3780 	}
3781 }
3782 
3783 static void
mpt_setsync(struct mpt_softc * mpt,int tgt,int period,int offset)3784 mpt_setsync(struct mpt_softc *mpt, int tgt, int period, int offset)
3785 {
3786 	PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr;
3787 
3788 	ptr = &mpt->mpt_dev_page1[tgt];
3789 	ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
3790 	ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
3791 	ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_DT;
3792 	ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_QAS;
3793 	ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_IU;
3794 	if (period == 0) {
3795 		return;
3796 	}
3797 	ptr->RequestedParameters |=
3798 	    period << MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD;
3799 	ptr->RequestedParameters |=
3800 	    offset << MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET;
3801 	if (period < 0xa) {
3802 		ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_DT;
3803 	}
3804 	if (period < 0x9) {
3805 		ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_QAS;
3806 		ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_IU;
3807 	}
3808 }
3809 
3810 static int
mpt_update_spi_config(struct mpt_softc * mpt,int tgt)3811 mpt_update_spi_config(struct mpt_softc *mpt, int tgt)
3812 {
3813 	CONFIG_PAGE_SCSI_DEVICE_1 tmp;
3814 	int rv;
3815 
3816 	mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3817 	    "mpt_update_spi_config[%d].page1: Requested Params 0x%08x\n",
3818 	    tgt, mpt->mpt_dev_page1[tgt].RequestedParameters);
3819 	tmp = mpt->mpt_dev_page1[tgt];
3820 	host2mpt_config_page_scsi_device_1(&tmp);
3821 	rv = mpt_write_cur_cfg_page(mpt, tgt,
3822 	    &tmp.Header, sizeof(tmp), FALSE, 5000);
3823 	if (rv) {
3824 		mpt_prt(mpt, "mpt_update_spi_config: write cur page failed\n");
3825 		return (-1);
3826 	}
3827 	return (0);
3828 }
3829 
3830 /****************************** Timeout Recovery ******************************/
3831 static int
mpt_spawn_recovery_thread(struct mpt_softc * mpt)3832 mpt_spawn_recovery_thread(struct mpt_softc *mpt)
3833 {
3834 	int error;
3835 
3836 	error = kproc_create(mpt_recovery_thread, mpt,
3837 	    &mpt->recovery_thread, /*flags*/0,
3838 	    /*altstack*/0, "mpt_recovery%d", mpt->unit);
3839 	return (error);
3840 }
3841 
3842 static void
mpt_terminate_recovery_thread(struct mpt_softc * mpt)3843 mpt_terminate_recovery_thread(struct mpt_softc *mpt)
3844 {
3845 
3846 	if (mpt->recovery_thread == NULL) {
3847 		return;
3848 	}
3849 	mpt->shutdwn_recovery = 1;
3850 	wakeup(mpt);
3851 	/*
3852 	 * Sleep on a slightly different location
3853 	 * for this interlock just for added safety.
3854 	 */
3855 	mpt_sleep(mpt, &mpt->recovery_thread, PUSER, "thtrm", 0);
3856 }
3857 
3858 static void
mpt_recovery_thread(void * arg)3859 mpt_recovery_thread(void *arg)
3860 {
3861 	struct mpt_softc *mpt;
3862 
3863 	mpt = (struct mpt_softc *)arg;
3864 	MPT_LOCK(mpt);
3865 	for (;;) {
3866 		if (TAILQ_EMPTY(&mpt->request_timeout_list) != 0) {
3867 			if (mpt->shutdwn_recovery == 0) {
3868 				mpt_sleep(mpt, mpt, PUSER, "idle", 0);
3869 			}
3870 		}
3871 		if (mpt->shutdwn_recovery != 0) {
3872 			break;
3873 		}
3874 		mpt_recover_commands(mpt);
3875 	}
3876 	mpt->recovery_thread = NULL;
3877 	wakeup(&mpt->recovery_thread);
3878 	MPT_UNLOCK(mpt);
3879 	kproc_exit(0);
3880 }
3881 
3882 static int
mpt_scsi_send_tmf(struct mpt_softc * mpt,u_int type,u_int flags,u_int channel,target_id_t target,lun_id_t lun,u_int abort_ctx,int sleep_ok)3883 mpt_scsi_send_tmf(struct mpt_softc *mpt, u_int type, u_int flags,
3884     u_int channel, target_id_t target, lun_id_t lun, u_int abort_ctx,
3885     int sleep_ok)
3886 {
3887 	MSG_SCSI_TASK_MGMT *tmf_req;
3888 	int		    error;
3889 
3890 	/*
3891 	 * Wait for any current TMF request to complete.
3892 	 * We're only allowed to issue one TMF at a time.
3893 	 */
3894 	error = mpt_wait_req(mpt, mpt->tmf_req, REQ_STATE_FREE, REQ_STATE_FREE,
3895 	    sleep_ok, MPT_TMF_MAX_TIMEOUT);
3896 	if (error != 0) {
3897 		mpt_reset(mpt, TRUE);
3898 		return (ETIMEDOUT);
3899 	}
3900 
3901 	mpt_assign_serno(mpt, mpt->tmf_req);
3902 	mpt->tmf_req->state = REQ_STATE_ALLOCATED|REQ_STATE_QUEUED;
3903 
3904 	tmf_req = (MSG_SCSI_TASK_MGMT *)mpt->tmf_req->req_vbuf;
3905 	memset(tmf_req, 0, sizeof(*tmf_req));
3906 	tmf_req->TargetID = target;
3907 	tmf_req->Bus = channel;
3908 	tmf_req->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
3909 	tmf_req->TaskType = type;
3910 	tmf_req->MsgFlags = flags;
3911 	tmf_req->MsgContext =
3912 	    htole32(mpt->tmf_req->index | scsi_tmf_handler_id);
3913 	be64enc(tmf_req->LUN, CAM_EXTLUN_BYTE_SWIZZLE(lun));
3914 	tmf_req->TaskMsgContext = abort_ctx;
3915 
3916 	mpt_lprt(mpt, MPT_PRT_DEBUG,
3917 	    "Issuing TMF %p:%u with MsgContext of 0x%x\n", mpt->tmf_req,
3918 	    mpt->tmf_req->serno, tmf_req->MsgContext);
3919 	if (mpt->verbose > MPT_PRT_DEBUG) {
3920 		mpt_print_request(tmf_req);
3921 	}
3922 
3923 	KASSERT(mpt_req_on_pending_list(mpt, mpt->tmf_req) == 0,
3924 	    ("mpt_scsi_send_tmf: tmf_req already on pending list"));
3925 	TAILQ_INSERT_HEAD(&mpt->request_pending_list, mpt->tmf_req, links);
3926 	error = mpt_send_handshake_cmd(mpt, sizeof(*tmf_req), tmf_req);
3927 	if (error != MPT_OK) {
3928 		TAILQ_REMOVE(&mpt->request_pending_list, mpt->tmf_req, links);
3929 		mpt->tmf_req->state = REQ_STATE_FREE;
3930 		mpt_reset(mpt, TRUE);
3931 	}
3932 	return (error);
3933 }
3934 
3935 /*
3936  * When a command times out, it is placed on the requeust_timeout_list
3937  * and we wake our recovery thread.  The MPT-Fusion architecture supports
3938  * only a single TMF operation at a time, so we serially abort/bdr, etc,
3939  * the timedout transactions.  The next TMF is issued either by the
3940  * completion handler of the current TMF waking our recovery thread,
3941  * or the TMF timeout handler causing a hard reset sequence.
3942  */
3943 static void
mpt_recover_commands(struct mpt_softc * mpt)3944 mpt_recover_commands(struct mpt_softc *mpt)
3945 {
3946 	request_t	   *req;
3947 	union ccb	   *ccb;
3948 	int		    error;
3949 
3950 	if (TAILQ_EMPTY(&mpt->request_timeout_list) != 0) {
3951 		/*
3952 		 * No work to do- leave.
3953 		 */
3954 		mpt_prt(mpt, "mpt_recover_commands: no requests.\n");
3955 		return;
3956 	}
3957 
3958 	/*
3959 	 * Flush any commands whose completion coincides with their timeout.
3960 	 */
3961 	mpt_intr(mpt);
3962 
3963 	if (TAILQ_EMPTY(&mpt->request_timeout_list) != 0) {
3964 		/*
3965 		 * The timedout commands have already
3966 		 * completed.  This typically means
3967 		 * that either the timeout value was on
3968 		 * the hairy edge of what the device
3969 		 * requires or - more likely - interrupts
3970 		 * are not happening.
3971 		 */
3972 		mpt_prt(mpt, "Timedout requests already complete. "
3973 		    "Interrupts may not be functioning.\n");
3974 		mpt_enable_ints(mpt);
3975 		return;
3976 	}
3977 
3978 	/*
3979 	 * We have no visibility into the current state of the
3980 	 * controller, so attempt to abort the commands in the
3981 	 * order they timed-out. For initiator commands, we
3982 	 * depend on the reply handler pulling requests off
3983 	 * the timeout list.
3984 	 */
3985 	while ((req = TAILQ_FIRST(&mpt->request_timeout_list)) != NULL) {
3986 		uint16_t status;
3987 		uint8_t response;
3988 		MSG_REQUEST_HEADER *hdrp = req->req_vbuf;
3989 
3990 		mpt_prt(mpt, "attempting to abort req %p:%u function %x\n",
3991 		    req, req->serno, hdrp->Function);
3992 		ccb = req->ccb;
3993 		if (ccb == NULL) {
3994 			mpt_prt(mpt, "null ccb in timed out request. "
3995 			    "Resetting Controller.\n");
3996 			mpt_reset(mpt, TRUE);
3997 			continue;
3998 		}
3999 		mpt_set_ccb_status(ccb, CAM_CMD_TIMEOUT);
4000 
4001 		/*
4002 		 * Check to see if this is not an initiator command and
4003 		 * deal with it differently if it is.
4004 		 */
4005 		switch (hdrp->Function) {
4006 		case MPI_FUNCTION_SCSI_IO_REQUEST:
4007 		case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
4008 			break;
4009 		default:
4010 			/*
4011 			 * XXX: FIX ME: need to abort target assists...
4012 			 */
4013 			mpt_prt(mpt, "just putting it back on the pend q\n");
4014 			TAILQ_REMOVE(&mpt->request_timeout_list, req, links);
4015 			TAILQ_INSERT_HEAD(&mpt->request_pending_list, req,
4016 			    links);
4017 			continue;
4018 		}
4019 
4020 		error = mpt_scsi_send_tmf(mpt,
4021 		    MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4022 		    0, 0, ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
4023 		    htole32(req->index | scsi_io_handler_id), TRUE);
4024 
4025 		if (error != 0) {
4026 			/*
4027 			 * mpt_scsi_send_tmf hard resets on failure, so no
4028 			 * need to do so here.  Our queue should be emptied
4029 			 * by the hard reset.
4030 			 */
4031 			continue;
4032 		}
4033 
4034 		error = mpt_wait_req(mpt, mpt->tmf_req, REQ_STATE_DONE,
4035 		    REQ_STATE_DONE, TRUE, 500);
4036 
4037 		status = le16toh(mpt->tmf_req->IOCStatus);
4038 		response = mpt->tmf_req->ResponseCode;
4039 		mpt->tmf_req->state = REQ_STATE_FREE;
4040 
4041 		if (error != 0) {
4042 			/*
4043 			 * If we've errored out,, reset the controller.
4044 			 */
4045 			mpt_prt(mpt, "mpt_recover_commands: abort timed-out. "
4046 			    "Resetting controller\n");
4047 			mpt_reset(mpt, TRUE);
4048 			continue;
4049 		}
4050 
4051 		if ((status & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
4052 			mpt_prt(mpt, "mpt_recover_commands: IOC Status 0x%x. "
4053 			    "Resetting controller.\n", status);
4054 			mpt_reset(mpt, TRUE);
4055 			continue;
4056 		}
4057 
4058 		if (response != MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED &&
4059 		    response != MPI_SCSITASKMGMT_RSP_TM_COMPLETE) {
4060 			mpt_prt(mpt, "mpt_recover_commands: TMF Response 0x%x. "
4061 			    "Resetting controller.\n", response);
4062 			mpt_reset(mpt, TRUE);
4063 			continue;
4064 		}
4065 		mpt_prt(mpt, "abort of req %p:%u completed\n", req, req->serno);
4066 	}
4067 }
4068 
4069 /************************ Target Mode Support ****************************/
4070 static void
mpt_fc_post_els(struct mpt_softc * mpt,request_t * req,int ioindex)4071 mpt_fc_post_els(struct mpt_softc *mpt, request_t *req, int ioindex)
4072 {
4073 	MSG_LINK_SERVICE_BUFFER_POST_REQUEST *fc;
4074 	PTR_SGE_TRANSACTION32 tep;
4075 	PTR_SGE_SIMPLE32 se;
4076 	bus_addr_t paddr;
4077 	uint32_t fl;
4078 
4079 	paddr = req->req_pbuf;
4080 	paddr += MPT_RQSL(mpt);
4081 
4082 	fc = req->req_vbuf;
4083 	memset(fc, 0, MPT_REQUEST_AREA);
4084 	fc->BufferCount = 1;
4085 	fc->Function = MPI_FUNCTION_FC_LINK_SRVC_BUF_POST;
4086 	fc->MsgContext = htole32(req->index | fc_els_handler_id);
4087 
4088 	/*
4089 	 * Okay, set up ELS buffer pointers. ELS buffer pointers
4090 	 * consist of a TE SGL element (with details length of zero)
4091 	 * followed by a SIMPLE SGL element which holds the address
4092 	 * of the buffer.
4093 	 */
4094 
4095 	tep = (PTR_SGE_TRANSACTION32) &fc->SGL;
4096 
4097 	tep->ContextSize = 4;
4098 	tep->Flags = 0;
4099 	tep->TransactionContext[0] = htole32(ioindex);
4100 
4101 	se = (PTR_SGE_SIMPLE32) &tep->TransactionDetails[0];
4102 	fl =
4103 		MPI_SGE_FLAGS_HOST_TO_IOC	|
4104 		MPI_SGE_FLAGS_SIMPLE_ELEMENT	|
4105 		MPI_SGE_FLAGS_LAST_ELEMENT	|
4106 		MPI_SGE_FLAGS_END_OF_LIST	|
4107 		MPI_SGE_FLAGS_END_OF_BUFFER;
4108 	fl <<= MPI_SGE_FLAGS_SHIFT;
4109 	fl |= (MPT_NRFM(mpt) - MPT_RQSL(mpt));
4110 	se->FlagsLength = htole32(fl);
4111 	se->Address = htole32((uint32_t) paddr);
4112 	mpt_lprt(mpt, MPT_PRT_DEBUG,
4113 	    "add ELS index %d ioindex %d for %p:%u\n",
4114 	    req->index, ioindex, req, req->serno);
4115 	KASSERT(((req->state & REQ_STATE_LOCKED) != 0),
4116 	    ("mpt_fc_post_els: request not locked"));
4117 	mpt_send_cmd(mpt, req);
4118 }
4119 
4120 static void
mpt_post_target_command(struct mpt_softc * mpt,request_t * req,int ioindex)4121 mpt_post_target_command(struct mpt_softc *mpt, request_t *req, int ioindex)
4122 {
4123 	PTR_MSG_TARGET_CMD_BUFFER_POST_REQUEST fc;
4124 	PTR_CMD_BUFFER_DESCRIPTOR cb;
4125 	bus_addr_t paddr;
4126 
4127 	paddr = req->req_pbuf;
4128 	paddr += MPT_RQSL(mpt);
4129 	memset(req->req_vbuf, 0, MPT_REQUEST_AREA);
4130 	MPT_TGT_STATE(mpt, req)->state = TGT_STATE_LOADING;
4131 
4132 	fc = req->req_vbuf;
4133 	fc->BufferCount = 1;
4134 	fc->Function = MPI_FUNCTION_TARGET_CMD_BUFFER_POST;
4135 	fc->BufferLength = MIN(MPT_REQUEST_AREA - MPT_RQSL(mpt), UINT8_MAX);
4136 	fc->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4137 
4138 	cb = &fc->Buffer[0];
4139 	cb->IoIndex = htole16(ioindex);
4140 	cb->u.PhysicalAddress32 = htole32((U32) paddr);
4141 
4142 	mpt_check_doorbell(mpt);
4143 	mpt_send_cmd(mpt, req);
4144 }
4145 
4146 static int
mpt_add_els_buffers(struct mpt_softc * mpt)4147 mpt_add_els_buffers(struct mpt_softc *mpt)
4148 {
4149 	int i;
4150 
4151 	if (mpt->is_fc == 0) {
4152 		return (TRUE);
4153 	}
4154 
4155 	if (mpt->els_cmds_allocated) {
4156 		return (TRUE);
4157 	}
4158 
4159 	mpt->els_cmd_ptrs = malloc(MPT_MAX_ELS * sizeof (request_t *),
4160 	    M_DEVBUF, M_NOWAIT | M_ZERO);
4161 
4162 	if (mpt->els_cmd_ptrs == NULL) {
4163 		return (FALSE);
4164 	}
4165 
4166 	/*
4167 	 * Feed the chip some ELS buffer resources
4168 	 */
4169 	for (i = 0; i < MPT_MAX_ELS; i++) {
4170 		request_t *req = mpt_get_request(mpt, FALSE);
4171 		if (req == NULL) {
4172 			break;
4173 		}
4174 		req->state |= REQ_STATE_LOCKED;
4175 		mpt->els_cmd_ptrs[i] = req;
4176 		mpt_fc_post_els(mpt, req, i);
4177 	}
4178 
4179 	if (i == 0) {
4180 		mpt_prt(mpt, "unable to add ELS buffer resources\n");
4181 		free(mpt->els_cmd_ptrs, M_DEVBUF);
4182 		mpt->els_cmd_ptrs = NULL;
4183 		return (FALSE);
4184 	}
4185 	if (i != MPT_MAX_ELS) {
4186 		mpt_lprt(mpt, MPT_PRT_INFO,
4187 		    "only added %d of %d  ELS buffers\n", i, MPT_MAX_ELS);
4188 	}
4189 	mpt->els_cmds_allocated = i;
4190 	return(TRUE);
4191 }
4192 
4193 static int
mpt_add_target_commands(struct mpt_softc * mpt)4194 mpt_add_target_commands(struct mpt_softc *mpt)
4195 {
4196 	int i, max;
4197 
4198 	if (mpt->tgt_cmd_ptrs) {
4199 		return (TRUE);
4200 	}
4201 
4202 	max = MPT_MAX_REQUESTS(mpt) >> 1;
4203 	if (max > mpt->mpt_max_tgtcmds) {
4204 		max = mpt->mpt_max_tgtcmds;
4205 	}
4206 	mpt->tgt_cmd_ptrs =
4207 	    malloc(max * sizeof (request_t *), M_DEVBUF, M_NOWAIT | M_ZERO);
4208 	if (mpt->tgt_cmd_ptrs == NULL) {
4209 		mpt_prt(mpt,
4210 		    "mpt_add_target_commands: could not allocate cmd ptrs\n");
4211 		return (FALSE);
4212 	}
4213 
4214 	for (i = 0; i < max; i++) {
4215 		request_t *req;
4216 
4217 		req = mpt_get_request(mpt, FALSE);
4218 		if (req == NULL) {
4219 			break;
4220 		}
4221 		req->state |= REQ_STATE_LOCKED;
4222 		mpt->tgt_cmd_ptrs[i] = req;
4223 		mpt_post_target_command(mpt, req, i);
4224 	}
4225 
4226 	if (i == 0) {
4227 		mpt_lprt(mpt, MPT_PRT_ERROR, "could not add any target bufs\n");
4228 		free(mpt->tgt_cmd_ptrs, M_DEVBUF);
4229 		mpt->tgt_cmd_ptrs = NULL;
4230 		return (FALSE);
4231 	}
4232 
4233 	mpt->tgt_cmds_allocated = i;
4234 
4235 	if (i < max) {
4236 		mpt_lprt(mpt, MPT_PRT_INFO,
4237 		    "added %d of %d target bufs\n", i, max);
4238 	}
4239 	return (i);
4240 }
4241 
4242 static int
mpt_enable_lun(struct mpt_softc * mpt,target_id_t tgt,lun_id_t lun)4243 mpt_enable_lun(struct mpt_softc *mpt, target_id_t tgt, lun_id_t lun)
4244 {
4245 
4246 	if (tgt == CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
4247 		mpt->twildcard = 1;
4248 	} else if (lun >= MPT_MAX_LUNS) {
4249 		return (EINVAL);
4250 	} else if (tgt != CAM_TARGET_WILDCARD && tgt != 0) {
4251 		return (EINVAL);
4252 	}
4253 	if (mpt->tenabled == 0) {
4254 		if (mpt->is_fc) {
4255 			(void) mpt_fc_reset_link(mpt, 0);
4256 		}
4257 		mpt->tenabled = 1;
4258 	}
4259 	if (lun == CAM_LUN_WILDCARD) {
4260 		mpt->trt_wildcard.enabled = 1;
4261 	} else {
4262 		mpt->trt[lun].enabled = 1;
4263 	}
4264 	return (0);
4265 }
4266 
4267 static int
mpt_disable_lun(struct mpt_softc * mpt,target_id_t tgt,lun_id_t lun)4268 mpt_disable_lun(struct mpt_softc *mpt, target_id_t tgt, lun_id_t lun)
4269 {
4270 	int i;
4271 
4272 	if (tgt == CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
4273 		mpt->twildcard = 0;
4274 	} else if (lun >= MPT_MAX_LUNS) {
4275 		return (EINVAL);
4276 	} else if (tgt != CAM_TARGET_WILDCARD && tgt != 0) {
4277 		return (EINVAL);
4278 	}
4279 	if (lun == CAM_LUN_WILDCARD) {
4280 		mpt->trt_wildcard.enabled = 0;
4281 	} else {
4282 		mpt->trt[lun].enabled = 0;
4283 	}
4284 	for (i = 0; i < MPT_MAX_LUNS; i++) {
4285 		if (mpt->trt[i].enabled) {
4286 			break;
4287 		}
4288 	}
4289 	if (i == MPT_MAX_LUNS && mpt->twildcard == 0) {
4290 		if (mpt->is_fc) {
4291 			(void) mpt_fc_reset_link(mpt, 0);
4292 		}
4293 		mpt->tenabled = 0;
4294 	}
4295 	return (0);
4296 }
4297 
4298 /*
4299  * Called with MPT lock held
4300  */
4301 static void
mpt_target_start_io(struct mpt_softc * mpt,union ccb * ccb)4302 mpt_target_start_io(struct mpt_softc *mpt, union ccb *ccb)
4303 {
4304 	struct ccb_scsiio *csio = &ccb->csio;
4305 	request_t *cmd_req = MPT_TAG_2_REQ(mpt, csio->tag_id);
4306 	mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, cmd_req);
4307 
4308 	switch (tgt->state) {
4309 	case TGT_STATE_IN_CAM:
4310 		break;
4311 	case TGT_STATE_MOVING_DATA:
4312 		mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
4313 		xpt_freeze_simq(mpt->sim, 1);
4314 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4315 		tgt->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
4316 		xpt_done(ccb);
4317 		return;
4318 	default:
4319 		mpt_prt(mpt, "ccb %p flags 0x%x tag 0x%08x had bad request "
4320 		    "starting I/O\n", ccb, csio->ccb_h.flags, csio->tag_id);
4321 		mpt_tgt_dump_req_state(mpt, cmd_req);
4322 		mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
4323 		xpt_done(ccb);
4324 		return;
4325 	}
4326 
4327 	if (csio->dxfer_len) {
4328 		bus_dmamap_callback_t *cb;
4329 		PTR_MSG_TARGET_ASSIST_REQUEST ta;
4330 		request_t *req;
4331 		int error;
4332 
4333 		KASSERT((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE,
4334 		    ("dxfer_len %u but direction is NONE", csio->dxfer_len));
4335 
4336 		if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
4337 			if (mpt->outofbeer == 0) {
4338 				mpt->outofbeer = 1;
4339 				xpt_freeze_simq(mpt->sim, 1);
4340 				mpt_lprt(mpt, MPT_PRT_DEBUG, "FREEZEQ\n");
4341 			}
4342 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4343 			mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
4344 			xpt_done(ccb);
4345 			return;
4346 		}
4347 		ccb->ccb_h.status = CAM_SIM_QUEUED | CAM_REQ_INPROG;
4348 		if (sizeof (bus_addr_t) > 4) {
4349 			cb = mpt_execute_req_a64;
4350 		} else {
4351 			cb = mpt_execute_req;
4352 		}
4353 
4354 		req->ccb = ccb;
4355 		ccb->ccb_h.ccb_req_ptr = req;
4356 
4357 		/*
4358 		 * Record the currently active ccb and the
4359 		 * request for it in our target state area.
4360 		 */
4361 		tgt->ccb = ccb;
4362 		tgt->req = req;
4363 
4364 		memset(req->req_vbuf, 0, MPT_RQSL(mpt));
4365 		ta = req->req_vbuf;
4366 
4367 		if (mpt->is_sas) {
4368 			PTR_MPI_TARGET_SSP_CMD_BUFFER ssp =
4369 			     cmd_req->req_vbuf;
4370 			ta->QueueTag = ssp->InitiatorTag;
4371 		} else if (mpt->is_spi) {
4372 			PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp =
4373 			     cmd_req->req_vbuf;
4374 			ta->QueueTag = sp->Tag;
4375 		}
4376 		ta->Function = MPI_FUNCTION_TARGET_ASSIST;
4377 		ta->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4378 		ta->ReplyWord = htole32(tgt->reply_desc);
4379 		be64enc(ta->LUN, CAM_EXTLUN_BYTE_SWIZZLE(csio->ccb_h.target_lun));
4380 
4381 		ta->RelativeOffset = tgt->bytes_xfered;
4382 		ta->DataLength = ccb->csio.dxfer_len;
4383 		if (ta->DataLength > tgt->resid) {
4384 			ta->DataLength = tgt->resid;
4385 		}
4386 
4387 		/*
4388 		 * XXX Should be done after data transfer completes?
4389 		 */
4390 		csio->resid = csio->dxfer_len - ta->DataLength;
4391 		tgt->resid -= csio->dxfer_len;
4392 		tgt->bytes_xfered += csio->dxfer_len;
4393 
4394 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4395 			ta->TargetAssistFlags |=
4396 			    TARGET_ASSIST_FLAGS_DATA_DIRECTION;
4397 		}
4398 
4399 #ifdef	WE_TRUST_AUTO_GOOD_STATUS
4400 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) &&
4401 		    csio->scsi_status == SCSI_STATUS_OK && tgt->resid == 0) {
4402 			ta->TargetAssistFlags |=
4403 			    TARGET_ASSIST_FLAGS_AUTO_STATUS;
4404 		}
4405 #endif
4406 		tgt->state = TGT_STATE_SETTING_UP_FOR_DATA;
4407 
4408 		mpt_lprt(mpt, MPT_PRT_DEBUG,
4409 		    "DATA_CCB %p tag %x %u bytes %u resid flg %x req %p:%u "
4410 		    "nxtstate=%d\n", csio, csio->tag_id, csio->dxfer_len,
4411 		    tgt->resid, ccb->ccb_h.flags, req, req->serno, tgt->state);
4412 
4413 		error = bus_dmamap_load_ccb(mpt->buffer_dmat, req->dmap, ccb,
4414 		    cb, req, 0);
4415 		if (error == EINPROGRESS) {
4416 			xpt_freeze_simq(mpt->sim, 1);
4417 			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
4418 		}
4419 	} else {
4420 		/*
4421 		 * XXX: I don't know why this seems to happen, but
4422 		 * XXX: completing the CCB seems to make things happy.
4423 		 * XXX: This seems to happen if the initiator requests
4424 		 * XXX: enough data that we have to do multiple CTIOs.
4425 		 */
4426 		if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
4427 			mpt_lprt(mpt, MPT_PRT_DEBUG,
4428 			    "Meaningless STATUS CCB (%p): flags %x status %x "
4429 			    "resid %d bytes_xfered %u\n", ccb, ccb->ccb_h.flags,
4430 			    ccb->ccb_h.status, tgt->resid, tgt->bytes_xfered);
4431 			mpt_set_ccb_status(ccb, CAM_REQ_CMP);
4432 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4433 			xpt_done(ccb);
4434 			return;
4435 		}
4436 		mpt_scsi_tgt_status(mpt, ccb, cmd_req, csio->scsi_status,
4437 		    (void *)&csio->sense_data,
4438 		    (ccb->ccb_h.flags & CAM_SEND_SENSE) ?
4439 		     csio->sense_len : 0);
4440 	}
4441 }
4442 
4443 static void
mpt_scsi_tgt_local(struct mpt_softc * mpt,request_t * cmd_req,lun_id_t lun,int send,uint8_t * data,size_t length)4444 mpt_scsi_tgt_local(struct mpt_softc *mpt, request_t *cmd_req,
4445     lun_id_t lun, int send, uint8_t *data, size_t length)
4446 {
4447 	mpt_tgt_state_t *tgt;
4448 	PTR_MSG_TARGET_ASSIST_REQUEST ta;
4449 	SGE_SIMPLE32 *se;
4450 	uint32_t flags;
4451 	uint8_t *dptr;
4452 	bus_addr_t pptr;
4453 	request_t *req;
4454 
4455 	/*
4456 	 * We enter with resid set to the data load for the command.
4457 	 */
4458 	tgt = MPT_TGT_STATE(mpt, cmd_req);
4459 	if (length == 0 || tgt->resid == 0) {
4460 		tgt->resid = 0;
4461 		mpt_scsi_tgt_status(mpt, NULL, cmd_req, 0, NULL, 0);
4462 		return;
4463 	}
4464 
4465 	if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
4466 		mpt_prt(mpt, "out of resources- dropping local response\n");
4467 		return;
4468 	}
4469 	tgt->is_local = 1;
4470 
4471 	memset(req->req_vbuf, 0, MPT_RQSL(mpt));
4472 	ta = req->req_vbuf;
4473 
4474 	if (mpt->is_sas) {
4475 		PTR_MPI_TARGET_SSP_CMD_BUFFER ssp = cmd_req->req_vbuf;
4476 		ta->QueueTag = ssp->InitiatorTag;
4477 	} else if (mpt->is_spi) {
4478 		PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp = cmd_req->req_vbuf;
4479 		ta->QueueTag = sp->Tag;
4480 	}
4481 	ta->Function = MPI_FUNCTION_TARGET_ASSIST;
4482 	ta->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4483 	ta->ReplyWord = htole32(tgt->reply_desc);
4484 	be64enc(ta->LUN, CAM_EXTLUN_BYTE_SWIZZLE(lun));
4485 	ta->RelativeOffset = 0;
4486 	ta->DataLength = length;
4487 
4488 	dptr = req->req_vbuf;
4489 	dptr += MPT_RQSL(mpt);
4490 	pptr = req->req_pbuf;
4491 	pptr += MPT_RQSL(mpt);
4492 	memcpy(dptr, data, min(length, MPT_RQSL(mpt)));
4493 
4494 	se = (SGE_SIMPLE32 *) &ta->SGL[0];
4495 	memset(se, 0,sizeof (*se));
4496 
4497 	flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
4498 	if (send) {
4499 		ta->TargetAssistFlags |= TARGET_ASSIST_FLAGS_DATA_DIRECTION;
4500 		flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
4501 	}
4502 	se->Address = pptr;
4503 	MPI_pSGE_SET_LENGTH(se, length);
4504 	flags |= MPI_SGE_FLAGS_LAST_ELEMENT;
4505 	flags |= MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_END_OF_BUFFER;
4506 	MPI_pSGE_SET_FLAGS(se, flags);
4507 
4508 	tgt->ccb = NULL;
4509 	tgt->req = req;
4510 	tgt->resid -= length;
4511 	tgt->bytes_xfered = length;
4512 #ifdef	WE_TRUST_AUTO_GOOD_STATUS
4513 	tgt->state = TGT_STATE_MOVING_DATA_AND_STATUS;
4514 #else
4515 	tgt->state = TGT_STATE_MOVING_DATA;
4516 #endif
4517 	mpt_send_cmd(mpt, req);
4518 }
4519 
4520 /*
4521  * Abort queued up CCBs
4522  */
4523 static cam_status
mpt_abort_target_ccb(struct mpt_softc * mpt,union ccb * ccb)4524 mpt_abort_target_ccb(struct mpt_softc *mpt, union ccb *ccb)
4525 {
4526 	struct mpt_hdr_stailq *lp;
4527 	struct ccb_hdr *srch;
4528 	union ccb *accb = ccb->cab.abort_ccb;
4529 	tgt_resource_t *trtp;
4530 	mpt_tgt_state_t *tgt;
4531 	request_t *req;
4532 	uint32_t tag;
4533 
4534 	mpt_lprt(mpt, MPT_PRT_DEBUG, "aborting ccb %p\n", accb);
4535 	if (ccb->ccb_h.target_lun == CAM_LUN_WILDCARD)
4536 		trtp = &mpt->trt_wildcard;
4537 	else
4538 		trtp = &mpt->trt[ccb->ccb_h.target_lun];
4539 	if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4540 		lp = &trtp->atios;
4541 		tag = accb->atio.tag_id;
4542 	} else {
4543 		lp = &trtp->inots;
4544 		tag = accb->cin1.tag_id;
4545 	}
4546 
4547 	/* Search the CCB among queued. */
4548 	STAILQ_FOREACH(srch, lp, sim_links.stqe) {
4549 		if (srch != &accb->ccb_h)
4550 			continue;
4551 		STAILQ_REMOVE(lp, srch, ccb_hdr, sim_links.stqe);
4552 		accb->ccb_h.status = CAM_REQ_ABORTED;
4553 		xpt_done(accb);
4554 		return (CAM_REQ_CMP);
4555 	}
4556 
4557 	/* Search the CCB among running. */
4558 	req = MPT_TAG_2_REQ(mpt, tag);
4559 	tgt = MPT_TGT_STATE(mpt, req);
4560 	if (tgt->tag_id == tag) {
4561 		mpt_abort_target_cmd(mpt, req);
4562 		return (CAM_REQ_CMP);
4563 	}
4564 
4565 	return (CAM_UA_ABORT);
4566 }
4567 
4568 /*
4569  * Ask the MPT to abort the current target command
4570  */
4571 static int
mpt_abort_target_cmd(struct mpt_softc * mpt,request_t * cmd_req)4572 mpt_abort_target_cmd(struct mpt_softc *mpt, request_t *cmd_req)
4573 {
4574 	int error;
4575 	request_t *req;
4576 	PTR_MSG_TARGET_MODE_ABORT abtp;
4577 
4578 	req = mpt_get_request(mpt, FALSE);
4579 	if (req == NULL) {
4580 		return (-1);
4581 	}
4582 	abtp = req->req_vbuf;
4583 	memset(abtp, 0, sizeof (*abtp));
4584 
4585 	abtp->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4586 	abtp->AbortType = TARGET_MODE_ABORT_TYPE_EXACT_IO;
4587 	abtp->Function = MPI_FUNCTION_TARGET_MODE_ABORT;
4588 	abtp->ReplyWord = htole32(MPT_TGT_STATE(mpt, cmd_req)->reply_desc);
4589 	error = 0;
4590 	if (mpt->is_fc || mpt->is_sas) {
4591 		mpt_send_cmd(mpt, req);
4592 	} else {
4593 		error = mpt_send_handshake_cmd(mpt, sizeof(*req), req);
4594 	}
4595 	return (error);
4596 }
4597 
4598 /*
4599  * WE_TRUST_AUTO_GOOD_STATUS- I've found that setting
4600  * TARGET_STATUS_SEND_FLAGS_AUTO_GOOD_STATUS leads the
4601  * FC929 to set bogus FC_RSP fields (nonzero residuals
4602  * but w/o RESID fields set). This causes QLogic initiators
4603  * to think maybe that a frame was lost.
4604  *
4605  * WE_CAN_USE_AUTO_REPOST- we can't use AUTO_REPOST because
4606  * we use allocated requests to do TARGET_ASSIST and we
4607  * need to know when to release them.
4608  */
4609 
4610 static void
mpt_scsi_tgt_status(struct mpt_softc * mpt,union ccb * ccb,request_t * cmd_req,uint8_t status,uint8_t const * sense_data,u_int sense_len)4611 mpt_scsi_tgt_status(struct mpt_softc *mpt, union ccb *ccb, request_t *cmd_req,
4612     uint8_t status, uint8_t const *sense_data, u_int sense_len)
4613 {
4614 	uint8_t *cmd_vbuf;
4615 	mpt_tgt_state_t *tgt;
4616 	PTR_MSG_TARGET_STATUS_SEND_REQUEST tp;
4617 	request_t *req;
4618 	bus_addr_t paddr;
4619 	int resplen = 0;
4620 	uint32_t fl;
4621 
4622 	cmd_vbuf = cmd_req->req_vbuf;
4623 	cmd_vbuf += MPT_RQSL(mpt);
4624 	tgt = MPT_TGT_STATE(mpt, cmd_req);
4625 
4626 	if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
4627 		if (mpt->outofbeer == 0) {
4628 			mpt->outofbeer = 1;
4629 			xpt_freeze_simq(mpt->sim, 1);
4630 			mpt_lprt(mpt, MPT_PRT_DEBUG, "FREEZEQ\n");
4631 		}
4632 		if (ccb) {
4633 			ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4634 			mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
4635 			xpt_done(ccb);
4636 		} else {
4637 			mpt_prt(mpt,
4638 			    "could not allocate status request- dropping\n");
4639 		}
4640 		return;
4641 	}
4642 	req->ccb = ccb;
4643 	if (ccb) {
4644 		ccb->ccb_h.ccb_mpt_ptr = mpt;
4645 		ccb->ccb_h.ccb_req_ptr = req;
4646 	}
4647 
4648 	/*
4649 	 * Record the currently active ccb, if any, and the
4650 	 * request for it in our target state area.
4651 	 */
4652 	tgt->ccb = ccb;
4653 	tgt->req = req;
4654 	tgt->state = TGT_STATE_SENDING_STATUS;
4655 
4656 	tp = req->req_vbuf;
4657 	paddr = req->req_pbuf;
4658 	paddr += MPT_RQSL(mpt);
4659 
4660 	memset(tp, 0, sizeof (*tp));
4661 	tp->StatusCode = status;
4662 	tp->Function = MPI_FUNCTION_TARGET_STATUS_SEND;
4663 	if (mpt->is_fc) {
4664 		PTR_MPI_TARGET_FCP_CMD_BUFFER fc =
4665 		    (PTR_MPI_TARGET_FCP_CMD_BUFFER) cmd_vbuf;
4666 		uint8_t *sts_vbuf;
4667 		uint32_t *rsp;
4668 
4669 		sts_vbuf = req->req_vbuf;
4670 		sts_vbuf += MPT_RQSL(mpt);
4671 		rsp = (uint32_t *) sts_vbuf;
4672 		memcpy(tp->LUN, fc->FcpLun, sizeof (tp->LUN));
4673 
4674 		/*
4675 		 * The MPI_TARGET_FCP_RSP_BUFFER define is unfortunate.
4676 		 * It has to be big-endian in memory and is organized
4677 		 * in 32 bit words, which are much easier to deal with
4678 		 * as words which are swizzled as needed.
4679 		 *
4680 		 * All we're filling here is the FC_RSP payload.
4681 		 * We may just have the chip synthesize it if
4682 		 * we have no residual and an OK status.
4683 		 *
4684 		 */
4685 		memset(rsp, 0, sizeof (MPI_TARGET_FCP_RSP_BUFFER));
4686 
4687 		rsp[2] = htobe32(status);
4688 #define	MIN_FCP_RESPONSE_SIZE	24
4689 #ifndef	WE_TRUST_AUTO_GOOD_STATUS
4690 		resplen = MIN_FCP_RESPONSE_SIZE;
4691 #endif
4692 		if (tgt->resid < 0) {
4693 			rsp[2] |= htobe32(0x400); /* XXXX NEED MNEMONIC!!!! */
4694 			rsp[3] = htobe32(-tgt->resid);
4695 			resplen = MIN_FCP_RESPONSE_SIZE;
4696 		} else if (tgt->resid > 0) {
4697 			rsp[2] |= htobe32(0x800); /* XXXX NEED MNEMONIC!!!! */
4698 			rsp[3] = htobe32(tgt->resid);
4699 			resplen = MIN_FCP_RESPONSE_SIZE;
4700 		}
4701 		if (sense_len > 0) {
4702 			rsp[2] |= htobe32(0x200); /* XXXX NEED MNEMONIC!!!! */
4703 			rsp[4] = htobe32(sense_len);
4704 			memcpy(&rsp[6], sense_data, sense_len);
4705 			resplen = MIN_FCP_RESPONSE_SIZE + sense_len;
4706 		}
4707 	} else if (mpt->is_sas) {
4708 		PTR_MPI_TARGET_SSP_CMD_BUFFER ssp =
4709 		    (PTR_MPI_TARGET_SSP_CMD_BUFFER) cmd_vbuf;
4710 		memcpy(tp->LUN, ssp->LogicalUnitNumber, sizeof (tp->LUN));
4711 	} else {
4712 		PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp =
4713 		    (PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER) cmd_vbuf;
4714 		tp->QueueTag = htole16(sp->Tag);
4715 		memcpy(tp->LUN, sp->LogicalUnitNumber, sizeof (tp->LUN));
4716 	}
4717 
4718 	tp->ReplyWord = htole32(tgt->reply_desc);
4719 	tp->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4720 
4721 #ifdef	WE_CAN_USE_AUTO_REPOST
4722 	tp->MsgFlags = TARGET_STATUS_SEND_FLAGS_REPOST_CMD_BUFFER;
4723 #endif
4724 	if (status == SCSI_STATUS_OK && resplen == 0) {
4725 		tp->MsgFlags |= TARGET_STATUS_SEND_FLAGS_AUTO_GOOD_STATUS;
4726 	} else {
4727 		tp->StatusDataSGE.u.Address32 = htole32((uint32_t) paddr);
4728 		fl = MPI_SGE_FLAGS_HOST_TO_IOC |
4729 		     MPI_SGE_FLAGS_SIMPLE_ELEMENT |
4730 		     MPI_SGE_FLAGS_LAST_ELEMENT |
4731 		     MPI_SGE_FLAGS_END_OF_LIST |
4732 		     MPI_SGE_FLAGS_END_OF_BUFFER;
4733 		fl <<= MPI_SGE_FLAGS_SHIFT;
4734 		fl |= resplen;
4735 		tp->StatusDataSGE.FlagsLength = htole32(fl);
4736 	}
4737 
4738 	mpt_lprt(mpt, MPT_PRT_DEBUG,
4739 	    "STATUS_CCB %p (with%s sense) tag %x req %p:%u resid %u\n",
4740 	    ccb, sense_len > 0 ? "" : "out", tgt->tag_id,
4741 	    req, req->serno, tgt->resid);
4742 	if (mpt->verbose > MPT_PRT_DEBUG)
4743 		mpt_print_request(req->req_vbuf);
4744 	if (ccb) {
4745 		ccb->ccb_h.status = CAM_SIM_QUEUED | CAM_REQ_INPROG;
4746 		mpt_req_timeout(req, SBT_1S * 60, mpt_timeout, ccb);
4747 	}
4748 	mpt_send_cmd(mpt, req);
4749 }
4750 
4751 static void
mpt_scsi_tgt_tsk_mgmt(struct mpt_softc * mpt,request_t * req,mpt_task_mgmt_t fc,tgt_resource_t * trtp,int init_id)4752 mpt_scsi_tgt_tsk_mgmt(struct mpt_softc *mpt, request_t *req, mpt_task_mgmt_t fc,
4753     tgt_resource_t *trtp, int init_id)
4754 {
4755 	struct ccb_immediate_notify *inot;
4756 	mpt_tgt_state_t *tgt;
4757 
4758 	tgt = MPT_TGT_STATE(mpt, req);
4759 	inot = (struct ccb_immediate_notify *) STAILQ_FIRST(&trtp->inots);
4760 	if (inot == NULL) {
4761 		mpt_lprt(mpt, MPT_PRT_WARN, "no INOTSs- sending back BSY\n");
4762 		mpt_scsi_tgt_status(mpt, NULL, req, SCSI_STATUS_BUSY, NULL, 0);
4763 		return;
4764 	}
4765 	STAILQ_REMOVE_HEAD(&trtp->inots, sim_links.stqe);
4766 	mpt_lprt(mpt, MPT_PRT_DEBUG1,
4767 	    "Get FREE INOT %p lun %jx\n", inot,
4768 	    (uintmax_t)inot->ccb_h.target_lun);
4769 
4770 	inot->initiator_id = init_id;	/* XXX */
4771 	inot->tag_id = tgt->tag_id;
4772 	inot->seq_id = 0;
4773 	/*
4774 	 * This is a somewhat grotesque attempt to map from task management
4775 	 * to old style SCSI messages. God help us all.
4776 	 */
4777 	switch (fc) {
4778 	case MPT_QUERY_TASK_SET:
4779 		inot->arg = MSG_QUERY_TASK_SET;
4780 		break;
4781 	case MPT_ABORT_TASK_SET:
4782 		inot->arg = MSG_ABORT_TASK_SET;
4783 		break;
4784 	case MPT_CLEAR_TASK_SET:
4785 		inot->arg = MSG_CLEAR_TASK_SET;
4786 		break;
4787 	case MPT_QUERY_ASYNC_EVENT:
4788 		inot->arg = MSG_QUERY_ASYNC_EVENT;
4789 		break;
4790 	case MPT_LOGICAL_UNIT_RESET:
4791 		inot->arg = MSG_LOGICAL_UNIT_RESET;
4792 		break;
4793 	case MPT_TARGET_RESET:
4794 		inot->arg = MSG_TARGET_RESET;
4795 		break;
4796 	case MPT_CLEAR_ACA:
4797 		inot->arg = MSG_CLEAR_ACA;
4798 		break;
4799 	default:
4800 		inot->arg = MSG_NOOP;
4801 		break;
4802 	}
4803 	tgt->ccb = (union ccb *) inot;
4804 	inot->ccb_h.status = CAM_MESSAGE_RECV;
4805 	xpt_done((union ccb *)inot);
4806 }
4807 
4808 static void
mpt_scsi_tgt_atio(struct mpt_softc * mpt,request_t * req,uint32_t reply_desc)4809 mpt_scsi_tgt_atio(struct mpt_softc *mpt, request_t *req, uint32_t reply_desc)
4810 {
4811 	static uint8_t null_iqd[SHORT_INQUIRY_LENGTH] = {
4812 	    0x7f, 0x00, 0x02, 0x02, 0x20, 0x00, 0x00, 0x32,
4813 	     'F',  'R',  'E',  'E',  'B',  'S',  'D',  ' ',
4814 	     'L',  'S',  'I',  '-',  'L',  'O',  'G',  'I',
4815 	     'C',  ' ',  'N',  'U',  'L',  'D',  'E',  'V',
4816 	     '0',  '0',  '0',  '1'
4817 	};
4818 	struct ccb_accept_tio *atiop;
4819 	lun_id_t lun;
4820 	int tag_action = 0;
4821 	mpt_tgt_state_t *tgt;
4822 	tgt_resource_t *trtp = NULL;
4823 	U8 *lunptr;
4824 	U8 *vbuf;
4825 	U16 ioindex;
4826 	mpt_task_mgmt_t fct = MPT_NIL_TMT_VALUE;
4827 	uint8_t *cdbp;
4828 
4829 	/*
4830 	 * Stash info for the current command where we can get at it later.
4831 	 */
4832 	vbuf = req->req_vbuf;
4833 	vbuf += MPT_RQSL(mpt);
4834 	if (mpt->verbose >= MPT_PRT_DEBUG) {
4835 		mpt_dump_data(mpt, "mpt_scsi_tgt_atio response", vbuf,
4836 		    max(sizeof (MPI_TARGET_FCP_CMD_BUFFER),
4837 		    max(sizeof (MPI_TARGET_SSP_CMD_BUFFER),
4838 		    sizeof (MPI_TARGET_SCSI_SPI_CMD_BUFFER))));
4839 	}
4840 
4841 	/*
4842 	 * Get our state pointer set up.
4843 	 */
4844 	tgt = MPT_TGT_STATE(mpt, req);
4845 	if (tgt->state != TGT_STATE_LOADED) {
4846 		mpt_tgt_dump_req_state(mpt, req);
4847 		panic("bad target state in mpt_scsi_tgt_atio");
4848 	}
4849 	memset(tgt, 0, sizeof (mpt_tgt_state_t));
4850 	tgt->state = TGT_STATE_IN_CAM;
4851 	tgt->reply_desc = reply_desc;
4852 	ioindex = GET_IO_INDEX(reply_desc);
4853 
4854 	/*
4855 	 * The tag we construct here allows us to find the
4856 	 * original request that the command came in with.
4857 	 *
4858 	 * This way we don't have to depend on anything but the
4859 	 * tag to find things when CCBs show back up from CAM.
4860 	 */
4861 	tgt->tag_id = MPT_MAKE_TAGID(mpt, req, ioindex);
4862 
4863 	if (mpt->is_fc) {
4864 		PTR_MPI_TARGET_FCP_CMD_BUFFER fc;
4865 		fc = (PTR_MPI_TARGET_FCP_CMD_BUFFER) vbuf;
4866 		if (fc->FcpCntl[2]) {
4867 			/*
4868 			 * Task Management Request
4869 			 */
4870 			switch (fc->FcpCntl[2]) {
4871 			case 0x1:
4872 				fct = MPT_QUERY_TASK_SET;
4873 				break;
4874 			case 0x2:
4875 				fct = MPT_ABORT_TASK_SET;
4876 				break;
4877 			case 0x4:
4878 				fct = MPT_CLEAR_TASK_SET;
4879 				break;
4880 			case 0x8:
4881 				fct = MPT_QUERY_ASYNC_EVENT;
4882 				break;
4883 			case 0x10:
4884 				fct = MPT_LOGICAL_UNIT_RESET;
4885 				break;
4886 			case 0x20:
4887 				fct = MPT_TARGET_RESET;
4888 				break;
4889 			case 0x40:
4890 				fct = MPT_CLEAR_ACA;
4891 				break;
4892 			default:
4893 				mpt_prt(mpt, "CORRUPTED TASK MGMT BITS: 0x%x\n",
4894 				    fc->FcpCntl[2]);
4895 				mpt_scsi_tgt_status(mpt, NULL, req,
4896 				    SCSI_STATUS_OK, NULL, 0);
4897 				return;
4898 			}
4899 		} else {
4900 			switch (fc->FcpCntl[1]) {
4901 			case 0:
4902 				tag_action = MSG_SIMPLE_Q_TAG;
4903 				break;
4904 			case 1:
4905 				tag_action = MSG_HEAD_OF_Q_TAG;
4906 				break;
4907 			case 2:
4908 				tag_action = MSG_ORDERED_Q_TAG;
4909 				break;
4910 			default:
4911 				/*
4912 				 * Bah. Ignore Untagged Queing and ACA
4913 				 */
4914 				tag_action = MSG_SIMPLE_Q_TAG;
4915 				break;
4916 			}
4917 		}
4918 		tgt->resid = be32toh(fc->FcpDl);
4919 		cdbp = fc->FcpCdb;
4920 		lunptr = fc->FcpLun;
4921 		tgt->itag = fc->OptionalOxid;
4922 	} else if (mpt->is_sas) {
4923 		PTR_MPI_TARGET_SSP_CMD_BUFFER ssp;
4924 		ssp = (PTR_MPI_TARGET_SSP_CMD_BUFFER) vbuf;
4925 		cdbp = ssp->CDB;
4926 		lunptr = ssp->LogicalUnitNumber;
4927 		tgt->itag = ssp->InitiatorTag;
4928 	} else {
4929 		PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp;
4930 		sp = (PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER) vbuf;
4931 		cdbp = sp->CDB;
4932 		lunptr = sp->LogicalUnitNumber;
4933 		tgt->itag = sp->Tag;
4934 	}
4935 
4936 	lun = CAM_EXTLUN_BYTE_SWIZZLE(be64dec(lunptr));
4937 
4938 	/*
4939 	 * Deal with non-enabled or bad luns here.
4940 	 */
4941 	if (lun >= MPT_MAX_LUNS || mpt->tenabled == 0 ||
4942 	    mpt->trt[lun].enabled == 0) {
4943 		if (mpt->twildcard) {
4944 			trtp = &mpt->trt_wildcard;
4945 		} else if (fct == MPT_NIL_TMT_VALUE) {
4946 			/*
4947 			 * In this case, we haven't got an upstream listener
4948 			 * for either a specific lun or wildcard luns. We
4949 			 * have to make some sensible response. For regular
4950 			 * inquiry, just return some NOT HERE inquiry data.
4951 			 * For VPD inquiry, report illegal field in cdb.
4952 			 * For REQUEST SENSE, just return NO SENSE data.
4953 			 * REPORT LUNS gets illegal command.
4954 			 * All other commands get 'no such device'.
4955 			 */
4956 			uint8_t sense[MPT_SENSE_SIZE];
4957 			size_t len;
4958 
4959 			memset(sense, 0, sizeof(sense));
4960 			sense[0] = 0xf0;
4961 			sense[2] = 0x5;
4962 			sense[7] = 0x8;
4963 
4964 			switch (cdbp[0]) {
4965 			case INQUIRY:
4966 			{
4967 				if (cdbp[1] != 0) {
4968 					sense[12] = 0x26;
4969 					sense[13] = 0x01;
4970 					break;
4971 				}
4972 				len = min(tgt->resid, cdbp[4]);
4973 				len = min(len, sizeof (null_iqd));
4974 				mpt_lprt(mpt, MPT_PRT_DEBUG,
4975 				    "local inquiry %ld bytes\n", (long) len);
4976 				mpt_scsi_tgt_local(mpt, req, lun, 1,
4977 				    null_iqd, len);
4978 				return;
4979 			}
4980 			case REQUEST_SENSE:
4981 			{
4982 				sense[2] = 0x0;
4983 				len = min(tgt->resid, cdbp[4]);
4984 				len = min(len, sizeof (sense));
4985 				mpt_lprt(mpt, MPT_PRT_DEBUG,
4986 				    "local reqsense %ld bytes\n", (long) len);
4987 				mpt_scsi_tgt_local(mpt, req, lun, 1,
4988 				    sense, len);
4989 				return;
4990 			}
4991 			case REPORT_LUNS:
4992 				mpt_lprt(mpt, MPT_PRT_DEBUG, "REPORT LUNS\n");
4993 				sense[12] = 0x26;
4994 				return;
4995 			default:
4996 				mpt_lprt(mpt, MPT_PRT_DEBUG,
4997 				    "CMD 0x%x to unmanaged lun %jx\n",
4998 				    cdbp[0], (uintmax_t)lun);
4999 				sense[12] = 0x25;
5000 				break;
5001 			}
5002 			mpt_scsi_tgt_status(mpt, NULL, req,
5003 			    SCSI_STATUS_CHECK_COND, sense, sizeof(sense));
5004 			return;
5005 		}
5006 		/* otherwise, leave trtp NULL */
5007 	} else {
5008 		trtp = &mpt->trt[lun];
5009 	}
5010 
5011 	/*
5012 	 * Deal with any task management
5013 	 */
5014 	if (fct != MPT_NIL_TMT_VALUE) {
5015 		if (trtp == NULL) {
5016 			mpt_prt(mpt, "task mgmt function %x but no listener\n",
5017 			    fct);
5018 			mpt_scsi_tgt_status(mpt, NULL, req,
5019 			    SCSI_STATUS_OK, NULL, 0);
5020 		} else {
5021 			mpt_scsi_tgt_tsk_mgmt(mpt, req, fct, trtp,
5022 			    GET_INITIATOR_INDEX(reply_desc));
5023 		}
5024 		return;
5025 	}
5026 
5027 	atiop = (struct ccb_accept_tio *) STAILQ_FIRST(&trtp->atios);
5028 	if (atiop == NULL) {
5029 		mpt_lprt(mpt, MPT_PRT_WARN,
5030 		    "no ATIOs for lun %jx- sending back %s\n", (uintmax_t)lun,
5031 		    mpt->tenabled? "QUEUE FULL" : "BUSY");
5032 		mpt_scsi_tgt_status(mpt, NULL, req,
5033 		    mpt->tenabled? SCSI_STATUS_QUEUE_FULL : SCSI_STATUS_BUSY,
5034 		    NULL, 0);
5035 		return;
5036 	}
5037 	STAILQ_REMOVE_HEAD(&trtp->atios, sim_links.stqe);
5038 	mpt_lprt(mpt, MPT_PRT_DEBUG1,
5039 	    "Get FREE ATIO %p lun %jx\n", atiop,
5040 	    (uintmax_t)atiop->ccb_h.target_lun);
5041 	atiop->ccb_h.ccb_mpt_ptr = mpt;
5042 	atiop->ccb_h.status = CAM_CDB_RECVD;
5043 	atiop->ccb_h.target_lun = lun;
5044 	atiop->sense_len = 0;
5045 	atiop->tag_id = tgt->tag_id;
5046 	atiop->init_id = GET_INITIATOR_INDEX(reply_desc);
5047 	atiop->cdb_len = 16;
5048 	memcpy(atiop->cdb_io.cdb_bytes, cdbp, atiop->cdb_len);
5049 	if (tag_action) {
5050 		atiop->tag_action = tag_action;
5051 		atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
5052 	}
5053 	if (mpt->verbose >= MPT_PRT_DEBUG) {
5054 		int i;
5055 		mpt_prt(mpt, "START_CCB %p for lun %jx CDB=<", atiop,
5056 		    (uintmax_t)atiop->ccb_h.target_lun);
5057 		for (i = 0; i < atiop->cdb_len; i++) {
5058 			mpt_prtc(mpt, "%02x%c", cdbp[i] & 0xff,
5059 			    (i == (atiop->cdb_len - 1))? '>' : ' ');
5060 		}
5061 		mpt_prtc(mpt, " itag %x tag %x rdesc %x dl=%u\n",
5062 		    tgt->itag, tgt->tag_id, tgt->reply_desc, tgt->resid);
5063 	}
5064 
5065 	xpt_done((union ccb *)atiop);
5066 }
5067 
5068 static void
mpt_tgt_dump_tgt_state(struct mpt_softc * mpt,request_t * req)5069 mpt_tgt_dump_tgt_state(struct mpt_softc *mpt, request_t *req)
5070 {
5071 	mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, req);
5072 
5073 	mpt_prt(mpt, "req %p:%u tgt:rdesc 0x%x resid %u xfrd %u ccb %p treq %p "
5074 	    "nx %d tag 0x%08x itag 0x%04x state=%d\n", req, req->serno,
5075 	    tgt->reply_desc, tgt->resid, tgt->bytes_xfered, tgt->ccb,
5076 	    tgt->req, tgt->nxfers, tgt->tag_id, tgt->itag, tgt->state);
5077 }
5078 
5079 static void
mpt_tgt_dump_req_state(struct mpt_softc * mpt,request_t * req)5080 mpt_tgt_dump_req_state(struct mpt_softc *mpt, request_t *req)
5081 {
5082 
5083 	mpt_prt(mpt, "req %p:%u index %u (%x) state %x\n", req, req->serno,
5084 	    req->index, req->index, req->state);
5085 	mpt_tgt_dump_tgt_state(mpt, req);
5086 }
5087 
5088 static int
mpt_scsi_tgt_reply_handler(struct mpt_softc * mpt,request_t * req,uint32_t reply_desc,MSG_DEFAULT_REPLY * reply_frame)5089 mpt_scsi_tgt_reply_handler(struct mpt_softc *mpt, request_t *req,
5090     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
5091 {
5092 	int dbg;
5093 	union ccb *ccb;
5094 	U16 status;
5095 
5096 	if (reply_frame == NULL) {
5097 		/*
5098 		 * Figure out what the state of the command is.
5099 		 */
5100 		mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, req);
5101 
5102 #ifdef	INVARIANTS
5103 		mpt_req_spcl(mpt, req, "turbo scsi_tgt_reply", __LINE__);
5104 		if (tgt->req) {
5105 			mpt_req_not_spcl(mpt, tgt->req,
5106 			    "turbo scsi_tgt_reply associated req", __LINE__);
5107 		}
5108 #endif
5109 		switch(tgt->state) {
5110 		case TGT_STATE_LOADED:
5111 			/*
5112 			 * This is a new command starting.
5113 			 */
5114 			mpt_scsi_tgt_atio(mpt, req, reply_desc);
5115 			break;
5116 		case TGT_STATE_MOVING_DATA:
5117 		{
5118 			ccb = tgt->ccb;
5119 			if (tgt->req == NULL) {
5120 				panic("mpt: turbo target reply with null "
5121 				    "associated request moving data");
5122 				/* NOTREACHED */
5123 			}
5124 			if (ccb == NULL) {
5125 				if (tgt->is_local == 0) {
5126 					panic("mpt: turbo target reply with "
5127 					    "null associated ccb moving data");
5128 					/* NOTREACHED */
5129 				}
5130 				mpt_lprt(mpt, MPT_PRT_DEBUG,
5131 				    "TARGET_ASSIST local done\n");
5132 				TAILQ_REMOVE(&mpt->request_pending_list,
5133 				    tgt->req, links);
5134 				mpt_free_request(mpt, tgt->req);
5135 				tgt->req = NULL;
5136 				mpt_scsi_tgt_status(mpt, NULL, req,
5137 				    0, NULL, 0);
5138 				return (TRUE);
5139 			}
5140 			tgt->ccb = NULL;
5141 			tgt->nxfers++;
5142 			mpt_req_untimeout(tgt->req, mpt_timeout, ccb);
5143 			mpt_lprt(mpt, MPT_PRT_DEBUG,
5144 			    "TARGET_ASSIST %p (req %p:%u) done tag 0x%x\n",
5145 			    ccb, tgt->req, tgt->req->serno, ccb->csio.tag_id);
5146 			/*
5147 			 * Free the Target Assist Request
5148 			 */
5149 			KASSERT(tgt->req->ccb == ccb,
5150 			    ("tgt->req %p:%u tgt->req->ccb %p", tgt->req,
5151 			    tgt->req->serno, tgt->req->ccb));
5152 			TAILQ_REMOVE(&mpt->request_pending_list,
5153 			    tgt->req, links);
5154 			mpt_free_request(mpt, tgt->req);
5155 			tgt->req = NULL;
5156 
5157 			/*
5158 			 * Do we need to send status now? That is, are
5159 			 * we done with all our data transfers?
5160 			 */
5161 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
5162 				mpt_set_ccb_status(ccb, CAM_REQ_CMP);
5163 				ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5164 				KASSERT(ccb->ccb_h.status,
5165 				    ("zero ccb sts at %d", __LINE__));
5166 				tgt->state = TGT_STATE_IN_CAM;
5167 				if (mpt->outofbeer) {
5168 					ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
5169 					mpt->outofbeer = 0;
5170 					mpt_lprt(mpt, MPT_PRT_DEBUG, "THAWQ\n");
5171 				}
5172 				xpt_done(ccb);
5173 				break;
5174 			}
5175 			/*
5176 			 * Otherwise, send status (and sense)
5177 			 */
5178 			mpt_scsi_tgt_status(mpt, ccb, req,
5179 			    ccb->csio.scsi_status,
5180 			    (void *)&ccb->csio.sense_data,
5181 			    (ccb->ccb_h.flags & CAM_SEND_SENSE) ?
5182 			     ccb->csio.sense_len : 0);
5183 			break;
5184 		}
5185 		case TGT_STATE_SENDING_STATUS:
5186 		case TGT_STATE_MOVING_DATA_AND_STATUS:
5187 		{
5188 			int ioindex;
5189 			ccb = tgt->ccb;
5190 
5191 			if (tgt->req == NULL) {
5192 				panic("mpt: turbo target reply with null "
5193 				    "associated request sending status");
5194 				/* NOTREACHED */
5195 			}
5196 
5197 			if (ccb) {
5198 				tgt->ccb = NULL;
5199 				if (tgt->state ==
5200 				    TGT_STATE_MOVING_DATA_AND_STATUS) {
5201 					tgt->nxfers++;
5202 				}
5203 				mpt_req_untimeout(tgt->req, mpt_timeout, ccb);
5204 				if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
5205 					ccb->ccb_h.status |= CAM_SENT_SENSE;
5206 				}
5207 				mpt_lprt(mpt, MPT_PRT_DEBUG,
5208 				    "TARGET_STATUS tag %x sts %x flgs %x req "
5209 				    "%p\n", ccb->csio.tag_id, ccb->ccb_h.status,
5210 				    ccb->ccb_h.flags, tgt->req);
5211 				/*
5212 				 * Free the Target Send Status Request
5213 				 */
5214 				KASSERT(tgt->req->ccb == ccb,
5215 				    ("tgt->req %p:%u tgt->req->ccb %p",
5216 				    tgt->req, tgt->req->serno, tgt->req->ccb));
5217 				/*
5218 				 * Notify CAM that we're done
5219 				 */
5220 				mpt_set_ccb_status(ccb, CAM_REQ_CMP);
5221 				ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5222 				KASSERT(ccb->ccb_h.status,
5223 				    ("ZERO ccb sts at %d", __LINE__));
5224 				tgt->ccb = NULL;
5225 			} else {
5226 				mpt_lprt(mpt, MPT_PRT_DEBUG,
5227 				    "TARGET_STATUS non-CAM for req %p:%u\n",
5228 				    tgt->req, tgt->req->serno);
5229 			}
5230 			TAILQ_REMOVE(&mpt->request_pending_list,
5231 			    tgt->req, links);
5232 			mpt_free_request(mpt, tgt->req);
5233 			tgt->req = NULL;
5234 
5235 			/*
5236 			 * And re-post the Command Buffer.
5237 			 * This will reset the state.
5238 			 */
5239 			ioindex = GET_IO_INDEX(reply_desc);
5240 			TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5241 			tgt->is_local = 0;
5242 			mpt_post_target_command(mpt, req, ioindex);
5243 
5244 			/*
5245 			 * And post a done for anyone who cares
5246 			 */
5247 			if (ccb) {
5248 				if (mpt->outofbeer) {
5249 					ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
5250 					mpt->outofbeer = 0;
5251 					mpt_lprt(mpt, MPT_PRT_DEBUG, "THAWQ\n");
5252 				}
5253 				xpt_done(ccb);
5254 			}
5255 			break;
5256 		}
5257 		case TGT_STATE_NIL:	/* XXX This Never Happens XXX */
5258 			tgt->state = TGT_STATE_LOADED;
5259 			break;
5260 		default:
5261 			mpt_prt(mpt, "Unknown Target State 0x%x in Context "
5262 			    "Reply Function\n", tgt->state);
5263 		}
5264 		return (TRUE);
5265 	}
5266 
5267 	status = le16toh(reply_frame->IOCStatus);
5268 	if (status != MPI_IOCSTATUS_SUCCESS) {
5269 		dbg = MPT_PRT_ERROR;
5270 	} else {
5271 		dbg = MPT_PRT_DEBUG1;
5272 	}
5273 
5274 	mpt_lprt(mpt, dbg,
5275 	    "SCSI_TGT REPLY: req=%p:%u reply=%p func=%x IOCstatus 0x%x\n",
5276 	     req, req->serno, reply_frame, reply_frame->Function, status);
5277 
5278 	switch (reply_frame->Function) {
5279 	case MPI_FUNCTION_TARGET_CMD_BUFFER_POST:
5280 	{
5281 		mpt_tgt_state_t *tgt;
5282 #ifdef	INVARIANTS
5283 		mpt_req_spcl(mpt, req, "tgt reply BUFFER POST", __LINE__);
5284 #endif
5285 		if (status != MPI_IOCSTATUS_SUCCESS) {
5286 			/*
5287 			 * XXX What to do?
5288 			 */
5289 			break;
5290 		}
5291 		tgt = MPT_TGT_STATE(mpt, req);
5292 		KASSERT(tgt->state == TGT_STATE_LOADING,
5293 		    ("bad state 0x%x on reply to buffer post", tgt->state));
5294 		mpt_assign_serno(mpt, req);
5295 		tgt->state = TGT_STATE_LOADED;
5296 		break;
5297 	}
5298 	case MPI_FUNCTION_TARGET_ASSIST:
5299 #ifdef	INVARIANTS
5300 		mpt_req_not_spcl(mpt, req, "tgt reply TARGET ASSIST", __LINE__);
5301 #endif
5302 		mpt_prt(mpt, "target assist completion\n");
5303 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5304 		mpt_free_request(mpt, req);
5305 		break;
5306 	case MPI_FUNCTION_TARGET_STATUS_SEND:
5307 #ifdef	INVARIANTS
5308 		mpt_req_not_spcl(mpt, req, "tgt reply STATUS SEND", __LINE__);
5309 #endif
5310 		mpt_prt(mpt, "status send completion\n");
5311 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5312 		mpt_free_request(mpt, req);
5313 		break;
5314 	case MPI_FUNCTION_TARGET_MODE_ABORT:
5315 	{
5316 		PTR_MSG_TARGET_MODE_ABORT_REPLY abtrp =
5317 		    (PTR_MSG_TARGET_MODE_ABORT_REPLY) reply_frame;
5318 		PTR_MSG_TARGET_MODE_ABORT abtp =
5319 		    (PTR_MSG_TARGET_MODE_ABORT) req->req_vbuf;
5320 		uint32_t cc = GET_IO_INDEX(le32toh(abtp->ReplyWord));
5321 #ifdef	INVARIANTS
5322 		mpt_req_not_spcl(mpt, req, "tgt reply TMODE ABORT", __LINE__);
5323 #endif
5324 		mpt_prt(mpt, "ABORT RX_ID 0x%x Complete; status 0x%x cnt %u\n",
5325 		    cc, le16toh(abtrp->IOCStatus), le32toh(abtrp->AbortCount));
5326 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5327 		mpt_free_request(mpt, req);
5328 		break;
5329 	}
5330 	default:
5331 		mpt_prt(mpt, "Unknown Target Address Reply Function code: "
5332 		    "0x%x\n", reply_frame->Function);
5333 		break;
5334 	}
5335 	return (TRUE);
5336 }
5337