aachba.c 72 KB

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