xref: /dflybsd-src/sys/dev/disk/nata/atapi-cam.c (revision cec957e929d4fbddf545b1918d45b9eadc8268ce)
1c1b3d7c5SThomas E. Spanjaard /*-
2c1b3d7c5SThomas E. Spanjaard  * Copyright (c) 2001-2003 Thomas Quinot <thomas@cuivre.fr.eu.org>
3c1b3d7c5SThomas E. Spanjaard  * All rights reserved.
4c1b3d7c5SThomas E. Spanjaard  *
5c1b3d7c5SThomas E. Spanjaard  * Redistribution and use in source and binary forms, with or without
6c1b3d7c5SThomas E. Spanjaard  * modification, are permitted provided that the following conditions
7c1b3d7c5SThomas E. Spanjaard  * are met:
8c1b3d7c5SThomas E. Spanjaard  * 1. Redistributions of source code must retain the above copyright
9c1b3d7c5SThomas E. Spanjaard  *    notice, this list of conditions and the following disclaimer,
10c1b3d7c5SThomas E. Spanjaard  *    without modification, immediately at the beginning of the file.
11c1b3d7c5SThomas E. Spanjaard  * 2. Redistributions in binary form must reproduce the above copyright
12c1b3d7c5SThomas E. Spanjaard  *    notice, this list of conditions and the following disclaimer in the
13c1b3d7c5SThomas E. Spanjaard  *    documentation and/or other materials provided with the distribution.
14c1b3d7c5SThomas E. Spanjaard  * 3. The name of the author may not be used to endorse or promote products
15c1b3d7c5SThomas E. Spanjaard  *    derived from this software without specific prior written permission.
16c1b3d7c5SThomas E. Spanjaard  *
17c1b3d7c5SThomas E. Spanjaard  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18c1b3d7c5SThomas E. Spanjaard  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19c1b3d7c5SThomas E. Spanjaard  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20c1b3d7c5SThomas E. Spanjaard  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21c1b3d7c5SThomas E. Spanjaard  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22c1b3d7c5SThomas E. Spanjaard  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23c1b3d7c5SThomas E. Spanjaard  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24c1b3d7c5SThomas E. Spanjaard  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25c1b3d7c5SThomas E. Spanjaard  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26c1b3d7c5SThomas E. Spanjaard  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27c1b3d7c5SThomas E. Spanjaard  *
28c1b3d7c5SThomas E. Spanjaard  * $FreeBSD: src/sys/dev/ata/atapi-cam.c,v 1.44 2006/03/31 08:09:05 sos Exp $
29c1b3d7c5SThomas E. Spanjaard  */
30c1b3d7c5SThomas E. Spanjaard 
31c1b3d7c5SThomas E. Spanjaard #include "opt_ata.h"
32d6aaf612SThomas E. Spanjaard #include "opt_scsi.h"
33c1b3d7c5SThomas E. Spanjaard 
34c1b3d7c5SThomas E. Spanjaard #include <sys/param.h>
35c1b3d7c5SThomas E. Spanjaard #include <sys/bus.h>
36c1b3d7c5SThomas E. Spanjaard #include <sys/libkern.h>
37c1b3d7c5SThomas E. Spanjaard #include <sys/lock.h>		/* for {get,rel}_mplock() */
38c1b3d7c5SThomas E. Spanjaard #include <sys/malloc.h>
39c1b3d7c5SThomas E. Spanjaard #include <sys/module.h>
40c1b3d7c5SThomas E. Spanjaard #include <sys/nata.h>
41c1b3d7c5SThomas E. Spanjaard #include <sys/queue.h>
42c1b3d7c5SThomas E. Spanjaard #include <sys/systm.h>
43684a93c4SMatthew Dillon 
44c1b3d7c5SThomas E. Spanjaard #include <sys/thread2.h>
45684a93c4SMatthew Dillon #include <sys/mplock2.h>
46c1b3d7c5SThomas E. Spanjaard 
47c1b3d7c5SThomas E. Spanjaard #include <bus/cam/cam.h>
48c1b3d7c5SThomas E. Spanjaard #include <bus/cam/cam_ccb.h>
49c1b3d7c5SThomas E. Spanjaard #include <bus/cam/cam_periph.h>
50c1b3d7c5SThomas E. Spanjaard #include <bus/cam/cam_sim.h>
51c1b3d7c5SThomas E. Spanjaard #include <bus/cam/cam_xpt_sim.h>
52c1b3d7c5SThomas E. Spanjaard #include <bus/cam/cam_debug.h>
53c1b3d7c5SThomas E. Spanjaard #include <bus/cam/scsi/scsi_all.h>
54*cec957e9SMatthew Dillon #include <bus/cam/cam_xpt_periph.h>
55c1b3d7c5SThomas E. Spanjaard 
56c1b3d7c5SThomas E. Spanjaard #include "ata-all.h"
57c1b3d7c5SThomas E. Spanjaard #include "ata_if.h"
58c1b3d7c5SThomas E. Spanjaard 
59c1b3d7c5SThomas E. Spanjaard /* private data associated with an ATA bus */
60c1b3d7c5SThomas E. Spanjaard struct atapi_xpt_softc {
61c1b3d7c5SThomas E. Spanjaard     struct ata_device   atapi_cam_dev;  /* must be first */
62c1b3d7c5SThomas E. Spanjaard     device_t            dev;
63c1b3d7c5SThomas E. Spanjaard     device_t            parent;
64c1b3d7c5SThomas E. Spanjaard     struct ata_channel  *ata_ch;
65c1b3d7c5SThomas E. Spanjaard     struct cam_path     *path;
66c1b3d7c5SThomas E. Spanjaard     struct cam_sim      *sim;
67c1b3d7c5SThomas E. Spanjaard     int                 flags;
68c1b3d7c5SThomas E. Spanjaard #define BUS_REGISTERED          0x01
69c1b3d7c5SThomas E. Spanjaard #define RESOURCE_SHORTAGE       0x02
70c1b3d7c5SThomas E. Spanjaard #define DETACHING               0x04
71c1b3d7c5SThomas E. Spanjaard 
72c1b3d7c5SThomas E. Spanjaard     TAILQ_HEAD(,atapi_hcb) pending_hcbs;
73c1b3d7c5SThomas E. Spanjaard     struct ata_device   *atadev[2];
7415bd3c73SMatthew Dillon     struct lock		state_lock;
75c1b3d7c5SThomas E. Spanjaard };
76c1b3d7c5SThomas E. Spanjaard 
77c1b3d7c5SThomas E. Spanjaard /* hardware command descriptor block */
78c1b3d7c5SThomas E. Spanjaard struct atapi_hcb {
79c1b3d7c5SThomas E. Spanjaard     struct atapi_xpt_softc *softc;
80c1b3d7c5SThomas E. Spanjaard     int                 unit;
81c1b3d7c5SThomas E. Spanjaard     int                 bus;
82c1b3d7c5SThomas E. Spanjaard     int                 target;
83c1b3d7c5SThomas E. Spanjaard     int                 lun;
84c1b3d7c5SThomas E. Spanjaard     union ccb           *ccb;
85c1b3d7c5SThomas E. Spanjaard     int                 flags;
86c1b3d7c5SThomas E. Spanjaard #define QUEUED          0x0001
87c1b3d7c5SThomas E. Spanjaard #define AUTOSENSE       0x0002
88c1b3d7c5SThomas E. Spanjaard     char                *dxfer_alloc;
89c1b3d7c5SThomas E. Spanjaard     TAILQ_ENTRY(atapi_hcb) chain;
90c1b3d7c5SThomas E. Spanjaard };
91c1b3d7c5SThomas E. Spanjaard 
92dbcd0c9bSMatthew Dillon enum reinit_reason { ATTACH, RESET };
93c1b3d7c5SThomas E. Spanjaard 
94c1b3d7c5SThomas E. Spanjaard /* Device methods */
95c1b3d7c5SThomas E. Spanjaard static void atapi_cam_identify(device_t *dev, device_t parent);
96c1b3d7c5SThomas E. Spanjaard static int atapi_cam_probe(device_t dev);
97c1b3d7c5SThomas E. Spanjaard static int atapi_cam_attach(device_t dev);
98c1b3d7c5SThomas E. Spanjaard static int atapi_cam_detach(device_t dev);
99c1b3d7c5SThomas E. Spanjaard static int atapi_cam_reinit(device_t dev);
100c1b3d7c5SThomas E. Spanjaard 
101c1b3d7c5SThomas E. Spanjaard /* CAM XPT methods */
102c1b3d7c5SThomas E. Spanjaard static void atapi_action(struct cam_sim *, union ccb *);
103c1b3d7c5SThomas E. Spanjaard static void atapi_poll(struct cam_sim *);
104c1b3d7c5SThomas E. Spanjaard static void atapi_async(void *, u_int32_t, struct cam_path *, void *);
105c1b3d7c5SThomas E. Spanjaard static void atapi_cb(struct ata_request *);
106c1b3d7c5SThomas E. Spanjaard 
107c1b3d7c5SThomas E. Spanjaard /* Module methods */
108c1b3d7c5SThomas E. Spanjaard static int atapi_cam_event_handler(module_t mod, int what, void *arg);
109c1b3d7c5SThomas E. Spanjaard 
110c1b3d7c5SThomas E. Spanjaard /* internal functions */
111c1b3d7c5SThomas E. Spanjaard static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason);
112c1b3d7c5SThomas E. Spanjaard static void setup_async_cb(struct atapi_xpt_softc *, uint32_t);
113c1b3d7c5SThomas E. Spanjaard static void cam_rescan_callback(struct cam_periph *, union ccb *);
114c1b3d7c5SThomas E. Spanjaard static void cam_rescan(struct cam_sim *);
115c1b3d7c5SThomas E. Spanjaard static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t);
116c1b3d7c5SThomas E. Spanjaard static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *);
117c1b3d7c5SThomas E. Spanjaard static void free_hcb(struct atapi_hcb *hcb);
118c1b3d7c5SThomas E. Spanjaard static void free_softc(struct atapi_xpt_softc *scp);
119c1b3d7c5SThomas E. Spanjaard 
120c1b3d7c5SThomas E. Spanjaard static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
121c1b3d7c5SThomas E. Spanjaard 
122c1b3d7c5SThomas E. Spanjaard static device_method_t atapi_cam_methods[] = {
123c1b3d7c5SThomas E. Spanjaard 	DEVMETHOD(device_identify,      atapi_cam_identify),
124c1b3d7c5SThomas E. Spanjaard 	DEVMETHOD(device_probe,         atapi_cam_probe),
125c1b3d7c5SThomas E. Spanjaard 	DEVMETHOD(device_attach,        atapi_cam_attach),
126c1b3d7c5SThomas E. Spanjaard 	DEVMETHOD(device_detach,        atapi_cam_detach),
127c1b3d7c5SThomas E. Spanjaard 	DEVMETHOD(ata_reinit,           atapi_cam_reinit),
128d3c9c58eSSascha Wildner 	DEVMETHOD_END
129c1b3d7c5SThomas E. Spanjaard };
130c1b3d7c5SThomas E. Spanjaard 
131c1b3d7c5SThomas E. Spanjaard static driver_t atapi_cam_driver = {
132c1b3d7c5SThomas E. Spanjaard 	"atapicam",
133c1b3d7c5SThomas E. Spanjaard 	atapi_cam_methods,
134c1b3d7c5SThomas E. Spanjaard 	sizeof(struct atapi_xpt_softc)
135c1b3d7c5SThomas E. Spanjaard };
136c1b3d7c5SThomas E. Spanjaard 
137c1b3d7c5SThomas E. Spanjaard static devclass_t       atapi_cam_devclass;
138c1b3d7c5SThomas E. Spanjaard DRIVER_MODULE(atapicam, ata,
139c1b3d7c5SThomas E. Spanjaard 	atapi_cam_driver,
140c1b3d7c5SThomas E. Spanjaard 	atapi_cam_devclass,
141c1b3d7c5SThomas E. Spanjaard 	atapi_cam_event_handler,
142c1b3d7c5SThomas E. Spanjaard 	/*arg*/NULL);
143c1b3d7c5SThomas E. Spanjaard MODULE_VERSION(atapicam, 1);
144c1b3d7c5SThomas E. Spanjaard MODULE_DEPEND(atapicam, ata, 1, 1, 1);
145c1b3d7c5SThomas E. Spanjaard MODULE_DEPEND(atapicam, cam, 1, 1, 1);
146c1b3d7c5SThomas E. Spanjaard 
147c1b3d7c5SThomas E. Spanjaard static void
atapi_cam_identify(device_t * dev,device_t parent)148c1b3d7c5SThomas E. Spanjaard atapi_cam_identify(device_t *dev, device_t parent)
149c1b3d7c5SThomas E. Spanjaard {
150c1b3d7c5SThomas E. Spanjaard 	struct atapi_xpt_softc *scp =
151a01741bbSMatthew Dillon 	    kmalloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_INTWAIT|M_ZERO);
152c1b3d7c5SThomas E. Spanjaard 	device_t child;
153c1b3d7c5SThomas E. Spanjaard 
1542458a87aSzrj 	if (scp == NULL) {
1552458a87aSzrj 		kprintf ("atapi_cam_identify: out of memory");
1562458a87aSzrj 		return;
1572458a87aSzrj 	}
1582458a87aSzrj 
159c1b3d7c5SThomas E. Spanjaard 	/* Assume one atapicam instance per parent channel instance. */
160c1b3d7c5SThomas E. Spanjaard 	child = device_add_child(parent, "atapicam", -1);
161c1b3d7c5SThomas E. Spanjaard 	if (child == NULL) {
162e3869ec7SSascha Wildner 		kprintf("atapi_cam_identify: out of memory, can't add child");
163c1b3d7c5SThomas E. Spanjaard 		kfree(scp, M_ATACAM);
164c1b3d7c5SThomas E. Spanjaard 		return;
165c1b3d7c5SThomas E. Spanjaard 	}
166c1b3d7c5SThomas E. Spanjaard 	scp->atapi_cam_dev.unit = -1;
167c1b3d7c5SThomas E. Spanjaard 	scp->atapi_cam_dev.dev = child;
168c1b3d7c5SThomas E. Spanjaard 	device_quiet(child);
169c1b3d7c5SThomas E. Spanjaard 	device_set_softc(child, scp);
170c1b3d7c5SThomas E. Spanjaard }
171c1b3d7c5SThomas E. Spanjaard 
172c1b3d7c5SThomas E. Spanjaard static int
atapi_cam_probe(device_t dev)173c1b3d7c5SThomas E. Spanjaard atapi_cam_probe(device_t dev)
174c1b3d7c5SThomas E. Spanjaard {
175c1b3d7c5SThomas E. Spanjaard 	struct ata_device *atadev = device_get_softc (dev);
176c1b3d7c5SThomas E. Spanjaard 
177c1b3d7c5SThomas E. Spanjaard 	KASSERT(atadev != NULL, ("expect valid struct ata_device"));
178c1b3d7c5SThomas E. Spanjaard 	if (atadev->unit < 0) {
179c1b3d7c5SThomas E. Spanjaard 		device_set_desc(dev, "ATAPI CAM Attachment");
180c1b3d7c5SThomas E. Spanjaard 		return (0);
181c1b3d7c5SThomas E. Spanjaard 	} else {
182c1b3d7c5SThomas E. Spanjaard 		return ENXIO;
183c1b3d7c5SThomas E. Spanjaard 	}
184c1b3d7c5SThomas E. Spanjaard }
185c1b3d7c5SThomas E. Spanjaard 
186c1b3d7c5SThomas E. Spanjaard static int
atapi_cam_attach(device_t dev)187c1b3d7c5SThomas E. Spanjaard atapi_cam_attach(device_t dev)
188c1b3d7c5SThomas E. Spanjaard {
189c1b3d7c5SThomas E. Spanjaard     struct atapi_xpt_softc *scp = NULL;
190c1b3d7c5SThomas E. Spanjaard     struct cam_devq *devq = NULL;
191c1b3d7c5SThomas E. Spanjaard     struct cam_sim *sim = NULL;
192c1b3d7c5SThomas E. Spanjaard     struct cam_path *path = NULL;
193c1b3d7c5SThomas E. Spanjaard     int unit, error;
194c1b3d7c5SThomas E. Spanjaard 
195c1b3d7c5SThomas E. Spanjaard     scp = (struct atapi_xpt_softc *)device_get_softc(dev);
196c1b3d7c5SThomas E. Spanjaard     if (scp == NULL) {
197c1b3d7c5SThomas E. Spanjaard 	device_printf(dev, "Cannot get softc\n");
198c1b3d7c5SThomas E. Spanjaard 	return ENOMEM;
199c1b3d7c5SThomas E. Spanjaard     }
200c1b3d7c5SThomas E. Spanjaard 
20115bd3c73SMatthew Dillon     lockinit(&scp->state_lock, "atapicamattach", 0, 0);
202c1b3d7c5SThomas E. Spanjaard 
203c1b3d7c5SThomas E. Spanjaard     scp->dev = dev;
204c1b3d7c5SThomas E. Spanjaard     scp->parent = device_get_parent(dev);
205c1b3d7c5SThomas E. Spanjaard     scp->ata_ch = device_get_softc(scp->parent);
206c1b3d7c5SThomas E. Spanjaard     TAILQ_INIT(&scp->pending_hcbs);
207c1b3d7c5SThomas E. Spanjaard     unit = device_get_unit(dev);
208c1b3d7c5SThomas E. Spanjaard 
209c1b3d7c5SThomas E. Spanjaard     if ((devq = cam_simq_alloc(16)) == NULL) {
210c1b3d7c5SThomas E. Spanjaard 	error = ENOMEM;
211c1b3d7c5SThomas E. Spanjaard 	goto out;
212c1b3d7c5SThomas E. Spanjaard     }
213c1b3d7c5SThomas E. Spanjaard 
214c1b3d7c5SThomas E. Spanjaard     if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
2151c8b7a9aSPeter Avalos 		 (void *)scp, unit, &sim_mplock, 1, 1, devq)) == NULL) {
216c1b3d7c5SThomas E. Spanjaard 	error = ENOMEM;
217c1b3d7c5SThomas E. Spanjaard 	goto out;
218c1b3d7c5SThomas E. Spanjaard     }
219c1b3d7c5SThomas E. Spanjaard     scp->sim = sim;
220c1b3d7c5SThomas E. Spanjaard 
221c1b3d7c5SThomas E. Spanjaard     if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
222c1b3d7c5SThomas E. Spanjaard 	error = EINVAL;
223c1b3d7c5SThomas E. Spanjaard 	goto out;
224c1b3d7c5SThomas E. Spanjaard     }
225c1b3d7c5SThomas E. Spanjaard     scp->flags |= BUS_REGISTERED;
226c1b3d7c5SThomas E. Spanjaard 
227c1b3d7c5SThomas E. Spanjaard     if (xpt_create_path(&path, /*periph*/ NULL,
228c1b3d7c5SThomas E. Spanjaard 		cam_sim_path(sim), CAM_TARGET_WILDCARD,
229c1b3d7c5SThomas E. Spanjaard 		CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
230c1b3d7c5SThomas E. Spanjaard 	error = ENOMEM;
231c1b3d7c5SThomas E. Spanjaard 	goto out;
232c1b3d7c5SThomas E. Spanjaard     }
233c1b3d7c5SThomas E. Spanjaard     scp->path = path;
234c1b3d7c5SThomas E. Spanjaard 
235c1b3d7c5SThomas E. Spanjaard     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
236c1b3d7c5SThomas E. Spanjaard 
237c1b3d7c5SThomas E. Spanjaard     setup_async_cb(scp, AC_LOST_DEVICE);
238dbcd0c9bSMatthew Dillon     reinit_bus(scp, ATTACH);
239c1b3d7c5SThomas E. Spanjaard     error = 0;
240c1b3d7c5SThomas E. Spanjaard 
241c1b3d7c5SThomas E. Spanjaard out:
242c1b3d7c5SThomas E. Spanjaard     if (error != 0)
243c1b3d7c5SThomas E. Spanjaard 	free_softc(scp);
244c1b3d7c5SThomas E. Spanjaard 
245c1b3d7c5SThomas E. Spanjaard     return(error);
246c1b3d7c5SThomas E. Spanjaard }
247c1b3d7c5SThomas E. Spanjaard 
248c1b3d7c5SThomas E. Spanjaard static int
atapi_cam_detach(device_t dev)249c1b3d7c5SThomas E. Spanjaard atapi_cam_detach(device_t dev)
250c1b3d7c5SThomas E. Spanjaard {
251c1b3d7c5SThomas E. Spanjaard     struct atapi_xpt_softc *scp = device_get_softc(dev);
252c1b3d7c5SThomas E. Spanjaard 
253c1b3d7c5SThomas E. Spanjaard     get_mplock();
254c1b3d7c5SThomas E. Spanjaard     xpt_freeze_simq(scp->sim, 1 /*count*/);
255c1b3d7c5SThomas E. Spanjaard     rel_mplock();
25615bd3c73SMatthew Dillon     lockmgr(&scp->state_lock, LK_EXCLUSIVE);
257c1b3d7c5SThomas E. Spanjaard     scp->flags |= DETACHING;
25815bd3c73SMatthew Dillon     lockmgr(&scp->state_lock, LK_RELEASE);
259c1b3d7c5SThomas E. Spanjaard     free_softc(scp);
260c1b3d7c5SThomas E. Spanjaard     return (0);
261c1b3d7c5SThomas E. Spanjaard }
262c1b3d7c5SThomas E. Spanjaard 
263c1b3d7c5SThomas E. Spanjaard static int
atapi_cam_reinit(device_t dev)264c1b3d7c5SThomas E. Spanjaard atapi_cam_reinit(device_t dev) {
265c1b3d7c5SThomas E. Spanjaard     struct atapi_xpt_softc *scp = device_get_softc(dev);
266c1b3d7c5SThomas E. Spanjaard 
267c1b3d7c5SThomas E. Spanjaard     /*
268c1b3d7c5SThomas E. Spanjaard      * scp might be null if the bus is being reinitialised during
269c1b3d7c5SThomas E. Spanjaard      * the boot-up sequence, before the ATAPI bus is registered.
270c1b3d7c5SThomas E. Spanjaard      */
271c1b3d7c5SThomas E. Spanjaard 
272c1b3d7c5SThomas E. Spanjaard     if (scp != NULL) {
273c1b3d7c5SThomas E. Spanjaard 	reinit_bus(scp, RESET);
274c1b3d7c5SThomas E. Spanjaard     }
275c1b3d7c5SThomas E. Spanjaard     return 0;
276c1b3d7c5SThomas E. Spanjaard }
277c1b3d7c5SThomas E. Spanjaard 
278c1b3d7c5SThomas E. Spanjaard static void
reinit_bus(struct atapi_xpt_softc * scp,enum reinit_reason reason)279c1b3d7c5SThomas E. Spanjaard reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
280c436b8b4SHasso Tepper     struct ata_device *old_atadev[2], *atadev;
281c1b3d7c5SThomas E. Spanjaard     device_t *children;
282c436b8b4SHasso Tepper     int nchildren, i, dev_changed;
283c1b3d7c5SThomas E. Spanjaard 
284c1b3d7c5SThomas E. Spanjaard     if (device_get_children(scp->parent, &children, &nchildren) != 0) {
285c1b3d7c5SThomas E. Spanjaard 	return;
286c1b3d7c5SThomas E. Spanjaard     }
287c1b3d7c5SThomas E. Spanjaard 
28815bd3c73SMatthew Dillon     lockmgr(&scp->state_lock, LK_EXCLUSIVE);
289c436b8b4SHasso Tepper     old_atadev[0] = scp->atadev[0];
290c436b8b4SHasso Tepper     old_atadev[1] = scp->atadev[1];
291c1b3d7c5SThomas E. Spanjaard     scp->atadev[0] = NULL;
292c1b3d7c5SThomas E. Spanjaard     scp->atadev[1] = NULL;
293c1b3d7c5SThomas E. Spanjaard 
294c1b3d7c5SThomas E. Spanjaard     for (i = 0; i < nchildren; i++) {
295c1b3d7c5SThomas E. Spanjaard 	/* XXX Does the child need to actually be attached yet? */
296c1b3d7c5SThomas E. Spanjaard 	if (children[i] != NULL) {
297c1b3d7c5SThomas E. Spanjaard 	    atadev = device_get_softc(children[i]);
298c1b3d7c5SThomas E. Spanjaard 	    if ((atadev->unit == ATA_MASTER) &&
299c1b3d7c5SThomas E. Spanjaard 		(scp->ata_ch->devices & ATA_ATAPI_MASTER) != 0)
300c1b3d7c5SThomas E. Spanjaard 		scp->atadev[0] = atadev;
301c1b3d7c5SThomas E. Spanjaard 	    if ((atadev->unit == ATA_SLAVE) &&
302c1b3d7c5SThomas E. Spanjaard 		(scp->ata_ch->devices & ATA_ATAPI_SLAVE) != 0)
303c1b3d7c5SThomas E. Spanjaard 		scp->atadev[1] = atadev;
304c1b3d7c5SThomas E. Spanjaard 	}
305c1b3d7c5SThomas E. Spanjaard     }
306c436b8b4SHasso Tepper     dev_changed = (old_atadev[0] != scp->atadev[0])
307c436b8b4SHasso Tepper 	    || (old_atadev[1] != scp->atadev[1]);
30815bd3c73SMatthew Dillon     lockmgr(&scp->state_lock, LK_RELEASE);
309c1b3d7c5SThomas E. Spanjaard     kfree(children, M_TEMP);
310c1b3d7c5SThomas E. Spanjaard 
311c1b3d7c5SThomas E. Spanjaard     switch (reason) {
312c1b3d7c5SThomas E. Spanjaard 	case RESET:
313c1b3d7c5SThomas E. Spanjaard 	    xpt_async(AC_BUS_RESET, scp->path, NULL);
314c436b8b4SHasso Tepper 
315c436b8b4SHasso Tepper 	    if (!dev_changed)
316c436b8b4SHasso Tepper 		break;
317c436b8b4SHasso Tepper 
318c1b3d7c5SThomas E. Spanjaard 	    /*FALLTHROUGH*/
319c1b3d7c5SThomas E. Spanjaard 	case ATTACH:
320c1b3d7c5SThomas E. Spanjaard 	    cam_rescan(scp->sim);
321c1b3d7c5SThomas E. Spanjaard 	    break;
322c1b3d7c5SThomas E. Spanjaard     }
323c1b3d7c5SThomas E. Spanjaard }
324c1b3d7c5SThomas E. Spanjaard 
325c1b3d7c5SThomas E. Spanjaard static void
setup_async_cb(struct atapi_xpt_softc * scp,uint32_t events)326c1b3d7c5SThomas E. Spanjaard setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
327c1b3d7c5SThomas E. Spanjaard {
328*cec957e9SMatthew Dillon     struct ccb_setasync *csa;
329*cec957e9SMatthew Dillon 
330*cec957e9SMatthew Dillon     csa = &xpt_alloc_ccb()->csa;
331c1b3d7c5SThomas E. Spanjaard 
332c1b3d7c5SThomas E. Spanjaard     get_mplock();
333*cec957e9SMatthew Dillon     xpt_setup_ccb(&csa->ccb_h, scp->path, /*priority*/ 5);
334*cec957e9SMatthew Dillon     csa->ccb_h.func_code = XPT_SASYNC_CB;
335*cec957e9SMatthew Dillon     csa->event_enable = events;
336*cec957e9SMatthew Dillon     csa->callback = &atapi_async;
337*cec957e9SMatthew Dillon     csa->callback_arg = scp->sim;
338*cec957e9SMatthew Dillon     xpt_action((union ccb *)csa);
339c1b3d7c5SThomas E. Spanjaard     rel_mplock();
340*cec957e9SMatthew Dillon 
341*cec957e9SMatthew Dillon     xpt_free_ccb(&csa->ccb_h);
342c1b3d7c5SThomas E. Spanjaard }
343c1b3d7c5SThomas E. Spanjaard 
344c1b3d7c5SThomas E. Spanjaard static void
atapi_action(struct cam_sim * sim,union ccb * ccb)345c1b3d7c5SThomas E. Spanjaard atapi_action(struct cam_sim *sim, union ccb *ccb)
346c1b3d7c5SThomas E. Spanjaard {
347c1b3d7c5SThomas E. Spanjaard     struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
348c1b3d7c5SThomas E. Spanjaard     struct ccb_hdr *ccb_h = &ccb->ccb_h;
349c1b3d7c5SThomas E. Spanjaard     struct atapi_hcb *hcb = NULL;
350c1b3d7c5SThomas E. Spanjaard     struct ata_request *request = NULL;
351c1b3d7c5SThomas E. Spanjaard     int unit = cam_sim_unit(sim);
352c1b3d7c5SThomas E. Spanjaard     int bus = cam_sim_bus(sim);
353c1b3d7c5SThomas E. Spanjaard     int len;
354c1b3d7c5SThomas E. Spanjaard     char *buf;
355c1b3d7c5SThomas E. Spanjaard 
356c1b3d7c5SThomas E. Spanjaard     switch (ccb_h->func_code) {
357c1b3d7c5SThomas E. Spanjaard     case XPT_PATH_INQ: {
358c1b3d7c5SThomas E. Spanjaard 	struct ccb_pathinq *cpi = &ccb->cpi;
359c1b3d7c5SThomas E. Spanjaard 	int tid = ccb_h->target_id;
360c1b3d7c5SThomas E. Spanjaard 
361c1b3d7c5SThomas E. Spanjaard 	cpi->version_num = 1;
362c1b3d7c5SThomas E. Spanjaard 	cpi->hba_inquiry = 0;
363c1b3d7c5SThomas E. Spanjaard 	cpi->target_sprt = 0;
364c1b3d7c5SThomas E. Spanjaard 	cpi->hba_misc = PIM_NO_6_BYTE;
365c1b3d7c5SThomas E. Spanjaard 	cpi->hba_eng_cnt = 0;
366c1b3d7c5SThomas E. Spanjaard 	bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
367c1b3d7c5SThomas E. Spanjaard 	cpi->max_target = 1;
368c1b3d7c5SThomas E. Spanjaard 	cpi->max_lun = 0;
369c1b3d7c5SThomas E. Spanjaard 	cpi->async_flags = 0;
370c1b3d7c5SThomas E. Spanjaard 	cpi->hpath_id = 0;
371c1b3d7c5SThomas E. Spanjaard 	cpi->initiator_id = 7;
372c1b3d7c5SThomas E. Spanjaard 	strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
373c1b3d7c5SThomas E. Spanjaard 	strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
374c1b3d7c5SThomas E. Spanjaard 	strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
375c1b3d7c5SThomas E. Spanjaard 	cpi->unit_number = cam_sim_unit(sim);
376c1b3d7c5SThomas E. Spanjaard 	cpi->bus_id = cam_sim_bus(sim);
377c1b3d7c5SThomas E. Spanjaard 	cpi->base_transfer_speed = 3300;
378f19fcfb0SPeter Avalos 	cpi->transport = XPORT_ATA;
379f19fcfb0SPeter Avalos 	cpi->transport_version = 2;
380f19fcfb0SPeter Avalos 	cpi->protocol = PROTO_SCSI;
381f19fcfb0SPeter Avalos 	cpi->protocol_version = SCSI_REV_2;
382c1b3d7c5SThomas E. Spanjaard 
383c1b3d7c5SThomas E. Spanjaard 	if (softc->ata_ch && tid != CAM_TARGET_WILDCARD) {
38415bd3c73SMatthew Dillon 	    lockmgr(&softc->state_lock, LK_EXCLUSIVE);
385c1b3d7c5SThomas E. Spanjaard 	    if (softc->atadev[tid] == NULL) {
386c1b3d7c5SThomas E. Spanjaard 		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
387c1b3d7c5SThomas E. Spanjaard 		xpt_done(ccb);
38815bd3c73SMatthew Dillon 		lockmgr(&softc->state_lock, LK_RELEASE);
389c1b3d7c5SThomas E. Spanjaard 		return;
390c1b3d7c5SThomas E. Spanjaard 	    }
391c1b3d7c5SThomas E. Spanjaard 	    switch (softc->atadev[ccb_h->target_id]->mode) {
392c1b3d7c5SThomas E. Spanjaard 	    case ATA_PIO1:
393c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 5200;
394c1b3d7c5SThomas E. Spanjaard 		break;
395c1b3d7c5SThomas E. Spanjaard 	    case ATA_PIO2:
396c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 7000;
397c1b3d7c5SThomas E. Spanjaard 		break;
398c1b3d7c5SThomas E. Spanjaard 	    case ATA_PIO3:
399c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 11000;
400c1b3d7c5SThomas E. Spanjaard 		break;
401c1b3d7c5SThomas E. Spanjaard 	    case ATA_PIO4:
402c1b3d7c5SThomas E. Spanjaard 	    case ATA_DMA:
403c1b3d7c5SThomas E. Spanjaard 	    case ATA_WDMA2:
404c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 16000;
405c1b3d7c5SThomas E. Spanjaard 		break;
406c1b3d7c5SThomas E. Spanjaard 	    case ATA_UDMA2:
407c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 33000;
408c1b3d7c5SThomas E. Spanjaard 		break;
409c1b3d7c5SThomas E. Spanjaard 	    case ATA_UDMA4:
410c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 66000;
411c1b3d7c5SThomas E. Spanjaard 		break;
412c1b3d7c5SThomas E. Spanjaard 	    case ATA_UDMA5:
413c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 100000;
414c1b3d7c5SThomas E. Spanjaard 		break;
415c1b3d7c5SThomas E. Spanjaard 	    case ATA_UDMA6:
416c1b3d7c5SThomas E. Spanjaard 		cpi->base_transfer_speed = 133000;
417c1b3d7c5SThomas E. Spanjaard 		break;
418c1b3d7c5SThomas E. Spanjaard 	    default:
419c1b3d7c5SThomas E. Spanjaard 		break;
420c1b3d7c5SThomas E. Spanjaard 	    }
42115bd3c73SMatthew Dillon 	    lockmgr(&softc->state_lock, LK_RELEASE);
422c1b3d7c5SThomas E. Spanjaard 	}
423c1b3d7c5SThomas E. Spanjaard 	ccb->ccb_h.status = CAM_REQ_CMP;
424c1b3d7c5SThomas E. Spanjaard 	xpt_done(ccb);
425c1b3d7c5SThomas E. Spanjaard 	return;
426c1b3d7c5SThomas E. Spanjaard     }
427c1b3d7c5SThomas E. Spanjaard 
428c1b3d7c5SThomas E. Spanjaard     case XPT_RESET_DEV: {
429c1b3d7c5SThomas E. Spanjaard 	int tid = ccb_h->target_id;
430c1b3d7c5SThomas E. Spanjaard 
431c1b3d7c5SThomas E. Spanjaard 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
432c1b3d7c5SThomas E. Spanjaard 	ata_controlcmd(softc->atadev[tid]->dev, ATA_DEVICE_RESET, 0, 0, 0);
433c1b3d7c5SThomas E. Spanjaard 	ccb->ccb_h.status = CAM_REQ_CMP;
434c1b3d7c5SThomas E. Spanjaard 	xpt_done(ccb);
435c1b3d7c5SThomas E. Spanjaard 	return;
436c1b3d7c5SThomas E. Spanjaard     }
437c1b3d7c5SThomas E. Spanjaard 
438c1b3d7c5SThomas E. Spanjaard     case XPT_RESET_BUS:
439c1b3d7c5SThomas E. Spanjaard 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
440c1b3d7c5SThomas E. Spanjaard 	ata_reinit(softc->parent);
441c1b3d7c5SThomas E. Spanjaard 	ccb->ccb_h.status = CAM_REQ_CMP;
442c1b3d7c5SThomas E. Spanjaard 	xpt_done(ccb);
443c1b3d7c5SThomas E. Spanjaard 	return;
444c1b3d7c5SThomas E. Spanjaard 
445c1b3d7c5SThomas E. Spanjaard     case XPT_SET_TRAN_SETTINGS:
446c1b3d7c5SThomas E. Spanjaard 	/* ignore these, we're not doing SCSI here */
447c1b3d7c5SThomas E. Spanjaard 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
448c1b3d7c5SThomas E. Spanjaard 		  ("SET_TRAN_SETTINGS not supported\n"));
449c1b3d7c5SThomas E. Spanjaard 	ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
450c1b3d7c5SThomas E. Spanjaard 	xpt_done(ccb);
451c1b3d7c5SThomas E. Spanjaard 	return;
452c1b3d7c5SThomas E. Spanjaard 
453c1b3d7c5SThomas E. Spanjaard     case XPT_GET_TRAN_SETTINGS: {
454c1b3d7c5SThomas E. Spanjaard 	struct ccb_trans_settings *cts = &ccb->cts;
455f19fcfb0SPeter Avalos 	cts->protocol = PROTO_SCSI;
456f19fcfb0SPeter Avalos 	cts->protocol_version = SCSI_REV_2;
457f19fcfb0SPeter Avalos 	cts->transport = XPORT_ATA;
458f19fcfb0SPeter Avalos 	cts->transport_version = XPORT_VERSION_UNSPECIFIED;
459f19fcfb0SPeter Avalos 	cts->proto_specific.valid = 0;
460f19fcfb0SPeter Avalos 	cts->xport_specific.valid = 0;
461f19fcfb0SPeter Avalos 	/* nothing more to do */
462c1b3d7c5SThomas E. Spanjaard 	ccb->ccb_h.status = CAM_REQ_CMP;
463f19fcfb0SPeter Avalos 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
464c1b3d7c5SThomas E. Spanjaard 	xpt_done(ccb);
465c1b3d7c5SThomas E. Spanjaard 	return;
466c1b3d7c5SThomas E. Spanjaard     }
467c1b3d7c5SThomas E. Spanjaard 
468c1b3d7c5SThomas E. Spanjaard     case XPT_CALC_GEOMETRY: {
469c1b3d7c5SThomas E. Spanjaard 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
470c1b3d7c5SThomas E. Spanjaard 	cam_calc_geometry(&ccb->ccg, /*extended*/1);
471c1b3d7c5SThomas E. Spanjaard 	xpt_done(ccb);
472c1b3d7c5SThomas E. Spanjaard 	return;
473c1b3d7c5SThomas E. Spanjaard     }
474c1b3d7c5SThomas E. Spanjaard 
475c1b3d7c5SThomas E. Spanjaard     case XPT_SCSI_IO: {
476c1b3d7c5SThomas E. Spanjaard 	struct ccb_scsiio *csio = &ccb->csio;
477c1b3d7c5SThomas E. Spanjaard 	int tid = ccb_h->target_id, lid = ccb_h->target_lun;
478a1347219SHasso Tepper 	int request_flags = ATA_R_QUIET | ATA_R_ATAPI;
479c1b3d7c5SThomas E. Spanjaard 
480c1b3d7c5SThomas E. Spanjaard 	CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
481c1b3d7c5SThomas E. Spanjaard 
48215bd3c73SMatthew Dillon 	lockmgr(&softc->state_lock, LK_EXCLUSIVE);
483c1b3d7c5SThomas E. Spanjaard 	if (softc->flags & DETACHING) {
484c1b3d7c5SThomas E. Spanjaard 	    ccb->ccb_h.status = CAM_REQ_ABORTED;
485c1b3d7c5SThomas E. Spanjaard 	    xpt_done(ccb);
48615bd3c73SMatthew Dillon 	    lockmgr(&softc->state_lock, LK_RELEASE);
487c1b3d7c5SThomas E. Spanjaard 	    return;
488c1b3d7c5SThomas E. Spanjaard 	}
489c1b3d7c5SThomas E. Spanjaard 
490c1b3d7c5SThomas E. Spanjaard 	if (softc->atadev[tid] == NULL) {
491c1b3d7c5SThomas E. Spanjaard 	    ccb->ccb_h.status = CAM_DEV_NOT_THERE;
492c1b3d7c5SThomas E. Spanjaard 	    xpt_done(ccb);
49315bd3c73SMatthew Dillon 	    lockmgr(&softc->state_lock, LK_RELEASE);
494c1b3d7c5SThomas E. Spanjaard 	    return;
495c1b3d7c5SThomas E. Spanjaard 	}
496c1b3d7c5SThomas E. Spanjaard 
497c1b3d7c5SThomas E. Spanjaard 	/* check that this request was not aborted already */
498c1b3d7c5SThomas E. Spanjaard 	if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
499e3869ec7SSascha Wildner 	    kprintf("XPT_SCSI_IO received but already in progress?\n");
500c1b3d7c5SThomas E. Spanjaard 	    xpt_done(ccb);
50115bd3c73SMatthew Dillon 	    lockmgr(&softc->state_lock, LK_RELEASE);
502c1b3d7c5SThomas E. Spanjaard 	    return;
503c1b3d7c5SThomas E. Spanjaard 	}
504c1b3d7c5SThomas E. Spanjaard 	if (lid > 0) {
505c1b3d7c5SThomas E. Spanjaard 	    CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
506c1b3d7c5SThomas E. Spanjaard 		      ("SCSI IO received for invalid lun %d\n", lid));
507c1b3d7c5SThomas E. Spanjaard 	    goto action_invalid;
508c1b3d7c5SThomas E. Spanjaard 	}
509c1b3d7c5SThomas E. Spanjaard 	if (csio->cdb_len > sizeof request->u.atapi.ccb) {
510c1b3d7c5SThomas E. Spanjaard 	    CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
511c1b3d7c5SThomas E. Spanjaard 		("CAM CCB too long for ATAPI"));
512c1b3d7c5SThomas E. Spanjaard 	    goto action_invalid;
513c1b3d7c5SThomas E. Spanjaard 	}
514c1b3d7c5SThomas E. Spanjaard 	if ((ccb_h->flags & CAM_SCATTER_VALID)) {
515c1b3d7c5SThomas E. Spanjaard 	    /* scatter-gather not supported */
516c1b3d7c5SThomas E. Spanjaard 	    xpt_print_path(ccb_h->path);
517e3869ec7SSascha Wildner 	    kprintf("ATAPI/CAM does not support scatter-gather yet!\n");
518c1b3d7c5SThomas E. Spanjaard 	    goto action_invalid;
519c1b3d7c5SThomas E. Spanjaard 	}
520c1b3d7c5SThomas E. Spanjaard 
521c1b3d7c5SThomas E. Spanjaard 	switch (ccb_h->flags & CAM_DIR_MASK) {
522c1b3d7c5SThomas E. Spanjaard 	case CAM_DIR_IN:
523c436b8b4SHasso Tepper 	     request_flags |= ATA_R_READ;
524c1b3d7c5SThomas E. Spanjaard 	     break;
525c1b3d7c5SThomas E. Spanjaard 	case CAM_DIR_OUT:
526c436b8b4SHasso Tepper 	     request_flags |= ATA_R_WRITE;
527c1b3d7c5SThomas E. Spanjaard 	     break;
528c1b3d7c5SThomas E. Spanjaard 	case CAM_DIR_NONE:
529c1b3d7c5SThomas E. Spanjaard 	     /* No flags need to be set */
530c1b3d7c5SThomas E. Spanjaard 	     break;
531c1b3d7c5SThomas E. Spanjaard 	default:
532c1b3d7c5SThomas E. Spanjaard 	     device_printf(softc->dev, "unknown IO operation\n");
533c1b3d7c5SThomas E. Spanjaard 	     goto action_invalid;
534c1b3d7c5SThomas E. Spanjaard 	}
535c1b3d7c5SThomas E. Spanjaard 
536c1b3d7c5SThomas E. Spanjaard 	if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
537e3869ec7SSascha Wildner 	    kprintf("cannot allocate ATAPI/CAM hcb\n");
538c1b3d7c5SThomas E. Spanjaard 	    goto action_oom;
539c1b3d7c5SThomas E. Spanjaard 	}
540c1b3d7c5SThomas E. Spanjaard 	if ((request = ata_alloc_request()) == NULL) {
541e3869ec7SSascha Wildner 	    kprintf("cannot allocate ATAPI/CAM request\n");
542c1b3d7c5SThomas E. Spanjaard 	    goto action_oom;
543c1b3d7c5SThomas E. Spanjaard 	}
544c1b3d7c5SThomas E. Spanjaard 
545c1b3d7c5SThomas E. Spanjaard 	bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
546c1b3d7c5SThomas E. Spanjaard 	      csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
547c1b3d7c5SThomas E. Spanjaard 	      request->u.atapi.ccb, csio->cdb_len);
548c1b3d7c5SThomas E. Spanjaard #ifdef CAMDEBUG
549c1b3d7c5SThomas E. Spanjaard 	if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
550c1b3d7c5SThomas E. Spanjaard 		char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
551c1b3d7c5SThomas E. Spanjaard 
552e3869ec7SSascha Wildner 		kprintf("atapi_action: hcb@%p: %s\n", hcb,
553c1b3d7c5SThomas E. Spanjaard 		       scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
554c1b3d7c5SThomas E. Spanjaard 	}
555c436b8b4SHasso Tepper 	if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_SUBTRACE)) {
556c436b8b4SHasso Tepper 		request_flags |= ATA_R_DEBUG;
557c436b8b4SHasso Tepper 	}
558c1b3d7c5SThomas E. Spanjaard #endif
559c1b3d7c5SThomas E. Spanjaard 
560c1b3d7c5SThomas E. Spanjaard 	len = csio->dxfer_len;
561c1b3d7c5SThomas E. Spanjaard 	buf = csio->data_ptr;
562c1b3d7c5SThomas E. Spanjaard 
563c1b3d7c5SThomas E. Spanjaard 	/* some SCSI commands require special processing */
564c1b3d7c5SThomas E. Spanjaard 	switch (request->u.atapi.ccb[0]) {
565c1b3d7c5SThomas E. Spanjaard 	case INQUIRY: {
566c1b3d7c5SThomas E. Spanjaard 	    /*
567c1b3d7c5SThomas E. Spanjaard 	     * many ATAPI devices seem to report more than
568c1b3d7c5SThomas E. Spanjaard 	     * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
569c1b3d7c5SThomas E. Spanjaard 	     * information, but respond with some incorrect condition
570c1b3d7c5SThomas E. Spanjaard 	     * when actually asked for it, so we are going to pretend
571c1b3d7c5SThomas E. Spanjaard 	     * that only SHORT_INQUIRY_LENGTH are expected, anyway.
572c1b3d7c5SThomas E. Spanjaard 	     */
573c1b3d7c5SThomas E. Spanjaard 	    struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
574c1b3d7c5SThomas E. Spanjaard 
575c1b3d7c5SThomas E. Spanjaard 	    if (inq->byte2 == 0 && inq->page_code == 0 &&
576c1b3d7c5SThomas E. Spanjaard 		inq->length > SHORT_INQUIRY_LENGTH) {
577c1b3d7c5SThomas E. Spanjaard 		bzero(buf, len);
578c1b3d7c5SThomas E. Spanjaard 		len = inq->length = SHORT_INQUIRY_LENGTH;
579c1b3d7c5SThomas E. Spanjaard 	    }
580c1b3d7c5SThomas E. Spanjaard 	    break;
581c1b3d7c5SThomas E. Spanjaard 	}
582c1b3d7c5SThomas E. Spanjaard 	case READ_6:
583c1b3d7c5SThomas E. Spanjaard 	    /* FALLTHROUGH */
584c1b3d7c5SThomas E. Spanjaard 
585c1b3d7c5SThomas E. Spanjaard 	case WRITE_6:
586c1b3d7c5SThomas E. Spanjaard 	    CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
587c1b3d7c5SThomas E. Spanjaard 		      ("Translating %s into _10 equivalent\n",
588c1b3d7c5SThomas E. Spanjaard 		      (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6"));
589c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[0] |= 0x20;
590c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[9] = request->u.atapi.ccb[5];
591c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[8] = request->u.atapi.ccb[4];
592c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[7] = 0;
593c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[6] = 0;
594c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[5] = request->u.atapi.ccb[3];
595c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[4] = request->u.atapi.ccb[2];
596c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f;
597c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[2] = 0;
598c1b3d7c5SThomas E. Spanjaard 	    request->u.atapi.ccb[1] = 0;
599c1b3d7c5SThomas E. Spanjaard 	    break;
600c1b3d7c5SThomas E. Spanjaard 	}
601c1b3d7c5SThomas E. Spanjaard 
602c1b3d7c5SThomas E. Spanjaard 	if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
603c1b3d7c5SThomas E. Spanjaard 	    /* ATA always transfers an even number of bytes */
6042458a87aSzrj 	    if ((buf = hcb->dxfer_alloc
6052458a87aSzrj 		 = kmalloc(++len, M_ATACAM, M_INTWAIT | M_ZERO)) == NULL) {
6062458a87aSzrj 		kprintf("cannot allocate ATAPI/CAM buffer\n");
6072458a87aSzrj 		goto action_oom;
6082458a87aSzrj 	    }
609c1b3d7c5SThomas E. Spanjaard 	}
6105b527db0SHasso Tepper 
6115b527db0SHasso Tepper 	/*
6125b527db0SHasso Tepper 	 * Don't allow DMA for requests with length not multiple of 16 bytes.
6135b527db0SHasso Tepper 	 * Some ATAPI devices don't like it.
6145b527db0SHasso Tepper 	 */
6155b527db0SHasso Tepper 	if ((softc->atadev[tid]->mode >= ATA_DMA) && len > 0 && !(len & 15))
6165b527db0SHasso Tepper 		request_flags |= ATA_R_DMA;
6175b527db0SHasso Tepper 
618c1b3d7c5SThomas E. Spanjaard 	request->dev = softc->atadev[tid]->dev;
619c1b3d7c5SThomas E. Spanjaard 	request->driver = hcb;
620c1b3d7c5SThomas E. Spanjaard 	request->data = buf;
621c1b3d7c5SThomas E. Spanjaard 	request->bytecount = len;
622405f453fSMatthew Dillon 	request->transfersize = min(request->bytecount,
623405f453fSMatthew Dillon 				min(softc->ata_ch->dma->max_iosize, 65534));
624c1b3d7c5SThomas E. Spanjaard 	request->timeout = ccb_h->timeout / 1000; /* XXX lost granularity */
625c1b3d7c5SThomas E. Spanjaard 	request->callback = &atapi_cb;
626c1b3d7c5SThomas E. Spanjaard 	request->flags = request_flags;
627c1b3d7c5SThomas E. Spanjaard 
628c436b8b4SHasso Tepper 	/*
629c436b8b4SHasso Tepper 	 * no retries are to be performed at the ATA level; any retries
630c436b8b4SHasso Tepper 	 * will be done by CAM .
631c436b8b4SHasso Tepper 	 */
632c436b8b4SHasso Tepper 	request->retries = 0;
633c436b8b4SHasso Tepper 
634c1b3d7c5SThomas E. Spanjaard 	TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
635c1b3d7c5SThomas E. Spanjaard 	hcb->flags |= QUEUED;
636c1b3d7c5SThomas E. Spanjaard 	ccb_h->status |= CAM_SIM_QUEUED;
63715bd3c73SMatthew Dillon 	lockmgr(&softc->state_lock, LK_RELEASE);
638c1b3d7c5SThomas E. Spanjaard 
639c1b3d7c5SThomas E. Spanjaard 	ata_queue_request(request);
640c1b3d7c5SThomas E. Spanjaard 	return;
641c1b3d7c5SThomas E. Spanjaard     }
642c1b3d7c5SThomas E. Spanjaard 
643c1b3d7c5SThomas E. Spanjaard     default:
644c1b3d7c5SThomas E. Spanjaard 	CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
645c1b3d7c5SThomas E. Spanjaard 		  ("unsupported function code 0x%02x\n", ccb_h->func_code));
646c1b3d7c5SThomas E. Spanjaard 	goto action_invalid;
647c1b3d7c5SThomas E. Spanjaard     }
648c1b3d7c5SThomas E. Spanjaard 
649c1b3d7c5SThomas E. Spanjaard     /* NOTREACHED */
650c1b3d7c5SThomas E. Spanjaard 
651c1b3d7c5SThomas E. Spanjaard action_oom:
652c1b3d7c5SThomas E. Spanjaard     if (request != NULL)
653c1b3d7c5SThomas E. Spanjaard 	ata_free_request(request);
654c1b3d7c5SThomas E. Spanjaard     if (hcb != NULL)
655c1b3d7c5SThomas E. Spanjaard 	free_hcb(hcb);
65615bd3c73SMatthew Dillon     lockmgr(&softc->state_lock, LK_RELEASE);
657c1b3d7c5SThomas E. Spanjaard     get_mplock();
658c1b3d7c5SThomas E. Spanjaard     xpt_print_path(ccb_h->path);
659e3869ec7SSascha Wildner     kprintf("out of memory, freezing queue.\n");
660c1b3d7c5SThomas E. Spanjaard     softc->flags |= RESOURCE_SHORTAGE;
661c1b3d7c5SThomas E. Spanjaard     xpt_freeze_simq(sim, /*count*/ 1);
662c1b3d7c5SThomas E. Spanjaard     rel_mplock();
663c1b3d7c5SThomas E. Spanjaard     ccb_h->status = CAM_REQUEUE_REQ;
664c1b3d7c5SThomas E. Spanjaard     xpt_done(ccb);
665c1b3d7c5SThomas E. Spanjaard     return;
666c1b3d7c5SThomas E. Spanjaard 
667c1b3d7c5SThomas E. Spanjaard action_invalid:
66815bd3c73SMatthew Dillon     lockmgr(&softc->state_lock, LK_RELEASE);
669c1b3d7c5SThomas E. Spanjaard     ccb_h->status = CAM_REQ_INVALID;
670c1b3d7c5SThomas E. Spanjaard     xpt_done(ccb);
671c1b3d7c5SThomas E. Spanjaard     return;
672c1b3d7c5SThomas E. Spanjaard }
673c1b3d7c5SThomas E. Spanjaard 
674c1b3d7c5SThomas E. Spanjaard static void
atapi_poll(struct cam_sim * sim)675c1b3d7c5SThomas E. Spanjaard atapi_poll(struct cam_sim *sim)
676c1b3d7c5SThomas E. Spanjaard {
677c1b3d7c5SThomas E. Spanjaard     /* do nothing - we do not actually service any interrupts */
678e3869ec7SSascha Wildner     kprintf("atapi_poll called!\n");
679c1b3d7c5SThomas E. Spanjaard }
680c1b3d7c5SThomas E. Spanjaard 
681c1b3d7c5SThomas E. Spanjaard static void
atapi_cb(struct ata_request * request)682c1b3d7c5SThomas E. Spanjaard atapi_cb(struct ata_request *request)
683c1b3d7c5SThomas E. Spanjaard {
684c1b3d7c5SThomas E. Spanjaard     struct atapi_xpt_softc *scp;
685c1b3d7c5SThomas E. Spanjaard     struct atapi_hcb *hcb;
686c1b3d7c5SThomas E. Spanjaard     struct ccb_scsiio *csio;
687c1b3d7c5SThomas E. Spanjaard     u_int32_t rc;
688c1b3d7c5SThomas E. Spanjaard 
689c1b3d7c5SThomas E. Spanjaard     hcb = (struct atapi_hcb *)request->driver;
690c1b3d7c5SThomas E. Spanjaard     scp = hcb->softc;
691c1b3d7c5SThomas E. Spanjaard     csio = &hcb->ccb->csio;
692c1b3d7c5SThomas E. Spanjaard 
693c1b3d7c5SThomas E. Spanjaard #ifdef CAMDEBUG
694c1b3d7c5SThomas E. Spanjaard # define err (request->u.atapi.sense.key)
695c1b3d7c5SThomas E. Spanjaard     if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
696c436b8b4SHasso Tepper 	kprintf("atapi_cb: hcb@%p sense = %02x: sk = %01x%s%s%s\n",
697c436b8b4SHasso Tepper 		hcb, err, err & 0x0f,
698c436b8b4SHasso Tepper 		(err & 0x80) ? ", Filemark" : "",
699c436b8b4SHasso Tepper 		(err & 0x40) ? ", EOM" : "",
700c436b8b4SHasso Tepper 		(err & 0x20) ? ", ILI" : "");
701c436b8b4SHasso Tepper 	device_printf(request->dev,
702c436b8b4SHasso Tepper 	    "cmd %s status %02x result %02x error %02x\n",
703c436b8b4SHasso Tepper 	    ata_cmd2str(request),
704c436b8b4SHasso Tepper 	    request->status, request->result, request->error);
705c1b3d7c5SThomas E. Spanjaard     }
706c1b3d7c5SThomas E. Spanjaard #endif
707c1b3d7c5SThomas E. Spanjaard 
708c1b3d7c5SThomas E. Spanjaard     if ((hcb->flags & AUTOSENSE) != 0) {
709c1b3d7c5SThomas E. Spanjaard 	rc = CAM_SCSI_STATUS_ERROR;
710c1b3d7c5SThomas E. Spanjaard 	if (request->result == 0) {
711c1b3d7c5SThomas E. Spanjaard 	    csio->ccb_h.status |= CAM_AUTOSNS_VALID;
712c1b3d7c5SThomas E. Spanjaard 	}
713c1b3d7c5SThomas E. Spanjaard     } else if (request->result != 0) {
714c436b8b4SHasso Tepper 	if ((request->flags & ATA_R_TIMEOUT) != 0) {
715c436b8b4SHasso Tepper 	    rc = CAM_CMD_TIMEOUT;
716c436b8b4SHasso Tepper 	} else {
717c1b3d7c5SThomas E. Spanjaard 	    rc = CAM_SCSI_STATUS_ERROR;
718c1b3d7c5SThomas E. Spanjaard 	    csio->scsi_status = SCSI_STATUS_CHECK_COND;
719c1b3d7c5SThomas E. Spanjaard 
720c1b3d7c5SThomas E. Spanjaard 	    if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
721c1b3d7c5SThomas E. Spanjaard #if 0
722c1b3d7c5SThomas E. Spanjaard 		static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
723c1b3d7c5SThomas E. Spanjaard 		    sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0,
724c1b3d7c5SThomas E. Spanjaard 		    0, 0, 0, 0, 0 };
725c1b3d7c5SThomas E. Spanjaard 
726c1b3d7c5SThomas E. Spanjaard 		bcopy (ccb, request->u.atapi.ccb, sizeof ccb);
727c1b3d7c5SThomas E. Spanjaard 		request->data = (caddr_t)&csio->sense_data;
728c1b3d7c5SThomas E. Spanjaard 		request->bytecount = sizeof(struct atapi_sense);
729c1b3d7c5SThomas E. Spanjaard 		request->transfersize = min(request->bytecount, 65534);
730c1b3d7c5SThomas E. Spanjaard 		request->timeout = csio->ccb_h.timeout / 1000;
731c1b3d7c5SThomas E. Spanjaard 		request->retries = 2;
732c1b3d7c5SThomas E. Spanjaard 		request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE;
733c1b3d7c5SThomas E. Spanjaard 		hcb->flags |= AUTOSENSE;
734c1b3d7c5SThomas E. Spanjaard 
735c1b3d7c5SThomas E. Spanjaard 		ata_queue_request(request);
736c1b3d7c5SThomas E. Spanjaard 		return;
737c1b3d7c5SThomas E. Spanjaard #else
738c436b8b4SHasso Tepper 		/*
739c436b8b4SHasso Tepper 		 * Use auto-sense data from the ATA layer, if it has
740c436b8b4SHasso Tepper 		 * issued a REQUEST SENSE automatically and that operation
741c436b8b4SHasso Tepper 		 * returned without error.
742c436b8b4SHasso Tepper 		 */
743c436b8b4SHasso Tepper 		if (request->u.atapi.sense.key != 0 && request->error == 0) {
744c1b3d7c5SThomas E. Spanjaard 		    bcopy (&request->u.atapi.sense, &csio->sense_data, sizeof(struct atapi_sense));
745c1b3d7c5SThomas E. Spanjaard 		    csio->ccb_h.status |= CAM_AUTOSNS_VALID;
746c1b3d7c5SThomas E. Spanjaard 		}
747c436b8b4SHasso Tepper 	    }
748c1b3d7c5SThomas E. Spanjaard #endif
749c1b3d7c5SThomas E. Spanjaard 	}
750c1b3d7c5SThomas E. Spanjaard     } else {
751c1b3d7c5SThomas E. Spanjaard 	rc = CAM_REQ_CMP;
752c1b3d7c5SThomas E. Spanjaard 	csio->scsi_status = SCSI_STATUS_OK;
753c1b3d7c5SThomas E. Spanjaard 	if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
754c1b3d7c5SThomas E. Spanjaard 	    hcb->dxfer_alloc != NULL)
755c1b3d7c5SThomas E. Spanjaard 	{
756c1b3d7c5SThomas E. Spanjaard 	    bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
757c1b3d7c5SThomas E. Spanjaard 	}
758c1b3d7c5SThomas E. Spanjaard     }
759c1b3d7c5SThomas E. Spanjaard 
76015bd3c73SMatthew Dillon     lockmgr(&scp->state_lock, LK_EXCLUSIVE);
761c1b3d7c5SThomas E. Spanjaard     free_hcb_and_ccb_done(hcb, rc);
76215bd3c73SMatthew Dillon     lockmgr(&scp->state_lock, LK_RELEASE);
763c1b3d7c5SThomas E. Spanjaard 
764c1b3d7c5SThomas E. Spanjaard     ata_free_request(request);
765c1b3d7c5SThomas E. Spanjaard }
766c1b3d7c5SThomas E. Spanjaard 
767c1b3d7c5SThomas E. Spanjaard static void
free_hcb_and_ccb_done(struct atapi_hcb * hcb,u_int32_t status)768c1b3d7c5SThomas E. Spanjaard free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
769c1b3d7c5SThomas E. Spanjaard {
770c1b3d7c5SThomas E. Spanjaard     struct atapi_xpt_softc *softc;
771c1b3d7c5SThomas E. Spanjaard     union ccb *ccb;
772c1b3d7c5SThomas E. Spanjaard 
773c1b3d7c5SThomas E. Spanjaard     if (hcb == NULL)
774c1b3d7c5SThomas E. Spanjaard 	return;
775c1b3d7c5SThomas E. Spanjaard 
776c1b3d7c5SThomas E. Spanjaard     softc = hcb->softc;
777c1b3d7c5SThomas E. Spanjaard     ccb = hcb->ccb;
778c1b3d7c5SThomas E. Spanjaard 
779c1b3d7c5SThomas E. Spanjaard     /* we're about to free a hcb, so the shortage has ended */
780c1b3d7c5SThomas E. Spanjaard     if (softc->flags & RESOURCE_SHORTAGE) {
781c1b3d7c5SThomas E. Spanjaard 	softc->flags &= ~RESOURCE_SHORTAGE;
782c1b3d7c5SThomas E. Spanjaard 	status |= CAM_RELEASE_SIMQ;
783c1b3d7c5SThomas E. Spanjaard     }
784c1b3d7c5SThomas E. Spanjaard     free_hcb(hcb);
785c1b3d7c5SThomas E. Spanjaard     ccb->ccb_h.status =
786c1b3d7c5SThomas E. Spanjaard 	status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
787c1b3d7c5SThomas E. Spanjaard     xpt_done(ccb);
788c1b3d7c5SThomas E. Spanjaard }
789c1b3d7c5SThomas E. Spanjaard 
790c1b3d7c5SThomas E. Spanjaard static void
atapi_async(void * callback_arg,u_int32_t code,struct cam_path * path,void * arg)791c1b3d7c5SThomas E. Spanjaard atapi_async(void *callback_arg, u_int32_t code,
792c1b3d7c5SThomas E. Spanjaard 	     struct cam_path* path, void *arg)
793c1b3d7c5SThomas E. Spanjaard {
794c1b3d7c5SThomas E. Spanjaard     int targ;
795c1b3d7c5SThomas E. Spanjaard 
796c1b3d7c5SThomas E. Spanjaard     crit_enter();
797c1b3d7c5SThomas E. Spanjaard 
798c1b3d7c5SThomas E. Spanjaard     switch (code) {
799c1b3d7c5SThomas E. Spanjaard     case AC_LOST_DEVICE:
800c1b3d7c5SThomas E. Spanjaard 	targ = xpt_path_target_id(path);
801c1b3d7c5SThomas E. Spanjaard 	xpt_print_path(path);
802c1b3d7c5SThomas E. Spanjaard 	if (targ == -1)
803e3869ec7SSascha Wildner 		kprintf("Lost host adapter\n");
804c1b3d7c5SThomas E. Spanjaard 	else
805e3869ec7SSascha Wildner 		kprintf("Lost target %d???\n", targ);
806c1b3d7c5SThomas E. Spanjaard 	break;
807c1b3d7c5SThomas E. Spanjaard 
808c1b3d7c5SThomas E. Spanjaard     default:
809c1b3d7c5SThomas E. Spanjaard 	break;
810c1b3d7c5SThomas E. Spanjaard     }
811c1b3d7c5SThomas E. Spanjaard 
812c1b3d7c5SThomas E. Spanjaard     crit_exit();
813c1b3d7c5SThomas E. Spanjaard }
814c1b3d7c5SThomas E. Spanjaard 
815c1b3d7c5SThomas E. Spanjaard static void
cam_rescan_callback(struct cam_periph * periph,union ccb * ccb)816c1b3d7c5SThomas E. Spanjaard cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
817c1b3d7c5SThomas E. Spanjaard {
818c1b3d7c5SThomas E. Spanjaard 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
819c1b3d7c5SThomas E. Spanjaard 	    CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
820c1b3d7c5SThomas E. Spanjaard 		      ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
821c1b3d7c5SThomas E. Spanjaard 	} else {
822c1b3d7c5SThomas E. Spanjaard 	    CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
823c1b3d7c5SThomas E. Spanjaard 		      ("Rescan succeeded\n"));
824c1b3d7c5SThomas E. Spanjaard 	}
825c1b3d7c5SThomas E. Spanjaard 	xpt_free_path(ccb->ccb_h.path);
826*cec957e9SMatthew Dillon 	xpt_free_ccb(&ccb->ccb_h);
827c1b3d7c5SThomas E. Spanjaard }
828c1b3d7c5SThomas E. Spanjaard 
829c1b3d7c5SThomas E. Spanjaard static void
cam_rescan(struct cam_sim * sim)830c1b3d7c5SThomas E. Spanjaard cam_rescan(struct cam_sim *sim)
831c1b3d7c5SThomas E. Spanjaard {
832c1b3d7c5SThomas E. Spanjaard     struct cam_path *path;
833*cec957e9SMatthew Dillon     union ccb *ccb;
834*cec957e9SMatthew Dillon 
835*cec957e9SMatthew Dillon     ccb = xpt_alloc_ccb();
836c1b3d7c5SThomas E. Spanjaard 
837c1b3d7c5SThomas E. Spanjaard     get_mplock();
838c1b3d7c5SThomas E. Spanjaard     if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
839c1b3d7c5SThomas E. Spanjaard 			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
840c1b3d7c5SThomas E. Spanjaard 	rel_mplock();
841*cec957e9SMatthew Dillon 	xpt_free_ccb(&ccb->ccb_h);
842c1b3d7c5SThomas E. Spanjaard 	return;
843c1b3d7c5SThomas E. Spanjaard     }
844c1b3d7c5SThomas E. Spanjaard 
845c436b8b4SHasso Tepper     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
846c1b3d7c5SThomas E. Spanjaard     xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
847c1b3d7c5SThomas E. Spanjaard     ccb->ccb_h.func_code = XPT_SCAN_BUS;
848c1b3d7c5SThomas E. Spanjaard     ccb->ccb_h.cbfcnp = cam_rescan_callback;
849c1b3d7c5SThomas E. Spanjaard     ccb->crcn.flags = CAM_FLAG_NONE;
850c1b3d7c5SThomas E. Spanjaard     xpt_action(ccb);
851c1b3d7c5SThomas E. Spanjaard     /* scan is in progress now */
852c1b3d7c5SThomas E. Spanjaard     rel_mplock();
853c1b3d7c5SThomas E. Spanjaard }
854c1b3d7c5SThomas E. Spanjaard 
855c1b3d7c5SThomas E. Spanjaard static struct atapi_hcb *
allocate_hcb(struct atapi_xpt_softc * softc,int unit,int bus,union ccb * ccb)856c1b3d7c5SThomas E. Spanjaard allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
857c1b3d7c5SThomas E. Spanjaard {
858c1b3d7c5SThomas E. Spanjaard     struct atapi_hcb *hcb = (struct atapi_hcb *)
859a01741bbSMatthew Dillon 	kmalloc(sizeof(struct atapi_hcb), M_ATACAM, M_INTWAIT | M_ZERO);
860c1b3d7c5SThomas E. Spanjaard 
861c1b3d7c5SThomas E. Spanjaard     hcb->softc = softc;
862c1b3d7c5SThomas E. Spanjaard     hcb->unit = unit;
863c1b3d7c5SThomas E. Spanjaard     hcb->bus = bus;
864c1b3d7c5SThomas E. Spanjaard     hcb->ccb = ccb;
865c1b3d7c5SThomas E. Spanjaard     return hcb;
866c1b3d7c5SThomas E. Spanjaard }
867c1b3d7c5SThomas E. Spanjaard 
868c1b3d7c5SThomas E. Spanjaard static void
free_hcb(struct atapi_hcb * hcb)869c1b3d7c5SThomas E. Spanjaard free_hcb(struct atapi_hcb *hcb)
870c1b3d7c5SThomas E. Spanjaard {
871c1b3d7c5SThomas E. Spanjaard     if ((hcb->flags & QUEUED) != 0)
872c1b3d7c5SThomas E. Spanjaard 	TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
873c1b3d7c5SThomas E. Spanjaard     if (hcb->dxfer_alloc != NULL)
874c1b3d7c5SThomas E. Spanjaard 	kfree(hcb->dxfer_alloc, M_ATACAM);
875c1b3d7c5SThomas E. Spanjaard     kfree(hcb, M_ATACAM);
876c1b3d7c5SThomas E. Spanjaard }
877c1b3d7c5SThomas E. Spanjaard 
878c1b3d7c5SThomas E. Spanjaard static void
free_softc(struct atapi_xpt_softc * scp)879c1b3d7c5SThomas E. Spanjaard free_softc(struct atapi_xpt_softc *scp)
880c1b3d7c5SThomas E. Spanjaard {
881c1b3d7c5SThomas E. Spanjaard     struct atapi_hcb *hcb;
882c1b3d7c5SThomas E. Spanjaard 
883c1b3d7c5SThomas E. Spanjaard     if (scp != NULL) {
88415bd3c73SMatthew Dillon 	lockmgr(&scp->state_lock, LK_EXCLUSIVE);
885c1b3d7c5SThomas E. Spanjaard 	TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
886c1b3d7c5SThomas E. Spanjaard 	    free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
887c1b3d7c5SThomas E. Spanjaard 	}
88815bd3c73SMatthew Dillon 	lockmgr(&scp->state_lock, LK_RELEASE);
889c1b3d7c5SThomas E. Spanjaard 	get_mplock();
890c1b3d7c5SThomas E. Spanjaard 	if (scp->path != NULL) {
891c1b3d7c5SThomas E. Spanjaard 	    setup_async_cb(scp, 0);
892c1b3d7c5SThomas E. Spanjaard 	    xpt_free_path(scp->path);
893c1b3d7c5SThomas E. Spanjaard 	}
894c1b3d7c5SThomas E. Spanjaard 	if ((scp->flags & BUS_REGISTERED) != 0) {
895c1b3d7c5SThomas E. Spanjaard 	    if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
896c1b3d7c5SThomas E. Spanjaard 		scp->flags &= ~BUS_REGISTERED;
897c1b3d7c5SThomas E. Spanjaard 	}
898c1b3d7c5SThomas E. Spanjaard 	if (scp->sim != NULL) {
899c1b3d7c5SThomas E. Spanjaard 	    if ((scp->flags & BUS_REGISTERED) == 0)
900d6aaf612SThomas E. Spanjaard 		cam_sim_free(scp->sim);
901c1b3d7c5SThomas E. Spanjaard 	    else
902e3869ec7SSascha Wildner 		kprintf("Can't free %s SIM (still registered)\n",
903c1b3d7c5SThomas E. Spanjaard 		       cam_sim_name(scp->sim));
904c1b3d7c5SThomas E. Spanjaard 	}
905c1b3d7c5SThomas E. Spanjaard 	rel_mplock();
90615bd3c73SMatthew Dillon 	lockuninit(&scp->state_lock);
907c1b3d7c5SThomas E. Spanjaard     }
908c1b3d7c5SThomas E. Spanjaard }
909c1b3d7c5SThomas E. Spanjaard 
910c1b3d7c5SThomas E. Spanjaard static int
atapi_cam_event_handler(module_t mod,int what,void * arg)911c1b3d7c5SThomas E. Spanjaard atapi_cam_event_handler(module_t mod, int what, void *arg) {
912c1b3d7c5SThomas E. Spanjaard     device_t *devlist;
913c1b3d7c5SThomas E. Spanjaard     int devcount;
914c1b3d7c5SThomas E. Spanjaard 
915c1b3d7c5SThomas E. Spanjaard     switch (what) {
916c1b3d7c5SThomas E. Spanjaard 	case MOD_UNLOAD:
917c1b3d7c5SThomas E. Spanjaard 	    if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
918c1b3d7c5SThomas E. Spanjaard 		  != 0)
919c1b3d7c5SThomas E. Spanjaard 		return ENXIO;
920c1b3d7c5SThomas E. Spanjaard 	    if (devlist != NULL) {
921c1b3d7c5SThomas E. Spanjaard 		while (devlist != NULL && devcount > 0) {
922c1b3d7c5SThomas E. Spanjaard 		    device_t child = devlist[--devcount];
923c1b3d7c5SThomas E. Spanjaard 		    struct atapi_xpt_softc *scp = device_get_softc(child);
924c1b3d7c5SThomas E. Spanjaard 
925c1b3d7c5SThomas E. Spanjaard 		    device_delete_child(device_get_parent(child),child);
926c1b3d7c5SThomas E. Spanjaard 		    if (scp != NULL)
927c1b3d7c5SThomas E. Spanjaard 			kfree(scp, M_ATACAM);
928c1b3d7c5SThomas E. Spanjaard 		}
929c1b3d7c5SThomas E. Spanjaard 		kfree(devlist, M_TEMP);
930c1b3d7c5SThomas E. Spanjaard 	    }
931c1b3d7c5SThomas E. Spanjaard 	    break;
932c1b3d7c5SThomas E. Spanjaard 
933c1b3d7c5SThomas E. Spanjaard 	default:
934c1b3d7c5SThomas E. Spanjaard 	    break;
935c1b3d7c5SThomas E. Spanjaard     }
936c1b3d7c5SThomas E. Spanjaard     return 0;
937c1b3d7c5SThomas E. Spanjaard }
938