drm/i915: Fix i965 secured batchbuffer usage (CVE-2007-3851)
[linux/fpc-iii.git] / drivers / fc4 / socal.c
blobc903ebfab526e6416bbb915654fcc3f391a4575f
1 /* socal.c: Sparc SUNW,socal (SOC+) Fibre Channel Sbus adapter support.
3 * Copyright (C) 1998,1999 Jakub Jelinek (jj@ultra.linux.cz)
5 * Sources:
6 * Fibre Channel Physical & Signaling Interface (FC-PH), dpANS, 1994
7 * dpANS Fibre Channel Protocol for SCSI (X3.269-199X), Rev. 012, 1995
8 * SOC+ Programming Guide 0.1
9 * Fibre Channel Arbitrated Loop (FC-AL), dpANS rev. 4.5, 1995
11 * Supported hardware:
12 * On-board SOC+ adapters of Ultra Enterprise servers and sun4d.
15 static char *version =
16 "socal.c: SOC+ driver v1.1 9/Feb/99 Jakub Jelinek (jj@ultra.linux.cz)\n";
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/fcntl.h>
22 #include <linux/interrupt.h>
23 #include <linux/ptrace.h>
24 #include <linux/ioport.h>
25 #include <linux/in.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/init.h>
29 #include <linux/bitops.h>
30 #include <asm/system.h>
31 #include <asm/io.h>
32 #include <asm/dma.h>
33 #include <linux/errno.h>
34 #include <asm/byteorder.h>
36 #include <asm/openprom.h>
37 #include <asm/oplib.h>
38 #include <asm/pgtable.h>
39 #include <asm/irq.h>
41 /* #define SOCALDEBUG */
42 /* #define HAVE_SOCAL_UCODE */
43 /* #define USE_64BIT_MODE */
45 #include "fcp_impl.h"
46 #include "socal.h"
47 #ifdef HAVE_SOCAL_UCODE
48 #include "socal_asm.h"
49 #endif
51 #define socal_printk printk ("socal%d: ", s->socal_no); printk
53 #ifdef SOCALDEBUG
54 #define SOD(x) socal_printk x;
55 #else
56 #define SOD(x)
57 #endif
59 #define for_each_socal(s) for (s = socals; s; s = s->next)
60 struct socal *socals = NULL;
62 static void socal_copy_from_xram(void *d, void __iomem *xram, long size)
64 u32 *dp = (u32 *) d;
65 while (size) {
66 *dp++ = sbus_readl(xram);
67 xram += sizeof(u32);
68 size -= sizeof(u32);
72 static void socal_copy_to_xram(void __iomem *xram, void *s, long size)
74 u32 *sp = (u32 *) s;
75 while (size) {
76 u32 val = *sp++;
77 sbus_writel(val, xram);
78 xram += sizeof(u32);
79 size -= sizeof(u32);
83 #ifdef HAVE_SOCAL_UCODE
84 static void socal_bzero(unsigned long xram, int size)
86 while (size) {
87 sbus_writel(0, xram);
88 xram += sizeof(u32);
89 size -= sizeof(u32);
92 #endif
94 static inline void socal_disable(struct socal *s)
96 sbus_writel(0, s->regs + IMASK);
97 sbus_writel(SOCAL_CMD_SOFT_RESET, s->regs + CMD);
100 static inline void socal_enable(struct socal *s)
102 SOD(("enable %08x\n", s->cfg))
103 sbus_writel(0, s->regs + SAE);
104 sbus_writel(s->cfg, s->regs + CFG);
105 sbus_writel(SOCAL_CMD_RSP_QALL, s->regs + CMD);
106 SOCAL_SETIMASK(s, SOCAL_IMASK_RSP_QALL | SOCAL_IMASK_SAE);
107 SOD(("imask %08x %08x\n", s->imask, sbus_readl(s->regs + IMASK)));
110 static void socal_reset(fc_channel *fc)
112 socal_port *port = (socal_port *)fc;
113 struct socal *s = port->s;
115 /* FIXME */
116 socal_disable(s);
117 s->req[0].seqno = 1;
118 s->req[1].seqno = 1;
119 s->rsp[0].seqno = 1;
120 s->rsp[1].seqno = 1;
121 s->req[0].in = 0;
122 s->req[1].in = 0;
123 s->rsp[0].in = 0;
124 s->rsp[1].in = 0;
125 s->req[0].out = 0;
126 s->req[1].out = 0;
127 s->rsp[0].out = 0;
128 s->rsp[1].out = 0;
130 /* FIXME */
131 socal_enable(s);
134 static inline void socal_solicited(struct socal *s, unsigned long qno)
136 socal_rsp *hwrsp;
137 socal_cq *sw_cq;
138 int token;
139 int status;
140 fc_channel *fc;
142 sw_cq = &s->rsp[qno];
144 /* Finally an improvement against old SOC :) */
145 sw_cq->in = sbus_readb(s->regs + RESP + qno);
146 SOD (("socal_solicited, %d packets arrived\n",
147 (sw_cq->in - sw_cq->out) & sw_cq->last))
148 for (;;) {
149 hwrsp = (socal_rsp *)sw_cq->pool + sw_cq->out;
150 SOD(("hwrsp %p out %d\n", hwrsp, sw_cq->out))
152 #if defined(SOCALDEBUG) && 0
154 u32 *u = (u32 *)hwrsp;
155 SOD(("%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n",
156 u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7]))
157 u += 8;
158 SOD(("%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n",
159 u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7]))
160 u = (u32 *)s->xram;
161 while (u < ((u32 *)s->regs)) {
162 if (sbus_readl(&u[0]) == 0x00003000 ||
163 sbus_readl(&u[0]) == 0x00003801) {
164 SOD(("Found at %04lx\n",
165 (unsigned long)u - (unsigned long)s->xram))
166 SOD((" %08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n",
167 sbus_readl(&u[0]), sbus_readl(&u[1]),
168 sbus_readl(&u[2]), sbus_readl(&u[3]),
169 sbus_readl(&u[4]), sbus_readl(&u[5]),
170 sbus_readl(&u[6]), sbus_readl(&u[7])))
171 u += 8;
172 SOD((" %08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n",
173 sbus_readl(&u[0]), sbus_readl(&u[1]),
174 sbus_readl(&u[2]), sbus_readl(&u[3]),
175 sbus_readl(&u[4]), sbus_readl(&u[5]),
176 sbus_readl(&u[6]), sbus_readl(&u[7])))
177 u -= 8;
179 u++;
182 #endif
184 token = hwrsp->shdr.token;
185 status = hwrsp->status;
186 fc = (fc_channel *)(&s->port[(token >> 11) & 1]);
188 SOD(("Solicited token %08x status %08x\n", token, status))
189 if (status == SOCAL_OK) {
190 fcp_receive_solicited(fc, token >> 12,
191 token & ((1 << 11) - 1),
192 FC_STATUS_OK, NULL);
193 } else {
194 /* We have intentionally defined FC_STATUS_* constants
195 * to match SOCAL_* constants, otherwise we'd have to
196 * translate status.
198 fcp_receive_solicited(fc, token >> 12,
199 token & ((1 << 11) - 1), status, &hwrsp->fchdr);
202 if (++sw_cq->out > sw_cq->last) {
203 sw_cq->seqno++;
204 sw_cq->out = 0;
207 if (sw_cq->out == sw_cq->in) {
208 sw_cq->in = sbus_readb(s->regs + RESP + qno);
209 if (sw_cq->out == sw_cq->in) {
210 /* Tell the hardware about it */
211 sbus_writel((sw_cq->out << 24) |
212 (SOCAL_CMD_RSP_QALL &
213 ~(SOCAL_CMD_RSP_Q0 << qno)),
214 s->regs + CMD);
216 /* Read it, so that we're sure it has been updated */
217 sbus_readl(s->regs + CMD);
218 sw_cq->in = sbus_readb(s->regs + RESP + qno);
219 if (sw_cq->out == sw_cq->in)
220 break;
226 static inline void socal_request (struct socal *s, u32 cmd)
228 SOCAL_SETIMASK(s, s->imask & ~(cmd & SOCAL_CMD_REQ_QALL));
229 SOD(("imask %08x %08x\n", s->imask, sbus_readl(s->regs + IMASK)));
231 SOD(("Queues available %08x OUT %X\n", cmd, s->regs->reqpr[0]))
232 if (s->port[s->curr_port].fc.state != FC_STATE_OFFLINE) {
233 fcp_queue_empty ((fc_channel *)&(s->port[s->curr_port]));
234 if (((s->req[1].in + 1) & s->req[1].last) != (s->req[1].out))
235 fcp_queue_empty ((fc_channel *)&(s->port[1 - s->curr_port]));
236 } else {
237 fcp_queue_empty ((fc_channel *)&(s->port[1 - s->curr_port]));
239 if (s->port[1 - s->curr_port].fc.state != FC_STATE_OFFLINE)
240 s->curr_port ^= 1;
243 static inline void socal_unsolicited (struct socal *s, unsigned long qno)
245 socal_rsp *hwrsp, *hwrspc;
246 socal_cq *sw_cq;
247 int count;
248 int status;
249 int flags;
250 fc_channel *fc;
252 sw_cq = &s->rsp[qno];
254 sw_cq->in = sbus_readb(s->regs + RESP + qno);
255 SOD (("socal_unsolicited, %d packets arrived, in %d\n",
256 (sw_cq->in - sw_cq->out) & sw_cq->last, sw_cq->in))
257 while (sw_cq->in != sw_cq->out) {
258 /* ...real work per entry here... */
259 hwrsp = (socal_rsp *)sw_cq->pool + sw_cq->out;
260 SOD(("hwrsp %p out %d\n", hwrsp, sw_cq->out))
262 #if defined(SOCALDEBUG) && 0
264 u32 *u = (u32 *)hwrsp;
265 SOD(("%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n",
266 u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7]))
267 u += 8;
268 SOD(("%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n",
269 u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7]))
271 #endif
273 hwrspc = NULL;
274 flags = hwrsp->shdr.flags;
275 count = hwrsp->count;
276 fc = (fc_channel *)&s->port[flags & SOCAL_PORT_B];
277 SOD(("FC %08lx\n", (long)fc))
279 if (count != 1) {
280 /* Ugh, continuation entries */
281 u8 in;
283 if (count != 2) {
284 printk("%s: Too many continuations entries %d\n",
285 fc->name, count);
286 goto update_out;
289 in = sw_cq->in;
290 if (in < sw_cq->out)
291 in += sw_cq->last + 1;
292 if (in < sw_cq->out + 2) {
293 /* Ask the hardware if they haven't arrived yet. */
294 sbus_writel((sw_cq->out << 24) |
295 (SOCAL_CMD_RSP_QALL &
296 ~(SOCAL_CMD_RSP_Q0 << qno)),
297 s->regs + CMD);
299 /* Read it, so that we're sure it has been updated */
300 sbus_readl(s->regs + CMD);
301 sw_cq->in = sbus_readb(s->regs + RESP + qno);
302 in = sw_cq->in;
303 if (in < sw_cq->out)
304 in += sw_cq->last + 1;
305 if (in < sw_cq->out + 2) /* Nothing came, let us wait */
306 return;
308 if (sw_cq->out == sw_cq->last)
309 hwrspc = (socal_rsp *)sw_cq->pool;
310 else
311 hwrspc = hwrsp + 1;
314 switch (flags & ~SOCAL_PORT_B) {
315 case SOCAL_STATUS:
316 status = hwrsp->status;
317 switch (status) {
318 case SOCAL_ONLINE:
319 SOD(("State change to ONLINE\n"));
320 fcp_state_change(fc, FC_STATE_ONLINE);
321 break;
322 case SOCAL_ONLINE_LOOP:
323 SOD(("State change to ONLINE_LOOP\n"));
324 fcp_state_change(fc, FC_STATE_ONLINE);
325 break;
326 case SOCAL_OFFLINE:
327 SOD(("State change to OFFLINE\n"));
328 fcp_state_change(fc, FC_STATE_OFFLINE);
329 break;
330 default:
331 printk ("%s: Unknown STATUS no %d\n",
332 fc->name, status);
333 break;
336 break;
337 case (SOCAL_UNSOLICITED|SOCAL_FC_HDR):
339 int r_ctl = *((u8 *)&hwrsp->fchdr);
340 unsigned len;
342 if ((r_ctl & 0xf0) == R_CTL_EXTENDED_SVC) {
343 len = hwrsp->shdr.bytecnt;
344 if (len < 4 || !hwrspc) {
345 printk ("%s: Invalid R_CTL %02x "
346 "continuation entries\n",
347 fc->name, r_ctl);
348 } else {
349 if (len > 60)
350 len = 60;
351 if (*(u32 *)hwrspc == LS_DISPLAY) {
352 int i;
354 for (i = 4; i < len; i++)
355 if (((u8 *)hwrspc)[i] == '\n')
356 ((u8 *)hwrspc)[i] = ' ';
357 ((u8 *)hwrspc)[len] = 0;
358 printk ("%s message: %s\n",
359 fc->name, ((u8 *)hwrspc) + 4);
360 } else {
361 printk ("%s: Unknown LS_CMD "
362 "%08x\n", fc->name,
363 *(u32 *)hwrspc);
366 } else {
367 printk ("%s: Unsolicited R_CTL %02x "
368 "not handled\n", fc->name, r_ctl);
371 break;
372 default:
373 printk ("%s: Unexpected flags %08x\n", fc->name, flags);
374 break;
376 update_out:
377 if (++sw_cq->out > sw_cq->last) {
378 sw_cq->seqno++;
379 sw_cq->out = 0;
382 if (hwrspc) {
383 if (++sw_cq->out > sw_cq->last) {
384 sw_cq->seqno++;
385 sw_cq->out = 0;
389 if (sw_cq->out == sw_cq->in) {
390 sw_cq->in = sbus_readb(s->regs + RESP + qno);
391 if (sw_cq->out == sw_cq->in) {
392 /* Tell the hardware about it */
393 sbus_writel((sw_cq->out << 24) |
394 (SOCAL_CMD_RSP_QALL &
395 ~(SOCAL_CMD_RSP_Q0 << qno)),
396 s->regs + CMD);
398 /* Read it, so that we're sure it has been updated */
399 sbus_readl(s->regs + CMD);
400 sw_cq->in = sbus_readb(s->regs + RESP + qno);
406 static irqreturn_t socal_intr(int irq, void *dev_id)
408 u32 cmd;
409 unsigned long flags;
410 register struct socal *s = (struct socal *)dev_id;
412 spin_lock_irqsave(&s->lock, flags);
413 cmd = sbus_readl(s->regs + CMD);
414 for (; (cmd = SOCAL_INTR (s, cmd)); cmd = sbus_readl(s->regs + CMD)) {
415 #ifdef SOCALDEBUG
416 static int cnt = 0;
417 if (cnt++ < 50)
418 printk("soc_intr %08x\n", cmd);
419 #endif
420 if (cmd & SOCAL_CMD_RSP_Q2)
421 socal_unsolicited (s, SOCAL_UNSOLICITED_RSP_Q);
422 if (cmd & SOCAL_CMD_RSP_Q1)
423 socal_unsolicited (s, SOCAL_SOLICITED_BAD_RSP_Q);
424 if (cmd & SOCAL_CMD_RSP_Q0)
425 socal_solicited (s, SOCAL_SOLICITED_RSP_Q);
426 if (cmd & SOCAL_CMD_REQ_QALL)
427 socal_request (s, cmd);
429 spin_unlock_irqrestore(&s->lock, flags);
431 return IRQ_HANDLED;
434 #define TOKEN(proto, port, token) (((proto)<<12)|(token)|(port))
436 static int socal_hw_enque (fc_channel *fc, fcp_cmnd *fcmd)
438 socal_port *port = (socal_port *)fc;
439 struct socal *s = port->s;
440 unsigned long qno;
441 socal_cq *sw_cq;
442 int cq_next_in;
443 socal_req *request;
444 fc_hdr *fch;
445 int i;
447 if (fcmd->proto == TYPE_SCSI_FCP)
448 qno = 1;
449 else
450 qno = 0;
451 SOD(("Putting a FCP packet type %d into hw queue %d\n", fcmd->proto, qno))
452 if (s->imask & (SOCAL_IMASK_REQ_Q0 << qno)) {
453 SOD(("EIO %08x\n", s->imask))
454 return -EIO;
456 sw_cq = s->req + qno;
457 cq_next_in = (sw_cq->in + 1) & sw_cq->last;
459 if (cq_next_in == sw_cq->out &&
460 cq_next_in == (sw_cq->out = sbus_readb(s->regs + REQP + qno))) {
461 SOD(("%d IN %d OUT %d LAST %d\n",
462 qno, sw_cq->in,
463 sw_cq->out, sw_cq->last))
464 SOCAL_SETIMASK(s, s->imask | (SOCAL_IMASK_REQ_Q0 << qno));
465 SOD(("imask %08x %08x\n", s->imask, sbus_readl(s->regs + IMASK)));
467 /* If queue is full, just say NO. */
468 return -EBUSY;
471 request = sw_cq->pool + sw_cq->in;
472 fch = &request->fchdr;
474 switch (fcmd->proto) {
475 case TYPE_SCSI_FCP:
476 request->shdr.token = TOKEN(TYPE_SCSI_FCP, port->mask, fcmd->token);
477 request->data[0].base = fc->dma_scsi_cmd + fcmd->token * sizeof(fcp_cmd);
478 request->data[0].count = sizeof(fcp_cmd);
479 request->data[1].base = fc->dma_scsi_rsp + fcmd->token * fc->rsp_size;
480 request->data[1].count = fc->rsp_size;
481 if (fcmd->data) {
482 request->shdr.segcnt = 3;
483 i = fc->scsi_cmd_pool[fcmd->token].fcp_data_len;
484 request->shdr.bytecnt = i;
485 request->data[2].base = fcmd->data;
486 request->data[2].count = i;
487 request->type = (fc->scsi_cmd_pool[fcmd->token].fcp_cntl & FCP_CNTL_WRITE) ?
488 SOCAL_CQTYPE_IO_WRITE : SOCAL_CQTYPE_IO_READ;
489 } else {
490 request->shdr.segcnt = 2;
491 request->shdr.bytecnt = 0;
492 request->data[2].base = 0;
493 request->data[2].count = 0;
494 request->type = SOCAL_CQTYPE_SIMPLE;
496 FILL_FCHDR_RCTL_DID(fch, R_CTL_COMMAND, fcmd->did);
497 FILL_FCHDR_SID(fch, fc->sid);
498 FILL_FCHDR_TYPE_FCTL(fch, TYPE_SCSI_FCP, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
499 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
500 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
501 fch->param = 0;
502 request->shdr.flags = port->flags;
503 request->shdr.class = fc->posmap ? 3 : 2;
504 break;
506 case PROTO_OFFLINE:
507 memset (request, 0, sizeof(*request));
508 request->shdr.token = TOKEN(PROTO_OFFLINE, port->mask, fcmd->token);
509 request->type = SOCAL_CQTYPE_OFFLINE;
510 FILL_FCHDR_RCTL_DID(fch, R_CTL_COMMAND, fcmd->did);
511 FILL_FCHDR_SID(fch, fc->sid);
512 FILL_FCHDR_TYPE_FCTL(fch, TYPE_SCSI_FCP, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
513 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
514 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
515 request->shdr.flags = port->flags;
516 break;
518 case PROTO_REPORT_AL_MAP:
519 memset (request, 0, sizeof(*request));
520 request->shdr.token = TOKEN(PROTO_REPORT_AL_MAP, port->mask, fcmd->token);
521 request->type = SOCAL_CQTYPE_REPORT_MAP;
522 request->shdr.flags = port->flags;
523 request->shdr.segcnt = 1;
524 request->shdr.bytecnt = sizeof(fc_al_posmap);
525 request->data[0].base = fcmd->cmd;
526 request->data[0].count = sizeof(fc_al_posmap);
527 break;
529 default:
530 request->shdr.token = TOKEN(fcmd->proto, port->mask, fcmd->token);
531 request->shdr.class = fc->posmap ? 3 : 2;
532 request->shdr.flags = port->flags;
533 memcpy (fch, &fcmd->fch, sizeof(fc_hdr));
534 request->data[0].count = fcmd->cmdlen;
535 request->data[1].count = fcmd->rsplen;
536 request->type = fcmd->class;
537 switch (fcmd->class) {
538 case FC_CLASS_OUTBOUND:
539 request->data[0].base = fcmd->cmd;
540 request->data[0].count = fcmd->cmdlen;
541 request->type = SOCAL_CQTYPE_OUTBOUND;
542 request->shdr.bytecnt = fcmd->cmdlen;
543 request->shdr.segcnt = 1;
544 break;
545 case FC_CLASS_INBOUND:
546 request->data[0].base = fcmd->rsp;
547 request->data[0].count = fcmd->rsplen;
548 request->type = SOCAL_CQTYPE_INBOUND;
549 request->shdr.bytecnt = 0;
550 request->shdr.segcnt = 1;
551 break;
552 case FC_CLASS_SIMPLE:
553 request->data[0].base = fcmd->cmd;
554 request->data[1].base = fcmd->rsp;
555 request->data[0].count = fcmd->cmdlen;
556 request->data[1].count = fcmd->rsplen;
557 request->type = SOCAL_CQTYPE_SIMPLE;
558 request->shdr.bytecnt = fcmd->cmdlen;
559 request->shdr.segcnt = 2;
560 break;
561 case FC_CLASS_IO_READ:
562 case FC_CLASS_IO_WRITE:
563 request->data[0].base = fcmd->cmd;
564 request->data[1].base = fcmd->rsp;
565 request->data[0].count = fcmd->cmdlen;
566 request->data[1].count = fcmd->rsplen;
567 request->type = (fcmd->class == FC_CLASS_IO_READ) ? SOCAL_CQTYPE_IO_READ : SOCAL_CQTYPE_IO_WRITE;
568 if (fcmd->data) {
569 request->data[2].base = fcmd->data;
570 request->data[2].count = fcmd->datalen;
571 request->shdr.bytecnt = fcmd->datalen;
572 request->shdr.segcnt = 3;
573 } else {
574 request->shdr.bytecnt = 0;
575 request->shdr.segcnt = 2;
577 break;
579 break;
582 request->count = 1;
583 request->flags = 0;
584 request->seqno = sw_cq->seqno;
586 SOD(("queueing token %08x\n", request->shdr.token))
588 /* And now tell the SOCAL about it */
590 if (++sw_cq->in > sw_cq->last) {
591 sw_cq->in = 0;
592 sw_cq->seqno++;
595 SOD(("Putting %08x into cmd\n", SOCAL_CMD_RSP_QALL | (sw_cq->in << 24) | (SOCAL_CMD_REQ_Q0 << qno)))
597 sbus_writel(SOCAL_CMD_RSP_QALL | (sw_cq->in << 24) | (SOCAL_CMD_REQ_Q0 << qno),
598 s->regs + CMD);
600 /* Read so that command is completed */
601 sbus_readl(s->regs + CMD);
603 return 0;
606 static inline void socal_download_fw(struct socal *s)
608 #ifdef HAVE_SOCAL_UCODE
609 SOD(("Loading %ld bytes from %p to %p\n", sizeof(socal_ucode), socal_ucode, s->xram))
610 socal_copy_to_xram(s->xram, socal_ucode, sizeof(socal_ucode));
611 SOD(("Clearing the rest of memory\n"))
612 socal_bzero (s->xram + sizeof(socal_ucode), 65536 - sizeof(socal_ucode));
613 SOD(("Done\n"))
614 #endif
617 /* Check for what the best SBUS burst we can use happens
618 * to be on this machine.
620 static inline void socal_init_bursts(struct socal *s, struct sbus_dev *sdev)
622 int bsizes, bsizes_more;
623 u32 cfg;
625 bsizes = (prom_getintdefault(sdev->prom_node,"burst-sizes",0xff) & 0xff);
626 bsizes_more = (prom_getintdefault(sdev->bus->prom_node, "burst-sizes", 0xff) & 0xff);
627 bsizes &= bsizes_more;
628 #ifdef USE_64BIT_MODE
629 #ifdef __sparc_v9__
630 mmu_set_sbus64(sdev, bsizes >> 16);
631 #endif
632 #endif
633 if ((bsizes & 0x7f) == 0x7f)
634 cfg = SOCAL_CFG_BURST_64;
635 else if ((bsizes & 0x3f) == 0x3f)
636 cfg = SOCAL_CFG_BURST_32;
637 else if ((bsizes & 0x1f) == 0x1f)
638 cfg = SOCAL_CFG_BURST_16;
639 else
640 cfg = SOCAL_CFG_BURST_4;
641 #ifdef USE_64BIT_MODE
642 #ifdef __sparc_v9__
643 /* What is BURST_128? -jj */
644 if ((bsizes & 0x780000) == 0x780000)
645 cfg |= (SOCAL_CFG_BURST_64 << 8) | SOCAL_CFG_SBUS_ENHANCED;
646 else if ((bsizes & 0x380000) == 0x380000)
647 cfg |= (SOCAL_CFG_BURST_32 << 8) | SOCAL_CFG_SBUS_ENHANCED;
648 else if ((bsizes & 0x180000) == 0x180000)
649 cfg |= (SOCAL_CFG_BURST_16 << 8) | SOCAL_CFG_SBUS_ENHANCED;
650 else
651 cfg |= (SOCAL_CFG_BURST_8 << 8) | SOCAL_CFG_SBUS_ENHANCED;
652 #endif
653 #endif
654 s->cfg = cfg;
657 static inline void socal_init(struct sbus_dev *sdev, int no)
659 unsigned char tmp[60];
660 int propl;
661 struct socal *s;
662 static unsigned version_printed = 0;
663 socal_hw_cq cq[8];
664 int size, i;
665 int irq, node;
667 s = kzalloc (sizeof (struct socal), GFP_KERNEL);
668 if (!s) return;
669 spin_lock_init(&s->lock);
670 s->socal_no = no;
672 SOD(("socals %08lx socal_intr %08lx socal_hw_enque %08lx\n",
673 (long)socals, (long)socal_intr, (long)socal_hw_enque))
674 if (version_printed++ == 0)
675 printk (version);
677 s->port[0].fc.module = THIS_MODULE;
678 s->port[1].fc.module = THIS_MODULE;
680 s->next = socals;
681 socals = s;
682 s->port[0].fc.dev = sdev;
683 s->port[1].fc.dev = sdev;
684 s->port[0].s = s;
685 s->port[1].s = s;
687 s->port[0].fc.next = &s->port[1].fc;
689 /* World Wide Name of SOCAL */
690 propl = prom_getproperty (sdev->prom_node, "wwn", tmp, sizeof(tmp));
691 if (propl != sizeof (fc_wwn)) {
692 s->wwn.naaid = NAAID_IEEE_REG;
693 s->wwn.nportid = 0x123;
694 s->wwn.hi = 0x1234;
695 s->wwn.lo = 0x12345678;
696 } else
697 memcpy (&s->wwn, tmp, sizeof (fc_wwn));
699 memcpy (&s->port[0].fc.wwn_nport, &s->wwn, sizeof (fc_wwn));
700 s->port[0].fc.wwn_nport.lo++;
701 memcpy (&s->port[1].fc.wwn_nport, &s->wwn, sizeof (fc_wwn));
702 s->port[1].fc.wwn_nport.lo+=2;
704 node = prom_getchild (sdev->prom_node);
705 while (node && (node = prom_searchsiblings (node, "sf"))) {
706 int port;
708 port = prom_getintdefault(node, "port#", -1);
709 switch (port) {
710 case 0:
711 case 1:
712 if (prom_getproplen(node, "port-wwn") == sizeof (fc_wwn))
713 prom_getproperty (node, "port-wwn",
714 (char *)&s->port[port].fc.wwn_nport,
715 sizeof (fc_wwn));
716 break;
717 default:
718 break;
721 node = prom_getsibling(node);
724 memcpy (&s->port[0].fc.wwn_node, &s->wwn, sizeof (fc_wwn));
725 memcpy (&s->port[1].fc.wwn_node, &s->wwn, sizeof (fc_wwn));
726 SOD(("Got wwns %08x%08x ports %08x%08x and %08x%08x\n",
727 *(u32 *)&s->port[0].fc.wwn_node, s->port[0].fc.wwn_node.lo,
728 *(u32 *)&s->port[0].fc.wwn_nport, s->port[0].fc.wwn_nport.lo,
729 *(u32 *)&s->port[1].fc.wwn_nport, s->port[1].fc.wwn_nport.lo))
731 s->port[0].fc.sid = 1;
732 s->port[1].fc.sid = 17;
733 s->port[0].fc.did = 2;
734 s->port[1].fc.did = 18;
736 s->port[0].fc.reset = socal_reset;
737 s->port[1].fc.reset = socal_reset;
739 if (sdev->num_registers == 1) {
740 s->eeprom = sbus_ioremap(&sdev->resource[0], 0,
741 sdev->reg_addrs[0].reg_size, "socal xram");
742 if (sdev->reg_addrs[0].reg_size > 0x20000)
743 s->xram = s->eeprom + 0x10000UL;
744 else
745 s->xram = s->eeprom;
746 s->regs = (s->xram + 0x10000UL);
747 } else {
748 /* E.g. starfire presents 3 registers for SOCAL */
749 s->xram = sbus_ioremap(&sdev->resource[1], 0,
750 sdev->reg_addrs[1].reg_size, "socal xram");
751 s->regs = sbus_ioremap(&sdev->resource[2], 0,
752 sdev->reg_addrs[2].reg_size, "socal regs");
755 socal_init_bursts(s, sdev);
757 SOD(("Disabling SOCAL\n"))
759 socal_disable (s);
761 irq = sdev->irqs[0];
763 if (request_irq (irq, socal_intr, IRQF_SHARED, "SOCAL", (void *)s)) {
764 socal_printk ("Cannot order irq %d to go\n", irq);
765 socals = s->next;
766 return;
769 SOD(("SOCAL uses IRQ %d\n", irq))
771 s->port[0].fc.irq = irq;
772 s->port[1].fc.irq = irq;
774 sprintf (s->port[0].fc.name, "socal%d port A", no);
775 sprintf (s->port[1].fc.name, "socal%d port B", no);
776 s->port[0].flags = SOCAL_FC_HDR | SOCAL_PORT_A;
777 s->port[1].flags = SOCAL_FC_HDR | SOCAL_PORT_B;
778 s->port[1].mask = (1 << 11);
780 s->port[0].fc.hw_enque = socal_hw_enque;
781 s->port[1].fc.hw_enque = socal_hw_enque;
783 socal_download_fw (s);
785 SOD(("Downloaded firmware\n"))
787 /* Now setup xram circular queues */
788 memset (cq, 0, sizeof(cq));
790 size = (SOCAL_CQ_REQ0_SIZE + SOCAL_CQ_REQ1_SIZE +
791 SOCAL_CQ_RSP0_SIZE + SOCAL_CQ_RSP1_SIZE +
792 SOCAL_CQ_RSP2_SIZE) * sizeof(socal_req);
793 s->req_cpu = sbus_alloc_consistent(sdev, size, &s->req_dvma);
794 s->req[0].pool = s->req_cpu;
795 cq[0].address = s->req_dvma;
796 s->req[1].pool = s->req[0].pool + SOCAL_CQ_REQ0_SIZE;
797 s->rsp[0].pool = s->req[1].pool + SOCAL_CQ_REQ1_SIZE;
798 s->rsp[1].pool = s->rsp[0].pool + SOCAL_CQ_RSP0_SIZE;
799 s->rsp[2].pool = s->rsp[1].pool + SOCAL_CQ_RSP1_SIZE;
801 s->req[0].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_REQ_OFFSET);
802 s->req[1].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_REQ_OFFSET + sizeof(socal_hw_cq));
803 s->rsp[0].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_RSP_OFFSET);
804 s->rsp[1].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_RSP_OFFSET + sizeof(socal_hw_cq));
805 s->rsp[2].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_RSP_OFFSET + 2 * sizeof(socal_hw_cq));
807 cq[1].address = cq[0].address + (SOCAL_CQ_REQ0_SIZE * sizeof(socal_req));
808 cq[4].address = cq[1].address + (SOCAL_CQ_REQ1_SIZE * sizeof(socal_req));
809 cq[5].address = cq[4].address + (SOCAL_CQ_RSP0_SIZE * sizeof(socal_req));
810 cq[6].address = cq[5].address + (SOCAL_CQ_RSP1_SIZE * sizeof(socal_req));
812 cq[0].last = SOCAL_CQ_REQ0_SIZE - 1;
813 cq[1].last = SOCAL_CQ_REQ1_SIZE - 1;
814 cq[4].last = SOCAL_CQ_RSP0_SIZE - 1;
815 cq[5].last = SOCAL_CQ_RSP1_SIZE - 1;
816 cq[6].last = SOCAL_CQ_RSP2_SIZE - 1;
817 for (i = 0; i < 8; i++)
818 cq[i].seqno = 1;
820 s->req[0].last = SOCAL_CQ_REQ0_SIZE - 1;
821 s->req[1].last = SOCAL_CQ_REQ1_SIZE - 1;
822 s->rsp[0].last = SOCAL_CQ_RSP0_SIZE - 1;
823 s->rsp[1].last = SOCAL_CQ_RSP1_SIZE - 1;
824 s->rsp[2].last = SOCAL_CQ_RSP2_SIZE - 1;
826 s->req[0].seqno = 1;
827 s->req[1].seqno = 1;
828 s->rsp[0].seqno = 1;
829 s->rsp[1].seqno = 1;
830 s->rsp[2].seqno = 1;
832 socal_copy_to_xram(s->xram + SOCAL_CQ_REQ_OFFSET, cq, sizeof(cq));
834 SOD(("Setting up params\n"))
836 /* Make our sw copy of SOCAL service parameters */
837 socal_copy_from_xram(s->serv_params, s->xram + 0x280, sizeof (s->serv_params));
839 s->port[0].fc.common_svc = (common_svc_parm *)s->serv_params;
840 s->port[0].fc.class_svcs = (svc_parm *)(s->serv_params + 0x20);
841 s->port[1].fc.common_svc = (common_svc_parm *)&s->serv_params;
842 s->port[1].fc.class_svcs = (svc_parm *)(s->serv_params + 0x20);
844 socal_enable (s);
846 SOD(("Enabled SOCAL\n"))
849 static int __init socal_probe(void)
851 struct sbus_bus *sbus;
852 struct sbus_dev *sdev = NULL;
853 struct socal *s;
854 int cards = 0;
856 for_each_sbus(sbus) {
857 for_each_sbusdev(sdev, sbus) {
858 if(!strcmp(sdev->prom_name, "SUNW,socal")) {
859 socal_init(sdev, cards);
860 cards++;
864 if (!cards)
865 return -EIO;
867 for_each_socal(s)
868 if (s->next)
869 s->port[1].fc.next = &s->next->port[0].fc;
871 fcp_init (&socals->port[0].fc);
872 return 0;
875 static void __exit socal_cleanup(void)
877 struct socal *s;
878 int irq;
879 struct sbus_dev *sdev;
881 for_each_socal(s) {
882 irq = s->port[0].fc.irq;
883 free_irq (irq, s);
885 fcp_release(&(s->port[0].fc), 2);
887 sdev = s->port[0].fc.dev;
888 if (sdev->num_registers == 1) {
889 sbus_iounmap(s->eeprom, sdev->reg_addrs[0].reg_size);
890 } else {
891 sbus_iounmap(s->xram, sdev->reg_addrs[1].reg_size);
892 sbus_iounmap(s->regs, sdev->reg_addrs[2].reg_size);
894 sbus_free_consistent(sdev,
895 (SOCAL_CQ_REQ0_SIZE + SOCAL_CQ_REQ1_SIZE +
896 SOCAL_CQ_RSP0_SIZE + SOCAL_CQ_RSP1_SIZE +
897 SOCAL_CQ_RSP2_SIZE) * sizeof(socal_req),
898 s->req_cpu, s->req_dvma);
902 module_init(socal_probe);
903 module_exit(socal_cleanup);
904 MODULE_LICENSE("GPL");