Merge tag 'dm-4.11-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/device...
[sfrench/cifs-2.6.git] / drivers / scsi / qla2xxx / qla_isr.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_target.h"
9
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/t10-pi.h>
13 #include <scsi/scsi_tcq.h>
14 #include <scsi/scsi_bsg_fc.h>
15 #include <scsi/scsi_eh.h>
16
17 static void qla2x00_mbx_completion(scsi_qla_host_t *, uint16_t);
18 static void qla2x00_status_entry(scsi_qla_host_t *, struct rsp_que *, void *);
19 static void qla2x00_status_cont_entry(struct rsp_que *, sts_cont_entry_t *);
20 static void qla2x00_error_entry(scsi_qla_host_t *, struct rsp_que *,
21         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 *vha;
36         struct qla_hw_data *ha;
37         struct device_reg_2xxx __iomem *reg;
38         int             status;
39         unsigned long   iter;
40         uint16_t        hccr;
41         uint16_t        mb[4];
42         struct rsp_que *rsp;
43         unsigned long   flags;
44
45         rsp = (struct rsp_que *) dev_id;
46         if (!rsp) {
47                 ql_log(ql_log_info, NULL, 0x505d,
48                     "%s: NULL response queue pointer.\n", __func__);
49                 return (IRQ_NONE);
50         }
51
52         ha = rsp->hw;
53         reg = &ha->iobase->isp;
54         status = 0;
55
56         spin_lock_irqsave(&ha->hardware_lock, flags);
57         vha = pci_get_drvdata(ha->pdev);
58         for (iter = 50; iter--; ) {
59                 hccr = RD_REG_WORD(&reg->hccr);
60                 if (qla2x00_check_reg16_for_disconnect(vha, hccr))
61                         break;
62                 if (hccr & HCCR_RISC_PAUSE) {
63                         if (pci_channel_offline(ha->pdev))
64                                 break;
65
66                         /*
67                          * Issue a "HARD" reset in order for the RISC interrupt
68                          * bit to be cleared.  Schedule a big hammer to get
69                          * out of the RISC PAUSED state.
70                          */
71                         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
72                         RD_REG_WORD(&reg->hccr);
73
74                         ha->isp_ops->fw_dump(vha, 1);
75                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
76                         break;
77                 } else if ((RD_REG_WORD(&reg->istatus) & ISR_RISC_INT) == 0)
78                         break;
79
80                 if (RD_REG_WORD(&reg->semaphore) & BIT_0) {
81                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
82                         RD_REG_WORD(&reg->hccr);
83
84                         /* Get mailbox data. */
85                         mb[0] = RD_MAILBOX_REG(ha, reg, 0);
86                         if (mb[0] > 0x3fff && mb[0] < 0x8000) {
87                                 qla2x00_mbx_completion(vha, mb[0]);
88                                 status |= MBX_INTERRUPT;
89                         } else if (mb[0] > 0x7fff && mb[0] < 0xc000) {
90                                 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
91                                 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
92                                 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
93                                 qla2x00_async_event(vha, rsp, mb);
94                         } else {
95                                 /*EMPTY*/
96                                 ql_dbg(ql_dbg_async, vha, 0x5025,
97                                     "Unrecognized interrupt type (%d).\n",
98                                     mb[0]);
99                         }
100                         /* Release mailbox registers. */
101                         WRT_REG_WORD(&reg->semaphore, 0);
102                         RD_REG_WORD(&reg->semaphore);
103                 } else {
104                         qla2x00_process_response_queue(rsp);
105
106                         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
107                         RD_REG_WORD(&reg->hccr);
108                 }
109         }
110         qla2x00_handle_mbx_completion(ha, status);
111         spin_unlock_irqrestore(&ha->hardware_lock, flags);
112
113         return (IRQ_HANDLED);
114 }
115
116 bool
117 qla2x00_check_reg32_for_disconnect(scsi_qla_host_t *vha, uint32_t reg)
118 {
119         /* Check for PCI disconnection */
120         if (reg == 0xffffffff && !pci_channel_offline(vha->hw->pdev)) {
121                 if (!test_and_set_bit(PFLG_DISCONNECTED, &vha->pci_flags) &&
122                     !test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags) &&
123                     !test_bit(PFLG_DRIVER_PROBING, &vha->pci_flags)) {
124                         /*
125                          * Schedule this (only once) on the default system
126                          * workqueue so that all the adapter workqueues and the
127                          * DPC thread can be shutdown cleanly.
128                          */
129                         schedule_work(&vha->hw->board_disable);
130                 }
131                 return true;
132         } else
133                 return false;
134 }
135
136 bool
137 qla2x00_check_reg16_for_disconnect(scsi_qla_host_t *vha, uint16_t reg)
138 {
139         return qla2x00_check_reg32_for_disconnect(vha, 0xffff0000 | reg);
140 }
141
142 /**
143  * qla2300_intr_handler() - Process interrupts for the ISP23xx and ISP63xx.
144  * @irq:
145  * @dev_id: SCSI driver HA context
146  *
147  * Called by system whenever the host adapter generates an interrupt.
148  *
149  * Returns handled flag.
150  */
151 irqreturn_t
152 qla2300_intr_handler(int irq, void *dev_id)
153 {
154         scsi_qla_host_t *vha;
155         struct device_reg_2xxx __iomem *reg;
156         int             status;
157         unsigned long   iter;
158         uint32_t        stat;
159         uint16_t        hccr;
160         uint16_t        mb[4];
161         struct rsp_que *rsp;
162         struct qla_hw_data *ha;
163         unsigned long   flags;
164
165         rsp = (struct rsp_que *) dev_id;
166         if (!rsp) {
167                 ql_log(ql_log_info, NULL, 0x5058,
168                     "%s: NULL response queue pointer.\n", __func__);
169                 return (IRQ_NONE);
170         }
171
172         ha = rsp->hw;
173         reg = &ha->iobase->isp;
174         status = 0;
175
176         spin_lock_irqsave(&ha->hardware_lock, flags);
177         vha = pci_get_drvdata(ha->pdev);
178         for (iter = 50; iter--; ) {
179                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
180                 if (qla2x00_check_reg32_for_disconnect(vha, stat))
181                         break;
182                 if (stat & HSR_RISC_PAUSED) {
183                         if (unlikely(pci_channel_offline(ha->pdev)))
184                                 break;
185
186                         hccr = RD_REG_WORD(&reg->hccr);
187
188                         if (hccr & (BIT_15 | BIT_13 | BIT_11 | BIT_8))
189                                 ql_log(ql_log_warn, vha, 0x5026,
190                                     "Parity error -- HCCR=%x, Dumping "
191                                     "firmware.\n", hccr);
192                         else
193                                 ql_log(ql_log_warn, vha, 0x5027,
194                                     "RISC paused -- HCCR=%x, Dumping "
195                                     "firmware.\n", hccr);
196
197                         /*
198                          * Issue a "HARD" reset in order for the RISC
199                          * interrupt bit to be cleared.  Schedule a big
200                          * hammer to get out of the RISC PAUSED state.
201                          */
202                         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
203                         RD_REG_WORD(&reg->hccr);
204
205                         ha->isp_ops->fw_dump(vha, 1);
206                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
207                         break;
208                 } else if ((stat & HSR_RISC_INT) == 0)
209                         break;
210
211                 switch (stat & 0xff) {
212                 case 0x1:
213                 case 0x2:
214                 case 0x10:
215                 case 0x11:
216                         qla2x00_mbx_completion(vha, MSW(stat));
217                         status |= MBX_INTERRUPT;
218
219                         /* Release mailbox registers. */
220                         WRT_REG_WORD(&reg->semaphore, 0);
221                         break;
222                 case 0x12:
223                         mb[0] = MSW(stat);
224                         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
225                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
226                         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
227                         qla2x00_async_event(vha, rsp, mb);
228                         break;
229                 case 0x13:
230                         qla2x00_process_response_queue(rsp);
231                         break;
232                 case 0x15:
233                         mb[0] = MBA_CMPLT_1_16BIT;
234                         mb[1] = MSW(stat);
235                         qla2x00_async_event(vha, rsp, mb);
236                         break;
237                 case 0x16:
238                         mb[0] = MBA_SCSI_COMPLETION;
239                         mb[1] = MSW(stat);
240                         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
241                         qla2x00_async_event(vha, rsp, mb);
242                         break;
243                 default:
244                         ql_dbg(ql_dbg_async, vha, 0x5028,
245                             "Unrecognized interrupt type (%d).\n", stat & 0xff);
246                         break;
247                 }
248                 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
249                 RD_REG_WORD_RELAXED(&reg->hccr);
250         }
251         qla2x00_handle_mbx_completion(ha, status);
252         spin_unlock_irqrestore(&ha->hardware_lock, flags);
253
254         return (IRQ_HANDLED);
255 }
256
257 /**
258  * qla2x00_mbx_completion() - Process mailbox command completions.
259  * @ha: SCSI driver HA context
260  * @mb0: Mailbox0 register
261  */
262 static void
263 qla2x00_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
264 {
265         uint16_t        cnt;
266         uint32_t        mboxes;
267         uint16_t __iomem *wptr;
268         struct qla_hw_data *ha = vha->hw;
269         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
270
271         /* Read all mbox registers? */
272         mboxes = (1 << ha->mbx_count) - 1;
273         if (!ha->mcp)
274                 ql_dbg(ql_dbg_async, vha, 0x5001, "MBX pointer ERROR.\n");
275         else
276                 mboxes = ha->mcp->in_mb;
277
278         /* Load return mailbox registers. */
279         ha->flags.mbox_int = 1;
280         ha->mailbox_out[0] = mb0;
281         mboxes >>= 1;
282         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 1);
283
284         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
285                 if (IS_QLA2200(ha) && cnt == 8)
286                         wptr = (uint16_t __iomem *)MAILBOX_REG(ha, reg, 8);
287                 if ((cnt == 4 || cnt == 5) && (mboxes & BIT_0))
288                         ha->mailbox_out[cnt] = qla2x00_debounce_register(wptr);
289                 else if (mboxes & BIT_0)
290                         ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
291
292                 wptr++;
293                 mboxes >>= 1;
294         }
295 }
296
297 static void
298 qla81xx_idc_event(scsi_qla_host_t *vha, uint16_t aen, uint16_t descr)
299 {
300         static char *event[] =
301                 { "Complete", "Request Notification", "Time Extension" };
302         int rval;
303         struct device_reg_24xx __iomem *reg24 = &vha->hw->iobase->isp24;
304         struct device_reg_82xx __iomem *reg82 = &vha->hw->iobase->isp82;
305         uint16_t __iomem *wptr;
306         uint16_t cnt, timeout, mb[QLA_IDC_ACK_REGS];
307
308         /* Seed data -- mailbox1 -> mailbox7. */
309         if (IS_QLA81XX(vha->hw) || IS_QLA83XX(vha->hw))
310                 wptr = (uint16_t __iomem *)&reg24->mailbox1;
311         else if (IS_QLA8044(vha->hw))
312                 wptr = (uint16_t __iomem *)&reg82->mailbox_out[1];
313         else
314                 return;
315
316         for (cnt = 0; cnt < QLA_IDC_ACK_REGS; cnt++, wptr++)
317                 mb[cnt] = RD_REG_WORD(wptr);
318
319         ql_dbg(ql_dbg_async, vha, 0x5021,
320             "Inter-Driver Communication %s -- "
321             "%04x %04x %04x %04x %04x %04x %04x.\n",
322             event[aen & 0xff], mb[0], mb[1], mb[2], mb[3],
323             mb[4], mb[5], mb[6]);
324         switch (aen) {
325         /* Handle IDC Error completion case. */
326         case MBA_IDC_COMPLETE:
327                 if (mb[1] >> 15) {
328                         vha->hw->flags.idc_compl_status = 1;
329                         if (vha->hw->notify_dcbx_comp && !vha->vp_idx)
330                                 complete(&vha->hw->dcbx_comp);
331                 }
332                 break;
333
334         case MBA_IDC_NOTIFY:
335                 /* Acknowledgement needed? [Notify && non-zero timeout]. */
336                 timeout = (descr >> 8) & 0xf;
337                 ql_dbg(ql_dbg_async, vha, 0x5022,
338                     "%lu Inter-Driver Communication %s -- ACK timeout=%d.\n",
339                     vha->host_no, event[aen & 0xff], timeout);
340
341                 if (!timeout)
342                         return;
343                 rval = qla2x00_post_idc_ack_work(vha, mb);
344                 if (rval != QLA_SUCCESS)
345                         ql_log(ql_log_warn, vha, 0x5023,
346                             "IDC failed to post ACK.\n");
347                 break;
348         case MBA_IDC_TIME_EXT:
349                 vha->hw->idc_extend_tmo = descr;
350                 ql_dbg(ql_dbg_async, vha, 0x5087,
351                     "%lu Inter-Driver Communication %s -- "
352                     "Extend timeout by=%d.\n",
353                     vha->host_no, event[aen & 0xff], vha->hw->idc_extend_tmo);
354                 break;
355         }
356 }
357
358 #define LS_UNKNOWN      2
359 const char *
360 qla2x00_get_link_speed_str(struct qla_hw_data *ha, uint16_t speed)
361 {
362         static const char *const link_speeds[] = {
363                 "1", "2", "?", "4", "8", "16", "32", "10"
364         };
365 #define QLA_LAST_SPEED  7
366
367         if (IS_QLA2100(ha) || IS_QLA2200(ha))
368                 return link_speeds[0];
369         else if (speed == 0x13)
370                 return link_speeds[QLA_LAST_SPEED];
371         else if (speed < QLA_LAST_SPEED)
372                 return link_speeds[speed];
373         else
374                 return link_speeds[LS_UNKNOWN];
375 }
376
377 static void
378 qla83xx_handle_8200_aen(scsi_qla_host_t *vha, uint16_t *mb)
379 {
380         struct qla_hw_data *ha = vha->hw;
381
382         /*
383          * 8200 AEN Interpretation:
384          * mb[0] = AEN code
385          * mb[1] = AEN Reason code
386          * mb[2] = LSW of Peg-Halt Status-1 Register
387          * mb[6] = MSW of Peg-Halt Status-1 Register
388          * mb[3] = LSW of Peg-Halt Status-2 register
389          * mb[7] = MSW of Peg-Halt Status-2 register
390          * mb[4] = IDC Device-State Register value
391          * mb[5] = IDC Driver-Presence Register value
392          */
393         ql_dbg(ql_dbg_async, vha, 0x506b, "AEN Code: mb[0] = 0x%x AEN reason: "
394             "mb[1] = 0x%x PH-status1: mb[2] = 0x%x PH-status1: mb[6] = 0x%x.\n",
395             mb[0], mb[1], mb[2], mb[6]);
396         ql_dbg(ql_dbg_async, vha, 0x506c, "PH-status2: mb[3] = 0x%x "
397             "PH-status2: mb[7] = 0x%x Device-State: mb[4] = 0x%x "
398             "Drv-Presence: mb[5] = 0x%x.\n", mb[3], mb[7], mb[4], mb[5]);
399
400         if (mb[1] & (IDC_PEG_HALT_STATUS_CHANGE | IDC_NIC_FW_REPORTED_FAILURE |
401                                 IDC_HEARTBEAT_FAILURE)) {
402                 ha->flags.nic_core_hung = 1;
403                 ql_log(ql_log_warn, vha, 0x5060,
404                     "83XX: F/W Error Reported: Check if reset required.\n");
405
406                 if (mb[1] & IDC_PEG_HALT_STATUS_CHANGE) {
407                         uint32_t protocol_engine_id, fw_err_code, err_level;
408
409                         /*
410                          * IDC_PEG_HALT_STATUS_CHANGE interpretation:
411                          *  - PEG-Halt Status-1 Register:
412                          *      (LSW = mb[2], MSW = mb[6])
413                          *      Bits 0-7   = protocol-engine ID
414                          *      Bits 8-28  = f/w error code
415                          *      Bits 29-31 = Error-level
416                          *          Error-level 0x1 = Non-Fatal error
417                          *          Error-level 0x2 = Recoverable Fatal error
418                          *          Error-level 0x4 = UnRecoverable Fatal error
419                          *  - PEG-Halt Status-2 Register:
420                          *      (LSW = mb[3], MSW = mb[7])
421                          */
422                         protocol_engine_id = (mb[2] & 0xff);
423                         fw_err_code = (((mb[2] & 0xff00) >> 8) |
424                             ((mb[6] & 0x1fff) << 8));
425                         err_level = ((mb[6] & 0xe000) >> 13);
426                         ql_log(ql_log_warn, vha, 0x5061, "PegHalt Status-1 "
427                             "Register: protocol_engine_id=0x%x "
428                             "fw_err_code=0x%x err_level=0x%x.\n",
429                             protocol_engine_id, fw_err_code, err_level);
430                         ql_log(ql_log_warn, vha, 0x5062, "PegHalt Status-2 "
431                             "Register: 0x%x%x.\n", mb[7], mb[3]);
432                         if (err_level == ERR_LEVEL_NON_FATAL) {
433                                 ql_log(ql_log_warn, vha, 0x5063,
434                                     "Not a fatal error, f/w has recovered "
435                                     "iteself.\n");
436                         } else if (err_level == ERR_LEVEL_RECOVERABLE_FATAL) {
437                                 ql_log(ql_log_fatal, vha, 0x5064,
438                                     "Recoverable Fatal error: Chip reset "
439                                     "required.\n");
440                                 qla83xx_schedule_work(vha,
441                                     QLA83XX_NIC_CORE_RESET);
442                         } else if (err_level == ERR_LEVEL_UNRECOVERABLE_FATAL) {
443                                 ql_log(ql_log_fatal, vha, 0x5065,
444                                     "Unrecoverable Fatal error: Set FAILED "
445                                     "state, reboot required.\n");
446                                 qla83xx_schedule_work(vha,
447                                     QLA83XX_NIC_CORE_UNRECOVERABLE);
448                         }
449                 }
450
451                 if (mb[1] & IDC_NIC_FW_REPORTED_FAILURE) {
452                         uint16_t peg_fw_state, nw_interface_link_up;
453                         uint16_t nw_interface_signal_detect, sfp_status;
454                         uint16_t htbt_counter, htbt_monitor_enable;
455                         uint16_t sfp_additonal_info, sfp_multirate;
456                         uint16_t sfp_tx_fault, link_speed, dcbx_status;
457
458                         /*
459                          * IDC_NIC_FW_REPORTED_FAILURE interpretation:
460                          *  - PEG-to-FC Status Register:
461                          *      (LSW = mb[2], MSW = mb[6])
462                          *      Bits 0-7   = Peg-Firmware state
463                          *      Bit 8      = N/W Interface Link-up
464                          *      Bit 9      = N/W Interface signal detected
465                          *      Bits 10-11 = SFP Status
466                          *        SFP Status 0x0 = SFP+ transceiver not expected
467                          *        SFP Status 0x1 = SFP+ transceiver not present
468                          *        SFP Status 0x2 = SFP+ transceiver invalid
469                          *        SFP Status 0x3 = SFP+ transceiver present and
470                          *        valid
471                          *      Bits 12-14 = Heartbeat Counter
472                          *      Bit 15     = Heartbeat Monitor Enable
473                          *      Bits 16-17 = SFP Additional Info
474                          *        SFP info 0x0 = Unregocnized transceiver for
475                          *        Ethernet
476                          *        SFP info 0x1 = SFP+ brand validation failed
477                          *        SFP info 0x2 = SFP+ speed validation failed
478                          *        SFP info 0x3 = SFP+ access error
479                          *      Bit 18     = SFP Multirate
480                          *      Bit 19     = SFP Tx Fault
481                          *      Bits 20-22 = Link Speed
482                          *      Bits 23-27 = Reserved
483                          *      Bits 28-30 = DCBX Status
484                          *        DCBX Status 0x0 = DCBX Disabled
485                          *        DCBX Status 0x1 = DCBX Enabled
486                          *        DCBX Status 0x2 = DCBX Exchange error
487                          *      Bit 31     = Reserved
488                          */
489                         peg_fw_state = (mb[2] & 0x00ff);
490                         nw_interface_link_up = ((mb[2] & 0x0100) >> 8);
491                         nw_interface_signal_detect = ((mb[2] & 0x0200) >> 9);
492                         sfp_status = ((mb[2] & 0x0c00) >> 10);
493                         htbt_counter = ((mb[2] & 0x7000) >> 12);
494                         htbt_monitor_enable = ((mb[2] & 0x8000) >> 15);
495                         sfp_additonal_info = (mb[6] & 0x0003);
496                         sfp_multirate = ((mb[6] & 0x0004) >> 2);
497                         sfp_tx_fault = ((mb[6] & 0x0008) >> 3);
498                         link_speed = ((mb[6] & 0x0070) >> 4);
499                         dcbx_status = ((mb[6] & 0x7000) >> 12);
500
501                         ql_log(ql_log_warn, vha, 0x5066,
502                             "Peg-to-Fc Status Register:\n"
503                             "peg_fw_state=0x%x, nw_interface_link_up=0x%x, "
504                             "nw_interface_signal_detect=0x%x"
505                             "\nsfp_statis=0x%x.\n ", peg_fw_state,
506                             nw_interface_link_up, nw_interface_signal_detect,
507                             sfp_status);
508                         ql_log(ql_log_warn, vha, 0x5067,
509                             "htbt_counter=0x%x, htbt_monitor_enable=0x%x, "
510                             "sfp_additonal_info=0x%x, sfp_multirate=0x%x.\n ",
511                             htbt_counter, htbt_monitor_enable,
512                             sfp_additonal_info, sfp_multirate);
513                         ql_log(ql_log_warn, vha, 0x5068,
514                             "sfp_tx_fault=0x%x, link_state=0x%x, "
515                             "dcbx_status=0x%x.\n", sfp_tx_fault, link_speed,
516                             dcbx_status);
517
518                         qla83xx_schedule_work(vha, QLA83XX_NIC_CORE_RESET);
519                 }
520
521                 if (mb[1] & IDC_HEARTBEAT_FAILURE) {
522                         ql_log(ql_log_warn, vha, 0x5069,
523                             "Heartbeat Failure encountered, chip reset "
524                             "required.\n");
525
526                         qla83xx_schedule_work(vha, QLA83XX_NIC_CORE_RESET);
527                 }
528         }
529
530         if (mb[1] & IDC_DEVICE_STATE_CHANGE) {
531                 ql_log(ql_log_info, vha, 0x506a,
532                     "IDC Device-State changed = 0x%x.\n", mb[4]);
533                 if (ha->flags.nic_core_reset_owner)
534                         return;
535                 qla83xx_schedule_work(vha, MBA_IDC_AEN);
536         }
537 }
538
539 int
540 qla2x00_is_a_vp_did(scsi_qla_host_t *vha, uint32_t rscn_entry)
541 {
542         struct qla_hw_data *ha = vha->hw;
543         scsi_qla_host_t *vp;
544         uint32_t vp_did;
545         unsigned long flags;
546         int ret = 0;
547
548         if (!ha->num_vhosts)
549                 return ret;
550
551         spin_lock_irqsave(&ha->vport_slock, flags);
552         list_for_each_entry(vp, &ha->vp_list, list) {
553                 vp_did = vp->d_id.b24;
554                 if (vp_did == rscn_entry) {
555                         ret = 1;
556                         break;
557                 }
558         }
559         spin_unlock_irqrestore(&ha->vport_slock, flags);
560
561         return ret;
562 }
563
564 static inline fc_port_t *
565 qla2x00_find_fcport_by_loopid(scsi_qla_host_t *vha, uint16_t loop_id)
566 {
567         fc_port_t *fcport;
568
569         list_for_each_entry(fcport, &vha->vp_fcports, list)
570                 if (fcport->loop_id == loop_id)
571                         return fcport;
572         return NULL;
573 }
574
575 /**
576  * qla2x00_async_event() - Process aynchronous events.
577  * @ha: SCSI driver HA context
578  * @mb: Mailbox registers (0 - 3)
579  */
580 void
581 qla2x00_async_event(scsi_qla_host_t *vha, struct rsp_que *rsp, uint16_t *mb)
582 {
583         uint16_t        handle_cnt;
584         uint16_t        cnt, mbx;
585         uint32_t        handles[5];
586         struct qla_hw_data *ha = vha->hw;
587         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
588         struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
589         struct device_reg_82xx __iomem *reg82 = &ha->iobase->isp82;
590         uint32_t        rscn_entry, host_pid;
591         unsigned long   flags;
592         fc_port_t       *fcport = NULL;
593
594         /* Setup to process RIO completion. */
595         handle_cnt = 0;
596         if (IS_CNA_CAPABLE(ha))
597                 goto skip_rio;
598         switch (mb[0]) {
599         case MBA_SCSI_COMPLETION:
600                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
601                 handle_cnt = 1;
602                 break;
603         case MBA_CMPLT_1_16BIT:
604                 handles[0] = mb[1];
605                 handle_cnt = 1;
606                 mb[0] = MBA_SCSI_COMPLETION;
607                 break;
608         case MBA_CMPLT_2_16BIT:
609                 handles[0] = mb[1];
610                 handles[1] = mb[2];
611                 handle_cnt = 2;
612                 mb[0] = MBA_SCSI_COMPLETION;
613                 break;
614         case MBA_CMPLT_3_16BIT:
615                 handles[0] = mb[1];
616                 handles[1] = mb[2];
617                 handles[2] = mb[3];
618                 handle_cnt = 3;
619                 mb[0] = MBA_SCSI_COMPLETION;
620                 break;
621         case MBA_CMPLT_4_16BIT:
622                 handles[0] = mb[1];
623                 handles[1] = mb[2];
624                 handles[2] = mb[3];
625                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
626                 handle_cnt = 4;
627                 mb[0] = MBA_SCSI_COMPLETION;
628                 break;
629         case MBA_CMPLT_5_16BIT:
630                 handles[0] = mb[1];
631                 handles[1] = mb[2];
632                 handles[2] = mb[3];
633                 handles[3] = (uint32_t)RD_MAILBOX_REG(ha, reg, 6);
634                 handles[4] = (uint32_t)RD_MAILBOX_REG(ha, reg, 7);
635                 handle_cnt = 5;
636                 mb[0] = MBA_SCSI_COMPLETION;
637                 break;
638         case MBA_CMPLT_2_32BIT:
639                 handles[0] = le32_to_cpu((uint32_t)((mb[2] << 16) | mb[1]));
640                 handles[1] = le32_to_cpu(
641                     ((uint32_t)(RD_MAILBOX_REG(ha, reg, 7) << 16)) |
642                     RD_MAILBOX_REG(ha, reg, 6));
643                 handle_cnt = 2;
644                 mb[0] = MBA_SCSI_COMPLETION;
645                 break;
646         default:
647                 break;
648         }
649 skip_rio:
650         switch (mb[0]) {
651         case MBA_SCSI_COMPLETION:       /* Fast Post */
652                 if (!vha->flags.online)
653                         break;
654
655                 for (cnt = 0; cnt < handle_cnt; cnt++)
656                         qla2x00_process_completed_request(vha, rsp->req,
657                                 handles[cnt]);
658                 break;
659
660         case MBA_RESET:                 /* Reset */
661                 ql_dbg(ql_dbg_async, vha, 0x5002,
662                     "Asynchronous RESET.\n");
663
664                 set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
665                 break;
666
667         case MBA_SYSTEM_ERR:            /* System Error */
668                 mbx = (IS_QLA81XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) ?
669                         RD_REG_WORD(&reg24->mailbox7) : 0;
670                 ql_log(ql_log_warn, vha, 0x5003,
671                     "ISP System Error - mbx1=%xh mbx2=%xh mbx3=%xh "
672                     "mbx7=%xh.\n", mb[1], mb[2], mb[3], mbx);
673
674                 ha->isp_ops->fw_dump(vha, 1);
675
676                 if (IS_FWI2_CAPABLE(ha)) {
677                         if (mb[1] == 0 && mb[2] == 0) {
678                                 ql_log(ql_log_fatal, vha, 0x5004,
679                                     "Unrecoverable Hardware Error: adapter "
680                                     "marked OFFLINE!\n");
681                                 vha->flags.online = 0;
682                                 vha->device_flags |= DFLG_DEV_FAILED;
683                         } else {
684                                 /* Check to see if MPI timeout occurred */
685                                 if ((mbx & MBX_3) && (ha->port_no == 0))
686                                         set_bit(MPI_RESET_NEEDED,
687                                             &vha->dpc_flags);
688
689                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
690                         }
691                 } else if (mb[1] == 0) {
692                         ql_log(ql_log_fatal, vha, 0x5005,
693                             "Unrecoverable Hardware Error: adapter marked "
694                             "OFFLINE!\n");
695                         vha->flags.online = 0;
696                         vha->device_flags |= DFLG_DEV_FAILED;
697                 } else
698                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
699                 break;
700
701         case MBA_REQ_TRANSFER_ERR:      /* Request Transfer Error */
702                 ql_log(ql_log_warn, vha, 0x5006,
703                     "ISP Request Transfer Error (%x).\n",  mb[1]);
704
705                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
706                 break;
707
708         case MBA_RSP_TRANSFER_ERR:      /* Response Transfer Error */
709                 ql_log(ql_log_warn, vha, 0x5007,
710                     "ISP Response Transfer Error (%x).\n", mb[1]);
711
712                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
713                 break;
714
715         case MBA_WAKEUP_THRES:          /* Request Queue Wake-up */
716                 ql_dbg(ql_dbg_async, vha, 0x5008,
717                     "Asynchronous WAKEUP_THRES (%x).\n", mb[1]);
718                 break;
719
720         case MBA_LOOP_INIT_ERR:
721                 ql_log(ql_log_warn, vha, 0x5090,
722                     "LOOP INIT ERROR (%x).\n", mb[1]);
723                 ha->isp_ops->fw_dump(vha, 1);
724                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
725                 break;
726
727         case MBA_LIP_OCCURRED:          /* Loop Initialization Procedure */
728                 ql_dbg(ql_dbg_async, vha, 0x5009,
729                     "LIP occurred (%x).\n", mb[1]);
730
731                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
732                         atomic_set(&vha->loop_state, LOOP_DOWN);
733                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
734                         qla2x00_mark_all_devices_lost(vha, 1);
735                 }
736
737                 if (vha->vp_idx) {
738                         atomic_set(&vha->vp_state, VP_FAILED);
739                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
740                 }
741
742                 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
743                 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
744
745                 vha->flags.management_server_logged_in = 0;
746                 qla2x00_post_aen_work(vha, FCH_EVT_LIP, mb[1]);
747                 break;
748
749         case MBA_LOOP_UP:               /* Loop Up Event */
750                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
751                         ha->link_data_rate = PORT_SPEED_1GB;
752                 else
753                         ha->link_data_rate = mb[1];
754
755                 ql_log(ql_log_info, vha, 0x500a,
756                     "LOOP UP detected (%s Gbps).\n",
757                     qla2x00_get_link_speed_str(ha, ha->link_data_rate));
758
759                 vha->flags.management_server_logged_in = 0;
760                 qla2x00_post_aen_work(vha, FCH_EVT_LINKUP, ha->link_data_rate);
761                 break;
762
763         case MBA_LOOP_DOWN:             /* Loop Down Event */
764                 mbx = (IS_QLA81XX(ha) || IS_QLA8031(ha))
765                         ? RD_REG_WORD(&reg24->mailbox4) : 0;
766                 mbx = (IS_P3P_TYPE(ha)) ? RD_REG_WORD(&reg82->mailbox_out[4])
767                         : mbx;
768                 ql_log(ql_log_info, vha, 0x500b,
769                     "LOOP DOWN detected (%x %x %x %x).\n",
770                     mb[1], mb[2], mb[3], mbx);
771
772                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
773                         atomic_set(&vha->loop_state, LOOP_DOWN);
774                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
775                         /*
776                          * In case of loop down, restore WWPN from
777                          * NVRAM in case of FA-WWPN capable ISP
778                          * Restore for Physical Port only
779                          */
780                         if (!vha->vp_idx) {
781                                 if (ha->flags.fawwpn_enabled) {
782                                         void *wwpn = ha->init_cb->port_name;
783                                         memcpy(vha->port_name, wwpn, WWN_SIZE);
784                                         fc_host_port_name(vha->host) =
785                                             wwn_to_u64(vha->port_name);
786                                         ql_dbg(ql_dbg_init + ql_dbg_verbose,
787                                             vha, 0x0144, "LOOP DOWN detected,"
788                                             "restore WWPN %016llx\n",
789                                             wwn_to_u64(vha->port_name));
790                                 }
791
792                                 clear_bit(VP_CONFIG_OK, &vha->vp_flags);
793                         }
794
795                         vha->device_flags |= DFLG_NO_CABLE;
796                         qla2x00_mark_all_devices_lost(vha, 1);
797                 }
798
799                 if (vha->vp_idx) {
800                         atomic_set(&vha->vp_state, VP_FAILED);
801                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
802                 }
803
804                 vha->flags.management_server_logged_in = 0;
805                 ha->link_data_rate = PORT_SPEED_UNKNOWN;
806                 qla2x00_post_aen_work(vha, FCH_EVT_LINKDOWN, 0);
807                 break;
808
809         case MBA_LIP_RESET:             /* LIP reset occurred */
810                 ql_dbg(ql_dbg_async, vha, 0x500c,
811                     "LIP reset occurred (%x).\n", mb[1]);
812
813                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
814                         atomic_set(&vha->loop_state, LOOP_DOWN);
815                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
816                         qla2x00_mark_all_devices_lost(vha, 1);
817                 }
818
819                 if (vha->vp_idx) {
820                         atomic_set(&vha->vp_state, VP_FAILED);
821                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
822                 }
823
824                 set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
825
826                 ha->operating_mode = LOOP;
827                 vha->flags.management_server_logged_in = 0;
828                 qla2x00_post_aen_work(vha, FCH_EVT_LIPRESET, mb[1]);
829                 break;
830
831         /* case MBA_DCBX_COMPLETE: */
832         case MBA_POINT_TO_POINT:        /* Point-to-Point */
833                 if (IS_QLA2100(ha))
834                         break;
835
836                 if (IS_CNA_CAPABLE(ha)) {
837                         ql_dbg(ql_dbg_async, vha, 0x500d,
838                             "DCBX Completed -- %04x %04x %04x.\n",
839                             mb[1], mb[2], mb[3]);
840                         if (ha->notify_dcbx_comp && !vha->vp_idx)
841                                 complete(&ha->dcbx_comp);
842
843                 } else
844                         ql_dbg(ql_dbg_async, vha, 0x500e,
845                             "Asynchronous P2P MODE received.\n");
846
847                 /*
848                  * Until there's a transition from loop down to loop up, treat
849                  * this as loop down only.
850                  */
851                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
852                         atomic_set(&vha->loop_state, LOOP_DOWN);
853                         if (!atomic_read(&vha->loop_down_timer))
854                                 atomic_set(&vha->loop_down_timer,
855                                     LOOP_DOWN_TIME);
856                         qla2x00_mark_all_devices_lost(vha, 1);
857                 }
858
859                 if (vha->vp_idx) {
860                         atomic_set(&vha->vp_state, VP_FAILED);
861                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
862                 }
863
864                 if (!(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)))
865                         set_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
866
867                 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
868                 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
869
870                 ha->flags.gpsc_supported = 1;
871                 vha->flags.management_server_logged_in = 0;
872                 break;
873
874         case MBA_CHG_IN_CONNECTION:     /* Change in connection mode */
875                 if (IS_QLA2100(ha))
876                         break;
877
878                 ql_dbg(ql_dbg_async, vha, 0x500f,
879                     "Configuration change detected: value=%x.\n", mb[1]);
880
881                 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
882                         atomic_set(&vha->loop_state, LOOP_DOWN);
883                         if (!atomic_read(&vha->loop_down_timer))
884                                 atomic_set(&vha->loop_down_timer,
885                                     LOOP_DOWN_TIME);
886                         qla2x00_mark_all_devices_lost(vha, 1);
887                 }
888
889                 if (vha->vp_idx) {
890                         atomic_set(&vha->vp_state, VP_FAILED);
891                         fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
892                 }
893
894                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
895                 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
896                 break;
897
898         case MBA_PORT_UPDATE:           /* Port database update */
899                 /*
900                  * Handle only global and vn-port update events
901                  *
902                  * Relevant inputs:
903                  * mb[1] = N_Port handle of changed port
904                  * OR 0xffff for global event
905                  * mb[2] = New login state
906                  * 7 = Port logged out
907                  * mb[3] = LSB is vp_idx, 0xff = all vps
908                  *
909                  * Skip processing if:
910                  *       Event is global, vp_idx is NOT all vps,
911                  *           vp_idx does not match
912                  *       Event is not global, vp_idx does not match
913                  */
914                 if (IS_QLA2XXX_MIDTYPE(ha) &&
915                     ((mb[1] == 0xffff && (mb[3] & 0xff) != 0xff) ||
916                         (mb[1] != 0xffff)) && vha->vp_idx != (mb[3] & 0xff))
917                         break;
918
919                 if (mb[2] == 0x7) {
920                         ql_dbg(ql_dbg_async, vha, 0x5010,
921                             "Port %s %04x %04x %04x.\n",
922                             mb[1] == 0xffff ? "unavailable" : "logout",
923                             mb[1], mb[2], mb[3]);
924
925                         if (mb[1] == 0xffff)
926                                 goto global_port_update;
927
928                         /* Port logout */
929                         fcport = qla2x00_find_fcport_by_loopid(vha, mb[1]);
930                         if (!fcport)
931                                 break;
932                         if (atomic_read(&fcport->state) != FCS_ONLINE)
933                                 break;
934                         ql_dbg(ql_dbg_async, vha, 0x508a,
935                             "Marking port lost loopid=%04x portid=%06x.\n",
936                             fcport->loop_id, fcport->d_id.b24);
937                         qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
938                         break;
939
940 global_port_update:
941                         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
942                                 atomic_set(&vha->loop_state, LOOP_DOWN);
943                                 atomic_set(&vha->loop_down_timer,
944                                     LOOP_DOWN_TIME);
945                                 vha->device_flags |= DFLG_NO_CABLE;
946                                 qla2x00_mark_all_devices_lost(vha, 1);
947                         }
948
949                         if (vha->vp_idx) {
950                                 atomic_set(&vha->vp_state, VP_FAILED);
951                                 fc_vport_set_state(vha->fc_vport,
952                                     FC_VPORT_FAILED);
953                                 qla2x00_mark_all_devices_lost(vha, 1);
954                         }
955
956                         vha->flags.management_server_logged_in = 0;
957                         ha->link_data_rate = PORT_SPEED_UNKNOWN;
958                         break;
959                 }
960
961                 /*
962                  * If PORT UPDATE is global (received LIP_OCCURRED/LIP_RESET
963                  * event etc. earlier indicating loop is down) then process
964                  * it.  Otherwise ignore it and Wait for RSCN to come in.
965                  */
966                 atomic_set(&vha->loop_down_timer, 0);
967                 if (atomic_read(&vha->loop_state) != LOOP_DOWN &&
968                     atomic_read(&vha->loop_state) != LOOP_DEAD) {
969                         ql_dbg(ql_dbg_async, vha, 0x5011,
970                             "Asynchronous PORT UPDATE ignored %04x/%04x/%04x.\n",
971                             mb[1], mb[2], mb[3]);
972
973                         qlt_async_event(mb[0], vha, mb);
974                         break;
975                 }
976
977                 ql_dbg(ql_dbg_async, vha, 0x5012,
978                     "Port database changed %04x %04x %04x.\n",
979                     mb[1], mb[2], mb[3]);
980
981                 /*
982                  * Mark all devices as missing so we will login again.
983                  */
984                 atomic_set(&vha->loop_state, LOOP_UP);
985
986                 qla2x00_mark_all_devices_lost(vha, 1);
987
988                 if (vha->vp_idx == 0 && !qla_ini_mode_enabled(vha))
989                         set_bit(SCR_PENDING, &vha->dpc_flags);
990
991                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
992                 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
993                 set_bit(VP_CONFIG_OK, &vha->vp_flags);
994
995                 qlt_async_event(mb[0], vha, mb);
996                 break;
997
998         case MBA_RSCN_UPDATE:           /* State Change Registration */
999                 /* Check if the Vport has issued a SCR */
1000                 if (vha->vp_idx && test_bit(VP_SCR_NEEDED, &vha->vp_flags))
1001                         break;
1002                 /* Only handle SCNs for our Vport index. */
1003                 if (ha->flags.npiv_supported && vha->vp_idx != (mb[3] & 0xff))
1004                         break;
1005
1006                 ql_dbg(ql_dbg_async, vha, 0x5013,
1007                     "RSCN database changed -- %04x %04x %04x.\n",
1008                     mb[1], mb[2], mb[3]);
1009
1010                 rscn_entry = ((mb[1] & 0xff) << 16) | mb[2];
1011                 host_pid = (vha->d_id.b.domain << 16) | (vha->d_id.b.area << 8)
1012                                 | vha->d_id.b.al_pa;
1013                 if (rscn_entry == host_pid) {
1014                         ql_dbg(ql_dbg_async, vha, 0x5014,
1015                             "Ignoring RSCN update to local host "
1016                             "port ID (%06x).\n", host_pid);
1017                         break;
1018                 }
1019
1020                 /* Ignore reserved bits from RSCN-payload. */
1021                 rscn_entry = ((mb[1] & 0x3ff) << 16) | mb[2];
1022
1023                 /* Skip RSCNs for virtual ports on the same physical port */
1024                 if (qla2x00_is_a_vp_did(vha, rscn_entry))
1025                         break;
1026
1027                 /*
1028                  * Search for the rport related to this RSCN entry and mark it
1029                  * as lost.
1030                  */
1031                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1032                         if (atomic_read(&fcport->state) != FCS_ONLINE)
1033                                 continue;
1034                         if (fcport->d_id.b24 == rscn_entry) {
1035                                 qla2x00_mark_device_lost(vha, fcport, 0, 0);
1036                                 break;
1037                         }
1038                 }
1039
1040                 atomic_set(&vha->loop_down_timer, 0);
1041                 vha->flags.management_server_logged_in = 0;
1042
1043                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1044                 set_bit(RSCN_UPDATE, &vha->dpc_flags);
1045                 qla2x00_post_aen_work(vha, FCH_EVT_RSCN, rscn_entry);
1046                 break;
1047
1048         /* case MBA_RIO_RESPONSE: */
1049         case MBA_ZIO_RESPONSE:
1050                 ql_dbg(ql_dbg_async, vha, 0x5015,
1051                     "[R|Z]IO update completion.\n");
1052
1053                 if (IS_FWI2_CAPABLE(ha))
1054                         qla24xx_process_response_queue(vha, rsp);
1055                 else
1056                         qla2x00_process_response_queue(rsp);
1057                 break;
1058
1059         case MBA_DISCARD_RND_FRAME:
1060                 ql_dbg(ql_dbg_async, vha, 0x5016,
1061                     "Discard RND Frame -- %04x %04x %04x.\n",
1062                     mb[1], mb[2], mb[3]);
1063                 break;
1064
1065         case MBA_TRACE_NOTIFICATION:
1066                 ql_dbg(ql_dbg_async, vha, 0x5017,
1067                     "Trace Notification -- %04x %04x.\n", mb[1], mb[2]);
1068                 break;
1069
1070         case MBA_ISP84XX_ALERT:
1071                 ql_dbg(ql_dbg_async, vha, 0x5018,
1072                     "ISP84XX Alert Notification -- %04x %04x %04x.\n",
1073                     mb[1], mb[2], mb[3]);
1074
1075                 spin_lock_irqsave(&ha->cs84xx->access_lock, flags);
1076                 switch (mb[1]) {
1077                 case A84_PANIC_RECOVERY:
1078                         ql_log(ql_log_info, vha, 0x5019,
1079                             "Alert 84XX: panic recovery %04x %04x.\n",
1080                             mb[2], mb[3]);
1081                         break;
1082                 case A84_OP_LOGIN_COMPLETE:
1083                         ha->cs84xx->op_fw_version = mb[3] << 16 | mb[2];
1084                         ql_log(ql_log_info, vha, 0x501a,
1085                             "Alert 84XX: firmware version %x.\n",
1086                             ha->cs84xx->op_fw_version);
1087                         break;
1088                 case A84_DIAG_LOGIN_COMPLETE:
1089                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
1090                         ql_log(ql_log_info, vha, 0x501b,
1091                             "Alert 84XX: diagnostic firmware version %x.\n",
1092                             ha->cs84xx->diag_fw_version);
1093                         break;
1094                 case A84_GOLD_LOGIN_COMPLETE:
1095                         ha->cs84xx->diag_fw_version = mb[3] << 16 | mb[2];
1096                         ha->cs84xx->fw_update = 1;
1097                         ql_log(ql_log_info, vha, 0x501c,
1098                             "Alert 84XX: gold firmware version %x.\n",
1099                             ha->cs84xx->gold_fw_version);
1100                         break;
1101                 default:
1102                         ql_log(ql_log_warn, vha, 0x501d,
1103                             "Alert 84xx: Invalid Alert %04x %04x %04x.\n",
1104                             mb[1], mb[2], mb[3]);
1105                 }
1106                 spin_unlock_irqrestore(&ha->cs84xx->access_lock, flags);
1107                 break;
1108         case MBA_DCBX_START:
1109                 ql_dbg(ql_dbg_async, vha, 0x501e,
1110                     "DCBX Started -- %04x %04x %04x.\n",
1111                     mb[1], mb[2], mb[3]);
1112                 break;
1113         case MBA_DCBX_PARAM_UPDATE:
1114                 ql_dbg(ql_dbg_async, vha, 0x501f,
1115                     "DCBX Parameters Updated -- %04x %04x %04x.\n",
1116                     mb[1], mb[2], mb[3]);
1117                 break;
1118         case MBA_FCF_CONF_ERR:
1119                 ql_dbg(ql_dbg_async, vha, 0x5020,
1120                     "FCF Configuration Error -- %04x %04x %04x.\n",
1121                     mb[1], mb[2], mb[3]);
1122                 break;
1123         case MBA_IDC_NOTIFY:
1124                 if (IS_QLA8031(vha->hw) || IS_QLA8044(ha)) {
1125                         mb[4] = RD_REG_WORD(&reg24->mailbox4);
1126                         if (((mb[2] & 0x7fff) == MBC_PORT_RESET ||
1127                             (mb[2] & 0x7fff) == MBC_SET_PORT_CONFIG) &&
1128                             (mb[4] & INTERNAL_LOOPBACK_MASK) != 0) {
1129                                 set_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags);
1130                                 /*
1131                                  * Extend loop down timer since port is active.
1132                                  */
1133                                 if (atomic_read(&vha->loop_state) == LOOP_DOWN)
1134                                         atomic_set(&vha->loop_down_timer,
1135                                             LOOP_DOWN_TIME);
1136                                 qla2xxx_wake_dpc(vha);
1137                         }
1138                 }
1139         case MBA_IDC_COMPLETE:
1140                 if (ha->notify_lb_portup_comp && !vha->vp_idx)
1141                         complete(&ha->lb_portup_comp);
1142                 /* Fallthru */
1143         case MBA_IDC_TIME_EXT:
1144                 if (IS_QLA81XX(vha->hw) || IS_QLA8031(vha->hw) ||
1145                     IS_QLA8044(ha))
1146                         qla81xx_idc_event(vha, mb[0], mb[1]);
1147                 break;
1148
1149         case MBA_IDC_AEN:
1150                 mb[4] = RD_REG_WORD(&reg24->mailbox4);
1151                 mb[5] = RD_REG_WORD(&reg24->mailbox5);
1152                 mb[6] = RD_REG_WORD(&reg24->mailbox6);
1153                 mb[7] = RD_REG_WORD(&reg24->mailbox7);
1154                 qla83xx_handle_8200_aen(vha, mb);
1155                 break;
1156
1157         case MBA_DPORT_DIAGNOSTICS:
1158                 ql_dbg(ql_dbg_async, vha, 0x5052,
1159                     "D-Port Diagnostics: %04x result=%s\n",
1160                     mb[0],
1161                     mb[1] == 0 ? "start" :
1162                     mb[1] == 1 ? "done (pass)" :
1163                     mb[1] == 2 ? "done (error)" : "other");
1164                 break;
1165
1166         case MBA_TEMPERATURE_ALERT:
1167                 ql_dbg(ql_dbg_async, vha, 0x505e,
1168                     "TEMPERATURE ALERT: %04x %04x %04x\n", mb[1], mb[2], mb[3]);
1169                 if (mb[1] == 0x12)
1170                         schedule_work(&ha->board_disable);
1171                 break;
1172
1173         default:
1174                 ql_dbg(ql_dbg_async, vha, 0x5057,
1175                     "Unknown AEN:%04x %04x %04x %04x\n",
1176                     mb[0], mb[1], mb[2], mb[3]);
1177         }
1178
1179         qlt_async_event(mb[0], vha, mb);
1180
1181         if (!vha->vp_idx && ha->num_vhosts)
1182                 qla2x00_alert_all_vps(rsp, mb);
1183 }
1184
1185 /**
1186  * qla2x00_process_completed_request() - Process a Fast Post response.
1187  * @ha: SCSI driver HA context
1188  * @index: SRB index
1189  */
1190 void
1191 qla2x00_process_completed_request(struct scsi_qla_host *vha,
1192                                   struct req_que *req, uint32_t index)
1193 {
1194         srb_t *sp;
1195         struct qla_hw_data *ha = vha->hw;
1196
1197         /* Validate handle. */
1198         if (index >= req->num_outstanding_cmds) {
1199                 ql_log(ql_log_warn, vha, 0x3014,
1200                     "Invalid SCSI command index (%x).\n", index);
1201
1202                 if (IS_P3P_TYPE(ha))
1203                         set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1204                 else
1205                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1206                 return;
1207         }
1208
1209         sp = req->outstanding_cmds[index];
1210         if (sp) {
1211                 /* Free outstanding command slot. */
1212                 req->outstanding_cmds[index] = NULL;
1213
1214                 /* Save ISP completion status */
1215                 sp->done(ha, sp, DID_OK << 16);
1216         } else {
1217                 ql_log(ql_log_warn, vha, 0x3016, "Invalid SCSI SRB.\n");
1218
1219                 if (IS_P3P_TYPE(ha))
1220                         set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1221                 else
1222                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1223         }
1224 }
1225
1226 srb_t *
1227 qla2x00_get_sp_from_handle(scsi_qla_host_t *vha, const char *func,
1228     struct req_que *req, void *iocb)
1229 {
1230         struct qla_hw_data *ha = vha->hw;
1231         sts_entry_t *pkt = iocb;
1232         srb_t *sp = NULL;
1233         uint16_t index;
1234
1235         index = LSW(pkt->handle);
1236         if (index >= req->num_outstanding_cmds) {
1237                 ql_log(ql_log_warn, vha, 0x5031,
1238                     "Invalid command index (%x).\n", index);
1239                 if (IS_P3P_TYPE(ha))
1240                         set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
1241                 else
1242                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1243                 goto done;
1244         }
1245         sp = req->outstanding_cmds[index];
1246         if (!sp) {
1247                 ql_log(ql_log_warn, vha, 0x5032,
1248                     "Invalid completion handle (%x) -- timed-out.\n", index);
1249                 return sp;
1250         }
1251         if (sp->handle != index) {
1252                 ql_log(ql_log_warn, vha, 0x5033,
1253                     "SRB handle (%x) mismatch %x.\n", sp->handle, index);
1254                 return NULL;
1255         }
1256
1257         req->outstanding_cmds[index] = NULL;
1258
1259 done:
1260         return sp;
1261 }
1262
1263 static void
1264 qla2x00_mbx_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
1265     struct mbx_entry *mbx)
1266 {
1267         const char func[] = "MBX-IOCB";
1268         const char *type;
1269         fc_port_t *fcport;
1270         srb_t *sp;
1271         struct srb_iocb *lio;
1272         uint16_t *data;
1273         uint16_t status;
1274
1275         sp = qla2x00_get_sp_from_handle(vha, func, req, mbx);
1276         if (!sp)
1277                 return;
1278
1279         lio = &sp->u.iocb_cmd;
1280         type = sp->name;
1281         fcport = sp->fcport;
1282         data = lio->u.logio.data;
1283
1284         data[0] = MBS_COMMAND_ERROR;
1285         data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1286             QLA_LOGIO_LOGIN_RETRIED : 0;
1287         if (mbx->entry_status) {
1288                 ql_dbg(ql_dbg_async, vha, 0x5043,
1289                     "Async-%s error entry - hdl=%x portid=%02x%02x%02x "
1290                     "entry-status=%x status=%x state-flag=%x "
1291                     "status-flags=%x.\n", type, sp->handle,
1292                     fcport->d_id.b.domain, fcport->d_id.b.area,
1293                     fcport->d_id.b.al_pa, mbx->entry_status,
1294                     le16_to_cpu(mbx->status), le16_to_cpu(mbx->state_flags),
1295                     le16_to_cpu(mbx->status_flags));
1296
1297                 ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5029,
1298                     (uint8_t *)mbx, sizeof(*mbx));
1299
1300                 goto logio_done;
1301         }
1302
1303         status = le16_to_cpu(mbx->status);
1304         if (status == 0x30 && sp->type == SRB_LOGIN_CMD &&
1305             le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE)
1306                 status = 0;
1307         if (!status && le16_to_cpu(mbx->mb0) == MBS_COMMAND_COMPLETE) {
1308                 ql_dbg(ql_dbg_async, vha, 0x5045,
1309                     "Async-%s complete - hdl=%x portid=%02x%02x%02x mbx1=%x.\n",
1310                     type, sp->handle, fcport->d_id.b.domain,
1311                     fcport->d_id.b.area, fcport->d_id.b.al_pa,
1312                     le16_to_cpu(mbx->mb1));
1313
1314                 data[0] = MBS_COMMAND_COMPLETE;
1315                 if (sp->type == SRB_LOGIN_CMD) {
1316                         fcport->port_type = FCT_TARGET;
1317                         if (le16_to_cpu(mbx->mb1) & BIT_0)
1318                                 fcport->port_type = FCT_INITIATOR;
1319                         else if (le16_to_cpu(mbx->mb1) & BIT_1)
1320                                 fcport->flags |= FCF_FCP2_DEVICE;
1321                 }
1322                 goto logio_done;
1323         }
1324
1325         data[0] = le16_to_cpu(mbx->mb0);
1326         switch (data[0]) {
1327         case MBS_PORT_ID_USED:
1328                 data[1] = le16_to_cpu(mbx->mb1);
1329                 break;
1330         case MBS_LOOP_ID_USED:
1331                 break;
1332         default:
1333                 data[0] = MBS_COMMAND_ERROR;
1334                 break;
1335         }
1336
1337         ql_log(ql_log_warn, vha, 0x5046,
1338             "Async-%s failed - hdl=%x portid=%02x%02x%02x status=%x "
1339             "mb0=%x mb1=%x mb2=%x mb6=%x mb7=%x.\n", type, sp->handle,
1340             fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
1341             status, le16_to_cpu(mbx->mb0), le16_to_cpu(mbx->mb1),
1342             le16_to_cpu(mbx->mb2), le16_to_cpu(mbx->mb6),
1343             le16_to_cpu(mbx->mb7));
1344
1345 logio_done:
1346         sp->done(vha, sp, 0);
1347 }
1348
1349 static void
1350 qla2x00_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
1351     sts_entry_t *pkt, int iocb_type)
1352 {
1353         const char func[] = "CT_IOCB";
1354         const char *type;
1355         srb_t *sp;
1356         struct bsg_job *bsg_job;
1357         struct fc_bsg_reply *bsg_reply;
1358         uint16_t comp_status;
1359         int res;
1360
1361         sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1362         if (!sp)
1363                 return;
1364
1365         bsg_job = sp->u.bsg_job;
1366         bsg_reply = bsg_job->reply;
1367
1368         type = "ct pass-through";
1369
1370         comp_status = le16_to_cpu(pkt->comp_status);
1371
1372         /* return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1373          * fc payload  to the caller
1374          */
1375         bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1376         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1377
1378         if (comp_status != CS_COMPLETE) {
1379                 if (comp_status == CS_DATA_UNDERRUN) {
1380                         res = DID_OK << 16;
1381                         bsg_reply->reply_payload_rcv_len =
1382                             le16_to_cpu(((sts_entry_t *)pkt)->rsp_info_len);
1383
1384                         ql_log(ql_log_warn, vha, 0x5048,
1385                             "CT pass-through-%s error "
1386                             "comp_status-status=0x%x total_byte = 0x%x.\n",
1387                             type, comp_status,
1388                             bsg_reply->reply_payload_rcv_len);
1389                 } else {
1390                         ql_log(ql_log_warn, vha, 0x5049,
1391                             "CT pass-through-%s error "
1392                             "comp_status-status=0x%x.\n", type, comp_status);
1393                         res = DID_ERROR << 16;
1394                         bsg_reply->reply_payload_rcv_len = 0;
1395                 }
1396                 ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5035,
1397                     (uint8_t *)pkt, sizeof(*pkt));
1398         } else {
1399                 res = DID_OK << 16;
1400                 bsg_reply->reply_payload_rcv_len =
1401                     bsg_job->reply_payload.payload_len;
1402                 bsg_job->reply_len = 0;
1403         }
1404
1405         sp->done(vha, sp, res);
1406 }
1407
1408 static void
1409 qla24xx_els_ct_entry(scsi_qla_host_t *vha, struct req_que *req,
1410     struct sts_entry_24xx *pkt, int iocb_type)
1411 {
1412         const char func[] = "ELS_CT_IOCB";
1413         const char *type;
1414         srb_t *sp;
1415         struct bsg_job *bsg_job;
1416         struct fc_bsg_reply *bsg_reply;
1417         uint16_t comp_status;
1418         uint32_t fw_status[3];
1419         uint8_t* fw_sts_ptr;
1420         int res;
1421
1422         sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
1423         if (!sp)
1424                 return;
1425         bsg_job = sp->u.bsg_job;
1426         bsg_reply = bsg_job->reply;
1427
1428         type = NULL;
1429         switch (sp->type) {
1430         case SRB_ELS_CMD_RPT:
1431         case SRB_ELS_CMD_HST:
1432                 type = "els";
1433                 break;
1434         case SRB_CT_CMD:
1435                 type = "ct pass-through";
1436                 break;
1437         case SRB_ELS_DCMD:
1438                 type = "Driver ELS logo";
1439                 ql_dbg(ql_dbg_user, vha, 0x5047,
1440                     "Completing %s: (%p) type=%d.\n", type, sp, sp->type);
1441                 sp->done(vha, sp, 0);
1442                 return;
1443         default:
1444                 ql_dbg(ql_dbg_user, vha, 0x503e,
1445                     "Unrecognized SRB: (%p) type=%d.\n", sp, sp->type);
1446                 return;
1447         }
1448
1449         comp_status = fw_status[0] = le16_to_cpu(pkt->comp_status);
1450         fw_status[1] = le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->error_subcode_1);
1451         fw_status[2] = le16_to_cpu(((struct els_sts_entry_24xx*)pkt)->error_subcode_2);
1452
1453         /* return FC_CTELS_STATUS_OK and leave the decoding of the ELS/CT
1454          * fc payload  to the caller
1455          */
1456         bsg_reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
1457         bsg_job->reply_len = sizeof(struct fc_bsg_reply) + sizeof(fw_status);
1458
1459         if (comp_status != CS_COMPLETE) {
1460                 if (comp_status == CS_DATA_UNDERRUN) {
1461                         res = DID_OK << 16;
1462                         bsg_reply->reply_payload_rcv_len =
1463                             le16_to_cpu(((struct els_sts_entry_24xx *)pkt)->total_byte_count);
1464
1465                         ql_dbg(ql_dbg_user, vha, 0x503f,
1466                             "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
1467                             "error subcode 1=0x%x error subcode 2=0x%x total_byte = 0x%x.\n",
1468                             type, sp->handle, comp_status, fw_status[1], fw_status[2],
1469                             le16_to_cpu(((struct els_sts_entry_24xx *)
1470                                 pkt)->total_byte_count));
1471                         fw_sts_ptr = ((uint8_t*)scsi_req(bsg_job->req)->sense) +
1472                                 sizeof(struct fc_bsg_reply);
1473                         memcpy( fw_sts_ptr, fw_status, sizeof(fw_status));
1474                 }
1475                 else {
1476                         ql_dbg(ql_dbg_user, vha, 0x5040,
1477                             "ELS-CT pass-through-%s error hdl=%x comp_status-status=0x%x "
1478                             "error subcode 1=0x%x error subcode 2=0x%x.\n",
1479                             type, sp->handle, comp_status,
1480                             le16_to_cpu(((struct els_sts_entry_24xx *)
1481                                 pkt)->error_subcode_1),
1482                             le16_to_cpu(((struct els_sts_entry_24xx *)
1483                                     pkt)->error_subcode_2));
1484                         res = DID_ERROR << 16;
1485                         bsg_reply->reply_payload_rcv_len = 0;
1486                         fw_sts_ptr = ((uint8_t*)scsi_req(bsg_job->req)->sense) +
1487                                         sizeof(struct fc_bsg_reply);
1488                         memcpy( fw_sts_ptr, fw_status, sizeof(fw_status));
1489                 }
1490                 ql_dump_buffer(ql_dbg_user + ql_dbg_buffer, vha, 0x5056,
1491                                 (uint8_t *)pkt, sizeof(*pkt));
1492         }
1493         else {
1494                 res =  DID_OK << 16;
1495                 bsg_reply->reply_payload_rcv_len = bsg_job->reply_payload.payload_len;
1496                 bsg_job->reply_len = 0;
1497         }
1498
1499         sp->done(vha, sp, res);
1500 }
1501
1502 static void
1503 qla24xx_logio_entry(scsi_qla_host_t *vha, struct req_que *req,
1504     struct logio_entry_24xx *logio)
1505 {
1506         const char func[] = "LOGIO-IOCB";
1507         const char *type;
1508         fc_port_t *fcport;
1509         srb_t *sp;
1510         struct srb_iocb *lio;
1511         uint16_t *data;
1512         uint32_t iop[2];
1513
1514         sp = qla2x00_get_sp_from_handle(vha, func, req, logio);
1515         if (!sp)
1516                 return;
1517
1518         lio = &sp->u.iocb_cmd;
1519         type = sp->name;
1520         fcport = sp->fcport;
1521         data = lio->u.logio.data;
1522
1523         data[0] = MBS_COMMAND_ERROR;
1524         data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
1525                 QLA_LOGIO_LOGIN_RETRIED : 0;
1526         if (logio->entry_status) {
1527                 ql_log(ql_log_warn, fcport->vha, 0x5034,
1528                     "Async-%s error entry - hdl=%x"
1529                     "portid=%02x%02x%02x entry-status=%x.\n",
1530                     type, sp->handle, fcport->d_id.b.domain,
1531                     fcport->d_id.b.area, fcport->d_id.b.al_pa,
1532                     logio->entry_status);
1533                 ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x504d,
1534                     (uint8_t *)logio, sizeof(*logio));
1535
1536                 goto logio_done;
1537         }
1538
1539         if (le16_to_cpu(logio->comp_status) == CS_COMPLETE) {
1540                 ql_dbg(ql_dbg_async, fcport->vha, 0x5036,
1541                     "Async-%s complete - hdl=%x portid=%02x%02x%02x "
1542                     "iop0=%x.\n", type, sp->handle, fcport->d_id.b.domain,
1543                     fcport->d_id.b.area, fcport->d_id.b.al_pa,
1544                     le32_to_cpu(logio->io_parameter[0]));
1545
1546                 data[0] = MBS_COMMAND_COMPLETE;
1547                 if (sp->type != SRB_LOGIN_CMD)
1548                         goto logio_done;
1549
1550                 iop[0] = le32_to_cpu(logio->io_parameter[0]);
1551                 if (iop[0] & BIT_4) {
1552                         fcport->port_type = FCT_TARGET;
1553                         if (iop[0] & BIT_8)
1554                                 fcport->flags |= FCF_FCP2_DEVICE;
1555                 } else if (iop[0] & BIT_5)
1556                         fcport->port_type = FCT_INITIATOR;
1557
1558                 if (iop[0] & BIT_7)
1559                         fcport->flags |= FCF_CONF_COMP_SUPPORTED;
1560
1561                 if (logio->io_parameter[7] || logio->io_parameter[8])
1562                         fcport->supported_classes |= FC_COS_CLASS2;
1563                 if (logio->io_parameter[9] || logio->io_parameter[10])
1564                         fcport->supported_classes |= FC_COS_CLASS3;
1565
1566                 goto logio_done;
1567         }
1568
1569         iop[0] = le32_to_cpu(logio->io_parameter[0]);
1570         iop[1] = le32_to_cpu(logio->io_parameter[1]);
1571         switch (iop[0]) {
1572         case LSC_SCODE_PORTID_USED:
1573                 data[0] = MBS_PORT_ID_USED;
1574                 data[1] = LSW(iop[1]);
1575                 break;
1576         case LSC_SCODE_NPORT_USED:
1577                 data[0] = MBS_LOOP_ID_USED;
1578                 break;
1579         default:
1580                 data[0] = MBS_COMMAND_ERROR;
1581                 break;
1582         }
1583
1584         ql_dbg(ql_dbg_async, fcport->vha, 0x5037,
1585             "Async-%s failed - hdl=%x portid=%02x%02x%02x comp=%x "
1586             "iop0=%x iop1=%x.\n", type, sp->handle, fcport->d_id.b.domain,
1587             fcport->d_id.b.area, fcport->d_id.b.al_pa,
1588             le16_to_cpu(logio->comp_status),
1589             le32_to_cpu(logio->io_parameter[0]),
1590             le32_to_cpu(logio->io_parameter[1]));
1591
1592 logio_done:
1593         sp->done(vha, sp, 0);
1594 }
1595
1596 static void
1597 qla24xx_tm_iocb_entry(scsi_qla_host_t *vha, struct req_que *req, void *tsk)
1598 {
1599         const char func[] = "TMF-IOCB";
1600         const char *type;
1601         fc_port_t *fcport;
1602         srb_t *sp;
1603         struct srb_iocb *iocb;
1604         struct sts_entry_24xx *sts = (struct sts_entry_24xx *)tsk;
1605
1606         sp = qla2x00_get_sp_from_handle(vha, func, req, tsk);
1607         if (!sp)
1608                 return;
1609
1610         iocb = &sp->u.iocb_cmd;
1611         type = sp->name;
1612         fcport = sp->fcport;
1613         iocb->u.tmf.data = QLA_SUCCESS;
1614
1615         if (sts->entry_status) {
1616                 ql_log(ql_log_warn, fcport->vha, 0x5038,
1617                     "Async-%s error - hdl=%x entry-status(%x).\n",
1618                     type, sp->handle, sts->entry_status);
1619                 iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1620         } else if (sts->comp_status != cpu_to_le16(CS_COMPLETE)) {
1621                 ql_log(ql_log_warn, fcport->vha, 0x5039,
1622                     "Async-%s error - hdl=%x completion status(%x).\n",
1623                     type, sp->handle, sts->comp_status);
1624                 iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1625         } else if ((le16_to_cpu(sts->scsi_status) &
1626             SS_RESPONSE_INFO_LEN_VALID)) {
1627                 if (le32_to_cpu(sts->rsp_data_len) < 4) {
1628                         ql_log(ql_log_warn, fcport->vha, 0x503b,
1629                             "Async-%s error - hdl=%x not enough response(%d).\n",
1630                             type, sp->handle, sts->rsp_data_len);
1631                 } else if (sts->data[3]) {
1632                         ql_log(ql_log_warn, fcport->vha, 0x503c,
1633                             "Async-%s error - hdl=%x response(%x).\n",
1634                             type, sp->handle, sts->data[3]);
1635                         iocb->u.tmf.data = QLA_FUNCTION_FAILED;
1636                 }
1637         }
1638
1639         if (iocb->u.tmf.data != QLA_SUCCESS)
1640                 ql_dump_buffer(ql_dbg_async + ql_dbg_buffer, vha, 0x5055,
1641                     (uint8_t *)sts, sizeof(*sts));
1642
1643         sp->done(vha, sp, 0);
1644 }
1645
1646 /**
1647  * qla2x00_process_response_queue() - Process response queue entries.
1648  * @ha: SCSI driver HA context
1649  */
1650 void
1651 qla2x00_process_response_queue(struct rsp_que *rsp)
1652 {
1653         struct scsi_qla_host *vha;
1654         struct qla_hw_data *ha = rsp->hw;
1655         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1656         sts_entry_t     *pkt;
1657         uint16_t        handle_cnt;
1658         uint16_t        cnt;
1659
1660         vha = pci_get_drvdata(ha->pdev);
1661
1662         if (!vha->flags.online)
1663                 return;
1664
1665         while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
1666                 pkt = (sts_entry_t *)rsp->ring_ptr;
1667
1668                 rsp->ring_index++;
1669                 if (rsp->ring_index == rsp->length) {
1670                         rsp->ring_index = 0;
1671                         rsp->ring_ptr = rsp->ring;
1672                 } else {
1673                         rsp->ring_ptr++;
1674                 }
1675
1676                 if (pkt->entry_status != 0) {
1677                         qla2x00_error_entry(vha, rsp, pkt);
1678                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1679                         wmb();
1680                         continue;
1681                 }
1682
1683                 switch (pkt->entry_type) {
1684                 case STATUS_TYPE:
1685                         qla2x00_status_entry(vha, rsp, pkt);
1686                         break;
1687                 case STATUS_TYPE_21:
1688                         handle_cnt = ((sts21_entry_t *)pkt)->handle_count;
1689                         for (cnt = 0; cnt < handle_cnt; cnt++) {
1690                                 qla2x00_process_completed_request(vha, rsp->req,
1691                                     ((sts21_entry_t *)pkt)->handle[cnt]);
1692                         }
1693                         break;
1694                 case STATUS_TYPE_22:
1695                         handle_cnt = ((sts22_entry_t *)pkt)->handle_count;
1696                         for (cnt = 0; cnt < handle_cnt; cnt++) {
1697                                 qla2x00_process_completed_request(vha, rsp->req,
1698                                     ((sts22_entry_t *)pkt)->handle[cnt]);
1699                         }
1700                         break;
1701                 case STATUS_CONT_TYPE:
1702                         qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
1703                         break;
1704                 case MBX_IOCB_TYPE:
1705                         qla2x00_mbx_iocb_entry(vha, rsp->req,
1706                             (struct mbx_entry *)pkt);
1707                         break;
1708                 case CT_IOCB_TYPE:
1709                         qla2x00_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
1710                         break;
1711                 default:
1712                         /* Type Not Supported. */
1713                         ql_log(ql_log_warn, vha, 0x504a,
1714                             "Received unknown response pkt type %x "
1715                             "entry status=%x.\n",
1716                             pkt->entry_type, pkt->entry_status);
1717                         break;
1718                 }
1719                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
1720                 wmb();
1721         }
1722
1723         /* Adjust ring index */
1724         WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), rsp->ring_index);
1725 }
1726
1727 static inline void
1728 qla2x00_handle_sense(srb_t *sp, uint8_t *sense_data, uint32_t par_sense_len,
1729                      uint32_t sense_len, struct rsp_que *rsp, int res)
1730 {
1731         struct scsi_qla_host *vha = sp->fcport->vha;
1732         struct scsi_cmnd *cp = GET_CMD_SP(sp);
1733         uint32_t track_sense_len;
1734
1735         if (sense_len >= SCSI_SENSE_BUFFERSIZE)
1736                 sense_len = SCSI_SENSE_BUFFERSIZE;
1737
1738         SET_CMD_SENSE_LEN(sp, sense_len);
1739         SET_CMD_SENSE_PTR(sp, cp->sense_buffer);
1740         track_sense_len = sense_len;
1741
1742         if (sense_len > par_sense_len)
1743                 sense_len = par_sense_len;
1744
1745         memcpy(cp->sense_buffer, sense_data, sense_len);
1746
1747         SET_CMD_SENSE_PTR(sp, cp->sense_buffer + sense_len);
1748         track_sense_len -= sense_len;
1749         SET_CMD_SENSE_LEN(sp, track_sense_len);
1750
1751         if (track_sense_len != 0) {
1752                 rsp->status_srb = sp;
1753                 cp->result = res;
1754         }
1755
1756         if (sense_len) {
1757                 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x301c,
1758                     "Check condition Sense data, nexus%ld:%d:%llu cmd=%p.\n",
1759                     sp->fcport->vha->host_no, cp->device->id, cp->device->lun,
1760                     cp);
1761                 ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302b,
1762                     cp->sense_buffer, sense_len);
1763         }
1764 }
1765
1766 struct scsi_dif_tuple {
1767         __be16 guard;       /* Checksum */
1768         __be16 app_tag;         /* APPL identifier */
1769         __be32 ref_tag;         /* Target LBA or indirect LBA */
1770 };
1771
1772 /*
1773  * Checks the guard or meta-data for the type of error
1774  * detected by the HBA. In case of errors, we set the
1775  * ASC/ASCQ fields in the sense buffer with ILLEGAL_REQUEST
1776  * to indicate to the kernel that the HBA detected error.
1777  */
1778 static inline int
1779 qla2x00_handle_dif_error(srb_t *sp, struct sts_entry_24xx *sts24)
1780 {
1781         struct scsi_qla_host *vha = sp->fcport->vha;
1782         struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1783         uint8_t         *ap = &sts24->data[12];
1784         uint8_t         *ep = &sts24->data[20];
1785         uint32_t        e_ref_tag, a_ref_tag;
1786         uint16_t        e_app_tag, a_app_tag;
1787         uint16_t        e_guard, a_guard;
1788
1789         /*
1790          * swab32 of the "data" field in the beginning of qla2x00_status_entry()
1791          * would make guard field appear at offset 2
1792          */
1793         a_guard   = le16_to_cpu(*(uint16_t *)(ap + 2));
1794         a_app_tag = le16_to_cpu(*(uint16_t *)(ap + 0));
1795         a_ref_tag = le32_to_cpu(*(uint32_t *)(ap + 4));
1796         e_guard   = le16_to_cpu(*(uint16_t *)(ep + 2));
1797         e_app_tag = le16_to_cpu(*(uint16_t *)(ep + 0));
1798         e_ref_tag = le32_to_cpu(*(uint32_t *)(ep + 4));
1799
1800         ql_dbg(ql_dbg_io, vha, 0x3023,
1801             "iocb(s) %p Returned STATUS.\n", sts24);
1802
1803         ql_dbg(ql_dbg_io, vha, 0x3024,
1804             "DIF ERROR in cmd 0x%x lba 0x%llx act ref"
1805             " tag=0x%x, exp ref_tag=0x%x, act app tag=0x%x, exp app"
1806             " tag=0x%x, act guard=0x%x, exp guard=0x%x.\n",
1807             cmd->cmnd[0], (u64)scsi_get_lba(cmd), a_ref_tag, e_ref_tag,
1808             a_app_tag, e_app_tag, a_guard, e_guard);
1809
1810         /*
1811          * Ignore sector if:
1812          * For type     3: ref & app tag is all 'f's
1813          * For type 0,1,2: app tag is all 'f's
1814          */
1815         if ((a_app_tag == 0xffff) &&
1816             ((scsi_get_prot_type(cmd) != SCSI_PROT_DIF_TYPE3) ||
1817              (a_ref_tag == 0xffffffff))) {
1818                 uint32_t blocks_done, resid;
1819                 sector_t lba_s = scsi_get_lba(cmd);
1820
1821                 /* 2TB boundary case covered automatically with this */
1822                 blocks_done = e_ref_tag - (uint32_t)lba_s + 1;
1823
1824                 resid = scsi_bufflen(cmd) - (blocks_done *
1825                     cmd->device->sector_size);
1826
1827                 scsi_set_resid(cmd, resid);
1828                 cmd->result = DID_OK << 16;
1829
1830                 /* Update protection tag */
1831                 if (scsi_prot_sg_count(cmd)) {
1832                         uint32_t i, j = 0, k = 0, num_ent;
1833                         struct scatterlist *sg;
1834                         struct t10_pi_tuple *spt;
1835
1836                         /* Patch the corresponding protection tags */
1837                         scsi_for_each_prot_sg(cmd, sg,
1838                             scsi_prot_sg_count(cmd), i) {
1839                                 num_ent = sg_dma_len(sg) / 8;
1840                                 if (k + num_ent < blocks_done) {
1841                                         k += num_ent;
1842                                         continue;
1843                                 }
1844                                 j = blocks_done - k - 1;
1845                                 k = blocks_done;
1846                                 break;
1847                         }
1848
1849                         if (k != blocks_done) {
1850                                 ql_log(ql_log_warn, vha, 0x302f,
1851                                     "unexpected tag values tag:lba=%x:%llx)\n",
1852                                     e_ref_tag, (unsigned long long)lba_s);
1853                                 return 1;
1854                         }
1855
1856                         spt = page_address(sg_page(sg)) + sg->offset;
1857                         spt += j;
1858
1859                         spt->app_tag = 0xffff;
1860                         if (scsi_get_prot_type(cmd) == SCSI_PROT_DIF_TYPE3)
1861                                 spt->ref_tag = 0xffffffff;
1862                 }
1863
1864                 return 0;
1865         }
1866
1867         /* check guard */
1868         if (e_guard != a_guard) {
1869                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1870                     0x10, 0x1);
1871                 set_driver_byte(cmd, DRIVER_SENSE);
1872                 set_host_byte(cmd, DID_ABORT);
1873                 cmd->result |= SAM_STAT_CHECK_CONDITION << 1;
1874                 return 1;
1875         }
1876
1877         /* check ref tag */
1878         if (e_ref_tag != a_ref_tag) {
1879                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1880                     0x10, 0x3);
1881                 set_driver_byte(cmd, DRIVER_SENSE);
1882                 set_host_byte(cmd, DID_ABORT);
1883                 cmd->result |= SAM_STAT_CHECK_CONDITION << 1;
1884                 return 1;
1885         }
1886
1887         /* check appl tag */
1888         if (e_app_tag != a_app_tag) {
1889                 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1890                     0x10, 0x2);
1891                 set_driver_byte(cmd, DRIVER_SENSE);
1892                 set_host_byte(cmd, DID_ABORT);
1893                 cmd->result |= SAM_STAT_CHECK_CONDITION << 1;
1894                 return 1;
1895         }
1896
1897         return 1;
1898 }
1899
1900 static void
1901 qla25xx_process_bidir_status_iocb(scsi_qla_host_t *vha, void *pkt,
1902                                   struct req_que *req, uint32_t index)
1903 {
1904         struct qla_hw_data *ha = vha->hw;
1905         srb_t *sp;
1906         uint16_t        comp_status;
1907         uint16_t        scsi_status;
1908         uint16_t thread_id;
1909         uint32_t rval = EXT_STATUS_OK;
1910         struct bsg_job *bsg_job = NULL;
1911         struct fc_bsg_request *bsg_request;
1912         struct fc_bsg_reply *bsg_reply;
1913         sts_entry_t *sts;
1914         struct sts_entry_24xx *sts24;
1915         sts = (sts_entry_t *) pkt;
1916         sts24 = (struct sts_entry_24xx *) pkt;
1917
1918         /* Validate handle. */
1919         if (index >= req->num_outstanding_cmds) {
1920                 ql_log(ql_log_warn, vha, 0x70af,
1921                     "Invalid SCSI completion handle 0x%x.\n", index);
1922                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1923                 return;
1924         }
1925
1926         sp = req->outstanding_cmds[index];
1927         if (!sp) {
1928                 ql_log(ql_log_warn, vha, 0x70b0,
1929                     "Req:%d: Invalid ISP SCSI completion handle(0x%x)\n",
1930                     req->id, index);
1931
1932                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1933                 return;
1934         }
1935
1936         /* Free outstanding command slot. */
1937         req->outstanding_cmds[index] = NULL;
1938         bsg_job = sp->u.bsg_job;
1939         bsg_request = bsg_job->request;
1940         bsg_reply = bsg_job->reply;
1941
1942         if (IS_FWI2_CAPABLE(ha)) {
1943                 comp_status = le16_to_cpu(sts24->comp_status);
1944                 scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
1945         } else {
1946                 comp_status = le16_to_cpu(sts->comp_status);
1947                 scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
1948         }
1949
1950         thread_id = bsg_request->rqst_data.h_vendor.vendor_cmd[1];
1951         switch (comp_status) {
1952         case CS_COMPLETE:
1953                 if (scsi_status == 0) {
1954                         bsg_reply->reply_payload_rcv_len =
1955                                         bsg_job->reply_payload.payload_len;
1956                         vha->qla_stats.input_bytes +=
1957                                 bsg_reply->reply_payload_rcv_len;
1958                         vha->qla_stats.input_requests++;
1959                         rval = EXT_STATUS_OK;
1960                 }
1961                 goto done;
1962
1963         case CS_DATA_OVERRUN:
1964                 ql_dbg(ql_dbg_user, vha, 0x70b1,
1965                     "Command completed with date overrun thread_id=%d\n",
1966                     thread_id);
1967                 rval = EXT_STATUS_DATA_OVERRUN;
1968                 break;
1969
1970         case CS_DATA_UNDERRUN:
1971                 ql_dbg(ql_dbg_user, vha, 0x70b2,
1972                     "Command completed with date underrun thread_id=%d\n",
1973                     thread_id);
1974                 rval = EXT_STATUS_DATA_UNDERRUN;
1975                 break;
1976         case CS_BIDIR_RD_OVERRUN:
1977                 ql_dbg(ql_dbg_user, vha, 0x70b3,
1978                     "Command completed with read data overrun thread_id=%d\n",
1979                     thread_id);
1980                 rval = EXT_STATUS_DATA_OVERRUN;
1981                 break;
1982
1983         case CS_BIDIR_RD_WR_OVERRUN:
1984                 ql_dbg(ql_dbg_user, vha, 0x70b4,
1985                     "Command completed with read and write data overrun "
1986                     "thread_id=%d\n", thread_id);
1987                 rval = EXT_STATUS_DATA_OVERRUN;
1988                 break;
1989
1990         case CS_BIDIR_RD_OVERRUN_WR_UNDERRUN:
1991                 ql_dbg(ql_dbg_user, vha, 0x70b5,
1992                     "Command completed with read data over and write data "
1993                     "underrun thread_id=%d\n", thread_id);
1994                 rval = EXT_STATUS_DATA_OVERRUN;
1995                 break;
1996
1997         case CS_BIDIR_RD_UNDERRUN:
1998                 ql_dbg(ql_dbg_user, vha, 0x70b6,
1999                     "Command completed with read data data underrun "
2000                     "thread_id=%d\n", thread_id);
2001                 rval = EXT_STATUS_DATA_UNDERRUN;
2002                 break;
2003
2004         case CS_BIDIR_RD_UNDERRUN_WR_OVERRUN:
2005                 ql_dbg(ql_dbg_user, vha, 0x70b7,
2006                     "Command completed with read data under and write data "
2007                     "overrun thread_id=%d\n", thread_id);
2008                 rval = EXT_STATUS_DATA_UNDERRUN;
2009                 break;
2010
2011         case CS_BIDIR_RD_WR_UNDERRUN:
2012                 ql_dbg(ql_dbg_user, vha, 0x70b8,
2013                     "Command completed with read and write data underrun "
2014                     "thread_id=%d\n", thread_id);
2015                 rval = EXT_STATUS_DATA_UNDERRUN;
2016                 break;
2017
2018         case CS_BIDIR_DMA:
2019                 ql_dbg(ql_dbg_user, vha, 0x70b9,
2020                     "Command completed with data DMA error thread_id=%d\n",
2021                     thread_id);
2022                 rval = EXT_STATUS_DMA_ERR;
2023                 break;
2024
2025         case CS_TIMEOUT:
2026                 ql_dbg(ql_dbg_user, vha, 0x70ba,
2027                     "Command completed with timeout thread_id=%d\n",
2028                     thread_id);
2029                 rval = EXT_STATUS_TIMEOUT;
2030                 break;
2031         default:
2032                 ql_dbg(ql_dbg_user, vha, 0x70bb,
2033                     "Command completed with completion status=0x%x "
2034                     "thread_id=%d\n", comp_status, thread_id);
2035                 rval = EXT_STATUS_ERR;
2036                 break;
2037         }
2038         bsg_reply->reply_payload_rcv_len = 0;
2039
2040 done:
2041         /* Return the vendor specific reply to API */
2042         bsg_reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
2043         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2044         /* Always return DID_OK, bsg will send the vendor specific response
2045          * in this case only */
2046         sp->done(vha, sp, (DID_OK << 6));
2047
2048 }
2049
2050 /**
2051  * qla2x00_status_entry() - Process a Status IOCB entry.
2052  * @ha: SCSI driver HA context
2053  * @pkt: Entry pointer
2054  */
2055 static void
2056 qla2x00_status_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, void *pkt)
2057 {
2058         srb_t           *sp;
2059         fc_port_t       *fcport;
2060         struct scsi_cmnd *cp;
2061         sts_entry_t *sts;
2062         struct sts_entry_24xx *sts24;
2063         uint16_t        comp_status;
2064         uint16_t        scsi_status;
2065         uint16_t        ox_id;
2066         uint8_t         lscsi_status;
2067         int32_t         resid;
2068         uint32_t sense_len, par_sense_len, rsp_info_len, resid_len,
2069             fw_resid_len;
2070         uint8_t         *rsp_info, *sense_data;
2071         struct qla_hw_data *ha = vha->hw;
2072         uint32_t handle;
2073         uint16_t que;
2074         struct req_que *req;
2075         int logit = 1;
2076         int res = 0;
2077         uint16_t state_flags = 0;
2078         uint16_t retry_delay = 0;
2079
2080         sts = (sts_entry_t *) pkt;
2081         sts24 = (struct sts_entry_24xx *) pkt;
2082         if (IS_FWI2_CAPABLE(ha)) {
2083                 comp_status = le16_to_cpu(sts24->comp_status);
2084                 scsi_status = le16_to_cpu(sts24->scsi_status) & SS_MASK;
2085                 state_flags = le16_to_cpu(sts24->state_flags);
2086         } else {
2087                 comp_status = le16_to_cpu(sts->comp_status);
2088                 scsi_status = le16_to_cpu(sts->scsi_status) & SS_MASK;
2089         }
2090         handle = (uint32_t) LSW(sts->handle);
2091         que = MSW(sts->handle);
2092         req = ha->req_q_map[que];
2093
2094         /* Check for invalid queue pointer */
2095         if (req == NULL ||
2096             que >= find_first_zero_bit(ha->req_qid_map, ha->max_req_queues)) {
2097                 ql_dbg(ql_dbg_io, vha, 0x3059,
2098                     "Invalid status handle (0x%x): Bad req pointer. req=%p, "
2099                     "que=%u.\n", sts->handle, req, que);
2100                 return;
2101         }
2102
2103         /* Validate handle. */
2104         if (handle < req->num_outstanding_cmds) {
2105                 sp = req->outstanding_cmds[handle];
2106                 if (!sp) {
2107                         ql_dbg(ql_dbg_io, vha, 0x3075,
2108                             "%s(%ld): Already returned command for status handle (0x%x).\n",
2109                             __func__, vha->host_no, sts->handle);
2110                         return;
2111                 }
2112         } else {
2113                 ql_dbg(ql_dbg_io, vha, 0x3017,
2114                     "Invalid status handle, out of range (0x%x).\n",
2115                     sts->handle);
2116
2117                 if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
2118                         if (IS_P3P_TYPE(ha))
2119                                 set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
2120                         else
2121                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2122                         qla2xxx_wake_dpc(vha);
2123                 }
2124                 return;
2125         }
2126
2127         if (unlikely((state_flags & BIT_1) && (sp->type == SRB_BIDI_CMD))) {
2128                 qla25xx_process_bidir_status_iocb(vha, pkt, req, handle);
2129                 return;
2130         }
2131
2132         /* Task Management completion. */
2133         if (sp->type == SRB_TM_CMD) {
2134                 qla24xx_tm_iocb_entry(vha, req, pkt);
2135                 return;
2136         }
2137
2138         /* Fast path completion. */
2139         if (comp_status == CS_COMPLETE && scsi_status == 0) {
2140                 qla2x00_process_completed_request(vha, req, handle);
2141
2142                 return;
2143         }
2144
2145         req->outstanding_cmds[handle] = NULL;
2146         cp = GET_CMD_SP(sp);
2147         if (cp == NULL) {
2148                 ql_dbg(ql_dbg_io, vha, 0x3018,
2149                     "Command already returned (0x%x/%p).\n",
2150                     sts->handle, sp);
2151
2152                 return;
2153         }
2154
2155         lscsi_status = scsi_status & STATUS_MASK;
2156
2157         fcport = sp->fcport;
2158
2159         ox_id = 0;
2160         sense_len = par_sense_len = rsp_info_len = resid_len =
2161             fw_resid_len = 0;
2162         if (IS_FWI2_CAPABLE(ha)) {
2163                 if (scsi_status & SS_SENSE_LEN_VALID)
2164                         sense_len = le32_to_cpu(sts24->sense_len);
2165                 if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
2166                         rsp_info_len = le32_to_cpu(sts24->rsp_data_len);
2167                 if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER))
2168                         resid_len = le32_to_cpu(sts24->rsp_residual_count);
2169                 if (comp_status == CS_DATA_UNDERRUN)
2170                         fw_resid_len = le32_to_cpu(sts24->residual_len);
2171                 rsp_info = sts24->data;
2172                 sense_data = sts24->data;
2173                 host_to_fcp_swap(sts24->data, sizeof(sts24->data));
2174                 ox_id = le16_to_cpu(sts24->ox_id);
2175                 par_sense_len = sizeof(sts24->data);
2176                 /* Valid values of the retry delay timer are 0x1-0xffef */
2177                 if (sts24->retry_delay > 0 && sts24->retry_delay < 0xfff1)
2178                         retry_delay = sts24->retry_delay;
2179         } else {
2180                 if (scsi_status & SS_SENSE_LEN_VALID)
2181                         sense_len = le16_to_cpu(sts->req_sense_length);
2182                 if (scsi_status & SS_RESPONSE_INFO_LEN_VALID)
2183                         rsp_info_len = le16_to_cpu(sts->rsp_info_len);
2184                 resid_len = le32_to_cpu(sts->residual_length);
2185                 rsp_info = sts->rsp_info;
2186                 sense_data = sts->req_sense_data;
2187                 par_sense_len = sizeof(sts->req_sense_data);
2188         }
2189
2190         /* Check for any FCP transport errors. */
2191         if (scsi_status & SS_RESPONSE_INFO_LEN_VALID) {
2192                 /* Sense data lies beyond any FCP RESPONSE data. */
2193                 if (IS_FWI2_CAPABLE(ha)) {
2194                         sense_data += rsp_info_len;
2195                         par_sense_len -= rsp_info_len;
2196                 }
2197                 if (rsp_info_len > 3 && rsp_info[3]) {
2198                         ql_dbg(ql_dbg_io, fcport->vha, 0x3019,
2199                             "FCP I/O protocol failure (0x%x/0x%x).\n",
2200                             rsp_info_len, rsp_info[3]);
2201
2202                         res = DID_BUS_BUSY << 16;
2203                         goto out;
2204                 }
2205         }
2206
2207         /* Check for overrun. */
2208         if (IS_FWI2_CAPABLE(ha) && comp_status == CS_COMPLETE &&
2209             scsi_status & SS_RESIDUAL_OVER)
2210                 comp_status = CS_DATA_OVERRUN;
2211
2212         /*
2213          * Check retry_delay_timer value if we receive a busy or
2214          * queue full.
2215          */
2216         if (lscsi_status == SAM_STAT_TASK_SET_FULL ||
2217             lscsi_status == SAM_STAT_BUSY)
2218                 qla2x00_set_retry_delay_timestamp(fcport, retry_delay);
2219
2220         /*
2221          * Based on Host and scsi status generate status code for Linux
2222          */
2223         switch (comp_status) {
2224         case CS_COMPLETE:
2225         case CS_QUEUE_FULL:
2226                 if (scsi_status == 0) {
2227                         res = DID_OK << 16;
2228                         break;
2229                 }
2230                 if (scsi_status & (SS_RESIDUAL_UNDER | SS_RESIDUAL_OVER)) {
2231                         resid = resid_len;
2232                         scsi_set_resid(cp, resid);
2233
2234                         if (!lscsi_status &&
2235                             ((unsigned)(scsi_bufflen(cp) - resid) <
2236                              cp->underflow)) {
2237                                 ql_dbg(ql_dbg_io, fcport->vha, 0x301a,
2238                                     "Mid-layer underflow "
2239                                     "detected (0x%x of 0x%x bytes).\n",
2240                                     resid, scsi_bufflen(cp));
2241
2242                                 res = DID_ERROR << 16;
2243                                 break;
2244                         }
2245                 }
2246                 res = DID_OK << 16 | lscsi_status;
2247
2248                 if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
2249                         ql_dbg(ql_dbg_io, fcport->vha, 0x301b,
2250                             "QUEUE FULL detected.\n");
2251                         break;
2252                 }
2253                 logit = 0;
2254                 if (lscsi_status != SS_CHECK_CONDITION)
2255                         break;
2256
2257                 memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2258                 if (!(scsi_status & SS_SENSE_LEN_VALID))
2259                         break;
2260
2261                 qla2x00_handle_sense(sp, sense_data, par_sense_len, sense_len,
2262                     rsp, res);
2263                 break;
2264
2265         case CS_DATA_UNDERRUN:
2266                 /* Use F/W calculated residual length. */
2267                 resid = IS_FWI2_CAPABLE(ha) ? fw_resid_len : resid_len;
2268                 scsi_set_resid(cp, resid);
2269                 if (scsi_status & SS_RESIDUAL_UNDER) {
2270                         if (IS_FWI2_CAPABLE(ha) && fw_resid_len != resid_len) {
2271                                 ql_dbg(ql_dbg_io, fcport->vha, 0x301d,
2272                                     "Dropped frame(s) detected "
2273                                     "(0x%x of 0x%x bytes).\n",
2274                                     resid, scsi_bufflen(cp));
2275
2276                                 res = DID_ERROR << 16 | lscsi_status;
2277                                 goto check_scsi_status;
2278                         }
2279
2280                         if (!lscsi_status &&
2281                             ((unsigned)(scsi_bufflen(cp) - resid) <
2282                             cp->underflow)) {
2283                                 ql_dbg(ql_dbg_io, fcport->vha, 0x301e,
2284                                     "Mid-layer underflow "
2285                                     "detected (0x%x of 0x%x bytes).\n",
2286                                     resid, scsi_bufflen(cp));
2287
2288                                 res = DID_ERROR << 16;
2289                                 break;
2290                         }
2291                 } else if (lscsi_status != SAM_STAT_TASK_SET_FULL &&
2292                             lscsi_status != SAM_STAT_BUSY) {
2293                         /*
2294                          * scsi status of task set and busy are considered to be
2295                          * task not completed.
2296                          */
2297
2298                         ql_dbg(ql_dbg_io, fcport->vha, 0x301f,
2299                             "Dropped frame(s) detected (0x%x "
2300                             "of 0x%x bytes).\n", resid,
2301                             scsi_bufflen(cp));
2302
2303                         res = DID_ERROR << 16 | lscsi_status;
2304                         goto check_scsi_status;
2305                 } else {
2306                         ql_dbg(ql_dbg_io, fcport->vha, 0x3030,
2307                             "scsi_status: 0x%x, lscsi_status: 0x%x\n",
2308                             scsi_status, lscsi_status);
2309                 }
2310
2311                 res = DID_OK << 16 | lscsi_status;
2312                 logit = 0;
2313
2314 check_scsi_status:
2315                 /*
2316                  * Check to see if SCSI Status is non zero. If so report SCSI
2317                  * Status.
2318                  */
2319                 if (lscsi_status != 0) {
2320                         if (lscsi_status == SAM_STAT_TASK_SET_FULL) {
2321                                 ql_dbg(ql_dbg_io, fcport->vha, 0x3020,
2322                                     "QUEUE FULL detected.\n");
2323                                 logit = 1;
2324                                 break;
2325                         }
2326                         if (lscsi_status != SS_CHECK_CONDITION)
2327                                 break;
2328
2329                         memset(cp->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2330                         if (!(scsi_status & SS_SENSE_LEN_VALID))
2331                                 break;
2332
2333                         qla2x00_handle_sense(sp, sense_data, par_sense_len,
2334                             sense_len, rsp, res);
2335                 }
2336                 break;
2337
2338         case CS_PORT_LOGGED_OUT:
2339         case CS_PORT_CONFIG_CHG:
2340         case CS_PORT_BUSY:
2341         case CS_INCOMPLETE:
2342         case CS_PORT_UNAVAILABLE:
2343         case CS_TIMEOUT:
2344         case CS_RESET:
2345
2346                 /*
2347                  * We are going to have the fc class block the rport
2348                  * while we try to recover so instruct the mid layer
2349                  * to requeue until the class decides how to handle this.
2350                  */
2351                 res = DID_TRANSPORT_DISRUPTED << 16;
2352
2353                 if (comp_status == CS_TIMEOUT) {
2354                         if (IS_FWI2_CAPABLE(ha))
2355                                 break;
2356                         else if ((le16_to_cpu(sts->status_flags) &
2357                             SF_LOGOUT_SENT) == 0)
2358                                 break;
2359                 }
2360
2361                 ql_dbg(ql_dbg_io, fcport->vha, 0x3021,
2362                     "Port to be marked lost on fcport=%02x%02x%02x, current "
2363                     "port state= %s.\n", fcport->d_id.b.domain,
2364                     fcport->d_id.b.area, fcport->d_id.b.al_pa,
2365                     port_state_str[atomic_read(&fcport->state)]);
2366
2367                 if (atomic_read(&fcport->state) == FCS_ONLINE)
2368                         qla2x00_mark_device_lost(fcport->vha, fcport, 1, 1);
2369                 break;
2370
2371         case CS_ABORTED:
2372                 res = DID_RESET << 16;
2373                 break;
2374
2375         case CS_DIF_ERROR:
2376                 logit = qla2x00_handle_dif_error(sp, sts24);
2377                 res = cp->result;
2378                 break;
2379
2380         case CS_TRANSPORT:
2381                 res = DID_ERROR << 16;
2382
2383                 if (!IS_PI_SPLIT_DET_CAPABLE(ha))
2384                         break;
2385
2386                 if (state_flags & BIT_4)
2387                         scmd_printk(KERN_WARNING, cp,
2388                             "Unsupported device '%s' found.\n",
2389                             cp->device->vendor);
2390                 break;
2391
2392         default:
2393                 res = DID_ERROR << 16;
2394                 break;
2395         }
2396
2397 out:
2398         if (logit)
2399                 ql_dbg(ql_dbg_io, fcport->vha, 0x3022,
2400                     "FCP command status: 0x%x-0x%x (0x%x) nexus=%ld:%d:%llu "
2401                     "portid=%02x%02x%02x oxid=0x%x cdb=%10phN len=0x%x "
2402                     "rsp_info=0x%x resid=0x%x fw_resid=0x%x sp=%p cp=%p.\n",
2403                     comp_status, scsi_status, res, vha->host_no,
2404                     cp->device->id, cp->device->lun, fcport->d_id.b.domain,
2405                     fcport->d_id.b.area, fcport->d_id.b.al_pa, ox_id,
2406                     cp->cmnd, scsi_bufflen(cp), rsp_info_len,
2407                     resid_len, fw_resid_len, sp, cp);
2408
2409         if (rsp->status_srb == NULL)
2410                 sp->done(ha, sp, res);
2411 }
2412
2413 /**
2414  * qla2x00_status_cont_entry() - Process a Status Continuations entry.
2415  * @ha: SCSI driver HA context
2416  * @pkt: Entry pointer
2417  *
2418  * Extended sense data.
2419  */
2420 static void
2421 qla2x00_status_cont_entry(struct rsp_que *rsp, sts_cont_entry_t *pkt)
2422 {
2423         uint8_t sense_sz = 0;
2424         struct qla_hw_data *ha = rsp->hw;
2425         struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
2426         srb_t *sp = rsp->status_srb;
2427         struct scsi_cmnd *cp;
2428         uint32_t sense_len;
2429         uint8_t *sense_ptr;
2430
2431         if (!sp || !GET_CMD_SENSE_LEN(sp))
2432                 return;
2433
2434         sense_len = GET_CMD_SENSE_LEN(sp);
2435         sense_ptr = GET_CMD_SENSE_PTR(sp);
2436
2437         cp = GET_CMD_SP(sp);
2438         if (cp == NULL) {
2439                 ql_log(ql_log_warn, vha, 0x3025,
2440                     "cmd is NULL: already returned to OS (sp=%p).\n", sp);
2441
2442                 rsp->status_srb = NULL;
2443                 return;
2444         }
2445
2446         if (sense_len > sizeof(pkt->data))
2447                 sense_sz = sizeof(pkt->data);
2448         else
2449                 sense_sz = sense_len;
2450
2451         /* Move sense data. */
2452         if (IS_FWI2_CAPABLE(ha))
2453                 host_to_fcp_swap(pkt->data, sizeof(pkt->data));
2454         memcpy(sense_ptr, pkt->data, sense_sz);
2455         ql_dump_buffer(ql_dbg_io + ql_dbg_buffer, vha, 0x302c,
2456                 sense_ptr, sense_sz);
2457
2458         sense_len -= sense_sz;
2459         sense_ptr += sense_sz;
2460
2461         SET_CMD_SENSE_PTR(sp, sense_ptr);
2462         SET_CMD_SENSE_LEN(sp, sense_len);
2463
2464         /* Place command on done queue. */
2465         if (sense_len == 0) {
2466                 rsp->status_srb = NULL;
2467                 sp->done(ha, sp, cp->result);
2468         }
2469 }
2470
2471 /**
2472  * qla2x00_error_entry() - Process an error entry.
2473  * @ha: SCSI driver HA context
2474  * @pkt: Entry pointer
2475  */
2476 static void
2477 qla2x00_error_entry(scsi_qla_host_t *vha, struct rsp_que *rsp, sts_entry_t *pkt)
2478 {
2479         srb_t *sp;
2480         struct qla_hw_data *ha = vha->hw;
2481         const char func[] = "ERROR-IOCB";
2482         uint16_t que = MSW(pkt->handle);
2483         struct req_que *req = NULL;
2484         int res = DID_ERROR << 16;
2485
2486         ql_dbg(ql_dbg_async, vha, 0x502a,
2487             "type of error status in response: 0x%x\n", pkt->entry_status);
2488
2489         if (que >= ha->max_req_queues || !ha->req_q_map[que])
2490                 goto fatal;
2491
2492         req = ha->req_q_map[que];
2493
2494         if (pkt->entry_status & RF_BUSY)
2495                 res = DID_BUS_BUSY << 16;
2496
2497         if (pkt->entry_type == NOTIFY_ACK_TYPE &&
2498             pkt->handle == QLA_TGT_SKIP_HANDLE)
2499                 return;
2500
2501         sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2502         if (sp) {
2503                 sp->done(ha, sp, res);
2504                 return;
2505         }
2506 fatal:
2507         ql_log(ql_log_warn, vha, 0x5030,
2508             "Error entry - invalid handle/queue (%04x).\n", que);
2509 }
2510
2511 /**
2512  * qla24xx_mbx_completion() - Process mailbox command completions.
2513  * @ha: SCSI driver HA context
2514  * @mb0: Mailbox0 register
2515  */
2516 static void
2517 qla24xx_mbx_completion(scsi_qla_host_t *vha, uint16_t mb0)
2518 {
2519         uint16_t        cnt;
2520         uint32_t        mboxes;
2521         uint16_t __iomem *wptr;
2522         struct qla_hw_data *ha = vha->hw;
2523         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2524
2525         /* Read all mbox registers? */
2526         mboxes = (1 << ha->mbx_count) - 1;
2527         if (!ha->mcp)
2528                 ql_dbg(ql_dbg_async, vha, 0x504e, "MBX pointer ERROR.\n");
2529         else
2530                 mboxes = ha->mcp->in_mb;
2531
2532         /* Load return mailbox registers. */
2533         ha->flags.mbox_int = 1;
2534         ha->mailbox_out[0] = mb0;
2535         mboxes >>= 1;
2536         wptr = (uint16_t __iomem *)&reg->mailbox1;
2537
2538         for (cnt = 1; cnt < ha->mbx_count; cnt++) {
2539                 if (mboxes & BIT_0)
2540                         ha->mailbox_out[cnt] = RD_REG_WORD(wptr);
2541
2542                 mboxes >>= 1;
2543                 wptr++;
2544         }
2545 }
2546
2547 static void
2548 qla24xx_abort_iocb_entry(scsi_qla_host_t *vha, struct req_que *req,
2549         struct abort_entry_24xx *pkt)
2550 {
2551         const char func[] = "ABT_IOCB";
2552         srb_t *sp;
2553         struct srb_iocb *abt;
2554
2555         sp = qla2x00_get_sp_from_handle(vha, func, req, pkt);
2556         if (!sp)
2557                 return;
2558
2559         abt = &sp->u.iocb_cmd;
2560         abt->u.abt.comp_status = le32_to_cpu(pkt->nport_handle);
2561         sp->done(vha, sp, 0);
2562 }
2563
2564 /**
2565  * qla24xx_process_response_queue() - Process response queue entries.
2566  * @ha: SCSI driver HA context
2567  */
2568 void qla24xx_process_response_queue(struct scsi_qla_host *vha,
2569         struct rsp_que *rsp)
2570 {
2571         struct sts_entry_24xx *pkt;
2572         struct qla_hw_data *ha = vha->hw;
2573
2574         if (!vha->flags.online)
2575                 return;
2576
2577         while (rsp->ring_ptr->signature != RESPONSE_PROCESSED) {
2578                 pkt = (struct sts_entry_24xx *)rsp->ring_ptr;
2579
2580                 rsp->ring_index++;
2581                 if (rsp->ring_index == rsp->length) {
2582                         rsp->ring_index = 0;
2583                         rsp->ring_ptr = rsp->ring;
2584                 } else {
2585                         rsp->ring_ptr++;
2586                 }
2587
2588                 if (pkt->entry_status != 0) {
2589                         qla2x00_error_entry(vha, rsp, (sts_entry_t *) pkt);
2590
2591                         if (qlt_24xx_process_response_error(vha, pkt))
2592                                 goto process_err;
2593
2594                         ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2595                         wmb();
2596                         continue;
2597                 }
2598 process_err:
2599
2600                 switch (pkt->entry_type) {
2601                 case STATUS_TYPE:
2602                         qla2x00_status_entry(vha, rsp, pkt);
2603                         break;
2604                 case STATUS_CONT_TYPE:
2605                         qla2x00_status_cont_entry(rsp, (sts_cont_entry_t *)pkt);
2606                         break;
2607                 case VP_RPT_ID_IOCB_TYPE:
2608                         qla24xx_report_id_acquisition(vha,
2609                             (struct vp_rpt_id_entry_24xx *)pkt);
2610                         break;
2611                 case LOGINOUT_PORT_IOCB_TYPE:
2612                         qla24xx_logio_entry(vha, rsp->req,
2613                             (struct logio_entry_24xx *)pkt);
2614                         break;
2615                 case CT_IOCB_TYPE:
2616                         qla24xx_els_ct_entry(vha, rsp->req, pkt, CT_IOCB_TYPE);
2617                         break;
2618                 case ELS_IOCB_TYPE:
2619                         qla24xx_els_ct_entry(vha, rsp->req, pkt, ELS_IOCB_TYPE);
2620                         break;
2621                 case ABTS_RECV_24XX:
2622                         if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
2623                                 /* ensure that the ATIO queue is empty */
2624                                 qlt_handle_abts_recv(vha, (response_t *)pkt);
2625                                 break;
2626                         } else {
2627                                 /* drop through */
2628                                 qlt_24xx_process_atio_queue(vha, 1);
2629                         }
2630                 case ABTS_RESP_24XX:
2631                 case CTIO_TYPE7:
2632                 case NOTIFY_ACK_TYPE:
2633                 case CTIO_CRC2:
2634                         qlt_response_pkt_all_vps(vha, (response_t *)pkt);
2635                         break;
2636                 case MARKER_TYPE:
2637                         /* Do nothing in this case, this check is to prevent it
2638                          * from falling into default case
2639                          */
2640                         break;
2641                 case ABORT_IOCB_TYPE:
2642                         qla24xx_abort_iocb_entry(vha, rsp->req,
2643                             (struct abort_entry_24xx *)pkt);
2644                         break;
2645                 default:
2646                         /* Type Not Supported. */
2647                         ql_dbg(ql_dbg_async, vha, 0x5042,
2648                             "Received unknown response pkt type %x "
2649                             "entry status=%x.\n",
2650                             pkt->entry_type, pkt->entry_status);
2651                         break;
2652                 }
2653                 ((response_t *)pkt)->signature = RESPONSE_PROCESSED;
2654                 wmb();
2655         }
2656
2657         /* Adjust ring index */
2658         if (IS_P3P_TYPE(ha)) {
2659                 struct device_reg_82xx __iomem *reg = &ha->iobase->isp82;
2660                 WRT_REG_DWORD(&reg->rsp_q_out[0], rsp->ring_index);
2661         } else
2662                 WRT_REG_DWORD(rsp->rsp_q_out, rsp->ring_index);
2663 }
2664
2665 static void
2666 qla2xxx_check_risc_status(scsi_qla_host_t *vha)
2667 {
2668         int rval;
2669         uint32_t cnt;
2670         struct qla_hw_data *ha = vha->hw;
2671         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2672
2673         if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2674             !IS_QLA27XX(ha))
2675                 return;
2676
2677         rval = QLA_SUCCESS;
2678         WRT_REG_DWORD(&reg->iobase_addr, 0x7C00);
2679         RD_REG_DWORD(&reg->iobase_addr);
2680         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
2681         for (cnt = 10000; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
2682             rval == QLA_SUCCESS; cnt--) {
2683                 if (cnt) {
2684                         WRT_REG_DWORD(&reg->iobase_window, 0x0001);
2685                         udelay(10);
2686                 } else
2687                         rval = QLA_FUNCTION_TIMEOUT;
2688         }
2689         if (rval == QLA_SUCCESS)
2690                 goto next_test;
2691
2692         rval = QLA_SUCCESS;
2693         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
2694         for (cnt = 100; (RD_REG_DWORD(&reg->iobase_window) & BIT_0) == 0 &&
2695             rval == QLA_SUCCESS; cnt--) {
2696                 if (cnt) {
2697                         WRT_REG_DWORD(&reg->iobase_window, 0x0003);
2698                         udelay(10);
2699                 } else
2700                         rval = QLA_FUNCTION_TIMEOUT;
2701         }
2702         if (rval != QLA_SUCCESS)
2703                 goto done;
2704
2705 next_test:
2706         if (RD_REG_DWORD(&reg->iobase_c8) & BIT_3)
2707                 ql_log(ql_log_info, vha, 0x504c,
2708                     "Additional code -- 0x55AA.\n");
2709
2710 done:
2711         WRT_REG_DWORD(&reg->iobase_window, 0x0000);
2712         RD_REG_DWORD(&reg->iobase_window);
2713 }
2714
2715 /**
2716  * qla24xx_intr_handler() - Process interrupts for the ISP23xx and ISP24xx.
2717  * @irq:
2718  * @dev_id: SCSI driver HA context
2719  *
2720  * Called by system whenever the host adapter generates an interrupt.
2721  *
2722  * Returns handled flag.
2723  */
2724 irqreturn_t
2725 qla24xx_intr_handler(int irq, void *dev_id)
2726 {
2727         scsi_qla_host_t *vha;
2728         struct qla_hw_data *ha;
2729         struct device_reg_24xx __iomem *reg;
2730         int             status;
2731         unsigned long   iter;
2732         uint32_t        stat;
2733         uint32_t        hccr;
2734         uint16_t        mb[8];
2735         struct rsp_que *rsp;
2736         unsigned long   flags;
2737
2738         rsp = (struct rsp_que *) dev_id;
2739         if (!rsp) {
2740                 ql_log(ql_log_info, NULL, 0x5059,
2741                     "%s: NULL response queue pointer.\n", __func__);
2742                 return IRQ_NONE;
2743         }
2744
2745         ha = rsp->hw;
2746         reg = &ha->iobase->isp24;
2747         status = 0;
2748
2749         if (unlikely(pci_channel_offline(ha->pdev)))
2750                 return IRQ_HANDLED;
2751
2752         spin_lock_irqsave(&ha->hardware_lock, flags);
2753         vha = pci_get_drvdata(ha->pdev);
2754         for (iter = 50; iter--; ) {
2755                 stat = RD_REG_DWORD(&reg->host_status);
2756                 if (qla2x00_check_reg32_for_disconnect(vha, stat))
2757                         break;
2758                 if (stat & HSRX_RISC_PAUSED) {
2759                         if (unlikely(pci_channel_offline(ha->pdev)))
2760                                 break;
2761
2762                         hccr = RD_REG_DWORD(&reg->hccr);
2763
2764                         ql_log(ql_log_warn, vha, 0x504b,
2765                             "RISC paused -- HCCR=%x, Dumping firmware.\n",
2766                             hccr);
2767
2768                         qla2xxx_check_risc_status(vha);
2769
2770                         ha->isp_ops->fw_dump(vha, 1);
2771                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2772                         break;
2773                 } else if ((stat & HSRX_RISC_INT) == 0)
2774                         break;
2775
2776                 switch (stat & 0xff) {
2777                 case INTR_ROM_MB_SUCCESS:
2778                 case INTR_ROM_MB_FAILED:
2779                 case INTR_MB_SUCCESS:
2780                 case INTR_MB_FAILED:
2781                         qla24xx_mbx_completion(vha, MSW(stat));
2782                         status |= MBX_INTERRUPT;
2783
2784                         break;
2785                 case INTR_ASYNC_EVENT:
2786                         mb[0] = MSW(stat);
2787                         mb[1] = RD_REG_WORD(&reg->mailbox1);
2788                         mb[2] = RD_REG_WORD(&reg->mailbox2);
2789                         mb[3] = RD_REG_WORD(&reg->mailbox3);
2790                         qla2x00_async_event(vha, rsp, mb);
2791                         break;
2792                 case INTR_RSP_QUE_UPDATE:
2793                 case INTR_RSP_QUE_UPDATE_83XX:
2794                         qla24xx_process_response_queue(vha, rsp);
2795                         break;
2796                 case INTR_ATIO_QUE_UPDATE:{
2797                         unsigned long flags2;
2798                         spin_lock_irqsave(&ha->tgt.atio_lock, flags2);
2799                         qlt_24xx_process_atio_queue(vha, 1);
2800                         spin_unlock_irqrestore(&ha->tgt.atio_lock, flags2);
2801                         break;
2802                 }
2803                 case INTR_ATIO_RSP_QUE_UPDATE: {
2804                         unsigned long flags2;
2805                         spin_lock_irqsave(&ha->tgt.atio_lock, flags2);
2806                         qlt_24xx_process_atio_queue(vha, 1);
2807                         spin_unlock_irqrestore(&ha->tgt.atio_lock, flags2);
2808
2809                         qla24xx_process_response_queue(vha, rsp);
2810                         break;
2811                 }
2812                 default:
2813                         ql_dbg(ql_dbg_async, vha, 0x504f,
2814                             "Unrecognized interrupt type (%d).\n", stat * 0xff);
2815                         break;
2816                 }
2817                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2818                 RD_REG_DWORD_RELAXED(&reg->hccr);
2819                 if (unlikely(IS_QLA83XX(ha) && (ha->pdev->revision == 1)))
2820                         ndelay(3500);
2821         }
2822         qla2x00_handle_mbx_completion(ha, status);
2823         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2824
2825         return IRQ_HANDLED;
2826 }
2827
2828 static irqreturn_t
2829 qla24xx_msix_rsp_q(int irq, void *dev_id)
2830 {
2831         struct qla_hw_data *ha;
2832         struct rsp_que *rsp;
2833         struct device_reg_24xx __iomem *reg;
2834         struct scsi_qla_host *vha;
2835         unsigned long flags;
2836         uint32_t stat = 0;
2837
2838         rsp = (struct rsp_que *) dev_id;
2839         if (!rsp) {
2840                 ql_log(ql_log_info, NULL, 0x505a,
2841                     "%s: NULL response queue pointer.\n", __func__);
2842                 return IRQ_NONE;
2843         }
2844         ha = rsp->hw;
2845         reg = &ha->iobase->isp24;
2846
2847         spin_lock_irqsave(&ha->hardware_lock, flags);
2848
2849         vha = pci_get_drvdata(ha->pdev);
2850         /*
2851          * Use host_status register to check to PCI disconnection before we
2852          * we process the response queue.
2853          */
2854         stat = RD_REG_DWORD(&reg->host_status);
2855         if (qla2x00_check_reg32_for_disconnect(vha, stat))
2856                 goto out;
2857         qla24xx_process_response_queue(vha, rsp);
2858         if (!ha->flags.disable_msix_handshake) {
2859                 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_INT);
2860                 RD_REG_DWORD_RELAXED(&reg->hccr);
2861         }
2862 out:
2863         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2864
2865         return IRQ_HANDLED;
2866 }
2867
2868 static irqreturn_t
2869 qla24xx_msix_default(int irq, void *dev_id)
2870 {
2871         scsi_qla_host_t *vha;
2872         struct qla_hw_data *ha;
2873         struct rsp_que *rsp;
2874         struct device_reg_24xx __iomem *reg;
2875         int             status;
2876         uint32_t        stat;
2877         uint32_t        hccr;
2878         uint16_t        mb[8];
2879         unsigned long flags;
2880
2881         rsp = (struct rsp_que *) dev_id;
2882         if (!rsp) {
2883                 ql_log(ql_log_info, NULL, 0x505c,
2884                     "%s: NULL response queue pointer.\n", __func__);
2885                 return IRQ_NONE;
2886         }
2887         ha = rsp->hw;
2888         reg = &ha->iobase->isp24;
2889         status = 0;
2890
2891         spin_lock_irqsave(&ha->hardware_lock, flags);
2892         vha = pci_get_drvdata(ha->pdev);
2893         do {
2894                 stat = RD_REG_DWORD(&reg->host_status);
2895                 if (qla2x00_check_reg32_for_disconnect(vha, stat))
2896                         break;
2897                 if (stat & HSRX_RISC_PAUSED) {
2898                         if (unlikely(pci_channel_offline(ha->pdev)))
2899                                 break;
2900
2901                         hccr = RD_REG_DWORD(&reg->hccr);
2902
2903                         ql_log(ql_log_info, vha, 0x5050,
2904                             "RISC paused -- HCCR=%x, Dumping firmware.\n",
2905                             hccr);
2906
2907                         qla2xxx_check_risc_status(vha);
2908
2909                         ha->isp_ops->fw_dump(vha, 1);
2910                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2911                         break;
2912                 } else if ((stat & HSRX_RISC_INT) == 0)
2913                         break;
2914
2915                 switch (stat & 0xff) {
2916                 case INTR_ROM_MB_SUCCESS:
2917                 case INTR_ROM_MB_FAILED:
2918                 case INTR_MB_SUCCESS:
2919                 case INTR_MB_FAILED:
2920                         qla24xx_mbx_completion(vha, MSW(stat));
2921                         status |= MBX_INTERRUPT;
2922
2923                         break;
2924                 case INTR_ASYNC_EVENT:
2925                         mb[0] = MSW(stat);
2926                         mb[1] = RD_REG_WORD(&reg->mailbox1);
2927                         mb[2] = RD_REG_WORD(&reg->mailbox2);
2928                         mb[3] = RD_REG_WORD(&reg->mailbox3);
2929                         qla2x00_async_event(vha, rsp, mb);
2930                         break;