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 $
33 /* WARNING: Any bug report must contain BS_REL_VERSION */
34 #define BS_REL_VERSION "NetBSD1.2/030" /* major jump */
38 #include <i386/Cbus/dev/bs/bsif.h>
39 #endif /* __NetBSD__ */
44 #include <i386/isa/bs/bsif.h>
45 #endif /* __FreeBSD__ */
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_sim.h>
50 #include <cam/cam_xpt_sim.h>
51 #include <cam/cam_debug.h>
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_message.h>
56 /**************************************************
58 **************************************************/
60 static void bs_scsi_minphys __P((struct buf *));
62 struct cfdriver bs_cd = {
66 struct scsi_device bs_dev = {
67 NULL, /* Use default error handler */
68 NULL, /* have a queue, served by this */
69 NULL, /* have no async handler */
70 NULL, /* Use default 'done' routine */
73 struct scsi_adapter pc98texa55bs = {
79 #endif /* __NetBSD__ */
82 static int bsprobe __P((struct isa_device *));
83 static void bs_poll(struct cam_sim *sim);
84 static int bsattach(struct isa_device *);
85 static ointhand2_t bsintr;
86 static int bs_dmarangecheck __P((caddr_t, unsigned));
88 struct isa_driver bsdriver = {
94 struct scsi_device bs_dev = {
95 NULL, /* Use default error handler */
96 NULL, /* have a queue, served by this */
97 NULL, /* have no async handler */
98 NULL, /* Use default 'done' routine */
104 bs_adapter_info(unit)
110 static struct scsi_adapter pc98texa55bs = {
119 static u_short pc98_irq_ball[16] = {
120 IRQ0, IRQ1, IRQ2, IRQ3, IRQ4, IRQ5, IRQ6, IRQ7,
121 IRQ8, IRQ9, IRQ10, IRQ11, IRQ12, IRQ13, IRQ14, IRQ15
124 static struct bs_softc *bscdata[NBS];
125 #endif /* __FreeBSD__ */
127 /*****************************************************************
128 * OS <=> BS INTERFACE
129 *****************************************************************/
133 struct isa_device *dev;
135 struct bs_softc *bsc;
136 int unit = dev->id_unit;
141 printf("bs%d: unit number too high\n", unit);
145 * Allocate a storage for us
148 printf("bs%d: memory already allocated\n", unit);
151 if (!(bsc = malloc(sizeof(struct bs_softc), M_TEMP, M_NOWAIT))) {
152 printf("bs%d cannot malloc!\n", unit);
155 bzero(bsc, sizeof(struct bs_softc));
156 callout_handle_init(&bsc->timeout_ch);
160 bsc->sc_cfgflags = DVCFG_MINOR(dev->id_flags);
161 bsc->sc_hw = DVCFG_HW(&bshw_hwsel, DVCFG_MAJOR(dev->id_flags));
162 if (bsc->sc_hw == NULL)
165 if ((bsc->sc_hw->hw_flags & BSHW_SMFIFO) &&
166 (dev->id_maddr != (caddr_t)MADDRUNK))
167 bsc->sm_offset = (u_long) dev->id_maddr;
169 bsc->sm_offset = (u_long) 0;
171 snprintf(bsc->sc_dvname, sizeof(bsc->sc_dvname), "bs%d", unit);
173 if (dev->id_iobase == 0)
175 printf("%s: iobase not specified. Assume default port(0x%x)\n",
176 bsc->sc_dvname, BSHW_DEFAULT_PORT);
177 dev->id_iobase = BSHW_DEFAULT_PORT;
180 bsc->sc_iobase = dev->id_iobase;
183 if (bshw_board_probe(bsc, &drq, &irq))
186 dev->id_irq = pc98_irq_ball[irq];
187 dev->id_drq = (short)drq;
189 /* initialize host queue and target info */
190 bs_hostque_init(bsc);
191 for (i = 0; i < NTARGETS; i++)
192 if (i != bsc->sc_hostid)
193 bs_init_target_info(bsc, i);
195 /* initialize ccb queue */
196 bs_init_ccbque(BS_MAX_CCB);
198 /* scsi bus reset and restart */
199 bsc->sc_hstate = BSC_BOOTUP;
200 bsc->sc_retry = RETRIES;
201 bsc->sc_wc = delaycount * 250; /* about 1 sec */
208 #endif /* __FreeBSD__ */
218 printf("%s: scsibus ", name);
225 bs_poll(struct cam_sim *sim)
227 bs_sequencer(cam_sim_softc(sim));
232 struct isa_device *dev;
234 int unit = dev->id_unit;
235 struct bs_softc *bsc = bscdata[unit];
236 struct cam_devq *devq;
238 dev->id_ointr = bsintr;
241 * CAM support HN2 MAX_START, MAX_TAGS xxxx
243 devq = cam_simq_alloc(256/*MAX_START*/);
247 bsc->sim = cam_sim_alloc(bs_scsi_cmd, bs_poll, "bs",
248 bsc, unit, 1, 32/*MAX_TAGS*/, devq);
249 if (bsc->sim == NULL) {
254 if (xpt_bus_register(bsc->sim, 0) != CAM_SUCCESS) {
255 free(bsc->sim, M_DEVBUF);
259 if (xpt_create_path(&bsc->path, /*periph*/NULL,
260 cam_sim_path(bsc->sim), CAM_TARGET_WILDCARD,
261 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
262 xpt_bus_deregister(cam_sim_path(bsc->sim));
263 cam_sim_free(bsc->sim, /*free_simq*/TRUE);
264 free(bsc->sim, M_DEVBUF);
267 bs_start_timeout(bsc);
270 #endif /* __FreeBSD__ */
278 return bs_sequencer((struct bs_softc *)arg);
280 #endif /* __NetBSD__ */
287 (void)bs_sequencer(bscdata[unit]);
289 #endif /* __FreeBSD__ */
291 /*****************************************************************
292 * JULIAN SCSI <=> BS INTERFACE
293 *****************************************************************/
300 if (bp->b_bcount > BSDMABUFSIZ)
301 bp->b_bcount = BSDMABUFSIZ;
307 bs_target_open(sc, cf)
308 struct scsi_link *sc;
311 u_int target = sc->target;
312 struct bs_softc *bsc = (struct bs_softc *) (sc->adapter_softc);
313 struct targ_info *ti = bsc->sc_ti[target];
316 if ((bsc->sc_openf & (1 << target)) == 0)
319 if ((flags = cf->cf_flags) == 0)
320 flags = BS_SCSI_DEFCFG;
322 bs_setup_ctrl(ti, (u_int)sc->quirks, flags);
326 /*****************************************************************
327 * BS MEMORY ALLOCATION INTERFACE
328 *****************************************************************/
332 struct targ_info *ti;
334 struct bs_softc *bsc = ti->ti_bsc;
335 caddr_t addr, physaddr;
336 bus_dma_segment_t seg;
343 * A) total memory >= 16M at boot: MAXBSIZE * 7 = 112k.
344 * B) others: 4K * 7 = 28 K.
346 if (get_sysinfo(SYSINFO_MEMLEVEL) == MEM_LEVEL1 && cold != 0)
350 ti->bounce_size = NBPG * pages;
353 error = bus_dmamem_alloc(bsc->sc_dmat, ti->bounce_size, NBPG, 0,
354 &seg, 1, &rseg, BUS_DMA_NOWAIT);
355 if (rseg == 1 && error == 0)
356 error = bus_dmamem_map(bsc->sc_dmat, &seg, rseg,
357 ti->bounce_size, &addr, BUS_DMA_NOWAIT);
358 if (rseg != 1 || error != 0)
360 ti->bounce_size = NBPG;
361 if ((addr = malloc(NBPG, M_DEVBUF, M_NOWAIT)) == NULL)
365 physaddr = (caddr_t) vtophys(addr);
366 if ((u_int) physaddr >= RAM_END)
368 /* XXX: mem from malloc only! */
369 free(addr, M_DEVBUF);
373 ti->bounce_addr = (u_int8_t *) addr;
374 ti->bounce_phys = (u_int8_t *) physaddr;
378 bs_printf(ti, "bs_alloc_buf", "no phys bounce buffer");
379 printf("WARNING: this target is dislocated\n");
381 #endif /* __NetBSD__ */
384 static int bs_dmarangecheck(caddr_t va, unsigned length)
386 vm_offset_t phys, priorpage = 0, endva;
388 endva = (vm_offset_t)round_page((unsigned long)(va+length));
389 for (; va < (caddr_t)endva; va += PAGE_SIZE) {
390 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
392 panic("bs_dmarangecheck: no physical page present");
396 if (priorpage + PAGE_SIZE != phys)
406 struct targ_info *ti;
408 caddr_t addr, physaddr;
410 #if BS_BOUNCE_SIZE != 0
411 ti->bounce_size = BS_BOUNCE_SIZE;
413 ti->bounce_size = BSHW_NBPG;
415 /* Try malloc() first. It works better if it works. */
416 addr = malloc(ti->bounce_size, M_DEVBUF, M_NOWAIT);
418 if (bs_dmarangecheck(addr, ti->bounce_size) == 0) {
419 physaddr = (caddr_t) vtophys(addr);
420 ti->bounce_addr = (u_int8_t *) addr;
421 ti->bounce_phys = (u_int8_t *) physaddr;
424 free(addr, M_DEVBUF);
426 addr = contigmalloc(ti->bounce_size, M_DEVBUF, M_NOWAIT,
427 0ul, RAM_END, 1ul, 0x10000ul);
431 physaddr = (caddr_t) vtophys(addr);
432 if ((u_int) physaddr >= RAM_END)
440 ti->bounce_addr = (u_int8_t *) addr;
441 ti->bounce_phys = (u_int8_t *) physaddr;
445 bs_printf(ti, "bs_alloc_buf", "no phys bounce buffer");
446 printf("WARNING: this target is dislocated\n");
448 #endif /* __FreeBSD__ */