summaryrefslogtreecommitdiffstats
path: root/kernel/drivers/scsi/esas2r/esas2r_int.c
blob: f16d6bcf9bb6db3f3c3e5b381fdb312d315cad55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/*
 *  linux/drivers/scsi/esas2r/esas2r_int.c
 *      esas2r interrupt handling
 *
 *  Copyright (c) 2001-2013 ATTO Technology, Inc.
 *  (mailto:linuxdrivers@attotech.com)
 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  NO WARRANTY
 *  THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 *  LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 *  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 *  solely responsible for determining the appropriateness of using and
 *  distributing the Program and assumes all risks associated with its
 *  exercise of rights under this Agreement, including but not limited to
 *  the risks and costs of program errors, damage to or loss of data,
 *  programs or equipment, and unavailability or interruption of operations.
 *
 *  DISCLAIMER OF LIABILITY
 *  NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 *  HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include "esas2r.h"

/* Local function prototypes */
static void esas2r_doorbell_interrupt(struct esas2r_adapter *a, u32 doorbell);
static void esas2r_get_outbound_responses(struct esas2r_adapter *a);
static void esas2r_process_bus_reset(struct esas2r_adapter *a);

/*
 * Poll the adapter for interrupts and service them.
 * This function handles both legacy interrupts and MSI.
 */
void esas2r_polled_interrupt(struct esas2r_adapter *a)
{
	u32 intstat;
	u32 doorbell;

	esas2r_disable_chip_interrupts(a);

	intstat = esas2r_read_register_dword(a, MU_INT_STATUS_OUT);

	if (intstat & MU_INTSTAT_POST_OUT) {
		/* clear the interrupt */

		esas2r_write_register_dword(a, MU_OUT_LIST_INT_STAT,
					    MU_OLIS_INT);
		esas2r_flush_register_dword(a, MU_OUT_LIST_INT_STAT);

		esas2r_get_outbound_responses(a);
	}

	if (intstat & MU_INTSTAT_DRBL) {
		doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
		if (doorbell != 0)
			esas2r_doorbell_interrupt(a, doorbell);
	}

	esas2r_enable_chip_interrupts(a);

	if (atomic_read(&a->disable_cnt) == 0)
		esas2r_do_deferred_processes(a);
}

/*
 * Legacy and MSI interrupt handlers.  Note that the legacy interrupt handler
 * schedules a TASKLET to process events, whereas the MSI handler just
 * processes interrupt events directly.
 */
irqreturn_t esas2r_interrupt(int irq, void *dev_id)
{
	struct esas2r_adapter *a = (struct esas2r_adapter *)dev_id;

	if (!esas2r_adapter_interrupt_pending(a))
		return IRQ_NONE;

	set_bit(AF2_INT_PENDING, &a->flags2);
	esas2r_schedule_tasklet(a);

	return IRQ_HANDLED;
}

void esas2r_adapter_interrupt(struct esas2r_adapter *a)
{
	u32 doorbell;

	if (likely(a->int_stat & MU_INTSTAT_POST_OUT)) {
		/* clear the interrupt */
		esas2r_write_register_dword(a, MU_OUT_LIST_INT_STAT,
					    MU_OLIS_INT);
		esas2r_flush_register_dword(a, MU_OUT_LIST_INT_STAT);
		esas2r_get_outbound_responses(a);
	}

	if (unlikely(a->int_stat & MU_INTSTAT_DRBL)) {
		doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
		if (doorbell != 0)
			esas2r_doorbell_interrupt(a, doorbell);
	}

	a->int_mask = ESAS2R_INT_STS_MASK;

	esas2r_enable_chip_interrupts(a);

	if (likely(atomic_read(&a->disable_cnt) == 0))
		esas2r_do_deferred_processes(a);
}

irqreturn_t esas2r_msi_interrupt(int irq, void *dev_id)
{
	struct esas2r_adapter *a = (struct esas2r_adapter *)dev_id;
	u32 intstat;
	u32 doorbell;

	intstat = esas2r_read_register_dword(a, MU_INT_STATUS_OUT);

	if (likely(intstat & MU_INTSTAT_POST_OUT)) {
		/* clear the interrupt */

		esas2r_write_register_dword(a, MU_OUT_LIST_INT_STAT,
					    MU_OLIS_INT);
		esas2r_flush_register_dword(a, MU_OUT_LIST_INT_STAT);

		esas2r_get_outbound_responses(a);
	}

	if (unlikely(intstat & MU_INTSTAT_DRBL)) {
		doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
		if (doorbell != 0)
			esas2r_doorbell_interrupt(a, doorbell);
	}

	/*
	 * Work around a chip bug and force a new MSI to be sent if one is
	 * still pending.
	 */
	esas2r_disable_chip_interrupts(a);
	esas2r_enable_chip_interrupts(a);

	if (likely(atomic_read(&a->disable_cnt) == 0))
		esas2r_do_deferred_processes(a);

	esas2r_do_tasklet_tasks(a);

	return 1;
}



