* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / fc4 / fc.c
blob8b45510a14afb06ae4fdb3405660df328d364f8c
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.
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
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/types.h>
28 #include <linux/fcntl.h>
29 #include <linux/interrupt.h>
30 #include <linux/ptrace.h>
31 #include <linux/ioport.h>
32 #include <linux/in.h>
33 #include <linux/malloc.h>
34 #include <linux/string.h>
35 #include <linux/init.h>
36 #include <linux/blk.h>
38 #include <asm/pgtable.h>
39 #include <asm/irq.h>
40 #include <asm/semaphore.h>
41 #include "fcp_impl.h"
42 #include "../scsi/hosts.h"
44 /* #define FCDEBUG */
46 #define fc_printk printk ("%s: ", fc->name); printk
48 #ifdef FCDEBUG
49 #define FCD(x) fc_printk x;
50 #define FCND(x) printk ("FC: "); printk x;
51 #else
52 #define FCD(x)
53 #define FCND(x)
54 #endif
56 #ifdef __sparc__
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);
81 #else
82 #error Port this
83 #endif
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)
94 if (!fc->scsi_que) {
95 fc->scsi_que = fcmd;
96 fcmd->next = fcmd;
97 fcmd->prev = fcmd;
98 } else {
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) {
109 fc->scsi_que = NULL;
110 return;
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
121 typedef struct {
122 /* Must be first */
123 struct semaphore sem;
124 int magic;
125 int count;
126 logi *logi;
127 fcp_cmnd *fcmds;
128 atomic_t todo;
129 struct timer_list timer;
130 unsigned char grace[0];
131 } ls;
133 #define LSOMAGIC 654907799
134 typedef struct {
135 /* Must be first */
136 struct semaphore sem;
137 int magic;
138 int count;
139 fcp_cmnd *fcmds;
140 atomic_t todo;
141 struct timer_list timer;
142 } lso;
144 #define LSEMAGIC 84482456
145 typedef struct {
146 /* Must be first */
147 struct semaphore sem;
148 int magic;
149 int status;
150 struct timer_list timer;
151 } lse;
153 static void fcp_login_timeout(unsigned long data)
155 ls *l = (ls *)data;
156 FCND(("Login timeout\n"))
157 up(&l->sem);
160 static void fcp_login_done(fc_channel *fc, int i, int status)
162 fcp_cmnd *fcmd;
163 logi *plogi;
164 fc_hdr *fch;
165 ls *l = (ls *)fc->ls;
167 FCD(("Login done %d %d\n", i, status))
168 if (i < l->count) {
169 if (fc->state == FC_STATE_FPORT_OK) {
170 FCD(("Additional FPORT_OK received with status %d\n", status))
171 return;
173 switch (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;
177 fcmd = l->fcmds + i;
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));
185 fch = &fcmd->fch;
186 fcmd->token += l->count;
187 FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, fc->did);
188 FILL_FCHDR_SID(fch, fc->sid);
189 #ifdef FCDEBUG
191 int i;
192 unsigned *x = (unsigned *)plogi;
193 printk ("logi: ");
194 for (i = 0; i < 21; i++)
195 printk ("%08x ", x[i]);
196 printk ("\n");
198 #endif
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);
203 break;
204 case FC_STATUS_ERR_OFFLINE:
205 fc->state = FC_STATE_MAYBEOFFLINE;
206 FCD (("FC is offline %d\n", l->grace[i]))
207 break;
208 default:
209 printk ("FLOGI failed for %s with status %d\n", fc->name, status);
210 /* Do some sort of error recovery here */
211 break;
213 } else {
214 i -= l->count;
215 if (fc->state != FC_STATE_FPORT_OK) {
216 FCD(("Unexpected N-PORT rsp received"))
217 return;
219 switch (status) {
220 case FC_STATUS_OK:
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",
229 fc->name,
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))
236 up(&l->sem);
237 break;
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))
243 up(&l->sem);
244 break;
245 default:
246 printk ("PLOGI failed for %s with status %d\n", fc->name, status);
247 /* Do some sort of error recovery here */
248 break;
253 static void fcp_report_map_done(fc_channel *fc, int i, int status)
255 fcp_cmnd *fcmd;
256 fc_hdr *fch;
257 unsigned char j;
258 ls *l = (ls *)fc->ls;
259 fc_al_posmap *p;
261 FCD(("Report map done %d %d\n", i, status))
262 switch (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);
266 #ifdef FCDEBUG
268 u32 *u = (u32 *)p;
269 FCD(("%08x\n", u[0]))
270 u ++;
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]))
273 #endif
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;
277 } else {
278 fc->posmap = (fcp_posmap *)kmalloc(sizeof(fcp_posmap)+p->len, GFP_KERNEL);
279 if (!fc->posmap) {
280 printk("FC: Not enough memory, offlining channel\n");
281 fc->state = FC_STATE_OFFLINE;
282 } else {
283 int k;
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)
290 break;
291 k = p->len;
292 if (i == p->len)
293 i = 0;
294 else {
295 p->len--;
296 i++;
298 fc->posmap->len = p->len;
299 for (j = 0; j < p->len; j++) {
300 if (i == k) i = 0;
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))
308 up(&l->sem);
309 break;
310 case FC_STATUS_POINTTOPOINT: /* We're Point-to-Point, no AL... */
311 FCD(("SID %d DID %d\n", fc->sid, fc->did))
312 fcmd = l->fcmds + i;
313 fc_sync_dma_exit(fcmd->cmd, 3 * sizeof(logi), fc);
314 fch = &fcmd->fch;
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);
321 fch->param = 0;
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);
332 break;
333 case FC_STATUS_ERR_OFFLINE:
334 fc->state = FC_STATE_MAYBEOFFLINE;
335 FCD (("FC is offline %d\n", l->grace[i]))
336 break;
337 default:
338 printk ("FLOGI failed for %s with status %d\n", fc->name, status);
339 /* Do some sort of error recovery here */
340 break;
344 void fcp_register(fc_channel *fc, u8 type, int unregister)
346 int size, i;
347 int slots = (fc->can_queue * 3) >> 1;
349 FCND(("Going to %sregister\n", unregister ? "un" : ""))
351 if (type == TYPE_SCSI_FCP) {
352 if (!unregister) {
353 fc->scsi_cmd_pool =
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*));
368 fc->abort_count = 0;
369 } else {
370 fc->scsi_name[0] = 0;
371 kfree (fc->scsi_bitmap);
372 kfree (fc->cmd_slots);
373 FCND(("Unregistering\n"));
374 if (fc->rst_pkt) {
375 if (fc->rst_pkt->eh_state == SCSI_STATE_UNUSED)
376 kfree(fc->rst_pkt);
377 else {
378 /* Can't happen. Some memory would be lost. */
379 printk("FC: Reset in progress. Now?!");
382 FCND(("Unregistered\n"));
384 } else
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)
392 fcp_cmnd *fcmd;
393 fcp_rsp *rsp;
394 int host_status;
395 Scsi_Cmnd *SCpnt;
396 int sense_len;
397 int rsp_status;
399 fcmd = fc->cmd_slots[token];
400 if (!fcmd) return;
401 rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
402 SCpnt = SC_FCMND(fcmd);
404 if (SCpnt->done != fcp_scsi_done)
405 return;
407 rsp_status = rsp->fcp_status;
408 FCD(("rsp_status %08x status %08x\n", rsp_status, status))
409 switch (status) {
410 case FC_STATUS_OK:
411 host_status=DID_OK;
413 if (rsp_status & FCP_STATUS_RESID) {
414 #ifdef FCDEBUG
415 FCD(("Resid %d\n", rsp->fcp_resid))
417 fcp_cmd *cmd = fc->scsi_cmd_pool + token;
418 int i;
420 printk ("Command ");
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));
426 printk ("\n");
428 #endif
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);
437 if (fcmd->data) {
438 if (SCpnt->use_sg)
439 fc_sync_dma_exit_sg((struct scatterlist *)SCpnt->buffer, SCpnt->use_sg, fc);
440 else
441 fc_sync_dma_exit(fcmd->data, SCpnt->request_bufflen, fc);
443 break;
444 default:
445 host_status=DID_ERROR; /* FIXME */
446 FCD(("Wrong FC status %d for token %d\n", status, token))
447 break;
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);
455 #ifdef FCDEBUG
456 if (host_status || SCpnt->result || rsp_status) printk("FC: host_status %d, packet status %d\n",
457 host_status, SCpnt->result);
458 #endif
459 SCpnt->done = fcmd->done;
460 fcmd->done=NULL;
461 clear_bit(token, fc->scsi_bitmap);
462 fc->scsi_free++;
463 FCD(("Calling scsi_done with %08x\n", SCpnt->result))
464 SCpnt->scsi_done(SCpnt);
467 void fcp_receive_solicited(fc_channel *fc, int proto, int token, int status, fc_hdr *fch)
469 int magic;
470 FCD(("receive_solicited %d %d %d\n", proto, token, status))
471 switch (proto) {
472 case TYPE_SCSI_FCP:
473 fcp_scsi_receive(fc, token, status, fch); break;
474 case TYPE_EXTENDED_LS:
475 case PROTO_REPORT_AL_MAP:
476 magic = 0;
477 if (fc->ls)
478 magic = ((ls *)(fc->ls))->magic;
479 if (magic == LSMAGIC) {
480 ls *l = (ls *)fc->ls;
481 int i = (token >= l->count) ? token - l->count : token;
483 /* Let's be sure */
484 if ((unsigned)i < l->count && l->fcmds[i].fc == fc) {
485 if (proto == TYPE_EXTENDED_LS)
486 fcp_login_done(fc, token, status);
487 else
488 fcp_report_map_done(fc, token, status);
489 break;
492 FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc, fc->ls, fc->cmd_slots))
493 if (proto == TYPE_EXTENDED_LS && !fc->ls && fc->cmd_slots) {
494 fcp_cmnd *fcmd;
496 fcmd = fc->cmd_slots[token];
497 if (fcmd && fcmd->ls && ((ls *)(fcmd->ls))->magic == LSEMAGIC) {
498 lse *l = (lse *)fcmd->ls;
500 l->status = status;
501 up (&l->sem);
504 break;
505 case PROTO_OFFLINE:
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))
514 up(&l->sem);
517 break;
519 default:
520 break;
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);
531 else
532 printk (KERN_ERR "%s: state change to OFFLINE\n", fc->name);
535 int fcp_initialize(fc_channel *fcchain, int count)
537 fc_channel *fc;
538 fcp_cmnd *fcmd;
539 int i, retry, ret;
540 ls *l;
542 FCND(("fcp_inititialize %08lx\n", (long)fcp_init))
543 FCND(("fc_channels %08lx\n", (long)fc_channels))
544 FCND((" SID %d DID %d\n", fcchain->sid, fcchain->did))
545 l = kmalloc(sizeof (ls) + count, GFP_KERNEL);
546 if (!l) {
547 printk ("FC: Cannot allocate memory for initialization\n");
548 return -ENOMEM;
550 memset (l, 0, sizeof(ls) + count);
551 l->magic = LSMAGIC;
552 l->count = count;
553 FCND(("FCP Init for %d channels\n", count))
554 init_MUTEX_LOCKED(&l->sem);
555 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);
563 kfree (l);
564 printk ("FC: Cannot allocate DMA memory for initialization\n");
565 return -ENOMEM;
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++) {
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 = fc_sync_dma_entry (l->logi + 3 * i, 3 * sizeof(logi), fc);
581 fcmd->proto = PROTO_REPORT_AL_MAP;
582 fcmd->token = i;
583 fcmd->fc = fc;
585 for (retry = 0; retry < 8; retry++) {
586 int nqueued = 0;
587 FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
588 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
589 if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
590 continue;
591 disable_irq(fc->irq);
592 if (fc->state == FC_STATE_MAYBEOFFLINE) {
593 if (!l->grace[i]) {
594 l->grace[i]++;
595 FCD(("Grace\n"))
596 } else {
597 fc->state = FC_STATE_OFFLINE;
598 enable_irq(fc->irq);
599 fc_sync_dma_exit (l->fcmds[i].cmd, 3 * sizeof(logi), fc);
600 if (atomic_dec_and_test (&l->todo))
601 goto all_done;
604 ret = fc->hw_enque (fc, fc->login);
605 enable_irq(fc->irq);
606 if (!ret) {
607 nqueued++;
608 continue;
610 if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
611 /* Oh yes, this card handles Point-to-Point only, so let's try that. */
612 fc_hdr *fch;
614 FCD(("SID %d DID %d\n", fc->sid, fc->did))
615 fcmd = l->fcmds + i;
616 fc_sync_dma_exit(fcmd->cmd, 3 * sizeof(logi), fc);
617 fch = &fcmd->fch;
618 FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
619 FILL_FCHDR_SID(fch, 0);
620 FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
621 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
622 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
623 fch->param = 0;
624 l->logi [3 * i].code = LS_FLOGI;
625 fcmd->cmd = 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;
632 } else
633 printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
636 if (nqueued) {
637 l->timer.expires = jiffies + 5 * HZ;
638 add_timer(&l->timer);
640 down(&l->sem);
641 if (!atomic_read(&l->todo)) {
642 FCND(("All channels answered in time\n"))
643 break; /* All fc channels have answered us */
647 all_done:
648 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
649 fc->ls = NULL;
650 switch (fc->state) {
651 case FC_STATE_ONLINE: break;
652 case FC_STATE_OFFLINE: break;
653 default: fc_sync_dma_exit (l->fcmds[i].cmd, 3 * sizeof(logi), fc);
654 break;
657 del_timer(&l->timer);
658 kfree (l->logi);
659 kfree (l->fcmds);
660 kfree (l);
661 return 0;
664 int fcp_forceoffline(fc_channel *fcchain, int count)
666 fc_channel *fc;
667 fcp_cmnd *fcmd;
668 int i, ret;
669 lso l;
671 memset (&l, 0, sizeof(lso));
672 l.count = count;
673 l.magic = LSOMAGIC;
674 FCND(("FCP Force Offline for %d channels\n", count))
675 init_MUTEX_LOCKED(&l.sem);
676 l.timer.function = fcp_login_timeout;
677 l.timer.data = (unsigned long)&l;
678 atomic_set (&l.todo, count);
679 l.fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
680 if (!l.fcmds) {
681 kfree (l.fcmds);
682 printk ("FC: Cannot allocate memory for forcing offline\n");
683 return -ENOMEM;
685 memset (l.fcmds, 0, count * sizeof(fcp_cmnd));
686 FCND(("Initializing OFFLINE packets\n"))
687 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
688 fc->state = FC_STATE_UNINITED;
689 fcmd = l.fcmds + i;
690 fc->login = fcmd;
691 fc->ls = (void *)&l;
692 fcmd->did = fc->did;
693 fcmd->class = FC_CLASS_OFFLINE;
694 fcmd->proto = PROTO_OFFLINE;
695 fcmd->token = i;
696 fcmd->fc = fc;
697 disable_irq(fc->irq);
698 ret = fc->hw_enque (fc, fc->login);
699 enable_irq(fc->irq);
700 if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
703 l.timer.expires = jiffies + 5 * HZ;
704 add_timer(&l.timer);
705 down(&l.sem);
706 del_timer(&l.timer);
708 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
709 fc->ls = NULL;
710 kfree (l.fcmds);
711 return 0;
714 int fcp_init(fc_channel *fcchain)
716 fc_channel *fc;
717 int count=0;
718 int ret;
720 for (fc = fcchain; fc; fc = fc->next) {
721 fc->fcp_register = fcp_register;
722 count++;
725 ret = fcp_initialize (fcchain, count);
726 if (ret)
727 return ret;
729 if (!fc_channels)
730 fc_channels = fcchain;
731 else {
732 for (fc = fc_channels; fc->next; fc = fc->next);
733 fc->next = fcchain;
735 return ret;
738 void fcp_release(fc_channel *fcchain, int count) /* count must > 0 */
740 fc_channel *fc;
741 fc_channel *fcx;
743 for (fc = fcchain; --count && fc->next; fc = fc->next);
744 if (count) {
745 printk("FC: nothing to release\n");
746 return;
749 if (fc_channels == fcchain)
750 fc_channels = fc->next;
751 else {
752 for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
753 fcx->next = fc->next;
755 fc->next = NULL;
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)
774 long i;
775 fcp_cmd *cmd;
776 u32 fcp_cntl;
777 if (prepare) {
778 i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
779 set_bit (i, fc->scsi_bitmap);
780 fcmd->token = i;
781 cmd = fc->scsi_cmd_pool + i;
783 if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
784 /* Invalid channel/id/lun and couldn't map it into fcp_addr */
785 clear_bit (i, fc->scsi_bitmap);
786 SCpnt->result = (DID_BAD_TARGET << 16);
787 SCpnt->scsi_done(SCpnt);
788 return 0;
790 fc->scsi_free--;
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;
797 } else
798 fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
799 } else
800 fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
801 if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
802 cmd->fcp_cntl = fcp_cntl;
803 fcmd->data = (dma_handle)NULL;
804 } else {
805 switch (SCpnt->cmnd[0]) {
806 case WRITE_6:
807 case WRITE_10:
808 case WRITE_12:
809 cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
810 default:
811 cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
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);
817 } else {
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))
832 if (!fc->scsi_que) {
833 if (!fc->hw_enque (fc, fcmd)) {
834 FCD(("hw_enque succeeded for %p\n", fcmd))
835 return 0;
838 FCD(("Putting into que1 %p\n", fcmd))
839 fcp_scsi_insert_queue (fc, fcmd);
840 return 0;
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");
858 #endif
859 return 1;
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)
868 fcp_cmnd *fcmd;
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))
874 break;
875 fcp_scsi_remove_queue (fc, fcmd);
879 int fcp_old_abort(Scsi_Cmnd *SCpnt)
881 printk("FC: Abort not implemented\n");
882 return 1;
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
902 * cmd_slots for us.
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;
912 fcmd->done(SCpnt);
913 printk("FC: soft abort\n");
914 return SUCCESS;
915 } else {
916 printk("FC: hard abort refused\n");
917 return FAILED;
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)
933 fcp_cmd *cmd;
934 fcp_cmnd *fcmd;
935 fc_channel *fc = FC_SCMND(SCpnt);
936 DECLARE_MUTEX_LOCKED(sem);
938 if (!fc->rst_pkt) {
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);
945 fcmd->token = 0;
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)))
963 } else {
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);
989 down(&sem);
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;
1000 return FAILED;
1002 fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
1003 return SUCCESS;
1006 int fcp_scsi_bus_reset(Scsi_Cmnd *SCpnt)
1008 printk ("FC: bus reset!\n");
1009 return FAILED;
1012 int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
1014 fc_channel *fc = FC_SCMND(SCpnt);
1015 fcp_cmnd *fcmd = FCP_CMND(SCpnt);
1016 int i;
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;
1023 fcmd->done(SCpnt);
1024 fc->cmd_slots[i] = NULL;
1027 fc->reset(fc);
1028 fc->abort_count = 0;
1029 if (fcp_initialize(fc, 1)) return SUCCESS;
1030 else return FAILED;
1033 static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
1035 long i;
1037 i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
1038 set_bit (i, fc->scsi_bitmap);
1039 fcmd->token = i;
1040 fc->scsi_free--;
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;
1048 fc_hdr *fch;
1049 lse l;
1050 int i;
1052 fcmd = &_fcmd;
1053 memset(fcmd, 0, sizeof(fcmd));
1054 FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
1055 fch = &fcmd->fch;
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);
1061 fch->param = 0;
1062 fcmd->cmd = fc_sync_dma_entry (data, 2 * len, fc);
1063 fcmd->rsp = fcmd->cmd + len;
1064 fcmd->cmdlen = len;
1065 fcmd->rsplen = len;
1066 fcmd->data = (dma_handle)NULL;
1067 fcmd->fc = fc;
1068 fcmd->class = FC_CLASS_SIMPLE;
1069 fcmd->proto = TYPE_EXTENDED_LS;
1071 memset (&l, 0, sizeof(lse));
1072 l.magic = LSEMAGIC;
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);
1083 for (i = 0;;) {
1084 l.timer.expires = jiffies + 5 * HZ;
1085 add_timer(&l.timer);
1086 down(&l.sem);
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);
1096 fc->scsi_free++;
1097 fc_sync_dma_exit (fcmd->cmd, 2 * len, fc);
1098 return l.status;
1101 int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
1103 logi *l;
1104 int status;
1106 l = (logi *)kmalloc(2 * sizeof(logi), GFP_DMA);
1107 if (!l) return -ENOMEM;
1108 memset(l, 0, 2 * sizeof(logi));
1109 l->code = LS_PLOGI;
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) {
1117 #ifdef FCDEBUG
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]))
1121 #endif
1122 memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
1123 memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
1124 } else
1125 status = FC_STATUS_BAD_RSP;
1127 kfree(l);
1128 return status;
1131 typedef struct {
1132 unsigned int code;
1133 unsigned params[4];
1134 } prli;
1136 int fc_do_prli(fc_channel *fc, unsigned char alpa)
1138 prli *p;
1139 int status;
1141 p = (prli *)kmalloc(2 * sizeof(prli), GFP_DMA);
1142 if (!p) return -ENOMEM;
1143 memset(p, 0, 2 * sizeof(prli));
1144 p->code = LS_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;
1150 kfree(p);
1151 return status;
1154 #ifdef MODULE
1155 int init_module(void)
1157 return 0;
1160 void cleanup_module(void)
1163 #endif