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