static void esas2r_handle_outbound_rsp_err(struct esas2r_adapter *a,
					   struct esas2r_request *rq,
					   struct atto_vda_ob_rsp *rsp)
{

	/*
	 * For I/O requests, only copy the response if an error
	 * occurred and setup a callback to do error processing.
	 */
	if (unlikely(rq->req_stat != RS_SUCCESS)) {
		memcpy(&rq->func_rsp, &rsp->func_rsp, sizeof(rsp->func_rsp));

		if (rq->req_stat == RS_ABORTED) {
			if (rq->timeout > RQ_MAX_TIMEOUT)
				rq->req_stat = RS_TIMEOUT;
		} else if (rq->req_stat == RS_SCSI_ERROR) {
			u8 scsistatus = rq->func_rsp.scsi_rsp.scsi_stat;

			esas2r_trace("scsistatus: %x", scsistatus);

			/* Any of these are a good result. */
			if (scsistatus == SAM_STAT_GOOD || scsistatus ==
			    SAM_STAT_CONDITION_MET || scsistatus ==
			    SAM_STAT_INTERMEDIATE || scsistatus ==
			    SAM_STAT_INTERMEDIATE_CONDITION_MET) {
				rq->req_stat = RS_SUCCESS;
				rq->func_rsp.scsi_rsp.scsi_stat =
					SAM_STAT_GOOD;
			}
		}
	}
}

static void esas2r_get_outbound_responses(struct esas2r_adapter *a)
{
	struct atto_vda_ob_rsp *rsp;
	u32 rspput_ptr;
	u32 rspget_ptr;
	struct esas2r_request *rq;
	u32 handle;
	unsigned long flags;

	LIST_HEAD(comp_list);

	esas2r_trace_enter();

	spin_lock_irqsave(&a->queue_lock, flags);

	/* Get the outbound limit and pointers */
	rspput_ptr = le32_to_cpu(*a->outbound_copy) & MU_OLC_WRT_PTR;
	rspget_ptr = a->last_read;

	esas2r_trace("rspput_ptr: %x, rspget_ptr: %x", rspput_ptr, rspget_ptr);

	/* If we don't have anything to process, get out */
	if (unlikely(rspget_ptr == rspput_ptr)) {
		spin_unlock_irqrestore(&a->queue_lock, flags);
		esas2r_trace_exit();
		return;
	}

	/* Make sure the firmware is healthy */
	if (unlikely(rspput_ptr >= a->list_size)) {
		spin_unlock_irqrestore(&a->queue_lock, flags);
		esas2r_bugon();
		esas2r_local_reset_adapter(a);
		esas2r_trace_exit();
		return;
	}

	do {
		rspget_ptr++;

		if (rspget_ptr >= a->list_size)
			rspget_ptr = 0;

		rsp = (struct atto_vda_ob_rsp *)a->outbound_list_md.virt_addr
		      + rspget_ptr;

		handle = rsp->handle;

		/* Verify the handle range */
		if (unlikely(LOWORD(handle) == 0
			     || LOWORD(handle) > num_requests +
			     num_ae_requests + 1)) {
			esas2r_bugon();
			continue;
		}

		/* Get the request for this handle */
		rq = a->req_table[LOWORD(handle)];

		if (unlikely(rq == NULL || rq->vrq->scsi.handle != handle)) {
			esas2r_bugon();
			continue;
		}

		list_del(&rq->req_list);

		/* Get the completion status */
		rq->req_stat = rsp->req_stat;

		esas2r_trace("handle: %x", handle);
		esas2r_trace("rq: %p", rq);
		esas2r_trace("req_status: %x", rq->req_stat);

		if (likely(rq->vrq->scsi.function == VDA_FUNC_SCSI)) {
			esas2r_handle_outbound_rsp_err(a, rq, rsp);
		} else {
			/*
			 * Copy the outbound completion struct for non-I/O
			 * requests.
			 */
			memcpy(&rq->func_rsp, &rsp->func_rsp,
			       sizeof(rsp->func_rsp));
		}

		/* Queue the request for completion. */
		list_add_tail(&rq->comp_list, &comp_list);

	} while (rspget_ptr != rspput_ptr);

	a->last_read = rspget_ptr;
	spin_unlock_irqrestore(&a->queue_lock, flags);

	esas2r_comp_list_drain(a, &comp_list);
	esas2r_trace_exit();
}

