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