/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2001 Michael Smith4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29* CISS adapter driver datastructures30*/3132typedef STAILQ_HEAD(, ciss_request) cr_qhead_t;3334/************************************************************************35* Tunable parameters36*/3738/*39* There is no guaranteed upper bound on the number of concurrent40* commands an adapter may claim to support. Cap it at a reasonable41* value.42*/43#define CISS_MAX_REQUESTS 10244445/*46* Maximum number of logical drives we support.47* If the controller does not indicate a maximum48* value. This is a compatibiliy value to support49* older ciss controllers (e.g. model 6i)50*/51#define CISS_MAX_LOGICAL 165253/*54* Maximum number of physical devices we support.55*/56#define CISS_MAX_PHYSICAL 10245758/*59* Interrupt reduction can be controlled by tuning the interrupt60* coalesce delay and count parameters. The delay (in microseconds)61* defers delivery of interrupts to increase the chance of there being62* more than one completed command ready when the interrupt is63* delivered. The count expedites the delivery of the interrupt when64* the given number of commands are ready.65*66* If the delay is set to 0, interrupts are delivered immediately.67*/68#define CISS_INTERRUPT_COALESCE_DELAY 069#define CISS_INTERRUPT_COALESCE_COUNT 167071/*72* Heartbeat routine timeout in seconds. Note that since event73* handling is performed on a callback basis, we don't need this to74* run very often.75*/76#define CISS_HEARTBEAT_RATE 107778/************************************************************************79* Driver version. Only really significant to the ACU interface.80*/81#define CISS_DRIVER_VERSION 200112018283/************************************************************************84* Driver data structures85*/8687/*88* Each command issued to the adapter is managed by a request89* structure.90*/91struct ciss_request92{93STAILQ_ENTRY(ciss_request) cr_link;94int cr_onq; /* which queue we are on */9596struct ciss_softc *cr_sc; /* controller softc */97void *cr_data; /* data buffer */98u_int32_t cr_length; /* data length */99bus_dmamap_t cr_datamap; /* DMA map for data */100struct ciss_command *cr_cc;101uint32_t cr_ccphys;102int cr_tag;103int cr_flags;104#define CISS_REQ_MAPPED (1<<0) /* data mapped */105#define CISS_REQ_SLEEP (1<<1) /* submitter sleeping */106#define CISS_REQ_POLL (1<<2) /* submitter polling */107#define CISS_REQ_DATAOUT (1<<3) /* data host->adapter */108#define CISS_REQ_DATAIN (1<<4) /* data adapter->host */109#define CISS_REQ_BUSY (1<<5) /* controller has req */110#define CISS_REQ_CCB (1<<6) /* data is ccb */111112void (* cr_complete)(struct ciss_request *);113void *cr_private;114int cr_sg_tag;115#define CISS_SG_MAX ((CISS_SG_FETCH_MAX << 1) | 0x1)116#define CISS_SG_1 ((CISS_SG_FETCH_1 << 1) | 0x01)117#define CISS_SG_2 ((CISS_SG_FETCH_2 << 1) | 0x01)118#define CISS_SG_4 ((CISS_SG_FETCH_4 << 1) | 0x01)119#define CISS_SG_8 ((CISS_SG_FETCH_8 << 1) | 0x01)120#define CISS_SG_16 ((CISS_SG_FETCH_16 << 1) | 0x01)121#define CISS_SG_32 ((CISS_SG_FETCH_32 << 1) | 0x01)122#define CISS_SG_NONE ((CISS_SG_FETCH_NONE << 1) | 0x01)123};124125/*126* The adapter command structure is defined with a zero-length127* scatter/gather list size. In practise, we want space for a128* scatter-gather list, and we also want to avoid having commands129* cross page boundaries.130*131* The size of the ciss_command is 52 bytes. 65 s/g elements are reserved132* to allow a max i/o size of 256k. This gives a total command size of133* 1120 bytes, including the 32 byte alignment padding. Modern controllers134* seem to saturate nicely at this value.135*/136137#define CISS_MAX_SG_ELEMENTS 65138#define CISS_COMMAND_ALIGN 32139#define CISS_COMMAND_SG_LENGTH (sizeof(struct ciss_sg_entry) * CISS_MAX_SG_ELEMENTS)140#define CISS_COMMAND_ALLOC_SIZE (roundup2(sizeof(struct ciss_command) + CISS_COMMAND_SG_LENGTH, CISS_COMMAND_ALIGN))141142/*143* Per-logical-drive data.144*/145struct ciss_ldrive146{147union ciss_device_address cl_address;148union ciss_device_address *cl_controller;149int cl_status;150#define CISS_LD_NONEXISTENT 0151#define CISS_LD_ONLINE 1152#define CISS_LD_OFFLINE 2153154int cl_update;155156struct ciss_bmic_id_ldrive *cl_ldrive;157struct ciss_bmic_id_lstatus *cl_lstatus;158struct ciss_ldrive_geometry cl_geometry;159160char cl_name[16]; /* device name */161};162163/*164* Per-physical-drive data165*/166struct ciss_pdrive167{168union ciss_device_address cp_address;169int cp_online;170};171172#define CISS_PHYSICAL_SHIFT 5173#define CISS_PHYSICAL_BASE (1 << CISS_PHYSICAL_SHIFT)174#define CISS_MAX_PHYSTGT 256175176#define CISS_IS_PHYSICAL(bus) (bus >= CISS_PHYSICAL_BASE)177#define CISS_CAM_TO_PBUS(bus) (bus - CISS_PHYSICAL_BASE)178179/*180* Per-adapter data181*/182struct ciss_softc183{184/* bus connections */185device_t ciss_dev; /* bus attachment */186struct cdev *ciss_dev_t; /* control device */187188struct resource *ciss_regs_resource; /* register interface window */189int ciss_regs_rid; /* resource ID */190bus_space_handle_t ciss_regs_bhandle; /* bus space handle */191bus_space_tag_t ciss_regs_btag; /* bus space tag */192193struct resource *ciss_cfg_resource; /* config struct interface window */194int ciss_cfg_rid; /* resource ID */195struct ciss_config_table *ciss_cfg; /* config table in adapter memory */196struct ciss_perf_config *ciss_perf; /* config table for the performant */197struct ciss_bmic_id_table *ciss_id; /* ID table in host memory */198u_int32_t ciss_heartbeat; /* last heartbeat value */199int ciss_heart_attack; /* number of times we have seen this value */200201int ciss_msi;202struct resource *ciss_irq_resource; /* interrupt */203int ciss_irq_rid[CISS_MSI_COUNT]; /* resource ID */204void *ciss_intr; /* interrupt handle */205206bus_dma_tag_t ciss_parent_dmat; /* parent DMA tag */207bus_dma_tag_t ciss_buffer_dmat; /* data buffer/command DMA tag */208209u_int32_t ciss_interrupt_mask; /* controller interrupt mask bits */210211uint64_t *ciss_reply;212int ciss_cycle;213int ciss_rqidx;214bus_dma_tag_t ciss_reply_dmat;215bus_dmamap_t ciss_reply_map;216uint32_t ciss_reply_phys;217218int ciss_max_requests;219struct ciss_request ciss_request[CISS_MAX_REQUESTS]; /* requests */220void *ciss_command; /* command structures */221bus_dma_tag_t ciss_command_dmat; /* command DMA tag */222bus_dmamap_t ciss_command_map; /* command DMA map */223u_int32_t ciss_command_phys; /* command array base address */224cr_qhead_t ciss_free; /* requests available for reuse */225cr_qhead_t ciss_notify; /* requests which are defered for processing */226struct proc *ciss_notify_thread;227228struct callout ciss_periodic; /* periodic event handling */229struct ciss_request *ciss_periodic_notify; /* notify callback request */230231struct mtx ciss_mtx;232struct ciss_ldrive **ciss_logical;233struct ciss_pdrive **ciss_physical;234union ciss_device_address *ciss_controllers; /* controller address */235int ciss_max_bus_number; /* maximum bus number */236int ciss_max_logical_bus;237int ciss_max_physical_bus;238int ciss_max_physical_target; /* highest physical target number */239240struct cam_devq *ciss_cam_devq;241struct cam_sim **ciss_cam_sim;242243int ciss_soft_reset;244245int ciss_flags;246#define CISS_FLAG_NOTIFY_OK (1<<0) /* notify command running OK */247#define CISS_FLAG_CONTROL_OPEN (1<<1) /* control device is open */248#define CISS_FLAG_ABORTING (1<<2) /* driver is going away */249#define CISS_FLAG_RUNNING (1<<3) /* driver is running (interrupts usable) */250#define CISS_FLAG_BUSY (1<<4) /* no free commands */251252#define CISS_FLAG_FAKE_SYNCH (1<<16) /* needs SYNCHRONISE_CACHE faked */253#define CISS_FLAG_BMIC_ABORT (1<<17) /* use BMIC command to abort Notify on Event */254#define CISS_FLAG_THREAD_SHUT (1<<20) /* shutdown the kthread */255256struct ciss_qstat ciss_qstat[CISSQ_COUNT]; /* queue statistics */257};258259/************************************************************************260* Debugging/diagnostic output.261*/262263/*264* Debugging levels:265* 0 - quiet, only emit warnings266* 1 - talkative, log major events, but nothing on the I/O path267* 2 - noisy, log events on the I/O path268* 3 - extremely noisy, log items in loops269*/270#ifdef CISS_DEBUG271# define debug(level, fmt, args...) \272do { \273if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \274} while(0)275# define debug_called(level) \276do { \277if (level <= CISS_DEBUG) printf("%s: called\n", __func__); \278} while(0)279# define debug_struct(s) printf(" SIZE %s: %zu\n", #s, sizeof(struct s))280# define debug_union(s) printf(" SIZE %s: %zu\n", #s, sizeof(union s))281# define debug_type(s) printf(" SIZE %s: %zu\n", #s, sizeof(s))282# define debug_field(s, f) printf(" OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))283# define debug_const(c) printf(" CONST %s %jd/0x%jx\n", #c, (intmax_t)c, (intmax_t)c);284#else285# define debug(level, fmt, args...)286# define debug_called(level)287# define debug_struct(s)288# define debug_union(s)289# define debug_type(s)290# define debug_field(s, f)291# define debug_const(c)292#endif293294#define ciss_printf(sc, fmt, args...) device_printf(sc->ciss_dev, fmt , ##args)295296/************************************************************************297* Queue primitives298*/299300#define CISSQ_ADD(sc, qname) \301do { \302struct ciss_qstat *qs = &(sc)->ciss_qstat[qname]; \303\304qs->q_length++; \305if (qs->q_length > qs->q_max) \306qs->q_max = qs->q_length; \307} while(0)308309#define CISSQ_REMOVE(sc, qname) (sc)->ciss_qstat[qname].q_length--310#define CISSQ_INIT(sc, qname) \311do { \312sc->ciss_qstat[qname].q_length = 0; \313sc->ciss_qstat[qname].q_max = 0; \314} while(0)315316#define CISSQ_REQUEST_QUEUE(name, index) \317static __inline void \318ciss_initq_ ## name (struct ciss_softc *sc) \319{ \320STAILQ_INIT(&sc->ciss_ ## name); \321CISSQ_INIT(sc, index); \322} \323static __inline void \324ciss_enqueue_ ## name (struct ciss_request *cr) \325{ \326\327STAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link); \328CISSQ_ADD(cr->cr_sc, index); \329cr->cr_onq = index; \330} \331static __inline void \332ciss_requeue_ ## name (struct ciss_request *cr) \333{ \334\335STAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link); \336CISSQ_ADD(cr->cr_sc, index); \337cr->cr_onq = index; \338} \339static __inline struct ciss_request * \340ciss_dequeue_ ## name (struct ciss_softc *sc) \341{ \342struct ciss_request *cr; \343\344if ((cr = STAILQ_FIRST(&sc->ciss_ ## name)) != NULL) { \345STAILQ_REMOVE_HEAD(&sc->ciss_ ## name, cr_link); \346CISSQ_REMOVE(sc, index); \347cr->cr_onq = -1; \348} \349return(cr); \350} \351struct hack352353CISSQ_REQUEST_QUEUE(free, CISSQ_FREE);354CISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY);355356static __inline void357ciss_enqueue_complete(struct ciss_request *ac, cr_qhead_t *head)358{359360STAILQ_INSERT_TAIL(head, ac, cr_link);361}362363static __inline struct ciss_request *364ciss_dequeue_complete(struct ciss_softc *sc, cr_qhead_t *head)365{366struct ciss_request *ac;367368if ((ac = STAILQ_FIRST(head)) != NULL)369STAILQ_REMOVE_HEAD(head, cr_link);370return(ac);371}372373/********************************************************************************374* space-fill a character string375*/376static __inline void377padstr(char *targ, const char *src, int len)378{379while (len-- > 0) {380if (*src != 0) {381*targ++ = *src++;382} else {383*targ++ = ' ';384}385}386}387388#define ciss_report_request(a, b, c) \389_ciss_report_request(a, b, c, __FUNCTION__)390391392