/*
 * Perform all deferred processes for the adapter.  Deferred
 * processes can only be done while the current interrupt
 * disable_cnt for the adapter is zero.
 */
void esas2r_do_deferred_processes(struct esas2r_adapter *a)
{
	int startreqs = 2;
	struct esas2r_request *rq;
	unsigned long flags;

	/*
	 * startreqs is used to control starting requests
	 * that are on the deferred queue
	 *  = 0 - do not start any requests
	 *  = 1 - can start discovery requests
	 *  = 2 - can start any request
	 */

	if (test_bit(AF_CHPRST_PENDING, &a->flags) ||
	    test_bit(AF_FLASHING, &a->flags))
		startreqs = 0;
	else if (test_bit(AF_DISC_PENDING, &a->flags))
		startreqs = 1;

	atomic_inc(&a->disable_cnt);

	/* Clear off the completed list to be processed later. */

	if (esas2r_is_tasklet_pending(a)) {
		esas2r_schedule_tasklet(a);

		startreqs = 0;
	}

	/*
	 * If we can start requests then traverse the defer queue
	 * looking for requests to start or complete
	 */
	if (startreqs && !list_empty(&a->defer_list)) {
		LIST_HEAD(comp_list);
		struct list_head *element, *next;

		spin_lock_irqsave(&a->queue_lock, flags);

		list_for_each_safe(element, next, &a->defer_list) {
			rq = list_entry(element, struct esas2r_request,
					req_list);

			if (rq->req_stat != RS_PENDING) {
				list_del(element);
				list_add_tail(&rq->comp_list, &comp_list);
			}
			/*
			 * Process discovery and OS requests separately.  We
			 * can't hold up discovery requests when discovery is
			 * pending.  In general, there may be different sets of
			 * conditions for starting different types of requests.
			 */
			else if (rq->req_type == RT_DISC_REQ) {
				list_del(element);
				esas2r_disc_local_start_request(a, rq);
			} else if (startreqs == 2) {
				list_del(element);
				esas2r_local_start_request(a, rq);

				/*
				 * Flashing could have been set by last local
				 * start
				 */
				if (test_bit(AF_FLASHING, &a->flags))
					break;
			}
		}

		spin_unlock_irqrestore(&a->queue_lock, flags);
		esas2r_comp_list_drain(a, &comp_list);
	}

	atomic_dec(&a->disable_cnt);
}

/*
 * Process an adapter reset (or one that is about to happen)
 * by making sure all outstanding requests are completed that
 * haven't been already.
 */
void esas2r_process_adapter_reset(struct esas2r_adapter *a)
{
	struct esas2r_request *rq = &a->general_req;
	unsigned long flags;
	struct esas2r_disc_context *dc;

	LIST_HEAD(comp_list);
	struct list_head *element;

	esas2r_trace_enter();

	spin_lock_irqsave(&a->queue_lock, flags);

	/* abort the active discovery, if any.   */

	if (rq->interrupt_cx) {
		dc = (struct esas2r_disc_context *)rq->interrupt_cx;

		dc->disc_evt = 0;

		clear_bit(AF_DISC_IN_PROG, &a->flags);
	}

	/*
	 * just clear the interrupt callback for now.  it will be dequeued if
	 * and when we find it on the active queue and we don't want the
	 * callback called.  also set the dummy completion callback in case we
	 * were doing an I/O request.
	 */

	rq->interrupt_cx = NULL;
	rq->interrupt_cb = NULL;

	rq->comp_cb = esas2r_dummy_complete;

	/* Reset the read and write pointers */

	*a->outbound_copy =
		a->last_write =
			a->last_read = a->list_size - 1;

	set_bit(AF_COMM_LIST_TOGGLE, &a->flags);

	/* Kill all the requests on the active list */
	list_for_each(element, &a->defer_list) {
		rq = list_entry(element, struct esas2r_request, req_list);

		if (rq->req_stat == RS_STARTED)
			if (esas2r_ioreq_aborted(a, rq, RS_ABORTED))
				list_add_tail(&rq->comp_list, &comp_list);
	}

	spin_unlock_irqrestore(&a->queue_lock, flags);
	esas2r_comp_list_drain(a, &comp_list);
	esas2r_process_bus_reset(a);
	esas2r_trace_exit();
}

