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