2 * Copyright (c) 2010, LSI Corp.
4 * Author : Manjunath Ranganathaiah
5 * Support: freebsdraid@lsi.com
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of the <ORGANIZATION> nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
37 #include <dev/tws/tws.h>
38 #include <dev/tws/tws_hdm.h>
39 #include <dev/tws/tws_services.h>
42 void tws_q_insert_tail(struct tws_softc *sc, struct tws_request *req,
44 struct tws_request * tws_q_remove_request(struct tws_softc *sc,
45 struct tws_request *req, u_int8_t q_type );
46 struct tws_request *tws_q_remove_head(struct tws_softc *sc, u_int8_t q_type );
47 void tws_q_insert_head(struct tws_softc *sc, struct tws_request *req,
49 struct tws_request * tws_q_remove_tail(struct tws_softc *sc, u_int8_t q_type );
50 void tws_print_stats(void *arg);
52 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)
91 struct tws_trace_rec *rec = (struct tws_trace_rec *)sc->trace_q.q;
92 volatile u_int16_t head, tail;
95 head = sc->trace_q.head;
96 tail = sc->trace_q.tail;
98 getnanotime(&rec[tail].ts);
100 strncpy(rec[tail].fname, file, TWS_TRACE_FNAME_LEN);
101 strncpy(rec[tail].func, fun, TWS_TRACE_FUNC_LEN);
102 rec[tail].linenum = linenum;
103 strncpy(rec[tail].desc, desc, TWS_TRACE_DESC_LEN);
104 rec[tail].val1 = val1;
105 rec[tail].val2 = val2;
107 tail = (tail+1) % sc->trace_q.depth;
109 if ( head == tail ) {
110 sc->trace_q.overflow = 1;
111 sc->trace_q.head = (head+1) % sc->trace_q.depth;
113 sc->trace_q.tail = tail;
116 tws_circular_q_insert(sc, &sc->trace_q,
117 &rec, sizeof(struct tws_trace_rec));
120 strcpy(fmt, "%05d:%s::%s :%s: 0x%016lx : 0x%016lx \n");
122 strcpy(fmt, "%05d:%s::%s :%s: 0x%016llx : 0x%016llx \n");
125 printf("%05d:%s::%s :%s: 0x%016llx : 0x%016llx \n",
126 linenum, file, fun, desc, val1, val2);
128 printf(fmt, linenum, file, fun, desc, val1, val2);
132 tws_log(struct tws_softc *sc, int index)
134 device_printf((sc)->tws_dev, array[index].fmt,
135 array[index].error_str,
136 array[index].error_code,
137 array[index].severity_level,
141 /* ----------- swap functions ----------- */
145 tws_swap16(u_int16_t val)
147 return((val << 8) | (val >> 8));
151 tws_swap32(u_int32_t val)
153 return(((val << 24) | ((val << 8) & (0xFF0000)) |
154 ((val >> 8) & (0xFF00)) | (val >> 24)));
159 tws_swap64(u_int64_t val)
161 return((((u_int64_t)(tws_swap32(((u_int32_t *)(&(val)))[1]))) << 32) |
162 ((u_int32_t)(tws_swap32(((u_int32_t *)(&(val)))[0]))));
166 /* ----------- reg access ----------- */
170 tws_write_reg(struct tws_softc *sc, int offset,
171 u_int32_t value, int size)
173 bus_space_tag_t bus_tag = sc->bus_tag;
174 bus_space_handle_t bus_handle = sc->bus_handle;
177 bus_space_write_4(bus_tag, bus_handle, offset, value);
180 bus_space_write_2(bus_tag, bus_handle, offset,
183 bus_space_write_1(bus_tag, bus_handle, offset, (u_int8_t)value);
187 tws_read_reg(struct tws_softc *sc, int offset, int size)
189 bus_space_tag_t bus_tag = sc->bus_tag;
190 bus_space_handle_t bus_handle = sc->bus_handle;
193 return((u_int32_t)bus_space_read_4(bus_tag, bus_handle, offset));
195 return((u_int32_t)bus_space_read_2(bus_tag, bus_handle, offset));
197 return((u_int32_t)bus_space_read_1(bus_tag, bus_handle, offset));
200 /* --------------------- Q service --------------------- */
203 * intialize q pointers with null.
206 tws_init_qs(struct tws_softc *sc)
209 mtx_lock(&sc->q_lock);
210 for(int i=0;i<TWS_MAX_QS;i++) {
211 sc->q_head[i] = NULL;
212 sc->q_tail[i] = NULL;
214 mtx_unlock(&sc->q_lock);
218 /* called with lock held */
220 tws_insert2_empty_q(struct tws_softc *sc, struct tws_request *req,
224 mtx_assert(&sc->q_lock, MA_OWNED);
225 req->next = req->prev = NULL;
226 sc->q_head[q_type] = sc->q_tail[q_type] = req;
230 /* called with lock held */
232 tws_q_insert_head(struct tws_softc *sc, struct tws_request *req,
236 mtx_assert(&sc->q_lock, MA_OWNED);
237 if ( sc->q_head[q_type] == NULL ) {
238 tws_insert2_empty_q(sc, req, q_type);
240 req->next = sc->q_head[q_type];
242 sc->q_head[q_type]->prev = req;
243 sc->q_head[q_type] = req;
248 /* called with lock held */
250 tws_q_insert_tail(struct tws_softc *sc, struct tws_request *req,
254 mtx_assert(&sc->q_lock, MA_OWNED);
255 if ( sc->q_tail[q_type] == NULL ) {
256 tws_insert2_empty_q(sc, req, q_type);
258 req->prev = sc->q_tail[q_type];
260 sc->q_tail[q_type]->next = req;
261 sc->q_tail[q_type] = req;
266 /* called with lock held */
268 tws_q_remove_head(struct tws_softc *sc, u_int8_t q_type )
271 struct tws_request *r;
273 mtx_assert(&sc->q_lock, MA_OWNED);
274 r = sc->q_head[q_type];
277 if ( r->next == NULL && r->prev == NULL ) {
279 sc->q_head[q_type] = sc->q_tail[q_type] = NULL;
281 sc->q_head[q_type] = r->next;
282 r->next->prev = NULL;
289 /* called with lock held */
291 tws_q_remove_tail(struct tws_softc *sc, u_int8_t q_type )
294 struct tws_request *r;
296 mtx_assert(&sc->q_lock, MA_OWNED);
297 r = sc->q_tail[q_type];
300 if ( r->next == NULL && r->prev == NULL ) {
302 sc->q_head[q_type] = sc->q_tail[q_type] = NULL;
304 sc->q_tail[q_type] = r->prev;
305 r->prev->next = NULL;
312 /* returns removed request if successful. return NULL otherwise */
313 /* called with lock held */
315 tws_q_remove_request(struct tws_softc *sc, struct tws_request *req,
319 struct tws_request *r;
321 mtx_assert(&sc->q_lock, MA_OWNED);
323 TWS_TRACE_DEBUG(sc, "null req", 0, q_type);
327 if ( req == sc->q_head[q_type] )
328 return(tws_q_remove_head(sc, q_type));
329 if ( req == sc->q_tail[q_type] )
330 return(tws_q_remove_tail(sc, q_type));
333 /* The given node is not at head or tail.
334 * It's in the middle and there are more than
335 * 2 elements on the q.
338 if ( req->next == NULL || req->prev == NULL ) {
339 TWS_TRACE_DEBUG(sc, "invalid req", 0, q_type);
344 r = sc->q_head[q_type];
352 TWS_TRACE_DEBUG(sc, "req not in q", 0, req->request_id);
357 req->prev->next = r->next;
358 req->next->prev = r->prev;
365 tws_find_sense_from_mfa(struct tws_softc *sc, u_int64_t mfa)
369 TWS_TRACE_DEBUG(sc, "entry",sc,mfa);
371 i = (mfa - sc->dma_mem_phys) / sizeof(struct tws_command_packet);
372 if ( i>= 0 && i<tws_queue_depth) {
373 s = &sc->sense_bufs[i];
374 if ( mfa == s->hdr_pkt_phy )
378 TWS_TRACE_DEBUG(sc, "return null",0,mfa);
383 /* --------------------- Q service end --------------------- */
384 /* --------------------- misc service start --------------------- */
388 tws_print_stats(void *arg)
391 struct tws_softc *sc = (struct tws_softc *)arg;
393 TWS_TRACE(sc, "reqs(in, out)", sc->stats.reqs_in, sc->stats.reqs_out);
394 TWS_TRACE(sc, "reqs(err, intrs)", sc->stats.reqs_errored
395 , sc->stats.num_intrs);
396 TWS_TRACE(sc, "reqs(ioctls, scsi)", sc->stats.ioctls
397 , sc->stats.scsi_ios);
398 timeout(tws_print_stats, sc, 300*hz);
401 /* --------------------- misc service end --------------------- */