aachba.c 73 KB

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