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