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