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/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)
60struct socal *socals = NULL;
61
62static 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
72static 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
84static 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
94static 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
100static 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
110static void socal_reset(fc_channel *fc)
111{
112	socal_port *port = (socal_port *)fc;
113	struct socal *s = port->s;
114
115	socal_disable(s);
116	s->req[0].seqno = 1;
117	s->req[1].seqno = 1;
118	s->rsp[0].seqno = 1;
119	s->rsp[1].seqno = 1;
120	s->req[0].in = 0;
121	s->req[1].in = 0;
122	s->rsp[0].in = 0;
123	s->rsp[1].in = 0;
124	s->req[0].out = 0;
125	s->req[1].out = 0;
126	s->rsp[0].out = 0;
127	s->rsp[1].out = 0;
128
129	socal_enable(s);
130}
131
132static inline void socal_solicited(struct socal *s, unsigned long qno)
133{
134	socal_rsp *hwrsp;
135	socal_cq *sw_cq;
136	int token;
137	int status;
138	fc_channel *fc;
139
140	sw_cq = &s->rsp[qno];
141
142	/* Finally an improvement against old SOC :) */
143	sw_cq->in = sbus_readb(s->regs + RESP + qno);
144	SOD (("socal_solicited, %d packets arrived\n",
145	      (sw_cq->in - sw_cq->out) & sw_cq->last))
146	for (;;) {
147		hwrsp = (socal_rsp *)sw_cq->pool + sw_cq->out;
148		SOD(("hwrsp %p out %d\n", hwrsp, sw_cq->out))
149
150
151		token = hwrsp->shdr.token;
152		status = hwrsp->status;
153		fc = (fc_channel *)(&s->port[(token >> 11) & 1]);
154
155		SOD(("Solicited token %08x status %08x\n", token, status))
156		if (status == SOCAL_OK) {
157			fcp_receive_solicited(fc, token >> 12,
158					      token & ((1 << 11) - 1),
159					      FC_STATUS_OK, NULL);
160		} else {
161			/* We have intentionally defined FC_STATUS_* constants
162			 * to match SOCAL_* constants, otherwise we'd have to
163			 * translate status.
164			 */
165			fcp_receive_solicited(fc, token >> 12,
166					      token & ((1 << 11) - 1), status, &hwrsp->fchdr);
167		}
168
169		if (++sw_cq->out > sw_cq->last) {
170			sw_cq->seqno++;
171			sw_cq->out = 0;
172		}
173
174		if (sw_cq->out == sw_cq->in) {
175			sw_cq->in = sbus_readb(s->regs + RESP + qno);
176			if (sw_cq->out == sw_cq->in) {
177				/* Tell the hardware about it */
178				sbus_writel((sw_cq->out << 24) |
179					    (SOCAL_CMD_RSP_QALL &
180					     ~(SOCAL_CMD_RSP_Q0 << qno)),
181					    s->regs + CMD);
182
183				/* Read it, so that we're sure it has been updated */
184				sbus_readl(s->regs + CMD);
185				sw_cq->in = sbus_readb(s->regs + RESP + qno);
186				if (sw_cq->out == sw_cq->in)
187					break;
188			}
189		}
190	}
191}
192
193static inline void socal_request (struct socal *s, u32 cmd)
194{
195	SOCAL_SETIMASK(s, s->imask & ~(cmd & SOCAL_CMD_REQ_QALL));
196	SOD(("imask %08x %08x\n", s->imask, sbus_readl(s->regs + IMASK)));
197
198	SOD(("Queues available %08x OUT %X\n", cmd, s->regs->reqpr[0]))
199	if (s->port[s->curr_port].fc.state != FC_STATE_OFFLINE) {
200		fcp_queue_empty ((fc_channel *)&(s->port[s->curr_port]));
201		if (((s->req[1].in + 1) & s->req[1].last) != (s->req[1].out))
202			fcp_queue_empty ((fc_channel *)&(s->port[1 - s->curr_port]));
203	} else {
204		fcp_queue_empty ((fc_channel *)&(s->port[1 - s->curr_port]));
205	}
206	if (s->port[1 - s->curr_port].fc.state != FC_STATE_OFFLINE)
207		s->curr_port ^= 1;
208}
209
210static inline void socal_unsolicited (struct socal *s, unsigned long qno)
211{
212	socal_rsp *hwrsp, *hwrspc;
213	socal_cq *sw_cq;
214	int count;
215	int status;
216	int flags;
217	fc_channel *fc;
218
219	sw_cq = &s->rsp[qno];
220
221	sw_cq->in = sbus_readb(s->regs + RESP + qno);
222	SOD (("socal_unsolicited, %d packets arrived, in %d\n",
223	      (sw_cq->in - sw_cq->out) & sw_cq->last, sw_cq->in))
224	while (sw_cq->in != sw_cq->out) {
225		/* ...real work per entry here... */
226		hwrsp = (socal_rsp *)sw_cq->pool + sw_cq->out;
227		SOD(("hwrsp %p out %d\n", hwrsp, sw_cq->out))
228
229
230		hwrspc = NULL;
231		flags = hwrsp->shdr.flags;
232		count = hwrsp->count;
233		fc = (fc_channel *)&s->port[flags & SOCAL_PORT_B];
234		SOD(("FC %08lx\n", (long)fc))
235
236		if (count != 1) {
237			/* Ugh, continuation entries */
238			u8 in;
239
240			if (count != 2) {
241				printk("%s: Too many continuations entries %d\n",
242				       fc->name, count);
243				goto update_out;
244			}
245
246			in = sw_cq->in;
247			if (in < sw_cq->out)
248				in += sw_cq->last + 1;
249			if (in < sw_cq->out + 2) {
250				/* Ask the hardware if they haven't arrived yet. */
251				sbus_writel((sw_cq->out << 24) |
252					    (SOCAL_CMD_RSP_QALL &
253					     ~(SOCAL_CMD_RSP_Q0 << qno)),
254					    s->regs + CMD);
255
256				/* Read it, so that we're sure it has been updated */
257				sbus_readl(s->regs + CMD);
258				sw_cq->in = sbus_readb(s->regs + RESP + qno);
259				in = sw_cq->in;
260				if (in < sw_cq->out)
261					in += sw_cq->last + 1;
262				if (in < sw_cq->out + 2) /* Nothing came, let us wait */
263					return;
264			}
265			if (sw_cq->out == sw_cq->last)
266				hwrspc = (socal_rsp *)sw_cq->pool;
267			else
268				hwrspc = hwrsp + 1;
269		}
270
271		switch (flags & ~SOCAL_PORT_B) {
272		case SOCAL_STATUS:
273			status = hwrsp->status;
274			switch (status) {
275			case SOCAL_ONLINE:
276				SOD(("State change to ONLINE\n"));
277				fcp_state_change(fc, FC_STATE_ONLINE);
278				break;
279			case SOCAL_ONLINE_LOOP:
280				SOD(("State change to ONLINE_LOOP\n"));
281				fcp_state_change(fc, FC_STATE_ONLINE);
282				break;
283			case SOCAL_OFFLINE:
284				SOD(("State change to OFFLINE\n"));
285				fcp_state_change(fc, FC_STATE_OFFLINE);
286				break;
287			default:
288				printk ("%s: Unknown STATUS no %d\n",
289					fc->name, status);
290				break;
291			};
292
293			break;
294		case (SOCAL_UNSOLICITED|SOCAL_FC_HDR):
295			{
296				int r_ctl = *((u8 *)&hwrsp->fchdr);
297				unsigned len;
298
299				if ((r_ctl & 0xf0) == R_CTL_EXTENDED_SVC) {
300					len = hwrsp->shdr.bytecnt;
301					if (len < 4 || !hwrspc) {
302						printk ("%s: Invalid R_CTL %02x "
303							"continuation entries\n",
304							fc->name, r_ctl);
305					} else {
306						if (len > 60)
307							len = 60;
308						if (*(u32 *)hwrspc == LS_DISPLAY) {
309							int i;
310
311							for (i = 4; i < len; i++)
312								if (((u8 *)hwrspc)[i] == '\n')
313									((u8 *)hwrspc)[i] = ' ';
314							((u8 *)hwrspc)[len] = 0;
315							printk ("%s message: %s\n",
316								fc->name, ((u8 *)hwrspc) + 4);
317						} else {
318							printk ("%s: Unknown LS_CMD "
319								"%08x\n", fc->name,
320								*(u32 *)hwrspc);
321						}
322					}
323				} else {
324					printk ("%s: Unsolicited R_CTL %02x "
325						"not handled\n", fc->name, r_ctl);
326				}
327			}
328			break;
329		default:
330			printk ("%s: Unexpected flags %08x\n", fc->name, flags);
331			break;
332		};
333update_out:
334		if (++sw_cq->out > sw_cq->last) {
335			sw_cq->seqno++;
336			sw_cq->out = 0;
337		}
338
339		if (hwrspc) {
340			if (++sw_cq->out > sw_cq->last) {
341				sw_cq->seqno++;
342				sw_cq->out = 0;
343			}
344		}
345
346		if (sw_cq->out == sw_cq->in) {
347			sw_cq->in = sbus_readb(s->regs + RESP + qno);
348			if (sw_cq->out == sw_cq->in) {
349				/* Tell the hardware about it */
350				sbus_writel((sw_cq->out << 24) |
351					    (SOCAL_CMD_RSP_QALL &
352					     ~(SOCAL_CMD_RSP_Q0 << qno)),
353					    s->regs + CMD);
354
355				/* Read it, so that we're sure it has been updated */
356				sbus_readl(s->regs + CMD);
357				sw_cq->in = sbus_readb(s->regs + RESP + qno);
358			}
359		}
360	}
361}
362
363static irqreturn_t socal_intr(int irq, void *dev_id)
364{
365	u32 cmd;
366	unsigned long flags;
367	register struct socal *s = (struct socal *)dev_id;
368
369	spin_lock_irqsave(&s->lock, flags);
370	cmd = sbus_readl(s->regs + CMD);
371	for (; (cmd = SOCAL_INTR (s, cmd)); cmd = sbus_readl(s->regs + CMD)) {
372#ifdef SOCALDEBUG
373		static int cnt = 0;
374		if (cnt++ < 50)
375			printk("soc_intr %08x\n", cmd);
376#endif
377		if (cmd & SOCAL_CMD_RSP_Q2)
378			socal_unsolicited (s, SOCAL_UNSOLICITED_RSP_Q);
379		if (cmd & SOCAL_CMD_RSP_Q1)
380			socal_unsolicited (s, SOCAL_SOLICITED_BAD_RSP_Q);
381		if (cmd & SOCAL_CMD_RSP_Q0)
382			socal_solicited (s, SOCAL_SOLICITED_RSP_Q);
383		if (cmd & SOCAL_CMD_REQ_QALL)
384			socal_request (s, cmd);
385	}
386	spin_unlock_irqrestore(&s->lock, flags);
387
388	return IRQ_HANDLED;
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 = kzalloc (sizeof (struct socal), GFP_KERNEL);
625	if (!s) return;
626	spin_lock_init(&s->lock);
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, IRQF_SHARED, "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 %d\n", 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 __iomem *)(s->xram + SOCAL_CQ_REQ_OFFSET);
759	s->req[1].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_REQ_OFFSET + sizeof(socal_hw_cq));
760	s->rsp[0].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_RSP_OFFSET);
761	s->rsp[1].hw_cq = (socal_hw_cq __iomem *)(s->xram + SOCAL_CQ_RSP_OFFSET + sizeof(socal_hw_cq));
762	s->rsp[2].hw_cq = (socal_hw_cq __iomem *)(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 = NULL;
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
859module_init(socal_probe);
860module_exit(socal_cleanup);
861MODULE_LICENSE("GPL");
862