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