static void esas2r_process_bus_reset(struct esas2r_adapter *a)
{
	struct esas2r_request *rq;
	struct list_head *element;
	unsigned long flags;

	LIST_HEAD(comp_list);

	esas2r_trace_enter();

	esas2r_hdebug("reset detected");

	spin_lock_irqsave(&a->queue_lock, flags);

	/* kill all the requests on the deferred queue */
	list_for_each(element, &a->defer_list) {
		rq = list_entry(element, struct esas2r_request, req_list);
		if (esas2r_ioreq_aborted(a, rq, RS_ABORTED))
			list_add_tail(&rq->comp_list, &comp_list);
	}

	spin_unlock_irqrestore(&a->queue_lock, flags);

	esas2r_comp_list_drain(a, &comp_list);

	if (atomic_read(&a->disable_cnt) == 0)
		esas2r_do_deferred_processes(a);

	clear_bit(AF_OS_RESET, &a->flags);

	esas2r_trace_exit();
}

static void esas2r_chip_rst_needed_during_tasklet(struct esas2r_adapter *a)
{

	clear_bit(AF_CHPRST_NEEDED, &a->flags);
	clear_bit(AF_BUSRST_NEEDED, &a->flags);
	clear_bit(AF_BUSRST_DETECTED, &a->flags);
	clear_bit(AF_BUSRST_PENDING, &a->flags);
	/*
	 * Make sure we don't get attempt more than 3 resets
	 * when the uptime between resets does not exceed one
	 * minute.  This will stop any situation where there is
	 * really something wrong with the hardware.  The way
	 * this works is that we start with uptime ticks at 0.
	 * Each time we do a reset, we add 20 seconds worth to
	 * the count.  Each time a timer tick occurs, as long
	 * as a chip reset is not pending, we decrement the
	 * tick count.  If the uptime ticks ever gets to 60
	 * seconds worth, we disable the adapter from that
	 * point forward.  Three strikes, you're out.
	 */
	if (!esas2r_is_adapter_present(a) || (a->chip_uptime >=
					      ESAS2R_CHP_UPTIME_MAX)) {
		esas2r_hdebug("*** adapter disabled ***");

		/*
		 * Ok, some kind of hard failure.  Make sure we
		 * exit this loop with chip interrupts
		 * permanently disabled so we don't lock up the
		 * entire system.  Also flag degraded mode to
		 * prevent the heartbeat from trying to recover.
		 */

		set_bit(AF_DEGRADED_MODE, &a->flags);
		set_bit(AF_DISABLED, &a->flags);
		clear_bit(AF_CHPRST_PENDING, &a->flags);
		clear_bit(AF_DISC_PENDING, &a->flags);

		esas2r_disable_chip_interrupts(a);
		a->int_mask = 0;
		esas2r_process_adapter_reset(a);

		esas2r_log(ESAS2R_LOG_CRIT,
			   "Adapter disabled because of hardware failure");
	} else {
		bool alrdyrst = test_and_set_bit(AF_CHPRST_STARTED, &a->flags);

		if (!alrdyrst)
			/*
			 * Only disable interrupts if this is
			 * the first reset attempt.
			 */
			esas2r_disable_chip_interrupts(a);

		if ((test_bit(AF_POWER_MGT, &a->flags)) &&
		    !test_bit(AF_FIRST_INIT, &a->flags) && !alrdyrst) {
			/*
			 * Don't reset the chip on the first
			 * deferred power up attempt.
			 */
		} else {
			esas2r_hdebug("*** resetting chip ***");
			esas2r_reset_chip(a);
		}

		/* Kick off the reinitialization */
		a->chip_uptime += ESAS2R_CHP_UPTIME_CNT;
		a->chip_init_time = jiffies_to_msecs(jiffies);
		if (!test_bit(AF_POWER_MGT, &a->flags)) {
			esas2r_process_adapter_reset(a);

			if (!alrdyrst) {
				/* Remove devices now that I/O is cleaned up. */
				a->prev_dev_cnt =
					esas2r_targ_db_get_tgt_cnt(a);
				esas2r_targ_db_remove_all(a, false);
			}
		}

		a->int_mask = 0;
	}
}

