Checkpath and compiler fixes
[mirror/scst/.git] / qla2x00t / qla_isr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/delay.h>
10 #include <scsi/scsi_tcq.h>
11
12 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
13 #include "qla2x_tgt.h"
14 struct qla2x_tgt_initiator qla_target;
15 #endif /* CONFIG_SCSI_QLA2XXX_TARGET */
16
17 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
18 static void qla2x00_process_completed_request(struct scsi_qla_host *, uint32_t);
19 static void qla2x00_status_entry(scsi_qla_host_t *, void *);
20 static void qla2x00_status_cont_entry(scsi_qla_host_t *, sts_cont_entry_t *);
21 static void qla2x00_error_entry(scsi_qla_host_t *, sts_entry_t *);
22
23 /**
24  * qla2100_intr_handler() - Process interrupts for the ISP2100 and ISP2200.
25  * @irq:
26  * @dev_id: SCSI driver HA context
27  *
28  * Called by system whenever the host adapter generates an interrupt.
29  *
30  * Returns handled flag.
31  */
32 irqreturn_t
33 qla2100_intr_handler(int irq, void *dev_id)
34 {
35         scsi_qla_host_t *ha;
36         struct device_reg_2xxx __iomem *reg;
37         int             status;
38         unsigned long   iter;
39         uint16_t        hccr;
40         uint16_t        mb[4];
41
42         ha = (scsi_qla_host_t *) dev_id;
43         if (!ha) {
44                 printk(KERN_INFO
45                     "%s(): NULL host pointer\n", __func__);
46                 return (IRQ_NONE);
47         }
48
49         reg = &ha->iobase->isp;
50         status = 0;
51
52         spin_lock(&ha->hardware_lock);
53         for (iter = 50; iter--; ) {
54                 hccr = RD_REG_WORD(&reg->hccr);
55                 if (hccr & HCCR_RISC_PAUSE) {
56                         if (pci_channel_offline(ha->pdev))
57                                 break;
58
59                         /*
60                          * Issue a "HARD" reset in order for the RISC interrupt
61                          * bit to be cleared.  Schedule a big hammmer to get
62                          * out of the RISC PAUSED state.
63                          */
64                         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
65                         RD_REG_WORD(&reg->hccr);
66
67                         ha->isp_ops->fw_dump(ha, 1);
68                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
69                         break;
70                 } else if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
71                         break;
72
73                 if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
74                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
75                         RD_REG_WORD(&reg->hccr);
76
77                         /* Get mailbox data. */
78                         mb[0] = RD_MAILBOX_REG(ha, reg, 0);
79                         if (mb[0] > 0x3fff && mb[0] < 0x8000) {
80                                 qla2x00_mbx_completion(ha, mb[0]);
81                                 status |= MBX_INTERRUPT;
82                         } else if (mb[0] > 0x7fff && mb[0] < 0xc000) {
83                                 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
84                                 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
85                                 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
86                                 qla2x00_async_event(ha, mb);
87                         } else {
88                                 /*EMPTY*/
89                                 DEBUG2(printk("scsi(%ld): Unrecognized "
90                                     "interrupt type (%d).\n",
91                                     ha->host_no, mb[0]));
92                         }
93                         /* Release mailbox registers. */
94                         WRT_REG_WORD(&reg->semaphore, 0);
95                         RD_REG_WORD(&reg->semaphore);
96                 } else {
97                         qla2x00_process_response_queue(ha);
98
99                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
100                         RD_REG_WORD(&reg->hccr);
101                 }
102         }
103         spin_unlock(&ha->hardware_lock);
104
105         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
106             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
107                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
108                 complete(&ha->mbx_intr_comp);
109         }
110
111         return (IRQ_HANDLED);
112 }
113
114 /**
115  * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
116  * @irq:
117  * @dev_id: SCSI driver HA context
118  *
119  * Called by system whenever the host adapter generates an interrupt.
120  *
121  * Returns handled flag.
122  */
123 irqreturn_t
124 qla2300_intr_handler(int irq, void *dev_id)
125 {
126         scsi_qla_host_t *ha;
127         struct device_reg_2xxx __iomem *reg;
128         int             status;
129         unsigned long   iter;
130         uint32_t        stat;
131         uint16_t        hccr;
132         uint16_t        mb[4];
133
134         ha = (scsi_qla_host_t *) dev_id;
135         if (!ha) {
136                 printk(KERN_INFO
137                     "%s(): NULL host pointer\n", __func__);
138                 return (IRQ_NONE);
139         }
140
141         reg = &ha->iobase->isp;
142         status = 0;
143
144         spin_lock(&ha->hardware_lock);
145         for (iter = 50; iter--; ) {
146                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
147                 if (stat & HSR_RISC_PAUSED) {
148                         if (pci_channel_offline(ha->pdev))
149                                 break;
150
151                         hccr = RD_REG_WORD(&reg->hccr);
152                         if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
153                                 qla_printk(KERN_INFO, ha, "Parity error -- "
154                                     "HCCR=%x, Dumping firmware!\n", hccr);
155                         else
156                                 qla_printk(KERN_INFO, ha, "RISC paused -- "
157                                     "HCCR=%x, Dumping firmware!\n", hccr);
158
159                         /*
160                          * Issue a "HARD" reset in order for the RISC
161                          * interrupt bit to be cleared.  Schedule a big
162                          * hammmer to get out of the RISC PAUSED state.
163                          */
164                         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
165                         RD_REG_WORD(&reg->hccr);
166
167                         ha->isp_ops->fw_dump(ha, 1);
168                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
169                         break;
170                 } else if ((stat & HSR_RISC_INT) == 0)
171                         break;
172
173                 switch (stat & 0xff) {
174                 case 0x1:
175                 case 0x2:
176                 case 0x10:
177                 case 0x11:
178                         qla2x00_mbx_completion(ha, MSW(stat));
179                         status |= MBX_INTERRUPT;
180
181                         /* Release mailbox registers. */
182                         WRT_REG_WORD(&reg->semaphore, 0);
183                         break;
184                 case 0x12:
185                         mb[0] = MSW(stat);
186                         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
187                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
188                         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
189                         qla2x00_async_event(ha, mb);
190                         break;
191                 case 0x13:
192                         qla2x00_process_response_queue(ha);
193                         break;
194                 case 0x15:
195                         mb[0] = MBA_CMPLT_1_16BIT;
196                         mb[1] = MSW(stat);
197                         qla2x00_async_event(ha, mb);
198                         break;
199                 case 0x16:
200                         mb[0] = MBA_SCSI_COMPLETION;
201                         mb[1] = MSW(stat);
202                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
203                         qla2x00_async_event(ha, mb);
204                         break;
205 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
206                 case 0x17: /* FAST_CTIO_COMP */
207                         mb[0] = MBA_CTIO_COMPLETION;
208                         mb[1] = MSW(stat);
209                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
210                         qla2x00_async_event(ha, mb);
211                         break;
212 #endif
213                 default:
214                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
215                             "(%d).\n",
216                             ha->host_no, stat & 0xff));
217                         break;
218                 }
219                 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
220                 RD_REG_WORD_RELAXED(&reg->hccr);
221         }
222         spin_unlock(&ha->hardware_lock);
223
224         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
225             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
226                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
227                 complete(&ha->mbx_intr_comp);
228         }
229
230         return (IRQ_HANDLED);
231 }
232
233 /**
234  * qla2x00_mbx_completion() - Process mailbox command completions.
235  * @ha: SCSI driver HA context
236  * @mb0: Mailbox0 register
237  */
238 static void
239 qla2x00_mbx_completion(scsi_qla_host_t *ha, uint16_t mb0)
240 {
241         uint16_t        cnt;
242         uint16_t __iomem *wptr;
243         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
244
245         /* Load return mailbox registers. */
246         ha->flags.mbox_int = 1;
247         ha->mailbox_out[0] = mb0;
248         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
249
250         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
251                 if (IS_QLA2200(ha) && cnt == 8)
252                         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
253                 if (cnt == 4 || cnt == 5)
254                         ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
255                 else
256                         ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
257
258                 wptr++;
259         }
260
261         if (ha->mcp) {
262                 DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
263                     __func__, ha->host_no, ha->mcp->mb[0]));
264         } else {
265                 DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
266                     __func__, ha->host_no));
267         }
268 }
269
270 /**
271  * qla2x00_async_event() - Process aynchronous events.
272  * @ha: SCSI driver HA context
273  * @mb: Mailbox registers (0 - 3)
274  */
275 void
276 qla2x00_async_event(scsi_qla_host_t *ha, uint16_t *mb)
277 {
278 #define LS_UNKNOWN      2
279         static char     *link_speeds[5] = { "1", "2", "?", "4", "8" };
280         char            *link_speed;
281         uint16_t        handle_cnt;
282         uint16_t        cnt;
283         uint32_t        handles[5];
284         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
285         uint32_t        rscn_entry, host_pid;
286         uint8_t         rscn_queue_index;
287         unsigned long   flags;
288
289         /* Setup to process RIO completion. */
290         handle_cnt = 0;
291         switch (mb[0]) {
292 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
293         case MBA_CTIO_COMPLETION:
294 #endif
295         case MBA_SCSI_COMPLETION:
296                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
297                 handle_cnt = 1;
298                 break;
299         case MBA_CMPLT_1_16BIT:
300                 handles[0] = mb[1];
301                 handle_cnt = 1;
302                 mb[0] = MBA_SCSI_COMPLETION;
303                 break;
304         case MBA_CMPLT_2_16BIT:
305                 handles[0] = mb[1];
306                 handles[1] = mb[2];
307                 handle_cnt = 2;
308                 mb[0] = MBA_SCSI_COMPLETION;
309                 break;
310         case MBA_CMPLT_3_16BIT:
311                 handles[0] = mb[1];
312                 handles[1] = mb[2];
313                 handles[2] = mb[3];
314                 handle_cnt = 3;
315                 mb[0] = MBA_SCSI_COMPLETION;
316                 break;
317         case MBA_CMPLT_4_16BIT:
318                 handles[0] = mb[1];
319                 handles[1] = mb[2];
320                 handles[2] = mb[3];
321                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
322                 handle_cnt = 4;
323                 mb[0] = MBA_SCSI_COMPLETION;
324                 break;
325         case MBA_CMPLT_5_16BIT:
326                 handles[0] = mb[1];
327                 handles[1] = mb[2];
328                 handles[2] = mb[3];
329                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
330                 handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
331                 handle_cnt = 5;
332                 mb[0] = MBA_SCSI_COMPLETION;
333                 break;
334         case MBA_CMPLT_2_32BIT:
335                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
336                 handles[1] = le32_to_cpu(
337                     ((uint32_t)(RD_MAILBOX_REG(ha, reg, 7) << 16)) |
338                     RD_MAILBOX_REG(ha, reg, 6));
339                 handle_cnt = 2;
340                 mb[0] = MBA_SCSI_COMPLETION;
341                 break;
342         default:
343                 break;
344         }
345
346         switch (mb[0]) {
347         case MBA_SCSI_COMPLETION:       /* Fast Post */
348                 if (!ha->flags.online)
349                         break;
350
351                 for (cnt = 0; cnt < handle_cnt; cnt++)
352                         qla2x00_process_completed_request(ha, handles[cnt]);
353                 break;
354
355 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
356         case MBA_CTIO_COMPLETION:
357                 if (qla_target.tgt_ctio_completion)
358                         qla_target.tgt_ctio_completion(ha, handles[0]);
359                 break;
360 #endif
361
362         case MBA_RESET:                 /* Reset */
363                 DEBUG2(printk("scsi(%ld): Asynchronous RESET.\n", ha->host_no));
364
365                 set_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
366 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
367                 if (qla_target.tgt_async_event)
368                         qla_target.tgt_async_event(mb[0], ha, mb);
369 #endif
370                 break;
371
372         case MBA_SYSTEM_ERR:            /* System Error */
373                 qla_printk(KERN_INFO, ha,
374                     "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh.\n",
375                     mb[1], mb[2], mb[3]);
376
377                 qla2x00_post_hwe_work(ha, mb[0], mb[1], mb[2], mb[3]);
378                 ha->isp_ops->fw_dump(ha, 1);
379
380                 if (IS_FWI2_CAPABLE(ha)) {
381                         if (mb[1] == 0 && mb[2] == 0) {
382                                 qla_printk(KERN_ERR, ha,
383                                     "Unrecoverable Hardware Error: adapter "
384                                     "marked OFFLINE!\n");
385                                 ha->flags.online = 0;
386                         } else
387                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
388                 } else if (mb[1] == 0) {
389                         qla_printk(KERN_INFO, ha,
390                             "Unrecoverable Hardware Error: adapter marked "
391                             "OFFLINE!\n");
392                         ha->flags.online = 0;
393                 } else
394                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
395 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
396                 if (qla_target.tgt_async_event)
397                         qla_target.tgt_async_event(mb[0], ha, mb);
398 #endif
399                 break;
400
401         case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
402                 DEBUG2(printk("scsi(%ld): ISP Request Transfer Error.\n",
403                     ha->host_no));
404                 qla_printk(KERN_WARNING, ha, "ISP Request Transfer Error.\n");
405
406                 qla2x00_post_hwe_work(ha, mb[0], mb[1], mb[2], mb[3]);
407                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
408 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
409                 if (qla_target.tgt_async_event)
410                         qla_target.tgt_async_event(mb[0], ha, mb);
411 #endif
412                 break;
413
414         case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
415                 DEBUG2(printk("scsi(%ld): ISP Response Transfer Error.\n",
416                     ha->host_no));
417                 qla_printk(KERN_WARNING, ha, "ISP Response Transfer Error.\n");
418
419                 qla2x00_post_hwe_work(ha, mb[0], mb[1], mb[2], mb[3]);
420                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
421 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
422                 if (qla_target.tgt_async_event)
423                         qla_target.tgt_async_event(mb[0], ha, mb);
424 #endif
425                 break;
426
427         case MBA_WAKEUP_THRES:          /* Request Queue Wake-up */
428                 DEBUG2(printk("scsi(%ld): Asynchronous WAKEUP_THRES.\n",
429                     ha->host_no));
430 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
431                 if (qla_target.tgt_async_event)
432                         qla_target.tgt_async_event(mb[0], ha, mb);
433 #endif
434                 break;
435
436         case MBA_LIP_OCCURRED:          /* Loop Initialization Procedure */
437                 DEBUG2(printk("scsi(%ld): LIP occured (%x).\n", ha->host_no,
438                     mb[1]));
439                 qla_printk(KERN_INFO, ha, "LIP occured (%x).\n", mb[1]);
440
441                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
442                         atomic_set(&ha->loop_state, LOOP_DOWN);
443                         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
444                         qla2x00_mark_all_devices_lost(ha, 1);
445                 }
446
447                 if (ha->parent) {
448                         atomic_set(&ha->vp_state, VP_FAILED);
449                         fc_vport_set_state(ha->fc_vport, FC_VPORT_FAILED);
450                 }
451
452                 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
453                 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
454
455                 ha->flags.management_server_logged_in = 0;
456                 qla2x00_post_aen_work(ha, FCH_EVT_LIP, mb[1]);
457 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
458                 if (qla_target.tgt_async_event)
459                         qla_target.tgt_async_event(mb[0], ha, mb);
460 #endif
461                 break;
462
463         case MBA_LOOP_UP:               /* Loop Up Event */
464                 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
465                         link_speed = link_speeds[0];
466                         ha->link_data_rate = PORT_SPEED_1GB;
467                 } else {
468                         link_speed = link_speeds[LS_UNKNOWN];
469                         if (mb[1] < 5)
470                                 link_speed = link_speeds[mb[1]];
471                         ha->link_data_rate = mb[1];
472                 }
473
474                 DEBUG2(printk("scsi(%ld): Asynchronous LOOP UP (%s Gbps).\n",
475                     ha->host_no, link_speed));
476                 qla_printk(KERN_INFO, ha, "LOOP UP detected (%s Gbps).\n",
477                     link_speed);
478
479                 ha->flags.management_server_logged_in = 0;
480                 qla2x00_post_aen_work(ha, FCH_EVT_LINKUP, ha->link_data_rate);
481 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
482                 if (qla_target.tgt_async_event)
483                         qla_target.tgt_async_event(mb[0], ha, mb);
484 #endif
485                 break;
486
487         case MBA_LOOP_DOWN:             /* Loop Down Event */
488                 DEBUG2(printk("scsi(%ld): Asynchronous LOOP DOWN "
489                     "(%x %x %x).\n", ha->host_no, mb[1], mb[2], mb[3]));
490                 qla_printk(KERN_INFO, ha, "LOOP DOWN detected (%x %x %x).\n",
491                     mb[1], mb[2], mb[3]);
492
493                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
494                         atomic_set(&ha->loop_state, LOOP_DOWN);
495                         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
496                         ha->device_flags |= DFLG_NO_CABLE;
497                         qla2x00_mark_all_devices_lost(ha, 1);
498                 }
499
500                 if (ha->parent) {
501                         atomic_set(&ha->vp_state, VP_FAILED);
502                         fc_vport_set_state(ha->fc_vport, FC_VPORT_FAILED);
503                 }
504
505                 ha->flags.management_server_logged_in = 0;
506                 ha->link_data_rate = PORT_SPEED_UNKNOWN;
507                 qla2x00_post_aen_work(ha, FCH_EVT_LINKDOWN, 0);
508 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
509                 if (qla_target.tgt_async_event)
510                         qla_target.tgt_async_event(mb[0], ha, mb);
511 #endif
512                 break;
513
514         case MBA_LIP_RESET:             /* LIP reset occurred */
515                 DEBUG2(printk("scsi(%ld): Asynchronous LIP RESET (%x).\n",
516                     ha->host_no, mb[1]));
517                 qla_printk(KERN_INFO, ha,
518                     "LIP reset occured (%x).\n", mb[1]);
519
520                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
521                         atomic_set(&ha->loop_state, LOOP_DOWN);
522                         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
523                         qla2x00_mark_all_devices_lost(ha, 1);
524                 }
525
526                 if (ha->parent) {
527                         atomic_set(&ha->vp_state, VP_FAILED);
528                         fc_vport_set_state(ha->fc_vport, FC_VPORT_FAILED);
529                 }
530
531                 set_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
532
533                 ha->operating_mode = LOOP;
534                 ha->flags.management_server_logged_in = 0;
535                 qla2x00_post_aen_work(ha, FCH_EVT_LIPRESET, mb[1]);
536 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
537                 if (qla_target.tgt_async_event)
538                         qla_target.tgt_async_event(mb[0], ha, mb);
539 #endif
540                 break;
541
542         case MBA_POINT_TO_POINT:        /* Point-to-Point */
543                 if (IS_QLA2100(ha))
544                         break;
545
546                 DEBUG2(printk("scsi(%ld): Asynchronous P2P MODE received.\n",
547                     ha->host_no));
548
549                 /*
550                  * Until there's a transition from loop down to loop up, treat
551                  * this as loop down only.
552                  */
553                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
554                         atomic_set(&ha->loop_state, LOOP_DOWN);
555                         if (!atomic_read(&ha->loop_down_timer))
556                                 atomic_set(&ha->loop_down_timer,
557                                     LOOP_DOWN_TIME);
558                         qla2x00_mark_all_devices_lost(ha, 1);
559                 }
560
561                 if (ha->parent) {
562                         atomic_set(&ha->vp_state, VP_FAILED);
563                         fc_vport_set_state(ha->fc_vport, FC_VPORT_FAILED);
564                 }
565
566                 if (!(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
567                         set_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
568                 }
569                 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
570                 set_bit(REGISTER_FDMI_NEEDED, &ha->dpc_flags);
571
572                 ha->flags.gpsc_supported = 1;
573                 ha->flags.management_server_logged_in = 0;
574 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
575                 if (qla_target.tgt_async_event)
576                         qla_target.tgt_async_event(mb[0], ha, mb);
577 #endif
578                 break;
579
580         case MBA_CHG_IN_CONNECTION:     /* Change in connection mode */
581                 if (IS_QLA2100(ha))
582                         break;
583
584                 DEBUG2(printk("scsi(%ld): Asynchronous Change In Connection "
585                     "received.\n",
586                     ha->host_no));
587                 qla_printk(KERN_INFO, ha,
588                     "Configuration change detected: value=%x.\n", mb[1]);
589
590                 if (atomic_read(&ha->loop_state) != LOOP_DOWN) {
591                         atomic_set(&ha->loop_state, LOOP_DOWN);
592                         if (!atomic_read(&ha->loop_down_timer))
593                                 atomic_set(&ha->loop_down_timer,
594                                     LOOP_DOWN_TIME);
595                         qla2x00_mark_all_devices_lost(ha, 1);
596                 }
597
598                 if (ha->parent) {
599                         atomic_set(&ha->vp_state, VP_FAILED);
600                         fc_vport_set_state(ha->fc_vport, FC_VPORT_FAILED);
601                 }
602
603                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
604                 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
605 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
606                 if (qla_target.tgt_async_event)
607                         qla_target.tgt_async_event(mb[0], ha, mb);
608 #endif
609                 break;
610
611         case MBA_PORT_UPDATE:           /* Port database update */
612                 /* Only handle SCNs for our Vport index. */
613                 if (ha->parent && ha->vp_idx != (mb[3] & 0xff))
614                         break;
615
616                 /*
617                  * If PORT UPDATE is global (recieved LIP_OCCURED/LIP_RESET
618                  * event etc. earlier indicating loop is down) then process
619                  * it.  Otherwise ignore it and Wait for RSCN to come in.
620                  */
621                 atomic_set(&ha->loop_down_timer, 0);
622                 if (atomic_read(&ha->loop_state) != LOOP_DOWN &&
623                     atomic_read(&ha->loop_state) != LOOP_DEAD) {
624                         DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE "
625                             "ignored %04x/%04x/%04x.\n", ha->host_no, mb[1],
626                             mb[2], mb[3]));
627 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
628                         if (qla_target.tgt_async_event)
629                                 qla_target.tgt_async_event(mb[0], ha, mb);
630 #endif
631                         break;
632                 }
633
634                 DEBUG2(printk("scsi(%ld): Asynchronous PORT UPDATE.\n",
635                     ha->host_no));
636                 DEBUG(printk(KERN_INFO
637                     "scsi(%ld): Port database changed %04x %04x %04x.\n",
638                     ha->host_no, mb[1], mb[2], mb[3]));
639
640                 /*
641                  * Mark all devices as missing so we will login again.
642                  */
643                 atomic_set(&ha->loop_state, LOOP_UP);
644
645                 qla2x00_mark_all_devices_lost(ha, 1);
646
647                 ha->flags.rscn_queue_overflow = 1;
648
649                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
650                 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
651 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
652                 if (qla_target.tgt_async_event)
653                         qla_target.tgt_async_event(mb[0], ha, mb);
654 #endif
655                 break;
656
657         case MBA_RSCN_UPDATE:           /* State Change Registration */
658                 /* Check if the Vport has issued a SCR */
659                 if (ha->parent && test_bit(VP_SCR_NEEDED, &ha->vp_flags))
660                         break;
661                 /* Only handle SCNs for our Vport index. */
662                 if (ha->parent && ha->vp_idx != (mb[3] & 0xff))
663                         break;
664
665                 DEBUG2(printk("scsi(%ld): Asynchronous RSCR UPDATE.\n",
666                     ha->host_no));
667                 DEBUG(printk(KERN_INFO
668                     "scsi(%ld): RSCN database changed -- %04x %04x %04x.\n",
669                     ha->host_no, mb[1], mb[2], mb[3]));
670
671                 rscn_entry = (mb[1] << 16) | mb[2];
672                 host_pid = (ha->d_id.b.domain << 16) | (ha->d_id.b.area << 8) |
673                     ha->d_id.b.al_pa;
674                 if (rscn_entry == host_pid) {
675                         DEBUG(printk(KERN_INFO
676                             "scsi(%ld): Ignoring RSCN update to local host "
677                             "port ID (%06x)\n",
678                             ha->host_no, host_pid));
679                         break;
680                 }
681
682                 rscn_queue_index = ha->rscn_in_ptr + 1;
683                 if (rscn_queue_index == MAX_RSCN_COUNT)
684                         rscn_queue_index = 0;
685                 if (rscn_queue_index != ha->rscn_out_ptr) {
686                         ha->rscn_queue[ha->rscn_in_ptr] = rscn_entry;
687                         ha->rscn_in_ptr = rscn_queue_index;
688                 } else {
689                         ha->flags.rscn_queue_overflow = 1;
690                 }
691
692                 atomic_set(&ha->loop_state, LOOP_UPDATE);
693                 atomic_set(&ha->loop_down_timer, 0);
694                 ha->flags.management_server_logged_in = 0;
695
696                 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
697                 set_bit(RSCN_UPDATE, &ha->dpc_flags);
698                 qla2x00_post_aen_work(ha, FCH_EVT_RSCN, rscn_entry);
699 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
700                 if (qla_target.tgt_async_event)
701                         qla_target.tgt_async_event(mb[0], ha, mb);
702 #endif
703                 break;
704
705         /* case MBA_RIO_RESPONSE: */
706         case MBA_ZIO_RESPONSE:
707                 DEBUG2(printk("scsi(%ld): [R|Z]IO update completion.\n",
708                     ha->host_no));
709                 DEBUG(printk(KERN_INFO
710                     "scsi(%ld): [R|Z]IO update completion.\n",
711                     ha->host_no));
712
713                 if (IS_FWI2_CAPABLE(ha))
714                         qla24xx_process_response_queue(ha);
715                 else
716                         qla2x00_process_response_queue(ha);
717                 break;
718
719         case MBA_DISCARD_RND_FRAME:
720                 DEBUG2(printk("scsi(%ld): Discard RND Frame -- %04x %04x "
721                     "%04x.\n", ha->host_no, mb[1], mb[2], mb[3]));
722                 break;
723
724         case MBA_TRACE_NOTIFICATION:
725                 DEBUG2(printk("scsi(%ld): Trace Notification -- %04x %04x.\n",
726                 ha->host_no, mb[1], mb[2]));
727                 break;
728
729         case MBA_ISP84XX_ALERT:
730                 DEBUG2(printk("scsi(%ld): ISP84XX Alert Notification -- "
731                     "%04x %04x %04x\n", ha->host_no, mb[1], mb[2], mb[3]));
732
733                 spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
734                 switch (mb[1]) {
735                 case A84_PANIC_RECOVERY:
736                         qla_printk(KERN_INFO, ha, "Alert 84XX: panic recovery "
737                             "%04x %04x\n", mb[2], mb[3]);
738                         break;
739                 case A84_OP_LOGIN_COMPLETE:
740                         ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
741                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
742                             "firmware version %x\n", ha->cs84xx->op_fw_version));
743                         break;
744                 case A84_DIAG_LOGIN_COMPLETE:
745                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
746                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX:"
747                             "diagnostic firmware version %x\n",
748                             ha->cs84xx->diag_fw_version));
749                         break;
750                 case A84_GOLD_LOGIN_COMPLETE:
751                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
752                         ha->cs84xx->fw_update = 1;
753                         DEBUG2(qla_printk(KERN_INFO, ha, "Alert 84XX: gold "
754                             "firmware version %x\n",
755                             ha->cs84xx->gold_fw_version));
756                         break;
757                 default:
758                         qla_printk(KERN_ERR, ha,
759                             "Alert 84xx: Invalid Alert %04x %04x %04x\n",
760                             mb[1], mb[2], mb[3]);
761                 }
762                 spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
763                 break;
764         }
765
766         if (!ha->parent && ha->num_vhosts)
767                 qla2x00_alert_all_vps(ha, mb);
768 }
769
770 static void
771 qla2x00_adjust_sdev_qdepth_up(struct scsi_device *sdev, void *data)
772 {
773         fc_port_t *fcport = data;
774
775         if (fcport->ha->max_q_depth <= sdev->queue_depth)
776                 return;
777
778         if (sdev->ordered_tags)
779                 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
780                     sdev->queue_depth + 1);
781         else
782                 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
783                     sdev->queue_depth + 1);
784
785         fcport->last_ramp_up = jiffies;
786
787         DEBUG2(qla_printk(KERN_INFO, fcport->ha,
788             "scsi(%ld:%d:%d:%d): Queue depth adjusted-up to %d.\n",
789             fcport->ha->host_no, sdev->channel, sdev->id, sdev->lun,
790             sdev->queue_depth));
791 }
792
793 static void
794 qla2x00_adjust_sdev_qdepth_down(struct scsi_device *sdev, void *data)
795 {
796         fc_port_t *fcport = data;
797
798         if (!scsi_track_queue_full(sdev, sdev->queue_depth - 1))
799                 return;
800
801         DEBUG2(qla_printk(KERN_INFO, fcport->ha,
802             "scsi(%ld:%d:%d:%d): Queue depth adjusted-down to %d.\n",
803             fcport->ha->host_no, sdev->channel, sdev->id, sdev->lun,
804             sdev->queue_depth));
805 }
806
807 static inline void
808 qla2x00_ramp_up_queue_depth(scsi_qla_host_t *ha, srb_t *sp)
809 {
810         fc_port_t *fcport;
811         struct scsi_device *sdev;
812
813         sdev = sp->cmd->device;
814         if (sdev->queue_depth >= ha->max_q_depth)
815                 return;
816
817         fcport = sp->fcport;
818         if (time_before(jiffies,
819             fcport->last_ramp_up + ql2xqfullrampup * HZ))
820                 return;
821         if (time_before(jiffies,
822             fcport->last_queue_full + ql2xqfullrampup * HZ))
823                 return;
824
825         starget_for_each_device(sdev->sdev_target, fcport,
826             qla2x00_adjust_sdev_qdepth_up);
827 }
828
829 /**
830  * qla2x00_process_completed_request() - Process a Fast Post response.
831  * @ha: SCSI driver HA context
832  * @index: SRB index
833  */
834 static void
835 qla2x00_process_completed_request(struct scsi_qla_host *ha, uint32_t index)
836 {
837         srb_t *sp;
838
839 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
840         if (HANDLE_IS_CTIO_COMP(index)) {
841                 if (qla_target.tgt_ctio_completion)
842                         qla_target.tgt_ctio_completion(ha, index);
843                 return;
844         }
845 #endif
846
847         /* Validate handle. */
848         if (index >= MAX_OUTSTANDING_COMMANDS) {
849                 DEBUG2(printk("scsi(%ld): Invalid SCSI completion handle %d.\n",
850                     ha->host_no, index));
851                 qla_printk(KERN_WARNING, ha,
852                     "Invalid SCSI completion handle %d.\n", index);
853
854                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
855                 return;
856         }
857
858         sp = ha->outstanding_cmds[index];
859         if (sp) {
860                 /* Free outstanding command slot. */
861                 ha->outstanding_cmds[index] = NULL;
862
863                 CMD_COMPL_STATUS(sp->cmd) = 0L;
864                 CMD_SCSI_STATUS(sp->cmd) = 0L;
865
866                 /* Save ISP completion status */
867                 sp->cmd->result = DID_OK << 16;
868
869                 qla2x00_ramp_up_queue_depth(ha, sp);
870                 qla2x00_sp_compl(ha, sp);
871         } else {
872                 DEBUG2(printk("scsi(%ld): Invalid ISP SCSI completion handle\n",
873                     ha->host_no));
874                 qla_printk(KERN_WARNING, ha,
875                     "Invalid ISP SCSI completion handle\n");
876
877                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
878         }
879 }
880
881 /**
882  * qla2x00_process_response_queue() - Process response queue entries.
883  * @ha: SCSI driver HA context
884  */
885 void
886 qla2x00_process_response_queue(struct scsi_qla_host *ha)
887 {
888         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
889         sts_entry_t     *pkt;
890         uint16_t        handle_cnt;
891         uint16_t        cnt;
892
893         if (!ha->flags.online)
894                 return;
895
896         while (ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
897                 pkt = (sts_entry_t *)ha->response_ring_ptr;
898
899                 ha->rsp_ring_index++;
900                 if (ha->rsp_ring_index == ha->response_q_length) {
901                         ha->rsp_ring_index = 0;
902                         ha->response_ring_ptr = ha->response_ring;
903                 } else {
904                         ha->response_ring_ptr++;
905                 }
906
907                 if (pkt->entry_status != 0) {
908                         DEBUG3(printk(KERN_INFO
909                             "scsi(%ld): Process error entry.\n", ha->host_no));
910
911                         qla2x00_error_entry(ha, pkt);
912                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
913                         wmb();
914                         continue;
915                 }
916
917                 switch (pkt->entry_type) {
918 #ifdef CONFIG_SCSI_QLA2XXX_TARGET
919                 case ACCEPT_TGT_IO_TYPE:
920                 case CONTINUE_TGT_IO_TYPE:
921                 case CTIO_A64_TYPE:
922                 case IMMED_NOTIFY_TYPE:
923                 case NOTIFY_ACK_TYPE:
924                 case ENABLE_LUN_TYPE:
925                 case MODIFY_LUN_TYPE:
926                         DEBUG4(printk(KERN_WARNING
927                                       "qla2x00_response_pkt: "
928                                       "calling tgt_response_pkt %p "
929                                       "(type %02X)\n",
930                                       qla_target.tgt_response_pkt,
931                                       pkt->entry_type););
932                         if (qla_target.tgt_response_pkt)
933                                 qla_target.tgt_response_pkt(ha, pkt);
934                         break;
935 #endif /* CONFIG_SCSI_QLA2XXX_TARGET */
936                 case STATUS_TYPE:
937                         qla2x00_status_entry(ha, pkt);
938                         break;
939                 case STATUS_TYPE_21:
940                         handle_cnt = ((sts21_entry_t *)pkt)->handle_count;
941                         for (cnt = 0; cnt < handle_cnt; cnt++) {
942                                 qla2x00_process_completed_request(ha,
943                                     ((sts21_entry_t *)pkt)->handle[cnt]);
944                         }
945                         break;
946                 case STATUS_TYPE_22:
947                         handle_cnt = ((sts22_entry_t *)pkt)->handle_count;
948                         for (cnt = 0; cnt < handle_cnt; cnt++) {
949                                 qla2x00_process_completed_request(ha,
950                                     ((sts22_entry_t *)pkt)->handle[cnt]);
951                         }
952                         break;
953                 case STATUS_CONT_TYPE:
954                         qla2x00_status_cont_entry(ha, (sts_cont_entry_t *)pkt);
955                         break;
956                 default:
957                         /* Type Not Supported. */
958                         DEBUG4(printk(KERN_WARNING
959                             "scsi(%ld): Received unknown response pkt type %x "
960                             "entry status=%x.\n",
961                             ha->host_no, pkt->entry_type, pkt->entry_status));
962                         break;
963                 }
964                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
965                 wmb();
966         }
967
968         /* Adjust ring index */
969         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), ha->rsp_ring_index);
970 }
971
972 static inline void
973 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t sense_len)
974 {
975         struct scsi_cmnd *cp = sp->cmd;
976
977         if (sense_len >= SCSI_SENSE_BUFFERSIZE)
978                 sense_len = SCSI_SENSE_BUFFERSIZE;
979
980         CMD_ACTUAL_SNSLEN(cp) = sense_len;
981         sp->request_sense_length = sense_len;
982         sp->request_sense_ptr = cp->sense_buffer;
983         if (sp->request_sense_length > 32)
984                 sense_len = 32;
985
986         memcpy(cp->sense_buffer, sense_data, sense_len);
987
988         sp->request_sense_ptr += sense_len;
989         sp->request_sense_length -= sense_len;
990         if (sp->request_sense_length != 0)
991                 sp->ha->status_srb = sp;
992
993         DEBUG5(printk("%s(): Check condition Sense data, scsi(%ld:%d:%d:%d) "
994             "cmd=%p pid=%ld\n", __func__, sp->ha->host_no, cp->device->channel,
995             cp->device->id, cp->device->lun, cp, cp->serial_number));
996         if (sense_len)
997                 DEBUG5(qla2x00_dump_buffer(cp->sense_buffer,
998                     CMD_ACTUAL_SNSLEN(cp)));
999 }
1000
1001 /**
1002  * qla2x00_status_entry() - Process a Status IOCB entry.
1003  * @ha: SCSI driver HA context
1004  * @pkt: Entry pointer
1005  */
1006 static void
1007 qla2x00_status_entry(scsi_qla_host_t *ha, void *pkt)
1008 {
1009         srb_t           *sp;
1010         fc_port_t       *fcport;
1011         struct scsi_cmnd *cp;
1012         sts_entry_t *sts;
1013         struct sts_entry_24xx *sts24;
1014         uint16_t        comp_status;
1015         uint16_t        scsi_status;
1016         uint8_t         lscsi_status;
1017         int32_t         resid;
1018         uint32_t        sense_len, rsp_info_len, resid_len, fw_resid_len;
1019         uint8_t         *rsp_info, *sense_data;
1020
1021         sts = (sts_entry_t *) pkt;
1022         sts24 = (struct sts_entry_24xx *) pkt;
1023         if (IS_FWI2_CAPABLE(ha)) {
1024                 comp_status = le16_to_cpu(sts24->comp_status);
1025                 scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
1026         } else {
1027                 comp_status = le16_to_cpu(sts->comp_status);
1028                 scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
1029         }
1030
1031         /* Fast path completion. */
1032         if (comp_status == CS_COMPLETE && scsi_status == 0) {
1033                 DEBUG3(printk(KERN_WARNING "scsi(%ld): "
1034                               "Status Type %#x:\n"
1035                               "  entry_count\t%d\n"
1036                               "  entry_status\t%#x\n"
1037                               "  handle:\t%#x\n"
1038                               "  scsi_status:\t%#x\n"
1039                               "  comp_status:\t%#x\n"
1040                               "  state_flags:\t%#x\n"
1041                               "  status_flags:\t%#x\n",
1042                               ha->host_no,
1043                               sts->entry_type,
1044                               sts->entry_count,
1045                               sts->entry_status,
1046                               sts->handle,
1047                               sts->scsi_status,
1048                               sts->comp_status,
1049                               sts->state_flags,
1050                               sts->status_flags));
1051                 qla2x00_process_completed_request(ha, sts->handle);
1052
1053                 return;
1054         }
1055
1056         /* Validate handle. */
1057         if (sts->handle < MAX_OUTSTANDING_COMMANDS) {
1058                 sp = ha->outstanding_cmds[sts->handle];
1059                 ha->outstanding_cmds[sts->handle] = NULL;
1060         } else
1061                 sp = NULL;
1062
1063         if (sp == NULL) {
1064                 DEBUG2(printk("scsi(%ld): Status Entry invalid handle.\n",
1065                     ha->host_no));
1066                 qla_printk(KERN_WARNING, ha, "Status Entry invalid handle.\n");
1067
1068                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1069                 qla2xxx_wake_dpc(ha);
1070                 return;
1071         }
1072         cp = sp->cmd;
1073         if (cp == NULL) {
1074                 DEBUG2(printk("scsi(%ld): Command already returned back to OS "
1075                     "pkt->handle=%d sp=%p.\n", ha->host_no, sts->handle, sp));
1076                 qla_printk(KERN_WARNING, ha,
1077                     "Command is NULL: already returned to OS (sp=%p)\n", sp);
1078
1079                 return;
1080         }
1081
1082         lscsi_status = scsi_status & STATUS_MASK;
1083         CMD_ENTRY_STATUS(cp) = sts->entry_status;
1084         CMD_COMPL_STATUS(cp) = comp_status;
1085         CMD_SCSI_STATUS(cp) = scsi_status;
1086
1087         fcport = sp->fcport;
1088
1089         sense_len = rsp_info_len = resid_len = fw_resid_len = 0;
1090         if (IS_FWI2_CAPABLE(ha)) {
1091                 sense_len = le32_to_cpu(sts24->sense_len);
1092                 rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
1093                 resid_len = le32_to_cpu(sts24->rsp_residual_count);
1094                 fw_resid_len = le32_to_cpu(sts24->residual_len);
1095                 rsp_info = sts24->data;
1096                 sense_data = sts24->data;
1097                 host_to_fcp_swap(sts24->data, sizeof(sts24->data));
1098         } else {
1099                 sense_len = le16_to_cpu(sts->req_sense_length);
1100                 rsp_info_len = le16_to_cpu(sts->rsp_info_len);
1101                 resid_len = le32_to_cpu(sts->residual_length);
1102                 rsp_info = sts->rsp_info;
1103                 sense_data = sts->req_sense_data;
1104         }
1105
1106         /* Check for any FCP transport errors. */
1107         if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
1108                 /* Sense data lies beyond any FCP RESPONSE data. */
1109                 if (IS_FWI2_CAPABLE(ha))
1110                         sense_data += rsp_info_len;
1111                 if (rsp_info_len > 3 && rsp_info[3]) {
1112                         DEBUG2(printk("scsi(%ld:%d:%d:%d) FCP I/O protocol "
1113                             "failure (%x/%02x%02x%02x%02x%02x%02x%02x%02x)..."
1114                             "retrying command\n", ha->host_no,
1115                             cp->device->channel, cp->device->id,
1116                             cp->device->lun, rsp_info_len, rsp_info[0],
1117                             rsp_info[1], rsp_info[2], rsp_info[3], rsp_info[4],
1118                             rsp_info[5], rsp_info[6], rsp_info[7]));
1119
1120                         cp->result = DID_BUS_BUSY << 16;
1121                         qla2x00_sp_compl(ha, sp);
1122                         return;
1123                 }
1124         }
1125
1126         /* Check for overrun. */
1127         if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
1128             scsi_status & SS_RESIDUAL_OVER)
1129                 comp_status = CS_DATA_OVERRUN;
1130
1131         /*
1132          * Based on Host and scsi status generate status code for Linux
1133          */
1134         switch (comp_status) {
1135         case CS_COMPLETE:
1136         case CS_QUEUE_FULL:
1137                 if (scsi_status == 0) {
1138                         cp->result = DID_OK << 16;
1139                         break;
1140                 }
1141                 if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
1142                         resid = resid_len;
1143                         scsi_set_resid(cp, resid);
1144                         CMD_RESID_LEN(cp) = resid;
1145
1146                         if (!lscsi_status &&
1147                             ((unsigned)(scsi_bufflen(cp) - resid) <
1148                              cp->underflow)) {
1149                                 qla_printk(KERN_INFO, ha,
1150                                            "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1151                                            "detected (%x of %x bytes)...returning "
1152                                            "error status.\n", ha->host_no,
1153                                            cp->device->channel, cp->device->id,
1154                                            cp->device->lun, resid,
1155                                            scsi_bufflen(cp));
1156
1157                                 cp->result = DID_ERROR << 16;
1158                                 break;
1159                         }
1160                 }
1161                 cp->result = DID_OK << 16 | lscsi_status;
1162
1163                 if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1164                         DEBUG2(printk(KERN_INFO
1165                             "scsi(%ld): QUEUE FULL status detected "
1166                             "0x%x-0x%x.\n", ha->host_no, comp_status,
1167                             scsi_status));
1168
1169                         /* Adjust queue depth for all luns on the port. */
1170                         fcport->last_queue_full = jiffies;
1171                         starget_for_each_device(cp->device->sdev_target,
1172                             fcport, qla2x00_adjust_sdev_qdepth_down);
1173                         break;
1174                 }
1175                 if (lscsi_status != SS_CHECK_CONDITION)
1176                         break;
1177
1178                 memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1179                 if (!(scsi_status & SS_SENSE_LEN_VALID))
1180                         break;
1181
1182                 qla2x00_handle_sense(sp, sense_data, sense_len);
1183                 break;
1184
1185         case CS_DATA_UNDERRUN:
1186                 resid = resid_len;
1187                 /* Use F/W calculated residual length. */
1188                 if (IS_FWI2_CAPABLE(ha)) {
1189                         if (scsi_status & SS_RESIDUAL_UNDER &&
1190                             resid != fw_resid_len) {
1191                                 scsi_status &= ~SS_RESIDUAL_UNDER;
1192                                 lscsi_status = 0;
1193                         }
1194                         resid = fw_resid_len;
1195                 }
1196
1197                 if (scsi_status & SS_RESIDUAL_UNDER) {
1198                         scsi_set_resid(cp, resid);
1199                         CMD_RESID_LEN(cp) = resid;
1200                 } else {
1201                         DEBUG2(printk(KERN_INFO
1202                             "scsi(%ld:%d:%d) UNDERRUN status detected "
1203                             "0x%x-0x%x. resid=0x%x fw_resid=0x%x cdb=0x%x "
1204                             "os_underflow=0x%x\n", ha->host_no,
1205                             cp->device->id, cp->device->lun, comp_status,
1206                             scsi_status, resid_len, resid, cp->cmnd[0],
1207                             cp->underflow));
1208
1209                 }
1210
1211                 /*
1212                  * Check to see if SCSI Status is non zero. If so report SCSI
1213                  * Status.
1214                  */
1215                 if (lscsi_status != 0) {
1216                         cp->result = DID_OK << 16 | lscsi_status;
1217
1218                         if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
1219                                 DEBUG2(printk(KERN_INFO
1220                                     "scsi(%ld): QUEUE FULL status detected "
1221                                     "0x%x-0x%x.\n", ha->host_no, comp_status,
1222                                     scsi_status));
1223
1224                                 /*
1225                                  * Adjust queue depth for all luns on the
1226                                  * port.
1227                                  */
1228                                 fcport->last_queue_full = jiffies;
1229                                 starget_for_each_device(
1230                                     cp->device->sdev_target, fcport,
1231                                     qla2x00_adjust_sdev_qdepth_down);
1232                                 break;
1233                         }
1234                         if (lscsi_status != SS_CHECK_CONDITION)
1235                                 break;
1236
1237                         memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1238                         if (!(scsi_status & SS_SENSE_LEN_VALID))
1239                                 break;
1240
1241                         qla2x00_handle_sense(sp, sense_data, sense_len);
1242                 } else {
1243                         /*
1244                          * If RISC reports underrun and target does not report
1245                          * it then we must have a lost frame, so tell upper
1246                          * layer to retry it by reporting a bus busy.
1247                          */
1248                         if (!(scsi_status & SS_RESIDUAL_UNDER)) {
1249                                 DEBUG2(printk("scsi(%ld:%d:%d:%d) Dropped "
1250                                               "frame(s) detected (%x of %x bytes)..."
1251                                               "retrying command.\n", ha->host_no,
1252                                               cp->device->channel, cp->device->id,
1253                                               cp->device->lun, resid,
1254                                               scsi_bufflen(cp)));
1255
1256                                 cp->result = DID_BUS_BUSY << 16;
1257                                 break;
1258                         }
1259
1260                         /* Handle mid-layer underflow */
1261                         if ((unsigned)(scsi_bufflen(cp) - resid) <
1262                             cp->underflow) {
1263                                 qla_printk(KERN_INFO, ha,
1264                                            "scsi(%ld:%d:%d:%d): Mid-layer underflow "
1265                                            "detected (%x of %x bytes)...returning "
1266                                            "error status.\n", ha->host_no,
1267                                            cp->device->channel, cp->device->id,
1268                                            cp->device->lun, resid,
1269                                            scsi_bufflen(cp));
1270
1271                                 cp->result = DID_ERROR << 16;
1272                                 break;
1273                         }
1274
1275                         /* Everybody online, looking good... */
1276                         cp->result = DID_OK << 16;
1277                 }
1278                 break;
1279
1280         case CS_DATA_OVERRUN:
1281                 DEBUG2(printk(KERN_INFO
1282                     "scsi(%ld:%d:%d): OVERRUN status detected 0x%x-0x%x\n",
1283                     ha->host_no, cp->device->id, cp->device->lun, comp_status,
1284                     scsi_status));
1285                 DEBUG2(printk(KERN_INFO
1286                     "CDB: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
1287                     cp->cmnd[0], cp->cmnd[1], cp->cmnd[2], cp->cmnd[3],
1288                     cp->cmnd[4], cp->cmnd[5]));
1289                 DEBUG2(printk(KERN_INFO
1290                     "PID=0x%lx req=0x%x xtra=0x%x -- returning DID_ERROR "
1291                     "status!\n",
1292                     cp->serial_number, scsi_bufflen(cp), resid_len));
1293
1294                 cp->result = DID_ERROR << 16;
1295                 break;
1296
1297         case CS_PORT_LOGGED_OUT:
1298         case CS_PORT_CONFIG_CHG:
1299         case CS_PORT_BUSY:
1300         case CS_INCOMPLETE:
1301         case CS_PORT_UNAVAILABLE:
1302                 /*
1303                  * If the port is in Target Down state, return all IOs for this
1304                  * Target with DID_NO_CONNECT ELSE Queue the IOs in the
1305                  * retry_queue.
1306                  */
1307                 DEBUG2(printk("scsi(%ld:%d:%d): status_entry: Port Down "
1308                     "pid=%ld, compl status=0x%x, port state=0x%x\n",
1309                     ha->host_no, cp->device->id, cp->device->lun,
1310                     cp->serial_number, comp_status,
1311                     atomic_read(&fcport->state)));
1312
1313                 cp->result = DID_BUS_BUSY << 16;
1314                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
1315                         qla2x00_mark_device_lost(ha, fcport, 1, 1);
1316                 }
1317                 break;
1318
1319         case CS_RESET:
1320                 DEBUG2(printk(KERN_INFO
1321                     "scsi(%ld): RESET status detected 0x%x-0x%x.\n",
1322                     ha->host_no, comp_status, scsi_status));
1323
1324                 cp->result = DID_RESET << 16;
1325                 break;
1326
1327         case CS_ABORTED:
1328                 /*
1329                  * hv2.19.12 - DID_ABORT does not retry the request if we
1330                  * aborted this request then abort otherwise it must be a
1331                  * reset.
1332                  */
1333                 DEBUG2(printk(KERN_INFO
1334                     "scsi(%ld): ABORT status detected 0x%x-0x%x.\n",
1335                     ha->host_no, comp_status, scsi_status));
1336
1337                 cp->result = DID_RESET << 16;
1338                 break;
1339
1340         case CS_TIMEOUT:
1341                 cp->result = DID_BUS_BUSY << 16;
1342
1343                 if (IS_FWI2_CAPABLE(ha)) {
1344                         DEBUG2(printk(KERN_INFO
1345                             "scsi(%ld:%d:%d:%d): TIMEOUT status detected "
1346                             "0x%x-0x%x\n", ha->host_no, cp->device->channel,
1347                             cp->device->id, cp->device->lun, comp_status,
1348                             scsi_status));
1349                         break;
1350                 }
1351                 DEBUG2(printk(KERN_INFO
1352                     "scsi(%ld:%d:%d:%d): TIMEOUT status detected 0x%x-0x%x "
1353                     "sflags=%x.\n", ha->host_no, cp->device->channel,
1354                     cp->device->id, cp->device->lun, comp_status, scsi_status,
1355                     le16_to_cpu(sts->status_flags)));
1356
1357                 /* Check to see if logout occurred. */
1358                 if ((le16_to_cpu(sts->status_flags) & SF_LOGOUT_SENT))
1359                         qla2x00_mark_device_lost(ha, fcport, 1, 1);
1360                 break;
1361
1362         default:
1363                 DEBUG3(printk("scsi(%ld): Error detected (unknown status) "
1364                     "0x%x-0x%x.\n", ha->host_no, comp_status, scsi_status));
1365                 qla_printk(KERN_INFO, ha,
1366                     "Unknown status detected 0x%x-0x%x.\n",
1367                     comp_status, scsi_status);
1368
1369                 cp->result = DID_ERROR << 16;
1370                 break;
1371         }
1372
1373         /* Place command on done queue. */
1374         if (ha->status_srb == NULL)
1375                 qla2x00_sp_compl(ha, sp);
1376 }
1377
1378 /**
1379  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
1380  * @ha: SCSI driver HA context
1381  * @pkt: Entry pointer
1382  *
1383  * Extended sense data.
1384  */
1385 static void
1386 qla2x00_status_cont_entry(scsi_qla_host_t *ha, sts_cont_entry_t *pkt)
1387 {
1388         uint8_t         sense_sz = 0;
1389         srb_t           *sp = ha->status_srb;
1390         struct scsi_cmnd *cp;
1391
1392         if (sp != NULL && sp->request_sense_length != 0) {
1393                 cp = sp->cmd;
1394                 if (cp == NULL) {
1395                         DEBUG2(printk("%s(): Cmd already returned back to OS "
1396                             "sp=%p.\n", __func__, sp));
1397                         qla_printk(KERN_INFO, ha,
1398                             "cmd is NULL: already returned to OS (sp=%p)\n",
1399                             sp);
1400
1401                         ha->status_srb = NULL;
1402                         return;
1403                 }
1404
1405                 if (sp->request_sense_length > sizeof(pkt->data)) {
1406                         sense_sz = sizeof(pkt->data);
1407                 } else {
1408                         sense_sz = sp->request_sense_length;
1409                 }
1410
1411                 /* Move sense data. */
1412                 if (IS_FWI2_CAPABLE(ha))
1413                         host_to_fcp_swap(pkt->data, sizeof(pkt->data));
1414                 memcpy(sp->request_sense_ptr, pkt->data, sense_sz);
1415                 DEBUG5(qla2x00_dump_buffer(sp->request_sense_ptr, sense_sz));
1416
1417                 sp->request_sense_ptr += sense_sz;
1418                 sp->request_sense_length -= sense_sz;
1419
1420                 /* Place command on done queue. */
1421                 if (sp->request_sense_length == 0) {
1422                         ha->status_srb = NULL;
1423                         qla2x00_sp_compl(ha, sp);
1424                 }
1425         }
1426 }
1427
1428 /**
1429  * qla2x00_error_entry() - Process an error entry.
1430  * @ha: SCSI driver HA context
1431  * @pkt: Entry pointer
1432  */
1433 static void
1434 qla2x00_error_entry(scsi_qla_host_t *ha, sts_entry_t *pkt)
1435 {
1436         srb_t *sp;
1437
1438 #if defined(QL_DEBUG_LEVEL_2)
1439         if (pkt->entry_status & RF_INV_E_ORDER)
1440                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Order\n", __func__);
1441         else if (pkt->entry_status & RF_INV_E_COUNT)
1442                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Count\n", __func__);
1443         else if (pkt->entry_status & RF_INV_E_PARAM)
1444                 qla_printk(KERN_ERR, ha,
1445                     "%s: Invalid Entry Parameter\n", __func__);
1446         else if (pkt->entry_status & RF_INV_E_TYPE)
1447                 qla_printk(KERN_ERR, ha, "%s: Invalid Entry Type\n", __func__);
1448         else if (pkt->entry_status & RF_BUSY)
1449                 qla_printk(KERN_ERR, ha, "%s: Busy\n", __func__);
1450         else
1451                 qla_printk(KERN_ERR, ha, "%s: UNKNOWN flag error\n", __func__);
1452 #endif
1453
1454         /* Validate handle. */
1455         if (pkt->handle < MAX_OUTSTANDING_COMMANDS)
1456                 sp = ha->outstanding_cmds[pkt->handle];
1457         else
1458                 sp = NULL;
1459
1460         if (sp) {
1461                 /* Free outstanding command slot. */
1462                 ha->outstanding_cmds[pkt->handle] = NULL;
1463
1464                 /* Bad payload or header */
1465                 if (pkt->entry_status &
1466                     (RF_INV_E_ORDER | RF_INV_E_COUNT |
1467                      RF_INV_E_PARAM | RF_INV_E_TYPE)) {
1468                         sp->cmd->result = DID_ERROR << 16;
1469                 } else if (pkt->entry_status & RF_BUSY) {
1470                         sp->cmd->result = DID_BUS_BUSY << 16;
1471                 } else {
1472                         sp->cmd->result = DID_ERROR << 16;
1473                 }
1474                 qla2x00_sp_compl(ha, sp);
1475
1476         } else if (pkt->entry_type == COMMAND_A64_TYPE || pkt->entry_type ==
1477             COMMAND_TYPE || pkt->entry_type == COMMAND_TYPE_7) {
1478                 DEBUG2(printk("scsi(%ld): Error entry - invalid handle\n",
1479                     ha->host_no));
1480                 qla_printk(KERN_WARNING, ha,
1481                     "Error entry - invalid handle\n");
1482
1483                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1484                 qla2xxx_wake_dpc(ha);
1485         }
1486 }
1487
1488 /**
1489  * qla24xx_mbx_completion() - Process mailbox command completions.
1490  * @ha: SCSI driver HA context
1491  * @mb0: Mailbox0 register
1492  */
1493 static void
1494 qla24xx_mbx_completion(scsi_qla_host_t *ha, uint16_t mb0)
1495 {
1496         uint16_t        cnt;
1497         uint16_t __iomem *wptr;
1498         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1499
1500         /* Load return mailbox registers. */
1501         ha->flags.mbox_int = 1;
1502         ha->mailbox_out[0] = mb0;
1503         wptr = (uint16_t __iomem *)&reg->mailbox1;
1504
1505         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
1506                 ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
1507                 wptr++;
1508         }
1509
1510         if (ha->mcp) {
1511                 DEBUG3(printk("%s(%ld): Got mailbox completion. cmd=%x.\n",
1512                     __func__, ha->host_no, ha->mcp->mb[0]));
1513         } else {
1514                 DEBUG2_3(printk("%s(%ld): MBX pointer ERROR!\n",
1515                     __func__, ha->host_no));
1516         }
1517 }
1518
1519 /**
1520  * qla24xx_process_response_queue() - Process response queue entries.
1521  * @ha: SCSI driver HA context
1522  */
1523 void
1524 qla24xx_process_response_queue(struct scsi_qla_host *ha)
1525 {
1526         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1527         struct sts_entry_24xx *pkt;
1528
1529         if (!ha->flags.online)
1530                 return;
1531
1532         while (ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
1533                 pkt = (struct sts_entry_24xx *)ha->response_ring_ptr;
1534
1535                 ha->rsp_ring_index++;
1536                 if (ha->rsp_ring_index == ha->response_q_length) {
1537                         ha->rsp_ring_index = 0;
1538                         ha->response_ring_ptr = ha->response_ring;
1539                 } else {
1540                         ha->response_ring_ptr++;
1541                 }
1542
1543                 if (pkt->entry_status != 0) {
1544                         DEBUG3(printk(KERN_INFO
1545                             "scsi(%ld): Process error entry.\n", ha->host_no));
1546
1547                         qla2x00_error_entry(ha, (sts_entry_t *) pkt);
1548                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1549                         wmb();
1550                         continue;
1551                 }
1552
1553                 switch (pkt->entry_type) {
1554                 case STATUS_TYPE:
1555                         qla2x00_status_entry(ha, pkt);
1556                         break;
1557                 case STATUS_CONT_TYPE:
1558                         qla2x00_status_cont_entry(ha, (sts_cont_entry_t *)pkt);
1559                         break;
1560                 case VP_RPT_ID_IOCB_TYPE:
1561                         qla24xx_report_id_acquisition(ha,
1562                             (struct vp_rpt_id_entry_24xx *)pkt);
1563                         break;
1564                 default:
1565                         /* Type Not Supported. */
1566                         DEBUG4(printk(KERN_WARNING
1567                             "scsi(%ld): Received unknown response pkt type %x "
1568                             "entry status=%x.\n",
1569                             ha->host_no, pkt->entry_type, pkt->entry_status));
1570                         break;
1571                 }
1572                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1573                 wmb();
1574         }
1575
1576         /* Adjust ring index */
1577         WRT_REG_DWORD(&reg->rsp_q_out, ha->rsp_ring_index);
1578 }
1579
1580 static void
1581 qla2xxx_check_risc_status(scsi_qla_host_t *ha)
1582 {
1583         int rval;
1584         uint32_t cnt;
1585         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1586
1587         if (!IS_QLA25XX(ha))
1588                 return;
1589
1590         rval = QLA_SUCCESS;
1591         WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
1592         RD_REG_DWORD(&reg->iobase_addr);
1593         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1594         for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1595             rval == QLA_SUCCESS; cnt--) {
1596                 if (cnt) {
1597                         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
1598                         udelay(10);
1599                 } else
1600                         rval = QLA_FUNCTION_TIMEOUT;
1601         }
1602         if (rval == QLA_SUCCESS)
1603                 goto next_test;
1604
1605         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1606         for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
1607             rval == QLA_SUCCESS; cnt--) {
1608                 if (cnt) {
1609                         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
1610                         udelay(10);
1611                 } else
1612                         rval = QLA_FUNCTION_TIMEOUT;
1613         }
1614         if (rval != QLA_SUCCESS)
1615                 goto done;
1616
1617 next_test:
1618         if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
1619                 qla_printk(KERN_INFO, ha, "Additional code -- 0x55AA.\n");
1620
1621 done:
1622         WRT_REG_DWORD(&reg->iobase_window, 0x0000);
1623         RD_REG_DWORD(&reg->iobase_window);
1624 }
1625
1626 /**
1627  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
1628  * @irq:
1629  * @dev_id: SCSI driver HA context
1630  *
1631  * Called by system whenever the host adapter generates an interrupt.
1632  *
1633  * Returns handled flag.
1634  */
1635 irqreturn_t
1636 qla24xx_intr_handler(int irq, void *dev_id)
1637 {
1638         scsi_qla_host_t *ha;
1639         struct device_reg_24xx __iomem *reg;
1640         int             status;
1641         unsigned long   iter;
1642         uint32_t        stat;
1643         uint32_t        hccr;
1644         uint16_t        mb[4];
1645
1646         ha = (scsi_qla_host_t *) dev_id;
1647         if (!ha) {
1648                 printk(KERN_INFO
1649                     "%s(): NULL host pointer\n", __func__);
1650                 return IRQ_NONE;
1651         }
1652
1653         reg = &ha->iobase->isp24;
1654         status = 0;
1655
1656         spin_lock(&ha->hardware_lock);
1657         for (iter = 50; iter--; ) {
1658                 stat = RD_REG_DWORD(&reg->host_status);
1659                 if (stat & HSRX_RISC_PAUSED) {
1660                         if (pci_channel_offline(ha->pdev))
1661                                 break;
1662
1663                         if (ha->hw_event_pause_errors == 0)
1664                                 qla2x00_post_hwe_work(ha, HW_EVENT_PARITY_ERR,
1665                                     0, MSW(stat), LSW(stat));
1666                         else if (ha->hw_event_pause_errors < 0xffffffff)
1667                                 ha->hw_event_pause_errors++;
1668
1669                         hccr = RD_REG_DWORD(&reg->hccr);
1670
1671                         qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1672                             "Dumping firmware!\n", hccr);
1673
1674                         qla2xxx_check_risc_status(ha);
1675
1676                         ha->isp_ops->fw_dump(ha, 1);
1677                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1678                         break;
1679                 } else if ((stat & HSRX_RISC_INT) == 0)
1680                         break;
1681
1682                 switch (stat & 0xff) {
1683                 case 0x1:
1684                 case 0x2:
1685                 case 0x10:
1686                 case 0x11:
1687                         qla24xx_mbx_completion(ha, MSW(stat));
1688                         status |= MBX_INTERRUPT;
1689
1690                         break;
1691                 case 0x12:
1692                         mb[0] = MSW(stat);
1693                         mb[1] = RD_REG_WORD(&reg->mailbox1);
1694                         mb[2] = RD_REG_WORD(&reg->mailbox2);
1695                         mb[3] = RD_REG_WORD(&reg->mailbox3);
1696                         qla2x00_async_event(ha, mb);
1697                         break;
1698                 case 0x13:
1699                         qla24xx_process_response_queue(ha);
1700                         break;
1701                 default:
1702                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1703                             "(%d).\n",
1704                             ha->host_no, stat & 0xff));
1705                         break;
1706                 }
1707                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1708                 RD_REG_DWORD_RELAXED(&reg->hccr);
1709         }
1710         spin_unlock(&ha->hardware_lock);
1711
1712         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1713             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1714                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1715                 complete(&ha->mbx_intr_comp);
1716         }
1717
1718         return IRQ_HANDLED;
1719 }
1720
1721 static irqreturn_t
1722 qla24xx_msix_rsp_q(int irq, void *dev_id)
1723 {
1724         scsi_qla_host_t *ha;
1725         struct device_reg_24xx __iomem *reg;
1726
1727         ha = dev_id;
1728         reg = &ha->iobase->isp24;
1729
1730         spin_lock_irq(&ha->hardware_lock);
1731
1732         qla24xx_process_response_queue(ha);
1733         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1734
1735         spin_unlock_irq(&ha->hardware_lock);
1736
1737         return IRQ_HANDLED;
1738 }
1739
1740 static irqreturn_t
1741 qla24xx_msix_default(int irq, void *dev_id)
1742 {
1743         scsi_qla_host_t *ha;
1744         struct device_reg_24xx __iomem *reg;
1745         int             status;
1746         uint32_t        stat;
1747         uint32_t        hccr;
1748         uint16_t        mb[4];
1749
1750         ha = dev_id;
1751         reg = &ha->iobase->isp24;
1752         status = 0;
1753
1754         spin_lock_irq(&ha->hardware_lock);
1755         do {
1756                 stat = RD_REG_DWORD(&reg->host_status);
1757                 if (stat & HSRX_RISC_PAUSED) {
1758                         if (pci_channel_offline(ha->pdev))
1759                                 break;
1760
1761                         if (ha->hw_event_pause_errors == 0)
1762                                 qla2x00_post_hwe_work(ha, HW_EVENT_PARITY_ERR,
1763                                     0, MSW(stat), LSW(stat));
1764                         else if (ha->hw_event_pause_errors < 0xffffffff)
1765                                 ha->hw_event_pause_errors++;
1766
1767                         hccr = RD_REG_DWORD(&reg->hccr);
1768
1769                         qla_printk(KERN_INFO, ha, "RISC paused -- HCCR=%x, "
1770                             "Dumping firmware!\n", hccr);
1771
1772                         qla2xxx_check_risc_status(ha);
1773
1774                         ha->isp_ops->fw_dump(ha, 1);
1775                         set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1776                         break;
1777                 } else if ((stat & HSRX_RISC_INT) == 0)
1778                         break;
1779
1780                 switch (stat & 0xff) {
1781                 case 0x1:
1782                 case 0x2:
1783                 case 0x10:
1784                 case 0x11:
1785                         qla24xx_mbx_completion(ha, MSW(stat));
1786                         status |= MBX_INTERRUPT;
1787
1788                         break;
1789                 case 0x12:
1790                         mb[0] = MSW(stat);
1791                         mb[1] = RD_REG_WORD(&reg->mailbox1);
1792                         mb[2] = RD_REG_WORD(&reg->mailbox2);
1793                         mb[3] = RD_REG_WORD(&reg->mailbox3);
1794                         qla2x00_async_event(ha, mb);
1795                         break;
1796                 case 0x13:
1797                         qla24xx_process_response_queue(ha);
1798                         break;
1799                 default:
1800                         DEBUG2(printk("scsi(%ld): Unrecognized interrupt type "
1801                             "(%d).\n",
1802                             ha->host_no, stat & 0xff));
1803                         break;
1804                 }
1805                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
1806         } while (0);
1807         spin_unlock_irq(&ha->hardware_lock);
1808
1809         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags) &&
1810             (status & MBX_INTERRUPT) && ha->flags.mbox_int) {
1811                 set_bit(MBX_INTERRUPT, &ha->mbx_cmd_flags);
1812                 complete(&ha->mbx_intr_comp);
1813         }
1814
1815         return IRQ_HANDLED;
1816 }
1817
1818 /* Interrupt handling helpers. */
1819
1820 struct qla_init_msix_entry {
1821         uint16_t entry;
1822         uint16_t index;
1823         const char *name;
1824         irq_handler_t handler;
1825 };
1826
1827 static struct qla_init_msix_entry imsix_entries[QLA_MSIX_ENTRIES] = {
1828         { QLA_MSIX_DEFAULT, QLA_MIDX_DEFAULT,
1829                 "qla2xxx (default)", qla24xx_msix_default },
1830
1831         { QLA_MSIX_RSP_Q, QLA_MIDX_RSP_Q,
1832                 "qla2xxx (rsp_q)", qla24xx_msix_rsp_q },
1833 };
1834
1835 static void
1836 qla24xx_disable_msix(scsi_qla_host_t *ha)
1837 {
1838         int i;
1839         struct qla_msix_entry *qentry;
1840
1841         for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
1842                 qentry = &ha->msix_entries[imsix_entries[i].index];
1843                 if (qentry->have_irq)
1844                         free_irq(qentry->msix_vector, ha);
1845         }
1846         pci_disable_msix(ha->pdev);
1847 }
1848
1849 static int
1850 qla24xx_enable_msix(scsi_qla_host_t *ha)
1851 {
1852         int i, ret;
1853         struct msix_entry entries[QLA_MSIX_ENTRIES];
1854         struct qla_msix_entry *qentry;
1855
1856         for (i = 0; i < QLA_MSIX_ENTRIES; i++)
1857                 entries[i].entry = imsix_entries[i].entry;
1858
1859         ret = pci_enable_msix(ha->pdev, entries, ARRAY_SIZE(entries));
1860         if (ret) {
1861                 qla_printk(KERN_WARNING, ha,
1862                     "MSI-X: Failed to enable support -- %d/%d\n",
1863                     QLA_MSIX_ENTRIES, ret);
1864                 goto msix_out;
1865         }
1866         ha->flags.msix_enabled = 1;
1867
1868         for (i = 0; i < QLA_MSIX_ENTRIES; i++) {
1869                 qentry = &ha->msix_entries[imsix_entries[i].index];
1870                 qentry->msix_vector = entries[i].vector;
1871                 qentry->msix_entry = entries[i].entry;
1872                 qentry->have_irq = 0;
1873                 ret = request_irq(qentry->msix_vector,
1874                     imsix_entries[i].handler, 0, imsix_entries[i].name, ha);
1875                 if (ret) {
1876                         qla_printk(KERN_WARNING, ha,
1877                             "MSI-X: Unable to register handler -- %x/%d.\n",
1878                             imsix_entries[i].index, ret);
1879                         qla24xx_disable_msix(ha);
1880                         goto msix_out;
1881                 }
1882                 qentry->have_irq = 1;
1883         }
1884
1885 msix_out:
1886         return ret;
1887 }
1888
1889 int
1890 qla2x00_request_irqs(scsi_qla_host_t *ha)
1891 {
1892         int ret;
1893         device_reg_t __iomem *reg = ha->iobase;
1894
1895         /* If possible, enable MSI-X. */
1896         if (!IS_QLA2432(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha))
1897                 goto skip_msix;
1898
1899         if (IS_QLA2432(ha) && (ha->chip_revision < QLA_MSIX_CHIP_REV_24XX ||
1900             !QLA_MSIX_FW_MODE_1(ha->fw_attributes))) {
1901                 DEBUG2(qla_printk(KERN_WARNING, ha,
1902                     "MSI-X: Unsupported ISP2432 (0x%X, 0x%X).\n",
1903                     ha->chip_revision, ha->fw_attributes));
1904
1905                 goto skip_msix;
1906         }
1907
1908         if (ha->pdev->subsystem_vendor == PCI_VENDOR_ID_HP &&
1909             (ha->pdev->subsystem_device == 0x7040 ||
1910                 ha->pdev->subsystem_device == 0x7041 ||
1911                 ha->pdev->subsystem_device == 0x1705)) {
1912                 DEBUG2(qla_printk(KERN_WARNING, ha,
1913                     "MSI-X: Unsupported ISP2432 SSVID/SSDID (0x%X, 0x%X).\n",
1914                     ha->pdev->subsystem_vendor,
1915                     ha->pdev->subsystem_device));
1916
1917                 goto skip_msi;
1918         }
1919
1920         ret = qla24xx_enable_msix(ha);
1921         if (!ret) {
1922                 DEBUG2(qla_printk(KERN_INFO, ha,
1923                     "MSI-X: Enabled (0x%X, 0x%X).\n", ha->chip_revision,
1924                     ha->fw_attributes));
1925                 goto clear_risc_ints;
1926         }
1927         qla_printk(KERN_WARNING, ha,
1928             "MSI-X: Falling back-to INTa mode -- %d.\n", ret);
1929 skip_msix:
1930
1931         if (!IS_QLA24XX(ha) && !IS_QLA2532(ha) && !IS_QLA8432(ha))
1932                 goto skip_msi;
1933
1934         ret = pci_enable_msi(ha->pdev);
1935         if (!ret) {
1936                 DEBUG2(qla_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
1937                 ha->flags.msi_enabled = 1;
1938         }
1939 skip_msi:
1940
1941         ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
1942             IRQF_DISABLED|IRQF_SHARED, QLA2XXX_DRIVER_NAME, ha);
1943         if (ret) {
1944                 qla_printk(KERN_WARNING, ha,
1945                     "Failed to reserve interrupt %d already in use.\n",
1946                     ha->pdev->irq);
1947                 goto fail;
1948         }
1949         ha->flags.inta_enabled = 1;
1950         ha->host->irq = ha->pdev->irq;
1951 clear_risc_ints:
1952
1953         ha->isp_ops->disable_intrs(ha);
1954         spin_lock_irq(&ha->hardware_lock);
1955         if (IS_FWI2_CAPABLE(ha)) {
1956                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
1957                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
1958         } else {
1959                 WRT_REG_WORD(&reg->isp.semaphore, 0);
1960                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
1961                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
1962         }
1963         spin_unlock_irq(&ha->hardware_lock);
1964         ha->isp_ops->enable_intrs(ha);
1965
1966 fail:
1967         return ret;
1968 }
1969
1970 void
1971 qla2x00_free_irqs(scsi_qla_host_t *ha)
1972 {
1973
1974         if (ha->flags.msix_enabled)
1975                 qla24xx_disable_msix(ha);
1976         else if (ha->flags.inta_enabled) {
1977                 free_irq(ha->host->irq, ha);
1978                 pci_disable_msi(ha->pdev);
1979         }
1980 }