1 /* fc.c: Generic Fibre Channel and FC4 SCSI driver.
3 * Copyright (C) 1997,1998,1999 Jakub Jelinek (jj@ultra.linux.cz)
4 * Copyright (C) 1997,1998 Jirka Hanika (geo@ff.cuni.cz)
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.
15 * This file should support both Point-to-Point and Arbitrated Loop topologies.
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
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/types.h>
28 #include <linux/fcntl.h>
29 #include <linux/interrupt.h>
30 #include <linux/ptrace.h>
31 #include <linux/ioport.h>
33 #include <linux/malloc.h>
34 #include <linux/string.h>
35 #include <linux/init.h>
36 #include <linux/blk.h>
38 #include <asm/pgtable.h>
40 #include <asm/semaphore.h>
42 #include "../scsi/hosts.h"
46 #define fc_printk printk ("%s: ", fc->name); printk
49 #define FCD(x) fc_printk x;
50 #define FCND(x) printk ("FC: "); printk x;
57 static inline void *fc_dma_alloc(long size
, char *name
, dma_handle
*dma
)
59 return (void *) sparc_dvma_malloc (size
, name
, dma
);
62 static inline dma_handle
fc_sync_dma_entry(void *buf
, int len
, fc_channel
*fc
)
64 return mmu_get_scsi_one (buf
, len
, fc
->dev
->my_bus
);
67 static inline void fc_sync_dma_exit(dma_handle dmh
, long size
, fc_channel
*fc
)
69 mmu_release_scsi_one (dmh
, size
, fc
->dev
->my_bus
);
72 static inline void fc_sync_dma_entry_sg(struct scatterlist
*list
, int count
, fc_channel
*fc
)
74 mmu_get_scsi_sgl((struct mmu_sglist
*)list
, count
- 1, fc
->dev
->my_bus
);
77 static inline void fc_sync_dma_exit_sg(struct scatterlist
*list
, int count
, fc_channel
*fc
)
79 mmu_release_scsi_sgl ((struct mmu_sglist
*)list
, count
- 1, fc
->dev
->my_bus
);
85 #define FCP_CMND(SCpnt) ((fcp_cmnd *)&(SCpnt->SCp))
86 #define FC_SCMND(SCpnt) ((fc_channel *)(SCpnt->host->hostdata[0]))
87 #define SC_FCMND(fcmnd) ((Scsi_Cmnd *)((long)fcmnd - (long)&(((Scsi_Cmnd *)0)->SCp)))
89 static int fcp_scsi_queue_it(fc_channel
*, Scsi_Cmnd
*, fcp_cmnd
*, int);
90 void fcp_queue_empty(fc_channel
*);
92 static void fcp_scsi_insert_queue (fc_channel
*fc
, fcp_cmnd
*fcmd
)
99 fc
->scsi_que
->prev
->next
= fcmd
;
100 fcmd
->prev
= fc
->scsi_que
->prev
;
101 fc
->scsi_que
->prev
= fcmd
;
102 fcmd
->next
= fc
->scsi_que
;
106 static void fcp_scsi_remove_queue (fc_channel
*fc
, fcp_cmnd
*fcmd
)
108 if (fcmd
== fcmd
->next
) {
112 if (fcmd
== fc
->scsi_que
)
113 fc
->scsi_que
= fcmd
->next
;
114 fcmd
->prev
->next
= fcmd
->next
;
115 fcmd
->next
->prev
= fcmd
->prev
;
118 fc_channel
*fc_channels
= NULL
;
120 #define LSMAGIC 620829043
123 struct semaphore sem
;
129 struct timer_list timer
;
130 unsigned char grace
[0];
133 #define LSOMAGIC 654907799
136 struct semaphore sem
;
141 struct timer_list timer
;
144 #define LSEMAGIC 84482456
147 struct semaphore sem
;
150 struct timer_list timer
;
153 static void fcp_login_timeout(unsigned long data
)
156 FCND(("Login timeout\n"))
160 static void fcp_login_done(fc_channel
*fc
, int i
, int status
)
165 ls
*l
= (ls
*)fc
->ls
;
167 FCD(("Login done %d %d\n", i
, status
))
169 if (fc
->state
== FC_STATE_FPORT_OK
) {
170 FCD(("Additional FPORT_OK received with status %d\n", status
))
174 case FC_STATUS_OK
: /* Oh, we found a fabric */
175 case FC_STATUS_P_RJT
: /* Oh, we haven't found any */
176 fc
->state
= FC_STATE_FPORT_OK
;
178 plogi
= l
->logi
+ 3 * i
;
179 fc_sync_dma_exit (fcmd
->cmd
, 3 * sizeof(logi
), fc
);
180 plogi
->code
= LS_PLOGI
;
181 memcpy (&plogi
->nport_wwn
, &fc
->wwn_nport
, sizeof(fc_wwn
));
182 memcpy (&plogi
->node_wwn
, &fc
->wwn_node
, sizeof(fc_wwn
));
183 memcpy (&plogi
->common
, fc
->common_svc
, sizeof(common_svc_parm
));
184 memcpy (&plogi
->class1
, fc
->class_svcs
, 3*sizeof(svc_parm
));
186 fcmd
->token
+= l
->count
;
187 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, fc
->did
);
188 FILL_FCHDR_SID(fch
, fc
->sid
);
192 unsigned *x
= (unsigned *)plogi
;
194 for (i
= 0; i
< 21; i
++)
195 printk ("%08x ", x
[i
]);
199 fcmd
->cmd
= fc_sync_dma_entry (plogi
, 3 * sizeof(logi
), fc
);
200 fcmd
->rsp
= fcmd
->cmd
+ 2 * sizeof(logi
);
201 if (fc
->hw_enque (fc
, fcmd
))
202 printk ("FC: Cannot enque PLOGI packet on %s\n", fc
->name
);
204 case FC_STATUS_ERR_OFFLINE
:
205 fc
->state
= FC_STATE_MAYBEOFFLINE
;
206 FCD (("FC is offline %d\n", l
->grace
[i
]))
209 printk ("FLOGI failed for %s with status %d\n", fc
->name
, status
);
210 /* Do some sort of error recovery here */
215 if (fc
->state
!= FC_STATE_FPORT_OK
) {
216 FCD(("Unexpected N-PORT rsp received"))
221 plogi
= l
->logi
+ 3 * i
;
222 fc_sync_dma_exit (l
->fcmds
[i
].cmd
, 3 * sizeof(logi
), fc
);
223 if (!fc
->wwn_dest
.lo
&& !fc
->wwn_dest
.hi
) {
224 memcpy (&fc
->wwn_dest
, &plogi
[1].node_wwn
, sizeof(fc_wwn
));
225 FCD(("Dest WWN %08x%08x\n", *(u32
*)&fc
->wwn_dest
, fc
->wwn_dest
.lo
))
226 } else if (fc
->wwn_dest
.lo
!= plogi
[1].node_wwn
.lo
||
227 fc
->wwn_dest
.hi
!= plogi
[1].node_wwn
.hi
) {
228 printk ("%s: mismatch in wwns. Got %08x%08x, expected %08x%08x\n",
230 *(u32
*)&plogi
[1].node_wwn
, plogi
[1].node_wwn
.lo
,
231 *(u32
*)&fc
->wwn_dest
, fc
->wwn_dest
.lo
);
233 fc
->state
= FC_STATE_ONLINE
;
234 printk ("%s: ONLINE\n", fc
->name
);
235 if (atomic_dec_and_test (&l
->todo
))
238 case FC_STATUS_ERR_OFFLINE
:
239 fc
->state
= FC_STATE_OFFLINE
;
240 fc_sync_dma_exit (l
->fcmds
[i
].cmd
, 3 * sizeof(logi
), fc
);
241 printk ("%s: FC is offline\n", fc
->name
);
242 if (atomic_dec_and_test (&l
->todo
))
246 printk ("PLOGI failed for %s with status %d\n", fc
->name
, status
);
247 /* Do some sort of error recovery here */
253 static void fcp_report_map_done(fc_channel
*fc
, int i
, int status
)
258 ls
*l
= (ls
*)fc
->ls
;
261 FCD(("Report map done %d %d\n", i
, status
))
263 case FC_STATUS_OK
: /* Ok, let's have a fun on a loop */
264 fc_sync_dma_exit (l
->fcmds
[i
].cmd
, 3 * sizeof(logi
), fc
);
265 p
= (fc_al_posmap
*)(l
->logi
+ 3 * i
);
269 FCD(("%08x\n", u
[0]))
271 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]))
274 if ((p
->magic
& 0xffff0000) != FC_AL_LILP
|| !p
->len
) {
275 printk ("FC: Bad magic from REPORT_AL_MAP on %s - %08x\n", fc
->name
, p
->magic
);
276 fc
->state
= FC_STATE_OFFLINE
;
278 fc
->posmap
= (fcp_posmap
*)kmalloc(sizeof(fcp_posmap
)+p
->len
, GFP_KERNEL
);
280 printk("FC: Not enough memory, offlining channel\n");
281 fc
->state
= FC_STATE_OFFLINE
;
284 memset(fc
->posmap
, 0, sizeof(fcp_posmap
)+p
->len
);
285 /* FIXME: This is where SOCAL transfers our AL-PA.
286 Keep it here till we found out what other cards do... */
287 fc
->sid
= (p
->magic
& 0xff);
288 for (i
= 0; i
< p
->len
; i
++)
289 if (p
->alpa
[i
] == fc
->sid
)
298 fc
->posmap
->len
= p
->len
;
299 for (j
= 0; j
< p
->len
; j
++) {
301 fc
->posmap
->list
[j
] = p
->alpa
[i
++];
303 fc
->state
= FC_STATE_ONLINE
;
306 printk ("%s: ONLINE\n", fc
->name
);
307 if (atomic_dec_and_test (&l
->todo
))
310 case FC_STATUS_POINTTOPOINT
: /* We're Point-to-Point, no AL... */
311 FCD(("SID %d DID %d\n", fc
->sid
, fc
->did
))
313 fc_sync_dma_exit(fcmd
->cmd
, 3 * sizeof(logi
), fc
);
315 memset(l
->logi
+ 3 * i
, 0, 3 * sizeof(logi
));
316 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, FS_FABRIC_F_PORT
);
317 FILL_FCHDR_SID(fch
, 0);
318 FILL_FCHDR_TYPE_FCTL(fch
, TYPE_EXTENDED_LS
, F_CTL_FIRST_SEQ
| F_CTL_SEQ_INITIATIVE
);
319 FILL_FCHDR_SEQ_DF_SEQ(fch
, 0, 0, 0);
320 FILL_FCHDR_OXRX(fch
, 0xffff, 0xffff);
322 l
->logi
[3 * i
].code
= LS_FLOGI
;
323 fcmd
->cmd
= fc_sync_dma_entry (l
->logi
+ 3 * i
, 3 * sizeof(logi
), fc
);
324 fcmd
->rsp
= fcmd
->cmd
+ sizeof(logi
);
325 fcmd
->cmdlen
= sizeof(logi
);
326 fcmd
->rsplen
= sizeof(logi
);
327 fcmd
->data
= (dma_handle
)NULL
;
328 fcmd
->class = FC_CLASS_SIMPLE
;
329 fcmd
->proto
= TYPE_EXTENDED_LS
;
330 if (fc
->hw_enque (fc
, fcmd
))
331 printk ("FC: Cannot enque FLOGI packet on %s\n", fc
->name
);
333 case FC_STATUS_ERR_OFFLINE
:
334 fc
->state
= FC_STATE_MAYBEOFFLINE
;
335 FCD (("FC is offline %d\n", l
->grace
[i
]))
338 printk ("FLOGI failed for %s with status %d\n", fc
->name
, status
);
339 /* Do some sort of error recovery here */
344 void fcp_register(fc_channel
*fc
, u8 type
, int unregister
)
347 int slots
= (fc
->can_queue
* 3) >> 1;
349 FCND(("Going to %sregister\n", unregister
? "un" : ""))
351 if (type
== TYPE_SCSI_FCP
) {
354 (fcp_cmd
*) fc_dma_alloc (slots
* (sizeof (fcp_cmd
) + fc
->rsp_size
),
355 "FCP SCSI cmd & rsp queues", &fc
->dma_scsi_cmd
);
356 fc
->scsi_rsp_pool
= (char *)(fc
->scsi_cmd_pool
+ slots
);
357 fc
->dma_scsi_rsp
= fc
->dma_scsi_cmd
+ slots
* sizeof (fcp_cmd
);
358 fc
->scsi_bitmap_end
= (slots
+ 63) & ~63;
359 size
= fc
->scsi_bitmap_end
/ 8;
360 fc
->scsi_bitmap
= kmalloc (size
, GFP_KERNEL
);
361 memset (fc
->scsi_bitmap
, 0, size
);
362 set_bit (0, fc
->scsi_bitmap
);
363 for (i
= fc
->can_queue
; i
< fc
->scsi_bitmap_end
; i
++)
364 set_bit (i
, fc
->scsi_bitmap
);
365 fc
->scsi_free
= fc
->can_queue
;
366 fc
->cmd_slots
= (fcp_cmnd
**)kmalloc(slots
* sizeof(fcp_cmnd
*), GFP_KERNEL
);
367 memset(fc
->cmd_slots
, 0, slots
* sizeof(fcp_cmnd
*));
370 fc
->scsi_name
[0] = 0;
371 kfree (fc
->scsi_bitmap
);
372 kfree (fc
->cmd_slots
);
373 FCND(("Unregistering\n"));
375 if (fc
->rst_pkt
->eh_state
== SCSI_STATE_UNUSED
)
378 /* Can't happen. Some memory would be lost. */
379 printk("FC: Reset in progress. Now?!");
382 FCND(("Unregistered\n"));
385 printk ("FC: %segistering unknown type %02x\n", unregister
? "Unr" : "R", type
);
388 static void fcp_scsi_done(Scsi_Cmnd
*SCpnt
);
390 static inline void fcp_scsi_receive(fc_channel
*fc
, int token
, int status
, fc_hdr
*fch
)
399 fcmd
= fc
->cmd_slots
[token
];
401 rsp
= (fcp_rsp
*) (fc
->scsi_rsp_pool
+ fc
->rsp_size
* token
);
402 SCpnt
= SC_FCMND(fcmd
);
404 if (SCpnt
->done
!= fcp_scsi_done
)
407 rsp_status
= rsp
->fcp_status
;
408 FCD(("rsp_status %08x status %08x\n", rsp_status
, status
))
413 if (rsp_status
& FCP_STATUS_RESID
) {
415 FCD(("Resid %d\n", rsp
->fcp_resid
))
417 fcp_cmd
*cmd
= fc
->scsi_cmd_pool
+ token
;
421 for (i
= 0; i
< sizeof(fcp_cmd
); i
+=4)
422 printk ("%08x ", *(u32
*)(((char *)cmd
)+i
));
423 printk ("\nResponse ");
424 for (i
= 0; i
< fc
->rsp_size
; i
+=4)
425 printk ("%08x ", *(u32
*)(((char *)rsp
)+i
));
431 if (rsp_status
& FCP_STATUS_SENSE_LEN
) {
432 sense_len
= rsp
->fcp_sense_len
;
433 if (sense_len
> sizeof(SCpnt
->sense_buffer
)) sense_len
= sizeof(SCpnt
->sense_buffer
);
434 memcpy(SCpnt
->sense_buffer
, ((char *)(rsp
+1)), sense_len
);
439 fc_sync_dma_exit_sg((struct scatterlist
*)SCpnt
->buffer
, SCpnt
->use_sg
, fc
);
441 fc_sync_dma_exit(fcmd
->data
, SCpnt
->request_bufflen
, fc
);
445 host_status
=DID_ERROR
; /* FIXME */
446 FCD(("Wrong FC status %d for token %d\n", status
, token
))
450 if (status_byte(rsp_status
) == QUEUE_FULL
) {
451 printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc
->name
, SCpnt
->channel
, SCpnt
->target
, rsp_status
);
454 SCpnt
->result
= (host_status
<< 16) | (rsp_status
& 0xff);
456 if (host_status
|| SCpnt
->result
|| rsp_status
) printk("FC: host_status %d, packet status %d\n",
457 host_status
, SCpnt
->result
);
459 SCpnt
->done
= fcmd
->done
;
461 clear_bit(token
, fc
->scsi_bitmap
);
463 FCD(("Calling scsi_done with %08x\n", SCpnt
->result
))
464 SCpnt
->scsi_done(SCpnt
);
467 void fcp_receive_solicited(fc_channel
*fc
, int proto
, int token
, int status
, fc_hdr
*fch
)
470 FCD(("receive_solicited %d %d %d\n", proto
, token
, status
))
473 fcp_scsi_receive(fc
, token
, status
, fch
); break;
474 case TYPE_EXTENDED_LS
:
475 case PROTO_REPORT_AL_MAP
:
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
;
484 if ((unsigned)i
< l
->count
&& l
->fcmds
[i
].fc
== fc
) {
485 if (proto
== TYPE_EXTENDED_LS
)
486 fcp_login_done(fc
, token
, status
);
488 fcp_report_map_done(fc
, token
, status
);
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
) {
496 fcmd
= fc
->cmd_slots
[token
];
497 if (fcmd
&& fcmd
->ls
&& ((ls
*)(fcmd
->ls
))->magic
== LSEMAGIC
) {
498 lse
*l
= (lse
*)fcmd
->ls
;
506 if (fc
->ls
&& ((lso
*)(fc
->ls
))->magic
== LSOMAGIC
) {
507 lso
*l
= (lso
*)fc
->ls
;
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
))
524 void fcp_state_change(fc_channel
*fc
, int state
)
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
);
532 printk (KERN_ERR
"%s: state change to OFFLINE\n", fc
->name
);
535 int fcp_initialize(fc_channel
*fcchain
, int count
)
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
);
547 printk ("FC: Cannot allocate memory for initialization\n");
550 memset (l
, 0, sizeof(ls
) + count
);
553 FCND(("FCP Init for %d channels\n", count
))
554 init_MUTEX_LOCKED(&l
->sem
);
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_DMA
);
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
);
564 printk ("FC: Cannot allocate DMA memory for initialization\n");
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 */
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
++) {
579 /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
580 fcmd
->cmd
= fc_sync_dma_entry (l
->logi
+ 3 * i
, 3 * sizeof(logi
), fc
);
581 fcmd
->proto
= PROTO_REPORT_AL_MAP
;
585 for (retry
= 0; retry
< 8; retry
++) {
587 FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
588 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
589 if (fc
->state
== FC_STATE_ONLINE
|| fc
->state
== FC_STATE_OFFLINE
)
591 disable_irq(fc
->irq
);
592 if (fc
->state
== FC_STATE_MAYBEOFFLINE
) {
597 fc
->state
= FC_STATE_OFFLINE
;
599 fc_sync_dma_exit (l
->fcmds
[i
].cmd
, 3 * sizeof(logi
), fc
);
600 if (atomic_dec_and_test (&l
->todo
))
604 ret
= fc
->hw_enque (fc
, fc
->login
);
610 if (ret
== -ENOSYS
&& fc
->login
->proto
== PROTO_REPORT_AL_MAP
) {
611 /* Oh yes, this card handles Point-to-Point only, so let's try that. */
614 FCD(("SID %d DID %d\n", fc
->sid
, fc
->did
))
616 fc_sync_dma_exit(fcmd
->cmd
, 3 * sizeof(logi
), fc
);
618 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, FS_FABRIC_F_PORT
);
619 FILL_FCHDR_SID(fch
, 0);
620 FILL_FCHDR_TYPE_FCTL(fch
, TYPE_EXTENDED_LS
, F_CTL_FIRST_SEQ
| F_CTL_SEQ_INITIATIVE
);
621 FILL_FCHDR_SEQ_DF_SEQ(fch
, 0, 0, 0);
622 FILL_FCHDR_OXRX(fch
, 0xffff, 0xffff);
624 l
->logi
[3 * i
].code
= LS_FLOGI
;
625 fcmd
->cmd
= fc_sync_dma_entry (l
->logi
+ 3 * i
, 3 * sizeof(logi
), fc
);
626 fcmd
->rsp
= fcmd
->cmd
+ sizeof(logi
);
627 fcmd
->cmdlen
= sizeof(logi
);
628 fcmd
->rsplen
= sizeof(logi
);
629 fcmd
->data
= (dma_handle
)NULL
;
630 fcmd
->class = FC_CLASS_SIMPLE
;
631 fcmd
->proto
= TYPE_EXTENDED_LS
;
633 printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc
->name
);
637 l
->timer
.expires
= jiffies
+ 5 * HZ
;
638 add_timer(&l
->timer
);
641 if (!atomic_read(&l
->todo
)) {
642 FCND(("All channels answered in time\n"))
643 break; /* All fc channels have answered us */
648 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
651 case FC_STATE_ONLINE
: break;
652 case FC_STATE_OFFLINE
: break;
653 default: fc_sync_dma_exit (l
->fcmds
[i
].cmd
, 3 * sizeof(logi
), fc
);
657 del_timer(&l
->timer
);
664 int fcp_forceoffline(fc_channel
*fcchain
, int count
)
671 memset (&l
, 0, sizeof(lso
));
674 FCND(("FCP Force Offline for %d channels\n", count
))
675 init_MUTEX_LOCKED(&l
.sem
);
676 l
.timer
.function
= fcp_login_timeout
;
677 l
.timer
.data
= (unsigned long)&l
;
678 atomic_set (&l
.todo
, count
);
679 l
.fcmds
= kmalloc (count
* sizeof(fcp_cmnd
), GFP_KERNEL
);
682 printk ("FC: Cannot allocate memory for forcing offline\n");
685 memset (l
.fcmds
, 0, count
* sizeof(fcp_cmnd
));
686 FCND(("Initializing OFFLINE packets\n"))
687 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
688 fc
->state
= FC_STATE_UNINITED
;
693 fcmd
->class = FC_CLASS_OFFLINE
;
694 fcmd
->proto
= PROTO_OFFLINE
;
697 disable_irq(fc
->irq
);
698 ret
= fc
->hw_enque (fc
, fc
->login
);
700 if (ret
) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc
->name
);
703 l
.timer
.expires
= jiffies
+ 5 * HZ
;
708 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++)
714 int fcp_init(fc_channel
*fcchain
)
720 for (fc
= fcchain
; fc
; fc
= fc
->next
) {
721 fc
->fcp_register
= fcp_register
;
725 ret
= fcp_initialize (fcchain
, count
);
730 fc_channels
= fcchain
;
732 for (fc
= fc_channels
; fc
->next
; fc
= fc
->next
);
738 void fcp_release(fc_channel
*fcchain
, int count
) /* count must > 0 */
743 for (fc
= fcchain
; --count
&& fc
->next
; fc
= fc
->next
);
745 printk("FC: nothing to release\n");
749 if (fc_channels
== fcchain
)
750 fc_channels
= fc
->next
;
752 for (fcx
= fc_channels
; fcx
->next
!= fcchain
; fcx
= fcx
->next
);
753 fcx
->next
= fc
->next
;
758 * We've just grabbed fcchain out of the fc_channel list
759 * and zero-terminated it, while destroying the count.
761 * Freeing the fc's is the low level driver's responsibility.
766 static void fcp_scsi_done (Scsi_Cmnd
*SCpnt
)
768 if (FCP_CMND(SCpnt
)->done
)
769 FCP_CMND(SCpnt
)->done(SCpnt
);
772 static int fcp_scsi_queue_it(fc_channel
*fc
, Scsi_Cmnd
*SCpnt
, fcp_cmnd
*fcmd
, int prepare
)
778 i
= find_first_zero_bit (fc
->scsi_bitmap
, fc
->scsi_bitmap_end
);
779 set_bit (i
, fc
->scsi_bitmap
);
781 cmd
= fc
->scsi_cmd_pool
+ i
;
783 if (fc
->encode_addr (SCpnt
, cmd
->fcp_addr
, fc
, fcmd
)) {
784 /* Invalid channel/id/lun and couldn't map it into fcp_addr */
785 clear_bit (i
, fc
->scsi_bitmap
);
786 SCpnt
->result
= (DID_BAD_TARGET
<< 16);
787 SCpnt
->scsi_done(SCpnt
);
791 fc
->cmd_slots
[fcmd
->token
] = fcmd
;
793 if (SCpnt
->device
->tagged_supported
) {
794 if (jiffies
- fc
->ages
[SCpnt
->channel
* fc
->targets
+ SCpnt
->target
] > (5 * 60 * HZ
)) {
795 fc
->ages
[SCpnt
->channel
* fc
->targets
+ SCpnt
->target
] = jiffies
;
796 fcp_cntl
= FCP_CNTL_QTYPE_ORDERED
;
798 fcp_cntl
= FCP_CNTL_QTYPE_SIMPLE
;
800 fcp_cntl
= FCP_CNTL_QTYPE_UNTAGGED
;
801 if (!SCpnt
->request_bufflen
&& !SCpnt
->use_sg
) {
802 cmd
->fcp_cntl
= fcp_cntl
;
803 fcmd
->data
= (dma_handle
)NULL
;
805 switch (SCpnt
->cmnd
[0]) {
809 cmd
->fcp_cntl
= (FCP_CNTL_WRITE
| fcp_cntl
); break;
811 cmd
->fcp_cntl
= (FCP_CNTL_READ
| fcp_cntl
); break;
813 if (!SCpnt
->use_sg
) {
814 cmd
->fcp_data_len
= SCpnt
->request_bufflen
;
815 fcmd
->data
= fc_sync_dma_entry ((char *)SCpnt
->request_buffer
,
816 SCpnt
->request_bufflen
, fc
);
818 struct scatterlist
*sg
= (struct scatterlist
*)SCpnt
->buffer
;
820 FCD(("XXX: Use_sg %d %d\n", SCpnt
->use_sg
, sg
->length
))
821 if (SCpnt
->use_sg
> 1) printk ("%s: SG for use_sg > 1 not handled yet\n", fc
->name
);
822 fc_sync_dma_entry_sg (sg
, SCpnt
->use_sg
, fc
);
823 fcmd
->data
= sg
->dvma_address
;
824 cmd
->fcp_data_len
= sg
->length
;
827 memcpy (cmd
->fcp_cdb
, SCpnt
->cmnd
, SCpnt
->cmd_len
);
828 memset (cmd
->fcp_cdb
+SCpnt
->cmd_len
, 0, sizeof(cmd
->fcp_cdb
)-SCpnt
->cmd_len
);
829 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)))
831 FCD(("Trying to enque %p\n", fcmd
))
833 if (!fc
->hw_enque (fc
, fcmd
)) {
834 FCD(("hw_enque succeeded for %p\n", fcmd
))
838 FCD(("Putting into que1 %p\n", fcmd
))
839 fcp_scsi_insert_queue (fc
, fcmd
);
843 int fcp_scsi_queuecommand(Scsi_Cmnd
*SCpnt
, void (* done
)(Scsi_Cmnd
*))
845 fcp_cmnd
*fcmd
= FCP_CMND(SCpnt
);
846 fc_channel
*fc
= FC_SCMND(SCpnt
);
848 FCD(("Entering SCSI queuecommand %p\n", fcmd
))
849 if (SCpnt
->done
!= fcp_scsi_done
) {
850 fcmd
->done
= SCpnt
->done
;
851 SCpnt
->done
= fcp_scsi_done
;
852 SCpnt
->scsi_done
= done
;
853 fcmd
->proto
= TYPE_SCSI_FCP
;
854 if (!fc
->scsi_free
) {
855 FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
856 #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
857 printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
861 return fcp_scsi_queue_it(fc
, SCpnt
, fcmd
, 1);
863 return fcp_scsi_queue_it(fc
, SCpnt
, fcmd
, 0);
866 void fcp_queue_empty(fc_channel
*fc
)
870 FCD(("Queue empty\n"))
871 while ((fcmd
= fc
->scsi_que
)) {
872 /* The hw told us we can try again queue some packet */
873 if (fc
->hw_enque (fc
, fcmd
))
875 fcp_scsi_remove_queue (fc
, fcmd
);
879 int fcp_old_abort(Scsi_Cmnd
*SCpnt
)
881 printk("FC: Abort not implemented\n");
885 int fcp_scsi_abort(Scsi_Cmnd
*SCpnt
)
887 /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
888 fcp_cmnd
*fcmd
= FCP_CMND(SCpnt
);
889 fc_channel
*fc
= FC_SCMND(SCpnt
);
892 * We react to abort requests by simply forgetting
893 * about the command and pretending everything's sweet.
894 * This may or may not be silly. We can't, however,
895 * immediately reuse the command's cmd_slots slot,
896 * as its result may arrive later and we cannot
897 * check whether it is the aborted one, can't we?
899 * Therefore, after the first few aborts are done,
900 * we tell the scsi error handler to do something clever.
901 * It will eventually call host reset, refreshing
904 * There is a theoretical chance that we sometimes allow
905 * more than can_queue packets to the jungle this way,
906 * but the worst outcome possible is a series of
907 * more aborts and eventually the dev_reset catharsis.
910 if (++fc
->abort_count
< (fc
->can_queue
>> 1)) {
911 SCpnt
->result
= DID_ABORT
;
913 printk("FC: soft abort\n");
916 printk("FC: hard abort refused\n");
921 void fcp_scsi_reset_done(Scsi_Cmnd
*SCpnt
)
923 fc_channel
*fc
= FC_SCMND(SCpnt
);
925 fc
->rst_pkt
->eh_state
= SCSI_STATE_FINISHED
;
926 up(fc
->rst_pkt
->host
->eh_action
);
929 #define FCP_RESET_TIMEOUT (2*HZ)
931 int fcp_scsi_dev_reset(Scsi_Cmnd
*SCpnt
)
935 fc_channel
*fc
= FC_SCMND(SCpnt
);
936 DECLARE_MUTEX_LOCKED(sem
);
939 fc
->rst_pkt
= (Scsi_Cmnd
*) kmalloc(sizeof(SCpnt
), GFP_KERNEL
);
940 if (!fc
->rst_pkt
) return FAILED
;
942 fcmd
= FCP_CMND(fc
->rst_pkt
);
946 cmd
= fc
->scsi_cmd_pool
+ 0;
947 FCD(("Preparing rst packet\n"))
948 fc
->encode_addr (SCpnt
, cmd
->fcp_addr
, fc
, fcmd
);
949 fc
->rst_pkt
->channel
= SCpnt
->channel
;
950 fc
->rst_pkt
->target
= SCpnt
->target
;
951 fc
->rst_pkt
->lun
= 0;
952 fc
->rst_pkt
->cmd_len
= 0;
954 fc
->cmd_slots
[0] = fcmd
;
956 cmd
->fcp_cntl
= FCP_CNTL_QTYPE_ORDERED
| FCP_CNTL_RESET
;
957 fcmd
->data
= (dma_handle
)NULL
;
958 fcmd
->proto
= TYPE_SCSI_FCP
;
960 memcpy (cmd
->fcp_cdb
, SCpnt
->cmnd
, SCpnt
->cmd_len
);
961 memset (cmd
->fcp_cdb
+SCpnt
->cmd_len
, 0, sizeof(cmd
->fcp_cdb
)-SCpnt
->cmd_len
);
962 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 fcmd
= FCP_CMND(fc
->rst_pkt
);
965 if (fc
->rst_pkt
->eh_state
== SCSI_STATE_QUEUED
)
966 return FAILED
; /* or SUCCESS. Only these */
968 fc
->rst_pkt
->done
= NULL
;
971 fc
->rst_pkt
->eh_state
= SCSI_STATE_QUEUED
;
973 fc
->rst_pkt
->eh_timeout
.data
= (unsigned long) fc
->rst_pkt
;
974 fc
->rst_pkt
->eh_timeout
.expires
= jiffies
+ FCP_RESET_TIMEOUT
;
975 fc
->rst_pkt
->eh_timeout
.function
= (void (*)(unsigned long))fcp_scsi_reset_done
;
977 add_timer(&fc
->rst_pkt
->eh_timeout
);
980 * Set up the semaphore so we wait for the command to complete.
983 fc
->rst_pkt
->host
->eh_action
= &sem
;
984 fc
->rst_pkt
->request
.rq_status
= RQ_SCSI_BUSY
;
986 fc
->rst_pkt
->done
= fcp_scsi_reset_done
;
987 fcp_scsi_queue_it(fc
, fc
->rst_pkt
, fcmd
, 0);
991 fc
->rst_pkt
->host
->eh_action
= NULL
;
992 del_timer(&fc
->rst_pkt
->eh_timeout
);
995 * See if timeout. If so, tell the host to forget about it.
996 * In other words, we don't want a callback any more.
998 if (fc
->rst_pkt
->eh_state
== SCSI_STATE_TIMEOUT
) {
999 fc
->rst_pkt
->eh_state
= SCSI_STATE_UNUSED
;
1002 fc
->rst_pkt
->eh_state
= SCSI_STATE_UNUSED
;
1006 int fcp_scsi_bus_reset(Scsi_Cmnd
*SCpnt
)
1008 printk ("FC: bus reset!\n");
1012 int fcp_scsi_host_reset(Scsi_Cmnd
*SCpnt
)
1014 fc_channel
*fc
= FC_SCMND(SCpnt
);
1015 fcp_cmnd
*fcmd
= FCP_CMND(SCpnt
);
1018 printk ("FC: host reset\n");
1020 for (i
=0; i
< fc
->can_queue
; i
++) {
1021 if (fc
->cmd_slots
[i
] && SCpnt
->result
!= DID_ABORT
) {
1022 SCpnt
->result
= DID_RESET
;
1024 fc
->cmd_slots
[i
] = NULL
;
1028 fc
->abort_count
= 0;
1029 if (fcp_initialize(fc
, 1)) return SUCCESS
;
1033 static int fcp_els_queue_it(fc_channel
*fc
, fcp_cmnd
*fcmd
)
1037 i
= find_first_zero_bit (fc
->scsi_bitmap
, fc
->scsi_bitmap_end
);
1038 set_bit (i
, fc
->scsi_bitmap
);
1041 fc
->cmd_slots
[fcmd
->token
] = fcmd
;
1042 return fcp_scsi_queue_it(fc
, NULL
, fcmd
, 0);
1045 static int fc_do_els(fc_channel
*fc
, unsigned int alpa
, void *data
, int len
)
1047 fcp_cmnd _fcmd
, *fcmd
;
1053 memset(fcmd
, 0, sizeof(fcmd
));
1054 FCD(("PLOGI SID %d DID %d\n", fc
->sid
, alpa
))
1056 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, alpa
);
1057 FILL_FCHDR_SID(fch
, fc
->sid
);
1058 FILL_FCHDR_TYPE_FCTL(fch
, TYPE_EXTENDED_LS
, F_CTL_FIRST_SEQ
| F_CTL_SEQ_INITIATIVE
);
1059 FILL_FCHDR_SEQ_DF_SEQ(fch
, 0, 0, 0);
1060 FILL_FCHDR_OXRX(fch
, 0xffff, 0xffff);
1062 fcmd
->cmd
= fc_sync_dma_entry (data
, 2 * len
, fc
);
1063 fcmd
->rsp
= fcmd
->cmd
+ len
;
1066 fcmd
->data
= (dma_handle
)NULL
;
1068 fcmd
->class = FC_CLASS_SIMPLE
;
1069 fcmd
->proto
= TYPE_EXTENDED_LS
;
1071 memset (&l
, 0, sizeof(lse
));
1073 init_MUTEX_LOCKED(&l
.sem
);
1074 l
.timer
.function
= fcp_login_timeout
;
1075 l
.timer
.data
= (unsigned long)&l
;
1076 l
.status
= FC_STATUS_TIMED_OUT
;
1077 fcmd
->ls
= (void *)&l
;
1079 disable_irq(fc
->irq
);
1080 fcp_els_queue_it(fc
, fcmd
);
1081 enable_irq(fc
->irq
);
1084 l
.timer
.expires
= jiffies
+ 5 * HZ
;
1085 add_timer(&l
.timer
);
1087 del_timer(&l
.timer
);
1088 if (l
.status
!= FC_STATUS_TIMED_OUT
) break;
1089 if (++i
== 3) break;
1090 disable_irq(fc
->irq
);
1091 fcp_scsi_queue_it(fc
, NULL
, fcmd
, 0);
1092 enable_irq(fc
->irq
);
1095 clear_bit(fcmd
->token
, fc
->scsi_bitmap
);
1097 fc_sync_dma_exit (fcmd
->cmd
, 2 * len
, fc
);
1101 int fc_do_plogi(fc_channel
*fc
, unsigned char alpa
, fc_wwn
*node
, fc_wwn
*nport
)
1106 l
= (logi
*)kmalloc(2 * sizeof(logi
), GFP_DMA
);
1107 if (!l
) return -ENOMEM
;
1108 memset(l
, 0, 2 * sizeof(logi
));
1110 memcpy (&l
->nport_wwn
, &fc
->wwn_nport
, sizeof(fc_wwn
));
1111 memcpy (&l
->node_wwn
, &fc
->wwn_node
, sizeof(fc_wwn
));
1112 memcpy (&l
->common
, fc
->common_svc
, sizeof(common_svc_parm
));
1113 memcpy (&l
->class1
, fc
->class_svcs
, 3*sizeof(svc_parm
));
1114 status
= fc_do_els(fc
, alpa
, l
, sizeof(logi
));
1115 if (status
== FC_STATUS_OK
) {
1116 if (l
[1].code
== LS_ACC
) {
1118 u32
*u
= (u32
*)&l
[1].nport_wwn
;
1119 FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa
,
1120 u
[0], u
[1], u
[2], u
[3]))
1122 memcpy(nport
, &l
[1].nport_wwn
, sizeof(fc_wwn
));
1123 memcpy(node
, &l
[1].node_wwn
, sizeof(fc_wwn
));
1125 status
= FC_STATUS_BAD_RSP
;
1136 int fc_do_prli(fc_channel
*fc
, unsigned char alpa
)
1141 p
= (prli
*)kmalloc(2 * sizeof(prli
), GFP_DMA
);
1142 if (!p
) return -ENOMEM
;
1143 memset(p
, 0, 2 * sizeof(prli
));
1145 p
->params
[0] = 0x08002000;
1146 p
->params
[3] = 0x00000022;
1147 status
= fc_do_els(fc
, alpa
, p
, sizeof(prli
));
1148 if (status
== FC_STATUS_OK
&& p
[1].code
!= LS_PRLI_ACC
&& p
[1].code
!= LS_ACC
)
1149 status
= FC_STATUS_BAD_RSP
;
1155 int init_module(void)
1160 void cleanup_module(void)