Merge branch 'vendor/EXPAT'
[dragonfly.git] / sys / dev / raid / mps / mpsvar.h
1 /*-
2  * Copyright (c) 2009 Yahoo! Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/mps/mpsvar.h,v 1.9 2012/01/26 18:17:21 ken Exp $
27  */
28 /*-
29  * Copyright (c) 2011 LSI Corp.
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * LSI MPT-Fusion Host Adapter FreeBSD
54  *
55  * $FreeBSD: src/sys/dev/mps/mpsvar.h,v 1.9 2012/01/26 18:17:21 ken Exp $
56  */
57
58 #ifndef _MPSVAR_H
59 #define _MPSVAR_H
60
61 #define MPS_DRIVER_VERSION      "11.255.03.00-fbsd"
62
63 #define MPS_DB_MAX_WAIT         2500
64
65 #define MPS_REQ_FRAMES          1024
66 #define MPS_EVT_REPLY_FRAMES    32
67 #define MPS_REPLY_FRAMES        MPS_REQ_FRAMES
68 #define MPS_CHAIN_FRAMES        2048
69 #define MPS_SENSE_LEN           SSD_FULL_SIZE
70 #define MPS_MSI_COUNT           1
71 #define MPS_SGE64_SIZE          12
72 #define MPS_SGE32_SIZE          8
73 #define MPS_SGC_SIZE            8
74
75 #define  CAN_SLEEP                      1
76 #define  NO_SLEEP                       0
77
78 #define MPS_PERIODIC_DELAY      1       /* 1 second heartbeat/watchdog check */
79
80 #define MPS_SCSI_RI_INVALID_FRAME       (0x00000002)
81
82 /*
83  * host mapping related macro definitions
84  */
85 #define MPS_MAPTABLE_BAD_IDX    0xFFFFFFFF
86 #define MPS_DPM_BAD_IDX         0xFFFF
87 #define MPS_ENCTABLE_BAD_IDX    0xFF
88 #define MPS_MAX_MISSING_COUNT   0x0F
89 #define MPS_DEV_RESERVED        0x20000000
90 #define MPS_MAP_IN_USE          0x10000000
91 #define MPS_RAID_CHANNEL        1
92 #define MPS_MAP_BAD_ID          0xFFFFFFFF
93
94 /*
95  * WarpDrive controller
96  */
97 #define MPS_CHIP_WD_DEVICE_ID   0x007E
98 #define MPS_WD_LSI_OEM          0x80
99 #define MPS_WD_HIDE_EXPOSE_MASK 0x03
100 #define MPS_WD_HIDE_ALWAYS      0x00
101 #define MPS_WD_EXPOSE_ALWAYS    0x01
102 #define MPS_WD_HIDE_IF_VOLUME   0x02
103 #define MPS_WD_RETRY            0x01
104 #define MPS_MAN_PAGE10_SIZE     0x5C    /* Hardcode for now */
105 #define MPS_MAX_DISKS_IN_VOL    10
106
107 /*
108  * WarpDrive Event Logging
109  */
110 #define MPI2_WD_LOG_ENTRY       0x8002
111 #define MPI2_WD_SSD_THROTTLING  0x0041
112 #define MPI2_WD_DRIVE_LIFE_WARN 0x0043
113 #define MPI2_WD_DRIVE_LIFE_DEAD 0x0044
114 #define MPI2_WD_RAIL_MON_FAIL   0x004D
115
116 typedef uint8_t u8;
117 typedef uint16_t u16;
118 typedef uint32_t u32;
119 typedef uint64_t u64;
120
121 /**
122  * struct dev_mapping_table - device mapping information
123  * @physical_id: SAS address for drives or WWID for RAID volumes
124  * @device_info: bitfield provides detailed info about the device
125  * @phy_bits: bitfields indicating controller phys
126  * @dpm_entry_num: index of this device in device persistent map table
127  * @dev_handle: device handle for the device pointed by this entry
128  * @channel: target channel
129  * @id: target id
130  * @missing_count: number of times the device not detected by driver
131  * @hide_flag: Hide this physical disk/not (foreign configuration)
132  * @init_complete: Whether the start of the day checks completed or not
133  */
134 struct dev_mapping_table {
135         u64     physical_id;
136         u32     device_info;
137         u32     phy_bits;
138         u16     dpm_entry_num;
139         u16     dev_handle;
140         u8      reserved1;
141         u8      channel;
142         u16     id;
143         u8      missing_count;
144         u8      init_complete;
145         u8      TLR_bits;
146         u8      reserved2;
147 };
148
149 /**
150  * struct enc_mapping_table -  mapping information about an enclosure
151  * @enclosure_id: Logical ID of this enclosure
152  * @start_index: index to the entry in dev_mapping_table
153  * @phy_bits: bitfields indicating controller phys
154  * @dpm_entry_num: index of this enclosure in device persistent map table
155  * @enc_handle: device handle for the enclosure pointed by this entry
156  * @num_slots: number of slots in the enclosure
157  * @start_slot: Starting slot id
158  * @missing_count: number of times the device not detected by driver
159  * @removal_flag: used to mark the device for removal
160  * @skip_search: used as a flag to include/exclude enclosure for search
161  * @init_complete: Whether the start of the day checks completed or not
162  */
163 struct enc_mapping_table {
164         u64     enclosure_id;
165         u32     start_index;
166         u32     phy_bits;
167         u16     dpm_entry_num;
168         u16     enc_handle;
169         u16     num_slots;
170         u16     start_slot;
171         u8      missing_count;
172         u8      removal_flag;
173         u8      skip_search;
174         u8      init_complete;
175 };
176
177 /**
178  * struct map_removal_table - entries to be removed from mapping table
179  * @dpm_entry_num: index of this device in device persistent map table
180  * @dev_handle: device handle for the device pointed by this entry
181  */
182 struct map_removal_table{
183         u16     dpm_entry_num;
184         u16     dev_handle;
185 };
186
187 typedef struct mps_fw_diagnostic_buffer {
188         size_t          size;
189         uint8_t         extended_type;
190         uint8_t         buffer_type;
191         uint8_t         force_release;
192         uint32_t        product_specific[23];
193         uint8_t         immediate;
194         uint8_t         enabled;
195         uint8_t         valid_data;
196         uint8_t         owned_by_firmware;
197         uint32_t        unique_id;
198 } mps_fw_diagnostic_buffer_t;
199
200 struct mps_softc;
201 struct mps_command;
202 struct mpssas_softc;
203 union ccb;
204 struct mpssas_target;
205 struct mps_column_map;
206
207 MALLOC_DECLARE(M_MPT2);
208
209 typedef void mps_evt_callback_t(struct mps_softc *, uintptr_t,
210     MPI2_EVENT_NOTIFICATION_REPLY *reply);
211 typedef void mps_command_callback_t(struct mps_softc *, struct mps_command *cm);
212
213 struct mps_chain {
214         TAILQ_ENTRY(mps_chain)          chain_link;
215         MPI2_SGE_IO_UNION               *chain;
216         uint32_t                        chain_busaddr;
217 };
218
219 /*
220  * This needs to be at least 2 to support SMP passthrough.
221  */
222 #define       MPS_IOVEC_COUNT 2
223
224 struct mps_command {
225         TAILQ_ENTRY(mps_command)        cm_link;
226         TAILQ_ENTRY(mps_command)        cm_recovery;
227         struct mps_softc                *cm_sc;
228         union ccb                       *cm_ccb;
229         void                            *cm_data;
230         u_int                           cm_length;
231         u_int                           cm_out_len;
232         struct uio                      cm_uio;
233         struct iovec                    cm_iovec[MPS_IOVEC_COUNT];
234         u_int                           cm_max_segs;
235         u_int                           cm_sglsize;
236         MPI2_SGE_IO_UNION               *cm_sge;
237         uint8_t                         *cm_req;
238         uint8_t                         *cm_reply;
239         uint32_t                        cm_reply_data;
240         mps_command_callback_t          *cm_complete;
241         void                            *cm_complete_data;
242         struct mpssas_target            *cm_targ;
243         MPI2_REQUEST_DESCRIPTOR_UNION   cm_desc;
244         u_int                           cm_lun;
245         u_int                           cm_flags;
246 #define MPS_CM_FLAGS_POLLED             (1 << 0)
247 #define MPS_CM_FLAGS_COMPLETE           (1 << 1)
248 #define MPS_CM_FLAGS_SGE_SIMPLE         (1 << 2)
249 #define MPS_CM_FLAGS_DATAOUT            (1 << 3)
250 #define MPS_CM_FLAGS_DATAIN             (1 << 4)
251 #define MPS_CM_FLAGS_WAKEUP             (1 << 5)
252 #define MPS_CM_FLAGS_DD_IO              (1 << 6)
253 #define MPS_CM_FLAGS_USE_UIO            (1 << 7)
254 #define MPS_CM_FLAGS_SMP_PASS           (1 << 8)
255 #define MPS_CM_FLAGS_CHAIN_FAILED       (1 << 9)
256 #define MPS_CM_FLAGS_ERROR_MASK         MPS_CM_FLAGS_CHAIN_FAILED
257         u_int                           cm_state;
258 #define MPS_CM_STATE_FREE               0
259 #define MPS_CM_STATE_BUSY               1
260 #define MPS_CM_STATE_TIMEDOUT           2
261         bus_dmamap_t                    cm_dmamap;
262         struct scsi_sense_data          *cm_sense;
263         TAILQ_HEAD(, mps_chain)         cm_chain_list;
264         uint32_t                        cm_req_busaddr;
265         uint32_t                        cm_sense_busaddr;
266         struct callout                  cm_callout;
267 };
268
269 struct mps_column_map {
270         uint16_t                        dev_handle;
271         uint8_t                         phys_disk_num;
272 };
273
274 struct mps_event_handle {
275         TAILQ_ENTRY(mps_event_handle)   eh_list;
276         mps_evt_callback_t              *callback;
277         void                            *data;
278         uint8_t                         mask[16];
279 };
280
281 struct mps_softc {
282         device_t                        mps_dev;
283         struct cdev                     *mps_cdev;
284         u_int                           mps_flags;
285 #define MPS_FLAGS_INTX          (1 << 0)
286 #define MPS_FLAGS_MSI           (1 << 1)
287 #define MPS_FLAGS_BUSY          (1 << 2)
288 #define MPS_FLAGS_SHUTDOWN      (1 << 3)
289 #define MPS_FLAGS_DIAGRESET     (1 << 4)
290 #define MPS_FLAGS_ATTACH_DONE   (1 << 5)
291 #define MPS_FLAGS_WD_AVAILABLE  (1 << 6)
292         u_int                           mps_debug;
293 #if 0 /* XXX swildner */
294         u_int                           disable_msix;
295 #endif
296         u_int                           enable_msi;
297         int                             tm_cmds_active;
298         int                             io_cmds_active;
299         int                             io_cmds_highwater;
300         int                             chain_free;
301         int                             max_chains;
302         int                             chain_free_lowwater;
303 #if __FreeBSD_version >= 900030
304         uint64_t                        chain_alloc_fail;
305 #endif
306         struct sysctl_ctx_list          sysctl_ctx;
307         struct sysctl_oid               *sysctl_tree;
308         char                            fw_version[16];
309         struct mps_command              *commands;
310         struct mps_chain                *chains;
311         struct callout                  periodic;
312
313         struct mpssas_softc             *sassc;
314
315         TAILQ_HEAD(, mps_command)       req_list;
316         TAILQ_HEAD(, mps_command)       high_priority_req_list;
317         TAILQ_HEAD(, mps_chain)         chain_list;
318         TAILQ_HEAD(, mps_command)       tm_list;
319         int                             replypostindex;
320         int                             replyfreeindex;
321
322         struct resource                 *mps_regs_resource;
323         bus_space_handle_t              mps_bhandle;
324         bus_space_tag_t                 mps_btag;
325         int                             mps_regs_rid;
326
327         bus_dma_tag_t                   mps_parent_dmat;
328         bus_dma_tag_t                   buffer_dmat;
329
330         MPI2_IOC_FACTS_REPLY            *facts;
331         MPI2_PORT_FACTS_REPLY           *pfacts;
332         int                             num_reqs;
333         int                             num_replies;
334         int                             fqdepth;        /* Free queue */
335         int                             pqdepth;        /* Post queue */
336
337         uint8_t                         event_mask[16];
338         TAILQ_HEAD(, mps_event_handle)  event_list;
339         struct mps_event_handle         *mps_log_eh;
340
341         struct lock                     mps_lock;
342         struct intr_config_hook         mps_ich;
343         struct resource                 *mps_irq[MPS_MSI_COUNT];
344         void                            *mps_intrhand[MPS_MSI_COUNT];
345         int                             mps_irq_rid[MPS_MSI_COUNT];
346         int                             mps_irq_type[MPS_MSI_COUNT];
347
348         uint8_t                         *req_frames;
349         bus_addr_t                      req_busaddr;
350         bus_dma_tag_t                   req_dmat;
351         bus_dmamap_t                    req_map;
352
353         uint8_t                         *reply_frames;
354         bus_addr_t                      reply_busaddr;
355         bus_dma_tag_t                   reply_dmat;
356         bus_dmamap_t                    reply_map;
357
358         struct scsi_sense_data          *sense_frames;
359         bus_addr_t                      sense_busaddr;
360         bus_dma_tag_t                   sense_dmat;
361         bus_dmamap_t                    sense_map;
362
363         uint8_t                         *chain_frames;
364         bus_addr_t                      chain_busaddr;
365         bus_dma_tag_t                   chain_dmat;
366         bus_dmamap_t                    chain_map;
367
368         MPI2_REPLY_DESCRIPTORS_UNION    *post_queue;
369         bus_addr_t                      post_busaddr;
370         uint32_t                        *free_queue;
371         bus_addr_t                      free_busaddr;
372         bus_dma_tag_t                   queues_dmat;
373         bus_dmamap_t                    queues_map;
374
375         uint8_t                         *fw_diag_buffer;
376         bus_addr_t                      fw_diag_busaddr;
377         bus_dma_tag_t                   fw_diag_dmat;
378         bus_dmamap_t                    fw_diag_map;
379
380         uint8_t                         ir_firmware;
381
382         /* static config pages */
383         Mpi2IOCPage8_t                  ioc_pg8;
384
385         /* host mapping support */
386         struct dev_mapping_table        *mapping_table;
387         struct enc_mapping_table        *enclosure_table;
388         struct map_removal_table        *removal_table;
389         uint8_t                         *dpm_entry_used;
390         uint8_t                         *dpm_flush_entry;
391         Mpi2DriverMappingPage0_t        *dpm_pg0;
392         uint16_t                        max_devices;
393         uint16_t                        max_enclosures;
394         uint16_t                        max_expanders;
395         uint8_t                         max_volumes;
396         uint8_t                         num_enc_table_entries;
397         uint8_t                         num_rsvd_entries;
398         uint8_t                         num_channels;
399         uint16_t                        max_dpm_entries;
400         uint8_t                         is_dpm_enable;
401         uint8_t                         track_mapping_events;
402         uint32_t                        pending_map_events;
403         uint8_t                         mt_full_retry;
404         uint8_t                         mt_add_device_failed;
405
406         /* FW diag Buffer List */
407         mps_fw_diagnostic_buffer_t
408                                 fw_diag_buffer_list[MPI2_DIAG_BUF_TYPE_COUNT];
409
410         /* Event Recording IOCTL support */
411         uint32_t                        events_to_record[4];
412         mps_event_entry_t               recorded_events[MPS_EVENT_QUEUE_SIZE];
413         uint8_t                         event_index;
414         uint32_t                        event_number;
415
416         /* EEDP and TLR support */
417         uint8_t                         eedp_enabled;
418         uint8_t                         control_TLR;
419
420         /* Shutdown Event Handler */
421         eventhandler_tag                shutdown_eh;
422
423         /* To track topo events during reset */
424 #define MPS_DIAG_RESET_TIMEOUT  300000
425         uint8_t                         wait_for_port_enable;
426         uint8_t                         port_enable_complete;
427
428         /* WD controller */
429         uint8_t                         WD_valid_config;
430         uint8_t                         WD_hide_expose;
431
432         /* Direct Drive for WarpDrive */
433         uint8_t                         DD_num_phys_disks;
434         uint16_t                        DD_dev_handle;
435         uint32_t                        DD_stripe_size;
436         uint32_t                        DD_stripe_exponent;
437         uint32_t                        DD_block_size;
438         uint16_t                        DD_block_exponent;
439         uint64_t                        DD_max_lba;
440         struct mps_column_map           DD_column_map[MPS_MAX_DISKS_IN_VOL];
441 };
442
443 struct mps_config_params {
444         MPI2_CONFIG_EXT_PAGE_HEADER_UNION       hdr;
445         u_int           action;
446         u_int           page_address;   /* Attributes, not a phys address */
447         u_int           status;
448         void            *buffer;
449         u_int           length;
450         int             timeout;
451         void            (*callback)(struct mps_softc *, struct mps_config_params *);
452         void            *cbdata;
453 };
454
455 struct scsi_read_capacity_eedp
456 {
457         uint8_t addr[8];
458         uint8_t length[4];
459         uint8_t protect;
460 };
461
462 static __inline uint32_t
463 mps_regread(struct mps_softc *sc, uint32_t offset)
464 {
465         return (bus_space_read_4(sc->mps_btag, sc->mps_bhandle, offset));
466 }
467
468 static __inline void
469 mps_regwrite(struct mps_softc *sc, uint32_t offset, uint32_t val)
470 {
471         bus_space_write_4(sc->mps_btag, sc->mps_bhandle, offset, val);
472 }
473
474 static __inline void
475 mps_free_reply(struct mps_softc *sc, uint32_t busaddr)
476 {
477         if (++sc->replyfreeindex >= sc->fqdepth)
478                 sc->replyfreeindex = 0;
479         sc->free_queue[sc->replyfreeindex] = busaddr;
480         mps_regwrite(sc, MPI2_REPLY_FREE_HOST_INDEX_OFFSET, sc->replyfreeindex);
481 }
482
483 static __inline struct mps_chain *
484 mps_alloc_chain(struct mps_softc *sc)
485 {
486         struct mps_chain *chain;
487
488         if ((chain = TAILQ_FIRST(&sc->chain_list)) != NULL) {
489                 TAILQ_REMOVE(&sc->chain_list, chain, chain_link);
490                 sc->chain_free--;
491                 if (sc->chain_free < sc->chain_free_lowwater)
492                         sc->chain_free_lowwater = sc->chain_free;
493         }
494 #if __FreeBSD_version >= 900030
495         else
496                 sc->chain_alloc_fail++;
497 #endif
498         return (chain);
499 }
500
501 static __inline void
502 mps_free_chain(struct mps_softc *sc, struct mps_chain *chain)
503 {
504 #if 0
505         bzero(chain->chain, 128);
506 #endif
507         sc->chain_free++;
508         TAILQ_INSERT_TAIL(&sc->chain_list, chain, chain_link);
509 }
510
511 static __inline void
512 mps_free_command(struct mps_softc *sc, struct mps_command *cm)
513 {
514         struct mps_chain *chain, *chain_temp;
515
516         if (cm->cm_reply != NULL)
517                 mps_free_reply(sc, cm->cm_reply_data);
518         cm->cm_reply = NULL;
519         cm->cm_flags = 0;
520         cm->cm_complete = NULL;
521         cm->cm_complete_data = NULL;
522         cm->cm_ccb = NULL;
523         cm->cm_targ = NULL;
524         cm->cm_max_segs = 0;
525         cm->cm_lun = 0;
526         cm->cm_state = MPS_CM_STATE_FREE;
527         TAILQ_FOREACH_MUTABLE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
528                 TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
529                 mps_free_chain(sc, chain);
530         }
531         TAILQ_INSERT_TAIL(&sc->req_list, cm, cm_link);
532 }
533
534 static __inline struct mps_command *
535 mps_alloc_command(struct mps_softc *sc)
536 {
537         struct mps_command *cm;
538
539         cm = TAILQ_FIRST(&sc->req_list);
540         if (cm == NULL)
541                 return (NULL);
542
543         TAILQ_REMOVE(&sc->req_list, cm, cm_link);
544         KASSERT(cm->cm_state == MPS_CM_STATE_FREE, ("mps: Allocating busy command"));
545         cm->cm_state = MPS_CM_STATE_BUSY;
546         return (cm);
547 }
548
549 static __inline void
550 mps_free_high_priority_command(struct mps_softc *sc, struct mps_command *cm)
551 {
552         struct mps_chain *chain, *chain_temp;
553
554         if (cm->cm_reply != NULL)
555                 mps_free_reply(sc, cm->cm_reply_data);
556         cm->cm_reply = NULL;
557         cm->cm_flags = 0;
558         cm->cm_complete = NULL;
559         cm->cm_complete_data = NULL;
560         cm->cm_ccb = NULL;
561         cm->cm_targ = NULL;
562         cm->cm_lun = 0;
563         cm->cm_state = MPS_CM_STATE_FREE;
564         TAILQ_FOREACH_MUTABLE(chain, &cm->cm_chain_list, chain_link, chain_temp) {
565                 TAILQ_REMOVE(&cm->cm_chain_list, chain, chain_link);
566                 mps_free_chain(sc, chain);
567         }
568         TAILQ_INSERT_TAIL(&sc->high_priority_req_list, cm, cm_link);
569 }
570
571 static __inline struct mps_command *
572 mps_alloc_high_priority_command(struct mps_softc *sc)
573 {
574         struct mps_command *cm;
575
576         cm = TAILQ_FIRST(&sc->high_priority_req_list);
577         if (cm == NULL)
578                 return (NULL);
579
580         TAILQ_REMOVE(&sc->high_priority_req_list, cm, cm_link);
581         KASSERT(cm->cm_state == MPS_CM_STATE_FREE, ("mps: Allocating busy command"));
582         cm->cm_state = MPS_CM_STATE_BUSY;
583         return (cm);
584 }
585
586 static __inline void
587 mps_lock(struct mps_softc *sc)
588 {
589         lockmgr(&sc->mps_lock, LK_EXCLUSIVE);
590 }
591
592 static __inline void
593 mps_unlock(struct mps_softc *sc)
594 {
595         lockmgr(&sc->mps_lock, LK_RELEASE);
596 }
597
598 #define MPS_INFO        (1 << 0)
599 #define MPS_TRACE       (1 << 1)
600 #define MPS_FAULT       (1 << 2)
601 #define MPS_EVENT       (1 << 3)
602 #define MPS_LOG         (1 << 4)
603
604 #define mps_printf(sc, args...)                         \
605         device_printf((sc)->mps_dev, ##args)
606
607 #define mps_vprintf(sc, args...)                        \
608 do {                                                    \
609         if (bootverbose)                                \
610                 mps_printf(sc, ##args);                 \
611 } while (0)
612
613 #define mps_dprint(sc, level, msg, args...)             \
614 do {                                                    \
615         if (sc->mps_debug & level)                      \
616                 device_printf(sc->mps_dev, msg, ##args);        \
617 } while (0)
618
619 #define mps_dprint_field(sc, level, msg, args...)               \
620 do {                                                            \
621         if (sc->mps_debug & level)                              \
622                 kprintf("\t" msg, ##args);                      \
623 } while (0)
624
625 #define MPS_PRINTFIELD_START(sc, tag...)        \
626         mps_dprint((sc), MPS_INFO, ##tag);      \
627         mps_dprint_field((sc), MPS_INFO, ":\n")
628 #define MPS_PRINTFIELD_END(sc, tag)             \
629         mps_dprint((sc), MPS_INFO, tag "\n")
630 #define MPS_PRINTFIELD(sc, facts, attr, fmt)    \
631         mps_dprint_field((sc), MPS_INFO, #attr ": " #fmt "\n", (facts)->attr)
632
633 #define MPS_EVENTFIELD_START(sc, tag...)        \
634         mps_dprint((sc), MPS_EVENT, ##tag);     \
635         mps_dprint_field((sc), MPS_EVENT, ":\n")
636 #define MPS_EVENTFIELD(sc, facts, attr, fmt)    \
637         mps_dprint_field((sc), MPS_EVENT, #attr ": " #fmt "\n", (facts)->attr)
638
639 static __inline void
640 mps_from_u64(uint64_t data, U64 *mps)
641 {
642         (mps)->High = (uint32_t)((data) >> 32);
643         (mps)->Low = (uint32_t)((data) & 0xffffffff);
644 }
645
646 static __inline uint64_t
647 mps_to_u64(U64 *data)
648 {
649
650         return (((uint64_t)data->High << 32) | data->Low);
651 }
652
653 static __inline void
654 mps_mask_intr(struct mps_softc *sc)
655 {
656         uint32_t mask;
657
658         mask = mps_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
659         mask |= MPI2_HIM_REPLY_INT_MASK;
660         mps_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
661 }
662
663 static __inline void
664 mps_unmask_intr(struct mps_softc *sc)
665 {
666         uint32_t mask;
667
668         mask = mps_regread(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET);
669         mask &= ~MPI2_HIM_REPLY_INT_MASK;
670         mps_regwrite(sc, MPI2_HOST_INTERRUPT_MASK_OFFSET, mask);
671 }
672
673 int mps_pci_setup_interrupts(struct mps_softc *sc);
674 int mps_pci_restore(struct mps_softc *sc);
675
676 int mps_attach(struct mps_softc *sc);
677 int mps_free(struct mps_softc *sc);
678 void mps_intr(void *);
679 void mps_intr_msi(void *);
680 void mps_intr_locked(void *);
681 int mps_register_events(struct mps_softc *, uint8_t *, mps_evt_callback_t *,
682     void *, struct mps_event_handle **);
683 int mps_restart(struct mps_softc *);
684 int mps_update_events(struct mps_softc *, struct mps_event_handle *, uint8_t *);
685 int mps_deregister_events(struct mps_softc *, struct mps_event_handle *);
686 int mps_push_sge(struct mps_command *, void *, size_t, int);
687 int mps_add_dmaseg(struct mps_command *, vm_paddr_t, size_t, u_int, int);
688 int mps_attach_sas(struct mps_softc *sc);
689 int mps_detach_sas(struct mps_softc *sc);
690 int mps_read_config_page(struct mps_softc *, struct mps_config_params *);
691 int mps_write_config_page(struct mps_softc *, struct mps_config_params *);
692 void mps_memaddr_cb(void *, bus_dma_segment_t *, int , int );
693 void mpi_init_sge(struct mps_command *cm, void *req, void *sge);
694 int mps_attach_user(struct mps_softc *);
695 void mps_detach_user(struct mps_softc *);
696 void mpssas_record_event(struct mps_softc *sc,
697     MPI2_EVENT_NOTIFICATION_REPLY *event_reply);
698
699 int mps_map_command(struct mps_softc *sc, struct mps_command *cm);
700 int mps_wait_command(struct mps_softc *sc, struct mps_command *cm, int timeout);
701 int mps_request_polled(struct mps_softc *sc, struct mps_command *cm);
702
703 int mps_config_get_bios_pg3(struct mps_softc *sc, Mpi2ConfigReply_t
704     *mpi_reply, Mpi2BiosPage3_t *config_page);
705 int mps_config_get_raid_volume_pg0(struct mps_softc *sc, Mpi2ConfigReply_t
706     *mpi_reply, Mpi2RaidVolPage0_t *config_page, u32 page_address);
707 int mps_config_get_ioc_pg8(struct mps_softc *sc, Mpi2ConfigReply_t *,
708     Mpi2IOCPage8_t *);
709 int mps_config_get_man_pg10(struct mps_softc *sc, Mpi2ConfigReply_t *mpi_reply);
710 int mps_config_get_sas_device_pg0(struct mps_softc *, Mpi2ConfigReply_t *,
711     Mpi2SasDevicePage0_t *, u32 , u16 );
712 int mps_config_get_dpm_pg0(struct mps_softc *, Mpi2ConfigReply_t *,
713     Mpi2DriverMappingPage0_t *, u16 );
714 int mps_config_get_raid_volume_pg1(struct mps_softc *sc,
715     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidVolPage1_t *config_page, u32 form,
716     u16 handle);
717 int mps_config_get_volume_wwid(struct mps_softc *sc, u16 volume_handle,
718     u64 *wwid);
719 int mps_config_get_raid_pd_pg0(struct mps_softc *sc,
720     Mpi2ConfigReply_t *mpi_reply, Mpi2RaidPhysDiskPage0_t *config_page,
721     u32 page_address);
722 void mpssas_ir_shutdown(struct mps_softc *sc);
723
724 int mps_reinit(struct mps_softc *sc);
725 void mpssas_handle_reinit(struct mps_softc *sc);
726
727 void mps_base_static_config_pages(struct mps_softc *sc);
728 void mps_wd_config_pages(struct mps_softc *sc);
729
730 int mps_mapping_initialize(struct mps_softc *);
731 void mps_mapping_topology_change_event(struct mps_softc *,
732     Mpi2EventDataSasTopologyChangeList_t *);
733 int mps_mapping_is_reinit_required(struct mps_softc *);
734 void mps_mapping_free_memory(struct mps_softc *sc);
735 int mps_config_set_dpm_pg0(struct mps_softc *, Mpi2ConfigReply_t *,
736     Mpi2DriverMappingPage0_t *, u16 );
737 void mps_mapping_exit(struct mps_softc *);
738 void mps_mapping_check_devices(struct mps_softc *, int);
739 int mps_mapping_allocate_memory(struct mps_softc *sc);
740 unsigned int mps_mapping_get_sas_id(struct mps_softc *, uint64_t , u16);
741 unsigned int mps_mapping_get_sas_id_from_handle(struct mps_softc *sc,
742     u16 handle);
743 unsigned int mps_mapping_get_raid_id(struct mps_softc *sc, u64 wwid,
744     u16 handle);
745 unsigned int mps_mapping_get_raid_id_from_handle(struct mps_softc *sc,
746     u16 volHandle);
747 void mps_mapping_enclosure_dev_status_change_event(struct mps_softc *,
748     Mpi2EventDataSasEnclDevStatusChange_t *event_data);
749 void mps_mapping_ir_config_change_event(struct mps_softc *sc,
750     Mpi2EventDataIrConfigChangeList_t *event_data);
751
752 void mpssas_evt_handler(struct mps_softc *sc, uintptr_t data,
753     MPI2_EVENT_NOTIFICATION_REPLY *event);
754 void mpssas_prepare_remove(struct mpssas_softc *sassc, uint16_t handle);
755 int mpssas_startup(struct mps_softc *sc);
756
757 SYSCTL_DECL(_hw_mps);
758
759 #define mps_kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
760     kthread_create(func, farg, proc_ptr, fmtstr, arg)
761 #define mps_kproc_exit(arg)     kthread_exit()
762
763 #if defined(CAM_PRIORITY_XPT)
764 #define MPS_PRIORITY_XPT        CAM_PRIORITY_XPT
765 #else
766 #define MPS_PRIORITY_XPT        5
767 #endif
768 #endif