]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - sys/dev/mpr/mprvar.h
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / sys / dev / mpr / mprvar.h
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * Copyright (c) 2011-2014 LSI Corp.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29
30 #ifndef _MPRVAR_H
31 #define _MPRVAR_H
32
33 #define MPR_DRIVER_VERSION      "05.255.05.00-fbsd"
34
35 #define MPR_DB_MAX_WAIT         2500
36
37 #define MPR_REQ_FRAMES          1024
38 #define MPR_EVT_REPLY_FRAMES    32
39 #define MPR_REPLY_FRAMES        MPR_REQ_FRAMES
40 #define MPR_CHAIN_FRAMES        2048
41 #define MPR_SENSE_LEN           SSD_FULL_SIZE
42 #define MPR_MSI_COUNT           1
43 #define MPR_SGE64_SIZE          12
44 #define MPR_SGE32_SIZE          8
45 #define MPR_SGC_SIZE            8
46
47 #define MPR_FUNCTRACE(sc)                       \
48         mpr_dprint((sc), MPR_TRACE, "%s\n", __func__)
49
50 #define  CAN_SLEEP                      1
51 #define  NO_SLEEP                       0
52
53 #define MPR_PERIODIC_DELAY      1       /* 1 second heartbeat/watchdog check */
54
55 #define IFAULT_IOP_OVER_TEMP_THRESHOLD_EXCEEDED 0x2810
56
57 #define MPR_SCSI_RI_INVALID_FRAME       (0x00000002)
58 #define MPR_STRING_LENGTH               64
59
60 #include <sys/endian.h>
61
62 /*
63  * host mapping related macro definitions
64  */
65 #define MPR_MAPTABLE_BAD_IDX    0xFFFFFFFF
66 #define MPR_DPM_BAD_IDX         0xFFFF
67 #define MPR_ENCTABLE_BAD_IDX    0xFF
68 #define MPR_MAX_MISSING_COUNT   0x0F
69 #define MPR_DEV_RESERVED        0x20000000
70 #define MPR_MAP_IN_USE          0x10000000
71 #define MPR_RAID_CHANNEL        1
72 #define MPR_MAP_BAD_ID          0xFFFFFFFF
73
74 typedef uint8_t u8;
75 typedef uint16_t u16;
76 typedef uint32_t u32;
77 typedef uint64_t u64;
78
79 /**
80  * struct dev_mapping_table - device mapping information
81  * @physical_id: SAS address for drives or WWID for RAID volumes
82  * @device_info: bitfield provides detailed info about the device
83  * @phy_bits: bitfields indicating controller phys
84  * @dpm_entry_num: index of this device in device persistent map table
85  * @dev_handle: device handle for the device pointed by this entry
86  * @channel: target channel
87  * @id: target id
88  * @missing_count: number of times the device not detected by driver
89  * @hide_flag: Hide this physical disk/not (foreign configuration)
90  * @init_complete: Whether the start of the day checks completed or not
91  * @TLR_bits: Turn TLR support on or off
92  */
93 struct dev_mapping_table {
94         u64     physical_id;
95         u32     device_info;
96         u32     phy_bits;
97         u16     dpm_entry_num;
98         u16     dev_handle;
99         u8      reserved1;
100         u8      channel;
101         u16     id;
102         u8      missing_count;
103         u8      init_complete;
104         u8      TLR_bits;
105         u8      reserved2;
106 };
107
108 /**
109  * struct enc_mapping_table -  mapping information about an enclosure
110  * @enclosure_id: Logical ID of this enclosure
111  * @start_index: index to the entry in dev_mapping_table
112  * @phy_bits: bitfields indicating controller phys
113  * @dpm_entry_num: index of this enclosure in device persistent map table
114  * @enc_handle: device handle for the enclosure pointed by this entry
115  * @num_slots: number of slots in the enclosure
116  * @start_slot: Starting slot id
117  * @missing_count: number of times the device not detected by driver
118  * @removal_flag: used to mark the device for removal
119  * @skip_search: used as a flag to include/exclude enclosure for search
120  * @init_complete: Whether the start of the day checks completed or not
121  */
122 struct enc_mapping_table {
123         u64     enclosure_id;
124         u32     start_index;
125         u32     phy_bits;
126         u16     dpm_entry_num;
127         u16     enc_handle;
128         u16     num_slots;
129         u16     start_slot;
130         u8      missing_count;
131         u8      removal_flag;
132         u8      skip_search;
133         u8      init_complete;
134 };
135
136 /**
137  * struct map_removal_table - entries to be removed from mapping table
138  * @dpm_entry_num: index of this device in device persistent map table
139  * @dev_handle: device handle for the device pointed by this entry
140  */
141 struct map_removal_table{
142         u16     dpm_entry_num;
143         u16     dev_handle;
144 };
145
146 typedef struct mpr_fw_diagnostic_buffer {
147         size_t          size;
148         uint8_t         extended_type;
149         uint8_t         buffer_type;
150         uint8_t         force_release;
151         uint32_t        product_specific[23];
152         uint8_t         immediate;
153         uint8_t         enabled;
154         uint8_t         valid_data;
155         uint8_t         owned_by_firmware;
156         uint32_t        unique_id;
157 } mpr_fw_diagnostic_buffer_t;
158
159 struct mpr_softc;
160 struct mpr_command;
161 struct mprsas_softc;
162 union ccb;
163 struct mprsas_target;
164 struct mpr_column_map;
165
166 MALLOC_DECLARE(M_MPR);
167
168 typedef void mpr_evt_callback_t(struct mpr_softc *, uintptr_t,
169     MPI2_EVENT_NOTIFICATION_REPLY *reply);
170 typedef void mpr_command_callback_t(struct mpr_softc *, struct mpr_command *cm);
171
172 struct mpr_chain {
173         TAILQ_ENTRY(mpr_chain)          chain_link;
174         void                            *chain;
175         uint64_t                        chain_busaddr;
176 };
177
178 /*
179  * This needs to be at least 2 to support SMP passthrough.
180  */
181 #define       MPR_IOVEC_COUNT 2
182
183 struct mpr_command {
184         TAILQ_ENTRY(mpr_command)        cm_link;
185         TAILQ_ENTRY(mpr_command)        cm_recovery;
186         struct mpr_softc                *cm_sc;
187         union ccb                       *cm_ccb;
188         void                            *cm_data;
189         u_int                           cm_length;
190         u_int                           cm_out_len;
191         struct uio                      cm_uio;
192         struct iovec                    cm_iovec[MPR_IOVEC_COUNT];
193         u_int                           cm_max_segs;
194         u_int                           cm_sglsize;
195         void                            *cm_sge;
196         uint8_t                         *cm_req;
197         uint8_t                         *cm_reply;
198         uint32_t                        cm_reply_data;
199         mpr_command_callback_t          *cm_complete;
200         void                            *cm_complete_data;
201         struct mprsas_target            *cm_targ;
202         MPI2_REQUEST_DESCRIPTOR_UNION   cm_desc;
203         u_int                           cm_lun;
204         u_int                           cm_flags;
205 #define MPR_CM_FLAGS_POLLED             (1 << 0)
206 #define MPR_CM_FLAGS_COMPLETE           (1 << 1)
207 #define MPR_CM_FLAGS_SGE_SIMPLE         (1 << 2)
208 #define MPR_CM_FLAGS_DATAOUT            (1 << 3)
209 #define MPR_CM_FLAGS_DATAIN             (1 << 4)
210 #define MPR_CM_FLAGS_WAKEUP             (1 << 5)
211 #define MPR_CM_FLAGS_USE_UIO            (1 << 6)
212 #define MPR_CM_FLAGS_SMP_PASS           (1 << 7)
213 #define MPR_CM_FLAGS_CHAIN_FAILED       (1 << 8)
214 #define MPR_CM_FLAGS_ERROR_MASK         MPR_CM_FLAGS_CHAIN_FAILED
215 #define MPR_CM_FLAGS_USE_CCB            (1 << 9)
216         u_int                           cm_state;
217 #define MPR_CM_STATE_FREE               0
218 #define MPR_CM_STATE_BUSY               1
219 #define MPR_CM_STATE_TIMEDOUT           2
220         bus_dmamap_t                    cm_dmamap;
221         struct scsi_sense_data          *cm_sense;
222         TAILQ_HEAD(, mpr_chain) cm_chain_list;
223         uint32_t                        cm_req_busaddr;
224         uint32_t                        cm_sense_busaddr;
225         struct callout                  cm_callout;
226 };
227
228 struct mpr_column_map {
229         uint16_t                        dev_handle;
230         uint8_t                         phys_disk_num;
231 };
232
233 struct mpr_event_handle {
234         TAILQ_ENTRY(mpr_event_handle)   eh_list;
235         mpr_evt_callback_t              *callback;
236         void                            *data;
237         uint8_t                         mask[16];
238 };
239
240 struct mpr_softc {
241         device_t                        mpr_dev;
242         struct cdev                     *mpr_cdev;
243         u_int                           mpr_flags;
244 #define MPR_FLAGS_INTX          (1 << 0)
245 #define MPR_FLAGS_MSI           (1 << 1)
246 #define MPR_FLAGS_BUSY          (1 << 2)
247 #define MPR_FLAGS_SHUTDOWN      (1 << 3)
248 #define MPR_FLAGS_DIAGRESET     (1 << 4)
249 #define MPR_FLAGS_ATTACH_DONE   (1 << 5)
250         u_int                           mpr_debug;
251         u_int                           disable_msix;
252         u_int                           disable_msi;
253         int                             tm_cmds_active;
254         int                             io_cmds_active;
255         int                             io_cmds_highwater;
256         int                             chain_free;
257         int                             max_chains;
258         int                             chain_free_lowwater;
259 #if __FreeBSD_version >= 900030
260         uint64_t                        chain_alloc_fail;
261 #endif
262         struct sysctl_ctx_list          sysctl_ctx;
263         struct sysctl_oid               *sysctl_tree;
264         char                            fw_version[16];
265         struct mpr_command              *commands;
266         struct mpr_chain                *chains;
267         struct callout                  periodic;
268
269         struct mprsas_softc             *sassc;
270         char            tmp_string[MPR_STRING_LENGTH];
271         TAILQ_HEAD(, mpr_command)       req_list;
272         TAILQ_HEAD(, mpr_command)       high_priority_req_list;
273         TAILQ_HEAD(, mpr_chain) chain_list;
274         TAILQ_HEAD(, mpr_command)       tm_list;
275         int                             replypostindex;
276         int                             replyfreeindex;
277
278         struct resource                 *mpr_regs_resource;
279         bus_space_handle_t              mpr_bhandle;
280         bus_space_tag_t                 mpr_btag;
281         int                             mpr_regs_rid;
282
283         bus_dma_tag_t                   mpr_parent_dmat;
284         bus_dma_tag_t                   buffer_dmat;
285
286         MPI2_IOC_FACTS_REPLY            *facts;
287         int                             num_reqs;
288         int                             num_replies;
289         int                             fqdepth;        /* Free queue */
290         int                             pqdepth;        /* Post queue */
291
292         uint8_t                         event_mask[16];
293         TAILQ_HEAD(, mpr_event_handle)  event_list;
294         struct mpr_event_handle *mpr_log_eh;
295
296         struct mtx                      mpr_mtx;
297         struct intr_config_hook         mpr_ich;
298         struct resource                 *mpr_irq[MPR_MSI_COUNT];
299         void                            *mpr_intrhand[MPR_MSI_COUNT];
300         int                             mpr_irq_rid[MPR_MSI_COUNT];
301
302         uint8_t                         *req_frames;
303         bus_addr_t                      req_busaddr;
304         bus_dma_tag_t                   req_dmat;
305         bus_dmamap_t                    req_map;
306
307         uint8_t                         *reply_frames;
308         bus_addr_t                      reply_busaddr;
309         bus_dma_tag_t                   reply_dmat;
310         bus_dmamap_t                    reply_map;
311
312         struct scsi_sense_data          *sense_frames;
313         bus_addr_t                      sense_busaddr;
314         bus_dma_tag_t                   sense_dmat;
315         bus_dmamap_t                    sense_map;
316
317         uint8_t                         *chain_frames;
318         bus_addr_t                      chain_busaddr;
319         bus_dma_tag_t                   chain_dmat;
320         bus_dmamap_t                    chain_map;
321
322         MPI2_REPLY_DESCRIPTORS_UNION    *post_queue;
323         bus_addr_t                      post_busaddr;
324         uint32_t                        *free_queue;
325         bus_addr_t                      free_busaddr;
326         bus_dma_tag_t                   queues_dmat;
327         bus_dmamap_t                    queues_map;
328
329         uint8_t                         *fw_diag_buffer;
330         bus_addr_t                      fw_diag_busaddr;
331         bus_dma_tag_t                   fw_diag_dmat;
332         bus_dmamap_t                    fw_diag_map;
333
334         uint8_t                         ir_firmware;
335
336         /* static config pages */
337         Mpi2IOCPage8_t                  ioc_pg8;
338         Mpi2IOUnitPage8_t               iounit_pg8;
339
340         /* host mapping support */
341         struct dev_mapping_table        *mapping_table;
342         struct enc_mapping_table        *enclosure_table;
343         struct map_removal_table        *removal_table;
344         uint8_t                         *dpm_entry_used;
345         uint8_t                         *dpm_flush_entry;
346         Mpi2DriverMappingPage0_t        *dpm_pg0;
347         uint16_t                        max_devices;
348         uint16_t                        max_enclosures;
349         uint16_t                        max_expanders;
350         uint8_t                         max_volumes;
351         uint8_t                         num_enc_table_entries;
352         uint8_t                         num_rsvd_entries;
353         uint8_t                         num_channels;
354         uint16_t                        max_dpm_entries;
355         uint8_t                         is_dpm_enable;
356         uint8_t                         track_mapping_events;
357         uint32_t                        pending_map_events;
358         uint8_t                         mt_full_retry;
359         uint8_t                         mt_add_device_failed;
360
361         /* FW diag Buffer List */
362         mpr_fw_diagnostic_buffer_t
363                                 fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_COUNT];
364
365         /* Event Recording IOCTL support */
366         uint32_t                        events_to_record[4];
367         mpr_event_entry_t               recorded_events[MPR_EVENT_QUEUE_SIZE];
368         uint8_t                         event_index;
369         uint32_t                        event_number;
370
371         /* EEDP and TLR support */
372         uint8_t                         eedp_enabled;
373         uint8_t                         control_TLR;
374
375         /* Shutdown Event Handler */
376         eventhandler_tag                shutdown_eh;
377
378         /* To track topo events during reset */
379 #define MPR_DIAG_RESET_TIMEOUT  300000
380         uint8_t                         wait_for_port_enable;
381         uint8_t                         port_enable_complete;
382         uint8_t                         msleep_fake_chan;
383
384         /* StartStopUnit command handling at shutdown */
385         uint32_t                        SSU_refcount;
386         uint8_t                         SSU_started;
387
388         char                            exclude_ids[80];
389         struct timeval                  lastfail;
390 };
391
392 struct mpr_config_params {
393         MPI2_CONFIG_EXT_PAGE_HEADER_UNION       hdr;
394         u_int           action;
395         u_int           page_address;   /* Attributes, not a phys address */
396         u_int           status;
397         void            *buffer;
398         u_int           length;
399         int             timeout;
400         void            (*callback)(struct mpr_softc *, struct mpr_config_params *);
401         void            *cbdata;
402 };
403
404 struct scsi_read_capacity_eedp
405 {
406         uint8_t addr[8];
407         uint8_t length[4];
408         uint8_t protect;
409 };
410
411 static __inline uint32_t
412 mpr_regread(struct mpr_softc *sc, uint32_t offset)
413 {
414         return (bus_space_read_4(sc->mpr_btag, sc->mpr_bhandle, offset));
415 }
416
417 static __inline void
418 mpr_regwrite(struct mpr_softc *sc, uint32_t offset, uint32_t val)
419 {
420         bus_space_write_4(sc->mpr_btag, sc->mpr_bhandle, offset, val);
421 }
422
423 /* free_queue must have Little Endian address 
424  * TODO- cm_reply_data is unwanted. We can remove it.
425  * */
426 static __inline void
427 mpr_free_reply(struct mpr_softc *sc, uint32_t busaddr)
428 {
429         if (++sc->replyfreeindex >= sc->fqdepth)
430                 sc->replyfreeindex = 0;
431         sc->free_queue[sc->replyfreeindex] = htole32(busaddr);
432         mpr_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
433 }
434
435 static __inline struct mpr_chain *
436 mpr_alloc_chain(struct mpr_softc *sc)
437 {
438         struct mpr_chain *chain;
439
440         if ((chain = TAILQ_FIRST(&sc->chain_list)) != NULL) {
441                 TAILQ_REMOVE(&sc->chain_list, chain, chain_link);
442                 sc->chain_free--;
443                 if (sc->chain_free < sc->chain_free_lowwater)
444                         sc->chain_free_lowwater = sc->chain_free;
445         }
446 #if __FreeBSD_version >= 900030
447         else
448                 sc->chain_alloc_fail++;
449 #endif
450         return (chain);
451 }
452
453 static __inline void
454 mpr_free_chain(struct mpr_softc *sc, struct mpr_chain *chain)
455 {
456 #if 0
457         bzero(chain->chain, 128);
458 #endif
459         sc->chain_free++;
460         TAILQ_INSERT_TAIL(&sc->chain_list, chain, chain_link);
461 }
462
463 static __inline void
464 mpr_free_command(struct mpr_softc *sc, struct mpr_command *cm)
465 {
466         struct mpr_chain *chain, *chain_temp;
467
468         if (cm->cm_reply != NULL)
469                 mpr_free_reply(sc, cm->cm_reply_data);
470         cm->cm_reply = NULL;
471         cm->cm_flags = 0;
472         cm->cm_complete = NULL;
473         cm->cm_complete_data = NULL;
474         cm->cm_ccb = NULL;
475         cm->cm_targ = NULL;
476         cm->cm_max_segs = 0;
477         cm->cm_lun = 0;
478         cm->cm_state = MPR_CM_STATE_FREE;
479         cm->cm_data = NULL;
480         cm->cm_length = 0;
481         cm->cm_out_len = 0;
482         cm->cm_sglsize = 0;
483         cm->cm_sge = NULL;
484
485         TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
486                 TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
487                 mpr_free_chain(sc, chain);
488         }
489         TAILQ_INSERT_TAIL(&sc->req_list, cm, cm_link);
490 }
491
492 static __inline struct mpr_command *
493 mpr_alloc_command(struct mpr_softc *sc)
494 {
495         struct mpr_command *cm;
496
497         cm = TAILQ_FIRST(&sc->req_list);
498         if (cm == NULL)
499                 return (NULL);
500
501         TAILQ_REMOVE(&sc->req_list, cm, cm_link);
502         KASSERT(cm->cm_state == MPR_CM_STATE_FREE, ("mpr: Allocating busy command\n"));
503         cm->cm_state = MPR_CM_STATE_BUSY;
504         return (cm);
505 }
506
507 static __inline void
508 mpr_free_high_priority_command(struct mpr_softc *sc, struct mpr_command *cm)
509 {
510         struct mpr_chain *chain, *chain_temp;
511
512         if (cm->cm_reply != NULL)
513                 mpr_free_reply(sc, cm->cm_reply_data);
514         cm->cm_reply = NULL;
515         cm->cm_flags = 0;
516         cm->cm_complete = NULL;
517         cm->cm_complete_data = NULL;
518         cm->cm_ccb = NULL;
519         cm->cm_targ = NULL;
520         cm->cm_lun = 0;
521         cm->cm_state = MPR_CM_STATE_FREE;
522         TAILQ_FOREACH_SAFE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
523                 TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
524                 mpr_free_chain(sc, chain);
525         }
526         TAILQ_INSERT_TAIL(&sc->high_priority_req_list, cm, cm_link);
527 }
528
529 static __inline struct mpr_command *
530 mpr_alloc_high_priority_command(struct mpr_softc *sc)
531 {
532         struct mpr_command *cm;
533
534         cm = TAILQ_FIRST(&sc->high_priority_req_list);
535         if (cm == NULL)
536                 return (NULL);
537
538         TAILQ_REMOVE(&sc->high_priority_req_list, cm, cm_link);
539         KASSERT(cm->cm_state == MPR_CM_STATE_FREE, ("mpr: Allocating busy command\n"));
540         cm->cm_state = MPR_CM_STATE_BUSY;
541         return (cm);
542 }
543
544 static __inline void
545 mpr_lock(struct mpr_softc *sc)
546 {
547         mtx_lock(&sc->mpr_mtx);
548 }
549
550 static __inline void
551 mpr_unlock(struct mpr_softc *sc)
552 {
553         mtx_unlock(&sc->mpr_mtx);
554 }
555
556 #define MPR_INFO        (1 << 0)        /* Basic info */
557 #define MPR_FAULT       (1 << 1)        /* Hardware faults */
558 #define MPR_EVENT       (1 << 2)        /* Event data from the controller */
559 #define MPR_LOG         (1 << 3)        /* Log data from the controller */
560 #define MPR_RECOVERY    (1 << 4)        /* Command error recovery tracing */
561 #define MPR_ERROR       (1 << 5)        /* Parameter errors, programming bugs */
562 #define MPR_INIT        (1 << 6)        /* Things related to system init */
563 #define MPR_XINFO       (1 << 7)        /* More detailed/noisy info */
564 #define MPR_USER        (1 << 8)        /* Trace user-generated commands */
565 #define MPR_MAPPING     (1 << 9)        /* Trace device mappings */
566 #define MPR_TRACE       (1 << 10)       /* Function-by-function trace */
567
568 #define mpr_printf(sc, args...)                         \
569         device_printf((sc)->mpr_dev, ##args)
570
571 #define mpr_vprintf(sc, args...)                        \
572 do {                                                    \
573         if (bootverbose)                                \
574                 mpr_printf(sc, ##args);                 \
575 } while (0)
576
577 #define mpr_dprint(sc, level, msg, args...)             \
578 do {                                                    \
579         if ((sc)->mpr_debug & level)                    \
580                 device_printf((sc)->mpr_dev, msg, ##args);      \
581 } while (0)
582
583 #define mpr_dprint_field(sc, level, msg, args...)               \
584 do {                                                            \
585         if ((sc)->mpr_debug & level)                            \
586                 printf("\t" msg, ##args);                       \
587 } while (0)
588
589 #define MPR_PRINTFIELD_START(sc, tag...)        \
590         mpr_dprint((sc), MPR_INFO, ##tag);      \
591         mpr_dprint_field((sc), MPR_INFO, ":\n")
592 #define MPR_PRINTFIELD_END(sc, tag)             \
593         mpr_dprint((sc), MPR_INFO, tag "\n")
594 #define MPR_PRINTFIELD(sc, facts, attr, fmt)    \
595         mpr_dprint_field((sc), MPR_INFO, #attr ": " #fmt "\n", (facts)->attr)
596
597 #define MPR_EVENTFIELD_START(sc, tag...)        \
598         mpr_dprint((sc), MPR_EVENT, ##tag);     \
599         mpr_dprint_field((sc), MPR_EVENT, ":\n")
600 #define MPR_EVENTFIELD(sc, facts, attr, fmt)    \
601         mpr_dprint_field((sc), MPR_EVENT, #attr ": " #fmt "\n", (facts)->attr)
602
603 #define  CAN_SLEEP                      1
604 #define  NO_SLEEP                       0
605
606 static __inline void
607 mpr_from_u64(uint64_t data, U64 *mpr)
608 {
609         (mpr)->High = htole32((uint32_t)((data) >> 32));
610         (mpr)->Low = htole32((uint32_t)((data) & 0xffffffff));
611 }
612
613 static __inline uint64_t
614 mpr_to_u64(U64 *data)
615 {
616
617         return (((uint64_t)le32toh(data->High) << 32) | le32toh(data->Low));
618 }
619
620 static __inline void
621 mpr_mask_intr(struct mpr_softc *sc)
622 {
623         uint32_t mask;
624
625         mask = mpr_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
626         mask |= MPI2_HIM_REPLY_INT_MASK;
627         mpr_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
628 }
629
630 static __inline void
631 mpr_unmask_intr(struct mpr_softc *sc)
632 {
633         uint32_t mask;
634
635         mask = mpr_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
636         mask &= ~MPI2_HIM_REPLY_INT_MASK;
637         mpr_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
638 }
639
640 int mpr_pci_setup_interrupts(struct mpr_softc *sc);
641 int mpr_pci_restore(struct mpr_softc *sc);
642
643 int mpr_attach(struct mpr_softc *sc);
644 int mpr_free(struct mpr_softc *sc);
645 void mpr_intr(void *);
646 void mpr_intr_msi(void *);
647 void mpr_intr_locked(void *);
648 int mpr_register_events(struct mpr_softc *, uint8_t *, mpr_evt_callback_t *,
649     void *, struct mpr_event_handle **);
650 int mpr_restart(struct mpr_softc *);
651 int mpr_update_events(struct mpr_softc *, struct mpr_event_handle *,
652     uint8_t *);
653 int mpr_deregister_events(struct mpr_softc *, struct mpr_event_handle *);
654 int mpr_push_sge(struct mpr_command *, MPI2_SGE_SIMPLE64 *, size_t, int);
655 int mpr_push_ieee_sge(struct mpr_command *, void *, int);
656 int mpr_add_dmaseg(struct mpr_command *, vm_paddr_t, size_t, u_int, int);
657 int mpr_attach_sas(struct mpr_softc *sc);
658 int mpr_detach_sas(struct mpr_softc *sc);
659 int mpr_read_config_page(struct mpr_softc *, struct mpr_config_params *);
660 int mpr_write_config_page(struct mpr_softc *, struct mpr_config_params *);
661 void mpr_memaddr_cb(void *, bus_dma_segment_t *, int , int );
662 void mpr_init_sge(struct mpr_command *cm, void *req, void *sge);
663 int mpr_attach_user(struct mpr_softc *);
664 void mpr_detach_user(struct mpr_softc *);
665 void mprsas_record_event(struct mpr_softc *sc,
666     MPI2_EVENT_NOTIFICATION_REPLY *event_reply);
667
668 int mpr_map_command(struct mpr_softc *sc, struct mpr_command *cm);
669 int mpr_wait_command(struct mpr_softc *sc, struct mpr_command *cm,
670     int timeout, int sleep_flag);
671 int mpr_request_polled(struct mpr_softc *sc, struct mpr_command *cm);
672
673 int mpr_config_get_bios_pg3(struct mpr_softc *sc, Mpi2ConfigReply_t
674     *mpi_reply, Mpi2BiosPage3_t *config_page);
675 int mpr_config_get_raid_volume_pg0(struct mpr_softc *sc, Mpi2ConfigReply_t
676     *mpi_reply, Mpi2RaidVolPage0_t *config_page, u32 page_address);
677 int mpr_config_get_ioc_pg8(struct mpr_softc *sc, Mpi2ConfigReply_t *,
678     Mpi2IOCPage8_t *);
679 int mpr_config_get_iounit_pg8(struct mpr_softc *sc,
680     Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage8_t *config_page);
681 int mpr_config_get_sas_device_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
682     Mpi2SasDevicePage0_t *, u32 , u16 );
683 int mpr_config_get_dpm_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
684     Mpi2DriverMappingPage0_t *, u16 );
685 int mpr_config_get_raid_volume_pg1(struct mpr_softc *sc,
686     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidVolPage1_t *config_page, u32 form,
687     u16 handle);
688 int mpr_config_get_volume_wwid(struct mpr_softc *sc, u16 volume_handle,
689     u64 *wwid);
690 int mpr_config_get_raid_pd_pg0(struct mpr_softc *sc,
691     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidPhysDiskPage0_t *config_page,
692     u32 page_address);
693 void mprsas_ir_shutdown(struct mpr_softc *sc);
694
695 int mpr_reinit(struct mpr_softc *sc);
696 void mprsas_handle_reinit(struct mpr_softc *sc);
697
698 void mpr_base_static_config_pages(struct mpr_softc *sc);
699
700 int mpr_mapping_initialize(struct mpr_softc *);
701 void mpr_mapping_topology_change_event(struct mpr_softc *,
702     Mpi2EventDataSasTopologyChangeList_t *);
703 int mpr_mapping_is_reinit_required(struct mpr_softc *);
704 void mpr_mapping_free_memory(struct mpr_softc *sc);
705 int mpr_config_set_dpm_pg0(struct mpr_softc *, Mpi2ConfigReply_t *,
706     Mpi2DriverMappingPage0_t *, u16 );
707 void mpr_mapping_exit(struct mpr_softc *);
708 void mpr_mapping_check_devices(struct mpr_softc *, int);
709 int mpr_mapping_allocate_memory(struct mpr_softc *sc);
710 unsigned int mpr_mapping_get_sas_id(struct mpr_softc *, uint64_t , u16);
711 unsigned int mpr_mapping_get_sas_id_from_handle(struct mpr_softc *sc,
712     u16 handle);
713 unsigned int mpr_mapping_get_raid_id(struct mpr_softc *sc, u64 wwid,
714     u16 handle);
715 unsigned int mpr_mapping_get_raid_id_from_handle(struct mpr_softc *sc,
716     u16 volHandle);
717 void mpr_mapping_enclosure_dev_status_change_event(struct mpr_softc *,
718     Mpi2EventDataSasEnclDevStatusChange_t *event_data);
719 void mpr_mapping_ir_config_change_event(struct mpr_softc *sc,
720     Mpi2EventDataIrConfigChangeList_t *event_data);
721
722 void mprsas_evt_handler(struct mpr_softc *sc, uintptr_t data,
723     MPI2_EVENT_NOTIFICATION_REPLY *event);
724 void mprsas_prepare_remove(struct mprsas_softc *sassc, uint16_t handle);
725 void mprsas_prepare_volume_remove(struct mprsas_softc *sassc,
726     uint16_t handle);
727 int mprsas_startup(struct mpr_softc *sc);
728 struct mprsas_target * mprsas_find_target_by_handle(struct mprsas_softc *,
729     int, uint16_t);
730
731 SYSCTL_DECL(_hw_mpr);
732
733 /* Compatibility shims for different OS versions */
734 #if __FreeBSD_version >= 800001
735 #define mpr_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
736     kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
737 #define mpr_kproc_exit(arg)     kproc_exit(arg)
738 #else
739 #define mpr_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
740     kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
741 #define mpr_kproc_exit(arg)     kthread_exit(arg)
742 #endif
743
744 #if defined(CAM_PRIORITY_XPT)
745 #define MPR_PRIORITY_XPT        CAM_PRIORITY_XPT
746 #else
747 #define MPR_PRIORITY_XPT        5
748 #endif
749
750 #if __FreeBSD_version < 800107
751 // Prior to FreeBSD-8.0 scp3_flags was not defined.
752 #define spc3_flags reserved
753
754 #define SPC3_SID_PROTECT    0x01
755 #define SPC3_SID_3PC        0x08
756 #define SPC3_SID_TPGS_MASK  0x30
757 #define SPC3_SID_TPGS_IMPLICIT  0x10
758 #define SPC3_SID_TPGS_EXPLICIT  0x20
759 #define SPC3_SID_ACC        0x40
760 #define SPC3_SID_SCCS       0x80
761
762 #define CAM_PRIORITY_NORMAL CAM_PRIORITY_NONE
763 #endif
764
765 #endif
766