static void esas2r_handle_chip_rst_during_tasklet(struct esas2r_adapter *a)
{
	while (test_bit(AF_CHPRST_DETECTED, &a->flags)) {
		/*
		 * Balance the enable in esas2r_initadapter_hw.
		 * Esas2r_power_down already took care of it for power
		 * management.
		 */
		if (!test_bit(AF_DEGRADED_MODE, &a->flags) &&
		    !test_bit(AF_POWER_MGT, &a->flags))
			esas2r_disable_chip_interrupts(a);

		/* Reinitialize the chip. */
		esas2r_check_adapter(a);
		esas2r_init_adapter_hw(a, 0);

		if (test_bit(AF_CHPRST_NEEDED, &a->flags))
			break;

		if (test_bit(AF_POWER_MGT, &a->flags)) {
			/* Recovery from power management. */
			if (test_bit(AF_FIRST_INIT, &a->flags)) {
				/* Chip reset during normal power up */
				esas2r_log(ESAS2R_LOG_CRIT,
					   "The firmware was reset during a normal power-up sequence");
			} else {
				/* Deferred power up complete. */
				clear_bit(AF_POWER_MGT, &a->flags);
				esas2r_send_reset_ae(a, true);
			}
		} else {
			/* Recovery from online chip reset. */
			if (test_bit(AF_FIRST_INIT, &a->flags)) {
				/* Chip reset during driver load */
			} else {
				/* Chip reset after driver load */
				esas2r_send_reset_ae(a, false);
			}

			esas2r_log(ESAS2R_LOG_CRIT,
				   "Recovering from a chip reset while the chip was online");
		}

		clear_bit(AF_CHPRST_STARTED, &a->flags);
		esas2r_enable_chip_interrupts(a);

		/*
		 * Clear this flag last!  this indicates that the chip has been
		 * reset already during initialization.
		 */
		clear_bit(AF_CHPRST_DETECTED, &a->flags);
	}
}


/* Perform deferred tasks when chip interrupts are disabled */
void esas2r_do_tasklet_tasks(struct esas2r_adapter *a)
{

	if (test_bit(AF_CHPRST_NEEDED, &a->flags) ||
	    test_bit(AF_CHPRST_DETECTED, &a->flags)) {
		if (test_bit(AF_CHPRST_NEEDED, &a->flags))
			esas2r_chip_rst_needed_during_tasklet(a);

		esas2r_handle_chip_rst_during_tasklet(a);
	}

	if (test_bit(AF_BUSRST_NEEDED, &a->flags)) {
		esas2r_hdebug("hard resetting bus");

		clear_bit(AF_BUSRST_NEEDED, &a->flags);

		if (test_bit(AF_FLASHING, &a->flags))
			set_bit(AF_BUSRST_DETECTED, &a->flags);
		else
			esas2r_write_register_dword(a, MU_DOORBELL_IN,
						    DRBL_RESET_BUS);
	}

	if (test_bit(AF_BUSRST_DETECTED, &a->flags)) {
		esas2r_process_bus_reset(a);

		esas2r_log_dev(ESAS2R_LOG_WARN,
			       &(a->host->shost_gendev),
			       "scsi_report_bus_reset() called");

		scsi_report_bus_reset(a->host, 0);

		clear_bit(AF_BUSRST_DETECTED, &a->flags);
		clear_bit(AF_BUSRST_PENDING, &a->flags);

		esas2r_log(ESAS2R_LOG_WARN, "Bus reset complete");
	}

	if (test_bit(AF_PORT_CHANGE, &a->flags)) {
		clear_bit(AF_PORT_CHANGE, &a->flags);

		esas2r_targ_db_report_changes(a);
	}

	if (atomic_read(&a->disable_cnt) == 0)
		esas2r_do_deferred_processes(a);
}

