2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2010, LSI Corp.
6 * Author : Manjunath Ranganathaiah
7 * Support: freebsdraid@lsi.com
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of the <ORGANIZATION> nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
39 #include <dev/tws/tws.h>
40 #include <dev/tws/tws_hdm.h>
41 #include <dev/tws/tws_services.h>
44 void tws_q_insert_tail(struct tws_softc *sc, struct tws_request *req,
46 struct tws_request * tws_q_remove_request(struct tws_softc *sc,
47 struct tws_request *req, u_int8_t q_type );
48 struct tws_request *tws_q_remove_head(struct tws_softc *sc, u_int8_t q_type );
49 void tws_q_insert_head(struct tws_softc *sc, struct tws_request *req,
51 struct tws_request * tws_q_remove_tail(struct tws_softc *sc, u_int8_t q_type );
52 void tws_print_stats(void *arg);
54 struct tws_sense *tws_find_sense_from_mfa(struct tws_softc *sc, u_int64_t mfa);
56 static struct error_desc array[] = {
57 { "Cannot add sysctl tree node", 0x2000, ERROR,
58 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
59 { "Register window not available", 0x2001, ERROR,
60 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
61 { "Can't allocate register window", 0x2002, ERROR,
62 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
63 { "Can't allocate interrupt", 0x2003, ERROR,
64 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
65 { "Can't set up interrupt", 0x2004, ERROR,
66 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
67 { "Couldn't intialize CAM", 0x2007, ERROR,
68 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
69 { "Couldn't create SIM device queue", 0x2100, ENOMEM,
70 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
71 { "Unable to create SIM entry", 0x2101, ENOMEM,
72 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
73 { "Unable to register the bus", 0x2102, ENXIO,
74 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
75 { "Unable to create the path", 0x2103, ENXIO,
76 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
77 { "Bus scan request to CAM failed", 0x2104, ENXIO,
78 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
79 { "Unable to intialize the driver", 0x2008, ENXIO,
80 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
81 { "Unable to intialize the controller", 0x2009, ENXIO,
82 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
86 tws_trace(const char *file, const char *fun, int linenum,
87 struct tws_softc *sc, char *desc, u_int64_t val1, u_int64_t val2)
90 struct tws_trace_rec *rec = (struct tws_trace_rec *)sc->trace_q.q;
91 volatile u_int16_t head, tail;
94 head = sc->trace_q.head;
95 tail = sc->trace_q.tail;
97 getnanotime(&rec[tail].ts);
99 strncpy(rec[tail].fname, file, TWS_TRACE_FNAME_LEN);
100 strncpy(rec[tail].func, fun, TWS_TRACE_FUNC_LEN);
101 rec[tail].linenum = linenum;
102 strncpy(rec[tail].desc, desc, TWS_TRACE_DESC_LEN);
103 rec[tail].val1 = val1;
104 rec[tail].val2 = val2;
106 tail = (tail+1) % sc->trace_q.depth;
108 if ( head == tail ) {
109 sc->trace_q.overflow = 1;
110 sc->trace_q.head = (head+1) % sc->trace_q.depth;
112 sc->trace_q.tail = tail;
115 tws_circular_q_insert(sc, &sc->trace_q,
116 &rec, sizeof(struct tws_trace_rec));
119 strcpy(fmt, "%05d:%s::%s :%s: 0x%016lx : 0x%016lx \n");
121 strcpy(fmt, "%05d:%s::%s :%s: 0x%016llx : 0x%016llx \n");
124 printf("%05d:%s::%s :%s: 0x%016llx : 0x%016llx \n",
125 linenum, file, fun, desc, val1, val2);
127 printf(fmt, linenum, file, fun, desc, val1, val2);
131 tws_log(struct tws_softc *sc, int index)
133 device_printf((sc)->tws_dev, array[index].fmt,
134 array[index].error_str,
135 array[index].error_code,
136 array[index].severity_level,
140 /* ----------- swap functions ----------- */
143 tws_swap16(u_int16_t val)
145 return((val << 8) | (val >> 8));
149 tws_swap32(u_int32_t val)
151 return(((val << 24) | ((val << 8) & (0xFF0000)) |
152 ((val >> 8) & (0xFF00)) | (val >> 24)));
156 tws_swap64(u_int64_t val)
158 return((((u_int64_t)(tws_swap32(((u_int32_t *)(&(val)))[1]))) << 32) |
159 ((u_int32_t)(tws_swap32(((u_int32_t *)(&(val)))[0]))));
162 /* ----------- reg access ----------- */
165 tws_write_reg(struct tws_softc *sc, int offset,
166 u_int32_t value, int size)
168 bus_space_tag_t bus_tag = sc->bus_tag;
169 bus_space_handle_t bus_handle = sc->bus_handle;
172 bus_space_write_4(bus_tag, bus_handle, offset, value);
175 bus_space_write_2(bus_tag, bus_handle, offset,
178 bus_space_write_1(bus_tag, bus_handle, offset, (u_int8_t)value);
182 tws_read_reg(struct tws_softc *sc, int offset, int size)
184 bus_space_tag_t bus_tag = sc->bus_tag;
185 bus_space_handle_t bus_handle = sc->bus_handle;
188 return((u_int32_t)bus_space_read_4(bus_tag, bus_handle, offset));
190 return((u_int32_t)bus_space_read_2(bus_tag, bus_handle, offset));
192 return((u_int32_t)bus_space_read_1(bus_tag, bus_handle, offset));
195 /* --------------------- Q service --------------------- */
198 * intialize q pointers with null.
201 tws_init_qs(struct tws_softc *sc)
204 mtx_lock(&sc->q_lock);
205 for(int i=0;i<TWS_MAX_QS;i++) {
206 sc->q_head[i] = NULL;
207 sc->q_tail[i] = NULL;
209 mtx_unlock(&sc->q_lock);
213 /* called with lock held */
215 tws_insert2_empty_q(struct tws_softc *sc, struct tws_request *req,
219 mtx_assert(&sc->q_lock, MA_OWNED);
220 req->next = req->prev = NULL;
221 sc->q_head[q_type] = sc->q_tail[q_type] = req;
225 /* called with lock held */
227 tws_q_insert_head(struct tws_softc *sc, struct tws_request *req,
231 mtx_assert(&sc->q_lock, MA_OWNED);
232 if ( sc->q_head[q_type] == NULL ) {
233 tws_insert2_empty_q(sc, req, q_type);
235 req->next = sc->q_head[q_type];
237 sc->q_head[q_type]->prev = req;
238 sc->q_head[q_type] = req;
243 /* called with lock held */
245 tws_q_insert_tail(struct tws_softc *sc, struct tws_request *req,
249 mtx_assert(&sc->q_lock, MA_OWNED);
250 if ( sc->q_tail[q_type] == NULL ) {
251 tws_insert2_empty_q(sc, req, q_type);
253 req->prev = sc->q_tail[q_type];
255 sc->q_tail[q_type]->next = req;
256 sc->q_tail[q_type] = req;
261 /* called with lock held */
263 tws_q_remove_head(struct tws_softc *sc, u_int8_t q_type )
266 struct tws_request *r;
268 mtx_assert(&sc->q_lock, MA_OWNED);
269 r = sc->q_head[q_type];
272 if ( r->next == NULL && r->prev == NULL ) {
274 sc->q_head[q_type] = sc->q_tail[q_type] = NULL;
276 sc->q_head[q_type] = r->next;
277 r->next->prev = NULL;
284 /* called with lock held */
286 tws_q_remove_tail(struct tws_softc *sc, u_int8_t q_type )
289 struct tws_request *r;
291 mtx_assert(&sc->q_lock, MA_OWNED);
292 r = sc->q_tail[q_type];
295 if ( r->next == NULL && r->prev == NULL ) {
297 sc->q_head[q_type] = sc->q_tail[q_type] = NULL;
299 sc->q_tail[q_type] = r->prev;
300 r->prev->next = NULL;
307 /* returns removed request if successful. return NULL otherwise */
308 /* called with lock held */
310 tws_q_remove_request(struct tws_softc *sc, struct tws_request *req,
314 struct tws_request *r;
316 mtx_assert(&sc->q_lock, MA_OWNED);
318 TWS_TRACE_DEBUG(sc, "null req", 0, q_type);
322 if ( req == sc->q_head[q_type] )
323 return(tws_q_remove_head(sc, q_type));
324 if ( req == sc->q_tail[q_type] )
325 return(tws_q_remove_tail(sc, q_type));
327 /* The given node is not at head or tail.
328 * It's in the middle and there are more than
329 * 2 elements on the q.
332 if ( req->next == NULL || req->prev == NULL ) {
333 TWS_TRACE_DEBUG(sc, "invalid req", 0, q_type);
338 r = sc->q_head[q_type];
346 TWS_TRACE_DEBUG(sc, "req not in q", 0, req->request_id);
351 req->prev->next = r->next;
352 req->next->prev = r->prev;
359 tws_find_sense_from_mfa(struct tws_softc *sc, u_int64_t mfa)
363 TWS_TRACE_DEBUG(sc, "entry",sc,mfa);
365 i = (mfa - sc->dma_mem_phys) / sizeof(struct tws_command_packet);
366 if ( i>= 0 && i<tws_queue_depth) {
367 s = &sc->sense_bufs[i];
368 if ( mfa == s->hdr_pkt_phy )
372 TWS_TRACE_DEBUG(sc, "return null",0,mfa);
377 /* --------------------- Q service end --------------------- */
378 /* --------------------- misc service start --------------------- */
381 tws_print_stats(void *arg)
384 struct tws_softc *sc = (struct tws_softc *)arg;
386 TWS_TRACE(sc, "reqs(in, out)", sc->stats.reqs_in, sc->stats.reqs_out);
387 TWS_TRACE(sc, "reqs(err, intrs)", sc->stats.reqs_errored
388 , sc->stats.num_intrs);
389 TWS_TRACE(sc, "reqs(ioctls, scsi)", sc->stats.ioctls
390 , sc->stats.scsi_ios);
391 callout_reset(&sc->stats_timer, 300 * hz, tws_print_stats, sc);
393 /* --------------------- misc service end --------------------- */