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