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