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