blob: 7dfd0fa272555dc17cf63200a19acc3e492e7e24 [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.
9 * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
26
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/pci.h>
31#include <linux/spinlock.h>
32#include <linux/slab.h>
33#include <linux/completion.h>
34#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/uaccess.h>
Salyzyn, Mark3a0086a2008-01-11 11:46:44 -080036#include <linux/highmem.h> /* For flush_kernel_dcache_page */
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040037#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <scsi/scsi.h>
40#include <scsi/scsi_cmnd.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_host.h>
43
44#include "aacraid.h"
45
46/* values for inqd_pdt: Peripheral device type in plain English */
47#define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
48#define INQD_PDT_PROC 0x03 /* Processor device */
49#define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
50#define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
51#define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
52#define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
53
54#define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
55#define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
58 * Sense codes
59 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080060
61#define SENCODE_NO_SENSE 0x00
62#define SENCODE_END_OF_DATA 0x00
63#define SENCODE_BECOMING_READY 0x04
64#define SENCODE_INIT_CMD_REQUIRED 0x04
65#define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
66#define SENCODE_INVALID_COMMAND 0x20
67#define SENCODE_LBA_OUT_OF_RANGE 0x21
68#define SENCODE_INVALID_CDB_FIELD 0x24
69#define SENCODE_LUN_NOT_SUPPORTED 0x25
70#define SENCODE_INVALID_PARAM_FIELD 0x26
71#define SENCODE_PARAM_NOT_SUPPORTED 0x26
72#define SENCODE_PARAM_VALUE_INVALID 0x26
73#define SENCODE_RESET_OCCURRED 0x29
74#define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
75#define SENCODE_INQUIRY_DATA_CHANGED 0x3F
76#define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
77#define SENCODE_DIAGNOSTIC_FAILURE 0x40
78#define SENCODE_INTERNAL_TARGET_FAILURE 0x44
79#define SENCODE_INVALID_MESSAGE_ERROR 0x49
80#define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
81#define SENCODE_OVERLAPPED_COMMAND 0x4E
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * Additional sense codes
85 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -080086
87#define ASENCODE_NO_SENSE 0x00
88#define ASENCODE_END_OF_DATA 0x05
89#define ASENCODE_BECOMING_READY 0x01
90#define ASENCODE_INIT_CMD_REQUIRED 0x02
91#define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
92#define ASENCODE_INVALID_COMMAND 0x00
93#define ASENCODE_LBA_OUT_OF_RANGE 0x00
94#define ASENCODE_INVALID_CDB_FIELD 0x00
95#define ASENCODE_LUN_NOT_SUPPORTED 0x00
96#define ASENCODE_INVALID_PARAM_FIELD 0x00
97#define ASENCODE_PARAM_NOT_SUPPORTED 0x01
98#define ASENCODE_PARAM_VALUE_INVALID 0x02
99#define ASENCODE_RESET_OCCURRED 0x00
100#define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
101#define ASENCODE_INQUIRY_DATA_CHANGED 0x03
102#define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
103#define ASENCODE_DIAGNOSTIC_FAILURE 0x80
104#define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
105#define ASENCODE_INVALID_MESSAGE_ERROR 0x00
106#define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
107#define ASENCODE_OVERLAPPED_COMMAND 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109#define BYTE0(x) (unsigned char)(x)
110#define BYTE1(x) (unsigned char)((x) >> 8)
111#define BYTE2(x) (unsigned char)((x) >> 16)
112#define BYTE3(x) (unsigned char)((x) >> 24)
113
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400114/* MODE_SENSE data format */
115typedef struct {
116 struct {
117 u8 data_length;
118 u8 med_type;
119 u8 dev_par;
120 u8 bd_length;
121 } __attribute__((packed)) hd;
122 struct {
123 u8 dens_code;
124 u8 block_count[3];
125 u8 reserved;
126 u8 block_length[3];
127 } __attribute__((packed)) bd;
128 u8 mpc_buf[3];
129} __attribute__((packed)) aac_modep_data;
130
131/* MODE_SENSE_10 data format */
132typedef struct {
133 struct {
134 u8 data_length[2];
135 u8 med_type;
136 u8 dev_par;
137 u8 rsrvd[2];
138 u8 bd_length[2];
139 } __attribute__((packed)) hd;
140 struct {
141 u8 dens_code;
142 u8 block_count[3];
143 u8 reserved;
144 u8 block_length[3];
145 } __attribute__((packed)) bd;
146 u8 mpc_buf[3];
147} __attribute__((packed)) aac_modep10_data;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*------------------------------------------------------------------------------
150 * S T R U C T S / T Y P E D E F S
151 *----------------------------------------------------------------------------*/
152/* SCSI inquiry data */
153struct inquiry_data {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800154 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
155 u8 inqd_dtq; /* RMB | Device Type Qualifier */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
157 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
158 u8 inqd_len; /* Additional length (n-4) */
159 u8 inqd_pad1[2];/* Reserved - must be zero */
160 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
161 u8 inqd_vid[8]; /* Vendor ID */
162 u8 inqd_pid[16];/* Product ID */
163 u8 inqd_prl[4]; /* Product Revision Level */
164};
165
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400166/* Added for VPD 0x83 */
167typedef struct {
168 u8 CodeSet:4; /* VPD_CODE_SET */
169 u8 Reserved:4;
170 u8 IdentifierType:4; /* VPD_IDENTIFIER_TYPE */
171 u8 Reserved2:4;
172 u8 Reserved3;
173 u8 IdentifierLength;
174 u8 VendId[8];
175 u8 ProductId[16];
176 u8 SerialNumber[8]; /* SN in ASCII */
177
178} TVPD_ID_Descriptor_Type_1;
179
180typedef struct {
181 u8 CodeSet:4; /* VPD_CODE_SET */
182 u8 Reserved:4;
183 u8 IdentifierType:4; /* VPD_IDENTIFIER_TYPE */
184 u8 Reserved2:4;
185 u8 Reserved3;
186 u8 IdentifierLength;
187 struct TEU64Id {
188 u32 Serial;
189 /* The serial number supposed to be 40 bits,
190 * bit we only support 32, so make the last byte zero. */
191 u8 Reserved;
192 u8 VendId[3];
193 } EU64Id;
194
195} TVPD_ID_Descriptor_Type_2;
196
197typedef struct {
198 u8 DeviceType:5;
199 u8 DeviceTypeQualifier:3;
200 u8 PageCode;
201 u8 Reserved;
202 u8 PageLength;
203 TVPD_ID_Descriptor_Type_1 IdDescriptorType1;
204 TVPD_ID_Descriptor_Type_2 IdDescriptorType2;
205
206} TVPD_Page83;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
209 * M O D U L E G L O B A L S
210 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800211
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +0530212static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
213static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
214static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
215static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
216 struct aac_raw_io2 *rio2, int sg_max);
217static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
218 int pages, int nseg, int nseg_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
220#ifdef AAC_DETAILED_STATUS_INFO
221static char *aac_get_status_string(u32 status);
222#endif
223
224/*
225 * Non dasd selection is handled entirely in aachba now
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800226 */
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int nondasd = -1;
Leubner, Achimd8e965072009-04-01 07:16:08 -0700229static int aac_cache = 2; /* WCE=0 to avoid performance problems */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int dacmode = -1;
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800231int aac_msi;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400232int aac_commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700233int startup_timeout = 180;
234int aif_timeout = 120;
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800235int aac_sync_mode; /* Only Sync. transfer - disabled */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530236int aac_convert_sgl = 1; /* convert non-conformable s/g list - enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800238module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
239MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
240 " 0=off, 1=on");
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530241module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
242MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
243 " 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800244module_param(nondasd, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800245MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
246 " 0=off, 1=on");
Salyzyn, Mark95e852e2008-01-08 12:01:07 -0800247module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800248MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
249 "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
250 "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
Leubner, Achimd8e965072009-04-01 07:16:08 -0700251 "\tbit 2 - Disable only if Battery is protecting Cache");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800252module_param(dacmode, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800253MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
254 " 0=off, 1=on");
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400255module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800256MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
257 " adapter for foreign arrays.\n"
258 "This is typically needed in systems that do not have a BIOS."
259 " 0=off, 1=on");
260module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
261MODULE_PARM_DESC(msi, "IRQ handling."
Mahesh Rajashekhara9022d372015-08-28 06:38:35 -0400262 " 0=PIC(default), 1=MSI, 2=MSI-X)");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700263module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800264MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
265 " adapter to have it's kernel up and\n"
266 "running. This is typically adjusted for large systems that do not"
267 " have a BIOS.");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700268module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800269MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
270 " applications to pick up AIFs before\n"
271 "deregistering them. This is typically adjusted for heavily burdened"
272 " systems.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700274int numacb = -1;
275module_param(numacb, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800276MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
277 " blocks (FIB) allocated. Valid values are 512 and down. Default is"
278 " to use suggestion from Firmware.");
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700279
280int acbsize = -1;
281module_param(acbsize, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800282MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
283 " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
284 " suggestion from Firmware.");
Mark Haverkamp653ba582006-09-19 08:59:43 -0700285
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400286int update_interval = 30 * 60;
287module_param(update_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800288MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
289 " updates issued to adapter.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400290
291int check_interval = 24 * 60 * 60;
292module_param(check_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800293MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
294 " checks.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400295
Andrew Morton87f3bda2007-08-10 14:50:30 -0700296int aac_check_reset = 1;
297module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
Paul Bolle95e7a8e2008-04-16 14:43:00 +0200298MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800299 " adapter. a value of -1 forces the reset to adapters programmed to"
300 " ignore it.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400301
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800302int expose_physicals = -1;
Mark Haverkamp653ba582006-09-19 08:59:43 -0700303module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800304MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
305 " -1=protect 0=off, 1=on");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700306
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800307int aac_reset_devices;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400308module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
309MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700310
Leubner, Achimd8e965072009-04-01 07:16:08 -0700311int aac_wwn = 1;
312module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
313MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
314 "\t0 - Disable\n"
315 "\t1 - Array Meta Data Signature (default)\n"
316 "\t2 - Adapter Serial Number");
317
318
Mark Haverkamp03d44332007-03-15 10:27:45 -0700319static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
320 struct fib *fibptr) {
321 struct scsi_device *device;
322
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800323 if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
Salyzyn, Markc835e372007-07-26 14:20:02 -0400324 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800325 aac_fib_complete(fibptr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800326 return 0;
327 }
Mark Haverkamp03d44332007-03-15 10:27:45 -0700328 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
329 device = scsicmd->device;
330 if (unlikely(!device || !scsi_device_online(device))) {
331 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
332 aac_fib_complete(fibptr);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700333 return 0;
334 }
335 return 1;
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/**
339 * aac_get_config_status - check the adapter configuration
340 * @common: adapter to query
341 *
342 * Query config status, and commit the configuration if needed.
343 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700344int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 int status = 0;
347 struct fib * fibptr;
348
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800349 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -ENOMEM;
351
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800352 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 {
354 struct aac_get_config_status *dinfo;
355 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
356
357 dinfo->command = cpu_to_le32(VM_ContainerConfig);
358 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
359 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
360 }
361
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800362 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 fibptr,
364 sizeof (struct aac_get_config_status),
365 FsaNormal,
366 1, 1,
367 NULL, NULL);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800368 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
370 } else {
371 struct aac_get_config_status_resp *reply
372 = (struct aac_get_config_status_resp *) fib_data(fibptr);
373 dprintk((KERN_WARNING
374 "aac_get_config_status: response=%d status=%d action=%d\n",
375 le32_to_cpu(reply->response),
376 le32_to_cpu(reply->status),
377 le32_to_cpu(reply->data.action)));
378 if ((le32_to_cpu(reply->response) != ST_OK) ||
379 (le32_to_cpu(reply->status) != CT_OK) ||
380 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
381 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
382 status = -EINVAL;
383 }
384 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530385 /* Do not set XferState to zero unless receives a response from F/W */
386 if (status >= 0)
387 aac_fib_complete(fibptr);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
390 if (status >= 0) {
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400391 if ((aac_commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800393 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 dinfo = (struct aac_commit_config *) fib_data(fibptr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 dinfo->command = cpu_to_le32(VM_ContainerConfig);
397 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800398
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800399 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 fibptr,
401 sizeof (struct aac_commit_config),
402 FsaNormal,
403 1, 1,
404 NULL, NULL);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530405 /* Do not set XferState to zero unless
406 * receives a response from F/W */
407 if (status >= 0)
408 aac_fib_complete(fibptr);
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400409 } else if (aac_commit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 printk(KERN_WARNING
411 "aac_get_config_status: Foreign device configurations are being ignored\n");
412 }
413 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530414 /* FIB should be freed only after getting the response from the F/W */
415 if (status != -ERESTARTSYS)
416 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return status;
418}
419
Rajashekhara, Maheshe3cc2682010-05-10 04:12:28 -0700420static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
421{
422 char inq_data;
423 scsi_sg_copy_to_buffer(scsicmd, &inq_data, sizeof(inq_data));
424 if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
425 inq_data &= 0xdf;
426 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
427 }
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/**
431 * aac_get_containers - list containers
432 * @common: adapter to probe
433 *
434 * Make a list of all containers on this controller
435 */
436int aac_get_containers(struct aac_dev *dev)
437{
438 struct fsa_dev_info *fsa_dev_ptr;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800439 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int status = 0;
441 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct aac_get_container_count *dinfo;
443 struct aac_get_container_count_resp *dresp;
444 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
445
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800446 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENOMEM;
448
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800449 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
451 dinfo->command = cpu_to_le32(VM_ContainerConfig);
452 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
453
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800454 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 fibptr,
456 sizeof (struct aac_get_container_count),
457 FsaNormal,
458 1, 1,
459 NULL, NULL);
460 if (status >= 0) {
461 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
462 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mahesh Rajashekharaa7129a52015-03-26 10:41:27 -0400463 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
464 AAC_OPTION_SUPPORTED_240_VOLUMES) {
465 maximum_num_containers =
466 le32_to_cpu(dresp->MaxSimpleVolumes);
467 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800468 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530470 /* FIB should be freed only after getting the response from the F/W */
471 if (status != -ERESTARTSYS)
472 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
475 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400476 fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700477 GFP_KERNEL);
478 if (!fsa_dev_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 dev->fsa_dev = fsa_dev_ptr;
482 dev->maximum_num_containers = maximum_num_containers;
483
Mark Haverkampfe76df42007-03-15 12:55:07 -0700484 for (index = 0; index < dev->maximum_num_containers; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 fsa_dev_ptr[index].devname[0] = '\0';
486
Mark Haverkampfe76df42007-03-15 12:55:07 -0700487 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Mark Haverkampfe76df42007-03-15 12:55:07 -0700489 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
491 break;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
495 * If there are no more containers, then stop asking.
496 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700497 if (++index >= status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return status;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static void get_container_name_callback(void *context, struct fib * fibptr)
504{
505 struct aac_get_name_resp * get_name_reply;
506 struct scsi_cmnd * scsicmd;
507
508 scsicmd = (struct scsi_cmnd *) context;
509
Mark Haverkamp03d44332007-03-15 10:27:45 -0700510 if (!aac_valid_context(scsicmd, fibptr))
511 return;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700514 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
517 /* Failure is irrelevant, using default value instead */
518 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
519 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700520 char *sp = get_name_reply->data;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400521 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 while (*sp == ' ')
523 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700524 if (*sp) {
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900525 struct inquiry_data inq;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700526 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
527 int count = sizeof(d);
528 char *dp = d;
529 do {
530 *dp++ = (*sp) ? *sp++ : ' ';
531 } while (--count > 0);
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900532
533 scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
534 memcpy(inq.inqd_pid, d, sizeof(d));
535 scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
540
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800541 aac_fib_complete(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700542 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/**
546 * aac_get_container_name - get container name, none blocking.
547 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700548static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 int status;
551 struct aac_get_name *dinfo;
552 struct fib * cmd_fibcontext;
553 struct aac_dev * dev;
554
555 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
556
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -0800557 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
558 if (!cmd_fibcontext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENOMEM;
560
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800561 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
563
564 dinfo->command = cpu_to_le32(VM_ContainerConfig);
565 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700566 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
568
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800569 status = aac_fib_send(ContainerCommand,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800570 cmd_fibcontext,
Mahesh Rajashekharafb5d40d2015-08-28 06:38:33 -0400571 sizeof(struct aac_get_name_resp),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800572 FsaNormal,
573 0, 1,
574 (fib_callback)get_container_name_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 (void *) scsicmd);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * Check that the command queued to the controller
579 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800580 if (status == -EINPROGRESS) {
581 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800583 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800584
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800585 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
586 aac_fib_complete(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return -1;
588}
589
Mark Haverkampfe76df42007-03-15 12:55:07 -0700590static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
591{
592 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
593
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400594 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700595 return aac_scsi_cmd(scsicmd);
596
597 scsicmd->result = DID_NO_CONNECT << 16;
598 scsicmd->scsi_done(scsicmd);
599 return 0;
600}
601
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400602static void _aac_probe_container2(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700603{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700604 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700605 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700606 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
607
Mark Haverkamp03d44332007-03-15 10:27:45 -0700608
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400609 if (!aac_valid_context(scsicmd, fibptr))
610 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700611
612 scsicmd->SCp.Status = 0;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400613 fsa_dev_ptr = fibptr->dev->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700614 if (fsa_dev_ptr) {
615 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
616 fsa_dev_ptr += scmd_id(scsicmd);
617
618 if ((le32_to_cpu(dresp->status) == ST_OK) &&
619 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
620 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400621 if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
622 AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
623 dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
624 fsa_dev_ptr->block_size = 0x200;
625 } else {
626 fsa_dev_ptr->block_size =
627 le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
628 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700629 fsa_dev_ptr->valid = 1;
Mark Salyzyn655d7222008-04-30 16:03:42 -0400630 /* sense_key holds the current state of the spin-up */
631 if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
632 fsa_dev_ptr->sense_data.sense_key = NOT_READY;
633 else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
634 fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700635 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
636 fsa_dev_ptr->size
637 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
638 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
639 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
640 }
641 if ((fsa_dev_ptr->valid & 1) == 0)
642 fsa_dev_ptr->valid = 0;
643 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
644 }
645 aac_fib_complete(fibptr);
646 aac_fib_free(fibptr);
647 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
648 scsicmd->SCp.ptr = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400649 (*callback)(scsicmd);
650 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700651}
652
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400653static void _aac_probe_container1(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700654{
655 struct scsi_cmnd * scsicmd;
656 struct aac_mount * dresp;
657 struct aac_query_mount *dinfo;
658 int status;
659
660 dresp = (struct aac_mount *) fib_data(fibptr);
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400661 if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
662 AAC_OPTION_VARIABLE_BLOCK_SIZE))
663 dresp->mnt[0].capacityhigh = 0;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700664 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400665 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
666 _aac_probe_container2(context, fibptr);
667 return;
668 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700669 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700670
Mark Haverkamp03d44332007-03-15 10:27:45 -0700671 if (!aac_valid_context(scsicmd, fibptr))
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400672 return;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700673
Mark Haverkampfe76df42007-03-15 12:55:07 -0700674 aac_fib_init(fibptr);
675
676 dinfo = (struct aac_query_mount *)fib_data(fibptr);
677
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400678 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
679 AAC_OPTION_VARIABLE_BLOCK_SIZE)
680 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
681 else
682 dinfo->command = cpu_to_le32(VM_NameServe64);
683
Mark Haverkampfe76df42007-03-15 12:55:07 -0700684 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
685 dinfo->type = cpu_to_le32(FT_FILESYS);
686
687 status = aac_fib_send(ContainerCommand,
688 fibptr,
689 sizeof(struct aac_query_mount),
690 FsaNormal,
691 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400692 _aac_probe_container2,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700693 (void *) scsicmd);
694 /*
695 * Check that the command queued to the controller
696 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400697 if (status == -EINPROGRESS)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700698 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400699 else if (status < 0) {
Mark Haverkampfe76df42007-03-15 12:55:07 -0700700 /* Inherit results from VM_NameServe, if any */
701 dresp->status = cpu_to_le32(ST_OK);
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400702 _aac_probe_container2(context, fibptr);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700703 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700704}
705
706static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
707{
708 struct fib * fibptr;
709 int status = -ENOMEM;
710
711 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
712 struct aac_query_mount *dinfo;
713
714 aac_fib_init(fibptr);
715
716 dinfo = (struct aac_query_mount *)fib_data(fibptr);
717
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400718 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
719 AAC_OPTION_VARIABLE_BLOCK_SIZE)
720 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
721 else
722 dinfo->command = cpu_to_le32(VM_NameServe);
723
Mark Haverkampfe76df42007-03-15 12:55:07 -0700724 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
725 dinfo->type = cpu_to_le32(FT_FILESYS);
726 scsicmd->SCp.ptr = (char *)callback;
727
728 status = aac_fib_send(ContainerCommand,
729 fibptr,
730 sizeof(struct aac_query_mount),
731 FsaNormal,
732 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400733 _aac_probe_container1,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700734 (void *) scsicmd);
735 /*
736 * Check that the command queued to the controller
737 */
738 if (status == -EINPROGRESS) {
739 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
740 return 0;
741 }
742 if (status < 0) {
743 scsicmd->SCp.ptr = NULL;
744 aac_fib_complete(fibptr);
745 aac_fib_free(fibptr);
746 }
747 }
748 if (status < 0) {
749 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
750 if (fsa_dev_ptr) {
751 fsa_dev_ptr += scmd_id(scsicmd);
752 if ((fsa_dev_ptr->valid & 1) == 0) {
753 fsa_dev_ptr->valid = 0;
754 return (*callback)(scsicmd);
755 }
756 }
757 }
758 return status;
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800762 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * @dev: device to query
764 * @cid: container identifier
765 *
766 * Queries the controller about the given volume. The volume information
767 * is updated in the struct fsa_dev_info structure rather than returned.
768 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700769static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
770{
771 scsicmd->device = NULL;
772 return 0;
773}
774
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800775int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700777 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
778 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Mark Haverkampfe76df42007-03-15 12:55:07 -0700781 if (!scsicmd || !scsidev) {
782 kfree(scsicmd);
783 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700784 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700786 scsicmd->list.next = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400787 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Mark Haverkampfe76df42007-03-15 12:55:07 -0700789 scsicmd->device = scsidev;
790 scsidev->sdev_state = 0;
791 scsidev->id = cid;
792 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Mark Haverkampfe76df42007-03-15 12:55:07 -0700794 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
795 while (scsicmd->device == scsidev)
796 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400797 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700798 status = scsicmd->SCp.Status;
799 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return status;
801}
802
803/* Local Structure to set SCSI inquiry data strings */
804struct scsi_inq {
805 char vid[8]; /* Vendor ID */
806 char pid[16]; /* Product ID */
807 char prl[4]; /* Product Revision Level */
808};
809
810/**
811 * InqStrCopy - string merge
812 * @a: string to copy from
813 * @b: string to copy to
814 *
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800815 * Copy a String from one location to another
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 * without copying \0
817 */
818
819static void inqstrcpy(char *a, char *b)
820{
821
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800822 while (*a != (char)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 *b++ = *a++;
824}
825
826static char *container_types[] = {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800827 "None",
828 "Volume",
829 "Mirror",
830 "Stripe",
831 "RAID5",
832 "SSRW",
833 "SSRO",
834 "Morph",
835 "Legacy",
836 "RAID4",
837 "RAID10",
838 "RAID00",
839 "V-MIRRORS",
840 "PSEUDO R4",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700842 "RAID5D",
843 "RAID5D0",
844 "RAID1E",
845 "RAID6",
846 "RAID60",
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800847 "Unknown"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848};
849
Salyzyn, Mark17eaace2008-01-08 12:23:49 -0800850char * get_container_type(unsigned tindex)
851{
852 if (tindex >= ARRAY_SIZE(container_types))
853 tindex = ARRAY_SIZE(container_types) - 1;
854 return container_types[tindex];
855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857/* Function: setinqstr
858 *
859 * Arguments: [1] pointer to void [1] int
860 *
861 * Purpose: Sets SCSI inquiry data strings for vendor, product
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300862 * and revision level. Allows strings to be set in platform dependent
863 * files instead of in OS dependent driver source.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
865
Mark Haverkamp794d0602005-10-24 10:51:53 -0700866static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700871 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Mark Haverkamp794d0602005-10-24 10:51:53 -0700873 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
874 char * cp = dev->supplement_adapter_info.AdapterTypeText;
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800875 int c;
876 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
877 inqstrcpy("SMC", str->vid);
878 else {
879 c = sizeof(str->vid);
880 while (*cp && *cp != ' ' && --c)
881 ++cp;
882 c = *cp;
883 *cp = '\0';
884 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
885 str->vid);
886 *cp = c;
887 while (*cp && *cp != ' ')
888 ++cp;
889 }
Mark Haverkamp794d0602005-10-24 10:51:53 -0700890 while (*cp == ' ')
891 ++cp;
892 /* last six chars reserved for vol type */
893 c = 0;
894 if (strlen(cp) > sizeof(str->pid)) {
895 c = cp[sizeof(str->pid)];
896 cp[sizeof(str->pid)] = '\0';
897 }
898 inqstrcpy (cp, str->pid);
899 if (c)
900 cp[sizeof(str->pid)] = c;
901 } else {
902 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700903
904 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700905 /* last six chars reserved for vol type */
906 inqstrcpy (mp->model, str->pid);
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Tobias Klauser6391a112006-06-08 22:23:48 -0700909 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 char *findit = str->pid;
911
912 for ( ; *findit != ' '; findit++); /* walk till we find a space */
913 /* RAID is superfluous in the context of a RAID device */
914 if (memcmp(findit-4, "RAID", 4) == 0)
915 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700916 if (((findit - str->pid) + strlen(container_types[tindex]))
917 < (sizeof(str->pid) + sizeof(str->prl)))
918 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 inqstrcpy ("V1.0", str->prl);
921}
922
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400923static void get_container_serial_callback(void *context, struct fib * fibptr)
924{
925 struct aac_get_serial_resp * get_serial_reply;
926 struct scsi_cmnd * scsicmd;
927
928 BUG_ON(fibptr == NULL);
929
930 scsicmd = (struct scsi_cmnd *) context;
931 if (!aac_valid_context(scsicmd, fibptr))
932 return;
933
934 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
935 /* Failure is irrelevant, using default value instead */
936 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -0400937 /*Check to see if it's for VPD 0x83 or 0x80 */
938 if (scsicmd->cmnd[2] == 0x83) {
939 /* vpd page 0x83 - Device Identification Page */
940 int i;
941 TVPD_Page83 VPDPage83Data;
942
943 memset(((u8 *)&VPDPage83Data), 0,
944 sizeof(VPDPage83Data));
945
946 /* DIRECT_ACCESS_DEVIC */
947 VPDPage83Data.DeviceType = 0;
948 /* DEVICE_CONNECTED */
949 VPDPage83Data.DeviceTypeQualifier = 0;
950 /* VPD_DEVICE_IDENTIFIERS */
951 VPDPage83Data.PageCode = 0x83;
952 VPDPage83Data.Reserved = 0;
953 VPDPage83Data.PageLength =
954 sizeof(VPDPage83Data.IdDescriptorType1) +
955 sizeof(VPDPage83Data.IdDescriptorType2);
956
957 /* T10 Vendor Identifier Field Format */
958 /* VpdCodeSetAscii */
959 VPDPage83Data.IdDescriptorType1.CodeSet = 2;
960 /* VpdIdentifierTypeVendorId */
961 VPDPage83Data.IdDescriptorType1.IdentifierType = 1;
962 VPDPage83Data.IdDescriptorType1.IdentifierLength =
963 sizeof(VPDPage83Data.IdDescriptorType1) - 4;
964
965 /* "ADAPTEC " for adaptec */
966 memcpy(VPDPage83Data.IdDescriptorType1.VendId,
967 "ADAPTEC ",
968 sizeof(VPDPage83Data.IdDescriptorType1.VendId));
969 memcpy(VPDPage83Data.IdDescriptorType1.ProductId,
970 "ARRAY ",
971 sizeof(
972 VPDPage83Data.IdDescriptorType1.ProductId));
973
974 /* Convert to ascii based serial number.
975 * The LSB is the the end.
976 */
977 for (i = 0; i < 8; i++) {
978 u8 temp =
979 (u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
980 if (temp > 0x9) {
981 VPDPage83Data.IdDescriptorType1.SerialNumber[i] =
982 'A' + (temp - 0xA);
983 } else {
984 VPDPage83Data.IdDescriptorType1.SerialNumber[i] =
985 '0' + temp;
986 }
987 }
988
989 /* VpdCodeSetBinary */
990 VPDPage83Data.IdDescriptorType2.CodeSet = 1;
991 /* VpdIdentifierTypeEUI64 */
992 VPDPage83Data.IdDescriptorType2.IdentifierType = 2;
993 VPDPage83Data.IdDescriptorType2.IdentifierLength =
994 sizeof(VPDPage83Data.IdDescriptorType2) - 4;
995
996 VPDPage83Data.IdDescriptorType2.EU64Id.VendId[0] = 0xD0;
997 VPDPage83Data.IdDescriptorType2.EU64Id.VendId[1] = 0;
998 VPDPage83Data.IdDescriptorType2.EU64Id.VendId[2] = 0;
999
1000 VPDPage83Data.IdDescriptorType2.EU64Id.Serial =
1001 get_serial_reply->uid;
1002 VPDPage83Data.IdDescriptorType2.EU64Id.Reserved = 0;
1003
1004 /* Move the inquiry data to the response buffer. */
1005 scsi_sg_copy_from_buffer(scsicmd, &VPDPage83Data,
1006 sizeof(VPDPage83Data));
1007 } else {
1008 /* It must be for VPD 0x80 */
1009 char sp[13];
1010 /* EVPD bit set */
1011 sp[0] = INQD_PDT_DA;
1012 sp[1] = scsicmd->cmnd[2];
1013 sp[2] = 0;
1014 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1015 le32_to_cpu(get_serial_reply->uid));
1016 scsi_sg_copy_from_buffer(scsicmd, sp,
1017 sizeof(sp));
1018 }
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001019 }
1020
1021 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1022
1023 aac_fib_complete(fibptr);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001024 scsicmd->scsi_done(scsicmd);
1025}
1026
1027/**
1028 * aac_get_container_serial - get container serial, none blocking.
1029 */
1030static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1031{
1032 int status;
1033 struct aac_get_serial *dinfo;
1034 struct fib * cmd_fibcontext;
1035 struct aac_dev * dev;
1036
1037 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1038
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08001039 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1040 if (!cmd_fibcontext)
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001041 return -ENOMEM;
1042
1043 aac_fib_init(cmd_fibcontext);
1044 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1045
1046 dinfo->command = cpu_to_le32(VM_ContainerConfig);
1047 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1048 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1049
1050 status = aac_fib_send(ContainerCommand,
1051 cmd_fibcontext,
Mahesh Rajashekharafb5d40d2015-08-28 06:38:33 -04001052 sizeof(struct aac_get_serial_resp),
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001053 FsaNormal,
1054 0, 1,
1055 (fib_callback) get_container_serial_callback,
1056 (void *) scsicmd);
1057
1058 /*
1059 * Check that the command queued to the controller
1060 */
1061 if (status == -EINPROGRESS) {
1062 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1063 return 0;
1064 }
1065
1066 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1067 aac_fib_complete(cmd_fibcontext);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04001068 return -1;
1069}
1070
1071/* Function: setinqserial
1072 *
1073 * Arguments: [1] pointer to void [1] int
1074 *
1075 * Purpose: Sets SCSI Unit Serial number.
1076 * This is a fake. We should read a proper
1077 * serial number from the container. <SuSE>But
1078 * without docs it's quite hard to do it :-)
1079 * So this will have to do in the meantime.</SuSE>
1080 */
1081
1082static int setinqserial(struct aac_dev *dev, void *data, int cid)
1083{
1084 /*
1085 * This breaks array migration.
1086 */
1087 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1088 le32_to_cpu(dev->adapter_info.serial[0]), cid);
1089}
1090
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001091static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1092 u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001094 u8 *sense_buf = (u8 *)sense_data;
1095 /* Sense data valid, err code 70h */
1096 sense_buf[0] = 0x70; /* No info field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 sense_buf[1] = 0; /* Segment number, always zero */
1098
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001099 sense_buf[2] = sense_key; /* Sense key */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 sense_buf[12] = sense_code; /* Additional sense code */
1102 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (sense_key == ILLEGAL_REQUEST) {
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001105 sense_buf[7] = 10; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001107 sense_buf[15] = bit_pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* Illegal parameter is in the parameter block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (sense_code == SENCODE_INVALID_CDB_FIELD)
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001110 sense_buf[15] |= 0xc0;/* Std sense key specific field */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* Illegal parameter is in the CDB block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 sense_buf[16] = field_pointer >> 8; /* MSB */
1113 sense_buf[17] = field_pointer; /* LSB */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001114 } else
1115 sense_buf[7] = 6; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001118static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1119{
1120 if (lba & 0xffffffff00000000LL) {
1121 int cid = scmd_id(cmd);
1122 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1123 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1124 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001125 set_sense(&dev->fsa_dev[cid].sense_data,
1126 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1127 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001128 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08001129 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1130 SCSI_SENSE_BUFFERSIZE));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001131 cmd->scsi_done(cmd);
1132 return 1;
1133 }
1134 return 0;
1135}
1136
1137static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1138{
1139 return 0;
1140}
1141
1142static void io_callback(void *context, struct fib * fibptr);
1143
1144static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1145{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301146 struct aac_dev *dev = fib->dev;
1147 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301148 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001149
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301150 aac_fib_init(fib);
1151 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
1152 struct aac_raw_io2 *readcmd2;
1153 readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1154 memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1155 readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1156 readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001157 readcmd2->byteCount = cpu_to_le32(count *
1158 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301159 readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1160 readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301161 ret = aac_build_sgraw2(cmd, readcmd2,
1162 dev->scsi_host_ptr->sg_tablesize);
1163 if (ret < 0)
1164 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301165 command = ContainerRawIo2;
1166 fibsize = sizeof(struct aac_raw_io2) +
1167 ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1168 } else {
1169 struct aac_raw_io *readcmd;
1170 readcmd = (struct aac_raw_io *) fib_data(fib);
1171 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1172 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001173 readcmd->count = cpu_to_le32(count *
1174 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301175 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1176 readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1177 readcmd->bpTotal = 0;
1178 readcmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301179 ret = aac_build_sgraw(cmd, &readcmd->sg);
1180 if (ret < 0)
1181 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301182 command = ContainerRawIo;
1183 fibsize = sizeof(struct aac_raw_io) +
1184 ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1185 }
1186
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001187 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1188 /*
1189 * Now send the Fib to the adapter
1190 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301191 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001192 fib,
1193 fibsize,
1194 FsaNormal,
1195 0, 1,
1196 (fib_callback) io_callback,
1197 (void *) cmd);
1198}
1199
1200static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1201{
1202 u16 fibsize;
1203 struct aac_read64 *readcmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301204 long ret;
1205
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001206 aac_fib_init(fib);
1207 readcmd = (struct aac_read64 *) fib_data(fib);
1208 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1209 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1210 readcmd->sector_count = cpu_to_le16(count);
1211 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1212 readcmd->pad = 0;
1213 readcmd->flags = 0;
1214
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301215 ret = aac_build_sg64(cmd, &readcmd->sg);
1216 if (ret < 0)
1217 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001218 fibsize = sizeof(struct aac_read64) +
1219 ((le32_to_cpu(readcmd->sg.count) - 1) *
1220 sizeof (struct sgentry64));
1221 BUG_ON (fibsize > (fib->dev->max_fib_size -
1222 sizeof(struct aac_fibhdr)));
1223 /*
1224 * Now send the Fib to the adapter
1225 */
1226 return aac_fib_send(ContainerCommand64,
1227 fib,
1228 fibsize,
1229 FsaNormal,
1230 0, 1,
1231 (fib_callback) io_callback,
1232 (void *) cmd);
1233}
1234
1235static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1236{
1237 u16 fibsize;
1238 struct aac_read *readcmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001239 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301240 long ret;
1241
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001242 aac_fib_init(fib);
1243 readcmd = (struct aac_read *) fib_data(fib);
1244 readcmd->command = cpu_to_le32(VM_CtBlockRead);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001245 readcmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001246 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001247 readcmd->count = cpu_to_le32(count *
1248 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001249
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301250 ret = aac_build_sg(cmd, &readcmd->sg);
1251 if (ret < 0)
1252 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001253 fibsize = sizeof(struct aac_read) +
1254 ((le32_to_cpu(readcmd->sg.count) - 1) *
1255 sizeof (struct sgentry));
1256 BUG_ON (fibsize > (fib->dev->max_fib_size -
1257 sizeof(struct aac_fibhdr)));
1258 /*
1259 * Now send the Fib to the adapter
1260 */
1261 return aac_fib_send(ContainerCommand,
1262 fib,
1263 fibsize,
1264 FsaNormal,
1265 0, 1,
1266 (fib_callback) io_callback,
1267 (void *) cmd);
1268}
1269
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001270static 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 -08001271{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301272 struct aac_dev *dev = fib->dev;
1273 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301274 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001275
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301276 aac_fib_init(fib);
1277 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
1278 struct aac_raw_io2 *writecmd2;
1279 writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1280 memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1281 writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1282 writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001283 writecmd2->byteCount = cpu_to_le32(count *
1284 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301285 writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1286 writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1287 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1288 cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1289 cpu_to_le16(RIO2_IO_TYPE_WRITE);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301290 ret = aac_build_sgraw2(cmd, writecmd2,
1291 dev->scsi_host_ptr->sg_tablesize);
1292 if (ret < 0)
1293 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301294 command = ContainerRawIo2;
1295 fibsize = sizeof(struct aac_raw_io2) +
1296 ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1297 } else {
1298 struct aac_raw_io *writecmd;
1299 writecmd = (struct aac_raw_io *) fib_data(fib);
1300 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1301 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001302 writecmd->count = cpu_to_le32(count *
1303 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301304 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1305 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1306 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1307 cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1308 cpu_to_le16(RIO_TYPE_WRITE);
1309 writecmd->bpTotal = 0;
1310 writecmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301311 ret = aac_build_sgraw(cmd, &writecmd->sg);
1312 if (ret < 0)
1313 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301314 command = ContainerRawIo;
1315 fibsize = sizeof(struct aac_raw_io) +
1316 ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1317 }
1318
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001319 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1320 /*
1321 * Now send the Fib to the adapter
1322 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301323 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001324 fib,
1325 fibsize,
1326 FsaNormal,
1327 0, 1,
1328 (fib_callback) io_callback,
1329 (void *) cmd);
1330}
1331
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001332static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001333{
1334 u16 fibsize;
1335 struct aac_write64 *writecmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301336 long ret;
1337
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001338 aac_fib_init(fib);
1339 writecmd = (struct aac_write64 *) fib_data(fib);
1340 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1341 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1342 writecmd->sector_count = cpu_to_le16(count);
1343 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1344 writecmd->pad = 0;
1345 writecmd->flags = 0;
1346
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301347 ret = aac_build_sg64(cmd, &writecmd->sg);
1348 if (ret < 0)
1349 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001350 fibsize = sizeof(struct aac_write64) +
1351 ((le32_to_cpu(writecmd->sg.count) - 1) *
1352 sizeof (struct sgentry64));
1353 BUG_ON (fibsize > (fib->dev->max_fib_size -
1354 sizeof(struct aac_fibhdr)));
1355 /*
1356 * Now send the Fib to the adapter
1357 */
1358 return aac_fib_send(ContainerCommand64,
1359 fib,
1360 fibsize,
1361 FsaNormal,
1362 0, 1,
1363 (fib_callback) io_callback,
1364 (void *) cmd);
1365}
1366
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001367static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001368{
1369 u16 fibsize;
1370 struct aac_write *writecmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001371 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301372 long ret;
1373
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001374 aac_fib_init(fib);
1375 writecmd = (struct aac_write *) fib_data(fib);
1376 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001377 writecmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001378 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001379 writecmd->count = cpu_to_le32(count *
1380 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001381 writecmd->sg.count = cpu_to_le32(1);
1382 /* ->stable is not used - it did mean which type of write */
1383
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301384 ret = aac_build_sg(cmd, &writecmd->sg);
1385 if (ret < 0)
1386 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001387 fibsize = sizeof(struct aac_write) +
1388 ((le32_to_cpu(writecmd->sg.count) - 1) *
1389 sizeof (struct sgentry));
1390 BUG_ON (fibsize > (fib->dev->max_fib_size -
1391 sizeof(struct aac_fibhdr)));
1392 /*
1393 * Now send the Fib to the adapter
1394 */
1395 return aac_fib_send(ContainerCommand,
1396 fib,
1397 fibsize,
1398 FsaNormal,
1399 0, 1,
1400 (fib_callback) io_callback,
1401 (void *) cmd);
1402}
1403
1404static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1405{
1406 struct aac_srb * srbcmd;
1407 u32 flag;
1408 u32 timeout;
1409
1410 aac_fib_init(fib);
1411 switch(cmd->sc_data_direction){
1412 case DMA_TO_DEVICE:
1413 flag = SRB_DataOut;
1414 break;
1415 case DMA_BIDIRECTIONAL:
1416 flag = SRB_DataIn | SRB_DataOut;
1417 break;
1418 case DMA_FROM_DEVICE:
1419 flag = SRB_DataIn;
1420 break;
1421 case DMA_NONE:
1422 default: /* shuts up some versions of gcc */
1423 flag = SRB_NoDataXfer;
1424 break;
1425 }
1426
1427 srbcmd = (struct aac_srb*) fib_data(fib);
1428 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1429 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1430 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1431 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1432 srbcmd->flags = cpu_to_le32(flag);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001433 timeout = cmd->request->timeout/HZ;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001434 if (timeout == 0)
1435 timeout = 1;
1436 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1437 srbcmd->retry_limit = 0; /* Obsolete parameter */
1438 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1439 return srbcmd;
1440}
1441
1442static void aac_srb_callback(void *context, struct fib * fibptr);
1443
1444static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1445{
1446 u16 fibsize;
1447 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301448 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001449
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301450 ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1451 if (ret < 0)
1452 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001453 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001454
1455 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1456 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1457 /*
1458 * Build Scatter/Gather list
1459 */
1460 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1461 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1462 sizeof (struct sgentry64));
1463 BUG_ON (fibsize > (fib->dev->max_fib_size -
1464 sizeof(struct aac_fibhdr)));
1465
1466 /*
1467 * Now send the Fib to the adapter
1468 */
1469 return aac_fib_send(ScsiPortCommand64, fib,
1470 fibsize, FsaNormal, 0, 1,
1471 (fib_callback) aac_srb_callback,
1472 (void *) cmd);
1473}
1474
1475static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1476{
1477 u16 fibsize;
1478 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301479 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001480
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301481 ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1482 if (ret < 0)
1483 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001484 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001485
1486 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1487 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1488 /*
1489 * Build Scatter/Gather list
1490 */
1491 fibsize = sizeof (struct aac_srb) +
1492 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1493 sizeof (struct sgentry));
1494 BUG_ON (fibsize > (fib->dev->max_fib_size -
1495 sizeof(struct aac_fibhdr)));
1496
1497 /*
1498 * Now send the Fib to the adapter
1499 */
1500 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1501 (fib_callback) aac_srb_callback, (void *) cmd);
1502}
1503
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001504static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1505{
Leubner, Achimd8e965072009-04-01 07:16:08 -07001506 if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1507 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001508 return FAILED;
1509 return aac_scsi_32(fib, cmd);
1510}
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512int aac_get_adapter_info(struct aac_dev* dev)
1513{
1514 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int rcode;
1516 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001517 struct aac_adapter_info *info;
1518 struct aac_bus_info *command;
1519 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001520
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001521 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return -ENOMEM;
1523
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001524 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001525 info = (struct aac_adapter_info *) fib_data(fibptr);
1526 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001528 rcode = aac_fib_send(RequestAdapterInfo,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001529 fibptr,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001530 sizeof(*info),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001531 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001532 -1, 1, /* First `interrupt' command uses special wait */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001533 NULL,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001534 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001536 if (rcode < 0) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301537 /* FIB should be freed only after
1538 * getting the response from the F/W */
1539 if (rcode != -ERESTARTSYS) {
1540 aac_fib_complete(fibptr);
1541 aac_fib_free(fibptr);
1542 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001543 return rcode;
1544 }
1545 memcpy(&dev->adapter_info, info, sizeof(*info));
1546
1547 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001548 struct aac_supplement_adapter_info * sinfo;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001549
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001550 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001551
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001552 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001553
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001554 memset(sinfo,0,sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001555
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001556 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001557 fibptr,
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001558 sizeof(*sinfo),
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001559 FsaNormal,
1560 1, 1,
1561 NULL,
1562 NULL);
1563
1564 if (rcode >= 0)
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001565 memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301566 if (rcode == -ERESTARTSYS) {
1567 fibptr = aac_fib_alloc(dev);
1568 if (!fibptr)
1569 return -ENOMEM;
1570 }
1571
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Mark Haverkamp84971732005-06-20 11:55:24 -07001574
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001575 /*
1576 * GetBusInfo
Mark Haverkamp84971732005-06-20 11:55:24 -07001577 */
1578
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001579 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001580
1581 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1582
1583 memset(bus_info, 0, sizeof(*bus_info));
1584
1585 command = (struct aac_bus_info *)bus_info;
1586
1587 command->Command = cpu_to_le32(VM_Ioctl);
1588 command->ObjType = cpu_to_le32(FT_DRIVE);
1589 command->MethodId = cpu_to_le32(1);
1590 command->CtlCmd = cpu_to_le32(GetBusInfo);
1591
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001592 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001593 fibptr,
1594 sizeof (*bus_info),
1595 FsaNormal,
1596 1, 1,
1597 NULL, NULL);
1598
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001599 /* reasoned default */
1600 dev->maximum_num_physicals = 16;
Mark Haverkamp84971732005-06-20 11:55:24 -07001601 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1602 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1603 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1604 }
1605
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001606 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001607 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001608 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1609 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001610 dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 dev->id,
1612 tmp>>24,
1613 (tmp>>16)&0xff,
1614 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001615 le32_to_cpu(dev->adapter_info.kernelbuild),
1616 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1617 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001618 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1619 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 dev->name, dev->id,
1621 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1622 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001623 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1624 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 dev->name, dev->id,
1626 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1627 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001628 buffer[0] = '\0';
Tony Jonesee959b02008-02-22 00:13:36 +01001629 if (aac_get_serial_number(
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001630 shost_to_class(dev->scsi_host_ptr), buffer))
1631 printk(KERN_INFO "%s%d: serial %s",
1632 dev->name, dev->id, buffer);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001633 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1634 printk(KERN_INFO "%s%d: TSID %.*s\n",
1635 dev->name, dev->id,
1636 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1637 dev->supplement_adapter_info.VpdInfo.Tsid);
1638 }
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001639 if (!aac_check_reset || ((aac_check_reset == 1) &&
Salyzyn, Marka3940da2008-01-08 12:48:25 -08001640 (dev->supplement_adapter_info.SupportedOptions2 &
1641 AAC_OPTION_IGNORE_RESET))) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001642 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1643 dev->name, dev->id);
1644 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001647 dev->cache_protected = 0;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001648 dev->jbod = ((dev->supplement_adapter_info.FeatureBits &
1649 AAC_FEATURE_JBOD) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 dev->nondasd_support = 0;
1651 dev->raid_scsi_mode = 0;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001652 if(dev->adapter_info.options & AAC_OPT_NONDASD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 dev->nondasd_support = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 /*
1656 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1657 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1658 * force nondasd support on. If we decide to allow the non-dasd flag
1659 * additional changes changes will have to be made to support
1660 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1661 * changed to support the new dev->raid_scsi_mode flag instead of
1662 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1663 * function aac_detect will have to be modified where it sets up the
1664 * max number of channels based on the aac->nondasd_support flag only.
1665 */
1666 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1667 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1668 dev->nondasd_support = 1;
1669 dev->raid_scsi_mode = 1;
1670 }
1671 if (dev->raid_scsi_mode != 0)
1672 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1673 dev->name, dev->id);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001674
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001675 if (nondasd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 dev->nondasd_support = (nondasd!=0);
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001677 if (dev->nondasd_support && !dev->in_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Yang Hongyange9304382009-04-13 14:40:14 -07001680 if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
Leubner, Achimd8e965072009-04-01 07:16:08 -07001681 dev->needs_dac = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 dev->dac_support = 0;
Leubner, Achimd8e965072009-04-01 07:16:08 -07001683 if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
1684 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001685 if (!dev->in_reset)
1686 printk(KERN_INFO "%s%d: 64bit support enabled.\n",
1687 dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 dev->dac_support = 1;
1689 }
1690
1691 if(dacmode != -1) {
1692 dev->dac_support = (dacmode!=0);
1693 }
Leubner, Achimd8e965072009-04-01 07:16:08 -07001694
1695 /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
1696 if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
1697 & AAC_QUIRK_SCSI_32)) {
1698 dev->nondasd_support = 0;
1699 dev->jbod = 0;
1700 expose_physicals = 0;
1701 }
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if(dev->dac_support != 0) {
Yang Hongyang6a355282009-04-06 19:01:13 -07001704 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64)) &&
1705 !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001706 if (!dev->in_reset)
1707 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1708 dev->name, dev->id);
Yang Hongyang284901a2009-04-06 19:01:15 -07001709 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32)) &&
1710 !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1712 dev->name, dev->id);
1713 dev->dac_support = 0;
1714 } else {
1715 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1716 dev->name, dev->id);
1717 rcode = -ENOMEM;
1718 }
1719 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001720 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001721 * Deal with configuring for the individualized limits of each packet
1722 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001723 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001724 dev->a_ops.adapter_scsi = (dev->dac_support)
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001725 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1726 ? aac_scsi_32_64
1727 : aac_scsi_64)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001728 : aac_scsi_32;
1729 if (dev->raw_io_interface) {
1730 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1731 ? aac_bounds_64
1732 : aac_bounds_32;
1733 dev->a_ops.adapter_read = aac_read_raw_io;
1734 dev->a_ops.adapter_write = aac_write_raw_io;
1735 } else {
1736 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001737 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001738 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001739 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1740 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001741 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001742 dev->a_ops.adapter_read = aac_read_block64;
1743 dev->a_ops.adapter_write = aac_write_block64;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001744 /*
1745 * 38 scatter gather elements
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001746 */
1747 dev->scsi_host_ptr->sg_tablesize =
1748 (dev->max_fib_size -
1749 sizeof(struct aac_fibhdr) -
1750 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001751 sizeof(struct sgentry64)) /
1752 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001753 } else {
1754 dev->a_ops.adapter_read = aac_read_block;
1755 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001756 }
1757 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07001758 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001759 /*
1760 * Worst case size that could cause sg overflow when
1761 * we break up SG elements that are larger than 64KB.
1762 * Would be nice if we could tell the SCSI layer what
1763 * the maximum SG element size can be. Worst case is
1764 * (sg_tablesize-1) 4KB elements with one 64KB
1765 * element.
1766 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1767 */
1768 dev->scsi_host_ptr->max_sectors =
1769 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1770 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001771 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301772 /* FIB should be freed only after getting the response from the F/W */
1773 if (rcode != -ERESTARTSYS) {
1774 aac_fib_complete(fibptr);
1775 aac_fib_free(fibptr);
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 return rcode;
1779}
1780
1781
Mark Haverkampe53cb352005-08-03 15:39:09 -07001782static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 struct aac_dev *dev;
1785 struct aac_read_reply *readreply;
1786 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 u32 cid;
1788
1789 scsicmd = (struct scsi_cmnd *) context;
1790
Mark Haverkamp03d44332007-03-15 10:27:45 -07001791 if (!aac_valid_context(scsicmd, fibptr))
1792 return;
1793
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001794 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001795 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001797 if (nblank(dprintk(x))) {
1798 u64 lba;
1799 switch (scsicmd->cmnd[0]) {
1800 case WRITE_6:
1801 case READ_6:
1802 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1803 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1804 break;
1805 case WRITE_16:
1806 case READ_16:
1807 lba = ((u64)scsicmd->cmnd[2] << 56) |
1808 ((u64)scsicmd->cmnd[3] << 48) |
1809 ((u64)scsicmd->cmnd[4] << 40) |
1810 ((u64)scsicmd->cmnd[5] << 32) |
1811 ((u64)scsicmd->cmnd[6] << 24) |
1812 (scsicmd->cmnd[7] << 16) |
1813 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1814 break;
1815 case WRITE_12:
1816 case READ_12:
1817 lba = ((u64)scsicmd->cmnd[2] << 24) |
1818 (scsicmd->cmnd[3] << 16) |
1819 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1820 break;
1821 default:
1822 lba = ((u64)scsicmd->cmnd[2] << 24) |
1823 (scsicmd->cmnd[3] << 16) |
1824 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1825 break;
1826 }
1827 printk(KERN_DEBUG
1828 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1829 smp_processor_id(), (unsigned long long)lba, jiffies);
1830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001832 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001833
1834 scsi_dma_unmap(scsicmd);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 readreply = (struct aac_read_reply *)fib_data(fibptr);
Mark Salyzyn655d7222008-04-30 16:03:42 -04001837 switch (le32_to_cpu(readreply->status)) {
1838 case ST_OK:
1839 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1840 SAM_STAT_GOOD;
1841 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
1842 break;
1843 case ST_NOT_READY:
1844 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1845 SAM_STAT_CHECK_CONDITION;
1846 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
1847 SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
1848 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1849 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1850 SCSI_SENSE_BUFFERSIZE));
1851 break;
1852 default:
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001853#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001854 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001855 le32_to_cpu(readreply->status));
1856#endif
Mark Salyzyn655d7222008-04-30 16:03:42 -04001857 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1858 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001859 set_sense(&dev->fsa_dev[cid].sense_data,
1860 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1861 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08001863 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1864 SCSI_SENSE_BUFFERSIZE));
Mark Salyzyn655d7222008-04-30 16:03:42 -04001865 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001867 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001869 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001872static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001874 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 u32 count;
1876 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 struct aac_dev *dev;
1878 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07001879 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1882 /*
1883 * Get block address and transfer length
1884 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001885 switch (scsicmd->cmnd[0]) {
1886 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001887 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001889 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001890 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 count = scsicmd->cmnd[4];
1892
1893 if (count == 0)
1894 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001895 break;
1896 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001897 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001898
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001899 lba = ((u64)scsicmd->cmnd[2] << 56) |
1900 ((u64)scsicmd->cmnd[3] << 48) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001901 ((u64)scsicmd->cmnd[4] << 40) |
1902 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001903 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001904 (scsicmd->cmnd[7] << 16) |
1905 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001906 count = (scsicmd->cmnd[10] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001907 (scsicmd->cmnd[11] << 16) |
1908 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1909 break;
1910 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001911 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001912
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001913 lba = ((u64)scsicmd->cmnd[2] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001914 (scsicmd->cmnd[3] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001915 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1916 count = (scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001917 (scsicmd->cmnd[7] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001918 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001919 break;
1920 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001921 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001923 lba = ((u64)scsicmd->cmnd[2] << 24) |
1924 (scsicmd->cmnd[3] << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001925 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07001929
1930 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1931 cid = scmd_id(scsicmd);
1932 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1933 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1934 SAM_STAT_CHECK_CONDITION;
1935 set_sense(&dev->fsa_dev[cid].sense_data,
1936 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1937 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1938 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1939 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1940 SCSI_SENSE_BUFFERSIZE));
1941 scsicmd->scsi_done(scsicmd);
1942 return 1;
1943 }
1944
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001945 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001946 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001947 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /*
1950 * Alocate and initialize a Fib
1951 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08001952 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1953 if (!cmd_fibcontext) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301954 printk(KERN_WARNING "aac_read: fib allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return -1;
1956 }
1957
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001958 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /*
1961 * Check that the command queued to the controller
1962 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001963 if (status == -EINPROGRESS) {
1964 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001966 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001967
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001968 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /*
1970 * For some reason, the Fib didn't queue, return QUEUE_FULL
1971 */
1972 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001973 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001974 aac_fib_complete(cmd_fibcontext);
1975 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return 0;
1977}
1978
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001979static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001981 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001983 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct aac_dev *dev;
1986 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07001987 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1990 /*
1991 * Get block address and transfer length
1992 */
1993 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1994 {
1995 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1996 count = scsicmd->cmnd[4];
1997 if (count == 0)
1998 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001999 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002000 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002001 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002002
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002003 lba = ((u64)scsicmd->cmnd[2] << 56) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002004 ((u64)scsicmd->cmnd[3] << 48) |
2005 ((u64)scsicmd->cmnd[4] << 40) |
2006 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002007 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002008 (scsicmd->cmnd[7] << 16) |
2009 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2010 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2011 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002012 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002013 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002014 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002015
2016 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2017 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2018 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2019 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002020 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002022 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002023 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002025 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07002027
2028 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2029 cid = scmd_id(scsicmd);
2030 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2031 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2032 SAM_STAT_CHECK_CONDITION;
2033 set_sense(&dev->fsa_dev[cid].sense_data,
2034 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2035 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2036 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2037 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2038 SCSI_SENSE_BUFFERSIZE));
2039 scsicmd->scsi_done(scsicmd);
2040 return 1;
2041 }
2042
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002043 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08002045 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /*
2048 * Allocate and initialize a Fib then setup a BlockWrite command
2049 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002050 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2051 if (!cmd_fibcontext) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05302052 /* FIB temporarily unavailable,not catastrophic failure */
2053
2054 /* scsicmd->result = DID_ERROR << 16;
2055 * scsicmd->scsi_done(scsicmd);
2056 * return 0;
2057 */
2058 printk(KERN_WARNING "aac_write: fib allocation failed\n");
2059 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002062 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /*
2065 * Check that the command queued to the controller
2066 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002067 if (status == -EINPROGRESS) {
2068 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return 0;
2070 }
2071
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002072 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
2074 * For some reason, the Fib didn't queue, return QUEUE_FULL
2075 */
2076 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002077 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002079 aac_fib_complete(cmd_fibcontext);
2080 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
2083
2084static void synchronize_callback(void *context, struct fib *fibptr)
2085{
2086 struct aac_synchronize_reply *synchronizereply;
2087 struct scsi_cmnd *cmd;
2088
2089 cmd = context;
2090
Mark Haverkamp03d44332007-03-15 10:27:45 -07002091 if (!aac_valid_context(cmd, fibptr))
2092 return;
2093
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002094 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 smp_processor_id(), jiffies));
2096 BUG_ON(fibptr == NULL);
2097
2098
2099 synchronizereply = fib_data(fibptr);
2100 if (le32_to_cpu(synchronizereply->status) == CT_OK)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002101 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2103 else {
2104 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002105 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08002106 u32 cid = sdev_id(sdev);
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002107 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 "synchronize_callback: synchronize failed, status = %d\n",
2109 le32_to_cpu(synchronizereply->status));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002110 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002112 set_sense(&dev->fsa_dev[cid].sense_data,
2113 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2114 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09002116 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2117 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002120 aac_fib_complete(fibptr);
2121 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002122 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002125static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 int status;
2128 struct fib *cmd_fibcontext;
2129 struct aac_synchronize *synchronizecmd;
2130 struct scsi_cmnd *cmd;
2131 struct scsi_device *sdev = scsicmd->device;
2132 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002133 struct aac_dev *aac;
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002134 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2135 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2136 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 unsigned long flags;
2138
2139 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002140 * Wait for all outstanding queued commands to complete to this
2141 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 */
2143 spin_lock_irqsave(&sdev->list_lock, flags);
2144 list_for_each_entry(cmd, &sdev->cmd_list, list)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002145 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2146 u64 cmnd_lba;
2147 u32 cmnd_count;
2148
2149 if (cmd->cmnd[0] == WRITE_6) {
2150 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2151 (cmd->cmnd[2] << 8) |
2152 cmd->cmnd[3];
2153 cmnd_count = cmd->cmnd[4];
2154 if (cmnd_count == 0)
2155 cmnd_count = 256;
2156 } else if (cmd->cmnd[0] == WRITE_16) {
2157 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2158 ((u64)cmd->cmnd[3] << 48) |
2159 ((u64)cmd->cmnd[4] << 40) |
2160 ((u64)cmd->cmnd[5] << 32) |
2161 ((u64)cmd->cmnd[6] << 24) |
2162 (cmd->cmnd[7] << 16) |
2163 (cmd->cmnd[8] << 8) |
2164 cmd->cmnd[9];
2165 cmnd_count = (cmd->cmnd[10] << 24) |
2166 (cmd->cmnd[11] << 16) |
2167 (cmd->cmnd[12] << 8) |
2168 cmd->cmnd[13];
2169 } else if (cmd->cmnd[0] == WRITE_12) {
2170 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2171 (cmd->cmnd[3] << 16) |
2172 (cmd->cmnd[4] << 8) |
2173 cmd->cmnd[5];
2174 cmnd_count = (cmd->cmnd[6] << 24) |
2175 (cmd->cmnd[7] << 16) |
2176 (cmd->cmnd[8] << 8) |
2177 cmd->cmnd[9];
2178 } else if (cmd->cmnd[0] == WRITE_10) {
2179 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2180 (cmd->cmnd[3] << 16) |
2181 (cmd->cmnd[4] << 8) |
2182 cmd->cmnd[5];
2183 cmnd_count = (cmd->cmnd[7] << 8) |
2184 cmd->cmnd[8];
2185 } else
2186 continue;
2187 if (((cmnd_lba + cmnd_count) < lba) ||
2188 (count && ((lba + count) < cmnd_lba)))
2189 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 ++active;
2191 break;
2192 }
2193
2194 spin_unlock_irqrestore(&sdev->list_lock, flags);
2195
2196 /*
2197 * Yield the processor (requeue for later)
2198 */
2199 if (active)
2200 return SCSI_MLQUEUE_DEVICE_BUSY;
2201
Salyzyn, Markf8583172007-10-30 15:50:49 -04002202 aac = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002203 if (aac->in_reset)
2204 return SCSI_MLQUEUE_HOST_BUSY;
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002207 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002209 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return SCSI_MLQUEUE_HOST_BUSY;
2211
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002212 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 synchronizecmd = fib_data(cmd_fibcontext);
2215 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2216 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002217 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002218 synchronizecmd->count =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2220
2221 /*
2222 * Now send the Fib to the adapter
2223 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002224 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 cmd_fibcontext,
2226 sizeof(struct aac_synchronize),
2227 FsaNormal,
2228 0, 1,
2229 (fib_callback)synchronize_callback,
2230 (void *)scsicmd);
2231
2232 /*
2233 * Check that the command queued to the controller
2234 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002235 if (status == -EINPROGRESS) {
2236 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002240 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002241 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2242 aac_fib_complete(cmd_fibcontext);
2243 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return SCSI_MLQUEUE_HOST_BUSY;
2245}
2246
Mark Salyzyn655d7222008-04-30 16:03:42 -04002247static void aac_start_stop_callback(void *context, struct fib *fibptr)
2248{
2249 struct scsi_cmnd *scsicmd = context;
2250
2251 if (!aac_valid_context(scsicmd, fibptr))
2252 return;
2253
2254 BUG_ON(fibptr == NULL);
2255
2256 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2257
2258 aac_fib_complete(fibptr);
2259 aac_fib_free(fibptr);
2260 scsicmd->scsi_done(scsicmd);
2261}
2262
2263static int aac_start_stop(struct scsi_cmnd *scsicmd)
2264{
2265 int status;
2266 struct fib *cmd_fibcontext;
2267 struct aac_power_management *pmcmd;
2268 struct scsi_device *sdev = scsicmd->device;
2269 struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2270
2271 if (!(aac->supplement_adapter_info.SupportedOptions2 &
2272 AAC_OPTION_POWER_MANAGEMENT)) {
2273 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2274 SAM_STAT_GOOD;
2275 scsicmd->scsi_done(scsicmd);
2276 return 0;
2277 }
2278
2279 if (aac->in_reset)
2280 return SCSI_MLQUEUE_HOST_BUSY;
2281
2282 /*
2283 * Allocate and initialize a Fib
2284 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08002285 cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
Mark Salyzyn655d7222008-04-30 16:03:42 -04002286 if (!cmd_fibcontext)
2287 return SCSI_MLQUEUE_HOST_BUSY;
2288
2289 aac_fib_init(cmd_fibcontext);
2290
2291 pmcmd = fib_data(cmd_fibcontext);
2292 pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2293 pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2294 /* Eject bit ignored, not relevant */
2295 pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2296 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2297 pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2298 pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2299 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2300
2301 /*
2302 * Now send the Fib to the adapter
2303 */
2304 status = aac_fib_send(ContainerCommand,
2305 cmd_fibcontext,
2306 sizeof(struct aac_power_management),
2307 FsaNormal,
2308 0, 1,
2309 (fib_callback)aac_start_stop_callback,
2310 (void *)scsicmd);
2311
2312 /*
2313 * Check that the command queued to the controller
2314 */
2315 if (status == -EINPROGRESS) {
2316 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2317 return 0;
2318 }
2319
2320 aac_fib_complete(cmd_fibcontext);
2321 aac_fib_free(cmd_fibcontext);
2322 return SCSI_MLQUEUE_HOST_BUSY;
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325/**
2326 * aac_scsi_cmd() - Process SCSI command
2327 * @scsicmd: SCSI command block
2328 *
2329 * Emulate a SCSI command and queue the required request for the
2330 * aacraid firmware.
2331 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2334{
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002335 u32 cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct Scsi_Host *host = scsicmd->device->host;
2337 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2338 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002339
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002340 if (fsa_dev_ptr == NULL)
2341 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /*
2343 * If the bus, id or lun is out of range, return fail
2344 * Test does not apply to ID 16, the pseudo id for the controller
2345 * itself.
2346 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002347 cid = scmd_id(scsicmd);
2348 if (cid != host->this_id) {
2349 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2350 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08002351 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 scsicmd->result = DID_NO_CONNECT << 16;
2353 scsicmd->scsi_done(scsicmd);
2354 return 0;
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
2357 /*
2358 * If the target container doesn't exist, it may have
2359 * been newly created
2360 */
Mark Salyzyn655d7222008-04-30 16:03:42 -04002361 if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2362 (fsa_dev_ptr[cid].sense_data.sense_key ==
2363 NOT_READY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 switch (scsicmd->cmnd[0]) {
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002365 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002366 if (!(dev->raw_io_interface) ||
2367 !(dev->raw_io_64) ||
2368 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 case INQUIRY:
2371 case READ_CAPACITY:
2372 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002373 if (dev->in_reset)
2374 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07002375 return _aac_probe_container(scsicmd,
2376 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 default:
2378 break;
2379 }
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 } else { /* check for physical non-dasd devices */
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08002382 if (dev->nondasd_support || expose_physicals ||
2383 dev->jbod) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002384 if (dev->in_reset)
2385 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return aac_send_srb_fib(scsicmd);
2387 } else {
2388 scsicmd->result = DID_NO_CONNECT << 16;
2389 scsicmd->scsi_done(scsicmd);
2390 return 0;
2391 }
2392 }
2393 }
2394 /*
2395 * else Command for the controller itself
2396 */
2397 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002398 (scsicmd->cmnd[0] != TEST_UNIT_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 {
2400 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2401 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002402 set_sense(&dev->fsa_dev[cid].sense_data,
2403 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2404 ASENCODE_INVALID_COMMAND, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002406 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2407 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 scsicmd->scsi_done(scsicmd);
2409 return 0;
2410 }
2411
2412
2413 /* Handle commands here that don't really require going out to the adapter */
2414 switch (scsicmd->cmnd[0]) {
2415 case INQUIRY:
2416 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002417 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002419 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002420 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Leubner, Achimd8e965072009-04-01 07:16:08 -07002422 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002423 char *arr = (char *)&inq_data;
2424
2425 /* EVPD bit set */
2426 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2427 INQD_PDT_PROC : INQD_PDT_DA;
2428 if (scsicmd->cmnd[2] == 0) {
2429 /* supported vital product data pages */
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002430 arr[3] = 3;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002431 arr[4] = 0x0;
2432 arr[5] = 0x80;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002433 arr[6] = 0x83;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002434 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002435 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2436 sizeof(inq_data));
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002437 scsicmd->result = DID_OK << 16 |
2438 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2439 } else if (scsicmd->cmnd[2] == 0x80) {
2440 /* unit serial number page */
2441 arr[3] = setinqserial(dev, &arr[4],
2442 scmd_id(scsicmd));
2443 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002444 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2445 sizeof(inq_data));
Leubner, Achimd8e965072009-04-01 07:16:08 -07002446 if (aac_wwn != 2)
2447 return aac_get_container_serial(
2448 scsicmd);
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002449 scsicmd->result = DID_OK << 16 |
2450 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2451 } else if (scsicmd->cmnd[2] == 0x83) {
2452 /* vpd page 0x83 - Device Identification Page */
2453 char *sno = (char *)&inq_data;
2454 sno[3] = setinqserial(dev, &sno[4],
2455 scmd_id(scsicmd));
2456 if (aac_wwn != 2)
2457 return aac_get_container_serial(
2458 scsicmd);
Leubner, Achimd8e965072009-04-01 07:16:08 -07002459 scsicmd->result = DID_OK << 16 |
2460 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002461 } else {
2462 /* vpd page not implemented */
2463 scsicmd->result = DID_OK << 16 |
2464 COMMAND_COMPLETE << 8 |
2465 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002466 set_sense(&dev->fsa_dev[cid].sense_data,
2467 ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2468 ASENCODE_NO_SENSE, 7, 2);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002469 memcpy(scsicmd->sense_buffer,
2470 &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002471 min_t(size_t,
2472 sizeof(dev->fsa_dev[cid].sense_data),
2473 SCSI_SENSE_BUFFERSIZE));
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002474 }
2475 scsicmd->scsi_done(scsicmd);
2476 return 0;
2477 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002478 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002479 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 */
2480 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002482 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /*
2484 * Set the Vendor, Product, and Revision Level
2485 * see: <vendor>.c i.e. aac.c
2486 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002487 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07002488 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002489 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002490 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2491 sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2493 scsicmd->scsi_done(scsicmd);
2494 return 0;
2495 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002496 if (dev->in_reset)
2497 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07002498 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002499 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002500 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002501 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002503 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002504 if (!(dev->raw_io_interface) ||
2505 !(dev->raw_io_64) ||
2506 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2507 break;
2508 {
2509 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002510 char cp[13];
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002511 unsigned int alloc_len;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002512
2513 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2514 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002515 cp[0] = (capacity >> 56) & 0xff;
2516 cp[1] = (capacity >> 48) & 0xff;
2517 cp[2] = (capacity >> 40) & 0xff;
2518 cp[3] = (capacity >> 32) & 0xff;
2519 cp[4] = (capacity >> 24) & 0xff;
2520 cp[5] = (capacity >> 16) & 0xff;
2521 cp[6] = (capacity >> 8) & 0xff;
2522 cp[7] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002523 cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2524 cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2525 cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2526 cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002527 cp[12] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002528
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002529 alloc_len = ((scsicmd->cmnd[10] << 24)
2530 + (scsicmd->cmnd[11] << 16)
2531 + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2532
2533 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002534 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002535 if (alloc_len < scsi_bufflen(scsicmd))
2536 scsi_set_resid(scsicmd,
2537 scsi_bufflen(scsicmd) - alloc_len);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002538
2539 /* Do not cache partition table for arrays */
2540 scsicmd->device->removable = 1;
2541
2542 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2543 scsicmd->scsi_done(scsicmd);
2544
2545 return 0;
2546 }
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 case READ_CAPACITY:
2549 {
2550 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002551 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002554 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 capacity = fsa_dev_ptr[cid].size - 1;
2556 else
2557 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 cp[0] = (capacity >> 24) & 0xff;
2560 cp[1] = (capacity >> 16) & 0xff;
2561 cp[2] = (capacity >> 8) & 0xff;
2562 cp[3] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002563 cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2564 cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2565 cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2566 cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002567 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002568 /* Do not cache partition table for arrays */
2569 scsicmd->device->removable = 1;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002570 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2571 SAM_STAT_GOOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 scsicmd->scsi_done(scsicmd);
2573
2574 return 0;
2575 }
2576
2577 case MODE_SENSE:
2578 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002579 int mode_buf_length = 4;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002580 u32 capacity;
2581 aac_modep_data mpd;
2582
2583 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2584 capacity = fsa_dev_ptr[cid].size - 1;
2585 else
2586 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002589 memset((char *)&mpd, 0, sizeof(aac_modep_data));
2590
2591 /* Mode data length */
2592 mpd.hd.data_length = sizeof(mpd.hd) - 1;
2593 /* Medium type - default */
2594 mpd.hd.med_type = 0;
2595 /* Device-specific param,
2596 bit 8: 0/1 = write enabled/protected
2597 bit 4: 0/1 = FUA enabled */
2598 mpd.hd.dev_par = 0;
2599
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002600 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002601 mpd.hd.dev_par = 0x10;
2602 if (scsicmd->cmnd[1] & 0x8)
2603 mpd.hd.bd_length = 0; /* Block descriptor length */
2604 else {
2605 mpd.hd.bd_length = sizeof(mpd.bd);
2606 mpd.hd.data_length += mpd.hd.bd_length;
2607 mpd.bd.block_length[0] =
2608 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2609 mpd.bd.block_length[1] =
2610 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2611 mpd.bd.block_length[2] =
2612 fsa_dev_ptr[cid].block_size & 0xff;
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002613
2614 mpd.mpc_buf[0] = scsicmd->cmnd[2];
2615 if (scsicmd->cmnd[2] == 0x1C) {
2616 /* page length */
2617 mpd.mpc_buf[1] = 0xa;
2618 /* Mode data length */
2619 mpd.hd.data_length = 23;
2620 } else {
2621 /* Mode data length */
2622 mpd.hd.data_length = 15;
2623 }
2624
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002625 if (capacity > 0xffffff) {
2626 mpd.bd.block_count[0] = 0xff;
2627 mpd.bd.block_count[1] = 0xff;
2628 mpd.bd.block_count[2] = 0xff;
2629 } else {
2630 mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
2631 mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
2632 mpd.bd.block_count[2] = capacity & 0xff;
2633 }
2634 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002635 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2636 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002637 mpd.hd.data_length += 3;
2638 mpd.mpc_buf[0] = 8;
2639 mpd.mpc_buf[1] = 1;
2640 mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002641 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002642 mode_buf_length = sizeof(mpd);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002643 }
Mahesh Rajashekhara5d910642015-03-26 10:41:26 -04002644
2645 if (mode_buf_length > scsicmd->cmnd[4])
2646 mode_buf_length = scsicmd->cmnd[4];
2647 else
2648 mode_buf_length = sizeof(mpd);
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002649 scsi_sg_copy_from_buffer(scsicmd,
2650 (char *)&mpd,
2651 mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2653 scsicmd->scsi_done(scsicmd);
2654
2655 return 0;
2656 }
2657 case MODE_SENSE_10:
2658 {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002659 u32 capacity;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002660 int mode_buf_length = 8;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002661 aac_modep10_data mpd10;
2662
2663 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2664 capacity = fsa_dev_ptr[cid].size - 1;
2665 else
2666 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002669 memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
2670 /* Mode data length (MSB) */
2671 mpd10.hd.data_length[0] = 0;
2672 /* Mode data length (LSB) */
2673 mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
2674 /* Medium type - default */
2675 mpd10.hd.med_type = 0;
2676 /* Device-specific param,
2677 bit 8: 0/1 = write enabled/protected
2678 bit 4: 0/1 = FUA enabled */
2679 mpd10.hd.dev_par = 0;
2680
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002681 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002682 mpd10.hd.dev_par = 0x10;
2683 mpd10.hd.rsrvd[0] = 0; /* reserved */
2684 mpd10.hd.rsrvd[1] = 0; /* reserved */
2685 if (scsicmd->cmnd[1] & 0x8) {
2686 /* Block descriptor length (MSB) */
2687 mpd10.hd.bd_length[0] = 0;
2688 /* Block descriptor length (LSB) */
2689 mpd10.hd.bd_length[1] = 0;
2690 } else {
2691 mpd10.hd.bd_length[0] = 0;
2692 mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
2693
2694 mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
2695
2696 mpd10.bd.block_length[0] =
2697 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2698 mpd10.bd.block_length[1] =
2699 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2700 mpd10.bd.block_length[2] =
2701 fsa_dev_ptr[cid].block_size & 0xff;
2702
2703 if (capacity > 0xffffff) {
2704 mpd10.bd.block_count[0] = 0xff;
2705 mpd10.bd.block_count[1] = 0xff;
2706 mpd10.bd.block_count[2] = 0xff;
2707 } else {
2708 mpd10.bd.block_count[0] =
2709 (capacity >> 16) & 0xff;
2710 mpd10.bd.block_count[1] =
2711 (capacity >> 8) & 0xff;
2712 mpd10.bd.block_count[2] =
2713 capacity & 0xff;
2714 }
2715 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002716 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2717 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002718 mpd10.hd.data_length[1] += 3;
2719 mpd10.mpc_buf[0] = 8;
2720 mpd10.mpc_buf[1] = 1;
2721 mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002722 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002723 mode_buf_length = sizeof(mpd10);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002724 if (mode_buf_length > scsicmd->cmnd[8])
2725 mode_buf_length = scsicmd->cmnd[8];
2726 }
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002727 scsi_sg_copy_from_buffer(scsicmd,
2728 (char *)&mpd10,
2729 mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2732 scsicmd->scsi_done(scsicmd);
2733
2734 return 0;
2735 }
2736 case REQUEST_SENSE:
2737 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2738 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2739 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2740 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2741 scsicmd->scsi_done(scsicmd);
2742 return 0;
2743
2744 case ALLOW_MEDIUM_REMOVAL:
2745 dprintk((KERN_DEBUG "LOCK command.\n"));
2746 if (scsicmd->cmnd[4])
2747 fsa_dev_ptr[cid].locked = 1;
2748 else
2749 fsa_dev_ptr[cid].locked = 0;
2750
2751 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2752 scsicmd->scsi_done(scsicmd);
2753 return 0;
2754 /*
2755 * These commands are all No-Ops
2756 */
2757 case TEST_UNIT_READY:
Mark Salyzyn655d7222008-04-30 16:03:42 -04002758 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
2759 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2760 SAM_STAT_CHECK_CONDITION;
2761 set_sense(&dev->fsa_dev[cid].sense_data,
2762 NOT_READY, SENCODE_BECOMING_READY,
2763 ASENCODE_BECOMING_READY, 0, 0);
2764 memcpy(scsicmd->sense_buffer,
2765 &dev->fsa_dev[cid].sense_data,
2766 min_t(size_t,
2767 sizeof(dev->fsa_dev[cid].sense_data),
2768 SCSI_SENSE_BUFFERSIZE));
2769 scsicmd->scsi_done(scsicmd);
2770 return 0;
2771 }
2772 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 case RESERVE:
2774 case RELEASE:
2775 case REZERO_UNIT:
2776 case REASSIGN_BLOCKS:
2777 case SEEK_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2779 scsicmd->scsi_done(scsicmd);
2780 return 0;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002781
2782 case START_STOP:
2783 return aac_start_stop(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002786 switch (scsicmd->cmnd[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 {
2788 case READ_6:
2789 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002790 case READ_12:
2791 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002792 if (dev->in_reset)
2793 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 /*
2795 * Hack to keep track of ordinal number of the device that
2796 * corresponds to a container. Needed to convert
2797 * containers to /dev/sd device names
2798 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002799
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002800 if (scsicmd->request->rq_disk)
2801 strlcpy(fsa_dev_ptr[cid].devname,
2802 scsicmd->request->rq_disk->disk_name,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002803 min(sizeof(fsa_dev_ptr[cid].devname),
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002804 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002805
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002806 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 case WRITE_6:
2809 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002810 case WRITE_12:
2811 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002812 if (dev->in_reset)
2813 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002814 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
2816 case SYNCHRONIZE_CACHE:
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002817 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2818 scsicmd->result = DID_OK << 16 |
2819 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2820 scsicmd->scsi_done(scsicmd);
2821 return 0;
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 /* Issue FIB to tell Firmware to flush it's cache */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002824 if ((aac_cache & 6) != 2)
2825 return aac_synchronize(scsicmd);
2826 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 default:
2828 /*
2829 * Unhandled commands
2830 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002831 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002833 set_sense(&dev->fsa_dev[cid].sense_data,
2834 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2835 ASENCODE_INVALID_COMMAND, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002837 min_t(size_t,
2838 sizeof(dev->fsa_dev[cid].sense_data),
2839 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 scsicmd->scsi_done(scsicmd);
2841 return 0;
2842 }
2843}
2844
2845static int query_disk(struct aac_dev *dev, void __user *arg)
2846{
2847 struct aac_query_disk qd;
2848 struct fsa_dev_info *fsa_dev_ptr;
2849
2850 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002851 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002852 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2854 return -EFAULT;
2855 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002856 qd.cnum = qd.id;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002857 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 {
2859 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2860 return -EINVAL;
2861 qd.instance = dev->scsi_host_ptr->host_no;
2862 qd.bus = 0;
2863 qd.id = CONTAINER_TO_ID(qd.cnum);
2864 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2865 }
2866 else return -EINVAL;
2867
Salyzyn, Markfd622b12007-07-17 10:59:19 -04002868 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2870 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2871
2872 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2873 qd.unmapped = 1;
2874 else
2875 qd.unmapped = 0;
2876
2877 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2878 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2879
2880 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2881 return -EFAULT;
2882 return 0;
2883}
2884
2885static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2886{
2887 struct aac_delete_disk dd;
2888 struct fsa_dev_info *fsa_dev_ptr;
2889
2890 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp651013552006-09-19 08:59:23 -07002891 if (!fsa_dev_ptr)
2892 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2895 return -EFAULT;
2896
2897 if (dd.cnum >= dev->maximum_num_containers)
2898 return -EINVAL;
2899 /*
2900 * Mark this container as being deleted.
2901 */
2902 fsa_dev_ptr[dd.cnum].deleted = 1;
2903 /*
2904 * Mark the container as no longer valid
2905 */
2906 fsa_dev_ptr[dd.cnum].valid = 0;
2907 return 0;
2908}
2909
2910static int delete_disk(struct aac_dev *dev, void __user *arg)
2911{
2912 struct aac_delete_disk dd;
2913 struct fsa_dev_info *fsa_dev_ptr;
2914
2915 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002916 if (!fsa_dev_ptr)
Mark Haverkamp651013552006-09-19 08:59:23 -07002917 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2920 return -EFAULT;
2921
2922 if (dd.cnum >= dev->maximum_num_containers)
2923 return -EINVAL;
2924 /*
2925 * If the container is locked, it can not be deleted by the API.
2926 */
2927 if (fsa_dev_ptr[dd.cnum].locked)
2928 return -EBUSY;
2929 else {
2930 /*
2931 * Mark the container as no longer being valid.
2932 */
2933 fsa_dev_ptr[dd.cnum].valid = 0;
2934 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2935 return 0;
2936 }
2937}
2938
2939int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2940{
2941 switch (cmd) {
2942 case FSACTL_QUERY_DISK:
2943 return query_disk(dev, arg);
2944 case FSACTL_DELETE_DISK:
2945 return delete_disk(dev, arg);
2946 case FSACTL_FORCE_DELETE_DISK:
2947 return force_delete_disk(dev, arg);
2948 case FSACTL_GET_CONTAINERS:
2949 return aac_get_containers(dev);
2950 default:
2951 return -ENOTTY;
2952 }
2953}
2954
2955/**
2956 *
2957 * aac_srb_callback
2958 * @context: the context set in the fib - here it is scsi cmd
2959 * @fibptr: pointer to the fib
2960 *
2961 * Handles the completion of a scsi command to a non dasd device
2962 *
2963 */
2964
2965static void aac_srb_callback(void *context, struct fib * fibptr)
2966{
2967 struct aac_dev *dev;
2968 struct aac_srb_reply *srbreply;
2969 struct scsi_cmnd *scsicmd;
2970
2971 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002972
2973 if (!aac_valid_context(scsicmd, fibptr))
2974 return;
2975
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002976 BUG_ON(fibptr == NULL);
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002977 dev = fibptr->dev;
2978
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04002979 scsi_dma_unmap(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04002981 /* expose physical device if expose_physicald flag is on */
2982 if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
2983 && expose_physicals > 0)
2984 aac_expose_phy_device(scsicmd);
2985
2986 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05302989 if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
2990 /* fast response */
2991 srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
2992 srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
2993 } else {
2994 /*
2995 * Calculate resid for sg
2996 */
2997 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2998 - le32_to_cpu(srbreply->data_xfer_length));
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04002999 /*
3000 * First check the fib status
3001 */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003002
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003003 if (le32_to_cpu(srbreply->status) != ST_OK) {
3004 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003006 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
3007 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3008 SCSI_SENSE_BUFFERSIZE);
3009 scsicmd->result = DID_ERROR << 16
3010 | COMMAND_COMPLETE << 8
3011 | SAM_STAT_CHECK_CONDITION;
3012 memcpy(scsicmd->sense_buffer,
3013 srbreply->sense_data, len);
3014 }
Rajashekhara, Maheshe3cc2682010-05-10 04:12:28 -07003015
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003016 /*
3017 * Next check the srb status
3018 */
3019 switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3020 case SRB_STATUS_ERROR_RECOVERY:
3021 case SRB_STATUS_PENDING:
3022 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08003023 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003025 case SRB_STATUS_DATA_OVERRUN:
3026 switch (scsicmd->cmnd[0]) {
3027 case READ_6:
3028 case WRITE_6:
3029 case READ_10:
3030 case WRITE_10:
3031 case READ_12:
3032 case WRITE_12:
3033 case READ_16:
3034 case WRITE_16:
3035 if (le32_to_cpu(srbreply->data_xfer_length)
3036 < scsicmd->underflow)
3037 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
3038 else
3039 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
3040 scsicmd->result = DID_ERROR << 16
3041 | COMMAND_COMPLETE << 8;
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003042 break;
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003043 case INQUIRY: {
3044 scsicmd->result = DID_OK << 16
3045 | COMMAND_COMPLETE << 8;
3046 break;
3047 }
3048 default:
3049 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3050 break;
3051 }
3052 break;
3053 case SRB_STATUS_ABORTED:
3054 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3055 break;
3056 case SRB_STATUS_ABORT_FAILED:
3057 /*
3058 * Not sure about this one - but assuming the
3059 * hba was trying to abort for some reason
3060 */
3061 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3062 break;
3063 case SRB_STATUS_PARITY_ERROR:
3064 scsicmd->result = DID_PARITY << 16
3065 | MSG_PARITY_ERROR << 8;
3066 break;
3067 case SRB_STATUS_NO_DEVICE:
3068 case SRB_STATUS_INVALID_PATH_ID:
3069 case SRB_STATUS_INVALID_TARGET_ID:
3070 case SRB_STATUS_INVALID_LUN:
3071 case SRB_STATUS_SELECTION_TIMEOUT:
3072 scsicmd->result = DID_NO_CONNECT << 16
3073 | COMMAND_COMPLETE << 8;
3074 break;
3075
3076 case SRB_STATUS_COMMAND_TIMEOUT:
3077 case SRB_STATUS_TIMEOUT:
3078 scsicmd->result = DID_TIME_OUT << 16
3079 | COMMAND_COMPLETE << 8;
3080 break;
3081
3082 case SRB_STATUS_BUSY:
3083 scsicmd->result = DID_BUS_BUSY << 16
3084 | COMMAND_COMPLETE << 8;
3085 break;
3086
3087 case SRB_STATUS_BUS_RESET:
3088 scsicmd->result = DID_RESET << 16
3089 | COMMAND_COMPLETE << 8;
3090 break;
3091
3092 case SRB_STATUS_MESSAGE_REJECTED:
3093 scsicmd->result = DID_ERROR << 16
3094 | MESSAGE_REJECT << 8;
3095 break;
3096 case SRB_STATUS_REQUEST_FLUSHED:
3097 case SRB_STATUS_ERROR:
3098 case SRB_STATUS_INVALID_REQUEST:
3099 case SRB_STATUS_REQUEST_SENSE_FAILED:
3100 case SRB_STATUS_NO_HBA:
3101 case SRB_STATUS_UNEXPECTED_BUS_FREE:
3102 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3103 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3104 case SRB_STATUS_DELAYED_RETRY:
3105 case SRB_STATUS_BAD_FUNCTION:
3106 case SRB_STATUS_NOT_STARTED:
3107 case SRB_STATUS_NOT_IN_USE:
3108 case SRB_STATUS_FORCE_ABORT:
3109 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3110 default:
3111#ifdef AAC_DETAILED_STATUS_INFO
3112 printk(KERN_INFO "aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
3113 le32_to_cpu(srbreply->srb_status) & 0x3F,
3114 aac_get_status_string(
3115 le32_to_cpu(srbreply->srb_status) & 0x3F),
3116 scsicmd->cmnd[0],
3117 le32_to_cpu(srbreply->scsi_status));
3118#endif
3119 if ((scsicmd->cmnd[0] == ATA_12)
3120 || (scsicmd->cmnd[0] == ATA_16)) {
3121 if (scsicmd->cmnd[2] & (0x01 << 5)) {
3122 scsicmd->result = DID_OK << 16
3123 | COMMAND_COMPLETE << 8;
3124 break;
3125 } else {
3126 scsicmd->result = DID_ERROR << 16
3127 | COMMAND_COMPLETE << 8;
3128 break;
3129 }
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003130 } else {
3131 scsicmd->result = DID_ERROR << 16
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003132 | COMMAND_COMPLETE << 8;
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003133 break;
3134 }
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07003135 }
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003136 if (le32_to_cpu(srbreply->scsi_status)
3137 == SAM_STAT_CHECK_CONDITION) {
3138 int len;
3139
3140 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3141 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3142 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143#ifdef AAC_DETAILED_STATUS_INFO
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003144 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
3145 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146#endif
Mahesh Rajashekhara55b87602015-08-28 06:38:37 -04003147 memcpy(scsicmd->sense_buffer,
3148 srbreply->sense_data, len);
3149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
3151 /*
3152 * OR in the scsi status (already shifted up a bit)
3153 */
3154 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3155
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003156 aac_fib_complete(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07003157 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
3160/**
3161 *
3162 * aac_send_scb_fib
3163 * @scsicmd: the scsi command block
3164 *
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08003165 * This routine will form a FIB and fill in the aac_srb from the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 * scsicmd passed in.
3167 */
3168
3169static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3170{
3171 struct fib* cmd_fibcontext;
3172 struct aac_dev* dev;
3173 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Mark Haverkamp84971732005-06-20 11:55:24 -07003175 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08003176 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07003177 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 scsicmd->result = DID_NO_CONNECT << 16;
3179 scsicmd->scsi_done(scsicmd);
3180 return 0;
3181 }
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 /*
3184 * Allocate and initialize a Fib then setup a BlockWrite command
3185 */
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08003186 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3187 if (!cmd_fibcontext)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return -1;
Raghava Aditya Renukunta6bf3b632016-02-03 15:05:59 -08003189
Mark Haverkampe8f32de2007-01-23 15:00:30 -08003190 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 /*
3193 * Check that the command queued to the controller
3194 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08003195 if (status == -EINPROGRESS) {
3196 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return 0;
3198 }
3199
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003200 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3201 aac_fib_complete(cmd_fibcontext);
3202 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 return -1;
3205}
3206
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303207static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 struct aac_dev *dev;
3210 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003211 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3214 // Get rid of old data
3215 psg->count = 0;
3216 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003217 psg->sg[0].count = 0;
3218
3219 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303220 if (nseg < 0)
3221 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003222 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 struct scatterlist *sg;
3224 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003226 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003228 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3230 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3231 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
3233 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003234 if (byte_count > scsi_bufflen(scsicmd)) {
3235 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3236 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003237 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003238 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240 /* Check for command underflow */
3241 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3242 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3243 byte_count, scsicmd->underflow);
3244 }
3245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return byte_count;
3247}
3248
3249
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303250static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
3252 struct aac_dev *dev;
3253 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003254 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003255 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3258 // Get rid of old data
3259 psg->count = 0;
3260 psg->sg[0].addr[0] = 0;
3261 psg->sg[0].addr[1] = 0;
3262 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003263
3264 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303265 if (nseg < 0)
3266 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003267 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 struct scatterlist *sg;
3269 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003271 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08003272 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003273 addr = sg_dma_address(sg);
3274 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3275 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08003276 psg->sg[i].count = cpu_to_le32(count);
3277 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003279 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003281 if (byte_count > scsi_bufflen(scsicmd)) {
3282 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3283 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003284 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003285 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
3287 /* Check for command underflow */
3288 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3289 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3290 byte_count, scsicmd->underflow);
3291 }
3292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return byte_count;
3294}
3295
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303296static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003297{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003298 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003299 int nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003300
3301 // Get rid of old data
3302 psg->count = 0;
3303 psg->sg[0].next = 0;
3304 psg->sg[0].prev = 0;
3305 psg->sg[0].addr[0] = 0;
3306 psg->sg[0].addr[1] = 0;
3307 psg->sg[0].count = 0;
3308 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003309
3310 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303311 if (nseg < 0)
3312 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003313 if (nseg) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003314 struct scatterlist *sg;
3315 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003316
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003317 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003318 int count = sg_dma_len(sg);
3319 u64 addr = sg_dma_address(sg);
3320 psg->sg[i].next = 0;
3321 psg->sg[i].prev = 0;
3322 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3323 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3324 psg->sg[i].count = cpu_to_le32(count);
3325 psg->sg[i].flags = 0;
3326 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003327 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003328 psg->count = cpu_to_le32(nseg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003329 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003330 if (byte_count > scsi_bufflen(scsicmd)) {
3331 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3332 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003333 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003334 byte_count = scsi_bufflen(scsicmd);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003335 }
3336 /* Check for command underflow */
3337 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3338 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3339 byte_count, scsicmd->underflow);
3340 }
3341 }
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003342 return byte_count;
3343}
3344
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303345static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3346 struct aac_raw_io2 *rio2, int sg_max)
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303347{
3348 unsigned long byte_count = 0;
3349 int nseg;
3350
3351 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303352 if (nseg < 0)
3353 return nseg;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303354 if (nseg) {
3355 struct scatterlist *sg;
3356 int i, conformable = 0;
3357 u32 min_size = PAGE_SIZE, cur_size;
3358
3359 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3360 int count = sg_dma_len(sg);
3361 u64 addr = sg_dma_address(sg);
3362
3363 BUG_ON(i >= sg_max);
3364 rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3365 rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3366 cur_size = cpu_to_le32(count);
3367 rio2->sge[i].length = cur_size;
3368 rio2->sge[i].flags = 0;
3369 if (i == 0) {
3370 conformable = 1;
3371 rio2->sgeFirstSize = cur_size;
3372 } else if (i == 1) {
3373 rio2->sgeNominalSize = cur_size;
3374 min_size = cur_size;
3375 } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
3376 conformable = 0;
3377 if (cur_size < min_size)
3378 min_size = cur_size;
3379 }
3380 byte_count += count;
3381 }
3382
3383 /* hba wants the size to be exact */
3384 if (byte_count > scsi_bufflen(scsicmd)) {
3385 u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
3386 (byte_count - scsi_bufflen(scsicmd));
3387 rio2->sge[i-1].length = cpu_to_le32(temp);
3388 byte_count = scsi_bufflen(scsicmd);
3389 }
3390
3391 rio2->sgeCnt = cpu_to_le32(nseg);
3392 rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
3393 /* not conformable: evaluate required sg elements */
3394 if (!conformable) {
3395 int j, nseg_new = nseg, err_found;
3396 for (i = min_size / PAGE_SIZE; i >= 1; --i) {
3397 err_found = 0;
3398 nseg_new = 2;
3399 for (j = 1; j < nseg - 1; ++j) {
3400 if (rio2->sge[j].length % (i*PAGE_SIZE)) {
3401 err_found = 1;
3402 break;
3403 }
3404 nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
3405 }
3406 if (!err_found)
3407 break;
3408 }
3409 if (i > 0 && nseg_new <= sg_max)
3410 aac_convert_sgraw2(rio2, i, nseg, nseg_new);
3411 } else
3412 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3413
3414 /* Check for command underflow */
3415 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3416 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3417 byte_count, scsicmd->underflow);
3418 }
3419 }
3420
3421 return byte_count;
3422}
3423
3424static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
3425{
3426 struct sge_ieee1212 *sge;
3427 int i, j, pos;
3428 u32 addr_low;
3429
3430 if (aac_convert_sgl == 0)
3431 return 0;
3432
3433 sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
3434 if (sge == NULL)
3435 return -1;
3436
3437 for (i = 1, pos = 1; i < nseg-1; ++i) {
3438 for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
3439 addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
3440 sge[pos].addrLow = addr_low;
3441 sge[pos].addrHigh = rio2->sge[i].addrHigh;
3442 if (addr_low < rio2->sge[i].addrLow)
3443 sge[pos].addrHigh++;
3444 sge[pos].length = pages * PAGE_SIZE;
3445 sge[pos].flags = 0;
3446 pos++;
3447 }
3448 }
3449 sge[pos] = rio2->sge[nseg-1];
3450 memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
3451
3452 kfree(sge);
3453 rio2->sgeCnt = cpu_to_le32(nseg_new);
3454 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3455 rio2->sgeNominalSize = pages * PAGE_SIZE;
3456 return 0;
3457}
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459#ifdef AAC_DETAILED_STATUS_INFO
3460
3461struct aac_srb_status_info {
3462 u32 status;
3463 char *str;
3464};
3465
3466
3467static struct aac_srb_status_info srb_status_info[] = {
3468 { SRB_STATUS_PENDING, "Pending Status"},
3469 { SRB_STATUS_SUCCESS, "Success"},
3470 { SRB_STATUS_ABORTED, "Aborted Command"},
3471 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07003472 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 { SRB_STATUS_BUSY, "Device Busy"},
3474 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
3475 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
3476 { SRB_STATUS_NO_DEVICE, "No Device"},
3477 { SRB_STATUS_TIMEOUT, "Timeout"},
3478 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
3479 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
3480 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
3481 { SRB_STATUS_BUS_RESET, "Bus Reset"},
3482 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
3483 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
3484 { SRB_STATUS_NO_HBA, "No HBA"},
3485 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
3486 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
3487 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
3488 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
3489 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
3490 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07003491 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
3493 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
3494 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
3495 { SRB_STATUS_NOT_STARTED, "Not Started"},
3496 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08003497 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
3499 { 0xff, "Unknown Error"}
3500};
3501
3502char *aac_get_status_string(u32 status)
3503{
3504 int i;
3505
Tobias Klauser6391a112006-06-08 22:23:48 -07003506 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
3507 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 return "Bad Status Code";
3511}
3512
3513#endif