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