Merge from vendor branch GPERF:
[dragonfly.git] / sys / dev / raid / pst / pst-iop.c
1 /*-
2  * Copyright (c) 2001,2002 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/pst/pst-iop.c,v 1.2.2.1 2002/08/18 12:32:36 sos Exp $
29  * $DragonFly: src/sys/dev/raid/pst/pst-iop.c,v 1.5 2005/02/04 02:55:48 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/buf.h>
38 #include <sys/malloc.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <machine/clock.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
45 #include <sys/rman.h>
46 #include <bus/pci/pcivar.h>
47 #include <bus/pci/pcireg.h>
48
49 #include "pst-iop.h"
50
51 /* local vars */
52 MALLOC_DEFINE(M_PSTIOP, "PSTIOP", "Promise SuperTrak IOP driver");
53
54 int
55 iop_init(struct iop_softc *sc)
56 {
57     int mfa, timeout = 10000;
58
59     while ((mfa = sc->reg->iqueue) == 0xffffffff && --timeout)
60         DELAY(1000);
61     if (!timeout) {
62         printf("pstiop: no free mfa\n");
63         return 0;
64     }
65     iop_free_mfa(sc, mfa);
66
67     sc->reg->oqueue_intr_mask = 0xffffffff;
68
69     if (!iop_reset(sc)) {
70         printf("pstiop: no reset response\n");
71         return 0;
72     }
73
74     if (!iop_init_outqueue(sc)) {
75         printf("pstiop: init outbound queue failed\n");
76         return 0;
77     }
78
79     /* register iop_attach to be run when interrupts are enabled */
80     sc->iop_delayed_attach = malloc(sizeof(struct intr_config_hook), 
81                                    M_PSTIOP, M_INTWAIT | M_ZERO);
82     sc->iop_delayed_attach->ich_func = (void *)iop_attach;
83     sc->iop_delayed_attach->ich_arg = (void *)sc;
84     sc->iop_delayed_attach->ich_desc = "pst";
85     if (config_intrhook_establish(sc->iop_delayed_attach)) {
86         printf("pstiop: config_intrhook_establish failed\n");
87         free(sc->iop_delayed_attach, M_PSTIOP);
88     }
89     return 1;
90 }
91
92 void
93 iop_attach(struct iop_softc *sc)
94 {
95     int i;
96
97     if (sc->iop_delayed_attach) {
98         config_intrhook_disestablish(sc->iop_delayed_attach);
99         free(sc->iop_delayed_attach, M_PSTIOP);
100         sc->iop_delayed_attach = NULL;
101     }
102
103     if (!iop_get_lct(sc)) {
104         printf("pstiop: get LCT failed\n");
105         return;
106     }
107
108     /* figure out what devices are here and config as needed */
109     for (i = 0; sc->lct[i].entry_size == I2O_LCT_ENTRYSIZE; i++) {
110 #ifdef PSTDEBUG
111         struct i2o_get_param_reply *reply;
112
113         printf("pstiop: LCT entry %d ", i);
114         printf("class=%04x ", sc->lct[i].class);
115         printf("sub=%04x ", sc->lct[i].sub_class);
116         printf("localtid=%04x ", sc->lct[i].local_tid);
117         printf("usertid=%04x ", sc->lct[i].user_tid);
118         printf("parentid=%04x\n", sc->lct[i].parent_tid);
119
120         if ((reply = iop_get_util_params(sc, sc->lct[i].local_tid,
121                                          I2O_PARAMS_OPERATION_FIELD_GET,
122                                          I2O_UTIL_DEVICE_IDENTITY_GROUP_NO))) {
123             struct i2o_device_identity *ident =
124                 (struct i2o_device_identity *)reply->result;
125             printf("pstiop: vendor=<%.16s> product=<%.16s>\n",
126                    ident->vendor, ident->product);
127             printf("pstiop: description=<%.16s> revision=<%.8s>\n",
128                    ident->description, ident->revision);
129             contigfree(reply, PAGE_SIZE, M_PSTIOP);
130         }
131 #endif
132
133         if (sc->lct[i].user_tid != I2O_TID_NONE &&
134             sc->lct[i].user_tid != I2O_TID_HOST)
135             continue;
136
137         switch (sc->lct[i].class) {
138         case I2O_CLASS_RANDOM_BLOCK_STORAGE:
139             pst_add_raid(sc, &sc->lct[i]);
140             break;
141         }
142     }
143     /* setup and enable interrupts */
144     bus_setup_intr(sc->dev, sc->r_irq, INTR_TYPE_BIO, iop_intr, sc,&sc->handle);
145     sc->reg->oqueue_intr_mask = 0x0;
146 }
147
148 void
149 iop_intr(void *data)
150 {
151     struct iop_softc *sc = (struct iop_softc *)data;
152     struct i2o_single_reply *reply;
153     u_int32_t mfa;
154
155     /* we might get more than one finished request pr interrupt */
156     while (1) {
157         if ((mfa = sc->reg->oqueue) == 0xffffffff)
158             if ((mfa = sc->reg->oqueue) == 0xffffffff)
159                 return;
160
161         reply = (struct i2o_single_reply *)(sc->obase + (mfa - sc->phys_obase));
162
163         /* if this is a event register reply, shout! */
164         if (reply->function == I2O_UTIL_EVENT_REGISTER) {
165             struct i2o_util_event_reply_message *event = 
166                 (struct i2o_util_event_reply_message *)reply;
167
168             printf("pstiop: EVENT!! idx=%08x data=%08x\n",
169                    event->event_mask, event->event_data[0]);
170             return;
171         }
172
173         /* if reply is a failurenotice we need to free the original mfa */
174         if (reply->message_flags & I2O_MESSAGE_FLAGS_FAIL)
175             iop_free_mfa(sc,((struct i2o_fault_reply *)(reply))->preserved_mfa);
176         
177         /* reply->initiator_context points to the service routine */
178         ((void (*)(struct iop_softc *, u_int32_t, struct i2o_single_reply *))
179             (reply->initiator_context))(sc, mfa, reply);
180     }
181 }
182
183 int
184 iop_reset(struct iop_softc *sc)
185 {
186     struct i2o_exec_iop_reset_message *msg;
187     int mfa, timeout = 5000;
188     volatile u_int32_t reply = 0;
189
190     mfa = iop_get_mfa(sc);
191     msg = (struct i2o_exec_iop_reset_message *)(sc->ibase + mfa);
192     bzero(msg, sizeof(struct i2o_exec_iop_reset_message));
193     msg->version_offset = 0x1;
194     msg->message_flags = 0x0;
195     msg->message_size = sizeof(struct i2o_exec_iop_reset_message) >> 2;
196     msg->target_address = I2O_TID_IOP;
197     msg->initiator_address = I2O_TID_HOST;
198     msg->function = I2O_EXEC_IOP_RESET;
199     msg->status_word_low_addr = vtophys(&reply);
200     msg->status_word_high_addr = 0;
201
202     sc->reg->iqueue = mfa;
203
204     while (--timeout && !reply)
205         DELAY(1000);
206
207     /* wait for iqueue ready */
208     timeout = 10000;
209     while ((mfa = sc->reg->iqueue) == 0xffffffff && --timeout)
210         DELAY(1000);
211
212     iop_free_mfa(sc, mfa);
213     return reply;
214 }
215
216 int
217 iop_init_outqueue(struct iop_softc *sc)
218 {
219     struct i2o_exec_init_outqueue_message *msg;
220     int i, mfa, timeout = 5000;
221     volatile u_int32_t reply = 0;
222
223     if (!(sc->obase = contigmalloc(I2O_IOP_OUTBOUND_FRAME_COUNT *
224                                    I2O_IOP_OUTBOUND_FRAME_SIZE,
225                                    M_PSTIOP, M_NOWAIT,
226                                    0x00010000, 0xFFFFFFFF,
227                                    sizeof(u_int32_t), 0))) {
228         printf("pstiop: contigmalloc of outqueue buffers failed!\n");
229         return 0;
230     }
231     sc->phys_obase = vtophys(sc->obase);
232     mfa = iop_get_mfa(sc);
233     msg = (struct i2o_exec_init_outqueue_message *)(sc->ibase + mfa);
234     bzero(msg, sizeof(struct i2o_exec_init_outqueue_message));
235     msg->version_offset = 0x61;
236     msg->message_flags = 0x0;
237     msg->message_size = sizeof(struct i2o_exec_init_outqueue_message) >> 2;
238     msg->target_address = I2O_TID_IOP;
239     msg->initiator_address = I2O_TID_HOST;
240     msg->function = I2O_EXEC_OUTBOUND_INIT;
241     msg->host_pagesize = PAGE_SIZE;
242     msg->init_code = 0x00; /* SOS XXX should be 0x80 == OS */
243     msg->queue_framesize = I2O_IOP_OUTBOUND_FRAME_SIZE / sizeof(u_int32_t);
244     msg->sgl[0].flags = I2O_SGL_SIMPLE | I2O_SGL_END | I2O_SGL_EOB;
245     msg->sgl[0].count = sizeof(reply);
246     msg->sgl[0].phys_addr[0] = vtophys(&reply);
247     msg->sgl[1].flags = I2O_SGL_END | I2O_SGL_EOB;
248     msg->sgl[1].count = 1;
249     msg->sgl[1].phys_addr[0] = 0;
250
251     sc->reg->iqueue = mfa;
252
253     /* wait for init to complete */
254     while (--timeout && reply != I2O_EXEC_OUTBOUND_INIT_COMPLETE)
255         DELAY(1000);
256     
257     if (!timeout) {
258         printf("pstiop: timeout waiting for init-complete response\n");
259         iop_free_mfa(sc, mfa);
260         return 0;
261     }
262
263     /* now init our oqueue bufs */
264     for (i = 0; i < I2O_IOP_OUTBOUND_FRAME_COUNT; i++) {
265         sc->reg->oqueue = sc->phys_obase + (i * I2O_IOP_OUTBOUND_FRAME_SIZE);
266         DELAY(1000);
267     }
268
269     iop_free_mfa(sc, mfa);
270     return 1;
271 }
272
273 int
274 iop_get_lct(struct iop_softc *sc)
275 {
276     struct i2o_exec_get_lct_message *msg;
277     struct i2o_get_lct_reply *reply;
278     int mfa;
279 #define ALLOCSIZE        (PAGE_SIZE + (256 * sizeof(struct i2o_lct_entry)))
280
281     if (!(reply = contigmalloc(ALLOCSIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
282                                0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
283         return 0;
284
285     mfa = iop_get_mfa(sc);
286     msg = (struct i2o_exec_get_lct_message *)(sc->ibase + mfa);
287     bzero(msg, sizeof(struct i2o_exec_get_lct_message));
288     msg->version_offset = 0x61;
289     msg->message_flags = 0x0;
290     msg->message_size = sizeof(struct i2o_exec_get_lct_message) >> 2;
291     msg->target_address = I2O_TID_IOP;
292     msg->initiator_address = I2O_TID_HOST;
293     msg->function = I2O_EXEC_LCT_NOTIFY;
294     msg->class = I2O_CLASS_MATCH_ANYCLASS;
295     msg->last_change_id = 0;
296
297     msg->sgl.flags = I2O_SGL_SIMPLE | I2O_SGL_END | I2O_SGL_EOB;
298     msg->sgl.count = ALLOCSIZE;
299     msg->sgl.phys_addr[0] = vtophys(reply);
300
301     if (iop_queue_wait_msg(sc, mfa, (struct i2o_basic_message *)msg)) {
302         contigfree(reply, ALLOCSIZE, M_PSTIOP);
303         return 0;
304     }
305     sc->lct = malloc(reply->table_size * sizeof(struct i2o_lct_entry),
306                            M_PSTIOP, M_INTWAIT | M_ZERO);
307     bcopy(&reply->entry[0], sc->lct, 
308           reply->table_size * sizeof(struct i2o_lct_entry));
309     sc->lct_count = reply->table_size;
310     contigfree(reply, ALLOCSIZE, M_PSTIOP);
311     return 1;
312 }
313
314 struct i2o_get_param_reply *
315 iop_get_util_params(struct iop_softc *sc, int target, int operation, int group)
316 {
317     struct i2o_util_get_param_message *msg;
318     struct i2o_get_param_operation *param;
319     struct i2o_get_param_reply *reply;
320     int mfa;
321
322     if (!(param = contigmalloc(PAGE_SIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
323                                0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
324         return NULL;
325
326     if (!(reply = contigmalloc(PAGE_SIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
327                                0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
328         return NULL;
329
330     mfa = iop_get_mfa(sc);
331     msg = (struct i2o_util_get_param_message *)(sc->ibase + mfa);
332     bzero(msg, sizeof(struct i2o_util_get_param_message));
333     msg->version_offset = 0x51;
334     msg->message_flags = 0x0;
335     msg->message_size = sizeof(struct i2o_util_get_param_message) >> 2;
336     msg->target_address = target;
337     msg->initiator_address = I2O_TID_HOST;
338     msg->function = I2O_UTIL_PARAMS_GET;
339     msg->operation_flags = 0;
340
341     param->operation_count = 1;
342     param->operation[0].operation = operation;
343     param->operation[0].group = group;
344     param->operation[0].field_count = 0xffff;
345
346     msg->sgl[0].flags = I2O_SGL_SIMPLE | I2O_SGL_DIR | I2O_SGL_EOB;
347     msg->sgl[0].count = sizeof(struct i2o_get_param_operation);
348     msg->sgl[0].phys_addr[0] = vtophys(param);
349
350     msg->sgl[1].flags = I2O_SGL_SIMPLE | I2O_SGL_END | I2O_SGL_EOB;
351     msg->sgl[1].count = PAGE_SIZE;
352     msg->sgl[1].phys_addr[0] = vtophys(reply);
353
354     if (iop_queue_wait_msg(sc, mfa, (struct i2o_basic_message *)msg) ||
355         reply->error_info_size) {
356         contigfree(reply, PAGE_SIZE, M_PSTIOP);
357         reply = NULL;
358     }
359     contigfree(param, PAGE_SIZE, M_PSTIOP);
360     return reply;
361 }
362
363 u_int32_t
364 iop_get_mfa(struct iop_softc *sc)
365 {
366     u_int32_t mfa;
367     int timeout = 10000;
368
369     while ((mfa = sc->reg->iqueue) == 0xffffffff && timeout) {
370         DELAY(1000);
371         timeout--;
372     }
373     if (!timeout)
374         printf("pstiop: no free mfa\n");
375     return mfa;
376 }
377
378 void
379 iop_free_mfa(struct iop_softc *sc, int mfa)
380 {
381     struct i2o_basic_message *msg = (struct i2o_basic_message *)(sc->ibase+mfa);
382
383     bzero(msg, sizeof(struct i2o_basic_message));
384     msg->version = 0x01;
385     msg->message_flags = 0x0;
386     msg->message_size = sizeof(struct i2o_basic_message) >> 2;
387     msg->target_address = I2O_TID_IOP;
388     msg->initiator_address = I2O_TID_HOST;
389     msg->function = I2O_UTIL_NOP;
390     sc->reg->iqueue = mfa;
391 }
392
393 int
394 iop_queue_wait_msg(struct iop_softc *sc, int mfa, struct i2o_basic_message *msg)
395 {
396     struct i2o_single_reply *reply;
397     int out_mfa, status, timeout = 10000;
398
399     sc->reg->iqueue = mfa;
400
401     while (--timeout && ((out_mfa = sc->reg->oqueue) == 0xffffffff))
402         DELAY(1000);
403     if (!timeout) {
404         printf("pstiop: timeout waiting for message response\n");
405         iop_free_mfa(sc, mfa);
406         return -1;
407     }
408
409     reply = (struct i2o_single_reply *)(sc->obase + (out_mfa - sc->phys_obase));
410     status = reply->status;
411     sc->reg->oqueue = out_mfa;
412     return status;
413 }
414
415 int
416 iop_create_sgl(struct i2o_basic_message *msg, caddr_t data, int count, int dir)
417 {
418     struct i2o_sgl *sgl = (struct i2o_sgl *)((int32_t *)msg + msg->offset);
419     u_int32_t sgl_count, sgl_phys;
420     int i = 0;
421
422     if (((uintptr_t)data & 3) || (count & 3)) {
423         printf("pstiop: non aligned DMA transfer attempted\n");
424         return 0;
425     }
426     if (!count) {
427         printf("pstiop: zero length DMA transfer attempted\n");
428         return 0;
429     }
430     
431     sgl_count = min(count, (PAGE_SIZE - ((uintptr_t)data & PAGE_MASK)));
432     sgl_phys = vtophys(data);
433     sgl->flags = dir | I2O_SGL_PAGELIST | I2O_SGL_EOB | I2O_SGL_END;
434     sgl->count = count;
435     data += sgl_count;
436     count -= sgl_count;
437
438     while (count) {
439         sgl->phys_addr[i] = sgl_phys;
440         sgl_phys = vtophys(data);
441         data += min(count, PAGE_SIZE);
442         count -= min(count, PAGE_SIZE);
443         if (++i >= I2O_SGL_MAX_SEGS) {
444             printf("pstiop: too many segments in SGL\n");
445             return 0;
446         }
447     }
448     sgl->phys_addr[i] = sgl_phys;
449     msg->message_size += i;
450     return 1;
451 }