aachba.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000 Adaptec, Inc. (aacraid@adaptec.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/sched.h>
  29. #include <linux/pci.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/slab.h>
  32. #include <linux/completion.h>
  33. #include <linux/blkdev.h>
  34. #include <asm/semaphore.h>
  35. #include <asm/uaccess.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_cmnd.h>
  38. #include <scsi/scsi_device.h>
  39. #include <scsi/scsi_host.h>
  40. #include "aacraid.h"
  41. /* values for inqd_pdt: Peripheral device type in plain English */
  42. #define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
  43. #define INQD_PDT_PROC 0x03 /* Processor device */
  44. #define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
  45. #define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
  46. #define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
  47. #define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
  48. #define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
  49. #define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
  50. #define MAX_FIB_DATA (sizeof(struct hw_fib) - sizeof(FIB_HEADER))
  51. #define MAX_DRIVER_SG_SEGMENT_COUNT 17
  52. /*
  53. * Sense codes
  54. */
  55. #define SENCODE_NO_SENSE 0x00
  56. #define SENCODE_END_OF_DATA 0x00
  57. #define SENCODE_BECOMING_READY 0x04
  58. #define SENCODE_INIT_CMD_REQUIRED 0x04
  59. #define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
  60. #define SENCODE_INVALID_COMMAND 0x20
  61. #define SENCODE_LBA_OUT_OF_RANGE 0x21
  62. #define SENCODE_INVALID_CDB_FIELD 0x24
  63. #define SENCODE_LUN_NOT_SUPPORTED 0x25
  64. #define SENCODE_INVALID_PARAM_FIELD 0x26
  65. #define SENCODE_PARAM_NOT_SUPPORTED 0x26
  66. #define SENCODE_PARAM_VALUE_INVALID 0x26
  67. #define SENCODE_RESET_OCCURRED 0x29
  68. #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
  69. #define SENCODE_INQUIRY_DATA_CHANGED 0x3F
  70. #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
  71. #define SENCODE_DIAGNOSTIC_FAILURE 0x40
  72. #define SENCODE_INTERNAL_TARGET_FAILURE 0x44
  73. #define SENCODE_INVALID_MESSAGE_ERROR 0x49
  74. #define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
  75. #define SENCODE_OVERLAPPED_COMMAND 0x4E
  76. /*
  77. * Additional sense codes
  78. */
  79. #define ASENCODE_NO_SENSE 0x00
  80. #define ASENCODE_END_OF_DATA 0x05
  81. #define ASENCODE_BECOMING_READY 0x01
  82. #define ASENCODE_INIT_CMD_REQUIRED 0x02
  83. #define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
  84. #define ASENCODE_INVALID_COMMAND 0x00
  85. #define ASENCODE_LBA_OUT_OF_RANGE 0x00
  86. #define ASENCODE_INVALID_CDB_FIELD 0x00
  87. #define ASENCODE_LUN_NOT_SUPPORTED 0x00
  88. #define ASENCODE_INVALID_PARAM_FIELD 0x00
  89. #define ASENCODE_PARAM_NOT_SUPPORTED 0x01
  90. #define ASENCODE_PARAM_VALUE_INVALID 0x02
  91. #define ASENCODE_RESET_OCCURRED 0x00
  92. #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
  93. #define ASENCODE_INQUIRY_DATA_CHANGED 0x03
  94. #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
  95. #define ASENCODE_DIAGNOSTIC_FAILURE 0x80
  96. #define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
  97. #define ASENCODE_INVALID_MESSAGE_ERROR 0x00
  98. #define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
  99. #define ASENCODE_OVERLAPPED_COMMAND 0x00
  100. #define BYTE0(x) (unsigned char)(x)
  101. #define BYTE1(x) (unsigned char)((x) >> 8)
  102. #define BYTE2(x) (unsigned char)((x) >> 16)
  103. #define BYTE3(x) (unsigned char)((x) >> 24)
  104. /*------------------------------------------------------------------------------
  105. * S T R U C T S / T Y P E D E F S
  106. *----------------------------------------------------------------------------*/
  107. /* SCSI inquiry data */
  108. struct inquiry_data {
  109. u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
  110. u8 inqd_dtq; /* RMB | Device Type Qualifier */
  111. u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
  112. u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
  113. u8 inqd_len; /* Additional length (n-4) */
  114. u8 inqd_pad1[2];/* Reserved - must be zero */
  115. u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
  116. u8 inqd_vid[8]; /* Vendor ID */
  117. u8 inqd_pid[16];/* Product ID */
  118. u8 inqd_prl[4]; /* Product Revision Level */
  119. };
  120. /*
  121. * M O D U L E G L O B A L S
  122. */
  123. static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* sgmap);
  124. static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg);
  125. static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
  126. #ifdef AAC_DETAILED_STATUS_INFO
  127. static char *aac_get_status_string(u32 status);
  128. #endif
  129. /*
  130. * Non dasd selection is handled entirely in aachba now
  131. */
  132. static int nondasd = -1;
  133. static int dacmode = -1;
  134. static int commit = -1;
  135. module_param(nondasd, int, 0);
  136. MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices. 0=off, 1=on");
  137. module_param(dacmode, int, 0);
  138. MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC. 0=off, 1=on");
  139. module_param(commit, int, 0);
  140. MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the adapter for foreign arrays.\nThis is typically needed in systems that do not have a BIOS. 0=off, 1=on");
  141. /**
  142. * aac_get_config_status - check the adapter configuration
  143. * @common: adapter to query
  144. *
  145. * Query config status, and commit the configuration if needed.
  146. */
  147. int aac_get_config_status(struct aac_dev *dev)
  148. {
  149. int status = 0;
  150. struct fib * fibptr;
  151. if (!(fibptr = fib_alloc(dev)))
  152. return -ENOMEM;
  153. fib_init(fibptr);
  154. {
  155. struct aac_get_config_status *dinfo;
  156. dinfo = (struct aac_get_config_status *) fib_data(fibptr);
  157. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  158. dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
  159. dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
  160. }
  161. status = fib_send(ContainerCommand,
  162. fibptr,
  163. sizeof (struct aac_get_config_status),
  164. FsaNormal,
  165. 1, 1,
  166. NULL, NULL);
  167. if (status < 0 ) {
  168. printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
  169. } else {
  170. struct aac_get_config_status_resp *reply
  171. = (struct aac_get_config_status_resp *) fib_data(fibptr);
  172. dprintk((KERN_WARNING
  173. "aac_get_config_status: response=%d status=%d action=%d\n",
  174. le32_to_cpu(reply->response),
  175. le32_to_cpu(reply->status),
  176. le32_to_cpu(reply->data.action)));
  177. if ((le32_to_cpu(reply->response) != ST_OK) ||
  178. (le32_to_cpu(reply->status) != CT_OK) ||
  179. (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
  180. printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
  181. status = -EINVAL;
  182. }
  183. }
  184. fib_complete(fibptr);
  185. /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
  186. if (status >= 0) {
  187. if (commit == 1) {
  188. struct aac_commit_config * dinfo;
  189. fib_init(fibptr);
  190. dinfo = (struct aac_commit_config *) fib_data(fibptr);
  191. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  192. dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
  193. status = fib_send(ContainerCommand,
  194. fibptr,
  195. sizeof (struct aac_commit_config),
  196. FsaNormal,
  197. 1, 1,
  198. NULL, NULL);
  199. fib_complete(fibptr);
  200. } else if (commit == 0) {
  201. printk(KERN_WARNING
  202. "aac_get_config_status: Foreign device configurations are being ignored\n");
  203. }
  204. }
  205. fib_free(fibptr);
  206. return status;
  207. }
  208. /**
  209. * aac_get_containers - list containers
  210. * @common: adapter to probe
  211. *
  212. * Make a list of all containers on this controller
  213. */
  214. int aac_get_containers(struct aac_dev *dev)
  215. {
  216. struct fsa_dev_info *fsa_dev_ptr;
  217. u32 index;
  218. int status = 0;
  219. struct fib * fibptr;
  220. unsigned instance;
  221. struct aac_get_container_count *dinfo;
  222. struct aac_get_container_count_resp *dresp;
  223. int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
  224. instance = dev->scsi_host_ptr->unique_id;
  225. if (!(fibptr = fib_alloc(dev)))
  226. return -ENOMEM;
  227. fib_init(fibptr);
  228. dinfo = (struct aac_get_container_count *) fib_data(fibptr);
  229. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  230. dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
  231. status = fib_send(ContainerCommand,
  232. fibptr,
  233. sizeof (struct aac_get_container_count),
  234. FsaNormal,
  235. 1, 1,
  236. NULL, NULL);
  237. if (status >= 0) {
  238. dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
  239. maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
  240. fib_complete(fibptr);
  241. }
  242. if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
  243. maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
  244. fsa_dev_ptr = (struct fsa_dev_info *) kmalloc(
  245. sizeof(*fsa_dev_ptr) * maximum_num_containers, GFP_KERNEL);
  246. if (!fsa_dev_ptr) {
  247. fib_free(fibptr);
  248. return -ENOMEM;
  249. }
  250. memset(fsa_dev_ptr, 0, sizeof(*fsa_dev_ptr) * maximum_num_containers);
  251. dev->fsa_dev = fsa_dev_ptr;
  252. dev->maximum_num_containers = maximum_num_containers;
  253. for (index = 0; index < dev->maximum_num_containers; index++) {
  254. struct aac_query_mount *dinfo;
  255. struct aac_mount *dresp;
  256. fsa_dev_ptr[index].devname[0] = '\0';
  257. fib_init(fibptr);
  258. dinfo = (struct aac_query_mount *) fib_data(fibptr);
  259. dinfo->command = cpu_to_le32(VM_NameServe);
  260. dinfo->count = cpu_to_le32(index);
  261. dinfo->type = cpu_to_le32(FT_FILESYS);
  262. status = fib_send(ContainerCommand,
  263. fibptr,
  264. sizeof (struct aac_query_mount),
  265. FsaNormal,
  266. 1, 1,
  267. NULL, NULL);
  268. if (status < 0 ) {
  269. printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
  270. break;
  271. }
  272. dresp = (struct aac_mount *)fib_data(fibptr);
  273. dprintk ((KERN_DEBUG
  274. "VM_NameServe cid=%d status=%d vol=%d state=%d cap=%u\n",
  275. (int)index, (int)le32_to_cpu(dresp->status),
  276. (int)le32_to_cpu(dresp->mnt[0].vol),
  277. (int)le32_to_cpu(dresp->mnt[0].state),
  278. (unsigned)le32_to_cpu(dresp->mnt[0].capacity)));
  279. if ((le32_to_cpu(dresp->status) == ST_OK) &&
  280. (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
  281. (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
  282. fsa_dev_ptr[index].valid = 1;
  283. fsa_dev_ptr[index].type = le32_to_cpu(dresp->mnt[0].vol);
  284. fsa_dev_ptr[index].size = le32_to_cpu(dresp->mnt[0].capacity);
  285. if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
  286. fsa_dev_ptr[index].ro = 1;
  287. }
  288. fib_complete(fibptr);
  289. /*
  290. * If there are no more containers, then stop asking.
  291. */
  292. if ((index + 1) >= le32_to_cpu(dresp->count)){
  293. break;
  294. }
  295. }
  296. fib_free(fibptr);
  297. return status;
  298. }
  299. static void aac_io_done(struct scsi_cmnd * scsicmd)
  300. {
  301. unsigned long cpu_flags;
  302. struct Scsi_Host *host = scsicmd->device->host;
  303. spin_lock_irqsave(host->host_lock, cpu_flags);
  304. scsicmd->scsi_done(scsicmd);
  305. spin_unlock_irqrestore(host->host_lock, cpu_flags);
  306. }
  307. static void get_container_name_callback(void *context, struct fib * fibptr)
  308. {
  309. struct aac_get_name_resp * get_name_reply;
  310. struct scsi_cmnd * scsicmd;
  311. scsicmd = (struct scsi_cmnd *) context;
  312. dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
  313. if (fibptr == NULL)
  314. BUG();
  315. get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
  316. /* Failure is irrelevant, using default value instead */
  317. if ((le32_to_cpu(get_name_reply->status) == CT_OK)
  318. && (get_name_reply->data[0] != '\0')) {
  319. int count;
  320. char * dp;
  321. char * sp = get_name_reply->data;
  322. sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
  323. while (*sp == ' ')
  324. ++sp;
  325. count = sizeof(((struct inquiry_data *)NULL)->inqd_pid);
  326. dp = ((struct inquiry_data *)scsicmd->request_buffer)->inqd_pid;
  327. if (*sp) do {
  328. *dp++ = (*sp) ? *sp++ : ' ';
  329. } while (--count > 0);
  330. }
  331. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  332. fib_complete(fibptr);
  333. fib_free(fibptr);
  334. aac_io_done(scsicmd);
  335. }
  336. /**
  337. * aac_get_container_name - get container name, none blocking.
  338. */
  339. static int aac_get_container_name(struct scsi_cmnd * scsicmd, int cid)
  340. {
  341. int status;
  342. struct aac_get_name *dinfo;
  343. struct fib * cmd_fibcontext;
  344. struct aac_dev * dev;
  345. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  346. if (!(cmd_fibcontext = fib_alloc(dev)))
  347. return -ENOMEM;
  348. fib_init(cmd_fibcontext);
  349. dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
  350. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  351. dinfo->type = cpu_to_le32(CT_READ_NAME);
  352. dinfo->cid = cpu_to_le32(cid);
  353. dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
  354. status = fib_send(ContainerCommand,
  355. cmd_fibcontext,
  356. sizeof (struct aac_get_name),
  357. FsaNormal,
  358. 0, 1,
  359. (fib_callback) get_container_name_callback,
  360. (void *) scsicmd);
  361. /*
  362. * Check that the command queued to the controller
  363. */
  364. if (status == -EINPROGRESS)
  365. return 0;
  366. printk(KERN_WARNING "aac_get_container_name: fib_send failed with status: %d.\n", status);
  367. fib_complete(cmd_fibcontext);
  368. fib_free(cmd_fibcontext);
  369. return -1;
  370. }
  371. /**
  372. * probe_container - query a logical volume
  373. * @dev: device to query
  374. * @cid: container identifier
  375. *
  376. * Queries the controller about the given volume. The volume information
  377. * is updated in the struct fsa_dev_info structure rather than returned.
  378. */
  379. static int probe_container(struct aac_dev *dev, int cid)
  380. {
  381. struct fsa_dev_info *fsa_dev_ptr;
  382. int status;
  383. struct aac_query_mount *dinfo;
  384. struct aac_mount *dresp;
  385. struct fib * fibptr;
  386. unsigned instance;
  387. fsa_dev_ptr = dev->fsa_dev;
  388. instance = dev->scsi_host_ptr->unique_id;
  389. if (!(fibptr = fib_alloc(dev)))
  390. return -ENOMEM;
  391. fib_init(fibptr);
  392. dinfo = (struct aac_query_mount *)fib_data(fibptr);
  393. dinfo->command = cpu_to_le32(VM_NameServe);
  394. dinfo->count = cpu_to_le32(cid);
  395. dinfo->type = cpu_to_le32(FT_FILESYS);
  396. status = fib_send(ContainerCommand,
  397. fibptr,
  398. sizeof(struct aac_query_mount),
  399. FsaNormal,
  400. 1, 1,
  401. NULL, NULL);
  402. if (status < 0) {
  403. printk(KERN_WARNING "aacraid: probe_containers query failed.\n");
  404. goto error;
  405. }
  406. dresp = (struct aac_mount *) fib_data(fibptr);
  407. if ((le32_to_cpu(dresp->status) == ST_OK) &&
  408. (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
  409. (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
  410. fsa_dev_ptr[cid].valid = 1;
  411. fsa_dev_ptr[cid].type = le32_to_cpu(dresp->mnt[0].vol);
  412. fsa_dev_ptr[cid].size = le32_to_cpu(dresp->mnt[0].capacity);
  413. if (le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY)
  414. fsa_dev_ptr[cid].ro = 1;
  415. }
  416. error:
  417. fib_complete(fibptr);
  418. fib_free(fibptr);
  419. return status;
  420. }
  421. /* Local Structure to set SCSI inquiry data strings */
  422. struct scsi_inq {
  423. char vid[8]; /* Vendor ID */
  424. char pid[16]; /* Product ID */
  425. char prl[4]; /* Product Revision Level */
  426. };
  427. /**
  428. * InqStrCopy - string merge
  429. * @a: string to copy from
  430. * @b: string to copy to
  431. *
  432. * Copy a String from one location to another
  433. * without copying \0
  434. */
  435. static void inqstrcpy(char *a, char *b)
  436. {
  437. while(*a != (char)0)
  438. *b++ = *a++;
  439. }
  440. static char *container_types[] = {
  441. "None",
  442. "Volume",
  443. "Mirror",
  444. "Stripe",
  445. "RAID5",
  446. "SSRW",
  447. "SSRO",
  448. "Morph",
  449. "Legacy",
  450. "RAID4",
  451. "RAID10",
  452. "RAID00",
  453. "V-MIRRORS",
  454. "PSEUDO R4",
  455. "RAID50",
  456. "Unknown"
  457. };
  458. /* Function: setinqstr
  459. *
  460. * Arguments: [1] pointer to void [1] int
  461. *
  462. * Purpose: Sets SCSI inquiry data strings for vendor, product
  463. * and revision level. Allows strings to be set in platform dependant
  464. * files instead of in OS dependant driver source.
  465. */
  466. static void setinqstr(int devtype, void *data, int tindex)
  467. {
  468. struct scsi_inq *str;
  469. struct aac_driver_ident *mp;
  470. mp = aac_get_driver_ident(devtype);
  471. str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
  472. inqstrcpy (mp->vname, str->vid);
  473. inqstrcpy (mp->model, str->pid); /* last six chars reserved for vol type */
  474. if (tindex < (sizeof(container_types)/sizeof(char *))){
  475. char *findit = str->pid;
  476. for ( ; *findit != ' '; findit++); /* walk till we find a space */
  477. /* RAID is superfluous in the context of a RAID device */
  478. if (memcmp(findit-4, "RAID", 4) == 0)
  479. *(findit -= 4) = ' ';
  480. inqstrcpy (container_types[tindex], findit + 1);
  481. }
  482. inqstrcpy ("V1.0", str->prl);
  483. }
  484. static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
  485. u8 a_sense_code, u8 incorrect_length,
  486. u8 bit_pointer, u16 field_pointer,
  487. u32 residue)
  488. {
  489. sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
  490. sense_buf[1] = 0; /* Segment number, always zero */
  491. if (incorrect_length) {
  492. sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
  493. sense_buf[3] = BYTE3(residue);
  494. sense_buf[4] = BYTE2(residue);
  495. sense_buf[5] = BYTE1(residue);
  496. sense_buf[6] = BYTE0(residue);
  497. } else
  498. sense_buf[2] = sense_key; /* Sense key */
  499. if (sense_key == ILLEGAL_REQUEST)
  500. sense_buf[7] = 10; /* Additional sense length */
  501. else
  502. sense_buf[7] = 6; /* Additional sense length */
  503. sense_buf[12] = sense_code; /* Additional sense code */
  504. sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
  505. if (sense_key == ILLEGAL_REQUEST) {
  506. sense_buf[15] = 0;
  507. if (sense_code == SENCODE_INVALID_PARAM_FIELD)
  508. sense_buf[15] = 0x80;/* Std sense key specific field */
  509. /* Illegal parameter is in the parameter block */
  510. if (sense_code == SENCODE_INVALID_CDB_FIELD)
  511. sense_buf[15] = 0xc0;/* Std sense key specific field */
  512. /* Illegal parameter is in the CDB block */
  513. sense_buf[15] |= bit_pointer;
  514. sense_buf[16] = field_pointer >> 8; /* MSB */
  515. sense_buf[17] = field_pointer; /* LSB */
  516. }
  517. }
  518. int aac_get_adapter_info(struct aac_dev* dev)
  519. {
  520. struct fib* fibptr;
  521. struct aac_adapter_info* info;
  522. int rcode;
  523. u32 tmp;
  524. if (!(fibptr = fib_alloc(dev)))
  525. return -ENOMEM;
  526. fib_init(fibptr);
  527. info = (struct aac_adapter_info*) fib_data(fibptr);
  528. memset(info,0,sizeof(struct aac_adapter_info));
  529. rcode = fib_send(RequestAdapterInfo,
  530. fibptr,
  531. sizeof(struct aac_adapter_info),
  532. FsaNormal,
  533. 1, 1,
  534. NULL,
  535. NULL);
  536. memcpy(&dev->adapter_info, info, sizeof(struct aac_adapter_info));
  537. tmp = le32_to_cpu(dev->adapter_info.kernelrev);
  538. printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d]\n",
  539. dev->name,
  540. dev->id,
  541. tmp>>24,
  542. (tmp>>16)&0xff,
  543. tmp&0xff,
  544. le32_to_cpu(dev->adapter_info.kernelbuild));
  545. tmp = le32_to_cpu(dev->adapter_info.monitorrev);
  546. printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
  547. dev->name, dev->id,
  548. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  549. le32_to_cpu(dev->adapter_info.monitorbuild));
  550. tmp = le32_to_cpu(dev->adapter_info.biosrev);
  551. printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
  552. dev->name, dev->id,
  553. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  554. le32_to_cpu(dev->adapter_info.biosbuild));
  555. if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
  556. printk(KERN_INFO "%s%d: serial %x\n",
  557. dev->name, dev->id,
  558. le32_to_cpu(dev->adapter_info.serial[0]));
  559. dev->nondasd_support = 0;
  560. dev->raid_scsi_mode = 0;
  561. if(dev->adapter_info.options & AAC_OPT_NONDASD){
  562. dev->nondasd_support = 1;
  563. }
  564. /*
  565. * If the firmware supports ROMB RAID/SCSI mode and we are currently
  566. * in RAID/SCSI mode, set the flag. For now if in this mode we will
  567. * force nondasd support on. If we decide to allow the non-dasd flag
  568. * additional changes changes will have to be made to support
  569. * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
  570. * changed to support the new dev->raid_scsi_mode flag instead of
  571. * leaching off of the dev->nondasd_support flag. Also in linit.c the
  572. * function aac_detect will have to be modified where it sets up the
  573. * max number of channels based on the aac->nondasd_support flag only.
  574. */
  575. if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
  576. (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
  577. dev->nondasd_support = 1;
  578. dev->raid_scsi_mode = 1;
  579. }
  580. if (dev->raid_scsi_mode != 0)
  581. printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
  582. dev->name, dev->id);
  583. if(nondasd != -1) {
  584. dev->nondasd_support = (nondasd!=0);
  585. }
  586. if(dev->nondasd_support != 0){
  587. printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
  588. }
  589. dev->dac_support = 0;
  590. if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
  591. printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
  592. dev->dac_support = 1;
  593. }
  594. if(dacmode != -1) {
  595. dev->dac_support = (dacmode!=0);
  596. }
  597. if(dev->dac_support != 0) {
  598. if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL) &&
  599. !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFFFFFFFFFULL)) {
  600. printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
  601. dev->name, dev->id);
  602. } else if (!pci_set_dma_mask(dev->pdev, 0xFFFFFFFFULL) &&
  603. !pci_set_consistent_dma_mask(dev->pdev, 0xFFFFFFFFULL)) {
  604. printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
  605. dev->name, dev->id);
  606. dev->dac_support = 0;
  607. } else {
  608. printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
  609. dev->name, dev->id);
  610. rcode = -ENOMEM;
  611. }
  612. }
  613. fib_complete(fibptr);
  614. fib_free(fibptr);
  615. return rcode;
  616. }
  617. static void read_callback(void *context, struct fib * fibptr)
  618. {
  619. struct aac_dev *dev;
  620. struct aac_read_reply *readreply;
  621. struct scsi_cmnd *scsicmd;
  622. u32 lba;
  623. u32 cid;
  624. scsicmd = (struct scsi_cmnd *) context;
  625. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  626. cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
  627. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  628. dprintk((KERN_DEBUG "read_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
  629. if (fibptr == NULL)
  630. BUG();
  631. if(scsicmd->use_sg)
  632. pci_unmap_sg(dev->pdev,
  633. (struct scatterlist *)scsicmd->buffer,
  634. scsicmd->use_sg,
  635. scsicmd->sc_data_direction);
  636. else if(scsicmd->request_bufflen)
  637. pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
  638. scsicmd->request_bufflen,
  639. scsicmd->sc_data_direction);
  640. readreply = (struct aac_read_reply *)fib_data(fibptr);
  641. if (le32_to_cpu(readreply->status) == ST_OK)
  642. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  643. else {
  644. printk(KERN_WARNING "read_callback: read failed, status = %d\n",
  645. le32_to_cpu(readreply->status));
  646. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  647. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  648. HARDWARE_ERROR,
  649. SENCODE_INTERNAL_TARGET_FAILURE,
  650. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  651. 0, 0);
  652. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  653. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  654. ? sizeof(scsicmd->sense_buffer)
  655. : sizeof(dev->fsa_dev[cid].sense_data));
  656. }
  657. fib_complete(fibptr);
  658. fib_free(fibptr);
  659. aac_io_done(scsicmd);
  660. }
  661. static void write_callback(void *context, struct fib * fibptr)
  662. {
  663. struct aac_dev *dev;
  664. struct aac_write_reply *writereply;
  665. struct scsi_cmnd *scsicmd;
  666. u32 lba;
  667. u32 cid;
  668. scsicmd = (struct scsi_cmnd *) context;
  669. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  670. cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
  671. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  672. dprintk((KERN_DEBUG "write_callback[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
  673. if (fibptr == NULL)
  674. BUG();
  675. if(scsicmd->use_sg)
  676. pci_unmap_sg(dev->pdev,
  677. (struct scatterlist *)scsicmd->buffer,
  678. scsicmd->use_sg,
  679. scsicmd->sc_data_direction);
  680. else if(scsicmd->request_bufflen)
  681. pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
  682. scsicmd->request_bufflen,
  683. scsicmd->sc_data_direction);
  684. writereply = (struct aac_write_reply *) fib_data(fibptr);
  685. if (le32_to_cpu(writereply->status) == ST_OK)
  686. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  687. else {
  688. printk(KERN_WARNING "write_callback: write failed, status = %d\n", writereply->status);
  689. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  690. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  691. HARDWARE_ERROR,
  692. SENCODE_INTERNAL_TARGET_FAILURE,
  693. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  694. 0, 0);
  695. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  696. sizeof(struct sense_data));
  697. }
  698. fib_complete(fibptr);
  699. fib_free(fibptr);
  700. aac_io_done(scsicmd);
  701. }
  702. static int aac_read(struct scsi_cmnd * scsicmd, int cid)
  703. {
  704. u32 lba;
  705. u32 count;
  706. int status;
  707. u16 fibsize;
  708. struct aac_dev *dev;
  709. struct fib * cmd_fibcontext;
  710. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  711. /*
  712. * Get block address and transfer length
  713. */
  714. if (scsicmd->cmnd[0] == READ_6) /* 6 byte command */
  715. {
  716. dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
  717. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  718. count = scsicmd->cmnd[4];
  719. if (count == 0)
  720. count = 256;
  721. } else {
  722. dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
  723. lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  724. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  725. }
  726. dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %u, t = %ld.\n", smp_processor_id(), lba, jiffies));
  727. /*
  728. * Alocate and initialize a Fib
  729. */
  730. if (!(cmd_fibcontext = fib_alloc(dev))) {
  731. return -1;
  732. }
  733. fib_init(cmd_fibcontext);
  734. if(dev->dac_support == 1) {
  735. struct aac_read64 *readcmd;
  736. readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
  737. readcmd->command = cpu_to_le32(VM_CtHostRead64);
  738. readcmd->cid = cpu_to_le16(cid);
  739. readcmd->sector_count = cpu_to_le16(count);
  740. readcmd->block = cpu_to_le32(lba);
  741. readcmd->pad = 0;
  742. readcmd->flags = 0;
  743. aac_build_sg64(scsicmd, &readcmd->sg);
  744. fibsize = sizeof(struct aac_read64) +
  745. ((le32_to_cpu(readcmd->sg.count) - 1) *
  746. sizeof (struct sgentry64));
  747. BUG_ON (fibsize > (sizeof(struct hw_fib) -
  748. sizeof(struct aac_fibhdr)));
  749. /*
  750. * Now send the Fib to the adapter
  751. */
  752. status = fib_send(ContainerCommand64,
  753. cmd_fibcontext,
  754. fibsize,
  755. FsaNormal,
  756. 0, 1,
  757. (fib_callback) read_callback,
  758. (void *) scsicmd);
  759. } else {
  760. struct aac_read *readcmd;
  761. readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
  762. readcmd->command = cpu_to_le32(VM_CtBlockRead);
  763. readcmd->cid = cpu_to_le32(cid);
  764. readcmd->block = cpu_to_le32(lba);
  765. readcmd->count = cpu_to_le32(count * 512);
  766. if (count * 512 > (64 * 1024))
  767. BUG();
  768. aac_build_sg(scsicmd, &readcmd->sg);
  769. fibsize = sizeof(struct aac_read) +
  770. ((le32_to_cpu(readcmd->sg.count) - 1) *
  771. sizeof (struct sgentry));
  772. BUG_ON (fibsize > (sizeof(struct hw_fib) -
  773. sizeof(struct aac_fibhdr)));
  774. /*
  775. * Now send the Fib to the adapter
  776. */
  777. status = fib_send(ContainerCommand,
  778. cmd_fibcontext,
  779. fibsize,
  780. FsaNormal,
  781. 0, 1,
  782. (fib_callback) read_callback,
  783. (void *) scsicmd);
  784. }
  785. /*
  786. * Check that the command queued to the controller
  787. */
  788. if (status == -EINPROGRESS)
  789. return 0;
  790. printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
  791. /*
  792. * For some reason, the Fib didn't queue, return QUEUE_FULL
  793. */
  794. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  795. aac_io_done(scsicmd);
  796. fib_complete(cmd_fibcontext);
  797. fib_free(cmd_fibcontext);
  798. return 0;
  799. }
  800. static int aac_write(struct scsi_cmnd * scsicmd, int cid)
  801. {
  802. u32 lba;
  803. u32 count;
  804. int status;
  805. u16 fibsize;
  806. struct aac_dev *dev;
  807. struct fib * cmd_fibcontext;
  808. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  809. /*
  810. * Get block address and transfer length
  811. */
  812. if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
  813. {
  814. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  815. count = scsicmd->cmnd[4];
  816. if (count == 0)
  817. count = 256;
  818. } else {
  819. dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
  820. lba = (scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  821. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  822. }
  823. dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %u, t = %ld.\n",
  824. smp_processor_id(), (unsigned long long)lba, jiffies));
  825. /*
  826. * Allocate and initialize a Fib then setup a BlockWrite command
  827. */
  828. if (!(cmd_fibcontext = fib_alloc(dev))) {
  829. scsicmd->result = DID_ERROR << 16;
  830. aac_io_done(scsicmd);
  831. return 0;
  832. }
  833. fib_init(cmd_fibcontext);
  834. if(dev->dac_support == 1) {
  835. struct aac_write64 *writecmd;
  836. writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
  837. writecmd->command = cpu_to_le32(VM_CtHostWrite64);
  838. writecmd->cid = cpu_to_le16(cid);
  839. writecmd->sector_count = cpu_to_le16(count);
  840. writecmd->block = cpu_to_le32(lba);
  841. writecmd->pad = 0;
  842. writecmd->flags = 0;
  843. aac_build_sg64(scsicmd, &writecmd->sg);
  844. fibsize = sizeof(struct aac_write64) +
  845. ((le32_to_cpu(writecmd->sg.count) - 1) *
  846. sizeof (struct sgentry64));
  847. BUG_ON (fibsize > (sizeof(struct hw_fib) -
  848. sizeof(struct aac_fibhdr)));
  849. /*
  850. * Now send the Fib to the adapter
  851. */
  852. status = fib_send(ContainerCommand64,
  853. cmd_fibcontext,
  854. fibsize,
  855. FsaNormal,
  856. 0, 1,
  857. (fib_callback) write_callback,
  858. (void *) scsicmd);
  859. } else {
  860. struct aac_write *writecmd;
  861. writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
  862. writecmd->command = cpu_to_le32(VM_CtBlockWrite);
  863. writecmd->cid = cpu_to_le32(cid);
  864. writecmd->block = cpu_to_le32(lba);
  865. writecmd->count = cpu_to_le32(count * 512);
  866. writecmd->sg.count = cpu_to_le32(1);
  867. /* ->stable is not used - it did mean which type of write */
  868. if (count * 512 > (64 * 1024)) {
  869. BUG();
  870. }
  871. aac_build_sg(scsicmd, &writecmd->sg);
  872. fibsize = sizeof(struct aac_write) +
  873. ((le32_to_cpu(writecmd->sg.count) - 1) *
  874. sizeof (struct sgentry));
  875. BUG_ON (fibsize > (sizeof(struct hw_fib) -
  876. sizeof(struct aac_fibhdr)));
  877. /*
  878. * Now send the Fib to the adapter
  879. */
  880. status = fib_send(ContainerCommand,
  881. cmd_fibcontext,
  882. fibsize,
  883. FsaNormal,
  884. 0, 1,
  885. (fib_callback) write_callback,
  886. (void *) scsicmd);
  887. }
  888. /*
  889. * Check that the command queued to the controller
  890. */
  891. if (status == -EINPROGRESS)
  892. {
  893. dprintk("write queued.\n");
  894. return 0;
  895. }
  896. printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
  897. /*
  898. * For some reason, the Fib didn't queue, return QUEUE_FULL
  899. */
  900. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  901. aac_io_done(scsicmd);
  902. fib_complete(cmd_fibcontext);
  903. fib_free(cmd_fibcontext);
  904. return 0;
  905. }
  906. static void synchronize_callback(void *context, struct fib *fibptr)
  907. {
  908. struct aac_synchronize_reply *synchronizereply;
  909. struct scsi_cmnd *cmd;
  910. cmd = context;
  911. dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
  912. smp_processor_id(), jiffies));
  913. BUG_ON(fibptr == NULL);
  914. synchronizereply = fib_data(fibptr);
  915. if (le32_to_cpu(synchronizereply->status) == CT_OK)
  916. cmd->result = DID_OK << 16 |
  917. COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  918. else {
  919. struct scsi_device *sdev = cmd->device;
  920. struct aac_dev *dev = (struct aac_dev *)sdev->host->hostdata;
  921. u32 cid = ID_LUN_TO_CONTAINER(sdev->id, sdev->lun);
  922. printk(KERN_WARNING
  923. "synchronize_callback: synchronize failed, status = %d\n",
  924. le32_to_cpu(synchronizereply->status));
  925. cmd->result = DID_OK << 16 |
  926. COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  927. set_sense((u8 *)&dev->fsa_dev[cid].sense_data,
  928. HARDWARE_ERROR,
  929. SENCODE_INTERNAL_TARGET_FAILURE,
  930. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  931. 0, 0);
  932. memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  933. min(sizeof(dev->fsa_dev[cid].sense_data),
  934. sizeof(cmd->sense_buffer)));
  935. }
  936. fib_complete(fibptr);
  937. fib_free(fibptr);
  938. aac_io_done(cmd);
  939. }
  940. static int aac_synchronize(struct scsi_cmnd *scsicmd, int cid)
  941. {
  942. int status;
  943. struct fib *cmd_fibcontext;
  944. struct aac_synchronize *synchronizecmd;
  945. struct scsi_cmnd *cmd;
  946. struct scsi_device *sdev = scsicmd->device;
  947. int active = 0;
  948. unsigned long flags;
  949. /*
  950. * Wait for all commands to complete to this specific
  951. * target (block).
  952. */
  953. spin_lock_irqsave(&sdev->list_lock, flags);
  954. list_for_each_entry(cmd, &sdev->cmd_list, list)
  955. if (cmd != scsicmd && cmd->serial_number != 0) {
  956. ++active;
  957. break;
  958. }
  959. spin_unlock_irqrestore(&sdev->list_lock, flags);
  960. /*
  961. * Yield the processor (requeue for later)
  962. */
  963. if (active)
  964. return SCSI_MLQUEUE_DEVICE_BUSY;
  965. /*
  966. * Alocate and initialize a Fib
  967. */
  968. if (!(cmd_fibcontext =
  969. fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata)))
  970. return SCSI_MLQUEUE_HOST_BUSY;
  971. fib_init(cmd_fibcontext);
  972. synchronizecmd = fib_data(cmd_fibcontext);
  973. synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
  974. synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
  975. synchronizecmd->cid = cpu_to_le32(cid);
  976. synchronizecmd->count =
  977. cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
  978. /*
  979. * Now send the Fib to the adapter
  980. */
  981. status = fib_send(ContainerCommand,
  982. cmd_fibcontext,
  983. sizeof(struct aac_synchronize),
  984. FsaNormal,
  985. 0, 1,
  986. (fib_callback)synchronize_callback,
  987. (void *)scsicmd);
  988. /*
  989. * Check that the command queued to the controller
  990. */
  991. if (status == -EINPROGRESS)
  992. return 0;
  993. printk(KERN_WARNING
  994. "aac_synchronize: fib_send failed with status: %d.\n", status);
  995. fib_complete(cmd_fibcontext);
  996. fib_free(cmd_fibcontext);
  997. return SCSI_MLQUEUE_HOST_BUSY;
  998. }
  999. /**
  1000. * aac_scsi_cmd() - Process SCSI command
  1001. * @scsicmd: SCSI command block
  1002. *
  1003. * Emulate a SCSI command and queue the required request for the
  1004. * aacraid firmware.
  1005. */
  1006. int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
  1007. {
  1008. u32 cid = 0;
  1009. struct Scsi_Host *host = scsicmd->device->host;
  1010. struct aac_dev *dev = (struct aac_dev *)host->hostdata;
  1011. struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
  1012. int cardtype = dev->cardtype;
  1013. int ret;
  1014. /*
  1015. * If the bus, id or lun is out of range, return fail
  1016. * Test does not apply to ID 16, the pseudo id for the controller
  1017. * itself.
  1018. */
  1019. if (scsicmd->device->id != host->this_id) {
  1020. if ((scsicmd->device->channel == 0) ){
  1021. if( (scsicmd->device->id >= dev->maximum_num_containers) || (scsicmd->device->lun != 0)){
  1022. scsicmd->result = DID_NO_CONNECT << 16;
  1023. scsicmd->scsi_done(scsicmd);
  1024. return 0;
  1025. }
  1026. cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
  1027. /*
  1028. * If the target container doesn't exist, it may have
  1029. * been newly created
  1030. */
  1031. if ((fsa_dev_ptr[cid].valid & 1) == 0) {
  1032. switch (scsicmd->cmnd[0]) {
  1033. case INQUIRY:
  1034. case READ_CAPACITY:
  1035. case TEST_UNIT_READY:
  1036. spin_unlock_irq(host->host_lock);
  1037. probe_container(dev, cid);
  1038. spin_lock_irq(host->host_lock);
  1039. if (fsa_dev_ptr[cid].valid == 0) {
  1040. scsicmd->result = DID_NO_CONNECT << 16;
  1041. scsicmd->scsi_done(scsicmd);
  1042. return 0;
  1043. }
  1044. default:
  1045. break;
  1046. }
  1047. }
  1048. /*
  1049. * If the target container still doesn't exist,
  1050. * return failure
  1051. */
  1052. if (fsa_dev_ptr[cid].valid == 0) {
  1053. scsicmd->result = DID_BAD_TARGET << 16;
  1054. scsicmd->scsi_done(scsicmd);
  1055. return 0;
  1056. }
  1057. } else { /* check for physical non-dasd devices */
  1058. if(dev->nondasd_support == 1){
  1059. return aac_send_srb_fib(scsicmd);
  1060. } else {
  1061. scsicmd->result = DID_NO_CONNECT << 16;
  1062. scsicmd->scsi_done(scsicmd);
  1063. return 0;
  1064. }
  1065. }
  1066. }
  1067. /*
  1068. * else Command for the controller itself
  1069. */
  1070. else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
  1071. (scsicmd->cmnd[0] != TEST_UNIT_READY))
  1072. {
  1073. dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
  1074. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1075. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1076. ILLEGAL_REQUEST,
  1077. SENCODE_INVALID_COMMAND,
  1078. ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
  1079. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1080. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1081. ? sizeof(scsicmd->sense_buffer)
  1082. : sizeof(dev->fsa_dev[cid].sense_data));
  1083. scsicmd->scsi_done(scsicmd);
  1084. return 0;
  1085. }
  1086. /* Handle commands here that don't really require going out to the adapter */
  1087. switch (scsicmd->cmnd[0]) {
  1088. case INQUIRY:
  1089. {
  1090. struct inquiry_data *inq_data_ptr;
  1091. dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
  1092. inq_data_ptr = (struct inquiry_data *)scsicmd->request_buffer;
  1093. memset(inq_data_ptr, 0, sizeof (struct inquiry_data));
  1094. inq_data_ptr->inqd_ver = 2; /* claim compliance to SCSI-2 */
  1095. inq_data_ptr->inqd_dtq = 0x80; /* set RMB bit to one indicating that the medium is removable */
  1096. inq_data_ptr->inqd_rdf = 2; /* A response data format value of two indicates that the data shall be in the format specified in SCSI-2 */
  1097. inq_data_ptr->inqd_len = 31;
  1098. /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
  1099. inq_data_ptr->inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
  1100. /*
  1101. * Set the Vendor, Product, and Revision Level
  1102. * see: <vendor>.c i.e. aac.c
  1103. */
  1104. if (scsicmd->device->id == host->this_id) {
  1105. setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), (sizeof(container_types)/sizeof(char *)));
  1106. inq_data_ptr->inqd_pdt = INQD_PDT_PROC; /* Processor device */
  1107. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1108. scsicmd->scsi_done(scsicmd);
  1109. return 0;
  1110. }
  1111. setinqstr(cardtype, (void *) (inq_data_ptr->inqd_vid), fsa_dev_ptr[cid].type);
  1112. inq_data_ptr->inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
  1113. return aac_get_container_name(scsicmd, cid);
  1114. }
  1115. case READ_CAPACITY:
  1116. {
  1117. u32 capacity;
  1118. char *cp;
  1119. dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
  1120. if (fsa_dev_ptr[cid].size <= 0x100000000LL)
  1121. capacity = fsa_dev_ptr[cid].size - 1;
  1122. else
  1123. capacity = (u32)-1;
  1124. cp = scsicmd->request_buffer;
  1125. cp[0] = (capacity >> 24) & 0xff;
  1126. cp[1] = (capacity >> 16) & 0xff;
  1127. cp[2] = (capacity >> 8) & 0xff;
  1128. cp[3] = (capacity >> 0) & 0xff;
  1129. cp[4] = 0;
  1130. cp[5] = 0;
  1131. cp[6] = 2;
  1132. cp[7] = 0;
  1133. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1134. scsicmd->scsi_done(scsicmd);
  1135. return 0;
  1136. }
  1137. case MODE_SENSE:
  1138. {
  1139. char *mode_buf;
  1140. dprintk((KERN_DEBUG "MODE SENSE command.\n"));
  1141. mode_buf = scsicmd->request_buffer;
  1142. mode_buf[0] = 3; /* Mode data length */
  1143. mode_buf[1] = 0; /* Medium type - default */
  1144. mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
  1145. mode_buf[3] = 0; /* Block descriptor length */
  1146. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1147. scsicmd->scsi_done(scsicmd);
  1148. return 0;
  1149. }
  1150. case MODE_SENSE_10:
  1151. {
  1152. char *mode_buf;
  1153. dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
  1154. mode_buf = scsicmd->request_buffer;
  1155. mode_buf[0] = 0; /* Mode data length (MSB) */
  1156. mode_buf[1] = 6; /* Mode data length (LSB) */
  1157. mode_buf[2] = 0; /* Medium type - default */
  1158. mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
  1159. mode_buf[4] = 0; /* reserved */
  1160. mode_buf[5] = 0; /* reserved */
  1161. mode_buf[6] = 0; /* Block descriptor length (MSB) */
  1162. mode_buf[7] = 0; /* Block descriptor length (LSB) */
  1163. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1164. scsicmd->scsi_done(scsicmd);
  1165. return 0;
  1166. }
  1167. case REQUEST_SENSE:
  1168. dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
  1169. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
  1170. memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
  1171. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1172. scsicmd->scsi_done(scsicmd);
  1173. return 0;
  1174. case ALLOW_MEDIUM_REMOVAL:
  1175. dprintk((KERN_DEBUG "LOCK command.\n"));
  1176. if (scsicmd->cmnd[4])
  1177. fsa_dev_ptr[cid].locked = 1;
  1178. else
  1179. fsa_dev_ptr[cid].locked = 0;
  1180. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1181. scsicmd->scsi_done(scsicmd);
  1182. return 0;
  1183. /*
  1184. * These commands are all No-Ops
  1185. */
  1186. case TEST_UNIT_READY:
  1187. case RESERVE:
  1188. case RELEASE:
  1189. case REZERO_UNIT:
  1190. case REASSIGN_BLOCKS:
  1191. case SEEK_10:
  1192. case START_STOP:
  1193. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1194. scsicmd->scsi_done(scsicmd);
  1195. return 0;
  1196. }
  1197. switch (scsicmd->cmnd[0])
  1198. {
  1199. case READ_6:
  1200. case READ_10:
  1201. /*
  1202. * Hack to keep track of ordinal number of the device that
  1203. * corresponds to a container. Needed to convert
  1204. * containers to /dev/sd device names
  1205. */
  1206. spin_unlock_irq(host->host_lock);
  1207. if (scsicmd->request->rq_disk)
  1208. memcpy(fsa_dev_ptr[cid].devname,
  1209. scsicmd->request->rq_disk->disk_name,
  1210. 8);
  1211. ret = aac_read(scsicmd, cid);
  1212. spin_lock_irq(host->host_lock);
  1213. return ret;
  1214. case WRITE_6:
  1215. case WRITE_10:
  1216. spin_unlock_irq(host->host_lock);
  1217. ret = aac_write(scsicmd, cid);
  1218. spin_lock_irq(host->host_lock);
  1219. return ret;
  1220. case SYNCHRONIZE_CACHE:
  1221. /* Issue FIB to tell Firmware to flush it's cache */
  1222. return aac_synchronize(scsicmd, cid);
  1223. default:
  1224. /*
  1225. * Unhandled commands
  1226. */
  1227. printk(KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]);
  1228. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1229. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1230. ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
  1231. ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
  1232. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1233. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1234. ? sizeof(scsicmd->sense_buffer)
  1235. : sizeof(dev->fsa_dev[cid].sense_data));
  1236. scsicmd->scsi_done(scsicmd);
  1237. return 0;
  1238. }
  1239. }
  1240. static int query_disk(struct aac_dev *dev, void __user *arg)
  1241. {
  1242. struct aac_query_disk qd;
  1243. struct fsa_dev_info *fsa_dev_ptr;
  1244. fsa_dev_ptr = dev->fsa_dev;
  1245. if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
  1246. return -EFAULT;
  1247. if (qd.cnum == -1)
  1248. qd.cnum = ID_LUN_TO_CONTAINER(qd.id, qd.lun);
  1249. else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
  1250. {
  1251. if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
  1252. return -EINVAL;
  1253. qd.instance = dev->scsi_host_ptr->host_no;
  1254. qd.bus = 0;
  1255. qd.id = CONTAINER_TO_ID(qd.cnum);
  1256. qd.lun = CONTAINER_TO_LUN(qd.cnum);
  1257. }
  1258. else return -EINVAL;
  1259. qd.valid = fsa_dev_ptr[qd.cnum].valid;
  1260. qd.locked = fsa_dev_ptr[qd.cnum].locked;
  1261. qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
  1262. if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
  1263. qd.unmapped = 1;
  1264. else
  1265. qd.unmapped = 0;
  1266. strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
  1267. min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
  1268. if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
  1269. return -EFAULT;
  1270. return 0;
  1271. }
  1272. static int force_delete_disk(struct aac_dev *dev, void __user *arg)
  1273. {
  1274. struct aac_delete_disk dd;
  1275. struct fsa_dev_info *fsa_dev_ptr;
  1276. fsa_dev_ptr = dev->fsa_dev;
  1277. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  1278. return -EFAULT;
  1279. if (dd.cnum >= dev->maximum_num_containers)
  1280. return -EINVAL;
  1281. /*
  1282. * Mark this container as being deleted.
  1283. */
  1284. fsa_dev_ptr[dd.cnum].deleted = 1;
  1285. /*
  1286. * Mark the container as no longer valid
  1287. */
  1288. fsa_dev_ptr[dd.cnum].valid = 0;
  1289. return 0;
  1290. }
  1291. static int delete_disk(struct aac_dev *dev, void __user *arg)
  1292. {
  1293. struct aac_delete_disk dd;
  1294. struct fsa_dev_info *fsa_dev_ptr;
  1295. fsa_dev_ptr = dev->fsa_dev;
  1296. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  1297. return -EFAULT;
  1298. if (dd.cnum >= dev->maximum_num_containers)
  1299. return -EINVAL;
  1300. /*
  1301. * If the container is locked, it can not be deleted by the API.
  1302. */
  1303. if (fsa_dev_ptr[dd.cnum].locked)
  1304. return -EBUSY;
  1305. else {
  1306. /*
  1307. * Mark the container as no longer being valid.
  1308. */
  1309. fsa_dev_ptr[dd.cnum].valid = 0;
  1310. fsa_dev_ptr[dd.cnum].devname[0] = '\0';
  1311. return 0;
  1312. }
  1313. }
  1314. int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
  1315. {
  1316. switch (cmd) {
  1317. case FSACTL_QUERY_DISK:
  1318. return query_disk(dev, arg);
  1319. case FSACTL_DELETE_DISK:
  1320. return delete_disk(dev, arg);
  1321. case FSACTL_FORCE_DELETE_DISK:
  1322. return force_delete_disk(dev, arg);
  1323. case FSACTL_GET_CONTAINERS:
  1324. return aac_get_containers(dev);
  1325. default:
  1326. return -ENOTTY;
  1327. }
  1328. }
  1329. /**
  1330. *
  1331. * aac_srb_callback
  1332. * @context: the context set in the fib - here it is scsi cmd
  1333. * @fibptr: pointer to the fib
  1334. *
  1335. * Handles the completion of a scsi command to a non dasd device
  1336. *
  1337. */
  1338. static void aac_srb_callback(void *context, struct fib * fibptr)
  1339. {
  1340. struct aac_dev *dev;
  1341. struct aac_srb_reply *srbreply;
  1342. struct scsi_cmnd *scsicmd;
  1343. scsicmd = (struct scsi_cmnd *) context;
  1344. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1345. if (fibptr == NULL)
  1346. BUG();
  1347. srbreply = (struct aac_srb_reply *) fib_data(fibptr);
  1348. scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
  1349. /*
  1350. * Calculate resid for sg
  1351. */
  1352. scsicmd->resid = scsicmd->request_bufflen -
  1353. le32_to_cpu(srbreply->data_xfer_length);
  1354. if(scsicmd->use_sg)
  1355. pci_unmap_sg(dev->pdev,
  1356. (struct scatterlist *)scsicmd->buffer,
  1357. scsicmd->use_sg,
  1358. scsicmd->sc_data_direction);
  1359. else if(scsicmd->request_bufflen)
  1360. pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle, scsicmd->request_bufflen,
  1361. scsicmd->sc_data_direction);
  1362. /*
  1363. * First check the fib status
  1364. */
  1365. if (le32_to_cpu(srbreply->status) != ST_OK){
  1366. int len;
  1367. printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
  1368. len = (le32_to_cpu(srbreply->sense_data_size) >
  1369. sizeof(scsicmd->sense_buffer)) ?
  1370. sizeof(scsicmd->sense_buffer) :
  1371. le32_to_cpu(srbreply->sense_data_size);
  1372. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1373. memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
  1374. }
  1375. /*
  1376. * Next check the srb status
  1377. */
  1378. switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
  1379. case SRB_STATUS_ERROR_RECOVERY:
  1380. case SRB_STATUS_PENDING:
  1381. case SRB_STATUS_SUCCESS:
  1382. if(scsicmd->cmnd[0] == INQUIRY ){
  1383. u8 b;
  1384. u8 b1;
  1385. /* We can't expose disk devices because we can't tell whether they
  1386. * are the raw container drives or stand alone drives. If they have
  1387. * the removable bit set then we should expose them though.
  1388. */
  1389. b = (*(u8*)scsicmd->buffer)&0x1f;
  1390. b1 = ((u8*)scsicmd->buffer)[1];
  1391. if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
  1392. || (b==TYPE_DISK && (b1&0x80)) ){
  1393. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1394. /*
  1395. * We will allow disk devices if in RAID/SCSI mode and
  1396. * the channel is 2
  1397. */
  1398. } else if ((dev->raid_scsi_mode) &&
  1399. (scsicmd->device->channel == 2)) {
  1400. scsicmd->result = DID_OK << 16 |
  1401. COMMAND_COMPLETE << 8;
  1402. } else {
  1403. scsicmd->result = DID_NO_CONNECT << 16 |
  1404. COMMAND_COMPLETE << 8;
  1405. }
  1406. } else {
  1407. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1408. }
  1409. break;
  1410. case SRB_STATUS_DATA_OVERRUN:
  1411. switch(scsicmd->cmnd[0]){
  1412. case READ_6:
  1413. case WRITE_6:
  1414. case READ_10:
  1415. case WRITE_10:
  1416. case READ_12:
  1417. case WRITE_12:
  1418. if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
  1419. printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
  1420. } else {
  1421. printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
  1422. }
  1423. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  1424. break;
  1425. case INQUIRY: {
  1426. u8 b;
  1427. u8 b1;
  1428. /* We can't expose disk devices because we can't tell whether they
  1429. * are the raw container drives or stand alone drives
  1430. */
  1431. b = (*(u8*)scsicmd->buffer)&0x0f;
  1432. b1 = ((u8*)scsicmd->buffer)[1];
  1433. if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
  1434. || (b==TYPE_DISK && (b1&0x80)) ){
  1435. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1436. /*
  1437. * We will allow disk devices if in RAID/SCSI mode and
  1438. * the channel is 2
  1439. */
  1440. } else if ((dev->raid_scsi_mode) &&
  1441. (scsicmd->device->channel == 2)) {
  1442. scsicmd->result = DID_OK << 16 |
  1443. COMMAND_COMPLETE << 8;
  1444. } else {
  1445. scsicmd->result = DID_NO_CONNECT << 16 |
  1446. COMMAND_COMPLETE << 8;
  1447. }
  1448. break;
  1449. }
  1450. default:
  1451. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1452. break;
  1453. }
  1454. break;
  1455. case SRB_STATUS_ABORTED:
  1456. scsicmd->result = DID_ABORT << 16 | ABORT << 8;
  1457. break;
  1458. case SRB_STATUS_ABORT_FAILED:
  1459. // Not sure about this one - but assuming the hba was trying to abort for some reason
  1460. scsicmd->result = DID_ERROR << 16 | ABORT << 8;
  1461. break;
  1462. case SRB_STATUS_PARITY_ERROR:
  1463. scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
  1464. break;
  1465. case SRB_STATUS_NO_DEVICE:
  1466. case SRB_STATUS_INVALID_PATH_ID:
  1467. case SRB_STATUS_INVALID_TARGET_ID:
  1468. case SRB_STATUS_INVALID_LUN:
  1469. case SRB_STATUS_SELECTION_TIMEOUT:
  1470. scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  1471. break;
  1472. case SRB_STATUS_COMMAND_TIMEOUT:
  1473. case SRB_STATUS_TIMEOUT:
  1474. scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
  1475. break;
  1476. case SRB_STATUS_BUSY:
  1477. scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  1478. break;
  1479. case SRB_STATUS_BUS_RESET:
  1480. scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
  1481. break;
  1482. case SRB_STATUS_MESSAGE_REJECTED:
  1483. scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
  1484. break;
  1485. case SRB_STATUS_REQUEST_FLUSHED:
  1486. case SRB_STATUS_ERROR:
  1487. case SRB_STATUS_INVALID_REQUEST:
  1488. case SRB_STATUS_REQUEST_SENSE_FAILED:
  1489. case SRB_STATUS_NO_HBA:
  1490. case SRB_STATUS_UNEXPECTED_BUS_FREE:
  1491. case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
  1492. case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
  1493. case SRB_STATUS_DELAYED_RETRY:
  1494. case SRB_STATUS_BAD_FUNCTION:
  1495. case SRB_STATUS_NOT_STARTED:
  1496. case SRB_STATUS_NOT_IN_USE:
  1497. case SRB_STATUS_FORCE_ABORT:
  1498. case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
  1499. default:
  1500. #ifdef AAC_DETAILED_STATUS_INFO
  1501. printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
  1502. le32_to_cpu(srbreply->srb_status) & 0x3F,
  1503. aac_get_status_string(
  1504. le32_to_cpu(srbreply->srb_status) & 0x3F),
  1505. scsicmd->cmnd[0],
  1506. le32_to_cpu(srbreply->scsi_status));
  1507. #endif
  1508. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  1509. break;
  1510. }
  1511. if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
  1512. int len;
  1513. scsicmd->result |= SAM_STAT_CHECK_CONDITION;
  1514. len = (le32_to_cpu(srbreply->sense_data_size) >
  1515. sizeof(scsicmd->sense_buffer)) ?
  1516. sizeof(scsicmd->sense_buffer) :
  1517. le32_to_cpu(srbreply->sense_data_size);
  1518. #ifdef AAC_DETAILED_STATUS_INFO
  1519. dprintk((KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
  1520. le32_to_cpu(srbreply->status), len));
  1521. #endif
  1522. memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
  1523. }
  1524. /*
  1525. * OR in the scsi status (already shifted up a bit)
  1526. */
  1527. scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
  1528. fib_complete(fibptr);
  1529. fib_free(fibptr);
  1530. aac_io_done(scsicmd);
  1531. }
  1532. /**
  1533. *
  1534. * aac_send_scb_fib
  1535. * @scsicmd: the scsi command block
  1536. *
  1537. * This routine will form a FIB and fill in the aac_srb from the
  1538. * scsicmd passed in.
  1539. */
  1540. static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
  1541. {
  1542. struct fib* cmd_fibcontext;
  1543. struct aac_dev* dev;
  1544. int status;
  1545. struct aac_srb *srbcmd;
  1546. u16 fibsize;
  1547. u32 flag;
  1548. u32 timeout;
  1549. if( scsicmd->device->id > 15 || scsicmd->device->lun > 7) {
  1550. scsicmd->result = DID_NO_CONNECT << 16;
  1551. scsicmd->scsi_done(scsicmd);
  1552. return 0;
  1553. }
  1554. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1555. switch(scsicmd->sc_data_direction){
  1556. case DMA_TO_DEVICE:
  1557. flag = SRB_DataOut;
  1558. break;
  1559. case DMA_BIDIRECTIONAL:
  1560. flag = SRB_DataIn | SRB_DataOut;
  1561. break;
  1562. case DMA_FROM_DEVICE:
  1563. flag = SRB_DataIn;
  1564. break;
  1565. case DMA_NONE:
  1566. default: /* shuts up some versions of gcc */
  1567. flag = SRB_NoDataXfer;
  1568. break;
  1569. }
  1570. /*
  1571. * Allocate and initialize a Fib then setup a BlockWrite command
  1572. */
  1573. if (!(cmd_fibcontext = fib_alloc(dev))) {
  1574. return -1;
  1575. }
  1576. fib_init(cmd_fibcontext);
  1577. srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
  1578. srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
  1579. srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
  1580. srbcmd->id = cpu_to_le32(scsicmd->device->id);
  1581. srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
  1582. srbcmd->flags = cpu_to_le32(flag);
  1583. timeout = (scsicmd->timeout-jiffies)/HZ;
  1584. if(timeout == 0){
  1585. timeout = 1;
  1586. }
  1587. srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
  1588. srbcmd->retry_limit = 0; /* Obsolete parameter */
  1589. srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
  1590. if( dev->dac_support == 1 ) {
  1591. aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
  1592. srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
  1593. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1594. memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
  1595. /*
  1596. * Build Scatter/Gather list
  1597. */
  1598. fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
  1599. ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
  1600. sizeof (struct sgentry64));
  1601. BUG_ON (fibsize > (sizeof(struct hw_fib) -
  1602. sizeof(struct aac_fibhdr)));
  1603. /*
  1604. * Now send the Fib to the adapter
  1605. */
  1606. status = fib_send(ScsiPortCommand64, cmd_fibcontext,
  1607. fibsize, FsaNormal, 0, 1,
  1608. (fib_callback) aac_srb_callback,
  1609. (void *) scsicmd);
  1610. } else {
  1611. aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
  1612. srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
  1613. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1614. memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
  1615. /*
  1616. * Build Scatter/Gather list
  1617. */
  1618. fibsize = sizeof (struct aac_srb) +
  1619. (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
  1620. sizeof (struct sgentry));
  1621. BUG_ON (fibsize > (sizeof(struct hw_fib) -
  1622. sizeof(struct aac_fibhdr)));
  1623. /*
  1624. * Now send the Fib to the adapter
  1625. */
  1626. status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
  1627. (fib_callback) aac_srb_callback, (void *) scsicmd);
  1628. }
  1629. /*
  1630. * Check that the command queued to the controller
  1631. */
  1632. if (status == -EINPROGRESS){
  1633. return 0;
  1634. }
  1635. printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
  1636. fib_complete(cmd_fibcontext);
  1637. fib_free(cmd_fibcontext);
  1638. return -1;
  1639. }
  1640. static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
  1641. {
  1642. struct aac_dev *dev;
  1643. unsigned long byte_count = 0;
  1644. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1645. // Get rid of old data
  1646. psg->count = 0;
  1647. psg->sg[0].addr = 0;
  1648. psg->sg[0].count = 0;
  1649. if (scsicmd->use_sg) {
  1650. struct scatterlist *sg;
  1651. int i;
  1652. int sg_count;
  1653. sg = (struct scatterlist *) scsicmd->request_buffer;
  1654. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  1655. scsicmd->sc_data_direction);
  1656. psg->count = cpu_to_le32(sg_count);
  1657. byte_count = 0;
  1658. for (i = 0; i < sg_count; i++) {
  1659. psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
  1660. psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
  1661. byte_count += sg_dma_len(sg);
  1662. sg++;
  1663. }
  1664. /* hba wants the size to be exact */
  1665. if(byte_count > scsicmd->request_bufflen){
  1666. psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
  1667. byte_count = scsicmd->request_bufflen;
  1668. }
  1669. /* Check for command underflow */
  1670. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  1671. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  1672. byte_count, scsicmd->underflow);
  1673. }
  1674. }
  1675. else if(scsicmd->request_bufflen) {
  1676. dma_addr_t addr;
  1677. addr = pci_map_single(dev->pdev,
  1678. scsicmd->request_buffer,
  1679. scsicmd->request_bufflen,
  1680. scsicmd->sc_data_direction);
  1681. psg->count = cpu_to_le32(1);
  1682. psg->sg[0].addr = cpu_to_le32(addr);
  1683. psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
  1684. scsicmd->SCp.dma_handle = addr;
  1685. byte_count = scsicmd->request_bufflen;
  1686. }
  1687. return byte_count;
  1688. }
  1689. static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
  1690. {
  1691. struct aac_dev *dev;
  1692. unsigned long byte_count = 0;
  1693. u64 le_addr;
  1694. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1695. // Get rid of old data
  1696. psg->count = 0;
  1697. psg->sg[0].addr[0] = 0;
  1698. psg->sg[0].addr[1] = 0;
  1699. psg->sg[0].count = 0;
  1700. if (scsicmd->use_sg) {
  1701. struct scatterlist *sg;
  1702. int i;
  1703. int sg_count;
  1704. sg = (struct scatterlist *) scsicmd->request_buffer;
  1705. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  1706. scsicmd->sc_data_direction);
  1707. psg->count = cpu_to_le32(sg_count);
  1708. byte_count = 0;
  1709. for (i = 0; i < sg_count; i++) {
  1710. le_addr = cpu_to_le64(sg_dma_address(sg));
  1711. psg->sg[i].addr[1] = (u32)(le_addr>>32);
  1712. psg->sg[i].addr[0] = (u32)(le_addr & 0xffffffff);
  1713. psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
  1714. byte_count += sg_dma_len(sg);
  1715. sg++;
  1716. }
  1717. /* hba wants the size to be exact */
  1718. if(byte_count > scsicmd->request_bufflen){
  1719. psg->sg[i-1].count -= (byte_count - scsicmd->request_bufflen);
  1720. byte_count = scsicmd->request_bufflen;
  1721. }
  1722. /* Check for command underflow */
  1723. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  1724. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  1725. byte_count, scsicmd->underflow);
  1726. }
  1727. }
  1728. else if(scsicmd->request_bufflen) {
  1729. dma_addr_t addr;
  1730. addr = pci_map_single(dev->pdev,
  1731. scsicmd->request_buffer,
  1732. scsicmd->request_bufflen,
  1733. scsicmd->sc_data_direction);
  1734. psg->count = cpu_to_le32(1);
  1735. le_addr = cpu_to_le64(addr);
  1736. psg->sg[0].addr[1] = (u32)(le_addr>>32);
  1737. psg->sg[0].addr[0] = (u32)(le_addr & 0xffffffff);
  1738. psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
  1739. scsicmd->SCp.dma_handle = addr;
  1740. byte_count = scsicmd->request_bufflen;
  1741. }
  1742. return byte_count;
  1743. }
  1744. #ifdef AAC_DETAILED_STATUS_INFO
  1745. struct aac_srb_status_info {
  1746. u32 status;
  1747. char *str;
  1748. };
  1749. static struct aac_srb_status_info srb_status_info[] = {
  1750. { SRB_STATUS_PENDING, "Pending Status"},
  1751. { SRB_STATUS_SUCCESS, "Success"},
  1752. { SRB_STATUS_ABORTED, "Aborted Command"},
  1753. { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
  1754. { SRB_STATUS_ERROR, "Error Event"},
  1755. { SRB_STATUS_BUSY, "Device Busy"},
  1756. { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
  1757. { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
  1758. { SRB_STATUS_NO_DEVICE, "No Device"},
  1759. { SRB_STATUS_TIMEOUT, "Timeout"},
  1760. { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
  1761. { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
  1762. { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
  1763. { SRB_STATUS_BUS_RESET, "Bus Reset"},
  1764. { SRB_STATUS_PARITY_ERROR, "Parity Error"},
  1765. { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
  1766. { SRB_STATUS_NO_HBA, "No HBA"},
  1767. { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
  1768. { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
  1769. { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
  1770. { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
  1771. { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
  1772. { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
  1773. { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
  1774. { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
  1775. { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
  1776. { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
  1777. { SRB_STATUS_NOT_STARTED, "Not Started"},
  1778. { SRB_STATUS_NOT_IN_USE, "Not In Use"},
  1779. { SRB_STATUS_FORCE_ABORT, "Force Abort"},
  1780. { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
  1781. { 0xff, "Unknown Error"}
  1782. };
  1783. char *aac_get_status_string(u32 status)
  1784. {
  1785. int i;
  1786. for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
  1787. if(srb_status_info[i].status == status){
  1788. return srb_status_info[i].str;
  1789. }
  1790. }
  1791. return "Bad Status Code";
  1792. }
  1793. #endif