]> Pileus Git - ~andy/linux/blob - drivers/scsi/qla4xxx/ql4_isr.c
Merge remote-tracking branch 'nouveau/drm-nouveau-next' into drm-fixes
[~andy/linux] / drivers / scsi / qla4xxx / ql4_isr.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2013 QLogic Corporation
4  *
5  * See LICENSE.qla4xxx for copyright and licensing details.
6  */
7
8 #include "ql4_def.h"
9 #include "ql4_glbl.h"
10 #include "ql4_dbg.h"
11 #include "ql4_inline.h"
12
13 /**
14  * qla4xxx_copy_sense - copy sense data into cmd sense buffer
15  * @ha: Pointer to host adapter structure.
16  * @sts_entry: Pointer to status entry structure.
17  * @srb: Pointer to srb structure.
18  **/
19 static void qla4xxx_copy_sense(struct scsi_qla_host *ha,
20                                struct status_entry *sts_entry,
21                                struct srb *srb)
22 {
23         struct scsi_cmnd *cmd = srb->cmd;
24         uint16_t sense_len;
25
26         memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
27         sense_len = le16_to_cpu(sts_entry->senseDataByteCnt);
28         if (sense_len == 0) {
29                 DEBUG2(ql4_printk(KERN_INFO, ha, "scsi%ld:%d:%d:%d: %s:"
30                                   " sense len 0\n", ha->host_no,
31                                   cmd->device->channel, cmd->device->id,
32                                   cmd->device->lun, __func__));
33                 ha->status_srb = NULL;
34                 return;
35         }
36         /* Save total available sense length,
37          * not to exceed cmd's sense buffer size */
38         sense_len = min_t(uint16_t, sense_len, SCSI_SENSE_BUFFERSIZE);
39         srb->req_sense_ptr = cmd->sense_buffer;
40         srb->req_sense_len = sense_len;
41
42         /* Copy sense from sts_entry pkt */
43         sense_len = min_t(uint16_t, sense_len, IOCB_MAX_SENSEDATA_LEN);
44         memcpy(cmd->sense_buffer, sts_entry->senseData, sense_len);
45
46         DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: %s: sense key = %x, "
47                 "ASL= %02x, ASC/ASCQ = %02x/%02x\n", ha->host_no,
48                 cmd->device->channel, cmd->device->id,
49                 cmd->device->lun, __func__,
50                 sts_entry->senseData[2] & 0x0f,
51                 sts_entry->senseData[7],
52                 sts_entry->senseData[12],
53                 sts_entry->senseData[13]));
54
55         DEBUG5(qla4xxx_dump_buffer(cmd->sense_buffer, sense_len));
56         srb->flags |= SRB_GOT_SENSE;
57
58         /* Update srb, in case a sts_cont pkt follows */
59         srb->req_sense_ptr += sense_len;
60         srb->req_sense_len -= sense_len;
61         if (srb->req_sense_len != 0)
62                 ha->status_srb = srb;
63         else
64                 ha->status_srb = NULL;
65 }
66
67 /**
68  * qla4xxx_status_cont_entry - Process a Status Continuations entry.
69  * @ha: SCSI driver HA context
70  * @sts_cont: Entry pointer
71  *
72  * Extended sense data.
73  */
74 static void
75 qla4xxx_status_cont_entry(struct scsi_qla_host *ha,
76                           struct status_cont_entry *sts_cont)
77 {
78         struct srb *srb = ha->status_srb;
79         struct scsi_cmnd *cmd;
80         uint16_t sense_len;
81
82         if (srb == NULL)
83                 return;
84
85         cmd = srb->cmd;
86         if (cmd == NULL) {
87                 DEBUG2(printk(KERN_INFO "scsi%ld: %s: Cmd already returned "
88                         "back to OS srb=%p srb->state:%d\n", ha->host_no,
89                         __func__, srb, srb->state));
90                 ha->status_srb = NULL;
91                 return;
92         }
93
94         /* Copy sense data. */
95         sense_len = min_t(uint16_t, srb->req_sense_len,
96                           IOCB_MAX_EXT_SENSEDATA_LEN);
97         memcpy(srb->req_sense_ptr, sts_cont->ext_sense_data, sense_len);
98         DEBUG5(qla4xxx_dump_buffer(srb->req_sense_ptr, sense_len));
99
100         srb->req_sense_ptr += sense_len;
101         srb->req_sense_len -= sense_len;
102
103         /* Place command on done queue. */
104         if (srb->req_sense_len == 0) {
105                 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
106                 ha->status_srb = NULL;
107         }
108 }
109
110 /**
111  * qla4xxx_status_entry - processes status IOCBs
112  * @ha: Pointer to host adapter structure.
113  * @sts_entry: Pointer to status entry structure.
114  **/
115 static void qla4xxx_status_entry(struct scsi_qla_host *ha,
116                                  struct status_entry *sts_entry)
117 {
118         uint8_t scsi_status;
119         struct scsi_cmnd *cmd;
120         struct srb *srb;
121         struct ddb_entry *ddb_entry;
122         uint32_t residual;
123
124         srb = qla4xxx_del_from_active_array(ha, le32_to_cpu(sts_entry->handle));
125         if (!srb) {
126                 ql4_printk(KERN_WARNING, ha, "%s invalid status entry: "
127                            "handle=0x%0x, srb=%p\n", __func__,
128                            sts_entry->handle, srb);
129                 if (is_qla80XX(ha))
130                         set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
131                 else
132                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
133                 return;
134         }
135
136         cmd = srb->cmd;
137         if (cmd == NULL) {
138                 DEBUG2(printk("scsi%ld: %s: Command already returned back to "
139                               "OS pkt->handle=%d srb=%p srb->state:%d\n",
140                               ha->host_no, __func__, sts_entry->handle,
141                               srb, srb->state));
142                 ql4_printk(KERN_WARNING, ha, "Command is NULL:"
143                     " already returned to OS (srb=%p)\n", srb);
144                 return;
145         }
146
147         ddb_entry = srb->ddb;
148         if (ddb_entry == NULL) {
149                 cmd->result = DID_NO_CONNECT << 16;
150                 goto status_entry_exit;
151         }
152
153         residual = le32_to_cpu(sts_entry->residualByteCnt);
154
155         /* Translate ISP error to a Linux SCSI error. */
156         scsi_status = sts_entry->scsiStatus;
157         switch (sts_entry->completionStatus) {
158         case SCS_COMPLETE:
159
160                 if (sts_entry->iscsiFlags & ISCSI_FLAG_RESIDUAL_OVER) {
161                         cmd->result = DID_ERROR << 16;
162                         break;
163                 }
164
165                 if (sts_entry->iscsiFlags &ISCSI_FLAG_RESIDUAL_UNDER) {
166                         scsi_set_resid(cmd, residual);
167                         if (!scsi_status && ((scsi_bufflen(cmd) - residual) <
168                                 cmd->underflow)) {
169
170                                 cmd->result = DID_ERROR << 16;
171
172                                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: "
173                                         "Mid-layer Data underrun0, "
174                                         "xferlen = 0x%x, "
175                                         "residual = 0x%x\n", ha->host_no,
176                                         cmd->device->channel,
177                                         cmd->device->id,
178                                         cmd->device->lun, __func__,
179                                         scsi_bufflen(cmd), residual));
180                                 break;
181                         }
182                 }
183
184                 cmd->result = DID_OK << 16 | scsi_status;
185
186                 if (scsi_status != SCSI_CHECK_CONDITION)
187                         break;
188
189                 /* Copy Sense Data into sense buffer. */
190                 qla4xxx_copy_sense(ha, sts_entry, srb);
191                 break;
192
193         case SCS_INCOMPLETE:
194                 /* Always set the status to DID_ERROR, since
195                  * all conditions result in that status anyway */
196                 cmd->result = DID_ERROR << 16;
197                 break;
198
199         case SCS_RESET_OCCURRED:
200                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: Device RESET occurred\n",
201                               ha->host_no, cmd->device->channel,
202                               cmd->device->id, cmd->device->lun, __func__));
203
204                 cmd->result = DID_RESET << 16;
205                 break;
206
207         case SCS_ABORTED:
208                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: Abort occurred\n",
209                               ha->host_no, cmd->device->channel,
210                               cmd->device->id, cmd->device->lun, __func__));
211
212                 cmd->result = DID_RESET << 16;
213                 break;
214
215         case SCS_TIMEOUT:
216                 DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: Timeout\n",
217                               ha->host_no, cmd->device->channel,
218                               cmd->device->id, cmd->device->lun));
219
220                 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
221
222                 /*
223                  * Mark device missing so that we won't continue to send
224                  * I/O to this device.  We should get a ddb state change
225                  * AEN soon.
226                  */
227                 if (iscsi_is_session_online(ddb_entry->sess))
228                         qla4xxx_mark_device_missing(ddb_entry->sess);
229                 break;
230
231         case SCS_DATA_UNDERRUN:
232         case SCS_DATA_OVERRUN:
233                 if ((sts_entry->iscsiFlags & ISCSI_FLAG_RESIDUAL_OVER) ||
234                      (sts_entry->completionStatus == SCS_DATA_OVERRUN)) {
235                         DEBUG2(printk("scsi%ld:%d:%d:%d: %s: " "Data overrun\n",
236                                       ha->host_no,
237                                       cmd->device->channel, cmd->device->id,
238                                       cmd->device->lun, __func__));
239
240                         cmd->result = DID_ERROR << 16;
241                         break;
242                 }
243
244                 scsi_set_resid(cmd, residual);
245
246                 if (sts_entry->iscsiFlags & ISCSI_FLAG_RESIDUAL_UNDER) {
247
248                         /* Both the firmware and target reported UNDERRUN:
249                          *
250                          * MID-LAYER UNDERFLOW case:
251                          * Some kernels do not properly detect midlayer
252                          * underflow, so we manually check it and return
253                          * ERROR if the minimum required data was not
254                          * received.
255                          *
256                          * ALL OTHER cases:
257                          * Fall thru to check scsi_status
258                          */
259                         if (!scsi_status && (scsi_bufflen(cmd) - residual) <
260                             cmd->underflow) {
261                                 DEBUG2(ql4_printk(KERN_INFO, ha,
262                                                   "scsi%ld:%d:%d:%d: %s: Mid-layer Data underrun, xferlen = 0x%x,residual = 0x%x\n",
263                                                    ha->host_no,
264                                                    cmd->device->channel,
265                                                    cmd->device->id,
266                                                    cmd->device->lun, __func__,
267                                                    scsi_bufflen(cmd),
268                                                    residual));
269
270                                 cmd->result = DID_ERROR << 16;
271                                 break;
272                         }
273
274                 } else if (scsi_status != SAM_STAT_TASK_SET_FULL &&
275                            scsi_status != SAM_STAT_BUSY) {
276
277                         /*
278                          * The firmware reports UNDERRUN, but the target does
279                          * not report it:
280                          *
281                          *   scsi_status     |    host_byte       device_byte
282                          *                   |     (19:16)          (7:0)
283                          *   =============   |    =========       ===========
284                          *   TASK_SET_FULL   |    DID_OK          scsi_status
285                          *   BUSY            |    DID_OK          scsi_status
286                          *   ALL OTHERS      |    DID_ERROR       scsi_status
287                          *
288                          *   Note: If scsi_status is task set full or busy,
289                          *   then this else if would fall thru to check the
290                          *   scsi_status and return DID_OK.
291                          */
292
293                         DEBUG2(ql4_printk(KERN_INFO, ha,
294                                           "scsi%ld:%d:%d:%d: %s: Dropped frame(s) detected (0x%x of 0x%x bytes).\n",
295                                           ha->host_no,
296                                           cmd->device->channel,
297                                           cmd->device->id,
298                                           cmd->device->lun, __func__,
299                                           residual,
300                                           scsi_bufflen(cmd)));
301
302                         cmd->result = DID_ERROR << 16 | scsi_status;
303                         goto check_scsi_status;
304                 }
305
306                 cmd->result = DID_OK << 16 | scsi_status;
307
308 check_scsi_status:
309                 if (scsi_status == SAM_STAT_CHECK_CONDITION)
310                         qla4xxx_copy_sense(ha, sts_entry, srb);
311
312                 break;
313
314         case SCS_DEVICE_LOGGED_OUT:
315         case SCS_DEVICE_UNAVAILABLE:
316                 DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: SCS_DEVICE "
317                     "state: 0x%x\n", ha->host_no,
318                     cmd->device->channel, cmd->device->id,
319                     cmd->device->lun, sts_entry->completionStatus));
320                 /*
321                  * Mark device missing so that we won't continue to
322                  * send I/O to this device.  We should get a ddb
323                  * state change AEN soon.
324                  */
325                 if (iscsi_is_session_online(ddb_entry->sess))
326                         qla4xxx_mark_device_missing(ddb_entry->sess);
327
328                 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
329                 break;
330
331         case SCS_QUEUE_FULL:
332                 /*
333                  * SCSI Mid-Layer handles device queue full
334                  */
335                 cmd->result = DID_OK << 16 | sts_entry->scsiStatus;
336                 DEBUG2(printk("scsi%ld:%d:%d: %s: QUEUE FULL detected "
337                               "compl=%02x, scsi=%02x, state=%02x, iFlags=%02x,"
338                               " iResp=%02x\n", ha->host_no, cmd->device->id,
339                               cmd->device->lun, __func__,
340                               sts_entry->completionStatus,
341                               sts_entry->scsiStatus, sts_entry->state_flags,
342                               sts_entry->iscsiFlags,
343                               sts_entry->iscsiResponse));
344                 break;
345
346         default:
347                 cmd->result = DID_ERROR << 16;
348                 break;
349         }
350
351 status_entry_exit:
352
353         /* complete the request, if not waiting for status_continuation pkt */
354         srb->cc_stat = sts_entry->completionStatus;
355         if (ha->status_srb == NULL)
356                 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
357 }
358
359 /**
360  * qla4xxx_passthru_status_entry - processes passthru status IOCBs (0x3C)
361  * @ha: Pointer to host adapter structure.
362  * @sts_entry: Pointer to status entry structure.
363  **/
364 static void qla4xxx_passthru_status_entry(struct scsi_qla_host *ha,
365                                           struct passthru_status *sts_entry)
366 {
367         struct iscsi_task *task;
368         struct ddb_entry *ddb_entry;
369         struct ql4_task_data *task_data;
370         struct iscsi_cls_conn *cls_conn;
371         struct iscsi_conn *conn;
372         itt_t itt;
373         uint32_t fw_ddb_index;
374
375         itt = sts_entry->handle;
376         fw_ddb_index = le32_to_cpu(sts_entry->target);
377
378         ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, fw_ddb_index);
379
380         if (ddb_entry == NULL) {
381                 ql4_printk(KERN_ERR, ha, "%s: Invalid target index = 0x%x\n",
382                            __func__, sts_entry->target);
383                 return;
384         }
385
386         cls_conn = ddb_entry->conn;
387         conn = cls_conn->dd_data;
388         spin_lock(&conn->session->lock);
389         task = iscsi_itt_to_task(conn, itt);
390         spin_unlock(&conn->session->lock);
391
392         if (task == NULL) {
393                 ql4_printk(KERN_ERR, ha, "%s: Task is NULL\n", __func__);
394                 return;
395         }
396
397         task_data = task->dd_data;
398         memcpy(&task_data->sts, sts_entry, sizeof(struct passthru_status));
399         ha->iocb_cnt -= task_data->iocb_req_cnt;
400         queue_work(ha->task_wq, &task_data->task_work);
401 }
402
403 static struct mrb *qla4xxx_del_mrb_from_active_array(struct scsi_qla_host *ha,
404                                                      uint32_t index)
405 {
406         struct mrb *mrb = NULL;
407
408         /* validate handle and remove from active array */
409         if (index >= MAX_MRB)
410                 return mrb;
411
412         mrb = ha->active_mrb_array[index];
413         ha->active_mrb_array[index] = NULL;
414         if (!mrb)
415                 return mrb;
416
417         /* update counters */
418         ha->iocb_cnt -= mrb->iocb_cnt;
419
420         return mrb;
421 }
422
423 static void qla4xxx_mbox_status_entry(struct scsi_qla_host *ha,
424                                       struct mbox_status_iocb *mbox_sts_entry)
425 {
426         struct mrb *mrb;
427         uint32_t status;
428         uint32_t data_size;
429
430         mrb = qla4xxx_del_mrb_from_active_array(ha,
431                                         le32_to_cpu(mbox_sts_entry->handle));
432
433         if (mrb == NULL) {
434                 ql4_printk(KERN_WARNING, ha, "%s: mrb[%d] is null\n", __func__,
435                            mbox_sts_entry->handle);
436                 return;
437         }
438
439         switch (mrb->mbox_cmd) {
440         case MBOX_CMD_PING:
441                 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: mbox_cmd = 0x%x, "
442                                   "mbox_sts[0] = 0x%x, mbox_sts[6] = 0x%x\n",
443                                   __func__, mrb->mbox_cmd,
444                                   mbox_sts_entry->out_mbox[0],
445                                   mbox_sts_entry->out_mbox[6]));
446
447                 if (mbox_sts_entry->out_mbox[0] == MBOX_STS_COMMAND_COMPLETE)
448                         status = ISCSI_PING_SUCCESS;
449                 else
450                         status = mbox_sts_entry->out_mbox[6];
451
452                 data_size = sizeof(mbox_sts_entry->out_mbox);
453
454                 qla4xxx_post_ping_evt_work(ha, status, mrb->pid, data_size,
455                                         (uint8_t *) mbox_sts_entry->out_mbox);
456                 break;
457
458         default:
459                 DEBUG2(ql4_printk(KERN_WARNING, ha, "%s: invalid mbox_cmd = "
460                                   "0x%x\n", __func__, mrb->mbox_cmd));
461         }
462
463         kfree(mrb);
464         return;
465 }
466
467 /**
468  * qla4xxx_process_response_queue - process response queue completions
469  * @ha: Pointer to host adapter structure.
470  *
471  * This routine process response queue completions in interrupt context.
472  * Hardware_lock locked upon entry
473  **/
474 void qla4xxx_process_response_queue(struct scsi_qla_host *ha)
475 {
476         uint32_t count = 0;
477         struct srb *srb = NULL;
478         struct status_entry *sts_entry;
479
480         /* Process all responses from response queue */
481         while ((ha->response_ptr->signature != RESPONSE_PROCESSED)) {
482                 sts_entry = (struct status_entry *) ha->response_ptr;
483                 count++;
484
485                 /* Advance pointers for next entry */
486                 if (ha->response_out == (RESPONSE_QUEUE_DEPTH - 1)) {
487                         ha->response_out = 0;
488                         ha->response_ptr = ha->response_ring;
489                 } else {
490                         ha->response_out++;
491                         ha->response_ptr++;
492                 }
493
494                 /* process entry */
495                 switch (sts_entry->hdr.entryType) {
496                 case ET_STATUS:
497                         /* Common status */
498                         qla4xxx_status_entry(ha, sts_entry);
499                         break;
500
501                 case ET_PASSTHRU_STATUS:
502                         if (sts_entry->hdr.systemDefined == SD_ISCSI_PDU)
503                                 qla4xxx_passthru_status_entry(ha,
504                                         (struct passthru_status *)sts_entry);
505                         else
506                                 ql4_printk(KERN_ERR, ha,
507                                            "%s: Invalid status received\n",
508                                            __func__);
509
510                         break;
511
512                 case ET_STATUS_CONTINUATION:
513                         qla4xxx_status_cont_entry(ha,
514                                 (struct status_cont_entry *) sts_entry);
515                         break;
516
517                 case ET_COMMAND:
518                         /* ISP device queue is full. Command not
519                          * accepted by ISP.  Queue command for
520                          * later */
521
522                         srb = qla4xxx_del_from_active_array(ha,
523                                                     le32_to_cpu(sts_entry->
524                                                                 handle));
525                         if (srb == NULL)
526                                 goto exit_prq_invalid_handle;
527
528                         DEBUG2(printk("scsi%ld: %s: FW device queue full, "
529                                       "srb %p\n", ha->host_no, __func__, srb));
530
531                         /* ETRY normally by sending it back with
532                          * DID_BUS_BUSY */
533                         srb->cmd->result = DID_BUS_BUSY << 16;
534                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
535                         break;
536
537                 case ET_CONTINUE:
538                         /* Just throw away the continuation entries */
539                         DEBUG2(printk("scsi%ld: %s: Continuation entry - "
540                                       "ignoring\n", ha->host_no, __func__));
541                         break;
542
543                 case ET_MBOX_STATUS:
544                         DEBUG2(ql4_printk(KERN_INFO, ha,
545                                           "%s: mbox status IOCB\n", __func__));
546                         qla4xxx_mbox_status_entry(ha,
547                                         (struct mbox_status_iocb *)sts_entry);
548                         break;
549
550                 default:
551                         /*
552                          * Invalid entry in response queue, reset RISC
553                          * firmware.
554                          */
555                         DEBUG2(printk("scsi%ld: %s: Invalid entry %x in "
556                                       "response queue \n", ha->host_no,
557                                       __func__,
558                                       sts_entry->hdr.entryType));
559                         goto exit_prq_error;
560                 }
561                 ((struct response *)sts_entry)->signature = RESPONSE_PROCESSED;
562                 wmb();
563         }
564
565         /*
566          * Tell ISP we're done with response(s). This also clears the interrupt.
567          */
568         ha->isp_ops->complete_iocb(ha);
569
570         return;
571
572 exit_prq_invalid_handle:
573         DEBUG2(printk("scsi%ld: %s: Invalid handle(srb)=%p type=%x IOCS=%x\n",
574                       ha->host_no, __func__, srb, sts_entry->hdr.entryType,
575                       sts_entry->completionStatus));
576
577 exit_prq_error:
578         ha->isp_ops->complete_iocb(ha);
579         set_bit(DPC_RESET_HA, &ha->dpc_flags);
580 }
581
582 /**
583  * qla4_83xx_loopback_in_progress: Is loopback in progress?
584  * @ha: Pointer to host adapter structure.
585  * @ret: 1 = loopback in progress, 0 = loopback not in progress
586  **/
587 static int qla4_83xx_loopback_in_progress(struct scsi_qla_host *ha)
588 {
589         int rval = 1;
590
591         if (is_qla8032(ha) || is_qla8042(ha)) {
592                 if ((ha->idc_info.info2 & ENABLE_INTERNAL_LOOPBACK) ||
593                     (ha->idc_info.info2 & ENABLE_EXTERNAL_LOOPBACK)) {
594                         DEBUG2(ql4_printk(KERN_INFO, ha,
595                                           "%s: Loopback diagnostics in progress\n",
596                                           __func__));
597                         rval = 1;
598                 } else {
599                         DEBUG2(ql4_printk(KERN_INFO, ha,
600                                           "%s: Loopback diagnostics not in progress\n",
601                                           __func__));
602                         rval = 0;
603                 }
604         }
605
606         return rval;
607 }
608
609 /**
610  * qla4xxx_isr_decode_mailbox - decodes mailbox status
611  * @ha: Pointer to host adapter structure.
612  * @mailbox_status: Mailbox status.
613  *
614  * This routine decodes the mailbox status during the ISR.
615  * Hardware_lock locked upon entry. runs in interrupt context.
616  **/
617 static void qla4xxx_isr_decode_mailbox(struct scsi_qla_host * ha,
618                                        uint32_t mbox_status)
619 {
620         int i;
621         uint32_t mbox_sts[MBOX_AEN_REG_COUNT];
622         __le32 __iomem *mailbox_out;
623
624         if (is_qla8032(ha) || is_qla8042(ha))
625                 mailbox_out = &ha->qla4_83xx_reg->mailbox_out[0];
626         else if (is_qla8022(ha))
627                 mailbox_out = &ha->qla4_82xx_reg->mailbox_out[0];
628         else
629                 mailbox_out = &ha->reg->mailbox[0];
630
631         if ((mbox_status == MBOX_STS_BUSY) ||
632             (mbox_status == MBOX_STS_INTERMEDIATE_COMPLETION) ||
633             (mbox_status >> 12 == MBOX_COMPLETION_STATUS)) {
634                 ha->mbox_status[0] = mbox_status;
635
636                 if (test_bit(AF_MBOX_COMMAND, &ha->flags)) {
637                         /*
638                          * Copy all mailbox registers to a temporary
639                          * location and set mailbox command done flag
640                          */
641                         for (i = 0; i < ha->mbox_status_count; i++)
642                                 ha->mbox_status[i] = readl(&mailbox_out[i]);
643
644                         set_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
645
646                         if (test_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags))
647                                 complete(&ha->mbx_intr_comp);
648                 }
649         } else if (mbox_status >> 12 == MBOX_ASYNC_EVENT_STATUS) {
650                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
651                         mbox_sts[i] = readl(&mailbox_out[i]);
652
653                 /* Immediately process the AENs that don't require much work.
654                  * Only queue the database_changed AENs */
655                 if (ha->aen_log.count < MAX_AEN_ENTRIES) {
656                         for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
657                                 ha->aen_log.entry[ha->aen_log.count].mbox_sts[i] =
658                                     mbox_sts[i];
659                         ha->aen_log.count++;
660                 }
661                 switch (mbox_status) {
662                 case MBOX_ASTS_SYSTEM_ERROR:
663                         /* Log Mailbox registers */
664                         ql4_printk(KERN_INFO, ha, "%s: System Err\n", __func__);
665                         qla4xxx_dump_registers(ha);
666
667                         if ((is_qla8022(ha) && ql4xdontresethba) ||
668                             ((is_qla8032(ha) || is_qla8042(ha)) &&
669                              qla4_83xx_idc_dontreset(ha))) {
670                                 DEBUG2(printk("scsi%ld: %s:Don't Reset HBA\n",
671                                     ha->host_no, __func__));
672                         } else {
673                                 set_bit(AF_GET_CRASH_RECORD, &ha->flags);
674                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
675                         }
676                         break;
677
678                 case MBOX_ASTS_REQUEST_TRANSFER_ERROR:
679                 case MBOX_ASTS_RESPONSE_TRANSFER_ERROR:
680                 case MBOX_ASTS_NVRAM_INVALID:
681                 case MBOX_ASTS_IP_ADDRESS_CHANGED:
682                 case MBOX_ASTS_DHCP_LEASE_EXPIRED:
683                         DEBUG2(printk("scsi%ld: AEN %04x, ERROR Status, "
684                                       "Reset HA\n", ha->host_no, mbox_status));
685                         if (is_qla80XX(ha))
686                                 set_bit(DPC_RESET_HA_FW_CONTEXT,
687                                         &ha->dpc_flags);
688                         else
689                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
690                         break;
691
692                 case MBOX_ASTS_LINK_UP:
693                         set_bit(AF_LINK_UP, &ha->flags);
694                         if (test_bit(AF_INIT_DONE, &ha->flags))
695                                 set_bit(DPC_LINK_CHANGED, &ha->dpc_flags);
696
697                         ql4_printk(KERN_INFO, ha, "%s: LINK UP\n", __func__);
698                         qla4xxx_post_aen_work(ha, ISCSI_EVENT_LINKUP,
699                                               sizeof(mbox_sts),
700                                               (uint8_t *) mbox_sts);
701                         break;
702
703                 case MBOX_ASTS_LINK_DOWN:
704                         clear_bit(AF_LINK_UP, &ha->flags);
705                         if (test_bit(AF_INIT_DONE, &ha->flags)) {
706                                 set_bit(DPC_LINK_CHANGED, &ha->dpc_flags);
707                                 qla4xxx_wake_dpc(ha);
708                         }
709
710                         ql4_printk(KERN_INFO, ha, "%s: LINK DOWN\n", __func__);
711                         qla4xxx_post_aen_work(ha, ISCSI_EVENT_LINKDOWN,
712                                               sizeof(mbox_sts),
713                                               (uint8_t *) mbox_sts);
714                         break;
715
716                 case MBOX_ASTS_HEARTBEAT:
717                         ha->seconds_since_last_heartbeat = 0;
718                         break;
719
720                 case MBOX_ASTS_DHCP_LEASE_ACQUIRED:
721                         DEBUG2(printk("scsi%ld: AEN %04x DHCP LEASE "
722                                       "ACQUIRED\n", ha->host_no, mbox_status));
723                         set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
724                         break;
725
726                 case MBOX_ASTS_PROTOCOL_STATISTIC_ALARM:
727                 case MBOX_ASTS_SCSI_COMMAND_PDU_REJECTED: /* Target
728                                                            * mode
729                                                            * only */
730                 case MBOX_ASTS_UNSOLICITED_PDU_RECEIVED:  /* Connection mode */
731                 case MBOX_ASTS_IPSEC_SYSTEM_FATAL_ERROR:
732                 case MBOX_ASTS_SUBNET_STATE_CHANGE:
733                 case MBOX_ASTS_DUPLICATE_IP:
734                         /* No action */
735                         DEBUG2(printk("scsi%ld: AEN %04x\n", ha->host_no,
736                                       mbox_status));
737                         break;
738
739                 case MBOX_ASTS_IP_ADDR_STATE_CHANGED:
740                         printk("scsi%ld: AEN %04x, mbox_sts[2]=%04x, "
741                             "mbox_sts[3]=%04x\n", ha->host_no, mbox_sts[0],
742                             mbox_sts[2], mbox_sts[3]);
743
744                         /* mbox_sts[2] = Old ACB state
745                          * mbox_sts[3] = new ACB state */
746                         if ((mbox_sts[3] == ACB_STATE_VALID) &&
747                             ((mbox_sts[2] == ACB_STATE_TENTATIVE) ||
748                             (mbox_sts[2] == ACB_STATE_ACQUIRING))) {
749                                 set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
750                         } else if ((mbox_sts[3] == ACB_STATE_ACQUIRING) &&
751                                    (mbox_sts[2] == ACB_STATE_VALID)) {
752                                 if (is_qla80XX(ha))
753                                         set_bit(DPC_RESET_HA_FW_CONTEXT,
754                                                 &ha->dpc_flags);
755                                 else
756                                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
757                         } else if (mbox_sts[3] == ACB_STATE_DISABLING) {
758                                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ACB in disabling state\n",
759                                            ha->host_no, __func__);
760                         } else if ((mbox_sts[3] == ACB_STATE_UNCONFIGURED)) {
761                                 complete(&ha->disable_acb_comp);
762                                 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ACB state unconfigured\n",
763                                            ha->host_no, __func__);
764                         }
765                         break;
766
767                 case MBOX_ASTS_MAC_ADDRESS_CHANGED:
768                 case MBOX_ASTS_DNS:
769                         /* No action */
770                         DEBUG2(printk(KERN_INFO "scsi%ld: AEN %04x, "
771                                       "mbox_sts[1]=%04x, mbox_sts[2]=%04x\n",
772                                       ha->host_no, mbox_sts[0],
773                                       mbox_sts[1], mbox_sts[2]));
774                         break;
775
776                 case MBOX_ASTS_SELF_TEST_FAILED:
777                 case MBOX_ASTS_LOGIN_FAILED:
778                         /* No action */
779                         DEBUG2(printk("scsi%ld: AEN %04x, mbox_sts[1]=%04x, "
780                                       "mbox_sts[2]=%04x, mbox_sts[3]=%04x\n",
781                                       ha->host_no, mbox_sts[0], mbox_sts[1],
782                                       mbox_sts[2], mbox_sts[3]));
783                         break;
784
785                 case MBOX_ASTS_DATABASE_CHANGED:
786                         /* Queue AEN information and process it in the DPC
787                          * routine */
788                         if (ha->aen_q_count > 0) {
789
790                                 /* decrement available counter */
791                                 ha->aen_q_count--;
792
793                                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
794                                         ha->aen_q[ha->aen_in].mbox_sts[i] =
795                                             mbox_sts[i];
796
797                                 /* print debug message */
798                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x queued "
799                                               "mb1:0x%x mb2:0x%x mb3:0x%x "
800                                               "mb4:0x%x mb5:0x%x\n",
801                                               ha->host_no, ha->aen_in,
802                                               mbox_sts[0], mbox_sts[1],
803                                               mbox_sts[2], mbox_sts[3],
804                                               mbox_sts[4], mbox_sts[5]));
805
806                                 /* advance pointer */
807                                 ha->aen_in++;
808                                 if (ha->aen_in == MAX_AEN_ENTRIES)
809                                         ha->aen_in = 0;
810
811                                 /* The DPC routine will process the aen */
812                                 set_bit(DPC_AEN, &ha->dpc_flags);
813                         } else {
814                                 DEBUG2(printk("scsi%ld: %s: aen %04x, queue "
815                                               "overflowed!  AEN LOST!!\n",
816                                               ha->host_no, __func__,
817                                               mbox_sts[0]));
818
819                                 DEBUG2(printk("scsi%ld: DUMP AEN QUEUE\n",
820                                               ha->host_no));
821
822                                 for (i = 0; i < MAX_AEN_ENTRIES; i++) {
823                                         DEBUG2(printk("AEN[%d] %04x %04x %04x "
824                                                       "%04x\n", i, mbox_sts[0],
825                                                       mbox_sts[1], mbox_sts[2],
826                                                       mbox_sts[3]));
827                                 }
828                         }
829                         break;
830
831                 case MBOX_ASTS_TXSCVR_INSERTED:
832                         DEBUG2(printk(KERN_WARNING
833                             "scsi%ld: AEN %04x Transceiver"
834                             " inserted\n",  ha->host_no, mbox_sts[0]));
835                         break;
836
837                 case MBOX_ASTS_TXSCVR_REMOVED:
838                         DEBUG2(printk(KERN_WARNING
839                             "scsi%ld: AEN %04x Transceiver"
840                             " removed\n",  ha->host_no, mbox_sts[0]));
841                         break;
842
843                 case MBOX_ASTS_IDC_REQUEST_NOTIFICATION:
844                 {
845                         uint32_t opcode;
846                         if (is_qla8032(ha) || is_qla8042(ha)) {
847                                 DEBUG2(ql4_printk(KERN_INFO, ha,
848                                                   "scsi%ld: AEN %04x, mbox_sts[1]=%08x, mbox_sts[2]=%08x, mbox_sts[3]=%08x, mbox_sts[4]=%08x\n",
849                                                   ha->host_no, mbox_sts[0],
850                                                   mbox_sts[1], mbox_sts[2],
851                                                   mbox_sts[3], mbox_sts[4]));
852                                 opcode = mbox_sts[1] >> 16;
853                                 if ((opcode == MBOX_CMD_SET_PORT_CONFIG) ||
854                                     (opcode == MBOX_CMD_PORT_RESET)) {
855                                         set_bit(DPC_POST_IDC_ACK,
856                                                 &ha->dpc_flags);
857                                         ha->idc_info.request_desc = mbox_sts[1];
858                                         ha->idc_info.info1 = mbox_sts[2];
859                                         ha->idc_info.info2 = mbox_sts[3];
860                                         ha->idc_info.info3 = mbox_sts[4];
861                                         qla4xxx_wake_dpc(ha);
862                                 }
863                         }
864                         break;
865                 }
866
867                 case MBOX_ASTS_IDC_COMPLETE:
868                         if (is_qla8032(ha) || is_qla8042(ha)) {
869                                 DEBUG2(ql4_printk(KERN_INFO, ha,
870                                                   "scsi%ld: AEN %04x, mbox_sts[1]=%08x, mbox_sts[2]=%08x, mbox_sts[3]=%08x, mbox_sts[4]=%08x\n",
871                                                   ha->host_no, mbox_sts[0],
872                                                   mbox_sts[1], mbox_sts[2],
873                                                   mbox_sts[3], mbox_sts[4]));
874                                 DEBUG2(ql4_printk(KERN_INFO, ha,
875                                                   "scsi:%ld: AEN %04x IDC Complete notification\n",
876                                                   ha->host_no, mbox_sts[0]));
877
878                                 if (qla4_83xx_loopback_in_progress(ha)) {
879                                         set_bit(AF_LOOPBACK, &ha->flags);
880                                 } else {
881                                         clear_bit(AF_LOOPBACK, &ha->flags);
882                                         if (ha->saved_acb)
883                                                 set_bit(DPC_RESTORE_ACB,
884                                                         &ha->dpc_flags);
885                                 }
886                                 qla4xxx_wake_dpc(ha);
887                         }
888                         break;
889
890                 case MBOX_ASTS_IPV6_DEFAULT_ROUTER_CHANGED:
891                         DEBUG2(ql4_printk(KERN_INFO, ha,
892                                           "scsi%ld: AEN %04x, mbox_sts[1]=%08x, mbox_sts[2]=%08x, mbox_sts[3]=%08x, mbox_sts[4]=%08x mbox_sts[5]=%08x\n",
893                                           ha->host_no, mbox_sts[0], mbox_sts[1],
894                                           mbox_sts[2], mbox_sts[3], mbox_sts[4],
895                                           mbox_sts[5]));
896                         DEBUG2(ql4_printk(KERN_INFO, ha,
897                                           "scsi%ld: AEN %04x Received IPv6 default router changed notification\n",
898                                           ha->host_no, mbox_sts[0]));
899                         break;
900
901                 case MBOX_ASTS_IDC_TIME_EXTEND_NOTIFICATION:
902                         DEBUG2(ql4_printk(KERN_INFO, ha,
903                                           "scsi%ld: AEN %04x, mbox_sts[1]=%08x, mbox_sts[2]=%08x, mbox_sts[3]=%08x, mbox_sts[4]=%08x mbox_sts[5]=%08x\n",
904                                           ha->host_no, mbox_sts[0], mbox_sts[1],
905                                           mbox_sts[2], mbox_sts[3], mbox_sts[4],
906                                           mbox_sts[5]));
907                         DEBUG2(ql4_printk(KERN_INFO, ha,
908                                           "scsi%ld: AEN %04x Received IDC Extend Timeout notification\n",
909                                           ha->host_no, mbox_sts[0]));
910                         break;
911
912                 case MBOX_ASTS_INITIALIZATION_FAILED:
913                         DEBUG2(ql4_printk(KERN_INFO, ha,
914                                           "scsi%ld: AEN %04x, mbox_sts[3]=%08x\n",
915                                           ha->host_no, mbox_sts[0],
916                                           mbox_sts[3]));
917                         break;
918
919                 case MBOX_ASTS_SYSTEM_WARNING_EVENT:
920                         DEBUG2(ql4_printk(KERN_WARNING, ha,
921                                           "scsi%ld: AEN %04x, mbox_sts[1]=%08x, mbox_sts[2]=%08x, mbox_sts[3]=%08x, mbox_sts[4]=%08x mbox_sts[5]=%08x\n",
922                                           ha->host_no, mbox_sts[0], mbox_sts[1],
923                                           mbox_sts[2], mbox_sts[3], mbox_sts[4],
924                                           mbox_sts[5]));
925                         break;
926
927                 case MBOX_ASTS_DCBX_CONF_CHANGE:
928                         DEBUG2(ql4_printk(KERN_INFO, ha,
929                                           "scsi%ld: AEN %04x, mbox_sts[1]=%08x, mbox_sts[2]=%08x, mbox_sts[3]=%08x, mbox_sts[4]=%08x mbox_sts[5]=%08x\n",
930                                           ha->host_no, mbox_sts[0], mbox_sts[1],
931                                           mbox_sts[2], mbox_sts[3], mbox_sts[4],
932                                           mbox_sts[5]));
933                         DEBUG2(ql4_printk(KERN_INFO, ha,
934                                           "scsi%ld: AEN %04x Received DCBX configuration changed notification\n",
935                                           ha->host_no, mbox_sts[0]));
936                         break;
937
938                 default:
939                         DEBUG2(printk(KERN_WARNING
940                                       "scsi%ld: AEN %04x UNKNOWN\n",
941                                       ha->host_no, mbox_sts[0]));
942                         break;
943                 }
944         } else {
945                 DEBUG2(printk("scsi%ld: Unknown mailbox status %08X\n",
946                               ha->host_no, mbox_status));
947
948                 ha->mbox_status[0] = mbox_status;
949         }
950 }
951
952 void qla4_83xx_interrupt_service_routine(struct scsi_qla_host *ha,
953                                          uint32_t intr_status)
954 {
955         /* Process mailbox/asynch event interrupt.*/
956         if (intr_status) {
957                 qla4xxx_isr_decode_mailbox(ha,
958                                 readl(&ha->qla4_83xx_reg->mailbox_out[0]));
959                 /* clear the interrupt */
960                 writel(0, &ha->qla4_83xx_reg->risc_intr);
961         } else {
962                 qla4xxx_process_response_queue(ha);
963         }
964
965         /* clear the interrupt */
966         writel(0, &ha->qla4_83xx_reg->mb_int_mask);
967 }
968
969 /**
970  * qla4_82xx_interrupt_service_routine - isr
971  * @ha: pointer to host adapter structure.
972  *
973  * This is the main interrupt service routine.
974  * hardware_lock locked upon entry. runs in interrupt context.
975  **/
976 void qla4_82xx_interrupt_service_routine(struct scsi_qla_host *ha,
977     uint32_t intr_status)
978 {
979         /* Process response queue interrupt. */
980         if (intr_status & HSRX_RISC_IOCB_INT)
981                 qla4xxx_process_response_queue(ha);
982
983         /* Process mailbox/asynch event interrupt.*/
984         if (intr_status & HSRX_RISC_MB_INT)
985                 qla4xxx_isr_decode_mailbox(ha,
986                     readl(&ha->qla4_82xx_reg->mailbox_out[0]));
987
988         /* clear the interrupt */
989         writel(0, &ha->qla4_82xx_reg->host_int);
990         readl(&ha->qla4_82xx_reg->host_int);
991 }
992
993 /**
994  * qla4xxx_interrupt_service_routine - isr
995  * @ha: pointer to host adapter structure.
996  *
997  * This is the main interrupt service routine.
998  * hardware_lock locked upon entry. runs in interrupt context.
999  **/
1000 void qla4xxx_interrupt_service_routine(struct scsi_qla_host * ha,
1001                                        uint32_t intr_status)
1002 {
1003         /* Process response queue interrupt. */
1004         if (intr_status & CSR_SCSI_COMPLETION_INTR)
1005                 qla4xxx_process_response_queue(ha);
1006
1007         /* Process mailbox/asynch event  interrupt.*/
1008         if (intr_status & CSR_SCSI_PROCESSOR_INTR) {
1009                 qla4xxx_isr_decode_mailbox(ha,
1010                                            readl(&ha->reg->mailbox[0]));
1011
1012                 /* Clear Mailbox Interrupt */
1013                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
1014                        &ha->reg->ctrl_status);
1015                 readl(&ha->reg->ctrl_status);
1016         }
1017 }
1018
1019 /**
1020  * qla4_82xx_spurious_interrupt - processes spurious interrupt
1021  * @ha: pointer to host adapter structure.
1022  * @reqs_count: .
1023  *
1024  **/
1025 static void qla4_82xx_spurious_interrupt(struct scsi_qla_host *ha,
1026     uint8_t reqs_count)
1027 {
1028         if (reqs_count)
1029                 return;
1030
1031         DEBUG2(ql4_printk(KERN_INFO, ha, "Spurious Interrupt\n"));
1032         if (is_qla8022(ha)) {
1033                 writel(0, &ha->qla4_82xx_reg->host_int);
1034                 if (test_bit(AF_INTx_ENABLED, &ha->flags))
1035                         qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg,
1036                             0xfbff);
1037         }
1038         ha->spurious_int_count++;
1039 }
1040
1041 /**
1042  * qla4xxx_intr_handler - hardware interrupt handler.
1043  * @irq: Unused
1044  * @dev_id: Pointer to host adapter structure
1045  **/
1046 irqreturn_t qla4xxx_intr_handler(int irq, void *dev_id)
1047 {
1048         struct scsi_qla_host *ha;
1049         uint32_t intr_status;
1050         unsigned long flags = 0;
1051         uint8_t reqs_count = 0;
1052
1053         ha = (struct scsi_qla_host *) dev_id;
1054         if (!ha) {
1055                 DEBUG2(printk(KERN_INFO
1056                               "qla4xxx: Interrupt with NULL host ptr\n"));
1057                 return IRQ_NONE;
1058         }
1059
1060         spin_lock_irqsave(&ha->hardware_lock, flags);
1061
1062         ha->isr_count++;
1063         /*
1064          * Repeatedly service interrupts up to a maximum of
1065          * MAX_REQS_SERVICED_PER_INTR
1066          */
1067         while (1) {
1068                 /*
1069                  * Read interrupt status
1070                  */
1071                 if (ha->isp_ops->rd_shdw_rsp_q_in(ha) !=
1072                     ha->response_out)
1073                         intr_status = CSR_SCSI_COMPLETION_INTR;
1074                 else
1075                         intr_status = readl(&ha->reg->ctrl_status);
1076
1077                 if ((intr_status &
1078                     (CSR_SCSI_RESET_INTR|CSR_FATAL_ERROR|INTR_PENDING)) == 0) {
1079                         if (reqs_count == 0)
1080                                 ha->spurious_int_count++;
1081                         break;
1082                 }
1083
1084                 if (intr_status & CSR_FATAL_ERROR) {
1085                         DEBUG2(printk(KERN_INFO "scsi%ld: Fatal Error, "
1086                                       "Status 0x%04x\n", ha->host_no,
1087                                       readl(isp_port_error_status (ha))));
1088
1089                         /* Issue Soft Reset to clear this error condition.
1090                          * This will prevent the RISC from repeatedly
1091                          * interrupting the driver; thus, allowing the DPC to
1092                          * get scheduled to continue error recovery.
1093                          * NOTE: Disabling RISC interrupts does not work in
1094                          * this case, as CSR_FATAL_ERROR overrides
1095                          * CSR_SCSI_INTR_ENABLE */
1096                         if ((readl(&ha->reg->ctrl_status) &
1097                              CSR_SCSI_RESET_INTR) == 0) {
1098                                 writel(set_rmask(CSR_SOFT_RESET),
1099                                        &ha->reg->ctrl_status);
1100                                 readl(&ha->reg->ctrl_status);
1101                         }
1102
1103                         writel(set_rmask(CSR_FATAL_ERROR),
1104                                &ha->reg->ctrl_status);
1105                         readl(&ha->reg->ctrl_status);
1106
1107                         __qla4xxx_disable_intrs(ha);
1108
1109                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
1110
1111                         break;
1112                 } else if (intr_status & CSR_SCSI_RESET_INTR) {
1113                         clear_bit(AF_ONLINE, &ha->flags);
1114                         __qla4xxx_disable_intrs(ha);
1115
1116                         writel(set_rmask(CSR_SCSI_RESET_INTR),
1117                                &ha->reg->ctrl_status);
1118                         readl(&ha->reg->ctrl_status);
1119
1120                         if (!test_bit(AF_HA_REMOVAL, &ha->flags))
1121                                 set_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
1122
1123                         break;
1124                 } else if (intr_status & INTR_PENDING) {
1125                         ha->isp_ops->interrupt_service_routine(ha, intr_status);
1126                         ha->total_io_count++;
1127                         if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
1128                                 break;
1129                 }
1130         }
1131
1132         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1133
1134         return IRQ_HANDLED;
1135 }
1136
1137 /**
1138  * qla4_82xx_intr_handler - hardware interrupt handler.
1139  * @irq: Unused
1140  * @dev_id: Pointer to host adapter structure
1141  **/
1142 irqreturn_t qla4_82xx_intr_handler(int irq, void *dev_id)
1143 {
1144         struct scsi_qla_host *ha = dev_id;
1145         uint32_t intr_status;
1146         uint32_t status;
1147         unsigned long flags = 0;
1148         uint8_t reqs_count = 0;
1149
1150         if (unlikely(pci_channel_offline(ha->pdev)))
1151                 return IRQ_HANDLED;
1152
1153         ha->isr_count++;
1154         status = qla4_82xx_rd_32(ha, ISR_INT_VECTOR);
1155         if (!(status & ha->nx_legacy_intr.int_vec_bit))
1156                 return IRQ_NONE;
1157
1158         status = qla4_82xx_rd_32(ha, ISR_INT_STATE_REG);
1159         if (!ISR_IS_LEGACY_INTR_TRIGGERED(status)) {
1160                 DEBUG7(ql4_printk(KERN_INFO, ha,
1161                                   "%s legacy Int not triggered\n", __func__));
1162                 return IRQ_NONE;
1163         }
1164
1165         /* clear the interrupt */
1166         qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
1167
1168         /* read twice to ensure write is flushed */
1169         qla4_82xx_rd_32(ha, ISR_INT_VECTOR);
1170         qla4_82xx_rd_32(ha, ISR_INT_VECTOR);
1171
1172         spin_lock_irqsave(&ha->hardware_lock, flags);
1173         while (1) {
1174                 if (!(readl(&ha->qla4_82xx_reg->host_int) &
1175                     ISRX_82XX_RISC_INT)) {
1176                         qla4_82xx_spurious_interrupt(ha, reqs_count);
1177                         break;
1178                 }
1179                 intr_status =  readl(&ha->qla4_82xx_reg->host_status);
1180                 if ((intr_status &
1181                     (HSRX_RISC_MB_INT | HSRX_RISC_IOCB_INT)) == 0)  {
1182                         qla4_82xx_spurious_interrupt(ha, reqs_count);
1183                         break;
1184                 }
1185
1186                 ha->isp_ops->interrupt_service_routine(ha, intr_status);
1187
1188                 /* Enable Interrupt */
1189                 qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
1190
1191                 if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
1192                         break;
1193         }
1194
1195         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1196         return IRQ_HANDLED;
1197 }
1198
1199 #define LEG_INT_PTR_B31         (1 << 31)
1200 #define LEG_INT_PTR_B30         (1 << 30)
1201 #define PF_BITS_MASK            (0xF << 16)
1202
1203 /**
1204  * qla4_83xx_intr_handler - hardware interrupt handler.
1205  * @irq: Unused
1206  * @dev_id: Pointer to host adapter structure
1207  **/
1208 irqreturn_t qla4_83xx_intr_handler(int irq, void *dev_id)
1209 {
1210         struct scsi_qla_host *ha = dev_id;
1211         uint32_t leg_int_ptr = 0;
1212         unsigned long flags = 0;
1213
1214         ha->isr_count++;
1215         leg_int_ptr = readl(&ha->qla4_83xx_reg->leg_int_ptr);
1216
1217         /* Legacy interrupt is valid if bit31 of leg_int_ptr is set */
1218         if (!(leg_int_ptr & LEG_INT_PTR_B31)) {
1219                 DEBUG7(ql4_printk(KERN_ERR, ha,
1220                                   "%s: Legacy Interrupt Bit 31 not set, spurious interrupt!\n",
1221                                   __func__));
1222                 return IRQ_NONE;
1223         }
1224
1225         /* Validate the PCIE function ID set in leg_int_ptr bits [19..16] */
1226         if ((leg_int_ptr & PF_BITS_MASK) != ha->pf_bit) {
1227                 DEBUG7(ql4_printk(KERN_ERR, ha,
1228                                   "%s: Incorrect function ID 0x%x in legacy interrupt register, ha->pf_bit = 0x%x\n",
1229                                   __func__, (leg_int_ptr & PF_BITS_MASK),
1230                                   ha->pf_bit));
1231                 return IRQ_NONE;
1232         }
1233
1234         /* To de-assert legacy interrupt, write 0 to Legacy Interrupt Trigger
1235          * Control register and poll till Legacy Interrupt Pointer register
1236          * bit30 is 0.
1237          */
1238         writel(0, &ha->qla4_83xx_reg->leg_int_trig);
1239         do {
1240                 leg_int_ptr = readl(&ha->qla4_83xx_reg->leg_int_ptr);
1241                 if ((leg_int_ptr & PF_BITS_MASK) != ha->pf_bit)
1242                         break;
1243         } while (leg_int_ptr & LEG_INT_PTR_B30);
1244
1245         spin_lock_irqsave(&ha->hardware_lock, flags);
1246         leg_int_ptr = readl(&ha->qla4_83xx_reg->risc_intr);
1247         ha->isp_ops->interrupt_service_routine(ha, leg_int_ptr);
1248         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1249
1250         return IRQ_HANDLED;
1251 }
1252
1253 irqreturn_t
1254 qla4_8xxx_msi_handler(int irq, void *dev_id)
1255 {
1256         struct scsi_qla_host *ha;
1257
1258         ha = (struct scsi_qla_host *) dev_id;
1259         if (!ha) {
1260                 DEBUG2(printk(KERN_INFO
1261                     "qla4xxx: MSIX: Interrupt with NULL host ptr\n"));
1262                 return IRQ_NONE;
1263         }
1264
1265         ha->isr_count++;
1266         /* clear the interrupt */
1267         qla4_82xx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
1268
1269         /* read twice to ensure write is flushed */
1270         qla4_82xx_rd_32(ha, ISR_INT_VECTOR);
1271         qla4_82xx_rd_32(ha, ISR_INT_VECTOR);
1272
1273         return qla4_8xxx_default_intr_handler(irq, dev_id);
1274 }
1275
1276 static irqreturn_t qla4_83xx_mailbox_intr_handler(int irq, void *dev_id)
1277 {
1278         struct scsi_qla_host *ha = dev_id;
1279         unsigned long flags;
1280         uint32_t ival = 0;
1281
1282         spin_lock_irqsave(&ha->hardware_lock, flags);
1283
1284         ival = readl(&ha->qla4_83xx_reg->risc_intr);
1285         if (ival == 0) {
1286                 ql4_printk(KERN_INFO, ha,
1287                            "%s: It is a spurious mailbox interrupt!\n",
1288                            __func__);
1289                 ival = readl(&ha->qla4_83xx_reg->mb_int_mask);
1290                 ival &= ~INT_MASK_FW_MB;
1291                 writel(ival, &ha->qla4_83xx_reg->mb_int_mask);
1292                 goto exit;
1293         }
1294
1295         qla4xxx_isr_decode_mailbox(ha,
1296                                    readl(&ha->qla4_83xx_reg->mailbox_out[0]));
1297         writel(0, &ha->qla4_83xx_reg->risc_intr);
1298         ival = readl(&ha->qla4_83xx_reg->mb_int_mask);
1299         ival &= ~INT_MASK_FW_MB;
1300         writel(ival, &ha->qla4_83xx_reg->mb_int_mask);
1301         ha->isr_count++;
1302 exit:
1303         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1304         return IRQ_HANDLED;
1305 }
1306
1307 /**
1308  * qla4_8xxx_default_intr_handler - hardware interrupt handler.
1309  * @irq: Unused
1310  * @dev_id: Pointer to host adapter structure
1311  *
1312  * This interrupt handler is called directly for MSI-X, and
1313  * called indirectly for MSI.
1314  **/
1315 irqreturn_t
1316 qla4_8xxx_default_intr_handler(int irq, void *dev_id)
1317 {
1318         struct scsi_qla_host *ha = dev_id;
1319         unsigned long   flags;
1320         uint32_t intr_status;
1321         uint8_t reqs_count = 0;
1322
1323         if (is_qla8032(ha) || is_qla8042(ha)) {
1324                 qla4_83xx_mailbox_intr_handler(irq, dev_id);
1325         } else {
1326                 spin_lock_irqsave(&ha->hardware_lock, flags);
1327                 while (1) {
1328                         if (!(readl(&ha->qla4_82xx_reg->host_int) &
1329                             ISRX_82XX_RISC_INT)) {
1330                                 qla4_82xx_spurious_interrupt(ha, reqs_count);
1331                                 break;
1332                         }
1333
1334                         intr_status =  readl(&ha->qla4_82xx_reg->host_status);
1335                         if ((intr_status &
1336                             (HSRX_RISC_MB_INT | HSRX_RISC_IOCB_INT)) == 0) {
1337                                 qla4_82xx_spurious_interrupt(ha, reqs_count);
1338                                 break;
1339                         }
1340
1341                         ha->isp_ops->interrupt_service_routine(ha, intr_status);
1342
1343                         if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
1344                                 break;
1345                 }
1346                 ha->isr_count++;
1347                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1348         }
1349         return IRQ_HANDLED;
1350 }
1351
1352 irqreturn_t
1353 qla4_8xxx_msix_rsp_q(int irq, void *dev_id)
1354 {
1355         struct scsi_qla_host *ha = dev_id;
1356         unsigned long flags;
1357         uint32_t ival = 0;
1358
1359         spin_lock_irqsave(&ha->hardware_lock, flags);
1360         if (is_qla8032(ha) || is_qla8042(ha)) {
1361                 ival = readl(&ha->qla4_83xx_reg->iocb_int_mask);
1362                 if (ival == 0) {
1363                         ql4_printk(KERN_INFO, ha, "%s: It is a spurious iocb interrupt!\n",
1364                                    __func__);
1365                         goto exit_msix_rsp_q;
1366                 }
1367                 qla4xxx_process_response_queue(ha);
1368                 writel(0, &ha->qla4_83xx_reg->iocb_int_mask);
1369         } else {
1370                 qla4xxx_process_response_queue(ha);
1371                 writel(0, &ha->qla4_82xx_reg->host_int);
1372         }
1373         ha->isr_count++;
1374 exit_msix_rsp_q:
1375         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1376         return IRQ_HANDLED;
1377 }
1378
1379 /**
1380  * qla4xxx_process_aen - processes AENs generated by firmware
1381  * @ha: pointer to host adapter structure.
1382  * @process_aen: type of AENs to process
1383  *
1384  * Processes specific types of Asynchronous Events generated by firmware.
1385  * The type of AENs to process is specified by process_aen and can be
1386  *      PROCESS_ALL_AENS         0
1387  *      FLUSH_DDB_CHANGED_AENS   1
1388  *      RELOGIN_DDB_CHANGED_AENS 2
1389  **/
1390 void qla4xxx_process_aen(struct scsi_qla_host * ha, uint8_t process_aen)
1391 {
1392         uint32_t mbox_sts[MBOX_AEN_REG_COUNT];
1393         struct aen *aen;
1394         int i;
1395         unsigned long flags;
1396
1397         spin_lock_irqsave(&ha->hardware_lock, flags);
1398         while (ha->aen_out != ha->aen_in) {
1399                 aen = &ha->aen_q[ha->aen_out];
1400                 /* copy aen information to local structure */
1401                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
1402                         mbox_sts[i] = aen->mbox_sts[i];
1403
1404                 ha->aen_q_count++;
1405                 ha->aen_out++;
1406
1407                 if (ha->aen_out == MAX_AEN_ENTRIES)
1408                         ha->aen_out = 0;
1409
1410                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1411
1412                 DEBUG2(printk("qla4xxx(%ld): AEN[%d]=0x%08x, mbx1=0x%08x mbx2=0x%08x"
1413                         " mbx3=0x%08x mbx4=0x%08x\n", ha->host_no,
1414                         (ha->aen_out ? (ha->aen_out-1): (MAX_AEN_ENTRIES-1)),
1415                         mbox_sts[0], mbox_sts[1], mbox_sts[2],
1416                         mbox_sts[3], mbox_sts[4]));
1417
1418                 switch (mbox_sts[0]) {
1419                 case MBOX_ASTS_DATABASE_CHANGED:
1420                         switch (process_aen) {
1421                         case FLUSH_DDB_CHANGED_AENS:
1422                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x, index "
1423                                               "[%d] state=%04x FLUSHED!\n",
1424                                               ha->host_no, ha->aen_out,
1425                                               mbox_sts[0], mbox_sts[2],
1426                                               mbox_sts[3]));
1427                                 break;
1428                         case PROCESS_ALL_AENS:
1429                         default:
1430                                 /* Specific device. */
1431                                 if (mbox_sts[1] == 1)
1432                                         qla4xxx_process_ddb_changed(ha,
1433                                                 mbox_sts[2], mbox_sts[3],
1434                                                 mbox_sts[4]);
1435                                 break;
1436                         }
1437                 }
1438                 spin_lock_irqsave(&ha->hardware_lock, flags);
1439         }
1440         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1441 }
1442
1443 int qla4xxx_request_irqs(struct scsi_qla_host *ha)
1444 {
1445         int ret;
1446
1447         if (is_qla40XX(ha))
1448                 goto try_intx;
1449
1450         if (ql4xenablemsix == 2) {
1451                 /* Note: MSI Interrupts not supported for ISP8324 and ISP8042 */
1452                 if (is_qla8032(ha) || is_qla8042(ha)) {
1453                         ql4_printk(KERN_INFO, ha, "%s: MSI Interrupts not supported for ISP%04x, Falling back-to INTx mode\n",
1454                                    __func__, ha->pdev->device);
1455                         goto try_intx;
1456                 }
1457                 goto try_msi;
1458         }
1459
1460         if (ql4xenablemsix == 0 || ql4xenablemsix != 1)
1461                 goto try_intx;
1462
1463         /* Trying MSI-X */
1464         ret = qla4_8xxx_enable_msix(ha);
1465         if (!ret) {
1466                 DEBUG2(ql4_printk(KERN_INFO, ha,
1467                     "MSI-X: Enabled (0x%X).\n", ha->revision_id));
1468                 goto irq_attached;
1469         } else {
1470                 if (is_qla8032(ha) || is_qla8042(ha)) {
1471                         ql4_printk(KERN_INFO, ha, "%s: ISP%04x: MSI-X: Falling back-to INTx mode. ret = %d\n",
1472                                    __func__, ha->pdev->device, ret);
1473                         goto try_intx;
1474                 }
1475         }
1476
1477         ql4_printk(KERN_WARNING, ha,
1478             "MSI-X: Falling back-to MSI mode -- %d.\n", ret);
1479
1480 try_msi:
1481         /* Trying MSI */
1482         ret = pci_enable_msi(ha->pdev);
1483         if (!ret) {
1484                 ret = request_irq(ha->pdev->irq, qla4_8xxx_msi_handler,
1485                         0, DRIVER_NAME, ha);
1486                 if (!ret) {
1487                         DEBUG2(ql4_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
1488                         set_bit(AF_MSI_ENABLED, &ha->flags);
1489                         goto irq_attached;
1490                 } else {
1491                         ql4_printk(KERN_WARNING, ha,
1492                             "MSI: Failed to reserve interrupt %d "
1493                             "already in use.\n", ha->pdev->irq);
1494                         pci_disable_msi(ha->pdev);
1495                 }
1496         }
1497
1498         /*
1499          * Prevent interrupts from falling back to INTx mode in cases where
1500          * interrupts cannot get acquired through MSI-X or MSI mode.
1501          */
1502         if (is_qla8022(ha)) {
1503                 ql4_printk(KERN_WARNING, ha, "IRQ not attached -- %d.\n", ret);
1504                 goto irq_not_attached;
1505         }
1506 try_intx:
1507         /* Trying INTx */
1508         ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
1509             IRQF_SHARED, DRIVER_NAME, ha);
1510         if (!ret) {
1511                 DEBUG2(ql4_printk(KERN_INFO, ha, "INTx: Enabled.\n"));
1512                 set_bit(AF_INTx_ENABLED, &ha->flags);
1513                 goto irq_attached;
1514
1515         } else {
1516                 ql4_printk(KERN_WARNING, ha,
1517                     "INTx: Failed to reserve interrupt %d already in"
1518                     " use.\n", ha->pdev->irq);
1519                 goto irq_not_attached;
1520         }
1521
1522 irq_attached:
1523         set_bit(AF_IRQ_ATTACHED, &ha->flags);
1524         ha->host->irq = ha->pdev->irq;
1525         ql4_printk(KERN_INFO, ha, "%s: irq %d attached\n",
1526             __func__, ha->pdev->irq);
1527 irq_not_attached:
1528         return ret;
1529 }
1530
1531 void qla4xxx_free_irqs(struct scsi_qla_host *ha)
1532 {
1533         if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags)) {
1534                 if (test_bit(AF_MSIX_ENABLED, &ha->flags)) {
1535                         qla4_8xxx_disable_msix(ha);
1536                 } else if (test_and_clear_bit(AF_MSI_ENABLED, &ha->flags)) {
1537                         free_irq(ha->pdev->irq, ha);
1538                         pci_disable_msi(ha->pdev);
1539                 } else if (test_and_clear_bit(AF_INTx_ENABLED, &ha->flags)) {
1540                         free_irq(ha->pdev->irq, ha);
1541                 }
1542         }
1543 }