FreeBSD/Linux Kernel Cross Reference
sys/dev/ppbus/vpo.c
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 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/malloc.h>
38
39 #include <cam/cam.h>
40 #include <cam/cam_ccb.h>
41 #include <cam/cam_sim.h>
42 #include <cam/cam_xpt_sim.h>
43 #include <cam/cam_debug.h>
44 #include <cam/cam_periph.h>
45
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/scsi_message.h>
48 #include <cam/scsi/scsi_da.h>
49
50 #include <sys/kernel.h>
51
52 #include "opt_vpo.h"
53
54 #include <dev/ppbus/ppbconf.h>
55 #include <dev/ppbus/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 device_t vpo_dev;
67 int vpo_stat;
68 int vpo_count;
69 int vpo_error;
70
71 int vpo_isplus;
72
73 struct cam_sim *sim;
74
75 struct vpo_sense vpo_sense;
76
77 struct vpoio_data vpo_io; /* interface to low level functions */
78 };
79
80 #define DEVTOSOFTC(dev) \
81 ((struct vpo_data *)device_get_softc(dev))
82
83 /* cam related functions */
84 static void vpo_action(struct cam_sim *sim, union ccb *ccb);
85 static void vpo_poll(struct cam_sim *sim);
86
87 static void
88 vpo_identify(driver_t *driver, device_t parent)
89 {
90
91 device_t dev;
92
93 dev = device_find_child(parent, "vpo", -1);
94 if (!dev)
95 BUS_ADD_CHILD(parent, 0, "vpo", -1);
96 }
97
98 /*
99 * vpo_probe()
100 */
101 static int
102 vpo_probe(device_t dev)
103 {
104 device_t ppbus = device_get_parent(dev);
105 struct vpo_data *vpo;
106 int error;
107
108 vpo = DEVTOSOFTC(dev);
109 vpo->vpo_dev = dev;
110
111 /* check ZIP before ZIP+ or imm_probe() will send controls to
112 * the printer or whatelse connected to the port */
113 ppb_lock(ppbus);
114 if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
115 vpo->vpo_isplus = 0;
116 device_set_desc(dev,
117 "Iomega VPI0 Parallel to SCSI interface");
118 } else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
119 vpo->vpo_isplus = 1;
120 device_set_desc(dev,
121 "Iomega Matchmaker Parallel to SCSI interface");
122 } else {
123 ppb_unlock(ppbus);
124 return (error);
125 }
126 ppb_unlock(ppbus);
127
128 return (0);
129 }
130
131 /*
132 * vpo_attach()
133 */
134 static int
135 vpo_attach(device_t dev)
136 {
137 struct vpo_data *vpo = DEVTOSOFTC(dev);
138 device_t ppbus = device_get_parent(dev);
139 struct ppb_data *ppb = device_get_softc(ppbus); /* XXX: layering */
140 struct cam_devq *devq;
141 int error;
142
143 /* low level attachment */
144 if (vpo->vpo_isplus) {
145 if ((error = imm_attach(&vpo->vpo_io)))
146 return (error);
147 } else {
148 if ((error = vpoio_attach(&vpo->vpo_io)))
149 return (error);
150 }
151
152 /*
153 ** Now tell the generic SCSI layer
154 ** about our bus.
155 */
156 devq = cam_simq_alloc(/*maxopenings*/1);
157 /* XXX What about low-level detach on error? */
158 if (devq == NULL)
159 return (ENXIO);
160
161 vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
162 device_get_unit(dev), ppb->ppc_lock,
163 /*untagged*/1, /*tagged*/0, devq);
164 if (vpo->sim == NULL) {
165 cam_simq_free(devq);
166 return (ENXIO);
167 }
168
169 ppb_lock(ppbus);
170 if (xpt_bus_register(vpo->sim, dev, /*bus*/0) != CAM_SUCCESS) {
171 cam_sim_free(vpo->sim, /*free_devq*/TRUE);
172 ppb_unlock(ppbus);
173 return (ENXIO);
174 }
175 ppb_unlock(ppbus);
176
177 return (0);
178 }
179
180 /*
181 * vpo_intr()
182 */
183 static void
184 vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
185 {
186 int errno; /* error in errno.h */
187 #ifdef VP0_DEBUG
188 int i;
189 #endif
190
191 if (vpo->vpo_isplus) {
192 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
193 csio->ccb_h.target_id,
194 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
195 (char *)csio->data_ptr, csio->dxfer_len,
196 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
197 } else {
198 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
199 csio->ccb_h.target_id,
200 (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
201 (char *)csio->data_ptr, csio->dxfer_len,
202 &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
203 }
204
205 #ifdef VP0_DEBUG
206 printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
207 errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
208
209 /* dump of command */
210 for (i=0; i<csio->cdb_len; i++)
211 printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
212
213 printf("\n");
214 #endif
215
216 if (errno) {
217 /* connection to ppbus interrupted */
218 csio->ccb_h.status = CAM_CMD_TIMEOUT;
219 return;
220 }
221
222 /* if a timeout occured, no sense */
223 if (vpo->vpo_error) {
224 if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
225 device_printf(vpo->vpo_dev, "VP0 error/timeout (%d)\n",
226 vpo->vpo_error);
227
228 csio->ccb_h.status = CAM_CMD_TIMEOUT;
229 return;
230 }
231
232 /* check scsi status */
233 if (vpo->vpo_stat != SCSI_STATUS_OK) {
234 csio->scsi_status = vpo->vpo_stat;
235
236 /* check if we have to sense the drive */
237 if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
238
239 vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
240 vpo->vpo_sense.cmd.length = csio->sense_len;
241 vpo->vpo_sense.cmd.control = 0;
242
243 if (vpo->vpo_isplus) {
244 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
245 csio->ccb_h.target_id,
246 (char *)&vpo->vpo_sense.cmd,
247 sizeof(vpo->vpo_sense.cmd),
248 (char *)&csio->sense_data, csio->sense_len,
249 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
250 &vpo->vpo_error);
251 } else {
252 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
253 csio->ccb_h.target_id,
254 (char *)&vpo->vpo_sense.cmd,
255 sizeof(vpo->vpo_sense.cmd),
256 (char *)&csio->sense_data, csio->sense_len,
257 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
258 &vpo->vpo_error);
259 }
260
261
262 #ifdef VP0_DEBUG
263 printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
264 errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
265 #endif
266
267 /* check sense return status */
268 if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
269 /* sense ok */
270 csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
271 csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
272
273 #ifdef VP0_DEBUG
274 /* dump of sense info */
275 printf("(sense) ");
276 for (i=0; i<vpo->vpo_sense.count; i++)
277 printf("%x ", ((char *)&csio->sense_data)[i]);
278 printf("\n");
279 #endif
280
281 } else {
282 /* sense failed */
283 csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
284 }
285 } else {
286 /* no sense */
287 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
288 }
289
290 return;
291 }
292
293 csio->resid = csio->dxfer_len - vpo->vpo_count;
294 csio->ccb_h.status = CAM_REQ_CMP;
295 }
296
297 static void
298 vpo_action(struct cam_sim *sim, union ccb *ccb)
299 {
300 struct vpo_data *vpo = (struct vpo_data *)sim->softc;
301 #ifdef INVARIANTS
302 device_t ppbus = device_get_parent(vpo->vpo_dev);
303
304 ppb_assert_locked(ppbus);
305 #endif
306 switch (ccb->ccb_h.func_code) {
307 case XPT_SCSI_IO:
308 {
309 struct ccb_scsiio *csio;
310
311 csio = &ccb->csio;
312
313 #ifdef VP0_DEBUG
314 device_printf(vpo->vpo_dev, "XPT_SCSI_IO (0x%x) request\n",
315 csio->cdb_io.cdb_bytes[0]);
316 #endif
317
318 vpo_intr(vpo, csio);
319
320 xpt_done(ccb);
321
322 break;
323 }
324 case XPT_CALC_GEOMETRY:
325 {
326 struct ccb_calc_geometry *ccg;
327
328 ccg = &ccb->ccg;
329
330 #ifdef VP0_DEBUG
331 device_printf(vpo->vpo_dev, "XPT_CALC_GEOMETRY (bs=%d,vs=%jd,c=%d,h=%d,spt=%d) request\n",
332 ccg->block_size,
333 (intmax_t)ccg->volume_size,
334 ccg->cylinders,
335 ccg->heads,
336 ccg->secs_per_track);
337 #endif
338
339 ccg->heads = 64;
340 ccg->secs_per_track = 32;
341 ccg->cylinders = ccg->volume_size /
342 (ccg->heads * ccg->secs_per_track);
343
344 ccb->ccb_h.status = CAM_REQ_CMP;
345 xpt_done(ccb);
346 break;
347 }
348 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
349 {
350
351 #ifdef VP0_DEBUG
352 device_printf(vpo->vpo_dev, "XPT_RESET_BUS request\n");
353 #endif
354
355 if (vpo->vpo_isplus) {
356 if (imm_reset_bus(&vpo->vpo_io)) {
357 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
358 xpt_done(ccb);
359 return;
360 }
361 } else {
362 if (vpoio_reset_bus(&vpo->vpo_io)) {
363 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
364 xpt_done(ccb);
365 return;
366 }
367 }
368
369 ccb->ccb_h.status = CAM_REQ_CMP;
370 xpt_done(ccb);
371 break;
372 }
373 case XPT_PATH_INQ: /* Path routing inquiry */
374 {
375 struct ccb_pathinq *cpi = &ccb->cpi;
376
377 #ifdef VP0_DEBUG
378 device_printf(vpo->vpo_dev, "XPT_PATH_INQ request\n");
379 #endif
380 cpi->version_num = 1; /* XXX??? */
381 cpi->hba_inquiry = 0;
382 cpi->target_sprt = 0;
383 cpi->hba_misc = 0;
384 cpi->hba_eng_cnt = 0;
385 cpi->max_target = 7;
386 cpi->max_lun = 0;
387 cpi->initiator_id = VP0_INITIATOR;
388 cpi->bus_id = sim->bus_id;
389 cpi->base_transfer_speed = 93;
390 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
391 strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
392 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
393 cpi->unit_number = sim->unit_number;
394 cpi->transport = XPORT_PPB;
395 cpi->transport_version = 0;
396
397 cpi->ccb_h.status = CAM_REQ_CMP;
398 xpt_done(ccb);
399 break;
400 }
401 default:
402 ccb->ccb_h.status = CAM_REQ_INVALID;
403 xpt_done(ccb);
404 break;
405 }
406
407 return;
408 }
409
410 static void
411 vpo_poll(struct cam_sim *sim)
412 {
413
414 /* The ZIP is actually always polled throw vpo_action(). */
415 }
416
417 static devclass_t vpo_devclass;
418
419 static device_method_t vpo_methods[] = {
420 /* device interface */
421 DEVMETHOD(device_identify, vpo_identify),
422 DEVMETHOD(device_probe, vpo_probe),
423 DEVMETHOD(device_attach, vpo_attach),
424
425 { 0, 0 }
426 };
427
428 static driver_t vpo_driver = {
429 "vpo",
430 vpo_methods,
431 sizeof(struct vpo_data),
432 };
433 DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);
434 MODULE_DEPEND(vpo, ppbus, 1, 1, 1);
435 MODULE_DEPEND(vpo, cam, 1, 1, 1);
Cache object: ef7f4625ff17ee62ea419c3079a1b0cf
|