xref: /illumos-gate/usr/src/uts/sun/io/scsi/adapters/fas.c (revision 89b43686)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
25  */
26 
27 
28 /*
29  * ISSUES
30  *
31  * - more consistent error messages
32  * - report name of device on errors?
33  * - if wide target renegotiates sync, back to narrow?
34  * - last_msgout is not accurate ????
35  * - resolve XXXX
36  * - improve msg reject code (use special msg reject handler)
37  * - better use of IDE message
38  * - keep track if ATN remains asserted and target not going into
39  *   a msg-out phase
40  * - improve comments
41  * - no slave accesses when start address is odd and dma hasn't started
42  *   this affect asserting ATN
43  */
44 
45 /*
46  * fas - QLogic fas366 wide/fast SCSI Processor HBA driver with
47  *	tagged and non-tagged queueing support
48  */
49 #if defined(lint) && !defined(DEBUG)
50 #define	DEBUG	1
51 #define	FASDEBUG
52 #endif
53 
54 #define	DMA_REG_TRACING 	/* enable dma register access tracing */
55 
56 
57 /*
58  * standard header files
59  */
60 #include <sys/note.h>
61 #include <sys/scsi/scsi.h>
62 #include <sys/file.h>
63 #include <sys/vtrace.h>
64 
65 /*
66  * private header files
67  */
68 #include <sys/scsi/adapters/fasdma.h>
69 #include <sys/scsi/adapters/fasreg.h>
70 #include <sys/scsi/adapters/fasvar.h>
71 #include <sys/scsi/adapters/fascmd.h>
72 #include <sys/scsi/impl/scsi_reset_notify.h>
73 
74 /*
75  * tunables
76  */
77 static int		fas_selection_timeout = 250; /* 250 milliseconds */
78 static uchar_t		fas_default_offset = DEFAULT_OFFSET;
79 
80 /*
81  * needed for presto support, do not remove
82  */
83 static int		fas_enable_sbus64 = 1;
84 
85 #ifdef	FASDEBUG
86 int			fasdebug = 0;
87 int			fasdebug_instance = -1; /* debug all instances */
88 static int		fas_burstsizes_limit = -1;
89 static int		fas_no_sync_wide_backoff = 0;
90 #endif	/* FASDEBUG */
91 
92 /*
93  * Local static data protected by global mutex
94  */
95 static kmutex_t 	fas_global_mutex; /* to allow concurrent attach */
96 
97 static int		fas_scsi_watchdog_tick; /* in seconds, for all	*/
98 					/* instances			*/
99 static clock_t		fas_tick;	/* fas_watch() interval in Hz	*/
100 static timeout_id_t	fas_reset_watch; /* timeout id for reset watch	*/
101 static timeout_id_t	fas_timeout_id = 0;
102 static int		fas_timeout_initted = 0;
103 
104 static krwlock_t	fas_global_rwlock;
105 
106 static void		*fas_state;	/* soft state ptr		*/
107 static struct fas	*fas_head;	/* link all softstate structures */
108 static struct fas	*fas_tail;	/* for fas_watch()		*/
109 
110 static kmutex_t		fas_log_mutex;
111 static char		fas_log_buf[256];
112 _NOTE(MUTEX_PROTECTS_DATA(fas_global_mutex, fas_reset_watch))
113 _NOTE(DATA_READABLE_WITHOUT_LOCK(fas_state fas_head fas_tail \
114 	fas_scsi_watchdog_tick fas_tick))
115 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", fas::f_quiesce_timeid))
116 
117 /*
118  * dma attribute structure for scsi engine
119  */
120 static ddi_dma_attr_t dma_fasattr	= {
121 	DMA_ATTR_V0, (unsigned long long)0,
122 	(unsigned long long)0xffffffff, (unsigned long long)((1<<24)-1),
123 	1, DEFAULT_BURSTSIZE, 1,
124 	(unsigned long long)0xffffffff, (unsigned long long)0xffffffff,
125 	1, 512, 0
126 };
127 
128 /*
129  * optional torture test stuff
130  */
131 #ifdef	FASDEBUG
132 #define	FAS_TEST
133 static int fas_ptest_emsgin;
134 static int fas_ptest_msgin;
135 static int fas_ptest_msg = -1;
136 static int fas_ptest_status;
137 static int fas_ptest_data_in;
138 static int fas_atest;
139 static int fas_atest_disc;
140 static int fas_atest_reconn;
141 static void fas_test_abort(struct fas *fas, int slot);
142 static int fas_rtest;
143 static int fas_rtest_type;
144 static void fas_test_reset(struct fas *fas, int slot);
145 static int fas_force_timeout;
146 static int fas_btest;
147 static int fas_test_stop;
148 static int fas_transport_busy;
149 static int fas_transport_busy_rqs;
150 static int fas_transport_reject;
151 static int fas_arqs_failure;
152 static int fas_tran_err;
153 static int fas_test_untagged;
154 static int fas_enable_untagged;
155 #endif
156 
157 /*
158  * warlock directives
159  */
160 _NOTE(DATA_READABLE_WITHOUT_LOCK(dma fasdebug))
161 _NOTE(SCHEME_PROTECTS_DATA("just test variables", fas_transport_busy))
162 _NOTE(SCHEME_PROTECTS_DATA("just test variables", fas_transport_busy_rqs))
163 _NOTE(SCHEME_PROTECTS_DATA("just test variables", fas_transport_reject))
164 _NOTE(SCHEME_PROTECTS_DATA("just test variables", fas_arqs_failure))
165 _NOTE(SCHEME_PROTECTS_DATA("just test variables", fas_tran_err))
166 _NOTE(MUTEX_PROTECTS_DATA(fas_log_mutex, fas_log_buf))
167 _NOTE(MUTEX_PROTECTS_DATA(fas_global_mutex, fas_reset_watch))
168 _NOTE(DATA_READABLE_WITHOUT_LOCK(fas_state fas_head fas_tail \
169 	fas_scsi_watchdog_tick fas_tick))
170 
171 /*
172  * function prototypes
173  *
174  * scsa functions are exported by means of the transport table:
175  */
176 static int fas_scsi_tgt_probe(struct scsi_device *sd,
177     int (*waitfunc)(void));
178 static int fas_scsi_tgt_init(dev_info_t *, dev_info_t *,
179     scsi_hba_tran_t *, struct scsi_device *);
180 static int fas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt);
181 static int fas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
182 static int fas_scsi_reset(struct scsi_address *ap, int level);
183 static int fas_scsi_getcap(struct scsi_address *ap, char *cap, int whom);
184 static int fas_scsi_setcap(struct scsi_address *ap, char *cap, int value,
185     int whom);
186 static struct scsi_pkt *fas_scsi_init_pkt(struct scsi_address *ap,
187     struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
188     int tgtlen, int flags, int (*callback)(), caddr_t arg);
189 static void fas_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt);
190 static void fas_scsi_dmafree(struct scsi_address *ap,
191     struct scsi_pkt *pkt);
192 static void fas_scsi_sync_pkt(struct scsi_address *ap,
193     struct scsi_pkt *pkt);
194 
195 /*
196  * internal functions:
197  */
198 static int fas_prepare_pkt(struct fas *fas, struct fas_cmd *sp);
199 static int fas_alloc_tag(struct fas *fas, struct fas_cmd *sp);
200 static int fas_accept_pkt(struct fas *fas, struct fas_cmd *sp, int flag);
201 static void fas_empty_waitQ(struct fas *fas);
202 static void fas_move_waitQ_to_readyQ(struct fas *fas);
203 static void fas_check_waitQ_and_mutex_exit(struct fas *fas);
204 static int fas_istart(struct fas *fas);
205 static int fas_ustart(struct fas *fas);
206 static int fas_startcmd(struct fas *fas, struct fas_cmd *sp);
207 
208 static int fas_pkt_alloc_extern(struct fas *fas, struct fas_cmd *sp,
209     int cmdlen, int tgtlen, int statuslen, int kf);
210 static void fas_pkt_destroy_extern(struct fas *fas, struct fas_cmd *sp);
211 static int fas_kmem_cache_constructor(void *buf, void *cdrarg, int kmflags);
212 static void fas_kmem_cache_destructor(void *buf, void *cdrarg);
213 
214 static int fas_finish(struct fas *fas);
215 static void fas_handle_qfull(struct fas *fas, struct fas_cmd *sp);
216 static void fas_restart_cmd(void *);
217 static int fas_dopoll(struct fas *fas, int timeout);
218 static void fas_runpoll(struct fas *fas, short slot, struct fas_cmd *sp);
219 static uint_t fas_intr(caddr_t arg);
220 static int fas_intr_svc(struct	fas *fas);
221 static int fas_phasemanage(struct fas *fas);
222 static int fas_handle_unknown(struct fas *fas);
223 static int fas_handle_cmd_start(struct fas *fas);
224 static int fas_handle_cmd_done(struct fas *fas);
225 static int fas_handle_msg_out_start(struct fas *fas);
226 static int fas_handle_msg_out_done(struct fas *fas);
227 static int fas_handle_clearing(struct fas *fas);
228 static int fas_handle_data_start(struct fas *fas);
229 static int fas_handle_data_done(struct fas *fas);
230 static int fas_handle_c_cmplt(struct fas *fas);
231 static int fas_handle_msg_in_start(struct fas *fas);
232 static int fas_handle_more_msgin(struct fas *fas);
233 static int fas_handle_msg_in_done(struct fas *fas);
234 static int fas_onebyte_msg(struct fas *fas);
235 static int fas_twobyte_msg(struct fas *fas);
236 static int fas_multibyte_msg(struct fas *fas);
237 static void fas_revert_to_async(struct fas *fas, int tgt);
238 static int fas_finish_select(struct fas *fas);
239 static int fas_reselect_preempt(struct fas *fas);
240 static int fas_reconnect(struct fas *fas);
241 static int fas_handle_selection(struct fas *fas);
242 static void fas_head_of_readyQ(struct fas *fas, struct fas_cmd *sp);
243 static int fas_handle_gross_err(struct fas *fas);
244 static int fas_illegal_cmd_or_bus_reset(struct fas *fas);
245 static int fas_check_dma_error(struct fas *fas);
246 
247 static void fas_make_sdtr(struct fas *fas, int msgout_offset, int target);
248 static void fas_make_wdtr(struct fas *fas, int msgout_offset, int target,
249     int width);
250 static void fas_update_props(struct fas *fas, int tgt);
251 static void fas_update_this_prop(struct fas *fas, char *property, int value);
252 
253 static int fas_commoncap(struct scsi_address *ap, char *cap, int val,
254     int tgtonly, int doset);
255 
256 static void fas_watch(void *arg);
257 static void fas_watchsubr(struct fas *fas);
258 static void fas_cmd_timeout(struct fas *fas, int slot);
259 static void fas_sync_wide_backoff(struct fas *fas, struct fas_cmd *sp,
260     int slot);
261 static void fas_reset_sync_wide(struct fas *fas);
262 static void fas_set_wide_conf3(struct fas *fas, int target, int width);
263 static void fas_force_renegotiation(struct fas *fas, int target);
264 
265 static int fas_set_new_window(struct fas *fas, struct fas_cmd *sp);
266 static int fas_restore_pointers(struct fas *fas, struct fas_cmd *sp);
267 static int fas_next_window(struct fas *fas, struct fas_cmd *sp, uint64_t end);
268 
269 /*PRINTFLIKE3*/
270 static void fas_log(struct fas *fas, int level, const char *fmt, ...);
271 /*PRINTFLIKE2*/
272 static void fas_printf(struct fas *fas, const char *fmt, ...);
273 static void fas_printstate(struct fas *fas, char *msg);
274 static void fas_dump_cmd(struct fas *fas, struct fas_cmd *sp);
275 static void fas_short_dump_cmd(struct fas *fas, struct fas_cmd *sp);
276 static char *fas_state_name(ushort_t state);
277 
278 static void fas_makeproxy_cmd(struct fas_cmd *sp,
279     struct scsi_address *ap, struct scsi_pkt *pkt, int nmsg, ...);
280 static int fas_do_proxy_cmd(struct fas *fas, struct fas_cmd *sp,
281     struct scsi_address *ap, char *what);
282 
283 static void fas_internal_reset(struct fas *fas, int reset_action);
284 static int fas_alloc_active_slots(struct fas *fas, int slot, int flag);
285 
286 static int fas_abort_curcmd(struct fas *fas);
287 static int fas_abort_cmd(struct fas *fas, struct fas_cmd *sp, int slot);
288 static int fas_do_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
289 static int fas_do_scsi_reset(struct scsi_address *ap, int level);
290 static int fas_remove_from_readyQ(struct fas *fas, struct fas_cmd *sp,
291     int slot);
292 static void fas_flush_readyQ(struct fas *fas, int slot);
293 static void fas_flush_tagQ(struct fas *fas, int slot);
294 static void fas_flush_cmd(struct fas *fas, struct fas_cmd *sp,
295     uchar_t reason, uint_t stat);
296 static int fas_abort_connected_cmd(struct fas *fas, struct fas_cmd *sp,
297     uchar_t msg);
298 static int fas_abort_disconnected_cmd(struct fas *fas, struct scsi_address *ap,
299     struct fas_cmd *sp, uchar_t msg, int slot);
300 static void fas_mark_packets(struct fas *fas, int slot, uchar_t reason,
301     uint_t stat);
302 static void fas_set_pkt_reason(struct fas *fas, struct fas_cmd *sp,
303     uchar_t reason, uint_t stat);
304 
305 static int fas_reset_bus(struct fas *fas);
306 static int fas_reset_recovery(struct fas *fas);
307 static int fas_reset_connected_cmd(struct fas *fas, struct scsi_address *ap);
308 static int fas_reset_disconnected_cmd(struct fas *fas, struct scsi_address *ap);
309 static void fas_start_watch_reset_delay(struct fas *);
310 static void fas_setup_reset_delay(struct fas *fas);
311 static void fas_watch_reset_delay(void *arg);
312 static int fas_watch_reset_delay_subr(struct fas *fas);
313 static void fas_reset_cleanup(struct fas *fas, int slot);
314 static int fas_scsi_reset_notify(struct scsi_address *ap, int flag,
315     void (*callback)(caddr_t), caddr_t arg);
316 static int fas_scsi_quiesce(dev_info_t *hba_dip);
317 static int fas_scsi_unquiesce(dev_info_t *hba_dip);
318 
319 static void fas_set_throttles(struct fas *fas, int slot,
320     int n, int what);
321 static void fas_set_all_lun_throttles(struct fas *fas, int slot, int what);
322 static void fas_full_throttle(struct fas *fas, int slot);
323 static void fas_remove_cmd(struct fas *fas, struct fas_cmd *sp, int timeout);
324 static void fas_decrement_ncmds(struct fas *fas, struct fas_cmd *sp);
325 
326 static int fas_quiesce_bus(struct fas *fas);
327 static int fas_unquiesce_bus(struct fas *fas);
328 static void fas_ncmds_checkdrain(void *arg);
329 static int fas_check_outstanding(struct fas *fas);
330 
331 static int fas_create_arq_pkt(struct fas *fas, struct scsi_address *ap);
332 static int fas_delete_arq_pkt(struct fas *fas, struct scsi_address *ap);
333 static int fas_handle_sts_chk(struct fas *fas, struct fas_cmd *sp);
334 void fas_complete_arq_pkt(struct scsi_pkt *pkt);
335 
336 void fas_call_pkt_comp(struct fas *fas, struct fas_cmd *sp);
337 void fas_empty_callbackQ(struct fas *fas);
338 int fas_init_callbacks(struct fas *fas);
339 void fas_destroy_callbacks(struct fas *fas);
340 
341 static int fas_check_dma_error(struct fas *fas);
342 static int fas_init_chip(struct fas *fas, uchar_t id);
343 
344 static void fas_read_fifo(struct fas *fas);
345 static void fas_write_fifo(struct fas *fas, uchar_t *buf, int length, int pad);
346 
347 #ifdef FASDEBUG
348 static void fas_reg_cmd_write(struct fas *fas, uint8_t cmd);
349 static void fas_reg_write(struct fas *fas, volatile uint8_t *p, uint8_t what);
350 static uint8_t fas_reg_read(struct fas *fas, volatile uint8_t *p);
351 
352 static void fas_dma_reg_write(struct fas *fas, volatile uint32_t *p,
353     uint32_t what);
354 static uint32_t fas_dma_reg_read(struct fas *fas, volatile uint32_t *p);
355 #else
356 #define	fas_reg_cmd_write(fas, cmd) \
357 	fas->f_reg->fas_cmd = (cmd), fas->f_last_cmd = (cmd)
358 #define	fas_reg_write(fas, p, what)  *(p) = (what)
359 #define	fas_reg_read(fas, p) *(p)
360 #define	fas_dma_reg_write(fas, p, what)  *(p) = (what)
361 #define	fas_dma_reg_read(fas, p) *(p)
362 #endif
363 
364 /*
365  * autoconfiguration data and routines.
366  */
367 static int fas_attach(dev_info_t *dev, ddi_attach_cmd_t cmd);
368 static int fas_detach(dev_info_t *dev, ddi_detach_cmd_t cmd);
369 static int fas_dr_detach(dev_info_t *dev);
370 
371 static struct dev_ops fas_ops = {
372 	DEVO_REV,		/* devo_rev, */
373 	0,			/* refcnt  */
374 	ddi_no_info,		/* info */
375 	nulldev,		/* identify */
376 	nulldev,		/* probe */
377 	fas_attach,		/* attach */
378 	fas_detach,		/* detach */
379 	nodev,			/* reset */
380 	NULL,			/* driver operations */
381 	NULL,			/* bus operations */
382 	NULL,			/* power */
383 	ddi_quiesce_not_supported,	/* devo_quiesce */
384 };
385 
386 static struct modldrv modldrv = {
387 	&mod_driverops, /* Type of module. This one is a driver */
388 	"FAS SCSI HBA Driver", /* Name of the module. */
389 	&fas_ops,	/* driver ops */
390 };
391 
392 static struct modlinkage modlinkage = {
393 	MODREV_1, (void *)&modldrv, NULL
394 };
395 
396 int
_init(void)397 _init(void)
398 {
399 	int rval;
400 	/* CONSTCOND */
401 	ASSERT(NO_COMPETING_THREADS);
402 
403 	rval = ddi_soft_state_init(&fas_state, sizeof (struct fas),
404 	    FAS_INITIAL_SOFT_SPACE);
405 	if (rval != 0) {
406 		return (rval);
407 	}
408 
409 	if ((rval = scsi_hba_init(&modlinkage)) != 0) {
410 		ddi_soft_state_fini(&fas_state);
411 		return (rval);
412 	}
413 
414 	mutex_init(&fas_global_mutex, NULL, MUTEX_DRIVER, NULL);
415 	rw_init(&fas_global_rwlock, NULL, RW_DRIVER, NULL);
416 
417 	mutex_init(&fas_log_mutex, NULL, MUTEX_DRIVER, NULL);
418 
419 	if ((rval = mod_install(&modlinkage)) != 0) {
420 		mutex_destroy(&fas_log_mutex);
421 		rw_destroy(&fas_global_rwlock);
422 		mutex_destroy(&fas_global_mutex);
423 		ddi_soft_state_fini(&fas_state);
424 		scsi_hba_fini(&modlinkage);
425 		return (rval);
426 	}
427 
428 	return (rval);
429 }
430 
431 int
_fini(void)432 _fini(void)
433 {
434 	int	rval;
435 	/* CONSTCOND */
436 	ASSERT(NO_COMPETING_THREADS);
437 
438 	if ((rval = mod_remove(&modlinkage)) == 0) {
439 		ddi_soft_state_fini(&fas_state);
440 		scsi_hba_fini(&modlinkage);
441 		mutex_destroy(&fas_log_mutex);
442 		rw_destroy(&fas_global_rwlock);
443 		mutex_destroy(&fas_global_mutex);
444 	}
445 	return (rval);
446 }
447 
448 int
_info(struct modinfo * modinfop)449 _info(struct modinfo *modinfop)
450 {
451 	/* CONSTCOND */
452 	ASSERT(NO_COMPETING_THREADS);
453 
454 	return (mod_info(&modlinkage, modinfop));
455 }
456 
457 static int
fas_scsi_tgt_probe(struct scsi_device * sd,int (* waitfunc)(void))458 fas_scsi_tgt_probe(struct scsi_device *sd,
459     int (*waitfunc)(void))
460 {
461 	dev_info_t *dip = ddi_get_parent(sd->sd_dev);
462 	int rval = SCSIPROBE_FAILURE;
463 	scsi_hba_tran_t *tran;
464 	struct fas *fas;
465 	int tgt = sd->sd_address.a_target;
466 
467 	tran = ddi_get_driver_private(dip);
468 	ASSERT(tran != NULL);
469 	fas = TRAN2FAS(tran);
470 
471 	/*
472 	 * force renegotiation since inquiry cmds do not cause
473 	 * check conditions
474 	 */
475 	mutex_enter(FAS_MUTEX(fas));
476 	fas_force_renegotiation(fas, tgt);
477 	mutex_exit(FAS_MUTEX(fas));
478 	rval = scsi_hba_probe(sd, waitfunc);
479 
480 	/*
481 	 * the scsi-options precedence is:
482 	 *	target-scsi-options		highest
483 	 * 	device-type-scsi-options
484 	 *	per bus scsi-options
485 	 *	global scsi-options		lowest
486 	 */
487 	mutex_enter(FAS_MUTEX(fas));
488 	if ((rval == SCSIPROBE_EXISTS) &&
489 	    ((fas->f_target_scsi_options_defined & (1 << tgt)) == 0)) {
490 		int options;
491 
492 		options = scsi_get_device_type_scsi_options(dip, sd, -1);
493 		if (options != -1) {
494 			fas->f_target_scsi_options[tgt] = options;
495 			fas_log(fas, CE_NOTE,
496 			    "?target%x-scsi-options = 0x%x\n", tgt,
497 			    fas->f_target_scsi_options[tgt]);
498 			fas_force_renegotiation(fas, tgt);
499 		}
500 	}
501 	mutex_exit(FAS_MUTEX(fas));
502 
503 	IPRINTF2("target%x-scsi-options= 0x%x\n",
504 	    tgt, fas->f_target_scsi_options[tgt]);
505 
506 	return (rval);
507 }
508 
509 
510 /*ARGSUSED*/
511 static int
fas_scsi_tgt_init(dev_info_t * hba_dip,dev_info_t * tgt_dip,scsi_hba_tran_t * hba_tran,struct scsi_device * sd)512 fas_scsi_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
513     scsi_hba_tran_t *hba_tran, struct scsi_device *sd)
514 {
515 	return (((sd->sd_address.a_target < NTARGETS_WIDE) &&
516 	    (sd->sd_address.a_lun < NLUNS_PER_TARGET)) ?
517 	    DDI_SUCCESS : DDI_FAILURE);
518 }
519 
520 /*ARGSUSED*/
521 static int
fas_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)522 fas_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
523 {
524 	struct fas	*fas = NULL;
525 	volatile struct dma	*dmar = NULL;
526 	volatile struct fasreg	*fasreg;
527 	ddi_dma_attr_t		*fas_dma_attr;
528 	ddi_device_acc_attr_t	dev_attr;
529 
530 	int			instance, id, slot, i, hm_rev;
531 	size_t			rlen;
532 	uint_t			count;
533 	char			buf[64];
534 	scsi_hba_tran_t		*tran =	NULL;
535 	char			intr_added = 0;
536 	char			mutex_init_done = 0;
537 	char			hba_attached = 0;
538 	char			bound_handle = 0;
539 	char			*prop_template = "target%d-scsi-options";
540 	char			prop_str[32];
541 
542 	/* CONSTCOND */
543 	ASSERT(NO_COMPETING_THREADS);
544 
545 	switch (cmd) {
546 	case DDI_ATTACH:
547 		break;
548 
549 	case DDI_RESUME:
550 		if ((tran = ddi_get_driver_private(dip)) == NULL)
551 			return (DDI_FAILURE);
552 
553 		fas = TRAN2FAS(tran);
554 		if (!fas) {
555 			return (DDI_FAILURE);
556 		}
557 		/*
558 		 * Reset hardware and softc to "no outstanding commands"
559 		 * Note that a check condition can result on first command
560 		 * to a target.
561 		 */
562 		mutex_enter(FAS_MUTEX(fas));
563 		fas_internal_reset(fas,
564 		    FAS_RESET_SOFTC|FAS_RESET_FAS|FAS_RESET_DMA);
565 
566 		(void) fas_reset_bus(fas);
567 
568 		fas->f_suspended = 0;
569 
570 		/* make sure that things get started */
571 		(void) fas_istart(fas);
572 		fas_check_waitQ_and_mutex_exit(fas);
573 
574 		mutex_enter(&fas_global_mutex);
575 		if (fas_timeout_id == 0) {
576 			fas_timeout_id = timeout(fas_watch, NULL, fas_tick);
577 			fas_timeout_initted = 1;
578 		}
579 		mutex_exit(&fas_global_mutex);
580 
581 		return (DDI_SUCCESS);
582 
583 	default:
584 		return (DDI_FAILURE);
585 	}
586 
587 	instance = ddi_get_instance(dip);
588 
589 	/*
590 	 * Since we know that some instantiations of this device can
591 	 * be plugged into slave-only SBus slots, check to see whether
592 	 * this is one such.
593 	 */
594 	if (ddi_slaveonly(dip) == DDI_SUCCESS) {
595 		cmn_err(CE_WARN,
596 		    "fas%d: device in slave-only slot", instance);
597 		return (DDI_FAILURE);
598 	}
599 
600 	if (ddi_intr_hilevel(dip, 0)) {
601 		/*
602 		 * Interrupt number '0' is a high-level interrupt.
603 		 * At this point you either add a special interrupt
604 		 * handler that triggers a soft interrupt at a lower level,
605 		 * or - more simply and appropriately here - you just
606 		 * fail the attach.
607 		 */
608 		cmn_err(CE_WARN,
609 		    "fas%d: Device is using a hilevel intr", instance);
610 		return (DDI_FAILURE);
611 	}
612 
613 	/*
614 	 * Allocate softc information.
615 	 */
616 	if (ddi_soft_state_zalloc(fas_state, instance) != DDI_SUCCESS) {
617 		cmn_err(CE_WARN,
618 		    "fas%d: cannot allocate soft state", instance);
619 		goto fail;
620 	}
621 
622 	fas = (struct fas *)ddi_get_soft_state(fas_state, instance);
623 
624 	if (fas == NULL) {
625 		goto fail;
626 	}
627 
628 	/*
629 	 * map in device registers
630 	 */
631 	dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
632 	dev_attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
633 	dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
634 
635 	if (ddi_regs_map_setup(dip, (uint_t)0, (caddr_t *)&dmar,
636 	    (off_t)0, (off_t)sizeof (struct dma),
637 	    &dev_attr, &fas->f_dmar_acc_handle) != DDI_SUCCESS) {
638 		cmn_err(CE_WARN, "fas%d: cannot map dma", instance);
639 		goto fail;
640 	}
641 
642 	if (ddi_regs_map_setup(dip, (uint_t)1, (caddr_t *)&fasreg,
643 	    (off_t)0, (off_t)sizeof (struct fasreg),
644 	    &dev_attr, &fas->f_regs_acc_handle) != DDI_SUCCESS) {
645 		cmn_err(CE_WARN,
646 		    "fas%d: unable to map fas366 registers", instance);
647 		goto fail;
648 	}
649 
650 	fas_dma_attr = &dma_fasattr;
651 	if (ddi_dma_alloc_handle(dip, fas_dma_attr,
652 	    DDI_DMA_SLEEP, NULL, &fas->f_dmahandle) != DDI_SUCCESS) {
653 		cmn_err(CE_WARN,
654 		    "fas%d: cannot alloc dma handle", instance);
655 		goto fail;
656 	}
657 
658 	/*
659 	 * allocate cmdarea and its dma handle
660 	 */
661 	if (ddi_dma_mem_alloc(fas->f_dmahandle,
662 	    (uint_t)2*FIFOSIZE,
663 	    &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
664 	    NULL, (caddr_t *)&fas->f_cmdarea, &rlen,
665 	    &fas->f_cmdarea_acc_handle) != DDI_SUCCESS) {
666 		cmn_err(CE_WARN,
667 		    "fas%d: cannot alloc cmd area", instance);
668 		goto fail;
669 	}
670 
671 	fas->f_reg = fasreg;
672 	fas->f_dma = dmar;
673 	fas->f_instance  = instance;
674 
675 	if (ddi_dma_addr_bind_handle(fas->f_dmahandle,
676 	    NULL, (caddr_t)fas->f_cmdarea,
677 	    rlen, DDI_DMA_RDWR|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
678 	    &fas->f_dmacookie, &count) != DDI_DMA_MAPPED) {
679 		cmn_err(CE_WARN,
680 		    "fas%d: cannot bind cmdarea", instance);
681 		goto fail;
682 	}
683 	bound_handle++;
684 
685 	ASSERT(count == 1);
686 
687 	/*
688 	 * Allocate a transport structure
689 	 */
690 	tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
691 
692 	/* Indicate that we are 'sizeof (scsi_*(9S))' clean. */
693 	scsi_size_clean(dip);		/* SCSI_SIZE_CLEAN_VERIFY ok */
694 
695 	/*
696 	 * initialize transport structure
697 	 */
698 	fas->f_tran			= tran;
699 	fas->f_dev			= dip;
700 	tran->tran_hba_private		= fas;
701 	tran->tran_tgt_private		= NULL;
702 	tran->tran_tgt_init		= fas_scsi_tgt_init;
703 	tran->tran_tgt_probe		= fas_scsi_tgt_probe;
704 	tran->tran_tgt_free		= NULL;
705 	tran->tran_start		= fas_scsi_start;
706 	tran->tran_abort		= fas_scsi_abort;
707 	tran->tran_reset		= fas_scsi_reset;
708 	tran->tran_getcap		= fas_scsi_getcap;
709 	tran->tran_setcap		= fas_scsi_setcap;
710 	tran->tran_init_pkt		= fas_scsi_init_pkt;
711 	tran->tran_destroy_pkt		= fas_scsi_destroy_pkt;
712 	tran->tran_dmafree		= fas_scsi_dmafree;
713 	tran->tran_sync_pkt		= fas_scsi_sync_pkt;
714 	tran->tran_reset_notify 	= fas_scsi_reset_notify;
715 	tran->tran_get_bus_addr		= NULL;
716 	tran->tran_get_name		= NULL;
717 	tran->tran_quiesce		= fas_scsi_quiesce;
718 	tran->tran_unquiesce		= fas_scsi_unquiesce;
719 	tran->tran_bus_reset		= NULL;
720 	tran->tran_add_eventcall	= NULL;
721 	tran->tran_get_eventcookie	= NULL;
722 	tran->tran_post_event		= NULL;
723 	tran->tran_remove_eventcall	= NULL;
724 
725 	fas->f_force_async = 0;
726 
727 	/*
728 	 * disable tagged queuing and wide for all targets
729 	 * (will be enabled by target driver if required)
730 	 * sync is enabled by default
731 	 */
732 	fas->f_nowide = fas->f_notag = ALL_TARGETS;
733 	fas->f_force_narrow = ALL_TARGETS;
734 
735 	/*
736 	 * By default we assume embedded devices and save time
737 	 * checking for timeouts in fas_watch() by skipping
738 	 * the rest of luns
739 	 * If we're talking to any non-embedded devices,
740 	 * we can't cheat and skip over non-zero luns anymore
741 	 * in fas_watch() and fas_ustart().
742 	 */
743 	fas->f_dslot = NLUNS_PER_TARGET;
744 
745 	/*
746 	 * f_active is used for saving disconnected cmds;
747 	 * For tagged targets, we need to increase the size later
748 	 * Only allocate for Lun == 0, if we probe a lun > 0 then
749 	 * we allocate an active structure
750 	 * If TQ gets enabled then we need to increase the size
751 	 * to hold 256 cmds
752 	 */
753 	for (slot = 0; slot < N_SLOTS; slot += NLUNS_PER_TARGET) {
754 		(void) fas_alloc_active_slots(fas, slot, KM_SLEEP);
755 	}
756 
757 	/*
758 	 * initialize the qfull retry counts
759 	 */
760 	for (i = 0; i < NTARGETS_WIDE; i++) {
761 		fas->f_qfull_retries[i] = QFULL_RETRIES;
762 		fas->f_qfull_retry_interval[i] =
763 		    drv_usectohz(QFULL_RETRY_INTERVAL * 1000);
764 
765 	}
766 
767 	/*
768 	 * Initialize throttles.
769 	 */
770 	fas_set_throttles(fas, 0, N_SLOTS, MAX_THROTTLE);
771 
772 	/*
773 	 * Initialize mask of deferred property updates
774 	 */
775 	fas->f_props_update = 0;
776 
777 	/*
778 	 * set host ID
779 	 */
780 	fas->f_fasconf = DEFAULT_HOSTID;
781 	id = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0, "initiator-id", -1);
782 	if (id == -1) {
783 		id = ddi_prop_get_int(DDI_DEV_T_ANY,	dip, 0,
784 		    "scsi-initiator-id", -1);
785 	}
786 	if (id != DEFAULT_HOSTID && id >= 0 && id < NTARGETS_WIDE) {
787 		fas_log(fas, CE_NOTE, "?initiator SCSI ID now %d\n", id);
788 		fas->f_fasconf = (uchar_t)id;
789 	}
790 
791 	/*
792 	 * find the burstsize and reduce ours if necessary
793 	 */
794 	fas->f_dma_attr = fas_dma_attr;
795 	fas->f_dma_attr->dma_attr_burstsizes &=
796 	    ddi_dma_burstsizes(fas->f_dmahandle);
797 
798 #ifdef FASDEBUG
799 	fas->f_dma_attr->dma_attr_burstsizes &= fas_burstsizes_limit;
800 	IPRINTF1("dma burstsize=%x\n", fas->f_dma_attr->dma_attr_burstsizes);
801 #endif
802 	/*
803 	 * Attach this instance of the hba
804 	 */
805 	if (scsi_hba_attach_setup(dip, fas->f_dma_attr, tran, 0) !=
806 	    DDI_SUCCESS) {
807 		fas_log(fas, CE_WARN, "scsi_hba_attach_setup failed");
808 		goto fail;
809 	}
810 	hba_attached++;
811 
812 	/*
813 	 * if scsi-options property exists, use it
814 	 */
815 	fas->f_scsi_options = ddi_prop_get_int(DDI_DEV_T_ANY,
816 	    dip, 0, "scsi-options", DEFAULT_SCSI_OPTIONS);
817 
818 	/*
819 	 * if scsi-selection-timeout property exists, use it
820 	 */
821 	fas_selection_timeout = ddi_prop_get_int(DDI_DEV_T_ANY,
822 	    dip, 0, "scsi-selection-timeout", SCSI_DEFAULT_SELECTION_TIMEOUT);
823 
824 	/*
825 	 * if hm-rev property doesn't exist, use old scheme for rev
826 	 */
827 	hm_rev = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
828 	    "hm-rev", -1);
829 
830 	if (hm_rev == 0xa0 || hm_rev == -1) {
831 		if (DMAREV(dmar) != 0) {
832 			fas->f_hm_rev = 0x20;
833 			fas_log(fas, CE_WARN,
834 			    "obsolete rev 2.0 FEPS chip, "
835 			    "possible data corruption");
836 		} else {
837 			fas->f_hm_rev = 0x10;
838 			fas_log(fas, CE_WARN,
839 			    "obsolete and unsupported rev 1.0 FEPS chip");
840 			goto fail;
841 		}
842 	} else if (hm_rev == 0x20) {
843 		fas->f_hm_rev = 0x21;
844 		fas_log(fas, CE_WARN, "obsolete rev 2.1 FEPS chip");
845 	} else {
846 		fas->f_hm_rev = (uchar_t)hm_rev;
847 		fas_log(fas, CE_NOTE, "?rev %x.%x FEPS chip\n",
848 		    (hm_rev >> 4) & 0xf, hm_rev & 0xf);
849 	}
850 
851 	if ((fas->f_scsi_options & SCSI_OPTIONS_SYNC) == 0) {
852 		fas->f_nosync = ALL_TARGETS;
853 	}
854 
855 	if ((fas->f_scsi_options & SCSI_OPTIONS_WIDE) == 0) {
856 		fas->f_nowide = ALL_TARGETS;
857 	}
858 
859 	/*
860 	 * if target<n>-scsi-options property exists, use it;
861 	 * otherwise use the f_scsi_options
862 	 */
863 	for (i = 0; i < NTARGETS_WIDE; i++) {
864 		(void) sprintf(prop_str, prop_template, i);
865 		fas->f_target_scsi_options[i] = ddi_prop_get_int(
866 		    DDI_DEV_T_ANY, dip, 0, prop_str, -1);
867 
868 		if (fas->f_target_scsi_options[i] != -1) {
869 			fas_log(fas, CE_NOTE, "?target%x-scsi-options=0x%x\n",
870 			    i, fas->f_target_scsi_options[i]);
871 			fas->f_target_scsi_options_defined |= 1 << i;
872 		} else {
873 			fas->f_target_scsi_options[i] = fas->f_scsi_options;
874 		}
875 		if (((fas->f_target_scsi_options[i] &
876 		    SCSI_OPTIONS_DR) == 0) &&
877 		    (fas->f_target_scsi_options[i] & SCSI_OPTIONS_TAG)) {
878 			fas->f_target_scsi_options[i] &= ~SCSI_OPTIONS_TAG;
879 			fas_log(fas, CE_WARN,
880 			    "Disabled TQ since disconnects are disabled");
881 		}
882 	}
883 
884 	fas->f_scsi_tag_age_limit =
885 	    ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0, "scsi-tag-age-limit",
886 	    DEFAULT_TAG_AGE_LIMIT);
887 
888 	fas->f_scsi_reset_delay = ddi_prop_get_int(DDI_DEV_T_ANY,
889 	    dip, 0, "scsi-reset-delay", SCSI_DEFAULT_RESET_DELAY);
890 	if (fas->f_scsi_reset_delay == 0) {
891 		fas_log(fas, CE_NOTE,
892 		    "scsi_reset_delay of 0 is not recommended,"
893 		    " resetting to SCSI_DEFAULT_RESET_DELAY\n");
894 		fas->f_scsi_reset_delay = SCSI_DEFAULT_RESET_DELAY;
895 	}
896 
897 	/*
898 	 * get iblock cookie and initialize mutexes
899 	 */
900 	if (ddi_get_iblock_cookie(dip, (uint_t)0, &fas->f_iblock)
901 	    != DDI_SUCCESS) {
902 		cmn_err(CE_WARN, "fas_attach: cannot get iblock cookie");
903 		goto fail;
904 	}
905 
906 	mutex_init(&fas->f_mutex, NULL, MUTEX_DRIVER, fas->f_iblock);
907 	cv_init(&fas->f_cv, NULL, CV_DRIVER, NULL);
908 
909 	/*
910 	 * initialize mutex for waitQ
911 	 */
912 	mutex_init(&fas->f_waitQ_mutex, NULL, MUTEX_DRIVER, fas->f_iblock);
913 	mutex_init_done++;
914 
915 	/*
916 	 * initialize callback mechanism (immediate callback)
917 	 */
918 	mutex_enter(&fas_global_mutex);
919 	if (fas_init_callbacks(fas)) {
920 		mutex_exit(&fas_global_mutex);
921 		goto fail;
922 	}
923 	mutex_exit(&fas_global_mutex);
924 
925 	/*
926 	 * kstat_intr support
927 	 */
928 	(void) sprintf(buf, "fas%d", instance);
929 	fas->f_intr_kstat = kstat_create("fas", instance, buf, "controller", \
930 	    KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
931 	if (fas->f_intr_kstat)
932 		kstat_install(fas->f_intr_kstat);
933 
934 	/*
935 	 * install interrupt handler
936 	 */
937 	mutex_enter(FAS_MUTEX(fas));
938 	if (ddi_add_intr(dip, (uint_t)0, &fas->f_iblock, NULL,
939 	    fas_intr, (caddr_t)fas)) {
940 		cmn_err(CE_WARN, "fas: cannot add intr");
941 		mutex_exit(FAS_MUTEX(fas));
942 		goto fail;
943 	}
944 	intr_added++;
945 
946 	/*
947 	 * initialize fas chip
948 	 */
949 	if (fas_init_chip(fas, id))	{
950 		cmn_err(CE_WARN, "fas: cannot initialize");
951 		mutex_exit(FAS_MUTEX(fas));
952 		goto fail;
953 	}
954 	mutex_exit(FAS_MUTEX(fas));
955 
956 	/*
957 	 * create kmem cache for packets
958 	 */
959 	(void) sprintf(buf, "fas%d_cache", instance);
960 	fas->f_kmem_cache = kmem_cache_create(buf,
961 	    EXTCMD_SIZE, 8,
962 	    fas_kmem_cache_constructor, fas_kmem_cache_destructor,
963 	    NULL, (void *)fas, NULL, 0);
964 	if (fas->f_kmem_cache == NULL) {
965 		cmn_err(CE_WARN, "fas: cannot create kmem_cache");
966 		goto fail;
967 	}
968 
969 	/*
970 	 * at this point, we are not going to fail the attach
971 	 * so there is no need to undo the rest:
972 	 *
973 	 * add this fas to the list, this makes debugging easier
974 	 * and fas_watch() needs it to walk thru all fas's
975 	 */
976 	rw_enter(&fas_global_rwlock, RW_WRITER);
977 	if (fas_head == NULL) {
978 		fas_head = fas;
979 	} else {
980 		fas_tail->f_next = fas;
981 	}
982 	fas_tail = fas; 	/* point to last fas in list */
983 	rw_exit(&fas_global_rwlock);
984 
985 	/*
986 	 * there is one watchdog handler for all driver instances.
987 	 * start the watchdog if it hasn't been done yet
988 	 */
989 	mutex_enter(&fas_global_mutex);
990 	if (fas_scsi_watchdog_tick == 0) {
991 		fas_scsi_watchdog_tick = ddi_prop_get_int(DDI_DEV_T_ANY,
992 		    dip, 0, "scsi-watchdog-tick", DEFAULT_WD_TICK);
993 		if (fas_scsi_watchdog_tick != DEFAULT_WD_TICK) {
994 			fas_log(fas, CE_NOTE, "?scsi-watchdog-tick=%d\n",
995 			    fas_scsi_watchdog_tick);
996 		}
997 		fas_tick = drv_usectohz((clock_t)
998 		    fas_scsi_watchdog_tick * 1000000);
999 		IPRINTF2("fas scsi watchdog tick=%x, fas_tick=%lx\n",
1000 		    fas_scsi_watchdog_tick, fas_tick);
1001 		if (fas_timeout_id == 0) {
1002 			fas_timeout_id = timeout(fas_watch, NULL, fas_tick);
1003 			fas_timeout_initted = 1;
1004 		}
1005 	}
1006 	mutex_exit(&fas_global_mutex);
1007 
1008 	ddi_report_dev(dip);
1009 
1010 	return (DDI_SUCCESS);
1011 
1012 fail:
1013 	cmn_err(CE_WARN, "fas%d: cannot attach", instance);
1014 	if (fas) {
1015 		for (slot = 0; slot < N_SLOTS; slot++) {
1016 			struct f_slots *active = fas->f_active[slot];
1017 			if (active) {
1018 				kmem_free(active, active->f_size);
1019 				fas->f_active[slot] = NULL;
1020 			}
1021 		}
1022 		if (mutex_init_done) {
1023 			mutex_destroy(&fas->f_mutex);
1024 			mutex_destroy(&fas->f_waitQ_mutex);
1025 			cv_destroy(&fas->f_cv);
1026 		}
1027 		if (intr_added) {
1028 			ddi_remove_intr(dip, (uint_t)0, fas->f_iblock);
1029 		}
1030 		/*
1031 		 * kstat_intr support
1032 		 */
1033 		if (fas->f_intr_kstat) {
1034 			kstat_delete(fas->f_intr_kstat);
1035 		}
1036 		if (hba_attached) {
1037 			(void) scsi_hba_detach(dip);
1038 		}
1039 		if (tran) {
1040 			scsi_hba_tran_free(tran);
1041 		}
1042 		if (fas->f_kmem_cache) {
1043 			kmem_cache_destroy(fas->f_kmem_cache);
1044 		}
1045 		if (fas->f_cmdarea) {
1046 			if (bound_handle) {
1047 				(void) ddi_dma_unbind_handle(fas->f_dmahandle);
1048 			}
1049 			ddi_dma_mem_free(&fas->f_cmdarea_acc_handle);
1050 		}
1051 		if (fas->f_dmahandle) {
1052 			ddi_dma_free_handle(&fas->f_dmahandle);
1053 		}
1054 		fas_destroy_callbacks(fas);
1055 		if (fas->f_regs_acc_handle) {
1056 			ddi_regs_map_free(&fas->f_regs_acc_handle);
1057 		}
1058 		if (fas->f_dmar_acc_handle) {
1059 			ddi_regs_map_free(&fas->f_dmar_acc_handle);
1060 		}
1061 		ddi_soft_state_free(fas_state, instance);
1062 
1063 		ddi_remove_minor_node(dip, NULL);
1064 	}
1065 	return (DDI_FAILURE);
1066 }
1067 
1068 /*ARGSUSED*/
1069 static int
fas_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1070 fas_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1071 {
1072 	struct fas	*fas, *nfas;
1073 	scsi_hba_tran_t 	*tran;
1074 
1075 	/* CONSTCOND */
1076 	ASSERT(NO_COMPETING_THREADS);
1077 
1078 	switch (cmd) {
1079 	case DDI_DETACH:
1080 		return (fas_dr_detach(dip));
1081 
1082 	case DDI_SUSPEND:
1083 		if ((tran = ddi_get_driver_private(dip)) == NULL)
1084 			return (DDI_FAILURE);
1085 
1086 		fas = TRAN2FAS(tran);
1087 		if (!fas) {
1088 			return (DDI_FAILURE);
1089 		}
1090 
1091 		mutex_enter(FAS_MUTEX(fas));
1092 
1093 		fas->f_suspended = 1;
1094 
1095 		if (fas->f_ncmds) {
1096 			(void) fas_reset_bus(fas);
1097 			(void) fas_dopoll(fas, SHORT_POLL_TIMEOUT);
1098 		}
1099 		/*
1100 		 * disable dma and fas interrupt
1101 		 */
1102 		fas->f_dma_csr &= ~DMA_INTEN;
1103 		fas->f_dma_csr &= ~DMA_ENDVMA;
1104 		fas_dma_reg_write(fas, &fas->f_dma->dma_csr, fas->f_dma_csr);
1105 
1106 		mutex_exit(FAS_MUTEX(fas));
1107 
1108 		if (fas->f_quiesce_timeid) {
1109 			(void) untimeout(fas->f_quiesce_timeid);
1110 				fas->f_quiesce_timeid = 0;
1111 		}
1112 
1113 		if (fas->f_restart_cmd_timeid) {
1114 			(void) untimeout(fas->f_restart_cmd_timeid);
1115 				fas->f_restart_cmd_timeid = 0;
1116 		}
1117 
1118 		/* Last fas? */
1119 		rw_enter(&fas_global_rwlock, RW_WRITER);
1120 		for (nfas = fas_head; nfas; nfas = nfas->f_next) {
1121 			if (!nfas->f_suspended) {
1122 				rw_exit(&fas_global_rwlock);
1123 				return (DDI_SUCCESS);
1124 			}
1125 		}
1126 		rw_exit(&fas_global_rwlock);
1127 
1128 		mutex_enter(&fas_global_mutex);
1129 		if (fas_timeout_id != 0) {
1130 			timeout_id_t tid = fas_timeout_id;
1131 			fas_timeout_id = 0;
1132 			fas_timeout_initted = 0;
1133 			mutex_exit(&fas_global_mutex);
1134 			(void) untimeout(tid);
1135 		} else {
1136 			mutex_exit(&fas_global_mutex);
1137 		}
1138 
1139 		mutex_enter(&fas_global_mutex);
1140 		if (fas_reset_watch) {
1141 			timeout_id_t tid = fas_reset_watch;
1142 			fas_reset_watch = 0;
1143 			mutex_exit(&fas_global_mutex);
1144 			(void) untimeout(tid);
1145 		} else {
1146 			mutex_exit(&fas_global_mutex);
1147 		}
1148 
1149 		return (DDI_SUCCESS);
1150 
1151 	default:
1152 		return (DDI_FAILURE);
1153 	}
1154 	_NOTE(NOT_REACHED)
1155 	/* NOTREACHED */
1156 }
1157 
1158 static int
fas_dr_detach(dev_info_t * dip)1159 fas_dr_detach(dev_info_t *dip)
1160 {
1161 	struct fas 	*fas, *f;
1162 	scsi_hba_tran_t		*tran;
1163 	short		slot;
1164 	int			i, j;
1165 
1166 	if ((tran = ddi_get_driver_private(dip)) == NULL)
1167 		return (DDI_FAILURE);
1168 
1169 	fas = TRAN2FAS(tran);
1170 	if (!fas) {
1171 		return (DDI_FAILURE);
1172 	}
1173 
1174 	/*
1175 	 * disable interrupts
1176 	 */
1177 	fas->f_dma_csr &= ~DMA_INTEN;
1178 	fas->f_dma->dma_csr = fas->f_dma_csr;
1179 	ddi_remove_intr(dip, (uint_t)0, fas->f_iblock);
1180 
1181 	/*
1182 	 * Remove device instance from the global linked list
1183 	 */
1184 	rw_enter(&fas_global_rwlock, RW_WRITER);
1185 
1186 	if (fas_head == fas) {
1187 		f = fas_head = fas->f_next;
1188 	} else {
1189 		for (f = fas_head; f != (struct fas *)NULL; f = f->f_next) {
1190 			if (f->f_next == fas) {
1191 				f->f_next = fas->f_next;
1192 				break;
1193 			}
1194 		}
1195 
1196 		/*
1197 		 * Instance not in softc list. Since the
1198 		 * instance is not there in softc list, don't
1199 		 * enable interrupts, the instance is effectively
1200 		 * unusable.
1201 		 */
1202 		if (f == (struct fas *)NULL) {
1203 			cmn_err(CE_WARN, "fas_dr_detach: fas instance not"
1204 			    " in softc list!");
1205 			rw_exit(&fas_global_rwlock);
1206 			return (DDI_FAILURE);
1207 		}
1208 
1209 
1210 	}
1211 
1212 	if (fas_tail == fas)
1213 		fas_tail = f;
1214 
1215 	rw_exit(&fas_global_rwlock);
1216 
1217 	if (fas->f_intr_kstat)
1218 		kstat_delete(fas->f_intr_kstat);
1219 
1220 	fas_destroy_callbacks(fas);
1221 
1222 	scsi_hba_reset_notify_tear_down(fas->f_reset_notify_listf);
1223 
1224 	mutex_enter(&fas_global_mutex);
1225 	/*
1226 	 * destroy any outstanding tagged command info
1227 	 */
1228 	for (slot = 0; slot < N_SLOTS; slot++) {
1229 		struct f_slots *active = fas->f_active[slot];
1230 		if (active) {
1231 			ushort_t	tag;
1232 			for (tag = 0; tag < active->f_n_slots; tag++) {
1233 				struct fas_cmd	*sp = active->f_slot[tag];
1234 				if (sp) {
1235 					struct scsi_pkt *pkt = sp->cmd_pkt;
1236 					if (pkt) {
1237 						(void) fas_scsi_destroy_pkt(
1238 						    &pkt->pkt_address, pkt);
1239 					}
1240 					/* sp freed in fas_scsi_destroy_pkt */
1241 					active->f_slot[tag] = NULL;
1242 				}
1243 			}
1244 			kmem_free(active, active->f_size);
1245 			fas->f_active[slot] = NULL;
1246 		}
1247 		ASSERT(fas->f_tcmds[slot] == 0);
1248 	}
1249 
1250 	/*
1251 	 * disallow timeout thread rescheduling
1252 	 */
1253 	fas->f_flags |= FAS_FLG_NOTIMEOUTS;
1254 	mutex_exit(&fas_global_mutex);
1255 
1256 	if (fas->f_quiesce_timeid) {
1257 		(void) untimeout(fas->f_quiesce_timeid);
1258 	}
1259 
1260 	/*
1261 	 * last fas? ... if active, CANCEL watch threads.
1262 	 */
1263 	mutex_enter(&fas_global_mutex);
1264 	if (fas_head == (struct fas *)NULL) {
1265 		if (fas_timeout_initted) {
1266 			timeout_id_t tid = fas_timeout_id;
1267 			fas_timeout_initted = 0;
1268 			fas_timeout_id = 0;		/* don't resched */
1269 			mutex_exit(&fas_global_mutex);
1270 			(void) untimeout(tid);
1271 			mutex_enter(&fas_global_mutex);
1272 		}
1273 
1274 		if (fas_reset_watch) {
1275 			mutex_exit(&fas_global_mutex);
1276 			(void) untimeout(fas_reset_watch);
1277 			mutex_enter(&fas_global_mutex);
1278 			fas_reset_watch = 0;
1279 		}
1280 	}
1281 	mutex_exit(&fas_global_mutex);
1282 
1283 	if (fas->f_restart_cmd_timeid) {
1284 		(void) untimeout(fas->f_restart_cmd_timeid);
1285 		fas->f_restart_cmd_timeid = 0;
1286 	}
1287 
1288 	/*
1289 	 * destroy outstanding ARQ pkts
1290 	 */
1291 	for (i = 0; i < NTARGETS_WIDE; i++) {
1292 		for (j = 0; j < NLUNS_PER_TARGET; j++) {
1293 			int slot = i * NLUNS_PER_TARGET | j;
1294 			if (fas->f_arq_pkt[slot]) {
1295 				struct scsi_address	sa;
1296 				sa.a_hba_tran = NULL;		/* not used */
1297 				sa.a_target = (ushort_t)i;
1298 				sa.a_lun = (uchar_t)j;
1299 				(void) fas_delete_arq_pkt(fas, &sa);
1300 			}
1301 		}
1302 	}
1303 
1304 	/*
1305 	 * Remove device MT locks and CV
1306 	 */
1307 	mutex_destroy(&fas->f_waitQ_mutex);
1308 	mutex_destroy(&fas->f_mutex);
1309 	cv_destroy(&fas->f_cv);
1310 
1311 	/*
1312 	 * Release miscellaneous device resources
1313 	 */
1314 
1315 	if (fas->f_kmem_cache) {
1316 		kmem_cache_destroy(fas->f_kmem_cache);
1317 	}
1318 
1319 	if (fas->f_cmdarea != (uchar_t *)NULL) {
1320 		(void) ddi_dma_unbind_handle(fas->f_dmahandle);
1321 		ddi_dma_mem_free(&fas->f_cmdarea_acc_handle);
1322 	}
1323 
1324 	if (fas->f_dmahandle != (ddi_dma_handle_t)NULL) {
1325 		ddi_dma_free_handle(&fas->f_dmahandle);
1326 	}
1327 
1328 	if (fas->f_regs_acc_handle) {
1329 		ddi_regs_map_free(&fas->f_regs_acc_handle);
1330 	}
1331 	if (fas->f_dmar_acc_handle) {
1332 		ddi_regs_map_free(&fas->f_dmar_acc_handle);
1333 	}
1334 
1335 	/*
1336 	 * Remove properties created during attach()
1337 	 */
1338 	ddi_prop_remove_all(dip);
1339 
1340 	/*
1341 	 * Delete the DMA limits, transport vectors and remove the device
1342 	 * links to the scsi_transport layer.
1343 	 *	-- ddi_set_driver_private(dip, NULL)
1344 	 */
1345 	(void) scsi_hba_detach(dip);
1346 
1347 	/*
1348 	 * Free the scsi_transport structure for this device.
1349 	 */
1350 	scsi_hba_tran_free(tran);
1351 
1352 	ddi_soft_state_free(fas_state, ddi_get_instance(dip));
1353 
1354 	return (DDI_SUCCESS);
1355 }
1356 
1357 static int
fas_quiesce_bus(struct fas * fas)1358 fas_quiesce_bus(struct fas *fas)
1359 {
1360 	mutex_enter(FAS_MUTEX(fas));
1361 	IPRINTF("fas_quiesce: QUIESCEing\n");
1362 	IPRINTF3("fas_quiesce: ncmds (%d) ndisc (%d) state (%d)\n",
1363 	    fas->f_ncmds, fas->f_ndisc, fas->f_softstate);
1364 	fas_set_throttles(fas, 0, N_SLOTS, HOLD_THROTTLE);
1365 	if (fas_check_outstanding(fas)) {
1366 		fas->f_softstate |= FAS_SS_DRAINING;
1367 		fas->f_quiesce_timeid = timeout(fas_ncmds_checkdrain,
1368 		    fas, (FAS_QUIESCE_TIMEOUT * drv_usectohz(1000000)));
1369 		if (cv_wait_sig(FAS_CV(fas), FAS_MUTEX(fas)) == 0) {
1370 			/*
1371 			 * quiesce has been interrupted.
1372 			 */
1373 			IPRINTF("fas_quiesce: abort QUIESCE\n");
1374 			fas->f_softstate &= ~FAS_SS_DRAINING;
1375 			fas_set_throttles(fas, 0, N_SLOTS, MAX_THROTTLE);
1376 			(void) fas_istart(fas);
1377 			if (fas->f_quiesce_timeid != 0) {
1378 				mutex_exit(FAS_MUTEX(fas));
1379 #ifndef __lock_lint	/* warlock complains but there is a NOTE on this */
1380 				(void) untimeout(fas->f_quiesce_timeid);
1381 				fas->f_quiesce_timeid = 0;
1382 #endif
1383 				return (-1);
1384 			}
1385 			mutex_exit(FAS_MUTEX(fas));
1386 			return (-1);
1387 		} else {
1388 			IPRINTF("fas_quiesce: bus is QUIESCED\n");
1389 			ASSERT(fas->f_quiesce_timeid == 0);
1390 			fas->f_softstate &= ~FAS_SS_DRAINING;
1391 			fas->f_softstate |= FAS_SS_QUIESCED;
1392 			mutex_exit(FAS_MUTEX(fas));
1393 			return (0);
1394 		}
1395 	}
1396 	IPRINTF("fas_quiesce: bus was not busy QUIESCED\n");
1397 	mutex_exit(FAS_MUTEX(fas));
1398 	return (0);
1399 }
1400 
1401 static int
fas_unquiesce_bus(struct fas * fas)1402 fas_unquiesce_bus(struct fas *fas)
1403 {
1404 	mutex_enter(FAS_MUTEX(fas));
1405 	fas->f_softstate &= ~FAS_SS_QUIESCED;
1406 	fas_set_throttles(fas, 0, N_SLOTS, MAX_THROTTLE);
1407 	(void) fas_istart(fas);
1408 	IPRINTF("fas_quiesce: bus has been UNQUIESCED\n");
1409 	mutex_exit(FAS_MUTEX(fas));
1410 
1411 	return (0);
1412 }
1413 
1414 /*
1415  * invoked from timeout() to check the number of outstanding commands
1416  */
1417 static void
fas_ncmds_checkdrain(void * arg)1418 fas_ncmds_checkdrain(void *arg)
1419 {
1420 	struct fas *fas = arg;
1421 
1422 	mutex_enter(FAS_MUTEX(fas));
1423 	IPRINTF3("fas_checkdrain: ncmds (%d) ndisc (%d) state (%d)\n",
1424 	    fas->f_ncmds, fas->f_ndisc, fas->f_softstate);
1425 	if (fas->f_softstate & FAS_SS_DRAINING) {
1426 		fas->f_quiesce_timeid = 0;
1427 		if (fas_check_outstanding(fas) == 0) {
1428 			IPRINTF("fas_drain: bus has drained\n");
1429 			cv_signal(FAS_CV(fas));
1430 		} else {
1431 			/*
1432 			 * throttle may have been reset by a bus reset
1433 			 * or fas_runpoll()
1434 			 * XXX shouldn't be necessary
1435 			 */
1436 			fas_set_throttles(fas, 0, N_SLOTS, HOLD_THROTTLE);
1437 			IPRINTF("fas_drain: rescheduling timeout\n");
1438 			fas->f_quiesce_timeid = timeout(fas_ncmds_checkdrain,
1439 			    fas, (FAS_QUIESCE_TIMEOUT * drv_usectohz(1000000)));
1440 		}
1441 	}
1442 	mutex_exit(FAS_MUTEX(fas));
1443 }
1444 
1445 static int
fas_check_outstanding(struct fas * fas)1446 fas_check_outstanding(struct fas *fas)
1447 {
1448 	uint_t slot;
1449 	uint_t d = ((fas->f_dslot == 0)? 1 : fas->f_dslot);
1450 	int ncmds = 0;
1451 
1452 	ASSERT(mutex_owned(FAS_MUTEX(fas)));
1453 
1454 	for (slot = 0; slot < N_SLOTS; slot += d)
1455 		ncmds += fas->f_tcmds[slot];
1456 
1457 	return (ncmds);
1458 }
1459 
1460 
1461 #ifdef	FASDEBUG
1462 /*
1463  * fas register read/write functions with tracing
1464  */
1465 static void
fas_reg_tracing(struct fas * fas,int type,int regno,uint32_t what)1466 fas_reg_tracing(struct fas *fas, int type, int regno, uint32_t what)
1467 {
1468 	fas->f_reg_trace[fas->f_reg_trace_index++] = type;
1469 	fas->f_reg_trace[fas->f_reg_trace_index++] = regno;
1470 	fas->f_reg_trace[fas->f_reg_trace_index++] = what;
1471 	fas->f_reg_trace[fas->f_reg_trace_index++] = gethrtime();
1472 	fas->f_reg_trace[fas->f_reg_trace_index] = 0xff;
1473 	if (fas->f_reg_trace_index >= REG_TRACE_BUF_SIZE) {
1474 		fas->f_reg_trace_index = 0;
1475 	}
1476 }
1477 
1478 static void
fas_reg_cmd_write(struct fas * fas,uint8_t cmd)1479 fas_reg_cmd_write(struct fas *fas, uint8_t cmd)
1480 {
1481 	volatile struct fasreg *fasreg = fas->f_reg;
1482 	int regno = (uintptr_t)&fasreg->fas_cmd - (uintptr_t)fasreg;
1483 
1484 	fasreg->fas_cmd = cmd;
1485 	fas->f_last_cmd = cmd;
1486 
1487 	EPRINTF1("issuing cmd %x\n", (uchar_t)cmd);
1488 	fas_reg_tracing(fas, 0, regno, cmd);
1489 
1490 	fas->f_reg_cmds++;
1491 }
1492 
1493 static void
fas_reg_write(struct fas * fas,volatile uint8_t * p,uint8_t what)1494 fas_reg_write(struct fas *fas, volatile uint8_t *p, uint8_t what)
1495 {
1496 	int regno = (uintptr_t)p - (uintptr_t)fas->f_reg;
1497 
1498 	*p = what;
1499 
1500 	EPRINTF2("writing reg%x = %x\n", regno, what);
1501 	fas_reg_tracing(fas, 1, regno, what);
1502 
1503 	fas->f_reg_writes++;
1504 }
1505 
1506 static uint8_t
fas_reg_read(struct fas * fas,volatile uint8_t * p)1507 fas_reg_read(struct fas *fas, volatile uint8_t *p)
1508 {
1509 	uint8_t what;
1510 	int regno = (uintptr_t)p - (uintptr_t)fas->f_reg;
1511 
1512 	what = *p;
1513 
1514 	EPRINTF2("reading reg%x => %x\n", regno, what);
1515 	fas_reg_tracing(fas, 2, regno, what);
1516 
1517 	fas->f_reg_reads++;
1518 
1519 	return (what);
1520 }
1521 
1522 /*
1523  * dma register access routines
1524  */
1525 static void
fas_dma_reg_write(struct fas * fas,volatile uint32_t * p,uint32_t what)1526 fas_dma_reg_write(struct fas *fas, volatile uint32_t *p, uint32_t what)
1527 {
1528 	*p = what;
1529 	fas->f_reg_dma_writes++;
1530 
1531 #ifdef DMA_REG_TRACING
1532 {
1533 	int regno = (uintptr_t)p - (uintptr_t)fas->f_dma;
1534 	EPRINTF2("writing dma reg%x = %x\n", regno, what);
1535 	fas_reg_tracing(fas, 3, regno, what);
1536 }
1537 #endif
1538 }
1539 
1540 static uint32_t
fas_dma_reg_read(struct fas * fas,volatile uint32_t * p)1541 fas_dma_reg_read(struct fas *fas, volatile uint32_t *p)
1542 {
1543 	uint32_t what = *p;
1544 	fas->f_reg_dma_reads++;
1545 
1546 #ifdef DMA_REG_TRACING
1547 {
1548 	int regno = (uintptr_t)p - (uintptr_t)fas->f_dma;
1549 	EPRINTF2("reading dma reg%x => %x\n", regno, what);
1550 	fas_reg_tracing(fas, 4, regno, what);
1551 }
1552 #endif
1553 	return (what);
1554 }
1555 #endif
1556 
1557 #define	FIFO_EMPTY(fas)  (fas_reg_read(fas, &fas->f_reg->fas_stat2) & \
1558 		FAS_STAT2_EMPTY)
1559 #define	FIFO_CNT(fas) \
1560 	(fas_reg_read(fas, &fas->f_reg->fas_fifo_flag) & FIFO_CNT_MASK)
1561 
1562 #ifdef FASDEBUG
1563 static void
fas_assert_atn(struct fas * fas)1564 fas_assert_atn(struct fas *fas)
1565 {
1566 	fas_reg_cmd_write(fas, CMD_SET_ATN);
1567 #ifdef FAS_TEST
1568 	if (fas_test_stop > 1)
1569 		debug_enter("asserted atn");
1570 #endif
1571 }
1572 #else
1573 #define	fas_assert_atn(fas)  fas_reg_cmd_write(fas, CMD_SET_ATN)
1574 #endif
1575 
1576 /*
1577  * DMA macros; we use a shadow copy of the dma_csr to	save unnecessary
1578  * reads
1579  */
1580 #define	FAS_DMA_WRITE(fas, count, base, cmd) { \
1581 	volatile struct fasreg *fasreg = fas->f_reg; \
1582 	volatile struct dma *dmar = fas->f_dma; \
1583 	ASSERT((fas_dma_reg_read(fas, &dmar->dma_csr) & DMA_ENDVMA) == 0); \
1584 	SET_FAS_COUNT(fasreg, count); \
1585 	fas_reg_cmd_write(fas, cmd); \
1586 	fas_dma_reg_write(fas, &dmar->dma_count, count); \
1587 	fas->f_dma_csr |= \
1588 	    DMA_WRITE | DMA_ENDVMA | DMA_DSBL_DRAIN; \
1589 	fas_dma_reg_write(fas, &dmar->dma_addr, (fas->f_lastdma = base)); \
1590 	fas_dma_reg_write(fas, &dmar->dma_csr, fas->f_dma_csr); \
1591 }
1592 
1593 #define	FAS_DMA_WRITE_SETUP(fas, count, base) { \
1594 	volatile struct fasreg *fasreg = fas->f_reg; \
1595 	volatile struct dma *dmar = fas->f_dma; \
1596 	ASSERT((fas_dma_reg_read(fas, &dmar->dma_csr) & DMA_ENDVMA) == 0); \
1597 	SET_FAS_COUNT(fasreg, count); \
1598 	fas_dma_reg_write(fas, &dmar->dma_count, count); \
1599 	fas->f_dma_csr |= \
1600 	    DMA_WRITE | DMA_ENDVMA | DMA_DSBL_DRAIN; \
1601 	fas_dma_reg_write(fas, &dmar->dma_addr, (fas->f_lastdma = base)); \
1602 }
1603 
1604 
1605 #define	FAS_DMA_READ(fas, count, base, dmacount, cmd) { \
1606 	volatile struct fasreg *fasreg = fas->f_reg; \
1607 	volatile struct dma *dmar = fas->f_dma; \
1608 	ASSERT((fas_dma_reg_read(fas, &dmar->dma_csr) & DMA_ENDVMA) == 0); \
1609 	SET_FAS_COUNT(fasreg, count); \
1610 	fas_reg_cmd_write(fas, cmd); \
1611 	fas->f_dma_csr |= \
1612 	    (fas->f_dma_csr &	~DMA_WRITE) | DMA_ENDVMA | DMA_DSBL_DRAIN; \
1613 	fas_dma_reg_write(fas, &dmar->dma_count, dmacount); \
1614 	fas_dma_reg_write(fas, &dmar->dma_addr, (fas->f_lastdma = base)); \
1615 	fas_dma_reg_write(fas, &dmar->dma_csr, fas->f_dma_csr); \
1616 }
1617 
1618 static void
FAS_FLUSH_DMA(struct fas * fas)1619 FAS_FLUSH_DMA(struct fas *fas)
1620 {
1621 	fas_dma_reg_write(fas, &fas->f_dma->dma_csr, DMA_RESET);
1622 	fas->f_dma_csr |= (DMA_INTEN|DMA_TWO_CYCLE|DMA_DSBL_PARITY|
1623 	    DMA_DSBL_DRAIN);
1624 	fas->f_dma_csr &= ~(DMA_ENDVMA | DMA_WRITE);
1625 	fas_dma_reg_write(fas, &fas->f_dma->dma_csr, 0);
1626 	fas_dma_reg_write(fas, &fas->f_dma->dma_csr, fas->f_dma_csr);
1627 	fas_dma_reg_write(fas, &fas->f_dma->dma_addr, 0);
1628 }
1629 
1630 /*
1631  * FAS_FLUSH_DMA_HARD checks on REQPEND before taking away the reset
1632  */
1633 static void
FAS_FLUSH_DMA_HARD(struct fas * fas)1634 FAS_FLUSH_DMA_HARD(struct fas *fas)
1635 {
1636 	fas_dma_reg_write(fas, &fas->f_dma->dma_csr, DMA_RESET);
1637 	fas->f_dma_csr |= (DMA_INTEN|DMA_TWO_CYCLE|DMA_DSBL_PARITY|
1638 	    DMA_DSBL_DRAIN);
1639 	fas->f_dma_csr &= ~(DMA_ENDVMA | DMA_WRITE);
1640 	while (fas_dma_reg_read(fas, &fas->f_dma->dma_csr) & DMA_REQPEND)
1641 		;
1642 	fas_dma_reg_write(fas, &fas->f_dma->dma_csr, 0);
1643 	fas_dma_reg_write(fas, &fas->f_dma->dma_csr, fas->f_dma_csr);
1644 	fas_dma_reg_write(fas, &fas->f_dma->dma_addr, 0);
1645 }
1646 
1647 /*
1648  * update period, conf3, offset reg, if necessary
1649  */
1650 #define	FAS_SET_PERIOD_OFFSET_CONF3_REGS(fas, target) \
1651 { \
1652 	uchar_t period, offset, conf3; \
1653 	period = fas->f_sync_period[target] & SYNC_PERIOD_MASK; \
1654 	offset = fas->f_offset[target]; \
1655 	conf3  = fas->f_fasconf3[target]; \
1656 	if ((period != fas->f_period_reg_last) || \
1657 	    (offset != fas->f_offset_reg_last) || \
1658 	    (conf3 != fas->f_fasconf3_reg_last)) { \
1659 		fas->f_period_reg_last = period; \
1660 		fas->f_offset_reg_last = offset; \
1661 		fas->f_fasconf3_reg_last = conf3; \
1662 		fas_reg_write(fas, &fasreg->fas_sync_period, period); \
1663 		fas_reg_write(fas, &fasreg->fas_sync_offset, offset); \
1664 		fas_reg_write(fas, &fasreg->fas_conf3, conf3); \
1665 	} \
1666 }
1667 
1668 /*
1669  * fifo read/write routines
1670  * always read the fifo bytes before reading the interrupt register
1671  */
1672 
1673 static void
fas_read_fifo(struct fas * fas)1674 fas_read_fifo(struct fas *fas)
1675 {
1676 	int stat = fas->f_stat;
1677 	volatile struct fasreg	 *fasreg = fas->f_reg;
1678 	int		 i;
1679 
1680 	i = fas_reg_read(fas, &fasreg->fas_fifo_flag) & FIFO_CNT_MASK;
1681 	EPRINTF2("fas_read_fifo: fifo cnt=%x, stat=%x\n", i, stat);
1682 	ASSERT(i <= FIFOSIZE);
1683 
1684 	fas->f_fifolen = 0;
1685 	while (i-- > 0) {
1686 		fas->f_fifo[fas->f_fifolen++] = fas_reg_read(fas,
1687 		    &fasreg->fas_fifo_data);
1688 		fas->f_fifo[fas->f_fifolen++] = fas_reg_read(fas,
1689 		    &fasreg->fas_fifo_data);
1690 	}
1691 	if (fas->f_stat2 & FAS_STAT2_ISHUTTLE)	{
1692 
1693 		/* write pad byte */
1694 		fas_reg_write(fas, &fasreg->fas_fifo_data, 0);
1695 		fas->f_fifo[fas->f_fifolen++] = fas_reg_read(fas,
1696 		    &fasreg->fas_fifo_data);
1697 		/* flush pad byte */
1698 		fas_reg_cmd_write(fas, CMD_FLUSH);
1699 	}
1700 	EPRINTF2("fas_read_fifo: fifo len=%x, stat2=%x\n",
1701 	    fas->f_fifolen, stat);
1702 } /* fas_read_fifo */
1703 
1704 static void
fas_write_fifo(struct fas * fas,uchar_t * buf,int length,int pad)1705 fas_write_fifo(struct fas *fas, uchar_t *buf, int length, int pad)
1706 {
1707 	int i;
1708 	volatile struct fasreg	 *fasreg = fas->f_reg;
1709 
1710 	EPRINTF1("writing fifo %x bytes\n", length);
1711 	ASSERT(length <= 15);
1712 	fas_reg_cmd_write(fas, CMD_FLUSH);
1713 	for (i = 0; i < length; i++) {
1714 		fas_reg_write(fas, &fasreg->fas_fifo_data, buf[i]);
1715 		if (pad) {
1716 			fas_reg_write(fas, &fasreg->fas_fifo_data, 0);
1717 		}
1718 	}
1719 }
1720 
1721 /*
1722  * Hardware and Software internal reset routines
1723  */
1724 static int
fas_init_chip(struct fas * fas,uchar_t initiator_id)1725 fas_init_chip(struct fas *fas, uchar_t initiator_id)
1726 {
1727 	int		i;
1728 	uchar_t		clock_conv;
1729 	uchar_t		initial_conf3;
1730 	uint_t		ticks;
1731 	static char	*prop_cfreq = "clock-frequency";
1732 
1733 	/*
1734 	 * Determine clock frequency of attached FAS chip.
1735 	 */
1736 	i = ddi_prop_get_int(DDI_DEV_T_ANY,
1737 	    fas->f_dev, DDI_PROP_DONTPASS, prop_cfreq, -1);
1738 	clock_conv = (i + FIVE_MEG - 1) / FIVE_MEG;
1739 	if (clock_conv != CLOCK_40MHZ) {
1740 		fas_log(fas, CE_WARN, "Bad clock frequency");
1741 		return (-1);
1742 	}
1743 
1744 	fas->f_clock_conv = clock_conv;
1745 	fas->f_clock_cycle = CLOCK_PERIOD(i);
1746 	ticks = FAS_CLOCK_TICK(fas);
1747 	fas->f_stval = FAS_CLOCK_TIMEOUT(ticks, fas_selection_timeout);
1748 
1749 	DPRINTF5("%d mhz, clock_conv %d, clock_cycle %d, ticks %d, stval %d\n",
1750 	    i, fas->f_clock_conv, fas->f_clock_cycle,
1751 	    ticks, fas->f_stval);
1752 	/*
1753 	 * set up conf registers
1754 	 */
1755 	fas->f_fasconf |= FAS_CONF_PAREN;
1756 	fas->f_fasconf2 = (uchar_t)(FAS_CONF2_FENABLE | FAS_CONF2_XL32);
1757 
1758 	if (initiator_id < NTARGETS) {
1759 		initial_conf3 = FAS_CONF3_FASTCLK | FAS_CONF3_ODDBYTE_AUTO;
1760 	} else {
1761 		initial_conf3 = FAS_CONF3_FASTCLK | FAS_CONF3_ODDBYTE_AUTO |
1762 		    FAS_CONF3_IDBIT3;
1763 	}
1764 
1765 	for (i = 0; i < NTARGETS_WIDE; i++) {
1766 		fas->f_fasconf3[i] = initial_conf3;
1767 	}
1768 
1769 	/*
1770 	 * Avoid resetting the scsi bus since this causes a few seconds
1771 	 * delay per fas in boot and also causes busy conditions in some
1772 	 * tape devices.
1773 	 */
1774 	fas_internal_reset(fas, FAS_RESET_SOFTC|FAS_RESET_FAS|FAS_RESET_DMA);
1775 
1776 	/*
1777 	 * initialize period and offset for each target
1778 	 */
1779 	for (i = 0; i < NTARGETS_WIDE; i++) {
1780 		if (fas->f_target_scsi_options[i] & SCSI_OPTIONS_SYNC) {
1781 			fas->f_offset[i] = fas_default_offset |
1782 			    fas->f_req_ack_delay;
1783 		} else {
1784 			fas->f_offset[i] = 0;
1785 		}
1786 		if (fas->f_target_scsi_options[i] & SCSI_OPTIONS_FAST) {
1787 			fas->f_neg_period[i] =
1788 			    (uchar_t)MIN_SYNC_PERIOD(fas);
1789 		} else {
1790 			fas->f_neg_period[i] =
1791 			    (uchar_t)CONVERT_PERIOD(DEFAULT_SYNC_PERIOD);
1792 		}
1793 	}
1794 	return (0);
1795 }
1796 
1797 /*
1798  * reset bus, chip, dma, or soft state
1799  */
1800 static void
fas_internal_reset(struct fas * fas,int reset_action)1801 fas_internal_reset(struct fas *fas, int reset_action)
1802 {
1803 	volatile struct fasreg *fasreg = fas->f_reg;
1804 	volatile struct dma *dmar = fas->f_dma;
1805 
1806 	if (reset_action & FAS_RESET_SCSIBUS)	{
1807 		fas_reg_cmd_write(fas, CMD_RESET_SCSI);
1808 		fas_setup_reset_delay(fas);
1809 	}
1810 
1811 	FAS_FLUSH_DMA_HARD(fas); /* resets and reinits the dma */
1812 
1813 	/*
1814 	 * NOTE: if dma is aborted while active, indefinite hangs
1815 	 * may occur; it is preferable to stop the target first before
1816 	 * flushing the dma
1817 	 */
1818 	if (reset_action & FAS_RESET_DMA) {
1819 		int burstsizes = fas->f_dma_attr->dma_attr_burstsizes;
1820 		if (burstsizes & BURST64) {
1821 			IPRINTF("64 byte burstsize\n");
1822 			fas->f_dma_csr |= DMA_BURST64;
1823 		} else if	(burstsizes & BURST32) {
1824 			IPRINTF("32 byte burstsize\n");
1825 			fas->f_dma_csr |= DMA_BURST32;
1826 		} else {
1827 			IPRINTF("16 byte burstsize\n");
1828 		}
1829 		if ((fas->f_hm_rev > 0x20) && (fas_enable_sbus64) &&
1830 		    (ddi_dma_set_sbus64(fas->f_dmahandle, burstsizes) ==
1831 		    DDI_SUCCESS)) {
1832 			IPRINTF("enabled 64 bit sbus\n");
1833 			fas->f_dma_csr |= DMA_WIDE_EN;
1834 		}
1835 	}
1836 
1837 	if (reset_action & FAS_RESET_FAS) {
1838 		/*
1839 		 * 2 NOPs with DMA are required here
1840 		 * id_code is unreliable if we don't do this)
1841 		 */
1842 		uchar_t idcode, fcode;
1843 		int dmarev;
1844 
1845 		fas_reg_cmd_write(fas, CMD_RESET_FAS);
1846 		fas_reg_cmd_write(fas, CMD_NOP | CMD_DMA);
1847 		fas_reg_cmd_write(fas, CMD_NOP | CMD_DMA);
1848 
1849 		/*
1850 		 * Re-load chip configurations
1851 		 * Only load registers which are not loaded in fas_startcmd()
1852 		 */
1853 		fas_reg_write(fas, &fasreg->fas_clock_conv,
1854 		    (fas->f_clock_conv & CLOCK_MASK));
1855 
1856 		fas_reg_write(fas, &fasreg->fas_timeout, fas->f_stval);
1857 
1858 		/*
1859 		 * enable default configurations
1860 		 */
1861 		fas->f_idcode = idcode =
1862 		    fas_reg_read(fas, &fasreg->fas_id_code);
1863 		fcode = (uchar_t)(idcode & FAS_FCODE_MASK) >> (uchar_t)3;
1864 		fas->f_type = FAS366;
1865 		IPRINTF2("Family code %d, revision %d\n",
1866 		    fcode, (idcode & FAS_REV_MASK));
1867 		dmarev = fas_dma_reg_read(fas, &dmar->dma_csr);
1868 		dmarev = (dmarev >> 11) & 0xf;
1869 		IPRINTF1("DMA channel revision %d\n", dmarev);
1870 
1871 		fas_reg_write(fas, &fasreg->fas_conf, fas->f_fasconf);
1872 		fas_reg_write(fas, &fasreg->fas_conf2, fas->f_fasconf2);
1873 
1874 		fas->f_req_ack_delay = DEFAULT_REQ_ACK_DELAY;
1875 
1876 		/*
1877 		 * Just in case... clear interrupt
1878 		 */
1879 		(void) fas_reg_read(fas, &fasreg->fas_intr);
1880 	}
1881 
1882 	if (reset_action & FAS_RESET_SOFTC) {
1883 		fas->f_wdtr_sent = fas->f_sdtr_sent = 0;
1884 		fas->f_wide_known = fas->f_sync_known = 0;
1885 		fas->f_wide_enabled = fas->f_sync_enabled = 0;
1886 		fas->f_omsglen = 0;
1887 		fas->f_cur_msgout[0] = fas->f_last_msgout =
1888 		    fas->f_last_msgin = INVALID_MSG;
1889 		fas->f_abort_msg_sent = fas->f_reset_msg_sent = 0;
1890 		fas->f_next_slot = 0;
1891 		fas->f_current_sp = NULL;
1892 		fas->f_fifolen = 0;
1893 		fas->f_fasconf3_reg_last = fas->f_offset_reg_last =
1894 		    fas->f_period_reg_last = 0xff;
1895 
1896 		New_state(fas, STATE_FREE);
1897 	}
1898 }
1899 
1900 
1901 #ifdef FASDEBUG
1902 /*
1903  * check if ncmds still reflects the truth
1904  * count all cmds for this driver instance and compare with ncmds
1905  */
1906 static void
fas_check_ncmds(struct fas * fas)1907 fas_check_ncmds(struct fas *fas)
1908 {
1909 	int slot = 0;
1910 	ushort_t tag, t;
1911 	int n, total = 0;
1912 
1913 	do {
1914 		if (fas->f_active[slot]) {
1915 			struct fas_cmd *sp = fas->f_readyf[slot];
1916 			t = fas->f_active[slot]->f_n_slots;
1917 			while (sp != 0) {
1918 				sp = sp->cmd_forw;
1919 				total++;
1920 			}
1921 			for (n = tag = 0; tag < t; tag++) {
1922 				if (fas->f_active[slot]->f_slot[tag] != 0) {
1923 					n++;
1924 					total++;
1925 				}
1926 			}
1927 			ASSERT(n == fas->f_tcmds[slot]);
1928 		}
1929 		slot = NEXTSLOT(slot, fas->f_dslot);
1930 	} while (slot != 0);
1931 
1932 	if (total != fas->f_ncmds) {
1933 		IPRINTF2("fas_check_ncmds: total=%x, ncmds=%x\n",
1934 		    total, fas->f_ncmds);
1935 	}
1936 	ASSERT(fas->f_ncmds >= fas->f_ndisc);
1937 }
1938 #else
1939 #define	fas_check_ncmds(fas)
1940 #endif
1941 
1942 /*
1943  * SCSA Interface functions
1944  *
1945  * Visible to the external world via the transport structure.
1946  *
1947  * fas_scsi_abort: abort a current cmd or all cmds for a target
1948  */
1949 /*ARGSUSED*/
1950 static int
fas_scsi_abort(struct scsi_address * ap,struct scsi_pkt * pkt)1951 fas_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
1952 {
1953 	struct fas *fas = ADDR2FAS(ap);
1954 	int rval;
1955 
1956 	IPRINTF2("fas_scsi_abort: target %d.%d\n", ap->a_target, ap->a_lun);
1957 
1958 	mutex_enter(FAS_MUTEX(fas));
1959 	rval =	fas_do_scsi_abort(ap, pkt);
1960 	fas_check_waitQ_and_mutex_exit(fas);
1961 	return (rval);
1962 }
1963 
1964 /*
1965  * reset handling: reset bus or target
1966  */
1967 /*ARGSUSED*/
1968 static int
fas_scsi_reset(struct scsi_address * ap,int level)1969 fas_scsi_reset(struct scsi_address *ap, int level)
1970 {
1971 	struct fas *fas = ADDR2FAS(ap);
1972 	int rval;
1973 
1974 	IPRINTF3("fas_scsi_reset: target %d.%d, level %d\n",
1975 	    ap->a_target, ap->a_lun, level);
1976 
1977 	mutex_enter(FAS_MUTEX(fas));
1978 	rval = fas_do_scsi_reset(ap, level);
1979 	fas_check_waitQ_and_mutex_exit(fas);
1980 	return (rval);
1981 }
1982 
1983 /*
1984  * entry point for reset notification setup, to register or to cancel.
1985  */
1986 static int
fas_scsi_reset_notify(struct scsi_address * ap,int flag,void (* callback)(caddr_t),caddr_t arg)1987 fas_scsi_reset_notify(struct scsi_address *ap, int flag,
1988     void (*callback)(caddr_t), caddr_t arg)
1989 {
1990 	struct fas	*fas = ADDR2FAS(ap);
1991 
1992 	return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
1993 	    &fas->f_mutex, &fas->f_reset_notify_listf));
1994 }
1995 
1996 /*
1997  * capability interface
1998  */
1999 /*ARGSUSED*/
2000 static int
fas_scsi_getcap(struct scsi_address * ap,char * cap,int whom)2001 fas_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
2002 {
2003 	struct fas *fas = ADDR2FAS(ap);
2004 	DPRINTF3("fas_scsi_getcap: tgt=%x, cap=%s, whom=%x\n",
2005 	    ap->a_target, cap, whom);
2006 	return (fas_commoncap(ap, cap, 0, whom, 0));
2007 }
2008 
2009 /*ARGSUSED*/
2010 static int
fas_scsi_setcap(struct scsi_address * ap,char * cap,int value,int whom)2011 fas_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
2012 {
2013 	struct fas *fas = ADDR2FAS(ap);
2014 	IPRINTF4("fas_scsi_setcap: tgt=%x, cap=%s, value=%x, whom=%x\n",
2015 	    ap->a_target, cap, value, whom);
2016 	return (fas_commoncap(ap, cap, value, whom, 1));
2017 }
2018 
2019 /*
2020  * pkt and dma allocation and deallocation
2021  */
2022 /*ARGSUSED*/
2023 static void
fas_scsi_dmafree(struct scsi_address * ap,struct scsi_pkt * pkt)2024 fas_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
2025 {
2026 	struct fas_cmd *cmd = PKT2CMD(pkt);
2027 
2028 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_DMAFREE_START,
2029 	    "fas_scsi_dmafree_start");
2030 
2031 	if (cmd->cmd_flags & CFLAG_DMAVALID) {
2032 		/*
2033 		 * Free the mapping.
2034 		 */
2035 		(void) ddi_dma_unbind_handle(cmd->cmd_dmahandle);
2036 		cmd->cmd_flags ^= CFLAG_DMAVALID;
2037 	}
2038 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_DMAFREE_END,
2039 	    "fas_scsi_dmafree_end");
2040 }
2041 
2042 /*ARGSUSED*/
2043 static void
fas_scsi_sync_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)2044 fas_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2045 {
2046 	struct fas_cmd *sp = PKT2CMD(pkt);
2047 
2048 	if (sp->cmd_flags & CFLAG_DMAVALID) {
2049 		if (ddi_dma_sync(sp->cmd_dmahandle, 0, 0,
2050 		    (sp->cmd_flags & CFLAG_DMASEND) ?
2051 		    DDI_DMA_SYNC_FORDEV : DDI_DMA_SYNC_FORCPU) !=
2052 		    DDI_SUCCESS) {
2053 			fas_log(ADDR2FAS(ap), CE_WARN,
2054 			    "sync of pkt (%p) failed", (void *)pkt);
2055 		}
2056 	}
2057 }
2058 
2059 /*
2060  * initialize pkt and allocate DVMA resources
2061  */
2062 static struct scsi_pkt *
fas_scsi_init_pkt(struct scsi_address * ap,struct scsi_pkt * pkt,struct buf * bp,int cmdlen,int statuslen,int tgtlen,int flags,int (* callback)(),caddr_t arg)2063 fas_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
2064 	struct buf *bp, int cmdlen, int statuslen, int tgtlen,
2065 	int flags, int (*callback)(), caddr_t arg)
2066 {
2067 	int kf;
2068 	int failure = 1;
2069 	struct fas_cmd *cmd;
2070 	struct fas *fas = ADDR2FAS(ap);
2071 	struct fas_cmd *new_cmd;
2072 	int rval;
2073 
2074 /* #define	FAS_TEST_EXTRN_ALLOC */
2075 #ifdef FAS_TEST_EXTRN_ALLOC
2076 	cmdlen *= 4; statuslen *= 4; tgtlen *= 4;
2077 #endif
2078 	/*
2079 	 * if no pkt was passed then allocate a pkt first
2080 	 */
2081 	if (pkt == NULL) {
2082 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_PKTALLOC_START,
2083 		    "fas_scsi_impl_pktalloc_start");
2084 
2085 		kf = (callback == SLEEP_FUNC)? KM_SLEEP: KM_NOSLEEP;
2086 
2087 		/*
2088 		 * only one size of pkt (with arq).
2089 		 */
2090 		cmd = kmem_cache_alloc(fas->f_kmem_cache, kf);
2091 
2092 		if (cmd) {
2093 
2094 			ddi_dma_handle_t	save_dma_handle;
2095 
2096 			save_dma_handle = cmd->cmd_dmahandle;
2097 			bzero(cmd, EXTCMD_SIZE);
2098 			cmd->cmd_dmahandle = save_dma_handle;
2099 
2100 			pkt = (struct scsi_pkt *)((uchar_t *)cmd +
2101 			    sizeof (struct fas_cmd));
2102 			cmd->cmd_pkt		= pkt;
2103 			pkt->pkt_ha_private	= (opaque_t)cmd;
2104 			pkt->pkt_scbp	= (opaque_t)&cmd->cmd_scb;
2105 			pkt->pkt_cdbp	= (opaque_t)&cmd->cmd_cdb;
2106 			pkt->pkt_address	= *ap;
2107 
2108 			pkt->pkt_cdbp = (opaque_t)&cmd->cmd_cdb;
2109 			pkt->pkt_private = cmd->cmd_pkt_private;
2110 
2111 			cmd->cmd_cdblen 	= cmdlen;
2112 			cmd->cmd_scblen 	= statuslen;
2113 			cmd->cmd_privlen	= tgtlen;
2114 			cmd->cmd_slot		=
2115 			    (Tgt(cmd) * NLUNS_PER_TARGET) | Lun(cmd);
2116 			failure = 0;
2117 		}
2118 		if (failure || (cmdlen > sizeof (cmd->cmd_cdb)) ||
2119 		    (tgtlen > PKT_PRIV_LEN) ||
2120 		    (statuslen > EXTCMDS_STATUS_SIZE)) {
2121 			if (failure == 0) {
2122 				/*
2123 				 * if extern alloc fails, all will be
2124 				 * deallocated, including cmd
2125 				 */
2126 				failure = fas_pkt_alloc_extern(fas, cmd,
2127 				    cmdlen, tgtlen, statuslen, kf);
2128 			}
2129 			if (failure) {
2130 				/*
2131 				 * nothing to deallocate so just return
2132 				 */
2133 				TRACE_0(TR_FAC_SCSI_FAS,
2134 				    TR_FAS_SCSI_IMPL_PKTALLOC_END,
2135 				    "fas_scsi_impl_pktalloc_end");
2136 				return (NULL);
2137 			}
2138 		}
2139 
2140 		new_cmd = cmd;
2141 
2142 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_PKTALLOC_END,
2143 		    "fas_scsi_impl_pktalloc_end");
2144 	} else {
2145 		cmd = PKT2CMD(pkt);
2146 		new_cmd = NULL;
2147 	}
2148 
2149 	/*
2150 	 * Second step of fas_scsi_init_pkt:
2151 	 * bind the buf to the handle
2152 	 */
2153 	if (bp && bp->b_bcount != 0 &&
2154 	    (cmd->cmd_flags & CFLAG_DMAVALID) == 0) {
2155 
2156 		int cmd_flags, dma_flags;
2157 		uint_t dmacookie_count;
2158 
2159 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_DMAGET_START,
2160 		    "fas_scsi_impl_dmaget_start");
2161 
2162 		cmd_flags = cmd->cmd_flags;
2163 
2164 		if (bp->b_flags & B_READ) {
2165 			cmd_flags &= ~CFLAG_DMASEND;
2166 			dma_flags = DDI_DMA_READ | DDI_DMA_PARTIAL;
2167 		} else {
2168 			cmd_flags |= CFLAG_DMASEND;
2169 			dma_flags = DDI_DMA_WRITE | DDI_DMA_PARTIAL;
2170 		}
2171 		if (flags & PKT_CONSISTENT) {
2172 			cmd_flags |= CFLAG_CMDIOPB;
2173 			dma_flags |= DDI_DMA_CONSISTENT;
2174 		}
2175 
2176 		/*
2177 		 * bind the handle to the buf
2178 		 */
2179 		ASSERT(cmd->cmd_dmahandle != NULL);
2180 		rval = ddi_dma_buf_bind_handle(cmd->cmd_dmahandle, bp,
2181 		    dma_flags, callback, arg, &cmd->cmd_dmacookie,
2182 		    &dmacookie_count);
2183 
2184 		if (rval && rval != DDI_DMA_PARTIAL_MAP) {
2185 			switch (rval) {
2186 			case DDI_DMA_NORESOURCES:
2187 				bioerror(bp, 0);
2188 				break;
2189 			case DDI_DMA_BADATTR:
2190 			case DDI_DMA_NOMAPPING:
2191 				bioerror(bp, EFAULT);
2192 				break;
2193 			case DDI_DMA_TOOBIG:
2194 			default:
2195 				bioerror(bp, EINVAL);
2196 				break;
2197 			}
2198 			cmd->cmd_flags = cmd_flags & ~CFLAG_DMAVALID;
2199 			if (new_cmd) {
2200 				fas_scsi_destroy_pkt(ap, pkt);
2201 			}
2202 			TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_DMAGET_END,
2203 			    "fas_scsi_impl_dmaget_end");
2204 			return ((struct scsi_pkt *)NULL);
2205 		}
2206 		ASSERT(dmacookie_count == 1);
2207 		cmd->cmd_dmacount = bp->b_bcount;
2208 		cmd->cmd_flags = cmd_flags | CFLAG_DMAVALID;
2209 
2210 		ASSERT(cmd->cmd_dmahandle != NULL);
2211 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_DMAGET_END,
2212 		    "fas_scsi_impl_dmaget_end");
2213 	}
2214 
2215 	return (pkt);
2216 }
2217 
2218 /*
2219  * unbind dma resources and deallocate the pkt
2220  */
2221 static void
fas_scsi_destroy_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)2222 fas_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
2223 {
2224 	struct fas_cmd *sp = PKT2CMD(pkt);
2225 	struct fas *fas = ADDR2FAS(ap);
2226 
2227 	/*
2228 	 * fas_scsi_impl_dmafree inline to speed things up
2229 	 */
2230 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_DMAFREE_START,
2231 	    "fas_scsi_impl_dmafree_start");
2232 
2233 	if (sp->cmd_flags & CFLAG_DMAVALID) {
2234 		/*
2235 		 * Free the mapping.
2236 		 */
2237 		(void) ddi_dma_unbind_handle(sp->cmd_dmahandle);
2238 		sp->cmd_flags ^= CFLAG_DMAVALID;
2239 	}
2240 
2241 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_DMAFREE_END,
2242 	    "fas_scsi_impl_dmafree_end");
2243 
2244 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_PKTFREE_START,
2245 	    "fas_scsi_impl_pktfree_start");
2246 
2247 	if ((sp->cmd_flags &
2248 	    (CFLAG_FREE | CFLAG_CDBEXTERN | CFLAG_PRIVEXTERN |
2249 	    CFLAG_SCBEXTERN)) == 0) {
2250 		sp->cmd_flags = CFLAG_FREE;
2251 		kmem_cache_free(fas->f_kmem_cache, (void *)sp);
2252 	} else {
2253 		fas_pkt_destroy_extern(fas, sp);
2254 	}
2255 
2256 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_SCSI_IMPL_PKTFREE_END,
2257 	    "fas_scsi_impl_pktfree_end");
2258 }
2259 
2260 /*
2261  * allocate and deallocate external pkt space (ie. not part of fas_cmd) for
2262  * non-standard length cdb, pkt_private, status areas
2263  * if allocation fails, then deallocate all external space and the pkt
2264  */
2265 /* ARGSUSED */
2266 static int
fas_pkt_alloc_extern(struct fas * fas,struct fas_cmd * sp,int cmdlen,int tgtlen,int statuslen,int kf)2267 fas_pkt_alloc_extern(struct fas *fas, struct fas_cmd *sp,
2268     int cmdlen, int tgtlen, int statuslen, int kf)
2269 {
2270 	caddr_t cdbp, scbp, tgt;
2271 	int failure = 0;
2272 
2273 	tgt = cdbp = scbp = NULL;
2274 	if (cmdlen > sizeof (sp->cmd_cdb)) {
2275 		if ((cdbp = kmem_zalloc((size_t)cmdlen, kf)) == NULL) {
2276 			failure++;
2277 		} else {
2278 			sp->cmd_pkt->pkt_cdbp = (opaque_t)cdbp;
2279 			sp->cmd_flags |= CFLAG_CDBEXTERN;
2280 		}
2281 	}
2282 	if (tgtlen > PKT_PRIV_LEN) {
2283 		if ((tgt = kmem_zalloc(tgtlen, kf)) == NULL) {
2284 			failure++;
2285 		} else {
2286 			sp->cmd_flags |= CFLAG_PRIVEXTERN;
2287 			sp->cmd_pkt->pkt_private = tgt;
2288 		}
2289 	}
2290 	if (statuslen > EXTCMDS_STATUS_SIZE) {
2291 		if ((scbp = kmem_zalloc((size_t)statuslen, kf)) == NULL) {
2292 			failure++;
2293 		} else {
2294 			sp->cmd_flags |= CFLAG_SCBEXTERN;
2295 			sp->cmd_pkt->pkt_scbp = (opaque_t)scbp;
2296 		}
2297 	}
2298 	if (failure) {
2299 		fas_pkt_destroy_extern(fas, sp);
2300 	}
2301 	return (failure);
2302 }
2303 
2304 /*
2305  * deallocate external pkt space and deallocate the pkt
2306  */
2307 static void
fas_pkt_destroy_extern(struct fas * fas,struct fas_cmd * sp)2308 fas_pkt_destroy_extern(struct fas *fas, struct fas_cmd *sp)
2309 {
2310 	if (sp->cmd_flags & CFLAG_FREE) {
2311 		panic("fas_pkt_destroy_extern: freeing free packet");
2312 		_NOTE(NOT_REACHED)
2313 		/* NOTREACHED */
2314 	}
2315 	if (sp->cmd_flags & CFLAG_CDBEXTERN) {
2316 		kmem_free((caddr_t)sp->cmd_pkt->pkt_cdbp,
2317 		    (size_t)sp->cmd_cdblen);
2318 	}
2319 	if (sp->cmd_flags & CFLAG_SCBEXTERN) {
2320 		kmem_free((caddr_t)sp->cmd_pkt->pkt_scbp,
2321 		    (size_t)sp->cmd_scblen);
2322 	}
2323 	if (sp->cmd_flags & CFLAG_PRIVEXTERN) {
2324 		kmem_free((caddr_t)sp->cmd_pkt->pkt_private,
2325 		    (size_t)sp->cmd_privlen);
2326 	}
2327 	sp->cmd_flags = CFLAG_FREE;
2328 	kmem_cache_free(fas->f_kmem_cache, (void *)sp);
2329 }
2330 
2331 /*
2332  * kmem cache constructor and destructor:
2333  * When constructing, we bzero the cmd and allocate the dma handle
2334  * When destructing, just free the dma handle
2335  */
2336 static int
fas_kmem_cache_constructor(void * buf,void * cdrarg,int kmflags)2337 fas_kmem_cache_constructor(void	*buf, void *cdrarg, int kmflags)
2338 {
2339 	struct fas_cmd *cmd = buf;
2340 	struct fas *fas = cdrarg;
2341 	int  (*callback)(caddr_t) = (kmflags == KM_SLEEP) ? DDI_DMA_SLEEP:
2342 	    DDI_DMA_DONTWAIT;
2343 
2344 	bzero(buf, EXTCMD_SIZE);
2345 
2346 	/*
2347 	 * allocate a dma handle
2348 	 */
2349 	if ((ddi_dma_alloc_handle(fas->f_dev, fas->f_dma_attr, callback,
2350 	    NULL, &cmd->cmd_dmahandle)) != DDI_SUCCESS) {
2351 		return (-1);
2352 	}
2353 	return (0);
2354 }
2355 
2356 /*ARGSUSED*/
2357 static void
fas_kmem_cache_destructor(void * buf,void * cdrarg)2358 fas_kmem_cache_destructor(void *buf, void *cdrarg)
2359 {
2360 	struct fas_cmd *cmd = buf;
2361 	if (cmd->cmd_dmahandle) {
2362 		ddi_dma_free_handle(&cmd->cmd_dmahandle);
2363 	}
2364 }
2365 
2366 /*
2367  * fas_scsi_start - Accept commands for transport
2368  */
2369 static int
fas_scsi_start(struct scsi_address * ap,struct scsi_pkt * pkt)2370 fas_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
2371 {
2372 	struct fas_cmd *sp = PKT2CMD(pkt);
2373 	struct fas *fas = ADDR2FAS(ap);
2374 	int rval;
2375 	int intr = 0;
2376 
2377 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_START_START, "fas_scsi_start_start");
2378 
2379 #ifdef FAS_TEST
2380 	if (fas_transport_busy > 0) {
2381 		fas_transport_busy--;
2382 		return (TRAN_BUSY);
2383 	}
2384 	if ((fas_transport_busy_rqs > 0) &&
2385 	    (*(sp->cmd_pkt->pkt_cdbp) == SCMD_REQUEST_SENSE)) {
2386 		fas_transport_busy_rqs--;
2387 		return (TRAN_BUSY);
2388 	}
2389 	if (fas_transport_reject > 0) {
2390 		fas_transport_reject--;
2391 		return (TRAN_BADPKT);
2392 	}
2393 #endif
2394 	/*
2395 	 * prepare packet before taking the mutex
2396 	 */
2397 	rval = fas_prepare_pkt(fas, sp);
2398 	if (rval != TRAN_ACCEPT) {
2399 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_START_PREPARE_PKT_END,
2400 		    "fas_scsi_start_end (prepare_pkt)");
2401 		return (rval);
2402 	}
2403 
2404 	/*
2405 	 * fas mutex can be held for a long time; therefore, if the mutex is
2406 	 * held, we queue the packet in a waitQ; we now should check
2407 	 * the waitQ on every mutex_exit(FAS_MUTEX(fas)) but we really only
2408 	 * need to do this when the bus is free
2409 	 * don't put NOINTR cmds including proxy cmds in waitQ! These
2410 	 * cmds are handled by fas_runpoll()
2411 	 * if the waitQ is non-empty, queue the pkt anyway to preserve
2412 	 * order
2413 	 * the goal is to queue in waitQ as much as possible so at
2414 	 * interrupt time, we can move the packets to readyQ or start
2415 	 * a packet immediately. It helps to do this at interrupt
2416 	 * time because we can then field more interrupts
2417 	 */
2418 	if ((sp->cmd_pkt_flags & FLAG_NOINTR) == 0) {
2419 
2420 		/*
2421 		 * if the bus is not free, we will get an interrupt shortly
2422 		 * so we don't want to take the fas mutex but queue up
2423 		 * the packet in the waitQ
2424 		 * also, if the waitQ is non-empty or there is an interrupt
2425 		 * pending then queue up the packet in the waitQ and let the
2426 		 * interrupt handler empty the waitQ
2427 		 */
2428 		mutex_enter(&fas->f_waitQ_mutex);
2429 
2430 		if ((fas->f_state != STATE_FREE) ||
2431 		    fas->f_waitf || (intr = INTPENDING(fas))) {
2432 			goto queue_in_waitQ;
2433 		}
2434 
2435 		/*
2436 		 * we didn't queue up in the waitQ, so now try to accept
2437 		 * the packet. if we fail to get the fas mutex, go back to
2438 		 * the waitQ again
2439 		 * do not release the waitQ mutex yet because that
2440 		 * leaves a window where the interrupt handler has
2441 		 * emptied the waitQ but not released the fas mutex yet
2442 		 *
2443 		 * the interrupt handler gets the locks in opposite order
2444 		 * but because we do a tryenter, there is no deadlock
2445 		 *
2446 		 * if another thread has the fas mutex then either this
2447 		 * thread or the other may find the bus free and
2448 		 * empty the waitQ
2449 		 */
2450 		if (mutex_tryenter(FAS_MUTEX(fas))) {
2451 			mutex_exit(&fas->f_waitQ_mutex);
2452 			rval = fas_accept_pkt(fas, sp, TRAN_BUSY_OK);
2453 		} else {
2454 			/*
2455 			 * we didn't get the fas mutex so
2456 			 * the packet has to go in the waitQ now
2457 			 */
2458 			goto queue_in_waitQ;
2459 		}
2460 	} else {
2461 		/*
2462 		 * for polled cmds, we have to take the mutex and
2463 		 * start the packet using fas_runpoll()
2464 		 */
2465 		mutex_enter(FAS_MUTEX(fas));
2466 		rval = fas_accept_pkt(fas, sp, TRAN_BUSY_OK);
2467 	}
2468 
2469 	/*
2470 	 * if the bus is free then empty waitQ and release the mutex
2471 	 * (this should be unlikely that the bus is still free after
2472 	 * accepting the packet. it may be the relatively unusual case
2473 	 * that we are throttling)
2474 	 */
2475 	if (fas->f_state == STATE_FREE) {
2476 		FAS_CHECK_WAITQ_AND_FAS_MUTEX_EXIT(fas);
2477 	} else {
2478 		mutex_exit(FAS_MUTEX(fas));
2479 	}
2480 
2481 done:
2482 	TRACE_1(TR_FAC_SCSI_FAS, TR_FAS_START_END,
2483 	    "fas_scsi_start_end: fas 0x%p", fas);
2484 	return (rval);
2485 
2486 queue_in_waitQ:
2487 	if (fas->f_waitf == NULL) {
2488 		fas->f_waitb = fas->f_waitf = sp;
2489 		sp->cmd_forw = NULL;
2490 	} else {
2491 		struct fas_cmd *dp = fas->f_waitb;
2492 		dp->cmd_forw = fas->f_waitb = sp;
2493 		sp->cmd_forw = NULL;
2494 	}
2495 
2496 	/*
2497 	 * check again the fas mutex
2498 	 * if there was an interrupt then the interrupt
2499 	 * handler will eventually empty the waitQ
2500 	 */
2501 	if ((intr == 0) && (fas->f_state == STATE_FREE) &&
2502 	    mutex_tryenter(FAS_MUTEX(fas))) {
2503 		/*
2504 		 * double check if the bus is still free
2505 		 * (this actually reduced mutex contention a bit)
2506 		 */
2507 		if (fas->f_state == STATE_FREE) {
2508 			fas_empty_waitQ(fas);
2509 		}
2510 		mutex_exit(FAS_MUTEX(fas));
2511 	}
2512 	mutex_exit(&fas->f_waitQ_mutex);
2513 
2514 	TRACE_1(TR_FAC_SCSI_FAS, TR_FAS_START_END,
2515 	    "fas_scsi_start_end: fas 0x%p", fas);
2516 	return (rval);
2517 }
2518 
2519 /*
2520  * prepare the pkt:
2521  * the pkt may have been resubmitted or just reused so
2522  * initialize some fields, reset the dma window, and do some checks
2523  */
2524 static int
fas_prepare_pkt(struct fas * fas,struct fas_cmd * sp)2525 fas_prepare_pkt(struct fas *fas, struct fas_cmd *sp)
2526 {
2527 	struct scsi_pkt *pkt = CMD2PKT(sp);
2528 
2529 	/*
2530 	 * Reinitialize some fields that need it; the packet may
2531 	 * have been resubmitted
2532 	 */
2533 	pkt->pkt_reason = CMD_CMPLT;
2534 	pkt->pkt_state	= 0;
2535 	pkt->pkt_statistics = 0;
2536 	pkt->pkt_resid	= 0;
2537 	sp->cmd_age	= 0;
2538 	sp->cmd_pkt_flags = pkt->pkt_flags;
2539 
2540 	/*
2541 	 * Copy the cdb pointer to the pkt wrapper area as we
2542 	 * might modify this pointer. Zero status byte
2543 	 */
2544 	sp->cmd_cdbp = pkt->pkt_cdbp;
2545 	*(pkt->pkt_scbp) = 0;
2546 
2547 	if (sp->cmd_flags & CFLAG_DMAVALID) {
2548 		pkt->pkt_resid	= sp->cmd_dmacount;
2549 
2550 		/*
2551 		 * if the pkt was resubmitted then the
2552 		 * windows may be at the wrong number
2553 		 */
2554 		if (sp->cmd_cur_win) {
2555 			sp->cmd_cur_win = 0;
2556 			if (fas_set_new_window(fas, sp)) {
2557 				IPRINTF("cannot reset window\n");
2558 				return (TRAN_BADPKT);
2559 			}
2560 		}
2561 		sp->cmd_saved_cur_addr =
2562 		    sp->cmd_cur_addr = sp->cmd_dmacookie.dmac_address;
2563 
2564 		/*
2565 		 * the common case is just one window, we worry
2566 		 * about multiple windows when we run out of the
2567 		 * current window
2568 		 */
2569 		sp->cmd_nwin = sp->cmd_saved_win = 0;
2570 		sp->cmd_data_count = sp->cmd_saved_data_count = 0;
2571 
2572 		/*
2573 		 * consistent packets need to be sync'ed first
2574 		 * (only for data going out)
2575 		 */
2576 		if ((sp->cmd_flags & (CFLAG_CMDIOPB | CFLAG_DMASEND)) ==
2577 		    (CFLAG_CMDIOPB | CFLAG_DMASEND)) {
2578 			(void) ddi_dma_sync(sp->cmd_dmahandle,	0, (uint_t)0,
2579 			    DDI_DMA_SYNC_FORDEV);
2580 		}
2581 	}
2582 
2583 	sp->cmd_actual_cdblen = sp->cmd_cdblen;
2584 
2585 #ifdef FAS_TEST
2586 #ifndef __lock_lint
2587 	if (fas_test_untagged > 0) {
2588 		if (TAGGED(Tgt(sp))) {
2589 			int slot = sp->cmd_slot;
2590 			sp->cmd_pkt_flags &= ~FLAG_TAGMASK;
2591 			sp->cmd_pkt_flags &= ~FLAG_NODISCON;
2592 			sp->cmd_pkt_flags |= 0x80000000;
2593 			fas_log(fas, CE_NOTE,
2594 			    "starting untagged cmd, target=%d,"
2595 			    " tcmds=%d, sp=0x%p, throttle=%d\n",
2596 			    Tgt(sp), fas->f_tcmds[slot], (void *)sp,
2597 			    fas->f_throttle[slot]);
2598 			fas_test_untagged = -10;
2599 		}
2600 	}
2601 #endif
2602 #endif
2603 
2604 #ifdef FASDEBUG
2605 	if (NOTAG(Tgt(sp)) && (pkt->pkt_flags & FLAG_TAGMASK)) {
2606 		IPRINTF2("tagged packet for non-tagged target %d.%d\n",
2607 		    Tgt(sp), Lun(sp));
2608 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_PREPARE_PKT_TRAN_BADPKT_END,
2609 		    "fas_prepare_pkt_end (tran_badpkt)");
2610 		return (TRAN_BADPKT);
2611 	}
2612 
2613 	/*
2614 	 * the SCSA spec states that it is an error to have no
2615 	 * completion function when FLAG_NOINTR is not set
2616 	 */
2617 	if ((pkt->pkt_comp == NULL) &&
2618 	    ((pkt->pkt_flags & FLAG_NOINTR) == 0)) {
2619 		IPRINTF("intr packet with pkt_comp == 0\n");
2620 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_PREPARE_PKT_TRAN_BADPKT_END,
2621 		    "fas_prepare_pkt_end (tran_badpkt)");
2622 		return (TRAN_BADPKT);
2623 	}
2624 #endif /* FASDEBUG */
2625 
2626 	if ((fas->f_target_scsi_options[Tgt(sp)] & SCSI_OPTIONS_DR) == 0) {
2627 		/*
2628 		 * no need to reset tag bits since tag queueing will
2629 		 * not be enabled if disconnects are disabled
2630 		 */
2631 		sp->cmd_pkt_flags |= FLAG_NODISCON;
2632 	}
2633 
2634 	sp->cmd_flags = (sp->cmd_flags & ~CFLAG_TRANFLAG) |
2635 	    CFLAG_PREPARED | CFLAG_IN_TRANSPORT;
2636 
2637 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_PREPARE_PKT_TRAN_ACCEPT_END,
2638 	    "fas_prepare_pkt_end (tran_accept)");
2639 	return (TRAN_ACCEPT);
2640 }
2641 
2642 /*
2643  * emptying the waitQ just before releasing FAS_MUTEX is a bit
2644  * tricky; if we release the waitQ mutex and then the FAS_MUTEX,
2645  * another thread could queue a cmd in the waitQ, just before
2646  * the FAS_MUTEX is released. This cmd is then stuck in the waitQ unless
2647  * another cmd comes in or fas_intr() or fas_watch() checks the waitQ.
2648  * Therefore, by releasing the FAS_MUTEX before releasing the waitQ mutex,
2649  * we prevent fas_scsi_start() filling the waitQ
2650  *
2651  * By setting NO_TRAN_BUSY, we force fas_accept_pkt() to queue up
2652  * the waitQ pkts in the readyQ.
2653  * If a QFull condition occurs, the target driver may set its throttle
2654  * too high because of the requests queued up in the readyQ but this
2655  * is not a big problem. The throttle should be periodically reset anyway.
2656  */
2657 static void
fas_empty_waitQ(struct fas * fas)2658 fas_empty_waitQ(struct fas *fas)
2659 {
2660 	struct fas_cmd *sp;
2661 	int rval;
2662 	struct fas_cmd *waitf, *waitb;
2663 
2664 	ASSERT(mutex_owned(&fas->f_waitQ_mutex));
2665 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_EMPTY_WAITQ_START,
2666 	    "fas_empty_waitQ_start");
2667 
2668 	while (fas->f_waitf) {
2669 
2670 		/* copy waitQ, zero the waitQ and release the mutex */
2671 		waitf = fas->f_waitf;
2672 		waitb = fas->f_waitb;
2673 		fas->f_waitf = fas->f_waitb = NULL;
2674 		mutex_exit(&fas->f_waitQ_mutex);
2675 
2676 		do {
2677 			sp = waitf;
2678 			waitf = sp->cmd_forw;
2679 			if (waitb == sp)	{
2680 				waitb = NULL;
2681 			}
2682 
2683 			rval = fas_accept_pkt(fas, sp, NO_TRAN_BUSY);
2684 
2685 			/*
2686 			 * If the  packet was rejected for other reasons then
2687 			 * complete it here
2688 			 */
2689 			if (rval != TRAN_ACCEPT) {
2690 				ASSERT(rval != TRAN_BUSY);
2691 				fas_set_pkt_reason(fas, sp, CMD_TRAN_ERR, 0);
2692 				if (sp->cmd_pkt->pkt_comp) {
2693 					sp->cmd_flags |= CFLAG_FINISHED;
2694 					fas_call_pkt_comp(fas, sp);
2695 				}
2696 			}
2697 
2698 			if (INTPENDING(fas)) {
2699 				/*
2700 				 * stop processing the waitQ and put back
2701 				 * the remaining packets on the waitQ
2702 				 */
2703 				mutex_enter(&fas->f_waitQ_mutex);
2704 				if (waitf) {
2705 					ASSERT(waitb != NULL);
2706 					waitb->cmd_forw = fas->f_waitf;
2707 					fas->f_waitf = waitf;
2708 					if (fas->f_waitb == NULL) {
2709 						fas->f_waitb = waitb;
2710 					}
2711 				}
2712 				return;
2713 			}
2714 		} while (waitf);
2715 
2716 		mutex_enter(&fas->f_waitQ_mutex);
2717 	}
2718 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_EMPTY_WAITQ_END,
2719 	    "fas_empty_waitQ_end");
2720 }
2721 
2722 static void
fas_move_waitQ_to_readyQ(struct fas * fas)2723 fas_move_waitQ_to_readyQ(struct fas *fas)
2724 {
2725 	/*
2726 	 * this may actually start cmds but it is most likely
2727 	 * that if waitQ is not empty that the bus is not free
2728 	 */
2729 	ASSERT(mutex_owned(FAS_MUTEX(fas)));
2730 	mutex_enter(&fas->f_waitQ_mutex);
2731 	fas_empty_waitQ(fas);
2732 	mutex_exit(&fas->f_waitQ_mutex);
2733 }
2734 
2735 
2736 /*
2737  * function wrapper for two frequently used macros. for the non-critical
2738  * path we use the function
2739  */
2740 static void
fas_check_waitQ_and_mutex_exit(struct fas * fas)2741 fas_check_waitQ_and_mutex_exit(struct fas *fas)
2742 {
2743 	_NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(fas->f_mutex))
2744 	FAS_CHECK_WAITQ_AND_FAS_MUTEX_EXIT(fas);
2745 	FAS_EMPTY_CALLBACKQ(fas);
2746 }
2747 
2748 /*
2749  * fas_accept_pkt():
2750  * the flag argument is to force fas_accept_pkt to accept the pkt;
2751  * the caller cannot take the pkt back and it has to be queued up in
2752  * the readyQ
2753  */
2754 static int
fas_accept_pkt(struct fas * fas,struct fas_cmd * sp,int flag)2755 fas_accept_pkt(struct fas *fas, struct fas_cmd *sp, int flag)
2756 {
2757 	short slot = sp->cmd_slot;
2758 	int rval = TRAN_ACCEPT;
2759 
2760 	TRACE_0(TR_FAC_SCSI_FAS, TR__FAS_START_START, "fas_accept_pkt_start");
2761 	ASSERT(mutex_owned(FAS_MUTEX(fas)));
2762 	ASSERT(fas->f_ncmds >= 0 && fas->f_ndisc >= 0);
2763 	ASSERT(fas->f_ncmds >= fas->f_ndisc);
2764 	ASSERT(fas->f_tcmds[slot] >= 0);
2765 
2766 	/*
2767 	 * prepare packet for transport if this hasn't been done yet and
2768 	 * do some checks
2769 	 */
2770 	if ((sp->cmd_flags & CFLAG_PREPARED) == 0) {
2771 		rval = fas_prepare_pkt(fas, sp);
2772 		if (rval != TRAN_ACCEPT) {
2773 			IPRINTF1("prepare pkt failed, slot=%x\n", slot);
2774 			sp->cmd_flags &= ~CFLAG_TRANFLAG;
2775 			goto done;
2776 		}
2777 	}
2778 
2779 	if (Lun(sp)) {
2780 		EPRINTF("fas_accept_pkt: switching target and lun slot scan\n");
2781 		fas->f_dslot = 1;
2782 
2783 		if ((fas->f_active[slot] == NULL) ||
2784 		    ((fas->f_active[slot]->f_n_slots != NTAGS) &&
2785 		    TAGGED(Tgt(sp)))) {
2786 			(void) fas_alloc_active_slots(fas, slot, KM_NOSLEEP);
2787 		}
2788 		if ((fas->f_active[slot] == NULL) ||
2789 		    (NOTAG(Tgt(sp)) && (sp->cmd_pkt_flags & FLAG_TAGMASK))) {
2790 			IPRINTF("fatal error on non-zero lun pkt\n");
2791 			return (TRAN_FATAL_ERROR);
2792 		}
2793 	}
2794 
2795 	/*
2796 	 * we accepted the command; increment the count
2797 	 * (we may still reject later if TRAN_BUSY_OK)
2798 	 */
2799 	fas_check_ncmds(fas);
2800 	fas->f_ncmds++;
2801 
2802 	/*
2803 	 * if it is a nointr packet, start it now
2804 	 * (NO_INTR pkts are not queued in the waitQ)
2805 	 */
2806 	if (sp->cmd_pkt_flags & FLAG_NOINTR) {
2807 		EPRINTF("starting a nointr cmd\n");
2808 		fas_runpoll(fas, slot, sp);
2809 		sp->cmd_flags &= ~CFLAG_TRANFLAG;
2810 		goto done;
2811 	}
2812 
2813 	/*
2814 	 * reset the throttle if we were draining
2815 	 */
2816 	if ((fas->f_tcmds[slot] == 0) &&
2817 	    (fas->f_throttle[slot] == DRAIN_THROTTLE)) {
2818 		DPRINTF("reset throttle\n");
2819 		ASSERT(fas->f_reset_delay[Tgt(sp)] == 0);
2820 		fas_full_throttle(fas, slot);
2821 	}
2822 
2823 	/*
2824 	 * accept the command:
2825 	 * If no readyQ and no bus free, and throttle is OK,
2826 	 * run cmd immediately.
2827 	 */
2828 #ifdef FASDEBUG
2829 	fas->f_total_cmds++;
2830 #endif
2831 
2832 	if ((fas->f_readyf[slot] == NULL) && (fas->f_state == STATE_FREE) &&
2833 	    (fas->f_throttle[slot] > fas->f_tcmds[slot])) {
2834 		ASSERT(fas->f_current_sp == 0);
2835 		(void) fas_startcmd(fas, sp);
2836 		goto exit;
2837 	} else {
2838 		/*
2839 		 * If FLAG_HEAD is set, run cmd if target and bus are
2840 		 * available. if first cmd in ready Q is request sense
2841 		 * then insert after this command, there shouldn't be more
2842 		 * than one request sense.
2843 		 */
2844 		if (sp->cmd_pkt_flags & FLAG_HEAD) {
2845 			struct fas_cmd *ssp = fas->f_readyf[slot];
2846 			EPRINTF("que head\n");
2847 			if (ssp &&
2848 			    *(ssp->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE) {
2849 				fas_head_of_readyQ(fas, sp);
2850 			} else if (ssp) {
2851 				struct fas_cmd *dp = ssp->cmd_forw;
2852 				ssp->cmd_forw = sp;
2853 				sp->cmd_forw = dp;
2854 				if (fas->f_readyb[slot] == ssp) {
2855 					fas->f_readyb[slot] = sp;
2856 				}
2857 			} else {
2858 				fas->f_readyf[slot] = fas->f_readyb[slot] = sp;
2859 				sp->cmd_forw = NULL;
2860 			}
2861 
2862 		/*
2863 		 * for tagged targets, check for qfull condition and
2864 		 * return TRAN_BUSY (if permitted), if throttle has been
2865 		 * exceeded
2866 		 */
2867 		} else if (TAGGED(Tgt(sp)) &&
2868 		    (fas->f_tcmds[slot] >= fas->f_throttle[slot]) &&
2869 		    (fas->f_throttle[slot] > HOLD_THROTTLE) &&
2870 		    (flag == TRAN_BUSY_OK)) {
2871 			IPRINTF2(
2872 			    "transport busy, slot=%x, ncmds=%x\n",
2873 			    slot, fas->f_ncmds);
2874 			rval = TRAN_BUSY;
2875 			fas->f_ncmds--;
2876 			sp->cmd_flags &=
2877 			    ~(CFLAG_PREPARED | CFLAG_IN_TRANSPORT);
2878 			goto done;
2879 			/*
2880 			 * append to readyQ or start a new readyQ
2881 			 */
2882 		} else if (fas->f_readyf[slot]) {
2883 			struct fas_cmd *dp = fas->f_readyb[slot];
2884 			ASSERT(dp != 0);
2885 			fas->f_readyb[slot] = sp;
2886 			sp->cmd_forw = NULL;
2887 			dp->cmd_forw = sp;
2888 		} else {
2889 			fas->f_readyf[slot] = fas->f_readyb[slot] = sp;
2890 			sp->cmd_forw = NULL;
2891 		}
2892 
2893 	}
2894 
2895 done:
2896 	/*
2897 	 * just in case that the bus is free and we haven't
2898 	 * been able to restart for some reason
2899 	 */
2900 	if (fas->f_state == STATE_FREE) {
2901 		(void) fas_istart(fas);
2902 	}
2903 
2904 exit:
2905 	fas_check_ncmds(fas);
2906 	ASSERT(mutex_owned(FAS_MUTEX(fas)));
2907 	TRACE_0(TR_FAC_SCSI_FAS, TR__FAS_START_END,	"fas_accept_pkt_end");
2908 	return (rval);
2909 }
2910 
2911 /*
2912  * allocate a tag byte and check for tag aging
2913  */
2914 static char fas_tag_lookup[] =
2915 	{0, MSG_HEAD_QTAG, MSG_ORDERED_QTAG, 0, MSG_SIMPLE_QTAG};
2916 
2917 static int
fas_alloc_tag(struct fas * fas,struct fas_cmd * sp)2918 fas_alloc_tag(struct fas *fas, struct fas_cmd *sp)
2919 {
2920 	struct f_slots *tag_slots;
2921 	int tag;
2922 	short slot = sp->cmd_slot;
2923 
2924 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_ALLOC_TAG_START, "fas_alloc_tag_start");
2925 	ASSERT(mutex_owned(FAS_MUTEX(fas)));
2926 
2927 	tag_slots = fas->f_active[slot];
2928 	ASSERT(tag_slots->f_n_slots == NTAGS);
2929 
2930 alloc_tag:
2931 	tag = (fas->f_active[slot]->f_tags)++;
2932 	if (fas->f_active[slot]->f_tags >= NTAGS) {
2933 		/*
2934 		 * we reserve tag 0 for non-tagged cmds
2935 		 */
2936 		fas->f_active[slot]->f_tags = 1;
2937 	}
2938 	EPRINTF1("tagged cmd, tag = %d\n", tag);
2939 
2940 	/* Validate tag, should never fail. */
2941 	if (tag_slots->f_slot[tag] == 0) {
2942 		/*
2943 		 * Store assigned tag and tag queue type.
2944 		 * Note, in case of multiple choice, default to simple queue.
2945 		 */
2946 		ASSERT(tag < NTAGS);
2947 		sp->cmd_tag[1] = (uchar_t)tag;
2948 		sp->cmd_tag[0] = fas_tag_lookup[((sp->cmd_pkt_flags &
2949 		    FLAG_TAGMASK) >> 12)];
2950 		EPRINTF1("tag= %d\n", tag);
2951 		tag_slots->f_slot[tag] = sp;
2952 		(fas->f_tcmds[slot])++;
2953 		ASSERT(mutex_owned(FAS_MUTEX(fas)));
2954 		TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_ALLOC_TAG_END,
2955 		    "fas_alloc_tag_end");
2956 		return (0);
2957 
2958 	} else {
2959 		int age, i;
2960 
2961 		/*
2962 		 * Check tag age.  If timeouts enabled and
2963 		 * tag age greater than 1, print warning msg.
2964 		 * If timeouts enabled and tag age greater than
2965 		 * age limit, begin draining tag que to check for
2966 		 * lost tag cmd.
2967 		 */
2968 		age = tag_slots->f_slot[tag]->cmd_age++;
2969 		if (age >= fas->f_scsi_tag_age_limit &&
2970 		    tag_slots->f_slot[tag]->cmd_pkt->pkt_time) {
2971 			IPRINTF2("tag %d in use, age= %d\n", tag, age);
2972 			DPRINTF("draining tag queue\n");
2973 			if (fas->f_reset_delay[Tgt(sp)] == 0) {
2974 				fas->f_throttle[slot] = DRAIN_THROTTLE;
2975 			}
2976 		}
2977 
2978 		/* If tag in use, scan until a free one is found. */
2979 		for (i = 1; i < NTAGS; i++) {
2980 			tag = fas->f_active[slot]->f_tags;
2981 			if (!tag_slots->f_slot[tag]) {
2982 				EPRINTF1("found free tag %d\n", tag);
2983 				break;
2984 			}
2985 			if (++(fas->f_active[slot]->f_tags) >= NTAGS) {
2986 			/*
2987 			 * we reserve tag 0 for non-tagged cmds
2988 			 */
2989 				fas->f_active[slot]->f_tags = 1;
2990 			}
2991 			EPRINTF1("found in use tag %d\n", tag);
2992 		}
2993 
2994 		/*
2995 		 * If no free tags, we're in serious trouble.
2996 		 * the target driver submitted more than 255
2997 		 * requests
2998 		 */
2999 		if (tag_slots->f_slot[tag]) {
3000 			IPRINTF1("slot %x: All tags in use!!!\n", slot);
3001 			goto fail;
3002 		}
3003 		goto alloc_tag;
3004 	}
3005 
3006 fail:
3007 	fas_head_of_readyQ(fas, sp);
3008 
3009 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_ALLOC_TAG_END,
3010 	    "fas_alloc_tag_end");
3011 	return (-1);
3012 }
3013 
3014 /*
3015  * Internal Search Routine.
3016  *
3017  * Search for a command to start.
3018  */
3019 static int
fas_istart(struct fas * fas)3020 fas_istart(struct fas *fas)
3021 {
3022 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_ISTART_START,
3023 	    "fas_istart_start");
3024 	EPRINTF("fas_istart:\n");
3025 
3026 	if (fas->f_state == STATE_FREE && fas->f_ncmds > fas->f_ndisc) {
3027 		(void) fas_ustart(fas);
3028 	}
3029 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_ISTART_END,
3030 	    "fas_istart_end");
3031 	return (ACTION_RETURN);
3032 }
3033 
3034 static int
fas_ustart(struct fas * fas)3035 fas_ustart(struct fas *fas)
3036 {
3037 	struct fas_cmd *sp;
3038 	short slot = fas->f_next_slot;
3039 	short start_slot = slot;
3040 	short dslot = fas->f_dslot;
3041 
3042 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_USTART_START, "fas_ustart_start");
3043 	EPRINTF1("fas_ustart: start_slot=%x\n", fas->f_next_slot);
3044 	ASSERT(fas->f_current_sp == NULL);
3045 	ASSERT(dslot != 0);
3046 	if (dslot == NLUNS_PER_TARGET) {
3047 		ASSERT((slot % NLUNS_PER_TARGET) == 0);
3048 	}
3049 
3050 	/*
3051 	 * if readyQ not empty and we are not draining, then we
3052 	 * can start another cmd
3053 	 */
3054 	do {
3055 		/*
3056 		 * If all cmds drained from tag Q, back to full throttle and
3057 		 * start queueing up new cmds again.
3058 		 */
3059 		if (fas->f_throttle[slot] == DRAIN_THROTTLE &&
3060 		    fas->f_tcmds[slot] == 0) {
3061 			fas_full_throttle(fas, slot);
3062 		}
3063 
3064 		if (fas->f_readyf[slot] &&
3065 		    (fas->f_throttle[slot] > fas->f_tcmds[slot])) {
3066 			sp = fas->f_readyf[slot];
3067 			fas->f_readyf[slot] = sp->cmd_forw;
3068 			if (sp->cmd_forw == NULL) {
3069 				fas->f_readyb[slot] = NULL;
3070 			}
3071 			fas->f_next_slot = NEXTSLOT(slot, dslot);
3072 			ASSERT((sp->cmd_pkt_flags & FLAG_NOINTR) == 0);
3073 			TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_USTART_END,
3074 			    "fas_ustart_end");
3075 			return (fas_startcmd(fas, sp));
3076 		} else {
3077 			slot = NEXTSLOT(slot, dslot);
3078 		}
3079 	} while (slot != start_slot);
3080 
3081 	EPRINTF("fas_ustart: no cmds to start\n");
3082 	fas->f_next_slot = NEXTSLOT(slot, dslot);
3083 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_USTART_NOT_FOUND_END,
3084 	    "fas_ustart_end (not_found)");
3085 	return (FALSE);
3086 }
3087 
3088 /*
3089  * Start a command off
3090  */
3091 static int
fas_startcmd(struct fas * fas,struct fas_cmd * sp)3092 fas_startcmd(struct fas *fas, struct fas_cmd *sp)
3093 {
3094 	volatile struct fasreg *fasreg = fas->f_reg;
3095 	ushort_t  nstate;
3096 	uchar_t cmd, target, lun;
3097 	ushort_t tshift;
3098 	volatile uchar_t *tp = fas->f_cmdarea;
3099 	struct scsi_pkt *pkt = CMD2PKT(sp);
3100 	int slot = sp->cmd_slot;
3101 	struct f_slots *slots = fas->f_active[slot];
3102 	int i, cdb_len;
3103 
3104 #define	LOAD_CMDP	*(tp++)
3105 
3106 	TRACE_0(TR_FAC_SCSI_FAS, TR_FAS_STARTCMD_START, "fas_startcmd_start");
3107 
3108 	EPRINTF2("fas_startcmd: sp=0x%p flags=%x\n",
3109 	    (void *)sp, sp->cmd_pkt_flags);
3110 	ASSERT((sp->cmd_flags & CFLAG_FREE) == 0);
3111 	ASSERT((sp->cmd_flags & CFLAG_COMPLETED) == 0);
3112 	ASSERT(fas->f_current_sp == NULL && fas->f_state == STATE_FREE);
3113 	if ((sp->cmd_pkt_flags & FLAG_NOINTR) == 0) {
3114 		ASSERT(fas->f_throttle[slot] > 0);
3115 		ASSERT(fas->f_reset_delay[Tgt(sp)] == 0);
3116 	}
3117 
3118 	target		= Tgt(sp);
3119 	lun		= Lun(sp);
3120 
3121 	/*
3122 	 * if a non-tagged cmd is submitted to an active tagged target
3123 	 * then drain before submitting this cmd; SCSI-2 allows RQSENSE
3124 	 * to be untagged
3125 	 */
3126 	if (((sp->cmd_pkt_flags & FLAG_TAGMASK) == 0) &&
3127 	    TAGGED(target) && fas->f_tcmds[slot] &&
3128 	    ((sp->cmd_flags & CFLAG_CMDPROXY) == 0) &&
3129 	    (*(sp->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE)) {
3130 		if ((sp->cmd_pkt_flags & FLAG_NOINTR) == 0) {
3131 			struct fas_cmd *dp;
3132 
3133 			IPRINTF("untagged cmd, start draining\n");
3134 
3135 			if (fas->f_reset_delay[Tgt(sp)] == 0) {
3136 				fas->f_throttle[slot] = DRAIN_THROTTLE;
3137 			}
3138 			dp = fas->f_readyf[slot];
3139 			fas->f_readyf[slot] = sp;
3140 			sp->cmd_forw = dp;
3141 			if (fas->f_readyb[slot] == NULL) {
3142 				fas->f_readyb[slot] = sp;
3143 			}
3144 		}
3145 		return (FALSE);
3146 	}
3147 
3148 	/*
3149 	 * allocate a tag; if no tag available then put request back
3150 	 * on the ready queue and return; eventually a cmd returns and we
3151 	 * get going again or we timeout
3152 	 */
3153 	if (TAGGED(target) && (sp->cmd_pkt_flags & FLAG_TAGMASK)) {
3154 		if (fas_alloc_tag(fas, sp)) {
3155 			return (FALSE);
3156 		}
3157 	} else {
3158 		/*
3159 		 * tag slot 0 is reserved for non-tagged cmds
3160 		 * and should be empty because we have drained
3161 		 */
3162 		if ((sp->cmd_flags & CFLAG_CMDPROXY) == 0) {
3163 			ASSERT(fas->f_active[slot]->f_slot[0] == NULL);
3164 			fas->f_active[slot]->f_slot[0] = sp;
3165 			sp->cmd_tag[1] = 0;
3166 			if (*(sp->cmd_pkt->pkt_cdbp) != SCMD_REQUEST_SENSE) {
3167 				ASSERT(fas->f_tcmds[slot] == 0);
3168 				/*
3169 				 * don't start any other cmd until this
3170 				 * one is finished. The throttle is reset
3171 				 * later in fas_watch()
3172 				 */
3173 				fas->f_throttle[slot] = 1;
3174 			}
3175 			(fas->f_tcmds[slot])++;
3176 
3177 		}
3178 	}
3179 
3180 	fas->f_current_sp = sp;
3181 	fas->f_omsglen	= 0;
3182 	tshift		= 1<<target;
3183 	fas->f_sdtr_sent = fas->f_wdtr_sent =	0;
3184 	cdb_len 	= sp->cmd_actual_cdblen;
3185 
3186 	if (sp->cmd_pkt_flags & FLAG_RENEGOTIATE_WIDE_SYNC) {
3187 		fas_force_renegotiation(fas, Tgt(sp));
3188 	}
3189 
3190 	/*
3191 	 * first send identify message, with or w