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