blob: 4ad9d3f017af41353ab3981be0637881c68d6006 [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
Raghava Aditya Renukunta4b000222017-12-26 20:34:36 -080045#include <asm/unaligned.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <scsi/scsi.h>
48#include <scsi/scsi_cmnd.h>
49#include <scsi/scsi_device.h>
50#include <scsi/scsi_host.h>
51
52#include "aacraid.h"
53
54/* values for inqd_pdt: Peripheral device type in plain English */
55#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
56#define INQD_PDT_PROC 0x03 /* Processor device */
57#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
58#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
59#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
60#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
61
62#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
63#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
66 * Sense codes
67 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080068
69#define SENCODE_NO_SENSE 0x00
70#define SENCODE_END_OF_DATA 0x00
71#define SENCODE_BECOMING_READY 0x04
72#define SENCODE_INIT_CMD_REQUIRED 0x04
Raghava Aditya Renukuntaf956a662017-02-02 15:53:23 -080073#define SENCODE_UNRECOVERED_READ_ERROR 0x11
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080074#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
75#define SENCODE_INVALID_COMMAND 0x20
76#define SENCODE_LBA_OUT_OF_RANGE 0x21
77#define SENCODE_INVALID_CDB_FIELD 0x24
78#define SENCODE_LUN_NOT_SUPPORTED 0x25
79#define SENCODE_INVALID_PARAM_FIELD 0x26
80#define SENCODE_PARAM_NOT_SUPPORTED 0x26
81#define SENCODE_PARAM_VALUE_INVALID 0x26
82#define SENCODE_RESET_OCCURRED 0x29
83#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
84#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
85#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
86#define SENCODE_DIAGNOSTIC_FAILURE 0x40
87#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
88#define SENCODE_INVALID_MESSAGE_ERROR 0x49
89#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
90#define SENCODE_OVERLAPPED_COMMAND 0x4E
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * Additional sense codes
94 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080095
96#define ASENCODE_NO_SENSE 0x00
97#define ASENCODE_END_OF_DATA 0x05
98#define ASENCODE_BECOMING_READY 0x01
99#define ASENCODE_INIT_CMD_REQUIRED 0x02
100#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
101#define ASENCODE_INVALID_COMMAND 0x00
102#define ASENCODE_LBA_OUT_OF_RANGE 0x00
103#define ASENCODE_INVALID_CDB_FIELD 0x00
104#define ASENCODE_LUN_NOT_SUPPORTED 0x00
105#define ASENCODE_INVALID_PARAM_FIELD 0x00
106#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
107#define ASENCODE_PARAM_VALUE_INVALID 0x02
108#define ASENCODE_RESET_OCCURRED 0x00
109#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
110#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
111#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
112#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
113#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
114#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
115#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
116#define ASENCODE_OVERLAPPED_COMMAND 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -0800118#define AAC_STAT_GOOD (DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD)
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#define BYTE0(x) (unsigned char)(x)
121#define BYTE1(x) (unsigned char)((x) >> 8)
122#define BYTE2(x) (unsigned char)((x) >> 16)
123#define BYTE3(x) (unsigned char)((x) >> 24)
124
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400125/* MODE_SENSE data format */
126typedef struct {
127 struct {
128 u8 data_length;
129 u8 med_type;
130 u8 dev_par;
131 u8 bd_length;
132 } __attribute__((packed)) hd;
133 struct {
134 u8 dens_code;
135 u8 block_count[3];
136 u8 reserved;
137 u8 block_length[3];
138 } __attribute__((packed)) bd;
139 u8 mpc_buf[3];
140} __attribute__((packed)) aac_modep_data;
141
142/* MODE_SENSE_10 data format */
143typedef struct {
144 struct {
145 u8 data_length[2];
146 u8 med_type;
147 u8 dev_par;
148 u8 rsrvd[2];
149 u8 bd_length[2];
150 } __attribute__((packed)) hd;
151 struct {
152 u8 dens_code;
153 u8 block_count[3];
154 u8 reserved;
155 u8 block_length[3];
156 } __attribute__((packed)) bd;
157 u8 mpc_buf[3];
158} __attribute__((packed)) aac_modep10_data;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*------------------------------------------------------------------------------
161 * S T R U C T S / T Y P E D E F S
162 *----------------------------------------------------------------------------*/
163/* SCSI inquiry data */
164struct inquiry_data {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800165 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
166 u8 inqd_dtq; /* RMB | Device Type Qualifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
168 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
169 u8 inqd_len; /* Additional length (n-4) */
170 u8 inqd_pad1[2];/* Reserved - must be zero */
171 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
172 u8 inqd_vid[8]; /* Vendor ID */
173 u8 inqd_pid[16];/* Product ID */
174 u8 inqd_prl[4]; /* Product Revision Level */
175};
176
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400177/* Added for VPD 0x83 */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800178struct tvpd_id_descriptor_type_1 {
179 u8 codeset:4; /* VPD_CODE_SET */
180 u8 reserved:4;
181 u8 identifiertype:4; /* VPD_IDENTIFIER_TYPE */
182 u8 reserved2:4;
183 u8 reserved3;
184 u8 identifierlength;
185 u8 venid[8];
186 u8 productid[16];
187 u8 serialnumber[8]; /* SN in ASCII */
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400188
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800189};
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400190
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800191struct tvpd_id_descriptor_type_2 {
192 u8 codeset:4; /* VPD_CODE_SET */
193 u8 reserved:4;
194 u8 identifiertype:4; /* VPD_IDENTIFIER_TYPE */
195 u8 reserved2:4;
196 u8 reserved3;
197 u8 identifierlength;
198 struct teu64id {
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400199 u32 Serial;
200 /* The serial number supposed to be 40 bits,
201 * bit we only support 32, so make the last byte zero. */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800202 u8 reserved;
203 u8 venid[3];
204 } eu64id;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400205
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800206};
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400207
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800208struct tvpd_id_descriptor_type_3 {
209 u8 codeset : 4; /* VPD_CODE_SET */
210 u8 reserved : 4;
211 u8 identifiertype : 4; /* VPD_IDENTIFIER_TYPE */
212 u8 reserved2 : 4;
213 u8 reserved3;
214 u8 identifierlength;
215 u8 Identifier[16];
216};
217
218struct tvpd_page83 {
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400219 u8 DeviceType:5;
220 u8 DeviceTypeQualifier:3;
221 u8 PageCode;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800222 u8 reserved;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400223 u8 PageLength;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800224 struct tvpd_id_descriptor_type_1 type1;
225 struct tvpd_id_descriptor_type_2 type2;
226 struct tvpd_id_descriptor_type_3 type3;
227};
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
230 * M O D U L E G L O B A L S
231 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800232
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +0530233static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
234static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
235static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
236static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
237 struct aac_raw_io2 *rio2, int sg_max);
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -0800238static long aac_build_sghba(struct scsi_cmnd *scsicmd,
239 struct aac_hba_cmd_req *hbacmd,
240 int sg_max, u64 sg_address);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +0530241static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
242 int pages, int nseg, int nseg_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -0800244static int aac_send_hba_fib(struct scsi_cmnd *scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#ifdef AAC_DETAILED_STATUS_INFO
246static char *aac_get_status_string(u32 status);
247#endif
248
249/*
250 * Non dasd selection is handled entirely in aachba now
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800251 */
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int nondasd = -1;
Leubner, Achimd8e965072009-04-01 07:16:08 -0700254static int aac_cache = 2; /* WCE=0 to avoid performance problems */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static int dacmode = -1;
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800256int aac_msi;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400257int aac_commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700258int startup_timeout = 180;
259int aif_timeout = 120;
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800260int aac_sync_mode; /* Only Sync. transfer - disabled */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530261int aac_convert_sgl = 1; /* convert non-conformable s/g list - enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800263module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
264MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
265 " 0=off, 1=on");
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530266module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
267MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
268 " 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800269module_param(nondasd, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800270MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
271 " 0=off, 1=on");
Salyzyn, Mark95e852e2008-01-08 12:01:07 -0800272module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800273MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
274 "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
275 "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
Leubner, Achimd8e965072009-04-01 07:16:08 -0700276 "\tbit 2 - Disable only if Battery is protecting Cache");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800277module_param(dacmode, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800278MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
279 " 0=off, 1=on");
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400280module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800281MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
282 " adapter for foreign arrays.\n"
283 "This is typically needed in systems that do not have a BIOS."
284 " 0=off, 1=on");
285module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
286MODULE_PARM_DESC(msi, "IRQ handling."
Mahesh Rajashekhara9022d372015-08-28 06:38:35 -0400287 " 0=PIC(default), 1=MSI, 2=MSI-X)");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700288module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800289MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
290 " adapter to have it's kernel up and\n"
291 "running. This is typically adjusted for large systems that do not"
292 " have a BIOS.");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700293module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800294MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
295 " applications to pick up AIFs before\n"
296 "deregistering them. This is typically adjusted for heavily burdened"
297 " systems.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Raghava Aditya Renukunta09624642017-02-16 12:51:22 -0800299int aac_fib_dump;
300module_param(aac_fib_dump, int, 0644);
301MODULE_PARM_DESC(aac_fib_dump, "Dump controller fibs prior to IOP_RESET 0=off, 1=on");
302
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700303int numacb = -1;
304module_param(numacb, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800305MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
306 " blocks (FIB) allocated. Valid values are 512 and down. Default is"
307 " to use suggestion from Firmware.");
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700308
309int acbsize = -1;
310module_param(acbsize, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800311MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
312 " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
313 " suggestion from Firmware.");
Mark Haverkamp653ba582006-09-19 08:59:43 -0700314
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400315int update_interval = 30 * 60;
316module_param(update_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800317MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
318 " updates issued to adapter.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400319
Raghava Aditya Renukunta11da1b72017-02-16 12:51:19 -0800320int check_interval = 60;
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400321module_param(check_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800322MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
323 " checks.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400324
Andrew Morton87f3bda2007-08-10 14:50:30 -0700325int aac_check_reset = 1;
326module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
Paul Bolle95e7a8e2008-04-16 14:43:00 +0200327MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800328 " adapter. a value of -1 forces the reset to adapters programmed to"
329 " ignore it.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400330
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800331int expose_physicals = -1;
Mark Haverkamp653ba582006-09-19 08:59:43 -0700332module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800333MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
334 " -1=protect 0=off, 1=on");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700335
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800336int aac_reset_devices;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400337module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
338MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700339
Leubner, Achimd8e965072009-04-01 07:16:08 -0700340int aac_wwn = 1;
341module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
342MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
343 "\t0 - Disable\n"
344 "\t1 - Array Meta Data Signature (default)\n"
345 "\t2 - Adapter Serial Number");
346
347
Mark Haverkamp03d44332007-03-15 10:27:45 -0700348static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
349 struct fib *fibptr) {
350 struct scsi_device *device;
351
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800352 if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
Salyzyn, Markc835e372007-07-26 14:20:02 -0400353 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800354 aac_fib_complete(fibptr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800355 return 0;
356 }
Mark Haverkamp03d44332007-03-15 10:27:45 -0700357 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
358 device = scsicmd->device;
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800359 if (unlikely(!device)) {
Mark Haverkamp03d44332007-03-15 10:27:45 -0700360 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
361 aac_fib_complete(fibptr);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700362 return 0;
363 }
364 return 1;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
368 * aac_get_config_status - check the adapter configuration
369 * @common: adapter to query
370 *
371 * Query config status, and commit the configuration if needed.
372 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700373int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 int status = 0;
376 struct fib * fibptr;
377
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800378 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -ENOMEM;
380
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800381 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 {
383 struct aac_get_config_status *dinfo;
384 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
385
386 dinfo->command = cpu_to_le32(VM_ContainerConfig);
387 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
388 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
389 }
390
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800391 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 fibptr,
393 sizeof (struct aac_get_config_status),
394 FsaNormal,
395 1, 1,
396 NULL, NULL);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800397 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
399 } else {
400 struct aac_get_config_status_resp *reply
401 = (struct aac_get_config_status_resp *) fib_data(fibptr);
402 dprintk((KERN_WARNING
403 "aac_get_config_status: response=%d status=%d action=%d\n",
404 le32_to_cpu(reply->response),
405 le32_to_cpu(reply->status),
406 le32_to_cpu(reply->data.action)));
407 if ((le32_to_cpu(reply->response) != ST_OK) ||
408 (le32_to_cpu(reply->status) != CT_OK) ||
409 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
410 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
411 status = -EINVAL;
412 }
413 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530414 /* Do not set XferState to zero unless receives a response from F/W */
415 if (status >= 0)
416 aac_fib_complete(fibptr);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
419 if (status >= 0) {
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400420 if ((aac_commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800422 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 dinfo = (struct aac_commit_config *) fib_data(fibptr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 dinfo->command = cpu_to_le32(VM_ContainerConfig);
426 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800427
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800428 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 fibptr,
430 sizeof (struct aac_commit_config),
431 FsaNormal,
432 1, 1,
433 NULL, NULL);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530434 /* Do not set XferState to zero unless
435 * receives a response from F/W */
436 if (status >= 0)
437 aac_fib_complete(fibptr);
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400438 } else if (aac_commit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 printk(KERN_WARNING
440 "aac_get_config_status: Foreign device configurations are being ignored\n");
441 }
442 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530443 /* FIB should be freed only after getting the response from the F/W */
444 if (status != -ERESTARTSYS)
445 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return status;
447}
448
Rajashekhara, Maheshe3cc2682010-05-10 04:12:28 -0700449static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
450{
451 char inq_data;
452 scsi_sg_copy_to_buffer(scsicmd, &inq_data, sizeof(inq_data));
453 if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
454 inq_data &= 0xdf;
455 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
456 }
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/**
460 * aac_get_containers - list containers
461 * @common: adapter to probe
462 *
463 * Make a list of all containers on this controller
464 */
465int aac_get_containers(struct aac_dev *dev)
466{
467 struct fsa_dev_info *fsa_dev_ptr;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800468 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 int status = 0;
470 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct aac_get_container_count *dinfo;
472 struct aac_get_container_count_resp *dresp;
473 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
474
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800475 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -ENOMEM;
477
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800478 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
480 dinfo->command = cpu_to_le32(VM_ContainerConfig);
481 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
482
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800483 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 fibptr,
485 sizeof (struct aac_get_container_count),
486 FsaNormal,
487 1, 1,
488 NULL, NULL);
489 if (status >= 0) {
490 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
491 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800492 if (fibptr->dev->supplement_adapter_info.supported_options2 &
Mahesh Rajashekharaa7129a52015-03-26 10:41:27 -0400493 AAC_OPTION_SUPPORTED_240_VOLUMES) {
494 maximum_num_containers =
495 le32_to_cpu(dresp->MaxSimpleVolumes);
496 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800497 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530499 /* FIB should be freed only after getting the response from the F/W */
500 if (status != -ERESTARTSYS)
501 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
504 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800505 if (dev->fsa_dev == NULL ||
506 dev->maximum_num_containers != maximum_num_containers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800508 fsa_dev_ptr = dev->fsa_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Raghava Aditya Renukunta3ffd6c52017-02-02 15:53:22 -0800510 dev->fsa_dev = kcalloc(maximum_num_containers,
511 sizeof(*fsa_dev_ptr), GFP_KERNEL);
512
513 kfree(fsa_dev_ptr);
514 fsa_dev_ptr = NULL;
515
516
517 if (!dev->fsa_dev)
518 return -ENOMEM;
519
520 dev->maximum_num_containers = maximum_num_containers;
521 }
522 for (index = 0; index < dev->maximum_num_containers; index++) {
523 dev->fsa_dev[index].devname[0] = '\0';
524 dev->fsa_dev[index].valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Mark Haverkampfe76df42007-03-15 12:55:07 -0700526 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Mark Haverkampfe76df42007-03-15 12:55:07 -0700528 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
530 break;
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return status;
534}
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536static void get_container_name_callback(void *context, struct fib * fibptr)
537{
538 struct aac_get_name_resp * get_name_reply;
539 struct scsi_cmnd * scsicmd;
540
541 scsicmd = (struct scsi_cmnd *) context;
542
Mark Haverkamp03d44332007-03-15 10:27:45 -0700543 if (!aac_valid_context(scsicmd, fibptr))
544 return;
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700547 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
550 /* Failure is irrelevant, using default value instead */
551 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
552 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700553 char *sp = get_name_reply->data;
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700554 int data_size = FIELD_SIZEOF(struct aac_get_name_resp, data);
555
556 sp[data_size - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 while (*sp == ' ')
558 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700559 if (*sp) {
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900560 struct inquiry_data inq;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700561 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
562 int count = sizeof(d);
563 char *dp = d;
564 do {
565 *dp++ = (*sp) ? *sp++ : ' ';
566 } while (--count > 0);
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900567
568 scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
569 memcpy(inq.inqd_pid, d, sizeof(d));
570 scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
575
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800576 aac_fib_complete(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700577 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/**
581 * aac_get_container_name - get container name, none blocking.
582 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700583static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 int status;
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700586 int data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct aac_get_name *dinfo;
588 struct fib * cmd_fibcontext;
589 struct aac_dev * dev;
590
591 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
592
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700593 data_size = FIELD_SIZEOF(struct aac_get_name_resp, data);
594
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -0800595 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800597 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
Brian King1ae948f2017-08-29 10:00:29 -0500599 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 dinfo->command = cpu_to_le32(VM_ContainerConfig);
602 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700603 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Raghava Aditya Renukuntac8026732017-08-04 03:51:41 -0700604 dinfo->count = cpu_to_le32(data_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800606 status = aac_fib_send(ContainerCommand,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800607 cmd_fibcontext,
Mahesh Rajashekharafb5d40d2015-08-28 06:38:33 -0400608 sizeof(struct aac_get_name_resp),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800609 FsaNormal,
610 0, 1,
611 (fib_callback)get_container_name_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 (void *) scsicmd);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Check that the command queued to the controller
616 */
Brian King1ae948f2017-08-29 10:00:29 -0500617 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800619
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800620 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
621 aac_fib_complete(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -1;
623}
624
Mark Haverkampfe76df42007-03-15 12:55:07 -0700625static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
626{
627 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
628
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400629 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700630 return aac_scsi_cmd(scsicmd);
631
632 scsicmd->result = DID_NO_CONNECT << 16;
633 scsicmd->scsi_done(scsicmd);
634 return 0;
635}
636
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400637static void _aac_probe_container2(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700638{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700639 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700640 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700641 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800642 int i;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700643
Mark Haverkamp03d44332007-03-15 10:27:45 -0700644
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400645 if (!aac_valid_context(scsicmd, fibptr))
646 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700647
648 scsicmd->SCp.Status = 0;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400649 fsa_dev_ptr = fibptr->dev->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700650 if (fsa_dev_ptr) {
651 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800652 __le32 sup_options2;
653
Mark Haverkampfe76df42007-03-15 12:55:07 -0700654 fsa_dev_ptr += scmd_id(scsicmd);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800655 sup_options2 =
656 fibptr->dev->supplement_adapter_info.supported_options2;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700657
658 if ((le32_to_cpu(dresp->status) == ST_OK) &&
659 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
660 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800661 if (!(sup_options2 & AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400662 dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
663 fsa_dev_ptr->block_size = 0x200;
664 } else {
665 fsa_dev_ptr->block_size =
666 le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
667 }
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800668 for (i = 0; i < 16; i++)
669 fsa_dev_ptr->identifier[i] =
670 dresp->mnt[0].fileinfo.bdevinfo
671 .identifier[i];
Mark Haverkampfe76df42007-03-15 12:55:07 -0700672 fsa_dev_ptr->valid = 1;
Mark Salyzyn655d7222008-04-30 16:03:42 -0400673 /* sense_key holds the current state of the spin-up */
674 if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
675 fsa_dev_ptr->sense_data.sense_key = NOT_READY;
676 else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
677 fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700678 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
679 fsa_dev_ptr->size
680 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
681 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
682 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
683 }
684 if ((fsa_dev_ptr->valid & 1) == 0)
685 fsa_dev_ptr->valid = 0;
686 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
687 }
688 aac_fib_complete(fibptr);
689 aac_fib_free(fibptr);
690 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
691 scsicmd->SCp.ptr = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400692 (*callback)(scsicmd);
693 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700694}
695
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400696static void _aac_probe_container1(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700697{
698 struct scsi_cmnd * scsicmd;
699 struct aac_mount * dresp;
700 struct aac_query_mount *dinfo;
701 int status;
702
703 dresp = (struct aac_mount *) fib_data(fibptr);
Dave Carroll6c92f7d2017-09-15 11:04:28 -0600704 if (!aac_supports_2T(fibptr->dev)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400705 dresp->mnt[0].capacityhigh = 0;
Dave Carroll6c92f7d2017-09-15 11:04:28 -0600706 if ((le32_to_cpu(dresp->status) == ST_OK) &&
707 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
708 _aac_probe_container2(context, fibptr);
709 return;
710 }
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400711 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700712 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700713
Mark Haverkamp03d44332007-03-15 10:27:45 -0700714 if (!aac_valid_context(scsicmd, fibptr))
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400715 return;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700716
Mark Haverkampfe76df42007-03-15 12:55:07 -0700717 aac_fib_init(fibptr);
718
719 dinfo = (struct aac_query_mount *)fib_data(fibptr);
720
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -0800721 if (fibptr->dev->supplement_adapter_info.supported_options2 &
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400722 AAC_OPTION_VARIABLE_BLOCK_SIZE)
723 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
724 else
725 dinfo->command = cpu_to_le32(VM_NameServe64);
726
Mark Haverkampfe76df42007-03-15 12:55:07 -0700727 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
728 dinfo->type = cpu_to_le32(FT_FILESYS);
Brian King1ae948f2017-08-29 10:00:29 -0500729 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700730
731 status = aac_fib_send(ContainerCommand,
732 fibptr,
733 sizeof(struct aac_query_mount),
734 FsaNormal,
735 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400736 _aac_probe_container2,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700737 (void *) scsicmd);
738 /*
739 * Check that the command queued to the controller
740 */
Brian King1ae948f2017-08-29 10:00:29 -0500741 if (status < 0 && status != -EINPROGRESS) {
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;
Brian King1ae948f2017-08-29 10:00:29 -0500769 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700770
771 status = aac_fib_send(ContainerCommand,
772 fibptr,
773 sizeof(struct aac_query_mount),
774 FsaNormal,
775 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400776 _aac_probe_container1,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700777 (void *) scsicmd);
778 /*
779 * Check that the command queued to the controller
780 */
Brian King1ae948f2017-08-29 10:00:29 -0500781 if (status == -EINPROGRESS)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700782 return 0;
Brian King1ae948f2017-08-29 10:00:29 -0500783
Mark Haverkampfe76df42007-03-15 12:55:07 -0700784 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]) {
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800918 int c;
Raghava Aditya Renukuntaf4e87082017-12-26 20:34:22 -0800919 char *cp;
920 char *cname = kmemdup(sup_adap_info->adapter_type_text,
921 sizeof(sup_adap_info->adapter_type_text),
922 GFP_ATOMIC);
Raghava Aditya Renukuntaf4e87082017-12-26 20:34:22 -0800923 if (!cname)
924 return;
925
926 cp = cname;
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800927 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
928 inqstrcpy("SMC", str->vid);
929 else {
930 c = sizeof(str->vid);
931 while (*cp && *cp != ' ' && --c)
932 ++cp;
933 c = *cp;
934 *cp = '\0';
Raghava Aditya Renukuntaf4e87082017-12-26 20:34:22 -0800935 inqstrcpy(cname, str->vid);
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800936 *cp = c;
937 while (*cp && *cp != ' ')
938 ++cp;
939 }
Mark Haverkamp794d0602005-10-24 10:51:53 -0700940 while (*cp == ' ')
941 ++cp;
942 /* last six chars reserved for vol type */
943 c = 0;
944 if (strlen(cp) > sizeof(str->pid)) {
945 c = cp[sizeof(str->pid)];
946 cp[sizeof(str->pid)] = '\0';
947 }
948 inqstrcpy (cp, str->pid);
Raghava Aditya Renukuntaf4e87082017-12-26 20:34:22 -0800949
950 kfree(cname);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700951 } else {
952 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700953
954 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700955 /* last six chars reserved for vol type */
956 inqstrcpy (mp->model, str->pid);
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Tobias Klauser6391a112006-06-08 22:23:48 -0700959 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 char *findit = str->pid;
961
962 for ( ; *findit != ' '; findit++); /* walk till we find a space */
963 /* RAID is superfluous in the context of a RAID device */
964 if (memcmp(findit-4, "RAID", 4) == 0)
965 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700966 if (((findit - str->pid) + strlen(container_types[tindex]))
967 < (sizeof(str->pid) + sizeof(str->prl)))
968 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 inqstrcpy ("V1.0", str->prl);
971}
972
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -0800973static void build_vpd83_type3(struct tvpd_page83 *vpdpage83data,
974 struct aac_dev *dev, struct scsi_cmnd *scsicmd)
975{
976 int container;
977
978 vpdpage83data->type3.codeset = 1;
979 vpdpage83data->type3.identifiertype = 3;
980 vpdpage83data->type3.identifierlength = sizeof(vpdpage83data->type3)
981 - 4;
982
983 for (container = 0; container < dev->maximum_num_containers;
984 container++) {
985
986 if (scmd_id(scsicmd) == container) {
987 memcpy(vpdpage83data->type3.Identifier,
988 dev->fsa_dev[container].identifier,
989 16);
990 break;
991 }
992 }
993}
994
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400995static void get_container_serial_callback(void *context, struct fib * fibptr)
996{
997 struct aac_get_serial_resp * get_serial_reply;
998 struct scsi_cmnd * scsicmd;
999
1000 BUG_ON(fibptr == NULL);
1001
1002 scsicmd = (struct scsi_cmnd *) context;
1003 if (!aac_valid_context(scsicmd, fibptr))
1004 return;
1005
1006 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
1007 /* Failure is irrelevant, using default value instead */
1008 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001009 /*Check to see if it's for VPD 0x83 or 0x80 */
1010 if (scsicmd->cmnd[2] == 0x83) {
1011 /* vpd page 0x83 - Device Identification Page */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001012 struct aac_dev *dev;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001013 int i;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001014 struct tvpd_page83 vpdpage83data;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001015
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001016 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1017
1018 memset(((u8 *)&vpdpage83data), 0,
1019 sizeof(vpdpage83data));
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001020
1021 /* DIRECT_ACCESS_DEVIC */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001022 vpdpage83data.DeviceType = 0;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001023 /* DEVICE_CONNECTED */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001024 vpdpage83data.DeviceTypeQualifier = 0;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001025 /* VPD_DEVICE_IDENTIFIERS */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001026 vpdpage83data.PageCode = 0x83;
1027 vpdpage83data.reserved = 0;
1028 vpdpage83data.PageLength =
1029 sizeof(vpdpage83data.type1) +
1030 sizeof(vpdpage83data.type2);
1031
1032 /* VPD 83 Type 3 is not supported for ARC */
1033 if (dev->sa_firmware)
1034 vpdpage83data.PageLength +=
1035 sizeof(vpdpage83data.type3);
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001036
1037 /* T10 Vendor Identifier Field Format */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001038 /* VpdcodesetAscii */
1039 vpdpage83data.type1.codeset = 2;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001040 /* VpdIdentifierTypeVendorId */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001041 vpdpage83data.type1.identifiertype = 1;
1042 vpdpage83data.type1.identifierlength =
1043 sizeof(vpdpage83data.type1) - 4;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001044
1045 /* "ADAPTEC " for adaptec */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001046 memcpy(vpdpage83data.type1.venid,
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001047 "ADAPTEC ",
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001048 sizeof(vpdpage83data.type1.venid));
1049 memcpy(vpdpage83data.type1.productid,
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001050 "ARRAY ",
1051 sizeof(
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001052 vpdpage83data.type1.productid));
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001053
1054 /* Convert to ascii based serial number.
1055 * The LSB is the the end.
1056 */
1057 for (i = 0; i < 8; i++) {
1058 u8 temp =
1059 (u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
1060 if (temp > 0x9) {
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001061 vpdpage83data.type1.serialnumber[i] =
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001062 'A' + (temp - 0xA);
1063 } else {
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001064 vpdpage83data.type1.serialnumber[i] =
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001065 '0' + temp;
1066 }
1067 }
1068
1069 /* VpdCodeSetBinary */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001070 vpdpage83data.type2.codeset = 1;
1071 /* VpdidentifiertypeEUI64 */
1072 vpdpage83data.type2.identifiertype = 2;
1073 vpdpage83data.type2.identifierlength =
1074 sizeof(vpdpage83data.type2) - 4;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001075
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001076 vpdpage83data.type2.eu64id.venid[0] = 0xD0;
1077 vpdpage83data.type2.eu64id.venid[1] = 0;
1078 vpdpage83data.type2.eu64id.venid[2] = 0;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001079
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001080 vpdpage83data.type2.eu64id.Serial =
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001081 get_serial_reply->uid;
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001082 vpdpage83data.type2.eu64id.reserved = 0;
1083
1084 /*
1085 * VpdIdentifierTypeFCPHName
1086 * VPD 0x83 Type 3 not supported for ARC
1087 */
1088 if (dev->sa_firmware) {
1089 build_vpd83_type3(&vpdpage83data,
1090 dev, scsicmd);
1091 }
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001092
1093 /* Move the inquiry data to the response buffer. */
Raghava Aditya Renukunta999b3ff2017-02-02 15:53:32 -08001094 scsi_sg_copy_from_buffer(scsicmd, &vpdpage83data,
1095 sizeof(vpdpage83data));
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04001096 } else {
1097 /* It must be for VPD 0x80 */
1098 char sp[13];
1099 /* EVPD bit set */
1100 sp[0] = INQD_PDT_DA;
1101 sp[1] = scsicmd->cmnd[2];
1102 sp[2] = 0;
1103 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1104 le32_to_cpu(get_serial_reply->uid));
1105 scsi_sg_copy_from_buffer(scsicmd, sp,
1106 sizeof(sp));
1107 }
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001108 }
1109
1110 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1111
1112 aac_fib_complete(fibptr);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001113 scsicmd->scsi_done(scsicmd);
1114}
1115
1116/**
1117 * aac_get_container_serial - get container serial, none blocking.
1118 */
1119static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1120{
1121 int status;
1122 struct aac_get_serial *dinfo;
1123 struct fib * cmd_fibcontext;
1124 struct aac_dev * dev;
1125
1126 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1127
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08001128 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001129
1130 aac_fib_init(cmd_fibcontext);
1131 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1132
1133 dinfo->command = cpu_to_le32(VM_ContainerConfig);
1134 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1135 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Brian King1ae948f2017-08-29 10:00:29 -05001136 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001137
1138 status = aac_fib_send(ContainerCommand,
1139 cmd_fibcontext,
Mahesh Rajashekharafb5d40d2015-08-28 06:38:33 -04001140 sizeof(struct aac_get_serial_resp),
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001141 FsaNormal,
1142 0, 1,
1143 (fib_callback) get_container_serial_callback,
1144 (void *) scsicmd);
1145
1146 /*
1147 * Check that the command queued to the controller
1148 */
Brian King1ae948f2017-08-29 10:00:29 -05001149 if (status == -EINPROGRESS)
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001150 return 0;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001151
1152 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1153 aac_fib_complete(cmd_fibcontext);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001154 return -1;
1155}
1156
1157/* Function: setinqserial
1158 *
1159 * Arguments: [1] pointer to void [1] int
1160 *
1161 * Purpose: Sets SCSI Unit Serial number.
1162 * This is a fake. We should read a proper
1163 * serial number from the container. <SuSE>But
1164 * without docs it's quite hard to do it :-)
1165 * So this will have to do in the meantime.</SuSE>
1166 */
1167
1168static int setinqserial(struct aac_dev *dev, void *data, int cid)
1169{
1170 /*
1171 * This breaks array migration.
1172 */
1173 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1174 le32_to_cpu(dev->adapter_info.serial[0]), cid);
1175}
1176
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001177static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1178 u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001180 u8 *sense_buf = (u8 *)sense_data;
1181 /* Sense data valid, err code 70h */
1182 sense_buf[0] = 0x70; /* No info field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 sense_buf[1] = 0; /* Segment number, always zero */
1184
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001185 sense_buf[2] = sense_key; /* Sense key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 sense_buf[12] = sense_code; /* Additional sense code */
1188 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (sense_key == ILLEGAL_REQUEST) {
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001191 sense_buf[7] = 10; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001193 sense_buf[15] = bit_pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* Illegal parameter is in the parameter block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (sense_code == SENCODE_INVALID_CDB_FIELD)
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001196 sense_buf[15] |= 0xc0;/* Std sense key specific field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* Illegal parameter is in the CDB block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 sense_buf[16] = field_pointer >> 8; /* MSB */
1199 sense_buf[17] = field_pointer; /* LSB */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001200 } else
1201 sense_buf[7] = 6; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001204static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1205{
1206 if (lba & 0xffffffff00000000LL) {
1207 int cid = scmd_id(cmd);
1208 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1209 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1210 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001211 set_sense(&dev->fsa_dev[cid].sense_data,
1212 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1213 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001214 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08001215 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1216 SCSI_SENSE_BUFFERSIZE));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001217 cmd->scsi_done(cmd);
1218 return 1;
1219 }
1220 return 0;
1221}
1222
1223static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1224{
1225 return 0;
1226}
1227
1228static void io_callback(void *context, struct fib * fibptr);
1229
1230static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1231{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301232 struct aac_dev *dev = fib->dev;
1233 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301234 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001235
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301236 aac_fib_init(fib);
Raghava Aditya Renukuntad1ef4da2017-02-02 15:53:17 -08001237 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1238 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1239 !dev->sync_mode) {
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301240 struct aac_raw_io2 *readcmd2;
1241 readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1242 memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1243 readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1244 readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001245 readcmd2->byteCount = cpu_to_le32(count *
1246 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301247 readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1248 readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301249 ret = aac_build_sgraw2(cmd, readcmd2,
1250 dev->scsi_host_ptr->sg_tablesize);
1251 if (ret < 0)
1252 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301253 command = ContainerRawIo2;
1254 fibsize = sizeof(struct aac_raw_io2) +
1255 ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1256 } else {
1257 struct aac_raw_io *readcmd;
1258 readcmd = (struct aac_raw_io *) fib_data(fib);
1259 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1260 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001261 readcmd->count = cpu_to_le32(count *
1262 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301263 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1264 readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1265 readcmd->bpTotal = 0;
1266 readcmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301267 ret = aac_build_sgraw(cmd, &readcmd->sg);
1268 if (ret < 0)
1269 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301270 command = ContainerRawIo;
1271 fibsize = sizeof(struct aac_raw_io) +
1272 ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1273 }
1274
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001275 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1276 /*
1277 * Now send the Fib to the adapter
1278 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301279 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001280 fib,
1281 fibsize,
1282 FsaNormal,
1283 0, 1,
1284 (fib_callback) io_callback,
1285 (void *) cmd);
1286}
1287
1288static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1289{
1290 u16 fibsize;
1291 struct aac_read64 *readcmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301292 long ret;
1293
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001294 aac_fib_init(fib);
1295 readcmd = (struct aac_read64 *) fib_data(fib);
1296 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1297 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1298 readcmd->sector_count = cpu_to_le16(count);
1299 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1300 readcmd->pad = 0;
1301 readcmd->flags = 0;
1302
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301303 ret = aac_build_sg64(cmd, &readcmd->sg);
1304 if (ret < 0)
1305 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001306 fibsize = sizeof(struct aac_read64) +
1307 ((le32_to_cpu(readcmd->sg.count) - 1) *
1308 sizeof (struct sgentry64));
1309 BUG_ON (fibsize > (fib->dev->max_fib_size -
1310 sizeof(struct aac_fibhdr)));
1311 /*
1312 * Now send the Fib to the adapter
1313 */
1314 return aac_fib_send(ContainerCommand64,
1315 fib,
1316 fibsize,
1317 FsaNormal,
1318 0, 1,
1319 (fib_callback) io_callback,
1320 (void *) cmd);
1321}
1322
1323static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1324{
1325 u16 fibsize;
1326 struct aac_read *readcmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001327 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301328 long ret;
1329
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001330 aac_fib_init(fib);
1331 readcmd = (struct aac_read *) fib_data(fib);
1332 readcmd->command = cpu_to_le32(VM_CtBlockRead);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001333 readcmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001334 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001335 readcmd->count = cpu_to_le32(count *
1336 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001337
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301338 ret = aac_build_sg(cmd, &readcmd->sg);
1339 if (ret < 0)
1340 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001341 fibsize = sizeof(struct aac_read) +
1342 ((le32_to_cpu(readcmd->sg.count) - 1) *
1343 sizeof (struct sgentry));
1344 BUG_ON (fibsize > (fib->dev->max_fib_size -
1345 sizeof(struct aac_fibhdr)));
1346 /*
1347 * Now send the Fib to the adapter
1348 */
1349 return aac_fib_send(ContainerCommand,
1350 fib,
1351 fibsize,
1352 FsaNormal,
1353 0, 1,
1354 (fib_callback) io_callback,
1355 (void *) cmd);
1356}
1357
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001358static 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 -08001359{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301360 struct aac_dev *dev = fib->dev;
1361 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301362 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001363
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301364 aac_fib_init(fib);
Raghava Aditya Renukuntad1ef4da2017-02-02 15:53:17 -08001365 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1366 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1367 !dev->sync_mode) {
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301368 struct aac_raw_io2 *writecmd2;
1369 writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1370 memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1371 writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1372 writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001373 writecmd2->byteCount = cpu_to_le32(count *
1374 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301375 writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1376 writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1377 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1378 cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1379 cpu_to_le16(RIO2_IO_TYPE_WRITE);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301380 ret = aac_build_sgraw2(cmd, writecmd2,
1381 dev->scsi_host_ptr->sg_tablesize);
1382 if (ret < 0)
1383 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301384 command = ContainerRawIo2;
1385 fibsize = sizeof(struct aac_raw_io2) +
1386 ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1387 } else {
1388 struct aac_raw_io *writecmd;
1389 writecmd = (struct aac_raw_io *) fib_data(fib);
1390 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1391 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001392 writecmd->count = cpu_to_le32(count *
1393 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301394 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1395 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1396 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1397 cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1398 cpu_to_le16(RIO_TYPE_WRITE);
1399 writecmd->bpTotal = 0;
1400 writecmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301401 ret = aac_build_sgraw(cmd, &writecmd->sg);
1402 if (ret < 0)
1403 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301404 command = ContainerRawIo;
1405 fibsize = sizeof(struct aac_raw_io) +
1406 ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1407 }
1408
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001409 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1410 /*
1411 * Now send the Fib to the adapter
1412 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301413 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001414 fib,
1415 fibsize,
1416 FsaNormal,
1417 0, 1,
1418 (fib_callback) io_callback,
1419 (void *) cmd);
1420}
1421
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001422static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001423{
1424 u16 fibsize;
1425 struct aac_write64 *writecmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301426 long ret;
1427
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001428 aac_fib_init(fib);
1429 writecmd = (struct aac_write64 *) fib_data(fib);
1430 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1431 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1432 writecmd->sector_count = cpu_to_le16(count);
1433 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1434 writecmd->pad = 0;
1435 writecmd->flags = 0;
1436
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301437 ret = aac_build_sg64(cmd, &writecmd->sg);
1438 if (ret < 0)
1439 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001440 fibsize = sizeof(struct aac_write64) +
1441 ((le32_to_cpu(writecmd->sg.count) - 1) *
1442 sizeof (struct sgentry64));
1443 BUG_ON (fibsize > (fib->dev->max_fib_size -
1444 sizeof(struct aac_fibhdr)));
1445 /*
1446 * Now send the Fib to the adapter
1447 */
1448 return aac_fib_send(ContainerCommand64,
1449 fib,
1450 fibsize,
1451 FsaNormal,
1452 0, 1,
1453 (fib_callback) io_callback,
1454 (void *) cmd);
1455}
1456
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001457static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001458{
1459 u16 fibsize;
1460 struct aac_write *writecmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001461 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301462 long ret;
1463
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001464 aac_fib_init(fib);
1465 writecmd = (struct aac_write *) fib_data(fib);
1466 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001467 writecmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001468 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001469 writecmd->count = cpu_to_le32(count *
1470 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001471 writecmd->sg.count = cpu_to_le32(1);
1472 /* ->stable is not used - it did mean which type of write */
1473
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301474 ret = aac_build_sg(cmd, &writecmd->sg);
1475 if (ret < 0)
1476 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001477 fibsize = sizeof(struct aac_write) +
1478 ((le32_to_cpu(writecmd->sg.count) - 1) *
1479 sizeof (struct sgentry));
1480 BUG_ON (fibsize > (fib->dev->max_fib_size -
1481 sizeof(struct aac_fibhdr)));
1482 /*
1483 * Now send the Fib to the adapter
1484 */
1485 return aac_fib_send(ContainerCommand,
1486 fib,
1487 fibsize,
1488 FsaNormal,
1489 0, 1,
1490 (fib_callback) io_callback,
1491 (void *) cmd);
1492}
1493
1494static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1495{
1496 struct aac_srb * srbcmd;
1497 u32 flag;
1498 u32 timeout;
1499
1500 aac_fib_init(fib);
1501 switch(cmd->sc_data_direction){
1502 case DMA_TO_DEVICE:
1503 flag = SRB_DataOut;
1504 break;
1505 case DMA_BIDIRECTIONAL:
1506 flag = SRB_DataIn | SRB_DataOut;
1507 break;
1508 case DMA_FROM_DEVICE:
1509 flag = SRB_DataIn;
1510 break;
1511 case DMA_NONE:
1512 default: /* shuts up some versions of gcc */
1513 flag = SRB_NoDataXfer;
1514 break;
1515 }
1516
1517 srbcmd = (struct aac_srb*) fib_data(fib);
1518 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1519 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1520 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1521 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1522 srbcmd->flags = cpu_to_le32(flag);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001523 timeout = cmd->request->timeout/HZ;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001524 if (timeout == 0)
1525 timeout = 1;
1526 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1527 srbcmd->retry_limit = 0; /* Obsolete parameter */
1528 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1529 return srbcmd;
1530}
1531
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08001532static struct aac_hba_cmd_req *aac_construct_hbacmd(struct fib *fib,
1533 struct scsi_cmnd *cmd)
1534{
1535 struct aac_hba_cmd_req *hbacmd;
1536 struct aac_dev *dev;
1537 int bus, target;
1538 u64 address;
1539
1540 dev = (struct aac_dev *)cmd->device->host->hostdata;
1541
1542 hbacmd = (struct aac_hba_cmd_req *)fib->hw_fib_va;
1543 memset(hbacmd, 0, 96); /* sizeof(*hbacmd) is not necessary */
1544 /* iu_type is a parameter of aac_hba_send */
1545 switch (cmd->sc_data_direction) {
1546 case DMA_TO_DEVICE:
1547 hbacmd->byte1 = 2;
1548 break;
1549 case DMA_FROM_DEVICE:
1550 case DMA_BIDIRECTIONAL:
1551 hbacmd->byte1 = 1;
1552 break;
1553 case DMA_NONE:
1554 default:
1555 break;
1556 }
1557 hbacmd->lun[1] = cpu_to_le32(cmd->device->lun);
1558
1559 bus = aac_logical_to_phys(scmd_channel(cmd));
1560 target = scmd_id(cmd);
1561 hbacmd->it_nexus = dev->hba_map[bus][target].rmw_nexus;
1562
1563 /* we fill in reply_qid later in aac_src_deliver_message */
1564 /* we fill in iu_type, request_id later in aac_hba_send */
1565 /* we fill in emb_data_desc_count later in aac_build_sghba */
1566
1567 memcpy(hbacmd->cdb, cmd->cmnd, cmd->cmd_len);
1568 hbacmd->data_length = cpu_to_le32(scsi_bufflen(cmd));
1569
1570 address = (u64)fib->hw_error_pa;
1571 hbacmd->error_ptr_hi = cpu_to_le32((u32)(address >> 32));
1572 hbacmd->error_ptr_lo = cpu_to_le32((u32)(address & 0xffffffff));
1573 hbacmd->error_length = cpu_to_le32(FW_ERROR_BUFFER_SIZE);
1574
1575 return hbacmd;
1576}
1577
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001578static void aac_srb_callback(void *context, struct fib * fibptr);
1579
1580static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1581{
1582 u16 fibsize;
1583 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301584 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001585
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301586 ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1587 if (ret < 0)
1588 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001589 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001590
1591 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1592 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1593 /*
1594 * Build Scatter/Gather list
1595 */
1596 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1597 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1598 sizeof (struct sgentry64));
1599 BUG_ON (fibsize > (fib->dev->max_fib_size -
1600 sizeof(struct aac_fibhdr)));
1601
1602 /*
1603 * Now send the Fib to the adapter
1604 */
1605 return aac_fib_send(ScsiPortCommand64, fib,
1606 fibsize, FsaNormal, 0, 1,
1607 (fib_callback) aac_srb_callback,
1608 (void *) cmd);
1609}
1610
1611static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1612{
1613 u16 fibsize;
1614 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301615 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001616
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301617 ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1618 if (ret < 0)
1619 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001620 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001621
1622 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1623 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1624 /*
1625 * Build Scatter/Gather list
1626 */
1627 fibsize = sizeof (struct aac_srb) +
1628 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1629 sizeof (struct sgentry));
1630 BUG_ON (fibsize > (fib->dev->max_fib_size -
1631 sizeof(struct aac_fibhdr)));
1632
1633 /*
1634 * Now send the Fib to the adapter
1635 */
1636 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1637 (fib_callback) aac_srb_callback, (void *) cmd);
1638}
1639
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001640static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1641{
Leubner, Achimd8e965072009-04-01 07:16:08 -07001642 if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1643 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001644 return FAILED;
1645 return aac_scsi_32(fib, cmd);
1646}
1647
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08001648static int aac_adapter_hba(struct fib *fib, struct scsi_cmnd *cmd)
1649{
1650 struct aac_hba_cmd_req *hbacmd = aac_construct_hbacmd(fib, cmd);
1651 struct aac_dev *dev;
1652 long ret;
1653
1654 dev = (struct aac_dev *)cmd->device->host->hostdata;
1655
1656 ret = aac_build_sghba(cmd, hbacmd,
1657 dev->scsi_host_ptr->sg_tablesize, (u64)fib->hw_sgl_pa);
1658 if (ret < 0)
1659 return ret;
1660
1661 /*
1662 * Now send the HBA command to the adapter
1663 */
1664 fib->hbacmd_size = 64 + le32_to_cpu(hbacmd->emb_data_desc_count) *
1665 sizeof(struct aac_hba_sgl);
1666
1667 return aac_hba_send(HBA_IU_TYPE_SCSI_CMD_REQ, fib,
1668 (fib_callback) aac_hba_callback,
1669 (void *) cmd);
1670}
1671
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001672static int aac_send_safw_bmic_cmd(struct aac_dev *dev,
1673 struct aac_srb_unit *srbu, void *xfer_buf, int xfer_len)
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001674{
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001675 struct fib *fibptr;
1676 dma_addr_t addr;
1677 int rcode;
1678 int fibsize;
1679 struct aac_srb *srb;
1680 struct aac_srb_reply *srb_reply;
1681 struct sgmap64 *sg64;
1682 u32 vbus;
1683 u32 vid;
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001684
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001685 if (!dev->sa_firmware)
1686 return 0;
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08001687
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001688 /* allocate FIB */
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001689 fibptr = aac_fib_alloc(dev);
1690 if (!fibptr)
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001691 return -ENOMEM;
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001692
1693 aac_fib_init(fibptr);
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001694 fibptr->hw_fib_va->header.XferState &=
1695 ~cpu_to_le32(FastResponseCapable);
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001696
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001697 fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry) +
1698 sizeof(struct sgentry64);
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001699
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001700 /* allocate DMA buffer for response */
1701 addr = dma_map_single(&dev->pdev->dev, xfer_buf, xfer_len,
1702 DMA_BIDIRECTIONAL);
1703 if (dma_mapping_error(&dev->pdev->dev, addr)) {
1704 rcode = -ENOMEM;
1705 goto fib_error;
1706 }
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001707
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001708 srb = fib_data(fibptr);
1709 memcpy(srb, &srbu->srb, sizeof(struct aac_srb));
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001710
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001711 vbus = (u32)le16_to_cpu(
1712 dev->supplement_adapter_info.virt_device_bus);
1713 vid = (u32)le16_to_cpu(
1714 dev->supplement_adapter_info.virt_device_target);
1715
1716 /* set the common request fields */
1717 srb->channel = cpu_to_le32(vbus);
1718 srb->id = cpu_to_le32(vid);
1719 srb->lun = 0;
1720 srb->function = cpu_to_le32(SRBF_ExecuteScsi);
1721 srb->timeout = 0;
1722 srb->retry_limit = 0;
1723 srb->cdb_size = cpu_to_le32(16);
1724 srb->count = cpu_to_le32(xfer_len);
1725
1726 sg64 = (struct sgmap64 *)&srb->sg;
1727 sg64->count = cpu_to_le32(1);
1728 sg64->sg[0].addr[1] = cpu_to_le32(upper_32_bits(addr));
1729 sg64->sg[0].addr[0] = cpu_to_le32(lower_32_bits(addr));
1730 sg64->sg[0].count = cpu_to_le32(xfer_len);
1731
1732 /*
1733 * Copy the updated data for other dumping or other usage if needed
1734 */
1735 memcpy(&srbu->srb, srb, sizeof(struct aac_srb));
1736
1737 /* issue request to the controller */
1738 rcode = aac_fib_send(ScsiPortCommand64, fibptr, fibsize, FsaNormal,
1739 1, 1, NULL, NULL);
1740
1741 if (rcode == -ERESTARTSYS)
1742 rcode = -ERESTART;
1743
1744 if (unlikely(rcode < 0))
1745 goto bmic_error;
1746
1747 srb_reply = (struct aac_srb_reply *)fib_data(fibptr);
1748 memcpy(&srbu->srb_reply, srb_reply, sizeof(struct aac_srb_reply));
1749
1750bmic_error:
1751 dma_unmap_single(&dev->pdev->dev, addr, xfer_len, DMA_BIDIRECTIONAL);
1752fib_error:
1753 aac_fib_complete(fibptr);
1754 aac_fib_free(fibptr);
1755 return rcode;
1756}
1757
Raghava Aditya Renukunta0bcb45f2017-12-26 20:34:38 -08001758static void aac_set_safw_target_qd(struct aac_dev *dev, int bus, int target)
1759{
1760
1761 struct aac_ciss_identify_pd *identify_resp;
1762
1763 if (dev->hba_map[bus][target].devtype != AAC_DEVTYPE_NATIVE_RAW)
1764 return;
1765
1766 identify_resp = dev->hba_map[bus][target].safw_identify_resp;
1767 if (identify_resp == NULL) {
1768 dev->hba_map[bus][target].qd_limit = 32;
1769 return;
1770 }
1771
1772 if (identify_resp->current_queue_depth_limit <= 0 ||
1773 identify_resp->current_queue_depth_limit > 255)
1774 dev->hba_map[bus][target].qd_limit = 32;
1775 else
1776 dev->hba_map[bus][target].qd_limit =
1777 identify_resp->current_queue_depth_limit;
1778}
1779
Raghava Aditya Renukuntab5a475e2017-12-26 20:34:32 -08001780static int aac_issue_safw_bmic_identify(struct aac_dev *dev,
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001781 struct aac_ciss_identify_pd **identify_resp, u32 bus, u32 target)
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001782{
1783 int rcode = -ENOMEM;
Raghava Aditya Renukuntab5a475e2017-12-26 20:34:32 -08001784 int datasize;
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001785 struct aac_srb_unit srbu;
1786 struct aac_srb *srbcmd;
Raghava Aditya Renukuntab5a475e2017-12-26 20:34:32 -08001787 struct aac_ciss_identify_pd *identify_reply;
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001788
1789 datasize = sizeof(struct aac_ciss_identify_pd);
Raghava Aditya Renukuntab5a475e2017-12-26 20:34:32 -08001790 identify_reply = kmalloc(datasize, GFP_KERNEL);
1791 if (!identify_reply)
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001792 goto out;
1793
1794 memset(&srbu, 0, sizeof(struct aac_srb_unit));
1795
1796 srbcmd = &srbu.srb;
1797 srbcmd->flags = cpu_to_le32(SRB_DataIn);
1798 srbcmd->cdb[0] = 0x26;
1799 srbcmd->cdb[2] = (u8)((AAC_MAX_LUN + target) & 0x00FF);
1800 srbcmd->cdb[6] = CISS_IDENTIFY_PHYSICAL_DEVICE;
1801
Raghava Aditya Renukuntab5a475e2017-12-26 20:34:32 -08001802 rcode = aac_send_safw_bmic_cmd(dev, &srbu, identify_reply, datasize);
Raghava Aditya Renukunta8fb39182017-12-26 20:34:30 -08001803 if (unlikely(rcode < 0))
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001804 goto mem_free_all;
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001805
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001806 *identify_resp = identify_reply;
1807
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001808out:
1809 return rcode;
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001810mem_free_all:
1811 kfree(identify_reply);
1812 goto out;
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08001813}
1814
Raghava Aditya Renukuntaa25b6ca2017-12-26 20:34:35 -08001815static inline void aac_free_safw_ciss_luns(struct aac_dev *dev)
1816{
1817 kfree(dev->safw_phys_luns);
1818 dev->safw_phys_luns = NULL;
1819}
1820
1821/**
1822 * aac_get_safw_ciss_luns() Process topology change
1823 * @dev: aac_dev structure
1824 * @rescan: Indicates rescan
1825 *
1826 * Execute a CISS REPORT PHYS LUNS and process the results into
1827 * the current hba_map.
1828 */
1829static int aac_get_safw_ciss_luns(struct aac_dev *dev, int rescan)
1830{
1831 int rcode = -ENOMEM;
1832 int datasize;
1833 struct aac_srb *srbcmd;
1834 struct aac_srb_unit srbu;
1835 struct aac_ciss_phys_luns_resp *phys_luns;
1836
1837 datasize = sizeof(struct aac_ciss_phys_luns_resp) +
1838 (AAC_MAX_TARGETS - 1) * sizeof(struct _ciss_lun);
1839 phys_luns = kmalloc(datasize, GFP_KERNEL);
1840 if (phys_luns == NULL)
1841 goto out;
1842
1843 memset(&srbu, 0, sizeof(struct aac_srb_unit));
1844
1845 srbcmd = &srbu.srb;
1846 srbcmd->flags = cpu_to_le32(SRB_DataIn);
1847 srbcmd->cdb[0] = CISS_REPORT_PHYSICAL_LUNS;
1848 srbcmd->cdb[1] = 2; /* extended reporting */
1849 srbcmd->cdb[8] = (u8)(datasize >> 8);
1850 srbcmd->cdb[9] = (u8)(datasize);
1851
1852 rcode = aac_send_safw_bmic_cmd(dev, &srbu, phys_luns, datasize);
1853 if (unlikely(rcode < 0))
1854 goto mem_free_all;
1855
1856 if (phys_luns->resp_flag != 2) {
1857 rcode = -ENOMSG;
1858 goto mem_free_all;
1859 }
1860
1861 dev->safw_phys_luns = phys_luns;
1862
1863out:
1864 return rcode;
1865mem_free_all:
1866 kfree(phys_luns);
1867 goto out;
Raghava Aditya Renukunta4b000222017-12-26 20:34:36 -08001868}
Raghava Aditya Renukuntaa25b6ca2017-12-26 20:34:35 -08001869
Raghava Aditya Renukunta4b000222017-12-26 20:34:36 -08001870static inline u32 aac_get_safw_phys_lun_count(struct aac_dev *dev)
1871{
1872 return get_unaligned_be32(&dev->safw_phys_luns->list_length[0]);
1873}
1874
1875static inline u32 aac_get_safw_phys_bus(struct aac_dev *dev, int lun)
1876{
1877 return dev->safw_phys_luns->lun[lun].level2[1] & 0x3f;
1878}
1879
1880static inline u32 aac_get_safw_phys_target(struct aac_dev *dev, int lun)
1881{
1882 return dev->safw_phys_luns->lun[lun].level2[0];
1883}
1884
1885static inline u32 aac_get_safw_phys_expose_flag(struct aac_dev *dev, int lun)
1886{
1887 return dev->safw_phys_luns->lun[lun].bus >> 6;
1888}
1889
1890static inline u32 aac_get_safw_phys_attribs(struct aac_dev *dev, int lun)
1891{
1892 return dev->safw_phys_luns->lun[lun].node_ident[9];
1893}
1894
1895static inline u32 aac_get_safw_phys_nexus(struct aac_dev *dev, int lun)
1896{
1897 return *((u32 *)&dev->safw_phys_luns->lun[lun].node_ident[12]);
1898}
1899
1900static inline u32 aac_get_safw_phys_device_type(struct aac_dev *dev, int lun)
1901{
1902 return dev->safw_phys_luns->lun[lun].node_ident[8];
Raghava Aditya Renukuntaa25b6ca2017-12-26 20:34:35 -08001903}
1904
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001905static inline void aac_free_safw_identify_resp(struct aac_dev *dev,
1906 int bus, int target)
1907{
1908 kfree(dev->hba_map[bus][target].safw_identify_resp);
1909 dev->hba_map[bus][target].safw_identify_resp = NULL;
1910}
1911
1912static inline void aac_free_safw_all_identify_resp(struct aac_dev *dev,
1913 int lun_count)
1914{
1915 int luns;
1916 int i;
1917 u32 bus;
1918 u32 target;
1919
1920 luns = aac_get_safw_phys_lun_count(dev);
1921
1922 if (luns < lun_count)
1923 lun_count = luns;
1924 else if (lun_count < 0)
1925 lun_count = luns;
1926
1927 for (i = 0; i < lun_count; i++) {
1928 bus = aac_get_safw_phys_bus(dev, i);
1929 target = aac_get_safw_phys_target(dev, i);
1930
1931 aac_free_safw_identify_resp(dev, bus, target);
1932 }
1933}
1934
1935static int aac_get_safw_attr_all_targets(struct aac_dev *dev, int rescan)
1936{
1937 int i;
1938 int rcode = 0;
1939 u32 lun_count;
1940 u32 bus;
1941 u32 target;
1942 struct aac_ciss_identify_pd *identify_resp = NULL;
1943
1944 lun_count = aac_get_safw_phys_lun_count(dev);
1945
1946 for (i = 0; i < lun_count; ++i) {
1947
1948 bus = aac_get_safw_phys_bus(dev, i);
1949 target = aac_get_safw_phys_target(dev, i);
1950
1951 rcode = aac_issue_safw_bmic_identify(dev,
1952 &identify_resp, bus, target);
1953
Raghava Aditya Renukunta0bcb45f2017-12-26 20:34:38 -08001954 if (unlikely(rcode < 0))
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001955 goto free_identify_resp;
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001956
1957 dev->hba_map[bus][target].safw_identify_resp = identify_resp;
1958 }
1959
1960out:
1961 return rcode;
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08001962free_identify_resp:
1963 aac_free_safw_all_identify_resp(dev, i);
1964 goto out;
1965}
1966
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001967/**
Raghava Aditya Renukuntab5a475e2017-12-26 20:34:32 -08001968 * aac_set_safw_attr_all_targets- update current hba map with data from FW
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001969 * @dev: aac_dev structure
1970 * @phys_luns: FW information from report phys luns
Raghava Aditya Renukuntab5a475e2017-12-26 20:34:32 -08001971 * @rescan: Indicates scan type
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001972 *
1973 * Update our hba map with the information gathered from the FW
1974 */
Raghava Aditya Renukunta3edfb8b2017-12-26 20:34:34 -08001975static void aac_set_safw_attr_all_targets(struct aac_dev *dev, int rescan)
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001976{
1977 /* ok and extended reporting */
1978 u32 lun_count, nexus;
1979 u32 i, bus, target;
1980 u8 expose_flag, attribs;
1981 u8 devtype;
Raghava Aditya Renukunta3edfb8b2017-12-26 20:34:34 -08001982
Raghava Aditya Renukunta4b000222017-12-26 20:34:36 -08001983 lun_count = aac_get_safw_phys_lun_count(dev);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001984
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08001985 dev->scan_counter++;
1986
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001987 for (i = 0; i < lun_count; ++i) {
1988
Raghava Aditya Renukunta4b000222017-12-26 20:34:36 -08001989 bus = aac_get_safw_phys_bus(dev, i);
1990 target = aac_get_safw_phys_target(dev, i);
1991 expose_flag = aac_get_safw_phys_expose_flag(dev, i);
1992 attribs = aac_get_safw_phys_attribs(dev, i);
1993 nexus = aac_get_safw_phys_nexus(dev, i);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08001994
1995 if (bus >= AAC_MAX_BUSES || target >= AAC_MAX_TARGETS)
1996 continue;
1997
1998 dev->hba_map[bus][target].expose = expose_flag;
1999
2000 if (expose_flag != 0) {
2001 devtype = AAC_DEVTYPE_RAID_MEMBER;
2002 goto update_devtype;
2003 }
2004
2005 if (nexus != 0 && (attribs & 8)) {
2006 devtype = AAC_DEVTYPE_NATIVE_RAW;
2007 dev->hba_map[bus][target].rmw_nexus =
2008 nexus;
2009 } else
2010 devtype = AAC_DEVTYPE_ARC_RAW;
2011
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08002012 dev->hba_map[bus][target].scan_counter = dev->scan_counter;
2013
Raghava Aditya Renukunta0bcb45f2017-12-26 20:34:38 -08002014 aac_set_safw_target_qd(dev, bus, target);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002015
2016update_devtype:
Raghava Aditya Renukuntaf2d2cab2017-12-26 20:34:40 -08002017 dev->hba_map[bus][target].devtype = devtype;
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002018 }
2019}
2020
Raghava Aditya Renukuntafc0fdd92017-12-26 20:34:33 -08002021static int aac_setup_safw_targets(struct aac_dev *dev, int rescan)
2022{
Raghava Aditya Renukunta3edfb8b2017-12-26 20:34:34 -08002023 int rcode = 0;
2024
Raghava Aditya Renukunta1d1fec52017-12-26 20:34:39 -08002025 rcode = aac_get_containers(dev);
2026 if (unlikely(rcode < 0))
2027 goto out;
2028
Raghava Aditya Renukunta3edfb8b2017-12-26 20:34:34 -08002029 rcode = aac_get_safw_ciss_luns(dev, rescan);
2030 if (unlikely(rcode < 0))
2031 goto out;
2032
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08002033 rcode = aac_get_safw_attr_all_targets(dev, rescan);
2034 if (unlikely(rcode < 0))
2035 goto free_ciss_luns;
2036
Raghava Aditya Renukunta3edfb8b2017-12-26 20:34:34 -08002037 aac_set_safw_attr_all_targets(dev, rescan);
2038
Raghava Aditya Renukuntae2ee8c92017-12-26 20:34:37 -08002039 aac_free_safw_all_identify_resp(dev, -1);
2040free_ciss_luns:
Raghava Aditya Renukunta3edfb8b2017-12-26 20:34:34 -08002041 aac_free_safw_ciss_luns(dev);
2042out:
2043 return rcode;
Raghava Aditya Renukuntafc0fdd92017-12-26 20:34:33 -08002044}
2045
2046int aac_setup_safw_adapter(struct aac_dev *dev, int rescan)
2047{
2048 return aac_setup_safw_targets(dev, rescan);
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051int aac_get_adapter_info(struct aac_dev* dev)
2052{
2053 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 int rcode;
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002055 u32 tmp, bus, target;
Mark Haverkamp84971732005-06-20 11:55:24 -07002056 struct aac_adapter_info *info;
2057 struct aac_bus_info *command;
2058 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002059
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002060 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return -ENOMEM;
2062
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002063 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002064 info = (struct aac_adapter_info *) fib_data(fibptr);
2065 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002067 rcode = aac_fib_send(RequestAdapterInfo,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002068 fibptr,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002069 sizeof(*info),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002070 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07002071 -1, 1, /* First `interrupt' command uses special wait */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002072 NULL,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002073 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002075 if (rcode < 0) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05302076 /* FIB should be freed only after
2077 * getting the response from the F/W */
2078 if (rcode != -ERESTARTSYS) {
2079 aac_fib_complete(fibptr);
2080 aac_fib_free(fibptr);
2081 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002082 return rcode;
2083 }
2084 memcpy(&dev->adapter_info, info, sizeof(*info));
2085
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002086 dev->supplement_adapter_info.virt_device_bus = 0xffff;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002087 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08002088 struct aac_supplement_adapter_info * sinfo;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002089
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002090 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002091
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08002092 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002093
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08002094 memset(sinfo,0,sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002095
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002096 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002097 fibptr,
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08002098 sizeof(*sinfo),
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002099 FsaNormal,
2100 1, 1,
2101 NULL,
2102 NULL);
2103
2104 if (rcode >= 0)
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08002105 memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05302106 if (rcode == -ERESTARTSYS) {
2107 fibptr = aac_fib_alloc(dev);
2108 if (!fibptr)
2109 return -ENOMEM;
2110 }
2111
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002114 /* reset all previous mapped devices (i.e. for init. after IOP_RESET) */
2115 for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08002116 for (target = 0; target < AAC_MAX_TARGETS; target++) {
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002117 dev->hba_map[bus][target].devtype = 0;
Raghava Aditya Renukunta71a91ca2017-02-02 15:53:26 -08002118 dev->hba_map[bus][target].qd_limit = 0;
2119 }
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002120 }
Mark Haverkamp84971732005-06-20 11:55:24 -07002121
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002122 /*
2123 * GetBusInfo
Mark Haverkamp84971732005-06-20 11:55:24 -07002124 */
2125
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002126 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07002127
2128 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
2129
2130 memset(bus_info, 0, sizeof(*bus_info));
2131
2132 command = (struct aac_bus_info *)bus_info;
2133
2134 command->Command = cpu_to_le32(VM_Ioctl);
2135 command->ObjType = cpu_to_le32(FT_DRIVE);
2136 command->MethodId = cpu_to_le32(1);
2137 command->CtlCmd = cpu_to_le32(GetBusInfo);
2138
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002139 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07002140 fibptr,
2141 sizeof (*bus_info),
2142 FsaNormal,
2143 1, 1,
2144 NULL, NULL);
2145
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08002146 /* reasoned default */
2147 dev->maximum_num_physicals = 16;
Mark Haverkamp84971732005-06-20 11:55:24 -07002148 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
2149 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
2150 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
2151 }
2152
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002153 if (!dev->sync_mode && dev->sa_firmware &&
Raghava Aditya Renukuntafc0fdd92017-12-26 20:34:33 -08002154 dev->supplement_adapter_info.virt_device_bus != 0xffff)
2155 rcode = aac_setup_safw_adapter(dev, AAC_INIT);
Raghava Aditya Renukuntac83b11e2017-02-02 15:53:19 -08002156
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002157 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04002158 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002159 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
2160 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002161 dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 dev->id,
2163 tmp>>24,
2164 (tmp>>16)&0xff,
2165 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002166 le32_to_cpu(dev->adapter_info.kernelbuild),
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002167 (int)sizeof(dev->supplement_adapter_info.build_date),
2168 dev->supplement_adapter_info.build_date);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002169 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
2170 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 dev->name, dev->id,
2172 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2173 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002174 tmp = le32_to_cpu(dev->adapter_info.biosrev);
2175 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 dev->name, dev->id,
2177 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
2178 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04002179 buffer[0] = '\0';
Tony Jonesee959b02008-02-22 00:13:36 +01002180 if (aac_get_serial_number(
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04002181 shost_to_class(dev->scsi_host_ptr), buffer))
2182 printk(KERN_INFO "%s%d: serial %s",
2183 dev->name, dev->id, buffer);
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002184 if (dev->supplement_adapter_info.vpd_info.tsid[0]) {
Salyzyn, Marka45c8632007-03-28 13:44:54 -04002185 printk(KERN_INFO "%s%d: TSID %.*s\n",
2186 dev->name, dev->id,
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002187 (int)sizeof(dev->supplement_adapter_info
2188 .vpd_info.tsid),
2189 dev->supplement_adapter_info.vpd_info.tsid);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04002190 }
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002191 if (!aac_check_reset || ((aac_check_reset == 1) &&
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002192 (dev->supplement_adapter_info.supported_options2 &
Salyzyn, Marka3940da2008-01-08 12:48:25 -08002193 AAC_OPTION_IGNORE_RESET))) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04002194 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
2195 dev->name, dev->id);
2196 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002199 dev->cache_protected = 0;
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002200 dev->jbod = ((dev->supplement_adapter_info.feature_bits &
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08002201 AAC_FEATURE_JBOD) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 dev->nondasd_support = 0;
2203 dev->raid_scsi_mode = 0;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002204 if(dev->adapter_info.options & AAC_OPT_NONDASD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 dev->nondasd_support = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /*
2208 * If the firmware supports ROMB RAID/SCSI mode and we are currently
2209 * in RAID/SCSI mode, set the flag. For now if in this mode we will
2210 * force nondasd support on. If we decide to allow the non-dasd flag
2211 * additional changes changes will have to be made to support
2212 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
2213 * changed to support the new dev->raid_scsi_mode flag instead of
2214 * leaching off of the dev->nondasd_support flag. Also in linit.c the
2215 * function aac_detect will have to be modified where it sets up the
2216 * max number of channels based on the aac->nondasd_support flag only.
2217 */
2218 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
2219 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
2220 dev->nondasd_support = 1;
2221 dev->raid_scsi_mode = 1;
2222 }
2223 if (dev->raid_scsi_mode != 0)
2224 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
2225 dev->name, dev->id);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002226
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002227 if (nondasd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 dev->nondasd_support = (nondasd!=0);
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002229 if (dev->nondasd_support && !dev->in_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Yang Hongyange9304382009-04-13 14:40:14 -07002232 if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
Leubner, Achimd8e965072009-04-01 07:16:08 -07002233 dev->needs_dac = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 dev->dac_support = 0;
Leubner, Achimd8e965072009-04-01 07:16:08 -07002235 if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
2236 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002237 if (!dev->in_reset)
2238 printk(KERN_INFO "%s%d: 64bit support enabled.\n",
2239 dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 dev->dac_support = 1;
2241 }
2242
2243 if(dacmode != -1) {
2244 dev->dac_support = (dacmode!=0);
2245 }
Leubner, Achimd8e965072009-04-01 07:16:08 -07002246
2247 /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
2248 if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
2249 & AAC_QUIRK_SCSI_32)) {
2250 dev->nondasd_support = 0;
2251 dev->jbod = 0;
2252 expose_physicals = 0;
2253 }
2254
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07002255 if (dev->dac_support) {
2256 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08002257 if (!dev->in_reset)
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07002258 dev_info(&dev->pdev->dev, "64 Bit DAC enabled\n");
2259 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
2260 dev_info(&dev->pdev->dev, "DMA mask set failed, 64 Bit DAC disabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 dev->dac_support = 0;
2262 } else {
Raghava Aditya Renukunta8105d392017-05-10 09:39:36 -07002263 dev_info(&dev->pdev->dev, "No suitable DMA available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 rcode = -ENOMEM;
2265 }
2266 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002267 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002268 * Deal with configuring for the individualized limits of each packet
2269 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002270 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002271 dev->a_ops.adapter_scsi = (dev->dac_support)
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08002272 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
2273 ? aac_scsi_32_64
2274 : aac_scsi_64)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002275 : aac_scsi_32;
2276 if (dev->raw_io_interface) {
2277 dev->a_ops.adapter_bounds = (dev->raw_io_64)
2278 ? aac_bounds_64
2279 : aac_bounds_32;
2280 dev->a_ops.adapter_read = aac_read_raw_io;
2281 dev->a_ops.adapter_write = aac_write_raw_io;
2282 } else {
2283 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002284 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002285 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07002286 sizeof(struct aac_write) + sizeof(struct sgentry)) /
2287 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002288 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002289 dev->a_ops.adapter_read = aac_read_block64;
2290 dev->a_ops.adapter_write = aac_write_block64;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002291 /*
2292 * 38 scatter gather elements
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002293 */
2294 dev->scsi_host_ptr->sg_tablesize =
2295 (dev->max_fib_size -
2296 sizeof(struct aac_fibhdr) -
2297 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07002298 sizeof(struct sgentry64)) /
2299 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002300 } else {
2301 dev->a_ops.adapter_read = aac_read_block;
2302 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002303 }
2304 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07002305 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07002306 /*
2307 * Worst case size that could cause sg overflow when
2308 * we break up SG elements that are larger than 64KB.
2309 * Would be nice if we could tell the SCSI layer what
2310 * the maximum SG element size can be. Worst case is
2311 * (sg_tablesize-1) 4KB elements with one 64KB
2312 * element.
2313 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
2314 */
2315 dev->scsi_host_ptr->max_sectors =
2316 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
2317 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002318 }
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08002319 if (!dev->sync_mode && dev->sa_firmware &&
2320 dev->scsi_host_ptr->sg_tablesize > HBA_MAX_SG_SEPARATE)
2321 dev->scsi_host_ptr->sg_tablesize = dev->sg_tablesize =
2322 HBA_MAX_SG_SEPARATE;
2323
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05302324 /* FIB should be freed only after getting the response from the F/W */
2325 if (rcode != -ERESTARTSYS) {
2326 aac_fib_complete(fibptr);
2327 aac_fib_free(fibptr);
2328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 return rcode;
2331}
2332
2333
Mark Haverkampe53cb352005-08-03 15:39:09 -07002334static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 struct aac_dev *dev;
2337 struct aac_read_reply *readreply;
2338 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 u32 cid;
2340
2341 scsicmd = (struct scsi_cmnd *) context;
2342
Mark Haverkamp03d44332007-03-15 10:27:45 -07002343 if (!aac_valid_context(scsicmd, fibptr))
2344 return;
2345
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002346 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08002347 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002349 if (nblank(dprintk(x))) {
2350 u64 lba;
2351 switch (scsicmd->cmnd[0]) {
2352 case WRITE_6:
2353 case READ_6:
2354 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2355 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2356 break;
2357 case WRITE_16:
2358 case READ_16:
2359 lba = ((u64)scsicmd->cmnd[2] << 56) |
2360 ((u64)scsicmd->cmnd[3] << 48) |
2361 ((u64)scsicmd->cmnd[4] << 40) |
2362 ((u64)scsicmd->cmnd[5] << 32) |
2363 ((u64)scsicmd->cmnd[6] << 24) |
2364 (scsicmd->cmnd[7] << 16) |
2365 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2366 break;
2367 case WRITE_12:
2368 case READ_12:
2369 lba = ((u64)scsicmd->cmnd[2] << 24) |
2370 (scsicmd->cmnd[3] << 16) |
2371 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2372 break;
2373 default:
2374 lba = ((u64)scsicmd->cmnd[2] << 24) |
2375 (scsicmd->cmnd[3] << 16) |
2376 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2377 break;
2378 }
2379 printk(KERN_DEBUG
2380 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
2381 smp_processor_id(), (unsigned long long)lba, jiffies);
2382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002384 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002385
2386 scsi_dma_unmap(scsicmd);
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 readreply = (struct aac_read_reply *)fib_data(fibptr);
Mark Salyzyn655d7222008-04-30 16:03:42 -04002389 switch (le32_to_cpu(readreply->status)) {
2390 case ST_OK:
2391 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2392 SAM_STAT_GOOD;
2393 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
2394 break;
2395 case ST_NOT_READY:
2396 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2397 SAM_STAT_CHECK_CONDITION;
2398 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
2399 SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
2400 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2401 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2402 SCSI_SENSE_BUFFERSIZE));
2403 break;
Raghava Aditya Renukuntaf956a662017-02-02 15:53:23 -08002404 case ST_MEDERR:
2405 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2406 SAM_STAT_CHECK_CONDITION;
2407 set_sense(&dev->fsa_dev[cid].sense_data, MEDIUM_ERROR,
2408 SENCODE_UNRECOVERED_READ_ERROR, ASENCODE_NO_SENSE, 0, 0);
2409 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2410 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2411 SCSI_SENSE_BUFFERSIZE));
2412 break;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002413 default:
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002414#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07002415 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002416 le32_to_cpu(readreply->status));
2417#endif
Mark Salyzyn655d7222008-04-30 16:03:42 -04002418 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2419 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002420 set_sense(&dev->fsa_dev[cid].sense_data,
2421 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2422 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002424 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2425 SCSI_SENSE_BUFFERSIZE));
Mark Salyzyn655d7222008-04-30 16:03:42 -04002426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002428 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002430 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002433static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002435 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 u32 count;
2437 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 struct aac_dev *dev;
2439 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002440 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2443 /*
2444 * Get block address and transfer length
2445 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002446 switch (scsicmd->cmnd[0]) {
2447 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002448 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002450 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002451 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 count = scsicmd->cmnd[4];
2453
2454 if (count == 0)
2455 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002456 break;
2457 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002458 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002459
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002460 lba = ((u64)scsicmd->cmnd[2] << 56) |
2461 ((u64)scsicmd->cmnd[3] << 48) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002462 ((u64)scsicmd->cmnd[4] << 40) |
2463 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002464 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002465 (scsicmd->cmnd[7] << 16) |
2466 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002467 count = (scsicmd->cmnd[10] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002468 (scsicmd->cmnd[11] << 16) |
2469 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2470 break;
2471 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002472 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002473
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002474 lba = ((u64)scsicmd->cmnd[2] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002475 (scsicmd->cmnd[3] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002476 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2477 count = (scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002478 (scsicmd->cmnd[7] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002479 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002480 break;
2481 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002482 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002484 lba = ((u64)scsicmd->cmnd[2] << 24) |
2485 (scsicmd->cmnd[3] << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002486 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002490
2491 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2492 cid = scmd_id(scsicmd);
2493 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2494 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2495 SAM_STAT_CHECK_CONDITION;
2496 set_sense(&dev->fsa_dev[cid].sense_data,
2497 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2498 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2499 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2500 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2501 SCSI_SENSE_BUFFERSIZE));
2502 scsicmd->scsi_done(scsicmd);
2503 return 1;
2504 }
2505
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002506 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002507 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002508 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 /*
2511 * Alocate and initialize a Fib
2512 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002513 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Brian King1ae948f2017-08-29 10:00:29 -05002514 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002515 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 /*
2518 * Check that the command queued to the controller
2519 */
Brian King1ae948f2017-08-29 10:00:29 -05002520 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return 0;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002522
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002523 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 /*
2525 * For some reason, the Fib didn't queue, return QUEUE_FULL
2526 */
2527 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002528 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002529 aac_fib_complete(cmd_fibcontext);
2530 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return 0;
2532}
2533
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002534static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002536 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002538 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 struct aac_dev *dev;
2541 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002542 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2545 /*
2546 * Get block address and transfer length
2547 */
2548 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
2549 {
2550 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2551 count = scsicmd->cmnd[4];
2552 if (count == 0)
2553 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002554 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002555 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002556 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002557
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002558 lba = ((u64)scsicmd->cmnd[2] << 56) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002559 ((u64)scsicmd->cmnd[3] << 48) |
2560 ((u64)scsicmd->cmnd[4] << 40) |
2561 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002562 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002563 (scsicmd->cmnd[7] << 16) |
2564 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2565 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2566 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002567 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002568 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002569 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002570
2571 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2572 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2573 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2574 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002575 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002577 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002578 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002580 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002582
2583 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2584 cid = scmd_id(scsicmd);
2585 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2586 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2587 SAM_STAT_CHECK_CONDITION;
2588 set_sense(&dev->fsa_dev[cid].sense_data,
2589 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2590 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2591 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2592 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2593 SCSI_SENSE_BUFFERSIZE));
2594 scsicmd->scsi_done(scsicmd);
2595 return 1;
2596 }
2597
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002598 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002600 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /*
2603 * Allocate and initialize a Fib then setup a BlockWrite command
2604 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002605 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Brian King1ae948f2017-08-29 10:00:29 -05002606 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002607 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 /*
2610 * Check that the command queued to the controller
2611 */
Brian King1ae948f2017-08-29 10:00:29 -05002612 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002615 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 /*
2617 * For some reason, the Fib didn't queue, return QUEUE_FULL
2618 */
2619 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002620 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002622 aac_fib_complete(cmd_fibcontext);
2623 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 return 0;
2625}
2626
2627static void synchronize_callback(void *context, struct fib *fibptr)
2628{
2629 struct aac_synchronize_reply *synchronizereply;
2630 struct scsi_cmnd *cmd;
2631
2632 cmd = context;
2633
Mark Haverkamp03d44332007-03-15 10:27:45 -07002634 if (!aac_valid_context(cmd, fibptr))
2635 return;
2636
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002637 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 smp_processor_id(), jiffies));
2639 BUG_ON(fibptr == NULL);
2640
2641
2642 synchronizereply = fib_data(fibptr);
2643 if (le32_to_cpu(synchronizereply->status) == CT_OK)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002644 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2646 else {
2647 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002648 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08002649 u32 cid = sdev_id(sdev);
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002650 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 "synchronize_callback: synchronize failed, status = %d\n",
2652 le32_to_cpu(synchronizereply->status));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002653 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002655 set_sense(&dev->fsa_dev[cid].sense_data,
2656 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2657 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002659 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2660 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002663 aac_fib_complete(fibptr);
2664 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002665 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002668static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
2670 int status;
2671 struct fib *cmd_fibcontext;
2672 struct aac_synchronize *synchronizecmd;
2673 struct scsi_cmnd *cmd;
2674 struct scsi_device *sdev = scsicmd->device;
2675 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002676 struct aac_dev *aac;
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002677 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2678 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2679 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 unsigned long flags;
2681
2682 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002683 * Wait for all outstanding queued commands to complete to this
2684 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 */
2686 spin_lock_irqsave(&sdev->list_lock, flags);
2687 list_for_each_entry(cmd, &sdev->cmd_list, list)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002688 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2689 u64 cmnd_lba;
2690 u32 cmnd_count;
2691
2692 if (cmd->cmnd[0] == WRITE_6) {
2693 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2694 (cmd->cmnd[2] << 8) |
2695 cmd->cmnd[3];
2696 cmnd_count = cmd->cmnd[4];
2697 if (cmnd_count == 0)
2698 cmnd_count = 256;
2699 } else if (cmd->cmnd[0] == WRITE_16) {
2700 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2701 ((u64)cmd->cmnd[3] << 48) |
2702 ((u64)cmd->cmnd[4] << 40) |
2703 ((u64)cmd->cmnd[5] << 32) |
2704 ((u64)cmd->cmnd[6] << 24) |
2705 (cmd->cmnd[7] << 16) |
2706 (cmd->cmnd[8] << 8) |
2707 cmd->cmnd[9];
2708 cmnd_count = (cmd->cmnd[10] << 24) |
2709 (cmd->cmnd[11] << 16) |
2710 (cmd->cmnd[12] << 8) |
2711 cmd->cmnd[13];
2712 } else if (cmd->cmnd[0] == WRITE_12) {
2713 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2714 (cmd->cmnd[3] << 16) |
2715 (cmd->cmnd[4] << 8) |
2716 cmd->cmnd[5];
2717 cmnd_count = (cmd->cmnd[6] << 24) |
2718 (cmd->cmnd[7] << 16) |
2719 (cmd->cmnd[8] << 8) |
2720 cmd->cmnd[9];
2721 } else if (cmd->cmnd[0] == WRITE_10) {
2722 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2723 (cmd->cmnd[3] << 16) |
2724 (cmd->cmnd[4] << 8) |
2725 cmd->cmnd[5];
2726 cmnd_count = (cmd->cmnd[7] << 8) |
2727 cmd->cmnd[8];
2728 } else
2729 continue;
2730 if (((cmnd_lba + cmnd_count) < lba) ||
2731 (count && ((lba + count) < cmnd_lba)))
2732 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 ++active;
2734 break;
2735 }
2736
2737 spin_unlock_irqrestore(&sdev->list_lock, flags);
2738
2739 /*
2740 * Yield the processor (requeue for later)
2741 */
2742 if (active)
2743 return SCSI_MLQUEUE_DEVICE_BUSY;
2744
Salyzyn, Markf8583172007-10-30 15:50:49 -04002745 aac = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002746 if (aac->in_reset)
2747 return SCSI_MLQUEUE_HOST_BUSY;
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002750 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002752 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return SCSI_MLQUEUE_HOST_BUSY;
2754
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002755 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 synchronizecmd = fib_data(cmd_fibcontext);
2758 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2759 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002760 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002761 synchronizecmd->count =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
Brian King1ae948f2017-08-29 10:00:29 -05002763 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 /*
2766 * Now send the Fib to the adapter
2767 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002768 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 cmd_fibcontext,
2770 sizeof(struct aac_synchronize),
2771 FsaNormal,
2772 0, 1,
2773 (fib_callback)synchronize_callback,
2774 (void *)scsicmd);
2775
2776 /*
2777 * Check that the command queued to the controller
2778 */
Brian King1ae948f2017-08-29 10:00:29 -05002779 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return 0;
2781
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002782 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002783 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2784 aac_fib_complete(cmd_fibcontext);
2785 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return SCSI_MLQUEUE_HOST_BUSY;
2787}
2788
Mark Salyzyn655d7222008-04-30 16:03:42 -04002789static void aac_start_stop_callback(void *context, struct fib *fibptr)
2790{
2791 struct scsi_cmnd *scsicmd = context;
2792
2793 if (!aac_valid_context(scsicmd, fibptr))
2794 return;
2795
2796 BUG_ON(fibptr == NULL);
2797
2798 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2799
2800 aac_fib_complete(fibptr);
2801 aac_fib_free(fibptr);
2802 scsicmd->scsi_done(scsicmd);
2803}
2804
2805static int aac_start_stop(struct scsi_cmnd *scsicmd)
2806{
2807 int status;
2808 struct fib *cmd_fibcontext;
2809 struct aac_power_management *pmcmd;
2810 struct scsi_device *sdev = scsicmd->device;
2811 struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2812
Raghava Aditya Renukunta1c688562017-02-16 12:51:10 -08002813 if (!(aac->supplement_adapter_info.supported_options2 &
Mark Salyzyn655d7222008-04-30 16:03:42 -04002814 AAC_OPTION_POWER_MANAGEMENT)) {
2815 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2816 SAM_STAT_GOOD;
2817 scsicmd->scsi_done(scsicmd);
2818 return 0;
2819 }
2820
2821 if (aac->in_reset)
2822 return SCSI_MLQUEUE_HOST_BUSY;
2823
2824 /*
2825 * Allocate and initialize a Fib
2826 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002827 cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
Mark Salyzyn655d7222008-04-30 16:03:42 -04002828
2829 aac_fib_init(cmd_fibcontext);
2830
2831 pmcmd = fib_data(cmd_fibcontext);
2832 pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2833 pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2834 /* Eject bit ignored, not relevant */
2835 pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2836 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2837 pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2838 pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2839 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
Brian King1ae948f2017-08-29 10:00:29 -05002840 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002841
2842 /*
2843 * Now send the Fib to the adapter
2844 */
2845 status = aac_fib_send(ContainerCommand,
2846 cmd_fibcontext,
2847 sizeof(struct aac_power_management),
2848 FsaNormal,
2849 0, 1,
2850 (fib_callback)aac_start_stop_callback,
2851 (void *)scsicmd);
2852
2853 /*
2854 * Check that the command queued to the controller
2855 */
Brian King1ae948f2017-08-29 10:00:29 -05002856 if (status == -EINPROGRESS)
Mark Salyzyn655d7222008-04-30 16:03:42 -04002857 return 0;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002858
2859 aac_fib_complete(cmd_fibcontext);
2860 aac_fib_free(cmd_fibcontext);
2861 return SCSI_MLQUEUE_HOST_BUSY;
2862}
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864/**
2865 * aac_scsi_cmd() - Process SCSI command
2866 * @scsicmd: SCSI command block
2867 *
2868 * Emulate a SCSI command and queue the required request for the
2869 * aacraid firmware.
2870 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2873{
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08002874 u32 cid, bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 struct Scsi_Host *host = scsicmd->device->host;
2876 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2877 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002878
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002879 if (fsa_dev_ptr == NULL)
2880 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 /*
2882 * If the bus, id or lun is out of range, return fail
2883 * Test does not apply to ID 16, the pseudo id for the controller
2884 * itself.
2885 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002886 cid = scmd_id(scsicmd);
2887 if (cid != host->this_id) {
2888 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2889 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08002890 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 scsicmd->result = DID_NO_CONNECT << 16;
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002892 goto scsi_done_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 /*
2896 * If the target container doesn't exist, it may have
2897 * been newly created
2898 */
Mark Salyzyn655d7222008-04-30 16:03:42 -04002899 if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2900 (fsa_dev_ptr[cid].sense_data.sense_key ==
2901 NOT_READY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 switch (scsicmd->cmnd[0]) {
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002903 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002904 if (!(dev->raw_io_interface) ||
2905 !(dev->raw_io_64) ||
2906 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 case INQUIRY:
2909 case READ_CAPACITY:
2910 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002911 if (dev->in_reset)
2912 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07002913 return _aac_probe_container(scsicmd,
2914 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 default:
2916 break;
2917 }
2918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 } else { /* check for physical non-dasd devices */
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08002920 bus = aac_logical_to_phys(scmd_channel(scsicmd));
2921 if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2922 (dev->hba_map[bus][cid].expose
2923 == AAC_HIDE_DISK)){
2924 if (scsicmd->cmnd[0] == INQUIRY) {
2925 scsicmd->result = DID_NO_CONNECT << 16;
2926 goto scsi_done_ret;
2927 }
2928 }
2929
2930 if (bus < AAC_MAX_BUSES && cid < AAC_MAX_TARGETS &&
2931 dev->hba_map[bus][cid].devtype
2932 == AAC_DEVTYPE_NATIVE_RAW) {
2933 if (dev->in_reset)
2934 return -1;
2935 return aac_send_hba_fib(scsicmd);
2936 } else if (dev->nondasd_support || expose_physicals ||
2937 dev->jbod) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002938 if (dev->in_reset)
2939 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return aac_send_srb_fib(scsicmd);
2941 } else {
2942 scsicmd->result = DID_NO_CONNECT << 16;
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002943 goto scsi_done_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
2945 }
2946 }
2947 /*
2948 * else Command for the controller itself
2949 */
2950 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002951 (scsicmd->cmnd[0] != TEST_UNIT_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 {
2953 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2954 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002955 set_sense(&dev->fsa_dev[cid].sense_data,
2956 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2957 ASENCODE_INVALID_COMMAND, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002959 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2960 SCSI_SENSE_BUFFERSIZE));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002961 goto scsi_done_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 switch (scsicmd->cmnd[0]) {
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08002965 case READ_6:
2966 case READ_10:
2967 case READ_12:
2968 case READ_16:
2969 if (dev->in_reset)
2970 return -1;
2971 return aac_read(scsicmd);
2972
2973 case WRITE_6:
2974 case WRITE_10:
2975 case WRITE_12:
2976 case WRITE_16:
2977 if (dev->in_reset)
2978 return -1;
2979 return aac_write(scsicmd);
2980
2981 case SYNCHRONIZE_CACHE:
2982 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2983 scsicmd->result = AAC_STAT_GOOD;
2984 break;
2985 }
2986 /* Issue FIB to tell Firmware to flush it's cache */
2987 if ((aac_cache & 6) != 2)
2988 return aac_synchronize(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 case INQUIRY:
2990 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002991 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002993 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002994 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Leubner, Achimd8e965072009-04-01 07:16:08 -07002996 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002997 char *arr = (char *)&inq_data;
2998
2999 /* EVPD bit set */
3000 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
3001 INQD_PDT_PROC : INQD_PDT_DA;
3002 if (scsicmd->cmnd[2] == 0) {
3003 /* supported vital product data pages */
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04003004 arr[3] = 3;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04003005 arr[4] = 0x0;
3006 arr[5] = 0x80;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04003007 arr[6] = 0x83;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04003008 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09003009 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
3010 sizeof(inq_data));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003011 scsicmd->result = AAC_STAT_GOOD;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04003012 } else if (scsicmd->cmnd[2] == 0x80) {
3013 /* unit serial number page */
3014 arr[3] = setinqserial(dev, &arr[4],
3015 scmd_id(scsicmd));
3016 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09003017 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
3018 sizeof(inq_data));
Leubner, Achimd8e965072009-04-01 07:16:08 -07003019 if (aac_wwn != 2)
3020 return aac_get_container_serial(
3021 scsicmd);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003022 scsicmd->result = AAC_STAT_GOOD;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04003023 } else if (scsicmd->cmnd[2] == 0x83) {
3024 /* vpd page 0x83 - Device Identification Page */
3025 char *sno = (char *)&inq_data;
3026 sno[3] = setinqserial(dev, &sno[4],
3027 scmd_id(scsicmd));
3028 if (aac_wwn != 2)
3029 return aac_get_container_serial(
3030 scsicmd);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003031 scsicmd->result = AAC_STAT_GOOD;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04003032 } else {
3033 /* vpd page not implemented */
3034 scsicmd->result = DID_OK << 16 |
3035 COMMAND_COMPLETE << 8 |
3036 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08003037 set_sense(&dev->fsa_dev[cid].sense_data,
3038 ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
3039 ASENCODE_NO_SENSE, 7, 2);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04003040 memcpy(scsicmd->sense_buffer,
3041 &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08003042 min_t(size_t,
3043 sizeof(dev->fsa_dev[cid].sense_data),
3044 SCSI_SENSE_BUFFERSIZE));
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04003045 }
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003046 break;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04003047 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07003048 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07003049 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 */
3050 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07003052 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 /*
3054 * Set the Vendor, Product, and Revision Level
3055 * see: <vendor>.c i.e. aac.c
3056 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04003057 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07003058 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07003059 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09003060 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
3061 sizeof(inq_data));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003062 scsicmd->result = AAC_STAT_GOOD;
3063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07003065 if (dev->in_reset)
3066 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07003067 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07003068 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09003069 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07003070 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
Hannes Reineckeeb846d92014-11-17 14:25:19 +01003072 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07003073 if (!(dev->raw_io_interface) ||
3074 !(dev->raw_io_64) ||
3075 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
3076 break;
3077 {
3078 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08003079 char cp[13];
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08003080 unsigned int alloc_len;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07003081
3082 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
3083 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07003084 cp[0] = (capacity >> 56) & 0xff;
3085 cp[1] = (capacity >> 48) & 0xff;
3086 cp[2] = (capacity >> 40) & 0xff;
3087 cp[3] = (capacity >> 32) & 0xff;
3088 cp[4] = (capacity >> 24) & 0xff;
3089 cp[5] = (capacity >> 16) & 0xff;
3090 cp[6] = (capacity >> 8) & 0xff;
3091 cp[7] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003092 cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
3093 cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3094 cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3095 cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08003096 cp[12] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08003097
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08003098 alloc_len = ((scsicmd->cmnd[10] << 24)
3099 + (scsicmd->cmnd[11] << 16)
3100 + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
3101
3102 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
FUJITA Tomonorid4345022008-03-09 13:44:34 +09003103 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08003104 if (alloc_len < scsi_bufflen(scsicmd))
3105 scsi_set_resid(scsicmd,
3106 scsi_bufflen(scsicmd) - alloc_len);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07003107
3108 /* Do not cache partition table for arrays */
3109 scsicmd->device->removable = 1;
3110
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003111 scsicmd->result = AAC_STAT_GOOD;
3112 break;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07003113 }
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 case READ_CAPACITY:
3116 {
3117 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07003118 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
3120 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07003121 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 capacity = fsa_dev_ptr[cid].size - 1;
3123 else
3124 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 cp[0] = (capacity >> 24) & 0xff;
3127 cp[1] = (capacity >> 16) & 0xff;
3128 cp[2] = (capacity >> 8) & 0xff;
3129 cp[3] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003130 cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
3131 cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3132 cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3133 cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
FUJITA Tomonorid4345022008-03-09 13:44:34 +09003134 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07003135 /* Do not cache partition table for arrays */
3136 scsicmd->device->removable = 1;
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003137 scsicmd->result = AAC_STAT_GOOD;
3138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
3140
3141 case MODE_SENSE:
3142 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003143 int mode_buf_length = 4;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003144 u32 capacity;
3145 aac_modep_data mpd;
3146
3147 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3148 capacity = fsa_dev_ptr[cid].size - 1;
3149 else
3150 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003153 memset((char *)&mpd, 0, sizeof(aac_modep_data));
3154
3155 /* Mode data length */
3156 mpd.hd.data_length = sizeof(mpd.hd) - 1;
3157 /* Medium type - default */
3158 mpd.hd.med_type = 0;
3159 /* Device-specific param,
3160 bit 8: 0/1 = write enabled/protected
3161 bit 4: 0/1 = FUA enabled */
3162 mpd.hd.dev_par = 0;
3163
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08003164 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003165 mpd.hd.dev_par = 0x10;
3166 if (scsicmd->cmnd[1] & 0x8)
3167 mpd.hd.bd_length = 0; /* Block descriptor length */
3168 else {
3169 mpd.hd.bd_length = sizeof(mpd.bd);
3170 mpd.hd.data_length += mpd.hd.bd_length;
3171 mpd.bd.block_length[0] =
3172 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3173 mpd.bd.block_length[1] =
3174 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3175 mpd.bd.block_length[2] =
3176 fsa_dev_ptr[cid].block_size & 0xff;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04003177
3178 mpd.mpc_buf[0] = scsicmd->cmnd[2];
3179 if (scsicmd->cmnd[2] == 0x1C) {
3180 /* page length */
3181 mpd.mpc_buf[1] = 0xa;
3182 /* Mode data length */
3183 mpd.hd.data_length = 23;
3184 } else {
3185 /* Mode data length */
3186 mpd.hd.data_length = 15;
3187 }
3188
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003189 if (capacity > 0xffffff) {
3190 mpd.bd.block_count[0] = 0xff;
3191 mpd.bd.block_count[1] = 0xff;
3192 mpd.bd.block_count[2] = 0xff;
3193 } else {
3194 mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
3195 mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
3196 mpd.bd.block_count[2] = capacity & 0xff;
3197 }
3198 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003199 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3200 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003201 mpd.hd.data_length += 3;
3202 mpd.mpc_buf[0] = 8;
3203 mpd.mpc_buf[1] = 1;
3204 mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08003205 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003206 mode_buf_length = sizeof(mpd);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003207 }
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04003208
3209 if (mode_buf_length > scsicmd->cmnd[4])
3210 mode_buf_length = scsicmd->cmnd[4];
3211 else
3212 mode_buf_length = sizeof(mpd);
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003213 scsi_sg_copy_from_buffer(scsicmd,
3214 (char *)&mpd,
3215 mode_buf_length);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003216 scsicmd->result = AAC_STAT_GOOD;
3217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
3219 case MODE_SENSE_10:
3220 {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003221 u32 capacity;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003222 int mode_buf_length = 8;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003223 aac_modep10_data mpd10;
3224
3225 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
3226 capacity = fsa_dev_ptr[cid].size - 1;
3227 else
3228 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003231 memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
3232 /* Mode data length (MSB) */
3233 mpd10.hd.data_length[0] = 0;
3234 /* Mode data length (LSB) */
3235 mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
3236 /* Medium type - default */
3237 mpd10.hd.med_type = 0;
3238 /* Device-specific param,
3239 bit 8: 0/1 = write enabled/protected
3240 bit 4: 0/1 = FUA enabled */
3241 mpd10.hd.dev_par = 0;
3242
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08003243 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003244 mpd10.hd.dev_par = 0x10;
3245 mpd10.hd.rsrvd[0] = 0; /* reserved */
3246 mpd10.hd.rsrvd[1] = 0; /* reserved */
3247 if (scsicmd->cmnd[1] & 0x8) {
3248 /* Block descriptor length (MSB) */
3249 mpd10.hd.bd_length[0] = 0;
3250 /* Block descriptor length (LSB) */
3251 mpd10.hd.bd_length[1] = 0;
3252 } else {
3253 mpd10.hd.bd_length[0] = 0;
3254 mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
3255
3256 mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
3257
3258 mpd10.bd.block_length[0] =
3259 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
3260 mpd10.bd.block_length[1] =
3261 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
3262 mpd10.bd.block_length[2] =
3263 fsa_dev_ptr[cid].block_size & 0xff;
3264
3265 if (capacity > 0xffffff) {
3266 mpd10.bd.block_count[0] = 0xff;
3267 mpd10.bd.block_count[1] = 0xff;
3268 mpd10.bd.block_count[2] = 0xff;
3269 } else {
3270 mpd10.bd.block_count[0] =
3271 (capacity >> 16) & 0xff;
3272 mpd10.bd.block_count[1] =
3273 (capacity >> 8) & 0xff;
3274 mpd10.bd.block_count[2] =
3275 capacity & 0xff;
3276 }
3277 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003278 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
3279 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003280 mpd10.hd.data_length[1] += 3;
3281 mpd10.mpc_buf[0] = 8;
3282 mpd10.mpc_buf[1] = 1;
3283 mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08003284 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003285 mode_buf_length = sizeof(mpd10);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04003286 if (mode_buf_length > scsicmd->cmnd[8])
3287 mode_buf_length = scsicmd->cmnd[8];
3288 }
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04003289 scsi_sg_copy_from_buffer(scsicmd,
3290 (char *)&mpd10,
3291 mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003293 scsicmd->result = AAC_STAT_GOOD;
3294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
3296 case REQUEST_SENSE:
3297 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003298 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3299 sizeof(struct sense_data));
3300 memset(&dev->fsa_dev[cid].sense_data, 0,
3301 sizeof(struct sense_data));
3302 scsicmd->result = AAC_STAT_GOOD;
3303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
3305 case ALLOW_MEDIUM_REMOVAL:
3306 dprintk((KERN_DEBUG "LOCK command.\n"));
3307 if (scsicmd->cmnd[4])
3308 fsa_dev_ptr[cid].locked = 1;
3309 else
3310 fsa_dev_ptr[cid].locked = 0;
3311
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003312 scsicmd->result = AAC_STAT_GOOD;
3313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 /*
3315 * These commands are all No-Ops
3316 */
3317 case TEST_UNIT_READY:
Mark Salyzyn655d7222008-04-30 16:03:42 -04003318 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
3319 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3320 SAM_STAT_CHECK_CONDITION;
3321 set_sense(&dev->fsa_dev[cid].sense_data,
3322 NOT_READY, SENCODE_BECOMING_READY,
3323 ASENCODE_BECOMING_READY, 0, 0);
3324 memcpy(scsicmd->sense_buffer,
3325 &dev->fsa_dev[cid].sense_data,
3326 min_t(size_t,
3327 sizeof(dev->fsa_dev[cid].sense_data),
3328 SCSI_SENSE_BUFFERSIZE));
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003329 break;
Mark Salyzyn655d7222008-04-30 16:03:42 -04003330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 case RESERVE:
3332 case RELEASE:
3333 case REZERO_UNIT:
3334 case REASSIGN_BLOCKS:
3335 case SEEK_10:
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003336 scsicmd->result = AAC_STAT_GOOD;
3337 break;
Mark Salyzyn655d7222008-04-30 16:03:42 -04003338
3339 case START_STOP:
3340 return aac_start_stop(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003342 /* FALLTHRU */
3343 default:
3344 /*
3345 * Unhandled commands
3346 */
3347 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n",
3348 scsicmd->cmnd[0]));
3349 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3350 SAM_STAT_CHECK_CONDITION;
3351 set_sense(&dev->fsa_dev[cid].sense_data,
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08003352 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
3353 ASENCODE_INVALID_COMMAND, 0, 0);
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003354 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08003355 min_t(size_t,
3356 sizeof(dev->fsa_dev[cid].sense_data),
3357 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Raghava Aditya Renukuntac4e2fbc2017-02-02 15:53:20 -08003359
3360scsi_done_ret:
3361
3362 scsicmd->scsi_done(scsicmd);
3363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
3366static int query_disk(struct aac_dev *dev, void __user *arg)
3367{
3368 struct aac_query_disk qd;
3369 struct fsa_dev_info *fsa_dev_ptr;
3370
3371 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07003372 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07003373 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
3375 return -EFAULT;
Dan Carpentere6fd9162017-07-25 22:49:55 +03003376 if (qd.cnum == -1) {
3377 if (qd.id < 0 || qd.id >= dev->maximum_num_containers)
3378 return -EINVAL;
Mark Haverkampe5718772006-03-27 09:43:25 -08003379 qd.cnum = qd.id;
Dan Carpentere6fd9162017-07-25 22:49:55 +03003380 } else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
3382 return -EINVAL;
3383 qd.instance = dev->scsi_host_ptr->host_no;
3384 qd.bus = 0;
3385 qd.id = CONTAINER_TO_ID(qd.cnum);
3386 qd.lun = CONTAINER_TO_LUN(qd.cnum);
3387 }
3388 else return -EINVAL;
3389
Salyzyn, Markfd622b12007-07-17 10:59:19 -04003390 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 qd.locked = fsa_dev_ptr[qd.cnum].locked;
3392 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
3393
3394 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
3395 qd.unmapped = 1;
3396 else
3397 qd.unmapped = 0;
3398
3399 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
3400 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
3401
3402 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
3403 return -EFAULT;
3404 return 0;
3405}
3406
3407static int force_delete_disk(struct aac_dev *dev, void __user *arg)
3408{
3409 struct aac_delete_disk dd;
3410 struct fsa_dev_info *fsa_dev_ptr;
3411
3412 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp65101352006-09-19 08:59:23 -07003413 if (!fsa_dev_ptr)
3414 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3417 return -EFAULT;
3418
3419 if (dd.cnum >= dev->maximum_num_containers)
3420 return -EINVAL;
3421 /*
3422 * Mark this container as being deleted.
3423 */
3424 fsa_dev_ptr[dd.cnum].deleted = 1;
3425 /*
3426 * Mark the container as no longer valid
3427 */
3428 fsa_dev_ptr[dd.cnum].valid = 0;
3429 return 0;
3430}
3431
3432static int delete_disk(struct aac_dev *dev, void __user *arg)
3433{
3434 struct aac_delete_disk dd;
3435 struct fsa_dev_info *fsa_dev_ptr;
3436
3437 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07003438 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07003439 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
3441 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3442 return -EFAULT;
3443
3444 if (dd.cnum >= dev->maximum_num_containers)
3445 return -EINVAL;
3446 /*
3447 * If the container is locked, it can not be deleted by the API.
3448 */
3449 if (fsa_dev_ptr[dd.cnum].locked)
3450 return -EBUSY;
3451 else {
3452 /*
3453 * Mark the container as no longer being valid.
3454 */
3455 fsa_dev_ptr[dd.cnum].valid = 0;
3456 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
3457 return 0;
3458 }
3459}
3460
3461int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
3462{
3463 switch (cmd) {
3464 case FSACTL_QUERY_DISK:
3465 return query_disk(dev, arg);
3466 case FSACTL_DELETE_DISK:
3467 return delete_disk(dev, arg);
3468 case FSACTL_FORCE_DELETE_DISK:
3469 return force_delete_disk(dev, arg);
3470 case FSACTL_GET_CONTAINERS:
3471 return aac_get_containers(dev);
3472 default:
3473 return -ENOTTY;
3474 }
3475}
3476
3477/**
3478 *
3479 * aac_srb_callback
3480 * @context: the context set in the fib - here it is scsi cmd
3481 * @fibptr: pointer to the fib
3482 *
3483 * Handles the completion of a scsi command to a non dasd device
3484 *
3485 */
3486
3487static void aac_srb_callback(void *context, struct fib * fibptr)
3488{
3489 struct aac_dev *dev;
3490 struct aac_srb_reply *srbreply;
3491 struct scsi_cmnd *scsicmd;
3492
3493 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07003494
3495 if (!aac_valid_context(scsicmd, fibptr))
3496 return;
3497
Eric Sesterhenn125e1872006-06-23 02:06:06 -07003498 BUG_ON(fibptr == NULL);
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003499
Salyzyn, Mark1a655042007-06-11 16:17:55 -04003500 dev = fibptr->dev;
3501
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003502 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303506 if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3507 /* fast response */
3508 srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
3509 srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
3510 } else {
3511 /*
3512 * Calculate resid for sg
3513 */
3514 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
3515 - le32_to_cpu(srbreply->data_xfer_length));
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003516 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003519 scsi_dma_unmap(scsicmd);
3520
3521 /* expose physical device if expose_physicald flag is on */
3522 if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
3523 && expose_physicals > 0)
3524 aac_expose_phy_device(scsicmd);
3525
3526 /*
3527 * First check the fib status
3528 */
3529
3530 if (le32_to_cpu(srbreply->status) != ST_OK) {
3531 int len;
3532
3533 pr_warn("aac_srb_callback: srb failed, status = %d\n",
3534 le32_to_cpu(srbreply->status));
3535 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3536 SCSI_SENSE_BUFFERSIZE);
3537 scsicmd->result = DID_ERROR << 16
3538 | COMMAND_COMPLETE << 8
3539 | SAM_STAT_CHECK_CONDITION;
3540 memcpy(scsicmd->sense_buffer,
3541 srbreply->sense_data, len);
3542 }
3543
3544 /*
3545 * Next check the srb status
3546 */
3547 switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3548 case SRB_STATUS_ERROR_RECOVERY:
3549 case SRB_STATUS_PENDING:
3550 case SRB_STATUS_SUCCESS:
3551 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3552 break;
3553 case SRB_STATUS_DATA_OVERRUN:
3554 switch (scsicmd->cmnd[0]) {
3555 case READ_6:
3556 case WRITE_6:
3557 case READ_10:
3558 case WRITE_10:
3559 case READ_12:
3560 case WRITE_12:
3561 case READ_16:
3562 case WRITE_16:
3563 if (le32_to_cpu(srbreply->data_xfer_length)
3564 < scsicmd->underflow)
3565 pr_warn("aacraid: SCSI CMD underflow\n");
3566 else
3567 pr_warn("aacraid: SCSI CMD Data Overrun\n");
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003568 scsicmd->result = DID_ERROR << 16
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003569 | COMMAND_COMPLETE << 8;
3570 break;
3571 case INQUIRY:
3572 scsicmd->result = DID_OK << 16
3573 | COMMAND_COMPLETE << 8;
3574 break;
3575 default:
Mark Haverkampbb08f922006-02-01 09:30:44 -08003576 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 break;
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003578 }
3579 break;
3580 case SRB_STATUS_ABORTED:
3581 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3582 break;
3583 case SRB_STATUS_ABORT_FAILED:
3584 /*
3585 * Not sure about this one - but assuming the
3586 * hba was trying to abort for some reason
3587 */
3588 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3589 break;
3590 case SRB_STATUS_PARITY_ERROR:
3591 scsicmd->result = DID_PARITY << 16
3592 | MSG_PARITY_ERROR << 8;
3593 break;
3594 case SRB_STATUS_NO_DEVICE:
3595 case SRB_STATUS_INVALID_PATH_ID:
3596 case SRB_STATUS_INVALID_TARGET_ID:
3597 case SRB_STATUS_INVALID_LUN:
3598 case SRB_STATUS_SELECTION_TIMEOUT:
3599 scsicmd->result = DID_NO_CONNECT << 16
3600 | COMMAND_COMPLETE << 8;
3601 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003602
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003603 case SRB_STATUS_COMMAND_TIMEOUT:
3604 case SRB_STATUS_TIMEOUT:
3605 scsicmd->result = DID_TIME_OUT << 16
3606 | COMMAND_COMPLETE << 8;
3607 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003608
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003609 case SRB_STATUS_BUSY:
3610 scsicmd->result = DID_BUS_BUSY << 16
3611 | COMMAND_COMPLETE << 8;
3612 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003613
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003614 case SRB_STATUS_BUS_RESET:
3615 scsicmd->result = DID_RESET << 16
3616 | COMMAND_COMPLETE << 8;
3617 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003618
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003619 case SRB_STATUS_MESSAGE_REJECTED:
3620 scsicmd->result = DID_ERROR << 16
3621 | MESSAGE_REJECT << 8;
3622 break;
3623 case SRB_STATUS_REQUEST_FLUSHED:
3624 case SRB_STATUS_ERROR:
3625 case SRB_STATUS_INVALID_REQUEST:
3626 case SRB_STATUS_REQUEST_SENSE_FAILED:
3627 case SRB_STATUS_NO_HBA:
3628 case SRB_STATUS_UNEXPECTED_BUS_FREE:
3629 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3630 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3631 case SRB_STATUS_DELAYED_RETRY:
3632 case SRB_STATUS_BAD_FUNCTION:
3633 case SRB_STATUS_NOT_STARTED:
3634 case SRB_STATUS_NOT_IN_USE:
3635 case SRB_STATUS_FORCE_ABORT:
3636 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3637 default:
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003638#ifdef AAC_DETAILED_STATUS_INFO
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003639 pr_info("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x -scsi status 0x%x\n",
3640 le32_to_cpu(srbreply->srb_status) & 0x3F,
3641 aac_get_status_string(
3642 le32_to_cpu(srbreply->srb_status) & 0x3F),
3643 scsicmd->cmnd[0],
3644 le32_to_cpu(srbreply->scsi_status));
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003645#endif
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003646 /*
3647 * When the CC bit is SET by the host in ATA pass thru CDB,
3648 * driver is supposed to return DID_OK
3649 *
3650 * When the CC bit is RESET by the host, driver should
3651 * return DID_ERROR
3652 */
3653 if ((scsicmd->cmnd[0] == ATA_12)
3654 || (scsicmd->cmnd[0] == ATA_16)) {
3655
3656 if (scsicmd->cmnd[2] & (0x01 << 5)) {
3657 scsicmd->result = DID_OK << 16
3658 | COMMAND_COMPLETE << 8;
3659 break;
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003660 } else {
3661 scsicmd->result = DID_ERROR << 16
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003662 | COMMAND_COMPLETE << 8;
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003663 break;
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003664 }
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003665 } else {
3666 scsicmd->result = DID_ERROR << 16
3667 | COMMAND_COMPLETE << 8;
3668 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
Raghava Aditya Renukunta4ec57fb2017-02-02 15:53:21 -08003671 if (le32_to_cpu(srbreply->scsi_status)
3672 == SAM_STAT_CHECK_CONDITION) {
3673 int len;
3674
3675 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3676 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3677 SCSI_SENSE_BUFFERSIZE);
3678#ifdef AAC_DETAILED_STATUS_INFO
3679 pr_warn("aac_srb_callback: check condition, status = %d len=%d\n",
3680 le32_to_cpu(srbreply->status), len);
3681#endif
3682 memcpy(scsicmd->sense_buffer,
3683 srbreply->sense_data, len);
3684 }
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 /*
3687 * OR in the scsi status (already shifted up a bit)
3688 */
3689 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3690
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003691 aac_fib_complete(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07003692 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003695static void hba_resp_task_complete(struct aac_dev *dev,
3696 struct scsi_cmnd *scsicmd,
3697 struct aac_hba_resp *err) {
3698
3699 scsicmd->result = err->status;
3700 /* set residual count */
3701 scsi_set_resid(scsicmd, le32_to_cpu(err->residual_count));
3702
3703 switch (err->status) {
3704 case SAM_STAT_GOOD:
3705 scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3706 break;
3707 case SAM_STAT_CHECK_CONDITION:
3708 {
3709 int len;
3710
3711 len = min_t(u8, err->sense_response_data_len,
3712 SCSI_SENSE_BUFFERSIZE);
3713 if (len)
3714 memcpy(scsicmd->sense_buffer,
3715 err->sense_response_buf, len);
3716 scsicmd->result |= DID_OK << 16 | COMMAND_COMPLETE << 8;
3717 break;
3718 }
3719 case SAM_STAT_BUSY:
3720 scsicmd->result |= DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
3721 break;
3722 case SAM_STAT_TASK_ABORTED:
3723 scsicmd->result |= DID_ABORT << 16 | ABORT << 8;
3724 break;
3725 case SAM_STAT_RESERVATION_CONFLICT:
3726 case SAM_STAT_TASK_SET_FULL:
3727 default:
3728 scsicmd->result |= DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3729 break;
3730 }
3731}
3732
3733static void hba_resp_task_failure(struct aac_dev *dev,
3734 struct scsi_cmnd *scsicmd,
3735 struct aac_hba_resp *err)
3736{
3737 switch (err->status) {
3738 case HBA_RESP_STAT_HBAMODE_DISABLED:
3739 {
3740 u32 bus, cid;
3741
3742 bus = aac_logical_to_phys(scmd_channel(scsicmd));
3743 cid = scmd_id(scsicmd);
3744 if (dev->hba_map[bus][cid].devtype == AAC_DEVTYPE_NATIVE_RAW) {
3745 dev->hba_map[bus][cid].devtype = AAC_DEVTYPE_ARC_RAW;
3746 dev->hba_map[bus][cid].rmw_nexus = 0xffffffff;
3747 }
3748 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3749 break;
3750 }
3751 case HBA_RESP_STAT_IO_ERROR:
3752 case HBA_RESP_STAT_NO_PATH_TO_DEVICE:
3753 scsicmd->result = DID_OK << 16 |
3754 COMMAND_COMPLETE << 8 | SAM_STAT_BUSY;
3755 break;
3756 case HBA_RESP_STAT_IO_ABORTED:
3757 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3758 break;
3759 case HBA_RESP_STAT_INVALID_DEVICE:
3760 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3761 break;
3762 case HBA_RESP_STAT_UNDERRUN:
3763 /* UNDERRUN is OK */
3764 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3765 break;
3766 case HBA_RESP_STAT_OVERRUN:
3767 default:
3768 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3769 break;
3770 }
3771}
3772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773/**
3774 *
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003775 * aac_hba_callback
3776 * @context: the context set in the fib - here it is scsi cmd
3777 * @fibptr: pointer to the fib
3778 *
3779 * Handles the completion of a native HBA scsi command
3780 *
3781 */
3782void aac_hba_callback(void *context, struct fib *fibptr)
3783{
3784 struct aac_dev *dev;
3785 struct scsi_cmnd *scsicmd;
3786
3787 struct aac_hba_resp *err =
3788 &((struct aac_native_hba *)fibptr->hw_fib_va)->resp.err;
3789
3790 scsicmd = (struct scsi_cmnd *) context;
3791
3792 if (!aac_valid_context(scsicmd, fibptr))
3793 return;
3794
3795 WARN_ON(fibptr == NULL);
3796 dev = fibptr->dev;
3797
3798 if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF))
3799 scsi_dma_unmap(scsicmd);
3800
3801 if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3802 /* fast response */
3803 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3804 goto out;
3805 }
3806
3807 switch (err->service_response) {
3808 case HBA_RESP_SVCRES_TASK_COMPLETE:
3809 hba_resp_task_complete(dev, scsicmd, err);
3810 break;
3811 case HBA_RESP_SVCRES_FAILURE:
3812 hba_resp_task_failure(dev, scsicmd, err);
3813 break;
3814 case HBA_RESP_SVCRES_TMF_REJECTED:
3815 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
3816 break;
3817 case HBA_RESP_SVCRES_TMF_LUN_INVALID:
3818 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
3819 break;
3820 case HBA_RESP_SVCRES_TMF_COMPLETE:
3821 case HBA_RESP_SVCRES_TMF_SUCCEEDED:
3822 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3823 break;
3824 default:
3825 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
3826 break;
3827 }
3828
3829out:
3830 aac_fib_complete(fibptr);
3831
3832 if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA_TMF)
3833 scsicmd->SCp.sent_command = 1;
3834 else
3835 scsicmd->scsi_done(scsicmd);
3836}
3837
3838/**
3839 *
3840 * aac_send_srb_fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 * @scsicmd: the scsi command block
3842 *
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08003843 * This routine will form a FIB and fill in the aac_srb from the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 * scsicmd passed in.
3845 */
3846
3847static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3848{
3849 struct fib* cmd_fibcontext;
3850 struct aac_dev* dev;
3851 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Mark Haverkamp84971732005-06-20 11:55:24 -07003853 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08003854 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07003855 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 scsicmd->result = DID_NO_CONNECT << 16;
3857 scsicmd->scsi_done(scsicmd);
3858 return 0;
3859 }
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 /*
3862 * Allocate and initialize a Fib then setup a BlockWrite command
3863 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08003864 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
Brian King1ae948f2017-08-29 10:00:29 -05003865 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08003866 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 /*
3869 * Check that the command queued to the controller
3870 */
Brian King1ae948f2017-08-29 10:00:29 -05003871 if (status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003874 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3875 aac_fib_complete(cmd_fibcontext);
3876 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 return -1;
3879}
3880
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003881/**
3882 *
3883 * aac_send_hba_fib
3884 * @scsicmd: the scsi command block
3885 *
3886 * This routine will form a FIB and fill in the aac_hba_cmd_req from the
3887 * scsicmd passed in.
3888 */
3889static int aac_send_hba_fib(struct scsi_cmnd *scsicmd)
3890{
3891 struct fib *cmd_fibcontext;
3892 struct aac_dev *dev;
3893 int status;
3894
3895 dev = shost_priv(scsicmd->device->host);
3896 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3897 scsicmd->device->lun > AAC_MAX_LUN - 1) {
3898 scsicmd->result = DID_NO_CONNECT << 16;
3899 scsicmd->scsi_done(scsicmd);
3900 return 0;
3901 }
3902
3903 /*
3904 * Allocate and initialize a Fib then setup a BlockWrite command
3905 */
3906 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3907 if (!cmd_fibcontext)
3908 return -1;
3909
Brian King1ae948f2017-08-29 10:00:29 -05003910 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003911 status = aac_adapter_hba(cmd_fibcontext, scsicmd);
3912
3913 /*
3914 * Check that the command queued to the controller
3915 */
Brian King1ae948f2017-08-29 10:00:29 -05003916 if (status == -EINPROGRESS)
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003917 return 0;
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08003918
3919 pr_warn("aac_hba_cmd_req: aac_fib_send failed with status: %d\n",
3920 status);
3921 aac_fib_complete(cmd_fibcontext);
3922 aac_fib_free(cmd_fibcontext);
3923
3924 return -1;
3925}
3926
3927
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303928static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
3930 struct aac_dev *dev;
3931 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003932 int nseg;
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003933 struct scatterlist *sg;
3934 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3937 // Get rid of old data
3938 psg->count = 0;
3939 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003940 psg->sg[0].count = 0;
3941
3942 nseg = scsi_dma_map(scsicmd);
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003943 if (nseg <= 0)
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303944 return nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003946 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003948 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3949 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3950 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3951 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003953 /* hba wants the size to be exact */
3954 if (byte_count > scsi_bufflen(scsicmd)) {
3955 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3956 (byte_count - scsi_bufflen(scsicmd));
3957 psg->sg[i-1].count = cpu_to_le32(temp);
3958 byte_count = scsi_bufflen(scsicmd);
3959 }
3960 /* Check for command underflow */
3961 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3962 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3963 byte_count, scsicmd->underflow);
3964 }
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return byte_count;
3967}
3968
3969
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303970static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
3972 struct aac_dev *dev;
3973 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003974 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003975 int nseg;
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003976 struct scatterlist *sg;
3977 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3980 // Get rid of old data
3981 psg->count = 0;
3982 psg->sg[0].addr[0] = 0;
3983 psg->sg[0].addr[1] = 0;
3984 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003985
3986 nseg = scsi_dma_map(scsicmd);
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003987 if (nseg <= 0)
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303988 return nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003990 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3991 int count = sg_dma_len(sg);
3992 addr = sg_dma_address(sg);
3993 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3994 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
3995 psg->sg[i].count = cpu_to_le32(count);
3996 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
Nikola Pajkovskya2260322017-08-29 13:59:03 +02003998 psg->count = cpu_to_le32(nseg);
3999 /* hba wants the size to be exact */
4000 if (byte_count > scsi_bufflen(scsicmd)) {
4001 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
4002 (byte_count - scsi_bufflen(scsicmd));
4003 psg->sg[i-1].count = cpu_to_le32(temp);
4004 byte_count = scsi_bufflen(scsicmd);
4005 }
4006 /* Check for command underflow */
4007 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4008 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
4009 byte_count, scsicmd->underflow);
4010 }
4011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return byte_count;
4013}
4014
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05304015static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
Mark Haverkamp0e68c002005-08-03 15:39:49 -07004016{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07004017 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09004018 int nseg;
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004019 struct scatterlist *sg;
4020 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07004021
4022 // Get rid of old data
4023 psg->count = 0;
4024 psg->sg[0].next = 0;
4025 psg->sg[0].prev = 0;
4026 psg->sg[0].addr[0] = 0;
4027 psg->sg[0].addr[1] = 0;
4028 psg->sg[0].count = 0;
4029 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09004030
4031 nseg = scsi_dma_map(scsicmd);
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004032 if (nseg <= 0)
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05304033 return nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07004034
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004035 scsi_for_each_sg(scsicmd, sg, nseg, i) {
4036 int count = sg_dma_len(sg);
4037 u64 addr = sg_dma_address(sg);
4038 psg->sg[i].next = 0;
4039 psg->sg[i].prev = 0;
4040 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
4041 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
4042 psg->sg[i].count = cpu_to_le32(count);
4043 psg->sg[i].flags = 0;
4044 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07004045 }
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004046 psg->count = cpu_to_le32(nseg);
4047 /* hba wants the size to be exact */
4048 if (byte_count > scsi_bufflen(scsicmd)) {
4049 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
4050 (byte_count - scsi_bufflen(scsicmd));
4051 psg->sg[i-1].count = cpu_to_le32(temp);
4052 byte_count = scsi_bufflen(scsicmd);
4053 }
4054 /* Check for command underflow */
4055 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4056 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
4057 byte_count, scsicmd->underflow);
4058 }
4059
Mark Haverkamp0e68c002005-08-03 15:39:49 -07004060 return byte_count;
4061}
4062
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05304063static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
4064 struct aac_raw_io2 *rio2, int sg_max)
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304065{
4066 unsigned long byte_count = 0;
4067 int nseg;
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004068 struct scatterlist *sg;
4069 int i, conformable = 0;
4070 u32 min_size = PAGE_SIZE, cur_size;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304071
4072 nseg = scsi_dma_map(scsicmd);
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004073 if (nseg <= 0)
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05304074 return nseg;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304075
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004076 scsi_for_each_sg(scsicmd, sg, nseg, i) {
4077 int count = sg_dma_len(sg);
4078 u64 addr = sg_dma_address(sg);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304079
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004080 BUG_ON(i >= sg_max);
4081 rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
4082 rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
4083 cur_size = cpu_to_le32(count);
4084 rio2->sge[i].length = cur_size;
4085 rio2->sge[i].flags = 0;
4086 if (i == 0) {
4087 conformable = 1;
4088 rio2->sgeFirstSize = cur_size;
4089 } else if (i == 1) {
4090 rio2->sgeNominalSize = cur_size;
4091 min_size = cur_size;
4092 } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
4093 conformable = 0;
4094 if (cur_size < min_size)
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304095 min_size = cur_size;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304096 }
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004097 byte_count += count;
4098 }
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304099
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004100 /* hba wants the size to be exact */
4101 if (byte_count > scsi_bufflen(scsicmd)) {
4102 u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
4103 (byte_count - scsi_bufflen(scsicmd));
4104 rio2->sge[i-1].length = cpu_to_le32(temp);
4105 byte_count = scsi_bufflen(scsicmd);
4106 }
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304107
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004108 rio2->sgeCnt = cpu_to_le32(nseg);
4109 rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
4110 /* not conformable: evaluate required sg elements */
4111 if (!conformable) {
4112 int j, nseg_new = nseg, err_found;
4113 for (i = min_size / PAGE_SIZE; i >= 1; --i) {
4114 err_found = 0;
4115 nseg_new = 2;
4116 for (j = 1; j < nseg - 1; ++j) {
4117 if (rio2->sge[j].length % (i*PAGE_SIZE)) {
4118 err_found = 1;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304119 break;
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004120 }
4121 nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304122 }
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004123 if (!err_found)
4124 break;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304125 }
Nikola Pajkovsky96676242017-08-29 13:59:04 +02004126 if (i > 0 && nseg_new <= sg_max) {
4127 int ret = aac_convert_sgraw2(rio2, i, nseg, nseg_new);
4128
4129 if (ret < 0)
4130 return ret;
4131 }
Nikola Pajkovskya2260322017-08-29 13:59:03 +02004132 } else
4133 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4134
4135 /* Check for command underflow */
4136 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4137 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
4138 byte_count, scsicmd->underflow);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304139 }
4140
4141 return byte_count;
4142}
4143
4144static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
4145{
4146 struct sge_ieee1212 *sge;
4147 int i, j, pos;
4148 u32 addr_low;
4149
4150 if (aac_convert_sgl == 0)
4151 return 0;
4152
4153 sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
4154 if (sge == NULL)
Nikola Pajkovsky96676242017-08-29 13:59:04 +02004155 return -ENOMEM;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05304156
4157 for (i = 1, pos = 1; i < nseg-1; ++i) {
4158 for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
4159 addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
4160 sge[pos].addrLow = addr_low;
4161 sge[pos].addrHigh = rio2->sge[i].addrHigh;
4162 if (addr_low < rio2->sge[i].addrLow)
4163 sge[pos].addrHigh++;
4164 sge[pos].length = pages * PAGE_SIZE;
4165 sge[pos].flags = 0;
4166 pos++;
4167 }
4168 }
4169 sge[pos] = rio2->sge[nseg-1];
4170 memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
4171
4172 kfree(sge);
4173 rio2->sgeCnt = cpu_to_le32(nseg_new);
4174 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
4175 rio2->sgeNominalSize = pages * PAGE_SIZE;
4176 return 0;
4177}
4178
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08004179static long aac_build_sghba(struct scsi_cmnd *scsicmd,
4180 struct aac_hba_cmd_req *hbacmd,
4181 int sg_max,
4182 u64 sg_address)
4183{
4184 unsigned long byte_count = 0;
4185 int nseg;
4186 struct scatterlist *sg;
4187 int i;
4188 u32 cur_size;
4189 struct aac_hba_sgl *sge;
4190
Raghava Aditya Renukuntaab5d1292017-02-02 15:53:30 -08004191 nseg = scsi_dma_map(scsicmd);
4192 if (nseg <= 0) {
4193 byte_count = nseg;
4194 goto out;
4195 }
4196
4197 if (nseg > HBA_MAX_SG_EMBEDDED)
4198 sge = &hbacmd->sge[2];
4199 else
4200 sge = &hbacmd->sge[0];
4201
4202 scsi_for_each_sg(scsicmd, sg, nseg, i) {
4203 int count = sg_dma_len(sg);
4204 u64 addr = sg_dma_address(sg);
4205
4206 WARN_ON(i >= sg_max);
4207 sge->addr_hi = cpu_to_le32((u32)(addr>>32));
4208 sge->addr_lo = cpu_to_le32((u32)(addr & 0xffffffff));
4209 cur_size = cpu_to_le32(count);
4210 sge->len = cur_size;
4211 sge->flags = 0;
4212 byte_count += count;
4213 sge++;
4214 }
4215
4216 sge--;
4217 /* hba wants the size to be exact */
4218 if (byte_count > scsi_bufflen(scsicmd)) {
4219 u32 temp;
4220
4221 temp = le32_to_cpu(sge->len) - byte_count
4222 - scsi_bufflen(scsicmd);
4223 sge->len = cpu_to_le32(temp);
4224 byte_count = scsi_bufflen(scsicmd);
4225 }
4226
4227 if (nseg <= HBA_MAX_SG_EMBEDDED) {
4228 hbacmd->emb_data_desc_count = cpu_to_le32(nseg);
4229 sge->flags = cpu_to_le32(0x40000000);
4230 } else {
4231 /* not embedded */
4232 hbacmd->sge[0].flags = cpu_to_le32(0x80000000);
4233 hbacmd->emb_data_desc_count = (u8)cpu_to_le32(1);
4234 hbacmd->sge[0].addr_hi = (u32)cpu_to_le32(sg_address >> 32);
4235 hbacmd->sge[0].addr_lo =
4236 cpu_to_le32((u32)(sg_address & 0xffffffff));
4237 }
4238
4239 /* Check for command underflow */
4240 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
4241 pr_warn("aacraid: cmd len %08lX cmd underflow %08X\n",
4242 byte_count, scsicmd->underflow);
4243 }
4244out:
4245 return byte_count;
4246}
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248#ifdef AAC_DETAILED_STATUS_INFO
4249
4250struct aac_srb_status_info {
4251 u32 status;
4252 char *str;
4253};
4254
4255
4256static struct aac_srb_status_info srb_status_info[] = {
4257 { SRB_STATUS_PENDING, "Pending Status"},
4258 { SRB_STATUS_SUCCESS, "Success"},
4259 { SRB_STATUS_ABORTED, "Aborted Command"},
4260 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07004261 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 { SRB_STATUS_BUSY, "Device Busy"},
4263 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
4264 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
4265 { SRB_STATUS_NO_DEVICE, "No Device"},
4266 { SRB_STATUS_TIMEOUT, "Timeout"},
4267 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
4268 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
4269 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
4270 { SRB_STATUS_BUS_RESET, "Bus Reset"},
4271 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
4272 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
4273 { SRB_STATUS_NO_HBA, "No HBA"},
4274 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
4275 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
4276 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
4277 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
4278 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
4279 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07004280 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
4282 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
4283 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
4284 { SRB_STATUS_NOT_STARTED, "Not Started"},
4285 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08004286 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
4288 { 0xff, "Unknown Error"}
4289};
4290
4291char *aac_get_status_string(u32 status)
4292{
4293 int i;
4294
Tobias Klauser6391a112006-06-08 22:23:48 -07004295 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
4296 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 return "Bad Status Code";
4300}
4301
4302#endif