1/* fc.c: Generic Fibre Channel and FC4 SCSI driver.
2 *
3 * Copyright (C) 1997,1998,1999 Jakub Jelinek (jj@ultra.linux.cz)
4 * Copyright (C) 1997,1998 Jirka Hanika (geo@ff.cuni.cz)
5 *
6 * There are two kinds of Fibre Channel adapters used in Linux. Either
7 * the adapter is "smart" and does all FC bookkeeping by itself and
8 * just presents a standard SCSI interface to the operating system
9 * (that's e.g. the case with Qlogic FC cards), or leaves most of the FC
10 * bookkeeping to the OS (e.g. soc, socal). Drivers for the former adapters
11 * will look like normal SCSI drivers (with the exception of max_id will be
12 * usually 127), the latter on the other side allows SCSI, IP over FC and other
13 * protocols. This driver tree is for the latter adapters.
14 *
15 * This file should support both Point-to-Point and Arbitrated Loop topologies.
16 *
17 * Sources:
18 *	Fibre Channel Physical & Signaling Interface (FC-PH), dpANS, 1994
19 *	dpANS Fibre Channel Protocol for SCSI (X3.269-199X), Rev. 012, 1995
20 *	Fibre Channel Arbitrated Loop (FC-AL), Rev. 4.5, 1995
21 *	Fibre Channel Private Loop SCSI Direct Attach (FC-PLDA), Rev. 2.1, 1997
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/jiffies.h>
27#include <linux/types.h>
28#include <linux/fcntl.h>
29#include <linux/interrupt.h>
30#include <linux/ptrace.h>
31#include <linux/ioport.h>
32#include <linux/in.h>
33#include <linux/slab.h>
34#include <linux/string.h>
35#include <linux/init.h>
36
37#include <asm/pgtable.h>
38#include <asm/irq.h>
39#include <asm/semaphore.h>
40#include "fcp_impl.h"
41#include <scsi/scsi_host.h>
42
43/* #define FCDEBUG */
44
45#define fc_printk printk ("%s: ", fc->name); printk
46
47#ifdef FCDEBUG
48#define FCD(x)  fc_printk x;
49#define FCND(x)	printk ("FC: "); printk x;
50#else
51#define FCD(x)
52#define FCND(x)
53#endif
54
55#ifdef __sparc__
56#define dma_alloc_consistent(d,s,p) sbus_alloc_consistent(d,s,p)
57#define dma_free_consistent(d,s,v,h) sbus_free_consistent(d,s,v,h)
58#define dma_map_single(d,v,s,dir) sbus_map_single(d,v,s,dir)
59#define dma_unmap_single(d,h,s,dir) sbus_unmap_single(d,h,s,dir)
60#define dma_map_sg(d,s,n,dir) sbus_map_sg(d,s,n,dir)
61#define dma_unmap_sg(d,s,n,dir) sbus_unmap_sg(d,s,n,dir)
62#else
63#define dma_alloc_consistent(d,s,p) pci_alloc_consistent(d,s,p)
64#define dma_free_consistent(d,s,v,h) pci_free_consistent(d,s,v,h)
65#define dma_map_single(d,v,s,dir) pci_map_single(d,v,s,dir)
66#define dma_unmap_single(d,h,s,dir) pci_unmap_single(d,h,s,dir)
67#define dma_map_sg(d,s,n,dir) pci_map_sg(d,s,n,dir)
68#define dma_unmap_sg(d,s,n,dir) pci_unmap_sg(d,s,n,dir)
69#endif
70
71#define FCP_CMND(SCpnt) ((fcp_cmnd *)&(SCpnt->SCp))
72#define FC_SCMND(SCpnt) ((fc_channel *)(SCpnt->device->host->hostdata[0]))
73#define SC_FCMND(fcmnd)	((struct scsi_cmnd *)((long)fcmnd - (long)&(((struct scsi_cmnd *)0)->SCp)))
74
75static int fcp_scsi_queue_it(fc_channel *, struct scsi_cmnd *, fcp_cmnd *, int);
76void fcp_queue_empty(fc_channel *);
77
78static void fcp_scsi_insert_queue (fc_channel *fc, fcp_cmnd *fcmd)
79{
80	if (!fc->scsi_que) {
81		fc->scsi_que = fcmd;
82		fcmd->next = fcmd;
83		fcmd->prev = fcmd;
84	} else {
85		fc->scsi_que->prev->next = fcmd;
86		fcmd->prev = fc->scsi_que->prev;
87		fc->scsi_que->prev = fcmd;
88		fcmd->next = fc->scsi_que;
89	}
90}
91
92static void fcp_scsi_remove_queue (fc_channel *fc, fcp_cmnd *fcmd)
93{
94	if (fcmd == fcmd->next) {
95		fc->scsi_que = NULL;
96		return;
97	}
98	if (fcmd == fc->scsi_que)
99		fc->scsi_que = fcmd->next;
100	fcmd->prev->next = fcmd->next;
101	fcmd->next->prev = fcmd->prev;
102}
103
104fc_channel *fc_channels = NULL;
105
106#define LSMAGIC	620829043
107typedef struct {
108	/* Must be first */
109	struct semaphore sem;
110	int magic;
111	int count;
112	logi *logi;
113	fcp_cmnd *fcmds;
114	atomic_t todo;
115	struct timer_list timer;
116	unsigned char grace[0];
117} ls;
118
119#define LSOMAGIC 654907799
120typedef struct {
121	/* Must be first */
122	struct semaphore sem;
123	int magic;
124	int count;
125	fcp_cmnd *fcmds;
126	atomic_t todo;
127	struct timer_list timer;
128} lso;
129
130#define LSEMAGIC 84482456
131typedef struct {
132	/* Must be first */
133	struct semaphore sem;
134	int magic;
135	int status;
136	struct timer_list timer;
137} lse;
138
139static void fcp_login_timeout(unsigned long data)
140{
141	ls *l = (ls *)data;
142	FCND(("Login timeout\n"))
143	up(&l->sem);
144}
145
146static void fcp_login_done(fc_channel *fc, int i, int status)
147{
148	fcp_cmnd *fcmd;
149	logi *plogi;
150	fc_hdr *fch;
151	ls *l = (ls *)fc->ls;
152
153	FCD(("Login done %d %d\n", i, status))
154	if (i < l->count) {
155		if (fc->state == FC_STATE_FPORT_OK) {
156			FCD(("Additional FPORT_OK received with status %d\n", status))
157			return;
158		}
159		switch (status) {
160		case FC_STATUS_OK: /* Oh, we found a fabric */
161		case FC_STATUS_P_RJT: /* Oh, we haven't found any */
162			fc->state = FC_STATE_FPORT_OK;
163			fcmd = l->fcmds + i;
164			plogi = l->logi + 3 * i;
165			dma_unmap_single (fc->dev, fcmd->cmd, 3 * sizeof(logi),
166					  DMA_BIDIRECTIONAL);
167			plogi->code = LS_PLOGI;
168			memcpy (&plogi->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
169			memcpy (&plogi->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
170			memcpy (&plogi->common, fc->common_svc, sizeof(common_svc_parm));
171			memcpy (&plogi->class1, fc->class_svcs, 3*sizeof(svc_parm));
172			fch = &fcmd->fch;
173			fcmd->token += l->count;
174			FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, fc->did);
175			FILL_FCHDR_SID(fch, fc->sid);
176#ifdef FCDEBUG
177			{
178				int i;
179				unsigned *x = (unsigned *)plogi;
180				printk ("logi: ");
181				for (i = 0; i < 21; i++)
182					printk ("%08x ", x[i]);
183				printk ("\n");
184			}
185#endif
186			fcmd->cmd = dma_map_single (fc->dev, plogi, 3 * sizeof(logi),
187						    DMA_BIDIRECTIONAL);
188			fcmd->rsp = fcmd->cmd + 2 * sizeof(logi);
189			if (fc->hw_enque (fc, fcmd))
190				printk ("FC: Cannot enque PLOGI packet on %s\n", fc->name);
191			break;
192		case FC_STATUS_ERR_OFFLINE:
193			fc->state = FC_STATE_MAYBEOFFLINE;
194			FCD (("FC is offline %d\n", l->grace[i]))
195			break;
196		default:
197			printk ("FLOGI failed for %s with status %d\n", fc->name, status);
198			/* Do some sort of error recovery here */
199			break;
200		}
201	} else {
202		i -= l->count;
203		if (fc->state != FC_STATE_FPORT_OK) {
204			FCD(("Unexpected N-PORT rsp received"))
205			return;
206		}
207		switch (status) {
208		case FC_STATUS_OK:
209			plogi = l->logi + 3 * i;
210			dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
211					  DMA_BIDIRECTIONAL);
212			if (!fc->wwn_dest.lo && !fc->wwn_dest.hi) {
213				memcpy (&fc->wwn_dest, &plogi[1].node_wwn, sizeof(fc_wwn));
214				FCD(("Dest WWN %08x%08x\n", *(u32 *)&fc->wwn_dest, fc->wwn_dest.lo))
215			} else if (fc->wwn_dest.lo != plogi[1].node_wwn.lo ||
216				   fc->wwn_dest.hi != plogi[1].node_wwn.hi) {
217				printk ("%s: mismatch in wwns. Got %08x%08x, expected %08x%08x\n",
218					fc->name,
219					*(u32 *)&plogi[1].node_wwn, plogi[1].node_wwn.lo,
220					*(u32 *)&fc->wwn_dest, fc->wwn_dest.lo);
221			}
222			fc->state = FC_STATE_ONLINE;
223			printk ("%s: ONLINE\n", fc->name);
224			if (atomic_dec_and_test (&l->todo))
225				up(&l->sem);
226			break;
227		case FC_STATUS_ERR_OFFLINE:
228			fc->state = FC_STATE_OFFLINE;
229			dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
230					  DMA_BIDIRECTIONAL);
231			printk ("%s: FC is offline\n", fc->name);
232			if (atomic_dec_and_test (&l->todo))
233				up(&l->sem);
234			break;
235		default:
236			printk ("PLOGI failed for %s with status %d\n", fc->name, status);
237			/* Do some sort of error recovery here */
238			break;
239		}
240	}
241}
242
243static void fcp_report_map_done(fc_channel *fc, int i, int status)
244{
245	fcp_cmnd *fcmd;
246	fc_hdr *fch;
247	unsigned char j;
248	ls *l = (ls *)fc->ls;
249	fc_al_posmap *p;
250
251	FCD(("Report map done %d %d\n", i, status))
252	switch (status) {
253	case FC_STATUS_OK: /* Ok, let's have a fun on a loop */
254		dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
255				  DMA_BIDIRECTIONAL);
256		p = (fc_al_posmap *)(l->logi + 3 * i);
257#ifdef FCDEBUG
258		{
259		u32 *u = (u32 *)p;
260		FCD(("%08x\n", u[0]))
261		u ++;
262		FCD(("%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n", u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7]))
263		}
264#endif
265		if ((p->magic & 0xffff0000) != FC_AL_LILP || !p->len) {
266			printk ("FC: Bad magic from REPORT_AL_MAP on %s - %08x\n", fc->name, p->magic);
267			fc->state = FC_STATE_OFFLINE;
268		} else {
269			fc->posmap = kzalloc(sizeof(fcp_posmap)+p->len, GFP_KERNEL);
270			if (!fc->posmap) {
271				printk("FC: Not enough memory, offlining channel\n");
272				fc->state = FC_STATE_OFFLINE;
273			} else {
274				int k;
275				fc->sid = (p->magic & 0xff);
276				for (i = 0; i < p->len; i++)
277					if (p->alpa[i] == fc->sid)
278						break;
279				k = p->len;
280				if (i == p->len)
281					i = 0;
282				else {
283					p->len--;
284					i++;
285				}
286				fc->posmap->len = p->len;
287				for (j = 0; j < p->len; j++) {
288					if (i == k) i = 0;
289					fc->posmap->list[j] = p->alpa[i++];
290				}
291				fc->state = FC_STATE_ONLINE;
292			}
293		}
294		printk ("%s: ONLINE\n", fc->name);
295		if (atomic_dec_and_test (&l->todo))
296			up(&l->sem);
297		break;
298	case FC_STATUS_POINTTOPOINT: /* We're Point-to-Point, no AL... */
299		FCD(("SID %d DID %d\n", fc->sid, fc->did))
300		fcmd = l->fcmds + i;
301		dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi),
302				 DMA_BIDIRECTIONAL);
303		fch = &fcmd->fch;
304		memset(l->logi + 3 * i, 0, 3 * sizeof(logi));
305		FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
306		FILL_FCHDR_SID(fch, 0);
307		FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
308		FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
309		FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
310		fch->param = 0;
311		l->logi [3 * i].code = LS_FLOGI;
312		fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
313					    DMA_BIDIRECTIONAL);
314		fcmd->rsp = fcmd->cmd + sizeof(logi);
315		fcmd->cmdlen = sizeof(logi);
316		fcmd->rsplen = sizeof(logi);
317		fcmd->data = (dma_addr_t)NULL;
318		fcmd->class = FC_CLASS_SIMPLE;
319		fcmd->proto = TYPE_EXTENDED_LS;
320		if (fc->hw_enque (fc, fcmd))
321			printk ("FC: Cannot enque FLOGI packet on %s\n", fc->name);
322		break;
323	case FC_STATUS_ERR_OFFLINE:
324		fc->state = FC_STATE_MAYBEOFFLINE;
325		FCD (("FC is offline %d\n", l->grace[i]))
326		break;
327	default:
328		printk ("FLOGI failed for %s with status %d\n", fc->name, status);
329		/* Do some sort of error recovery here */
330		break;
331	}
332}
333
334void fcp_register(fc_channel *fc, u8 type, int unregister)
335{
336	int size, i;
337	int slots = (fc->can_queue * 3) >> 1;
338
339	FCND(("Going to %sregister\n", unregister ? "un" : ""))
340
341	if (type == TYPE_SCSI_FCP) {
342		if (!unregister) {
343			fc->scsi_cmd_pool = (fcp_cmd *)
344				dma_alloc_consistent (fc->dev,
345						      slots * (sizeof (fcp_cmd) + fc->rsp_size),
346						      &fc->dma_scsi_cmd);
347			fc->scsi_rsp_pool = (char *)(fc->scsi_cmd_pool + slots);
348			fc->dma_scsi_rsp = fc->dma_scsi_cmd + slots * sizeof (fcp_cmd);
349			fc->scsi_bitmap_end = (slots + 63) & ~63;
350			size = fc->scsi_bitmap_end / 8;
351			fc->scsi_bitmap = kzalloc (size, GFP_KERNEL);
352			set_bit (0, fc->scsi_bitmap);
353			for (i = fc->can_queue; i < fc->scsi_bitmap_end; i++)
354				set_bit (i, fc->scsi_bitmap);
355			fc->scsi_free = fc->can_queue;
356			fc->cmd_slots = kzalloc(slots * sizeof(fcp_cmnd*), GFP_KERNEL);
357			fc->abort_count = 0;
358		} else {
359			fc->scsi_name[0] = 0;
360			kfree (fc->scsi_bitmap);
361			kfree (fc->cmd_slots);
362			FCND(("Unregistering\n"));
363			FCND(("Unregistered\n"));
364		}
365	} else
366		printk ("FC: %segistering unknown type %02x\n", unregister ? "Unr" : "R", type);
367}
368
369static void fcp_scsi_done(struct scsi_cmnd *SCpnt);
370
371static inline void fcp_scsi_receive(fc_channel *fc, int token, int status, fc_hdr *fch)
372{
373	fcp_cmnd *fcmd;
374	fcp_rsp  *rsp;
375	int host_status;
376	struct scsi_cmnd *SCpnt;
377	int sense_len;
378	int rsp_status;
379
380	fcmd = fc->cmd_slots[token];
381	if (!fcmd) return;
382	rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
383	SCpnt = SC_FCMND(fcmd);
384
385	if (SCpnt->done != fcp_scsi_done)
386		return;
387
388	rsp_status = rsp->fcp_status;
389	FCD(("rsp_status %08x status %08x\n", rsp_status, status))
390	switch (status) {
391	case FC_STATUS_OK:
392		host_status=DID_OK;
393
394		if (rsp_status & FCP_STATUS_RESID) {
395#ifdef FCDEBUG
396			FCD(("Resid %d\n", rsp->fcp_resid))
397			{
398				fcp_cmd *cmd = fc->scsi_cmd_pool + token;
399				int i;
400
401				printk ("Command ");
402				for (i = 0; i < sizeof(fcp_cmd); i+=4)
403					printk ("%08x ", *(u32 *)(((char *)cmd)+i));
404				printk ("\nResponse ");
405				for (i = 0; i < fc->rsp_size; i+=4)
406					printk ("%08x ", *(u32 *)(((char *)rsp)+i));
407				printk ("\n");
408			}
409#endif
410		}
411
412		if (rsp_status & FCP_STATUS_SENSE_LEN) {
413			sense_len = rsp->fcp_sense_len;
414			if (sense_len > sizeof(SCpnt->sense_buffer)) sense_len = sizeof(SCpnt->sense_buffer);
415			memcpy(SCpnt->sense_buffer, ((char *)(rsp+1)), sense_len);
416		}
417
418		if (fcmd->data) {
419			if (SCpnt->use_sg)
420				dma_unmap_sg(fc->dev, (struct scatterlist *)SCpnt->request_buffer,
421						SCpnt->use_sg,
422						SCpnt->sc_data_direction);
423			else
424				dma_unmap_single(fc->dev, fcmd->data, SCpnt->request_bufflen,
425						 SCpnt->sc_data_direction);
426		}
427		break;
428	default:
429		host_status=DID_ERROR;
430		FCD(("Wrong FC status %d for token %d\n", status, token))
431		break;
432	}
433
434	if (status_byte(rsp_status) == QUEUE_FULL) {
435		printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc->name, SCpnt->device->channel, SCpnt->device->id, rsp_status);
436	}
437
438	SCpnt->result = (host_status << 16) | (rsp_status & 0xff);
439#ifdef FCDEBUG
440	if (host_status || SCpnt->result || rsp_status) printk("FC: host_status %d, packet status %d\n",
441			host_status, SCpnt->result);
442#endif
443	SCpnt->done = fcmd->done;
444	fcmd->done=NULL;
445	clear_bit(token, fc->scsi_bitmap);
446	fc->scsi_free++;
447	FCD(("Calling scsi_done with %08x\n", SCpnt->result))
448	SCpnt->scsi_done(SCpnt);
449}
450
451void fcp_receive_solicited(fc_channel *fc, int proto, int token, int status, fc_hdr *fch)
452{
453	int magic;
454	FCD(("receive_solicited %d %d %d\n", proto, token, status))
455	switch (proto) {
456	case TYPE_SCSI_FCP:
457		fcp_scsi_receive(fc, token, status, fch); break;
458	case TYPE_EXTENDED_LS:
459	case PROTO_REPORT_AL_MAP:
460		magic = 0;
461		if (fc->ls)
462			magic = ((ls *)(fc->ls))->magic;
463		if (magic == LSMAGIC) {
464			ls *l = (ls *)fc->ls;
465			int i = (token >= l->count) ? token - l->count : token;
466
467			/* Let's be sure */
468			if ((unsigned)i < l->count && l->fcmds[i].fc == fc) {
469				if (proto == TYPE_EXTENDED_LS)
470					fcp_login_done(fc, token, status);
471				else
472					fcp_report_map_done(fc, token, status);
473				break;
474			}
475		}
476		FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc, fc->ls, fc->cmd_slots))
477		if (proto == TYPE_EXTENDED_LS && !fc->ls && fc->cmd_slots) {
478			fcp_cmnd *fcmd;
479
480			fcmd = fc->cmd_slots[token];
481			if (fcmd && fcmd->ls && ((ls *)(fcmd->ls))->magic == LSEMAGIC) {
482				lse *l = (lse *)fcmd->ls;
483
484				l->status = status;
485				up (&l->sem);
486			}
487		}
488		break;
489	case PROTO_OFFLINE:
490		if (fc->ls && ((lso *)(fc->ls))->magic == LSOMAGIC) {
491			lso *l = (lso *)fc->ls;
492
493			if ((unsigned)token < l->count && l->fcmds[token].fc == fc) {
494				/* Wow, OFFLINE response arrived :) */
495				FCD(("OFFLINE Response arrived\n"))
496				fc->state = FC_STATE_OFFLINE;
497				if (atomic_dec_and_test (&l->todo))
498					up(&l->sem);
499			}
500		}
501		break;
502
503	default:
504		break;
505	}
506}
507
508void fcp_state_change(fc_channel *fc, int state)
509{
510	FCD(("state_change %d %d\n", state, fc->state))
511	if (state == FC_STATE_ONLINE && fc->state == FC_STATE_MAYBEOFFLINE)
512		fc->state = FC_STATE_UNINITED;
513	else if (state == FC_STATE_ONLINE)
514		printk (KERN_WARNING "%s: state change to ONLINE\n", fc->name);
515	else
516		printk (KERN_ERR "%s: state change to OFFLINE\n", fc->name);
517}
518
519int fcp_initialize(fc_channel *fcchain, int count)
520{
521	fc_channel *fc;
522	fcp_cmnd *fcmd;
523	int i, retry, ret;
524	ls *l;
525
526	FCND(("fcp_inititialize %08lx\n", (long)fcp_init))
527	FCND(("fc_channels %08lx\n", (long)fc_channels))
528	FCND((" SID %d DID %d\n", fcchain->sid, fcchain->did))
529	l = kzalloc(sizeof (ls) + count, GFP_KERNEL);
530	if (!l) {
531		printk ("FC: Cannot allocate memory for initialization\n");
532		return -ENOMEM;
533	}
534	l->magic = LSMAGIC;
535	l->count = count;
536	FCND(("FCP Init for %d channels\n", count))
537	init_MUTEX_LOCKED(&l->sem);
538	init_timer(&l->timer);
539	l->timer.function = fcp_login_timeout;
540	l->timer.data = (unsigned long)l;
541	atomic_set (&l->todo, count);
542	l->logi = kzalloc (count * 3 * sizeof(logi), GFP_KERNEL);
543	l->fcmds = kzalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
544	if (!l->logi || !l->fcmds) {
545		kfree (l->logi);
546		kfree (l->fcmds);
547		kfree (l);
548		printk ("FC: Cannot allocate DMA memory for initialization\n");
549		return -ENOMEM;
550	}
551	for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
552		fc->state = FC_STATE_UNINITED;
553		fc->rst_pkt = NULL;	/* kmalloc when first used */
554	}
555	/* First try if we are in a AL topology */
556	FCND(("Initializing REPORT_MAP packets\n"))
557	for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
558		fcmd = l->fcmds + i;
559		fc->login = fcmd;
560		fc->ls = (void *)l;
561		/* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
562		fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
563					    DMA_BIDIRECTIONAL);
564		fcmd->proto = PROTO_REPORT_AL_MAP;
565		fcmd->token = i;
566		fcmd->fc = fc;
567	}
568	for (retry = 0; retry < 8; retry++) {
569		int nqueued = 0;
570		FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
571		for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
572			if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
573				continue;
574			disable_irq(fc->irq);
575			if (fc->state == FC_STATE_MAYBEOFFLINE) {
576				if (!l->grace[i]) {
577					l->grace[i]++;
578					FCD(("Grace\n"))
579				} else {
580					fc->state = FC_STATE_OFFLINE;
581					enable_irq(fc->irq);
582					dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
583					if (atomic_dec_and_test (&l->todo))
584						goto all_done;
585				}
586			}
587			ret = fc->hw_enque (fc, fc->login);
588			enable_irq(fc->irq);
589			if (!ret) {
590				nqueued++;
591				continue;
592			}
593			if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
594				/* Oh yes, this card handles Point-to-Point only, so let's try that. */
595				fc_hdr *fch;
596
597				FCD(("SID %d DID %d\n", fc->sid, fc->did))
598				fcmd = l->fcmds + i;
599				dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
600				fch = &fcmd->fch;
601				FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
602				FILL_FCHDR_SID(fch, 0);
603				FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
604				FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
605				FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
606				fch->param = 0;
607				l->logi [3 * i].code = LS_FLOGI;
608				fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
609				fcmd->rsp = fcmd->cmd + sizeof(logi);
610				fcmd->cmdlen = sizeof(logi);
611				fcmd->rsplen = sizeof(logi);
612				fcmd->data = (dma_addr_t)NULL;
613				fcmd->class = FC_CLASS_SIMPLE;
614				fcmd->proto = TYPE_EXTENDED_LS;
615			} else
616				printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
617		}
618
619		if (nqueued) {
620			l->timer.expires = jiffies + 5 * HZ;
621			add_timer(&l->timer);
622
623			down(&l->sem);
624			if (!atomic_read(&l->todo)) {
625				FCND(("All channels answered in time\n"))
626				break; /* All fc channels have answered us */
627			}
628		}
629	}
630all_done:
631	for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
632		fc->ls = NULL;
633		switch (fc->state) {
634		case FC_STATE_ONLINE: break;
635		case FC_STATE_OFFLINE: break;
636		default: dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
637			break;
638		}
639	}
640	del_timer(&l->timer);
641	kfree (l->logi);
642	kfree (l->fcmds);
643	kfree (l);
644	return 0;
645}
646
647int fcp_forceoffline(fc_channel *fcchain, int count)
648{
649	fc_channel *fc;
650	fcp_cmnd *fcmd;
651	int i, ret;
652	lso l;
653
654	memset (&l, 0, sizeof(lso));
655	l.count = count;
656	l.magic = LSOMAGIC;
657	FCND(("FCP Force Offline for %d channels\n", count))
658	init_MUTEX_LOCKED(&l.sem);
659	init_timer(&l.timer);
660	l.timer.function = fcp_login_timeout;
661	l.timer.data = (unsigned long)&l;
662	atomic_set (&l.todo, count);
663	l.fcmds = kzalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
664	if (!l.fcmds) {
665		printk ("FC: Cannot allocate memory for forcing offline\n");
666		return -ENOMEM;
667	}
668	FCND(("Initializing OFFLINE packets\n"))
669	for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
670		fc->state = FC_STATE_UNINITED;
671		fcmd = l.fcmds + i;
672		fc->login = fcmd;
673		fc->ls = (void *)&l;
674		fcmd->did = fc->did;
675		fcmd->class = FC_CLASS_OFFLINE;
676		fcmd->proto = PROTO_OFFLINE;
677		fcmd->token = i;
678		fcmd->fc = fc;
679		disable_irq(fc->irq);
680		ret = fc->hw_enque (fc, fc->login);
681		enable_irq(fc->irq);
682		if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
683	}
684
685	l.timer.expires = jiffies + 5 * HZ;
686	add_timer(&l.timer);
687	down(&l.sem);
688	del_timer(&l.timer);
689
690	for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
691		fc->ls = NULL;
692	kfree (l.fcmds);
693	return 0;
694}
695
696int fcp_init(fc_channel *fcchain)
697{
698	fc_channel *fc;
699	int count=0;
700	int ret;
701
702	for (fc = fcchain; fc; fc = fc->next) {
703		fc->fcp_register = fcp_register;
704		count++;
705	}
706
707	ret = fcp_initialize (fcchain, count);
708	if (ret)
709		return ret;
710
711	if (!fc_channels)
712		fc_channels = fcchain;
713	else {
714		for (fc = fc_channels; fc->next; fc = fc->next);
715		fc->next = fcchain;
716	}
717	return ret;
718}
719
720void fcp_release(fc_channel *fcchain, int count)  /* count must > 0 */
721{
722	fc_channel *fc;
723	fc_channel *fcx;
724
725	for (fc = fcchain; --count && fc->next; fc = fc->next);
726	if (count) {
727		printk("FC: nothing to release\n");
728		return;
729	}
730
731	if (fc_channels == fcchain)
732		fc_channels = fc->next;
733	else {
734		for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
735		fcx->next = fc->next;
736	}
737	fc->next = NULL;
738
739	/*
740	 *  We've just grabbed fcchain out of the fc_channel list
741	 *  and zero-terminated it, while destroying the count.
742	 *
743	 *  Freeing the fc's is the low level driver's responsibility.
744	 */
745}
746
747
748static void fcp_scsi_done(struct scsi_cmnd *SCpnt)
749{
750	if (FCP_CMND(SCpnt)->done)
751		FCP_CMND(SCpnt)->done(SCpnt);
752}
753
754static int fcp_scsi_queue_it(fc_channel *fc, struct scsi_cmnd *SCpnt,
755			     fcp_cmnd *fcmd, int prepare)
756{
757	long i;
758	fcp_cmd *cmd;
759	u32 fcp_cntl;
760	if (prepare) {
761		i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
762		set_bit (i, fc->scsi_bitmap);
763		fcmd->token = i;
764		cmd = fc->scsi_cmd_pool + i;
765
766		if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
767			/* Invalid channel/id/lun and couldn't map it into fcp_addr */
768			clear_bit (i, fc->scsi_bitmap);
769			SCpnt->result = (DID_BAD_TARGET << 16);
770			SCpnt->scsi_done(SCpnt);
771			return 0;
772		}
773		fc->scsi_free--;
774		fc->cmd_slots[fcmd->token] = fcmd;
775
776		if (SCpnt->device->tagged_supported) {
777			if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
778				fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
779				fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
780			} else
781				fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
782		} else
783			fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
784		if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
785			cmd->fcp_cntl = fcp_cntl;
786			fcmd->data = (dma_addr_t)NULL;
787		} else {
788			switch (SCpnt->cmnd[0]) {
789			case WRITE_6:
790			case WRITE_10:
791			case WRITE_12:
792				cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
793			default:
794				cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
795			}
796			if (!SCpnt->use_sg) {
797				cmd->fcp_data_len = SCpnt->request_bufflen;
798				fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
799							     SCpnt->request_bufflen,
800							     SCpnt->sc_data_direction);
801			} else {
802				struct scatterlist *sg = (struct scatterlist *)SCpnt->request_buffer;
803				int nents;
804
805				FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
806				nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
807						    SCpnt->sc_data_direction);
808				if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
809				fcmd->data = sg_dma_address(sg);
810				cmd->fcp_data_len = sg_dma_len(sg);
811			}
812		}
813		memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
814		memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
815		FCD(("XXX: %04x.%04x.%04x.%04x - %08x%08x%08x\n", cmd->fcp_addr[0], cmd->fcp_addr[1], cmd->fcp_addr[2], cmd->fcp_addr[3], *(u32 *)SCpnt->cmnd, *(u32 *)(SCpnt->cmnd+4), *(u32 *)(SCpnt->cmnd+8)))
816	}
817	FCD(("Trying to enque %p\n", fcmd))
818	if (!fc->scsi_que) {
819		if (!fc->hw_enque (fc, fcmd)) {
820			FCD(("hw_enque succeeded for %p\n", fcmd))
821			return 0;
822		}
823	}
824	FCD(("Putting into que1 %p\n", fcmd))
825	fcp_scsi_insert_queue (fc, fcmd);
826	return 0;
827}
828
829int fcp_scsi_queuecommand(struct scsi_cmnd *SCpnt,
830			  void (* done)(struct scsi_cmnd *))
831{
832	fcp_cmnd *fcmd = FCP_CMND(SCpnt);
833	fc_channel *fc = FC_SCMND(SCpnt);
834
835	FCD(("Entering SCSI queuecommand %p\n", fcmd))
836	if (SCpnt->done != fcp_scsi_done) {
837		fcmd->done = SCpnt->done;
838		SCpnt->done = fcp_scsi_done;
839		SCpnt->scsi_done = done;
840		fcmd->proto = TYPE_SCSI_FCP;
841		if (!fc->scsi_free) {
842			FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
843#if (FCP_SCSI_USE_NEW_EH_CODE == 0)
844			printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
845#endif
846			return 1;
847		}
848		return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
849	}
850	return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
851}
852
853void fcp_queue_empty(fc_channel *fc)
854{
855	fcp_cmnd *fcmd;
856
857	FCD(("Queue empty\n"))
858	while ((fcmd = fc->scsi_que)) {
859		/* The hw told us we can try again queue some packet */
860		if (fc->hw_enque (fc, fcmd))
861			break;
862		fcp_scsi_remove_queue (fc, fcmd);
863	}
864}
865
866int fcp_scsi_abort(struct scsi_cmnd *SCpnt)
867{
868	/* Internal bookkeeping only. Lose 1 cmd_slots slot. */
869	fcp_cmnd *fcmd = FCP_CMND(SCpnt);
870	fc_channel *fc = FC_SCMND(SCpnt);
871
872	/*
873	 * We react to abort requests by simply forgetting
874	 * about the command and pretending everything's sweet.
875	 * This may or may not be silly. We can't, however,
876	 * immediately reuse the command's cmd_slots slot,
877	 * as its result may arrive later and we cannot
878	 * check whether it is the aborted one, can't we?
879	 *
880	 * Therefore, after the first few aborts are done,
881	 * we tell the scsi error handler to do something clever.
882	 * It will eventually call host reset, refreshing
883	 * cmd_slots for us.
884	 *
885	 * There is a theoretical chance that we sometimes allow
886	 * more than can_queue packets to the jungle this way,
887	 * but the worst outcome possible is a series of
888	 * more aborts and eventually the dev_reset catharsis.
889	 */
890
891	if (++fc->abort_count < (fc->can_queue >> 1)) {
892		SCpnt->result = DID_ABORT;
893		fcmd->done(SCpnt);
894		printk("FC: soft abort\n");
895		return SUCCESS;
896	} else {
897		printk("FC: hard abort refused\n");
898		return FAILED;
899	}
900}
901
902
903#define FCP_RESET_TIMEOUT (2*HZ)
904
905int fcp_scsi_dev_reset(struct scsi_cmnd *SCpnt)
906{
907	return SUCCESS;
908}
909
910static int __fcp_scsi_host_reset(struct scsi_cmnd *SCpnt)
911{
912	fc_channel *fc = FC_SCMND(SCpnt);
913	fcp_cmnd *fcmd = FCP_CMND(SCpnt);
914	int i;
915
916	printk ("FC: host reset\n");
917
918	for (i=0; i < fc->can_queue; i++) {
919		if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
920			SCpnt->result = DID_RESET;
921			fcmd->done(SCpnt);
922			fc->cmd_slots[i] = NULL;
923		}
924	}
925	fc->reset(fc);
926	fc->abort_count = 0;
927	if (fcp_initialize(fc, 1)) return SUCCESS;
928	else return FAILED;
929}
930
931int fcp_scsi_host_reset(struct scsi_cmnd *SCpnt)
932{
933	unsigned long flags;
934	int rc;
935
936	spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
937	rc = __fcp_scsi_host_reset(SCpnt);
938	spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
939
940	return rc;
941}
942
943static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
944{
945	long i;
946
947	i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
948	set_bit (i, fc->scsi_bitmap);
949	fcmd->token = i;
950	fc->scsi_free--;
951	fc->cmd_slots[fcmd->token] = fcmd;
952	return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
953}
954
955static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
956{
957	fcp_cmnd _fcmd, *fcmd;
958	fc_hdr *fch;
959	lse l;
960	int i;
961
962	fcmd = &_fcmd;
963	memset(fcmd, 0, sizeof(fcp_cmnd));
964	FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
965	fch = &fcmd->fch;
966	FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
967	FILL_FCHDR_SID(fch, fc->sid);
968	FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
969	FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
970	FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
971	fch->param = 0;
972	fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
973	fcmd->rsp = fcmd->cmd + len;
974	fcmd->cmdlen = len;
975	fcmd->rsplen = len;
976	fcmd->data = (dma_addr_t)NULL;
977	fcmd->fc = fc;
978	fcmd->class = FC_CLASS_SIMPLE;
979	fcmd->proto = TYPE_EXTENDED_LS;
980
981	memset (&l, 0, sizeof(lse));
982	l.magic = LSEMAGIC;
983	init_MUTEX_LOCKED(&l.sem);
984	l.timer.function = fcp_login_timeout;
985	l.timer.data = (unsigned long)&l;
986	l.status = FC_STATUS_TIMED_OUT;
987	fcmd->ls = (void *)&l;
988
989	disable_irq(fc->irq);
990	fcp_els_queue_it(fc, fcmd);
991	enable_irq(fc->irq);
992
993	for (i = 0;;) {
994		l.timer.expires = jiffies + 5 * HZ;
995		add_timer(&l.timer);
996		down(&l.sem);
997		del_timer(&l.timer);
998		if (l.status != FC_STATUS_TIMED_OUT) break;
999		if (++i == 3) break;
1000		disable_irq(fc->irq);
1001		fcp_scsi_queue_it(fc, NULL, fcmd, 0);
1002		enable_irq(fc->irq);
1003	}
1004
1005	clear_bit(fcmd->token, fc->scsi_bitmap);
1006	fc->scsi_free++;
1007	dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
1008	return l.status;
1009}
1010
1011int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
1012{
1013	logi *l;
1014	int status;
1015
1016	l = kzalloc(2 * sizeof(logi), GFP_KERNEL);
1017	if (!l) return -ENOMEM;
1018	l->code = LS_PLOGI;
1019	memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
1020	memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
1021	memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
1022	memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
1023	status = fc_do_els(fc, alpa, l, sizeof(logi));
1024	if (status == FC_STATUS_OK) {
1025		if (l[1].code == LS_ACC) {
1026#ifdef FCDEBUG
1027			u32 *u = (u32 *)&l[1].nport_wwn;
1028			FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa,
1029				u[0], u[1], u[2], u[3]))
1030#endif
1031			memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
1032			memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
1033		} else
1034			status = FC_STATUS_BAD_RSP;
1035	}
1036	kfree(l);
1037	return status;
1038}
1039
1040typedef struct {
1041	unsigned int code;
1042	unsigned params[4];
1043} prli;
1044
1045int fc_do_prli(fc_channel *fc, unsigned char alpa)
1046{
1047	prli *p;
1048	int status;
1049
1050	p = kzalloc(2 * sizeof(prli), GFP_KERNEL);
1051	if (!p) return -ENOMEM;
1052	p->code = LS_PRLI;
1053	p->params[0] = 0x08002000;
1054	p->params[3] = 0x00000022;
1055	status = fc_do_els(fc, alpa, p, sizeof(prli));
1056	if (status == FC_STATUS_OK && p[1].code != LS_PRLI_ACC && p[1].code != LS_ACC)
1057		status = FC_STATUS_BAD_RSP;
1058	kfree(p);
1059	return status;
1060}
1061
1062MODULE_LICENSE("GPL");
1063