[PATCH] swsusp: Use suspend_console
[pv_ops_mirror.git] / drivers / fc4 / fc.c
blob22d17474755f73ac86660c499e486750132f3c60
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/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>
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>
43 /* #define FCDEBUG */
45 #define fc_printk printk ("%s: ", fc->name); printk
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
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
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)))
75 static int fcp_scsi_queue_it(fc_channel *, 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)
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;
92 static void fcp_scsi_remove_queue (fc_channel *fc, fcp_cmnd *fcmd)
94 if (fcmd == fcmd->next) {
95 fc->scsi_que = NULL;
96 return;
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
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;
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;
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;
139 static void fcp_login_timeout(unsigned long data)
141 ls *l = (ls *)data;
142 FCND(("Login timeout\n"))
143 up(&l->sem);
146 static void fcp_login_done(fc_channel *fc, int i, int status)
148 fcp_cmnd *fcmd;
149 logi *plogi;
150 fc_hdr *fch;
151 ls *l = (ls *)fc->ls;
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;
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
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");
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;
201 } else {
202 i -= l->count;
203 if (fc->state != FC_STATE_FPORT_OK) {
204 FCD(("Unexpected N-PORT rsp received"))
205 return;
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);
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;
243 static void fcp_report_map_done(fc_channel *fc, int i, int status)
245 fcp_cmnd *fcmd;
246 fc_hdr *fch;
247 unsigned char j;
248 ls *l = (ls *)fc->ls;
249 fc_al_posmap *p;
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
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]))
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 *)kzalloc(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 /* 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)
280 break;
281 k = p->len;
282 if (i == p->len)
283 i = 0;
284 else {
285 p->len--;
286 i++;
288 fc->posmap->len = p->len;
289 for (j = 0; j < p->len; j++) {
290 if (i == k) i = 0;
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))
298 up(&l->sem);
299 break;
300 case FC_STATUS_POINTTOPOINT: /* We're Point-to-Point, no AL... */
301 FCD(("SID %d DID %d\n", fc->sid, fc->did))
302 fcmd = l->fcmds + i;
303 dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi),
304 DMA_BIDIRECTIONAL);
305 fch = &fcmd->fch;
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);
312 fch->param = 0;
313 l->logi [3 * i].code = LS_FLOGI;
314 fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
315 DMA_BIDIRECTIONAL);
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);
324 break;
325 case FC_STATUS_ERR_OFFLINE:
326 fc->state = FC_STATE_MAYBEOFFLINE;
327 FCD (("FC is offline %d\n", l->grace[i]))
328 break;
329 default:
330 printk ("FLOGI failed for %s with status %d\n", fc->name, status);
331 /* Do some sort of error recovery here */
332 break;
336 void fcp_register(fc_channel *fc, u8 type, int unregister)
338 int size, i;
339 int slots = (fc->can_queue * 3) >> 1;
341 FCND(("Going to %sregister\n", unregister ? "un" : ""))
343 if (type == TYPE_SCSI_FCP) {
344 if (!unregister) {
345 fc->scsi_cmd_pool = (fcp_cmd *)
346 dma_alloc_consistent (fc->dev,
347 slots * (sizeof (fcp_cmd) + fc->rsp_size),
348 &fc->dma_scsi_cmd);
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 = (fcp_cmnd **)kzalloc(slots * sizeof(fcp_cmnd*), GFP_KERNEL);
359 fc->abort_count = 0;
360 } else {
361 fc->scsi_name[0] = 0;
362 kfree (fc->scsi_bitmap);
363 kfree (fc->cmd_slots);
364 FCND(("Unregistering\n"));
365 #if 0
366 if (fc->rst_pkt) {
367 if (fc->rst_pkt->eh_state == SCSI_STATE_UNUSED)
368 kfree(fc->rst_pkt);
369 else {
370 /* Can't happen. Some memory would be lost. */
371 printk("FC: Reset in progress. Now?!");
374 #endif
375 FCND(("Unregistered\n"));
377 } else
378 printk ("FC: %segistering unknown type %02x\n", unregister ? "Unr" : "R", type);
381 static void fcp_scsi_done(Scsi_Cmnd *SCpnt);
383 static inline void fcp_scsi_receive(fc_channel *fc, int token, int status, fc_hdr *fch)
385 fcp_cmnd *fcmd;
386 fcp_rsp *rsp;
387 int host_status;
388 Scsi_Cmnd *SCpnt;
389 int sense_len;
390 int rsp_status;
392 fcmd = fc->cmd_slots[token];
393 if (!fcmd) return;
394 rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
395 SCpnt = SC_FCMND(fcmd);
397 if (SCpnt->done != fcp_scsi_done)
398 return;
400 rsp_status = rsp->fcp_status;
401 FCD(("rsp_status %08x status %08x\n", rsp_status, status))
402 switch (status) {
403 case FC_STATUS_OK:
404 host_status=DID_OK;
406 if (rsp_status & FCP_STATUS_RESID) {
407 #ifdef FCDEBUG
408 FCD(("Resid %d\n", rsp->fcp_resid))
410 fcp_cmd *cmd = fc->scsi_cmd_pool + token;
411 int i;
413 printk ("Command ");
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));
419 printk ("\n");
421 #endif
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);
430 if (fcmd->data) {
431 if (SCpnt->use_sg)
432 dma_unmap_sg(fc->dev, (struct scatterlist *)SCpnt->request_buffer,
433 SCpnt->use_sg,
434 SCpnt->sc_data_direction);
435 else
436 dma_unmap_single(fc->dev, fcmd->data, SCpnt->request_bufflen,
437 SCpnt->sc_data_direction);
439 break;
440 default:
441 host_status=DID_ERROR; /* FIXME */
442 FCD(("Wrong FC status %d for token %d\n", status, token))
443 break;
446 if (status_byte(rsp_status) == QUEUE_FULL) {
447 printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc->name, SCpnt->device->channel, SCpnt->device->id, rsp_status);
450 SCpnt->result = (host_status << 16) | (rsp_status & 0xff);
451 #ifdef FCDEBUG
452 if (host_status || SCpnt->result || rsp_status) printk("FC: host_status %d, packet status %d\n",
453 host_status, SCpnt->result);
454 #endif
455 SCpnt->done = fcmd->done;
456 fcmd->done=NULL;
457 clear_bit(token, fc->scsi_bitmap);
458 fc->scsi_free++;
459 FCD(("Calling scsi_done with %08x\n", SCpnt->result))
460 SCpnt->scsi_done(SCpnt);
463 void fcp_receive_solicited(fc_channel *fc, int proto, int token, int status, fc_hdr *fch)
465 int magic;
466 FCD(("receive_solicited %d %d %d\n", proto, token, status))
467 switch (proto) {
468 case TYPE_SCSI_FCP:
469 fcp_scsi_receive(fc, token, status, fch); break;
470 case TYPE_EXTENDED_LS:
471 case PROTO_REPORT_AL_MAP:
472 magic = 0;
473 if (fc->ls)
474 magic = ((ls *)(fc->ls))->magic;
475 if (magic == LSMAGIC) {
476 ls *l = (ls *)fc->ls;
477 int i = (token >= l->count) ? token - l->count : token;
479 /* Let's be sure */
480 if ((unsigned)i < l->count && l->fcmds[i].fc == fc) {
481 if (proto == TYPE_EXTENDED_LS)
482 fcp_login_done(fc, token, status);
483 else
484 fcp_report_map_done(fc, token, status);
485 break;
488 FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc, fc->ls, fc->cmd_slots))
489 if (proto == TYPE_EXTENDED_LS && !fc->ls && fc->cmd_slots) {
490 fcp_cmnd *fcmd;
492 fcmd = fc->cmd_slots[token];
493 if (fcmd && fcmd->ls && ((ls *)(fcmd->ls))->magic == LSEMAGIC) {
494 lse *l = (lse *)fcmd->ls;
496 l->status = status;
497 up (&l->sem);
500 break;
501 case PROTO_OFFLINE:
502 if (fc->ls && ((lso *)(fc->ls))->magic == LSOMAGIC) {
503 lso *l = (lso *)fc->ls;
505 if ((unsigned)token < l->count && l->fcmds[token].fc == fc) {
506 /* Wow, OFFLINE response arrived :) */
507 FCD(("OFFLINE Response arrived\n"))
508 fc->state = FC_STATE_OFFLINE;
509 if (atomic_dec_and_test (&l->todo))
510 up(&l->sem);
513 break;
515 default:
516 break;
520 void fcp_state_change(fc_channel *fc, int state)
522 FCD(("state_change %d %d\n", state, fc->state))
523 if (state == FC_STATE_ONLINE && fc->state == FC_STATE_MAYBEOFFLINE)
524 fc->state = FC_STATE_UNINITED;
525 else if (state == FC_STATE_ONLINE)
526 printk (KERN_WARNING "%s: state change to ONLINE\n", fc->name);
527 else
528 printk (KERN_ERR "%s: state change to OFFLINE\n", fc->name);
531 int fcp_initialize(fc_channel *fcchain, int count)
533 fc_channel *fc;
534 fcp_cmnd *fcmd;
535 int i, retry, ret;
536 ls *l;
538 FCND(("fcp_inititialize %08lx\n", (long)fcp_init))
539 FCND(("fc_channels %08lx\n", (long)fc_channels))
540 FCND((" SID %d DID %d\n", fcchain->sid, fcchain->did))
541 l = kzalloc(sizeof (ls) + count, GFP_KERNEL);
542 if (!l) {
543 printk ("FC: Cannot allocate memory for initialization\n");
544 return -ENOMEM;
546 l->magic = LSMAGIC;
547 l->count = count;
548 FCND(("FCP Init for %d channels\n", count))
549 init_MUTEX_LOCKED(&l->sem);
550 init_timer(&l->timer);
551 l->timer.function = fcp_login_timeout;
552 l->timer.data = (unsigned long)l;
553 atomic_set (&l->todo, count);
554 l->logi = kzalloc (count * 3 * sizeof(logi), GFP_KERNEL);
555 l->fcmds = kzalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
556 if (!l->logi || !l->fcmds) {
557 kfree (l->logi);
558 kfree (l->fcmds);
559 kfree (l);
560 printk ("FC: Cannot allocate DMA memory for initialization\n");
561 return -ENOMEM;
563 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
564 fc->state = FC_STATE_UNINITED;
565 fc->rst_pkt = NULL; /* kmalloc when first used */
567 /* First try if we are in a AL topology */
568 FCND(("Initializing REPORT_MAP packets\n"))
569 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
570 fcmd = l->fcmds + i;
571 fc->login = fcmd;
572 fc->ls = (void *)l;
573 /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
574 fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
575 DMA_BIDIRECTIONAL);
576 fcmd->proto = PROTO_REPORT_AL_MAP;
577 fcmd->token = i;
578 fcmd->fc = fc;
580 for (retry = 0; retry < 8; retry++) {
581 int nqueued = 0;
582 FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
583 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
584 if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
585 continue;
586 disable_irq(fc->irq);
587 if (fc->state == FC_STATE_MAYBEOFFLINE) {
588 if (!l->grace[i]) {
589 l->grace[i]++;
590 FCD(("Grace\n"))
591 } else {
592 fc->state = FC_STATE_OFFLINE;
593 enable_irq(fc->irq);
594 dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
595 if (atomic_dec_and_test (&l->todo))
596 goto all_done;
599 ret = fc->hw_enque (fc, fc->login);
600 enable_irq(fc->irq);
601 if (!ret) {
602 nqueued++;
603 continue;
605 if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
606 /* Oh yes, this card handles Point-to-Point only, so let's try that. */
607 fc_hdr *fch;
609 FCD(("SID %d DID %d\n", fc->sid, fc->did))
610 fcmd = l->fcmds + i;
611 dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
612 fch = &fcmd->fch;
613 FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
614 FILL_FCHDR_SID(fch, 0);
615 FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
616 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
617 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
618 fch->param = 0;
619 l->logi [3 * i].code = LS_FLOGI;
620 fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
621 fcmd->rsp = fcmd->cmd + sizeof(logi);
622 fcmd->cmdlen = sizeof(logi);
623 fcmd->rsplen = sizeof(logi);
624 fcmd->data = (dma_addr_t)NULL;
625 fcmd->class = FC_CLASS_SIMPLE;
626 fcmd->proto = TYPE_EXTENDED_LS;
627 } else
628 printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
631 if (nqueued) {
632 l->timer.expires = jiffies + 5 * HZ;
633 add_timer(&l->timer);
635 down(&l->sem);
636 if (!atomic_read(&l->todo)) {
637 FCND(("All channels answered in time\n"))
638 break; /* All fc channels have answered us */
642 all_done:
643 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
644 fc->ls = NULL;
645 switch (fc->state) {
646 case FC_STATE_ONLINE: break;
647 case FC_STATE_OFFLINE: break;
648 default: dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
649 break;
652 del_timer(&l->timer);
653 kfree (l->logi);
654 kfree (l->fcmds);
655 kfree (l);
656 return 0;
659 int fcp_forceoffline(fc_channel *fcchain, int count)
661 fc_channel *fc;
662 fcp_cmnd *fcmd;
663 int i, ret;
664 lso l;
666 memset (&l, 0, sizeof(lso));
667 l.count = count;
668 l.magic = LSOMAGIC;
669 FCND(("FCP Force Offline for %d channels\n", count))
670 init_MUTEX_LOCKED(&l.sem);
671 init_timer(&l.timer);
672 l.timer.function = fcp_login_timeout;
673 l.timer.data = (unsigned long)&l;
674 atomic_set (&l.todo, count);
675 l.fcmds = kzalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
676 if (!l.fcmds) {
677 printk ("FC: Cannot allocate memory for forcing offline\n");
678 return -ENOMEM;
680 FCND(("Initializing OFFLINE packets\n"))
681 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
682 fc->state = FC_STATE_UNINITED;
683 fcmd = l.fcmds + i;
684 fc->login = fcmd;
685 fc->ls = (void *)&l;
686 fcmd->did = fc->did;
687 fcmd->class = FC_CLASS_OFFLINE;
688 fcmd->proto = PROTO_OFFLINE;
689 fcmd->token = i;
690 fcmd->fc = fc;
691 disable_irq(fc->irq);
692 ret = fc->hw_enque (fc, fc->login);
693 enable_irq(fc->irq);
694 if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
697 l.timer.expires = jiffies + 5 * HZ;
698 add_timer(&l.timer);
699 down(&l.sem);
700 del_timer(&l.timer);
702 for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
703 fc->ls = NULL;
704 kfree (l.fcmds);
705 return 0;
708 int fcp_init(fc_channel *fcchain)
710 fc_channel *fc;
711 int count=0;
712 int ret;
714 for (fc = fcchain; fc; fc = fc->next) {
715 fc->fcp_register = fcp_register;
716 count++;
719 ret = fcp_initialize (fcchain, count);
720 if (ret)
721 return ret;
723 if (!fc_channels)
724 fc_channels = fcchain;
725 else {
726 for (fc = fc_channels; fc->next; fc = fc->next);
727 fc->next = fcchain;
729 return ret;
732 void fcp_release(fc_channel *fcchain, int count) /* count must > 0 */
734 fc_channel *fc;
735 fc_channel *fcx;
737 for (fc = fcchain; --count && fc->next; fc = fc->next);
738 if (count) {
739 printk("FC: nothing to release\n");
740 return;
743 if (fc_channels == fcchain)
744 fc_channels = fc->next;
745 else {
746 for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
747 fcx->next = fc->next;
749 fc->next = NULL;
752 * We've just grabbed fcchain out of the fc_channel list
753 * and zero-terminated it, while destroying the count.
755 * Freeing the fc's is the low level driver's responsibility.
760 static void fcp_scsi_done (Scsi_Cmnd *SCpnt)
762 if (FCP_CMND(SCpnt)->done)
763 FCP_CMND(SCpnt)->done(SCpnt);
766 static int fcp_scsi_queue_it(fc_channel *fc, Scsi_Cmnd *SCpnt, fcp_cmnd *fcmd, int prepare)
768 long i;
769 fcp_cmd *cmd;
770 u32 fcp_cntl;
771 if (prepare) {
772 i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
773 set_bit (i, fc->scsi_bitmap);
774 fcmd->token = i;
775 cmd = fc->scsi_cmd_pool + i;
777 if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
778 /* Invalid channel/id/lun and couldn't map it into fcp_addr */
779 clear_bit (i, fc->scsi_bitmap);
780 SCpnt->result = (DID_BAD_TARGET << 16);
781 SCpnt->scsi_done(SCpnt);
782 return 0;
784 fc->scsi_free--;
785 fc->cmd_slots[fcmd->token] = fcmd;
787 if (SCpnt->device->tagged_supported) {
788 if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
789 fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
790 fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
791 } else
792 fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
793 } else
794 fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
795 if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
796 cmd->fcp_cntl = fcp_cntl;
797 fcmd->data = (dma_addr_t)NULL;
798 } else {
799 switch (SCpnt->cmnd[0]) {
800 case WRITE_6:
801 case WRITE_10:
802 case WRITE_12:
803 cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
804 default:
805 cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
807 if (!SCpnt->use_sg) {
808 cmd->fcp_data_len = SCpnt->request_bufflen;
809 fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
810 SCpnt->request_bufflen,
811 SCpnt->sc_data_direction);
812 } else {
813 struct scatterlist *sg = (struct scatterlist *)SCpnt->request_buffer;
814 int nents;
816 FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
817 nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
818 SCpnt->sc_data_direction);
819 if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
820 fcmd->data = sg_dma_address(sg);
821 cmd->fcp_data_len = sg_dma_len(sg);
824 memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
825 memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
826 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)))
828 FCD(("Trying to enque %p\n", fcmd))
829 if (!fc->scsi_que) {
830 if (!fc->hw_enque (fc, fcmd)) {
831 FCD(("hw_enque succeeded for %p\n", fcmd))
832 return 0;
835 FCD(("Putting into que1 %p\n", fcmd))
836 fcp_scsi_insert_queue (fc, fcmd);
837 return 0;
840 int fcp_scsi_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *))
842 fcp_cmnd *fcmd = FCP_CMND(SCpnt);
843 fc_channel *fc = FC_SCMND(SCpnt);
845 FCD(("Entering SCSI queuecommand %p\n", fcmd))
846 if (SCpnt->done != fcp_scsi_done) {
847 fcmd->done = SCpnt->done;
848 SCpnt->done = fcp_scsi_done;
849 SCpnt->scsi_done = done;
850 fcmd->proto = TYPE_SCSI_FCP;
851 if (!fc->scsi_free) {
852 FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
853 #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
854 printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
855 #endif
856 return 1;
858 return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
860 return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
863 void fcp_queue_empty(fc_channel *fc)
865 fcp_cmnd *fcmd;
867 FCD(("Queue empty\n"))
868 while ((fcmd = fc->scsi_que)) {
869 /* The hw told us we can try again queue some packet */
870 if (fc->hw_enque (fc, fcmd))
871 break;
872 fcp_scsi_remove_queue (fc, fcmd);
876 int fcp_scsi_abort(Scsi_Cmnd *SCpnt)
878 /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
879 fcp_cmnd *fcmd = FCP_CMND(SCpnt);
880 fc_channel *fc = FC_SCMND(SCpnt);
883 * We react to abort requests by simply forgetting
884 * about the command and pretending everything's sweet.
885 * This may or may not be silly. We can't, however,
886 * immediately reuse the command's cmd_slots slot,
887 * as its result may arrive later and we cannot
888 * check whether it is the aborted one, can't we?
890 * Therefore, after the first few aborts are done,
891 * we tell the scsi error handler to do something clever.
892 * It will eventually call host reset, refreshing
893 * cmd_slots for us.
895 * There is a theoretical chance that we sometimes allow
896 * more than can_queue packets to the jungle this way,
897 * but the worst outcome possible is a series of
898 * more aborts and eventually the dev_reset catharsis.
901 if (++fc->abort_count < (fc->can_queue >> 1)) {
902 SCpnt->result = DID_ABORT;
903 fcmd->done(SCpnt);
904 printk("FC: soft abort\n");
905 return SUCCESS;
906 } else {
907 printk("FC: hard abort refused\n");
908 return FAILED;
912 #if 0
913 void fcp_scsi_reset_done(Scsi_Cmnd *SCpnt)
915 fc_channel *fc = FC_SCMND(SCpnt);
917 fc->rst_pkt->eh_state = SCSI_STATE_FINISHED;
918 up(fc->rst_pkt->device->host->eh_action);
920 #endif
922 #define FCP_RESET_TIMEOUT (2*HZ)
924 int fcp_scsi_dev_reset(Scsi_Cmnd *SCpnt)
926 #if 0 /* broken junk, but if davem wants to compile this driver, let him.. */
927 unsigned long flags;
928 fcp_cmd *cmd;
929 fcp_cmnd *fcmd;
930 fc_channel *fc = FC_SCMND(SCpnt);
931 DECLARE_MUTEX_LOCKED(sem);
933 if (!fc->rst_pkt) {
934 fc->rst_pkt = (Scsi_Cmnd *) kmalloc(sizeof(SCpnt), GFP_KERNEL);
935 if (!fc->rst_pkt) return FAILED;
937 fcmd = FCP_CMND(fc->rst_pkt);
940 fcmd->token = 0;
941 cmd = fc->scsi_cmd_pool + 0;
942 FCD(("Preparing rst packet\n"))
943 fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd);
944 fc->rst_pkt->device = SCpnt->device;
945 fc->rst_pkt->cmd_len = 0;
947 fc->cmd_slots[0] = fcmd;
949 cmd->fcp_cntl = FCP_CNTL_QTYPE_ORDERED | FCP_CNTL_RESET;
950 fcmd->data = (dma_addr_t)NULL;
951 fcmd->proto = TYPE_SCSI_FCP;
953 memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
954 memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
955 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)))
956 } else {
957 fcmd = FCP_CMND(fc->rst_pkt);
958 if (fc->rst_pkt->eh_state == SCSI_STATE_QUEUED)
959 return FAILED; /* or SUCCESS. Only these */
961 fc->rst_pkt->done = NULL;
964 fc->rst_pkt->eh_state = SCSI_STATE_QUEUED;
965 init_timer(&fc->rst_pkt->eh_timeout);
966 fc->rst_pkt->eh_timeout.data = (unsigned long) fc->rst_pkt;
967 fc->rst_pkt->eh_timeout.expires = jiffies + FCP_RESET_TIMEOUT;
968 fc->rst_pkt->eh_timeout.function = (void (*)(unsigned long))fcp_scsi_reset_done;
970 add_timer(&fc->rst_pkt->eh_timeout);
973 * Set up the semaphore so we wait for the command to complete.
976 fc->rst_pkt->device->host->eh_action = &sem;
978 fc->rst_pkt->done = fcp_scsi_reset_done;
980 spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
981 fcp_scsi_queue_it(fc, fc->rst_pkt, fcmd, 0);
982 spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
984 down(&sem);
986 fc->rst_pkt->device->host->eh_action = NULL;
987 del_timer(&fc->rst_pkt->eh_timeout);
990 * See if timeout. If so, tell the host to forget about it.
991 * In other words, we don't want a callback any more.
993 if (fc->rst_pkt->eh_state == SCSI_STATE_TIMEOUT ) {
994 fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
995 return FAILED;
997 fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
998 #endif
999 return SUCCESS;
1002 static int __fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
1004 fc_channel *fc = FC_SCMND(SCpnt);
1005 fcp_cmnd *fcmd = FCP_CMND(SCpnt);
1006 int i;
1008 printk ("FC: host reset\n");
1010 for (i=0; i < fc->can_queue; i++) {
1011 if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
1012 SCpnt->result = DID_RESET;
1013 fcmd->done(SCpnt);
1014 fc->cmd_slots[i] = NULL;
1017 fc->reset(fc);
1018 fc->abort_count = 0;
1019 if (fcp_initialize(fc, 1)) return SUCCESS;
1020 else return FAILED;
1023 int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
1025 unsigned long flags;
1026 int rc;
1028 spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
1029 rc = __fcp_scsi_host_reset(SCpnt);
1030 spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
1032 return rc;
1035 static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
1037 long i;
1039 i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
1040 set_bit (i, fc->scsi_bitmap);
1041 fcmd->token = i;
1042 fc->scsi_free--;
1043 fc->cmd_slots[fcmd->token] = fcmd;
1044 return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
1047 static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
1049 fcp_cmnd _fcmd, *fcmd;
1050 fc_hdr *fch;
1051 lse l;
1052 int i;
1054 fcmd = &_fcmd;
1055 memset(fcmd, 0, sizeof(fcp_cmnd));
1056 FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
1057 fch = &fcmd->fch;
1058 FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
1059 FILL_FCHDR_SID(fch, fc->sid);
1060 FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
1061 FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
1062 FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
1063 fch->param = 0;
1064 fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
1065 fcmd->rsp = fcmd->cmd + len;
1066 fcmd->cmdlen = len;
1067 fcmd->rsplen = len;
1068 fcmd->data = (dma_addr_t)NULL;
1069 fcmd->fc = fc;
1070 fcmd->class = FC_CLASS_SIMPLE;
1071 fcmd->proto = TYPE_EXTENDED_LS;
1073 memset (&l, 0, sizeof(lse));
1074 l.magic = LSEMAGIC;
1075 init_MUTEX_LOCKED(&l.sem);
1076 l.timer.function = fcp_login_timeout;
1077 l.timer.data = (unsigned long)&l;
1078 l.status = FC_STATUS_TIMED_OUT;
1079 fcmd->ls = (void *)&l;
1081 disable_irq(fc->irq);
1082 fcp_els_queue_it(fc, fcmd);
1083 enable_irq(fc->irq);
1085 for (i = 0;;) {
1086 l.timer.expires = jiffies + 5 * HZ;
1087 add_timer(&l.timer);
1088 down(&l.sem);
1089 del_timer(&l.timer);
1090 if (l.status != FC_STATUS_TIMED_OUT) break;
1091 if (++i == 3) break;
1092 disable_irq(fc->irq);
1093 fcp_scsi_queue_it(fc, NULL, fcmd, 0);
1094 enable_irq(fc->irq);
1097 clear_bit(fcmd->token, fc->scsi_bitmap);
1098 fc->scsi_free++;
1099 dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
1100 return l.status;
1103 int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
1105 logi *l;
1106 int status;
1108 l = (logi *)kzalloc(2 * sizeof(logi), GFP_KERNEL);
1109 if (!l) return -ENOMEM;
1110 l->code = LS_PLOGI;
1111 memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
1112 memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
1113 memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
1114 memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
1115 status = fc_do_els(fc, alpa, l, sizeof(logi));
1116 if (status == FC_STATUS_OK) {
1117 if (l[1].code == LS_ACC) {
1118 #ifdef FCDEBUG
1119 u32 *u = (u32 *)&l[1].nport_wwn;
1120 FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa,
1121 u[0], u[1], u[2], u[3]))
1122 #endif
1123 memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
1124 memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
1125 } else
1126 status = FC_STATUS_BAD_RSP;
1128 kfree(l);
1129 return status;
1132 typedef struct {
1133 unsigned int code;
1134 unsigned params[4];
1135 } prli;
1137 int fc_do_prli(fc_channel *fc, unsigned char alpa)
1139 prli *p;
1140 int status;
1142 p = (prli *)kzalloc(2 * sizeof(prli), GFP_KERNEL);
1143 if (!p) return -ENOMEM;
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 MODULE_LICENSE("GPL");