OSDN Git Service

pch_dma: Support new device LAPIS Semiconductor ML7831 IOH
[android-x86/kernel.git] / drivers / scsi / qla4xxx / ql4_isr.c
1 /*
2  * QLogic iSCSI HBA Driver
3  * Copyright (c)  2003-2010 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                 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Status Entry invalid "
127                               "handle 0x%x, sp=%p. This cmd may have already "
128                               "been completed.\n", ha->host_no, __func__,
129                               le32_to_cpu(sts_entry->handle), srb));
130                 ql4_printk(KERN_WARNING, ha, "%s invalid status entry:"
131                     " handle=0x%0x\n", __func__, sts_entry->handle);
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 (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
228                         qla4xxx_mark_device_missing(ha, ddb_entry);
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                 /*
247                  * If there is scsi_status, it takes precedense over
248                  * underflow condition.
249                  */
250                 if (scsi_status != 0) {
251                         cmd->result = DID_OK << 16 | scsi_status;
252
253                         if (scsi_status != SCSI_CHECK_CONDITION)
254                                 break;
255
256                         /* Copy Sense Data into sense buffer. */
257                         qla4xxx_copy_sense(ha, sts_entry, srb);
258                 } else {
259                         /*
260                          * If RISC reports underrun and target does not
261                          * report it then we must have a lost frame, so
262                          * tell upper layer to retry it by reporting a
263                          * bus busy.
264                          */
265                         if ((sts_entry->iscsiFlags &
266                              ISCSI_FLAG_RESIDUAL_UNDER) == 0) {
267                                 cmd->result = DID_BUS_BUSY << 16;
268                         } else if ((scsi_bufflen(cmd) - residual) <
269                                    cmd->underflow) {
270                                 /*
271                                  * Handle mid-layer underflow???
272                                  *
273                                  * For kernels less than 2.4, the driver must
274                                  * return an error if an underflow is detected.
275                                  * For kernels equal-to and above 2.4, the
276                                  * mid-layer will appearantly handle the
277                                  * underflow by detecting the residual count --
278                                  * unfortunately, we do not see where this is
279                                  * actually being done.  In the interim, we
280                                  * will return DID_ERROR.
281                                  */
282                                 DEBUG2(printk("scsi%ld:%d:%d:%d: %s: "
283                                         "Mid-layer Data underrun1, "
284                                         "xferlen = 0x%x, "
285                                         "residual = 0x%x\n", ha->host_no,
286                                         cmd->device->channel,
287                                         cmd->device->id,
288                                         cmd->device->lun, __func__,
289                                         scsi_bufflen(cmd), residual));
290
291                                 cmd->result = DID_ERROR << 16;
292                         } else {
293                                 cmd->result = DID_OK << 16;
294                         }
295                 }
296                 break;
297
298         case SCS_DEVICE_LOGGED_OUT:
299         case SCS_DEVICE_UNAVAILABLE:
300                 DEBUG2(printk(KERN_INFO "scsi%ld:%d:%d:%d: SCS_DEVICE "
301                     "state: 0x%x\n", ha->host_no,
302                     cmd->device->channel, cmd->device->id,
303                     cmd->device->lun, sts_entry->completionStatus));
304                 /*
305                  * Mark device missing so that we won't continue to
306                  * send I/O to this device.  We should get a ddb
307                  * state change AEN soon.
308                  */
309                 if (atomic_read(&ddb_entry->state) == DDB_STATE_ONLINE)
310                         qla4xxx_mark_device_missing(ha, ddb_entry);
311
312                 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
313                 break;
314
315         case SCS_QUEUE_FULL:
316                 /*
317                  * SCSI Mid-Layer handles device queue full
318                  */
319                 cmd->result = DID_OK << 16 | sts_entry->scsiStatus;
320                 DEBUG2(printk("scsi%ld:%d:%d: %s: QUEUE FULL detected "
321                               "compl=%02x, scsi=%02x, state=%02x, iFlags=%02x,"
322                               " iResp=%02x\n", ha->host_no, cmd->device->id,
323                               cmd->device->lun, __func__,
324                               sts_entry->completionStatus,
325                               sts_entry->scsiStatus, sts_entry->state_flags,
326                               sts_entry->iscsiFlags,
327                               sts_entry->iscsiResponse));
328                 break;
329
330         default:
331                 cmd->result = DID_ERROR << 16;
332                 break;
333         }
334
335 status_entry_exit:
336
337         /* complete the request, if not waiting for status_continuation pkt */
338         srb->cc_stat = sts_entry->completionStatus;
339         if (ha->status_srb == NULL)
340                 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
341 }
342
343 /**
344  * qla4xxx_process_response_queue - process response queue completions
345  * @ha: Pointer to host adapter structure.
346  *
347  * This routine process response queue completions in interrupt context.
348  * Hardware_lock locked upon entry
349  **/
350 void qla4xxx_process_response_queue(struct scsi_qla_host *ha)
351 {
352         uint32_t count = 0;
353         struct srb *srb = NULL;
354         struct status_entry *sts_entry;
355
356         /* Process all responses from response queue */
357         while ((ha->response_ptr->signature != RESPONSE_PROCESSED)) {
358                 sts_entry = (struct status_entry *) ha->response_ptr;
359                 count++;
360
361                 /* Advance pointers for next entry */
362                 if (ha->response_out == (RESPONSE_QUEUE_DEPTH - 1)) {
363                         ha->response_out = 0;
364                         ha->response_ptr = ha->response_ring;
365                 } else {
366                         ha->response_out++;
367                         ha->response_ptr++;
368                 }
369
370                 /* process entry */
371                 switch (sts_entry->hdr.entryType) {
372                 case ET_STATUS:
373                         /* Common status */
374                         qla4xxx_status_entry(ha, sts_entry);
375                         break;
376
377                 case ET_PASSTHRU_STATUS:
378                         break;
379
380                 case ET_STATUS_CONTINUATION:
381                         qla4xxx_status_cont_entry(ha,
382                                 (struct status_cont_entry *) sts_entry);
383                         break;
384
385                 case ET_COMMAND:
386                         /* ISP device queue is full. Command not
387                          * accepted by ISP.  Queue command for
388                          * later */
389
390                         srb = qla4xxx_del_from_active_array(ha,
391                                                     le32_to_cpu(sts_entry->
392                                                                 handle));
393                         if (srb == NULL)
394                                 goto exit_prq_invalid_handle;
395
396                         DEBUG2(printk("scsi%ld: %s: FW device queue full, "
397                                       "srb %p\n", ha->host_no, __func__, srb));
398
399                         /* ETRY normally by sending it back with
400                          * DID_BUS_BUSY */
401                         srb->cmd->result = DID_BUS_BUSY << 16;
402                         kref_put(&srb->srb_ref, qla4xxx_srb_compl);
403                         break;
404
405                 case ET_CONTINUE:
406                         /* Just throw away the continuation entries */
407                         DEBUG2(printk("scsi%ld: %s: Continuation entry - "
408                                       "ignoring\n", ha->host_no, __func__));
409                         break;
410
411                 default:
412                         /*
413                          * Invalid entry in response queue, reset RISC
414                          * firmware.
415                          */
416                         DEBUG2(printk("scsi%ld: %s: Invalid entry %x in "
417                                       "response queue \n", ha->host_no,
418                                       __func__,
419                                       sts_entry->hdr.entryType));
420                         goto exit_prq_error;
421                 }
422                 ((struct response *)sts_entry)->signature = RESPONSE_PROCESSED;
423                 wmb();
424         }
425
426         /*
427          * Tell ISP we're done with response(s). This also clears the interrupt.
428          */
429         ha->isp_ops->complete_iocb(ha);
430
431         return;
432
433 exit_prq_invalid_handle:
434         DEBUG2(printk("scsi%ld: %s: Invalid handle(srb)=%p type=%x IOCS=%x\n",
435                       ha->host_no, __func__, srb, sts_entry->hdr.entryType,
436                       sts_entry->completionStatus));
437
438 exit_prq_error:
439         ha->isp_ops->complete_iocb(ha);
440         set_bit(DPC_RESET_HA, &ha->dpc_flags);
441 }
442
443 /**
444  * qla4xxx_isr_decode_mailbox - decodes mailbox status
445  * @ha: Pointer to host adapter structure.
446  * @mailbox_status: Mailbox status.
447  *
448  * This routine decodes the mailbox status during the ISR.
449  * Hardware_lock locked upon entry. runs in interrupt context.
450  **/
451 static void qla4xxx_isr_decode_mailbox(struct scsi_qla_host * ha,
452                                        uint32_t mbox_status)
453 {
454         int i;
455         uint32_t mbox_sts[MBOX_AEN_REG_COUNT];
456
457         if ((mbox_status == MBOX_STS_BUSY) ||
458             (mbox_status == MBOX_STS_INTERMEDIATE_COMPLETION) ||
459             (mbox_status >> 12 == MBOX_COMPLETION_STATUS)) {
460                 ha->mbox_status[0] = mbox_status;
461
462                 if (test_bit(AF_MBOX_COMMAND, &ha->flags)) {
463                         /*
464                          * Copy all mailbox registers to a temporary
465                          * location and set mailbox command done flag
466                          */
467                         for (i = 0; i < ha->mbox_status_count; i++)
468                                 ha->mbox_status[i] = is_qla8022(ha)
469                                     ? readl(&ha->qla4_8xxx_reg->mailbox_out[i])
470                                     : readl(&ha->reg->mailbox[i]);
471
472                         set_bit(AF_MBOX_COMMAND_DONE, &ha->flags);
473
474                         if (test_bit(AF_MBOX_COMMAND_NOPOLL, &ha->flags))
475                                 complete(&ha->mbx_intr_comp);
476                 }
477         } else if (mbox_status >> 12 == MBOX_ASYNC_EVENT_STATUS) {
478                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
479                         mbox_sts[i] = is_qla8022(ha)
480                             ? readl(&ha->qla4_8xxx_reg->mailbox_out[i])
481                             : readl(&ha->reg->mailbox[i]);
482
483                 /* Immediately process the AENs that don't require much work.
484                  * Only queue the database_changed AENs */
485                 if (ha->aen_log.count < MAX_AEN_ENTRIES) {
486                         for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
487                                 ha->aen_log.entry[ha->aen_log.count].mbox_sts[i] =
488                                     mbox_sts[i];
489                         ha->aen_log.count++;
490                 }
491                 switch (mbox_status) {
492                 case MBOX_ASTS_SYSTEM_ERROR:
493                         /* Log Mailbox registers */
494                         ql4_printk(KERN_INFO, ha, "%s: System Err\n", __func__);
495                         qla4xxx_dump_registers(ha);
496
497                         if (ql4xdontresethba) {
498                                 DEBUG2(printk("scsi%ld: %s:Don't Reset HBA\n",
499                                     ha->host_no, __func__));
500                         } else {
501                                 set_bit(AF_GET_CRASH_RECORD, &ha->flags);
502                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
503                         }
504                         break;
505
506                 case MBOX_ASTS_REQUEST_TRANSFER_ERROR:
507                 case MBOX_ASTS_RESPONSE_TRANSFER_ERROR:
508                 case MBOX_ASTS_NVRAM_INVALID:
509                 case MBOX_ASTS_IP_ADDRESS_CHANGED:
510                 case MBOX_ASTS_DHCP_LEASE_EXPIRED:
511                         DEBUG2(printk("scsi%ld: AEN %04x, ERROR Status, "
512                                       "Reset HA\n", ha->host_no, mbox_status));
513                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
514                         break;
515
516                 case MBOX_ASTS_LINK_UP:
517                         set_bit(AF_LINK_UP, &ha->flags);
518                         if (test_bit(AF_INIT_DONE, &ha->flags))
519                                 set_bit(DPC_LINK_CHANGED, &ha->dpc_flags);
520
521                         ql4_printk(KERN_INFO, ha, "%s: LINK UP\n", __func__);
522                         break;
523
524                 case MBOX_ASTS_LINK_DOWN:
525                         clear_bit(AF_LINK_UP, &ha->flags);
526                         if (test_bit(AF_INIT_DONE, &ha->flags))
527                                 set_bit(DPC_LINK_CHANGED, &ha->dpc_flags);
528
529                         ql4_printk(KERN_INFO, ha, "%s: LINK DOWN\n", __func__);
530                         break;
531
532                 case MBOX_ASTS_HEARTBEAT:
533                         ha->seconds_since_last_heartbeat = 0;
534                         break;
535
536                 case MBOX_ASTS_DHCP_LEASE_ACQUIRED:
537                         DEBUG2(printk("scsi%ld: AEN %04x DHCP LEASE "
538                                       "ACQUIRED\n", ha->host_no, mbox_status));
539                         set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
540                         break;
541
542                 case MBOX_ASTS_PROTOCOL_STATISTIC_ALARM:
543                 case MBOX_ASTS_SCSI_COMMAND_PDU_REJECTED: /* Target
544                                                            * mode
545                                                            * only */
546                 case MBOX_ASTS_UNSOLICITED_PDU_RECEIVED:  /* Connection mode */
547                 case MBOX_ASTS_IPSEC_SYSTEM_FATAL_ERROR:
548                 case MBOX_ASTS_SUBNET_STATE_CHANGE:
549                 case MBOX_ASTS_DUPLICATE_IP:
550                         /* No action */
551                         DEBUG2(printk("scsi%ld: AEN %04x\n", ha->host_no,
552                                       mbox_status));
553                         break;
554
555                 case MBOX_ASTS_IP_ADDR_STATE_CHANGED:
556                         printk("scsi%ld: AEN %04x, mbox_sts[2]=%04x, "
557                             "mbox_sts[3]=%04x\n", ha->host_no, mbox_sts[0],
558                             mbox_sts[2], mbox_sts[3]);
559
560                         /* mbox_sts[2] = Old ACB state
561                          * mbox_sts[3] = new ACB state */
562                         if ((mbox_sts[3] == ACB_STATE_VALID) &&
563                             ((mbox_sts[2] == ACB_STATE_TENTATIVE) ||
564                             (mbox_sts[2] == ACB_STATE_ACQUIRING)))
565                                 set_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags);
566                         else if ((mbox_sts[3] == ACB_STATE_ACQUIRING) &&
567                             (mbox_sts[2] == ACB_STATE_VALID))
568                                 set_bit(DPC_RESET_HA, &ha->dpc_flags);
569                         break;
570
571                 case MBOX_ASTS_MAC_ADDRESS_CHANGED:
572                 case MBOX_ASTS_DNS:
573                         /* No action */
574                         DEBUG2(printk(KERN_INFO "scsi%ld: AEN %04x, "
575                                       "mbox_sts[1]=%04x, mbox_sts[2]=%04x\n",
576                                       ha->host_no, mbox_sts[0],
577                                       mbox_sts[1], mbox_sts[2]));
578                         break;
579
580                 case MBOX_ASTS_SELF_TEST_FAILED:
581                 case MBOX_ASTS_LOGIN_FAILED:
582                         /* No action */
583                         DEBUG2(printk("scsi%ld: AEN %04x, mbox_sts[1]=%04x, "
584                                       "mbox_sts[2]=%04x, mbox_sts[3]=%04x\n",
585                                       ha->host_no, mbox_sts[0], mbox_sts[1],
586                                       mbox_sts[2], mbox_sts[3]));
587                         break;
588
589                 case MBOX_ASTS_DATABASE_CHANGED:
590                         /* Queue AEN information and process it in the DPC
591                          * routine */
592                         if (ha->aen_q_count > 0) {
593
594                                 /* decrement available counter */
595                                 ha->aen_q_count--;
596
597                                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
598                                         ha->aen_q[ha->aen_in].mbox_sts[i] =
599                                             mbox_sts[i];
600
601                                 /* print debug message */
602                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x queued "
603                                               "mb1:0x%x mb2:0x%x mb3:0x%x "
604                                               "mb4:0x%x mb5:0x%x\n",
605                                               ha->host_no, ha->aen_in,
606                                               mbox_sts[0], mbox_sts[1],
607                                               mbox_sts[2], mbox_sts[3],
608                                               mbox_sts[4], mbox_sts[5]));
609
610                                 /* advance pointer */
611                                 ha->aen_in++;
612                                 if (ha->aen_in == MAX_AEN_ENTRIES)
613                                         ha->aen_in = 0;
614
615                                 /* The DPC routine will process the aen */
616                                 set_bit(DPC_AEN, &ha->dpc_flags);
617                         } else {
618                                 DEBUG2(printk("scsi%ld: %s: aen %04x, queue "
619                                               "overflowed!  AEN LOST!!\n",
620                                               ha->host_no, __func__,
621                                               mbox_sts[0]));
622
623                                 DEBUG2(printk("scsi%ld: DUMP AEN QUEUE\n",
624                                               ha->host_no));
625
626                                 for (i = 0; i < MAX_AEN_ENTRIES; i++) {
627                                         DEBUG2(printk("AEN[%d] %04x %04x %04x "
628                                                       "%04x\n", i, mbox_sts[0],
629                                                       mbox_sts[1], mbox_sts[2],
630                                                       mbox_sts[3]));
631                                 }
632                         }
633                         break;
634
635                 case MBOX_ASTS_TXSCVR_INSERTED:
636                         DEBUG2(printk(KERN_WARNING
637                             "scsi%ld: AEN %04x Transceiver"
638                             " inserted\n",  ha->host_no, mbox_sts[0]));
639                         break;
640
641                 case MBOX_ASTS_TXSCVR_REMOVED:
642                         DEBUG2(printk(KERN_WARNING
643                             "scsi%ld: AEN %04x Transceiver"
644                             " removed\n",  ha->host_no, mbox_sts[0]));
645                         break;
646
647                 default:
648                         DEBUG2(printk(KERN_WARNING
649                                       "scsi%ld: AEN %04x UNKNOWN\n",
650                                       ha->host_no, mbox_sts[0]));
651                         break;
652                 }
653         } else {
654                 DEBUG2(printk("scsi%ld: Unknown mailbox status %08X\n",
655                               ha->host_no, mbox_status));
656
657                 ha->mbox_status[0] = mbox_status;
658         }
659 }
660
661 /**
662  * qla4_8xxx_interrupt_service_routine - isr
663  * @ha: pointer to host adapter structure.
664  *
665  * This is the main interrupt service routine.
666  * hardware_lock locked upon entry. runs in interrupt context.
667  **/
668 void qla4_8xxx_interrupt_service_routine(struct scsi_qla_host *ha,
669     uint32_t intr_status)
670 {
671         /* Process response queue interrupt. */
672         if (intr_status & HSRX_RISC_IOCB_INT)
673                 qla4xxx_process_response_queue(ha);
674
675         /* Process mailbox/asynch event interrupt.*/
676         if (intr_status & HSRX_RISC_MB_INT)
677                 qla4xxx_isr_decode_mailbox(ha,
678                     readl(&ha->qla4_8xxx_reg->mailbox_out[0]));
679
680         /* clear the interrupt */
681         writel(0, &ha->qla4_8xxx_reg->host_int);
682         readl(&ha->qla4_8xxx_reg->host_int);
683 }
684
685 /**
686  * qla4xxx_interrupt_service_routine - isr
687  * @ha: pointer to host adapter structure.
688  *
689  * This is the main interrupt service routine.
690  * hardware_lock locked upon entry. runs in interrupt context.
691  **/
692 void qla4xxx_interrupt_service_routine(struct scsi_qla_host * ha,
693                                        uint32_t intr_status)
694 {
695         /* Process response queue interrupt. */
696         if (intr_status & CSR_SCSI_COMPLETION_INTR)
697                 qla4xxx_process_response_queue(ha);
698
699         /* Process mailbox/asynch event  interrupt.*/
700         if (intr_status & CSR_SCSI_PROCESSOR_INTR) {
701                 qla4xxx_isr_decode_mailbox(ha,
702                                            readl(&ha->reg->mailbox[0]));
703
704                 /* Clear Mailbox Interrupt */
705                 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
706                        &ha->reg->ctrl_status);
707                 readl(&ha->reg->ctrl_status);
708         }
709 }
710
711 /**
712  * qla4_8xxx_spurious_interrupt - processes spurious interrupt
713  * @ha: pointer to host adapter structure.
714  * @reqs_count: .
715  *
716  **/
717 static void qla4_8xxx_spurious_interrupt(struct scsi_qla_host *ha,
718     uint8_t reqs_count)
719 {
720         if (reqs_count)
721                 return;
722
723         DEBUG2(ql4_printk(KERN_INFO, ha, "Spurious Interrupt\n"));
724         if (is_qla8022(ha)) {
725                 writel(0, &ha->qla4_8xxx_reg->host_int);
726                 if (test_bit(AF_INTx_ENABLED, &ha->flags))
727                         qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg,
728                             0xfbff);
729         }
730         ha->spurious_int_count++;
731 }
732
733 /**
734  * qla4xxx_intr_handler - hardware interrupt handler.
735  * @irq: Unused
736  * @dev_id: Pointer to host adapter structure
737  **/
738 irqreturn_t qla4xxx_intr_handler(int irq, void *dev_id)
739 {
740         struct scsi_qla_host *ha;
741         uint32_t intr_status;
742         unsigned long flags = 0;
743         uint8_t reqs_count = 0;
744
745         ha = (struct scsi_qla_host *) dev_id;
746         if (!ha) {
747                 DEBUG2(printk(KERN_INFO
748                               "qla4xxx: Interrupt with NULL host ptr\n"));
749                 return IRQ_NONE;
750         }
751
752         spin_lock_irqsave(&ha->hardware_lock, flags);
753
754         ha->isr_count++;
755         /*
756          * Repeatedly service interrupts up to a maximum of
757          * MAX_REQS_SERVICED_PER_INTR
758          */
759         while (1) {
760                 /*
761                  * Read interrupt status
762                  */
763                 if (ha->isp_ops->rd_shdw_rsp_q_in(ha) !=
764                     ha->response_out)
765                         intr_status = CSR_SCSI_COMPLETION_INTR;
766                 else
767                         intr_status = readl(&ha->reg->ctrl_status);
768
769                 if ((intr_status &
770                     (CSR_SCSI_RESET_INTR|CSR_FATAL_ERROR|INTR_PENDING)) == 0) {
771                         if (reqs_count == 0)
772                                 ha->spurious_int_count++;
773                         break;
774                 }
775
776                 if (intr_status & CSR_FATAL_ERROR) {
777                         DEBUG2(printk(KERN_INFO "scsi%ld: Fatal Error, "
778                                       "Status 0x%04x\n", ha->host_no,
779                                       readl(isp_port_error_status (ha))));
780
781                         /* Issue Soft Reset to clear this error condition.
782                          * This will prevent the RISC from repeatedly
783                          * interrupting the driver; thus, allowing the DPC to
784                          * get scheduled to continue error recovery.
785                          * NOTE: Disabling RISC interrupts does not work in
786                          * this case, as CSR_FATAL_ERROR overrides
787                          * CSR_SCSI_INTR_ENABLE */
788                         if ((readl(&ha->reg->ctrl_status) &
789                              CSR_SCSI_RESET_INTR) == 0) {
790                                 writel(set_rmask(CSR_SOFT_RESET),
791                                        &ha->reg->ctrl_status);
792                                 readl(&ha->reg->ctrl_status);
793                         }
794
795                         writel(set_rmask(CSR_FATAL_ERROR),
796                                &ha->reg->ctrl_status);
797                         readl(&ha->reg->ctrl_status);
798
799                         __qla4xxx_disable_intrs(ha);
800
801                         set_bit(DPC_RESET_HA, &ha->dpc_flags);
802
803                         break;
804                 } else if (intr_status & CSR_SCSI_RESET_INTR) {
805                         clear_bit(AF_ONLINE, &ha->flags);
806                         __qla4xxx_disable_intrs(ha);
807
808                         writel(set_rmask(CSR_SCSI_RESET_INTR),
809                                &ha->reg->ctrl_status);
810                         readl(&ha->reg->ctrl_status);
811
812                         if (!test_bit(AF_HA_REMOVAL, &ha->flags))
813                                 set_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
814
815                         break;
816                 } else if (intr_status & INTR_PENDING) {
817                         ha->isp_ops->interrupt_service_routine(ha, intr_status);
818                         ha->total_io_count++;
819                         if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
820                                 break;
821                 }
822         }
823
824         spin_unlock_irqrestore(&ha->hardware_lock, flags);
825
826         return IRQ_HANDLED;
827 }
828
829 /**
830  * qla4_8xxx_intr_handler - hardware interrupt handler.
831  * @irq: Unused
832  * @dev_id: Pointer to host adapter structure
833  **/
834 irqreturn_t qla4_8xxx_intr_handler(int irq, void *dev_id)
835 {
836         struct scsi_qla_host *ha = dev_id;
837         uint32_t intr_status;
838         uint32_t status;
839         unsigned long flags = 0;
840         uint8_t reqs_count = 0;
841
842         if (unlikely(pci_channel_offline(ha->pdev)))
843                 return IRQ_HANDLED;
844
845         ha->isr_count++;
846         status = qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
847         if (!(status & ha->nx_legacy_intr.int_vec_bit))
848                 return IRQ_NONE;
849
850         status = qla4_8xxx_rd_32(ha, ISR_INT_STATE_REG);
851         if (!ISR_IS_LEGACY_INTR_TRIGGERED(status)) {
852                 DEBUG2(ql4_printk(KERN_INFO, ha,
853                     "%s legacy Int not triggered\n", __func__));
854                 return IRQ_NONE;
855         }
856
857         /* clear the interrupt */
858         qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
859
860         /* read twice to ensure write is flushed */
861         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
862         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
863
864         spin_lock_irqsave(&ha->hardware_lock, flags);
865         while (1) {
866                 if (!(readl(&ha->qla4_8xxx_reg->host_int) &
867                     ISRX_82XX_RISC_INT)) {
868                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
869                         break;
870                 }
871                 intr_status =  readl(&ha->qla4_8xxx_reg->host_status);
872                 if ((intr_status &
873                     (HSRX_RISC_MB_INT | HSRX_RISC_IOCB_INT)) == 0)  {
874                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
875                         break;
876                 }
877
878                 ha->isp_ops->interrupt_service_routine(ha, intr_status);
879
880                 /* Enable Interrupt */
881                 qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_mask_reg, 0xfbff);
882
883                 if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
884                         break;
885         }
886
887         spin_unlock_irqrestore(&ha->hardware_lock, flags);
888         return IRQ_HANDLED;
889 }
890
891 irqreturn_t
892 qla4_8xxx_msi_handler(int irq, void *dev_id)
893 {
894         struct scsi_qla_host *ha;
895
896         ha = (struct scsi_qla_host *) dev_id;
897         if (!ha) {
898                 DEBUG2(printk(KERN_INFO
899                     "qla4xxx: MSIX: Interrupt with NULL host ptr\n"));
900                 return IRQ_NONE;
901         }
902
903         ha->isr_count++;
904         /* clear the interrupt */
905         qla4_8xxx_wr_32(ha, ha->nx_legacy_intr.tgt_status_reg, 0xffffffff);
906
907         /* read twice to ensure write is flushed */
908         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
909         qla4_8xxx_rd_32(ha, ISR_INT_VECTOR);
910
911         return qla4_8xxx_default_intr_handler(irq, dev_id);
912 }
913
914 /**
915  * qla4_8xxx_default_intr_handler - hardware interrupt handler.
916  * @irq: Unused
917  * @dev_id: Pointer to host adapter structure
918  *
919  * This interrupt handler is called directly for MSI-X, and
920  * called indirectly for MSI.
921  **/
922 irqreturn_t
923 qla4_8xxx_default_intr_handler(int irq, void *dev_id)
924 {
925         struct scsi_qla_host *ha = dev_id;
926         unsigned long   flags;
927         uint32_t intr_status;
928         uint8_t reqs_count = 0;
929
930         spin_lock_irqsave(&ha->hardware_lock, flags);
931         while (1) {
932                 if (!(readl(&ha->qla4_8xxx_reg->host_int) &
933                     ISRX_82XX_RISC_INT)) {
934                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
935                         break;
936                 }
937
938                 intr_status =  readl(&ha->qla4_8xxx_reg->host_status);
939                 if ((intr_status &
940                     (HSRX_RISC_MB_INT | HSRX_RISC_IOCB_INT)) == 0) {
941                         qla4_8xxx_spurious_interrupt(ha, reqs_count);
942                         break;
943                 }
944
945                 ha->isp_ops->interrupt_service_routine(ha, intr_status);
946
947                 if (++reqs_count == MAX_REQS_SERVICED_PER_INTR)
948                         break;
949         }
950
951         ha->isr_count++;
952         spin_unlock_irqrestore(&ha->hardware_lock, flags);
953         return IRQ_HANDLED;
954 }
955
956 irqreturn_t
957 qla4_8xxx_msix_rsp_q(int irq, void *dev_id)
958 {
959         struct scsi_qla_host *ha = dev_id;
960         unsigned long flags;
961
962         spin_lock_irqsave(&ha->hardware_lock, flags);
963         qla4xxx_process_response_queue(ha);
964         writel(0, &ha->qla4_8xxx_reg->host_int);
965         spin_unlock_irqrestore(&ha->hardware_lock, flags);
966
967         ha->isr_count++;
968         return IRQ_HANDLED;
969 }
970
971 /**
972  * qla4xxx_process_aen - processes AENs generated by firmware
973  * @ha: pointer to host adapter structure.
974  * @process_aen: type of AENs to process
975  *
976  * Processes specific types of Asynchronous Events generated by firmware.
977  * The type of AENs to process is specified by process_aen and can be
978  *      PROCESS_ALL_AENS         0
979  *      FLUSH_DDB_CHANGED_AENS   1
980  *      RELOGIN_DDB_CHANGED_AENS 2
981  **/
982 void qla4xxx_process_aen(struct scsi_qla_host * ha, uint8_t process_aen)
983 {
984         uint32_t mbox_sts[MBOX_AEN_REG_COUNT];
985         struct aen *aen;
986         int i;
987         unsigned long flags;
988
989         spin_lock_irqsave(&ha->hardware_lock, flags);
990         while (ha->aen_out != ha->aen_in) {
991                 aen = &ha->aen_q[ha->aen_out];
992                 /* copy aen information to local structure */
993                 for (i = 0; i < MBOX_AEN_REG_COUNT; i++)
994                         mbox_sts[i] = aen->mbox_sts[i];
995
996                 ha->aen_q_count++;
997                 ha->aen_out++;
998
999                 if (ha->aen_out == MAX_AEN_ENTRIES)
1000                         ha->aen_out = 0;
1001
1002                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1003
1004                 DEBUG2(printk("qla4xxx(%ld): AEN[%d]=0x%08x, mbx1=0x%08x mbx2=0x%08x"
1005                         " mbx3=0x%08x mbx4=0x%08x\n", ha->host_no,
1006                         (ha->aen_out ? (ha->aen_out-1): (MAX_AEN_ENTRIES-1)),
1007                         mbox_sts[0], mbox_sts[1], mbox_sts[2],
1008                         mbox_sts[3], mbox_sts[4]));
1009
1010                 switch (mbox_sts[0]) {
1011                 case MBOX_ASTS_DATABASE_CHANGED:
1012                         if (process_aen == FLUSH_DDB_CHANGED_AENS) {
1013                                 DEBUG2(printk("scsi%ld: AEN[%d] %04x, index "
1014                                               "[%d] state=%04x FLUSHED!\n",
1015                                               ha->host_no, ha->aen_out,
1016                                               mbox_sts[0], mbox_sts[2],
1017                                               mbox_sts[3]));
1018                                 break;
1019                         }
1020                 case PROCESS_ALL_AENS:
1021                 default:
1022                         if (mbox_sts[1] == 0) { /* Global DB change. */
1023                                 qla4xxx_reinitialize_ddb_list(ha);
1024                         } else if (mbox_sts[1] == 1) {  /* Specific device. */
1025                                 qla4xxx_process_ddb_changed(ha, mbox_sts[2],
1026                                                 mbox_sts[3], mbox_sts[4]);
1027                         }
1028                         break;
1029                 }
1030                 spin_lock_irqsave(&ha->hardware_lock, flags);
1031         }
1032         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1033 }
1034
1035 int qla4xxx_request_irqs(struct scsi_qla_host *ha)
1036 {
1037         int ret;
1038
1039         if (!is_qla8022(ha))
1040                 goto try_intx;
1041
1042         if (ql4xenablemsix == 2)
1043                 goto try_msi;
1044
1045         if (ql4xenablemsix == 0 || ql4xenablemsix != 1)
1046                 goto try_intx;
1047
1048         /* Trying MSI-X */
1049         ret = qla4_8xxx_enable_msix(ha);
1050         if (!ret) {
1051                 DEBUG2(ql4_printk(KERN_INFO, ha,
1052                     "MSI-X: Enabled (0x%X).\n", ha->revision_id));
1053                 goto irq_attached;
1054         }
1055
1056         ql4_printk(KERN_WARNING, ha,
1057             "MSI-X: Falling back-to MSI mode -- %d.\n", ret);
1058
1059 try_msi:
1060         /* Trying MSI */
1061         ret = pci_enable_msi(ha->pdev);
1062         if (!ret) {
1063                 ret = request_irq(ha->pdev->irq, qla4_8xxx_msi_handler,
1064                         0, DRIVER_NAME, ha);
1065                 if (!ret) {
1066                         DEBUG2(ql4_printk(KERN_INFO, ha, "MSI: Enabled.\n"));
1067                         set_bit(AF_MSI_ENABLED, &ha->flags);
1068                         goto irq_attached;
1069                 } else {
1070                         ql4_printk(KERN_WARNING, ha,
1071                             "MSI: Failed to reserve interrupt %d "
1072                             "already in use.\n", ha->pdev->irq);
1073                         pci_disable_msi(ha->pdev);
1074                 }
1075         }
1076         ql4_printk(KERN_WARNING, ha,
1077             "MSI: Falling back-to INTx mode -- %d.\n", ret);
1078
1079 try_intx:
1080         /* Trying INTx */
1081         ret = request_irq(ha->pdev->irq, ha->isp_ops->intr_handler,
1082             IRQF_SHARED, DRIVER_NAME, ha);
1083         if (!ret) {
1084                 DEBUG2(ql4_printk(KERN_INFO, ha, "INTx: Enabled.\n"));
1085                 set_bit(AF_INTx_ENABLED, &ha->flags);
1086                 goto irq_attached;
1087
1088         } else {
1089                 ql4_printk(KERN_WARNING, ha,
1090                     "INTx: Failed to reserve interrupt %d already in"
1091                     " use.\n", ha->pdev->irq);
1092                 return ret;
1093         }
1094
1095 irq_attached:
1096         set_bit(AF_IRQ_ATTACHED, &ha->flags);
1097         ha->host->irq = ha->pdev->irq;
1098         ql4_printk(KERN_INFO, ha, "%s: irq %d attached\n",
1099             __func__, ha->pdev->irq);
1100         return ret;
1101 }
1102
1103 void qla4xxx_free_irqs(struct scsi_qla_host *ha)
1104 {
1105         if (test_bit(AF_MSIX_ENABLED, &ha->flags))
1106                 qla4_8xxx_disable_msix(ha);
1107         else if (test_and_clear_bit(AF_MSI_ENABLED, &ha->flags)) {
1108                 free_irq(ha->pdev->irq, ha);
1109                 pci_disable_msi(ha->pdev);
1110         } else if (test_and_clear_bit(AF_INTx_ENABLED, &ha->flags))
1111                 free_irq(ha->pdev->irq, ha);
1112 }