]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/scsi/aacraid/aachba.c
scsi: aacraid: Added support for hotplug
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / aacraid / aachba.c
1 /*
2 * Adaptec AAC series RAID controller driver
3 * (c) Copyright 2001 Red Hat Inc.
4 *
5 * based on the old aacraid driver that is..
6 * Adaptec aacraid device driver for Linux.
7 *
8 * Copyright (c) 2000-2010 Adaptec, Inc.
9 * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
10 *
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>
30 #include <linux/pci.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
33 #include <linux/completion.h>
34 #include <linux/blkdev.h>
35 #include <linux/uaccess.h>
36 #include <linux/highmem.h> /* For flush_kernel_dcache_page */
37 #include <linux/module.h>
38
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
57 /*
58 * Sense codes
59 */
60
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_UNRECOVERED_READ_ERROR 0x11
66 #define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
67 #define SENCODE_INVALID_COMMAND 0x20
68 #define SENCODE_LBA_OUT_OF_RANGE 0x21
69 #define SENCODE_INVALID_CDB_FIELD 0x24
70 #define SENCODE_LUN_NOT_SUPPORTED 0x25
71 #define SENCODE_INVALID_PARAM_FIELD 0x26
72 #define SENCODE_PARAM_NOT_SUPPORTED 0x26
73 #define SENCODE_PARAM_VALUE_INVALID 0x26
74 #define SENCODE_RESET_OCCURRED 0x29
75 #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
76 #define SENCODE_INQUIRY_DATA_CHANGED 0x3F
77 #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
78 #define SENCODE_DIAGNOSTIC_FAILURE 0x40
79 #define SENCODE_INTERNAL_TARGET_FAILURE 0x44
80 #define SENCODE_INVALID_MESSAGE_ERROR 0x49
81 #define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
82 #define SENCODE_OVERLAPPED_COMMAND 0x4E
83
84 /*
85 * Additional sense codes
86 */
87
88 #define ASENCODE_NO_SENSE 0x00
89 #define ASENCODE_END_OF_DATA 0x05
90 #define ASENCODE_BECOMING_READY 0x01
91 #define ASENCODE_INIT_CMD_REQUIRED 0x02
92 #define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
93 #define ASENCODE_INVALID_COMMAND 0x00
94 #define ASENCODE_LBA_OUT_OF_RANGE 0x00
95 #define ASENCODE_INVALID_CDB_FIELD 0x00
96 #define ASENCODE_LUN_NOT_SUPPORTED 0x00
97 #define ASENCODE_INVALID_PARAM_FIELD 0x00
98 #define ASENCODE_PARAM_NOT_SUPPORTED 0x01
99 #define ASENCODE_PARAM_VALUE_INVALID 0x02
100 #define ASENCODE_RESET_OCCURRED 0x00
101 #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
102 #define ASENCODE_INQUIRY_DATA_CHANGED 0x03
103 #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
104 #define ASENCODE_DIAGNOSTIC_FAILURE 0x80
105 #define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
106 #define ASENCODE_INVALID_MESSAGE_ERROR 0x00
107 #define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
108 #define ASENCODE_OVERLAPPED_COMMAND 0x00
109
110 #define AAC_STAT_GOOD (DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD)
111
112 #define BYTE0(x) (unsigned char)(x)
113 #define BYTE1(x) (unsigned char)((x) >> 8)
114 #define BYTE2(x) (unsigned char)((x) >> 16)
115 #define BYTE3(x) (unsigned char)((x) >> 24)
116
117 /* MODE_SENSE data format */
118 typedef struct {
119 struct {
120 u8 data_length;
121 u8 med_type;
122 u8 dev_par;
123 u8 bd_length;
124 } __attribute__((packed)) hd;
125 struct {
126 u8 dens_code;
127 u8 block_count[3];
128 u8 reserved;
129 u8 block_length[3];
130 } __attribute__((packed)) bd;
131 u8 mpc_buf[3];
132 } __attribute__((packed)) aac_modep_data;
133
134 /* MODE_SENSE_10 data format */
135 typedef struct {
136 struct {
137 u8 data_length[2];
138 u8 med_type;
139 u8 dev_par;
140 u8 rsrvd[2];
141 u8 bd_length[2];
142 } __attribute__((packed)) hd;
143 struct {
144 u8 dens_code;
145 u8 block_count[3];
146 u8 reserved;
147 u8 block_length[3];
148 } __attribute__((packed)) bd;
149 u8 mpc_buf[3];
150 } __attribute__((packed)) aac_modep10_data;
151
152 /*------------------------------------------------------------------------------
153 * S T R U C T S / T Y P E D E F S
154 *----------------------------------------------------------------------------*/
155 /* SCSI inquiry data */
156 struct inquiry_data {
157 u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
158 u8 inqd_dtq; /* RMB | Device Type Qualifier */
159 u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
160 u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
161 u8 inqd_len; /* Additional length (n-4) */
162 u8 inqd_pad1[2];/* Reserved - must be zero */
163 u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
164 u8 inqd_vid[8]; /* Vendor ID */
165 u8 inqd_pid[16];/* Product ID */
166 u8 inqd_prl[4]; /* Product Revision Level */
167 };
168
169 /* Added for VPD 0x83 */
170 typedef struct {
171 u8 CodeSet:4; /* VPD_CODE_SET */
172 u8 Reserved:4;
173 u8 IdentifierType:4; /* VPD_IDENTIFIER_TYPE */
174 u8 Reserved2:4;
175 u8 Reserved3;
176 u8 IdentifierLength;
177 u8 VendId[8];
178 u8 ProductId[16];
179 u8 SerialNumber[8]; /* SN in ASCII */
180
181 } TVPD_ID_Descriptor_Type_1;
182
183 typedef struct {
184 u8 CodeSet:4; /* VPD_CODE_SET */
185 u8 Reserved:4;
186 u8 IdentifierType:4; /* VPD_IDENTIFIER_TYPE */
187 u8 Reserved2:4;
188 u8 Reserved3;
189 u8 IdentifierLength;
190 struct TEU64Id {
191 u32 Serial;
192 /* The serial number supposed to be 40 bits,
193 * bit we only support 32, so make the last byte zero. */
194 u8 Reserved;
195 u8 VendId[3];
196 } EU64Id;
197
198 } TVPD_ID_Descriptor_Type_2;
199
200 typedef struct {
201 u8 DeviceType:5;
202 u8 DeviceTypeQualifier:3;
203 u8 PageCode;
204 u8 Reserved;
205 u8 PageLength;
206 TVPD_ID_Descriptor_Type_1 IdDescriptorType1;
207 TVPD_ID_Descriptor_Type_2 IdDescriptorType2;
208
209 } TVPD_Page83;
210
211 /*
212 * M O D U L E G L O B A L S
213 */
214
215 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
216 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
217 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
218 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
219 struct aac_raw_io2 *rio2, int sg_max);
220 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
221 int pages, int nseg, int nseg_new);
222 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
223 #ifdef AAC_DETAILED_STATUS_INFO
224 static char *aac_get_status_string(u32 status);
225 #endif
226
227 /*
228 * Non dasd selection is handled entirely in aachba now
229 */
230
231 static int nondasd = -1;
232 static int aac_cache = 2; /* WCE=0 to avoid performance problems */
233 static int dacmode = -1;
234 int aac_msi;
235 int aac_commit = -1;
236 int startup_timeout = 180;
237 int aif_timeout = 120;
238 int aac_sync_mode; /* Only Sync. transfer - disabled */
239 int aac_convert_sgl = 1; /* convert non-conformable s/g list - enabled */
240
241 module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
242 MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
243 " 0=off, 1=on");
244 module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
245 MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
246 " 0=off, 1=on");
247 module_param(nondasd, int, S_IRUGO|S_IWUSR);
248 MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
249 " 0=off, 1=on");
250 module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
251 MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
252 "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
253 "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
254 "\tbit 2 - Disable only if Battery is protecting Cache");
255 module_param(dacmode, int, S_IRUGO|S_IWUSR);
256 MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
257 " 0=off, 1=on");
258 module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
259 MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
260 " adapter for foreign arrays.\n"
261 "This is typically needed in systems that do not have a BIOS."
262 " 0=off, 1=on");
263 module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
264 MODULE_PARM_DESC(msi, "IRQ handling."
265 " 0=PIC(default), 1=MSI, 2=MSI-X)");
266 module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
267 MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
268 " adapter to have it's kernel up and\n"
269 "running. This is typically adjusted for large systems that do not"
270 " have a BIOS.");
271 module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
272 MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
273 " applications to pick up AIFs before\n"
274 "deregistering them. This is typically adjusted for heavily burdened"
275 " systems.");
276
277 int numacb = -1;
278 module_param(numacb, int, S_IRUGO|S_IWUSR);
279 MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
280 " blocks (FIB) allocated. Valid values are 512 and down. Default is"
281 " to use suggestion from Firmware.");
282
283 int acbsize = -1;
284 module_param(acbsize, int, S_IRUGO|S_IWUSR);
285 MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
286 " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
287 " suggestion from Firmware.");
288
289 int update_interval = 30 * 60;
290 module_param(update_interval, int, S_IRUGO|S_IWUSR);
291 MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
292 " updates issued to adapter.");
293
294 int check_interval = 24 * 60 * 60;
295 module_param(check_interval, int, S_IRUGO|S_IWUSR);
296 MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
297 " checks.");
298
299 int aac_check_reset = 1;
300 module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
301 MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
302 " adapter. a value of -1 forces the reset to adapters programmed to"
303 " ignore it.");
304
305 int expose_physicals = -1;
306 module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
307 MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
308 " -1=protect 0=off, 1=on");
309
310 int aac_reset_devices;
311 module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
312 MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
313
314 int aac_wwn = 1;
315 module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
316 MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
317 "\t0 - Disable\n"
318 "\t1 - Array Meta Data Signature (default)\n"
319 "\t2 - Adapter Serial Number");
320
321
322 static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
323 struct fib *fibptr) {
324 struct scsi_device *device;
325
326 if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
327 dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
328 aac_fib_complete(fibptr);
329 return 0;
330 }
331 scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
332 device = scsicmd->device;
333 if (unlikely(!device)) {
334 dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
335 aac_fib_complete(fibptr);
336 return 0;
337 }
338 return 1;
339 }
340
341 /**
342 * aac_get_config_status - check the adapter configuration
343 * @common: adapter to query
344 *
345 * Query config status, and commit the configuration if needed.
346 */
347 int aac_get_config_status(struct aac_dev *dev, int commit_flag)
348 {
349 int status = 0;
350 struct fib * fibptr;
351
352 if (!(fibptr = aac_fib_alloc(dev)))
353 return -ENOMEM;
354
355 aac_fib_init(fibptr);
356 {
357 struct aac_get_config_status *dinfo;
358 dinfo = (struct aac_get_config_status *) fib_data(fibptr);
359
360 dinfo->command = cpu_to_le32(VM_ContainerConfig);
361 dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
362 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
363 }
364
365 status = aac_fib_send(ContainerCommand,
366 fibptr,
367 sizeof (struct aac_get_config_status),
368 FsaNormal,
369 1, 1,
370 NULL, NULL);
371 if (status < 0) {
372 printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
373 } else {
374 struct aac_get_config_status_resp *reply
375 = (struct aac_get_config_status_resp *) fib_data(fibptr);
376 dprintk((KERN_WARNING
377 "aac_get_config_status: response=%d status=%d action=%d\n",
378 le32_to_cpu(reply->response),
379 le32_to_cpu(reply->status),
380 le32_to_cpu(reply->data.action)));
381 if ((le32_to_cpu(reply->response) != ST_OK) ||
382 (le32_to_cpu(reply->status) != CT_OK) ||
383 (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
384 printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
385 status = -EINVAL;
386 }
387 }
388 /* Do not set XferState to zero unless receives a response from F/W */
389 if (status >= 0)
390 aac_fib_complete(fibptr);
391
392 /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
393 if (status >= 0) {
394 if ((aac_commit == 1) || commit_flag) {
395 struct aac_commit_config * dinfo;
396 aac_fib_init(fibptr);
397 dinfo = (struct aac_commit_config *) fib_data(fibptr);
398
399 dinfo->command = cpu_to_le32(VM_ContainerConfig);
400 dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
401
402 status = aac_fib_send(ContainerCommand,
403 fibptr,
404 sizeof (struct aac_commit_config),
405 FsaNormal,
406 1, 1,
407 NULL, NULL);
408 /* Do not set XferState to zero unless
409 * receives a response from F/W */
410 if (status >= 0)
411 aac_fib_complete(fibptr);
412 } else if (aac_commit == 0) {
413 printk(KERN_WARNING
414 "aac_get_config_status: Foreign device configurations are being ignored\n");
415 }
416 }
417 /* FIB should be freed only after getting the response from the F/W */
418 if (status != -ERESTARTSYS)
419 aac_fib_free(fibptr);
420 return status;
421 }
422
423 static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
424 {
425 char inq_data;
426 scsi_sg_copy_to_buffer(scsicmd, &inq_data, sizeof(inq_data));
427 if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
428 inq_data &= 0xdf;
429 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
430 }
431 }
432
433 /**
434 * aac_get_containers - list containers
435 * @common: adapter to probe
436 *
437 * Make a list of all containers on this controller
438 */
439 int aac_get_containers(struct aac_dev *dev)
440 {
441 struct fsa_dev_info *fsa_dev_ptr;
442 u32 index;
443 int status = 0;
444 struct fib * fibptr;
445 struct aac_get_container_count *dinfo;
446 struct aac_get_container_count_resp *dresp;
447 int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
448
449 if (!(fibptr = aac_fib_alloc(dev)))
450 return -ENOMEM;
451
452 aac_fib_init(fibptr);
453 dinfo = (struct aac_get_container_count *) fib_data(fibptr);
454 dinfo->command = cpu_to_le32(VM_ContainerConfig);
455 dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
456
457 status = aac_fib_send(ContainerCommand,
458 fibptr,
459 sizeof (struct aac_get_container_count),
460 FsaNormal,
461 1, 1,
462 NULL, NULL);
463 if (status >= 0) {
464 dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
465 maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
466 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
467 AAC_OPTION_SUPPORTED_240_VOLUMES) {
468 maximum_num_containers =
469 le32_to_cpu(dresp->MaxSimpleVolumes);
470 }
471 aac_fib_complete(fibptr);
472 }
473 /* FIB should be freed only after getting the response from the F/W */
474 if (status != -ERESTARTSYS)
475 aac_fib_free(fibptr);
476
477 if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
478 maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
479 if (dev->fsa_dev == NULL ||
480 dev->maximum_num_containers != maximum_num_containers) {
481
482 fsa_dev_ptr = dev->fsa_dev;
483
484 dev->fsa_dev = kcalloc(maximum_num_containers,
485 sizeof(*fsa_dev_ptr), GFP_KERNEL);
486
487 kfree(fsa_dev_ptr);
488 fsa_dev_ptr = NULL;
489
490
491 if (!dev->fsa_dev)
492 return -ENOMEM;
493
494 dev->maximum_num_containers = maximum_num_containers;
495 }
496 for (index = 0; index < dev->maximum_num_containers; index++) {
497 dev->fsa_dev[index].devname[0] = '\0';
498 dev->fsa_dev[index].valid = 0;
499
500 status = aac_probe_container(dev, index);
501
502 if (status < 0) {
503 printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
504 break;
505 }
506 }
507 return status;
508 }
509
510 static void get_container_name_callback(void *context, struct fib * fibptr)
511 {
512 struct aac_get_name_resp * get_name_reply;
513 struct scsi_cmnd * scsicmd;
514
515 scsicmd = (struct scsi_cmnd *) context;
516
517 if (!aac_valid_context(scsicmd, fibptr))
518 return;
519
520 dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
521 BUG_ON(fibptr == NULL);
522
523 get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
524 /* Failure is irrelevant, using default value instead */
525 if ((le32_to_cpu(get_name_reply->status) == CT_OK)
526 && (get_name_reply->data[0] != '\0')) {
527 char *sp = get_name_reply->data;
528 sp[sizeof(((struct aac_get_name_resp *)NULL)->data)] = '\0';
529 while (*sp == ' ')
530 ++sp;
531 if (*sp) {
532 struct inquiry_data inq;
533 char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
534 int count = sizeof(d);
535 char *dp = d;
536 do {
537 *dp++ = (*sp) ? *sp++ : ' ';
538 } while (--count > 0);
539
540 scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
541 memcpy(inq.inqd_pid, d, sizeof(d));
542 scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
543 }
544 }
545
546 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
547
548 aac_fib_complete(fibptr);
549 scsicmd->scsi_done(scsicmd);
550 }
551
552 /**
553 * aac_get_container_name - get container name, none blocking.
554 */
555 static int aac_get_container_name(struct scsi_cmnd * scsicmd)
556 {
557 int status;
558 struct aac_get_name *dinfo;
559 struct fib * cmd_fibcontext;
560 struct aac_dev * dev;
561
562 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
563
564 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
565
566 aac_fib_init(cmd_fibcontext);
567 dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
568
569 dinfo->command = cpu_to_le32(VM_ContainerConfig);
570 dinfo->type = cpu_to_le32(CT_READ_NAME);
571 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
572 dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
573
574 status = aac_fib_send(ContainerCommand,
575 cmd_fibcontext,
576 sizeof(struct aac_get_name_resp),
577 FsaNormal,
578 0, 1,
579 (fib_callback)get_container_name_callback,
580 (void *) scsicmd);
581
582 /*
583 * Check that the command queued to the controller
584 */
585 if (status == -EINPROGRESS) {
586 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
587 return 0;
588 }
589
590 printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
591 aac_fib_complete(cmd_fibcontext);
592 return -1;
593 }
594
595 static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
596 {
597 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
598
599 if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
600 return aac_scsi_cmd(scsicmd);
601
602 scsicmd->result = DID_NO_CONNECT << 16;
603 scsicmd->scsi_done(scsicmd);
604 return 0;
605 }
606
607 static void _aac_probe_container2(void * context, struct fib * fibptr)
608 {
609 struct fsa_dev_info *fsa_dev_ptr;
610 int (*callback)(struct scsi_cmnd *);
611 struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
612
613
614 if (!aac_valid_context(scsicmd, fibptr))
615 return;
616
617 scsicmd->SCp.Status = 0;
618 fsa_dev_ptr = fibptr->dev->fsa_dev;
619 if (fsa_dev_ptr) {
620 struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
621 fsa_dev_ptr += scmd_id(scsicmd);
622
623 if ((le32_to_cpu(dresp->status) == ST_OK) &&
624 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
625 (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
626 if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
627 AAC_OPTION_VARIABLE_BLOCK_SIZE)) {
628 dresp->mnt[0].fileinfo.bdevinfo.block_size = 0x200;
629 fsa_dev_ptr->block_size = 0x200;
630 } else {
631 fsa_dev_ptr->block_size =
632 le32_to_cpu(dresp->mnt[0].fileinfo.bdevinfo.block_size);
633 }
634 fsa_dev_ptr->valid = 1;
635 /* sense_key holds the current state of the spin-up */
636 if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
637 fsa_dev_ptr->sense_data.sense_key = NOT_READY;
638 else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
639 fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
640 fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
641 fsa_dev_ptr->size
642 = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
643 (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
644 fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
645 }
646 if ((fsa_dev_ptr->valid & 1) == 0)
647 fsa_dev_ptr->valid = 0;
648 scsicmd->SCp.Status = le32_to_cpu(dresp->count);
649 }
650 aac_fib_complete(fibptr);
651 aac_fib_free(fibptr);
652 callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
653 scsicmd->SCp.ptr = NULL;
654 (*callback)(scsicmd);
655 return;
656 }
657
658 static void _aac_probe_container1(void * context, struct fib * fibptr)
659 {
660 struct scsi_cmnd * scsicmd;
661 struct aac_mount * dresp;
662 struct aac_query_mount *dinfo;
663 int status;
664
665 dresp = (struct aac_mount *) fib_data(fibptr);
666 if (!(fibptr->dev->supplement_adapter_info.SupportedOptions2 &
667 AAC_OPTION_VARIABLE_BLOCK_SIZE))
668 dresp->mnt[0].capacityhigh = 0;
669 if ((le32_to_cpu(dresp->status) != ST_OK) ||
670 (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
671 _aac_probe_container2(context, fibptr);
672 return;
673 }
674 scsicmd = (struct scsi_cmnd *) context;
675
676 if (!aac_valid_context(scsicmd, fibptr))
677 return;
678
679 aac_fib_init(fibptr);
680
681 dinfo = (struct aac_query_mount *)fib_data(fibptr);
682
683 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
684 AAC_OPTION_VARIABLE_BLOCK_SIZE)
685 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
686 else
687 dinfo->command = cpu_to_le32(VM_NameServe64);
688
689 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
690 dinfo->type = cpu_to_le32(FT_FILESYS);
691
692 status = aac_fib_send(ContainerCommand,
693 fibptr,
694 sizeof(struct aac_query_mount),
695 FsaNormal,
696 0, 1,
697 _aac_probe_container2,
698 (void *) scsicmd);
699 /*
700 * Check that the command queued to the controller
701 */
702 if (status == -EINPROGRESS)
703 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
704 else if (status < 0) {
705 /* Inherit results from VM_NameServe, if any */
706 dresp->status = cpu_to_le32(ST_OK);
707 _aac_probe_container2(context, fibptr);
708 }
709 }
710
711 static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
712 {
713 struct fib * fibptr;
714 int status = -ENOMEM;
715
716 if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
717 struct aac_query_mount *dinfo;
718
719 aac_fib_init(fibptr);
720
721 dinfo = (struct aac_query_mount *)fib_data(fibptr);
722
723 if (fibptr->dev->supplement_adapter_info.SupportedOptions2 &
724 AAC_OPTION_VARIABLE_BLOCK_SIZE)
725 dinfo->command = cpu_to_le32(VM_NameServeAllBlk);
726 else
727 dinfo->command = cpu_to_le32(VM_NameServe);
728
729 dinfo->count = cpu_to_le32(scmd_id(scsicmd));
730 dinfo->type = cpu_to_le32(FT_FILESYS);
731 scsicmd->SCp.ptr = (char *)callback;
732
733 status = aac_fib_send(ContainerCommand,
734 fibptr,
735 sizeof(struct aac_query_mount),
736 FsaNormal,
737 0, 1,
738 _aac_probe_container1,
739 (void *) scsicmd);
740 /*
741 * Check that the command queued to the controller
742 */
743 if (status == -EINPROGRESS) {
744 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
745 return 0;
746 }
747 if (status < 0) {
748 scsicmd->SCp.ptr = NULL;
749 aac_fib_complete(fibptr);
750 aac_fib_free(fibptr);
751 }
752 }
753 if (status < 0) {
754 struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
755 if (fsa_dev_ptr) {
756 fsa_dev_ptr += scmd_id(scsicmd);
757 if ((fsa_dev_ptr->valid & 1) == 0) {
758 fsa_dev_ptr->valid = 0;
759 return (*callback)(scsicmd);
760 }
761 }
762 }
763 return status;
764 }
765
766 /**
767 * aac_probe_container - query a logical volume
768 * @dev: device to query
769 * @cid: container identifier
770 *
771 * Queries the controller about the given volume. The volume information
772 * is updated in the struct fsa_dev_info structure rather than returned.
773 */
774 static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
775 {
776 scsicmd->device = NULL;
777 return 0;
778 }
779
780 int aac_probe_container(struct aac_dev *dev, int cid)
781 {
782 struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
783 struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
784 int status;
785
786 if (!scsicmd || !scsidev) {
787 kfree(scsicmd);
788 kfree(scsidev);
789 return -ENOMEM;
790 }
791 scsicmd->list.next = NULL;
792 scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
793
794 scsicmd->device = scsidev;
795 scsidev->sdev_state = 0;
796 scsidev->id = cid;
797 scsidev->host = dev->scsi_host_ptr;
798
799 if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
800 while (scsicmd->device == scsidev)
801 schedule();
802 kfree(scsidev);
803 status = scsicmd->SCp.Status;
804 kfree(scsicmd);
805 return status;
806 }
807
808 /* Local Structure to set SCSI inquiry data strings */
809 struct scsi_inq {
810 char vid[8]; /* Vendor ID */
811 char pid[16]; /* Product ID */
812 char prl[4]; /* Product Revision Level */
813 };
814
815 /**
816 * InqStrCopy - string merge
817 * @a: string to copy from
818 * @b: string to copy to
819 *
820 * Copy a String from one location to another
821 * without copying \0
822 */
823
824 static void inqstrcpy(char *a, char *b)
825 {
826
827 while (*a != (char)0)
828 *b++ = *a++;
829 }
830
831 static char *container_types[] = {
832 "None",
833 "Volume",
834 "Mirror",
835 "Stripe",
836 "RAID5",
837 "SSRW",
838 "SSRO",
839 "Morph",
840 "Legacy",
841 "RAID4",
842 "RAID10",
843 "RAID00",
844 "V-MIRRORS",
845 "PSEUDO R4",
846 "RAID50",
847 "RAID5D",
848 "RAID5D0",
849 "RAID1E",
850 "RAID6",
851 "RAID60",
852 "Unknown"
853 };
854
855 char * get_container_type(unsigned tindex)
856 {
857 if (tindex >= ARRAY_SIZE(container_types))
858 tindex = ARRAY_SIZE(container_types) - 1;
859 return container_types[tindex];
860 }
861
862 /* Function: setinqstr
863 *
864 * Arguments: [1] pointer to void [1] int
865 *
866 * Purpose: Sets SCSI inquiry data strings for vendor, product
867 * and revision level. Allows strings to be set in platform dependent
868 * files instead of in OS dependent driver source.
869 */
870
871 static void setinqstr(struct aac_dev *dev, void *data, int tindex)
872 {
873 struct scsi_inq *str;
874
875 str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
876 memset(str, ' ', sizeof(*str));
877
878 if (dev->supplement_adapter_info.AdapterTypeText[0]) {
879 char * cp = dev->supplement_adapter_info.AdapterTypeText;
880 int c;
881 if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
882 inqstrcpy("SMC", str->vid);
883 else {
884 c = sizeof(str->vid);
885 while (*cp && *cp != ' ' && --c)
886 ++cp;
887 c = *cp;
888 *cp = '\0';
889 inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
890 str->vid);
891 *cp = c;
892 while (*cp && *cp != ' ')
893 ++cp;
894 }
895 while (*cp == ' ')
896 ++cp;
897 /* last six chars reserved for vol type */
898 c = 0;
899 if (strlen(cp) > sizeof(str->pid)) {
900 c = cp[sizeof(str->pid)];
901 cp[sizeof(str->pid)] = '\0';
902 }
903 inqstrcpy (cp, str->pid);
904 if (c)
905 cp[sizeof(str->pid)] = c;
906 } else {
907 struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
908
909 inqstrcpy (mp->vname, str->vid);
910 /* last six chars reserved for vol type */
911 inqstrcpy (mp->model, str->pid);
912 }
913
914 if (tindex < ARRAY_SIZE(container_types)){
915 char *findit = str->pid;
916
917 for ( ; *findit != ' '; findit++); /* walk till we find a space */
918 /* RAID is superfluous in the context of a RAID device */
919 if (memcmp(findit-4, "RAID", 4) == 0)
920 *(findit -= 4) = ' ';
921 if (((findit - str->pid) + strlen(container_types[tindex]))
922 < (sizeof(str->pid) + sizeof(str->prl)))
923 inqstrcpy (container_types[tindex], findit + 1);
924 }
925 inqstrcpy ("V1.0", str->prl);
926 }
927
928 static void get_container_serial_callback(void *context, struct fib * fibptr)
929 {
930 struct aac_get_serial_resp * get_serial_reply;
931 struct scsi_cmnd * scsicmd;
932
933 BUG_ON(fibptr == NULL);
934
935 scsicmd = (struct scsi_cmnd *) context;
936 if (!aac_valid_context(scsicmd, fibptr))
937 return;
938
939 get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
940 /* Failure is irrelevant, using default value instead */
941 if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
942 /*Check to see if it's for VPD 0x83 or 0x80 */
943 if (scsicmd->cmnd[2] == 0x83) {
944 /* vpd page 0x83 - Device Identification Page */
945 int i;
946 TVPD_Page83 VPDPage83Data;
947
948 memset(((u8 *)&VPDPage83Data), 0,
949 sizeof(VPDPage83Data));
950
951 /* DIRECT_ACCESS_DEVIC */
952 VPDPage83Data.DeviceType = 0;
953 /* DEVICE_CONNECTED */
954 VPDPage83Data.DeviceTypeQualifier = 0;
955 /* VPD_DEVICE_IDENTIFIERS */
956 VPDPage83Data.PageCode = 0x83;
957 VPDPage83Data.Reserved = 0;
958 VPDPage83Data.PageLength =
959 sizeof(VPDPage83Data.IdDescriptorType1) +
960 sizeof(VPDPage83Data.IdDescriptorType2);
961
962 /* T10 Vendor Identifier Field Format */
963 /* VpdCodeSetAscii */
964 VPDPage83Data.IdDescriptorType1.CodeSet = 2;
965 /* VpdIdentifierTypeVendorId */
966 VPDPage83Data.IdDescriptorType1.IdentifierType = 1;
967 VPDPage83Data.IdDescriptorType1.IdentifierLength =
968 sizeof(VPDPage83Data.IdDescriptorType1) - 4;
969
970 /* "ADAPTEC " for adaptec */
971 memcpy(VPDPage83Data.IdDescriptorType1.VendId,
972 "ADAPTEC ",
973 sizeof(VPDPage83Data.IdDescriptorType1.VendId));
974 memcpy(VPDPage83Data.IdDescriptorType1.ProductId,
975 "ARRAY ",
976 sizeof(
977 VPDPage83Data.IdDescriptorType1.ProductId));
978
979 /* Convert to ascii based serial number.
980 * The LSB is the the end.
981 */
982 for (i = 0; i < 8; i++) {
983 u8 temp =
984 (u8)((get_serial_reply->uid >> ((7 - i) * 4)) & 0xF);
985 if (temp > 0x9) {
986 VPDPage83Data.IdDescriptorType1.SerialNumber[i] =
987 'A' + (temp - 0xA);
988 } else {
989 VPDPage83Data.IdDescriptorType1.SerialNumber[i] =
990 '0' + temp;
991 }
992 }
993
994 /* VpdCodeSetBinary */
995 VPDPage83Data.IdDescriptorType2.CodeSet = 1;
996 /* VpdIdentifierTypeEUI64 */
997 VPDPage83Data.IdDescriptorType2.IdentifierType = 2;
998 VPDPage83Data.IdDescriptorType2.IdentifierLength =
999 sizeof(VPDPage83Data.IdDescriptorType2) - 4;
1000
1001 VPDPage83Data.IdDescriptorType2.EU64Id.VendId[0] = 0xD0;
1002 VPDPage83Data.IdDescriptorType2.EU64Id.VendId[1] = 0;
1003 VPDPage83Data.IdDescriptorType2.EU64Id.VendId[2] = 0;
1004
1005 VPDPage83Data.IdDescriptorType2.EU64Id.Serial =
1006 get_serial_reply->uid;
1007 VPDPage83Data.IdDescriptorType2.EU64Id.Reserved = 0;
1008
1009 /* Move the inquiry data to the response buffer. */
1010 scsi_sg_copy_from_buffer(scsicmd, &VPDPage83Data,
1011 sizeof(VPDPage83Data));
1012 } else {
1013 /* It must be for VPD 0x80 */
1014 char sp[13];
1015 /* EVPD bit set */
1016 sp[0] = INQD_PDT_DA;
1017 sp[1] = scsicmd->cmnd[2];
1018 sp[2] = 0;
1019 sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
1020 le32_to_cpu(get_serial_reply->uid));
1021 scsi_sg_copy_from_buffer(scsicmd, sp,
1022 sizeof(sp));
1023 }
1024 }
1025
1026 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
1027
1028 aac_fib_complete(fibptr);
1029 scsicmd->scsi_done(scsicmd);
1030 }
1031
1032 /**
1033 * aac_get_container_serial - get container serial, none blocking.
1034 */
1035 static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
1036 {
1037 int status;
1038 struct aac_get_serial *dinfo;
1039 struct fib * cmd_fibcontext;
1040 struct aac_dev * dev;
1041
1042 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
1043
1044 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
1045
1046 aac_fib_init(cmd_fibcontext);
1047 dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
1048
1049 dinfo->command = cpu_to_le32(VM_ContainerConfig);
1050 dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
1051 dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
1052
1053 status = aac_fib_send(ContainerCommand,
1054 cmd_fibcontext,
1055 sizeof(struct aac_get_serial_resp),
1056 FsaNormal,
1057 0, 1,
1058 (fib_callback) get_container_serial_callback,
1059 (void *) scsicmd);
1060
1061 /*
1062 * Check that the command queued to the controller
1063 */
1064 if (status == -EINPROGRESS) {
1065 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
1066 return 0;
1067 }
1068
1069 printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
1070 aac_fib_complete(cmd_fibcontext);
1071 return -1;
1072 }
1073
1074 /* Function: setinqserial
1075 *
1076 * Arguments: [1] pointer to void [1] int
1077 *
1078 * Purpose: Sets SCSI Unit Serial number.
1079 * This is a fake. We should read a proper
1080 * serial number from the container. <SuSE>But
1081 * without docs it's quite hard to do it :-)
1082 * So this will have to do in the meantime.</SuSE>
1083 */
1084
1085 static int setinqserial(struct aac_dev *dev, void *data, int cid)
1086 {
1087 /*
1088 * This breaks array migration.
1089 */
1090 return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
1091 le32_to_cpu(dev->adapter_info.serial[0]), cid);
1092 }
1093
1094 static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
1095 u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
1096 {
1097 u8 *sense_buf = (u8 *)sense_data;
1098 /* Sense data valid, err code 70h */
1099 sense_buf[0] = 0x70; /* No info field */
1100 sense_buf[1] = 0; /* Segment number, always zero */
1101
1102 sense_buf[2] = sense_key; /* Sense key */
1103
1104 sense_buf[12] = sense_code; /* Additional sense code */
1105 sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
1106
1107 if (sense_key == ILLEGAL_REQUEST) {
1108 sense_buf[7] = 10; /* Additional sense length */
1109
1110 sense_buf[15] = bit_pointer;
1111 /* Illegal parameter is in the parameter block */
1112 if (sense_code == SENCODE_INVALID_CDB_FIELD)
1113 sense_buf[15] |= 0xc0;/* Std sense key specific field */
1114 /* Illegal parameter is in the CDB block */
1115 sense_buf[16] = field_pointer >> 8; /* MSB */
1116 sense_buf[17] = field_pointer; /* LSB */
1117 } else
1118 sense_buf[7] = 6; /* Additional sense length */
1119 }
1120
1121 static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1122 {
1123 if (lba & 0xffffffff00000000LL) {
1124 int cid = scmd_id(cmd);
1125 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
1126 cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
1127 SAM_STAT_CHECK_CONDITION;
1128 set_sense(&dev->fsa_dev[cid].sense_data,
1129 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
1130 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
1131 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
1132 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
1133 SCSI_SENSE_BUFFERSIZE));
1134 cmd->scsi_done(cmd);
1135 return 1;
1136 }
1137 return 0;
1138 }
1139
1140 static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
1141 {
1142 return 0;
1143 }
1144
1145 static void io_callback(void *context, struct fib * fibptr);
1146
1147 static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1148 {
1149 struct aac_dev *dev = fib->dev;
1150 u16 fibsize, command;
1151 long ret;
1152
1153 aac_fib_init(fib);
1154 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1155 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1156 !dev->sync_mode) {
1157 struct aac_raw_io2 *readcmd2;
1158 readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
1159 memset(readcmd2, 0, sizeof(struct aac_raw_io2));
1160 readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1161 readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1162 readcmd2->byteCount = cpu_to_le32(count *
1163 dev->fsa_dev[scmd_id(cmd)].block_size);
1164 readcmd2->cid = cpu_to_le16(scmd_id(cmd));
1165 readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
1166 ret = aac_build_sgraw2(cmd, readcmd2,
1167 dev->scsi_host_ptr->sg_tablesize);
1168 if (ret < 0)
1169 return ret;
1170 command = ContainerRawIo2;
1171 fibsize = sizeof(struct aac_raw_io2) +
1172 ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1173 } else {
1174 struct aac_raw_io *readcmd;
1175 readcmd = (struct aac_raw_io *) fib_data(fib);
1176 readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1177 readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1178 readcmd->count = cpu_to_le32(count *
1179 dev->fsa_dev[scmd_id(cmd)].block_size);
1180 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1181 readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
1182 readcmd->bpTotal = 0;
1183 readcmd->bpComplete = 0;
1184 ret = aac_build_sgraw(cmd, &readcmd->sg);
1185 if (ret < 0)
1186 return ret;
1187 command = ContainerRawIo;
1188 fibsize = sizeof(struct aac_raw_io) +
1189 ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
1190 }
1191
1192 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1193 /*
1194 * Now send the Fib to the adapter
1195 */
1196 return aac_fib_send(command,
1197 fib,
1198 fibsize,
1199 FsaNormal,
1200 0, 1,
1201 (fib_callback) io_callback,
1202 (void *) cmd);
1203 }
1204
1205 static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1206 {
1207 u16 fibsize;
1208 struct aac_read64 *readcmd;
1209 long ret;
1210
1211 aac_fib_init(fib);
1212 readcmd = (struct aac_read64 *) fib_data(fib);
1213 readcmd->command = cpu_to_le32(VM_CtHostRead64);
1214 readcmd->cid = cpu_to_le16(scmd_id(cmd));
1215 readcmd->sector_count = cpu_to_le16(count);
1216 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1217 readcmd->pad = 0;
1218 readcmd->flags = 0;
1219
1220 ret = aac_build_sg64(cmd, &readcmd->sg);
1221 if (ret < 0)
1222 return ret;
1223 fibsize = sizeof(struct aac_read64) +
1224 ((le32_to_cpu(readcmd->sg.count) - 1) *
1225 sizeof (struct sgentry64));
1226 BUG_ON (fibsize > (fib->dev->max_fib_size -
1227 sizeof(struct aac_fibhdr)));
1228 /*
1229 * Now send the Fib to the adapter
1230 */
1231 return aac_fib_send(ContainerCommand64,
1232 fib,
1233 fibsize,
1234 FsaNormal,
1235 0, 1,
1236 (fib_callback) io_callback,
1237 (void *) cmd);
1238 }
1239
1240 static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
1241 {
1242 u16 fibsize;
1243 struct aac_read *readcmd;
1244 struct aac_dev *dev = fib->dev;
1245 long ret;
1246
1247 aac_fib_init(fib);
1248 readcmd = (struct aac_read *) fib_data(fib);
1249 readcmd->command = cpu_to_le32(VM_CtBlockRead);
1250 readcmd->cid = cpu_to_le32(scmd_id(cmd));
1251 readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1252 readcmd->count = cpu_to_le32(count *
1253 dev->fsa_dev[scmd_id(cmd)].block_size);
1254
1255 ret = aac_build_sg(cmd, &readcmd->sg);
1256 if (ret < 0)
1257 return ret;
1258 fibsize = sizeof(struct aac_read) +
1259 ((le32_to_cpu(readcmd->sg.count) - 1) *
1260 sizeof (struct sgentry));
1261 BUG_ON (fibsize > (fib->dev->max_fib_size -
1262 sizeof(struct aac_fibhdr)));
1263 /*
1264 * Now send the Fib to the adapter
1265 */
1266 return aac_fib_send(ContainerCommand,
1267 fib,
1268 fibsize,
1269 FsaNormal,
1270 0, 1,
1271 (fib_callback) io_callback,
1272 (void *) cmd);
1273 }
1274
1275 static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1276 {
1277 struct aac_dev *dev = fib->dev;
1278 u16 fibsize, command;
1279 long ret;
1280
1281 aac_fib_init(fib);
1282 if ((dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 ||
1283 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) &&
1284 !dev->sync_mode) {
1285 struct aac_raw_io2 *writecmd2;
1286 writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
1287 memset(writecmd2, 0, sizeof(struct aac_raw_io2));
1288 writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
1289 writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1290 writecmd2->byteCount = cpu_to_le32(count *
1291 dev->fsa_dev[scmd_id(cmd)].block_size);
1292 writecmd2->cid = cpu_to_le16(scmd_id(cmd));
1293 writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
1294 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1295 cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
1296 cpu_to_le16(RIO2_IO_TYPE_WRITE);
1297 ret = aac_build_sgraw2(cmd, writecmd2,
1298 dev->scsi_host_ptr->sg_tablesize);
1299 if (ret < 0)
1300 return ret;
1301 command = ContainerRawIo2;
1302 fibsize = sizeof(struct aac_raw_io2) +
1303 ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
1304 } else {
1305 struct aac_raw_io *writecmd;
1306 writecmd = (struct aac_raw_io *) fib_data(fib);
1307 writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
1308 writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
1309 writecmd->count = cpu_to_le32(count *
1310 dev->fsa_dev[scmd_id(cmd)].block_size);
1311 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1312 writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
1313 (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
1314 cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
1315 cpu_to_le16(RIO_TYPE_WRITE);
1316 writecmd->bpTotal = 0;
1317 writecmd->bpComplete = 0;
1318 ret = aac_build_sgraw(cmd, &writecmd->sg);
1319 if (ret < 0)
1320 return ret;
1321 command = ContainerRawIo;
1322 fibsize = sizeof(struct aac_raw_io) +
1323 ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
1324 }
1325
1326 BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
1327 /*
1328 * Now send the Fib to the adapter
1329 */
1330 return aac_fib_send(command,
1331 fib,
1332 fibsize,
1333 FsaNormal,
1334 0, 1,
1335 (fib_callback) io_callback,
1336 (void *) cmd);
1337 }
1338
1339 static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1340 {
1341 u16 fibsize;
1342 struct aac_write64 *writecmd;
1343 long ret;
1344
1345 aac_fib_init(fib);
1346 writecmd = (struct aac_write64 *) fib_data(fib);
1347 writecmd->command = cpu_to_le32(VM_CtHostWrite64);
1348 writecmd->cid = cpu_to_le16(scmd_id(cmd));
1349 writecmd->sector_count = cpu_to_le16(count);
1350 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1351 writecmd->pad = 0;
1352 writecmd->flags = 0;
1353
1354 ret = aac_build_sg64(cmd, &writecmd->sg);
1355 if (ret < 0)
1356 return ret;
1357 fibsize = sizeof(struct aac_write64) +
1358 ((le32_to_cpu(writecmd->sg.count) - 1) *
1359 sizeof (struct sgentry64));
1360 BUG_ON (fibsize > (fib->dev->max_fib_size -
1361 sizeof(struct aac_fibhdr)));
1362 /*
1363 * Now send the Fib to the adapter
1364 */
1365 return aac_fib_send(ContainerCommand64,
1366 fib,
1367 fibsize,
1368 FsaNormal,
1369 0, 1,
1370 (fib_callback) io_callback,
1371 (void *) cmd);
1372 }
1373
1374 static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
1375 {
1376 u16 fibsize;
1377 struct aac_write *writecmd;
1378 struct aac_dev *dev = fib->dev;
1379 long ret;
1380
1381 aac_fib_init(fib);
1382 writecmd = (struct aac_write *) fib_data(fib);
1383 writecmd->command = cpu_to_le32(VM_CtBlockWrite);
1384 writecmd->cid = cpu_to_le32(scmd_id(cmd));
1385 writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
1386 writecmd->count = cpu_to_le32(count *
1387 dev->fsa_dev[scmd_id(cmd)].block_size);
1388 writecmd->sg.count = cpu_to_le32(1);
1389 /* ->stable is not used - it did mean which type of write */
1390
1391 ret = aac_build_sg(cmd, &writecmd->sg);
1392 if (ret < 0)
1393 return ret;
1394 fibsize = sizeof(struct aac_write) +
1395 ((le32_to_cpu(writecmd->sg.count) - 1) *
1396 sizeof (struct sgentry));
1397 BUG_ON (fibsize > (fib->dev->max_fib_size -
1398 sizeof(struct aac_fibhdr)));
1399 /*
1400 * Now send the Fib to the adapter
1401 */
1402 return aac_fib_send(ContainerCommand,
1403 fib,
1404 fibsize,
1405 FsaNormal,
1406 0, 1,
1407 (fib_callback) io_callback,
1408 (void *) cmd);
1409 }
1410
1411 static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
1412 {
1413 struct aac_srb * srbcmd;
1414 u32 flag;
1415 u32 timeout;
1416
1417 aac_fib_init(fib);
1418 switch(cmd->sc_data_direction){
1419 case DMA_TO_DEVICE:
1420 flag = SRB_DataOut;
1421 break;
1422 case DMA_BIDIRECTIONAL:
1423 flag = SRB_DataIn | SRB_DataOut;
1424 break;
1425 case DMA_FROM_DEVICE:
1426 flag = SRB_DataIn;
1427 break;
1428 case DMA_NONE:
1429 default: /* shuts up some versions of gcc */
1430 flag = SRB_NoDataXfer;
1431 break;
1432 }
1433
1434 srbcmd = (struct aac_srb*) fib_data(fib);
1435 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1436 srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
1437 srbcmd->id = cpu_to_le32(scmd_id(cmd));
1438 srbcmd->lun = cpu_to_le32(cmd->device->lun);
1439 srbcmd->flags = cpu_to_le32(flag);
1440 timeout = cmd->request->timeout/HZ;
1441 if (timeout == 0)
1442 timeout = 1;
1443 srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
1444 srbcmd->retry_limit = 0; /* Obsolete parameter */
1445 srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
1446 return srbcmd;
1447 }
1448
1449 static void aac_srb_callback(void *context, struct fib * fibptr);
1450
1451 static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
1452 {
1453 u16 fibsize;
1454 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1455 long ret;
1456
1457 ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
1458 if (ret < 0)
1459 return ret;
1460 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1461
1462 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1463 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1464 /*
1465 * Build Scatter/Gather list
1466 */
1467 fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
1468 ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
1469 sizeof (struct sgentry64));
1470 BUG_ON (fibsize > (fib->dev->max_fib_size -
1471 sizeof(struct aac_fibhdr)));
1472
1473 /*
1474 * Now send the Fib to the adapter
1475 */
1476 return aac_fib_send(ScsiPortCommand64, fib,
1477 fibsize, FsaNormal, 0, 1,
1478 (fib_callback) aac_srb_callback,
1479 (void *) cmd);
1480 }
1481
1482 static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
1483 {
1484 u16 fibsize;
1485 struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
1486 long ret;
1487
1488 ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
1489 if (ret < 0)
1490 return ret;
1491 srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
1492
1493 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1494 memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
1495 /*
1496 * Build Scatter/Gather list
1497 */
1498 fibsize = sizeof (struct aac_srb) +
1499 (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
1500 sizeof (struct sgentry));
1501 BUG_ON (fibsize > (fib->dev->max_fib_size -
1502 sizeof(struct aac_fibhdr)));
1503
1504 /*
1505 * Now send the Fib to the adapter
1506 */
1507 return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
1508 (fib_callback) aac_srb_callback, (void *) cmd);
1509 }
1510
1511 static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
1512 {
1513 if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
1514 (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
1515 return FAILED;
1516 return aac_scsi_32(fib, cmd);
1517 }
1518
1519 int aac_issue_bmic_identify(struct aac_dev *dev, u32 bus, u32 target)
1520 {
1521 struct fib *fibptr;
1522 struct aac_srb *srbcmd;
1523 struct sgmap64 *sg64;
1524 struct aac_ciss_identify_pd *identify_resp;
1525 dma_addr_t addr;
1526 u32 vbus, vid;
1527 u16 fibsize, datasize;
1528 int rcode = -ENOMEM;
1529
1530 fibptr = aac_fib_alloc(dev);
1531 if (!fibptr)
1532 goto out;
1533
1534 fibsize = sizeof(struct aac_srb) -
1535 sizeof(struct sgentry) + sizeof(struct sgentry64);
1536 datasize = sizeof(struct aac_ciss_identify_pd);
1537
1538 identify_resp = pci_alloc_consistent(dev->pdev, datasize, &addr);
1539
1540 if (!identify_resp)
1541 goto fib_free_ptr;
1542
1543 vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.VirtDeviceBus);
1544 vid = (u32)le16_to_cpu(dev->supplement_adapter_info.VirtDeviceTarget);
1545
1546 aac_fib_init(fibptr);
1547
1548 srbcmd = (struct aac_srb *) fib_data(fibptr);
1549 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1550 srbcmd->channel = cpu_to_le32(vbus);
1551 srbcmd->id = cpu_to_le32(vid);
1552 srbcmd->lun = 0;
1553 srbcmd->flags = cpu_to_le32(SRB_DataIn);
1554 srbcmd->timeout = cpu_to_le32(10);
1555 srbcmd->retry_limit = 0;
1556 srbcmd->cdb_size = cpu_to_le32(12);
1557 srbcmd->count = cpu_to_le32(datasize);
1558
1559 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1560 srbcmd->cdb[0] = 0x26;
1561 srbcmd->cdb[2] = (u8)((AAC_MAX_LUN + target) & 0x00FF);
1562 srbcmd->cdb[6] = CISS_IDENTIFY_PHYSICAL_DEVICE;
1563
1564 sg64 = (struct sgmap64 *)&srbcmd->sg;
1565 sg64->count = cpu_to_le32(1);
1566 sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16));
1567 sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
1568 sg64->sg[0].count = cpu_to_le32(datasize);
1569
1570 rcode = aac_fib_send(ScsiPortCommand64,
1571 fibptr, fibsize, FsaNormal, 1, 1, NULL, NULL);
1572
1573 if (identify_resp->current_queue_depth_limit <= 0 ||
1574 identify_resp->current_queue_depth_limit > 32)
1575 dev->hba_map[bus][target].qd_limit = 32;
1576 else
1577 dev->hba_map[bus][target].qd_limit =
1578 identify_resp->current_queue_depth_limit;
1579
1580 pci_free_consistent(dev->pdev, datasize, (void *)identify_resp, addr);
1581
1582 aac_fib_complete(fibptr);
1583
1584 fib_free_ptr:
1585 aac_fib_free(fibptr);
1586 out:
1587 return rcode;
1588 }
1589
1590 /**
1591 * aac_update hba_map()- update current hba map with data from FW
1592 * @dev: aac_dev structure
1593 * @phys_luns: FW information from report phys luns
1594 *
1595 * Update our hba map with the information gathered from the FW
1596 */
1597 void aac_update_hba_map(struct aac_dev *dev,
1598 struct aac_ciss_phys_luns_resp *phys_luns, int rescan)
1599 {
1600 /* ok and extended reporting */
1601 u32 lun_count, nexus;
1602 u32 i, bus, target;
1603 u8 expose_flag, attribs;
1604 u8 devtype;
1605
1606 lun_count = ((phys_luns->list_length[0] << 24)
1607 + (phys_luns->list_length[1] << 16)
1608 + (phys_luns->list_length[2] << 8)
1609 + (phys_luns->list_length[3])) / 24;
1610
1611 for (i = 0; i < lun_count; ++i) {
1612
1613 bus = phys_luns->lun[i].level2[1] & 0x3f;
1614 target = phys_luns->lun[i].level2[0];
1615 expose_flag = phys_luns->lun[i].bus >> 6;
1616 attribs = phys_luns->lun[i].node_ident[9];
1617 nexus = *((u32 *) &phys_luns->lun[i].node_ident[12]);
1618
1619 if (bus >= AAC_MAX_BUSES || target >= AAC_MAX_TARGETS)
1620 continue;
1621
1622 dev->hba_map[bus][target].expose = expose_flag;
1623
1624 if (expose_flag != 0) {
1625 devtype = AAC_DEVTYPE_RAID_MEMBER;
1626 goto update_devtype;
1627 }
1628
1629 if (nexus != 0 && (attribs & 8)) {
1630 devtype = AAC_DEVTYPE_NATIVE_RAW;
1631 dev->hba_map[bus][target].rmw_nexus =
1632 nexus;
1633 } else
1634 devtype = AAC_DEVTYPE_ARC_RAW;
1635
1636 if (devtype != AAC_DEVTYPE_NATIVE_RAW)
1637 goto update_devtype;
1638
1639 if (aac_issue_bmic_identify(dev, bus, target) < 0)
1640 dev->hba_map[bus][target].qd_limit = 32;
1641
1642 update_devtype:
1643 if (rescan == AAC_INIT)
1644 dev->hba_map[bus][target].devtype = devtype;
1645 else
1646 dev->hba_map[bus][target].new_devtype = devtype;
1647 }
1648 }
1649
1650 /**
1651 * aac_report_phys_luns() Process topology change
1652 * @dev: aac_dev structure
1653 * @fibptr: fib pointer
1654 *
1655 * Execute a CISS REPORT PHYS LUNS and process the results into
1656 * the current hba_map.
1657 */
1658 int aac_report_phys_luns(struct aac_dev *dev, struct fib *fibptr, int rescan)
1659 {
1660 int fibsize, datasize;
1661 struct aac_ciss_phys_luns_resp *phys_luns;
1662 struct aac_srb *srbcmd;
1663 struct sgmap64 *sg64;
1664 dma_addr_t addr;
1665 u32 vbus, vid;
1666 u32 rcode = 0;
1667
1668 /* Thor SA Firmware -> CISS_REPORT_PHYSICAL_LUNS */
1669 fibsize = sizeof(struct aac_srb) - sizeof(struct sgentry)
1670 + sizeof(struct sgentry64);
1671 datasize = sizeof(struct aac_ciss_phys_luns_resp)
1672 + (AAC_MAX_TARGETS - 1) * sizeof(struct _ciss_lun);
1673
1674 phys_luns = (struct aac_ciss_phys_luns_resp *) pci_alloc_consistent(
1675 dev->pdev, datasize, &addr);
1676
1677 if (phys_luns == NULL) {
1678 rcode = -ENOMEM;
1679 goto err_out;
1680 }
1681
1682 vbus = (u32) le16_to_cpu(
1683 dev->supplement_adapter_info.VirtDeviceBus);
1684 vid = (u32) le16_to_cpu(
1685 dev->supplement_adapter_info.VirtDeviceTarget);
1686
1687 aac_fib_init(fibptr);
1688
1689 srbcmd = (struct aac_srb *) fib_data(fibptr);
1690 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
1691 srbcmd->channel = cpu_to_le32(vbus);
1692 srbcmd->id = cpu_to_le32(vid);
1693 srbcmd->lun = 0;
1694 srbcmd->flags = cpu_to_le32(SRB_DataIn);
1695 srbcmd->timeout = cpu_to_le32(10);
1696 srbcmd->retry_limit = 0;
1697 srbcmd->cdb_size = cpu_to_le32(12);
1698 srbcmd->count = cpu_to_le32(datasize);
1699
1700 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
1701 srbcmd->cdb[0] = CISS_REPORT_PHYSICAL_LUNS;
1702 srbcmd->cdb[1] = 2; /* extended reporting */
1703 srbcmd->cdb[8] = (u8)(datasize >> 8);
1704 srbcmd->cdb[9] = (u8)(datasize);
1705
1706 sg64 = (struct sgmap64 *) &srbcmd->sg;
1707 sg64->count = cpu_to_le32(1);
1708 sg64->sg[0].addr[1] = cpu_to_le32(upper_32_bits(addr));
1709 sg64->sg[0].addr[0] = cpu_to_le32(lower_32_bits(addr));
1710 sg64->sg[0].count = cpu_to_le32(datasize);
1711
1712 rcode = aac_fib_send(ScsiPortCommand64, fibptr, fibsize,
1713 FsaNormal, 1, 1, NULL, NULL);
1714
1715 /* analyse data */
1716 if (rcode >= 0 && phys_luns->resp_flag == 2) {
1717 /* ok and extended reporting */
1718 aac_update_hba_map(dev, phys_luns, rescan);
1719 }
1720
1721 pci_free_consistent(dev->pdev, datasize, (void *) phys_luns, addr);
1722 err_out:
1723 return rcode;
1724 }
1725
1726 int aac_get_adapter_info(struct aac_dev* dev)
1727 {
1728 struct fib* fibptr;
1729 int rcode;
1730 u32 tmp, bus, target;
1731 struct aac_adapter_info *info;
1732 struct aac_bus_info *command;
1733 struct aac_bus_info_response *bus_info;
1734
1735 if (!(fibptr = aac_fib_alloc(dev)))
1736 return -ENOMEM;
1737
1738 aac_fib_init(fibptr);
1739 info = (struct aac_adapter_info *) fib_data(fibptr);
1740 memset(info,0,sizeof(*info));
1741
1742 rcode = aac_fib_send(RequestAdapterInfo,
1743 fibptr,
1744 sizeof(*info),
1745 FsaNormal,
1746 -1, 1, /* First `interrupt' command uses special wait */
1747 NULL,
1748 NULL);
1749
1750 if (rcode < 0) {
1751 /* FIB should be freed only after
1752 * getting the response from the F/W */
1753 if (rcode != -ERESTARTSYS) {
1754 aac_fib_complete(fibptr);
1755 aac_fib_free(fibptr);
1756 }
1757 return rcode;
1758 }
1759 memcpy(&dev->adapter_info, info, sizeof(*info));
1760
1761 dev->supplement_adapter_info.VirtDeviceBus = 0xffff;
1762 if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
1763 struct aac_supplement_adapter_info * sinfo;
1764
1765 aac_fib_init(fibptr);
1766
1767 sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
1768
1769 memset(sinfo,0,sizeof(*sinfo));
1770
1771 rcode = aac_fib_send(RequestSupplementAdapterInfo,
1772 fibptr,
1773 sizeof(*sinfo),
1774 FsaNormal,
1775 1, 1,
1776 NULL,
1777 NULL);
1778
1779 if (rcode >= 0)
1780 memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
1781 if (rcode == -ERESTARTSYS) {
1782 fibptr = aac_fib_alloc(dev);
1783 if (!fibptr)
1784 return -ENOMEM;
1785 }
1786
1787 }
1788
1789 /* reset all previous mapped devices (i.e. for init. after IOP_RESET) */
1790 for (bus = 0; bus < AAC_MAX_BUSES; bus++) {
1791 for (target = 0; target < AAC_MAX_TARGETS; target++) {
1792 dev->hba_map[bus][target].devtype = 0;
1793 dev->hba_map[bus][target].qd_limit = 0;
1794 }
1795 }
1796
1797 /*
1798 * GetBusInfo
1799 */
1800
1801 aac_fib_init(fibptr);
1802
1803 bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
1804
1805 memset(bus_info, 0, sizeof(*bus_info));
1806
1807 command = (struct aac_bus_info *)bus_info;
1808
1809 command->Command = cpu_to_le32(VM_Ioctl);
1810 command->ObjType = cpu_to_le32(FT_DRIVE);
1811 command->MethodId = cpu_to_le32(1);
1812 command->CtlCmd = cpu_to_le32(GetBusInfo);
1813
1814 rcode = aac_fib_send(ContainerCommand,
1815 fibptr,
1816 sizeof (*bus_info),
1817 FsaNormal,
1818 1, 1,
1819 NULL, NULL);
1820
1821 /* reasoned default */
1822 dev->maximum_num_physicals = 16;
1823 if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
1824 dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
1825 dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
1826 }
1827
1828 if (!dev->sync_mode && dev->sa_firmware &&
1829 dev->supplement_adapter_info.VirtDeviceBus != 0xffff) {
1830 /* Thor SA Firmware -> CISS_REPORT_PHYSICAL_LUNS */
1831 rcode = aac_report_phys_luns(dev, fibptr, AAC_INIT);
1832 }
1833
1834 if (!dev->in_reset) {
1835 char buffer[16];
1836 tmp = le32_to_cpu(dev->adapter_info.kernelrev);
1837 printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
1838 dev->name,
1839 dev->id,
1840 tmp>>24,
1841 (tmp>>16)&0xff,
1842 tmp&0xff,
1843 le32_to_cpu(dev->adapter_info.kernelbuild),
1844 (int)sizeof(dev->supplement_adapter_info.BuildDate),
1845 dev->supplement_adapter_info.BuildDate);
1846 tmp = le32_to_cpu(dev->adapter_info.monitorrev);
1847 printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
1848 dev->name, dev->id,
1849 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1850 le32_to_cpu(dev->adapter_info.monitorbuild));
1851 tmp = le32_to_cpu(dev->adapter_info.biosrev);
1852 printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
1853 dev->name, dev->id,
1854 tmp>>24,(tmp>>16)&0xff,tmp&0xff,
1855 le32_to_cpu(dev->adapter_info.biosbuild));
1856 buffer[0] = '\0';
1857 if (aac_get_serial_number(
1858 shost_to_class(dev->scsi_host_ptr), buffer))
1859 printk(KERN_INFO "%s%d: serial %s",
1860 dev->name, dev->id, buffer);
1861 if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
1862 printk(KERN_INFO "%s%d: TSID %.*s\n",
1863 dev->name, dev->id,
1864 (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
1865 dev->supplement_adapter_info.VpdInfo.Tsid);
1866 }
1867 if (!aac_check_reset || ((aac_check_reset == 1) &&
1868 (dev->supplement_adapter_info.SupportedOptions2 &
1869 AAC_OPTION_IGNORE_RESET))) {
1870 printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
1871 dev->name, dev->id);
1872 }
1873 }
1874
1875 dev->cache_protected = 0;
1876 dev->jbod = ((dev->supplement_adapter_info.FeatureBits &
1877 AAC_FEATURE_JBOD) != 0);
1878 dev->nondasd_support = 0;
1879 dev->raid_scsi_mode = 0;
1880 if(dev->adapter_info.options & AAC_OPT_NONDASD)
1881 dev->nondasd_support = 1;
1882
1883 /*
1884 * If the firmware supports ROMB RAID/SCSI mode and we are currently
1885 * in RAID/SCSI mode, set the flag. For now if in this mode we will
1886 * force nondasd support on. If we decide to allow the non-dasd flag
1887 * additional changes changes will have to be made to support
1888 * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
1889 * changed to support the new dev->raid_scsi_mode flag instead of
1890 * leaching off of the dev->nondasd_support flag. Also in linit.c the
1891 * function aac_detect will have to be modified where it sets up the
1892 * max number of channels based on the aac->nondasd_support flag only.
1893 */
1894 if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
1895 (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
1896 dev->nondasd_support = 1;
1897 dev->raid_scsi_mode = 1;
1898 }
1899 if (dev->raid_scsi_mode != 0)
1900 printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
1901 dev->name, dev->id);
1902
1903 if (nondasd != -1)
1904 dev->nondasd_support = (nondasd!=0);
1905 if (dev->nondasd_support && !dev->in_reset)
1906 printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
1907
1908 if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
1909 dev->needs_dac = 1;
1910 dev->dac_support = 0;
1911 if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
1912 (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
1913 if (!dev->in_reset)
1914 printk(KERN_INFO "%s%d: 64bit support enabled.\n",
1915 dev->name, dev->id);
1916 dev->dac_support = 1;
1917 }
1918
1919 if(dacmode != -1) {
1920 dev->dac_support = (dacmode!=0);
1921 }
1922
1923 /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
1924 if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
1925 & AAC_QUIRK_SCSI_32)) {
1926 dev->nondasd_support = 0;
1927 dev->jbod = 0;
1928 expose_physicals = 0;
1929 }
1930
1931 if(dev->dac_support != 0) {
1932 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64)) &&
1933 !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
1934 if (!dev->in_reset)
1935 printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
1936 dev->name, dev->id);
1937 } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32)) &&
1938 !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
1939 printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
1940 dev->name, dev->id);
1941 dev->dac_support = 0;
1942 } else {
1943 printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
1944 dev->name, dev->id);
1945 rcode = -ENOMEM;
1946 }
1947 }
1948 /*
1949 * Deal with configuring for the individualized limits of each packet
1950 * interface.
1951 */
1952 dev->a_ops.adapter_scsi = (dev->dac_support)
1953 ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
1954 ? aac_scsi_32_64
1955 : aac_scsi_64)
1956 : aac_scsi_32;
1957 if (dev->raw_io_interface) {
1958 dev->a_ops.adapter_bounds = (dev->raw_io_64)
1959 ? aac_bounds_64
1960 : aac_bounds_32;
1961 dev->a_ops.adapter_read = aac_read_raw_io;
1962 dev->a_ops.adapter_write = aac_write_raw_io;
1963 } else {
1964 dev->a_ops.adapter_bounds = aac_bounds_32;
1965 dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
1966 sizeof(struct aac_fibhdr) -
1967 sizeof(struct aac_write) + sizeof(struct sgentry)) /
1968 sizeof(struct sgentry);
1969 if (dev->dac_support) {
1970 dev->a_ops.adapter_read = aac_read_block64;
1971 dev->a_ops.adapter_write = aac_write_block64;
1972 /*
1973 * 38 scatter gather elements
1974 */
1975 dev->scsi_host_ptr->sg_tablesize =
1976 (dev->max_fib_size -
1977 sizeof(struct aac_fibhdr) -
1978 sizeof(struct aac_write64) +
1979 sizeof(struct sgentry64)) /
1980 sizeof(struct sgentry64);
1981 } else {
1982 dev->a_ops.adapter_read = aac_read_block;
1983 dev->a_ops.adapter_write = aac_write_block;
1984 }
1985 dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
1986 if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
1987 /*
1988 * Worst case size that could cause sg overflow when
1989 * we break up SG elements that are larger than 64KB.
1990 * Would be nice if we could tell the SCSI layer what
1991 * the maximum SG element size can be. Worst case is
1992 * (sg_tablesize-1) 4KB elements with one 64KB
1993 * element.
1994 * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
1995 */
1996 dev->scsi_host_ptr->max_sectors =
1997 (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
1998 }
1999 }
2000 /* FIB should be freed only after getting the response from the F/W */
2001 if (rcode != -ERESTARTSYS) {
2002 aac_fib_complete(fibptr);
2003 aac_fib_free(fibptr);
2004 }
2005
2006 return rcode;
2007 }
2008
2009
2010 static void io_callback(void *context, struct fib * fibptr)
2011 {
2012 struct aac_dev *dev;
2013 struct aac_read_reply *readreply;
2014 struct scsi_cmnd *scsicmd;
2015 u32 cid;
2016
2017 scsicmd = (struct scsi_cmnd *) context;
2018
2019 if (!aac_valid_context(scsicmd, fibptr))
2020 return;
2021
2022 dev = fibptr->dev;
2023 cid = scmd_id(scsicmd);
2024
2025 if (nblank(dprintk(x))) {
2026 u64 lba;
2027 switch (scsicmd->cmnd[0]) {
2028 case WRITE_6:
2029 case READ_6:
2030 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2031 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2032 break;
2033 case WRITE_16:
2034 case READ_16:
2035 lba = ((u64)scsicmd->cmnd[2] << 56) |
2036 ((u64)scsicmd->cmnd[3] << 48) |
2037 ((u64)scsicmd->cmnd[4] << 40) |
2038 ((u64)scsicmd->cmnd[5] << 32) |
2039 ((u64)scsicmd->cmnd[6] << 24) |
2040 (scsicmd->cmnd[7] << 16) |
2041 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2042 break;
2043 case WRITE_12:
2044 case READ_12:
2045 lba = ((u64)scsicmd->cmnd[2] << 24) |
2046 (scsicmd->cmnd[3] << 16) |
2047 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2048 break;
2049 default:
2050 lba = ((u64)scsicmd->cmnd[2] << 24) |
2051 (scsicmd->cmnd[3] << 16) |
2052 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2053 break;
2054 }
2055 printk(KERN_DEBUG
2056 "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
2057 smp_processor_id(), (unsigned long long)lba, jiffies);
2058 }
2059
2060 BUG_ON(fibptr == NULL);
2061
2062 scsi_dma_unmap(scsicmd);
2063
2064 readreply = (struct aac_read_reply *)fib_data(fibptr);
2065 switch (le32_to_cpu(readreply->status)) {
2066 case ST_OK:
2067 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2068 SAM_STAT_GOOD;
2069 dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
2070 break;
2071 case ST_NOT_READY:
2072 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2073 SAM_STAT_CHECK_CONDITION;
2074 set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
2075 SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
2076 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2077 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2078 SCSI_SENSE_BUFFERSIZE));
2079 break;
2080 case ST_MEDERR:
2081 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2082 SAM_STAT_CHECK_CONDITION;
2083 set_sense(&dev->fsa_dev[cid].sense_data, MEDIUM_ERROR,
2084 SENCODE_UNRECOVERED_READ_ERROR, ASENCODE_NO_SENSE, 0, 0);
2085 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2086 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2087 SCSI_SENSE_BUFFERSIZE));
2088 break;
2089 default:
2090 #ifdef AAC_DETAILED_STATUS_INFO
2091 printk(KERN_WARNING "io_callback: io failed, status = %d\n",
2092 le32_to_cpu(readreply->status));
2093 #endif
2094 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2095 SAM_STAT_CHECK_CONDITION;
2096 set_sense(&dev->fsa_dev[cid].sense_data,
2097 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2098 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2099 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2100 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2101 SCSI_SENSE_BUFFERSIZE));
2102 break;
2103 }
2104 aac_fib_complete(fibptr);
2105
2106 scsicmd->scsi_done(scsicmd);
2107 }
2108
2109 static int aac_read(struct scsi_cmnd * scsicmd)
2110 {
2111 u64 lba;
2112 u32 count;
2113 int status;
2114 struct aac_dev *dev;
2115 struct fib * cmd_fibcontext;
2116 int cid;
2117
2118 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2119 /*
2120 * Get block address and transfer length
2121 */
2122 switch (scsicmd->cmnd[0]) {
2123 case READ_6:
2124 dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
2125
2126 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
2127 (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2128 count = scsicmd->cmnd[4];
2129
2130 if (count == 0)
2131 count = 256;
2132 break;
2133 case READ_16:
2134 dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
2135
2136 lba = ((u64)scsicmd->cmnd[2] << 56) |
2137 ((u64)scsicmd->cmnd[3] << 48) |
2138 ((u64)scsicmd->cmnd[4] << 40) |
2139 ((u64)scsicmd->cmnd[5] << 32) |
2140 ((u64)scsicmd->cmnd[6] << 24) |
2141 (scsicmd->cmnd[7] << 16) |
2142 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2143 count = (scsicmd->cmnd[10] << 24) |
2144 (scsicmd->cmnd[11] << 16) |
2145 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2146 break;
2147 case READ_12:
2148 dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
2149
2150 lba = ((u64)scsicmd->cmnd[2] << 24) |
2151 (scsicmd->cmnd[3] << 16) |
2152 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2153 count = (scsicmd->cmnd[6] << 24) |
2154 (scsicmd->cmnd[7] << 16) |
2155 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2156 break;
2157 default:
2158 dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
2159
2160 lba = ((u64)scsicmd->cmnd[2] << 24) |
2161 (scsicmd->cmnd[3] << 16) |
2162 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2163 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2164 break;
2165 }
2166
2167 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2168 cid = scmd_id(scsicmd);
2169 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2170 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2171 SAM_STAT_CHECK_CONDITION;
2172 set_sense(&dev->fsa_dev[cid].sense_data,
2173 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2174 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2175 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2176 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2177 SCSI_SENSE_BUFFERSIZE));
2178 scsicmd->scsi_done(scsicmd);
2179 return 1;
2180 }
2181
2182 dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
2183 smp_processor_id(), (unsigned long long)lba, jiffies));
2184 if (aac_adapter_bounds(dev,scsicmd,lba))
2185 return 0;
2186 /*
2187 * Alocate and initialize a Fib
2188 */
2189 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2190
2191 status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
2192
2193 /*
2194 * Check that the command queued to the controller
2195 */
2196 if (status == -EINPROGRESS) {
2197 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2198 return 0;
2199 }
2200
2201 printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
2202 /*
2203 * For some reason, the Fib didn't queue, return QUEUE_FULL
2204 */
2205 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2206 scsicmd->scsi_done(scsicmd);
2207 aac_fib_complete(cmd_fibcontext);
2208 aac_fib_free(cmd_fibcontext);
2209 return 0;
2210 }
2211
2212 static int aac_write(struct scsi_cmnd * scsicmd)
2213 {
2214 u64 lba;
2215 u32 count;
2216 int fua;
2217 int status;
2218 struct aac_dev *dev;
2219 struct fib * cmd_fibcontext;
2220 int cid;
2221
2222 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
2223 /*
2224 * Get block address and transfer length
2225 */
2226 if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
2227 {
2228 lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
2229 count = scsicmd->cmnd[4];
2230 if (count == 0)
2231 count = 256;
2232 fua = 0;
2233 } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
2234 dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
2235
2236 lba = ((u64)scsicmd->cmnd[2] << 56) |
2237 ((u64)scsicmd->cmnd[3] << 48) |
2238 ((u64)scsicmd->cmnd[4] << 40) |
2239 ((u64)scsicmd->cmnd[5] << 32) |
2240 ((u64)scsicmd->cmnd[6] << 24) |
2241 (scsicmd->cmnd[7] << 16) |
2242 (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2243 count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
2244 (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
2245 fua = scsicmd->cmnd[1] & 0x8;
2246 } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
2247 dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
2248
2249 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
2250 | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2251 count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
2252 | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
2253 fua = scsicmd->cmnd[1] & 0x8;
2254 } else {
2255 dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
2256 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2257 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2258 fua = scsicmd->cmnd[1] & 0x8;
2259 }
2260
2261 if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
2262 cid = scmd_id(scsicmd);
2263 dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
2264 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2265 SAM_STAT_CHECK_CONDITION;
2266 set_sense(&dev->fsa_dev[cid].sense_data,
2267 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2268 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2269 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2270 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2271 SCSI_SENSE_BUFFERSIZE));
2272 scsicmd->scsi_done(scsicmd);
2273 return 1;
2274 }
2275
2276 dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
2277 smp_processor_id(), (unsigned long long)lba, jiffies));
2278 if (aac_adapter_bounds(dev,scsicmd,lba))
2279 return 0;
2280 /*
2281 * Allocate and initialize a Fib then setup a BlockWrite command
2282 */
2283 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
2284
2285 status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
2286
2287 /*
2288 * Check that the command queued to the controller
2289 */
2290 if (status == -EINPROGRESS) {
2291 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2292 return 0;
2293 }
2294
2295 printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
2296 /*
2297 * For some reason, the Fib didn't queue, return QUEUE_FULL
2298 */
2299 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
2300 scsicmd->scsi_done(scsicmd);
2301
2302 aac_fib_complete(cmd_fibcontext);
2303 aac_fib_free(cmd_fibcontext);
2304 return 0;
2305 }
2306
2307 static void synchronize_callback(void *context, struct fib *fibptr)
2308 {
2309 struct aac_synchronize_reply *synchronizereply;
2310 struct scsi_cmnd *cmd;
2311
2312 cmd = context;
2313
2314 if (!aac_valid_context(cmd, fibptr))
2315 return;
2316
2317 dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
2318 smp_processor_id(), jiffies));
2319 BUG_ON(fibptr == NULL);
2320
2321
2322 synchronizereply = fib_data(fibptr);
2323 if (le32_to_cpu(synchronizereply->status) == CT_OK)
2324 cmd->result = DID_OK << 16 |
2325 COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2326 else {
2327 struct scsi_device *sdev = cmd->device;
2328 struct aac_dev *dev = fibptr->dev;
2329 u32 cid = sdev_id(sdev);
2330 printk(KERN_WARNING
2331 "synchronize_callback: synchronize failed, status = %d\n",
2332 le32_to_cpu(synchronizereply->status));
2333 cmd->result = DID_OK << 16 |
2334 COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2335 set_sense(&dev->fsa_dev[cid].sense_data,
2336 HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
2337 ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
2338 memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2339 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2340 SCSI_SENSE_BUFFERSIZE));
2341 }
2342
2343 aac_fib_complete(fibptr);
2344 aac_fib_free(fibptr);
2345 cmd->scsi_done(cmd);
2346 }
2347
2348 static int aac_synchronize(struct scsi_cmnd *scsicmd)
2349 {
2350 int status;
2351 struct fib *cmd_fibcontext;
2352 struct aac_synchronize *synchronizecmd;
2353 struct scsi_cmnd *cmd;
2354 struct scsi_device *sdev = scsicmd->device;
2355 int active = 0;
2356 struct aac_dev *aac;
2357 u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
2358 (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
2359 u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
2360 unsigned long flags;
2361
2362 /*
2363 * Wait for all outstanding queued commands to complete to this
2364 * specific target (block).
2365 */
2366 spin_lock_irqsave(&sdev->list_lock, flags);
2367 list_for_each_entry(cmd, &sdev->cmd_list, list)
2368 if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
2369 u64 cmnd_lba;
2370 u32 cmnd_count;
2371
2372 if (cmd->cmnd[0] == WRITE_6) {
2373 cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
2374 (cmd->cmnd[2] << 8) |
2375 cmd->cmnd[3];
2376 cmnd_count = cmd->cmnd[4];
2377 if (cmnd_count == 0)
2378 cmnd_count = 256;
2379 } else if (cmd->cmnd[0] == WRITE_16) {
2380 cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
2381 ((u64)cmd->cmnd[3] << 48) |
2382 ((u64)cmd->cmnd[4] << 40) |
2383 ((u64)cmd->cmnd[5] << 32) |
2384 ((u64)cmd->cmnd[6] << 24) |
2385 (cmd->cmnd[7] << 16) |
2386 (cmd->cmnd[8] << 8) |
2387 cmd->cmnd[9];
2388 cmnd_count = (cmd->cmnd[10] << 24) |
2389 (cmd->cmnd[11] << 16) |
2390 (cmd->cmnd[12] << 8) |
2391 cmd->cmnd[13];
2392 } else if (cmd->cmnd[0] == WRITE_12) {
2393 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2394 (cmd->cmnd[3] << 16) |
2395 (cmd->cmnd[4] << 8) |
2396 cmd->cmnd[5];
2397 cmnd_count = (cmd->cmnd[6] << 24) |
2398 (cmd->cmnd[7] << 16) |
2399 (cmd->cmnd[8] << 8) |
2400 cmd->cmnd[9];
2401 } else if (cmd->cmnd[0] == WRITE_10) {
2402 cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
2403 (cmd->cmnd[3] << 16) |
2404 (cmd->cmnd[4] << 8) |
2405 cmd->cmnd[5];
2406 cmnd_count = (cmd->cmnd[7] << 8) |
2407 cmd->cmnd[8];
2408 } else
2409 continue;
2410 if (((cmnd_lba + cmnd_count) < lba) ||
2411 (count && ((lba + count) < cmnd_lba)))
2412 continue;
2413 ++active;
2414 break;
2415 }
2416
2417 spin_unlock_irqrestore(&sdev->list_lock, flags);
2418
2419 /*
2420 * Yield the processor (requeue for later)
2421 */
2422 if (active)
2423 return SCSI_MLQUEUE_DEVICE_BUSY;
2424
2425 aac = (struct aac_dev *)sdev->host->hostdata;
2426 if (aac->in_reset)
2427 return SCSI_MLQUEUE_HOST_BUSY;
2428
2429 /*
2430 * Allocate and initialize a Fib
2431 */
2432 if (!(cmd_fibcontext = aac_fib_alloc(aac)))
2433 return SCSI_MLQUEUE_HOST_BUSY;
2434
2435 aac_fib_init(cmd_fibcontext);
2436
2437 synchronizecmd = fib_data(cmd_fibcontext);
2438 synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
2439 synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
2440 synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
2441 synchronizecmd->count =
2442 cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
2443
2444 /*
2445 * Now send the Fib to the adapter
2446 */
2447 status = aac_fib_send(ContainerCommand,
2448 cmd_fibcontext,
2449 sizeof(struct aac_synchronize),
2450 FsaNormal,
2451 0, 1,
2452 (fib_callback)synchronize_callback,
2453 (void *)scsicmd);
2454
2455 /*
2456 * Check that the command queued to the controller
2457 */
2458 if (status == -EINPROGRESS) {
2459 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2460 return 0;
2461 }
2462
2463 printk(KERN_WARNING
2464 "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
2465 aac_fib_complete(cmd_fibcontext);
2466 aac_fib_free(cmd_fibcontext);
2467 return SCSI_MLQUEUE_HOST_BUSY;
2468 }
2469
2470 static void aac_start_stop_callback(void *context, struct fib *fibptr)
2471 {
2472 struct scsi_cmnd *scsicmd = context;
2473
2474 if (!aac_valid_context(scsicmd, fibptr))
2475 return;
2476
2477 BUG_ON(fibptr == NULL);
2478
2479 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
2480
2481 aac_fib_complete(fibptr);
2482 aac_fib_free(fibptr);
2483 scsicmd->scsi_done(scsicmd);
2484 }
2485
2486 static int aac_start_stop(struct scsi_cmnd *scsicmd)
2487 {
2488 int status;
2489 struct fib *cmd_fibcontext;
2490 struct aac_power_management *pmcmd;
2491 struct scsi_device *sdev = scsicmd->device;
2492 struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
2493
2494 if (!(aac->supplement_adapter_info.SupportedOptions2 &
2495 AAC_OPTION_POWER_MANAGEMENT)) {
2496 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2497 SAM_STAT_GOOD;
2498 scsicmd->scsi_done(scsicmd);
2499 return 0;
2500 }
2501
2502 if (aac->in_reset)
2503 return SCSI_MLQUEUE_HOST_BUSY;
2504
2505 /*
2506 * Allocate and initialize a Fib
2507 */
2508 cmd_fibcontext = aac_fib_alloc_tag(aac, scsicmd);
2509
2510 aac_fib_init(cmd_fibcontext);
2511
2512 pmcmd = fib_data(cmd_fibcontext);
2513 pmcmd->command = cpu_to_le32(VM_ContainerConfig);
2514 pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
2515 /* Eject bit ignored, not relevant */
2516 pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
2517 cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
2518 pmcmd->cid = cpu_to_le32(sdev_id(sdev));
2519 pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
2520 cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
2521
2522 /*
2523 * Now send the Fib to the adapter
2524 */
2525 status = aac_fib_send(ContainerCommand,
2526 cmd_fibcontext,
2527 sizeof(struct aac_power_management),
2528 FsaNormal,
2529 0, 1,
2530 (fib_callback)aac_start_stop_callback,
2531 (void *)scsicmd);
2532
2533 /*
2534 * Check that the command queued to the controller
2535 */
2536 if (status == -EINPROGRESS) {
2537 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
2538 return 0;
2539 }
2540
2541 aac_fib_complete(cmd_fibcontext);
2542 aac_fib_free(cmd_fibcontext);
2543 return SCSI_MLQUEUE_HOST_BUSY;
2544 }
2545
2546 /**
2547 * aac_scsi_cmd() - Process SCSI command
2548 * @scsicmd: SCSI command block
2549 *
2550 * Emulate a SCSI command and queue the required request for the
2551 * aacraid firmware.
2552 */
2553
2554 int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
2555 {
2556 u32 cid;
2557 struct Scsi_Host *host = scsicmd->device->host;
2558 struct aac_dev *dev = (struct aac_dev *)host->hostdata;
2559 struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
2560
2561 if (fsa_dev_ptr == NULL)
2562 return -1;
2563 /*
2564 * If the bus, id or lun is out of range, return fail
2565 * Test does not apply to ID 16, the pseudo id for the controller
2566 * itself.
2567 */
2568 cid = scmd_id(scsicmd);
2569 if (cid != host->this_id) {
2570 if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
2571 if((cid >= dev->maximum_num_containers) ||
2572 (scsicmd->device->lun != 0)) {
2573 scsicmd->result = DID_NO_CONNECT << 16;
2574 goto scsi_done_ret;
2575 }
2576
2577 /*
2578 * If the target container doesn't exist, it may have
2579 * been newly created
2580 */
2581 if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
2582 (fsa_dev_ptr[cid].sense_data.sense_key ==
2583 NOT_READY)) {
2584 switch (scsicmd->cmnd[0]) {
2585 case SERVICE_ACTION_IN_16:
2586 if (!(dev->raw_io_interface) ||
2587 !(dev->raw_io_64) ||
2588 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2589 break;
2590 case INQUIRY:
2591 case READ_CAPACITY:
2592 case TEST_UNIT_READY:
2593 if (dev->in_reset)
2594 return -1;
2595 return _aac_probe_container(scsicmd,
2596 aac_probe_container_callback2);
2597 default:
2598 break;
2599 }
2600 }
2601 } else { /* check for physical non-dasd devices */
2602 if (dev->nondasd_support || expose_physicals ||
2603 dev->jbod) {
2604 if (dev->in_reset)
2605 return -1;
2606 return aac_send_srb_fib(scsicmd);
2607 } else {
2608 scsicmd->result = DID_NO_CONNECT << 16;
2609 goto scsi_done_ret;
2610 }
2611 }
2612 }
2613 /*
2614 * else Command for the controller itself
2615 */
2616 else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
2617 (scsicmd->cmnd[0] != TEST_UNIT_READY))
2618 {
2619 dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
2620 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
2621 set_sense(&dev->fsa_dev[cid].sense_data,
2622 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
2623 ASENCODE_INVALID_COMMAND, 0, 0);
2624 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2625 min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
2626 SCSI_SENSE_BUFFERSIZE));
2627 goto scsi_done_ret;
2628 }
2629
2630 switch (scsicmd->cmnd[0]) {
2631 case READ_6:
2632 case READ_10:
2633 case READ_12:
2634 case READ_16:
2635 if (dev->in_reset)
2636 return -1;
2637 return aac_read(scsicmd);
2638
2639 case WRITE_6:
2640 case WRITE_10:
2641 case WRITE_12:
2642 case WRITE_16:
2643 if (dev->in_reset)
2644 return -1;
2645 return aac_write(scsicmd);
2646
2647 case SYNCHRONIZE_CACHE:
2648 if (((aac_cache & 6) == 6) && dev->cache_protected) {
2649 scsicmd->result = AAC_STAT_GOOD;
2650 break;
2651 }
2652 /* Issue FIB to tell Firmware to flush it's cache */
2653 if ((aac_cache & 6) != 2)
2654 return aac_synchronize(scsicmd);
2655 case INQUIRY:
2656 {
2657 struct inquiry_data inq_data;
2658
2659 dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
2660 memset(&inq_data, 0, sizeof (struct inquiry_data));
2661
2662 if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
2663 char *arr = (char *)&inq_data;
2664
2665 /* EVPD bit set */
2666 arr[0] = (scmd_id(scsicmd) == host->this_id) ?
2667 INQD_PDT_PROC : INQD_PDT_DA;
2668 if (scsicmd->cmnd[2] == 0) {
2669 /* supported vital product data pages */
2670 arr[3] = 3;
2671 arr[4] = 0x0;
2672 arr[5] = 0x80;
2673 arr[6] = 0x83;
2674 arr[1] = scsicmd->cmnd[2];
2675 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2676 sizeof(inq_data));
2677 scsicmd->result = AAC_STAT_GOOD;
2678 } else if (scsicmd->cmnd[2] == 0x80) {
2679 /* unit serial number page */
2680 arr[3] = setinqserial(dev, &arr[4],
2681 scmd_id(scsicmd));
2682 arr[1] = scsicmd->cmnd[2];
2683 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2684 sizeof(inq_data));
2685 if (aac_wwn != 2)
2686 return aac_get_container_serial(
2687 scsicmd);
2688 scsicmd->result = AAC_STAT_GOOD;
2689 } else if (scsicmd->cmnd[2] == 0x83) {
2690 /* vpd page 0x83 - Device Identification Page */
2691 char *sno = (char *)&inq_data;
2692 sno[3] = setinqserial(dev, &sno[4],
2693 scmd_id(scsicmd));
2694 if (aac_wwn != 2)
2695 return aac_get_container_serial(
2696 scsicmd);
2697 scsicmd->result = AAC_STAT_GOOD;
2698 } else {
2699 /* vpd page not implemented */
2700 scsicmd->result = DID_OK << 16 |
2701 COMMAND_COMPLETE << 8 |
2702 SAM_STAT_CHECK_CONDITION;
2703 set_sense(&dev->fsa_dev[cid].sense_data,
2704 ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
2705 ASENCODE_NO_SENSE, 7, 2);
2706 memcpy(scsicmd->sense_buffer,
2707 &dev->fsa_dev[cid].sense_data,
2708 min_t(size_t,
2709 sizeof(dev->fsa_dev[cid].sense_data),
2710 SCSI_SENSE_BUFFERSIZE));
2711 }
2712 break;
2713 }
2714 inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
2715 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 */
2716 inq_data.inqd_len = 31;
2717 /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
2718 inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
2719 /*
2720 * Set the Vendor, Product, and Revision Level
2721 * see: <vendor>.c i.e. aac.c
2722 */
2723 if (cid == host->this_id) {
2724 setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
2725 inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
2726 scsi_sg_copy_from_buffer(scsicmd, &inq_data,
2727 sizeof(inq_data));
2728 scsicmd->result = AAC_STAT_GOOD;
2729 break;
2730 }
2731 if (dev->in_reset)
2732 return -1;
2733 setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
2734 inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
2735 scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
2736 return aac_get_container_name(scsicmd);
2737 }
2738 case SERVICE_ACTION_IN_16:
2739 if (!(dev->raw_io_interface) ||
2740 !(dev->raw_io_64) ||
2741 ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
2742 break;
2743 {
2744 u64 capacity;
2745 char cp[13];
2746 unsigned int alloc_len;
2747
2748 dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
2749 capacity = fsa_dev_ptr[cid].size - 1;
2750 cp[0] = (capacity >> 56) & 0xff;
2751 cp[1] = (capacity >> 48) & 0xff;
2752 cp[2] = (capacity >> 40) & 0xff;
2753 cp[3] = (capacity >> 32) & 0xff;
2754 cp[4] = (capacity >> 24) & 0xff;
2755 cp[5] = (capacity >> 16) & 0xff;
2756 cp[6] = (capacity >> 8) & 0xff;
2757 cp[7] = (capacity >> 0) & 0xff;
2758 cp[8] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2759 cp[9] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2760 cp[10] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2761 cp[11] = (fsa_dev_ptr[cid].block_size) & 0xff;
2762 cp[12] = 0;
2763
2764 alloc_len = ((scsicmd->cmnd[10] << 24)
2765 + (scsicmd->cmnd[11] << 16)
2766 + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
2767
2768 alloc_len = min_t(size_t, alloc_len, sizeof(cp));
2769 scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
2770 if (alloc_len < scsi_bufflen(scsicmd))
2771 scsi_set_resid(scsicmd,
2772 scsi_bufflen(scsicmd) - alloc_len);
2773
2774 /* Do not cache partition table for arrays */
2775 scsicmd->device->removable = 1;
2776
2777 scsicmd->result = AAC_STAT_GOOD;
2778 break;
2779 }
2780
2781 case READ_CAPACITY:
2782 {
2783 u32 capacity;
2784 char cp[8];
2785
2786 dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
2787 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2788 capacity = fsa_dev_ptr[cid].size - 1;
2789 else
2790 capacity = (u32)-1;
2791
2792 cp[0] = (capacity >> 24) & 0xff;
2793 cp[1] = (capacity >> 16) & 0xff;
2794 cp[2] = (capacity >> 8) & 0xff;
2795 cp[3] = (capacity >> 0) & 0xff;
2796 cp[4] = (fsa_dev_ptr[cid].block_size >> 24) & 0xff;
2797 cp[5] = (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2798 cp[6] = (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2799 cp[7] = (fsa_dev_ptr[cid].block_size) & 0xff;
2800 scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
2801 /* Do not cache partition table for arrays */
2802 scsicmd->device->removable = 1;
2803 scsicmd->result = AAC_STAT_GOOD;
2804 break;
2805 }
2806
2807 case MODE_SENSE:
2808 {
2809 int mode_buf_length = 4;
2810 u32 capacity;
2811 aac_modep_data mpd;
2812
2813 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2814 capacity = fsa_dev_ptr[cid].size - 1;
2815 else
2816 capacity = (u32)-1;
2817
2818 dprintk((KERN_DEBUG "MODE SENSE command.\n"));
2819 memset((char *)&mpd, 0, sizeof(aac_modep_data));
2820
2821 /* Mode data length */
2822 mpd.hd.data_length = sizeof(mpd.hd) - 1;
2823 /* Medium type - default */
2824 mpd.hd.med_type = 0;
2825 /* Device-specific param,
2826 bit 8: 0/1 = write enabled/protected
2827 bit 4: 0/1 = FUA enabled */
2828 mpd.hd.dev_par = 0;
2829
2830 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2831 mpd.hd.dev_par = 0x10;
2832 if (scsicmd->cmnd[1] & 0x8)
2833 mpd.hd.bd_length = 0; /* Block descriptor length */
2834 else {
2835 mpd.hd.bd_length = sizeof(mpd.bd);
2836 mpd.hd.data_length += mpd.hd.bd_length;
2837 mpd.bd.block_length[0] =
2838 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2839 mpd.bd.block_length[1] =
2840 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2841 mpd.bd.block_length[2] =
2842 fsa_dev_ptr[cid].block_size & 0xff;
2843
2844 mpd.mpc_buf[0] = scsicmd->cmnd[2];
2845 if (scsicmd->cmnd[2] == 0x1C) {
2846 /* page length */
2847 mpd.mpc_buf[1] = 0xa;
2848 /* Mode data length */
2849 mpd.hd.data_length = 23;
2850 } else {
2851 /* Mode data length */
2852 mpd.hd.data_length = 15;
2853 }
2854
2855 if (capacity > 0xffffff) {
2856 mpd.bd.block_count[0] = 0xff;
2857 mpd.bd.block_count[1] = 0xff;
2858 mpd.bd.block_count[2] = 0xff;
2859 } else {
2860 mpd.bd.block_count[0] = (capacity >> 16) & 0xff;
2861 mpd.bd.block_count[1] = (capacity >> 8) & 0xff;
2862 mpd.bd.block_count[2] = capacity & 0xff;
2863 }
2864 }
2865 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2866 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2867 mpd.hd.data_length += 3;
2868 mpd.mpc_buf[0] = 8;
2869 mpd.mpc_buf[1] = 1;
2870 mpd.mpc_buf[2] = ((aac_cache & 6) == 2)
2871 ? 0 : 0x04; /* WCE */
2872 mode_buf_length = sizeof(mpd);
2873 }
2874
2875 if (mode_buf_length > scsicmd->cmnd[4])
2876 mode_buf_length = scsicmd->cmnd[4];
2877 else
2878 mode_buf_length = sizeof(mpd);
2879 scsi_sg_copy_from_buffer(scsicmd,
2880 (char *)&mpd,
2881 mode_buf_length);
2882 scsicmd->result = AAC_STAT_GOOD;
2883 break;
2884 }
2885 case MODE_SENSE_10:
2886 {
2887 u32 capacity;
2888 int mode_buf_length = 8;
2889 aac_modep10_data mpd10;
2890
2891 if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
2892 capacity = fsa_dev_ptr[cid].size - 1;
2893 else
2894 capacity = (u32)-1;
2895
2896 dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
2897 memset((char *)&mpd10, 0, sizeof(aac_modep10_data));
2898 /* Mode data length (MSB) */
2899 mpd10.hd.data_length[0] = 0;
2900 /* Mode data length (LSB) */
2901 mpd10.hd.data_length[1] = sizeof(mpd10.hd) - 1;
2902 /* Medium type - default */
2903 mpd10.hd.med_type = 0;
2904 /* Device-specific param,
2905 bit 8: 0/1 = write enabled/protected
2906 bit 4: 0/1 = FUA enabled */
2907 mpd10.hd.dev_par = 0;
2908
2909 if (dev->raw_io_interface && ((aac_cache & 5) != 1))
2910 mpd10.hd.dev_par = 0x10;
2911 mpd10.hd.rsrvd[0] = 0; /* reserved */
2912 mpd10.hd.rsrvd[1] = 0; /* reserved */
2913 if (scsicmd->cmnd[1] & 0x8) {
2914 /* Block descriptor length (MSB) */
2915 mpd10.hd.bd_length[0] = 0;
2916 /* Block descriptor length (LSB) */
2917 mpd10.hd.bd_length[1] = 0;
2918 } else {
2919 mpd10.hd.bd_length[0] = 0;
2920 mpd10.hd.bd_length[1] = sizeof(mpd10.bd);
2921
2922 mpd10.hd.data_length[1] += mpd10.hd.bd_length[1];
2923
2924 mpd10.bd.block_length[0] =
2925 (fsa_dev_ptr[cid].block_size >> 16) & 0xff;
2926 mpd10.bd.block_length[1] =
2927 (fsa_dev_ptr[cid].block_size >> 8) & 0xff;
2928 mpd10.bd.block_length[2] =
2929 fsa_dev_ptr[cid].block_size & 0xff;
2930
2931 if (capacity > 0xffffff) {
2932 mpd10.bd.block_count[0] = 0xff;
2933 mpd10.bd.block_count[1] = 0xff;
2934 mpd10.bd.block_count[2] = 0xff;
2935 } else {
2936 mpd10.bd.block_count[0] =
2937 (capacity >> 16) & 0xff;
2938 mpd10.bd.block_count[1] =
2939 (capacity >> 8) & 0xff;
2940 mpd10.bd.block_count[2] =
2941 capacity & 0xff;
2942 }
2943 }
2944 if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
2945 ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
2946 mpd10.hd.data_length[1] += 3;
2947 mpd10.mpc_buf[0] = 8;
2948 mpd10.mpc_buf[1] = 1;
2949 mpd10.mpc_buf[2] = ((aac_cache & 6) == 2)
2950 ? 0 : 0x04; /* WCE */
2951 mode_buf_length = sizeof(mpd10);
2952 if (mode_buf_length > scsicmd->cmnd[8])
2953 mode_buf_length = scsicmd->cmnd[8];
2954 }
2955 scsi_sg_copy_from_buffer(scsicmd,
2956 (char *)&mpd10,
2957 mode_buf_length);
2958
2959 scsicmd->result = AAC_STAT_GOOD;
2960 break;
2961 }
2962 case REQUEST_SENSE:
2963 dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
2964 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
2965 sizeof(struct sense_data));
2966 memset(&dev->fsa_dev[cid].sense_data, 0,
2967 sizeof(struct sense_data));
2968 scsicmd->result = AAC_STAT_GOOD;
2969 break;
2970
2971 case ALLOW_MEDIUM_REMOVAL:
2972 dprintk((KERN_DEBUG "LOCK command.\n"));
2973 if (scsicmd->cmnd[4])
2974 fsa_dev_ptr[cid].locked = 1;
2975 else
2976 fsa_dev_ptr[cid].locked = 0;
2977
2978 scsicmd->result = AAC_STAT_GOOD;
2979 break;
2980 /*
2981 * These commands are all No-Ops
2982 */
2983 case TEST_UNIT_READY:
2984 if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
2985 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
2986 SAM_STAT_CHECK_CONDITION;
2987 set_sense(&dev->fsa_dev[cid].sense_data,
2988 NOT_READY, SENCODE_BECOMING_READY,
2989 ASENCODE_BECOMING_READY, 0, 0);
2990 memcpy(scsicmd->sense_buffer,
2991 &dev->fsa_dev[cid].sense_data,
2992 min_t(size_t,
2993 sizeof(dev->fsa_dev[cid].sense_data),
2994 SCSI_SENSE_BUFFERSIZE));
2995 break;
2996 }
2997 case RESERVE:
2998 case RELEASE:
2999 case REZERO_UNIT:
3000 case REASSIGN_BLOCKS:
3001 case SEEK_10:
3002 scsicmd->result = AAC_STAT_GOOD;
3003 break;
3004
3005 case START_STOP:
3006 return aac_start_stop(scsicmd);
3007
3008 /* FALLTHRU */
3009 default:
3010 /*
3011 * Unhandled commands
3012 */
3013 dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n",
3014 scsicmd->cmnd[0]));
3015 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
3016 SAM_STAT_CHECK_CONDITION;
3017 set_sense(&dev->fsa_dev[cid].sense_data,
3018 ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
3019 ASENCODE_INVALID_COMMAND, 0, 0);
3020 memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
3021 min_t(size_t,
3022 sizeof(dev->fsa_dev[cid].sense_data),
3023 SCSI_SENSE_BUFFERSIZE));
3024 }
3025
3026 scsi_done_ret:
3027
3028 scsicmd->scsi_done(scsicmd);
3029 return 0;
3030 }
3031
3032 static int query_disk(struct aac_dev *dev, void __user *arg)
3033 {
3034 struct aac_query_disk qd;
3035 struct fsa_dev_info *fsa_dev_ptr;
3036
3037 fsa_dev_ptr = dev->fsa_dev;
3038 if (!fsa_dev_ptr)
3039 return -EBUSY;
3040 if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
3041 return -EFAULT;
3042 if (qd.cnum == -1)
3043 qd.cnum = qd.id;
3044 else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
3045 {
3046 if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
3047 return -EINVAL;
3048 qd.instance = dev->scsi_host_ptr->host_no;
3049 qd.bus = 0;
3050 qd.id = CONTAINER_TO_ID(qd.cnum);
3051 qd.lun = CONTAINER_TO_LUN(qd.cnum);
3052 }
3053 else return -EINVAL;
3054
3055 qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
3056 qd.locked = fsa_dev_ptr[qd.cnum].locked;
3057 qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
3058
3059 if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
3060 qd.unmapped = 1;
3061 else
3062 qd.unmapped = 0;
3063
3064 strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
3065 min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
3066
3067 if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
3068 return -EFAULT;
3069 return 0;
3070 }
3071
3072 static int force_delete_disk(struct aac_dev *dev, void __user *arg)
3073 {
3074 struct aac_delete_disk dd;
3075 struct fsa_dev_info *fsa_dev_ptr;
3076
3077 fsa_dev_ptr = dev->fsa_dev;
3078 if (!fsa_dev_ptr)
3079 return -EBUSY;
3080
3081 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3082 return -EFAULT;
3083
3084 if (dd.cnum >= dev->maximum_num_containers)
3085 return -EINVAL;
3086 /*
3087 * Mark this container as being deleted.
3088 */
3089 fsa_dev_ptr[dd.cnum].deleted = 1;
3090 /*
3091 * Mark the container as no longer valid
3092 */
3093 fsa_dev_ptr[dd.cnum].valid = 0;
3094 return 0;
3095 }
3096
3097 static int delete_disk(struct aac_dev *dev, void __user *arg)
3098 {
3099 struct aac_delete_disk dd;
3100 struct fsa_dev_info *fsa_dev_ptr;
3101
3102 fsa_dev_ptr = dev->fsa_dev;
3103 if (!fsa_dev_ptr)
3104 return -EBUSY;
3105
3106 if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
3107 return -EFAULT;
3108
3109 if (dd.cnum >= dev->maximum_num_containers)
3110 return -EINVAL;
3111 /*
3112 * If the container is locked, it can not be deleted by the API.
3113 */
3114 if (fsa_dev_ptr[dd.cnum].locked)
3115 return -EBUSY;
3116 else {
3117 /*
3118 * Mark the container as no longer being valid.
3119 */
3120 fsa_dev_ptr[dd.cnum].valid = 0;
3121 fsa_dev_ptr[dd.cnum].devname[0] = '\0';
3122 return 0;
3123 }
3124 }
3125
3126 int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
3127 {
3128 switch (cmd) {
3129 case FSACTL_QUERY_DISK:
3130 return query_disk(dev, arg);
3131 case FSACTL_DELETE_DISK:
3132 return delete_disk(dev, arg);
3133 case FSACTL_FORCE_DELETE_DISK:
3134 return force_delete_disk(dev, arg);
3135 case FSACTL_GET_CONTAINERS:
3136 return aac_get_containers(dev);
3137 default:
3138 return -ENOTTY;
3139 }
3140 }
3141
3142 /**
3143 *
3144 * aac_srb_callback
3145 * @context: the context set in the fib - here it is scsi cmd
3146 * @fibptr: pointer to the fib
3147 *
3148 * Handles the completion of a scsi command to a non dasd device
3149 *
3150 */
3151
3152 static void aac_srb_callback(void *context, struct fib * fibptr)
3153 {
3154 struct aac_dev *dev;
3155 struct aac_srb_reply *srbreply;
3156 struct scsi_cmnd *scsicmd;
3157
3158 scsicmd = (struct scsi_cmnd *) context;
3159
3160 if (!aac_valid_context(scsicmd, fibptr))
3161 return;
3162
3163 BUG_ON(fibptr == NULL);
3164
3165 dev = fibptr->dev;
3166
3167 srbreply = (struct aac_srb_reply *) fib_data(fibptr);
3168
3169 scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
3170
3171 if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
3172 /* fast response */
3173 srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
3174 srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
3175 } else {
3176 /*
3177 * Calculate resid for sg
3178 */
3179 scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
3180 - le32_to_cpu(srbreply->data_xfer_length));
3181 }
3182
3183
3184 scsi_dma_unmap(scsicmd);
3185
3186 /* expose physical device if expose_physicald flag is on */
3187 if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
3188 && expose_physicals > 0)
3189 aac_expose_phy_device(scsicmd);
3190
3191 /*
3192 * First check the fib status
3193 */
3194
3195 if (le32_to_cpu(srbreply->status) != ST_OK) {
3196 int len;
3197
3198 pr_warn("aac_srb_callback: srb failed, status = %d\n",
3199 le32_to_cpu(srbreply->status));
3200 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3201 SCSI_SENSE_BUFFERSIZE);
3202 scsicmd->result = DID_ERROR << 16
3203 | COMMAND_COMPLETE << 8
3204 | SAM_STAT_CHECK_CONDITION;
3205 memcpy(scsicmd->sense_buffer,
3206 srbreply->sense_data, len);
3207 }
3208
3209 /*
3210 * Next check the srb status
3211 */
3212 switch ((le32_to_cpu(srbreply->srb_status))&0x3f) {
3213 case SRB_STATUS_ERROR_RECOVERY:
3214 case SRB_STATUS_PENDING:
3215 case SRB_STATUS_SUCCESS:
3216 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3217 break;
3218 case SRB_STATUS_DATA_OVERRUN:
3219 switch (scsicmd->cmnd[0]) {
3220 case READ_6:
3221 case WRITE_6:
3222 case READ_10:
3223 case WRITE_10:
3224 case READ_12:
3225 case WRITE_12:
3226 case READ_16:
3227 case WRITE_16:
3228 if (le32_to_cpu(srbreply->data_xfer_length)
3229 < scsicmd->underflow)
3230 pr_warn("aacraid: SCSI CMD underflow\n");
3231 else
3232 pr_warn("aacraid: SCSI CMD Data Overrun\n");
3233 scsicmd->result = DID_ERROR << 16
3234 | COMMAND_COMPLETE << 8;
3235 break;
3236 case INQUIRY:
3237 scsicmd->result = DID_OK << 16
3238 | COMMAND_COMPLETE << 8;
3239 break;
3240 default:
3241 scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
3242 break;
3243 }
3244 break;
3245 case SRB_STATUS_ABORTED:
3246 scsicmd->result = DID_ABORT << 16 | ABORT << 8;
3247 break;
3248 case SRB_STATUS_ABORT_FAILED:
3249 /*
3250 * Not sure about this one - but assuming the
3251 * hba was trying to abort for some reason
3252 */
3253 scsicmd->result = DID_ERROR << 16 | ABORT << 8;
3254 break;
3255 case SRB_STATUS_PARITY_ERROR:
3256 scsicmd->result = DID_PARITY << 16
3257 | MSG_PARITY_ERROR << 8;
3258 break;
3259 case SRB_STATUS_NO_DEVICE:
3260 case SRB_STATUS_INVALID_PATH_ID:
3261 case SRB_STATUS_INVALID_TARGET_ID:
3262 case SRB_STATUS_INVALID_LUN:
3263 case SRB_STATUS_SELECTION_TIMEOUT:
3264 scsicmd->result = DID_NO_CONNECT << 16
3265 | COMMAND_COMPLETE << 8;
3266 break;
3267
3268 case SRB_STATUS_COMMAND_TIMEOUT:
3269 case SRB_STATUS_TIMEOUT:
3270 scsicmd->result = DID_TIME_OUT << 16
3271 | COMMAND_COMPLETE << 8;
3272 break;
3273
3274 case SRB_STATUS_BUSY:
3275 scsicmd->result = DID_BUS_BUSY << 16
3276 | COMMAND_COMPLETE << 8;
3277 break;
3278
3279 case SRB_STATUS_BUS_RESET:
3280 scsicmd->result = DID_RESET << 16
3281 | COMMAND_COMPLETE << 8;
3282 break;
3283
3284 case SRB_STATUS_MESSAGE_REJECTED:
3285 scsicmd->result = DID_ERROR << 16
3286 | MESSAGE_REJECT << 8;
3287 break;
3288 case SRB_STATUS_REQUEST_FLUSHED:
3289 case SRB_STATUS_ERROR:
3290 case SRB_STATUS_INVALID_REQUEST:
3291 case SRB_STATUS_REQUEST_SENSE_FAILED:
3292 case SRB_STATUS_NO_HBA:
3293 case SRB_STATUS_UNEXPECTED_BUS_FREE:
3294 case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
3295 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
3296 case SRB_STATUS_DELAYED_RETRY:
3297 case SRB_STATUS_BAD_FUNCTION:
3298 case SRB_STATUS_NOT_STARTED:
3299 case SRB_STATUS_NOT_IN_USE:
3300 case SRB_STATUS_FORCE_ABORT:
3301 case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
3302 default:
3303 #ifdef AAC_DETAILED_STATUS_INFO
3304 pr_info("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x -scsi status 0x%x\n",
3305 le32_to_cpu(srbreply->srb_status) & 0x3F,
3306 aac_get_status_string(
3307 le32_to_cpu(srbreply->srb_status) & 0x3F),
3308 scsicmd->cmnd[0],
3309 le32_to_cpu(srbreply->scsi_status));
3310 #endif
3311 /*
3312 * When the CC bit is SET by the host in ATA pass thru CDB,
3313 * driver is supposed to return DID_OK
3314 *
3315 * When the CC bit is RESET by the host, driver should
3316 * return DID_ERROR
3317 */
3318 if ((scsicmd->cmnd[0] == ATA_12)
3319 || (scsicmd->cmnd[0] == ATA_16)) {
3320
3321 if (scsicmd->cmnd[2] & (0x01 << 5)) {
3322 scsicmd->result = DID_OK << 16
3323 | COMMAND_COMPLETE << 8;
3324 break;
3325 } else {
3326 scsicmd->result = DID_ERROR << 16
3327 | COMMAND_COMPLETE << 8;
3328 break;
3329 }
3330 } else {
3331 scsicmd->result = DID_ERROR << 16
3332 | COMMAND_COMPLETE << 8;
3333 break;
3334 }
3335 }
3336 if (le32_to_cpu(srbreply->scsi_status)
3337 == SAM_STAT_CHECK_CONDITION) {
3338 int len;
3339
3340 scsicmd->result |= SAM_STAT_CHECK_CONDITION;
3341 len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
3342 SCSI_SENSE_BUFFERSIZE);
3343 #ifdef AAC_DETAILED_STATUS_INFO
3344 pr_warn("aac_srb_callback: check condition, status = %d len=%d\n",
3345 le32_to_cpu(srbreply->status), len);
3346 #endif
3347 memcpy(scsicmd->sense_buffer,
3348 srbreply->sense_data, len);
3349 }
3350
3351 /*
3352 * OR in the scsi status (already shifted up a bit)
3353 */
3354 scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
3355
3356 aac_fib_complete(fibptr);
3357 scsicmd->scsi_done(scsicmd);
3358 }
3359
3360 /**
3361 *
3362 * aac_send_scb_fib
3363 * @scsicmd: the scsi command block
3364 *
3365 * This routine will form a FIB and fill in the aac_srb from the
3366 * scsicmd passed in.
3367 */
3368
3369 static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
3370 {
3371 struct fib* cmd_fibcontext;
3372 struct aac_dev* dev;
3373 int status;
3374
3375 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3376 if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
3377 scsicmd->device->lun > 7) {
3378 scsicmd->result = DID_NO_CONNECT << 16;
3379 scsicmd->scsi_done(scsicmd);
3380 return 0;
3381 }
3382
3383 /*
3384 * Allocate and initialize a Fib then setup a BlockWrite command
3385 */
3386 cmd_fibcontext = aac_fib_alloc_tag(dev, scsicmd);
3387
3388 status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
3389
3390 /*
3391 * Check that the command queued to the controller
3392 */
3393 if (status == -EINPROGRESS) {
3394 scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
3395 return 0;
3396 }
3397
3398 printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
3399 aac_fib_complete(cmd_fibcontext);
3400 aac_fib_free(cmd_fibcontext);
3401
3402 return -1;
3403 }
3404
3405 static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
3406 {
3407 struct aac_dev *dev;
3408 unsigned long byte_count = 0;
3409 int nseg;
3410
3411 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3412 // Get rid of old data
3413 psg->count = 0;
3414 psg->sg[0].addr = 0;
3415 psg->sg[0].count = 0;
3416
3417 nseg = scsi_dma_map(scsicmd);
3418 if (nseg < 0)
3419 return nseg;
3420 if (nseg) {
3421 struct scatterlist *sg;
3422 int i;
3423
3424 psg->count = cpu_to_le32(nseg);
3425
3426 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3427 psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
3428 psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
3429 byte_count += sg_dma_len(sg);
3430 }
3431 /* hba wants the size to be exact */
3432 if (byte_count > scsi_bufflen(scsicmd)) {
3433 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3434 (byte_count - scsi_bufflen(scsicmd));
3435 psg->sg[i-1].count = cpu_to_le32(temp);
3436 byte_count = scsi_bufflen(scsicmd);
3437 }
3438 /* Check for command underflow */
3439 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3440 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3441 byte_count, scsicmd->underflow);
3442 }
3443 }
3444 return byte_count;
3445 }
3446
3447
3448 static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
3449 {
3450 struct aac_dev *dev;
3451 unsigned long byte_count = 0;
3452 u64 addr;
3453 int nseg;
3454
3455 dev = (struct aac_dev *)scsicmd->device->host->hostdata;
3456 // Get rid of old data
3457 psg->count = 0;
3458 psg->sg[0].addr[0] = 0;
3459 psg->sg[0].addr[1] = 0;
3460 psg->sg[0].count = 0;
3461
3462 nseg = scsi_dma_map(scsicmd);
3463 if (nseg < 0)
3464 return nseg;
3465 if (nseg) {
3466 struct scatterlist *sg;
3467 int i;
3468
3469 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3470 int count = sg_dma_len(sg);
3471 addr = sg_dma_address(sg);
3472 psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
3473 psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
3474 psg->sg[i].count = cpu_to_le32(count);
3475 byte_count += count;
3476 }
3477 psg->count = cpu_to_le32(nseg);
3478 /* hba wants the size to be exact */
3479 if (byte_count > scsi_bufflen(scsicmd)) {
3480 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3481 (byte_count - scsi_bufflen(scsicmd));
3482 psg->sg[i-1].count = cpu_to_le32(temp);
3483 byte_count = scsi_bufflen(scsicmd);
3484 }
3485 /* Check for command underflow */
3486 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3487 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3488 byte_count, scsicmd->underflow);
3489 }
3490 }
3491 return byte_count;
3492 }
3493
3494 static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
3495 {
3496 unsigned long byte_count = 0;
3497 int nseg;
3498
3499 // Get rid of old data
3500 psg->count = 0;
3501 psg->sg[0].next = 0;
3502 psg->sg[0].prev = 0;
3503 psg->sg[0].addr[0] = 0;
3504 psg->sg[0].addr[1] = 0;
3505 psg->sg[0].count = 0;
3506 psg->sg[0].flags = 0;
3507
3508 nseg = scsi_dma_map(scsicmd);
3509 if (nseg < 0)
3510 return nseg;
3511 if (nseg) {
3512 struct scatterlist *sg;
3513 int i;
3514
3515 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3516 int count = sg_dma_len(sg);
3517 u64 addr = sg_dma_address(sg);
3518 psg->sg[i].next = 0;
3519 psg->sg[i].prev = 0;
3520 psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
3521 psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
3522 psg->sg[i].count = cpu_to_le32(count);
3523 psg->sg[i].flags = 0;
3524 byte_count += count;
3525 }
3526 psg->count = cpu_to_le32(nseg);
3527 /* hba wants the size to be exact */
3528 if (byte_count > scsi_bufflen(scsicmd)) {
3529 u32 temp = le32_to_cpu(psg->sg[i-1].count) -
3530 (byte_count - scsi_bufflen(scsicmd));
3531 psg->sg[i-1].count = cpu_to_le32(temp);
3532 byte_count = scsi_bufflen(scsicmd);
3533 }
3534 /* Check for command underflow */
3535 if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
3536 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3537 byte_count, scsicmd->underflow);
3538 }
3539 }
3540 return byte_count;
3541 }
3542
3543 static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
3544 struct aac_raw_io2 *rio2, int sg_max)
3545 {
3546 unsigned long byte_count = 0;
3547 int nseg;
3548
3549 nseg = scsi_dma_map(scsicmd);
3550 if (nseg < 0)
3551 return nseg;
3552 if (nseg) {
3553 struct scatterlist *sg;
3554 int i, conformable = 0;
3555 u32 min_size = PAGE_SIZE, cur_size;
3556
3557 scsi_for_each_sg(scsicmd, sg, nseg, i) {
3558 int count = sg_dma_len(sg);
3559 u64 addr = sg_dma_address(sg);
3560
3561 BUG_ON(i >= sg_max);
3562 rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
3563 rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
3564 cur_size = cpu_to_le32(count);
3565 rio2->sge[i].length = cur_size;
3566 rio2->sge[i].flags = 0;
3567 if (i == 0) {
3568 conformable = 1;
3569 rio2->sgeFirstSize = cur_size;
3570 } else if (i == 1) {
3571 rio2->sgeNominalSize = cur_size;
3572 min_size = cur_size;
3573 } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
3574 conformable = 0;
3575 if (cur_size < min_size)
3576 min_size = cur_size;
3577 }
3578 byte_count += count;
3579 }
3580
3581 /* hba wants the size to be exact */
3582 if (byte_count > scsi_bufflen(scsicmd)) {
3583 u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
3584 (byte_count - scsi_bufflen(scsicmd));
3585 rio2->sge[i-1].length = cpu_to_le32(temp);
3586 byte_count = scsi_bufflen(scsicmd);
3587 }
3588
3589 rio2->sgeCnt = cpu_to_le32(nseg);
3590 rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
3591 /* not conformable: evaluate required sg elements */
3592 if (!conformable) {
3593 int j, nseg_new = nseg, err_found;
3594 for (i = min_size / PAGE_SIZE; i >= 1; --i) {
3595 err_found = 0;
3596 nseg_new = 2;
3597 for (j = 1; j < nseg - 1; ++j) {
3598 if (rio2->sge[j].length % (i*PAGE_SIZE)) {
3599 err_found = 1;
3600 break;
3601 }
3602 nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
3603 }
3604 if (!err_found)
3605 break;
3606 }
3607 if (i > 0 && nseg_new <= sg_max)
3608 aac_convert_sgraw2(rio2, i, nseg, nseg_new);
3609 } else
3610 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3611
3612 /* Check for command underflow */
3613 if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
3614 printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
3615 byte_count, scsicmd->underflow);
3616 }
3617 }
3618
3619 return byte_count;
3620 }
3621
3622 static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
3623 {
3624 struct sge_ieee1212 *sge;
3625 int i, j, pos;
3626 u32 addr_low;
3627
3628 if (aac_convert_sgl == 0)
3629 return 0;
3630
3631 sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
3632 if (sge == NULL)
3633 return -1;
3634
3635 for (i = 1, pos = 1; i < nseg-1; ++i) {
3636 for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
3637 addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
3638 sge[pos].addrLow = addr_low;
3639 sge[pos].addrHigh = rio2->sge[i].addrHigh;
3640 if (addr_low < rio2->sge[i].addrLow)
3641 sge[pos].addrHigh++;
3642 sge[pos].length = pages * PAGE_SIZE;
3643 sge[pos].flags = 0;
3644 pos++;
3645 }
3646 }
3647 sge[pos] = rio2->sge[nseg-1];
3648 memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
3649
3650 kfree(sge);
3651 rio2->sgeCnt = cpu_to_le32(nseg_new);
3652 rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
3653 rio2->sgeNominalSize = pages * PAGE_SIZE;
3654 return 0;
3655 }
3656
3657 #ifdef AAC_DETAILED_STATUS_INFO
3658
3659 struct aac_srb_status_info {
3660 u32 status;
3661 char *str;
3662 };
3663
3664
3665 static struct aac_srb_status_info srb_status_info[] = {
3666 { SRB_STATUS_PENDING, "Pending Status"},
3667 { SRB_STATUS_SUCCESS, "Success"},
3668 { SRB_STATUS_ABORTED, "Aborted Command"},
3669 { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
3670 { SRB_STATUS_ERROR, "Error Event"},
3671 { SRB_STATUS_BUSY, "Device Busy"},
3672 { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
3673 { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
3674 { SRB_STATUS_NO_DEVICE, "No Device"},
3675 { SRB_STATUS_TIMEOUT, "Timeout"},
3676 { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
3677 { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
3678 { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
3679 { SRB_STATUS_BUS_RESET, "Bus Reset"},
3680 { SRB_STATUS_PARITY_ERROR, "Parity Error"},
3681 { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
3682 { SRB_STATUS_NO_HBA, "No HBA"},
3683 { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
3684 { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
3685 { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
3686 { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
3687 { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
3688 { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
3689 { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
3690 { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
3691 { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
3692 { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
3693 { SRB_STATUS_NOT_STARTED, "Not Started"},
3694 { SRB_STATUS_NOT_IN_USE, "Not In Use"},
3695 { SRB_STATUS_FORCE_ABORT, "Force Abort"},
3696 { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
3697 { 0xff, "Unknown Error"}
3698 };
3699
3700 char *aac_get_status_string(u32 status)
3701 {
3702 int i;
3703
3704 for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
3705 if (srb_status_info[i].status == status)
3706 return srb_status_info[i].str;
3707
3708 return "Bad Status Code";
3709 }
3710
3711 #endif