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