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);
58 static struct error_desc array[] = {
59 { "Cannot add sysctl tree node", 0x2000, ERROR,
60 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
61 { "Register window not available", 0x2001, ERROR,
62 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
63 { "Can't allocate register window", 0x2002, ERROR,
64 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
65 { "Can't allocate interrupt", 0x2003, ERROR,
66 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
67 { "Can't set up interrupt", 0x2004, ERROR,
68 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
69 { "Couldn't intialize CAM", 0x2007, ERROR,
70 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
71 { "Couldn't create SIM device queue", 0x2100, ENOMEM,
72 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
73 { "Unable to create SIM entry", 0x2101, ENOMEM,
74 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
75 { "Unable to register the bus", 0x2102, ENXIO,
76 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
77 { "Unable to create the path", 0x2103, ENXIO,
78 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
79 { "Bus scan request to CAM failed", 0x2104, ENXIO,
80 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
81 { "Unable to intialize the driver", 0x2008, ENXIO,
82 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
83 { "Unable to intialize the controller", 0x2009, ENXIO,
84 "%s: (0x%02X: 0x%04X): %s:\n", "ERROR" },
88 tws_trace(const char *file, const char *fun, int linenum,
89 struct tws_softc *sc, char *desc, u_int64_t val1, u_int64_t val2)
93 struct tws_trace_rec *rec = (struct tws_trace_rec *)sc->trace_q.q;
94 volatile u_int16_t head, tail;
97 head = sc->trace_q.head;
98 tail = sc->trace_q.tail;
100 getnanotime(&rec[tail].ts);
102 strncpy(rec[tail].fname, file, TWS_TRACE_FNAME_LEN);
103 strncpy(rec[tail].func, fun, TWS_TRACE_FUNC_LEN);
104 rec[tail].linenum = linenum;
105 strncpy(rec[tail].desc, desc, TWS_TRACE_DESC_LEN);
106 rec[tail].val1 = val1;
107 rec[tail].val2 = val2;
109 tail = (tail+1) % sc->trace_q.depth;
111 if ( head == tail ) {
112 sc->trace_q.overflow = 1;
113 sc->trace_q.head = (head+1) % sc->trace_q.depth;
115 sc->trace_q.tail = tail;
118 tws_circular_q_insert(sc, &sc->trace_q,
119 &rec, sizeof(struct tws_trace_rec));
122 strcpy(fmt, "%05d:%s::%s :%s: 0x%016lx : 0x%016lx \n");
124 strcpy(fmt, "%05d:%s::%s :%s: 0x%016llx : 0x%016llx \n");
127 printf("%05d:%s::%s :%s: 0x%016llx : 0x%016llx \n",
128 linenum, file, fun, desc, val1, val2);
130 printf(fmt, linenum, file, fun, desc, val1, val2);
134 tws_log(struct tws_softc *sc, int index)
136 device_printf((sc)->tws_dev, array[index].fmt,
137 array[index].error_str,
138 array[index].error_code,
139 array[index].severity_level,
143 /* ----------- swap functions ----------- */
147 tws_swap16(u_int16_t val)
149 return((val << 8) | (val >> 8));
153 tws_swap32(u_int32_t val)
155 return(((val << 24) | ((val << 8) & (0xFF0000)) |
156 ((val >> 8) & (0xFF00)) | (val >> 24)));
161 tws_swap64(u_int64_t val)
163 return((((u_int64_t)(tws_swap32(((u_int32_t *)(&(val)))[1]))) << 32) |
164 ((u_int32_t)(tws_swap32(((u_int32_t *)(&(val)))[0]))));
168 /* ----------- reg access ----------- */
172 tws_write_reg(struct tws_softc *sc, int offset,
173 u_int32_t value, int size)
175 bus_space_tag_t bus_tag = sc->bus_tag;
176 bus_space_handle_t bus_handle = sc->bus_handle;
179 bus_space_write_4(bus_tag, bus_handle, offset, value);
182 bus_space_write_2(bus_tag, bus_handle, offset,
185 bus_space_write_1(bus_tag, bus_handle, offset, (u_int8_t)value);
189 tws_read_reg(struct tws_softc *sc, int offset, int size)
191 bus_space_tag_t bus_tag = sc->bus_tag;
192 bus_space_handle_t bus_handle = sc->bus_handle;
195 return((u_int32_t)bus_space_read_4(bus_tag, bus_handle, offset));
197 return((u_int32_t)bus_space_read_2(bus_tag, bus_handle, offset));
199 return((u_int32_t)bus_space_read_1(bus_tag, bus_handle, offset));
202 /* --------------------- Q service --------------------- */
205 * intialize q pointers with null.
208 tws_init_qs(struct tws_softc *sc)
211 mtx_lock(&sc->q_lock);
212 for(int i=0;i<TWS_MAX_QS;i++) {
213 sc->q_head[i] = NULL;
214 sc->q_tail[i] = NULL;
216 mtx_unlock(&sc->q_lock);
220 /* called with lock held */
222 tws_insert2_empty_q(struct tws_softc *sc, struct tws_request *req,
226 mtx_assert(&sc->q_lock, MA_OWNED);
227 req->next = req->prev = NULL;
228 sc->q_head[q_type] = sc->q_tail[q_type] = req;
232 /* called with lock held */
234 tws_q_insert_head(struct tws_softc *sc, struct tws_request *req,
238 mtx_assert(&sc->q_lock, MA_OWNED);
239 if ( sc->q_head[q_type] == NULL ) {
240 tws_insert2_empty_q(sc, req, q_type);
242 req->next = sc->q_head[q_type];
244 sc->q_head[q_type]->prev = req;
245 sc->q_head[q_type] = req;
250 /* called with lock held */
252 tws_q_insert_tail(struct tws_softc *sc, struct tws_request *req,
256 mtx_assert(&sc->q_lock, MA_OWNED);
257 if ( sc->q_tail[q_type] == NULL ) {
258 tws_insert2_empty_q(sc, req, q_type);
260 req->prev = sc->q_tail[q_type];
262 sc->q_tail[q_type]->next = req;
263 sc->q_tail[q_type] = req;
268 /* called with lock held */
270 tws_q_remove_head(struct tws_softc *sc, u_int8_t q_type )
273 struct tws_request *r;
275 mtx_assert(&sc->q_lock, MA_OWNED);
276 r = sc->q_head[q_type];
279 if ( r->next == NULL && r->prev == NULL ) {
281 sc->q_head[q_type] = sc->q_tail[q_type] = NULL;
283 sc->q_head[q_type] = r->next;
284 r->next->prev = NULL;
291 /* called with lock held */
293 tws_q_remove_tail(struct tws_softc *sc, u_int8_t q_type )
296 struct tws_request *r;
298 mtx_assert(&sc->q_lock, MA_OWNED);
299 r = sc->q_tail[q_type];
302 if ( r->next == NULL && r->prev == NULL ) {
304 sc->q_head[q_type] = sc->q_tail[q_type] = NULL;
306 sc->q_tail[q_type] = r->prev;
307 r->prev->next = NULL;
314 /* returns removed request if successful. return NULL otherwise */
315 /* called with lock held */
317 tws_q_remove_request(struct tws_softc *sc, struct tws_request *req,
321 struct tws_request *r;
323 mtx_assert(&sc->q_lock, MA_OWNED);
325 TWS_TRACE_DEBUG(sc, "null req", 0, q_type);
329 if ( req == sc->q_head[q_type] )
330 return(tws_q_remove_head(sc, q_type));
331 if ( req == sc->q_tail[q_type] )
332 return(tws_q_remove_tail(sc, q_type));
335 /* The given node is not at head or tail.
336 * It's in the middle and there are more than
337 * 2 elements on the q.
340 if ( req->next == NULL || req->prev == NULL ) {
341 TWS_TRACE_DEBUG(sc, "invalid req", 0, q_type);
346 r = sc->q_head[q_type];
354 TWS_TRACE_DEBUG(sc, "req not in q", 0, req->request_id);
359 req->prev->next = r->next;
360 req->next->prev = r->prev;
367 tws_find_sense_from_mfa(struct tws_softc *sc, u_int64_t mfa)
371 TWS_TRACE_DEBUG(sc, "entry",sc,mfa);
373 i = (mfa - sc->dma_mem_phys) / sizeof(struct tws_command_packet);
374 if ( i>= 0 && i<tws_queue_depth) {
375 s = &sc->sense_bufs[i];
376 if ( mfa == s->hdr_pkt_phy )
380 TWS_TRACE_DEBUG(sc, "return null",0,mfa);
385 /* --------------------- Q service end --------------------- */
386 /* --------------------- misc service start --------------------- */
390 tws_print_stats(void *arg)
393 struct tws_softc *sc = (struct tws_softc *)arg;
395 TWS_TRACE(sc, "reqs(in, out)", sc->stats.reqs_in, sc->stats.reqs_out);
396 TWS_TRACE(sc, "reqs(err, intrs)", sc->stats.reqs_errored
397 , sc->stats.num_intrs);
398 TWS_TRACE(sc, "reqs(ioctls, scsi)", sc->stats.ioctls
399 , sc->stats.scsi_ios);
400 callout_reset(&sc->stats_timer, 300 * hz, tws_print_stats, sc);
402 /* --------------------- misc service end --------------------- */