Merge branch 'vendor/OPENSSH'
[dragonfly.git] / sys / dev / disk / mpt / mpt.h
1 /* $FreeBSD: src/sys/dev/mpt/mpt.h,v 1.48 2009/07/10 08:18:08 scottl Exp $ */
2 /*-
3  * Generic defines for LSI '909 FC  adapters.
4  * FreeBSD Version.
5  *
6  * Copyright (c)  2000, 2001 by Greg Ansley
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*-
30  * Copyright (c) 2002, 2006 by Matthew Jacob
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions are
35  * met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
39  *    substantially similar to the "NO WARRANTY" disclaimer below
40  *    ("Disclaimer") and any redistribution must be conditioned upon including
41  *    a substantially similar Disclaimer requirement for further binary
42  *    redistribution.
43  * 3. Neither the names of the above listed copyright holders nor the names
44  *    of any contributors may be used to endorse or promote products derived
45  *    from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
57  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59  * Support from Chris Ellsworth in order to make SAS adapters work
60  * is gratefully acknowledged.
61  *
62  *
63  * Support from LSI-Logic has also gone a great deal toward making this a
64  * workable subsystem and is gratefully acknowledged.
65  */
66 /*
67  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
68  * Copyright (c) 2004, 2005 Justin T. Gibbs
69  * Copyright (c) 2005, WHEEL Sp. z o.o.
70  * All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions are
74  * met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
78  *    substantially similar to the "NO WARRANTY" disclaimer below
79  *    ("Disclaimer") and any redistribution must be conditioned upon including
80  *    a substantially similar Disclaimer requirement for further binary
81  *    redistribution.
82  * 3. Neither the names of the above listed copyright holders nor the names
83  *    of any contributors may be used to endorse or promote products derived
84  *    from this software without specific prior written permission.
85  *
86  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
90  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
91  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
92  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
94  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
95  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
96  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  */
98
99 #ifndef _MPT_H_
100 #define _MPT_H_
101
102 /********************************* OS Includes ********************************/
103 #include <sys/types.h>
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/endian.h>
107 #include <sys/eventhandler.h>
108 #if __FreeBSD_version < 500000
109 #include <sys/kernel.h>
110 #include <sys/queue.h>
111 #include <sys/malloc.h>
112 #include <sys/devicestat.h>
113 #include <sys/thread2.h>
114 #else
115 #include <sys/lock.h>
116 #include <sys/kernel.h>
117 #include <sys/queue.h>
118 #include <sys/malloc.h>
119 #include <sys/mutex.h>
120 #include <sys/condvar.h>
121 #endif
122 #include <sys/proc.h>
123 #include <sys/bus.h>
124 #include <sys/module.h>
125
126 #include <sys/rman.h>
127
128 #if __FreeBSD_version < 500000
129 #include <bus/pci/pcireg.h>
130 #include <bus/pci/pcivar.h>
131 #else
132 #include <dev/pci/pcireg.h>
133 #include <dev/pci/pcivar.h>
134 #endif
135
136 #include "opt_ddb.h"
137
138 /**************************** Register Definitions ****************************/
139 #include <dev/disk/mpt/mpt_reg.h>
140
141 /******************************* MPI Definitions ******************************/
142 #include <dev/disk/mpt/mpilib/mpi_type.h>
143 #include <dev/disk/mpt/mpilib/mpi.h>
144 #include <dev/disk/mpt/mpilib/mpi_cnfg.h>
145 #include <dev/disk/mpt/mpilib/mpi_ioc.h>
146 #include <dev/disk/mpt/mpilib/mpi_raid.h>
147
148 /* XXX For mpt_debug.c */
149 #include <dev/disk/mpt/mpilib/mpi_init.h>
150
151 #define MPT_S64_2_SCALAR(y)     ((((int64_t)y.High) << 32) | (y.Low))
152 #define MPT_U64_2_SCALAR(y)     ((((uint64_t)y.High) << 32) | (y.Low))
153
154 /****************************** Misc Definitions ******************************/
155 /* #define MPT_TEST_MULTIPATH   1 */
156 #define MPT_OK (0)
157 #define MPT_FAIL (0x10000)
158
159 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
160
161 #define MPT_ROLE_NONE           0
162 #define MPT_ROLE_INITIATOR      1
163 #define MPT_ROLE_TARGET         2
164 #define MPT_ROLE_BOTH           3
165 #define MPT_ROLE_DEFAULT        MPT_ROLE_INITIATOR
166
167 /**************************** Forward Declarations ****************************/
168 struct mpt_softc;
169 struct mpt_personality;
170 typedef struct req_entry request_t;
171
172 /************************* Personality Module Support *************************/
173 typedef int mpt_load_handler_t(struct mpt_personality *);
174 typedef int mpt_probe_handler_t(struct mpt_softc *);
175 typedef int mpt_attach_handler_t(struct mpt_softc *);
176 typedef int mpt_enable_handler_t(struct mpt_softc *);
177 typedef void mpt_ready_handler_t(struct mpt_softc *);
178 typedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
179                                 MSG_EVENT_NOTIFY_REPLY *);
180 typedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
181 /* XXX Add return value and use for veto? */
182 typedef void mpt_shutdown_handler_t(struct mpt_softc *);
183 typedef void mpt_detach_handler_t(struct mpt_softc *);
184 typedef int mpt_unload_handler_t(struct mpt_personality *);
185
186 struct mpt_personality
187 {
188         const char              *name;
189         uint32_t                 id;            /* Assigned identifier. */
190         u_int                    use_count;     /* Instances using personality*/
191         mpt_load_handler_t      *load;          /* configure personailty */
192 #define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
193         mpt_probe_handler_t     *probe;         /* configure personailty */
194         mpt_attach_handler_t    *attach;        /* initialize device instance */
195         mpt_enable_handler_t    *enable;        /* enable device */
196         mpt_ready_handler_t     *ready;         /* final open for business */
197         mpt_event_handler_t     *event;         /* Handle MPI event. */
198         mpt_reset_handler_t     *reset;         /* Re-init after reset. */
199         mpt_shutdown_handler_t  *shutdown;      /* Shutdown instance. */
200         mpt_detach_handler_t    *detach;        /* release device instance */
201         mpt_unload_handler_t    *unload;        /* Shutdown personality */
202 #define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
203 };
204
205 int mpt_modevent(module_t, int, void *);
206
207 /* Maximum supported number of personalities. */
208 #define MPT_MAX_PERSONALITIES   (15)
209
210 #define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
211         MODULE_DEPEND(name, dep, vmin, vpref, vmax)
212
213 #define DECLARE_MPT_PERSONALITY(name, order)                              \
214         static moduledata_t name##_mod = {                                \
215                 #name, mpt_modevent, &name##_personality                  \
216         };                                                                \
217         DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);          \
218         MODULE_VERSION(name, 1);                                          \
219         MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
220
221 /******************************* Bus DMA Support ******************************/
222 /* XXX Need to update bus_dmamap_sync to take a range argument. */
223 #define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op) \
224         bus_dmamap_sync(dma_tag, dmamap, op)
225
226 #if __FreeBSD_version < 600000
227 #define bus_get_dma_tag(x)      NULL
228 #endif
229 #if __FreeBSD_version >= 501102
230 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,        \
231                            lowaddr, highaddr, filter, filterarg,        \
232                            maxsize, nsegments, maxsegsz, flags,         \
233                            dma_tagp)                                    \
234         bus_dma_tag_create(parent_tag, alignment, boundary,             \
235                            lowaddr, highaddr, filter, filterarg,        \
236                            maxsize, nsegments, maxsegsz, flags,         \
237                            busdma_lock_mutex, &(mpt)->mpt_lock,         \
238                            dma_tagp)
239 #else
240 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,        \
241                            lowaddr, highaddr, filter, filterarg,        \
242                            maxsize, nsegments, maxsegsz, flags,         \
243                            dma_tagp)                                    \
244         bus_dma_tag_create(parent_tag, alignment, boundary,             \
245                            lowaddr, highaddr, filter, filterarg,        \
246                            maxsize, nsegments, maxsegsz, flags,         \
247                            dma_tagp)
248 #endif
249
250 struct mpt_map_info {
251         struct mpt_softc *mpt;
252         int               error;
253         uint32_t          phys;
254 };
255
256 void mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
257 /* **************************** NewBUS interrupt Crock ************************/
258 #ifdef __DragonFly__
259 #define mpt_setup_intr(d, i, f, U, if, ifa, hp) \
260         bus_setup_intr(d, i, f, if, ifa, hp, NULL)
261 #else
262 #if __FreeBSD_version < 700031
263 #define mpt_setup_intr(d, i, f, U, if, ifa, hp) \
264         bus_setup_intr(d, i, f, if, ifa, hp)
265 #else
266 #define mpt_setup_intr  bus_setup_intr
267 #endif
268 #endif
269
270 /* **************************** NewBUS CAM Support ****************************/
271 #if __FreeBSD_version < 700049
272 #define mpt_xpt_bus_register(sim, parent, bus)  \
273         xpt_bus_register(sim, bus)
274 #else
275 #define mpt_xpt_bus_register    xpt_bus_register
276 #endif
277
278 /**************************** Kernel Thread Support ***************************/
279 #if __FreeBSD_version > 800001
280 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
281         kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
282 #define mpt_kthread_exit(status)        \
283         kproc_exit(status)
284 #elif __FreeBSD_version > 500005
285 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
286         kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
287 #define mpt_kthread_exit(status)        \
288         kthread_exit(status)
289 #else
290 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
291         kthread_create(func, farg, proc_ptr, fmtstr, arg)
292 #define mpt_kthread_exit(status)        \
293         kthread_exit()
294 #endif
295
296 /****************************** Timer Facilities ******************************/
297 #if __FreeBSD_version > 500000
298 #define mpt_callout_init(c)     callout_init(c, /*mpsafe*/1);
299 #else
300 #define mpt_callout_init(c)     callout_init(c);
301 #endif
302
303 /********************************** Endianess *********************************/
304 #define MPT_2_HOST64(ptr, tag)  ptr->tag = le64toh(ptr->tag)
305 #define MPT_2_HOST32(ptr, tag)  ptr->tag = le32toh(ptr->tag)
306 #define MPT_2_HOST16(ptr, tag)  ptr->tag = le16toh(ptr->tag)
307
308 #define HOST_2_MPT64(ptr, tag)  ptr->tag = htole64(ptr->tag)
309 #define HOST_2_MPT32(ptr, tag)  ptr->tag = htole32(ptr->tag)
310 #define HOST_2_MPT16(ptr, tag)  ptr->tag = htole16(ptr->tag)
311
312 #if     _BYTE_ORDER == _BIG_ENDIAN
313 void mpt2host_sge_simple_union(SGE_SIMPLE_UNION *);
314 void mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *);
315 void mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *);
316 void mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *);
317 void mpt2host_config_page_ioc3(CONFIG_PAGE_IOC_3 *);
318 void mpt2host_config_page_scsi_port_0(CONFIG_PAGE_SCSI_PORT_0 *);
319 void mpt2host_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *);
320 void host2mpt_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *);
321 void mpt2host_config_page_scsi_port_2(CONFIG_PAGE_SCSI_PORT_2 *);
322 void mpt2host_config_page_scsi_device_0(CONFIG_PAGE_SCSI_DEVICE_0 *);
323 void mpt2host_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *);
324 void host2mpt_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *);
325 void mpt2host_config_page_fc_port_0(CONFIG_PAGE_FC_PORT_0 *);
326 void mpt2host_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *);
327 void host2mpt_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *);
328 void mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *);
329 void mpt2host_config_page_raid_phys_disk_0(CONFIG_PAGE_RAID_PHYS_DISK_0 *);
330 void mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *);
331 #else
332 #define mpt2host_sge_simple_union(x)            do { ; } while (0)
333 #define mpt2host_iocfacts_reply(x)              do { ; } while (0)
334 #define mpt2host_portfacts_reply(x)             do { ; } while (0)
335 #define mpt2host_config_page_ioc2(x)            do { ; } while (0)
336 #define mpt2host_config_page_ioc3(x)            do { ; } while (0)
337 #define mpt2host_config_page_scsi_port_0(x)     do { ; } while (0)
338 #define mpt2host_config_page_scsi_port_1(x)     do { ; } while (0)
339 #define host2mpt_config_page_scsi_port_1(x)     do { ; } while (0)
340 #define mpt2host_config_page_scsi_port_2(x)     do { ; } while (0)
341 #define mpt2host_config_page_scsi_device_0(x)   do { ; } while (0)
342 #define mpt2host_config_page_scsi_device_1(x)   do { ; } while (0)
343 #define host2mpt_config_page_scsi_device_1(x)   do { ; } while (0)
344 #define mpt2host_config_page_fc_port_0(x)       do { ; } while (0)
345 #define mpt2host_config_page_fc_port_1(x)       do { ; } while (0)
346 #define host2mpt_config_page_fc_port_1(x)       do { ; } while (0)
347 #define mpt2host_config_page_raid_vol_0(x)      do { ; } while (0)
348 #define mpt2host_config_page_raid_phys_disk_0(x)                        \
349         do { ; } while (0)
350 #define mpt2host_mpi_raid_vol_indicator(x)      do { ; } while (0)
351 #endif
352
353 /**************************** MPI Transaction State ***************************/
354 typedef enum {
355         REQ_STATE_NIL           = 0x00,
356         REQ_STATE_FREE          = 0x01,
357         REQ_STATE_ALLOCATED     = 0x02,
358         REQ_STATE_QUEUED        = 0x04,
359         REQ_STATE_DONE          = 0x08,
360         REQ_STATE_TIMEDOUT      = 0x10,
361         REQ_STATE_NEED_WAKEUP   = 0x20,
362         REQ_STATE_LOCKED        = 0x80, /* can't be freed */
363         REQ_STATE_MASK          = 0xFF
364 } mpt_req_state_t;
365
366 struct req_entry {
367         TAILQ_ENTRY(req_entry) links;   /* Pointer to next in list */
368         mpt_req_state_t state;          /* Request State Information */
369         uint16_t        index;          /* Index of this entry */
370         uint16_t        IOCStatus;      /* Completion status */
371         uint16_t        ResponseCode;   /* TMF Reponse Code */
372         uint16_t        serno;          /* serial number */
373         union ccb      *ccb;            /* CAM request */
374         void           *req_vbuf;       /* Virtual Address of Entry */
375         void           *sense_vbuf;     /* Virtual Address of sense data */
376         bus_addr_t      req_pbuf;       /* Physical Address of Entry */
377         bus_addr_t      sense_pbuf;     /* Physical Address of sense data */
378         bus_dmamap_t    dmap;           /* DMA map for data buffers */
379         struct req_entry *chain;        /* for SGE overallocations */
380         struct callout  callout;        /* Timeout for the request */
381 };
382
383 typedef struct mpt_config_params {
384         u_int           Action;
385         u_int           PageVersion;
386         u_int           PageLength;
387         u_int           PageNumber;
388         u_int           PageType;
389         u_int           PageAddress;
390         u_int           ExtPageLength;
391         u_int           ExtPageType;
392 } cfgparms_t;
393
394 /**************************** MPI Target State Info ***************************/
395
396 typedef struct {
397         uint32_t reply_desc;    /* current reply descriptor */
398         uint32_t resid;         /* current data residual */
399         uint32_t bytes_xfered;  /* current relative offset */
400         union ccb *ccb;         /* pointer to currently active ccb */
401         request_t *req;         /* pointer to currently active assist request */
402         uint32_t
403                 is_local : 1,
404                 nxfers   : 31;
405         uint32_t tag_id;
406         enum {
407                 TGT_STATE_NIL,
408                 TGT_STATE_LOADING,
409                 TGT_STATE_LOADED,
410                 TGT_STATE_IN_CAM,
411                 TGT_STATE_SETTING_UP_FOR_DATA,
412                 TGT_STATE_MOVING_DATA,
413                 TGT_STATE_MOVING_DATA_AND_STATUS,
414                 TGT_STATE_SENDING_STATUS
415         } state;
416 } mpt_tgt_state_t;
417
418 /*
419  * When we get an incoming command it has its own tag which is called the
420  * IoIndex. This is the value we gave that particular command buffer when
421  * we originally assigned it. It's just a number, really. The FC card uses
422  * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
423  * contains pointers the request_t structures related to that IoIndex.
424  *
425  * What *we* do is construct a tag out of the index for the target command
426  * which owns the incoming ATIO plus a rolling sequence number.
427  */
428 #define MPT_MAKE_TAGID(mpt, req, ioindex)       \
429  ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
430
431 #ifdef  INVARIANTS
432 #define MPT_TAG_2_REQ(a, b)             mpt_tag_2_req(a, (uint32_t) b)
433 #else
434 #define MPT_TAG_2_REQ(mpt, tag)         mpt->tgt_cmd_ptrs[tag >> 18]
435 #endif
436
437 #define MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
438     (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
439
440 STAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
441 #define MPT_MAX_LUNS    256
442 typedef struct {
443         struct mpt_hdr_stailq   atios;
444         struct mpt_hdr_stailq   inots;
445         int enabled;
446 } tgt_resource_t;
447 #define MPT_MAX_ELS     64
448
449 /**************************** Handler Registration ****************************/
450 /*
451  * Global table of registered reply handlers.  The
452  * handler is indicated by byte 3 of the request
453  * index submitted to the IOC.  This allows the
454  * driver core to perform generic processing without
455  * any knowledge of per-personality behavior.
456  *
457  * MPT_NUM_REPLY_HANDLERS must be a power of 2
458  * to allow the easy generation of a mask.
459  *
460  * The handler offsets used by the core are hard coded
461  * allowing faster code generation when assigning a handler
462  * to a request.  All "personalities" must use the
463  * the handler registration mechanism.
464  *
465  * The IOC handlers that are rarely executed are placed
466  * at the tail of the table to make it more likely that
467  * all commonly executed handlers fit in a single cache
468  * line.
469  */
470 #define MPT_NUM_REPLY_HANDLERS          (32)
471 #define MPT_REPLY_HANDLER_EVENTS        MPT_CBI_TO_HID(0)
472 #define MPT_REPLY_HANDLER_CONFIG        MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
473 #define MPT_REPLY_HANDLER_HANDSHAKE     MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
474 typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
475     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
476 typedef union {
477         mpt_reply_handler_t     *reply_handler;
478 } mpt_handler_t;
479
480 typedef enum {
481         MPT_HANDLER_REPLY,
482         MPT_HANDLER_EVENT,
483         MPT_HANDLER_RESET,
484         MPT_HANDLER_SHUTDOWN
485 } mpt_handler_type;
486
487 struct mpt_handler_record
488 {
489         LIST_ENTRY(mpt_handler_record)  links;
490         mpt_handler_t                   handler;
491 };
492
493 LIST_HEAD(mpt_handler_list, mpt_handler_record);
494
495 /*
496  * The handler_id is currently unused but would contain the
497  * handler ID used in the MsgContext field to allow direction
498  * of replies to the handler.  Registrations that don't require
499  * a handler id can pass in NULL for the handler_id.
500  *
501  * Deregistrations for handlers without a handler id should
502  * pass in MPT_HANDLER_ID_NONE.
503  */
504 #define MPT_HANDLER_ID_NONE             (0xFFFFFFFF)
505 int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
506                          mpt_handler_t, uint32_t *);
507 int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
508                            mpt_handler_t, uint32_t);
509
510 /******************* Per-Controller Instance Data Structures ******************/
511 TAILQ_HEAD(req_queue, req_entry);
512
513 /* Structure for saving proper values for modifyable PCI config registers */
514 struct mpt_pci_cfg {
515         uint16_t Command;
516         uint16_t LatencyTimer_LineSize;
517         uint32_t IO_BAR;
518         uint32_t Mem0_BAR[2];
519         uint32_t Mem1_BAR[2];
520         uint32_t ROM_BAR;
521         uint8_t  IntLine;
522         uint32_t PMCSR;
523 };
524
525 typedef enum {
526         MPT_RVF_NONE            = 0x0,
527         MPT_RVF_ACTIVE          = 0x1,
528         MPT_RVF_ANNOUNCED       = 0x2,
529         MPT_RVF_UP2DATE         = 0x4,
530         MPT_RVF_REFERENCED      = 0x8,
531         MPT_RVF_WCE_CHANGED     = 0x10
532 } mpt_raid_volume_flags;
533
534 struct mpt_raid_volume {
535         CONFIG_PAGE_RAID_VOL_0         *config_page;
536         MPI_RAID_VOL_INDICATOR          sync_progress;
537         mpt_raid_volume_flags           flags;
538         u_int                           quiesced_disks;
539 };
540
541 typedef enum {
542         MPT_RDF_NONE            = 0x00,
543         MPT_RDF_ACTIVE          = 0x01,
544         MPT_RDF_ANNOUNCED       = 0x02,
545         MPT_RDF_UP2DATE         = 0x04,
546         MPT_RDF_REFERENCED      = 0x08,
547         MPT_RDF_QUIESCING       = 0x10,
548         MPT_RDF_QUIESCED        = 0x20
549 } mpt_raid_disk_flags;
550
551 struct mpt_raid_disk {
552         CONFIG_PAGE_RAID_PHYS_DISK_0    config_page;
553         struct mpt_raid_volume         *volume;
554         u_int                           member_number;
555         u_int                           pass_thru_active;
556         mpt_raid_disk_flags             flags;
557 };
558
559 struct mpt_evtf_record {
560         MSG_EVENT_NOTIFY_REPLY          reply;
561         uint32_t                        context;
562         LIST_ENTRY(mpt_evtf_record)     links;
563 };
564
565 LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
566
567 struct mptsas_devinfo {
568         uint16_t        dev_handle;
569         uint16_t        parent_dev_handle;
570         uint16_t        enclosure_handle;
571         uint16_t        slot;
572         uint8_t         phy_num;
573         uint8_t         physical_port;
574         uint8_t         target_id;
575         uint8_t         bus;
576         uint64_t        sas_address;
577         uint32_t        device_info;
578 };
579
580 struct mptsas_phyinfo {
581         uint16_t        handle;
582         uint8_t         phy_num;
583         uint8_t         port_id;
584         uint8_t         negotiated_link_rate;
585         uint8_t         hw_link_rate;
586         uint8_t         programmed_link_rate;
587         uint8_t         sas_port_add_phy;
588         struct mptsas_devinfo identify;
589         struct mptsas_devinfo attached;
590 };
591
592 struct mptsas_portinfo {
593         uint16_t                        num_phys;
594         struct mptsas_phyinfo           *phy_info;
595 };
596
597 struct mpt_softc {
598         device_t                dev;
599 #if __FreeBSD_version < 500000
600         uint32_t                mpt_islocked;
601         int                     mpt_splsaved;
602 #else
603         struct mtx              mpt_lock;
604         int                     mpt_locksetup;
605 #endif
606         uint32_t                mpt_pers_mask;
607         uint32_t
608                                 : 8,
609                 unit            : 8,
610                 ready           : 1,
611                 fw_uploaded     : 1,
612                 msi_enable      : 1,
613                 twildcard       : 1,
614                 tenabled        : 1,
615                 do_cfg_role     : 1,
616                 raid_enabled    : 1,
617                 raid_mwce_set   : 1,
618                 getreqwaiter    : 1,
619                 shutdwn_raid    : 1,
620                 shutdwn_recovery: 1,
621                 outofbeer       : 1,
622                 disabled        : 1,
623                 is_spi          : 1,
624                 is_sas          : 1,
625                 is_fc           : 1;
626
627         u_int                   cfg_role;
628         u_int                   role;   /* role: none, ini, target, both */
629
630         u_int                   verbose;
631 #ifdef  MPT_TEST_MULTIPATH
632         int                     failure_id;
633 #endif
634
635         /*
636          * IOC Facts
637          */
638         MSG_IOC_FACTS_REPLY     ioc_facts;
639
640         /*
641          * Port Facts
642          */
643         MSG_PORT_FACTS_REPLY *  port_facts;
644 #define mpt_ini_id      port_facts[0].PortSCSIID
645 #define mpt_max_tgtcmds port_facts[0].MaxPostedCmdBuffers
646
647         /*
648          * Device Configuration Information
649          */
650         union {
651                 struct mpt_spi_cfg {
652                         CONFIG_PAGE_SCSI_PORT_0         _port_page0;
653                         CONFIG_PAGE_SCSI_PORT_1         _port_page1;
654                         CONFIG_PAGE_SCSI_PORT_2         _port_page2;
655                         CONFIG_PAGE_SCSI_DEVICE_0       _dev_page0[16];
656                         CONFIG_PAGE_SCSI_DEVICE_1       _dev_page1[16];
657                         uint16_t                        _tag_enable;
658                         uint16_t                        _disc_enable;
659                 } spi;
660 #define mpt_port_page0          cfg.spi._port_page0
661 #define mpt_port_page1          cfg.spi._port_page1
662 #define mpt_port_page2          cfg.spi._port_page2
663 #define mpt_dev_page0           cfg.spi._dev_page0
664 #define mpt_dev_page1           cfg.spi._dev_page1
665 #define mpt_tag_enable          cfg.spi._tag_enable
666 #define mpt_disc_enable         cfg.spi._disc_enable
667                 struct mpi_fc_cfg {
668                         CONFIG_PAGE_FC_PORT_0 _port_page0;
669                         uint32_t _port_speed;
670 #define mpt_fcport_page0        cfg.fc._port_page0
671 #define mpt_fcport_speed        cfg.fc._port_speed
672                 } fc;
673         } cfg;
674 #if __FreeBSD_version >= 500000
675         /*
676          * Device config information stored up for sysctl to access
677          */
678         union {
679                 struct {
680                         unsigned int initiator_id;
681                 } spi;
682                 struct {
683                         char wwnn[19];
684                         char wwpn[19];
685                 } fc;
686         } scinfo;
687 #endif
688
689         /* Controller Info for RAID information */
690         CONFIG_PAGE_IOC_2 *     ioc_page2;
691         CONFIG_PAGE_IOC_3 *     ioc_page3;
692
693         /* Raid Data */
694         struct mpt_raid_volume* raid_volumes;
695         struct mpt_raid_disk*   raid_disks;
696         u_int                   raid_max_volumes;
697         u_int                   raid_max_disks;
698         u_int                   raid_page0_len;
699         u_int                   raid_wakeup;
700         u_int                   raid_rescan;
701         u_int                   raid_resync_rate;
702         u_int                   raid_mwce_setting;
703         u_int                   raid_queue_depth;
704         u_int                   raid_nonopt_volumes;
705         thread_t                raid_thread;
706         struct callout          raid_timer;
707
708         /*
709          * PCI Hardware info
710          */
711         int                     pci_msi_count;
712         struct resource *       pci_irq;        /* Interrupt map for chip */
713         void *                  ih;             /* Interupt handle */
714         struct mpt_pci_cfg      pci_cfg;        /* saved PCI conf registers */
715
716         /*
717          * DMA Mapping Stuff
718          */
719         struct resource *       pci_reg;        /* Register map for chip */
720         int                     pci_mem_rid;    /* Resource ID */
721         bus_space_tag_t         pci_st;         /* Bus tag for registers */
722         bus_space_handle_t      pci_sh;         /* Bus handle for registers */
723         /* PIO versions of above. */
724         int                     pci_pio_rid;
725         struct resource *       pci_pio_reg;
726         bus_space_tag_t         pci_pio_st;
727         bus_space_handle_t      pci_pio_sh;
728
729         bus_dma_tag_t           parent_dmat;    /* DMA tag for parent PCI bus */
730         bus_dma_tag_t           reply_dmat;     /* DMA tag for reply memory */
731         bus_dmamap_t            reply_dmap;     /* DMA map for reply memory */
732         uint8_t                *reply;          /* KVA of reply memory */
733         bus_addr_t              reply_phys;     /* BusAddr of reply memory */
734
735         bus_dma_tag_t           buffer_dmat;    /* DMA tag for buffers */
736         bus_dma_tag_t           request_dmat;   /* DMA tag for request memroy */
737         bus_dmamap_t            request_dmap;   /* DMA map for request memroy */
738         uint8_t                *request;        /* KVA of Request memory */
739         bus_addr_t              request_phys;   /* BusAddr of request memory */
740
741         uint32_t                max_seg_cnt;    /* calculated after IOC facts */
742
743         /*
744          * Hardware management
745          */
746         u_int                   reset_cnt;
747
748         /*
749          * CAM && Software Management
750          */
751         request_t              *request_pool;
752         struct req_queue        request_free_list;
753         struct req_queue        request_pending_list;
754         struct req_queue        request_timeout_list;
755
756
757         struct cam_sim         *sim;
758         struct cam_path        *path;
759
760         struct cam_sim         *phydisk_sim;
761         struct cam_path        *phydisk_path;
762
763         thread_t               recovery_thread;
764         request_t              *tmf_req;
765
766         /*
767          * Deferred frame acks due to resource shortage.
768          */
769         struct mpt_evtf_list    ack_frames;
770
771         /*
772          * Target Mode Support
773          */
774         uint32_t                scsi_tgt_handler_id;
775         request_t **            tgt_cmd_ptrs;
776         request_t **            els_cmd_ptrs;   /* FC only */
777
778         /*
779          * *snork*- this is chosen to be here *just in case* somebody
780          * forgets to point to it exactly and we index off of trt with
781          * CAM_LUN_WILDCARD.
782          */
783         tgt_resource_t          trt_wildcard;           /* wildcard luns */
784         tgt_resource_t          trt[MPT_MAX_LUNS];
785         uint16_t                tgt_cmds_allocated;
786         uint16_t                els_cmds_allocated;     /* FC only */
787
788         uint16_t                timeouts;       /* timeout count */
789         uint16_t                success;        /* successes afer timeout */
790         uint16_t                sequence;       /* Sequence Number */
791         uint16_t                pad3;
792
793
794         /* Paired port in some dual adapters configurations */
795         struct mpt_softc *      mpt2;
796
797         /* FW Image management */
798         uint32_t                fw_image_size;
799         uint8_t                *fw_image;
800         bus_dma_tag_t           fw_dmat;        /* DMA tag for firmware image */
801         bus_dmamap_t            fw_dmap;        /* DMA map for firmware image */
802         bus_addr_t              fw_phys;        /* BusAddr of firmware image */
803
804         /* SAS Topology */
805         struct mptsas_portinfo  *sas_portinfo;
806
807         /* Shutdown Event Handler. */
808         eventhandler_tag         eh;
809
810         /* Userland management interface. */
811         struct cdev             *cdev;
812
813         TAILQ_ENTRY(mpt_softc)  links;
814 };
815
816 static __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
817
818 static __inline void
819 mpt_assign_serno(struct mpt_softc *mpt, request_t *req)
820 {
821         if ((req->serno = mpt->sequence++) == 0) {
822                 req->serno = mpt->sequence++;
823         }
824 }
825
826 /***************************** Locking Primitives *****************************/
827 #ifdef __DragonFly__
828 #define PUSER 0
829 #define MPT_IFLAGS              0
830 #define MPT_LOCK(mpt)           crit_enter()
831 #define MPT_UNLOCK(mpt)         crit_exit()
832 #define MPT_LOCK_SETUP(mpt)     do { } while(0)
833 #define MPT_LOCK_DESTROY(mpt)   do { } while(0)
834 #define MPT_LOCK_ASSERT(mpt)    do { } while(0)
835 #define MPTLOCK_2_CAMLOCK       MPT_UNLOCK
836 #define CAMLOCK_2_MPTLOCK       MPT_LOCK
837 #define splx(s)
838 #define splsoftvm() 0
839 static __inline int
840 mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
841            const char *wmesg, int timo) {
842         int error;
843         error = tsleep(ident, 0, wmesg, timo);
844         return(error);
845 }
846 #endif
847 #define mpt_req_timeout(req, ticks, func, arg) \
848         callout_reset(&(req)->callout, (ticks), (func), (arg));
849 #define mpt_req_untimeout(req, func, arg) \
850         callout_stop(&(req)->callout)
851 #if 0
852 #if __FreeBSD_version < 500000
853 #define MPT_IFLAGS              INTR_TYPE_CAM
854 #define MPT_LOCK(mpt)           mpt_lockspl(mpt)
855 #define MPT_UNLOCK(mpt)         mpt_unlockspl(mpt)
856 #define MPT_OWNED(mpt)          mpt->mpt_islocked
857 #define MPT_LOCK_ASSERT(mpt)
858 #define MPTLOCK_2_CAMLOCK       MPT_UNLOCK
859 #define CAMLOCK_2_MPTLOCK       MPT_LOCK
860 #define MPT_LOCK_SETUP(mpt)
861 #define MPT_LOCK_DESTROY(mpt)
862
863 static __inline void mpt_lockspl(struct mpt_softc *mpt);
864 static __inline void mpt_unlockspl(struct mpt_softc *mpt);
865
866 static __inline void
867 mpt_lockspl(struct mpt_softc *mpt)
868 {
869        int s;
870
871        s = splcam();
872        if (mpt->mpt_islocked++ == 0) {
873                mpt->mpt_splsaved = s;
874        } else {
875                splx(s);
876                panic("Recursed lock with mask: 0x%x\n", s);
877        }
878 }
879
880 static __inline void
881 mpt_unlockspl(struct mpt_softc *mpt)
882 {
883        if (mpt->mpt_islocked) {
884                if (--mpt->mpt_islocked == 0) {
885                        splx(mpt->mpt_splsaved);
886                }
887        } else
888                panic("Negative lock count\n");
889 }
890
891 static __inline int
892 mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
893            const char *wmesg, int timo)
894 {
895         int saved_cnt;
896         int saved_spl;
897         int error;
898
899         KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
900         saved_cnt = mpt->mpt_islocked;
901         saved_spl = mpt->mpt_splsaved;
902         mpt->mpt_islocked = 0;
903         error = tsleep(ident, priority, wmesg, timo);
904         KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
905         mpt->mpt_islocked = saved_cnt;
906         mpt->mpt_splsaved = saved_spl;
907         return (error);
908 }
909
910 #define mpt_req_timeout(req, ticks, func, arg) \
911         callout_reset(&(req)->callout, (ticks), (func), (arg));
912 #define mpt_req_untimeout(req, func, arg) \
913         callout_stop(&(req)->callout)
914 #else
915 #if 1
916 #define MPT_IFLAGS              INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
917 #define MPT_LOCK_SETUP(mpt)                                             \
918                 mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);         \
919                 mpt->mpt_locksetup = 1
920 #define MPT_LOCK_DESTROY(mpt)                                           \
921         if (mpt->mpt_locksetup) {                                       \
922                 mtx_destroy(&mpt->mpt_lock);                            \
923                 mpt->mpt_locksetup = 0;                                 \
924         }
925
926 #define MPT_LOCK(mpt)           mtx_lock(&(mpt)->mpt_lock)
927 #define MPT_UNLOCK(mpt)         mtx_unlock(&(mpt)->mpt_lock)
928 #define MPT_OWNED(mpt)          mtx_owned(&(mpt)->mpt_lock)
929 #define MPT_LOCK_ASSERT(mpt)    mtx_assert(&(mpt)->mpt_lock, MA_OWNED)
930 #define MPTLOCK_2_CAMLOCK(mpt)
931 #define CAMLOCK_2_MPTLOCK(mpt)
932 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
933         msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
934 #define mpt_req_timeout(req, ticks, func, arg) \
935         callout_reset(&(req)->callout, (ticks), (func), (arg));
936 #define mpt_req_untimeout(req, func, arg) \
937         callout_stop(&(req)->callout)
938 #else
939
940 #define MPT_IFLAGS              INTR_TYPE_CAM | INTR_ENTROPY
941 #define MPT_LOCK_SETUP(mpt)     do { } while (0)
942 #define MPT_LOCK_DESTROY(mpt)   do { } while (0)
943 #define MPT_LOCK_ASSERT(mpt)    mtx_assert(&Giant, MA_OWNED)
944 #define MPT_LOCK(mpt)           mtx_lock(&Giant)
945 #define MPT_UNLOCK(mpt)         mtx_unlock(&Giant)
946 #define MPTLOCK_2_CAMLOCK(mpt)
947 #define CAMLOCK_2_MPTLOCK(mpt)
948
949 static __inline int
950 mpt_sleep(struct mpt_softc *, void *, int, const char *, int);
951
952 #define mpt_ccb_timeout(ccb, ticks, func, arg) \
953         do {    \
954                 (ccb)->ccb_h.timeout_ch = timeout((func), (arg), (ticks)); \
955         } while (0)
956 #define mpt_ccb_untimeout(ccb, func, arg) \
957         untimeout((func), (arg), (ccb)->ccb_h.timeout_ch)
958 #define mpt_ccb_timeout_init(ccb) \
959         callout_handle_init(&(ccb)->ccb_h.timeout_ch)
960
961 static __inline int
962 mpt_sleep(struct mpt_softc *mpt, void *i, int p, const char *w, int t)
963 {
964         int r;
965         r = tsleep(i, p, w, t);
966         return (r);
967 }
968 #endif
969 #endif
970 #endif
971
972 /******************************* Register Access ******************************/
973 static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
974 static __inline uint32_t mpt_read(struct mpt_softc *, int);
975 static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
976 static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
977
978 static __inline void
979 mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
980 {
981         bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
982 }
983
984 static __inline uint32_t
985 mpt_read(struct mpt_softc *mpt, int offset)
986 {
987         return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
988 }
989
990 /*
991  * Some operations (e.g. diagnostic register writes while the ARM proccessor
992  * is disabled), must be performed using "PCI pio" operations.  On non-PCI
993  * busses, these operations likely map to normal register accesses.
994  */
995 static __inline void
996 mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
997 {
998         bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
999 }
1000
1001 static __inline uint32_t
1002 mpt_pio_read(struct mpt_softc *mpt, int offset)
1003 {
1004         return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
1005 }
1006 /*********************** Reply Frame/Request Management ***********************/
1007 /* Max MPT Reply we are willing to accept (must be power of 2) */
1008 #define MPT_REPLY_SIZE          256
1009
1010 /* Max i/o size, based on legacy MAXPHYS.  Can be increased. */
1011 #define MPT_MAXPHYS             (128 * 1024)
1012
1013 /*
1014  * Must be less than 16384 in order for target mode to work
1015  */
1016 #define MPT_MAX_REQUESTS(mpt)   512
1017 #define MPT_REQUEST_AREA        512
1018 #define MPT_SENSE_SIZE          32      /* included in MPT_REQUEST_AREA */
1019 #define MPT_REQ_MEM_SIZE(mpt)   (MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
1020
1021 #define MPT_CONTEXT_CB_SHIFT    (16)
1022 #define MPT_CBI(handle)         (handle >> MPT_CONTEXT_CB_SHIFT)
1023 #define MPT_CBI_TO_HID(cbi)     ((cbi) << MPT_CONTEXT_CB_SHIFT)
1024 #define MPT_CONTEXT_TO_CBI(x)   \
1025     (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
1026 #define MPT_CONTEXT_REQI_MASK   0xFFFF
1027 #define MPT_CONTEXT_TO_REQI(x)  ((x) & MPT_CONTEXT_REQI_MASK)
1028
1029 /*
1030  * Convert a 32bit physical address returned from IOC to an
1031  * offset into our reply frame memory or the kvm address needed
1032  * to access the data.  The returned address is only the low
1033  * 32 bits, so mask our base physical address accordingly.
1034  */
1035 #define MPT_REPLY_BADDR(x)              \
1036         (x << 1)
1037 #define MPT_REPLY_OTOV(m, i)            \
1038         ((void *)(&m->reply[i]))
1039
1040 #define MPT_DUMP_REPLY_FRAME(mpt, reply_frame)          \
1041 do {                                                    \
1042         if (mpt->verbose > MPT_PRT_DEBUG)               \
1043                 mpt_dump_reply_frame(mpt, reply_frame); \
1044 } while(0)
1045
1046 static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
1047 static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
1048
1049 /*
1050  * Give the reply buffer back to the IOC after we have
1051  * finished processing it.
1052  */
1053 static __inline void
1054 mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
1055 {
1056      mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
1057 }
1058
1059 /* Get a reply from the IOC */
1060 static __inline uint32_t
1061 mpt_pop_reply_queue(struct mpt_softc *mpt)
1062 {
1063      return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
1064 }
1065
1066 void
1067 mpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
1068
1069 /************************** Scatter Gather Managment **************************/
1070 /* MPT_RQSL- size of request frame, in bytes */
1071 #define MPT_RQSL(mpt)           (mpt->ioc_facts.RequestFrameSize << 2)
1072
1073 /* MPT_NSGL- how many SG entries can fit in a request frame size */
1074 #define MPT_NSGL(mpt)           (MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
1075
1076 /* MPT_NRFM- how many request frames can fit in each request alloc we make */
1077 #define MPT_NRFM(mpt)           (MPT_REQUEST_AREA / MPT_RQSL(mpt))
1078
1079 /*
1080  * MPT_NSGL_FIRST- # of SG elements that can fit after
1081  * an I/O request but still within the request frame.
1082  * Do this safely based upon SGE_IO_UNION.
1083  *
1084  * Note that the first element is *within* the SCSI request.
1085  */
1086 #define MPT_NSGL_FIRST(mpt)     \
1087     ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
1088     sizeof (SGE_IO_UNION))
1089
1090 /***************************** IOC Initialization *****************************/
1091 int mpt_reset(struct mpt_softc *, int /*reinit*/);
1092
1093 /****************************** Debugging ************************************/
1094 typedef struct mpt_decode_entry {
1095         char    *name;
1096         u_int    value;
1097         u_int    mask;
1098 } mpt_decode_entry_t;
1099
1100 int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
1101                      const char *name, u_int value, u_int *cur_column,
1102                      u_int wrap_point);
1103
1104 void mpt_dump_data(struct mpt_softc *, const char *, void *, int);
1105 void mpt_dump_request(struct mpt_softc *, request_t *);
1106
1107 enum {
1108         MPT_PRT_ALWAYS,
1109         MPT_PRT_FATAL,
1110         MPT_PRT_ERROR,
1111         MPT_PRT_WARN,
1112         MPT_PRT_INFO,
1113         MPT_PRT_NEGOTIATION,
1114         MPT_PRT_DEBUG,
1115         MPT_PRT_DEBUG1,
1116         MPT_PRT_DEBUG2,
1117         MPT_PRT_DEBUG3,
1118         MPT_PRT_TRACE,
1119         MPT_PRT_NONE=100
1120 };
1121
1122 #if __FreeBSD_version > 500000
1123 #define mpt_lprt(mpt, level, ...)               \
1124 do {                                            \
1125         if (level <= (mpt)->verbose)            \
1126                 mpt_prt(mpt, __VA_ARGS__);      \
1127 } while (0)
1128
1129 #define mpt_lprtc(mpt, level, ...)               \
1130 do {                                             \
1131         if (level <= (mpt)->debug_level)         \
1132                 mpt_prtc(mpt, __VA_ARGS__);      \
1133 } while (0)
1134 #else
1135 void mpt_lprt(struct mpt_softc *, int, const char *, ...)
1136         __printflike(3, 4);
1137 void mpt_lprtc(struct mpt_softc *, int, const char *, ...)
1138         __printflike(3, 4);
1139 #endif
1140 void mpt_prt(struct mpt_softc *, const char *, ...)
1141         __printflike(2, 3);
1142 void mpt_prtc(struct mpt_softc *, const char *, ...)
1143         __printflike(2, 3);
1144
1145 /**************************** Target Mode Related ***************************/
1146 static __inline int mpt_cdblen(uint8_t, int);
1147 static __inline int
1148 mpt_cdblen(uint8_t cdb0, int maxlen)
1149 {
1150         int group = cdb0 >> 5;
1151         switch (group) {
1152         case 0:
1153                 return (6);
1154         case 1:
1155                 return (10);
1156         case 4:
1157         case 5:
1158                 return (12);
1159         default:
1160                 return (16);
1161         }
1162 }
1163 #ifdef  INVARIANTS
1164 static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
1165 static __inline request_t *
1166 mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
1167 {
1168         uint16_t rtg = (tag >> 18);
1169         KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
1170         KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
1171         KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
1172         return (mpt->tgt_cmd_ptrs[rtg]);
1173 }
1174
1175
1176 static __inline int
1177 mpt_req_on_free_list(struct mpt_softc *, request_t *);
1178 static __inline int
1179 mpt_req_on_pending_list(struct mpt_softc *, request_t *);
1180
1181 static __inline void
1182 mpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1183 static __inline void
1184 mpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1185
1186
1187 /*
1188  * Is request on freelist?
1189  */
1190 static __inline int
1191 mpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
1192 {
1193         request_t *lrq;
1194
1195         TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
1196                 if (lrq == req) {
1197                         return (1);
1198                 }
1199         }
1200         return (0);
1201 }
1202
1203 /*
1204  * Is request on pending list?
1205  */
1206 static __inline int
1207 mpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1208 {
1209         request_t *lrq;
1210
1211         TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1212                 if (lrq == req) {
1213                         return (1);
1214                 }
1215         }
1216         return (0);
1217 }
1218
1219 /*
1220  * Make sure that req *is* part of one of the special lists
1221  */
1222 static __inline void
1223 mpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1224 {
1225         int i;
1226         for (i = 0; i < mpt->els_cmds_allocated; i++) {
1227                 if (req == mpt->els_cmd_ptrs[i]) {
1228                         return;
1229                 }
1230         }
1231         for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1232                 if (req == mpt->tgt_cmd_ptrs[i]) {
1233                         return;
1234                 }
1235         }
1236         panic("%s(%d): req %p:%u function %x not in els or tgt ptrs\n",
1237             s, line, req, req->serno,
1238             ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1239 }
1240
1241 /*
1242  * Make sure that req is *not* part of one of the special lists.
1243  */
1244 static __inline void
1245 mpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1246 {
1247         int i;
1248         for (i = 0; i < mpt->els_cmds_allocated; i++) {
1249                 KASSERT(req != mpt->els_cmd_ptrs[i],
1250                     ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d\n",
1251                     s, line, req, req->serno,
1252                     ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1253         }
1254         for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1255                 KASSERT(req != mpt->tgt_cmd_ptrs[i],
1256                     ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d\n",
1257                     s, line, req, req->serno,
1258                     ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1259         }
1260 }
1261 #endif
1262
1263 /*
1264  * Task Management Types, purely for internal consumption
1265  */
1266 typedef enum {
1267         MPT_ABORT_TASK_SET=1234,
1268         MPT_CLEAR_TASK_SET,
1269         MPT_TARGET_RESET,
1270         MPT_CLEAR_ACA,
1271         MPT_TERMINATE_TASK,
1272         MPT_NIL_TMT_VALUE=5678
1273 } mpt_task_mgmt_t;
1274
1275 /**************************** Unclassified Routines ***************************/
1276 void            mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1277 int             mpt_recv_handshake_reply(struct mpt_softc *mpt,
1278                                          size_t reply_len, void *reply);
1279 int             mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1280                              mpt_req_state_t state, mpt_req_state_t mask,
1281                              int sleep_ok, int time_ms);
1282 void            mpt_enable_ints(struct mpt_softc *mpt);
1283 void            mpt_disable_ints(struct mpt_softc *mpt);
1284 int             mpt_attach(struct mpt_softc *mpt);
1285 int             mpt_shutdown(struct mpt_softc *mpt);
1286 int             mpt_detach(struct mpt_softc *mpt);
1287 int             mpt_send_handshake_cmd(struct mpt_softc *mpt,
1288                                        size_t len, void *cmd);
1289 request_t *     mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1290 void            mpt_free_request(struct mpt_softc *mpt, request_t *req);
1291 void            mpt_intr(void *arg);
1292 void            mpt_check_doorbell(struct mpt_softc *mpt);
1293 void            mpt_dump_reply_frame(struct mpt_softc *mpt,
1294                                      MSG_DEFAULT_REPLY *reply_frame);
1295
1296 void            mpt_set_config_regs(struct mpt_softc *);
1297 int             mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1298                                   cfgparms_t *params,
1299                                   bus_addr_t /*addr*/, bus_size_t/*len*/,
1300                                   int /*sleep_ok*/, int /*timeout_ms*/);
1301 int             mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion,
1302                                        int PageNumber, uint32_t PageAddress,
1303                                        int ExtPageType,
1304                                        CONFIG_EXTENDED_PAGE_HEADER *rslt,
1305                                        int sleep_ok, int timeout_ms);
1306 int             mpt_read_extcfg_page(struct mpt_softc *mpt, int Action,
1307                                      uint32_t PageAddress,
1308                                      CONFIG_EXTENDED_PAGE_HEADER *hdr,
1309                                      void *buf, size_t len, int sleep_ok,
1310                                      int timeout_ms);
1311 int             mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1312                                     int /*PageNumber*/,
1313                                     uint32_t /*PageAddress*/,
1314                                     CONFIG_PAGE_HEADER *,
1315                                     int /*sleep_ok*/, int /*timeout_ms*/);
1316 int             mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1317                                   uint32_t /*PageAddress*/,
1318                                   CONFIG_PAGE_HEADER *, size_t /*len*/,
1319                                   int /*sleep_ok*/, int /*timeout_ms*/);
1320 int             mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1321                                    uint32_t /*PageAddress*/,
1322                                    CONFIG_PAGE_HEADER *, size_t /*len*/,
1323                                    int /*sleep_ok*/, int /*timeout_ms*/);
1324 static __inline int
1325 mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1326                       CONFIG_PAGE_HEADER *hdr, size_t len,
1327                       int sleep_ok, int timeout_ms)
1328 {
1329         return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1330                                   PageAddress, hdr, len, sleep_ok, timeout_ms));
1331 }
1332
1333 static __inline int
1334 mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1335                        CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1336                        int timeout_ms)
1337 {
1338         return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1339                                    PageAddress, hdr, len, sleep_ok,
1340                                    timeout_ms));
1341 }
1342 /* mpt_debug.c functions */
1343 void mpt_print_reply(void *vmsg);
1344 void mpt_print_db(uint32_t mb);
1345 void mpt_print_config_reply(void *vmsg);
1346 char *mpt_ioc_diag(uint32_t diag);
1347 void mpt_req_state(mpt_req_state_t state);
1348 void mpt_print_config_request(void *vmsg);
1349 void mpt_print_request(void *vmsg);
1350 void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1351 void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1352 #endif /* _MPT_H_ */