1b1dd958fScth /*
2b1dd958fScth * CDDL HEADER START
3b1dd958fScth *
4b1dd958fScth * The contents of this file are subject to the terms of the
519397407SSherry Moore * Common Development and Distribution License (the "License").
619397407SSherry Moore * You may not use this file except in compliance with the License.
7b1dd958fScth *
8b1dd958fScth * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9b1dd958fScth * or http://www.opensolaris.org/os/licensing.
10b1dd958fScth * See the License for the specific language governing permissions
11b1dd958fScth * and limitations under the License.
12b1dd958fScth *
13b1dd958fScth * When distributing Covered Code, include this CDDL HEADER in each
14b1dd958fScth * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15b1dd958fScth * If applicable, add the following below this CDDL HEADER, with the
16b1dd958fScth * fields enclosed by brackets "[]" replaced with your own identifying
17b1dd958fScth * information: Portions Copyright [yyyy] [name of copyright owner]
18b1dd958fScth *
19b1dd958fScth * CDDL HEADER END
20b1dd958fScth */
21b1dd958fScth /*
229c57abc8Ssrivijitha dugganapalli * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23b1dd958fScth * Use is subject to license terms.
2489b43686SBayard Bell * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
25b1dd958fScth */
26b1dd958fScth
27b1dd958fScth
28b1dd958fScth /*
29b1dd958fScth * SCSA HBA nexus driver that emulates an HBA connected to SCSI target
30b1dd958fScth * devices (large disks).
31b1dd958fScth */
32b1dd958fScth
33b1dd958fScth #ifdef DEBUG
34b1dd958fScth #define EMUL64DEBUG
35b1dd958fScth #endif
36b1dd958fScth
37b1dd958fScth #include <sys/scsi/scsi.h>
38b1dd958fScth #include <sys/ddi.h>
39b1dd958fScth #include <sys/sunddi.h>
40b1dd958fScth #include <sys/taskq.h>
41b1dd958fScth #include <sys/disp.h>
42b1dd958fScth #include <sys/types.h>
43b1dd958fScth #include <sys/buf.h>
44b1dd958fScth #include <sys/cpuvar.h>
45b1dd958fScth #include <sys/dklabel.h>
46b1dd958fScth
47b1dd958fScth #include <sys/emul64.h>
48b1dd958fScth #include <sys/emul64cmd.h>
49b1dd958fScth #include <sys/emul64var.h>
50b1dd958fScth
51b1dd958fScth int emul64_usetaskq = 1; /* set to zero for debugging */
52b1dd958fScth int emul64debug = 0;
53b1dd958fScth #ifdef EMUL64DEBUG
54b1dd958fScth static int emul64_cdb_debug = 0;
55b1dd958fScth #include <sys/debug.h>
56b1dd958fScth #endif
57b1dd958fScth
58b1dd958fScth /*
59b1dd958fScth * cb_ops function prototypes
60b1dd958fScth */
61*2178a880SToomas Soome static int emul64_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
62b1dd958fScth
63b1dd958fScth /*
64b1dd958fScth * dev_ops functions prototypes
65b1dd958fScth */
66b1dd958fScth static int emul64_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
67b1dd958fScth void *arg, void **result);
68b1dd958fScth static int emul64_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
69b1dd958fScth static int emul64_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
70b1dd958fScth
71b1dd958fScth /*
72b1dd958fScth * Function prototypes
73b1dd958fScth *
74b1dd958fScth * SCSA functions exported by means of the transport table
75b1dd958fScth */
76b1dd958fScth static int emul64_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
77b1dd958fScth scsi_hba_tran_t *tran, struct scsi_device *sd);
78b1dd958fScth static int emul64_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt);
79b1dd958fScth static void emul64_pkt_comp(void *);
80b1dd958fScth static int emul64_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt);
81b1dd958fScth static int emul64_scsi_reset(struct scsi_address *ap, int level);
82b1dd958fScth static int emul64_scsi_getcap(struct scsi_address *ap, char *cap, int whom);
83b1dd958fScth static int emul64_scsi_setcap(struct scsi_address *ap, char *cap, int value,
84b1dd958fScth int whom);
85b1dd958fScth static struct scsi_pkt *emul64_scsi_init_pkt(struct scsi_address *ap,
86b1dd958fScth struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen,
87b1dd958fScth int tgtlen, int flags, int (*callback)(), caddr_t arg);
88b1dd958fScth static void emul64_scsi_destroy_pkt(struct scsi_address *ap,
89*2178a880SToomas Soome struct scsi_pkt *pkt);
90b1dd958fScth static void emul64_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt);
91b1dd958fScth static void emul64_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt);
92b1dd958fScth static int emul64_scsi_reset_notify(struct scsi_address *ap, int flag,
93b1dd958fScth void (*callback)(caddr_t), caddr_t arg);
94b1dd958fScth
95b1dd958fScth /*
96b1dd958fScth * internal functions
97b1dd958fScth */
98b1dd958fScth static void emul64_i_initcap(struct emul64 *emul64);
99b1dd958fScth
100b1dd958fScth static void emul64_i_log(struct emul64 *emul64, int level, char *fmt, ...);
101*2178a880SToomas Soome static int emul64_get_tgtrange(struct emul64 *, intptr_t, emul64_tgt_t **,
102*2178a880SToomas Soome emul64_tgt_range_t *);
103*2178a880SToomas Soome static int emul64_write_off(struct emul64 *, emul64_tgt_t *,
104*2178a880SToomas Soome emul64_tgt_range_t *);
105*2178a880SToomas Soome static int emul64_write_on(struct emul64 *, emul64_tgt_t *,
106*2178a880SToomas Soome emul64_tgt_range_t *);
107b1dd958fScth static emul64_nowrite_t *emul64_nowrite_alloc(emul64_range_t *);
108b1dd958fScth static void emul64_nowrite_free(emul64_nowrite_t *);
109b1dd958fScth static emul64_nowrite_t *emul64_find_nowrite(emul64_tgt_t *,
110*2178a880SToomas Soome diskaddr_t start_block, size_t blkcnt, emul64_rng_overlap_t *overlapp,
111*2178a880SToomas Soome emul64_nowrite_t ***prevp);
112b1dd958fScth
113b1dd958fScth extern emul64_tgt_t *find_tgt(struct emul64 *, ushort_t, ushort_t);
114b1dd958fScth
115b1dd958fScth #ifdef EMUL64DEBUG
116b1dd958fScth static void emul64_debug_dump_cdb(struct scsi_address *ap,
117*2178a880SToomas Soome struct scsi_pkt *pkt);
118b1dd958fScth #endif
119b1dd958fScth
120b1dd958fScth
121b1dd958fScth #ifdef _DDICT
122b1dd958fScth static int ddi_in_panic(void);
ddi_in_panic()123b1dd958fScth static int ddi_in_panic() { return (0); }
124b1dd958fScth #ifndef SCSI_CAP_RESET_NOTIFICATION
125b1dd958fScth #define SCSI_CAP_RESET_NOTIFICATION 14
126b1dd958fScth #endif
127b1dd958fScth #ifndef SCSI_RESET_NOTIFY
128b1dd958fScth #define SCSI_RESET_NOTIFY 0x01
129b1dd958fScth #endif
130b1dd958fScth #ifndef SCSI_RESET_CANCEL
131b1dd958fScth #define SCSI_RESET_CANCEL 0x02
132b1dd958fScth #endif
133b1dd958fScth #endif
134b1dd958fScth
135b1dd958fScth /*
136b1dd958fScth * Tunables:
137b1dd958fScth *
138b1dd958fScth * emul64_max_task
139b1dd958fScth * The taskq facility is used to queue up SCSI start requests on a per
140b1dd958fScth * controller basis. If the maximum number of queued tasks is hit,
141b1dd958fScth * taskq_ent_alloc() delays for a second, which adversely impacts our
142b1dd958fScth * performance. This value establishes the maximum number of task
143b1dd958fScth * queue entries when taskq_create is called.
144b1dd958fScth *
145b1dd958fScth * emul64_task_nthreads
146b1dd958fScth * Specifies the number of threads that should be used to process a
147b1dd958fScth * controller's task queue. Our init function sets this to the number
148b1dd958fScth * of CPUs on the system, but this can be overridden in emul64.conf.
149b1dd958fScth */
150b1dd958fScth int emul64_max_task = 16;
151b1dd958fScth int emul64_task_nthreads = 1;
152b1dd958fScth
153b1dd958fScth /*
154b1dd958fScth * Local static data
155b1dd958fScth */
156b1dd958fScth static void *emul64_state = NULL;
157b1dd958fScth
158b1dd958fScth /*
159b1dd958fScth * Character/block operations.
160b1dd958fScth */
161b1dd958fScth static struct cb_ops emul64_cbops = {
162b1dd958fScth scsi_hba_open, /* cb_open */
163b1dd958fScth scsi_hba_close, /* cb_close */
164b1dd958fScth nodev, /* cb_strategy */
165b1dd958fScth nodev, /* cb_print */
166b1dd958fScth nodev, /* cb_dump */
167b1dd958fScth nodev, /* cb_read */
168b1dd958fScth nodev, /* cb_write */
169b1dd958fScth emul64_ioctl, /* cb_ioctl */
170b1dd958fScth nodev, /* cb_devmap */
171b1dd958fScth nodev, /* cb_mmap */
172b1dd958fScth nodev, /* cb_segmap */
173b1dd958fScth nochpoll, /* cb_chpoll */
174b1dd958fScth ddi_prop_op, /* cb_prop_op */
175b1dd958fScth NULL, /* cb_str */
176b1dd958fScth D_MP | D_64BIT | D_HOTPLUG, /* cb_flag */
177b1dd958fScth CB_REV, /* cb_rev */
178b1dd958fScth nodev, /* cb_aread */
179b1dd958fScth nodev /* cb_awrite */
180b1dd958fScth };
181b1dd958fScth
182b1dd958fScth /*
183b1dd958fScth * autoconfiguration routines.
184b1dd958fScth */
185b1dd958fScth
186b1dd958fScth static struct dev_ops emul64_ops = {
187b1dd958fScth DEVO_REV, /* rev, */
188b1dd958fScth 0, /* refcnt */
189b1dd958fScth emul64_info, /* getinfo */
190b1dd958fScth nulldev, /* identify */
191b1dd958fScth nulldev, /* probe */
192b1dd958fScth emul64_attach, /* attach */
193b1dd958fScth emul64_detach, /* detach */
194b1dd958fScth nodev, /* reset */
195b1dd958fScth &emul64_cbops, /* char/block ops */
19619397407SSherry Moore NULL, /* bus ops */
19719397407SSherry Moore NULL, /* power */
19819397407SSherry Moore ddi_quiesce_not_needed, /* quiesce */
199b1dd958fScth };
200b1dd958fScth
201b1dd958fScth static struct modldrv modldrv = {
202b1dd958fScth &mod_driverops, /* module type - driver */
203b1dd958fScth "emul64 SCSI Host Bus Adapter", /* module name */
204b1dd958fScth &emul64_ops, /* driver ops */
205b1dd958fScth };
206b1dd958fScth
207b1dd958fScth static struct modlinkage modlinkage = {
208b1dd958fScth MODREV_1, /* ml_rev - must be MODREV_1 */
209b1dd958fScth &modldrv, /* ml_linkage */
210b1dd958fScth NULL /* end of driver linkage */
211b1dd958fScth };
212b1dd958fScth
213b1dd958fScth int
_init(void)214b1dd958fScth _init(void)
215b1dd958fScth {
216b1dd958fScth int ret;
217b1dd958fScth
218b1dd958fScth ret = ddi_soft_state_init(&emul64_state, sizeof (struct emul64),
219b1dd958fScth EMUL64_INITIAL_SOFT_SPACE);
220b1dd958fScth if (ret != 0)
221b1dd958fScth return (ret);
222b1dd958fScth
223b1dd958fScth if ((ret = scsi_hba_init(&modlinkage)) != 0) {
224b1dd958fScth ddi_soft_state_fini(&emul64_state);
225b1dd958fScth return (ret);
226b1dd958fScth }
227b1dd958fScth
228b1dd958fScth /* Set the number of task threads to the number of CPUs */
229b1dd958fScth if (boot_max_ncpus == -1) {
230b1dd958fScth emul64_task_nthreads = max_ncpus;
231b1dd958fScth } else {
232b1dd958fScth emul64_task_nthreads = boot_max_ncpus;
233b1dd958fScth }
234b1dd958fScth
235b1dd958fScth emul64_bsd_init();
236b1dd958fScth
237b1dd958fScth ret = mod_install(&modlinkage);
238b1dd958fScth if (ret != 0) {
239b1dd958fScth emul64_bsd_fini();
240b1dd958fScth scsi_hba_fini(&modlinkage);
241b1dd958fScth ddi_soft_state_fini(&emul64_state);
242b1dd958fScth }
243b1dd958fScth
244b1dd958fScth return (ret);
245b1dd958fScth }
246b1dd958fScth
247b1dd958fScth int
_fini(void)248b1dd958fScth _fini(void)
249b1dd958fScth {
250b1dd958fScth int ret;
251b1dd958fScth
252b1dd958fScth if ((ret = mod_remove(&modlinkage)) != 0)
253b1dd958fScth return (ret);
254b1dd958fScth
255b1dd958fScth emul64_bsd_fini();
256b1dd958fScth
257b1dd958fScth scsi_hba_fini(&modlinkage);
258b1dd958fScth
259b1dd958fScth ddi_soft_state_fini(&emul64_state);
260b1dd958fScth
261b1dd958fScth return (ret);
262b1dd958fScth }
263b1dd958fScth
264b1dd958fScth int
_info(struct modinfo * modinfop)265b1dd958fScth _info(struct modinfo *modinfop)
266b1dd958fScth {
267b1dd958fScth return (mod_info(&modlinkage, modinfop));
268b1dd958fScth }
269b1dd958fScth
270b1dd958fScth /*
271b1dd958fScth * Given the device number return the devinfo pointer
272b1dd958fScth * from the scsi_device structure.
273b1dd958fScth */
274b1dd958fScth /*ARGSUSED*/
275b1dd958fScth static int
emul64_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)276b1dd958fScth emul64_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
277b1dd958fScth {
278b1dd958fScth struct emul64 *foo;
279b1dd958fScth int instance = getminor((dev_t)arg);
280b1dd958fScth
281b1dd958fScth switch (cmd) {
282b1dd958fScth case DDI_INFO_DEVT2DEVINFO:
283b1dd958fScth foo = ddi_get_soft_state(emul64_state, instance);
284b1dd958fScth if (foo != NULL)
285b1dd958fScth *result = (void *)foo->emul64_dip;
286b1dd958fScth else {
287b1dd958fScth *result = NULL;
288b1dd958fScth return (DDI_FAILURE);
289b1dd958fScth }
290b1dd958fScth break;
291b1dd958fScth
292b1dd958fScth case DDI_INFO_DEVT2INSTANCE:
293b1dd958fScth *result = (void *)(uintptr_t)instance;
294b1dd958fScth break;
295b1dd958fScth
296b1dd958fScth default:
297b1dd958fScth return (DDI_FAILURE);
298b1dd958fScth }
299b1dd958fScth
300b1dd958fScth return (DDI_SUCCESS);
301b1dd958fScth }
302b1dd958fScth
303b1dd958fScth /*
304b1dd958fScth * Attach an instance of an emul64 host adapter. Allocate data structures,
305b1dd958fScth * initialize the emul64 and we're on the air.
306b1dd958fScth */
307b1dd958fScth /*ARGSUSED*/
308b1dd958fScth static int
emul64_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)309b1dd958fScth emul64_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
310b1dd958fScth {
311b1dd958fScth int mutex_initted = 0;
312b1dd958fScth struct emul64 *emul64;
313b1dd958fScth int instance;
314b1dd958fScth scsi_hba_tran_t *tran = NULL;
315b1dd958fScth ddi_dma_attr_t tmp_dma_attr;
316b1dd958fScth
317b1dd958fScth emul64_bsd_get_props(dip);
318b1dd958fScth
319b1dd958fScth bzero((void *) &tmp_dma_attr, sizeof (tmp_dma_attr));
320b1dd958fScth instance = ddi_get_instance(dip);
321b1dd958fScth
322b1dd958fScth switch (cmd) {
323b1dd958fScth case DDI_ATTACH:
324b1dd958fScth break;
325b1dd958fScth
326b1dd958fScth case DDI_RESUME:
327b1dd958fScth tran = (scsi_hba_tran_t *)ddi_get_driver_private(dip);
328b1dd958fScth if (!tran) {
329b1dd958fScth return (DDI_FAILURE);
330b1dd958fScth }
331b1dd958fScth emul64 = TRAN2EMUL64(tran);
332b1dd958fScth
333b1dd958fScth return (DDI_SUCCESS);
334b1dd958fScth
335b1dd958fScth default:
336b1dd958fScth emul64_i_log(NULL, CE_WARN,
337b1dd958fScth "emul64%d: Cmd != DDI_ATTACH/DDI_RESUME", instance);
338b1dd958fScth return (DDI_FAILURE);
339b1dd958fScth }
340b1dd958fScth
341b1dd958fScth /*
342b1dd958fScth * Allocate emul64 data structure.
343b1dd958fScth */
344b1dd958fScth if (ddi_soft_state_zalloc(emul64_state, instance) != DDI_SUCCESS) {
345b1dd958fScth emul64_i_log(NULL, CE_WARN,
34619397407SSherry Moore "emul64%d: Failed to alloc soft state",
34719397407SSherry Moore instance);
348b1dd958fScth return (DDI_FAILURE);
349b1dd958fScth }
350b1dd958fScth
351b1dd958fScth emul64 = (struct emul64 *)ddi_get_soft_state(emul64_state, instance);
352b1dd958fScth if (emul64 == (struct emul64 *)NULL) {
353b1dd958fScth emul64_i_log(NULL, CE_WARN, "emul64%d: Bad soft state",
35419397407SSherry Moore instance);
355b1dd958fScth ddi_soft_state_free(emul64_state, instance);
356b1dd958fScth return (DDI_FAILURE);
357b1dd958fScth }
358b1dd958fScth
359b1dd958fScth
360b1dd958fScth /*
361b1dd958fScth * Allocate a transport structure
362b1dd958fScth */
363b1dd958fScth tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP);
364b1dd958fScth if (tran == NULL) {
365b1dd958fScth cmn_err(CE_WARN, "emul64: scsi_hba_tran_alloc failed\n");
366b1dd958fScth goto fail;
367b1dd958fScth }
368b1dd958fScth
369b1dd958fScth emul64->emul64_tran = tran;
370b1dd958fScth emul64->emul64_dip = dip;
371b1dd958fScth
372b1dd958fScth tran->tran_hba_private = emul64;
373b1dd958fScth tran->tran_tgt_private = NULL;
374b1dd958fScth tran->tran_tgt_init = emul64_tran_tgt_init;
375b1dd958fScth tran->tran_tgt_probe = scsi_hba_probe;
376b1dd958fScth tran->tran_tgt_free = NULL;
377b1dd958fScth
378b1dd958fScth tran->tran_start = emul64_scsi_start;
379b1dd958fScth tran->tran_abort = emul64_scsi_abort;
380b1dd958fScth tran->tran_reset = emul64_scsi_reset;
381b1dd958fScth tran->tran_getcap = emul64_scsi_getcap;
382b1dd958fScth tran->tran_setcap = emul64_scsi_setcap;
383b1dd958fScth tran->tran_init_pkt = emul64_scsi_init_pkt;
384b1dd958fScth tran->tran_destroy_pkt = emul64_scsi_destroy_pkt;
385b1dd958fScth tran->tran_dmafree = emul64_scsi_dmafree;
386b1dd958fScth tran->tran_sync_pkt = emul64_scsi_sync_pkt;
387*2178a880SToomas Soome tran->tran_reset_notify = emul64_scsi_reset_notify;
388b1dd958fScth
389b1dd958fScth tmp_dma_attr.dma_attr_minxfer = 0x1;
390b1dd958fScth tmp_dma_attr.dma_attr_burstsizes = 0x7f;
391b1dd958fScth
392b1dd958fScth /*
393b1dd958fScth * Attach this instance of the hba
394b1dd958fScth */
395b1dd958fScth if (scsi_hba_attach_setup(dip, &tmp_dma_attr, tran,
396b1dd958fScth 0) != DDI_SUCCESS) {
397b1dd958fScth cmn_err(CE_WARN, "emul64: scsi_hba_attach failed\n");
398b1dd958fScth goto fail;
399b1dd958fScth }
400b1dd958fScth
401b1dd958fScth emul64->emul64_initiator_id = 2;
402b1dd958fScth
403b1dd958fScth /*
404b1dd958fScth * Look up the scsi-options property
405b1dd958fScth */
406b1dd958fScth emul64->emul64_scsi_options =
40719397407SSherry Moore ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0, "scsi-options",
40819397407SSherry Moore EMUL64_DEFAULT_SCSI_OPTIONS);
409b1dd958fScth EMUL64_DEBUG(emul64, SCSI_DEBUG, "emul64 scsi-options=%x",
410b1dd958fScth emul64->emul64_scsi_options);
411b1dd958fScth
412b1dd958fScth
413b1dd958fScth /* mutexes to protect the emul64 request and response queue */
414b1dd958fScth mutex_init(EMUL64_REQ_MUTEX(emul64), NULL, MUTEX_DRIVER,
415b1dd958fScth emul64->emul64_iblock);
416b1dd958fScth mutex_init(EMUL64_RESP_MUTEX(emul64), NULL, MUTEX_DRIVER,
417b1dd958fScth emul64->emul64_iblock);
418b1dd958fScth
419b1dd958fScth mutex_initted = 1;
420b1dd958fScth
421b1dd958fScth EMUL64_MUTEX_ENTER(emul64);
422b1dd958fScth
423b1dd958fScth /*
424b1dd958fScth * Initialize the default Target Capabilities and Sync Rates
425b1dd958fScth */
426b1dd958fScth emul64_i_initcap(emul64);
427b1dd958fScth
428b1dd958fScth EMUL64_MUTEX_EXIT(emul64);
429b1dd958fScth
430b1dd958fScth
431b1dd958fScth ddi_report_dev(dip);
432b1dd958fScth emul64->emul64_taskq = taskq_create("emul64_comp",
43319397407SSherry Moore emul64_task_nthreads, MINCLSYSPRI, 1, emul64_max_task, 0);
434b1dd958fScth
435b1dd958fScth return (DDI_SUCCESS);
436b1dd958fScth
437b1dd958fScth fail:
438b1dd958fScth emul64_i_log(NULL, CE_WARN, "emul64%d: Unable to attach", instance);
439b1dd958fScth
440b1dd958fScth if (mutex_initted) {
441b1dd958fScth mutex_destroy(EMUL64_REQ_MUTEX(emul64));
442b1dd958fScth mutex_destroy(EMUL64_RESP_MUTEX(emul64));
443b1dd958fScth }
444b1dd958fScth if (tran) {
445b1dd958fScth scsi_hba_tran_free(tran);
446b1dd958fScth }
447b1dd958fScth ddi_soft_state_free(emul64_state, instance);
448b1dd958fScth return (DDI_FAILURE);
449b1dd958fScth }
450b1dd958fScth
451b1dd958fScth /*ARGSUSED*/
452b1dd958fScth static int
emul64_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)453b1dd958fScth emul64_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
454b1dd958fScth {
455b1dd958fScth struct emul64 *emul64;
456b1dd958fScth scsi_hba_tran_t *tran;
457b1dd958fScth int instance = ddi_get_instance(dip);
458b1dd958fScth
459b1dd958fScth
460b1dd958fScth /* get transport structure pointer from the dip */
461b1dd958fScth if (!(tran = (scsi_hba_tran_t *)ddi_get_driver_private(dip))) {
462b1dd958fScth return (DDI_FAILURE);
463b1dd958fScth }
464b1dd958fScth
465b1dd958fScth /* get soft state from transport structure */
466b1dd958fScth emul64 = TRAN2EMUL64(tran);
467b1dd958fScth
468b1dd958fScth if (!emul64) {
469b1dd958fScth return (DDI_FAILURE);
470b1dd958fScth }
471b1dd958fScth
472b1dd958fScth EMUL64_DEBUG(emul64, SCSI_DEBUG, "emul64_detach: cmd = %d", cmd);
473b1dd958fScth
474b1dd958fScth switch (cmd) {
475b1dd958fScth case DDI_DETACH:
476b1dd958fScth EMUL64_MUTEX_ENTER(emul64);
477b1dd958fScth
478b1dd958fScth taskq_destroy(emul64->emul64_taskq);
479b1dd958fScth (void) scsi_hba_detach(dip);
480b1dd958fScth
481b1dd958fScth scsi_hba_tran_free(emul64->emul64_tran);
482b1dd958fScth
483b1dd958fScth
484b1dd958fScth EMUL64_MUTEX_EXIT(emul64);
485b1dd958fScth
486b1dd958fScth mutex_destroy(EMUL64_REQ_MUTEX(emul64));
487b1dd958fScth mutex_destroy(EMUL64_RESP_MUTEX(emul64));
488b1dd958fScth
489b1dd958fScth
490b1dd958fScth EMUL64_DEBUG(emul64, SCSI_DEBUG, "emul64_detach: done");
491b1dd958fScth ddi_soft_state_free(emul64_state, instance);
492b1dd958fScth
493b1dd958fScth return (DDI_SUCCESS);
494b1dd958fScth
495b1dd958fScth case DDI_SUSPEND:
496b1dd958fScth return (DDI_SUCCESS);
497b1dd958fScth
498b1dd958fScth default:
499b1dd958fScth return (DDI_FAILURE);
500b1dd958fScth }
501b1dd958fScth }
502b1dd958fScth
503b1dd958fScth /*
504b1dd958fScth * Function name : emul64_tran_tgt_init
505b1dd958fScth *
506b1dd958fScth * Return Values : DDI_SUCCESS if target supported, DDI_FAILURE otherwise
507b1dd958fScth *
508b1dd958fScth */
509b1dd958fScth /*ARGSUSED*/
510b1dd958fScth static int
emul64_tran_tgt_init(dev_info_t * hba_dip,dev_info_t * tgt_dip,scsi_hba_tran_t * tran,struct scsi_device * sd)511b1dd958fScth emul64_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
512*2178a880SToomas Soome scsi_hba_tran_t *tran, struct scsi_device *sd)
513b1dd958fScth {
514b1dd958fScth struct emul64 *emul64;
515b1dd958fScth emul64_tgt_t *tgt;
516b1dd958fScth char **geo_vidpid = NULL;
517b1dd958fScth char *geo, *vidpid;
518b1dd958fScth uint32_t *geoip = NULL;
519b1dd958fScth uint_t length;
520b1dd958fScth uint_t length2;
521b1dd958fScth lldaddr_t sector_count;
522b1dd958fScth char prop_name[15];
523b1dd958fScth int ret = DDI_FAILURE;
524b1dd958fScth
525b1dd958fScth emul64 = TRAN2EMUL64(tran);
526b1dd958fScth EMUL64_MUTEX_ENTER(emul64);
527b1dd958fScth
528b1dd958fScth /*
529b1dd958fScth * We get called for each target driver.conf node, multiple
530b1dd958fScth * nodes may map to the same tgt,lun (sd.conf, st.conf, etc).
531b1dd958fScth * Check to see if transport to tgt,lun already established.
532b1dd958fScth */
533b1dd958fScth tgt = find_tgt(emul64, sd->sd_address.a_target, sd->sd_address.a_lun);
534b1dd958fScth if (tgt) {
535b1dd958fScth ret = DDI_SUCCESS;
536b1dd958fScth goto out;
537b1dd958fScth }
538b1dd958fScth
539b1dd958fScth /* see if we have driver.conf specified device for this target,lun */
540b1dd958fScth (void) snprintf(prop_name, sizeof (prop_name), "targ_%d_%d",
541b1dd958fScth sd->sd_address.a_target, sd->sd_address.a_lun);
542b1dd958fScth if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, hba_dip,
543b1dd958fScth DDI_PROP_DONTPASS, prop_name,
544b1dd958fScth &geo_vidpid, &length) != DDI_PROP_SUCCESS)
545b1dd958fScth goto out;
546b1dd958fScth if (length < 2) {
547b1dd958fScth cmn_err(CE_WARN, "emul64: %s property does not have 2 "
54819397407SSherry Moore "elements", prop_name);
549b1dd958fScth goto out;
550b1dd958fScth }
551b1dd958fScth
552b1dd958fScth /* pick geometry name and vidpid string from string array */
553b1dd958fScth geo = *geo_vidpid;
554b1dd958fScth vidpid = *(geo_vidpid + 1);
555b1dd958fScth
556b1dd958fScth /* lookup geometry property integer array */
557b1dd958fScth if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, hba_dip, DDI_PROP_DONTPASS,
558b1dd958fScth geo, (int **)&geoip, &length2) != DDI_PROP_SUCCESS) {
559b1dd958fScth cmn_err(CE_WARN, "emul64: didn't get prop '%s'", geo);
560b1dd958fScth goto out;
561b1dd958fScth }
562b1dd958fScth if (length2 < 6) {
563b1dd958fScth cmn_err(CE_WARN, "emul64: property %s does not have 6 "
56419397407SSherry Moore "elements", *geo_vidpid);
565b1dd958fScth goto out;
566b1dd958fScth }
567b1dd958fScth
568b1dd958fScth /* allocate and initialize tgt structure for tgt,lun */
569b1dd958fScth tgt = kmem_zalloc(sizeof (emul64_tgt_t), KM_SLEEP);
570b1dd958fScth rw_init(&tgt->emul64_tgt_nw_lock, NULL, RW_DRIVER, NULL);
571b1dd958fScth mutex_init(&tgt->emul64_tgt_blk_lock, NULL, MUTEX_DRIVER, NULL);
572b1dd958fScth
573b1dd958fScth /* create avl for data block storage */
574b1dd958fScth avl_create(&tgt->emul64_tgt_data, emul64_bsd_blkcompare,
57519397407SSherry Moore sizeof (blklist_t), offsetof(blklist_t, bl_node));
576b1dd958fScth
577b1dd958fScth /* save scsi_address and vidpid */
578b1dd958fScth bcopy(sd, &tgt->emul64_tgt_saddr, sizeof (struct scsi_address));
579b1dd958fScth (void) strncpy(tgt->emul64_tgt_inq, vidpid,
58019397407SSherry Moore sizeof (emul64->emul64_tgt->emul64_tgt_inq));
581b1dd958fScth
582b1dd958fScth /*
583b1dd958fScth * The high order 4 bytes of the sector count always come first in
584b1dd958fScth * emul64.conf. They are followed by the low order 4 bytes. Not
585b1dd958fScth * all CPU types want them in this order, but laddr_t takes care of
586b1dd958fScth * this for us. We then pick up geometry (ncyl X nheads X nsect).
587b1dd958fScth */
588b1dd958fScth sector_count._p._u = *(geoip + 0);
589b1dd958fScth sector_count._p._l = *(geoip + 1);
590b1dd958fScth /*
591b1dd958fScth * On 32-bit platforms, fix block size if it's greater than the
592b1dd958fScth * allowable maximum.
593b1dd958fScth */
594b1dd958fScth #if !defined(_LP64)
595b1dd958fScth if (sector_count._f > DK_MAX_BLOCKS)
596b1dd958fScth sector_count._f = DK_MAX_BLOCKS;
597b1dd958fScth #endif
598b1dd958fScth tgt->emul64_tgt_sectors = sector_count._f;
599b1dd958fScth tgt->emul64_tgt_dtype = *(geoip + 2);
600b1dd958fScth tgt->emul64_tgt_ncyls = *(geoip + 3);
601b1dd958fScth tgt->emul64_tgt_nheads = *(geoip + 4);
602b1dd958fScth tgt->emul64_tgt_nsect = *(geoip + 5);
603b1dd958fScth
604b1dd958fScth /* insert target structure into list */
605b1dd958fScth tgt->emul64_tgt_next = emul64->emul64_tgt;
606b1dd958fScth emul64->emul64_tgt = tgt;
607b1dd958fScth ret = DDI_SUCCESS;
608b1dd958fScth
609b1dd958fScth out: EMUL64_MUTEX_EXIT(emul64);
610b1dd958fScth if (geoip)
611b1dd958fScth ddi_prop_free(geoip);
612b1dd958fScth if (geo_vidpid)
613b1dd958fScth ddi_prop_free(geo_vidpid);
614b1dd958fScth return (ret);
615b1dd958fScth }
616b1dd958fScth
617b1dd958fScth /*
618b1dd958fScth * Function name : emul64_i_initcap
619b1dd958fScth *
620b1dd958fScth * Return Values : NONE
621b1dd958fScth * Description : Initializes the default target capabilities and
622b1dd958fScth * Sync Rates.
623b1dd958fScth *
624b1dd958fScth * Context : Called from the user thread through attach.
625b1dd958fScth *
626b1dd958fScth */
627b1dd958fScth static void
emul64_i_initcap(struct emul64 * emul64)628b1dd958fScth emul64_i_initcap(struct emul64 *emul64)
629b1dd958fScth {
630b1dd958fScth uint16_t cap, synch;
631b1dd958fScth int i;
632b1dd958fScth
633b1dd958fScth cap = 0;
634b1dd958fScth synch = 0;
635b1dd958fScth for (i = 0; i < NTARGETS_WIDE; i++) {
636b1dd958fScth emul64->emul64_cap[i] = cap;
637b1dd958fScth emul64->emul64_synch[i] = synch;
638b1dd958fScth }
639b1dd958fScth EMUL64_DEBUG(emul64, SCSI_DEBUG, "default cap = 0x%x", cap);
640b1dd958fScth }
641b1dd958fScth
642b1dd958fScth /*
643b1dd958fScth * Function name : emul64_scsi_getcap()
644b1dd958fScth *
645b1dd958fScth * Return Values : current value of capability, if defined
646b1dd958fScth * -1 if capability is not defined
647b1dd958fScth * Description : returns current capability value
648b1dd958fScth *
649b1dd958fScth * Context : Can be called from different kernel process threads.
650b1dd958fScth * Can be called by interrupt thread.
651b1dd958fScth */
652b1dd958fScth static int
emul64_scsi_getcap(struct scsi_address * ap,char * cap,int whom)653b1dd958fScth emul64_scsi_getcap(struct scsi_address *ap, char *cap, int whom)
654b1dd958fScth {
655b1dd958fScth struct emul64 *emul64 = ADDR2EMUL64(ap);
656b1dd958fScth int rval = 0;
657b1dd958fScth
658b1dd958fScth /*
659b1dd958fScth * We don't allow inquiring about capabilities for other targets
660b1dd958fScth */
661b1dd958fScth if (cap == NULL || whom == 0) {
662b1dd958fScth return (-1);
663b1dd958fScth }
664b1dd958fScth
665b1dd958fScth EMUL64_MUTEX_ENTER(emul64);
666b1dd958fScth
667b1dd958fScth switch (scsi_hba_lookup_capstr(cap)) {
668b1dd958fScth case SCSI_CAP_DMA_MAX:
669b1dd958fScth rval = 1 << 24; /* Limit to 16MB max transfer */
670b1dd958fScth break;
671b1dd958fScth case SCSI_CAP_MSG_OUT:
672b1dd958fScth rval = 1;
673b1dd958fScth break;
674b1dd958fScth case SCSI_CAP_DISCONNECT:
675b1dd958fScth rval = 1;
676b1dd958fScth break;
677b1dd958fScth case SCSI_CAP_SYNCHRONOUS:
678b1dd958fScth rval = 1;
679b1dd958fScth break;
680b1dd958fScth case SCSI_CAP_WIDE_XFER:
681b1dd958fScth rval = 1;
682b1dd958fScth break;
683b1dd958fScth case SCSI_CAP_TAGGED_QING:
684b1dd958fScth rval = 1;
685b1dd958fScth break;
686b1dd958fScth case SCSI_CAP_UNTAGGED_QING:
687b1dd958fScth rval = 1;
688b1dd958fScth break;
689b1dd958fScth case SCSI_CAP_PARITY:
690b1dd958fScth rval = 1;
691b1dd958fScth break;
692b1dd958fScth case SCSI_CAP_INITIATOR_ID:
693b1dd958fScth rval = emul64->emul64_initiator_id;
694b1dd958fScth break;
695b1dd958fScth case SCSI_CAP_ARQ:
696b1dd958fScth rval = 1;
697b1dd958fScth break;
698b1dd958fScth case SCSI_CAP_LINKED_CMDS:
699b1dd958fScth break;
700b1dd958fScth case SCSI_CAP_RESET_NOTIFICATION:
701b1dd958fScth rval = 1;
702b1dd958fScth break;
703b1dd958fScth
704b1dd958fScth default:
705b1dd958fScth rval = -1;
706b1dd958fScth break;
707b1dd958fScth }
708b1dd958fScth
709b1dd958fScth EMUL64_MUTEX_EXIT(emul64);
710b1dd958fScth
711b1dd958fScth return (rval);
712b1dd958fScth }
713b1dd958fScth
714b1dd958fScth /*
715b1dd958fScth * Function name : emul64_scsi_setcap()
716b1dd958fScth *
717b1dd958fScth * Return Values : 1 - capability exists and can be set to new value
718b1dd958fScth * 0 - capability could not be set to new value
719b1dd958fScth * -1 - no such capability
720b1dd958fScth *
721b1dd958fScth * Description : sets a capability for a target
722b1dd958fScth *
723b1dd958fScth * Context : Can be called from different kernel process threads.
724b1dd958fScth * Can be called by interrupt thread.
725b1dd958fScth */
726b1dd958fScth static int
emul64_scsi_setcap(struct scsi_address * ap,char * cap,int value,int whom)727b1dd958fScth emul64_scsi_setcap(struct scsi_address *ap, char *cap, int value, int whom)
728b1dd958fScth {
729b1dd958fScth struct emul64 *emul64 = ADDR2EMUL64(ap);
730b1dd958fScth int rval = 0;
731b1dd958fScth
732b1dd958fScth /*
733b1dd958fScth * We don't allow setting capabilities for other targets
734b1dd958fScth */
735b1dd958fScth if (cap == NULL || whom == 0) {
736b1dd958fScth return (-1);
737b1dd958fScth }
738b1dd958fScth
739b1dd958fScth EMUL64_MUTEX_ENTER(emul64);
740b1dd958fScth
741b1dd958fScth switch (scsi_hba_lookup_capstr(cap)) {
742b1dd958fScth case SCSI_CAP_DMA_MAX:
743b1dd958fScth case SCSI_CAP_MSG_OUT:
744b1dd958fScth case SCSI_CAP_PARITY:
745b1dd958fScth case SCSI_CAP_UNTAGGED_QING:
746b1dd958fScth case SCSI_CAP_LINKED_CMDS:
747b1dd958fScth case SCSI_CAP_RESET_NOTIFICATION:
748b1dd958fScth /*
749b1dd958fScth * None of these are settable via
750b1dd958fScth * the capability interface.
751b1dd958fScth */
752b1dd958fScth break;
753b1dd958fScth case SCSI_CAP_DISCONNECT:
754b1dd958fScth rval = 1;
755b1dd958fScth break;
756b1dd958fScth case SCSI_CAP_SYNCHRONOUS:
757b1dd958fScth rval = 1;
758b1dd958fScth break;
759b1dd958fScth case SCSI_CAP_TAGGED_QING:
760b1dd958fScth rval = 1;
761b1dd958fScth break;
762b1dd958fScth case SCSI_CAP_WIDE_XFER:
763b1dd958fScth rval = 1;
764b1dd958fScth break;
765b1dd958fScth case SCSI_CAP_INITIATOR_ID:
766b1dd958fScth rval = -1;
767b1dd958fScth break;
768b1dd958fScth case SCSI_CAP_ARQ:
769b1dd958fScth rval = 1;
770b1dd958fScth break;
771b1dd958fScth case SCSI_CAP_TOTAL_SECTORS:
772b1dd958fScth emul64->nt_total_sectors[ap->a_target][ap->a_lun] = value;
773b1dd958fScth rval = TRUE;
774b1dd958fScth break;
775b1dd958fScth case SCSI_CAP_SECTOR_SIZE:
776b1dd958fScth rval = TRUE;
777b1dd958fScth break;
778b1dd958fScth default:
779b1dd958fScth rval = -1;
780b1dd958fScth break;
781b1dd958fScth }
782b1dd958fScth
783b1dd958fScth
784b1dd958fScth EMUL64_MUTEX_EXIT(emul64);
785b1dd958fScth
786b1dd958fScth return (rval);
787b1dd958fScth }
788b1dd958fScth
789b1dd958fScth /*
790b1dd958fScth * Function name : emul64_scsi_init_pkt
791b1dd958fScth *
792b1dd958fScth * Return Values : pointer to scsi_pkt, or NULL
793b1dd958fScth * Description : Called by kernel on behalf of a target driver
794b1dd958fScth * calling scsi_init_pkt(9F).
795b1dd958fScth * Refer to tran_init_pkt(9E) man page
796b1dd958fScth *
797b1dd958fScth * Context : Can be called from different kernel process threads.
798b1dd958fScth * Can be called by interrupt thread.
799b1dd958fScth */
800b1dd958fScth /* ARGSUSED */
801b1dd958fScth static struct scsi_pkt *
emul64_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)802b1dd958fScth emul64_scsi_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt,
803*2178a880SToomas Soome struct buf *bp, int cmdlen, int statuslen, int tgtlen,
804*2178a880SToomas Soome int flags, int (*callback)(), caddr_t arg)
805b1dd958fScth {
806b1dd958fScth struct emul64 *emul64 = ADDR2EMUL64(ap);
807b1dd958fScth struct emul64_cmd *sp;
808b1dd958fScth
809b1dd958fScth ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC);
810b1dd958fScth
811b1dd958fScth /*
812b1dd958fScth * First step of emul64_scsi_init_pkt: pkt allocation
813b1dd958fScth */
814b1dd958fScth if (pkt == NULL) {
815b1dd958fScth pkt = scsi_hba_pkt_alloc(emul64->emul64_dip, ap, cmdlen,
81619397407SSherry Moore statuslen,
81719397407SSherry Moore tgtlen, sizeof (struct emul64_cmd), callback, arg);
818b1dd958fScth if (pkt == NULL) {
819b1dd958fScth cmn_err(CE_WARN, "emul64_scsi_init_pkt: "
82019397407SSherry Moore "scsi_hba_pkt_alloc failed");
821b1dd958fScth return (NULL);
822b1dd958fScth }
823b1dd958fScth
824b1dd958fScth sp = PKT2CMD(pkt);
825b1dd958fScth
826b1dd958fScth /*
827b1dd958fScth * Initialize the new pkt - we redundantly initialize
828b1dd958fScth * all the fields for illustrative purposes.
829b1dd958fScth */
830b1dd958fScth sp->cmd_pkt = pkt;
831b1dd958fScth sp->cmd_flags = 0;
832b1dd958fScth sp->cmd_scblen = statuslen;
833b1dd958fScth sp->cmd_cdblen = cmdlen;
834b1dd958fScth sp->cmd_emul64 = emul64;
835b1dd958fScth pkt->pkt_address = *ap;
836b1dd958fScth pkt->pkt_comp = (void (*)())NULL;
837b1dd958fScth pkt->pkt_flags = 0;
838b1dd958fScth pkt->pkt_time = 0;
839b1dd958fScth pkt->pkt_resid = 0;
840b1dd958fScth pkt->pkt_statistics = 0;
841b1dd958fScth pkt->pkt_reason = 0;
842b1dd958fScth
843b1dd958fScth } else {
844b1dd958fScth sp = PKT2CMD(pkt);
845b1dd958fScth }
846b1dd958fScth
847b1dd958fScth /*
848b1dd958fScth * Second step of emul64_scsi_init_pkt: dma allocation/move
849b1dd958fScth */
850b1dd958fScth if (bp && bp->b_bcount != 0) {
851b1dd958fScth if (bp->b_flags & B_READ) {
852b1dd958fScth sp->cmd_flags &= ~CFLAG_DMASEND;
853b1dd958fScth } else {
854b1dd958fScth sp->cmd_flags |= CFLAG_DMASEND;
855b1dd958fScth }
856b1dd958fScth bp_mapin(bp);
857b1dd958fScth sp->cmd_addr = (unsigned char *) bp->b_un.b_addr;
858b1dd958fScth sp->cmd_count = bp->b_bcount;
859b1dd958fScth pkt->pkt_resid = 0;
860b1dd958fScth }
861b1dd958fScth
862b1dd958fScth return (pkt);
863b1dd958fScth }
864b1dd958fScth
865b1dd958fScth
866b1dd958fScth /*
867b1dd958fScth * Function name : emul64_scsi_destroy_pkt
868b1dd958fScth *
869b1dd958fScth * Return Values : none
870b1dd958fScth * Description : Called by kernel on behalf of a target driver
871b1dd958fScth * calling scsi_destroy_pkt(9F).
872b1dd958fScth * Refer to tran_destroy_pkt(9E) man page
873b1dd958fScth *
874b1dd958fScth * Context : Can be called from different kernel process threads.
875b1dd958fScth * Can be called by interrupt thread.
876b1dd958fScth */
877b1dd958fScth static void
emul64_scsi_destroy_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)878b1dd958fScth emul64_scsi_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
879b1dd958fScth {
880b1dd958fScth struct emul64_cmd *sp = PKT2CMD(pkt);
881b1dd958fScth
882b1dd958fScth /*
883b1dd958fScth * emul64_scsi_dmafree inline to make things faster
884b1dd958fScth */
885b1dd958fScth if (sp->cmd_flags & CFLAG_DMAVALID) {
886b1dd958fScth /*
887b1dd958fScth * Free the mapping.
888b1dd958fScth */
889b1dd958fScth sp->cmd_flags &= ~CFLAG_DMAVALID;
890b1dd958fScth }
891b1dd958fScth
892b1dd958fScth /*
893b1dd958fScth * Free the pkt
894b1dd958fScth */
895b1dd958fScth scsi_hba_pkt_free(ap, pkt);
896b1dd958fScth }
897b1dd958fScth
898b1dd958fScth
899b1dd958fScth /*
900b1dd958fScth * Function name : emul64_scsi_dmafree()
901b1dd958fScth *
902b1dd958fScth * Return Values : none
903b1dd958fScth * Description : free dvma resources
904b1dd958fScth *
905b1dd958fScth * Context : Can be called from different kernel process threads.
906b1dd958fScth * Can be called by interrupt thread.
907b1dd958fScth */
908b1dd958fScth /*ARGSUSED*/
909b1dd958fScth static void
emul64_scsi_dmafree(struct scsi_address * ap,struct scsi_pkt * pkt)910b1dd958fScth emul64_scsi_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt)
911b1dd958fScth {
912b1dd958fScth }
913b1dd958fScth
914b1dd958fScth /*
915b1dd958fScth * Function name : emul64_scsi_sync_pkt()
916b1dd958fScth *
917b1dd958fScth * Return Values : none
918b1dd958fScth * Description : sync dma
919b1dd958fScth *
920b1dd958fScth * Context : Can be called from different kernel process threads.
921b1dd958fScth * Can be called by interrupt thread.
922b1dd958fScth */
923b1dd958fScth /*ARGSUSED*/
924b1dd958fScth static void
emul64_scsi_sync_pkt(struct scsi_address * ap,struct scsi_pkt * pkt)925b1dd958fScth emul64_scsi_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt)
926b1dd958fScth {
927b1dd958fScth }
928b1dd958fScth
929b1dd958fScth /*
930b1dd958fScth * routine for reset notification setup, to register or cancel.
931b1dd958fScth */
932b1dd958fScth static int
emul64_scsi_reset_notify(struct scsi_address * ap,int flag,void (* callback)(caddr_t),caddr_t arg)933b1dd958fScth emul64_scsi_reset_notify(struct scsi_address *ap, int flag,
934*2178a880SToomas Soome void (*callback)(caddr_t), caddr_t arg)
935b1dd958fScth {
936b1dd958fScth struct emul64 *emul64 = ADDR2EMUL64(ap);
937b1dd958fScth struct emul64_reset_notify_entry *p, *beforep;
938b1dd958fScth int rval = DDI_FAILURE;
939b1dd958fScth
940b1dd958fScth mutex_enter(EMUL64_REQ_MUTEX(emul64));
941b1dd958fScth
942b1dd958fScth p = emul64->emul64_reset_notify_listf;
943b1dd958fScth beforep = NULL;
944b1dd958fScth
945b1dd958fScth while (p) {
946b1dd958fScth if (p->ap == ap)
947b1dd958fScth break; /* An entry exists for this target */
948b1dd958fScth beforep = p;
949b1dd958fScth p = p->next;
950b1dd958fScth }
951b1dd958fScth
952b1dd958fScth if ((flag & SCSI_RESET_CANCEL) && (p != NULL)) {
953b1dd958fScth if (beforep == NULL) {
954b1dd958fScth emul64->emul64_reset_notify_listf = p->next;
955b1dd958fScth } else {
956b1dd958fScth beforep->next = p->next;
957b1dd958fScth }
958b1dd958fScth kmem_free((caddr_t)p,
95919397407SSherry Moore sizeof (struct emul64_reset_notify_entry));
960b1dd958fScth rval = DDI_SUCCESS;
961b1dd958fScth
962b1dd958fScth } else if ((flag & SCSI_RESET_NOTIFY) && (p == NULL)) {
963b1dd958fScth p = kmem_zalloc(sizeof (struct emul64_reset_notify_entry),
96419397407SSherry Moore KM_SLEEP);
965b1dd958fScth p->ap = ap;
966b1dd958fScth p->callback = callback;
967b1dd958fScth p->arg = arg;
968b1dd958fScth p->next = emul64->emul64_reset_notify_listf;
969b1dd958fScth emul64->emul64_reset_notify_listf = p;
970b1dd958fScth rval = DDI_SUCCESS;
971b1dd958fScth }
972b1dd958fScth
973b1dd958fScth mutex_exit(EMUL64_REQ_MUTEX(emul64));
974b1dd958fScth
975b1dd958fScth return (rval);
976b1dd958fScth }
977b1dd958fScth
978b1dd958fScth /*
979b1dd958fScth * Function name : emul64_scsi_start()
980b1dd958fScth *
981b1dd958fScth * Return Values : TRAN_FATAL_ERROR - emul64 has been shutdown
982b1dd958fScth * TRAN_BUSY - request queue is full
983b1dd958fScth * TRAN_ACCEPT - pkt has been submitted to emul64
984b1dd958fScth *
985b1dd958fScth * Description : init pkt, start the request
986b1dd958fScth *
987b1dd958fScth * Context : Can be called from different kernel process threads.
988b1dd958fScth * Can be called by interrupt thread.
989b1dd958fScth */
990b1dd958fScth static int
emul64_scsi_start(struct scsi_address * ap,struct scsi_pkt * pkt)991b1dd958fScth emul64_scsi_start(struct scsi_address *ap, struct scsi_pkt *pkt)
992b1dd958fScth {
993b1dd958fScth struct emul64_cmd *sp = PKT2CMD(pkt);
994b1dd958fScth int rval = TRAN_ACCEPT;
995b1dd958fScth struct emul64 *emul64 = ADDR2EMUL64(ap);
996b1dd958fScth clock_t cur_lbolt;
997b1dd958fScth taskqid_t dispatched;
998b1dd958fScth
999b1dd958fScth ASSERT(mutex_owned(EMUL64_REQ_MUTEX(emul64)) == 0 || ddi_in_panic());
1000b1dd958fScth ASSERT(mutex_owned(EMUL64_RESP_MUTEX(emul64)) == 0 || ddi_in_panic());
1001b1dd958fScth
1002b1dd958fScth EMUL64_DEBUG2(emul64, SCSI_DEBUG, "emul64_scsi_start %x", sp);
1003b1dd958fScth
1004b1dd958fScth pkt->pkt_reason = CMD_CMPLT;
1005b1dd958fScth
1006b1dd958fScth #ifdef EMUL64DEBUG
1007b1dd958fScth if (emul64_cdb_debug) {
1008b1dd958fScth emul64_debug_dump_cdb(ap, pkt);
1009b1dd958fScth }
1010b1dd958fScth #endif /* EMUL64DEBUG */
1011b1dd958fScth
1012b1dd958fScth /*
1013b1dd958fScth * calculate deadline from pkt_time
1014b1dd958fScth * Instead of multiplying by 100 (ie. HZ), we multiply by 128 so
1015b1dd958fScth * we can shift and at the same time have a 28% grace period
1016b1dd958fScth * we ignore the rare case of pkt_time == 0 and deal with it
1017b1dd958fScth * in emul64_i_watch()
1018b1dd958fScth */
1019b1dd958fScth cur_lbolt = ddi_get_lbolt();
1020b1dd958fScth sp->cmd_deadline = cur_lbolt + (pkt->pkt_time * 128);
1021b1dd958fScth
1022b1dd958fScth if ((emul64_usetaskq == 0) || (pkt->pkt_flags & FLAG_NOINTR) != 0) {
1023b1dd958fScth emul64_pkt_comp((caddr_t)pkt);
1024b1dd958fScth } else {
1025fc8ae2ecSToomas Soome dispatched = TASKQID_INVALID;
1026b1dd958fScth if (emul64_collect_stats) {
1027b1dd958fScth /*
1028b1dd958fScth * If we are collecting statistics, call
1029b1dd958fScth * taskq_dispatch in no sleep mode, so that we can
1030b1dd958fScth * detect if we are exceeding the queue length that
1031b1dd958fScth * was established in the call to taskq_create in
1032b1dd958fScth * emul64_attach. If the no sleep call fails
1033b1dd958fScth * (returns NULL), the task will be dispatched in
1034b1dd958fScth * sleep mode below.
1035b1dd958fScth */
1036b1dd958fScth dispatched = taskq_dispatch(emul64->emul64_taskq,
103719397407SSherry Moore emul64_pkt_comp, (void *)pkt, TQ_NOSLEEP);
1038fc8ae2ecSToomas Soome if (dispatched == TASKQID_INVALID) {
1039b1dd958fScth /* Queue was full. dispatch failed. */
1040b1dd958fScth mutex_enter(&emul64_stats_mutex);
1041b1dd958fScth emul64_taskq_max++;
1042b1dd958fScth mutex_exit(&emul64_stats_mutex);
1043b1dd958fScth }
1044b1dd958fScth }
1045fc8ae2ecSToomas Soome if (dispatched == TASKQID_INVALID) {
1046b1dd958fScth (void) taskq_dispatch(emul64->emul64_taskq,
104719397407SSherry Moore emul64_pkt_comp, (void *)pkt, TQ_SLEEP);
1048b1dd958fScth }
1049b1dd958fScth }
1050b1dd958fScth
1051b1dd958fScth done:
1052b1dd958fScth ASSERT(mutex_owned(EMUL64_REQ_MUTEX(emul64)) == 0 || ddi_in_panic());
1053b1dd958fScth ASSERT(mutex_owned(EMUL64_RESP_MUTEX(emul64)) == 0 || ddi_in_panic());
1054b1dd958fScth
1055b1dd958fScth return (rval);
1056b1dd958fScth }
1057b1dd958fScth
1058b1dd958fScth void
emul64_check_cond(struct scsi_pkt * pkt,uchar_t key,uchar_t asc,uchar_t ascq)1059b1dd958fScth emul64_check_cond(struct scsi_pkt *pkt, uchar_t key, uchar_t asc, uchar_t ascq)
1060b1dd958fScth {
1061b1dd958fScth struct scsi_arq_status *arq =
106219397407SSherry Moore (struct scsi_arq_status *)pkt->pkt_scbp;
1063b1dd958fScth
1064b1dd958fScth /* got check, no data transferred and ARQ done */
1065b1dd958fScth arq->sts_status.sts_chk = 1;
1066b1dd958fScth pkt->pkt_state |= STATE_ARQ_DONE;
1067b1dd958fScth pkt->pkt_state &= ~STATE_XFERRED_DATA;
1068b1dd958fScth
1069b1dd958fScth /* for ARQ */
1070b1dd958fScth arq->sts_rqpkt_reason = CMD_CMPLT;
1071b1dd958fScth arq->sts_rqpkt_resid = 0;
1072b1dd958fScth arq->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
1073b1dd958fScth STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
1074b1dd958fScth arq->sts_sensedata.es_valid = 1;
1075b1dd958fScth arq->sts_sensedata.es_class = 0x7;
1076b1dd958fScth arq->sts_sensedata.es_key = key;
1077b1dd958fScth arq->sts_sensedata.es_add_code = asc;
1078b1dd958fScth arq->sts_sensedata.es_qual_code = ascq;
1079b1dd958fScth }
1080b1dd958fScth
1081cefe316eSpd ushort_t
emul64_error_inject(struct scsi_pkt * pkt)1082cefe316eSpd emul64_error_inject(struct scsi_pkt *pkt)
1083cefe316eSpd {
1084cefe316eSpd struct emul64_cmd *sp = PKT2CMD(pkt);
1085cefe316eSpd emul64_tgt_t *tgt;
1086cefe316eSpd struct scsi_arq_status *arq =
108719397407SSherry Moore (struct scsi_arq_status *)pkt->pkt_scbp;
1088cefe316eSpd uint_t max_sense_len;
1089cefe316eSpd
1090cefe316eSpd EMUL64_MUTEX_ENTER(sp->cmd_emul64);
1091cefe316eSpd tgt = find_tgt(sp->cmd_emul64,
109219397407SSherry Moore pkt->pkt_address.a_target, pkt->pkt_address.a_lun);
1093cefe316eSpd EMUL64_MUTEX_EXIT(sp->cmd_emul64);
1094cefe316eSpd
1095cefe316eSpd /*
1096cefe316eSpd * If there is no target, skip the error injection and
1097cefe316eSpd * let the packet be handled normally. This would normally
1098cefe316eSpd * never happen since a_target and a_lun are setup in
1099cefe316eSpd * emul64_scsi_init_pkt.
1100cefe316eSpd */
1101cefe316eSpd if (tgt == NULL) {
1102cefe316eSpd return (ERR_INJ_DISABLE);
1103cefe316eSpd }
1104cefe316eSpd
1105cefe316eSpd if (tgt->emul64_einj_state != ERR_INJ_DISABLE) {
1106cefe316eSpd arq->sts_status = tgt->emul64_einj_scsi_status;
1107cefe316eSpd pkt->pkt_state = tgt->emul64_einj_pkt_state;
1108cefe316eSpd pkt->pkt_reason = tgt->emul64_einj_pkt_reason;
1109cefe316eSpd
1110cefe316eSpd /*
1111cefe316eSpd * Calculate available sense buffer length. We could just
1112cefe316eSpd * assume sizeof(struct scsi_extended_sense) but hopefully
1113cefe316eSpd * that limitation will go away soon.
1114cefe316eSpd */
1115cefe316eSpd max_sense_len = sp->cmd_scblen -
1116cefe316eSpd (sizeof (struct scsi_arq_status) -
111719397407SSherry Moore sizeof (struct scsi_extended_sense));
1118cefe316eSpd if (max_sense_len > tgt->emul64_einj_sense_length) {
1119cefe316eSpd max_sense_len = tgt->emul64_einj_sense_length;
1120cefe316eSpd }
1121cefe316eSpd
1122cefe316eSpd /* for ARQ */
1123cefe316eSpd arq->sts_rqpkt_reason = CMD_CMPLT;
1124cefe316eSpd arq->sts_rqpkt_resid = 0;
1125cefe316eSpd arq->sts_rqpkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
1126cefe316eSpd STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
1127cefe316eSpd
1128cefe316eSpd /* Copy sense data */
1129cefe316eSpd if (tgt->emul64_einj_sense_data != 0) {
1130cefe316eSpd bcopy(tgt->emul64_einj_sense_data,
1131cefe316eSpd (uint8_t *)&arq->sts_sensedata,
1132cefe316eSpd max_sense_len);
1133cefe316eSpd }
1134cefe316eSpd }
1135cefe316eSpd
1136cefe316eSpd /* Return current error injection state */
1137cefe316eSpd return (tgt->emul64_einj_state);
1138cefe316eSpd }
1139cefe316eSpd
1140cefe316eSpd int
emul64_error_inject_req(struct emul64 * emul64,intptr_t arg)1141cefe316eSpd emul64_error_inject_req(struct emul64 *emul64, intptr_t arg)
1142cefe316eSpd {
1143cefe316eSpd emul64_tgt_t *tgt;
1144cefe316eSpd struct emul64_error_inj_data error_inj_req;
1145cefe316eSpd
1146cefe316eSpd /* Check args */
1147*2178a880SToomas Soome if (arg == (intptr_t)NULL) {
1148cefe316eSpd return (EINVAL);
1149cefe316eSpd }
1150cefe316eSpd
1151cefe316eSpd if (ddi_copyin((void *)arg, &error_inj_req,
1152cefe316eSpd sizeof (error_inj_req), 0) != 0) {
1153cefe316eSpd cmn_err(CE_WARN, "emul64: ioctl - inj copyin failed\n");
1154cefe316eSpd return (EFAULT);
1155cefe316eSpd }
1156cefe316eSpd
1157cefe316eSpd EMUL64_MUTEX_ENTER(emul64);
1158cefe316eSpd tgt = find_tgt(emul64, error_inj_req.eccd_target,
1159cefe316eSpd error_inj_req.eccd_lun);
1160cefe316eSpd EMUL64_MUTEX_EXIT(emul64);
1161cefe316eSpd
1162cefe316eSpd /* Make sure device exists */
1163cefe316eSpd if (tgt == NULL) {
1164cefe316eSpd return (ENODEV);
1165cefe316eSpd }
1166cefe316eSpd
1167cefe316eSpd /* Free old sense buffer if we have one */
1168cefe316eSpd if (tgt->emul64_einj_sense_data != NULL) {
1169cefe316eSpd ASSERT(tgt->emul64_einj_sense_length != 0);
1170cefe316eSpd kmem_free(tgt->emul64_einj_sense_data,
1171cefe316eSpd tgt->emul64_einj_sense_length);
1172cefe316eSpd tgt->emul64_einj_sense_data = NULL;
1173cefe316eSpd tgt->emul64_einj_sense_length = 0;
1174cefe316eSpd }
1175cefe316eSpd
1176cefe316eSpd /*
1177cefe316eSpd * Now handle error injection request. If error injection
1178cefe316eSpd * is requested we will return the sense data provided for
1179cefe316eSpd * any I/O to this target until told to stop.
1180cefe316eSpd */
1181cefe316eSpd tgt->emul64_einj_state = error_inj_req.eccd_inj_state;
1182cefe316eSpd tgt->emul64_einj_sense_length = error_inj_req.eccd_sns_dlen;
1183cefe316eSpd tgt->emul64_einj_pkt_state = error_inj_req.eccd_pkt_state;
1184cefe316eSpd tgt->emul64_einj_pkt_reason = error_inj_req.eccd_pkt_reason;
1185cefe316eSpd tgt->emul64_einj_scsi_status = error_inj_req.eccd_scsi_status;
1186cefe316eSpd switch (error_inj_req.eccd_inj_state) {
1187cefe316eSpd case ERR_INJ_ENABLE:
1188cefe316eSpd case ERR_INJ_ENABLE_NODATA:
1189cefe316eSpd if (error_inj_req.eccd_sns_dlen) {
1190cefe316eSpd tgt->emul64_einj_sense_data =
1191cefe316eSpd kmem_alloc(error_inj_req.eccd_sns_dlen, KM_SLEEP);
1192cefe316eSpd /* Copy sense data */
1193cefe316eSpd if (ddi_copyin((void *)(arg + sizeof (error_inj_req)),
119419397407SSherry Moore tgt->emul64_einj_sense_data,
119519397407SSherry Moore error_inj_req.eccd_sns_dlen, 0) != 0) {
1196cefe316eSpd cmn_err(CE_WARN,
1197cefe316eSpd "emul64: sense data copy in failed\n");
1198cefe316eSpd return (EFAULT);
1199cefe316eSpd }
1200cefe316eSpd }
1201cefe316eSpd break;
1202cefe316eSpd case ERR_INJ_DISABLE:
1203cefe316eSpd default:
1204cefe316eSpd break;
1205cefe316eSpd }
1206cefe316eSpd
1207cefe316eSpd return (0);
1208cefe316eSpd }
1209cefe316eSpd
1210b1dd958fScth int bsd_scsi_start_stop_unit(struct scsi_pkt *);
1211b1dd958fScth int bsd_scsi_test_unit_ready(struct scsi_pkt *);
1212b1dd958fScth int bsd_scsi_request_sense(struct scsi_pkt *);
1213b1dd958fScth int bsd_scsi_inquiry(struct scsi_pkt *);
1214b1dd958fScth int bsd_scsi_format(struct scsi_pkt *);
1215b1dd958fScth int bsd_scsi_io(struct scsi_pkt *);
1216b1dd958fScth int bsd_scsi_log_sense(struct scsi_pkt *);
1217b1dd958fScth int bsd_scsi_mode_sense(struct scsi_pkt *);
1218b1dd958fScth int bsd_scsi_mode_select(struct scsi_pkt *);
1219b1dd958fScth int bsd_scsi_read_capacity(struct scsi_pkt *);
1220b1dd958fScth int bsd_scsi_read_capacity_16(struct scsi_pkt *);
1221b1dd958fScth int bsd_scsi_reserve(struct scsi_pkt *);
1222b1dd958fScth int bsd_scsi_format(struct scsi_pkt *);
1223b1dd958fScth int bsd_scsi_release(struct scsi_pkt *);
1224b1dd958fScth int bsd_scsi_read_defect_list(struct scsi_pkt *);
1225b1dd958fScth int bsd_scsi_reassign_block(struct scsi_pkt *);
1226b1dd958fScth int bsd_freeblkrange(emul64_tgt_t *, emul64_range_t *);
1227b1dd958fScth
1228b1dd958fScth static void
emul64_handle_cmd(struct scsi_pkt * pkt)1229b1dd958fScth emul64_handle_cmd(struct scsi_pkt *pkt)
1230b1dd958fScth {
1231cefe316eSpd if (emul64_error_inject(pkt) == ERR_INJ_ENABLE_NODATA) {
1232cefe316eSpd /*
1233cefe316eSpd * If error injection is configured to return with
1234cefe316eSpd * no data return now without handling the command.
1235cefe316eSpd * This is how normal check conditions work.
1236cefe316eSpd *
1237cefe316eSpd * If the error injection state is ERR_INJ_ENABLE
1238cefe316eSpd * (or if error injection is disabled) continue and
1239cefe316eSpd * handle the command. This would be used for
1240cefe316eSpd * KEY_RECOVERABLE_ERROR type conditions.
1241cefe316eSpd */
1242cefe316eSpd return;
1243cefe316eSpd }
1244cefe316eSpd
1245b1dd958fScth switch (pkt->pkt_cdbp[0]) {
1246b1dd958fScth case SCMD_START_STOP:
1247b1dd958fScth (void) bsd_scsi_start_stop_unit(pkt);
1248b1dd958fScth break;
1249b1dd958fScth case SCMD_TEST_UNIT_READY:
1250b1dd958fScth (void) bsd_scsi_test_unit_ready(pkt);
1251b1dd958fScth break;
1252b1dd958fScth case SCMD_REQUEST_SENSE:
1253b1dd958fScth (void) bsd_scsi_request_sense(pkt);
1254b1dd958fScth break;
1255b1dd958fScth case SCMD_INQUIRY:
1256b1dd958fScth (void) bsd_scsi_inquiry(pkt);
1257b1dd958fScth break;
1258b1dd958fScth case SCMD_FORMAT:
1259b1dd958fScth (void) bsd_scsi_format(pkt);
1260b1dd958fScth break;
1261b1dd958fScth case SCMD_READ:
1262b1dd958fScth case SCMD_WRITE:
1263b1dd958fScth case SCMD_READ_G1:
1264b1dd958fScth case SCMD_WRITE_G1:
1265b1dd958fScth case SCMD_READ_G4:
1266b1dd958fScth case SCMD_WRITE_G4:
1267b1dd958fScth (void) bsd_scsi_io(pkt);
1268b1dd958fScth break;
1269b1dd958fScth case SCMD_LOG_SENSE_G1:
1270b1dd958fScth (void) bsd_scsi_log_sense(pkt);
1271b1dd958fScth break;
1272b1dd958fScth case SCMD_MODE_SENSE:
1273b1dd958fScth case SCMD_MODE_SENSE_G1:
1274b1dd958fScth (void) bsd_scsi_mode_sense(pkt);
1275b1dd958fScth break;
1276b1dd958fScth case SCMD_MODE_SELECT:
1277b1dd958fScth case SCMD_MODE_SELECT_G1:
1278b1dd958fScth (void) bsd_scsi_mode_select(pkt);
1279b1dd958fScth break;
1280b1dd958fScth case SCMD_READ_CAPACITY:
1281b1dd958fScth (void) bsd_scsi_read_capacity(pkt);
1282b1dd958fScth break;
1283b1dd958fScth case SCMD_SVC_ACTION_IN_G4:
1284b1dd958fScth if (pkt->pkt_cdbp[1] == SSVC_ACTION_READ_CAPACITY_G4) {
1285b1dd958fScth (void) bsd_scsi_read_capacity_16(pkt);
1286b1dd958fScth } else {
1287b1dd958fScth cmn_err(CE_WARN, "emul64: unrecognized G4 service "
128819397407SSherry Moore "action 0x%x", pkt->pkt_cdbp[1]);
1289b1dd958fScth }
1290b1dd958fScth break;
1291b1dd958fScth case SCMD_RESERVE:
1292b1dd958fScth case SCMD_RESERVE_G1:
1293b1dd958fScth (void) bsd_scsi_reserve(pkt);
1294b1dd958fScth break;
1295b1dd958fScth case SCMD_RELEASE:
1296b1dd958fScth case SCMD_RELEASE_G1:
1297b1dd958fScth (void) bsd_scsi_release(pkt);
1298b1dd958fScth break;
1299b1dd958fScth case SCMD_REASSIGN_BLOCK:
1300b1dd958fScth (void) bsd_scsi_reassign_block(pkt);
1301b1dd958fScth break;
1302b1dd958fScth case SCMD_READ_DEFECT_LIST:
1303b1dd958fScth (void) bsd_scsi_read_defect_list(pkt);
1304b1dd958fScth break;
1305b1dd958fScth case SCMD_PRIN:
1306b1dd958fScth case SCMD_PROUT:
1307b1dd958fScth case SCMD_REPORT_LUNS:
1308b1dd958fScth /* ASC 0x24 INVALID FIELD IN CDB */
1309b1dd958fScth emul64_check_cond(pkt, KEY_ILLEGAL_REQUEST, 0x24, 0x0);
1310b1dd958fScth break;
1311b1dd958fScth default:
1312b1dd958fScth cmn_err(CE_WARN, "emul64: unrecognized "
1313b1dd958fScth "SCSI cmd 0x%x", pkt->pkt_cdbp[0]);
1314b1dd958fScth emul64_check_cond(pkt, KEY_ILLEGAL_REQUEST, 0x24, 0x0);
1315b1dd958fScth break;
1316b1dd958fScth case SCMD_GET_CONFIGURATION:
1317b1dd958fScth case 0x35: /* SCMD_SYNCHRONIZE_CACHE */
1318b1dd958fScth /* Don't complain */
1319b1dd958fScth break;
1320b1dd958fScth }
1321b1dd958fScth }
1322b1dd958fScth
1323b1dd958fScth static void
emul64_pkt_comp(void * arg)1324b1dd958fScth emul64_pkt_comp(void * arg)
1325b1dd958fScth {
1326b1dd958fScth struct scsi_pkt *pkt = (struct scsi_pkt *)arg;
1327b1dd958fScth struct emul64_cmd *sp = PKT2CMD(pkt);
1328b1dd958fScth emul64_tgt_t *tgt;
1329b1dd958fScth
1330b1dd958fScth EMUL64_MUTEX_ENTER(sp->cmd_emul64);
1331b1dd958fScth tgt = find_tgt(sp->cmd_emul64,
133219397407SSherry Moore pkt->pkt_address.a_target, pkt->pkt_address.a_lun);
1333b1dd958fScth EMUL64_MUTEX_EXIT(sp->cmd_emul64);
1334b1dd958fScth if (!tgt) {
1335b1dd958fScth pkt->pkt_reason = CMD_TIMEOUT;
1336b1dd958fScth pkt->pkt_state = STATE_GOT_BUS | STATE_SENT_CMD;
1337b1dd958fScth pkt->pkt_statistics = STAT_TIMEOUT;
1338b1dd958fScth } else {
1339b1dd958fScth pkt->pkt_reason = CMD_CMPLT;
1340b1dd958fScth *pkt->pkt_scbp = STATUS_GOOD;
1341b1dd958fScth pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
1342b1dd958fScth STATE_SENT_CMD | STATE_XFERRED_DATA | STATE_GOT_STATUS;
1343b1dd958fScth pkt->pkt_statistics = 0;
1344b1dd958fScth emul64_handle_cmd(pkt);
1345b1dd958fScth }
13469c57abc8Ssrivijitha dugganapalli scsi_hba_pkt_comp(pkt);
1347b1dd958fScth }
1348b1dd958fScth
1349b1dd958fScth /* ARGSUSED */
1350b1dd958fScth static int
emul64_scsi_abort(struct scsi_address * ap,struct scsi_pkt * pkt)1351b1dd958fScth emul64_scsi_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
1352b1dd958fScth {
1353b1dd958fScth return (1);
1354b1dd958fScth }
1355b1dd958fScth
1356b1dd958fScth /* ARGSUSED */
1357b1dd958fScth static int
emul64_scsi_reset(struct scsi_address * ap,int level)1358b1dd958fScth emul64_scsi_reset(struct scsi_address *ap, int level)
1359b1dd958fScth {
1360b1dd958fScth return (1);
1361b1dd958fScth }
1362b1dd958fScth
1363b1dd958fScth static int
emul64_get_tgtrange(struct emul64 * emul64,intptr_t arg,emul64_tgt_t ** tgtp,emul64_tgt_range_t * tgtr)1364*2178a880SToomas Soome emul64_get_tgtrange(struct emul64 *emul64, intptr_t arg, emul64_tgt_t **tgtp,
1365*2178a880SToomas Soome emul64_tgt_range_t *tgtr)
1366b1dd958fScth {
1367b1dd958fScth if (ddi_copyin((void *)arg, tgtr, sizeof (*tgtr), 0) != 0) {
1368b1dd958fScth cmn_err(CE_WARN, "emul64: ioctl - copy in failed\n");
1369b1dd958fScth return (EFAULT);
1370b1dd958fScth }
1371b1dd958fScth EMUL64_MUTEX_ENTER(emul64);
1372b1dd958fScth *tgtp = find_tgt(emul64, tgtr->emul64_target, tgtr->emul64_lun);
1373b1dd958fScth EMUL64_MUTEX_EXIT(emul64);
1374b1dd958fScth if (*tgtp == NULL) {
1375b1dd958fScth cmn_err(CE_WARN, "emul64: ioctl - no target for %d,%d on %d",
137619397407SSherry Moore tgtr->emul64_target, tgtr->emul64_lun,
137719397407SSherry Moore ddi_get_instance(emul64->emul64_dip));
1378b1dd958fScth return (ENXIO);
1379b1dd958fScth }
1380b1dd958fScth return (0);
1381b1dd958fScth }
1382b1dd958fScth
1383b1dd958fScth static int
emul64_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)1384*2178a880SToomas Soome emul64_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
1385*2178a880SToomas Soome int *rvalp)
1386b1dd958fScth {
1387b1dd958fScth struct emul64 *emul64;
1388b1dd958fScth int instance;
1389b1dd958fScth int rv = 0;
1390b1dd958fScth emul64_tgt_range_t tgtr;
1391b1dd958fScth emul64_tgt_t *tgt;
1392b1dd958fScth
1393b1dd958fScth instance = MINOR2INST(getminor(dev));
1394b1dd958fScth emul64 = (struct emul64 *)ddi_get_soft_state(emul64_state, instance);
1395b1dd958fScth if (emul64 == NULL) {
1396b1dd958fScth cmn_err(CE_WARN, "emul64: ioctl - no softstate for %d\n",
139719397407SSherry Moore getminor(dev));
1398b1dd958fScth return (ENXIO);
1399b1dd958fScth }
1400b1dd958fScth
1401b1dd958fScth switch (cmd) {
1402b1dd958fScth case EMUL64_WRITE_OFF:
1403b1dd958fScth rv = emul64_get_tgtrange(emul64, arg, &tgt, &tgtr);
1404b1dd958fScth if (rv == 0) {
1405b1dd958fScth rv = emul64_write_off(emul64, tgt, &tgtr);
1406b1dd958fScth }
1407b1dd958fScth break;
1408b1dd958fScth case EMUL64_WRITE_ON:
1409b1dd958fScth rv = emul64_get_tgtrange(emul64, arg, &tgt, &tgtr);
1410b1dd958fScth if (rv == 0) {
1411b1dd958fScth rv = emul64_write_on(emul64, tgt, &tgtr);
1412b1dd958fScth }
1413b1dd958fScth break;
1414b1dd958fScth case EMUL64_ZERO_RANGE:
1415b1dd958fScth rv = emul64_get_tgtrange(emul64, arg, &tgt, &tgtr);
1416b1dd958fScth if (rv == 0) {
1417b1dd958fScth mutex_enter(&tgt->emul64_tgt_blk_lock);
1418b1dd958fScth rv = bsd_freeblkrange(tgt, &tgtr.emul64_blkrange);
1419b1dd958fScth mutex_exit(&tgt->emul64_tgt_blk_lock);
1420b1dd958fScth }
1421b1dd958fScth break;
1422cefe316eSpd case EMUL64_ERROR_INJECT:
1423cefe316eSpd rv = emul64_error_inject_req(emul64, arg);
1424cefe316eSpd break;
1425b1dd958fScth default:
1426b1dd958fScth rv = scsi_hba_ioctl(dev, cmd, arg, mode, credp, rvalp);
1427b1dd958fScth break;
1428b1dd958fScth }
1429b1dd958fScth return (rv);
1430b1dd958fScth }
1431b1dd958fScth
1432b1dd958fScth /* ARGSUSED */
1433b1dd958fScth static int
emul64_write_off(struct emul64 * emul64,emul64_tgt_t * tgt,emul64_tgt_range_t * tgtr)1434*2178a880SToomas Soome emul64_write_off(struct emul64 *emul64, emul64_tgt_t *tgt,
1435*2178a880SToomas Soome emul64_tgt_range_t *tgtr)
1436b1dd958fScth {
1437b1dd958fScth size_t blkcnt = tgtr->emul64_blkrange.emul64_blkcnt;
1438b1dd958fScth emul64_nowrite_t *cur;
1439b1dd958fScth emul64_nowrite_t *nowrite;
1440b1dd958fScth emul64_rng_overlap_t overlap = O_NONE;
1441b1dd958fScth emul64_nowrite_t **prev = NULL;
1442b1dd958fScth diskaddr_t sb = tgtr->emul64_blkrange.emul64_sb;
1443b1dd958fScth
1444b1dd958fScth nowrite = emul64_nowrite_alloc(&tgtr->emul64_blkrange);
1445b1dd958fScth
1446b1dd958fScth /* Find spot in list */
1447b1dd958fScth rw_enter(&tgt->emul64_tgt_nw_lock, RW_WRITER);
1448b1dd958fScth cur = emul64_find_nowrite(tgt, sb, blkcnt, &overlap, &prev);
1449b1dd958fScth if (overlap == O_NONE) {
1450b1dd958fScth /* Insert into list */
1451b1dd958fScth *prev = nowrite;
1452b1dd958fScth nowrite->emul64_nwnext = cur;
1453b1dd958fScth }
1454b1dd958fScth rw_exit(&tgt->emul64_tgt_nw_lock);
1455b1dd958fScth if (overlap == O_NONE) {
1456b1dd958fScth if (emul64_collect_stats) {
1457b1dd958fScth mutex_enter(&emul64_stats_mutex);
1458b1dd958fScth emul64_nowrite_count++;
1459b1dd958fScth mutex_exit(&emul64_stats_mutex);
1460b1dd958fScth }
1461b1dd958fScth } else {
1462b1dd958fScth cmn_err(CE_WARN, "emul64: EMUL64_WRITE_OFF 0x%llx,0x%"
1463b1dd958fScth PRIx64 "overlaps 0x%llx,0x%" PRIx64 "\n",
1464b1dd958fScth nowrite->emul64_blocked.emul64_sb,
1465b1dd958fScth nowrite->emul64_blocked.emul64_blkcnt,
1466b1dd958fScth cur->emul64_blocked.emul64_sb,
1467b1dd958fScth cur->emul64_blocked.emul64_blkcnt);
1468b1dd958fScth emul64_nowrite_free(nowrite);
1469b1dd958fScth return (EINVAL);
1470b1dd958fScth }
1471b1dd958fScth return (0);
1472b1dd958fScth }
1473b1dd958fScth
1474b1dd958fScth /* ARGSUSED */
1475b1dd958fScth static int
emul64_write_on(struct emul64 * emul64,emul64_tgt_t * tgt,emul64_tgt_range_t * tgtr)1476*2178a880SToomas Soome emul64_write_on(struct emul64 *emul64, emul64_tgt_t *tgt,
1477*2178a880SToomas Soome emul64_tgt_range_t *tgtr)
1478b1dd958fScth {
1479b1dd958fScth size_t blkcnt = tgtr->emul64_blkrange.emul64_blkcnt;
1480b1dd958fScth emul64_nowrite_t *cur;
1481b1dd958fScth emul64_rng_overlap_t overlap = O_NONE;
1482b1dd958fScth emul64_nowrite_t **prev = NULL;
1483b1dd958fScth int rv = 0;
1484b1dd958fScth diskaddr_t sb = tgtr->emul64_blkrange.emul64_sb;
1485b1dd958fScth
1486b1dd958fScth /* Find spot in list */
1487b1dd958fScth rw_enter(&tgt->emul64_tgt_nw_lock, RW_WRITER);
1488b1dd958fScth cur = emul64_find_nowrite(tgt, sb, blkcnt, &overlap, &prev);
1489b1dd958fScth if (overlap == O_SAME) {
1490b1dd958fScth /* Remove from list */
1491b1dd958fScth *prev = cur->emul64_nwnext;
1492b1dd958fScth }
1493b1dd958fScth rw_exit(&tgt->emul64_tgt_nw_lock);
1494b1dd958fScth
1495b1dd958fScth switch (overlap) {
1496b1dd958fScth case O_NONE:
1497b1dd958fScth cmn_err(CE_WARN, "emul64: EMUL64_WRITE_ON 0x%llx,0x%lx "
149819397407SSherry Moore "range not found\n", sb, blkcnt);
1499b1dd958fScth rv = ENXIO;
1500b1dd958fScth break;
1501b1dd958fScth case O_SAME:
1502b1dd958fScth if (emul64_collect_stats) {
1503b1dd958fScth mutex_enter(&emul64_stats_mutex);
1504b1dd958fScth emul64_nowrite_count--;
1505b1dd958fScth mutex_exit(&emul64_stats_mutex);
1506b1dd958fScth }
1507b1dd958fScth emul64_nowrite_free(cur);
1508b1dd958fScth break;
1509b1dd958fScth case O_OVERLAP:
1510b1dd958fScth case O_SUBSET:
1511b1dd958fScth cmn_err(CE_WARN, "emul64: EMUL64_WRITE_ON 0x%llx,0x%lx "
151219397407SSherry Moore "overlaps 0x%llx,0x%" PRIx64 "\n",
151319397407SSherry Moore sb, blkcnt, cur->emul64_blocked.emul64_sb,
151419397407SSherry Moore cur->emul64_blocked.emul64_blkcnt);
1515b1dd958fScth rv = EINVAL;
1516b1dd958fScth break;
1517b1dd958fScth }
1518b1dd958fScth return (rv);
1519b1dd958fScth }
1520b1dd958fScth
1521b1dd958fScth static emul64_nowrite_t *
emul64_find_nowrite(emul64_tgt_t * tgt,diskaddr_t sb,size_t blkcnt,emul64_rng_overlap_t * overlap,emul64_nowrite_t *** prevp)1522*2178a880SToomas Soome emul64_find_nowrite(emul64_tgt_t *tgt, diskaddr_t sb, size_t blkcnt,
1523*2178a880SToomas Soome emul64_rng_overlap_t *overlap, emul64_nowrite_t ***prevp)
1524b1dd958fScth {
1525b1dd958fScth emul64_nowrite_t *cur;
1526b1dd958fScth emul64_nowrite_t **prev;
1527b1dd958fScth
1528b1dd958fScth /* Find spot in list */
1529b1dd958fScth *overlap = O_NONE;
1530b1dd958fScth prev = &tgt->emul64_tgt_nowrite;
1531b1dd958fScth cur = tgt->emul64_tgt_nowrite;
1532b1dd958fScth while (cur != NULL) {
1533b1dd958fScth *overlap = emul64_overlap(&cur->emul64_blocked, sb, blkcnt);
1534b1dd958fScth if (*overlap != O_NONE)
1535b1dd958fScth break;
1536b1dd958fScth prev = &cur->emul64_nwnext;
1537b1dd958fScth cur = cur->emul64_nwnext;
1538b1dd958fScth }
1539b1dd958fScth
1540b1dd958fScth *prevp = prev;
1541b1dd958fScth return (cur);
1542b1dd958fScth }
1543b1dd958fScth
1544b1dd958fScth static emul64_nowrite_t *
emul64_nowrite_alloc(emul64_range_t * range)1545b1dd958fScth emul64_nowrite_alloc(emul64_range_t *range)
1546b1dd958fScth {
1547b1dd958fScth emul64_nowrite_t *nw;
1548b1dd958fScth
1549b1dd958fScth nw = kmem_zalloc(sizeof (*nw), KM_SLEEP);
1550b1dd958fScth bcopy((void *) range,
155119397407SSherry Moore (void *) &nw->emul64_blocked,
155219397407SSherry Moore sizeof (nw->emul64_blocked));
1553b1dd958fScth return (nw);
1554b1dd958fScth }
1555b1dd958fScth
1556b1dd958fScth static void
emul64_nowrite_free(emul64_nowrite_t * nw)1557b1dd958fScth emul64_nowrite_free(emul64_nowrite_t *nw)
1558b1dd958fScth {
1559b1dd958fScth kmem_free((void *) nw, sizeof (*nw));
1560b1dd958fScth }
1561b1dd958fScth
1562b1dd958fScth emul64_rng_overlap_t
emul64_overlap(emul64_range_t * rng,diskaddr_t sb,size_t cnt)1563b1dd958fScth emul64_overlap(emul64_range_t *rng, diskaddr_t sb, size_t cnt)
1564b1dd958fScth {
1565b1dd958fScth
1566b1dd958fScth if (rng->emul64_sb >= sb + cnt)
1567b1dd958fScth return (O_NONE);
1568b1dd958fScth if (rng->emul64_sb + rng->emul64_blkcnt <= sb)
1569b1dd958fScth return (O_NONE);
1570b1dd958fScth if ((rng->emul64_sb == sb) && (rng->emul64_blkcnt == cnt))
1571b1dd958fScth return (O_SAME);
1572b1dd958fScth if ((sb >= rng->emul64_sb) &&
1573b1dd958fScth ((sb + cnt) <= (rng->emul64_sb + rng->emul64_blkcnt))) {
1574b1dd958fScth return (O_SUBSET);
1575b1dd958fScth }
1576b1dd958fScth return (O_OVERLAP);
1577b1dd958fScth }
1578b1dd958fScth
1579b1dd958fScth #include <sys/varargs.h>
1580b1dd958fScth
1581b1dd958fScth /*
1582b1dd958fScth * Error logging, printing, and debug print routines
1583b1dd958fScth */
1584b1dd958fScth
1585b1dd958fScth /*VARARGS3*/
1586b1dd958fScth static void
emul64_i_log(struct emul64 * emul64,int level,char * fmt,...)1587b1dd958fScth emul64_i_log(struct emul64 *emul64, int level, char *fmt, ...)
1588b1dd958fScth {
1589b1dd958fScth char buf[256];
1590b1dd958fScth va_list ap;
1591b1dd958fScth
1592b1dd958fScth va_start(ap, fmt);
1593b1dd958fScth (void) vsnprintf(buf, sizeof (buf), fmt, ap);
1594b1dd958fScth va_end(ap);
1595b1dd958fScth
1596b1dd958fScth scsi_log(emul64 ? emul64->emul64_dip : NULL,
1597b1dd958fScth "emul64", level, "%s\n", buf);
1598b1dd958fScth }
1599b1dd958fScth
1600b1dd958fScth
1601b1dd958fScth #ifdef EMUL64DEBUG
1602b1dd958fScth
1603b1dd958fScth static void
emul64_debug_dump_cdb(struct scsi_address * ap,struct scsi_pkt * pkt)1604b1dd958fScth emul64_debug_dump_cdb(struct scsi_address *ap, struct scsi_pkt *pkt)
1605b1dd958fScth {
1606b1dd958fScth static char hex[] = "0123456789abcdef";
1607b1dd958fScth struct emul64 *emul64 = ADDR2EMUL64(ap);
1608b1dd958fScth struct emul64_cmd *sp = PKT2CMD(pkt);
1609b1dd958fScth uint8_t *cdb = pkt->pkt_cdbp;
1610*2178a880SToomas Soome char buf[256];
1611b1dd958fScth char *p;
1612b1dd958fScth int i;
1613b1dd958fScth
1614b1dd958fScth (void) snprintf(buf, sizeof (buf), "emul64%d: <%d,%d> ",
161519397407SSherry Moore ddi_get_instance(emul64->emul64_dip),
161619397407SSherry Moore ap->a_target, ap->a_lun);
1617b1dd958fScth
1618b1dd958fScth p = buf + strlen(buf);
1619b1dd958fScth
1620b1dd958fScth *p++ = '[';
1621b1dd958fScth for (i = 0; i < sp->cmd_cdblen; i++, cdb++) {
1622b1dd958fScth if (i != 0)
1623b1dd958fScth *p++ = ' ';
1624b1dd958fScth *p++ = hex[(*cdb >> 4) & 0x0f];
1625b1dd958fScth *p++ = hex[*cdb & 0x0f];
1626b1dd958fScth }
1627b1dd958fScth *p++ = ']';
1628b1dd958fScth *p++ = '\n';
1629*2178a880SToomas Soome *p = '\0';
1630b1dd958fScth
1631b1dd958fScth cmn_err(CE_CONT, buf);
1632b1dd958fScth }
1633b1dd958fScth #endif /* EMUL64DEBUG */
1634