2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Michael Smith
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 * CISS adapter driver datastructures
35 typedef STAILQ_HEAD(, ciss_request) cr_qhead_t;
37 /************************************************************************
42 * There is no guaranteed upper bound on the number of concurrent
43 * commands an adapter may claim to support. Cap it at a reasonable
46 #define CISS_MAX_REQUESTS 1024
49 * Maximum number of logical drives we support.
50 * If the controller does not indicate a maximum
51 * value. This is a compatibiliy value to support
52 * older ciss controllers (e.g. model 6i)
54 #define CISS_MAX_LOGICAL 16
57 * Maximum number of physical devices we support.
59 #define CISS_MAX_PHYSICAL 1024
62 * Interrupt reduction can be controlled by tuning the interrupt
63 * coalesce delay and count parameters. The delay (in microseconds)
64 * defers delivery of interrupts to increase the chance of there being
65 * more than one completed command ready when the interrupt is
66 * delivered. The count expedites the delivery of the interrupt when
67 * the given number of commands are ready.
69 * If the delay is set to 0, interrupts are delivered immediately.
71 #define CISS_INTERRUPT_COALESCE_DELAY 0
72 #define CISS_INTERRUPT_COALESCE_COUNT 16
75 * Heartbeat routine timeout in seconds. Note that since event
76 * handling is performed on a callback basis, we don't need this to
79 #define CISS_HEARTBEAT_RATE 10
81 /************************************************************************
82 * Driver version. Only really significant to the ACU interface.
84 #define CISS_DRIVER_VERSION 20011201
86 /************************************************************************
87 * Driver data structures
91 * Each command issued to the adapter is managed by a request
96 STAILQ_ENTRY(ciss_request) cr_link;
97 int cr_onq; /* which queue we are on */
99 struct ciss_softc *cr_sc; /* controller softc */
100 void *cr_data; /* data buffer */
101 u_int32_t cr_length; /* data length */
102 bus_dmamap_t cr_datamap; /* DMA map for data */
103 struct ciss_command *cr_cc;
107 #define CISS_REQ_MAPPED (1<<0) /* data mapped */
108 #define CISS_REQ_SLEEP (1<<1) /* submitter sleeping */
109 #define CISS_REQ_POLL (1<<2) /* submitter polling */
110 #define CISS_REQ_DATAOUT (1<<3) /* data host->adapter */
111 #define CISS_REQ_DATAIN (1<<4) /* data adapter->host */
112 #define CISS_REQ_BUSY (1<<5) /* controller has req */
113 #define CISS_REQ_CCB (1<<6) /* data is ccb */
115 void (* cr_complete)(struct ciss_request *);
118 #define CISS_SG_MAX ((CISS_SG_FETCH_MAX << 1) | 0x1)
119 #define CISS_SG_1 ((CISS_SG_FETCH_1 << 1) | 0x01)
120 #define CISS_SG_2 ((CISS_SG_FETCH_2 << 1) | 0x01)
121 #define CISS_SG_4 ((CISS_SG_FETCH_4 << 1) | 0x01)
122 #define CISS_SG_8 ((CISS_SG_FETCH_8 << 1) | 0x01)
123 #define CISS_SG_16 ((CISS_SG_FETCH_16 << 1) | 0x01)
124 #define CISS_SG_32 ((CISS_SG_FETCH_32 << 1) | 0x01)
125 #define CISS_SG_NONE ((CISS_SG_FETCH_NONE << 1) | 0x01)
129 * The adapter command structure is defined with a zero-length
130 * scatter/gather list size. In practise, we want space for a
131 * scatter-gather list, and we also want to avoid having commands
132 * cross page boundaries.
134 * The size of the ciss_command is 52 bytes. 65 s/g elements are reserved
135 * to allow a max i/o size of 256k. This gives a total command size of
136 * 1120 bytes, including the 32 byte alignment padding. Modern controllers
137 * seem to saturate nicely at this value.
140 #define CISS_MAX_SG_ELEMENTS 65
141 #define CISS_COMMAND_ALIGN 32
142 #define CISS_COMMAND_SG_LENGTH (sizeof(struct ciss_sg_entry) * CISS_MAX_SG_ELEMENTS)
143 #define CISS_COMMAND_ALLOC_SIZE (roundup2(sizeof(struct ciss_command) + CISS_COMMAND_SG_LENGTH, CISS_COMMAND_ALIGN))
146 * Per-logical-drive data.
150 union ciss_device_address cl_address;
151 union ciss_device_address *cl_controller;
153 #define CISS_LD_NONEXISTENT 0
154 #define CISS_LD_ONLINE 1
155 #define CISS_LD_OFFLINE 2
159 struct ciss_bmic_id_ldrive *cl_ldrive;
160 struct ciss_bmic_id_lstatus *cl_lstatus;
161 struct ciss_ldrive_geometry cl_geometry;
163 char cl_name[16]; /* device name */
167 * Per-physical-drive data
171 union ciss_device_address cp_address;
175 #define CISS_PHYSICAL_SHIFT 5
176 #define CISS_PHYSICAL_BASE (1 << CISS_PHYSICAL_SHIFT)
177 #define CISS_MAX_PHYSTGT 256
179 #define CISS_IS_PHYSICAL(bus) (bus >= CISS_PHYSICAL_BASE)
180 #define CISS_CAM_TO_PBUS(bus) (bus - CISS_PHYSICAL_BASE)
187 /* bus connections */
188 device_t ciss_dev; /* bus attachment */
189 struct cdev *ciss_dev_t; /* control device */
191 struct resource *ciss_regs_resource; /* register interface window */
192 int ciss_regs_rid; /* resource ID */
193 bus_space_handle_t ciss_regs_bhandle; /* bus space handle */
194 bus_space_tag_t ciss_regs_btag; /* bus space tag */
196 struct resource *ciss_cfg_resource; /* config struct interface window */
197 int ciss_cfg_rid; /* resource ID */
198 struct ciss_config_table *ciss_cfg; /* config table in adapter memory */
199 struct ciss_perf_config *ciss_perf; /* config table for the performant */
200 struct ciss_bmic_id_table *ciss_id; /* ID table in host memory */
201 u_int32_t ciss_heartbeat; /* last heartbeat value */
202 int ciss_heart_attack; /* number of times we have seen this value */
205 struct resource *ciss_irq_resource; /* interrupt */
206 int ciss_irq_rid[CISS_MSI_COUNT]; /* resource ID */
207 void *ciss_intr; /* interrupt handle */
209 bus_dma_tag_t ciss_parent_dmat; /* parent DMA tag */
210 bus_dma_tag_t ciss_buffer_dmat; /* data buffer/command DMA tag */
212 u_int32_t ciss_interrupt_mask; /* controller interrupt mask bits */
214 uint64_t *ciss_reply;
217 bus_dma_tag_t ciss_reply_dmat;
218 bus_dmamap_t ciss_reply_map;
219 uint32_t ciss_reply_phys;
221 int ciss_max_requests;
222 struct ciss_request ciss_request[CISS_MAX_REQUESTS]; /* requests */
223 void *ciss_command; /* command structures */
224 bus_dma_tag_t ciss_command_dmat; /* command DMA tag */
225 bus_dmamap_t ciss_command_map; /* command DMA map */
226 u_int32_t ciss_command_phys; /* command array base address */
227 cr_qhead_t ciss_free; /* requests available for reuse */
228 cr_qhead_t ciss_notify; /* requests which are defered for processing */
229 struct proc *ciss_notify_thread;
231 struct callout ciss_periodic; /* periodic event handling */
232 struct ciss_request *ciss_periodic_notify; /* notify callback request */
235 struct ciss_ldrive **ciss_logical;
236 struct ciss_pdrive **ciss_physical;
237 union ciss_device_address *ciss_controllers; /* controller address */
238 int ciss_max_bus_number; /* maximum bus number */
239 int ciss_max_logical_bus;
240 int ciss_max_physical_bus;
242 struct cam_devq *ciss_cam_devq;
243 struct cam_sim **ciss_cam_sim;
248 #define CISS_FLAG_NOTIFY_OK (1<<0) /* notify command running OK */
249 #define CISS_FLAG_CONTROL_OPEN (1<<1) /* control device is open */
250 #define CISS_FLAG_ABORTING (1<<2) /* driver is going away */
251 #define CISS_FLAG_RUNNING (1<<3) /* driver is running (interrupts usable) */
252 #define CISS_FLAG_BUSY (1<<4) /* no free commands */
254 #define CISS_FLAG_FAKE_SYNCH (1<<16) /* needs SYNCHRONISE_CACHE faked */
255 #define CISS_FLAG_BMIC_ABORT (1<<17) /* use BMIC command to abort Notify on Event */
256 #define CISS_FLAG_THREAD_SHUT (1<<20) /* shutdown the kthread */
258 struct ciss_qstat ciss_qstat[CISSQ_COUNT]; /* queue statistics */
261 /************************************************************************
262 * Debugging/diagnostic output.
267 * 0 - quiet, only emit warnings
268 * 1 - talkative, log major events, but nothing on the I/O path
269 * 2 - noisy, log events on the I/O path
270 * 3 - extremely noisy, log items in loops
273 # define debug(level, fmt, args...) \
275 if (level <= CISS_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
277 # define debug_called(level) \
279 if (level <= CISS_DEBUG) printf("%s: called\n", __func__); \
281 # define debug_struct(s) printf(" SIZE %s: %zu\n", #s, sizeof(struct s))
282 # define debug_union(s) printf(" SIZE %s: %zu\n", #s, sizeof(union s))
283 # define debug_type(s) printf(" SIZE %s: %zu\n", #s, sizeof(s))
284 # define debug_field(s, f) printf(" OFFSET %s.%s: %d\n", #s, #f, ((int)&(((struct s *)0)->f)))
285 # define debug_const(c) printf(" CONST %s %jd/0x%jx\n", #c, (intmax_t)c, (intmax_t)c);
287 # define debug(level, fmt, args...)
288 # define debug_called(level)
289 # define debug_struct(s)
290 # define debug_union(s)
291 # define debug_type(s)
292 # define debug_field(s, f)
293 # define debug_const(c)
296 #define ciss_printf(sc, fmt, args...) device_printf(sc->ciss_dev, fmt , ##args)
298 /************************************************************************
302 #define CISSQ_ADD(sc, qname) \
304 struct ciss_qstat *qs = &(sc)->ciss_qstat[qname]; \
307 if (qs->q_length > qs->q_max) \
308 qs->q_max = qs->q_length; \
311 #define CISSQ_REMOVE(sc, qname) (sc)->ciss_qstat[qname].q_length--
312 #define CISSQ_INIT(sc, qname) \
314 sc->ciss_qstat[qname].q_length = 0; \
315 sc->ciss_qstat[qname].q_max = 0; \
319 #define CISSQ_REQUEST_QUEUE(name, index) \
320 static __inline void \
321 ciss_initq_ ## name (struct ciss_softc *sc) \
323 STAILQ_INIT(&sc->ciss_ ## name); \
324 CISSQ_INIT(sc, index); \
326 static __inline void \
327 ciss_enqueue_ ## name (struct ciss_request *cr) \
330 STAILQ_INSERT_TAIL(&cr->cr_sc->ciss_ ## name, cr, cr_link); \
331 CISSQ_ADD(cr->cr_sc, index); \
332 cr->cr_onq = index; \
334 static __inline void \
335 ciss_requeue_ ## name (struct ciss_request *cr) \
338 STAILQ_INSERT_HEAD(&cr->cr_sc->ciss_ ## name, cr, cr_link); \
339 CISSQ_ADD(cr->cr_sc, index); \
340 cr->cr_onq = index; \
342 static __inline struct ciss_request * \
343 ciss_dequeue_ ## name (struct ciss_softc *sc) \
345 struct ciss_request *cr; \
347 if ((cr = STAILQ_FIRST(&sc->ciss_ ## name)) != NULL) { \
348 STAILQ_REMOVE_HEAD(&sc->ciss_ ## name, cr_link); \
349 CISSQ_REMOVE(sc, index); \
356 CISSQ_REQUEST_QUEUE(free, CISSQ_FREE);
357 CISSQ_REQUEST_QUEUE(notify, CISSQ_NOTIFY);
360 ciss_enqueue_complete(struct ciss_request *ac, cr_qhead_t *head)
363 STAILQ_INSERT_TAIL(head, ac, cr_link);
366 static __inline struct ciss_request *
367 ciss_dequeue_complete(struct ciss_softc *sc, cr_qhead_t *head)
369 struct ciss_request *ac;
371 if ((ac = STAILQ_FIRST(head)) != NULL)
372 STAILQ_REMOVE_HEAD(head, cr_link);
376 /********************************************************************************
377 * space-fill a character string
380 padstr(char *targ, const char *src, int len)
391 #define ciss_report_request(a, b, c) \
392 _ciss_report_request(a, b, c, __FUNCTION__)