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.5 2005/02/04 02:55:48 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 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);
93 iop_attach(struct iop_softc *sc)
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;
103 if (!iop_get_lct(sc)) {
104 printf("pstiop: get LCT failed\n");
108 /* figure out what devices are here and config as needed */
109 for (i = 0; sc->lct[i].entry_size == I2O_LCT_ENTRYSIZE; i++) {
111 struct i2o_get_param_reply *reply;
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);
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);
133 if (sc->lct[i].user_tid != I2O_TID_NONE &&
134 sc->lct[i].user_tid != I2O_TID_HOST)
137 switch (sc->lct[i].class) {
138 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
139 pst_add_raid(sc, &sc->lct[i]);
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;
151 struct iop_softc *sc = (struct iop_softc *)data;
152 struct i2o_single_reply *reply;
155 /* we might get more than one finished request pr interrupt */
157 if ((mfa = sc->reg->oqueue) == 0xffffffff)
158 if ((mfa = sc->reg->oqueue) == 0xffffffff)
161 reply = (struct i2o_single_reply *)(sc->obase + (mfa - sc->phys_obase));
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;
168 printf("pstiop: EVENT!! idx=%08x data=%08x\n",
169 event->event_mask, event->event_data[0]);
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);
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);
184 iop_reset(struct iop_softc *sc)
186 struct i2o_exec_iop_reset_message *msg;
187 int mfa, timeout = 5000;
188 volatile u_int32_t reply = 0;
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;
202 sc->reg->iqueue = mfa;
204 while (--timeout && !reply)
207 /* wait for iqueue ready */
209 while ((mfa = sc->reg->iqueue) == 0xffffffff && --timeout)
212 iop_free_mfa(sc, mfa);
217 iop_init_outqueue(struct iop_softc *sc)
219 struct i2o_exec_init_outqueue_message *msg;
220 int i, mfa, timeout = 5000;
221 volatile u_int32_t reply = 0;
223 if (!(sc->obase = contigmalloc(I2O_IOP_OUTBOUND_FRAME_COUNT *
224 I2O_IOP_OUTBOUND_FRAME_SIZE,
226 0x00010000, 0xFFFFFFFF,
227 sizeof(u_int32_t), 0))) {
228 printf("pstiop: contigmalloc of outqueue buffers failed!\n");
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;
251 sc->reg->iqueue = mfa;
253 /* wait for init to complete */
254 while (--timeout && reply != I2O_EXEC_OUTBOUND_INIT_COMPLETE)
258 printf("pstiop: timeout waiting for init-complete response\n");
259 iop_free_mfa(sc, mfa);
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);
269 iop_free_mfa(sc, mfa);
274 iop_get_lct(struct iop_softc *sc)
276 struct i2o_exec_get_lct_message *msg;
277 struct i2o_get_lct_reply *reply;
279 #define ALLOCSIZE (PAGE_SIZE + (256 * sizeof(struct i2o_lct_entry)))
281 if (!(reply = contigmalloc(ALLOCSIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
282 0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
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;
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);
301 if (iop_queue_wait_msg(sc, mfa, (struct i2o_basic_message *)msg)) {
302 contigfree(reply, ALLOCSIZE, M_PSTIOP);
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);
314 struct i2o_get_param_reply *
315 iop_get_util_params(struct iop_softc *sc, int target, int operation, int group)
317 struct i2o_util_get_param_message *msg;
318 struct i2o_get_param_operation *param;
319 struct i2o_get_param_reply *reply;
322 if (!(param = contigmalloc(PAGE_SIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
323 0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
326 if (!(reply = contigmalloc(PAGE_SIZE, M_PSTIOP, M_NOWAIT | M_ZERO,
327 0x00010000, 0xFFFFFFFF, sizeof(u_int32_t), 0)))
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;
341 param->operation_count = 1;
342 param->operation[0].operation = operation;
343 param->operation[0].group = group;
344 param->operation[0].field_count = 0xffff;
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);
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);
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);
359 contigfree(param, PAGE_SIZE, M_PSTIOP);
364 iop_get_mfa(struct iop_softc *sc)
369 while ((mfa = sc->reg->iqueue) == 0xffffffff && timeout) {
374 printf("pstiop: no free mfa\n");
379 iop_free_mfa(struct iop_softc *sc, int mfa)
381 struct i2o_basic_message *msg = (struct i2o_basic_message *)(sc->ibase+mfa);
383 bzero(msg, sizeof(struct i2o_basic_message));
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;
394 iop_queue_wait_msg(struct iop_softc *sc, int mfa, struct i2o_basic_message *msg)
396 struct i2o_single_reply *reply;
397 int out_mfa, status, timeout = 10000;
399 sc->reg->iqueue = mfa;
401 while (--timeout && ((out_mfa = sc->reg->oqueue) == 0xffffffff))
404 printf("pstiop: timeout waiting for message response\n");
405 iop_free_mfa(sc, mfa);
409 reply = (struct i2o_single_reply *)(sc->obase + (out_mfa - sc->phys_obase));
410 status = reply->status;
411 sc->reg->oqueue = out_mfa;
416 iop_create_sgl(struct i2o_basic_message *msg, caddr_t data, int count, int dir)
418 struct i2o_sgl *sgl = (struct i2o_sgl *)((int32_t *)msg + msg->offset);
419 u_int32_t sgl_count, sgl_phys;
422 if (((uintptr_t)data & 3) || (count & 3)) {
423 printf("pstiop: non aligned DMA transfer attempted\n");
427 printf("pstiop: zero length DMA transfer attempted\n");
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;
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");
448 sgl->phys_addr[i] = sgl_phys;
449 msg->message_size += i;