Initial import from FreeBSD RELENG_4:
[games.git] / sys / dev / disk / mpt / mpt_freebsd.h
1 /* $FreeBSD: src/sys/dev/mpt/mpt_freebsd.h,v 1.3.2.3 2002/09/24 21:37:25 mjacob Exp $ */
2 /*
3  * LSI MPT Host Adapter FreeBSD Wrapper Definitions (CAM version)
4  *
5  * Copyright (c) 2000, 2001 by Greg Ansley, Adam Prewett
6  *
7  * Partially derived from Matty Jacobs ISP driver.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice immediately at the beginning of the file, without modification,
14  *    this list of conditions, and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  */
28 /*
29  * Additional Copyright (c) 2002 by Matthew Jacob under same license.
30  */
31
32 #ifndef  _MPT_FREEBSD_H_
33 #define  _MPT_FREEBSD_H_
34
35 #define RELENG_4        1
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #ifdef  RELENG_4
40 #include <sys/kernel.h>
41 #include <sys/queue.h>
42 #include <sys/malloc.h>
43 #else
44 #include <sys/endian.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #endif
50 #include <sys/proc.h>
51 #include <sys/bus.h>
52
53 #include <machine/bus_memio.h>
54 #include <machine/bus_pio.h>
55 #include <machine/bus.h>
56 #include <machine/clock.h>
57 #include <machine/cpu.h>
58
59 #include <cam/cam.h>
60 #include <cam/cam_debug.h>
61 #include <cam/cam_ccb.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt.h>
64 #include <cam/cam_xpt_sim.h>
65 #include <cam/cam_debug.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.h>
68
69 #include "opt_ddb.h"
70
71 #include "dev/mpt/mpilib/mpi_type.h"
72 #include "dev/mpt/mpilib/mpi.h"
73 #include "dev/mpt/mpilib/mpi_cnfg.h"
74 #include "dev/mpt/mpilib/mpi_fc.h"
75 #include "dev/mpt/mpilib/mpi_init.h"
76 #include "dev/mpt/mpilib/mpi_ioc.h"
77 #include "dev/mpt/mpilib/mpi_lan.h"
78 #include "dev/mpt/mpilib/mpi_targ.h"
79
80
81 #define INLINE __inline
82
83 #ifdef  RELENG_4
84 #define MPT_IFLAGS              INTR_TYPE_CAM
85 #define MPT_LOCK(mpt)           mpt_lockspl(mpt)
86 #define MPT_UNLOCK(mpt)         mpt_unlockspl(mpt)
87 #define MPTLOCK_2_CAMLOCK       MPT_UNLOCK
88 #define CAMLOCK_2_MPTLOCK       MPT_LOCK
89 #define MPT_LOCK_SETUP(mpt)
90 #define MPT_LOCK_DESTROY(mpt)
91 #else
92 #if     LOCKING_WORKED_AS_IT_SHOULD
93 #define MPT_IFLAGS              INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
94 #define MPT_LOCK_SETUP(mpt)                                             \
95                 mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);         \
96                 mpt->mpt_locksetup = 1
97 #define MPT_LOCK_DESTROY(mpt)                                           \
98         if (mpt->mpt_locksetup) {                                       \
99                 mtx_destroy(&mpt->mpt_lock);                            \
100                 mpt->mpt_locksetup = 0;                                 \
101         }
102
103 #define MPT_LOCK(mpt)           mtx_lock(&(mpt)->mpt_lock)
104 #define MPT_UNLOCK(mpt)         mtx_unlock(&(mpt)->mpt_lock)
105 #define MPTLOCK_2_CAMLOCK(mpt)  \
106         mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
107 #define CAMLOCK_2_MPTLOCK(mpt)  \
108         mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
109 #else
110 #define MPT_IFLAGS              INTR_TYPE_CAM | INTR_ENTROPY
111 #define MPT_LOCK_SETUP(mpt)     do { } while (0)
112 #define MPT_LOCK_DESTROY(mpt)   do { } while (0)
113 #define MPT_LOCK(mpt)           do { } while (0)
114 #define MPT_UNLOCK(mpt)         do { } while (0)
115 #define MPTLOCK_2_CAMLOCK(mpt)  do { } while (0)
116 #define CAMLOCK_2_MPTLOCK(mpt)  do { } while (0)
117 #endif
118 #endif
119         
120
121
122 /* Max MPT Reply we are willing to accept (must be power of 2) */
123 #define MPT_REPLY_SIZE          128
124
125 #define MPT_MAX_REQUESTS(mpt)   ((mpt)->is_fc? 1024 : 256)
126 #define MPT_REQUEST_AREA 512
127 #define MPT_SENSE_SIZE    32    /* included in MPT_REQUEST_SIZE */
128 #define MPT_REQ_MEM_SIZE(mpt)   (MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
129
130 /*
131  * We cannot tell prior to getting IOC facts how big the IOC's request
132  * area is. Because of this we cannot tell at compile time how many
133  * simple SG elements we can fit within an IOC request prior to having
134  * to put in a chain element.
135  * 
136  * Experimentally we know that the Ultra4 parts have a 96 byte request
137  * element size and the Fibre Channel units have a 144 byte request
138  * element size. Therefore, if we have 512-32 (== 480) bytes of request
139  * area to play with, we have room for between 3 and 5 request sized
140  * regions- the first of which is the command  plus a simple SG list,
141  * the rest of which are chained continuation SG lists. Given that the
142  * normal request we use is 48 bytes w/o the first SG element, we can
143  * assume we have 480-48 == 432 bytes to have simple SG elements and/or
144  * chain elements. If we assume 32 bit addressing, this works out to
145  * 54 SG or chain elements. If we assume 5 chain elements, then we have
146  * a maximum of 49 seperate actual SG segments.
147  */
148
149 #define MPT_SGL_MAX             49
150
151 #define MPT_RQSL(mpt)           (mpt->request_frame_size << 2)
152 #define MPT_NSGL(mpt)           (MPT_RQSL(mpt) / sizeof (SGE_SIMPLE32))
153
154 #define MPT_NSGL_FIRST(mpt)                             \
155         (((mpt->request_frame_size << 2) -              \
156         sizeof (MSG_SCSI_IO_REQUEST) -                  \
157         sizeof (SGE_IO_UNION)) / sizeof (SGE_SIMPLE32))
158
159 /*
160  * Convert a physical address returned from IOC to kvm address
161  * needed to access the data.
162  */
163 #define MPT_REPLY_PTOV(m, x)            \
164         ((void *)(&m->reply[((x << 1) - m->reply_phys)]))
165
166 #define ccb_mpt_ptr sim_priv.entries[0].ptr
167 #define ccb_req_ptr sim_priv.entries[1].ptr
168
169 enum mpt_req_state {
170     REQ_FREE, REQ_IN_PROGRESS, REQ_TIMEOUT, REQ_ON_CHIP, REQ_DONE
171 };
172 typedef struct req_entry {
173         u_int16_t       index;          /* Index of this entry */
174         union ccb *     ccb;            /* CAM request */
175         void *          req_vbuf;       /* Virtual Address of Entry */
176         void *          sense_vbuf;     /* Virtual Address of sense data */
177         bus_addr_t      req_pbuf;       /* Physical Address of Entry */
178         bus_addr_t      sense_pbuf;     /* Physical Address of sense data */
179         bus_dmamap_t    dmap;           /* DMA map for data buffer */
180         SLIST_ENTRY(req_entry) link;    /* Pointer to next in list */
181         enum mpt_req_state debug;       /* Debugging */
182         u_int32_t       sequence;       /* Sequence Number */
183 } request_t;
184
185
186 /* Structure for saving proper values for modifyable PCI configuration registers */
187 struct mpt_pci_cfg {
188         u_int16_t Command;
189         u_int16_t LatencyTimer_LineSize;
190         u_int32_t IO_BAR;
191         u_int32_t Mem0_BAR[2];
192         u_int32_t Mem1_BAR[2];
193         u_int32_t ROM_BAR;
194         u_int8_t  IntLine;
195         u_int32_t PMCSR;
196 };
197
198 typedef struct mpt_softc {
199         device_t                dev;
200 #ifdef  RELENG_4
201         int                     mpt_splsaved;
202         u_int32_t               mpt_islocked;   
203 #else
204         struct mtx              mpt_lock;
205 #endif
206         u_int32_t               : 16,
207                 unit            : 8,
208                 verbose         : 3,
209                 outofbeer       : 1,
210                 mpt_locksetup   : 1,
211                 disabled        : 1,
212                 is_fc           : 1,
213                 bus             : 1;    /* FC929/1030 have two busses */
214
215         /*
216          * IOC Facts
217          */
218         u_int16_t               mpt_global_credits;
219         u_int16_t               request_frame_size;
220         u_int8_t                mpt_max_devices;
221         u_int8_t                mpt_max_buses;
222
223         /*
224          * Port Facts
225          */
226         u_int16_t               mpt_ini_id;
227
228
229         /*
230          * Device Configuration Information
231          */
232         union {
233                 struct mpt_spi_cfg {
234                         fCONFIG_PAGE_SCSI_PORT_0        _port_page0;
235                         fCONFIG_PAGE_SCSI_PORT_1        _port_page1;
236                         fCONFIG_PAGE_SCSI_PORT_2        _port_page2;
237                         fCONFIG_PAGE_SCSI_DEVICE_0      _dev_page0[16];
238                         fCONFIG_PAGE_SCSI_DEVICE_1      _dev_page1[16];
239                         uint16_t                        _tag_enable;
240                         uint16_t                        _disc_enable;
241                         uint16_t                        _update_params0;
242                         uint16_t                        _update_params1;
243                 } spi;
244 #define mpt_port_page0          cfg.spi._port_page0
245 #define mpt_port_page1          cfg.spi._port_page1
246 #define mpt_port_page2          cfg.spi._port_page2
247 #define mpt_dev_page0           cfg.spi._dev_page0
248 #define mpt_dev_page1           cfg.spi._dev_page1
249 #define mpt_tag_enable          cfg.spi._tag_enable
250 #define mpt_disc_enable         cfg.spi._disc_enable
251 #define mpt_update_params0      cfg.spi._update_params0
252 #define mpt_update_params1      cfg.spi._update_params1
253                 struct mpi_fc_cfg {
254                         u_int8_t        nada;
255                 } fc;
256         } cfg;
257
258         /*
259          * PCI Hardware info
260          */
261         struct resource *       pci_irq;        /* Interrupt map for chip */
262         void *                  ih;             /* Interupt handle */
263         struct mpt_pci_cfg      pci_cfg;        /* saved PCI conf registers */
264
265         /*
266          * DMA Mapping Stuff
267          */
268
269         struct resource *       pci_reg;        /* Register map for chip */
270         int                     pci_reg_id;     /* Resource ID */
271         bus_space_tag_t         pci_st;         /* Bus tag for registers */
272         bus_space_handle_t      pci_sh;         /* Bus handle for registers */
273         vm_offset_t             pci_pa;         /* Physical Address */
274
275         bus_dma_tag_t           parent_dmat;    /* DMA tag for parent PCI bus */
276         bus_dma_tag_t           reply_dmat;     /* DMA tag for reply memory */
277         bus_dmamap_t            reply_dmap;     /* DMA map for reply memory */
278         char *                  reply;          /* KVA of reply memory */
279         bus_addr_t              reply_phys;     /* BusAddr of reply memory */
280
281
282         bus_dma_tag_t           buffer_dmat;    /* DMA tag for buffers */
283         bus_dma_tag_t           request_dmat;   /* DMA tag for request memroy */
284         bus_dmamap_t            request_dmap;   /* DMA map for request memroy */
285         char *                  request;        /* KVA of Request memory */
286         bus_addr_t              request_phys;   /* BusADdr of request memory */
287
288         /*
289          * CAM && Software Management
290          */
291
292         request_t *             request_pool;
293         SLIST_HEAD(req_queue, req_entry) request_free_list;
294
295         struct cam_sim *        sim;
296         struct cam_path *       path;
297
298         u_int32_t               sequence;       /* Sequence Number */
299         u_int32_t               timeouts;       /* timeout count */
300         u_int32_t               success;        /* successes afer timeout */
301
302         /* Opposing port in a 929 or 1030, or NULL */
303         struct mpt_softc *      mpt2;
304
305 } mpt_softc_t;
306
307 #include <dev/mpt/mpt.h>
308
309
310 static INLINE void mpt_write(mpt_softc_t *, size_t, u_int32_t);
311 static INLINE u_int32_t mpt_read(mpt_softc_t *, int);
312
313 static INLINE void
314 mpt_write(mpt_softc_t *mpt, size_t offset, u_int32_t val)
315 {
316         bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
317 }
318
319 static INLINE u_int32_t
320 mpt_read(mpt_softc_t *mpt, int offset)
321 {
322         return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
323 }
324
325 void mpt_cam_attach(mpt_softc_t *);
326 void mpt_cam_detach(mpt_softc_t *);
327 void mpt_done(mpt_softc_t *, u_int32_t);
328 void mpt_set_config_regs(mpt_softc_t *);
329
330 #ifdef  RELENG_4
331 static INLINE void mpt_lockspl(mpt_softc_t *);
332 static INLINE void mpt_unlockspl(mpt_softc_t *);
333
334 static INLINE void
335 mpt_lockspl(mpt_softc_t *mpt)
336 {
337        int s = splcam();
338        if (mpt->mpt_islocked++ == 0) {  
339                mpt->mpt_splsaved = s;
340        } else {
341                splx(s);
342        }
343 }
344
345 static INLINE void
346 mpt_unlockspl(mpt_softc_t *mpt)
347 {
348        if (mpt->mpt_islocked) {
349                if (--mpt->mpt_islocked == 0) {
350                        splx(mpt->mpt_splsaved);
351                }
352        }
353 }
354 #endif
355
356 #endif  /* _MPT_FREEBSD_H */