Merge master.kernel.org:/pub/scm/linux/kernel/git/holtmann/bluetooth-2.6
[linux-2.6] / drivers / scsi / qla2xxx / qla_isr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <scsi/scsi_tcq.h>
10
11 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
12 static void qla2x00_async_event(scsi_qla_host_t *, uint16_t *);
13 static void qla2x00_process_completed_request(struct scsi_qla_host *, uint32_t);
14 static void qla2x00_status_entry(scsi_qla_host_t *, void *);
15 static void qla2x00_status_cont_entry(scsi_qla_host_t *, sts_cont_entry_t *);
16 static void qla2x00_error_entry(scsi_qla_host_t *, sts_entry_t *);
17 static void qla2x00_ms_entry(scsi_qla_host_t *, ms_iocb_entry_t *);
18
19 static void qla24xx_ms_entry(scsi_qla_host_t *, struct ct_entry_24xx *);
20
21 /**
22  * qla2100_intr_handler() - Process interrupts for the ISP2100 and ISP2200.
23  * @irq:
24  * @dev_id: SCSI driver HA context
25  *
26  * Called by system whenever the host adapter generates an interrupt.
27  *
28  * Returns handled flag.
29  */
30 irqreturn_t
31 qla2100_intr_handler(int irq, void *dev_id)
32 {
33         scsi_qla_host_t *ha;
34         struct device_reg_2xxx __iomem *reg;
35         int             status;
36         unsigned long   flags;
37         unsigned long   iter;
38         uint16_t        mb[4];
39
40         ha = (scsi_qla_host_t *) dev_id;
41         if (!ha) {
42                 printk(KERN_INFO
43                     "%s(): NULL host pointer\n", __func__);
44                 return (IRQ_NONE);
45         }
46
47         reg = &ha->iobase->isp;
48         status = 0;
49
50         spin_lock_irqsave(&ha->hardware_lock, flags);
51         for (iter = 50; iter--; ) {
52                 if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
53                         break;
54
55                 if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
56                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
57                         RD_REG_WORD(&reg->hccr);
58
59                         /* Get mailbox data. */
60                         mb[0] = RD_MAILBOX_REG(ha, reg, 0);
61                         if (mb[0] > 0x3fff && mb[0] < 0x8000) {
62                                 qla2x00_mbx_completion(ha, mb[0]);
63                                 status |= MBX_INTERRUPT;
64                         } else if (mb[0] > 0x7fff && mb[0] < 0xc000) {
65                                 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
66                                 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
67                                 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
68                                 qla2x00_async_event(ha, mb);
69                         } else {
70                                 /*EMPTY*/
71                                 DEBUG2(printk("scsi(%ld): Unrecognized "
72                                     "interrupt type (%d).\n",
73                                     ha->host_no, mb[0]));
74                         }
75                         /* Release mailbox registers. */
76                         WRT_REG_WORD(&reg->semaphore, 0);
77                         RD_REG_WORD(&reg->semaphore);
78                 } else {
79                         qla2x00_process_response_queue(ha);
80
81                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
82                         RD_REG_WORD(&reg->hccr);
83                 }
84         }
85         spin_unlock_irqrestore(&ha->hardware_lock, flags);
86
87         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
88             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
89                 spin_lock_irqsave(&ha->mbx_reg_lock, flags);
90
91                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
92                 up(&ha->mbx_intr_sem);
93
94                 spin_unlock_irqrestore(&ha->mbx_reg_lock, flags);
95         }
96
97         return (IRQ_HANDLED);
98 }
99
100 /**
101  * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
102  * @irq:
103  * @dev_id: SCSI driver HA context
104  *
105  * Called by system whenever the host adapter generates an interrupt.
106  *
107  * Returns handled flag.
108  */
109 irqreturn_t
110 qla2300_intr_handler(int irq, void *dev_id)
111 {
112         scsi_qla_host_t *ha;
113         struct device_reg_2xxx __iomem *reg;
114         int             status;
115         unsigned long   flags;
116         unsigned long   iter;
117         uint32_t        stat;
118         uint16_t        hccr;
119         uint16_t        mb[4];
120
121         ha = (scsi_qla_host_t *) dev_id;
122         if (!ha) {
123                 printk(KERN_INFO
124                     "%s(): NULL host pointer\n", __func__);
125                 return (IRQ_NONE);
126         }
127
128         reg = &ha->iobase->isp;
129         status = 0;
130
131         spin_lock_irqsave(&ha->hardware_lock, flags);
132         for (iter = 50; iter--; ) {
133                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
134                 if (stat & HSR_RISC_PAUSED) {
135                         hccr = RD_REG_WORD(&reg->hccr);
136                         if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
137                                 qla_printk(KERN_INFO, ha, "Parity error -- "
138                                     "HCCR=%x, Dumping firmware!\n", hccr);
139                         else
140                                 qla_printk(KERN_INFO, ha, "RISC paused -- "
141                                     "HCCR=%x, Dumping firmware!\n", hccr);
142
143                         /*
144                          * Issue a "HARD" reset in order for the RISC
145                          * interrupt bit to be cleared.  Schedule a big
146                          * hammmer to get out of the RISC PAUSED state.
147                          */
148                         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
149                         RD_REG_WORD(&reg->hccr);
150
151                         ha->isp_ops.fw_dump(ha, 1);
152                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
153                         break;
154                 } else if ((stat & HSR_RISC_INT) == 0)
155                         break;
156
157                 switch (stat & 0xff) {
158                 case 0x1:
159                 case 0x2:
160                 case 0x10:
161                 case 0x11:
162                         qla2x00_mbx_completion(ha, MSW(stat));
163                         status |= MBX_INTERRUPT;
164
165                         /* Release mailbox registers. */
166                         WRT_REG_WORD(&reg->semaphore, 0);
167                         break;
168                 case 0x12:
169                         mb[0] = MSW(stat);
170                         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
171                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
172                         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
173                         qla2x00_async_event(ha, mb);
174                         break;
175                 case 0x13:
176                         qla2x00_process_response_queue(ha);
177                         break;
178                 case 0x15:
179                         mb[0] = MBA_CMPLT_1_16BIT;
180                         mb[1] = MSW(stat);
181                         qla2x00_async_event(ha, mb);
182                         break;
183                 case 0x16:
184                         mb[0] = MBA_SCSI_COMPLETION;
185                         mb[1] = MSW(stat);
186                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
187                         qla2x00_async_event(ha, mb);
188                         break;
189                 default:
190                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
191                             "(%d).\n",
192                             ha->host_no, stat & 0xff));
193                         break;
194                 }
195                 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
196                 RD_REG_WORD_RELAXED(&reg->hccr);
197         }
198         spin_unlock_irqrestore(&ha->hardware_lock, flags);
199
200         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
201             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
202                 spin_lock_irqsave(&ha->mbx_reg_lock, flags);
203
204                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
205                 up(&ha->mbx_intr_sem);
206
207                 spin_unlock_irqrestore(&ha->mbx_reg_lock, flags);
208         }
209
210         return (IRQ_HANDLED);
211 }
212
213 /**
214  * qla2x00_mbx_completion() - Process mailbox command completions.
215  * @ha: SCSI driver HA context
216  * @mb0: Mailbox0 register
217  */
218 static void
219 qla2x00_mbx_completion(scsi_qla_host_t *ha, uint16_t mb0)
220 {
221         uint16_t        cnt;
222         uint16_t __iomem *wptr;
223         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
224
225         /* Load return mailbox registers. */
226         ha->flags.mbox_int = 1;
227         ha->mailbox_out[0] = mb0;
228         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
229
230         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
231                 if (IS_QLA2200(ha) && cnt == 8)
232                         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
233                 if (cnt == 4 || cnt == 5)
234                         ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
235                 else
236                         ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
237
238                 wptr++;
239         }
240
241         if (ha->mcp) {
242                 DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
243                     __func__, ha->host_no, ha->mcp->mb[0]));
244         } else {
245                 DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
246                     __func__, ha->host_no));
247         }
248 }
249
250 /**
251  * qla2x00_async_event() - Process aynchronous events.
252  * @ha: SCSI driver HA context
253  * @mb: Mailbox registers (0 - 3)
254  */
255 static void
256 qla2x00_async_event(scsi_qla_host_t *ha, uint16_t *mb)
257 {
258 #define LS_UNKNOWN      2
259         static char     *link_speeds[5] = { "1", "2", "?", "4", "10" };
260         char            *link_speed;
261         uint16_t        handle_cnt;
262         uint16_t        cnt;
263         uint32_t        handles[5];
264         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
265         uint32_t        rscn_entry, host_pid;
266         uint8_t         rscn_queue_index;
267
268         /* Setup to process RIO completion. */
269         handle_cnt = 0;
270         switch (mb[0]) {
271         case MBA_SCSI_COMPLETION:
272                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
273                 handle_cnt = 1;
274                 break;
275         case MBA_CMPLT_1_16BIT:
276                 handles[0] = mb[1];
277                 handle_cnt = 1;
278                 mb[0] = MBA_SCSI_COMPLETION;
279                 break;
280         case MBA_CMPLT_2_16BIT:
281                 handles[0] = mb[1];
282                 handles[1] = mb[2];
283                 handle_cnt = 2;
284                 mb[0] = MBA_SCSI_COMPLETION;
285                 break;
286         case MBA_CMPLT_3_16BIT:
287                 handles[0] = mb[1];
288                 handles[1] = mb[2];
289                 handles[2] = mb[3];
290                 handle_cnt = 3;
291                 mb[0] = MBA_SCSI_COMPLETION;
292                 break;
293         case MBA_CMPLT_4_16BIT:
294                 handles[0] = mb[1];
295                 handles[1] = mb[2];
296                 handles[2] = mb[3];
297                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
298                 handle_cnt = 4;
299                 mb[0] = MBA_SCSI_COMPLETION;
300                 break;
301         case MBA_CMPLT_5_16BIT:
302                 handles[0] = mb[1];
303                 handles[1] = mb[2];
304                 handles[2] = mb[3];
305                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
306                 handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
307                 handle_cnt = 5;
308                 mb[0] = MBA_SCSI_COMPLETION;
309                 break;
310         case MBA_CMPLT_2_32BIT:
311                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
312                 handles[1] = le32_to_cpu(
313                     ((uint32_t)(RD_MAILBOX_REG(ha, reg, 7) << 16)) |
314                     RD_MAILBOX_REG(ha, reg, 6));
315                 handle_cnt = 2;
316                 mb[0] = MBA_SCSI_COMPLETION;
317                 break;
318         default:
319                 break;
320         }
321
322         switch (mb[0]) {
323         case MBA_SCSI_COMPLETION:       /* Fast Post */
324                 if (!ha->flags.online)
325                         break;
326
327                 for (cnt = 0; cnt < handle_cnt; cnt++)
328                         qla2x00_process_completed_request(ha, handles[cnt]);
329                 break;
330
331         case MBA_RESET:                 /* Reset */
332                 DEBUG2(printk("scsi(%ld): Asynchronous RESET.\n", ha->host_no));
333
334                 set_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
335                 break;
336
337         case MBA_SYSTEM_ERR:            /* System Error */
338                 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
339                 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
340                 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
341
342                 qla_printk(KERN_INFO, ha,
343                     "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh.\n",
344                     mb[1], mb[2], mb[3]);
345
346                 ha->isp_ops.fw_dump(ha, 1);
347
348                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
349                         if (mb[1] == 0 && mb[2] == 0) {
350                                 qla_printk(KERN_ERR, ha,
351                                     "Unrecoverable Hardware Error: adapter "
352                                     "marked OFFLINE!\n");
353                                 ha->flags.online = 0;
354                         } else
355                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
356                 } else if (mb[1] == 0) {
357                         qla_printk(KERN_INFO, ha,
358                             "Unrecoverable Hardware Error: adapter marked "
359                             "OFFLINE!\n");
360                         ha->flags.online = 0;
361                 } else
362                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
363                 break;
364
365         case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
366                 DEBUG2(printk("scsi(%ld): ISP Request Transfer Error.\n",
367                     ha->host_no));
368                 qla_printk(KERN_WARNING, ha, "ISP Request Transfer Error.\n");
369
370                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
371                 break;
372
373         case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
374                 DEBUG2(printk("scsi(%ld): ISP Response Transfer Error.\n",
375                     ha->host_no));
376                 qla_printk(KERN_WARNING, ha, "ISP Response Transfer Error.\n");
377
378                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
379                 break;
380
381         case MBA_WAKEUP_THRES:          /* Request Queue Wake-up */
382                 DEBUG2(printk("scsi(%ld): Asynchronous WAKEUP_THRES.\n",
383                     ha->host_no));
384                 break;
385
386         case MBA_LIP_OCCURRED:          /* Loop Initialization Procedure */
387                 DEBUG2(printk("scsi(%ld): LIP occured (%x).\n", ha->host_no,
388                     mb[1]));
389                 qla_printk(KERN_INFO, ha, "LIP occured (%x).\n", mb[1]);
390
391                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
392                         atomic_set(&ha->loop_state, LOOP_DOWN);
393                         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
394                         qla2x00_mark_all_devices_lost(ha, 1);
395                 }
396
397                 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
398
399                 ha->flags.management_server_logged_in = 0;
400                 break;
401
402         case MBA_LOOP_UP:               /* Loop Up Event */
403                 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
404                         link_speed = link_speeds[0];
405                         ha->link_data_rate = PORT_SPEED_1GB;
406                 } else {
407                         link_speed = link_speeds[LS_UNKNOWN];
408                         if (mb[1] < 5)
409                                 link_speed = link_speeds[mb[1]];
410                         ha->link_data_rate = mb[1];
411                 }
412
413                 DEBUG2(printk("scsi(%ld): Asynchronous LOOP UP (%s Gbps).\n",
414                     ha->host_no, link_speed));
415                 qla_printk(KERN_INFO, ha, "LOOP UP detected (%s Gbps).\n",
416                     link_speed);
417
418                 ha->flags.management_server_logged_in = 0;
419                 break;
420
421         case MBA_LOOP_DOWN:             /* Loop Down Event */
422                 DEBUG2(printk("scsi(%ld): Asynchronous LOOP DOWN (%x).\n",
423                     ha->host_no, mb[1]));
424                 qla_printk(KERN_INFO, ha, "LOOP DOWN detected (%x).\n", mb[1]);
425
426                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
427                         atomic_set(&ha->loop_state, LOOP_DOWN);
428                         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
429                         ha->device_flags |= DFLG_NO_CABLE;
430                         qla2x00_mark_all_devices_lost(ha, 1);
431                 }
432
433                 ha->flags.management_server_logged_in = 0;
434                 ha->link_data_rate = PORT_SPEED_UNKNOWN;
435                 if (ql2xfdmienable)
436                         set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
437                 break;
438
439         case MBA_LIP_RESET:             /* LIP reset occurred */
440                 DEBUG2(printk("scsi(%ld): Asynchronous LIP RESET (%x).\n",
441                     ha->host_no, mb[1]));
442                 qla_printk(KERN_INFO, ha,
443                     "LIP reset occured (%x).\n", mb[1]);
444
445                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
446                         atomic_set(&ha->loop_state, LOOP_DOWN);
447                         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
448                         qla2x00_mark_all_devices_lost(ha, 1);
449                 }
450
451                 set_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
452
453                 ha->operating_mode = LOOP;
454                 ha->flags.management_server_logged_in = 0;
455                 break;
456
457         case MBA_POINT_TO_POINT:        /* Point-to-Point */
458                 if (IS_QLA2100(ha))
459                         break;
460
461                 DEBUG2(printk("scsi(%ld): Asynchronous P2P MODE received.\n",
462                     ha->host_no));
463
464                 /*
465                  * Until there's a transition from loop down to loop up, treat
466                  * this as loop down only.
467                  */
468                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
469                         atomic_set(&ha->loop_state, LOOP_DOWN);
470                         if (!atomic_read(&ha->loop_down_timer))
471                                 atomic_set(&ha->loop_down_timer,
472                                     LOOP_DOWN_TIME);
473                         qla2x00_mark_all_devices_lost(ha, 1);
474                 }
475
476                 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
477                         set_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
478                 }
479                 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
480
481                 ha->flags.gpsc_supported = 1;
482                 break;
483
484         case MBA_CHG_IN_CONNECTION:     /* Change in connection mode */
485                 if (IS_QLA2100(ha))
486                         break;
487
488                 DEBUG2(printk("scsi(%ld): Asynchronous Change In Connection "
489                     "received.\n",
490                     ha->host_no));
491                 qla_printk(KERN_INFO, ha,
492                     "Configuration change detected: value=%x.\n", mb[1]);
493
494                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
495                         atomic_set(&ha->loop_state, LOOP_DOWN);
496                         if (!atomic_read(&ha->loop_down_timer))
497                                 atomic_set(&ha->loop_down_timer,
498                                     LOOP_DOWN_TIME);
499                         qla2x00_mark_all_devices_lost(ha, 1);
500                 }
501
502                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
503                 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
504                 break;
505
506         case MBA_PORT_UPDATE:           /* Port database update */
507                 /*
508                  * If PORT UPDATE is global (recieved LIP_OCCURED/LIP_RESET
509                  * event etc. earlier indicating loop is down) then process
510                  * it.  Otherwise ignore it and Wait for RSCN to come in.
511                  */
512                 atomic_set(&ha->loop_down_timer, 0);
513                 if (atomic_read(&ha->loop_state) != LOOP_DOWN &&
514                     atomic_read(&ha->loop_state) != LOOP_DEAD) {
515                         DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE "
516                             "ignored %04x/%04x/%04x.\n", ha->host_no, mb[1],
517                             mb[2], mb[3]));
518                         break;
519                 }
520
521                 DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
522                     ha->host_no));
523                 DEBUG(printk(KERN_INFO
524                     "scsi(%ld): Port database changed %04x %04x %04x.\n",
525                     ha->host_no, mb[1], mb[2], mb[3]));
526
527                 /*
528                  * Mark all devices as missing so we will login again.
529                  */
530                 atomic_set(&ha->loop_state, LOOP_UP);
531
532                 qla2x00_mark_all_devices_lost(ha, 1);
533
534                 ha->flags.rscn_queue_overflow = 1;
535
536                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
537                 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
538                 break;
539
540         case MBA_RSCN_UPDATE:           /* State Change Registration */
541                 DEBUG2(printk("scsi(%ld): Asynchronous RSCR UPDATE.\n",
542                     ha->host_no));
543                 DEBUG(printk(KERN_INFO
544                     "scsi(%ld): RSCN database changed -- %04x %04x.\n",
545                     ha->host_no, mb[1], mb[2]));
546
547                 rscn_entry = (mb[1] << 16) | mb[2];
548                 host_pid = (ha->d_id.b.domain << 16) | (ha->d_id.b.area << 8) |
549                     ha->d_id.b.al_pa;
550                 if (rscn_entry == host_pid) {
551                         DEBUG(printk(KERN_INFO
552                             "scsi(%ld): Ignoring RSCN update to local host "
553                             "port ID (%06x)\n",
554                             ha->host_no, host_pid));
555                         break;
556                 }
557
558                 rscn_queue_index = ha->rscn_in_ptr + 1;
559                 if (rscn_queue_index == MAX_RSCN_COUNT)
560                         rscn_queue_index = 0;
561                 if (rscn_queue_index != ha->rscn_out_ptr) {
562                         ha->rscn_queue[ha->rscn_in_ptr] = rscn_entry;
563                         ha->rscn_in_ptr = rscn_queue_index;
564                 } else {
565                         ha->flags.rscn_queue_overflow = 1;
566                 }
567
568                 atomic_set(&ha->loop_state, LOOP_UPDATE);
569                 atomic_set(&ha->loop_down_timer, 0);
570                 ha->flags.management_server_logged_in = 0;
571
572                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
573                 set_bit(RSCN_UPDATE, &ha->dpc_flags);
574                 break;
575
576         /* case MBA_RIO_RESPONSE: */
577         case MBA_ZIO_RESPONSE:
578                 DEBUG2(printk("scsi(%ld): [R|Z]IO update completion.\n",
579                     ha->host_no));
580                 DEBUG(printk(KERN_INFO
581                     "scsi(%ld): [R|Z]IO update completion.\n",
582                     ha->host_no));
583
584                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
585                         qla24xx_process_response_queue(ha);
586                 else
587                         qla2x00_process_response_queue(ha);
588                 break;
589
590         case MBA_DISCARD_RND_FRAME:
591                 DEBUG2(printk("scsi(%ld): Discard RND Frame -- %04x %04x "
592                     "%04x.\n", ha->host_no, mb[1], mb[2], mb[3]));
593                 break;
594
595         case MBA_TRACE_NOTIFICATION:
596                 DEBUG2(printk("scsi(%ld): Trace Notification -- %04x %04x.\n",
597                 ha->host_no, mb[1], mb[2]));
598                 break;
599         }
600 }
601
602 static void
603 qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, void *data)
604 {
605         fc_port_t *fcport = data;
606
607         if (fcport->ha->max_q_depth <= sdev->queue_depth)
608                 return;
609
610         if (sdev->ordered_tags)
611                 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
612                     sdev->queue_depth + 1);
613         else
614                 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
615                     sdev->queue_depth + 1);
616
617         fcport->last_ramp_up = jiffies;
618
619         DEBUG2(qla_printk(KERN_INFO, fcport->ha,
620             "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
621             fcport->ha->host_no, sdev->channel, sdev->id, sdev->lun,
622             sdev->queue_depth));
623 }
624
625 static void
626 qla2x00_adjust_sdev_qdepth_down(struct scsi_device *sdev, void *data)
627 {
628         fc_port_t *fcport = data;
629
630         if (!scsi_track_queue_full(sdev, sdev->queue_depth - 1))
631                 return;
632
633         DEBUG2(qla_printk(KERN_INFO, fcport->ha,
634             "scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
635             fcport->ha->host_no, sdev->channel, sdev->id, sdev->lun,
636             sdev->queue_depth));
637 }
638
639 static inline void
640 qla2x00_ramp_up_queue_depth(scsi_qla_host_t *ha, srb_t *sp)
641 {
642         fc_port_t *fcport;
643         struct scsi_device *sdev;
644
645         sdev = sp->cmd->device;
646         if (sdev->queue_depth >= ha->max_q_depth)
647                 return;
648
649         fcport = sp->fcport;
650         if (time_before(jiffies,
651             fcport->last_ramp_up + ql2xqfullrampup * HZ))
652                 return;
653         if (time_before(jiffies,
654             fcport->last_queue_full + ql2xqfullrampup * HZ))
655                 return;
656
657         spin_unlock_irq(&ha->hardware_lock);
658         starget_for_each_device(sdev->sdev_target, fcport,
659             qla2x00_adjust_sdev_qdepth_up);
660         spin_lock_irq(&ha->hardware_lock);
661 }
662
663 /**
664  * qla2x00_process_completed_request() - Process a Fast Post response.
665  * @ha: SCSI driver HA context
666  * @index: SRB index
667  */
668 static void
669 qla2x00_process_completed_request(struct scsi_qla_host *ha, uint32_t index)
670 {
671         srb_t *sp;
672
673         /* Validate handle. */
674         if (index >= MAX_OUTSTANDING_COMMANDS) {
675                 DEBUG2(printk("scsi(%ld): Invalid SCSI completion handle %d.\n",
676                     ha->host_no, index));
677                 qla_printk(KERN_WARNING, ha,
678                     "Invalid SCSI completion handle %d.\n", index);
679
680                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
681                 return;
682         }
683
684         sp = ha->outstanding_cmds[index];
685         if (sp) {
686                 /* Free outstanding command slot. */
687                 ha->outstanding_cmds[index] = NULL;
688
689                 CMD_COMPL_STATUS(sp->cmd) = 0L;
690                 CMD_SCSI_STATUS(sp->cmd) = 0L;
691
692                 /* Save ISP completion status */
693                 sp->cmd->result = DID_OK << 16;
694
695                 qla2x00_ramp_up_queue_depth(ha, sp);
696                 qla2x00_sp_compl(ha, sp);
697         } else {
698                 DEBUG2(printk("scsi(%ld): Invalid ISP SCSI completion handle\n",
699                     ha->host_no));
700                 qla_printk(KERN_WARNING, ha,
701                     "Invalid ISP SCSI completion handle\n");
702
703                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
704         }
705 }
706
707 /**
708  * qla2x00_process_response_queue() - Process response queue entries.
709  * @ha: SCSI driver HA context
710  */
711 void
712 qla2x00_process_response_queue(struct scsi_qla_host *ha)
713 {
714         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
715         sts_entry_t     *pkt;
716         uint16_t        handle_cnt;
717         uint16_t        cnt;
718
719         if (!ha->flags.online)
720                 return;
721
722         while (ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
723                 pkt = (sts_entry_t *)ha->response_ring_ptr;
724
725                 ha->rsp_ring_index++;
726                 if (ha->rsp_ring_index == ha->response_q_length) {
727                         ha->rsp_ring_index = 0;
728                         ha->response_ring_ptr = ha->response_ring;
729                 } else {
730                         ha->response_ring_ptr++;
731                 }
732
733                 if (pkt->entry_status != 0) {
734                         DEBUG3(printk(KERN_INFO
735                             "scsi(%ld): Process error entry.\n", ha->host_no));
736
737                         qla2x00_error_entry(ha, pkt);
738                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
739                         wmb();
740                         continue;
741                 }
742
743                 switch (pkt->entry_type) {
744                 case STATUS_TYPE:
745                         qla2x00_status_entry(ha, pkt);
746                         break;
747                 case STATUS_TYPE_21:
748                         handle_cnt = ((sts21_entry_t *)pkt)->handle_count;
749                         for (cnt = 0; cnt < handle_cnt; cnt++) {
750                                 qla2x00_process_completed_request(ha,
751                                     ((sts21_entry_t *)pkt)->handle[cnt]);
752                         }
753                         break;
754                 case STATUS_TYPE_22:
755                         handle_cnt = ((sts22_entry_t *)pkt)->handle_count;
756                         for (cnt = 0; cnt < handle_cnt; cnt++) {
757                                 qla2x00_process_completed_request(ha,
758                                     ((sts22_entry_t *)pkt)->handle[cnt]);
759                         }
760                         break;
761                 case STATUS_CONT_TYPE:
762                         qla2x00_status_cont_entry(ha, (sts_cont_entry_t *)pkt);
763                         break;
764                 case MS_IOCB_TYPE:
765                         qla2x00_ms_entry(ha, (ms_iocb_entry_t *)pkt);
766                         break;
767                 default:
768                         /* Type Not Supported. */
769                         DEBUG4(printk(KERN_WARNING
770                             "scsi(%ld): Received unknown response pkt type %x "
771                             "entry status=%x.\n",
772                             ha->host_no, pkt->entry_type, pkt->entry_status));
773                         break;
774                 }
775                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
776                 wmb();
777         }
778
779         /* Adjust ring index */
780         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), ha->rsp_ring_index);
781 }
782
783 /**
784  * qla2x00_status_entry() - Process a Status IOCB entry.
785  * @ha: SCSI driver HA context
786  * @pkt: Entry pointer
787  */
788 static void
789 qla2x00_status_entry(scsi_qla_host_t *ha, void *pkt)
790 {
791         srb_t           *sp;
792         fc_port_t       *fcport;
793         struct scsi_cmnd *cp;
794         sts_entry_t *sts;
795         struct sts_entry_24xx *sts24;
796         uint16_t        comp_status;
797         uint16_t        scsi_status;
798         uint8_t         lscsi_status;
799         int32_t         resid;
800         uint32_t        sense_len, rsp_info_len, resid_len, fw_resid_len;
801         uint8_t         *rsp_info, *sense_data;
802
803         sts = (sts_entry_t *) pkt;
804         sts24 = (struct sts_entry_24xx *) pkt;
805         if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
806                 comp_status = le16_to_cpu(sts24->comp_status);
807                 scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
808         } else {
809                 comp_status = le16_to_cpu(sts->comp_status);
810                 scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
811         }
812
813         /* Fast path completion. */
814         if (comp_status == CS_COMPLETE && scsi_status == 0) {
815                 qla2x00_process_completed_request(ha, sts->handle);
816
817                 return;
818         }
819
820         /* Validate handle. */
821         if (sts->handle < MAX_OUTSTANDING_COMMANDS) {
822                 sp = ha->outstanding_cmds[sts->handle];
823                 ha->outstanding_cmds[sts->handle] = NULL;
824         } else
825                 sp = NULL;
826
827         if (sp == NULL) {
828                 DEBUG2(printk("scsi(%ld): Status Entry invalid handle.\n",
829                     ha->host_no));
830                 qla_printk(KERN_WARNING, ha, "Status Entry invalid handle.\n");
831
832                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
833                 qla2xxx_wake_dpc(ha);
834                 return;
835         }
836         cp = sp->cmd;
837         if (cp == NULL) {
838                 DEBUG2(printk("scsi(%ld): Command already returned back to OS "
839                     "pkt->handle=%d sp=%p.\n", ha->host_no, sts->handle, sp));
840                 qla_printk(KERN_WARNING, ha,
841                     "Command is NULL: already returned to OS (sp=%p)\n", sp);
842
843                 return;
844         }
845
846         lscsi_status = scsi_status & STATUS_MASK;
847         CMD_ENTRY_STATUS(cp) = sts->entry_status;
848         CMD_COMPL_STATUS(cp) = comp_status;
849         CMD_SCSI_STATUS(cp) = scsi_status;
850
851         fcport = sp->fcport;
852
853         sense_len = rsp_info_len = resid_len = fw_resid_len = 0;
854         if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
855                 sense_len = le32_to_cpu(sts24->sense_len);
856                 rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
857                 resid_len = le32_to_cpu(sts24->rsp_residual_count);
858                 fw_resid_len = le32_to_cpu(sts24->residual_len);
859                 rsp_info = sts24->data;
860                 sense_data = sts24->data;
861                 host_to_fcp_swap(sts24->data, sizeof(sts24->data));
862         } else {
863                 sense_len = le16_to_cpu(sts->req_sense_length);
864                 rsp_info_len = le16_to_cpu(sts->rsp_info_len);
865                 resid_len = le32_to_cpu(sts->residual_length);
866                 rsp_info = sts->rsp_info;
867                 sense_data = sts->req_sense_data;
868         }
869
870         /* Check for any FCP transport errors. */
871         if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
872                 /* Sense data lies beyond any FCP RESPONSE data. */
873                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
874                         sense_data += rsp_info_len;
875                 if (rsp_info_len > 3 && rsp_info[3]) {
876                         DEBUG2(printk("scsi(%ld:%d:%d:%d) FCP I/O protocol "
877                             "failure (%x/%02x%02x%02x%02x%02x%02x%02x%02x)..."
878                             "retrying command\n", ha->host_no,
879                             cp->device->channel, cp->device->id,
880                             cp->device->lun, rsp_info_len, rsp_info[0],
881                             rsp_info[1], rsp_info[2], rsp_info[3], rsp_info[4],
882                             rsp_info[5], rsp_info[6], rsp_info[7]));
883
884                         cp->result = DID_BUS_BUSY << 16;
885                         qla2x00_sp_compl(ha, sp);
886                         return;
887                 }
888         }
889
890         /*
891          * Based on Host and scsi status generate status code for Linux
892          */
893         switch (comp_status) {
894         case CS_COMPLETE:
895         case CS_QUEUE_FULL:
896                 if (scsi_status == 0) {
897                         cp->result = DID_OK << 16;
898                         break;
899                 }
900                 if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
901                         resid = resid_len;
902                         cp->resid = resid;
903                         CMD_RESID_LEN(cp) = resid;
904
905                         if (!lscsi_status &&
906                             ((unsigned)(cp->request_bufflen - resid) <
907                              cp->underflow)) {
908                                 qla_printk(KERN_INFO, ha,
909                                     "scsi(%ld:%d:%d:%d): Mid-layer underflow "
910                                     "detected (%x of %x bytes)...returning "
911                                     "error status.\n", ha->host_no,
912                                     cp->device->channel, cp->device->id,
913                                     cp->device->lun, resid,
914                                     cp->request_bufflen);
915
916                                 cp->result = DID_ERROR << 16;
917                                 break;
918                         }
919                 }
920                 cp->result = DID_OK << 16 | lscsi_status;
921
922                 if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
923                         DEBUG2(printk(KERN_INFO
924                             "scsi(%ld): QUEUE FULL status detected "
925                             "0x%x-0x%x.\n", ha->host_no, comp_status,
926                             scsi_status));
927
928                         /* Adjust queue depth for all luns on the port. */
929                         fcport->last_queue_full = jiffies;
930                         spin_unlock_irq(&ha->hardware_lock);
931                         starget_for_each_device(cp->device->sdev_target,
932                             fcport, qla2x00_adjust_sdev_qdepth_down);
933                         spin_lock_irq(&ha->hardware_lock);
934                         break;
935                 }
936                 if (lscsi_status != SS_CHECK_CONDITION)
937                         break;
938
939                 /* Copy Sense Data into sense buffer. */
940                 memset(cp->sense_buffer, 0, sizeof(cp->sense_buffer));
941
942                 if (!(scsi_status & SS_SENSE_LEN_VALID))
943                         break;
944
945                 if (sense_len >= sizeof(cp->sense_buffer))
946                         sense_len = sizeof(cp->sense_buffer);
947
948                 CMD_ACTUAL_SNSLEN(cp) = sense_len;
949                 sp->request_sense_length = sense_len;
950                 sp->request_sense_ptr = cp->sense_buffer;
951
952                 if (sp->request_sense_length > 32)
953                         sense_len = 32;
954
955                 memcpy(cp->sense_buffer, sense_data, sense_len);
956
957                 sp->request_sense_ptr += sense_len;
958                 sp->request_sense_length -= sense_len;
959                 if (sp->request_sense_length != 0)
960                         ha->status_srb = sp;
961
962                 DEBUG5(printk("%s(): Check condition Sense data, "
963                     "scsi(%ld:%d:%d:%d) cmd=%p pid=%ld\n", __func__,
964                     ha->host_no, cp->device->channel, cp->device->id,
965                     cp->device->lun, cp, cp->serial_number));
966                 if (sense_len)
967                         DEBUG5(qla2x00_dump_buffer(cp->sense_buffer,
968                             CMD_ACTUAL_SNSLEN(cp)));
969                 break;
970
971         case CS_DATA_UNDERRUN:
972                 resid = resid_len;
973                 /* Use F/W calculated residual length. */
974                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
975                         resid = fw_resid_len;
976
977                 if (scsi_status & SS_RESIDUAL_UNDER) {
978                         cp->resid = resid;
979                         CMD_RESID_LEN(cp) = resid;
980                 } else {
981                         DEBUG2(printk(KERN_INFO
982                             "scsi(%ld:%d:%d) UNDERRUN status detected "
983                             "0x%x-0x%x. resid=0x%x fw_resid=0x%x cdb=0x%x "
984                             "os_underflow=0x%x\n", ha->host_no,
985                             cp->device->id, cp->device->lun, comp_status,
986                             scsi_status, resid_len, resid, cp->cmnd[0],
987                             cp->underflow));
988
989                 }
990
991                 /*
992                  * Check to see if SCSI Status is non zero. If so report SCSI
993                  * Status.
994                  */
995                 if (lscsi_status != 0) {
996                         cp->result = DID_OK << 16 | lscsi_status;
997
998                         if (lscsi_status != SS_CHECK_CONDITION)
999                                 break;
1000
1001                         /* Copy Sense Data into sense buffer */
1002                         memset(cp->sense_buffer, 0, sizeof(cp->sense_buffer));
1003
1004                         if (!(scsi_status & SS_SENSE_LEN_VALID))
1005                                 break;
1006
1007                         if (sense_len >= sizeof(cp->sense_buffer))
1008                                 sense_len = sizeof(cp->sense_buffer);
1009
1010                         CMD_ACTUAL_SNSLEN(cp) = sense_len;
1011                         sp->request_sense_length = sense_len;
1012                         sp->request_sense_ptr = cp->sense_buffer;
1013
1014                         if (sp->request_sense_length > 32)
1015                                 sense_len = 32;
1016
1017                         memcpy(cp->sense_buffer, sense_data, sense_len);
1018
1019                         sp->request_sense_ptr += sense_len;
1020                         sp->request_sense_length -= sense_len;
1021                         if (sp->request_sense_length != 0)
1022                                 ha->status_srb = sp;
1023
1024                         DEBUG5(printk("%s(): Check condition Sense data, "
1025                             "scsi(%ld:%d:%d:%d) cmd=%p pid=%ld\n",
1026                             __func__, ha->host_no, cp->device->channel,
1027                             cp->device->id, cp->device->lun, cp,
1028                             cp->serial_number));
1029
1030                         if (sense_len)
1031                                 DEBUG5(qla2x00_dump_buffer(cp->sense_buffer,
1032                                     CMD_ACTUAL_SNSLEN(cp)));
1033                 } else {
1034                         /*
1035                          * If RISC reports underrun and target does not report
1036                          * it then we must have a lost frame, so tell upper
1037                          * layer to retry it by reporting a bus busy.
1038                          */
1039                         if (!(scsi_status & SS_RESIDUAL_UNDER)) {
1040                                 DEBUG2(printk("scsi(%ld:%d:%d:%d) Dropped "
1041                                     "frame(s) detected (%x of %x bytes)..."
1042                                     "retrying command.\n", ha->host_no,
1043                                     cp->device->channel, cp->device->id,
1044                                     cp->device->lun, resid,
1045                                     cp->request_bufflen));
1046
1047                                 cp->result = DID_BUS_BUSY << 16;
1048                                 break;
1049                         }
1050
1051                         /* Handle mid-layer underflow */
1052                         if ((unsigned)(cp->request_bufflen - resid) <
1053                             cp->underflow) {
1054                                 qla_printk(KERN_INFO, ha,
1055                                     "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1056                                     "detected (%x of %x bytes)...returning "
1057                                     "error status.\n", ha->host_no,
1058                                     cp->device->channel, cp->device->id,
1059                                     cp->device->lun, resid,
1060                                     cp->request_bufflen);
1061
1062                                 cp->result = DID_ERROR << 16;
1063                                 break;
1064                         }
1065
1066                         /* Everybody online, looking good... */
1067                         cp->result = DID_OK << 16;
1068                 }
1069                 break;
1070
1071         case CS_DATA_OVERRUN:
1072                 DEBUG2(printk(KERN_INFO
1073                     "scsi(%ld:%d:%d): OVERRUN status detected 0x%x-0x%x\n",
1074                     ha->host_no, cp->device->id, cp->device->lun, comp_status,
1075                     scsi_status));
1076                 DEBUG2(printk(KERN_INFO
1077                     "CDB: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1078                     cp->cmnd[0], cp->cmnd[1], cp->cmnd[2], cp->cmnd[3],
1079                     cp->cmnd[4], cp->cmnd[5]));
1080                 DEBUG2(printk(KERN_INFO
1081                     "PID=0x%lx req=0x%x xtra=0x%x -- returning DID_ERROR "
1082                     "status!\n",
1083                     cp->serial_number, cp->request_bufflen, resid_len));
1084
1085                 cp->result = DID_ERROR << 16;
1086                 break;
1087
1088         case CS_PORT_LOGGED_OUT:
1089         case CS_PORT_CONFIG_CHG:
1090         case CS_PORT_BUSY:
1091         case CS_INCOMPLETE:
1092         case CS_PORT_UNAVAILABLE:
1093                 /*
1094                  * If the port is in Target Down state, return all IOs for this
1095                  * Target with DID_NO_CONNECT ELSE Queue the IOs in the
1096                  * retry_queue.
1097                  */
1098                 DEBUG2(printk("scsi(%ld:%d:%d): status_entry: Port Down "
1099                     "pid=%ld, compl status=0x%x, port state=0x%x\n",
1100                     ha->host_no, cp->device->id, cp->device->lun,
1101                     cp->serial_number, comp_status,
1102                     atomic_read(&fcport->state)));
1103
1104                 cp->result = DID_BUS_BUSY << 16;
1105                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
1106                         qla2x00_mark_device_lost(ha, fcport, 1, 1);
1107                 }
1108                 break;
1109
1110         case CS_RESET:
1111                 DEBUG2(printk(KERN_INFO
1112                     "scsi(%ld): RESET status detected 0x%x-0x%x.\n",
1113                     ha->host_no, comp_status, scsi_status));
1114
1115                 cp->result = DID_RESET << 16;
1116                 break;
1117
1118         case CS_ABORTED:
1119                 /*
1120                  * hv2.19.12 - DID_ABORT does not retry the request if we
1121                  * aborted this request then abort otherwise it must be a
1122                  * reset.
1123                  */
1124                 DEBUG2(printk(KERN_INFO
1125                     "scsi(%ld): ABORT status detected 0x%x-0x%x.\n",
1126                     ha->host_no, comp_status, scsi_status));
1127
1128                 cp->result = DID_RESET << 16;
1129                 break;
1130
1131         case CS_TIMEOUT:
1132                 cp->result = DID_BUS_BUSY << 16;
1133
1134                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
1135                         DEBUG2(printk(KERN_INFO
1136                             "scsi(%ld:%d:%d:%d): TIMEOUT status detected "
1137                             "0x%x-0x%x\n", ha->host_no, cp->device->channel,
1138                             cp->device->id, cp->device->lun, comp_status,
1139                             scsi_status));
1140                         break;
1141                 }
1142                 DEBUG2(printk(KERN_INFO
1143                     "scsi(%ld:%d:%d:%d): TIMEOUT status detected 0x%x-0x%x "
1144                     "sflags=%x.\n", ha->host_no, cp->device->channel,
1145                     cp->device->id, cp->device->lun, comp_status, scsi_status,
1146                     le16_to_cpu(sts->status_flags)));
1147
1148                 /* Check to see if logout occurred. */
1149                 if ((le16_to_cpu(sts->status_flags) & SF_LOGOUT_SENT))
1150                         qla2x00_mark_device_lost(ha, fcport, 1, 1);
1151                 break;
1152
1153         default:
1154                 DEBUG3(printk("scsi(%ld): Error detected (unknown status) "
1155                     "0x%x-0x%x.\n", ha->host_no, comp_status, scsi_status));
1156                 qla_printk(KERN_INFO, ha,
1157                     "Unknown status detected 0x%x-0x%x.\n",
1158                     comp_status, scsi_status);
1159
1160                 cp->result = DID_ERROR << 16;
1161                 break;
1162         }
1163
1164         /* Place command on done queue. */
1165         if (ha->status_srb == NULL)
1166                 qla2x00_sp_compl(ha, sp);
1167 }
1168
1169 /**
1170  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
1171  * @ha: SCSI driver HA context
1172  * @pkt: Entry pointer
1173  *
1174  * Extended sense data.
1175  */
1176 static void
1177 qla2x00_status_cont_entry(scsi_qla_host_t *ha, sts_cont_entry_t *pkt)
1178 {
1179         uint8_t         sense_sz = 0;
1180         srb_t           *sp = ha->status_srb;
1181         struct scsi_cmnd *cp;
1182
1183         if (sp != NULL && sp->request_sense_length != 0) {
1184                 cp = sp->cmd;
1185                 if (cp == NULL) {
1186                         DEBUG2(printk("%s(): Cmd already returned back to OS "
1187                             "sp=%p.\n", __func__, sp));
1188                         qla_printk(KERN_INFO, ha,
1189                             "cmd is NULL: already returned to OS (sp=%p)\n",
1190                             sp);
1191
1192                         ha->status_srb = NULL;
1193                         return;
1194                 }
1195
1196                 if (sp->request_sense_length > sizeof(pkt->data)) {
1197                         sense_sz = sizeof(pkt->data);
1198                 } else {
1199                         sense_sz = sp->request_sense_length;
1200                 }
1201
1202                 /* Move sense data. */
1203                 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
1204                         host_to_fcp_swap(pkt->data, sizeof(pkt->data));
1205                 memcpy(sp->request_sense_ptr, pkt->data, sense_sz);
1206                 DEBUG5(qla2x00_dump_buffer(sp->request_sense_ptr, sense_sz));
1207
1208                 sp->request_sense_ptr += sense_sz;
1209                 sp->request_sense_length -= sense_sz;
1210
1211                 /* Place command on done queue. */
1212                 if (sp->request_sense_length == 0) {
1213                         ha->status_srb = NULL;
1214                         qla2x00_sp_compl(ha, sp);
1215                 }
1216         }
1217 }
1218
1219 /**
1220  * qla2x00_error_entry() - Process an error entry.
1221  * @ha: SCSI driver HA context
1222  * @pkt: Entry pointer
1223  */
1224 static void
1225 qla2x00_error_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
1226 {
1227         srb_t *sp;
1228
1229 #if defined(QL_DEBUG_LEVEL_2)
1230         if (pkt->entry_status & RF_INV_E_ORDER)
1231                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Order\n", __func__);
1232         else if (pkt->entry_status & RF_INV_E_COUNT)
1233                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Count\n", __func__);
1234         else if (pkt->entry_status & RF_INV_E_PARAM)
1235                 qla_printk(KERN_ERR, ha,
1236                     "%s: Invalid Entry Parameter\n", __func__);
1237         else if (pkt->entry_status & RF_INV_E_TYPE)
1238                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Type\n", __func__);
1239         else if (pkt->entry_status & RF_BUSY)
1240                 qla_printk(KERN_ERR, ha, "%s: Busy\n", __func__);
1241         else
1242                 qla_printk(KERN_ERR, ha, "%s: UNKNOWN flag error\n", __func__);
1243 #endif
1244
1245         /* Validate handle. */
1246         if (pkt->handle < MAX_OUTSTANDING_COMMANDS)
1247                 sp = ha->outstanding_cmds[pkt->handle];
1248         else
1249                 sp = NULL;
1250
1251         if (sp) {
1252                 /* Free outstanding command slot. */
1253                 ha->outstanding_cmds[pkt->handle] = NULL;
1254
1255                 /* Bad payload or header */
1256                 if (pkt->entry_status &
1257                     (RF_INV_E_ORDER | RF_INV_E_COUNT |
1258                      RF_INV_E_PARAM | RF_INV_E_TYPE)) {
1259                         sp->cmd->result = DID_ERROR << 16;
1260                 } else if (pkt->entry_status & RF_BUSY) {
1261                         sp->cmd->result = DID_BUS_BUSY << 16;
1262                 } else {
1263                         sp->cmd->result = DID_ERROR << 16;
1264                 }
1265                 qla2x00_sp_compl(ha, sp);
1266
1267         } else if (pkt->entry_type == COMMAND_A64_TYPE || pkt->entry_type ==
1268             COMMAND_TYPE || pkt->entry_type == COMMAND_TYPE_7) {
1269                 DEBUG2(printk("scsi(%ld): Error entry - invalid handle\n",
1270                     ha->host_no));
1271                 qla_printk(KERN_WARNING, ha,
1272                     "Error entry - invalid handle\n");
1273
1274                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1275                 qla2xxx_wake_dpc(ha);
1276         }
1277 }
1278
1279 /**
1280  * qla2x00_ms_entry() - Process a Management Server entry.
1281  * @ha: SCSI driver HA context
1282  * @index: Response queue out pointer
1283  */
1284 static void
1285 qla2x00_ms_entry(scsi_qla_host_t *ha, ms_iocb_entry_t *pkt)
1286 {
1287         srb_t          *sp;
1288
1289         DEBUG3(printk("%s(%ld): pkt=%p pkthandle=%d.\n",
1290             __func__, ha->host_no, pkt, pkt->handle1));
1291
1292         /* Validate handle. */
1293         if (pkt->handle1 < MAX_OUTSTANDING_COMMANDS)
1294                 sp = ha->outstanding_cmds[pkt->handle1];
1295         else
1296                 sp = NULL;
1297
1298         if (sp == NULL) {
1299                 DEBUG2(printk("scsi(%ld): MS entry - invalid handle\n",
1300                     ha->host_no));
1301                 qla_printk(KERN_WARNING, ha, "MS entry - invalid handle\n");
1302
1303                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1304                 return;
1305         }
1306
1307         CMD_COMPL_STATUS(sp->cmd) = le16_to_cpu(pkt->status);
1308         CMD_ENTRY_STATUS(sp->cmd) = pkt->entry_status;
1309
1310         /* Free outstanding command slot. */
1311         ha->outstanding_cmds[pkt->handle1] = NULL;
1312
1313         qla2x00_sp_compl(ha, sp);
1314 }
1315
1316
1317 /**
1318  * qla24xx_mbx_completion() - Process mailbox command completions.
1319  * @ha: SCSI driver HA context
1320  * @mb0: Mailbox0 register
1321  */
1322 static void
1323 qla24xx_mbx_completion(scsi_qla_host_t *ha, uint16_t mb0)
1324 {
1325         uint16_t        cnt;
1326         uint16_t __iomem *wptr;
1327         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1328
1329         /* Load return mailbox registers. */
1330         ha->flags.mbox_int = 1;
1331         ha->mailbox_out[0] = mb0;
1332         wptr = (uint16_t __iomem *)&reg->mailbox1;
1333
1334         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1335                 ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1336                 wptr++;
1337         }
1338
1339         if (ha->mcp) {
1340                 DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
1341                     __func__, ha->host_no, ha->mcp->mb[0]));
1342         } else {
1343                 DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
1344                     __func__, ha->host_no));
1345         }
1346 }
1347
1348 /**
1349  * qla24xx_process_response_queue() - Process response queue entries.
1350  * @ha: SCSI driver HA context
1351  */
1352 void
1353 qla24xx_process_response_queue(struct scsi_qla_host *ha)
1354 {
1355         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1356         struct sts_entry_24xx *pkt;
1357
1358         if (!ha->flags.online)
1359                 return;
1360
1361         while (ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
1362                 pkt = (struct sts_entry_24xx *)ha->response_ring_ptr;
1363
1364                 ha->rsp_ring_index++;
1365                 if (ha->rsp_ring_index == ha->response_q_length) {
1366                         ha->rsp_ring_index = 0;
1367                         ha->response_ring_ptr = ha->response_ring;
1368                 } else {
1369                         ha->response_ring_ptr++;
1370                 }
1371
1372                 if (pkt->entry_status != 0) {
1373                         DEBUG3(printk(KERN_INFO
1374                             "scsi(%ld): Process error entry.\n", ha->host_no));
1375
1376                         qla2x00_error_entry(ha, (sts_entry_t *) pkt);
1377                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1378                         wmb();
1379                         continue;
1380                 }
1381
1382                 switch (pkt->entry_type) {
1383                 case STATUS_TYPE:
1384                         qla2x00_status_entry(ha, pkt);
1385                         break;
1386                 case STATUS_CONT_TYPE:
1387                         qla2x00_status_cont_entry(ha, (sts_cont_entry_t *)pkt);
1388                         break;
1389                 case MS_IOCB_TYPE:
1390                         qla24xx_ms_entry(ha, (struct ct_entry_24xx *)pkt);
1391                         break;
1392                 default:
1393                         /* Type Not Supported. */
1394                         DEBUG4(printk(KERN_WARNING
1395                             "scsi(%ld): Received unknown response pkt type %x "
1396                             "entry status=%x.\n",
1397                             ha->host_no, pkt->entry_type, pkt->entry_status));
1398                         break;
1399                 }
1400                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1401                 wmb();
1402         }
1403
1404         /* Adjust ring index */
1405         WRT_REG_DWORD(&reg->rsp_q_out, ha->rsp_ring_index);
1406 }
1407
1408 /**
1409  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
1410  * @irq:
1411  * @dev_id: SCSI driver HA context
1412  *
1413  * Called by system whenever the host adapter generates an interrupt.
1414  *
1415  * Returns handled flag.
1416  */
1417 irqreturn_t
1418 qla24xx_intr_handler(int irq, void *dev_id)
1419 {
1420         scsi_qla_host_t *ha;
1421         struct device_reg_24xx __iomem *reg;
1422         int             status;
1423         unsigned long   flags;
1424         unsigned long   iter;
1425         uint32_t        stat;
1426         uint32_t        hccr;
1427         uint16_t        mb[4];
1428
1429         ha = (scsi_qla_host_t *) dev_id;
1430         if (!ha) {
1431                 printk(KERN_INFO
1432                     "%s(): NULL host pointer\n", __func__);
1433                 return IRQ_NONE;
1434         }
1435
1436         reg = &ha->iobase->isp24;
1437         status = 0;
1438
1439         spin_lock_irqsave(&ha->hardware_lock, flags);
1440         for (iter = 50; iter--; ) {
1441                 stat = RD_REG_DWORD(&reg->host_status);
1442                 if (stat & HSRX_RISC_PAUSED) {
1443                         hccr = RD_REG_DWORD(&reg->hccr);
1444
1445                         qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1446                             "Dumping firmware!\n", hccr);
1447                         ha->isp_ops.fw_dump(ha, 1);
1448                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1449                         break;
1450                 } else if ((stat & HSRX_RISC_INT) == 0)
1451                         break;
1452
1453                 switch (stat & 0xff) {
1454                 case 0x1:
1455                 case 0x2:
1456                 case 0x10:
1457                 case 0x11:
1458                         qla24xx_mbx_completion(ha, MSW(stat));
1459                         status |= MBX_INTERRUPT;
1460
1461                         break;
1462                 case 0x12:
1463                         mb[0] = MSW(stat);
1464                         mb[1] = RD_REG_WORD(&reg->mailbox1);
1465                         mb[2] = RD_REG_WORD(&reg->mailbox2);
1466                         mb[3] = RD_REG_WORD(&reg->mailbox3);
1467                         qla2x00_async_event(ha, mb);
1468                         break;
1469                 case 0x13:
1470                         qla24xx_process_response_queue(ha);
1471                         break;
1472                 default:
1473                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1474                             "(%d).\n",
1475                             ha->host_no, stat & 0xff));
1476                         break;
1477                 }
1478                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1479                 RD_REG_DWORD_RELAXED(&reg->hccr);
1480         }
1481         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1482
1483         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1484             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1485                 spin_lock_irqsave(&ha->mbx_reg_lock, flags);
1486
1487                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1488                 up(&ha->mbx_intr_sem);
1489
1490                 spin_unlock_irqrestore(&ha->mbx_reg_lock, flags);
1491         }
1492
1493         return IRQ_HANDLED;
1494 }
1495
1496 /**
1497  * qla24xx_ms_entry() - Process a Management Server entry.
1498  * @ha: SCSI driver HA context
1499  * @index: Response queue out pointer
1500  */
1501 static void
1502 qla24xx_ms_entry(scsi_qla_host_t *ha, struct ct_entry_24xx *pkt)
1503 {
1504         srb_t          *sp;
1505
1506         DEBUG3(printk("%s(%ld): pkt=%p pkthandle=%d.\n",
1507             __func__, ha->host_no, pkt, pkt->handle));
1508
1509         DEBUG9(printk("%s: ct pkt dump:\n", __func__));
1510         DEBUG9(qla2x00_dump_buffer((void *)pkt, sizeof(struct ct_entry_24xx)));
1511
1512         /* Validate handle. */
1513         if (pkt->handle < MAX_OUTSTANDING_COMMANDS)
1514                 sp = ha->outstanding_cmds[pkt->handle];
1515         else
1516                 sp = NULL;
1517
1518         if (sp == NULL) {
1519                 DEBUG2(printk("scsi(%ld): MS entry - invalid handle\n",
1520                     ha->host_no));
1521                 DEBUG10(printk("scsi(%ld): MS entry - invalid handle\n",
1522                     ha->host_no));
1523                 qla_printk(KERN_WARNING, ha, "MS entry - invalid handle %d\n",
1524                     pkt->handle);
1525
1526                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1527                 return;
1528         }
1529
1530         CMD_COMPL_STATUS(sp->cmd) = le16_to_cpu(pkt->comp_status);
1531         CMD_ENTRY_STATUS(sp->cmd) = pkt->entry_status;
1532
1533         /* Free outstanding command slot. */
1534         ha->outstanding_cmds[pkt->handle] = NULL;
1535
1536         qla2x00_sp_compl(ha, sp);
1537 }
1538