PageRenderTime 78ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/aacraid/aachba.c

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