Merge branch 'for-linus' of git://one.firstfloor.org/home/andi/git/linux-2.6
[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/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>
35
36 #include <asm/openprom.h>
37 #include <asm/oplib.h>
38 #include <asm/pgtable.h>
39 #include <asm/irq.h>
40
41 /* #define SOCALDEBUG */
42 /* #define HAVE_SOCAL_UCODE */
43 /* #define USE_64BIT_MODE */
44
45 #include "fcp_impl.h"
46 #include "socal.h"
47 #ifdef HAVE_SOCAL_UCODE
48 #include "socal_asm.h"
49 #endif
50
51 #define socal_printk printk ("socal%d: ", s->socal_no); printk 
52
53 #ifdef SOCALDEBUG
54 #define SOD(x)  socal_printk x;
55 #else
56 #define SOD(x)
57 #endif
58
59 #define for_each_socal(s) for (s = socals; s; s = s->next)
60 struct socal *socals = NULL;
61
62 static void socal_copy_from_xram(void *d, void __iomem *xram, long size)
63 {
64         u32 *dp = (u32 *) d;
65         while (size) {
66                 *dp++ = sbus_readl(xram);
67                 xram += sizeof(u32);
68                 size -= sizeof(u32);
69         }
70 }
71
72 static void socal_copy_to_xram(void __iomem *xram, void *s, long size)
73 {
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);
80         }
81 }
82
83 #ifdef HAVE_SOCAL_UCODE
84 static void socal_bzero(unsigned long xram, int size)
85 {
86         while (size) {
87                 sbus_writel(0, xram);
88                 xram += sizeof(u32);
89                 size -= sizeof(u32);
90         }
91 }
92 #endif
93
94 static inline void socal_disable(struct socal *s)
95 {
96         sbus_writel(0, s->regs + IMASK);
97         sbus_writel(SOCAL_CMD_SOFT_RESET, s->regs + CMD);
98 }
99
100 static inline void socal_enable(struct socal *s)
101 {
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)));
108 }
109
110 static void socal_reset(fc_channel *fc)
111 {
112         socal_port *port = (socal_port *)fc;
113         struct socal *s = port->s;
114         
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;
129
130         /* FIXME */
131         socal_enable(s);
132 }
133
134 static inline void socal_solicited(struct socal *s, unsigned long qno)
135 {
136         socal_rsp *hwrsp;
137         socal_cq *sw_cq;
138         int token;
139         int status;
140         fc_channel *fc;
141
142         sw_cq = &s->rsp[qno];
143
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))
151                 
152 #if defined(SOCALDEBUG) && 0
153                 {
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;
178                         }
179                         u++;
180                 }
181                 }
182 #endif
183
184                 token = hwrsp->shdr.token;
185                 status = hwrsp->status;
186                 fc = (fc_channel *)(&s->port[(token >> 11) & 1]);
187                 
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.
197                          */
198                         fcp_receive_solicited(fc, token >> 12,
199                                               token & ((1 << 11) - 1), status, &hwrsp->fchdr);
200                 }
201                         
202                 if (++sw_cq->out > sw_cq->last) {
203                         sw_cq->seqno++;
204                         sw_cq->out = 0;
205                 }
206                 
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);
215
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;
221                         }
222                 }
223         }
224 }
225
226 static inline void socal_request (struct socal *s, u32 cmd)
227 {
228         SOCAL_SETIMASK(s, s->imask & ~(cmd & SOCAL_CMD_REQ_QALL));
229         SOD(("imask %08x %08x\n", s->imask, sbus_readl(s->regs + IMASK)));
230
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]));
238         }
239         if (s->port[1 - s->curr_port].fc.state != FC_STATE_OFFLINE)
240                 s->curr_port ^= 1;
241 }
242
243 static inline void socal_unsolicited (struct socal *s, unsigned long qno)
244 {
245         socal_rsp *hwrsp, *hwrspc;
246         socal_cq *sw_cq;
247         int count;
248         int status;
249         int flags;
250         fc_channel *fc;
251
252         sw_cq = &s->rsp[qno];
253
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))
261
262 #if defined(SOCALDEBUG) && 0
263                 {
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]))
270                 }
271 #endif
272
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))
278                 
279                 if (count != 1) {
280                         /* Ugh, continuation entries */
281                         u8 in;
282
283                         if (count != 2) {
284                                 printk("%s: Too many continuations entries %d\n",
285                                        fc->name, count);
286                                 goto update_out;
287                         }
288                         
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);
298
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;
307                         }
308                         if (sw_cq->out == sw_cq->last)
309                                 hwrspc = (socal_rsp *)sw_cq->pool;
310                         else
311                                 hwrspc = hwrsp + 1;
312                 }
313                 
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;
334                         };
335
336                         break;
337                 case (SOCAL_UNSOLICITED|SOCAL_FC_HDR):
338                         {
339                                 int r_ctl = *((u8 *)&hwrsp->fchdr);
340                                 unsigned len;
341                                 
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;
353                                                         
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);
364                                                 }
365                                         }
366                                 } else {
367                                         printk ("%s: Unsolicited R_CTL %02x "
368                                                 "not handled\n", fc->name, r_ctl);
369                                 }
370                         }
371                         break;
372                 default:
373                         printk ("%s: Unexpected flags %08x\n", fc->name, flags);
374                         break;
375                 };
376 update_out:
377                 if (++sw_cq->out > sw_cq->last) {
378                         sw_cq->seqno++;
379                         sw_cq->out = 0;
380                 }
381                 
382                 if (hwrspc) {
383                         if (++sw_cq->out > sw_cq->last) {
384                                 sw_cq->seqno++;
385                                 sw_cq->out = 0;
386                         }
387                 }
388                 
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);
397
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);
401                         }
402                 }
403         }
404 }
405
406 static irqreturn_t socal_intr(int irq, void *dev_id)
407 {
408         u32 cmd;
409         unsigned long flags;
410         register struct socal *s = (struct socal *)dev_id;
411
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);
428         }
429         spin_unlock_irqrestore(&s->lock, flags);
430
431         return IRQ_HANDLED;
432 }
433
434 #define TOKEN(proto, port, token) (((proto)<<12)|(token)|(port))
435
436 static int socal_hw_enque (fc_channel *fc, fcp_cmnd *fcmd)
437 {
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;
446
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;
455         }
456         sw_cq = s->req + qno;
457         cq_next_in = (sw_cq->in + 1) & sw_cq->last;
458         
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)));
466
467                 /* If queue is full, just say NO. */
468                 return -EBUSY;
469         }
470         
471         request = sw_cq->pool + sw_cq->in;
472         fch = &request->fchdr;
473         
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;
495                 }
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;
505                 
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;
517                 
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;
528
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;
576                         }
577                         break;
578                 }
579                 break;
580         }
581
582         request->count = 1;
583         request->flags = 0;
584         request->seqno = sw_cq->seqno;
585         
586         SOD(("queueing token %08x\n", request->shdr.token))
587         
588         /* And now tell the SOCAL about it */
589
590         if (++sw_cq->in > sw_cq->last) {
591                 sw_cq->in = 0;
592                 sw_cq->seqno++;
593         }
594         
595         SOD(("Putting %08x into cmd\n", SOCAL_CMD_RSP_QALL | (sw_cq->in << 24) | (SOCAL_CMD_REQ_Q0 << qno)))
596         
597         sbus_writel(SOCAL_CMD_RSP_QALL | (sw_cq->in << 24) | (SOCAL_CMD_REQ_Q0 << qno),
598                     s->regs + CMD);
599
600         /* Read so that command is completed */ 
601         sbus_readl(s->regs + CMD);
602         
603         return 0;
604 }
605
606 static inline void socal_download_fw(struct socal *s)
607 {
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
615 }
616
617 /* Check for what the best SBUS burst we can use happens
618  * to be on this machine.
619  */
620 static inline void socal_init_bursts(struct socal *s, struct sbus_dev *sdev)
621 {
622         int bsizes, bsizes_more;
623         u32 cfg;
624
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;
655 }
656
657 static inline void socal_init(struct sbus_dev *sdev, int no)
658 {
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;
666         
667         s = kzalloc (sizeof (struct socal), GFP_KERNEL);
668         if (!s) return;
669         spin_lock_init(&s->lock);
670         s->socal_no = no;
671
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);
676
677         s->port[0].fc.module = THIS_MODULE;
678         s->port[1].fc.module = THIS_MODULE;
679                                         
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;
686
687         s->port[0].fc.next = &s->port[1].fc;
688
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));
698         
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;
703         
704         node = prom_getchild (sdev->prom_node);
705         while (node && (node = prom_searchsiblings (node, "sf"))) {
706                 int port;
707                 
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;
719                 };
720
721                 node = prom_getsibling(node);
722         }
723
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))
730                 
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;
735         
736         s->port[0].fc.reset = socal_reset;
737         s->port[1].fc.reset = socal_reset;
738         
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");
753         }
754         
755         socal_init_bursts(s, sdev);
756         
757         SOD(("Disabling SOCAL\n"))
758         
759         socal_disable (s);
760         
761         irq = sdev->irqs[0];
762
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;
767         }
768
769         SOD(("SOCAL uses IRQ %d\n", irq))
770         
771         s->port[0].fc.irq = irq;
772         s->port[1].fc.irq = irq;
773         
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);
779         
780         s->port[0].fc.hw_enque = socal_hw_enque;
781         s->port[1].fc.hw_enque = socal_hw_enque;
782         
783         socal_download_fw (s);
784         
785         SOD(("Downloaded firmware\n"))
786
787         /* Now setup xram circular queues */
788         memset (cq, 0, sizeof(cq));
789
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;
800         
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));
806         
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));
811
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;
819         
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;
825         
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;
831         
832         socal_copy_to_xram(s->xram + SOCAL_CQ_REQ_OFFSET, cq, sizeof(cq));
833         
834         SOD(("Setting up params\n"))
835         
836         /* Make our sw copy of SOCAL service parameters */
837         socal_copy_from_xram(s->serv_params, s->xram + 0x280, sizeof (s->serv_params));
838         
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);
843         
844         socal_enable (s);
845         
846         SOD(("Enabled SOCAL\n"))
847 }
848
849 static int __init socal_probe(void)
850 {
851         struct sbus_bus *sbus;
852         struct sbus_dev *sdev = NULL;
853         struct socal *s;
854         int cards = 0;
855
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++;
861                         }
862                 }
863         }
864         if (!cards)
865                 return -EIO;
866
867         for_each_socal(s)
868                 if (s->next)
869                         s->port[1].fc.next = &s->next->port[0].fc;
870                         
871         fcp_init (&socals->port[0].fc);
872         return 0;
873 }
874
875 static void __exit socal_cleanup(void)
876 {
877         struct socal *s;
878         int irq;
879         struct sbus_dev *sdev;
880         
881         for_each_socal(s) {
882                 irq = s->port[0].fc.irq;
883                 free_irq (irq, s);
884
885                 fcp_release(&(s->port[0].fc), 2);
886
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);
893                 }
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);
899         }
900 }
901
902 module_init(socal_probe);
903 module_exit(socal_cleanup);
904 MODULE_LICENSE("GPL");