2 * Copyright (c) 2001,2002 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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.4 2004/06/21 15:39:31 dillon Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
38 #include <sys/malloc.h>
41 #include <machine/clock.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
46 #include <bus/pci/pcivar.h>
47 #include <bus/pci/pcireg.h>
52 MALLOC_DEFINE(M_PSTIOP, "PSTIOP", "Promise SuperTrak IOP driver");
55 iop_init(struct iop_softc *sc)
57 int mfa, timeout = 10000;
59 while ((mfa = sc->reg->iqueue) == 0xffffffff && --timeout)
62 printf("pstiop: no free mfa\n");
65 iop_free_mfa(sc, mfa);
67 sc->reg->oqueue_intr_mask = 0xffffffff;
70 printf("pstiop: no reset response\n");
74 if (!iop_init_outqueue(sc)) {
75 printf("pstiop: init outbound queue failed\n");
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 if (config_intrhook_establish(sc->iop_delayed_attach)) {
85 printf("pstiop: config_intrhook_establish failed\n");
86 free(sc->iop_delayed_attach, M_PSTIOP);
92 iop_attach(struct iop_softc *sc)
96 if (sc->iop_delayed_attach) {
97 config_intrhook_disestablish(sc->iop_delayed_attach);
98 free(sc->iop_delayed_attach, M_PSTIOP);
99 sc->iop_delayed_attach = NULL;
102 if (!iop_get_lct(sc)) {
103 printf("pstiop: get LCT failed\n");
107 /* figure out what devices are here and config as needed */
108 for (i = 0; sc->lct[i].entry_size == I2O_LCT_ENTRYSIZE; i++) {
110 struct i2o_get_param_reply *reply;
112 printf("pstiop: LCT entry %d ", i);
113 printf("class=%04x ", sc->lct[i].class);
114 printf("sub=%04x ", sc->lct[i].sub_class);
115 printf("localtid=%04x ", sc->lct[i].local_tid);
116 printf("usertid=%04x ", sc->lct[i].user_tid);
117 printf("parentid=%04x\n", sc->lct[i].parent_tid);
119 if ((reply = iop_get_util_params(sc, sc->lct[i].local_tid,
120 I2O_PARAMS_OPERATION_FIELD_GET,
121 I2O_UTIL_DEVICE_IDENTITY_GROUP_NO))) {
122 struct i2o_device_identity *ident =
123 (struct i2o_device_identity *)reply->result;
124 printf("pstiop: vendor=<%.16s> product=<%.16s>\n",
125 ident->vendor, ident->product);
126 printf("pstiop: description=<%.16s> revision=<%.8s>\n",
127 ident->description, ident->revision);
128 contigfree(reply, PAGE_SIZE, M_PSTIOP);
132 if (sc->lct[i].user_tid != I2O_TID_NONE &&
133 sc->lct[i].user_tid != I2O_TID_HOST)
136 switch (sc->lct[i].class) {
137 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
138 pst_add_raid(sc, &sc->lct[i]);
142 /* setup and enable interrupts */
143 bus_setup_intr(sc->dev, sc->r_irq, INTR_TYPE_BIO, iop_intr, sc,&sc->handle);
144 sc->reg->oqueue_intr_mask = 0x0;
150 struct iop_softc *sc = (struct iop_softc *)data;
151 struct i2o_single_reply *reply;
154 /* we might get more than one finished request pr interrupt */
156 if ((mfa = sc->reg->oqueue) == 0xffffffff)
157 if ((mfa = sc->reg->oqueue) == 0xffffffff)
160 reply = (struct i2o_single_reply *)(sc->obase + (mfa - sc->phys_obase));
162 /* if this is a event register reply, shout! */
163 if (reply->function == I2O_UTIL_EVENT_REGISTER) {
164 struct i2o_util_event_reply_message *event =
165 (struct i2o_util_event_reply_message *)reply;
167 printf("pstiop: EVENT!! idx=%08x data=%08x\n",
168 event->event_mask, event->event_data[0]);
172 /* if reply is a failurenotice we need to free the original mfa */
173 if (reply->message_flags & I2O_MESSAGE_FLAGS_FAIL)
174 iop_free_mfa(sc,((struct i2o_fault_reply *)(reply))->preserved_mfa);
176 /* reply->initiator_context points to the service routine */
177 ((void (*)(struct iop_softc *, u_int32_t, struct i2o_single_reply *))
178 (reply->initiator_context))(sc, mfa, reply);
183 iop_reset(struct iop_softc *sc)
185 struct i2o_exec_iop_reset_message *msg;
186 int mfa, timeout = 5000;
187 volatile u_int32_t reply = 0;
189 mfa = iop_get_mfa(sc);
190 msg = (struct i2o_exec_iop_reset_message *)(sc->ibase + mfa);
191 bzero(msg, sizeof(struct i2o_exec_iop_reset_message));
192 msg->version_offset = 0x1;
193 msg->message_flags = 0x0;
194 msg->message_size = sizeof(struct i2o_exec_iop_reset_message) >> 2;
195 msg->target_address = I2O_TID_IOP;
196 msg->initiator_address = I2O_TID_HOST;
197 msg->function = I2O_EXEC_IOP_RESET;
198 msg->status_word_low_addr = vtophys(&reply);
199 msg->status_word_high_addr = 0;
201 sc->reg->iqueue = mfa;
203 while (--timeout && !reply)
206 /* wait for iqueue ready */
208 while ((mfa = sc->reg->iqueue) == 0xffffffff && --timeout)
211 iop_free_mfa(sc, mfa);
216 iop_init_outqueue(struct iop_softc *sc)
218 struct i2o_exec_init_outqueue_message *msg;
219 int i, mfa, timeout = 5000;
220 volatile u_int32_t reply = 0;
222 if (!(sc->obase = contigmalloc(I2O_IOP_OUTBOUND_FRAME_COUNT *
223 I2O_IOP_OUTBOUND_FRAME_SIZE,
225 0x00010000, 0xFFFFFFFF,
226 sizeof(u_int32_t), 0))) {
227 printf("pstiop: contigmalloc of outqueue buffers failed!\n");
230 sc->phys_obase = vtophys(sc->obase);
231 mfa = iop_get_mfa(sc);
232 msg = (struct i2o_exec_init_outqueue_message *)(sc->ibase + mfa);
233 bzero(msg, sizeof(struct i2o_exec_init_outqueue_message));
234 msg->version_offset = 0x61;
235 msg->message_flags = 0x0;
236 msg->message_size = sizeof(struct i2o_exec_init_outqueue_message) >> 2;
237 msg->target_address = I2O_TID_IOP;
238 msg->initiator_address = I2O_TID_HOST;
239 msg->function = I2O_EXEC_OUTBOUND_INIT;
240 msg->host_pagesize = PAGE_SIZE;
241 msg->init_code = 0x00; /* SOS XXX should be 0x80 == OS */
242 msg->queue_framesize = I2O_IOP_OUTBOUND_FRAME_SIZE / sizeof(u_int32_t);
243 msg->sgl[0].flags = I2O_SGL_SIMPLE | I2O_SGL_END | I2O_SGL_EOB;
244 msg->sgl[0].count = sizeof(reply);
245 msg->sgl[0].phys_addr[0] = vtophys(&reply);
246 msg->sgl[1].flags = I2O_SGL_END | I2O_SGL_EOB;
247 msg->sgl[1].count = 1;
248 msg->sgl[1].phys_addr[0] = 0;
250 sc->reg->iqueue = mfa;
252 /* wait for init to complete */
253 while (--timeout && reply != I2O_EXEC_OUTBOUND_INIT_COMPLETE)
257 printf("pstiop: timeout waiting for init-complete response\n");
258 iop_free_mfa(sc, mfa);
262 /* now init our oqueue bufs */
263 for (i = 0; i < I2O_IOP_OUTBOUND_FRAME_COUNT; i++) {
264 sc->reg->oqueue = sc->phys_obase + (i * I2O_IOP_OUTBOUND_FRAME_SIZE);
268 iop_free_mfa(sc, mfa);
273 iop_get_lct(struct iop_softc *sc)
275 struct i2o_exec_get_lct_message *msg;
276 struct i2o_get_lct_reply *reply;
278 #define ALLOCSIZE (PAGE_SIZE + (256 * sizeof(struct i2o_lct_entry)))
280 if (!(reply = contigmalloc(ALLOCSIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
281 0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
284 mfa = iop_get_mfa(sc);
285 msg = (struct i2o_exec_get_lct_message *)(sc->ibase + mfa);
286 bzero(msg, sizeof(struct i2o_exec_get_lct_message));
287 msg->version_offset = 0x61;
288 msg->message_flags = 0x0;
289 msg->message_size = sizeof(struct i2o_exec_get_lct_message) >> 2;
290 msg->target_address = I2O_TID_IOP;
291 msg->initiator_address = I2O_TID_HOST;
292 msg->function = I2O_EXEC_LCT_NOTIFY;
293 msg->class = I2O_CLASS_MATCH_ANYCLASS;
294 msg->last_change_id = 0;
296 msg->sgl.flags = I2O_SGL_SIMPLE | I2O_SGL_END | I2O_SGL_EOB;
297 msg->sgl.count = ALLOCSIZE;
298 msg->sgl.phys_addr[0] = vtophys(reply);
300 if (iop_queue_wait_msg(sc, mfa, (struct i2o_basic_message *)msg)) {
301 contigfree(reply, ALLOCSIZE, M_PSTIOP);
304 sc->lct = malloc(reply->table_size * sizeof(struct i2o_lct_entry),
305 M_PSTIOP, M_INTWAIT | M_ZERO);
306 bcopy(&reply->entry[0], sc->lct,
307 reply->table_size * sizeof(struct i2o_lct_entry));
308 sc->lct_count = reply->table_size;
309 contigfree(reply, ALLOCSIZE, M_PSTIOP);
313 struct i2o_get_param_reply *
314 iop_get_util_params(struct iop_softc *sc, int target, int operation, int group)
316 struct i2o_util_get_param_message *msg;
317 struct i2o_get_param_operation *param;
318 struct i2o_get_param_reply *reply;
321 if (!(param = contigmalloc(PAGE_SIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
322 0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
325 if (!(reply = contigmalloc(PAGE_SIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
326 0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
329 mfa = iop_get_mfa(sc);
330 msg = (struct i2o_util_get_param_message *)(sc->ibase + mfa);
331 bzero(msg, sizeof(struct i2o_util_get_param_message));
332 msg->version_offset = 0x51;
333 msg->message_flags = 0x0;
334 msg->message_size = sizeof(struct i2o_util_get_param_message) >> 2;
335 msg->target_address = target;
336 msg->initiator_address = I2O_TID_HOST;
337 msg->function = I2O_UTIL_PARAMS_GET;
338 msg->operation_flags = 0;
340 param->operation_count = 1;
341 param->operation[0].operation = operation;
342 param->operation[0].group = group;
343 param->operation[0].field_count = 0xffff;
345 msg->sgl[0].flags = I2O_SGL_SIMPLE | I2O_SGL_DIR | I2O_SGL_EOB;
346 msg->sgl[0].count = sizeof(struct i2o_get_param_operation);
347 msg->sgl[0].phys_addr[0] = vtophys(param);
349 msg->sgl[1].flags = I2O_SGL_SIMPLE | I2O_SGL_END | I2O_SGL_EOB;
350 msg->sgl[1].count = PAGE_SIZE;
351 msg->sgl[1].phys_addr[0] = vtophys(reply);
353 if (iop_queue_wait_msg(sc, mfa, (struct i2o_basic_message *)msg) ||
354 reply->error_info_size) {
355 contigfree(reply, PAGE_SIZE, M_PSTIOP);
358 contigfree(param, PAGE_SIZE, M_PSTIOP);
363 iop_get_mfa(struct iop_softc *sc)
368 while ((mfa = sc->reg->iqueue) == 0xffffffff && timeout) {
373 printf("pstiop: no free mfa\n");
378 iop_free_mfa(struct iop_softc *sc, int mfa)
380 struct i2o_basic_message *msg = (struct i2o_basic_message *)(sc->ibase+mfa);
382 bzero(msg, sizeof(struct i2o_basic_message));
384 msg->message_flags = 0x0;
385 msg->message_size = sizeof(struct i2o_basic_message) >> 2;
386 msg->target_address = I2O_TID_IOP;
387 msg->initiator_address = I2O_TID_HOST;
388 msg->function = I2O_UTIL_NOP;
389 sc->reg->iqueue = mfa;
393 iop_queue_wait_msg(struct iop_softc *sc, int mfa, struct i2o_basic_message *msg)
395 struct i2o_single_reply *reply;
396 int out_mfa, status, timeout = 10000;
398 sc->reg->iqueue = mfa;
400 while (--timeout && ((out_mfa = sc->reg->oqueue) == 0xffffffff))
403 printf("pstiop: timeout waiting for message response\n");
404 iop_free_mfa(sc, mfa);
408 reply = (struct i2o_single_reply *)(sc->obase + (out_mfa - sc->phys_obase));
409 status = reply->status;
410 sc->reg->oqueue = out_mfa;
415 iop_create_sgl(struct i2o_basic_message *msg, caddr_t data, int count, int dir)
417 struct i2o_sgl *sgl = (struct i2o_sgl *)((int32_t *)msg + msg->offset);
418 u_int32_t sgl_count, sgl_phys;
421 if (((uintptr_t)data & 3) || (count & 3)) {
422 printf("pstiop: non aligned DMA transfer attempted\n");
426 printf("pstiop: zero length DMA transfer attempted\n");
430 sgl_count = min(count, (PAGE_SIZE - ((uintptr_t)data & PAGE_MASK)));
431 sgl_phys = vtophys(data);
432 sgl->flags = dir | I2O_SGL_PAGELIST | I2O_SGL_EOB | I2O_SGL_END;
438 sgl->phys_addr[i] = sgl_phys;
439 sgl_phys = vtophys(data);
440 data += min(count, PAGE_SIZE);
441 count -= min(count, PAGE_SIZE);
442 if (++i >= I2O_SGL_MAX_SEGS) {
443 printf("pstiop: too many segments in SGL\n");
447 sgl->phys_addr[i] = sgl_phys;
448 msg->message_size += i;