remove __P() from this directory
[dragonfly.git] / sys / dev / disk / simos / simos.c
CommitLineData
984263bc
MD
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 $
38e94a25 27 * $DragonFly: src/sys/dev/disk/simos/Attic/simos.c,v 1.4 2003/08/27 10:35:17 rob Exp $
984263bc
MD
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
1f2de5d4
MD
36#include <bus/cam/cam.h>
37#include <bus/cam/cam_ccb.h>
38#include <bus/cam/cam_sim.h>
39#include <bus/cam/cam_xpt_sim.h>
40#include <bus/cam/cam_debug.h>
984263bc 41
1f2de5d4
MD
42#include <bus/cam/scsi/scsi_all.h>
43#include <bus/cam/scsi/scsi_message.h>
984263bc
MD
44
45#include <machine/clock.h>
46#include <vm/vm.h>
47#include <vm/vm_param.h>
48#include <vm/pmap.h>
49#include <sys/kernel.h>
50
1f2de5d4 51#include "simos.h"
984263bc 52
1f2de5d4
MD
53#include <bus/pci/pcireg.h>
54#include <bus/pci/pcivar.h>
984263bc
MD
55
56#include <machine/alpha_cpu.h>
57
58struct simos_softc {
59 int sc_unit;
60 SimOS_SCSI* sc_regs;
61
62 /*
63 * SimOS only supports one pending command.
64 */
65 struct cam_sim *sc_sim;
66 struct cam_path *sc_path;
67 struct ccb_scsiio *sc_pending;
68};
69
70struct simos_softc* simosp[10];
71
72static u_long simos_unit;
73
38e94a25
RG
74static const char *simos_probe (pcici_t tag, pcidi_t type);
75static void simos_attach (pcici_t config_d, int unit);
76static void simos_action (struct cam_sim *sim, union ccb *ccb);
77static void simos_poll (struct cam_sim *sim);
984263bc
MD
78
79struct pci_device simos_driver = {
80 "simos",
81 simos_probe,
82 simos_attach,
83 &simos_unit,
84 NULL
85};
86COMPAT_PCI_DRIVER (simos, simos_driver);
87
88static const char *
89simos_probe(pcici_t tag, pcidi_t type)
90{
91 switch (type) {
92 case 0x1291|(0x1291<<16):
93 return "SimOS SCSI";
94 default:
95 return NULL;
96 }
97
98}
99
100static void
101simos_attach(pcici_t config_id, int unit)
102{
103 struct simos_softc* sc;
104 struct cam_devq *devq;
105
106 sc = malloc(sizeof(struct simos_softc), M_DEVBUF, M_WAITOK);
107 simosp[unit] = sc;
108 bzero(sc, sizeof *sc);
109
110 sc->sc_unit = unit;
111 sc->sc_regs = (SimOS_SCSI*) SIMOS_SCSI_ADDR;
112 sc->sc_pending = 0;
113
114 devq = cam_simq_alloc(/*maxopenings*/1);
115 if (devq == NULL)
116 return;
117
118 sc->sc_sim = cam_sim_alloc(simos_action, simos_poll, "simos", sc, unit,
119 /*untagged*/1, /*tagged*/0, devq);
120 if (sc->sc_sim == NULL) {
121 cam_simq_free(devq);
122 return;
123 }
124
125 if (xpt_bus_register(sc->sc_sim, /*bus*/0) != CAM_SUCCESS) {
126 cam_sim_free(sc->sc_sim, /*free_devq*/TRUE);
127 return;
128 }
129
130 if (xpt_create_path(&sc->sc_path, /*periph*/NULL,
131 cam_sim_path(sc->sc_sim), CAM_TARGET_WILDCARD,
132 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
133 xpt_bus_deregister(cam_sim_path(sc->sc_sim));
134 cam_sim_free(sc->sc_sim, /*free_devq*/TRUE);
135 return;
136 }
137
138 alpha_register_pci_scsi(config_id->bus, config_id->slot, sc->sc_sim);
139
140 return;
141}
142
143static void
144simos_start(struct simos_softc* sc, struct ccb_scsiio *csio)
145{
146 struct scsi_generic *cmd;
147 int cmdlen;
148 caddr_t data;
149 int datalen;
150 int s;
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 printf("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 s = splcam();
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 splx(s);
225}
226
227static void
228simos_done(struct simos_softc* sc)
229{
230 struct ccb_scsiio* csio = sc->sc_pending;
231 int s, 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 s = splcam();
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 splx(s);
262}
263
264static void
265simos_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
334static void
335simos_poll(struct cam_sim *sim)
336{
337 simos_done(cam_sim_softc(sim));
338}
339
340void
341simos_intr(int unit)
342{
343 /* XXX bogus */
344 struct simos_softc* sc = simosp[unit];
345
346 simos_done(sc);
347}