Deleted Added
full compact
isp_freebsd.c (290779) isp_freebsd.c (290785)
1/*-
2 * Copyright (c) 1997-2009 by Matthew Jacob
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29 */
30#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1997-2009 by Matthew Jacob
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Platform (FreeBSD) dependent common attachment code for Qlogic adapters.
29 */
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: stable/10/sys/dev/isp/isp_freebsd.c 290779 2015-11-13 19:32:17Z mav $");
31__FBSDID("$FreeBSD: stable/10/sys/dev/isp/isp_freebsd.c 290785 2015-11-13 19:42:55Z mav $");
32
33#include <dev/isp/isp_freebsd.h>
34#include <sys/unistd.h>
35#include <sys/kthread.h>
36#include <sys/conf.h>
37#include <sys/module.h>
38#include <sys/ioccom.h>
39#include <dev/isp/isp_ioctl.h>
40#include <sys/devicestat.h>
41#include <cam/cam_periph.h>
42#include <cam/cam_xpt_periph.h>
43
44#if __FreeBSD_version < 800002
45#define THREAD_CREATE kthread_create
46#else
47#define THREAD_CREATE kproc_create
48#endif
49
50MODULE_VERSION(isp, 1);
51MODULE_DEPEND(isp, cam, 1, 1, 1);
52int isp_announced = 0;
53int isp_fabric_hysteresis = 5;
54int isp_loop_down_limit = 60; /* default loop down limit */
55int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */
56int isp_gone_device_time = 30; /* grace time before reporting device lost */
57int isp_autoconfig = 1; /* automatically attach/detach devices */
58static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
59
60static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
61static d_ioctl_t ispioctl;
62static void isp_intr_enable(void *);
63static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
64static void isp_poll(struct cam_sim *);
65static timeout_t isp_watchdog;
66static timeout_t isp_gdt;
67static task_fn_t isp_gdt_task;
68static timeout_t isp_ldt;
69static task_fn_t isp_ldt_task;
70static void isp_kthread(void *);
71static void isp_action(struct cam_sim *, union ccb *);
72#ifdef ISP_INTERNAL_TARGET
73static void isp_target_thread_pi(void *);
74static void isp_target_thread_fc(void *);
75#endif
76static int isp_timer_count;
77static void isp_timer(void *);
78
79static struct cdevsw isp_cdevsw = {
80 .d_version = D_VERSION,
81 .d_ioctl = ispioctl,
82 .d_name = "isp",
83};
84
85static int
86isp_role_sysctl(SYSCTL_HANDLER_ARGS)
87{
88 ispsoftc_t *isp = (ispsoftc_t *)arg1;
89 int chan = arg2;
90 int error, old, value;
91
92 value = FCPARAM(isp, chan)->role;
93
94 error = sysctl_handle_int(oidp, &value, 0, req);
95 if ((error != 0) || (req->newptr == NULL))
96 return (error);
97
98 if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
99 return (EINVAL);
100
101 ISP_LOCK(isp);
102 old = FCPARAM(isp, chan)->role;
103
104 /* We don't allow target mode switch from here. */
105 value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
106
107 /* If nothing has changed -- we are done. */
108 if (value == old) {
109 ISP_UNLOCK(isp);
110 return (0);
111 }
112
113 /* Actually change the role. */
114 error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
115 ISP_UNLOCK(isp);
116 return (error);
117}
118
119static int
120isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
121{
122 struct ccb_setasync csa;
123 struct cam_sim *sim;
124 struct cam_path *path;
125
126 /*
127 * Construct our SIM entry.
128 */
129 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
130
131 if (sim == NULL) {
132 return (ENOMEM);
133 }
134
135 ISP_LOCK(isp);
136 if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
137 ISP_UNLOCK(isp);
138 cam_sim_free(sim, FALSE);
139 return (EIO);
140 }
141 ISP_UNLOCK(isp);
142 if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
143 ISP_LOCK(isp);
144 xpt_bus_deregister(cam_sim_path(sim));
145 ISP_UNLOCK(isp);
146 cam_sim_free(sim, FALSE);
147 return (ENXIO);
148 }
149 xpt_setup_ccb(&csa.ccb_h, path, 5);
150 csa.ccb_h.func_code = XPT_SASYNC_CB;
151 csa.event_enable = AC_LOST_DEVICE;
152 csa.callback = isp_cam_async;
153 csa.callback_arg = sim;
154
155 ISP_LOCK(isp);
156 xpt_action((union ccb *)&csa);
157 ISP_UNLOCK(isp);
158
159 if (IS_SCSI(isp)) {
160 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
161 spi->sim = sim;
162 spi->path = path;
163#ifdef ISP_INTERNAL_TARGET
164 ISP_SET_PC(isp, chan, proc_active, 1);
165 if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
166 ISP_SET_PC(isp, chan, proc_active, 0);
167 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
168 }
169 ISP_SPI_PC(isp, chan)->num_threads += 1;
170#endif
171 } else {
172 fcparam *fcp = FCPARAM(isp, chan);
173 struct isp_fc *fc = ISP_FC_PC(isp, chan);
174 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
175 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
176 char name[16];
177
178 ISP_LOCK(isp);
179 fc->sim = sim;
180 fc->path = path;
181 fc->isp = isp;
182 fc->ready = 1;
183
184 callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
185 callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
186 TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
187 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
188
189 /*
190 * We start by being "loop down" if we have an initiator role
191 */
192 if (fcp->role & ISP_ROLE_INITIATOR) {
193 isp_freeze_loopdown(isp, chan, "isp_attach");
194 callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
195 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
196 }
197 ISP_UNLOCK(isp);
198 if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
199 xpt_free_path(fc->path);
200 ISP_LOCK(isp);
201 if (callout_active(&fc->ldt))
202 callout_stop(&fc->ldt);
203 xpt_bus_deregister(cam_sim_path(fc->sim));
204 ISP_UNLOCK(isp);
205 cam_sim_free(fc->sim, FALSE);
206 return (ENOMEM);
207 }
208 fc->num_threads += 1;
209#ifdef ISP_INTERNAL_TARGET
210 ISP_SET_PC(isp, chan, proc_active, 1);
211 if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
212 ISP_SET_PC(isp, chan, proc_active, 0);
213 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
214 }
215 fc->num_threads += 1;
216#endif
217 if (chan > 0) {
218 snprintf(name, sizeof(name), "chan%d", chan);
219 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
220 OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
221 }
222 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
223 "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
224 "World Wide Node Name");
225 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
226 "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
227 "World Wide Port Name");
228 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
229 "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
230 "Loop Down Limit");
231 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
232 "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
233 "Gone Device Time");
234#if defined(ISP_TARGET_MODE) && defined(DEBUG)
235 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
236 "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
237 "Cause a Lost Frame on a Read");
238#endif
239 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
240 "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
241 isp_role_sysctl, "I", "Current role");
242 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
243 "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
244 "Connection speed in gigabits");
245 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
246 "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
247 "Link state");
248 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
249 "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
250 "Firmware state");
251 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
252 "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
253 "Loop state");
254 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
255 "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
256 "Connection topology");
257 }
258 return (0);
259}
260
261static void
262isp_detach_internal_target(ispsoftc_t *isp, int chan)
263{
264#ifdef ISP_INTERNAL_TARGET
265 void *wchan;
266
267 ISP_GET_PC(isp, chan, target_proc, wchan);
268 ISP_SET_PC(isp, chan, proc_active, 0);
269 wakeup(wchan);
270#endif
271}
272
273static void
274isp_detach_chan(ispsoftc_t *isp, int chan)
275{
276 struct cam_sim *sim;
277 struct cam_path *path;
278 struct ccb_setasync csa;
279 int *num_threads;
280
281 ISP_GET_PC(isp, chan, sim, sim);
282 ISP_GET_PC(isp, chan, path, path);
283 ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
284
285 xpt_setup_ccb(&csa.ccb_h, path, 5);
286 csa.ccb_h.func_code = XPT_SASYNC_CB;
287 csa.event_enable = 0;
288 csa.callback = isp_cam_async;
289 csa.callback_arg = sim;
290 xpt_action((union ccb *)&csa);
291 xpt_free_path(path);
292 xpt_bus_deregister(cam_sim_path(sim));
293 cam_sim_free(sim, FALSE);
294
295 /* Wait for the channel's spawned threads to exit. */
296 wakeup(isp->isp_osinfo.pc.ptr);
297 isp_detach_internal_target(isp, chan);
298 while (*num_threads != 0)
299 mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
300}
301
302int
303isp_attach(ispsoftc_t *isp)
304{
305 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
306 int du = device_get_unit(isp->isp_dev);
307 int chan;
308
309 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
310 isp->isp_osinfo.ehook.ich_arg = isp;
311 /*
312 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
313 * will be called right awawy, which will clear isp_osinfo.ehook_active,
314 * which would be unwise to then set again later.
315 */
316 isp->isp_osinfo.ehook_active = 1;
317 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
318 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
319 return (-EIO);
320 }
321
322 /*
323 * Create the device queue for our SIM(s).
324 */
325 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
326 if (isp->isp_osinfo.devq == NULL) {
327 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
328 return (EIO);
329 }
330
331 for (chan = 0; chan < isp->isp_nchan; chan++) {
332 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
333 goto unwind;
334 }
335 }
336
337 callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
338 isp_timer_count = hz >> 2;
339 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
340 isp->isp_osinfo.timer_active = 1;
341
342 isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
343 if (isp->isp_osinfo.cdev) {
344 isp->isp_osinfo.cdev->si_drv1 = isp;
345 }
346 return (0);
347
348unwind:
349 while (--chan >= 0) {
350 struct cam_sim *sim;
351 struct cam_path *path;
352
353 ISP_GET_PC(isp, chan, sim, sim);
354 ISP_GET_PC(isp, chan, path, path);
355 xpt_free_path(path);
356 ISP_LOCK(isp);
357 xpt_bus_deregister(cam_sim_path(sim));
358 ISP_UNLOCK(isp);
359 cam_sim_free(sim, FALSE);
360 }
361 if (isp->isp_osinfo.ehook_active) {
362 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
363 isp->isp_osinfo.ehook_active = 0;
364 }
365 if (isp->isp_osinfo.cdev) {
366 destroy_dev(isp->isp_osinfo.cdev);
367 isp->isp_osinfo.cdev = NULL;
368 }
369 cam_simq_free(isp->isp_osinfo.devq);
370 isp->isp_osinfo.devq = NULL;
371 return (-1);
372}
373
374int
375isp_detach(ispsoftc_t *isp)
376{
377 struct cam_sim *sim;
378 int chan;
379
380 ISP_LOCK(isp);
381 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
382 ISP_GET_PC(isp, chan, sim, sim);
383 if (sim->refcount > 2) {
384 ISP_UNLOCK(isp);
385 return (EBUSY);
386 }
387 }
388 /* Tell spawned threads that we're exiting. */
389 isp->isp_osinfo.is_exiting = 1;
390 if (isp->isp_osinfo.timer_active) {
391 callout_stop(&isp->isp_osinfo.tmo);
392 isp->isp_osinfo.timer_active = 0;
393 }
394 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
395 isp_detach_chan(isp, chan);
396 ISP_UNLOCK(isp);
397
398 if (isp->isp_osinfo.cdev) {
399 destroy_dev(isp->isp_osinfo.cdev);
400 isp->isp_osinfo.cdev = NULL;
401 }
402 if (isp->isp_osinfo.ehook_active) {
403 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
404 isp->isp_osinfo.ehook_active = 0;
405 }
406 if (isp->isp_osinfo.devq != NULL) {
407 cam_simq_free(isp->isp_osinfo.devq);
408 isp->isp_osinfo.devq = NULL;
409 }
410 return (0);
411}
412
413static void
414isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
415{
416 if (IS_FC(isp)) {
417 struct isp_fc *fc = ISP_FC_PC(isp, chan);
418 if (fc->simqfrozen == 0) {
419 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
420 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
421 xpt_freeze_simq(fc->sim, 1);
422 } else {
423 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
424 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
425 }
426 }
427}
428
429static void
430isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
431{
432 if (IS_FC(isp)) {
433 struct isp_fc *fc = ISP_FC_PC(isp, chan);
434 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
435 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
436 if (wasfrozen && fc->simqfrozen == 0) {
437 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
438 xpt_release_simq(fc->sim, 1);
439 }
440 }
441}
442
443
444static int
445ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
446{
447 ispsoftc_t *isp;
448 int nr, chan, retval = ENOTTY;
449
450 isp = dev->si_drv1;
451
452 switch (c) {
453 case ISP_SDBLEV:
454 {
455 int olddblev = isp->isp_dblev;
456 isp->isp_dblev = *(int *)addr;
457 *(int *)addr = olddblev;
458 retval = 0;
459 break;
460 }
461 case ISP_GETROLE:
462 chan = *(int *)addr;
463 if (chan < 0 || chan >= isp->isp_nchan) {
464 retval = -ENXIO;
465 break;
466 }
467 if (IS_FC(isp)) {
468 *(int *)addr = FCPARAM(isp, chan)->role;
469 } else {
470 *(int *)addr = SDPARAM(isp, chan)->role;
471 }
472 retval = 0;
473 break;
474 case ISP_SETROLE:
475 nr = *(int *)addr;
476 chan = nr >> 8;
477 if (chan < 0 || chan >= isp->isp_nchan) {
478 retval = -ENXIO;
479 break;
480 }
481 nr &= 0xff;
482 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
483 retval = EINVAL;
484 break;
485 }
486 ISP_LOCK(isp);
487 if (IS_FC(isp))
488 *(int *)addr = FCPARAM(isp, chan)->role;
489 else
490 *(int *)addr = SDPARAM(isp, chan)->role;
491 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
492 ISP_UNLOCK(isp);
493 retval = 0;
494 break;
495
496 case ISP_RESETHBA:
497 ISP_LOCK(isp);
498 isp_reinit(isp, 0);
499 ISP_UNLOCK(isp);
500 retval = 0;
501 break;
502
503 case ISP_RESCAN:
504 if (IS_FC(isp)) {
505 chan = *(int *)addr;
506 if (chan < 0 || chan >= isp->isp_nchan) {
507 retval = -ENXIO;
508 break;
509 }
510 ISP_LOCK(isp);
511 if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
512 retval = EIO;
513 } else {
514 retval = 0;
515 }
516 ISP_UNLOCK(isp);
517 }
518 break;
519
520 case ISP_FC_LIP:
521 if (IS_FC(isp)) {
522 chan = *(int *)addr;
523 if (chan < 0 || chan >= isp->isp_nchan) {
524 retval = -ENXIO;
525 break;
526 }
527 ISP_LOCK(isp);
528 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
529 retval = EIO;
530 } else {
531 retval = 0;
532 }
533 ISP_UNLOCK(isp);
534 }
535 break;
536 case ISP_FC_GETDINFO:
537 {
538 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
539 fcportdb_t *lp;
540
541 if (IS_SCSI(isp)) {
542 break;
543 }
544 if (ifc->loopid >= MAX_FC_TARG) {
545 retval = EINVAL;
546 break;
547 }
548 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
549 if (lp->state != FC_PORTDB_STATE_NIL) {
550 ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
551 ifc->loopid = lp->handle;
552 ifc->portid = lp->portid;
553 ifc->node_wwn = lp->node_wwn;
554 ifc->port_wwn = lp->port_wwn;
555 retval = 0;
556 } else {
557 retval = ENODEV;
558 }
559 break;
560 }
561 case ISP_GET_STATS:
562 {
563 isp_stats_t *sp = (isp_stats_t *) addr;
564
565 ISP_MEMZERO(sp, sizeof (*sp));
566 sp->isp_stat_version = ISP_STATS_VERSION;
567 sp->isp_type = isp->isp_type;
568 sp->isp_revision = isp->isp_revision;
569 ISP_LOCK(isp);
570 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
571 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
572 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
573 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
574 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
575 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
576 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
577 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
578 ISP_UNLOCK(isp);
579 retval = 0;
580 break;
581 }
582 case ISP_CLR_STATS:
583 ISP_LOCK(isp);
584 isp->isp_intcnt = 0;
585 isp->isp_intbogus = 0;
586 isp->isp_intmboxc = 0;
587 isp->isp_intoasync = 0;
588 isp->isp_rsltccmplt = 0;
589 isp->isp_fphccmplt = 0;
590 isp->isp_rscchiwater = 0;
591 isp->isp_fpcchiwater = 0;
592 ISP_UNLOCK(isp);
593 retval = 0;
594 break;
595 case ISP_FC_GETHINFO:
596 {
597 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
598 int chan = hba->fc_channel;
599
600 if (chan < 0 || chan >= isp->isp_nchan) {
601 retval = ENXIO;
602 break;
603 }
604 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
605 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
606 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
607 hba->fc_nchannels = isp->isp_nchan;
608 if (IS_FC(isp)) {
609 hba->fc_nports = MAX_FC_TARG;
610 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
611 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
612 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
613 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
614 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
615 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
616 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
617 } else {
618 hba->fc_nports = MAX_TARGETS;
619 hba->fc_speed = 0;
620 hba->fc_topology = 0;
621 hba->nvram_node_wwn = 0ull;
622 hba->nvram_port_wwn = 0ull;
623 hba->active_node_wwn = 0ull;
624 hba->active_port_wwn = 0ull;
625 }
626 retval = 0;
627 break;
628 }
629 case ISP_TSK_MGMT:
630 {
631 int needmarker;
632 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
633 uint16_t loopid;
634 mbreg_t mbs;
635
636 if (IS_SCSI(isp)) {
637 break;
638 }
639
640 chan = fct->chan;
641 if (chan < 0 || chan >= isp->isp_nchan) {
642 retval = -ENXIO;
643 break;
644 }
645
646 needmarker = retval = 0;
647 loopid = fct->loopid;
648 ISP_LOCK(isp);
649 if (IS_24XX(isp)) {
650 uint8_t local[QENTRY_LEN];
651 isp24xx_tmf_t *tmf;
652 isp24xx_statusreq_t *sp;
653 fcparam *fcp = FCPARAM(isp, chan);
654 fcportdb_t *lp;
655 int i;
656
657 for (i = 0; i < MAX_FC_TARG; i++) {
658 lp = &fcp->portdb[i];
659 if (lp->handle == loopid) {
660 break;
661 }
662 }
663 if (i == MAX_FC_TARG) {
664 retval = ENXIO;
665 ISP_UNLOCK(isp);
666 break;
667 }
668 /* XXX VALIDATE LP XXX */
669 tmf = (isp24xx_tmf_t *) local;
670 ISP_MEMZERO(tmf, QENTRY_LEN);
671 tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
672 tmf->tmf_header.rqs_entry_count = 1;
673 tmf->tmf_nphdl = lp->handle;
674 tmf->tmf_delay = 2;
675 tmf->tmf_timeout = 2;
676 tmf->tmf_tidlo = lp->portid;
677 tmf->tmf_tidhi = lp->portid >> 16;
678 tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
679 tmf->tmf_lun[1] = fct->lun & 0xff;
680 if (fct->lun >= 256) {
681 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
682 }
683 switch (fct->action) {
684 case IPT_CLEAR_ACA:
685 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
686 break;
687 case IPT_TARGET_RESET:
688 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
689 needmarker = 1;
690 break;
691 case IPT_LUN_RESET:
692 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
693 needmarker = 1;
694 break;
695 case IPT_CLEAR_TASK_SET:
696 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
697 needmarker = 1;
698 break;
699 case IPT_ABORT_TASK_SET:
700 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
701 needmarker = 1;
702 break;
703 default:
704 retval = EINVAL;
705 break;
706 }
707 if (retval) {
708 ISP_UNLOCK(isp);
709 break;
710 }
711 MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
712 mbs.param[1] = QENTRY_LEN;
713 mbs.param[2] = DMA_WD1(fcp->isp_scdma);
714 mbs.param[3] = DMA_WD0(fcp->isp_scdma);
715 mbs.param[6] = DMA_WD3(fcp->isp_scdma);
716 mbs.param[7] = DMA_WD2(fcp->isp_scdma);
717
718 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
719 ISP_UNLOCK(isp);
720 retval = ENOMEM;
721 break;
722 }
723 isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
724 MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
725 sp = (isp24xx_statusreq_t *) local;
726 sp->req_completion_status = 1;
727 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
728 MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
729 isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
730 FC_SCRATCH_RELEASE(isp, chan);
731 if (retval || sp->req_completion_status != 0) {
732 FC_SCRATCH_RELEASE(isp, chan);
733 retval = EIO;
734 }
735 if (retval == 0) {
736 if (needmarker) {
737 fcp->sendmarker = 1;
738 }
739 }
740 } else {
741 MBSINIT(&mbs, 0, MBLOGALL, 0);
742 if (ISP_CAP_2KLOGIN(isp) == 0) {
743 loopid <<= 8;
744 }
745 switch (fct->action) {
746 case IPT_CLEAR_ACA:
747 mbs.param[0] = MBOX_CLEAR_ACA;
748 mbs.param[1] = loopid;
749 mbs.param[2] = fct->lun;
750 break;
751 case IPT_TARGET_RESET:
752 mbs.param[0] = MBOX_TARGET_RESET;
753 mbs.param[1] = loopid;
754 needmarker = 1;
755 break;
756 case IPT_LUN_RESET:
757 mbs.param[0] = MBOX_LUN_RESET;
758 mbs.param[1] = loopid;
759 mbs.param[2] = fct->lun;
760 needmarker = 1;
761 break;
762 case IPT_CLEAR_TASK_SET:
763 mbs.param[0] = MBOX_CLEAR_TASK_SET;
764 mbs.param[1] = loopid;
765 mbs.param[2] = fct->lun;
766 needmarker = 1;
767 break;
768 case IPT_ABORT_TASK_SET:
769 mbs.param[0] = MBOX_ABORT_TASK_SET;
770 mbs.param[1] = loopid;
771 mbs.param[2] = fct->lun;
772 needmarker = 1;
773 break;
774 default:
775 retval = EINVAL;
776 break;
777 }
778 if (retval == 0) {
779 if (needmarker) {
780 FCPARAM(isp, chan)->sendmarker = 1;
781 }
782 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
783 if (retval) {
784 retval = EIO;
785 }
786 }
787 }
788 ISP_UNLOCK(isp);
789 break;
790 }
791 default:
792 break;
793 }
794 return (retval);
795}
796
797static void
798isp_intr_enable(void *arg)
799{
800 int chan;
801 ispsoftc_t *isp = arg;
802 ISP_LOCK(isp);
803 for (chan = 0; chan < isp->isp_nchan; chan++) {
804 if (IS_FC(isp)) {
805 if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
806 ISP_ENABLE_INTS(isp);
807 break;
808 }
809 } else {
810 if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
811 ISP_ENABLE_INTS(isp);
812 break;
813 }
814 }
815 }
816 isp->isp_osinfo.ehook_active = 0;
817 ISP_UNLOCK(isp);
818 /* Release our hook so that the boot can continue. */
819 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
820}
821
822/*
823 * Local Inlines
824 */
825
826static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
827static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
828
829static ISP_INLINE int
830isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
831{
832 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
833 if (ISP_PCMD(ccb) == NULL) {
834 return (-1);
835 }
836 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
837 return (0);
838}
839
840static ISP_INLINE void
841isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
842{
843 if (ISP_PCMD(ccb)) {
844#ifdef ISP_TARGET_MODE
845 PISP_PCMD(ccb)->datalen = 0;
846 PISP_PCMD(ccb)->totslen = 0;
847 PISP_PCMD(ccb)->cumslen = 0;
848 PISP_PCMD(ccb)->crn = 0;
849#endif
850 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
851 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
852 ISP_PCMD(ccb) = NULL;
853 }
854}
855
856/*
857 * Put the target mode functions here, because some are inlines
858 */
859#ifdef ISP_TARGET_MODE
860static ISP_INLINE void isp_tmlock(ispsoftc_t *, const char *);
861static ISP_INLINE void isp_tmunlk(ispsoftc_t *);
862static ISP_INLINE int is_any_lun_enabled(ispsoftc_t *, int);
863static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
864static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
865static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
866static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
867static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
868static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
869static ISP_INLINE atio_private_data_t *isp_find_atpd(ispsoftc_t *, tstate_t *, uint32_t);
870static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
871static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
872static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
873static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
874static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
875static void destroy_lun_state(ispsoftc_t *, tstate_t *);
876static void isp_enable_lun(ispsoftc_t *, union ccb *);
877static cam_status isp_enable_deferred_luns(ispsoftc_t *, int);
878static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
879static void isp_disable_lun(ispsoftc_t *, union ccb *);
880static int isp_enable_target_mode(ispsoftc_t *, int);
881static int isp_disable_target_mode(ispsoftc_t *, int);
882static void isp_ledone(ispsoftc_t *, lun_entry_t *);
883static timeout_t isp_refire_putback_atio;
884static timeout_t isp_refire_notify_ack;
885static void isp_complete_ctio(union ccb *);
886static void isp_target_putback_atio(union ccb *);
887enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
888static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
889static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
890static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
891static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
892static void isp_handle_platform_ctio(ispsoftc_t *, void *);
893static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
894static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
895static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
896static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
897static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
898static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
899static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
900
901static ISP_INLINE void
902isp_tmlock(ispsoftc_t *isp, const char *msg)
903{
904 while (isp->isp_osinfo.tmbusy) {
905 isp->isp_osinfo.tmwanted = 1;
906 mtx_sleep(isp, &isp->isp_lock, PRIBIO, msg, 0);
907 }
908 isp->isp_osinfo.tmbusy = 1;
909}
910
911static ISP_INLINE void
912isp_tmunlk(ispsoftc_t *isp)
913{
914 isp->isp_osinfo.tmbusy = 0;
915 if (isp->isp_osinfo.tmwanted) {
916 isp->isp_osinfo.tmwanted = 0;
917 wakeup(isp);
918 }
919}
920
921static ISP_INLINE int
922is_any_lun_enabled(ispsoftc_t *isp, int bus)
923{
924 struct tslist *lhp;
925 int i;
926
927 for (i = 0; i < LUN_HASH_SIZE; i++) {
928 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
929 if (SLIST_FIRST(lhp))
930 return (1);
931 }
932 return (0);
933}
934
935static ISP_INLINE int
936is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
937{
938 tstate_t *tptr;
939 struct tslist *lhp;
940
941 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
942 SLIST_FOREACH(tptr, lhp, next) {
943 if (tptr->ts_lun == lun) {
944 return (1);
945 }
946 }
947 return (0);
948}
949
950static void
951dump_tstates(ispsoftc_t *isp, int bus)
952{
953 int i, j;
954 struct tslist *lhp;
955 tstate_t *tptr = NULL;
956
957 if (bus >= isp->isp_nchan) {
958 return;
959 }
960 for (i = 0; i < LUN_HASH_SIZE; i++) {
961 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
962 j = 0;
963 SLIST_FOREACH(tptr, lhp, next) {
964 xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
965 j++;
966 }
967 }
968}
969
970static ISP_INLINE tstate_t *
971get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
972{
973 tstate_t *tptr = NULL;
974 struct tslist *lhp;
975
976 if (bus < isp->isp_nchan) {
977 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
978 SLIST_FOREACH(tptr, lhp, next) {
979 if (tptr->ts_lun == lun) {
980 tptr->hold++;
981 return (tptr);
982 }
983 }
984 }
985 return (NULL);
986}
987
988static ISP_INLINE tstate_t *
989get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
990{
991 tstate_t *tptr = NULL;
992 atio_private_data_t *atp;
993 struct tslist *lhp;
994 int i;
995
996 if (bus < isp->isp_nchan && tagval != 0) {
997 for (i = 0; i < LUN_HASH_SIZE; i++) {
998 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
999 SLIST_FOREACH(tptr, lhp, next) {
1000 atp = isp_find_atpd(isp, tptr, tagval);
1001 if (atp) {
1002 tptr->hold++;
1003 return (tptr);
1004 }
1005 }
1006 }
1007 }
1008 return (NULL);
1009}
1010
1011static ISP_INLINE inot_private_data_t *
1012get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
1013{
1014 inot_private_data_t *ntp;
1015 tstate_t *tptr;
1016 struct tslist *lhp;
1017 int bus, i;
1018
1019 for (bus = 0; bus < isp->isp_nchan; bus++) {
1020 for (i = 0; i < LUN_HASH_SIZE; i++) {
1021 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1022 SLIST_FOREACH(tptr, lhp, next) {
1023 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
1024 if (ntp) {
1025 *rslt = tptr;
1026 tptr->hold++;
1027 return (ntp);
1028 }
1029 }
1030 }
1031 }
1032 return (NULL);
1033}
1034
1035static ISP_INLINE void
1036rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
1037{
1038 KASSERT((tptr->hold), ("tptr not held"));
1039 tptr->hold--;
1040}
1041
1042static void
1043isp_tmcmd_restart(ispsoftc_t *isp)
1044{
1045 inot_private_data_t *ntp;
1046 inot_private_data_t *restart_queue;
1047 tstate_t *tptr;
1048 union ccb *ccb;
1049 struct tslist *lhp;
1050 int bus, i;
1051
1052 for (bus = 0; bus < isp->isp_nchan; bus++) {
1053 for (i = 0; i < LUN_HASH_SIZE; i++) {
1054 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1055 SLIST_FOREACH(tptr, lhp, next) {
1056 if ((restart_queue = tptr->restart_queue) != NULL)
1057 tptr->restart_queue = NULL;
1058 while (restart_queue) {
1059 ntp = restart_queue;
1060 restart_queue = ntp->rd.nt.nt_hba;
1061 if (IS_24XX(isp)) {
1062 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
1063 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
1064 } else {
1065 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1066 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
1067 }
1068 isp_put_ntpd(isp, tptr, ntp);
1069 if (tptr->restart_queue && restart_queue != NULL) {
1070 ntp = tptr->restart_queue;
1071 tptr->restart_queue = restart_queue;
1072 while (restart_queue->rd.nt.nt_hba) {
1073 restart_queue = restart_queue->rd.nt.nt_hba;
1074 }
1075 restart_queue->rd.nt.nt_hba = ntp;
1076 break;
1077 }
1078 }
1079 /*
1080 * We only need to do this once per tptr
1081 */
1082 if (!TAILQ_EMPTY(&tptr->waitq)) {
1083 ccb = (union ccb *)TAILQ_LAST(&tptr->waitq, isp_ccbq);
1084 TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1085 isp_target_start_ctio(isp, ccb, FROM_TIMER);
1086 }
1087 }
1088 }
1089 }
1090}
1091
1092static ISP_INLINE atio_private_data_t *
1093isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1094{
1095 atio_private_data_t *atp;
1096
1097 atp = LIST_FIRST(&tptr->atfree);
1098 if (atp) {
1099 LIST_REMOVE(atp, next);
1100 atp->tag = tag;
1101 LIST_INSERT_HEAD(&tptr->atused[ATPDPHASH(tag)], atp, next);
1102 }
1103 return (atp);
1104}
1105
1106static ISP_INLINE atio_private_data_t *
1107isp_find_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1108{
1109 atio_private_data_t *atp;
1110
1111 LIST_FOREACH(atp, &tptr->atused[ATPDPHASH(tag)], next) {
1112 if (atp->tag == tag)
1113 return (atp);
1114 }
1115 return (NULL);
1116}
1117
1118static ISP_INLINE void
1119isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
1120{
1121 if (atp->ests) {
1122 isp_put_ecmd(isp, atp->ests);
1123 }
1124 LIST_REMOVE(atp, next);
1125 memset(atp, 0, sizeof (*atp));
1126 LIST_INSERT_HEAD(&tptr->atfree, atp, next);
1127}
1128
1129static void
1130isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1131{
1132 atio_private_data_t *atp;
1133 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1134
1135 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1136 xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
1137 atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
1138 }
1139}
1140
1141
1142static ISP_INLINE inot_private_data_t *
1143isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1144{
1145 inot_private_data_t *ntp;
1146 ntp = tptr->ntfree;
1147 if (ntp) {
1148 tptr->ntfree = ntp->next;
1149 }
1150 return (ntp);
1151}
1152
1153static ISP_INLINE inot_private_data_t *
1154isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1155{
1156 inot_private_data_t *ntp;
1157 for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1158 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1159 return (ntp);
1160 }
1161 }
1162 return (NULL);
1163}
1164
1165static ISP_INLINE void
1166isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1167{
1168 ntp->rd.tag_id = ntp->rd.seq_id = 0;
1169 ntp->next = tptr->ntfree;
1170 tptr->ntfree = ntp;
1171}
1172
1173static cam_status
1174create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1175{
1176 cam_status status;
1177 lun_id_t lun;
1178 struct tslist *lhp;
1179 tstate_t *tptr;
1180 int i;
1181
1182 lun = xpt_path_lun_id(path);
1183 if (lun != CAM_LUN_WILDCARD) {
1184 if (lun >= ISP_MAX_LUNS(isp)) {
1185 return (CAM_LUN_INVALID);
1186 }
1187 }
1188 if (is_lun_enabled(isp, bus, lun)) {
1189 return (CAM_LUN_ALRDY_ENA);
1190 }
1191 tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1192 if (tptr == NULL) {
1193 return (CAM_RESRC_UNAVAIL);
1194 }
1195 tptr->ts_lun = lun;
1196 status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1197 if (status != CAM_REQ_CMP) {
1198 free(tptr, M_DEVBUF);
1199 return (status);
1200 }
1201 SLIST_INIT(&tptr->atios);
1202 SLIST_INIT(&tptr->inots);
1203 TAILQ_INIT(&tptr->waitq);
1204 LIST_INIT(&tptr->atfree);
1205 for (i = ATPDPSIZE-1; i >= 0; i--)
1206 LIST_INSERT_HEAD(&tptr->atfree, &tptr->atpool[i], next);
1207 for (i = 0; i < ATPDPHASHSIZE; i++)
1208 LIST_INIT(&tptr->atused[i]);
1209 for (i = 0; i < ATPDPSIZE-1; i++)
1210 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1211 tptr->ntfree = tptr->ntpool;
1212 tptr->hold = 1;
1213 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
1214 SLIST_INSERT_HEAD(lhp, tptr, next);
1215 *rslt = tptr;
1216 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1217 return (CAM_REQ_CMP);
1218}
1219
1220static ISP_INLINE void
1221destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1222{
1223 union ccb *ccb;
1224 struct tslist *lhp;
1225
1226 KASSERT((tptr->hold != 0), ("tptr is not held"));
1227 KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1228 do {
1229 ccb = (union ccb *)SLIST_FIRST(&tptr->atios);
1230 if (ccb) {
1231 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1232 ccb->ccb_h.status = CAM_REQ_ABORTED;
1233 xpt_done(ccb);
1234 }
1235 } while (ccb);
1236 do {
1237 ccb = (union ccb *)SLIST_FIRST(&tptr->inots);
1238 if (ccb) {
1239 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1240 ccb->ccb_h.status = CAM_REQ_ABORTED;
1241 xpt_done(ccb);
1242 }
1243 } while (ccb);
1244 ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
1245 SLIST_REMOVE(lhp, tptr, tstate, next);
1246 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
1247 xpt_free_path(tptr->owner);
1248 free(tptr, M_DEVBUF);
1249}
1250
1251/*
1252 * Enable a lun.
1253 */
1254static void
1255isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1256{
1257 tstate_t *tptr = NULL;
1258 int bus, tm_enabled, target_role;
1259 target_id_t target;
1260 lun_id_t lun;
1261
1262
1263 /*
1264 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1265 */
1266 bus = XS_CHANNEL(ccb);
1267 target = ccb->ccb_h.target_id;
1268 lun = ccb->ccb_h.target_lun;
1269 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "enabling lun %u\n", lun);
1270 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1271 ccb->ccb_h.status = CAM_LUN_INVALID;
1272 xpt_done(ccb);
1273 return;
1274 }
1275
1276 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1277 ccb->ccb_h.status = CAM_LUN_INVALID;
1278 xpt_done(ccb);
1279 return;
1280 }
1281 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1282 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1283 }
1284
1285 /*
1286 * Wait until we're not busy with the lun enables subsystem
1287 */
1288 isp_tmlock(isp, "isp_enable_lun");
1289
1290 /*
1291 * This is as a good a place as any to check f/w capabilities.
1292 */
1293
1294 if (IS_FC(isp)) {
1295 if (ISP_CAP_TMODE(isp) == 0) {
1296 xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1297 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1298 goto done;
1299 }
1300 /*
1301 * We *could* handle non-SCCLUN f/w, but we'd have to
1302 * dork with our already fragile enable/disable code.
1303 */
1304 if (ISP_CAP_SCCFW(isp) == 0) {
1305 xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1306 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1307 goto done;
1308 }
1309
1310 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1311
1312 } else {
1313 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1314 }
1315
1316 /*
1317 * Create the state pointer.
1318 * It should not already exist.
1319 */
1320 tptr = get_lun_statep(isp, bus, lun);
1321 if (tptr) {
1322 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1323 goto done;
1324 }
1325 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1326 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1327 goto done;
1328 }
1329
1330 /*
1331 * We have a tricky maneuver to perform here.
1332 *
1333 * If target mode isn't already enabled here,
1334 * *and* our current role includes target mode,
1335 * we enable target mode here.
1336 *
1337 */
1338 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1339 if (tm_enabled == 0 && target_role != 0) {
1340 if (isp_enable_target_mode(isp, bus)) {
1341 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1342 destroy_lun_state(isp, tptr);
1343 tptr = NULL;
1344 goto done;
1345 }
1346 tm_enabled = 1;
1347 }
1348
1349 /*
1350 * Now check to see whether this bus is in target mode already.
1351 *
1352 * If not, a later role change into target mode will finish the job.
1353 */
1354 if (tm_enabled == 0) {
1355 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1356 ccb->ccb_h.status = CAM_REQ_CMP;
1357 xpt_print(ccb->ccb_h.path, "Target Mode not enabled yet- lun enable deferred\n");
1358 goto done1;
1359 }
1360
1361 /*
1362 * Enable the lun.
1363 */
1364 ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1365
1366done:
1367 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1368 if (tptr) {
1369 destroy_lun_state(isp, tptr);
1370 tptr = NULL;
1371 }
1372 } else {
1373 tptr->enabled = 1;
1374 }
1375done1:
1376 if (tptr) {
1377 rls_lun_statep(isp, tptr);
1378 }
1379
1380 /*
1381 * And we're outta here....
1382 */
1383 isp_tmunlk(isp);
1384 xpt_done(ccb);
1385}
1386
1387static cam_status
1388isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1389{
1390 tstate_t *tptr = NULL;
1391 struct tslist *lhp;
1392 int i, n;
1393
1394
1395 ISP_GET_PC(isp, bus, tm_enabled, i);
1396 if (i == 1) {
1397 return (CAM_REQ_CMP);
1398 }
1399 ISP_GET_PC(isp, bus, tm_enable_defer, i);
1400 if (i == 0) {
1401 return (CAM_REQ_CMP);
1402 }
1403 /*
1404 * If this succeeds, it will set tm_enable
1405 */
1406 if (isp_enable_target_mode(isp, bus)) {
1407 return (CAM_REQ_CMP_ERR);
1408 }
1409 isp_tmlock(isp, "isp_enable_deferred_luns");
1410 for (n = i = 0; i < LUN_HASH_SIZE; i++) {
1411 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1412 SLIST_FOREACH(tptr, lhp, next) {
1413 tptr->hold++;
1414 if (tptr->enabled == 0) {
1415 if (isp_enable_deferred(isp, bus, tptr->ts_lun) == CAM_REQ_CMP) {
1416 tptr->enabled = 1;
1417 n++;
1418 }
1419 } else {
1420 n++;
1421 }
1422 tptr->hold--;
1423 }
1424 }
1425 isp_tmunlk(isp);
1426 if (n == 0) {
1427 return (CAM_REQ_CMP_ERR);
1428 }
1429 ISP_SET_PC(isp, bus, tm_enable_defer, 0);
1430 return (CAM_REQ_CMP);
1431}
1432
1433static cam_status
1434isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1435{
1436 cam_status status;
1437 int luns_already_enabled;
1438
1439 ISP_GET_PC(isp, bus, tm_luns_enabled, luns_already_enabled);
1440 isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u luns_enabled %d", __func__, bus, lun, luns_already_enabled);
1441 if (IS_24XX(isp) || (IS_FC(isp) && luns_already_enabled)) {
1442 status = CAM_REQ_CMP;
1443 } else {
1444 int cmd_cnt, not_cnt;
1445
1446 if (IS_23XX(isp)) {
1447 cmd_cnt = DFLT_CMND_CNT;
1448 not_cnt = DFLT_INOT_CNT;
1449 } else {
1450 cmd_cnt = 64;
1451 not_cnt = 8;
1452 }
1453 status = CAM_REQ_INPROG;
1454 isp->isp_osinfo.rptr = &status;
1455 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun == CAM_LUN_WILDCARD? 0 : lun, cmd_cnt, not_cnt)) {
1456 status = CAM_RESRC_UNAVAIL;
1457 } else {
1458 mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1459 }
1460 isp->isp_osinfo.rptr = NULL;
1461 }
1462 if (status == CAM_REQ_CMP) {
1463 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1464 isp_prt(isp, ISP_LOGCONFIG|ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1465 }
1466 return (status);
1467}
1468
1469static void
1470isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1471{
1472 tstate_t *tptr = NULL;
1473 int bus;
1474 cam_status status;
1475 target_id_t target;
1476 lun_id_t lun;
1477
1478 bus = XS_CHANNEL(ccb);
1479 target = ccb->ccb_h.target_id;
1480 lun = ccb->ccb_h.target_lun;
1481 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "disabling lun %u\n", lun);
1482 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1483 ccb->ccb_h.status = CAM_LUN_INVALID;
1484 xpt_done(ccb);
1485 return;
1486 }
1487
1488 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1489 ccb->ccb_h.status = CAM_LUN_INVALID;
1490 xpt_done(ccb);
1491 return;
1492 }
1493
1494 /*
1495 * See if we're busy disabling a lun now.
1496 */
1497 isp_tmlock(isp, "isp_disable_lun");
1498 status = CAM_REQ_INPROG;
1499
1500 /*
1501 * Find the state pointer.
1502 */
1503 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1504 status = CAM_PATH_INVALID;
1505 goto done;
1506 }
1507
1508 /*
1509 * If we're a 24XX card, we're done.
1510 */
1511 if (IS_24XX(isp)) {
1512 status = CAM_REQ_CMP;
1513 goto done;
1514 }
1515
1516 /*
1517 * For SCC FW, we only deal with lun zero.
1518 */
1519 if (IS_FC(isp) && lun > 0) {
1520 status = CAM_REQ_CMP;
1521 goto done;
1522 }
1523 isp->isp_osinfo.rptr = &status;
1524 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1525 status = CAM_RESRC_UNAVAIL;
1526 } else {
1527 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1528 }
1529 isp->isp_osinfo.rptr = NULL;
1530done:
1531 if (status == CAM_REQ_CMP) {
1532 tptr->enabled = 0;
1533 if (is_any_lun_enabled(isp, bus) == 0) {
1534 if (isp_disable_target_mode(isp, bus)) {
1535 status = CAM_REQ_CMP_ERR;
1536 }
1537 }
1538 }
1539 ccb->ccb_h.status = status;
1540 if (status == CAM_REQ_CMP) {
1541 destroy_lun_state(isp, tptr);
1542 xpt_print(ccb->ccb_h.path, "lun now disabled for target mode\n");
1543 } else {
1544 if (tptr)
1545 rls_lun_statep(isp, tptr);
1546 }
1547 isp_tmunlk(isp);
1548 xpt_done(ccb);
1549}
1550
1551static int
1552isp_enable_target_mode(ispsoftc_t *isp, int bus)
1553{
1554 int tm_enabled;
1555
1556 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1557 if (tm_enabled != 0) {
1558 return (0);
1559 }
1560 if (IS_SCSI(isp)) {
1561 mbreg_t mbs;
1562 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1563 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1564 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1565 mbs.param[2] = bus << 7;
1566 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1567 isp_prt(isp, ISP_LOGERR, "Unable to enable Target Role on Bus %d", bus);
1568 return (EIO);
1569 }
1570 }
1571 ISP_SET_PC(isp, bus, tm_enabled, 1);
1572 isp_prt(isp, ISP_LOGINFO, "Target Role enabled on Bus %d", bus);
1573 return (0);
1574}
1575
1576static int
1577isp_disable_target_mode(ispsoftc_t *isp, int bus)
1578{
1579 int tm_enabled;
1580
1581 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1582 if (tm_enabled == 0) {
1583 return (0);
1584 }
1585 if (IS_SCSI(isp)) {
1586 mbreg_t mbs;
1587 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1588 mbs.param[2] = bus << 7;
1589 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1590 isp_prt(isp, ISP_LOGERR, "Unable to disable Target Role on Bus %d", bus);
1591 return (EIO);
1592 }
1593 }
1594 ISP_SET_PC(isp, bus, tm_enabled, 0);
1595 isp_prt(isp, ISP_LOGINFO, "Target Role disabled on Bus %d", bus);
1596 return (0);
1597}
1598
1599static void
1600isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1601{
1602 uint32_t *rptr;
1603
1604 rptr = isp->isp_osinfo.rptr;
1605 if (lep->le_status != LUN_OK) {
1606 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1607 if (rptr) {
1608 *rptr = CAM_REQ_CMP_ERR;
1609 wakeup_one(rptr);
1610 }
1611 } else {
1612 if (rptr) {
1613 *rptr = CAM_REQ_CMP;
1614 wakeup_one(rptr);
1615 }
1616 }
1617}
1618
1619static void
1620isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1621{
1622 int fctape, sendstatus, resid;
1623 tstate_t *tptr;
1624 fcparam *fcp;
1625 atio_private_data_t *atp;
1626 struct ccb_scsiio *cso;
1627 uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1628 uint8_t local[QENTRY_LEN];
1629
1630 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1631 if (tptr == NULL) {
1632 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1633 if (tptr == NULL) {
1634 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find tstate pointer", __func__, ccb->csio.tag_id);
1635 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1636 xpt_done(ccb);
1637 return;
1638 }
1639 }
1640 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len,
1641 (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1642
1643 switch (how) {
1644 case FROM_TIMER:
1645 case FROM_CAM:
1646 /*
1647 * Insert at the tail of the list, if any, waiting CTIO CCBs
1648 */
1649 TAILQ_INSERT_TAIL(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1650 break;
1651 case FROM_SRR:
1652 case FROM_CTIO_DONE:
1653 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1654 break;
1655 }
1656
1657 while (TAILQ_FIRST(&tptr->waitq) != NULL) {
1658 ccb = (union ccb *) TAILQ_FIRST(&tptr->waitq);
1659 TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1660
1661 cso = &ccb->csio;
1662 xfrlen = cso->dxfer_len;
1663 if (xfrlen == 0) {
1664 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1665 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1666 ccb->ccb_h.status = CAM_REQ_INVALID;
1667 xpt_done(ccb);
1668 continue;
1669 }
1670 }
1671
1672 atp = isp_find_atpd(isp, tptr, cso->tag_id);
1673 if (atp == NULL) {
1674 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1675 isp_dump_atpd(isp, tptr);
1676 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1677 xpt_done(ccb);
1678 continue;
1679 }
1680
1681 /*
1682 * Is this command a dead duck?
1683 */
1684 if (atp->dead) {
1685 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1686 ccb->ccb_h.status = CAM_REQ_ABORTED;
1687 xpt_done(ccb);
1688 continue;
1689 }
1690
1691 /*
1692 * Check to make sure we're still in target mode.
1693 */
1694 fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1695 if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1696 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1697 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1698 xpt_done(ccb);
1699 continue;
1700 }
1701
1702 /*
1703 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1704 * could be split into two CTIOs to split data and status).
1705 */
1706 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1707 isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags);
1708 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1709 break;
1710 }
1711
1712 /*
1713 * Does the initiator expect FC-Tape style responses?
1714 */
1715 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1716 fctape = 1;
1717 } else {
1718 fctape = 0;
1719 }
1720
1721 /*
1722 * If we already did the data xfer portion of a CTIO that sends data
1723 * and status, don't do it again and do the status portion now.
1724 */
1725 if (atp->sendst) {
1726 isp_prt(isp, ISP_LOGTINFO, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1727 cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1728 xfrlen = 0; /* we already did the data transfer */
1729 atp->sendst = 0;
1730 }
1731 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1732 sendstatus = 1;
1733 } else {
1734 sendstatus = 0;
1735 }
1736
1737 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1738 KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1739 /*
1740 * Sense length is not the entire sense data structure size. Periph
1741 * drivers don't seem to be setting sense_len to reflect the actual
1742 * size. We'll peek inside to get the right amount.
1743 */
1744 sense_length = cso->sense_len;
1745
1746 /*
1747 * This 'cannot' happen
1748 */
1749 if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1750 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1751 }
1752 } else {
1753 sense_length = 0;
1754 }
1755
1756 memset(local, 0, QENTRY_LEN);
1757
1758 /*
1759 * Check for overflow
1760 */
1761 tmp = atp->bytes_xfered + atp->bytes_in_transit + xfrlen;
1762 if (tmp > atp->orig_datalen) {
1763 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] data overflow by %u bytes", __func__, cso->tag_id, tmp - atp->orig_datalen);
1764 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1765 xpt_done(ccb);
1766 continue;
1767 }
1768
1769 if (IS_24XX(isp)) {
1770 ct7_entry_t *cto = (ct7_entry_t *) local;
1771
1772 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1773 cto->ct_header.rqs_entry_count = 1;
1774 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1775 ATPD_SET_SEQNO(cto, atp);
1776 cto->ct_nphdl = atp->nphdl;
1777 cto->ct_rxid = atp->tag;
1778 cto->ct_iid_lo = atp->portid;
1779 cto->ct_iid_hi = atp->portid >> 16;
1780 cto->ct_oxid = atp->oxid;
1781 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1782 cto->ct_timeout = 120;
1783 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1784
1785 /*
1786 * Mode 1, status, no data. Only possible when we are sending status, have
1787 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1788 *
1789 * Mode 2, status, no data. We have to use this in the case that
1790 * the sense data won't fit into a ct7_entry_t.
1791 *
1792 */
1793 if (sendstatus && xfrlen == 0) {
1794 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1795 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1796 if (sense_length <= MAXRESPLEN_24XX) {
1797 if (resid < 0) {
1798 cto->ct_resid = -resid;
1799 } else if (resid > 0) {
1800 cto->ct_resid = resid;
1801 }
1802 cto->ct_flags |= CT7_FLAG_MODE1;
1803 cto->ct_scsi_status = cso->scsi_status;
1804 if (resid < 0) {
1805 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1806 } else if (resid > 0) {
1807 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1808 }
1809 if (fctape) {
1810 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1811 }
1812 if (sense_length) {
1813 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1814 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1815 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1816 }
1817 } else {
1818 bus_addr_t addr;
1819 char buf[XCMD_SIZE];
1820 fcp_rsp_iu_t *rp;
1821
1822 if (atp->ests == NULL) {
1823 atp->ests = isp_get_ecmd(isp);
1824 if (atp->ests == NULL) {
1825 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1826 break;
1827 }
1828 }
1829 memset(buf, 0, sizeof (buf));
1830 rp = (fcp_rsp_iu_t *)buf;
1831 if (fctape) {
1832 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1833 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1834 }
1835 cto->ct_flags |= CT7_FLAG_MODE2;
1836 rp->fcp_rsp_scsi_status = cso->scsi_status;
1837 if (resid < 0) {
1838 rp->fcp_rsp_resid = -resid;
1839 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1840 } else if (resid > 0) {
1841 rp->fcp_rsp_resid = resid;
1842 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1843 }
1844 if (sense_length) {
1845 rp->fcp_rsp_snslen = sense_length;
1846 cto->ct_senselen = sense_length;
1847 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1848 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1849 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1850 } else {
1851 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1852 }
1853 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1854 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1855 }
1856 addr = isp->isp_osinfo.ecmd_dma;
1857 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1858 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1859 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1860 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1861 cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1862 cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1863 cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1864 }
1865 if (sense_length) {
1866 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
1867 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length,
1868 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1869 } else {
1870 isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1871 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1872 }
1873 atp->state = ATPD_STATE_LAST_CTIO;
1874 }
1875
1876 /*
1877 * Mode 0 data transfers, *possibly* with status.
1878 */
1879 if (xfrlen != 0) {
1880 cto->ct_flags |= CT7_FLAG_MODE0;
1881 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1882 cto->ct_flags |= CT7_DATA_IN;
1883 } else {
1884 cto->ct_flags |= CT7_DATA_OUT;
1885 }
1886
1887 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1888 cto->rsp.m0.ct_xfrlen = xfrlen;
1889
1890#ifdef DEBUG
1891 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1892 isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1893 ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1894 cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1895 }
1896#endif
1897 if (sendstatus) {
1898 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1899 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1900 cto->ct_flags |= CT7_SENDSTATUS;
1901 atp->state = ATPD_STATE_LAST_CTIO;
1902 if (fctape) {
1903 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1904 }
1905 } else {
1906 atp->sendst = 1; /* send status later */
1907 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1908 atp->state = ATPD_STATE_CTIO;
1909 }
1910 } else {
1911 atp->state = ATPD_STATE_CTIO;
1912 }
1913 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
1914 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1915 }
1916 } else if (IS_FC(isp)) {
1917 ct2_entry_t *cto = (ct2_entry_t *) local;
1918
1919 if (isp->isp_osinfo.sixtyfourbit)
1920 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1921 else
1922 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1923 cto->ct_header.rqs_entry_count = 1;
1924 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1925 ATPD_SET_SEQNO(cto, atp);
1926 if (ISP_CAP_2KLOGIN(isp)) {
1927 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1928 } else {
1929 cto->ct_iid = atp->nphdl;
1930 if (ISP_CAP_SCCFW(isp) == 0) {
1931 cto->ct_lun = ccb->ccb_h.target_lun;
1932 }
1933 }
1934 cto->ct_timeout = 10;
1935 cto->ct_rxid = cso->tag_id;
1936
1937 /*
1938 * Mode 1, status, no data. Only possible when we are sending status, have
1939 * no data to transfer, and the sense length can fit in the ct7_entry.
1940 *
1941 * Mode 2, status, no data. We have to use this in the case the response
1942 * length won't fit into a ct2_entry_t.
1943 *
1944 * We'll fill out this structure with information as if this were a
1945 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1946 * needed based upon this.
1947 */
1948 if (sendstatus && xfrlen == 0) {
1949 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1950 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1951 if (sense_length <= MAXRESPLEN) {
1952 if (resid < 0) {
1953 cto->ct_resid = -resid;
1954 } else if (resid > 0) {
1955 cto->ct_resid = resid;
1956 }
1957 cto->ct_flags |= CT2_FLAG_MODE1;
1958 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1959 if (resid < 0) {
1960 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1961 } else if (resid > 0) {
1962 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1963 }
1964 if (fctape) {
1965 cto->ct_flags |= CT2_CONFIRM;
1966 }
1967 if (sense_length) {
1968 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1969 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1970 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1971 }
1972 } else {
1973 bus_addr_t addr;
1974 char buf[XCMD_SIZE];
1975 fcp_rsp_iu_t *rp;
1976
1977 if (atp->ests == NULL) {
1978 atp->ests = isp_get_ecmd(isp);
1979 if (atp->ests == NULL) {
1980 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1981 break;
1982 }
1983 }
1984 memset(buf, 0, sizeof (buf));
1985 rp = (fcp_rsp_iu_t *)buf;
1986 if (fctape) {
1987 cto->ct_flags |= CT2_CONFIRM;
1988 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1989 }
1990 cto->ct_flags |= CT2_FLAG_MODE2;
1991 rp->fcp_rsp_scsi_status = cso->scsi_status;
1992 if (resid < 0) {
1993 rp->fcp_rsp_resid = -resid;
1994 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1995 } else if (resid > 0) {
1996 rp->fcp_rsp_resid = resid;
1997 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1998 }
1999 if (sense_length) {
2000 rp->fcp_rsp_snslen = sense_length;
2001 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
2002 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
2003 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
2004 } else {
2005 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
2006 }
2007 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
2008 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
2009 }
2010 addr = isp->isp_osinfo.ecmd_dma;
2011 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
2012 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
2013 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
2014 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
2015 if (isp->isp_osinfo.sixtyfourbit) {
2016 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
2017 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
2018 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
2019 } else {
2020 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
2021 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
2022 }
2023 }
2024 if (sense_length) {
2025 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
2026 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
2027 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
2028 } else {
2029 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid,
2030 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
2031 }
2032 atp->state = ATPD_STATE_LAST_CTIO;
2033 }
2034
2035 if (xfrlen != 0) {
2036 cto->ct_flags |= CT2_FLAG_MODE0;
2037 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2038 cto->ct_flags |= CT2_DATA_IN;
2039 } else {
2040 cto->ct_flags |= CT2_DATA_OUT;
2041 }
2042
2043 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
2044 cto->rsp.m0.ct_xfrlen = xfrlen;
2045
2046 if (sendstatus) {
2047 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
2048 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
2049 cto->ct_flags |= CT2_SENDSTATUS;
2050 atp->state = ATPD_STATE_LAST_CTIO;
2051 if (fctape) {
2052 cto->ct_flags |= CT2_CONFIRM;
2053 }
2054 } else {
2055 atp->sendst = 1; /* send status later */
2056 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
2057 atp->state = ATPD_STATE_CTIO;
2058 }
2059 } else {
2060 atp->state = ATPD_STATE_CTIO;
2061 }
2062 }
2063 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
2064 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
2065 } else {
2066 ct_entry_t *cto = (ct_entry_t *) local;
2067
2068 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
2069 cto->ct_header.rqs_entry_count = 1;
2070 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
2071 ATPD_SET_SEQNO(cto, atp);
2072 cto->ct_iid = cso->init_id;
2073 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
2074 cto->ct_tgt = ccb->ccb_h.target_id;
2075 cto->ct_lun = ccb->ccb_h.target_lun;
2076 cto->ct_fwhandle = cso->tag_id;
2077 if (atp->rxid) {
2078 cto->ct_tag_val = atp->rxid;
2079 cto->ct_flags |= CT_TQAE;
2080 }
2081 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
2082 cto->ct_flags |= CT_NODISC;
2083 }
2084 if (cso->dxfer_len == 0) {
2085 cto->ct_flags |= CT_NO_DATA;
2086 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2087 cto->ct_flags |= CT_DATA_IN;
2088 } else {
2089 cto->ct_flags |= CT_DATA_OUT;
2090 }
2091 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
2092 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
2093 cto->ct_scsi_status = cso->scsi_status;
2094 cto->ct_resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit - xfrlen;
2095 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d scsi status %x resid %d tag_id %x", __func__,
2096 cto->ct_fwhandle, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), cso->scsi_status, cso->resid, cso->tag_id);
2097 }
2098 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
2099 cto->ct_timeout = 10;
2100 }
2101
2102 if (isp_get_pcmd(isp, ccb)) {
2103 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
2104 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
2105 break;
2106 }
2107 if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
2108 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
2109 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
2110 isp_free_pcmd(isp, ccb);
2111 break;
2112 }
2113 atp->bytes_in_transit += xfrlen;
2114 PISP_PCMD(ccb)->datalen = xfrlen;
2115
2116
2117 /*
2118 * Call the dma setup routines for this entry (and any subsequent
2119 * CTIOs) if there's data to move, and then tell the f/w it's got
2120 * new things to play with. As with isp_start's usage of DMA setup,
2121 * any swizzling is done in the machine dependent layer. Because
2122 * of this, we put the request onto the queue area first in native
2123 * format.
2124 */
2125
2126 if (IS_24XX(isp)) {
2127 ct7_entry_t *cto = (ct7_entry_t *) local;
2128 cto->ct_syshandle = handle;
2129 } else if (IS_FC(isp)) {
2130 ct2_entry_t *cto = (ct2_entry_t *) local;
2131 cto->ct_syshandle = handle;
2132 } else {
2133 ct_entry_t *cto = (ct_entry_t *) local;
2134 cto->ct_syshandle = handle;
2135 }
2136
2137 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
2138 if (dmaresult != CMD_QUEUED) {
2139 isp_destroy_tgt_handle(isp, handle);
2140 isp_free_pcmd(isp, ccb);
2141 if (dmaresult == CMD_EAGAIN) {
2142 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
2143 break;
2144 }
2145 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2146 xpt_done(ccb);
2147 continue;
2148 }
2149 isp->isp_nactive++;
2150 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2151 if (xfrlen) {
2152 ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
2153 } else {
2154 ccb->ccb_h.spriv_field0 = ~0;
2155 }
2156 atp->ctcnt++;
2157 atp->seqno++;
2158 }
2159 rls_lun_statep(isp, tptr);
2160}
2161
2162static void
2163isp_refire_putback_atio(void *arg)
2164{
2165 union ccb *ccb = arg;
2166
2167 ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
2168 isp_target_putback_atio(ccb);
2169}
2170
2171static void
2172isp_refire_notify_ack(void *arg)
2173{
2174 isp_tna_t *tp = arg;
2175 ispsoftc_t *isp = tp->isp;
2176
2177 ISP_ASSERT_LOCKED(isp);
2178 if (isp_notify_ack(isp, tp->not)) {
2179 callout_schedule(&tp->timer, 5);
2180 } else {
2181 free(tp, M_DEVBUF);
2182 }
2183}
2184
2185
2186static void
2187isp_target_putback_atio(union ccb *ccb)
2188{
2189 ispsoftc_t *isp;
2190 struct ccb_scsiio *cso;
2191 void *qe;
2192
2193 isp = XS_ISP(ccb);
2194
2195 qe = isp_getrqentry(isp);
2196 if (qe == NULL) {
2197 xpt_print(ccb->ccb_h.path,
2198 "%s: Request Queue Overflow\n", __func__);
2199 callout_reset(&PISP_PCMD(ccb)->wdog, 10,
2200 isp_refire_putback_atio, ccb);
2201 return;
2202 }
2203 memset(qe, 0, QENTRY_LEN);
2204 cso = &ccb->csio;
2205 if (IS_FC(isp)) {
2206 at2_entry_t local, *at = &local;
2207 ISP_MEMZERO(at, sizeof (at2_entry_t));
2208 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
2209 at->at_header.rqs_entry_count = 1;
2210 if (ISP_CAP_SCCFW(isp)) {
2211 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
2212 } else {
2213 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
2214 }
2215 at->at_status = CT_OK;
2216 at->at_rxid = cso->tag_id;
2217 at->at_iid = cso->ccb_h.target_id;
2218 isp_put_atio2(isp, at, qe);
2219 } else {
2220 at_entry_t local, *at = &local;
2221 ISP_MEMZERO(at, sizeof (at_entry_t));
2222 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
2223 at->at_header.rqs_entry_count = 1;
2224 at->at_iid = cso->init_id;
2225 at->at_iid |= XS_CHANNEL(ccb) << 7;
2226 at->at_tgt = cso->ccb_h.target_id;
2227 at->at_lun = cso->ccb_h.target_lun;
2228 at->at_status = CT_OK;
2229 at->at_tag_val = AT_GET_TAG(cso->tag_id);
2230 at->at_handle = AT_GET_HANDLE(cso->tag_id);
2231 isp_put_atio(isp, at, qe);
2232 }
2233 ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
2234 ISP_SYNC_REQUEST(isp);
2235 isp_complete_ctio(ccb);
2236}
2237
2238static void
2239isp_complete_ctio(union ccb *ccb)
2240{
2241 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2242 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2243 xpt_done(ccb);
2244 }
2245}
2246
2247/*
2248 * Handle ATIO stuff that the generic code can't.
2249 * This means handling CDBs.
2250 */
2251
2252static void
2253isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
2254{
2255 tstate_t *tptr;
2256 int status, bus;
2257 struct ccb_accept_tio *atiop;
2258 atio_private_data_t *atp;
2259
2260 /*
2261 * The firmware status (except for the QLTM_SVALID bit)
2262 * indicates why this ATIO was sent to us.
2263 *
2264 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2265 *
2266 * If the DISCONNECTS DISABLED bit is set in the flags field,
2267 * we're still connected on the SCSI bus.
2268 */
2269 status = aep->at_status;
2270 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
2271 /*
2272 * Bus Phase Sequence error. We should have sense data
2273 * suggested by the f/w. I'm not sure quite yet what
2274 * to do about this for CAM.
2275 */
2276 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
2277 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2278 return;
2279 }
2280 if ((status & ~QLTM_SVALID) != AT_CDB) {
2281 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
2282 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2283 return;
2284 }
2285
2286 bus = GET_BUS_VAL(aep->at_iid);
2287 tptr = get_lun_statep(isp, bus, aep->at_lun);
2288 if (tptr == NULL) {
2289 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2290 if (tptr == NULL) {
2291 /*
2292 * Because we can't autofeed sense data back with
2293 * a command for parallel SCSI, we can't give back
2294 * a CHECK CONDITION. We'll give back a BUSY status
2295 * instead. This works out okay because the only
2296 * time we should, in fact, get this, is in the
2297 * case that somebody configured us without the
2298 * blackhole driver, so they get what they deserve.
2299 */
2300 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2301 return;
2302 }
2303 }
2304
2305 atp = isp_get_atpd(isp, tptr, aep->at_handle);
2306 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2307 if (atiop == NULL || atp == NULL) {
2308 /*
2309 * Because we can't autofeed sense data back with
2310 * a command for parallel SCSI, we can't give back
2311 * a CHECK CONDITION. We'll give back a QUEUE FULL status
2312 * instead. This works out okay because the only time we
2313 * should, in fact, get this, is in the case that we've
2314 * run out of ATIOS.
2315 */
2316 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
2317 ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
2318 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2319 if (atp) {
2320 isp_put_atpd(isp, tptr, atp);
2321 }
2322 rls_lun_statep(isp, tptr);
2323 return;
2324 }
2325 atp->rxid = aep->at_tag_val;
2326 atp->state = ATPD_STATE_ATIO;
2327 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2328 tptr->atio_count--;
2329 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2330 atiop->ccb_h.target_id = aep->at_tgt;
2331 atiop->ccb_h.target_lun = aep->at_lun;
2332 if (aep->at_flags & AT_NODISC) {
2333 atiop->ccb_h.flags |= CAM_DIS_DISCONNECT;
2334 } else {
2335 atiop->ccb_h.flags &= ~CAM_DIS_DISCONNECT;
2336 }
2337
2338 if (status & QLTM_SVALID) {
2339 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
2340 atiop->sense_len = amt;
2341 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
2342 } else {
2343 atiop->sense_len = 0;
2344 }
2345
2346 atiop->init_id = GET_IID_VAL(aep->at_iid);
2347 atiop->cdb_len = aep->at_cdblen;
2348 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
2349 atiop->ccb_h.status = CAM_CDB_RECVD;
2350 /*
2351 * Construct a tag 'id' based upon tag value (which may be 0..255)
2352 * and the handle (which we have to preserve).
2353 */
2354 atiop->tag_id = atp->tag;
2355 if (aep->at_flags & AT_TQAE) {
2356 atiop->tag_action = aep->at_tag_type;
2357 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
2358 }
2359 atp->orig_datalen = 0;
2360 atp->bytes_xfered = 0;
2361 atp->lun = aep->at_lun;
2362 atp->nphdl = aep->at_iid;
2363 atp->portid = PORT_NONE;
2364 atp->oxid = 0;
2365 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2366 atp->tattr = aep->at_tag_type;
2367 atp->state = ATPD_STATE_CAM;
2368 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO[0x%x] CDB=0x%x lun %d", aep->at_tag_val, atp->cdb0, atp->lun);
2369 rls_lun_statep(isp, tptr);
2370}
2371
2372static void
2373isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
2374{
2375 lun_id_t lun;
2376 fcportdb_t *lp;
2377 tstate_t *tptr;
2378 struct ccb_accept_tio *atiop;
2379 uint16_t nphdl;
2380 atio_private_data_t *atp;
2381 inot_private_data_t *ntp;
2382
2383 /*
2384 * The firmware status (except for the QLTM_SVALID bit)
2385 * indicates why this ATIO was sent to us.
2386 *
2387 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2388 */
2389 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
2390 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
2391 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2392 return;
2393 }
2394
2395 if (ISP_CAP_SCCFW(isp)) {
2396 lun = aep->at_scclun;
2397 } else {
2398 lun = aep->at_lun;
2399 }
2400 if (ISP_CAP_2KLOGIN(isp)) {
2401 nphdl = ((at2e_entry_t *)aep)->at_iid;
2402 } else {
2403 nphdl = aep->at_iid;
2404 }
2405 tptr = get_lun_statep(isp, 0, lun);
2406 if (tptr == NULL) {
2407 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2408 if (tptr == NULL) {
2409 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
2410 if (lun == 0) {
2411 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2412 } else {
2413 isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2414 }
2415 return;
2416 }
2417 }
2418
2419 /*
2420 * Start any commands pending resources first.
2421 */
2422 if (tptr->restart_queue) {
2423 inot_private_data_t *restart_queue = tptr->restart_queue;
2424 tptr->restart_queue = NULL;
2425 while (restart_queue) {
2426 ntp = restart_queue;
2427 restart_queue = ntp->rd.nt.nt_hba;
2428 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
2429 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2430 isp_put_ntpd(isp, tptr, ntp);
2431 /*
2432 * If a recursion caused the restart queue to start to fill again,
2433 * stop and splice the new list on top of the old list and restore
2434 * it and go to noresrc.
2435 */
2436 if (tptr->restart_queue) {
2437 ntp = tptr->restart_queue;
2438 tptr->restart_queue = restart_queue;
2439 while (restart_queue->rd.nt.nt_hba) {
2440 restart_queue = restart_queue->rd.nt.nt_hba;
2441 }
2442 restart_queue->rd.nt.nt_hba = ntp;
2443 goto noresrc;
2444 }
2445 }
2446 }
2447
2448 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2449 if (atiop == NULL) {
2450 goto noresrc;
2451 }
2452
2453 atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2454 if (atp == NULL) {
2455 goto noresrc;
2456 }
2457
2458 atp->state = ATPD_STATE_ATIO;
2459 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2460 tptr->atio_count--;
2461 isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
2462 atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2463 atiop->ccb_h.target_lun = lun;
2464
2465 /*
2466 * We don't get 'suggested' sense data as we do with SCSI cards.
2467 */
2468 atiop->sense_len = 0;
2469
2470 /*
2471 * If we're not in the port database, add ourselves.
2472 */
2473 if (IS_2100(isp))
2474 atiop->init_id = nphdl;
2475 else {
2476 if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
2477 lp->state == FC_PORTDB_STATE_ZOMBIE)) {
2478 uint64_t iid =
2479 (((uint64_t) aep->at_wwpn[0]) << 48) |
2480 (((uint64_t) aep->at_wwpn[1]) << 32) |
2481 (((uint64_t) aep->at_wwpn[2]) << 16) |
2482 (((uint64_t) aep->at_wwpn[3]) << 0);
2483 isp_add_wwn_entry(isp, 0, iid, nphdl, PORT_ANY, 0);
2484 isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
2485 }
2486 atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
2487 }
2488 atiop->cdb_len = ATIO2_CDBLEN;
2489 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2490 atiop->ccb_h.status = CAM_CDB_RECVD;
2491 atiop->tag_id = atp->tag;
2492 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2493 case ATIO2_TC_ATTR_SIMPLEQ:
2494 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2495 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2496 break;
2497 case ATIO2_TC_ATTR_HEADOFQ:
2498 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2499 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2500 break;
2501 case ATIO2_TC_ATTR_ORDERED:
2502 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2503 atiop->tag_action = MSG_ORDERED_Q_TAG;
2504 break;
2505 case ATIO2_TC_ATTR_ACAQ: /* ?? */
2506 case ATIO2_TC_ATTR_UNTAGGED:
2507 default:
2508 atiop->tag_action = 0;
2509 break;
2510 }
2511
2512 atp->orig_datalen = aep->at_datalen;
2513 atp->bytes_xfered = 0;
2514 atp->lun = lun;
2515 atp->nphdl = nphdl;
2516 atp->sid = PORT_ANY;
2517 atp->oxid = aep->at_oxid;
2518 atp->cdb0 = aep->at_cdb[0];
2519 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2520 atp->state = ATPD_STATE_CAM;
2521 xpt_done((union ccb *)atiop);
2522 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2523 rls_lun_statep(isp, tptr);
2524 return;
2525noresrc:
2526 ntp = isp_get_ntpd(isp, tptr);
2527 if (ntp == NULL) {
2528 rls_lun_statep(isp, tptr);
2529 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2530 return;
2531 }
2532 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2533 ntp->rd.nt.nt_hba = tptr->restart_queue;
2534 tptr->restart_queue = ntp;
2535 rls_lun_statep(isp, tptr);
2536}
2537
2538static void
2539isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2540{
2541 int cdbxlen;
2542 uint16_t lun, chan, nphdl = NIL_HANDLE;
2543 uint32_t did, sid;
2544 fcportdb_t *lp;
2545 tstate_t *tptr;
2546 struct ccb_accept_tio *atiop;
2547 atio_private_data_t *atp = NULL;
2548 atio_private_data_t *oatp;
2549 inot_private_data_t *ntp;
2550
2551 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2552 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2553 lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2554
2555 /*
2556 * Find the N-port handle, and Virtual Port Index for this command.
2557 *
2558 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2559 * We also, as a matter of course, need to know the WWN of the initiator too.
2560 */
2561 if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
2562 /*
2563 * Find the right channel based upon D_ID
2564 */
2565 isp_find_chan_by_did(isp, did, &chan);
2566
2567 if (chan == ISP_NOCHAN) {
2568 NANOTIME_T now;
2569
2570 /*
2571 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2572 * It's a bit tricky here as we need to stash this command *somewhere*.
2573 */
2574 GET_NANOTIME(&now);
2575 if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2576 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2577 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2578 return;
2579 }
2580 tptr = get_lun_statep(isp, 0, 0);
2581 if (tptr == NULL) {
2582 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2583 if (tptr == NULL) {
2584 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
2585 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2586 return;
2587 }
2588 }
2589 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2590 goto noresrc;
2591 }
2592 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
2593 } else {
2594 chan = 0;
2595 }
2596
2597 /*
2598 * Find the PDB entry for this initiator
2599 */
2600 if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2601 /*
2602 * If we're not in the port database terminate the exchange.
2603 */
2604 isp_prt(isp, ISP_LOGTINFO, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x wasn't in PDB already",
2605 __func__, aep->at_rxid, did, chan, sid);
2606 isp_dump_portdb(isp, chan);
2607 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2608 return;
2609 }
2610 nphdl = lp->handle;
2611
2612 /*
2613 * Get the tstate pointer
2614 */
2615 tptr = get_lun_statep(isp, chan, lun);
2616 if (tptr == NULL) {
2617 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2618 if (tptr == NULL) {
2619 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
2620 if (lun == 0) {
2621 isp_endcmd(isp, aep, nphdl, SCSI_STATUS_BUSY, 0);
2622 } else {
2623 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2624 }
2625 return;
2626 }
2627 }
2628
2629 /*
2630 * Start any commands pending resources first.
2631 */
2632 if (tptr->restart_queue) {
2633 inot_private_data_t *restart_queue = tptr->restart_queue;
2634 tptr->restart_queue = NULL;
2635 while (restart_queue) {
2636 ntp = restart_queue;
2637 restart_queue = ntp->rd.nt.nt_hba;
2638 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2639 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2640 isp_put_ntpd(isp, tptr, ntp);
2641 /*
2642 * If a recursion caused the restart queue to start to fill again,
2643 * stop and splice the new list on top of the old list and restore
2644 * it and go to noresrc.
2645 */
2646 if (tptr->restart_queue) {
2647 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
2648 if (restart_queue) {
2649 ntp = tptr->restart_queue;
2650 tptr->restart_queue = restart_queue;
2651 while (restart_queue->rd.nt.nt_hba) {
2652 restart_queue = restart_queue->rd.nt.nt_hba;
2653 }
2654 restart_queue->rd.nt.nt_hba = ntp;
2655 }
2656 goto noresrc;
2657 }
2658 }
2659 }
2660
2661 /*
2662 * If the f/w is out of resources, just send a BUSY status back.
2663 */
2664 if (aep->at_rxid == AT7_NORESRC_RXID) {
2665 rls_lun_statep(isp, tptr);
2666 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2667 return;
2668 }
2669
2670 /*
2671 * If we're out of resources, just send a BUSY status back.
2672 */
2673 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2674 if (atiop == NULL) {
2675 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2676 goto noresrc;
2677 }
2678
2679 oatp = isp_find_atpd(isp, tptr, aep->at_rxid);
2680 if (oatp) {
2681 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
2682 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
2683 /*
2684 * It's not a "no resource" condition- but we can treat it like one
2685 */
2686 goto noresrc;
2687 }
2688 atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2689 if (atp == NULL) {
2690 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2691 goto noresrc;
2692 }
2693 atp->word3 = lp->prli_word3;
2694 atp->state = ATPD_STATE_ATIO;
2695 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2696 tptr->atio_count--;
2697 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2698 atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
2699 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2700 atiop->ccb_h.target_lun = lun;
2701 atiop->sense_len = 0;
2702 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2703 if (cdbxlen) {
2704 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2705 }
2706 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2707 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2708 atiop->cdb_len = cdbxlen;
2709 atiop->ccb_h.status = CAM_CDB_RECVD;
2710 atiop->tag_id = atp->tag;
2711 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2712 case FCP_CMND_TASK_ATTR_SIMPLE:
2713 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2714 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2715 break;
2716 case FCP_CMND_TASK_ATTR_HEAD:
2717 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2718 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2719 break;
2720 case FCP_CMND_TASK_ATTR_ORDERED:
2721 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2722 atiop->tag_action = MSG_ORDERED_Q_TAG;
2723 break;
2724 default:
2725 /* FALLTHROUGH */
2726 case FCP_CMND_TASK_ATTR_ACA:
2727 case FCP_CMND_TASK_ATTR_UNTAGGED:
2728 atiop->tag_action = 0;
2729 break;
2730 }
2731 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2732 atp->bytes_xfered = 0;
2733 atp->lun = lun;
2734 atp->nphdl = nphdl;
2735 atp->portid = sid;
2736 atp->oxid = aep->at_hdr.ox_id;
2737 atp->rxid = aep->at_hdr.rx_id;
2738 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2739 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2740 atp->state = ATPD_STATE_CAM;
2741 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2742 xpt_done((union ccb *)atiop);
2743 rls_lun_statep(isp, tptr);
2744 return;
2745noresrc:
2746 if (atp) {
2747 isp_put_atpd(isp, tptr, atp);
2748 }
2749 ntp = isp_get_ntpd(isp, tptr);
2750 if (ntp == NULL) {
2751 rls_lun_statep(isp, tptr);
2752 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2753 return;
2754 }
2755 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2756 ntp->rd.nt.nt_hba = tptr->restart_queue;
2757 tptr->restart_queue = ntp;
2758 rls_lun_statep(isp, tptr);
2759}
2760
2761
2762/*
2763 * Handle starting an SRR (sequence retransmit request)
2764 * We get here when we've gotten the immediate notify
2765 * and the return of all outstanding CTIOs for this
2766 * transaction.
2767 */
2768static void
2769isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
2770{
2771 in_fcentry_24xx_t *inot;
2772 uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2773 union ccb *ccb;
2774
2775 inot = (in_fcentry_24xx_t *)atp->srr;
2776 srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2777 ccb = atp->srr_ccb;
2778 atp->srr_ccb = NULL;
2779 atp->nsrr++;
2780 if (ccb == NULL) {
2781 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2782 goto fail;
2783 }
2784
2785 ccb_off = ccb->ccb_h.spriv_field0;
2786 ccb_len = ccb->csio.dxfer_len;
2787 ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2788
2789 switch (inot->in_srr_iu) {
2790 case R_CTL_INFO_SOLICITED_DATA:
2791 /*
2792 * We have to restart a FCP_DATA data out transaction
2793 */
2794 atp->sendst = 0;
2795 atp->bytes_xfered = srr_off;
2796 if (ccb_len == 0) {
2797 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2798 goto mdp;
2799 }
2800 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
2801 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2802 goto mdp;
2803 }
2804 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2805 break;
2806 case R_CTL_INFO_COMMAND_STATUS:
2807 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2808 atp->sendst = 1;
2809 /*
2810 * We have to restart a FCP_RSP IU transaction
2811 */
2812 break;
2813 case R_CTL_INFO_DATA_DESCRIPTOR:
2814 /*
2815 * We have to restart an FCP DATA in transaction
2816 */
2817 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2818 goto fail;
2819
2820 default:
2821 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2822 goto fail;
2823 }
2824
2825 /*
2826 * We can't do anything until this is acked, so we might as well start it now.
2827 * We aren't going to do the usual asynchronous ack issue because we need
2828 * to make sure this gets on the wire first.
2829 */
2830 if (isp_notify_ack(isp, inot)) {
2831 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2832 goto fail;
2833 }
2834 isp_target_start_ctio(isp, ccb, FROM_SRR);
2835 return;
2836fail:
2837 inot->in_reserved = 1;
2838 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2839 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2840 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2841 isp_complete_ctio(ccb);
2842 return;
2843mdp:
2844 if (isp_notify_ack(isp, inot)) {
2845 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2846 goto fail;
2847 }
2848 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2849 ccb->ccb_h.status = CAM_MESSAGE_RECV;
2850 /*
2851 * This is not a strict interpretation of MDP, but it's close
2852 */
2853 ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2854 ccb->csio.msg_len = 7;
2855 ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2856 ccb->csio.msg_ptr[1] = 5;
2857 ccb->csio.msg_ptr[2] = 0; /* modify data pointer */
2858 ccb->csio.msg_ptr[3] = srr_off >> 24;
2859 ccb->csio.msg_ptr[4] = srr_off >> 16;
2860 ccb->csio.msg_ptr[5] = srr_off >> 8;
2861 ccb->csio.msg_ptr[6] = srr_off;
2862 isp_complete_ctio(ccb);
2863}
2864
2865
2866static void
2867isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2868{
2869 tstate_t *tptr;
2870 in_fcentry_24xx_t *inot = inot_raw;
2871 atio_private_data_t *atp;
2872 uint32_t tag = inot->in_rxid;
2873 uint32_t bus = inot->in_vpidx;
2874
2875 if (!IS_24XX(isp)) {
2876 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2877 return;
2878 }
2879
2880 tptr = get_lun_statep_from_tag(isp, bus, tag);
2881 if (tptr == NULL) {
2882 isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
2883 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2884 return;
2885 }
2886 atp = isp_find_atpd(isp, tptr, tag);
2887 if (atp == NULL) {
2888 rls_lun_statep(isp, tptr);
2889 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2890 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2891 return;
2892 }
2893 atp->srr_notify_rcvd = 1;
2894 memcpy(atp->srr, inot, sizeof (atp->srr));
2895 isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2896 inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2897 if (atp->srr_ccb)
2898 isp_handle_srr_start(isp, tptr, atp);
2899 rls_lun_statep(isp, tptr);
2900}
2901
2902static void
2903isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2904{
2905 union ccb *ccb;
2906 int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, failure = 0;
2907 tstate_t *tptr = NULL;
2908 atio_private_data_t *atp = NULL;
2909 int bus;
2910 uint32_t handle, moved_data = 0, data_requested;
2911
2912 /*
2913 * CTIO handles are 16 bits.
2914 * CTIO2 and CTIO7 are 32 bits.
2915 */
2916
2917 if (IS_SCSI(isp)) {
2918 handle = ((ct_entry_t *)arg)->ct_syshandle;
2919 } else {
2920 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2921 }
2922 ccb = isp_find_xs_tgt(isp, handle);
2923 if (ccb == NULL) {
2924 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2925 return;
2926 }
2927 isp_destroy_tgt_handle(isp, handle);
2928 data_requested = PISP_PCMD(ccb)->datalen;
2929 isp_free_pcmd(isp, ccb);
2930 if (isp->isp_nactive) {
2931 isp->isp_nactive--;
2932 }
2933
2934 bus = XS_CHANNEL(ccb);
2935 tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2936 if (tptr == NULL) {
2937 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2938 }
2939 if (tptr == NULL) {
2940 isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
2941 return;
2942 }
2943
2944 if (IS_24XX(isp)) {
2945 atp = isp_find_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
2946 } else if (IS_FC(isp)) {
2947 atp = isp_find_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
2948 } else {
2949 atp = isp_find_atpd(isp, tptr, ((ct_entry_t *)arg)->ct_fwhandle);
2950 }
2951 if (atp == NULL) {
2952 /*
2953 * XXX: isp_clear_commands() generates fake CTIO with zero
2954 * ct_rxid value, filling only ct_syshandle. Workaround
2955 * that using tag_id from the CCB, pointed by ct_syshandle.
2956 */
2957 atp = isp_find_atpd(isp, tptr, ccb->csio.tag_id);
2958 }
2959 if (atp == NULL) {
2960 rls_lun_statep(isp, tptr);
2961 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2962 return;
2963 }
2964 KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2965 atp->bytes_in_transit -= data_requested;
2966 atp->ctcnt -= 1;
2967 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2968
2969 if (IS_24XX(isp)) {
2970 ct7_entry_t *ct = arg;
2971
2972 if (ct->ct_nphdl == CT7_SRR) {
2973 atp->srr_ccb = ccb;
2974 if (atp->srr_notify_rcvd)
2975 isp_handle_srr_start(isp, tptr, atp);
2976 rls_lun_statep(isp, tptr);
2977 return;
2978 }
2979 if (ct->ct_nphdl == CT_HBA_RESET) {
2980 failure = CAM_UNREC_HBA_ERROR;
2981 } else {
2982 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2983 ok = (ct->ct_nphdl == CT7_OK);
2984 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2985 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2986 resid = ct->ct_resid;
2987 moved_data = data_requested - resid;
2988 }
2989 }
2990 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
2991 notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2992 } else if (IS_FC(isp)) {
2993 ct2_entry_t *ct = arg;
2994 if (ct->ct_status == CT_SRR) {
2995 atp->srr_ccb = ccb;
2996 if (atp->srr_notify_rcvd)
2997 isp_handle_srr_start(isp, tptr, atp);
2998 rls_lun_statep(isp, tptr);
2999 isp_target_putback_atio(ccb);
3000 return;
3001 }
3002 if (ct->ct_status == CT_HBA_RESET) {
3003 failure = CAM_UNREC_HBA_ERROR;
3004 } else {
3005 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
3006 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
3007 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
3008 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
3009 resid = ct->ct_resid;
3010 moved_data = data_requested - resid;
3011 }
3012 }
3013 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
3014 notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
3015 } else {
3016 ct_entry_t *ct = arg;
3017
3018 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
3019 failure = CAM_UNREC_HBA_ERROR;
3020 } else {
3021 sentstatus = ct->ct_flags & CT_SENDSTATUS;
3022 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
3023 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
3024 }
3025 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
3026 resid = ct->ct_resid;
3027 moved_data = data_requested - resid;
3028 }
3029 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__, ct->ct_fwhandle, ATPD_GET_SEQNO(ct),
3030 notify_cam, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
3031 }
3032 if (ok) {
3033 if (moved_data) {
3034 atp->bytes_xfered += moved_data;
3035 ccb->csio.resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
3036 }
3037 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
3038 ccb->ccb_h.status |= CAM_SENT_SENSE;
3039 }
3040 ccb->ccb_h.status |= CAM_REQ_CMP;
3041 } else {
3042 notify_cam = 1;
3043 if (failure == CAM_UNREC_HBA_ERROR)
3044 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
3045 else
3046 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
3047 }
3048 atp->state = ATPD_STATE_PDON;
3049 rls_lun_statep(isp, tptr);
3050
3051 /*
3052 * We never *not* notify CAM when there has been any error (ok == 0),
3053 * so we never need to do an ATIO putback if we're not notifying CAM.
3054 */
3055 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
3056 (sentstatus)? " FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
3057 if (notify_cam == 0) {
3058 if (atp->sendst) {
3059 isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
3060 }
3061 return;
3062 }
3063
3064 /*
3065 * We're telling CAM we're done with this CTIO transaction.
3066 *
3067 * 24XX cards never need an ATIO put back.
3068 *
3069 * Other cards need one put back only on error.
3070 * In the latter case, a timeout will re-fire
3071 * and try again in case we didn't have
3072 * queue resources to do so at first. In any case,
3073 * once the putback is done we do the completion
3074 * call.
3075 */
3076 if (ok || IS_24XX(isp)) {
3077 isp_complete_ctio(ccb);
3078 } else {
3079 isp_target_putback_atio(ccb);
3080 }
3081}
3082
3083static void
3084isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
3085{
3086 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3087}
3088
3089static void
3090isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
3091{
3092 int needack = 1;
3093 switch (inp->in_status) {
3094 case IN_PORT_LOGOUT:
3095 /*
3096 * XXX: Need to delete this initiator's WWN from the database
3097 * XXX: Need to send this LOGOUT upstream
3098 */
3099 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
3100 break;
3101 case IN_PORT_CHANGED:
3102 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
3103 break;
3104 case IN_GLOBAL_LOGO:
3105 isp_del_all_wwn_entries(isp, 0);
3106 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
3107 break;
3108 case IN_ABORT_TASK:
3109 {
3110 tstate_t *tptr;
3111 uint16_t lun;
3112 uint32_t loopid, sid;
3113 uint64_t wwn;
3114 atio_private_data_t *atp;
3115 fcportdb_t *lp;
3116 struct ccb_immediate_notify *inot = NULL;
3117
3118 if (ISP_CAP_SCCFW(isp)) {
3119 lun = inp->in_scclun;
3120 } else {
3121 lun = inp->in_lun;
3122 }
3123 if (ISP_CAP_2KLOGIN(isp)) {
3124 loopid = ((in_fcentry_e_t *)inp)->in_iid;
3125 } else {
3126 loopid = inp->in_iid;
3127 }
3128 if (isp_find_pdb_by_handle(isp, 0, loopid, &lp)) {
3129 wwn = lp->port_wwn;
3130 sid = lp->portid;
3131 } else {
3132 wwn = INI_ANY;
3133 sid = PORT_ANY;
3134 }
3135 tptr = get_lun_statep(isp, 0, lun);
3136 if (tptr == NULL) {
3137 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
3138 if (tptr == NULL) {
3139 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
3140 return;
3141 }
3142 }
3143 atp = isp_find_atpd(isp, tptr, inp->in_seqid);
3144
3145 if (atp) {
3146 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
3147 isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
3148 if (inot) {
3149 tptr->inot_count--;
3150 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3151 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3152 } else {
3153 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "out of INOT structures\n");
3154 }
3155 } else {
3156 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
3157 }
3158 if (inot) {
3159 isp_notify_t tmp, *nt = &tmp;
3160 ISP_MEMZERO(nt, sizeof (isp_notify_t));
3161 nt->nt_hba = isp;
3162 nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
3163 nt->nt_wwn = wwn;
3164 nt->nt_nphdl = loopid;
3165 nt->nt_sid = sid;
3166 nt->nt_did = PORT_ANY;
3167 nt->nt_lun = lun;
3168 nt->nt_need_ack = 1;
3169 nt->nt_channel = 0;
3170 nt->nt_ncode = NT_ABORT_TASK;
3171 nt->nt_lreserved = inot;
3172 isp_handle_platform_target_tmf(isp, nt);
3173 needack = 0;
3174 }
3175 rls_lun_statep(isp, tptr);
3176 break;
3177 }
3178 default:
3179 break;
3180 }
3181 if (needack) {
3182 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
3183 }
3184}
3185
3186static void
3187isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
3188{
3189 uint16_t nphdl;
3190 uint16_t prli_options = 0;
3191 uint32_t portid;
3192 fcportdb_t *lp;
3193 uint8_t *ptr = NULL;
3194 uint64_t wwn;
3195
3196 nphdl = inot->in_nphdl;
3197 if (nphdl != NIL_HANDLE) {
3198 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
3199 } else {
3200 portid = PORT_ANY;
3201 }
3202
3203 switch (inot->in_status) {
3204 case IN24XX_ELS_RCVD:
3205 {
3206 char buf[16], *msg;
3207 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
3208
3209 /*
3210 * Note that we're just getting notification that an ELS was received
3211 * (possibly with some associated information sent upstream). This is
3212 * *not* the same as being given the ELS frame to accept or reject.
3213 */
3214 switch (inot->in_status_subcode) {
3215 case LOGO:
3216 msg = "LOGO";
3217 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
3218 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
3219 wwn = (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF]) << 56) |
3220 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
3221 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
3222 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
3223 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
3224 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
3225 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) << 8) |
3226 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
3227 } else {
3228 wwn = INI_ANY;
3229 }
3230 isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
3231 break;
3232 case PRLO:
3233 msg = "PRLO";
3234 break;
3235 case PLOGI:
3236 case PRLI:
3237 /*
3238 * Treat PRLI the same as PLOGI and make a database entry for it.
3239 */
3240 if (inot->in_status_subcode == PLOGI) {
3241 msg = "PLOGI";
3242 } else {
3243 prli_options = inot->in_prli_options;
3244 msg = "PRLI";
3245 }
3246 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
3247 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
3248 wwn = (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF]) << 56) |
3249 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
3250 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
3251 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
3252 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
3253 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
3254 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) << 8) |
3255 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
3256 } else {
3257 wwn = INI_NONE;
3258 }
3259 isp_add_wwn_entry(isp, chan, wwn, nphdl, portid, prli_options);
3260 break;
3261 case PDISC:
3262 msg = "PDISC";
3263 break;
3264 case ADISC:
3265 msg = "ADISC";
3266 break;
3267 default:
3268 ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
3269 msg = buf;
3270 break;
3271 }
3272 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
3273 isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
3274 break;
3275 }
3276 isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
3277 inot->in_rxid, inot->in_oxid);
3278 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3279 break;
3280 }
3281
3282 case IN24XX_PORT_LOGOUT:
3283 ptr = "PORT LOGOUT";
3284 if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
3285 isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
3286 }
3287 /* FALLTHROUGH */
3288 case IN24XX_PORT_CHANGED:
3289 if (ptr == NULL) {
3290 ptr = "PORT CHANGED";
3291 }
3292 /* FALLTHROUGH */
3293 case IN24XX_LIP_RESET:
3294 if (ptr == NULL) {
3295 ptr = "LIP RESET";
3296 }
3297 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr, inot->in_status_subcode, nphdl);
3298
3299 /*
3300 * All subcodes here are irrelevant. What is relevant
3301 * is that we need to terminate all active commands from
3302 * this initiator (known by N-port handle).
3303 */
3304 /* XXX IMPLEMENT XXX */
3305 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3306 break;
3307
3308 case IN24XX_SRR_RCVD:
3309#ifdef ISP_TARGET_MODE
3310 isp_handle_srr_notify(isp, inot);
3311 break;
3312#else
3313 if (ptr == NULL) {
3314 ptr = "SRR RCVD";
3315 }
3316 /* FALLTHROUGH */
3317#endif
3318 case IN24XX_LINK_RESET:
3319 if (ptr == NULL) {
3320 ptr = "LINK RESET";
3321 }
3322 case IN24XX_LINK_FAILED:
3323 if (ptr == NULL) {
3324 ptr = "LINK FAILED";
3325 }
3326 default:
3327 isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr);
3328 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3329 break;
3330 }
3331}
3332
3333static int
3334isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
3335{
3336
3337 if (isp->isp_state != ISP_RUNSTATE) {
3338 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
3339 return (0);
3340 }
3341
3342 /*
3343 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
3344 */
3345 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
3346 ct7_entry_t local, *cto = &local;
3347 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
3348 fcportdb_t *lp;
3349 uint32_t sid;
3350 uint16_t nphdl;
3351
3352 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
3353 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
3354 nphdl = lp->handle;
3355 } else {
3356 nphdl = NIL_HANDLE;
3357 }
3358 ISP_MEMZERO(&local, sizeof (local));
3359 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
3360 cto->ct_header.rqs_entry_count = 1;
3361 cto->ct_nphdl = nphdl;
3362 cto->ct_rxid = aep->at_rxid;
3363 cto->ct_vpidx = mp->nt_channel;
3364 cto->ct_iid_lo = sid;
3365 cto->ct_iid_hi = sid >> 16;
3366 cto->ct_oxid = aep->at_hdr.ox_id;
3367 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
3368 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
3369 return (isp_target_put_entry(isp, &local));
3370 }
3371
3372 /*
3373 * This case is for a responding to an ABTS frame
3374 */
3375 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3376
3377 /*
3378 * Overload nt_need_ack here to mark whether we've terminated the associated command.
3379 */
3380 if (mp->nt_need_ack) {
3381 uint8_t storage[QENTRY_LEN];
3382 ct7_entry_t *cto = (ct7_entry_t *) storage;
3383 abts_t *abts = (abts_t *)mp->nt_lreserved;
3384
3385 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
3386 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
3387 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
3388 cto->ct_header.rqs_entry_count = 1;
3389 cto->ct_nphdl = mp->nt_nphdl;
3390 cto->ct_rxid = abts->abts_rxid_task;
3391 cto->ct_iid_lo = mp->nt_sid;
3392 cto->ct_iid_hi = mp->nt_sid >> 16;
3393 cto->ct_oxid = abts->abts_ox_id;
3394 cto->ct_vpidx = mp->nt_channel;
3395 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
3396 if (isp_target_put_entry(isp, cto)) {
3397 return (ENOMEM);
3398 }
3399 mp->nt_need_ack = 0;
3400 }
3401 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
3402 return (ENOMEM);
3403 } else {
3404 return (0);
3405 }
3406 }
3407
3408 /*
3409 * Handle logout cases here
3410 */
3411 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
3412 isp_del_all_wwn_entries(isp, mp->nt_channel);
3413 }
3414
3415 if (mp->nt_ncode == NT_LOGOUT) {
3416 if (!IS_2100(isp) && IS_FC(isp)) {
3417 isp_del_wwn_entries(isp, mp);
3418 }
3419 }
3420
3421 /*
3422 * General purpose acknowledgement
3423 */
3424 if (mp->nt_need_ack) {
3425 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
3426 /*
3427 * Don't need to use the guaranteed send because the caller can retry
3428 */
3429 return (isp_notify_ack(isp, mp->nt_lreserved));
3430 }
3431 return (0);
3432}
3433
3434/*
3435 * Handle task management functions.
3436 *
3437 * We show up here with a notify structure filled out.
3438 *
3439 * The nt_lreserved tag points to the original queue entry
3440 */
3441static void
3442isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
3443{
3444 tstate_t *tptr;
3445 fcportdb_t *lp;
3446 struct ccb_immediate_notify *inot;
3447 inot_private_data_t *ntp = NULL;
3448 lun_id_t lun;
3449
3450 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
3451 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
3452 /*
3453 * NB: This assignment is necessary because of tricky type conversion.
3454 * XXX: This is tricky and I need to check this. If the lun isn't known
3455 * XXX: for the task management function, it does not of necessity follow
3456 * XXX: that it should go up stream to the wildcard listener.
3457 */
3458 if (notify->nt_lun == LUN_ANY) {
3459 lun = CAM_LUN_WILDCARD;
3460 } else {
3461 lun = notify->nt_lun;
3462 }
3463 tptr = get_lun_statep(isp, notify->nt_channel, lun);
3464 if (tptr == NULL) {
3465 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
3466 if (tptr == NULL) {
3467 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
3468 goto bad;
3469 }
3470 }
3471 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
3472 if (inot == NULL) {
3473 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
3474 goto bad;
3475 }
3476
3477 if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
3478 isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
3479 inot->initiator_id = CAM_TARGET_WILDCARD;
3480 } else {
3481 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
3482 }
3483 inot->seq_id = notify->nt_tagval;
3484 inot->tag_id = notify->nt_tagval >> 32;
3485
3486 switch (notify->nt_ncode) {
3487 case NT_ABORT_TASK:
3488 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
3489 inot->arg = MSG_ABORT_TASK;
3490 break;
3491 case NT_ABORT_TASK_SET:
3492 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
3493 inot->arg = MSG_ABORT_TASK_SET;
3494 break;
3495 case NT_CLEAR_ACA:
3496 inot->arg = MSG_CLEAR_ACA;
3497 break;
3498 case NT_CLEAR_TASK_SET:
3499 inot->arg = MSG_CLEAR_TASK_SET;
3500 break;
3501 case NT_LUN_RESET:
3502 inot->arg = MSG_LOGICAL_UNIT_RESET;
3503 break;
3504 case NT_TARGET_RESET:
3505 inot->arg = MSG_TARGET_RESET;
3506 break;
3507 case NT_QUERY_TASK_SET:
3508 inot->arg = MSG_QUERY_TASK_SET;
3509 break;
3510 case NT_QUERY_ASYNC_EVENT:
3511 inot->arg = MSG_QUERY_ASYNC_EVENT;
3512 break;
3513 default:
3514 isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun 0x%x", __func__, notify->nt_ncode, notify->nt_channel, lun);
3515 goto bad;
3516 }
3517
3518 ntp = isp_get_ntpd(isp, tptr);
3519 if (ntp == NULL) {
3520 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
3521 goto bad;
3522 }
3523 ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
3524 if (notify->nt_lreserved) {
3525 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
3526 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
3527 }
3528 ntp->rd.seq_id = notify->nt_tagval;
3529 ntp->rd.tag_id = notify->nt_tagval >> 32;
3530
3531 tptr->inot_count--;
3532 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3533 rls_lun_statep(isp, tptr);
3534 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3535 inot->ccb_h.status = CAM_MESSAGE_RECV;
3536 xpt_done((union ccb *)inot);
3537 return;
3538bad:
3539 if (tptr) {
3540 rls_lun_statep(isp, tptr);
3541 }
3542 if (notify->nt_need_ack && notify->nt_lreserved) {
3543 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3544 if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
3545 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
3546 }
3547 } else {
3548 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
3549 }
3550 }
3551}
3552
3553/*
3554 * Find the associated private data and mark it as dead so
3555 * we don't try to work on it any further.
3556 */
3557static void
3558isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
3559{
3560 tstate_t *tptr;
3561 atio_private_data_t *atp;
3562 union ccb *accb = ccb->cab.abort_ccb;
3563
3564 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3565 if (tptr == NULL) {
3566 tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
3567 if (tptr == NULL) {
3568 ccb->ccb_h.status = CAM_REQ_INVALID;
3569 return;
3570 }
3571 }
3572
3573 atp = isp_find_atpd(isp, tptr, accb->atio.tag_id);
3574 if (atp == NULL) {
3575 ccb->ccb_h.status = CAM_REQ_INVALID;
3576 } else {
3577 atp->dead = 1;
3578 ccb->ccb_h.status = CAM_REQ_CMP;
3579 }
3580 rls_lun_statep(isp, tptr);
3581}
3582
3583static void
3584isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
3585{
3586 atio_private_data_t *atp;
3587 inot_private_data_t *restart_queue = tptr->restart_queue;
3588
3589 /*
3590 * First, clean any commands pending restart
3591 */
3592 tptr->restart_queue = NULL;
3593 while (restart_queue) {
3594 uint32_t this_tag_id;
3595 inot_private_data_t *ntp = restart_queue;
3596
3597 restart_queue = ntp->rd.nt.nt_hba;
3598
3599 if (IS_24XX(isp)) {
3600 this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
3601 } else {
3602 this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
3603 }
3604 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
3605 isp_put_ntpd(isp, tptr, ntp);
3606 } else {
3607 ntp->rd.nt.nt_hba = tptr->restart_queue;
3608 tptr->restart_queue = ntp;
3609 }
3610 }
3611
3612 /*
3613 * Now mark other ones dead as well.
3614 */
3615 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3616 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3617 atp->dead = 1;
3618 }
3619 }
3620}
3621
3622
3623#ifdef ISP_INTERNAL_TARGET
3624//#define ISP_SEPARATE_STATUS 1
3625#define ISP_MULTI_CCBS 1
3626#if defined(ISP_MULTI_CCBS) && !defined(ISP_SEPARATE_STATUS)
3627#define ISP_SEPARATE_STATUS 1
3628#endif
3629
3630typedef struct periph_private_data_t {
3631 union ccb *ccb; /* original ATIO or Immediate Notify */
3632 unsigned long offset; /* current offset */
3633 int sequence; /* current CTIO sequence */
3634 int ctio_cnt; /* current # of ctio's outstanding */
3635 int
3636 status_sent : 1,
3637 on_queue : 1; /* on restart queue */
3638} ppd_t;
3639/*
3640 * Each ATIO we allocate will have periph private data associated with it
3641 * that maintains per-command state. This private to each ATIO.
3642 */
3643#define ATIO_PPD(ccb) ((ppd_t *)(((struct ccb_hdr *)ccb)->ppriv_ptr0))
3644/*
3645 * Each CTIO we send downstream will get a pointer to the ATIO itself
3646 * so that on completion we can retrieve that pointer.
3647 */
3648#define ccb_atio ppriv_ptr1
3649#define ccb_inot ppriv_ptr1
3650
3651/*
3652 * Each CTIO we send downstream will contain a sequence number
3653 */
3654#define CTIO_SEQ(ccb) ccb->ccb_h.ppriv_field0
3655
3656#define MAX_ISP_TARG_TRANSFER (2 << 20)
3657#define NISP_TARG_CMDS 64
3658#define NISP_TARG_NOTIFIES 64
3659#define DISK_SHIFT 9
3660#define JUNK_SIZE 256
3661#define MULTI_CCB_DATA_LIM 8192
3662//#define MULTI_CCB_DATA_CNT 64
3663#define MULTI_CCB_DATA_CNT 8
3664
3665extern u_int vm_kmem_size;
3666static int ca;
3667static uint32_t disk_size;
3668static uint8_t *disk_data = NULL;
3669static uint8_t *junk_data;
3670static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3671struct isptarg_softc {
3672 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3673 struct isp_ccbq work_queue;
3674 struct isp_ccbq rework_queue;
3675 struct isp_ccbq running_queue;
3676 struct isp_ccbq inot_queue;
3677 struct cam_periph *periph;
3678 struct cam_path *path;
3679 ispsoftc_t *isp;
3680};
3681static periph_ctor_t isptargctor;
3682static periph_dtor_t isptargdtor;
3683static periph_start_t isptargstart;
3684static periph_init_t isptarginit;
3685static void isptarg_done(struct cam_periph *, union ccb *);
3686static void isptargasync(void *, u_int32_t, struct cam_path *, void *);
3687
3688
3689static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3690
3691static struct periph_driver isptargdriver =
3692{
3693 isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), 0
3694};
3695
3696static void
3697isptarginit(void)
3698{
3699}
3700
3701static void
3702isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3703{
3704 struct ccb_notify_acknowledge *ack = &iccb->cna2;
3705
3706 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: [0x%x] immediate notify for 0x%x from 0x%x status 0x%x arg 0x%x\n", __func__,
3707 inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3708 ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3709 ack->ccb_h.flags = 0;
3710 ack->ccb_h.retry_count = 0;
3711 ack->ccb_h.cbfcnp = isptarg_done;
3712 ack->ccb_h.timeout = 0;
3713 ack->ccb_h.ccb_inot = inot;
3714 ack->tag_id = inot->tag_id;
3715 ack->seq_id = inot->seq_id;
3716 ack->initiator_id = inot->initiator_id;
3717 xpt_action(iccb);
3718}
3719
3720static void
3721isptargstart(struct cam_periph *periph, union ccb *iccb)
3722{
3723 const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3724 0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3725 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3726 'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3727 'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3728 '0', '0', '0', '1'
3729 };
3730 const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3731 0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3732 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3733 'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3734 'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3735 '0', '0', '0', '1'
3736 };
3737 int r, i, more = 0, last, is_data_cmd = 0, is_write;
3738 char *queue;
3739 struct isptarg_softc *softc = periph->softc;
3740 struct ccb_scsiio *csio;
3741 lun_id_t return_lun;
3742 struct ccb_accept_tio *atio;
3743 uint8_t *cdb, *ptr, status;
3744 uint8_t *data_ptr;
3745 uint32_t data_len, flags;
3746 struct ccb_hdr *ccbh;
3747
3748 mtx_assert(periph->sim->mtx, MA_OWNED);
3749 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
3750 TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3751 /*
3752 * Check for immediate notifies first
3753 */
3754 ccbh = TAILQ_FIRST(&softc->inot_queue);
3755 if (ccbh) {
3756 TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3757 if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3758 xpt_schedule(periph, 1);
3759 }
3760 isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3761 return;
3762 }
3763
3764 /*
3765 * Check the rework (continuation) work queue first.
3766 */
3767 ccbh = TAILQ_FIRST(&softc->rework_queue);
3768 if (ccbh) {
3769 atio = (struct ccb_accept_tio *)ccbh;
3770 TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3771 more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3772 queue = "rework";
3773 } else {
3774 ccbh = TAILQ_FIRST(&softc->work_queue);
3775 if (ccbh == NULL) {
3776 xpt_release_ccb(iccb);
3777 return;
3778 }
3779 atio = (struct ccb_accept_tio *)ccbh;
3780 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3781 more = TAILQ_FIRST(&softc->work_queue) != NULL;
3782 queue = "work";
3783 }
3784 ATIO_PPD(atio)->on_queue = 0;
3785
3786 if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3787 panic("BAD ATIO");
3788 }
3789
3790 data_len = is_write = 0;
3791 data_ptr = NULL;
3792 csio = &iccb->csio;
3793 status = SCSI_STATUS_OK;
3794 flags = CAM_SEND_STATUS;
3795 memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3796 cdb = atio->cdb_io.cdb_bytes;
3797 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from %s queue initiator 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id,
3798 queue, atio->init_id, cdb[0], ATIO_PPD(atio)->offset);
3799
3800 return_lun = XS_LUN(atio);
3801 if (return_lun != 0) {
3802 xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3803 if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3804 status = SCSI_STATUS_CHECK_COND;
3805 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3806 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3807 SDFIXED(atio->sense_data)->add_sense_code = 0x25; /* LOGICAL UNIT NOT SUPPORTED */
3808 atio->sense_len = SSD_MIN_SIZE;
3809 }
3810 return_lun = CAM_LUN_WILDCARD;
3811 }
3812
3813 switch (cdb[0]) {
3814 case REQUEST_SENSE:
3815 flags |= CAM_DIR_IN;
3816 data_len = sizeof (atio->sense_data);
3817 junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3818 memset(junk_data+1, 0, data_len-1);
3819 if (data_len > cdb[4]) {
3820 data_len = cdb[4];
3821 }
3822 if (data_len) {
3823 data_ptr = junk_data;
3824 }
3825 break;
3826 case WRITE_6:
3827 case WRITE_10:
3828 case WRITE_12:
3829 case WRITE_16:
3830 is_write = 1;
3831 /* FALLTHROUGH */
3832 case READ_6:
3833 case READ_10:
3834 case READ_12:
3835 case READ_16:
3836 is_data_cmd = 1;
3837 r = isptarg_rwparm(cdb, disk_data, disk_size, ATIO_PPD(atio)->offset, &data_ptr, &data_len, &last);
3838 if (r != 0) {
3839 status = SCSI_STATUS_CHECK_COND;
3840 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3841 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3842 if (r == -1) {
3843 SDFIXED(atio->sense_data)->add_sense_code = 0x21; /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
3844 } else {
3845 SDFIXED(atio->sense_data)->add_sense_code = 0x20; /* INVALID COMMAND OPERATION CODE */
3846 }
3847 atio->sense_len = SSD_MIN_SIZE;
3848 } else {
3849#ifdef ISP_SEPARATE_STATUS
3850 if (last && data_len) {
3851 last = 0;
3852 }
3853#endif
3854 if (last == 0) {
3855 flags &= ~CAM_SEND_STATUS;
3856 }
3857 if (data_len) {
3858 ATIO_PPD(atio)->offset += data_len;
3859 if (is_write)
3860 flags |= CAM_DIR_OUT;
3861 else
3862 flags |= CAM_DIR_IN;
3863 } else {
3864 flags |= CAM_DIR_NONE;
3865 }
3866 }
3867 break;
3868 case INQUIRY:
3869 flags |= CAM_DIR_IN;
3870 if (cdb[1] || cdb[2] || cdb[3]) {
3871 status = SCSI_STATUS_CHECK_COND;
3872 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3873 SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
3874 SDFIXED(atio->sense_data)->add_sense_code = 0x24; /* INVALID FIELD IN CDB */
3875 atio->sense_len = SSD_MIN_SIZE;
3876 break;
3877 }
3878 data_len = sizeof (iqd);
3879 if (data_len > cdb[4]) {
3880 data_len = cdb[4];
3881 }
3882 if (data_len) {
3883 if (XS_LUN(iccb) != 0) {
3884 memcpy(junk_data, niliqd, sizeof (iqd));
3885 } else {
3886 memcpy(junk_data, iqd, sizeof (iqd));
3887 }
3888 data_ptr = junk_data;
3889 }
3890 break;
3891 case TEST_UNIT_READY:
3892 flags |= CAM_DIR_NONE;
3893 if (ca) {
3894 ca = 0;
3895 status = SCSI_STATUS_CHECK_COND;
3896 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3897 SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
3898 SDFIXED(atio->sense_data)->add_sense_code = 0x29; /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
3899 atio->sense_len = SSD_MIN_SIZE;
3900 }
3901 break;
3902 case SYNCHRONIZE_CACHE:
3903 case START_STOP:
3904 case RESERVE:
3905 case RELEASE:
3906 case VERIFY_10:
3907 flags |= CAM_DIR_NONE;
3908 break;
3909
3910 case READ_CAPACITY:
3911 flags |= CAM_DIR_IN;
3912 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3913 status = SCSI_STATUS_CHECK_COND;
3914 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3915 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3916 SDFIXED(atio->sense_data)->add_sense_code = 0x24; /* INVALID FIELD IN CDB */
3917 atio->sense_len = SSD_MIN_SIZE;
3918 break;
3919 }
3920 if (cdb[8] & 0x1) { /* PMI */
3921 junk_data[0] = 0xff;
3922 junk_data[1] = 0xff;
3923 junk_data[2] = 0xff;
3924 junk_data[3] = 0xff;
3925 } else {
3926 uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3927 if (last_blk < 0xffffffffULL) {
3928 junk_data[0] = (last_blk >> 24) & 0xff;
3929 junk_data[1] = (last_blk >> 16) & 0xff;
3930 junk_data[2] = (last_blk >> 8) & 0xff;
3931 junk_data[3] = (last_blk) & 0xff;
3932 } else {
3933 junk_data[0] = 0xff;
3934 junk_data[1] = 0xff;
3935 junk_data[2] = 0xff;
3936 junk_data[3] = 0xff;
3937 }
3938 }
3939 junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3940 junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3941 junk_data[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
3942 junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3943 data_ptr = junk_data;
3944 data_len = 8;
3945 break;
3946 case REPORT_LUNS:
3947 flags |= CAM_DIR_IN;
3948 memset(junk_data, 0, JUNK_SIZE);
3949 junk_data[0] = (1 << 3) >> 24;
3950 junk_data[1] = (1 << 3) >> 16;
3951 junk_data[2] = (1 << 3) >> 8;
3952 junk_data[3] = (1 << 3);
3953 ptr = NULL;
3954 for (i = 0; i < 1; i++) {
3955 ptr = &junk_data[8 + (i << 3)];
3956 if (i >= 256) {
3957 ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3958 }
3959 ptr[1] = i;
3960 }
3961 data_ptr = junk_data;
3962 data_len = (ptr + 8) - junk_data;
3963 break;
3964
3965 default:
3966 flags |= CAM_DIR_NONE;
3967 status = SCSI_STATUS_CHECK_COND;
3968 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3969 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3970 SDFIXED(atio->sense_data)->add_sense_code = 0x20; /* INVALID COMMAND OPERATION CODE */
3971 atio->sense_len = SSD_MIN_SIZE;
3972 break;
3973 }
3974
3975 /*
3976 * If we are done with the transaction, tell the
3977 * controller to send status and perform a CMD_CMPLT.
3978 * If we have associated sense data, see if we can
3979 * send that too.
3980 */
3981 if (status == SCSI_STATUS_CHECK_COND) {
3982 flags |= CAM_SEND_SENSE;
3983 csio->sense_len = atio->sense_len;
3984 csio->sense_data = atio->sense_data;
3985 flags &= ~CAM_DIR_MASK;
3986 data_len = 0;
3987 data_ptr = NULL;
3988 }
3989 cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 30 * hz);
3990 iccb->ccb_h.target_id = atio->ccb_h.target_id;
3991 iccb->ccb_h.target_lun = return_lun;
3992 iccb->ccb_h.ccb_atio = atio;
3993 CTIO_SEQ(iccb) = ATIO_PPD(atio)->sequence++;
3994 ATIO_PPD(atio)->ctio_cnt++;
3995 if (flags & CAM_SEND_STATUS) {
3996 KASSERT((ATIO_PPD(atio)->status_sent == 0), ("we have already sent status for 0x%x in %s", atio->tag_id, __func__));
3997 ATIO_PPD(atio)->status_sent = 1;
3998 }
3999 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, atio->ccb_h.path, "%s: sending downstream for 0x%x sequence %u len %u flags %x\n", __func__, atio->tag_id, CTIO_SEQ(iccb), data_len, flags);
4000 xpt_action(iccb);
4001
4002 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4003 cam_release_devq(periph->path, 0, 0, 0, 0);
4004 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
4005 }
4006#ifdef ISP_MULTI_CCBS
4007 if (is_data_cmd && ATIO_PPD(atio)->status_sent == 0 && ATIO_PPD(atio)->ctio_cnt < MULTI_CCB_DATA_CNT && ATIO_PPD(atio)->on_queue == 0) {
4008 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, atio->ccb_h.path, "%s: more still to do for 0x%x\n", __func__, atio->tag_id);
4009 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
4010 ATIO_PPD(atio)->on_queue = 1;
4011 more = 1;
4012 }
4013#endif
4014 if (more) {
4015 xpt_schedule(periph, 1);
4016 }
4017}
4018
4019static cam_status
4020isptargctor(struct cam_periph *periph, void *arg)
4021{
4022 struct isptarg_softc *softc;
4023
4024 softc = (struct isptarg_softc *)arg;
4025 periph->softc = softc;
4026 softc->periph = periph;
4027 softc->path = periph->path;
4028 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
4029 return (CAM_REQ_CMP);
4030}
4031
4032static void
4033isptargdtor(struct cam_periph *periph)
4034{
4035 struct isptarg_softc *softc;
4036 softc = (struct isptarg_softc *)periph->softc;
4037 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
4038 softc->periph = NULL;
4039 softc->path = NULL;
4040 periph->softc = NULL;
4041}
4042
4043static void
4044isptarg_done(struct cam_periph *periph, union ccb *ccb)
4045{
4046 struct isptarg_softc *softc;
4047 ispsoftc_t *isp;
4048 uint32_t newoff;
4049 struct ccb_accept_tio *atio;
4050 struct ccb_immediate_notify *inot;
4051 cam_status status;
4052
4053 softc = (struct isptarg_softc *)periph->softc;
4054 isp = softc->isp;
4055 status = ccb->ccb_h.status & CAM_STATUS_MASK;
4056
4057 switch (ccb->ccb_h.func_code) {
4058 case XPT_ACCEPT_TARGET_IO:
4059 atio = (struct ccb_accept_tio *) ccb;
4060 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
4061 memset(ATIO_PPD(atio), 0, sizeof (ppd_t));
4062 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
4063 ATIO_PPD(atio)->on_queue = 1;
4064 xpt_schedule(periph, 1);
4065 break;
4066 case XPT_IMMEDIATE_NOTIFY:
4067 inot = (struct ccb_immediate_notify *) ccb;
4068 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
4069 TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
4070 xpt_schedule(periph, 1);
4071 break;
4072 case XPT_CONT_TARGET_IO:
4073 atio = ccb->ccb_h.ccb_atio;
4074 KASSERT((ATIO_PPD(atio)->ctio_cnt != 0), ("ctio zero when finishing a CTIO"));
4075 ATIO_PPD(atio)->ctio_cnt--;
4076 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4077 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
4078 case CAM_MESSAGE_RECV:
4079 newoff = (ccb->csio.msg_ptr[3] << 24) | (ccb->csio.msg_ptr[4] << 16) | (ccb->csio.msg_ptr[5] << 8) | (ccb->csio.msg_ptr[6]);
4080 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "[0x%x] got message to return to reset offset to 0x%x at sequence %u\n", atio->tag_id, newoff, CTIO_SEQ(ccb));
4081 ATIO_PPD(atio)->offset = newoff;
4082 ATIO_PPD(atio)->status_sent = 0;
4083 if (ATIO_PPD(atio)->on_queue == 0) {
4084 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
4085 ATIO_PPD(atio)->on_queue = 1;
4086 }
4087 xpt_schedule(periph, 1);
4088 break;
4089 default:
4090 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
4091 xpt_action((union ccb *)atio);
4092 break;
4093 }
4094 } else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
4095 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO sequence %u seen in %s\n", atio->tag_id, CTIO_SEQ(ccb), __func__);
4096 if (ATIO_PPD(atio)->status_sent == 0 && ATIO_PPD(atio)->on_queue == 0) {
4097 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
4098 ATIO_PPD(atio)->on_queue = 1;
4099 }
4100 xpt_schedule(periph, 1);
4101 } else {
4102 KASSERT((ATIO_PPD(atio)->ctio_cnt == 0), ("ctio count still %d when we think we've sent the STATUS ctio", ATIO_PPD(atio)->ctio_cnt));
4103 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO sequence %u seen in %s\n", atio->tag_id, CTIO_SEQ(ccb), __func__);
4104 xpt_action((union ccb *)atio);
4105 }
4106 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4107 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
4108 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
4109 }
4110 xpt_release_ccb(ccb);
4111 break;
4112 case XPT_NOTIFY_ACKNOWLEDGE:
4113 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4114 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
4115 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
4116 }
4117 inot = ccb->ccb_h.ccb_inot;
4118 ISP_PATH_PRT(isp, ISP_LOGTDEBUG1, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
4119 xpt_release_ccb(ccb);
4120 xpt_action((union ccb *)inot);
4121 break;
4122 default:
4123 xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
4124 break;
4125 }
4126}
4127
4128static void
4129isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
4130{
4131 struct ac_contract *acp = arg;
4132 struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
4133
4134 if (code != AC_CONTRACT) {
4135 return;
4136 }
4137 xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
4138}
4139
4140static void
4141isp_target_thread(ispsoftc_t *isp, int chan)
4142{
4143 union ccb *ccb = NULL;
4144 int i;
4145 void *wchan;
4146 cam_status status;
4147 struct isptarg_softc *softc = NULL;
4148 struct cam_periph *periph = NULL, *wperiph = NULL;
4149 struct cam_path *path, *wpath;
4150 struct cam_sim *sim;
4151
4152 if (disk_data == NULL) {
4153 disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
4154 if (disk_size < (50 << 20)) {
4155 disk_size = 50 << 20;
4156 }
4157 disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
4158 if (disk_data == NULL) {
4159 isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
4160 goto out;
4161 }
4162 isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
4163 }
4164 junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
4165 if (junk_data == NULL) {
4166 isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
4167 goto out;
4168 }
4169
4170
4171 softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
4172 if (softc == NULL) {
4173 isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
4174 goto out;
4175 }
4176 TAILQ_INIT(&softc->work_queue);
4177 TAILQ_INIT(&softc->rework_queue);
4178 TAILQ_INIT(&softc->running_queue);
4179 TAILQ_INIT(&softc->inot_queue);
4180 softc->isp = isp;
4181
4182 periphdriver_register(&isptargdriver);
4183 ISP_GET_PC(isp, chan, sim, sim);
4184 ISP_GET_PC(isp, chan, path, path);
4185 status = xpt_create_path(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4186 if (status != CAM_REQ_CMP) {
4187 isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
4188 return;
4189 }
4190 status = xpt_create_path(&path, NULL, cam_sim_path(sim), 0, 0);
4191 if (status != CAM_REQ_CMP) {
4192 xpt_free_path(wpath);
4193 isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
4194 return;
4195 }
4196
4197 ISP_LOCK(isp);
4198 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
4199 if (status != CAM_REQ_CMP) {
4200 ISP_UNLOCK(isp);
4201 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
4202 goto out;
4203 }
4204 wperiph = cam_periph_find(wpath, "isptarg");
4205 if (wperiph == NULL) {
4206 ISP_UNLOCK(isp);
4207 isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
4208 goto out;
4209 }
4210
4211 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
4212 if (status != CAM_REQ_CMP) {
4213 ISP_UNLOCK(isp);
4214 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
4215 goto out;
4216 }
4217
4218 periph = cam_periph_find(path, "isptarg");
4219 if (periph == NULL) {
4220 ISP_UNLOCK(isp);
4221 isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
4222 goto out;
4223 }
4224
4225 status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
4226 if (status != CAM_REQ_CMP) {
4227 ISP_UNLOCK(isp);
4228 isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
4229 goto out;
4230 }
4231
4232 ISP_UNLOCK(isp);
4233
4234 ccb = xpt_alloc_ccb();
4235
4236 /*
4237 * Make sure role is none.
4238 */
4239 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
4240 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
4241 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
4242 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
4243
4244 ISP_LOCK(isp);
4245 xpt_action(ccb);
4246 ISP_UNLOCK(isp);
4247
4248 /*
4249 * Now enable luns
4250 */
4251 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
4252 ccb->ccb_h.func_code = XPT_EN_LUN;
4253 ccb->cel.enable = 1;
4254 ISP_LOCK(isp);
4255 xpt_action(ccb);
4256 ISP_UNLOCK(isp);
4257 if (ccb->ccb_h.status != CAM_REQ_CMP) {
4258 xpt_free_ccb(ccb);
4259 xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
4260 goto out;
4261 }
4262
4263 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
4264 ccb->ccb_h.func_code = XPT_EN_LUN;
4265 ccb->cel.enable = 1;
4266 ISP_LOCK(isp);
4267 xpt_action(ccb);
4268 ISP_UNLOCK(isp);
4269 if (ccb->ccb_h.status != CAM_REQ_CMP) {
4270 xpt_free_ccb(ccb);
4271 xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
4272 goto out;
4273 }
4274 xpt_free_ccb(ccb);
4275
4276 /*
4277 * Add resources
4278 */
4279 ISP_GET_PC(isp, chan, target_proc, wchan);
4280 for (i = 0; i < 4; i++) {
4281 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4282 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
4283 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
4284 ccb->ccb_h.cbfcnp = isptarg_done;
4285 ccb->ccb_h.ppriv_ptr0 = malloc(sizeof (ppd_t), M_ISPTARG, M_WAITOK | M_ZERO);
4286 ISP_LOCK(isp);
4287 xpt_action(ccb);
4288 ISP_UNLOCK(isp);
4289 }
4290 for (i = 0; i < NISP_TARG_CMDS; i++) {
4291 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4292 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
4293 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
4294 ccb->ccb_h.cbfcnp = isptarg_done;
4295 ccb->ccb_h.ppriv_ptr0 = malloc(sizeof (ppd_t), M_ISPTARG, M_WAITOK | M_ZERO);
4296 ISP_LOCK(isp);
4297 xpt_action(ccb);
4298 ISP_UNLOCK(isp);
4299 }
4300 for (i = 0; i < 4; i++) {
4301 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4302 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
4303 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
4304 ccb->ccb_h.cbfcnp = isptarg_done;
4305 ISP_LOCK(isp);
4306 xpt_action(ccb);
4307 ISP_UNLOCK(isp);
4308 }
4309 for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
4310 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4311 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
4312 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
4313 ccb->ccb_h.cbfcnp = isptarg_done;
4314 ISP_LOCK(isp);
4315 xpt_action(ccb);
4316 ISP_UNLOCK(isp);
4317 }
4318
4319 /*
4320 * Now turn it all back on
4321 */
4322 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
4323 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
4324 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
4325 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
4326 ISP_LOCK(isp);
4327 xpt_action(ccb);
4328 ISP_UNLOCK(isp);
4329
4330 /*
4331 * Okay, while things are still active, sleep...
4332 */
4333 ISP_LOCK(isp);
4334 for (;;) {
4335 ISP_GET_PC(isp, chan, proc_active, i);
4336 if (i == 0) {
4337 break;
4338 }
4339 msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
4340 }
4341 ISP_UNLOCK(isp);
4342
4343out:
4344 if (wperiph) {
4345 cam_periph_invalidate(wperiph);
4346 }
4347 if (periph) {
4348 cam_periph_invalidate(periph);
4349 }
4350 if (junk_data) {
4351 free(junk_data, M_ISPTARG);
4352 }
4353 if (disk_data) {
4354 free(disk_data, M_ISPTARG);
4355 }
4356 if (softc) {
4357 free(softc, M_ISPTARG);
4358 }
4359 xpt_free_path(path);
4360 xpt_free_path(wpath);
4361}
4362
4363static void
4364isp_target_thread_pi(void *arg)
4365{
4366 struct isp_spi *pi = arg;
4367 ispsoftc_t *isp = cam_sim_softc(pi->sim);
4368 int chan = cam_sim_bus(pi->sim);
4369
4370 isp_target_thread(isp, chan);
4371 ISP_SPI_PC(isp, chan)->num_threads -= 1;
4372 kthread_exit();
4373}
4374
4375static void
4376isp_target_thread_fc(void *arg)
4377{
4378 struct isp_fc *fc = arg;
4379 ispsoftc_t *isp = cam_sim_softc(pi->sim);
4380 int chan = cam_sim_bus(pi->sim);
4381
4382 isp_target_thread(isp, chan);
4383 ISP_FC_PC(isp, chan)->num_threads -= 1;
4384 kthread_exit();
4385}
4386
4387static int
4388isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
4389{
4390 uint32_t cnt, curcnt;
4391 uint64_t lba;
4392
4393 switch (cdb[0]) {
4394 case WRITE_16:
4395 case READ_16:
4396 cnt = (((uint32_t)cdb[10]) << 24) |
4397 (((uint32_t)cdb[11]) << 16) |
4398 (((uint32_t)cdb[12]) << 8) |
4399 ((uint32_t)cdb[13]);
4400
4401 lba = (((uint64_t)cdb[2]) << 56) |
4402 (((uint64_t)cdb[3]) << 48) |
4403 (((uint64_t)cdb[4]) << 40) |
4404 (((uint64_t)cdb[5]) << 32) |
4405 (((uint64_t)cdb[6]) << 24) |
4406 (((uint64_t)cdb[7]) << 16) |
4407 (((uint64_t)cdb[8]) << 8) |
4408 ((uint64_t)cdb[9]);
4409 break;
4410 case WRITE_12:
4411 case READ_12:
4412 cnt = (((uint32_t)cdb[6]) << 16) |
4413 (((uint32_t)cdb[7]) << 8) |
4414 ((u_int32_t)cdb[8]);
4415
4416 lba = (((uint32_t)cdb[2]) << 24) |
4417 (((uint32_t)cdb[3]) << 16) |
4418 (((uint32_t)cdb[4]) << 8) |
4419 ((uint32_t)cdb[5]);
4420 break;
4421 case WRITE_10:
4422 case READ_10:
4423 cnt = (((uint32_t)cdb[7]) << 8) |
4424 ((u_int32_t)cdb[8]);
4425
4426 lba = (((uint32_t)cdb[2]) << 24) |
4427 (((uint32_t)cdb[3]) << 16) |
4428 (((uint32_t)cdb[4]) << 8) |
4429 ((uint32_t)cdb[5]);
4430 break;
4431 case WRITE_6:
4432 case READ_6:
4433 cnt = cdb[4];
4434 if (cnt == 0) {
4435 cnt = 256;
4436 }
4437 lba = (((uint32_t)cdb[1] & 0x1f) << 16) |
4438 (((uint32_t)cdb[2]) << 8) |
4439 ((uint32_t)cdb[3]);
4440 break;
4441 default:
4442 return (-1);
4443 }
4444
4445 cnt <<= DISK_SHIFT;
4446 lba <<= DISK_SHIFT;
4447
4448 if (offset == cnt) {
4449 *lp = 1;
4450 return (0);
4451 }
4452
4453 if (lba + cnt > dl) {
4454 return (-2);
4455 }
4456
4457 curcnt = MAX_ISP_TARG_TRANSFER;
4458 if (offset + curcnt >= cnt) {
4459 curcnt = cnt - offset;
4460 *lp = 1;
4461 } else {
4462 *lp = 0;
4463 }
4464#ifdef ISP_MULTI_CCBS
4465 if (curcnt > MULTI_CCB_DATA_LIM)
4466 curcnt = MULTI_CCB_DATA_LIM;
4467#endif
4468 *tl = curcnt;
4469 *kp = &dp[lba + offset];
4470 return (0);
4471}
4472
4473#endif
4474#endif
4475
4476static void
4477isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
4478{
4479 struct cam_sim *sim;
4480 int bus, tgt;
4481 ispsoftc_t *isp;
4482
4483 sim = (struct cam_sim *)cbarg;
4484 isp = (ispsoftc_t *) cam_sim_softc(sim);
4485 bus = cam_sim_bus(sim);
4486 tgt = xpt_path_target_id(path);
4487
4488 switch (code) {
4489 case AC_LOST_DEVICE:
4490 if (IS_SCSI(isp)) {
4491 uint16_t oflags, nflags;
4492 sdparam *sdp = SDPARAM(isp, bus);
4493
4494 if (tgt >= 0) {
4495 nflags = sdp->isp_devparam[tgt].nvrm_flags;
4496#ifndef ISP_TARGET_MODE
4497 nflags &= DPARM_SAFE_DFLT;
4498 if (isp->isp_loaded_fw) {
4499 nflags |= DPARM_NARROW | DPARM_ASYNC;
4500 }
4501#else
4502 nflags = DPARM_DEFAULT;
4503#endif
4504 oflags = sdp->isp_devparam[tgt].goal_flags;
4505 sdp->isp_devparam[tgt].goal_flags = nflags;
4506 sdp->isp_devparam[tgt].dev_update = 1;
4507 sdp->update = 1;
4508 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4509 sdp->isp_devparam[tgt].goal_flags = oflags;
4510 }
4511 }
4512 break;
4513 default:
4514 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
4515 break;
4516 }
4517}
4518
4519static void
4520isp_poll(struct cam_sim *sim)
4521{
4522 ispsoftc_t *isp = cam_sim_softc(sim);
32
33#include <dev/isp/isp_freebsd.h>
34#include <sys/unistd.h>
35#include <sys/kthread.h>
36#include <sys/conf.h>
37#include <sys/module.h>
38#include <sys/ioccom.h>
39#include <dev/isp/isp_ioctl.h>
40#include <sys/devicestat.h>
41#include <cam/cam_periph.h>
42#include <cam/cam_xpt_periph.h>
43
44#if __FreeBSD_version < 800002
45#define THREAD_CREATE kthread_create
46#else
47#define THREAD_CREATE kproc_create
48#endif
49
50MODULE_VERSION(isp, 1);
51MODULE_DEPEND(isp, cam, 1, 1, 1);
52int isp_announced = 0;
53int isp_fabric_hysteresis = 5;
54int isp_loop_down_limit = 60; /* default loop down limit */
55int isp_quickboot_time = 7; /* don't wait more than N secs for loop up */
56int isp_gone_device_time = 30; /* grace time before reporting device lost */
57int isp_autoconfig = 1; /* automatically attach/detach devices */
58static const char prom3[] = "Chan %d [%u] PortID 0x%06x Departed because of %s";
59
60static void isp_freeze_loopdown(ispsoftc_t *, int, char *);
61static d_ioctl_t ispioctl;
62static void isp_intr_enable(void *);
63static void isp_cam_async(void *, uint32_t, struct cam_path *, void *);
64static void isp_poll(struct cam_sim *);
65static timeout_t isp_watchdog;
66static timeout_t isp_gdt;
67static task_fn_t isp_gdt_task;
68static timeout_t isp_ldt;
69static task_fn_t isp_ldt_task;
70static void isp_kthread(void *);
71static void isp_action(struct cam_sim *, union ccb *);
72#ifdef ISP_INTERNAL_TARGET
73static void isp_target_thread_pi(void *);
74static void isp_target_thread_fc(void *);
75#endif
76static int isp_timer_count;
77static void isp_timer(void *);
78
79static struct cdevsw isp_cdevsw = {
80 .d_version = D_VERSION,
81 .d_ioctl = ispioctl,
82 .d_name = "isp",
83};
84
85static int
86isp_role_sysctl(SYSCTL_HANDLER_ARGS)
87{
88 ispsoftc_t *isp = (ispsoftc_t *)arg1;
89 int chan = arg2;
90 int error, old, value;
91
92 value = FCPARAM(isp, chan)->role;
93
94 error = sysctl_handle_int(oidp, &value, 0, req);
95 if ((error != 0) || (req->newptr == NULL))
96 return (error);
97
98 if (value < ISP_ROLE_NONE || value > ISP_ROLE_BOTH)
99 return (EINVAL);
100
101 ISP_LOCK(isp);
102 old = FCPARAM(isp, chan)->role;
103
104 /* We don't allow target mode switch from here. */
105 value = (old & ISP_ROLE_TARGET) | (value & ISP_ROLE_INITIATOR);
106
107 /* If nothing has changed -- we are done. */
108 if (value == old) {
109 ISP_UNLOCK(isp);
110 return (0);
111 }
112
113 /* Actually change the role. */
114 error = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, value);
115 ISP_UNLOCK(isp);
116 return (error);
117}
118
119static int
120isp_attach_chan(ispsoftc_t *isp, struct cam_devq *devq, int chan)
121{
122 struct ccb_setasync csa;
123 struct cam_sim *sim;
124 struct cam_path *path;
125
126 /*
127 * Construct our SIM entry.
128 */
129 sim = cam_sim_alloc(isp_action, isp_poll, "isp", isp, device_get_unit(isp->isp_dev), &isp->isp_osinfo.lock, isp->isp_maxcmds, isp->isp_maxcmds, devq);
130
131 if (sim == NULL) {
132 return (ENOMEM);
133 }
134
135 ISP_LOCK(isp);
136 if (xpt_bus_register(sim, isp->isp_dev, chan) != CAM_SUCCESS) {
137 ISP_UNLOCK(isp);
138 cam_sim_free(sim, FALSE);
139 return (EIO);
140 }
141 ISP_UNLOCK(isp);
142 if (xpt_create_path(&path, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
143 ISP_LOCK(isp);
144 xpt_bus_deregister(cam_sim_path(sim));
145 ISP_UNLOCK(isp);
146 cam_sim_free(sim, FALSE);
147 return (ENXIO);
148 }
149 xpt_setup_ccb(&csa.ccb_h, path, 5);
150 csa.ccb_h.func_code = XPT_SASYNC_CB;
151 csa.event_enable = AC_LOST_DEVICE;
152 csa.callback = isp_cam_async;
153 csa.callback_arg = sim;
154
155 ISP_LOCK(isp);
156 xpt_action((union ccb *)&csa);
157 ISP_UNLOCK(isp);
158
159 if (IS_SCSI(isp)) {
160 struct isp_spi *spi = ISP_SPI_PC(isp, chan);
161 spi->sim = sim;
162 spi->path = path;
163#ifdef ISP_INTERNAL_TARGET
164 ISP_SET_PC(isp, chan, proc_active, 1);
165 if (THREAD_CREATE(isp_target_thread_pi, spi, &spi->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
166 ISP_SET_PC(isp, chan, proc_active, 0);
167 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
168 }
169 ISP_SPI_PC(isp, chan)->num_threads += 1;
170#endif
171 } else {
172 fcparam *fcp = FCPARAM(isp, chan);
173 struct isp_fc *fc = ISP_FC_PC(isp, chan);
174 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(isp->isp_osinfo.dev);
175 struct sysctl_oid *tree = device_get_sysctl_tree(isp->isp_osinfo.dev);
176 char name[16];
177
178 ISP_LOCK(isp);
179 fc->sim = sim;
180 fc->path = path;
181 fc->isp = isp;
182 fc->ready = 1;
183
184 callout_init_mtx(&fc->ldt, &isp->isp_osinfo.lock, 0);
185 callout_init_mtx(&fc->gdt, &isp->isp_osinfo.lock, 0);
186 TASK_INIT(&fc->ltask, 1, isp_ldt_task, fc);
187 TASK_INIT(&fc->gtask, 1, isp_gdt_task, fc);
188
189 /*
190 * We start by being "loop down" if we have an initiator role
191 */
192 if (fcp->role & ISP_ROLE_INITIATOR) {
193 isp_freeze_loopdown(isp, chan, "isp_attach");
194 callout_reset(&fc->ldt, isp_quickboot_time * hz, isp_ldt, fc);
195 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Initial Loop Down Timer @ %lu", (unsigned long) time_uptime);
196 }
197 ISP_UNLOCK(isp);
198 if (THREAD_CREATE(isp_kthread, fc, &fc->kproc, 0, 0, "%s: fc_thrd%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
199 xpt_free_path(fc->path);
200 ISP_LOCK(isp);
201 if (callout_active(&fc->ldt))
202 callout_stop(&fc->ldt);
203 xpt_bus_deregister(cam_sim_path(fc->sim));
204 ISP_UNLOCK(isp);
205 cam_sim_free(fc->sim, FALSE);
206 return (ENOMEM);
207 }
208 fc->num_threads += 1;
209#ifdef ISP_INTERNAL_TARGET
210 ISP_SET_PC(isp, chan, proc_active, 1);
211 if (THREAD_CREATE(isp_target_thread_fc, fc, &fc->target_proc, 0, 0, "%s: isp_test_tgt%d", device_get_nameunit(isp->isp_osinfo.dev), chan)) {
212 ISP_SET_PC(isp, chan, proc_active, 0);
213 isp_prt(isp, ISP_LOGERR, "cannot create test target thread");
214 }
215 fc->num_threads += 1;
216#endif
217 if (chan > 0) {
218 snprintf(name, sizeof(name), "chan%d", chan);
219 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree),
220 OID_AUTO, name, CTLFLAG_RW, 0, "Virtual channel");
221 }
222 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
223 "wwnn", CTLFLAG_RD, &fcp->isp_wwnn,
224 "World Wide Node Name");
225 SYSCTL_ADD_QUAD(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
226 "wwpn", CTLFLAG_RD, &fcp->isp_wwpn,
227 "World Wide Port Name");
228 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
229 "loop_down_limit", CTLFLAG_RW, &fc->loop_down_limit, 0,
230 "Loop Down Limit");
231 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
232 "gone_device_time", CTLFLAG_RW, &fc->gone_device_time, 0,
233 "Gone Device Time");
234#if defined(ISP_TARGET_MODE) && defined(DEBUG)
235 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
236 "inject_lost_data_frame", CTLFLAG_RW, &fc->inject_lost_data_frame, 0,
237 "Cause a Lost Frame on a Read");
238#endif
239 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
240 "role", CTLTYPE_INT | CTLFLAG_RW, isp, chan,
241 isp_role_sysctl, "I", "Current role");
242 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
243 "speed", CTLFLAG_RD, &fcp->isp_gbspeed, 0,
244 "Connection speed in gigabits");
245 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
246 "linkstate", CTLFLAG_RD, &fcp->isp_linkstate, 0,
247 "Link state");
248 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
249 "fwstate", CTLFLAG_RD, &fcp->isp_fwstate, 0,
250 "Firmware state");
251 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
252 "loopstate", CTLFLAG_RD, &fcp->isp_loopstate, 0,
253 "Loop state");
254 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
255 "topo", CTLFLAG_RD, &fcp->isp_topo, 0,
256 "Connection topology");
257 }
258 return (0);
259}
260
261static void
262isp_detach_internal_target(ispsoftc_t *isp, int chan)
263{
264#ifdef ISP_INTERNAL_TARGET
265 void *wchan;
266
267 ISP_GET_PC(isp, chan, target_proc, wchan);
268 ISP_SET_PC(isp, chan, proc_active, 0);
269 wakeup(wchan);
270#endif
271}
272
273static void
274isp_detach_chan(ispsoftc_t *isp, int chan)
275{
276 struct cam_sim *sim;
277 struct cam_path *path;
278 struct ccb_setasync csa;
279 int *num_threads;
280
281 ISP_GET_PC(isp, chan, sim, sim);
282 ISP_GET_PC(isp, chan, path, path);
283 ISP_GET_PC_ADDR(isp, chan, num_threads, num_threads);
284
285 xpt_setup_ccb(&csa.ccb_h, path, 5);
286 csa.ccb_h.func_code = XPT_SASYNC_CB;
287 csa.event_enable = 0;
288 csa.callback = isp_cam_async;
289 csa.callback_arg = sim;
290 xpt_action((union ccb *)&csa);
291 xpt_free_path(path);
292 xpt_bus_deregister(cam_sim_path(sim));
293 cam_sim_free(sim, FALSE);
294
295 /* Wait for the channel's spawned threads to exit. */
296 wakeup(isp->isp_osinfo.pc.ptr);
297 isp_detach_internal_target(isp, chan);
298 while (*num_threads != 0)
299 mtx_sleep(isp, &isp->isp_osinfo.lock, PRIBIO, "isp_reap", 100);
300}
301
302int
303isp_attach(ispsoftc_t *isp)
304{
305 const char *nu = device_get_nameunit(isp->isp_osinfo.dev);
306 int du = device_get_unit(isp->isp_dev);
307 int chan;
308
309 isp->isp_osinfo.ehook.ich_func = isp_intr_enable;
310 isp->isp_osinfo.ehook.ich_arg = isp;
311 /*
312 * Haha. Set this first, because if we're loaded as a module isp_intr_enable
313 * will be called right awawy, which will clear isp_osinfo.ehook_active,
314 * which would be unwise to then set again later.
315 */
316 isp->isp_osinfo.ehook_active = 1;
317 if (config_intrhook_establish(&isp->isp_osinfo.ehook) != 0) {
318 isp_prt(isp, ISP_LOGERR, "could not establish interrupt enable hook");
319 return (-EIO);
320 }
321
322 /*
323 * Create the device queue for our SIM(s).
324 */
325 isp->isp_osinfo.devq = cam_simq_alloc(isp->isp_maxcmds);
326 if (isp->isp_osinfo.devq == NULL) {
327 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
328 return (EIO);
329 }
330
331 for (chan = 0; chan < isp->isp_nchan; chan++) {
332 if (isp_attach_chan(isp, isp->isp_osinfo.devq, chan)) {
333 goto unwind;
334 }
335 }
336
337 callout_init_mtx(&isp->isp_osinfo.tmo, &isp->isp_osinfo.lock, 0);
338 isp_timer_count = hz >> 2;
339 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
340 isp->isp_osinfo.timer_active = 1;
341
342 isp->isp_osinfo.cdev = make_dev(&isp_cdevsw, du, UID_ROOT, GID_OPERATOR, 0600, "%s", nu);
343 if (isp->isp_osinfo.cdev) {
344 isp->isp_osinfo.cdev->si_drv1 = isp;
345 }
346 return (0);
347
348unwind:
349 while (--chan >= 0) {
350 struct cam_sim *sim;
351 struct cam_path *path;
352
353 ISP_GET_PC(isp, chan, sim, sim);
354 ISP_GET_PC(isp, chan, path, path);
355 xpt_free_path(path);
356 ISP_LOCK(isp);
357 xpt_bus_deregister(cam_sim_path(sim));
358 ISP_UNLOCK(isp);
359 cam_sim_free(sim, FALSE);
360 }
361 if (isp->isp_osinfo.ehook_active) {
362 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
363 isp->isp_osinfo.ehook_active = 0;
364 }
365 if (isp->isp_osinfo.cdev) {
366 destroy_dev(isp->isp_osinfo.cdev);
367 isp->isp_osinfo.cdev = NULL;
368 }
369 cam_simq_free(isp->isp_osinfo.devq);
370 isp->isp_osinfo.devq = NULL;
371 return (-1);
372}
373
374int
375isp_detach(ispsoftc_t *isp)
376{
377 struct cam_sim *sim;
378 int chan;
379
380 ISP_LOCK(isp);
381 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1) {
382 ISP_GET_PC(isp, chan, sim, sim);
383 if (sim->refcount > 2) {
384 ISP_UNLOCK(isp);
385 return (EBUSY);
386 }
387 }
388 /* Tell spawned threads that we're exiting. */
389 isp->isp_osinfo.is_exiting = 1;
390 if (isp->isp_osinfo.timer_active) {
391 callout_stop(&isp->isp_osinfo.tmo);
392 isp->isp_osinfo.timer_active = 0;
393 }
394 for (chan = isp->isp_nchan - 1; chan >= 0; chan -= 1)
395 isp_detach_chan(isp, chan);
396 ISP_UNLOCK(isp);
397
398 if (isp->isp_osinfo.cdev) {
399 destroy_dev(isp->isp_osinfo.cdev);
400 isp->isp_osinfo.cdev = NULL;
401 }
402 if (isp->isp_osinfo.ehook_active) {
403 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
404 isp->isp_osinfo.ehook_active = 0;
405 }
406 if (isp->isp_osinfo.devq != NULL) {
407 cam_simq_free(isp->isp_osinfo.devq);
408 isp->isp_osinfo.devq = NULL;
409 }
410 return (0);
411}
412
413static void
414isp_freeze_loopdown(ispsoftc_t *isp, int chan, char *msg)
415{
416 if (IS_FC(isp)) {
417 struct isp_fc *fc = ISP_FC_PC(isp, chan);
418 if (fc->simqfrozen == 0) {
419 isp_prt(isp, ISP_LOGDEBUG0, "%s: freeze simq (loopdown) chan %d", msg, chan);
420 fc->simqfrozen = SIMQFRZ_LOOPDOWN;
421 xpt_freeze_simq(fc->sim, 1);
422 } else {
423 isp_prt(isp, ISP_LOGDEBUG0, "%s: mark frozen (loopdown) chan %d", msg, chan);
424 fc->simqfrozen |= SIMQFRZ_LOOPDOWN;
425 }
426 }
427}
428
429static void
430isp_unfreeze_loopdown(ispsoftc_t *isp, int chan)
431{
432 if (IS_FC(isp)) {
433 struct isp_fc *fc = ISP_FC_PC(isp, chan);
434 int wasfrozen = fc->simqfrozen & SIMQFRZ_LOOPDOWN;
435 fc->simqfrozen &= ~SIMQFRZ_LOOPDOWN;
436 if (wasfrozen && fc->simqfrozen == 0) {
437 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d releasing simq", __func__, chan);
438 xpt_release_simq(fc->sim, 1);
439 }
440 }
441}
442
443
444static int
445ispioctl(struct cdev *dev, u_long c, caddr_t addr, int flags, struct thread *td)
446{
447 ispsoftc_t *isp;
448 int nr, chan, retval = ENOTTY;
449
450 isp = dev->si_drv1;
451
452 switch (c) {
453 case ISP_SDBLEV:
454 {
455 int olddblev = isp->isp_dblev;
456 isp->isp_dblev = *(int *)addr;
457 *(int *)addr = olddblev;
458 retval = 0;
459 break;
460 }
461 case ISP_GETROLE:
462 chan = *(int *)addr;
463 if (chan < 0 || chan >= isp->isp_nchan) {
464 retval = -ENXIO;
465 break;
466 }
467 if (IS_FC(isp)) {
468 *(int *)addr = FCPARAM(isp, chan)->role;
469 } else {
470 *(int *)addr = SDPARAM(isp, chan)->role;
471 }
472 retval = 0;
473 break;
474 case ISP_SETROLE:
475 nr = *(int *)addr;
476 chan = nr >> 8;
477 if (chan < 0 || chan >= isp->isp_nchan) {
478 retval = -ENXIO;
479 break;
480 }
481 nr &= 0xff;
482 if (nr & ~(ISP_ROLE_INITIATOR|ISP_ROLE_TARGET)) {
483 retval = EINVAL;
484 break;
485 }
486 ISP_LOCK(isp);
487 if (IS_FC(isp))
488 *(int *)addr = FCPARAM(isp, chan)->role;
489 else
490 *(int *)addr = SDPARAM(isp, chan)->role;
491 retval = isp_control(isp, ISPCTL_CHANGE_ROLE, chan, nr);
492 ISP_UNLOCK(isp);
493 retval = 0;
494 break;
495
496 case ISP_RESETHBA:
497 ISP_LOCK(isp);
498 isp_reinit(isp, 0);
499 ISP_UNLOCK(isp);
500 retval = 0;
501 break;
502
503 case ISP_RESCAN:
504 if (IS_FC(isp)) {
505 chan = *(int *)addr;
506 if (chan < 0 || chan >= isp->isp_nchan) {
507 retval = -ENXIO;
508 break;
509 }
510 ISP_LOCK(isp);
511 if (isp_fc_runstate(isp, chan, 5 * 1000000)) {
512 retval = EIO;
513 } else {
514 retval = 0;
515 }
516 ISP_UNLOCK(isp);
517 }
518 break;
519
520 case ISP_FC_LIP:
521 if (IS_FC(isp)) {
522 chan = *(int *)addr;
523 if (chan < 0 || chan >= isp->isp_nchan) {
524 retval = -ENXIO;
525 break;
526 }
527 ISP_LOCK(isp);
528 if (isp_control(isp, ISPCTL_SEND_LIP, chan)) {
529 retval = EIO;
530 } else {
531 retval = 0;
532 }
533 ISP_UNLOCK(isp);
534 }
535 break;
536 case ISP_FC_GETDINFO:
537 {
538 struct isp_fc_device *ifc = (struct isp_fc_device *) addr;
539 fcportdb_t *lp;
540
541 if (IS_SCSI(isp)) {
542 break;
543 }
544 if (ifc->loopid >= MAX_FC_TARG) {
545 retval = EINVAL;
546 break;
547 }
548 lp = &FCPARAM(isp, ifc->chan)->portdb[ifc->loopid];
549 if (lp->state != FC_PORTDB_STATE_NIL) {
550 ifc->role = (lp->prli_word3 & SVC3_ROLE_MASK) >> SVC3_ROLE_SHIFT;
551 ifc->loopid = lp->handle;
552 ifc->portid = lp->portid;
553 ifc->node_wwn = lp->node_wwn;
554 ifc->port_wwn = lp->port_wwn;
555 retval = 0;
556 } else {
557 retval = ENODEV;
558 }
559 break;
560 }
561 case ISP_GET_STATS:
562 {
563 isp_stats_t *sp = (isp_stats_t *) addr;
564
565 ISP_MEMZERO(sp, sizeof (*sp));
566 sp->isp_stat_version = ISP_STATS_VERSION;
567 sp->isp_type = isp->isp_type;
568 sp->isp_revision = isp->isp_revision;
569 ISP_LOCK(isp);
570 sp->isp_stats[ISP_INTCNT] = isp->isp_intcnt;
571 sp->isp_stats[ISP_INTBOGUS] = isp->isp_intbogus;
572 sp->isp_stats[ISP_INTMBOXC] = isp->isp_intmboxc;
573 sp->isp_stats[ISP_INGOASYNC] = isp->isp_intoasync;
574 sp->isp_stats[ISP_RSLTCCMPLT] = isp->isp_rsltccmplt;
575 sp->isp_stats[ISP_FPHCCMCPLT] = isp->isp_fphccmplt;
576 sp->isp_stats[ISP_RSCCHIWAT] = isp->isp_rscchiwater;
577 sp->isp_stats[ISP_FPCCHIWAT] = isp->isp_fpcchiwater;
578 ISP_UNLOCK(isp);
579 retval = 0;
580 break;
581 }
582 case ISP_CLR_STATS:
583 ISP_LOCK(isp);
584 isp->isp_intcnt = 0;
585 isp->isp_intbogus = 0;
586 isp->isp_intmboxc = 0;
587 isp->isp_intoasync = 0;
588 isp->isp_rsltccmplt = 0;
589 isp->isp_fphccmplt = 0;
590 isp->isp_rscchiwater = 0;
591 isp->isp_fpcchiwater = 0;
592 ISP_UNLOCK(isp);
593 retval = 0;
594 break;
595 case ISP_FC_GETHINFO:
596 {
597 struct isp_hba_device *hba = (struct isp_hba_device *) addr;
598 int chan = hba->fc_channel;
599
600 if (chan < 0 || chan >= isp->isp_nchan) {
601 retval = ENXIO;
602 break;
603 }
604 hba->fc_fw_major = ISP_FW_MAJORX(isp->isp_fwrev);
605 hba->fc_fw_minor = ISP_FW_MINORX(isp->isp_fwrev);
606 hba->fc_fw_micro = ISP_FW_MICROX(isp->isp_fwrev);
607 hba->fc_nchannels = isp->isp_nchan;
608 if (IS_FC(isp)) {
609 hba->fc_nports = MAX_FC_TARG;
610 hba->fc_speed = FCPARAM(isp, hba->fc_channel)->isp_gbspeed;
611 hba->fc_topology = FCPARAM(isp, chan)->isp_topo + 1;
612 hba->fc_loopid = FCPARAM(isp, chan)->isp_loopid;
613 hba->nvram_node_wwn = FCPARAM(isp, chan)->isp_wwnn_nvram;
614 hba->nvram_port_wwn = FCPARAM(isp, chan)->isp_wwpn_nvram;
615 hba->active_node_wwn = FCPARAM(isp, chan)->isp_wwnn;
616 hba->active_port_wwn = FCPARAM(isp, chan)->isp_wwpn;
617 } else {
618 hba->fc_nports = MAX_TARGETS;
619 hba->fc_speed = 0;
620 hba->fc_topology = 0;
621 hba->nvram_node_wwn = 0ull;
622 hba->nvram_port_wwn = 0ull;
623 hba->active_node_wwn = 0ull;
624 hba->active_port_wwn = 0ull;
625 }
626 retval = 0;
627 break;
628 }
629 case ISP_TSK_MGMT:
630 {
631 int needmarker;
632 struct isp_fc_tsk_mgmt *fct = (struct isp_fc_tsk_mgmt *) addr;
633 uint16_t loopid;
634 mbreg_t mbs;
635
636 if (IS_SCSI(isp)) {
637 break;
638 }
639
640 chan = fct->chan;
641 if (chan < 0 || chan >= isp->isp_nchan) {
642 retval = -ENXIO;
643 break;
644 }
645
646 needmarker = retval = 0;
647 loopid = fct->loopid;
648 ISP_LOCK(isp);
649 if (IS_24XX(isp)) {
650 uint8_t local[QENTRY_LEN];
651 isp24xx_tmf_t *tmf;
652 isp24xx_statusreq_t *sp;
653 fcparam *fcp = FCPARAM(isp, chan);
654 fcportdb_t *lp;
655 int i;
656
657 for (i = 0; i < MAX_FC_TARG; i++) {
658 lp = &fcp->portdb[i];
659 if (lp->handle == loopid) {
660 break;
661 }
662 }
663 if (i == MAX_FC_TARG) {
664 retval = ENXIO;
665 ISP_UNLOCK(isp);
666 break;
667 }
668 /* XXX VALIDATE LP XXX */
669 tmf = (isp24xx_tmf_t *) local;
670 ISP_MEMZERO(tmf, QENTRY_LEN);
671 tmf->tmf_header.rqs_entry_type = RQSTYPE_TSK_MGMT;
672 tmf->tmf_header.rqs_entry_count = 1;
673 tmf->tmf_nphdl = lp->handle;
674 tmf->tmf_delay = 2;
675 tmf->tmf_timeout = 2;
676 tmf->tmf_tidlo = lp->portid;
677 tmf->tmf_tidhi = lp->portid >> 16;
678 tmf->tmf_vpidx = ISP_GET_VPIDX(isp, chan);
679 tmf->tmf_lun[1] = fct->lun & 0xff;
680 if (fct->lun >= 256) {
681 tmf->tmf_lun[0] = 0x40 | (fct->lun >> 8);
682 }
683 switch (fct->action) {
684 case IPT_CLEAR_ACA:
685 tmf->tmf_flags = ISP24XX_TMF_CLEAR_ACA;
686 break;
687 case IPT_TARGET_RESET:
688 tmf->tmf_flags = ISP24XX_TMF_TARGET_RESET;
689 needmarker = 1;
690 break;
691 case IPT_LUN_RESET:
692 tmf->tmf_flags = ISP24XX_TMF_LUN_RESET;
693 needmarker = 1;
694 break;
695 case IPT_CLEAR_TASK_SET:
696 tmf->tmf_flags = ISP24XX_TMF_CLEAR_TASK_SET;
697 needmarker = 1;
698 break;
699 case IPT_ABORT_TASK_SET:
700 tmf->tmf_flags = ISP24XX_TMF_ABORT_TASK_SET;
701 needmarker = 1;
702 break;
703 default:
704 retval = EINVAL;
705 break;
706 }
707 if (retval) {
708 ISP_UNLOCK(isp);
709 break;
710 }
711 MBSINIT(&mbs, MBOX_EXEC_COMMAND_IOCB_A64, MBLOGALL, 5000000);
712 mbs.param[1] = QENTRY_LEN;
713 mbs.param[2] = DMA_WD1(fcp->isp_scdma);
714 mbs.param[3] = DMA_WD0(fcp->isp_scdma);
715 mbs.param[6] = DMA_WD3(fcp->isp_scdma);
716 mbs.param[7] = DMA_WD2(fcp->isp_scdma);
717
718 if (FC_SCRATCH_ACQUIRE(isp, chan)) {
719 ISP_UNLOCK(isp);
720 retval = ENOMEM;
721 break;
722 }
723 isp_put_24xx_tmf(isp, tmf, fcp->isp_scratch);
724 MEMORYBARRIER(isp, SYNC_SFORDEV, 0, QENTRY_LEN, chan);
725 sp = (isp24xx_statusreq_t *) local;
726 sp->req_completion_status = 1;
727 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
728 MEMORYBARRIER(isp, SYNC_SFORCPU, QENTRY_LEN, QENTRY_LEN, chan);
729 isp_get_24xx_response(isp, &((isp24xx_statusreq_t *)fcp->isp_scratch)[1], sp);
730 FC_SCRATCH_RELEASE(isp, chan);
731 if (retval || sp->req_completion_status != 0) {
732 FC_SCRATCH_RELEASE(isp, chan);
733 retval = EIO;
734 }
735 if (retval == 0) {
736 if (needmarker) {
737 fcp->sendmarker = 1;
738 }
739 }
740 } else {
741 MBSINIT(&mbs, 0, MBLOGALL, 0);
742 if (ISP_CAP_2KLOGIN(isp) == 0) {
743 loopid <<= 8;
744 }
745 switch (fct->action) {
746 case IPT_CLEAR_ACA:
747 mbs.param[0] = MBOX_CLEAR_ACA;
748 mbs.param[1] = loopid;
749 mbs.param[2] = fct->lun;
750 break;
751 case IPT_TARGET_RESET:
752 mbs.param[0] = MBOX_TARGET_RESET;
753 mbs.param[1] = loopid;
754 needmarker = 1;
755 break;
756 case IPT_LUN_RESET:
757 mbs.param[0] = MBOX_LUN_RESET;
758 mbs.param[1] = loopid;
759 mbs.param[2] = fct->lun;
760 needmarker = 1;
761 break;
762 case IPT_CLEAR_TASK_SET:
763 mbs.param[0] = MBOX_CLEAR_TASK_SET;
764 mbs.param[1] = loopid;
765 mbs.param[2] = fct->lun;
766 needmarker = 1;
767 break;
768 case IPT_ABORT_TASK_SET:
769 mbs.param[0] = MBOX_ABORT_TASK_SET;
770 mbs.param[1] = loopid;
771 mbs.param[2] = fct->lun;
772 needmarker = 1;
773 break;
774 default:
775 retval = EINVAL;
776 break;
777 }
778 if (retval == 0) {
779 if (needmarker) {
780 FCPARAM(isp, chan)->sendmarker = 1;
781 }
782 retval = isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs);
783 if (retval) {
784 retval = EIO;
785 }
786 }
787 }
788 ISP_UNLOCK(isp);
789 break;
790 }
791 default:
792 break;
793 }
794 return (retval);
795}
796
797static void
798isp_intr_enable(void *arg)
799{
800 int chan;
801 ispsoftc_t *isp = arg;
802 ISP_LOCK(isp);
803 for (chan = 0; chan < isp->isp_nchan; chan++) {
804 if (IS_FC(isp)) {
805 if (FCPARAM(isp, chan)->role != ISP_ROLE_NONE) {
806 ISP_ENABLE_INTS(isp);
807 break;
808 }
809 } else {
810 if (SDPARAM(isp, chan)->role != ISP_ROLE_NONE) {
811 ISP_ENABLE_INTS(isp);
812 break;
813 }
814 }
815 }
816 isp->isp_osinfo.ehook_active = 0;
817 ISP_UNLOCK(isp);
818 /* Release our hook so that the boot can continue. */
819 config_intrhook_disestablish(&isp->isp_osinfo.ehook);
820}
821
822/*
823 * Local Inlines
824 */
825
826static ISP_INLINE int isp_get_pcmd(ispsoftc_t *, union ccb *);
827static ISP_INLINE void isp_free_pcmd(ispsoftc_t *, union ccb *);
828
829static ISP_INLINE int
830isp_get_pcmd(ispsoftc_t *isp, union ccb *ccb)
831{
832 ISP_PCMD(ccb) = isp->isp_osinfo.pcmd_free;
833 if (ISP_PCMD(ccb) == NULL) {
834 return (-1);
835 }
836 isp->isp_osinfo.pcmd_free = ((struct isp_pcmd *)ISP_PCMD(ccb))->next;
837 return (0);
838}
839
840static ISP_INLINE void
841isp_free_pcmd(ispsoftc_t *isp, union ccb *ccb)
842{
843 if (ISP_PCMD(ccb)) {
844#ifdef ISP_TARGET_MODE
845 PISP_PCMD(ccb)->datalen = 0;
846 PISP_PCMD(ccb)->totslen = 0;
847 PISP_PCMD(ccb)->cumslen = 0;
848 PISP_PCMD(ccb)->crn = 0;
849#endif
850 PISP_PCMD(ccb)->next = isp->isp_osinfo.pcmd_free;
851 isp->isp_osinfo.pcmd_free = ISP_PCMD(ccb);
852 ISP_PCMD(ccb) = NULL;
853 }
854}
855
856/*
857 * Put the target mode functions here, because some are inlines
858 */
859#ifdef ISP_TARGET_MODE
860static ISP_INLINE void isp_tmlock(ispsoftc_t *, const char *);
861static ISP_INLINE void isp_tmunlk(ispsoftc_t *);
862static ISP_INLINE int is_any_lun_enabled(ispsoftc_t *, int);
863static ISP_INLINE int is_lun_enabled(ispsoftc_t *, int, lun_id_t);
864static ISP_INLINE tstate_t *get_lun_statep(ispsoftc_t *, int, lun_id_t);
865static ISP_INLINE tstate_t *get_lun_statep_from_tag(ispsoftc_t *, int, uint32_t);
866static ISP_INLINE void rls_lun_statep(ispsoftc_t *, tstate_t *);
867static ISP_INLINE inot_private_data_t *get_ntp_from_tagdata(ispsoftc_t *, uint32_t, uint32_t, tstate_t **);
868static ISP_INLINE atio_private_data_t *isp_get_atpd(ispsoftc_t *, tstate_t *, uint32_t);
869static ISP_INLINE atio_private_data_t *isp_find_atpd(ispsoftc_t *, tstate_t *, uint32_t);
870static ISP_INLINE void isp_put_atpd(ispsoftc_t *, tstate_t *, atio_private_data_t *);
871static ISP_INLINE inot_private_data_t *isp_get_ntpd(ispsoftc_t *, tstate_t *);
872static ISP_INLINE inot_private_data_t *isp_find_ntpd(ispsoftc_t *, tstate_t *, uint32_t, uint32_t);
873static ISP_INLINE void isp_put_ntpd(ispsoftc_t *, tstate_t *, inot_private_data_t *);
874static cam_status create_lun_state(ispsoftc_t *, int, struct cam_path *, tstate_t **);
875static void destroy_lun_state(ispsoftc_t *, tstate_t *);
876static void isp_enable_lun(ispsoftc_t *, union ccb *);
877static cam_status isp_enable_deferred_luns(ispsoftc_t *, int);
878static cam_status isp_enable_deferred(ispsoftc_t *, int, lun_id_t);
879static void isp_disable_lun(ispsoftc_t *, union ccb *);
880static int isp_enable_target_mode(ispsoftc_t *, int);
881static int isp_disable_target_mode(ispsoftc_t *, int);
882static void isp_ledone(ispsoftc_t *, lun_entry_t *);
883static timeout_t isp_refire_putback_atio;
884static timeout_t isp_refire_notify_ack;
885static void isp_complete_ctio(union ccb *);
886static void isp_target_putback_atio(union ccb *);
887enum Start_Ctio_How { FROM_CAM, FROM_TIMER, FROM_SRR, FROM_CTIO_DONE };
888static void isp_target_start_ctio(ispsoftc_t *, union ccb *, enum Start_Ctio_How);
889static void isp_handle_platform_atio(ispsoftc_t *, at_entry_t *);
890static void isp_handle_platform_atio2(ispsoftc_t *, at2_entry_t *);
891static void isp_handle_platform_atio7(ispsoftc_t *, at7_entry_t *);
892static void isp_handle_platform_ctio(ispsoftc_t *, void *);
893static void isp_handle_platform_notify_scsi(ispsoftc_t *, in_entry_t *);
894static void isp_handle_platform_notify_fc(ispsoftc_t *, in_fcentry_t *);
895static void isp_handle_platform_notify_24xx(ispsoftc_t *, in_fcentry_24xx_t *);
896static int isp_handle_platform_target_notify_ack(ispsoftc_t *, isp_notify_t *);
897static void isp_handle_platform_target_tmf(ispsoftc_t *, isp_notify_t *);
898static void isp_target_mark_aborted(ispsoftc_t *, union ccb *);
899static void isp_target_mark_aborted_early(ispsoftc_t *, tstate_t *, uint32_t);
900
901static ISP_INLINE void
902isp_tmlock(ispsoftc_t *isp, const char *msg)
903{
904 while (isp->isp_osinfo.tmbusy) {
905 isp->isp_osinfo.tmwanted = 1;
906 mtx_sleep(isp, &isp->isp_lock, PRIBIO, msg, 0);
907 }
908 isp->isp_osinfo.tmbusy = 1;
909}
910
911static ISP_INLINE void
912isp_tmunlk(ispsoftc_t *isp)
913{
914 isp->isp_osinfo.tmbusy = 0;
915 if (isp->isp_osinfo.tmwanted) {
916 isp->isp_osinfo.tmwanted = 0;
917 wakeup(isp);
918 }
919}
920
921static ISP_INLINE int
922is_any_lun_enabled(ispsoftc_t *isp, int bus)
923{
924 struct tslist *lhp;
925 int i;
926
927 for (i = 0; i < LUN_HASH_SIZE; i++) {
928 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
929 if (SLIST_FIRST(lhp))
930 return (1);
931 }
932 return (0);
933}
934
935static ISP_INLINE int
936is_lun_enabled(ispsoftc_t *isp, int bus, lun_id_t lun)
937{
938 tstate_t *tptr;
939 struct tslist *lhp;
940
941 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
942 SLIST_FOREACH(tptr, lhp, next) {
943 if (tptr->ts_lun == lun) {
944 return (1);
945 }
946 }
947 return (0);
948}
949
950static void
951dump_tstates(ispsoftc_t *isp, int bus)
952{
953 int i, j;
954 struct tslist *lhp;
955 tstate_t *tptr = NULL;
956
957 if (bus >= isp->isp_nchan) {
958 return;
959 }
960 for (i = 0; i < LUN_HASH_SIZE; i++) {
961 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
962 j = 0;
963 SLIST_FOREACH(tptr, lhp, next) {
964 xpt_print(tptr->owner, "[%d, %d] atio_cnt=%d inot_cnt=%d\n", i, j, tptr->atio_count, tptr->inot_count);
965 j++;
966 }
967 }
968}
969
970static ISP_INLINE tstate_t *
971get_lun_statep(ispsoftc_t *isp, int bus, lun_id_t lun)
972{
973 tstate_t *tptr = NULL;
974 struct tslist *lhp;
975
976 if (bus < isp->isp_nchan) {
977 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
978 SLIST_FOREACH(tptr, lhp, next) {
979 if (tptr->ts_lun == lun) {
980 tptr->hold++;
981 return (tptr);
982 }
983 }
984 }
985 return (NULL);
986}
987
988static ISP_INLINE tstate_t *
989get_lun_statep_from_tag(ispsoftc_t *isp, int bus, uint32_t tagval)
990{
991 tstate_t *tptr = NULL;
992 atio_private_data_t *atp;
993 struct tslist *lhp;
994 int i;
995
996 if (bus < isp->isp_nchan && tagval != 0) {
997 for (i = 0; i < LUN_HASH_SIZE; i++) {
998 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
999 SLIST_FOREACH(tptr, lhp, next) {
1000 atp = isp_find_atpd(isp, tptr, tagval);
1001 if (atp) {
1002 tptr->hold++;
1003 return (tptr);
1004 }
1005 }
1006 }
1007 }
1008 return (NULL);
1009}
1010
1011static ISP_INLINE inot_private_data_t *
1012get_ntp_from_tagdata(ispsoftc_t *isp, uint32_t tag_id, uint32_t seq_id, tstate_t **rslt)
1013{
1014 inot_private_data_t *ntp;
1015 tstate_t *tptr;
1016 struct tslist *lhp;
1017 int bus, i;
1018
1019 for (bus = 0; bus < isp->isp_nchan; bus++) {
1020 for (i = 0; i < LUN_HASH_SIZE; i++) {
1021 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1022 SLIST_FOREACH(tptr, lhp, next) {
1023 ntp = isp_find_ntpd(isp, tptr, tag_id, seq_id);
1024 if (ntp) {
1025 *rslt = tptr;
1026 tptr->hold++;
1027 return (ntp);
1028 }
1029 }
1030 }
1031 }
1032 return (NULL);
1033}
1034
1035static ISP_INLINE void
1036rls_lun_statep(ispsoftc_t *isp, tstate_t *tptr)
1037{
1038 KASSERT((tptr->hold), ("tptr not held"));
1039 tptr->hold--;
1040}
1041
1042static void
1043isp_tmcmd_restart(ispsoftc_t *isp)
1044{
1045 inot_private_data_t *ntp;
1046 inot_private_data_t *restart_queue;
1047 tstate_t *tptr;
1048 union ccb *ccb;
1049 struct tslist *lhp;
1050 int bus, i;
1051
1052 for (bus = 0; bus < isp->isp_nchan; bus++) {
1053 for (i = 0; i < LUN_HASH_SIZE; i++) {
1054 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1055 SLIST_FOREACH(tptr, lhp, next) {
1056 if ((restart_queue = tptr->restart_queue) != NULL)
1057 tptr->restart_queue = NULL;
1058 while (restart_queue) {
1059 ntp = restart_queue;
1060 restart_queue = ntp->rd.nt.nt_hba;
1061 if (IS_24XX(isp)) {
1062 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
1063 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
1064 } else {
1065 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
1066 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
1067 }
1068 isp_put_ntpd(isp, tptr, ntp);
1069 if (tptr->restart_queue && restart_queue != NULL) {
1070 ntp = tptr->restart_queue;
1071 tptr->restart_queue = restart_queue;
1072 while (restart_queue->rd.nt.nt_hba) {
1073 restart_queue = restart_queue->rd.nt.nt_hba;
1074 }
1075 restart_queue->rd.nt.nt_hba = ntp;
1076 break;
1077 }
1078 }
1079 /*
1080 * We only need to do this once per tptr
1081 */
1082 if (!TAILQ_EMPTY(&tptr->waitq)) {
1083 ccb = (union ccb *)TAILQ_LAST(&tptr->waitq, isp_ccbq);
1084 TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1085 isp_target_start_ctio(isp, ccb, FROM_TIMER);
1086 }
1087 }
1088 }
1089 }
1090}
1091
1092static ISP_INLINE atio_private_data_t *
1093isp_get_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1094{
1095 atio_private_data_t *atp;
1096
1097 atp = LIST_FIRST(&tptr->atfree);
1098 if (atp) {
1099 LIST_REMOVE(atp, next);
1100 atp->tag = tag;
1101 LIST_INSERT_HEAD(&tptr->atused[ATPDPHASH(tag)], atp, next);
1102 }
1103 return (atp);
1104}
1105
1106static ISP_INLINE atio_private_data_t *
1107isp_find_atpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag)
1108{
1109 atio_private_data_t *atp;
1110
1111 LIST_FOREACH(atp, &tptr->atused[ATPDPHASH(tag)], next) {
1112 if (atp->tag == tag)
1113 return (atp);
1114 }
1115 return (NULL);
1116}
1117
1118static ISP_INLINE void
1119isp_put_atpd(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
1120{
1121 if (atp->ests) {
1122 isp_put_ecmd(isp, atp->ests);
1123 }
1124 LIST_REMOVE(atp, next);
1125 memset(atp, 0, sizeof (*atp));
1126 LIST_INSERT_HEAD(&tptr->atfree, atp, next);
1127}
1128
1129static void
1130isp_dump_atpd(ispsoftc_t *isp, tstate_t *tptr)
1131{
1132 atio_private_data_t *atp;
1133 const char *states[8] = { "Free", "ATIO", "CAM", "CTIO", "LAST_CTIO", "PDON", "?6", "7" };
1134
1135 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
1136 xpt_print(tptr->owner, "ATP: [0x%x] origdlen %u bytes_xfrd %u lun %u nphdl 0x%04x s_id 0x%06x d_id 0x%06x oxid 0x%04x state %s\n",
1137 atp->tag, atp->orig_datalen, atp->bytes_xfered, atp->lun, atp->nphdl, atp->sid, atp->portid, atp->oxid, states[atp->state & 0x7]);
1138 }
1139}
1140
1141
1142static ISP_INLINE inot_private_data_t *
1143isp_get_ntpd(ispsoftc_t *isp, tstate_t *tptr)
1144{
1145 inot_private_data_t *ntp;
1146 ntp = tptr->ntfree;
1147 if (ntp) {
1148 tptr->ntfree = ntp->next;
1149 }
1150 return (ntp);
1151}
1152
1153static ISP_INLINE inot_private_data_t *
1154isp_find_ntpd(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id, uint32_t seq_id)
1155{
1156 inot_private_data_t *ntp;
1157 for (ntp = tptr->ntpool; ntp < &tptr->ntpool[ATPDPSIZE]; ntp++) {
1158 if (ntp->rd.tag_id == tag_id && ntp->rd.seq_id == seq_id) {
1159 return (ntp);
1160 }
1161 }
1162 return (NULL);
1163}
1164
1165static ISP_INLINE void
1166isp_put_ntpd(ispsoftc_t *isp, tstate_t *tptr, inot_private_data_t *ntp)
1167{
1168 ntp->rd.tag_id = ntp->rd.seq_id = 0;
1169 ntp->next = tptr->ntfree;
1170 tptr->ntfree = ntp;
1171}
1172
1173static cam_status
1174create_lun_state(ispsoftc_t *isp, int bus, struct cam_path *path, tstate_t **rslt)
1175{
1176 cam_status status;
1177 lun_id_t lun;
1178 struct tslist *lhp;
1179 tstate_t *tptr;
1180 int i;
1181
1182 lun = xpt_path_lun_id(path);
1183 if (lun != CAM_LUN_WILDCARD) {
1184 if (lun >= ISP_MAX_LUNS(isp)) {
1185 return (CAM_LUN_INVALID);
1186 }
1187 }
1188 if (is_lun_enabled(isp, bus, lun)) {
1189 return (CAM_LUN_ALRDY_ENA);
1190 }
1191 tptr = malloc(sizeof (tstate_t), M_DEVBUF, M_NOWAIT|M_ZERO);
1192 if (tptr == NULL) {
1193 return (CAM_RESRC_UNAVAIL);
1194 }
1195 tptr->ts_lun = lun;
1196 status = xpt_create_path(&tptr->owner, NULL, xpt_path_path_id(path), xpt_path_target_id(path), lun);
1197 if (status != CAM_REQ_CMP) {
1198 free(tptr, M_DEVBUF);
1199 return (status);
1200 }
1201 SLIST_INIT(&tptr->atios);
1202 SLIST_INIT(&tptr->inots);
1203 TAILQ_INIT(&tptr->waitq);
1204 LIST_INIT(&tptr->atfree);
1205 for (i = ATPDPSIZE-1; i >= 0; i--)
1206 LIST_INSERT_HEAD(&tptr->atfree, &tptr->atpool[i], next);
1207 for (i = 0; i < ATPDPHASHSIZE; i++)
1208 LIST_INIT(&tptr->atused[i]);
1209 for (i = 0; i < ATPDPSIZE-1; i++)
1210 tptr->ntpool[i].next = &tptr->ntpool[i+1];
1211 tptr->ntfree = tptr->ntpool;
1212 tptr->hold = 1;
1213 ISP_GET_PC_ADDR(isp, bus, lun_hash[LUN_HASH_FUNC(lun)], lhp);
1214 SLIST_INSERT_HEAD(lhp, tptr, next);
1215 *rslt = tptr;
1216 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, path, "created tstate\n");
1217 return (CAM_REQ_CMP);
1218}
1219
1220static ISP_INLINE void
1221destroy_lun_state(ispsoftc_t *isp, tstate_t *tptr)
1222{
1223 union ccb *ccb;
1224 struct tslist *lhp;
1225
1226 KASSERT((tptr->hold != 0), ("tptr is not held"));
1227 KASSERT((tptr->hold == 1), ("tptr still held (%d)", tptr->hold));
1228 do {
1229 ccb = (union ccb *)SLIST_FIRST(&tptr->atios);
1230 if (ccb) {
1231 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
1232 ccb->ccb_h.status = CAM_REQ_ABORTED;
1233 xpt_done(ccb);
1234 }
1235 } while (ccb);
1236 do {
1237 ccb = (union ccb *)SLIST_FIRST(&tptr->inots);
1238 if (ccb) {
1239 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
1240 ccb->ccb_h.status = CAM_REQ_ABORTED;
1241 xpt_done(ccb);
1242 }
1243 } while (ccb);
1244 ISP_GET_PC_ADDR(isp, cam_sim_bus(xpt_path_sim(tptr->owner)), lun_hash[LUN_HASH_FUNC(tptr->ts_lun)], lhp);
1245 SLIST_REMOVE(lhp, tptr, tstate, next);
1246 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, tptr->owner, "destroyed tstate\n");
1247 xpt_free_path(tptr->owner);
1248 free(tptr, M_DEVBUF);
1249}
1250
1251/*
1252 * Enable a lun.
1253 */
1254static void
1255isp_enable_lun(ispsoftc_t *isp, union ccb *ccb)
1256{
1257 tstate_t *tptr = NULL;
1258 int bus, tm_enabled, target_role;
1259 target_id_t target;
1260 lun_id_t lun;
1261
1262
1263 /*
1264 * We only support either a wildcard target/lun or a target ID of zero and a non-wildcard lun
1265 */
1266 bus = XS_CHANNEL(ccb);
1267 target = ccb->ccb_h.target_id;
1268 lun = ccb->ccb_h.target_lun;
1269 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "enabling lun %u\n", lun);
1270 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1271 ccb->ccb_h.status = CAM_LUN_INVALID;
1272 xpt_done(ccb);
1273 return;
1274 }
1275
1276 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1277 ccb->ccb_h.status = CAM_LUN_INVALID;
1278 xpt_done(ccb);
1279 return;
1280 }
1281 if (isp->isp_dblev & ISP_LOGTDEBUG0) {
1282 xpt_print(ccb->ccb_h.path, "enabling lun 0x%x on channel %d\n", lun, bus);
1283 }
1284
1285 /*
1286 * Wait until we're not busy with the lun enables subsystem
1287 */
1288 isp_tmlock(isp, "isp_enable_lun");
1289
1290 /*
1291 * This is as a good a place as any to check f/w capabilities.
1292 */
1293
1294 if (IS_FC(isp)) {
1295 if (ISP_CAP_TMODE(isp) == 0) {
1296 xpt_print(ccb->ccb_h.path, "firmware does not support target mode\n");
1297 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1298 goto done;
1299 }
1300 /*
1301 * We *could* handle non-SCCLUN f/w, but we'd have to
1302 * dork with our already fragile enable/disable code.
1303 */
1304 if (ISP_CAP_SCCFW(isp) == 0) {
1305 xpt_print(ccb->ccb_h.path, "firmware not SCCLUN capable\n");
1306 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1307 goto done;
1308 }
1309
1310 target_role = (FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1311
1312 } else {
1313 target_role = (SDPARAM(isp, bus)->role & ISP_ROLE_TARGET) != 0;
1314 }
1315
1316 /*
1317 * Create the state pointer.
1318 * It should not already exist.
1319 */
1320 tptr = get_lun_statep(isp, bus, lun);
1321 if (tptr) {
1322 ccb->ccb_h.status = CAM_LUN_ALRDY_ENA;
1323 goto done;
1324 }
1325 ccb->ccb_h.status = create_lun_state(isp, bus, ccb->ccb_h.path, &tptr);
1326 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1327 goto done;
1328 }
1329
1330 /*
1331 * We have a tricky maneuver to perform here.
1332 *
1333 * If target mode isn't already enabled here,
1334 * *and* our current role includes target mode,
1335 * we enable target mode here.
1336 *
1337 */
1338 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1339 if (tm_enabled == 0 && target_role != 0) {
1340 if (isp_enable_target_mode(isp, bus)) {
1341 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1342 destroy_lun_state(isp, tptr);
1343 tptr = NULL;
1344 goto done;
1345 }
1346 tm_enabled = 1;
1347 }
1348
1349 /*
1350 * Now check to see whether this bus is in target mode already.
1351 *
1352 * If not, a later role change into target mode will finish the job.
1353 */
1354 if (tm_enabled == 0) {
1355 ISP_SET_PC(isp, bus, tm_enable_defer, 1);
1356 ccb->ccb_h.status = CAM_REQ_CMP;
1357 xpt_print(ccb->ccb_h.path, "Target Mode not enabled yet- lun enable deferred\n");
1358 goto done1;
1359 }
1360
1361 /*
1362 * Enable the lun.
1363 */
1364 ccb->ccb_h.status = isp_enable_deferred(isp, bus, lun);
1365
1366done:
1367 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1368 if (tptr) {
1369 destroy_lun_state(isp, tptr);
1370 tptr = NULL;
1371 }
1372 } else {
1373 tptr->enabled = 1;
1374 }
1375done1:
1376 if (tptr) {
1377 rls_lun_statep(isp, tptr);
1378 }
1379
1380 /*
1381 * And we're outta here....
1382 */
1383 isp_tmunlk(isp);
1384 xpt_done(ccb);
1385}
1386
1387static cam_status
1388isp_enable_deferred_luns(ispsoftc_t *isp, int bus)
1389{
1390 tstate_t *tptr = NULL;
1391 struct tslist *lhp;
1392 int i, n;
1393
1394
1395 ISP_GET_PC(isp, bus, tm_enabled, i);
1396 if (i == 1) {
1397 return (CAM_REQ_CMP);
1398 }
1399 ISP_GET_PC(isp, bus, tm_enable_defer, i);
1400 if (i == 0) {
1401 return (CAM_REQ_CMP);
1402 }
1403 /*
1404 * If this succeeds, it will set tm_enable
1405 */
1406 if (isp_enable_target_mode(isp, bus)) {
1407 return (CAM_REQ_CMP_ERR);
1408 }
1409 isp_tmlock(isp, "isp_enable_deferred_luns");
1410 for (n = i = 0; i < LUN_HASH_SIZE; i++) {
1411 ISP_GET_PC_ADDR(isp, bus, lun_hash[i], lhp);
1412 SLIST_FOREACH(tptr, lhp, next) {
1413 tptr->hold++;
1414 if (tptr->enabled == 0) {
1415 if (isp_enable_deferred(isp, bus, tptr->ts_lun) == CAM_REQ_CMP) {
1416 tptr->enabled = 1;
1417 n++;
1418 }
1419 } else {
1420 n++;
1421 }
1422 tptr->hold--;
1423 }
1424 }
1425 isp_tmunlk(isp);
1426 if (n == 0) {
1427 return (CAM_REQ_CMP_ERR);
1428 }
1429 ISP_SET_PC(isp, bus, tm_enable_defer, 0);
1430 return (CAM_REQ_CMP);
1431}
1432
1433static cam_status
1434isp_enable_deferred(ispsoftc_t *isp, int bus, lun_id_t lun)
1435{
1436 cam_status status;
1437 int luns_already_enabled;
1438
1439 ISP_GET_PC(isp, bus, tm_luns_enabled, luns_already_enabled);
1440 isp_prt(isp, ISP_LOGTINFO, "%s: bus %d lun %u luns_enabled %d", __func__, bus, lun, luns_already_enabled);
1441 if (IS_24XX(isp) || (IS_FC(isp) && luns_already_enabled)) {
1442 status = CAM_REQ_CMP;
1443 } else {
1444 int cmd_cnt, not_cnt;
1445
1446 if (IS_23XX(isp)) {
1447 cmd_cnt = DFLT_CMND_CNT;
1448 not_cnt = DFLT_INOT_CNT;
1449 } else {
1450 cmd_cnt = 64;
1451 not_cnt = 8;
1452 }
1453 status = CAM_REQ_INPROG;
1454 isp->isp_osinfo.rptr = &status;
1455 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun == CAM_LUN_WILDCARD? 0 : lun, cmd_cnt, not_cnt)) {
1456 status = CAM_RESRC_UNAVAIL;
1457 } else {
1458 mtx_sleep(&status, &isp->isp_lock, PRIBIO, "isp_enable_deferred", 0);
1459 }
1460 isp->isp_osinfo.rptr = NULL;
1461 }
1462 if (status == CAM_REQ_CMP) {
1463 ISP_SET_PC(isp, bus, tm_luns_enabled, 1);
1464 isp_prt(isp, ISP_LOGCONFIG|ISP_LOGTINFO, "bus %d lun %u now enabled for target mode", bus, lun);
1465 }
1466 return (status);
1467}
1468
1469static void
1470isp_disable_lun(ispsoftc_t *isp, union ccb *ccb)
1471{
1472 tstate_t *tptr = NULL;
1473 int bus;
1474 cam_status status;
1475 target_id_t target;
1476 lun_id_t lun;
1477
1478 bus = XS_CHANNEL(ccb);
1479 target = ccb->ccb_h.target_id;
1480 lun = ccb->ccb_h.target_lun;
1481 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0|ISP_LOGCONFIG, ccb->ccb_h.path, "disabling lun %u\n", lun);
1482 if (target == CAM_TARGET_WILDCARD && lun != CAM_LUN_WILDCARD) {
1483 ccb->ccb_h.status = CAM_LUN_INVALID;
1484 xpt_done(ccb);
1485 return;
1486 }
1487
1488 if (target != CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
1489 ccb->ccb_h.status = CAM_LUN_INVALID;
1490 xpt_done(ccb);
1491 return;
1492 }
1493
1494 /*
1495 * See if we're busy disabling a lun now.
1496 */
1497 isp_tmlock(isp, "isp_disable_lun");
1498 status = CAM_REQ_INPROG;
1499
1500 /*
1501 * Find the state pointer.
1502 */
1503 if ((tptr = get_lun_statep(isp, bus, lun)) == NULL) {
1504 status = CAM_PATH_INVALID;
1505 goto done;
1506 }
1507
1508 /*
1509 * If we're a 24XX card, we're done.
1510 */
1511 if (IS_24XX(isp)) {
1512 status = CAM_REQ_CMP;
1513 goto done;
1514 }
1515
1516 /*
1517 * For SCC FW, we only deal with lun zero.
1518 */
1519 if (IS_FC(isp) && lun > 0) {
1520 status = CAM_REQ_CMP;
1521 goto done;
1522 }
1523 isp->isp_osinfo.rptr = &status;
1524 if (isp_lun_cmd(isp, RQSTYPE_ENABLE_LUN, bus, lun, 0, 0)) {
1525 status = CAM_RESRC_UNAVAIL;
1526 } else {
1527 mtx_sleep(ccb, &isp->isp_lock, PRIBIO, "isp_disable_lun", 0);
1528 }
1529 isp->isp_osinfo.rptr = NULL;
1530done:
1531 if (status == CAM_REQ_CMP) {
1532 tptr->enabled = 0;
1533 if (is_any_lun_enabled(isp, bus) == 0) {
1534 if (isp_disable_target_mode(isp, bus)) {
1535 status = CAM_REQ_CMP_ERR;
1536 }
1537 }
1538 }
1539 ccb->ccb_h.status = status;
1540 if (status == CAM_REQ_CMP) {
1541 destroy_lun_state(isp, tptr);
1542 xpt_print(ccb->ccb_h.path, "lun now disabled for target mode\n");
1543 } else {
1544 if (tptr)
1545 rls_lun_statep(isp, tptr);
1546 }
1547 isp_tmunlk(isp);
1548 xpt_done(ccb);
1549}
1550
1551static int
1552isp_enable_target_mode(ispsoftc_t *isp, int bus)
1553{
1554 int tm_enabled;
1555
1556 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1557 if (tm_enabled != 0) {
1558 return (0);
1559 }
1560 if (IS_SCSI(isp)) {
1561 mbreg_t mbs;
1562 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1563 mbs.param[0] = MBOX_ENABLE_TARGET_MODE;
1564 mbs.param[1] = ENABLE_TARGET_FLAG|ENABLE_TQING_FLAG;
1565 mbs.param[2] = bus << 7;
1566 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1567 isp_prt(isp, ISP_LOGERR, "Unable to enable Target Role on Bus %d", bus);
1568 return (EIO);
1569 }
1570 }
1571 ISP_SET_PC(isp, bus, tm_enabled, 1);
1572 isp_prt(isp, ISP_LOGINFO, "Target Role enabled on Bus %d", bus);
1573 return (0);
1574}
1575
1576static int
1577isp_disable_target_mode(ispsoftc_t *isp, int bus)
1578{
1579 int tm_enabled;
1580
1581 ISP_GET_PC(isp, bus, tm_enabled, tm_enabled);
1582 if (tm_enabled == 0) {
1583 return (0);
1584 }
1585 if (IS_SCSI(isp)) {
1586 mbreg_t mbs;
1587 MBSINIT(&mbs, MBOX_ENABLE_TARGET_MODE, MBLOGALL, 0);
1588 mbs.param[2] = bus << 7;
1589 if (isp_control(isp, ISPCTL_RUN_MBOXCMD, &mbs) < 0 || mbs.param[0] != MBOX_COMMAND_COMPLETE) {
1590 isp_prt(isp, ISP_LOGERR, "Unable to disable Target Role on Bus %d", bus);
1591 return (EIO);
1592 }
1593 }
1594 ISP_SET_PC(isp, bus, tm_enabled, 0);
1595 isp_prt(isp, ISP_LOGINFO, "Target Role disabled on Bus %d", bus);
1596 return (0);
1597}
1598
1599static void
1600isp_ledone(ispsoftc_t *isp, lun_entry_t *lep)
1601{
1602 uint32_t *rptr;
1603
1604 rptr = isp->isp_osinfo.rptr;
1605 if (lep->le_status != LUN_OK) {
1606 isp_prt(isp, ISP_LOGERR, "ENABLE/MODIFY LUN returned 0x%x", lep->le_status);
1607 if (rptr) {
1608 *rptr = CAM_REQ_CMP_ERR;
1609 wakeup_one(rptr);
1610 }
1611 } else {
1612 if (rptr) {
1613 *rptr = CAM_REQ_CMP;
1614 wakeup_one(rptr);
1615 }
1616 }
1617}
1618
1619static void
1620isp_target_start_ctio(ispsoftc_t *isp, union ccb *ccb, enum Start_Ctio_How how)
1621{
1622 int fctape, sendstatus, resid;
1623 tstate_t *tptr;
1624 fcparam *fcp;
1625 atio_private_data_t *atp;
1626 struct ccb_scsiio *cso;
1627 uint32_t dmaresult, handle, xfrlen, sense_length, tmp;
1628 uint8_t local[QENTRY_LEN];
1629
1630 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), XS_LUN(ccb));
1631 if (tptr == NULL) {
1632 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
1633 if (tptr == NULL) {
1634 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find tstate pointer", __func__, ccb->csio.tag_id);
1635 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
1636 xpt_done(ccb);
1637 return;
1638 }
1639 }
1640 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ENTRY[0x%x] how %u xfrlen %u sendstatus %d sense_len %u", __func__, ccb->csio.tag_id, how, ccb->csio.dxfer_len,
1641 (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0, ((ccb->ccb_h.flags & CAM_SEND_SENSE)? ccb->csio.sense_len : 0));
1642
1643 switch (how) {
1644 case FROM_TIMER:
1645 case FROM_CAM:
1646 /*
1647 * Insert at the tail of the list, if any, waiting CTIO CCBs
1648 */
1649 TAILQ_INSERT_TAIL(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1650 break;
1651 case FROM_SRR:
1652 case FROM_CTIO_DONE:
1653 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1654 break;
1655 }
1656
1657 while (TAILQ_FIRST(&tptr->waitq) != NULL) {
1658 ccb = (union ccb *) TAILQ_FIRST(&tptr->waitq);
1659 TAILQ_REMOVE(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1660
1661 cso = &ccb->csio;
1662 xfrlen = cso->dxfer_len;
1663 if (xfrlen == 0) {
1664 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
1665 ISP_PATH_PRT(isp, ISP_LOGERR, ccb->ccb_h.path, "a data transfer length of zero but no status to send is wrong\n");
1666 ccb->ccb_h.status = CAM_REQ_INVALID;
1667 xpt_done(ccb);
1668 continue;
1669 }
1670 }
1671
1672 atp = isp_find_atpd(isp, tptr, cso->tag_id);
1673 if (atp == NULL) {
1674 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] cannot find private data adjunct in %s", __func__, cso->tag_id, __func__);
1675 isp_dump_atpd(isp, tptr);
1676 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1677 xpt_done(ccb);
1678 continue;
1679 }
1680
1681 /*
1682 * Is this command a dead duck?
1683 */
1684 if (atp->dead) {
1685 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] not sending a CTIO for a dead command", __func__, cso->tag_id);
1686 ccb->ccb_h.status = CAM_REQ_ABORTED;
1687 xpt_done(ccb);
1688 continue;
1689 }
1690
1691 /*
1692 * Check to make sure we're still in target mode.
1693 */
1694 fcp = FCPARAM(isp, XS_CHANNEL(ccb));
1695 if ((fcp->role & ISP_ROLE_TARGET) == 0) {
1696 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] stopping sending a CTIO because we're no longer in target mode", __func__, cso->tag_id);
1697 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1698 xpt_done(ccb);
1699 continue;
1700 }
1701
1702 /*
1703 * We're only handling ATPD_CCB_OUTSTANDING outstanding CCB at a time (one of which
1704 * could be split into two CTIOs to split data and status).
1705 */
1706 if (atp->ctcnt >= ATPD_CCB_OUTSTANDING) {
1707 isp_prt(isp, ISP_LOGTINFO, "[0x%x] handling only %d CCBs at a time (flags for this ccb: 0x%x)", cso->tag_id, ATPD_CCB_OUTSTANDING, ccb->ccb_h.flags);
1708 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1709 break;
1710 }
1711
1712 /*
1713 * Does the initiator expect FC-Tape style responses?
1714 */
1715 if ((atp->word3 & PRLI_WD3_RETRY) && fcp->fctape_enabled) {
1716 fctape = 1;
1717 } else {
1718 fctape = 0;
1719 }
1720
1721 /*
1722 * If we already did the data xfer portion of a CTIO that sends data
1723 * and status, don't do it again and do the status portion now.
1724 */
1725 if (atp->sendst) {
1726 isp_prt(isp, ISP_LOGTINFO, "[0x%x] now sending synthesized status orig_dl=%u xfered=%u bit=%u",
1727 cso->tag_id, atp->orig_datalen, atp->bytes_xfered, atp->bytes_in_transit);
1728 xfrlen = 0; /* we already did the data transfer */
1729 atp->sendst = 0;
1730 }
1731 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1732 sendstatus = 1;
1733 } else {
1734 sendstatus = 0;
1735 }
1736
1737 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1738 KASSERT((sendstatus != 0), ("how can you have CAM_SEND_SENSE w/o CAM_SEND_STATUS?"));
1739 /*
1740 * Sense length is not the entire sense data structure size. Periph
1741 * drivers don't seem to be setting sense_len to reflect the actual
1742 * size. We'll peek inside to get the right amount.
1743 */
1744 sense_length = cso->sense_len;
1745
1746 /*
1747 * This 'cannot' happen
1748 */
1749 if (sense_length > (XCMD_SIZE - MIN_FCP_RESPONSE_SIZE)) {
1750 sense_length = XCMD_SIZE - MIN_FCP_RESPONSE_SIZE;
1751 }
1752 } else {
1753 sense_length = 0;
1754 }
1755
1756 memset(local, 0, QENTRY_LEN);
1757
1758 /*
1759 * Check for overflow
1760 */
1761 tmp = atp->bytes_xfered + atp->bytes_in_transit + xfrlen;
1762 if (tmp > atp->orig_datalen) {
1763 isp_prt(isp, ISP_LOGERR, "%s: [0x%x] data overflow by %u bytes", __func__, cso->tag_id, tmp - atp->orig_datalen);
1764 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1765 xpt_done(ccb);
1766 continue;
1767 }
1768
1769 if (IS_24XX(isp)) {
1770 ct7_entry_t *cto = (ct7_entry_t *) local;
1771
1772 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
1773 cto->ct_header.rqs_entry_count = 1;
1774 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1775 ATPD_SET_SEQNO(cto, atp);
1776 cto->ct_nphdl = atp->nphdl;
1777 cto->ct_rxid = atp->tag;
1778 cto->ct_iid_lo = atp->portid;
1779 cto->ct_iid_hi = atp->portid >> 16;
1780 cto->ct_oxid = atp->oxid;
1781 cto->ct_vpidx = ISP_GET_VPIDX(isp, XS_CHANNEL(ccb));
1782 cto->ct_timeout = 120;
1783 cto->ct_flags = atp->tattr << CT7_TASK_ATTR_SHIFT;
1784
1785 /*
1786 * Mode 1, status, no data. Only possible when we are sending status, have
1787 * no data to transfer, and any sense data can fit into a ct7_entry_t.
1788 *
1789 * Mode 2, status, no data. We have to use this in the case that
1790 * the sense data won't fit into a ct7_entry_t.
1791 *
1792 */
1793 if (sendstatus && xfrlen == 0) {
1794 cto->ct_flags |= CT7_SENDSTATUS | CT7_NO_DATA;
1795 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1796 if (sense_length <= MAXRESPLEN_24XX) {
1797 if (resid < 0) {
1798 cto->ct_resid = -resid;
1799 } else if (resid > 0) {
1800 cto->ct_resid = resid;
1801 }
1802 cto->ct_flags |= CT7_FLAG_MODE1;
1803 cto->ct_scsi_status = cso->scsi_status;
1804 if (resid < 0) {
1805 cto->ct_scsi_status |= (FCP_RESID_OVERFLOW << 8);
1806 } else if (resid > 0) {
1807 cto->ct_scsi_status |= (FCP_RESID_UNDERFLOW << 8);
1808 }
1809 if (fctape) {
1810 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1811 }
1812 if (sense_length) {
1813 cto->ct_scsi_status |= (FCP_SNSLEN_VALID << 8);
1814 cto->rsp.m1.ct_resplen = cto->ct_senselen = sense_length;
1815 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1816 }
1817 } else {
1818 bus_addr_t addr;
1819 char buf[XCMD_SIZE];
1820 fcp_rsp_iu_t *rp;
1821
1822 if (atp->ests == NULL) {
1823 atp->ests = isp_get_ecmd(isp);
1824 if (atp->ests == NULL) {
1825 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1826 break;
1827 }
1828 }
1829 memset(buf, 0, sizeof (buf));
1830 rp = (fcp_rsp_iu_t *)buf;
1831 if (fctape) {
1832 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1833 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1834 }
1835 cto->ct_flags |= CT7_FLAG_MODE2;
1836 rp->fcp_rsp_scsi_status = cso->scsi_status;
1837 if (resid < 0) {
1838 rp->fcp_rsp_resid = -resid;
1839 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1840 } else if (resid > 0) {
1841 rp->fcp_rsp_resid = resid;
1842 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1843 }
1844 if (sense_length) {
1845 rp->fcp_rsp_snslen = sense_length;
1846 cto->ct_senselen = sense_length;
1847 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
1848 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1849 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
1850 } else {
1851 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
1852 }
1853 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
1854 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
1855 }
1856 addr = isp->isp_osinfo.ecmd_dma;
1857 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
1858 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
1859 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
1860 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
1861 cto->rsp.m2.ct_fcp_rsp_iudata.ds_base = DMA_LO32(addr);
1862 cto->rsp.m2.ct_fcp_rsp_iudata.ds_basehi = DMA_HI32(addr);
1863 cto->rsp.m2.ct_fcp_rsp_iudata.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
1864 }
1865 if (sense_length) {
1866 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d slen %u sense: %x %x/%x/%x", __func__,
1867 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid, sense_length,
1868 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
1869 } else {
1870 isp_prt(isp, ISP_LOGDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__,
1871 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, cto->ct_resid);
1872 }
1873 atp->state = ATPD_STATE_LAST_CTIO;
1874 }
1875
1876 /*
1877 * Mode 0 data transfers, *possibly* with status.
1878 */
1879 if (xfrlen != 0) {
1880 cto->ct_flags |= CT7_FLAG_MODE0;
1881 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1882 cto->ct_flags |= CT7_DATA_IN;
1883 } else {
1884 cto->ct_flags |= CT7_DATA_OUT;
1885 }
1886
1887 cto->rsp.m0.reloff = atp->bytes_xfered + atp->bytes_in_transit;
1888 cto->rsp.m0.ct_xfrlen = xfrlen;
1889
1890#ifdef DEBUG
1891 if (ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame && xfrlen > ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame) {
1892 isp_prt(isp, ISP_LOGWARN, "%s: truncating data frame with xfrlen %d to %d", __func__, xfrlen, xfrlen - (xfrlen >> 2));
1893 ISP_FC_PC(isp, XS_CHANNEL(ccb))->inject_lost_data_frame = 0;
1894 cto->rsp.m0.ct_xfrlen -= xfrlen >> 2;
1895 }
1896#endif
1897 if (sendstatus) {
1898 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
1899 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /* && fctape == 0 */) {
1900 cto->ct_flags |= CT7_SENDSTATUS;
1901 atp->state = ATPD_STATE_LAST_CTIO;
1902 if (fctape) {
1903 cto->ct_flags |= CT7_CONFIRM|CT7_EXPLCT_CONF;
1904 }
1905 } else {
1906 atp->sendst = 1; /* send status later */
1907 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
1908 atp->state = ATPD_STATE_CTIO;
1909 }
1910 } else {
1911 atp->state = ATPD_STATE_CTIO;
1912 }
1913 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO7[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x xfrlen=%u off=%u", __func__,
1914 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cto->ct_scsi_status, cto->ct_flags, xfrlen, atp->bytes_xfered);
1915 }
1916 } else if (IS_FC(isp)) {
1917 ct2_entry_t *cto = (ct2_entry_t *) local;
1918
1919 if (isp->isp_osinfo.sixtyfourbit)
1920 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO3;
1921 else
1922 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO2;
1923 cto->ct_header.rqs_entry_count = 1;
1924 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
1925 ATPD_SET_SEQNO(cto, atp);
1926 if (ISP_CAP_2KLOGIN(isp)) {
1927 ((ct2e_entry_t *)cto)->ct_iid = atp->nphdl;
1928 } else {
1929 cto->ct_iid = atp->nphdl;
1930 if (ISP_CAP_SCCFW(isp) == 0) {
1931 cto->ct_lun = ccb->ccb_h.target_lun;
1932 }
1933 }
1934 cto->ct_timeout = 10;
1935 cto->ct_rxid = cso->tag_id;
1936
1937 /*
1938 * Mode 1, status, no data. Only possible when we are sending status, have
1939 * no data to transfer, and the sense length can fit in the ct7_entry.
1940 *
1941 * Mode 2, status, no data. We have to use this in the case the response
1942 * length won't fit into a ct2_entry_t.
1943 *
1944 * We'll fill out this structure with information as if this were a
1945 * Mode 1. The hardware layer will create the Mode 2 FCP RSP IU as
1946 * needed based upon this.
1947 */
1948 if (sendstatus && xfrlen == 0) {
1949 cto->ct_flags |= CT2_SENDSTATUS | CT2_NO_DATA;
1950 resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
1951 if (sense_length <= MAXRESPLEN) {
1952 if (resid < 0) {
1953 cto->ct_resid = -resid;
1954 } else if (resid > 0) {
1955 cto->ct_resid = resid;
1956 }
1957 cto->ct_flags |= CT2_FLAG_MODE1;
1958 cto->rsp.m1.ct_scsi_status = cso->scsi_status;
1959 if (resid < 0) {
1960 cto->rsp.m1.ct_scsi_status |= CT2_DATA_OVER;
1961 } else if (resid > 0) {
1962 cto->rsp.m1.ct_scsi_status |= CT2_DATA_UNDER;
1963 }
1964 if (fctape) {
1965 cto->ct_flags |= CT2_CONFIRM;
1966 }
1967 if (sense_length) {
1968 cto->rsp.m1.ct_scsi_status |= CT2_SNSLEN_VALID;
1969 cto->rsp.m1.ct_resplen = cto->rsp.m1.ct_senselen = sense_length;
1970 memcpy(cto->rsp.m1.ct_resp, &cso->sense_data, sense_length);
1971 }
1972 } else {
1973 bus_addr_t addr;
1974 char buf[XCMD_SIZE];
1975 fcp_rsp_iu_t *rp;
1976
1977 if (atp->ests == NULL) {
1978 atp->ests = isp_get_ecmd(isp);
1979 if (atp->ests == NULL) {
1980 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
1981 break;
1982 }
1983 }
1984 memset(buf, 0, sizeof (buf));
1985 rp = (fcp_rsp_iu_t *)buf;
1986 if (fctape) {
1987 cto->ct_flags |= CT2_CONFIRM;
1988 rp->fcp_rsp_bits |= FCP_CONF_REQ;
1989 }
1990 cto->ct_flags |= CT2_FLAG_MODE2;
1991 rp->fcp_rsp_scsi_status = cso->scsi_status;
1992 if (resid < 0) {
1993 rp->fcp_rsp_resid = -resid;
1994 rp->fcp_rsp_bits |= FCP_RESID_OVERFLOW;
1995 } else if (resid > 0) {
1996 rp->fcp_rsp_resid = resid;
1997 rp->fcp_rsp_bits |= FCP_RESID_UNDERFLOW;
1998 }
1999 if (sense_length) {
2000 rp->fcp_rsp_snslen = sense_length;
2001 rp->fcp_rsp_bits |= FCP_SNSLEN_VALID;
2002 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
2003 memcpy(((fcp_rsp_iu_t *)atp->ests)->fcp_rsp_extra, &cso->sense_data, sense_length);
2004 } else {
2005 isp_put_fcp_rsp_iu(isp, rp, atp->ests);
2006 }
2007 if (isp->isp_dblev & ISP_LOGTDEBUG1) {
2008 isp_print_bytes(isp, "FCP Response Frame After Swizzling", MIN_FCP_RESPONSE_SIZE + sense_length, atp->ests);
2009 }
2010 addr = isp->isp_osinfo.ecmd_dma;
2011 addr += ((((isp_ecmd_t *)atp->ests) - isp->isp_osinfo.ecmd_base) * XCMD_SIZE);
2012 isp_prt(isp, ISP_LOGTDEBUG0, "%s: ests base %p vaddr %p ecmd_dma %jx addr %jx len %u", __func__, isp->isp_osinfo.ecmd_base, atp->ests,
2013 (uintmax_t) isp->isp_osinfo.ecmd_dma, (uintmax_t)addr, MIN_FCP_RESPONSE_SIZE + sense_length);
2014 cto->rsp.m2.ct_datalen = MIN_FCP_RESPONSE_SIZE + sense_length;
2015 if (isp->isp_osinfo.sixtyfourbit) {
2016 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_base = DMA_LO32(addr);
2017 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_basehi = DMA_HI32(addr);
2018 cto->rsp.m2.u.ct_fcp_rsp_iudata_64.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
2019 } else {
2020 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_base = DMA_LO32(addr);
2021 cto->rsp.m2.u.ct_fcp_rsp_iudata_32.ds_count = MIN_FCP_RESPONSE_SIZE + sense_length;
2022 }
2023 }
2024 if (sense_length) {
2025 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d sense: %x %x/%x/%x", __func__,
2026 cto->ct_rxid, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid,
2027 cso->sense_data.error_code, cso->sense_data.sense_buf[1], cso->sense_data.sense_buf[11], cso->sense_data.sense_buf[12]);
2028 } else {
2029 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[0x%x] seq %u nc %d CDB0=%x sstatus=0x%x flags=0x%x resid=%d", __func__, cto->ct_rxid,
2030 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid);
2031 }
2032 atp->state = ATPD_STATE_LAST_CTIO;
2033 }
2034
2035 if (xfrlen != 0) {
2036 cto->ct_flags |= CT2_FLAG_MODE0;
2037 if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2038 cto->ct_flags |= CT2_DATA_IN;
2039 } else {
2040 cto->ct_flags |= CT2_DATA_OUT;
2041 }
2042
2043 cto->ct_reloff = atp->bytes_xfered + atp->bytes_in_transit;
2044 cto->rsp.m0.ct_xfrlen = xfrlen;
2045
2046 if (sendstatus) {
2047 resid = atp->orig_datalen - atp->bytes_xfered - xfrlen;
2048 if (cso->scsi_status == SCSI_STATUS_OK && resid == 0 /*&& fctape == 0*/) {
2049 cto->ct_flags |= CT2_SENDSTATUS;
2050 atp->state = ATPD_STATE_LAST_CTIO;
2051 if (fctape) {
2052 cto->ct_flags |= CT2_CONFIRM;
2053 }
2054 } else {
2055 atp->sendst = 1; /* send status later */
2056 cto->ct_header.rqs_seqno &= ~ATPD_SEQ_NOTIFY_CAM;
2057 atp->state = ATPD_STATE_CTIO;
2058 }
2059 } else {
2060 atp->state = ATPD_STATE_CTIO;
2061 }
2062 }
2063 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO2[%x] seq %u nc %d CDB0=%x scsi status %x flags %x resid %d xfrlen %u offset %u", __func__, cto->ct_rxid,
2064 ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), atp->cdb0, cso->scsi_status, cto->ct_flags, cto->ct_resid, cso->dxfer_len, atp->bytes_xfered);
2065 } else {
2066 ct_entry_t *cto = (ct_entry_t *) local;
2067
2068 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO;
2069 cto->ct_header.rqs_entry_count = 1;
2070 cto->ct_header.rqs_seqno |= ATPD_SEQ_NOTIFY_CAM;
2071 ATPD_SET_SEQNO(cto, atp);
2072 cto->ct_iid = cso->init_id;
2073 cto->ct_iid |= XS_CHANNEL(ccb) << 7;
2074 cto->ct_tgt = ccb->ccb_h.target_id;
2075 cto->ct_lun = ccb->ccb_h.target_lun;
2076 cto->ct_fwhandle = cso->tag_id;
2077 if (atp->rxid) {
2078 cto->ct_tag_val = atp->rxid;
2079 cto->ct_flags |= CT_TQAE;
2080 }
2081 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
2082 cto->ct_flags |= CT_NODISC;
2083 }
2084 if (cso->dxfer_len == 0) {
2085 cto->ct_flags |= CT_NO_DATA;
2086 } else if ((cso->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2087 cto->ct_flags |= CT_DATA_IN;
2088 } else {
2089 cto->ct_flags |= CT_DATA_OUT;
2090 }
2091 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
2092 cto->ct_flags |= CT_SENDSTATUS|CT_CCINCR;
2093 cto->ct_scsi_status = cso->scsi_status;
2094 cto->ct_resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit - xfrlen;
2095 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d scsi status %x resid %d tag_id %x", __func__,
2096 cto->ct_fwhandle, ATPD_GET_SEQNO(cto), ATPD_GET_NCAM(cto), cso->scsi_status, cso->resid, cso->tag_id);
2097 }
2098 ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
2099 cto->ct_timeout = 10;
2100 }
2101
2102 if (isp_get_pcmd(isp, ccb)) {
2103 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "out of PCMDs\n");
2104 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
2105 break;
2106 }
2107 if (isp_allocate_xs_tgt(isp, ccb, &handle)) {
2108 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "No XFLIST pointers for %s\n", __func__);
2109 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
2110 isp_free_pcmd(isp, ccb);
2111 break;
2112 }
2113 atp->bytes_in_transit += xfrlen;
2114 PISP_PCMD(ccb)->datalen = xfrlen;
2115
2116
2117 /*
2118 * Call the dma setup routines for this entry (and any subsequent
2119 * CTIOs) if there's data to move, and then tell the f/w it's got
2120 * new things to play with. As with isp_start's usage of DMA setup,
2121 * any swizzling is done in the machine dependent layer. Because
2122 * of this, we put the request onto the queue area first in native
2123 * format.
2124 */
2125
2126 if (IS_24XX(isp)) {
2127 ct7_entry_t *cto = (ct7_entry_t *) local;
2128 cto->ct_syshandle = handle;
2129 } else if (IS_FC(isp)) {
2130 ct2_entry_t *cto = (ct2_entry_t *) local;
2131 cto->ct_syshandle = handle;
2132 } else {
2133 ct_entry_t *cto = (ct_entry_t *) local;
2134 cto->ct_syshandle = handle;
2135 }
2136
2137 dmaresult = ISP_DMASETUP(isp, cso, (ispreq_t *) local);
2138 if (dmaresult != CMD_QUEUED) {
2139 isp_destroy_tgt_handle(isp, handle);
2140 isp_free_pcmd(isp, ccb);
2141 if (dmaresult == CMD_EAGAIN) {
2142 TAILQ_INSERT_HEAD(&tptr->waitq, &ccb->ccb_h, periph_links.tqe);
2143 break;
2144 }
2145 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2146 xpt_done(ccb);
2147 continue;
2148 }
2149 isp->isp_nactive++;
2150 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2151 if (xfrlen) {
2152 ccb->ccb_h.spriv_field0 = atp->bytes_xfered;
2153 } else {
2154 ccb->ccb_h.spriv_field0 = ~0;
2155 }
2156 atp->ctcnt++;
2157 atp->seqno++;
2158 }
2159 rls_lun_statep(isp, tptr);
2160}
2161
2162static void
2163isp_refire_putback_atio(void *arg)
2164{
2165 union ccb *ccb = arg;
2166
2167 ISP_ASSERT_LOCKED((ispsoftc_t *)XS_ISP(ccb));
2168 isp_target_putback_atio(ccb);
2169}
2170
2171static void
2172isp_refire_notify_ack(void *arg)
2173{
2174 isp_tna_t *tp = arg;
2175 ispsoftc_t *isp = tp->isp;
2176
2177 ISP_ASSERT_LOCKED(isp);
2178 if (isp_notify_ack(isp, tp->not)) {
2179 callout_schedule(&tp->timer, 5);
2180 } else {
2181 free(tp, M_DEVBUF);
2182 }
2183}
2184
2185
2186static void
2187isp_target_putback_atio(union ccb *ccb)
2188{
2189 ispsoftc_t *isp;
2190 struct ccb_scsiio *cso;
2191 void *qe;
2192
2193 isp = XS_ISP(ccb);
2194
2195 qe = isp_getrqentry(isp);
2196 if (qe == NULL) {
2197 xpt_print(ccb->ccb_h.path,
2198 "%s: Request Queue Overflow\n", __func__);
2199 callout_reset(&PISP_PCMD(ccb)->wdog, 10,
2200 isp_refire_putback_atio, ccb);
2201 return;
2202 }
2203 memset(qe, 0, QENTRY_LEN);
2204 cso = &ccb->csio;
2205 if (IS_FC(isp)) {
2206 at2_entry_t local, *at = &local;
2207 ISP_MEMZERO(at, sizeof (at2_entry_t));
2208 at->at_header.rqs_entry_type = RQSTYPE_ATIO2;
2209 at->at_header.rqs_entry_count = 1;
2210 if (ISP_CAP_SCCFW(isp)) {
2211 at->at_scclun = (uint16_t) ccb->ccb_h.target_lun;
2212 } else {
2213 at->at_lun = (uint8_t) ccb->ccb_h.target_lun;
2214 }
2215 at->at_status = CT_OK;
2216 at->at_rxid = cso->tag_id;
2217 at->at_iid = cso->ccb_h.target_id;
2218 isp_put_atio2(isp, at, qe);
2219 } else {
2220 at_entry_t local, *at = &local;
2221 ISP_MEMZERO(at, sizeof (at_entry_t));
2222 at->at_header.rqs_entry_type = RQSTYPE_ATIO;
2223 at->at_header.rqs_entry_count = 1;
2224 at->at_iid = cso->init_id;
2225 at->at_iid |= XS_CHANNEL(ccb) << 7;
2226 at->at_tgt = cso->ccb_h.target_id;
2227 at->at_lun = cso->ccb_h.target_lun;
2228 at->at_status = CT_OK;
2229 at->at_tag_val = AT_GET_TAG(cso->tag_id);
2230 at->at_handle = AT_GET_HANDLE(cso->tag_id);
2231 isp_put_atio(isp, at, qe);
2232 }
2233 ISP_TDQE(isp, "isp_target_putback_atio", isp->isp_reqidx, qe);
2234 ISP_SYNC_REQUEST(isp);
2235 isp_complete_ctio(ccb);
2236}
2237
2238static void
2239isp_complete_ctio(union ccb *ccb)
2240{
2241 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2242 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2243 xpt_done(ccb);
2244 }
2245}
2246
2247/*
2248 * Handle ATIO stuff that the generic code can't.
2249 * This means handling CDBs.
2250 */
2251
2252static void
2253isp_handle_platform_atio(ispsoftc_t *isp, at_entry_t *aep)
2254{
2255 tstate_t *tptr;
2256 int status, bus;
2257 struct ccb_accept_tio *atiop;
2258 atio_private_data_t *atp;
2259
2260 /*
2261 * The firmware status (except for the QLTM_SVALID bit)
2262 * indicates why this ATIO was sent to us.
2263 *
2264 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2265 *
2266 * If the DISCONNECTS DISABLED bit is set in the flags field,
2267 * we're still connected on the SCSI bus.
2268 */
2269 status = aep->at_status;
2270 if ((status & ~QLTM_SVALID) == AT_PHASE_ERROR) {
2271 /*
2272 * Bus Phase Sequence error. We should have sense data
2273 * suggested by the f/w. I'm not sure quite yet what
2274 * to do about this for CAM.
2275 */
2276 isp_prt(isp, ISP_LOGWARN, "PHASE ERROR");
2277 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2278 return;
2279 }
2280 if ((status & ~QLTM_SVALID) != AT_CDB) {
2281 isp_prt(isp, ISP_LOGWARN, "bad atio (0x%x) leaked to platform", status);
2282 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2283 return;
2284 }
2285
2286 bus = GET_BUS_VAL(aep->at_iid);
2287 tptr = get_lun_statep(isp, bus, aep->at_lun);
2288 if (tptr == NULL) {
2289 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2290 if (tptr == NULL) {
2291 /*
2292 * Because we can't autofeed sense data back with
2293 * a command for parallel SCSI, we can't give back
2294 * a CHECK CONDITION. We'll give back a BUSY status
2295 * instead. This works out okay because the only
2296 * time we should, in fact, get this, is in the
2297 * case that somebody configured us without the
2298 * blackhole driver, so they get what they deserve.
2299 */
2300 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2301 return;
2302 }
2303 }
2304
2305 atp = isp_get_atpd(isp, tptr, aep->at_handle);
2306 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2307 if (atiop == NULL || atp == NULL) {
2308 /*
2309 * Because we can't autofeed sense data back with
2310 * a command for parallel SCSI, we can't give back
2311 * a CHECK CONDITION. We'll give back a QUEUE FULL status
2312 * instead. This works out okay because the only time we
2313 * should, in fact, get this, is in the case that we've
2314 * run out of ATIOS.
2315 */
2316 xpt_print(tptr->owner, "no %s for lun %d from initiator %d\n", (atp == NULL && atiop == NULL)? "ATIOs *or* ATPS" :
2317 ((atp == NULL)? "ATPs" : "ATIOs"), aep->at_lun, aep->at_iid);
2318 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2319 if (atp) {
2320 isp_put_atpd(isp, tptr, atp);
2321 }
2322 rls_lun_statep(isp, tptr);
2323 return;
2324 }
2325 atp->rxid = aep->at_tag_val;
2326 atp->state = ATPD_STATE_ATIO;
2327 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2328 tptr->atio_count--;
2329 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2330 atiop->ccb_h.target_id = aep->at_tgt;
2331 atiop->ccb_h.target_lun = aep->at_lun;
2332 if (aep->at_flags & AT_NODISC) {
2333 atiop->ccb_h.flags |= CAM_DIS_DISCONNECT;
2334 } else {
2335 atiop->ccb_h.flags &= ~CAM_DIS_DISCONNECT;
2336 }
2337
2338 if (status & QLTM_SVALID) {
2339 size_t amt = ISP_MIN(QLTM_SENSELEN, sizeof (atiop->sense_data));
2340 atiop->sense_len = amt;
2341 ISP_MEMCPY(&atiop->sense_data, aep->at_sense, amt);
2342 } else {
2343 atiop->sense_len = 0;
2344 }
2345
2346 atiop->init_id = GET_IID_VAL(aep->at_iid);
2347 atiop->cdb_len = aep->at_cdblen;
2348 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, aep->at_cdblen);
2349 atiop->ccb_h.status = CAM_CDB_RECVD;
2350 /*
2351 * Construct a tag 'id' based upon tag value (which may be 0..255)
2352 * and the handle (which we have to preserve).
2353 */
2354 atiop->tag_id = atp->tag;
2355 if (aep->at_flags & AT_TQAE) {
2356 atiop->tag_action = aep->at_tag_type;
2357 atiop->ccb_h.status |= CAM_TAG_ACTION_VALID;
2358 }
2359 atp->orig_datalen = 0;
2360 atp->bytes_xfered = 0;
2361 atp->lun = aep->at_lun;
2362 atp->nphdl = aep->at_iid;
2363 atp->portid = PORT_NONE;
2364 atp->oxid = 0;
2365 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2366 atp->tattr = aep->at_tag_type;
2367 atp->state = ATPD_STATE_CAM;
2368 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO[0x%x] CDB=0x%x lun %d", aep->at_tag_val, atp->cdb0, atp->lun);
2369 rls_lun_statep(isp, tptr);
2370}
2371
2372static void
2373isp_handle_platform_atio2(ispsoftc_t *isp, at2_entry_t *aep)
2374{
2375 lun_id_t lun;
2376 fcportdb_t *lp;
2377 tstate_t *tptr;
2378 struct ccb_accept_tio *atiop;
2379 uint16_t nphdl;
2380 atio_private_data_t *atp;
2381 inot_private_data_t *ntp;
2382
2383 /*
2384 * The firmware status (except for the QLTM_SVALID bit)
2385 * indicates why this ATIO was sent to us.
2386 *
2387 * If QLTM_SVALID is set, the firmware has recommended Sense Data.
2388 */
2389 if ((aep->at_status & ~QLTM_SVALID) != AT_CDB) {
2390 isp_prt(isp, ISP_LOGWARN, "bogus atio (0x%x) leaked to platform", aep->at_status);
2391 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2392 return;
2393 }
2394
2395 if (ISP_CAP_SCCFW(isp)) {
2396 lun = aep->at_scclun;
2397 } else {
2398 lun = aep->at_lun;
2399 }
2400 if (ISP_CAP_2KLOGIN(isp)) {
2401 nphdl = ((at2e_entry_t *)aep)->at_iid;
2402 } else {
2403 nphdl = aep->at_iid;
2404 }
2405 tptr = get_lun_statep(isp, 0, lun);
2406 if (tptr == NULL) {
2407 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2408 if (tptr == NULL) {
2409 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
2410 if (lun == 0) {
2411 isp_endcmd(isp, aep, SCSI_STATUS_BUSY, 0);
2412 } else {
2413 isp_endcmd(isp, aep, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2414 }
2415 return;
2416 }
2417 }
2418
2419 /*
2420 * Start any commands pending resources first.
2421 */
2422 if (tptr->restart_queue) {
2423 inot_private_data_t *restart_queue = tptr->restart_queue;
2424 tptr->restart_queue = NULL;
2425 while (restart_queue) {
2426 ntp = restart_queue;
2427 restart_queue = ntp->rd.nt.nt_hba;
2428 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at2_entry_t *)ntp->rd.data)->at_rxid);
2429 isp_handle_platform_atio2(isp, (at2_entry_t *) ntp->rd.data);
2430 isp_put_ntpd(isp, tptr, ntp);
2431 /*
2432 * If a recursion caused the restart queue to start to fill again,
2433 * stop and splice the new list on top of the old list and restore
2434 * it and go to noresrc.
2435 */
2436 if (tptr->restart_queue) {
2437 ntp = tptr->restart_queue;
2438 tptr->restart_queue = restart_queue;
2439 while (restart_queue->rd.nt.nt_hba) {
2440 restart_queue = restart_queue->rd.nt.nt_hba;
2441 }
2442 restart_queue->rd.nt.nt_hba = ntp;
2443 goto noresrc;
2444 }
2445 }
2446 }
2447
2448 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2449 if (atiop == NULL) {
2450 goto noresrc;
2451 }
2452
2453 atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2454 if (atp == NULL) {
2455 goto noresrc;
2456 }
2457
2458 atp->state = ATPD_STATE_ATIO;
2459 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2460 tptr->atio_count--;
2461 isp_prt(isp, ISP_LOGTDEBUG2, "Take FREE ATIO count now %d", tptr->atio_count);
2462 atiop->ccb_h.target_id = FCPARAM(isp, 0)->isp_loopid;
2463 atiop->ccb_h.target_lun = lun;
2464
2465 /*
2466 * We don't get 'suggested' sense data as we do with SCSI cards.
2467 */
2468 atiop->sense_len = 0;
2469
2470 /*
2471 * If we're not in the port database, add ourselves.
2472 */
2473 if (IS_2100(isp))
2474 atiop->init_id = nphdl;
2475 else {
2476 if ((isp_find_pdb_by_handle(isp, 0, nphdl, &lp) == 0 ||
2477 lp->state == FC_PORTDB_STATE_ZOMBIE)) {
2478 uint64_t iid =
2479 (((uint64_t) aep->at_wwpn[0]) << 48) |
2480 (((uint64_t) aep->at_wwpn[1]) << 32) |
2481 (((uint64_t) aep->at_wwpn[2]) << 16) |
2482 (((uint64_t) aep->at_wwpn[3]) << 0);
2483 isp_add_wwn_entry(isp, 0, iid, nphdl, PORT_ANY, 0);
2484 isp_find_pdb_by_handle(isp, 0, nphdl, &lp);
2485 }
2486 atiop->init_id = FC_PORTDB_TGT(isp, 0, lp);
2487 }
2488 atiop->cdb_len = ATIO2_CDBLEN;
2489 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cdb, ATIO2_CDBLEN);
2490 atiop->ccb_h.status = CAM_CDB_RECVD;
2491 atiop->tag_id = atp->tag;
2492 switch (aep->at_taskflags & ATIO2_TC_ATTR_MASK) {
2493 case ATIO2_TC_ATTR_SIMPLEQ:
2494 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2495 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2496 break;
2497 case ATIO2_TC_ATTR_HEADOFQ:
2498 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2499 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2500 break;
2501 case ATIO2_TC_ATTR_ORDERED:
2502 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2503 atiop->tag_action = MSG_ORDERED_Q_TAG;
2504 break;
2505 case ATIO2_TC_ATTR_ACAQ: /* ?? */
2506 case ATIO2_TC_ATTR_UNTAGGED:
2507 default:
2508 atiop->tag_action = 0;
2509 break;
2510 }
2511
2512 atp->orig_datalen = aep->at_datalen;
2513 atp->bytes_xfered = 0;
2514 atp->lun = lun;
2515 atp->nphdl = nphdl;
2516 atp->sid = PORT_ANY;
2517 atp->oxid = aep->at_oxid;
2518 atp->cdb0 = aep->at_cdb[0];
2519 atp->tattr = aep->at_taskflags & ATIO2_TC_ATTR_MASK;
2520 atp->state = ATPD_STATE_CAM;
2521 xpt_done((union ccb *)atiop);
2522 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO2[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2523 rls_lun_statep(isp, tptr);
2524 return;
2525noresrc:
2526 ntp = isp_get_ntpd(isp, tptr);
2527 if (ntp == NULL) {
2528 rls_lun_statep(isp, tptr);
2529 isp_endcmd(isp, aep, nphdl, 0, SCSI_STATUS_BUSY, 0);
2530 return;
2531 }
2532 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2533 ntp->rd.nt.nt_hba = tptr->restart_queue;
2534 tptr->restart_queue = ntp;
2535 rls_lun_statep(isp, tptr);
2536}
2537
2538static void
2539isp_handle_platform_atio7(ispsoftc_t *isp, at7_entry_t *aep)
2540{
2541 int cdbxlen;
2542 uint16_t lun, chan, nphdl = NIL_HANDLE;
2543 uint32_t did, sid;
2544 fcportdb_t *lp;
2545 tstate_t *tptr;
2546 struct ccb_accept_tio *atiop;
2547 atio_private_data_t *atp = NULL;
2548 atio_private_data_t *oatp;
2549 inot_private_data_t *ntp;
2550
2551 did = (aep->at_hdr.d_id[0] << 16) | (aep->at_hdr.d_id[1] << 8) | aep->at_hdr.d_id[2];
2552 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
2553 lun = (aep->at_cmnd.fcp_cmnd_lun[0] << 8) | aep->at_cmnd.fcp_cmnd_lun[1];
2554
2555 /*
2556 * Find the N-port handle, and Virtual Port Index for this command.
2557 *
2558 * If we can't, we're somewhat in trouble because we can't actually respond w/o that information.
2559 * We also, as a matter of course, need to know the WWN of the initiator too.
2560 */
2561 if (ISP_CAP_MULTI_ID(isp) && isp->isp_nchan > 1) {
2562 /*
2563 * Find the right channel based upon D_ID
2564 */
2565 isp_find_chan_by_did(isp, did, &chan);
2566
2567 if (chan == ISP_NOCHAN) {
2568 NANOTIME_T now;
2569
2570 /*
2571 * If we don't recognizer our own D_DID, terminate the exchange, unless we're within 2 seconds of startup
2572 * It's a bit tricky here as we need to stash this command *somewhere*.
2573 */
2574 GET_NANOTIME(&now);
2575 if (NANOTIME_SUB(&isp->isp_init_time, &now) > 2000000000ULL) {
2576 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- dropping", __func__, aep->at_rxid, did);
2577 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2578 return;
2579 }
2580 tptr = get_lun_statep(isp, 0, 0);
2581 if (tptr == NULL) {
2582 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
2583 if (tptr == NULL) {
2584 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel and no tptr- dropping", __func__, aep->at_rxid, did);
2585 isp_endcmd(isp, aep, NIL_HANDLE, ISP_NOCHAN, ECMD_TERMINATE, 0);
2586 return;
2587 }
2588 }
2589 isp_prt(isp, ISP_LOGWARN, "%s: [RX_ID 0x%x] D_ID %x not found on any channel- deferring", __func__, aep->at_rxid, did);
2590 goto noresrc;
2591 }
2592 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x", __func__, aep->at_rxid, did, chan, sid);
2593 } else {
2594 chan = 0;
2595 }
2596
2597 /*
2598 * Find the PDB entry for this initiator
2599 */
2600 if (isp_find_pdb_by_sid(isp, chan, sid, &lp) == 0) {
2601 /*
2602 * If we're not in the port database terminate the exchange.
2603 */
2604 isp_prt(isp, ISP_LOGTINFO, "%s: [RX_ID 0x%x] D_ID 0x%06x found on Chan %d for S_ID 0x%06x wasn't in PDB already",
2605 __func__, aep->at_rxid, did, chan, sid);
2606 isp_dump_portdb(isp, chan);
2607 isp_endcmd(isp, aep, NIL_HANDLE, chan, ECMD_TERMINATE, 0);
2608 return;
2609 }
2610 nphdl = lp->handle;
2611
2612 /*
2613 * Get the tstate pointer
2614 */
2615 tptr = get_lun_statep(isp, chan, lun);
2616 if (tptr == NULL) {
2617 tptr = get_lun_statep(isp, chan, CAM_LUN_WILDCARD);
2618 if (tptr == NULL) {
2619 isp_prt(isp, ISP_LOGWARN, "%s: [0x%x] no state pointer for lun %d or wildcard", __func__, aep->at_rxid, lun);
2620 if (lun == 0) {
2621 isp_endcmd(isp, aep, nphdl, SCSI_STATUS_BUSY, 0);
2622 } else {
2623 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_CHECK_COND | ECMD_SVALID | (0x5 << 12) | (0x25 << 16), 0);
2624 }
2625 return;
2626 }
2627 }
2628
2629 /*
2630 * Start any commands pending resources first.
2631 */
2632 if (tptr->restart_queue) {
2633 inot_private_data_t *restart_queue = tptr->restart_queue;
2634 tptr->restart_queue = NULL;
2635 while (restart_queue) {
2636 ntp = restart_queue;
2637 restart_queue = ntp->rd.nt.nt_hba;
2638 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restarting resrc deprived %x", __func__, ((at7_entry_t *)ntp->rd.data)->at_rxid);
2639 isp_handle_platform_atio7(isp, (at7_entry_t *) ntp->rd.data);
2640 isp_put_ntpd(isp, tptr, ntp);
2641 /*
2642 * If a recursion caused the restart queue to start to fill again,
2643 * stop and splice the new list on top of the old list and restore
2644 * it and go to noresrc.
2645 */
2646 if (tptr->restart_queue) {
2647 isp_prt(isp, ISP_LOGTDEBUG0, "%s: restart queue refilling", __func__);
2648 if (restart_queue) {
2649 ntp = tptr->restart_queue;
2650 tptr->restart_queue = restart_queue;
2651 while (restart_queue->rd.nt.nt_hba) {
2652 restart_queue = restart_queue->rd.nt.nt_hba;
2653 }
2654 restart_queue->rd.nt.nt_hba = ntp;
2655 }
2656 goto noresrc;
2657 }
2658 }
2659 }
2660
2661 /*
2662 * If the f/w is out of resources, just send a BUSY status back.
2663 */
2664 if (aep->at_rxid == AT7_NORESRC_RXID) {
2665 rls_lun_statep(isp, tptr);
2666 isp_endcmd(isp, aep, nphdl, chan, SCSI_BUSY, 0);
2667 return;
2668 }
2669
2670 /*
2671 * If we're out of resources, just send a BUSY status back.
2672 */
2673 atiop = (struct ccb_accept_tio *) SLIST_FIRST(&tptr->atios);
2674 if (atiop == NULL) {
2675 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atios", aep->at_rxid);
2676 goto noresrc;
2677 }
2678
2679 oatp = isp_find_atpd(isp, tptr, aep->at_rxid);
2680 if (oatp) {
2681 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] tag wraparound in isp_handle_platforms_atio7 (N-Port Handle 0x%04x S_ID 0x%04x OX_ID 0x%04x) oatp state %d",
2682 aep->at_rxid, nphdl, sid, aep->at_hdr.ox_id, oatp->state);
2683 /*
2684 * It's not a "no resource" condition- but we can treat it like one
2685 */
2686 goto noresrc;
2687 }
2688 atp = isp_get_atpd(isp, tptr, aep->at_rxid);
2689 if (atp == NULL) {
2690 isp_prt(isp, ISP_LOGTDEBUG0, "[0x%x] out of atps", aep->at_rxid);
2691 goto noresrc;
2692 }
2693 atp->word3 = lp->prli_word3;
2694 atp->state = ATPD_STATE_ATIO;
2695 SLIST_REMOVE_HEAD(&tptr->atios, sim_links.sle);
2696 tptr->atio_count--;
2697 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, atiop->ccb_h.path, "Take FREE ATIO count now %d\n", tptr->atio_count);
2698 atiop->init_id = FC_PORTDB_TGT(isp, chan, lp);
2699 atiop->ccb_h.target_id = FCPARAM(isp, chan)->isp_loopid;
2700 atiop->ccb_h.target_lun = lun;
2701 atiop->sense_len = 0;
2702 cdbxlen = aep->at_cmnd.fcp_cmnd_alen_datadir >> FCP_CMND_ADDTL_CDBLEN_SHIFT;
2703 if (cdbxlen) {
2704 isp_prt(isp, ISP_LOGWARN, "additional CDBLEN ignored");
2705 }
2706 cdbxlen = sizeof (aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb);
2707 ISP_MEMCPY(atiop->cdb_io.cdb_bytes, aep->at_cmnd.cdb_dl.sf.fcp_cmnd_cdb, cdbxlen);
2708 atiop->cdb_len = cdbxlen;
2709 atiop->ccb_h.status = CAM_CDB_RECVD;
2710 atiop->tag_id = atp->tag;
2711 switch (aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK) {
2712 case FCP_CMND_TASK_ATTR_SIMPLE:
2713 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2714 atiop->tag_action = MSG_SIMPLE_Q_TAG;
2715 break;
2716 case FCP_CMND_TASK_ATTR_HEAD:
2717 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2718 atiop->tag_action = MSG_HEAD_OF_Q_TAG;
2719 break;
2720 case FCP_CMND_TASK_ATTR_ORDERED:
2721 atiop->ccb_h.flags |= CAM_TAG_ACTION_VALID;
2722 atiop->tag_action = MSG_ORDERED_Q_TAG;
2723 break;
2724 default:
2725 /* FALLTHROUGH */
2726 case FCP_CMND_TASK_ATTR_ACA:
2727 case FCP_CMND_TASK_ATTR_UNTAGGED:
2728 atiop->tag_action = 0;
2729 break;
2730 }
2731 atp->orig_datalen = aep->at_cmnd.cdb_dl.sf.fcp_cmnd_dl;
2732 atp->bytes_xfered = 0;
2733 atp->lun = lun;
2734 atp->nphdl = nphdl;
2735 atp->portid = sid;
2736 atp->oxid = aep->at_hdr.ox_id;
2737 atp->rxid = aep->at_hdr.rx_id;
2738 atp->cdb0 = atiop->cdb_io.cdb_bytes[0];
2739 atp->tattr = aep->at_cmnd.fcp_cmnd_task_attribute & FCP_CMND_TASK_ATTR_MASK;
2740 atp->state = ATPD_STATE_CAM;
2741 isp_prt(isp, ISP_LOGTDEBUG0, "ATIO7[0x%x] CDB=0x%x lun %d datalen %u", aep->at_rxid, atp->cdb0, lun, atp->orig_datalen);
2742 xpt_done((union ccb *)atiop);
2743 rls_lun_statep(isp, tptr);
2744 return;
2745noresrc:
2746 if (atp) {
2747 isp_put_atpd(isp, tptr, atp);
2748 }
2749 ntp = isp_get_ntpd(isp, tptr);
2750 if (ntp == NULL) {
2751 rls_lun_statep(isp, tptr);
2752 isp_endcmd(isp, aep, nphdl, chan, SCSI_STATUS_BUSY, 0);
2753 return;
2754 }
2755 memcpy(ntp->rd.data, aep, QENTRY_LEN);
2756 ntp->rd.nt.nt_hba = tptr->restart_queue;
2757 tptr->restart_queue = ntp;
2758 rls_lun_statep(isp, tptr);
2759}
2760
2761
2762/*
2763 * Handle starting an SRR (sequence retransmit request)
2764 * We get here when we've gotten the immediate notify
2765 * and the return of all outstanding CTIOs for this
2766 * transaction.
2767 */
2768static void
2769isp_handle_srr_start(ispsoftc_t *isp, tstate_t *tptr, atio_private_data_t *atp)
2770{
2771 in_fcentry_24xx_t *inot;
2772 uint32_t srr_off, ccb_off, ccb_len, ccb_end;
2773 union ccb *ccb;
2774
2775 inot = (in_fcentry_24xx_t *)atp->srr;
2776 srr_off = inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16);
2777 ccb = atp->srr_ccb;
2778 atp->srr_ccb = NULL;
2779 atp->nsrr++;
2780 if (ccb == NULL) {
2781 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] null ccb", atp->tag);
2782 goto fail;
2783 }
2784
2785 ccb_off = ccb->ccb_h.spriv_field0;
2786 ccb_len = ccb->csio.dxfer_len;
2787 ccb_end = (ccb_off == ~0)? ~0 : ccb_off + ccb_len;
2788
2789 switch (inot->in_srr_iu) {
2790 case R_CTL_INFO_SOLICITED_DATA:
2791 /*
2792 * We have to restart a FCP_DATA data out transaction
2793 */
2794 atp->sendst = 0;
2795 atp->bytes_xfered = srr_off;
2796 if (ccb_len == 0) {
2797 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x but current CCB doesn't transfer data", atp->tag, srr_off);
2798 goto mdp;
2799 }
2800 if (srr_off < ccb_off || ccb_off > srr_off + ccb_len) {
2801 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x not covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2802 goto mdp;
2803 }
2804 isp_prt(isp, ISP_LOGWARN, "SRR[0x%x] SRR offset 0x%x covered by current CCB data range [0x%x..0x%x]", atp->tag, srr_off, ccb_off, ccb_end);
2805 break;
2806 case R_CTL_INFO_COMMAND_STATUS:
2807 isp_prt(isp, ISP_LOGTINFO, "SRR[0x%x] Got an FCP RSP SRR- resending status", atp->tag);
2808 atp->sendst = 1;
2809 /*
2810 * We have to restart a FCP_RSP IU transaction
2811 */
2812 break;
2813 case R_CTL_INFO_DATA_DESCRIPTOR:
2814 /*
2815 * We have to restart an FCP DATA in transaction
2816 */
2817 isp_prt(isp, ISP_LOGWARN, "Got an FCP DATA IN SRR- dropping");
2818 goto fail;
2819
2820 default:
2821 isp_prt(isp, ISP_LOGWARN, "Got an unknown information (%x) SRR- dropping", inot->in_srr_iu);
2822 goto fail;
2823 }
2824
2825 /*
2826 * We can't do anything until this is acked, so we might as well start it now.
2827 * We aren't going to do the usual asynchronous ack issue because we need
2828 * to make sure this gets on the wire first.
2829 */
2830 if (isp_notify_ack(isp, inot)) {
2831 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2832 goto fail;
2833 }
2834 isp_target_start_ctio(isp, ccb, FROM_SRR);
2835 return;
2836fail:
2837 inot->in_reserved = 1;
2838 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2839 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2840 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
2841 isp_complete_ctio(ccb);
2842 return;
2843mdp:
2844 if (isp_notify_ack(isp, inot)) {
2845 isp_prt(isp, ISP_LOGWARN, "could not push positive ack for SRR- you lose");
2846 goto fail;
2847 }
2848 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2849 ccb->ccb_h.status = CAM_MESSAGE_RECV;
2850 /*
2851 * This is not a strict interpretation of MDP, but it's close
2852 */
2853 ccb->csio.msg_ptr = &ccb->csio.sense_data.sense_buf[SSD_FULL_SIZE - 16];
2854 ccb->csio.msg_len = 7;
2855 ccb->csio.msg_ptr[0] = MSG_EXTENDED;
2856 ccb->csio.msg_ptr[1] = 5;
2857 ccb->csio.msg_ptr[2] = 0; /* modify data pointer */
2858 ccb->csio.msg_ptr[3] = srr_off >> 24;
2859 ccb->csio.msg_ptr[4] = srr_off >> 16;
2860 ccb->csio.msg_ptr[5] = srr_off >> 8;
2861 ccb->csio.msg_ptr[6] = srr_off;
2862 isp_complete_ctio(ccb);
2863}
2864
2865
2866static void
2867isp_handle_srr_notify(ispsoftc_t *isp, void *inot_raw)
2868{
2869 tstate_t *tptr;
2870 in_fcentry_24xx_t *inot = inot_raw;
2871 atio_private_data_t *atp;
2872 uint32_t tag = inot->in_rxid;
2873 uint32_t bus = inot->in_vpidx;
2874
2875 if (!IS_24XX(isp)) {
2876 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot_raw);
2877 return;
2878 }
2879
2880 tptr = get_lun_statep_from_tag(isp, bus, tag);
2881 if (tptr == NULL) {
2882 isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x in SRR Notify", __func__, tag);
2883 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2884 return;
2885 }
2886 atp = isp_find_atpd(isp, tptr, tag);
2887 if (atp == NULL) {
2888 rls_lun_statep(isp, tptr);
2889 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x in SRR Notify", __func__, tag);
2890 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
2891 return;
2892 }
2893 atp->srr_notify_rcvd = 1;
2894 memcpy(atp->srr, inot, sizeof (atp->srr));
2895 isp_prt(isp, ISP_LOGTINFO /* ISP_LOGTDEBUG0 */, "SRR[0x%x] inot->in_rxid flags 0x%x srr_iu=%x reloff 0x%x", inot->in_rxid, inot->in_flags, inot->in_srr_iu,
2896 inot->in_srr_reloff_lo | (inot->in_srr_reloff_hi << 16));
2897 if (atp->srr_ccb)
2898 isp_handle_srr_start(isp, tptr, atp);
2899 rls_lun_statep(isp, tptr);
2900}
2901
2902static void
2903isp_handle_platform_ctio(ispsoftc_t *isp, void *arg)
2904{
2905 union ccb *ccb;
2906 int sentstatus = 0, ok = 0, notify_cam = 0, resid = 0, failure = 0;
2907 tstate_t *tptr = NULL;
2908 atio_private_data_t *atp = NULL;
2909 int bus;
2910 uint32_t handle, moved_data = 0, data_requested;
2911
2912 /*
2913 * CTIO handles are 16 bits.
2914 * CTIO2 and CTIO7 are 32 bits.
2915 */
2916
2917 if (IS_SCSI(isp)) {
2918 handle = ((ct_entry_t *)arg)->ct_syshandle;
2919 } else {
2920 handle = ((ct2_entry_t *)arg)->ct_syshandle;
2921 }
2922 ccb = isp_find_xs_tgt(isp, handle);
2923 if (ccb == NULL) {
2924 isp_print_bytes(isp, "null ccb in isp_handle_platform_ctio", QENTRY_LEN, arg);
2925 return;
2926 }
2927 isp_destroy_tgt_handle(isp, handle);
2928 data_requested = PISP_PCMD(ccb)->datalen;
2929 isp_free_pcmd(isp, ccb);
2930 if (isp->isp_nactive) {
2931 isp->isp_nactive--;
2932 }
2933
2934 bus = XS_CHANNEL(ccb);
2935 tptr = get_lun_statep(isp, bus, XS_LUN(ccb));
2936 if (tptr == NULL) {
2937 tptr = get_lun_statep(isp, bus, CAM_LUN_WILDCARD);
2938 }
2939 if (tptr == NULL) {
2940 isp_prt(isp, ISP_LOGERR, "%s: cannot find tptr for tag %x after I/O", __func__, ccb->csio.tag_id);
2941 return;
2942 }
2943
2944 if (IS_24XX(isp)) {
2945 atp = isp_find_atpd(isp, tptr, ((ct7_entry_t *)arg)->ct_rxid);
2946 } else if (IS_FC(isp)) {
2947 atp = isp_find_atpd(isp, tptr, ((ct2_entry_t *)arg)->ct_rxid);
2948 } else {
2949 atp = isp_find_atpd(isp, tptr, ((ct_entry_t *)arg)->ct_fwhandle);
2950 }
2951 if (atp == NULL) {
2952 /*
2953 * XXX: isp_clear_commands() generates fake CTIO with zero
2954 * ct_rxid value, filling only ct_syshandle. Workaround
2955 * that using tag_id from the CCB, pointed by ct_syshandle.
2956 */
2957 atp = isp_find_atpd(isp, tptr, ccb->csio.tag_id);
2958 }
2959 if (atp == NULL) {
2960 rls_lun_statep(isp, tptr);
2961 isp_prt(isp, ISP_LOGERR, "%s: cannot find adjunct for %x after I/O", __func__, ccb->csio.tag_id);
2962 return;
2963 }
2964 KASSERT((atp->ctcnt > 0), ("ctio count not greater than zero"));
2965 atp->bytes_in_transit -= data_requested;
2966 atp->ctcnt -= 1;
2967 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
2968
2969 if (IS_24XX(isp)) {
2970 ct7_entry_t *ct = arg;
2971
2972 if (ct->ct_nphdl == CT7_SRR) {
2973 atp->srr_ccb = ccb;
2974 if (atp->srr_notify_rcvd)
2975 isp_handle_srr_start(isp, tptr, atp);
2976 rls_lun_statep(isp, tptr);
2977 return;
2978 }
2979 if (ct->ct_nphdl == CT_HBA_RESET) {
2980 failure = CAM_UNREC_HBA_ERROR;
2981 } else {
2982 sentstatus = ct->ct_flags & CT7_SENDSTATUS;
2983 ok = (ct->ct_nphdl == CT7_OK);
2984 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
2985 if ((ct->ct_flags & CT7_DATAMASK) != CT7_NO_DATA) {
2986 resid = ct->ct_resid;
2987 moved_data = data_requested - resid;
2988 }
2989 }
2990 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO7[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
2991 notify_cam, ct->ct_nphdl, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
2992 } else if (IS_FC(isp)) {
2993 ct2_entry_t *ct = arg;
2994 if (ct->ct_status == CT_SRR) {
2995 atp->srr_ccb = ccb;
2996 if (atp->srr_notify_rcvd)
2997 isp_handle_srr_start(isp, tptr, atp);
2998 rls_lun_statep(isp, tptr);
2999 isp_target_putback_atio(ccb);
3000 return;
3001 }
3002 if (ct->ct_status == CT_HBA_RESET) {
3003 failure = CAM_UNREC_HBA_ERROR;
3004 } else {
3005 sentstatus = ct->ct_flags & CT2_SENDSTATUS;
3006 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
3007 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
3008 if ((ct->ct_flags & CT2_DATAMASK) != CT2_NO_DATA) {
3009 resid = ct->ct_resid;
3010 moved_data = data_requested - resid;
3011 }
3012 }
3013 isp_prt(isp, ok? ISP_LOGTDEBUG0 : ISP_LOGWARN, "%s: CTIO2[%x] seq %u nc %d sts 0x%x flg 0x%x sns %d resid %d %s", __func__, ct->ct_rxid, ATPD_GET_SEQNO(ct),
3014 notify_cam, ct->ct_status, ct->ct_flags, (ccb->ccb_h.status & CAM_SENT_SENSE) != 0, resid, sentstatus? "FIN" : "MID");
3015 } else {
3016 ct_entry_t *ct = arg;
3017
3018 if (ct->ct_status == (CT_HBA_RESET & 0xff)) {
3019 failure = CAM_UNREC_HBA_ERROR;
3020 } else {
3021 sentstatus = ct->ct_flags & CT_SENDSTATUS;
3022 ok = (ct->ct_status & ~QLTM_SVALID) == CT_OK;
3023 notify_cam = (ct->ct_header.rqs_seqno & ATPD_SEQ_NOTIFY_CAM) != 0;
3024 }
3025 if ((ct->ct_flags & CT_DATAMASK) != CT_NO_DATA) {
3026 resid = ct->ct_resid;
3027 moved_data = data_requested - resid;
3028 }
3029 isp_prt(isp, ISP_LOGTDEBUG0, "%s: CTIO[%x] seq %u nc %d tag %x S_ID 0x%x lun %d sts %x flg %x resid %d %s", __func__, ct->ct_fwhandle, ATPD_GET_SEQNO(ct),
3030 notify_cam, ct->ct_tag_val, ct->ct_iid, ct->ct_lun, ct->ct_status, ct->ct_flags, resid, sentstatus? "FIN" : "MID");
3031 }
3032 if (ok) {
3033 if (moved_data) {
3034 atp->bytes_xfered += moved_data;
3035 ccb->csio.resid = atp->orig_datalen - atp->bytes_xfered - atp->bytes_in_transit;
3036 }
3037 if (sentstatus && (ccb->ccb_h.flags & CAM_SEND_SENSE)) {
3038 ccb->ccb_h.status |= CAM_SENT_SENSE;
3039 }
3040 ccb->ccb_h.status |= CAM_REQ_CMP;
3041 } else {
3042 notify_cam = 1;
3043 if (failure == CAM_UNREC_HBA_ERROR)
3044 ccb->ccb_h.status |= CAM_UNREC_HBA_ERROR;
3045 else
3046 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
3047 }
3048 atp->state = ATPD_STATE_PDON;
3049 rls_lun_statep(isp, tptr);
3050
3051 /*
3052 * We never *not* notify CAM when there has been any error (ok == 0),
3053 * so we never need to do an ATIO putback if we're not notifying CAM.
3054 */
3055 isp_prt(isp, ISP_LOGTDEBUG0, "%s CTIO[0x%x] done (ok=%d nc=%d nowsendstatus=%d ccb ss=%d)",
3056 (sentstatus)? " FINAL " : "MIDTERM ", atp->tag, ok, notify_cam, atp->sendst, (ccb->ccb_h.flags & CAM_SEND_STATUS) != 0);
3057 if (notify_cam == 0) {
3058 if (atp->sendst) {
3059 isp_target_start_ctio(isp, ccb, FROM_CTIO_DONE);
3060 }
3061 return;
3062 }
3063
3064 /*
3065 * We're telling CAM we're done with this CTIO transaction.
3066 *
3067 * 24XX cards never need an ATIO put back.
3068 *
3069 * Other cards need one put back only on error.
3070 * In the latter case, a timeout will re-fire
3071 * and try again in case we didn't have
3072 * queue resources to do so at first. In any case,
3073 * once the putback is done we do the completion
3074 * call.
3075 */
3076 if (ok || IS_24XX(isp)) {
3077 isp_complete_ctio(ccb);
3078 } else {
3079 isp_target_putback_atio(ccb);
3080 }
3081}
3082
3083static void
3084isp_handle_platform_notify_scsi(ispsoftc_t *isp, in_entry_t *inot)
3085{
3086 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3087}
3088
3089static void
3090isp_handle_platform_notify_fc(ispsoftc_t *isp, in_fcentry_t *inp)
3091{
3092 int needack = 1;
3093 switch (inp->in_status) {
3094 case IN_PORT_LOGOUT:
3095 /*
3096 * XXX: Need to delete this initiator's WWN from the database
3097 * XXX: Need to send this LOGOUT upstream
3098 */
3099 isp_prt(isp, ISP_LOGWARN, "port logout of S_ID 0x%x", inp->in_iid);
3100 break;
3101 case IN_PORT_CHANGED:
3102 isp_prt(isp, ISP_LOGWARN, "port changed for S_ID 0x%x", inp->in_iid);
3103 break;
3104 case IN_GLOBAL_LOGO:
3105 isp_del_all_wwn_entries(isp, 0);
3106 isp_prt(isp, ISP_LOGINFO, "all ports logged out");
3107 break;
3108 case IN_ABORT_TASK:
3109 {
3110 tstate_t *tptr;
3111 uint16_t lun;
3112 uint32_t loopid, sid;
3113 uint64_t wwn;
3114 atio_private_data_t *atp;
3115 fcportdb_t *lp;
3116 struct ccb_immediate_notify *inot = NULL;
3117
3118 if (ISP_CAP_SCCFW(isp)) {
3119 lun = inp->in_scclun;
3120 } else {
3121 lun = inp->in_lun;
3122 }
3123 if (ISP_CAP_2KLOGIN(isp)) {
3124 loopid = ((in_fcentry_e_t *)inp)->in_iid;
3125 } else {
3126 loopid = inp->in_iid;
3127 }
3128 if (isp_find_pdb_by_handle(isp, 0, loopid, &lp)) {
3129 wwn = lp->port_wwn;
3130 sid = lp->portid;
3131 } else {
3132 wwn = INI_ANY;
3133 sid = PORT_ANY;
3134 }
3135 tptr = get_lun_statep(isp, 0, lun);
3136 if (tptr == NULL) {
3137 tptr = get_lun_statep(isp, 0, CAM_LUN_WILDCARD);
3138 if (tptr == NULL) {
3139 isp_prt(isp, ISP_LOGWARN, "ABORT TASK for lun %u- but no tstate", lun);
3140 return;
3141 }
3142 }
3143 atp = isp_find_atpd(isp, tptr, inp->in_seqid);
3144
3145 if (atp) {
3146 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
3147 isp_prt(isp, ISP_LOGTDEBUG0, "ABORT TASK RX_ID %x WWN 0x%016llx state %d", inp->in_seqid, (unsigned long long) wwn, atp->state);
3148 if (inot) {
3149 tptr->inot_count--;
3150 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3151 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3152 } else {
3153 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "out of INOT structures\n");
3154 }
3155 } else {
3156 ISP_PATH_PRT(isp, ISP_LOGWARN, tptr->owner, "abort task RX_ID %x from wwn 0x%016llx, state unknown\n", inp->in_seqid, wwn);
3157 }
3158 if (inot) {
3159 isp_notify_t tmp, *nt = &tmp;
3160 ISP_MEMZERO(nt, sizeof (isp_notify_t));
3161 nt->nt_hba = isp;
3162 nt->nt_tgt = FCPARAM(isp, 0)->isp_wwpn;
3163 nt->nt_wwn = wwn;
3164 nt->nt_nphdl = loopid;
3165 nt->nt_sid = sid;
3166 nt->nt_did = PORT_ANY;
3167 nt->nt_lun = lun;
3168 nt->nt_need_ack = 1;
3169 nt->nt_channel = 0;
3170 nt->nt_ncode = NT_ABORT_TASK;
3171 nt->nt_lreserved = inot;
3172 isp_handle_platform_target_tmf(isp, nt);
3173 needack = 0;
3174 }
3175 rls_lun_statep(isp, tptr);
3176 break;
3177 }
3178 default:
3179 break;
3180 }
3181 if (needack) {
3182 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inp);
3183 }
3184}
3185
3186static void
3187isp_handle_platform_notify_24xx(ispsoftc_t *isp, in_fcentry_24xx_t *inot)
3188{
3189 uint16_t nphdl;
3190 uint16_t prli_options = 0;
3191 uint32_t portid;
3192 fcportdb_t *lp;
3193 uint8_t *ptr = NULL;
3194 uint64_t wwn;
3195
3196 nphdl = inot->in_nphdl;
3197 if (nphdl != NIL_HANDLE) {
3198 portid = inot->in_portid_hi << 16 | inot->in_portid_lo;
3199 } else {
3200 portid = PORT_ANY;
3201 }
3202
3203 switch (inot->in_status) {
3204 case IN24XX_ELS_RCVD:
3205 {
3206 char buf[16], *msg;
3207 int chan = ISP_GET_VPIDX(isp, inot->in_vpidx);
3208
3209 /*
3210 * Note that we're just getting notification that an ELS was received
3211 * (possibly with some associated information sent upstream). This is
3212 * *not* the same as being given the ELS frame to accept or reject.
3213 */
3214 switch (inot->in_status_subcode) {
3215 case LOGO:
3216 msg = "LOGO";
3217 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
3218 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
3219 wwn = (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF]) << 56) |
3220 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+1]) << 48) |
3221 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+2]) << 40) |
3222 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+3]) << 32) |
3223 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+4]) << 24) |
3224 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+5]) << 16) |
3225 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+6]) << 8) |
3226 (((uint64_t) ptr[IN24XX_LOGO_WWPN_OFF+7]));
3227 } else {
3228 wwn = INI_ANY;
3229 }
3230 isp_del_wwn_entry(isp, chan, wwn, nphdl, portid);
3231 break;
3232 case PRLO:
3233 msg = "PRLO";
3234 break;
3235 case PLOGI:
3236 case PRLI:
3237 /*
3238 * Treat PRLI the same as PLOGI and make a database entry for it.
3239 */
3240 if (inot->in_status_subcode == PLOGI) {
3241 msg = "PLOGI";
3242 } else {
3243 prli_options = inot->in_prli_options;
3244 msg = "PRLI";
3245 }
3246 if (ISP_FW_NEWER_THAN(isp, 4, 0, 25)) {
3247 ptr = (uint8_t *)inot; /* point to unswizzled entry! */
3248 wwn = (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF]) << 56) |
3249 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+1]) << 48) |
3250 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+2]) << 40) |
3251 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+3]) << 32) |
3252 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+4]) << 24) |
3253 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+5]) << 16) |
3254 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+6]) << 8) |
3255 (((uint64_t) ptr[IN24XX_PLOGI_WWPN_OFF+7]));
3256 } else {
3257 wwn = INI_NONE;
3258 }
3259 isp_add_wwn_entry(isp, chan, wwn, nphdl, portid, prli_options);
3260 break;
3261 case PDISC:
3262 msg = "PDISC";
3263 break;
3264 case ADISC:
3265 msg = "ADISC";
3266 break;
3267 default:
3268 ISP_SNPRINTF(buf, sizeof (buf), "ELS 0x%x", inot->in_status_subcode);
3269 msg = buf;
3270 break;
3271 }
3272 if (inot->in_flags & IN24XX_FLAG_PUREX_IOCB) {
3273 isp_prt(isp, ISP_LOGERR, "%s Chan %d ELS N-port handle %x PortID 0x%06x marked as needing a PUREX response", msg, chan, nphdl, portid);
3274 break;
3275 }
3276 isp_prt(isp, ISP_LOGTDEBUG0, "%s Chan %d ELS N-port handle %x PortID 0x%06x RX_ID 0x%x OX_ID 0x%x", msg, chan, nphdl, portid,
3277 inot->in_rxid, inot->in_oxid);
3278 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3279 break;
3280 }
3281
3282 case IN24XX_PORT_LOGOUT:
3283 ptr = "PORT LOGOUT";
3284 if (isp_find_pdb_by_handle(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), nphdl, &lp)) {
3285 isp_del_wwn_entry(isp, ISP_GET_VPIDX(isp, inot->in_vpidx), lp->port_wwn, nphdl, lp->portid);
3286 }
3287 /* FALLTHROUGH */
3288 case IN24XX_PORT_CHANGED:
3289 if (ptr == NULL) {
3290 ptr = "PORT CHANGED";
3291 }
3292 /* FALLTHROUGH */
3293 case IN24XX_LIP_RESET:
3294 if (ptr == NULL) {
3295 ptr = "LIP RESET";
3296 }
3297 isp_prt(isp, ISP_LOGINFO, "Chan %d %s (sub-status 0x%x) for N-port handle 0x%x", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr, inot->in_status_subcode, nphdl);
3298
3299 /*
3300 * All subcodes here are irrelevant. What is relevant
3301 * is that we need to terminate all active commands from
3302 * this initiator (known by N-port handle).
3303 */
3304 /* XXX IMPLEMENT XXX */
3305 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3306 break;
3307
3308 case IN24XX_SRR_RCVD:
3309#ifdef ISP_TARGET_MODE
3310 isp_handle_srr_notify(isp, inot);
3311 break;
3312#else
3313 if (ptr == NULL) {
3314 ptr = "SRR RCVD";
3315 }
3316 /* FALLTHROUGH */
3317#endif
3318 case IN24XX_LINK_RESET:
3319 if (ptr == NULL) {
3320 ptr = "LINK RESET";
3321 }
3322 case IN24XX_LINK_FAILED:
3323 if (ptr == NULL) {
3324 ptr = "LINK FAILED";
3325 }
3326 default:
3327 isp_prt(isp, ISP_LOGWARN, "Chan %d %s", ISP_GET_VPIDX(isp, inot->in_vpidx), ptr);
3328 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, inot);
3329 break;
3330 }
3331}
3332
3333static int
3334isp_handle_platform_target_notify_ack(ispsoftc_t *isp, isp_notify_t *mp)
3335{
3336
3337 if (isp->isp_state != ISP_RUNSTATE) {
3338 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) acked- h/w not ready (dropping)", mp->nt_ncode, mp->nt_lreserved != NULL);
3339 return (0);
3340 }
3341
3342 /*
3343 * This case is for a Task Management Function, which shows up as an ATIO7 entry.
3344 */
3345 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ATIO) {
3346 ct7_entry_t local, *cto = &local;
3347 at7_entry_t *aep = (at7_entry_t *)mp->nt_lreserved;
3348 fcportdb_t *lp;
3349 uint32_t sid;
3350 uint16_t nphdl;
3351
3352 sid = (aep->at_hdr.s_id[0] << 16) | (aep->at_hdr.s_id[1] << 8) | aep->at_hdr.s_id[2];
3353 if (isp_find_pdb_by_sid(isp, mp->nt_channel, sid, &lp)) {
3354 nphdl = lp->handle;
3355 } else {
3356 nphdl = NIL_HANDLE;
3357 }
3358 ISP_MEMZERO(&local, sizeof (local));
3359 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
3360 cto->ct_header.rqs_entry_count = 1;
3361 cto->ct_nphdl = nphdl;
3362 cto->ct_rxid = aep->at_rxid;
3363 cto->ct_vpidx = mp->nt_channel;
3364 cto->ct_iid_lo = sid;
3365 cto->ct_iid_hi = sid >> 16;
3366 cto->ct_oxid = aep->at_hdr.ox_id;
3367 cto->ct_flags = CT7_SENDSTATUS|CT7_NOACK|CT7_NO_DATA|CT7_FLAG_MODE1;
3368 cto->ct_flags |= (aep->at_ta_len >> 12) << CT7_TASK_ATTR_SHIFT;
3369 return (isp_target_put_entry(isp, &local));
3370 }
3371
3372 /*
3373 * This case is for a responding to an ABTS frame
3374 */
3375 if (IS_24XX(isp) && mp->nt_lreserved && ((isphdr_t *)mp->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3376
3377 /*
3378 * Overload nt_need_ack here to mark whether we've terminated the associated command.
3379 */
3380 if (mp->nt_need_ack) {
3381 uint8_t storage[QENTRY_LEN];
3382 ct7_entry_t *cto = (ct7_entry_t *) storage;
3383 abts_t *abts = (abts_t *)mp->nt_lreserved;
3384
3385 ISP_MEMZERO(cto, sizeof (ct7_entry_t));
3386 isp_prt(isp, ISP_LOGTDEBUG0, "%s: [%x] terminating after ABTS received", __func__, abts->abts_rxid_task);
3387 cto->ct_header.rqs_entry_type = RQSTYPE_CTIO7;
3388 cto->ct_header.rqs_entry_count = 1;
3389 cto->ct_nphdl = mp->nt_nphdl;
3390 cto->ct_rxid = abts->abts_rxid_task;
3391 cto->ct_iid_lo = mp->nt_sid;
3392 cto->ct_iid_hi = mp->nt_sid >> 16;
3393 cto->ct_oxid = abts->abts_ox_id;
3394 cto->ct_vpidx = mp->nt_channel;
3395 cto->ct_flags = CT7_NOACK|CT7_TERMINATE;
3396 if (isp_target_put_entry(isp, cto)) {
3397 return (ENOMEM);
3398 }
3399 mp->nt_need_ack = 0;
3400 }
3401 if (isp_acknak_abts(isp, mp->nt_lreserved, 0) == ENOMEM) {
3402 return (ENOMEM);
3403 } else {
3404 return (0);
3405 }
3406 }
3407
3408 /*
3409 * Handle logout cases here
3410 */
3411 if (mp->nt_ncode == NT_GLOBAL_LOGOUT) {
3412 isp_del_all_wwn_entries(isp, mp->nt_channel);
3413 }
3414
3415 if (mp->nt_ncode == NT_LOGOUT) {
3416 if (!IS_2100(isp) && IS_FC(isp)) {
3417 isp_del_wwn_entries(isp, mp);
3418 }
3419 }
3420
3421 /*
3422 * General purpose acknowledgement
3423 */
3424 if (mp->nt_need_ack) {
3425 isp_prt(isp, ISP_LOGTINFO, "Notify Code 0x%x (qevalid=%d) being acked", mp->nt_ncode, mp->nt_lreserved != NULL);
3426 /*
3427 * Don't need to use the guaranteed send because the caller can retry
3428 */
3429 return (isp_notify_ack(isp, mp->nt_lreserved));
3430 }
3431 return (0);
3432}
3433
3434/*
3435 * Handle task management functions.
3436 *
3437 * We show up here with a notify structure filled out.
3438 *
3439 * The nt_lreserved tag points to the original queue entry
3440 */
3441static void
3442isp_handle_platform_target_tmf(ispsoftc_t *isp, isp_notify_t *notify)
3443{
3444 tstate_t *tptr;
3445 fcportdb_t *lp;
3446 struct ccb_immediate_notify *inot;
3447 inot_private_data_t *ntp = NULL;
3448 lun_id_t lun;
3449
3450 isp_prt(isp, ISP_LOGTDEBUG0, "%s: code 0x%x sid 0x%x tagval 0x%016llx chan %d lun 0x%x", __func__, notify->nt_ncode,
3451 notify->nt_sid, (unsigned long long) notify->nt_tagval, notify->nt_channel, notify->nt_lun);
3452 /*
3453 * NB: This assignment is necessary because of tricky type conversion.
3454 * XXX: This is tricky and I need to check this. If the lun isn't known
3455 * XXX: for the task management function, it does not of necessity follow
3456 * XXX: that it should go up stream to the wildcard listener.
3457 */
3458 if (notify->nt_lun == LUN_ANY) {
3459 lun = CAM_LUN_WILDCARD;
3460 } else {
3461 lun = notify->nt_lun;
3462 }
3463 tptr = get_lun_statep(isp, notify->nt_channel, lun);
3464 if (tptr == NULL) {
3465 tptr = get_lun_statep(isp, notify->nt_channel, CAM_LUN_WILDCARD);
3466 if (tptr == NULL) {
3467 isp_prt(isp, ISP_LOGWARN, "%s: no state pointer found for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
3468 goto bad;
3469 }
3470 }
3471 inot = (struct ccb_immediate_notify *) SLIST_FIRST(&tptr->inots);
3472 if (inot == NULL) {
3473 isp_prt(isp, ISP_LOGWARN, "%s: out of immediate notify structures for chan %d lun 0x%x", __func__, notify->nt_channel, lun);
3474 goto bad;
3475 }
3476
3477 if (isp_find_pdb_by_sid(isp, notify->nt_channel, notify->nt_sid, &lp) == 0 &&
3478 isp_find_pdb_by_handle(isp, notify->nt_channel, notify->nt_nphdl, &lp) == 0) {
3479 inot->initiator_id = CAM_TARGET_WILDCARD;
3480 } else {
3481 inot->initiator_id = FC_PORTDB_TGT(isp, notify->nt_channel, lp);
3482 }
3483 inot->seq_id = notify->nt_tagval;
3484 inot->tag_id = notify->nt_tagval >> 32;
3485
3486 switch (notify->nt_ncode) {
3487 case NT_ABORT_TASK:
3488 isp_target_mark_aborted_early(isp, tptr, inot->tag_id);
3489 inot->arg = MSG_ABORT_TASK;
3490 break;
3491 case NT_ABORT_TASK_SET:
3492 isp_target_mark_aborted_early(isp, tptr, TAG_ANY);
3493 inot->arg = MSG_ABORT_TASK_SET;
3494 break;
3495 case NT_CLEAR_ACA:
3496 inot->arg = MSG_CLEAR_ACA;
3497 break;
3498 case NT_CLEAR_TASK_SET:
3499 inot->arg = MSG_CLEAR_TASK_SET;
3500 break;
3501 case NT_LUN_RESET:
3502 inot->arg = MSG_LOGICAL_UNIT_RESET;
3503 break;
3504 case NT_TARGET_RESET:
3505 inot->arg = MSG_TARGET_RESET;
3506 break;
3507 case NT_QUERY_TASK_SET:
3508 inot->arg = MSG_QUERY_TASK_SET;
3509 break;
3510 case NT_QUERY_ASYNC_EVENT:
3511 inot->arg = MSG_QUERY_ASYNC_EVENT;
3512 break;
3513 default:
3514 isp_prt(isp, ISP_LOGWARN, "%s: unknown TMF code 0x%x for chan %d lun 0x%x", __func__, notify->nt_ncode, notify->nt_channel, lun);
3515 goto bad;
3516 }
3517
3518 ntp = isp_get_ntpd(isp, tptr);
3519 if (ntp == NULL) {
3520 isp_prt(isp, ISP_LOGWARN, "%s: out of inotify private structures", __func__);
3521 goto bad;
3522 }
3523 ISP_MEMCPY(&ntp->rd.nt, notify, sizeof (isp_notify_t));
3524 if (notify->nt_lreserved) {
3525 ISP_MEMCPY(&ntp->rd.data, notify->nt_lreserved, QENTRY_LEN);
3526 ntp->rd.nt.nt_lreserved = &ntp->rd.data;
3527 }
3528 ntp->rd.seq_id = notify->nt_tagval;
3529 ntp->rd.tag_id = notify->nt_tagval >> 32;
3530
3531 tptr->inot_count--;
3532 SLIST_REMOVE_HEAD(&tptr->inots, sim_links.sle);
3533 rls_lun_statep(isp, tptr);
3534 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, inot->ccb_h.path, "%s: Take FREE INOT count now %d\n", __func__, tptr->inot_count);
3535 inot->ccb_h.status = CAM_MESSAGE_RECV;
3536 xpt_done((union ccb *)inot);
3537 return;
3538bad:
3539 if (tptr) {
3540 rls_lun_statep(isp, tptr);
3541 }
3542 if (notify->nt_need_ack && notify->nt_lreserved) {
3543 if (((isphdr_t *)notify->nt_lreserved)->rqs_entry_type == RQSTYPE_ABTS_RCVD) {
3544 if (isp_acknak_abts(isp, notify->nt_lreserved, ENOMEM)) {
3545 isp_prt(isp, ISP_LOGWARN, "you lose- unable to send an ACKNAK");
3546 }
3547 } else {
3548 isp_async(isp, ISPASYNC_TARGET_NOTIFY_ACK, notify->nt_lreserved);
3549 }
3550 }
3551}
3552
3553/*
3554 * Find the associated private data and mark it as dead so
3555 * we don't try to work on it any further.
3556 */
3557static void
3558isp_target_mark_aborted(ispsoftc_t *isp, union ccb *ccb)
3559{
3560 tstate_t *tptr;
3561 atio_private_data_t *atp;
3562 union ccb *accb = ccb->cab.abort_ccb;
3563
3564 tptr = get_lun_statep(isp, XS_CHANNEL(accb), XS_LUN(accb));
3565 if (tptr == NULL) {
3566 tptr = get_lun_statep(isp, XS_CHANNEL(accb), CAM_LUN_WILDCARD);
3567 if (tptr == NULL) {
3568 ccb->ccb_h.status = CAM_REQ_INVALID;
3569 return;
3570 }
3571 }
3572
3573 atp = isp_find_atpd(isp, tptr, accb->atio.tag_id);
3574 if (atp == NULL) {
3575 ccb->ccb_h.status = CAM_REQ_INVALID;
3576 } else {
3577 atp->dead = 1;
3578 ccb->ccb_h.status = CAM_REQ_CMP;
3579 }
3580 rls_lun_statep(isp, tptr);
3581}
3582
3583static void
3584isp_target_mark_aborted_early(ispsoftc_t *isp, tstate_t *tptr, uint32_t tag_id)
3585{
3586 atio_private_data_t *atp;
3587 inot_private_data_t *restart_queue = tptr->restart_queue;
3588
3589 /*
3590 * First, clean any commands pending restart
3591 */
3592 tptr->restart_queue = NULL;
3593 while (restart_queue) {
3594 uint32_t this_tag_id;
3595 inot_private_data_t *ntp = restart_queue;
3596
3597 restart_queue = ntp->rd.nt.nt_hba;
3598
3599 if (IS_24XX(isp)) {
3600 this_tag_id = ((at7_entry_t *)ntp->rd.data)->at_rxid;
3601 } else {
3602 this_tag_id = ((at2_entry_t *)ntp->rd.data)->at_rxid;
3603 }
3604 if ((uint64_t)tag_id == TAG_ANY || tag_id == this_tag_id) {
3605 isp_put_ntpd(isp, tptr, ntp);
3606 } else {
3607 ntp->rd.nt.nt_hba = tptr->restart_queue;
3608 tptr->restart_queue = ntp;
3609 }
3610 }
3611
3612 /*
3613 * Now mark other ones dead as well.
3614 */
3615 for (atp = tptr->atpool; atp < &tptr->atpool[ATPDPSIZE]; atp++) {
3616 if ((uint64_t)tag_id == TAG_ANY || atp->tag == tag_id) {
3617 atp->dead = 1;
3618 }
3619 }
3620}
3621
3622
3623#ifdef ISP_INTERNAL_TARGET
3624//#define ISP_SEPARATE_STATUS 1
3625#define ISP_MULTI_CCBS 1
3626#if defined(ISP_MULTI_CCBS) && !defined(ISP_SEPARATE_STATUS)
3627#define ISP_SEPARATE_STATUS 1
3628#endif
3629
3630typedef struct periph_private_data_t {
3631 union ccb *ccb; /* original ATIO or Immediate Notify */
3632 unsigned long offset; /* current offset */
3633 int sequence; /* current CTIO sequence */
3634 int ctio_cnt; /* current # of ctio's outstanding */
3635 int
3636 status_sent : 1,
3637 on_queue : 1; /* on restart queue */
3638} ppd_t;
3639/*
3640 * Each ATIO we allocate will have periph private data associated with it
3641 * that maintains per-command state. This private to each ATIO.
3642 */
3643#define ATIO_PPD(ccb) ((ppd_t *)(((struct ccb_hdr *)ccb)->ppriv_ptr0))
3644/*
3645 * Each CTIO we send downstream will get a pointer to the ATIO itself
3646 * so that on completion we can retrieve that pointer.
3647 */
3648#define ccb_atio ppriv_ptr1
3649#define ccb_inot ppriv_ptr1
3650
3651/*
3652 * Each CTIO we send downstream will contain a sequence number
3653 */
3654#define CTIO_SEQ(ccb) ccb->ccb_h.ppriv_field0
3655
3656#define MAX_ISP_TARG_TRANSFER (2 << 20)
3657#define NISP_TARG_CMDS 64
3658#define NISP_TARG_NOTIFIES 64
3659#define DISK_SHIFT 9
3660#define JUNK_SIZE 256
3661#define MULTI_CCB_DATA_LIM 8192
3662//#define MULTI_CCB_DATA_CNT 64
3663#define MULTI_CCB_DATA_CNT 8
3664
3665extern u_int vm_kmem_size;
3666static int ca;
3667static uint32_t disk_size;
3668static uint8_t *disk_data = NULL;
3669static uint8_t *junk_data;
3670static MALLOC_DEFINE(M_ISPTARG, "ISPTARG", "ISP TARGET data");
3671struct isptarg_softc {
3672 /* CCBs (CTIOs, ATIOs, INOTs) pending on the controller */
3673 struct isp_ccbq work_queue;
3674 struct isp_ccbq rework_queue;
3675 struct isp_ccbq running_queue;
3676 struct isp_ccbq inot_queue;
3677 struct cam_periph *periph;
3678 struct cam_path *path;
3679 ispsoftc_t *isp;
3680};
3681static periph_ctor_t isptargctor;
3682static periph_dtor_t isptargdtor;
3683static periph_start_t isptargstart;
3684static periph_init_t isptarginit;
3685static void isptarg_done(struct cam_periph *, union ccb *);
3686static void isptargasync(void *, u_int32_t, struct cam_path *, void *);
3687
3688
3689static int isptarg_rwparm(uint8_t *, uint8_t *, uint64_t, uint32_t, uint8_t **, uint32_t *, int *);
3690
3691static struct periph_driver isptargdriver =
3692{
3693 isptarginit, "isptarg", TAILQ_HEAD_INITIALIZER(isptargdriver.units), 0
3694};
3695
3696static void
3697isptarginit(void)
3698{
3699}
3700
3701static void
3702isptargnotify(ispsoftc_t *isp, union ccb *iccb, struct ccb_immediate_notify *inot)
3703{
3704 struct ccb_notify_acknowledge *ack = &iccb->cna2;
3705
3706 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, inot->ccb_h.path, "%s: [0x%x] immediate notify for 0x%x from 0x%x status 0x%x arg 0x%x\n", __func__,
3707 inot->tag_id, inot->initiator_id, inot->seq_id, inot->ccb_h.status, inot->arg);
3708 ack->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
3709 ack->ccb_h.flags = 0;
3710 ack->ccb_h.retry_count = 0;
3711 ack->ccb_h.cbfcnp = isptarg_done;
3712 ack->ccb_h.timeout = 0;
3713 ack->ccb_h.ccb_inot = inot;
3714 ack->tag_id = inot->tag_id;
3715 ack->seq_id = inot->seq_id;
3716 ack->initiator_id = inot->initiator_id;
3717 xpt_action(iccb);
3718}
3719
3720static void
3721isptargstart(struct cam_periph *periph, union ccb *iccb)
3722{
3723 const uint8_t niliqd[SHORT_INQUIRY_LENGTH] = {
3724 0x7f, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3725 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3726 'S', 'C', 'S', 'I', ' ', 'N', 'U', 'L',
3727 'L', ' ', 'D', 'E', 'V', 'I', 'C', 'E',
3728 '0', '0', '0', '1'
3729 };
3730 const uint8_t iqd[SHORT_INQUIRY_LENGTH] = {
3731 0, 0x0, 0x5, 0x2, 32, 0, 0, 0x32,
3732 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
3733 'S', 'C', 'S', 'I', ' ', 'M', 'E', 'M',
3734 'O', 'R', 'Y', ' ', 'D', 'I', 'S', 'K',
3735 '0', '0', '0', '1'
3736 };
3737 int r, i, more = 0, last, is_data_cmd = 0, is_write;
3738 char *queue;
3739 struct isptarg_softc *softc = periph->softc;
3740 struct ccb_scsiio *csio;
3741 lun_id_t return_lun;
3742 struct ccb_accept_tio *atio;
3743 uint8_t *cdb, *ptr, status;
3744 uint8_t *data_ptr;
3745 uint32_t data_len, flags;
3746 struct ccb_hdr *ccbh;
3747
3748 mtx_assert(periph->sim->mtx, MA_OWNED);
3749 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, iccb->ccb_h.path, "%s: function code 0x%x INOTQ=%c WORKQ=%c REWORKQ=%c\n", __func__, iccb->ccb_h.func_code,
3750 TAILQ_FIRST(&softc->inot_queue)? 'y' : 'n', TAILQ_FIRST(&softc->work_queue)? 'y' : 'n', TAILQ_FIRST(&softc->rework_queue)? 'y' : 'n');
3751 /*
3752 * Check for immediate notifies first
3753 */
3754 ccbh = TAILQ_FIRST(&softc->inot_queue);
3755 if (ccbh) {
3756 TAILQ_REMOVE(&softc->inot_queue, ccbh, periph_links.tqe);
3757 if (TAILQ_FIRST(&softc->inot_queue) || TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue)) {
3758 xpt_schedule(periph, 1);
3759 }
3760 isptargnotify(softc->isp, iccb, (struct ccb_immediate_notify *)ccbh);
3761 return;
3762 }
3763
3764 /*
3765 * Check the rework (continuation) work queue first.
3766 */
3767 ccbh = TAILQ_FIRST(&softc->rework_queue);
3768 if (ccbh) {
3769 atio = (struct ccb_accept_tio *)ccbh;
3770 TAILQ_REMOVE(&softc->rework_queue, ccbh, periph_links.tqe);
3771 more = TAILQ_FIRST(&softc->work_queue) || TAILQ_FIRST(&softc->rework_queue);
3772 queue = "rework";
3773 } else {
3774 ccbh = TAILQ_FIRST(&softc->work_queue);
3775 if (ccbh == NULL) {
3776 xpt_release_ccb(iccb);
3777 return;
3778 }
3779 atio = (struct ccb_accept_tio *)ccbh;
3780 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
3781 more = TAILQ_FIRST(&softc->work_queue) != NULL;
3782 queue = "work";
3783 }
3784 ATIO_PPD(atio)->on_queue = 0;
3785
3786 if (atio->tag_id == 0xffffffff || atio->ccb_h.func_code != XPT_ACCEPT_TARGET_IO) {
3787 panic("BAD ATIO");
3788 }
3789
3790 data_len = is_write = 0;
3791 data_ptr = NULL;
3792 csio = &iccb->csio;
3793 status = SCSI_STATUS_OK;
3794 flags = CAM_SEND_STATUS;
3795 memset(&atio->sense_data, 0, sizeof (atio->sense_data));
3796 cdb = atio->cdb_io.cdb_bytes;
3797 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, ccbh->path, "%s: [0x%x] processing ATIO from %s queue initiator 0x%x CDB=0x%x data_offset=%u\n", __func__, atio->tag_id,
3798 queue, atio->init_id, cdb[0], ATIO_PPD(atio)->offset);
3799
3800 return_lun = XS_LUN(atio);
3801 if (return_lun != 0) {
3802 xpt_print(atio->ccb_h.path, "[0x%x] Non-Zero Lun %d: cdb0=0x%x\n", atio->tag_id, return_lun, cdb[0]);
3803 if (cdb[0] != INQUIRY && cdb[0] != REPORT_LUNS && cdb[0] != REQUEST_SENSE) {
3804 status = SCSI_STATUS_CHECK_COND;
3805 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3806 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3807 SDFIXED(atio->sense_data)->add_sense_code = 0x25; /* LOGICAL UNIT NOT SUPPORTED */
3808 atio->sense_len = SSD_MIN_SIZE;
3809 }
3810 return_lun = CAM_LUN_WILDCARD;
3811 }
3812
3813 switch (cdb[0]) {
3814 case REQUEST_SENSE:
3815 flags |= CAM_DIR_IN;
3816 data_len = sizeof (atio->sense_data);
3817 junk_data[0] = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR|SSD_KEY_NO_SENSE;
3818 memset(junk_data+1, 0, data_len-1);
3819 if (data_len > cdb[4]) {
3820 data_len = cdb[4];
3821 }
3822 if (data_len) {
3823 data_ptr = junk_data;
3824 }
3825 break;
3826 case WRITE_6:
3827 case WRITE_10:
3828 case WRITE_12:
3829 case WRITE_16:
3830 is_write = 1;
3831 /* FALLTHROUGH */
3832 case READ_6:
3833 case READ_10:
3834 case READ_12:
3835 case READ_16:
3836 is_data_cmd = 1;
3837 r = isptarg_rwparm(cdb, disk_data, disk_size, ATIO_PPD(atio)->offset, &data_ptr, &data_len, &last);
3838 if (r != 0) {
3839 status = SCSI_STATUS_CHECK_COND;
3840 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3841 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3842 if (r == -1) {
3843 SDFIXED(atio->sense_data)->add_sense_code = 0x21; /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
3844 } else {
3845 SDFIXED(atio->sense_data)->add_sense_code = 0x20; /* INVALID COMMAND OPERATION CODE */
3846 }
3847 atio->sense_len = SSD_MIN_SIZE;
3848 } else {
3849#ifdef ISP_SEPARATE_STATUS
3850 if (last && data_len) {
3851 last = 0;
3852 }
3853#endif
3854 if (last == 0) {
3855 flags &= ~CAM_SEND_STATUS;
3856 }
3857 if (data_len) {
3858 ATIO_PPD(atio)->offset += data_len;
3859 if (is_write)
3860 flags |= CAM_DIR_OUT;
3861 else
3862 flags |= CAM_DIR_IN;
3863 } else {
3864 flags |= CAM_DIR_NONE;
3865 }
3866 }
3867 break;
3868 case INQUIRY:
3869 flags |= CAM_DIR_IN;
3870 if (cdb[1] || cdb[2] || cdb[3]) {
3871 status = SCSI_STATUS_CHECK_COND;
3872 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3873 SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
3874 SDFIXED(atio->sense_data)->add_sense_code = 0x24; /* INVALID FIELD IN CDB */
3875 atio->sense_len = SSD_MIN_SIZE;
3876 break;
3877 }
3878 data_len = sizeof (iqd);
3879 if (data_len > cdb[4]) {
3880 data_len = cdb[4];
3881 }
3882 if (data_len) {
3883 if (XS_LUN(iccb) != 0) {
3884 memcpy(junk_data, niliqd, sizeof (iqd));
3885 } else {
3886 memcpy(junk_data, iqd, sizeof (iqd));
3887 }
3888 data_ptr = junk_data;
3889 }
3890 break;
3891 case TEST_UNIT_READY:
3892 flags |= CAM_DIR_NONE;
3893 if (ca) {
3894 ca = 0;
3895 status = SCSI_STATUS_CHECK_COND;
3896 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3897 SDFIXED(atio->sense_data)->flags = SSD_KEY_UNIT_ATTENTION;
3898 SDFIXED(atio->sense_data)->add_sense_code = 0x29; /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
3899 atio->sense_len = SSD_MIN_SIZE;
3900 }
3901 break;
3902 case SYNCHRONIZE_CACHE:
3903 case START_STOP:
3904 case RESERVE:
3905 case RELEASE:
3906 case VERIFY_10:
3907 flags |= CAM_DIR_NONE;
3908 break;
3909
3910 case READ_CAPACITY:
3911 flags |= CAM_DIR_IN;
3912 if (cdb[2] || cdb[3] || cdb[4] || cdb[5]) {
3913 status = SCSI_STATUS_CHECK_COND;
3914 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3915 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3916 SDFIXED(atio->sense_data)->add_sense_code = 0x24; /* INVALID FIELD IN CDB */
3917 atio->sense_len = SSD_MIN_SIZE;
3918 break;
3919 }
3920 if (cdb[8] & 0x1) { /* PMI */
3921 junk_data[0] = 0xff;
3922 junk_data[1] = 0xff;
3923 junk_data[2] = 0xff;
3924 junk_data[3] = 0xff;
3925 } else {
3926 uint64_t last_blk = (disk_size >> DISK_SHIFT) - 1;
3927 if (last_blk < 0xffffffffULL) {
3928 junk_data[0] = (last_blk >> 24) & 0xff;
3929 junk_data[1] = (last_blk >> 16) & 0xff;
3930 junk_data[2] = (last_blk >> 8) & 0xff;
3931 junk_data[3] = (last_blk) & 0xff;
3932 } else {
3933 junk_data[0] = 0xff;
3934 junk_data[1] = 0xff;
3935 junk_data[2] = 0xff;
3936 junk_data[3] = 0xff;
3937 }
3938 }
3939 junk_data[4] = ((1 << DISK_SHIFT) >> 24) & 0xff;
3940 junk_data[5] = ((1 << DISK_SHIFT) >> 16) & 0xff;
3941 junk_data[6] = ((1 << DISK_SHIFT) >> 8) & 0xff;
3942 junk_data[7] = ((1 << DISK_SHIFT)) & 0xff;
3943 data_ptr = junk_data;
3944 data_len = 8;
3945 break;
3946 case REPORT_LUNS:
3947 flags |= CAM_DIR_IN;
3948 memset(junk_data, 0, JUNK_SIZE);
3949 junk_data[0] = (1 << 3) >> 24;
3950 junk_data[1] = (1 << 3) >> 16;
3951 junk_data[2] = (1 << 3) >> 8;
3952 junk_data[3] = (1 << 3);
3953 ptr = NULL;
3954 for (i = 0; i < 1; i++) {
3955 ptr = &junk_data[8 + (i << 3)];
3956 if (i >= 256) {
3957 ptr[0] = 0x40 | ((i >> 8) & 0x3f);
3958 }
3959 ptr[1] = i;
3960 }
3961 data_ptr = junk_data;
3962 data_len = (ptr + 8) - junk_data;
3963 break;
3964
3965 default:
3966 flags |= CAM_DIR_NONE;
3967 status = SCSI_STATUS_CHECK_COND;
3968 SDFIXED(atio->sense_data)->error_code = SSD_ERRCODE_VALID|SSD_CURRENT_ERROR;
3969 SDFIXED(atio->sense_data)->flags = SSD_KEY_ILLEGAL_REQUEST;
3970 SDFIXED(atio->sense_data)->add_sense_code = 0x20; /* INVALID COMMAND OPERATION CODE */
3971 atio->sense_len = SSD_MIN_SIZE;
3972 break;
3973 }
3974
3975 /*
3976 * If we are done with the transaction, tell the
3977 * controller to send status and perform a CMD_CMPLT.
3978 * If we have associated sense data, see if we can
3979 * send that too.
3980 */
3981 if (status == SCSI_STATUS_CHECK_COND) {
3982 flags |= CAM_SEND_SENSE;
3983 csio->sense_len = atio->sense_len;
3984 csio->sense_data = atio->sense_data;
3985 flags &= ~CAM_DIR_MASK;
3986 data_len = 0;
3987 data_ptr = NULL;
3988 }
3989 cam_fill_ctio(csio, 0, isptarg_done, flags, MSG_SIMPLE_Q_TAG, atio->tag_id, atio->init_id, status, data_ptr, data_len, 30 * hz);
3990 iccb->ccb_h.target_id = atio->ccb_h.target_id;
3991 iccb->ccb_h.target_lun = return_lun;
3992 iccb->ccb_h.ccb_atio = atio;
3993 CTIO_SEQ(iccb) = ATIO_PPD(atio)->sequence++;
3994 ATIO_PPD(atio)->ctio_cnt++;
3995 if (flags & CAM_SEND_STATUS) {
3996 KASSERT((ATIO_PPD(atio)->status_sent == 0), ("we have already sent status for 0x%x in %s", atio->tag_id, __func__));
3997 ATIO_PPD(atio)->status_sent = 1;
3998 }
3999 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, atio->ccb_h.path, "%s: sending downstream for 0x%x sequence %u len %u flags %x\n", __func__, atio->tag_id, CTIO_SEQ(iccb), data_len, flags);
4000 xpt_action(iccb);
4001
4002 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4003 cam_release_devq(periph->path, 0, 0, 0, 0);
4004 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
4005 }
4006#ifdef ISP_MULTI_CCBS
4007 if (is_data_cmd && ATIO_PPD(atio)->status_sent == 0 && ATIO_PPD(atio)->ctio_cnt < MULTI_CCB_DATA_CNT && ATIO_PPD(atio)->on_queue == 0) {
4008 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG0, atio->ccb_h.path, "%s: more still to do for 0x%x\n", __func__, atio->tag_id);
4009 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
4010 ATIO_PPD(atio)->on_queue = 1;
4011 more = 1;
4012 }
4013#endif
4014 if (more) {
4015 xpt_schedule(periph, 1);
4016 }
4017}
4018
4019static cam_status
4020isptargctor(struct cam_periph *periph, void *arg)
4021{
4022 struct isptarg_softc *softc;
4023
4024 softc = (struct isptarg_softc *)arg;
4025 periph->softc = softc;
4026 softc->periph = periph;
4027 softc->path = periph->path;
4028 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
4029 return (CAM_REQ_CMP);
4030}
4031
4032static void
4033isptargdtor(struct cam_periph *periph)
4034{
4035 struct isptarg_softc *softc;
4036 softc = (struct isptarg_softc *)periph->softc;
4037 ISP_PATH_PRT(softc->isp, ISP_LOGTDEBUG1, periph->path, "%s called\n", __func__);
4038 softc->periph = NULL;
4039 softc->path = NULL;
4040 periph->softc = NULL;
4041}
4042
4043static void
4044isptarg_done(struct cam_periph *periph, union ccb *ccb)
4045{
4046 struct isptarg_softc *softc;
4047 ispsoftc_t *isp;
4048 uint32_t newoff;
4049 struct ccb_accept_tio *atio;
4050 struct ccb_immediate_notify *inot;
4051 cam_status status;
4052
4053 softc = (struct isptarg_softc *)periph->softc;
4054 isp = softc->isp;
4055 status = ccb->ccb_h.status & CAM_STATUS_MASK;
4056
4057 switch (ccb->ccb_h.func_code) {
4058 case XPT_ACCEPT_TARGET_IO:
4059 atio = (struct ccb_accept_tio *) ccb;
4060 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] ATIO seen in %s\n", atio->tag_id, __func__);
4061 memset(ATIO_PPD(atio), 0, sizeof (ppd_t));
4062 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h, periph_links.tqe);
4063 ATIO_PPD(atio)->on_queue = 1;
4064 xpt_schedule(periph, 1);
4065 break;
4066 case XPT_IMMEDIATE_NOTIFY:
4067 inot = (struct ccb_immediate_notify *) ccb;
4068 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] INOT for 0x%x seen in %s\n", inot->tag_id, inot->seq_id, __func__);
4069 TAILQ_INSERT_TAIL(&softc->inot_queue, &ccb->ccb_h, periph_links.tqe);
4070 xpt_schedule(periph, 1);
4071 break;
4072 case XPT_CONT_TARGET_IO:
4073 atio = ccb->ccb_h.ccb_atio;
4074 KASSERT((ATIO_PPD(atio)->ctio_cnt != 0), ("ctio zero when finishing a CTIO"));
4075 ATIO_PPD(atio)->ctio_cnt--;
4076 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4077 switch (ccb->ccb_h.status & CAM_STATUS_MASK) {
4078 case CAM_MESSAGE_RECV:
4079 newoff = (ccb->csio.msg_ptr[3] << 24) | (ccb->csio.msg_ptr[4] << 16) | (ccb->csio.msg_ptr[5] << 8) | (ccb->csio.msg_ptr[6]);
4080 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "[0x%x] got message to return to reset offset to 0x%x at sequence %u\n", atio->tag_id, newoff, CTIO_SEQ(ccb));
4081 ATIO_PPD(atio)->offset = newoff;
4082 ATIO_PPD(atio)->status_sent = 0;
4083 if (ATIO_PPD(atio)->on_queue == 0) {
4084 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
4085 ATIO_PPD(atio)->on_queue = 1;
4086 }
4087 xpt_schedule(periph, 1);
4088 break;
4089 default:
4090 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
4091 xpt_action((union ccb *)atio);
4092 break;
4093 }
4094 } else if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
4095 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] MID CTIO sequence %u seen in %s\n", atio->tag_id, CTIO_SEQ(ccb), __func__);
4096 if (ATIO_PPD(atio)->status_sent == 0 && ATIO_PPD(atio)->on_queue == 0) {
4097 TAILQ_INSERT_TAIL(&softc->rework_queue, &atio->ccb_h, periph_links.tqe);
4098 ATIO_PPD(atio)->on_queue = 1;
4099 }
4100 xpt_schedule(periph, 1);
4101 } else {
4102 KASSERT((ATIO_PPD(atio)->ctio_cnt == 0), ("ctio count still %d when we think we've sent the STATUS ctio", ATIO_PPD(atio)->ctio_cnt));
4103 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "[0x%x] FINAL CTIO sequence %u seen in %s\n", atio->tag_id, CTIO_SEQ(ccb), __func__);
4104 xpt_action((union ccb *)atio);
4105 }
4106 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4107 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
4108 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
4109 }
4110 xpt_release_ccb(ccb);
4111 break;
4112 case XPT_NOTIFY_ACKNOWLEDGE:
4113 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
4114 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
4115 ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
4116 }
4117 inot = ccb->ccb_h.ccb_inot;
4118 ISP_PATH_PRT(isp, ISP_LOGTDEBUG1, inot->ccb_h.path, "[0x%x] recycle notify for tag 0x%x\n", inot->tag_id, inot->seq_id);
4119 xpt_release_ccb(ccb);
4120 xpt_action((union ccb *)inot);
4121 break;
4122 default:
4123 xpt_print(ccb->ccb_h.path, "unexpected code 0x%x\n", ccb->ccb_h.func_code);
4124 break;
4125 }
4126}
4127
4128static void
4129isptargasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
4130{
4131 struct ac_contract *acp = arg;
4132 struct ac_device_changed *fc = (struct ac_device_changed *) acp->contract_data;
4133
4134 if (code != AC_CONTRACT) {
4135 return;
4136 }
4137 xpt_print(path, "0x%016llx Port ID 0x%06x %s\n", (unsigned long long) fc->wwpn, fc->port, fc->arrived? "arrived" : "departed");
4138}
4139
4140static void
4141isp_target_thread(ispsoftc_t *isp, int chan)
4142{
4143 union ccb *ccb = NULL;
4144 int i;
4145 void *wchan;
4146 cam_status status;
4147 struct isptarg_softc *softc = NULL;
4148 struct cam_periph *periph = NULL, *wperiph = NULL;
4149 struct cam_path *path, *wpath;
4150 struct cam_sim *sim;
4151
4152 if (disk_data == NULL) {
4153 disk_size = roundup2(vm_kmem_size >> 1, (1ULL << 20));
4154 if (disk_size < (50 << 20)) {
4155 disk_size = 50 << 20;
4156 }
4157 disk_data = malloc(disk_size, M_ISPTARG, M_WAITOK | M_ZERO);
4158 if (disk_data == NULL) {
4159 isp_prt(isp, ISP_LOGERR, "%s: could not allocate disk data", __func__);
4160 goto out;
4161 }
4162 isp_prt(isp, ISP_LOGINFO, "allocated a %ju MiB disk", (uintmax_t) (disk_size >> 20));
4163 }
4164 junk_data = malloc(JUNK_SIZE, M_ISPTARG, M_WAITOK | M_ZERO);
4165 if (junk_data == NULL) {
4166 isp_prt(isp, ISP_LOGERR, "%s: could not allocate junk", __func__);
4167 goto out;
4168 }
4169
4170
4171 softc = malloc(sizeof (*softc), M_ISPTARG, M_WAITOK | M_ZERO);
4172 if (softc == NULL) {
4173 isp_prt(isp, ISP_LOGERR, "%s: could not allocate softc", __func__);
4174 goto out;
4175 }
4176 TAILQ_INIT(&softc->work_queue);
4177 TAILQ_INIT(&softc->rework_queue);
4178 TAILQ_INIT(&softc->running_queue);
4179 TAILQ_INIT(&softc->inot_queue);
4180 softc->isp = isp;
4181
4182 periphdriver_register(&isptargdriver);
4183 ISP_GET_PC(isp, chan, sim, sim);
4184 ISP_GET_PC(isp, chan, path, path);
4185 status = xpt_create_path(&wpath, NULL, cam_sim_path(sim), CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4186 if (status != CAM_REQ_CMP) {
4187 isp_prt(isp, ISP_LOGERR, "%s: could not allocate wildcard path", __func__);
4188 return;
4189 }
4190 status = xpt_create_path(&path, NULL, cam_sim_path(sim), 0, 0);
4191 if (status != CAM_REQ_CMP) {
4192 xpt_free_path(wpath);
4193 isp_prt(isp, ISP_LOGERR, "%s: could not allocate path", __func__);
4194 return;
4195 }
4196
4197 ISP_LOCK(isp);
4198 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, wpath, NULL, 0, softc);
4199 if (status != CAM_REQ_CMP) {
4200 ISP_UNLOCK(isp);
4201 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc for wildcard failed", __func__);
4202 goto out;
4203 }
4204 wperiph = cam_periph_find(wpath, "isptarg");
4205 if (wperiph == NULL) {
4206 ISP_UNLOCK(isp);
4207 isp_prt(isp, ISP_LOGERR, "%s: wildcard periph already allocated but doesn't exist", __func__);
4208 goto out;
4209 }
4210
4211 status = cam_periph_alloc(isptargctor, NULL, isptargdtor, isptargstart, "isptarg", CAM_PERIPH_BIO, path, NULL, 0, softc);
4212 if (status != CAM_REQ_CMP) {
4213 ISP_UNLOCK(isp);
4214 isp_prt(isp, ISP_LOGERR, "%s: cam_periph_alloc failed", __func__);
4215 goto out;
4216 }
4217
4218 periph = cam_periph_find(path, "isptarg");
4219 if (periph == NULL) {
4220 ISP_UNLOCK(isp);
4221 isp_prt(isp, ISP_LOGERR, "%s: periph already allocated but doesn't exist", __func__);
4222 goto out;
4223 }
4224
4225 status = xpt_register_async(AC_CONTRACT, isptargasync, isp, wpath);
4226 if (status != CAM_REQ_CMP) {
4227 ISP_UNLOCK(isp);
4228 isp_prt(isp, ISP_LOGERR, "%s: xpt_register_async failed", __func__);
4229 goto out;
4230 }
4231
4232 ISP_UNLOCK(isp);
4233
4234 ccb = xpt_alloc_ccb();
4235
4236 /*
4237 * Make sure role is none.
4238 */
4239 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
4240 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
4241 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
4242 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
4243
4244 ISP_LOCK(isp);
4245 xpt_action(ccb);
4246 ISP_UNLOCK(isp);
4247
4248 /*
4249 * Now enable luns
4250 */
4251 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
4252 ccb->ccb_h.func_code = XPT_EN_LUN;
4253 ccb->cel.enable = 1;
4254 ISP_LOCK(isp);
4255 xpt_action(ccb);
4256 ISP_UNLOCK(isp);
4257 if (ccb->ccb_h.status != CAM_REQ_CMP) {
4258 xpt_free_ccb(ccb);
4259 xpt_print(periph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
4260 goto out;
4261 }
4262
4263 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 10);
4264 ccb->ccb_h.func_code = XPT_EN_LUN;
4265 ccb->cel.enable = 1;
4266 ISP_LOCK(isp);
4267 xpt_action(ccb);
4268 ISP_UNLOCK(isp);
4269 if (ccb->ccb_h.status != CAM_REQ_CMP) {
4270 xpt_free_ccb(ccb);
4271 xpt_print(wperiph->path, "failed to enable lun (0x%x)\n", ccb->ccb_h.status);
4272 goto out;
4273 }
4274 xpt_free_ccb(ccb);
4275
4276 /*
4277 * Add resources
4278 */
4279 ISP_GET_PC(isp, chan, target_proc, wchan);
4280 for (i = 0; i < 4; i++) {
4281 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4282 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
4283 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
4284 ccb->ccb_h.cbfcnp = isptarg_done;
4285 ccb->ccb_h.ppriv_ptr0 = malloc(sizeof (ppd_t), M_ISPTARG, M_WAITOK | M_ZERO);
4286 ISP_LOCK(isp);
4287 xpt_action(ccb);
4288 ISP_UNLOCK(isp);
4289 }
4290 for (i = 0; i < NISP_TARG_CMDS; i++) {
4291 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4292 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
4293 ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
4294 ccb->ccb_h.cbfcnp = isptarg_done;
4295 ccb->ccb_h.ppriv_ptr0 = malloc(sizeof (ppd_t), M_ISPTARG, M_WAITOK | M_ZERO);
4296 ISP_LOCK(isp);
4297 xpt_action(ccb);
4298 ISP_UNLOCK(isp);
4299 }
4300 for (i = 0; i < 4; i++) {
4301 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4302 xpt_setup_ccb(&ccb->ccb_h, wperiph->path, 1);
4303 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
4304 ccb->ccb_h.cbfcnp = isptarg_done;
4305 ISP_LOCK(isp);
4306 xpt_action(ccb);
4307 ISP_UNLOCK(isp);
4308 }
4309 for (i = 0; i < NISP_TARG_NOTIFIES; i++) {
4310 ccb = malloc(sizeof (*ccb), M_ISPTARG, M_WAITOK | M_ZERO);
4311 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1);
4312 ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
4313 ccb->ccb_h.cbfcnp = isptarg_done;
4314 ISP_LOCK(isp);
4315 xpt_action(ccb);
4316 ISP_UNLOCK(isp);
4317 }
4318
4319 /*
4320 * Now turn it all back on
4321 */
4322 xpt_setup_ccb(&ccb->ccb_h, periph->path, 10);
4323 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
4324 ccb->knob.xport_specific.fc.valid = KNOB_VALID_ROLE;
4325 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
4326 ISP_LOCK(isp);
4327 xpt_action(ccb);
4328 ISP_UNLOCK(isp);
4329
4330 /*
4331 * Okay, while things are still active, sleep...
4332 */
4333 ISP_LOCK(isp);
4334 for (;;) {
4335 ISP_GET_PC(isp, chan, proc_active, i);
4336 if (i == 0) {
4337 break;
4338 }
4339 msleep(wchan, &isp->isp_lock, PUSER, "tsnooze", 0);
4340 }
4341 ISP_UNLOCK(isp);
4342
4343out:
4344 if (wperiph) {
4345 cam_periph_invalidate(wperiph);
4346 }
4347 if (periph) {
4348 cam_periph_invalidate(periph);
4349 }
4350 if (junk_data) {
4351 free(junk_data, M_ISPTARG);
4352 }
4353 if (disk_data) {
4354 free(disk_data, M_ISPTARG);
4355 }
4356 if (softc) {
4357 free(softc, M_ISPTARG);
4358 }
4359 xpt_free_path(path);
4360 xpt_free_path(wpath);
4361}
4362
4363static void
4364isp_target_thread_pi(void *arg)
4365{
4366 struct isp_spi *pi = arg;
4367 ispsoftc_t *isp = cam_sim_softc(pi->sim);
4368 int chan = cam_sim_bus(pi->sim);
4369
4370 isp_target_thread(isp, chan);
4371 ISP_SPI_PC(isp, chan)->num_threads -= 1;
4372 kthread_exit();
4373}
4374
4375static void
4376isp_target_thread_fc(void *arg)
4377{
4378 struct isp_fc *fc = arg;
4379 ispsoftc_t *isp = cam_sim_softc(pi->sim);
4380 int chan = cam_sim_bus(pi->sim);
4381
4382 isp_target_thread(isp, chan);
4383 ISP_FC_PC(isp, chan)->num_threads -= 1;
4384 kthread_exit();
4385}
4386
4387static int
4388isptarg_rwparm(uint8_t *cdb, uint8_t *dp, uint64_t dl, uint32_t offset, uint8_t **kp, uint32_t *tl, int *lp)
4389{
4390 uint32_t cnt, curcnt;
4391 uint64_t lba;
4392
4393 switch (cdb[0]) {
4394 case WRITE_16:
4395 case READ_16:
4396 cnt = (((uint32_t)cdb[10]) << 24) |
4397 (((uint32_t)cdb[11]) << 16) |
4398 (((uint32_t)cdb[12]) << 8) |
4399 ((uint32_t)cdb[13]);
4400
4401 lba = (((uint64_t)cdb[2]) << 56) |
4402 (((uint64_t)cdb[3]) << 48) |
4403 (((uint64_t)cdb[4]) << 40) |
4404 (((uint64_t)cdb[5]) << 32) |
4405 (((uint64_t)cdb[6]) << 24) |
4406 (((uint64_t)cdb[7]) << 16) |
4407 (((uint64_t)cdb[8]) << 8) |
4408 ((uint64_t)cdb[9]);
4409 break;
4410 case WRITE_12:
4411 case READ_12:
4412 cnt = (((uint32_t)cdb[6]) << 16) |
4413 (((uint32_t)cdb[7]) << 8) |
4414 ((u_int32_t)cdb[8]);
4415
4416 lba = (((uint32_t)cdb[2]) << 24) |
4417 (((uint32_t)cdb[3]) << 16) |
4418 (((uint32_t)cdb[4]) << 8) |
4419 ((uint32_t)cdb[5]);
4420 break;
4421 case WRITE_10:
4422 case READ_10:
4423 cnt = (((uint32_t)cdb[7]) << 8) |
4424 ((u_int32_t)cdb[8]);
4425
4426 lba = (((uint32_t)cdb[2]) << 24) |
4427 (((uint32_t)cdb[3]) << 16) |
4428 (((uint32_t)cdb[4]) << 8) |
4429 ((uint32_t)cdb[5]);
4430 break;
4431 case WRITE_6:
4432 case READ_6:
4433 cnt = cdb[4];
4434 if (cnt == 0) {
4435 cnt = 256;
4436 }
4437 lba = (((uint32_t)cdb[1] & 0x1f) << 16) |
4438 (((uint32_t)cdb[2]) << 8) |
4439 ((uint32_t)cdb[3]);
4440 break;
4441 default:
4442 return (-1);
4443 }
4444
4445 cnt <<= DISK_SHIFT;
4446 lba <<= DISK_SHIFT;
4447
4448 if (offset == cnt) {
4449 *lp = 1;
4450 return (0);
4451 }
4452
4453 if (lba + cnt > dl) {
4454 return (-2);
4455 }
4456
4457 curcnt = MAX_ISP_TARG_TRANSFER;
4458 if (offset + curcnt >= cnt) {
4459 curcnt = cnt - offset;
4460 *lp = 1;
4461 } else {
4462 *lp = 0;
4463 }
4464#ifdef ISP_MULTI_CCBS
4465 if (curcnt > MULTI_CCB_DATA_LIM)
4466 curcnt = MULTI_CCB_DATA_LIM;
4467#endif
4468 *tl = curcnt;
4469 *kp = &dp[lba + offset];
4470 return (0);
4471}
4472
4473#endif
4474#endif
4475
4476static void
4477isp_cam_async(void *cbarg, uint32_t code, struct cam_path *path, void *arg)
4478{
4479 struct cam_sim *sim;
4480 int bus, tgt;
4481 ispsoftc_t *isp;
4482
4483 sim = (struct cam_sim *)cbarg;
4484 isp = (ispsoftc_t *) cam_sim_softc(sim);
4485 bus = cam_sim_bus(sim);
4486 tgt = xpt_path_target_id(path);
4487
4488 switch (code) {
4489 case AC_LOST_DEVICE:
4490 if (IS_SCSI(isp)) {
4491 uint16_t oflags, nflags;
4492 sdparam *sdp = SDPARAM(isp, bus);
4493
4494 if (tgt >= 0) {
4495 nflags = sdp->isp_devparam[tgt].nvrm_flags;
4496#ifndef ISP_TARGET_MODE
4497 nflags &= DPARM_SAFE_DFLT;
4498 if (isp->isp_loaded_fw) {
4499 nflags |= DPARM_NARROW | DPARM_ASYNC;
4500 }
4501#else
4502 nflags = DPARM_DEFAULT;
4503#endif
4504 oflags = sdp->isp_devparam[tgt].goal_flags;
4505 sdp->isp_devparam[tgt].goal_flags = nflags;
4506 sdp->isp_devparam[tgt].dev_update = 1;
4507 sdp->update = 1;
4508 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
4509 sdp->isp_devparam[tgt].goal_flags = oflags;
4510 }
4511 }
4512 break;
4513 default:
4514 isp_prt(isp, ISP_LOGWARN, "isp_cam_async: Code 0x%x", code);
4515 break;
4516 }
4517}
4518
4519static void
4520isp_poll(struct cam_sim *sim)
4521{
4522 ispsoftc_t *isp = cam_sim_softc(sim);
4523 uint32_t isr;
4524 uint16_t sema, mbox;
4523 uint16_t isr, sema, info;
4525
4524
4526 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
4527 isp_intr(isp, isr, sema, mbox);
4528 }
4525 if (ISP_READ_ISR(isp, &isr, &sema, &info))
4526 isp_intr(isp, isr, sema, info);
4529}
4530
4531
4532static void
4533isp_watchdog(void *arg)
4534{
4535 struct ccb_scsiio *xs = arg;
4536 ispsoftc_t *isp;
4537 uint32_t ohandle = ISP_HANDLE_FREE, handle;
4538
4539 isp = XS_ISP(xs);
4540
4541 handle = isp_find_handle(isp, xs);
4542
4543 /*
4544 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
4545 */
4546 if (handle != ISP_HANDLE_FREE) {
4527}
4528
4529
4530static void
4531isp_watchdog(void *arg)
4532{
4533 struct ccb_scsiio *xs = arg;
4534 ispsoftc_t *isp;
4535 uint32_t ohandle = ISP_HANDLE_FREE, handle;
4536
4537 isp = XS_ISP(xs);
4538
4539 handle = isp_find_handle(isp, xs);
4540
4541 /*
4542 * Hand crank the interrupt code just to be sure the command isn't stuck somewhere.
4543 */
4544 if (handle != ISP_HANDLE_FREE) {
4547 uint32_t isr;
4548 uint16_t sema, mbox;
4549 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) != 0) {
4550 isp_intr(isp, isr, sema, mbox);
4551 }
4545 uint16_t isr, sema, info;
4546 if (ISP_READ_ISR(isp, &isr, &sema, &info) != 0)
4547 isp_intr(isp, isr, sema, info);
4552 ohandle = handle;
4553 handle = isp_find_handle(isp, xs);
4554 }
4555 if (handle != ISP_HANDLE_FREE) {
4556 /*
4557 * Try and make sure the command is really dead before
4558 * we release the handle (and DMA resources) for reuse.
4559 *
4560 * If we are successful in aborting the command then
4561 * we're done here because we'll get the command returned
4562 * back separately.
4563 */
4564 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
4565 return;
4566 }
4567
4568 /*
4569 * Note that after calling the above, the command may in
4570 * fact have been completed.
4571 */
4572 xs = isp_find_xs(isp, handle);
4573
4574 /*
4575 * If the command no longer exists, then we won't
4576 * be able to find the xs again with this handle.
4577 */
4578 if (xs == NULL) {
4579 return;
4580 }
4581
4582 /*
4583 * After this point, the command is really dead.
4584 */
4585 if (XS_XFRLEN(xs)) {
4586 ISP_DMAFREE(isp, xs, handle);
4587 }
4588 isp_destroy_handle(isp, handle);
4589 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
4590 xs->ccb_h.status &= ~CAM_STATUS_MASK;
4591 xs->ccb_h.status |= CAM_CMD_TIMEOUT;
4592 isp_prt_endcmd(isp, xs);
4593 isp_done(xs);
4594 } else {
4595 if (ohandle != ISP_HANDLE_FREE) {
4596 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
4597 } else {
4598 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
4599 }
4600 }
4601}
4602
4603static void
4604isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
4605{
4606 union ccb *ccb;
4607 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4608
4609 if (isp_autoconfig == 0) {
4610 return;
4611 }
4612
4613 /*
4614 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
4615 */
4616 ccb = xpt_alloc_ccb_nowait();
4617 if (ccb == NULL) {
4618 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
4619 return;
4620 }
4621 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
4622 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4623 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
4624 xpt_free_ccb(ccb);
4625 return;
4626 }
4627 xpt_rescan(ccb);
4628}
4629
4630static void
4631isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
4632{
4633 struct cam_path *tp;
4634 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4635
4636 if (isp_autoconfig == 0) {
4637 return;
4638 }
4639 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
4640 xpt_async(AC_LOST_DEVICE, tp, NULL);
4641 xpt_free_path(tp);
4642 }
4643}
4644
4645/*
4646 * Gone Device Timer Function- when we have decided that a device has gone
4647 * away, we wait a specific period of time prior to telling the OS it has
4648 * gone away.
4649 *
4650 * This timer function fires once a second and then scans the port database
4651 * for devices that are marked dead but still have a virtual target assigned.
4652 * We decrement a counter for that port database entry, and when it hits zero,
4653 * we tell the OS the device has gone away.
4654 */
4655static void
4656isp_gdt(void *arg)
4657{
4658 struct isp_fc *fc = arg;
4659 taskqueue_enqueue(taskqueue_thread, &fc->gtask);
4660}
4661
4662static void
4663isp_gdt_task(void *arg, int pending)
4664{
4665 struct isp_fc *fc = arg;
4666 ispsoftc_t *isp = fc->isp;
4667 int chan = fc - isp->isp_osinfo.pc.fc;
4668 fcportdb_t *lp;
4669 struct ac_contract ac;
4670 struct ac_device_changed *adc;
4671 int dbidx, more_to_do = 0;
4672
4673 ISP_LOCK(isp);
4674 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4675 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4676 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4677
4678 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4679 continue;
4680 }
4681 if (lp->gone_timer != 0) {
4682 lp->gone_timer -= 1;
4683 more_to_do++;
4684 continue;
4685 }
4686 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
4687 if (lp->is_target) {
4688 lp->is_target = 0;
4689 isp_make_gone(isp, lp, chan, dbidx);
4690 }
4691 if (lp->is_initiator) {
4692 lp->is_initiator = 0;
4693 ac.contract_number = AC_CONTRACT_DEV_CHG;
4694 adc = (struct ac_device_changed *) ac.contract_data;
4695 adc->wwpn = lp->port_wwn;
4696 adc->port = lp->portid;
4697 adc->target = dbidx;
4698 adc->arrived = 0;
4699 xpt_async(AC_CONTRACT, fc->path, &ac);
4700 }
4701 lp->state = FC_PORTDB_STATE_NIL;
4702 }
4703 if (fc->ready) {
4704 if (more_to_do) {
4705 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4706 } else {
4707 callout_deactivate(&fc->gdt);
4708 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
4709 }
4710 }
4711 ISP_UNLOCK(isp);
4712}
4713
4714/*
4715 * Loop Down Timer Function- when loop goes down, a timer is started and
4716 * and after it expires we come here and take all probational devices that
4717 * the OS knows about and the tell the OS that they've gone away.
4718 *
4719 * We don't clear the devices out of our port database because, when loop
4720 * come back up, we have to do some actual cleanup with the chip at that
4721 * point (implicit PLOGO, e.g., to get the chip's port database state right).
4722 */
4723static void
4724isp_ldt(void *arg)
4725{
4726 struct isp_fc *fc = arg;
4727 taskqueue_enqueue(taskqueue_thread, &fc->ltask);
4728}
4729
4730static void
4731isp_ldt_task(void *arg, int pending)
4732{
4733 struct isp_fc *fc = arg;
4734 ispsoftc_t *isp = fc->isp;
4735 int chan = fc - isp->isp_osinfo.pc.fc;
4736 fcportdb_t *lp;
4737 struct ac_contract ac;
4738 struct ac_device_changed *adc;
4739 int dbidx, i;
4740
4741 ISP_LOCK(isp);
4742 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
4743 callout_deactivate(&fc->ldt);
4744
4745 /*
4746 * Notify to the OS all targets who we now consider have departed.
4747 */
4748 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4749 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4750
4751 if (lp->state == FC_PORTDB_STATE_NIL)
4752 continue;
4753
4754 /*
4755 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4756 */
4757 for (i = 0; i < isp->isp_maxcmds; i++) {
4758 struct ccb_scsiio *xs;
4759
4760 if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4761 continue;
4762 }
4763 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4764 continue;
4765 }
4766 if (dbidx != XS_TGT(xs)) {
4767 continue;
4768 }
4769 isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%d orphaned by loop down timeout",
4770 isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4771 }
4772
4773 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
4774 if (lp->is_target) {
4775 lp->is_target = 0;
4776 isp_make_gone(isp, lp, chan, dbidx);
4777 }
4778 if (lp->is_initiator) {
4779 lp->is_initiator = 0;
4780 ac.contract_number = AC_CONTRACT_DEV_CHG;
4781 adc = (struct ac_device_changed *) ac.contract_data;
4782 adc->wwpn = lp->port_wwn;
4783 adc->port = lp->portid;
4784 adc->target = dbidx;
4785 adc->arrived = 0;
4786 xpt_async(AC_CONTRACT, fc->path, &ac);
4787 }
4788 }
4789
4790 isp_unfreeze_loopdown(isp, chan);
4791 /*
4792 * The loop down timer has expired. Wake up the kthread
4793 * to notice that fact (or make it false).
4794 */
4795 fc->loop_dead = 1;
4796 fc->loop_down_time = fc->loop_down_limit+1;
4797 wakeup(fc);
4798 ISP_UNLOCK(isp);
4799}
4800
4801static void
4802isp_kthread(void *arg)
4803{
4804 struct isp_fc *fc = arg;
4805 ispsoftc_t *isp = fc->isp;
4806 int chan = fc - isp->isp_osinfo.pc.fc;
4807 int slp = 0;
4808
4809 mtx_lock(&isp->isp_osinfo.lock);
4810
4811 while (isp->isp_osinfo.is_exiting == 0) {
4812 int lb, lim;
4813
4814 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4815 lb = isp_fc_runstate(isp, chan, 250000);
4816
4817 /*
4818 * Our action is different based upon whether we're supporting
4819 * Initiator mode or not. If we are, we might freeze the simq
4820 * when loop is down and set all sorts of different delays to
4821 * check again.
4822 *
4823 * If not, we simply just wait for loop to come up.
4824 */
4825 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4826 /*
4827 * Increment loop down time by the last sleep interval
4828 */
4829 fc->loop_down_time += slp;
4830
4831 if (lb < 0) {
4832 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4833 } else {
4834 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4835 }
4836
4837 /*
4838 * If we've never seen loop up and we've waited longer
4839 * than quickboot time, or we've seen loop up but we've
4840 * waited longer than loop_down_limit, give up and go
4841 * to sleep until loop comes up.
4842 */
4843 if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4844 lim = isp_quickboot_time;
4845 } else {
4846 lim = fc->loop_down_limit;
4847 }
4848 if (fc->loop_down_time >= lim) {
4849 isp_freeze_loopdown(isp, chan, "loop limit hit");
4850 slp = 0;
4851 } else if (fc->loop_down_time < 10) {
4852 slp = 1;
4853 } else if (fc->loop_down_time < 30) {
4854 slp = 5;
4855 } else if (fc->loop_down_time < 60) {
4856 slp = 10;
4857 } else if (fc->loop_down_time < 120) {
4858 slp = 20;
4859 } else {
4860 slp = 30;
4861 }
4862
4863 } else if (lb) {
4864 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4865 fc->loop_down_time += slp;
4866 if (fc->loop_down_time > 300)
4867 slp = 0;
4868 else
4869 slp = 60;
4870 } else {
4871 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4872 fc->loop_down_time = 0;
4873 slp = 0;
4874 }
4875
4876
4877 /*
4878 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4879 * now so that CAM can start sending us commands.
4880 *
4881 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4882 * or kill the commands, as appropriate.
4883 */
4884
4885 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4886 isp_unfreeze_loopdown(isp, chan);
4887 }
4888
4889 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4890
4891 msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
4892
4893 /*
4894 * If slp is zero, we're waking up for the first time after
4895 * things have been okay. In this case, we set a deferral state
4896 * for all commands and delay hysteresis seconds before starting
4897 * the FC state evaluation. This gives the loop/fabric a chance
4898 * to settle.
4899 */
4900 if (slp == 0 && fc->hysteresis) {
4901 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4902 mtx_unlock(&isp->isp_osinfo.lock);
4903 pause("ispt", fc->hysteresis * hz);
4904 mtx_lock(&isp->isp_osinfo.lock);
4905 }
4906 }
4907 fc->num_threads -= 1;
4908 mtx_unlock(&isp->isp_osinfo.lock);
4909 kthread_exit();
4910}
4911
4912static void
4913isp_action(struct cam_sim *sim, union ccb *ccb)
4914{
4915 int bus, tgt, ts, error, lim;
4916 ispsoftc_t *isp;
4917 struct ccb_trans_settings *cts;
4918
4919 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4920
4921 isp = (ispsoftc_t *)cam_sim_softc(sim);
4922 mtx_assert(&isp->isp_lock, MA_OWNED);
4923 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4924 ISP_PCMD(ccb) = NULL;
4925
4926 if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4927 isp_init(isp);
4928 if (isp->isp_state != ISP_INITSTATE) {
4929 /*
4930 * Lie. Say it was a selection timeout.
4931 */
4932 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
4933 isp_done((struct ccb_scsiio *) ccb);
4934 return;
4935 }
4936 isp->isp_state = ISP_RUNSTATE;
4937 }
4938
4939 switch (ccb->ccb_h.func_code) {
4940 case XPT_SCSI_IO: /* Execute the requested I/O operation */
4941 bus = XS_CHANNEL(ccb);
4942 /*
4943 * Do a couple of preliminary checks...
4944 */
4945 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4946 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4947 ccb->ccb_h.status = CAM_REQ_INVALID;
4948 isp_done((struct ccb_scsiio *) ccb);
4949 break;
4950 }
4951 }
4952 ccb->csio.req_map = NULL;
4953#ifdef DIAGNOSTIC
4954 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4955 xpt_print(ccb->ccb_h.path, "invalid target\n");
4956 ccb->ccb_h.status = CAM_PATH_INVALID;
4957 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4958 xpt_print(ccb->ccb_h.path, "invalid lun\n");
4959 ccb->ccb_h.status = CAM_PATH_INVALID;
4960 }
4961 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4962 xpt_done(ccb);
4963 break;
4964 }
4965#endif
4966 ccb->csio.scsi_status = SCSI_STATUS_OK;
4967 if (isp_get_pcmd(isp, ccb)) {
4968 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4969 cam_freeze_devq(ccb->ccb_h.path);
4970 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4971 ccb->ccb_h.status = CAM_REQUEUE_REQ;
4972 xpt_done(ccb);
4973 break;
4974 }
4975 error = isp_start((XS_T *) ccb);
4976 switch (error) {
4977 case CMD_QUEUED:
4978 ccb->ccb_h.status |= CAM_SIM_QUEUED;
4979 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4980 break;
4981 }
4982 ts = ccb->ccb_h.timeout;
4983 if (ts == CAM_TIME_DEFAULT) {
4984 ts = 60*1000;
4985 }
4986 ts = isp_mstohz(ts);
4987 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4988 break;
4989 case CMD_RQLATER:
4990 /*
4991 * We get this result for FC devices if the loop state isn't ready yet
4992 * or if the device in question has gone zombie on us.
4993 *
4994 * If we've never seen Loop UP at all, we requeue this request and wait
4995 * for the initial loop up delay to expire.
4996 */
4997 lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4998 if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4999 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
5000 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
5001 } else {
5002 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d downtime (%d) > lim (%d)", XS_TGT(ccb), XS_LUN(ccb), ISP_FC_PC(isp, bus)->loop_down_time, lim);
5003 }
5004 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
5005 isp_done((struct ccb_scsiio *) ccb);
5006 break;
5007 }
5008 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
5009 cam_freeze_devq(ccb->ccb_h.path);
5010 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
5011 ccb->ccb_h.status = CAM_REQUEUE_REQ;
5012 isp_free_pcmd(isp, ccb);
5013 xpt_done(ccb);
5014 break;
5015 case CMD_EAGAIN:
5016 isp_free_pcmd(isp, ccb);
5017 cam_freeze_devq(ccb->ccb_h.path);
5018 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
5019 ccb->ccb_h.status = CAM_REQUEUE_REQ;
5020 xpt_done(ccb);
5021 break;
5022 case CMD_COMPLETE:
5023 isp_done((struct ccb_scsiio *) ccb);
5024 break;
5025 default:
5026 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
5027 ccb->ccb_h.status = CAM_REQUEUE_REQ;
5028 isp_free_pcmd(isp, ccb);
5029 xpt_done(ccb);
5030 }
5031 break;
5032
5033#ifdef ISP_TARGET_MODE
5034 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
5035 if (ccb->cel.enable) {
5036 isp_enable_lun(isp, ccb);
5037 } else {
5038 isp_disable_lun(isp, ccb);
5039 }
5040 break;
5041 case XPT_IMMED_NOTIFY:
5042 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
5043 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
5044 {
5045 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
5046 if (tptr == NULL) {
5047 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
5048 }
5049 if (tptr == NULL) {
5050 const char *str;
5051 uint32_t tag;
5052
5053 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
5054 str = "XPT_IMMEDIATE_NOTIFY";
5055 tag = ccb->cin1.seq_id;
5056 } else {
5057 tag = ccb->atio.tag_id;
5058 str = "XPT_ACCEPT_TARGET_IO";
5059 }
5060 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
5061 dump_tstates(isp, XS_CHANNEL(ccb));
5062 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
5063 break;
5064 }
5065 ccb->ccb_h.spriv_field0 = 0;
5066 ccb->ccb_h.spriv_ptr1 = isp;
5067
5068 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
5069 if (ccb->atio.tag_id) {
5070 atio_private_data_t *atp = isp_find_atpd(isp, tptr, ccb->atio.tag_id);
5071 if (atp) {
5072 isp_put_atpd(isp, tptr, atp);
5073 }
5074 }
5075 tptr->atio_count++;
5076 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
5077 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
5078 ccb->atio.tag_id, tptr->atio_count);
5079 ccb->atio.tag_id = 0;
5080 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
5081 if (ccb->cin1.tag_id) {
5082 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
5083 if (ntp) {
5084 isp_put_ntpd(isp, tptr, ntp);
5085 }
5086 }
5087 tptr->inot_count++;
5088 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
5089 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
5090 ccb->cin1.seq_id, tptr->inot_count);
5091 ccb->cin1.seq_id = 0;
5092 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
5093 tptr->inot_count++;
5094 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
5095 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
5096 ccb->cin1.seq_id, tptr->inot_count);
5097 ccb->cin1.seq_id = 0;
5098 }
5099 rls_lun_statep(isp, tptr);
5100 ccb->ccb_h.status = CAM_REQ_INPROG;
5101 break;
5102 }
5103 case XPT_NOTIFY_ACK:
5104 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5105 break;
5106 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
5107 {
5108 tstate_t *tptr;
5109 inot_private_data_t *ntp;
5110
5111 /*
5112 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
5113 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
5114 */
5115 /*
5116 * All the relevant path information is in the associated immediate notify
5117 */
5118 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] NOTIFY ACKNOWLEDGE for 0x%x seen\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
5119 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
5120 if (ntp == NULL) {
5121 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] XPT_NOTIFY_ACKNOWLEDGE of 0x%x cannot find ntp private data\n", __func__,
5122 ccb->cna2.tag_id, ccb->cna2.seq_id);
5123 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
5124 xpt_done(ccb);
5125 break;
5126 }
5127 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
5128 rls_lun_statep(isp, tptr);
5129 cam_freeze_devq(ccb->ccb_h.path);
5130 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
5131 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
5132 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
5133 break;
5134 }
5135 isp_put_ntpd(isp, tptr, ntp);
5136 rls_lun_statep(isp, tptr);
5137 ccb->ccb_h.status = CAM_REQ_CMP;
5138 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] calling xpt_done for tag 0x%x\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
5139 xpt_done(ccb);
5140 break;
5141 }
5142 case XPT_CONT_TARGET_IO:
5143 isp_target_start_ctio(isp, ccb, FROM_CAM);
5144 break;
5145#endif
5146 case XPT_RESET_DEV: /* BDR the specified SCSI device */
5147 {
5148 struct isp_fc *fc;
5149
5150 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
5151 tgt = ccb->ccb_h.target_id;
5152 tgt |= (bus << 16);
5153 if (IS_FC(isp))
5154 fc = ISP_FC_PC(isp, bus);
5155 else
5156 fc = NULL;
5157
5158 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
5159 if (error) {
5160 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5161 } else {
5162 /*
5163 * If we have a FC device, reset the Command
5164 * Reference Number, because the target will expect
5165 * that we re-start the CRN at 1 after a reset.
5166 */
5167 if (fc != NULL)
5168 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5169
5170 ccb->ccb_h.status = CAM_REQ_CMP;
5171 }
5172 xpt_done(ccb);
5173 break;
5174 }
5175 case XPT_ABORT: /* Abort the specified CCB */
5176 {
5177 union ccb *accb = ccb->cab.abort_ccb;
5178 switch (accb->ccb_h.func_code) {
5179#ifdef ISP_TARGET_MODE
5180 case XPT_ACCEPT_TARGET_IO:
5181 isp_target_mark_aborted(isp, ccb);
5182 break;
5183#endif
5184 case XPT_SCSI_IO:
5185 error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
5186 if (error) {
5187 ccb->ccb_h.status = CAM_UA_ABORT;
5188 } else {
5189 ccb->ccb_h.status = CAM_REQ_CMP;
5190 }
5191 break;
5192 default:
5193 ccb->ccb_h.status = CAM_REQ_INVALID;
5194 break;
5195 }
5196 /*
5197 * This is not a queued CCB, so the caller expects it to be
5198 * complete when control is returned.
5199 */
5200 break;
5201 }
5202#define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
5203 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
5204 cts = &ccb->cts;
5205 if (!IS_CURRENT_SETTINGS(cts)) {
5206 ccb->ccb_h.status = CAM_REQ_INVALID;
5207 xpt_done(ccb);
5208 break;
5209 }
5210 tgt = cts->ccb_h.target_id;
5211 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
5212 if (IS_SCSI(isp)) {
5213 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
5214 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
5215 sdparam *sdp = SDPARAM(isp, bus);
5216 uint16_t *dptr;
5217
5218 if (spi->valid == 0 && scsi->valid == 0) {
5219 ccb->ccb_h.status = CAM_REQ_CMP;
5220 xpt_done(ccb);
5221 break;
5222 }
5223
5224 /*
5225 * We always update (internally) from goal_flags
5226 * so any request to change settings just gets
5227 * vectored to that location.
5228 */
5229 dptr = &sdp->isp_devparam[tgt].goal_flags;
5230
5231 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
5232 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
5233 *dptr |= DPARM_DISC;
5234 else
5235 *dptr &= ~DPARM_DISC;
5236 }
5237
5238 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
5239 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
5240 *dptr |= DPARM_TQING;
5241 else
5242 *dptr &= ~DPARM_TQING;
5243 }
5244
5245 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
5246 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
5247 *dptr |= DPARM_WIDE;
5248 else
5249 *dptr &= ~DPARM_WIDE;
5250 }
5251
5252 /*
5253 * XXX: FIX ME
5254 */
5255 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
5256 *dptr |= DPARM_SYNC;
5257 /*
5258 * XXX: CHECK FOR LEGALITY
5259 */
5260 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
5261 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
5262 } else {
5263 *dptr &= ~DPARM_SYNC;
5264 }
5265 isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%d) to flags %x off %x per %x", bus, tgt, cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
5266 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
5267 sdp->isp_devparam[tgt].dev_update = 1;
5268 sdp->update = 1;
5269 }
5270 ccb->ccb_h.status = CAM_REQ_CMP;
5271 xpt_done(ccb);
5272 break;
5273 case XPT_GET_TRAN_SETTINGS:
5274 cts = &ccb->cts;
5275 tgt = cts->ccb_h.target_id;
5276 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
5277 if (IS_FC(isp)) {
5278 fcparam *fcp = FCPARAM(isp, bus);
5279 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
5280 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
5281
5282 cts->protocol = PROTO_SCSI;
5283 cts->protocol_version = SCSI_REV_2;
5284 cts->transport = XPORT_FC;
5285 cts->transport_version = 0;
5286
5287 scsi->valid = CTS_SCSI_VALID_TQ;
5288 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
5289 fc->valid = CTS_FC_VALID_SPEED;
5290 fc->bitrate = 100000;
5291 fc->bitrate *= fcp->isp_gbspeed;
5292 if (tgt < MAX_FC_TARG) {
5293 fcportdb_t *lp = &fcp->portdb[tgt];
5294 fc->wwnn = lp->node_wwn;
5295 fc->wwpn = lp->port_wwn;
5296 fc->port = lp->portid;
5297 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
5298 }
5299 } else {
5300 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
5301 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
5302 sdparam *sdp = SDPARAM(isp, bus);
5303 uint16_t dval, pval, oval;
5304
5305 if (IS_CURRENT_SETTINGS(cts)) {
5306 sdp->isp_devparam[tgt].dev_refresh = 1;
5307 sdp->update = 1;
5308 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
5309 dval = sdp->isp_devparam[tgt].actv_flags;
5310 oval = sdp->isp_devparam[tgt].actv_offset;
5311 pval = sdp->isp_devparam[tgt].actv_period;
5312 } else {
5313 dval = sdp->isp_devparam[tgt].nvrm_flags;
5314 oval = sdp->isp_devparam[tgt].nvrm_offset;
5315 pval = sdp->isp_devparam[tgt].nvrm_period;
5316 }
5317
5318 cts->protocol = PROTO_SCSI;
5319 cts->protocol_version = SCSI_REV_2;
5320 cts->transport = XPORT_SPI;
5321 cts->transport_version = 2;
5322
5323 spi->valid = 0;
5324 scsi->valid = 0;
5325 spi->flags = 0;
5326 scsi->flags = 0;
5327 if (dval & DPARM_DISC) {
5328 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5329 }
5330 if ((dval & DPARM_SYNC) && oval && pval) {
5331 spi->sync_offset = oval;
5332 spi->sync_period = pval;
5333 } else {
5334 spi->sync_offset = 0;
5335 spi->sync_period = 0;
5336 }
5337 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5338 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5339 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
5340 if (dval & DPARM_WIDE) {
5341 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5342 } else {
5343 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5344 }
5345 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
5346 scsi->valid = CTS_SCSI_VALID_TQ;
5347 if (dval & DPARM_TQING) {
5348 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5349 }
5350 spi->valid |= CTS_SPI_VALID_DISC;
5351 }
5352 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
5353 bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
5354 }
5355 ccb->ccb_h.status = CAM_REQ_CMP;
5356 xpt_done(ccb);
5357 break;
5358
5359 case XPT_CALC_GEOMETRY:
5360 cam_calc_geometry(&ccb->ccg, 1);
5361 xpt_done(ccb);
5362 break;
5363
5364 case XPT_RESET_BUS: /* Reset the specified bus */
5365 bus = cam_sim_bus(sim);
5366 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
5367 if (error) {
5368 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5369 xpt_done(ccb);
5370 break;
5371 }
5372 if (bootverbose) {
5373 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
5374 }
5375 if (IS_FC(isp)) {
5376 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
5377 } else {
5378 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
5379 }
5380 ccb->ccb_h.status = CAM_REQ_CMP;
5381 xpt_done(ccb);
5382 break;
5383
5384 case XPT_TERM_IO: /* Terminate the I/O process */
5385 ccb->ccb_h.status = CAM_REQ_INVALID;
5386 xpt_done(ccb);
5387 break;
5388
5389 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
5390 {
5391 struct ccb_sim_knob *kp = &ccb->knob;
5392 fcparam *fcp;
5393
5394 if (!IS_FC(isp)) {
5395 ccb->ccb_h.status = CAM_REQ_INVALID;
5396 xpt_done(ccb);
5397 break;
5398 }
5399
5400 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
5401 fcp = FCPARAM(isp, bus);
5402
5403 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
5404 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
5405 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
5406 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
5407 }
5408 ccb->ccb_h.status = CAM_REQ_CMP;
5409 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
5410 int rchange = 0;
5411 int newrole = 0;
5412
5413 switch (kp->xport_specific.fc.role) {
5414 case KNOB_ROLE_NONE:
5415 if (fcp->role != ISP_ROLE_NONE) {
5416 rchange = 1;
5417 newrole = ISP_ROLE_NONE;
5418 }
5419 break;
5420 case KNOB_ROLE_TARGET:
5421 if (fcp->role != ISP_ROLE_TARGET) {
5422 rchange = 1;
5423 newrole = ISP_ROLE_TARGET;
5424 }
5425 break;
5426 case KNOB_ROLE_INITIATOR:
5427 if (fcp->role != ISP_ROLE_INITIATOR) {
5428 rchange = 1;
5429 newrole = ISP_ROLE_INITIATOR;
5430 }
5431 break;
5432 case KNOB_ROLE_BOTH:
5433 if (fcp->role != ISP_ROLE_BOTH) {
5434 rchange = 1;
5435 newrole = ISP_ROLE_BOTH;
5436 }
5437 break;
5438 }
5439 if (rchange) {
5440 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
5441#ifdef ISP_TARGET_MODE
5442 ISP_SET_PC(isp, bus, tm_enabled, 0);
5443 ISP_SET_PC(isp, bus, tm_luns_enabled, 0);
5444#endif
5445 if (isp_control(isp, ISPCTL_CHANGE_ROLE,
5446 bus, newrole) != 0) {
5447 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5448 xpt_done(ccb);
5449 break;
5450 }
5451#ifdef ISP_TARGET_MODE
5452 if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
5453 /*
5454 * Give the new role a chance to complain and settle
5455 */
5456 msleep(isp, &isp->isp_lock, PRIBIO, "taking a breather", 2);
5457 ccb->ccb_h.status = isp_enable_deferred_luns(isp, bus);
5458 }
5459#endif
5460 }
5461 }
5462 xpt_done(ccb);
5463 break;
5464 }
5465 case XPT_GET_SIM_KNOB: /* Get SIM knobs */
5466 {
5467 struct ccb_sim_knob *kp = &ccb->knob;
5468
5469 if (IS_FC(isp)) {
5470 fcparam *fcp;
5471
5472 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
5473 fcp = FCPARAM(isp, bus);
5474
5475 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
5476 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
5477 switch (fcp->role) {
5478 case ISP_ROLE_NONE:
5479 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
5480 break;
5481 case ISP_ROLE_TARGET:
5482 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
5483 break;
5484 case ISP_ROLE_INITIATOR:
5485 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
5486 break;
5487 case ISP_ROLE_BOTH:
5488 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
5489 break;
5490 }
5491 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
5492 ccb->ccb_h.status = CAM_REQ_CMP;
5493 } else {
5494 ccb->ccb_h.status = CAM_REQ_INVALID;
5495 }
5496 xpt_done(ccb);
5497 break;
5498 }
5499 case XPT_PATH_INQ: /* Path routing inquiry */
5500 {
5501 struct ccb_pathinq *cpi = &ccb->cpi;
5502
5503 cpi->version_num = 1;
5504#ifdef ISP_TARGET_MODE
5505 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
5506#else
5507 cpi->target_sprt = 0;
5508#endif
5509 cpi->hba_eng_cnt = 0;
5510 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
5511 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
5512 cpi->bus_id = cam_sim_bus(sim);
5513 if (isp->isp_osinfo.sixtyfourbit)
5514 cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
5515 else
5516 cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
5517
5518 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
5519 if (IS_FC(isp)) {
5520 fcparam *fcp = FCPARAM(isp, bus);
5521
5522 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
5523
5524 /*
5525 * Because our loop ID can shift from time to time,
5526 * make our initiator ID out of range of our bus.
5527 */
5528 cpi->initiator_id = cpi->max_target + 1;
5529
5530 /*
5531 * Set base transfer capabilities for Fibre Channel, for this HBA.
5532 */
5533 if (IS_25XX(isp)) {
5534 cpi->base_transfer_speed = 8000000;
5535 } else if (IS_24XX(isp)) {
5536 cpi->base_transfer_speed = 4000000;
5537 } else if (IS_23XX(isp)) {
5538 cpi->base_transfer_speed = 2000000;
5539 } else {
5540 cpi->base_transfer_speed = 1000000;
5541 }
5542 cpi->hba_inquiry = PI_TAG_ABLE;
5543 cpi->transport = XPORT_FC;
5544 cpi->transport_version = 0;
5545 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
5546 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
5547 cpi->xport_specific.fc.port = fcp->isp_portid;
5548 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
5549 } else {
5550 sdparam *sdp = SDPARAM(isp, bus);
5551 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
5552 cpi->hba_misc = PIM_UNMAPPED;
5553 cpi->initiator_id = sdp->isp_initiator_id;
5554 cpi->base_transfer_speed = 3300;
5555 cpi->transport = XPORT_SPI;
5556 cpi->transport_version = 2;
5557 }
5558 cpi->protocol = PROTO_SCSI;
5559 cpi->protocol_version = SCSI_REV_2;
5560 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5561 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
5562 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
5563 cpi->unit_number = cam_sim_unit(sim);
5564 cpi->ccb_h.status = CAM_REQ_CMP;
5565 xpt_done(ccb);
5566 break;
5567 }
5568 default:
5569 ccb->ccb_h.status = CAM_REQ_INVALID;
5570 xpt_done(ccb);
5571 break;
5572 }
5573}
5574
5575#define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
5576
5577void
5578isp_done(XS_T *sccb)
5579{
5580 ispsoftc_t *isp = XS_ISP(sccb);
5581 uint32_t status;
5582
5583 if (XS_NOERR(sccb))
5584 XS_SETERR(sccb, CAM_REQ_CMP);
5585
5586 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
5587 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
5588 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
5589 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
5590 } else {
5591 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
5592 }
5593 }
5594
5595 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5596 status = sccb->ccb_h.status & CAM_STATUS_MASK;
5597 if (status != CAM_REQ_CMP) {
5598 if (status != CAM_SEL_TIMEOUT)
5599 isp_prt(isp, ISP_LOGDEBUG0, "target %d lun %d CAM status 0x%x SCSI status 0x%x", XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status, sccb->scsi_status);
5600 else if ((IS_FC(isp))
5601 && (XS_TGT(sccb) < MAX_FC_TARG)) {
5602 fcparam *fcp;
5603
5604 fcp = FCPARAM(isp, XS_CHANNEL(sccb));
5605 fcp->portdb[XS_TGT(sccb)].is_target = 0;
5606 }
5607 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
5608 sccb->ccb_h.status |= CAM_DEV_QFRZN;
5609 xpt_freeze_devq(sccb->ccb_h.path, 1);
5610 }
5611 }
5612
5613 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5614 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
5615 }
5616
5617 if (ISP_PCMD(sccb)) {
5618 if (callout_active(&PISP_PCMD(sccb)->wdog))
5619 callout_stop(&PISP_PCMD(sccb)->wdog);
5620 isp_free_pcmd(isp, (union ccb *) sccb);
5621 }
5622 xpt_done((union ccb *) sccb);
5623}
5624
5625void
5626isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
5627{
5628 int bus;
5629 static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
5630 char buf[64];
5631 char *msg = NULL;
5632 target_id_t tgt;
5633 fcportdb_t *lp;
5634 struct isp_fc *fc;
5635 struct cam_path *tmppath;
5636 struct ac_contract ac;
5637 struct ac_device_changed *adc;
5638 va_list ap;
5639
5640 switch (cmd) {
5641 case ISPASYNC_NEW_TGT_PARAMS:
5642 {
5643 struct ccb_trans_settings_scsi *scsi;
5644 struct ccb_trans_settings_spi *spi;
5645 int flags, tgt;
5646 sdparam *sdp;
5647 struct ccb_trans_settings cts;
5648
5649 memset(&cts, 0, sizeof (struct ccb_trans_settings));
5650
5651 va_start(ap, cmd);
5652 bus = va_arg(ap, int);
5653 tgt = va_arg(ap, int);
5654 va_end(ap);
5655 sdp = SDPARAM(isp, bus);
5656
5657 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
5658 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
5659 break;
5660 }
5661 flags = sdp->isp_devparam[tgt].actv_flags;
5662 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5663 cts.protocol = PROTO_SCSI;
5664 cts.transport = XPORT_SPI;
5665
5666 scsi = &cts.proto_specific.scsi;
5667 spi = &cts.xport_specific.spi;
5668
5669 if (flags & DPARM_TQING) {
5670 scsi->valid |= CTS_SCSI_VALID_TQ;
5671 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5672 }
5673
5674 if (flags & DPARM_DISC) {
5675 spi->valid |= CTS_SPI_VALID_DISC;
5676 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5677 }
5678 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5679 if (flags & DPARM_WIDE) {
5680 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5681 } else {
5682 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5683 }
5684 if (flags & DPARM_SYNC) {
5685 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5686 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5687 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5688 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5689 }
5690 isp_prt(isp, ISP_LOGDEBUG2, "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x", bus, tgt, sdp->isp_devparam[tgt].actv_period, sdp->isp_devparam[tgt].actv_offset, flags);
5691 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5692 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5693 xpt_free_path(tmppath);
5694 break;
5695 }
5696 case ISPASYNC_BUS_RESET:
5697 {
5698 va_start(ap, cmd);
5699 bus = va_arg(ap, int);
5700 va_end(ap);
5701 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5702 if (IS_FC(isp)) {
5703 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5704 } else {
5705 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5706 }
5707 break;
5708 }
5709 case ISPASYNC_LIP:
5710 if (msg == NULL) {
5711 msg = "LIP Received";
5712 }
5713 /* FALLTHROUGH */
5714 case ISPASYNC_LOOP_RESET:
5715 if (msg == NULL) {
5716 msg = "LOOP Reset";
5717 }
5718 /* FALLTHROUGH */
5719 case ISPASYNC_LOOP_DOWN:
5720 {
5721 if (msg == NULL) {
5722 msg = "LOOP Down";
5723 }
5724 va_start(ap, cmd);
5725 bus = va_arg(ap, int);
5726 va_end(ap);
5727
5728 FCPARAM(isp, bus)->isp_linkstate = 0;
5729
5730 fc = ISP_FC_PC(isp, bus);
5731 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5732 /*
5733 * We don't do any simq freezing if we are only in target mode
5734 */
5735 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5736 if (fc->path) {
5737 isp_freeze_loopdown(isp, bus, msg);
5738 }
5739 }
5740 if (!callout_active(&fc->ldt)) {
5741 callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5742 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
5743 }
5744 }
5745 isp_fcp_reset_crn(fc, /*tgt*/0, /*tgt_set*/ 0);
5746
5747 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5748 break;
5749 }
5750 case ISPASYNC_LOOP_UP:
5751 va_start(ap, cmd);
5752 bus = va_arg(ap, int);
5753 va_end(ap);
5754 fc = ISP_FC_PC(isp, bus);
5755 /*
5756 * Now we just note that Loop has come up. We don't
5757 * actually do anything because we're waiting for a
5758 * Change Notify before activating the FC cleanup
5759 * thread to look at the state of the loop again.
5760 */
5761 FCPARAM(isp, bus)->isp_linkstate = 1;
5762 fc->loop_dead = 0;
5763 fc->loop_down_time = 0;
5764 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5765 break;
5766 case ISPASYNC_DEV_ARRIVED:
5767 va_start(ap, cmd);
5768 bus = va_arg(ap, int);
5769 lp = va_arg(ap, fcportdb_t *);
5770 va_end(ap);
5771 fc = ISP_FC_PC(isp, bus);
5772 tgt = FC_PORTDB_TGT(isp, bus, lp);
5773 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5774 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
5775 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
5776 (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
5777 lp->is_target = 1;
5778 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5779 isp_make_here(isp, lp, bus, tgt);
5780 }
5781 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
5782 (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
5783 lp->is_initiator = 1;
5784 ac.contract_number = AC_CONTRACT_DEV_CHG;
5785 adc = (struct ac_device_changed *) ac.contract_data;
5786 adc->wwpn = lp->port_wwn;
5787 adc->port = lp->portid;
5788 adc->target = tgt;
5789 adc->arrived = 1;
5790 xpt_async(AC_CONTRACT, fc->path, &ac);
5791 }
5792 break;
5793 case ISPASYNC_DEV_CHANGED:
5794 va_start(ap, cmd);
5795 bus = va_arg(ap, int);
5796 lp = va_arg(ap, fcportdb_t *);
5797 va_end(ap);
5798 fc = ISP_FC_PC(isp, bus);
5799 tgt = FC_PORTDB_TGT(isp, bus, lp);
5800 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
5801 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
5802changed:
5803 if (lp->is_target !=
5804 ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
5805 (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
5806 lp->is_target = !lp->is_target;
5807 if (lp->is_target) {
5808 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5809 isp_make_here(isp, lp, bus, tgt);
5810 } else {
5811 isp_make_gone(isp, lp, bus, tgt);
5812 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5813 }
5814 }
5815 if (lp->is_initiator !=
5816 ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
5817 (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
5818 lp->is_initiator = !lp->is_initiator;
5819 ac.contract_number = AC_CONTRACT_DEV_CHG;
5820 adc = (struct ac_device_changed *) ac.contract_data;
5821 adc->wwpn = lp->port_wwn;
5822 adc->port = lp->portid;
5823 adc->target = tgt;
5824 adc->arrived = lp->is_initiator;
5825 xpt_async(AC_CONTRACT, fc->path, &ac);
5826 }
5827 break;
5828 case ISPASYNC_DEV_STAYED:
5829 va_start(ap, cmd);
5830 bus = va_arg(ap, int);
5831 lp = va_arg(ap, fcportdb_t *);
5832 va_end(ap);
5833 fc = ISP_FC_PC(isp, bus);
5834 tgt = FC_PORTDB_TGT(isp, bus, lp);
5835 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5836 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
5837 goto changed;
5838 case ISPASYNC_DEV_GONE:
5839 va_start(ap, cmd);
5840 bus = va_arg(ap, int);
5841 lp = va_arg(ap, fcportdb_t *);
5842 va_end(ap);
5843 fc = ISP_FC_PC(isp, bus);
5844 tgt = FC_PORTDB_TGT(isp, bus, lp);
5845 /*
5846 * If this has a virtual target or initiator set the isp_gdt
5847 * timer running on it to delay its departure.
5848 */
5849 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5850 if (lp->is_target || lp->is_initiator) {
5851 lp->state = FC_PORTDB_STATE_ZOMBIE;
5852 lp->gone_timer = fc->gone_device_time;
5853 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
5854 if (fc->ready && !callout_active(&fc->gdt)) {
5855 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
5856 callout_reset(&fc->gdt, hz, isp_gdt, fc);
5857 }
5858 break;
5859 }
5860 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
5861 break;
5862 case ISPASYNC_CHANGE_NOTIFY:
5863 {
5864 char *msg;
5865 int evt, nphdl, nlstate, reason;
5866
5867 va_start(ap, cmd);
5868 bus = va_arg(ap, int);
5869 evt = va_arg(ap, int);
5870 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5871 nphdl = va_arg(ap, int);
5872 nlstate = va_arg(ap, int);
5873 reason = va_arg(ap, int);
5874 } else {
5875 nphdl = NIL_HANDLE;
5876 nlstate = reason = 0;
5877 }
5878 va_end(ap);
5879 fc = ISP_FC_PC(isp, bus);
5880
5881 if (evt == ISPASYNC_CHANGE_PDB) {
5882 msg = "Chan %d Port Database Changed";
5883 } else if (evt == ISPASYNC_CHANGE_SNS) {
5884 msg = "Chan %d Name Server Database Changed";
5885 } else {
5886 msg = "Chan %d Other Change Notify";
5887 }
5888
5889 /*
5890 * If the loop down timer is running, cancel it.
5891 */
5892 if (fc->ready && callout_active(&fc->ldt)) {
5893 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5894 callout_stop(&fc->ldt);
5895 }
5896 isp_prt(isp, ISP_LOGINFO, msg, bus);
5897 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5898 isp_freeze_loopdown(isp, bus, msg);
5899 }
5900 wakeup(fc);
5901 break;
5902 }
5903#ifdef ISP_TARGET_MODE
5904 case ISPASYNC_TARGET_NOTIFY:
5905 {
5906 isp_notify_t *notify;
5907 va_start(ap, cmd);
5908 notify = va_arg(ap, isp_notify_t *);
5909 va_end(ap);
5910 switch (notify->nt_ncode) {
5911 case NT_ABORT_TASK:
5912 case NT_ABORT_TASK_SET:
5913 case NT_CLEAR_ACA:
5914 case NT_CLEAR_TASK_SET:
5915 case NT_LUN_RESET:
5916 case NT_TARGET_RESET:
5917 case NT_QUERY_TASK_SET:
5918 case NT_QUERY_ASYNC_EVENT:
5919 /*
5920 * These are task management functions.
5921 */
5922 isp_handle_platform_target_tmf(isp, notify);
5923 break;
5924 case NT_BUS_RESET:
5925 case NT_LIP_RESET:
5926 case NT_LINK_UP:
5927 case NT_LINK_DOWN:
5928 case NT_HBA_RESET:
5929 /*
5930 * No action need be taken here.
5931 */
5932 break;
5933 case NT_GLOBAL_LOGOUT:
5934 case NT_LOGOUT:
5935 /*
5936 * This is device arrival/departure notification
5937 */
5938 isp_handle_platform_target_notify_ack(isp, notify);
5939 break;
5940 default:
5941 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5942 isp_handle_platform_target_notify_ack(isp, notify);
5943 break;
5944 }
5945 break;
5946 }
5947 case ISPASYNC_TARGET_NOTIFY_ACK:
5948 {
5949 void *inot;
5950 va_start(ap, cmd);
5951 inot = va_arg(ap, void *);
5952 va_end(ap);
5953 if (isp_notify_ack(isp, inot)) {
5954 isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
5955 if (tp) {
5956 tp->isp = isp;
5957 if (inot) {
5958 memcpy(tp->data, inot, sizeof (tp->data));
5959 tp->not = tp->data;
5960 } else {
5961 tp->not = NULL;
5962 }
5963 callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
5964 callout_reset(&tp->timer, 5,
5965 isp_refire_notify_ack, tp);
5966 } else {
5967 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
5968 }
5969 }
5970 break;
5971 }
5972 case ISPASYNC_TARGET_ACTION:
5973 {
5974 isphdr_t *hp;
5975
5976 va_start(ap, cmd);
5977 hp = va_arg(ap, isphdr_t *);
5978 va_end(ap);
5979 switch (hp->rqs_entry_type) {
5980 default:
5981 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5982 break;
5983 case RQSTYPE_NOTIFY:
5984 if (IS_SCSI(isp)) {
5985 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5986 } else if (IS_24XX(isp)) {
5987 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5988 } else {
5989 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5990 }
5991 break;
5992 case RQSTYPE_ATIO:
5993 if (IS_24XX(isp)) {
5994 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5995 } else {
5996 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5997 }
5998 break;
5999 case RQSTYPE_ATIO2:
6000 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
6001 break;
6002 case RQSTYPE_CTIO7:
6003 case RQSTYPE_CTIO3:
6004 case RQSTYPE_CTIO2:
6005 case RQSTYPE_CTIO:
6006 isp_handle_platform_ctio(isp, hp);
6007 break;
6008 case RQSTYPE_ABTS_RCVD:
6009 {
6010 abts_t *abts = (abts_t *)hp;
6011 isp_notify_t notify, *nt = &notify;
6012 tstate_t *tptr;
6013 fcportdb_t *lp;
6014 uint16_t chan;
6015 uint32_t sid, did;
6016
6017 did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
6018 sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
6019 ISP_MEMZERO(nt, sizeof (isp_notify_t));
6020
6021 nt->nt_hba = isp;
6022 nt->nt_did = did;
6023 nt->nt_nphdl = abts->abts_nphdl;
6024 nt->nt_sid = sid;
6025 isp_find_chan_by_did(isp, did, &chan);
6026 if (chan == ISP_NOCHAN) {
6027 nt->nt_tgt = TGT_ANY;
6028 } else {
6029 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
6030 if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
6031 nt->nt_wwn = lp->port_wwn;
6032 } else {
6033 nt->nt_wwn = INI_ANY;
6034 }
6035 }
6036 /*
6037 * Try hard to find the lun for this command.
6038 */
6039 tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
6040 if (tptr) {
6041 nt->nt_lun = tptr->ts_lun;
6042 rls_lun_statep(isp, tptr);
6043 } else {
6044 nt->nt_lun = LUN_ANY;
6045 }
6046 nt->nt_need_ack = 1;
6047 nt->nt_tagval = abts->abts_rxid_task;
6048 nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
6049 if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
6050 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
6051 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
6052 } else {
6053 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
6054 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
6055 }
6056 nt->nt_channel = chan;
6057 nt->nt_ncode = NT_ABORT_TASK;
6058 nt->nt_lreserved = hp;
6059 isp_handle_platform_target_tmf(isp, nt);
6060 break;
6061 }
6062 case RQSTYPE_ENABLE_LUN:
6063 case RQSTYPE_MODIFY_LUN:
6064 isp_ledone(isp, (lun_entry_t *) hp);
6065 break;
6066 }
6067 break;
6068 }
6069#endif
6070 case ISPASYNC_FW_CRASH:
6071 {
6072 uint16_t mbox1, mbox6;
6073 mbox1 = ISP_READ(isp, OUTMAILBOX1);
6074 if (IS_DUALBUS(isp)) {
6075 mbox6 = ISP_READ(isp, OUTMAILBOX6);
6076 } else {
6077 mbox6 = 0;
6078 }
6079 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
6080 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
6081 isp->isp_osinfo.mbox_sleep_ok = 0;
6082 isp_reinit(isp, 1);
6083 isp->isp_osinfo.mbox_sleep_ok = mbox1;
6084 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
6085 break;
6086 }
6087 default:
6088 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
6089 break;
6090 }
6091}
6092
6093
6094/*
6095 * Locks are held before coming here.
6096 */
6097void
6098isp_uninit(ispsoftc_t *isp)
6099{
6100 if (IS_24XX(isp)) {
6101 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
6102 } else {
6103 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
6104 }
6105 ISP_DISABLE_INTS(isp);
6106}
6107
6108/*
6109 * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
6110 * up from our platform default (defww{p|n}n) and morph them based upon
6111 * channel.
6112 *
6113 * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
6114 * based upon channel.
6115 */
6116
6117uint64_t
6118isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
6119{
6120 uint64_t seed;
6121 struct isp_fc *fc = ISP_FC_PC(isp, chan);
6122
6123 /*
6124 * If we're asking for a active WWN, the default overrides get
6125 * returned, otherwise the NVRAM value is picked.
6126 *
6127 * If we're asking for a default WWN, we just pick the default override.
6128 */
6129 if (isactive) {
6130 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
6131 if (seed) {
6132 return (seed);
6133 }
6134 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
6135 if (seed) {
6136 return (seed);
6137 }
6138 return (0x400000007F000009ull);
6139 }
6140
6141 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
6142
6143 /*
6144 * For channel zero just return what we have. For either ACTIVE or
6145 * DEFAULT cases, we depend on default override of NVRAM values for
6146 * channel zero.
6147 */
6148 if (chan == 0) {
6149 return (seed);
6150 }
6151
6152 /*
6153 * For other channels, we are doing one of three things:
6154 *
6155 * 1. If what we have now is non-zero, return it. Otherwise we morph
6156 * values from channel 0. 2. If we're here for a WWPN we synthesize
6157 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
6158 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
6159 */
6160
6161 if (seed) {
6162 return (seed);
6163 }
6164 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
6165 if (seed == 0)
6166 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
6167
6168 if (((seed >> 60) & 0xf) == 2) {
6169 /*
6170 * The type 2 NAA fields for QLogic cards appear be laid out
6171 * thusly:
6172 *
6173 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
6174 * port (1) or node (0) WWN distinguishor bit 48
6175 * physical port on dual-port chips (23XX/24XX)
6176 *
6177 * This is somewhat nutty, particularly since bit 48 is
6178 * irrelevant as they assign separate serial numbers to
6179 * different physical ports anyway.
6180 *
6181 * We'll stick our channel number plus one first into bits
6182 * 57..59 and thence into bits 52..55 which allows for 8 bits
6183 * of channel which is comfortably more than our maximum
6184 * (126) now.
6185 */
6186 seed &= ~0x0FF0000000000000ULL;
6187 if (iswwnn == 0) {
6188 seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
6189 seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
6190 }
6191 } else {
6192 seed = 0;
6193 }
6194 return (seed);
6195}
6196
6197void
6198isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
6199{
6200 int loc;
6201 char lbuf[200];
6202 va_list ap;
6203
6204 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
6205 return;
6206 }
6207 snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
6208 loc = strlen(lbuf);
6209 va_start(ap, fmt);
6210 vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
6211 va_end(ap);
6212 printf("%s\n", lbuf);
6213}
6214
6215void
6216isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
6217{
6218 va_list ap;
6219 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
6220 return;
6221 }
6222 xpt_print_path(xs->ccb_h.path);
6223 va_start(ap, fmt);
6224 vprintf(fmt, ap);
6225 va_end(ap);
6226 printf("\n");
6227}
6228
6229uint64_t
6230isp_nanotime_sub(struct timespec *b, struct timespec *a)
6231{
6232 uint64_t elapsed;
6233 struct timespec x = *b;
6234 timespecsub(&x, a);
6235 elapsed = GET_NANOSEC(&x);
6236 if (elapsed == 0)
6237 elapsed++;
6238 return (elapsed);
6239}
6240
6241int
6242isp_mbox_acquire(ispsoftc_t *isp)
6243{
6244 if (isp->isp_osinfo.mboxbsy) {
6245 return (1);
6246 } else {
6247 isp->isp_osinfo.mboxcmd_done = 0;
6248 isp->isp_osinfo.mboxbsy = 1;
6249 return (0);
6250 }
6251}
6252
6253void
6254isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
6255{
6256 unsigned int usecs = mbp->timeout;
6257 unsigned int max, olim, ilim;
6258
6259 if (usecs == 0) {
6260 usecs = MBCMD_DEFAULT_TIMEOUT;
6261 }
6262 max = isp->isp_mbxwrk0 + 1;
6263
6264 if (isp->isp_osinfo.mbox_sleep_ok) {
6265 unsigned int ms = (usecs + 999) / 1000;
6266
6267 isp->isp_osinfo.mbox_sleep_ok = 0;
6268 isp->isp_osinfo.mbox_sleeping = 1;
6269 for (olim = 0; olim < max; olim++) {
6270 msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
6271 if (isp->isp_osinfo.mboxcmd_done) {
6272 break;
6273 }
6274 }
6275 isp->isp_osinfo.mbox_sleep_ok = 1;
6276 isp->isp_osinfo.mbox_sleeping = 0;
6277 } else {
6278 for (olim = 0; olim < max; olim++) {
6279 for (ilim = 0; ilim < usecs; ilim += 100) {
4548 ohandle = handle;
4549 handle = isp_find_handle(isp, xs);
4550 }
4551 if (handle != ISP_HANDLE_FREE) {
4552 /*
4553 * Try and make sure the command is really dead before
4554 * we release the handle (and DMA resources) for reuse.
4555 *
4556 * If we are successful in aborting the command then
4557 * we're done here because we'll get the command returned
4558 * back separately.
4559 */
4560 if (isp_control(isp, ISPCTL_ABORT_CMD, xs) == 0) {
4561 return;
4562 }
4563
4564 /*
4565 * Note that after calling the above, the command may in
4566 * fact have been completed.
4567 */
4568 xs = isp_find_xs(isp, handle);
4569
4570 /*
4571 * If the command no longer exists, then we won't
4572 * be able to find the xs again with this handle.
4573 */
4574 if (xs == NULL) {
4575 return;
4576 }
4577
4578 /*
4579 * After this point, the command is really dead.
4580 */
4581 if (XS_XFRLEN(xs)) {
4582 ISP_DMAFREE(isp, xs, handle);
4583 }
4584 isp_destroy_handle(isp, handle);
4585 isp_prt(isp, ISP_LOGERR, "%s: timeout for handle 0x%x", __func__, handle);
4586 xs->ccb_h.status &= ~CAM_STATUS_MASK;
4587 xs->ccb_h.status |= CAM_CMD_TIMEOUT;
4588 isp_prt_endcmd(isp, xs);
4589 isp_done(xs);
4590 } else {
4591 if (ohandle != ISP_HANDLE_FREE) {
4592 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle 0x%x, recovered during interrupt", __func__, ohandle);
4593 } else {
4594 isp_prt(isp, ISP_LOGWARN, "%s: timeout for handle already free", __func__);
4595 }
4596 }
4597}
4598
4599static void
4600isp_make_here(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
4601{
4602 union ccb *ccb;
4603 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4604
4605 if (isp_autoconfig == 0) {
4606 return;
4607 }
4608
4609 /*
4610 * Allocate a CCB, create a wildcard path for this target and schedule a rescan.
4611 */
4612 ccb = xpt_alloc_ccb_nowait();
4613 if (ccb == NULL) {
4614 isp_prt(isp, ISP_LOGWARN, "Chan %d unable to alloc CCB for rescan", chan);
4615 return;
4616 }
4617 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(fc->sim),
4618 tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
4619 isp_prt(isp, ISP_LOGWARN, "unable to create path for rescan");
4620 xpt_free_ccb(ccb);
4621 return;
4622 }
4623 xpt_rescan(ccb);
4624}
4625
4626static void
4627isp_make_gone(ispsoftc_t *isp, fcportdb_t *fcp, int chan, int tgt)
4628{
4629 struct cam_path *tp;
4630 struct isp_fc *fc = ISP_FC_PC(isp, chan);
4631
4632 if (isp_autoconfig == 0) {
4633 return;
4634 }
4635 if (xpt_create_path(&tp, NULL, cam_sim_path(fc->sim), tgt, CAM_LUN_WILDCARD) == CAM_REQ_CMP) {
4636 xpt_async(AC_LOST_DEVICE, tp, NULL);
4637 xpt_free_path(tp);
4638 }
4639}
4640
4641/*
4642 * Gone Device Timer Function- when we have decided that a device has gone
4643 * away, we wait a specific period of time prior to telling the OS it has
4644 * gone away.
4645 *
4646 * This timer function fires once a second and then scans the port database
4647 * for devices that are marked dead but still have a virtual target assigned.
4648 * We decrement a counter for that port database entry, and when it hits zero,
4649 * we tell the OS the device has gone away.
4650 */
4651static void
4652isp_gdt(void *arg)
4653{
4654 struct isp_fc *fc = arg;
4655 taskqueue_enqueue(taskqueue_thread, &fc->gtask);
4656}
4657
4658static void
4659isp_gdt_task(void *arg, int pending)
4660{
4661 struct isp_fc *fc = arg;
4662 ispsoftc_t *isp = fc->isp;
4663 int chan = fc - isp->isp_osinfo.pc.fc;
4664 fcportdb_t *lp;
4665 struct ac_contract ac;
4666 struct ac_device_changed *adc;
4667 int dbidx, more_to_do = 0;
4668
4669 ISP_LOCK(isp);
4670 isp_prt(isp, ISP_LOGDEBUG0, "Chan %d GDT timer expired", chan);
4671 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4672 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4673
4674 if (lp->state != FC_PORTDB_STATE_ZOMBIE) {
4675 continue;
4676 }
4677 if (lp->gone_timer != 0) {
4678 lp->gone_timer -= 1;
4679 more_to_do++;
4680 continue;
4681 }
4682 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Gone Device Timeout");
4683 if (lp->is_target) {
4684 lp->is_target = 0;
4685 isp_make_gone(isp, lp, chan, dbidx);
4686 }
4687 if (lp->is_initiator) {
4688 lp->is_initiator = 0;
4689 ac.contract_number = AC_CONTRACT_DEV_CHG;
4690 adc = (struct ac_device_changed *) ac.contract_data;
4691 adc->wwpn = lp->port_wwn;
4692 adc->port = lp->portid;
4693 adc->target = dbidx;
4694 adc->arrived = 0;
4695 xpt_async(AC_CONTRACT, fc->path, &ac);
4696 }
4697 lp->state = FC_PORTDB_STATE_NIL;
4698 }
4699 if (fc->ready) {
4700 if (more_to_do) {
4701 callout_reset(&fc->gdt, hz, isp_gdt, fc);
4702 } else {
4703 callout_deactivate(&fc->gdt);
4704 isp_prt(isp, ISP_LOG_SANCFG, "Chan %d Stopping Gone Device Timer @ %lu", chan, (unsigned long) time_uptime);
4705 }
4706 }
4707 ISP_UNLOCK(isp);
4708}
4709
4710/*
4711 * Loop Down Timer Function- when loop goes down, a timer is started and
4712 * and after it expires we come here and take all probational devices that
4713 * the OS knows about and the tell the OS that they've gone away.
4714 *
4715 * We don't clear the devices out of our port database because, when loop
4716 * come back up, we have to do some actual cleanup with the chip at that
4717 * point (implicit PLOGO, e.g., to get the chip's port database state right).
4718 */
4719static void
4720isp_ldt(void *arg)
4721{
4722 struct isp_fc *fc = arg;
4723 taskqueue_enqueue(taskqueue_thread, &fc->ltask);
4724}
4725
4726static void
4727isp_ldt_task(void *arg, int pending)
4728{
4729 struct isp_fc *fc = arg;
4730 ispsoftc_t *isp = fc->isp;
4731 int chan = fc - isp->isp_osinfo.pc.fc;
4732 fcportdb_t *lp;
4733 struct ac_contract ac;
4734 struct ac_device_changed *adc;
4735 int dbidx, i;
4736
4737 ISP_LOCK(isp);
4738 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Loop Down Timer expired @ %lu", chan, (unsigned long) time_uptime);
4739 callout_deactivate(&fc->ldt);
4740
4741 /*
4742 * Notify to the OS all targets who we now consider have departed.
4743 */
4744 for (dbidx = 0; dbidx < MAX_FC_TARG; dbidx++) {
4745 lp = &FCPARAM(isp, chan)->portdb[dbidx];
4746
4747 if (lp->state == FC_PORTDB_STATE_NIL)
4748 continue;
4749
4750 /*
4751 * XXX: CLEAN UP AND COMPLETE ANY PENDING COMMANDS FIRST!
4752 */
4753 for (i = 0; i < isp->isp_maxcmds; i++) {
4754 struct ccb_scsiio *xs;
4755
4756 if (!ISP_VALID_HANDLE(isp, isp->isp_xflist[i].handle)) {
4757 continue;
4758 }
4759 if ((xs = isp->isp_xflist[i].cmd) == NULL) {
4760 continue;
4761 }
4762 if (dbidx != XS_TGT(xs)) {
4763 continue;
4764 }
4765 isp_prt(isp, ISP_LOGWARN, "command handle 0x%x for %d.%d.%d orphaned by loop down timeout",
4766 isp->isp_xflist[i].handle, chan, XS_TGT(xs), XS_LUN(xs));
4767 }
4768
4769 isp_prt(isp, ISP_LOGCONFIG, prom3, chan, dbidx, lp->portid, "Loop Down Timeout");
4770 if (lp->is_target) {
4771 lp->is_target = 0;
4772 isp_make_gone(isp, lp, chan, dbidx);
4773 }
4774 if (lp->is_initiator) {
4775 lp->is_initiator = 0;
4776 ac.contract_number = AC_CONTRACT_DEV_CHG;
4777 adc = (struct ac_device_changed *) ac.contract_data;
4778 adc->wwpn = lp->port_wwn;
4779 adc->port = lp->portid;
4780 adc->target = dbidx;
4781 adc->arrived = 0;
4782 xpt_async(AC_CONTRACT, fc->path, &ac);
4783 }
4784 }
4785
4786 isp_unfreeze_loopdown(isp, chan);
4787 /*
4788 * The loop down timer has expired. Wake up the kthread
4789 * to notice that fact (or make it false).
4790 */
4791 fc->loop_dead = 1;
4792 fc->loop_down_time = fc->loop_down_limit+1;
4793 wakeup(fc);
4794 ISP_UNLOCK(isp);
4795}
4796
4797static void
4798isp_kthread(void *arg)
4799{
4800 struct isp_fc *fc = arg;
4801 ispsoftc_t *isp = fc->isp;
4802 int chan = fc - isp->isp_osinfo.pc.fc;
4803 int slp = 0;
4804
4805 mtx_lock(&isp->isp_osinfo.lock);
4806
4807 while (isp->isp_osinfo.is_exiting == 0) {
4808 int lb, lim;
4809
4810 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d checking FC state", __func__, chan);
4811 lb = isp_fc_runstate(isp, chan, 250000);
4812
4813 /*
4814 * Our action is different based upon whether we're supporting
4815 * Initiator mode or not. If we are, we might freeze the simq
4816 * when loop is down and set all sorts of different delays to
4817 * check again.
4818 *
4819 * If not, we simply just wait for loop to come up.
4820 */
4821 if (lb && (FCPARAM(isp, chan)->role & ISP_ROLE_INITIATOR)) {
4822 /*
4823 * Increment loop down time by the last sleep interval
4824 */
4825 fc->loop_down_time += slp;
4826
4827 if (lb < 0) {
4828 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC loop not up (down count %d)", __func__, chan, fc->loop_down_time);
4829 } else {
4830 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC got to %d (down count %d)", __func__, chan, lb, fc->loop_down_time);
4831 }
4832
4833 /*
4834 * If we've never seen loop up and we've waited longer
4835 * than quickboot time, or we've seen loop up but we've
4836 * waited longer than loop_down_limit, give up and go
4837 * to sleep until loop comes up.
4838 */
4839 if (FCPARAM(isp, chan)->loop_seen_once == 0) {
4840 lim = isp_quickboot_time;
4841 } else {
4842 lim = fc->loop_down_limit;
4843 }
4844 if (fc->loop_down_time >= lim) {
4845 isp_freeze_loopdown(isp, chan, "loop limit hit");
4846 slp = 0;
4847 } else if (fc->loop_down_time < 10) {
4848 slp = 1;
4849 } else if (fc->loop_down_time < 30) {
4850 slp = 5;
4851 } else if (fc->loop_down_time < 60) {
4852 slp = 10;
4853 } else if (fc->loop_down_time < 120) {
4854 slp = 20;
4855 } else {
4856 slp = 30;
4857 }
4858
4859 } else if (lb) {
4860 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC Loop Down", __func__, chan);
4861 fc->loop_down_time += slp;
4862 if (fc->loop_down_time > 300)
4863 slp = 0;
4864 else
4865 slp = 60;
4866 } else {
4867 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d FC state OK", __func__, chan);
4868 fc->loop_down_time = 0;
4869 slp = 0;
4870 }
4871
4872
4873 /*
4874 * If this is past the first loop up or the loop is dead and if we'd frozen the simq, unfreeze it
4875 * now so that CAM can start sending us commands.
4876 *
4877 * If the FC state isn't okay yet, they'll hit that in isp_start which will freeze the queue again
4878 * or kill the commands, as appropriate.
4879 */
4880
4881 if (FCPARAM(isp, chan)->loop_seen_once || fc->loop_dead) {
4882 isp_unfreeze_loopdown(isp, chan);
4883 }
4884
4885 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep time %d", __func__, chan, slp);
4886
4887 msleep(fc, &isp->isp_osinfo.lock, PRIBIO, "ispf", slp * hz);
4888
4889 /*
4890 * If slp is zero, we're waking up for the first time after
4891 * things have been okay. In this case, we set a deferral state
4892 * for all commands and delay hysteresis seconds before starting
4893 * the FC state evaluation. This gives the loop/fabric a chance
4894 * to settle.
4895 */
4896 if (slp == 0 && fc->hysteresis) {
4897 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "%s: Chan %d sleep hysteresis ticks %d", __func__, chan, fc->hysteresis * hz);
4898 mtx_unlock(&isp->isp_osinfo.lock);
4899 pause("ispt", fc->hysteresis * hz);
4900 mtx_lock(&isp->isp_osinfo.lock);
4901 }
4902 }
4903 fc->num_threads -= 1;
4904 mtx_unlock(&isp->isp_osinfo.lock);
4905 kthread_exit();
4906}
4907
4908static void
4909isp_action(struct cam_sim *sim, union ccb *ccb)
4910{
4911 int bus, tgt, ts, error, lim;
4912 ispsoftc_t *isp;
4913 struct ccb_trans_settings *cts;
4914
4915 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("isp_action\n"));
4916
4917 isp = (ispsoftc_t *)cam_sim_softc(sim);
4918 mtx_assert(&isp->isp_lock, MA_OWNED);
4919 isp_prt(isp, ISP_LOGDEBUG2, "isp_action code %x", ccb->ccb_h.func_code);
4920 ISP_PCMD(ccb) = NULL;
4921
4922 if (isp->isp_state != ISP_RUNSTATE && ccb->ccb_h.func_code == XPT_SCSI_IO) {
4923 isp_init(isp);
4924 if (isp->isp_state != ISP_INITSTATE) {
4925 /*
4926 * Lie. Say it was a selection timeout.
4927 */
4928 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
4929 isp_done((struct ccb_scsiio *) ccb);
4930 return;
4931 }
4932 isp->isp_state = ISP_RUNSTATE;
4933 }
4934
4935 switch (ccb->ccb_h.func_code) {
4936 case XPT_SCSI_IO: /* Execute the requested I/O operation */
4937 bus = XS_CHANNEL(ccb);
4938 /*
4939 * Do a couple of preliminary checks...
4940 */
4941 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
4942 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
4943 ccb->ccb_h.status = CAM_REQ_INVALID;
4944 isp_done((struct ccb_scsiio *) ccb);
4945 break;
4946 }
4947 }
4948 ccb->csio.req_map = NULL;
4949#ifdef DIAGNOSTIC
4950 if (ccb->ccb_h.target_id > (ISP_MAX_TARGETS(isp) - 1)) {
4951 xpt_print(ccb->ccb_h.path, "invalid target\n");
4952 ccb->ccb_h.status = CAM_PATH_INVALID;
4953 } else if (ccb->ccb_h.target_lun > (ISP_MAX_LUNS(isp) - 1)) {
4954 xpt_print(ccb->ccb_h.path, "invalid lun\n");
4955 ccb->ccb_h.status = CAM_PATH_INVALID;
4956 }
4957 if (ccb->ccb_h.status == CAM_PATH_INVALID) {
4958 xpt_done(ccb);
4959 break;
4960 }
4961#endif
4962 ccb->csio.scsi_status = SCSI_STATUS_OK;
4963 if (isp_get_pcmd(isp, ccb)) {
4964 isp_prt(isp, ISP_LOGWARN, "out of PCMDs");
4965 cam_freeze_devq(ccb->ccb_h.path);
4966 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 250, 0);
4967 ccb->ccb_h.status = CAM_REQUEUE_REQ;
4968 xpt_done(ccb);
4969 break;
4970 }
4971 error = isp_start((XS_T *) ccb);
4972 switch (error) {
4973 case CMD_QUEUED:
4974 ccb->ccb_h.status |= CAM_SIM_QUEUED;
4975 if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
4976 break;
4977 }
4978 ts = ccb->ccb_h.timeout;
4979 if (ts == CAM_TIME_DEFAULT) {
4980 ts = 60*1000;
4981 }
4982 ts = isp_mstohz(ts);
4983 callout_reset(&PISP_PCMD(ccb)->wdog, ts, isp_watchdog, ccb);
4984 break;
4985 case CMD_RQLATER:
4986 /*
4987 * We get this result for FC devices if the loop state isn't ready yet
4988 * or if the device in question has gone zombie on us.
4989 *
4990 * If we've never seen Loop UP at all, we requeue this request and wait
4991 * for the initial loop up delay to expire.
4992 */
4993 lim = ISP_FC_PC(isp, bus)->loop_down_limit;
4994 if (FCPARAM(isp, bus)->loop_seen_once == 0 || ISP_FC_PC(isp, bus)->loop_down_time >= lim) {
4995 if (FCPARAM(isp, bus)->loop_seen_once == 0) {
4996 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d loop not seen yet @ %lu", XS_TGT(ccb), XS_LUN(ccb), (unsigned long) time_uptime);
4997 } else {
4998 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d downtime (%d) > lim (%d)", XS_TGT(ccb), XS_LUN(ccb), ISP_FC_PC(isp, bus)->loop_down_time, lim);
4999 }
5000 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
5001 isp_done((struct ccb_scsiio *) ccb);
5002 break;
5003 }
5004 isp_prt(isp, ISP_LOGDEBUG0, "%d.%d retry later", XS_TGT(ccb), XS_LUN(ccb));
5005 cam_freeze_devq(ccb->ccb_h.path);
5006 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
5007 ccb->ccb_h.status = CAM_REQUEUE_REQ;
5008 isp_free_pcmd(isp, ccb);
5009 xpt_done(ccb);
5010 break;
5011 case CMD_EAGAIN:
5012 isp_free_pcmd(isp, ccb);
5013 cam_freeze_devq(ccb->ccb_h.path);
5014 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
5015 ccb->ccb_h.status = CAM_REQUEUE_REQ;
5016 xpt_done(ccb);
5017 break;
5018 case CMD_COMPLETE:
5019 isp_done((struct ccb_scsiio *) ccb);
5020 break;
5021 default:
5022 isp_prt(isp, ISP_LOGERR, "What's this? 0x%x at %d in file %s", error, __LINE__, __FILE__);
5023 ccb->ccb_h.status = CAM_REQUEUE_REQ;
5024 isp_free_pcmd(isp, ccb);
5025 xpt_done(ccb);
5026 }
5027 break;
5028
5029#ifdef ISP_TARGET_MODE
5030 case XPT_EN_LUN: /* Enable/Disable LUN as a target */
5031 if (ccb->cel.enable) {
5032 isp_enable_lun(isp, ccb);
5033 } else {
5034 isp_disable_lun(isp, ccb);
5035 }
5036 break;
5037 case XPT_IMMED_NOTIFY:
5038 case XPT_IMMEDIATE_NOTIFY: /* Add Immediate Notify Resource */
5039 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
5040 {
5041 tstate_t *tptr = get_lun_statep(isp, XS_CHANNEL(ccb), ccb->ccb_h.target_lun);
5042 if (tptr == NULL) {
5043 tptr = get_lun_statep(isp, XS_CHANNEL(ccb), CAM_LUN_WILDCARD);
5044 }
5045 if (tptr == NULL) {
5046 const char *str;
5047 uint32_t tag;
5048
5049 if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
5050 str = "XPT_IMMEDIATE_NOTIFY";
5051 tag = ccb->cin1.seq_id;
5052 } else {
5053 tag = ccb->atio.tag_id;
5054 str = "XPT_ACCEPT_TARGET_IO";
5055 }
5056 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] no state pointer found for %s\n", __func__, tag, str);
5057 dump_tstates(isp, XS_CHANNEL(ccb));
5058 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
5059 break;
5060 }
5061 ccb->ccb_h.spriv_field0 = 0;
5062 ccb->ccb_h.spriv_ptr1 = isp;
5063
5064 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
5065 if (ccb->atio.tag_id) {
5066 atio_private_data_t *atp = isp_find_atpd(isp, tptr, ccb->atio.tag_id);
5067 if (atp) {
5068 isp_put_atpd(isp, tptr, atp);
5069 }
5070 }
5071 tptr->atio_count++;
5072 SLIST_INSERT_HEAD(&tptr->atios, &ccb->ccb_h, sim_links.sle);
5073 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE ATIO (tag id 0x%x), count now %d\n",
5074 ccb->atio.tag_id, tptr->atio_count);
5075 ccb->atio.tag_id = 0;
5076 } else if (ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY) {
5077 if (ccb->cin1.tag_id) {
5078 inot_private_data_t *ntp = isp_find_ntpd(isp, tptr, ccb->cin1.tag_id, ccb->cin1.seq_id);
5079 if (ntp) {
5080 isp_put_ntpd(isp, tptr, ntp);
5081 }
5082 }
5083 tptr->inot_count++;
5084 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
5085 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
5086 ccb->cin1.seq_id, tptr->inot_count);
5087 ccb->cin1.seq_id = 0;
5088 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
5089 tptr->inot_count++;
5090 SLIST_INSERT_HEAD(&tptr->inots, &ccb->ccb_h, sim_links.sle);
5091 ISP_PATH_PRT(isp, ISP_LOGTDEBUG2, ccb->ccb_h.path, "Put FREE INOT, (seq id 0x%x) count now %d\n",
5092 ccb->cin1.seq_id, tptr->inot_count);
5093 ccb->cin1.seq_id = 0;
5094 }
5095 rls_lun_statep(isp, tptr);
5096 ccb->ccb_h.status = CAM_REQ_INPROG;
5097 break;
5098 }
5099 case XPT_NOTIFY_ACK:
5100 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5101 break;
5102 case XPT_NOTIFY_ACKNOWLEDGE: /* notify ack */
5103 {
5104 tstate_t *tptr;
5105 inot_private_data_t *ntp;
5106
5107 /*
5108 * XXX: Because we cannot guarantee that the path information in the notify acknowledge ccb
5109 * XXX: matches that for the immediate notify, we have to *search* for the notify structure
5110 */
5111 /*
5112 * All the relevant path information is in the associated immediate notify
5113 */
5114 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] NOTIFY ACKNOWLEDGE for 0x%x seen\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
5115 ntp = get_ntp_from_tagdata(isp, ccb->cna2.tag_id, ccb->cna2.seq_id, &tptr);
5116 if (ntp == NULL) {
5117 ISP_PATH_PRT(isp, ISP_LOGWARN, ccb->ccb_h.path, "%s: [0x%x] XPT_NOTIFY_ACKNOWLEDGE of 0x%x cannot find ntp private data\n", __func__,
5118 ccb->cna2.tag_id, ccb->cna2.seq_id);
5119 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
5120 xpt_done(ccb);
5121 break;
5122 }
5123 if (isp_handle_platform_target_notify_ack(isp, &ntp->rd.nt)) {
5124 rls_lun_statep(isp, tptr);
5125 cam_freeze_devq(ccb->ccb_h.path);
5126 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 1000, 0);
5127 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
5128 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
5129 break;
5130 }
5131 isp_put_ntpd(isp, tptr, ntp);
5132 rls_lun_statep(isp, tptr);
5133 ccb->ccb_h.status = CAM_REQ_CMP;
5134 ISP_PATH_PRT(isp, ISP_LOGTDEBUG0, ccb->ccb_h.path, "%s: [0x%x] calling xpt_done for tag 0x%x\n", __func__, ccb->cna2.tag_id, ccb->cna2.seq_id);
5135 xpt_done(ccb);
5136 break;
5137 }
5138 case XPT_CONT_TARGET_IO:
5139 isp_target_start_ctio(isp, ccb, FROM_CAM);
5140 break;
5141#endif
5142 case XPT_RESET_DEV: /* BDR the specified SCSI device */
5143 {
5144 struct isp_fc *fc;
5145
5146 bus = cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
5147 tgt = ccb->ccb_h.target_id;
5148 tgt |= (bus << 16);
5149 if (IS_FC(isp))
5150 fc = ISP_FC_PC(isp, bus);
5151 else
5152 fc = NULL;
5153
5154 error = isp_control(isp, ISPCTL_RESET_DEV, bus, tgt);
5155 if (error) {
5156 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5157 } else {
5158 /*
5159 * If we have a FC device, reset the Command
5160 * Reference Number, because the target will expect
5161 * that we re-start the CRN at 1 after a reset.
5162 */
5163 if (fc != NULL)
5164 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5165
5166 ccb->ccb_h.status = CAM_REQ_CMP;
5167 }
5168 xpt_done(ccb);
5169 break;
5170 }
5171 case XPT_ABORT: /* Abort the specified CCB */
5172 {
5173 union ccb *accb = ccb->cab.abort_ccb;
5174 switch (accb->ccb_h.func_code) {
5175#ifdef ISP_TARGET_MODE
5176 case XPT_ACCEPT_TARGET_IO:
5177 isp_target_mark_aborted(isp, ccb);
5178 break;
5179#endif
5180 case XPT_SCSI_IO:
5181 error = isp_control(isp, ISPCTL_ABORT_CMD, accb);
5182 if (error) {
5183 ccb->ccb_h.status = CAM_UA_ABORT;
5184 } else {
5185 ccb->ccb_h.status = CAM_REQ_CMP;
5186 }
5187 break;
5188 default:
5189 ccb->ccb_h.status = CAM_REQ_INVALID;
5190 break;
5191 }
5192 /*
5193 * This is not a queued CCB, so the caller expects it to be
5194 * complete when control is returned.
5195 */
5196 break;
5197 }
5198#define IS_CURRENT_SETTINGS(c) (c->type == CTS_TYPE_CURRENT_SETTINGS)
5199 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
5200 cts = &ccb->cts;
5201 if (!IS_CURRENT_SETTINGS(cts)) {
5202 ccb->ccb_h.status = CAM_REQ_INVALID;
5203 xpt_done(ccb);
5204 break;
5205 }
5206 tgt = cts->ccb_h.target_id;
5207 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
5208 if (IS_SCSI(isp)) {
5209 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
5210 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
5211 sdparam *sdp = SDPARAM(isp, bus);
5212 uint16_t *dptr;
5213
5214 if (spi->valid == 0 && scsi->valid == 0) {
5215 ccb->ccb_h.status = CAM_REQ_CMP;
5216 xpt_done(ccb);
5217 break;
5218 }
5219
5220 /*
5221 * We always update (internally) from goal_flags
5222 * so any request to change settings just gets
5223 * vectored to that location.
5224 */
5225 dptr = &sdp->isp_devparam[tgt].goal_flags;
5226
5227 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
5228 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
5229 *dptr |= DPARM_DISC;
5230 else
5231 *dptr &= ~DPARM_DISC;
5232 }
5233
5234 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
5235 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
5236 *dptr |= DPARM_TQING;
5237 else
5238 *dptr &= ~DPARM_TQING;
5239 }
5240
5241 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
5242 if (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT)
5243 *dptr |= DPARM_WIDE;
5244 else
5245 *dptr &= ~DPARM_WIDE;
5246 }
5247
5248 /*
5249 * XXX: FIX ME
5250 */
5251 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) && (spi->valid & CTS_SPI_VALID_SYNC_RATE) && (spi->sync_period && spi->sync_offset)) {
5252 *dptr |= DPARM_SYNC;
5253 /*
5254 * XXX: CHECK FOR LEGALITY
5255 */
5256 sdp->isp_devparam[tgt].goal_period = spi->sync_period;
5257 sdp->isp_devparam[tgt].goal_offset = spi->sync_offset;
5258 } else {
5259 *dptr &= ~DPARM_SYNC;
5260 }
5261 isp_prt(isp, ISP_LOGDEBUG0, "SET (%d.%d.%d) to flags %x off %x per %x", bus, tgt, cts->ccb_h.target_lun, sdp->isp_devparam[tgt].goal_flags,
5262 sdp->isp_devparam[tgt].goal_offset, sdp->isp_devparam[tgt].goal_period);
5263 sdp->isp_devparam[tgt].dev_update = 1;
5264 sdp->update = 1;
5265 }
5266 ccb->ccb_h.status = CAM_REQ_CMP;
5267 xpt_done(ccb);
5268 break;
5269 case XPT_GET_TRAN_SETTINGS:
5270 cts = &ccb->cts;
5271 tgt = cts->ccb_h.target_id;
5272 bus = cam_sim_bus(xpt_path_sim(cts->ccb_h.path));
5273 if (IS_FC(isp)) {
5274 fcparam *fcp = FCPARAM(isp, bus);
5275 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
5276 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
5277
5278 cts->protocol = PROTO_SCSI;
5279 cts->protocol_version = SCSI_REV_2;
5280 cts->transport = XPORT_FC;
5281 cts->transport_version = 0;
5282
5283 scsi->valid = CTS_SCSI_VALID_TQ;
5284 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
5285 fc->valid = CTS_FC_VALID_SPEED;
5286 fc->bitrate = 100000;
5287 fc->bitrate *= fcp->isp_gbspeed;
5288 if (tgt < MAX_FC_TARG) {
5289 fcportdb_t *lp = &fcp->portdb[tgt];
5290 fc->wwnn = lp->node_wwn;
5291 fc->wwpn = lp->port_wwn;
5292 fc->port = lp->portid;
5293 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN | CTS_FC_VALID_PORT;
5294 }
5295 } else {
5296 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
5297 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
5298 sdparam *sdp = SDPARAM(isp, bus);
5299 uint16_t dval, pval, oval;
5300
5301 if (IS_CURRENT_SETTINGS(cts)) {
5302 sdp->isp_devparam[tgt].dev_refresh = 1;
5303 sdp->update = 1;
5304 (void) isp_control(isp, ISPCTL_UPDATE_PARAMS, bus);
5305 dval = sdp->isp_devparam[tgt].actv_flags;
5306 oval = sdp->isp_devparam[tgt].actv_offset;
5307 pval = sdp->isp_devparam[tgt].actv_period;
5308 } else {
5309 dval = sdp->isp_devparam[tgt].nvrm_flags;
5310 oval = sdp->isp_devparam[tgt].nvrm_offset;
5311 pval = sdp->isp_devparam[tgt].nvrm_period;
5312 }
5313
5314 cts->protocol = PROTO_SCSI;
5315 cts->protocol_version = SCSI_REV_2;
5316 cts->transport = XPORT_SPI;
5317 cts->transport_version = 2;
5318
5319 spi->valid = 0;
5320 scsi->valid = 0;
5321 spi->flags = 0;
5322 scsi->flags = 0;
5323 if (dval & DPARM_DISC) {
5324 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5325 }
5326 if ((dval & DPARM_SYNC) && oval && pval) {
5327 spi->sync_offset = oval;
5328 spi->sync_period = pval;
5329 } else {
5330 spi->sync_offset = 0;
5331 spi->sync_period = 0;
5332 }
5333 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5334 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5335 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
5336 if (dval & DPARM_WIDE) {
5337 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5338 } else {
5339 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5340 }
5341 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
5342 scsi->valid = CTS_SCSI_VALID_TQ;
5343 if (dval & DPARM_TQING) {
5344 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5345 }
5346 spi->valid |= CTS_SPI_VALID_DISC;
5347 }
5348 isp_prt(isp, ISP_LOGDEBUG0, "GET %s (%d.%d.%d) to flags %x off %x per %x", IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM",
5349 bus, tgt, cts->ccb_h.target_lun, dval, oval, pval);
5350 }
5351 ccb->ccb_h.status = CAM_REQ_CMP;
5352 xpt_done(ccb);
5353 break;
5354
5355 case XPT_CALC_GEOMETRY:
5356 cam_calc_geometry(&ccb->ccg, 1);
5357 xpt_done(ccb);
5358 break;
5359
5360 case XPT_RESET_BUS: /* Reset the specified bus */
5361 bus = cam_sim_bus(sim);
5362 error = isp_control(isp, ISPCTL_RESET_BUS, bus);
5363 if (error) {
5364 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5365 xpt_done(ccb);
5366 break;
5367 }
5368 if (bootverbose) {
5369 xpt_print(ccb->ccb_h.path, "reset bus on channel %d\n", bus);
5370 }
5371 if (IS_FC(isp)) {
5372 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, 0);
5373 } else {
5374 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, 0);
5375 }
5376 ccb->ccb_h.status = CAM_REQ_CMP;
5377 xpt_done(ccb);
5378 break;
5379
5380 case XPT_TERM_IO: /* Terminate the I/O process */
5381 ccb->ccb_h.status = CAM_REQ_INVALID;
5382 xpt_done(ccb);
5383 break;
5384
5385 case XPT_SET_SIM_KNOB: /* Set SIM knobs */
5386 {
5387 struct ccb_sim_knob *kp = &ccb->knob;
5388 fcparam *fcp;
5389
5390 if (!IS_FC(isp)) {
5391 ccb->ccb_h.status = CAM_REQ_INVALID;
5392 xpt_done(ccb);
5393 break;
5394 }
5395
5396 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
5397 fcp = FCPARAM(isp, bus);
5398
5399 if (kp->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
5400 fcp->isp_wwnn = ISP_FC_PC(isp, bus)->def_wwnn = kp->xport_specific.fc.wwnn;
5401 fcp->isp_wwpn = ISP_FC_PC(isp, bus)->def_wwpn = kp->xport_specific.fc.wwpn;
5402 isp_prt(isp, ISP_LOGALL, "Setting Channel %d wwns to 0x%jx 0x%jx", bus, fcp->isp_wwnn, fcp->isp_wwpn);
5403 }
5404 ccb->ccb_h.status = CAM_REQ_CMP;
5405 if (kp->xport_specific.fc.valid & KNOB_VALID_ROLE) {
5406 int rchange = 0;
5407 int newrole = 0;
5408
5409 switch (kp->xport_specific.fc.role) {
5410 case KNOB_ROLE_NONE:
5411 if (fcp->role != ISP_ROLE_NONE) {
5412 rchange = 1;
5413 newrole = ISP_ROLE_NONE;
5414 }
5415 break;
5416 case KNOB_ROLE_TARGET:
5417 if (fcp->role != ISP_ROLE_TARGET) {
5418 rchange = 1;
5419 newrole = ISP_ROLE_TARGET;
5420 }
5421 break;
5422 case KNOB_ROLE_INITIATOR:
5423 if (fcp->role != ISP_ROLE_INITIATOR) {
5424 rchange = 1;
5425 newrole = ISP_ROLE_INITIATOR;
5426 }
5427 break;
5428 case KNOB_ROLE_BOTH:
5429 if (fcp->role != ISP_ROLE_BOTH) {
5430 rchange = 1;
5431 newrole = ISP_ROLE_BOTH;
5432 }
5433 break;
5434 }
5435 if (rchange) {
5436 ISP_PATH_PRT(isp, ISP_LOGCONFIG, ccb->ccb_h.path, "changing role on from %d to %d\n", fcp->role, newrole);
5437#ifdef ISP_TARGET_MODE
5438 ISP_SET_PC(isp, bus, tm_enabled, 0);
5439 ISP_SET_PC(isp, bus, tm_luns_enabled, 0);
5440#endif
5441 if (isp_control(isp, ISPCTL_CHANGE_ROLE,
5442 bus, newrole) != 0) {
5443 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
5444 xpt_done(ccb);
5445 break;
5446 }
5447#ifdef ISP_TARGET_MODE
5448 if (newrole == ISP_ROLE_TARGET || newrole == ISP_ROLE_BOTH) {
5449 /*
5450 * Give the new role a chance to complain and settle
5451 */
5452 msleep(isp, &isp->isp_lock, PRIBIO, "taking a breather", 2);
5453 ccb->ccb_h.status = isp_enable_deferred_luns(isp, bus);
5454 }
5455#endif
5456 }
5457 }
5458 xpt_done(ccb);
5459 break;
5460 }
5461 case XPT_GET_SIM_KNOB: /* Get SIM knobs */
5462 {
5463 struct ccb_sim_knob *kp = &ccb->knob;
5464
5465 if (IS_FC(isp)) {
5466 fcparam *fcp;
5467
5468 bus = cam_sim_bus(xpt_path_sim(kp->ccb_h.path));
5469 fcp = FCPARAM(isp, bus);
5470
5471 kp->xport_specific.fc.wwnn = fcp->isp_wwnn;
5472 kp->xport_specific.fc.wwpn = fcp->isp_wwpn;
5473 switch (fcp->role) {
5474 case ISP_ROLE_NONE:
5475 kp->xport_specific.fc.role = KNOB_ROLE_NONE;
5476 break;
5477 case ISP_ROLE_TARGET:
5478 kp->xport_specific.fc.role = KNOB_ROLE_TARGET;
5479 break;
5480 case ISP_ROLE_INITIATOR:
5481 kp->xport_specific.fc.role = KNOB_ROLE_INITIATOR;
5482 break;
5483 case ISP_ROLE_BOTH:
5484 kp->xport_specific.fc.role = KNOB_ROLE_BOTH;
5485 break;
5486 }
5487 kp->xport_specific.fc.valid = KNOB_VALID_ADDRESS | KNOB_VALID_ROLE;
5488 ccb->ccb_h.status = CAM_REQ_CMP;
5489 } else {
5490 ccb->ccb_h.status = CAM_REQ_INVALID;
5491 }
5492 xpt_done(ccb);
5493 break;
5494 }
5495 case XPT_PATH_INQ: /* Path routing inquiry */
5496 {
5497 struct ccb_pathinq *cpi = &ccb->cpi;
5498
5499 cpi->version_num = 1;
5500#ifdef ISP_TARGET_MODE
5501 cpi->target_sprt = PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
5502#else
5503 cpi->target_sprt = 0;
5504#endif
5505 cpi->hba_eng_cnt = 0;
5506 cpi->max_target = ISP_MAX_TARGETS(isp) - 1;
5507 cpi->max_lun = ISP_MAX_LUNS(isp) - 1;
5508 cpi->bus_id = cam_sim_bus(sim);
5509 if (isp->isp_osinfo.sixtyfourbit)
5510 cpi->maxio = (ISP_NSEG64_MAX - 1) * PAGE_SIZE;
5511 else
5512 cpi->maxio = (ISP_NSEG_MAX - 1) * PAGE_SIZE;
5513
5514 bus = cam_sim_bus(xpt_path_sim(cpi->ccb_h.path));
5515 if (IS_FC(isp)) {
5516 fcparam *fcp = FCPARAM(isp, bus);
5517
5518 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
5519
5520 /*
5521 * Because our loop ID can shift from time to time,
5522 * make our initiator ID out of range of our bus.
5523 */
5524 cpi->initiator_id = cpi->max_target + 1;
5525
5526 /*
5527 * Set base transfer capabilities for Fibre Channel, for this HBA.
5528 */
5529 if (IS_25XX(isp)) {
5530 cpi->base_transfer_speed = 8000000;
5531 } else if (IS_24XX(isp)) {
5532 cpi->base_transfer_speed = 4000000;
5533 } else if (IS_23XX(isp)) {
5534 cpi->base_transfer_speed = 2000000;
5535 } else {
5536 cpi->base_transfer_speed = 1000000;
5537 }
5538 cpi->hba_inquiry = PI_TAG_ABLE;
5539 cpi->transport = XPORT_FC;
5540 cpi->transport_version = 0;
5541 cpi->xport_specific.fc.wwnn = fcp->isp_wwnn;
5542 cpi->xport_specific.fc.wwpn = fcp->isp_wwpn;
5543 cpi->xport_specific.fc.port = fcp->isp_portid;
5544 cpi->xport_specific.fc.bitrate = fcp->isp_gbspeed * 1000;
5545 } else {
5546 sdparam *sdp = SDPARAM(isp, bus);
5547 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
5548 cpi->hba_misc = PIM_UNMAPPED;
5549 cpi->initiator_id = sdp->isp_initiator_id;
5550 cpi->base_transfer_speed = 3300;
5551 cpi->transport = XPORT_SPI;
5552 cpi->transport_version = 2;
5553 }
5554 cpi->protocol = PROTO_SCSI;
5555 cpi->protocol_version = SCSI_REV_2;
5556 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5557 strncpy(cpi->hba_vid, "Qlogic", HBA_IDLEN);
5558 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
5559 cpi->unit_number = cam_sim_unit(sim);
5560 cpi->ccb_h.status = CAM_REQ_CMP;
5561 xpt_done(ccb);
5562 break;
5563 }
5564 default:
5565 ccb->ccb_h.status = CAM_REQ_INVALID;
5566 xpt_done(ccb);
5567 break;
5568 }
5569}
5570
5571#define ISPDDB (CAM_DEBUG_INFO|CAM_DEBUG_TRACE|CAM_DEBUG_CDB)
5572
5573void
5574isp_done(XS_T *sccb)
5575{
5576 ispsoftc_t *isp = XS_ISP(sccb);
5577 uint32_t status;
5578
5579 if (XS_NOERR(sccb))
5580 XS_SETERR(sccb, CAM_REQ_CMP);
5581
5582 if ((sccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP && (sccb->scsi_status != SCSI_STATUS_OK)) {
5583 sccb->ccb_h.status &= ~CAM_STATUS_MASK;
5584 if ((sccb->scsi_status == SCSI_STATUS_CHECK_COND) && (sccb->ccb_h.status & CAM_AUTOSNS_VALID) == 0) {
5585 sccb->ccb_h.status |= CAM_AUTOSENSE_FAIL;
5586 } else {
5587 sccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
5588 }
5589 }
5590
5591 sccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5592 status = sccb->ccb_h.status & CAM_STATUS_MASK;
5593 if (status != CAM_REQ_CMP) {
5594 if (status != CAM_SEL_TIMEOUT)
5595 isp_prt(isp, ISP_LOGDEBUG0, "target %d lun %d CAM status 0x%x SCSI status 0x%x", XS_TGT(sccb), XS_LUN(sccb), sccb->ccb_h.status, sccb->scsi_status);
5596 else if ((IS_FC(isp))
5597 && (XS_TGT(sccb) < MAX_FC_TARG)) {
5598 fcparam *fcp;
5599
5600 fcp = FCPARAM(isp, XS_CHANNEL(sccb));
5601 fcp->portdb[XS_TGT(sccb)].is_target = 0;
5602 }
5603 if ((sccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
5604 sccb->ccb_h.status |= CAM_DEV_QFRZN;
5605 xpt_freeze_devq(sccb->ccb_h.path, 1);
5606 }
5607 }
5608
5609 if ((CAM_DEBUGGED(sccb->ccb_h.path, ISPDDB)) && (sccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5610 xpt_print(sccb->ccb_h.path, "cam completion status 0x%x\n", sccb->ccb_h.status);
5611 }
5612
5613 if (ISP_PCMD(sccb)) {
5614 if (callout_active(&PISP_PCMD(sccb)->wdog))
5615 callout_stop(&PISP_PCMD(sccb)->wdog);
5616 isp_free_pcmd(isp, (union ccb *) sccb);
5617 }
5618 xpt_done((union ccb *) sccb);
5619}
5620
5621void
5622isp_async(ispsoftc_t *isp, ispasync_t cmd, ...)
5623{
5624 int bus;
5625 static const char prom[] = "Chan %d [%d] WWPN 0x%16jx PortID 0x%06x handle 0x%x %s %s";
5626 char buf[64];
5627 char *msg = NULL;
5628 target_id_t tgt;
5629 fcportdb_t *lp;
5630 struct isp_fc *fc;
5631 struct cam_path *tmppath;
5632 struct ac_contract ac;
5633 struct ac_device_changed *adc;
5634 va_list ap;
5635
5636 switch (cmd) {
5637 case ISPASYNC_NEW_TGT_PARAMS:
5638 {
5639 struct ccb_trans_settings_scsi *scsi;
5640 struct ccb_trans_settings_spi *spi;
5641 int flags, tgt;
5642 sdparam *sdp;
5643 struct ccb_trans_settings cts;
5644
5645 memset(&cts, 0, sizeof (struct ccb_trans_settings));
5646
5647 va_start(ap, cmd);
5648 bus = va_arg(ap, int);
5649 tgt = va_arg(ap, int);
5650 va_end(ap);
5651 sdp = SDPARAM(isp, bus);
5652
5653 if (xpt_create_path(&tmppath, NULL, cam_sim_path(ISP_SPI_PC(isp, bus)->sim), tgt, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
5654 isp_prt(isp, ISP_LOGWARN, "isp_async cannot make temp path for %d.%d", tgt, bus);
5655 break;
5656 }
5657 flags = sdp->isp_devparam[tgt].actv_flags;
5658 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5659 cts.protocol = PROTO_SCSI;
5660 cts.transport = XPORT_SPI;
5661
5662 scsi = &cts.proto_specific.scsi;
5663 spi = &cts.xport_specific.spi;
5664
5665 if (flags & DPARM_TQING) {
5666 scsi->valid |= CTS_SCSI_VALID_TQ;
5667 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
5668 }
5669
5670 if (flags & DPARM_DISC) {
5671 spi->valid |= CTS_SPI_VALID_DISC;
5672 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
5673 }
5674 spi->flags |= CTS_SPI_VALID_BUS_WIDTH;
5675 if (flags & DPARM_WIDE) {
5676 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
5677 } else {
5678 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
5679 }
5680 if (flags & DPARM_SYNC) {
5681 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
5682 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
5683 spi->sync_period = sdp->isp_devparam[tgt].actv_period;
5684 spi->sync_offset = sdp->isp_devparam[tgt].actv_offset;
5685 }
5686 isp_prt(isp, ISP_LOGDEBUG2, "NEW_TGT_PARAMS bus %d tgt %d period %x offset %x flags %x", bus, tgt, sdp->isp_devparam[tgt].actv_period, sdp->isp_devparam[tgt].actv_offset, flags);
5687 xpt_setup_ccb(&cts.ccb_h, tmppath, 1);
5688 xpt_async(AC_TRANSFER_NEG, tmppath, &cts);
5689 xpt_free_path(tmppath);
5690 break;
5691 }
5692 case ISPASYNC_BUS_RESET:
5693 {
5694 va_start(ap, cmd);
5695 bus = va_arg(ap, int);
5696 va_end(ap);
5697 isp_prt(isp, ISP_LOGINFO, "SCSI bus reset on bus %d detected", bus);
5698 if (IS_FC(isp)) {
5699 xpt_async(AC_BUS_RESET, ISP_FC_PC(isp, bus)->path, NULL);
5700 } else {
5701 xpt_async(AC_BUS_RESET, ISP_SPI_PC(isp, bus)->path, NULL);
5702 }
5703 break;
5704 }
5705 case ISPASYNC_LIP:
5706 if (msg == NULL) {
5707 msg = "LIP Received";
5708 }
5709 /* FALLTHROUGH */
5710 case ISPASYNC_LOOP_RESET:
5711 if (msg == NULL) {
5712 msg = "LOOP Reset";
5713 }
5714 /* FALLTHROUGH */
5715 case ISPASYNC_LOOP_DOWN:
5716 {
5717 if (msg == NULL) {
5718 msg = "LOOP Down";
5719 }
5720 va_start(ap, cmd);
5721 bus = va_arg(ap, int);
5722 va_end(ap);
5723
5724 FCPARAM(isp, bus)->isp_linkstate = 0;
5725
5726 fc = ISP_FC_PC(isp, bus);
5727 if (cmd == ISPASYNC_LOOP_DOWN && fc->ready) {
5728 /*
5729 * We don't do any simq freezing if we are only in target mode
5730 */
5731 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5732 if (fc->path) {
5733 isp_freeze_loopdown(isp, bus, msg);
5734 }
5735 }
5736 if (!callout_active(&fc->ldt)) {
5737 callout_reset(&fc->ldt, fc->loop_down_limit * hz, isp_ldt, fc);
5738 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Starting Loop Down Timer @ %lu", (unsigned long) time_uptime);
5739 }
5740 }
5741 isp_fcp_reset_crn(fc, /*tgt*/0, /*tgt_set*/ 0);
5742
5743 isp_prt(isp, ISP_LOGINFO, "Chan %d: %s", bus, msg);
5744 break;
5745 }
5746 case ISPASYNC_LOOP_UP:
5747 va_start(ap, cmd);
5748 bus = va_arg(ap, int);
5749 va_end(ap);
5750 fc = ISP_FC_PC(isp, bus);
5751 /*
5752 * Now we just note that Loop has come up. We don't
5753 * actually do anything because we're waiting for a
5754 * Change Notify before activating the FC cleanup
5755 * thread to look at the state of the loop again.
5756 */
5757 FCPARAM(isp, bus)->isp_linkstate = 1;
5758 fc->loop_dead = 0;
5759 fc->loop_down_time = 0;
5760 isp_prt(isp, ISP_LOGINFO, "Chan %d Loop UP", bus);
5761 break;
5762 case ISPASYNC_DEV_ARRIVED:
5763 va_start(ap, cmd);
5764 bus = va_arg(ap, int);
5765 lp = va_arg(ap, fcportdb_t *);
5766 va_end(ap);
5767 fc = ISP_FC_PC(isp, bus);
5768 tgt = FC_PORTDB_TGT(isp, bus, lp);
5769 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5770 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "arrived");
5771 if ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
5772 (lp->prli_word3 & PRLI_WD3_TARGET_FUNCTION)) {
5773 lp->is_target = 1;
5774 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5775 isp_make_here(isp, lp, bus, tgt);
5776 }
5777 if ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
5778 (lp->prli_word3 & PRLI_WD3_INITIATOR_FUNCTION)) {
5779 lp->is_initiator = 1;
5780 ac.contract_number = AC_CONTRACT_DEV_CHG;
5781 adc = (struct ac_device_changed *) ac.contract_data;
5782 adc->wwpn = lp->port_wwn;
5783 adc->port = lp->portid;
5784 adc->target = tgt;
5785 adc->arrived = 1;
5786 xpt_async(AC_CONTRACT, fc->path, &ac);
5787 }
5788 break;
5789 case ISPASYNC_DEV_CHANGED:
5790 va_start(ap, cmd);
5791 bus = va_arg(ap, int);
5792 lp = va_arg(ap, fcportdb_t *);
5793 va_end(ap);
5794 fc = ISP_FC_PC(isp, bus);
5795 tgt = FC_PORTDB_TGT(isp, bus, lp);
5796 isp_gen_role_str(buf, sizeof (buf), lp->new_prli_word3);
5797 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->new_portid, lp->handle, buf, "changed");
5798changed:
5799 if (lp->is_target !=
5800 ((FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) &&
5801 (lp->new_prli_word3 & PRLI_WD3_TARGET_FUNCTION))) {
5802 lp->is_target = !lp->is_target;
5803 if (lp->is_target) {
5804 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5805 isp_make_here(isp, lp, bus, tgt);
5806 } else {
5807 isp_make_gone(isp, lp, bus, tgt);
5808 isp_fcp_reset_crn(fc, tgt, /*tgt_set*/ 1);
5809 }
5810 }
5811 if (lp->is_initiator !=
5812 ((FCPARAM(isp, bus)->role & ISP_ROLE_TARGET) &&
5813 (lp->new_prli_word3 & PRLI_WD3_INITIATOR_FUNCTION))) {
5814 lp->is_initiator = !lp->is_initiator;
5815 ac.contract_number = AC_CONTRACT_DEV_CHG;
5816 adc = (struct ac_device_changed *) ac.contract_data;
5817 adc->wwpn = lp->port_wwn;
5818 adc->port = lp->portid;
5819 adc->target = tgt;
5820 adc->arrived = lp->is_initiator;
5821 xpt_async(AC_CONTRACT, fc->path, &ac);
5822 }
5823 break;
5824 case ISPASYNC_DEV_STAYED:
5825 va_start(ap, cmd);
5826 bus = va_arg(ap, int);
5827 lp = va_arg(ap, fcportdb_t *);
5828 va_end(ap);
5829 fc = ISP_FC_PC(isp, bus);
5830 tgt = FC_PORTDB_TGT(isp, bus, lp);
5831 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5832 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "stayed");
5833 goto changed;
5834 case ISPASYNC_DEV_GONE:
5835 va_start(ap, cmd);
5836 bus = va_arg(ap, int);
5837 lp = va_arg(ap, fcportdb_t *);
5838 va_end(ap);
5839 fc = ISP_FC_PC(isp, bus);
5840 tgt = FC_PORTDB_TGT(isp, bus, lp);
5841 /*
5842 * If this has a virtual target or initiator set the isp_gdt
5843 * timer running on it to delay its departure.
5844 */
5845 isp_gen_role_str(buf, sizeof (buf), lp->prli_word3);
5846 if (lp->is_target || lp->is_initiator) {
5847 lp->state = FC_PORTDB_STATE_ZOMBIE;
5848 lp->gone_timer = fc->gone_device_time;
5849 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone zombie");
5850 if (fc->ready && !callout_active(&fc->gdt)) {
5851 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Chan %d Starting Gone Device Timer with %u seconds time now %lu", bus, lp->gone_timer, (unsigned long)time_uptime);
5852 callout_reset(&fc->gdt, hz, isp_gdt, fc);
5853 }
5854 break;
5855 }
5856 isp_prt(isp, ISP_LOGCONFIG, prom, bus, tgt, lp->port_wwn, lp->portid, lp->handle, buf, "gone");
5857 break;
5858 case ISPASYNC_CHANGE_NOTIFY:
5859 {
5860 char *msg;
5861 int evt, nphdl, nlstate, reason;
5862
5863 va_start(ap, cmd);
5864 bus = va_arg(ap, int);
5865 evt = va_arg(ap, int);
5866 if (IS_24XX(isp) && evt == ISPASYNC_CHANGE_PDB) {
5867 nphdl = va_arg(ap, int);
5868 nlstate = va_arg(ap, int);
5869 reason = va_arg(ap, int);
5870 } else {
5871 nphdl = NIL_HANDLE;
5872 nlstate = reason = 0;
5873 }
5874 va_end(ap);
5875 fc = ISP_FC_PC(isp, bus);
5876
5877 if (evt == ISPASYNC_CHANGE_PDB) {
5878 msg = "Chan %d Port Database Changed";
5879 } else if (evt == ISPASYNC_CHANGE_SNS) {
5880 msg = "Chan %d Name Server Database Changed";
5881 } else {
5882 msg = "Chan %d Other Change Notify";
5883 }
5884
5885 /*
5886 * If the loop down timer is running, cancel it.
5887 */
5888 if (fc->ready && callout_active(&fc->ldt)) {
5889 isp_prt(isp, ISP_LOG_SANCFG|ISP_LOGDEBUG0, "Stopping Loop Down Timer @ %lu", (unsigned long) time_uptime);
5890 callout_stop(&fc->ldt);
5891 }
5892 isp_prt(isp, ISP_LOGINFO, msg, bus);
5893 if (FCPARAM(isp, bus)->role & ISP_ROLE_INITIATOR) {
5894 isp_freeze_loopdown(isp, bus, msg);
5895 }
5896 wakeup(fc);
5897 break;
5898 }
5899#ifdef ISP_TARGET_MODE
5900 case ISPASYNC_TARGET_NOTIFY:
5901 {
5902 isp_notify_t *notify;
5903 va_start(ap, cmd);
5904 notify = va_arg(ap, isp_notify_t *);
5905 va_end(ap);
5906 switch (notify->nt_ncode) {
5907 case NT_ABORT_TASK:
5908 case NT_ABORT_TASK_SET:
5909 case NT_CLEAR_ACA:
5910 case NT_CLEAR_TASK_SET:
5911 case NT_LUN_RESET:
5912 case NT_TARGET_RESET:
5913 case NT_QUERY_TASK_SET:
5914 case NT_QUERY_ASYNC_EVENT:
5915 /*
5916 * These are task management functions.
5917 */
5918 isp_handle_platform_target_tmf(isp, notify);
5919 break;
5920 case NT_BUS_RESET:
5921 case NT_LIP_RESET:
5922 case NT_LINK_UP:
5923 case NT_LINK_DOWN:
5924 case NT_HBA_RESET:
5925 /*
5926 * No action need be taken here.
5927 */
5928 break;
5929 case NT_GLOBAL_LOGOUT:
5930 case NT_LOGOUT:
5931 /*
5932 * This is device arrival/departure notification
5933 */
5934 isp_handle_platform_target_notify_ack(isp, notify);
5935 break;
5936 default:
5937 isp_prt(isp, ISP_LOGALL, "target notify code 0x%x", notify->nt_ncode);
5938 isp_handle_platform_target_notify_ack(isp, notify);
5939 break;
5940 }
5941 break;
5942 }
5943 case ISPASYNC_TARGET_NOTIFY_ACK:
5944 {
5945 void *inot;
5946 va_start(ap, cmd);
5947 inot = va_arg(ap, void *);
5948 va_end(ap);
5949 if (isp_notify_ack(isp, inot)) {
5950 isp_tna_t *tp = malloc(sizeof (*tp), M_DEVBUF, M_NOWAIT);
5951 if (tp) {
5952 tp->isp = isp;
5953 if (inot) {
5954 memcpy(tp->data, inot, sizeof (tp->data));
5955 tp->not = tp->data;
5956 } else {
5957 tp->not = NULL;
5958 }
5959 callout_init_mtx(&tp->timer, &isp->isp_lock, 0);
5960 callout_reset(&tp->timer, 5,
5961 isp_refire_notify_ack, tp);
5962 } else {
5963 isp_prt(isp, ISP_LOGERR, "you lose- cannot allocate a notify refire");
5964 }
5965 }
5966 break;
5967 }
5968 case ISPASYNC_TARGET_ACTION:
5969 {
5970 isphdr_t *hp;
5971
5972 va_start(ap, cmd);
5973 hp = va_arg(ap, isphdr_t *);
5974 va_end(ap);
5975 switch (hp->rqs_entry_type) {
5976 default:
5977 isp_prt(isp, ISP_LOGWARN, "%s: unhandled target action 0x%x", __func__, hp->rqs_entry_type);
5978 break;
5979 case RQSTYPE_NOTIFY:
5980 if (IS_SCSI(isp)) {
5981 isp_handle_platform_notify_scsi(isp, (in_entry_t *) hp);
5982 } else if (IS_24XX(isp)) {
5983 isp_handle_platform_notify_24xx(isp, (in_fcentry_24xx_t *) hp);
5984 } else {
5985 isp_handle_platform_notify_fc(isp, (in_fcentry_t *) hp);
5986 }
5987 break;
5988 case RQSTYPE_ATIO:
5989 if (IS_24XX(isp)) {
5990 isp_handle_platform_atio7(isp, (at7_entry_t *) hp);
5991 } else {
5992 isp_handle_platform_atio(isp, (at_entry_t *) hp);
5993 }
5994 break;
5995 case RQSTYPE_ATIO2:
5996 isp_handle_platform_atio2(isp, (at2_entry_t *) hp);
5997 break;
5998 case RQSTYPE_CTIO7:
5999 case RQSTYPE_CTIO3:
6000 case RQSTYPE_CTIO2:
6001 case RQSTYPE_CTIO:
6002 isp_handle_platform_ctio(isp, hp);
6003 break;
6004 case RQSTYPE_ABTS_RCVD:
6005 {
6006 abts_t *abts = (abts_t *)hp;
6007 isp_notify_t notify, *nt = &notify;
6008 tstate_t *tptr;
6009 fcportdb_t *lp;
6010 uint16_t chan;
6011 uint32_t sid, did;
6012
6013 did = (abts->abts_did_hi << 16) | abts->abts_did_lo;
6014 sid = (abts->abts_sid_hi << 16) | abts->abts_sid_lo;
6015 ISP_MEMZERO(nt, sizeof (isp_notify_t));
6016
6017 nt->nt_hba = isp;
6018 nt->nt_did = did;
6019 nt->nt_nphdl = abts->abts_nphdl;
6020 nt->nt_sid = sid;
6021 isp_find_chan_by_did(isp, did, &chan);
6022 if (chan == ISP_NOCHAN) {
6023 nt->nt_tgt = TGT_ANY;
6024 } else {
6025 nt->nt_tgt = FCPARAM(isp, chan)->isp_wwpn;
6026 if (isp_find_pdb_by_handle(isp, chan, abts->abts_nphdl, &lp)) {
6027 nt->nt_wwn = lp->port_wwn;
6028 } else {
6029 nt->nt_wwn = INI_ANY;
6030 }
6031 }
6032 /*
6033 * Try hard to find the lun for this command.
6034 */
6035 tptr = get_lun_statep_from_tag(isp, chan, abts->abts_rxid_task);
6036 if (tptr) {
6037 nt->nt_lun = tptr->ts_lun;
6038 rls_lun_statep(isp, tptr);
6039 } else {
6040 nt->nt_lun = LUN_ANY;
6041 }
6042 nt->nt_need_ack = 1;
6043 nt->nt_tagval = abts->abts_rxid_task;
6044 nt->nt_tagval |= (((uint64_t) abts->abts_rxid_abts) << 32);
6045 if (abts->abts_rxid_task == ISP24XX_NO_TASK) {
6046 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x has no task id (rx_id 0x%04x ox_id 0x%04x)",
6047 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rx_id, abts->abts_ox_id);
6048 } else {
6049 isp_prt(isp, ISP_LOGTINFO, "[0x%x] ABTS from N-Port handle 0x%x Port 0x%06x for task 0x%x (rx_id 0x%04x ox_id 0x%04x)",
6050 abts->abts_rxid_abts, abts->abts_nphdl, sid, abts->abts_rxid_task, abts->abts_rx_id, abts->abts_ox_id);
6051 }
6052 nt->nt_channel = chan;
6053 nt->nt_ncode = NT_ABORT_TASK;
6054 nt->nt_lreserved = hp;
6055 isp_handle_platform_target_tmf(isp, nt);
6056 break;
6057 }
6058 case RQSTYPE_ENABLE_LUN:
6059 case RQSTYPE_MODIFY_LUN:
6060 isp_ledone(isp, (lun_entry_t *) hp);
6061 break;
6062 }
6063 break;
6064 }
6065#endif
6066 case ISPASYNC_FW_CRASH:
6067 {
6068 uint16_t mbox1, mbox6;
6069 mbox1 = ISP_READ(isp, OUTMAILBOX1);
6070 if (IS_DUALBUS(isp)) {
6071 mbox6 = ISP_READ(isp, OUTMAILBOX6);
6072 } else {
6073 mbox6 = 0;
6074 }
6075 isp_prt(isp, ISP_LOGERR, "Internal Firmware Error on bus %d @ RISC Address 0x%x", mbox6, mbox1);
6076 mbox1 = isp->isp_osinfo.mbox_sleep_ok;
6077 isp->isp_osinfo.mbox_sleep_ok = 0;
6078 isp_reinit(isp, 1);
6079 isp->isp_osinfo.mbox_sleep_ok = mbox1;
6080 isp_async(isp, ISPASYNC_FW_RESTARTED, NULL);
6081 break;
6082 }
6083 default:
6084 isp_prt(isp, ISP_LOGERR, "unknown isp_async event %d", cmd);
6085 break;
6086 }
6087}
6088
6089
6090/*
6091 * Locks are held before coming here.
6092 */
6093void
6094isp_uninit(ispsoftc_t *isp)
6095{
6096 if (IS_24XX(isp)) {
6097 ISP_WRITE(isp, BIU2400_HCCR, HCCR_2400_CMD_RESET);
6098 } else {
6099 ISP_WRITE(isp, HCCR, HCCR_CMD_RESET);
6100 }
6101 ISP_DISABLE_INTS(isp);
6102}
6103
6104/*
6105 * When we want to get the 'default' WWNs (when lacking NVRAM), we pick them
6106 * up from our platform default (defww{p|n}n) and morph them based upon
6107 * channel.
6108 *
6109 * When we want to get the 'active' WWNs, we get NVRAM WWNs and then morph them
6110 * based upon channel.
6111 */
6112
6113uint64_t
6114isp_default_wwn(ispsoftc_t * isp, int chan, int isactive, int iswwnn)
6115{
6116 uint64_t seed;
6117 struct isp_fc *fc = ISP_FC_PC(isp, chan);
6118
6119 /*
6120 * If we're asking for a active WWN, the default overrides get
6121 * returned, otherwise the NVRAM value is picked.
6122 *
6123 * If we're asking for a default WWN, we just pick the default override.
6124 */
6125 if (isactive) {
6126 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
6127 if (seed) {
6128 return (seed);
6129 }
6130 seed = iswwnn ? FCPARAM(isp, chan)->isp_wwnn_nvram : FCPARAM(isp, chan)->isp_wwpn_nvram;
6131 if (seed) {
6132 return (seed);
6133 }
6134 return (0x400000007F000009ull);
6135 }
6136
6137 seed = iswwnn ? fc->def_wwnn : fc->def_wwpn;
6138
6139 /*
6140 * For channel zero just return what we have. For either ACTIVE or
6141 * DEFAULT cases, we depend on default override of NVRAM values for
6142 * channel zero.
6143 */
6144 if (chan == 0) {
6145 return (seed);
6146 }
6147
6148 /*
6149 * For other channels, we are doing one of three things:
6150 *
6151 * 1. If what we have now is non-zero, return it. Otherwise we morph
6152 * values from channel 0. 2. If we're here for a WWPN we synthesize
6153 * it if Channel 0's wwpn has a type 2 NAA. 3. If we're here for a
6154 * WWNN we synthesize it if Channel 0's wwnn has a type 2 NAA.
6155 */
6156
6157 if (seed) {
6158 return (seed);
6159 }
6160 seed = iswwnn ? ISP_FC_PC(isp, 0)->def_wwnn : ISP_FC_PC(isp, 0)->def_wwpn;
6161 if (seed == 0)
6162 seed = iswwnn ? FCPARAM(isp, 0)->isp_wwnn_nvram : FCPARAM(isp, 0)->isp_wwpn_nvram;
6163
6164 if (((seed >> 60) & 0xf) == 2) {
6165 /*
6166 * The type 2 NAA fields for QLogic cards appear be laid out
6167 * thusly:
6168 *
6169 * bits 63..60 NAA == 2 bits 59..57 unused/zero bit 56
6170 * port (1) or node (0) WWN distinguishor bit 48
6171 * physical port on dual-port chips (23XX/24XX)
6172 *
6173 * This is somewhat nutty, particularly since bit 48 is
6174 * irrelevant as they assign separate serial numbers to
6175 * different physical ports anyway.
6176 *
6177 * We'll stick our channel number plus one first into bits
6178 * 57..59 and thence into bits 52..55 which allows for 8 bits
6179 * of channel which is comfortably more than our maximum
6180 * (126) now.
6181 */
6182 seed &= ~0x0FF0000000000000ULL;
6183 if (iswwnn == 0) {
6184 seed |= ((uint64_t) (chan + 1) & 0xf) << 56;
6185 seed |= ((uint64_t) ((chan + 1) >> 4) & 0xf) << 52;
6186 }
6187 } else {
6188 seed = 0;
6189 }
6190 return (seed);
6191}
6192
6193void
6194isp_prt(ispsoftc_t *isp, int level, const char *fmt, ...)
6195{
6196 int loc;
6197 char lbuf[200];
6198 va_list ap;
6199
6200 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
6201 return;
6202 }
6203 snprintf(lbuf, sizeof (lbuf), "%s: ", device_get_nameunit(isp->isp_dev));
6204 loc = strlen(lbuf);
6205 va_start(ap, fmt);
6206 vsnprintf(&lbuf[loc], sizeof (lbuf) - loc - 1, fmt, ap);
6207 va_end(ap);
6208 printf("%s\n", lbuf);
6209}
6210
6211void
6212isp_xs_prt(ispsoftc_t *isp, XS_T *xs, int level, const char *fmt, ...)
6213{
6214 va_list ap;
6215 if (level != ISP_LOGALL && (level & isp->isp_dblev) == 0) {
6216 return;
6217 }
6218 xpt_print_path(xs->ccb_h.path);
6219 va_start(ap, fmt);
6220 vprintf(fmt, ap);
6221 va_end(ap);
6222 printf("\n");
6223}
6224
6225uint64_t
6226isp_nanotime_sub(struct timespec *b, struct timespec *a)
6227{
6228 uint64_t elapsed;
6229 struct timespec x = *b;
6230 timespecsub(&x, a);
6231 elapsed = GET_NANOSEC(&x);
6232 if (elapsed == 0)
6233 elapsed++;
6234 return (elapsed);
6235}
6236
6237int
6238isp_mbox_acquire(ispsoftc_t *isp)
6239{
6240 if (isp->isp_osinfo.mboxbsy) {
6241 return (1);
6242 } else {
6243 isp->isp_osinfo.mboxcmd_done = 0;
6244 isp->isp_osinfo.mboxbsy = 1;
6245 return (0);
6246 }
6247}
6248
6249void
6250isp_mbox_wait_complete(ispsoftc_t *isp, mbreg_t *mbp)
6251{
6252 unsigned int usecs = mbp->timeout;
6253 unsigned int max, olim, ilim;
6254
6255 if (usecs == 0) {
6256 usecs = MBCMD_DEFAULT_TIMEOUT;
6257 }
6258 max = isp->isp_mbxwrk0 + 1;
6259
6260 if (isp->isp_osinfo.mbox_sleep_ok) {
6261 unsigned int ms = (usecs + 999) / 1000;
6262
6263 isp->isp_osinfo.mbox_sleep_ok = 0;
6264 isp->isp_osinfo.mbox_sleeping = 1;
6265 for (olim = 0; olim < max; olim++) {
6266 msleep(&isp->isp_mbxworkp, &isp->isp_osinfo.lock, PRIBIO, "ispmbx_sleep", isp_mstohz(ms));
6267 if (isp->isp_osinfo.mboxcmd_done) {
6268 break;
6269 }
6270 }
6271 isp->isp_osinfo.mbox_sleep_ok = 1;
6272 isp->isp_osinfo.mbox_sleeping = 0;
6273 } else {
6274 for (olim = 0; olim < max; olim++) {
6275 for (ilim = 0; ilim < usecs; ilim += 100) {
6280 uint32_t isr;
6281 uint16_t sema, mbox;
6276 uint16_t isr, sema, info;
6282 if (isp->isp_osinfo.mboxcmd_done) {
6283 break;
6284 }
6277 if (isp->isp_osinfo.mboxcmd_done) {
6278 break;
6279 }
6285 if (ISP_READ_ISR(isp, &isr, &sema, &mbox)) {
6286 isp_intr(isp, isr, sema, mbox);
6280 if (ISP_READ_ISR(isp, &isr, &sema, &info)) {
6281 isp_intr(isp, isr, sema, info);
6287 if (isp->isp_osinfo.mboxcmd_done) {
6288 break;
6289 }
6290 }
6291 ISP_DELAY(100);
6292 }
6293 if (isp->isp_osinfo.mboxcmd_done) {
6294 break;
6295 }
6296 }
6297 }
6298 if (isp->isp_osinfo.mboxcmd_done == 0) {
6299 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
6300 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
6301 mbp->param[0] = MBOX_TIMEOUT;
6302 isp->isp_osinfo.mboxcmd_done = 1;
6303 }
6304}
6305
6306void
6307isp_mbox_notify_done(ispsoftc_t *isp)
6308{
6309 if (isp->isp_osinfo.mbox_sleeping) {
6310 wakeup(&isp->isp_mbxworkp);
6311 }
6312 isp->isp_osinfo.mboxcmd_done = 1;
6313}
6314
6315void
6316isp_mbox_release(ispsoftc_t *isp)
6317{
6318 isp->isp_osinfo.mboxbsy = 0;
6319}
6320
6321int
6322isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
6323{
6324 int ret = 0;
6325 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
6326 ret = -1;
6327 } else {
6328 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
6329 }
6330 return (ret);
6331}
6332
6333int
6334isp_mstohz(int ms)
6335{
6336 int hz;
6337 struct timeval t;
6338 t.tv_sec = ms / 1000;
6339 t.tv_usec = (ms % 1000) * 1000;
6340 hz = tvtohz(&t);
6341 if (hz < 0) {
6342 hz = 0x7fffffff;
6343 }
6344 if (hz == 0) {
6345 hz = 1;
6346 }
6347 return (hz);
6348}
6349
6350void
6351isp_platform_intr(void *arg)
6352{
6353 ispsoftc_t *isp = arg;
6282 if (isp->isp_osinfo.mboxcmd_done) {
6283 break;
6284 }
6285 }
6286 ISP_DELAY(100);
6287 }
6288 if (isp->isp_osinfo.mboxcmd_done) {
6289 break;
6290 }
6291 }
6292 }
6293 if (isp->isp_osinfo.mboxcmd_done == 0) {
6294 isp_prt(isp, ISP_LOGWARN, "%s Mailbox Command (0x%x) Timeout (%uus) (started @ %s:%d)",
6295 isp->isp_osinfo.mbox_sleep_ok? "Interrupting" : "Polled", isp->isp_lastmbxcmd, usecs, mbp->func, mbp->lineno);
6296 mbp->param[0] = MBOX_TIMEOUT;
6297 isp->isp_osinfo.mboxcmd_done = 1;
6298 }
6299}
6300
6301void
6302isp_mbox_notify_done(ispsoftc_t *isp)
6303{
6304 if (isp->isp_osinfo.mbox_sleeping) {
6305 wakeup(&isp->isp_mbxworkp);
6306 }
6307 isp->isp_osinfo.mboxcmd_done = 1;
6308}
6309
6310void
6311isp_mbox_release(ispsoftc_t *isp)
6312{
6313 isp->isp_osinfo.mboxbsy = 0;
6314}
6315
6316int
6317isp_fc_scratch_acquire(ispsoftc_t *isp, int chan)
6318{
6319 int ret = 0;
6320 if (isp->isp_osinfo.pc.fc[chan].fcbsy) {
6321 ret = -1;
6322 } else {
6323 isp->isp_osinfo.pc.fc[chan].fcbsy = 1;
6324 }
6325 return (ret);
6326}
6327
6328int
6329isp_mstohz(int ms)
6330{
6331 int hz;
6332 struct timeval t;
6333 t.tv_sec = ms / 1000;
6334 t.tv_usec = (ms % 1000) * 1000;
6335 hz = tvtohz(&t);
6336 if (hz < 0) {
6337 hz = 0x7fffffff;
6338 }
6339 if (hz == 0) {
6340 hz = 1;
6341 }
6342 return (hz);
6343}
6344
6345void
6346isp_platform_intr(void *arg)
6347{
6348 ispsoftc_t *isp = arg;
6354 uint32_t isr;
6355 uint16_t sema, mbox;
6349 uint16_t isr, sema, info;
6356
6357 ISP_LOCK(isp);
6358 isp->isp_intcnt++;
6350
6351 ISP_LOCK(isp);
6352 isp->isp_intcnt++;
6359 if (ISP_READ_ISR(isp, &isr, &sema, &mbox) == 0) {
6353 if (ISP_READ_ISR(isp, &isr, &sema, &info))
6354 isp_intr(isp, isr, sema, info);
6355 else
6360 isp->isp_intbogus++;
6356 isp->isp_intbogus++;
6361 } else {
6362 isp_intr(isp, isr, sema, mbox);
6363 }
6364 ISP_UNLOCK(isp);
6365}
6366
6367void
6368isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
6369{
6370 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
6371 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
6372 } else {
6373 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
6374 }
6375 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
6376}
6377
6378/*
6379 * Reset the command reference number for all LUNs on a specific target
6380 * (needed when a target arrives again) or for all targets on a port
6381 * (needed for events like a LIP).
6382 */
6383void
6384isp_fcp_reset_crn(struct isp_fc *fc, uint32_t tgt, int tgt_set)
6385{
6386 int i;
6387 struct isp_nexus *nxp;
6388
6389 if (tgt_set == 0)
6390 isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN on all targets");
6391 else
6392 isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN target %u", tgt);
6393
6394 for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
6395 nxp = fc->nexus_hash[i];
6396 while (nxp) {
6397 if ((tgt_set != 0) && (tgt == nxp->tgt))
6398 nxp->crnseed = 0;
6399
6400 nxp = nxp->next;
6401 }
6402 }
6403}
6404
6405int
6406isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
6407{
6408 uint32_t chan, tgt, lun;
6409 struct isp_fc *fc;
6410 struct isp_nexus *nxp;
6411 int idx;
6412
6413 if (isp->isp_type < ISP_HA_FC_2300)
6414 return (0);
6415
6416 chan = XS_CHANNEL(cmd);
6417 tgt = XS_TGT(cmd);
6418 lun = XS_LUN(cmd);
6419 fc = &isp->isp_osinfo.pc.fc[chan];
6420 idx = NEXUS_HASH(tgt, lun);
6421 nxp = fc->nexus_hash[idx];
6422
6423 while (nxp) {
6424 if (nxp->tgt == tgt && nxp->lun == lun)
6425 break;
6426 nxp = nxp->next;
6427 }
6428 if (nxp == NULL) {
6429 nxp = fc->nexus_free_list;
6430 if (nxp == NULL) {
6431 nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
6432 if (nxp == NULL) {
6433 return (-1);
6434 }
6435 } else {
6436 fc->nexus_free_list = nxp->next;
6437 }
6438 nxp->tgt = tgt;
6439 nxp->lun = lun;
6440 nxp->next = fc->nexus_hash[idx];
6441 fc->nexus_hash[idx] = nxp;
6442 }
6443 if (nxp) {
6444 if (nxp->crnseed == 0)
6445 nxp->crnseed = 1;
6446 if (cmd)
6447 PISP_PCMD(cmd)->crn = nxp->crnseed;
6448 *crnp = nxp->crnseed++;
6449 return (0);
6450 }
6451 return (-1);
6452}
6453
6454/*
6455 * We enter with the lock held
6456 */
6457void
6458isp_timer(void *arg)
6459{
6460 ispsoftc_t *isp = arg;
6461#ifdef ISP_TARGET_MODE
6462 isp_tmcmd_restart(isp);
6463#endif
6464 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
6465}
6466
6467isp_ecmd_t *
6468isp_get_ecmd(ispsoftc_t *isp)
6469{
6470 isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
6471 if (ecmd) {
6472 isp->isp_osinfo.ecmd_free = ecmd->next;
6473 }
6474 return (ecmd);
6475}
6476
6477void
6478isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
6479{
6480 ecmd->next = isp->isp_osinfo.ecmd_free;
6481 isp->isp_osinfo.ecmd_free = ecmd;
6482}
6357 ISP_UNLOCK(isp);
6358}
6359
6360void
6361isp_common_dmateardown(ispsoftc_t *isp, struct ccb_scsiio *csio, uint32_t hdl)
6362{
6363 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
6364 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTREAD);
6365 } else {
6366 bus_dmamap_sync(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap, BUS_DMASYNC_POSTWRITE);
6367 }
6368 bus_dmamap_unload(isp->isp_osinfo.dmat, PISP_PCMD(csio)->dmap);
6369}
6370
6371/*
6372 * Reset the command reference number for all LUNs on a specific target
6373 * (needed when a target arrives again) or for all targets on a port
6374 * (needed for events like a LIP).
6375 */
6376void
6377isp_fcp_reset_crn(struct isp_fc *fc, uint32_t tgt, int tgt_set)
6378{
6379 int i;
6380 struct isp_nexus *nxp;
6381
6382 if (tgt_set == 0)
6383 isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN on all targets");
6384 else
6385 isp_prt(fc->isp, ISP_LOG_SANCFG, "resetting CRN target %u", tgt);
6386
6387 for (i = 0; i < NEXUS_HASH_WIDTH; i++) {
6388 nxp = fc->nexus_hash[i];
6389 while (nxp) {
6390 if ((tgt_set != 0) && (tgt == nxp->tgt))
6391 nxp->crnseed = 0;
6392
6393 nxp = nxp->next;
6394 }
6395 }
6396}
6397
6398int
6399isp_fcp_next_crn(ispsoftc_t *isp, uint8_t *crnp, XS_T *cmd)
6400{
6401 uint32_t chan, tgt, lun;
6402 struct isp_fc *fc;
6403 struct isp_nexus *nxp;
6404 int idx;
6405
6406 if (isp->isp_type < ISP_HA_FC_2300)
6407 return (0);
6408
6409 chan = XS_CHANNEL(cmd);
6410 tgt = XS_TGT(cmd);
6411 lun = XS_LUN(cmd);
6412 fc = &isp->isp_osinfo.pc.fc[chan];
6413 idx = NEXUS_HASH(tgt, lun);
6414 nxp = fc->nexus_hash[idx];
6415
6416 while (nxp) {
6417 if (nxp->tgt == tgt && nxp->lun == lun)
6418 break;
6419 nxp = nxp->next;
6420 }
6421 if (nxp == NULL) {
6422 nxp = fc->nexus_free_list;
6423 if (nxp == NULL) {
6424 nxp = malloc(sizeof (struct isp_nexus), M_DEVBUF, M_ZERO|M_NOWAIT);
6425 if (nxp == NULL) {
6426 return (-1);
6427 }
6428 } else {
6429 fc->nexus_free_list = nxp->next;
6430 }
6431 nxp->tgt = tgt;
6432 nxp->lun = lun;
6433 nxp->next = fc->nexus_hash[idx];
6434 fc->nexus_hash[idx] = nxp;
6435 }
6436 if (nxp) {
6437 if (nxp->crnseed == 0)
6438 nxp->crnseed = 1;
6439 if (cmd)
6440 PISP_PCMD(cmd)->crn = nxp->crnseed;
6441 *crnp = nxp->crnseed++;
6442 return (0);
6443 }
6444 return (-1);
6445}
6446
6447/*
6448 * We enter with the lock held
6449 */
6450void
6451isp_timer(void *arg)
6452{
6453 ispsoftc_t *isp = arg;
6454#ifdef ISP_TARGET_MODE
6455 isp_tmcmd_restart(isp);
6456#endif
6457 callout_reset(&isp->isp_osinfo.tmo, isp_timer_count, isp_timer, isp);
6458}
6459
6460isp_ecmd_t *
6461isp_get_ecmd(ispsoftc_t *isp)
6462{
6463 isp_ecmd_t *ecmd = isp->isp_osinfo.ecmd_free;
6464 if (ecmd) {
6465 isp->isp_osinfo.ecmd_free = ecmd->next;
6466 }
6467 return (ecmd);
6468}
6469
6470void
6471isp_put_ecmd(ispsoftc_t *isp, isp_ecmd_t *ecmd)
6472{
6473 ecmd->next = isp->isp_osinfo.ecmd_free;
6474 isp->isp_osinfo.ecmd_free = ecmd;
6475}