static void esas2r_doorbell_interrupt(struct esas2r_adapter *a, u32 doorbell)
{
	if (!(doorbell & DRBL_FORCE_INT)) {
		esas2r_trace_enter();
		esas2r_trace("doorbell: %x", doorbell);
	}

	/* First clear the doorbell bits */
	esas2r_write_register_dword(a, MU_DOORBELL_OUT, doorbell);

	if (doorbell & DRBL_RESET_BUS)
		set_bit(AF_BUSRST_DETECTED, &a->flags);

	if (doorbell & DRBL_FORCE_INT)
		clear_bit(AF_HEARTBEAT, &a->flags);

	if (doorbell & DRBL_PANIC_REASON_MASK) {
		esas2r_hdebug("*** Firmware Panic ***");
		esas2r_log(ESAS2R_LOG_CRIT, "The firmware has panicked");
	}

	if (doorbell & DRBL_FW_RESET) {
		set_bit(AF2_COREDUMP_AVAIL, &a->flags2);
		esas2r_local_reset_adapter(a);
	}

	if (!(doorbell & DRBL_FORCE_INT))
		esas2r_trace_exit();
}

void esas2r_force_interrupt(struct esas2r_adapter *a)
{
	esas2r_write_register_dword(a, MU_DOORBELL_IN, DRBL_FORCE_INT |
				    DRBL_DRV_VER);
}


static void esas2r_lun_event(struct esas2r_adapter *a, union atto_vda_ae *ae,
			     u16 target, u32 length)
{
	struct esas2r_target *t = a->targetdb + target;
	u32 cplen = length;
	unsigned long flags;

	if (cplen > sizeof(t->lu_event))
		cplen = sizeof(t->lu_event);

	esas2r_trace("ae->lu.dwevent: %x", ae->lu.dwevent);
	esas2r_trace("ae->lu.bystate: %x", ae->lu.bystate);

	spin_lock_irqsave(&a->mem_lock, flags);

	t->new_target_state = TS_INVALID;

	if (ae->lu.dwevent  & VDAAE_LU_LOST) {
		t->new_target_state = TS_NOT_PRESENT;
	} else {
		switch (ae->lu.bystate) {
		case VDAAE_LU_NOT_PRESENT:
		case VDAAE_LU_OFFLINE:
		case VDAAE_LU_DELETED:
		case VDAAE_LU_FACTORY_DISABLED:
			t->new_target_state = TS_NOT_PRESENT;
			break;

		case VDAAE_LU_ONLINE:
		case VDAAE_LU_DEGRADED:
			t->new_target_state = TS_PRESENT;
			break;
		}
	}

	if (t->new_target_state != TS_INVALID) {
		memcpy(&t->lu_event, &ae->lu, cplen);

		esas2r_disc_queue_event(a, DCDE_DEV_CHANGE);
	}

	spin_unlock_irqrestore(&a->mem_lock, flags);
}



void esas2r_ae_complete(struct esas2r_adapter *a, struct esas2r_request *rq)
{
	union atto_vda_ae *ae =
		(union atto_vda_ae *)rq->vda_rsp_data->ae_data.event_data;
	u32 length = le32_to_cpu(rq->func_rsp.ae_rsp.length);
	union atto_vda_ae *last =
		(union atto_vda_ae *)(rq->vda_rsp_data->ae_data.event_data
				      + length);

	esas2r_trace_enter();
	esas2r_trace("length: %d", length);

	if (length > sizeof(struct atto_vda_ae_data)
	    || (length & 3) != 0
	    || length == 0) {
		esas2r_log(ESAS2R_LOG_WARN,
			   "The AE request response length (%p) is too long: %d",
			   rq, length);

		esas2r_hdebug("aereq->length (0x%x) too long", length);
		esas2r_bugon();

		last = ae;
	}

	while (ae < last) {
		u16 target;

		esas2r_trace("ae: %p", ae);
		esas2r_trace("ae->hdr: %p", &(ae->hdr));

		length = ae->hdr.bylength;

		if (length > (u32)((u8 *)last - (u8 *)ae)
		    || (length & 3) != 0
		    || length == 0) {
			esas2r_log(ESAS2R_LOG_CRIT,
				   "the async event length is invalid (%p): %d",
				   ae, length);

			esas2r_hdebug("ae->hdr.length (0x%x) invalid", length);
			esas2r_bugon();

			break;
		}

		esas2r_nuxi_ae_data(ae);

		esas2r_queue_fw_event(a, fw_event_vda_ae, ae,
				      sizeof(union atto_vda_ae));

		switch (ae->hdr.bytype) {
		case VDAAE_HDR_TYPE_RAID:

			if (ae->raid.dwflags & (VDAAE_GROUP_STATE
						| VDAAE_RBLD_STATE
						| VDAAE_MEMBER_CHG
						| VDAAE_PART_CHG)) {
				esas2r_log(ESAS2R_LOG_INFO,
					   "RAID event received - name:%s rebuild_state:%d group_state:%d",
					   ae->raid.acname,
					   ae->raid.byrebuild_state,
					   ae->raid.bygroup_state);
			}

			break;

		case VDAAE_HDR_TYPE_LU:
			esas2r_log(ESAS2R_LOG_INFO,
				   "LUN event received: event:%d target_id:%d LUN:%d state:%d",
				   ae->lu.dwevent,
				   ae->lu.id.tgtlun.wtarget_id,
				   ae->lu.id.tgtlun.bylun,
				   ae->lu.bystate);

			target = ae->lu.id.tgtlun.wtarget_id;

			if (target < ESAS2R_MAX_TARGETS)
				esas2r_lun_event(a, ae, target, length);

			break;

		case VDAAE_HDR_TYPE_DISK:
			esas2r_log(ESAS2R_LOG_INFO, "Disk event received");
			break;

		default:

			/* Silently ignore the rest and let the apps deal with
			 * them.
			 */

			break;
		}

		ae = (union atto_vda_ae *)((u8 *)ae + length);
	}

	/* Now requeue it. */
	esas2r_start_ae_request(a, rq);
	esas2r_trace_exit();
}

