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