xref: /dragonfly/sys/dev/disk/vpo/vpo.c (revision 77b0c609)
1 /*-
2  * Copyright (c) 1997, 1998, 1999 Nicolas Souchu
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ppbus/vpo.c,v 1.20.2.1 2000/05/07 21:08:18 n_hibma Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/malloc.h>
34 #include <sys/devicestat.h>	/* for struct devstat */
35 #include <sys/thread2.h>	/* for crit_*() */
36 
37 #include <machine/clock.h>
38 
39 #include <bus/cam/cam.h>
40 #include <bus/cam/cam_ccb.h>
41 #include <bus/cam/cam_sim.h>
42 #include <bus/cam/cam_xpt_sim.h>
43 #include <bus/cam/cam_debug.h>
44 #include <bus/cam/cam_periph.h>
45 
46 #include <bus/cam/scsi/scsi_all.h>
47 #include <bus/cam/scsi/scsi_message.h>
48 #include <bus/cam/scsi/scsi_da.h>
49 
50 #include <sys/kernel.h>
51 
52 #include "opt_vpo.h"
53 
54 #include <bus/ppbus/ppbconf.h>
55 #include "vpoio.h"
56 
57 #include "ppbus_if.h"
58 
59 struct vpo_sense {
60 	struct scsi_sense cmd;
61 	unsigned int stat;
62 	unsigned int count;
63 };
64 
65 struct vpo_data {
66 	unsigned short vpo_unit;
67 
68 	int vpo_stat;
69 	int vpo_count;
70 	int vpo_error;
71 
72 	int vpo_isplus;
73 
74 	struct cam_sim  *sim;
75 
76 	struct vpo_sense vpo_sense;
77 
78 	struct vpoio_data vpo_io;	/* interface to low level functions */
79 };
80 
81 #define DEVTOSOFTC(dev) \
82 	((struct vpo_data *)device_get_softc(dev))
83 
84 /* cam related functions */
85 static void	vpo_action(struct cam_sim *sim, union ccb *ccb);
86 static void	vpo_poll(struct cam_sim *sim);
87 static void	vpo_cam_rescan_callback(struct cam_periph *periph,
88 					union ccb *ccb);
89 static void	vpo_cam_rescan(struct vpo_data *vpo);
90 
91 /*
92  * vpo_probe()
93  */
94 static int
95 vpo_probe(device_t dev)
96 {
97 	struct vpo_data *vpo;
98 	int error;
99 
100 	vpo = DEVTOSOFTC(dev);
101 	bzero(vpo, sizeof(struct vpo_data));
102 
103 	/* vpo dependent initialisation */
104 	vpo->vpo_unit = device_get_unit(dev);
105 
106 	/* low level probe */
107 	vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
108 
109 	/* check ZIP before ZIP+ or imm_probe() will send controls to
110 	 * the printer or whatelse connected to the port */
111 	if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
112 		vpo->vpo_isplus = 0;
113 		device_set_desc(dev,
114 				"Iomega VPI0 Parallel to SCSI interface");
115 	} else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
116 		vpo->vpo_isplus = 1;
117 		device_set_desc(dev,
118 				"Iomega Matchmaker Parallel to SCSI interface");
119 	} else {
120 		return (error);
121 	}
122 
123 	return (0);
124 }
125 
126 /*
127  * vpo_attach()
128  */
129 static int
130 vpo_attach(device_t dev)
131 {
132 	struct vpo_data *vpo = DEVTOSOFTC(dev);
133 	struct cam_devq *devq;
134 	int error;
135 
136 	/* low level attachment */
137 	if (vpo->vpo_isplus) {
138 		if ((error = imm_attach(&vpo->vpo_io)))
139 			return (error);
140 	} else {
141 		if ((error = vpoio_attach(&vpo->vpo_io)))
142 			return (error);
143 	}
144 
145 	/*
146 	**	Now tell the generic SCSI layer
147 	**	about our bus.
148 	*/
149 	devq = cam_simq_alloc(/*maxopenings*/1);
150 	/* XXX What about low-level detach on error? */
151 	if (devq == NULL)
152 		return (ENXIO);
153 
154 	vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
155 				 device_get_unit(dev), &sim_mplock,
156 				 /*untagged*/1, /*tagged*/0, devq);
157 	cam_simq_release(devq);
158 	if (vpo->sim == NULL) {
159 		return (ENXIO);
160 	}
161 
162 	if (xpt_bus_register(vpo->sim, /*bus*/0) != CAM_SUCCESS) {
163 		cam_sim_free(vpo->sim);
164 		return (ENXIO);
165 	}
166 
167 	/* all went ok */
168 
169 	vpo_cam_rescan(vpo);	/* have CAM rescan the bus */
170 
171 	return (0);
172 }
173 
174 static void
175 vpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
176 {
177         kfree(ccb, M_TEMP);
178 }
179 
180 static void
181 vpo_cam_rescan(struct vpo_data *vpo)
182 {
183         struct cam_path *path;
184         union ccb *ccb = kmalloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO);
185 
186         if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0)
187             != CAM_REQ_CMP) {
188 		/* A failure is benign as the user can do a manual rescan */
189                 return;
190 	}
191 
192         xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
193         ccb->ccb_h.func_code = XPT_SCAN_BUS;
194         ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback;
195         ccb->crcn.flags = CAM_FLAG_NONE;
196         xpt_action(ccb);
197 
198         /* The scan is in progress now. */
199 }
200 
201 /*
202  * vpo_intr()
203  */
204 static void
205 vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
206 {
207 	int error;
208 #ifdef VP0_DEBUG
209 	int i;
210 #endif
211 
212 	crit_enter();
213 
214 	if (vpo->vpo_isplus) {
215 		error = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
216 			csio->ccb_h.target_id,
217 			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
218 			(char *)csio->data_ptr, csio->dxfer_len,
219 			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
220 	} else {
221 		error = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
222 			csio->ccb_h.target_id,
223 			(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
224 			(char *)csio->data_ptr, csio->dxfer_len,
225 			&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
226 	}
227 
228 #ifdef VP0_DEBUG
229 	kprintf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
230 		 error, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
231 
232 	/* dump of command */
233 	for (i=0; i<csio->cdb_len; i++)
234 		kprintf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
235 
236 	kprintf("\n");
237 #endif
238 
239 	if (error) {
240 		/* connection to ppbus interrupted */
241 		csio->ccb_h.status = CAM_CMD_TIMEOUT;
242 		goto error;
243 	}
244 
245 	/* if a timeout occured, no sense */
246 	if (vpo->vpo_error) {
247 		if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
248 			kprintf("vpo%d: VP0 error/timeout (%d)\n",
249 				vpo->vpo_unit, vpo->vpo_error);
250 
251 		csio->ccb_h.status = CAM_CMD_TIMEOUT;
252 		goto error;
253 	}
254 
255 	/* check scsi status */
256 	if (vpo->vpo_stat != SCSI_STATUS_OK) {
257 	   csio->scsi_status = vpo->vpo_stat;
258 
259 	   /* check if we have to sense the drive */
260 	   if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
261 
262 		vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
263 		vpo->vpo_sense.cmd.length = csio->sense_len;
264 		vpo->vpo_sense.cmd.control = 0;
265 
266 		if (vpo->vpo_isplus) {
267 			error = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
268 				csio->ccb_h.target_id,
269 				(char *)&vpo->vpo_sense.cmd,
270 				sizeof(vpo->vpo_sense.cmd),
271 				(char *)&csio->sense_data, csio->sense_len,
272 				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
273 				&vpo->vpo_error);
274 		} else {
275 			error = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
276 				csio->ccb_h.target_id,
277 				(char *)&vpo->vpo_sense.cmd,
278 				sizeof(vpo->vpo_sense.cmd),
279 				(char *)&csio->sense_data, csio->sense_len,
280 				&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
281 				&vpo->vpo_error);
282 		}
283 
284 
285 #ifdef VP0_DEBUG
286 		kprintf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
287 			error, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
288 #endif
289 
290 		/* check sense return status */
291 		if (error == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
292 		   /* sense ok */
293 		   csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
294 		   csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
295 
296 #ifdef VP0_DEBUG
297 		   /* dump of sense info */
298 		   kprintf("(sense) ");
299 		   for (i=0; i<vpo->vpo_sense.count; i++)
300 			kprintf("%x ", ((char *)&csio->sense_data)[i]);
301 		   kprintf("\n");
302 #endif
303 
304 		} else {
305 		   /* sense failed */
306 		   csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
307 		}
308 	   } else {
309 		/* no sense */
310 		csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
311 	   }
312 
313 	   goto error;
314 	}
315 
316 	csio->resid = csio->dxfer_len - vpo->vpo_count;
317 	csio->ccb_h.status = CAM_REQ_CMP;
318 
319 error:
320 	crit_exit();
321 
322 	return;
323 }
324 
325 static void
326 vpo_action(struct cam_sim *sim, union ccb *ccb)
327 {
328 
329 	struct vpo_data *vpo = (struct vpo_data *)sim->softc;
330 
331 	switch (ccb->ccb_h.func_code) {
332 	case XPT_SCSI_IO:
333 	{
334 		struct ccb_scsiio *csio;
335 
336 		csio = &ccb->csio;
337 
338 #ifdef VP0_DEBUG
339 		kprintf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
340 			vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
341 #endif
342 
343 		vpo_intr(vpo, csio);
344 
345 		xpt_done(ccb);
346 
347 		break;
348 	}
349 	case XPT_CALC_GEOMETRY:
350 	{
351 		struct	  ccb_calc_geometry *ccg;
352 
353 		ccg = &ccb->ccg;
354 
355 #ifdef VP0_DEBUG
356 		kprintf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%ju,c=%d,h=%d,spt=%d) request\n",
357 			vpo->vpo_unit,
358 			ccg->block_size,
359 			(uintmax_t)ccg->volume_size,
360 			ccg->cylinders,
361 			ccg->heads,
362 			ccg->secs_per_track);
363 #endif
364 
365 		ccg->heads = 64;
366 		ccg->secs_per_track = 32;
367 		ccg->cylinders = ccg->volume_size /
368 				 (ccg->heads * ccg->secs_per_track);
369 
370 		ccb->ccb_h.status = CAM_REQ_CMP;
371 		xpt_done(ccb);
372 		break;
373 	}
374 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
375 	{
376 
377 #ifdef VP0_DEBUG
378 		kprintf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
379 #endif
380 
381 		if (vpo->vpo_isplus) {
382 			if (imm_reset_bus(&vpo->vpo_io)) {
383 				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
384 				xpt_done(ccb);
385 				return;
386 			}
387 		} else {
388 			if (vpoio_reset_bus(&vpo->vpo_io)) {
389 				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
390 				xpt_done(ccb);
391 				return;
392 			}
393 		}
394 
395 		ccb->ccb_h.status = CAM_REQ_CMP;
396 		xpt_done(ccb);
397 		break;
398 	}
399 	case XPT_PATH_INQ:		/* Path routing inquiry */
400 	{
401 		struct ccb_pathinq *cpi = &ccb->cpi;
402 
403 #ifdef VP0_DEBUG
404 		kprintf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
405 #endif
406 		cpi->version_num = 1; /* XXX??? */
407 		cpi->hba_inquiry = 0;
408 		cpi->target_sprt = 0;
409 		cpi->hba_misc = 0;
410 		cpi->hba_eng_cnt = 0;
411 		cpi->max_target = 7;
412 		cpi->max_lun = 0;
413 		cpi->initiator_id = VP0_INITIATOR;
414 		cpi->bus_id = sim->bus_id;
415 		cpi->base_transfer_speed = 93;
416 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
417 		strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
418 		strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
419 		cpi->unit_number = sim->unit_number;
420 
421 		cpi->ccb_h.status = CAM_REQ_CMP;
422 		xpt_done(ccb);
423 		break;
424 	}
425 	default:
426 		ccb->ccb_h.status = CAM_REQ_INVALID;
427 		xpt_done(ccb);
428 		break;
429 	}
430 
431 	return;
432 }
433 
434 static void
435 vpo_poll(struct cam_sim *sim)
436 {
437 	/* The ZIP is actually always polled throw vpo_action() */
438 	return;
439 }
440 
441 /*
442  * Always create a "vpo" device under ppbus.  Use device_identify to
443  * create the static entry for any attached ppbus.
444  */
445 static devclass_t vpo_devclass;
446 
447 static device_method_t vpo_methods[] = {
448 	/* device interface */
449 	DEVMETHOD(device_identify,	bus_generic_identify),
450 	DEVMETHOD(device_probe,		vpo_probe),
451 	DEVMETHOD(device_attach,	vpo_attach),
452 	{ 0, 0 }
453 };
454 
455 static driver_t vpo_driver = {
456 	"vpo",
457 	vpo_methods,
458 	sizeof(struct vpo_data),
459 };
460 DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, NULL, NULL);
461 
462