mpii.c revision 1.26 1 1.26 skrll /* $NetBSD: mpii.c,v 1.26 2021/02/23 07:15:30 skrll Exp $ */
2 1.15 bouyer /* $OpenBSD: mpii.c,v 1.115 2018/08/14 05:22:21 jmatthew Exp $ */
3 1.1 bouyer /*
4 1.15 bouyer * Copyright (c) 2010, 2012 Mike Belopuhov
5 1.1 bouyer * Copyright (c) 2009 James Giannoules
6 1.1 bouyer * Copyright (c) 2005 - 2010 David Gwynne <dlg (at) openbsd.org>
7 1.1 bouyer * Copyright (c) 2005 - 2010 Marco Peereboom <marco (at) openbsd.org>
8 1.1 bouyer *
9 1.1 bouyer * Permission to use, copy, modify, and distribute this software for any
10 1.1 bouyer * purpose with or without fee is hereby granted, provided that the above
11 1.1 bouyer * copyright notice and this permission notice appear in all copies.
12 1.1 bouyer *
13 1.1 bouyer * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 1.1 bouyer * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 1.1 bouyer * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 1.1 bouyer * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 1.1 bouyer * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 1.1 bouyer * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 1.1 bouyer * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 1.1 bouyer */
21 1.1 bouyer
22 1.1 bouyer #include <sys/cdefs.h>
23 1.26 skrll __KERNEL_RCSID(0, "$NetBSD: mpii.c,v 1.26 2021/02/23 07:15:30 skrll Exp $");
24 1.1 bouyer
25 1.1 bouyer #include "bio.h"
26 1.1 bouyer
27 1.1 bouyer #include <sys/param.h>
28 1.1 bouyer #include <sys/systm.h>
29 1.1 bouyer #include <sys/buf.h>
30 1.1 bouyer #include <sys/device.h>
31 1.1 bouyer #include <sys/ioctl.h>
32 1.1 bouyer #include <sys/malloc.h>
33 1.1 bouyer #include <sys/kernel.h>
34 1.1 bouyer #include <sys/mutex.h>
35 1.1 bouyer #include <sys/condvar.h>
36 1.1 bouyer #include <sys/dkio.h>
37 1.1 bouyer #include <sys/tree.h>
38 1.1 bouyer
39 1.1 bouyer #include <dev/pci/pcireg.h>
40 1.1 bouyer #include <dev/pci/pcivar.h>
41 1.1 bouyer #include <dev/pci/pcidevs.h>
42 1.1 bouyer
43 1.1 bouyer #include <dev/scsipi/scsipi_all.h>
44 1.1 bouyer #include <dev/scsipi/scsi_all.h>
45 1.1 bouyer #include <dev/scsipi/scsiconf.h>
46 1.1 bouyer
47 1.13 bouyer #if NBIO > 0
48 1.1 bouyer #include <dev/biovar.h>
49 1.15 bouyer #include <dev/sysmon/sysmonvar.h>
50 1.1 bouyer #include <sys/envsys.h>
51 1.13 bouyer #endif
52 1.1 bouyer
53 1.15 bouyer #include <dev/pci/mpiireg.h>
54 1.15 bouyer
55 1.15 bouyer // #define MPII_DEBUG
56 1.1 bouyer #ifdef MPII_DEBUG
57 1.1 bouyer #define DPRINTF(x...) do { if (mpii_debug) printf(x); } while(0)
58 1.1 bouyer #define DNPRINTF(n,x...) do { if (mpii_debug & (n)) printf(x); } while(0)
59 1.1 bouyer #define MPII_D_CMD (0x0001)
60 1.1 bouyer #define MPII_D_INTR (0x0002)
61 1.1 bouyer #define MPII_D_MISC (0x0004)
62 1.1 bouyer #define MPII_D_DMA (0x0008)
63 1.1 bouyer #define MPII_D_IOCTL (0x0010)
64 1.1 bouyer #define MPII_D_RW (0x0020)
65 1.1 bouyer #define MPII_D_MEM (0x0040)
66 1.1 bouyer #define MPII_D_CCB (0x0080)
67 1.1 bouyer #define MPII_D_PPR (0x0100)
68 1.1 bouyer #define MPII_D_RAID (0x0200)
69 1.1 bouyer #define MPII_D_EVT (0x0400)
70 1.1 bouyer #define MPII_D_CFG (0x0800)
71 1.1 bouyer #define MPII_D_MAP (0x1000)
72 1.15 bouyer
73 1.1 bouyer u_int32_t mpii_debug = 0
74 1.15 bouyer // | MPII_D_CMD
75 1.15 bouyer // | MPII_D_INTR
76 1.15 bouyer // | MPII_D_MISC
77 1.15 bouyer // | MPII_D_DMA
78 1.15 bouyer // | MPII_D_IOCTL
79 1.15 bouyer // | MPII_D_RW
80 1.15 bouyer // | MPII_D_MEM
81 1.15 bouyer // | MPII_D_CCB
82 1.15 bouyer // | MPII_D_PPR
83 1.15 bouyer // | MPII_D_RAID
84 1.15 bouyer // | MPII_D_EVT
85 1.15 bouyer // | MPII_D_CFG
86 1.15 bouyer // | MPII_D_MAP
87 1.1 bouyer ;
88 1.1 bouyer #else
89 1.1 bouyer #define DPRINTF(x...)
90 1.1 bouyer #define DNPRINTF(n,x...)
91 1.1 bouyer #endif
92 1.1 bouyer
93 1.15 bouyer #define MPII_REQUEST_SIZE (512)
94 1.15 bouyer #define MPII_REQUEST_CREDIT (128)
95 1.1 bouyer
96 1.1 bouyer struct mpii_dmamem {
97 1.1 bouyer bus_dmamap_t mdm_map;
98 1.1 bouyer bus_dma_segment_t mdm_seg;
99 1.1 bouyer size_t mdm_size;
100 1.1 bouyer void *mdm_kva;
101 1.1 bouyer };
102 1.15 bouyer #define MPII_DMA_MAP(_mdm) ((_mdm)->mdm_map)
103 1.15 bouyer #define MPII_DMA_DVA(_mdm) ((uint64_t)(_mdm)->mdm_map->dm_segs[0].ds_addr)
104 1.15 bouyer #define MPII_DMA_KVA(_mdm) ((_mdm)->mdm_kva)
105 1.1 bouyer
106 1.1 bouyer struct mpii_softc;
107 1.1 bouyer
108 1.1 bouyer struct mpii_rcb {
109 1.15 bouyer SIMPLEQ_ENTRY(mpii_rcb) rcb_link;
110 1.1 bouyer void *rcb_reply;
111 1.1 bouyer u_int32_t rcb_reply_dva;
112 1.1 bouyer };
113 1.1 bouyer
114 1.1 bouyer SIMPLEQ_HEAD(mpii_rcb_list, mpii_rcb);
115 1.1 bouyer
116 1.1 bouyer struct mpii_device {
117 1.1 bouyer int flags;
118 1.1 bouyer #define MPII_DF_ATTACH (0x0001)
119 1.1 bouyer #define MPII_DF_DETACH (0x0002)
120 1.1 bouyer #define MPII_DF_HIDDEN (0x0004)
121 1.1 bouyer #define MPII_DF_UNUSED (0x0008)
122 1.1 bouyer #define MPII_DF_VOLUME (0x0010)
123 1.1 bouyer #define MPII_DF_VOLUME_DISK (0x0020)
124 1.1 bouyer #define MPII_DF_HOT_SPARE (0x0040)
125 1.1 bouyer short slot;
126 1.1 bouyer short percent;
127 1.1 bouyer u_int16_t dev_handle;
128 1.1 bouyer u_int16_t enclosure;
129 1.1 bouyer u_int16_t expander;
130 1.1 bouyer u_int8_t phy_num;
131 1.1 bouyer u_int8_t physical_port;
132 1.1 bouyer };
133 1.1 bouyer
134 1.1 bouyer struct mpii_ccb {
135 1.1 bouyer struct mpii_softc *ccb_sc;
136 1.1 bouyer
137 1.1 bouyer void * ccb_cookie;
138 1.15 bouyer kmutex_t ccb_mtx;
139 1.15 bouyer kcondvar_t ccb_cv;
140 1.15 bouyer
141 1.1 bouyer bus_dmamap_t ccb_dmamap;
142 1.1 bouyer
143 1.1 bouyer bus_addr_t ccb_offset;
144 1.1 bouyer void *ccb_cmd;
145 1.1 bouyer bus_addr_t ccb_cmd_dva;
146 1.1 bouyer u_int16_t ccb_dev_handle;
147 1.15 bouyer u_int16_t ccb_smid;
148 1.1 bouyer
149 1.1 bouyer volatile enum {
150 1.1 bouyer MPII_CCB_FREE,
151 1.1 bouyer MPII_CCB_READY,
152 1.1 bouyer MPII_CCB_QUEUED,
153 1.1 bouyer MPII_CCB_TIMEOUT
154 1.1 bouyer } ccb_state;
155 1.1 bouyer
156 1.1 bouyer void (*ccb_done)(struct mpii_ccb *);
157 1.1 bouyer struct mpii_rcb *ccb_rcb;
158 1.1 bouyer
159 1.15 bouyer SIMPLEQ_ENTRY(mpii_ccb) ccb_link;
160 1.1 bouyer };
161 1.1 bouyer
162 1.1 bouyer SIMPLEQ_HEAD(mpii_ccb_list, mpii_ccb);
163 1.1 bouyer
164 1.1 bouyer struct mpii_softc {
165 1.1 bouyer device_t sc_dev;
166 1.1 bouyer
167 1.1 bouyer pci_chipset_tag_t sc_pc;
168 1.1 bouyer pcitag_t sc_tag;
169 1.1 bouyer
170 1.1 bouyer void *sc_ih;
171 1.15 bouyer pci_intr_handle_t *sc_pihp;
172 1.1 bouyer
173 1.1 bouyer struct scsipi_adapter sc_adapt;
174 1.1 bouyer struct scsipi_channel sc_chan;
175 1.1 bouyer device_t sc_child; /* our scsibus */
176 1.1 bouyer
177 1.15 bouyer int sc_flags;
178 1.15 bouyer #define MPII_F_RAID (1<<1)
179 1.15 bouyer #define MPII_F_SAS3 (1<<2)
180 1.15 bouyer
181 1.1 bouyer struct mpii_device **sc_devs;
182 1.15 bouyer kmutex_t sc_devs_mtx;
183 1.1 bouyer
184 1.1 bouyer bus_space_tag_t sc_iot;
185 1.1 bouyer bus_space_handle_t sc_ioh;
186 1.1 bouyer bus_size_t sc_ios;
187 1.1 bouyer bus_dma_tag_t sc_dmat;
188 1.1 bouyer
189 1.1 bouyer kmutex_t sc_req_mtx;
190 1.1 bouyer kmutex_t sc_rep_mtx;
191 1.1 bouyer
192 1.15 bouyer ushort sc_reply_size;
193 1.15 bouyer ushort sc_request_size;
194 1.15 bouyer
195 1.15 bouyer ushort sc_max_cmds;
196 1.15 bouyer ushort sc_num_reply_frames;
197 1.15 bouyer u_int sc_reply_free_qdepth;
198 1.15 bouyer u_int sc_reply_post_qdepth;
199 1.15 bouyer
200 1.15 bouyer ushort sc_chain_sge;
201 1.15 bouyer ushort sc_max_sgl;
202 1.1 bouyer
203 1.1 bouyer u_int8_t sc_ioc_event_replay;
204 1.15 bouyer
205 1.15 bouyer u_int8_t sc_porttype;
206 1.1 bouyer u_int8_t sc_max_volumes;
207 1.1 bouyer u_int16_t sc_max_devices;
208 1.1 bouyer u_int16_t sc_vd_count;
209 1.1 bouyer u_int16_t sc_vd_id_low;
210 1.1 bouyer u_int16_t sc_pd_id_start;
211 1.1 bouyer int sc_ioc_number;
212 1.1 bouyer u_int8_t sc_vf_id;
213 1.1 bouyer
214 1.1 bouyer struct mpii_ccb *sc_ccbs;
215 1.1 bouyer struct mpii_ccb_list sc_ccb_free;
216 1.1 bouyer kmutex_t sc_ccb_free_mtx;
217 1.1 bouyer kcondvar_t sc_ccb_free_cv;
218 1.1 bouyer
219 1.15 bouyer struct mpii_ccb_list sc_ccb_tmos;
220 1.15 bouyer kmutex_t sc_ssb_tmomtx;
221 1.1 bouyer struct workqueue *sc_ssb_tmowk;
222 1.15 bouyer struct work sc_ssb_tmowork;
223 1.1 bouyer
224 1.1 bouyer struct mpii_dmamem *sc_requests;
225 1.1 bouyer
226 1.1 bouyer struct mpii_dmamem *sc_replies;
227 1.1 bouyer struct mpii_rcb *sc_rcbs;
228 1.1 bouyer
229 1.1 bouyer struct mpii_dmamem *sc_reply_postq;
230 1.1 bouyer struct mpii_reply_descr *sc_reply_postq_kva;
231 1.15 bouyer u_int sc_reply_post_host_index;
232 1.1 bouyer
233 1.1 bouyer struct mpii_dmamem *sc_reply_freeq;
234 1.15 bouyer u_int sc_reply_free_host_index;
235 1.15 bouyer kmutex_t sc_reply_free_mtx;
236 1.1 bouyer
237 1.15 bouyer struct mpii_rcb_list sc_evt_sas_queue;
238 1.15 bouyer kmutex_t sc_evt_sas_mtx;
239 1.15 bouyer struct workqueue *sc_evt_sas_wq;
240 1.15 bouyer struct work sc_evt_sas_work;
241 1.15 bouyer
242 1.15 bouyer struct mpii_rcb_list sc_evt_ack_queue;
243 1.15 bouyer kmutex_t sc_evt_ack_mtx;
244 1.15 bouyer struct workqueue *sc_evt_ack_wq;
245 1.15 bouyer struct work sc_evt_ack_work;
246 1.1 bouyer
247 1.25 jnemeth #if NBIO > 0
248 1.1 bouyer struct sysmon_envsys *sc_sme;
249 1.1 bouyer envsys_data_t *sc_sensors;
250 1.25 jnemeth #endif
251 1.1 bouyer };
252 1.1 bouyer
253 1.24 maxv static int mpii_match(device_t, cfdata_t, void *);
254 1.24 maxv static void mpii_attach(device_t, device_t, void *);
255 1.24 maxv static int mpii_detach(device_t, int);
256 1.24 maxv static void mpii_childdetached(device_t, device_t);
257 1.24 maxv static int mpii_rescan(device_t, const char *, const int *);
258 1.1 bouyer
259 1.24 maxv static int mpii_intr(void *);
260 1.1 bouyer
261 1.1 bouyer CFATTACH_DECL3_NEW(mpii, sizeof(struct mpii_softc),
262 1.1 bouyer mpii_match, mpii_attach, mpii_detach, NULL, mpii_rescan,
263 1.1 bouyer mpii_childdetached, DVF_DETACH_SHUTDOWN);
264 1.1 bouyer
265 1.24 maxv static void mpii_scsipi_request(struct scsipi_channel *,
266 1.15 bouyer scsipi_adapter_req_t, void *);
267 1.24 maxv static void mpii_scsi_cmd_done(struct mpii_ccb *);
268 1.15 bouyer
269 1.24 maxv static struct mpii_dmamem *
270 1.15 bouyer mpii_dmamem_alloc(struct mpii_softc *, size_t);
271 1.24 maxv static void mpii_dmamem_free(struct mpii_softc *,
272 1.15 bouyer struct mpii_dmamem *);
273 1.24 maxv static int mpii_alloc_ccbs(struct mpii_softc *);
274 1.24 maxv static struct mpii_ccb *mpii_get_ccb(struct mpii_softc *);
275 1.24 maxv static void mpii_put_ccb(struct mpii_softc *, struct mpii_ccb *);
276 1.24 maxv static int mpii_alloc_replies(struct mpii_softc *);
277 1.24 maxv static int mpii_alloc_queues(struct mpii_softc *);
278 1.24 maxv static void mpii_push_reply(struct mpii_softc *, struct mpii_rcb *);
279 1.24 maxv static void mpii_push_replies(struct mpii_softc *);
280 1.24 maxv
281 1.24 maxv static void mpii_scsi_cmd_tmo(void *);
282 1.24 maxv static void mpii_scsi_cmd_tmo_handler(struct work *, void *);
283 1.24 maxv static void mpii_scsi_cmd_tmo_done(struct mpii_ccb *);
284 1.24 maxv
285 1.24 maxv static int mpii_insert_dev(struct mpii_softc *, struct mpii_device *);
286 1.24 maxv static int mpii_remove_dev(struct mpii_softc *, struct mpii_device *);
287 1.24 maxv static struct mpii_device *
288 1.15 bouyer mpii_find_dev(struct mpii_softc *, u_int16_t);
289 1.15 bouyer
290 1.24 maxv static void mpii_start(struct mpii_softc *, struct mpii_ccb *);
291 1.24 maxv static int mpii_poll(struct mpii_softc *, struct mpii_ccb *);
292 1.24 maxv static void mpii_poll_done(struct mpii_ccb *);
293 1.24 maxv static struct mpii_rcb *
294 1.15 bouyer mpii_reply(struct mpii_softc *, struct mpii_reply_descr *);
295 1.15 bouyer
296 1.24 maxv static void mpii_wait(struct mpii_softc *, struct mpii_ccb *);
297 1.24 maxv static void mpii_wait_done(struct mpii_ccb *);
298 1.15 bouyer
299 1.24 maxv static void mpii_init_queues(struct mpii_softc *);
300 1.15 bouyer
301 1.24 maxv static int mpii_load_xs(struct mpii_ccb *);
302 1.24 maxv static int mpii_load_xs_sas3(struct mpii_ccb *);
303 1.15 bouyer
304 1.24 maxv static u_int32_t mpii_read(struct mpii_softc *, bus_size_t);
305 1.24 maxv static void mpii_write(struct mpii_softc *, bus_size_t, u_int32_t);
306 1.24 maxv static int mpii_wait_eq(struct mpii_softc *, bus_size_t, u_int32_t,
307 1.1 bouyer u_int32_t);
308 1.24 maxv static int mpii_wait_ne(struct mpii_softc *, bus_size_t, u_int32_t,
309 1.1 bouyer u_int32_t);
310 1.1 bouyer
311 1.24 maxv static int mpii_init(struct mpii_softc *);
312 1.24 maxv static int mpii_reset_soft(struct mpii_softc *);
313 1.24 maxv static int mpii_reset_hard(struct mpii_softc *);
314 1.1 bouyer
315 1.24 maxv static int mpii_handshake_send(struct mpii_softc *, void *, size_t);
316 1.24 maxv static int mpii_handshake_recv_dword(struct mpii_softc *,
317 1.1 bouyer u_int32_t *);
318 1.24 maxv static int mpii_handshake_recv(struct mpii_softc *, void *, size_t);
319 1.1 bouyer
320 1.24 maxv static void mpii_empty_done(struct mpii_ccb *);
321 1.1 bouyer
322 1.24 maxv static int mpii_iocinit(struct mpii_softc *);
323 1.24 maxv static int mpii_iocfacts(struct mpii_softc *);
324 1.24 maxv static int mpii_portfacts(struct mpii_softc *);
325 1.24 maxv static int mpii_portenable(struct mpii_softc *);
326 1.24 maxv static int mpii_cfg_coalescing(struct mpii_softc *);
327 1.24 maxv static int mpii_board_info(struct mpii_softc *);
328 1.24 maxv static int mpii_target_map(struct mpii_softc *);
329 1.24 maxv
330 1.24 maxv static int mpii_eventnotify(struct mpii_softc *);
331 1.24 maxv static void mpii_eventnotify_done(struct mpii_ccb *);
332 1.24 maxv static void mpii_eventack(struct work *, void *);
333 1.24 maxv static void mpii_eventack_done(struct mpii_ccb *);
334 1.24 maxv static void mpii_event_process(struct mpii_softc *, struct mpii_rcb *);
335 1.24 maxv static void mpii_event_done(struct mpii_softc *, struct mpii_rcb *);
336 1.24 maxv static void mpii_event_sas(struct mpii_softc *, struct mpii_rcb *);
337 1.24 maxv static void mpii_event_sas_work(struct work *, void *);
338 1.24 maxv static void mpii_event_raid(struct mpii_softc *,
339 1.1 bouyer struct mpii_msg_event_reply *);
340 1.24 maxv static void mpii_event_discovery(struct mpii_softc *,
341 1.1 bouyer struct mpii_msg_event_reply *);
342 1.1 bouyer
343 1.24 maxv static void mpii_sas_remove_device(struct mpii_softc *, u_int16_t);
344 1.1 bouyer
345 1.24 maxv static int mpii_req_cfg_header(struct mpii_softc *, u_int8_t,
346 1.1 bouyer u_int8_t, u_int32_t, int, void *);
347 1.24 maxv static int mpii_req_cfg_page(struct mpii_softc *, u_int32_t, int,
348 1.1 bouyer void *, int, void *, size_t);
349 1.1 bouyer
350 1.1 bouyer #if 0
351 1.15 bouyer int mpii_ioctl_cache(struct scsi_link *, u_long, struct dk_cache *);
352 1.1 bouyer #endif
353 1.1 bouyer
354 1.1 bouyer #if NBIO > 0
355 1.24 maxv static int mpii_ioctl(device_t, u_long, void *);
356 1.24 maxv static int mpii_ioctl_inq(struct mpii_softc *, struct bioc_inq *);
357 1.24 maxv static int mpii_ioctl_vol(struct mpii_softc *, struct bioc_vol *);
358 1.24 maxv static int mpii_ioctl_disk(struct mpii_softc *, struct bioc_disk *);
359 1.24 maxv static int mpii_bio_hs(struct mpii_softc *, struct bioc_disk *, int,
360 1.1 bouyer int, int *);
361 1.24 maxv static int mpii_bio_disk(struct mpii_softc *, struct bioc_disk *,
362 1.1 bouyer u_int8_t);
363 1.24 maxv static struct mpii_device *
364 1.15 bouyer mpii_find_vol(struct mpii_softc *, int);
365 1.15 bouyer #ifndef SMALL_KERNEL
366 1.24 maxv static int mpii_bio_volstate(struct mpii_softc *, struct bioc_vol *);
367 1.24 maxv static int mpii_create_sensors(struct mpii_softc *);
368 1.24 maxv static void mpii_refresh_sensors(struct sysmon_envsys *, envsys_data_t *);
369 1.24 maxv static int mpii_destroy_sensors(struct mpii_softc *);
370 1.15 bouyer #endif /* SMALL_KERNEL */
371 1.1 bouyer #endif /* NBIO > 0 */
372 1.1 bouyer
373 1.15 bouyer #define DEVNAME(s) (device_xname((s)->sc_dev))
374 1.1 bouyer
375 1.1 bouyer #define dwordsof(s) (sizeof(s) / sizeof(u_int32_t))
376 1.1 bouyer
377 1.1 bouyer #define mpii_read_db(s) mpii_read((s), MPII_DOORBELL)
378 1.1 bouyer #define mpii_write_db(s, v) mpii_write((s), MPII_DOORBELL, (v))
379 1.1 bouyer #define mpii_read_intr(s) mpii_read((s), MPII_INTR_STATUS)
380 1.1 bouyer #define mpii_write_intr(s, v) mpii_write((s), MPII_INTR_STATUS, (v))
381 1.1 bouyer #define mpii_reply_waiting(s) ((mpii_read_intr((s)) & MPII_INTR_STATUS_REPLY)\
382 1.1 bouyer == MPII_INTR_STATUS_REPLY)
383 1.1 bouyer
384 1.15 bouyer #define mpii_write_reply_free(s, v) \
385 1.15 bouyer bus_space_write_4((s)->sc_iot, (s)->sc_ioh, \
386 1.15 bouyer MPII_REPLY_FREE_HOST_INDEX, (v))
387 1.15 bouyer #define mpii_write_reply_post(s, v) \
388 1.15 bouyer bus_space_write_4((s)->sc_iot, (s)->sc_ioh, \
389 1.15 bouyer MPII_REPLY_POST_HOST_INDEX, (v))
390 1.1 bouyer
391 1.1 bouyer #define mpii_wait_db_int(s) mpii_wait_ne((s), MPII_INTR_STATUS, \
392 1.1 bouyer MPII_INTR_STATUS_IOC2SYSDB, 0)
393 1.1 bouyer #define mpii_wait_db_ack(s) mpii_wait_eq((s), MPII_INTR_STATUS, \
394 1.1 bouyer MPII_INTR_STATUS_SYS2IOCDB, 0)
395 1.1 bouyer
396 1.15 bouyer static inline void
397 1.15 bouyer mpii_dvatosge(struct mpii_sge *sge, u_int64_t dva)
398 1.15 bouyer {
399 1.15 bouyer sge->sg_addr_lo = htole32(dva);
400 1.15 bouyer sge->sg_addr_hi = htole32(dva >> 32);
401 1.15 bouyer }
402 1.15 bouyer
403 1.1 bouyer #define MPII_PG_EXTENDED (1<<0)
404 1.1 bouyer #define MPII_PG_POLL (1<<1)
405 1.1 bouyer #define MPII_PG_FMT "\020" "\002POLL" "\001EXTENDED"
406 1.1 bouyer
407 1.1 bouyer static const struct mpii_pci_product {
408 1.1 bouyer pci_vendor_id_t mpii_vendor;
409 1.1 bouyer pci_product_id_t mpii_product;
410 1.1 bouyer } mpii_devices[] = {
411 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2004 },
412 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2008 },
413 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2108_3 },
414 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2108_4 },
415 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2108_5 },
416 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2116_1 },
417 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2116_2 },
418 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2208_1 },
419 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2208_2 },
420 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2208_3 },
421 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2208_4 },
422 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2208_5 },
423 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2208_6 },
424 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2308_1 },
425 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2308_2 },
426 1.1 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2308_3 },
427 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3004 },
428 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3008 },
429 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3108_1 },
430 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3108_2 },
431 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3108_3 },
432 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3108_4 },
433 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3408 },
434 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3416 },
435 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3508 },
436 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3508_1 },
437 1.15 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3516 },
438 1.17 bouyer { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS3516_1 },
439 1.17 bouyer { 0, 0}
440 1.1 bouyer };
441 1.1 bouyer
442 1.24 maxv static int
443 1.1 bouyer mpii_match(device_t parent, cfdata_t match, void *aux)
444 1.1 bouyer {
445 1.1 bouyer struct pci_attach_args *pa = aux;
446 1.1 bouyer const struct mpii_pci_product *mpii;
447 1.1 bouyer
448 1.15 bouyer for (mpii = mpii_devices; mpii->mpii_vendor != 0; mpii++) {
449 1.15 bouyer if (PCI_VENDOR(pa->pa_id) == mpii->mpii_vendor &&
450 1.15 bouyer PCI_PRODUCT(pa->pa_id) == mpii->mpii_product)
451 1.15 bouyer return (1);
452 1.1 bouyer }
453 1.1 bouyer return (0);
454 1.1 bouyer }
455 1.1 bouyer
456 1.24 maxv static void
457 1.1 bouyer mpii_attach(device_t parent, device_t self, void *aux)
458 1.1 bouyer {
459 1.1 bouyer struct mpii_softc *sc = device_private(self);
460 1.1 bouyer struct pci_attach_args *pa = aux;
461 1.1 bouyer pcireg_t memtype;
462 1.1 bouyer int r;
463 1.1 bouyer struct mpii_ccb *ccb;
464 1.1 bouyer struct scsipi_adapter *adapt = &sc->sc_adapt;
465 1.1 bouyer struct scsipi_channel *chan = &sc->sc_chan;
466 1.5 christos char intrbuf[PCI_INTRSTR_LEN];
467 1.15 bouyer const char *intrstr;
468 1.1 bouyer
469 1.1 bouyer pci_aprint_devinfo(pa, NULL);
470 1.1 bouyer
471 1.1 bouyer sc->sc_pc = pa->pa_pc;
472 1.1 bouyer sc->sc_tag = pa->pa_tag;
473 1.1 bouyer sc->sc_dmat = pa->pa_dmat;
474 1.1 bouyer sc->sc_dev = self;
475 1.15 bouyer
476 1.1 bouyer mutex_init(&sc->sc_req_mtx, MUTEX_DEFAULT, IPL_BIO);
477 1.1 bouyer mutex_init(&sc->sc_rep_mtx, MUTEX_DEFAULT, IPL_BIO);
478 1.1 bouyer
479 1.1 bouyer /* find the appropriate memory base */
480 1.1 bouyer for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) {
481 1.1 bouyer memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, r);
482 1.26 skrll if (PCI_MAPREG_TYPE(memtype) == PCI_MAPREG_TYPE_MEM)
483 1.1 bouyer break;
484 1.1 bouyer }
485 1.1 bouyer if (r >= PCI_MAPREG_END) {
486 1.1 bouyer aprint_error_dev(self,
487 1.1 bouyer "unable to locate system interface registers\n");
488 1.1 bouyer return;
489 1.1 bouyer }
490 1.1 bouyer
491 1.1 bouyer if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh,
492 1.1 bouyer NULL, &sc->sc_ios) != 0) {
493 1.1 bouyer aprint_error_dev(self,
494 1.1 bouyer "unable to map system interface registers\n");
495 1.1 bouyer return;
496 1.1 bouyer }
497 1.1 bouyer
498 1.1 bouyer /* disable the expansion rom */
499 1.15 bouyer pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_MAPREG_ROM,
500 1.15 bouyer pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_MAPREG_ROM) &
501 1.15 bouyer ~PCI_MAPREG_ROM_ENABLE);
502 1.1 bouyer
503 1.1 bouyer /* disable interrupts */
504 1.1 bouyer mpii_write(sc, MPII_INTR_MASK,
505 1.1 bouyer MPII_INTR_MASK_RESET | MPII_INTR_MASK_REPLY |
506 1.1 bouyer MPII_INTR_MASK_DOORBELL);
507 1.1 bouyer
508 1.1 bouyer /* hook up the interrupt */
509 1.15 bouyer if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
510 1.1 bouyer aprint_error_dev(self, "unable to map interrupt\n");
511 1.1 bouyer goto unmap;
512 1.1 bouyer }
513 1.15 bouyer intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0],
514 1.15 bouyer intrbuf, sizeof(intrbuf));
515 1.15 bouyer pci_intr_setattr(pa->pa_pc, &sc->sc_pihp[0], PCI_INTR_MPSAFE, true);
516 1.15 bouyer sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, sc->sc_pihp[0], IPL_BIO,
517 1.15 bouyer mpii_intr, sc, device_xname(self));
518 1.15 bouyer if (sc->sc_ih == NULL) {
519 1.15 bouyer aprint_error_dev(self, "couldn't establish interrupt");
520 1.15 bouyer if (intrstr != NULL)
521 1.15 bouyer aprint_error(" at %s", intrstr);
522 1.15 bouyer aprint_error("\n");
523 1.15 bouyer return;
524 1.15 bouyer }
525 1.15 bouyer aprint_normal_dev(self, "interrupting at %s\n", intrstr);
526 1.15 bouyer aprint_naive("\n");
527 1.1 bouyer
528 1.15 bouyer if (mpii_iocfacts(sc) != 0) {
529 1.15 bouyer aprint_error_dev(self, "unable to get iocfacts\n");
530 1.1 bouyer goto unmap;
531 1.1 bouyer }
532 1.1 bouyer
533 1.15 bouyer if (mpii_init(sc) != 0) {
534 1.15 bouyer aprint_error_dev(self, "unable to initialize ioc\n");
535 1.1 bouyer goto unmap;
536 1.1 bouyer }
537 1.1 bouyer
538 1.1 bouyer if (mpii_alloc_ccbs(sc) != 0) {
539 1.1 bouyer /* error already printed */
540 1.1 bouyer goto unmap;
541 1.1 bouyer }
542 1.1 bouyer
543 1.1 bouyer if (mpii_alloc_replies(sc) != 0) {
544 1.1 bouyer aprint_error_dev(self, "unable to allocated reply space\n");
545 1.1 bouyer goto free_ccbs;
546 1.1 bouyer }
547 1.1 bouyer
548 1.1 bouyer if (mpii_alloc_queues(sc) != 0) {
549 1.1 bouyer aprint_error_dev(self, "unable to allocate reply queues\n");
550 1.1 bouyer goto free_replies;
551 1.1 bouyer }
552 1.1 bouyer
553 1.1 bouyer if (mpii_iocinit(sc) != 0) {
554 1.1 bouyer aprint_error_dev(self, "unable to send iocinit\n");
555 1.1 bouyer goto free_queues;
556 1.1 bouyer }
557 1.1 bouyer
558 1.1 bouyer if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
559 1.1 bouyer MPII_DOORBELL_STATE_OPER) != 0) {
560 1.1 bouyer aprint_error_dev(self, "state: 0x%08x\n",
561 1.1 bouyer mpii_read_db(sc) & MPII_DOORBELL_STATE);
562 1.1 bouyer aprint_error_dev(self, "operational state timeout\n");
563 1.1 bouyer goto free_queues;
564 1.1 bouyer }
565 1.1 bouyer
566 1.1 bouyer mpii_push_replies(sc);
567 1.1 bouyer mpii_init_queues(sc);
568 1.1 bouyer
569 1.15 bouyer if (mpii_board_info(sc) != 0) {
570 1.15 bouyer aprint_error_dev(self, "unable to get manufacturing page 0\n");
571 1.15 bouyer goto free_queues;
572 1.15 bouyer }
573 1.15 bouyer
574 1.1 bouyer if (mpii_portfacts(sc) != 0) {
575 1.1 bouyer aprint_error_dev(self, "unable to get portfacts\n");
576 1.1 bouyer goto free_queues;
577 1.1 bouyer }
578 1.1 bouyer
579 1.15 bouyer if (mpii_target_map(sc) != 0) {
580 1.15 bouyer aprint_error_dev(self, "unable to setup target mappings\n");
581 1.1 bouyer goto free_queues;
582 1.1 bouyer }
583 1.1 bouyer
584 1.1 bouyer if (mpii_cfg_coalescing(sc) != 0) {
585 1.1 bouyer aprint_error_dev(self, "unable to configure coalescing\n");
586 1.1 bouyer goto free_queues;
587 1.1 bouyer }
588 1.1 bouyer
589 1.1 bouyer /* XXX bail on unsupported porttype? */
590 1.1 bouyer if ((sc->sc_porttype == MPII_PORTFACTS_PORTTYPE_SAS_PHYSICAL) ||
591 1.15 bouyer (sc->sc_porttype == MPII_PORTFACTS_PORTTYPE_SAS_VIRTUAL) ||
592 1.15 bouyer (sc->sc_porttype == MPII_PORTFACTS_PORTTYPE_TRI_MODE)) {
593 1.1 bouyer if (mpii_eventnotify(sc) != 0) {
594 1.1 bouyer aprint_error_dev(self, "unable to enable events\n");
595 1.1 bouyer goto free_queues;
596 1.1 bouyer }
597 1.1 bouyer }
598 1.1 bouyer
599 1.15 bouyer mutex_init(&sc->sc_devs_mtx, MUTEX_DEFAULT, IPL_BIO);
600 1.15 bouyer sc->sc_devs = malloc(sc->sc_max_devices * sizeof(struct mpii_device *),
601 1.23 chs M_DEVBUF, M_WAITOK | M_ZERO);
602 1.1 bouyer
603 1.1 bouyer if (mpii_portenable(sc) != 0) {
604 1.1 bouyer aprint_error_dev(self, "unable to enable port\n");
605 1.15 bouyer goto free_devs;
606 1.1 bouyer }
607 1.1 bouyer
608 1.15 bouyer /* we should be good to go now, attach scsibus */
609 1.1 bouyer memset(adapt, 0, sizeof(*adapt));
610 1.1 bouyer adapt->adapt_dev = sc->sc_dev;
611 1.1 bouyer adapt->adapt_nchannels = 1;
612 1.15 bouyer adapt->adapt_openings = sc->sc_max_cmds - 4;
613 1.1 bouyer adapt->adapt_max_periph = adapt->adapt_openings;
614 1.1 bouyer adapt->adapt_request = mpii_scsipi_request;
615 1.15 bouyer adapt->adapt_minphys = minphys;
616 1.16 bouyer adapt->adapt_flags = SCSIPI_ADAPT_MPSAFE;
617 1.1 bouyer
618 1.1 bouyer memset(chan, 0, sizeof(*chan));
619 1.1 bouyer chan->chan_adapter = adapt;
620 1.1 bouyer chan->chan_bustype = &scsi_sas_bustype;
621 1.15 bouyer chan->chan_channel = 0;
622 1.1 bouyer chan->chan_flags = 0;
623 1.2 kardel chan->chan_nluns = 8;
624 1.1 bouyer chan->chan_ntargets = sc->sc_max_devices;
625 1.1 bouyer chan->chan_id = -1;
626 1.1 bouyer
627 1.1 bouyer mpii_rescan(self, "scsi", NULL);
628 1.1 bouyer
629 1.1 bouyer /* enable interrupts */
630 1.1 bouyer mpii_write(sc, MPII_INTR_MASK, MPII_INTR_MASK_DOORBELL
631 1.1 bouyer | MPII_INTR_MASK_RESET);
632 1.1 bouyer
633 1.1 bouyer #if NBIO > 0
634 1.1 bouyer if (ISSET(sc->sc_flags, MPII_F_RAID)) {
635 1.1 bouyer if (bio_register(sc->sc_dev, mpii_ioctl) != 0)
636 1.1 bouyer panic("%s: controller registration failed",
637 1.1 bouyer DEVNAME(sc));
638 1.1 bouyer if (mpii_create_sensors(sc) != 0)
639 1.1 bouyer aprint_error_dev(self, "unable to create sensors\n");
640 1.1 bouyer }
641 1.1 bouyer #endif
642 1.1 bouyer
643 1.1 bouyer return;
644 1.1 bouyer
645 1.15 bouyer free_devs:
646 1.15 bouyer free(sc->sc_devs, M_DEVBUF);
647 1.15 bouyer sc->sc_devs = NULL;
648 1.1 bouyer
649 1.1 bouyer free_queues:
650 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_freeq),
651 1.15 bouyer 0, sc->sc_reply_free_qdepth * 4, BUS_DMASYNC_POSTREAD);
652 1.1 bouyer mpii_dmamem_free(sc, sc->sc_reply_freeq);
653 1.1 bouyer
654 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
655 1.1 bouyer 0, sc->sc_reply_post_qdepth * 8, BUS_DMASYNC_POSTREAD);
656 1.1 bouyer mpii_dmamem_free(sc, sc->sc_reply_postq);
657 1.1 bouyer
658 1.1 bouyer free_replies:
659 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
660 1.1 bouyer 0, PAGE_SIZE, BUS_DMASYNC_POSTREAD);
661 1.1 bouyer mpii_dmamem_free(sc, sc->sc_replies);
662 1.1 bouyer
663 1.1 bouyer free_ccbs:
664 1.15 bouyer while ((ccb = mpii_get_ccb(sc)) != NULL)
665 1.1 bouyer bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
666 1.1 bouyer mpii_dmamem_free(sc, sc->sc_requests);
667 1.1 bouyer free(sc->sc_ccbs, M_DEVBUF);
668 1.1 bouyer
669 1.1 bouyer unmap:
670 1.1 bouyer bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
671 1.1 bouyer sc->sc_ios = 0;
672 1.1 bouyer }
673 1.1 bouyer
674 1.24 maxv static int
675 1.1 bouyer mpii_detach(device_t self, int flags)
676 1.1 bouyer {
677 1.15 bouyer struct mpii_softc *sc = device_private(self);
678 1.1 bouyer int error;
679 1.1 bouyer struct mpii_ccb *ccb;
680 1.1 bouyer
681 1.1 bouyer if ((error = config_detach_children(sc->sc_dev, flags)) != 0)
682 1.1 bouyer return error;
683 1.1 bouyer
684 1.1 bouyer #if NBIO > 0
685 1.1 bouyer mpii_destroy_sensors(sc);
686 1.1 bouyer bio_unregister(sc->sc_dev);
687 1.1 bouyer #endif /* NBIO > 0 */
688 1.1 bouyer
689 1.1 bouyer if (sc->sc_ih != NULL) {
690 1.15 bouyer pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
691 1.15 bouyer sc->sc_ih = NULL;
692 1.15 bouyer }
693 1.15 bouyer if (sc->sc_ios != 0) {
694 1.15 bouyer bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
695 1.15 bouyer free(sc->sc_devs, M_DEVBUF);
696 1.15 bouyer sc->sc_devs = NULL;
697 1.1 bouyer
698 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_freeq),
699 1.1 bouyer 0, sc->sc_reply_free_qdepth * 4, BUS_DMASYNC_POSTREAD);
700 1.1 bouyer mpii_dmamem_free(sc, sc->sc_reply_freeq);
701 1.1 bouyer
702 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
703 1.1 bouyer 0, sc->sc_reply_post_qdepth * 8, BUS_DMASYNC_POSTREAD);
704 1.1 bouyer mpii_dmamem_free(sc, sc->sc_reply_postq);
705 1.1 bouyer
706 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
707 1.1 bouyer 0, PAGE_SIZE, BUS_DMASYNC_POSTREAD);
708 1.1 bouyer mpii_dmamem_free(sc, sc->sc_replies);
709 1.1 bouyer
710 1.15 bouyer while ((ccb = mpii_get_ccb(sc)) != NULL)
711 1.1 bouyer bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
712 1.1 bouyer mpii_dmamem_free(sc, sc->sc_requests);
713 1.1 bouyer free(sc->sc_ccbs, M_DEVBUF);
714 1.1 bouyer
715 1.1 bouyer sc->sc_ios = 0;
716 1.1 bouyer }
717 1.1 bouyer
718 1.1 bouyer return (0);
719 1.1 bouyer }
720 1.1 bouyer
721 1.24 maxv static int
722 1.15 bouyer mpii_rescan(device_t self, const char *ifattr, const int *locators)
723 1.1 bouyer {
724 1.1 bouyer struct mpii_softc *sc = device_private(self);
725 1.1 bouyer
726 1.1 bouyer if (sc->sc_child != NULL)
727 1.1 bouyer return 0;
728 1.1 bouyer
729 1.1 bouyer sc->sc_child = config_found_sm_loc(self, ifattr, locators, &sc->sc_chan,
730 1.1 bouyer scsiprint, NULL);
731 1.1 bouyer
732 1.1 bouyer return 0;
733 1.1 bouyer }
734 1.1 bouyer
735 1.24 maxv static void
736 1.1 bouyer mpii_childdetached(device_t self, device_t child)
737 1.1 bouyer {
738 1.15 bouyer struct mpii_softc *sc = device_private(self);
739 1.1 bouyer
740 1.15 bouyer KASSERT(self == sc->sc_dev);
741 1.15 bouyer KASSERT(child == sc->sc_child);
742 1.1 bouyer
743 1.15 bouyer if (child == sc->sc_child)
744 1.15 bouyer sc->sc_child = NULL;
745 1.1 bouyer }
746 1.1 bouyer
747 1.15 bouyer
748 1.24 maxv static int
749 1.1 bouyer mpii_intr(void *arg)
750 1.1 bouyer {
751 1.1 bouyer struct mpii_rcb_list evts = SIMPLEQ_HEAD_INITIALIZER(evts);
752 1.1 bouyer struct mpii_ccb_list ccbs = SIMPLEQ_HEAD_INITIALIZER(ccbs);
753 1.1 bouyer struct mpii_softc *sc = arg;
754 1.1 bouyer struct mpii_reply_descr *postq = sc->sc_reply_postq_kva, *rdp;
755 1.1 bouyer struct mpii_ccb *ccb;
756 1.1 bouyer struct mpii_rcb *rcb;
757 1.1 bouyer int smid;
758 1.15 bouyer u_int idx;
759 1.1 bouyer int rv = 0;
760 1.1 bouyer
761 1.1 bouyer mutex_enter(&sc->sc_rep_mtx);
762 1.1 bouyer bus_dmamap_sync(sc->sc_dmat,
763 1.1 bouyer MPII_DMA_MAP(sc->sc_reply_postq),
764 1.15 bouyer 0, sc->sc_reply_post_qdepth * sizeof(*rdp),
765 1.1 bouyer BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
766 1.1 bouyer
767 1.15 bouyer idx = sc->sc_reply_post_host_index;
768 1.1 bouyer for (;;) {
769 1.15 bouyer rdp = &postq[idx];
770 1.1 bouyer if ((rdp->reply_flags & MPII_REPLY_DESCR_TYPE_MASK) ==
771 1.1 bouyer MPII_REPLY_DESCR_UNUSED)
772 1.1 bouyer break;
773 1.1 bouyer if (rdp->data == 0xffffffff) {
774 1.1 bouyer /*
775 1.1 bouyer * ioc is still writing to the reply post queue
776 1.1 bouyer * race condition - bail!
777 1.1 bouyer */
778 1.1 bouyer break;
779 1.1 bouyer }
780 1.1 bouyer
781 1.1 bouyer smid = le16toh(rdp->smid);
782 1.1 bouyer rcb = mpii_reply(sc, rdp);
783 1.1 bouyer
784 1.1 bouyer if (smid) {
785 1.1 bouyer ccb = &sc->sc_ccbs[smid - 1];
786 1.1 bouyer ccb->ccb_state = MPII_CCB_READY;
787 1.1 bouyer ccb->ccb_rcb = rcb;
788 1.15 bouyer SIMPLEQ_INSERT_TAIL(&ccbs, ccb, ccb_link);
789 1.1 bouyer } else
790 1.15 bouyer SIMPLEQ_INSERT_TAIL(&evts, rcb, rcb_link);
791 1.15 bouyer
792 1.15 bouyer if (++idx >= sc->sc_reply_post_qdepth)
793 1.15 bouyer idx = 0;
794 1.1 bouyer
795 1.1 bouyer rv = 1;
796 1.1 bouyer }
797 1.1 bouyer
798 1.1 bouyer bus_dmamap_sync(sc->sc_dmat,
799 1.1 bouyer MPII_DMA_MAP(sc->sc_reply_postq),
800 1.15 bouyer 0, sc->sc_reply_post_qdepth * sizeof(*rdp),
801 1.1 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
802 1.1 bouyer
803 1.1 bouyer if (rv)
804 1.15 bouyer mpii_write_reply_post(sc, sc->sc_reply_post_host_index = idx);
805 1.1 bouyer
806 1.1 bouyer mutex_exit(&sc->sc_rep_mtx);
807 1.1 bouyer
808 1.1 bouyer if (rv == 0)
809 1.1 bouyer return (0);
810 1.1 bouyer
811 1.1 bouyer while ((ccb = SIMPLEQ_FIRST(&ccbs)) != NULL) {
812 1.15 bouyer SIMPLEQ_REMOVE_HEAD(&ccbs, ccb_link);
813 1.1 bouyer ccb->ccb_done(ccb);
814 1.1 bouyer }
815 1.1 bouyer while ((rcb = SIMPLEQ_FIRST(&evts)) != NULL) {
816 1.15 bouyer SIMPLEQ_REMOVE_HEAD(&evts, rcb_link);
817 1.1 bouyer mpii_event_process(sc, rcb);
818 1.1 bouyer }
819 1.1 bouyer
820 1.1 bouyer return (1);
821 1.1 bouyer }
822 1.1 bouyer
823 1.24 maxv static int
824 1.15 bouyer mpii_load_xs_sas3(struct mpii_ccb *ccb)
825 1.1 bouyer {
826 1.1 bouyer struct mpii_softc *sc = ccb->ccb_sc;
827 1.1 bouyer struct scsipi_xfer *xs = ccb->ccb_cookie;
828 1.15 bouyer struct mpii_msg_scsi_io *io = ccb->ccb_cmd;
829 1.15 bouyer struct mpii_ieee_sge *csge, *nsge, *sge;
830 1.1 bouyer bus_dmamap_t dmap = ccb->ccb_dmamap;
831 1.1 bouyer int i, error;
832 1.1 bouyer
833 1.15 bouyer /* Request frame structure is described in the mpii_iocfacts */
834 1.15 bouyer nsge = (struct mpii_ieee_sge *)(io + 1);
835 1.15 bouyer csge = nsge + sc->sc_chain_sge;
836 1.15 bouyer
837 1.1 bouyer /* zero length transfer still requires an SGE */
838 1.1 bouyer if (xs->datalen == 0) {
839 1.15 bouyer nsge->sg_flags = MPII_IEEE_SGE_END_OF_LIST;
840 1.1 bouyer return (0);
841 1.1 bouyer }
842 1.1 bouyer
843 1.15 bouyer error = bus_dmamap_load(sc->sc_dmat, dmap, xs->data, xs->datalen, NULL,
844 1.15 bouyer (xs->xs_control & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
845 1.1 bouyer if (error) {
846 1.15 bouyer printf("%s: error %d loading dmamap\n", DEVNAME(sc), error);
847 1.1 bouyer return (1);
848 1.1 bouyer }
849 1.1 bouyer
850 1.15 bouyer sge = nsge;
851 1.15 bouyer for (i = 0; i < dmap->dm_nsegs; i++, nsge++) {
852 1.15 bouyer if (nsge == csge) {
853 1.15 bouyer nsge++;
854 1.15 bouyer /* offset to the chain sge from the beginning */
855 1.15 bouyer io->chain_offset = ((uintptr_t)csge - (uintptr_t)io) / 4;
856 1.15 bouyer csge->sg_flags = MPII_IEEE_SGE_CHAIN_ELEMENT |
857 1.15 bouyer MPII_IEEE_SGE_ADDR_SYSTEM;
858 1.15 bouyer /* address of the next sge */
859 1.15 bouyer csge->sg_addr = htole64(ccb->ccb_cmd_dva +
860 1.15 bouyer ((uintptr_t)nsge - (uintptr_t)io));
861 1.15 bouyer csge->sg_len = htole32((dmap->dm_nsegs - i) *
862 1.15 bouyer sizeof(*sge));
863 1.15 bouyer }
864 1.1 bouyer
865 1.15 bouyer sge = nsge;
866 1.15 bouyer sge->sg_flags = MPII_IEEE_SGE_ADDR_SYSTEM;
867 1.15 bouyer sge->sg_len = htole32(dmap->dm_segs[i].ds_len);
868 1.15 bouyer sge->sg_addr = htole64(dmap->dm_segs[i].ds_addr);
869 1.1 bouyer }
870 1.1 bouyer
871 1.15 bouyer /* terminate list */
872 1.15 bouyer sge->sg_flags |= MPII_IEEE_SGE_END_OF_LIST;
873 1.1 bouyer
874 1.15 bouyer bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
875 1.15 bouyer (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
876 1.15 bouyer BUS_DMASYNC_PREWRITE);
877 1.1 bouyer
878 1.15 bouyer return (0);
879 1.15 bouyer }
880 1.1 bouyer
881 1.24 maxv static int
882 1.15 bouyer mpii_load_xs(struct mpii_ccb *ccb)
883 1.15 bouyer {
884 1.15 bouyer struct mpii_softc *sc = ccb->ccb_sc;
885 1.15 bouyer struct scsipi_xfer *xs = ccb->ccb_cookie;
886 1.15 bouyer struct mpii_msg_scsi_io *io = ccb->ccb_cmd;
887 1.15 bouyer struct mpii_sge *csge, *nsge, *sge;
888 1.15 bouyer bus_dmamap_t dmap = ccb->ccb_dmamap;
889 1.15 bouyer u_int32_t flags;
890 1.15 bouyer u_int16_t len;
891 1.15 bouyer int i, error;
892 1.1 bouyer
893 1.15 bouyer /* Request frame structure is described in the mpii_iocfacts */
894 1.15 bouyer nsge = (struct mpii_sge *)(io + 1);
895 1.15 bouyer csge = nsge + sc->sc_chain_sge;
896 1.1 bouyer
897 1.15 bouyer /* zero length transfer still requires an SGE */
898 1.15 bouyer if (xs->datalen == 0) {
899 1.15 bouyer nsge->sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
900 1.15 bouyer MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL);
901 1.15 bouyer return (0);
902 1.15 bouyer }
903 1.15 bouyer
904 1.15 bouyer error = bus_dmamap_load(sc->sc_dmat, dmap, xs->data, xs->datalen, NULL,
905 1.15 bouyer (xs->xs_control & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
906 1.15 bouyer if (error) {
907 1.15 bouyer printf("%s: error %d loading dmamap\n", DEVNAME(sc), error);
908 1.15 bouyer return (1);
909 1.15 bouyer }
910 1.1 bouyer
911 1.15 bouyer /* safe default starting flags */
912 1.15 bouyer flags = MPII_SGE_FL_TYPE_SIMPLE | MPII_SGE_FL_SIZE_64;
913 1.15 bouyer if (xs->xs_control & XS_CTL_DATA_OUT)
914 1.15 bouyer flags |= MPII_SGE_FL_DIR_OUT;
915 1.15 bouyer
916 1.15 bouyer sge = nsge;
917 1.15 bouyer for (i = 0; i < dmap->dm_nsegs; i++, nsge++) {
918 1.15 bouyer if (nsge == csge) {
919 1.15 bouyer nsge++;
920 1.15 bouyer /* offset to the chain sge from the beginning */
921 1.15 bouyer io->chain_offset = ((uintptr_t)csge - (uintptr_t)io) / 4;
922 1.15 bouyer /* length of the sgl segment we're pointing to */
923 1.15 bouyer len = (dmap->dm_nsegs - i) * sizeof(*sge);
924 1.15 bouyer csge->sg_hdr = htole32(MPII_SGE_FL_TYPE_CHAIN |
925 1.15 bouyer MPII_SGE_FL_SIZE_64 | len);
926 1.15 bouyer /* address of the next sge */
927 1.15 bouyer mpii_dvatosge(csge, ccb->ccb_cmd_dva +
928 1.15 bouyer ((uintptr_t)nsge - (uintptr_t)io));
929 1.1 bouyer }
930 1.1 bouyer
931 1.1 bouyer sge = nsge;
932 1.1 bouyer sge->sg_hdr = htole32(flags | dmap->dm_segs[i].ds_len);
933 1.15 bouyer mpii_dvatosge(sge, dmap->dm_segs[i].ds_addr);
934 1.1 bouyer }
935 1.1 bouyer
936 1.1 bouyer /* terminate list */
937 1.1 bouyer sge->sg_hdr |= htole32(MPII_SGE_FL_LAST | MPII_SGE_FL_EOB |
938 1.1 bouyer MPII_SGE_FL_EOL);
939 1.1 bouyer
940 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
941 1.1 bouyer (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
942 1.1 bouyer BUS_DMASYNC_PREWRITE);
943 1.1 bouyer
944 1.1 bouyer return (0);
945 1.1 bouyer }
946 1.1 bouyer
947 1.24 maxv static u_int32_t
948 1.1 bouyer mpii_read(struct mpii_softc *sc, bus_size_t r)
949 1.1 bouyer {
950 1.1 bouyer u_int32_t rv;
951 1.1 bouyer
952 1.1 bouyer bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
953 1.1 bouyer BUS_SPACE_BARRIER_READ);
954 1.1 bouyer rv = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
955 1.1 bouyer
956 1.15 bouyer DNPRINTF(MPII_D_RW, "%s: mpii_read %#lx %#x\n", DEVNAME(sc), r, rv);
957 1.1 bouyer
958 1.1 bouyer return (rv);
959 1.1 bouyer }
960 1.1 bouyer
961 1.24 maxv static void
962 1.1 bouyer mpii_write(struct mpii_softc *sc, bus_size_t r, u_int32_t v)
963 1.1 bouyer {
964 1.15 bouyer DNPRINTF(MPII_D_RW, "%s: mpii_write %#lx %#x\n", DEVNAME(sc), r, v);
965 1.1 bouyer
966 1.1 bouyer bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
967 1.1 bouyer bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
968 1.1 bouyer BUS_SPACE_BARRIER_WRITE);
969 1.1 bouyer }
970 1.1 bouyer
971 1.1 bouyer
972 1.24 maxv static int
973 1.1 bouyer mpii_wait_eq(struct mpii_softc *sc, bus_size_t r, u_int32_t mask,
974 1.1 bouyer u_int32_t target)
975 1.1 bouyer {
976 1.1 bouyer int i;
977 1.1 bouyer
978 1.15 bouyer DNPRINTF(MPII_D_RW, "%s: mpii_wait_eq %#lx %#x %#x\n", DEVNAME(sc), r,
979 1.15 bouyer mask, target);
980 1.1 bouyer
981 1.1 bouyer for (i = 0; i < 15000; i++) {
982 1.1 bouyer if ((mpii_read(sc, r) & mask) == target)
983 1.1 bouyer return (0);
984 1.1 bouyer delay(1000);
985 1.1 bouyer }
986 1.1 bouyer
987 1.1 bouyer return (1);
988 1.1 bouyer }
989 1.1 bouyer
990 1.24 maxv static int
991 1.1 bouyer mpii_wait_ne(struct mpii_softc *sc, bus_size_t r, u_int32_t mask,
992 1.1 bouyer u_int32_t target)
993 1.1 bouyer {
994 1.1 bouyer int i;
995 1.1 bouyer
996 1.15 bouyer DNPRINTF(MPII_D_RW, "%s: mpii_wait_ne %#lx %#x %#x\n", DEVNAME(sc), r,
997 1.15 bouyer mask, target);
998 1.1 bouyer
999 1.1 bouyer for (i = 0; i < 15000; i++) {
1000 1.1 bouyer if ((mpii_read(sc, r) & mask) != target)
1001 1.1 bouyer return (0);
1002 1.1 bouyer delay(1000);
1003 1.1 bouyer }
1004 1.1 bouyer
1005 1.1 bouyer return (1);
1006 1.1 bouyer }
1007 1.1 bouyer
1008 1.24 maxv static int
1009 1.1 bouyer mpii_init(struct mpii_softc *sc)
1010 1.1 bouyer {
1011 1.1 bouyer u_int32_t db;
1012 1.1 bouyer int i;
1013 1.1 bouyer
1014 1.1 bouyer /* spin until the ioc leaves the reset state */
1015 1.1 bouyer if (mpii_wait_ne(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
1016 1.1 bouyer MPII_DOORBELL_STATE_RESET) != 0) {
1017 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_init timeout waiting to leave "
1018 1.1 bouyer "reset state\n", DEVNAME(sc));
1019 1.1 bouyer return (1);
1020 1.1 bouyer }
1021 1.1 bouyer
1022 1.1 bouyer /* check current ownership */
1023 1.1 bouyer db = mpii_read_db(sc);
1024 1.1 bouyer if ((db & MPII_DOORBELL_WHOINIT) == MPII_DOORBELL_WHOINIT_PCIPEER) {
1025 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_init initialised by pci peer\n",
1026 1.1 bouyer DEVNAME(sc));
1027 1.1 bouyer return (0);
1028 1.1 bouyer }
1029 1.1 bouyer
1030 1.1 bouyer for (i = 0; i < 5; i++) {
1031 1.1 bouyer switch (db & MPII_DOORBELL_STATE) {
1032 1.1 bouyer case MPII_DOORBELL_STATE_READY:
1033 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is ready\n",
1034 1.1 bouyer DEVNAME(sc));
1035 1.1 bouyer return (0);
1036 1.1 bouyer
1037 1.1 bouyer case MPII_DOORBELL_STATE_OPER:
1038 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is oper\n",
1039 1.1 bouyer DEVNAME(sc));
1040 1.1 bouyer if (sc->sc_ioc_event_replay)
1041 1.1 bouyer mpii_reset_soft(sc);
1042 1.1 bouyer else
1043 1.1 bouyer mpii_reset_hard(sc);
1044 1.1 bouyer break;
1045 1.1 bouyer
1046 1.1 bouyer case MPII_DOORBELL_STATE_FAULT:
1047 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_init ioc is being "
1048 1.1 bouyer "reset hard\n" , DEVNAME(sc));
1049 1.1 bouyer mpii_reset_hard(sc);
1050 1.1 bouyer break;
1051 1.1 bouyer
1052 1.1 bouyer case MPII_DOORBELL_STATE_RESET:
1053 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_init waiting to come "
1054 1.1 bouyer "out of reset\n", DEVNAME(sc));
1055 1.1 bouyer if (mpii_wait_ne(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
1056 1.1 bouyer MPII_DOORBELL_STATE_RESET) != 0)
1057 1.1 bouyer return (1);
1058 1.1 bouyer break;
1059 1.1 bouyer }
1060 1.1 bouyer db = mpii_read_db(sc);
1061 1.1 bouyer }
1062 1.1 bouyer
1063 1.1 bouyer return (1);
1064 1.1 bouyer }
1065 1.1 bouyer
1066 1.24 maxv static int
1067 1.1 bouyer mpii_reset_soft(struct mpii_softc *sc)
1068 1.1 bouyer {
1069 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_reset_soft\n", DEVNAME(sc));
1070 1.1 bouyer
1071 1.1 bouyer if (mpii_read_db(sc) & MPII_DOORBELL_INUSE) {
1072 1.1 bouyer return (1);
1073 1.1 bouyer }
1074 1.1 bouyer
1075 1.1 bouyer mpii_write_db(sc,
1076 1.1 bouyer MPII_DOORBELL_FUNCTION(MPII_FUNCTION_IOC_MESSAGE_UNIT_RESET));
1077 1.15 bouyer
1078 1.1 bouyer /* XXX LSI waits 15 sec */
1079 1.1 bouyer if (mpii_wait_db_ack(sc) != 0)
1080 1.1 bouyer return (1);
1081 1.1 bouyer
1082 1.1 bouyer /* XXX LSI waits 15 sec */
1083 1.1 bouyer if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_STATE,
1084 1.1 bouyer MPII_DOORBELL_STATE_READY) != 0)
1085 1.1 bouyer return (1);
1086 1.1 bouyer
1087 1.1 bouyer /* XXX wait for Sys2IOCDB bit to clear in HIS?? */
1088 1.1 bouyer
1089 1.1 bouyer return (0);
1090 1.1 bouyer }
1091 1.1 bouyer
1092 1.24 maxv static int
1093 1.1 bouyer mpii_reset_hard(struct mpii_softc *sc)
1094 1.1 bouyer {
1095 1.1 bouyer u_int16_t i;
1096 1.1 bouyer
1097 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_reset_hard\n", DEVNAME(sc));
1098 1.1 bouyer
1099 1.1 bouyer mpii_write_intr(sc, 0);
1100 1.1 bouyer
1101 1.1 bouyer /* enable diagnostic register */
1102 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_FLUSH);
1103 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_1);
1104 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_2);
1105 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_3);
1106 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_4);
1107 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_5);
1108 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, MPII_WRITESEQ_6);
1109 1.1 bouyer
1110 1.1 bouyer delay(100);
1111 1.1 bouyer
1112 1.1 bouyer if ((mpii_read(sc, MPII_HOSTDIAG) & MPII_HOSTDIAG_DWRE) == 0) {
1113 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_reset_hard failure to enable "
1114 1.1 bouyer "diagnostic read/write\n", DEVNAME(sc));
1115 1.1 bouyer return(1);
1116 1.1 bouyer }
1117 1.1 bouyer
1118 1.1 bouyer /* reset ioc */
1119 1.1 bouyer mpii_write(sc, MPII_HOSTDIAG, MPII_HOSTDIAG_RESET_ADAPTER);
1120 1.1 bouyer
1121 1.1 bouyer /* 240 milliseconds */
1122 1.1 bouyer delay(240000);
1123 1.1 bouyer
1124 1.1 bouyer
1125 1.1 bouyer /* XXX this whole function should be more robust */
1126 1.15 bouyer
1127 1.1 bouyer /* XXX read the host diagnostic reg until reset adapter bit clears ? */
1128 1.1 bouyer for (i = 0; i < 30000; i++) {
1129 1.1 bouyer if ((mpii_read(sc, MPII_HOSTDIAG) &
1130 1.1 bouyer MPII_HOSTDIAG_RESET_ADAPTER) == 0)
1131 1.1 bouyer break;
1132 1.1 bouyer delay(10000);
1133 1.1 bouyer }
1134 1.1 bouyer
1135 1.1 bouyer /* disable diagnostic register */
1136 1.1 bouyer mpii_write(sc, MPII_WRITESEQ, 0xff);
1137 1.1 bouyer
1138 1.1 bouyer /* XXX what else? */
1139 1.1 bouyer
1140 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: done with mpii_reset_hard\n", DEVNAME(sc));
1141 1.1 bouyer
1142 1.1 bouyer return(0);
1143 1.1 bouyer }
1144 1.1 bouyer
1145 1.24 maxv static int
1146 1.1 bouyer mpii_handshake_send(struct mpii_softc *sc, void *buf, size_t dwords)
1147 1.1 bouyer {
1148 1.1 bouyer u_int32_t *query = buf;
1149 1.1 bouyer int i;
1150 1.1 bouyer
1151 1.1 bouyer /* make sure the doorbell is not in use. */
1152 1.1 bouyer if (mpii_read_db(sc) & MPII_DOORBELL_INUSE)
1153 1.1 bouyer return (1);
1154 1.1 bouyer
1155 1.1 bouyer /* clear pending doorbell interrupts */
1156 1.1 bouyer if (mpii_read_intr(sc) & MPII_INTR_STATUS_IOC2SYSDB)
1157 1.1 bouyer mpii_write_intr(sc, 0);
1158 1.1 bouyer
1159 1.1 bouyer /*
1160 1.1 bouyer * first write the doorbell with the handshake function and the
1161 1.1 bouyer * dword count.
1162 1.1 bouyer */
1163 1.1 bouyer mpii_write_db(sc, MPII_DOORBELL_FUNCTION(MPII_FUNCTION_HANDSHAKE) |
1164 1.1 bouyer MPII_DOORBELL_DWORDS(dwords));
1165 1.1 bouyer
1166 1.1 bouyer /*
1167 1.1 bouyer * the doorbell used bit will be set because a doorbell function has
1168 1.1 bouyer * started. wait for the interrupt and then ack it.
1169 1.1 bouyer */
1170 1.1 bouyer if (mpii_wait_db_int(sc) != 0)
1171 1.1 bouyer return (1);
1172 1.1 bouyer mpii_write_intr(sc, 0);
1173 1.1 bouyer
1174 1.1 bouyer /* poll for the acknowledgement. */
1175 1.1 bouyer if (mpii_wait_db_ack(sc) != 0)
1176 1.1 bouyer return (1);
1177 1.1 bouyer
1178 1.1 bouyer /* write the query through the doorbell. */
1179 1.1 bouyer for (i = 0; i < dwords; i++) {
1180 1.1 bouyer mpii_write_db(sc, htole32(query[i]));
1181 1.1 bouyer if (mpii_wait_db_ack(sc) != 0)
1182 1.1 bouyer return (1);
1183 1.1 bouyer }
1184 1.1 bouyer
1185 1.1 bouyer return (0);
1186 1.1 bouyer }
1187 1.1 bouyer
1188 1.24 maxv static int
1189 1.1 bouyer mpii_handshake_recv_dword(struct mpii_softc *sc, u_int32_t *dword)
1190 1.1 bouyer {
1191 1.1 bouyer u_int16_t *words = (u_int16_t *)dword;
1192 1.1 bouyer int i;
1193 1.1 bouyer
1194 1.1 bouyer for (i = 0; i < 2; i++) {
1195 1.1 bouyer if (mpii_wait_db_int(sc) != 0)
1196 1.1 bouyer return (1);
1197 1.1 bouyer words[i] = le16toh(mpii_read_db(sc) & MPII_DOORBELL_DATA_MASK);
1198 1.1 bouyer mpii_write_intr(sc, 0);
1199 1.1 bouyer }
1200 1.1 bouyer
1201 1.1 bouyer return (0);
1202 1.1 bouyer }
1203 1.1 bouyer
1204 1.24 maxv static int
1205 1.1 bouyer mpii_handshake_recv(struct mpii_softc *sc, void *buf, size_t dwords)
1206 1.1 bouyer {
1207 1.1 bouyer struct mpii_msg_reply *reply = buf;
1208 1.1 bouyer u_int32_t *dbuf = buf, dummy;
1209 1.1 bouyer int i;
1210 1.1 bouyer
1211 1.1 bouyer /* get the first dword so we can read the length out of the header. */
1212 1.1 bouyer if (mpii_handshake_recv_dword(sc, &dbuf[0]) != 0)
1213 1.1 bouyer return (1);
1214 1.1 bouyer
1215 1.15 bouyer DNPRINTF(MPII_D_CMD, "%s: mpii_handshake_recv dwords: %lu reply: %d\n",
1216 1.1 bouyer DEVNAME(sc), dwords, reply->msg_length);
1217 1.1 bouyer
1218 1.1 bouyer /*
1219 1.1 bouyer * the total length, in dwords, is in the message length field of the
1220 1.1 bouyer * reply header.
1221 1.1 bouyer */
1222 1.1 bouyer for (i = 1; i < MIN(dwords, reply->msg_length); i++) {
1223 1.1 bouyer if (mpii_handshake_recv_dword(sc, &dbuf[i]) != 0)
1224 1.1 bouyer return (1);
1225 1.1 bouyer }
1226 1.1 bouyer
1227 1.1 bouyer /* if there's extra stuff to come off the ioc, discard it */
1228 1.1 bouyer while (i++ < reply->msg_length) {
1229 1.1 bouyer if (mpii_handshake_recv_dword(sc, &dummy) != 0)
1230 1.1 bouyer return (1);
1231 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: mpii_handshake_recv dummy read: "
1232 1.1 bouyer "0x%08x\n", DEVNAME(sc), dummy);
1233 1.1 bouyer }
1234 1.1 bouyer
1235 1.1 bouyer /* wait for the doorbell used bit to be reset and clear the intr */
1236 1.1 bouyer if (mpii_wait_db_int(sc) != 0)
1237 1.1 bouyer return (1);
1238 1.15 bouyer
1239 1.1 bouyer if (mpii_wait_eq(sc, MPII_DOORBELL, MPII_DOORBELL_INUSE, 0) != 0)
1240 1.1 bouyer return (1);
1241 1.15 bouyer
1242 1.1 bouyer mpii_write_intr(sc, 0);
1243 1.1 bouyer
1244 1.1 bouyer return (0);
1245 1.1 bouyer }
1246 1.1 bouyer
1247 1.24 maxv static void
1248 1.1 bouyer mpii_empty_done(struct mpii_ccb *ccb)
1249 1.1 bouyer {
1250 1.1 bouyer /* nothing to do */
1251 1.1 bouyer }
1252 1.1 bouyer
1253 1.24 maxv static int
1254 1.1 bouyer mpii_iocfacts(struct mpii_softc *sc)
1255 1.1 bouyer {
1256 1.1 bouyer struct mpii_msg_iocfacts_request ifq;
1257 1.1 bouyer struct mpii_msg_iocfacts_reply ifp;
1258 1.15 bouyer int irs;
1259 1.15 bouyer int sge_size;
1260 1.15 bouyer u_int qdepth;
1261 1.1 bouyer
1262 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts\n", DEVNAME(sc));
1263 1.1 bouyer
1264 1.15 bouyer memset(&ifq, 0, sizeof(ifq));
1265 1.15 bouyer memset(&ifp, 0, sizeof(ifp));
1266 1.1 bouyer
1267 1.1 bouyer ifq.function = MPII_FUNCTION_IOC_FACTS;
1268 1.1 bouyer
1269 1.1 bouyer if (mpii_handshake_send(sc, &ifq, dwordsof(ifq)) != 0) {
1270 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts send failed\n",
1271 1.1 bouyer DEVNAME(sc));
1272 1.1 bouyer return (1);
1273 1.1 bouyer }
1274 1.1 bouyer
1275 1.1 bouyer if (mpii_handshake_recv(sc, &ifp, dwordsof(ifp)) != 0) {
1276 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_iocfacts recv failed\n",
1277 1.1 bouyer DEVNAME(sc));
1278 1.1 bouyer return (1);
1279 1.1 bouyer }
1280 1.1 bouyer
1281 1.1 bouyer sc->sc_ioc_number = ifp.ioc_number;
1282 1.1 bouyer sc->sc_vf_id = ifp.vf_id;
1283 1.1 bouyer
1284 1.1 bouyer sc->sc_max_volumes = ifp.max_volumes;
1285 1.1 bouyer sc->sc_max_devices = ifp.max_volumes + le16toh(ifp.max_targets);
1286 1.1 bouyer
1287 1.1 bouyer if (ISSET(le32toh(ifp.ioc_capabilities),
1288 1.1 bouyer MPII_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
1289 1.1 bouyer SET(sc->sc_flags, MPII_F_RAID);
1290 1.15 bouyer if (ISSET(le32toh(ifp.ioc_capabilities),
1291 1.15 bouyer MPII_IOCFACTS_CAPABILITY_EVENT_REPLAY))
1292 1.15 bouyer sc->sc_ioc_event_replay = 1;
1293 1.15 bouyer
1294 1.15 bouyer sc->sc_max_cmds = MIN(le16toh(ifp.request_credit),
1295 1.15 bouyer MPII_REQUEST_CREDIT);
1296 1.1 bouyer
1297 1.15 bouyer /* SAS3 and 3.5 controllers have different sgl layouts */
1298 1.15 bouyer if (ifp.msg_version_maj == 2 && ((ifp.msg_version_min == 5)
1299 1.15 bouyer || (ifp.msg_version_min == 6)))
1300 1.15 bouyer SET(sc->sc_flags, MPII_F_SAS3);
1301 1.1 bouyer
1302 1.15 bouyer /*
1303 1.15 bouyer * The host driver must ensure that there is at least one
1304 1.15 bouyer * unused entry in the Reply Free Queue. One way to ensure
1305 1.15 bouyer * that this requirement is met is to never allocate a number
1306 1.15 bouyer * of reply frames that is a multiple of 16.
1307 1.15 bouyer */
1308 1.15 bouyer sc->sc_num_reply_frames = sc->sc_max_cmds + 32;
1309 1.1 bouyer if (!(sc->sc_num_reply_frames % 16))
1310 1.1 bouyer sc->sc_num_reply_frames--;
1311 1.1 bouyer
1312 1.1 bouyer /* must be multiple of 16 */
1313 1.15 bouyer sc->sc_reply_post_qdepth = sc->sc_max_cmds +
1314 1.15 bouyer sc->sc_num_reply_frames;
1315 1.15 bouyer sc->sc_reply_post_qdepth += 16 - (sc->sc_reply_post_qdepth % 16);
1316 1.15 bouyer
1317 1.15 bouyer qdepth = le16toh(ifp.max_reply_descriptor_post_queue_depth);
1318 1.15 bouyer if (sc->sc_reply_post_qdepth > qdepth) {
1319 1.15 bouyer sc->sc_reply_post_qdepth = qdepth;
1320 1.15 bouyer if (sc->sc_reply_post_qdepth < 16) {
1321 1.15 bouyer printf("%s: RDPQ is too shallow\n", DEVNAME(sc));
1322 1.15 bouyer return (1);
1323 1.15 bouyer }
1324 1.15 bouyer sc->sc_max_cmds = sc->sc_reply_post_qdepth / 2 - 4;
1325 1.15 bouyer sc->sc_num_reply_frames = sc->sc_max_cmds + 4;
1326 1.15 bouyer }
1327 1.15 bouyer
1328 1.1 bouyer sc->sc_reply_free_qdepth = sc->sc_num_reply_frames +
1329 1.15 bouyer 16 - (sc->sc_num_reply_frames % 16);
1330 1.1 bouyer
1331 1.1 bouyer /*
1332 1.15 bouyer * Our request frame for an I/O operation looks like this:
1333 1.15 bouyer *
1334 1.15 bouyer * +-------------------+ -.
1335 1.15 bouyer * | mpii_msg_scsi_io | |
1336 1.15 bouyer * +-------------------| |
1337 1.15 bouyer * | mpii_sge | |
1338 1.15 bouyer * + - - - - - - - - - + |
1339 1.15 bouyer * | ... | > ioc_request_frame_size
1340 1.15 bouyer * + - - - - - - - - - + |
1341 1.15 bouyer * | mpii_sge (tail) | |
1342 1.15 bouyer * + - - - - - - - - - + |
1343 1.15 bouyer * | mpii_sge (csge) | | --.
1344 1.15 bouyer * + - - - - - - - - - + -' | chain sge points to the next sge
1345 1.15 bouyer * | mpii_sge |<-----'
1346 1.15 bouyer * + - - - - - - - - - +
1347 1.15 bouyer * | ... |
1348 1.15 bouyer * + - - - - - - - - - +
1349 1.15 bouyer * | mpii_sge (tail) |
1350 1.15 bouyer * +-------------------+
1351 1.15 bouyer * | |
1352 1.15 bouyer * ~~~~~~~~~~~~~~~~~~~~~
1353 1.15 bouyer * | |
1354 1.15 bouyer * +-------------------+ <- sc_request_size - sizeof(scsi_sense_data)
1355 1.15 bouyer * | scsi_sense_data |
1356 1.15 bouyer * +-------------------+
1357 1.1 bouyer */
1358 1.1 bouyer
1359 1.15 bouyer /* both sizes are in 32-bit words */
1360 1.15 bouyer sc->sc_reply_size = ifp.reply_frame_size * 4;
1361 1.15 bouyer irs = le16toh(ifp.ioc_request_frame_size) * 4;
1362 1.15 bouyer sc->sc_request_size = MPII_REQUEST_SIZE;
1363 1.15 bouyer /* make sure we have enough space for scsi sense data */
1364 1.15 bouyer if (irs > sc->sc_request_size) {
1365 1.15 bouyer sc->sc_request_size = irs + sizeof(struct scsi_sense_data);
1366 1.15 bouyer sc->sc_request_size += 16 - (sc->sc_request_size % 16);
1367 1.15 bouyer }
1368 1.15 bouyer
1369 1.15 bouyer if (ISSET(sc->sc_flags, MPII_F_SAS3)) {
1370 1.15 bouyer sge_size = sizeof(struct mpii_ieee_sge);
1371 1.15 bouyer } else {
1372 1.15 bouyer sge_size = sizeof(struct mpii_sge);
1373 1.15 bouyer }
1374 1.1 bouyer
1375 1.15 bouyer /* offset to the chain sge */
1376 1.15 bouyer sc->sc_chain_sge = (irs - sizeof(struct mpii_msg_scsi_io)) /
1377 1.15 bouyer sge_size - 1;
1378 1.1 bouyer
1379 1.15 bouyer /*
1380 1.15 bouyer * A number of simple scatter-gather elements we can fit into the
1381 1.15 bouyer * request buffer after the I/O command minus the chain element.
1382 1.15 bouyer */
1383 1.15 bouyer sc->sc_max_sgl = (sc->sc_request_size -
1384 1.15 bouyer sizeof(struct mpii_msg_scsi_io) - sizeof(struct scsi_sense_data)) /
1385 1.15 bouyer sge_size - 1;
1386 1.1 bouyer
1387 1.15 bouyer return (0);
1388 1.1 bouyer }
1389 1.1 bouyer
1390 1.24 maxv static int
1391 1.1 bouyer mpii_iocinit(struct mpii_softc *sc)
1392 1.1 bouyer {
1393 1.1 bouyer struct mpii_msg_iocinit_request iiq;
1394 1.1 bouyer struct mpii_msg_iocinit_reply iip;
1395 1.1 bouyer
1396 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit\n", DEVNAME(sc));
1397 1.1 bouyer
1398 1.15 bouyer memset(&iiq, 0, sizeof(iiq));
1399 1.15 bouyer memset(&iip, 0, sizeof(iip));
1400 1.1 bouyer
1401 1.1 bouyer iiq.function = MPII_FUNCTION_IOC_INIT;
1402 1.1 bouyer iiq.whoinit = MPII_WHOINIT_HOST_DRIVER;
1403 1.15 bouyer
1404 1.1 bouyer /* XXX JPG do something about vf_id */
1405 1.1 bouyer iiq.vf_id = 0;
1406 1.1 bouyer
1407 1.1 bouyer iiq.msg_version_maj = 0x02;
1408 1.1 bouyer iiq.msg_version_min = 0x00;
1409 1.1 bouyer
1410 1.1 bouyer /* XXX JPG ensure compliance with some level and hard-code? */
1411 1.1 bouyer iiq.hdr_version_unit = 0x00;
1412 1.1 bouyer iiq.hdr_version_dev = 0x00;
1413 1.1 bouyer
1414 1.15 bouyer iiq.system_request_frame_size = htole16(sc->sc_request_size / 4);
1415 1.1 bouyer
1416 1.1 bouyer iiq.reply_descriptor_post_queue_depth =
1417 1.1 bouyer htole16(sc->sc_reply_post_qdepth);
1418 1.1 bouyer
1419 1.1 bouyer iiq.reply_free_queue_depth = htole16(sc->sc_reply_free_qdepth);
1420 1.1 bouyer
1421 1.15 bouyer iiq.sense_buffer_address_high =
1422 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_requests) >> 32);
1423 1.1 bouyer
1424 1.15 bouyer iiq.system_reply_address_high =
1425 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_replies) >> 32);
1426 1.1 bouyer
1427 1.15 bouyer iiq.system_request_frame_base_address_lo =
1428 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_requests));
1429 1.15 bouyer iiq.system_request_frame_base_address_hi =
1430 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_requests) >> 32);
1431 1.15 bouyer
1432 1.15 bouyer iiq.reply_descriptor_post_queue_address_lo =
1433 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_reply_postq));
1434 1.15 bouyer iiq.reply_descriptor_post_queue_address_hi =
1435 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_reply_postq) >> 32);
1436 1.15 bouyer
1437 1.15 bouyer iiq.reply_free_queue_address_lo =
1438 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_reply_freeq));
1439 1.15 bouyer iiq.reply_free_queue_address_hi =
1440 1.15 bouyer htole32(MPII_DMA_DVA(sc->sc_reply_freeq) >> 32);
1441 1.1 bouyer
1442 1.1 bouyer if (mpii_handshake_send(sc, &iiq, dwordsof(iiq)) != 0) {
1443 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit send failed\n",
1444 1.1 bouyer DEVNAME(sc));
1445 1.1 bouyer return (1);
1446 1.1 bouyer }
1447 1.1 bouyer
1448 1.1 bouyer if (mpii_handshake_recv(sc, &iip, dwordsof(iip)) != 0) {
1449 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_iocinit recv failed\n",
1450 1.1 bouyer DEVNAME(sc));
1451 1.1 bouyer return (1);
1452 1.1 bouyer }
1453 1.1 bouyer
1454 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: function: 0x%02x msg_length: %d "
1455 1.1 bouyer "whoinit: 0x%02x\n", DEVNAME(sc), iip.function,
1456 1.1 bouyer iip.msg_length, iip.whoinit);
1457 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: msg_flags: 0x%02x\n", DEVNAME(sc),
1458 1.1 bouyer iip.msg_flags);
1459 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: vf_id: 0x%02x vp_id: 0x%02x\n", DEVNAME(sc),
1460 1.1 bouyer iip.vf_id, iip.vp_id);
1461 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ioc_status: 0x%04x\n", DEVNAME(sc),
1462 1.1 bouyer le16toh(iip.ioc_status));
1463 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ioc_loginfo: 0x%08x\n", DEVNAME(sc),
1464 1.1 bouyer le32toh(iip.ioc_loginfo));
1465 1.1 bouyer
1466 1.15 bouyer if (le16toh(iip.ioc_status) != MPII_IOCSTATUS_SUCCESS ||
1467 1.15 bouyer le32toh(iip.ioc_loginfo))
1468 1.1 bouyer return (1);
1469 1.1 bouyer
1470 1.1 bouyer return (0);
1471 1.1 bouyer }
1472 1.1 bouyer
1473 1.24 maxv static void
1474 1.1 bouyer mpii_push_reply(struct mpii_softc *sc, struct mpii_rcb *rcb)
1475 1.1 bouyer {
1476 1.1 bouyer u_int32_t *rfp;
1477 1.15 bouyer u_int idx;
1478 1.1 bouyer
1479 1.1 bouyer if (rcb == NULL)
1480 1.1 bouyer return;
1481 1.1 bouyer
1482 1.15 bouyer mutex_enter(&sc->sc_reply_free_mtx);
1483 1.15 bouyer idx = sc->sc_reply_free_host_index;
1484 1.15 bouyer
1485 1.1 bouyer rfp = MPII_DMA_KVA(sc->sc_reply_freeq);
1486 1.15 bouyer rfp[idx] = htole32(rcb->rcb_reply_dva);
1487 1.1 bouyer
1488 1.15 bouyer if (++idx >= sc->sc_reply_free_qdepth)
1489 1.15 bouyer idx = 0;
1490 1.1 bouyer
1491 1.15 bouyer mpii_write_reply_free(sc, sc->sc_reply_free_host_index = idx);
1492 1.15 bouyer mutex_exit(&sc->sc_reply_free_mtx);
1493 1.1 bouyer }
1494 1.1 bouyer
1495 1.24 maxv static int
1496 1.1 bouyer mpii_portfacts(struct mpii_softc *sc)
1497 1.1 bouyer {
1498 1.1 bouyer struct mpii_msg_portfacts_request *pfq;
1499 1.1 bouyer struct mpii_msg_portfacts_reply *pfp;
1500 1.1 bouyer struct mpii_ccb *ccb;
1501 1.1 bouyer int rv = 1;
1502 1.1 bouyer
1503 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts\n", DEVNAME(sc));
1504 1.1 bouyer
1505 1.15 bouyer ccb = mpii_get_ccb(sc);
1506 1.1 bouyer if (ccb == NULL) {
1507 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts mpii_get_ccb fail\n",
1508 1.1 bouyer DEVNAME(sc));
1509 1.1 bouyer return (rv);
1510 1.1 bouyer }
1511 1.1 bouyer
1512 1.1 bouyer ccb->ccb_done = mpii_empty_done;
1513 1.1 bouyer pfq = ccb->ccb_cmd;
1514 1.1 bouyer
1515 1.15 bouyer memset(pfq, 0, sizeof(*pfq));
1516 1.1 bouyer
1517 1.1 bouyer pfq->function = MPII_FUNCTION_PORT_FACTS;
1518 1.1 bouyer pfq->chain_offset = 0;
1519 1.1 bouyer pfq->msg_flags = 0;
1520 1.1 bouyer pfq->port_number = 0;
1521 1.1 bouyer pfq->vp_id = 0;
1522 1.1 bouyer pfq->vf_id = 0;
1523 1.1 bouyer
1524 1.1 bouyer if (mpii_poll(sc, ccb) != 0) {
1525 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_portfacts poll\n",
1526 1.1 bouyer DEVNAME(sc));
1527 1.1 bouyer goto err;
1528 1.1 bouyer }
1529 1.1 bouyer
1530 1.1 bouyer if (ccb->ccb_rcb == NULL) {
1531 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: empty portfacts reply\n",
1532 1.1 bouyer DEVNAME(sc));
1533 1.1 bouyer goto err;
1534 1.1 bouyer }
1535 1.1 bouyer
1536 1.1 bouyer pfp = ccb->ccb_rcb->rcb_reply;
1537 1.1 bouyer sc->sc_porttype = pfp->port_type;
1538 1.1 bouyer
1539 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
1540 1.1 bouyer rv = 0;
1541 1.1 bouyer err:
1542 1.1 bouyer mpii_put_ccb(sc, ccb);
1543 1.1 bouyer
1544 1.1 bouyer return (rv);
1545 1.1 bouyer }
1546 1.1 bouyer
1547 1.24 maxv static void
1548 1.15 bouyer mpii_eventack(struct work *wk, void * cookie)
1549 1.1 bouyer {
1550 1.1 bouyer struct mpii_softc *sc = cookie;
1551 1.1 bouyer struct mpii_ccb *ccb;
1552 1.15 bouyer struct mpii_rcb *rcb, *next;
1553 1.1 bouyer struct mpii_msg_event_reply *enp;
1554 1.1 bouyer struct mpii_msg_eventack_request *eaq;
1555 1.1 bouyer
1556 1.15 bouyer mutex_enter(&sc->sc_evt_ack_mtx);
1557 1.15 bouyer next = SIMPLEQ_FIRST(&sc->sc_evt_ack_queue);
1558 1.15 bouyer SIMPLEQ_INIT(&sc->sc_evt_ack_queue);
1559 1.15 bouyer mutex_exit(&sc->sc_evt_ack_mtx);
1560 1.1 bouyer
1561 1.15 bouyer while (next != NULL) {
1562 1.15 bouyer rcb = next;
1563 1.15 bouyer next = SIMPLEQ_NEXT(rcb, rcb_link);
1564 1.1 bouyer
1565 1.15 bouyer enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
1566 1.1 bouyer
1567 1.15 bouyer ccb = mpii_get_ccb(sc);
1568 1.15 bouyer ccb->ccb_done = mpii_eventack_done;
1569 1.15 bouyer eaq = ccb->ccb_cmd;
1570 1.1 bouyer
1571 1.15 bouyer eaq->function = MPII_FUNCTION_EVENT_ACK;
1572 1.1 bouyer
1573 1.15 bouyer eaq->event = enp->event;
1574 1.15 bouyer eaq->event_context = enp->event_context;
1575 1.1 bouyer
1576 1.15 bouyer mpii_push_reply(sc, rcb);
1577 1.1 bouyer
1578 1.15 bouyer mpii_start(sc, ccb);
1579 1.15 bouyer }
1580 1.1 bouyer }
1581 1.1 bouyer
1582 1.24 maxv static void
1583 1.1 bouyer mpii_eventack_done(struct mpii_ccb *ccb)
1584 1.1 bouyer {
1585 1.1 bouyer struct mpii_softc *sc = ccb->ccb_sc;
1586 1.1 bouyer
1587 1.1 bouyer DNPRINTF(MPII_D_EVT, "%s: event ack done\n", DEVNAME(sc));
1588 1.1 bouyer
1589 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
1590 1.1 bouyer mpii_put_ccb(sc, ccb);
1591 1.1 bouyer }
1592 1.1 bouyer
1593 1.24 maxv static int
1594 1.1 bouyer mpii_portenable(struct mpii_softc *sc)
1595 1.1 bouyer {
1596 1.1 bouyer struct mpii_msg_portenable_request *peq;
1597 1.1 bouyer struct mpii_ccb *ccb;
1598 1.1 bouyer
1599 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_portenable\n", DEVNAME(sc));
1600 1.1 bouyer
1601 1.15 bouyer ccb = mpii_get_ccb(sc);
1602 1.1 bouyer if (ccb == NULL) {
1603 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_portenable ccb_get\n",
1604 1.1 bouyer DEVNAME(sc));
1605 1.1 bouyer return (1);
1606 1.1 bouyer }
1607 1.1 bouyer
1608 1.1 bouyer ccb->ccb_done = mpii_empty_done;
1609 1.1 bouyer peq = ccb->ccb_cmd;
1610 1.1 bouyer
1611 1.1 bouyer peq->function = MPII_FUNCTION_PORT_ENABLE;
1612 1.1 bouyer peq->vf_id = sc->sc_vf_id;
1613 1.1 bouyer
1614 1.1 bouyer if (mpii_poll(sc, ccb) != 0) {
1615 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_portenable poll\n",
1616 1.1 bouyer DEVNAME(sc));
1617 1.1 bouyer return (1);
1618 1.1 bouyer }
1619 1.1 bouyer
1620 1.1 bouyer if (ccb->ccb_rcb == NULL) {
1621 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: empty portenable reply\n",
1622 1.1 bouyer DEVNAME(sc));
1623 1.1 bouyer return (1);
1624 1.1 bouyer }
1625 1.1 bouyer
1626 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
1627 1.1 bouyer mpii_put_ccb(sc, ccb);
1628 1.1 bouyer
1629 1.1 bouyer return (0);
1630 1.1 bouyer }
1631 1.1 bouyer
1632 1.24 maxv static int
1633 1.1 bouyer mpii_cfg_coalescing(struct mpii_softc *sc)
1634 1.1 bouyer {
1635 1.15 bouyer struct mpii_cfg_hdr hdr;
1636 1.15 bouyer struct mpii_cfg_ioc_pg1 ipg;
1637 1.1 bouyer
1638 1.15 bouyer hdr.page_version = 0;
1639 1.15 bouyer hdr.page_length = sizeof(ipg) / 4;
1640 1.15 bouyer hdr.page_number = 1;
1641 1.15 bouyer hdr.page_type = MPII_CONFIG_REQ_PAGE_TYPE_IOC;
1642 1.15 bouyer memset(&ipg, 0, sizeof(ipg));
1643 1.15 bouyer if (mpii_req_cfg_page(sc, 0, MPII_PG_POLL, &hdr, 1, &ipg,
1644 1.15 bouyer sizeof(ipg)) != 0) {
1645 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: unable to fetch IOC page 1\n"
1646 1.1 bouyer "page 1\n", DEVNAME(sc));
1647 1.1 bouyer return (1);
1648 1.1 bouyer }
1649 1.1 bouyer
1650 1.15 bouyer if (!ISSET(le32toh(ipg.flags), MPII_CFG_IOC_1_REPLY_COALESCING))
1651 1.1 bouyer return (0);
1652 1.1 bouyer
1653 1.15 bouyer /* Disable coalescing */
1654 1.15 bouyer CLR(ipg.flags, htole32(MPII_CFG_IOC_1_REPLY_COALESCING));
1655 1.15 bouyer if (mpii_req_cfg_page(sc, 0, MPII_PG_POLL, &hdr, 0, &ipg,
1656 1.15 bouyer sizeof(ipg)) != 0) {
1657 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: unable to clear coalescing\n",
1658 1.1 bouyer DEVNAME(sc));
1659 1.1 bouyer return (1);
1660 1.1 bouyer }
1661 1.1 bouyer
1662 1.1 bouyer return (0);
1663 1.1 bouyer }
1664 1.1 bouyer
1665 1.1 bouyer #define MPII_EVENT_MASKALL(enq) do { \
1666 1.1 bouyer enq->event_masks[0] = 0xffffffff; \
1667 1.1 bouyer enq->event_masks[1] = 0xffffffff; \
1668 1.1 bouyer enq->event_masks[2] = 0xffffffff; \
1669 1.1 bouyer enq->event_masks[3] = 0xffffffff; \
1670 1.1 bouyer } while (0)
1671 1.1 bouyer
1672 1.1 bouyer #define MPII_EVENT_UNMASK(enq, evt) do { \
1673 1.1 bouyer enq->event_masks[evt / 32] &= \
1674 1.1 bouyer htole32(~(1 << (evt % 32))); \
1675 1.1 bouyer } while (0)
1676 1.1 bouyer
1677 1.24 maxv static int
1678 1.1 bouyer mpii_eventnotify(struct mpii_softc *sc)
1679 1.1 bouyer {
1680 1.1 bouyer struct mpii_msg_event_request *enq;
1681 1.1 bouyer struct mpii_ccb *ccb;
1682 1.15 bouyer char wkname[15];
1683 1.1 bouyer
1684 1.15 bouyer ccb = mpii_get_ccb(sc);
1685 1.1 bouyer if (ccb == NULL) {
1686 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_eventnotify ccb_get\n",
1687 1.1 bouyer DEVNAME(sc));
1688 1.1 bouyer return (1);
1689 1.1 bouyer }
1690 1.1 bouyer
1691 1.15 bouyer SIMPLEQ_INIT(&sc->sc_evt_sas_queue);
1692 1.15 bouyer mutex_init(&sc->sc_evt_sas_mtx, MUTEX_DEFAULT, IPL_BIO);
1693 1.15 bouyer snprintf(wkname, sizeof(wkname), "%ssas", DEVNAME(sc));
1694 1.15 bouyer if (workqueue_create(&sc->sc_evt_sas_wq, wkname,
1695 1.15 bouyer mpii_event_sas_work, sc, PRI_NONE, IPL_BIO, WQ_MPSAFE) != 0) {
1696 1.15 bouyer mpii_put_ccb(sc, ccb);
1697 1.15 bouyer aprint_error_dev(sc->sc_dev,
1698 1.15 bouyer "can't create %s workqueue\n", wkname);
1699 1.15 bouyer return 1;
1700 1.15 bouyer }
1701 1.15 bouyer
1702 1.15 bouyer SIMPLEQ_INIT(&sc->sc_evt_ack_queue);
1703 1.15 bouyer mutex_init(&sc->sc_evt_ack_mtx, MUTEX_DEFAULT, IPL_BIO);
1704 1.15 bouyer snprintf(wkname, sizeof(wkname), "%sevt", DEVNAME(sc));
1705 1.15 bouyer if (workqueue_create(&sc->sc_evt_ack_wq, wkname,
1706 1.15 bouyer mpii_eventack, sc, PRI_NONE, IPL_BIO, WQ_MPSAFE) != 0) {
1707 1.15 bouyer mpii_put_ccb(sc, ccb);
1708 1.15 bouyer aprint_error_dev(sc->sc_dev,
1709 1.15 bouyer "can't create %s workqueue\n", wkname);
1710 1.15 bouyer return 1;
1711 1.15 bouyer }
1712 1.15 bouyer
1713 1.1 bouyer ccb->ccb_done = mpii_eventnotify_done;
1714 1.1 bouyer enq = ccb->ccb_cmd;
1715 1.1 bouyer
1716 1.1 bouyer enq->function = MPII_FUNCTION_EVENT_NOTIFICATION;
1717 1.1 bouyer
1718 1.1 bouyer /*
1719 1.1 bouyer * Enable reporting of the following events:
1720 1.1 bouyer *
1721 1.1 bouyer * MPII_EVENT_SAS_DISCOVERY
1722 1.1 bouyer * MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST
1723 1.1 bouyer * MPII_EVENT_SAS_DEVICE_STATUS_CHANGE
1724 1.1 bouyer * MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE
1725 1.1 bouyer * MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST
1726 1.1 bouyer * MPII_EVENT_IR_VOLUME
1727 1.1 bouyer * MPII_EVENT_IR_PHYSICAL_DISK
1728 1.1 bouyer * MPII_EVENT_IR_OPERATION_STATUS
1729 1.1 bouyer */
1730 1.1 bouyer
1731 1.1 bouyer MPII_EVENT_MASKALL(enq);
1732 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_DISCOVERY);
1733 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
1734 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_DEVICE_STATUS_CHANGE);
1735 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
1736 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST);
1737 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_VOLUME);
1738 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_PHYSICAL_DISK);
1739 1.1 bouyer MPII_EVENT_UNMASK(enq, MPII_EVENT_IR_OPERATION_STATUS);
1740 1.1 bouyer
1741 1.1 bouyer mpii_start(sc, ccb);
1742 1.1 bouyer
1743 1.1 bouyer return (0);
1744 1.1 bouyer }
1745 1.1 bouyer
1746 1.24 maxv static void
1747 1.1 bouyer mpii_eventnotify_done(struct mpii_ccb *ccb)
1748 1.1 bouyer {
1749 1.1 bouyer struct mpii_softc *sc = ccb->ccb_sc;
1750 1.1 bouyer struct mpii_rcb *rcb = ccb->ccb_rcb;
1751 1.1 bouyer
1752 1.1 bouyer DNPRINTF(MPII_D_EVT, "%s: mpii_eventnotify_done\n", DEVNAME(sc));
1753 1.1 bouyer
1754 1.1 bouyer mpii_put_ccb(sc, ccb);
1755 1.1 bouyer mpii_event_process(sc, rcb);
1756 1.1 bouyer }
1757 1.1 bouyer
1758 1.24 maxv static void
1759 1.1 bouyer mpii_event_raid(struct mpii_softc *sc, struct mpii_msg_event_reply *enp)
1760 1.1 bouyer {
1761 1.1 bouyer struct mpii_evt_ir_cfg_change_list *ccl;
1762 1.1 bouyer struct mpii_evt_ir_cfg_element *ce;
1763 1.1 bouyer struct mpii_device *dev;
1764 1.1 bouyer u_int16_t type;
1765 1.1 bouyer int i;
1766 1.1 bouyer
1767 1.1 bouyer ccl = (struct mpii_evt_ir_cfg_change_list *)(enp + 1);
1768 1.1 bouyer if (ccl->num_elements == 0)
1769 1.1 bouyer return;
1770 1.15 bouyer
1771 1.15 bouyer if (ISSET(le32toh(ccl->flags), MPII_EVT_IR_CFG_CHANGE_LIST_FOREIGN)) {
1772 1.1 bouyer /* bail on foreign configurations */
1773 1.1 bouyer return;
1774 1.15 bouyer }
1775 1.1 bouyer
1776 1.1 bouyer ce = (struct mpii_evt_ir_cfg_element *)(ccl + 1);
1777 1.1 bouyer
1778 1.1 bouyer for (i = 0; i < ccl->num_elements; i++, ce++) {
1779 1.1 bouyer type = (le16toh(ce->element_flags) &
1780 1.1 bouyer MPII_EVT_IR_CFG_ELEMENT_TYPE_MASK);
1781 1.1 bouyer
1782 1.1 bouyer switch (type) {
1783 1.1 bouyer case MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME:
1784 1.1 bouyer switch (ce->reason_code) {
1785 1.1 bouyer case MPII_EVT_IR_CFG_ELEMENT_RC_ADDED:
1786 1.1 bouyer case MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_CREATED:
1787 1.15 bouyer dev = malloc(sizeof(*dev), M_DEVBUF,
1788 1.23 chs M_WAITOK | M_ZERO);
1789 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
1790 1.1 bouyer if (mpii_find_dev(sc,
1791 1.1 bouyer le16toh(ce->vol_dev_handle))) {
1792 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1793 1.15 bouyer free(dev, M_DEVBUF);
1794 1.15 bouyer printf("%s: device %#x is already "
1795 1.15 bouyer "configured\n", DEVNAME(sc),
1796 1.1 bouyer le16toh(ce->vol_dev_handle));
1797 1.1 bouyer break;
1798 1.1 bouyer }
1799 1.1 bouyer SET(dev->flags, MPII_DF_VOLUME);
1800 1.1 bouyer dev->slot = sc->sc_vd_id_low;
1801 1.1 bouyer dev->dev_handle = le16toh(ce->vol_dev_handle);
1802 1.1 bouyer if (mpii_insert_dev(sc, dev)) {
1803 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1804 1.1 bouyer free(dev, M_DEVBUF);
1805 1.1 bouyer break;
1806 1.1 bouyer }
1807 1.1 bouyer sc->sc_vd_count++;
1808 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1809 1.1 bouyer break;
1810 1.1 bouyer case MPII_EVT_IR_CFG_ELEMENT_RC_REMOVED:
1811 1.1 bouyer case MPII_EVT_IR_CFG_ELEMENT_RC_VOLUME_DELETED:
1812 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
1813 1.1 bouyer if (!(dev = mpii_find_dev(sc,
1814 1.15 bouyer le16toh(ce->vol_dev_handle)))) {
1815 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1816 1.1 bouyer break;
1817 1.15 bouyer }
1818 1.1 bouyer mpii_remove_dev(sc, dev);
1819 1.1 bouyer sc->sc_vd_count--;
1820 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1821 1.1 bouyer break;
1822 1.1 bouyer }
1823 1.1 bouyer break;
1824 1.1 bouyer case MPII_EVT_IR_CFG_ELEMENT_TYPE_VOLUME_DISK:
1825 1.1 bouyer if (ce->reason_code ==
1826 1.1 bouyer MPII_EVT_IR_CFG_ELEMENT_RC_PD_CREATED ||
1827 1.1 bouyer ce->reason_code ==
1828 1.1 bouyer MPII_EVT_IR_CFG_ELEMENT_RC_HIDE) {
1829 1.1 bouyer /* there should be an underlying sas drive */
1830 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
1831 1.1 bouyer if (!(dev = mpii_find_dev(sc,
1832 1.15 bouyer le16toh(ce->phys_disk_dev_handle)))) {
1833 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1834 1.1 bouyer break;
1835 1.15 bouyer }
1836 1.1 bouyer /* promoted from a hot spare? */
1837 1.1 bouyer CLR(dev->flags, MPII_DF_HOT_SPARE);
1838 1.1 bouyer SET(dev->flags, MPII_DF_VOLUME_DISK |
1839 1.1 bouyer MPII_DF_HIDDEN);
1840 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1841 1.1 bouyer }
1842 1.1 bouyer break;
1843 1.1 bouyer case MPII_EVT_IR_CFG_ELEMENT_TYPE_HOT_SPARE:
1844 1.1 bouyer if (ce->reason_code ==
1845 1.1 bouyer MPII_EVT_IR_CFG_ELEMENT_RC_HIDE) {
1846 1.1 bouyer /* there should be an underlying sas drive */
1847 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
1848 1.1 bouyer if (!(dev = mpii_find_dev(sc,
1849 1.15 bouyer le16toh(ce->phys_disk_dev_handle)))) {
1850 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1851 1.1 bouyer break;
1852 1.15 bouyer }
1853 1.1 bouyer SET(dev->flags, MPII_DF_HOT_SPARE |
1854 1.1 bouyer MPII_DF_HIDDEN);
1855 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1856 1.1 bouyer }
1857 1.1 bouyer break;
1858 1.1 bouyer }
1859 1.1 bouyer }
1860 1.1 bouyer }
1861 1.1 bouyer
1862 1.24 maxv static void
1863 1.15 bouyer mpii_event_sas(struct mpii_softc *sc, struct mpii_rcb *rcb)
1864 1.1 bouyer {
1865 1.15 bouyer struct mpii_msg_event_reply *enp;
1866 1.1 bouyer struct mpii_evt_sas_tcl *tcl;
1867 1.1 bouyer struct mpii_evt_phy_entry *pe;
1868 1.1 bouyer struct mpii_device *dev;
1869 1.1 bouyer int i;
1870 1.15 bouyer u_int16_t handle;
1871 1.15 bouyer int need_queue = 0;
1872 1.15 bouyer
1873 1.15 bouyer enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
1874 1.15 bouyer DNPRINTF(MPII_D_EVT, "%s: mpii_event_sas 0x%x\n",
1875 1.15 bouyer DEVNAME(sc), le16toh(enp->event));
1876 1.15 bouyer KASSERT(le16toh(enp->event) == MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
1877 1.1 bouyer
1878 1.1 bouyer tcl = (struct mpii_evt_sas_tcl *)(enp + 1);
1879 1.1 bouyer pe = (struct mpii_evt_phy_entry *)(tcl + 1);
1880 1.1 bouyer
1881 1.1 bouyer for (i = 0; i < tcl->num_entries; i++, pe++) {
1882 1.15 bouyer DNPRINTF(MPII_D_EVT, "%s: sas change %d stat %d h %d slot %d phy %d enc %d expand %d\n",
1883 1.15 bouyer DEVNAME(sc), i, pe->phy_status,
1884 1.15 bouyer le16toh(pe->dev_handle),
1885 1.15 bouyer sc->sc_pd_id_start + tcl->start_phy_num + i,
1886 1.15 bouyer tcl->start_phy_num + i, le16toh(tcl->enclosure_handle), le16toh(tcl->expander_handle));
1887 1.15 bouyer
1888 1.1 bouyer switch (pe->phy_status & MPII_EVENT_SAS_TOPO_PS_RC_MASK) {
1889 1.1 bouyer case MPII_EVENT_SAS_TOPO_PS_RC_ADDED:
1890 1.15 bouyer handle = le16toh(pe->dev_handle);
1891 1.15 bouyer DNPRINTF(MPII_D_EVT, "%s: sas add handle %d\n",
1892 1.15 bouyer DEVNAME(sc), handle);
1893 1.15 bouyer dev = malloc(sizeof(*dev), M_DEVBUF, M_WAITOK | M_ZERO);
1894 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
1895 1.15 bouyer if (mpii_find_dev(sc, handle)) {
1896 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1897 1.15 bouyer free(dev, M_DEVBUF);
1898 1.15 bouyer printf("%s: device %#x is already "
1899 1.15 bouyer "configured\n", DEVNAME(sc), handle);
1900 1.1 bouyer break;
1901 1.1 bouyer }
1902 1.15 bouyer
1903 1.1 bouyer dev->slot = sc->sc_pd_id_start + tcl->start_phy_num + i;
1904 1.15 bouyer dev->dev_handle = handle;
1905 1.1 bouyer dev->phy_num = tcl->start_phy_num + i;
1906 1.1 bouyer if (tcl->enclosure_handle)
1907 1.1 bouyer dev->physical_port = tcl->physical_port;
1908 1.1 bouyer dev->enclosure = le16toh(tcl->enclosure_handle);
1909 1.1 bouyer dev->expander = le16toh(tcl->expander_handle);
1910 1.15 bouyer
1911 1.1 bouyer if (mpii_insert_dev(sc, dev)) {
1912 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1913 1.1 bouyer free(dev, M_DEVBUF);
1914 1.1 bouyer break;
1915 1.1 bouyer }
1916 1.22 kardel printf("%s: physical device inserted in slot %d\n",
1917 1.15 bouyer DEVNAME(sc), dev->slot);
1918 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1919 1.1 bouyer break;
1920 1.15 bouyer
1921 1.1 bouyer case MPII_EVENT_SAS_TOPO_PS_RC_MISSING:
1922 1.15 bouyer /* defer to workqueue thread */
1923 1.15 bouyer need_queue++;
1924 1.15 bouyer break;
1925 1.15 bouyer }
1926 1.15 bouyer }
1927 1.15 bouyer
1928 1.15 bouyer if (need_queue) {
1929 1.15 bouyer bool start_wk;
1930 1.15 bouyer mutex_enter(&sc->sc_evt_sas_mtx);
1931 1.15 bouyer start_wk = (SIMPLEQ_FIRST(&sc->sc_evt_sas_queue) == 0);
1932 1.15 bouyer SIMPLEQ_INSERT_TAIL(&sc->sc_evt_sas_queue, rcb, rcb_link);
1933 1.15 bouyer if (start_wk) {
1934 1.15 bouyer workqueue_enqueue(sc->sc_evt_sas_wq,
1935 1.15 bouyer &sc->sc_evt_sas_work, NULL);
1936 1.15 bouyer }
1937 1.15 bouyer mutex_exit(&sc->sc_evt_sas_mtx);
1938 1.15 bouyer } else
1939 1.15 bouyer mpii_event_done(sc, rcb);
1940 1.15 bouyer }
1941 1.15 bouyer
1942 1.24 maxv static void
1943 1.15 bouyer mpii_event_sas_work(struct work *wq, void *xsc)
1944 1.15 bouyer {
1945 1.15 bouyer struct mpii_softc *sc = xsc;
1946 1.15 bouyer struct mpii_rcb *rcb, *next;
1947 1.15 bouyer struct mpii_msg_event_reply *enp;
1948 1.15 bouyer struct mpii_evt_sas_tcl *tcl;
1949 1.15 bouyer struct mpii_evt_phy_entry *pe;
1950 1.15 bouyer struct mpii_device *dev;
1951 1.15 bouyer int i;
1952 1.15 bouyer
1953 1.15 bouyer mutex_enter(&sc->sc_evt_sas_mtx);
1954 1.15 bouyer next = SIMPLEQ_FIRST(&sc->sc_evt_sas_queue);
1955 1.15 bouyer SIMPLEQ_INIT(&sc->sc_evt_sas_queue);
1956 1.15 bouyer mutex_exit(&sc->sc_evt_sas_mtx);
1957 1.15 bouyer
1958 1.15 bouyer while (next != NULL) {
1959 1.15 bouyer rcb = next;
1960 1.15 bouyer next = SIMPLEQ_NEXT(rcb, rcb_link);
1961 1.15 bouyer
1962 1.15 bouyer enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
1963 1.15 bouyer DNPRINTF(MPII_D_EVT, "%s: mpii_event_sas_work 0x%x\n",
1964 1.15 bouyer DEVNAME(sc), le16toh(enp->event));
1965 1.15 bouyer KASSERT(le16toh(enp->event) == MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
1966 1.15 bouyer tcl = (struct mpii_evt_sas_tcl *)(enp + 1);
1967 1.15 bouyer pe = (struct mpii_evt_phy_entry *)(tcl + 1);
1968 1.15 bouyer
1969 1.15 bouyer for (i = 0; i < tcl->num_entries; i++, pe++) {
1970 1.15 bouyer DNPRINTF(MPII_D_EVT, "%s: sas change %d stat %d h %d slot %d phy %d enc %d expand %d\n",
1971 1.15 bouyer DEVNAME(sc), i, pe->phy_status,
1972 1.15 bouyer le16toh(pe->dev_handle),
1973 1.15 bouyer sc->sc_pd_id_start + tcl->start_phy_num + i,
1974 1.15 bouyer tcl->start_phy_num + i, le16toh(tcl->enclosure_handle), le16toh(tcl->expander_handle));
1975 1.15 bouyer
1976 1.15 bouyer switch (pe->phy_status & MPII_EVENT_SAS_TOPO_PS_RC_MASK) {
1977 1.15 bouyer case MPII_EVENT_SAS_TOPO_PS_RC_ADDED:
1978 1.15 bouyer /* already handled */
1979 1.15 bouyer break;
1980 1.15 bouyer
1981 1.15 bouyer case MPII_EVENT_SAS_TOPO_PS_RC_MISSING:
1982 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
1983 1.15 bouyer dev = mpii_find_dev(sc, le16toh(pe->dev_handle));
1984 1.15 bouyer if (dev == NULL) {
1985 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1986 1.15 bouyer break;
1987 1.15 bouyer }
1988 1.15 bouyer
1989 1.15 bouyer printf(
1990 1.22 kardel "%s: physical device removed from slot %d\n",
1991 1.15 bouyer DEVNAME(sc), dev->slot);
1992 1.15 bouyer mpii_remove_dev(sc, dev);
1993 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
1994 1.15 bouyer mpii_sas_remove_device(sc, dev->dev_handle);
1995 1.15 bouyer if (!ISSET(dev->flags, MPII_DF_HIDDEN)) {
1996 1.15 bouyer scsipi_target_detach(&sc->sc_chan,
1997 1.15 bouyer dev->slot, 0, DETACH_FORCE);
1998 1.15 bouyer }
1999 1.15 bouyer
2000 1.15 bouyer free(dev, M_DEVBUF);
2001 1.1 bouyer break;
2002 1.1 bouyer }
2003 1.1 bouyer }
2004 1.15 bouyer mpii_event_done(sc, rcb);
2005 1.15 bouyer }
2006 1.15 bouyer }
2007 1.15 bouyer
2008 1.24 maxv static void
2009 1.15 bouyer mpii_event_discovery(struct mpii_softc *sc, struct mpii_msg_event_reply *enp)
2010 1.15 bouyer {
2011 1.15 bouyer struct mpii_evt_sas_discovery *esd =
2012 1.15 bouyer (struct mpii_evt_sas_discovery *)(enp + 1);
2013 1.15 bouyer
2014 1.15 bouyer if (esd->reason_code == MPII_EVENT_SAS_DISC_REASON_CODE_COMPLETED) {
2015 1.15 bouyer if (esd->discovery_status != 0) {
2016 1.15 bouyer printf("%s: sas discovery completed with status %#x\n",
2017 1.15 bouyer DEVNAME(sc), esd->discovery_status);
2018 1.15 bouyer }
2019 1.15 bouyer
2020 1.1 bouyer }
2021 1.1 bouyer }
2022 1.1 bouyer
2023 1.24 maxv static void
2024 1.1 bouyer mpii_event_process(struct mpii_softc *sc, struct mpii_rcb *rcb)
2025 1.1 bouyer {
2026 1.1 bouyer struct mpii_msg_event_reply *enp;
2027 1.1 bouyer
2028 1.1 bouyer enp = (struct mpii_msg_event_reply *)rcb->rcb_reply;
2029 1.1 bouyer
2030 1.1 bouyer DNPRINTF(MPII_D_EVT, "%s: mpii_event_process: %#x\n", DEVNAME(sc),
2031 1.15 bouyer le16toh(enp->event));
2032 1.1 bouyer
2033 1.15 bouyer switch (le16toh(enp->event)) {
2034 1.1 bouyer case MPII_EVENT_EVENT_CHANGE:
2035 1.1 bouyer /* should be properly ignored */
2036 1.1 bouyer break;
2037 1.15 bouyer case MPII_EVENT_SAS_DISCOVERY:
2038 1.15 bouyer mpii_event_discovery(sc, enp);
2039 1.1 bouyer break;
2040 1.1 bouyer case MPII_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
2041 1.15 bouyer mpii_event_sas(sc, rcb);
2042 1.15 bouyer return;
2043 1.1 bouyer case MPII_EVENT_SAS_DEVICE_STATUS_CHANGE:
2044 1.1 bouyer break;
2045 1.1 bouyer case MPII_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
2046 1.1 bouyer break;
2047 1.1 bouyer case MPII_EVENT_IR_VOLUME: {
2048 1.1 bouyer struct mpii_evt_ir_volume *evd =
2049 1.1 bouyer (struct mpii_evt_ir_volume *)(enp + 1);
2050 1.1 bouyer struct mpii_device *dev;
2051 1.1 bouyer #if NBIO > 0
2052 1.1 bouyer const char *vol_states[] = {
2053 1.1 bouyer BIOC_SVINVALID_S,
2054 1.1 bouyer BIOC_SVOFFLINE_S,
2055 1.1 bouyer BIOC_SVBUILDING_S,
2056 1.1 bouyer BIOC_SVONLINE_S,
2057 1.1 bouyer BIOC_SVDEGRADED_S,
2058 1.1 bouyer BIOC_SVONLINE_S,
2059 1.1 bouyer };
2060 1.1 bouyer #endif
2061 1.1 bouyer
2062 1.1 bouyer if (cold)
2063 1.1 bouyer break;
2064 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
2065 1.15 bouyer dev = mpii_find_dev(sc, le16toh(evd->vol_dev_handle));
2066 1.15 bouyer if (dev == NULL) {
2067 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
2068 1.1 bouyer break;
2069 1.15 bouyer }
2070 1.1 bouyer #if NBIO > 0
2071 1.1 bouyer if (evd->reason_code == MPII_EVENT_IR_VOL_RC_STATE_CHANGED)
2072 1.1 bouyer printf("%s: volume %d state changed from %s to %s\n",
2073 1.1 bouyer DEVNAME(sc), dev->slot - sc->sc_vd_id_low,
2074 1.1 bouyer vol_states[evd->prev_value],
2075 1.1 bouyer vol_states[evd->new_value]);
2076 1.1 bouyer #endif
2077 1.1 bouyer if (evd->reason_code == MPII_EVENT_IR_VOL_RC_STATUS_CHANGED &&
2078 1.1 bouyer ISSET(evd->new_value, MPII_CFG_RAID_VOL_0_STATUS_RESYNC) &&
2079 1.1 bouyer !ISSET(evd->prev_value, MPII_CFG_RAID_VOL_0_STATUS_RESYNC))
2080 1.1 bouyer printf("%s: started resync on a volume %d\n",
2081 1.1 bouyer DEVNAME(sc), dev->slot - sc->sc_vd_id_low);
2082 1.1 bouyer }
2083 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
2084 1.1 bouyer break;
2085 1.1 bouyer case MPII_EVENT_IR_PHYSICAL_DISK:
2086 1.1 bouyer break;
2087 1.1 bouyer case MPII_EVENT_IR_CONFIGURATION_CHANGE_LIST:
2088 1.1 bouyer mpii_event_raid(sc, enp);
2089 1.1 bouyer break;
2090 1.1 bouyer case MPII_EVENT_IR_OPERATION_STATUS: {
2091 1.1 bouyer struct mpii_evt_ir_status *evs =
2092 1.1 bouyer (struct mpii_evt_ir_status *)(enp + 1);
2093 1.1 bouyer struct mpii_device *dev;
2094 1.1 bouyer
2095 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
2096 1.15 bouyer dev = mpii_find_dev(sc, le16toh(evs->vol_dev_handle));
2097 1.15 bouyer if (dev != NULL &&
2098 1.15 bouyer evs->operation == MPII_EVENT_IR_RAIDOP_RESYNC)
2099 1.1 bouyer dev->percent = evs->percent;
2100 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
2101 1.1 bouyer break;
2102 1.1 bouyer }
2103 1.1 bouyer default:
2104 1.1 bouyer DNPRINTF(MPII_D_EVT, "%s: unhandled event 0x%02x\n",
2105 1.15 bouyer DEVNAME(sc), le16toh(enp->event));
2106 1.1 bouyer }
2107 1.1 bouyer
2108 1.15 bouyer mpii_event_done(sc, rcb);
2109 1.1 bouyer }
2110 1.1 bouyer
2111 1.24 maxv static void
2112 1.15 bouyer mpii_event_done(struct mpii_softc *sc, struct mpii_rcb *rcb)
2113 1.1 bouyer {
2114 1.15 bouyer struct mpii_msg_event_reply *enp = rcb->rcb_reply;
2115 1.15 bouyer bool need_start;
2116 1.1 bouyer
2117 1.15 bouyer if (enp->ack_required) {
2118 1.15 bouyer mutex_enter(&sc->sc_evt_ack_mtx);
2119 1.15 bouyer need_start = (SIMPLEQ_FIRST(&sc->sc_evt_ack_queue) == 0);
2120 1.15 bouyer SIMPLEQ_INSERT_TAIL(&sc->sc_evt_ack_queue, rcb, rcb_link);
2121 1.15 bouyer if (need_start)
2122 1.15 bouyer workqueue_enqueue(sc->sc_evt_ack_wq,
2123 1.15 bouyer &sc->sc_evt_ack_work, NULL);
2124 1.15 bouyer mutex_exit(&sc->sc_evt_ack_mtx);
2125 1.15 bouyer } else
2126 1.15 bouyer mpii_push_reply(sc, rcb);
2127 1.1 bouyer }
2128 1.1 bouyer
2129 1.24 maxv static void
2130 1.1 bouyer mpii_sas_remove_device(struct mpii_softc *sc, u_int16_t handle)
2131 1.1 bouyer {
2132 1.15 bouyer struct mpii_msg_scsi_task_request *stq;
2133 1.1 bouyer struct mpii_msg_sas_oper_request *soq;
2134 1.1 bouyer struct mpii_ccb *ccb;
2135 1.1 bouyer
2136 1.15 bouyer ccb = mpii_get_ccb(sc);
2137 1.1 bouyer if (ccb == NULL)
2138 1.1 bouyer return;
2139 1.1 bouyer
2140 1.1 bouyer stq = ccb->ccb_cmd;
2141 1.1 bouyer stq->function = MPII_FUNCTION_SCSI_TASK_MGMT;
2142 1.1 bouyer stq->task_type = MPII_SCSI_TASK_TARGET_RESET;
2143 1.1 bouyer stq->dev_handle = htole16(handle);
2144 1.1 bouyer
2145 1.1 bouyer ccb->ccb_done = mpii_empty_done;
2146 1.1 bouyer mpii_wait(sc, ccb);
2147 1.1 bouyer
2148 1.1 bouyer if (ccb->ccb_rcb != NULL)
2149 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
2150 1.1 bouyer
2151 1.1 bouyer /* reuse a ccb */
2152 1.1 bouyer ccb->ccb_state = MPII_CCB_READY;
2153 1.1 bouyer ccb->ccb_rcb = NULL;
2154 1.1 bouyer
2155 1.1 bouyer soq = ccb->ccb_cmd;
2156 1.15 bouyer memset(soq, 0, sizeof(*soq));
2157 1.1 bouyer soq->function = MPII_FUNCTION_SAS_IO_UNIT_CONTROL;
2158 1.1 bouyer soq->operation = MPII_SAS_OP_REMOVE_DEVICE;
2159 1.1 bouyer soq->dev_handle = htole16(handle);
2160 1.1 bouyer
2161 1.1 bouyer ccb->ccb_done = mpii_empty_done;
2162 1.1 bouyer mpii_wait(sc, ccb);
2163 1.1 bouyer if (ccb->ccb_rcb != NULL)
2164 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
2165 1.15 bouyer
2166 1.15 bouyer mpii_put_ccb(sc, ccb);
2167 1.1 bouyer }
2168 1.1 bouyer
2169 1.24 maxv static int
2170 1.15 bouyer mpii_board_info(struct mpii_softc *sc)
2171 1.1 bouyer {
2172 1.15 bouyer struct mpii_msg_iocfacts_request ifq;
2173 1.15 bouyer struct mpii_msg_iocfacts_reply ifp;
2174 1.15 bouyer struct mpii_cfg_manufacturing_pg0 mpg;
2175 1.15 bouyer struct mpii_cfg_hdr hdr;
2176 1.15 bouyer
2177 1.15 bouyer memset(&ifq, 0, sizeof(ifq));
2178 1.15 bouyer memset(&ifp, 0, sizeof(ifp));
2179 1.1 bouyer
2180 1.15 bouyer ifq.function = MPII_FUNCTION_IOC_FACTS;
2181 1.1 bouyer
2182 1.15 bouyer if (mpii_handshake_send(sc, &ifq, dwordsof(ifq)) != 0) {
2183 1.15 bouyer DNPRINTF(MPII_D_MISC, "%s: failed to request ioc facts\n",
2184 1.15 bouyer DEVNAME(sc));
2185 1.1 bouyer return (1);
2186 1.1 bouyer }
2187 1.1 bouyer
2188 1.15 bouyer if (mpii_handshake_recv(sc, &ifp, dwordsof(ifp)) != 0) {
2189 1.15 bouyer DNPRINTF(MPII_D_MISC, "%s: failed to receive ioc facts\n",
2190 1.15 bouyer DEVNAME(sc));
2191 1.1 bouyer return (1);
2192 1.1 bouyer }
2193 1.1 bouyer
2194 1.15 bouyer hdr.page_version = 0;
2195 1.15 bouyer hdr.page_length = sizeof(mpg) / 4;
2196 1.15 bouyer hdr.page_number = 0;
2197 1.15 bouyer hdr.page_type = MPII_CONFIG_REQ_PAGE_TYPE_MANUFACTURING;
2198 1.15 bouyer memset(&mpg, 0, sizeof(mpg));
2199 1.15 bouyer if (mpii_req_cfg_page(sc, 0, MPII_PG_POLL, &hdr, 1, &mpg,
2200 1.15 bouyer sizeof(mpg)) != 0) {
2201 1.15 bouyer printf("%s: unable to fetch manufacturing page 0\n",
2202 1.15 bouyer DEVNAME(sc));
2203 1.15 bouyer return (EINVAL);
2204 1.1 bouyer }
2205 1.1 bouyer
2206 1.15 bouyer printf("%s: %s, firmware %u.%u.%u.%u%s, MPI %u.%u\n", DEVNAME(sc),
2207 1.15 bouyer mpg.board_name, ifp.fw_version_maj, ifp.fw_version_min,
2208 1.15 bouyer ifp.fw_version_unit, ifp.fw_version_dev,
2209 1.15 bouyer ISSET(sc->sc_flags, MPII_F_RAID) ? " IR" : "",
2210 1.15 bouyer ifp.msg_version_maj, ifp.msg_version_min);
2211 1.15 bouyer
2212 1.15 bouyer return (0);
2213 1.15 bouyer }
2214 1.15 bouyer
2215 1.24 maxv static int
2216 1.15 bouyer mpii_target_map(struct mpii_softc *sc)
2217 1.15 bouyer {
2218 1.15 bouyer struct mpii_cfg_hdr hdr;
2219 1.15 bouyer struct mpii_cfg_ioc_pg8 ipg;
2220 1.15 bouyer int flags, pad = 0;
2221 1.1 bouyer
2222 1.15 bouyer hdr.page_version = 0;
2223 1.15 bouyer hdr.page_length = sizeof(ipg) / 4;
2224 1.15 bouyer hdr.page_number = 8;
2225 1.15 bouyer hdr.page_type = MPII_CONFIG_REQ_PAGE_TYPE_IOC;
2226 1.15 bouyer memset(&ipg, 0, sizeof(ipg));
2227 1.15 bouyer if (mpii_req_cfg_page(sc, 0, MPII_PG_POLL, &hdr, 1, &ipg,
2228 1.15 bouyer sizeof(ipg)) != 0) {
2229 1.15 bouyer printf("%s: unable to fetch ioc page 8\n",
2230 1.15 bouyer DEVNAME(sc));
2231 1.15 bouyer return (EINVAL);
2232 1.15 bouyer }
2233 1.15 bouyer
2234 1.15 bouyer if (le16toh(ipg.flags) & MPII_IOC_PG8_FLAGS_RESERVED_TARGETID_0)
2235 1.1 bouyer pad = 1;
2236 1.1 bouyer
2237 1.15 bouyer flags = le16toh(ipg.ir_volume_mapping_flags) &
2238 1.1 bouyer MPII_IOC_PG8_IRFLAGS_VOLUME_MAPPING_MODE_MASK;
2239 1.1 bouyer if (ISSET(sc->sc_flags, MPII_F_RAID)) {
2240 1.1 bouyer if (flags == MPII_IOC_PG8_IRFLAGS_LOW_VOLUME_MAPPING) {
2241 1.1 bouyer sc->sc_vd_id_low += pad;
2242 1.1 bouyer pad = sc->sc_max_volumes; /* for sc_pd_id_start */
2243 1.1 bouyer } else
2244 1.1 bouyer sc->sc_vd_id_low = sc->sc_max_devices -
2245 1.1 bouyer sc->sc_max_volumes;
2246 1.1 bouyer }
2247 1.1 bouyer
2248 1.1 bouyer sc->sc_pd_id_start += pad;
2249 1.1 bouyer
2250 1.15 bouyer return (0);
2251 1.1 bouyer }
2252 1.1 bouyer
2253 1.24 maxv static int
2254 1.1 bouyer mpii_req_cfg_header(struct mpii_softc *sc, u_int8_t type, u_int8_t number,
2255 1.1 bouyer u_int32_t address, int flags, void *p)
2256 1.1 bouyer {
2257 1.1 bouyer struct mpii_msg_config_request *cq;
2258 1.1 bouyer struct mpii_msg_config_reply *cp;
2259 1.15 bouyer struct mpii_ccb *ccb;
2260 1.15 bouyer struct mpii_cfg_hdr *hdr = p;
2261 1.15 bouyer struct mpii_ecfg_hdr *ehdr = p;
2262 1.15 bouyer int etype = 0;
2263 1.15 bouyer int rv = 0;
2264 1.1 bouyer
2265 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_req_cfg_header type: %#x number: %x "
2266 1.1 bouyer "address: 0x%08x flags: 0x%x\n", DEVNAME(sc), type, number,
2267 1.1 bouyer address, flags);
2268 1.1 bouyer
2269 1.15 bouyer ccb = mpii_get_ccb(sc);
2270 1.1 bouyer if (ccb == NULL) {
2271 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header ccb_get\n",
2272 1.1 bouyer DEVNAME(sc));
2273 1.1 bouyer return (1);
2274 1.1 bouyer }
2275 1.1 bouyer
2276 1.1 bouyer if (ISSET(flags, MPII_PG_EXTENDED)) {
2277 1.1 bouyer etype = type;
2278 1.1 bouyer type = MPII_CONFIG_REQ_PAGE_TYPE_EXTENDED;
2279 1.1 bouyer }
2280 1.1 bouyer
2281 1.1 bouyer cq = ccb->ccb_cmd;
2282 1.1 bouyer
2283 1.1 bouyer cq->function = MPII_FUNCTION_CONFIG;
2284 1.1 bouyer
2285 1.1 bouyer cq->action = MPII_CONFIG_REQ_ACTION_PAGE_HEADER;
2286 1.1 bouyer
2287 1.1 bouyer cq->config_header.page_number = number;
2288 1.1 bouyer cq->config_header.page_type = type;
2289 1.1 bouyer cq->ext_page_type = etype;
2290 1.1 bouyer cq->page_address = htole32(address);
2291 1.1 bouyer cq->page_buffer.sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
2292 1.1 bouyer MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL);
2293 1.1 bouyer
2294 1.1 bouyer ccb->ccb_done = mpii_empty_done;
2295 1.1 bouyer if (ISSET(flags, MPII_PG_POLL)) {
2296 1.1 bouyer if (mpii_poll(sc, ccb) != 0) {
2297 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header poll\n",
2298 1.1 bouyer DEVNAME(sc));
2299 1.1 bouyer return (1);
2300 1.1 bouyer }
2301 1.1 bouyer } else
2302 1.1 bouyer mpii_wait(sc, ccb);
2303 1.1 bouyer
2304 1.1 bouyer if (ccb->ccb_rcb == NULL) {
2305 1.1 bouyer mpii_put_ccb(sc, ccb);
2306 1.1 bouyer return (1);
2307 1.1 bouyer }
2308 1.1 bouyer cp = ccb->ccb_rcb->rcb_reply;
2309 1.1 bouyer
2310 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: action: 0x%02x sgl_flags: 0x%02x "
2311 1.1 bouyer "msg_length: %d function: 0x%02x\n", DEVNAME(sc), cp->action,
2312 1.1 bouyer cp->sgl_flags, cp->msg_length, cp->function);
2313 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ext_page_length: %d ext_page_type: 0x%02x "
2314 1.1 bouyer "msg_flags: 0x%02x\n", DEVNAME(sc),
2315 1.1 bouyer le16toh(cp->ext_page_length), cp->ext_page_type,
2316 1.1 bouyer cp->msg_flags);
2317 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
2318 1.15 bouyer cp->vp_id, cp->vf_id);
2319 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ioc_status: 0x%04x\n", DEVNAME(sc),
2320 1.1 bouyer le16toh(cp->ioc_status));
2321 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ioc_loginfo: 0x%08x\n", DEVNAME(sc),
2322 1.1 bouyer le32toh(cp->ioc_loginfo));
2323 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: page_version: 0x%02x page_length: %d "
2324 1.1 bouyer "page_number: 0x%02x page_type: 0x%02x\n", DEVNAME(sc),
2325 1.1 bouyer cp->config_header.page_version,
2326 1.1 bouyer cp->config_header.page_length,
2327 1.1 bouyer cp->config_header.page_number,
2328 1.1 bouyer cp->config_header.page_type);
2329 1.1 bouyer
2330 1.1 bouyer if (le16toh(cp->ioc_status) != MPII_IOCSTATUS_SUCCESS)
2331 1.1 bouyer rv = 1;
2332 1.1 bouyer else if (ISSET(flags, MPII_PG_EXTENDED)) {
2333 1.15 bouyer memset(ehdr, 0, sizeof(*ehdr));
2334 1.1 bouyer ehdr->page_version = cp->config_header.page_version;
2335 1.1 bouyer ehdr->page_number = cp->config_header.page_number;
2336 1.1 bouyer ehdr->page_type = cp->config_header.page_type;
2337 1.1 bouyer ehdr->ext_page_length = cp->ext_page_length;
2338 1.1 bouyer ehdr->ext_page_type = cp->ext_page_type;
2339 1.1 bouyer } else
2340 1.1 bouyer *hdr = cp->config_header;
2341 1.1 bouyer
2342 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
2343 1.1 bouyer mpii_put_ccb(sc, ccb);
2344 1.1 bouyer
2345 1.1 bouyer return (rv);
2346 1.1 bouyer }
2347 1.1 bouyer
2348 1.24 maxv static int
2349 1.1 bouyer mpii_req_cfg_page(struct mpii_softc *sc, u_int32_t address, int flags,
2350 1.1 bouyer void *p, int read, void *page, size_t len)
2351 1.1 bouyer {
2352 1.1 bouyer struct mpii_msg_config_request *cq;
2353 1.1 bouyer struct mpii_msg_config_reply *cp;
2354 1.15 bouyer struct mpii_ccb *ccb;
2355 1.15 bouyer struct mpii_cfg_hdr *hdr = p;
2356 1.15 bouyer struct mpii_ecfg_hdr *ehdr = p;
2357 1.15 bouyer uintptr_t kva;
2358 1.15 bouyer int page_length;
2359 1.15 bouyer int rv = 0;
2360 1.1 bouyer
2361 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_page address: %d read: %d "
2362 1.1 bouyer "type: %x\n", DEVNAME(sc), address, read, hdr->page_type);
2363 1.1 bouyer
2364 1.1 bouyer page_length = ISSET(flags, MPII_PG_EXTENDED) ?
2365 1.1 bouyer le16toh(ehdr->ext_page_length) : hdr->page_length;
2366 1.1 bouyer
2367 1.15 bouyer if (len > sc->sc_request_size - sizeof(*cq) || len < page_length * 4)
2368 1.1 bouyer return (1);
2369 1.1 bouyer
2370 1.15 bouyer ccb = mpii_get_ccb(sc);
2371 1.1 bouyer if (ccb == NULL) {
2372 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_page ccb_get\n",
2373 1.1 bouyer DEVNAME(sc));
2374 1.1 bouyer return (1);
2375 1.1 bouyer }
2376 1.1 bouyer
2377 1.1 bouyer cq = ccb->ccb_cmd;
2378 1.1 bouyer
2379 1.1 bouyer cq->function = MPII_FUNCTION_CONFIG;
2380 1.1 bouyer
2381 1.1 bouyer cq->action = (read ? MPII_CONFIG_REQ_ACTION_PAGE_READ_CURRENT :
2382 1.1 bouyer MPII_CONFIG_REQ_ACTION_PAGE_WRITE_CURRENT);
2383 1.1 bouyer
2384 1.1 bouyer if (ISSET(flags, MPII_PG_EXTENDED)) {
2385 1.1 bouyer cq->config_header.page_version = ehdr->page_version;
2386 1.1 bouyer cq->config_header.page_number = ehdr->page_number;
2387 1.1 bouyer cq->config_header.page_type = ehdr->page_type;
2388 1.1 bouyer cq->ext_page_len = ehdr->ext_page_length;
2389 1.1 bouyer cq->ext_page_type = ehdr->ext_page_type;
2390 1.1 bouyer } else
2391 1.1 bouyer cq->config_header = *hdr;
2392 1.1 bouyer cq->config_header.page_type &= MPII_CONFIG_REQ_PAGE_TYPE_MASK;
2393 1.1 bouyer cq->page_address = htole32(address);
2394 1.1 bouyer cq->page_buffer.sg_hdr = htole32(MPII_SGE_FL_TYPE_SIMPLE |
2395 1.1 bouyer MPII_SGE_FL_LAST | MPII_SGE_FL_EOB | MPII_SGE_FL_EOL |
2396 1.1 bouyer MPII_SGE_FL_SIZE_64 | (page_length * 4) |
2397 1.1 bouyer (read ? MPII_SGE_FL_DIR_IN : MPII_SGE_FL_DIR_OUT));
2398 1.1 bouyer
2399 1.1 bouyer /* bounce the page via the request space to avoid more bus_dma games */
2400 1.15 bouyer mpii_dvatosge(&cq->page_buffer, ccb->ccb_cmd_dva +
2401 1.15 bouyer sizeof(struct mpii_msg_config_request));
2402 1.1 bouyer
2403 1.15 bouyer kva = (uintptr_t)ccb->ccb_cmd;
2404 1.1 bouyer kva += sizeof(struct mpii_msg_config_request);
2405 1.1 bouyer
2406 1.1 bouyer if (!read)
2407 1.15 bouyer memcpy((void *)kva, page, len);
2408 1.1 bouyer
2409 1.1 bouyer ccb->ccb_done = mpii_empty_done;
2410 1.1 bouyer if (ISSET(flags, MPII_PG_POLL)) {
2411 1.1 bouyer if (mpii_poll(sc, ccb) != 0) {
2412 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_cfg_header poll\n",
2413 1.1 bouyer DEVNAME(sc));
2414 1.1 bouyer return (1);
2415 1.1 bouyer }
2416 1.1 bouyer } else
2417 1.1 bouyer mpii_wait(sc, ccb);
2418 1.1 bouyer
2419 1.1 bouyer if (ccb->ccb_rcb == NULL) {
2420 1.1 bouyer mpii_put_ccb(sc, ccb);
2421 1.1 bouyer return (1);
2422 1.1 bouyer }
2423 1.1 bouyer cp = ccb->ccb_rcb->rcb_reply;
2424 1.1 bouyer
2425 1.15 bouyer DNPRINTF(MPII_D_MISC, "%s: action: 0x%02x msg_length: %d "
2426 1.15 bouyer "function: 0x%02x\n", DEVNAME(sc), cp->action, cp->msg_length,
2427 1.15 bouyer cp->function);
2428 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ext_page_length: %d ext_page_type: 0x%02x "
2429 1.1 bouyer "msg_flags: 0x%02x\n", DEVNAME(sc),
2430 1.1 bouyer le16toh(cp->ext_page_length), cp->ext_page_type,
2431 1.1 bouyer cp->msg_flags);
2432 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
2433 1.1 bouyer cp->vp_id, cp->vf_id);
2434 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ioc_status: 0x%04x\n", DEVNAME(sc),
2435 1.1 bouyer le16toh(cp->ioc_status));
2436 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: ioc_loginfo: 0x%08x\n", DEVNAME(sc),
2437 1.1 bouyer le32toh(cp->ioc_loginfo));
2438 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: page_version: 0x%02x page_length: %d "
2439 1.1 bouyer "page_number: 0x%02x page_type: 0x%02x\n", DEVNAME(sc),
2440 1.1 bouyer cp->config_header.page_version,
2441 1.1 bouyer cp->config_header.page_length,
2442 1.1 bouyer cp->config_header.page_number,
2443 1.1 bouyer cp->config_header.page_type);
2444 1.15 bouyer
2445 1.1 bouyer if (le16toh(cp->ioc_status) != MPII_IOCSTATUS_SUCCESS)
2446 1.1 bouyer rv = 1;
2447 1.1 bouyer else if (read)
2448 1.15 bouyer memcpy(page, (void *)kva, len);
2449 1.1 bouyer
2450 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
2451 1.1 bouyer mpii_put_ccb(sc, ccb);
2452 1.1 bouyer
2453 1.1 bouyer return (rv);
2454 1.1 bouyer }
2455 1.1 bouyer
2456 1.24 maxv static struct mpii_rcb *
2457 1.1 bouyer mpii_reply(struct mpii_softc *sc, struct mpii_reply_descr *rdp)
2458 1.1 bouyer {
2459 1.1 bouyer struct mpii_rcb *rcb = NULL;
2460 1.1 bouyer u_int32_t rfid;
2461 1.1 bouyer
2462 1.15 bouyer KASSERT(mutex_owned(&sc->sc_rep_mtx));
2463 1.1 bouyer DNPRINTF(MPII_D_INTR, "%s: mpii_reply\n", DEVNAME(sc));
2464 1.1 bouyer
2465 1.1 bouyer if ((rdp->reply_flags & MPII_REPLY_DESCR_TYPE_MASK) ==
2466 1.1 bouyer MPII_REPLY_DESCR_ADDRESS_REPLY) {
2467 1.1 bouyer rfid = (le32toh(rdp->frame_addr) -
2468 1.15 bouyer (u_int32_t)MPII_DMA_DVA(sc->sc_replies)) /
2469 1.15 bouyer sc->sc_reply_size;
2470 1.1 bouyer
2471 1.1 bouyer bus_dmamap_sync(sc->sc_dmat,
2472 1.15 bouyer MPII_DMA_MAP(sc->sc_replies), sc->sc_reply_size * rfid,
2473 1.15 bouyer sc->sc_reply_size, BUS_DMASYNC_POSTREAD);
2474 1.1 bouyer
2475 1.1 bouyer rcb = &sc->sc_rcbs[rfid];
2476 1.1 bouyer }
2477 1.1 bouyer
2478 1.1 bouyer memset(rdp, 0xff, sizeof(*rdp));
2479 1.1 bouyer
2480 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_reply_postq),
2481 1.1 bouyer 8 * sc->sc_reply_post_host_index, 8,
2482 1.1 bouyer BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2483 1.1 bouyer
2484 1.1 bouyer return (rcb);
2485 1.1 bouyer }
2486 1.1 bouyer
2487 1.24 maxv static struct mpii_dmamem *
2488 1.1 bouyer mpii_dmamem_alloc(struct mpii_softc *sc, size_t size)
2489 1.1 bouyer {
2490 1.1 bouyer struct mpii_dmamem *mdm;
2491 1.1 bouyer int nsegs;
2492 1.1 bouyer
2493 1.23 chs mdm = malloc(sizeof(*mdm), M_DEVBUF, M_WAITOK | M_ZERO);
2494 1.1 bouyer mdm->mdm_size = size;
2495 1.1 bouyer
2496 1.1 bouyer if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
2497 1.1 bouyer BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &mdm->mdm_map) != 0)
2498 1.1 bouyer goto mdmfree;
2499 1.1 bouyer
2500 1.1 bouyer if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &mdm->mdm_seg,
2501 1.15 bouyer 1, &nsegs, BUS_DMA_NOWAIT) != 0)
2502 1.15 bouyer goto destroy;
2503 1.1 bouyer
2504 1.1 bouyer if (bus_dmamem_map(sc->sc_dmat, &mdm->mdm_seg, nsegs, size,
2505 1.1 bouyer &mdm->mdm_kva, BUS_DMA_NOWAIT) != 0)
2506 1.1 bouyer goto free;
2507 1.1 bouyer
2508 1.1 bouyer if (bus_dmamap_load(sc->sc_dmat, mdm->mdm_map, mdm->mdm_kva, size,
2509 1.1 bouyer NULL, BUS_DMA_NOWAIT) != 0)
2510 1.1 bouyer goto unmap;
2511 1.1 bouyer
2512 1.15 bouyer memset(mdm->mdm_kva, 0, size);
2513 1.1 bouyer
2514 1.1 bouyer return (mdm);
2515 1.1 bouyer
2516 1.1 bouyer unmap:
2517 1.1 bouyer bus_dmamem_unmap(sc->sc_dmat, mdm->mdm_kva, size);
2518 1.1 bouyer free:
2519 1.1 bouyer bus_dmamem_free(sc->sc_dmat, &mdm->mdm_seg, 1);
2520 1.1 bouyer destroy:
2521 1.1 bouyer bus_dmamap_destroy(sc->sc_dmat, mdm->mdm_map);
2522 1.1 bouyer mdmfree:
2523 1.1 bouyer free(mdm, M_DEVBUF);
2524 1.1 bouyer
2525 1.1 bouyer return (NULL);
2526 1.1 bouyer }
2527 1.1 bouyer
2528 1.24 maxv static void
2529 1.1 bouyer mpii_dmamem_free(struct mpii_softc *sc, struct mpii_dmamem *mdm)
2530 1.1 bouyer {
2531 1.1 bouyer DNPRINTF(MPII_D_MEM, "%s: mpii_dmamem_free %p\n", DEVNAME(sc), mdm);
2532 1.1 bouyer
2533 1.1 bouyer bus_dmamap_unload(sc->sc_dmat, mdm->mdm_map);
2534 1.1 bouyer bus_dmamem_unmap(sc->sc_dmat, mdm->mdm_kva, mdm->mdm_size);
2535 1.1 bouyer bus_dmamem_free(sc->sc_dmat, &mdm->mdm_seg, 1);
2536 1.1 bouyer bus_dmamap_destroy(sc->sc_dmat, mdm->mdm_map);
2537 1.1 bouyer free(mdm, M_DEVBUF);
2538 1.1 bouyer }
2539 1.1 bouyer
2540 1.24 maxv static int
2541 1.1 bouyer mpii_insert_dev(struct mpii_softc *sc, struct mpii_device *dev)
2542 1.1 bouyer {
2543 1.15 bouyer int slot; /* initial hint */
2544 1.10 maya
2545 1.15 bouyer KASSERT(mutex_owned(&sc->sc_devs_mtx));
2546 1.15 bouyer DNPRINTF(MPII_D_EVT, "%s: mpii_insert_dev wants slot %d\n",
2547 1.15 bouyer DEVNAME(sc), dev->slot);
2548 1.15 bouyer if (dev == NULL || dev->slot < 0)
2549 1.10 maya return (1);
2550 1.15 bouyer slot = dev->slot;
2551 1.1 bouyer
2552 1.1 bouyer while (slot < sc->sc_max_devices && sc->sc_devs[slot] != NULL)
2553 1.1 bouyer slot++;
2554 1.15 bouyer
2555 1.1 bouyer if (slot >= sc->sc_max_devices)
2556 1.1 bouyer return (1);
2557 1.15 bouyer
2558 1.15 bouyer DNPRINTF(MPII_D_EVT, "%s: mpii_insert_dev alloc slot %d\n",
2559 1.15 bouyer DEVNAME(sc), slot);
2560 1.15 bouyer
2561 1.1 bouyer dev->slot = slot;
2562 1.1 bouyer sc->sc_devs[slot] = dev;
2563 1.15 bouyer
2564 1.1 bouyer return (0);
2565 1.1 bouyer }
2566 1.1 bouyer
2567 1.24 maxv static int
2568 1.1 bouyer mpii_remove_dev(struct mpii_softc *sc, struct mpii_device *dev)
2569 1.1 bouyer {
2570 1.1 bouyer int i;
2571 1.1 bouyer
2572 1.15 bouyer KASSERT(mutex_owned(&sc->sc_devs_mtx));
2573 1.15 bouyer if (dev == NULL)
2574 1.1 bouyer return (1);
2575 1.15 bouyer
2576 1.15 bouyer for (i = 0; i < sc->sc_max_devices; i++) {
2577 1.15 bouyer if (sc->sc_devs[i] == NULL)
2578 1.15 bouyer continue;
2579 1.15 bouyer
2580 1.15 bouyer if (sc->sc_devs[i]->dev_handle == dev->dev_handle) {
2581 1.1 bouyer sc->sc_devs[i] = NULL;
2582 1.1 bouyer return (0);
2583 1.1 bouyer }
2584 1.15 bouyer }
2585 1.15 bouyer
2586 1.1 bouyer return (1);
2587 1.1 bouyer }
2588 1.1 bouyer
2589 1.24 maxv static struct mpii_device *
2590 1.1 bouyer mpii_find_dev(struct mpii_softc *sc, u_int16_t handle)
2591 1.1 bouyer {
2592 1.1 bouyer int i;
2593 1.15 bouyer KASSERT(mutex_owned(&sc->sc_devs_mtx));
2594 1.1 bouyer
2595 1.15 bouyer for (i = 0; i < sc->sc_max_devices; i++) {
2596 1.15 bouyer if (sc->sc_devs[i] == NULL)
2597 1.15 bouyer continue;
2598 1.15 bouyer
2599 1.15 bouyer if (sc->sc_devs[i]->dev_handle == handle)
2600 1.1 bouyer return (sc->sc_devs[i]);
2601 1.15 bouyer }
2602 1.15 bouyer
2603 1.1 bouyer return (NULL);
2604 1.1 bouyer }
2605 1.1 bouyer
2606 1.24 maxv static int
2607 1.1 bouyer mpii_alloc_ccbs(struct mpii_softc *sc)
2608 1.1 bouyer {
2609 1.1 bouyer struct mpii_ccb *ccb;
2610 1.1 bouyer u_int8_t *cmd;
2611 1.1 bouyer int i;
2612 1.15 bouyer char wqname[16];
2613 1.1 bouyer
2614 1.1 bouyer SIMPLEQ_INIT(&sc->sc_ccb_free);
2615 1.15 bouyer SIMPLEQ_INIT(&sc->sc_ccb_tmos);
2616 1.15 bouyer mutex_init(&sc->sc_ccb_free_mtx, MUTEX_DEFAULT, IPL_BIO);
2617 1.15 bouyer cv_init(&sc->sc_ccb_free_cv, "mpii_ccbs");
2618 1.15 bouyer mutex_init(&sc->sc_ssb_tmomtx, MUTEX_DEFAULT, IPL_BIO);
2619 1.15 bouyer snprintf(wqname, sizeof(wqname) - 1, "%sabrt", DEVNAME(sc));
2620 1.15 bouyer workqueue_create(&sc->sc_ssb_tmowk, wqname, mpii_scsi_cmd_tmo_handler,
2621 1.15 bouyer sc, PRI_BIO, IPL_BIO, WQ_MPSAFE);
2622 1.15 bouyer if (sc->sc_ssb_tmowk == NULL)
2623 1.15 bouyer return 1;
2624 1.1 bouyer
2625 1.15 bouyer sc->sc_ccbs = malloc((sc->sc_max_cmds-1) * sizeof(*ccb),
2626 1.23 chs M_DEVBUF, M_WAITOK | M_ZERO);
2627 1.1 bouyer sc->sc_requests = mpii_dmamem_alloc(sc,
2628 1.15 bouyer sc->sc_request_size * sc->sc_max_cmds);
2629 1.1 bouyer if (sc->sc_requests == NULL) {
2630 1.1 bouyer printf("%s: unable to allocate ccb dmamem\n", DEVNAME(sc));
2631 1.1 bouyer goto free_ccbs;
2632 1.1 bouyer }
2633 1.1 bouyer cmd = MPII_DMA_KVA(sc->sc_requests);
2634 1.1 bouyer
2635 1.1 bouyer /*
2636 1.15 bouyer * we have sc->sc_max_cmds system request message
2637 1.1 bouyer * frames, but smid zero cannot be used. so we then
2638 1.15 bouyer * have (sc->sc_max_cmds - 1) number of ccbs
2639 1.1 bouyer */
2640 1.15 bouyer for (i = 1; i < sc->sc_max_cmds; i++) {
2641 1.1 bouyer ccb = &sc->sc_ccbs[i - 1];
2642 1.1 bouyer
2643 1.15 bouyer if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, sc->sc_max_sgl,
2644 1.15 bouyer MAXPHYS, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
2645 1.1 bouyer &ccb->ccb_dmamap) != 0) {
2646 1.1 bouyer printf("%s: unable to create dma map\n", DEVNAME(sc));
2647 1.1 bouyer goto free_maps;
2648 1.1 bouyer }
2649 1.1 bouyer
2650 1.1 bouyer ccb->ccb_sc = sc;
2651 1.15 bouyer mutex_init(&ccb->ccb_mtx, MUTEX_DEFAULT, IPL_BIO);
2652 1.15 bouyer cv_init(&ccb->ccb_cv, "mpiiexec");
2653 1.15 bouyer
2654 1.15 bouyer ccb->ccb_smid = htole16(i);
2655 1.15 bouyer ccb->ccb_offset = sc->sc_request_size * i;
2656 1.1 bouyer
2657 1.1 bouyer ccb->ccb_cmd = &cmd[ccb->ccb_offset];
2658 1.1 bouyer ccb->ccb_cmd_dva = (u_int32_t)MPII_DMA_DVA(sc->sc_requests) +
2659 1.1 bouyer ccb->ccb_offset;
2660 1.1 bouyer
2661 1.1 bouyer DNPRINTF(MPII_D_CCB, "%s: mpii_alloc_ccbs(%d) ccb: %p map: %p "
2662 1.15 bouyer "sc: %p smid: %#x offs: %#lx cmd: %p dva: %#lx\n",
2663 1.1 bouyer DEVNAME(sc), i, ccb, ccb->ccb_dmamap, ccb->ccb_sc,
2664 1.15 bouyer ccb->ccb_smid, ccb->ccb_offset, ccb->ccb_cmd,
2665 1.15 bouyer ccb->ccb_cmd_dva);
2666 1.1 bouyer
2667 1.1 bouyer mpii_put_ccb(sc, ccb);
2668 1.1 bouyer }
2669 1.1 bouyer
2670 1.1 bouyer return (0);
2671 1.1 bouyer
2672 1.1 bouyer free_maps:
2673 1.15 bouyer while ((ccb = mpii_get_ccb(sc)) != NULL)
2674 1.1 bouyer bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
2675 1.1 bouyer
2676 1.1 bouyer mpii_dmamem_free(sc, sc->sc_requests);
2677 1.1 bouyer free_ccbs:
2678 1.1 bouyer free(sc->sc_ccbs, M_DEVBUF);
2679 1.1 bouyer
2680 1.1 bouyer return (1);
2681 1.1 bouyer }
2682 1.1 bouyer
2683 1.24 maxv static void
2684 1.1 bouyer mpii_put_ccb(struct mpii_softc *sc, struct mpii_ccb *ccb)
2685 1.1 bouyer {
2686 1.1 bouyer DNPRINTF(MPII_D_CCB, "%s: mpii_put_ccb %p\n", DEVNAME(sc), ccb);
2687 1.1 bouyer
2688 1.1 bouyer ccb->ccb_state = MPII_CCB_FREE;
2689 1.1 bouyer ccb->ccb_cookie = NULL;
2690 1.1 bouyer ccb->ccb_done = NULL;
2691 1.1 bouyer ccb->ccb_rcb = NULL;
2692 1.15 bouyer memset(ccb->ccb_cmd, 0, sc->sc_request_size);
2693 1.1 bouyer
2694 1.1 bouyer mutex_enter(&sc->sc_ccb_free_mtx);
2695 1.15 bouyer SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_link);
2696 1.1 bouyer mutex_exit(&sc->sc_ccb_free_mtx);
2697 1.1 bouyer }
2698 1.1 bouyer
2699 1.24 maxv static struct mpii_ccb *
2700 1.15 bouyer mpii_get_ccb(struct mpii_softc *sc)
2701 1.1 bouyer {
2702 1.1 bouyer struct mpii_ccb *ccb;
2703 1.1 bouyer
2704 1.1 bouyer mutex_enter(&sc->sc_ccb_free_mtx);
2705 1.15 bouyer ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free);
2706 1.1 bouyer if (ccb != NULL) {
2707 1.15 bouyer SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb_link);
2708 1.1 bouyer ccb->ccb_state = MPII_CCB_READY;
2709 1.1 bouyer KASSERT(ccb->ccb_sc == sc);
2710 1.1 bouyer }
2711 1.1 bouyer mutex_exit(&sc->sc_ccb_free_mtx);
2712 1.1 bouyer
2713 1.1 bouyer DNPRINTF(MPII_D_CCB, "%s: mpii_get_ccb %p\n", DEVNAME(sc), ccb);
2714 1.1 bouyer
2715 1.1 bouyer return (ccb);
2716 1.1 bouyer }
2717 1.1 bouyer
2718 1.24 maxv static int
2719 1.1 bouyer mpii_alloc_replies(struct mpii_softc *sc)
2720 1.1 bouyer {
2721 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_alloc_replies\n", DEVNAME(sc));
2722 1.1 bouyer
2723 1.1 bouyer sc->sc_rcbs = malloc(sc->sc_num_reply_frames * sizeof(struct mpii_rcb),
2724 1.23 chs M_DEVBUF, M_WAITOK);
2725 1.1 bouyer
2726 1.15 bouyer sc->sc_replies = mpii_dmamem_alloc(sc, sc->sc_reply_size *
2727 1.1 bouyer sc->sc_num_reply_frames);
2728 1.1 bouyer if (sc->sc_replies == NULL) {
2729 1.1 bouyer free(sc->sc_rcbs, M_DEVBUF);
2730 1.1 bouyer return (1);
2731 1.1 bouyer }
2732 1.1 bouyer
2733 1.1 bouyer return (0);
2734 1.1 bouyer }
2735 1.1 bouyer
2736 1.24 maxv static void
2737 1.1 bouyer mpii_push_replies(struct mpii_softc *sc)
2738 1.1 bouyer {
2739 1.1 bouyer struct mpii_rcb *rcb;
2740 1.15 bouyer uintptr_t kva = (uintptr_t)MPII_DMA_KVA(sc->sc_replies);
2741 1.1 bouyer int i;
2742 1.1 bouyer
2743 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_replies),
2744 1.15 bouyer 0, sc->sc_reply_size * sc->sc_num_reply_frames,
2745 1.15 bouyer BUS_DMASYNC_PREREAD);
2746 1.1 bouyer
2747 1.1 bouyer for (i = 0; i < sc->sc_num_reply_frames; i++) {
2748 1.1 bouyer rcb = &sc->sc_rcbs[i];
2749 1.1 bouyer
2750 1.15 bouyer rcb->rcb_reply = (void *)(kva + sc->sc_reply_size * i);
2751 1.1 bouyer rcb->rcb_reply_dva = (u_int32_t)MPII_DMA_DVA(sc->sc_replies) +
2752 1.15 bouyer sc->sc_reply_size * i;
2753 1.1 bouyer mpii_push_reply(sc, rcb);
2754 1.1 bouyer }
2755 1.1 bouyer }
2756 1.1 bouyer
2757 1.24 maxv static void
2758 1.1 bouyer mpii_start(struct mpii_softc *sc, struct mpii_ccb *ccb)
2759 1.1 bouyer {
2760 1.1 bouyer struct mpii_request_header *rhp;
2761 1.1 bouyer struct mpii_request_descr descr;
2762 1.15 bouyer #if defined(__LP64__) && 0
2763 1.15 bouyer u_long *rdp = (u_long *)&descr;
2764 1.15 bouyer #else
2765 1.15 bouyer u_int32_t *rdp = (u_int32_t *)&descr;
2766 1.15 bouyer #endif
2767 1.15 bouyer
2768 1.15 bouyer DNPRINTF(MPII_D_RW, "%s: mpii_start %#lx\n", DEVNAME(sc),
2769 1.15 bouyer ccb->ccb_cmd_dva);
2770 1.1 bouyer
2771 1.15 bouyer bus_dmamap_sync(sc->sc_dmat, MPII_DMA_MAP(sc->sc_requests),
2772 1.15 bouyer ccb->ccb_offset, sc->sc_request_size,
2773 1.15 bouyer BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2774 1.15 bouyer
2775 1.15 bouyer ccb->ccb_state = MPII_CCB_QUEUED;
2776 1.1 bouyer
2777 1.1 bouyer rhp = ccb->ccb_cmd;
2778 1.1 bouyer
2779 1.15 bouyer memset(&descr, 0, sizeof(descr));
2780 1.1 bouyer
2781 1.1 bouyer switch (rhp->function) {
2782 1.1 bouyer case MPII_FUNCTION_SCSI_IO_REQUEST:
2783 1.1 bouyer descr.request_flags = MPII_REQ_DESCR_SCSI_IO;
2784 1.1 bouyer descr.dev_handle = htole16(ccb->ccb_dev_handle);
2785 1.1 bouyer break;
2786 1.1 bouyer case MPII_FUNCTION_SCSI_TASK_MGMT:
2787 1.1 bouyer descr.request_flags = MPII_REQ_DESCR_HIGH_PRIORITY;
2788 1.1 bouyer break;
2789 1.1 bouyer default:
2790 1.1 bouyer descr.request_flags = MPII_REQ_DESCR_DEFAULT;
2791 1.1 bouyer }
2792 1.1 bouyer
2793 1.1 bouyer descr.vf_id = sc->sc_vf_id;
2794 1.15 bouyer descr.smid = ccb->ccb_smid;
2795 1.1 bouyer
2796 1.15 bouyer #if defined(__LP64__) && 0
2797 1.15 bouyer DNPRINTF(MPII_D_RW, "%s: MPII_REQ_DESCR_POST_LOW (0x%08x) write "
2798 1.15 bouyer "0x%08lx\n", DEVNAME(sc), MPII_REQ_DESCR_POST_LOW, *rdp);
2799 1.15 bouyer bus_space_write_raw_8(sc->sc_iot, sc->sc_ioh,
2800 1.15 bouyer MPII_REQ_DESCR_POST_LOW, *rdp);
2801 1.15 bouyer #else
2802 1.1 bouyer DNPRINTF(MPII_D_RW, "%s: MPII_REQ_DESCR_POST_LOW (0x%08x) write "
2803 1.15 bouyer "0x%04x\n", DEVNAME(sc), MPII_REQ_DESCR_POST_LOW, *rdp);
2804 1.1 bouyer
2805 1.1 bouyer DNPRINTF(MPII_D_RW, "%s: MPII_REQ_DESCR_POST_HIGH (0x%08x) write "
2806 1.15 bouyer "0x%04x\n", DEVNAME(sc), MPII_REQ_DESCR_POST_HIGH, *(rdp+1));
2807 1.1 bouyer
2808 1.1 bouyer mutex_enter(&sc->sc_req_mtx);
2809 1.15 bouyer bus_space_write_4(sc->sc_iot, sc->sc_ioh,
2810 1.15 bouyer MPII_REQ_DESCR_POST_LOW, rdp[0]);
2811 1.15 bouyer bus_space_barrier(sc->sc_iot, sc->sc_ioh,
2812 1.15 bouyer MPII_REQ_DESCR_POST_LOW, 8, BUS_SPACE_BARRIER_WRITE);
2813 1.15 bouyer
2814 1.15 bouyer bus_space_write_4(sc->sc_iot, sc->sc_ioh,
2815 1.15 bouyer MPII_REQ_DESCR_POST_HIGH, rdp[1]);
2816 1.15 bouyer bus_space_barrier(sc->sc_iot, sc->sc_ioh,
2817 1.15 bouyer MPII_REQ_DESCR_POST_LOW, 8, BUS_SPACE_BARRIER_WRITE);
2818 1.1 bouyer mutex_exit(&sc->sc_req_mtx);
2819 1.15 bouyer #endif
2820 1.1 bouyer }
2821 1.1 bouyer
2822 1.24 maxv static int
2823 1.1 bouyer mpii_poll(struct mpii_softc *sc, struct mpii_ccb *ccb)
2824 1.1 bouyer {
2825 1.1 bouyer void (*done)(struct mpii_ccb *);
2826 1.1 bouyer void *cookie;
2827 1.1 bouyer int rv = 1;
2828 1.1 bouyer
2829 1.15 bouyer DNPRINTF(MPII_D_INTR, "%s: mpii_poll\n", DEVNAME(sc));
2830 1.1 bouyer
2831 1.1 bouyer done = ccb->ccb_done;
2832 1.1 bouyer cookie = ccb->ccb_cookie;
2833 1.1 bouyer
2834 1.1 bouyer ccb->ccb_done = mpii_poll_done;
2835 1.1 bouyer ccb->ccb_cookie = &rv;
2836 1.1 bouyer
2837 1.1 bouyer mpii_start(sc, ccb);
2838 1.1 bouyer
2839 1.1 bouyer while (rv == 1) {
2840 1.1 bouyer /* avoid excessive polling */
2841 1.1 bouyer if (mpii_reply_waiting(sc))
2842 1.1 bouyer mpii_intr(sc);
2843 1.1 bouyer else
2844 1.1 bouyer delay(10);
2845 1.1 bouyer }
2846 1.1 bouyer
2847 1.1 bouyer ccb->ccb_cookie = cookie;
2848 1.1 bouyer done(ccb);
2849 1.1 bouyer
2850 1.1 bouyer return (0);
2851 1.1 bouyer }
2852 1.1 bouyer
2853 1.24 maxv static void
2854 1.1 bouyer mpii_poll_done(struct mpii_ccb *ccb)
2855 1.1 bouyer {
2856 1.1 bouyer int *rv = ccb->ccb_cookie;
2857 1.1 bouyer
2858 1.1 bouyer *rv = 0;
2859 1.1 bouyer }
2860 1.1 bouyer
2861 1.24 maxv static int
2862 1.1 bouyer mpii_alloc_queues(struct mpii_softc *sc)
2863 1.1 bouyer {
2864 1.15 bouyer u_int32_t *rfp;
2865 1.1 bouyer int i;
2866 1.15 bouyer
2867 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_alloc_queues\n", DEVNAME(sc));
2868 1.1 bouyer
2869 1.15 bouyer mutex_init(&sc->sc_reply_free_mtx, MUTEX_DEFAULT, IPL_BIO);
2870 1.1 bouyer sc->sc_reply_freeq = mpii_dmamem_alloc(sc,
2871 1.15 bouyer sc->sc_reply_free_qdepth * sizeof(*rfp));
2872 1.1 bouyer if (sc->sc_reply_freeq == NULL)
2873 1.1 bouyer return (1);
2874 1.15 bouyer rfp = MPII_DMA_KVA(sc->sc_reply_freeq);
2875 1.1 bouyer for (i = 0; i < sc->sc_num_reply_frames; i++) {
2876 1.15 bouyer rfp[i] = (u_int32_t)MPII_DMA_DVA(sc->sc_replies) +
2877 1.15 bouyer sc->sc_reply_size * i;
2878 1.1 bouyer }
2879 1.1 bouyer
2880 1.15 bouyer sc->sc_reply_postq = mpii_dmamem_alloc(sc,
2881 1.15 bouyer sc->sc_reply_post_qdepth * sizeof(struct mpii_reply_descr));
2882 1.1 bouyer if (sc->sc_reply_postq == NULL)
2883 1.1 bouyer goto free_reply_freeq;
2884 1.1 bouyer sc->sc_reply_postq_kva = MPII_DMA_KVA(sc->sc_reply_postq);
2885 1.15 bouyer memset(sc->sc_reply_postq_kva, 0xff, sc->sc_reply_post_qdepth *
2886 1.15 bouyer sizeof(struct mpii_reply_descr));
2887 1.1 bouyer
2888 1.1 bouyer return (0);
2889 1.1 bouyer
2890 1.1 bouyer free_reply_freeq:
2891 1.1 bouyer mpii_dmamem_free(sc, sc->sc_reply_freeq);
2892 1.1 bouyer return (1);
2893 1.1 bouyer }
2894 1.1 bouyer
2895 1.24 maxv static void
2896 1.1 bouyer mpii_init_queues(struct mpii_softc *sc)
2897 1.1 bouyer {
2898 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_init_queues\n", DEVNAME(sc));
2899 1.1 bouyer
2900 1.1 bouyer sc->sc_reply_free_host_index = sc->sc_reply_free_qdepth - 1;
2901 1.1 bouyer sc->sc_reply_post_host_index = 0;
2902 1.1 bouyer mpii_write_reply_free(sc, sc->sc_reply_free_host_index);
2903 1.1 bouyer mpii_write_reply_post(sc, sc->sc_reply_post_host_index);
2904 1.1 bouyer }
2905 1.1 bouyer
2906 1.24 maxv static void
2907 1.1 bouyer mpii_wait(struct mpii_softc *sc, struct mpii_ccb *ccb)
2908 1.1 bouyer {
2909 1.1 bouyer void (*done)(struct mpii_ccb *);
2910 1.1 bouyer void *cookie;
2911 1.1 bouyer
2912 1.1 bouyer done = ccb->ccb_done;
2913 1.1 bouyer cookie = ccb->ccb_cookie;
2914 1.1 bouyer
2915 1.1 bouyer ccb->ccb_done = mpii_wait_done;
2916 1.15 bouyer ccb->ccb_cookie = ccb;
2917 1.1 bouyer
2918 1.1 bouyer /* XXX this will wait forever for the ccb to complete */
2919 1.1 bouyer
2920 1.1 bouyer mpii_start(sc, ccb);
2921 1.1 bouyer
2922 1.15 bouyer mutex_enter(&ccb->ccb_mtx);
2923 1.15 bouyer while (ccb->ccb_cookie != NULL)
2924 1.15 bouyer cv_wait(&ccb->ccb_cv, &ccb->ccb_mtx);
2925 1.15 bouyer mutex_exit(&ccb->ccb_mtx);
2926 1.1 bouyer
2927 1.1 bouyer ccb->ccb_cookie = cookie;
2928 1.1 bouyer done(ccb);
2929 1.1 bouyer }
2930 1.1 bouyer
2931 1.24 maxv static void
2932 1.1 bouyer mpii_wait_done(struct mpii_ccb *ccb)
2933 1.1 bouyer {
2934 1.15 bouyer mutex_enter(&ccb->ccb_mtx);
2935 1.1 bouyer ccb->ccb_cookie = NULL;
2936 1.15 bouyer cv_signal(&ccb->ccb_cv);
2937 1.15 bouyer mutex_exit(&ccb->ccb_mtx);
2938 1.1 bouyer }
2939 1.1 bouyer
2940 1.24 maxv static void
2941 1.1 bouyer mpii_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
2942 1.1 bouyer void *arg)
2943 1.1 bouyer {
2944 1.1 bouyer struct scsipi_periph *periph;
2945 1.1 bouyer struct scsipi_xfer *xs;
2946 1.1 bouyer struct scsipi_adapter *adapt = chan->chan_adapter;
2947 1.1 bouyer struct mpii_softc *sc = device_private(adapt->adapt_dev);
2948 1.1 bouyer struct mpii_ccb *ccb;
2949 1.1 bouyer struct mpii_msg_scsi_io *io;
2950 1.1 bouyer struct mpii_device *dev;
2951 1.15 bouyer int target, timeout, ret;
2952 1.15 bouyer u_int16_t dev_handle;
2953 1.1 bouyer
2954 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: mpii_scsipi_request\n", DEVNAME(sc));
2955 1.15 bouyer
2956 1.1 bouyer switch (req) {
2957 1.1 bouyer case ADAPTER_REQ_GROW_RESOURCES:
2958 1.15 bouyer /* Not supported. */
2959 1.1 bouyer return;
2960 1.1 bouyer case ADAPTER_REQ_SET_XFER_MODE:
2961 1.1 bouyer {
2962 1.1 bouyer struct scsipi_xfer_mode *xm = arg;
2963 1.1 bouyer xm->xm_mode = PERIPH_CAP_TQING;
2964 1.15 bouyer xm->xm_period = 0;
2965 1.15 bouyer xm->xm_offset = 0;
2966 1.1 bouyer scsipi_async_event(&sc->sc_chan, ASYNC_EVENT_XFER_MODE, xm);
2967 1.1 bouyer return;
2968 1.1 bouyer }
2969 1.15 bouyer case ADAPTER_REQ_RUN_XFER:
2970 1.1 bouyer break;
2971 1.1 bouyer }
2972 1.1 bouyer
2973 1.1 bouyer xs = arg;
2974 1.1 bouyer periph = xs->xs_periph;
2975 1.1 bouyer target = periph->periph_target;
2976 1.1 bouyer
2977 1.1 bouyer if (xs->cmdlen > MPII_CDB_LEN) {
2978 1.15 bouyer DNPRINTF(MPII_D_CMD, "%s: CDB too big %d\n",
2979 1.1 bouyer DEVNAME(sc), xs->cmdlen);
2980 1.15 bouyer memset(&xs->sense, 0, sizeof(xs->sense));
2981 1.1 bouyer xs->sense.scsi_sense.response_code =
2982 1.1 bouyer SSD_RCODE_VALID | SSD_RCODE_CURRENT;
2983 1.1 bouyer xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
2984 1.1 bouyer xs->sense.scsi_sense.asc = 0x20;
2985 1.1 bouyer xs->error = XS_SENSE;
2986 1.1 bouyer scsipi_done(xs);
2987 1.1 bouyer return;
2988 1.1 bouyer }
2989 1.1 bouyer
2990 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
2991 1.1 bouyer if ((dev = sc->sc_devs[target]) == NULL) {
2992 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
2993 1.1 bouyer /* device no longer exists */
2994 1.1 bouyer xs->error = XS_SELTIMEOUT;
2995 1.1 bouyer scsipi_done(xs);
2996 1.1 bouyer return;
2997 1.1 bouyer }
2998 1.15 bouyer dev_handle = dev->dev_handle;
2999 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3000 1.1 bouyer
3001 1.15 bouyer ccb = mpii_get_ccb(sc);
3002 1.1 bouyer if (ccb == NULL) {
3003 1.1 bouyer xs->error = XS_RESOURCE_SHORTAGE;
3004 1.1 bouyer scsipi_done(xs);
3005 1.1 bouyer return;
3006 1.1 bouyer }
3007 1.22 kardel DNPRINTF(MPII_D_CMD, "%s: ccb_smid: %d xs->cmd->opcode: 0x%02x xs->xs_control: 0x%x\n",
3008 1.22 kardel DEVNAME(sc), ccb->ccb_smid, xs->cmd->opcode, xs->xs_control);
3009 1.1 bouyer
3010 1.1 bouyer ccb->ccb_cookie = xs;
3011 1.1 bouyer ccb->ccb_done = mpii_scsi_cmd_done;
3012 1.15 bouyer ccb->ccb_dev_handle = dev_handle;
3013 1.1 bouyer
3014 1.15 bouyer io = ccb->ccb_cmd;
3015 1.15 bouyer memset(io, 0, sizeof(*io));
3016 1.1 bouyer io->function = MPII_FUNCTION_SCSI_IO_REQUEST;
3017 1.1 bouyer io->sense_buffer_length = sizeof(xs->sense);
3018 1.15 bouyer io->sgl_offset0 = sizeof(struct mpii_msg_scsi_io) / 4;
3019 1.1 bouyer io->io_flags = htole16(xs->cmdlen);
3020 1.1 bouyer io->dev_handle = htole16(ccb->ccb_dev_handle);
3021 1.19 kardel io->lun[0] = htobe16(periph->periph_lun);
3022 1.1 bouyer
3023 1.1 bouyer switch (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
3024 1.1 bouyer case XS_CTL_DATA_IN:
3025 1.1 bouyer io->direction = MPII_SCSIIO_DIR_READ;
3026 1.1 bouyer break;
3027 1.1 bouyer case XS_CTL_DATA_OUT:
3028 1.1 bouyer io->direction = MPII_SCSIIO_DIR_WRITE;
3029 1.1 bouyer break;
3030 1.1 bouyer default:
3031 1.1 bouyer io->direction = MPII_SCSIIO_DIR_NONE;
3032 1.15 bouyer break;
3033 1.1 bouyer }
3034 1.1 bouyer
3035 1.1 bouyer io->tagging = MPII_SCSIIO_ATTR_SIMPLE_Q;
3036 1.1 bouyer
3037 1.1 bouyer memcpy(io->cdb, xs->cmd, xs->cmdlen);
3038 1.1 bouyer
3039 1.1 bouyer io->data_length = htole32(xs->datalen);
3040 1.1 bouyer
3041 1.15 bouyer /* sense data is at the end of a request */
3042 1.1 bouyer io->sense_buffer_low_address = htole32(ccb->ccb_cmd_dva +
3043 1.15 bouyer sc->sc_request_size - sizeof(struct scsi_sense_data));
3044 1.15 bouyer
3045 1.15 bouyer if (ISSET(sc->sc_flags, MPII_F_SAS3))
3046 1.15 bouyer ret = mpii_load_xs_sas3(ccb);
3047 1.15 bouyer else
3048 1.15 bouyer ret = mpii_load_xs(ccb);
3049 1.1 bouyer
3050 1.15 bouyer if (ret != 0) {
3051 1.1 bouyer xs->error = XS_DRIVER_STUFFUP;
3052 1.15 bouyer goto done;
3053 1.1 bouyer }
3054 1.1 bouyer
3055 1.1 bouyer if (xs->xs_control & XS_CTL_POLL) {
3056 1.1 bouyer if (mpii_poll(sc, ccb) != 0) {
3057 1.1 bouyer xs->error = XS_DRIVER_STUFFUP;
3058 1.15 bouyer goto done;
3059 1.1 bouyer }
3060 1.1 bouyer return;
3061 1.1 bouyer }
3062 1.15 bouyer timeout = mstohz(xs->timeout);
3063 1.1 bouyer if (timeout == 0)
3064 1.1 bouyer timeout = 1;
3065 1.1 bouyer callout_reset(&xs->xs_callout, timeout, mpii_scsi_cmd_tmo, ccb);
3066 1.1 bouyer mpii_start(sc, ccb);
3067 1.15 bouyer return;
3068 1.15 bouyer done:
3069 1.15 bouyer mpii_put_ccb(sc, ccb);
3070 1.15 bouyer scsipi_done(xs);
3071 1.1 bouyer }
3072 1.1 bouyer
3073 1.24 maxv static void
3074 1.1 bouyer mpii_scsi_cmd_tmo(void *xccb)
3075 1.1 bouyer {
3076 1.1 bouyer struct mpii_ccb *ccb = xccb;
3077 1.1 bouyer struct mpii_softc *sc = ccb->ccb_sc;
3078 1.15 bouyer bool start_work;
3079 1.1 bouyer
3080 1.1 bouyer printf("%s: mpii_scsi_cmd_tmo\n", DEVNAME(sc));
3081 1.1 bouyer
3082 1.1 bouyer if (ccb->ccb_state == MPII_CCB_QUEUED) {
3083 1.15 bouyer mutex_enter(&sc->sc_ssb_tmomtx);
3084 1.15 bouyer start_work = (SIMPLEQ_FIRST(&sc->sc_ccb_tmos) == 0);
3085 1.1 bouyer ccb->ccb_state = MPII_CCB_TIMEOUT;
3086 1.15 bouyer SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_tmos, ccb, ccb_link);
3087 1.15 bouyer if (start_work) {
3088 1.15 bouyer workqueue_enqueue(sc->sc_ssb_tmowk,
3089 1.15 bouyer &sc->sc_ssb_tmowork, NULL);
3090 1.15 bouyer }
3091 1.15 bouyer mutex_exit(&sc->sc_ssb_tmomtx);
3092 1.1 bouyer }
3093 1.1 bouyer }
3094 1.1 bouyer
3095 1.24 maxv static void
3096 1.1 bouyer mpii_scsi_cmd_tmo_handler(struct work *wk, void *cookie)
3097 1.1 bouyer {
3098 1.1 bouyer struct mpii_softc *sc = cookie;
3099 1.15 bouyer struct mpii_ccb *next;
3100 1.15 bouyer struct mpii_ccb *ccb;
3101 1.1 bouyer struct mpii_ccb *tccb;
3102 1.1 bouyer struct mpii_msg_scsi_task_request *stq;
3103 1.1 bouyer
3104 1.15 bouyer mutex_enter(&sc->sc_ssb_tmomtx);
3105 1.15 bouyer next = SIMPLEQ_FIRST(&sc->sc_ccb_tmos);
3106 1.15 bouyer SIMPLEQ_INIT(&sc->sc_ccb_tmos);
3107 1.15 bouyer mutex_exit(&sc->sc_ssb_tmomtx);
3108 1.15 bouyer
3109 1.15 bouyer while (next != NULL) {
3110 1.15 bouyer ccb = next;
3111 1.15 bouyer next = SIMPLEQ_NEXT(ccb, ccb_link);
3112 1.15 bouyer if (ccb->ccb_state != MPII_CCB_TIMEOUT)
3113 1.15 bouyer continue;
3114 1.15 bouyer tccb = mpii_get_ccb(sc);
3115 1.15 bouyer stq = tccb->ccb_cmd;
3116 1.15 bouyer stq->function = MPII_FUNCTION_SCSI_TASK_MGMT;
3117 1.15 bouyer stq->task_type = MPII_SCSI_TASK_TARGET_RESET;
3118 1.15 bouyer stq->dev_handle = htole16(ccb->ccb_dev_handle);
3119 1.1 bouyer
3120 1.15 bouyer tccb->ccb_done = mpii_scsi_cmd_tmo_done;
3121 1.15 bouyer mpii_wait(sc, tccb);
3122 1.1 bouyer }
3123 1.1 bouyer }
3124 1.1 bouyer
3125 1.24 maxv static void
3126 1.1 bouyer mpii_scsi_cmd_tmo_done(struct mpii_ccb *tccb)
3127 1.1 bouyer {
3128 1.15 bouyer mpii_put_ccb(tccb->ccb_sc, tccb);
3129 1.1 bouyer }
3130 1.1 bouyer
3131 1.3 kardel static u_int8_t
3132 1.3 kardel map_scsi_status(u_int8_t mpii_scsi_status)
3133 1.3 kardel {
3134 1.3 kardel u_int8_t scsi_status;
3135 1.3 kardel
3136 1.3 kardel switch (mpii_scsi_status)
3137 1.3 kardel {
3138 1.15 bouyer case MPII_SCSIIO_STATUS_GOOD:
3139 1.3 kardel scsi_status = SCSI_OK;
3140 1.3 kardel break;
3141 1.3 kardel
3142 1.15 bouyer case MPII_SCSIIO_STATUS_CHECK_COND:
3143 1.3 kardel scsi_status = SCSI_CHECK;
3144 1.3 kardel break;
3145 1.3 kardel
3146 1.15 bouyer case MPII_SCSIIO_STATUS_BUSY:
3147 1.3 kardel scsi_status = SCSI_BUSY;
3148 1.3 kardel break;
3149 1.3 kardel
3150 1.15 bouyer case MPII_SCSIIO_STATUS_INTERMEDIATE:
3151 1.3 kardel scsi_status = SCSI_INTERM;
3152 1.3 kardel break;
3153 1.3 kardel
3154 1.15 bouyer case MPII_SCSIIO_STATUS_INTERMEDIATE_CONDMET:
3155 1.3 kardel scsi_status = SCSI_INTERM;
3156 1.3 kardel break;
3157 1.3 kardel
3158 1.15 bouyer case MPII_SCSIIO_STATUS_RESERVATION_CONFLICT:
3159 1.3 kardel scsi_status = SCSI_RESV_CONFLICT;
3160 1.3 kardel break;
3161 1.3 kardel
3162 1.15 bouyer case MPII_SCSIIO_STATUS_CMD_TERM:
3163 1.15 bouyer case MPII_SCSIIO_STATUS_TASK_ABORTED:
3164 1.3 kardel scsi_status = SCSI_TERMINATED;
3165 1.3 kardel break;
3166 1.3 kardel
3167 1.15 bouyer case MPII_SCSIIO_STATUS_TASK_SET_FULL:
3168 1.3 kardel scsi_status = SCSI_QUEUE_FULL;
3169 1.3 kardel break;
3170 1.3 kardel
3171 1.15 bouyer case MPII_SCSIIO_STATUS_ACA_ACTIVE:
3172 1.3 kardel scsi_status = SCSI_ACA_ACTIVE;
3173 1.3 kardel break;
3174 1.3 kardel
3175 1.3 kardel default:
3176 1.3 kardel /* XXX: for the lack of anything better and other than OK */
3177 1.3 kardel scsi_status = 0xFF;
3178 1.3 kardel break;
3179 1.3 kardel }
3180 1.3 kardel
3181 1.3 kardel return scsi_status;
3182 1.3 kardel }
3183 1.1 bouyer
3184 1.24 maxv static void
3185 1.1 bouyer mpii_scsi_cmd_done(struct mpii_ccb *ccb)
3186 1.1 bouyer {
3187 1.1 bouyer struct mpii_msg_scsi_io_error *sie;
3188 1.1 bouyer struct mpii_softc *sc = ccb->ccb_sc;
3189 1.1 bouyer struct scsipi_xfer *xs = ccb->ccb_cookie;
3190 1.15 bouyer struct scsi_sense_data *sense;
3191 1.1 bouyer bus_dmamap_t dmap = ccb->ccb_dmamap;
3192 1.15 bouyer bool timeout = 1;
3193 1.1 bouyer
3194 1.1 bouyer callout_stop(&xs->xs_callout);
3195 1.1 bouyer if (ccb->ccb_state == MPII_CCB_TIMEOUT)
3196 1.1 bouyer timeout = 1;
3197 1.1 bouyer ccb->ccb_state = MPII_CCB_READY;
3198 1.1 bouyer
3199 1.1 bouyer if (xs->datalen != 0) {
3200 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
3201 1.1 bouyer (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
3202 1.1 bouyer BUS_DMASYNC_POSTWRITE);
3203 1.1 bouyer
3204 1.1 bouyer bus_dmamap_unload(sc->sc_dmat, dmap);
3205 1.1 bouyer }
3206 1.22 kardel
3207 1.22 kardel KASSERT(xs->error == XS_NOERROR);
3208 1.22 kardel KASSERT(xs->resid == xs->datalen);
3209 1.22 kardel KASSERT(xs->status == SCSI_OK);
3210 1.22 kardel
3211 1.1 bouyer if (ccb->ccb_rcb == NULL) {
3212 1.1 bouyer /* no scsi error, we're ok so drop out early */
3213 1.22 kardel xs->resid = 0;
3214 1.15 bouyer goto done;
3215 1.1 bouyer }
3216 1.1 bouyer
3217 1.1 bouyer sie = ccb->ccb_rcb->rcb_reply;
3218 1.1 bouyer
3219 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: mpii_scsi_cmd_done xs cmd: 0x%02x len: %d "
3220 1.15 bouyer "flags 0x%x\n", DEVNAME(sc), xs->cmd->opcode, xs->datalen,
3221 1.1 bouyer xs->xs_control);
3222 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: dev_handle: %d msg_length: %d "
3223 1.1 bouyer "function: 0x%02x\n", DEVNAME(sc), le16toh(sie->dev_handle),
3224 1.1 bouyer sie->msg_length, sie->function);
3225 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: vp_id: 0x%02x vf_id: 0x%02x\n", DEVNAME(sc),
3226 1.1 bouyer sie->vp_id, sie->vf_id);
3227 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: scsi_status: 0x%02x scsi_state: 0x%02x "
3228 1.1 bouyer "ioc_status: 0x%04x\n", DEVNAME(sc), sie->scsi_status,
3229 1.1 bouyer sie->scsi_state, le16toh(sie->ioc_status));
3230 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: ioc_loginfo: 0x%08x\n", DEVNAME(sc),
3231 1.1 bouyer le32toh(sie->ioc_loginfo));
3232 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: transfer_count: %d\n", DEVNAME(sc),
3233 1.1 bouyer le32toh(sie->transfer_count));
3234 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: sense_count: %d\n", DEVNAME(sc),
3235 1.1 bouyer le32toh(sie->sense_count));
3236 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: response_info: 0x%08x\n", DEVNAME(sc),
3237 1.1 bouyer le32toh(sie->response_info));
3238 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: task_tag: 0x%04x\n", DEVNAME(sc),
3239 1.1 bouyer le16toh(sie->task_tag));
3240 1.1 bouyer DNPRINTF(MPII_D_CMD, "%s: bidirectional_transfer_count: 0x%08x\n",
3241 1.1 bouyer DEVNAME(sc), le32toh(sie->bidirectional_transfer_count));
3242 1.1 bouyer
3243 1.3 kardel xs->status = map_scsi_status(sie->scsi_status);
3244 1.3 kardel
3245 1.1 bouyer switch (le16toh(sie->ioc_status) & MPII_IOCSTATUS_MASK) {
3246 1.1 bouyer case MPII_IOCSTATUS_SCSI_DATA_UNDERRUN:
3247 1.15 bouyer switch(sie->scsi_status) {
3248 1.15 bouyer case MPII_SCSIIO_STATUS_CHECK_COND:
3249 1.3 kardel xs->error = XS_SENSE;
3250 1.21 mrg /* FALLTHROUGH */
3251 1.15 bouyer case MPII_SCSIIO_STATUS_GOOD:
3252 1.1 bouyer xs->resid = xs->datalen - le32toh(sie->transfer_count);
3253 1.1 bouyer break;
3254 1.1 bouyer default:
3255 1.1 bouyer xs->error = XS_DRIVER_STUFFUP;
3256 1.1 bouyer break;
3257 1.1 bouyer }
3258 1.1 bouyer break;
3259 1.3 kardel
3260 1.1 bouyer case MPII_IOCSTATUS_SUCCESS:
3261 1.1 bouyer case MPII_IOCSTATUS_SCSI_RECOVERED_ERROR:
3262 1.3 kardel switch (sie->scsi_status) {
3263 1.15 bouyer case MPII_SCSIIO_STATUS_GOOD:
3264 1.22 kardel xs->resid = 0;
3265 1.1 bouyer break;
3266 1.1 bouyer
3267 1.15 bouyer case MPII_SCSIIO_STATUS_CHECK_COND:
3268 1.22 kardel xs->resid = 0;
3269 1.1 bouyer xs->error = XS_SENSE;
3270 1.1 bouyer break;
3271 1.15 bouyer
3272 1.15 bouyer case MPII_SCSIIO_STATUS_BUSY:
3273 1.15 bouyer case MPII_SCSIIO_STATUS_TASK_SET_FULL:
3274 1.1 bouyer xs->error = XS_BUSY;
3275 1.1 bouyer break;
3276 1.1 bouyer
3277 1.1 bouyer default:
3278 1.1 bouyer xs->error = XS_DRIVER_STUFFUP;
3279 1.1 bouyer }
3280 1.1 bouyer break;
3281 1.1 bouyer
3282 1.1 bouyer case MPII_IOCSTATUS_BUSY:
3283 1.1 bouyer case MPII_IOCSTATUS_INSUFFICIENT_RESOURCES:
3284 1.1 bouyer xs->error = XS_BUSY;
3285 1.1 bouyer break;
3286 1.1 bouyer
3287 1.1 bouyer case MPII_IOCSTATUS_SCSI_IOC_TERMINATED:
3288 1.1 bouyer case MPII_IOCSTATUS_SCSI_TASK_TERMINATED:
3289 1.1 bouyer xs->error = timeout ? XS_TIMEOUT : XS_RESET;
3290 1.1 bouyer break;
3291 1.1 bouyer
3292 1.1 bouyer case MPII_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3293 1.1 bouyer case MPII_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3294 1.1 bouyer xs->error = XS_SELTIMEOUT;
3295 1.1 bouyer break;
3296 1.1 bouyer
3297 1.1 bouyer default:
3298 1.1 bouyer xs->error = XS_DRIVER_STUFFUP;
3299 1.1 bouyer break;
3300 1.1 bouyer }
3301 1.1 bouyer
3302 1.15 bouyer sense = (struct scsi_sense_data *)((uintptr_t)ccb->ccb_cmd +
3303 1.15 bouyer sc->sc_request_size - sizeof(*sense));
3304 1.15 bouyer if (sie->scsi_state & MPII_SCSIIO_STATE_AUTOSENSE_VALID)
3305 1.15 bouyer memcpy(&xs->sense, sense, sizeof(xs->sense));
3306 1.1 bouyer
3307 1.22 kardel mpii_push_reply(sc, ccb->ccb_rcb);
3308 1.1 bouyer
3309 1.22 kardel done:
3310 1.1 bouyer mpii_put_ccb(sc, ccb);
3311 1.22 kardel
3312 1.22 kardel DNPRINTF(MPII_D_CMD, "%s: xs err: %d status: %#x len: %d resid: %d\n",
3313 1.22 kardel DEVNAME(sc), xs->error, xs->status, xs->datalen, xs->resid);
3314 1.22 kardel
3315 1.1 bouyer scsipi_done(xs);
3316 1.1 bouyer }
3317 1.1 bouyer
3318 1.1 bouyer #if 0
3319 1.15 bouyer int
3320 1.15 bouyer mpii_scsi_ioctl(struct scsi_link *link, u_long cmd, void *addr, int flag)
3321 1.15 bouyer {
3322 1.15 bouyer struct mpii_softc *sc = (struct mpii_softc *)link->adapter_softc;
3323 1.15 bouyer struct mpii_device *dev = sc->sc_devs[link->target];
3324 1.15 bouyer
3325 1.15 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_scsi_ioctl\n", DEVNAME(sc));
3326 1.15 bouyer
3327 1.15 bouyer switch (cmd) {
3328 1.15 bouyer case DIOCGCACHE:
3329 1.15 bouyer case DIOCSCACHE:
3330 1.15 bouyer if (dev != NULL && ISSET(dev->flags, MPII_DF_VOLUME)) {
3331 1.15 bouyer return (mpii_ioctl_cache(link, cmd,
3332 1.15 bouyer (struct dk_cache *)addr));
3333 1.15 bouyer }
3334 1.15 bouyer break;
3335 1.15 bouyer
3336 1.15 bouyer default:
3337 1.15 bouyer if (sc->sc_ioctl)
3338 1.15 bouyer return (sc->sc_ioctl(link->adapter_softc, cmd, addr));
3339 1.15 bouyer
3340 1.15 bouyer break;
3341 1.15 bouyer }
3342 1.15 bouyer
3343 1.15 bouyer return (ENOTTY);
3344 1.15 bouyer }
3345 1.15 bouyer
3346 1.15 bouyer int
3347 1.1 bouyer mpii_ioctl_cache(struct scsi_link *link, u_long cmd, struct dk_cache *dc)
3348 1.1 bouyer {
3349 1.1 bouyer struct mpii_softc *sc = (struct mpii_softc *)link->adapter_softc;
3350 1.1 bouyer struct mpii_device *dev = sc->sc_devs[link->target];
3351 1.1 bouyer struct mpii_cfg_raid_vol_pg0 *vpg;
3352 1.1 bouyer struct mpii_msg_raid_action_request *req;
3353 1.15 bouyer struct mpii_msg_raid_action_reply *rep;
3354 1.1 bouyer struct mpii_cfg_hdr hdr;
3355 1.1 bouyer struct mpii_ccb *ccb;
3356 1.1 bouyer u_int32_t addr = MPII_CFG_RAID_VOL_ADDR_HANDLE | dev->dev_handle;
3357 1.1 bouyer size_t pagelen;
3358 1.1 bouyer int rv = 0;
3359 1.1 bouyer int enabled;
3360 1.1 bouyer
3361 1.1 bouyer if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
3362 1.1 bouyer addr, MPII_PG_POLL, &hdr) != 0)
3363 1.1 bouyer return (EINVAL);
3364 1.1 bouyer
3365 1.1 bouyer pagelen = hdr.page_length * 4;
3366 1.12 jdolecek vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
3367 1.1 bouyer if (vpg == NULL)
3368 1.1 bouyer return (ENOMEM);
3369 1.1 bouyer
3370 1.1 bouyer if (mpii_req_cfg_page(sc, addr, MPII_PG_POLL, &hdr, 1,
3371 1.1 bouyer vpg, pagelen) != 0) {
3372 1.1 bouyer rv = EINVAL;
3373 1.1 bouyer goto done;
3374 1.1 bouyer }
3375 1.1 bouyer
3376 1.1 bouyer enabled = ((le16toh(vpg->volume_settings) &
3377 1.1 bouyer MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_MASK) ==
3378 1.1 bouyer MPII_CFG_RAID_VOL_0_SETTINGS_CACHE_ENABLED) ? 1 : 0;
3379 1.1 bouyer
3380 1.1 bouyer if (cmd == DIOCGCACHE) {
3381 1.1 bouyer dc->wrcache = enabled;
3382 1.1 bouyer dc->rdcache = 0;
3383 1.1 bouyer goto done;
3384 1.1 bouyer } /* else DIOCSCACHE */
3385 1.1 bouyer
3386 1.1 bouyer if (dc->rdcache) {
3387 1.1 bouyer rv = EOPNOTSUPP;
3388 1.1 bouyer goto done;
3389 1.1 bouyer }
3390 1.1 bouyer
3391 1.1 bouyer if (((dc->wrcache) ? 1 : 0) == enabled)
3392 1.1 bouyer goto done;
3393 1.1 bouyer
3394 1.15 bouyer ccb = scsi_io_get(&sc->sc_iopool, SCSI_POLL);
3395 1.1 bouyer if (ccb == NULL) {
3396 1.1 bouyer rv = ENOMEM;
3397 1.1 bouyer goto done;
3398 1.1 bouyer }
3399 1.1 bouyer
3400 1.1 bouyer ccb->ccb_done = mpii_empty_done;
3401 1.1 bouyer
3402 1.1 bouyer req = ccb->ccb_cmd;
3403 1.15 bouyer memset(req, 0, sizeof(*req));
3404 1.1 bouyer req->function = MPII_FUNCTION_RAID_ACTION;
3405 1.1 bouyer req->action = MPII_RAID_ACTION_CHANGE_VOL_WRITE_CACHE;
3406 1.1 bouyer req->vol_dev_handle = htole16(dev->dev_handle);
3407 1.1 bouyer req->action_data = htole32(dc->wrcache ?
3408 1.1 bouyer MPII_RAID_VOL_WRITE_CACHE_ENABLE :
3409 1.1 bouyer MPII_RAID_VOL_WRITE_CACHE_DISABLE);
3410 1.1 bouyer
3411 1.1 bouyer if (mpii_poll(sc, ccb) != 0) {
3412 1.1 bouyer rv = EIO;
3413 1.1 bouyer goto done;
3414 1.1 bouyer }
3415 1.1 bouyer
3416 1.1 bouyer if (ccb->ccb_rcb != NULL) {
3417 1.1 bouyer rep = ccb->ccb_rcb->rcb_reply;
3418 1.1 bouyer if ((rep->ioc_status != MPII_IOCSTATUS_SUCCESS) ||
3419 1.1 bouyer ((rep->action_data[0] &
3420 1.1 bouyer MPII_RAID_VOL_WRITE_CACHE_MASK) !=
3421 1.1 bouyer (dc->wrcache ? MPII_RAID_VOL_WRITE_CACHE_ENABLE :
3422 1.1 bouyer MPII_RAID_VOL_WRITE_CACHE_DISABLE)))
3423 1.1 bouyer rv = EINVAL;
3424 1.1 bouyer mpii_push_reply(sc, ccb->ccb_rcb);
3425 1.1 bouyer }
3426 1.1 bouyer
3427 1.15 bouyer scsi_io_put(&sc->sc_iopool, ccb);
3428 1.1 bouyer
3429 1.1 bouyer done:
3430 1.1 bouyer free(vpg, M_TEMP);
3431 1.1 bouyer return (rv);
3432 1.1 bouyer }
3433 1.15 bouyer #endif /* 0 */
3434 1.1 bouyer
3435 1.1 bouyer #if NBIO > 0
3436 1.24 maxv static int
3437 1.1 bouyer mpii_ioctl(device_t dev, u_long cmd, void *addr)
3438 1.1 bouyer {
3439 1.1 bouyer struct mpii_softc *sc = device_private(dev);
3440 1.15 bouyer int error = 0;
3441 1.1 bouyer
3442 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl ", DEVNAME(sc));
3443 1.1 bouyer
3444 1.1 bouyer switch (cmd) {
3445 1.1 bouyer case BIOCINQ:
3446 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "inq\n");
3447 1.1 bouyer error = mpii_ioctl_inq(sc, (struct bioc_inq *)addr);
3448 1.1 bouyer break;
3449 1.1 bouyer case BIOCVOL:
3450 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "vol\n");
3451 1.1 bouyer error = mpii_ioctl_vol(sc, (struct bioc_vol *)addr);
3452 1.1 bouyer break;
3453 1.1 bouyer case BIOCDISK:
3454 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "disk\n");
3455 1.1 bouyer error = mpii_ioctl_disk(sc, (struct bioc_disk *)addr);
3456 1.1 bouyer break;
3457 1.1 bouyer default:
3458 1.1 bouyer DNPRINTF(MPII_D_IOCTL, " invalid ioctl\n");
3459 1.15 bouyer error = ENOTTY;
3460 1.1 bouyer }
3461 1.1 bouyer
3462 1.1 bouyer return (error);
3463 1.1 bouyer }
3464 1.1 bouyer
3465 1.24 maxv static int
3466 1.1 bouyer mpii_ioctl_inq(struct mpii_softc *sc, struct bioc_inq *bi)
3467 1.1 bouyer {
3468 1.1 bouyer int i;
3469 1.1 bouyer
3470 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_inq\n", DEVNAME(sc));
3471 1.1 bouyer
3472 1.1 bouyer strlcpy(bi->bi_dev, DEVNAME(sc), sizeof(bi->bi_dev));
3473 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
3474 1.1 bouyer for (i = 0; i < sc->sc_max_devices; i++)
3475 1.1 bouyer if (sc->sc_devs[i] &&
3476 1.1 bouyer ISSET(sc->sc_devs[i]->flags, MPII_DF_VOLUME))
3477 1.1 bouyer bi->bi_novol++;
3478 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3479 1.1 bouyer return (0);
3480 1.1 bouyer }
3481 1.1 bouyer
3482 1.24 maxv static int
3483 1.1 bouyer mpii_ioctl_vol(struct mpii_softc *sc, struct bioc_vol *bv)
3484 1.1 bouyer {
3485 1.1 bouyer struct mpii_cfg_raid_vol_pg0 *vpg;
3486 1.1 bouyer struct mpii_cfg_hdr hdr;
3487 1.1 bouyer struct mpii_device *dev;
3488 1.1 bouyer size_t pagelen;
3489 1.1 bouyer u_int16_t volh;
3490 1.1 bouyer int rv, hcnt = 0;
3491 1.15 bouyer int percent;
3492 1.1 bouyer
3493 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_vol %d\n",
3494 1.1 bouyer DEVNAME(sc), bv->bv_volid);
3495 1.1 bouyer
3496 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
3497 1.15 bouyer if ((dev = mpii_find_vol(sc, bv->bv_volid)) == NULL) {
3498 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3499 1.1 bouyer return (ENODEV);
3500 1.15 bouyer }
3501 1.1 bouyer volh = dev->dev_handle;
3502 1.15 bouyer percent = dev->percent;
3503 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3504 1.1 bouyer
3505 1.1 bouyer if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
3506 1.1 bouyer MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0, &hdr) != 0) {
3507 1.1 bouyer printf("%s: unable to fetch header for raid volume page 0\n",
3508 1.1 bouyer DEVNAME(sc));
3509 1.1 bouyer return (EINVAL);
3510 1.1 bouyer }
3511 1.1 bouyer
3512 1.1 bouyer pagelen = hdr.page_length * 4;
3513 1.12 jdolecek vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
3514 1.1 bouyer if (vpg == NULL) {
3515 1.1 bouyer printf("%s: unable to allocate space for raid "
3516 1.1 bouyer "volume page 0\n", DEVNAME(sc));
3517 1.1 bouyer return (ENOMEM);
3518 1.1 bouyer }
3519 1.1 bouyer
3520 1.1 bouyer if (mpii_req_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0,
3521 1.1 bouyer &hdr, 1, vpg, pagelen) != 0) {
3522 1.1 bouyer printf("%s: unable to fetch raid volume page 0\n",
3523 1.1 bouyer DEVNAME(sc));
3524 1.1 bouyer free(vpg, M_TEMP);
3525 1.1 bouyer return (EINVAL);
3526 1.1 bouyer }
3527 1.1 bouyer
3528 1.1 bouyer switch (vpg->volume_state) {
3529 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_ONLINE:
3530 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_OPTIMAL:
3531 1.1 bouyer bv->bv_status = BIOC_SVONLINE;
3532 1.1 bouyer break;
3533 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_DEGRADED:
3534 1.1 bouyer if (ISSET(le32toh(vpg->volume_status),
3535 1.1 bouyer MPII_CFG_RAID_VOL_0_STATUS_RESYNC)) {
3536 1.1 bouyer bv->bv_status = BIOC_SVREBUILD;
3537 1.15 bouyer bv->bv_percent = percent;
3538 1.1 bouyer } else
3539 1.1 bouyer bv->bv_status = BIOC_SVDEGRADED;
3540 1.1 bouyer break;
3541 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_FAILED:
3542 1.1 bouyer bv->bv_status = BIOC_SVOFFLINE;
3543 1.1 bouyer break;
3544 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_INITIALIZING:
3545 1.1 bouyer bv->bv_status = BIOC_SVBUILDING;
3546 1.1 bouyer break;
3547 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_MISSING:
3548 1.1 bouyer default:
3549 1.1 bouyer bv->bv_status = BIOC_SVINVALID;
3550 1.1 bouyer break;
3551 1.1 bouyer }
3552 1.1 bouyer
3553 1.1 bouyer switch (vpg->volume_type) {
3554 1.1 bouyer case MPII_CFG_RAID_VOL_0_TYPE_RAID0:
3555 1.1 bouyer bv->bv_level = 0;
3556 1.1 bouyer break;
3557 1.1 bouyer case MPII_CFG_RAID_VOL_0_TYPE_RAID1:
3558 1.1 bouyer bv->bv_level = 1;
3559 1.1 bouyer break;
3560 1.1 bouyer case MPII_CFG_RAID_VOL_0_TYPE_RAID1E:
3561 1.1 bouyer case MPII_CFG_RAID_VOL_0_TYPE_RAID10:
3562 1.1 bouyer bv->bv_level = 10;
3563 1.1 bouyer break;
3564 1.1 bouyer default:
3565 1.1 bouyer bv->bv_level = -1;
3566 1.1 bouyer }
3567 1.1 bouyer
3568 1.1 bouyer if ((rv = mpii_bio_hs(sc, NULL, 0, vpg->hot_spare_pool, &hcnt)) != 0) {
3569 1.1 bouyer free(vpg, M_TEMP);
3570 1.1 bouyer return (rv);
3571 1.1 bouyer }
3572 1.1 bouyer
3573 1.1 bouyer bv->bv_nodisk = vpg->num_phys_disks + hcnt;
3574 1.1 bouyer
3575 1.1 bouyer bv->bv_size = le64toh(vpg->max_lba) * le16toh(vpg->block_size);
3576 1.1 bouyer
3577 1.1 bouyer free(vpg, M_TEMP);
3578 1.1 bouyer return (0);
3579 1.1 bouyer }
3580 1.1 bouyer
3581 1.24 maxv static int
3582 1.1 bouyer mpii_ioctl_disk(struct mpii_softc *sc, struct bioc_disk *bd)
3583 1.1 bouyer {
3584 1.1 bouyer struct mpii_cfg_raid_vol_pg0 *vpg;
3585 1.1 bouyer struct mpii_cfg_raid_vol_pg0_physdisk *pd;
3586 1.1 bouyer struct mpii_cfg_hdr hdr;
3587 1.1 bouyer struct mpii_device *dev;
3588 1.1 bouyer size_t pagelen;
3589 1.1 bouyer u_int16_t volh;
3590 1.1 bouyer u_int8_t dn;
3591 1.1 bouyer
3592 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_ioctl_disk %d/%d\n",
3593 1.1 bouyer DEVNAME(sc), bd->bd_volid, bd->bd_diskid);
3594 1.1 bouyer
3595 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
3596 1.15 bouyer if ((dev = mpii_find_vol(sc, bd->bd_volid)) == NULL) {
3597 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3598 1.1 bouyer return (ENODEV);
3599 1.15 bouyer }
3600 1.1 bouyer volh = dev->dev_handle;
3601 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3602 1.1 bouyer
3603 1.1 bouyer if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
3604 1.1 bouyer MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0, &hdr) != 0) {
3605 1.1 bouyer printf("%s: unable to fetch header for raid volume page 0\n",
3606 1.1 bouyer DEVNAME(sc));
3607 1.1 bouyer return (EINVAL);
3608 1.1 bouyer }
3609 1.1 bouyer
3610 1.1 bouyer pagelen = hdr.page_length * 4;
3611 1.12 jdolecek vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
3612 1.1 bouyer if (vpg == NULL) {
3613 1.1 bouyer printf("%s: unable to allocate space for raid "
3614 1.1 bouyer "volume page 0\n", DEVNAME(sc));
3615 1.1 bouyer return (ENOMEM);
3616 1.1 bouyer }
3617 1.1 bouyer
3618 1.1 bouyer if (mpii_req_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, 0,
3619 1.1 bouyer &hdr, 1, vpg, pagelen) != 0) {
3620 1.1 bouyer printf("%s: unable to fetch raid volume page 0\n",
3621 1.1 bouyer DEVNAME(sc));
3622 1.1 bouyer free(vpg, M_TEMP);
3623 1.1 bouyer return (EINVAL);
3624 1.1 bouyer }
3625 1.1 bouyer
3626 1.1 bouyer if (bd->bd_diskid >= vpg->num_phys_disks) {
3627 1.1 bouyer int nvdsk = vpg->num_phys_disks;
3628 1.1 bouyer int hsmap = vpg->hot_spare_pool;
3629 1.1 bouyer
3630 1.1 bouyer free(vpg, M_TEMP);
3631 1.1 bouyer return (mpii_bio_hs(sc, bd, nvdsk, hsmap, NULL));
3632 1.1 bouyer }
3633 1.1 bouyer
3634 1.1 bouyer pd = (struct mpii_cfg_raid_vol_pg0_physdisk *)(vpg + 1) +
3635 1.1 bouyer bd->bd_diskid;
3636 1.1 bouyer dn = pd->phys_disk_num;
3637 1.1 bouyer
3638 1.1 bouyer free(vpg, M_TEMP);
3639 1.1 bouyer return (mpii_bio_disk(sc, bd, dn));
3640 1.1 bouyer }
3641 1.1 bouyer
3642 1.24 maxv static int
3643 1.1 bouyer mpii_bio_hs(struct mpii_softc *sc, struct bioc_disk *bd, int nvdsk,
3644 1.1 bouyer int hsmap, int *hscnt)
3645 1.1 bouyer {
3646 1.1 bouyer struct mpii_cfg_raid_config_pg0 *cpg;
3647 1.1 bouyer struct mpii_raid_config_element *el;
3648 1.1 bouyer struct mpii_ecfg_hdr ehdr;
3649 1.1 bouyer size_t pagelen;
3650 1.1 bouyer int i, nhs = 0;
3651 1.1 bouyer
3652 1.1 bouyer if (bd) {
3653 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_hs %d\n", DEVNAME(sc),
3654 1.1 bouyer bd->bd_diskid - nvdsk);
3655 1.1 bouyer } else {
3656 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_hs\n", DEVNAME(sc));
3657 1.1 bouyer }
3658 1.1 bouyer
3659 1.1 bouyer if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_CONFIG,
3660 1.1 bouyer 0, MPII_CFG_RAID_CONFIG_ACTIVE_CONFIG, MPII_PG_EXTENDED,
3661 1.1 bouyer &ehdr) != 0) {
3662 1.1 bouyer printf("%s: unable to fetch header for raid config page 0\n",
3663 1.1 bouyer DEVNAME(sc));
3664 1.1 bouyer return (EINVAL);
3665 1.1 bouyer }
3666 1.1 bouyer
3667 1.1 bouyer pagelen = le16toh(ehdr.ext_page_length) * 4;
3668 1.12 jdolecek cpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
3669 1.1 bouyer if (cpg == NULL) {
3670 1.1 bouyer printf("%s: unable to allocate space for raid config page 0\n",
3671 1.1 bouyer DEVNAME(sc));
3672 1.1 bouyer return (ENOMEM);
3673 1.1 bouyer }
3674 1.1 bouyer
3675 1.1 bouyer if (mpii_req_cfg_page(sc, MPII_CFG_RAID_CONFIG_ACTIVE_CONFIG,
3676 1.1 bouyer MPII_PG_EXTENDED, &ehdr, 1, cpg, pagelen) != 0) {
3677 1.1 bouyer printf("%s: unable to fetch raid config page 0\n",
3678 1.1 bouyer DEVNAME(sc));
3679 1.1 bouyer free(cpg, M_TEMP);
3680 1.1 bouyer return (EINVAL);
3681 1.1 bouyer }
3682 1.1 bouyer
3683 1.1 bouyer el = (struct mpii_raid_config_element *)(cpg + 1);
3684 1.1 bouyer for (i = 0; i < cpg->num_elements; i++, el++) {
3685 1.1 bouyer if (ISSET(le16toh(el->element_flags),
3686 1.1 bouyer MPII_RAID_CONFIG_ELEMENT_FLAG_HSP_PHYS_DISK) &&
3687 1.1 bouyer el->hot_spare_pool == hsmap) {
3688 1.1 bouyer /*
3689 1.1 bouyer * diskid comparison is based on the idea that all
3690 1.1 bouyer * disks are counted by the bio(4) in sequence, thus
3691 1.1 bouyer * substracting the number of disks in the volume
3692 1.1 bouyer * from the diskid yields us a "relative" hotspare
3693 1.1 bouyer * number, which is good enough for us.
3694 1.1 bouyer */
3695 1.1 bouyer if (bd != NULL && bd->bd_diskid == nhs + nvdsk) {
3696 1.1 bouyer u_int8_t dn = el->phys_disk_num;
3697 1.1 bouyer
3698 1.1 bouyer free(cpg, M_TEMP);
3699 1.1 bouyer return (mpii_bio_disk(sc, bd, dn));
3700 1.1 bouyer }
3701 1.1 bouyer nhs++;
3702 1.1 bouyer }
3703 1.1 bouyer }
3704 1.1 bouyer
3705 1.1 bouyer if (hscnt)
3706 1.1 bouyer *hscnt = nhs;
3707 1.1 bouyer
3708 1.1 bouyer free(cpg, M_TEMP);
3709 1.1 bouyer return (0);
3710 1.1 bouyer }
3711 1.1 bouyer
3712 1.24 maxv static int
3713 1.1 bouyer mpii_bio_disk(struct mpii_softc *sc, struct bioc_disk *bd, u_int8_t dn)
3714 1.1 bouyer {
3715 1.1 bouyer struct mpii_cfg_raid_physdisk_pg0 *ppg;
3716 1.1 bouyer struct mpii_cfg_hdr hdr;
3717 1.1 bouyer struct mpii_device *dev;
3718 1.1 bouyer int len;
3719 1.1 bouyer
3720 1.1 bouyer DNPRINTF(MPII_D_IOCTL, "%s: mpii_bio_disk %d\n", DEVNAME(sc),
3721 1.1 bouyer bd->bd_diskid);
3722 1.1 bouyer
3723 1.12 jdolecek ppg = malloc(sizeof(*ppg), M_TEMP, M_WAITOK | M_ZERO);
3724 1.1 bouyer if (ppg == NULL) {
3725 1.1 bouyer printf("%s: unable to allocate space for raid physical disk "
3726 1.1 bouyer "page 0\n", DEVNAME(sc));
3727 1.1 bouyer return (ENOMEM);
3728 1.1 bouyer }
3729 1.1 bouyer
3730 1.1 bouyer hdr.page_version = 0;
3731 1.1 bouyer hdr.page_length = sizeof(*ppg) / 4;
3732 1.1 bouyer hdr.page_number = 0;
3733 1.1 bouyer hdr.page_type = MPII_CONFIG_REQ_PAGE_TYPE_RAID_PD;
3734 1.1 bouyer
3735 1.1 bouyer if (mpii_req_cfg_page(sc, MPII_CFG_RAID_PHYS_DISK_ADDR_NUMBER | dn, 0,
3736 1.1 bouyer &hdr, 1, ppg, sizeof(*ppg)) != 0) {
3737 1.1 bouyer printf("%s: unable to fetch raid drive page 0\n",
3738 1.1 bouyer DEVNAME(sc));
3739 1.1 bouyer free(ppg, M_TEMP);
3740 1.1 bouyer return (EINVAL);
3741 1.1 bouyer }
3742 1.1 bouyer
3743 1.1 bouyer bd->bd_target = ppg->phys_disk_num;
3744 1.1 bouyer
3745 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
3746 1.1 bouyer if ((dev = mpii_find_dev(sc, le16toh(ppg->dev_handle))) == NULL) {
3747 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3748 1.1 bouyer bd->bd_status = BIOC_SDINVALID;
3749 1.1 bouyer free(ppg, M_TEMP);
3750 1.1 bouyer return (0);
3751 1.1 bouyer }
3752 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3753 1.1 bouyer
3754 1.1 bouyer switch (ppg->phys_disk_state) {
3755 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_ONLINE:
3756 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_OPTIMAL:
3757 1.1 bouyer bd->bd_status = BIOC_SDONLINE;
3758 1.1 bouyer break;
3759 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_OFFLINE:
3760 1.1 bouyer if (ppg->offline_reason ==
3761 1.1 bouyer MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILED ||
3762 1.1 bouyer ppg->offline_reason ==
3763 1.1 bouyer MPII_CFG_RAID_PHYDISK_0_OFFLINE_FAILEDREQ)
3764 1.1 bouyer bd->bd_status = BIOC_SDFAILED;
3765 1.1 bouyer else
3766 1.1 bouyer bd->bd_status = BIOC_SDOFFLINE;
3767 1.1 bouyer break;
3768 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_DEGRADED:
3769 1.1 bouyer bd->bd_status = BIOC_SDFAILED;
3770 1.1 bouyer break;
3771 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_REBUILDING:
3772 1.1 bouyer bd->bd_status = BIOC_SDREBUILD;
3773 1.1 bouyer break;
3774 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_HOTSPARE:
3775 1.1 bouyer bd->bd_status = BIOC_SDHOTSPARE;
3776 1.1 bouyer break;
3777 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_NOTCONFIGURED:
3778 1.1 bouyer bd->bd_status = BIOC_SDUNUSED;
3779 1.1 bouyer break;
3780 1.1 bouyer case MPII_CFG_RAID_PHYDISK_0_STATE_NOTCOMPATIBLE:
3781 1.1 bouyer default:
3782 1.1 bouyer bd->bd_status = BIOC_SDINVALID;
3783 1.1 bouyer break;
3784 1.1 bouyer }
3785 1.1 bouyer
3786 1.1 bouyer bd->bd_size = le64toh(ppg->dev_max_lba) * le16toh(ppg->block_size);
3787 1.1 bouyer
3788 1.8 christos strnvisx(bd->bd_vendor, sizeof(bd->bd_vendor),
3789 1.8 christos ppg->vendor_id, sizeof(ppg->vendor_id),
3790 1.8 christos VIS_TRIM|VIS_SAFE|VIS_OCTAL);
3791 1.1 bouyer len = strlen(bd->bd_vendor);
3792 1.1 bouyer bd->bd_vendor[len] = ' ';
3793 1.8 christos strnvisx(&bd->bd_vendor[len + 1], sizeof(ppg->vendor_id) - len - 1,
3794 1.8 christos ppg->product_id, sizeof(ppg->product_id),
3795 1.8 christos VIS_TRIM|VIS_SAFE|VIS_OCTAL);
3796 1.8 christos strnvisx(bd->bd_serial, sizeof(bd->bd_serial),
3797 1.8 christos ppg->serial, sizeof(ppg->serial), VIS_TRIM|VIS_SAFE|VIS_OCTAL);
3798 1.1 bouyer
3799 1.1 bouyer free(ppg, M_TEMP);
3800 1.1 bouyer return (0);
3801 1.1 bouyer }
3802 1.1 bouyer
3803 1.24 maxv static struct mpii_device *
3804 1.1 bouyer mpii_find_vol(struct mpii_softc *sc, int volid)
3805 1.1 bouyer {
3806 1.1 bouyer struct mpii_device *dev = NULL;
3807 1.1 bouyer
3808 1.15 bouyer KASSERT(mutex_owned(&sc->sc_devs_mtx));
3809 1.1 bouyer if (sc->sc_vd_id_low + volid >= sc->sc_max_devices)
3810 1.1 bouyer return (NULL);
3811 1.1 bouyer dev = sc->sc_devs[sc->sc_vd_id_low + volid];
3812 1.1 bouyer if (dev && ISSET(dev->flags, MPII_DF_VOLUME))
3813 1.1 bouyer return (dev);
3814 1.1 bouyer return (NULL);
3815 1.1 bouyer }
3816 1.1 bouyer
3817 1.1 bouyer /*
3818 1.1 bouyer * Non-sleeping lightweight version of the mpii_ioctl_vol
3819 1.1 bouyer */
3820 1.24 maxv static int
3821 1.1 bouyer mpii_bio_volstate(struct mpii_softc *sc, struct bioc_vol *bv)
3822 1.1 bouyer {
3823 1.1 bouyer struct mpii_cfg_raid_vol_pg0 *vpg;
3824 1.1 bouyer struct mpii_cfg_hdr hdr;
3825 1.1 bouyer struct mpii_device *dev = NULL;
3826 1.1 bouyer size_t pagelen;
3827 1.1 bouyer u_int16_t volh;
3828 1.1 bouyer
3829 1.15 bouyer mutex_enter(&sc->sc_devs_mtx);
3830 1.15 bouyer if ((dev = mpii_find_vol(sc, bv->bv_volid)) == NULL) {
3831 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3832 1.1 bouyer return (ENODEV);
3833 1.15 bouyer }
3834 1.1 bouyer volh = dev->dev_handle;
3835 1.15 bouyer mutex_exit(&sc->sc_devs_mtx);
3836 1.1 bouyer
3837 1.15 bouyer if (mpii_req_cfg_header(sc, MPII_CONFIG_REQ_PAGE_TYPE_RAID_VOL, 0,
3838 1.15 bouyer MPII_CFG_RAID_VOL_ADDR_HANDLE | volh, MPII_PG_POLL, &hdr) != 0) {
3839 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: unable to fetch header for raid "
3840 1.1 bouyer "volume page 0\n", DEVNAME(sc));
3841 1.1 bouyer return (EINVAL);
3842 1.1 bouyer }
3843 1.1 bouyer
3844 1.1 bouyer pagelen = hdr.page_length * 4;
3845 1.23 chs vpg = malloc(pagelen, M_TEMP, M_WAITOK | M_ZERO);
3846 1.15 bouyer if (mpii_req_cfg_page(sc, MPII_CFG_RAID_VOL_ADDR_HANDLE | volh,
3847 1.15 bouyer MPII_PG_POLL, &hdr, 1, vpg, pagelen) != 0) {
3848 1.1 bouyer DNPRINTF(MPII_D_MISC, "%s: unable to fetch raid volume "
3849 1.1 bouyer "page 0\n", DEVNAME(sc));
3850 1.1 bouyer free(vpg, M_TEMP);
3851 1.1 bouyer return (EINVAL);
3852 1.1 bouyer }
3853 1.1 bouyer
3854 1.1 bouyer switch (vpg->volume_state) {
3855 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_ONLINE:
3856 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_OPTIMAL:
3857 1.1 bouyer bv->bv_status = BIOC_SVONLINE;
3858 1.1 bouyer break;
3859 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_DEGRADED:
3860 1.1 bouyer if (ISSET(le32toh(vpg->volume_status),
3861 1.1 bouyer MPII_CFG_RAID_VOL_0_STATUS_RESYNC))
3862 1.1 bouyer bv->bv_status = BIOC_SVREBUILD;
3863 1.1 bouyer else
3864 1.1 bouyer bv->bv_status = BIOC_SVDEGRADED;
3865 1.1 bouyer break;
3866 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_FAILED:
3867 1.1 bouyer bv->bv_status = BIOC_SVOFFLINE;
3868 1.1 bouyer break;
3869 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_INITIALIZING:
3870 1.1 bouyer bv->bv_status = BIOC_SVBUILDING;
3871 1.1 bouyer break;
3872 1.1 bouyer case MPII_CFG_RAID_VOL_0_STATE_MISSING:
3873 1.1 bouyer default:
3874 1.1 bouyer bv->bv_status = BIOC_SVINVALID;
3875 1.1 bouyer break;
3876 1.1 bouyer }
3877 1.1 bouyer
3878 1.1 bouyer free(vpg, M_TEMP);
3879 1.1 bouyer return (0);
3880 1.1 bouyer }
3881 1.1 bouyer
3882 1.24 maxv static int
3883 1.1 bouyer mpii_create_sensors(struct mpii_softc *sc)
3884 1.1 bouyer {
3885 1.1 bouyer int i, rv;
3886 1.1 bouyer
3887 1.15 bouyer DNPRINTF(MPII_D_MISC, "%s: mpii_create_sensors(%d)\n",
3888 1.15 bouyer DEVNAME(sc), sc->sc_max_volumes);
3889 1.1 bouyer sc->sc_sme = sysmon_envsys_create();
3890 1.15 bouyer sc->sc_sensors = malloc(sizeof(envsys_data_t) * sc->sc_max_volumes,
3891 1.23 chs M_DEVBUF, M_WAITOK | M_ZERO);
3892 1.1 bouyer
3893 1.15 bouyer for (i = 0; i < sc->sc_max_volumes; i++) {
3894 1.1 bouyer sc->sc_sensors[i].units = ENVSYS_DRIVE;
3895 1.1 bouyer sc->sc_sensors[i].state = ENVSYS_SINVALID;
3896 1.1 bouyer sc->sc_sensors[i].value_cur = ENVSYS_DRIVE_EMPTY;
3897 1.1 bouyer sc->sc_sensors[i].flags |= ENVSYS_FMONSTCHANGED;
3898 1.1 bouyer
3899 1.15 bouyer /* logical drives */
3900 1.1 bouyer snprintf(sc->sc_sensors[i].desc,
3901 1.1 bouyer sizeof(sc->sc_sensors[i].desc), "%s:%d",
3902 1.15 bouyer DEVNAME(sc), i);
3903 1.15 bouyer if ((rv = sysmon_envsys_sensor_attach(sc->sc_sme,
3904 1.1 bouyer &sc->sc_sensors[i])) != 0) {
3905 1.1 bouyer aprint_error_dev(sc->sc_dev,
3906 1.1 bouyer "unable to attach sensor (rv = %d)\n", rv);
3907 1.15 bouyer goto out;
3908 1.15 bouyer }
3909 1.1 bouyer }
3910 1.1 bouyer sc->sc_sme->sme_name = DEVNAME(sc);
3911 1.15 bouyer sc->sc_sme->sme_cookie = sc;
3912 1.1 bouyer sc->sc_sme->sme_refresh = mpii_refresh_sensors;
3913 1.1 bouyer
3914 1.1 bouyer rv = sysmon_envsys_register(sc->sc_sme);
3915 1.1 bouyer if (rv != 0) {
3916 1.1 bouyer aprint_error_dev(sc->sc_dev,
3917 1.15 bouyer "unable to register with sysmon (rv = %d)\n", rv);
3918 1.1 bouyer goto out;
3919 1.1 bouyer }
3920 1.1 bouyer return 0;
3921 1.1 bouyer
3922 1.1 bouyer out:
3923 1.1 bouyer free(sc->sc_sensors, M_DEVBUF);
3924 1.1 bouyer sysmon_envsys_destroy(sc->sc_sme);
3925 1.1 bouyer sc->sc_sme = NULL;
3926 1.15 bouyer return 1;
3927 1.1 bouyer }
3928 1.1 bouyer
3929 1.24 maxv static int
3930 1.1 bouyer mpii_destroy_sensors(struct mpii_softc *sc)
3931 1.1 bouyer {
3932 1.15 bouyer if (sc->sc_sme == NULL)
3933 1.1 bouyer return 0;
3934 1.1 bouyer sysmon_envsys_unregister(sc->sc_sme);
3935 1.1 bouyer sc->sc_sme = NULL;
3936 1.1 bouyer free(sc->sc_sensors, M_DEVBUF);
3937 1.1 bouyer return 0;
3938 1.15 bouyer
3939 1.1 bouyer }
3940 1.1 bouyer
3941 1.24 maxv static void
3942 1.1 bouyer mpii_refresh_sensors(struct sysmon_envsys *sme, envsys_data_t *edata)
3943 1.1 bouyer {
3944 1.15 bouyer struct mpii_softc *sc = sme->sme_cookie;
3945 1.1 bouyer struct bioc_vol bv;
3946 1.1 bouyer
3947 1.15 bouyer memset(&bv, 0, sizeof(bv));
3948 1.1 bouyer bv.bv_volid = edata->sensor;
3949 1.15 bouyer if (mpii_bio_volstate(sc, &bv))
3950 1.6 christos bv.bv_status = BIOC_SVINVALID;
3951 1.6 christos bio_vol_to_envsys(edata, &bv);
3952 1.1 bouyer }
3953 1.1 bouyer #endif /* NBIO > 0 */
3954