Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / disk / simos / simos.c
1 /*-
2  * Copyright (c) 1998 Doug Rabson
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/pci/simos.c,v 1.7 1999/08/28 00:51:06 peter Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/buf.h>
33 #include <sys/proc.h>
34
35 #include <cam/cam.h>
36 #include <cam/cam_ccb.h>
37 #include <cam/cam_sim.h>
38 #include <cam/cam_xpt_sim.h>
39 #include <cam/cam_debug.h>
40
41 #include <cam/scsi/scsi_all.h>
42 #include <cam/scsi/scsi_message.h>
43
44 #include <machine/clock.h>
45 #include <vm/vm.h>
46 #include <vm/vm_param.h>
47 #include <vm/pmap.h>
48 #include <sys/kernel.h>
49
50 #include <pci/simos.h>
51
52 #include <pci/pcireg.h>
53 #include <pci/pcivar.h>
54
55 #include <machine/alpha_cpu.h>
56
57 struct simos_softc {
58         int                     sc_unit;
59         SimOS_SCSI*             sc_regs;
60
61         /*
62          * SimOS only supports one pending command.
63          */
64         struct cam_sim         *sc_sim;
65         struct cam_path        *sc_path;
66         struct ccb_scsiio      *sc_pending;
67 };
68
69 struct simos_softc* simosp[10];
70
71 static u_long simos_unit;
72
73 static const char *simos_probe __P((pcici_t tag, pcidi_t type));
74 static void simos_attach __P((pcici_t config_d, int unit));
75 static void simos_action __P((struct cam_sim *sim, union ccb *ccb));
76 static void simos_poll __P((struct cam_sim *sim));
77
78 struct pci_device simos_driver = {
79         "simos",
80         simos_probe,
81         simos_attach,
82         &simos_unit,
83         NULL
84 };
85 COMPAT_PCI_DRIVER (simos, simos_driver);
86
87 static const char *
88 simos_probe(pcici_t tag, pcidi_t type)
89 {       
90         switch (type) {
91         case 0x1291|(0x1291<<16):
92                 return "SimOS SCSI";
93         default:
94                 return NULL;
95         }
96                 
97 }
98
99 static void    
100 simos_attach(pcici_t config_id, int unit)
101 {
102         struct simos_softc* sc;
103         struct cam_devq *devq;
104
105         sc = malloc(sizeof(struct simos_softc), M_DEVBUF, M_WAITOK);
106         simosp[unit] = sc;
107         bzero(sc, sizeof *sc);
108
109         sc->sc_unit = unit;
110         sc->sc_regs = (SimOS_SCSI*) SIMOS_SCSI_ADDR;
111         sc->sc_pending = 0;
112
113         devq = cam_simq_alloc(/*maxopenings*/1);
114         if (devq == NULL)
115                 return;
116
117         sc->sc_sim = cam_sim_alloc(simos_action, simos_poll, "simos", sc, unit,
118                                    /*untagged*/1, /*tagged*/0, devq);
119         if (sc->sc_sim == NULL) {
120                 cam_simq_free(devq);
121                 return;
122         }
123
124         if (xpt_bus_register(sc->sc_sim, /*bus*/0) != CAM_SUCCESS) {
125                 cam_sim_free(sc->sc_sim, /*free_devq*/TRUE);
126                 return;
127         }
128
129         if (xpt_create_path(&sc->sc_path, /*periph*/NULL,
130                             cam_sim_path(sc->sc_sim), CAM_TARGET_WILDCARD,
131                             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
132                 xpt_bus_deregister(cam_sim_path(sc->sc_sim));
133                 cam_sim_free(sc->sc_sim, /*free_devq*/TRUE);
134                 return;
135         }
136
137         alpha_register_pci_scsi(config_id->bus, config_id->slot, sc->sc_sim);
138
139         return;
140 }
141
142 static void
143 simos_start(struct simos_softc* sc, struct ccb_scsiio *csio)
144 {
145         struct scsi_generic *cmd;
146         int cmdlen;
147         caddr_t data;
148         int datalen;
149         int s;
150         u_int8_t* p;
151         int i, count, target;
152         vm_offset_t va;
153         vm_size_t size;
154
155         cmd = (struct scsi_generic *) &csio->cdb_io.cdb_bytes;
156         cmdlen = csio->cdb_len;
157         data = csio->data_ptr;
158         datalen = csio->dxfer_len;
159
160         /*
161          * Simos doesn't understand some commands
162          */
163         if (cmd->opcode == START_STOP || cmd->opcode == PREVENT_ALLOW
164             || cmd->opcode == SYNCHRONIZE_CACHE) {
165                 csio->ccb_h.status = CAM_REQ_CMP;
166                 xpt_done((union ccb *) csio);
167                 return;
168         }
169
170         if (sc->sc_pending) {
171                 /*
172                  * Don't think this can happen.
173                  */
174                 printf("simos_start: can't start command while one is pending\n");
175                 csio->ccb_h.status = CAM_BUSY;
176                 xpt_done((union ccb *) csio);
177                 return;
178         }
179
180         s = splcam();
181         
182         csio->ccb_h.status |= CAM_SIM_QUEUED;
183         sc->sc_pending = csio;
184
185         target = csio->ccb_h.target_id;
186
187         /*
188          * Copy the command into SimOS' buffer
189          */
190         p = (u_int8_t*) cmd;
191         count = cmdlen;
192         for (i = 0; i < count; i++)
193                 sc->sc_regs->cmd[i] = *p++;
194         sc->sc_regs->length = count;
195         sc->sc_regs->target = target;
196         sc->sc_regs->lun = csio->ccb_h.target_lun;
197
198         /*
199          * Setup the segment descriptors.
200          */
201         va = (vm_offset_t) data;
202         size = datalen;
203         i = 0;
204         while (size > 0) {
205                 vm_size_t len = PAGE_SIZE - (va & PAGE_MASK);
206                 if (len > size)
207                         len = size;
208                 sc->sc_regs->sgMap[i].pAddr = vtophys(va);
209                 sc->sc_regs->sgMap[i].len = len;
210                 size -= len;
211                 va += len;
212                 i++;
213         }
214         sc->sc_regs->sgLen = i;
215
216         /*
217          * Start the i/o.
218          */
219         alpha_wmb();
220         sc->sc_regs->startIO = 1;
221         alpha_wmb();
222
223         splx(s);
224 }
225
226 static void
227 simos_done(struct simos_softc* sc)
228 {
229         struct ccb_scsiio* csio = sc->sc_pending;
230         int s, done;
231
232         /*
233          * Spurious interrupt caused by my bogus interrupt broadcasting.
234          */
235         if (!csio)
236                 return;
237
238         sc->sc_pending = 0;
239
240         done = sc->sc_regs->done[csio->ccb_h.target_id];
241         if (!done)
242                 return;
243
244         s = splcam();
245
246         if (done >> 16)
247                 /* Error detected */
248                 csio->ccb_h.status = CAM_CMD_TIMEOUT;
249         else
250                 csio->ccb_h.status = CAM_REQ_CMP;
251
252         /*
253          * Ack the interrupt to clear it.
254          */
255         sc->sc_regs->done[csio->ccb_h.target_id] = 1;
256         alpha_wmb();
257         
258         xpt_done((union ccb *) csio);
259
260         splx(s);
261 }
262
263 static void
264 simos_action(struct cam_sim *sim, union ccb *ccb)
265 {
266         struct simos_softc* sc = (struct simos_softc *)sim->softc;
267
268         switch (ccb->ccb_h.func_code) {
269         case XPT_SCSI_IO:
270         {
271                 struct ccb_scsiio *csio;
272
273                 csio = &ccb->csio;
274                 simos_start(sc, csio);
275                 break;
276         }
277
278         case XPT_CALC_GEOMETRY:
279         {
280                 struct    ccb_calc_geometry *ccg;
281                 u_int32_t size_mb;
282                 u_int32_t secs_per_cylinder;
283
284                 ccg = &ccb->ccg;
285                 size_mb = ccg->volume_size
286                         / ((1024L * 1024L) / ccg->block_size);
287
288                 ccg->heads = 64;
289                 ccg->secs_per_track = 32;
290
291                 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
292                 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
293
294                 ccb->ccb_h.status = CAM_REQ_CMP;
295                 xpt_done(ccb);
296                 break;
297         }
298
299         case XPT_RESET_BUS:
300         {
301                 ccb->ccb_h.status = CAM_REQ_CMP;
302                 xpt_done(ccb);
303                 break;
304         }
305
306         case XPT_PATH_INQ:
307         {
308                 struct ccb_pathinq *cpi = &ccb->cpi;
309                 
310                 cpi->version_num = 1; /* XXX??? */
311                 cpi->max_target = 2;
312                 cpi->max_lun = 0;
313                 cpi->initiator_id = 7;
314                 cpi->bus_id = sim->bus_id;
315                 cpi->base_transfer_speed = 3300;
316                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
317                 strncpy(cpi->hba_vid, "SimOS", HBA_IDLEN);
318                 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
319                 cpi->unit_number = sim->unit_number;
320
321                 cpi->ccb_h.status = CAM_REQ_CMP;
322                 xpt_done(ccb);
323                 break;
324         }
325
326         default:
327                 ccb->ccb_h.status = CAM_REQ_INVALID;
328                 xpt_done(ccb);
329                 break;
330         }
331 }
332
333 static void
334 simos_poll(struct cam_sim *sim)
335 {       
336         simos_done(cam_sim_softc(sim));
337 }
338
339 void
340 simos_intr(int unit)
341 {
342         /* XXX bogus */
343         struct simos_softc* sc = simosp[unit];
344
345         simos_done(sc);
346 }