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/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>
33 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/init.h>
37 #include <asm/pgtable.h>
39 #include <asm/semaphore.h>
41 #include <scsi/scsi_host.h>
45 #define fc_printk printk ("%s: ", fc->name); printk
48 #define FCD(x) fc_printk x;
49 #define FCND(x) printk ("FC: "); printk x;
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)
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)
71 #define FCP_CMND(SCpnt) ((fcp_cmnd *)&(SCpnt->SCp))
72 #define FC_SCMND(SCpnt) ((fc_channel *)(SCpnt->device->host->hostdata[0]))
73 #define SC_FCMND(fcmnd) ((struct scsi_cmnd *)((long)fcmnd - (long)&(((struct scsi_cmnd *)0)->SCp)))
75 static int fcp_scsi_queue_it(fc_channel
*, struct scsi_cmnd
*, fcp_cmnd
*, int);
76 void fcp_queue_empty(fc_channel
*);
78 static void fcp_scsi_insert_queue (fc_channel
*fc
, fcp_cmnd
*fcmd
)
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
;
92 static void fcp_scsi_remove_queue (fc_channel
*fc
, fcp_cmnd
*fcmd
)
94 if (fcmd
== fcmd
->next
) {
98 if (fcmd
== fc
->scsi_que
)
99 fc
->scsi_que
= fcmd
->next
;
100 fcmd
->prev
->next
= fcmd
->next
;
101 fcmd
->next
->prev
= fcmd
->prev
;
104 fc_channel
*fc_channels
= NULL
;
106 #define LSMAGIC 620829043
109 struct semaphore sem
;
115 struct timer_list timer
;
116 unsigned char grace
[0];
119 #define LSOMAGIC 654907799
122 struct semaphore sem
;
127 struct timer_list timer
;
130 #define LSEMAGIC 84482456
133 struct semaphore sem
;
136 struct timer_list timer
;
139 static void fcp_login_timeout(unsigned long data
)
142 FCND(("Login timeout\n"))
146 static void fcp_login_done(fc_channel
*fc
, int i
, int status
)
151 ls
*l
= (ls
*)fc
->ls
;
153 FCD(("Login done %d %d\n", i
, status
))
155 if (fc
->state
== FC_STATE_FPORT_OK
) {
156 FCD(("Additional FPORT_OK received with status %d\n", 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
;
164 plogi
= l
->logi
+ 3 * i
;
165 dma_unmap_single (fc
->dev
, fcmd
->cmd
, 3 * sizeof(logi
),
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
));
173 fcmd
->token
+= l
->count
;
174 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, fc
->did
);
175 FILL_FCHDR_SID(fch
, fc
->sid
);
179 unsigned *x
= (unsigned *)plogi
;
181 for (i
= 0; i
< 21; i
++)
182 printk ("%08x ", x
[i
]);
186 fcmd
->cmd
= dma_map_single (fc
->dev
, plogi
, 3 * sizeof(logi
),
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
);
192 case FC_STATUS_ERR_OFFLINE
:
193 fc
->state
= FC_STATE_MAYBEOFFLINE
;
194 FCD (("FC is offline %d\n", l
->grace
[i
]))
197 printk ("FLOGI failed for %s with status %d\n", fc
->name
, status
);
198 /* Do some sort of error recovery here */
203 if (fc
->state
!= FC_STATE_FPORT_OK
) {
204 FCD(("Unexpected N-PORT rsp received"))
209 plogi
= l
->logi
+ 3 * i
;
210 dma_unmap_single (fc
->dev
, l
->fcmds
[i
].cmd
, 3 * sizeof(logi
),
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",
219 *(u32
*)&plogi
[1].node_wwn
, plogi
[1].node_wwn
.lo
,
220 *(u32
*)&fc
->wwn_dest
, fc
->wwn_dest
.lo
);
222 fc
->state
= FC_STATE_ONLINE
;
223 printk ("%s: ONLINE\n", fc
->name
);
224 if (atomic_dec_and_test (&l
->todo
))
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
),
231 printk ("%s: FC is offline\n", fc
->name
);
232 if (atomic_dec_and_test (&l
->todo
))
236 printk ("PLOGI failed for %s with status %d\n", fc
->name
, status
);
237 /* Do some sort of error recovery here */
243 static void fcp_report_map_done(fc_channel
*fc
, int i
, int status
)
248 ls
*l
= (ls
*)fc
->ls
;
251 FCD(("Report map done %d %d\n", i
, 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
),
256 p
= (fc_al_posmap
*)(l
->logi
+ 3 * i
);
260 FCD(("%08x\n", u
[0]))
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]))
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
;
269 fc
->posmap
= kzalloc(sizeof(fcp_posmap
)+p
->len
, GFP_KERNEL
);
271 printk("FC: Not enough memory, offlining channel\n");
272 fc
->state
= FC_STATE_OFFLINE
;
275 /* FIXME: This is where SOCAL transfers our AL-PA.
276 Keep it here till we found out what other cards do... */
277 fc
->sid
= (p
->magic
& 0xff);
278 for (i
= 0; i
< p
->len
; i
++)
279 if (p
->alpa
[i
] == fc
->sid
)
288 fc
->posmap
->len
= p
->len
;
289 for (j
= 0; j
< p
->len
; j
++) {
291 fc
->posmap
->list
[j
] = p
->alpa
[i
++];
293 fc
->state
= FC_STATE_ONLINE
;
296 printk ("%s: ONLINE\n", fc
->name
);
297 if (atomic_dec_and_test (&l
->todo
))
300 case FC_STATUS_POINTTOPOINT
: /* We're Point-to-Point, no AL... */
301 FCD(("SID %d DID %d\n", fc
->sid
, fc
->did
))
303 dma_unmap_single(fc
->dev
, fcmd
->cmd
, 3 * sizeof(logi
),
306 memset(l
->logi
+ 3 * i
, 0, 3 * sizeof(logi
));
307 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, FS_FABRIC_F_PORT
);
308 FILL_FCHDR_SID(fch
, 0);
309 FILL_FCHDR_TYPE_FCTL(fch
, TYPE_EXTENDED_LS
, F_CTL_FIRST_SEQ
| F_CTL_SEQ_INITIATIVE
);
310 FILL_FCHDR_SEQ_DF_SEQ(fch
, 0, 0, 0);
311 FILL_FCHDR_OXRX(fch
, 0xffff, 0xffff);
313 l
->logi
[3 * i
].code
= LS_FLOGI
;
314 fcmd
->cmd
= dma_map_single (fc
->dev
, l
->logi
+ 3 * i
, 3 * sizeof(logi
),
316 fcmd
->rsp
= fcmd
->cmd
+ sizeof(logi
);
317 fcmd
->cmdlen
= sizeof(logi
);
318 fcmd
->rsplen
= sizeof(logi
);
319 fcmd
->data
= (dma_addr_t
)NULL
;
320 fcmd
->class = FC_CLASS_SIMPLE
;
321 fcmd
->proto
= TYPE_EXTENDED_LS
;
322 if (fc
->hw_enque (fc
, fcmd
))
323 printk ("FC: Cannot enque FLOGI packet on %s\n", fc
->name
);
325 case FC_STATUS_ERR_OFFLINE
:
326 fc
->state
= FC_STATE_MAYBEOFFLINE
;
327 FCD (("FC is offline %d\n", l
->grace
[i
]))
330 printk ("FLOGI failed for %s with status %d\n", fc
->name
, status
);
331 /* Do some sort of error recovery here */
336 void fcp_register(fc_channel
*fc
, u8 type
, int unregister
)
339 int slots
= (fc
->can_queue
* 3) >> 1;
341 FCND(("Going to %sregister\n", unregister
? "un" : ""))
343 if (type
== TYPE_SCSI_FCP
) {
345 fc
->scsi_cmd_pool
= (fcp_cmd
*)
346 dma_alloc_consistent (fc
->dev
,
347 slots
* (sizeof (fcp_cmd
) + fc
->rsp_size
),
349 fc
->scsi_rsp_pool
= (char *)(fc
->scsi_cmd_pool
+ slots
);
350 fc
->dma_scsi_rsp
= fc
->dma_scsi_cmd
+ slots
* sizeof (fcp_cmd
);
351 fc
->scsi_bitmap_end
= (slots
+ 63) & ~63;
352 size
= fc
->scsi_bitmap_end
/ 8;
353 fc
->scsi_bitmap
= kzalloc (size
, GFP_KERNEL
);
354 set_bit (0, fc
->scsi_bitmap
);
355 for (i
= fc
->can_queue
; i
< fc
->scsi_bitmap_end
; i
++)
356 set_bit (i
, fc
->scsi_bitmap
);
357 fc
->scsi_free
= fc
->can_queue
;
358 fc
->cmd_slots
= kzalloc(slots
* sizeof(fcp_cmnd
*), GFP_KERNEL
);
361 fc
->scsi_name
[0] = 0;
362 kfree (fc
->scsi_bitmap
);
363 kfree (fc
->cmd_slots
);
364 FCND(("Unregistering\n"));
367 if (fc
->rst_pkt
->eh_state
== SCSI_STATE_UNUSED
)
370 /* Can't happen. Some memory would be lost. */
371 printk("FC: Reset in progress. Now?!");
375 FCND(("Unregistered\n"));
378 printk ("FC: %segistering unknown type %02x\n", unregister
? "Unr" : "R", type
);
381 static void fcp_scsi_done(struct scsi_cmnd
*SCpnt
);
383 static inline void fcp_scsi_receive(fc_channel
*fc
, int token
, int status
, fc_hdr
*fch
)
388 struct scsi_cmnd
*SCpnt
;
392 fcmd
= fc
->cmd_slots
[token
];
394 rsp
= (fcp_rsp
*) (fc
->scsi_rsp_pool
+ fc
->rsp_size
* token
);
395 SCpnt
= SC_FCMND(fcmd
);
397 if (SCpnt
->done
!= fcp_scsi_done
)
400 rsp_status
= rsp
->fcp_status
;
401 FCD(("rsp_status %08x status %08x\n", rsp_status
, status
))
406 if (rsp_status
& FCP_STATUS_RESID
) {
408 FCD(("Resid %d\n", rsp
->fcp_resid
))
410 fcp_cmd
*cmd
= fc
->scsi_cmd_pool
+ token
;
414 for (i
= 0; i
< sizeof(fcp_cmd
); i
+=4)
415 printk ("%08x ", *(u32
*)(((char *)cmd
)+i
));
416 printk ("\nResponse ");
417 for (i
= 0; i
< fc
->rsp_size
; i
+=4)
418 printk ("%08x ", *(u32
*)(((char *)rsp
)+i
));
424 if (rsp_status
& FCP_STATUS_SENSE_LEN
) {
425 sense_len
= rsp
->fcp_sense_len
;
426 if (sense_len
> sizeof(SCpnt
->sense_buffer
)) sense_len
= sizeof(SCpnt
->sense_buffer
);
427 memcpy(SCpnt
->sense_buffer
, ((char *)(rsp
+1)), sense_len
);
431 dma_unmap_sg(fc
->dev
, scsi_sglist(SCpnt
),
432 scsi_sg_count(SCpnt
),
433 SCpnt
->sc_data_direction
);
436 host_status
=DID_ERROR
; /* FIXME */
437 FCD(("Wrong FC status %d for token %d\n", status
, token
))
441 if (status_byte(rsp_status
) == QUEUE_FULL
) {
442 printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc
->name
, SCpnt
->device
->channel
, SCpnt
->device
->id
, rsp_status
);
445 SCpnt
->result
= (host_status
<< 16) | (rsp_status
& 0xff);
447 if (host_status
|| SCpnt
->result
|| rsp_status
) printk("FC: host_status %d, packet status %d\n",
448 host_status
, SCpnt
->result
);
450 SCpnt
->done
= fcmd
->done
;
452 clear_bit(token
, fc
->scsi_bitmap
);
454 FCD(("Calling scsi_done with %08x\n", SCpnt
->result
))
455 SCpnt
->scsi_done(SCpnt
);
458 void fcp_receive_solicited(fc_channel
*fc
, int proto
, int token
, int status
, fc_hdr
*fch
)
461 FCD(("receive_solicited %d %d %d\n", proto
, token
, status
))
464 fcp_scsi_receive(fc
, token
, status
, fch
); break;
465 case TYPE_EXTENDED_LS
:
466 case PROTO_REPORT_AL_MAP
:
469 magic
= ((ls
*)(fc
->ls
))->magic
;
470 if (magic
== LSMAGIC
) {
471 ls
*l
= (ls
*)fc
->ls
;
472 int i
= (token
>= l
->count
) ? token
- l
->count
: token
;
475 if ((unsigned)i
< l
->count
&& l
->fcmds
[i
].fc
== fc
) {
476 if (proto
== TYPE_EXTENDED_LS
)
477 fcp_login_done(fc
, token
, status
);
479 fcp_report_map_done(fc
, token
, status
);
483 FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc
, fc
->ls
, fc
->cmd_slots
))
484 if (proto
== TYPE_EXTENDED_LS
&& !fc
->ls
&& fc
->cmd_slots
) {
487 fcmd
= fc
->cmd_slots
[token
];
488 if (fcmd
&& fcmd
->ls
&& ((ls
*)(fcmd
->ls
))->magic
== LSEMAGIC
) {
489 lse
*l
= (lse
*)fcmd
->ls
;
497 if (fc
->ls
&& ((lso
*)(fc
->ls
))->magic
== LSOMAGIC
) {
498 lso
*l
= (lso
*)fc
->ls
;
500 if ((unsigned)token
< l
->count
&& l
->fcmds
[token
].fc
== fc
) {
501 /* Wow, OFFLINE response arrived :) */
502 FCD(("OFFLINE Response arrived\n"))
503 fc
->state
= FC_STATE_OFFLINE
;
504 if (atomic_dec_and_test (&l
->todo
))
515 void fcp_state_change(fc_channel
*fc
, int state
)
517 FCD(("state_change %d %d\n", state
, fc
->state
))
518 if (state
== FC_STATE_ONLINE
&& fc
->state
== FC_STATE_MAYBEOFFLINE
)
519 fc
->state
= FC_STATE_UNINITED
;
520 else if (state
== FC_STATE_ONLINE
)
521 printk (KERN_WARNING
"%s: state change to ONLINE\n", fc
->name
);
523 printk (KERN_ERR
"%s: state change to OFFLINE\n", fc
->name
);
526 int fcp_initialize(fc_channel
*fcchain
, int count
)
533 FCND(("fcp_inititialize %08lx\n", (long)fcp_init
))
534 FCND(("fc_channels %08lx\n", (long)fc_channels
))
535 FCND((" SID %d DID %d\n", fcchain
->sid
, fcchain
->did
))
536 l
= kzalloc(sizeof (ls
) + count
, GFP_KERNEL
);
538 printk ("FC: Cannot allocate memory for initialization\n");
543 FCND(("FCP Init for %d channels\n", count
))
544 init_MUTEX_LOCKED(&l
->sem
);
545 init_timer(&l
->timer
);
546 l
->timer
.function
= fcp_login_timeout
;
547 l
->timer
.data
= (unsigned long)l
;
548 atomic_set (&l
->todo
, count
);
549 l
->logi
= kzalloc (count
* 3 * sizeof(logi
), GFP_KERNEL
);
550 l
->fcmds
= kzalloc (count
* sizeof(fcp_cmnd
), GFP_KERNEL
);
551 if (!l
->logi
|| !l
->fcmds
) {
555 printk ("FC: Cannot allocate DMA memory for initialization\n");
558 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
559 fc
->state
= FC_STATE_UNINITED
;
560 fc
->rst_pkt
= NULL
; /* kmalloc when first used */
562 /* First try if we are in a AL topology */
563 FCND(("Initializing REPORT_MAP packets\n"))
564 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
568 /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
569 fcmd
->cmd
= dma_map_single (fc
->dev
, l
->logi
+ 3 * i
, 3 * sizeof(logi
),
571 fcmd
->proto
= PROTO_REPORT_AL_MAP
;
575 for (retry
= 0; retry
< 8; retry
++) {
577 FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
578 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
579 if (fc
->state
== FC_STATE_ONLINE
|| fc
->state
== FC_STATE_OFFLINE
)
581 disable_irq(fc
->irq
);
582 if (fc
->state
== FC_STATE_MAYBEOFFLINE
) {
587 fc
->state
= FC_STATE_OFFLINE
;
589 dma_unmap_single (fc
->dev
, l
->fcmds
[i
].cmd
, 3 * sizeof(logi
), DMA_BIDIRECTIONAL
);
590 if (atomic_dec_and_test (&l
->todo
))
594 ret
= fc
->hw_enque (fc
, fc
->login
);
600 if (ret
== -ENOSYS
&& fc
->login
->proto
== PROTO_REPORT_AL_MAP
) {
601 /* Oh yes, this card handles Point-to-Point only, so let's try that. */
604 FCD(("SID %d DID %d\n", fc
->sid
, fc
->did
))
606 dma_unmap_single(fc
->dev
, fcmd
->cmd
, 3 * sizeof(logi
), DMA_BIDIRECTIONAL
);
608 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, FS_FABRIC_F_PORT
);
609 FILL_FCHDR_SID(fch
, 0);
610 FILL_FCHDR_TYPE_FCTL(fch
, TYPE_EXTENDED_LS
, F_CTL_FIRST_SEQ
| F_CTL_SEQ_INITIATIVE
);
611 FILL_FCHDR_SEQ_DF_SEQ(fch
, 0, 0, 0);
612 FILL_FCHDR_OXRX(fch
, 0xffff, 0xffff);
614 l
->logi
[3 * i
].code
= LS_FLOGI
;
615 fcmd
->cmd
= dma_map_single (fc
->dev
, l
->logi
+ 3 * i
, 3 * sizeof(logi
), DMA_BIDIRECTIONAL
);
616 fcmd
->rsp
= fcmd
->cmd
+ sizeof(logi
);
617 fcmd
->cmdlen
= sizeof(logi
);
618 fcmd
->rsplen
= sizeof(logi
);
619 fcmd
->data
= (dma_addr_t
)NULL
;
620 fcmd
->class = FC_CLASS_SIMPLE
;
621 fcmd
->proto
= TYPE_EXTENDED_LS
;
623 printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc
->name
);
627 l
->timer
.expires
= jiffies
+ 5 * HZ
;
628 add_timer(&l
->timer
);
631 if (!atomic_read(&l
->todo
)) {
632 FCND(("All channels answered in time\n"))
633 break; /* All fc channels have answered us */
638 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
641 case FC_STATE_ONLINE
: break;
642 case FC_STATE_OFFLINE
: break;
643 default: dma_unmap_single (fc
->dev
, l
->fcmds
[i
].cmd
, 3 * sizeof(logi
), DMA_BIDIRECTIONAL
);
647 del_timer(&l
->timer
);
654 int fcp_forceoffline(fc_channel
*fcchain
, int count
)
661 memset (&l
, 0, sizeof(lso
));
664 FCND(("FCP Force Offline for %d channels\n", count
))
665 init_MUTEX_LOCKED(&l
.sem
);
666 init_timer(&l
.timer
);
667 l
.timer
.function
= fcp_login_timeout
;
668 l
.timer
.data
= (unsigned long)&l
;
669 atomic_set (&l
.todo
, count
);
670 l
.fcmds
= kzalloc (count
* sizeof(fcp_cmnd
), GFP_KERNEL
);
672 printk ("FC: Cannot allocate memory for forcing offline\n");
675 FCND(("Initializing OFFLINE packets\n"))
676 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++) {
677 fc
->state
= FC_STATE_UNINITED
;
682 fcmd
->class = FC_CLASS_OFFLINE
;
683 fcmd
->proto
= PROTO_OFFLINE
;
686 disable_irq(fc
->irq
);
687 ret
= fc
->hw_enque (fc
, fc
->login
);
689 if (ret
) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc
->name
);
692 l
.timer
.expires
= jiffies
+ 5 * HZ
;
697 for (fc
= fcchain
, i
= 0; fc
&& i
< count
; fc
= fc
->next
, i
++)
703 int fcp_init(fc_channel
*fcchain
)
709 for (fc
= fcchain
; fc
; fc
= fc
->next
) {
710 fc
->fcp_register
= fcp_register
;
714 ret
= fcp_initialize (fcchain
, count
);
719 fc_channels
= fcchain
;
721 for (fc
= fc_channels
; fc
->next
; fc
= fc
->next
);
727 void fcp_release(fc_channel
*fcchain
, int count
) /* count must > 0 */
732 for (fc
= fcchain
; --count
&& fc
->next
; fc
= fc
->next
);
734 printk("FC: nothing to release\n");
738 if (fc_channels
== fcchain
)
739 fc_channels
= fc
->next
;
741 for (fcx
= fc_channels
; fcx
->next
!= fcchain
; fcx
= fcx
->next
);
742 fcx
->next
= fc
->next
;
747 * We've just grabbed fcchain out of the fc_channel list
748 * and zero-terminated it, while destroying the count.
750 * Freeing the fc's is the low level driver's responsibility.
755 static void fcp_scsi_done(struct scsi_cmnd
*SCpnt
)
757 if (FCP_CMND(SCpnt
)->done
)
758 FCP_CMND(SCpnt
)->done(SCpnt
);
761 static int fcp_scsi_queue_it(fc_channel
*fc
, struct scsi_cmnd
*SCpnt
,
762 fcp_cmnd
*fcmd
, int prepare
)
768 i
= find_first_zero_bit (fc
->scsi_bitmap
, fc
->scsi_bitmap_end
);
769 set_bit (i
, fc
->scsi_bitmap
);
771 cmd
= fc
->scsi_cmd_pool
+ i
;
773 if (fc
->encode_addr (SCpnt
, cmd
->fcp_addr
, fc
, fcmd
)) {
774 /* Invalid channel/id/lun and couldn't map it into fcp_addr */
775 clear_bit (i
, fc
->scsi_bitmap
);
776 SCpnt
->result
= (DID_BAD_TARGET
<< 16);
777 SCpnt
->scsi_done(SCpnt
);
781 fc
->cmd_slots
[fcmd
->token
] = fcmd
;
783 if (SCpnt
->device
->tagged_supported
) {
784 if (jiffies
- fc
->ages
[SCpnt
->device
->channel
* fc
->targets
+ SCpnt
->device
->id
] > (5 * 60 * HZ
)) {
785 fc
->ages
[SCpnt
->device
->channel
* fc
->targets
+ SCpnt
->device
->id
] = jiffies
;
786 fcp_cntl
= FCP_CNTL_QTYPE_ORDERED
;
788 fcp_cntl
= FCP_CNTL_QTYPE_SIMPLE
;
790 fcp_cntl
= FCP_CNTL_QTYPE_UNTAGGED
;
792 if (!scsi_bufflen(SCpnt
)) {
793 cmd
->fcp_cntl
= fcp_cntl
;
794 fcmd
->data
= (dma_addr_t
)NULL
;
796 struct scatterlist
*sg
;
799 switch (SCpnt
->cmnd
[0]) {
803 cmd
->fcp_cntl
= (FCP_CNTL_WRITE
| fcp_cntl
); break;
805 cmd
->fcp_cntl
= (FCP_CNTL_READ
| fcp_cntl
); break;
808 sg
= scsi_sglist(SCpnt
);
809 nents
= dma_map_sg(fc
->dev
, sg
, scsi_sg_count(SCpnt
),
810 SCpnt
->sc_data_direction
);
811 fcmd
->data
= sg_dma_address(sg
);
812 cmd
->fcp_data_len
= sg_dma_len(sg
);
814 memcpy (cmd
->fcp_cdb
, SCpnt
->cmnd
, SCpnt
->cmd_len
);
815 memset (cmd
->fcp_cdb
+SCpnt
->cmd_len
, 0, sizeof(cmd
->fcp_cdb
)-SCpnt
->cmd_len
);
816 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)))
818 FCD(("Trying to enque %p\n", fcmd
))
820 if (!fc
->hw_enque (fc
, fcmd
)) {
821 FCD(("hw_enque succeeded for %p\n", fcmd
))
825 FCD(("Putting into que1 %p\n", fcmd
))
826 fcp_scsi_insert_queue (fc
, fcmd
);
830 int fcp_scsi_queuecommand(struct scsi_cmnd
*SCpnt
,
831 void (* done
)(struct scsi_cmnd
*))
833 fcp_cmnd
*fcmd
= FCP_CMND(SCpnt
);
834 fc_channel
*fc
= FC_SCMND(SCpnt
);
836 FCD(("Entering SCSI queuecommand %p\n", fcmd
))
837 if (SCpnt
->done
!= fcp_scsi_done
) {
838 fcmd
->done
= SCpnt
->done
;
839 SCpnt
->done
= fcp_scsi_done
;
840 SCpnt
->scsi_done
= done
;
841 fcmd
->proto
= TYPE_SCSI_FCP
;
842 if (!fc
->scsi_free
) {
843 FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
844 #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
845 printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
849 return fcp_scsi_queue_it(fc
, SCpnt
, fcmd
, 1);
851 return fcp_scsi_queue_it(fc
, SCpnt
, fcmd
, 0);
854 void fcp_queue_empty(fc_channel
*fc
)
858 FCD(("Queue empty\n"))
859 while ((fcmd
= fc
->scsi_que
)) {
860 /* The hw told us we can try again queue some packet */
861 if (fc
->hw_enque (fc
, fcmd
))
863 fcp_scsi_remove_queue (fc
, fcmd
);
867 int fcp_scsi_abort(struct scsi_cmnd
*SCpnt
)
869 /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
870 fcp_cmnd
*fcmd
= FCP_CMND(SCpnt
);
871 fc_channel
*fc
= FC_SCMND(SCpnt
);
874 * We react to abort requests by simply forgetting
875 * about the command and pretending everything's sweet.
876 * This may or may not be silly. We can't, however,
877 * immediately reuse the command's cmd_slots slot,
878 * as its result may arrive later and we cannot
879 * check whether it is the aborted one, can't we?
881 * Therefore, after the first few aborts are done,
882 * we tell the scsi error handler to do something clever.
883 * It will eventually call host reset, refreshing
886 * There is a theoretical chance that we sometimes allow
887 * more than can_queue packets to the jungle this way,
888 * but the worst outcome possible is a series of
889 * more aborts and eventually the dev_reset catharsis.
892 if (++fc
->abort_count
< (fc
->can_queue
>> 1)) {
893 SCpnt
->result
= DID_ABORT
;
895 printk("FC: soft abort\n");
898 printk("FC: hard abort refused\n");
904 void fcp_scsi_reset_done(struct scsi_cmnd
*SCpnt
)
906 fc_channel
*fc
= FC_SCMND(SCpnt
);
908 fc
->rst_pkt
->eh_state
= SCSI_STATE_FINISHED
;
909 up(fc
->rst_pkt
->device
->host
->eh_action
);
913 #define FCP_RESET_TIMEOUT (2*HZ)
915 int fcp_scsi_dev_reset(struct scsi_cmnd
*SCpnt
)
917 #if 0 /* broken junk, but if davem wants to compile this driver, let him.. */
921 fc_channel
*fc
= FC_SCMND(SCpnt
);
922 DECLARE_MUTEX_LOCKED(sem
);
925 fc
->rst_pkt
= kmalloc(sizeof(SCpnt
), GFP_KERNEL
);
926 if (!fc
->rst_pkt
) return FAILED
;
928 fcmd
= FCP_CMND(fc
->rst_pkt
);
932 cmd
= fc
->scsi_cmd_pool
+ 0;
933 FCD(("Preparing rst packet\n"))
934 fc
->encode_addr (SCpnt
, cmd
->fcp_addr
, fc
, fcmd
);
935 fc
->rst_pkt
->device
= SCpnt
->device
;
936 fc
->rst_pkt
->cmd_len
= 0;
938 fc
->cmd_slots
[0] = fcmd
;
940 cmd
->fcp_cntl
= FCP_CNTL_QTYPE_ORDERED
| FCP_CNTL_RESET
;
941 fcmd
->data
= (dma_addr_t
)NULL
;
942 fcmd
->proto
= TYPE_SCSI_FCP
;
944 memcpy (cmd
->fcp_cdb
, SCpnt
->cmnd
, SCpnt
->cmd_len
);
945 memset (cmd
->fcp_cdb
+SCpnt
->cmd_len
, 0, sizeof(cmd
->fcp_cdb
)-SCpnt
->cmd_len
);
946 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)))
948 fcmd
= FCP_CMND(fc
->rst_pkt
);
949 if (fc
->rst_pkt
->eh_state
== SCSI_STATE_QUEUED
)
950 return FAILED
; /* or SUCCESS. Only these */
952 fc
->rst_pkt
->done
= NULL
;
955 fc
->rst_pkt
->eh_state
= SCSI_STATE_QUEUED
;
956 init_timer(&fc
->rst_pkt
->eh_timeout
);
957 fc
->rst_pkt
->eh_timeout
.data
= (unsigned long) fc
->rst_pkt
;
958 fc
->rst_pkt
->eh_timeout
.expires
= jiffies
+ FCP_RESET_TIMEOUT
;
959 fc
->rst_pkt
->eh_timeout
.function
= (void (*)(unsigned long))fcp_scsi_reset_done
;
961 add_timer(&fc
->rst_pkt
->eh_timeout
);
964 * Set up the semaphore so we wait for the command to complete.
967 fc
->rst_pkt
->device
->host
->eh_action
= &sem
;
969 fc
->rst_pkt
->done
= fcp_scsi_reset_done
;
971 spin_lock_irqsave(SCpnt
->device
->host
->host_lock
, flags
);
972 fcp_scsi_queue_it(fc
, fc
->rst_pkt
, fcmd
, 0);
973 spin_unlock_irqrestore(SCpnt
->device
->host
->host_lock
, flags
);
977 fc
->rst_pkt
->device
->host
->eh_action
= NULL
;
978 del_timer(&fc
->rst_pkt
->eh_timeout
);
981 * See if timeout. If so, tell the host to forget about it.
982 * In other words, we don't want a callback any more.
984 if (fc
->rst_pkt
->eh_state
== SCSI_STATE_TIMEOUT
) {
985 fc
->rst_pkt
->eh_state
= SCSI_STATE_UNUSED
;
988 fc
->rst_pkt
->eh_state
= SCSI_STATE_UNUSED
;
993 static int __fcp_scsi_host_reset(struct scsi_cmnd
*SCpnt
)
995 fc_channel
*fc
= FC_SCMND(SCpnt
);
996 fcp_cmnd
*fcmd
= FCP_CMND(SCpnt
);
999 printk ("FC: host reset\n");
1001 for (i
=0; i
< fc
->can_queue
; i
++) {
1002 if (fc
->cmd_slots
[i
] && SCpnt
->result
!= DID_ABORT
) {
1003 SCpnt
->result
= DID_RESET
;
1005 fc
->cmd_slots
[i
] = NULL
;
1009 fc
->abort_count
= 0;
1010 if (fcp_initialize(fc
, 1)) return SUCCESS
;
1014 int fcp_scsi_host_reset(struct scsi_cmnd
*SCpnt
)
1016 unsigned long flags
;
1019 spin_lock_irqsave(SCpnt
->device
->host
->host_lock
, flags
);
1020 rc
= __fcp_scsi_host_reset(SCpnt
);
1021 spin_unlock_irqrestore(SCpnt
->device
->host
->host_lock
, flags
);
1026 static int fcp_els_queue_it(fc_channel
*fc
, fcp_cmnd
*fcmd
)
1030 i
= find_first_zero_bit (fc
->scsi_bitmap
, fc
->scsi_bitmap_end
);
1031 set_bit (i
, fc
->scsi_bitmap
);
1034 fc
->cmd_slots
[fcmd
->token
] = fcmd
;
1035 return fcp_scsi_queue_it(fc
, NULL
, fcmd
, 0);
1038 static int fc_do_els(fc_channel
*fc
, unsigned int alpa
, void *data
, int len
)
1040 fcp_cmnd _fcmd
, *fcmd
;
1046 memset(fcmd
, 0, sizeof(fcp_cmnd
));
1047 FCD(("PLOGI SID %d DID %d\n", fc
->sid
, alpa
))
1049 FILL_FCHDR_RCTL_DID(fch
, R_CTL_ELS_REQ
, alpa
);
1050 FILL_FCHDR_SID(fch
, fc
->sid
);
1051 FILL_FCHDR_TYPE_FCTL(fch
, TYPE_EXTENDED_LS
, F_CTL_FIRST_SEQ
| F_CTL_SEQ_INITIATIVE
);
1052 FILL_FCHDR_SEQ_DF_SEQ(fch
, 0, 0, 0);
1053 FILL_FCHDR_OXRX(fch
, 0xffff, 0xffff);
1055 fcmd
->cmd
= dma_map_single (fc
->dev
, data
, 2 * len
, DMA_BIDIRECTIONAL
);
1056 fcmd
->rsp
= fcmd
->cmd
+ len
;
1059 fcmd
->data
= (dma_addr_t
)NULL
;
1061 fcmd
->class = FC_CLASS_SIMPLE
;
1062 fcmd
->proto
= TYPE_EXTENDED_LS
;
1064 memset (&l
, 0, sizeof(lse
));
1066 init_MUTEX_LOCKED(&l
.sem
);
1067 l
.timer
.function
= fcp_login_timeout
;
1068 l
.timer
.data
= (unsigned long)&l
;
1069 l
.status
= FC_STATUS_TIMED_OUT
;
1070 fcmd
->ls
= (void *)&l
;
1072 disable_irq(fc
->irq
);
1073 fcp_els_queue_it(fc
, fcmd
);
1074 enable_irq(fc
->irq
);
1077 l
.timer
.expires
= jiffies
+ 5 * HZ
;
1078 add_timer(&l
.timer
);
1080 del_timer(&l
.timer
);
1081 if (l
.status
!= FC_STATUS_TIMED_OUT
) break;
1082 if (++i
== 3) break;
1083 disable_irq(fc
->irq
);
1084 fcp_scsi_queue_it(fc
, NULL
, fcmd
, 0);
1085 enable_irq(fc
->irq
);
1088 clear_bit(fcmd
->token
, fc
->scsi_bitmap
);
1090 dma_unmap_single (fc
->dev
, fcmd
->cmd
, 2 * len
, DMA_BIDIRECTIONAL
);
1094 int fc_do_plogi(fc_channel
*fc
, unsigned char alpa
, fc_wwn
*node
, fc_wwn
*nport
)
1099 l
= kzalloc(2 * sizeof(logi
), GFP_KERNEL
);
1100 if (!l
) return -ENOMEM
;
1102 memcpy (&l
->nport_wwn
, &fc
->wwn_nport
, sizeof(fc_wwn
));
1103 memcpy (&l
->node_wwn
, &fc
->wwn_node
, sizeof(fc_wwn
));
1104 memcpy (&l
->common
, fc
->common_svc
, sizeof(common_svc_parm
));
1105 memcpy (&l
->class1
, fc
->class_svcs
, 3*sizeof(svc_parm
));
1106 status
= fc_do_els(fc
, alpa
, l
, sizeof(logi
));
1107 if (status
== FC_STATUS_OK
) {
1108 if (l
[1].code
== LS_ACC
) {
1110 u32
*u
= (u32
*)&l
[1].nport_wwn
;
1111 FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa
,
1112 u
[0], u
[1], u
[2], u
[3]))
1114 memcpy(nport
, &l
[1].nport_wwn
, sizeof(fc_wwn
));
1115 memcpy(node
, &l
[1].node_wwn
, sizeof(fc_wwn
));
1117 status
= FC_STATUS_BAD_RSP
;
1128 int fc_do_prli(fc_channel
*fc
, unsigned char alpa
)
1133 p
= kzalloc(2 * sizeof(prli
), GFP_KERNEL
);
1134 if (!p
) return -ENOMEM
;
1136 p
->params
[0] = 0x08002000;
1137 p
->params
[3] = 0x00000022;
1138 status
= fc_do_els(fc
, alpa
, p
, sizeof(prli
));
1139 if (status
== FC_STATUS_OK
&& p
[1].code
!= LS_PRLI_ACC
&& p
[1].code
!= LS_ACC
)
1140 status
= FC_STATUS_BAD_RSP
;
1145 MODULE_LICENSE("GPL");