Merge /spare/repo/linux-2.6/
[linux-2.6] / drivers / fc4 / soc.c
1 /* soc.c: Sparc SUNW,soc (Serial Optical Channel) Fibre Channel Sbus adapter support.
2  *
3  * Copyright (C) 1996,1997,1999 Jakub Jelinek (jj@ultra.linux.cz)
4  * Copyright (C) 1997,1998 Jirka Hanika (geo@ff.cuni.cz)
5  *
6  * Sources:
7  *      Fibre Channel Physical & Signaling Interface (FC-PH), dpANS, 1994
8  *      dpANS Fibre Channel Protocol for SCSI (X3.269-199X), Rev. 012, 1995
9  *
10  * Supported hardware:
11  *      Tested on SOC sbus card bought with SS1000 in Linux running on SS5 and Ultra1. 
12  *      For SOC sbus cards, you have to make sure your FCode is 1.52 or later.
13  *      If you have older FCode, you should try to upgrade or get SOC microcode from Sun
14  *      (the microcode is present in Solaris soc driver as well). In that case you need
15  *      to #define HAVE_SOC_UCODE and format the microcode into soc_asm.c. For the exact
16  *      format mail me and I will tell you. I cannot offer you the actual microcode though,
17  *      unless Sun confirms they don't mind.
18  */
19
20 static char *version =
21         "soc.c:v1.3 9/Feb/99 Jakub Jelinek (jj@ultra.linux.cz), Jirka Hanika (geo@ff.cuni.cz)\n";
22
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/interrupt.h>
29 #include <linux/ptrace.h>
30 #include <linux/ioport.h>
31 #include <linux/in.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/init.h>
35 #include <linux/bitops.h>
36 #include <asm/io.h>
37 #include <asm/dma.h>
38 #include <linux/errno.h>
39 #include <asm/byteorder.h>
40
41 #include <asm/openprom.h>
42 #include <asm/oplib.h>
43 #include <asm/pgtable.h>
44 #include <asm/irq.h>
45
46 /* #define SOCDEBUG */
47 /* #define HAVE_SOC_UCODE */
48
49 #include "fcp_impl.h"
50 #include "soc.h"
51 #ifdef HAVE_SOC_UCODE
52 #include "soc_asm.h"
53 #endif
54
55 #define soc_printk printk ("soc%d: ", s->soc_no); printk 
56
57 #ifdef SOCDEBUG
58 #define SOD(x)  soc_printk x;
59 #else
60 #define SOD(x)
61 #endif
62
63 #define for_each_soc(s) for (s = socs; s; s = s->next)
64 struct soc *socs = NULL;
65
66 static inline void soc_disable(struct soc *s)
67 {
68         sbus_writel(0, s->regs + IMASK);
69         sbus_writel(SOC_CMD_SOFT_RESET, s->regs + CMD);
70 }
71
72 static inline void soc_enable(struct soc *s)
73 {
74         SOD(("enable %08x\n", s->cfg))
75         sbus_writel(0, s->regs + SAE);
76         sbus_writel(s->cfg, s->regs + CFG);
77         sbus_writel(SOC_CMD_RSP_QALL, s->regs + CMD);
78         SOC_SETIMASK(s, SOC_IMASK_RSP_QALL | SOC_IMASK_SAE);
79         SOD(("imask %08lx %08lx\n", s->imask, sbus_readl(s->regs + IMAK)));
80 }
81
82 static void soc_reset(fc_channel *fc)
83 {
84         soc_port *port = (soc_port *)fc;
85         struct soc *s = port->s;
86         
87         /* FIXME */
88         soc_disable(s);
89         s->req[0].seqno = 1;
90         s->req[1].seqno = 1;
91         s->rsp[0].seqno = 1;
92         s->rsp[1].seqno = 1;
93         s->req[0].in = 0;
94         s->req[1].in = 0;
95         s->rsp[0].in = 0;
96         s->rsp[1].in = 0;
97         s->req[0].out = 0;
98         s->req[1].out = 0;
99         s->rsp[0].out = 0;
100         s->rsp[1].out = 0;
101
102         /* FIXME */
103         soc_enable(s);
104 }
105
106 static inline void soc_solicited (struct soc *s)
107 {
108         fc_hdr fchdr;
109         soc_rsp __iomem *hwrsp;
110         soc_cq_rsp *sw_cq;
111         int token;
112         int status;
113         fc_channel *fc;
114
115         sw_cq = &s->rsp[SOC_SOLICITED_RSP_Q];
116
117         if (sw_cq->pool == NULL)
118                 sw_cq->pool = (soc_req __iomem *)
119                         (s->xram + xram_get_32low ((xram_p)&sw_cq->hw_cq->address));
120         sw_cq->in = xram_get_8 ((xram_p)&sw_cq->hw_cq->in);
121         SOD (("soc_solicited, %d pkts arrived\n", (sw_cq->in-sw_cq->out) & sw_cq->last))
122         for (;;) {
123                 hwrsp = (soc_rsp __iomem *)sw_cq->pool + sw_cq->out;
124                 token = xram_get_32low ((xram_p)&hwrsp->shdr.token);
125                 status = xram_get_32low ((xram_p)&hwrsp->status);
126                 fc = (fc_channel *)(&s->port[(token >> 11) & 1]);
127                 
128                 if (status == SOC_OK) {
129                         fcp_receive_solicited(fc, token >> 12,
130                                               token & ((1 << 11) - 1),
131                                               FC_STATUS_OK, NULL);
132                 } else {
133                         xram_copy_from(&fchdr, (xram_p)&hwrsp->fchdr, sizeof(fchdr));
134                         /* We have intentionally defined FC_STATUS_* constants
135                          * to match SOC_* constants, otherwise we'd have to
136                          * translate status.
137                          */
138                         fcp_receive_solicited(fc, token >> 12,
139                                               token & ((1 << 11) - 1),
140                                               status, &fchdr);
141                 }
142                         
143                 if (++sw_cq->out > sw_cq->last) {
144                         sw_cq->seqno++;
145                         sw_cq->out = 0;
146                 }
147                 
148                 if (sw_cq->out == sw_cq->in) {
149                         sw_cq->in = xram_get_8 ((xram_p)&sw_cq->hw_cq->in);
150                         if (sw_cq->out == sw_cq->in) {
151                                 /* Tell the hardware about it */
152                                 sbus_writel((sw_cq->out << 24) |
153                                             (SOC_CMD_RSP_QALL &
154                                              ~(SOC_CMD_RSP_Q0 << SOC_SOLICITED_RSP_Q)),
155                                             s->regs + CMD);
156
157                                 /* Read it, so that we're sure it has been updated */
158                                 sbus_readl(s->regs + CMD);
159                                 sw_cq->in = xram_get_8 ((xram_p)&sw_cq->hw_cq->in);
160                                 if (sw_cq->out == sw_cq->in)
161                                         break;
162                         }
163                 }
164         }
165 }
166
167 static inline void soc_request (struct soc *s, u32 cmd)
168 {
169         SOC_SETIMASK(s, s->imask & ~(cmd & SOC_CMD_REQ_QALL));
170         SOD(("imask %08lx %08lx\n", s->imask, sbus_readl(s->regs + IMASK)));
171
172         SOD(("Queues available %08x OUT %X %X\n", cmd,
173              xram_get_8((xram_p)&s->req[0].hw_cq->out),
174              xram_get_8((xram_p)&s->req[0].hw_cq->out)))
175         if (s->port[s->curr_port].fc.state != FC_STATE_OFFLINE) {
176                 fcp_queue_empty ((fc_channel *)&(s->port[s->curr_port]));
177                 if (((s->req[1].in + 1) & s->req[1].last) != (s->req[1].out))
178                         fcp_queue_empty ((fc_channel *)&(s->port[1 - s->curr_port]));
179         } else {
180                 fcp_queue_empty ((fc_channel *)&(s->port[1 - s->curr_port]));
181         }
182         if (s->port[1 - s->curr_port].fc.state != FC_STATE_OFFLINE)
183                 s->curr_port ^= 1;
184 }
185
186 static inline void soc_unsolicited (struct soc *s)
187 {
188         soc_rsp __iomem *hwrsp, *hwrspc;
189         soc_cq_rsp *sw_cq;
190         int count;
191         int status;
192         int flags;
193         fc_channel *fc;
194
195         sw_cq = &s->rsp[SOC_UNSOLICITED_RSP_Q];
196         if (sw_cq->pool == NULL)
197                 sw_cq->pool = (soc_req __iomem *)
198                         (s->xram + (xram_get_32low ((xram_p)&sw_cq->hw_cq->address)));
199
200         sw_cq->in = xram_get_8 ((xram_p)&sw_cq->hw_cq->in);
201         SOD (("soc_unsolicited, %d packets arrived\n", (sw_cq->in - sw_cq->out) & sw_cq->last))
202         while (sw_cq->in != sw_cq->out) {
203                 /* ...real work per entry here... */
204                 hwrsp = (soc_rsp __iomem *)sw_cq->pool + sw_cq->out;
205
206                 hwrspc = NULL;
207                 flags = xram_get_16 ((xram_p)&hwrsp->shdr.flags);
208                 count = xram_get_8 ((xram_p)&hwrsp->count);
209                 fc = (fc_channel *)&s->port[flags & SOC_PORT_B];
210                 SOD(("FC %08lx fcp_state_change %08lx\n",
211                      (long)fc, (long)fc->fcp_state_change))
212                 
213                 if (count != 1) {
214                         /* Ugh, continuation entries */
215                         u8 in;
216
217                         if (count != 2) {
218                                 printk("%s: Too many continuations entries %d\n",
219                                        fc->name, count);
220                                 goto update_out;
221                         }
222                         
223                         in = sw_cq->in;
224                         if (in < sw_cq->out) in += sw_cq->last + 1;
225                         if (in < sw_cq->out + 2) {
226                                 /* Ask the hardware if they haven't arrived yet. */
227                                 sbus_writel((sw_cq->out << 24) |
228                                             (SOC_CMD_RSP_QALL &
229                                              ~(SOC_CMD_RSP_Q0 << SOC_UNSOLICITED_RSP_Q)),
230                                             s->regs + CMD);
231
232                                 /* Read it, so that we're sure it has been updated */
233                                 sbus_readl(s->regs + CMD);
234                                 sw_cq->in = xram_get_8 ((xram_p)&sw_cq->hw_cq->in);
235                                 in = sw_cq->in;
236                                 if (in < sw_cq->out)
237                                         in += sw_cq->last + 1;
238                                 if (in < sw_cq->out + 2) /* Nothing came, let us wait */
239                                         return;
240                         }
241                         if (sw_cq->out == sw_cq->last)
242                                 hwrspc = (soc_rsp __iomem *)sw_cq->pool;
243                         else
244                                 hwrspc = hwrsp + 1;
245                 }
246                 
247                 switch (flags & ~SOC_PORT_B) {
248                 case SOC_STATUS:
249                         status = xram_get_32low ((xram_p)&hwrsp->status);
250                         switch (status) {
251                         case SOC_ONLINE:
252                                 SOD(("State change to ONLINE\n"));
253                                 fcp_state_change(fc, FC_STATE_ONLINE);
254                                 break;
255                         case SOC_OFFLINE:
256                                 SOD(("State change to OFFLINE\n"));
257                                 fcp_state_change(fc, FC_STATE_OFFLINE);
258                                 break;
259                         default:
260                                 printk ("%s: Unknown STATUS no %d\n",
261                                         fc->name, status);
262                                 break;
263                         }
264                         break;
265                 case (SOC_UNSOLICITED|SOC_FC_HDR):
266                         {
267                                 int r_ctl = xram_get_8 ((xram_p)&hwrsp->fchdr);
268                                 unsigned len;
269                                 char buf[64];
270                                 
271                                 if ((r_ctl & 0xf0) == R_CTL_EXTENDED_SVC) {
272                                         len = xram_get_32 ((xram_p)&hwrsp->shdr.bytecnt);
273                                         if (len < 4 || !hwrspc) {
274                                                 printk ("%s: Invalid R_CTL %02x "
275                                                         "continuation entries\n",
276                                                         fc->name, r_ctl);
277                                         } else {
278                                                 if (len > 60)
279                                                         len = 60;
280                                                 xram_copy_from (buf, (xram_p)hwrspc,
281                                                                 (len + 3) & ~3);
282                                                 if (*(u32 *)buf == LS_DISPLAY) {
283                                                         int i;
284                                                         
285                                                         for (i = 4; i < len; i++)
286                                                                 if (buf[i] == '\n')
287                                                                         buf[i] = ' ';
288                                                         buf[len] = 0;
289                                                         printk ("%s message: %s\n",
290                                                                 fc->name, buf + 4);
291                                                 } else {
292                                                         printk ("%s: Unknown LS_CMD "
293                                                                 "%02x\n", fc->name,
294                                                                 buf[0]);
295                                                 }
296                                         }
297                                 } else {
298                                         printk ("%s: Unsolicited R_CTL %02x "
299                                                 "not handled\n", fc->name, r_ctl);
300                                 }
301                         }
302                         break;
303                 default:
304                         printk ("%s: Unexpected flags %08x\n", fc->name, flags);
305                         break;
306                 };
307 update_out:
308                 if (++sw_cq->out > sw_cq->last) {
309                         sw_cq->seqno++;
310                         sw_cq->out = 0;
311                 }
312                 
313                 if (hwrspc) {
314                         if (++sw_cq->out > sw_cq->last) {
315                                 sw_cq->seqno++;
316                                 sw_cq->out = 0;
317                         }
318                 }
319                 
320                 if (sw_cq->out == sw_cq->in) {
321                         sw_cq->in = xram_get_8 ((xram_p)&sw_cq->hw_cq->in);
322                         if (sw_cq->out == sw_cq->in) {
323                                 /* Tell the hardware about it */
324                                 sbus_writel((sw_cq->out << 24) |
325                                             (SOC_CMD_RSP_QALL &
326                                              ~(SOC_CMD_RSP_Q0 << SOC_UNSOLICITED_RSP_Q)),
327                                             s->regs + CMD);
328
329                                 /* Read it, so that we're sure it has been updated */
330                                 sbus_readl(s->regs + CMD);
331                                 sw_cq->in = xram_get_8 ((xram_p)&sw_cq->hw_cq->in);
332                         }
333                 }
334         }
335 }
336
337 static irqreturn_t soc_intr(int irq, void *dev_id, struct pt_regs *regs)
338 {
339         u32 cmd;
340         unsigned long flags;
341         register struct soc *s = (struct soc *)dev_id;
342
343         spin_lock_irqsave(&s->lock, flags);
344         cmd = sbus_readl(s->regs + CMD);
345         for (; (cmd = SOC_INTR (s, cmd)); cmd = sbus_readl(s->regs + CMD)) {
346                 if (cmd & SOC_CMD_RSP_Q1) soc_unsolicited (s);
347                 if (cmd & SOC_CMD_RSP_Q0) soc_solicited (s);
348                 if (cmd & SOC_CMD_REQ_QALL) soc_request (s, cmd);
349         }
350         spin_unlock_irqrestore(&s->lock, flags);
351
352         return IRQ_HANDLED;
353 }
354
355 #define TOKEN(proto, port, token) (((proto)<<12)|(token)|(port))
356
357 static int soc_hw_enque (fc_channel *fc, fcp_cmnd *fcmd)
358 {
359         soc_port *port = (soc_port *)fc;
360         struct soc *s = port->s;
361         int qno;
362         soc_cq_req *sw_cq;
363         int cq_next_in;
364         soc_req *request;
365         fc_hdr *fch;
366         int i;
367
368         if (fcmd->proto == TYPE_SCSI_FCP)
369                 qno = 1;
370         else
371                 qno = 0;
372         SOD(("Putting a FCP packet type %d into hw queue %d\n", fcmd->proto, qno))
373         if (s->imask & (SOC_IMASK_REQ_Q0 << qno)) {
374                 SOD(("EIO %08x\n", s->imask))
375                 return -EIO;
376         }
377         sw_cq = s->req + qno;
378         cq_next_in = (sw_cq->in + 1) & sw_cq->last;
379         
380         if (cq_next_in == sw_cq->out &&
381             cq_next_in == (sw_cq->out = xram_get_8((xram_p)&sw_cq->hw_cq->out))) {
382                 SOD(("%d IN %d OUT %d LAST %d\n", qno, sw_cq->in, sw_cq->out, sw_cq->last))
383                 SOC_SETIMASK(s, s->imask | (SOC_IMASK_REQ_Q0 << qno));
384                 SOD(("imask %08lx %08lx\n", s->imask, sbus_readl(s->regs + IMASK)));
385                 /* If queue is full, just say NO */
386                 return -EBUSY;
387         }
388         
389         request = sw_cq->pool + sw_cq->in;
390         fch = &request->fchdr;
391         
392         switch (fcmd->proto) {
393         case TYPE_SCSI_FCP:
394                 request->shdr.token = TOKEN(TYPE_SCSI_FCP, port->mask, fcmd->token); 
395                 request->data[0].base = fc->dma_scsi_cmd + fcmd->token * sizeof(fcp_cmd);
396                 request->data[0].count = sizeof(fcp_cmd);
397                 request->data[1].base = fc->dma_scsi_rsp + fcmd->token * fc->rsp_size;
398                 request->data[1].count = fc->rsp_size;
399                 if (fcmd->data) {
400                         request->shdr.segcnt = 3;
401                         i = fc->scsi_cmd_pool[fcmd->token].fcp_data_len;
402                         request->shdr.bytecnt = i;
403                         request->data[2].base = fcmd->data;
404                         request->data[2].count = i;
405                         request->type =
406                             (fc->scsi_cmd_pool[fcmd->token].fcp_cntl & FCP_CNTL_WRITE) ?
407                                 SOC_CQTYPE_IO_WRITE : SOC_CQTYPE_IO_READ;
408                 } else {
409                         request->shdr.segcnt = 2;
410                         request->shdr.bytecnt = 0;
411                         request->data[2].base = 0;
412                         request->data[2].count = 0;
413                         request->type = SOC_CQTYPE_SIMPLE;
414                 }
415                 FILL_FCHDR_RCTL_DID(fch, R_CTL_COMMAND, fc->did);
416                 FILL_FCHDR_SID(fch, fc->sid);
417                 FILL_FCHDR_TYPE_FCTL(fch, TYPE_SCSI_FCP,
418                                      F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
419                 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
420                 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
421                 fch->param = 0;
422                 request->shdr.flags = port->flags;
423                 request->shdr.class = 2;
424                 break;
425                 
426         case PROTO_OFFLINE:
427                 memset (request, 0, sizeof(*request));
428                 request->shdr.token = TOKEN(PROTO_OFFLINE, port->mask, fcmd->token); 
429                 request->type = SOC_CQTYPE_OFFLINE;
430                 FILL_FCHDR_RCTL_DID(fch, R_CTL_COMMAND, fc->did);
431                 FILL_FCHDR_SID(fch, fc->sid);
432                 FILL_FCHDR_TYPE_FCTL(fch, TYPE_SCSI_FCP,
433                                      F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
434                 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
435                 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
436                 request->shdr.flags = port->flags;
437                 break;
438                 
439         case PROTO_REPORT_AL_MAP:
440                 /* SOC only supports Point-to-Point topology, no FC-AL, sorry... */
441                 return -ENOSYS;
442
443         default: 
444                 request->shdr.token = TOKEN(fcmd->proto, port->mask, fcmd->token);
445                 request->shdr.class = 2;
446                 request->shdr.flags = port->flags;
447                 memcpy (fch, &fcmd->fch, sizeof(fc_hdr));
448                 request->data[0].count = fcmd->cmdlen;
449                 request->data[1].count = fcmd->rsplen;
450                 request->type = fcmd->class;
451                 switch (fcmd->class) {
452                 case FC_CLASS_OUTBOUND:
453                         request->data[0].base = fcmd->cmd;
454                         request->data[0].count = fcmd->cmdlen;
455                         request->type = SOC_CQTYPE_OUTBOUND;
456                         request->shdr.bytecnt = fcmd->cmdlen;
457                         request->shdr.segcnt = 1;
458                         break;
459                 case FC_CLASS_INBOUND:
460                         request->data[0].base = fcmd->rsp;
461                         request->data[0].count = fcmd->rsplen;
462                         request->type = SOC_CQTYPE_INBOUND;
463                         request->shdr.bytecnt = 0;
464                         request->shdr.segcnt = 1;
465                         break;
466                 case FC_CLASS_SIMPLE:
467                         request->data[0].base = fcmd->cmd;
468                         request->data[1].base = fcmd->rsp;
469                         request->data[0].count = fcmd->cmdlen;
470                         request->data[1].count = fcmd->rsplen;
471                         request->type = SOC_CQTYPE_SIMPLE;
472                         request->shdr.bytecnt = fcmd->cmdlen;
473                         request->shdr.segcnt = 2;
474                         break;
475                 case FC_CLASS_IO_READ:
476                 case FC_CLASS_IO_WRITE:
477                         request->data[0].base = fcmd->cmd;
478                         request->data[1].base = fcmd->rsp;
479                         request->data[0].count = fcmd->cmdlen;
480                         request->data[1].count = fcmd->rsplen;
481                         request->type =
482                               (fcmd->class == FC_CLASS_IO_READ) ?
483                                 SOC_CQTYPE_IO_READ : SOC_CQTYPE_IO_WRITE;
484                         if (fcmd->data) {
485                                 request->data[2].base = fcmd->data;
486                                 request->data[2].count = fcmd->datalen;
487                                 request->shdr.bytecnt = fcmd->datalen;
488                                 request->shdr.segcnt = 3;
489                         } else {
490                                 request->shdr.bytecnt = 0;
491                                 request->shdr.segcnt = 2;
492                         }
493                         break;
494                 };
495                 break;
496         };
497
498         request->count = 1;
499         request->flags = 0;
500         request->seqno = sw_cq->seqno;
501         
502         /* And now tell the SOC about it */
503
504         if (++sw_cq->in > sw_cq->last) {
505                 sw_cq->in = 0;
506                 sw_cq->seqno++;
507         }
508         
509         SOD(("Putting %08x into cmd\n",
510              SOC_CMD_RSP_QALL | (sw_cq->in << 24) | (SOC_CMD_REQ_Q0 << qno)))
511         
512         sbus_writel(SOC_CMD_RSP_QALL | (sw_cq->in << 24) | (SOC_CMD_REQ_Q0 << qno),
513                     s->regs + CMD);
514
515         /* Read so that command is completed. */        
516         sbus_readl(s->regs + CMD);
517         
518         return 0;
519 }
520
521 static inline void soc_download_fw(struct soc *s)
522 {
523 #ifdef HAVE_SOC_UCODE
524         xram_copy_to (s->xram, soc_ucode, sizeof(soc_ucode));
525         xram_bzero (s->xram + sizeof(soc_ucode), 32768 - sizeof(soc_ucode));
526 #endif
527 }
528
529 /* Check for what the best SBUS burst we can use happens
530  * to be on this machine.
531  */
532 static inline void soc_init_bursts(struct soc *s, struct sbus_dev *sdev)
533 {
534         int bsizes, bsizes_more;
535
536         bsizes = (prom_getintdefault(sdev->prom_node,"burst-sizes",0xff) & 0xff);
537         bsizes_more = (prom_getintdefault(sdev->bus->prom_node, "burst-sizes", 0xff) & 0xff);
538         bsizes &= bsizes_more;
539         if ((bsizes & 0x7f) == 0x7f)
540                 s->cfg = SOC_CFG_BURST_64;
541         else if ((bsizes & 0x3f) == 0x3f) 
542                 s->cfg = SOC_CFG_BURST_32;
543         else if ((bsizes & 0x1f) == 0x1f)
544                 s->cfg = SOC_CFG_BURST_16;
545         else
546                 s->cfg = SOC_CFG_BURST_4;
547 }
548
549 static inline void soc_init(struct sbus_dev *sdev, int no)
550 {
551         unsigned char tmp[60];
552         int propl;
553         struct soc *s;
554         static int version_printed = 0;
555         soc_hw_cq cq[8];
556         int size, i;
557         int irq;
558         
559         s = kmalloc (sizeof (struct soc), GFP_KERNEL);
560         if (s == NULL)
561                 return;
562         memset (s, 0, sizeof(struct soc));
563         spin_lock_init(&s->lock);
564         s->soc_no = no;
565
566         SOD(("socs %08lx soc_intr %08lx soc_hw_enque %08x\n",
567              (long)socs, (long)soc_intr, (long)soc_hw_enque))   
568         if (version_printed++ == 0)
569                 printk (version);
570
571         s->port[0].fc.module = THIS_MODULE;
572         s->port[1].fc.module = THIS_MODULE;
573
574         s->next = socs;
575         socs = s;
576         s->port[0].fc.dev = sdev;
577         s->port[1].fc.dev = sdev;
578         s->port[0].s = s;
579         s->port[1].s = s;
580
581         s->port[0].fc.next = &s->port[1].fc;
582
583         /* World Wide Name of SOC */
584         propl = prom_getproperty (sdev->prom_node, "soc-wwn", tmp, sizeof(tmp));
585         if (propl != sizeof (fc_wwn)) {
586                 s->wwn.naaid = NAAID_IEEE;
587                 s->wwn.lo = 0x12345678;
588         } else
589                 memcpy (&s->wwn, tmp, sizeof (fc_wwn));
590                 
591         propl = prom_getproperty (sdev->prom_node, "port-wwns", tmp, sizeof(tmp));
592         if (propl != 2 * sizeof (fc_wwn)) {
593                 s->port[0].fc.wwn_nport.naaid = NAAID_IEEE_EXT;
594                 s->port[0].fc.wwn_nport.hi = s->wwn.hi;
595                 s->port[0].fc.wwn_nport.lo = s->wwn.lo;
596                 s->port[1].fc.wwn_nport.naaid = NAAID_IEEE_EXT;
597                 s->port[1].fc.wwn_nport.nportid = 1;
598                 s->port[1].fc.wwn_nport.hi = s->wwn.hi;
599                 s->port[1].fc.wwn_nport.lo = s->wwn.lo;
600         } else {
601                 memcpy (&s->port[0].fc.wwn_nport, tmp, sizeof (fc_wwn));
602                 memcpy (&s->port[1].fc.wwn_nport, tmp + sizeof (fc_wwn), sizeof (fc_wwn));
603         }
604         memcpy (&s->port[0].fc.wwn_node, &s->wwn, sizeof (fc_wwn));
605         memcpy (&s->port[1].fc.wwn_node, &s->wwn, sizeof (fc_wwn));
606         SOD(("Got wwns %08x%08x ports %08x%08x and %08x%08x\n", 
607              *(u32 *)&s->port[0].fc.wwn_nport, s->port[0].fc.wwn_nport.lo,
608              *(u32 *)&s->port[0].fc.wwn_nport, s->port[0].fc.wwn_nport.lo,
609              *(u32 *)&s->port[1].fc.wwn_nport, s->port[1].fc.wwn_nport.lo))
610                 
611         s->port[0].fc.sid = 1;
612         s->port[1].fc.sid = 17;
613         s->port[0].fc.did = 2;
614         s->port[1].fc.did = 18;
615         
616         s->port[0].fc.reset = soc_reset;
617         s->port[1].fc.reset = soc_reset;
618         
619         if (sdev->num_registers == 1) {
620                 /* Probably SunFire onboard SOC */
621                 s->xram = sbus_ioremap(&sdev->resource[0], 0,
622                                        0x10000UL, "soc xram");
623                 s->regs = sbus_ioremap(&sdev->resource[0], 0x10000UL,
624                                        0x10UL, "soc regs");
625         } else {
626                 /* Probably SOC sbus card */
627                 s->xram = sbus_ioremap(&sdev->resource[1], 0,
628                                        sdev->reg_addrs[1].reg_size, "soc xram");
629                 s->regs = sbus_ioremap(&sdev->resource[2], 0,
630                                        sdev->reg_addrs[2].reg_size, "soc regs");
631         }
632         
633         soc_init_bursts(s, sdev);
634         
635         SOD(("Disabling SOC\n"))
636         
637         soc_disable (s);
638         
639         irq = sdev->irqs[0];
640
641         if (request_irq (irq, soc_intr, SA_SHIRQ, "SOC", (void *)s)) {
642                 soc_printk ("Cannot order irq %d to go\n", irq);
643                 socs = s->next;
644                 return;
645         }
646
647         SOD(("SOC uses IRQ%s\n", __irq_itoa(irq)))
648         
649         s->port[0].fc.irq = irq;
650         s->port[1].fc.irq = irq;
651         
652         sprintf (s->port[0].fc.name, "soc%d port A", no);
653         sprintf (s->port[1].fc.name, "soc%d port B", no);
654         s->port[0].flags = SOC_FC_HDR | SOC_PORT_A;
655         s->port[1].flags = SOC_FC_HDR | SOC_PORT_B;
656         s->port[1].mask = (1 << 11);
657         
658         s->port[0].fc.hw_enque = soc_hw_enque;
659         s->port[1].fc.hw_enque = soc_hw_enque;
660         
661         soc_download_fw (s);
662         
663         SOD(("Downloaded firmware\n"))
664
665         /* Now setup xram circular queues */
666         memset (cq, 0, sizeof(cq));
667         
668         size = (SOC_CQ_REQ0_SIZE + SOC_CQ_REQ1_SIZE) * sizeof(soc_req);
669         s->req_cpu = sbus_alloc_consistent(sdev, size, &s->req_dvma);
670         s->req[0].pool = s->req_cpu;
671         cq[0].address = s->req_dvma;
672         s->req[1].pool = s->req[0].pool + SOC_CQ_REQ0_SIZE;
673         
674         s->req[0].hw_cq = (soc_hw_cq __iomem *)(s->xram + SOC_CQ_REQ_OFFSET);
675         s->req[1].hw_cq = (soc_hw_cq __iomem *)(s->xram + SOC_CQ_REQ_OFFSET + sizeof(soc_hw_cq));
676         s->rsp[0].hw_cq = (soc_hw_cq __iomem *)(s->xram + SOC_CQ_RSP_OFFSET);
677         s->rsp[1].hw_cq = (soc_hw_cq __iomem *)(s->xram + SOC_CQ_RSP_OFFSET + sizeof(soc_hw_cq));
678         
679         cq[1].address = cq[0].address + (SOC_CQ_REQ0_SIZE * sizeof(soc_req));
680         cq[4].address = 1;
681         cq[5].address = 1;
682         cq[0].last = SOC_CQ_REQ0_SIZE - 1;
683         cq[1].last = SOC_CQ_REQ1_SIZE - 1;
684         cq[4].last = SOC_CQ_RSP0_SIZE - 1;
685         cq[5].last = SOC_CQ_RSP1_SIZE - 1;
686         for (i = 0; i < 8; i++)
687                 cq[i].seqno = 1;
688         
689         s->req[0].last = SOC_CQ_REQ0_SIZE - 1;
690         s->req[1].last = SOC_CQ_REQ1_SIZE - 1;
691         s->rsp[0].last = SOC_CQ_RSP0_SIZE - 1;
692         s->rsp[1].last = SOC_CQ_RSP1_SIZE - 1;
693         
694         s->req[0].seqno = 1;
695         s->req[1].seqno = 1;
696         s->rsp[0].seqno = 1;
697         s->rsp[1].seqno = 1;
698
699         xram_copy_to (s->xram + SOC_CQ_REQ_OFFSET, cq, sizeof(cq));
700         
701         /* Make our sw copy of SOC service parameters */
702         xram_copy_from (s->serv_params, s->xram + 0x140, sizeof (s->serv_params));
703         
704         s->port[0].fc.common_svc = (common_svc_parm *)s->serv_params;
705         s->port[0].fc.class_svcs = (svc_parm *)(s->serv_params + 0x20);
706         s->port[1].fc.common_svc = (common_svc_parm *)&s->serv_params;
707         s->port[1].fc.class_svcs = (svc_parm *)(s->serv_params + 0x20);
708         
709         soc_enable (s);
710         
711         SOD(("Enabled SOC\n"))
712 }
713
714 static int __init soc_probe(void)
715 {
716         struct sbus_bus *sbus;
717         struct sbus_dev *sdev = NULL;
718         struct soc *s;
719         int cards = 0;
720
721         for_each_sbus(sbus) {
722                 for_each_sbusdev(sdev, sbus) {
723                         if(!strcmp(sdev->prom_name, "SUNW,soc")) {
724                                 soc_init(sdev, cards);
725                                 cards++;
726                         }
727                 }
728         }
729         if (!cards) return -EIO;
730
731         for_each_soc(s)
732                 if (s->next)
733                         s->port[1].fc.next = &s->next->port[0].fc;
734         fcp_init (&socs->port[0].fc);
735         return 0;
736 }
737
738 static void __exit soc_cleanup(void)
739 {
740         struct soc *s;
741         int irq;
742         struct sbus_dev *sdev;
743         
744         for_each_soc(s) {
745                 irq = s->port[0].fc.irq;
746                 free_irq (irq, s);
747
748                 fcp_release(&(s->port[0].fc), 2);
749
750                 sdev = s->port[0].fc.dev;
751                 if (sdev->num_registers == 1) {
752                         sbus_iounmap(s->xram, 0x10000UL);
753                         sbus_iounmap(s->regs, 0x10UL);
754                 } else {
755                         sbus_iounmap(s->xram, sdev->reg_addrs[1].reg_size);
756                         sbus_iounmap(s->regs, sdev->reg_addrs[2].reg_size);
757                 }
758                 sbus_free_consistent(sdev,
759                                      (SOC_CQ_REQ0_SIZE+SOC_CQ_REQ1_SIZE)*sizeof(soc_req),
760                                      s->req_cpu, s->req_dvma);
761         }
762 }
763
764 module_init(soc_probe);
765 module_exit(soc_cleanup);
766 MODULE_LICENSE("GPL");