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