xref: /freebsd/sys/dev/mps/mps_sas.c (revision 0e1497aefd602cea581d2380d22e67dfdcac6b4e)
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /* Communications core for LSI MPT2 */
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/selinfo.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/conf.h>
40 #include <sys/bio.h>
41 #include <sys/malloc.h>
42 #include <sys/uio.h>
43 #include <sys/sysctl.h>
44 #include <sys/sglist.h>
45 #include <sys/endian.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/rman.h>
50 
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_debug.h>
54 #include <cam/cam_sim.h>
55 #include <cam/cam_xpt_sim.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_periph.h>
58 #include <cam/scsi/scsi_all.h>
59 #include <cam/scsi/scsi_message.h>
60 #if __FreeBSD_version >= 900026
61 #include <cam/scsi/smp_all.h>
62 #endif
63 
64 #include <dev/mps/mpi/mpi2_type.h>
65 #include <dev/mps/mpi/mpi2.h>
66 #include <dev/mps/mpi/mpi2_ioc.h>
67 #include <dev/mps/mpi/mpi2_sas.h>
68 #include <dev/mps/mpi/mpi2_cnfg.h>
69 #include <dev/mps/mpi/mpi2_init.h>
70 #include <dev/mps/mpsvar.h>
71 #include <dev/mps/mps_table.h>
72 
73 struct mpssas_target {
74 	uint16_t	handle;
75 	uint8_t		linkrate;
76 	uint64_t	devname;
77 	uint64_t	sasaddr;
78 	uint32_t	devinfo;
79 	uint16_t	encl_handle;
80 	uint16_t	encl_slot;
81 	uint16_t	parent_handle;
82 	int		flags;
83 #define MPSSAS_TARGET_INABORT	(1 << 0)
84 #define MPSSAS_TARGET_INRESET	(1 << 1)
85 #define MPSSAS_TARGET_INCHIPRESET (1 << 2)
86 #define MPSSAS_TARGET_INRECOVERY 0x7
87 	uint16_t	tid;
88 };
89 
90 struct mpssas_softc {
91 	struct mps_softc	*sc;
92 	u_int			flags;
93 #define MPSSAS_IN_DISCOVERY	(1 << 0)
94 #define MPSSAS_IN_STARTUP	(1 << 1)
95 #define MPSSAS_DISCOVERY_TIMEOUT_PENDING	(1 << 2)
96 #define MPSSAS_QUEUE_FROZEN	(1 << 3)
97 	struct mpssas_target	*targets;
98 	struct cam_devq		*devq;
99 	struct cam_sim		*sim;
100 	struct cam_path		*path;
101 	struct intr_config_hook	sas_ich;
102 	struct callout		discovery_callout;
103 	u_int			discovery_timeouts;
104 	struct mps_event_handle	*mpssas_eh;
105 };
106 
107 struct mpssas_devprobe {
108 	struct mps_config_params	params;
109 	u_int			state;
110 #define MPSSAS_PROBE_DEV1	0x01
111 #define MPSSAS_PROBE_DEV2	0x02
112 #define MPSSAS_PROBE_PHY	0x03
113 #define MPSSAS_PROBE_EXP	0x04
114 #define MPSSAS_PROBE_PHY2	0x05
115 #define MPSSAS_PROBE_EXP2	0x06
116 	struct mpssas_target	target;
117 };
118 
119 #define MPSSAS_DISCOVERY_TIMEOUT	20
120 #define MPSSAS_MAX_DISCOVERY_TIMEOUTS	10 /* 200 seconds */
121 
122 MALLOC_DEFINE(M_MPSSAS, "MPSSAS", "MPS SAS memory");
123 
124 static __inline int mpssas_set_lun(uint8_t *lun, u_int ccblun);
125 static struct mpssas_target * mpssas_alloc_target(struct mpssas_softc *,
126     struct mpssas_target *);
127 static struct mpssas_target * mpssas_find_target(struct mpssas_softc *, int,
128      uint16_t);
129 static void mpssas_announce_device(struct mpssas_softc *,
130      struct mpssas_target *);
131 static void mpssas_startup(void *data);
132 static void mpssas_discovery_end(struct mpssas_softc *sassc);
133 static void mpssas_discovery_timeout(void *data);
134 static void mpssas_prepare_remove(struct mpssas_softc *,
135     MPI2_EVENT_SAS_TOPO_PHY_ENTRY *);
136 static void mpssas_remove_device(struct mps_softc *, struct mps_command *);
137 static void mpssas_remove_complete(struct mps_softc *, struct mps_command *);
138 static void mpssas_action(struct cam_sim *sim, union ccb *ccb);
139 static void mpssas_poll(struct cam_sim *sim);
140 static void mpssas_probe_device(struct mps_softc *sc, uint16_t handle);
141 static void mpssas_probe_device_complete(struct mps_softc *sc,
142      struct mps_config_params *params);
143 static void mpssas_scsiio_timeout(void *data);
144 static void mpssas_abort_complete(struct mps_softc *sc, struct mps_command *cm);
145 static void mpssas_recovery(struct mps_softc *, struct mps_command *);
146 static int mpssas_map_tm_request(struct mps_softc *sc, struct mps_command *cm);
147 static void mpssas_issue_tm_request(struct mps_softc *sc,
148 				    struct mps_command *cm);
149 static void mpssas_tm_complete(struct mps_softc *sc, struct mps_command *cm,
150 			       int error);
151 static int mpssas_complete_tm_request(struct mps_softc *sc,
152 				      struct mps_command *cm, int free_cm);
153 static void mpssas_action_scsiio(struct mpssas_softc *, union ccb *);
154 static void mpssas_scsiio_complete(struct mps_softc *, struct mps_command *);
155 #if __FreeBSD_version >= 900026
156 static void mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm);
157 static void mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb,
158 			       uint64_t sasaddr);
159 static void mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb);
160 #endif /* __FreeBSD_version >= 900026 */
161 static void mpssas_resetdev(struct mpssas_softc *, struct mps_command *);
162 static void mpssas_action_resetdev(struct mpssas_softc *, union ccb *);
163 static void mpssas_resetdev_complete(struct mps_softc *, struct mps_command *);
164 static void mpssas_freeze_device(struct mpssas_softc *, struct mpssas_target *);
165 static void mpssas_unfreeze_device(struct mpssas_softc *, struct mpssas_target *) __unused;
166 
167 /*
168  * Abstracted so that the driver can be backwards and forwards compatible
169  * with future versions of CAM that will provide this functionality.
170  */
171 #define MPS_SET_LUN(lun, ccblun)	\
172 	mpssas_set_lun(lun, ccblun)
173 
174 static __inline int
175 mpssas_set_lun(uint8_t *lun, u_int ccblun)
176 {
177 	uint64_t *newlun;
178 
179 	newlun = (uint64_t *)lun;
180 	*newlun = 0;
181 	if (ccblun <= 0xff) {
182 		/* Peripheral device address method, LUN is 0 to 255 */
183 		lun[1] = ccblun;
184 	} else if (ccblun <= 0x3fff) {
185 		/* Flat space address method, LUN is <= 16383 */
186 		scsi_ulto2b(ccblun, lun);
187 		lun[0] |= 0x40;
188 	} else if (ccblun <= 0xffffff) {
189 		/* Extended flat space address method, LUN is <= 16777215 */
190 		scsi_ulto3b(ccblun, &lun[1]);
191 		/* Extended Flat space address method */
192 		lun[0] = 0xc0;
193 		/* Length = 1, i.e. LUN is 3 bytes long */
194 		lun[0] |= 0x10;
195 		/* Extended Address Method */
196 		lun[0] |= 0x02;
197 	} else {
198 		return (EINVAL);
199 	}
200 
201 	return (0);
202 }
203 
204 static struct mpssas_target *
205 mpssas_alloc_target(struct mpssas_softc *sassc, struct mpssas_target *probe)
206 {
207 	struct mpssas_target *target;
208 	int start;
209 
210 	mps_dprint(sassc->sc, MPS_TRACE, "%s\n", __func__);
211 
212 	/*
213 	 * If it's not a sata or sas target, CAM won't be able to see it.  Put
214 	 * it into a high-numbered slot so that it's accessible but not
215 	 * interrupting the target numbering sequence of real drives.
216 	 */
217 	if ((probe->devinfo & (MPI2_SAS_DEVICE_INFO_SSP_TARGET |
218 	    MPI2_SAS_DEVICE_INFO_STP_TARGET | MPI2_SAS_DEVICE_INFO_SATA_DEVICE))
219 	    == 0) {
220 		start = 200;
221 	} else {
222 		/*
223 		 * Use the enclosure number and slot number as a hint for target
224 		 * numbering.  If that doesn't produce a sane result, search the
225 		 * entire space.
226 		 */
227 #if 0
228 		start = probe->encl_handle * 16 + probe->encl_slot;
229 #else
230 		start = probe->encl_slot;
231 #endif
232 		if (start >= sassc->sc->facts->MaxTargets)
233 			start = 0;
234 	}
235 
236 	target = mpssas_find_target(sassc, start, 0);
237 
238 	/*
239 	 * Nothing found on the first pass, try a second pass that searches the
240 	 * entire space.
241 	 */
242 	if (target == NULL)
243 		target = mpssas_find_target(sassc, 0, 0);
244 
245 	return (target);
246 }
247 
248 static struct mpssas_target *
249 mpssas_find_target(struct mpssas_softc *sassc, int start, uint16_t handle)
250 {
251 	struct mpssas_target *target;
252 	int i;
253 
254 	for (i = start; i < sassc->sc->facts->MaxTargets; i++) {
255 		target = &sassc->targets[i];
256 		if (target->handle == handle)
257 			return (target);
258 	}
259 
260 	return (NULL);
261 }
262 
263 /*
264  * Start the probe sequence for a given device handle.  This will not
265  * block.
266  */
267 static void
268 mpssas_probe_device(struct mps_softc *sc, uint16_t handle)
269 {
270 	struct mpssas_devprobe *probe;
271 	struct mps_config_params *params;
272 	MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
273 	int error;
274 
275 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
276 
277 	probe = malloc(sizeof(*probe), M_MPSSAS, M_NOWAIT | M_ZERO);
278 	if (probe == NULL) {
279 		mps_dprint(sc, MPS_FAULT, "Out of memory starting probe\n");
280 		return;
281 	}
282 	params = &probe->params;
283 	hdr = &params->hdr.Ext;
284 
285 	params->action = MPI2_CONFIG_ACTION_PAGE_HEADER;
286 	params->page_address = MPI2_SAS_DEVICE_PGAD_FORM_HANDLE | handle;
287 	hdr->ExtPageType = MPI2_CONFIG_EXTPAGETYPE_SAS_DEVICE;
288 	hdr->ExtPageLength = 0;
289 	hdr->PageNumber = 0;
290 	hdr->PageVersion = 0;
291 	params->buffer = NULL;
292 	params->length = 0;
293 	params->callback = mpssas_probe_device_complete;
294 	params->cbdata = probe;
295 	probe->target.handle = handle;
296 	probe->state = MPSSAS_PROBE_DEV1;
297 
298 	if ((error = mps_read_config_page(sc, params)) != 0) {
299 		free(probe, M_MPSSAS);
300 		mps_dprint(sc, MPS_FAULT, "Failure starting device probe\n");
301 		return;
302 	}
303 }
304 
305 static void
306 mpssas_probe_device_complete(struct mps_softc *sc,
307     struct mps_config_params *params)
308 {
309 	MPI2_CONFIG_EXTENDED_PAGE_HEADER *hdr;
310 	struct mpssas_devprobe *probe;
311 	int error;
312 
313 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
314 
315 	hdr = &params->hdr.Ext;
316 	probe = params->cbdata;
317 
318 	switch (probe->state) {
319 	case MPSSAS_PROBE_DEV1:
320 	case MPSSAS_PROBE_PHY:
321 	case MPSSAS_PROBE_EXP:
322 		if (params->status != MPI2_IOCSTATUS_SUCCESS) {
323 			mps_dprint(sc, MPS_FAULT,
324 			    "Probe Failure 0x%x state %d\n", params->status,
325 			    probe->state);
326 			free(probe, M_MPSSAS);
327 			return;
328 		}
329 		params->action = MPI2_CONFIG_ACTION_PAGE_READ_CURRENT;
330 		params->length = hdr->ExtPageLength * 4;
331 		params->buffer = malloc(params->length, M_MPSSAS,
332 		    M_ZERO|M_NOWAIT);
333 		if (params->buffer == NULL) {
334 			mps_dprint(sc, MPS_FAULT, "Out of memory at state "
335 			   "0x%x, size 0x%x\n", probe->state, params->length);
336 			free(probe, M_MPSSAS);
337 			return;
338 		}
339 		if (probe->state == MPSSAS_PROBE_DEV1)
340 			probe->state = MPSSAS_PROBE_DEV2;
341 		else if (probe->state == MPSSAS_PROBE_PHY)
342 			probe->state = MPSSAS_PROBE_PHY2;
343 		else if (probe->state == MPSSAS_PROBE_EXP)
344 			probe->state = MPSSAS_PROBE_EXP2;
345 		error = mps_read_config_page(sc, params);
346 		break;
347 	case MPSSAS_PROBE_DEV2:
348 	{
349 		MPI2_CONFIG_PAGE_SAS_DEV_0 *buf;
350 
351 		if (params->status != MPI2_IOCSTATUS_SUCCESS) {
352 			mps_dprint(sc, MPS_FAULT,
353 			    "Probe Failure 0x%x state %d\n", params->status,
354 			    probe->state);
355 			free(params->buffer, M_MPSSAS);
356 			free(probe, M_MPSSAS);
357 			return;
358 		}
359 		buf = params->buffer;
360 		mps_print_sasdev0(sc, buf);
361 
362 		probe->target.devname = mps_to_u64(&buf->DeviceName);
363 		probe->target.devinfo = buf->DeviceInfo;
364 		probe->target.encl_handle = buf->EnclosureHandle;
365 		probe->target.encl_slot = buf->Slot;
366 		probe->target.sasaddr = mps_to_u64(&buf->SASAddress);
367 		probe->target.parent_handle = buf->ParentDevHandle;
368 
369 		if (buf->DeviceInfo & MPI2_SAS_DEVICE_INFO_DIRECT_ATTACH) {
370 			params->page_address =
371 			    MPI2_SAS_PHY_PGAD_FORM_PHY_NUMBER | buf->PhyNum;
372 			hdr->ExtPageType = MPI2_CONFIG_EXTPAGETYPE_SAS_PHY;
373 			hdr->PageNumber = 0;
374 			probe->state = MPSSAS_PROBE_PHY;
375 		} else {
376 			params->page_address =
377 			    MPI2_SAS_EXPAND_PGAD_FORM_HNDL_PHY_NUM |
378 			    buf->ParentDevHandle | (buf->PhyNum << 16);
379 			hdr->ExtPageType = MPI2_CONFIG_EXTPAGETYPE_SAS_EXPANDER;
380 			hdr->PageNumber = 1;
381 			probe->state = MPSSAS_PROBE_EXP;
382 		}
383 		params->action = MPI2_CONFIG_ACTION_PAGE_HEADER;
384 		hdr->ExtPageLength = 0;
385 		hdr->PageVersion = 0;
386 		params->buffer = NULL;
387 		params->length = 0;
388 		free(buf, M_MPSSAS);
389 		error = mps_read_config_page(sc, params);
390 		break;
391 	}
392 	case MPSSAS_PROBE_PHY2:
393 	case MPSSAS_PROBE_EXP2:
394 	{
395 		MPI2_CONFIG_PAGE_SAS_PHY_0 *phy;
396 		MPI2_CONFIG_PAGE_EXPANDER_1 *exp;
397 		struct mpssas_softc *sassc;
398 		struct mpssas_target *targ;
399 		char devstring[80];
400 		uint16_t handle;
401 
402 		if (params->status != MPI2_IOCSTATUS_SUCCESS) {
403 			mps_dprint(sc, MPS_FAULT,
404 			    "Probe Failure 0x%x state %d\n", params->status,
405 			    probe->state);
406 			free(params->buffer, M_MPSSAS);
407 			free(probe, M_MPSSAS);
408 			return;
409 		}
410 
411 		if (probe->state == MPSSAS_PROBE_PHY2) {
412 			phy = params->buffer;
413 			mps_print_sasphy0(sc, phy);
414 			probe->target.linkrate = phy->NegotiatedLinkRate & 0xf;
415 		} else {
416 			exp = params->buffer;
417 			mps_print_expander1(sc, exp);
418 			probe->target.linkrate = exp->NegotiatedLinkRate & 0xf;
419 		}
420 		free(params->buffer, M_MPSSAS);
421 
422 		sassc = sc->sassc;
423 		handle = probe->target.handle;
424 		if ((targ = mpssas_find_target(sassc, 0, handle)) != NULL) {
425 			mps_printf(sc, "Ignoring dup device handle 0x%04x\n",
426 			    handle);
427 			free(probe, M_MPSSAS);
428 			return;
429 		}
430 		if ((targ = mpssas_alloc_target(sassc, &probe->target)) == NULL) {
431 			mps_printf(sc, "Target table overflow, handle 0x%04x\n",
432 			    handle);
433 			free(probe, M_MPSSAS);
434 			return;
435 		}
436 
437 		*targ = probe->target;	/* Copy the attributes */
438 		targ->tid = targ - sassc->targets;
439 		mps_describe_devinfo(targ->devinfo, devstring, 80);
440 		if (bootverbose)
441 			mps_printf(sc, "Found device <%s> <%s> <0x%04x> "
442 			    "<%d/%d>\n", devstring,
443 			    mps_describe_table(mps_linkrate_names,
444 			    targ->linkrate), targ->handle, targ->encl_handle,
445 			    targ->encl_slot);
446 
447 		free(probe, M_MPSSAS);
448 		mpssas_announce_device(sassc, targ);
449 		break;
450 	}
451 	default:
452 		printf("what?\n");
453 	}
454 }
455 
456 /*
457  * The MPT2 firmware performs debounce on the link to avoid transient link errors
458  * and false removals.  When it does decide that link has been lost and a device
459  * need to go away, it expects that the host will perform a target reset and then
460  * an op remove.  The reset has the side-effect of aborting any outstanding
461  * requests for the device, which is required for the op-remove to succeed.  It's
462  * not clear if the host should check for the device coming back alive after the
463  * reset.
464  */
465 static void
466 mpssas_prepare_remove(struct mpssas_softc *sassc, MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy)
467 {
468 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
469 	struct mps_softc *sc;
470 	struct mps_command *cm;
471 	struct mpssas_target *targ = NULL;
472 	uint16_t handle;
473 
474 	mps_dprint(sassc->sc, MPS_TRACE, "%s\n", __func__);
475 
476 	handle = phy->AttachedDevHandle;
477 	targ = mpssas_find_target(sassc, 0, handle);
478 	if (targ == NULL)
479 		/* We don't know about this device? */
480 		return;
481 
482 	sc = sassc->sc;
483 	cm = mps_alloc_command(sc);
484 	if (cm == NULL) {
485 		mps_printf(sc, "comand alloc failure in mpssas_prepare_remove\n");
486 		return;
487 	}
488 
489 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
490 	req->DevHandle = targ->handle;
491 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
492 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
493 
494 	/* SAS Hard Link Reset / SATA Link Reset */
495 	req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
496 
497 	cm->cm_data = NULL;
498 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
499 	cm->cm_complete = mpssas_remove_device;
500 	cm->cm_targ = targ;
501 	mpssas_issue_tm_request(sc, cm);
502 }
503 
504 static void
505 mpssas_remove_device(struct mps_softc *sc, struct mps_command *cm)
506 {
507 	MPI2_SCSI_TASK_MANAGE_REPLY *reply;
508 	MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
509 	struct mpssas_target *targ;
510 	uint16_t handle;
511 
512 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
513 
514 	reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)cm->cm_reply;
515 	handle = cm->cm_targ->handle;
516 
517 	mpssas_complete_tm_request(sc, cm, /*free_cm*/ 0);
518 
519 	if (reply->IOCStatus != MPI2_IOCSTATUS_SUCCESS) {
520 		mps_printf(sc, "Failure 0x%x reseting device 0x%04x\n",
521 		   reply->IOCStatus, handle);
522 		mps_free_command(sc, cm);
523 		return;
524 	}
525 
526 	mps_printf(sc, "Reset aborted %d commands\n", reply->TerminationCount);
527 	mps_free_reply(sc, cm->cm_reply_data);
528 
529 	/* Reuse the existing command */
530 	req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)cm->cm_req;
531 	req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
532 	req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
533 	req->DevHandle = handle;
534 	cm->cm_data = NULL;
535 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
536 	cm->cm_flags &= ~MPS_CM_FLAGS_COMPLETE;
537 	cm->cm_complete = mpssas_remove_complete;
538 
539 	mps_map_command(sc, cm);
540 
541 	mps_dprint(sc, MPS_INFO, "clearing target handle 0x%04x\n", handle);
542 	targ = mpssas_find_target(sc->sassc, 0, handle);
543 	if (targ != NULL) {
544 		targ->handle = 0x0;
545 		mpssas_announce_device(sc->sassc, targ);
546 	}
547 }
548 
549 static void
550 mpssas_remove_complete(struct mps_softc *sc, struct mps_command *cm)
551 {
552 	MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
553 
554 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
555 
556 	reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)cm->cm_reply;
557 
558 	mps_printf(sc, "mpssas_remove_complete on target 0x%04x,"
559 	   " IOCStatus= 0x%x\n", cm->cm_targ->tid, reply->IOCStatus);
560 
561 	mps_free_command(sc, cm);
562 }
563 
564 static void
565 mpssas_evt_handler(struct mps_softc *sc, uintptr_t data,
566     MPI2_EVENT_NOTIFICATION_REPLY *event)
567 {
568 	struct mpssas_softc *sassc;
569 
570 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
571 
572 	sassc = sc->sassc;
573 	mps_print_evt_sas(sc, event);
574 
575 	switch (event->Event) {
576 	case MPI2_EVENT_SAS_DISCOVERY:
577 	{
578 		MPI2_EVENT_DATA_SAS_DISCOVERY *data;
579 
580 		data = (MPI2_EVENT_DATA_SAS_DISCOVERY *)&event->EventData;
581 
582 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_STARTED)
583 			mps_dprint(sc, MPS_TRACE,"SAS discovery start event\n");
584 		if (data->ReasonCode & MPI2_EVENT_SAS_DISC_RC_COMPLETED) {
585 			mps_dprint(sc, MPS_TRACE, "SAS discovery end event\n");
586 			sassc->flags &= ~MPSSAS_IN_DISCOVERY;
587 			mpssas_discovery_end(sassc);
588 		}
589 		break;
590 	}
591 	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
592 	{
593 		MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *data;
594 		MPI2_EVENT_SAS_TOPO_PHY_ENTRY *phy;
595 		int i;
596 
597 		data = (MPI2_EVENT_DATA_SAS_TOPOLOGY_CHANGE_LIST *)
598 		    &event->EventData;
599 
600 		if (data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED) {
601 			if (bootverbose)
602 				printf("Expander found at enclosure %d\n",
603 				    data->EnclosureHandle);
604 			mpssas_probe_device(sc, data->ExpanderDevHandle);
605 		}
606 
607 		for (i = 0; i < data->NumEntries; i++) {
608 			phy = &data->PHY[i];
609 			switch (phy->PhyStatus & MPI2_EVENT_SAS_TOPO_RC_MASK) {
610 			case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
611 				mpssas_probe_device(sc, phy->AttachedDevHandle);
612 				break;
613 			case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
614 				mpssas_prepare_remove(sassc, phy);
615 				break;
616 			case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
617 			case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
618 			case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
619 			default:
620 				break;
621 			}
622 		}
623 
624 		break;
625 	}
626 	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
627 		break;
628 	default:
629 		break;
630 	}
631 
632 	mps_free_reply(sc, data);
633 }
634 
635 static int
636 mpssas_register_events(struct mps_softc *sc)
637 {
638 	uint8_t events[16];
639 
640 	bzero(events, 16);
641 	setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
642 	setbit(events, MPI2_EVENT_SAS_DISCOVERY);
643 	setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
644 	setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
645 	setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
646 	setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
647 	setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
648 
649 	mps_register_events(sc, events, mpssas_evt_handler, NULL,
650 	    &sc->sassc->mpssas_eh);
651 
652 	return (0);
653 }
654 
655 int
656 mps_attach_sas(struct mps_softc *sc)
657 {
658 	struct mpssas_softc *sassc;
659 	int error = 0;
660 	int num_sim_reqs;
661 
662 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
663 
664 	sassc = malloc(sizeof(struct mpssas_softc), M_MPT2, M_WAITOK|M_ZERO);
665 	sassc->targets = malloc(sizeof(struct mpssas_target) *
666 	    sc->facts->MaxTargets, M_MPT2, M_WAITOK|M_ZERO);
667 	sc->sassc = sassc;
668 	sassc->sc = sc;
669 
670 	/*
671 	 * Tell CAM that we can handle 5 fewer requests than we have
672 	 * allocated.  If we allow the full number of requests, all I/O
673 	 * will halt when we run out of resources.  Things work fine with
674 	 * just 1 less request slot given to CAM than we have allocated.
675 	 * We also need a couple of extra commands so that we can send down
676 	 * abort, reset, etc. requests when commands time out.  Otherwise
677 	 * we could wind up in a situation with sc->num_reqs requests down
678 	 * on the card and no way to send an abort.
679 	 *
680 	 * XXX KDM need to figure out why I/O locks up if all commands are
681 	 * used.
682 	 */
683 	num_sim_reqs = sc->num_reqs - 5;
684 
685 	if ((sassc->devq = cam_simq_alloc(num_sim_reqs)) == NULL) {
686 		mps_dprint(sc, MPS_FAULT, "Cannot allocate SIMQ\n");
687 		error = ENOMEM;
688 		goto out;
689 	}
690 
691 	sassc->sim = cam_sim_alloc(mpssas_action, mpssas_poll, "mps", sassc,
692 	    device_get_unit(sc->mps_dev), &sc->mps_mtx, num_sim_reqs,
693 	    num_sim_reqs, sassc->devq);
694 	if (sassc->sim == NULL) {
695 		mps_dprint(sc, MPS_FAULT, "Cannot allocate SIM\n");
696 		error = EINVAL;
697 		goto out;
698 	}
699 
700 	/*
701 	 * XXX There should be a bus for every port on the adapter, but since
702 	 * we're just going to fake the topology for now, we'll pretend that
703 	 * everything is just a target on a single bus.
704 	 */
705 	mps_lock(sc);
706 	if ((error = xpt_bus_register(sassc->sim, sc->mps_dev, 0)) != 0) {
707 		mps_dprint(sc, MPS_FAULT, "Error %d registering SCSI bus\n",
708 		    error);
709 		mps_unlock(sc);
710 		goto out;
711 	}
712 
713 	/*
714 	 * Assume that discovery events will start right away.  Freezing
715 	 * the simq will prevent the CAM boottime scanner from running
716 	 * before discovery is complete.
717 	 */
718 	sassc->flags = MPSSAS_IN_STARTUP | MPSSAS_IN_DISCOVERY;
719 	xpt_freeze_simq(sassc->sim, 1);
720 
721 	mps_unlock(sc);
722 
723 	callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
724 	sassc->discovery_timeouts = 0;
725 
726 	mpssas_register_events(sc);
727 out:
728 	if (error)
729 		mps_detach_sas(sc);
730 	return (error);
731 }
732 
733 int
734 mps_detach_sas(struct mps_softc *sc)
735 {
736 	struct mpssas_softc *sassc;
737 
738 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
739 
740 	if (sc->sassc == NULL)
741 		return (0);
742 
743 	sassc = sc->sassc;
744 
745 	/* Make sure CAM doesn't wedge if we had to bail out early. */
746 	mps_lock(sc);
747 	if (sassc->flags & MPSSAS_IN_STARTUP)
748 		xpt_release_simq(sassc->sim, 1);
749 	mps_unlock(sc);
750 
751 	if (sassc->mpssas_eh != NULL)
752 		mps_deregister_events(sc, sassc->mpssas_eh);
753 
754 	mps_lock(sc);
755 
756 	if (sassc->sim != NULL) {
757 		xpt_bus_deregister(cam_sim_path(sassc->sim));
758 		cam_sim_free(sassc->sim, FALSE);
759 	}
760 	mps_unlock(sc);
761 
762 	if (sassc->devq != NULL)
763 		cam_simq_free(sassc->devq);
764 
765 	free(sassc->targets, M_MPT2);
766 	free(sassc, M_MPT2);
767 	sc->sassc = NULL;
768 
769 	return (0);
770 }
771 
772 static void
773 mpssas_discovery_end(struct mpssas_softc *sassc)
774 {
775 	struct mps_softc *sc = sassc->sc;
776 
777 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
778 
779 	if (sassc->flags & MPSSAS_DISCOVERY_TIMEOUT_PENDING)
780 		callout_stop(&sassc->discovery_callout);
781 
782 	if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) {
783 		mps_dprint(sc, MPS_INFO,
784 		    "mpssas_discovery_end: removing confighook\n");
785 		sassc->flags &= ~MPSSAS_IN_STARTUP;
786 		xpt_release_simq(sassc->sim, 1);
787 	}
788 #if 0
789 	mpssas_announce_device(sassc, NULL);
790 #endif
791 
792 }
793 
794 static void
795 mpssas_announce_device(struct mpssas_softc *sassc, struct mpssas_target *targ)
796 {
797 	union ccb *ccb;
798 	int bus, tid, lun;
799 
800 	/*
801 	 * Force a rescan, a hackish way to announce devices.
802 	 * XXX Doing a scan on an individual device is hackish in that it
803 	 *     won't scan the LUNs.
804 	 * XXX Does it matter if any of this fails?
805 	 */
806 	bus = cam_sim_path(sassc->sim);
807 	if (targ != NULL) {
808 		tid = targ->tid;
809 		lun = 0;
810 	} else {
811 		tid = CAM_TARGET_WILDCARD;
812 		lun = CAM_LUN_WILDCARD;
813 	}
814 	ccb = xpt_alloc_ccb_nowait();
815 	if (ccb == NULL)
816 		return;
817 	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, bus, tid,
818 	    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
819 		xpt_free_ccb(ccb);
820 		return;
821 	}
822 	mps_dprint(sassc->sc, MPS_INFO, "Triggering rescan of %d:%d:-1\n",
823 	    bus, tid);
824 	xpt_rescan(ccb);
825 }
826 
827 static void
828 mpssas_startup(void *data)
829 {
830 	struct mpssas_softc *sassc = data;
831 
832 	mps_dprint(sassc->sc, MPS_TRACE, "%s\n", __func__);
833 
834 	mps_lock(sassc->sc);
835 	if ((sassc->flags & MPSSAS_IN_DISCOVERY) == 0) {
836 		mpssas_discovery_end(sassc);
837 	} else {
838 		if (sassc->discovery_timeouts < MPSSAS_MAX_DISCOVERY_TIMEOUTS) {
839 			sassc->flags |= MPSSAS_DISCOVERY_TIMEOUT_PENDING;
840 			callout_reset(&sassc->discovery_callout,
841 			    MPSSAS_DISCOVERY_TIMEOUT * hz,
842 			    mpssas_discovery_timeout, sassc);
843 			sassc->discovery_timeouts++;
844 		} else {
845 			mps_dprint(sassc->sc, MPS_FAULT,
846 			    "Discovery timed out, continuing.\n");
847 			sassc->flags &= ~MPSSAS_IN_DISCOVERY;
848 			mpssas_discovery_end(sassc);
849 		}
850 	}
851 	mps_unlock(sassc->sc);
852 
853 	return;
854 }
855 
856 static void
857 mpssas_discovery_timeout(void *data)
858 {
859 	struct mpssas_softc *sassc = data;
860 	struct mps_softc *sc;
861 
862 	sc = sassc->sc;
863 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
864 
865 	mps_lock(sc);
866 	mps_printf(sc,
867 	    "Timeout waiting for discovery, interrupts may not be working!\n");
868 	sassc->flags &= ~MPSSAS_DISCOVERY_TIMEOUT_PENDING;
869 
870 	/* Poll the hardware for events in case interrupts aren't working */
871 	mps_intr_locked(sc);
872 	mps_unlock(sc);
873 
874 	/* Check the status of discovery and re-arm the timeout if needed */
875 	mpssas_startup(sassc);
876 }
877 
878 static void
879 mpssas_action(struct cam_sim *sim, union ccb *ccb)
880 {
881 	struct mpssas_softc *sassc;
882 
883 	sassc = cam_sim_softc(sim);
884 
885 	mps_dprint(sassc->sc, MPS_TRACE, "%s func 0x%x\n", __func__,
886 	    ccb->ccb_h.func_code);
887 
888 	switch (ccb->ccb_h.func_code) {
889 	case XPT_PATH_INQ:
890 	{
891 		struct ccb_pathinq *cpi = &ccb->cpi;
892 
893 		cpi->version_num = 1;
894 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
895 		cpi->target_sprt = 0;
896 		cpi->hba_misc = PIM_NOBUSRESET;
897 		cpi->hba_eng_cnt = 0;
898 		cpi->max_target = sassc->sc->facts->MaxTargets - 1;
899 		cpi->max_lun = 0;
900 		cpi->initiator_id = 255;
901 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
902 		strncpy(cpi->hba_vid, "LSILogic", HBA_IDLEN);
903 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
904 		cpi->unit_number = cam_sim_unit(sim);
905 		cpi->bus_id = cam_sim_bus(sim);
906 		cpi->base_transfer_speed = 150000;
907 		cpi->transport = XPORT_SAS;
908 		cpi->transport_version = 0;
909 		cpi->protocol = PROTO_SCSI;
910 		cpi->protocol_version = SCSI_REV_SPC;
911 		cpi->ccb_h.status = CAM_REQ_CMP;
912 		break;
913 	}
914 	case XPT_GET_TRAN_SETTINGS:
915 	{
916 		struct ccb_trans_settings	*cts;
917 		struct ccb_trans_settings_sas	*sas;
918 		struct ccb_trans_settings_scsi	*scsi;
919 		struct mpssas_target *targ;
920 
921 		cts = &ccb->cts;
922 		sas = &cts->xport_specific.sas;
923 		scsi = &cts->proto_specific.scsi;
924 
925 		targ = &sassc->targets[cts->ccb_h.target_id];
926 		if (targ->handle == 0x0) {
927 			cts->ccb_h.status = CAM_TID_INVALID;
928 			break;
929 		}
930 
931 		cts->protocol_version = SCSI_REV_SPC2;
932 		cts->transport = XPORT_SAS;
933 		cts->transport_version = 0;
934 
935 		sas->valid = CTS_SAS_VALID_SPEED;
936 		switch (targ->linkrate) {
937 		case 0x08:
938 			sas->bitrate = 150000;
939 			break;
940 		case 0x09:
941 			sas->bitrate = 300000;
942 			break;
943 		case 0x0a:
944 			sas->bitrate = 600000;
945 			break;
946 		default:
947 			sas->valid = 0;
948 		}
949 
950 		cts->protocol = PROTO_SCSI;
951 		scsi->valid = CTS_SCSI_VALID_TQ;
952 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
953 
954 		cts->ccb_h.status = CAM_REQ_CMP;
955 		break;
956 	}
957 	case XPT_CALC_GEOMETRY:
958 		cam_calc_geometry(&ccb->ccg, /*extended*/1);
959 		ccb->ccb_h.status = CAM_REQ_CMP;
960 		break;
961 	case XPT_RESET_DEV:
962 		mpssas_action_resetdev(sassc, ccb);
963 		return;
964 	case XPT_RESET_BUS:
965 	case XPT_ABORT:
966 	case XPT_TERM_IO:
967 		ccb->ccb_h.status = CAM_REQ_CMP;
968 		break;
969 	case XPT_SCSI_IO:
970 		mpssas_action_scsiio(sassc, ccb);
971 		return;
972 #if __FreeBSD_version >= 900026
973 	case XPT_SMP_IO:
974 		mpssas_action_smpio(sassc, ccb);
975 		return;
976 #endif /* __FreeBSD_version >= 900026 */
977 	default:
978 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
979 		break;
980 	}
981 	xpt_done(ccb);
982 
983 }
984 
985 #if 0
986 static void
987 mpssas_resettimeout_complete(struct mps_softc *sc, struct mps_command *cm)
988 {
989 	MPI2_SCSI_TASK_MANAGE_REPLY *resp;
990 	uint16_t code;
991 
992 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
993 
994 	resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)cm->cm_reply;
995 	code = resp->ResponseCode;
996 
997 	mps_free_command(sc, cm);
998 	mpssas_unfreeze_device(sassc, targ);
999 
1000 	if (code != MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
1001 		mps_reset_controller(sc);
1002 	}
1003 
1004 	return;
1005 }
1006 #endif
1007 
1008 static void
1009 mpssas_scsiio_timeout(void *data)
1010 {
1011 	union ccb *ccb;
1012 	struct mps_softc *sc;
1013 	struct mps_command *cm;
1014 	struct mpssas_target *targ;
1015 #if 0
1016 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
1017 #endif
1018 
1019 	cm = (struct mps_command *)data;
1020 	sc = cm->cm_sc;
1021 
1022 	/*
1023 	 * Run the interrupt handler to make sure it's not pending.  This
1024 	 * isn't perfect because the command could have already completed
1025 	 * and been re-used, though this is unlikely.
1026 	 */
1027 	mps_lock(sc);
1028 	mps_intr_locked(sc);
1029 	if (cm->cm_state == MPS_CM_STATE_FREE) {
1030 		mps_unlock(sc);
1031 		return;
1032 	}
1033 
1034 	ccb = cm->cm_complete_data;
1035 	targ = cm->cm_targ;
1036 	if (targ == 0x00)
1037 		/* Driver bug */
1038 		targ = &sc->sassc->targets[ccb->ccb_h.target_id];
1039 
1040 	xpt_print(ccb->ccb_h.path, "SCSI command timeout on device handle "
1041 		  "0x%04x SMID %d\n", targ->handle, cm->cm_desc.Default.SMID);
1042 	/*
1043 	 * XXX KDM this is useful for debugging purposes, but the existing
1044 	 * scsi_op_desc() implementation can't handle a NULL value for
1045 	 * inq_data.  So this will remain commented out until I bring in
1046 	 * those changes as well.
1047 	 */
1048 #if 0
1049 	xpt_print(ccb->ccb_h.path, "Timed out command: %s. CDB %s\n",
1050 		  scsi_op_desc((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1051 		  		ccb->csio.cdb_io.cdb_ptr[0] :
1052 				ccb->csio.cdb_io.cdb_bytes[0], NULL),
1053 		  scsi_cdb_string((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1054 				   ccb->csio.cdb_io.cdb_ptr :
1055 				   ccb->csio.cdb_io.cdb_bytes, cdb_str,
1056 		  		   sizeof(cdb_str)));
1057 #endif
1058 
1059 	/* Inform CAM about the timeout and that recovery is starting. */
1060 #if 0
1061 	if ((targ->flags & MPSSAS_TARGET_INRECOVERY) == 0) {
1062 		mpssas_freeze_device(sc->sassc, targ);
1063 		ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1064 		xpt_done(ccb);
1065 	}
1066 #endif
1067 	mpssas_freeze_device(sc->sassc, targ);
1068 	ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1069 
1070 	/*
1071 	 * recycle the command into recovery so that there's no risk of
1072 	 * command allocation failure.
1073 	 */
1074 	cm->cm_state = MPS_CM_STATE_TIMEDOUT;
1075 	mpssas_recovery(sc, cm);
1076 	mps_unlock(sc);
1077 }
1078 
1079 static void
1080 mpssas_abort_complete(struct mps_softc *sc, struct mps_command *cm)
1081 {
1082 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1083 
1084 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
1085 
1086 	mps_printf(sc, "%s: abort request on handle %#04x SMID %d "
1087 		   "complete\n", __func__, req->DevHandle, req->TaskMID);
1088 
1089 	mpssas_complete_tm_request(sc, cm, /*free_cm*/ 1);
1090 }
1091 
1092 static void
1093 mpssas_recovery(struct mps_softc *sc, struct mps_command *abort_cm)
1094 {
1095 	struct mps_command *cm;
1096 	MPI2_SCSI_TASK_MANAGE_REQUEST *req, *orig_req;
1097 
1098 	cm = mps_alloc_command(sc);
1099 	if (cm == NULL) {
1100 		mps_printf(sc, "%s: command allocation failure\n", __func__);
1101 		return;
1102 	}
1103 
1104 	cm->cm_targ = abort_cm->cm_targ;
1105 	cm->cm_complete = mpssas_abort_complete;
1106 
1107 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
1108 	orig_req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)abort_cm->cm_req;
1109 	req->DevHandle = abort_cm->cm_targ->handle;
1110 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1111 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1112 	memcpy(req->LUN, orig_req->LUN, sizeof(req->LUN));
1113 	req->TaskMID = abort_cm->cm_desc.Default.SMID;
1114 
1115 	cm->cm_data = NULL;
1116 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1117 
1118 	mpssas_issue_tm_request(sc, cm);
1119 
1120 }
1121 
1122 /*
1123  * Can return 0 or EINPROGRESS on success.  Any other value means failure.
1124  */
1125 static int
1126 mpssas_map_tm_request(struct mps_softc *sc, struct mps_command *cm)
1127 {
1128 	int error;
1129 
1130 	error = 0;
1131 
1132 	cm->cm_flags |= MPS_CM_FLAGS_ACTIVE;
1133 	error = mps_map_command(sc, cm);
1134 	if ((error == 0)
1135 	 || (error == EINPROGRESS))
1136 		sc->tm_cmds_active++;
1137 
1138 	return (error);
1139 }
1140 
1141 static void
1142 mpssas_issue_tm_request(struct mps_softc *sc, struct mps_command *cm)
1143 {
1144 	int freeze_queue, send_command, error;
1145 
1146 	freeze_queue = 0;
1147 	send_command = 0;
1148 	error = 0;
1149 
1150 	mtx_assert(&sc->mps_mtx, MA_OWNED);
1151 
1152 	/*
1153 	 * If there are no other pending task management commands, go
1154 	 * ahead and send this one.  There is a small amount of anecdotal
1155 	 * evidence that sending lots of task management commands at once
1156 	 * may cause the controller to lock up.  Or, if the user has
1157 	 * configured the driver (via the allow_multiple_tm_cmds variable) to
1158 	 * not serialize task management commands, go ahead and send the
1159 	 * command if even other task management commands are pending.
1160 	 */
1161 	if (TAILQ_FIRST(&sc->tm_list) == NULL) {
1162 		send_command = 1;
1163 		freeze_queue = 1;
1164 	} else if (sc->allow_multiple_tm_cmds != 0)
1165 		send_command = 1;
1166 
1167 	TAILQ_INSERT_TAIL(&sc->tm_list, cm, cm_link);
1168 	if (send_command != 0) {
1169 		/*
1170 		 * Freeze the SIM queue while we issue the task management
1171 		 * command.  According to the Fusion-MPT 2.0 spec, task
1172 		 * management requests are serialized, and so the host
1173 		 * should not send any I/O requests while task management
1174 		 * requests are pending.
1175 		 */
1176 		if (freeze_queue != 0)
1177 			xpt_freeze_simq(sc->sassc->sim, 1);
1178 
1179 		error = mpssas_map_tm_request(sc, cm);
1180 
1181 		/*
1182 		 * At present, there is no error path back from
1183 		 * mpssas_map_tm_request() (which calls mps_map_command())
1184 		 * when cm->cm_data == NULL.  But since there is a return
1185 		 * value, we check it just in case the implementation
1186 		 * changes later.
1187 		 */
1188 		if ((error != 0)
1189 		 && (error != EINPROGRESS))
1190 			mpssas_tm_complete(sc, cm,
1191 			    MPI2_SCSITASKMGMT_RSP_TM_FAILED);
1192 	}
1193 }
1194 
1195 static void
1196 mpssas_tm_complete(struct mps_softc *sc, struct mps_command *cm, int error)
1197 {
1198 	MPI2_SCSI_TASK_MANAGE_REPLY *resp;
1199 
1200 	resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)cm->cm_reply;
1201 
1202 	resp->ResponseCode = error;
1203 
1204 	/*
1205 	 * Call the callback for this command, it will be
1206 	 * removed from the list and freed via the callback.
1207 	 */
1208 	cm->cm_complete(sc, cm);
1209 }
1210 
1211 /*
1212  * Complete a task management request.  The basic completion operation will
1213  * always succeed.  Returns status for sending any further task management
1214  * commands that were queued.
1215  */
1216 static int
1217 mpssas_complete_tm_request(struct mps_softc *sc, struct mps_command *cm,
1218 			   int free_cm)
1219 {
1220 	int error;
1221 
1222 	error = 0;
1223 
1224 	mtx_assert(&sc->mps_mtx, MA_OWNED);
1225 
1226 	TAILQ_REMOVE(&sc->tm_list, cm, cm_link);
1227 	cm->cm_flags &= ~MPS_CM_FLAGS_ACTIVE;
1228 	sc->tm_cmds_active--;
1229 
1230 	if (free_cm != 0)
1231 		mps_free_command(sc, cm);
1232 
1233 	if (TAILQ_FIRST(&sc->tm_list) == NULL) {
1234 		/*
1235 		 * Release the SIM queue, we froze it when we sent the first
1236 		 * task management request.
1237 		 */
1238 		xpt_release_simq(sc->sassc->sim, 1);
1239 	} else if ((sc->tm_cmds_active == 0)
1240 		|| (sc->allow_multiple_tm_cmds != 0)) {
1241 		int error;
1242 		struct mps_command *cm2;
1243 
1244 restart_traversal:
1245 
1246 		/*
1247 		 * We don't bother using TAILQ_FOREACH_SAFE here, but
1248 		 * rather use the standard version and just restart the
1249 		 * list traversal if we run into the error case.
1250 		 * TAILQ_FOREACH_SAFE allows safe removal of the current
1251 		 * list element, but if you have a queue of task management
1252 		 * commands, all of which have mapping errors, you'll end
1253 		 * up with recursive calls to this routine and so you could
1254 		 * wind up removing more than just the current list element.
1255 		 */
1256 		TAILQ_FOREACH(cm2, &sc->tm_list, cm_link) {
1257 			MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1258 
1259 			/* This command is active, no need to send it again */
1260 			if (cm2->cm_flags & MPS_CM_FLAGS_ACTIVE)
1261 				continue;
1262 
1263 			req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm2->cm_req;
1264 
1265 			mps_printf(sc, "%s: sending deferred task management "
1266 			    "request for handle %#04x SMID %d\n", __func__,
1267 			    req->DevHandle, req->TaskMID);
1268 
1269 			error = mpssas_map_tm_request(sc, cm2);
1270 
1271 			/*
1272 			 * Check for errors.  If we had an error, complete
1273 			 * this command with an error, and keep going through
1274 			 * the list until we are able to send at least one
1275 			 * command or all of them are completed with errors.
1276 			 *
1277 			 * We don't want to wind up in a situation where
1278 			 * we're stalled out with no way for queued task
1279 			 * management commands to complete.
1280 			 *
1281 			 * Note that there is not currently an error path
1282 			 * back from mpssas_map_tm_request() (which calls
1283 			 * mps_map_command()) when cm->cm_data == NULL.
1284 			 * But we still want to check for errors here in
1285 			 * case the implementation changes, or in case
1286 			 * there is some reason for a data payload here.
1287 			 */
1288 			if ((error != 0)
1289 			 && (error != EINPROGRESS)) {
1290 				mpssas_tm_complete(sc, cm,
1291 				    MPI2_SCSITASKMGMT_RSP_TM_FAILED);
1292 
1293 				/*
1294 				 * If we don't currently have any commands
1295 				 * active, go back to the beginning and see
1296 				 * if there are any more that can be started.
1297 				 * Otherwise, we're done here.
1298 				 */
1299 				if (sc->tm_cmds_active == 0)
1300 					goto restart_traversal;
1301 				else
1302 					break;
1303 			}
1304 
1305 			/*
1306 			 * If the user only wants one task management command
1307 			 * active at a time, we're done, since we've
1308 			 * already successfully sent a command at this point.
1309 			 */
1310 			if (sc->allow_multiple_tm_cmds == 0)
1311 				break;
1312 		}
1313 	}
1314 
1315 	return (error);
1316 }
1317 
1318 static void
1319 mpssas_action_scsiio(struct mpssas_softc *sassc, union ccb *ccb)
1320 {
1321 	MPI2_SCSI_IO_REQUEST *req;
1322 	struct ccb_scsiio *csio;
1323 	struct mps_softc *sc;
1324 	struct mpssas_target *targ;
1325 	struct mps_command *cm;
1326 
1327 	mps_dprint(sassc->sc, MPS_TRACE, "%s\n", __func__);
1328 
1329 	sc = sassc->sc;
1330 
1331 	csio = &ccb->csio;
1332 	targ = &sassc->targets[csio->ccb_h.target_id];
1333 	if (targ->handle == 0x0) {
1334 		csio->ccb_h.status = CAM_SEL_TIMEOUT;
1335 		xpt_done(ccb);
1336 		return;
1337 	}
1338 
1339 	cm = mps_alloc_command(sc);
1340 	if (cm == NULL) {
1341 		if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
1342 			xpt_freeze_simq(sassc->sim, 1);
1343 			sassc->flags |= MPSSAS_QUEUE_FROZEN;
1344 		}
1345 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1346 		ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1347 		xpt_done(ccb);
1348 		return;
1349 	}
1350 
1351 	req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1352 	req->DevHandle = targ->handle;
1353 	req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1354 	req->MsgFlags = 0;
1355 	req->SenseBufferLowAddress = cm->cm_sense_busaddr;
1356 	req->SenseBufferLength = MPS_SENSE_LEN;
1357 	req->SGLFlags = 0;
1358 	req->ChainOffset = 0;
1359 	req->SGLOffset0 = 24;	/* 32bit word offset to the SGL */
1360 	req->SGLOffset1= 0;
1361 	req->SGLOffset2= 0;
1362 	req->SGLOffset3= 0;
1363 	req->SkipCount = 0;
1364 	req->DataLength = csio->dxfer_len;
1365 	req->BidirectionalDataLength = 0;
1366 	req->IoFlags = csio->cdb_len;
1367 	req->EEDPFlags = 0;
1368 
1369 	/* Note: BiDirectional transfers are not supported */
1370 	switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1371 	case CAM_DIR_IN:
1372 		req->Control = MPI2_SCSIIO_CONTROL_READ;
1373 		cm->cm_flags |= MPS_CM_FLAGS_DATAIN;
1374 		break;
1375 	case CAM_DIR_OUT:
1376 		req->Control = MPI2_SCSIIO_CONTROL_WRITE;
1377 		cm->cm_flags |= MPS_CM_FLAGS_DATAOUT;
1378 		break;
1379 	case CAM_DIR_NONE:
1380 	default:
1381 		req->Control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1382 		break;
1383 	}
1384 
1385 	/*
1386 	 * It looks like the hardware doesn't require an explicit tag
1387 	 * number for each transaction.  SAM Task Management not supported
1388 	 * at the moment.
1389 	 */
1390 	switch (csio->tag_action) {
1391 	case MSG_HEAD_OF_Q_TAG:
1392 		req->Control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
1393 		break;
1394 	case MSG_ORDERED_Q_TAG:
1395 		req->Control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
1396 		break;
1397 	case MSG_ACA_TASK:
1398 		req->Control |= MPI2_SCSIIO_CONTROL_ACAQ;
1399 		break;
1400 	case CAM_TAG_ACTION_NONE:
1401 	case MSG_SIMPLE_Q_TAG:
1402 	default:
1403 		req->Control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
1404 		break;
1405 	}
1406 
1407 	if (MPS_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
1408 		mps_free_command(sc, cm);
1409 		ccb->ccb_h.status = CAM_LUN_INVALID;
1410 		xpt_done(ccb);
1411 		return;
1412 	}
1413 
1414 	if (csio->ccb_h.flags & CAM_CDB_POINTER)
1415 		bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
1416 	else
1417 		bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
1418 	req->IoFlags = csio->cdb_len;
1419 
1420 	/*
1421 	 * XXX need to handle S/G lists and physical addresses here.
1422 	 */
1423 	cm->cm_data = csio->data_ptr;
1424 	cm->cm_length = csio->dxfer_len;
1425 	cm->cm_sge = &req->SGL;
1426 	cm->cm_sglsize = (32 - 24) * 4;
1427 	cm->cm_desc.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1428 	cm->cm_desc.SCSIIO.DevHandle = targ->handle;
1429 	cm->cm_complete = mpssas_scsiio_complete;
1430 	cm->cm_complete_data = ccb;
1431 	cm->cm_targ = targ;
1432 
1433 	callout_reset(&cm->cm_callout, (ccb->ccb_h.timeout * hz) / 1000,
1434 	   mpssas_scsiio_timeout, cm);
1435 
1436 	mps_map_command(sc, cm);
1437 	return;
1438 }
1439 
1440 static void
1441 mpssas_scsiio_complete(struct mps_softc *sc, struct mps_command *cm)
1442 {
1443 	MPI2_SCSI_IO_REPLY *rep;
1444 	union ccb *ccb;
1445 	struct mpssas_softc *sassc;
1446 	u_int sense_len;
1447 	int dir = 0;
1448 
1449 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1450 
1451 	callout_stop(&cm->cm_callout);
1452 
1453 	sassc = sc->sassc;
1454 	ccb = cm->cm_complete_data;
1455 	rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
1456 
1457 	if (cm->cm_data != NULL) {
1458 		if (cm->cm_flags & MPS_CM_FLAGS_DATAIN)
1459 			dir = BUS_DMASYNC_POSTREAD;
1460 		else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT)
1461 			dir = BUS_DMASYNC_POSTWRITE;;
1462 		bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
1463 		bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
1464 	}
1465 
1466 	if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
1467 		ccb->ccb_h.flags |= CAM_RELEASE_SIMQ;
1468 		sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
1469 	}
1470 
1471 	/* Take the fast path to completion */
1472 	if (cm->cm_reply == NULL) {
1473 		ccb->ccb_h.status = CAM_REQ_CMP;
1474 		ccb->csio.scsi_status = SCSI_STATUS_OK;
1475 		mps_free_command(sc, cm);
1476 		xpt_done(ccb);
1477 		return;
1478 	}
1479 
1480 	mps_dprint(sc, MPS_INFO, "(%d:%d:%d) IOCStatus= 0x%x, "
1481 	    "ScsiStatus= 0x%x, SCSIState= 0x%x TransferCount= 0x%x\n",
1482 	    xpt_path_path_id(ccb->ccb_h.path),
1483 	    xpt_path_target_id(ccb->ccb_h.path),
1484 	    xpt_path_lun_id(ccb->ccb_h.path), rep->IOCStatus,
1485 	    rep->SCSIStatus, rep->SCSIState, rep->TransferCount);
1486 
1487 	switch (rep->IOCStatus & MPI2_IOCSTATUS_MASK) {
1488 	case MPI2_IOCSTATUS_BUSY:
1489 	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
1490 		/*
1491 		 * The controller is overloaded, try waiting a bit for it
1492 		 * to free up.
1493 		 */
1494 		ccb->ccb_h.status = CAM_BUSY;
1495 		break;
1496 	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
1497 		ccb->csio.resid = cm->cm_length - rep->TransferCount;
1498 		/* FALLTHROUGH */
1499 	case MPI2_IOCSTATUS_SUCCESS:
1500 	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
1501 		ccb->ccb_h.status = CAM_REQ_CMP;
1502 		break;
1503 	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
1504 		/* resid is ignored for this condition */
1505 		ccb->csio.resid = 0;
1506 		ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1507 		break;
1508 	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
1509 	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
1510 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1511 		break;
1512 	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
1513 		/*
1514 		 * This is one of the responses that comes back when an I/O
1515 		 * has been aborted.  If it is because of a timeout that we
1516 		 * initiated, just set the status to CAM_CMD_TIMEOUT.
1517 		 * Otherwise set it to CAM_REQ_ABORTED.  The effect on the
1518 		 * command is the same (it gets retried, subject to the
1519 		 * retry counter), the only difference is what gets printed
1520 		 * on the console.
1521 		 */
1522 		if (cm->cm_state == MPS_CM_STATE_TIMEDOUT)
1523 			ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1524 		else
1525 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1526 		break;
1527 	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
1528 	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
1529 		ccb->ccb_h.status = CAM_REQ_ABORTED;
1530 		break;
1531 	case MPI2_IOCSTATUS_INVALID_SGL:
1532 		mps_print_scsiio_cmd(sc, cm);
1533 		ccb->ccb_h.status = CAM_UNREC_HBA_ERROR;
1534 		break;
1535 	case MPI2_IOCSTATUS_INVALID_FUNCTION:
1536 	case MPI2_IOCSTATUS_INTERNAL_ERROR:
1537 	case MPI2_IOCSTATUS_INVALID_VPID:
1538 	case MPI2_IOCSTATUS_INVALID_FIELD:
1539 	case MPI2_IOCSTATUS_INVALID_STATE:
1540 	case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
1541 	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
1542 	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1543 	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
1544 	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
1545 	default:
1546 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1547 	}
1548 
1549 
1550 	if ((rep->SCSIState & MPI2_SCSI_STATE_NO_SCSI_STATUS) == 0) {
1551 		ccb->csio.scsi_status = rep->SCSIStatus;
1552 
1553 		switch (rep->SCSIStatus) {
1554 		case MPI2_SCSI_STATUS_TASK_SET_FULL:
1555 		case MPI2_SCSI_STATUS_CHECK_CONDITION:
1556 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1557 			break;
1558 		case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
1559 		case MPI2_SCSI_STATUS_TASK_ABORTED:
1560 			ccb->ccb_h.status = CAM_REQ_ABORTED;
1561 			break;
1562 		case MPI2_SCSI_STATUS_GOOD:
1563 		default:
1564 			break;
1565 		}
1566 	}
1567 
1568 	if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
1569 		sense_len = MIN(rep->SenseCount,
1570 		    sizeof(struct scsi_sense_data));
1571 		if (sense_len < rep->SenseCount)
1572 			ccb->csio.sense_resid = rep->SenseCount - sense_len;
1573 		bcopy(cm->cm_sense, &ccb->csio.sense_data, sense_len);
1574 		ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1575 	}
1576 
1577 	if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
1578 		ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1579 
1580 	if (rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
1581 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1582 
1583 	mps_free_command(sc, cm);
1584 	xpt_done(ccb);
1585 }
1586 
1587 #if __FreeBSD_version >= 900026
1588 static void
1589 mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm)
1590 {
1591 	MPI2_SMP_PASSTHROUGH_REPLY *rpl;
1592 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
1593 	uint64_t sasaddr;
1594 	union ccb *ccb;
1595 
1596 	ccb = cm->cm_complete_data;
1597 	rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
1598 	if (rpl == NULL) {
1599 		mps_dprint(sc, MPS_INFO, "%s: NULL cm_reply!\n", __func__);
1600 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1601 		goto bailout;
1602 	}
1603 
1604 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
1605 	sasaddr = le32toh(req->SASAddress.Low);
1606 	sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
1607 
1608 	if ((rpl->IOCStatus & MPI2_IOCSTATUS_MASK) != MPI2_IOCSTATUS_SUCCESS ||
1609 	    rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
1610 		mps_dprint(sc, MPS_INFO, "%s: IOCStatus %04x SASStatus %02x\n",
1611 		    __func__, rpl->IOCStatus, rpl->SASStatus);
1612 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1613 		goto bailout;
1614 	}
1615 
1616 	mps_dprint(sc, MPS_INFO, "%s: SMP request to SAS address "
1617 		   "%#jx completed successfully\n", __func__,
1618 		   (uintmax_t)sasaddr);
1619 
1620 	if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
1621 		ccb->ccb_h.status = CAM_REQ_CMP;
1622 	else
1623 		ccb->ccb_h.status = CAM_SMP_STATUS_ERROR;
1624 
1625 bailout:
1626 	/*
1627 	 * We sync in both directions because we had DMAs in the S/G list
1628 	 * in both directions.
1629 	 */
1630 	bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
1631 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1632 	bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
1633 	mps_free_command(sc, cm);
1634 	xpt_done(ccb);
1635 }
1636 
1637 static void
1638 mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
1639 {
1640 	struct mps_command *cm;
1641 	uint8_t *request, *response;
1642 	MPI2_SMP_PASSTHROUGH_REQUEST *req;
1643 	struct mps_softc *sc;
1644 	struct sglist *sg;
1645 	int error;
1646 
1647 	sc = sassc->sc;
1648 	sg = NULL;
1649 	error = 0;
1650 
1651 	/*
1652 	 * XXX We don't yet support physical addresses here.
1653 	 */
1654 	if (ccb->ccb_h.flags & (CAM_DATA_PHYS|CAM_SG_LIST_PHYS)) {
1655 		mps_printf(sc, "%s: physical addresses not supported\n",
1656 			   __func__);
1657 		ccb->ccb_h.status = CAM_REQ_INVALID;
1658 		xpt_done(ccb);
1659 		return;
1660 	}
1661 
1662 	/*
1663 	 * If the user wants to send an S/G list, check to make sure they
1664 	 * have single buffers.
1665 	 */
1666 	if (ccb->ccb_h.flags & CAM_SCATTER_VALID) {
1667 		/*
1668 		 * The chip does not support more than one buffer for the
1669 		 * request or response.
1670 		 */
1671 	 	if ((ccb->smpio.smp_request_sglist_cnt > 1)
1672 		  || (ccb->smpio.smp_response_sglist_cnt > 1)) {
1673 			mps_printf(sc, "%s: multiple request or response "
1674 				   "buffer segments not supported for SMP\n",
1675 				   __func__);
1676 			ccb->ccb_h.status = CAM_REQ_INVALID;
1677 			xpt_done(ccb);
1678 			return;
1679 		}
1680 
1681 		/*
1682 		 * The CAM_SCATTER_VALID flag was originally implemented
1683 		 * for the XPT_SCSI_IO CCB, which only has one data pointer.
1684 		 * We have two.  So, just take that flag to mean that we
1685 		 * might have S/G lists, and look at the S/G segment count
1686 		 * to figure out whether that is the case for each individual
1687 		 * buffer.
1688 		 */
1689 		if (ccb->smpio.smp_request_sglist_cnt != 0) {
1690 			bus_dma_segment_t *req_sg;
1691 
1692 			req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
1693 			request = (uint8_t *)req_sg[0].ds_addr;
1694 		} else
1695 			request = ccb->smpio.smp_request;
1696 
1697 		if (ccb->smpio.smp_response_sglist_cnt != 0) {
1698 			bus_dma_segment_t *rsp_sg;
1699 
1700 			rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
1701 			response = (uint8_t *)rsp_sg[0].ds_addr;
1702 		} else
1703 			response = ccb->smpio.smp_response;
1704 	} else {
1705 		request = ccb->smpio.smp_request;
1706 		response = ccb->smpio.smp_response;
1707 	}
1708 
1709 	cm = mps_alloc_command(sc);
1710 	if (cm == NULL) {
1711 		mps_printf(sc, "%s: cannot allocate command\n", __func__);
1712 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1713 		xpt_done(ccb);
1714 		return;
1715 	}
1716 
1717 	req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
1718 	bzero(req, sizeof(*req));
1719 	req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1720 
1721 	/* Allow the chip to use any route to this SAS address. */
1722 	req->PhysicalPort = 0xff;
1723 
1724 	req->RequestDataLength = ccb->smpio.smp_request_len;
1725 	req->SGLFlags =
1726 	    MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
1727 
1728 	mps_dprint(sc, MPS_INFO, "%s: sending SMP request to SAS "
1729 		   "address %#jx\n", __func__, (uintmax_t)sasaddr);
1730 
1731 	mpi_init_sge(cm, req, &req->SGL);
1732 
1733 	/*
1734 	 * Set up a uio to pass into mps_map_command().  This allows us to
1735 	 * do one map command, and one busdma call in there.
1736 	 */
1737 	cm->cm_uio.uio_iov = cm->cm_iovec;
1738 	cm->cm_uio.uio_iovcnt = 2;
1739 	cm->cm_uio.uio_segflg = UIO_SYSSPACE;
1740 
1741 	/*
1742 	 * The read/write flag isn't used by busdma, but set it just in
1743 	 * case.  This isn't exactly accurate, either, since we're going in
1744 	 * both directions.
1745 	 */
1746 	cm->cm_uio.uio_rw = UIO_WRITE;
1747 
1748 	cm->cm_iovec[0].iov_base = request;
1749 	cm->cm_iovec[0].iov_len = req->RequestDataLength;
1750 	cm->cm_iovec[1].iov_base = response;
1751 	cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
1752 
1753 	cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
1754 			       cm->cm_iovec[1].iov_len;
1755 
1756 	/*
1757 	 * Trigger a warning message in mps_data_cb() for the user if we
1758 	 * wind up exceeding two S/G segments.  The chip expects one
1759 	 * segment for the request and another for the response.
1760 	 */
1761 	cm->cm_max_segs = 2;
1762 
1763 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1764 	cm->cm_complete = mpssas_smpio_complete;
1765 	cm->cm_complete_data = ccb;
1766 
1767 	/*
1768 	 * Tell the mapping code that we're using a uio, and that this is
1769 	 * an SMP passthrough request.  There is a little special-case
1770 	 * logic there (in mps_data_cb()) to handle the bidirectional
1771 	 * transfer.
1772 	 */
1773 	cm->cm_flags |= MPS_CM_FLAGS_USE_UIO | MPS_CM_FLAGS_SMP_PASS |
1774 			MPS_CM_FLAGS_DATAIN | MPS_CM_FLAGS_DATAOUT;
1775 
1776 	/* The chip data format is little endian. */
1777 	req->SASAddress.High = htole32(sasaddr >> 32);
1778 	req->SASAddress.Low = htole32(sasaddr);
1779 
1780 	/*
1781 	 * XXX Note that we don't have a timeout/abort mechanism here.
1782 	 * From the manual, it looks like task management requests only
1783 	 * work for SCSI IO and SATA passthrough requests.  We may need to
1784 	 * have a mechanism to retry requests in the event of a chip reset
1785 	 * at least.  Hopefully the chip will insure that any errors short
1786 	 * of that are relayed back to the driver.
1787 	 */
1788 	error = mps_map_command(sc, cm);
1789 	if ((error != 0) && (error != EINPROGRESS)) {
1790 		mps_printf(sc, "%s: error %d returned from mps_map_command()\n",
1791 			   __func__, error);
1792 		goto bailout_error;
1793 	}
1794 
1795 	return;
1796 
1797 bailout_error:
1798 	mps_free_command(sc, cm);
1799 	ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1800 	xpt_done(ccb);
1801 	return;
1802 
1803 }
1804 
1805 static void
1806 mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb)
1807 {
1808 	struct mps_softc *sc;
1809 	struct mpssas_target *targ;
1810 	uint64_t sasaddr = 0;
1811 
1812 	sc = sassc->sc;
1813 
1814 	/*
1815 	 * Make sure the target exists.
1816 	 */
1817 	targ = &sassc->targets[ccb->ccb_h.target_id];
1818 	if (targ->handle == 0x0) {
1819 		mps_printf(sc, "%s: target %d does not exist!\n", __func__,
1820 			   ccb->ccb_h.target_id);
1821 		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1822 		xpt_done(ccb);
1823 		return;
1824 	}
1825 
1826 	/*
1827 	 * If this device has an embedded SMP target, we'll talk to it
1828 	 * directly.
1829 	 * figure out what the expander's address is.
1830 	 */
1831 	if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
1832 		sasaddr = targ->sasaddr;
1833 
1834 	/*
1835 	 * If we don't have a SAS address for the expander yet, try
1836 	 * grabbing it from the page 0x83 information cached in the
1837 	 * transport layer for this target.  LSI expanders report the
1838 	 * expander SAS address as the port-associated SAS address in
1839 	 * Inquiry VPD page 0x83.  Maxim expanders don't report it in page
1840 	 * 0x83.
1841 	 *
1842 	 * XXX KDM disable this for now, but leave it commented out so that
1843 	 * it is obvious that this is another possible way to get the SAS
1844 	 * address.
1845 	 *
1846 	 * The parent handle method below is a little more reliable, and
1847 	 * the other benefit is that it works for devices other than SES
1848 	 * devices.  So you can send a SMP request to a da(4) device and it
1849 	 * will get routed to the expander that device is attached to.
1850 	 * (Assuming the da(4) device doesn't contain an SMP target...)
1851 	 */
1852 #if 0
1853 	if (sasaddr == 0)
1854 		sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
1855 #endif
1856 
1857 	/*
1858 	 * If we still don't have a SAS address for the expander, look for
1859 	 * the parent device of this device, which is probably the expander.
1860 	 */
1861 	if (sasaddr == 0) {
1862 		struct mpssas_target *parent_target;
1863 
1864 		if (targ->parent_handle == 0x0) {
1865 			mps_printf(sc, "%s: handle %d does not have a valid "
1866 				   "parent handle!\n", __func__, targ->handle);
1867 			ccb->ccb_h.status = CAM_REQ_INVALID;
1868 			goto bailout;
1869 		}
1870 		parent_target = mpssas_find_target(sassc, 0,
1871 						   targ->parent_handle);
1872 
1873 		if (parent_target == NULL) {
1874 			mps_printf(sc, "%s: handle %d does not have a valid "
1875 				   "parent target!\n", __func__, targ->handle);
1876 			ccb->ccb_h.status = CAM_REQ_INVALID;
1877 			goto bailout;
1878 		}
1879 
1880 		if ((parent_target->devinfo &
1881 		     MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
1882 			mps_printf(sc, "%s: handle %d parent %d does not "
1883 				   "have an SMP target!\n", __func__,
1884 				   targ->handle, parent_target->handle);
1885 			ccb->ccb_h.status = CAM_REQ_INVALID;
1886 			goto bailout;
1887 
1888 		}
1889 
1890 		sasaddr = parent_target->sasaddr;
1891 	}
1892 
1893 	if (sasaddr == 0) {
1894 		mps_printf(sc, "%s: unable to find SAS address for handle %d\n",
1895 			   __func__, targ->handle);
1896 		ccb->ccb_h.status = CAM_REQ_INVALID;
1897 		goto bailout;
1898 	}
1899 	mpssas_send_smpcmd(sassc, ccb, sasaddr);
1900 
1901 	return;
1902 
1903 bailout:
1904 	xpt_done(ccb);
1905 
1906 }
1907 
1908 #endif /* __FreeBSD_version >= 900026 */
1909 
1910 static void
1911 mpssas_action_resetdev(struct mpssas_softc *sassc, union ccb *ccb)
1912 {
1913 	struct mps_softc *sc;
1914 	struct mps_command *cm;
1915 	struct mpssas_target *targ;
1916 
1917 	sc = sassc->sc;
1918 	targ = &sassc->targets[ccb->ccb_h.target_id];
1919 
1920 	if (targ->flags & MPSSAS_TARGET_INRECOVERY) {
1921 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1922 		xpt_done(ccb);
1923 		return;
1924 	}
1925 
1926 	cm = mps_alloc_command(sc);
1927 	if (cm == NULL) {
1928 		mps_printf(sc, "%s: cannot alloc command\n", __func__);
1929 		ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1930 		xpt_done(ccb);
1931 		return;
1932 	}
1933 
1934 	cm->cm_targ = targ;
1935 	cm->cm_complete = mpssas_resetdev_complete;
1936 	cm->cm_complete_data = ccb;
1937 
1938 	mpssas_resetdev(sassc, cm);
1939 }
1940 
1941 static void
1942 mpssas_resetdev(struct mpssas_softc *sassc, struct mps_command *cm)
1943 {
1944 	MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1945 	struct mps_softc *sc;
1946 
1947 	mps_dprint(sassc->sc, MPS_TRACE, "%s\n", __func__);
1948 
1949 	sc = sassc->sc;
1950 
1951 	req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
1952 	req->DevHandle = cm->cm_targ->handle;
1953 	req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1954 	req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
1955 
1956 	/* SAS Hard Link Reset / SATA Link Reset */
1957 	req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
1958 
1959 	cm->cm_data = NULL;
1960 	cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1961 
1962 	mpssas_issue_tm_request(sc, cm);
1963 }
1964 
1965 static void
1966 mpssas_resetdev_complete(struct mps_softc *sc, struct mps_command *cm)
1967 {
1968 	MPI2_SCSI_TASK_MANAGE_REPLY *resp;
1969 	union ccb *ccb;
1970 
1971 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
1972 
1973 	resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)cm->cm_reply;
1974 	ccb = cm->cm_complete_data;
1975 
1976 	printf("resetdev complete IOCStatus= 0x%x ResponseCode= 0x%x\n",
1977 	    resp->IOCStatus, resp->ResponseCode);
1978 
1979 	if (resp->ResponseCode == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE)
1980 		ccb->ccb_h.status = CAM_REQ_CMP;
1981 	else
1982 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1983 
1984 	mpssas_complete_tm_request(sc, cm, /*free_cm*/ 1);
1985 
1986 	xpt_done(ccb);
1987 }
1988 
1989 static void
1990 mpssas_poll(struct cam_sim *sim)
1991 {
1992 	struct mpssas_softc *sassc;
1993 
1994 	sassc = cam_sim_softc(sim);
1995 	mps_intr_locked(sassc->sc);
1996 }
1997 
1998 static void
1999 mpssas_freeze_device(struct mpssas_softc *sassc, struct mpssas_target *targ)
2000 {
2001 }
2002 
2003 static void
2004 mpssas_unfreeze_device(struct mpssas_softc *sassc, struct mpssas_target *targ)
2005 {
2006 }
2007 
2008