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