[PATCH] time.h: remove ifdefs
[linux-2.6] / 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/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) ((Scsi_Cmnd *)((long)fcmnd - (long)&(((Scsi_Cmnd *)0)->SCp)))
74
75 static int fcp_scsi_queue_it(fc_channel *, Scsi_Cmnd *, fcp_cmnd *, int);
76 void fcp_queue_empty(fc_channel *);
77
78 static 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
92 static 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
104 fc_channel *fc_channels = NULL;
105
106 #define LSMAGIC 620829043
107 typedef 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
120 typedef 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
131 typedef struct {
132         /* Must be first */
133         struct semaphore sem;
134         int magic;
135         int status;
136         struct timer_list timer;
137 } lse;
138
139 static void fcp_login_timeout(unsigned long data)
140 {
141         ls *l = (ls *)data;
142         FCND(("Login timeout\n"))
143         up(&l->sem);
144 }
145
146 static 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
243 static 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 = (fcp_posmap *)kmalloc(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                                 memset(fc->posmap, 0, sizeof(fcp_posmap)+p->len);
276                                 /* FIXME: This is where SOCAL transfers our AL-PA.
277                                    Keep it here till we found out what other cards do... */
278                                 fc->sid = (p->magic & 0xff);
279                                 for (i = 0; i < p->len; i++)
280                                         if (p->alpa[i] == fc->sid)
281                                                 break;
282                                 k = p->len;
283                                 if (i == p->len)
284                                         i = 0;
285                                 else {
286                                         p->len--;
287                                         i++;
288                                 }
289                                 fc->posmap->len = p->len;
290                                 for (j = 0; j < p->len; j++) {
291                                         if (i == k) i = 0;
292                                         fc->posmap->list[j] = p->alpa[i++];
293                                 }
294                                 fc->state = FC_STATE_ONLINE;
295                         }
296                 }
297                 printk ("%s: ONLINE\n", fc->name);
298                 if (atomic_dec_and_test (&l->todo))
299                         up(&l->sem);
300                 break;
301         case FC_STATUS_POINTTOPOINT: /* We're Point-to-Point, no AL... */
302                 FCD(("SID %d DID %d\n", fc->sid, fc->did))
303                 fcmd = l->fcmds + i;
304                 dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi),
305                                  DMA_BIDIRECTIONAL);
306                 fch = &fcmd->fch;
307                 memset(l->logi + 3 * i, 0, 3 * sizeof(logi));
308                 FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
309                 FILL_FCHDR_SID(fch, 0);
310                 FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
311                 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
312                 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
313                 fch->param = 0;
314                 l->logi [3 * i].code = LS_FLOGI;
315                 fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
316                                             DMA_BIDIRECTIONAL);
317                 fcmd->rsp = fcmd->cmd + sizeof(logi);
318                 fcmd->cmdlen = sizeof(logi);
319                 fcmd->rsplen = sizeof(logi);
320                 fcmd->data = (dma_addr_t)NULL;
321                 fcmd->class = FC_CLASS_SIMPLE;
322                 fcmd->proto = TYPE_EXTENDED_LS;
323                 if (fc->hw_enque (fc, fcmd))
324                         printk ("FC: Cannot enque FLOGI packet on %s\n", fc->name);
325                 break;
326         case FC_STATUS_ERR_OFFLINE:
327                 fc->state = FC_STATE_MAYBEOFFLINE;
328                 FCD (("FC is offline %d\n", l->grace[i]))
329                 break;
330         default:
331                 printk ("FLOGI failed for %s with status %d\n", fc->name, status);
332                 /* Do some sort of error recovery here */
333                 break;
334         }
335 }
336
337 void fcp_register(fc_channel *fc, u8 type, int unregister)
338 {
339         int size, i;
340         int slots = (fc->can_queue * 3) >> 1;
341
342         FCND(("Going to %sregister\n", unregister ? "un" : ""))
343
344         if (type == TYPE_SCSI_FCP) {
345                 if (!unregister) {
346                         fc->scsi_cmd_pool = (fcp_cmd *)
347                                 dma_alloc_consistent (fc->dev,
348                                                       slots * (sizeof (fcp_cmd) + fc->rsp_size),
349                                                       &fc->dma_scsi_cmd);
350                         fc->scsi_rsp_pool = (char *)(fc->scsi_cmd_pool + slots);
351                         fc->dma_scsi_rsp = fc->dma_scsi_cmd + slots * sizeof (fcp_cmd);
352                         fc->scsi_bitmap_end = (slots + 63) & ~63;
353                         size = fc->scsi_bitmap_end / 8;
354                         fc->scsi_bitmap = kmalloc (size, GFP_KERNEL);
355                         memset (fc->scsi_bitmap, 0, size);
356                         set_bit (0, fc->scsi_bitmap);
357                         for (i = fc->can_queue; i < fc->scsi_bitmap_end; i++)
358                                 set_bit (i, fc->scsi_bitmap);
359                         fc->scsi_free = fc->can_queue;
360                         fc->cmd_slots = (fcp_cmnd **)kmalloc(slots * sizeof(fcp_cmnd*), GFP_KERNEL);
361                         memset(fc->cmd_slots, 0, slots * sizeof(fcp_cmnd*));
362                         fc->abort_count = 0;
363                 } else {
364                         fc->scsi_name[0] = 0;
365                         kfree (fc->scsi_bitmap);
366                         kfree (fc->cmd_slots);
367                         FCND(("Unregistering\n"));
368 #if 0
369                         if (fc->rst_pkt) {
370                                 if (fc->rst_pkt->eh_state == SCSI_STATE_UNUSED)
371                                         kfree(fc->rst_pkt);
372                                 else {
373                                         /* Can't happen. Some memory would be lost. */
374                                         printk("FC: Reset in progress. Now?!");
375                                 }
376                         }
377 #endif
378                         FCND(("Unregistered\n"));
379                 }
380         } else
381                 printk ("FC: %segistering unknown type %02x\n", unregister ? "Unr" : "R", type);
382 }
383
384 static void fcp_scsi_done(Scsi_Cmnd *SCpnt);
385
386 static inline void fcp_scsi_receive(fc_channel *fc, int token, int status, fc_hdr *fch)
387 {
388         fcp_cmnd *fcmd;
389         fcp_rsp  *rsp;
390         int host_status;
391         Scsi_Cmnd *SCpnt;
392         int sense_len;
393         int rsp_status;
394
395         fcmd = fc->cmd_slots[token];
396         if (!fcmd) return;
397         rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
398         SCpnt = SC_FCMND(fcmd);
399
400         if (SCpnt->done != fcp_scsi_done)
401                 return;
402
403         rsp_status = rsp->fcp_status;
404         FCD(("rsp_status %08x status %08x\n", rsp_status, status))
405         switch (status) {
406         case FC_STATUS_OK:
407                 host_status=DID_OK;
408                 
409                 if (rsp_status & FCP_STATUS_RESID) {
410 #ifdef FCDEBUG
411                         FCD(("Resid %d\n", rsp->fcp_resid))
412                         {
413                                 fcp_cmd *cmd = fc->scsi_cmd_pool + token;
414                                 int i;
415                                 
416                                 printk ("Command ");
417                                 for (i = 0; i < sizeof(fcp_cmd); i+=4)
418                                         printk ("%08x ", *(u32 *)(((char *)cmd)+i));
419                                 printk ("\nResponse ");
420                                 for (i = 0; i < fc->rsp_size; i+=4)
421                                         printk ("%08x ", *(u32 *)(((char *)rsp)+i));
422                                 printk ("\n");
423                         }
424 #endif                  
425                 }
426
427                 if (rsp_status & FCP_STATUS_SENSE_LEN) {
428                         sense_len = rsp->fcp_sense_len;
429                         if (sense_len > sizeof(SCpnt->sense_buffer)) sense_len = sizeof(SCpnt->sense_buffer);
430                         memcpy(SCpnt->sense_buffer, ((char *)(rsp+1)), sense_len);
431                 }
432                 
433                 if (fcmd->data) {
434                         if (SCpnt->use_sg)
435                                 dma_unmap_sg(fc->dev, (struct scatterlist *)SCpnt->buffer,
436                                                 SCpnt->use_sg,
437                                                 SCpnt->sc_data_direction);
438                         else
439                                 dma_unmap_single(fc->dev, fcmd->data, SCpnt->request_bufflen,
440                                                  SCpnt->sc_data_direction);
441                 }
442                 break;
443         default:
444                 host_status=DID_ERROR; /* FIXME */
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->device->channel, SCpnt->device->id, 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
466 void 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
523 void 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
534 int 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         init_timer(&l->timer);
555         l->timer.function = fcp_login_timeout;
556         l->timer.data = (unsigned long)l;
557         atomic_set (&l->todo, count);
558         l->logi = kmalloc (count * 3 * sizeof(logi), GFP_KERNEL);
559         l->fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
560         if (!l->logi || !l->fcmds) {
561                 if (l->logi) kfree (l->logi);
562                 if (l->fcmds) kfree (l->fcmds);
563                 kfree (l);
564                 printk ("FC: Cannot allocate DMA memory for initialization\n");
565                 return -ENOMEM;
566         }
567         memset (l->logi, 0, count * 3 * sizeof(logi));
568         memset (l->fcmds, 0, count * sizeof(fcp_cmnd));
569         for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
570                 fc->state = FC_STATE_UNINITED;
571                 fc->rst_pkt = NULL;     /* kmalloc when first used */
572         }
573         /* First try if we are in a AL topology */
574         FCND(("Initializing REPORT_MAP packets\n"))
575         for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
576                 fcmd = l->fcmds + i;
577                 fc->login = fcmd;
578                 fc->ls = (void *)l;
579                 /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
580                 fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
581                                             DMA_BIDIRECTIONAL);
582                 fcmd->proto = PROTO_REPORT_AL_MAP;
583                 fcmd->token = i;
584                 fcmd->fc = fc;
585         }
586         for (retry = 0; retry < 8; retry++) {
587                 int nqueued = 0;
588                 FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
589                 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
590                         if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
591                                 continue;
592                         disable_irq(fc->irq);
593                         if (fc->state == FC_STATE_MAYBEOFFLINE) {
594                                 if (!l->grace[i]) {
595                                         l->grace[i]++;
596                                         FCD(("Grace\n"))
597                                 } else {
598                                         fc->state = FC_STATE_OFFLINE;
599                                         enable_irq(fc->irq);
600                                         dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
601                                         if (atomic_dec_and_test (&l->todo))
602                                                 goto all_done;
603                                 }
604                         }
605                         ret = fc->hw_enque (fc, fc->login);
606                         enable_irq(fc->irq);
607                         if (!ret) {
608                                 nqueued++;
609                                 continue;
610                         }
611                         if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
612                                 /* Oh yes, this card handles Point-to-Point only, so let's try that. */
613                                 fc_hdr *fch;
614
615                                 FCD(("SID %d DID %d\n", fc->sid, fc->did))
616                                 fcmd = l->fcmds + i;
617                                 dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
618                                 fch = &fcmd->fch;
619                                 FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
620                                 FILL_FCHDR_SID(fch, 0);
621                                 FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
622                                 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
623                                 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
624                                 fch->param = 0;
625                                 l->logi [3 * i].code = LS_FLOGI;
626                                 fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
627                                 fcmd->rsp = fcmd->cmd + sizeof(logi);
628                                 fcmd->cmdlen = sizeof(logi);
629                                 fcmd->rsplen = sizeof(logi);
630                                 fcmd->data = (dma_addr_t)NULL;
631                                 fcmd->class = FC_CLASS_SIMPLE;
632                                 fcmd->proto = TYPE_EXTENDED_LS;
633                         } else
634                                 printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
635                 }
636                 
637                 if (nqueued) {
638                         l->timer.expires = jiffies + 5 * HZ;
639                         add_timer(&l->timer);
640
641                         down(&l->sem);
642                         if (!atomic_read(&l->todo)) {
643                                 FCND(("All channels answered in time\n"))
644                                 break; /* All fc channels have answered us */
645                         }
646                 }
647         }
648 all_done:
649         for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
650                 fc->ls = NULL;
651                 switch (fc->state) {
652                 case FC_STATE_ONLINE: break;
653                 case FC_STATE_OFFLINE: break;
654                 default: dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
655                         break;
656                 }
657         }
658         del_timer(&l->timer);
659         kfree (l->logi);
660         kfree (l->fcmds);
661         kfree (l);
662         return 0;
663 }
664
665 int fcp_forceoffline(fc_channel *fcchain, int count)
666 {
667         fc_channel *fc;
668         fcp_cmnd *fcmd;
669         int i, ret;
670         lso l;
671
672         memset (&l, 0, sizeof(lso));
673         l.count = count;
674         l.magic = LSOMAGIC;
675         FCND(("FCP Force Offline for %d channels\n", count))
676         init_MUTEX_LOCKED(&l.sem);
677         init_timer(&l.timer);
678         l.timer.function = fcp_login_timeout;
679         l.timer.data = (unsigned long)&l;
680         atomic_set (&l.todo, count);
681         l.fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
682         if (!l.fcmds) {
683                 kfree (l.fcmds);
684                 printk ("FC: Cannot allocate memory for forcing offline\n");
685                 return -ENOMEM;
686         }
687         memset (l.fcmds, 0, count * sizeof(fcp_cmnd));
688         FCND(("Initializing OFFLINE packets\n"))
689         for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
690                 fc->state = FC_STATE_UNINITED;
691                 fcmd = l.fcmds + i;
692                 fc->login = fcmd;
693                 fc->ls = (void *)&l;
694                 fcmd->did = fc->did;
695                 fcmd->class = FC_CLASS_OFFLINE;
696                 fcmd->proto = PROTO_OFFLINE;
697                 fcmd->token = i;
698                 fcmd->fc = fc;
699                 disable_irq(fc->irq);
700                 ret = fc->hw_enque (fc, fc->login);
701                 enable_irq(fc->irq);
702                 if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
703         }
704                 
705         l.timer.expires = jiffies + 5 * HZ;
706         add_timer(&l.timer);
707         down(&l.sem);
708         del_timer(&l.timer);
709         
710         for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
711                 fc->ls = NULL;
712         kfree (l.fcmds);
713         return 0;
714 }
715
716 int fcp_init(fc_channel *fcchain)
717 {
718         fc_channel *fc;
719         int count=0;
720         int ret;
721         
722         for (fc = fcchain; fc; fc = fc->next) {
723                 fc->fcp_register = fcp_register;
724                 count++;
725         }
726
727         ret = fcp_initialize (fcchain, count);
728         if (ret)
729                 return ret;
730                 
731         if (!fc_channels)
732                 fc_channels = fcchain;
733         else {
734                 for (fc = fc_channels; fc->next; fc = fc->next);
735                 fc->next = fcchain;
736         }
737         return ret;
738 }
739
740 void fcp_release(fc_channel *fcchain, int count)  /* count must > 0 */
741 {
742         fc_channel *fc;
743         fc_channel *fcx;
744
745         for (fc = fcchain; --count && fc->next; fc = fc->next);
746         if (count) {
747                 printk("FC: nothing to release\n");
748                 return;
749         }
750         
751         if (fc_channels == fcchain)
752                 fc_channels = fc->next;
753         else {
754                 for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
755                 fcx->next = fc->next;
756         }
757         fc->next = NULL;
758
759         /*
760          *  We've just grabbed fcchain out of the fc_channel list
761          *  and zero-terminated it, while destroying the count.
762          *
763          *  Freeing the fc's is the low level driver's responsibility.
764          */
765 }
766
767
768 static void fcp_scsi_done (Scsi_Cmnd *SCpnt)
769 {
770         if (FCP_CMND(SCpnt)->done)
771                 FCP_CMND(SCpnt)->done(SCpnt);
772 }
773
774 static int fcp_scsi_queue_it(fc_channel *fc, Scsi_Cmnd *SCpnt, fcp_cmnd *fcmd, int prepare)
775 {
776         long i;
777         fcp_cmd *cmd;
778         u32 fcp_cntl;
779         if (prepare) {
780                 i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
781                 set_bit (i, fc->scsi_bitmap);
782                 fcmd->token = i;
783                 cmd = fc->scsi_cmd_pool + i;
784
785                 if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
786                         /* Invalid channel/id/lun and couldn't map it into fcp_addr */
787                         clear_bit (i, fc->scsi_bitmap);
788                         SCpnt->result = (DID_BAD_TARGET << 16);
789                         SCpnt->scsi_done(SCpnt);
790                         return 0;
791                 }
792                 fc->scsi_free--;
793                 fc->cmd_slots[fcmd->token] = fcmd;
794
795                 if (SCpnt->device->tagged_supported) {
796                         if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
797                                 fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
798                                 fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
799                         } else
800                                 fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
801                 } else
802                         fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
803                 if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
804                         cmd->fcp_cntl = fcp_cntl;
805                         fcmd->data = (dma_addr_t)NULL;
806                 } else {
807                         switch (SCpnt->cmnd[0]) {
808                         case WRITE_6:
809                         case WRITE_10:
810                         case WRITE_12:
811                                 cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
812                         default:
813                                 cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
814                         }
815                         if (!SCpnt->use_sg) {
816                                 cmd->fcp_data_len = SCpnt->request_bufflen;
817                                 fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
818                                                              SCpnt->request_bufflen,
819                                                              SCpnt->sc_data_direction);
820                         } else {
821                                 struct scatterlist *sg = (struct scatterlist *)SCpnt->buffer;
822                                 int nents;
823
824                                 FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
825                                 nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
826                                                     SCpnt->sc_data_direction);
827                                 if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
828                                 fcmd->data = sg_dma_address(sg);
829                                 cmd->fcp_data_len = sg_dma_len(sg);
830                         }
831                 }
832                 memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
833                 memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
834                 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)))
835         }
836         FCD(("Trying to enque %p\n", fcmd))
837         if (!fc->scsi_que) {
838                 if (!fc->hw_enque (fc, fcmd)) {
839                         FCD(("hw_enque succeeded for %p\n", fcmd))
840                         return 0;
841                 }
842         }
843         FCD(("Putting into que1 %p\n", fcmd))
844         fcp_scsi_insert_queue (fc, fcmd);
845         return 0;
846 }
847
848 int fcp_scsi_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *))
849 {
850         fcp_cmnd *fcmd = FCP_CMND(SCpnt);
851         fc_channel *fc = FC_SCMND(SCpnt);
852         
853         FCD(("Entering SCSI queuecommand %p\n", fcmd))
854         if (SCpnt->done != fcp_scsi_done) {
855                 fcmd->done = SCpnt->done;
856                 SCpnt->done = fcp_scsi_done;
857                 SCpnt->scsi_done = done;
858                 fcmd->proto = TYPE_SCSI_FCP;
859                 if (!fc->scsi_free) {
860                         FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
861 #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
862                         printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
863 #endif
864                         return 1;
865                 }
866                 return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
867         }
868         return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
869 }
870
871 void fcp_queue_empty(fc_channel *fc)
872 {
873         fcp_cmnd *fcmd;
874         
875         FCD(("Queue empty\n"))
876         while ((fcmd = fc->scsi_que)) {
877                 /* The hw told us we can try again queue some packet */
878                 if (fc->hw_enque (fc, fcmd))
879                         break;
880                 fcp_scsi_remove_queue (fc, fcmd);
881         }
882 }
883
884 int fcp_scsi_abort(Scsi_Cmnd *SCpnt)
885 {
886         /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
887         fcp_cmnd *fcmd = FCP_CMND(SCpnt);
888         fc_channel *fc = FC_SCMND(SCpnt);
889         
890         /*
891          * We react to abort requests by simply forgetting
892          * about the command and pretending everything's sweet.
893          * This may or may not be silly. We can't, however,
894          * immediately reuse the command's cmd_slots slot,
895          * as its result may arrive later and we cannot
896          * check whether it is the aborted one, can't we?
897          *
898          * Therefore, after the first few aborts are done,
899          * we tell the scsi error handler to do something clever.
900          * It will eventually call host reset, refreshing
901          * cmd_slots for us.
902          *
903          * There is a theoretical chance that we sometimes allow
904          * more than can_queue packets to the jungle this way,
905          * but the worst outcome possible is a series of
906          * more aborts and eventually the dev_reset catharsis.
907          */
908
909         if (++fc->abort_count < (fc->can_queue >> 1)) {
910                 SCpnt->result = DID_ABORT;
911                 fcmd->done(SCpnt);
912                 printk("FC: soft abort\n");
913                 return SUCCESS;
914         } else {
915                 printk("FC: hard abort refused\n");
916                 return FAILED;
917         }
918 }
919
920 #if 0
921 void fcp_scsi_reset_done(Scsi_Cmnd *SCpnt)
922 {
923         fc_channel *fc = FC_SCMND(SCpnt);
924
925         fc->rst_pkt->eh_state = SCSI_STATE_FINISHED;
926         up(fc->rst_pkt->device->host->eh_action);
927 }
928 #endif
929
930 #define FCP_RESET_TIMEOUT (2*HZ)
931
932 int fcp_scsi_dev_reset(Scsi_Cmnd *SCpnt)
933 {
934 #if 0 /* broken junk, but if davem wants to compile this driver, let him.. */
935         unsigned long flags;
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->device = SCpnt->device;
953                 fc->rst_pkt->cmd_len = 0;
954                 
955                 fc->cmd_slots[0] = fcmd;
956
957                 cmd->fcp_cntl = FCP_CNTL_QTYPE_ORDERED | FCP_CNTL_RESET;
958                 fcmd->data = (dma_addr_t)NULL;
959                 fcmd->proto = TYPE_SCSI_FCP;
960
961                 memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
962                 memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
963                 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)))
964         } else {
965                 fcmd = FCP_CMND(fc->rst_pkt);
966                 if (fc->rst_pkt->eh_state == SCSI_STATE_QUEUED)
967                         return FAILED; /* or SUCCESS. Only these */
968         }
969         fc->rst_pkt->done = NULL;
970
971
972         fc->rst_pkt->eh_state = SCSI_STATE_QUEUED;
973         init_timer(&fc->rst_pkt->eh_timeout);
974         fc->rst_pkt->eh_timeout.data = (unsigned long) fc->rst_pkt;
975         fc->rst_pkt->eh_timeout.expires = jiffies + FCP_RESET_TIMEOUT;
976         fc->rst_pkt->eh_timeout.function = (void (*)(unsigned long))fcp_scsi_reset_done;
977
978         add_timer(&fc->rst_pkt->eh_timeout);
979
980         /*
981          * Set up the semaphore so we wait for the command to complete.
982          */
983
984         fc->rst_pkt->device->host->eh_action = &sem;
985         fc->rst_pkt->request->rq_status = RQ_SCSI_BUSY;
986
987         fc->rst_pkt->done = fcp_scsi_reset_done;
988
989         spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
990         fcp_scsi_queue_it(fc, fc->rst_pkt, fcmd, 0);
991         spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
992         
993         down(&sem);
994
995         fc->rst_pkt->device->host->eh_action = NULL;
996         del_timer(&fc->rst_pkt->eh_timeout);
997
998         /*
999          * See if timeout.  If so, tell the host to forget about it.
1000          * In other words, we don't want a callback any more.
1001          */
1002         if (fc->rst_pkt->eh_state == SCSI_STATE_TIMEOUT ) {
1003                 fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
1004                 return FAILED;
1005         }
1006         fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
1007 #endif
1008         return SUCCESS;
1009 }
1010
1011 static int __fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
1012 {
1013         fc_channel *fc = FC_SCMND(SCpnt);
1014         fcp_cmnd *fcmd = FCP_CMND(SCpnt);
1015         int i;
1016
1017         printk ("FC: host reset\n");
1018
1019         for (i=0; i < fc->can_queue; i++) {
1020                 if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
1021                         SCpnt->result = DID_RESET;
1022                         fcmd->done(SCpnt);
1023                         fc->cmd_slots[i] = NULL;
1024                 }
1025         }
1026         fc->reset(fc);
1027         fc->abort_count = 0;
1028         if (fcp_initialize(fc, 1)) return SUCCESS;
1029         else return FAILED;
1030 }
1031
1032 int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
1033 {
1034         unsigned long flags;
1035         int rc;
1036
1037         spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
1038         rc = __fcp_scsi_host_reset(SCpnt);
1039         spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
1040
1041         return rc;
1042 }
1043
1044 static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
1045 {
1046         long i;
1047
1048         i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
1049         set_bit (i, fc->scsi_bitmap);
1050         fcmd->token = i;
1051         fc->scsi_free--;
1052         fc->cmd_slots[fcmd->token] = fcmd;
1053         return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
1054 }
1055
1056 static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
1057 {
1058         fcp_cmnd _fcmd, *fcmd;
1059         fc_hdr *fch;
1060         lse l;
1061         int i;
1062
1063         fcmd = &_fcmd;
1064         memset(fcmd, 0, sizeof(fcmd));
1065         FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
1066         fch = &fcmd->fch;
1067         FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
1068         FILL_FCHDR_SID(fch, fc->sid);
1069         FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
1070         FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
1071         FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
1072         fch->param = 0;
1073         fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
1074         fcmd->rsp = fcmd->cmd + len;
1075         fcmd->cmdlen = len;
1076         fcmd->rsplen = len;
1077         fcmd->data = (dma_addr_t)NULL;
1078         fcmd->fc = fc;
1079         fcmd->class = FC_CLASS_SIMPLE;
1080         fcmd->proto = TYPE_EXTENDED_LS;
1081         
1082         memset (&l, 0, sizeof(lse));
1083         l.magic = LSEMAGIC;
1084         init_MUTEX_LOCKED(&l.sem);
1085         l.timer.function = fcp_login_timeout;
1086         l.timer.data = (unsigned long)&l;
1087         l.status = FC_STATUS_TIMED_OUT;
1088         fcmd->ls = (void *)&l;
1089
1090         disable_irq(fc->irq);   
1091         fcp_els_queue_it(fc, fcmd);
1092         enable_irq(fc->irq);
1093
1094         for (i = 0;;) {
1095                 l.timer.expires = jiffies + 5 * HZ;
1096                 add_timer(&l.timer);
1097                 down(&l.sem);
1098                 del_timer(&l.timer);
1099                 if (l.status != FC_STATUS_TIMED_OUT) break;
1100                 if (++i == 3) break;
1101                 disable_irq(fc->irq);
1102                 fcp_scsi_queue_it(fc, NULL, fcmd, 0);
1103                 enable_irq(fc->irq);
1104         }
1105         
1106         clear_bit(fcmd->token, fc->scsi_bitmap);
1107         fc->scsi_free++;
1108         dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
1109         return l.status;
1110 }
1111
1112 int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
1113 {
1114         logi *l;
1115         int status;
1116
1117         l = (logi *)kmalloc(2 * sizeof(logi), GFP_KERNEL);
1118         if (!l) return -ENOMEM;
1119         memset(l, 0, 2 * sizeof(logi));
1120         l->code = LS_PLOGI;
1121         memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
1122         memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
1123         memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
1124         memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
1125         status = fc_do_els(fc, alpa, l, sizeof(logi));
1126         if (status == FC_STATUS_OK) {
1127                 if (l[1].code == LS_ACC) {
1128 #ifdef FCDEBUG
1129                         u32 *u = (u32 *)&l[1].nport_wwn;
1130                         FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa, 
1131                                 u[0], u[1], u[2], u[3]))
1132 #endif
1133                         memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
1134                         memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
1135                 } else
1136                         status = FC_STATUS_BAD_RSP;
1137         }
1138         kfree(l);
1139         return status;
1140 }
1141
1142 typedef struct {
1143         unsigned int code;
1144         unsigned params[4];
1145 } prli;
1146
1147 int fc_do_prli(fc_channel *fc, unsigned char alpa)
1148 {
1149         prli *p;
1150         int status;
1151
1152         p = (prli *)kmalloc(2 * sizeof(prli), GFP_KERNEL);
1153         if (!p) return -ENOMEM;
1154         memset(p, 0, 2 * sizeof(prli));
1155         p->code = LS_PRLI;
1156         p->params[0] = 0x08002000;
1157         p->params[3] = 0x00000022;
1158         status = fc_do_els(fc, alpa, p, sizeof(prli));
1159         if (status == FC_STATUS_OK && p[1].code != LS_PRLI_ACC && p[1].code != LS_ACC)
1160                 status = FC_STATUS_BAD_RSP;
1161         kfree(p);
1162         return status;
1163 }
1164
1165 MODULE_LICENSE("GPL");
1166