blob: aee3eaa0bd5d81180266639f612c71c8b03c857f [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, Mark3a0086a82008-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
166/*
167 * M O D U L E G L O B A L S
168 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800169
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +0530170static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
171static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
172static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
173static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
174 struct aac_raw_io2 *rio2, int sg_max);
175static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
176 int pages, int nseg, int nseg_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
178#ifdef AAC_DETAILED_STATUS_INFO
179static char *aac_get_status_string(u32 status);
180#endif
181
182/*
183 * Non dasd selection is handled entirely in aachba now
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800184 */
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static int nondasd = -1;
Leubner, Achimd8e965072009-04-01 07:16:08 -0700187static int aac_cache = 2; /* WCE=0 to avoid performance problems */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int dacmode = -1;
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800189int aac_msi;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400190int aac_commit = -1;
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700191int startup_timeout = 180;
192int aif_timeout = 120;
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800193int aac_sync_mode; /* Only Sync. transfer - disabled */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530194int aac_convert_sgl = 1; /* convert non-conformable s/g list - enabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Mahesh Rajashekhara11604612012-02-08 22:51:04 -0800196module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
197MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
198 " 0=off, 1=on");
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +0530199module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
200MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
201 " 0=off, 1=on");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800202module_param(nondasd, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800203MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
204 " 0=off, 1=on");
Salyzyn, Mark95e852e2008-01-08 12:01:07 -0800205module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800206MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
207 "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
208 "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
Leubner, Achimd8e965072009-04-01 07:16:08 -0700209 "\tbit 2 - Disable only if Battery is protecting Cache");
Mark Haverkamp9a72f972006-03-27 09:44:37 -0800210module_param(dacmode, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800211MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
212 " 0=off, 1=on");
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400213module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800214MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
215 " adapter for foreign arrays.\n"
216 "This is typically needed in systems that do not have a BIOS."
217 " 0=off, 1=on");
218module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
219MODULE_PARM_DESC(msi, "IRQ handling."
220 " 0=PIC(default), 1=MSI, 2=MSI-X(unsupported, uses MSI)");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700221module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800222MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
223 " adapter to have it's kernel up and\n"
224 "running. This is typically adjusted for large systems that do not"
225 " have a BIOS.");
Mark Haverkamp404d9a92006-05-10 09:12:48 -0700226module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800227MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
228 " applications to pick up AIFs before\n"
229 "deregistering them. This is typically adjusted for heavily burdened"
230 " systems.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700232int numacb = -1;
233module_param(numacb, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800234MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
235 " blocks (FIB) allocated. Valid values are 512 and down. Default is"
236 " to use suggestion from Firmware.");
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -0700237
238int acbsize = -1;
239module_param(acbsize, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800240MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
241 " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
242 " suggestion from Firmware.");
Mark Haverkamp653ba582006-09-19 08:59:43 -0700243
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400244int update_interval = 30 * 60;
245module_param(update_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800246MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
247 " updates issued to adapter.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400248
249int check_interval = 24 * 60 * 60;
250module_param(check_interval, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800251MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
252 " checks.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400253
Andrew Morton87f3bda2007-08-10 14:50:30 -0700254int aac_check_reset = 1;
255module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
Paul Bolle95e7a8e2008-04-16 14:43:00 +0200256MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800257 " adapter. a value of -1 forces the reset to adapters programmed to"
258 " ignore it.");
Salyzyn, Mark29c97682007-06-12 09:33:54 -0400259
Mark Haverkampe37ee4b2007-01-26 09:23:32 -0800260int expose_physicals = -1;
Mark Haverkamp653ba582006-09-19 08:59:43 -0700261module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800262MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
263 " -1=protect 0=off, 1=on");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700264
Salyzyn, Mark8ef22242008-02-08 05:48:22 -0800265int aac_reset_devices;
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400266module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
267MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
Mark Haverkamp03d44332007-03-15 10:27:45 -0700268
Leubner, Achimd8e965072009-04-01 07:16:08 -0700269int aac_wwn = 1;
270module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
271MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
272 "\t0 - Disable\n"
273 "\t1 - Array Meta Data Signature (default)\n"
274 "\t2 - Adapter Serial Number");
275
276
Mark Haverkamp03d44332007-03-15 10:27:45 -0700277static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
278 struct fib *fibptr) {
279 struct scsi_device *device;
280
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800281 if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
Salyzyn, Markc835e372007-07-26 14:20:02 -0400282 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800283 aac_fib_complete(fibptr);
284 aac_fib_free(fibptr);
285 return 0;
286 }
Mark Haverkamp03d44332007-03-15 10:27:45 -0700287 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
288 device = scsicmd->device;
289 if (unlikely(!device || !scsi_device_online(device))) {
290 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
291 aac_fib_complete(fibptr);
292 aac_fib_free(fibptr);
293 return 0;
294 }
295 return 1;
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/**
299 * aac_get_config_status - check the adapter configuration
300 * @common: adapter to query
301 *
302 * Query config status, and commit the configuration if needed.
303 */
Mark Haverkamp8c867b22006-08-03 08:03:30 -0700304int aac_get_config_status(struct aac_dev *dev, int commit_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int status = 0;
307 struct fib * fibptr;
308
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800309 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -ENOMEM;
311
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800312 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 {
314 struct aac_get_config_status *dinfo;
315 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
316
317 dinfo->command = cpu_to_le32(VM_ContainerConfig);
318 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
319 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
320 }
321
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800322 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 fibptr,
324 sizeof (struct aac_get_config_status),
325 FsaNormal,
326 1, 1,
327 NULL, NULL);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800328 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
330 } else {
331 struct aac_get_config_status_resp *reply
332 = (struct aac_get_config_status_resp *) fib_data(fibptr);
333 dprintk((KERN_WARNING
334 "aac_get_config_status: response=%d status=%d action=%d\n",
335 le32_to_cpu(reply->response),
336 le32_to_cpu(reply->status),
337 le32_to_cpu(reply->data.action)));
338 if ((le32_to_cpu(reply->response) != ST_OK) ||
339 (le32_to_cpu(reply->status) != CT_OK) ||
340 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
341 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
342 status = -EINVAL;
343 }
344 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530345 /* Do not set XferState to zero unless receives a response from F/W */
346 if (status >= 0)
347 aac_fib_complete(fibptr);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
350 if (status >= 0) {
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400351 if ((aac_commit == 1) || commit_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct aac_commit_config * dinfo;
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800353 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 dinfo = (struct aac_commit_config *) fib_data(fibptr);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 dinfo->command = cpu_to_le32(VM_ContainerConfig);
357 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800358
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800359 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 fibptr,
361 sizeof (struct aac_commit_config),
362 FsaNormal,
363 1, 1,
364 NULL, NULL);
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530365 /* Do not set XferState to zero unless
366 * receives a response from F/W */
367 if (status >= 0)
368 aac_fib_complete(fibptr);
Salyzyn, Mark1208bab2007-05-22 09:32:29 -0400369 } else if (aac_commit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 printk(KERN_WARNING
371 "aac_get_config_status: Foreign device configurations are being ignored\n");
372 }
373 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530374 /* FIB should be freed only after getting the response from the F/W */
375 if (status != -ERESTARTSYS)
376 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return status;
378}
379
Rajashekhara, Maheshe3cc2682010-05-10 04:12:28 -0700380static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
381{
382 char inq_data;
383 scsi_sg_copy_to_buffer(scsicmd, &inq_data, sizeof(inq_data));
384 if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
385 inq_data &= 0xdf;
386 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
387 }
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/**
391 * aac_get_containers - list containers
392 * @common: adapter to probe
393 *
394 * Make a list of all containers on this controller
395 */
396int aac_get_containers(struct aac_dev *dev)
397{
398 struct fsa_dev_info *fsa_dev_ptr;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800399 u32 index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 int status = 0;
401 struct fib * fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct aac_get_container_count *dinfo;
403 struct aac_get_container_count_resp *dresp;
404 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
405
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800406 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return -ENOMEM;
408
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800409 aac_fib_init(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
411 dinfo->command = cpu_to_le32(VM_ContainerConfig);
412 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
413
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800414 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 fibptr,
416 sizeof (struct aac_get_container_count),
417 FsaNormal,
418 1, 1,
419 NULL, NULL);
420 if (status >= 0) {
421 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
422 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800423 aac_fib_complete(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +0530425 /* FIB should be freed only after getting the response from the F/W */
426 if (status != -ERESTARTSYS)
427 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
430 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400431 fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700432 GFP_KERNEL);
433 if (!fsa_dev_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 dev->fsa_dev = fsa_dev_ptr;
437 dev->maximum_num_containers = maximum_num_containers;
438
Mark Haverkampfe76df42007-03-15 12:55:07 -0700439 for (index = 0; index < dev->maximum_num_containers; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 fsa_dev_ptr[index].devname[0] = '\0';
441
Mark Haverkampfe76df42007-03-15 12:55:07 -0700442 status = aac_probe_container(dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Mark Haverkampfe76df42007-03-15 12:55:07 -0700444 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
446 break;
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * If there are no more containers, then stop asking.
451 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700452 if (++index >= status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return status;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static void get_container_name_callback(void *context, struct fib * fibptr)
459{
460 struct aac_get_name_resp * get_name_reply;
461 struct scsi_cmnd * scsicmd;
462
463 scsicmd = (struct scsi_cmnd *) context;
464
Mark Haverkamp03d44332007-03-15 10:27:45 -0700465 if (!aac_valid_context(scsicmd, fibptr))
466 return;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700469 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
472 /* Failure is irrelevant, using default value instead */
473 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
474 && (get_name_reply->data[0] != '\0')) {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700475 char *sp = get_name_reply->data;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400476 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 while (*sp == ' ')
478 ++sp;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700479 if (*sp) {
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900480 struct inquiry_data inq;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700481 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
482 int count = sizeof(d);
483 char *dp = d;
484 do {
485 *dp++ = (*sp) ? *sp++ : ' ';
486 } while (--count > 0);
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900487
488 scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
489 memcpy(inq.inqd_pid, d, sizeof(d));
490 scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
495
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800496 aac_fib_complete(fibptr);
497 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -0700498 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/**
502 * aac_get_container_name - get container name, none blocking.
503 */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700504static int aac_get_container_name(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 int status;
507 struct aac_get_name *dinfo;
508 struct fib * cmd_fibcontext;
509 struct aac_dev * dev;
510
511 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
512
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800513 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -ENOMEM;
515
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800516 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
518
519 dinfo->command = cpu_to_le32(VM_ContainerConfig);
520 dinfo->type = cpu_to_le32(CT_READ_NAME);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -0700521 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
523
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800524 status = aac_fib_send(ContainerCommand,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800525 cmd_fibcontext,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sizeof (struct aac_get_name),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800527 FsaNormal,
528 0, 1,
529 (fib_callback)get_container_name_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 (void *) scsicmd);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 * Check that the command queued to the controller
534 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800535 if (status == -EINPROGRESS) {
536 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -0800538 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800539
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800540 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
541 aac_fib_complete(cmd_fibcontext);
542 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -1;
544}
545
Mark Haverkampfe76df42007-03-15 12:55:07 -0700546static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
547{
548 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
549
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400550 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
Mark Haverkampfe76df42007-03-15 12:55:07 -0700551 return aac_scsi_cmd(scsicmd);
552
553 scsicmd->result = DID_NO_CONNECT << 16;
554 scsicmd->scsi_done(scsicmd);
555 return 0;
556}
557
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400558static void _aac_probe_container2(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700559{
Mark Haverkamp03d44332007-03-15 10:27:45 -0700560 struct fsa_dev_info *fsa_dev_ptr;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700561 int (*callback)(struct scsi_cmnd *);
Mark Haverkamp03d44332007-03-15 10:27:45 -0700562 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
563
Mark Haverkamp03d44332007-03-15 10:27:45 -0700564
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400565 if (!aac_valid_context(scsicmd, fibptr))
566 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700567
568 scsicmd->SCp.Status = 0;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400569 fsa_dev_ptr = fibptr->dev->fsa_dev;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700570 if (fsa_dev_ptr) {
571 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
572 fsa_dev_ptr += scmd_id(scsicmd);
573
574 if ((le32_to_cpu(dresp->status) == ST_OK) &&
575 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
576 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400577 if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
578 AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
579 dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
580 fsa_dev_ptr->block_size = 0x200;
581 } else {
582 fsa_dev_ptr->block_size =
583 le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
584 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700585 fsa_dev_ptr->valid = 1;
Mark Salyzyn655d7222008-04-30 16:03:42 -0400586 /* sense_key holds the current state of the spin-up */
587 if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
588 fsa_dev_ptr->sense_data.sense_key = NOT_READY;
589 else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
590 fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700591 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
592 fsa_dev_ptr->size
593 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
594 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
595 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
596 }
597 if ((fsa_dev_ptr->valid & 1) == 0)
598 fsa_dev_ptr->valid = 0;
599 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
600 }
601 aac_fib_complete(fibptr);
602 aac_fib_free(fibptr);
603 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
604 scsicmd->SCp.ptr = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400605 (*callback)(scsicmd);
606 return;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700607}
608
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400609static void _aac_probe_container1(void * context, struct fib * fibptr)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700610{
611 struct scsi_cmnd * scsicmd;
612 struct aac_mount * dresp;
613 struct aac_query_mount *dinfo;
614 int status;
615
616 dresp = (struct aac_mount *) fib_data(fibptr);
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400617 if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
618 AAC_OPTION_VARIABLE_BLOCK_SIZE))
619 dresp->mnt[0].capacityhigh = 0;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700620 if ((le32_to_cpu(dresp->status) != ST_OK) ||
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400621 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
622 _aac_probe_container2(context, fibptr);
623 return;
624 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700625 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkampfe76df42007-03-15 12:55:07 -0700626
Mark Haverkamp03d44332007-03-15 10:27:45 -0700627 if (!aac_valid_context(scsicmd, fibptr))
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400628 return;
Mark Haverkamp03d44332007-03-15 10:27:45 -0700629
Mark Haverkampfe76df42007-03-15 12:55:07 -0700630 aac_fib_init(fibptr);
631
632 dinfo = (struct aac_query_mount *)fib_data(fibptr);
633
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400634 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
635 AAC_OPTION_VARIABLE_BLOCK_SIZE)
636 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
637 else
638 dinfo->command = cpu_to_le32(VM_NameServe64);
639
Mark Haverkampfe76df42007-03-15 12:55:07 -0700640 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
641 dinfo->type = cpu_to_le32(FT_FILESYS);
642
643 status = aac_fib_send(ContainerCommand,
644 fibptr,
645 sizeof(struct aac_query_mount),
646 FsaNormal,
647 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400648 _aac_probe_container2,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700649 (void *) scsicmd);
650 /*
651 * Check that the command queued to the controller
652 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400653 if (status == -EINPROGRESS)
Mark Haverkampfe76df42007-03-15 12:55:07 -0700654 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400655 else if (status < 0) {
Mark Haverkampfe76df42007-03-15 12:55:07 -0700656 /* Inherit results from VM_NameServe, if any */
657 dresp->status = cpu_to_le32(ST_OK);
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400658 _aac_probe_container2(context, fibptr);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700659 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700660}
661
662static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
663{
664 struct fib * fibptr;
665 int status = -ENOMEM;
666
667 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
668 struct aac_query_mount *dinfo;
669
670 aac_fib_init(fibptr);
671
672 dinfo = (struct aac_query_mount *)fib_data(fibptr);
673
Mahesh Rajashekharab8364392015-03-26 10:41:24 -0400674 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
675 AAC_OPTION_VARIABLE_BLOCK_SIZE)
676 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
677 else
678 dinfo->command = cpu_to_le32(VM_NameServe);
679
Mark Haverkampfe76df42007-03-15 12:55:07 -0700680 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
681 dinfo->type = cpu_to_le32(FT_FILESYS);
682 scsicmd->SCp.ptr = (char *)callback;
683
684 status = aac_fib_send(ContainerCommand,
685 fibptr,
686 sizeof(struct aac_query_mount),
687 FsaNormal,
688 0, 1,
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400689 _aac_probe_container1,
Mark Haverkampfe76df42007-03-15 12:55:07 -0700690 (void *) scsicmd);
691 /*
692 * Check that the command queued to the controller
693 */
694 if (status == -EINPROGRESS) {
695 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
696 return 0;
697 }
698 if (status < 0) {
699 scsicmd->SCp.ptr = NULL;
700 aac_fib_complete(fibptr);
701 aac_fib_free(fibptr);
702 }
703 }
704 if (status < 0) {
705 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
706 if (fsa_dev_ptr) {
707 fsa_dev_ptr += scmd_id(scsicmd);
708 if ((fsa_dev_ptr->valid & 1) == 0) {
709 fsa_dev_ptr->valid = 0;
710 return (*callback)(scsicmd);
711 }
712 }
713 }
714 return status;
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/**
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800718 * aac_probe_container - query a logical volume
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * @dev: device to query
720 * @cid: container identifier
721 *
722 * Queries the controller about the given volume. The volume information
723 * is updated in the struct fsa_dev_info structure rather than returned.
724 */
Mark Haverkampfe76df42007-03-15 12:55:07 -0700725static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
726{
727 scsicmd->device = NULL;
728 return 0;
729}
730
Mark Haverkampbfb35aa82006-02-01 09:30:55 -0800731int aac_probe_container(struct aac_dev *dev, int cid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Mark Haverkampfe76df42007-03-15 12:55:07 -0700733 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
734 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Mark Haverkampfe76df42007-03-15 12:55:07 -0700737 if (!scsicmd || !scsidev) {
738 kfree(scsicmd);
739 kfree(scsidev);
Mark Haverkamp90ee3462006-08-03 08:03:07 -0700740 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Mark Haverkampfe76df42007-03-15 12:55:07 -0700742 scsicmd->list.next = NULL;
Salyzyn, Mark1a655042007-06-11 16:17:55 -0400743 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Mark Haverkampfe76df42007-03-15 12:55:07 -0700745 scsicmd->device = scsidev;
746 scsidev->sdev_state = 0;
747 scsidev->id = cid;
748 scsidev->host = dev->scsi_host_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Mark Haverkampfe76df42007-03-15 12:55:07 -0700750 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
751 while (scsicmd->device == scsidev)
752 schedule();
Salyzyn, Mark802ae2f2007-03-21 13:49:47 -0400753 kfree(scsidev);
Mark Haverkampfe76df42007-03-15 12:55:07 -0700754 status = scsicmd->SCp.Status;
755 kfree(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return status;
757}
758
759/* Local Structure to set SCSI inquiry data strings */
760struct scsi_inq {
761 char vid[8]; /* Vendor ID */
762 char pid[16]; /* Product ID */
763 char prl[4]; /* Product Revision Level */
764};
765
766/**
767 * InqStrCopy - string merge
768 * @a: string to copy from
769 * @b: string to copy to
770 *
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800771 * Copy a String from one location to another
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * without copying \0
773 */
774
775static void inqstrcpy(char *a, char *b)
776{
777
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800778 while (*a != (char)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *b++ = *a++;
780}
781
782static char *container_types[] = {
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800783 "None",
784 "Volume",
785 "Mirror",
786 "Stripe",
787 "RAID5",
788 "SSRW",
789 "SSRO",
790 "Morph",
791 "Legacy",
792 "RAID4",
793 "RAID10",
794 "RAID00",
795 "V-MIRRORS",
796 "PSEUDO R4",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 "RAID50",
Mark Haverkamp84971732005-06-20 11:55:24 -0700798 "RAID5D",
799 "RAID5D0",
800 "RAID1E",
801 "RAID6",
802 "RAID60",
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -0800803 "Unknown"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Salyzyn, Mark17eaace2008-01-08 12:23:49 -0800806char * get_container_type(unsigned tindex)
807{
808 if (tindex >= ARRAY_SIZE(container_types))
809 tindex = ARRAY_SIZE(container_types) - 1;
810 return container_types[tindex];
811}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813/* Function: setinqstr
814 *
815 * Arguments: [1] pointer to void [1] int
816 *
817 * Purpose: Sets SCSI inquiry data strings for vendor, product
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300818 * and revision level. Allows strings to be set in platform dependent
819 * files instead of in OS dependent driver source.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
821
Mark Haverkamp794d0602005-10-24 10:51:53 -0700822static void setinqstr(struct aac_dev *dev, void *data, int tindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct scsi_inq *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
Mark Haverkamp794d0602005-10-24 10:51:53 -0700827 memset(str, ' ', sizeof(*str));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Mark Haverkamp794d0602005-10-24 10:51:53 -0700829 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
830 char * cp = dev->supplement_adapter_info.AdapterTypeText;
Salyzyn, Mark3bc80702008-01-11 13:46:24 -0800831 int c;
832 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
833 inqstrcpy("SMC", str->vid);
834 else {
835 c = sizeof(str->vid);
836 while (*cp && *cp != ' ' && --c)
837 ++cp;
838 c = *cp;
839 *cp = '\0';
840 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
841 str->vid);
842 *cp = c;
843 while (*cp && *cp != ' ')
844 ++cp;
845 }
Mark Haverkamp794d0602005-10-24 10:51:53 -0700846 while (*cp == ' ')
847 ++cp;
848 /* last six chars reserved for vol type */
849 c = 0;
850 if (strlen(cp) > sizeof(str->pid)) {
851 c = cp[sizeof(str->pid)];
852 cp[sizeof(str->pid)] = '\0';
853 }
854 inqstrcpy (cp, str->pid);
855 if (c)
856 cp[sizeof(str->pid)] = c;
857 } else {
858 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
Tobias Klauser6391a112006-06-08 22:23:48 -0700859
860 inqstrcpy (mp->vname, str->vid);
Mark Haverkamp794d0602005-10-24 10:51:53 -0700861 /* last six chars reserved for vol type */
862 inqstrcpy (mp->model, str->pid);
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Tobias Klauser6391a112006-06-08 22:23:48 -0700865 if (tindex < ARRAY_SIZE(container_types)){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 char *findit = str->pid;
867
868 for ( ; *findit != ' '; findit++); /* walk till we find a space */
869 /* RAID is superfluous in the context of a RAID device */
870 if (memcmp(findit-4, "RAID", 4) == 0)
871 *(findit -= 4) = ' ';
Mark Haverkamp794d0602005-10-24 10:51:53 -0700872 if (((findit - str->pid) + strlen(container_types[tindex]))
873 < (sizeof(str->pid) + sizeof(str->prl)))
874 inqstrcpy (container_types[tindex], findit + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 inqstrcpy ("V1.0", str->prl);
877}
878
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400879static void get_container_serial_callback(void *context, struct fib * fibptr)
880{
881 struct aac_get_serial_resp * get_serial_reply;
882 struct scsi_cmnd * scsicmd;
883
884 BUG_ON(fibptr == NULL);
885
886 scsicmd = (struct scsi_cmnd *) context;
887 if (!aac_valid_context(scsicmd, fibptr))
888 return;
889
890 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
891 /* Failure is irrelevant, using default value instead */
892 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
893 char sp[13];
894 /* EVPD bit set */
895 sp[0] = INQD_PDT_DA;
896 sp[1] = scsicmd->cmnd[2];
897 sp[2] = 0;
898 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
899 le32_to_cpu(get_serial_reply->uid));
FUJITA Tomonorid4345022008-03-09 13:44:34 +0900900 scsi_sg_copy_from_buffer(scsicmd, sp, sizeof(sp));
Salyzyn, Mark88e2f982007-07-17 14:01:28 -0400901 }
902
903 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
904
905 aac_fib_complete(fibptr);
906 aac_fib_free(fibptr);
907 scsicmd->scsi_done(scsicmd);
908}
909
910/**
911 * aac_get_container_serial - get container serial, none blocking.
912 */
913static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
914{
915 int status;
916 struct aac_get_serial *dinfo;
917 struct fib * cmd_fibcontext;
918 struct aac_dev * dev;
919
920 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
921
922 if (!(cmd_fibcontext = aac_fib_alloc(dev)))
923 return -ENOMEM;
924
925 aac_fib_init(cmd_fibcontext);
926 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
927
928 dinfo->command = cpu_to_le32(VM_ContainerConfig);
929 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
930 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
931
932 status = aac_fib_send(ContainerCommand,
933 cmd_fibcontext,
934 sizeof (struct aac_get_serial),
935 FsaNormal,
936 0, 1,
937 (fib_callback) get_container_serial_callback,
938 (void *) scsicmd);
939
940 /*
941 * Check that the command queued to the controller
942 */
943 if (status == -EINPROGRESS) {
944 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
945 return 0;
946 }
947
948 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
949 aac_fib_complete(cmd_fibcontext);
950 aac_fib_free(cmd_fibcontext);
951 return -1;
952}
953
954/* Function: setinqserial
955 *
956 * Arguments: [1] pointer to void [1] int
957 *
958 * Purpose: Sets SCSI Unit Serial number.
959 * This is a fake. We should read a proper
960 * serial number from the container. <SuSE>But
961 * without docs it's quite hard to do it :-)
962 * So this will have to do in the meantime.</SuSE>
963 */
964
965static int setinqserial(struct aac_dev *dev, void *data, int cid)
966{
967 /*
968 * This breaks array migration.
969 */
970 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
971 le32_to_cpu(dev->adapter_info.serial[0]), cid);
972}
973
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800974static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
975 u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800977 u8 *sense_buf = (u8 *)sense_data;
978 /* Sense data valid, err code 70h */
979 sense_buf[0] = 0x70; /* No info field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sense_buf[1] = 0; /* Segment number, always zero */
981
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800982 sense_buf[2] = sense_key; /* Sense key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 sense_buf[12] = sense_code; /* Additional sense code */
985 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (sense_key == ILLEGAL_REQUEST) {
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800988 sense_buf[7] = 10; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800990 sense_buf[15] = bit_pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 /* Illegal parameter is in the parameter block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (sense_code == SENCODE_INVALID_CDB_FIELD)
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800993 sense_buf[15] |= 0xc0;/* Std sense key specific field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* Illegal parameter is in the CDB block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 sense_buf[16] = field_pointer >> 8; /* MSB */
996 sense_buf[17] = field_pointer; /* LSB */
Salyzyn, Mark8e31e602008-02-06 13:54:12 -0800997 } else
998 sense_buf[7] = 6; /* Additional sense length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001001static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1002{
1003 if (lba & 0xffffffff00000000LL) {
1004 int cid = scmd_id(cmd);
1005 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1006 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1007 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001008 set_sense(&dev->fsa_dev[cid].sense_data,
1009 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1010 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001011 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08001012 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1013 SCSI_SENSE_BUFFERSIZE));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001014 cmd->scsi_done(cmd);
1015 return 1;
1016 }
1017 return 0;
1018}
1019
1020static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1021{
1022 return 0;
1023}
1024
1025static void io_callback(void *context, struct fib * fibptr);
1026
1027static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1028{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301029 struct aac_dev *dev = fib->dev;
1030 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301031 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001032
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301033 aac_fib_init(fib);
1034 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
1035 struct aac_raw_io2 *readcmd2;
1036 readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1037 memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1038 readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1039 readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001040 readcmd2->byteCount = cpu_to_le32(count *
1041 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301042 readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1043 readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301044 ret = aac_build_sgraw2(cmd, readcmd2,
1045 dev->scsi_host_ptr->sg_tablesize);
1046 if (ret < 0)
1047 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301048 command = ContainerRawIo2;
1049 fibsize = sizeof(struct aac_raw_io2) +
1050 ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1051 } else {
1052 struct aac_raw_io *readcmd;
1053 readcmd = (struct aac_raw_io *) fib_data(fib);
1054 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1055 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001056 readcmd->count = cpu_to_le32(count *
1057 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301058 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1059 readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1060 readcmd->bpTotal = 0;
1061 readcmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301062 ret = aac_build_sgraw(cmd, &readcmd->sg);
1063 if (ret < 0)
1064 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301065 command = ContainerRawIo;
1066 fibsize = sizeof(struct aac_raw_io) +
1067 ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1068 }
1069
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001070 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1071 /*
1072 * Now send the Fib to the adapter
1073 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301074 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001075 fib,
1076 fibsize,
1077 FsaNormal,
1078 0, 1,
1079 (fib_callback) io_callback,
1080 (void *) cmd);
1081}
1082
1083static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1084{
1085 u16 fibsize;
1086 struct aac_read64 *readcmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301087 long ret;
1088
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001089 aac_fib_init(fib);
1090 readcmd = (struct aac_read64 *) fib_data(fib);
1091 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1092 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1093 readcmd->sector_count = cpu_to_le16(count);
1094 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1095 readcmd->pad = 0;
1096 readcmd->flags = 0;
1097
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301098 ret = aac_build_sg64(cmd, &readcmd->sg);
1099 if (ret < 0)
1100 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001101 fibsize = sizeof(struct aac_read64) +
1102 ((le32_to_cpu(readcmd->sg.count) - 1) *
1103 sizeof (struct sgentry64));
1104 BUG_ON (fibsize > (fib->dev->max_fib_size -
1105 sizeof(struct aac_fibhdr)));
1106 /*
1107 * Now send the Fib to the adapter
1108 */
1109 return aac_fib_send(ContainerCommand64,
1110 fib,
1111 fibsize,
1112 FsaNormal,
1113 0, 1,
1114 (fib_callback) io_callback,
1115 (void *) cmd);
1116}
1117
1118static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1119{
1120 u16 fibsize;
1121 struct aac_read *readcmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001122 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301123 long ret;
1124
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001125 aac_fib_init(fib);
1126 readcmd = (struct aac_read *) fib_data(fib);
1127 readcmd->command = cpu_to_le32(VM_CtBlockRead);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001128 readcmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001129 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001130 readcmd->count = cpu_to_le32(count *
1131 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001132
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301133 ret = aac_build_sg(cmd, &readcmd->sg);
1134 if (ret < 0)
1135 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001136 fibsize = sizeof(struct aac_read) +
1137 ((le32_to_cpu(readcmd->sg.count) - 1) *
1138 sizeof (struct sgentry));
1139 BUG_ON (fibsize > (fib->dev->max_fib_size -
1140 sizeof(struct aac_fibhdr)));
1141 /*
1142 * Now send the Fib to the adapter
1143 */
1144 return aac_fib_send(ContainerCommand,
1145 fib,
1146 fibsize,
1147 FsaNormal,
1148 0, 1,
1149 (fib_callback) io_callback,
1150 (void *) cmd);
1151}
1152
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001153static 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 -08001154{
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301155 struct aac_dev *dev = fib->dev;
1156 u16 fibsize, command;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301157 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001158
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301159 aac_fib_init(fib);
1160 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
1161 struct aac_raw_io2 *writecmd2;
1162 writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1163 memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1164 writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1165 writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001166 writecmd2->byteCount = cpu_to_le32(count *
1167 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301168 writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1169 writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1170 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1171 cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1172 cpu_to_le16(RIO2_IO_TYPE_WRITE);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301173 ret = aac_build_sgraw2(cmd, writecmd2,
1174 dev->scsi_host_ptr->sg_tablesize);
1175 if (ret < 0)
1176 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301177 command = ContainerRawIo2;
1178 fibsize = sizeof(struct aac_raw_io2) +
1179 ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1180 } else {
1181 struct aac_raw_io *writecmd;
1182 writecmd = (struct aac_raw_io *) fib_data(fib);
1183 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1184 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001185 writecmd->count = cpu_to_le32(count *
1186 dev->fsa_dev[scmd_id(cmd)].block_size);
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301187 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1188 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1189 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1190 cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1191 cpu_to_le16(RIO_TYPE_WRITE);
1192 writecmd->bpTotal = 0;
1193 writecmd->bpComplete = 0;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301194 ret = aac_build_sgraw(cmd, &writecmd->sg);
1195 if (ret < 0)
1196 return ret;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301197 command = ContainerRawIo;
1198 fibsize = sizeof(struct aac_raw_io) +
1199 ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1200 }
1201
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001202 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1203 /*
1204 * Now send the Fib to the adapter
1205 */
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05301206 return aac_fib_send(command,
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001207 fib,
1208 fibsize,
1209 FsaNormal,
1210 0, 1,
1211 (fib_callback) io_callback,
1212 (void *) cmd);
1213}
1214
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001215static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001216{
1217 u16 fibsize;
1218 struct aac_write64 *writecmd;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301219 long ret;
1220
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001221 aac_fib_init(fib);
1222 writecmd = (struct aac_write64 *) fib_data(fib);
1223 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1224 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1225 writecmd->sector_count = cpu_to_le16(count);
1226 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1227 writecmd->pad = 0;
1228 writecmd->flags = 0;
1229
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301230 ret = aac_build_sg64(cmd, &writecmd->sg);
1231 if (ret < 0)
1232 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001233 fibsize = sizeof(struct aac_write64) +
1234 ((le32_to_cpu(writecmd->sg.count) - 1) *
1235 sizeof (struct sgentry64));
1236 BUG_ON (fibsize > (fib->dev->max_fib_size -
1237 sizeof(struct aac_fibhdr)));
1238 /*
1239 * Now send the Fib to the adapter
1240 */
1241 return aac_fib_send(ContainerCommand64,
1242 fib,
1243 fibsize,
1244 FsaNormal,
1245 0, 1,
1246 (fib_callback) io_callback,
1247 (void *) cmd);
1248}
1249
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001250static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001251{
1252 u16 fibsize;
1253 struct aac_write *writecmd;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001254 struct aac_dev *dev = fib->dev;
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301255 long ret;
1256
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001257 aac_fib_init(fib);
1258 writecmd = (struct aac_write *) fib_data(fib);
1259 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
Christoph Hellwigf3307f72007-11-08 17:27:47 +00001260 writecmd->cid = cpu_to_le32(scmd_id(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001261 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04001262 writecmd->count = cpu_to_le32(count *
1263 dev->fsa_dev[scmd_id(cmd)].block_size);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001264 writecmd->sg.count = cpu_to_le32(1);
1265 /* ->stable is not used - it did mean which type of write */
1266
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301267 ret = aac_build_sg(cmd, &writecmd->sg);
1268 if (ret < 0)
1269 return ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001270 fibsize = sizeof(struct aac_write) +
1271 ((le32_to_cpu(writecmd->sg.count) - 1) *
1272 sizeof (struct sgentry));
1273 BUG_ON (fibsize > (fib->dev->max_fib_size -
1274 sizeof(struct aac_fibhdr)));
1275 /*
1276 * Now send the Fib to the adapter
1277 */
1278 return aac_fib_send(ContainerCommand,
1279 fib,
1280 fibsize,
1281 FsaNormal,
1282 0, 1,
1283 (fib_callback) io_callback,
1284 (void *) cmd);
1285}
1286
1287static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1288{
1289 struct aac_srb * srbcmd;
1290 u32 flag;
1291 u32 timeout;
1292
1293 aac_fib_init(fib);
1294 switch(cmd->sc_data_direction){
1295 case DMA_TO_DEVICE:
1296 flag = SRB_DataOut;
1297 break;
1298 case DMA_BIDIRECTIONAL:
1299 flag = SRB_DataIn | SRB_DataOut;
1300 break;
1301 case DMA_FROM_DEVICE:
1302 flag = SRB_DataIn;
1303 break;
1304 case DMA_NONE:
1305 default: /* shuts up some versions of gcc */
1306 flag = SRB_NoDataXfer;
1307 break;
1308 }
1309
1310 srbcmd = (struct aac_srb*) fib_data(fib);
1311 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1312 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1313 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1314 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1315 srbcmd->flags = cpu_to_le32(flag);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001316 timeout = cmd->request->timeout/HZ;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001317 if (timeout == 0)
1318 timeout = 1;
1319 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1320 srbcmd->retry_limit = 0; /* Obsolete parameter */
1321 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1322 return srbcmd;
1323}
1324
1325static void aac_srb_callback(void *context, struct fib * fibptr);
1326
1327static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1328{
1329 u16 fibsize;
1330 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301331 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001332
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301333 ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1334 if (ret < 0)
1335 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001336 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001337
1338 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1339 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1340 /*
1341 * Build Scatter/Gather list
1342 */
1343 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1344 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1345 sizeof (struct sgentry64));
1346 BUG_ON (fibsize > (fib->dev->max_fib_size -
1347 sizeof(struct aac_fibhdr)));
1348
1349 /*
1350 * Now send the Fib to the adapter
1351 */
1352 return aac_fib_send(ScsiPortCommand64, fib,
1353 fibsize, FsaNormal, 0, 1,
1354 (fib_callback) aac_srb_callback,
1355 (void *) cmd);
1356}
1357
1358static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1359{
1360 u16 fibsize;
1361 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301362 long ret;
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001363
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05301364 ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1365 if (ret < 0)
1366 return ret;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001367 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001368
1369 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1370 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1371 /*
1372 * Build Scatter/Gather list
1373 */
1374 fibsize = sizeof (struct aac_srb) +
1375 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1376 sizeof (struct sgentry));
1377 BUG_ON (fibsize > (fib->dev->max_fib_size -
1378 sizeof(struct aac_fibhdr)));
1379
1380 /*
1381 * Now send the Fib to the adapter
1382 */
1383 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1384 (fib_callback) aac_srb_callback, (void *) cmd);
1385}
1386
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001387static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1388{
Leubner, Achimd8e965072009-04-01 07:16:08 -07001389 if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1390 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001391 return FAILED;
1392 return aac_scsi_32(fib, cmd);
1393}
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395int aac_get_adapter_info(struct aac_dev* dev)
1396{
1397 struct fib* fibptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 int rcode;
1399 u32 tmp;
Mark Haverkamp84971732005-06-20 11:55:24 -07001400 struct aac_adapter_info *info;
1401 struct aac_bus_info *command;
1402 struct aac_bus_info_response *bus_info;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001403
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001404 if (!(fibptr = aac_fib_alloc(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -ENOMEM;
1406
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001407 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001408 info = (struct aac_adapter_info *) fib_data(fibptr);
1409 memset(info,0,sizeof(*info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001411 rcode = aac_fib_send(RequestAdapterInfo,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001412 fibptr,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001413 sizeof(*info),
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001414 FsaNormal,
Mark Haverkamp92033442005-09-20 12:56:50 -07001415 -1, 1, /* First `interrupt' command uses special wait */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001416 NULL,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001417 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001419 if (rcode < 0) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301420 /* FIB should be freed only after
1421 * getting the response from the F/W */
1422 if (rcode != -ERESTARTSYS) {
1423 aac_fib_complete(fibptr);
1424 aac_fib_free(fibptr);
1425 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001426 return rcode;
1427 }
1428 memcpy(&dev->adapter_info, info, sizeof(*info));
1429
1430 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001431 struct aac_supplement_adapter_info * sinfo;
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001432
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001433 aac_fib_init(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001434
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001435 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001436
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001437 memset(sinfo,0,sizeof(*sinfo));
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001438
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001439 rcode = aac_fib_send(RequestSupplementAdapterInfo,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001440 fibptr,
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001441 sizeof(*sinfo),
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001442 FsaNormal,
1443 1, 1,
1444 NULL,
1445 NULL);
1446
1447 if (rcode >= 0)
Salyzyn, Mark06a43d12008-01-08 12:32:00 -08001448 memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301449 if (rcode == -ERESTARTSYS) {
1450 fibptr = aac_fib_alloc(dev);
1451 if (!fibptr)
1452 return -ENOMEM;
1453 }
1454
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Mark Haverkamp84971732005-06-20 11:55:24 -07001457
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001458 /*
1459 * GetBusInfo
Mark Haverkamp84971732005-06-20 11:55:24 -07001460 */
1461
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001462 aac_fib_init(fibptr);
Mark Haverkamp84971732005-06-20 11:55:24 -07001463
1464 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1465
1466 memset(bus_info, 0, sizeof(*bus_info));
1467
1468 command = (struct aac_bus_info *)bus_info;
1469
1470 command->Command = cpu_to_le32(VM_Ioctl);
1471 command->ObjType = cpu_to_le32(FT_DRIVE);
1472 command->MethodId = cpu_to_le32(1);
1473 command->CtlCmd = cpu_to_le32(GetBusInfo);
1474
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001475 rcode = aac_fib_send(ContainerCommand,
Mark Haverkamp84971732005-06-20 11:55:24 -07001476 fibptr,
1477 sizeof (*bus_info),
1478 FsaNormal,
1479 1, 1,
1480 NULL, NULL);
1481
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001482 /* reasoned default */
1483 dev->maximum_num_physicals = 16;
Mark Haverkamp84971732005-06-20 11:55:24 -07001484 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1485 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1486 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1487 }
1488
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001489 if (!dev->in_reset) {
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001490 char buffer[16];
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001491 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1492 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001493 dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 dev->id,
1495 tmp>>24,
1496 (tmp>>16)&0xff,
1497 tmp&0xff,
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001498 le32_to_cpu(dev->adapter_info.kernelbuild),
1499 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1500 dev->supplement_adapter_info.BuildDate);
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001501 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1502 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 dev->name, dev->id,
1504 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1505 le32_to_cpu(dev->adapter_info.monitorbuild));
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001506 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1507 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 dev->name, dev->id,
1509 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1510 le32_to_cpu(dev->adapter_info.biosbuild));
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001511 buffer[0] = '\0';
Tony Jonesee959b02008-02-22 00:13:36 +01001512 if (aac_get_serial_number(
Salyzyn, Mark24f02e12007-06-19 16:29:24 -04001513 shost_to_class(dev->scsi_host_ptr), buffer))
1514 printk(KERN_INFO "%s%d: serial %s",
1515 dev->name, dev->id, buffer);
Salyzyn, Marka45c8632007-03-28 13:44:54 -04001516 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1517 printk(KERN_INFO "%s%d: TSID %.*s\n",
1518 dev->name, dev->id,
1519 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1520 dev->supplement_adapter_info.VpdInfo.Tsid);
1521 }
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001522 if (!aac_check_reset || ((aac_check_reset == 1) &&
Salyzyn, Marka3940da2008-01-08 12:48:25 -08001523 (dev->supplement_adapter_info.SupportedOptions2 &
1524 AAC_OPTION_IGNORE_RESET))) {
Salyzyn, Mark29c97682007-06-12 09:33:54 -04001525 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1526 dev->name, dev->id);
1527 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001530 dev->cache_protected = 0;
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08001531 dev->jbod = ((dev->supplement_adapter_info.FeatureBits &
1532 AAC_FEATURE_JBOD) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 dev->nondasd_support = 0;
1534 dev->raid_scsi_mode = 0;
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001535 if(dev->adapter_info.options & AAC_OPT_NONDASD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 dev->nondasd_support = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /*
1539 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1540 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1541 * force nondasd support on. If we decide to allow the non-dasd flag
1542 * additional changes changes will have to be made to support
1543 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1544 * changed to support the new dev->raid_scsi_mode flag instead of
1545 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1546 * function aac_detect will have to be modified where it sets up the
1547 * max number of channels based on the aac->nondasd_support flag only.
1548 */
1549 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1550 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1551 dev->nondasd_support = 1;
1552 dev->raid_scsi_mode = 1;
1553 }
1554 if (dev->raid_scsi_mode != 0)
1555 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1556 dev->name, dev->id);
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001557
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08001558 if (nondasd != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 dev->nondasd_support = (nondasd!=0);
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001560 if (dev->nondasd_support && !dev->in_reset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Yang Hongyange9304382009-04-13 14:40:14 -07001563 if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
Leubner, Achimd8e965072009-04-01 07:16:08 -07001564 dev->needs_dac = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 dev->dac_support = 0;
Leubner, Achimd8e965072009-04-01 07:16:08 -07001566 if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
1567 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001568 if (!dev->in_reset)
1569 printk(KERN_INFO "%s%d: 64bit support enabled.\n",
1570 dev->name, dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 dev->dac_support = 1;
1572 }
1573
1574 if(dacmode != -1) {
1575 dev->dac_support = (dacmode!=0);
1576 }
Leubner, Achimd8e965072009-04-01 07:16:08 -07001577
1578 /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
1579 if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
1580 & AAC_QUIRK_SCSI_32)) {
1581 dev->nondasd_support = 0;
1582 dev->jbod = 0;
1583 expose_physicals = 0;
1584 }
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if(dev->dac_support != 0) {
Yang Hongyang6a355282009-04-06 19:01:13 -07001587 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64)) &&
1588 !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
Salyzyn, Mark2f7ecc52008-02-08 08:36:23 -08001589 if (!dev->in_reset)
1590 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1591 dev->name, dev->id);
Yang Hongyang284901a2009-04-06 19:01:15 -07001592 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32)) &&
1593 !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1595 dev->name, dev->id);
1596 dev->dac_support = 0;
1597 } else {
1598 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1599 dev->name, dev->id);
1600 rcode = -ENOMEM;
1601 }
1602 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001603 /*
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001604 * Deal with configuring for the individualized limits of each packet
1605 * interface.
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001606 */
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001607 dev->a_ops.adapter_scsi = (dev->dac_support)
Salyzyn, Mark94cf6ba2007-12-13 16:14:18 -08001608 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1609 ? aac_scsi_32_64
1610 : aac_scsi_64)
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001611 : aac_scsi_32;
1612 if (dev->raw_io_interface) {
1613 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1614 ? aac_bounds_64
1615 : aac_bounds_32;
1616 dev->a_ops.adapter_read = aac_read_raw_io;
1617 dev->a_ops.adapter_write = aac_write_raw_io;
1618 } else {
1619 dev->a_ops.adapter_bounds = aac_bounds_32;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001620 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001621 sizeof(struct aac_fibhdr) -
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001622 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1623 sizeof(struct sgentry);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001624 if (dev->dac_support) {
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001625 dev->a_ops.adapter_read = aac_read_block64;
1626 dev->a_ops.adapter_write = aac_write_block64;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001627 /*
1628 * 38 scatter gather elements
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001629 */
1630 dev->scsi_host_ptr->sg_tablesize =
1631 (dev->max_fib_size -
1632 sizeof(struct aac_fibhdr) -
1633 sizeof(struct aac_write64) +
Mark Haverkamp63a70ee2005-09-20 12:57:04 -07001634 sizeof(struct sgentry64)) /
1635 sizeof(struct sgentry64);
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001636 } else {
1637 dev->a_ops.adapter_read = aac_read_block;
1638 dev->a_ops.adapter_write = aac_write_block;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001639 }
1640 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
Mahesh Rajashekharae8b12f02011-03-17 02:10:32 -07001641 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07001642 /*
1643 * Worst case size that could cause sg overflow when
1644 * we break up SG elements that are larger than 64KB.
1645 * Would be nice if we could tell the SCSI layer what
1646 * the maximum SG element size can be. Worst case is
1647 * (sg_tablesize-1) 4KB elements with one 64KB
1648 * element.
1649 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1650 */
1651 dev->scsi_host_ptr->max_sectors =
1652 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1653 }
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001654 }
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301655 /* FIB should be freed only after getting the response from the F/W */
1656 if (rcode != -ERESTARTSYS) {
1657 aac_fib_complete(fibptr);
1658 aac_fib_free(fibptr);
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 return rcode;
1662}
1663
1664
Mark Haverkampe53cb352005-08-03 15:39:09 -07001665static void io_callback(void *context, struct fib * fibptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 struct aac_dev *dev;
1668 struct aac_read_reply *readreply;
1669 struct scsi_cmnd *scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 u32 cid;
1671
1672 scsicmd = (struct scsi_cmnd *) context;
1673
Mark Haverkamp03d44332007-03-15 10:27:45 -07001674 if (!aac_valid_context(scsicmd, fibptr))
1675 return;
1676
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001677 dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001678 cid = scmd_id(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001680 if (nblank(dprintk(x))) {
1681 u64 lba;
1682 switch (scsicmd->cmnd[0]) {
1683 case WRITE_6:
1684 case READ_6:
1685 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
1686 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1687 break;
1688 case WRITE_16:
1689 case READ_16:
1690 lba = ((u64)scsicmd->cmnd[2] << 56) |
1691 ((u64)scsicmd->cmnd[3] << 48) |
1692 ((u64)scsicmd->cmnd[4] << 40) |
1693 ((u64)scsicmd->cmnd[5] << 32) |
1694 ((u64)scsicmd->cmnd[6] << 24) |
1695 (scsicmd->cmnd[7] << 16) |
1696 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1697 break;
1698 case WRITE_12:
1699 case READ_12:
1700 lba = ((u64)scsicmd->cmnd[2] << 24) |
1701 (scsicmd->cmnd[3] << 16) |
1702 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1703 break;
1704 default:
1705 lba = ((u64)scsicmd->cmnd[2] << 24) |
1706 (scsicmd->cmnd[3] << 16) |
1707 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1708 break;
1709 }
1710 printk(KERN_DEBUG
1711 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
1712 smp_processor_id(), (unsigned long long)lba, jiffies);
1713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001715 BUG_ON(fibptr == NULL);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09001716
1717 scsi_dma_unmap(scsicmd);
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 readreply = (struct aac_read_reply *)fib_data(fibptr);
Mark Salyzyn655d7222008-04-30 16:03:42 -04001720 switch (le32_to_cpu(readreply->status)) {
1721 case ST_OK:
1722 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1723 SAM_STAT_GOOD;
1724 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
1725 break;
1726 case ST_NOT_READY:
1727 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1728 SAM_STAT_CHECK_CONDITION;
1729 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
1730 SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
1731 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1732 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1733 SCSI_SENSE_BUFFERSIZE));
1734 break;
1735 default:
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001736#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkampe53cb352005-08-03 15:39:09 -07001737 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001738 le32_to_cpu(readreply->status));
1739#endif
Mark Salyzyn655d7222008-04-30 16:03:42 -04001740 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1741 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001742 set_sense(&dev->fsa_dev[cid].sense_data,
1743 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1744 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08001746 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1747 SCSI_SENSE_BUFFERSIZE));
Mark Salyzyn655d7222008-04-30 16:03:42 -04001748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001750 aac_fib_complete(fibptr);
1751 aac_fib_free(fibptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001753 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001756static int aac_read(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001758 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 u32 count;
1760 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct aac_dev *dev;
1762 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07001763 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1766 /*
1767 * Get block address and transfer length
1768 */
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001769 switch (scsicmd->cmnd[0]) {
1770 case READ_6:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001771 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001773 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001774 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 count = scsicmd->cmnd[4];
1776
1777 if (count == 0)
1778 count = 256;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001779 break;
1780 case READ_16:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001781 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001782
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001783 lba = ((u64)scsicmd->cmnd[2] << 56) |
1784 ((u64)scsicmd->cmnd[3] << 48) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001785 ((u64)scsicmd->cmnd[4] << 40) |
1786 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001787 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001788 (scsicmd->cmnd[7] << 16) |
1789 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001790 count = (scsicmd->cmnd[10] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001791 (scsicmd->cmnd[11] << 16) |
1792 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
1793 break;
1794 case READ_12:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001795 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001796
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001797 lba = ((u64)scsicmd->cmnd[2] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001798 (scsicmd->cmnd[3] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001799 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1800 count = (scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001801 (scsicmd->cmnd[7] << 16) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001802 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001803 break;
1804 default:
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001805 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001807 lba = ((u64)scsicmd->cmnd[2] << 24) |
1808 (scsicmd->cmnd[3] << 16) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001809 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001811 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07001813
1814 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1815 cid = scmd_id(scsicmd);
1816 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1817 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1818 SAM_STAT_CHECK_CONDITION;
1819 set_sense(&dev->fsa_dev[cid].sense_data,
1820 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1821 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1822 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1823 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1824 SCSI_SENSE_BUFFERSIZE));
1825 scsicmd->scsi_done(scsicmd);
1826 return 1;
1827 }
1828
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001829 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07001830 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001831 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /*
1834 * Alocate and initialize a Fib
1835 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001836 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301837 printk(KERN_WARNING "aac_read: fib allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return -1;
1839 }
1840
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001841 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 /*
1844 * Check that the command queued to the controller
1845 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001846 if (status == -EINPROGRESS) {
1847 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001849 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001850
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001851 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /*
1853 * For some reason, the Fib didn't queue, return QUEUE_FULL
1854 */
1855 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001856 scsicmd->scsi_done(scsicmd);
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001857 aac_fib_complete(cmd_fibcontext);
1858 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return 0;
1860}
1861
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001862static int aac_write(struct scsi_cmnd * scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001864 u64 lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 u32 count;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001866 int fua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 struct aac_dev *dev;
1869 struct fib * cmd_fibcontext;
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07001870 int cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1873 /*
1874 * Get block address and transfer length
1875 */
1876 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
1877 {
1878 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
1879 count = scsicmd->cmnd[4];
1880 if (count == 0)
1881 count = 256;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001882 fua = 0;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001883 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001884 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001885
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001886 lba = ((u64)scsicmd->cmnd[2] << 56) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001887 ((u64)scsicmd->cmnd[3] << 48) |
1888 ((u64)scsicmd->cmnd[4] << 40) |
1889 ((u64)scsicmd->cmnd[5] << 32) |
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08001890 ((u64)scsicmd->cmnd[6] << 24) |
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001891 (scsicmd->cmnd[7] << 16) |
1892 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
1893 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
1894 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001895 fua = scsicmd->cmnd[1] & 0x8;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001896 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001897 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001898
1899 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
1900 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
1901 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
1902 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001903 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 } else {
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07001905 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001906 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001908 fua = scsicmd->cmnd[1] & 0x8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Rajashekhara, Maheshda3cc672010-05-10 04:24:01 -07001910
1911 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
1912 cid = scmd_id(scsicmd);
1913 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1914 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1915 SAM_STAT_CHECK_CONDITION;
1916 set_sense(&dev->fsa_dev[cid].sense_data,
1917 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1918 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1919 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1920 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1921 SCSI_SENSE_BUFFERSIZE));
1922 scsicmd->scsi_done(scsicmd);
1923 return 1;
1924 }
1925
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001926 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 smp_processor_id(), (unsigned long long)lba, jiffies));
Mark Haverkampe8f32de2007-01-23 15:00:30 -08001928 if (aac_adapter_bounds(dev,scsicmd,lba))
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
1931 * Allocate and initialize a Fib then setup a BlockWrite command
1932 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001933 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Penchala Narasimha Reddy Chilakala, ERS-HCLTechcacb6dc2009-12-21 18:39:27 +05301934 /* FIB temporarily unavailable,not catastrophic failure */
1935
1936 /* scsicmd->result = DID_ERROR << 16;
1937 * scsicmd->scsi_done(scsicmd);
1938 * return 0;
1939 */
1940 printk(KERN_WARNING "aac_write: fib allocation failed\n");
1941 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04001944 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /*
1947 * Check that the command queued to the controller
1948 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08001949 if (status == -EINPROGRESS) {
1950 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return 0;
1952 }
1953
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001954 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /*
1956 * For some reason, the Fib didn't queue, return QUEUE_FULL
1957 */
1958 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07001959 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08001961 aac_fib_complete(cmd_fibcontext);
1962 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return 0;
1964}
1965
1966static void synchronize_callback(void *context, struct fib *fibptr)
1967{
1968 struct aac_synchronize_reply *synchronizereply;
1969 struct scsi_cmnd *cmd;
1970
1971 cmd = context;
1972
Mark Haverkamp03d44332007-03-15 10:27:45 -07001973 if (!aac_valid_context(cmd, fibptr))
1974 return;
1975
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001976 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 smp_processor_id(), jiffies));
1978 BUG_ON(fibptr == NULL);
1979
1980
1981 synchronizereply = fib_data(fibptr);
1982 if (le32_to_cpu(synchronizereply->status) == CT_OK)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001983 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1985 else {
1986 struct scsi_device *sdev = cmd->device;
Salyzyn, Mark1a655042007-06-11 16:17:55 -04001987 struct aac_dev *dev = fibptr->dev;
Mark Haverkampe5718772006-03-27 09:43:25 -08001988 u32 cid = sdev_id(sdev);
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001989 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 "synchronize_callback: synchronize failed, status = %d\n",
1991 le32_to_cpu(synchronizereply->status));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04001992 cmd->result = DID_OK << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08001994 set_sense(&dev->fsa_dev[cid].sense_data,
1995 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1996 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
FUJITA Tomonorib80ca4f2008-01-13 15:46:13 +09001998 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1999 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
2001
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002002 aac_fib_complete(fibptr);
2003 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07002004 cmd->scsi_done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002007static int aac_synchronize(struct scsi_cmnd *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 int status;
2010 struct fib *cmd_fibcontext;
2011 struct aac_synchronize *synchronizecmd;
2012 struct scsi_cmnd *cmd;
2013 struct scsi_device *sdev = scsicmd->device;
2014 int active = 0;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002015 struct aac_dev *aac;
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002016 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2017 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2018 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 unsigned long flags;
2020
2021 /*
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002022 * Wait for all outstanding queued commands to complete to this
2023 * specific target (block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 */
2025 spin_lock_irqsave(&sdev->list_lock, flags);
2026 list_for_each_entry(cmd, &sdev->cmd_list, list)
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002027 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2028 u64 cmnd_lba;
2029 u32 cmnd_count;
2030
2031 if (cmd->cmnd[0] == WRITE_6) {
2032 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2033 (cmd->cmnd[2] << 8) |
2034 cmd->cmnd[3];
2035 cmnd_count = cmd->cmnd[4];
2036 if (cmnd_count == 0)
2037 cmnd_count = 256;
2038 } else if (cmd->cmnd[0] == WRITE_16) {
2039 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2040 ((u64)cmd->cmnd[3] << 48) |
2041 ((u64)cmd->cmnd[4] << 40) |
2042 ((u64)cmd->cmnd[5] << 32) |
2043 ((u64)cmd->cmnd[6] << 24) |
2044 (cmd->cmnd[7] << 16) |
2045 (cmd->cmnd[8] << 8) |
2046 cmd->cmnd[9];
2047 cmnd_count = (cmd->cmnd[10] << 24) |
2048 (cmd->cmnd[11] << 16) |
2049 (cmd->cmnd[12] << 8) |
2050 cmd->cmnd[13];
2051 } else if (cmd->cmnd[0] == WRITE_12) {
2052 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2053 (cmd->cmnd[3] << 16) |
2054 (cmd->cmnd[4] << 8) |
2055 cmd->cmnd[5];
2056 cmnd_count = (cmd->cmnd[6] << 24) |
2057 (cmd->cmnd[7] << 16) |
2058 (cmd->cmnd[8] << 8) |
2059 cmd->cmnd[9];
2060 } else if (cmd->cmnd[0] == WRITE_10) {
2061 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2062 (cmd->cmnd[3] << 16) |
2063 (cmd->cmnd[4] << 8) |
2064 cmd->cmnd[5];
2065 cmnd_count = (cmd->cmnd[7] << 8) |
2066 cmd->cmnd[8];
2067 } else
2068 continue;
2069 if (((cmnd_lba + cmnd_count) < lba) ||
2070 (count && ((lba + count) < cmnd_lba)))
2071 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 ++active;
2073 break;
2074 }
2075
2076 spin_unlock_irqrestore(&sdev->list_lock, flags);
2077
2078 /*
2079 * Yield the processor (requeue for later)
2080 */
2081 if (active)
2082 return SCSI_MLQUEUE_DEVICE_BUSY;
2083
Salyzyn, Markf8583172007-10-30 15:50:49 -04002084 aac = (struct aac_dev *)sdev->host->hostdata;
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002085 if (aac->in_reset)
2086 return SCSI_MLQUEUE_HOST_BUSY;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002089 * Allocate and initialize a Fib
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002091 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return SCSI_MLQUEUE_HOST_BUSY;
2093
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002094 aac_fib_init(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 synchronizecmd = fib_data(cmd_fibcontext);
2097 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2098 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002099 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002100 synchronizecmd->count =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2102
2103 /*
2104 * Now send the Fib to the adapter
2105 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002106 status = aac_fib_send(ContainerCommand,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 cmd_fibcontext,
2108 sizeof(struct aac_synchronize),
2109 FsaNormal,
2110 0, 1,
2111 (fib_callback)synchronize_callback,
2112 (void *)scsicmd);
2113
2114 /*
2115 * Check that the command queued to the controller
2116 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002117 if (status == -EINPROGRESS) {
2118 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return 0;
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Salyzyn, Markb90f90d2007-07-27 09:48:49 -04002122 printk(KERN_WARNING
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002123 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2124 aac_fib_complete(cmd_fibcontext);
2125 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return SCSI_MLQUEUE_HOST_BUSY;
2127}
2128
Mark Salyzyn655d7222008-04-30 16:03:42 -04002129static void aac_start_stop_callback(void *context, struct fib *fibptr)
2130{
2131 struct scsi_cmnd *scsicmd = context;
2132
2133 if (!aac_valid_context(scsicmd, fibptr))
2134 return;
2135
2136 BUG_ON(fibptr == NULL);
2137
2138 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2139
2140 aac_fib_complete(fibptr);
2141 aac_fib_free(fibptr);
2142 scsicmd->scsi_done(scsicmd);
2143}
2144
2145static int aac_start_stop(struct scsi_cmnd *scsicmd)
2146{
2147 int status;
2148 struct fib *cmd_fibcontext;
2149 struct aac_power_management *pmcmd;
2150 struct scsi_device *sdev = scsicmd->device;
2151 struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2152
2153 if (!(aac->supplement_adapter_info.SupportedOptions2 &
2154 AAC_OPTION_POWER_MANAGEMENT)) {
2155 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2156 SAM_STAT_GOOD;
2157 scsicmd->scsi_done(scsicmd);
2158 return 0;
2159 }
2160
2161 if (aac->in_reset)
2162 return SCSI_MLQUEUE_HOST_BUSY;
2163
2164 /*
2165 * Allocate and initialize a Fib
2166 */
2167 cmd_fibcontext = aac_fib_alloc(aac);
2168 if (!cmd_fibcontext)
2169 return SCSI_MLQUEUE_HOST_BUSY;
2170
2171 aac_fib_init(cmd_fibcontext);
2172
2173 pmcmd = fib_data(cmd_fibcontext);
2174 pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2175 pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2176 /* Eject bit ignored, not relevant */
2177 pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2178 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2179 pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2180 pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2181 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2182
2183 /*
2184 * Now send the Fib to the adapter
2185 */
2186 status = aac_fib_send(ContainerCommand,
2187 cmd_fibcontext,
2188 sizeof(struct aac_power_management),
2189 FsaNormal,
2190 0, 1,
2191 (fib_callback)aac_start_stop_callback,
2192 (void *)scsicmd);
2193
2194 /*
2195 * Check that the command queued to the controller
2196 */
2197 if (status == -EINPROGRESS) {
2198 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2199 return 0;
2200 }
2201
2202 aac_fib_complete(cmd_fibcontext);
2203 aac_fib_free(cmd_fibcontext);
2204 return SCSI_MLQUEUE_HOST_BUSY;
2205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/**
2208 * aac_scsi_cmd() - Process SCSI command
2209 * @scsicmd: SCSI command block
2210 *
2211 * Emulate a SCSI command and queue the required request for the
2212 * aacraid firmware.
2213 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2216{
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002217 u32 cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct Scsi_Host *host = scsicmd->device->host;
2219 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2220 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002221
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002222 if (fsa_dev_ptr == NULL)
2223 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /*
2225 * If the bus, id or lun is out of range, return fail
2226 * Test does not apply to ID 16, the pseudo id for the controller
2227 * itself.
2228 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002229 cid = scmd_id(scsicmd);
2230 if (cid != host->this_id) {
2231 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2232 if((cid >= dev->maximum_num_containers) ||
Mark Haverkampe5718772006-03-27 09:43:25 -08002233 (scsicmd->device->lun != 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 scsicmd->result = DID_NO_CONNECT << 16;
2235 scsicmd->scsi_done(scsicmd);
2236 return 0;
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /*
2240 * If the target container doesn't exist, it may have
2241 * been newly created
2242 */
Mark Salyzyn655d7222008-04-30 16:03:42 -04002243 if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2244 (fsa_dev_ptr[cid].sense_data.sense_key ==
2245 NOT_READY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 switch (scsicmd->cmnd[0]) {
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002247 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002248 if (!(dev->raw_io_interface) ||
2249 !(dev->raw_io_64) ||
2250 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 case INQUIRY:
2253 case READ_CAPACITY:
2254 case TEST_UNIT_READY:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002255 if (dev->in_reset)
2256 return -1;
Mark Haverkampfe76df42007-03-15 12:55:07 -07002257 return _aac_probe_container(scsicmd,
2258 aac_probe_container_callback2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 default:
2260 break;
2261 }
2262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 } else { /* check for physical non-dasd devices */
Salyzyn, Markcb1042f2008-01-17 09:25:07 -08002264 if (dev->nondasd_support || expose_physicals ||
2265 dev->jbod) {
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002266 if (dev->in_reset)
2267 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return aac_send_srb_fib(scsicmd);
2269 } else {
2270 scsicmd->result = DID_NO_CONNECT << 16;
2271 scsicmd->scsi_done(scsicmd);
2272 return 0;
2273 }
2274 }
2275 }
2276 /*
2277 * else Command for the controller itself
2278 */
2279 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002280 (scsicmd->cmnd[0] != TEST_UNIT_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 {
2282 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2283 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002284 set_sense(&dev->fsa_dev[cid].sense_data,
2285 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2286 ASENCODE_INVALID_COMMAND, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002288 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2289 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 scsicmd->scsi_done(scsicmd);
2291 return 0;
2292 }
2293
2294
2295 /* Handle commands here that don't really require going out to the adapter */
2296 switch (scsicmd->cmnd[0]) {
2297 case INQUIRY:
2298 {
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002299 struct inquiry_data inq_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002301 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002302 memset(&inq_data, 0, sizeof (struct inquiry_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Leubner, Achimd8e965072009-04-01 07:16:08 -07002304 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002305 char *arr = (char *)&inq_data;
2306
2307 /* EVPD bit set */
2308 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2309 INQD_PDT_PROC : INQD_PDT_DA;
2310 if (scsicmd->cmnd[2] == 0) {
2311 /* supported vital product data pages */
2312 arr[3] = 2;
2313 arr[4] = 0x0;
2314 arr[5] = 0x80;
2315 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002316 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2317 sizeof(inq_data));
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002318 scsicmd->result = DID_OK << 16 |
2319 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2320 } else if (scsicmd->cmnd[2] == 0x80) {
2321 /* unit serial number page */
2322 arr[3] = setinqserial(dev, &arr[4],
2323 scmd_id(scsicmd));
2324 arr[1] = scsicmd->cmnd[2];
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002325 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2326 sizeof(inq_data));
Leubner, Achimd8e965072009-04-01 07:16:08 -07002327 if (aac_wwn != 2)
2328 return aac_get_container_serial(
2329 scsicmd);
2330 /* SLES 10 SP1 special */
2331 scsicmd->result = DID_OK << 16 |
2332 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002333 } else {
2334 /* vpd page not implemented */
2335 scsicmd->result = DID_OK << 16 |
2336 COMMAND_COMPLETE << 8 |
2337 SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002338 set_sense(&dev->fsa_dev[cid].sense_data,
2339 ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2340 ASENCODE_NO_SENSE, 7, 2);
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002341 memcpy(scsicmd->sense_buffer,
2342 &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002343 min_t(size_t,
2344 sizeof(dev->fsa_dev[cid].sense_data),
2345 SCSI_SENSE_BUFFERSIZE));
Salyzyn, Mark88e2f982007-07-17 14:01:28 -04002346 }
2347 scsicmd->scsi_done(scsicmd);
2348 return 0;
2349 }
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002350 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002351 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 */
2352 inq_data.inqd_len = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002354 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /*
2356 * Set the Vendor, Product, and Revision Level
2357 * see: <vendor>.c i.e. aac.c
2358 */
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002359 if (cid == host->this_id) {
Tobias Klauser6391a112006-06-08 22:23:48 -07002360 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002361 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002362 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2363 sizeof(inq_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2365 scsicmd->scsi_done(scsicmd);
2366 return 0;
2367 }
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002368 if (dev->in_reset)
2369 return -1;
Mark Haverkamp794d0602005-10-24 10:51:53 -07002370 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002371 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002372 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002373 return aac_get_container_name(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
Hannes Reineckeeb846d92014-11-17 14:25:19 +01002375 case SERVICE_ACTION_IN_16:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002376 if (!(dev->raw_io_interface) ||
2377 !(dev->raw_io_64) ||
2378 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2379 break;
2380 {
2381 u64 capacity;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002382 char cp[13];
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002383 unsigned int alloc_len;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002384
2385 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2386 capacity = fsa_dev_ptr[cid].size - 1;
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002387 cp[0] = (capacity >> 56) & 0xff;
2388 cp[1] = (capacity >> 48) & 0xff;
2389 cp[2] = (capacity >> 40) & 0xff;
2390 cp[3] = (capacity >> 32) & 0xff;
2391 cp[4] = (capacity >> 24) & 0xff;
2392 cp[5] = (capacity >> 16) & 0xff;
2393 cp[6] = (capacity >> 8) & 0xff;
2394 cp[7] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002395 cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2396 cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2397 cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2398 cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002399 cp[12] = 0;
Mark Haverkamp07ce5eb2005-11-08 14:26:33 -08002400
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002401 alloc_len = ((scsicmd->cmnd[10] << 24)
2402 + (scsicmd->cmnd[11] << 16)
2403 + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2404
2405 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002406 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
FUJITA Tomonorib271f1c2008-02-24 15:25:23 -08002407 if (alloc_len < scsi_bufflen(scsicmd))
2408 scsi_set_resid(scsicmd,
2409 scsi_bufflen(scsicmd) - alloc_len);
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002410
2411 /* Do not cache partition table for arrays */
2412 scsicmd->device->removable = 1;
2413
2414 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2415 scsicmd->scsi_done(scsicmd);
2416
2417 return 0;
2418 }
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 case READ_CAPACITY:
2421 {
2422 u32 capacity;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002423 char cp[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002426 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 capacity = fsa_dev_ptr[cid].size - 1;
2428 else
2429 capacity = (u32)-1;
Mark Haverkamp3b2946c2005-08-15 10:50:24 -07002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 cp[0] = (capacity >> 24) & 0xff;
2432 cp[1] = (capacity >> 16) & 0xff;
2433 cp[2] = (capacity >> 8) & 0xff;
2434 cp[3] = (capacity >> 0) & 0xff;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002435 cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2436 cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2437 cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2438 cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
FUJITA Tomonorid4345022008-03-09 13:44:34 +09002439 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002440 /* Do not cache partition table for arrays */
2441 scsicmd->device->removable = 1;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002442 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2443 SAM_STAT_GOOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 scsicmd->scsi_done(scsicmd);
2445
2446 return 0;
2447 }
2448
2449 case MODE_SENSE:
2450 {
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002451 int mode_buf_length = 4;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002452 u32 capacity;
2453 aac_modep_data mpd;
2454
2455 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2456 capacity = fsa_dev_ptr[cid].size - 1;
2457 else
2458 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002461 memset((char *)&mpd, 0, sizeof(aac_modep_data));
2462
2463 /* Mode data length */
2464 mpd.hd.data_length = sizeof(mpd.hd) - 1;
2465 /* Medium type - default */
2466 mpd.hd.med_type = 0;
2467 /* Device-specific param,
2468 bit 8: 0/1 = write enabled/protected
2469 bit 4: 0/1 = FUA enabled */
2470 mpd.hd.dev_par = 0;
2471
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002472 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002473 mpd.hd.dev_par = 0x10;
2474 if (scsicmd->cmnd[1] & 0x8)
2475 mpd.hd.bd_length = 0; /* Block descriptor length */
2476 else {
2477 mpd.hd.bd_length = sizeof(mpd.bd);
2478 mpd.hd.data_length += mpd.hd.bd_length;
2479 mpd.bd.block_length[0] =
2480 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2481 mpd.bd.block_length[1] =
2482 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2483 mpd.bd.block_length[2] =
2484 fsa_dev_ptr[cid].block_size & 0xff;
2485 if (capacity > 0xffffff) {
2486 mpd.bd.block_count[0] = 0xff;
2487 mpd.bd.block_count[1] = 0xff;
2488 mpd.bd.block_count[2] = 0xff;
2489 } else {
2490 mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
2491 mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
2492 mpd.bd.block_count[2] = capacity & 0xff;
2493 }
2494 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002495 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2496 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002497 mpd.hd.data_length += 3;
2498 mpd.mpc_buf[0] = 8;
2499 mpd.mpc_buf[1] = 1;
2500 mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002501 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002502 mode_buf_length = sizeof(mpd);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002503 if (mode_buf_length > scsicmd->cmnd[4])
2504 mode_buf_length = scsicmd->cmnd[4];
2505 }
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002506 scsi_sg_copy_from_buffer(scsicmd,
2507 (char *)&mpd,
2508 mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2510 scsicmd->scsi_done(scsicmd);
2511
2512 return 0;
2513 }
2514 case MODE_SENSE_10:
2515 {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002516 u32 capacity;
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002517 int mode_buf_length = 8;
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002518 aac_modep10_data mpd10;
2519
2520 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2521 capacity = fsa_dev_ptr[cid].size - 1;
2522 else
2523 capacity = (u32)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002526 memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
2527 /* Mode data length (MSB) */
2528 mpd10.hd.data_length[0] = 0;
2529 /* Mode data length (LSB) */
2530 mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
2531 /* Medium type - default */
2532 mpd10.hd.med_type = 0;
2533 /* Device-specific param,
2534 bit 8: 0/1 = write enabled/protected
2535 bit 4: 0/1 = FUA enabled */
2536 mpd10.hd.dev_par = 0;
2537
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002538 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002539 mpd10.hd.dev_par = 0x10;
2540 mpd10.hd.rsrvd[0] = 0; /* reserved */
2541 mpd10.hd.rsrvd[1] = 0; /* reserved */
2542 if (scsicmd->cmnd[1] & 0x8) {
2543 /* Block descriptor length (MSB) */
2544 mpd10.hd.bd_length[0] = 0;
2545 /* Block descriptor length (LSB) */
2546 mpd10.hd.bd_length[1] = 0;
2547 } else {
2548 mpd10.hd.bd_length[0] = 0;
2549 mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
2550
2551 mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
2552
2553 mpd10.bd.block_length[0] =
2554 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2555 mpd10.bd.block_length[1] =
2556 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2557 mpd10.bd.block_length[2] =
2558 fsa_dev_ptr[cid].block_size & 0xff;
2559
2560 if (capacity > 0xffffff) {
2561 mpd10.bd.block_count[0] = 0xff;
2562 mpd10.bd.block_count[1] = 0xff;
2563 mpd10.bd.block_count[2] = 0xff;
2564 } else {
2565 mpd10.bd.block_count[0] =
2566 (capacity >> 16) & 0xff;
2567 mpd10.bd.block_count[1] =
2568 (capacity >> 8) & 0xff;
2569 mpd10.bd.block_count[2] =
2570 capacity & 0xff;
2571 }
2572 }
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002573 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2574 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002575 mpd10.hd.data_length[1] += 3;
2576 mpd10.mpc_buf[0] = 8;
2577 mpd10.mpc_buf[1] = 1;
2578 mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002579 ? 0 : 0x04; /* WCE */
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002580 mode_buf_length = sizeof(mpd10);
Salyzyn, Mark9d399cc2007-05-18 14:51:34 -04002581 if (mode_buf_length > scsicmd->cmnd[8])
2582 mode_buf_length = scsicmd->cmnd[8];
2583 }
Mahesh Rajashekharab8364392015-03-26 10:41:24 -04002584 scsi_sg_copy_from_buffer(scsicmd,
2585 (char *)&mpd10,
2586 mode_buf_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2589 scsicmd->scsi_done(scsicmd);
2590
2591 return 0;
2592 }
2593 case REQUEST_SENSE:
2594 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2595 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
2596 memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
2597 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2598 scsicmd->scsi_done(scsicmd);
2599 return 0;
2600
2601 case ALLOW_MEDIUM_REMOVAL:
2602 dprintk((KERN_DEBUG "LOCK command.\n"));
2603 if (scsicmd->cmnd[4])
2604 fsa_dev_ptr[cid].locked = 1;
2605 else
2606 fsa_dev_ptr[cid].locked = 0;
2607
2608 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2609 scsicmd->scsi_done(scsicmd);
2610 return 0;
2611 /*
2612 * These commands are all No-Ops
2613 */
2614 case TEST_UNIT_READY:
Mark Salyzyn655d7222008-04-30 16:03:42 -04002615 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
2616 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2617 SAM_STAT_CHECK_CONDITION;
2618 set_sense(&dev->fsa_dev[cid].sense_data,
2619 NOT_READY, SENCODE_BECOMING_READY,
2620 ASENCODE_BECOMING_READY, 0, 0);
2621 memcpy(scsicmd->sense_buffer,
2622 &dev->fsa_dev[cid].sense_data,
2623 min_t(size_t,
2624 sizeof(dev->fsa_dev[cid].sense_data),
2625 SCSI_SENSE_BUFFERSIZE));
2626 scsicmd->scsi_done(scsicmd);
2627 return 0;
2628 }
2629 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 case RESERVE:
2631 case RELEASE:
2632 case REZERO_UNIT:
2633 case REASSIGN_BLOCKS:
2634 case SEEK_10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2636 scsicmd->scsi_done(scsicmd);
2637 return 0;
Mark Salyzyn655d7222008-04-30 16:03:42 -04002638
2639 case START_STOP:
2640 return aac_start_stop(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 }
2642
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002643 switch (scsicmd->cmnd[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 {
2645 case READ_6:
2646 case READ_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002647 case READ_12:
2648 case READ_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002649 if (dev->in_reset)
2650 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /*
2652 * Hack to keep track of ordinal number of the device that
2653 * corresponds to a container. Needed to convert
2654 * containers to /dev/sd device names
2655 */
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002656
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002657 if (scsicmd->request->rq_disk)
2658 strlcpy(fsa_dev_ptr[cid].devname,
2659 scsicmd->request->rq_disk->disk_name,
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002660 min(sizeof(fsa_dev_ptr[cid].devname),
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002661 sizeof(scsicmd->request->rq_disk->disk_name) + 1));
Mark Haverkamp77d644d2006-03-27 09:43:40 -08002662
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002663 return aac_read(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 case WRITE_6:
2666 case WRITE_10:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002667 case WRITE_12:
2668 case WRITE_16:
Mark Haverkamp8c867b22006-08-03 08:03:30 -07002669 if (dev->in_reset)
2670 return -1;
Mark Haverkamp9e7c3492007-03-15 10:26:55 -07002671 return aac_write(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 case SYNCHRONIZE_CACHE:
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002674 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2675 scsicmd->result = DID_OK << 16 |
2676 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2677 scsicmd->scsi_done(scsicmd);
2678 return 0;
2679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /* Issue FIB to tell Firmware to flush it's cache */
Salyzyn, Mark95e852e2008-01-08 12:01:07 -08002681 if ((aac_cache & 6) != 2)
2682 return aac_synchronize(scsicmd);
2683 /* FALLTHRU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 default:
2685 /*
2686 * Unhandled commands
2687 */
Mark Haverkamp 7c00ffa2005-05-16 18:28:42 -07002688 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark8e31e602008-02-06 13:54:12 -08002690 set_sense(&dev->fsa_dev[cid].sense_data,
2691 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2692 ASENCODE_INVALID_COMMAND, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002694 min_t(size_t,
2695 sizeof(dev->fsa_dev[cid].sense_data),
2696 SCSI_SENSE_BUFFERSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 scsicmd->scsi_done(scsicmd);
2698 return 0;
2699 }
2700}
2701
2702static int query_disk(struct aac_dev *dev, void __user *arg)
2703{
2704 struct aac_query_disk qd;
2705 struct fsa_dev_info *fsa_dev_ptr;
2706
2707 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002708 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07002709 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
2711 return -EFAULT;
2712 if (qd.cnum == -1)
Mark Haverkampe5718772006-03-27 09:43:25 -08002713 qd.cnum = qd.id;
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002714 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 {
2716 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
2717 return -EINVAL;
2718 qd.instance = dev->scsi_host_ptr->host_no;
2719 qd.bus = 0;
2720 qd.id = CONTAINER_TO_ID(qd.cnum);
2721 qd.lun = CONTAINER_TO_LUN(qd.cnum);
2722 }
2723 else return -EINVAL;
2724
Salyzyn, Markfd622b12007-07-17 10:59:19 -04002725 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 qd.locked = fsa_dev_ptr[qd.cnum].locked;
2727 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
2728
2729 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
2730 qd.unmapped = 1;
2731 else
2732 qd.unmapped = 0;
2733
2734 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
2735 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
2736
2737 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
2738 return -EFAULT;
2739 return 0;
2740}
2741
2742static int force_delete_disk(struct aac_dev *dev, void __user *arg)
2743{
2744 struct aac_delete_disk dd;
2745 struct fsa_dev_info *fsa_dev_ptr;
2746
2747 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp65101352006-09-19 08:59:23 -07002748 if (!fsa_dev_ptr)
2749 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2752 return -EFAULT;
2753
2754 if (dd.cnum >= dev->maximum_num_containers)
2755 return -EINVAL;
2756 /*
2757 * Mark this container as being deleted.
2758 */
2759 fsa_dev_ptr[dd.cnum].deleted = 1;
2760 /*
2761 * Mark the container as no longer valid
2762 */
2763 fsa_dev_ptr[dd.cnum].valid = 0;
2764 return 0;
2765}
2766
2767static int delete_disk(struct aac_dev *dev, void __user *arg)
2768{
2769 struct aac_delete_disk dd;
2770 struct fsa_dev_info *fsa_dev_ptr;
2771
2772 fsa_dev_ptr = dev->fsa_dev;
Mark Haverkamp90ee3462006-08-03 08:03:07 -07002773 if (!fsa_dev_ptr)
Mark Haverkamp65101352006-09-19 08:59:23 -07002774 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
2777 return -EFAULT;
2778
2779 if (dd.cnum >= dev->maximum_num_containers)
2780 return -EINVAL;
2781 /*
2782 * If the container is locked, it can not be deleted by the API.
2783 */
2784 if (fsa_dev_ptr[dd.cnum].locked)
2785 return -EBUSY;
2786 else {
2787 /*
2788 * Mark the container as no longer being valid.
2789 */
2790 fsa_dev_ptr[dd.cnum].valid = 0;
2791 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
2792 return 0;
2793 }
2794}
2795
2796int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
2797{
2798 switch (cmd) {
2799 case FSACTL_QUERY_DISK:
2800 return query_disk(dev, arg);
2801 case FSACTL_DELETE_DISK:
2802 return delete_disk(dev, arg);
2803 case FSACTL_FORCE_DELETE_DISK:
2804 return force_delete_disk(dev, arg);
2805 case FSACTL_GET_CONTAINERS:
2806 return aac_get_containers(dev);
2807 default:
2808 return -ENOTTY;
2809 }
2810}
2811
2812/**
2813 *
2814 * aac_srb_callback
2815 * @context: the context set in the fib - here it is scsi cmd
2816 * @fibptr: pointer to the fib
2817 *
2818 * Handles the completion of a scsi command to a non dasd device
2819 *
2820 */
2821
2822static void aac_srb_callback(void *context, struct fib * fibptr)
2823{
2824 struct aac_dev *dev;
2825 struct aac_srb_reply *srbreply;
2826 struct scsi_cmnd *scsicmd;
2827
2828 scsicmd = (struct scsi_cmnd *) context;
Mark Haverkamp03d44332007-03-15 10:27:45 -07002829
2830 if (!aac_valid_context(scsicmd, fibptr))
2831 return;
2832
Eric Sesterhenn125e1872006-06-23 02:06:06 -07002833 BUG_ON(fibptr == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Salyzyn, Mark1a655042007-06-11 16:17:55 -04002835 dev = fibptr->dev;
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
2838
2839 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05302841 if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
2842 /* fast response */
2843 srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
2844 srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
2845 } else {
2846 /*
2847 * Calculate resid for sg
2848 */
2849 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
2850 - le32_to_cpu(srbreply->data_xfer_length));
2851 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09002852
2853 scsi_dma_unmap(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Rajashekhara, Maheshe3cc2682010-05-10 04:12:28 -07002855 /* expose physical device if expose_physicald flag is on */
2856 if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
2857 && expose_physicals > 0)
2858 aac_expose_phy_device(scsicmd);
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 /*
2861 * First check the fib status
2862 */
2863
2864 if (le32_to_cpu(srbreply->status) != ST_OK){
2865 int len;
2866 printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002867 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
2868 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2870 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
2871 }
2872
2873 /*
2874 * Next check the srb status
2875 */
2876 switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
2877 case SRB_STATUS_ERROR_RECOVERY:
2878 case SRB_STATUS_PENDING:
2879 case SRB_STATUS_SUCCESS:
Mark Haverkampbb08f922006-02-01 09:30:44 -08002880 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 break;
2882 case SRB_STATUS_DATA_OVERRUN:
2883 switch(scsicmd->cmnd[0]){
2884 case READ_6:
2885 case WRITE_6:
2886 case READ_10:
2887 case WRITE_10:
2888 case READ_12:
2889 case WRITE_12:
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002890 case READ_16:
2891 case WRITE_16:
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002892 if (le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
2894 } else {
2895 printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
2896 }
2897 scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
2898 break;
2899 case INQUIRY: {
Mark Haverkampbb08f922006-02-01 09:30:44 -08002900 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 break;
2902 }
2903 default:
2904 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
2905 break;
2906 }
2907 break;
2908 case SRB_STATUS_ABORTED:
2909 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
2910 break;
2911 case SRB_STATUS_ABORT_FAILED:
2912 // Not sure about this one - but assuming the hba was trying to abort for some reason
2913 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
2914 break;
2915 case SRB_STATUS_PARITY_ERROR:
2916 scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
2917 break;
2918 case SRB_STATUS_NO_DEVICE:
2919 case SRB_STATUS_INVALID_PATH_ID:
2920 case SRB_STATUS_INVALID_TARGET_ID:
2921 case SRB_STATUS_INVALID_LUN:
2922 case SRB_STATUS_SELECTION_TIMEOUT:
2923 scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
2924 break;
2925
2926 case SRB_STATUS_COMMAND_TIMEOUT:
2927 case SRB_STATUS_TIMEOUT:
2928 scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
2929 break;
2930
2931 case SRB_STATUS_BUSY:
Salyzyn, Mark760af102007-06-19 11:41:21 -04002932 scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 break;
2934
2935 case SRB_STATUS_BUS_RESET:
2936 scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
2937 break;
2938
2939 case SRB_STATUS_MESSAGE_REJECTED:
2940 scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
2941 break;
2942 case SRB_STATUS_REQUEST_FLUSHED:
2943 case SRB_STATUS_ERROR:
2944 case SRB_STATUS_INVALID_REQUEST:
2945 case SRB_STATUS_REQUEST_SENSE_FAILED:
2946 case SRB_STATUS_NO_HBA:
2947 case SRB_STATUS_UNEXPECTED_BUS_FREE:
2948 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
2949 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
2950 case SRB_STATUS_DELAYED_RETRY:
2951 case SRB_STATUS_BAD_FUNCTION:
2952 case SRB_STATUS_NOT_STARTED:
2953 case SRB_STATUS_NOT_IN_USE:
2954 case SRB_STATUS_FORCE_ABORT:
2955 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
2956 default:
2957#ifdef AAC_DETAILED_STATUS_INFO
2958 printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
2959 le32_to_cpu(srbreply->srb_status) & 0x3F,
2960 aac_get_status_string(
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002961 le32_to_cpu(srbreply->srb_status) & 0x3F),
2962 scsicmd->cmnd[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 le32_to_cpu(srbreply->scsi_status));
2964#endif
Rajashekhara, Mahesh1fc80102010-05-10 04:17:57 -07002965 if ((scsicmd->cmnd[0] == ATA_12)
2966 || (scsicmd->cmnd[0] == ATA_16)) {
2967 if (scsicmd->cmnd[2] & (0x01 << 5)) {
2968 scsicmd->result = DID_OK << 16
2969 | COMMAND_COMPLETE << 8;
2970 break;
2971 } else {
2972 scsicmd->result = DID_ERROR << 16
2973 | COMMAND_COMPLETE << 8;
2974 break;
2975 }
2976 } else {
2977 scsicmd->result = DID_ERROR << 16
2978 | COMMAND_COMPLETE << 8;
2979 break;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08002982 if (le32_to_cpu(srbreply->scsi_status) == SAM_STAT_CHECK_CONDITION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 int len;
2984 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
Salyzyn, Mark3ace4262008-01-14 07:25:33 -08002985 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
2986 SCSI_SENSE_BUFFERSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987#ifdef AAC_DETAILED_STATUS_INFO
Mark Haverkamp7a8cf292005-09-22 09:15:24 -07002988 printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
2989 le32_to_cpu(srbreply->status), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990#endif
2991 memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
2993 /*
2994 * OR in the scsi status (already shifted up a bit)
2995 */
2996 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
2997
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08002998 aac_fib_complete(fibptr);
2999 aac_fib_free(fibptr);
Mark Haverkamp8e0c5eb2005-10-24 10:52:22 -07003000 scsicmd->scsi_done(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
3003/**
3004 *
3005 * aac_send_scb_fib
3006 * @scsicmd: the scsi command block
3007 *
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08003008 * This routine will form a FIB and fill in the aac_srb from the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 * scsicmd passed in.
3010 */
3011
3012static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3013{
3014 struct fib* cmd_fibcontext;
3015 struct aac_dev* dev;
3016 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Mark Haverkamp84971732005-06-20 11:55:24 -07003018 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
Mark Haverkampe5718772006-03-27 09:43:25 -08003019 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
Mark Haverkamp84971732005-06-20 11:55:24 -07003020 scsicmd->device->lun > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 scsicmd->result = DID_NO_CONNECT << 16;
3022 scsicmd->scsi_done(scsicmd);
3023 return 0;
3024 }
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 /*
3027 * Allocate and initialize a Fib then setup a BlockWrite command
3028 */
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003029 if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return -1;
3031 }
Mark Haverkampe8f32de2007-01-23 15:00:30 -08003032 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /*
3035 * Check that the command queued to the controller
3036 */
Mark Haverkamp77d644d2006-03-27 09:43:40 -08003037 if (status == -EINPROGRESS) {
3038 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return 0;
3040 }
3041
Mark Haverkampbfb35aa82006-02-01 09:30:55 -08003042 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3043 aac_fib_complete(cmd_fibcontext);
3044 aac_fib_free(cmd_fibcontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 return -1;
3047}
3048
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303049static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
3051 struct aac_dev *dev;
3052 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003053 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3056 // Get rid of old data
3057 psg->count = 0;
3058 psg->sg[0].addr = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003059 psg->sg[0].count = 0;
3060
3061 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303062 if (nseg < 0)
3063 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003064 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 struct scatterlist *sg;
3066 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003068 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003070 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3072 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3073 byte_count += sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003076 if (byte_count > scsi_bufflen(scsicmd)) {
3077 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3078 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003079 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003080 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
3082 /* Check for command underflow */
3083 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3084 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3085 byte_count, scsicmd->underflow);
3086 }
3087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return byte_count;
3089}
3090
3091
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303092static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
3094 struct aac_dev *dev;
3095 unsigned long byte_count = 0;
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003096 u64 addr;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003097 int nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3100 // Get rid of old data
3101 psg->count = 0;
3102 psg->sg[0].addr[0] = 0;
3103 psg->sg[0].addr[1] = 0;
3104 psg->sg[0].count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003105
3106 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303107 if (nseg < 0)
3108 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003109 if (nseg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 struct scatterlist *sg;
3111 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003113 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp1241f352006-03-27 09:44:23 -08003114 int count = sg_dma_len(sg);
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003115 addr = sg_dma_address(sg);
3116 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3117 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
Mark Haverkamp1241f352006-03-27 09:44:23 -08003118 psg->sg[i].count = cpu_to_le32(count);
3119 byte_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003121 psg->count = cpu_to_le32(nseg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003123 if (byte_count > scsi_bufflen(scsicmd)) {
3124 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3125 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp 56b58712005-04-27 06:05:51 -07003126 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003127 byte_count = scsi_bufflen(scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
3129 /* Check for command underflow */
3130 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3131 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3132 byte_count, scsicmd->underflow);
3133 }
3134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return byte_count;
3136}
3137
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303138static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003139{
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003140 unsigned long byte_count = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003141 int nseg;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003142
3143 // Get rid of old data
3144 psg->count = 0;
3145 psg->sg[0].next = 0;
3146 psg->sg[0].prev = 0;
3147 psg->sg[0].addr[0] = 0;
3148 psg->sg[0].addr[1] = 0;
3149 psg->sg[0].count = 0;
3150 psg->sg[0].flags = 0;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003151
3152 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303153 if (nseg < 0)
3154 return nseg;
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003155 if (nseg) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003156 struct scatterlist *sg;
3157 int i;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003158
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003159 scsi_for_each_sg(scsicmd, sg, nseg, i) {
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003160 int count = sg_dma_len(sg);
3161 u64 addr = sg_dma_address(sg);
3162 psg->sg[i].next = 0;
3163 psg->sg[i].prev = 0;
3164 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3165 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3166 psg->sg[i].count = cpu_to_le32(count);
3167 psg->sg[i].flags = 0;
3168 byte_count += count;
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003169 }
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003170 psg->count = cpu_to_le32(nseg);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003171 /* hba wants the size to be exact */
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003172 if (byte_count > scsi_bufflen(scsicmd)) {
3173 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3174 (byte_count - scsi_bufflen(scsicmd));
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003175 psg->sg[i-1].count = cpu_to_le32(temp);
FUJITA Tomonori727eead2007-05-26 02:00:42 +09003176 byte_count = scsi_bufflen(scsicmd);
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003177 }
3178 /* Check for command underflow */
3179 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3180 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3181 byte_count, scsicmd->underflow);
3182 }
3183 }
Mark Haverkamp0e68c002005-08-03 15:39:49 -07003184 return byte_count;
3185}
3186
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303187static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3188 struct aac_raw_io2 *rio2, int sg_max)
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303189{
3190 unsigned long byte_count = 0;
3191 int nseg;
3192
3193 nseg = scsi_dma_map(scsicmd);
Mahesh Rajashekhara0b433442012-10-19 19:21:07 +05303194 if (nseg < 0)
3195 return nseg;
Mahesh Rajashekhara85d22bb2012-07-14 18:18:51 +05303196 if (nseg) {
3197 struct scatterlist *sg;
3198 int i, conformable = 0;
3199 u32 min_size = PAGE_SIZE, cur_size;
3200
3201 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3202 int count = sg_dma_len(sg);
3203 u64 addr = sg_dma_address(sg);
3204
3205 BUG_ON(i >= sg_max);
3206 rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3207 rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3208 cur_size = cpu_to_le32(count);
3209 rio2->sge[i].length = cur_size;
3210 rio2->sge[i].flags = 0;
3211 if (i == 0) {
3212 conformable = 1;
3213 rio2->sgeFirstSize = cur_size;
3214 } else if (i == 1) {
3215 rio2->sgeNominalSize = cur_size;
3216 min_size = cur_size;
3217 } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
3218 conformable = 0;
3219 if (cur_size < min_size)
3220 min_size = cur_size;
3221 }
3222 byte_count += count;
3223 }
3224
3225 /* hba wants the size to be exact */
3226 if (byte_count > scsi_bufflen(scsicmd)) {
3227 u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
3228 (byte_count - scsi_bufflen(scsicmd));
3229 rio2->sge[i-1].length = cpu_to_le32(temp);
3230 byte_count = scsi_bufflen(scsicmd);
3231 }
3232
3233 rio2->sgeCnt = cpu_to_le32(nseg);
3234 rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
3235 /* not conformable: evaluate required sg elements */
3236 if (!conformable) {
3237 int j, nseg_new = nseg, err_found;
3238 for (i = min_size / PAGE_SIZE; i >= 1; --i) {
3239 err_found = 0;
3240 nseg_new = 2;
3241 for (j = 1; j < nseg - 1; ++j) {
3242 if (rio2->sge[j].length % (i*PAGE_SIZE)) {
3243 err_found = 1;
3244 break;
3245 }
3246 nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
3247 }
3248 if (!err_found)
3249 break;
3250 }
3251 if (i > 0 && nseg_new <= sg_max)
3252 aac_convert_sgraw2(rio2, i, nseg, nseg_new);
3253 } else
3254 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3255
3256 /* Check for command underflow */
3257 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3258 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3259 byte_count, scsicmd->underflow);
3260 }
3261 }
3262
3263 return byte_count;
3264}
3265
3266static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
3267{
3268 struct sge_ieee1212 *sge;
3269 int i, j, pos;
3270 u32 addr_low;
3271
3272 if (aac_convert_sgl == 0)
3273 return 0;
3274
3275 sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
3276 if (sge == NULL)
3277 return -1;
3278
3279 for (i = 1, pos = 1; i < nseg-1; ++i) {
3280 for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
3281 addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
3282 sge[pos].addrLow = addr_low;
3283 sge[pos].addrHigh = rio2->sge[i].addrHigh;
3284 if (addr_low < rio2->sge[i].addrLow)
3285 sge[pos].addrHigh++;
3286 sge[pos].length = pages * PAGE_SIZE;
3287 sge[pos].flags = 0;
3288 pos++;
3289 }
3290 }
3291 sge[pos] = rio2->sge[nseg-1];
3292 memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
3293
3294 kfree(sge);
3295 rio2->sgeCnt = cpu_to_le32(nseg_new);
3296 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3297 rio2->sgeNominalSize = pages * PAGE_SIZE;
3298 return 0;
3299}
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301#ifdef AAC_DETAILED_STATUS_INFO
3302
3303struct aac_srb_status_info {
3304 u32 status;
3305 char *str;
3306};
3307
3308
3309static struct aac_srb_status_info srb_status_info[] = {
3310 { SRB_STATUS_PENDING, "Pending Status"},
3311 { SRB_STATUS_SUCCESS, "Success"},
3312 { SRB_STATUS_ABORTED, "Aborted Command"},
3313 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
Tobias Klauser6391a112006-06-08 22:23:48 -07003314 { SRB_STATUS_ERROR, "Error Event"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 { SRB_STATUS_BUSY, "Device Busy"},
3316 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
3317 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
3318 { SRB_STATUS_NO_DEVICE, "No Device"},
3319 { SRB_STATUS_TIMEOUT, "Timeout"},
3320 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
3321 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
3322 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
3323 { SRB_STATUS_BUS_RESET, "Bus Reset"},
3324 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
3325 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
3326 { SRB_STATUS_NO_HBA, "No HBA"},
3327 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
3328 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
3329 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
3330 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
3331 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
3332 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
Tobias Klauser6391a112006-06-08 22:23:48 -07003333 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
3335 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
3336 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
3337 { SRB_STATUS_NOT_STARTED, "Not Started"},
3338 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
Salyzyn, Mark8ce3eca2008-01-16 07:39:06 -08003339 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
3341 { 0xff, "Unknown Error"}
3342};
3343
3344char *aac_get_status_string(u32 status)
3345{
3346 int i;
3347
Tobias Klauser6391a112006-06-08 22:23:48 -07003348 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
3349 if (srb_status_info[i].status == status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return srb_status_info[i].str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
3352 return "Bad Status Code";
3353}
3354
3355#endif