/* Send an asynchronous event for a chip reset or power management. */
void esas2r_send_reset_ae(struct esas2r_adapter *a, bool pwr_mgt)
{
	struct atto_vda_ae_hdr ae;

	if (pwr_mgt)
		ae.bytype = VDAAE_HDR_TYPE_PWRMGT;
	else
		ae.bytype = VDAAE_HDR_TYPE_RESET;

	ae.byversion = VDAAE_HDR_VER_0;
	ae.byflags = 0;
	ae.bylength = (u8)sizeof(struct atto_vda_ae_hdr);

	if (pwr_mgt)
		esas2r_hdebug("*** sending power management AE ***");
	else
		esas2r_hdebug("*** sending reset AE ***");

	esas2r_queue_fw_event(a, fw_event_vda_ae, &ae,
			      sizeof(union atto_vda_ae));
}

void esas2r_dummy_complete(struct esas2r_adapter *a, struct esas2r_request *rq)
{}

static void esas2r_check_req_rsp_sense(struct esas2r_adapter *a,
				       struct esas2r_request *rq)
{
	u8 snslen, snslen2;

	snslen = snslen2 = rq->func_rsp.scsi_rsp.sense_len;

	if (snslen > rq->sense_len)
		snslen = rq->sense_len;

	if (snslen) {
		if (rq->sense_buf)
			memcpy(rq->sense_buf, rq->data_buf, snslen);
		else
			rq->sense_buf = (u8 *)rq->data_buf;

		/* See about possible sense data */
		if (snslen2 > 0x0c) {
			u8 *s = (u8 *)rq->data_buf;

			esas2r_trace_enter();

			/* Report LUNS data has changed */
			if (s[0x0c] == 0x3f && s[0x0d] == 0x0E) {
				esas2r_trace("rq->target_id: %d",
					     rq->target_id);
				esas2r_target_state_changed(a, rq->target_id,
							    TS_LUN_CHANGE);
			}

			esas2r_trace("add_sense_key=%x", s[0x0c]);
			esas2r_trace("add_sense_qual=%x", s[0x0d]);
			esas2r_trace_exit();
		}
	}

	rq->sense_len = snslen;
}


void esas2r_complete_request(struct esas2r_adapter *a,
			     struct esas2r_request *rq)
{
	if (rq->vrq->scsi.function == VDA_FUNC_FLASH
	    && rq->vrq->flash.sub_func == VDA_FLASH_COMMIT)
		clear_bit(AF_FLASHING, &a->flags);

	/* See if we setup a callback to do special processing */

	if (rq->interrupt_cb) {
		(*rq->interrupt_cb)(a, rq);

		if (rq->req_stat == RS_PENDING) {
			esas2r_start_request(a, rq);
			return;
		}
	}

	if (likely(rq->vrq->scsi.function == VDA_FUNC_SCSI)
	    && unlikely(rq->req_stat != RS_SUCCESS)) {
		esas2r_check_req_rsp_sense(a, rq);
		esas2r_log_request_failure(a, rq);
	}

	(*rq->comp_cb)(a, rq);
}