]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/mps/mps_sas.c
MFC r255090 and r255128:
[FreeBSD/stable/9.git] / sys / dev / mps / mps_sas.c
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * Copyright (c) 2011, 2012 LSI Corp.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * LSI MPT-Fusion Host Adapter FreeBSD
28  *
29  * $FreeBSD$
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /* Communications core for LSI MPT2 */
36
37 /* TODO Move headers to mpsvar */
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/selinfo.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <sys/conf.h>
46 #include <sys/bio.h>
47 #include <sys/malloc.h>
48 #include <sys/uio.h>
49 #include <sys/sysctl.h>
50 #include <sys/endian.h>
51 #include <sys/queue.h>
52 #include <sys/kthread.h>
53 #include <sys/taskqueue.h>
54 #include <sys/sbuf.h>
55
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <sys/rman.h>
59
60 #include <machine/stdarg.h>
61
62 #include <cam/cam.h>
63 #include <cam/cam_ccb.h>
64 #include <cam/cam_xpt.h>
65 #include <cam/cam_debug.h>
66 #include <cam/cam_sim.h>
67 #include <cam/cam_xpt_sim.h>
68 #include <cam/cam_xpt_periph.h>
69 #include <cam/cam_periph.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_message.h>
72 #if __FreeBSD_version >= 900026
73 #include <cam/scsi/smp_all.h>
74 #endif
75
76 #include <dev/mps/mpi/mpi2_type.h>
77 #include <dev/mps/mpi/mpi2.h>
78 #include <dev/mps/mpi/mpi2_ioc.h>
79 #include <dev/mps/mpi/mpi2_sas.h>
80 #include <dev/mps/mpi/mpi2_cnfg.h>
81 #include <dev/mps/mpi/mpi2_init.h>
82 #include <dev/mps/mpi/mpi2_tool.h>
83 #include <dev/mps/mps_ioctl.h>
84 #include <dev/mps/mpsvar.h>
85 #include <dev/mps/mps_table.h>
86 #include <dev/mps/mps_sas.h>
87
88 #define MPSSAS_DISCOVERY_TIMEOUT        20
89 #define MPSSAS_MAX_DISCOVERY_TIMEOUTS   10 /* 200 seconds */
90
91 /*
92  * static array to check SCSI OpCode for EEDP protection bits
93  */
94 #define PRO_R MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
95 #define PRO_W MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
96 #define PRO_V MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
97 static uint8_t op_code_prot[256] = {
98         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100         0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
101         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102         0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106         0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
107         0, 0, 0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
108         0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
109         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
110         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
114 };
115
116 MALLOC_DEFINE(M_MPSSAS, "MPSSAS", "MPS SAS memory");
117
118 static void mpssas_discovery_timeout(void *data);
119 static void mpssas_remove_device(struct mps_softc *, struct mps_command *);
120 static void mpssas_remove_complete(struct mps_softc *, struct mps_command *);
121 static void mpssas_action(struct cam_sim *sim, union ccb *ccb);
122 static void mpssas_poll(struct cam_sim *sim);
123 static void mpssas_scsiio_timeout(void *data);
124 static void mpssas_abort_complete(struct mps_softc *sc, struct mps_command *cm);
125 static void mpssas_direct_drive_io(struct mpssas_softc *sassc,
126     struct mps_command *cm, union ccb *ccb);
127 static void mpssas_action_scsiio(struct mpssas_softc *, union ccb *);
128 static void mpssas_scsiio_complete(struct mps_softc *, struct mps_command *);
129 static void mpssas_action_resetdev(struct mpssas_softc *, union ccb *);
130 #if __FreeBSD_version >= 900026
131 static void mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm);
132 static void mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb,
133                                uint64_t sasaddr);
134 static void mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb);
135 #endif //FreeBSD_version >= 900026
136 static void mpssas_resetdev_complete(struct mps_softc *, struct mps_command *);
137 static int  mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm, struct mps_command *cm);
138 static int  mpssas_send_reset(struct mps_softc *sc, struct mps_command *tm, uint8_t type);
139 static void mpssas_async(void *callback_arg, uint32_t code,
140                          struct cam_path *path, void *arg);
141 #if (__FreeBSD_version < 901503) || \
142     ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006))
143 static void mpssas_check_eedp(struct mps_softc *sc, struct cam_path *path,
144                               struct ccb_getdev *cgd);
145 static void mpssas_read_cap_done(struct cam_periph *periph, union ccb *done_ccb);
146 #endif
147 static int mpssas_send_portenable(struct mps_softc *sc);
148 static void mpssas_portenable_complete(struct mps_softc *sc,
149     struct mps_command *cm);
150
151 struct mpssas_target *
152 mpssas_find_target_by_handle(struct mpssas_softc *sassc, int start, uint16_t handle)
153 {
154         struct mpssas_target *target;
155         int i;
156
157         for (i = start; i < sassc->sc->facts->MaxTargets; i++) {
158                 target = &sassc->targets[i];
159                 if (target->handle == handle)
160                         return (target);
161         }
162
163         return (NULL);
164 }
165
166 /* we need to freeze the simq during attach and diag reset, to avoid failing
167  * commands before device handles have been found by discovery.  Since
168  * discovery involves reading config pages and possibly sending commands,
169  * discovery actions may continue even after we receive the end of discovery
170  * event, so refcount discovery actions instead of assuming we can unfreeze
171  * the simq when we get the event.
172  */
173 void
174 mpssas_startup_increment(struct mpssas_softc *sassc)
175 {
176         MPS_FUNCTRACE(sassc->sc);
177
178         if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) {
179                 if (sassc->startup_refcount++ == 0) {
180                         /* just starting, freeze the simq */
181                         mps_dprint(sassc->sc, MPS_INIT,
182                             "%s freezing simq\n", __func__);
183                         xpt_freeze_simq(sassc->sim, 1);
184                 }
185                 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__,
186                     sassc->startup_refcount);
187         }
188 }
189
190 void
191 mpssas_startup_decrement(struct mpssas_softc *sassc)
192 {
193         MPS_FUNCTRACE(sassc->sc);
194
195         if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) {
196                 if (--sassc->startup_refcount == 0) {
197                         /* finished all discovery-related actions, release
198                          * the simq and rescan for the latest topology.
199                          */
200                         mps_dprint(sassc->sc, MPS_INIT,
201                             "%s releasing simq\n", __func__);
202                         sassc->flags &= ~MPSSAS_IN_STARTUP;
203 #if (__FreeBSD_version >= 1000039) || \
204     ((__FreeBSD_version < 1000000) && (__FreeBSD_version >= 902502))
205                         xpt_release_boot();
206 #else
207                         xpt_release_simq(sassc->sim, 1);
208                         mpssas_rescan_target(sassc->sc, NULL);
209 #endif
210                 }
211                 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__,
212                     sassc->startup_refcount);
213         }
214 }
215
216 /* LSI's firmware requires us to stop sending commands when we're doing task
217  * management, so refcount the TMs and keep the simq frozen when any are in
218  * use.
219  */
220 struct mps_command *
221 mpssas_alloc_tm(struct mps_softc *sc)
222 {
223         struct mps_command *tm;
224
225         MPS_FUNCTRACE(sc);
226         tm = mps_alloc_high_priority_command(sc);
227         if (tm != NULL) {
228                 if (sc->sassc->tm_count++ == 0) {
229                         mps_dprint(sc, MPS_RECOVERY,
230                             "%s freezing simq\n", __func__);
231                         xpt_freeze_simq(sc->sassc->sim, 1);
232                 }
233                 mps_dprint(sc, MPS_RECOVERY, "%s tm_count %u\n", __func__,
234                     sc->sassc->tm_count);
235         }
236         return tm;
237 }
238
239 void
240 mpssas_free_tm(struct mps_softc *sc, struct mps_command *tm)
241 {
242         mps_dprint(sc, MPS_TRACE, "%s", __func__);
243         if (tm == NULL)
244                 return;
245
246         /* if there are no TMs in use, we can release the simq.  We use our
247          * own refcount so that it's easier for a diag reset to cleanup and
248          * release the simq.
249          */
250         if (--sc->sassc->tm_count == 0) {
251                 mps_dprint(sc, MPS_RECOVERY, "%s releasing simq\n", __func__);
252                 xpt_release_simq(sc->sassc->sim, 1);
253         }
254         mps_dprint(sc, MPS_RECOVERY, "%s tm_count %u\n", __func__,
255             sc->sassc->tm_count);
256
257         mps_free_high_priority_command(sc, tm);
258 }
259
260 void
261 mpssas_rescan_target(struct mps_softc *sc, struct mpssas_target *targ)
262 {
263         struct mpssas_softc *sassc = sc->sassc;
264         path_id_t pathid;
265         target_id_t targetid;
266         union ccb *ccb;
267
268         MPS_FUNCTRACE(sc);
269         pathid = cam_sim_path(sassc->sim);
270         if (targ == NULL)
271                 targetid = CAM_TARGET_WILDCARD;
272         else
273                 targetid = targ - sassc->targets;
274
275         /*
276          * Allocate a CCB and schedule a rescan.
277          */
278         ccb = xpt_alloc_ccb_nowait();
279         if (ccb == NULL) {
280                 mps_dprint(sc, MPS_ERROR, "unable to alloc CCB for rescan\n");
281                 return;
282         }
283
284         if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
285             targetid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
286                 mps_dprint(sc, MPS_ERROR, "unable to create path for rescan\n");
287                 xpt_free_ccb(ccb);
288                 return;
289         }
290
291         if (targetid == CAM_TARGET_WILDCARD)
292                 ccb->ccb_h.func_code = XPT_SCAN_BUS;
293         else
294                 ccb->ccb_h.func_code = XPT_SCAN_TGT;     
295
296         mps_dprint(sc, MPS_TRACE, "%s targetid %u\n", __func__, targetid);
297         xpt_rescan(ccb);
298 }
299
300 static void
301 mpssas_log_command(struct mps_command *cm, u_int level, const char *fmt, ...)
302 {
303         struct sbuf sb;
304         va_list ap;
305         char str[192];
306         char path_str[64];
307
308         if (cm == NULL)
309                 return;
310
311         sbuf_new(&sb, str, sizeof(str), 0);
312
313         va_start(ap, fmt);
314
315         if (cm->cm_ccb != NULL) {
316                 xpt_path_string(cm->cm_ccb->csio.ccb_h.path, path_str,
317                                 sizeof(path_str));
318                 sbuf_cat(&sb, path_str);
319                 if (cm->cm_ccb->ccb_h.func_code == XPT_SCSI_IO) {
320                         scsi_command_string(&cm->cm_ccb->csio, &sb);
321                         sbuf_printf(&sb, "length %d ",
322                                     cm->cm_ccb->csio.dxfer_len);
323                 }
324         }
325         else {
326                 sbuf_printf(&sb, "(noperiph:%s%d:%u:%u:%u): ",
327                     cam_sim_name(cm->cm_sc->sassc->sim),
328                     cam_sim_unit(cm->cm_sc->sassc->sim),
329                     cam_sim_bus(cm->cm_sc->sassc->sim),
330                     cm->cm_targ ? cm->cm_targ->tid : 0xFFFFFFFF,
331                     cm->cm_lun);
332         }
333
334         sbuf_printf(&sb, "SMID %u ", cm->cm_desc.Default.SMID);
335         sbuf_vprintf(&sb, fmt, ap);
336         sbuf_finish(&sb);
337         mps_dprint_field(cm->cm_sc, level, "%s", sbuf_data(&sb));
338
339         va_end(ap);
340 }
341
342
343 static void
344 mpssas_remove_volume(struct mps_softc *sc, struct mps_command *tm)
345 {
346         MPI2_SCSI_TASK_MANAGE_REPLY *reply;
347         struct mpssas_target *targ;
348         uint16_t handle;
349
350         MPS_FUNCTRACE(sc);
351
352         reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
353         handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
354         targ = tm->cm_targ;
355
356         if (reply == NULL) {
357                 /* XXX retry the remove after the diag reset completes? */
358                 mps_dprint(sc, MPS_FAULT,
359                     "%s NULL reply reseting device 0x%04x\n", __func__, handle);
360                 mpssas_free_tm(sc, tm);
361                 return;
362         }
363
364         if (reply->IOCStatus != MPI2_IOCSTATUS_SUCCESS) {
365                 mps_dprint(sc, MPS_FAULT,
366                    "IOCStatus = 0x%x while resetting device 0x%x\n",
367                    reply->IOCStatus, handle);
368                 mpssas_free_tm(sc, tm);
369                 return;
370         }
371
372         mps_dprint(sc, MPS_XINFO,
373             "Reset aborted %u commands\n", reply->TerminationCount);
374         mps_free_reply(sc, tm->cm_reply_data);
375         tm->cm_reply = NULL;    /* Ensures the reply won't get re-freed */
376
377         mps_dprint(sc, MPS_XINFO,
378             "clearing target %u handle 0x%04x\n", targ->tid, handle);
379         
380         /*
381          * Don't clear target if remove fails because things will get confusing.
382          * Leave the devname and sasaddr intact so that we know to avoid reusing
383          * this target id if possible, and so we can assign the same target id
384          * to this device if it comes back in the future.
385          */
386         if (reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) {
387                 targ = tm->cm_targ;
388                 targ->handle = 0x0;
389                 targ->encl_handle = 0x0;
390                 targ->encl_slot = 0x0;
391                 targ->exp_dev_handle = 0x0;
392                 targ->phy_num = 0x0;
393                 targ->linkrate = 0x0;
394                 targ->devinfo = 0x0;
395                 targ->flags = 0x0;
396         }
397
398         mpssas_free_tm(sc, tm);
399 }
400
401
402 /*
403  * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal.
404  * Otherwise Volume Delete is same as Bare Drive Removal.
405  */
406 void
407 mpssas_prepare_volume_remove(struct mpssas_softc *sassc, uint16_t handle)
408 {
409         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
410         struct mps_softc *sc;
411         struct mps_command *cm;
412         struct mpssas_target *targ = NULL;
413
414         MPS_FUNCTRACE(sassc->sc);
415         sc = sassc->sc;
416
417 #ifdef WD_SUPPORT
418         /*
419          * If this is a WD controller, determine if the disk should be exposed
420          * to the OS or not.  If disk should be exposed, return from this
421          * function without doing anything.
422          */
423         if (sc->WD_available && (sc->WD_hide_expose ==
424             MPS_WD_EXPOSE_ALWAYS)) {
425                 return;
426         }
427 #endif //WD_SUPPORT
428
429         targ = mpssas_find_target_by_handle(sassc, 0, handle);
430         if (targ == NULL) {
431                 /* FIXME: what is the action? */
432                 /* We don't know about this device? */
433                 mps_dprint(sc, MPS_ERROR,
434                    "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle);
435                 return;
436         }
437
438         targ->flags |= MPSSAS_TARGET_INREMOVAL;
439
440         cm = mpssas_alloc_tm(sc);
441         if (cm == NULL) {
442                 mps_dprint(sc, MPS_ERROR,
443                     "%s: command alloc failure\n", __func__);
444                 return;
445         }
446
447         mpssas_rescan_target(sc, targ);
448
449         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
450         req->DevHandle = targ->handle;
451         req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
452         req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
453
454         /* SAS Hard Link Reset / SATA Link Reset */
455         req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
456
457         cm->cm_targ = targ;
458         cm->cm_data = NULL;
459         cm->cm_desc.HighPriority.RequestFlags =
460             MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
461         cm->cm_complete = mpssas_remove_volume;
462         cm->cm_complete_data = (void *)(uintptr_t)handle;
463         mps_map_command(sc, cm);
464 }
465
466 /*
467  * The MPT2 firmware performs debounce on the link to avoid transient link
468  * errors and false removals.  When it does decide that link has been lost
469  * and a device need to go away, it expects that the host will perform a
470  * target reset and then an op remove.  The reset has the side-effect of
471  * aborting any outstanding requests for the device, which is required for
472  * the op-remove to succeed.  It's not clear if the host should check for
473  * the device coming back alive after the reset.
474  */
475 void
476 mpssas_prepare_remove(struct mpssas_softc *sassc, uint16_t handle)
477 {
478         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
479         struct mps_softc *sc;
480         struct mps_command *cm;
481         struct mpssas_target *targ = NULL;
482
483         MPS_FUNCTRACE(sassc->sc);
484
485         sc = sassc->sc;
486
487         targ = mpssas_find_target_by_handle(sassc, 0, handle);
488         if (targ == NULL) {
489                 /* FIXME: what is the action? */
490                 /* We don't know about this device? */
491                 mps_dprint(sc, MPS_ERROR,
492                     "%s : invalid handle 0x%x \n", __func__, handle);
493                 return;
494         }
495
496         targ->flags |= MPSSAS_TARGET_INREMOVAL;
497
498         cm = mpssas_alloc_tm(sc);
499         if (cm == NULL) {
500                 mps_dprint(sc, MPS_ERROR,
501                     "%s: command alloc failure\n", __func__);
502                 return;
503         }
504
505         mpssas_rescan_target(sc, targ);
506
507         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
508         memset(req, 0, sizeof(*req));
509         req->DevHandle = htole16(targ->handle);
510         req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
511         req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
512
513         /* SAS Hard Link Reset / SATA Link Reset */
514         req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
515
516         cm->cm_targ = targ;
517         cm->cm_data = NULL;
518         cm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
519         cm->cm_complete = mpssas_remove_device;
520         cm->cm_complete_data = (void *)(uintptr_t)handle;
521         mps_map_command(sc, cm);
522 }
523
524 static void
525 mpssas_remove_device(struct mps_softc *sc, struct mps_command *tm)
526 {
527         MPI2_SCSI_TASK_MANAGE_REPLY *reply;
528         MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
529         struct mpssas_target *targ;
530         struct mps_command *next_cm;
531         uint16_t handle;
532
533         MPS_FUNCTRACE(sc);
534
535         reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
536         handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
537         targ = tm->cm_targ;
538
539         /*
540          * Currently there should be no way we can hit this case.  It only
541          * happens when we have a failure to allocate chain frames, and
542          * task management commands don't have S/G lists.
543          */
544         if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
545                 mps_dprint(sc, MPS_ERROR,
546                     "%s: cm_flags = %#x for remove of handle %#04x! "
547                     "This should not happen!\n", __func__, tm->cm_flags,
548                     handle);
549                 mpssas_free_tm(sc, tm);
550                 return;
551         }
552
553         if (reply == NULL) {
554                 /* XXX retry the remove after the diag reset completes? */
555                 mps_dprint(sc, MPS_FAULT,
556                     "%s NULL reply reseting device 0x%04x\n", __func__, handle);
557                 mpssas_free_tm(sc, tm);
558                 return;
559         }
560
561         if (le16toh(reply->IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
562                 mps_dprint(sc, MPS_FAULT,
563                    "IOCStatus = 0x%x while resetting device 0x%x\n",
564                    le16toh(reply->IOCStatus), handle);
565                 mpssas_free_tm(sc, tm);
566                 return;
567         }
568
569         mps_dprint(sc, MPS_XINFO, "Reset aborted %u commands\n",
570             le32toh(reply->TerminationCount));
571         mps_free_reply(sc, tm->cm_reply_data);
572         tm->cm_reply = NULL;    /* Ensures the reply won't get re-freed */
573
574         /* Reuse the existing command */
575         req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req;
576         memset(req, 0, sizeof(*req));
577         req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
578         req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
579         req->DevHandle = htole16(handle);
580         tm->cm_data = NULL;
581         tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
582         tm->cm_complete = mpssas_remove_complete;
583         tm->cm_complete_data = (void *)(uintptr_t)handle;
584
585         mps_map_command(sc, tm);
586
587         mps_dprint(sc, MPS_XINFO, "clearing target %u handle 0x%04x\n",
588                    targ->tid, handle);
589         TAILQ_FOREACH_SAFE(tm, &targ->commands, cm_link, next_cm) {
590                 union ccb *ccb;
591
592                 mps_dprint(sc, MPS_XINFO, "Completing missed command %p\n", tm);
593                 ccb = tm->cm_complete_data;
594                 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
595                 mpssas_scsiio_complete(sc, tm);
596         }
597 }
598
599 static void
600 mpssas_remove_complete(struct mps_softc *sc, struct mps_command *tm)
601 {
602         MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
603         uint16_t handle;
604         struct mpssas_target *targ;
605         struct mpssas_lun *lun;
606
607         MPS_FUNCTRACE(sc);
608
609         reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply;
610         handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
611
612         /*
613          * Currently there should be no way we can hit this case.  It only
614          * happens when we have a failure to allocate chain frames, and
615          * task management commands don't have S/G lists.
616          */
617         if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
618                 mps_dprint(sc, MPS_XINFO,
619                            "%s: cm_flags = %#x for remove of handle %#04x! "
620                            "This should not happen!\n", __func__, tm->cm_flags,
621                            handle);
622                 mpssas_free_tm(sc, tm);
623                 return;
624         }
625
626         if (reply == NULL) {
627                 /* most likely a chip reset */
628                 mps_dprint(sc, MPS_FAULT,
629                     "%s NULL reply removing device 0x%04x\n", __func__, handle);
630                 mpssas_free_tm(sc, tm);
631                 return;
632         }
633
634         mps_dprint(sc, MPS_XINFO,
635             "%s on handle 0x%04x, IOCStatus= 0x%x\n", __func__, 
636             handle, le16toh(reply->IOCStatus));
637
638         /*
639          * Don't clear target if remove fails because things will get confusing.
640          * Leave the devname and sasaddr intact so that we know to avoid reusing
641          * this target id if possible, and so we can assign the same target id
642          * to this device if it comes back in the future.
643          */
644         if (le16toh(reply->IOCStatus) == MPI2_IOCSTATUS_SUCCESS) {
645                 targ = tm->cm_targ;
646                 targ->handle = 0x0;
647                 targ->encl_handle = 0x0;
648                 targ->encl_slot = 0x0;
649                 targ->exp_dev_handle = 0x0;
650                 targ->phy_num = 0x0;
651                 targ->linkrate = 0x0;
652                 targ->devinfo = 0x0;
653                 targ->flags = 0x0;
654                 
655                 while(!SLIST_EMPTY(&targ->luns)) {
656                         lun = SLIST_FIRST(&targ->luns);
657                         SLIST_REMOVE_HEAD(&targ->luns, lun_link);
658                         free(lun, M_MPT2);
659                 }
660         }
661         
662
663         mpssas_free_tm(sc, tm);
664 }
665
666 static int
667 mpssas_register_events(struct mps_softc *sc)
668 {
669         u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
670
671         bzero(events, 16);
672         setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
673         setbit(events, MPI2_EVENT_SAS_DISCOVERY);
674         setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
675         setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
676         setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
677         setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
678         setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
679         setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
680         setbit(events, MPI2_EVENT_IR_VOLUME);
681         setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK);
682         setbit(events, MPI2_EVENT_IR_OPERATION_STATUS);
683         setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
684
685         mps_register_events(sc, events, mpssas_evt_handler, NULL,
686             &sc->sassc->mpssas_eh);
687
688         return (0);
689 }
690
691 int
692 mps_attach_sas(struct mps_softc *sc)
693 {
694         struct mpssas_softc *sassc;
695         cam_status status;
696         int unit, error = 0;
697
698         MPS_FUNCTRACE(sc);
699
700         sassc = malloc(sizeof(struct mpssas_softc), M_MPT2, M_WAITOK|M_ZERO);
701         if(!sassc) {
702                 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
703                 __func__, __LINE__);
704                 return (ENOMEM);
705         }
706         sassc->targets = malloc(sizeof(struct mpssas_target) *
707             sc->facts->MaxTargets, M_MPT2, M_WAITOK|M_ZERO);
708         if(!sassc->targets) {
709                 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
710                 __func__, __LINE__);
711                 free(sassc, M_MPT2);
712                 return (ENOMEM);
713         }
714         sc->sassc = sassc;
715         sassc->sc = sc;
716
717         if ((sassc->devq = cam_simq_alloc(sc->num_reqs)) == NULL) {
718                 mps_dprint(sc, MPS_ERROR, "Cannot allocate SIMQ\n");
719                 error = ENOMEM;
720                 goto out;
721         }
722
723         unit = device_get_unit(sc->mps_dev);
724         sassc->sim = cam_sim_alloc(mpssas_action, mpssas_poll, "mps", sassc,
725             unit, &sc->mps_mtx, sc->num_reqs, sc->num_reqs, sassc->devq);
726         if (sassc->sim == NULL) {
727                 mps_dprint(sc, MPS_ERROR, "Cannot allocate SIM\n");
728                 error = EINVAL;
729                 goto out;
730         }
731
732         TAILQ_INIT(&sassc->ev_queue);
733
734         /* Initialize taskqueue for Event Handling */
735         TASK_INIT(&sassc->ev_task, 0, mpssas_firmware_event_work, sc);
736         sassc->ev_tq = taskqueue_create("mps_taskq", M_NOWAIT | M_ZERO,
737             taskqueue_thread_enqueue, &sassc->ev_tq);
738
739         /* Run the task queue with lowest priority */
740         taskqueue_start_threads(&sassc->ev_tq, 1, 255, "%s taskq", 
741             device_get_nameunit(sc->mps_dev));
742
743         mps_lock(sc);
744
745         /*
746          * XXX There should be a bus for every port on the adapter, but since
747          * we're just going to fake the topology for now, we'll pretend that
748          * everything is just a target on a single bus.
749          */
750         if ((error = xpt_bus_register(sassc->sim, sc->mps_dev, 0)) != 0) {
751                 mps_dprint(sc, MPS_ERROR, "Error %d registering SCSI bus\n",
752                     error);
753                 mps_unlock(sc);
754                 goto out;
755         }
756
757         /*
758          * Assume that discovery events will start right away.
759          *
760          * Hold off boot until discovery is complete.
761          */
762         sassc->flags |= MPSSAS_IN_STARTUP | MPSSAS_IN_DISCOVERY;
763 #if (__FreeBSD_version >= 1000039) || \
764     ((__FreeBSD_version < 1000000) && (__FreeBSD_version >= 902502))
765         xpt_hold_boot();
766 #else
767         xpt_freeze_simq(sassc->sim, 1);
768 #endif
769         sc->sassc->startup_refcount = 0;
770
771         callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
772         sassc->discovery_timeouts = 0;
773
774         sassc->tm_count = 0;
775
776         /*
777          * Register for async events so we can determine the EEDP
778          * capabilities of devices.
779          */
780         status = xpt_create_path(&sassc->path, /*periph*/NULL,
781             cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD,
782             CAM_LUN_WILDCARD);
783         if (status != CAM_REQ_CMP) {
784                 mps_printf(sc, "Error %#x creating sim path\n", status);
785                 sassc->path = NULL;
786         } else {
787                 int event;
788
789 #if (__FreeBSD_version >= 1000006) || \
790     ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000))
791                 event = AC_ADVINFO_CHANGED;
792 #else
793                 event = AC_FOUND_DEVICE;
794 #endif
795                 status = xpt_register_async(event, mpssas_async, sc,
796                                             sassc->path);
797                 if (status != CAM_REQ_CMP) {
798                         mps_dprint(sc, MPS_ERROR,
799                             "Error %#x registering async handler for "
800                             "AC_ADVINFO_CHANGED events\n", status);
801                         xpt_free_path(sassc->path);
802                         sassc->path = NULL;
803                 }
804         }
805         if (status != CAM_REQ_CMP) {
806                 /*
807                  * EEDP use is the exception, not the rule.
808                  * Warn the user, but do not fail to attach.
809                  */
810                 mps_printf(sc, "EEDP capabilities disabled.\n");
811         }
812
813         mps_unlock(sc);
814
815         mpssas_register_events(sc);
816 out:
817         if (error)
818                 mps_detach_sas(sc);
819         return (error);
820 }
821
822 int
823 mps_detach_sas(struct mps_softc *sc)
824 {
825         struct mpssas_softc *sassc;
826         struct mpssas_lun *lun, *lun_tmp;
827         struct mpssas_target *targ;
828         int i;
829
830         MPS_FUNCTRACE(sc);
831
832         if (sc->sassc == NULL)
833                 return (0);
834
835         sassc = sc->sassc;
836         mps_deregister_events(sc, sassc->mpssas_eh);
837
838         /*
839          * Drain and free the event handling taskqueue with the lock
840          * unheld so that any parallel processing tasks drain properly
841          * without deadlocking.
842          */
843         if (sassc->ev_tq != NULL)
844                 taskqueue_free(sassc->ev_tq);
845
846         /* Make sure CAM doesn't wedge if we had to bail out early. */
847         mps_lock(sc);
848
849         /* Deregister our async handler */
850         if (sassc->path != NULL) {
851                 xpt_register_async(0, mpssas_async, sc, sassc->path);
852                 xpt_free_path(sassc->path);
853                 sassc->path = NULL;
854         }
855
856         if (sassc->flags & MPSSAS_IN_STARTUP)
857                 xpt_release_simq(sassc->sim, 1);
858
859         if (sassc->sim != NULL) {
860                 xpt_bus_deregister(cam_sim_path(sassc->sim));
861                 cam_sim_free(sassc->sim, FALSE);
862         }
863
864         sassc->flags |= MPSSAS_SHUTDOWN;
865         mps_unlock(sc);
866
867         if (sassc->devq != NULL)
868                 cam_simq_free(sassc->devq);
869
870         for(i=0; i< sc->facts->MaxTargets ;i++) {
871                 targ = &sassc->targets[i];
872                 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
873                         free(lun, M_MPT2);
874                 }
875         }
876         free(sassc->targets, M_MPT2);
877         free(sassc, M_MPT2);
878         sc->sassc = NULL;
879
880         return (0);
881 }
882
883 void
884 mpssas_discovery_end(struct mpssas_softc *sassc)
885 {
886         struct mps_softc *sc = sassc->sc;
887
888         MPS_FUNCTRACE(sc);
889
890         if (sassc->flags & MPSSAS_DISCOVERY_TIMEOUT_PENDING)
891                 callout_stop(&sassc->discovery_callout);
892
893 }
894
895 static void
896 mpssas_discovery_timeout(void *data)
897 {
898         struct mpssas_softc *sassc = data;
899         struct mps_softc *sc;
900
901         sc = sassc->sc;
902         MPS_FUNCTRACE(sc);
903
904         mps_lock(sc);
905         mps_dprint(sc, MPS_INFO,
906             "Timeout waiting for discovery, interrupts may not be working!\n");
907         sassc->flags &= ~MPSSAS_DISCOVERY_TIMEOUT_PENDING;
908
909         /* Poll the hardware for events in case interrupts aren't working */
910         mps_intr_locked(sc);
911
912         mps_dprint(sassc->sc, MPS_INFO,
913             "Finished polling after discovery timeout at %d\n", ticks);
914
915         if ((sassc->flags & MPSSAS_IN_DISCOVERY) == 0) {
916                 mpssas_discovery_end(sassc);
917         } else {
918                 if (sassc->discovery_timeouts < MPSSAS_MAX_DISCOVERY_TIMEOUTS) {
919                         sassc->flags |= MPSSAS_DISCOVERY_TIMEOUT_PENDING;
920                         callout_reset(&sassc->discovery_callout,
921                             MPSSAS_DISCOVERY_TIMEOUT * hz,
922                             mpssas_discovery_timeout, sassc);
923                         sassc->discovery_timeouts++;
924                 } else {
925                         mps_dprint(sassc->sc, MPS_FAULT,
926                             "Discovery timed out, continuing.\n");
927                         sassc->flags &= ~MPSSAS_IN_DISCOVERY;
928                         mpssas_discovery_end(sassc);
929                 }
930         }
931
932         mps_unlock(sc);
933 }
934
935 static void
936 mpssas_action(struct cam_sim *sim, union ccb *ccb)
937 {
938         struct mpssas_softc *sassc;
939
940         sassc = cam_sim_softc(sim);
941
942         MPS_FUNCTRACE(sassc->sc);
943         mps_dprint(sassc->sc, MPS_TRACE, "ccb func_code 0x%x\n",
944             ccb->ccb_h.func_code);
945         mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
946
947         switch (ccb->ccb_h.func_code) {
948         case XPT_PATH_INQ:
949         {
950                 struct ccb_pathinq *cpi = &ccb->cpi;
951
952                 cpi->version_num = 1;
953                 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
954                 cpi->target_sprt = 0;
955 #if (__FreeBSD_version >= 1000039) || \
956     ((__FreeBSD_version < 1000000) && (__FreeBSD_version >= 902502))
957                 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN;
958 #else
959                 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
960 #endif
961                 cpi->hba_eng_cnt = 0;
962                 cpi->max_target = sassc->sc->facts->MaxTargets - 1;
963                 cpi->max_lun = 255;
964                 cpi->initiator_id = 255;
965                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
966                 strncpy(cpi->hba_vid, "LSILogic", HBA_IDLEN);
967                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
968                 cpi->unit_number = cam_sim_unit(sim);
969                 cpi->bus_id = cam_sim_bus(sim);
970                 cpi->base_transfer_speed = 150000;
971                 cpi->transport = XPORT_SAS;
972                 cpi->transport_version = 0;
973                 cpi->protocol = PROTO_SCSI;
974                 cpi->protocol_version = SCSI_REV_SPC;
975 #if __FreeBSD_version >= 800001
976                 /*
977                  * XXX KDM where does this number come from?
978                  */
979                 cpi->maxio = 256 * 1024;
980 #endif
981                 cpi->ccb_h.status = CAM_REQ_CMP;
982                 break;
983         }
984         case XPT_GET_TRAN_SETTINGS:
985         {
986                 struct ccb_trans_settings       *cts;
987                 struct ccb_trans_settings_sas   *sas;
988                 struct ccb_trans_settings_scsi  *scsi;
989                 struct mpssas_target *targ;
990
991                 cts = &ccb->cts;
992                 sas = &cts->xport_specific.sas;
993                 scsi = &cts->proto_specific.scsi;
994
995                 targ = &sassc->targets[cts->ccb_h.target_id];
996                 if (targ->handle == 0x0) {
997                         cts->ccb_h.status = CAM_SEL_TIMEOUT;
998                         break;
999                 }
1000
1001                 cts->protocol_version = SCSI_REV_SPC2;
1002                 cts->transport = XPORT_SAS;
1003                 cts->transport_version = 0;
1004
1005                 sas->valid = CTS_SAS_VALID_SPEED;
1006                 switch (targ->linkrate) {
1007                 case 0x08:
1008                         sas->bitrate = 150000;
1009                         break;
1010                 case 0x09:
1011                         sas->bitrate = 300000;
1012                         break;
1013                 case 0x0a:
1014                         sas->bitrate = 600000;
1015                         break;
1016                 default:
1017                         sas->valid = 0;
1018                 }
1019
1020                 cts->protocol = PROTO_SCSI;
1021                 scsi->valid = CTS_SCSI_VALID_TQ;
1022                 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1023
1024                 cts->ccb_h.status = CAM_REQ_CMP;
1025                 break;
1026         }
1027         case XPT_CALC_GEOMETRY:
1028                 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1029                 ccb->ccb_h.status = CAM_REQ_CMP;
1030                 break;
1031         case XPT_RESET_DEV:
1032                 mps_dprint(sassc->sc, MPS_XINFO, "mpssas_action XPT_RESET_DEV\n");
1033                 mpssas_action_resetdev(sassc, ccb);
1034                 return;
1035         case XPT_RESET_BUS:
1036         case XPT_ABORT:
1037         case XPT_TERM_IO:
1038                 mps_dprint(sassc->sc, MPS_XINFO,
1039                     "mpssas_action faking success for abort or reset\n");
1040                 ccb->ccb_h.status = CAM_REQ_CMP;
1041                 break;
1042         case XPT_SCSI_IO:
1043                 mpssas_action_scsiio(sassc, ccb);
1044                 return;
1045 #if __FreeBSD_version >= 900026
1046         case XPT_SMP_IO:
1047                 mpssas_action_smpio(sassc, ccb);
1048                 return;
1049 #endif
1050         default:
1051                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1052                 break;
1053         }
1054         xpt_done(ccb);
1055
1056 }
1057
1058 static void
1059 mpssas_announce_reset(struct mps_softc *sc, uint32_t ac_code,
1060     target_id_t target_id, lun_id_t lun_id)
1061 {
1062         path_id_t path_id = cam_sim_path(sc->sassc->sim);
1063         struct cam_path *path;
1064
1065         mps_dprint(sc, MPS_XINFO, "%s code %x target %d lun %d\n", __func__,
1066             ac_code, target_id, lun_id);
1067
1068         if (xpt_create_path(&path, NULL, 
1069                 path_id, target_id, lun_id) != CAM_REQ_CMP) {
1070                 mps_dprint(sc, MPS_ERROR, "unable to create path for reset "
1071                            "notification\n");
1072                 return;
1073         }
1074
1075         xpt_async(ac_code, path, NULL);
1076         xpt_free_path(path);
1077 }
1078
1079 static void 
1080 mpssas_complete_all_commands(struct mps_softc *sc)
1081 {
1082         struct mps_command *cm;
1083         int i;
1084         int completed;
1085
1086         MPS_FUNCTRACE(sc);
1087         mtx_assert(&sc->mps_mtx, MA_OWNED);
1088
1089         /* complete all commands with a NULL reply */
1090         for (i = 1; i < sc->num_reqs; i++) {
1091                 cm = &sc->commands[i];
1092                 cm->cm_reply = NULL;
1093                 completed = 0;
1094
1095                 if (cm->cm_flags & MPS_CM_FLAGS_POLLED)
1096                         cm->cm_flags |= MPS_CM_FLAGS_COMPLETE;
1097
1098                 if (cm->cm_complete != NULL) {
1099                         mpssas_log_command(cm, MPS_RECOVERY,
1100                             "completing cm %p state %x ccb %p for diag reset\n", 
1101                             cm, cm->cm_state, cm->cm_ccb);
1102
1103                         cm->cm_complete(sc, cm);
1104                         completed = 1;
1105                 }
1106
1107                 if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) {
1108                         mpssas_log_command(cm, MPS_RECOVERY,
1109                             "waking up cm %p state %x ccb %p for diag reset\n", 
1110                             cm, cm->cm_state, cm->cm_ccb);
1111                         wakeup(cm);
1112                         completed = 1;
1113                 }
1114                 
1115                 if ((completed == 0) && (cm->cm_state != MPS_CM_STATE_FREE)) {
1116                         /* this should never happen, but if it does, log */
1117                         mpssas_log_command(cm, MPS_RECOVERY,
1118                             "cm %p state %x flags 0x%x ccb %p during diag "
1119                             "reset\n", cm, cm->cm_state, cm->cm_flags,
1120                             cm->cm_ccb);
1121                 }
1122         }
1123 }
1124
1125 void
1126 mpssas_handle_reinit(struct mps_softc *sc)
1127 {
1128         int i;
1129
1130         /* Go back into startup mode and freeze the simq, so that CAM
1131          * doesn't send any commands until after we've rediscovered all
1132          * targets and found the proper device handles for them.
1133          *
1134          * After the reset, portenable will trigger discovery, and after all
1135          * discovery-related activities have finished, the simq will be
1136          * released.
1137          */
1138         mps_dprint(sc, MPS_INIT, "%s startup\n", __func__);
1139         sc->sassc->flags |= MPSSAS_IN_STARTUP;
1140         sc->sassc->flags |= MPSSAS_IN_DISCOVERY;
1141         xpt_freeze_simq(sc->sassc->sim, 1);
1142
1143         /* notify CAM of a bus reset */
1144         mpssas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD, 
1145             CAM_LUN_WILDCARD);
1146
1147         /* complete and cleanup after all outstanding commands */
1148         mpssas_complete_all_commands(sc);
1149
1150         mps_dprint(sc, MPS_INIT,
1151             "%s startup %u tm %u after command completion\n",
1152             __func__, sc->sassc->startup_refcount, sc->sassc->tm_count);
1153
1154         /*
1155          * The simq was explicitly frozen above, so set the refcount to 0.
1156          * The simq will be explicitly released after port enable completes.
1157          */
1158         sc->sassc->startup_refcount = 0;
1159
1160         /* zero all the target handles, since they may change after the
1161          * reset, and we have to rediscover all the targets and use the new
1162          * handles.  
1163          */
1164         for (i = 0; i < sc->facts->MaxTargets; i++) {
1165                 if (sc->sassc->targets[i].outstanding != 0)
1166                         mps_dprint(sc, MPS_INIT, "target %u outstanding %u\n", 
1167                             i, sc->sassc->targets[i].outstanding);
1168                 sc->sassc->targets[i].handle = 0x0;
1169                 sc->sassc->targets[i].exp_dev_handle = 0x0;
1170                 sc->sassc->targets[i].outstanding = 0;
1171                 sc->sassc->targets[i].flags = MPSSAS_TARGET_INDIAGRESET;
1172         }
1173 }
1174
1175 static void
1176 mpssas_tm_timeout(void *data)
1177 {
1178         struct mps_command *tm = data;
1179         struct mps_softc *sc = tm->cm_sc;
1180
1181         mtx_assert(&sc->mps_mtx, MA_OWNED);
1182
1183         mpssas_log_command(tm, MPS_INFO|MPS_RECOVERY,
1184             "task mgmt %p timed out\n", tm);
1185         mps_reinit(sc);
1186 }
1187
1188 static void
1189 mpssas_logical_unit_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1190 {
1191         MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1192         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1193         unsigned int cm_count = 0;
1194         struct mps_command *cm;
1195         struct mpssas_target *targ;
1196
1197         callout_stop(&tm->cm_callout);
1198
1199         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1200         reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1201         targ = tm->cm_targ;
1202
1203         /*
1204          * Currently there should be no way we can hit this case.  It only
1205          * happens when we have a failure to allocate chain frames, and
1206          * task management commands don't have S/G lists.
1207          * XXXSL So should it be an assertion?
1208          */
1209         if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1210                 mps_dprint(sc, MPS_ERROR, "%s: cm_flags = %#x for LUN reset! "
1211                            "This should not happen!\n", __func__, tm->cm_flags);
1212                 mpssas_free_tm(sc, tm);
1213                 return;
1214         }
1215
1216         if (reply == NULL) {
1217                 mpssas_log_command(tm, MPS_RECOVERY,
1218                     "NULL reset reply for tm %p\n", tm);
1219                 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1220                         /* this completion was due to a reset, just cleanup */
1221                         targ->flags &= ~MPSSAS_TARGET_INRESET;
1222                         targ->tm = NULL;
1223                         mpssas_free_tm(sc, tm);
1224                 }
1225                 else {
1226                         /* we should have gotten a reply. */
1227                         mps_reinit(sc);
1228                 }
1229                 return;
1230         }
1231
1232         mpssas_log_command(tm, MPS_RECOVERY,
1233             "logical unit reset status 0x%x code 0x%x count %u\n",
1234             le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1235             le32toh(reply->TerminationCount));
1236                 
1237         /* See if there are any outstanding commands for this LUN.
1238          * This could be made more efficient by using a per-LU data
1239          * structure of some sort.
1240          */
1241         TAILQ_FOREACH(cm, &targ->commands, cm_link) {
1242                 if (cm->cm_lun == tm->cm_lun)
1243                         cm_count++;
1244         }
1245
1246         if (cm_count == 0) {
1247                 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1248                     "logical unit %u finished recovery after reset\n",
1249                     tm->cm_lun, tm);
1250
1251                 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid, 
1252                     tm->cm_lun);
1253
1254                 /* we've finished recovery for this logical unit.  check and
1255                  * see if some other logical unit has a timedout command
1256                  * that needs to be processed.
1257                  */
1258                 cm = TAILQ_FIRST(&targ->timedout_commands);
1259                 if (cm) {
1260                         mpssas_send_abort(sc, tm, cm);
1261                 }
1262                 else {
1263                         targ->tm = NULL;
1264                         mpssas_free_tm(sc, tm);
1265                 }
1266         }
1267         else {
1268                 /* if we still have commands for this LUN, the reset
1269                  * effectively failed, regardless of the status reported.
1270                  * Escalate to a target reset.
1271                  */
1272                 mpssas_log_command(tm, MPS_RECOVERY,
1273                     "logical unit reset complete for tm %p, but still have %u command(s)\n",
1274                     tm, cm_count);
1275                 mpssas_send_reset(sc, tm,
1276                     MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1277         }
1278 }
1279
1280 static void
1281 mpssas_target_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1282 {
1283         MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1284         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1285         struct mpssas_target *targ;
1286
1287         callout_stop(&tm->cm_callout);
1288
1289         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1290         reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1291         targ = tm->cm_targ;
1292
1293         /*
1294          * Currently there should be no way we can hit this case.  It only
1295          * happens when we have a failure to allocate chain frames, and
1296          * task management commands don't have S/G lists.
1297          */
1298         if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1299                 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x for target reset! "
1300                            "This should not happen!\n", __func__, tm->cm_flags);
1301                 mpssas_free_tm(sc, tm);
1302                 return;
1303         }
1304
1305         if (reply == NULL) {
1306                 mpssas_log_command(tm, MPS_RECOVERY,
1307                     "NULL reset reply for tm %p\n", tm);
1308                 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1309                         /* this completion was due to a reset, just cleanup */
1310                         targ->flags &= ~MPSSAS_TARGET_INRESET;
1311                         targ->tm = NULL;
1312                         mpssas_free_tm(sc, tm);
1313                 }
1314                 else {
1315                         /* we should have gotten a reply. */
1316                         mps_reinit(sc);
1317                 }
1318                 return;
1319         }
1320
1321         mpssas_log_command(tm, MPS_RECOVERY,
1322             "target reset status 0x%x code 0x%x count %u\n",
1323             le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1324             le32toh(reply->TerminationCount));
1325
1326         targ->flags &= ~MPSSAS_TARGET_INRESET;
1327
1328         if (targ->outstanding == 0) {
1329                 /* we've finished recovery for this target and all
1330                  * of its logical units.
1331                  */
1332                 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1333                     "recovery finished after target reset\n");
1334
1335                 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1336                     CAM_LUN_WILDCARD);
1337
1338                 targ->tm = NULL;
1339                 mpssas_free_tm(sc, tm);
1340         }
1341         else {
1342                 /* after a target reset, if this target still has
1343                  * outstanding commands, the reset effectively failed,
1344                  * regardless of the status reported.  escalate.
1345                  */
1346                 mpssas_log_command(tm, MPS_RECOVERY,
1347                     "target reset complete for tm %p, but still have %u command(s)\n", 
1348                     tm, targ->outstanding);
1349                 mps_reinit(sc);
1350         }
1351 }
1352
1353 #define MPS_RESET_TIMEOUT 30
1354
1355 static int
1356 mpssas_send_reset(struct mps_softc *sc, struct mps_command *tm, uint8_t type)
1357 {
1358         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1359         struct mpssas_target *target;
1360         int err;
1361
1362         target = tm->cm_targ;
1363         if (target->handle == 0) {
1364                 mps_dprint(sc, MPS_ERROR,"%s null devhandle for target_id %d\n",
1365                     __func__, target->tid);
1366                 return -1;
1367         }
1368
1369         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1370         req->DevHandle = htole16(target->handle);
1371         req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1372         req->TaskType = type;
1373
1374         if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) {
1375                 /* XXX Need to handle invalid LUNs */
1376                 MPS_SET_LUN(req->LUN, tm->cm_lun);
1377                 tm->cm_targ->logical_unit_resets++;
1378                 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1379                     "sending logical unit reset\n");
1380                 tm->cm_complete = mpssas_logical_unit_reset_complete;
1381         }
1382         else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
1383                 /* Target reset method =  SAS Hard Link Reset / SATA Link Reset */
1384                 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
1385                 tm->cm_targ->target_resets++;
1386                 tm->cm_targ->flags |= MPSSAS_TARGET_INRESET;
1387                 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1388                     "sending target reset\n");
1389                 tm->cm_complete = mpssas_target_reset_complete;
1390         }
1391         else {
1392                 mps_dprint(sc, MPS_ERROR, "unexpected reset type 0x%x\n", type);
1393                 return -1;
1394         }
1395
1396         tm->cm_data = NULL;
1397         tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1398         tm->cm_complete_data = (void *)tm;
1399
1400         callout_reset(&tm->cm_callout, MPS_RESET_TIMEOUT * hz,
1401             mpssas_tm_timeout, tm);
1402
1403         err = mps_map_command(sc, tm);
1404         if (err)
1405                 mpssas_log_command(tm, MPS_RECOVERY,
1406                     "error %d sending reset type %u\n",
1407                     err, type);
1408
1409         return err;
1410 }
1411
1412
1413 static void
1414 mpssas_abort_complete(struct mps_softc *sc, struct mps_command *tm)
1415 {
1416         struct mps_command *cm;
1417         MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1418         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1419         struct mpssas_target *targ;
1420
1421         callout_stop(&tm->cm_callout);
1422
1423         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1424         reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1425         targ = tm->cm_targ;
1426
1427         /*
1428          * Currently there should be no way we can hit this case.  It only
1429          * happens when we have a failure to allocate chain frames, and
1430          * task management commands don't have S/G lists.
1431          */
1432         if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1433                 mpssas_log_command(tm, MPS_RECOVERY,
1434                     "cm_flags = %#x for abort %p TaskMID %u!\n", 
1435                     tm->cm_flags, tm, le16toh(req->TaskMID));
1436                 mpssas_free_tm(sc, tm);
1437                 return;
1438         }
1439
1440         if (reply == NULL) {
1441                 mpssas_log_command(tm, MPS_RECOVERY,
1442                     "NULL abort reply for tm %p TaskMID %u\n", 
1443                     tm, le16toh(req->TaskMID));
1444                 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1445                         /* this completion was due to a reset, just cleanup */
1446                         targ->tm = NULL;
1447                         mpssas_free_tm(sc, tm);
1448                 }
1449                 else {
1450                         /* we should have gotten a reply. */
1451                         mps_reinit(sc);
1452                 }
1453                 return;
1454         }
1455
1456         mpssas_log_command(tm, MPS_RECOVERY,
1457             "abort TaskMID %u status 0x%x code 0x%x count %u\n",
1458             le16toh(req->TaskMID),
1459             le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1460             le32toh(reply->TerminationCount));
1461
1462         cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands);
1463         if (cm == NULL) {
1464                 /* if there are no more timedout commands, we're done with
1465                  * error recovery for this target.
1466                  */
1467                 mpssas_log_command(tm, MPS_RECOVERY,
1468                     "finished recovery after aborting TaskMID %u\n",
1469                     le16toh(req->TaskMID));
1470
1471                 targ->tm = NULL;
1472                 mpssas_free_tm(sc, tm);
1473         }
1474         else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) {
1475                 /* abort success, but we have more timedout commands to abort */
1476                 mpssas_log_command(tm, MPS_RECOVERY,
1477                     "continuing recovery after aborting TaskMID %u\n",
1478                     le16toh(req->TaskMID));
1479                 
1480                 mpssas_send_abort(sc, tm, cm);
1481         }
1482         else {
1483                 /* we didn't get a command completion, so the abort
1484                  * failed as far as we're concerned.  escalate.
1485                  */
1486                 mpssas_log_command(tm, MPS_RECOVERY,
1487                     "abort failed for TaskMID %u tm %p\n",
1488                     le16toh(req->TaskMID), tm);
1489
1490                 mpssas_send_reset(sc, tm, 
1491                     MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET);
1492         }
1493 }
1494
1495 #define MPS_ABORT_TIMEOUT 5
1496
1497 static int
1498 mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm, struct mps_command *cm)
1499 {
1500         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1501         struct mpssas_target *targ;
1502         int err;
1503
1504         targ = cm->cm_targ;
1505         if (targ->handle == 0) {
1506                 mps_dprint(sc, MPS_ERROR,"%s null devhandle for target_id %d\n",
1507                     __func__, cm->cm_ccb->ccb_h.target_id);
1508                 return -1;
1509         }
1510
1511         mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1512             "Aborting command %p\n", cm);
1513
1514         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1515         req->DevHandle = htole16(targ->handle);
1516         req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1517         req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1518
1519         /* XXX Need to handle invalid LUNs */
1520         MPS_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun);
1521
1522         req->TaskMID = htole16(cm->cm_desc.Default.SMID);
1523
1524         tm->cm_data = NULL;
1525         tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1526         tm->cm_complete = mpssas_abort_complete;
1527         tm->cm_complete_data = (void *)tm;
1528         tm->cm_targ = cm->cm_targ;
1529         tm->cm_lun = cm->cm_lun;
1530
1531         callout_reset(&tm->cm_callout, MPS_ABORT_TIMEOUT * hz,
1532             mpssas_tm_timeout, tm);
1533
1534         targ->aborts++;
1535
1536         err = mps_map_command(sc, tm);
1537         if (err)
1538                 mpssas_log_command(tm, MPS_RECOVERY,
1539                     "error %d sending abort for cm %p SMID %u\n",
1540                     err, cm, req->TaskMID);
1541         return err;
1542 }
1543
1544
1545 static void
1546 mpssas_scsiio_timeout(void *data)
1547 {
1548         struct mps_softc *sc;
1549         struct mps_command *cm;
1550         struct mpssas_target *targ;
1551
1552         cm = (struct mps_command *)data;
1553         sc = cm->cm_sc;
1554
1555         MPS_FUNCTRACE(sc);
1556         mtx_assert(&sc->mps_mtx, MA_OWNED);
1557
1558         mps_dprint(sc, MPS_XINFO, "Timeout checking cm %p\n", sc);
1559
1560         /*
1561          * Run the interrupt handler to make sure it's not pending.  This
1562          * isn't perfect because the command could have already completed
1563          * and been re-used, though this is unlikely.
1564          */
1565         mps_intr_locked(sc);
1566         if (cm->cm_state == MPS_CM_STATE_FREE) {
1567                 mpssas_log_command(cm, MPS_XINFO,
1568                     "SCSI command %p almost timed out\n", cm);
1569                 return;
1570         }
1571
1572         if (cm->cm_ccb == NULL) {
1573                 mps_dprint(sc, MPS_ERROR, "command timeout with NULL ccb\n");
1574                 return;
1575         }
1576
1577         mpssas_log_command(cm, MPS_INFO, "command timeout cm %p ccb %p\n", 
1578             cm, cm->cm_ccb);
1579
1580         targ = cm->cm_targ;
1581         targ->timeouts++;
1582
1583         /* XXX first, check the firmware state, to see if it's still
1584          * operational.  if not, do a diag reset.
1585          */
1586
1587         cm->cm_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1588         cm->cm_state = MPS_CM_STATE_TIMEDOUT;
1589         TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery);
1590
1591         if (targ->tm != NULL) {
1592                 /* target already in recovery, just queue up another
1593                  * timedout command to be processed later.
1594                  */
1595                 mps_dprint(sc, MPS_RECOVERY,
1596                     "queued timedout cm %p for processing by tm %p\n",
1597                     cm, targ->tm);
1598         }
1599         else if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) {
1600                 mps_dprint(sc, MPS_RECOVERY, "timedout cm %p allocated tm %p\n",
1601                     cm, targ->tm);
1602
1603                 /* start recovery by aborting the first timedout command */
1604                 mpssas_send_abort(sc, targ->tm, cm);
1605         }
1606         else {
1607                 /* XXX queue this target up for recovery once a TM becomes
1608                  * available.  The firmware only has a limited number of
1609                  * HighPriority credits for the high priority requests used
1610                  * for task management, and we ran out.
1611                  * 
1612                  * Isilon: don't worry about this for now, since we have
1613                  * more credits than disks in an enclosure, and limit
1614                  * ourselves to one TM per target for recovery.
1615                  */
1616                 mps_dprint(sc, MPS_RECOVERY,
1617                     "timedout cm %p failed to allocate a tm\n", cm);
1618         }
1619
1620 }
1621
1622 static void
1623 mpssas_action_scsiio(struct mpssas_softc *sassc, union ccb *ccb)
1624 {
1625         MPI2_SCSI_IO_REQUEST *req;
1626         struct ccb_scsiio *csio;
1627         struct mps_softc *sc;
1628         struct mpssas_target *targ;
1629         struct mpssas_lun *lun;
1630         struct mps_command *cm;
1631         uint8_t i, lba_byte, *ref_tag_addr;
1632         uint16_t eedp_flags;
1633         uint32_t mpi_control;
1634
1635         sc = sassc->sc;
1636         MPS_FUNCTRACE(sc);
1637         mtx_assert(&sc->mps_mtx, MA_OWNED);
1638
1639         csio = &ccb->csio;
1640         targ = &sassc->targets[csio->ccb_h.target_id];
1641         mps_dprint(sc, MPS_TRACE, "ccb %p target flag %x\n", ccb, targ->flags);
1642         if (targ->handle == 0x0) {
1643                 mps_dprint(sc, MPS_ERROR, "%s NULL handle for target %u\n", 
1644                     __func__, csio->ccb_h.target_id);
1645                 csio->ccb_h.status = CAM_SEL_TIMEOUT;
1646                 xpt_done(ccb);
1647                 return;
1648         }
1649         if (targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT) {
1650                 mps_dprint(sc, MPS_ERROR, "%s Raid component no SCSI IO "
1651                     "supported %u\n", __func__, csio->ccb_h.target_id);
1652                 csio->ccb_h.status = CAM_TID_INVALID;
1653                 xpt_done(ccb);
1654                 return;
1655         }
1656         /*
1657          * Sometimes, it is possible to get a command that is not "In
1658          * Progress" and was actually aborted by the upper layer.  Check for
1659          * this here and complete the command without error.
1660          */
1661         if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1662                 mps_dprint(sc, MPS_TRACE, "%s Command is not in progress for "
1663                     "target %u\n", __func__, csio->ccb_h.target_id);
1664                 xpt_done(ccb);
1665                 return;
1666         }
1667         /*
1668          * If devinfo is 0 this will be a volume.  In that case don't tell CAM
1669          * that the volume has timed out.  We want volumes to be enumerated
1670          * until they are deleted/removed, not just failed.
1671          */
1672         if (targ->flags & MPSSAS_TARGET_INREMOVAL) {
1673                 if (targ->devinfo == 0)
1674                         csio->ccb_h.status = CAM_REQ_CMP;
1675                 else
1676                         csio->ccb_h.status = CAM_SEL_TIMEOUT;
1677                 xpt_done(ccb);
1678                 return;
1679         }
1680
1681         if ((sc->mps_flags & MPS_FLAGS_SHUTDOWN) != 0) {
1682                 mps_dprint(sc, MPS_INFO, "%s shutting down\n", __func__);
1683                 csio->ccb_h.status = CAM_TID_INVALID;
1684                 xpt_done(ccb);
1685                 return;
1686         }
1687
1688         cm = mps_alloc_command(sc);
1689         if (cm == NULL) {
1690                 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
1691                         xpt_freeze_simq(sassc->sim, 1);
1692                         sassc->flags |= MPSSAS_QUEUE_FROZEN;
1693                 }
1694                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1695                 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1696                 xpt_done(ccb);
1697                 return;
1698         }
1699
1700         req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1701         bzero(req, sizeof(*req));
1702         req->DevHandle = htole16(targ->handle);
1703         req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1704         req->MsgFlags = 0;
1705         req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr);
1706         req->SenseBufferLength = MPS_SENSE_LEN;
1707         req->SGLFlags = 0;
1708         req->ChainOffset = 0;
1709         req->SGLOffset0 = 24;   /* 32bit word offset to the SGL */
1710         req->SGLOffset1= 0;
1711         req->SGLOffset2= 0;
1712         req->SGLOffset3= 0;
1713         req->SkipCount = 0;
1714         req->DataLength = htole32(csio->dxfer_len);
1715         req->BidirectionalDataLength = 0;
1716         req->IoFlags = htole16(csio->cdb_len);
1717         req->EEDPFlags = 0;
1718
1719         /* Note: BiDirectional transfers are not supported */
1720         switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1721         case CAM_DIR_IN:
1722                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
1723                 cm->cm_flags |= MPS_CM_FLAGS_DATAIN;
1724                 break;
1725         case CAM_DIR_OUT:
1726                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
1727                 cm->cm_flags |= MPS_CM_FLAGS_DATAOUT;
1728                 break;
1729         case CAM_DIR_NONE:
1730         default:
1731                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1732                 break;
1733         }
1734  
1735         if (csio->cdb_len == 32)
1736                 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
1737         /*
1738          * It looks like the hardware doesn't require an explicit tag
1739          * number for each transaction.  SAM Task Management not supported
1740          * at the moment.
1741          */
1742         switch (csio->tag_action) {
1743         case MSG_HEAD_OF_Q_TAG:
1744                 mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
1745                 break;
1746         case MSG_ORDERED_Q_TAG:
1747                 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
1748                 break;
1749         case MSG_ACA_TASK:
1750                 mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ;
1751                 break;
1752         case CAM_TAG_ACTION_NONE:
1753         case MSG_SIMPLE_Q_TAG:
1754         default:
1755                 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
1756                 break;
1757         }
1758         mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits;
1759         req->Control = htole32(mpi_control);
1760         if (MPS_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
1761                 mps_free_command(sc, cm);
1762                 ccb->ccb_h.status = CAM_LUN_INVALID;
1763                 xpt_done(ccb);
1764                 return;
1765         }
1766
1767         if (csio->ccb_h.flags & CAM_CDB_POINTER)
1768                 bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
1769         else
1770                 bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
1771         req->IoFlags = htole16(csio->cdb_len);
1772
1773         /*
1774          * Check if EEDP is supported and enabled.  If it is then check if the
1775          * SCSI opcode could be using EEDP.  If so, make sure the LUN exists and
1776          * is formatted for EEDP support.  If all of this is true, set CDB up
1777          * for EEDP transfer.
1778          */
1779         eedp_flags = op_code_prot[req->CDB.CDB32[0]];
1780         if (sc->eedp_enabled && eedp_flags) {
1781                 SLIST_FOREACH(lun, &targ->luns, lun_link) {
1782                         if (lun->lun_id == csio->ccb_h.target_lun) {
1783                                 break;
1784                         }
1785                 }
1786
1787                 if ((lun != NULL) && (lun->eedp_formatted)) {
1788                         req->EEDPBlockSize = htole16(lun->eedp_block_size);
1789                         eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1790                             MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1791                             MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1792                         req->EEDPFlags = htole16(eedp_flags);
1793
1794                         /*
1795                          * If CDB less than 32, fill in Primary Ref Tag with
1796                          * low 4 bytes of LBA.  If CDB is 32, tag stuff is
1797                          * already there.  Also, set protection bit.  FreeBSD
1798                          * currently does not support CDBs bigger than 16, but
1799                          * the code doesn't hurt, and will be here for the
1800                          * future.
1801                          */
1802                         if (csio->cdb_len != 32) {
1803                                 lba_byte = (csio->cdb_len == 16) ? 6 : 2;
1804                                 ref_tag_addr = (uint8_t *)&req->CDB.EEDP32.
1805                                     PrimaryReferenceTag;
1806                                 for (i = 0; i < 4; i++) {
1807                                         *ref_tag_addr =
1808                                             req->CDB.CDB32[lba_byte + i];
1809                                         ref_tag_addr++;
1810                                 }
1811                                 req->CDB.EEDP32.PrimaryReferenceTag = 
1812                                         htole32(req->CDB.EEDP32.PrimaryReferenceTag);
1813                                 req->CDB.EEDP32.PrimaryApplicationTagMask =
1814                                     0xFFFF;
1815                                 req->CDB.CDB32[1] = (req->CDB.CDB32[1] & 0x1F) |
1816                                     0x20;
1817                         } else {
1818                                 eedp_flags |=
1819                                     MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG;
1820                                 req->EEDPFlags = htole16(eedp_flags);
1821                                 req->CDB.CDB32[10] = (req->CDB.CDB32[10] &
1822                                     0x1F) | 0x20;
1823                         }
1824                 }
1825         }
1826
1827         cm->cm_length = csio->dxfer_len;
1828         if (cm->cm_length != 0) {
1829                 cm->cm_data = ccb;
1830                 cm->cm_flags |= MPS_CM_FLAGS_USE_CCB;
1831         } else {
1832                 cm->cm_data = NULL;
1833         }
1834         cm->cm_sge = &req->SGL;
1835         cm->cm_sglsize = (32 - 24) * 4;
1836         cm->cm_desc.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1837         cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle);
1838         cm->cm_complete = mpssas_scsiio_complete;
1839         cm->cm_complete_data = ccb;
1840         cm->cm_targ = targ;
1841         cm->cm_lun = csio->ccb_h.target_lun;
1842         cm->cm_ccb = ccb;
1843
1844         /*
1845          * If HBA is a WD and the command is not for a retry, try to build a
1846          * direct I/O message. If failed, or the command is for a retry, send
1847          * the I/O to the IR volume itself.
1848          */
1849         if (sc->WD_valid_config) {
1850                 if (ccb->ccb_h.status != MPS_WD_RETRY) {
1851                         mpssas_direct_drive_io(sassc, cm, ccb);
1852                 } else {
1853                         ccb->ccb_h.status = CAM_REQ_INPROG;
1854                 }
1855         }
1856
1857         callout_reset(&cm->cm_callout, (ccb->ccb_h.timeout * hz) / 1000,
1858            mpssas_scsiio_timeout, cm);
1859
1860         targ->issued++;
1861         targ->outstanding++;
1862         TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
1863         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1864
1865         mpssas_log_command(cm, MPS_XINFO, "%s cm %p ccb %p outstanding %u\n",
1866             __func__, cm, ccb, targ->outstanding);
1867
1868         mps_map_command(sc, cm);
1869         return;
1870 }
1871
1872 static void
1873 mps_response_code(struct mps_softc *sc, u8 response_code)
1874 {
1875         char *desc;
1876  
1877         switch (response_code) {
1878         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1879                 desc = "task management request completed";
1880                 break;
1881         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1882                 desc = "invalid frame";
1883                 break;
1884         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1885                 desc = "task management request not supported";
1886                 break;
1887         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1888                 desc = "task management request failed";
1889                 break;
1890         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1891                 desc = "task management request succeeded";
1892                 break;
1893         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1894                 desc = "invalid lun";
1895                 break;
1896         case 0xA:
1897                 desc = "overlapped tag attempted";
1898                 break;
1899         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1900                 desc = "task queued, however not sent to target";
1901                 break;
1902         default:
1903                 desc = "unknown";
1904                 break;
1905         }
1906                 mps_dprint(sc, MPS_XINFO, "response_code(0x%01x): %s\n",
1907                 response_code, desc);
1908 }
1909 /**
1910  * mps_sc_failed_io_info - translated non-succesfull SCSI_IO request
1911  */
1912 static void
1913 mps_sc_failed_io_info(struct mps_softc *sc, struct ccb_scsiio *csio,
1914     Mpi2SCSIIOReply_t *mpi_reply)
1915 {
1916         u32 response_info;
1917         u8 *response_bytes;
1918         u16 ioc_status = le16toh(mpi_reply->IOCStatus) &
1919             MPI2_IOCSTATUS_MASK;
1920         u8 scsi_state = mpi_reply->SCSIState;
1921         u8 scsi_status = mpi_reply->SCSIStatus;
1922         char *desc_ioc_state = NULL;
1923         char *desc_scsi_status = NULL;
1924         char *desc_scsi_state = sc->tmp_string;
1925         u32 log_info = le32toh(mpi_reply->IOCLogInfo);
1926         
1927         if (log_info == 0x31170000)
1928                 return;
1929
1930         switch (ioc_status) {
1931         case MPI2_IOCSTATUS_SUCCESS:
1932                 desc_ioc_state = "success";
1933                 break;
1934         case MPI2_IOCSTATUS_INVALID_FUNCTION:
1935                 desc_ioc_state = "invalid function";
1936                 break;
1937         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
1938                 desc_ioc_state = "scsi recovered error";
1939                 break;
1940         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
1941                 desc_ioc_state = "scsi invalid dev handle";
1942                 break;
1943         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
1944                 desc_ioc_state = "scsi device not there";
1945                 break;
1946         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
1947                 desc_ioc_state = "scsi data overrun";
1948                 break;
1949         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
1950                 desc_ioc_state = "scsi data underrun";
1951                 break;
1952         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
1953                 desc_ioc_state = "scsi io data error";
1954                 break;
1955         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1956                 desc_ioc_state = "scsi protocol error";
1957                 break;
1958         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
1959                 desc_ioc_state = "scsi task terminated";
1960                 break;
1961         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
1962                 desc_ioc_state = "scsi residual mismatch";
1963                 break;
1964         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
1965                 desc_ioc_state = "scsi task mgmt failed";
1966                 break;
1967         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
1968                 desc_ioc_state = "scsi ioc terminated";
1969                 break;
1970         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
1971                 desc_ioc_state = "scsi ext terminated";
1972                 break;
1973         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
1974                 desc_ioc_state = "eedp guard error";
1975                 break;
1976         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
1977                 desc_ioc_state = "eedp ref tag error";
1978                 break;
1979         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
1980                 desc_ioc_state = "eedp app tag error";
1981                 break;
1982         default:
1983                 desc_ioc_state = "unknown";
1984                 break;
1985         }
1986
1987         switch (scsi_status) {
1988         case MPI2_SCSI_STATUS_GOOD:
1989                 desc_scsi_status = "good";
1990                 break;
1991         case MPI2_SCSI_STATUS_CHECK_CONDITION:
1992                 desc_scsi_status = "check condition";
1993                 break;
1994         case MPI2_SCSI_STATUS_CONDITION_MET:
1995                 desc_scsi_status = "condition met";
1996                 break;
1997         case MPI2_SCSI_STATUS_BUSY:
1998                 desc_scsi_status = "busy";
1999                 break;
2000         case MPI2_SCSI_STATUS_INTERMEDIATE:
2001                 desc_scsi_status = "intermediate";
2002                 break;
2003         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2004                 desc_scsi_status = "intermediate condmet";
2005                 break;
2006         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2007                 desc_scsi_status = "reservation conflict";
2008                 break;
2009         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2010                 desc_scsi_status = "command terminated";
2011                 break;
2012         case MPI2_SCSI_STATUS_TASK_SET_FULL:
2013                 desc_scsi_status = "task set full";
2014                 break;
2015         case MPI2_SCSI_STATUS_ACA_ACTIVE:
2016                 desc_scsi_status = "aca active";
2017                 break;
2018         case MPI2_SCSI_STATUS_TASK_ABORTED:
2019                 desc_scsi_status = "task aborted";
2020                 break;
2021         default:
2022                 desc_scsi_status = "unknown";
2023                 break;
2024         }
2025
2026         desc_scsi_state[0] = '\0';
2027         if (!scsi_state)
2028                 desc_scsi_state = " ";
2029         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2030                 strcat(desc_scsi_state, "response info ");
2031         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2032                 strcat(desc_scsi_state, "state terminated ");
2033         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2034                 strcat(desc_scsi_state, "no status ");
2035         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2036                 strcat(desc_scsi_state, "autosense failed ");
2037         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2038                 strcat(desc_scsi_state, "autosense valid ");
2039
2040         mps_dprint(sc, MPS_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n",
2041             le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status);
2042         /* We can add more detail about underflow data here
2043          * TO-DO
2044          * */
2045         mps_dprint(sc, MPS_XINFO, "\tscsi_status(%s)(0x%02x), "
2046             "scsi_state(%s)(0x%02x)\n", desc_scsi_status, scsi_status,
2047             desc_scsi_state, scsi_state);
2048
2049         if (sc->mps_debug & MPS_XINFO &&
2050                 scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2051                 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : Start :\n");
2052                 scsi_sense_print(csio);
2053                 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : End :\n");
2054         }
2055
2056         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2057                 response_info = le32toh(mpi_reply->ResponseInfo);
2058                 response_bytes = (u8 *)&response_info;
2059                 mps_response_code(sc,response_bytes[0]);
2060         }
2061 }
2062
2063 static void
2064 mpssas_scsiio_complete(struct mps_softc *sc, struct mps_command *cm)
2065 {
2066         MPI2_SCSI_IO_REPLY *rep;
2067         union ccb *ccb;
2068         struct ccb_scsiio *csio;
2069         struct mpssas_softc *sassc;
2070         struct scsi_vpd_supported_page_list *vpd_list = NULL;
2071         u8 *TLR_bits, TLR_on;
2072         int dir = 0, i;
2073         u16 alloc_len;
2074
2075         MPS_FUNCTRACE(sc);
2076         mps_dprint(sc, MPS_TRACE,
2077             "cm %p SMID %u ccb %p reply %p outstanding %u\n", cm,
2078             cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply,
2079             cm->cm_targ->outstanding);
2080
2081         callout_stop(&cm->cm_callout);
2082         mtx_assert(&sc->mps_mtx, MA_OWNED);
2083
2084         sassc = sc->sassc;
2085         ccb = cm->cm_complete_data;
2086         csio = &ccb->csio;
2087         rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
2088         /*
2089          * XXX KDM if the chain allocation fails, does it matter if we do
2090          * the sync and unload here?  It is simpler to do it in every case,
2091          * assuming it doesn't cause problems.
2092          */
2093         if (cm->cm_data != NULL) {
2094                 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN)
2095                         dir = BUS_DMASYNC_POSTREAD;
2096                 else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT)
2097                         dir = BUS_DMASYNC_POSTWRITE;
2098                 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2099                 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2100         }
2101
2102         cm->cm_targ->completed++;
2103         cm->cm_targ->outstanding--;
2104         TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link);
2105         ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED);
2106
2107         if (cm->cm_state == MPS_CM_STATE_TIMEDOUT) {
2108                 TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery);
2109                 if (cm->cm_reply != NULL)
2110                         mpssas_log_command(cm, MPS_RECOVERY,
2111                             "completed timedout cm %p ccb %p during recovery "
2112                             "ioc %x scsi %x state %x xfer %u\n",
2113                             cm, cm->cm_ccb,
2114                             le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2115                             le32toh(rep->TransferCount));
2116                 else
2117                         mpssas_log_command(cm, MPS_RECOVERY,
2118                             "completed timedout cm %p ccb %p during recovery\n",
2119                             cm, cm->cm_ccb);
2120         } else if (cm->cm_targ->tm != NULL) {
2121                 if (cm->cm_reply != NULL)
2122                         mpssas_log_command(cm, MPS_RECOVERY,
2123                             "completed cm %p ccb %p during recovery "
2124                             "ioc %x scsi %x state %x xfer %u\n",
2125                             cm, cm->cm_ccb,
2126                             le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2127                             le32toh(rep->TransferCount));
2128                 else
2129                         mpssas_log_command(cm, MPS_RECOVERY,
2130                             "completed cm %p ccb %p during recovery\n",
2131                             cm, cm->cm_ccb);
2132         } else if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
2133                 mpssas_log_command(cm, MPS_RECOVERY,
2134                     "reset completed cm %p ccb %p\n",
2135                     cm, cm->cm_ccb);
2136         }
2137
2138         if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2139                 /*
2140                  * We ran into an error after we tried to map the command,
2141                  * so we're getting a callback without queueing the command
2142                  * to the hardware.  So we set the status here, and it will
2143                  * be retained below.  We'll go through the "fast path",
2144                  * because there can be no reply when we haven't actually
2145                  * gone out to the hardware.
2146                  */
2147                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2148
2149                 /*
2150                  * Currently the only error included in the mask is
2151                  * MPS_CM_FLAGS_CHAIN_FAILED, which means we're out of
2152                  * chain frames.  We need to freeze the queue until we get
2153                  * a command that completed without this error, which will
2154                  * hopefully have some chain frames attached that we can
2155                  * use.  If we wanted to get smarter about it, we would
2156                  * only unfreeze the queue in this condition when we're
2157                  * sure that we're getting some chain frames back.  That's
2158                  * probably unnecessary.
2159                  */
2160                 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
2161                         xpt_freeze_simq(sassc->sim, 1);
2162                         sassc->flags |= MPSSAS_QUEUE_FROZEN;
2163                         mps_dprint(sc, MPS_XINFO, "Error sending command, "
2164                                    "freezing SIM queue\n");
2165                 }
2166         }
2167
2168         /* Take the fast path to completion */
2169         if (cm->cm_reply == NULL) {
2170                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
2171                         if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0)
2172                                 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
2173                         else {
2174                                 ccb->ccb_h.status = CAM_REQ_CMP;
2175                                 ccb->csio.scsi_status = SCSI_STATUS_OK;
2176                         }
2177                         if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2178                                 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2179                                 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2180                                 mps_dprint(sc, MPS_XINFO,
2181                                     "Unfreezing SIM queue\n");
2182                         }
2183                 } 
2184
2185                 /*
2186                  * There are two scenarios where the status won't be
2187                  * CAM_REQ_CMP.  The first is if MPS_CM_FLAGS_ERROR_MASK is
2188                  * set, the second is in the MPS_FLAGS_DIAGRESET above.
2189                  */
2190                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2191                         /*
2192                          * Freeze the dev queue so that commands are
2193                          * executed in the correct order with after error
2194                          * recovery.
2195                          */
2196                         ccb->ccb_h.status |= CAM_DEV_QFRZN;
2197                         xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2198                 }
2199                 mps_free_command(sc, cm);
2200                 xpt_done(ccb);
2201                 return;
2202         }
2203
2204         mpssas_log_command(cm, MPS_XINFO,
2205             "ioc %x scsi %x state %x xfer %u\n",
2206             le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2207             le32toh(rep->TransferCount));
2208
2209         /*
2210          * If this is a Direct Drive I/O, reissue the I/O to the original IR
2211          * Volume if an error occurred (normal I/O retry).  Use the original
2212          * CCB, but set a flag that this will be a retry so that it's sent to
2213          * the original volume.  Free the command but reuse the CCB.
2214          */
2215         if (cm->cm_flags & MPS_CM_FLAGS_DD_IO) {
2216                 mps_free_command(sc, cm);
2217                 ccb->ccb_h.status = MPS_WD_RETRY;
2218                 mpssas_action_scsiio(sassc, ccb);
2219                 return;
2220         }
2221
2222         switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) {
2223         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2224                 csio->resid = cm->cm_length - le32toh(rep->TransferCount);
2225                 /* FALLTHROUGH */
2226         case MPI2_IOCSTATUS_SUCCESS:
2227         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2228
2229                 if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
2230                     MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR)
2231                         mpssas_log_command(cm, MPS_XINFO, "recovered error\n");
2232
2233                 /* Completion failed at the transport level. */
2234                 if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS |
2235                     MPI2_SCSI_STATE_TERMINATED)) {
2236                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2237                         break;
2238                 }
2239
2240                 /* In a modern packetized environment, an autosense failure
2241                  * implies that there's not much else that can be done to
2242                  * recover the command.
2243                  */
2244                 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) {
2245                         ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
2246                         break;
2247                 }
2248
2249                 /*
2250                  * CAM doesn't care about SAS Response Info data, but if this is
2251                  * the state check if TLR should be done.  If not, clear the
2252                  * TLR_bits for the target.
2253                  */
2254                 if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) &&
2255                     ((le32toh(rep->ResponseInfo) & MPI2_SCSI_RI_MASK_REASONCODE) ==
2256                     MPS_SCSI_RI_INVALID_FRAME)) {
2257                         sc->mapping_table[csio->ccb_h.target_id].TLR_bits =
2258                             (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2259                 }
2260
2261                 /*
2262                  * Intentionally override the normal SCSI status reporting
2263                  * for these two cases.  These are likely to happen in a
2264                  * multi-initiator environment, and we want to make sure that
2265                  * CAM retries these commands rather than fail them.
2266                  */
2267                 if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) ||
2268                     (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) {
2269                         ccb->ccb_h.status = CAM_REQ_ABORTED;
2270                         break;
2271                 }
2272
2273                 /* Handle normal status and sense */
2274                 csio->scsi_status = rep->SCSIStatus;
2275                 if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD)
2276                         ccb->ccb_h.status = CAM_REQ_CMP;
2277                 else
2278                         ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
2279
2280                 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2281                         int sense_len, returned_sense_len;
2282
2283                         returned_sense_len = min(le32toh(rep->SenseCount),
2284                             sizeof(struct scsi_sense_data));
2285                         if (returned_sense_len < ccb->csio.sense_len)
2286                                 ccb->csio.sense_resid = ccb->csio.sense_len -
2287                                         returned_sense_len;
2288                         else
2289                                 ccb->csio.sense_resid = 0;
2290
2291                         sense_len = min(returned_sense_len,
2292                             ccb->csio.sense_len - ccb->csio.sense_resid);
2293                         bzero(&ccb->csio.sense_data,
2294                               sizeof(ccb->csio.sense_data));
2295                         bcopy(cm->cm_sense, &ccb->csio.sense_data, sense_len);
2296                         ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2297                 }
2298
2299                 /*
2300                  * Check if this is an INQUIRY command.  If it's a VPD inquiry,
2301                  * and it's page code 0 (Supported Page List), and there is
2302                  * inquiry data, and this is for a sequential access device, and
2303                  * the device is an SSP target, and TLR is supported by the
2304                  * controller, turn the TLR_bits value ON if page 0x90 is
2305                  * supported.
2306                  */
2307                 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2308                     (csio->cdb_io.cdb_bytes[1] & SI_EVPD) &&
2309                     (csio->cdb_io.cdb_bytes[2] == SVPD_SUPPORTED_PAGE_LIST) &&
2310                     ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) &&
2311                     (csio->data_ptr != NULL) && (((uint8_t *)cm->cm_data)[0] ==
2312                     T_SEQUENTIAL) && (sc->control_TLR) &&
2313                     (sc->mapping_table[csio->ccb_h.target_id].device_info &
2314                     MPI2_SAS_DEVICE_INFO_SSP_TARGET)) {
2315                         vpd_list = (struct scsi_vpd_supported_page_list *)
2316                             csio->data_ptr;
2317                         TLR_bits = &sc->mapping_table[csio->ccb_h.target_id].
2318                             TLR_bits;
2319                         *TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2320                         TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON;
2321                         alloc_len = ((u16)csio->cdb_io.cdb_bytes[3] << 8) +
2322                             csio->cdb_io.cdb_bytes[4];
2323                         for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) {
2324                                 if (vpd_list->list[i] == 0x90) {
2325                                         *TLR_bits = TLR_on;
2326                                         break;
2327                                 }
2328                         }
2329                 }
2330                 break;
2331         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2332         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2333                 /*
2334                  * If devinfo is 0 this will be a volume.  In that case don't
2335                  * tell CAM that the volume is not there.  We want volumes to
2336                  * be enumerated until they are deleted/removed, not just
2337                  * failed.
2338                  */
2339                 if (cm->cm_targ->devinfo == 0)
2340                         ccb->ccb_h.status = CAM_REQ_CMP;
2341                 else
2342                         ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2343                 break;
2344         case MPI2_IOCSTATUS_INVALID_SGL:
2345                 mps_print_scsiio_cmd(sc, cm);
2346                 ccb->ccb_h.status = CAM_UNREC_HBA_ERROR;
2347                 break;
2348         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2349                 /*
2350                  * This is one of the responses that comes back when an I/O
2351                  * has been aborted.  If it is because of a timeout that we
2352                  * initiated, just set the status to CAM_CMD_TIMEOUT.
2353                  * Otherwise set it to CAM_REQ_ABORTED.  The effect on the
2354                  * command is the same (it gets retried, subject to the
2355                  * retry counter), the only difference is what gets printed
2356                  * on the console.
2357                  */
2358                 if (cm->cm_state == MPS_CM_STATE_TIMEDOUT)
2359                         ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2360                 else
2361                         ccb->ccb_h.status = CAM_REQ_ABORTED;
2362                 break;
2363         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2364                 /* resid is ignored for this condition */
2365                 csio->resid = 0;
2366                 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
2367                 break;
2368         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2369         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2370                 /*
2371                  * Since these are generally external (i.e. hopefully
2372                  * transient transport-related) errors, retry these without
2373                  * decrementing the retry count.
2374                  */
2375                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
2376                 mpssas_log_command(cm, MPS_INFO,
2377                     "terminated ioc %x scsi %x state %x xfer %u\n",
2378                     le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2379                     le32toh(rep->TransferCount));
2380                 break;
2381         case MPI2_IOCSTATUS_INVALID_FUNCTION:
2382         case MPI2_IOCSTATUS_INTERNAL_ERROR:
2383         case MPI2_IOCSTATUS_INVALID_VPID:
2384         case MPI2_IOCSTATUS_INVALID_FIELD:
2385         case MPI2_IOCSTATUS_INVALID_STATE:
2386         case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
2387         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2388         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2389         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2390         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2391         default:
2392                 mpssas_log_command(cm, MPS_XINFO,
2393                     "completed ioc %x scsi %x state %x xfer %u\n",
2394                     le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2395                     le32toh(rep->TransferCount));
2396                 csio->resid = cm->cm_length;
2397                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2398                 break;
2399         }
2400         
2401         mps_sc_failed_io_info(sc,csio,rep);
2402
2403         if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2404                 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2405                 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2406                 mps_dprint(sc, MPS_XINFO, "Command completed, "
2407                     "unfreezing SIM queue\n");
2408         }
2409
2410         if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2411                 ccb->ccb_h.status |= CAM_DEV_QFRZN;
2412                 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2413         }
2414
2415         mps_free_command(sc, cm);
2416         xpt_done(ccb);
2417 }
2418
2419 /* All Request reached here are Endian safe */
2420 static void
2421 mpssas_direct_drive_io(struct mpssas_softc *sassc, struct mps_command *cm,
2422     union ccb *ccb) {
2423         pMpi2SCSIIORequest_t    pIO_req;
2424         struct mps_softc        *sc = sassc->sc;
2425         uint64_t                virtLBA;
2426         uint32_t                physLBA, stripe_offset, stripe_unit;
2427         uint32_t                io_size, column;
2428         uint8_t                 *ptrLBA, lba_idx, physLBA_byte, *CDB;
2429
2430         /*
2431          * If this is a valid SCSI command (Read6, Read10, Read16, Write6,
2432          * Write10, or Write16), build a direct I/O message.  Otherwise, the I/O
2433          * will be sent to the IR volume itself.  Since Read6 and Write6 are a
2434          * bit different than the 10/16 CDBs, handle them separately.
2435          */
2436         pIO_req = (pMpi2SCSIIORequest_t)cm->cm_req;
2437         CDB = pIO_req->CDB.CDB32;
2438
2439         /*
2440          * Handle 6 byte CDBs.
2441          */
2442         if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_6) ||
2443             (CDB[0] == WRITE_6))) {
2444                 /*
2445                  * Get the transfer size in blocks.
2446                  */
2447                 io_size = (cm->cm_length >> sc->DD_block_exponent);
2448
2449                 /*
2450                  * Get virtual LBA given in the CDB.
2451                  */
2452                 virtLBA = ((uint64_t)(CDB[1] & 0x1F) << 16) |
2453                     ((uint64_t)CDB[2] << 8) | (uint64_t)CDB[3];
2454
2455                 /*
2456                  * Check that LBA range for I/O does not exceed volume's
2457                  * MaxLBA.
2458                  */
2459                 if ((virtLBA + (uint64_t)io_size - 1) <=
2460                     sc->DD_max_lba) {
2461                         /*
2462                          * Check if the I/O crosses a stripe boundary.  If not,
2463                          * translate the virtual LBA to a physical LBA and set
2464                          * the DevHandle for the PhysDisk to be used.  If it
2465                          * does cross a boundry, do normal I/O.  To get the
2466                          * right DevHandle to use, get the map number for the
2467                          * column, then use that map number to look up the
2468                          * DevHandle of the PhysDisk.
2469                          */
2470                         stripe_offset = (uint32_t)virtLBA &
2471                             (sc->DD_stripe_size - 1);
2472                         if ((stripe_offset + io_size) <= sc->DD_stripe_size) {
2473                                 physLBA = (uint32_t)virtLBA >>
2474                                     sc->DD_stripe_exponent;
2475                                 stripe_unit = physLBA / sc->DD_num_phys_disks;
2476                                 column = physLBA % sc->DD_num_phys_disks;
2477                                 pIO_req->DevHandle =
2478                                     htole16(sc->DD_column_map[column].dev_handle);
2479                                 /* ???? Is this endian safe*/
2480                                 cm->cm_desc.SCSIIO.DevHandle =
2481                                     pIO_req->DevHandle;
2482
2483                                 physLBA = (stripe_unit <<
2484                                     sc->DD_stripe_exponent) + stripe_offset;
2485                                 ptrLBA = &pIO_req->CDB.CDB32[1];
2486                                 physLBA_byte = (uint8_t)(physLBA >> 16);
2487                                 *ptrLBA = physLBA_byte;
2488                                 ptrLBA = &pIO_req->CDB.CDB32[2];
2489                                 physLBA_byte = (uint8_t)(physLBA >> 8);
2490                                 *ptrLBA = physLBA_byte;
2491                                 ptrLBA = &pIO_req->CDB.CDB32[3];
2492                                 physLBA_byte = (uint8_t)physLBA;
2493                                 *ptrLBA = physLBA_byte;
2494
2495                                 /*
2496                                  * Set flag that Direct Drive I/O is
2497                                  * being done.
2498                                  */
2499                                 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2500                         }
2501                 }
2502                 return;
2503         }
2504
2505         /*
2506          * Handle 10, 12 or 16 byte CDBs.
2507          */
2508         if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_10) ||
2509             (CDB[0] == WRITE_10) || (CDB[0] == READ_16) ||
2510             (CDB[0] == WRITE_16) || (CDB[0] == READ_12) ||
2511             (CDB[0] == WRITE_12))) {
2512                 /*
2513                  * For 16-byte CDB's, verify that the upper 4 bytes of the CDB
2514                  * are 0.  If not, this is accessing beyond 2TB so handle it in
2515                  * the else section.  10-byte and 12-byte CDB's are OK.
2516                  * FreeBSD sends very rare 12 byte READ/WRITE, but driver is 
2517                  * ready to accept 12byte CDB for Direct IOs.
2518                  */
2519                 if ((CDB[0] == READ_10 || CDB[0] == WRITE_10) ||
2520                     (CDB[0] == READ_12 || CDB[0] == WRITE_12) ||
2521                     !(CDB[2] | CDB[3] | CDB[4] | CDB[5])) {
2522                         /*
2523                          * Get the transfer size in blocks.
2524                          */
2525                         io_size = (cm->cm_length >> sc->DD_block_exponent);
2526
2527                         /*
2528                          * Get virtual LBA.  Point to correct lower 4 bytes of
2529                          * LBA in the CDB depending on command.
2530                          */
2531                         lba_idx = ((CDB[0] == READ_12) || 
2532                                 (CDB[0] == WRITE_12) ||
2533                                 (CDB[0] == READ_10) ||
2534                                 (CDB[0] == WRITE_10))? 2 : 6;
2535                         virtLBA = ((uint64_t)CDB[lba_idx] << 24) |
2536                             ((uint64_t)CDB[lba_idx + 1] << 16) |
2537                             ((uint64_t)CDB[lba_idx + 2] << 8) |
2538                             (uint64_t)CDB[lba_idx + 3];
2539
2540                         /*
2541                          * Check that LBA range for I/O does not exceed volume's
2542                          * MaxLBA.
2543                          */
2544                         if ((virtLBA + (uint64_t)io_size - 1) <=
2545                             sc->DD_max_lba) {
2546                                 /*
2547                                  * Check if the I/O crosses a stripe boundary.
2548                                  * If not, translate the virtual LBA to a
2549                                  * physical LBA and set the DevHandle for the
2550                                  * PhysDisk to be used.  If it does cross a
2551                                  * boundry, do normal I/O.  To get the right
2552                                  * DevHandle to use, get the map number for the
2553                                  * column, then use that map number to look up
2554                                  * the DevHandle of the PhysDisk.
2555                                  */
2556                                 stripe_offset = (uint32_t)virtLBA &
2557                                     (sc->DD_stripe_size - 1);
2558                                 if ((stripe_offset + io_size) <=
2559                                     sc->DD_stripe_size) {
2560                                         physLBA = (uint32_t)virtLBA >>
2561                                             sc->DD_stripe_exponent;
2562                                         stripe_unit = physLBA /
2563                                             sc->DD_num_phys_disks;
2564                                         column = physLBA %
2565                                             sc->DD_num_phys_disks;
2566                                         pIO_req->DevHandle =
2567                                             htole16(sc->DD_column_map[column].
2568                                             dev_handle);
2569                                         cm->cm_desc.SCSIIO.DevHandle =
2570                                             pIO_req->DevHandle;
2571
2572                                         physLBA = (stripe_unit <<
2573                                             sc->DD_stripe_exponent) +
2574                                             stripe_offset;
2575                                         ptrLBA =
2576                                             &pIO_req->CDB.CDB32[lba_idx];
2577                                         physLBA_byte = (uint8_t)(physLBA >> 24);
2578                                         *ptrLBA = physLBA_byte;
2579                                         ptrLBA =
2580                                             &pIO_req->CDB.CDB32[lba_idx + 1];
2581                                         physLBA_byte = (uint8_t)(physLBA >> 16);
2582                                         *ptrLBA = physLBA_byte;
2583                                         ptrLBA =
2584                                             &pIO_req->CDB.CDB32[lba_idx + 2];
2585                                         physLBA_byte = (uint8_t)(physLBA >> 8);
2586                                         *ptrLBA = physLBA_byte;
2587                                         ptrLBA =
2588                                             &pIO_req->CDB.CDB32[lba_idx + 3];
2589                                         physLBA_byte = (uint8_t)physLBA;
2590                                         *ptrLBA = physLBA_byte;
2591
2592                                         /*
2593                                          * Set flag that Direct Drive I/O is
2594                                          * being done.
2595                                          */
2596                                         cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2597                                 }
2598                         }
2599                 } else {
2600                         /*
2601                          * 16-byte CDB and the upper 4 bytes of the CDB are not
2602                          * 0.  Get the transfer size in blocks.
2603                          */
2604                         io_size = (cm->cm_length >> sc->DD_block_exponent);
2605
2606                         /*
2607                          * Get virtual LBA.
2608                          */
2609                         virtLBA = ((uint64_t)CDB[2] << 54) |
2610                             ((uint64_t)CDB[3] << 48) |
2611                             ((uint64_t)CDB[4] << 40) |
2612                             ((uint64_t)CDB[5] << 32) |
2613                             ((uint64_t)CDB[6] << 24) |
2614                             ((uint64_t)CDB[7] << 16) |
2615                             ((uint64_t)CDB[8] << 8) |
2616                             (uint64_t)CDB[9]; 
2617
2618                         /*
2619                          * Check that LBA range for I/O does not exceed volume's
2620                          * MaxLBA.
2621                          */
2622                         if ((virtLBA + (uint64_t)io_size - 1) <=
2623                             sc->DD_max_lba) {
2624                                 /*
2625                                  * Check if the I/O crosses a stripe boundary.
2626                                  * If not, translate the virtual LBA to a
2627                                  * physical LBA and set the DevHandle for the
2628                                  * PhysDisk to be used.  If it does cross a
2629                                  * boundry, do normal I/O.  To get the right
2630                                  * DevHandle to use, get the map number for the
2631                                  * column, then use that map number to look up
2632                                  * the DevHandle of the PhysDisk.
2633                                  */
2634                                 stripe_offset = (uint32_t)virtLBA &
2635                                     (sc->DD_stripe_size - 1);
2636                                 if ((stripe_offset + io_size) <=
2637                                     sc->DD_stripe_size) {
2638                                         physLBA = (uint32_t)(virtLBA >>
2639                                             sc->DD_stripe_exponent);
2640                                         stripe_unit = physLBA /
2641                                             sc->DD_num_phys_disks;
2642                                         column = physLBA %
2643                                             sc->DD_num_phys_disks;
2644                                         pIO_req->DevHandle =
2645                                             htole16(sc->DD_column_map[column].
2646                                             dev_handle);
2647                                         cm->cm_desc.SCSIIO.DevHandle =
2648                                             pIO_req->DevHandle;
2649
2650                                         physLBA = (stripe_unit <<
2651                                             sc->DD_stripe_exponent) +
2652                                             stripe_offset;
2653
2654                                         /*
2655                                          * Set upper 4 bytes of LBA to 0.  We
2656                                          * assume that the phys disks are less
2657                                          * than 2 TB's in size.  Then, set the
2658                                          * lower 4 bytes.
2659                                          */
2660                                         pIO_req->CDB.CDB32[2] = 0;
2661                                         pIO_req->CDB.CDB32[3] = 0;
2662                                         pIO_req->CDB.CDB32[4] = 0;
2663                                         pIO_req->CDB.CDB32[5] = 0;
2664                                         ptrLBA = &pIO_req->CDB.CDB32[6];
2665                                         physLBA_byte = (uint8_t)(physLBA >> 24);
2666                                         *ptrLBA = physLBA_byte;
2667                                         ptrLBA = &pIO_req->CDB.CDB32[7];
2668                                         physLBA_byte = (uint8_t)(physLBA >> 16);
2669                                         *ptrLBA = physLBA_byte;
2670                                         ptrLBA = &pIO_req->CDB.CDB32[8];
2671                                         physLBA_byte = (uint8_t)(physLBA >> 8);
2672                                         *ptrLBA = physLBA_byte;
2673                                         ptrLBA = &pIO_req->CDB.CDB32[9];
2674                                         physLBA_byte = (uint8_t)physLBA;
2675                                         *ptrLBA = physLBA_byte;
2676
2677                                         /*
2678                                          * Set flag that Direct Drive I/O is
2679                                          * being done.
2680                                          */
2681                                         cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2682                                 }
2683                         }
2684                 }
2685         }
2686 }
2687
2688 #if __FreeBSD_version >= 900026
2689 static void
2690 mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm)
2691 {
2692         MPI2_SMP_PASSTHROUGH_REPLY *rpl;
2693         MPI2_SMP_PASSTHROUGH_REQUEST *req;
2694         uint64_t sasaddr;
2695         union ccb *ccb;
2696
2697         ccb = cm->cm_complete_data;
2698
2699         /*
2700          * Currently there should be no way we can hit this case.  It only
2701          * happens when we have a failure to allocate chain frames, and SMP
2702          * commands require two S/G elements only.  That should be handled
2703          * in the standard request size.
2704          */
2705         if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2706                 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x on SMP request!\n",
2707                            __func__, cm->cm_flags);
2708                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2709                 goto bailout;
2710         }
2711
2712         rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
2713         if (rpl == NULL) {
2714                 mps_dprint(sc, MPS_ERROR, "%s: NULL cm_reply!\n", __func__);
2715                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2716                 goto bailout;
2717         }
2718
2719         req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2720         sasaddr = le32toh(req->SASAddress.Low);
2721         sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
2722
2723         if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
2724             MPI2_IOCSTATUS_SUCCESS ||
2725             rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
2726                 mps_dprint(sc, MPS_XINFO, "%s: IOCStatus %04x SASStatus %02x\n",
2727                     __func__, le16toh(rpl->IOCStatus), rpl->SASStatus);
2728                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2729                 goto bailout;
2730         }
2731
2732         mps_dprint(sc, MPS_XINFO, "%s: SMP request to SAS address "
2733                    "%#jx completed successfully\n", __func__,
2734                    (uintmax_t)sasaddr);
2735
2736         if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
2737                 ccb->ccb_h.status = CAM_REQ_CMP;
2738         else
2739                 ccb->ccb_h.status = CAM_SMP_STATUS_ERROR;
2740
2741 bailout:
2742         /*
2743          * We sync in both directions because we had DMAs in the S/G list
2744          * in both directions.
2745          */
2746         bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2747                         BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2748         bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2749         mps_free_command(sc, cm);
2750         xpt_done(ccb);
2751 }
2752
2753 static void
2754 mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
2755 {
2756         struct mps_command *cm;
2757         uint8_t *request, *response;
2758         MPI2_SMP_PASSTHROUGH_REQUEST *req;
2759         struct mps_softc *sc;
2760         struct sglist *sg;
2761         int error;
2762
2763         sc = sassc->sc;
2764         sg = NULL;
2765         error = 0;
2766
2767         /*
2768          * XXX We don't yet support physical addresses here.
2769          */
2770         switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
2771         case CAM_DATA_PADDR:
2772         case CAM_DATA_SG_PADDR:
2773                 mps_dprint(sc, MPS_ERROR,
2774                            "%s: physical addresses not supported\n", __func__);
2775                 ccb->ccb_h.status = CAM_REQ_INVALID;
2776                 xpt_done(ccb);
2777                 return;
2778         case CAM_DATA_SG:
2779                 /*
2780                  * The chip does not support more than one buffer for the
2781                  * request or response.
2782                  */
2783                 if ((ccb->smpio.smp_request_sglist_cnt > 1)
2784                   || (ccb->smpio.smp_response_sglist_cnt > 1)) {
2785                         mps_dprint(sc, MPS_ERROR,
2786                                    "%s: multiple request or response "
2787                                    "buffer segments not supported for SMP\n",
2788                                    __func__);
2789                         ccb->ccb_h.status = CAM_REQ_INVALID;
2790                         xpt_done(ccb);
2791                         return;
2792                 }
2793
2794                 /*
2795                  * The CAM_SCATTER_VALID flag was originally implemented
2796                  * for the XPT_SCSI_IO CCB, which only has one data pointer.
2797                  * We have two.  So, just take that flag to mean that we
2798                  * might have S/G lists, and look at the S/G segment count
2799                  * to figure out whether that is the case for each individual
2800                  * buffer.
2801                  */
2802                 if (ccb->smpio.smp_request_sglist_cnt != 0) {
2803                         bus_dma_segment_t *req_sg;
2804
2805                         req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2806                         request = (uint8_t *)req_sg[0].ds_addr;
2807                 } else
2808                         request = ccb->smpio.smp_request;
2809
2810                 if (ccb->smpio.smp_response_sglist_cnt != 0) {
2811                         bus_dma_segment_t *rsp_sg;
2812
2813                         rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2814                         response = (uint8_t *)rsp_sg[0].ds_addr;
2815                 } else
2816                         response = ccb->smpio.smp_response;
2817                 break;
2818         case CAM_DATA_VADDR:
2819                 request = ccb->smpio.smp_request;
2820                 response = ccb->smpio.smp_response;
2821                 break;
2822         default:
2823                 ccb->ccb_h.status = CAM_REQ_INVALID;
2824                 xpt_done(ccb);
2825                 return;
2826         }
2827
2828         cm = mps_alloc_command(sc);
2829         if (cm == NULL) {
2830                 mps_dprint(sc, MPS_ERROR,
2831                     "%s: cannot allocate command\n", __func__);
2832                 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2833                 xpt_done(ccb);
2834                 return;
2835         }
2836
2837         req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2838         bzero(req, sizeof(*req));
2839         req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2840
2841         /* Allow the chip to use any route to this SAS address. */
2842         req->PhysicalPort = 0xff;
2843
2844         req->RequestDataLength = htole16(ccb->smpio.smp_request_len);
2845         req->SGLFlags = 
2846             MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
2847
2848         mps_dprint(sc, MPS_XINFO, "%s: sending SMP request to SAS "
2849             "address %#jx\n", __func__, (uintmax_t)sasaddr);
2850
2851         mpi_init_sge(cm, req, &req->SGL);
2852
2853         /*
2854          * Set up a uio to pass into mps_map_command().  This allows us to
2855          * do one map command, and one busdma call in there.
2856          */
2857         cm->cm_uio.uio_iov = cm->cm_iovec;
2858         cm->cm_uio.uio_iovcnt = 2;
2859         cm->cm_uio.uio_segflg = UIO_SYSSPACE;
2860
2861         /*
2862          * The read/write flag isn't used by busdma, but set it just in
2863          * case.  This isn't exactly accurate, either, since we're going in
2864          * both directions.
2865          */
2866         cm->cm_uio.uio_rw = UIO_WRITE;
2867
2868         cm->cm_iovec[0].iov_base = request;
2869         cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength);
2870         cm->cm_iovec[1].iov_base = response;
2871         cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
2872
2873         cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
2874                                cm->cm_iovec[1].iov_len;
2875
2876         /*
2877          * Trigger a warning message in mps_data_cb() for the user if we
2878          * wind up exceeding two S/G segments.  The chip expects one
2879          * segment for the request and another for the response.
2880          */
2881         cm->cm_max_segs = 2;
2882
2883         cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2884         cm->cm_complete = mpssas_smpio_complete;
2885         cm->cm_complete_data = ccb;
2886
2887         /*
2888          * Tell the mapping code that we're using a uio, and that this is
2889          * an SMP passthrough request.  There is a little special-case
2890          * logic there (in mps_data_cb()) to handle the bidirectional
2891          * transfer.  
2892          */
2893         cm->cm_flags |= MPS_CM_FLAGS_USE_UIO | MPS_CM_FLAGS_SMP_PASS |
2894                         MPS_CM_FLAGS_DATAIN | MPS_CM_FLAGS_DATAOUT;
2895
2896         /* The chip data format is little endian. */
2897         req->SASAddress.High = htole32(sasaddr >> 32);
2898         req->SASAddress.Low = htole32(sasaddr);
2899
2900         /*
2901          * XXX Note that we don't have a timeout/abort mechanism here.
2902          * From the manual, it looks like task management requests only
2903          * work for SCSI IO and SATA passthrough requests.  We may need to
2904          * have a mechanism to retry requests in the event of a chip reset
2905          * at least.  Hopefully the chip will insure that any errors short
2906          * of that are relayed back to the driver.
2907          */
2908         error = mps_map_command(sc, cm);
2909         if ((error != 0) && (error != EINPROGRESS)) {
2910                 mps_dprint(sc, MPS_ERROR,
2911                            "%s: error %d returned from mps_map_command()\n",
2912                            __func__, error);
2913                 goto bailout_error;
2914         }
2915
2916         return;
2917
2918 bailout_error:
2919         mps_free_command(sc, cm);
2920         ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2921         xpt_done(ccb);
2922         return;
2923
2924 }
2925
2926 static void
2927 mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb)
2928 {
2929         struct mps_softc *sc;
2930         struct mpssas_target *targ;
2931         uint64_t sasaddr = 0;
2932
2933         sc = sassc->sc;
2934
2935         /*
2936          * Make sure the target exists.
2937          */
2938         targ = &sassc->targets[ccb->ccb_h.target_id];
2939         if (targ->handle == 0x0) {
2940                 mps_dprint(sc, MPS_ERROR,
2941                            "%s: target %d does not exist!\n", __func__,
2942                            ccb->ccb_h.target_id);
2943                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2944                 xpt_done(ccb);
2945                 return;
2946         }
2947
2948         /*
2949          * If this device has an embedded SMP target, we'll talk to it
2950          * directly.
2951          * figure out what the expander's address is.
2952          */
2953         if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
2954                 sasaddr = targ->sasaddr;
2955
2956         /*
2957          * If we don't have a SAS address for the expander yet, try
2958          * grabbing it from the page 0x83 information cached in the
2959          * transport layer for this target.  LSI expanders report the
2960          * expander SAS address as the port-associated SAS address in
2961          * Inquiry VPD page 0x83.  Maxim expanders don't report it in page
2962          * 0x83.
2963          *
2964          * XXX KDM disable this for now, but leave it commented out so that
2965          * it is obvious that this is another possible way to get the SAS
2966          * address.
2967          *
2968          * The parent handle method below is a little more reliable, and
2969          * the other benefit is that it works for devices other than SES
2970          * devices.  So you can send a SMP request to a da(4) device and it
2971          * will get routed to the expander that device is attached to.
2972          * (Assuming the da(4) device doesn't contain an SMP target...)
2973          */
2974 #if 0
2975         if (sasaddr == 0)
2976                 sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
2977 #endif
2978
2979         /*
2980          * If we still don't have a SAS address for the expander, look for
2981          * the parent device of this device, which is probably the expander.
2982          */
2983         if (sasaddr == 0) {
2984 #ifdef OLD_MPS_PROBE
2985                 struct mpssas_target *parent_target;
2986 #endif
2987
2988                 if (targ->parent_handle == 0x0) {
2989                         mps_dprint(sc, MPS_ERROR,
2990                                    "%s: handle %d does not have a valid "
2991                                    "parent handle!\n", __func__, targ->handle);
2992                         ccb->ccb_h.status = CAM_REQ_INVALID;
2993                         goto bailout;
2994                 }
2995 #ifdef OLD_MPS_PROBE
2996                 parent_target = mpssas_find_target_by_handle(sassc, 0,
2997                         targ->parent_handle);
2998
2999                 if (parent_target == NULL) {
3000                         mps_dprint(sc, MPS_ERROR,
3001                                    "%s: handle %d does not have a valid "
3002                                    "parent target!\n", __func__, targ->handle);
3003                         ccb->ccb_h.status = CAM_REQ_INVALID;
3004                         goto bailout;
3005                 }
3006
3007                 if ((parent_target->devinfo &
3008                      MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3009                         mps_dprint(sc, MPS_ERROR,
3010                                    "%s: handle %d parent %d does not "
3011                                    "have an SMP target!\n", __func__,
3012                                    targ->handle, parent_target->handle);
3013                         ccb->ccb_h.status = CAM_REQ_INVALID;
3014                         goto bailout;
3015
3016                 }
3017
3018                 sasaddr = parent_target->sasaddr;
3019 #else /* OLD_MPS_PROBE */
3020                 if ((targ->parent_devinfo &
3021                      MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3022                         mps_dprint(sc, MPS_ERROR,
3023                                    "%s: handle %d parent %d does not "
3024                                    "have an SMP target!\n", __func__,
3025                                    targ->handle, targ->parent_handle);
3026                         ccb->ccb_h.status = CAM_REQ_INVALID;
3027                         goto bailout;
3028
3029                 }
3030                 if (targ->parent_sasaddr == 0x0) {
3031                         mps_dprint(sc, MPS_ERROR,
3032                                    "%s: handle %d parent handle %d does "
3033                                    "not have a valid SAS address!\n",
3034                                    __func__, targ->handle, targ->parent_handle);
3035                         ccb->ccb_h.status = CAM_REQ_INVALID;
3036                         goto bailout;
3037                 }
3038
3039                 sasaddr = targ->parent_sasaddr;
3040 #endif /* OLD_MPS_PROBE */
3041
3042         }
3043
3044         if (sasaddr == 0) {
3045                 mps_dprint(sc, MPS_INFO,
3046                            "%s: unable to find SAS address for handle %d\n",
3047                            __func__, targ->handle);
3048                 ccb->ccb_h.status = CAM_REQ_INVALID;
3049                 goto bailout;
3050         }
3051         mpssas_send_smpcmd(sassc, ccb, sasaddr);
3052
3053         return;
3054
3055 bailout:
3056         xpt_done(ccb);
3057
3058 }
3059 #endif //__FreeBSD_version >= 900026
3060
3061 static void
3062 mpssas_action_resetdev(struct mpssas_softc *sassc, union ccb *ccb)
3063 {
3064         MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3065         struct mps_softc *sc;
3066         struct mps_command *tm;
3067         struct mpssas_target *targ;
3068
3069         MPS_FUNCTRACE(sassc->sc);
3070         mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
3071
3072         sc = sassc->sc;
3073         tm = mps_alloc_command(sc);
3074         if (tm == NULL) {
3075                 mps_dprint(sc, MPS_ERROR,
3076                     "comand alloc failure in mpssas_action_resetdev\n");
3077                 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3078                 xpt_done(ccb);
3079                 return;
3080         }
3081
3082         targ = &sassc->targets[ccb->ccb_h.target_id];
3083         req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3084         req->DevHandle = htole16(targ->handle);
3085         req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3086         req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3087
3088         /* SAS Hard Link Reset / SATA Link Reset */
3089         req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3090
3091         tm->cm_data = NULL;
3092         tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3093         tm->cm_complete = mpssas_resetdev_complete;
3094         tm->cm_complete_data = ccb;
3095         tm->cm_targ = targ;
3096         mps_map_command(sc, tm);
3097 }
3098
3099 static void
3100 mpssas_resetdev_complete(struct mps_softc *sc, struct mps_command *tm)
3101 {
3102         MPI2_SCSI_TASK_MANAGE_REPLY *resp;
3103         union ccb *ccb;
3104
3105         MPS_FUNCTRACE(sc);
3106         mtx_assert(&sc->mps_mtx, MA_OWNED);
3107
3108         resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
3109         ccb = tm->cm_complete_data;
3110
3111         /*
3112          * Currently there should be no way we can hit this case.  It only
3113          * happens when we have a failure to allocate chain frames, and
3114          * task management commands don't have S/G lists.
3115          */
3116         if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3117                 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3118
3119                 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3120
3121                 mps_dprint(sc, MPS_ERROR,
3122                            "%s: cm_flags = %#x for reset of handle %#04x! "
3123                            "This should not happen!\n", __func__, tm->cm_flags,
3124                            req->DevHandle);
3125                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3126                 goto bailout;
3127         }
3128
3129         mps_dprint(sc, MPS_XINFO,
3130             "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n", __func__,
3131             le16toh(resp->IOCStatus), le32toh(resp->ResponseCode));
3132
3133         if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
3134                 ccb->ccb_h.status = CAM_REQ_CMP;
3135                 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
3136                     CAM_LUN_WILDCARD);
3137         }
3138         else
3139                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3140
3141 bailout:
3142
3143         mpssas_free_tm(sc, tm);
3144         xpt_done(ccb);
3145 }
3146
3147 static void
3148 mpssas_poll(struct cam_sim *sim)
3149 {
3150         struct mpssas_softc *sassc;
3151
3152         sassc = cam_sim_softc(sim);
3153
3154         if (sassc->sc->mps_debug & MPS_TRACE) {
3155                 /* frequent debug messages during a panic just slow
3156                  * everything down too much.
3157                  */
3158                 mps_printf(sassc->sc, "%s clearing MPS_TRACE\n", __func__);
3159                 sassc->sc->mps_debug &= ~MPS_TRACE;
3160         }
3161
3162         mps_intr_locked(sassc->sc);
3163 }
3164
3165 static void
3166 mpssas_async(void *callback_arg, uint32_t code, struct cam_path *path,
3167              void *arg)
3168 {
3169         struct mps_softc *sc;
3170
3171         sc = (struct mps_softc *)callback_arg;
3172
3173         switch (code) {
3174 #if (__FreeBSD_version >= 1000006) || \
3175     ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000))
3176         case AC_ADVINFO_CHANGED: {
3177                 struct mpssas_target *target;
3178                 struct mpssas_softc *sassc;
3179                 struct scsi_read_capacity_data_long rcap_buf;
3180                 struct ccb_dev_advinfo cdai;
3181                 struct mpssas_lun *lun;
3182                 lun_id_t lunid;
3183                 int found_lun;
3184                 uintptr_t buftype;
3185
3186                 buftype = (uintptr_t)arg;
3187
3188                 found_lun = 0;
3189                 sassc = sc->sassc;
3190
3191                 /*
3192                  * We're only interested in read capacity data changes.
3193                  */
3194                 if (buftype != CDAI_TYPE_RCAPLONG)
3195                         break;
3196
3197                 /*
3198                  * We should have a handle for this, but check to make sure.
3199                  */
3200                 target = &sassc->targets[xpt_path_target_id(path)];
3201                 if (target->handle == 0)
3202                         break;
3203
3204                 lunid = xpt_path_lun_id(path);
3205
3206                 SLIST_FOREACH(lun, &target->luns, lun_link) {
3207                         if (lun->lun_id == lunid) {
3208                                 found_lun = 1;
3209                                 break;
3210                         }
3211                 }
3212
3213                 if (found_lun == 0) {
3214                         lun = malloc(sizeof(struct mpssas_lun), M_MPT2,
3215                                      M_NOWAIT | M_ZERO);
3216                         if (lun == NULL) {
3217                                 mps_dprint(sc, MPS_ERROR, "Unable to alloc "
3218                                            "LUN for EEDP support.\n");
3219                                 break;
3220                         }
3221                         lun->lun_id = lunid;
3222                         SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3223                 }
3224
3225                 bzero(&rcap_buf, sizeof(rcap_buf));
3226                 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
3227                 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3228                 cdai.ccb_h.flags = CAM_DIR_IN;
3229                 cdai.buftype = CDAI_TYPE_RCAPLONG;
3230                 cdai.flags = 0;
3231                 cdai.bufsiz = sizeof(rcap_buf);
3232                 cdai.buf = (uint8_t *)&rcap_buf;
3233                 xpt_action((union ccb *)&cdai);
3234                 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3235                         cam_release_devq(cdai.ccb_h.path,
3236                                          0, 0, 0, FALSE);
3237
3238                 if (((cdai.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
3239                  && (rcap_buf.prot & SRC16_PROT_EN)) {
3240                         lun->eedp_formatted = TRUE;
3241                         lun->eedp_block_size = scsi_4btoul(rcap_buf.length);
3242                 } else {
3243                         lun->eedp_formatted = FALSE;
3244                         lun->eedp_block_size = 0;
3245                 }
3246                 break;
3247         }
3248 #else
3249         case AC_FOUND_DEVICE: {
3250                 struct ccb_getdev *cgd;
3251
3252                 cgd = arg;
3253                 mpssas_check_eedp(sc, path, cgd);
3254                 break;
3255         }
3256 #endif
3257         default:
3258                 break;
3259         }
3260 }
3261
3262 #if (__FreeBSD_version < 901503) || \
3263     ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006))
3264 static void
3265 mpssas_check_eedp(struct mps_softc *sc, struct cam_path *path,
3266                   struct ccb_getdev *cgd)
3267 {
3268         struct mpssas_softc *sassc = sc->sassc;
3269         struct ccb_scsiio *csio;
3270         struct scsi_read_capacity_16 *scsi_cmd;
3271         struct scsi_read_capacity_eedp *rcap_buf;
3272         path_id_t pathid;
3273         target_id_t targetid;
3274         lun_id_t lunid;
3275         union ccb *ccb;
3276         struct cam_path *local_path;
3277         struct mpssas_target *target;
3278         struct mpssas_lun *lun;
3279         uint8_t found_lun;
3280         char path_str[64];
3281
3282         sassc = sc->sassc;
3283         pathid = cam_sim_path(sassc->sim);
3284         targetid = xpt_path_target_id(path);
3285         lunid = xpt_path_lun_id(path);
3286
3287         target = &sassc->targets[targetid];
3288         if (target->handle == 0x0)
3289                 return;
3290
3291         /*
3292          * Determine if the device is EEDP capable.
3293          *
3294          * If this flag is set in the inquiry data, 
3295          * the device supports protection information,
3296          * and must support the 16 byte read
3297          * capacity command, otherwise continue without
3298          * sending read cap 16
3299          */
3300         if ((cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) == 0)
3301                 return;
3302
3303         /*
3304          * Issue a READ CAPACITY 16 command.  This info
3305          * is used to determine if the LUN is formatted
3306          * for EEDP support.
3307          */
3308         ccb = xpt_alloc_ccb_nowait();
3309         if (ccb == NULL) {
3310                 mps_dprint(sc, MPS_ERROR, "Unable to alloc CCB "
3311                     "for EEDP support.\n");
3312                 return;
3313         }
3314
3315         if (xpt_create_path(&local_path, xpt_periph,
3316             pathid, targetid, lunid) != CAM_REQ_CMP) {
3317                 mps_dprint(sc, MPS_ERROR, "Unable to create "
3318                     "path for EEDP support\n");
3319                 xpt_free_ccb(ccb);
3320                 return;
3321         }
3322
3323         /*
3324          * If LUN is already in list, don't create a new
3325          * one.
3326          */
3327         found_lun = FALSE;
3328         SLIST_FOREACH(lun, &target->luns, lun_link) {
3329                 if (lun->lun_id == lunid) {
3330                         found_lun = TRUE;
3331                         break;
3332                 }
3333         }
3334         if (!found_lun) {
3335                 lun = malloc(sizeof(struct mpssas_lun), M_MPT2,
3336                     M_NOWAIT | M_ZERO);
3337                 if (lun == NULL) {
3338                         mps_dprint(sc, MPS_ERROR,
3339                             "Unable to alloc LUN for EEDP support.\n");
3340                         xpt_free_path(local_path);
3341                         xpt_free_ccb(ccb);
3342                         return;
3343                 }
3344                 lun->lun_id = lunid;
3345                 SLIST_INSERT_HEAD(&target->luns, lun,
3346                     lun_link);
3347         }
3348
3349         xpt_path_string(local_path, path_str, sizeof(path_str));
3350         mps_dprint(sc, MPS_INFO, "Sending read cap: path %s handle %d\n",
3351             path_str, target->handle);
3352
3353         /*
3354          * Issue a READ CAPACITY 16 command for the LUN.
3355          * The mpssas_read_cap_done function will load
3356          * the read cap info into the LUN struct.
3357          */
3358         rcap_buf = malloc(sizeof(struct scsi_read_capacity_eedp),
3359             M_MPT2, M_NOWAIT | M_ZERO);
3360         if (rcap_buf == NULL) {
3361                 mps_dprint(sc, MPS_FAULT,
3362                     "Unable to alloc read capacity buffer for EEDP support.\n");
3363                 xpt_free_path(ccb->ccb_h.path);
3364                 xpt_free_ccb(ccb);
3365                 return;
3366         }
3367         xpt_setup_ccb(&ccb->ccb_h, local_path, CAM_PRIORITY_XPT);
3368         csio = &ccb->csio;
3369         csio->ccb_h.func_code = XPT_SCSI_IO;
3370         csio->ccb_h.flags = CAM_DIR_IN;
3371         csio->ccb_h.retry_count = 4;    
3372         csio->ccb_h.cbfcnp = mpssas_read_cap_done;
3373         csio->ccb_h.timeout = 60000;
3374         csio->data_ptr = (uint8_t *)rcap_buf;
3375         csio->dxfer_len = sizeof(struct scsi_read_capacity_eedp);
3376         csio->sense_len = MPS_SENSE_LEN;
3377         csio->cdb_len = sizeof(*scsi_cmd);
3378         csio->tag_action = MSG_SIMPLE_Q_TAG;
3379
3380         scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
3381         bzero(scsi_cmd, sizeof(*scsi_cmd));
3382         scsi_cmd->opcode = 0x9E;
3383         scsi_cmd->service_action = SRC16_SERVICE_ACTION;
3384         ((uint8_t *)scsi_cmd)[13] = sizeof(struct scsi_read_capacity_eedp);
3385
3386         ccb->ccb_h.ppriv_ptr1 = sassc;
3387         xpt_action(ccb);
3388 }
3389
3390 static void
3391 mpssas_read_cap_done(struct cam_periph *periph, union ccb *done_ccb)
3392 {
3393         struct mpssas_softc *sassc;
3394         struct mpssas_target *target;
3395         struct mpssas_lun *lun;
3396         struct scsi_read_capacity_eedp *rcap_buf;
3397
3398         if (done_ccb == NULL)
3399                 return;
3400         
3401         /* Driver need to release devq, it Scsi command is
3402          * generated by driver internally.
3403          * Currently there is a single place where driver
3404          * calls scsi command internally. In future if driver
3405          * calls more scsi command internally, it needs to release
3406          * devq internally, since those command will not go back to
3407          * cam_periph.
3408          */
3409         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) ) {
3410                 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3411                 xpt_release_devq(done_ccb->ccb_h.path,
3412                                 /*count*/ 1, /*run_queue*/TRUE);
3413         }
3414
3415         rcap_buf = (struct scsi_read_capacity_eedp *)done_ccb->csio.data_ptr;
3416
3417         /*
3418          * Get the LUN ID for the path and look it up in the LUN list for the
3419          * target.
3420          */
3421         sassc = (struct mpssas_softc *)done_ccb->ccb_h.ppriv_ptr1;
3422         target = &sassc->targets[done_ccb->ccb_h.target_id];
3423         SLIST_FOREACH(lun, &target->luns, lun_link) {
3424                 if (lun->lun_id != done_ccb->ccb_h.target_lun)
3425                         continue;
3426
3427                 /*
3428                  * Got the LUN in the target's LUN list.  Fill it in
3429                  * with EEDP info.  If the READ CAP 16 command had some
3430                  * SCSI error (common if command is not supported), mark
3431                  * the lun as not supporting EEDP and set the block size
3432                  * to 0.
3433                  */
3434                 if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
3435                  || (done_ccb->csio.scsi_status != SCSI_STATUS_OK)) {
3436                         lun->eedp_formatted = FALSE;
3437                         lun->eedp_block_size = 0;
3438                         break;
3439                 }
3440
3441                 if (rcap_buf->protect & 0x01) {
3442                         mps_dprint(sassc->sc, MPS_INFO, "LUN %d for "
3443                             "target ID %d is formatted for EEDP "
3444                             "support.\n", done_ccb->ccb_h.target_lun,
3445                             done_ccb->ccb_h.target_id);
3446                         lun->eedp_formatted = TRUE;
3447                         lun->eedp_block_size = scsi_4btoul(rcap_buf->length);
3448                 }
3449                 break;
3450         }
3451
3452         // Finished with this CCB and path.
3453         free(rcap_buf, M_MPT2);
3454         xpt_free_path(done_ccb->ccb_h.path);
3455         xpt_free_ccb(done_ccb);
3456 }
3457 #endif /* (__FreeBSD_version < 901503) || \
3458           ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006)) */
3459
3460 int
3461 mpssas_startup(struct mps_softc *sc)
3462 {
3463         struct mpssas_softc *sassc;
3464
3465         /*
3466          * Send the port enable message and set the wait_for_port_enable flag.
3467          * This flag helps to keep the simq frozen until all discovery events
3468          * are processed.
3469          */
3470         sassc = sc->sassc;
3471         mpssas_startup_increment(sassc);
3472         sc->wait_for_port_enable = 1;
3473         mpssas_send_portenable(sc);
3474         return (0);
3475 }
3476
3477 static int
3478 mpssas_send_portenable(struct mps_softc *sc)
3479 {
3480         MPI2_PORT_ENABLE_REQUEST *request;
3481         struct mps_command *cm;
3482
3483         MPS_FUNCTRACE(sc);
3484
3485         if ((cm = mps_alloc_command(sc)) == NULL)
3486                 return (EBUSY);
3487         request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req;
3488         request->Function = MPI2_FUNCTION_PORT_ENABLE;
3489         request->MsgFlags = 0;
3490         request->VP_ID = 0;
3491         cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3492         cm->cm_complete = mpssas_portenable_complete;
3493         cm->cm_data = NULL;
3494         cm->cm_sge = NULL;
3495
3496         mps_map_command(sc, cm);
3497         mps_dprint(sc, MPS_XINFO, 
3498             "mps_send_portenable finished cm %p req %p complete %p\n",
3499             cm, cm->cm_req, cm->cm_complete);
3500         return (0);
3501 }
3502
3503 static void
3504 mpssas_portenable_complete(struct mps_softc *sc, struct mps_command *cm)
3505 {
3506         MPI2_PORT_ENABLE_REPLY *reply;
3507         struct mpssas_softc *sassc;
3508
3509         MPS_FUNCTRACE(sc);
3510         sassc = sc->sassc;
3511
3512         /*
3513          * Currently there should be no way we can hit this case.  It only
3514          * happens when we have a failure to allocate chain frames, and
3515          * port enable commands don't have S/G lists.
3516          */
3517         if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3518                 mps_dprint(sc, MPS_ERROR, "%s: cm_flags = %#x for port enable! "
3519                            "This should not happen!\n", __func__, cm->cm_flags);
3520         }
3521
3522         reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply;
3523         if (reply == NULL)
3524                 mps_dprint(sc, MPS_FAULT, "Portenable NULL reply\n");
3525         else if (le16toh(reply->IOCStatus & MPI2_IOCSTATUS_MASK) !=
3526             MPI2_IOCSTATUS_SUCCESS)
3527                 mps_dprint(sc, MPS_FAULT, "Portenable failed\n");
3528
3529         mps_free_command(sc, cm);
3530         if (sc->mps_ich.ich_arg != NULL) {
3531                 mps_dprint(sc, MPS_XINFO, "disestablish config intrhook\n");
3532                 config_intrhook_disestablish(&sc->mps_ich);
3533                 sc->mps_ich.ich_arg = NULL;
3534         }
3535
3536         /*
3537          * Get WarpDrive info after discovery is complete but before the scan
3538          * starts.  At this point, all devices are ready to be exposed to the
3539          * OS.  If devices should be hidden instead, take them out of the
3540          * 'targets' array before the scan.  The devinfo for a disk will have
3541          * some info and a volume's will be 0.  Use that to remove disks.
3542          */
3543         mps_wd_config_pages(sc);
3544
3545         /*
3546          * Done waiting for port enable to complete.  Decrement the refcount.
3547          * If refcount is 0, discovery is complete and a rescan of the bus can
3548          * take place.  Since the simq was explicitly frozen before port
3549          * enable, it must be explicitly released here to keep the
3550          * freeze/release count in sync.
3551          */
3552         sc->wait_for_port_enable = 0;
3553         sc->port_enable_complete = 1;
3554         wakeup(&sc->port_enable_complete);
3555         mpssas_startup_decrement(sassc);
3556         xpt_release_simq(sassc->sim, 1);
3557 }
3558