Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / disk / vpo / 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  * $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
36 #include <machine/clock.h>
37
38 #include <cam/cam.h>
39 #include <cam/cam_ccb.h>
40 #include <cam/cam_sim.h>
41 #include <cam/cam_xpt_sim.h>
42 #include <cam/cam_debug.h>
43 #include <cam/cam_periph.h>
44
45 #include <cam/scsi/scsi_all.h>
46 #include <cam/scsi/scsi_message.h>
47 #include <cam/scsi/scsi_da.h>
48
49 #include <sys/kernel.h>
50
51 #include "opt_vpo.h"
52
53 #include <dev/ppbus/ppbconf.h>
54 #include <dev/ppbus/vpoio.h>
55
56 #include "ppbus_if.h"
57
58 struct vpo_sense {
59         struct scsi_sense cmd;
60         unsigned int stat;
61         unsigned int count;
62 };
63
64 struct vpo_data {
65         unsigned short vpo_unit;
66
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 static void     vpo_cam_rescan_callback(struct cam_periph *periph,
87                                         union ccb *ccb);
88 static void     vpo_cam_rescan(struct vpo_data *vpo);
89
90 static void
91 vpo_identify(driver_t *driver, device_t parent)
92 {
93
94         BUS_ADD_CHILD(parent, 0, "vpo", 0);
95 }
96
97 /*
98  * vpo_probe()
99  */
100 static int
101 vpo_probe(device_t dev)
102 {
103         struct vpo_data *vpo;
104         int error;
105
106         vpo = DEVTOSOFTC(dev);
107         bzero(vpo, sizeof(struct vpo_data));
108
109         /* vpo dependent initialisation */
110         vpo->vpo_unit = device_get_unit(dev);
111
112         /* low level probe */
113         vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
114
115         /* check ZIP before ZIP+ or imm_probe() will send controls to
116          * the printer or whatelse connected to the port */
117         if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
118                 vpo->vpo_isplus = 0;
119                 device_set_desc(dev,
120                                 "Iomega VPI0 Parallel to SCSI interface");
121         } else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
122                 vpo->vpo_isplus = 1;
123                 device_set_desc(dev,
124                                 "Iomega Matchmaker Parallel to SCSI interface");
125         } else {
126                 return (error);
127         }
128
129         return (0);
130 }
131
132 /*
133  * vpo_attach()
134  */
135 static int
136 vpo_attach(device_t dev)
137 {
138         struct vpo_data *vpo = DEVTOSOFTC(dev);
139         struct cam_devq *devq;
140         int error;
141
142         /* low level attachment */
143         if (vpo->vpo_isplus) {
144                 if ((error = imm_attach(&vpo->vpo_io)))
145                         return (error);
146         } else {
147                 if ((error = vpoio_attach(&vpo->vpo_io)))
148                         return (error);
149         }
150
151         /*
152         **      Now tell the generic SCSI layer
153         **      about our bus.
154         */
155         devq = cam_simq_alloc(/*maxopenings*/1);
156         /* XXX What about low-level detach on error? */
157         if (devq == NULL)
158                 return (ENXIO);
159
160         vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
161                                  device_get_unit(dev),
162                                  /*untagged*/1, /*tagged*/0, devq);
163         if (vpo->sim == NULL) {
164                 cam_simq_free(devq);
165                 return (ENXIO);
166         }
167
168         if (xpt_bus_register(vpo->sim, /*bus*/0) != CAM_SUCCESS) {
169                 cam_sim_free(vpo->sim, /*free_devq*/TRUE);
170                 return (ENXIO);
171         }
172
173         /* all went ok */
174
175         vpo_cam_rescan(vpo);    /* have CAM rescan the bus */
176
177         return (0);
178 }
179
180 static void
181 vpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
182 {
183         free(ccb, M_TEMP);
184 }
185
186 static void
187 vpo_cam_rescan(struct vpo_data *vpo)
188 {
189         struct cam_path *path;
190         union ccb *ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK);
191
192         bzero(ccb, sizeof(union ccb));
193
194         if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0)
195             != CAM_REQ_CMP) {
196                 /* A failure is benign as the user can do a manual rescan */
197                 return;
198         }
199
200         xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
201         ccb->ccb_h.func_code = XPT_SCAN_BUS;
202         ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback;
203         ccb->crcn.flags = CAM_FLAG_NONE;
204         xpt_action(ccb);
205
206         /* The scan is in progress now. */
207 }
208
209 /*
210  * vpo_intr()
211  */
212 static void
213 vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
214 {
215         int errno;      /* error in errno.h */
216         int s;
217 #ifdef VP0_DEBUG
218         int i;
219 #endif
220
221         s = splcam();
222
223         if (vpo->vpo_isplus) {
224                 errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
225                         csio->ccb_h.target_id,
226                         (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
227                         (char *)csio->data_ptr, csio->dxfer_len,
228                         &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
229         } else {
230                 errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
231                         csio->ccb_h.target_id,
232                         (char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
233                         (char *)csio->data_ptr, csio->dxfer_len,
234                         &vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
235         }
236
237 #ifdef VP0_DEBUG
238         printf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n", 
239                  errno, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
240
241         /* dump of command */
242         for (i=0; i<csio->cdb_len; i++)
243                 printf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
244
245         printf("\n");
246 #endif
247
248         if (errno) {
249                 /* connection to ppbus interrupted */
250                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
251                 goto error;
252         }
253
254         /* if a timeout occured, no sense */
255         if (vpo->vpo_error) {
256                 if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
257                         printf("vpo%d: VP0 error/timeout (%d)\n",
258                                 vpo->vpo_unit, vpo->vpo_error);
259
260                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
261                 goto error;
262         }
263
264         /* check scsi status */
265         if (vpo->vpo_stat != SCSI_STATUS_OK) {
266            csio->scsi_status = vpo->vpo_stat;
267
268            /* check if we have to sense the drive */
269            if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
270
271                 vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
272                 vpo->vpo_sense.cmd.length = csio->sense_len;
273                 vpo->vpo_sense.cmd.control = 0;
274
275                 if (vpo->vpo_isplus) {
276                         errno = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
277                                 csio->ccb_h.target_id,
278                                 (char *)&vpo->vpo_sense.cmd,
279                                 sizeof(vpo->vpo_sense.cmd),
280                                 (char *)&csio->sense_data, csio->sense_len,
281                                 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
282                                 &vpo->vpo_error);
283                 } else {
284                         errno = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
285                                 csio->ccb_h.target_id,
286                                 (char *)&vpo->vpo_sense.cmd,
287                                 sizeof(vpo->vpo_sense.cmd),
288                                 (char *)&csio->sense_data, csio->sense_len,
289                                 &vpo->vpo_sense.stat, &vpo->vpo_sense.count,
290                                 &vpo->vpo_error);
291                 }
292                         
293
294 #ifdef VP0_DEBUG
295                 printf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n", 
296                         errno, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
297 #endif
298
299                 /* check sense return status */
300                 if (errno == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
301                    /* sense ok */
302                    csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
303                    csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
304
305 #ifdef VP0_DEBUG
306                    /* dump of sense info */
307                    printf("(sense) ");
308                    for (i=0; i<vpo->vpo_sense.count; i++)
309                         printf("%x ", ((char *)&csio->sense_data)[i]);
310                    printf("\n");
311 #endif
312
313                 } else {
314                    /* sense failed */
315                    csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
316                 }
317            } else {
318                 /* no sense */
319                 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;                     
320            }
321
322            goto error;
323         }
324
325         csio->resid = csio->dxfer_len - vpo->vpo_count;
326         csio->ccb_h.status = CAM_REQ_CMP;
327
328 error:
329         splx(s);
330
331         return;
332 }
333
334 static void
335 vpo_action(struct cam_sim *sim, union ccb *ccb)
336 {
337
338         struct vpo_data *vpo = (struct vpo_data *)sim->softc;
339
340         switch (ccb->ccb_h.func_code) {
341         case XPT_SCSI_IO:
342         {
343                 struct ccb_scsiio *csio;
344
345                 csio = &ccb->csio;
346
347 #ifdef VP0_DEBUG
348                 printf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
349                         vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
350 #endif
351                 
352                 vpo_intr(vpo, csio);
353
354                 xpt_done(ccb);
355
356                 break;
357         }
358         case XPT_CALC_GEOMETRY:
359         {
360                 struct    ccb_calc_geometry *ccg;
361
362                 ccg = &ccb->ccg;
363
364 #ifdef VP0_DEBUG
365                 printf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%d,c=%d,h=%d,spt=%d) request\n",
366                         vpo->vpo_unit,
367                         ccg->block_size,
368                         ccg->volume_size,
369                         ccg->cylinders,
370                         ccg->heads,
371                         ccg->secs_per_track);
372 #endif
373
374                 ccg->heads = 64;
375                 ccg->secs_per_track = 32;
376                 ccg->cylinders = ccg->volume_size /
377                                  (ccg->heads * ccg->secs_per_track);
378
379                 ccb->ccb_h.status = CAM_REQ_CMP;
380                 xpt_done(ccb);
381                 break;
382         }
383         case XPT_RESET_BUS:             /* Reset the specified SCSI bus */
384         {
385
386 #ifdef VP0_DEBUG
387                 printf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
388 #endif
389
390                 if (vpo->vpo_isplus) {
391                         if (imm_reset_bus(&vpo->vpo_io)) {
392                                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
393                                 xpt_done(ccb);
394                                 return;
395                         }
396                 } else {
397                         if (vpoio_reset_bus(&vpo->vpo_io)) {
398                                 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
399                                 xpt_done(ccb);
400                                 return;
401                         }
402                 }
403
404                 ccb->ccb_h.status = CAM_REQ_CMP;
405                 xpt_done(ccb);
406                 break;
407         }
408         case XPT_PATH_INQ:              /* Path routing inquiry */
409         {
410                 struct ccb_pathinq *cpi = &ccb->cpi;
411                 
412 #ifdef VP0_DEBUG
413                 printf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
414 #endif
415                 cpi->version_num = 1; /* XXX??? */
416                 cpi->hba_inquiry = 0;
417                 cpi->target_sprt = 0;
418                 cpi->hba_misc = 0;
419                 cpi->hba_eng_cnt = 0;
420                 cpi->max_target = 7;
421                 cpi->max_lun = 0;
422                 cpi->initiator_id = VP0_INITIATOR;
423                 cpi->bus_id = sim->bus_id;
424                 cpi->base_transfer_speed = 93;
425                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
426                 strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
427                 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
428                 cpi->unit_number = sim->unit_number;
429
430                 cpi->ccb_h.status = CAM_REQ_CMP;
431                 xpt_done(ccb);
432                 break;
433         }
434         default:
435                 ccb->ccb_h.status = CAM_REQ_INVALID;
436                 xpt_done(ccb);
437                 break;
438         }
439
440         return;
441 }
442
443 static void
444 vpo_poll(struct cam_sim *sim)
445 {       
446         /* The ZIP is actually always polled throw vpo_action() */
447         return;
448 }
449
450 static devclass_t vpo_devclass;
451
452 static device_method_t vpo_methods[] = {
453         /* device interface */
454         DEVMETHOD(device_identify,      vpo_identify),
455         DEVMETHOD(device_probe,         vpo_probe),
456         DEVMETHOD(device_attach,        vpo_attach),
457
458         { 0, 0 }
459 };
460
461 static driver_t vpo_driver = {
462         "vpo",
463         vpo_methods,
464         sizeof(struct vpo_data),
465 };
466 DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, 0, 0);