1 /* $NecBSD: bsif.c,v 1.6 1997/10/31 17:43:40 honda Exp $ */
3 * Copyright (c) HONDA Naofumi, KATO Takenori, 1996. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer as
11 * the first lines of this file unmodified.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/sys/i386/isa/bs/bsif.c,v 1.10.2.1 2000/08/24 08:06:08 kato Exp $
30 * $DragonFly: src/sys/dev/disk/i386/bs/Attic/bsif.c,v 1.8 2004/09/18 18:47:20 dillon Exp $
34 /* WARNING: Any bug report must contain BS_REL_VERSION */
35 #define BS_REL_VERSION "NetBSD1.2/030" /* major jump */
39 #include <i386/Cbus/dev/bs/bsif.h>
40 #endif /* __NetBSD__ */
41 #if defined(__DragonFly__) || defined(__FreeBSD__)
46 #endif /* __FreeBSD__ */
48 #include <bus/cam/cam.h>
49 #include <bus/cam/cam_ccb.h>
50 #include <bus/cam/cam_sim.h>
51 #include <bus/cam/cam_xpt_sim.h>
52 #include <bus/cam/cam_debug.h>
54 #include <bus/cam/scsi/scsi_all.h>
55 #include <bus/cam/scsi/scsi_message.h>
57 /**************************************************
59 **************************************************/
61 static void bs_scsi_minphys (struct buf *);
63 struct cfdriver bs_cd = {
67 struct scsi_device bs_dev = {
68 NULL, /* Use default error handler */
69 NULL, /* have a queue, served by this */
70 NULL, /* have no async handler */
71 NULL, /* Use default 'done' routine */
74 struct scsi_adapter pc98texa55bs = {
80 #endif /* __NetBSD__ */
82 #if defined(__DragonFly__) || defined(__FreeBSD__)
83 static int bsprobe (struct isa_device *);
84 static void bs_poll(struct cam_sim *sim);
85 static int bsattach(struct isa_device *);
86 static ointhand2_t bsintr;
87 static int bs_dmarangecheck (caddr_t, unsigned);
89 struct isa_driver bsdriver = {
95 struct scsi_device bs_dev = {
96 NULL, /* Use default error handler */
97 NULL, /* have a queue, served by this */
98 NULL, /* have no async handler */
99 NULL, /* Use default 'done' routine */
105 bs_adapter_info(unit)
111 static struct scsi_adapter pc98texa55bs = {
120 static u_short pc98_irq_ball[16] = {
121 IRQ0, IRQ1, IRQ2, IRQ3, IRQ4, IRQ5, IRQ6, IRQ7,
122 IRQ8, IRQ9, IRQ10, IRQ11, IRQ12, IRQ13, IRQ14, IRQ15
125 static struct bs_softc *bscdata[NBS];
126 #endif /* __FreeBSD__ */
128 /*****************************************************************
129 * OS <=> BS INTERFACE
130 *****************************************************************/
131 #if defined(__DragonFly__) || defined(__FreeBSD__)
134 struct isa_device *dev;
136 struct bs_softc *bsc;
137 int unit = dev->id_unit;
142 printf("bs%d: unit number too high\n", unit);
146 * Allocate a storage for us
149 printf("bs%d: memory already allocated\n", unit);
152 bsc = malloc(sizeof(struct bs_softc), M_TEMP, M_WAITOK | M_ZERO);
153 callout_init(&bsc->timeout_ch);
157 bsc->sc_cfgflags = DVCFG_MINOR(dev->id_flags);
158 bsc->sc_hw = DVCFG_HW(&bshw_hwsel, DVCFG_MAJOR(dev->id_flags));
159 if (bsc->sc_hw == NULL)
162 if ((bsc->sc_hw->hw_flags & BSHW_SMFIFO) &&
163 (dev->id_maddr != (caddr_t)MADDRUNK))
164 bsc->sm_offset = (u_long) dev->id_maddr;
166 bsc->sm_offset = (u_long) 0;
168 snprintf(bsc->sc_dvname, sizeof(bsc->sc_dvname), "bs%d", unit);
170 if (dev->id_iobase == 0)
172 printf("%s: iobase not specified. Assume default port(0x%x)\n",
173 bsc->sc_dvname, BSHW_DEFAULT_PORT);
174 dev->id_iobase = BSHW_DEFAULT_PORT;
177 bsc->sc_iobase = dev->id_iobase;
180 if (bshw_board_probe(bsc, &drq, &irq))
183 dev->id_irq = pc98_irq_ball[irq];
184 dev->id_drq = (short)drq;
186 /* initialize host queue and target info */
187 bs_hostque_init(bsc);
188 for (i = 0; i < NTARGETS; i++)
189 if (i != bsc->sc_hostid)
190 bs_init_target_info(bsc, i);
192 /* initialize ccb queue */
193 bs_init_ccbque(BS_MAX_CCB);
195 /* scsi bus reset and restart */
196 bsc->sc_hstate = BSC_BOOTUP;
197 bsc->sc_retry = RETRIES;
198 bsc->sc_wc = delaycount * 250; /* about 1 sec */
205 #endif /* __FreeBSD__ */
215 printf("%s: scsibus ", name);
220 #if defined(__DragonFly__) || defined(__FreeBSD__)
222 bs_poll(struct cam_sim *sim)
224 bs_sequencer(cam_sim_softc(sim));
229 struct isa_device *dev;
231 int unit = dev->id_unit;
232 struct bs_softc *bsc = bscdata[unit];
233 struct cam_devq *devq;
235 dev->id_ointr = bsintr;
238 * CAM support HN2 MAX_START, MAX_TAGS xxxx
240 devq = cam_simq_alloc(256/*MAX_START*/);
244 bsc->sim = cam_sim_alloc(bs_scsi_cmd, bs_poll, "bs",
245 bsc, unit, 1, 32/*MAX_TAGS*/, devq);
246 cam_simq_release(devq);
247 if (bsc->sim == NULL)
250 if (xpt_bus_register(bsc->sim, 0) != CAM_SUCCESS) {
251 free(bsc->sim, M_DEVBUF);
255 if (xpt_create_path(&bsc->path, /*periph*/NULL,
256 cam_sim_path(bsc->sim), CAM_TARGET_WILDCARD,
257 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
258 xpt_bus_deregister(cam_sim_path(bsc->sim));
259 cam_sim_free(bsc->sim);
260 free(bsc->sim, M_DEVBUF);
263 bs_start_timeout(bsc);
266 #endif /* __FreeBSD__ */
274 return bs_sequencer((struct bs_softc *)arg);
276 #endif /* __NetBSD__ */
278 #if defined(__DragonFly__) || defined(__FreeBSD__)
283 (void)bs_sequencer(bscdata[unit]);
285 #endif /* __FreeBSD__ */
287 /*****************************************************************
288 * JULIAN SCSI <=> BS INTERFACE
289 *****************************************************************/
290 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
296 if (bp->b_bcount > BSDMABUFSIZ)
297 bp->b_bcount = BSDMABUFSIZ;
303 bs_target_open(sc, cf)
304 struct scsi_link *sc;
307 u_int target = sc->target;
308 struct bs_softc *bsc = (struct bs_softc *) (sc->adapter_softc);
309 struct targ_info *ti = bsc->sc_ti[target];
312 if ((bsc->sc_openf & (1 << target)) == 0)
315 if ((flags = cf->cf_flags) == 0)
316 flags = BS_SCSI_DEFCFG;
318 bs_setup_ctrl(ti, (u_int)sc->quirks, flags);
322 /*****************************************************************
323 * BS MEMORY ALLOCATION INTERFACE
324 *****************************************************************/
328 struct targ_info *ti;
330 struct bs_softc *bsc = ti->ti_bsc;
331 caddr_t addr, physaddr;
332 bus_dma_segment_t seg;
339 * A) total memory >= 16M at boot: MAXBSIZE * 7 = 112k.
340 * B) others: 4K * 7 = 28 K.
342 if (get_sysinfo(SYSINFO_MEMLEVEL) == MEM_LEVEL1 && cold != 0)
346 ti->bounce_size = NBPG * pages;
349 error = bus_dmamem_alloc(bsc->sc_dmat, ti->bounce_size, NBPG, 0,
350 &seg, 1, &rseg, BUS_DMA_NOWAIT);
351 if (rseg == 1 && error == 0)
352 error = bus_dmamem_map(bsc->sc_dmat, &seg, rseg,
353 ti->bounce_size, &addr, BUS_DMA_NOWAIT);
354 if (rseg != 1 || error != 0)
356 ti->bounce_size = NBPG;
357 if ((addr = malloc(NBPG, M_DEVBUF, M_INTWAIT)) == NULL)
361 physaddr = (caddr_t) vtophys(addr);
362 if ((u_int) physaddr >= RAM_END)
364 /* XXX: mem from malloc only! */
365 free(addr, M_DEVBUF);
369 ti->bounce_addr = (u_int8_t *) addr;
370 ti->bounce_phys = (u_int8_t *) physaddr;
374 bs_printf(ti, "bs_alloc_buf", "no phys bounce buffer");
375 printf("WARNING: this target is dislocated\n");
377 #endif /* __NetBSD__ */
379 #if defined(__DragonFly__) || defined(__FreeBSD__)
380 static int bs_dmarangecheck(caddr_t va, unsigned length)
382 vm_offset_t phys, priorpage = 0, endva;
384 endva = (vm_offset_t)round_page((unsigned long)(va+length));
385 for (; va < (caddr_t)endva; va += PAGE_SIZE) {
386 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
388 panic("bs_dmarangecheck: no physical page present");
392 if (priorpage + PAGE_SIZE != phys)
402 struct targ_info *ti;
404 caddr_t addr, physaddr;
406 #if BS_BOUNCE_SIZE != 0
407 ti->bounce_size = BS_BOUNCE_SIZE;
409 ti->bounce_size = BSHW_NBPG;
411 /* Try malloc() first. It works better if it works. */
412 addr = malloc(ti->bounce_size, M_DEVBUF, M_INTWAIT);
414 if (bs_dmarangecheck(addr, ti->bounce_size) == 0) {
415 physaddr = (caddr_t) vtophys(addr);
416 ti->bounce_addr = (u_int8_t *) addr;
417 ti->bounce_phys = (u_int8_t *) physaddr;
420 free(addr, M_DEVBUF);
422 addr = contigmalloc(ti->bounce_size, M_DEVBUF, M_NOWAIT,
423 0ul, RAM_END, 1ul, 0x10000ul);
427 physaddr = (caddr_t) vtophys(addr);
428 if ((u_int) physaddr >= RAM_END)
436 ti->bounce_addr = (u_int8_t *) addr;
437 ti->bounce_phys = (u_int8_t *) physaddr;
441 bs_printf(ti, "bs_alloc_buf", "no phys bounce buffer");
442 printf("WARNING: this target is dislocated\n");
444 #endif /* __FreeBSD__ */