blob: a1a2c71e162651f93d499c56e024e7073cc7fc79 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Adaptec AAC series RAID controller driver
Alan Coxfa195af2008-10-27 15:16:36 +00003 * (c) Copyright 2001 Red Hat Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07008 * Copyright (c) 2000-2010 Adaptec, Inc.
Raghava Aditya Renukuntaf4babba2017-02-02 15:53:36 -08009 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
Raghava Aditya Renukuntaf4babba2017-02-02 15:53:36 -080026 * Module Name:
27 * aachba.c
28 *
29 * Abstract: Contains Interfaces to manage IOs.
30 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
33#include <linux/kernel.h>
34#include <linux/init.h>
35#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/pci.h>
37#include <linux/spinlock.h>
38#include <linux/slab.h>
39#include <linux/completion.h>
40#include <linux/blkdev.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080041#include <linux/uaccess.h>
Salyzyn, Mark3a0086a82008-01-11 11:46:44 -080042#include <linux/highmem.h> /* For flush_kernel_dcache_page */
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040043#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <scsi/scsi.h>
46#include <scsi/scsi_cmnd.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_host.h>
49
50#include "aacraid.h"
51
52/* values for inqd_pdt: Peripheral device type in plain English */
53#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
54#define INQD_PDT_PROC 0x03 /* Processor device */
55#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
56#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
57#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
58#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
59
60#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
61#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Sense codes
65 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080066
67#define SENCODE_NO_SENSE 0x00
68#define SENCODE_END_OF_DATA 0x00
69#define SENCODE_BECOMING_READY 0x04
70#define SENCODE_INIT_CMD_REQUIRED 0x04
Raghava Aditya Renukuntaf956a662017-02-02 15:53:23 -080071#define SENCODE_UNRECOVERED_READ_ERROR 0x11
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080072#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
73#define SENCODE_INVALID_COMMAND 0x20
74#define SENCODE_LBA_OUT_OF_RANGE 0x21
75#define SENCODE_INVALID_CDB_FIELD 0x24
76#define SENCODE_LUN_NOT_SUPPORTED 0x25
77#define SENCODE_INVALID_PARAM_FIELD 0x26
78#define SENCODE_PARAM_NOT_SUPPORTED 0x26
79#define SENCODE_PARAM_VALUE_INVALID 0x26
80#define SENCODE_RESET_OCCURRED 0x29
81#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
82#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
83#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
84#define SENCODE_DIAGNOSTIC_FAILURE 0x40
85#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
86#define SENCODE_INVALID_MESSAGE_ERROR 0x49
87#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
88#define SENCODE_OVERLAPPED_COMMAND 0x4E
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/*
91 * Additional sense codes
92 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080093
94#define ASENCODE_NO_SENSE 0x00
95#define ASENCODE_END_OF_DATA 0x05
96#define ASENCODE_BECOMING_READY 0x01
97#define ASENCODE_INIT_CMD_REQUIRED 0x02
98#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
99#define ASENCODE_INVALID_COMMAND 0x00
100#define ASENCODE_LBA_OUT_OF_RANGE 0x00
101#define ASENCODE_INVALID_CDB_FIELD 0x00
102#define ASENCODE_LUN_NOT_SUPPORTED 0x00
103#define ASENCODE_INVALID_PARAM_FIELD 0x00
104#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
105#define ASENCODE_PARAM_VALUE_INVALID 0x02
106#define ASENCODE_RESET_OCCURRED 0x00
107#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
108#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
109#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
110#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
111#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
112#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
113#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
114#define ASENCODE_OVERLAPPED_COMMAND 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -0800116#define AAC_STAT_GOOD (DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD)
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define BYTE0(x) (unsigned char)(x)
119#define BYTE1(x) (unsigned char)((x) >> 8)
120#define BYTE2(x) (unsigned char)((x) >> 16)
121#define BYTE3(x) (unsigned char)((x) >> 24)
122
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400123/* MODE_SENSE data format */
124typedef struct {
125 struct {
126 u8 data_length;
127 u8 med_type;
128 u8 dev_par;
129 u8 bd_length;
130 } __attribute__((packed)) hd;
131 struct {
132 u8 dens_code;
133 u8 block_count[3];
134 u8 reserved;
135 u8 block_length[3];
136 } __attribute__((packed)) bd;
137 u8 mpc_buf[3];
138} __attribute__((packed)) aac_modep_data;
139
140/* MODE_SENSE_10 data format */
141typedef struct {
142 struct {
143 u8 data_length[2];
144 u8 med_type;
145 u8 dev_par;
146 u8 rsrvd[2];
147 u8 bd_length[2];
148 } __attribute__((packed)) hd;
149 struct {
150 u8 dens_code;
151 u8 block_count[3];
152 u8 reserved;
153 u8 block_length[3];
154 } __attribute__((packed)) bd;
155 u8 mpc_buf[3];
156} __attribute__((packed)) aac_modep10_data;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*------------------------------------------------------------------------------
159 * S T R U C T S / T Y P E D E F S
160 *----------------------------------------------------------------------------*/
161/* SCSI inquiry data */
162struct inquiry_data {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800163 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
164 u8 inqd_dtq; /* RMB | Device Type Qualifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
166 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
167 u8 inqd_len; /* Additional length (n-4) */
168 u8 inqd_pad1[2];/* Reserved - must be zero */
169 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
170 u8 inqd_vid[8]; /* Vendor ID */
171 u8 inqd_pid[16];/* Product ID */
172 u8 inqd_prl[4]; /* Product Revision Level */
173};
174
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400175/* Added for VPD 0x83 */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800176struct tvpd_id_descriptor_type_1 {
177 u8 codeset:4; /* VPD_CODE_SET */
178 u8 reserved:4;
179 u8 identifiertype:4; /* VPD_IDENTIFIER_TYPE */
180 u8 reserved2:4;
181 u8 reserved3;
182 u8 identifierlength;
183 u8 venid[8];
184 u8 productid[16];
185 u8 serialnumber[8]; /* SN in ASCII */
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400186
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800187};
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400188
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800189struct tvpd_id_descriptor_type_2 {
190 u8 codeset:4; /* VPD_CODE_SET */
191 u8 reserved:4;
192 u8 identifiertype:4; /* VPD_IDENTIFIER_TYPE */
193 u8 reserved2:4;
194 u8 reserved3;
195 u8 identifierlength;
196 struct teu64id {
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400197 u32 Serial;
198 /* The serial number supposed to be 40 bits,
199 * bit we only support 32, so make the last byte zero. */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800200 u8 reserved;
201 u8 venid[3];
202 } eu64id;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400203
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800204};
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400205
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800206struct tvpd_id_descriptor_type_3 {
207 u8 codeset : 4; /* VPD_CODE_SET */
208 u8 reserved : 4;
209 u8 identifiertype : 4; /* VPD_IDENTIFIER_TYPE */
210 u8 reserved2 : 4;
211 u8 reserved3;
212 u8 identifierlength;
213 u8 Identifier[16];
214};
215
216struct tvpd_page83 {
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400217 u8 DeviceType:5;
218 u8 DeviceTypeQualifier:3;
219 u8 PageCode;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800220 u8 reserved;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400221 u8 PageLength;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800222 struct tvpd_id_descriptor_type_1 type1;
223 struct tvpd_id_descriptor_type_2 type2;
224 struct tvpd_id_descriptor_type_3 type3;
225};
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*
228 * M O D U L E G L O B A L S
229 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800230
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +0530231static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
232static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
233static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
234static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
235 struct aac_raw_io2 *rio2, int sg_max);
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -0800236static long aac_build_sghba(struct scsi_cmnd *scsicmd,
237 struct aac_hba_cmd_req *hbacmd,
238 int sg_max, u64 sg_address);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +0530239static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
240 int pages, int nseg, int nseg_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -0800242static int aac_send_hba_fib(struct scsi_cmnd *scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#ifdef AAC_DETAILED_STATUS_INFO
244static char *aac_get_status_string(u32 status);
245#endif
246
247/*
248 * Non dasd selection is handled entirely in aachba now
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800249 */
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int nondasd = -1;
Leubner, Achimd8e965072009-04-01 07:16:08 -0700252static int aac_cache = 2; /* WCE=0 to avoid performance problems */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int dacmode = -1;
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800254int aac_msi;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400255int aac_commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700256int startup_timeout = 180;
257int aif_timeout = 120;
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800258int aac_sync_mode; /* Only Sync. transfer - disabled */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530259int aac_convert_sgl = 1; /* convert non-conformable s/g list - enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800261module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
262MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
263 " 0=off, 1=on");
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530264module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
265MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
266 " 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800267module_param(nondasd, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800268MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
269 " 0=off, 1=on");
Salyzyn, Mark95e852e2008-01-08 12:01:07 -0800270module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800271MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
272 "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
273 "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
Leubner, Achimd8e965072009-04-01 07:16:08 -0700274 "\tbit 2 - Disable only if Battery is protecting Cache");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800275module_param(dacmode, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800276MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
277 " 0=off, 1=on");
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400278module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800279MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
280 " adapter for foreign arrays.\n"
281 "This is typically needed in systems that do not have a BIOS."
282 " 0=off, 1=on");
283module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
284MODULE_PARM_DESC(msi, "IRQ handling."
Mahesh Rajashekhara9022d372015-08-28 06:38:35 -0400285 " 0=PIC(default), 1=MSI, 2=MSI-X)");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700286module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800287MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
288 " adapter to have it's kernel up and\n"
289 "running. This is typically adjusted for large systems that do not"
290 " have a BIOS.");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700291module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800292MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
293 " applications to pick up AIFs before\n"
294 "deregistering them. This is typically adjusted for heavily burdened"
295 " systems.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Raghava Aditya Renukunta09624642017-02-16 12:51:22 -0800297int aac_fib_dump;
298module_param(aac_fib_dump, int, 0644);
299MODULE_PARM_DESC(aac_fib_dump, "Dump controller fibs prior to IOP_RESET 0=off, 1=on");
300
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700301int numacb = -1;
302module_param(numacb, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800303MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
304 " blocks (FIB) allocated. Valid values are 512 and down. Default is"
305 " to use suggestion from Firmware.");
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700306
307int acbsize = -1;
308module_param(acbsize, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800309MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
310 " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
311 " suggestion from Firmware.");
Mark Haverkamp653ba582006-09-19 08:59:43 -0700312
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400313int update_interval = 30 * 60;
314module_param(update_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800315MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
316 " updates issued to adapter.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400317
Raghava Aditya Renukunta11da1b72017-02-16 12:51:19 -0800318int check_interval = 60;
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400319module_param(check_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800320MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
321 " checks.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400322
Andrew Morton87f3bda2007-08-10 14:50:30 -0700323int aac_check_reset = 1;
324module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
Paul Bolle95e7a8e2008-04-16 14:43:00 +0200325MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800326 " adapter. a value of -1 forces the reset to adapters programmed to"
327 " ignore it.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400328
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800329int expose_physicals = -1;
Mark Haverkamp653ba582006-09-19 08:59:43 -0700330module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800331MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
332 " -1=protect 0=off, 1=on");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700333
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800334int aac_reset_devices;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400335module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
336MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700337
Leubner, Achimd8e965072009-04-01 07:16:08 -0700338int aac_wwn = 1;
339module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
340MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
341 "\t0 - Disable\n"
342 "\t1 - Array Meta Data Signature (default)\n"
343 "\t2 - Adapter Serial Number");
344
345
Mark Haverkamp03d44332007-03-15 10:27:45 -0700346static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
347 struct fib *fibptr) {
348 struct scsi_device *device;
349
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800350 if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
Salyzyn, Markc835e372007-07-26 14:20:02 -0400351 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800352 aac_fib_complete(fibptr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800353 return 0;
354 }
Mark Haverkamp03d44332007-03-15 10:27:45 -0700355 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
356 device = scsicmd->device;
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800357 if (unlikely(!device)) {
Mark Haverkamp03d44332007-03-15 10:27:45 -0700358 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
359 aac_fib_complete(fibptr);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700360 return 0;
361 }
362 return 1;
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/**
366 * aac_get_config_status - check the adapter configuration
367 * @common: adapter to query
368 *
369 * Query config status, and commit the configuration if needed.
370 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700371int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 int status = 0;
374 struct fib * fibptr;
375
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800376 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -ENOMEM;
378
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800379 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 {
381 struct aac_get_config_status *dinfo;
382 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
383
384 dinfo->command = cpu_to_le32(VM_ContainerConfig);
385 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
386 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
387 }
388
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800389 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 fibptr,
391 sizeof (struct aac_get_config_status),
392 FsaNormal,
393 1, 1,
394 NULL, NULL);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800395 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
397 } else {
398 struct aac_get_config_status_resp *reply
399 = (struct aac_get_config_status_resp *) fib_data(fibptr);
400 dprintk((KERN_WARNING
401 "aac_get_config_status: response=%d status=%d action=%d\n",
402 le32_to_cpu(reply->response),
403 le32_to_cpu(reply->status),
404 le32_to_cpu(reply->data.action)));
405 if ((le32_to_cpu(reply->response) != ST_OK) ||
406 (le32_to_cpu(reply->status) != CT_OK) ||
407 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
408 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
409 status = -EINVAL;
410 }
411 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530412 /* Do not set XferState to zero unless receives a response from F/W */
413 if (status >= 0)
414 aac_fib_complete(fibptr);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
417 if (status >= 0) {
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400418 if ((aac_commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800420 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dinfo = (struct aac_commit_config *) fib_data(fibptr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dinfo->command = cpu_to_le32(VM_ContainerConfig);
424 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800425
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800426 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 fibptr,
428 sizeof (struct aac_commit_config),
429 FsaNormal,
430 1, 1,
431 NULL, NULL);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530432 /* Do not set XferState to zero unless
433 * receives a response from F/W */
434 if (status >= 0)
435 aac_fib_complete(fibptr);
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400436 } else if (aac_commit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 printk(KERN_WARNING
438 "aac_get_config_status: Foreign device configurations are being ignored\n");
439 }
440 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530441 /* FIB should be freed only after getting the response from the F/W */
442 if (status != -ERESTARTSYS)
443 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return status;
445}
446
Rajashekhara, Maheshe3cc2682010-05-10 04:12:28 -0700447static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
448{
449 char inq_data;
450 scsi_sg_copy_to_buffer(scsicmd, &inq_data, sizeof(inq_data));
451 if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
452 inq_data &= 0xdf;
453 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
454 }
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/**
458 * aac_get_containers - list containers
459 * @common: adapter to probe
460 *
461 * Make a list of all containers on this controller
462 */
463int aac_get_containers(struct aac_dev *dev)
464{
465 struct fsa_dev_info *fsa_dev_ptr;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800466 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int status = 0;
468 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct aac_get_container_count *dinfo;
470 struct aac_get_container_count_resp *dresp;
471 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
472
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800473 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -ENOMEM;
475
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800476 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
478 dinfo->command = cpu_to_le32(VM_ContainerConfig);
479 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
480
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800481 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 fibptr,
483 sizeof (struct aac_get_container_count),
484 FsaNormal,
485 1, 1,
486 NULL, NULL);
487 if (status >= 0) {
488 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
489 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800490 if (fibptr->dev->supplement_adapter_info.supported_options2 &
Mahesh Rajashekharaa7129a52015-03-26 10:41:27 -0400491 AAC_OPTION_SUPPORTED_240_VOLUMES) {
492 maximum_num_containers =
493 le32_to_cpu(dresp->MaxSimpleVolumes);
494 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800495 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530497 /* FIB should be freed only after getting the response from the F/W */
498 if (status != -ERESTARTSYS)
499 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
502 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800503 if (dev->fsa_dev == NULL ||
504 dev->maximum_num_containers != maximum_num_containers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800506 fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800508 dev->fsa_dev = kcalloc(maximum_num_containers,
509 sizeof(*fsa_dev_ptr), GFP_KERNEL);
510
511 kfree(fsa_dev_ptr);
512 fsa_dev_ptr = NULL;
513
514
515 if (!dev->fsa_dev)
516 return -ENOMEM;
517
518 dev->maximum_num_containers = maximum_num_containers;
519 }
520 for (index = 0; index < dev->maximum_num_containers; index++) {
521 dev->fsa_dev[index].devname[0] = '\0';
522 dev->fsa_dev[index].valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Mark Haverkampfe76df42007-03-15 12:55:07 -0700524 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Mark Haverkampfe76df42007-03-15 12:55:07 -0700526 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
528 break;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return status;
532}
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534static void get_container_name_callback(void *context, struct fib * fibptr)
535{
536 struct aac_get_name_resp * get_name_reply;
537 struct scsi_cmnd * scsicmd;
538
539 scsicmd = (struct scsi_cmnd *) context;
540
Mark Haverkamp03d44332007-03-15 10:27:45 -0700541 if (!aac_valid_context(scsicmd, fibptr))
542 return;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700545 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
548 /* Failure is irrelevant, using default value instead */
549 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
550 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700551 char *sp = get_name_reply->data;
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700552 int data_size = FIELD_SIZEOF(struct aac_get_name_resp, data);
553
554 sp[data_size - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 while (*sp == ' ')
556 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700557 if (*sp) {
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900558 struct inquiry_data inq;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700559 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
560 int count = sizeof(d);
561 char *dp = d;
562 do {
563 *dp++ = (*sp) ? *sp++ : ' ';
564 } while (--count > 0);
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900565
566 scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
567 memcpy(inq.inqd_pid, d, sizeof(d));
568 scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
573
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800574 aac_fib_complete(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700575 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
578/**
579 * aac_get_container_name - get container name, none blocking.
580 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700581static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 int status;
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700584 int data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct aac_get_name *dinfo;
586 struct fib * cmd_fibcontext;
587 struct aac_dev * dev;
588
589 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
590
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700591 data_size = FIELD_SIZEOF(struct aac_get_name_resp, data);
592
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -0800593 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800595 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
597
598 dinfo->command = cpu_to_le32(VM_ContainerConfig);
599 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700600 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700601 dinfo->count = cpu_to_le32(data_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800603 status = aac_fib_send(ContainerCommand,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800604 cmd_fibcontext,
Mahesh Rajashekharafb5d40d2015-08-28 06:38:33 -0400605 sizeof(struct aac_get_name_resp),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800606 FsaNormal,
607 0, 1,
608 (fib_callback)get_container_name_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 (void *) scsicmd);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Check that the command queued to the controller
613 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800614 if (status == -EINPROGRESS) {
615 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800617 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800618
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800619 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
620 aac_fib_complete(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -1;
622}
623
Mark Haverkampfe76df42007-03-15 12:55:07 -0700624static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
625{
626 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
627
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400628 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700629 return aac_scsi_cmd(scsicmd);
630
631 scsicmd->result = DID_NO_CONNECT << 16;
632 scsicmd->scsi_done(scsicmd);
633 return 0;
634}
635
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400636static void _aac_probe_container2(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700637{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700638 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700639 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700640 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800641 int i;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700642
Mark Haverkamp03d44332007-03-15 10:27:45 -0700643
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400644 if (!aac_valid_context(scsicmd, fibptr))
645 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700646
647 scsicmd->SCp.Status = 0;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400648 fsa_dev_ptr = fibptr->dev->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700649 if (fsa_dev_ptr) {
650 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800651 __le32 sup_options2;
652
Mark Haverkampfe76df42007-03-15 12:55:07 -0700653 fsa_dev_ptr += scmd_id(scsicmd);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800654 sup_options2 =
655 fibptr->dev->supplement_adapter_info.supported_options2;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700656
657 if ((le32_to_cpu(dresp->status) == ST_OK) &&
658 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
659 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800660 if (!(sup_options2 & AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400661 dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
662 fsa_dev_ptr->block_size = 0x200;
663 } else {
664 fsa_dev_ptr->block_size =
665 le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
666 }
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800667 for (i = 0; i < 16; i++)
668 fsa_dev_ptr->identifier[i] =
669 dresp->mnt[0].fileinfo.bdevinfo
670 .identifier[i];
Mark Haverkampfe76df42007-03-15 12:55:07 -0700671 fsa_dev_ptr->valid = 1;
Mark Salyzyn655d7222008-04-30 16:03:42 -0400672 /* sense_key holds the current state of the spin-up */
673 if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
674 fsa_dev_ptr->sense_data.sense_key = NOT_READY;
675 else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
676 fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700677 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
678 fsa_dev_ptr->size
679 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
680 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
681 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
682 }
683 if ((fsa_dev_ptr->valid & 1) == 0)
684 fsa_dev_ptr->valid = 0;
685 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
686 }
687 aac_fib_complete(fibptr);
688 aac_fib_free(fibptr);
689 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
690 scsicmd->SCp.ptr = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400691 (*callback)(scsicmd);
692 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700693}
694
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400695static void _aac_probe_container1(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700696{
697 struct scsi_cmnd * scsicmd;
698 struct aac_mount * dresp;
699 struct aac_query_mount *dinfo;
700 int status;
701
702 dresp = (struct aac_mount *) fib_data(fibptr);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800703 if (!(fibptr->dev->supplement_adapter_info.supported_options2 &
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400704 AAC_OPTION_VARIABLE_BLOCK_SIZE))
705 dresp->mnt[0].capacityhigh = 0;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700706 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400707 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
708 _aac_probe_container2(context, fibptr);
709 return;
710 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700711 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700712
Mark Haverkamp03d44332007-03-15 10:27:45 -0700713 if (!aac_valid_context(scsicmd, fibptr))
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400714 return;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700715
Mark Haverkampfe76df42007-03-15 12:55:07 -0700716 aac_fib_init(fibptr);
717
718 dinfo = (struct aac_query_mount *)fib_data(fibptr);
719
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800720 if (fibptr->dev->supplement_adapter_info.supported_options2 &
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400721 AAC_OPTION_VARIABLE_BLOCK_SIZE)
722 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
723 else
724 dinfo->command = cpu_to_le32(VM_NameServe64);
725
Mark Haverkampfe76df42007-03-15 12:55:07 -0700726 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
727 dinfo->type = cpu_to_le32(FT_FILESYS);
728
729 status = aac_fib_send(ContainerCommand,
730 fibptr,
731 sizeof(struct aac_query_mount),
732 FsaNormal,
733 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400734 _aac_probe_container2,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700735 (void *) scsicmd);
736 /*
737 * Check that the command queued to the controller
738 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400739 if (status == -EINPROGRESS)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700740 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400741 else if (status < 0) {
Mark Haverkampfe76df42007-03-15 12:55:07 -0700742 /* Inherit results from VM_NameServe, if any */
743 dresp->status = cpu_to_le32(ST_OK);
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400744 _aac_probe_container2(context, fibptr);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700745 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700746}
747
748static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
749{
750 struct fib * fibptr;
751 int status = -ENOMEM;
752
753 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
754 struct aac_query_mount *dinfo;
755
756 aac_fib_init(fibptr);
757
758 dinfo = (struct aac_query_mount *)fib_data(fibptr);
759
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800760 if (fibptr->dev->supplement_adapter_info.supported_options2 &
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400761 AAC_OPTION_VARIABLE_BLOCK_SIZE)
762 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
763 else
764 dinfo->command = cpu_to_le32(VM_NameServe);
765
Mark Haverkampfe76df42007-03-15 12:55:07 -0700766 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
767 dinfo->type = cpu_to_le32(FT_FILESYS);
768 scsicmd->SCp.ptr = (char *)callback;
769
770 status = aac_fib_send(ContainerCommand,
771 fibptr,
772 sizeof(struct aac_query_mount),
773 FsaNormal,
774 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400775 _aac_probe_container1,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700776 (void *) scsicmd);
777 /*
778 * Check that the command queued to the controller
779 */
780 if (status == -EINPROGRESS) {
781 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
782 return 0;
783 }
784 if (status < 0) {
785 scsicmd->SCp.ptr = NULL;
786 aac_fib_complete(fibptr);
787 aac_fib_free(fibptr);
788 }
789 }
790 if (status < 0) {
791 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
792 if (fsa_dev_ptr) {
793 fsa_dev_ptr += scmd_id(scsicmd);
794 if ((fsa_dev_ptr->valid & 1) == 0) {
795 fsa_dev_ptr->valid = 0;
796 return (*callback)(scsicmd);
797 }
798 }
799 }
800 return status;
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800804 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * @dev: device to query
806 * @cid: container identifier
807 *
808 * Queries the controller about the given volume. The volume information
809 * is updated in the struct fsa_dev_info structure rather than returned.
810 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700811static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
812{
813 scsicmd->device = NULL;
814 return 0;
815}
816
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800817int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700819 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
820 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Mark Haverkampfe76df42007-03-15 12:55:07 -0700823 if (!scsicmd || !scsidev) {
824 kfree(scsicmd);
825 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700826 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700828 scsicmd->list.next = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400829 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Mark Haverkampfe76df42007-03-15 12:55:07 -0700831 scsicmd->device = scsidev;
832 scsidev->sdev_state = 0;
833 scsidev->id = cid;
834 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Mark Haverkampfe76df42007-03-15 12:55:07 -0700836 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
837 while (scsicmd->device == scsidev)
838 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400839 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700840 status = scsicmd->SCp.Status;
841 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return status;
843}
844
845/* Local Structure to set SCSI inquiry data strings */
846struct scsi_inq {
847 char vid[8]; /* Vendor ID */
848 char pid[16]; /* Product ID */
849 char prl[4]; /* Product Revision Level */
850};
851
852/**
853 * InqStrCopy - string merge
854 * @a: string to copy from
855 * @b: string to copy to
856 *
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800857 * Copy a String from one location to another
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * without copying \0
859 */
860
861static void inqstrcpy(char *a, char *b)
862{
863
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800864 while (*a != (char)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *b++ = *a++;
866}
867
868static char *container_types[] = {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800869 "None",
870 "Volume",
871 "Mirror",
872 "Stripe",
873 "RAID5",
874 "SSRW",
875 "SSRO",
876 "Morph",
877 "Legacy",
878 "RAID4",
879 "RAID10",
880 "RAID00",
881 "V-MIRRORS",
882 "PSEUDO R4",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700884 "RAID5D",
885 "RAID5D0",
886 "RAID1E",
887 "RAID6",
888 "RAID60",
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800889 "Unknown"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890};
891
Salyzyn, Mark17eaace2008-01-08 12:23:49 -0800892char * get_container_type(unsigned tindex)
893{
894 if (tindex >= ARRAY_SIZE(container_types))
895 tindex = ARRAY_SIZE(container_types) - 1;
896 return container_types[tindex];
897}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899/* Function: setinqstr
900 *
901 * Arguments: [1] pointer to void [1] int
902 *
903 * Purpose: Sets SCSI inquiry data strings for vendor, product
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300904 * and revision level. Allows strings to be set in platform dependent
905 * files instead of in OS dependent driver source.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
907
Mark Haverkamp794d0602005-10-24 10:51:53 -0700908static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct scsi_inq *str;
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800911 struct aac_supplement_adapter_info *sup_adap_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800913 sup_adap_info = &dev->supplement_adapter_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700915 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800917 if (sup_adap_info->adapter_type_text[0]) {
918 char *cp = sup_adap_info->adapter_type_text;
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800919 int c;
920 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
921 inqstrcpy("SMC", str->vid);
922 else {
923 c = sizeof(str->vid);
924 while (*cp && *cp != ' ' && --c)
925 ++cp;
926 c = *cp;
927 *cp = '\0';
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800928 inqstrcpy(sup_adap_info->adapter_type_text, str->vid);
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800929 *cp = c;
930 while (*cp && *cp != ' ')
931 ++cp;
932 }
Mark Haverkamp794d0602005-10-24 10:51:53 -0700933 while (*cp == ' ')
934 ++cp;
935 /* last six chars reserved for vol type */
936 c = 0;
937 if (strlen(cp) > sizeof(str->pid)) {
938 c = cp[sizeof(str->pid)];
939 cp[sizeof(str->pid)] = '\0';
940 }
941 inqstrcpy (cp, str->pid);
942 if (c)
943 cp[sizeof(str->pid)] = c;
944 } else {
945 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700946
947 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700948 /* last six chars reserved for vol type */
949 inqstrcpy (mp->model, str->pid);
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Tobias Klauser6391a112006-06-08 22:23:48 -0700952 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 char *findit = str->pid;
954
955 for ( ; *findit != ' '; findit++); /* walk till we find a space */
956 /* RAID is superfluous in the context of a RAID device */
957 if (memcmp(findit-4, "RAID", 4) == 0)
958 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700959 if (((findit - str->pid) + strlen(container_types[tindex]))
960 < (sizeof(str->pid) + sizeof(str->prl)))
961 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 inqstrcpy ("V1.0", str->prl);
964}
965
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800966static void build_vpd83_type3(struct tvpd_page83 *vpdpage83data,
967 struct aac_dev *dev, struct scsi_cmnd *scsicmd)
968{
969 int container;
970
971 vpdpage83data->type3.codeset = 1;
972 vpdpage83data->type3.identifiertype = 3;
973 vpdpage83data->type3.identifierlength = sizeof(vpdpage83data->type3)
974 - 4;
975
976 for (container = 0; container < dev->maximum_num_containers;
977 container++) {
978
979 if (scmd_id(scsicmd) == container) {
980 memcpy(vpdpage83data->type3.Identifier,
981 dev->fsa_dev[container].identifier,
982 16);
983 break;
984 }
985 }
986}
987
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400988static void get_container_serial_callback(void *context, struct fib * fibptr)
989{
990 struct aac_get_serial_resp * get_serial_reply;
991 struct scsi_cmnd * scsicmd;
992
993 BUG_ON(fibptr == NULL);
994
995 scsicmd = (struct scsi_cmnd *) context;
996 if (!aac_valid_context(scsicmd, fibptr))
997 return;
998
999 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
1000 /* Failure is irrelevant, using default value instead */
1001 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001002 /*Check to see if it's for VPD 0x83 or 0x80 */
1003 if (scsicmd->cmnd[2] == 0x83) {
1004 /* vpd page 0x83 - Device Identification Page */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001005 struct aac_dev *dev;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001006 int i;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001007 struct tvpd_page83 vpdpage83data;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001008
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001009 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1010
1011 memset(((u8 *)&vpdpage83data), 0,
1012 sizeof(vpdpage83data));
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001013
1014 /* DIRECT_ACCESS_DEVIC */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001015 vpdpage83data.DeviceType = 0;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001016 /* DEVICE_CONNECTED */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001017 vpdpage83data.DeviceTypeQualifier = 0;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001018 /* VPD_DEVICE_IDENTIFIERS */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001019 vpdpage83data.PageCode = 0x83;
1020 vpdpage83data.reserved = 0;
1021 vpdpage83data.PageLength =
1022 sizeof(vpdpage83data.type1) +
1023 sizeof(vpdpage83data.type2);
1024
1025 /* VPD 83 Type 3 is not supported for ARC */
1026 if (dev->sa_firmware)
1027 vpdpage83data.PageLength +=
1028 sizeof(vpdpage83data.type3);
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001029
1030 /* T10 Vendor Identifier Field Format */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001031 /* VpdcodesetAscii */
1032 vpdpage83data.type1.codeset = 2;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001033 /* VpdIdentifierTypeVendorId */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001034 vpdpage83data.type1.identifiertype = 1;
1035 vpdpage83data.type1.identifierlength =
1036 sizeof(vpdpage83data.type1) - 4;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001037
1038 /* "ADAPTEC " for adaptec */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001039 memcpy(vpdpage83data.type1.venid,
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001040 "ADAPTEC ",
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001041 sizeof(vpdpage83data.type1.venid));
1042 memcpy(vpdpage83data.type1.productid,
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001043 "ARRAY ",
1044 sizeof(
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001045 vpdpage83data.type1.productid));
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001046
1047 /* Convert to ascii based serial number.
1048 * The LSB is the the end.
1049 */
1050 for (i = 0; i < 8; i++) {
1051 u8 temp =
1052 (u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
1053 if (temp > 0x9) {
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001054 vpdpage83data.type1.serialnumber[i] =
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001055 'A' + (temp - 0xA);
1056 } else {
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001057 vpdpage83data.type1.serialnumber[i] =
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001058 '0' + temp;
1059 }
1060 }
1061
1062 /* VpdCodeSetBinary */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001063 vpdpage83data.type2.codeset = 1;
1064 /* VpdidentifiertypeEUI64 */
1065 vpdpage83data.type2.identifiertype = 2;
1066 vpdpage83data.type2.identifierlength =
1067 sizeof(vpdpage83data.type2) - 4;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001068
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001069 vpdpage83data.type2.eu64id.venid[0] = 0xD0;
1070 vpdpage83data.type2.eu64id.venid[1] = 0;
1071 vpdpage83data.type2.eu64id.venid[2] = 0;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001072
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001073 vpdpage83data.type2.eu64id.Serial =
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001074 get_serial_reply->uid;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001075 vpdpage83data.type2.eu64id.reserved = 0;
1076
1077 /*
1078 * VpdIdentifierTypeFCPHName
1079 * VPD 0x83 Type 3 not supported for ARC
1080 */
1081 if (dev->sa_firmware) {
1082 build_vpd83_type3(&vpdpage83data,
1083 dev, scsicmd);
1084 }
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001085
1086 /* Move the inquiry data to the response buffer. */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001087 scsi_sg_copy_from_buffer(scsicmd, &vpdpage83data,
1088 sizeof(vpdpage83data));
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001089 } else {
1090 /* It must be for VPD 0x80 */
1091 char sp[13];
1092 /* EVPD bit set */
1093 sp[0] = INQD_PDT_DA;
1094 sp[1] = scsicmd->cmnd[2];
1095 sp[2] = 0;
1096 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1097 le32_to_cpu(get_serial_reply->uid));
1098 scsi_sg_copy_from_buffer(scsicmd, sp,
1099 sizeof(sp));
1100 }
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001101 }
1102
1103 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1104
1105 aac_fib_complete(fibptr);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001106 scsicmd->scsi_done(scsicmd);
1107}
1108
1109/**
1110 * aac_get_container_serial - get container serial, none blocking.
1111 */
1112static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1113{
1114 int status;
1115 struct aac_get_serial *dinfo;
1116 struct fib * cmd_fibcontext;
1117 struct aac_dev * dev;
1118
1119 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1120
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08001121 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001122
1123 aac_fib_init(cmd_fibcontext);
1124 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1125
1126 dinfo->command = cpu_to_le32(VM_ContainerConfig);
1127 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1128 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1129
1130 status = aac_fib_send(ContainerCommand,
1131 cmd_fibcontext,
Mahesh Rajashekharafb5d40d2015-08-28 06:38:33 -04001132 sizeof(struct aac_get_serial_resp),
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001133 FsaNormal,
1134 0, 1,
1135 (fib_callback) get_container_serial_callback,
1136 (void *) scsicmd);
1137
1138 /*
1139 * Check that the command queued to the controller
1140 */
1141 if (status == -EINPROGRESS) {
1142 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1143 return 0;
1144 }
1145
1146 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1147 aac_fib_complete(cmd_fibcontext);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001148 return -1;
1149}
1150
1151/* Function: setinqserial
1152 *
1153 * Arguments: [1] pointer to void [1] int
1154 *
1155 * Purpose: Sets SCSI Unit Serial number.
1156 * This is a fake. We should read a proper
1157 * serial number from the container. <SuSE>But
1158 * without docs it's quite hard to do it :-)
1159 * So this will have to do in the meantime.</SuSE>
1160 */
1161
1162static int setinqserial(struct aac_dev *dev, void *data, int cid)
1163{
1164 /*
1165 * This breaks array migration.
1166 */
1167 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1168 le32_to_cpu(dev->adapter_info.serial[0]), cid);
1169}
1170
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001171static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1172 u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001174 u8 *sense_buf = (u8 *)sense_data;
1175 /* Sense data valid, err code 70h */
1176 sense_buf[0] = 0x70; /* No info field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 sense_buf[1] = 0; /* Segment number, always zero */
1178
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001179 sense_buf[2] = sense_key; /* Sense key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 sense_buf[12] = sense_code; /* Additional sense code */
1182 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (sense_key == ILLEGAL_REQUEST) {
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001185 sense_buf[7] = 10; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001187 sense_buf[15] = bit_pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /* Illegal parameter is in the parameter block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (sense_code == SENCODE_INVALID_CDB_FIELD)
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001190 sense_buf[15] |= 0xc0;/* Std sense key specific field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /* Illegal parameter is in the CDB block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 sense_buf[16] = field_pointer >> 8; /* MSB */
1193 sense_buf[17] = field_pointer; /* LSB */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001194 } else
1195 sense_buf[7] = 6; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001198static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1199{
1200 if (lba & 0xffffffff00000000LL) {
1201 int cid = scmd_id(cmd);
1202 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1203 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1204 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001205 set_sense(&dev->fsa_dev[cid].sense_data,
1206 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1207 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001208 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08001209 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1210 SCSI_SENSE_BUFFERSIZE));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001211 cmd->scsi_done(cmd);
1212 return 1;
1213 }
1214 return 0;
1215}
1216
1217static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1218{
1219 return 0;
1220}
1221
1222static void io_callback(void *context, struct fib * fibptr);
1223
1224static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1225{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301226 struct aac_dev *dev = fib->dev;
1227 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301228 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001229
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301230 aac_fib_init(fib);
Raghava Aditya Renukuntad1ef4da2017-02-02 15:53:17 -08001231 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1232 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1233 !dev->sync_mode) {
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301234 struct aac_raw_io2 *readcmd2;
1235 readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1236 memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1237 readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1238 readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001239 readcmd2->byteCount = cpu_to_le32(count *
1240 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301241 readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1242 readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301243 ret = aac_build_sgraw2(cmd, readcmd2,
1244 dev->scsi_host_ptr->sg_tablesize);
1245 if (ret < 0)
1246 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301247 command = ContainerRawIo2;
1248 fibsize = sizeof(struct aac_raw_io2) +
1249 ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1250 } else {
1251 struct aac_raw_io *readcmd;
1252 readcmd = (struct aac_raw_io *) fib_data(fib);
1253 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1254 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001255 readcmd->count = cpu_to_le32(count *
1256 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301257 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1258 readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1259 readcmd->bpTotal = 0;
1260 readcmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301261 ret = aac_build_sgraw(cmd, &readcmd->sg);
1262 if (ret < 0)
1263 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301264 command = ContainerRawIo;
1265 fibsize = sizeof(struct aac_raw_io) +
1266 ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1267 }
1268
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001269 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1270 /*
1271 * Now send the Fib to the adapter
1272 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301273 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001274 fib,
1275 fibsize,
1276 FsaNormal,
1277 0, 1,
1278 (fib_callback) io_callback,
1279 (void *) cmd);
1280}
1281
1282static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1283{
1284 u16 fibsize;
1285 struct aac_read64 *readcmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301286 long ret;
1287
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001288 aac_fib_init(fib);
1289 readcmd = (struct aac_read64 *) fib_data(fib);
1290 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1291 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1292 readcmd->sector_count = cpu_to_le16(count);
1293 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1294 readcmd->pad = 0;
1295 readcmd->flags = 0;
1296
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301297 ret = aac_build_sg64(cmd, &readcmd->sg);
1298 if (ret < 0)
1299 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001300 fibsize = sizeof(struct aac_read64) +
1301 ((le32_to_cpu(readcmd->sg.count) - 1) *
1302 sizeof (struct sgentry64));
1303 BUG_ON (fibsize > (fib->dev->max_fib_size -
1304 sizeof(struct aac_fibhdr)));
1305 /*
1306 * Now send the Fib to the adapter
1307 */
1308 return aac_fib_send(ContainerCommand64,
1309 fib,
1310 fibsize,
1311 FsaNormal,
1312 0, 1,
1313 (fib_callback) io_callback,
1314 (void *) cmd);
1315}
1316
1317static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1318{
1319 u16 fibsize;
1320 struct aac_read *readcmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001321 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301322 long ret;
1323
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001324 aac_fib_init(fib);
1325 readcmd = (struct aac_read *) fib_data(fib);
1326 readcmd->command = cpu_to_le32(VM_CtBlockRead);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001327 readcmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001328 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001329 readcmd->count = cpu_to_le32(count *
1330 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001331
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301332 ret = aac_build_sg(cmd, &readcmd->sg);
1333 if (ret < 0)
1334 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001335 fibsize = sizeof(struct aac_read) +
1336 ((le32_to_cpu(readcmd->sg.count) - 1) *
1337 sizeof (struct sgentry));
1338 BUG_ON (fibsize > (fib->dev->max_fib_size -
1339 sizeof(struct aac_fibhdr)));
1340 /*
1341 * Now send the Fib to the adapter
1342 */
1343 return aac_fib_send(ContainerCommand,
1344 fib,
1345 fibsize,
1346 FsaNormal,
1347 0, 1,
1348 (fib_callback) io_callback,
1349 (void *) cmd);
1350}
1351
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001352static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001353{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301354 struct aac_dev *dev = fib->dev;
1355 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301356 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001357
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301358 aac_fib_init(fib);
Raghava Aditya Renukuntad1ef4da2017-02-02 15:53:17 -08001359 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1360 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1361 !dev->sync_mode) {
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301362 struct aac_raw_io2 *writecmd2;
1363 writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1364 memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1365 writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1366 writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001367 writecmd2->byteCount = cpu_to_le32(count *
1368 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301369 writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1370 writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1371 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1372 cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1373 cpu_to_le16(RIO2_IO_TYPE_WRITE);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301374 ret = aac_build_sgraw2(cmd, writecmd2,
1375 dev->scsi_host_ptr->sg_tablesize);
1376 if (ret < 0)
1377 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301378 command = ContainerRawIo2;
1379 fibsize = sizeof(struct aac_raw_io2) +
1380 ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1381 } else {
1382 struct aac_raw_io *writecmd;
1383 writecmd = (struct aac_raw_io *) fib_data(fib);
1384 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1385 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001386 writecmd->count = cpu_to_le32(count *
1387 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301388 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1389 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1390 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1391 cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1392 cpu_to_le16(RIO_TYPE_WRITE);
1393 writecmd->bpTotal = 0;
1394 writecmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301395 ret = aac_build_sgraw(cmd, &writecmd->sg);
1396 if (ret < 0)
1397 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301398 command = ContainerRawIo;
1399 fibsize = sizeof(struct aac_raw_io) +
1400 ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1401 }
1402
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001403 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1404 /*
1405 * Now send the Fib to the adapter
1406 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301407 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001408 fib,
1409 fibsize,
1410 FsaNormal,
1411 0, 1,
1412 (fib_callback) io_callback,
1413 (void *) cmd);
1414}
1415
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001416static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001417{
1418 u16 fibsize;
1419 struct aac_write64 *writecmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301420 long ret;
1421
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001422 aac_fib_init(fib);
1423 writecmd = (struct aac_write64 *) fib_data(fib);
1424 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1425 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1426 writecmd->sector_count = cpu_to_le16(count);
1427 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1428 writecmd->pad = 0;
1429 writecmd->flags = 0;
1430
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301431 ret = aac_build_sg64(cmd, &writecmd->sg);
1432 if (ret < 0)
1433 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001434 fibsize = sizeof(struct aac_write64) +
1435 ((le32_to_cpu(writecmd->sg.count) - 1) *
1436 sizeof (struct sgentry64));
1437 BUG_ON (fibsize > (fib->dev->max_fib_size -
1438 sizeof(struct aac_fibhdr)));
1439 /*
1440 * Now send the Fib to the adapter
1441 */
1442 return aac_fib_send(ContainerCommand64,
1443 fib,
1444 fibsize,
1445 FsaNormal,
1446 0, 1,
1447 (fib_callback) io_callback,
1448 (void *) cmd);
1449}
1450
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001451static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001452{
1453 u16 fibsize;
1454 struct aac_write *writecmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001455 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301456 long ret;
1457
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001458 aac_fib_init(fib);
1459 writecmd = (struct aac_write *) fib_data(fib);
1460 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001461 writecmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001462 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001463 writecmd->count = cpu_to_le32(count *
1464 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001465 writecmd->sg.count = cpu_to_le32(1);
1466 /* ->stable is not used - it did mean which type of write */
1467
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301468 ret = aac_build_sg(cmd, &writecmd->sg);
1469 if (ret < 0)
1470 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001471 fibsize = sizeof(struct aac_write) +
1472 ((le32_to_cpu(writecmd->sg.count) - 1) *
1473 sizeof (struct sgentry));
1474 BUG_ON (fibsize > (fib->dev->max_fib_size -
1475 sizeof(struct aac_fibhdr)));
1476 /*
1477 * Now send the Fib to the adapter
1478 */
1479 return aac_fib_send(ContainerCommand,
1480 fib,
1481 fibsize,
1482 FsaNormal,
1483 0, 1,
1484 (fib_callback) io_callback,
1485 (void *) cmd);
1486}
1487
1488static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1489{
1490 struct aac_srb * srbcmd;
1491 u32 flag;
1492 u32 timeout;
1493
1494 aac_fib_init(fib);
1495 switch(cmd->sc_data_direction){
1496 case DMA_TO_DEVICE:
1497 flag = SRB_DataOut;
1498 break;
1499 case DMA_BIDIRECTIONAL:
1500 flag = SRB_DataIn | SRB_DataOut;
1501 break;
1502 case DMA_FROM_DEVICE:
1503 flag = SRB_DataIn;
1504 break;
1505 case DMA_NONE:
1506 default: /* shuts up some versions of gcc */
1507 flag = SRB_NoDataXfer;
1508 break;
1509 }
1510
1511 srbcmd = (struct aac_srb*) fib_data(fib);
1512 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1513 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1514 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1515 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1516 srbcmd->flags = cpu_to_le32(flag);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001517 timeout = cmd->request->timeout/HZ;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001518 if (timeout == 0)
1519 timeout = 1;
1520 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1521 srbcmd->retry_limit = 0; /* Obsolete parameter */
1522 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1523 return srbcmd;
1524}
1525
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08001526static struct aac_hba_cmd_req *aac_construct_hbacmd(struct fib *fib,
1527 struct scsi_cmnd *cmd)
1528{
1529 struct aac_hba_cmd_req *hbacmd;
1530 struct aac_dev *dev;
1531 int bus, target;
1532 u64 address;
1533
1534 dev = (struct aac_dev *)cmd->device->host->hostdata;
1535
1536 hbacmd = (struct aac_hba_cmd_req *)fib->hw_fib_va;
1537 memset(hbacmd, 0, 96); /* sizeof(*hbacmd) is not necessary */
1538 /* iu_type is a parameter of aac_hba_send */
1539 switch (cmd->sc_data_direction) {
1540 case DMA_TO_DEVICE:
1541 hbacmd->byte1 = 2;
1542 break;
1543 case DMA_FROM_DEVICE:
1544 case DMA_BIDIRECTIONAL:
1545 hbacmd->byte1 = 1;
1546 break;
1547 case DMA_NONE:
1548 default:
1549 break;
1550 }
1551 hbacmd->lun[1] = cpu_to_le32(cmd->device->lun);
1552
1553 bus = aac_logical_to_phys(scmd_channel(cmd));
1554 target = scmd_id(cmd);
1555 hbacmd->it_nexus = dev->hba_map[bus][target].rmw_nexus;
1556
1557 /* we fill in reply_qid later in aac_src_deliver_message */
1558 /* we fill in iu_type, request_id later in aac_hba_send */
1559 /* we fill in emb_data_desc_count later in aac_build_sghba */
1560
1561 memcpy(hbacmd->cdb, cmd->cmnd, cmd->cmd_len);
1562 hbacmd->data_length = cpu_to_le32(scsi_bufflen(cmd));
1563
1564 address = (u64)fib->hw_error_pa;
1565 hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
1566 hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
1567 hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
1568
1569 return hbacmd;
1570}
1571
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001572static void aac_srb_callback(void *context, struct fib * fibptr);
1573
1574static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1575{
1576 u16 fibsize;
1577 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301578 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001579
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301580 ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1581 if (ret < 0)
1582 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001583 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001584
1585 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1586 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1587 /*
1588 * Build Scatter/Gather list
1589 */
1590 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1591 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1592 sizeof (struct sgentry64));
1593 BUG_ON (fibsize > (fib->dev->max_fib_size -
1594 sizeof(struct aac_fibhdr)));
1595
1596 /*
1597 * Now send the Fib to the adapter
1598 */
1599 return aac_fib_send(ScsiPortCommand64, fib,
1600 fibsize, FsaNormal, 0, 1,
1601 (fib_callback) aac_srb_callback,
1602 (void *) cmd);
1603}
1604
1605static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1606{
1607 u16 fibsize;
1608 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301609 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001610
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301611 ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1612 if (ret < 0)
1613 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001614 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001615
1616 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1617 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1618 /*
1619 * Build Scatter/Gather list
1620 */
1621 fibsize = sizeof (struct aac_srb) +
1622 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1623 sizeof (struct sgentry));
1624 BUG_ON (fibsize > (fib->dev->max_fib_size -
1625 sizeof(struct aac_fibhdr)));
1626
1627 /*
1628 * Now send the Fib to the adapter
1629 */
1630 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1631 (fib_callback) aac_srb_callback, (void *) cmd);
1632}
1633
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001634static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1635{
Leubner, Achimd8e965072009-04-01 07:16:08 -07001636 if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1637 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001638 return FAILED;
1639 return aac_scsi_32(fib, cmd);
1640}
1641
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08001642static int aac_adapter_hba(struct fib *fib, struct scsi_cmnd *cmd)
1643{
1644 struct aac_hba_cmd_req *hbacmd = aac_construct_hbacmd(fib, cmd);
1645 struct aac_dev *dev;
1646 long ret;
1647
1648 dev = (struct aac_dev *)cmd->device->host->hostdata;
1649
1650 ret = aac_build_sghba(cmd, hbacmd,
1651 dev->scsi_host_ptr->sg_tablesize, (u64)fib->hw_sgl_pa);
1652 if (ret < 0)
1653 return ret;
1654
1655 /*
1656 * Now send the HBA command to the adapter
1657 */
1658 fib->hbacmd_size = 64 + le32_to_cpu(hbacmd->emb_data_desc_count) *
1659 sizeof(struct aac_hba_sgl);
1660
1661 return aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, fib,
1662 (fib_callback) aac_hba_callback,
1663 (void *) cmd);
1664}
1665
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001666int aac_issue_bmic_identify(struct aac_dev *dev, u32 bus, u32 target)
1667{
1668 struct fib *fibptr;
1669 struct aac_srb *srbcmd;
1670 struct sgmap64 *sg64;
1671 struct aac_ciss_identify_pd *identify_resp;
1672 dma_addr_t addr;
1673 u32 vbus, vid;
1674 u16 fibsize, datasize;
1675 int rcode = -ENOMEM;
1676
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08001677
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001678 fibptr = aac_fib_alloc(dev);
1679 if (!fibptr)
1680 goto out;
1681
1682 fibsize = sizeof(struct aac_srb) -
1683 sizeof(struct sgentry) + sizeof(struct sgentry64);
1684 datasize = sizeof(struct aac_ciss_identify_pd);
1685
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +05301686 identify_resp = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
1687 GFP_KERNEL);
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001688 if (!identify_resp)
1689 goto fib_free_ptr;
1690
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08001691 vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus);
1692 vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target);
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001693
1694 aac_fib_init(fibptr);
1695
1696 srbcmd = (struct aac_srb *) fib_data(fibptr);
1697 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1698 srbcmd->channel = cpu_to_le32(vbus);
1699 srbcmd->id = cpu_to_le32(vid);
1700 srbcmd->lun = 0;
1701 srbcmd->flags = cpu_to_le32(SRB_DataIn);
1702 srbcmd->timeout = cpu_to_le32(10);
1703 srbcmd->retry_limit = 0;
1704 srbcmd->cdb_size = cpu_to_le32(12);
1705 srbcmd->count = cpu_to_le32(datasize);
1706
1707 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1708 srbcmd->cdb[0] = 0x26;
1709 srbcmd->cdb[2] = (u8)((AAC_MAX_LUN + target) & 0x00FF);
1710 srbcmd->cdb[6] = CISS_IDENTIFY_PHYSICAL_DEVICE;
1711
1712 sg64 = (struct sgmap64 *)&srbcmd->sg;
1713 sg64->count = cpu_to_le32(1);
1714 sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
1715 sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
1716 sg64->sg[0].count = cpu_to_le32(datasize);
1717
1718 rcode = aac_fib_send(ScsiPortCommand64,
1719 fibptr, fibsize, FsaNormal, 1, 1, NULL, NULL);
1720
1721 if (identify_resp->current_queue_depth_limit <= 0 ||
1722 identify_resp->current_queue_depth_limit > 32)
1723 dev->hba_map[bus][target].qd_limit = 32;
1724 else
1725 dev->hba_map[bus][target].qd_limit =
1726 identify_resp->current_queue_depth_limit;
1727
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +05301728 dma_free_coherent(&dev->pdev->dev, datasize, identify_resp, addr);
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001729
1730 aac_fib_complete(fibptr);
1731
1732fib_free_ptr:
1733 aac_fib_free(fibptr);
1734out:
1735 return rcode;
1736}
1737
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001738/**
1739 * aac_update hba_map()- update current hba map with data from FW
1740 * @dev: aac_dev structure
1741 * @phys_luns: FW information from report phys luns
1742 *
1743 * Update our hba map with the information gathered from the FW
1744 */
1745void aac_update_hba_map(struct aac_dev *dev,
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001746 struct aac_ciss_phys_luns_resp *phys_luns, int rescan)
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001747{
1748 /* ok and extended reporting */
1749 u32 lun_count, nexus;
1750 u32 i, bus, target;
1751 u8 expose_flag, attribs;
1752 u8 devtype;
1753
1754 lun_count = ((phys_luns->list_length[0] << 24)
1755 + (phys_luns->list_length[1] << 16)
1756 + (phys_luns->list_length[2] << 8)
1757 + (phys_luns->list_length[3])) / 24;
1758
1759 for (i = 0; i < lun_count; ++i) {
1760
1761 bus = phys_luns->lun[i].level2[1] & 0x3f;
1762 target = phys_luns->lun[i].level2[0];
1763 expose_flag = phys_luns->lun[i].bus >> 6;
1764 attribs = phys_luns->lun[i].node_ident[9];
1765 nexus = *((u32 *) &phys_luns->lun[i].node_ident[12]);
1766
1767 if (bus >= AAC_MAX_BUSES || target >= AAC_MAX_TARGETS)
1768 continue;
1769
1770 dev->hba_map[bus][target].expose = expose_flag;
1771
1772 if (expose_flag != 0) {
1773 devtype = AAC_DEVTYPE_RAID_MEMBER;
1774 goto update_devtype;
1775 }
1776
1777 if (nexus != 0 && (attribs & 8)) {
1778 devtype = AAC_DEVTYPE_NATIVE_RAW;
1779 dev->hba_map[bus][target].rmw_nexus =
1780 nexus;
1781 } else
1782 devtype = AAC_DEVTYPE_ARC_RAW;
1783
1784 if (devtype != AAC_DEVTYPE_NATIVE_RAW)
1785 goto update_devtype;
1786
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001787 if (aac_issue_bmic_identify(dev, bus, target) < 0)
1788 dev->hba_map[bus][target].qd_limit = 32;
1789
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001790update_devtype:
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001791 if (rescan == AAC_INIT)
1792 dev->hba_map[bus][target].devtype = devtype;
1793 else
1794 dev->hba_map[bus][target].new_devtype = devtype;
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001795 }
1796}
1797
1798/**
1799 * aac_report_phys_luns() Process topology change
1800 * @dev: aac_dev structure
1801 * @fibptr: fib pointer
1802 *
1803 * Execute a CISS REPORT PHYS LUNS and process the results into
1804 * the current hba_map.
1805 */
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001806int aac_report_phys_luns(struct aac_dev *dev, struct fib *fibptr, int rescan)
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001807{
1808 int fibsize, datasize;
1809 struct aac_ciss_phys_luns_resp *phys_luns;
1810 struct aac_srb *srbcmd;
1811 struct sgmap64 *sg64;
1812 dma_addr_t addr;
1813 u32 vbus, vid;
Colin Ian King76291462017-02-07 11:51:29 +00001814 int rcode = 0;
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001815
1816 /* Thor SA Firmware -> CISS_REPORT_PHYSICAL_LUNS */
1817 fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry)
1818 + sizeof(struct sgentry64);
1819 datasize = sizeof(struct aac_ciss_phys_luns_resp)
1820 + (AAC_MAX_TARGETS - 1) * sizeof(struct _ciss_lun);
1821
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +05301822 phys_luns = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr,
1823 GFP_KERNEL);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001824 if (phys_luns == NULL) {
1825 rcode = -ENOMEM;
1826 goto err_out;
1827 }
1828
1829 vbus = (u32) le16_to_cpu(
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08001830 dev->supplement_adapter_info.virt_device_bus);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001831 vid = (u32) le16_to_cpu(
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08001832 dev->supplement_adapter_info.virt_device_target);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001833
1834 aac_fib_init(fibptr);
1835
1836 srbcmd = (struct aac_srb *) fib_data(fibptr);
1837 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1838 srbcmd->channel = cpu_to_le32(vbus);
1839 srbcmd->id = cpu_to_le32(vid);
1840 srbcmd->lun = 0;
1841 srbcmd->flags = cpu_to_le32(SRB_DataIn);
1842 srbcmd->timeout = cpu_to_le32(10);
1843 srbcmd->retry_limit = 0;
1844 srbcmd->cdb_size = cpu_to_le32(12);
1845 srbcmd->count = cpu_to_le32(datasize);
1846
1847 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1848 srbcmd->cdb[0] = CISS_REPORT_PHYSICAL_LUNS;
1849 srbcmd->cdb[1] = 2; /* extended reporting */
1850 srbcmd->cdb[8] = (u8)(datasize >> 8);
1851 srbcmd->cdb[9] = (u8)(datasize);
1852
1853 sg64 = (struct sgmap64 *) &srbcmd->sg;
1854 sg64->count = cpu_to_le32(1);
1855 sg64->sg[0].addr[1] = cpu_to_le32(upper_32_bits(addr));
1856 sg64->sg[0].addr[0] = cpu_to_le32(lower_32_bits(addr));
1857 sg64->sg[0].count = cpu_to_le32(datasize);
1858
1859 rcode = aac_fib_send(ScsiPortCommand64, fibptr, fibsize,
1860 FsaNormal, 1, 1, NULL, NULL);
1861
1862 /* analyse data */
1863 if (rcode >= 0 && phys_luns->resp_flag == 2) {
1864 /* ok and extended reporting */
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001865 aac_update_hba_map(dev, phys_luns, rescan);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001866 }
1867
Mahesh Rajashekharaf4819732017-04-05 16:14:16 +05301868 dma_free_coherent(&dev->pdev->dev, datasize, phys_luns, addr);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001869err_out:
1870 return rcode;
1871}
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873int aac_get_adapter_info(struct aac_dev* dev)
1874{
1875 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int rcode;
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001877 u32 tmp, bus, target;
Mark Haverkamp84971732005-06-20 11:55:24 -07001878 struct aac_adapter_info *info;
1879 struct aac_bus_info *command;
1880 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001881
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001882 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return -ENOMEM;
1884
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001885 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001886 info = (struct aac_adapter_info *) fib_data(fibptr);
1887 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001889 rcode = aac_fib_send(RequestAdapterInfo,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001890 fibptr,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001891 sizeof(*info),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001892 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001893 -1, 1, /* First `interrupt' command uses special wait */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001894 NULL,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001895 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001897 if (rcode < 0) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301898 /* FIB should be freed only after
1899 * getting the response from the F/W */
1900 if (rcode != -ERESTARTSYS) {
1901 aac_fib_complete(fibptr);
1902 aac_fib_free(fibptr);
1903 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001904 return rcode;
1905 }
1906 memcpy(&dev->adapter_info, info, sizeof(*info));
1907
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08001908 dev->supplement_adapter_info.virt_device_bus = 0xffff;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001909 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001910 struct aac_supplement_adapter_info * sinfo;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001911
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001912 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001913
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001914 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001915
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001916 memset(sinfo,0,sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001917
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001918 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001919 fibptr,
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001920 sizeof(*sinfo),
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001921 FsaNormal,
1922 1, 1,
1923 NULL,
1924 NULL);
1925
1926 if (rcode >= 0)
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001927 memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301928 if (rcode == -ERESTARTSYS) {
1929 fibptr = aac_fib_alloc(dev);
1930 if (!fibptr)
1931 return -ENOMEM;
1932 }
1933
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001936 /* reset all previous mapped devices (i.e. for init. after IOP_RESET) */
1937 for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001938 for (target = 0; target < AAC_MAX_TARGETS; target++) {
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001939 dev->hba_map[bus][target].devtype = 0;
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001940 dev->hba_map[bus][target].qd_limit = 0;
1941 }
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001942 }
Mark Haverkamp84971732005-06-20 11:55:24 -07001943
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001944 /*
1945 * GetBusInfo
Mark Haverkamp84971732005-06-20 11:55:24 -07001946 */
1947
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001948 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001949
1950 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1951
1952 memset(bus_info, 0, sizeof(*bus_info));
1953
1954 command = (struct aac_bus_info *)bus_info;
1955
1956 command->Command = cpu_to_le32(VM_Ioctl);
1957 command->ObjType = cpu_to_le32(FT_DRIVE);
1958 command->MethodId = cpu_to_le32(1);
1959 command->CtlCmd = cpu_to_le32(GetBusInfo);
1960
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001961 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001962 fibptr,
1963 sizeof (*bus_info),
1964 FsaNormal,
1965 1, 1,
1966 NULL, NULL);
1967
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001968 /* reasoned default */
1969 dev->maximum_num_physicals = 16;
Mark Haverkamp84971732005-06-20 11:55:24 -07001970 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1971 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1972 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1973 }
1974
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001975 if (!dev->sync_mode && dev->sa_firmware &&
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08001976 dev->supplement_adapter_info.virt_device_bus != 0xffff) {
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001977 /* Thor SA Firmware -> CISS_REPORT_PHYSICAL_LUNS */
Raghava Aditya Renukunta6223a392017-02-02 15:53:28 -08001978 rcode = aac_report_phys_luns(dev, fibptr, AAC_INIT);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001979 }
1980
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001981 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001982 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001983 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1984 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001985 dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 dev->id,
1987 tmp>>24,
1988 (tmp>>16)&0xff,
1989 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001990 le32_to_cpu(dev->adapter_info.kernelbuild),
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08001991 (int)sizeof(dev->supplement_adapter_info.build_date),
1992 dev->supplement_adapter_info.build_date);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001993 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1994 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 dev->name, dev->id,
1996 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1997 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001998 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1999 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 dev->name, dev->id,
2001 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2002 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04002003 buffer[0] = '\0';
Tony Jonesee959b02008-02-22 00:13:36 +01002004 if (aac_get_serial_number(
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04002005 shost_to_class(dev->scsi_host_ptr), buffer))
2006 printk(KERN_INFO "%s%d: serial %s",
2007 dev->name, dev->id, buffer);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002008 if (dev->supplement_adapter_info.vpd_info.tsid[0]) {
Salyzyn, Marka45c8632007-03-28 13:44:54 -04002009 printk(KERN_INFO "%s%d: TSID %.*s\n",
2010 dev->name, dev->id,
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002011 (int)sizeof(dev->supplement_adapter_info
2012 .vpd_info.tsid),
2013 dev->supplement_adapter_info.vpd_info.tsid);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04002014 }
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002015 if (!aac_check_reset || ((aac_check_reset == 1) &&
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002016 (dev->supplement_adapter_info.supported_options2 &
Salyzyn, Marka3940da2008-01-08 12:48:25 -08002017 AAC_OPTION_IGNORE_RESET))) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002018 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
2019 dev->name, dev->id);
2020 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002023 dev->cache_protected = 0;
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002024 dev->jbod = ((dev->supplement_adapter_info.feature_bits &
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08002025 AAC_FEATURE_JBOD) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 dev->nondasd_support = 0;
2027 dev->raid_scsi_mode = 0;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002028 if(dev->adapter_info.options & AAC_OPT_NONDASD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 dev->nondasd_support = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 /*
2032 * If the firmware supports ROMB RAID/SCSI mode and we are currently
2033 * in RAID/SCSI mode, set the flag. For now if in this mode we will
2034 * force nondasd support on. If we decide to allow the non-dasd flag
2035 * additional changes changes will have to be made to support
2036 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
2037 * changed to support the new dev->raid_scsi_mode flag instead of
2038 * leaching off of the dev->nondasd_support flag. Also in linit.c the
2039 * function aac_detect will have to be modified where it sets up the
2040 * max number of channels based on the aac->nondasd_support flag only.
2041 */
2042 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
2043 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
2044 dev->nondasd_support = 1;
2045 dev->raid_scsi_mode = 1;
2046 }
2047 if (dev->raid_scsi_mode != 0)
2048 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
2049 dev->name, dev->id);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002050
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002051 if (nondasd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 dev->nondasd_support = (nondasd!=0);
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002053 if (dev->nondasd_support && !dev->in_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Yang Hongyange9304382009-04-13 14:40:14 -07002056 if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
Leubner, Achimd8e965072009-04-01 07:16:08 -07002057 dev->needs_dac = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 dev->dac_support = 0;
Leubner, Achimd8e965072009-04-01 07:16:08 -07002059 if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
2060 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002061 if (!dev->in_reset)
2062 printk(KERN_INFO "%s%d: 64bit support enabled.\n",
2063 dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 dev->dac_support = 1;
2065 }
2066
2067 if(dacmode != -1) {
2068 dev->dac_support = (dacmode!=0);
2069 }
Leubner, Achimd8e965072009-04-01 07:16:08 -07002070
2071 /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
2072 if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
2073 & AAC_QUIRK_SCSI_32)) {
2074 dev->nondasd_support = 0;
2075 dev->jbod = 0;
2076 expose_physicals = 0;
2077 }
2078
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07002079 if (dev->dac_support) {
2080 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002081 if (!dev->in_reset)
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07002082 dev_info(&dev->pdev->dev, "64 Bit DAC enabled\n");
2083 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
2084 dev_info(&dev->pdev->dev, "DMA mask set failed, 64 Bit DAC disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 dev->dac_support = 0;
2086 } else {
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07002087 dev_info(&dev->pdev->dev, "No suitable DMA available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 rcode = -ENOMEM;
2089 }
2090 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002091 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002092 * Deal with configuring for the individualized limits of each packet
2093 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002094 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002095 dev->a_ops.adapter_scsi = (dev->dac_support)
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08002096 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
2097 ? aac_scsi_32_64
2098 : aac_scsi_64)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002099 : aac_scsi_32;
2100 if (dev->raw_io_interface) {
2101 dev->a_ops.adapter_bounds = (dev->raw_io_64)
2102 ? aac_bounds_64
2103 : aac_bounds_32;
2104 dev->a_ops.adapter_read = aac_read_raw_io;
2105 dev->a_ops.adapter_write = aac_write_raw_io;
2106 } else {
2107 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002108 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002109 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07002110 sizeof(struct aac_write) + sizeof(struct sgentry)) /
2111 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002112 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002113 dev->a_ops.adapter_read = aac_read_block64;
2114 dev->a_ops.adapter_write = aac_write_block64;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002115 /*
2116 * 38 scatter gather elements
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002117 */
2118 dev->scsi_host_ptr->sg_tablesize =
2119 (dev->max_fib_size -
2120 sizeof(struct aac_fibhdr) -
2121 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07002122 sizeof(struct sgentry64)) /
2123 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002124 } else {
2125 dev->a_ops.adapter_read = aac_read_block;
2126 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002127 }
2128 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07002129 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002130 /*
2131 * Worst case size that could cause sg overflow when
2132 * we break up SG elements that are larger than 64KB.
2133 * Would be nice if we could tell the SCSI layer what
2134 * the maximum SG element size can be. Worst case is
2135 * (sg_tablesize-1) 4KB elements with one 64KB
2136 * element.
2137 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
2138 */
2139 dev->scsi_host_ptr->max_sectors =
2140 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
2141 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002142 }
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08002143 if (!dev->sync_mode && dev->sa_firmware &&
2144 dev->scsi_host_ptr->sg_tablesize > HBA_MAX_SG_SEPARATE)
2145 dev->scsi_host_ptr->sg_tablesize = dev->sg_tablesize =
2146 HBA_MAX_SG_SEPARATE;
2147
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05302148 /* FIB should be freed only after getting the response from the F/W */
2149 if (rcode != -ERESTARTSYS) {
2150 aac_fib_complete(fibptr);
2151 aac_fib_free(fibptr);
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 return rcode;
2155}
2156
2157
Mark Haverkampe53cb352005-08-03 15:39:09 -07002158static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 struct aac_dev *dev;
2161 struct aac_read_reply *readreply;
2162 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 u32 cid;
2164
2165 scsicmd = (struct scsi_cmnd *) context;
2166
Mark Haverkamp03d44332007-03-15 10:27:45 -07002167 if (!aac_valid_context(scsicmd, fibptr))
2168 return;
2169
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002170 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08002171 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002173 if (nblank(dprintk(x))) {
2174 u64 lba;
2175 switch (scsicmd->cmnd[0]) {
2176 case WRITE_6:
2177 case READ_6:
2178 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2179 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2180 break;
2181 case WRITE_16:
2182 case READ_16:
2183 lba = ((u64)scsicmd->cmnd[2] << 56) |
2184 ((u64)scsicmd->cmnd[3] << 48) |
2185 ((u64)scsicmd->cmnd[4] << 40) |
2186 ((u64)scsicmd->cmnd[5] << 32) |
2187 ((u64)scsicmd->cmnd[6] << 24) |
2188 (scsicmd->cmnd[7] << 16) |
2189 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2190 break;
2191 case WRITE_12:
2192 case READ_12:
2193 lba = ((u64)scsicmd->cmnd[2] << 24) |
2194 (scsicmd->cmnd[3] << 16) |
2195 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2196 break;
2197 default:
2198 lba = ((u64)scsicmd->cmnd[2] << 24) |
2199 (scsicmd->cmnd[3] << 16) |
2200 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2201 break;
2202 }
2203 printk(KERN_DEBUG
2204 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
2205 smp_processor_id(), (unsigned long long)lba, jiffies);
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002208 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002209
2210 scsi_dma_unmap(scsicmd);
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 readreply = (struct aac_read_reply *)fib_data(fibptr);
Mark Salyzyn655d7222008-04-30 16:03:42 -04002213 switch (le32_to_cpu(readreply->status)) {
2214 case ST_OK:
2215 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2216 SAM_STAT_GOOD;
2217 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
2218 break;
2219 case ST_NOT_READY:
2220 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2221 SAM_STAT_CHECK_CONDITION;
2222 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
2223 SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
2224 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2225 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2226 SCSI_SENSE_BUFFERSIZE));
2227 break;
Raghava Aditya Renukuntaf956a662017-02-02 15:53:23 -08002228 case ST_MEDERR:
2229 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2230 SAM_STAT_CHECK_CONDITION;
2231 set_sense(&dev->fsa_dev[cid].sense_data, MEDIUM_ERROR,
2232 SENCODE_UNRECOVERED_READ_ERROR, ASENCODE_NO_SENSE, 0, 0);
2233 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2234 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2235 SCSI_SENSE_BUFFERSIZE));
2236 break;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002237 default:
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002238#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07002239 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002240 le32_to_cpu(readreply->status));
2241#endif
Mark Salyzyn655d7222008-04-30 16:03:42 -04002242 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2243 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002244 set_sense(&dev->fsa_dev[cid].sense_data,
2245 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2246 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002248 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2249 SCSI_SENSE_BUFFERSIZE));
Mark Salyzyn655d7222008-04-30 16:03:42 -04002250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002252 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002254 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002257static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002259 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 u32 count;
2261 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 struct aac_dev *dev;
2263 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002264 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2267 /*
2268 * Get block address and transfer length
2269 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002270 switch (scsicmd->cmnd[0]) {
2271 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002272 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002274 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002275 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 count = scsicmd->cmnd[4];
2277
2278 if (count == 0)
2279 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002280 break;
2281 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002282 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002283
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002284 lba = ((u64)scsicmd->cmnd[2] << 56) |
2285 ((u64)scsicmd->cmnd[3] << 48) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002286 ((u64)scsicmd->cmnd[4] << 40) |
2287 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002288 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002289 (scsicmd->cmnd[7] << 16) |
2290 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002291 count = (scsicmd->cmnd[10] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002292 (scsicmd->cmnd[11] << 16) |
2293 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2294 break;
2295 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002296 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002297
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002298 lba = ((u64)scsicmd->cmnd[2] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002299 (scsicmd->cmnd[3] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002300 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2301 count = (scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002302 (scsicmd->cmnd[7] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002303 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002304 break;
2305 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002306 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002308 lba = ((u64)scsicmd->cmnd[2] << 24) |
2309 (scsicmd->cmnd[3] << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002310 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002312 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002314
2315 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2316 cid = scmd_id(scsicmd);
2317 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2318 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2319 SAM_STAT_CHECK_CONDITION;
2320 set_sense(&dev->fsa_dev[cid].sense_data,
2321 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2322 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2323 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2324 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2325 SCSI_SENSE_BUFFERSIZE));
2326 scsicmd->scsi_done(scsicmd);
2327 return 1;
2328 }
2329
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002330 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002331 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002332 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /*
2335 * Alocate and initialize a Fib
2336 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002337 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002339 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 /*
2342 * Check that the command queued to the controller
2343 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002344 if (status == -EINPROGRESS) {
2345 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002347 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002348
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002349 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /*
2351 * For some reason, the Fib didn't queue, return QUEUE_FULL
2352 */
2353 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002354 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002355 aac_fib_complete(cmd_fibcontext);
2356 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return 0;
2358}
2359
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002360static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002362 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002364 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct aac_dev *dev;
2367 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002368 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2371 /*
2372 * Get block address and transfer length
2373 */
2374 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
2375 {
2376 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2377 count = scsicmd->cmnd[4];
2378 if (count == 0)
2379 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002380 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002381 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002382 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002383
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002384 lba = ((u64)scsicmd->cmnd[2] << 56) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002385 ((u64)scsicmd->cmnd[3] << 48) |
2386 ((u64)scsicmd->cmnd[4] << 40) |
2387 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002388 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002389 (scsicmd->cmnd[7] << 16) |
2390 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2391 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2392 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002393 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002394 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002395 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002396
2397 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2398 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2399 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2400 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002401 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002403 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002404 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002406 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002408
2409 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2410 cid = scmd_id(scsicmd);
2411 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2412 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2413 SAM_STAT_CHECK_CONDITION;
2414 set_sense(&dev->fsa_dev[cid].sense_data,
2415 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2416 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2417 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2418 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2419 SCSI_SENSE_BUFFERSIZE));
2420 scsicmd->scsi_done(scsicmd);
2421 return 1;
2422 }
2423
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002424 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002426 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /*
2429 * Allocate and initialize a Fib then setup a BlockWrite command
2430 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002431 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002433 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 /*
2436 * Check that the command queued to the controller
2437 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002438 if (status == -EINPROGRESS) {
2439 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return 0;
2441 }
2442
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002443 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /*
2445 * For some reason, the Fib didn't queue, return QUEUE_FULL
2446 */
2447 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002448 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002450 aac_fib_complete(cmd_fibcontext);
2451 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 return 0;
2453}
2454
2455static void synchronize_callback(void *context, struct fib *fibptr)
2456{
2457 struct aac_synchronize_reply *synchronizereply;
2458 struct scsi_cmnd *cmd;
2459
2460 cmd = context;
2461
Mark Haverkamp03d44332007-03-15 10:27:45 -07002462 if (!aac_valid_context(cmd, fibptr))
2463 return;
2464
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002465 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 smp_processor_id(), jiffies));
2467 BUG_ON(fibptr == NULL);
2468
2469
2470 synchronizereply = fib_data(fibptr);
2471 if (le32_to_cpu(synchronizereply->status) == CT_OK)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002472 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2474 else {
2475 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002476 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08002477 u32 cid = sdev_id(sdev);
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002478 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 "synchronize_callback: synchronize failed, status = %d\n",
2480 le32_to_cpu(synchronizereply->status));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002481 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002483 set_sense(&dev->fsa_dev[cid].sense_data,
2484 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2485 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002487 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2488 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002491 aac_fib_complete(fibptr);
2492 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002493 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002496static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 int status;
2499 struct fib *cmd_fibcontext;
2500 struct aac_synchronize *synchronizecmd;
2501 struct scsi_cmnd *cmd;
2502 struct scsi_device *sdev = scsicmd->device;
2503 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002504 struct aac_dev *aac;
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002505 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2506 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2507 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 unsigned long flags;
2509
2510 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002511 * Wait for all outstanding queued commands to complete to this
2512 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 */
2514 spin_lock_irqsave(&sdev->list_lock, flags);
2515 list_for_each_entry(cmd, &sdev->cmd_list, list)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002516 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2517 u64 cmnd_lba;
2518 u32 cmnd_count;
2519
2520 if (cmd->cmnd[0] == WRITE_6) {
2521 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2522 (cmd->cmnd[2] << 8) |
2523 cmd->cmnd[3];
2524 cmnd_count = cmd->cmnd[4];
2525 if (cmnd_count == 0)
2526 cmnd_count = 256;
2527 } else if (cmd->cmnd[0] == WRITE_16) {
2528 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2529 ((u64)cmd->cmnd[3] << 48) |
2530 ((u64)cmd->cmnd[4] << 40) |
2531 ((u64)cmd->cmnd[5] << 32) |
2532 ((u64)cmd->cmnd[6] << 24) |
2533 (cmd->cmnd[7] << 16) |
2534 (cmd->cmnd[8] << 8) |
2535 cmd->cmnd[9];
2536 cmnd_count = (cmd->cmnd[10] << 24) |
2537 (cmd->cmnd[11] << 16) |
2538 (cmd->cmnd[12] << 8) |
2539 cmd->cmnd[13];
2540 } else if (cmd->cmnd[0] == WRITE_12) {
2541 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2542 (cmd->cmnd[3] << 16) |
2543 (cmd->cmnd[4] << 8) |
2544 cmd->cmnd[5];
2545 cmnd_count = (cmd->cmnd[6] << 24) |
2546 (cmd->cmnd[7] << 16) |
2547 (cmd->cmnd[8] << 8) |
2548 cmd->cmnd[9];
2549 } else if (cmd->cmnd[0] == WRITE_10) {
2550 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2551 (cmd->cmnd[3] << 16) |
2552 (cmd->cmnd[4] << 8) |
2553 cmd->cmnd[5];
2554 cmnd_count = (cmd->cmnd[7] << 8) |
2555 cmd->cmnd[8];
2556 } else
2557 continue;
2558 if (((cmnd_lba + cmnd_count) < lba) ||
2559 (count && ((lba + count) < cmnd_lba)))
2560 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 ++active;
2562 break;
2563 }
2564
2565 spin_unlock_irqrestore(&sdev->list_lock, flags);
2566
2567 /*
2568 * Yield the processor (requeue for later)
2569 */
2570 if (active)
2571 return SCSI_MLQUEUE_DEVICE_BUSY;
2572
Salyzyn, Markf8583172007-10-30 15:50:49 -04002573 aac = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002574 if (aac->in_reset)
2575 return SCSI_MLQUEUE_HOST_BUSY;
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002578 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002580 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return SCSI_MLQUEUE_HOST_BUSY;
2582
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002583 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 synchronizecmd = fib_data(cmd_fibcontext);
2586 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2587 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002588 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002589 synchronizecmd->count =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2591
2592 /*
2593 * Now send the Fib to the adapter
2594 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002595 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 cmd_fibcontext,
2597 sizeof(struct aac_synchronize),
2598 FsaNormal,
2599 0, 1,
2600 (fib_callback)synchronize_callback,
2601 (void *)scsicmd);
2602
2603 /*
2604 * Check that the command queued to the controller
2605 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002606 if (status == -EINPROGRESS) {
2607 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002611 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002612 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2613 aac_fib_complete(cmd_fibcontext);
2614 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return SCSI_MLQUEUE_HOST_BUSY;
2616}
2617
Mark Salyzyn655d7222008-04-30 16:03:42 -04002618static void aac_start_stop_callback(void *context, struct fib *fibptr)
2619{
2620 struct scsi_cmnd *scsicmd = context;
2621
2622 if (!aac_valid_context(scsicmd, fibptr))
2623 return;
2624
2625 BUG_ON(fibptr == NULL);
2626
2627 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2628
2629 aac_fib_complete(fibptr);
2630 aac_fib_free(fibptr);
2631 scsicmd->scsi_done(scsicmd);
2632}
2633
2634static int aac_start_stop(struct scsi_cmnd *scsicmd)
2635{
2636 int status;
2637 struct fib *cmd_fibcontext;
2638 struct aac_power_management *pmcmd;
2639 struct scsi_device *sdev = scsicmd->device;
2640 struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2641
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002642 if (!(aac->supplement_adapter_info.supported_options2 &
Mark Salyzyn655d7222008-04-30 16:03:42 -04002643 AAC_OPTION_POWER_MANAGEMENT)) {
2644 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2645 SAM_STAT_GOOD;
2646 scsicmd->scsi_done(scsicmd);
2647 return 0;
2648 }
2649
2650 if (aac->in_reset)
2651 return SCSI_MLQUEUE_HOST_BUSY;
2652
2653 /*
2654 * Allocate and initialize a Fib
2655 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002656 cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
Mark Salyzyn655d7222008-04-30 16:03:42 -04002657
2658 aac_fib_init(cmd_fibcontext);
2659
2660 pmcmd = fib_data(cmd_fibcontext);
2661 pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2662 pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2663 /* Eject bit ignored, not relevant */
2664 pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2665 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2666 pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2667 pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2668 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2669
2670 /*
2671 * Now send the Fib to the adapter
2672 */
2673 status = aac_fib_send(ContainerCommand,
2674 cmd_fibcontext,
2675 sizeof(struct aac_power_management),
2676 FsaNormal,
2677 0, 1,
2678 (fib_callback)aac_start_stop_callback,
2679 (void *)scsicmd);
2680
2681 /*
2682 * Check that the command queued to the controller
2683 */
2684 if (status == -EINPROGRESS) {
2685 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2686 return 0;
2687 }
2688
2689 aac_fib_complete(cmd_fibcontext);
2690 aac_fib_free(cmd_fibcontext);
2691 return SCSI_MLQUEUE_HOST_BUSY;
2692}
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694/**
2695 * aac_scsi_cmd() - Process SCSI command
2696 * @scsicmd: SCSI command block
2697 *
2698 * Emulate a SCSI command and queue the required request for the
2699 * aacraid firmware.
2700 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2703{
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08002704 u32 cid, bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct Scsi_Host *host = scsicmd->device->host;
2706 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2707 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002708
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002709 if (fsa_dev_ptr == NULL)
2710 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /*
2712 * If the bus, id or lun is out of range, return fail
2713 * Test does not apply to ID 16, the pseudo id for the controller
2714 * itself.
2715 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002716 cid = scmd_id(scsicmd);
2717 if (cid != host->this_id) {
2718 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2719 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08002720 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 scsicmd->result = DID_NO_CONNECT << 16;
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002722 goto scsi_done_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 /*
2726 * If the target container doesn't exist, it may have
2727 * been newly created
2728 */
Mark Salyzyn655d7222008-04-30 16:03:42 -04002729 if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2730 (fsa_dev_ptr[cid].sense_data.sense_key ==
2731 NOT_READY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 switch (scsicmd->cmnd[0]) {
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002733 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002734 if (!(dev->raw_io_interface) ||
2735 !(dev->raw_io_64) ||
2736 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 case INQUIRY:
2739 case READ_CAPACITY:
2740 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002741 if (dev->in_reset)
2742 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07002743 return _aac_probe_container(scsicmd,
2744 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 default:
2746 break;
2747 }
2748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 } else { /* check for physical non-dasd devices */
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08002750 bus = aac_logical_to_phys(scmd_channel(scsicmd));
2751 if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2752 (dev->hba_map[bus][cid].expose
2753 == AAC_HIDE_DISK)){
2754 if (scsicmd->cmnd[0] == INQUIRY) {
2755 scsicmd->result = DID_NO_CONNECT << 16;
2756 goto scsi_done_ret;
2757 }
2758 }
2759
2760 if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2761 dev->hba_map[bus][cid].devtype
2762 == AAC_DEVTYPE_NATIVE_RAW) {
2763 if (dev->in_reset)
2764 return -1;
2765 return aac_send_hba_fib(scsicmd);
2766 } else if (dev->nondasd_support || expose_physicals ||
2767 dev->jbod) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002768 if (dev->in_reset)
2769 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return aac_send_srb_fib(scsicmd);
2771 } else {
2772 scsicmd->result = DID_NO_CONNECT << 16;
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002773 goto scsi_done_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
2775 }
2776 }
2777 /*
2778 * else Command for the controller itself
2779 */
2780 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002781 (scsicmd->cmnd[0] != TEST_UNIT_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 {
2783 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2784 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002785 set_sense(&dev->fsa_dev[cid].sense_data,
2786 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2787 ASENCODE_INVALID_COMMAND, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002789 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2790 SCSI_SENSE_BUFFERSIZE));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002791 goto scsi_done_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 switch (scsicmd->cmnd[0]) {
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002795 case READ_6:
2796 case READ_10:
2797 case READ_12:
2798 case READ_16:
2799 if (dev->in_reset)
2800 return -1;
2801 return aac_read(scsicmd);
2802
2803 case WRITE_6:
2804 case WRITE_10:
2805 case WRITE_12:
2806 case WRITE_16:
2807 if (dev->in_reset)
2808 return -1;
2809 return aac_write(scsicmd);
2810
2811 case SYNCHRONIZE_CACHE:
2812 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2813 scsicmd->result = AAC_STAT_GOOD;
2814 break;
2815 }
2816 /* Issue FIB to tell Firmware to flush it's cache */
2817 if ((aac_cache & 6) != 2)
2818 return aac_synchronize(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 case INQUIRY:
2820 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002821 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002823 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002824 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Leubner, Achimd8e965072009-04-01 07:16:08 -07002826 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002827 char *arr = (char *)&inq_data;
2828
2829 /* EVPD bit set */
2830 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2831 INQD_PDT_PROC : INQD_PDT_DA;
2832 if (scsicmd->cmnd[2] == 0) {
2833 /* supported vital product data pages */
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002834 arr[3] = 3;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002835 arr[4] = 0x0;
2836 arr[5] = 0x80;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002837 arr[6] = 0x83;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002838 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002839 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2840 sizeof(inq_data));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002841 scsicmd->result = AAC_STAT_GOOD;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002842 } else if (scsicmd->cmnd[2] == 0x80) {
2843 /* unit serial number page */
2844 arr[3] = setinqserial(dev, &arr[4],
2845 scmd_id(scsicmd));
2846 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002847 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2848 sizeof(inq_data));
Leubner, Achimd8e965072009-04-01 07:16:08 -07002849 if (aac_wwn != 2)
2850 return aac_get_container_serial(
2851 scsicmd);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002852 scsicmd->result = AAC_STAT_GOOD;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002853 } else if (scsicmd->cmnd[2] == 0x83) {
2854 /* vpd page 0x83 - Device Identification Page */
2855 char *sno = (char *)&inq_data;
2856 sno[3] = setinqserial(dev, &sno[4],
2857 scmd_id(scsicmd));
2858 if (aac_wwn != 2)
2859 return aac_get_container_serial(
2860 scsicmd);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002861 scsicmd->result = AAC_STAT_GOOD;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002862 } else {
2863 /* vpd page not implemented */
2864 scsicmd->result = DID_OK << 16 |
2865 COMMAND_COMPLETE << 8 |
2866 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002867 set_sense(&dev->fsa_dev[cid].sense_data,
2868 ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2869 ASENCODE_NO_SENSE, 7, 2);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002870 memcpy(scsicmd->sense_buffer,
2871 &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002872 min_t(size_t,
2873 sizeof(dev->fsa_dev[cid].sense_data),
2874 SCSI_SENSE_BUFFERSIZE));
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002875 }
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002876 break;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002877 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002878 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002879 inq_data.inqd_rdf = 2; /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
2880 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002882 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 /*
2884 * Set the Vendor, Product, and Revision Level
2885 * see: <vendor>.c i.e. aac.c
2886 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002887 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07002888 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002889 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002890 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2891 sizeof(inq_data));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002892 scsicmd->result = AAC_STAT_GOOD;
2893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002895 if (dev->in_reset)
2896 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07002897 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002898 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002899 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002900 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002902 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002903 if (!(dev->raw_io_interface) ||
2904 !(dev->raw_io_64) ||
2905 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2906 break;
2907 {
2908 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002909 char cp[13];
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002910 unsigned int alloc_len;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002911
2912 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2913 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002914 cp[0] = (capacity >> 56) & 0xff;
2915 cp[1] = (capacity >> 48) & 0xff;
2916 cp[2] = (capacity >> 40) & 0xff;
2917 cp[3] = (capacity >> 32) & 0xff;
2918 cp[4] = (capacity >> 24) & 0xff;
2919 cp[5] = (capacity >> 16) & 0xff;
2920 cp[6] = (capacity >> 8) & 0xff;
2921 cp[7] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002922 cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2923 cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2924 cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2925 cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002926 cp[12] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002927
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002928 alloc_len = ((scsicmd->cmnd[10] << 24)
2929 + (scsicmd->cmnd[11] << 16)
2930 + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2931
2932 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002933 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002934 if (alloc_len < scsi_bufflen(scsicmd))
2935 scsi_set_resid(scsicmd,
2936 scsi_bufflen(scsicmd) - alloc_len);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002937
2938 /* Do not cache partition table for arrays */
2939 scsicmd->device->removable = 1;
2940
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002941 scsicmd->result = AAC_STAT_GOOD;
2942 break;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002943 }
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 case READ_CAPACITY:
2946 {
2947 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002948 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002951 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 capacity = fsa_dev_ptr[cid].size - 1;
2953 else
2954 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 cp[0] = (capacity >> 24) & 0xff;
2957 cp[1] = (capacity >> 16) & 0xff;
2958 cp[2] = (capacity >> 8) & 0xff;
2959 cp[3] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002960 cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2961 cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2962 cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2963 cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002964 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002965 /* Do not cache partition table for arrays */
2966 scsicmd->device->removable = 1;
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002967 scsicmd->result = AAC_STAT_GOOD;
2968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970
2971 case MODE_SENSE:
2972 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002973 int mode_buf_length = 4;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002974 u32 capacity;
2975 aac_modep_data mpd;
2976
2977 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2978 capacity = fsa_dev_ptr[cid].size - 1;
2979 else
2980 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002983 memset((char *)&mpd, 0, sizeof(aac_modep_data));
2984
2985 /* Mode data length */
2986 mpd.hd.data_length = sizeof(mpd.hd) - 1;
2987 /* Medium type - default */
2988 mpd.hd.med_type = 0;
2989 /* Device-specific param,
2990 bit 8: 0/1 = write enabled/protected
2991 bit 4: 0/1 = FUA enabled */
2992 mpd.hd.dev_par = 0;
2993
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002994 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002995 mpd.hd.dev_par = 0x10;
2996 if (scsicmd->cmnd[1] & 0x8)
2997 mpd.hd.bd_length = 0; /* Block descriptor length */
2998 else {
2999 mpd.hd.bd_length = sizeof(mpd.bd);
3000 mpd.hd.data_length += mpd.hd.bd_length;
3001 mpd.bd.block_length[0] =
3002 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3003 mpd.bd.block_length[1] =
3004 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3005 mpd.bd.block_length[2] =
3006 fsa_dev_ptr[cid].block_size & 0xff;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04003007
3008 mpd.mpc_buf[0] = scsicmd->cmnd[2];
3009 if (scsicmd->cmnd[2] == 0x1C) {
3010 /* page length */
3011 mpd.mpc_buf[1] = 0xa;
3012 /* Mode data length */
3013 mpd.hd.data_length = 23;
3014 } else {
3015 /* Mode data length */
3016 mpd.hd.data_length = 15;
3017 }
3018
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003019 if (capacity > 0xffffff) {
3020 mpd.bd.block_count[0] = 0xff;
3021 mpd.bd.block_count[1] = 0xff;
3022 mpd.bd.block_count[2] = 0xff;
3023 } else {
3024 mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
3025 mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
3026 mpd.bd.block_count[2] = capacity & 0xff;
3027 }
3028 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003029 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3030 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003031 mpd.hd.data_length += 3;
3032 mpd.mpc_buf[0] = 8;
3033 mpd.mpc_buf[1] = 1;
3034 mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08003035 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003036 mode_buf_length = sizeof(mpd);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003037 }
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04003038
3039 if (mode_buf_length > scsicmd->cmnd[4])
3040 mode_buf_length = scsicmd->cmnd[4];
3041 else
3042 mode_buf_length = sizeof(mpd);
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003043 scsi_sg_copy_from_buffer(scsicmd,
3044 (char *)&mpd,
3045 mode_buf_length);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003046 scsicmd->result = AAC_STAT_GOOD;
3047 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049 case MODE_SENSE_10:
3050 {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003051 u32 capacity;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003052 int mode_buf_length = 8;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003053 aac_modep10_data mpd10;
3054
3055 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3056 capacity = fsa_dev_ptr[cid].size - 1;
3057 else
3058 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003061 memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
3062 /* Mode data length (MSB) */
3063 mpd10.hd.data_length[0] = 0;
3064 /* Mode data length (LSB) */
3065 mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
3066 /* Medium type - default */
3067 mpd10.hd.med_type = 0;
3068 /* Device-specific param,
3069 bit 8: 0/1 = write enabled/protected
3070 bit 4: 0/1 = FUA enabled */
3071 mpd10.hd.dev_par = 0;
3072
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08003073 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003074 mpd10.hd.dev_par = 0x10;
3075 mpd10.hd.rsrvd[0] = 0; /* reserved */
3076 mpd10.hd.rsrvd[1] = 0; /* reserved */
3077 if (scsicmd->cmnd[1] & 0x8) {
3078 /* Block descriptor length (MSB) */
3079 mpd10.hd.bd_length[0] = 0;
3080 /* Block descriptor length (LSB) */
3081 mpd10.hd.bd_length[1] = 0;
3082 } else {
3083 mpd10.hd.bd_length[0] = 0;
3084 mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
3085
3086 mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
3087
3088 mpd10.bd.block_length[0] =
3089 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3090 mpd10.bd.block_length[1] =
3091 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3092 mpd10.bd.block_length[2] =
3093 fsa_dev_ptr[cid].block_size & 0xff;
3094
3095 if (capacity > 0xffffff) {
3096 mpd10.bd.block_count[0] = 0xff;
3097 mpd10.bd.block_count[1] = 0xff;
3098 mpd10.bd.block_count[2] = 0xff;
3099 } else {
3100 mpd10.bd.block_count[0] =
3101 (capacity >> 16) & 0xff;
3102 mpd10.bd.block_count[1] =
3103 (capacity >> 8) & 0xff;
3104 mpd10.bd.block_count[2] =
3105 capacity & 0xff;
3106 }
3107 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003108 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3109 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003110 mpd10.hd.data_length[1] += 3;
3111 mpd10.mpc_buf[0] = 8;
3112 mpd10.mpc_buf[1] = 1;
3113 mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08003114 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003115 mode_buf_length = sizeof(mpd10);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003116 if (mode_buf_length > scsicmd->cmnd[8])
3117 mode_buf_length = scsicmd->cmnd[8];
3118 }
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003119 scsi_sg_copy_from_buffer(scsicmd,
3120 (char *)&mpd10,
3121 mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003123 scsicmd->result = AAC_STAT_GOOD;
3124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 case REQUEST_SENSE:
3127 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003128 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3129 sizeof(struct sense_data));
3130 memset(&dev->fsa_dev[cid].sense_data, 0,
3131 sizeof(struct sense_data));
3132 scsicmd->result = AAC_STAT_GOOD;
3133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 case ALLOW_MEDIUM_REMOVAL:
3136 dprintk((KERN_DEBUG "LOCK command.\n"));
3137 if (scsicmd->cmnd[4])
3138 fsa_dev_ptr[cid].locked = 1;
3139 else
3140 fsa_dev_ptr[cid].locked = 0;
3141
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003142 scsicmd->result = AAC_STAT_GOOD;
3143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 /*
3145 * These commands are all No-Ops
3146 */
3147 case TEST_UNIT_READY:
Mark Salyzyn655d7222008-04-30 16:03:42 -04003148 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
3149 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3150 SAM_STAT_CHECK_CONDITION;
3151 set_sense(&dev->fsa_dev[cid].sense_data,
3152 NOT_READY, SENCODE_BECOMING_READY,
3153 ASENCODE_BECOMING_READY, 0, 0);
3154 memcpy(scsicmd->sense_buffer,
3155 &dev->fsa_dev[cid].sense_data,
3156 min_t(size_t,
3157 sizeof(dev->fsa_dev[cid].sense_data),
3158 SCSI_SENSE_BUFFERSIZE));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003159 break;
Mark Salyzyn655d7222008-04-30 16:03:42 -04003160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 case RESERVE:
3162 case RELEASE:
3163 case REZERO_UNIT:
3164 case REASSIGN_BLOCKS:
3165 case SEEK_10:
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003166 scsicmd->result = AAC_STAT_GOOD;
3167 break;
Mark Salyzyn655d7222008-04-30 16:03:42 -04003168
3169 case START_STOP:
3170 return aac_start_stop(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003172 /* FALLTHRU */
3173 default:
3174 /*
3175 * Unhandled commands
3176 */
3177 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n",
3178 scsicmd->cmnd[0]));
3179 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3180 SAM_STAT_CHECK_CONDITION;
3181 set_sense(&dev->fsa_dev[cid].sense_data,
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08003182 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
3183 ASENCODE_INVALID_COMMAND, 0, 0);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003184 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08003185 min_t(size_t,
3186 sizeof(dev->fsa_dev[cid].sense_data),
3187 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003189
3190scsi_done_ret:
3191
3192 scsicmd->scsi_done(scsicmd);
3193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
3196static int query_disk(struct aac_dev *dev, void __user *arg)
3197{
3198 struct aac_query_disk qd;
3199 struct fsa_dev_info *fsa_dev_ptr;
3200
3201 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07003202 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07003203 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
3205 return -EFAULT;
Dan Carpentere6fd9162017-07-25 22:49:55 +03003206 if (qd.cnum == -1) {
3207 if (qd.id < 0 || qd.id >= dev->maximum_num_containers)
3208 return -EINVAL;
Mark Haverkampe5718772006-03-27 09:43:25 -08003209 qd.cnum = qd.id;
Dan Carpentere6fd9162017-07-25 22:49:55 +03003210 } else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
3212 return -EINVAL;
3213 qd.instance = dev->scsi_host_ptr->host_no;
3214 qd.bus = 0;
3215 qd.id = CONTAINER_TO_ID(qd.cnum);
3216 qd.lun = CONTAINER_TO_LUN(qd.cnum);
3217 }
3218 else return -EINVAL;
3219
Salyzyn, Markfd622b12007-07-17 10:59:19 -04003220 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 qd.locked = fsa_dev_ptr[qd.cnum].locked;
3222 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
3223
3224 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
3225 qd.unmapped = 1;
3226 else
3227 qd.unmapped = 0;
3228
3229 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
3230 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
3231
3232 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
3233 return -EFAULT;
3234 return 0;
3235}
3236
3237static int force_delete_disk(struct aac_dev *dev, void __user *arg)
3238{
3239 struct aac_delete_disk dd;
3240 struct fsa_dev_info *fsa_dev_ptr;
3241
3242 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp65101352006-09-19 08:59:23 -07003243 if (!fsa_dev_ptr)
3244 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3247 return -EFAULT;
3248
3249 if (dd.cnum >= dev->maximum_num_containers)
3250 return -EINVAL;
3251 /*
3252 * Mark this container as being deleted.
3253 */
3254 fsa_dev_ptr[dd.cnum].deleted = 1;
3255 /*
3256 * Mark the container as no longer valid
3257 */
3258 fsa_dev_ptr[dd.cnum].valid = 0;
3259 return 0;
3260}
3261
3262static int delete_disk(struct aac_dev *dev, void __user *arg)
3263{
3264 struct aac_delete_disk dd;
3265 struct fsa_dev_info *fsa_dev_ptr;
3266
3267 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07003268 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07003269 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3272 return -EFAULT;
3273
3274 if (dd.cnum >= dev->maximum_num_containers)
3275 return -EINVAL;
3276 /*
3277 * If the container is locked, it can not be deleted by the API.
3278 */
3279 if (fsa_dev_ptr[dd.cnum].locked)
3280 return -EBUSY;
3281 else {
3282 /*
3283 * Mark the container as no longer being valid.
3284 */
3285 fsa_dev_ptr[dd.cnum].valid = 0;
3286 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
3287 return 0;
3288 }
3289}
3290
3291int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
3292{
3293 switch (cmd) {
3294 case FSACTL_QUERY_DISK:
3295 return query_disk(dev, arg);
3296 case FSACTL_DELETE_DISK:
3297 return delete_disk(dev, arg);
3298 case FSACTL_FORCE_DELETE_DISK:
3299 return force_delete_disk(dev, arg);
3300 case FSACTL_GET_CONTAINERS:
3301 return aac_get_containers(dev);
3302 default:
3303 return -ENOTTY;
3304 }
3305}
3306
3307/**
3308 *
3309 * aac_srb_callback
3310 * @context: the context set in the fib - here it is scsi cmd
3311 * @fibptr: pointer to the fib
3312 *
3313 * Handles the completion of a scsi command to a non dasd device
3314 *
3315 */
3316
3317static void aac_srb_callback(void *context, struct fib * fibptr)
3318{
3319 struct aac_dev *dev;
3320 struct aac_srb_reply *srbreply;
3321 struct scsi_cmnd *scsicmd;
3322
3323 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07003324
3325 if (!aac_valid_context(scsicmd, fibptr))
3326 return;
3327
Eric Sesterhenn125e1872006-06-23 02:06:06 -07003328 BUG_ON(fibptr == NULL);
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003329
Salyzyn, Mark1a655042007-06-11 16:17:55 -04003330 dev = fibptr->dev;
3331
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003332 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003333
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303336 if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3337 /* fast response */
3338 srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
3339 srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
3340 } else {
3341 /*
3342 * Calculate resid for sg
3343 */
3344 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
3345 - le32_to_cpu(srbreply->data_xfer_length));
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003346 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003349 scsi_dma_unmap(scsicmd);
3350
3351 /* expose physical device if expose_physicald flag is on */
3352 if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
3353 && expose_physicals > 0)
3354 aac_expose_phy_device(scsicmd);
3355
3356 /*
3357 * First check the fib status
3358 */
3359
3360 if (le32_to_cpu(srbreply->status) != ST_OK) {
3361 int len;
3362
3363 pr_warn("aac_srb_callback: srb failed, status = %d\n",
3364 le32_to_cpu(srbreply->status));
3365 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3366 SCSI_SENSE_BUFFERSIZE);
3367 scsicmd->result = DID_ERROR << 16
3368 | COMMAND_COMPLETE << 8
3369 | SAM_STAT_CHECK_CONDITION;
3370 memcpy(scsicmd->sense_buffer,
3371 srbreply->sense_data, len);
3372 }
3373
3374 /*
3375 * Next check the srb status
3376 */
3377 switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3378 case SRB_STATUS_ERROR_RECOVERY:
3379 case SRB_STATUS_PENDING:
3380 case SRB_STATUS_SUCCESS:
3381 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3382 break;
3383 case SRB_STATUS_DATA_OVERRUN:
3384 switch (scsicmd->cmnd[0]) {
3385 case READ_6:
3386 case WRITE_6:
3387 case READ_10:
3388 case WRITE_10:
3389 case READ_12:
3390 case WRITE_12:
3391 case READ_16:
3392 case WRITE_16:
3393 if (le32_to_cpu(srbreply->data_xfer_length)
3394 < scsicmd->underflow)
3395 pr_warn("aacraid: SCSI CMD underflow\n");
3396 else
3397 pr_warn("aacraid: SCSI CMD Data Overrun\n");
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003398 scsicmd->result = DID_ERROR << 16
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003399 | COMMAND_COMPLETE << 8;
3400 break;
3401 case INQUIRY:
3402 scsicmd->result = DID_OK << 16
3403 | COMMAND_COMPLETE << 8;
3404 break;
3405 default:
Mark Haverkampbb08f922006-02-01 09:30:44 -08003406 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 break;
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003408 }
3409 break;
3410 case SRB_STATUS_ABORTED:
3411 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3412 break;
3413 case SRB_STATUS_ABORT_FAILED:
3414 /*
3415 * Not sure about this one - but assuming the
3416 * hba was trying to abort for some reason
3417 */
3418 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3419 break;
3420 case SRB_STATUS_PARITY_ERROR:
3421 scsicmd->result = DID_PARITY << 16
3422 | MSG_PARITY_ERROR << 8;
3423 break;
3424 case SRB_STATUS_NO_DEVICE:
3425 case SRB_STATUS_INVALID_PATH_ID:
3426 case SRB_STATUS_INVALID_TARGET_ID:
3427 case SRB_STATUS_INVALID_LUN:
3428 case SRB_STATUS_SELECTION_TIMEOUT:
3429 scsicmd->result = DID_NO_CONNECT << 16
3430 | COMMAND_COMPLETE << 8;
3431 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003432
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003433 case SRB_STATUS_COMMAND_TIMEOUT:
3434 case SRB_STATUS_TIMEOUT:
3435 scsicmd->result = DID_TIME_OUT << 16
3436 | COMMAND_COMPLETE << 8;
3437 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003438
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003439 case SRB_STATUS_BUSY:
3440 scsicmd->result = DID_BUS_BUSY << 16
3441 | COMMAND_COMPLETE << 8;
3442 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003443
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003444 case SRB_STATUS_BUS_RESET:
3445 scsicmd->result = DID_RESET << 16
3446 | COMMAND_COMPLETE << 8;
3447 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003448
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003449 case SRB_STATUS_MESSAGE_REJECTED:
3450 scsicmd->result = DID_ERROR << 16
3451 | MESSAGE_REJECT << 8;
3452 break;
3453 case SRB_STATUS_REQUEST_FLUSHED:
3454 case SRB_STATUS_ERROR:
3455 case SRB_STATUS_INVALID_REQUEST:
3456 case SRB_STATUS_REQUEST_SENSE_FAILED:
3457 case SRB_STATUS_NO_HBA:
3458 case SRB_STATUS_UNEXPECTED_BUS_FREE:
3459 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3460 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3461 case SRB_STATUS_DELAYED_RETRY:
3462 case SRB_STATUS_BAD_FUNCTION:
3463 case SRB_STATUS_NOT_STARTED:
3464 case SRB_STATUS_NOT_IN_USE:
3465 case SRB_STATUS_FORCE_ABORT:
3466 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3467 default:
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003468#ifdef AAC_DETAILED_STATUS_INFO
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003469 pr_info("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x -scsi status 0x%x\n",
3470 le32_to_cpu(srbreply->srb_status) & 0x3F,
3471 aac_get_status_string(
3472 le32_to_cpu(srbreply->srb_status) & 0x3F),
3473 scsicmd->cmnd[0],
3474 le32_to_cpu(srbreply->scsi_status));
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003475#endif
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003476 /*
3477 * When the CC bit is SET by the host in ATA pass thru CDB,
3478 * driver is supposed to return DID_OK
3479 *
3480 * When the CC bit is RESET by the host, driver should
3481 * return DID_ERROR
3482 */
3483 if ((scsicmd->cmnd[0] == ATA_12)
3484 || (scsicmd->cmnd[0] == ATA_16)) {
3485
3486 if (scsicmd->cmnd[2] & (0x01 << 5)) {
3487 scsicmd->result = DID_OK << 16
3488 | COMMAND_COMPLETE << 8;
3489 break;
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003490 } else {
3491 scsicmd->result = DID_ERROR << 16
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003492 | COMMAND_COMPLETE << 8;
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003493 break;
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003494 }
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003495 } else {
3496 scsicmd->result = DID_ERROR << 16
3497 | COMMAND_COMPLETE << 8;
3498 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003501 if (le32_to_cpu(srbreply->scsi_status)
3502 == SAM_STAT_CHECK_CONDITION) {
3503 int len;
3504
3505 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3506 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3507 SCSI_SENSE_BUFFERSIZE);
3508#ifdef AAC_DETAILED_STATUS_INFO
3509 pr_warn("aac_srb_callback: check condition, status = %d len=%d\n",
3510 le32_to_cpu(srbreply->status), len);
3511#endif
3512 memcpy(scsicmd->sense_buffer,
3513 srbreply->sense_data, len);
3514 }
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 /*
3517 * OR in the scsi status (already shifted up a bit)
3518 */
3519 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3520
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003521 aac_fib_complete(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07003522 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003525static void hba_resp_task_complete(struct aac_dev *dev,
3526 struct scsi_cmnd *scsicmd,
3527 struct aac_hba_resp *err) {
3528
3529 scsicmd->result = err->status;
3530 /* set residual count */
3531 scsi_set_resid(scsicmd, le32_to_cpu(err->residual_count));
3532
3533 switch (err->status) {
3534 case SAM_STAT_GOOD:
3535 scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3536 break;
3537 case SAM_STAT_CHECK_CONDITION:
3538 {
3539 int len;
3540
3541 len = min_t(u8, err->sense_response_data_len,
3542 SCSI_SENSE_BUFFERSIZE);
3543 if (len)
3544 memcpy(scsicmd->sense_buffer,
3545 err->sense_response_buf, len);
3546 scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3547 break;
3548 }
3549 case SAM_STAT_BUSY:
3550 scsicmd->result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
3551 break;
3552 case SAM_STAT_TASK_ABORTED:
3553 scsicmd->result |= DID_ABORT << 16 | ABORT << 8;
3554 break;
3555 case SAM_STAT_RESERVATION_CONFLICT:
3556 case SAM_STAT_TASK_SET_FULL:
3557 default:
3558 scsicmd->result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3559 break;
3560 }
3561}
3562
3563static void hba_resp_task_failure(struct aac_dev *dev,
3564 struct scsi_cmnd *scsicmd,
3565 struct aac_hba_resp *err)
3566{
3567 switch (err->status) {
3568 case HBA_RESP_STAT_HBAMODE_DISABLED:
3569 {
3570 u32 bus, cid;
3571
3572 bus = aac_logical_to_phys(scmd_channel(scsicmd));
3573 cid = scmd_id(scsicmd);
3574 if (dev->hba_map[bus][cid].devtype == AAC_DEVTYPE_NATIVE_RAW) {
3575 dev->hba_map[bus][cid].devtype = AAC_DEVTYPE_ARC_RAW;
3576 dev->hba_map[bus][cid].rmw_nexus = 0xffffffff;
3577 }
3578 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3579 break;
3580 }
3581 case HBA_RESP_STAT_IO_ERROR:
3582 case HBA_RESP_STAT_NO_PATH_TO_DEVICE:
3583 scsicmd->result = DID_OK << 16 |
3584 COMMAND_COMPLETE << 8 | SAM_STAT_BUSY;
3585 break;
3586 case HBA_RESP_STAT_IO_ABORTED:
3587 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3588 break;
3589 case HBA_RESP_STAT_INVALID_DEVICE:
3590 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3591 break;
3592 case HBA_RESP_STAT_UNDERRUN:
3593 /* UNDERRUN is OK */
3594 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3595 break;
3596 case HBA_RESP_STAT_OVERRUN:
3597 default:
3598 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3599 break;
3600 }
3601}
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603/**
3604 *
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003605 * aac_hba_callback
3606 * @context: the context set in the fib - here it is scsi cmd
3607 * @fibptr: pointer to the fib
3608 *
3609 * Handles the completion of a native HBA scsi command
3610 *
3611 */
3612void aac_hba_callback(void *context, struct fib *fibptr)
3613{
3614 struct aac_dev *dev;
3615 struct scsi_cmnd *scsicmd;
3616
3617 struct aac_hba_resp *err =
3618 &((struct aac_native_hba *)fibptr->hw_fib_va)->resp.err;
3619
3620 scsicmd = (struct scsi_cmnd *) context;
3621
3622 if (!aac_valid_context(scsicmd, fibptr))
3623 return;
3624
3625 WARN_ON(fibptr == NULL);
3626 dev = fibptr->dev;
3627
3628 if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF))
3629 scsi_dma_unmap(scsicmd);
3630
3631 if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3632 /* fast response */
3633 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3634 goto out;
3635 }
3636
3637 switch (err->service_response) {
3638 case HBA_RESP_SVCRES_TASK_COMPLETE:
3639 hba_resp_task_complete(dev, scsicmd, err);
3640 break;
3641 case HBA_RESP_SVCRES_FAILURE:
3642 hba_resp_task_failure(dev, scsicmd, err);
3643 break;
3644 case HBA_RESP_SVCRES_TMF_REJECTED:
3645 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
3646 break;
3647 case HBA_RESP_SVCRES_TMF_LUN_INVALID:
3648 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3649 break;
3650 case HBA_RESP_SVCRES_TMF_COMPLETE:
3651 case HBA_RESP_SVCRES_TMF_SUCCEEDED:
3652 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3653 break;
3654 default:
3655 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3656 break;
3657 }
3658
3659out:
3660 aac_fib_complete(fibptr);
3661
3662 if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF)
3663 scsicmd->SCp.sent_command = 1;
3664 else
3665 scsicmd->scsi_done(scsicmd);
3666}
3667
3668/**
3669 *
3670 * aac_send_srb_fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 * @scsicmd: the scsi command block
3672 *
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08003673 * This routine will form a FIB and fill in the aac_srb from the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 * scsicmd passed in.
3675 */
3676
3677static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3678{
3679 struct fib* cmd_fibcontext;
3680 struct aac_dev* dev;
3681 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Mark Haverkamp84971732005-06-20 11:55:24 -07003683 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08003684 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07003685 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 scsicmd->result = DID_NO_CONNECT << 16;
3687 scsicmd->scsi_done(scsicmd);
3688 return 0;
3689 }
3690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 /*
3692 * Allocate and initialize a Fib then setup a BlockWrite command
3693 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08003694 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08003695
Mark Haverkampe8f32de2007-01-23 15:00:30 -08003696 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 /*
3699 * Check that the command queued to the controller
3700 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08003701 if (status == -EINPROGRESS) {
3702 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return 0;
3704 }
3705
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003706 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3707 aac_fib_complete(cmd_fibcontext);
3708 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 return -1;
3711}
3712
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003713/**
3714 *
3715 * aac_send_hba_fib
3716 * @scsicmd: the scsi command block
3717 *
3718 * This routine will form a FIB and fill in the aac_hba_cmd_req from the
3719 * scsicmd passed in.
3720 */
3721static int aac_send_hba_fib(struct scsi_cmnd *scsicmd)
3722{
3723 struct fib *cmd_fibcontext;
3724 struct aac_dev *dev;
3725 int status;
3726
3727 dev = shost_priv(scsicmd->device->host);
3728 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3729 scsicmd->device->lun > AAC_MAX_LUN - 1) {
3730 scsicmd->result = DID_NO_CONNECT << 16;
3731 scsicmd->scsi_done(scsicmd);
3732 return 0;
3733 }
3734
3735 /*
3736 * Allocate and initialize a Fib then setup a BlockWrite command
3737 */
3738 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3739 if (!cmd_fibcontext)
3740 return -1;
3741
3742 status = aac_adapter_hba(cmd_fibcontext, scsicmd);
3743
3744 /*
3745 * Check that the command queued to the controller
3746 */
3747 if (status == -EINPROGRESS) {
3748 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3749 return 0;
3750 }
3751
3752 pr_warn("aac_hba_cmd_req: aac_fib_send failed with status: %d\n",
3753 status);
3754 aac_fib_complete(cmd_fibcontext);
3755 aac_fib_free(cmd_fibcontext);
3756
3757 return -1;
3758}
3759
3760
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303761static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
3763 struct aac_dev *dev;
3764 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003765 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3768 // Get rid of old data
3769 psg->count = 0;
3770 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003771 psg->sg[0].count = 0;
3772
3773 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303774 if (nseg < 0)
3775 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003776 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 struct scatterlist *sg;
3778 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003780 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003782 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3784 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3785 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 }
3787 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003788 if (byte_count > scsi_bufflen(scsicmd)) {
3789 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3790 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003791 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003792 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 }
3794 /* Check for command underflow */
3795 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3796 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3797 byte_count, scsicmd->underflow);
3798 }
3799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return byte_count;
3801}
3802
3803
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303804static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
3806 struct aac_dev *dev;
3807 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003808 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003809 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3812 // Get rid of old data
3813 psg->count = 0;
3814 psg->sg[0].addr[0] = 0;
3815 psg->sg[0].addr[1] = 0;
3816 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003817
3818 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303819 if (nseg < 0)
3820 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003821 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 struct scatterlist *sg;
3823 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003825 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08003826 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003827 addr = sg_dma_address(sg);
3828 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3829 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08003830 psg->sg[i].count = cpu_to_le32(count);
3831 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003833 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003835 if (byte_count > scsi_bufflen(scsicmd)) {
3836 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3837 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003838 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003839 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 }
3841 /* Check for command underflow */
3842 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3843 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3844 byte_count, scsicmd->underflow);
3845 }
3846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return byte_count;
3848}
3849
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303850static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003851{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003852 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003853 int nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003854
3855 // Get rid of old data
3856 psg->count = 0;
3857 psg->sg[0].next = 0;
3858 psg->sg[0].prev = 0;
3859 psg->sg[0].addr[0] = 0;
3860 psg->sg[0].addr[1] = 0;
3861 psg->sg[0].count = 0;
3862 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003863
3864 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303865 if (nseg < 0)
3866 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003867 if (nseg) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003868 struct scatterlist *sg;
3869 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003870
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003871 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003872 int count = sg_dma_len(sg);
3873 u64 addr = sg_dma_address(sg);
3874 psg->sg[i].next = 0;
3875 psg->sg[i].prev = 0;
3876 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3877 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3878 psg->sg[i].count = cpu_to_le32(count);
3879 psg->sg[i].flags = 0;
3880 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003881 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003882 psg->count = cpu_to_le32(nseg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003883 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003884 if (byte_count > scsi_bufflen(scsicmd)) {
3885 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3886 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003887 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003888 byte_count = scsi_bufflen(scsicmd);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003889 }
3890 /* Check for command underflow */
3891 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3892 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3893 byte_count, scsicmd->underflow);
3894 }
3895 }
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003896 return byte_count;
3897}
3898
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303899static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3900 struct aac_raw_io2 *rio2, int sg_max)
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303901{
3902 unsigned long byte_count = 0;
3903 int nseg;
3904
3905 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303906 if (nseg < 0)
3907 return nseg;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303908 if (nseg) {
3909 struct scatterlist *sg;
3910 int i, conformable = 0;
3911 u32 min_size = PAGE_SIZE, cur_size;
3912
3913 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3914 int count = sg_dma_len(sg);
3915 u64 addr = sg_dma_address(sg);
3916
3917 BUG_ON(i >= sg_max);
3918 rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3919 rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3920 cur_size = cpu_to_le32(count);
3921 rio2->sge[i].length = cur_size;
3922 rio2->sge[i].flags = 0;
3923 if (i == 0) {
3924 conformable = 1;
3925 rio2->sgeFirstSize = cur_size;
3926 } else if (i == 1) {
3927 rio2->sgeNominalSize = cur_size;
3928 min_size = cur_size;
3929 } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
3930 conformable = 0;
3931 if (cur_size < min_size)
3932 min_size = cur_size;
3933 }
3934 byte_count += count;
3935 }
3936
3937 /* hba wants the size to be exact */
3938 if (byte_count > scsi_bufflen(scsicmd)) {
3939 u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
3940 (byte_count - scsi_bufflen(scsicmd));
3941 rio2->sge[i-1].length = cpu_to_le32(temp);
3942 byte_count = scsi_bufflen(scsicmd);
3943 }
3944
3945 rio2->sgeCnt = cpu_to_le32(nseg);
3946 rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
3947 /* not conformable: evaluate required sg elements */
3948 if (!conformable) {
3949 int j, nseg_new = nseg, err_found;
3950 for (i = min_size / PAGE_SIZE; i >= 1; --i) {
3951 err_found = 0;
3952 nseg_new = 2;
3953 for (j = 1; j < nseg - 1; ++j) {
3954 if (rio2->sge[j].length % (i*PAGE_SIZE)) {
3955 err_found = 1;
3956 break;
3957 }
3958 nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
3959 }
3960 if (!err_found)
3961 break;
3962 }
3963 if (i > 0 && nseg_new <= sg_max)
3964 aac_convert_sgraw2(rio2, i, nseg, nseg_new);
3965 } else
3966 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3967
3968 /* Check for command underflow */
3969 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3970 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3971 byte_count, scsicmd->underflow);
3972 }
3973 }
3974
3975 return byte_count;
3976}
3977
3978static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
3979{
3980 struct sge_ieee1212 *sge;
3981 int i, j, pos;
3982 u32 addr_low;
3983
3984 if (aac_convert_sgl == 0)
3985 return 0;
3986
3987 sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
3988 if (sge == NULL)
3989 return -1;
3990
3991 for (i = 1, pos = 1; i < nseg-1; ++i) {
3992 for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
3993 addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
3994 sge[pos].addrLow = addr_low;
3995 sge[pos].addrHigh = rio2->sge[i].addrHigh;
3996 if (addr_low < rio2->sge[i].addrLow)
3997 sge[pos].addrHigh++;
3998 sge[pos].length = pages * PAGE_SIZE;
3999 sge[pos].flags = 0;
4000 pos++;
4001 }
4002 }
4003 sge[pos] = rio2->sge[nseg-1];
4004 memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
4005
4006 kfree(sge);
4007 rio2->sgeCnt = cpu_to_le32(nseg_new);
4008 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4009 rio2->sgeNominalSize = pages * PAGE_SIZE;
4010 return 0;
4011}
4012
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08004013static long aac_build_sghba(struct scsi_cmnd *scsicmd,
4014 struct aac_hba_cmd_req *hbacmd,
4015 int sg_max,
4016 u64 sg_address)
4017{
4018 unsigned long byte_count = 0;
4019 int nseg;
4020 struct scatterlist *sg;
4021 int i;
4022 u32 cur_size;
4023 struct aac_hba_sgl *sge;
4024
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08004025 nseg = scsi_dma_map(scsicmd);
4026 if (nseg <= 0) {
4027 byte_count = nseg;
4028 goto out;
4029 }
4030
4031 if (nseg > HBA_MAX_SG_EMBEDDED)
4032 sge = &hbacmd->sge[2];
4033 else
4034 sge = &hbacmd->sge[0];
4035
4036 scsi_for_each_sg(scsicmd, sg, nseg, i) {
4037 int count = sg_dma_len(sg);
4038 u64 addr = sg_dma_address(sg);
4039
4040 WARN_ON(i >= sg_max);
4041 sge->addr_hi = cpu_to_le32((u32)(addr>>32));
4042 sge->addr_lo = cpu_to_le32((u32)(addr & 0xffffffff));
4043 cur_size = cpu_to_le32(count);
4044 sge->len = cur_size;
4045 sge->flags = 0;
4046 byte_count += count;
4047 sge++;
4048 }
4049
4050 sge--;
4051 /* hba wants the size to be exact */
4052 if (byte_count > scsi_bufflen(scsicmd)) {
4053 u32 temp;
4054
4055 temp = le32_to_cpu(sge->len) - byte_count
4056 - scsi_bufflen(scsicmd);
4057 sge->len = cpu_to_le32(temp);
4058 byte_count = scsi_bufflen(scsicmd);
4059 }
4060
4061 if (nseg <= HBA_MAX_SG_EMBEDDED) {
4062 hbacmd->emb_data_desc_count = cpu_to_le32(nseg);
4063 sge->flags = cpu_to_le32(0x40000000);
4064 } else {
4065 /* not embedded */
4066 hbacmd->sge[0].flags = cpu_to_le32(0x80000000);
4067 hbacmd->emb_data_desc_count = (u8)cpu_to_le32(1);
4068 hbacmd->sge[0].addr_hi = (u32)cpu_to_le32(sg_address >> 32);
4069 hbacmd->sge[0].addr_lo =
4070 cpu_to_le32((u32)(sg_address & 0xffffffff));
4071 }
4072
4073 /* Check for command underflow */
4074 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4075 pr_warn("aacraid: cmd len %08lX cmd underflow %08X\n",
4076 byte_count, scsicmd->underflow);
4077 }
4078out:
4079 return byte_count;
4080}
4081
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082#ifdef AAC_DETAILED_STATUS_INFO
4083
4084struct aac_srb_status_info {
4085 u32 status;
4086 char *str;
4087};
4088
4089
4090static struct aac_srb_status_info srb_status_info[] = {
4091 { SRB_STATUS_PENDING, "Pending Status"},
4092 { SRB_STATUS_SUCCESS, "Success"},
4093 { SRB_STATUS_ABORTED, "Aborted Command"},
4094 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07004095 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 { SRB_STATUS_BUSY, "Device Busy"},
4097 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
4098 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
4099 { SRB_STATUS_NO_DEVICE, "No Device"},
4100 { SRB_STATUS_TIMEOUT, "Timeout"},
4101 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
4102 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
4103 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
4104 { SRB_STATUS_BUS_RESET, "Bus Reset"},
4105 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
4106 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
4107 { SRB_STATUS_NO_HBA, "No HBA"},
4108 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
4109 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
4110 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
4111 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
4112 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
4113 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07004114 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
4116 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
4117 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
4118 { SRB_STATUS_NOT_STARTED, "Not Started"},
4119 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08004120 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
4122 { 0xff, "Unknown Error"}
4123};
4124
4125char *aac_get_status_string(u32 status)
4126{
4127 int i;
4128
Tobias Klauser6391a112006-06-08 22:23:48 -07004129 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
4130 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
4133 return "Bad Status Code";
4134}
4135
4136#endif