aachba.c 71 KB

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