aachba.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471
  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. 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(struct aac_dev *dev, void *data, int tindex)
  529. {
  530. struct scsi_inq *str;
  531. str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
  532. memset(str, ' ', sizeof(*str));
  533. if (dev->supplement_adapter_info.AdapterTypeText[0]) {
  534. char * cp = dev->supplement_adapter_info.AdapterTypeText;
  535. int c = sizeof(str->vid);
  536. while (*cp && *cp != ' ' && --c)
  537. ++cp;
  538. c = *cp;
  539. *cp = '\0';
  540. inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
  541. str->vid);
  542. *cp = c;
  543. while (*cp && *cp != ' ')
  544. ++cp;
  545. while (*cp == ' ')
  546. ++cp;
  547. /* last six chars reserved for vol type */
  548. c = 0;
  549. if (strlen(cp) > sizeof(str->pid)) {
  550. c = cp[sizeof(str->pid)];
  551. cp[sizeof(str->pid)] = '\0';
  552. }
  553. inqstrcpy (cp, str->pid);
  554. if (c)
  555. cp[sizeof(str->pid)] = c;
  556. } else {
  557. struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
  558. inqstrcpy (mp->vname, str->vid);
  559. /* last six chars reserved for vol type */
  560. inqstrcpy (mp->model, str->pid);
  561. }
  562. if (tindex < (sizeof(container_types)/sizeof(char *))){
  563. char *findit = str->pid;
  564. for ( ; *findit != ' '; findit++); /* walk till we find a space */
  565. /* RAID is superfluous in the context of a RAID device */
  566. if (memcmp(findit-4, "RAID", 4) == 0)
  567. *(findit -= 4) = ' ';
  568. if (((findit - str->pid) + strlen(container_types[tindex]))
  569. < (sizeof(str->pid) + sizeof(str->prl)))
  570. inqstrcpy (container_types[tindex], findit + 1);
  571. }
  572. inqstrcpy ("V1.0", str->prl);
  573. }
  574. static void set_sense(u8 *sense_buf, u8 sense_key, u8 sense_code,
  575. u8 a_sense_code, u8 incorrect_length,
  576. u8 bit_pointer, u16 field_pointer,
  577. u32 residue)
  578. {
  579. sense_buf[0] = 0xF0; /* Sense data valid, err code 70h (current error) */
  580. sense_buf[1] = 0; /* Segment number, always zero */
  581. if (incorrect_length) {
  582. sense_buf[2] = sense_key | 0x20;/* Set ILI bit | sense key */
  583. sense_buf[3] = BYTE3(residue);
  584. sense_buf[4] = BYTE2(residue);
  585. sense_buf[5] = BYTE1(residue);
  586. sense_buf[6] = BYTE0(residue);
  587. } else
  588. sense_buf[2] = sense_key; /* Sense key */
  589. if (sense_key == ILLEGAL_REQUEST)
  590. sense_buf[7] = 10; /* Additional sense length */
  591. else
  592. sense_buf[7] = 6; /* Additional sense length */
  593. sense_buf[12] = sense_code; /* Additional sense code */
  594. sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
  595. if (sense_key == ILLEGAL_REQUEST) {
  596. sense_buf[15] = 0;
  597. if (sense_code == SENCODE_INVALID_PARAM_FIELD)
  598. sense_buf[15] = 0x80;/* Std sense key specific field */
  599. /* Illegal parameter is in the parameter block */
  600. if (sense_code == SENCODE_INVALID_CDB_FIELD)
  601. sense_buf[15] = 0xc0;/* Std sense key specific field */
  602. /* Illegal parameter is in the CDB block */
  603. sense_buf[15] |= bit_pointer;
  604. sense_buf[16] = field_pointer >> 8; /* MSB */
  605. sense_buf[17] = field_pointer; /* LSB */
  606. }
  607. }
  608. int aac_get_adapter_info(struct aac_dev* dev)
  609. {
  610. struct fib* fibptr;
  611. int rcode;
  612. u32 tmp;
  613. struct aac_adapter_info *info;
  614. struct aac_bus_info *command;
  615. struct aac_bus_info_response *bus_info;
  616. if (!(fibptr = fib_alloc(dev)))
  617. return -ENOMEM;
  618. fib_init(fibptr);
  619. info = (struct aac_adapter_info *) fib_data(fibptr);
  620. memset(info,0,sizeof(*info));
  621. rcode = fib_send(RequestAdapterInfo,
  622. fibptr,
  623. sizeof(*info),
  624. FsaNormal,
  625. -1, 1, /* First `interrupt' command uses special wait */
  626. NULL,
  627. NULL);
  628. if (rcode < 0) {
  629. fib_complete(fibptr);
  630. fib_free(fibptr);
  631. return rcode;
  632. }
  633. memcpy(&dev->adapter_info, info, sizeof(*info));
  634. if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
  635. struct aac_supplement_adapter_info * info;
  636. fib_init(fibptr);
  637. info = (struct aac_supplement_adapter_info *) fib_data(fibptr);
  638. memset(info,0,sizeof(*info));
  639. rcode = fib_send(RequestSupplementAdapterInfo,
  640. fibptr,
  641. sizeof(*info),
  642. FsaNormal,
  643. 1, 1,
  644. NULL,
  645. NULL);
  646. if (rcode >= 0)
  647. memcpy(&dev->supplement_adapter_info, info, sizeof(*info));
  648. }
  649. /*
  650. * GetBusInfo
  651. */
  652. fib_init(fibptr);
  653. bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
  654. memset(bus_info, 0, sizeof(*bus_info));
  655. command = (struct aac_bus_info *)bus_info;
  656. command->Command = cpu_to_le32(VM_Ioctl);
  657. command->ObjType = cpu_to_le32(FT_DRIVE);
  658. command->MethodId = cpu_to_le32(1);
  659. command->CtlCmd = cpu_to_le32(GetBusInfo);
  660. rcode = fib_send(ContainerCommand,
  661. fibptr,
  662. sizeof (*bus_info),
  663. FsaNormal,
  664. 1, 1,
  665. NULL, NULL);
  666. if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
  667. dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
  668. dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
  669. }
  670. tmp = le32_to_cpu(dev->adapter_info.kernelrev);
  671. printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
  672. dev->name,
  673. dev->id,
  674. tmp>>24,
  675. (tmp>>16)&0xff,
  676. tmp&0xff,
  677. le32_to_cpu(dev->adapter_info.kernelbuild),
  678. (int)sizeof(dev->supplement_adapter_info.BuildDate),
  679. dev->supplement_adapter_info.BuildDate);
  680. tmp = le32_to_cpu(dev->adapter_info.monitorrev);
  681. printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
  682. dev->name, dev->id,
  683. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  684. le32_to_cpu(dev->adapter_info.monitorbuild));
  685. tmp = le32_to_cpu(dev->adapter_info.biosrev);
  686. printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
  687. dev->name, dev->id,
  688. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  689. le32_to_cpu(dev->adapter_info.biosbuild));
  690. if (le32_to_cpu(dev->adapter_info.serial[0]) != 0xBAD0)
  691. printk(KERN_INFO "%s%d: serial %x\n",
  692. dev->name, dev->id,
  693. le32_to_cpu(dev->adapter_info.serial[0]));
  694. dev->nondasd_support = 0;
  695. dev->raid_scsi_mode = 0;
  696. if(dev->adapter_info.options & AAC_OPT_NONDASD){
  697. dev->nondasd_support = 1;
  698. }
  699. /*
  700. * If the firmware supports ROMB RAID/SCSI mode and we are currently
  701. * in RAID/SCSI mode, set the flag. For now if in this mode we will
  702. * force nondasd support on. If we decide to allow the non-dasd flag
  703. * additional changes changes will have to be made to support
  704. * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
  705. * changed to support the new dev->raid_scsi_mode flag instead of
  706. * leaching off of the dev->nondasd_support flag. Also in linit.c the
  707. * function aac_detect will have to be modified where it sets up the
  708. * max number of channels based on the aac->nondasd_support flag only.
  709. */
  710. if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
  711. (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
  712. dev->nondasd_support = 1;
  713. dev->raid_scsi_mode = 1;
  714. }
  715. if (dev->raid_scsi_mode != 0)
  716. printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
  717. dev->name, dev->id);
  718. if(nondasd != -1) {
  719. dev->nondasd_support = (nondasd!=0);
  720. }
  721. if(dev->nondasd_support != 0){
  722. printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
  723. }
  724. dev->dac_support = 0;
  725. if( (sizeof(dma_addr_t) > 4) && (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)){
  726. printk(KERN_INFO "%s%d: 64bit support enabled.\n", dev->name, dev->id);
  727. dev->dac_support = 1;
  728. }
  729. if(dacmode != -1) {
  730. dev->dac_support = (dacmode!=0);
  731. }
  732. if(dev->dac_support != 0) {
  733. if (!pci_set_dma_mask(dev->pdev, DMA_64BIT_MASK) &&
  734. !pci_set_consistent_dma_mask(dev->pdev, DMA_64BIT_MASK)) {
  735. printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
  736. dev->name, dev->id);
  737. } else if (!pci_set_dma_mask(dev->pdev, DMA_32BIT_MASK) &&
  738. !pci_set_consistent_dma_mask(dev->pdev, DMA_32BIT_MASK)) {
  739. printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
  740. dev->name, dev->id);
  741. dev->dac_support = 0;
  742. } else {
  743. printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
  744. dev->name, dev->id);
  745. rcode = -ENOMEM;
  746. }
  747. }
  748. /*
  749. * 57 scatter gather elements
  750. */
  751. if (!(dev->raw_io_interface)) {
  752. dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
  753. sizeof(struct aac_fibhdr) -
  754. sizeof(struct aac_write) + sizeof(struct sgentry)) /
  755. sizeof(struct sgentry);
  756. if (dev->dac_support) {
  757. /*
  758. * 38 scatter gather elements
  759. */
  760. dev->scsi_host_ptr->sg_tablesize =
  761. (dev->max_fib_size -
  762. sizeof(struct aac_fibhdr) -
  763. sizeof(struct aac_write64) +
  764. sizeof(struct sgentry64)) /
  765. sizeof(struct sgentry64);
  766. }
  767. dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
  768. if(!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
  769. /*
  770. * Worst case size that could cause sg overflow when
  771. * we break up SG elements that are larger than 64KB.
  772. * Would be nice if we could tell the SCSI layer what
  773. * the maximum SG element size can be. Worst case is
  774. * (sg_tablesize-1) 4KB elements with one 64KB
  775. * element.
  776. * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
  777. */
  778. dev->scsi_host_ptr->max_sectors =
  779. (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
  780. }
  781. }
  782. fib_complete(fibptr);
  783. fib_free(fibptr);
  784. return rcode;
  785. }
  786. static void io_callback(void *context, struct fib * fibptr)
  787. {
  788. struct aac_dev *dev;
  789. struct aac_read_reply *readreply;
  790. struct scsi_cmnd *scsicmd;
  791. u32 cid;
  792. scsicmd = (struct scsi_cmnd *) context;
  793. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  794. cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
  795. if (nblank(dprintk(x))) {
  796. u64 lba;
  797. switch (scsicmd->cmnd[0]) {
  798. case WRITE_6:
  799. case READ_6:
  800. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
  801. (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  802. break;
  803. case WRITE_16:
  804. case READ_16:
  805. lba = ((u64)scsicmd->cmnd[2] << 56) |
  806. ((u64)scsicmd->cmnd[3] << 48) |
  807. ((u64)scsicmd->cmnd[4] << 40) |
  808. ((u64)scsicmd->cmnd[5] << 32) |
  809. ((u64)scsicmd->cmnd[6] << 24) |
  810. (scsicmd->cmnd[7] << 16) |
  811. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  812. break;
  813. case WRITE_12:
  814. case READ_12:
  815. lba = ((u64)scsicmd->cmnd[2] << 24) |
  816. (scsicmd->cmnd[3] << 16) |
  817. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  818. break;
  819. default:
  820. lba = ((u64)scsicmd->cmnd[2] << 24) |
  821. (scsicmd->cmnd[3] << 16) |
  822. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  823. break;
  824. }
  825. printk(KERN_DEBUG
  826. "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
  827. smp_processor_id(), (unsigned long long)lba, jiffies);
  828. }
  829. if (fibptr == NULL)
  830. BUG();
  831. if(scsicmd->use_sg)
  832. pci_unmap_sg(dev->pdev,
  833. (struct scatterlist *)scsicmd->buffer,
  834. scsicmd->use_sg,
  835. scsicmd->sc_data_direction);
  836. else if(scsicmd->request_bufflen)
  837. pci_unmap_single(dev->pdev, scsicmd->SCp.dma_handle,
  838. scsicmd->request_bufflen,
  839. scsicmd->sc_data_direction);
  840. readreply = (struct aac_read_reply *)fib_data(fibptr);
  841. if (le32_to_cpu(readreply->status) == ST_OK)
  842. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  843. else {
  844. #ifdef AAC_DETAILED_STATUS_INFO
  845. printk(KERN_WARNING "io_callback: io failed, status = %d\n",
  846. le32_to_cpu(readreply->status));
  847. #endif
  848. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  849. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  850. HARDWARE_ERROR,
  851. SENCODE_INTERNAL_TARGET_FAILURE,
  852. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  853. 0, 0);
  854. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  855. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  856. ? sizeof(scsicmd->sense_buffer)
  857. : sizeof(dev->fsa_dev[cid].sense_data));
  858. }
  859. fib_complete(fibptr);
  860. fib_free(fibptr);
  861. aac_io_done(scsicmd);
  862. }
  863. static int aac_read(struct scsi_cmnd * scsicmd, int cid)
  864. {
  865. u64 lba;
  866. u32 count;
  867. int status;
  868. u16 fibsize;
  869. struct aac_dev *dev;
  870. struct fib * cmd_fibcontext;
  871. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  872. /*
  873. * Get block address and transfer length
  874. */
  875. switch (scsicmd->cmnd[0]) {
  876. case READ_6:
  877. dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", cid));
  878. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
  879. (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  880. count = scsicmd->cmnd[4];
  881. if (count == 0)
  882. count = 256;
  883. break;
  884. case READ_16:
  885. dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", cid));
  886. lba = ((u64)scsicmd->cmnd[2] << 56) |
  887. ((u64)scsicmd->cmnd[3] << 48) |
  888. ((u64)scsicmd->cmnd[4] << 40) |
  889. ((u64)scsicmd->cmnd[5] << 32) |
  890. ((u64)scsicmd->cmnd[6] << 24) |
  891. (scsicmd->cmnd[7] << 16) |
  892. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  893. count = (scsicmd->cmnd[10] << 24) |
  894. (scsicmd->cmnd[11] << 16) |
  895. (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
  896. break;
  897. case READ_12:
  898. dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", cid));
  899. lba = ((u64)scsicmd->cmnd[2] << 24) |
  900. (scsicmd->cmnd[3] << 16) |
  901. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  902. count = (scsicmd->cmnd[6] << 24) |
  903. (scsicmd->cmnd[7] << 16) |
  904. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  905. break;
  906. default:
  907. dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", cid));
  908. lba = ((u64)scsicmd->cmnd[2] << 24) |
  909. (scsicmd->cmnd[3] << 16) |
  910. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  911. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  912. break;
  913. }
  914. dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
  915. smp_processor_id(), (unsigned long long)lba, jiffies));
  916. if ((!(dev->raw_io_interface) || !(dev->raw_io_64)) &&
  917. (lba & 0xffffffff00000000LL)) {
  918. dprintk((KERN_DEBUG "aac_read: Illegal lba\n"));
  919. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  920. SAM_STAT_CHECK_CONDITION;
  921. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  922. HARDWARE_ERROR,
  923. SENCODE_INTERNAL_TARGET_FAILURE,
  924. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  925. 0, 0);
  926. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  927. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  928. ? sizeof(scsicmd->sense_buffer)
  929. : sizeof(dev->fsa_dev[cid].sense_data));
  930. scsicmd->scsi_done(scsicmd);
  931. return 0;
  932. }
  933. /*
  934. * Alocate and initialize a Fib
  935. */
  936. if (!(cmd_fibcontext = fib_alloc(dev))) {
  937. return -1;
  938. }
  939. fib_init(cmd_fibcontext);
  940. if (dev->raw_io_interface) {
  941. struct aac_raw_io *readcmd;
  942. readcmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
  943. readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
  944. readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  945. readcmd->count = cpu_to_le32(count<<9);
  946. readcmd->cid = cpu_to_le16(cid);
  947. readcmd->flags = cpu_to_le16(1);
  948. readcmd->bpTotal = 0;
  949. readcmd->bpComplete = 0;
  950. aac_build_sgraw(scsicmd, &readcmd->sg);
  951. fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(readcmd->sg.count) - 1) * sizeof (struct sgentryraw));
  952. if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
  953. BUG();
  954. /*
  955. * Now send the Fib to the adapter
  956. */
  957. status = fib_send(ContainerRawIo,
  958. cmd_fibcontext,
  959. fibsize,
  960. FsaNormal,
  961. 0, 1,
  962. (fib_callback) io_callback,
  963. (void *) scsicmd);
  964. } else if (dev->dac_support == 1) {
  965. struct aac_read64 *readcmd;
  966. readcmd = (struct aac_read64 *) fib_data(cmd_fibcontext);
  967. readcmd->command = cpu_to_le32(VM_CtHostRead64);
  968. readcmd->cid = cpu_to_le16(cid);
  969. readcmd->sector_count = cpu_to_le16(count);
  970. readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  971. readcmd->pad = 0;
  972. readcmd->flags = 0;
  973. aac_build_sg64(scsicmd, &readcmd->sg);
  974. fibsize = sizeof(struct aac_read64) +
  975. ((le32_to_cpu(readcmd->sg.count) - 1) *
  976. sizeof (struct sgentry64));
  977. BUG_ON (fibsize > (dev->max_fib_size -
  978. sizeof(struct aac_fibhdr)));
  979. /*
  980. * Now send the Fib to the adapter
  981. */
  982. status = fib_send(ContainerCommand64,
  983. cmd_fibcontext,
  984. fibsize,
  985. FsaNormal,
  986. 0, 1,
  987. (fib_callback) io_callback,
  988. (void *) scsicmd);
  989. } else {
  990. struct aac_read *readcmd;
  991. readcmd = (struct aac_read *) fib_data(cmd_fibcontext);
  992. readcmd->command = cpu_to_le32(VM_CtBlockRead);
  993. readcmd->cid = cpu_to_le32(cid);
  994. readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  995. readcmd->count = cpu_to_le32(count * 512);
  996. aac_build_sg(scsicmd, &readcmd->sg);
  997. fibsize = sizeof(struct aac_read) +
  998. ((le32_to_cpu(readcmd->sg.count) - 1) *
  999. sizeof (struct sgentry));
  1000. BUG_ON (fibsize > (dev->max_fib_size -
  1001. sizeof(struct aac_fibhdr)));
  1002. /*
  1003. * Now send the Fib to the adapter
  1004. */
  1005. status = fib_send(ContainerCommand,
  1006. cmd_fibcontext,
  1007. fibsize,
  1008. FsaNormal,
  1009. 0, 1,
  1010. (fib_callback) io_callback,
  1011. (void *) scsicmd);
  1012. }
  1013. /*
  1014. * Check that the command queued to the controller
  1015. */
  1016. if (status == -EINPROGRESS)
  1017. return 0;
  1018. printk(KERN_WARNING "aac_read: fib_send failed with status: %d.\n", status);
  1019. /*
  1020. * For some reason, the Fib didn't queue, return QUEUE_FULL
  1021. */
  1022. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  1023. aac_io_done(scsicmd);
  1024. fib_complete(cmd_fibcontext);
  1025. fib_free(cmd_fibcontext);
  1026. return 0;
  1027. }
  1028. static int aac_write(struct scsi_cmnd * scsicmd, int cid)
  1029. {
  1030. u64 lba;
  1031. u32 count;
  1032. int status;
  1033. u16 fibsize;
  1034. struct aac_dev *dev;
  1035. struct fib * cmd_fibcontext;
  1036. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1037. /*
  1038. * Get block address and transfer length
  1039. */
  1040. if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
  1041. {
  1042. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  1043. count = scsicmd->cmnd[4];
  1044. if (count == 0)
  1045. count = 256;
  1046. } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
  1047. dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", cid));
  1048. lba = ((u64)scsicmd->cmnd[2] << 56) |
  1049. ((u64)scsicmd->cmnd[3] << 48) |
  1050. ((u64)scsicmd->cmnd[4] << 40) |
  1051. ((u64)scsicmd->cmnd[5] << 32) |
  1052. ((u64)scsicmd->cmnd[6] << 24) |
  1053. (scsicmd->cmnd[7] << 16) |
  1054. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1055. count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
  1056. (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
  1057. } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
  1058. dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", cid));
  1059. lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
  1060. | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1061. count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
  1062. | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1063. } else {
  1064. dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", cid));
  1065. lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1066. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  1067. }
  1068. dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
  1069. smp_processor_id(), (unsigned long long)lba, jiffies));
  1070. if ((!(dev->raw_io_interface) || !(dev->raw_io_64))
  1071. && (lba & 0xffffffff00000000LL)) {
  1072. dprintk((KERN_DEBUG "aac_write: Illegal lba\n"));
  1073. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1074. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1075. HARDWARE_ERROR,
  1076. SENCODE_INTERNAL_TARGET_FAILURE,
  1077. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0,
  1078. 0, 0);
  1079. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1080. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1081. ? sizeof(scsicmd->sense_buffer)
  1082. : sizeof(dev->fsa_dev[cid].sense_data));
  1083. scsicmd->scsi_done(scsicmd);
  1084. return 0;
  1085. }
  1086. /*
  1087. * Allocate and initialize a Fib then setup a BlockWrite command
  1088. */
  1089. if (!(cmd_fibcontext = fib_alloc(dev))) {
  1090. scsicmd->result = DID_ERROR << 16;
  1091. aac_io_done(scsicmd);
  1092. return 0;
  1093. }
  1094. fib_init(cmd_fibcontext);
  1095. if (dev->raw_io_interface) {
  1096. struct aac_raw_io *writecmd;
  1097. writecmd = (struct aac_raw_io *) fib_data(cmd_fibcontext);
  1098. writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
  1099. writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  1100. writecmd->count = cpu_to_le32(count<<9);
  1101. writecmd->cid = cpu_to_le16(cid);
  1102. writecmd->flags = 0;
  1103. writecmd->bpTotal = 0;
  1104. writecmd->bpComplete = 0;
  1105. aac_build_sgraw(scsicmd, &writecmd->sg);
  1106. fibsize = sizeof(struct aac_raw_io) + ((le32_to_cpu(writecmd->sg.count) - 1) * sizeof (struct sgentryraw));
  1107. if (fibsize > (dev->max_fib_size - sizeof(struct aac_fibhdr)))
  1108. BUG();
  1109. /*
  1110. * Now send the Fib to the adapter
  1111. */
  1112. status = fib_send(ContainerRawIo,
  1113. cmd_fibcontext,
  1114. fibsize,
  1115. FsaNormal,
  1116. 0, 1,
  1117. (fib_callback) io_callback,
  1118. (void *) scsicmd);
  1119. } else if (dev->dac_support == 1) {
  1120. struct aac_write64 *writecmd;
  1121. writecmd = (struct aac_write64 *) fib_data(cmd_fibcontext);
  1122. writecmd->command = cpu_to_le32(VM_CtHostWrite64);
  1123. writecmd->cid = cpu_to_le16(cid);
  1124. writecmd->sector_count = cpu_to_le16(count);
  1125. writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  1126. writecmd->pad = 0;
  1127. writecmd->flags = 0;
  1128. aac_build_sg64(scsicmd, &writecmd->sg);
  1129. fibsize = sizeof(struct aac_write64) +
  1130. ((le32_to_cpu(writecmd->sg.count) - 1) *
  1131. sizeof (struct sgentry64));
  1132. BUG_ON (fibsize > (dev->max_fib_size -
  1133. sizeof(struct aac_fibhdr)));
  1134. /*
  1135. * Now send the Fib to the adapter
  1136. */
  1137. status = fib_send(ContainerCommand64,
  1138. cmd_fibcontext,
  1139. fibsize,
  1140. FsaNormal,
  1141. 0, 1,
  1142. (fib_callback) io_callback,
  1143. (void *) scsicmd);
  1144. } else {
  1145. struct aac_write *writecmd;
  1146. writecmd = (struct aac_write *) fib_data(cmd_fibcontext);
  1147. writecmd->command = cpu_to_le32(VM_CtBlockWrite);
  1148. writecmd->cid = cpu_to_le32(cid);
  1149. writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  1150. writecmd->count = cpu_to_le32(count * 512);
  1151. writecmd->sg.count = cpu_to_le32(1);
  1152. /* ->stable is not used - it did mean which type of write */
  1153. aac_build_sg(scsicmd, &writecmd->sg);
  1154. fibsize = sizeof(struct aac_write) +
  1155. ((le32_to_cpu(writecmd->sg.count) - 1) *
  1156. sizeof (struct sgentry));
  1157. BUG_ON (fibsize > (dev->max_fib_size -
  1158. sizeof(struct aac_fibhdr)));
  1159. /*
  1160. * Now send the Fib to the adapter
  1161. */
  1162. status = fib_send(ContainerCommand,
  1163. cmd_fibcontext,
  1164. fibsize,
  1165. FsaNormal,
  1166. 0, 1,
  1167. (fib_callback) io_callback,
  1168. (void *) scsicmd);
  1169. }
  1170. /*
  1171. * Check that the command queued to the controller
  1172. */
  1173. if (status == -EINPROGRESS)
  1174. {
  1175. return 0;
  1176. }
  1177. printk(KERN_WARNING "aac_write: fib_send failed with status: %d\n", status);
  1178. /*
  1179. * For some reason, the Fib didn't queue, return QUEUE_FULL
  1180. */
  1181. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  1182. aac_io_done(scsicmd);
  1183. fib_complete(cmd_fibcontext);
  1184. fib_free(cmd_fibcontext);
  1185. return 0;
  1186. }
  1187. static void synchronize_callback(void *context, struct fib *fibptr)
  1188. {
  1189. struct aac_synchronize_reply *synchronizereply;
  1190. struct scsi_cmnd *cmd;
  1191. cmd = context;
  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 = ID_LUN_TO_CONTAINER(sdev->id, sdev->lun);
  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. fib_complete(fibptr);
  1218. fib_free(fibptr);
  1219. aac_io_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 commands to complete to this specific
  1232. * 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->serial_number != 0) {
  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. fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata)))
  1251. return SCSI_MLQUEUE_HOST_BUSY;
  1252. 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 = 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. return 0;
  1274. printk(KERN_WARNING
  1275. "aac_synchronize: fib_send failed with status: %d.\n", status);
  1276. fib_complete(cmd_fibcontext);
  1277. fib_free(cmd_fibcontext);
  1278. return SCSI_MLQUEUE_HOST_BUSY;
  1279. }
  1280. /**
  1281. * aac_scsi_cmd() - Process SCSI command
  1282. * @scsicmd: SCSI command block
  1283. *
  1284. * Emulate a SCSI command and queue the required request for the
  1285. * aacraid firmware.
  1286. */
  1287. int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
  1288. {
  1289. u32 cid = 0;
  1290. struct Scsi_Host *host = scsicmd->device->host;
  1291. struct aac_dev *dev = (struct aac_dev *)host->hostdata;
  1292. struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
  1293. int ret;
  1294. /*
  1295. * If the bus, id or lun is out of range, return fail
  1296. * Test does not apply to ID 16, the pseudo id for the controller
  1297. * itself.
  1298. */
  1299. if (scsicmd->device->id != host->this_id) {
  1300. if ((scsicmd->device->channel == 0) ){
  1301. if( (scsicmd->device->id >= dev->maximum_num_containers) || (scsicmd->device->lun != 0)){
  1302. scsicmd->result = DID_NO_CONNECT << 16;
  1303. scsicmd->scsi_done(scsicmd);
  1304. return 0;
  1305. }
  1306. cid = ID_LUN_TO_CONTAINER(scsicmd->device->id, scsicmd->device->lun);
  1307. /*
  1308. * If the target container doesn't exist, it may have
  1309. * been newly created
  1310. */
  1311. if ((fsa_dev_ptr[cid].valid & 1) == 0) {
  1312. switch (scsicmd->cmnd[0]) {
  1313. case SERVICE_ACTION_IN:
  1314. if (!(dev->raw_io_interface) ||
  1315. !(dev->raw_io_64) ||
  1316. ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
  1317. break;
  1318. case INQUIRY:
  1319. case READ_CAPACITY:
  1320. case TEST_UNIT_READY:
  1321. spin_unlock_irq(host->host_lock);
  1322. probe_container(dev, cid);
  1323. if ((fsa_dev_ptr[cid].valid & 1) == 0)
  1324. fsa_dev_ptr[cid].valid = 0;
  1325. spin_lock_irq(host->host_lock);
  1326. if (fsa_dev_ptr[cid].valid == 0) {
  1327. scsicmd->result = DID_NO_CONNECT << 16;
  1328. scsicmd->scsi_done(scsicmd);
  1329. return 0;
  1330. }
  1331. default:
  1332. break;
  1333. }
  1334. }
  1335. /*
  1336. * If the target container still doesn't exist,
  1337. * return failure
  1338. */
  1339. if (fsa_dev_ptr[cid].valid == 0) {
  1340. scsicmd->result = DID_BAD_TARGET << 16;
  1341. scsicmd->scsi_done(scsicmd);
  1342. return 0;
  1343. }
  1344. } else { /* check for physical non-dasd devices */
  1345. if(dev->nondasd_support == 1){
  1346. return aac_send_srb_fib(scsicmd);
  1347. } else {
  1348. scsicmd->result = DID_NO_CONNECT << 16;
  1349. scsicmd->scsi_done(scsicmd);
  1350. return 0;
  1351. }
  1352. }
  1353. }
  1354. /*
  1355. * else Command for the controller itself
  1356. */
  1357. else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
  1358. (scsicmd->cmnd[0] != TEST_UNIT_READY))
  1359. {
  1360. dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
  1361. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1362. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1363. ILLEGAL_REQUEST,
  1364. SENCODE_INVALID_COMMAND,
  1365. ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
  1366. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1367. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1368. ? sizeof(scsicmd->sense_buffer)
  1369. : sizeof(dev->fsa_dev[cid].sense_data));
  1370. scsicmd->scsi_done(scsicmd);
  1371. return 0;
  1372. }
  1373. /* Handle commands here that don't really require going out to the adapter */
  1374. switch (scsicmd->cmnd[0]) {
  1375. case INQUIRY:
  1376. {
  1377. struct inquiry_data inq_data;
  1378. dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", scsicmd->device->id));
  1379. memset(&inq_data, 0, sizeof (struct inquiry_data));
  1380. inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
  1381. 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 */
  1382. inq_data.inqd_len = 31;
  1383. /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
  1384. inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
  1385. /*
  1386. * Set the Vendor, Product, and Revision Level
  1387. * see: <vendor>.c i.e. aac.c
  1388. */
  1389. if (scsicmd->device->id == host->this_id) {
  1390. setinqstr(dev, (void *) (inq_data.inqd_vid), (sizeof(container_types)/sizeof(char *)));
  1391. inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
  1392. aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
  1393. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1394. scsicmd->scsi_done(scsicmd);
  1395. return 0;
  1396. }
  1397. setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
  1398. inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
  1399. aac_internal_transfer(scsicmd, &inq_data, 0, sizeof(inq_data));
  1400. return aac_get_container_name(scsicmd, cid);
  1401. }
  1402. case SERVICE_ACTION_IN:
  1403. if (!(dev->raw_io_interface) ||
  1404. !(dev->raw_io_64) ||
  1405. ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
  1406. break;
  1407. {
  1408. u64 capacity;
  1409. char cp[12];
  1410. unsigned int offset = 0;
  1411. dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
  1412. capacity = fsa_dev_ptr[cid].size - 1;
  1413. if (scsicmd->cmnd[13] > 12) {
  1414. offset = scsicmd->cmnd[13] - 12;
  1415. if (offset > sizeof(cp))
  1416. break;
  1417. memset(cp, 0, offset);
  1418. aac_internal_transfer(scsicmd, cp, 0, offset);
  1419. }
  1420. cp[0] = (capacity >> 56) & 0xff;
  1421. cp[1] = (capacity >> 48) & 0xff;
  1422. cp[2] = (capacity >> 40) & 0xff;
  1423. cp[3] = (capacity >> 32) & 0xff;
  1424. cp[4] = (capacity >> 24) & 0xff;
  1425. cp[5] = (capacity >> 16) & 0xff;
  1426. cp[6] = (capacity >> 8) & 0xff;
  1427. cp[7] = (capacity >> 0) & 0xff;
  1428. cp[8] = 0;
  1429. cp[9] = 0;
  1430. cp[10] = 2;
  1431. cp[11] = 0;
  1432. aac_internal_transfer(scsicmd, cp, offset, sizeof(cp));
  1433. /* Do not cache partition table for arrays */
  1434. scsicmd->device->removable = 1;
  1435. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1436. scsicmd->scsi_done(scsicmd);
  1437. return 0;
  1438. }
  1439. case READ_CAPACITY:
  1440. {
  1441. u32 capacity;
  1442. char cp[8];
  1443. dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
  1444. if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
  1445. capacity = fsa_dev_ptr[cid].size - 1;
  1446. else
  1447. capacity = (u32)-1;
  1448. cp[0] = (capacity >> 24) & 0xff;
  1449. cp[1] = (capacity >> 16) & 0xff;
  1450. cp[2] = (capacity >> 8) & 0xff;
  1451. cp[3] = (capacity >> 0) & 0xff;
  1452. cp[4] = 0;
  1453. cp[5] = 0;
  1454. cp[6] = 2;
  1455. cp[7] = 0;
  1456. aac_internal_transfer(scsicmd, cp, 0, sizeof(cp));
  1457. /* Do not cache partition table for arrays */
  1458. scsicmd->device->removable = 1;
  1459. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1460. scsicmd->scsi_done(scsicmd);
  1461. return 0;
  1462. }
  1463. case MODE_SENSE:
  1464. {
  1465. char mode_buf[4];
  1466. dprintk((KERN_DEBUG "MODE SENSE command.\n"));
  1467. mode_buf[0] = 3; /* Mode data length */
  1468. mode_buf[1] = 0; /* Medium type - default */
  1469. mode_buf[2] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
  1470. mode_buf[3] = 0; /* Block descriptor length */
  1471. aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
  1472. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1473. scsicmd->scsi_done(scsicmd);
  1474. return 0;
  1475. }
  1476. case MODE_SENSE_10:
  1477. {
  1478. char mode_buf[8];
  1479. dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
  1480. mode_buf[0] = 0; /* Mode data length (MSB) */
  1481. mode_buf[1] = 6; /* Mode data length (LSB) */
  1482. mode_buf[2] = 0; /* Medium type - default */
  1483. mode_buf[3] = 0; /* Device-specific param, bit 8: 0/1 = write enabled/protected */
  1484. mode_buf[4] = 0; /* reserved */
  1485. mode_buf[5] = 0; /* reserved */
  1486. mode_buf[6] = 0; /* Block descriptor length (MSB) */
  1487. mode_buf[7] = 0; /* Block descriptor length (LSB) */
  1488. aac_internal_transfer(scsicmd, mode_buf, 0, sizeof(mode_buf));
  1489. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1490. scsicmd->scsi_done(scsicmd);
  1491. return 0;
  1492. }
  1493. case REQUEST_SENSE:
  1494. dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
  1495. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
  1496. memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
  1497. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1498. scsicmd->scsi_done(scsicmd);
  1499. return 0;
  1500. case ALLOW_MEDIUM_REMOVAL:
  1501. dprintk((KERN_DEBUG "LOCK command.\n"));
  1502. if (scsicmd->cmnd[4])
  1503. fsa_dev_ptr[cid].locked = 1;
  1504. else
  1505. fsa_dev_ptr[cid].locked = 0;
  1506. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1507. scsicmd->scsi_done(scsicmd);
  1508. return 0;
  1509. /*
  1510. * These commands are all No-Ops
  1511. */
  1512. case TEST_UNIT_READY:
  1513. case RESERVE:
  1514. case RELEASE:
  1515. case REZERO_UNIT:
  1516. case REASSIGN_BLOCKS:
  1517. case SEEK_10:
  1518. case START_STOP:
  1519. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1520. scsicmd->scsi_done(scsicmd);
  1521. return 0;
  1522. }
  1523. switch (scsicmd->cmnd[0])
  1524. {
  1525. case READ_6:
  1526. case READ_10:
  1527. case READ_12:
  1528. case READ_16:
  1529. /*
  1530. * Hack to keep track of ordinal number of the device that
  1531. * corresponds to a container. Needed to convert
  1532. * containers to /dev/sd device names
  1533. */
  1534. spin_unlock_irq(host->host_lock);
  1535. if (scsicmd->request->rq_disk)
  1536. strlcpy(fsa_dev_ptr[cid].devname,
  1537. scsicmd->request->rq_disk->disk_name,
  1538. min(sizeof(fsa_dev_ptr[cid].devname),
  1539. sizeof(scsicmd->request->rq_disk->disk_name) + 1));
  1540. ret = aac_read(scsicmd, cid);
  1541. spin_lock_irq(host->host_lock);
  1542. return ret;
  1543. case WRITE_6:
  1544. case WRITE_10:
  1545. case WRITE_12:
  1546. case WRITE_16:
  1547. spin_unlock_irq(host->host_lock);
  1548. ret = aac_write(scsicmd, cid);
  1549. spin_lock_irq(host->host_lock);
  1550. return ret;
  1551. case SYNCHRONIZE_CACHE:
  1552. /* Issue FIB to tell Firmware to flush it's cache */
  1553. return aac_synchronize(scsicmd, cid);
  1554. default:
  1555. /*
  1556. * Unhandled commands
  1557. */
  1558. dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
  1559. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1560. set_sense((u8 *) &dev->fsa_dev[cid].sense_data,
  1561. ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
  1562. ASENCODE_INVALID_COMMAND, 0, 0, 0, 0);
  1563. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1564. (sizeof(dev->fsa_dev[cid].sense_data) > sizeof(scsicmd->sense_buffer))
  1565. ? sizeof(scsicmd->sense_buffer)
  1566. : sizeof(dev->fsa_dev[cid].sense_data));
  1567. scsicmd->scsi_done(scsicmd);
  1568. return 0;
  1569. }
  1570. }
  1571. static int query_disk(struct aac_dev *dev, void __user *arg)
  1572. {
  1573. struct aac_query_disk qd;
  1574. struct fsa_dev_info *fsa_dev_ptr;
  1575. fsa_dev_ptr = dev->fsa_dev;
  1576. if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
  1577. return -EFAULT;
  1578. if (qd.cnum == -1)
  1579. qd.cnum = ID_LUN_TO_CONTAINER(qd.id, qd.lun);
  1580. else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
  1581. {
  1582. if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
  1583. return -EINVAL;
  1584. qd.instance = dev->scsi_host_ptr->host_no;
  1585. qd.bus = 0;
  1586. qd.id = CONTAINER_TO_ID(qd.cnum);
  1587. qd.lun = CONTAINER_TO_LUN(qd.cnum);
  1588. }
  1589. else return -EINVAL;
  1590. qd.valid = fsa_dev_ptr[qd.cnum].valid;
  1591. qd.locked = fsa_dev_ptr[qd.cnum].locked;
  1592. qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
  1593. if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
  1594. qd.unmapped = 1;
  1595. else
  1596. qd.unmapped = 0;
  1597. strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
  1598. min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
  1599. if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
  1600. return -EFAULT;
  1601. return 0;
  1602. }
  1603. static int force_delete_disk(struct aac_dev *dev, void __user *arg)
  1604. {
  1605. struct aac_delete_disk dd;
  1606. struct fsa_dev_info *fsa_dev_ptr;
  1607. fsa_dev_ptr = dev->fsa_dev;
  1608. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  1609. return -EFAULT;
  1610. if (dd.cnum >= dev->maximum_num_containers)
  1611. return -EINVAL;
  1612. /*
  1613. * Mark this container as being deleted.
  1614. */
  1615. fsa_dev_ptr[dd.cnum].deleted = 1;
  1616. /*
  1617. * Mark the container as no longer valid
  1618. */
  1619. fsa_dev_ptr[dd.cnum].valid = 0;
  1620. return 0;
  1621. }
  1622. static int delete_disk(struct aac_dev *dev, void __user *arg)
  1623. {
  1624. struct aac_delete_disk dd;
  1625. struct fsa_dev_info *fsa_dev_ptr;
  1626. fsa_dev_ptr = dev->fsa_dev;
  1627. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  1628. return -EFAULT;
  1629. if (dd.cnum >= dev->maximum_num_containers)
  1630. return -EINVAL;
  1631. /*
  1632. * If the container is locked, it can not be deleted by the API.
  1633. */
  1634. if (fsa_dev_ptr[dd.cnum].locked)
  1635. return -EBUSY;
  1636. else {
  1637. /*
  1638. * Mark the container as no longer being valid.
  1639. */
  1640. fsa_dev_ptr[dd.cnum].valid = 0;
  1641. fsa_dev_ptr[dd.cnum].devname[0] = '\0';
  1642. return 0;
  1643. }
  1644. }
  1645. int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
  1646. {
  1647. switch (cmd) {
  1648. case FSACTL_QUERY_DISK:
  1649. return query_disk(dev, arg);
  1650. case FSACTL_DELETE_DISK:
  1651. return delete_disk(dev, arg);
  1652. case FSACTL_FORCE_DELETE_DISK:
  1653. return force_delete_disk(dev, arg);
  1654. case FSACTL_GET_CONTAINERS:
  1655. return aac_get_containers(dev);
  1656. default:
  1657. return -ENOTTY;
  1658. }
  1659. }
  1660. /**
  1661. *
  1662. * aac_srb_callback
  1663. * @context: the context set in the fib - here it is scsi cmd
  1664. * @fibptr: pointer to the fib
  1665. *
  1666. * Handles the completion of a scsi command to a non dasd device
  1667. *
  1668. */
  1669. static void aac_srb_callback(void *context, struct fib * fibptr)
  1670. {
  1671. struct aac_dev *dev;
  1672. struct aac_srb_reply *srbreply;
  1673. struct scsi_cmnd *scsicmd;
  1674. scsicmd = (struct scsi_cmnd *) context;
  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. if(scsicmd->cmnd[0] == INQUIRY ){
  1714. u8 b;
  1715. u8 b1;
  1716. /* We can't expose disk devices because we can't tell whether they
  1717. * are the raw container drives or stand alone drives. If they have
  1718. * the removable bit set then we should expose them though.
  1719. */
  1720. b = (*(u8*)scsicmd->buffer)&0x1f;
  1721. b1 = ((u8*)scsicmd->buffer)[1];
  1722. if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
  1723. || (b==TYPE_DISK && (b1&0x80)) ){
  1724. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1725. /*
  1726. * We will allow disk devices if in RAID/SCSI mode and
  1727. * the channel is 2
  1728. */
  1729. } else if ((dev->raid_scsi_mode) &&
  1730. (scsicmd->device->channel == 2)) {
  1731. scsicmd->result = DID_OK << 16 |
  1732. COMMAND_COMPLETE << 8;
  1733. } else {
  1734. scsicmd->result = DID_NO_CONNECT << 16 |
  1735. COMMAND_COMPLETE << 8;
  1736. }
  1737. } else {
  1738. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1739. }
  1740. break;
  1741. case SRB_STATUS_DATA_OVERRUN:
  1742. switch(scsicmd->cmnd[0]){
  1743. case READ_6:
  1744. case WRITE_6:
  1745. case READ_10:
  1746. case WRITE_10:
  1747. case READ_12:
  1748. case WRITE_12:
  1749. case READ_16:
  1750. case WRITE_16:
  1751. if(le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow ) {
  1752. printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
  1753. } else {
  1754. printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
  1755. }
  1756. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  1757. break;
  1758. case INQUIRY: {
  1759. u8 b;
  1760. u8 b1;
  1761. /* We can't expose disk devices because we can't tell whether they
  1762. * are the raw container drives or stand alone drives
  1763. */
  1764. b = (*(u8*)scsicmd->buffer)&0x0f;
  1765. b1 = ((u8*)scsicmd->buffer)[1];
  1766. if( b==TYPE_TAPE || b==TYPE_WORM || b==TYPE_ROM || b==TYPE_MOD|| b==TYPE_MEDIUM_CHANGER
  1767. || (b==TYPE_DISK && (b1&0x80)) ){
  1768. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1769. /*
  1770. * We will allow disk devices if in RAID/SCSI mode and
  1771. * the channel is 2
  1772. */
  1773. } else if ((dev->raid_scsi_mode) &&
  1774. (scsicmd->device->channel == 2)) {
  1775. scsicmd->result = DID_OK << 16 |
  1776. COMMAND_COMPLETE << 8;
  1777. } else {
  1778. scsicmd->result = DID_NO_CONNECT << 16 |
  1779. COMMAND_COMPLETE << 8;
  1780. }
  1781. break;
  1782. }
  1783. default:
  1784. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  1785. break;
  1786. }
  1787. break;
  1788. case SRB_STATUS_ABORTED:
  1789. scsicmd->result = DID_ABORT << 16 | ABORT << 8;
  1790. break;
  1791. case SRB_STATUS_ABORT_FAILED:
  1792. // Not sure about this one - but assuming the hba was trying to abort for some reason
  1793. scsicmd->result = DID_ERROR << 16 | ABORT << 8;
  1794. break;
  1795. case SRB_STATUS_PARITY_ERROR:
  1796. scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
  1797. break;
  1798. case SRB_STATUS_NO_DEVICE:
  1799. case SRB_STATUS_INVALID_PATH_ID:
  1800. case SRB_STATUS_INVALID_TARGET_ID:
  1801. case SRB_STATUS_INVALID_LUN:
  1802. case SRB_STATUS_SELECTION_TIMEOUT:
  1803. scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  1804. break;
  1805. case SRB_STATUS_COMMAND_TIMEOUT:
  1806. case SRB_STATUS_TIMEOUT:
  1807. scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
  1808. break;
  1809. case SRB_STATUS_BUSY:
  1810. scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  1811. break;
  1812. case SRB_STATUS_BUS_RESET:
  1813. scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
  1814. break;
  1815. case SRB_STATUS_MESSAGE_REJECTED:
  1816. scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
  1817. break;
  1818. case SRB_STATUS_REQUEST_FLUSHED:
  1819. case SRB_STATUS_ERROR:
  1820. case SRB_STATUS_INVALID_REQUEST:
  1821. case SRB_STATUS_REQUEST_SENSE_FAILED:
  1822. case SRB_STATUS_NO_HBA:
  1823. case SRB_STATUS_UNEXPECTED_BUS_FREE:
  1824. case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
  1825. case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
  1826. case SRB_STATUS_DELAYED_RETRY:
  1827. case SRB_STATUS_BAD_FUNCTION:
  1828. case SRB_STATUS_NOT_STARTED:
  1829. case SRB_STATUS_NOT_IN_USE:
  1830. case SRB_STATUS_FORCE_ABORT:
  1831. case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
  1832. default:
  1833. #ifdef AAC_DETAILED_STATUS_INFO
  1834. printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
  1835. le32_to_cpu(srbreply->srb_status) & 0x3F,
  1836. aac_get_status_string(
  1837. le32_to_cpu(srbreply->srb_status) & 0x3F),
  1838. scsicmd->cmnd[0],
  1839. le32_to_cpu(srbreply->scsi_status));
  1840. #endif
  1841. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  1842. break;
  1843. }
  1844. if (le32_to_cpu(srbreply->scsi_status) == 0x02 ){ // Check Condition
  1845. int len;
  1846. scsicmd->result |= SAM_STAT_CHECK_CONDITION;
  1847. len = (le32_to_cpu(srbreply->sense_data_size) >
  1848. sizeof(scsicmd->sense_buffer)) ?
  1849. sizeof(scsicmd->sense_buffer) :
  1850. le32_to_cpu(srbreply->sense_data_size);
  1851. #ifdef AAC_DETAILED_STATUS_INFO
  1852. printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
  1853. le32_to_cpu(srbreply->status), len);
  1854. #endif
  1855. memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
  1856. }
  1857. /*
  1858. * OR in the scsi status (already shifted up a bit)
  1859. */
  1860. scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
  1861. fib_complete(fibptr);
  1862. fib_free(fibptr);
  1863. aac_io_done(scsicmd);
  1864. }
  1865. /**
  1866. *
  1867. * aac_send_scb_fib
  1868. * @scsicmd: the scsi command block
  1869. *
  1870. * This routine will form a FIB and fill in the aac_srb from the
  1871. * scsicmd passed in.
  1872. */
  1873. static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
  1874. {
  1875. struct fib* cmd_fibcontext;
  1876. struct aac_dev* dev;
  1877. int status;
  1878. struct aac_srb *srbcmd;
  1879. u16 fibsize;
  1880. u32 flag;
  1881. u32 timeout;
  1882. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1883. if (scsicmd->device->id >= dev->maximum_num_physicals ||
  1884. scsicmd->device->lun > 7) {
  1885. scsicmd->result = DID_NO_CONNECT << 16;
  1886. scsicmd->scsi_done(scsicmd);
  1887. return 0;
  1888. }
  1889. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1890. switch(scsicmd->sc_data_direction){
  1891. case DMA_TO_DEVICE:
  1892. flag = SRB_DataOut;
  1893. break;
  1894. case DMA_BIDIRECTIONAL:
  1895. flag = SRB_DataIn | SRB_DataOut;
  1896. break;
  1897. case DMA_FROM_DEVICE:
  1898. flag = SRB_DataIn;
  1899. break;
  1900. case DMA_NONE:
  1901. default: /* shuts up some versions of gcc */
  1902. flag = SRB_NoDataXfer;
  1903. break;
  1904. }
  1905. /*
  1906. * Allocate and initialize a Fib then setup a BlockWrite command
  1907. */
  1908. if (!(cmd_fibcontext = fib_alloc(dev))) {
  1909. return -1;
  1910. }
  1911. fib_init(cmd_fibcontext);
  1912. srbcmd = (struct aac_srb*) fib_data(cmd_fibcontext);
  1913. srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
  1914. srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scsicmd->device->channel));
  1915. srbcmd->id = cpu_to_le32(scsicmd->device->id);
  1916. srbcmd->lun = cpu_to_le32(scsicmd->device->lun);
  1917. srbcmd->flags = cpu_to_le32(flag);
  1918. timeout = scsicmd->timeout_per_command/HZ;
  1919. if(timeout == 0){
  1920. timeout = 1;
  1921. }
  1922. srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
  1923. srbcmd->retry_limit = 0; /* Obsolete parameter */
  1924. srbcmd->cdb_size = cpu_to_le32(scsicmd->cmd_len);
  1925. if( dev->dac_support == 1 ) {
  1926. aac_build_sg64(scsicmd, (struct sgmap64*) &srbcmd->sg);
  1927. srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
  1928. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1929. memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
  1930. /*
  1931. * Build Scatter/Gather list
  1932. */
  1933. fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
  1934. ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
  1935. sizeof (struct sgentry64));
  1936. BUG_ON (fibsize > (dev->max_fib_size -
  1937. sizeof(struct aac_fibhdr)));
  1938. /*
  1939. * Now send the Fib to the adapter
  1940. */
  1941. status = fib_send(ScsiPortCommand64, cmd_fibcontext,
  1942. fibsize, FsaNormal, 0, 1,
  1943. (fib_callback) aac_srb_callback,
  1944. (void *) scsicmd);
  1945. } else {
  1946. aac_build_sg(scsicmd, (struct sgmap*)&srbcmd->sg);
  1947. srbcmd->count = cpu_to_le32(scsicmd->request_bufflen);
  1948. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1949. memcpy(srbcmd->cdb, scsicmd->cmnd, scsicmd->cmd_len);
  1950. /*
  1951. * Build Scatter/Gather list
  1952. */
  1953. fibsize = sizeof (struct aac_srb) +
  1954. (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
  1955. sizeof (struct sgentry));
  1956. BUG_ON (fibsize > (dev->max_fib_size -
  1957. sizeof(struct aac_fibhdr)));
  1958. /*
  1959. * Now send the Fib to the adapter
  1960. */
  1961. status = fib_send(ScsiPortCommand, cmd_fibcontext, fibsize, FsaNormal, 0, 1,
  1962. (fib_callback) aac_srb_callback, (void *) scsicmd);
  1963. }
  1964. /*
  1965. * Check that the command queued to the controller
  1966. */
  1967. if (status == -EINPROGRESS){
  1968. return 0;
  1969. }
  1970. printk(KERN_WARNING "aac_srb: fib_send failed with status: %d\n", status);
  1971. fib_complete(cmd_fibcontext);
  1972. fib_free(cmd_fibcontext);
  1973. return -1;
  1974. }
  1975. static unsigned long aac_build_sg(struct scsi_cmnd* scsicmd, struct sgmap* psg)
  1976. {
  1977. struct aac_dev *dev;
  1978. unsigned long byte_count = 0;
  1979. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1980. // Get rid of old data
  1981. psg->count = 0;
  1982. psg->sg[0].addr = 0;
  1983. psg->sg[0].count = 0;
  1984. if (scsicmd->use_sg) {
  1985. struct scatterlist *sg;
  1986. int i;
  1987. int sg_count;
  1988. sg = (struct scatterlist *) scsicmd->request_buffer;
  1989. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  1990. scsicmd->sc_data_direction);
  1991. psg->count = cpu_to_le32(sg_count);
  1992. byte_count = 0;
  1993. for (i = 0; i < sg_count; i++) {
  1994. psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
  1995. psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
  1996. byte_count += sg_dma_len(sg);
  1997. sg++;
  1998. }
  1999. /* hba wants the size to be exact */
  2000. if(byte_count > scsicmd->request_bufflen){
  2001. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2002. (byte_count - scsicmd->request_bufflen);
  2003. psg->sg[i-1].count = cpu_to_le32(temp);
  2004. byte_count = scsicmd->request_bufflen;
  2005. }
  2006. /* Check for command underflow */
  2007. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2008. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2009. byte_count, scsicmd->underflow);
  2010. }
  2011. }
  2012. else if(scsicmd->request_bufflen) {
  2013. dma_addr_t addr;
  2014. addr = pci_map_single(dev->pdev,
  2015. scsicmd->request_buffer,
  2016. scsicmd->request_bufflen,
  2017. scsicmd->sc_data_direction);
  2018. psg->count = cpu_to_le32(1);
  2019. psg->sg[0].addr = cpu_to_le32(addr);
  2020. psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
  2021. scsicmd->SCp.dma_handle = addr;
  2022. byte_count = scsicmd->request_bufflen;
  2023. }
  2024. return byte_count;
  2025. }
  2026. static unsigned long aac_build_sg64(struct scsi_cmnd* scsicmd, struct sgmap64* psg)
  2027. {
  2028. struct aac_dev *dev;
  2029. unsigned long byte_count = 0;
  2030. u64 addr;
  2031. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  2032. // Get rid of old data
  2033. psg->count = 0;
  2034. psg->sg[0].addr[0] = 0;
  2035. psg->sg[0].addr[1] = 0;
  2036. psg->sg[0].count = 0;
  2037. if (scsicmd->use_sg) {
  2038. struct scatterlist *sg;
  2039. int i;
  2040. int sg_count;
  2041. sg = (struct scatterlist *) scsicmd->request_buffer;
  2042. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  2043. scsicmd->sc_data_direction);
  2044. psg->count = cpu_to_le32(sg_count);
  2045. byte_count = 0;
  2046. for (i = 0; i < sg_count; i++) {
  2047. addr = sg_dma_address(sg);
  2048. psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
  2049. psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
  2050. psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
  2051. byte_count += sg_dma_len(sg);
  2052. sg++;
  2053. }
  2054. /* hba wants the size to be exact */
  2055. if(byte_count > scsicmd->request_bufflen){
  2056. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2057. (byte_count - scsicmd->request_bufflen);
  2058. psg->sg[i-1].count = cpu_to_le32(temp);
  2059. byte_count = scsicmd->request_bufflen;
  2060. }
  2061. /* Check for command underflow */
  2062. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2063. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2064. byte_count, scsicmd->underflow);
  2065. }
  2066. }
  2067. else if(scsicmd->request_bufflen) {
  2068. u64 addr;
  2069. addr = pci_map_single(dev->pdev,
  2070. scsicmd->request_buffer,
  2071. scsicmd->request_bufflen,
  2072. scsicmd->sc_data_direction);
  2073. psg->count = cpu_to_le32(1);
  2074. psg->sg[0].addr[0] = cpu_to_le32(addr & 0xffffffff);
  2075. psg->sg[0].addr[1] = cpu_to_le32(addr >> 32);
  2076. psg->sg[0].count = cpu_to_le32(scsicmd->request_bufflen);
  2077. scsicmd->SCp.dma_handle = addr;
  2078. byte_count = scsicmd->request_bufflen;
  2079. }
  2080. return byte_count;
  2081. }
  2082. static unsigned long aac_build_sgraw(struct scsi_cmnd* scsicmd, struct sgmapraw* psg)
  2083. {
  2084. struct Scsi_Host *host = scsicmd->device->host;
  2085. struct aac_dev *dev = (struct aac_dev *)host->hostdata;
  2086. unsigned long byte_count = 0;
  2087. // Get rid of old data
  2088. psg->count = 0;
  2089. psg->sg[0].next = 0;
  2090. psg->sg[0].prev = 0;
  2091. psg->sg[0].addr[0] = 0;
  2092. psg->sg[0].addr[1] = 0;
  2093. psg->sg[0].count = 0;
  2094. psg->sg[0].flags = 0;
  2095. if (scsicmd->use_sg) {
  2096. struct scatterlist *sg;
  2097. int i;
  2098. int sg_count;
  2099. sg = (struct scatterlist *) scsicmd->request_buffer;
  2100. sg_count = pci_map_sg(dev->pdev, sg, scsicmd->use_sg,
  2101. scsicmd->sc_data_direction);
  2102. for (i = 0; i < sg_count; i++) {
  2103. int count = sg_dma_len(sg);
  2104. u64 addr = sg_dma_address(sg);
  2105. psg->sg[i].next = 0;
  2106. psg->sg[i].prev = 0;
  2107. psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
  2108. psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
  2109. psg->sg[i].count = cpu_to_le32(count);
  2110. psg->sg[i].flags = 0;
  2111. byte_count += count;
  2112. sg++;
  2113. }
  2114. psg->count = cpu_to_le32(sg_count);
  2115. /* hba wants the size to be exact */
  2116. if(byte_count > scsicmd->request_bufflen){
  2117. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2118. (byte_count - scsicmd->request_bufflen);
  2119. psg->sg[i-1].count = cpu_to_le32(temp);
  2120. byte_count = scsicmd->request_bufflen;
  2121. }
  2122. /* Check for command underflow */
  2123. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2124. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2125. byte_count, scsicmd->underflow);
  2126. }
  2127. }
  2128. else if(scsicmd->request_bufflen) {
  2129. int count;
  2130. u64 addr;
  2131. scsicmd->SCp.dma_handle = pci_map_single(dev->pdev,
  2132. scsicmd->request_buffer,
  2133. scsicmd->request_bufflen,
  2134. scsicmd->sc_data_direction);
  2135. addr = scsicmd->SCp.dma_handle;
  2136. count = scsicmd->request_bufflen;
  2137. psg->count = cpu_to_le32(1);
  2138. psg->sg[0].next = 0;
  2139. psg->sg[0].prev = 0;
  2140. psg->sg[0].addr[1] = cpu_to_le32((u32)(addr>>32));
  2141. psg->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
  2142. psg->sg[0].count = cpu_to_le32(count);
  2143. psg->sg[0].flags = 0;
  2144. byte_count = scsicmd->request_bufflen;
  2145. }
  2146. return byte_count;
  2147. }
  2148. #ifdef AAC_DETAILED_STATUS_INFO
  2149. struct aac_srb_status_info {
  2150. u32 status;
  2151. char *str;
  2152. };
  2153. static struct aac_srb_status_info srb_status_info[] = {
  2154. { SRB_STATUS_PENDING, "Pending Status"},
  2155. { SRB_STATUS_SUCCESS, "Success"},
  2156. { SRB_STATUS_ABORTED, "Aborted Command"},
  2157. { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
  2158. { SRB_STATUS_ERROR, "Error Event"},
  2159. { SRB_STATUS_BUSY, "Device Busy"},
  2160. { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
  2161. { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
  2162. { SRB_STATUS_NO_DEVICE, "No Device"},
  2163. { SRB_STATUS_TIMEOUT, "Timeout"},
  2164. { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
  2165. { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
  2166. { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
  2167. { SRB_STATUS_BUS_RESET, "Bus Reset"},
  2168. { SRB_STATUS_PARITY_ERROR, "Parity Error"},
  2169. { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
  2170. { SRB_STATUS_NO_HBA, "No HBA"},
  2171. { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
  2172. { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
  2173. { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
  2174. { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
  2175. { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
  2176. { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
  2177. { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
  2178. { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
  2179. { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
  2180. { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
  2181. { SRB_STATUS_NOT_STARTED, "Not Started"},
  2182. { SRB_STATUS_NOT_IN_USE, "Not In Use"},
  2183. { SRB_STATUS_FORCE_ABORT, "Force Abort"},
  2184. { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
  2185. { 0xff, "Unknown Error"}
  2186. };
  2187. char *aac_get_status_string(u32 status)
  2188. {
  2189. int i;
  2190. for(i=0; i < (sizeof(srb_status_info)/sizeof(struct aac_srb_status_info)); i++ ){
  2191. if(srb_status_info[i].status == status){
  2192. return srb_status_info[i].str;
  2193. }
  2194. }
  2195. return "Bad Status Code";
  2196. }
  2197. #endif