18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	Adaptec AAC series RAID controller driver
48c2ecf20Sopenharmony_ci *	(c) Copyright 2001 Red Hat Inc.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * based on the old aacraid driver that is..
78c2ecf20Sopenharmony_ci * Adaptec aacraid device driver for Linux.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (c) 2000-2010 Adaptec, Inc.
108c2ecf20Sopenharmony_ci *               2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
118c2ecf20Sopenharmony_ci *		 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Module Name:
148c2ecf20Sopenharmony_ci *   linit.c
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * Abstract: Linux Driver entry module for Adaptec RAID Array Controller
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/compat.h>
218c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
228c2ecf20Sopenharmony_ci#include <linux/completion.h>
238c2ecf20Sopenharmony_ci#include <linux/init.h>
248c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
258c2ecf20Sopenharmony_ci#include <linux/kernel.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
288c2ecf20Sopenharmony_ci#include <linux/pci.h>
298c2ecf20Sopenharmony_ci#include <linux/aer.h>
308c2ecf20Sopenharmony_ci#include <linux/slab.h>
318c2ecf20Sopenharmony_ci#include <linux/mutex.h>
328c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
338c2ecf20Sopenharmony_ci#include <linux/syscalls.h>
348c2ecf20Sopenharmony_ci#include <linux/delay.h>
358c2ecf20Sopenharmony_ci#include <linux/kthread.h>
368c2ecf20Sopenharmony_ci#include <linux/msdos_partition.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
398c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
408c2ecf20Sopenharmony_ci#include <scsi/scsi_device.h>
418c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
428c2ecf20Sopenharmony_ci#include <scsi/scsi_tcq.h>
438c2ecf20Sopenharmony_ci#include <scsi/scsicam.h>
448c2ecf20Sopenharmony_ci#include <scsi/scsi_eh.h>
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#include "aacraid.h"
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define AAC_DRIVER_VERSION		"1.2.1"
498c2ecf20Sopenharmony_ci#ifndef AAC_DRIVER_BRANCH
508c2ecf20Sopenharmony_ci#define AAC_DRIVER_BRANCH		""
518c2ecf20Sopenharmony_ci#endif
528c2ecf20Sopenharmony_ci#define AAC_DRIVERNAME			"aacraid"
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#ifdef AAC_DRIVER_BUILD
558c2ecf20Sopenharmony_ci#define _str(x) #x
568c2ecf20Sopenharmony_ci#define str(x) _str(x)
578c2ecf20Sopenharmony_ci#define AAC_DRIVER_FULL_VERSION	AAC_DRIVER_VERSION "[" str(AAC_DRIVER_BUILD) "]" AAC_DRIVER_BRANCH
588c2ecf20Sopenharmony_ci#else
598c2ecf20Sopenharmony_ci#define AAC_DRIVER_FULL_VERSION	AAC_DRIVER_VERSION AAC_DRIVER_BRANCH
608c2ecf20Sopenharmony_ci#endif
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciMODULE_AUTHOR("Red Hat Inc and Adaptec");
638c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Dell PERC2, 2/Si, 3/Si, 3/Di, "
648c2ecf20Sopenharmony_ci		   "Adaptec Advanced Raid Products, "
658c2ecf20Sopenharmony_ci		   "HP NetRAID-4M, IBM ServeRAID & ICP SCSI driver");
668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
678c2ecf20Sopenharmony_ciMODULE_VERSION(AAC_DRIVER_FULL_VERSION);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(aac_mutex);
708c2ecf20Sopenharmony_cistatic LIST_HEAD(aac_devices);
718c2ecf20Sopenharmony_cistatic int aac_cfg_major = AAC_CHARDEV_UNREGISTERED;
728c2ecf20Sopenharmony_cichar aac_driver_version[] = AAC_DRIVER_FULL_VERSION;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/*
758c2ecf20Sopenharmony_ci * Because of the way Linux names scsi devices, the order in this table has
768c2ecf20Sopenharmony_ci * become important.  Check for on-board Raid first, add-in cards second.
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * Note: The last field is used to index into aac_drivers below.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_cistatic const struct pci_device_id aac_pci_tbl[] = {
818c2ecf20Sopenharmony_ci	{ 0x1028, 0x0001, 0x1028, 0x0001, 0, 0, 0 }, /* PERC 2/Si (Iguana/PERC2Si) */
828c2ecf20Sopenharmony_ci	{ 0x1028, 0x0002, 0x1028, 0x0002, 0, 0, 1 }, /* PERC 3/Di (Opal/PERC3Di) */
838c2ecf20Sopenharmony_ci	{ 0x1028, 0x0003, 0x1028, 0x0003, 0, 0, 2 }, /* PERC 3/Si (SlimFast/PERC3Si */
848c2ecf20Sopenharmony_ci	{ 0x1028, 0x0004, 0x1028, 0x00d0, 0, 0, 3 }, /* PERC 3/Di (Iguana FlipChip/PERC3DiF */
858c2ecf20Sopenharmony_ci	{ 0x1028, 0x0002, 0x1028, 0x00d1, 0, 0, 4 }, /* PERC 3/Di (Viper/PERC3DiV) */
868c2ecf20Sopenharmony_ci	{ 0x1028, 0x0002, 0x1028, 0x00d9, 0, 0, 5 }, /* PERC 3/Di (Lexus/PERC3DiL) */
878c2ecf20Sopenharmony_ci	{ 0x1028, 0x000a, 0x1028, 0x0106, 0, 0, 6 }, /* PERC 3/Di (Jaguar/PERC3DiJ) */
888c2ecf20Sopenharmony_ci	{ 0x1028, 0x000a, 0x1028, 0x011b, 0, 0, 7 }, /* PERC 3/Di (Dagger/PERC3DiD) */
898c2ecf20Sopenharmony_ci	{ 0x1028, 0x000a, 0x1028, 0x0121, 0, 0, 8 }, /* PERC 3/Di (Boxster/PERC3DiB) */
908c2ecf20Sopenharmony_ci	{ 0x9005, 0x0283, 0x9005, 0x0283, 0, 0, 9 }, /* catapult */
918c2ecf20Sopenharmony_ci	{ 0x9005, 0x0284, 0x9005, 0x0284, 0, 0, 10 }, /* tomcat */
928c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0286, 0, 0, 11 }, /* Adaptec 2120S (Crusader) */
938c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0285, 0, 0, 12 }, /* Adaptec 2200S (Vulcan) */
948c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0287, 0, 0, 13 }, /* Adaptec 2200S (Vulcan-2m) */
958c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x17aa, 0x0286, 0, 0, 14 }, /* Legend S220 (Legend Crusader) */
968c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x17aa, 0x0287, 0, 0, 15 }, /* Legend S230 (Legend Vulcan) */
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0288, 0, 0, 16 }, /* Adaptec 3230S (Harrier) */
998c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0289, 0, 0, 17 }, /* Adaptec 3240S (Tornado) */
1008c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x028a, 0, 0, 18 }, /* ASR-2020ZCR SCSI PCI-X ZCR (Skyhawk) */
1018c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x028b, 0, 0, 19 }, /* ASR-2025ZCR SCSI SO-DIMM PCI-X ZCR (Terminator) */
1028c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x028c, 0, 0, 20 }, /* ASR-2230S + ASR-2230SLP PCI-X (Lancer) */
1038c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x028d, 0, 0, 21 }, /* ASR-2130S (Lancer) */
1048c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x029b, 0, 0, 22 }, /* AAR-2820SA (Intruder) */
1058c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x029c, 0, 0, 23 }, /* AAR-2620SA (Intruder) */
1068c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x029d, 0, 0, 24 }, /* AAR-2420SA (Intruder) */
1078c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x029e, 0, 0, 25 }, /* ICP9024RO (Lancer) */
1088c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x029f, 0, 0, 26 }, /* ICP9014RO (Lancer) */
1098c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x02a0, 0, 0, 27 }, /* ICP9047MA (Lancer) */
1108c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x02a1, 0, 0, 28 }, /* ICP9087MA (Lancer) */
1118c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x02a3, 0, 0, 29 }, /* ICP5445AU (Hurricane44) */
1128c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x02a4, 0, 0, 30 }, /* ICP9085LI (Marauder-X) */
1138c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x02a5, 0, 0, 31 }, /* ICP5085BR (Marauder-E) */
1148c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x02a6, 0, 0, 32 }, /* ICP9067MA (Intruder-6) */
1158c2ecf20Sopenharmony_ci	{ 0x9005, 0x0287, 0x9005, 0x0800, 0, 0, 33 }, /* Themisto Jupiter Platform */
1168c2ecf20Sopenharmony_ci	{ 0x9005, 0x0200, 0x9005, 0x0200, 0, 0, 33 }, /* Themisto Jupiter Platform */
1178c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x0800, 0, 0, 34 }, /* Callisto Jupiter Platform */
1188c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x028e, 0, 0, 35 }, /* ASR-2020SA SATA PCI-X ZCR (Skyhawk) */
1198c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x028f, 0, 0, 36 }, /* ASR-2025SA SATA SO-DIMM PCI-X ZCR (Terminator) */
1208c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0290, 0, 0, 37 }, /* AAR-2410SA PCI SATA 4ch (Jaguar II) */
1218c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x1028, 0x0291, 0, 0, 38 }, /* CERC SATA RAID 2 PCI SATA 6ch (DellCorsair) */
1228c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0292, 0, 0, 39 }, /* AAR-2810SA PCI SATA 8ch (Corsair-8) */
1238c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0293, 0, 0, 40 }, /* AAR-21610SA PCI SATA 16ch (Corsair-16) */
1248c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0294, 0, 0, 41 }, /* ESD SO-DIMM PCI-X SATA ZCR (Prowler) */
1258c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x103C, 0x3227, 0, 0, 42 }, /* AAR-2610SA PCI SATA 6ch */
1268c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0296, 0, 0, 43 }, /* ASR-2240S (SabreExpress) */
1278c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0297, 0, 0, 44 }, /* ASR-4005 */
1288c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x1014, 0x02F2, 0, 0, 45 }, /* IBM 8i (AvonPark) */
1298c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x1014, 0x0312, 0, 0, 45 }, /* IBM 8i (AvonPark Lite) */
1308c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x1014, 0x9580, 0, 0, 46 }, /* IBM 8k/8k-l8 (Aurora) */
1318c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x1014, 0x9540, 0, 0, 47 }, /* IBM 8k/8k-l4 (Aurora Lite) */
1328c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0298, 0, 0, 48 }, /* ASR-4000 (BlackBird) */
1338c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x0299, 0, 0, 49 }, /* ASR-4800SAS (Marauder-X) */
1348c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x9005, 0x029a, 0, 0, 50 }, /* ASR-4805SAS (Marauder-E) */
1358c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, 0x9005, 0x02a2, 0, 0, 51 }, /* ASR-3800 (Hurricane44) */
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x1028, 0x0287, 0, 0, 52 }, /* Perc 320/DC*/
1388c2ecf20Sopenharmony_ci	{ 0x1011, 0x0046, 0x9005, 0x0365, 0, 0, 53 }, /* Adaptec 5400S (Mustang)*/
1398c2ecf20Sopenharmony_ci	{ 0x1011, 0x0046, 0x9005, 0x0364, 0, 0, 54 }, /* Adaptec 5400S (Mustang)*/
1408c2ecf20Sopenharmony_ci	{ 0x1011, 0x0046, 0x9005, 0x1364, 0, 0, 55 }, /* Dell PERC2/QC */
1418c2ecf20Sopenharmony_ci	{ 0x1011, 0x0046, 0x103c, 0x10c2, 0, 0, 56 }, /* HP NetRAID-4M */
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x1028, PCI_ANY_ID, 0, 0, 57 }, /* Dell Catchall */
1448c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, 0x17aa, PCI_ANY_ID, 0, 0, 58 }, /* Legend Catchall */
1458c2ecf20Sopenharmony_ci	{ 0x9005, 0x0285, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 59 }, /* Adaptec Catch All */
1468c2ecf20Sopenharmony_ci	{ 0x9005, 0x0286, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 60 }, /* Adaptec Rocket Catch All */
1478c2ecf20Sopenharmony_ci	{ 0x9005, 0x0288, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 61 }, /* Adaptec NEMER/ARK Catch All */
1488c2ecf20Sopenharmony_ci	{ 0x9005, 0x028b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 62 }, /* Adaptec PMC Series 6 (Tupelo) */
1498c2ecf20Sopenharmony_ci	{ 0x9005, 0x028c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 63 }, /* Adaptec PMC Series 7 (Denali) */
1508c2ecf20Sopenharmony_ci	{ 0x9005, 0x028d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 64 }, /* Adaptec PMC Series 8 */
1518c2ecf20Sopenharmony_ci	{ 0,}
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, aac_pci_tbl);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/*
1568c2ecf20Sopenharmony_ci * dmb - For now we add the number of channels to this structure.
1578c2ecf20Sopenharmony_ci * In the future we should add a fib that reports the number of channels
1588c2ecf20Sopenharmony_ci * for the card.  At that time we can remove the channels from here
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic struct aac_driver_ident aac_drivers[] = {
1618c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 2/Si (Iguana/PERC2Si) */
1628c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Di (Opal/PERC3Di) */
1638c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Si (SlimFast/PERC3Si */
1648c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Di (Iguana FlipChip/PERC3DiF */
1658c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Di (Viper/PERC3DiV) */
1668c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Di (Lexus/PERC3DiL) */
1678c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 1, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Di (Jaguar/PERC3DiJ) */
1688c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Di (Dagger/PERC3DiD) */
1698c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERCRAID        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* PERC 3/Di (Boxster/PERC3DiB) */
1708c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "catapult        ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* catapult */
1718c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "tomcat          ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* tomcat */
1728c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "Adaptec 2120S   ", 1, AAC_QUIRK_31BIT | AAC_QUIRK_34SG },		      /* Adaptec 2120S (Crusader) */
1738c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "Adaptec 2200S   ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG },		      /* Adaptec 2200S (Vulcan) */
1748c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "Adaptec 2200S   ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* Adaptec 2200S (Vulcan-2m) */
1758c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "Legend  ", "Legend S220     ", 1, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* Legend S220 (Legend Crusader) */
1768c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "Legend  ", "Legend S230     ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* Legend S230 (Legend Vulcan) */
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "Adaptec 3230S   ", 2 }, /* Adaptec 3230S (Harrier) */
1798c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "Adaptec 3240S   ", 2 }, /* Adaptec 3240S (Tornado) */
1808c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-2020ZCR     ", 2 }, /* ASR-2020ZCR SCSI PCI-X ZCR (Skyhawk) */
1818c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-2025ZCR     ", 2 }, /* ASR-2025ZCR SCSI SO-DIMM PCI-X ZCR (Terminator) */
1828c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ADAPTEC ", "ASR-2230S PCI-X ", 2 }, /* ASR-2230S + ASR-2230SLP PCI-X (Lancer) */
1838c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ADAPTEC ", "ASR-2130S PCI-X ", 1 }, /* ASR-2130S (Lancer) */
1848c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ADAPTEC ", "AAR-2820SA      ", 1 }, /* AAR-2820SA (Intruder) */
1858c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ADAPTEC ", "AAR-2620SA      ", 1 }, /* AAR-2620SA (Intruder) */
1868c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ADAPTEC ", "AAR-2420SA      ", 1 }, /* AAR-2420SA (Intruder) */
1878c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ICP     ", "ICP9024RO       ", 2 }, /* ICP9024RO (Lancer) */
1888c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ICP     ", "ICP9014RO       ", 1 }, /* ICP9014RO (Lancer) */
1898c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ICP     ", "ICP9047MA       ", 1 }, /* ICP9047MA (Lancer) */
1908c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ICP     ", "ICP9087MA       ", 1 }, /* ICP9087MA (Lancer) */
1918c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ICP     ", "ICP5445AU       ", 1 }, /* ICP5445AU (Hurricane44) */
1928c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ICP     ", "ICP9085LI       ", 1 }, /* ICP9085LI (Marauder-X) */
1938c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ICP     ", "ICP5085BR       ", 1 }, /* ICP5085BR (Marauder-E) */
1948c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ICP     ", "ICP9067MA       ", 1 }, /* ICP9067MA (Intruder-6) */
1958c2ecf20Sopenharmony_ci	{ NULL        , "aacraid",  "ADAPTEC ", "Themisto        ", 0, AAC_QUIRK_SLAVE }, /* Jupiter Platform */
1968c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ADAPTEC ", "Callisto        ", 2, AAC_QUIRK_MASTER }, /* Jupiter Platform */
1978c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-2020SA       ", 1 }, /* ASR-2020SA SATA PCI-X ZCR (Skyhawk) */
1988c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-2025SA       ", 1 }, /* ASR-2025SA SATA SO-DIMM PCI-X ZCR (Terminator) */
1998c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "AAR-2410SA SATA ", 1, AAC_QUIRK_17SG }, /* AAR-2410SA PCI SATA 4ch (Jaguar II) */
2008c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "DELL    ", "CERC SR2        ", 1, AAC_QUIRK_17SG }, /* CERC SATA RAID 2 PCI SATA 6ch (DellCorsair) */
2018c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "AAR-2810SA SATA ", 1, AAC_QUIRK_17SG }, /* AAR-2810SA PCI SATA 8ch (Corsair-8) */
2028c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "AAR-21610SA SATA", 1, AAC_QUIRK_17SG }, /* AAR-21610SA PCI SATA 16ch (Corsair-16) */
2038c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-2026ZCR     ", 1 }, /* ESD SO-DIMM PCI-X SATA ZCR (Prowler) */
2048c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "AAR-2610SA      ", 1 }, /* SATA 6Ch (Bearcat) */
2058c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-2240S       ", 1 }, /* ASR-2240S (SabreExpress) */
2068c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-4005        ", 1 }, /* ASR-4005 */
2078c2ecf20Sopenharmony_ci	{ aac_rx_init, "ServeRAID","IBM     ", "ServeRAID 8i    ", 1 }, /* IBM 8i (AvonPark) */
2088c2ecf20Sopenharmony_ci	{ aac_rkt_init, "ServeRAID","IBM     ", "ServeRAID 8k-l8 ", 1 }, /* IBM 8k/8k-l8 (Aurora) */
2098c2ecf20Sopenharmony_ci	{ aac_rkt_init, "ServeRAID","IBM     ", "ServeRAID 8k-l4 ", 1 }, /* IBM 8k/8k-l4 (Aurora Lite) */
2108c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-4000        ", 1 }, /* ASR-4000 (BlackBird & AvonPark) */
2118c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-4800SAS     ", 1 }, /* ASR-4800SAS (Marauder-X) */
2128c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "ASR-4805SAS     ", 1 }, /* ASR-4805SAS (Marauder-E) */
2138c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid",  "ADAPTEC ", "ASR-3800        ", 1 }, /* ASR-3800 (Hurricane44) */
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	{ aac_rx_init, "percraid", "DELL    ", "PERC 320/DC     ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG }, /* Perc 320/DC*/
2168c2ecf20Sopenharmony_ci	{ aac_sa_init, "aacraid",  "ADAPTEC ", "Adaptec 5400S   ", 4, AAC_QUIRK_34SG }, /* Adaptec 5400S (Mustang)*/
2178c2ecf20Sopenharmony_ci	{ aac_sa_init, "aacraid",  "ADAPTEC ", "AAC-364         ", 4, AAC_QUIRK_34SG }, /* Adaptec 5400S (Mustang)*/
2188c2ecf20Sopenharmony_ci	{ aac_sa_init, "percraid", "DELL    ", "PERCRAID        ", 4, AAC_QUIRK_34SG }, /* Dell PERC2/QC */
2198c2ecf20Sopenharmony_ci	{ aac_sa_init, "hpnraid",  "HP      ", "NetRAID         ", 4, AAC_QUIRK_34SG }, /* HP NetRAID-4M */
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "DELL    ", "RAID            ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* Dell Catchall */
2228c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "Legend  ", "RAID            ", 2, AAC_QUIRK_31BIT | AAC_QUIRK_34SG | AAC_QUIRK_SCSI_32 }, /* Legend Catchall */
2238c2ecf20Sopenharmony_ci	{ aac_rx_init, "aacraid",  "ADAPTEC ", "RAID            ", 2 }, /* Adaptec Catch All */
2248c2ecf20Sopenharmony_ci	{ aac_rkt_init, "aacraid", "ADAPTEC ", "RAID            ", 2 }, /* Adaptec Rocket Catch All */
2258c2ecf20Sopenharmony_ci	{ aac_nark_init, "aacraid", "ADAPTEC ", "RAID           ", 2 }, /* Adaptec NEMER/ARK Catch All */
2268c2ecf20Sopenharmony_ci	{ aac_src_init, "aacraid", "ADAPTEC ", "RAID            ", 2, AAC_QUIRK_SRC }, /* Adaptec PMC Series 6 (Tupelo) */
2278c2ecf20Sopenharmony_ci	{ aac_srcv_init, "aacraid", "ADAPTEC ", "RAID            ", 2, AAC_QUIRK_SRC }, /* Adaptec PMC Series 7 (Denali) */
2288c2ecf20Sopenharmony_ci	{ aac_srcv_init, "aacraid", "ADAPTEC ", "RAID            ", 2, AAC_QUIRK_SRC }, /* Adaptec PMC Series 8 */
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/**
2328c2ecf20Sopenharmony_ci *	aac_queuecommand	-	queue a SCSI command
2338c2ecf20Sopenharmony_ci *	@shost:		Scsi host to queue command on
2348c2ecf20Sopenharmony_ci *	@cmd:		SCSI command to queue
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci *	Queues a command for execution by the associated Host Adapter.
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci *	TODO: unify with aac_scsi_cmd().
2398c2ecf20Sopenharmony_ci */
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int aac_queuecommand(struct Scsi_Host *shost,
2428c2ecf20Sopenharmony_ci			    struct scsi_cmnd *cmd)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	int r = 0;
2458c2ecf20Sopenharmony_ci	cmd->SCp.phase = AAC_OWNER_LOWLEVEL;
2468c2ecf20Sopenharmony_ci	r = (aac_scsi_cmd(cmd) ? FAILED : 0);
2478c2ecf20Sopenharmony_ci	return r;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/**
2518c2ecf20Sopenharmony_ci *	aac_info		-	Returns the host adapter name
2528c2ecf20Sopenharmony_ci *	@shost:		Scsi host to report on
2538c2ecf20Sopenharmony_ci *
2548c2ecf20Sopenharmony_ci *	Returns a static string describing the device in question
2558c2ecf20Sopenharmony_ci */
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic const char *aac_info(struct Scsi_Host *shost)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev *)shost->hostdata;
2608c2ecf20Sopenharmony_ci	return aac_drivers[dev->cardtype].name;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/**
2648c2ecf20Sopenharmony_ci *	aac_get_driver_ident
2658c2ecf20Sopenharmony_ci *	@devtype: index into lookup table
2668c2ecf20Sopenharmony_ci *
2678c2ecf20Sopenharmony_ci *	Returns a pointer to the entry in the driver lookup table.
2688c2ecf20Sopenharmony_ci */
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistruct aac_driver_ident* aac_get_driver_ident(int devtype)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	return &aac_drivers[devtype];
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci/**
2768c2ecf20Sopenharmony_ci *	aac_biosparm	-	return BIOS parameters for disk
2778c2ecf20Sopenharmony_ci *	@sdev: The scsi device corresponding to the disk
2788c2ecf20Sopenharmony_ci *	@bdev: the block device corresponding to the disk
2798c2ecf20Sopenharmony_ci *	@capacity: the sector capacity of the disk
2808c2ecf20Sopenharmony_ci *	@geom: geometry block to fill in
2818c2ecf20Sopenharmony_ci *
2828c2ecf20Sopenharmony_ci *	Return the Heads/Sectors/Cylinders BIOS Disk Parameters for Disk.
2838c2ecf20Sopenharmony_ci *	The default disk geometry is 64 heads, 32 sectors, and the appropriate
2848c2ecf20Sopenharmony_ci *	number of cylinders so as not to exceed drive capacity.  In order for
2858c2ecf20Sopenharmony_ci *	disks equal to or larger than 1 GB to be addressable by the BIOS
2868c2ecf20Sopenharmony_ci *	without exceeding the BIOS limitation of 1024 cylinders, Extended
2878c2ecf20Sopenharmony_ci *	Translation should be enabled.   With Extended Translation enabled,
2888c2ecf20Sopenharmony_ci *	drives between 1 GB inclusive and 2 GB exclusive are given a disk
2898c2ecf20Sopenharmony_ci *	geometry of 128 heads and 32 sectors, and drives above 2 GB inclusive
2908c2ecf20Sopenharmony_ci *	are given a disk geometry of 255 heads and 63 sectors.  However, if
2918c2ecf20Sopenharmony_ci *	the BIOS detects that the Extended Translation setting does not match
2928c2ecf20Sopenharmony_ci *	the geometry in the partition table, then the translation inferred
2938c2ecf20Sopenharmony_ci *	from the partition table will be used by the BIOS, and a warning may
2948c2ecf20Sopenharmony_ci *	be displayed.
2958c2ecf20Sopenharmony_ci */
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic int aac_biosparm(struct scsi_device *sdev, struct block_device *bdev,
2988c2ecf20Sopenharmony_ci			sector_t capacity, int *geom)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	struct diskparm *param = (struct diskparm *)geom;
3018c2ecf20Sopenharmony_ci	unsigned char *buf;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	dprintk((KERN_DEBUG "aac_biosparm.\n"));
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/*
3068c2ecf20Sopenharmony_ci	 *	Assuming extended translation is enabled - #REVISIT#
3078c2ecf20Sopenharmony_ci	 */
3088c2ecf20Sopenharmony_ci	if (capacity >= 2 * 1024 * 1024) { /* 1 GB in 512 byte sectors */
3098c2ecf20Sopenharmony_ci		if(capacity >= 4 * 1024 * 1024) { /* 2 GB in 512 byte sectors */
3108c2ecf20Sopenharmony_ci			param->heads = 255;
3118c2ecf20Sopenharmony_ci			param->sectors = 63;
3128c2ecf20Sopenharmony_ci		} else {
3138c2ecf20Sopenharmony_ci			param->heads = 128;
3148c2ecf20Sopenharmony_ci			param->sectors = 32;
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci	} else {
3178c2ecf20Sopenharmony_ci		param->heads = 64;
3188c2ecf20Sopenharmony_ci		param->sectors = 32;
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	param->cylinders = cap_to_cyls(capacity, param->heads * param->sectors);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	/*
3248c2ecf20Sopenharmony_ci	 *	Read the first 1024 bytes from the disk device, if the boot
3258c2ecf20Sopenharmony_ci	 *	sector partition table is valid, search for a partition table
3268c2ecf20Sopenharmony_ci	 *	entry whose end_head matches one of the standard geometry
3278c2ecf20Sopenharmony_ci	 *	translations ( 64/32, 128/32, 255/63 ).
3288c2ecf20Sopenharmony_ci	 */
3298c2ecf20Sopenharmony_ci	buf = scsi_bios_ptable(bdev);
3308c2ecf20Sopenharmony_ci	if (!buf)
3318c2ecf20Sopenharmony_ci		return 0;
3328c2ecf20Sopenharmony_ci	if (*(__le16 *)(buf + 0x40) == cpu_to_le16(MSDOS_LABEL_MAGIC)) {
3338c2ecf20Sopenharmony_ci		struct msdos_partition *first = (struct msdos_partition *)buf;
3348c2ecf20Sopenharmony_ci		struct msdos_partition *entry = first;
3358c2ecf20Sopenharmony_ci		int saved_cylinders = param->cylinders;
3368c2ecf20Sopenharmony_ci		int num;
3378c2ecf20Sopenharmony_ci		unsigned char end_head, end_sec;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci		for(num = 0; num < 4; num++) {
3408c2ecf20Sopenharmony_ci			end_head = entry->end_head;
3418c2ecf20Sopenharmony_ci			end_sec = entry->end_sector & 0x3f;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci			if(end_head == 63) {
3448c2ecf20Sopenharmony_ci				param->heads = 64;
3458c2ecf20Sopenharmony_ci				param->sectors = 32;
3468c2ecf20Sopenharmony_ci				break;
3478c2ecf20Sopenharmony_ci			} else if(end_head == 127) {
3488c2ecf20Sopenharmony_ci				param->heads = 128;
3498c2ecf20Sopenharmony_ci				param->sectors = 32;
3508c2ecf20Sopenharmony_ci				break;
3518c2ecf20Sopenharmony_ci			} else if(end_head == 254) {
3528c2ecf20Sopenharmony_ci				param->heads = 255;
3538c2ecf20Sopenharmony_ci				param->sectors = 63;
3548c2ecf20Sopenharmony_ci				break;
3558c2ecf20Sopenharmony_ci			}
3568c2ecf20Sopenharmony_ci			entry++;
3578c2ecf20Sopenharmony_ci		}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci		if (num == 4) {
3608c2ecf20Sopenharmony_ci			end_head = first->end_head;
3618c2ecf20Sopenharmony_ci			end_sec = first->end_sector & 0x3f;
3628c2ecf20Sopenharmony_ci		}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci		param->cylinders = cap_to_cyls(capacity, param->heads * param->sectors);
3658c2ecf20Sopenharmony_ci		if (num < 4 && end_sec == param->sectors) {
3668c2ecf20Sopenharmony_ci			if (param->cylinders != saved_cylinders) {
3678c2ecf20Sopenharmony_ci				dprintk((KERN_DEBUG "Adopting geometry: heads=%d, sectors=%d from partition table %d.\n",
3688c2ecf20Sopenharmony_ci					param->heads, param->sectors, num));
3698c2ecf20Sopenharmony_ci			}
3708c2ecf20Sopenharmony_ci		} else if (end_head > 0 || end_sec > 0) {
3718c2ecf20Sopenharmony_ci			dprintk((KERN_DEBUG "Strange geometry: heads=%d, sectors=%d in partition table %d.\n",
3728c2ecf20Sopenharmony_ci				end_head + 1, end_sec, num));
3738c2ecf20Sopenharmony_ci			dprintk((KERN_DEBUG "Using geometry: heads=%d, sectors=%d.\n",
3748c2ecf20Sopenharmony_ci					param->heads, param->sectors));
3758c2ecf20Sopenharmony_ci		}
3768c2ecf20Sopenharmony_ci	}
3778c2ecf20Sopenharmony_ci	kfree(buf);
3788c2ecf20Sopenharmony_ci	return 0;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/**
3828c2ecf20Sopenharmony_ci *	aac_slave_configure		-	compute queue depths
3838c2ecf20Sopenharmony_ci *	@sdev:	SCSI device we are considering
3848c2ecf20Sopenharmony_ci *
3858c2ecf20Sopenharmony_ci *	Selects queue depths for each target device based on the host adapter's
3868c2ecf20Sopenharmony_ci *	total capacity and the queue depth supported by the target device.
3878c2ecf20Sopenharmony_ci *	A queue depth of one automatically disables tagged queueing.
3888c2ecf20Sopenharmony_ci */
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_cistatic int aac_slave_configure(struct scsi_device *sdev)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
3938c2ecf20Sopenharmony_ci	int chn, tid;
3948c2ecf20Sopenharmony_ci	unsigned int depth = 0;
3958c2ecf20Sopenharmony_ci	unsigned int set_timeout = 0;
3968c2ecf20Sopenharmony_ci	int timeout = 0;
3978c2ecf20Sopenharmony_ci	bool set_qd_dev_type = false;
3988c2ecf20Sopenharmony_ci	u8 devtype = 0;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	chn = aac_logical_to_phys(sdev_channel(sdev));
4018c2ecf20Sopenharmony_ci	tid = sdev_id(sdev);
4028c2ecf20Sopenharmony_ci	if (chn < AAC_MAX_BUSES && tid < AAC_MAX_TARGETS && aac->sa_firmware) {
4038c2ecf20Sopenharmony_ci		devtype = aac->hba_map[chn][tid].devtype;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci		if (devtype == AAC_DEVTYPE_NATIVE_RAW) {
4068c2ecf20Sopenharmony_ci			depth = aac->hba_map[chn][tid].qd_limit;
4078c2ecf20Sopenharmony_ci			set_timeout = 1;
4088c2ecf20Sopenharmony_ci			goto common_config;
4098c2ecf20Sopenharmony_ci		}
4108c2ecf20Sopenharmony_ci		if (devtype == AAC_DEVTYPE_ARC_RAW) {
4118c2ecf20Sopenharmony_ci			set_qd_dev_type = true;
4128c2ecf20Sopenharmony_ci			set_timeout = 1;
4138c2ecf20Sopenharmony_ci			goto common_config;
4148c2ecf20Sopenharmony_ci		}
4158c2ecf20Sopenharmony_ci	}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	if (aac->jbod && (sdev->type == TYPE_DISK))
4188c2ecf20Sopenharmony_ci		sdev->removable = 1;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	if (sdev->type == TYPE_DISK
4218c2ecf20Sopenharmony_ci	 && sdev_channel(sdev) != CONTAINER_CHANNEL
4228c2ecf20Sopenharmony_ci	 && (!aac->jbod || sdev->inq_periph_qual)
4238c2ecf20Sopenharmony_ci	 && (!aac->raid_scsi_mode || (sdev_channel(sdev) != 2))) {
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci		if (expose_physicals == 0)
4268c2ecf20Sopenharmony_ci			return -ENXIO;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		if (expose_physicals < 0)
4298c2ecf20Sopenharmony_ci			sdev->no_uld_attach = 1;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	if (sdev->tagged_supported
4338c2ecf20Sopenharmony_ci	 &&  sdev->type == TYPE_DISK
4348c2ecf20Sopenharmony_ci	 &&  (!aac->raid_scsi_mode || (sdev_channel(sdev) != 2))
4358c2ecf20Sopenharmony_ci	 && !sdev->no_uld_attach) {
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci		struct scsi_device * dev;
4388c2ecf20Sopenharmony_ci		struct Scsi_Host *host = sdev->host;
4398c2ecf20Sopenharmony_ci		unsigned num_lsu = 0;
4408c2ecf20Sopenharmony_ci		unsigned num_one = 0;
4418c2ecf20Sopenharmony_ci		unsigned cid;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		set_timeout = 1;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci		for (cid = 0; cid < aac->maximum_num_containers; ++cid)
4468c2ecf20Sopenharmony_ci			if (aac->fsa_dev[cid].valid)
4478c2ecf20Sopenharmony_ci				++num_lsu;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		__shost_for_each_device(dev, host) {
4508c2ecf20Sopenharmony_ci			if (dev->tagged_supported
4518c2ecf20Sopenharmony_ci			 && dev->type == TYPE_DISK
4528c2ecf20Sopenharmony_ci			 && (!aac->raid_scsi_mode || (sdev_channel(sdev) != 2))
4538c2ecf20Sopenharmony_ci			 && !dev->no_uld_attach) {
4548c2ecf20Sopenharmony_ci				if ((sdev_channel(dev) != CONTAINER_CHANNEL)
4558c2ecf20Sopenharmony_ci				 || !aac->fsa_dev[sdev_id(dev)].valid) {
4568c2ecf20Sopenharmony_ci					++num_lsu;
4578c2ecf20Sopenharmony_ci				}
4588c2ecf20Sopenharmony_ci			} else {
4598c2ecf20Sopenharmony_ci				++num_one;
4608c2ecf20Sopenharmony_ci			}
4618c2ecf20Sopenharmony_ci		}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci		if (num_lsu == 0)
4648c2ecf20Sopenharmony_ci			++num_lsu;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci		depth = (host->can_queue - num_one) / num_lsu;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci		if (sdev_channel(sdev) != NATIVE_CHANNEL)
4698c2ecf20Sopenharmony_ci			goto common_config;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci		set_qd_dev_type = true;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cicommon_config:
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	/*
4788c2ecf20Sopenharmony_ci	 * Check if SATA drive
4798c2ecf20Sopenharmony_ci	 */
4808c2ecf20Sopenharmony_ci	if (set_qd_dev_type) {
4818c2ecf20Sopenharmony_ci		if (strncmp(sdev->vendor, "ATA", 3) == 0)
4828c2ecf20Sopenharmony_ci			depth = 32;
4838c2ecf20Sopenharmony_ci		else
4848c2ecf20Sopenharmony_ci			depth = 64;
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	/*
4888c2ecf20Sopenharmony_ci	 * Firmware has an individual device recovery time typically
4898c2ecf20Sopenharmony_ci	 * of 35 seconds, give us a margin. Thor devices can take longer in
4908c2ecf20Sopenharmony_ci	 * error recovery, hence different value.
4918c2ecf20Sopenharmony_ci	 */
4928c2ecf20Sopenharmony_ci	if (set_timeout) {
4938c2ecf20Sopenharmony_ci		timeout = aac->sa_firmware ? AAC_SA_TIMEOUT : AAC_ARC_TIMEOUT;
4948c2ecf20Sopenharmony_ci		blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	if (depth > 256)
4988c2ecf20Sopenharmony_ci		depth = 256;
4998c2ecf20Sopenharmony_ci	else if (depth < 1)
5008c2ecf20Sopenharmony_ci		depth = 1;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	scsi_change_queue_depth(sdev, depth);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	sdev->tagged_supported = 1;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	return 0;
5078c2ecf20Sopenharmony_ci}
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci/**
5108c2ecf20Sopenharmony_ci *	aac_change_queue_depth		-	alter queue depths
5118c2ecf20Sopenharmony_ci *	@sdev:	SCSI device we are considering
5128c2ecf20Sopenharmony_ci *	@depth:	desired queue depth
5138c2ecf20Sopenharmony_ci *
5148c2ecf20Sopenharmony_ci *	Alters queue depths for target device based on the host adapter's
5158c2ecf20Sopenharmony_ci *	total capacity and the queue depth supported by the target device.
5168c2ecf20Sopenharmony_ci */
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int aac_change_queue_depth(struct scsi_device *sdev, int depth)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)(sdev->host->hostdata);
5218c2ecf20Sopenharmony_ci	int chn, tid, is_native_device = 0;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	chn = aac_logical_to_phys(sdev_channel(sdev));
5248c2ecf20Sopenharmony_ci	tid = sdev_id(sdev);
5258c2ecf20Sopenharmony_ci	if (chn < AAC_MAX_BUSES && tid < AAC_MAX_TARGETS &&
5268c2ecf20Sopenharmony_ci		aac->hba_map[chn][tid].devtype == AAC_DEVTYPE_NATIVE_RAW)
5278c2ecf20Sopenharmony_ci		is_native_device = 1;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	if (sdev->tagged_supported && (sdev->type == TYPE_DISK) &&
5308c2ecf20Sopenharmony_ci	    (sdev_channel(sdev) == CONTAINER_CHANNEL)) {
5318c2ecf20Sopenharmony_ci		struct scsi_device * dev;
5328c2ecf20Sopenharmony_ci		struct Scsi_Host *host = sdev->host;
5338c2ecf20Sopenharmony_ci		unsigned num = 0;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci		__shost_for_each_device(dev, host) {
5368c2ecf20Sopenharmony_ci			if (dev->tagged_supported && (dev->type == TYPE_DISK) &&
5378c2ecf20Sopenharmony_ci			    (sdev_channel(dev) == CONTAINER_CHANNEL))
5388c2ecf20Sopenharmony_ci				++num;
5398c2ecf20Sopenharmony_ci			++num;
5408c2ecf20Sopenharmony_ci		}
5418c2ecf20Sopenharmony_ci		if (num >= host->can_queue)
5428c2ecf20Sopenharmony_ci			num = host->can_queue - 1;
5438c2ecf20Sopenharmony_ci		if (depth > (host->can_queue - num))
5448c2ecf20Sopenharmony_ci			depth = host->can_queue - num;
5458c2ecf20Sopenharmony_ci		if (depth > 256)
5468c2ecf20Sopenharmony_ci			depth = 256;
5478c2ecf20Sopenharmony_ci		else if (depth < 2)
5488c2ecf20Sopenharmony_ci			depth = 2;
5498c2ecf20Sopenharmony_ci		return scsi_change_queue_depth(sdev, depth);
5508c2ecf20Sopenharmony_ci	} else if (is_native_device) {
5518c2ecf20Sopenharmony_ci		scsi_change_queue_depth(sdev, aac->hba_map[chn][tid].qd_limit);
5528c2ecf20Sopenharmony_ci	} else {
5538c2ecf20Sopenharmony_ci		scsi_change_queue_depth(sdev, 1);
5548c2ecf20Sopenharmony_ci	}
5558c2ecf20Sopenharmony_ci	return sdev->queue_depth;
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic ssize_t aac_show_raid_level(struct device *dev, struct device_attribute *attr, char *buf)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	struct scsi_device *sdev = to_scsi_device(dev);
5618c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)(sdev->host->hostdata);
5628c2ecf20Sopenharmony_ci	if (sdev_channel(sdev) != CONTAINER_CHANNEL)
5638c2ecf20Sopenharmony_ci		return snprintf(buf, PAGE_SIZE, sdev->no_uld_attach
5648c2ecf20Sopenharmony_ci		  ? "Hidden\n" :
5658c2ecf20Sopenharmony_ci		  ((aac->jbod && (sdev->type == TYPE_DISK)) ? "JBOD\n" : ""));
5668c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n",
5678c2ecf20Sopenharmony_ci	  get_container_type(aac->fsa_dev[sdev_id(sdev)].type));
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic struct device_attribute aac_raid_level_attr = {
5718c2ecf20Sopenharmony_ci	.attr = {
5728c2ecf20Sopenharmony_ci		.name = "level",
5738c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
5748c2ecf20Sopenharmony_ci	},
5758c2ecf20Sopenharmony_ci	.show = aac_show_raid_level
5768c2ecf20Sopenharmony_ci};
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic ssize_t aac_show_unique_id(struct device *dev,
5798c2ecf20Sopenharmony_ci	     struct device_attribute *attr, char *buf)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	struct scsi_device *sdev = to_scsi_device(dev);
5828c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)(sdev->host->hostdata);
5838c2ecf20Sopenharmony_ci	unsigned char sn[16];
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	memset(sn, 0, sizeof(sn));
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	if (sdev_channel(sdev) == CONTAINER_CHANNEL)
5888c2ecf20Sopenharmony_ci		memcpy(sn, aac->fsa_dev[sdev_id(sdev)].identifier, sizeof(sn));
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	return snprintf(buf, 16 * 2 + 2,
5918c2ecf20Sopenharmony_ci		"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X\n",
5928c2ecf20Sopenharmony_ci		sn[0], sn[1], sn[2], sn[3],
5938c2ecf20Sopenharmony_ci		sn[4], sn[5], sn[6], sn[7],
5948c2ecf20Sopenharmony_ci		sn[8], sn[9], sn[10], sn[11],
5958c2ecf20Sopenharmony_ci		sn[12], sn[13], sn[14], sn[15]);
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_cistatic struct device_attribute aac_unique_id_attr = {
5998c2ecf20Sopenharmony_ci	.attr = {
6008c2ecf20Sopenharmony_ci		.name = "unique_id",
6018c2ecf20Sopenharmony_ci		.mode = 0444,
6028c2ecf20Sopenharmony_ci	},
6038c2ecf20Sopenharmony_ci	.show = aac_show_unique_id
6048c2ecf20Sopenharmony_ci};
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic struct device_attribute *aac_dev_attrs[] = {
6098c2ecf20Sopenharmony_ci	&aac_raid_level_attr,
6108c2ecf20Sopenharmony_ci	&aac_unique_id_attr,
6118c2ecf20Sopenharmony_ci	NULL,
6128c2ecf20Sopenharmony_ci};
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic int aac_ioctl(struct scsi_device *sdev, unsigned int cmd,
6158c2ecf20Sopenharmony_ci		     void __user *arg)
6168c2ecf20Sopenharmony_ci{
6178c2ecf20Sopenharmony_ci	int retval;
6188c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
6198c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_RAWIO))
6208c2ecf20Sopenharmony_ci		return -EPERM;
6218c2ecf20Sopenharmony_ci	retval = aac_adapter_check_health(dev);
6228c2ecf20Sopenharmony_ci	if (retval)
6238c2ecf20Sopenharmony_ci		return -EBUSY;
6248c2ecf20Sopenharmony_ci	return aac_do_ioctl(dev, cmd, arg);
6258c2ecf20Sopenharmony_ci}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_cistruct fib_count_data {
6288c2ecf20Sopenharmony_ci	int mlcnt;
6298c2ecf20Sopenharmony_ci	int llcnt;
6308c2ecf20Sopenharmony_ci	int ehcnt;
6318c2ecf20Sopenharmony_ci	int fwcnt;
6328c2ecf20Sopenharmony_ci	int krlcnt;
6338c2ecf20Sopenharmony_ci};
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_cistatic bool fib_count_iter(struct scsi_cmnd *scmnd, void *data, bool reserved)
6368c2ecf20Sopenharmony_ci{
6378c2ecf20Sopenharmony_ci	struct fib_count_data *fib_count = data;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	switch (scmnd->SCp.phase) {
6408c2ecf20Sopenharmony_ci	case AAC_OWNER_FIRMWARE:
6418c2ecf20Sopenharmony_ci		fib_count->fwcnt++;
6428c2ecf20Sopenharmony_ci		break;
6438c2ecf20Sopenharmony_ci	case AAC_OWNER_ERROR_HANDLER:
6448c2ecf20Sopenharmony_ci		fib_count->ehcnt++;
6458c2ecf20Sopenharmony_ci		break;
6468c2ecf20Sopenharmony_ci	case AAC_OWNER_LOWLEVEL:
6478c2ecf20Sopenharmony_ci		fib_count->llcnt++;
6488c2ecf20Sopenharmony_ci		break;
6498c2ecf20Sopenharmony_ci	case AAC_OWNER_MIDLEVEL:
6508c2ecf20Sopenharmony_ci		fib_count->mlcnt++;
6518c2ecf20Sopenharmony_ci		break;
6528c2ecf20Sopenharmony_ci	default:
6538c2ecf20Sopenharmony_ci		fib_count->krlcnt++;
6548c2ecf20Sopenharmony_ci		break;
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci	return true;
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci/* Called during SCSI EH, so we don't need to block requests */
6608c2ecf20Sopenharmony_cistatic int get_num_of_incomplete_fibs(struct aac_dev *aac)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = aac->scsi_host_ptr;
6638c2ecf20Sopenharmony_ci	struct device *ctrl_dev;
6648c2ecf20Sopenharmony_ci	struct fib_count_data fcnt = { };
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	scsi_host_busy_iter(shost, fib_count_iter, &fcnt);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	ctrl_dev = &aac->pdev->dev;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	dev_info(ctrl_dev, "outstanding cmd: midlevel-%d\n", fcnt.mlcnt);
6718c2ecf20Sopenharmony_ci	dev_info(ctrl_dev, "outstanding cmd: lowlevel-%d\n", fcnt.llcnt);
6728c2ecf20Sopenharmony_ci	dev_info(ctrl_dev, "outstanding cmd: error handler-%d\n", fcnt.ehcnt);
6738c2ecf20Sopenharmony_ci	dev_info(ctrl_dev, "outstanding cmd: firmware-%d\n", fcnt.fwcnt);
6748c2ecf20Sopenharmony_ci	dev_info(ctrl_dev, "outstanding cmd: kernel-%d\n", fcnt.krlcnt);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	return fcnt.mlcnt + fcnt.llcnt + fcnt.ehcnt + fcnt.fwcnt;
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic int aac_eh_abort(struct scsi_cmnd* cmd)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	struct scsi_device * dev = cmd->device;
6828c2ecf20Sopenharmony_ci	struct Scsi_Host * host = dev->host;
6838c2ecf20Sopenharmony_ci	struct aac_dev * aac = (struct aac_dev *)host->hostdata;
6848c2ecf20Sopenharmony_ci	int count, found;
6858c2ecf20Sopenharmony_ci	u32 bus, cid;
6868c2ecf20Sopenharmony_ci	int ret = FAILED;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	if (aac_adapter_check_health(aac))
6898c2ecf20Sopenharmony_ci		return ret;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	bus = aac_logical_to_phys(scmd_channel(cmd));
6928c2ecf20Sopenharmony_ci	cid = scmd_id(cmd);
6938c2ecf20Sopenharmony_ci	if (aac->hba_map[bus][cid].devtype == AAC_DEVTYPE_NATIVE_RAW) {
6948c2ecf20Sopenharmony_ci		struct fib *fib;
6958c2ecf20Sopenharmony_ci		struct aac_hba_tm_req *tmf;
6968c2ecf20Sopenharmony_ci		int status;
6978c2ecf20Sopenharmony_ci		u64 address;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci		pr_err("%s: Host adapter abort request (%d,%d,%d,%d)\n",
7008c2ecf20Sopenharmony_ci		 AAC_DRIVERNAME,
7018c2ecf20Sopenharmony_ci		 host->host_no, sdev_channel(dev), sdev_id(dev), (int)dev->lun);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci		found = 0;
7048c2ecf20Sopenharmony_ci		for (count = 0; count < (host->can_queue + AAC_NUM_MGT_FIB); ++count) {
7058c2ecf20Sopenharmony_ci			fib = &aac->fibs[count];
7068c2ecf20Sopenharmony_ci			if (*(u8 *)fib->hw_fib_va != 0 &&
7078c2ecf20Sopenharmony_ci				(fib->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) &&
7088c2ecf20Sopenharmony_ci				(fib->callback_data == cmd)) {
7098c2ecf20Sopenharmony_ci				found = 1;
7108c2ecf20Sopenharmony_ci				break;
7118c2ecf20Sopenharmony_ci			}
7128c2ecf20Sopenharmony_ci		}
7138c2ecf20Sopenharmony_ci		if (!found)
7148c2ecf20Sopenharmony_ci			return ret;
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci		/* start a HBA_TMF_ABORT_TASK TMF request */
7178c2ecf20Sopenharmony_ci		fib = aac_fib_alloc(aac);
7188c2ecf20Sopenharmony_ci		if (!fib)
7198c2ecf20Sopenharmony_ci			return ret;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		tmf = (struct aac_hba_tm_req *)fib->hw_fib_va;
7228c2ecf20Sopenharmony_ci		memset(tmf, 0, sizeof(*tmf));
7238c2ecf20Sopenharmony_ci		tmf->tmf = HBA_TMF_ABORT_TASK;
7248c2ecf20Sopenharmony_ci		tmf->it_nexus = aac->hba_map[bus][cid].rmw_nexus;
7258c2ecf20Sopenharmony_ci		tmf->lun[1] = cmd->device->lun;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci		address = (u64)fib->hw_error_pa;
7288c2ecf20Sopenharmony_ci		tmf->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
7298c2ecf20Sopenharmony_ci		tmf->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
7308c2ecf20Sopenharmony_ci		tmf->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci		fib->hbacmd_size = sizeof(*tmf);
7338c2ecf20Sopenharmony_ci		cmd->SCp.sent_command = 0;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci		status = aac_hba_send(HBA_IU_TYPE_SCSI_TM_REQ, fib,
7368c2ecf20Sopenharmony_ci				  (fib_callback) aac_hba_callback,
7378c2ecf20Sopenharmony_ci				  (void *) cmd);
7388c2ecf20Sopenharmony_ci		if (status != -EINPROGRESS) {
7398c2ecf20Sopenharmony_ci			aac_fib_complete(fib);
7408c2ecf20Sopenharmony_ci			aac_fib_free(fib);
7418c2ecf20Sopenharmony_ci			return ret;
7428c2ecf20Sopenharmony_ci		}
7438c2ecf20Sopenharmony_ci		/* Wait up to 15 secs for completion */
7448c2ecf20Sopenharmony_ci		for (count = 0; count < 15; ++count) {
7458c2ecf20Sopenharmony_ci			if (cmd->SCp.sent_command) {
7468c2ecf20Sopenharmony_ci				ret = SUCCESS;
7478c2ecf20Sopenharmony_ci				break;
7488c2ecf20Sopenharmony_ci			}
7498c2ecf20Sopenharmony_ci			msleep(1000);
7508c2ecf20Sopenharmony_ci		}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci		if (ret != SUCCESS)
7538c2ecf20Sopenharmony_ci			pr_err("%s: Host adapter abort request timed out\n",
7548c2ecf20Sopenharmony_ci			AAC_DRIVERNAME);
7558c2ecf20Sopenharmony_ci	} else {
7568c2ecf20Sopenharmony_ci		pr_err(
7578c2ecf20Sopenharmony_ci			"%s: Host adapter abort request.\n"
7588c2ecf20Sopenharmony_ci			"%s: Outstanding commands on (%d,%d,%d,%d):\n",
7598c2ecf20Sopenharmony_ci			AAC_DRIVERNAME, AAC_DRIVERNAME,
7608c2ecf20Sopenharmony_ci			host->host_no, sdev_channel(dev), sdev_id(dev),
7618c2ecf20Sopenharmony_ci			(int)dev->lun);
7628c2ecf20Sopenharmony_ci		switch (cmd->cmnd[0]) {
7638c2ecf20Sopenharmony_ci		case SERVICE_ACTION_IN_16:
7648c2ecf20Sopenharmony_ci			if (!(aac->raw_io_interface) ||
7658c2ecf20Sopenharmony_ci			    !(aac->raw_io_64) ||
7668c2ecf20Sopenharmony_ci			    ((cmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
7678c2ecf20Sopenharmony_ci				break;
7688c2ecf20Sopenharmony_ci			fallthrough;
7698c2ecf20Sopenharmony_ci		case INQUIRY:
7708c2ecf20Sopenharmony_ci		case READ_CAPACITY:
7718c2ecf20Sopenharmony_ci			/*
7728c2ecf20Sopenharmony_ci			 * Mark associated FIB to not complete,
7738c2ecf20Sopenharmony_ci			 * eh handler does this
7748c2ecf20Sopenharmony_ci			 */
7758c2ecf20Sopenharmony_ci			for (count = 0;
7768c2ecf20Sopenharmony_ci				count < (host->can_queue + AAC_NUM_MGT_FIB);
7778c2ecf20Sopenharmony_ci				++count) {
7788c2ecf20Sopenharmony_ci				struct fib *fib = &aac->fibs[count];
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci				if (fib->hw_fib_va->header.XferState &&
7818c2ecf20Sopenharmony_ci				(fib->flags & FIB_CONTEXT_FLAG) &&
7828c2ecf20Sopenharmony_ci				(fib->callback_data == cmd)) {
7838c2ecf20Sopenharmony_ci					fib->flags |=
7848c2ecf20Sopenharmony_ci						FIB_CONTEXT_FLAG_TIMED_OUT;
7858c2ecf20Sopenharmony_ci					cmd->SCp.phase =
7868c2ecf20Sopenharmony_ci						AAC_OWNER_ERROR_HANDLER;
7878c2ecf20Sopenharmony_ci					ret = SUCCESS;
7888c2ecf20Sopenharmony_ci				}
7898c2ecf20Sopenharmony_ci			}
7908c2ecf20Sopenharmony_ci			break;
7918c2ecf20Sopenharmony_ci		case TEST_UNIT_READY:
7928c2ecf20Sopenharmony_ci			/*
7938c2ecf20Sopenharmony_ci			 * Mark associated FIB to not complete,
7948c2ecf20Sopenharmony_ci			 * eh handler does this
7958c2ecf20Sopenharmony_ci			 */
7968c2ecf20Sopenharmony_ci			for (count = 0;
7978c2ecf20Sopenharmony_ci				count < (host->can_queue + AAC_NUM_MGT_FIB);
7988c2ecf20Sopenharmony_ci				++count) {
7998c2ecf20Sopenharmony_ci				struct scsi_cmnd *command;
8008c2ecf20Sopenharmony_ci				struct fib *fib = &aac->fibs[count];
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci				command = fib->callback_data;
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci				if ((fib->hw_fib_va->header.XferState &
8058c2ecf20Sopenharmony_ci					cpu_to_le32
8068c2ecf20Sopenharmony_ci					(Async | NoResponseExpected)) &&
8078c2ecf20Sopenharmony_ci					(fib->flags & FIB_CONTEXT_FLAG) &&
8088c2ecf20Sopenharmony_ci					((command)) &&
8098c2ecf20Sopenharmony_ci					(command->device == cmd->device)) {
8108c2ecf20Sopenharmony_ci					fib->flags |=
8118c2ecf20Sopenharmony_ci						FIB_CONTEXT_FLAG_TIMED_OUT;
8128c2ecf20Sopenharmony_ci					command->SCp.phase =
8138c2ecf20Sopenharmony_ci						AAC_OWNER_ERROR_HANDLER;
8148c2ecf20Sopenharmony_ci					if (command == cmd)
8158c2ecf20Sopenharmony_ci						ret = SUCCESS;
8168c2ecf20Sopenharmony_ci				}
8178c2ecf20Sopenharmony_ci			}
8188c2ecf20Sopenharmony_ci			break;
8198c2ecf20Sopenharmony_ci		}
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci	return ret;
8228c2ecf20Sopenharmony_ci}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic u8 aac_eh_tmf_lun_reset_fib(struct aac_hba_map_info *info,
8258c2ecf20Sopenharmony_ci				   struct fib *fib, u64 tmf_lun)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct aac_hba_tm_req *tmf;
8288c2ecf20Sopenharmony_ci	u64 address;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	/* start a HBA_TMF_LUN_RESET TMF request */
8318c2ecf20Sopenharmony_ci	tmf = (struct aac_hba_tm_req *)fib->hw_fib_va;
8328c2ecf20Sopenharmony_ci	memset(tmf, 0, sizeof(*tmf));
8338c2ecf20Sopenharmony_ci	tmf->tmf = HBA_TMF_LUN_RESET;
8348c2ecf20Sopenharmony_ci	tmf->it_nexus = info->rmw_nexus;
8358c2ecf20Sopenharmony_ci	int_to_scsilun(tmf_lun, (struct scsi_lun *)tmf->lun);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	address = (u64)fib->hw_error_pa;
8388c2ecf20Sopenharmony_ci	tmf->error_ptr_hi = cpu_to_le32
8398c2ecf20Sopenharmony_ci		((u32)(address >> 32));
8408c2ecf20Sopenharmony_ci	tmf->error_ptr_lo = cpu_to_le32
8418c2ecf20Sopenharmony_ci		((u32)(address & 0xffffffff));
8428c2ecf20Sopenharmony_ci	tmf->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
8438c2ecf20Sopenharmony_ci	fib->hbacmd_size = sizeof(*tmf);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	return HBA_IU_TYPE_SCSI_TM_REQ;
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic u8 aac_eh_tmf_hard_reset_fib(struct aac_hba_map_info *info,
8498c2ecf20Sopenharmony_ci				    struct fib *fib)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	struct aac_hba_reset_req *rst;
8528c2ecf20Sopenharmony_ci	u64 address;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	/* already tried, start a hard reset now */
8558c2ecf20Sopenharmony_ci	rst = (struct aac_hba_reset_req *)fib->hw_fib_va;
8568c2ecf20Sopenharmony_ci	memset(rst, 0, sizeof(*rst));
8578c2ecf20Sopenharmony_ci	rst->it_nexus = info->rmw_nexus;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	address = (u64)fib->hw_error_pa;
8608c2ecf20Sopenharmony_ci	rst->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
8618c2ecf20Sopenharmony_ci	rst->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
8628c2ecf20Sopenharmony_ci	rst->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
8638c2ecf20Sopenharmony_ci	fib->hbacmd_size = sizeof(*rst);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci       return HBA_IU_TYPE_SATA_REQ;
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic void aac_tmf_callback(void *context, struct fib *fibptr)
8698c2ecf20Sopenharmony_ci{
8708c2ecf20Sopenharmony_ci	struct aac_hba_resp *err =
8718c2ecf20Sopenharmony_ci		&((struct aac_native_hba *)fibptr->hw_fib_va)->resp.err;
8728c2ecf20Sopenharmony_ci	struct aac_hba_map_info *info = context;
8738c2ecf20Sopenharmony_ci	int res;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	switch (err->service_response) {
8768c2ecf20Sopenharmony_ci	case HBA_RESP_SVCRES_TMF_REJECTED:
8778c2ecf20Sopenharmony_ci		res = -1;
8788c2ecf20Sopenharmony_ci		break;
8798c2ecf20Sopenharmony_ci	case HBA_RESP_SVCRES_TMF_LUN_INVALID:
8808c2ecf20Sopenharmony_ci		res = 0;
8818c2ecf20Sopenharmony_ci		break;
8828c2ecf20Sopenharmony_ci	case HBA_RESP_SVCRES_TMF_COMPLETE:
8838c2ecf20Sopenharmony_ci	case HBA_RESP_SVCRES_TMF_SUCCEEDED:
8848c2ecf20Sopenharmony_ci		res = 0;
8858c2ecf20Sopenharmony_ci		break;
8868c2ecf20Sopenharmony_ci	default:
8878c2ecf20Sopenharmony_ci		res = -2;
8888c2ecf20Sopenharmony_ci		break;
8898c2ecf20Sopenharmony_ci	}
8908c2ecf20Sopenharmony_ci	aac_fib_complete(fibptr);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	info->reset_state = res;
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci/*
8968c2ecf20Sopenharmony_ci *	aac_eh_dev_reset	- Device reset command handling
8978c2ecf20Sopenharmony_ci *	@scsi_cmd:	SCSI command block causing the reset
8988c2ecf20Sopenharmony_ci *
8998c2ecf20Sopenharmony_ci */
9008c2ecf20Sopenharmony_cistatic int aac_eh_dev_reset(struct scsi_cmnd *cmd)
9018c2ecf20Sopenharmony_ci{
9028c2ecf20Sopenharmony_ci	struct scsi_device * dev = cmd->device;
9038c2ecf20Sopenharmony_ci	struct Scsi_Host * host = dev->host;
9048c2ecf20Sopenharmony_ci	struct aac_dev * aac = (struct aac_dev *)host->hostdata;
9058c2ecf20Sopenharmony_ci	struct aac_hba_map_info *info;
9068c2ecf20Sopenharmony_ci	int count;
9078c2ecf20Sopenharmony_ci	u32 bus, cid;
9088c2ecf20Sopenharmony_ci	struct fib *fib;
9098c2ecf20Sopenharmony_ci	int ret = FAILED;
9108c2ecf20Sopenharmony_ci	int status;
9118c2ecf20Sopenharmony_ci	u8 command;
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	bus = aac_logical_to_phys(scmd_channel(cmd));
9148c2ecf20Sopenharmony_ci	cid = scmd_id(cmd);
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	if (bus >= AAC_MAX_BUSES || cid >= AAC_MAX_TARGETS)
9178c2ecf20Sopenharmony_ci		return FAILED;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	info = &aac->hba_map[bus][cid];
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (!(info->devtype == AAC_DEVTYPE_NATIVE_RAW &&
9228c2ecf20Sopenharmony_ci	 !(info->reset_state > 0)))
9238c2ecf20Sopenharmony_ci		return FAILED;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	pr_err("%s: Host device reset request. SCSI hang ?\n",
9268c2ecf20Sopenharmony_ci	       AAC_DRIVERNAME);
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	fib = aac_fib_alloc(aac);
9298c2ecf20Sopenharmony_ci	if (!fib)
9308c2ecf20Sopenharmony_ci		return ret;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	/* start a HBA_TMF_LUN_RESET TMF request */
9338c2ecf20Sopenharmony_ci	command = aac_eh_tmf_lun_reset_fib(info, fib, dev->lun);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	info->reset_state = 1;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	status = aac_hba_send(command, fib,
9388c2ecf20Sopenharmony_ci			      (fib_callback) aac_tmf_callback,
9398c2ecf20Sopenharmony_ci			      (void *) info);
9408c2ecf20Sopenharmony_ci	if (status != -EINPROGRESS) {
9418c2ecf20Sopenharmony_ci		info->reset_state = 0;
9428c2ecf20Sopenharmony_ci		aac_fib_complete(fib);
9438c2ecf20Sopenharmony_ci		aac_fib_free(fib);
9448c2ecf20Sopenharmony_ci		return ret;
9458c2ecf20Sopenharmony_ci	}
9468c2ecf20Sopenharmony_ci	/* Wait up to 15 seconds for completion */
9478c2ecf20Sopenharmony_ci	for (count = 0; count < 15; ++count) {
9488c2ecf20Sopenharmony_ci		if (info->reset_state == 0) {
9498c2ecf20Sopenharmony_ci			ret = info->reset_state == 0 ? SUCCESS : FAILED;
9508c2ecf20Sopenharmony_ci			break;
9518c2ecf20Sopenharmony_ci		}
9528c2ecf20Sopenharmony_ci		msleep(1000);
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	return ret;
9568c2ecf20Sopenharmony_ci}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci/*
9598c2ecf20Sopenharmony_ci *	aac_eh_target_reset	- Target reset command handling
9608c2ecf20Sopenharmony_ci *	@scsi_cmd:	SCSI command block causing the reset
9618c2ecf20Sopenharmony_ci *
9628c2ecf20Sopenharmony_ci */
9638c2ecf20Sopenharmony_cistatic int aac_eh_target_reset(struct scsi_cmnd *cmd)
9648c2ecf20Sopenharmony_ci{
9658c2ecf20Sopenharmony_ci	struct scsi_device * dev = cmd->device;
9668c2ecf20Sopenharmony_ci	struct Scsi_Host * host = dev->host;
9678c2ecf20Sopenharmony_ci	struct aac_dev * aac = (struct aac_dev *)host->hostdata;
9688c2ecf20Sopenharmony_ci	struct aac_hba_map_info *info;
9698c2ecf20Sopenharmony_ci	int count;
9708c2ecf20Sopenharmony_ci	u32 bus, cid;
9718c2ecf20Sopenharmony_ci	int ret = FAILED;
9728c2ecf20Sopenharmony_ci	struct fib *fib;
9738c2ecf20Sopenharmony_ci	int status;
9748c2ecf20Sopenharmony_ci	u8 command;
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	bus = aac_logical_to_phys(scmd_channel(cmd));
9778c2ecf20Sopenharmony_ci	cid = scmd_id(cmd);
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	if (bus >= AAC_MAX_BUSES || cid >= AAC_MAX_TARGETS)
9808c2ecf20Sopenharmony_ci		return FAILED;
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	info = &aac->hba_map[bus][cid];
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	if (!(info->devtype == AAC_DEVTYPE_NATIVE_RAW &&
9858c2ecf20Sopenharmony_ci	 !(info->reset_state > 0)))
9868c2ecf20Sopenharmony_ci		return FAILED;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	pr_err("%s: Host target reset request. SCSI hang ?\n",
9898c2ecf20Sopenharmony_ci	       AAC_DRIVERNAME);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	fib = aac_fib_alloc(aac);
9928c2ecf20Sopenharmony_ci	if (!fib)
9938c2ecf20Sopenharmony_ci		return ret;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ci	/* already tried, start a hard reset now */
9978c2ecf20Sopenharmony_ci	command = aac_eh_tmf_hard_reset_fib(info, fib);
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	info->reset_state = 2;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	status = aac_hba_send(command, fib,
10028c2ecf20Sopenharmony_ci			      (fib_callback) aac_tmf_callback,
10038c2ecf20Sopenharmony_ci			      (void *) info);
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	if (status != -EINPROGRESS) {
10068c2ecf20Sopenharmony_ci		info->reset_state = 0;
10078c2ecf20Sopenharmony_ci		aac_fib_complete(fib);
10088c2ecf20Sopenharmony_ci		aac_fib_free(fib);
10098c2ecf20Sopenharmony_ci		return ret;
10108c2ecf20Sopenharmony_ci	}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	/* Wait up to 15 seconds for completion */
10138c2ecf20Sopenharmony_ci	for (count = 0; count < 15; ++count) {
10148c2ecf20Sopenharmony_ci		if (info->reset_state <= 0) {
10158c2ecf20Sopenharmony_ci			ret = info->reset_state == 0 ? SUCCESS : FAILED;
10168c2ecf20Sopenharmony_ci			break;
10178c2ecf20Sopenharmony_ci		}
10188c2ecf20Sopenharmony_ci		msleep(1000);
10198c2ecf20Sopenharmony_ci	}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	return ret;
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci/*
10258c2ecf20Sopenharmony_ci *	aac_eh_bus_reset	- Bus reset command handling
10268c2ecf20Sopenharmony_ci *	@scsi_cmd:	SCSI command block causing the reset
10278c2ecf20Sopenharmony_ci *
10288c2ecf20Sopenharmony_ci */
10298c2ecf20Sopenharmony_cistatic int aac_eh_bus_reset(struct scsi_cmnd* cmd)
10308c2ecf20Sopenharmony_ci{
10318c2ecf20Sopenharmony_ci	struct scsi_device * dev = cmd->device;
10328c2ecf20Sopenharmony_ci	struct Scsi_Host * host = dev->host;
10338c2ecf20Sopenharmony_ci	struct aac_dev * aac = (struct aac_dev *)host->hostdata;
10348c2ecf20Sopenharmony_ci	int count;
10358c2ecf20Sopenharmony_ci	u32 cmd_bus;
10368c2ecf20Sopenharmony_ci	int status = 0;
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	cmd_bus = aac_logical_to_phys(scmd_channel(cmd));
10408c2ecf20Sopenharmony_ci	/* Mark the assoc. FIB to not complete, eh handler does this */
10418c2ecf20Sopenharmony_ci	for (count = 0; count < (host->can_queue + AAC_NUM_MGT_FIB); ++count) {
10428c2ecf20Sopenharmony_ci		struct fib *fib = &aac->fibs[count];
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci		if (fib->hw_fib_va->header.XferState &&
10458c2ecf20Sopenharmony_ci		    (fib->flags & FIB_CONTEXT_FLAG) &&
10468c2ecf20Sopenharmony_ci		    (fib->flags & FIB_CONTEXT_FLAG_SCSI_CMD)) {
10478c2ecf20Sopenharmony_ci			struct aac_hba_map_info *info;
10488c2ecf20Sopenharmony_ci			u32 bus, cid;
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci			cmd = (struct scsi_cmnd *)fib->callback_data;
10518c2ecf20Sopenharmony_ci			bus = aac_logical_to_phys(scmd_channel(cmd));
10528c2ecf20Sopenharmony_ci			if (bus != cmd_bus)
10538c2ecf20Sopenharmony_ci				continue;
10548c2ecf20Sopenharmony_ci			cid = scmd_id(cmd);
10558c2ecf20Sopenharmony_ci			info = &aac->hba_map[bus][cid];
10568c2ecf20Sopenharmony_ci			if (bus >= AAC_MAX_BUSES || cid >= AAC_MAX_TARGETS ||
10578c2ecf20Sopenharmony_ci			    info->devtype != AAC_DEVTYPE_NATIVE_RAW) {
10588c2ecf20Sopenharmony_ci				fib->flags |= FIB_CONTEXT_FLAG_EH_RESET;
10598c2ecf20Sopenharmony_ci				cmd->SCp.phase = AAC_OWNER_ERROR_HANDLER;
10608c2ecf20Sopenharmony_ci			}
10618c2ecf20Sopenharmony_ci		}
10628c2ecf20Sopenharmony_ci	}
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	pr_err("%s: Host bus reset request. SCSI hang ?\n", AAC_DRIVERNAME);
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	/*
10678c2ecf20Sopenharmony_ci	 * Check the health of the controller
10688c2ecf20Sopenharmony_ci	 */
10698c2ecf20Sopenharmony_ci	status = aac_adapter_check_health(aac);
10708c2ecf20Sopenharmony_ci	if (status)
10718c2ecf20Sopenharmony_ci		dev_err(&aac->pdev->dev, "Adapter health - %d\n", status);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	count = get_num_of_incomplete_fibs(aac);
10748c2ecf20Sopenharmony_ci	return (count == 0) ? SUCCESS : FAILED;
10758c2ecf20Sopenharmony_ci}
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci/*
10788c2ecf20Sopenharmony_ci *	aac_eh_host_reset	- Host reset command handling
10798c2ecf20Sopenharmony_ci *	@scsi_cmd:	SCSI command block causing the reset
10808c2ecf20Sopenharmony_ci *
10818c2ecf20Sopenharmony_ci */
10828c2ecf20Sopenharmony_cistatic int aac_eh_host_reset(struct scsi_cmnd *cmd)
10838c2ecf20Sopenharmony_ci{
10848c2ecf20Sopenharmony_ci	struct scsi_device * dev = cmd->device;
10858c2ecf20Sopenharmony_ci	struct Scsi_Host * host = dev->host;
10868c2ecf20Sopenharmony_ci	struct aac_dev * aac = (struct aac_dev *)host->hostdata;
10878c2ecf20Sopenharmony_ci	int ret = FAILED;
10888c2ecf20Sopenharmony_ci	__le32 supported_options2 = 0;
10898c2ecf20Sopenharmony_ci	bool is_mu_reset;
10908c2ecf20Sopenharmony_ci	bool is_ignore_reset;
10918c2ecf20Sopenharmony_ci	bool is_doorbell_reset;
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	/*
10948c2ecf20Sopenharmony_ci	 * Check if reset is supported by the firmware
10958c2ecf20Sopenharmony_ci	 */
10968c2ecf20Sopenharmony_ci	supported_options2 = aac->supplement_adapter_info.supported_options2;
10978c2ecf20Sopenharmony_ci	is_mu_reset = supported_options2 & AAC_OPTION_MU_RESET;
10988c2ecf20Sopenharmony_ci	is_doorbell_reset = supported_options2 & AAC_OPTION_DOORBELL_RESET;
10998c2ecf20Sopenharmony_ci	is_ignore_reset = supported_options2 & AAC_OPTION_IGNORE_RESET;
11008c2ecf20Sopenharmony_ci	/*
11018c2ecf20Sopenharmony_ci	 * This adapter needs a blind reset, only do so for
11028c2ecf20Sopenharmony_ci	 * Adapters that support a register, instead of a commanded,
11038c2ecf20Sopenharmony_ci	 * reset.
11048c2ecf20Sopenharmony_ci	 */
11058c2ecf20Sopenharmony_ci	if ((is_mu_reset || is_doorbell_reset)
11068c2ecf20Sopenharmony_ci	 && aac_check_reset
11078c2ecf20Sopenharmony_ci	 && (aac_check_reset != -1 || !is_ignore_reset)) {
11088c2ecf20Sopenharmony_ci		/* Bypass wait for command quiesce */
11098c2ecf20Sopenharmony_ci		if (aac_reset_adapter(aac, 2, IOP_HWSOFT_RESET) == 0)
11108c2ecf20Sopenharmony_ci			ret = SUCCESS;
11118c2ecf20Sopenharmony_ci	}
11128c2ecf20Sopenharmony_ci	/*
11138c2ecf20Sopenharmony_ci	 * Reset EH state
11148c2ecf20Sopenharmony_ci	 */
11158c2ecf20Sopenharmony_ci	if (ret == SUCCESS) {
11168c2ecf20Sopenharmony_ci		int bus, cid;
11178c2ecf20Sopenharmony_ci		struct aac_hba_map_info *info;
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci		for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
11208c2ecf20Sopenharmony_ci			for (cid = 0; cid < AAC_MAX_TARGETS; cid++) {
11218c2ecf20Sopenharmony_ci				info = &aac->hba_map[bus][cid];
11228c2ecf20Sopenharmony_ci				if (info->devtype == AAC_DEVTYPE_NATIVE_RAW)
11238c2ecf20Sopenharmony_ci					info->reset_state = 0;
11248c2ecf20Sopenharmony_ci			}
11258c2ecf20Sopenharmony_ci		}
11268c2ecf20Sopenharmony_ci	}
11278c2ecf20Sopenharmony_ci	return ret;
11288c2ecf20Sopenharmony_ci}
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci/**
11318c2ecf20Sopenharmony_ci *	aac_cfg_open		-	open a configuration file
11328c2ecf20Sopenharmony_ci *	@inode: inode being opened
11338c2ecf20Sopenharmony_ci *	@file: file handle attached
11348c2ecf20Sopenharmony_ci *
11358c2ecf20Sopenharmony_ci *	Called when the configuration device is opened. Does the needed
11368c2ecf20Sopenharmony_ci *	set up on the handle and then returns
11378c2ecf20Sopenharmony_ci *
11388c2ecf20Sopenharmony_ci *	Bugs: This needs extending to check a given adapter is present
11398c2ecf20Sopenharmony_ci *	so we can support hot plugging, and to ref count adapters.
11408c2ecf20Sopenharmony_ci */
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_cistatic int aac_cfg_open(struct inode *inode, struct file *file)
11438c2ecf20Sopenharmony_ci{
11448c2ecf20Sopenharmony_ci	struct aac_dev *aac;
11458c2ecf20Sopenharmony_ci	unsigned minor_number = iminor(inode);
11468c2ecf20Sopenharmony_ci	int err = -ENODEV;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	mutex_lock(&aac_mutex);  /* BKL pushdown: nothing else protects this list */
11498c2ecf20Sopenharmony_ci	list_for_each_entry(aac, &aac_devices, entry) {
11508c2ecf20Sopenharmony_ci		if (aac->id == minor_number) {
11518c2ecf20Sopenharmony_ci			file->private_data = aac;
11528c2ecf20Sopenharmony_ci			err = 0;
11538c2ecf20Sopenharmony_ci			break;
11548c2ecf20Sopenharmony_ci		}
11558c2ecf20Sopenharmony_ci	}
11568c2ecf20Sopenharmony_ci	mutex_unlock(&aac_mutex);
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	return err;
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci/**
11628c2ecf20Sopenharmony_ci *	aac_cfg_ioctl		-	AAC configuration request
11638c2ecf20Sopenharmony_ci *	@file: file handle
11648c2ecf20Sopenharmony_ci *	@cmd: ioctl command code
11658c2ecf20Sopenharmony_ci *	@arg: argument
11668c2ecf20Sopenharmony_ci *
11678c2ecf20Sopenharmony_ci *	Handles a configuration ioctl. Currently this involves wrapping it
11688c2ecf20Sopenharmony_ci *	up and feeding it into the nasty windowsalike glue layer.
11698c2ecf20Sopenharmony_ci *
11708c2ecf20Sopenharmony_ci *	Bugs: Needs locking against parallel ioctls lower down
11718c2ecf20Sopenharmony_ci *	Bugs: Needs to handle hot plugging
11728c2ecf20Sopenharmony_ci */
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_cistatic long aac_cfg_ioctl(struct file *file,
11758c2ecf20Sopenharmony_ci		unsigned int cmd, unsigned long arg)
11768c2ecf20Sopenharmony_ci{
11778c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)file->private_data;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_RAWIO))
11808c2ecf20Sopenharmony_ci		return -EPERM;
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	return aac_do_ioctl(aac, cmd, (void __user *)arg);
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_cistatic ssize_t aac_show_model(struct device *device,
11868c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
11878c2ecf20Sopenharmony_ci{
11888c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(device)->hostdata;
11898c2ecf20Sopenharmony_ci	int len;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	if (dev->supplement_adapter_info.adapter_type_text[0]) {
11928c2ecf20Sopenharmony_ci		char *cp = dev->supplement_adapter_info.adapter_type_text;
11938c2ecf20Sopenharmony_ci		while (*cp && *cp != ' ')
11948c2ecf20Sopenharmony_ci			++cp;
11958c2ecf20Sopenharmony_ci		while (*cp == ' ')
11968c2ecf20Sopenharmony_ci			++cp;
11978c2ecf20Sopenharmony_ci		len = snprintf(buf, PAGE_SIZE, "%s\n", cp);
11988c2ecf20Sopenharmony_ci	} else
11998c2ecf20Sopenharmony_ci		len = snprintf(buf, PAGE_SIZE, "%s\n",
12008c2ecf20Sopenharmony_ci		  aac_drivers[dev->cardtype].model);
12018c2ecf20Sopenharmony_ci	return len;
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cistatic ssize_t aac_show_vendor(struct device *device,
12058c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(device)->hostdata;
12088c2ecf20Sopenharmony_ci	struct aac_supplement_adapter_info *sup_adap_info;
12098c2ecf20Sopenharmony_ci	int len;
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	sup_adap_info = &dev->supplement_adapter_info;
12128c2ecf20Sopenharmony_ci	if (sup_adap_info->adapter_type_text[0]) {
12138c2ecf20Sopenharmony_ci		char *cp = sup_adap_info->adapter_type_text;
12148c2ecf20Sopenharmony_ci		while (*cp && *cp != ' ')
12158c2ecf20Sopenharmony_ci			++cp;
12168c2ecf20Sopenharmony_ci		len = snprintf(buf, PAGE_SIZE, "%.*s\n",
12178c2ecf20Sopenharmony_ci			(int)(cp - (char *)sup_adap_info->adapter_type_text),
12188c2ecf20Sopenharmony_ci					sup_adap_info->adapter_type_text);
12198c2ecf20Sopenharmony_ci	} else
12208c2ecf20Sopenharmony_ci		len = snprintf(buf, PAGE_SIZE, "%s\n",
12218c2ecf20Sopenharmony_ci			aac_drivers[dev->cardtype].vname);
12228c2ecf20Sopenharmony_ci	return len;
12238c2ecf20Sopenharmony_ci}
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_cistatic ssize_t aac_show_flags(struct device *cdev,
12268c2ecf20Sopenharmony_ci			      struct device_attribute *attr, char *buf)
12278c2ecf20Sopenharmony_ci{
12288c2ecf20Sopenharmony_ci	int len = 0;
12298c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(cdev)->hostdata;
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	if (nblank(dprintk(x)))
12328c2ecf20Sopenharmony_ci		len = snprintf(buf, PAGE_SIZE, "dprintk\n");
12338c2ecf20Sopenharmony_ci#ifdef AAC_DETAILED_STATUS_INFO
12348c2ecf20Sopenharmony_ci	len += scnprintf(buf + len, PAGE_SIZE - len,
12358c2ecf20Sopenharmony_ci			 "AAC_DETAILED_STATUS_INFO\n");
12368c2ecf20Sopenharmony_ci#endif
12378c2ecf20Sopenharmony_ci	if (dev->raw_io_interface && dev->raw_io_64)
12388c2ecf20Sopenharmony_ci		len += scnprintf(buf + len, PAGE_SIZE - len,
12398c2ecf20Sopenharmony_ci				 "SAI_READ_CAPACITY_16\n");
12408c2ecf20Sopenharmony_ci	if (dev->jbod)
12418c2ecf20Sopenharmony_ci		len += scnprintf(buf + len, PAGE_SIZE - len,
12428c2ecf20Sopenharmony_ci				 "SUPPORTED_JBOD\n");
12438c2ecf20Sopenharmony_ci	if (dev->supplement_adapter_info.supported_options2 &
12448c2ecf20Sopenharmony_ci		AAC_OPTION_POWER_MANAGEMENT)
12458c2ecf20Sopenharmony_ci		len += scnprintf(buf + len, PAGE_SIZE - len,
12468c2ecf20Sopenharmony_ci				 "SUPPORTED_POWER_MANAGEMENT\n");
12478c2ecf20Sopenharmony_ci	if (dev->msi)
12488c2ecf20Sopenharmony_ci		len += scnprintf(buf + len, PAGE_SIZE - len, "PCI_HAS_MSI\n");
12498c2ecf20Sopenharmony_ci	return len;
12508c2ecf20Sopenharmony_ci}
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_cistatic ssize_t aac_show_kernel_version(struct device *device,
12538c2ecf20Sopenharmony_ci				       struct device_attribute *attr,
12548c2ecf20Sopenharmony_ci				       char *buf)
12558c2ecf20Sopenharmony_ci{
12568c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(device)->hostdata;
12578c2ecf20Sopenharmony_ci	int len, tmp;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	tmp = le32_to_cpu(dev->adapter_info.kernelrev);
12608c2ecf20Sopenharmony_ci	len = snprintf(buf, PAGE_SIZE, "%d.%d-%d[%d]\n",
12618c2ecf20Sopenharmony_ci	  tmp >> 24, (tmp >> 16) & 0xff, tmp & 0xff,
12628c2ecf20Sopenharmony_ci	  le32_to_cpu(dev->adapter_info.kernelbuild));
12638c2ecf20Sopenharmony_ci	return len;
12648c2ecf20Sopenharmony_ci}
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_cistatic ssize_t aac_show_monitor_version(struct device *device,
12678c2ecf20Sopenharmony_ci					struct device_attribute *attr,
12688c2ecf20Sopenharmony_ci					char *buf)
12698c2ecf20Sopenharmony_ci{
12708c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(device)->hostdata;
12718c2ecf20Sopenharmony_ci	int len, tmp;
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	tmp = le32_to_cpu(dev->adapter_info.monitorrev);
12748c2ecf20Sopenharmony_ci	len = snprintf(buf, PAGE_SIZE, "%d.%d-%d[%d]\n",
12758c2ecf20Sopenharmony_ci	  tmp >> 24, (tmp >> 16) & 0xff, tmp & 0xff,
12768c2ecf20Sopenharmony_ci	  le32_to_cpu(dev->adapter_info.monitorbuild));
12778c2ecf20Sopenharmony_ci	return len;
12788c2ecf20Sopenharmony_ci}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_cistatic ssize_t aac_show_bios_version(struct device *device,
12818c2ecf20Sopenharmony_ci				     struct device_attribute *attr,
12828c2ecf20Sopenharmony_ci				     char *buf)
12838c2ecf20Sopenharmony_ci{
12848c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(device)->hostdata;
12858c2ecf20Sopenharmony_ci	int len, tmp;
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_ci	tmp = le32_to_cpu(dev->adapter_info.biosrev);
12888c2ecf20Sopenharmony_ci	len = snprintf(buf, PAGE_SIZE, "%d.%d-%d[%d]\n",
12898c2ecf20Sopenharmony_ci	  tmp >> 24, (tmp >> 16) & 0xff, tmp & 0xff,
12908c2ecf20Sopenharmony_ci	  le32_to_cpu(dev->adapter_info.biosbuild));
12918c2ecf20Sopenharmony_ci	return len;
12928c2ecf20Sopenharmony_ci}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_cistatic ssize_t aac_show_driver_version(struct device *device,
12958c2ecf20Sopenharmony_ci					struct device_attribute *attr,
12968c2ecf20Sopenharmony_ci					char *buf)
12978c2ecf20Sopenharmony_ci{
12988c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%s\n", aac_driver_version);
12998c2ecf20Sopenharmony_ci}
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_cistatic ssize_t aac_show_serial_number(struct device *device,
13028c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
13038c2ecf20Sopenharmony_ci{
13048c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(device)->hostdata;
13058c2ecf20Sopenharmony_ci	int len = 0;
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
13088c2ecf20Sopenharmony_ci		len = snprintf(buf, 16, "%06X\n",
13098c2ecf20Sopenharmony_ci		  le32_to_cpu(dev->adapter_info.serial[0]));
13108c2ecf20Sopenharmony_ci	if (len &&
13118c2ecf20Sopenharmony_ci	  !memcmp(&dev->supplement_adapter_info.mfg_pcba_serial_no[
13128c2ecf20Sopenharmony_ci	    sizeof(dev->supplement_adapter_info.mfg_pcba_serial_no)-len],
13138c2ecf20Sopenharmony_ci	  buf, len-1))
13148c2ecf20Sopenharmony_ci		len = snprintf(buf, 16, "%.*s\n",
13158c2ecf20Sopenharmony_ci		  (int)sizeof(dev->supplement_adapter_info.mfg_pcba_serial_no),
13168c2ecf20Sopenharmony_ci		  dev->supplement_adapter_info.mfg_pcba_serial_no);
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	return min(len, 16);
13198c2ecf20Sopenharmony_ci}
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_cistatic ssize_t aac_show_max_channel(struct device *device,
13228c2ecf20Sopenharmony_ci				    struct device_attribute *attr, char *buf)
13238c2ecf20Sopenharmony_ci{
13248c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%d\n",
13258c2ecf20Sopenharmony_ci	  class_to_shost(device)->max_channel);
13268c2ecf20Sopenharmony_ci}
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_cistatic ssize_t aac_show_max_id(struct device *device,
13298c2ecf20Sopenharmony_ci			       struct device_attribute *attr, char *buf)
13308c2ecf20Sopenharmony_ci{
13318c2ecf20Sopenharmony_ci	return snprintf(buf, PAGE_SIZE, "%d\n",
13328c2ecf20Sopenharmony_ci	  class_to_shost(device)->max_id);
13338c2ecf20Sopenharmony_ci}
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_cistatic ssize_t aac_store_reset_adapter(struct device *device,
13368c2ecf20Sopenharmony_ci				       struct device_attribute *attr,
13378c2ecf20Sopenharmony_ci				       const char *buf, size_t count)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	int retval = -EACCES;
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	if (!capable(CAP_SYS_ADMIN))
13428c2ecf20Sopenharmony_ci		return retval;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	retval = aac_reset_adapter(shost_priv(class_to_shost(device)),
13458c2ecf20Sopenharmony_ci					buf[0] == '!', IOP_HWSOFT_RESET);
13468c2ecf20Sopenharmony_ci	if (retval >= 0)
13478c2ecf20Sopenharmony_ci		retval = count;
13488c2ecf20Sopenharmony_ci
13498c2ecf20Sopenharmony_ci	return retval;
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_cistatic ssize_t aac_show_reset_adapter(struct device *device,
13538c2ecf20Sopenharmony_ci				      struct device_attribute *attr,
13548c2ecf20Sopenharmony_ci				      char *buf)
13558c2ecf20Sopenharmony_ci{
13568c2ecf20Sopenharmony_ci	struct aac_dev *dev = (struct aac_dev*)class_to_shost(device)->hostdata;
13578c2ecf20Sopenharmony_ci	int len, tmp;
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	tmp = aac_adapter_check_health(dev);
13608c2ecf20Sopenharmony_ci	if ((tmp == 0) && dev->in_reset)
13618c2ecf20Sopenharmony_ci		tmp = -EBUSY;
13628c2ecf20Sopenharmony_ci	len = snprintf(buf, PAGE_SIZE, "0x%x\n", tmp);
13638c2ecf20Sopenharmony_ci	return len;
13648c2ecf20Sopenharmony_ci}
13658c2ecf20Sopenharmony_ci
13668c2ecf20Sopenharmony_cistatic struct device_attribute aac_model = {
13678c2ecf20Sopenharmony_ci	.attr = {
13688c2ecf20Sopenharmony_ci		.name = "model",
13698c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
13708c2ecf20Sopenharmony_ci	},
13718c2ecf20Sopenharmony_ci	.show = aac_show_model,
13728c2ecf20Sopenharmony_ci};
13738c2ecf20Sopenharmony_cistatic struct device_attribute aac_vendor = {
13748c2ecf20Sopenharmony_ci	.attr = {
13758c2ecf20Sopenharmony_ci		.name = "vendor",
13768c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
13778c2ecf20Sopenharmony_ci	},
13788c2ecf20Sopenharmony_ci	.show = aac_show_vendor,
13798c2ecf20Sopenharmony_ci};
13808c2ecf20Sopenharmony_cistatic struct device_attribute aac_flags = {
13818c2ecf20Sopenharmony_ci	.attr = {
13828c2ecf20Sopenharmony_ci		.name = "flags",
13838c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
13848c2ecf20Sopenharmony_ci	},
13858c2ecf20Sopenharmony_ci	.show = aac_show_flags,
13868c2ecf20Sopenharmony_ci};
13878c2ecf20Sopenharmony_cistatic struct device_attribute aac_kernel_version = {
13888c2ecf20Sopenharmony_ci	.attr = {
13898c2ecf20Sopenharmony_ci		.name = "hba_kernel_version",
13908c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
13918c2ecf20Sopenharmony_ci	},
13928c2ecf20Sopenharmony_ci	.show = aac_show_kernel_version,
13938c2ecf20Sopenharmony_ci};
13948c2ecf20Sopenharmony_cistatic struct device_attribute aac_monitor_version = {
13958c2ecf20Sopenharmony_ci	.attr = {
13968c2ecf20Sopenharmony_ci		.name = "hba_monitor_version",
13978c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
13988c2ecf20Sopenharmony_ci	},
13998c2ecf20Sopenharmony_ci	.show = aac_show_monitor_version,
14008c2ecf20Sopenharmony_ci};
14018c2ecf20Sopenharmony_cistatic struct device_attribute aac_bios_version = {
14028c2ecf20Sopenharmony_ci	.attr = {
14038c2ecf20Sopenharmony_ci		.name = "hba_bios_version",
14048c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
14058c2ecf20Sopenharmony_ci	},
14068c2ecf20Sopenharmony_ci	.show = aac_show_bios_version,
14078c2ecf20Sopenharmony_ci};
14088c2ecf20Sopenharmony_cistatic struct device_attribute aac_lld_version = {
14098c2ecf20Sopenharmony_ci	.attr = {
14108c2ecf20Sopenharmony_ci		.name = "driver_version",
14118c2ecf20Sopenharmony_ci		.mode = 0444,
14128c2ecf20Sopenharmony_ci	},
14138c2ecf20Sopenharmony_ci	.show = aac_show_driver_version,
14148c2ecf20Sopenharmony_ci};
14158c2ecf20Sopenharmony_cistatic struct device_attribute aac_serial_number = {
14168c2ecf20Sopenharmony_ci	.attr = {
14178c2ecf20Sopenharmony_ci		.name = "serial_number",
14188c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
14198c2ecf20Sopenharmony_ci	},
14208c2ecf20Sopenharmony_ci	.show = aac_show_serial_number,
14218c2ecf20Sopenharmony_ci};
14228c2ecf20Sopenharmony_cistatic struct device_attribute aac_max_channel = {
14238c2ecf20Sopenharmony_ci	.attr = {
14248c2ecf20Sopenharmony_ci		.name = "max_channel",
14258c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
14268c2ecf20Sopenharmony_ci	},
14278c2ecf20Sopenharmony_ci	.show = aac_show_max_channel,
14288c2ecf20Sopenharmony_ci};
14298c2ecf20Sopenharmony_cistatic struct device_attribute aac_max_id = {
14308c2ecf20Sopenharmony_ci	.attr = {
14318c2ecf20Sopenharmony_ci		.name = "max_id",
14328c2ecf20Sopenharmony_ci		.mode = S_IRUGO,
14338c2ecf20Sopenharmony_ci	},
14348c2ecf20Sopenharmony_ci	.show = aac_show_max_id,
14358c2ecf20Sopenharmony_ci};
14368c2ecf20Sopenharmony_cistatic struct device_attribute aac_reset = {
14378c2ecf20Sopenharmony_ci	.attr = {
14388c2ecf20Sopenharmony_ci		.name = "reset_host",
14398c2ecf20Sopenharmony_ci		.mode = S_IWUSR|S_IRUGO,
14408c2ecf20Sopenharmony_ci	},
14418c2ecf20Sopenharmony_ci	.store = aac_store_reset_adapter,
14428c2ecf20Sopenharmony_ci	.show = aac_show_reset_adapter,
14438c2ecf20Sopenharmony_ci};
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_cistatic struct device_attribute *aac_attrs[] = {
14468c2ecf20Sopenharmony_ci	&aac_model,
14478c2ecf20Sopenharmony_ci	&aac_vendor,
14488c2ecf20Sopenharmony_ci	&aac_flags,
14498c2ecf20Sopenharmony_ci	&aac_kernel_version,
14508c2ecf20Sopenharmony_ci	&aac_monitor_version,
14518c2ecf20Sopenharmony_ci	&aac_bios_version,
14528c2ecf20Sopenharmony_ci	&aac_lld_version,
14538c2ecf20Sopenharmony_ci	&aac_serial_number,
14548c2ecf20Sopenharmony_ci	&aac_max_channel,
14558c2ecf20Sopenharmony_ci	&aac_max_id,
14568c2ecf20Sopenharmony_ci	&aac_reset,
14578c2ecf20Sopenharmony_ci	NULL
14588c2ecf20Sopenharmony_ci};
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_cissize_t aac_get_serial_number(struct device *device, char *buf)
14618c2ecf20Sopenharmony_ci{
14628c2ecf20Sopenharmony_ci	return aac_show_serial_number(device, &aac_serial_number, buf);
14638c2ecf20Sopenharmony_ci}
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_cistatic const struct file_operations aac_cfg_fops = {
14668c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
14678c2ecf20Sopenharmony_ci	.unlocked_ioctl	= aac_cfg_ioctl,
14688c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
14698c2ecf20Sopenharmony_ci	.compat_ioctl   = aac_cfg_ioctl,
14708c2ecf20Sopenharmony_ci#endif
14718c2ecf20Sopenharmony_ci	.open		= aac_cfg_open,
14728c2ecf20Sopenharmony_ci	.llseek		= noop_llseek,
14738c2ecf20Sopenharmony_ci};
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_cistatic struct scsi_host_template aac_driver_template = {
14768c2ecf20Sopenharmony_ci	.module				= THIS_MODULE,
14778c2ecf20Sopenharmony_ci	.name				= "AAC",
14788c2ecf20Sopenharmony_ci	.proc_name			= AAC_DRIVERNAME,
14798c2ecf20Sopenharmony_ci	.info				= aac_info,
14808c2ecf20Sopenharmony_ci	.ioctl				= aac_ioctl,
14818c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT
14828c2ecf20Sopenharmony_ci	.compat_ioctl			= aac_ioctl,
14838c2ecf20Sopenharmony_ci#endif
14848c2ecf20Sopenharmony_ci	.queuecommand			= aac_queuecommand,
14858c2ecf20Sopenharmony_ci	.bios_param			= aac_biosparm,
14868c2ecf20Sopenharmony_ci	.shost_attrs			= aac_attrs,
14878c2ecf20Sopenharmony_ci	.slave_configure		= aac_slave_configure,
14888c2ecf20Sopenharmony_ci	.change_queue_depth		= aac_change_queue_depth,
14898c2ecf20Sopenharmony_ci	.sdev_attrs			= aac_dev_attrs,
14908c2ecf20Sopenharmony_ci	.eh_abort_handler		= aac_eh_abort,
14918c2ecf20Sopenharmony_ci	.eh_device_reset_handler	= aac_eh_dev_reset,
14928c2ecf20Sopenharmony_ci	.eh_target_reset_handler	= aac_eh_target_reset,
14938c2ecf20Sopenharmony_ci	.eh_bus_reset_handler		= aac_eh_bus_reset,
14948c2ecf20Sopenharmony_ci	.eh_host_reset_handler		= aac_eh_host_reset,
14958c2ecf20Sopenharmony_ci	.can_queue			= AAC_NUM_IO_FIB,
14968c2ecf20Sopenharmony_ci	.this_id			= MAXIMUM_NUM_CONTAINERS,
14978c2ecf20Sopenharmony_ci	.sg_tablesize			= 16,
14988c2ecf20Sopenharmony_ci	.max_sectors			= 128,
14998c2ecf20Sopenharmony_ci#if (AAC_NUM_IO_FIB > 256)
15008c2ecf20Sopenharmony_ci	.cmd_per_lun			= 256,
15018c2ecf20Sopenharmony_ci#else
15028c2ecf20Sopenharmony_ci	.cmd_per_lun			= AAC_NUM_IO_FIB,
15038c2ecf20Sopenharmony_ci#endif
15048c2ecf20Sopenharmony_ci	.emulated			= 1,
15058c2ecf20Sopenharmony_ci	.no_write_same			= 1,
15068c2ecf20Sopenharmony_ci};
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_cistatic void __aac_shutdown(struct aac_dev * aac)
15098c2ecf20Sopenharmony_ci{
15108c2ecf20Sopenharmony_ci	int i;
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	mutex_lock(&aac->ioctl_mutex);
15138c2ecf20Sopenharmony_ci	aac->adapter_shutdown = 1;
15148c2ecf20Sopenharmony_ci	mutex_unlock(&aac->ioctl_mutex);
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci	if (aac->aif_thread) {
15178c2ecf20Sopenharmony_ci		int i;
15188c2ecf20Sopenharmony_ci		/* Clear out events first */
15198c2ecf20Sopenharmony_ci		for (i = 0; i < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); i++) {
15208c2ecf20Sopenharmony_ci			struct fib *fib = &aac->fibs[i];
15218c2ecf20Sopenharmony_ci			if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
15228c2ecf20Sopenharmony_ci			    (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected)))
15238c2ecf20Sopenharmony_ci				complete(&fib->event_wait);
15248c2ecf20Sopenharmony_ci		}
15258c2ecf20Sopenharmony_ci		kthread_stop(aac->thread);
15268c2ecf20Sopenharmony_ci		aac->thread = NULL;
15278c2ecf20Sopenharmony_ci	}
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	aac_send_shutdown(aac);
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci	aac_adapter_disable_int(aac);
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	if (aac_is_src(aac)) {
15348c2ecf20Sopenharmony_ci		if (aac->max_msix > 1) {
15358c2ecf20Sopenharmony_ci			for (i = 0; i < aac->max_msix; i++) {
15368c2ecf20Sopenharmony_ci				free_irq(pci_irq_vector(aac->pdev, i),
15378c2ecf20Sopenharmony_ci					 &(aac->aac_msix[i]));
15388c2ecf20Sopenharmony_ci			}
15398c2ecf20Sopenharmony_ci		} else {
15408c2ecf20Sopenharmony_ci			free_irq(aac->pdev->irq,
15418c2ecf20Sopenharmony_ci				 &(aac->aac_msix[0]));
15428c2ecf20Sopenharmony_ci		}
15438c2ecf20Sopenharmony_ci	} else {
15448c2ecf20Sopenharmony_ci		free_irq(aac->pdev->irq, aac);
15458c2ecf20Sopenharmony_ci	}
15468c2ecf20Sopenharmony_ci	if (aac->msi)
15478c2ecf20Sopenharmony_ci		pci_disable_msi(aac->pdev);
15488c2ecf20Sopenharmony_ci	else if (aac->max_msix > 1)
15498c2ecf20Sopenharmony_ci		pci_disable_msix(aac->pdev);
15508c2ecf20Sopenharmony_ci}
15518c2ecf20Sopenharmony_cistatic void aac_init_char(void)
15528c2ecf20Sopenharmony_ci{
15538c2ecf20Sopenharmony_ci	aac_cfg_major = register_chrdev(0, "aac", &aac_cfg_fops);
15548c2ecf20Sopenharmony_ci	if (aac_cfg_major < 0) {
15558c2ecf20Sopenharmony_ci		pr_err("aacraid: unable to register \"aac\" device.\n");
15568c2ecf20Sopenharmony_ci	}
15578c2ecf20Sopenharmony_ci}
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_civoid aac_reinit_aif(struct aac_dev *aac, unsigned int index)
15608c2ecf20Sopenharmony_ci{
15618c2ecf20Sopenharmony_ci	/*
15628c2ecf20Sopenharmony_ci	 * Firmware may send a AIF messages very early and the Driver may have
15638c2ecf20Sopenharmony_ci	 * ignored as it is not fully ready to process the messages. Send
15648c2ecf20Sopenharmony_ci	 * AIF to firmware so that if there are any unprocessed events they
15658c2ecf20Sopenharmony_ci	 * can be processed now.
15668c2ecf20Sopenharmony_ci	 */
15678c2ecf20Sopenharmony_ci	if (aac_drivers[index].quirks & AAC_QUIRK_SRC)
15688c2ecf20Sopenharmony_ci		aac_intr_normal(aac, 0, 2, 0, NULL);
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_ci}
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_cistatic int aac_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
15738c2ecf20Sopenharmony_ci{
15748c2ecf20Sopenharmony_ci	unsigned index = id->driver_data;
15758c2ecf20Sopenharmony_ci	struct Scsi_Host *shost;
15768c2ecf20Sopenharmony_ci	struct aac_dev *aac;
15778c2ecf20Sopenharmony_ci	struct list_head *insert = &aac_devices;
15788c2ecf20Sopenharmony_ci	int error;
15798c2ecf20Sopenharmony_ci	int unique_id = 0;
15808c2ecf20Sopenharmony_ci	u64 dmamask;
15818c2ecf20Sopenharmony_ci	int mask_bits = 0;
15828c2ecf20Sopenharmony_ci	extern int aac_sync_mode;
15838c2ecf20Sopenharmony_ci
15848c2ecf20Sopenharmony_ci	/*
15858c2ecf20Sopenharmony_ci	 * Only series 7 needs freset.
15868c2ecf20Sopenharmony_ci	 */
15878c2ecf20Sopenharmony_ci	if (pdev->device == PMC_DEVICE_S7)
15888c2ecf20Sopenharmony_ci		pdev->needs_freset = 1;
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci	list_for_each_entry(aac, &aac_devices, entry) {
15918c2ecf20Sopenharmony_ci		if (aac->id > unique_id)
15928c2ecf20Sopenharmony_ci			break;
15938c2ecf20Sopenharmony_ci		insert = &aac->entry;
15948c2ecf20Sopenharmony_ci		unique_id++;
15958c2ecf20Sopenharmony_ci	}
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci	pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
15988c2ecf20Sopenharmony_ci			       PCIE_LINK_STATE_CLKPM);
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci	error = pci_enable_device(pdev);
16018c2ecf20Sopenharmony_ci	if (error)
16028c2ecf20Sopenharmony_ci		goto out;
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci	if (!(aac_drivers[index].quirks & AAC_QUIRK_SRC)) {
16058c2ecf20Sopenharmony_ci		error = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
16068c2ecf20Sopenharmony_ci		if (error) {
16078c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "PCI 32 BIT dma mask set failed");
16088c2ecf20Sopenharmony_ci			goto out_disable_pdev;
16098c2ecf20Sopenharmony_ci		}
16108c2ecf20Sopenharmony_ci	}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	/*
16138c2ecf20Sopenharmony_ci	 * If the quirk31 bit is set, the adapter needs adapter
16148c2ecf20Sopenharmony_ci	 * to driver communication memory to be allocated below 2gig
16158c2ecf20Sopenharmony_ci	 */
16168c2ecf20Sopenharmony_ci	if (aac_drivers[index].quirks & AAC_QUIRK_31BIT) {
16178c2ecf20Sopenharmony_ci		dmamask = DMA_BIT_MASK(31);
16188c2ecf20Sopenharmony_ci		mask_bits = 31;
16198c2ecf20Sopenharmony_ci	} else {
16208c2ecf20Sopenharmony_ci		dmamask = DMA_BIT_MASK(32);
16218c2ecf20Sopenharmony_ci		mask_bits = 32;
16228c2ecf20Sopenharmony_ci	}
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci	error = dma_set_coherent_mask(&pdev->dev, dmamask);
16258c2ecf20Sopenharmony_ci	if (error) {
16268c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "PCI %d B consistent dma mask set failed\n"
16278c2ecf20Sopenharmony_ci				, mask_bits);
16288c2ecf20Sopenharmony_ci		goto out_disable_pdev;
16298c2ecf20Sopenharmony_ci	}
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	pci_set_master(pdev);
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci	shost = scsi_host_alloc(&aac_driver_template, sizeof(struct aac_dev));
16348c2ecf20Sopenharmony_ci	if (!shost) {
16358c2ecf20Sopenharmony_ci		error = -ENOMEM;
16368c2ecf20Sopenharmony_ci		goto out_disable_pdev;
16378c2ecf20Sopenharmony_ci	}
16388c2ecf20Sopenharmony_ci
16398c2ecf20Sopenharmony_ci	shost->irq = pdev->irq;
16408c2ecf20Sopenharmony_ci	shost->unique_id = unique_id;
16418c2ecf20Sopenharmony_ci	shost->max_cmd_len = 16;
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci	if (aac_cfg_major == AAC_CHARDEV_NEEDS_REINIT)
16448c2ecf20Sopenharmony_ci		aac_init_char();
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ci	aac = (struct aac_dev *)shost->hostdata;
16478c2ecf20Sopenharmony_ci	aac->base_start = pci_resource_start(pdev, 0);
16488c2ecf20Sopenharmony_ci	aac->scsi_host_ptr = shost;
16498c2ecf20Sopenharmony_ci	aac->pdev = pdev;
16508c2ecf20Sopenharmony_ci	aac->name = aac_driver_template.name;
16518c2ecf20Sopenharmony_ci	aac->id = shost->unique_id;
16528c2ecf20Sopenharmony_ci	aac->cardtype = index;
16538c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&aac->entry);
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci	if (aac_reset_devices || reset_devices)
16568c2ecf20Sopenharmony_ci		aac->init_reset = true;
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_ci	aac->fibs = kcalloc(shost->can_queue + AAC_NUM_MGT_FIB,
16598c2ecf20Sopenharmony_ci			    sizeof(struct fib),
16608c2ecf20Sopenharmony_ci			    GFP_KERNEL);
16618c2ecf20Sopenharmony_ci	if (!aac->fibs) {
16628c2ecf20Sopenharmony_ci		error = -ENOMEM;
16638c2ecf20Sopenharmony_ci		goto out_free_host;
16648c2ecf20Sopenharmony_ci	}
16658c2ecf20Sopenharmony_ci
16668c2ecf20Sopenharmony_ci	spin_lock_init(&aac->fib_lock);
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_ci	mutex_init(&aac->ioctl_mutex);
16698c2ecf20Sopenharmony_ci	mutex_init(&aac->scan_mutex);
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&aac->safw_rescan_work, aac_safw_rescan_worker);
16728c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&aac->src_reinit_aif_worker,
16738c2ecf20Sopenharmony_ci				aac_src_reinit_aif_worker);
16748c2ecf20Sopenharmony_ci	/*
16758c2ecf20Sopenharmony_ci	 *	Map in the registers from the adapter.
16768c2ecf20Sopenharmony_ci	 */
16778c2ecf20Sopenharmony_ci	aac->base_size = AAC_MIN_FOOTPRINT_SIZE;
16788c2ecf20Sopenharmony_ci	if ((*aac_drivers[index].init)(aac)) {
16798c2ecf20Sopenharmony_ci		error = -ENODEV;
16808c2ecf20Sopenharmony_ci		goto out_unmap;
16818c2ecf20Sopenharmony_ci	}
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	if (aac->sync_mode) {
16848c2ecf20Sopenharmony_ci		if (aac_sync_mode)
16858c2ecf20Sopenharmony_ci			printk(KERN_INFO "%s%d: Sync. mode enforced "
16868c2ecf20Sopenharmony_ci				"by driver parameter. This will cause "
16878c2ecf20Sopenharmony_ci				"a significant performance decrease!\n",
16888c2ecf20Sopenharmony_ci				aac->name,
16898c2ecf20Sopenharmony_ci				aac->id);
16908c2ecf20Sopenharmony_ci		else
16918c2ecf20Sopenharmony_ci			printk(KERN_INFO "%s%d: Async. mode not supported "
16928c2ecf20Sopenharmony_ci				"by current driver, sync. mode enforced."
16938c2ecf20Sopenharmony_ci				"\nPlease update driver to get full performance.\n",
16948c2ecf20Sopenharmony_ci				aac->name,
16958c2ecf20Sopenharmony_ci				aac->id);
16968c2ecf20Sopenharmony_ci	}
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci	/*
16998c2ecf20Sopenharmony_ci	 *	Start any kernel threads needed
17008c2ecf20Sopenharmony_ci	 */
17018c2ecf20Sopenharmony_ci	aac->thread = kthread_run(aac_command_thread, aac, AAC_DRIVERNAME);
17028c2ecf20Sopenharmony_ci	if (IS_ERR(aac->thread)) {
17038c2ecf20Sopenharmony_ci		printk(KERN_ERR "aacraid: Unable to create command thread.\n");
17048c2ecf20Sopenharmony_ci		error = PTR_ERR(aac->thread);
17058c2ecf20Sopenharmony_ci		aac->thread = NULL;
17068c2ecf20Sopenharmony_ci		goto out_deinit;
17078c2ecf20Sopenharmony_ci	}
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	aac->maximum_num_channels = aac_drivers[index].channels;
17108c2ecf20Sopenharmony_ci	error = aac_get_adapter_info(aac);
17118c2ecf20Sopenharmony_ci	if (error < 0)
17128c2ecf20Sopenharmony_ci		goto out_deinit;
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci	/*
17158c2ecf20Sopenharmony_ci	 * Lets override negotiations and drop the maximum SG limit to 34
17168c2ecf20Sopenharmony_ci	 */
17178c2ecf20Sopenharmony_ci	if ((aac_drivers[index].quirks & AAC_QUIRK_34SG) &&
17188c2ecf20Sopenharmony_ci			(shost->sg_tablesize > 34)) {
17198c2ecf20Sopenharmony_ci		shost->sg_tablesize = 34;
17208c2ecf20Sopenharmony_ci		shost->max_sectors = (shost->sg_tablesize * 8) + 112;
17218c2ecf20Sopenharmony_ci	}
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	if ((aac_drivers[index].quirks & AAC_QUIRK_17SG) &&
17248c2ecf20Sopenharmony_ci			(shost->sg_tablesize > 17)) {
17258c2ecf20Sopenharmony_ci		shost->sg_tablesize = 17;
17268c2ecf20Sopenharmony_ci		shost->max_sectors = (shost->sg_tablesize * 8) + 112;
17278c2ecf20Sopenharmony_ci	}
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci	if (aac->adapter_info.options & AAC_OPT_NEW_COMM)
17308c2ecf20Sopenharmony_ci		shost->max_segment_size = shost->max_sectors << 9;
17318c2ecf20Sopenharmony_ci	else
17328c2ecf20Sopenharmony_ci		shost->max_segment_size = 65536;
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci	/*
17358c2ecf20Sopenharmony_ci	 * Firmware printf works only with older firmware.
17368c2ecf20Sopenharmony_ci	 */
17378c2ecf20Sopenharmony_ci	if (aac_drivers[index].quirks & AAC_QUIRK_34SG)
17388c2ecf20Sopenharmony_ci		aac->printf_enabled = 1;
17398c2ecf20Sopenharmony_ci	else
17408c2ecf20Sopenharmony_ci		aac->printf_enabled = 0;
17418c2ecf20Sopenharmony_ci
17428c2ecf20Sopenharmony_ci	/*
17438c2ecf20Sopenharmony_ci	 * max channel will be the physical channels plus 1 virtual channel
17448c2ecf20Sopenharmony_ci	 * all containers are on the virtual channel 0 (CONTAINER_CHANNEL)
17458c2ecf20Sopenharmony_ci	 * physical channels are address by their actual physical number+1
17468c2ecf20Sopenharmony_ci	 */
17478c2ecf20Sopenharmony_ci	if (aac->nondasd_support || expose_physicals || aac->jbod)
17488c2ecf20Sopenharmony_ci		shost->max_channel = aac->maximum_num_channels;
17498c2ecf20Sopenharmony_ci	else
17508c2ecf20Sopenharmony_ci		shost->max_channel = 0;
17518c2ecf20Sopenharmony_ci
17528c2ecf20Sopenharmony_ci	aac_get_config_status(aac, 0);
17538c2ecf20Sopenharmony_ci	aac_get_containers(aac);
17548c2ecf20Sopenharmony_ci	list_add(&aac->entry, insert);
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci	shost->max_id = aac->maximum_num_containers;
17578c2ecf20Sopenharmony_ci	if (shost->max_id < aac->maximum_num_physicals)
17588c2ecf20Sopenharmony_ci		shost->max_id = aac->maximum_num_physicals;
17598c2ecf20Sopenharmony_ci	if (shost->max_id < MAXIMUM_NUM_CONTAINERS)
17608c2ecf20Sopenharmony_ci		shost->max_id = MAXIMUM_NUM_CONTAINERS;
17618c2ecf20Sopenharmony_ci	else
17628c2ecf20Sopenharmony_ci		shost->this_id = shost->max_id;
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci	if (!aac->sa_firmware && aac_drivers[index].quirks & AAC_QUIRK_SRC)
17658c2ecf20Sopenharmony_ci		aac_intr_normal(aac, 0, 2, 0, NULL);
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ci	/*
17688c2ecf20Sopenharmony_ci	 * dmb - we may need to move the setting of these parms somewhere else once
17698c2ecf20Sopenharmony_ci	 * we get a fib that can report the actual numbers
17708c2ecf20Sopenharmony_ci	 */
17718c2ecf20Sopenharmony_ci	shost->max_lun = AAC_MAX_LUN;
17728c2ecf20Sopenharmony_ci
17738c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, shost);
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci	error = scsi_add_host(shost, &pdev->dev);
17768c2ecf20Sopenharmony_ci	if (error)
17778c2ecf20Sopenharmony_ci		goto out_deinit;
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci	aac_scan_host(aac);
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci	pci_enable_pcie_error_reporting(pdev);
17828c2ecf20Sopenharmony_ci	pci_save_state(pdev);
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci	return 0;
17858c2ecf20Sopenharmony_ci
17868c2ecf20Sopenharmony_ci out_deinit:
17878c2ecf20Sopenharmony_ci	__aac_shutdown(aac);
17888c2ecf20Sopenharmony_ci out_unmap:
17898c2ecf20Sopenharmony_ci	aac_fib_map_free(aac);
17908c2ecf20Sopenharmony_ci	if (aac->comm_addr)
17918c2ecf20Sopenharmony_ci		dma_free_coherent(&aac->pdev->dev, aac->comm_size,
17928c2ecf20Sopenharmony_ci				  aac->comm_addr, aac->comm_phys);
17938c2ecf20Sopenharmony_ci	kfree(aac->queues);
17948c2ecf20Sopenharmony_ci	aac_adapter_ioremap(aac, 0);
17958c2ecf20Sopenharmony_ci	kfree(aac->fibs);
17968c2ecf20Sopenharmony_ci	kfree(aac->fsa_dev);
17978c2ecf20Sopenharmony_ci out_free_host:
17988c2ecf20Sopenharmony_ci	scsi_host_put(shost);
17998c2ecf20Sopenharmony_ci out_disable_pdev:
18008c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
18018c2ecf20Sopenharmony_ci out:
18028c2ecf20Sopenharmony_ci	return error;
18038c2ecf20Sopenharmony_ci}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_cistatic void aac_release_resources(struct aac_dev *aac)
18068c2ecf20Sopenharmony_ci{
18078c2ecf20Sopenharmony_ci	aac_adapter_disable_int(aac);
18088c2ecf20Sopenharmony_ci	aac_free_irq(aac);
18098c2ecf20Sopenharmony_ci}
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_cistatic int aac_acquire_resources(struct aac_dev *dev)
18128c2ecf20Sopenharmony_ci{
18138c2ecf20Sopenharmony_ci	unsigned long status;
18148c2ecf20Sopenharmony_ci	/*
18158c2ecf20Sopenharmony_ci	 *	First clear out all interrupts.  Then enable the one's that we
18168c2ecf20Sopenharmony_ci	 *	can handle.
18178c2ecf20Sopenharmony_ci	 */
18188c2ecf20Sopenharmony_ci	while (!((status = src_readl(dev, MUnit.OMR)) & KERNEL_UP_AND_RUNNING)
18198c2ecf20Sopenharmony_ci		|| status == 0xffffffff)
18208c2ecf20Sopenharmony_ci			msleep(20);
18218c2ecf20Sopenharmony_ci
18228c2ecf20Sopenharmony_ci	aac_adapter_disable_int(dev);
18238c2ecf20Sopenharmony_ci	aac_adapter_enable_int(dev);
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci
18268c2ecf20Sopenharmony_ci	if (aac_is_src(dev))
18278c2ecf20Sopenharmony_ci		aac_define_int_mode(dev);
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_ci	if (dev->msi_enabled)
18308c2ecf20Sopenharmony_ci		aac_src_access_devreg(dev, AAC_ENABLE_MSIX);
18318c2ecf20Sopenharmony_ci
18328c2ecf20Sopenharmony_ci	if (aac_acquire_irq(dev))
18338c2ecf20Sopenharmony_ci		goto error_iounmap;
18348c2ecf20Sopenharmony_ci
18358c2ecf20Sopenharmony_ci	aac_adapter_enable_int(dev);
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci	/*max msix may change  after EEH
18388c2ecf20Sopenharmony_ci	 * Re-assign vectors to fibs
18398c2ecf20Sopenharmony_ci	 */
18408c2ecf20Sopenharmony_ci	aac_fib_vector_assign(dev);
18418c2ecf20Sopenharmony_ci
18428c2ecf20Sopenharmony_ci	if (!dev->sync_mode) {
18438c2ecf20Sopenharmony_ci		/* After EEH recovery or suspend resume, max_msix count
18448c2ecf20Sopenharmony_ci		 * may change, therefore updating in init as well.
18458c2ecf20Sopenharmony_ci		 */
18468c2ecf20Sopenharmony_ci		dev->init->r7.no_of_msix_vectors = cpu_to_le32(dev->max_msix);
18478c2ecf20Sopenharmony_ci		aac_adapter_start(dev);
18488c2ecf20Sopenharmony_ci	}
18498c2ecf20Sopenharmony_ci	return 0;
18508c2ecf20Sopenharmony_ci
18518c2ecf20Sopenharmony_cierror_iounmap:
18528c2ecf20Sopenharmony_ci	return -1;
18538c2ecf20Sopenharmony_ci
18548c2ecf20Sopenharmony_ci}
18558c2ecf20Sopenharmony_ci
18568c2ecf20Sopenharmony_ci#if (defined(CONFIG_PM))
18578c2ecf20Sopenharmony_cistatic int aac_suspend(struct pci_dev *pdev, pm_message_t state)
18588c2ecf20Sopenharmony_ci{
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = pci_get_drvdata(pdev);
18618c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)shost->hostdata;
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci	scsi_host_block(shost);
18648c2ecf20Sopenharmony_ci	aac_cancel_rescan_worker(aac);
18658c2ecf20Sopenharmony_ci	aac_send_shutdown(aac);
18668c2ecf20Sopenharmony_ci
18678c2ecf20Sopenharmony_ci	aac_release_resources(aac);
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, shost);
18708c2ecf20Sopenharmony_ci	pci_save_state(pdev);
18718c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
18728c2ecf20Sopenharmony_ci	pci_set_power_state(pdev, pci_choose_state(pdev, state));
18738c2ecf20Sopenharmony_ci
18748c2ecf20Sopenharmony_ci	return 0;
18758c2ecf20Sopenharmony_ci}
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_cistatic int aac_resume(struct pci_dev *pdev)
18788c2ecf20Sopenharmony_ci{
18798c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = pci_get_drvdata(pdev);
18808c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)shost->hostdata;
18818c2ecf20Sopenharmony_ci	int r;
18828c2ecf20Sopenharmony_ci
18838c2ecf20Sopenharmony_ci	pci_set_power_state(pdev, PCI_D0);
18848c2ecf20Sopenharmony_ci	pci_enable_wake(pdev, PCI_D0, 0);
18858c2ecf20Sopenharmony_ci	pci_restore_state(pdev);
18868c2ecf20Sopenharmony_ci	r = pci_enable_device(pdev);
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci	if (r)
18898c2ecf20Sopenharmony_ci		goto fail_device;
18908c2ecf20Sopenharmony_ci
18918c2ecf20Sopenharmony_ci	pci_set_master(pdev);
18928c2ecf20Sopenharmony_ci	if (aac_acquire_resources(aac))
18938c2ecf20Sopenharmony_ci		goto fail_device;
18948c2ecf20Sopenharmony_ci	/*
18958c2ecf20Sopenharmony_ci	* reset this flag to unblock ioctl() as it was set at
18968c2ecf20Sopenharmony_ci	* aac_send_shutdown() to block ioctls from upperlayer
18978c2ecf20Sopenharmony_ci	*/
18988c2ecf20Sopenharmony_ci	aac->adapter_shutdown = 0;
18998c2ecf20Sopenharmony_ci	scsi_host_unblock(shost, SDEV_RUNNING);
19008c2ecf20Sopenharmony_ci
19018c2ecf20Sopenharmony_ci	return 0;
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_cifail_device:
19048c2ecf20Sopenharmony_ci	printk(KERN_INFO "%s%d: resume failed.\n", aac->name, aac->id);
19058c2ecf20Sopenharmony_ci	scsi_host_put(shost);
19068c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
19078c2ecf20Sopenharmony_ci	return -ENODEV;
19088c2ecf20Sopenharmony_ci}
19098c2ecf20Sopenharmony_ci#endif
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_cistatic void aac_shutdown(struct pci_dev *dev)
19128c2ecf20Sopenharmony_ci{
19138c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = pci_get_drvdata(dev);
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_ci	scsi_host_block(shost);
19168c2ecf20Sopenharmony_ci	__aac_shutdown((struct aac_dev *)shost->hostdata);
19178c2ecf20Sopenharmony_ci}
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_cistatic void aac_remove_one(struct pci_dev *pdev)
19208c2ecf20Sopenharmony_ci{
19218c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = pci_get_drvdata(pdev);
19228c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)shost->hostdata;
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_ci	aac_cancel_rescan_worker(aac);
19258c2ecf20Sopenharmony_ci	scsi_remove_host(shost);
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_ci	__aac_shutdown(aac);
19288c2ecf20Sopenharmony_ci	aac_fib_map_free(aac);
19298c2ecf20Sopenharmony_ci	dma_free_coherent(&aac->pdev->dev, aac->comm_size, aac->comm_addr,
19308c2ecf20Sopenharmony_ci			  aac->comm_phys);
19318c2ecf20Sopenharmony_ci	kfree(aac->queues);
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_ci	aac_adapter_ioremap(aac, 0);
19348c2ecf20Sopenharmony_ci
19358c2ecf20Sopenharmony_ci	kfree(aac->fibs);
19368c2ecf20Sopenharmony_ci	kfree(aac->fsa_dev);
19378c2ecf20Sopenharmony_ci
19388c2ecf20Sopenharmony_ci	list_del(&aac->entry);
19398c2ecf20Sopenharmony_ci	scsi_host_put(shost);
19408c2ecf20Sopenharmony_ci	pci_disable_device(pdev);
19418c2ecf20Sopenharmony_ci	if (list_empty(&aac_devices)) {
19428c2ecf20Sopenharmony_ci		unregister_chrdev(aac_cfg_major, "aac");
19438c2ecf20Sopenharmony_ci		aac_cfg_major = AAC_CHARDEV_NEEDS_REINIT;
19448c2ecf20Sopenharmony_ci	}
19458c2ecf20Sopenharmony_ci}
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_cistatic pci_ers_result_t aac_pci_error_detected(struct pci_dev *pdev,
19488c2ecf20Sopenharmony_ci					pci_channel_state_t error)
19498c2ecf20Sopenharmony_ci{
19508c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = pci_get_drvdata(pdev);
19518c2ecf20Sopenharmony_ci	struct aac_dev *aac = shost_priv(shost);
19528c2ecf20Sopenharmony_ci
19538c2ecf20Sopenharmony_ci	dev_err(&pdev->dev, "aacraid: PCI error detected %x\n", error);
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ci	switch (error) {
19568c2ecf20Sopenharmony_ci	case pci_channel_io_normal:
19578c2ecf20Sopenharmony_ci		return PCI_ERS_RESULT_CAN_RECOVER;
19588c2ecf20Sopenharmony_ci	case pci_channel_io_frozen:
19598c2ecf20Sopenharmony_ci		aac->handle_pci_error = 1;
19608c2ecf20Sopenharmony_ci
19618c2ecf20Sopenharmony_ci		scsi_host_block(shost);
19628c2ecf20Sopenharmony_ci		aac_cancel_rescan_worker(aac);
19638c2ecf20Sopenharmony_ci		scsi_host_complete_all_commands(shost, DID_NO_CONNECT);
19648c2ecf20Sopenharmony_ci		aac_release_resources(aac);
19658c2ecf20Sopenharmony_ci
19668c2ecf20Sopenharmony_ci		pci_disable_pcie_error_reporting(pdev);
19678c2ecf20Sopenharmony_ci		aac_adapter_ioremap(aac, 0);
19688c2ecf20Sopenharmony_ci
19698c2ecf20Sopenharmony_ci		return PCI_ERS_RESULT_NEED_RESET;
19708c2ecf20Sopenharmony_ci	case pci_channel_io_perm_failure:
19718c2ecf20Sopenharmony_ci		aac->handle_pci_error = 1;
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci		scsi_host_complete_all_commands(shost, DID_NO_CONNECT);
19748c2ecf20Sopenharmony_ci		return PCI_ERS_RESULT_DISCONNECT;
19758c2ecf20Sopenharmony_ci	}
19768c2ecf20Sopenharmony_ci
19778c2ecf20Sopenharmony_ci	return PCI_ERS_RESULT_NEED_RESET;
19788c2ecf20Sopenharmony_ci}
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_cistatic pci_ers_result_t aac_pci_mmio_enabled(struct pci_dev *pdev)
19818c2ecf20Sopenharmony_ci{
19828c2ecf20Sopenharmony_ci	dev_err(&pdev->dev, "aacraid: PCI error - mmio enabled\n");
19838c2ecf20Sopenharmony_ci	return PCI_ERS_RESULT_NEED_RESET;
19848c2ecf20Sopenharmony_ci}
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_cistatic pci_ers_result_t aac_pci_slot_reset(struct pci_dev *pdev)
19878c2ecf20Sopenharmony_ci{
19888c2ecf20Sopenharmony_ci	dev_err(&pdev->dev, "aacraid: PCI error - slot reset\n");
19898c2ecf20Sopenharmony_ci	pci_restore_state(pdev);
19908c2ecf20Sopenharmony_ci	if (pci_enable_device(pdev)) {
19918c2ecf20Sopenharmony_ci		dev_warn(&pdev->dev,
19928c2ecf20Sopenharmony_ci			"aacraid: failed to enable slave\n");
19938c2ecf20Sopenharmony_ci		goto fail_device;
19948c2ecf20Sopenharmony_ci	}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	pci_set_master(pdev);
19978c2ecf20Sopenharmony_ci
19988c2ecf20Sopenharmony_ci	if (pci_enable_device_mem(pdev)) {
19998c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "pci_enable_device_mem failed\n");
20008c2ecf20Sopenharmony_ci		goto fail_device;
20018c2ecf20Sopenharmony_ci	}
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci	return PCI_ERS_RESULT_RECOVERED;
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_cifail_device:
20068c2ecf20Sopenharmony_ci	dev_err(&pdev->dev, "aacraid: PCI error - slot reset failed\n");
20078c2ecf20Sopenharmony_ci	return PCI_ERS_RESULT_DISCONNECT;
20088c2ecf20Sopenharmony_ci}
20098c2ecf20Sopenharmony_ci
20108c2ecf20Sopenharmony_ci
20118c2ecf20Sopenharmony_cistatic void aac_pci_resume(struct pci_dev *pdev)
20128c2ecf20Sopenharmony_ci{
20138c2ecf20Sopenharmony_ci	struct Scsi_Host *shost = pci_get_drvdata(pdev);
20148c2ecf20Sopenharmony_ci	struct aac_dev *aac = (struct aac_dev *)shost_priv(shost);
20158c2ecf20Sopenharmony_ci
20168c2ecf20Sopenharmony_ci	if (aac_adapter_ioremap(aac, aac->base_size)) {
20178c2ecf20Sopenharmony_ci
20188c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "aacraid: ioremap failed\n");
20198c2ecf20Sopenharmony_ci		/* remap failed, go back ... */
20208c2ecf20Sopenharmony_ci		aac->comm_interface = AAC_COMM_PRODUCER;
20218c2ecf20Sopenharmony_ci		if (aac_adapter_ioremap(aac, AAC_MIN_FOOTPRINT_SIZE)) {
20228c2ecf20Sopenharmony_ci			dev_warn(&pdev->dev,
20238c2ecf20Sopenharmony_ci				"aacraid: unable to map adapter.\n");
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci			return;
20268c2ecf20Sopenharmony_ci		}
20278c2ecf20Sopenharmony_ci	}
20288c2ecf20Sopenharmony_ci
20298c2ecf20Sopenharmony_ci	msleep(10000);
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci	aac_acquire_resources(aac);
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci	/*
20348c2ecf20Sopenharmony_ci	 * reset this flag to unblock ioctl() as it was set
20358c2ecf20Sopenharmony_ci	 * at aac_send_shutdown() to block ioctls from upperlayer
20368c2ecf20Sopenharmony_ci	 */
20378c2ecf20Sopenharmony_ci	aac->adapter_shutdown = 0;
20388c2ecf20Sopenharmony_ci	aac->handle_pci_error = 0;
20398c2ecf20Sopenharmony_ci
20408c2ecf20Sopenharmony_ci	scsi_host_unblock(shost, SDEV_RUNNING);
20418c2ecf20Sopenharmony_ci	aac_scan_host(aac);
20428c2ecf20Sopenharmony_ci	pci_save_state(pdev);
20438c2ecf20Sopenharmony_ci
20448c2ecf20Sopenharmony_ci	dev_err(&pdev->dev, "aacraid: PCI error - resume\n");
20458c2ecf20Sopenharmony_ci}
20468c2ecf20Sopenharmony_ci
20478c2ecf20Sopenharmony_cistatic struct pci_error_handlers aac_pci_err_handler = {
20488c2ecf20Sopenharmony_ci	.error_detected		= aac_pci_error_detected,
20498c2ecf20Sopenharmony_ci	.mmio_enabled		= aac_pci_mmio_enabled,
20508c2ecf20Sopenharmony_ci	.slot_reset		= aac_pci_slot_reset,
20518c2ecf20Sopenharmony_ci	.resume			= aac_pci_resume,
20528c2ecf20Sopenharmony_ci};
20538c2ecf20Sopenharmony_ci
20548c2ecf20Sopenharmony_cistatic struct pci_driver aac_pci_driver = {
20558c2ecf20Sopenharmony_ci	.name		= AAC_DRIVERNAME,
20568c2ecf20Sopenharmony_ci	.id_table	= aac_pci_tbl,
20578c2ecf20Sopenharmony_ci	.probe		= aac_probe_one,
20588c2ecf20Sopenharmony_ci	.remove		= aac_remove_one,
20598c2ecf20Sopenharmony_ci#if (defined(CONFIG_PM))
20608c2ecf20Sopenharmony_ci	.suspend	= aac_suspend,
20618c2ecf20Sopenharmony_ci	.resume		= aac_resume,
20628c2ecf20Sopenharmony_ci#endif
20638c2ecf20Sopenharmony_ci	.shutdown	= aac_shutdown,
20648c2ecf20Sopenharmony_ci	.err_handler    = &aac_pci_err_handler,
20658c2ecf20Sopenharmony_ci};
20668c2ecf20Sopenharmony_ci
20678c2ecf20Sopenharmony_cistatic int __init aac_init(void)
20688c2ecf20Sopenharmony_ci{
20698c2ecf20Sopenharmony_ci	int error;
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_ci	printk(KERN_INFO "Adaptec %s driver %s\n",
20728c2ecf20Sopenharmony_ci	  AAC_DRIVERNAME, aac_driver_version);
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci	error = pci_register_driver(&aac_pci_driver);
20758c2ecf20Sopenharmony_ci	if (error < 0)
20768c2ecf20Sopenharmony_ci		return error;
20778c2ecf20Sopenharmony_ci
20788c2ecf20Sopenharmony_ci	aac_init_char();
20798c2ecf20Sopenharmony_ci
20808c2ecf20Sopenharmony_ci
20818c2ecf20Sopenharmony_ci	return 0;
20828c2ecf20Sopenharmony_ci}
20838c2ecf20Sopenharmony_ci
20848c2ecf20Sopenharmony_cistatic void __exit aac_exit(void)
20858c2ecf20Sopenharmony_ci{
20868c2ecf20Sopenharmony_ci	if (aac_cfg_major > -1)
20878c2ecf20Sopenharmony_ci		unregister_chrdev(aac_cfg_major, "aac");
20888c2ecf20Sopenharmony_ci	pci_unregister_driver(&aac_pci_driver);
20898c2ecf20Sopenharmony_ci}
20908c2ecf20Sopenharmony_ci
20918c2ecf20Sopenharmony_cimodule_init(aac_init);
20928c2ecf20Sopenharmony_cimodule_exit(aac_exit);
2093