aachba.c 71 KB

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