aachba.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc.
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000-2010 Adaptec, Inc.
  9. * 2010 PMC-Sierra, Inc. (aacraid@pmc-sierra.com)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/pci.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/slab.h>
  32. #include <linux/completion.h>
  33. #include <linux/blkdev.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/highmem.h> /* For flush_kernel_dcache_page */
  36. #include <linux/module.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_host.h>
  41. #include "aacraid.h"
  42. /* values for inqd_pdt: Peripheral device type in plain English */
  43. #define INQD_PDT_DA 0x00 /* Direct-access (DISK) device */
  44. #define INQD_PDT_PROC 0x03 /* Processor device */
  45. #define INQD_PDT_CHNGR 0x08 /* Changer (jukebox, scsi2) */
  46. #define INQD_PDT_COMM 0x09 /* Communication device (scsi2) */
  47. #define INQD_PDT_NOLUN2 0x1f /* Unknown Device (scsi2) */
  48. #define INQD_PDT_NOLUN 0x7f /* Logical Unit Not Present */
  49. #define INQD_PDT_DMASK 0x1F /* Peripheral Device Type Mask */
  50. #define INQD_PDT_QMASK 0xE0 /* Peripheral Device Qualifer Mask */
  51. /*
  52. * Sense codes
  53. */
  54. #define SENCODE_NO_SENSE 0x00
  55. #define SENCODE_END_OF_DATA 0x00
  56. #define SENCODE_BECOMING_READY 0x04
  57. #define SENCODE_INIT_CMD_REQUIRED 0x04
  58. #define SENCODE_PARAM_LIST_LENGTH_ERROR 0x1A
  59. #define SENCODE_INVALID_COMMAND 0x20
  60. #define SENCODE_LBA_OUT_OF_RANGE 0x21
  61. #define SENCODE_INVALID_CDB_FIELD 0x24
  62. #define SENCODE_LUN_NOT_SUPPORTED 0x25
  63. #define SENCODE_INVALID_PARAM_FIELD 0x26
  64. #define SENCODE_PARAM_NOT_SUPPORTED 0x26
  65. #define SENCODE_PARAM_VALUE_INVALID 0x26
  66. #define SENCODE_RESET_OCCURRED 0x29
  67. #define SENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x3E
  68. #define SENCODE_INQUIRY_DATA_CHANGED 0x3F
  69. #define SENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x39
  70. #define SENCODE_DIAGNOSTIC_FAILURE 0x40
  71. #define SENCODE_INTERNAL_TARGET_FAILURE 0x44
  72. #define SENCODE_INVALID_MESSAGE_ERROR 0x49
  73. #define SENCODE_LUN_FAILED_SELF_CONFIG 0x4c
  74. #define SENCODE_OVERLAPPED_COMMAND 0x4E
  75. /*
  76. * Additional sense codes
  77. */
  78. #define ASENCODE_NO_SENSE 0x00
  79. #define ASENCODE_END_OF_DATA 0x05
  80. #define ASENCODE_BECOMING_READY 0x01
  81. #define ASENCODE_INIT_CMD_REQUIRED 0x02
  82. #define ASENCODE_PARAM_LIST_LENGTH_ERROR 0x00
  83. #define ASENCODE_INVALID_COMMAND 0x00
  84. #define ASENCODE_LBA_OUT_OF_RANGE 0x00
  85. #define ASENCODE_INVALID_CDB_FIELD 0x00
  86. #define ASENCODE_LUN_NOT_SUPPORTED 0x00
  87. #define ASENCODE_INVALID_PARAM_FIELD 0x00
  88. #define ASENCODE_PARAM_NOT_SUPPORTED 0x01
  89. #define ASENCODE_PARAM_VALUE_INVALID 0x02
  90. #define ASENCODE_RESET_OCCURRED 0x00
  91. #define ASENCODE_LUN_NOT_SELF_CONFIGURED_YET 0x00
  92. #define ASENCODE_INQUIRY_DATA_CHANGED 0x03
  93. #define ASENCODE_SAVING_PARAMS_NOT_SUPPORTED 0x00
  94. #define ASENCODE_DIAGNOSTIC_FAILURE 0x80
  95. #define ASENCODE_INTERNAL_TARGET_FAILURE 0x00
  96. #define ASENCODE_INVALID_MESSAGE_ERROR 0x00
  97. #define ASENCODE_LUN_FAILED_SELF_CONFIG 0x00
  98. #define ASENCODE_OVERLAPPED_COMMAND 0x00
  99. #define BYTE0(x) (unsigned char)(x)
  100. #define BYTE1(x) (unsigned char)((x) >> 8)
  101. #define BYTE2(x) (unsigned char)((x) >> 16)
  102. #define BYTE3(x) (unsigned char)((x) >> 24)
  103. /*------------------------------------------------------------------------------
  104. * S T R U C T S / T Y P E D E F S
  105. *----------------------------------------------------------------------------*/
  106. /* SCSI inquiry data */
  107. struct inquiry_data {
  108. u8 inqd_pdt; /* Peripheral qualifier | Peripheral Device Type */
  109. u8 inqd_dtq; /* RMB | Device Type Qualifier */
  110. u8 inqd_ver; /* ISO version | ECMA version | ANSI-approved version */
  111. u8 inqd_rdf; /* AENC | TrmIOP | Response data format */
  112. u8 inqd_len; /* Additional length (n-4) */
  113. u8 inqd_pad1[2];/* Reserved - must be zero */
  114. u8 inqd_pad2; /* RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
  115. u8 inqd_vid[8]; /* Vendor ID */
  116. u8 inqd_pid[16];/* Product ID */
  117. u8 inqd_prl[4]; /* Product Revision Level */
  118. };
  119. /*
  120. * M O D U L E G L O B A L S
  121. */
  122. static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *sgmap);
  123. static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg);
  124. static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg);
  125. static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
  126. struct aac_raw_io2 *rio2, int sg_max);
  127. static int aac_convert_sgraw2(struct aac_raw_io2 *rio2,
  128. int pages, int nseg, int nseg_new);
  129. static int aac_send_srb_fib(struct scsi_cmnd* scsicmd);
  130. #ifdef AAC_DETAILED_STATUS_INFO
  131. static char *aac_get_status_string(u32 status);
  132. #endif
  133. /*
  134. * Non dasd selection is handled entirely in aachba now
  135. */
  136. static int nondasd = -1;
  137. static int aac_cache = 2; /* WCE=0 to avoid performance problems */
  138. static int dacmode = -1;
  139. int aac_msi;
  140. int aac_commit = -1;
  141. int startup_timeout = 180;
  142. int aif_timeout = 120;
  143. int aac_sync_mode; /* Only Sync. transfer - disabled */
  144. int aac_convert_sgl = 1; /* convert non-conformable s/g list - enabled */
  145. module_param(aac_sync_mode, int, S_IRUGO|S_IWUSR);
  146. MODULE_PARM_DESC(aac_sync_mode, "Force sync. transfer mode"
  147. " 0=off, 1=on");
  148. module_param(aac_convert_sgl, int, S_IRUGO|S_IWUSR);
  149. MODULE_PARM_DESC(aac_convert_sgl, "Convert non-conformable s/g list"
  150. " 0=off, 1=on");
  151. module_param(nondasd, int, S_IRUGO|S_IWUSR);
  152. MODULE_PARM_DESC(nondasd, "Control scanning of hba for nondasd devices."
  153. " 0=off, 1=on");
  154. module_param_named(cache, aac_cache, int, S_IRUGO|S_IWUSR);
  155. MODULE_PARM_DESC(cache, "Disable Queue Flush commands:\n"
  156. "\tbit 0 - Disable FUA in WRITE SCSI commands\n"
  157. "\tbit 1 - Disable SYNCHRONIZE_CACHE SCSI command\n"
  158. "\tbit 2 - Disable only if Battery is protecting Cache");
  159. module_param(dacmode, int, S_IRUGO|S_IWUSR);
  160. MODULE_PARM_DESC(dacmode, "Control whether dma addressing is using 64 bit DAC."
  161. " 0=off, 1=on");
  162. module_param_named(commit, aac_commit, int, S_IRUGO|S_IWUSR);
  163. MODULE_PARM_DESC(commit, "Control whether a COMMIT_CONFIG is issued to the"
  164. " adapter for foreign arrays.\n"
  165. "This is typically needed in systems that do not have a BIOS."
  166. " 0=off, 1=on");
  167. module_param_named(msi, aac_msi, int, S_IRUGO|S_IWUSR);
  168. MODULE_PARM_DESC(msi, "IRQ handling."
  169. " 0=PIC(default), 1=MSI, 2=MSI-X(unsupported, uses MSI)");
  170. module_param(startup_timeout, int, S_IRUGO|S_IWUSR);
  171. MODULE_PARM_DESC(startup_timeout, "The duration of time in seconds to wait for"
  172. " adapter to have it's kernel up and\n"
  173. "running. This is typically adjusted for large systems that do not"
  174. " have a BIOS.");
  175. module_param(aif_timeout, int, S_IRUGO|S_IWUSR);
  176. MODULE_PARM_DESC(aif_timeout, "The duration of time in seconds to wait for"
  177. " applications to pick up AIFs before\n"
  178. "deregistering them. This is typically adjusted for heavily burdened"
  179. " systems.");
  180. int numacb = -1;
  181. module_param(numacb, int, S_IRUGO|S_IWUSR);
  182. MODULE_PARM_DESC(numacb, "Request a limit to the number of adapter control"
  183. " blocks (FIB) allocated. Valid values are 512 and down. Default is"
  184. " to use suggestion from Firmware.");
  185. int acbsize = -1;
  186. module_param(acbsize, int, S_IRUGO|S_IWUSR);
  187. MODULE_PARM_DESC(acbsize, "Request a specific adapter control block (FIB)"
  188. " size. Valid values are 512, 2048, 4096 and 8192. Default is to use"
  189. " suggestion from Firmware.");
  190. int update_interval = 30 * 60;
  191. module_param(update_interval, int, S_IRUGO|S_IWUSR);
  192. MODULE_PARM_DESC(update_interval, "Interval in seconds between time sync"
  193. " updates issued to adapter.");
  194. int check_interval = 24 * 60 * 60;
  195. module_param(check_interval, int, S_IRUGO|S_IWUSR);
  196. MODULE_PARM_DESC(check_interval, "Interval in seconds between adapter health"
  197. " checks.");
  198. int aac_check_reset = 1;
  199. module_param_named(check_reset, aac_check_reset, int, S_IRUGO|S_IWUSR);
  200. MODULE_PARM_DESC(check_reset, "If adapter fails health check, reset the"
  201. " adapter. a value of -1 forces the reset to adapters programmed to"
  202. " ignore it.");
  203. int expose_physicals = -1;
  204. module_param(expose_physicals, int, S_IRUGO|S_IWUSR);
  205. MODULE_PARM_DESC(expose_physicals, "Expose physical components of the arrays."
  206. " -1=protect 0=off, 1=on");
  207. int aac_reset_devices;
  208. module_param_named(reset_devices, aac_reset_devices, int, S_IRUGO|S_IWUSR);
  209. MODULE_PARM_DESC(reset_devices, "Force an adapter reset at initialization.");
  210. int aac_wwn = 1;
  211. module_param_named(wwn, aac_wwn, int, S_IRUGO|S_IWUSR);
  212. MODULE_PARM_DESC(wwn, "Select a WWN type for the arrays:\n"
  213. "\t0 - Disable\n"
  214. "\t1 - Array Meta Data Signature (default)\n"
  215. "\t2 - Adapter Serial Number");
  216. static inline int aac_valid_context(struct scsi_cmnd *scsicmd,
  217. struct fib *fibptr) {
  218. struct scsi_device *device;
  219. if (unlikely(!scsicmd || !scsicmd->scsi_done)) {
  220. dprintk((KERN_WARNING "aac_valid_context: scsi command corrupt\n"));
  221. aac_fib_complete(fibptr);
  222. aac_fib_free(fibptr);
  223. return 0;
  224. }
  225. scsicmd->SCp.phase = AAC_OWNER_MIDLEVEL;
  226. device = scsicmd->device;
  227. if (unlikely(!device || !scsi_device_online(device))) {
  228. dprintk((KERN_WARNING "aac_valid_context: scsi device corrupt\n"));
  229. aac_fib_complete(fibptr);
  230. aac_fib_free(fibptr);
  231. return 0;
  232. }
  233. return 1;
  234. }
  235. /**
  236. * aac_get_config_status - check the adapter configuration
  237. * @common: adapter to query
  238. *
  239. * Query config status, and commit the configuration if needed.
  240. */
  241. int aac_get_config_status(struct aac_dev *dev, int commit_flag)
  242. {
  243. int status = 0;
  244. struct fib * fibptr;
  245. if (!(fibptr = aac_fib_alloc(dev)))
  246. return -ENOMEM;
  247. aac_fib_init(fibptr);
  248. {
  249. struct aac_get_config_status *dinfo;
  250. dinfo = (struct aac_get_config_status *) fib_data(fibptr);
  251. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  252. dinfo->type = cpu_to_le32(CT_GET_CONFIG_STATUS);
  253. dinfo->count = cpu_to_le32(sizeof(((struct aac_get_config_status_resp *)NULL)->data));
  254. }
  255. status = aac_fib_send(ContainerCommand,
  256. fibptr,
  257. sizeof (struct aac_get_config_status),
  258. FsaNormal,
  259. 1, 1,
  260. NULL, NULL);
  261. if (status < 0) {
  262. printk(KERN_WARNING "aac_get_config_status: SendFIB failed.\n");
  263. } else {
  264. struct aac_get_config_status_resp *reply
  265. = (struct aac_get_config_status_resp *) fib_data(fibptr);
  266. dprintk((KERN_WARNING
  267. "aac_get_config_status: response=%d status=%d action=%d\n",
  268. le32_to_cpu(reply->response),
  269. le32_to_cpu(reply->status),
  270. le32_to_cpu(reply->data.action)));
  271. if ((le32_to_cpu(reply->response) != ST_OK) ||
  272. (le32_to_cpu(reply->status) != CT_OK) ||
  273. (le32_to_cpu(reply->data.action) > CFACT_PAUSE)) {
  274. printk(KERN_WARNING "aac_get_config_status: Will not issue the Commit Configuration\n");
  275. status = -EINVAL;
  276. }
  277. }
  278. /* Do not set XferState to zero unless receives a response from F/W */
  279. if (status >= 0)
  280. aac_fib_complete(fibptr);
  281. /* Send a CT_COMMIT_CONFIG to enable discovery of devices */
  282. if (status >= 0) {
  283. if ((aac_commit == 1) || commit_flag) {
  284. struct aac_commit_config * dinfo;
  285. aac_fib_init(fibptr);
  286. dinfo = (struct aac_commit_config *) fib_data(fibptr);
  287. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  288. dinfo->type = cpu_to_le32(CT_COMMIT_CONFIG);
  289. status = aac_fib_send(ContainerCommand,
  290. fibptr,
  291. sizeof (struct aac_commit_config),
  292. FsaNormal,
  293. 1, 1,
  294. NULL, NULL);
  295. /* Do not set XferState to zero unless
  296. * receives a response from F/W */
  297. if (status >= 0)
  298. aac_fib_complete(fibptr);
  299. } else if (aac_commit == 0) {
  300. printk(KERN_WARNING
  301. "aac_get_config_status: Foreign device configurations are being ignored\n");
  302. }
  303. }
  304. /* FIB should be freed only after getting the response from the F/W */
  305. if (status != -ERESTARTSYS)
  306. aac_fib_free(fibptr);
  307. return status;
  308. }
  309. static void aac_expose_phy_device(struct scsi_cmnd *scsicmd)
  310. {
  311. char inq_data;
  312. scsi_sg_copy_to_buffer(scsicmd, &inq_data, sizeof(inq_data));
  313. if ((inq_data & 0x20) && (inq_data & 0x1f) == TYPE_DISK) {
  314. inq_data &= 0xdf;
  315. scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
  316. }
  317. }
  318. /**
  319. * aac_get_containers - list containers
  320. * @common: adapter to probe
  321. *
  322. * Make a list of all containers on this controller
  323. */
  324. int aac_get_containers(struct aac_dev *dev)
  325. {
  326. struct fsa_dev_info *fsa_dev_ptr;
  327. u32 index;
  328. int status = 0;
  329. struct fib * fibptr;
  330. struct aac_get_container_count *dinfo;
  331. struct aac_get_container_count_resp *dresp;
  332. int maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
  333. if (!(fibptr = aac_fib_alloc(dev)))
  334. return -ENOMEM;
  335. aac_fib_init(fibptr);
  336. dinfo = (struct aac_get_container_count *) fib_data(fibptr);
  337. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  338. dinfo->type = cpu_to_le32(CT_GET_CONTAINER_COUNT);
  339. status = aac_fib_send(ContainerCommand,
  340. fibptr,
  341. sizeof (struct aac_get_container_count),
  342. FsaNormal,
  343. 1, 1,
  344. NULL, NULL);
  345. if (status >= 0) {
  346. dresp = (struct aac_get_container_count_resp *)fib_data(fibptr);
  347. maximum_num_containers = le32_to_cpu(dresp->ContainerSwitchEntries);
  348. aac_fib_complete(fibptr);
  349. }
  350. /* FIB should be freed only after getting the response from the F/W */
  351. if (status != -ERESTARTSYS)
  352. aac_fib_free(fibptr);
  353. if (maximum_num_containers < MAXIMUM_NUM_CONTAINERS)
  354. maximum_num_containers = MAXIMUM_NUM_CONTAINERS;
  355. fsa_dev_ptr = kzalloc(sizeof(*fsa_dev_ptr) * maximum_num_containers,
  356. GFP_KERNEL);
  357. if (!fsa_dev_ptr)
  358. return -ENOMEM;
  359. dev->fsa_dev = fsa_dev_ptr;
  360. dev->maximum_num_containers = maximum_num_containers;
  361. for (index = 0; index < dev->maximum_num_containers; ) {
  362. fsa_dev_ptr[index].devname[0] = '\0';
  363. status = aac_probe_container(dev, index);
  364. if (status < 0) {
  365. printk(KERN_WARNING "aac_get_containers: SendFIB failed.\n");
  366. break;
  367. }
  368. /*
  369. * If there are no more containers, then stop asking.
  370. */
  371. if (++index >= status)
  372. break;
  373. }
  374. return status;
  375. }
  376. static void get_container_name_callback(void *context, struct fib * fibptr)
  377. {
  378. struct aac_get_name_resp * get_name_reply;
  379. struct scsi_cmnd * scsicmd;
  380. scsicmd = (struct scsi_cmnd *) context;
  381. if (!aac_valid_context(scsicmd, fibptr))
  382. return;
  383. dprintk((KERN_DEBUG "get_container_name_callback[cpu %d]: t = %ld.\n", smp_processor_id(), jiffies));
  384. BUG_ON(fibptr == NULL);
  385. get_name_reply = (struct aac_get_name_resp *) fib_data(fibptr);
  386. /* Failure is irrelevant, using default value instead */
  387. if ((le32_to_cpu(get_name_reply->status) == CT_OK)
  388. && (get_name_reply->data[0] != '\0')) {
  389. char *sp = get_name_reply->data;
  390. sp[sizeof(((struct aac_get_name_resp *)NULL)->data)-1] = '\0';
  391. while (*sp == ' ')
  392. ++sp;
  393. if (*sp) {
  394. struct inquiry_data inq;
  395. char d[sizeof(((struct inquiry_data *)NULL)->inqd_pid)];
  396. int count = sizeof(d);
  397. char *dp = d;
  398. do {
  399. *dp++ = (*sp) ? *sp++ : ' ';
  400. } while (--count > 0);
  401. scsi_sg_copy_to_buffer(scsicmd, &inq, sizeof(inq));
  402. memcpy(inq.inqd_pid, d, sizeof(d));
  403. scsi_sg_copy_from_buffer(scsicmd, &inq, sizeof(inq));
  404. }
  405. }
  406. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  407. aac_fib_complete(fibptr);
  408. aac_fib_free(fibptr);
  409. scsicmd->scsi_done(scsicmd);
  410. }
  411. /**
  412. * aac_get_container_name - get container name, none blocking.
  413. */
  414. static int aac_get_container_name(struct scsi_cmnd * scsicmd)
  415. {
  416. int status;
  417. struct aac_get_name *dinfo;
  418. struct fib * cmd_fibcontext;
  419. struct aac_dev * dev;
  420. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  421. if (!(cmd_fibcontext = aac_fib_alloc(dev)))
  422. return -ENOMEM;
  423. aac_fib_init(cmd_fibcontext);
  424. dinfo = (struct aac_get_name *) fib_data(cmd_fibcontext);
  425. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  426. dinfo->type = cpu_to_le32(CT_READ_NAME);
  427. dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
  428. dinfo->count = cpu_to_le32(sizeof(((struct aac_get_name_resp *)NULL)->data));
  429. status = aac_fib_send(ContainerCommand,
  430. cmd_fibcontext,
  431. sizeof (struct aac_get_name),
  432. FsaNormal,
  433. 0, 1,
  434. (fib_callback)get_container_name_callback,
  435. (void *) scsicmd);
  436. /*
  437. * Check that the command queued to the controller
  438. */
  439. if (status == -EINPROGRESS) {
  440. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  441. return 0;
  442. }
  443. printk(KERN_WARNING "aac_get_container_name: aac_fib_send failed with status: %d.\n", status);
  444. aac_fib_complete(cmd_fibcontext);
  445. aac_fib_free(cmd_fibcontext);
  446. return -1;
  447. }
  448. static int aac_probe_container_callback2(struct scsi_cmnd * scsicmd)
  449. {
  450. struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
  451. if ((fsa_dev_ptr[scmd_id(scsicmd)].valid & 1))
  452. return aac_scsi_cmd(scsicmd);
  453. scsicmd->result = DID_NO_CONNECT << 16;
  454. scsicmd->scsi_done(scsicmd);
  455. return 0;
  456. }
  457. static void _aac_probe_container2(void * context, struct fib * fibptr)
  458. {
  459. struct fsa_dev_info *fsa_dev_ptr;
  460. int (*callback)(struct scsi_cmnd *);
  461. struct scsi_cmnd * scsicmd = (struct scsi_cmnd *)context;
  462. if (!aac_valid_context(scsicmd, fibptr))
  463. return;
  464. scsicmd->SCp.Status = 0;
  465. fsa_dev_ptr = fibptr->dev->fsa_dev;
  466. if (fsa_dev_ptr) {
  467. struct aac_mount * dresp = (struct aac_mount *) fib_data(fibptr);
  468. fsa_dev_ptr += scmd_id(scsicmd);
  469. if ((le32_to_cpu(dresp->status) == ST_OK) &&
  470. (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE) &&
  471. (le32_to_cpu(dresp->mnt[0].state) != FSCS_HIDDEN)) {
  472. fsa_dev_ptr->valid = 1;
  473. /* sense_key holds the current state of the spin-up */
  474. if (dresp->mnt[0].state & cpu_to_le32(FSCS_NOT_READY))
  475. fsa_dev_ptr->sense_data.sense_key = NOT_READY;
  476. else if (fsa_dev_ptr->sense_data.sense_key == NOT_READY)
  477. fsa_dev_ptr->sense_data.sense_key = NO_SENSE;
  478. fsa_dev_ptr->type = le32_to_cpu(dresp->mnt[0].vol);
  479. fsa_dev_ptr->size
  480. = ((u64)le32_to_cpu(dresp->mnt[0].capacity)) +
  481. (((u64)le32_to_cpu(dresp->mnt[0].capacityhigh)) << 32);
  482. fsa_dev_ptr->ro = ((le32_to_cpu(dresp->mnt[0].state) & FSCS_READONLY) != 0);
  483. }
  484. if ((fsa_dev_ptr->valid & 1) == 0)
  485. fsa_dev_ptr->valid = 0;
  486. scsicmd->SCp.Status = le32_to_cpu(dresp->count);
  487. }
  488. aac_fib_complete(fibptr);
  489. aac_fib_free(fibptr);
  490. callback = (int (*)(struct scsi_cmnd *))(scsicmd->SCp.ptr);
  491. scsicmd->SCp.ptr = NULL;
  492. (*callback)(scsicmd);
  493. return;
  494. }
  495. static void _aac_probe_container1(void * context, struct fib * fibptr)
  496. {
  497. struct scsi_cmnd * scsicmd;
  498. struct aac_mount * dresp;
  499. struct aac_query_mount *dinfo;
  500. int status;
  501. dresp = (struct aac_mount *) fib_data(fibptr);
  502. dresp->mnt[0].capacityhigh = 0;
  503. if ((le32_to_cpu(dresp->status) != ST_OK) ||
  504. (le32_to_cpu(dresp->mnt[0].vol) != CT_NONE)) {
  505. _aac_probe_container2(context, fibptr);
  506. return;
  507. }
  508. scsicmd = (struct scsi_cmnd *) context;
  509. if (!aac_valid_context(scsicmd, fibptr))
  510. return;
  511. aac_fib_init(fibptr);
  512. dinfo = (struct aac_query_mount *)fib_data(fibptr);
  513. dinfo->command = cpu_to_le32(VM_NameServe64);
  514. dinfo->count = cpu_to_le32(scmd_id(scsicmd));
  515. dinfo->type = cpu_to_le32(FT_FILESYS);
  516. status = aac_fib_send(ContainerCommand,
  517. fibptr,
  518. sizeof(struct aac_query_mount),
  519. FsaNormal,
  520. 0, 1,
  521. _aac_probe_container2,
  522. (void *) scsicmd);
  523. /*
  524. * Check that the command queued to the controller
  525. */
  526. if (status == -EINPROGRESS)
  527. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  528. else if (status < 0) {
  529. /* Inherit results from VM_NameServe, if any */
  530. dresp->status = cpu_to_le32(ST_OK);
  531. _aac_probe_container2(context, fibptr);
  532. }
  533. }
  534. static int _aac_probe_container(struct scsi_cmnd * scsicmd, int (*callback)(struct scsi_cmnd *))
  535. {
  536. struct fib * fibptr;
  537. int status = -ENOMEM;
  538. if ((fibptr = aac_fib_alloc((struct aac_dev *)scsicmd->device->host->hostdata))) {
  539. struct aac_query_mount *dinfo;
  540. aac_fib_init(fibptr);
  541. dinfo = (struct aac_query_mount *)fib_data(fibptr);
  542. dinfo->command = cpu_to_le32(VM_NameServe);
  543. dinfo->count = cpu_to_le32(scmd_id(scsicmd));
  544. dinfo->type = cpu_to_le32(FT_FILESYS);
  545. scsicmd->SCp.ptr = (char *)callback;
  546. status = aac_fib_send(ContainerCommand,
  547. fibptr,
  548. sizeof(struct aac_query_mount),
  549. FsaNormal,
  550. 0, 1,
  551. _aac_probe_container1,
  552. (void *) scsicmd);
  553. /*
  554. * Check that the command queued to the controller
  555. */
  556. if (status == -EINPROGRESS) {
  557. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  558. return 0;
  559. }
  560. if (status < 0) {
  561. scsicmd->SCp.ptr = NULL;
  562. aac_fib_complete(fibptr);
  563. aac_fib_free(fibptr);
  564. }
  565. }
  566. if (status < 0) {
  567. struct fsa_dev_info *fsa_dev_ptr = ((struct aac_dev *)(scsicmd->device->host->hostdata))->fsa_dev;
  568. if (fsa_dev_ptr) {
  569. fsa_dev_ptr += scmd_id(scsicmd);
  570. if ((fsa_dev_ptr->valid & 1) == 0) {
  571. fsa_dev_ptr->valid = 0;
  572. return (*callback)(scsicmd);
  573. }
  574. }
  575. }
  576. return status;
  577. }
  578. /**
  579. * aac_probe_container - query a logical volume
  580. * @dev: device to query
  581. * @cid: container identifier
  582. *
  583. * Queries the controller about the given volume. The volume information
  584. * is updated in the struct fsa_dev_info structure rather than returned.
  585. */
  586. static int aac_probe_container_callback1(struct scsi_cmnd * scsicmd)
  587. {
  588. scsicmd->device = NULL;
  589. return 0;
  590. }
  591. int aac_probe_container(struct aac_dev *dev, int cid)
  592. {
  593. struct scsi_cmnd *scsicmd = kmalloc(sizeof(*scsicmd), GFP_KERNEL);
  594. struct scsi_device *scsidev = kmalloc(sizeof(*scsidev), GFP_KERNEL);
  595. int status;
  596. if (!scsicmd || !scsidev) {
  597. kfree(scsicmd);
  598. kfree(scsidev);
  599. return -ENOMEM;
  600. }
  601. scsicmd->list.next = NULL;
  602. scsicmd->scsi_done = (void (*)(struct scsi_cmnd*))aac_probe_container_callback1;
  603. scsicmd->device = scsidev;
  604. scsidev->sdev_state = 0;
  605. scsidev->id = cid;
  606. scsidev->host = dev->scsi_host_ptr;
  607. if (_aac_probe_container(scsicmd, aac_probe_container_callback1) == 0)
  608. while (scsicmd->device == scsidev)
  609. schedule();
  610. kfree(scsidev);
  611. status = scsicmd->SCp.Status;
  612. kfree(scsicmd);
  613. return status;
  614. }
  615. /* Local Structure to set SCSI inquiry data strings */
  616. struct scsi_inq {
  617. char vid[8]; /* Vendor ID */
  618. char pid[16]; /* Product ID */
  619. char prl[4]; /* Product Revision Level */
  620. };
  621. /**
  622. * InqStrCopy - string merge
  623. * @a: string to copy from
  624. * @b: string to copy to
  625. *
  626. * Copy a String from one location to another
  627. * without copying \0
  628. */
  629. static void inqstrcpy(char *a, char *b)
  630. {
  631. while (*a != (char)0)
  632. *b++ = *a++;
  633. }
  634. static char *container_types[] = {
  635. "None",
  636. "Volume",
  637. "Mirror",
  638. "Stripe",
  639. "RAID5",
  640. "SSRW",
  641. "SSRO",
  642. "Morph",
  643. "Legacy",
  644. "RAID4",
  645. "RAID10",
  646. "RAID00",
  647. "V-MIRRORS",
  648. "PSEUDO R4",
  649. "RAID50",
  650. "RAID5D",
  651. "RAID5D0",
  652. "RAID1E",
  653. "RAID6",
  654. "RAID60",
  655. "Unknown"
  656. };
  657. char * get_container_type(unsigned tindex)
  658. {
  659. if (tindex >= ARRAY_SIZE(container_types))
  660. tindex = ARRAY_SIZE(container_types) - 1;
  661. return container_types[tindex];
  662. }
  663. /* Function: setinqstr
  664. *
  665. * Arguments: [1] pointer to void [1] int
  666. *
  667. * Purpose: Sets SCSI inquiry data strings for vendor, product
  668. * and revision level. Allows strings to be set in platform dependent
  669. * files instead of in OS dependent driver source.
  670. */
  671. static void setinqstr(struct aac_dev *dev, void *data, int tindex)
  672. {
  673. struct scsi_inq *str;
  674. str = (struct scsi_inq *)(data); /* cast data to scsi inq block */
  675. memset(str, ' ', sizeof(*str));
  676. if (dev->supplement_adapter_info.AdapterTypeText[0]) {
  677. char * cp = dev->supplement_adapter_info.AdapterTypeText;
  678. int c;
  679. if ((cp[0] == 'A') && (cp[1] == 'O') && (cp[2] == 'C'))
  680. inqstrcpy("SMC", str->vid);
  681. else {
  682. c = sizeof(str->vid);
  683. while (*cp && *cp != ' ' && --c)
  684. ++cp;
  685. c = *cp;
  686. *cp = '\0';
  687. inqstrcpy (dev->supplement_adapter_info.AdapterTypeText,
  688. str->vid);
  689. *cp = c;
  690. while (*cp && *cp != ' ')
  691. ++cp;
  692. }
  693. while (*cp == ' ')
  694. ++cp;
  695. /* last six chars reserved for vol type */
  696. c = 0;
  697. if (strlen(cp) > sizeof(str->pid)) {
  698. c = cp[sizeof(str->pid)];
  699. cp[sizeof(str->pid)] = '\0';
  700. }
  701. inqstrcpy (cp, str->pid);
  702. if (c)
  703. cp[sizeof(str->pid)] = c;
  704. } else {
  705. struct aac_driver_ident *mp = aac_get_driver_ident(dev->cardtype);
  706. inqstrcpy (mp->vname, str->vid);
  707. /* last six chars reserved for vol type */
  708. inqstrcpy (mp->model, str->pid);
  709. }
  710. if (tindex < ARRAY_SIZE(container_types)){
  711. char *findit = str->pid;
  712. for ( ; *findit != ' '; findit++); /* walk till we find a space */
  713. /* RAID is superfluous in the context of a RAID device */
  714. if (memcmp(findit-4, "RAID", 4) == 0)
  715. *(findit -= 4) = ' ';
  716. if (((findit - str->pid) + strlen(container_types[tindex]))
  717. < (sizeof(str->pid) + sizeof(str->prl)))
  718. inqstrcpy (container_types[tindex], findit + 1);
  719. }
  720. inqstrcpy ("V1.0", str->prl);
  721. }
  722. static void get_container_serial_callback(void *context, struct fib * fibptr)
  723. {
  724. struct aac_get_serial_resp * get_serial_reply;
  725. struct scsi_cmnd * scsicmd;
  726. BUG_ON(fibptr == NULL);
  727. scsicmd = (struct scsi_cmnd *) context;
  728. if (!aac_valid_context(scsicmd, fibptr))
  729. return;
  730. get_serial_reply = (struct aac_get_serial_resp *) fib_data(fibptr);
  731. /* Failure is irrelevant, using default value instead */
  732. if (le32_to_cpu(get_serial_reply->status) == CT_OK) {
  733. char sp[13];
  734. /* EVPD bit set */
  735. sp[0] = INQD_PDT_DA;
  736. sp[1] = scsicmd->cmnd[2];
  737. sp[2] = 0;
  738. sp[3] = snprintf(sp+4, sizeof(sp)-4, "%08X",
  739. le32_to_cpu(get_serial_reply->uid));
  740. scsi_sg_copy_from_buffer(scsicmd, sp, sizeof(sp));
  741. }
  742. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  743. aac_fib_complete(fibptr);
  744. aac_fib_free(fibptr);
  745. scsicmd->scsi_done(scsicmd);
  746. }
  747. /**
  748. * aac_get_container_serial - get container serial, none blocking.
  749. */
  750. static int aac_get_container_serial(struct scsi_cmnd * scsicmd)
  751. {
  752. int status;
  753. struct aac_get_serial *dinfo;
  754. struct fib * cmd_fibcontext;
  755. struct aac_dev * dev;
  756. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  757. if (!(cmd_fibcontext = aac_fib_alloc(dev)))
  758. return -ENOMEM;
  759. aac_fib_init(cmd_fibcontext);
  760. dinfo = (struct aac_get_serial *) fib_data(cmd_fibcontext);
  761. dinfo->command = cpu_to_le32(VM_ContainerConfig);
  762. dinfo->type = cpu_to_le32(CT_CID_TO_32BITS_UID);
  763. dinfo->cid = cpu_to_le32(scmd_id(scsicmd));
  764. status = aac_fib_send(ContainerCommand,
  765. cmd_fibcontext,
  766. sizeof (struct aac_get_serial),
  767. FsaNormal,
  768. 0, 1,
  769. (fib_callback) get_container_serial_callback,
  770. (void *) scsicmd);
  771. /*
  772. * Check that the command queued to the controller
  773. */
  774. if (status == -EINPROGRESS) {
  775. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  776. return 0;
  777. }
  778. printk(KERN_WARNING "aac_get_container_serial: aac_fib_send failed with status: %d.\n", status);
  779. aac_fib_complete(cmd_fibcontext);
  780. aac_fib_free(cmd_fibcontext);
  781. return -1;
  782. }
  783. /* Function: setinqserial
  784. *
  785. * Arguments: [1] pointer to void [1] int
  786. *
  787. * Purpose: Sets SCSI Unit Serial number.
  788. * This is a fake. We should read a proper
  789. * serial number from the container. <SuSE>But
  790. * without docs it's quite hard to do it :-)
  791. * So this will have to do in the meantime.</SuSE>
  792. */
  793. static int setinqserial(struct aac_dev *dev, void *data, int cid)
  794. {
  795. /*
  796. * This breaks array migration.
  797. */
  798. return snprintf((char *)(data), sizeof(struct scsi_inq) - 4, "%08X%02X",
  799. le32_to_cpu(dev->adapter_info.serial[0]), cid);
  800. }
  801. static inline void set_sense(struct sense_data *sense_data, u8 sense_key,
  802. u8 sense_code, u8 a_sense_code, u8 bit_pointer, u16 field_pointer)
  803. {
  804. u8 *sense_buf = (u8 *)sense_data;
  805. /* Sense data valid, err code 70h */
  806. sense_buf[0] = 0x70; /* No info field */
  807. sense_buf[1] = 0; /* Segment number, always zero */
  808. sense_buf[2] = sense_key; /* Sense key */
  809. sense_buf[12] = sense_code; /* Additional sense code */
  810. sense_buf[13] = a_sense_code; /* Additional sense code qualifier */
  811. if (sense_key == ILLEGAL_REQUEST) {
  812. sense_buf[7] = 10; /* Additional sense length */
  813. sense_buf[15] = bit_pointer;
  814. /* Illegal parameter is in the parameter block */
  815. if (sense_code == SENCODE_INVALID_CDB_FIELD)
  816. sense_buf[15] |= 0xc0;/* Std sense key specific field */
  817. /* Illegal parameter is in the CDB block */
  818. sense_buf[16] = field_pointer >> 8; /* MSB */
  819. sense_buf[17] = field_pointer; /* LSB */
  820. } else
  821. sense_buf[7] = 6; /* Additional sense length */
  822. }
  823. static int aac_bounds_32(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
  824. {
  825. if (lba & 0xffffffff00000000LL) {
  826. int cid = scmd_id(cmd);
  827. dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
  828. cmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  829. SAM_STAT_CHECK_CONDITION;
  830. set_sense(&dev->fsa_dev[cid].sense_data,
  831. HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
  832. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
  833. memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  834. min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
  835. SCSI_SENSE_BUFFERSIZE));
  836. cmd->scsi_done(cmd);
  837. return 1;
  838. }
  839. return 0;
  840. }
  841. static int aac_bounds_64(struct aac_dev * dev, struct scsi_cmnd * cmd, u64 lba)
  842. {
  843. return 0;
  844. }
  845. static void io_callback(void *context, struct fib * fibptr);
  846. static int aac_read_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
  847. {
  848. struct aac_dev *dev = fib->dev;
  849. u16 fibsize, command;
  850. long ret;
  851. aac_fib_init(fib);
  852. if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
  853. struct aac_raw_io2 *readcmd2;
  854. readcmd2 = (struct aac_raw_io2 *) fib_data(fib);
  855. memset(readcmd2, 0, sizeof(struct aac_raw_io2));
  856. readcmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
  857. readcmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  858. readcmd2->byteCount = cpu_to_le32(count<<9);
  859. readcmd2->cid = cpu_to_le16(scmd_id(cmd));
  860. readcmd2->flags = cpu_to_le16(RIO2_IO_TYPE_READ);
  861. ret = aac_build_sgraw2(cmd, readcmd2,
  862. dev->scsi_host_ptr->sg_tablesize);
  863. if (ret < 0)
  864. return ret;
  865. command = ContainerRawIo2;
  866. fibsize = sizeof(struct aac_raw_io2) +
  867. ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
  868. } else {
  869. struct aac_raw_io *readcmd;
  870. readcmd = (struct aac_raw_io *) fib_data(fib);
  871. readcmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
  872. readcmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  873. readcmd->count = cpu_to_le32(count<<9);
  874. readcmd->cid = cpu_to_le16(scmd_id(cmd));
  875. readcmd->flags = cpu_to_le16(RIO_TYPE_READ);
  876. readcmd->bpTotal = 0;
  877. readcmd->bpComplete = 0;
  878. ret = aac_build_sgraw(cmd, &readcmd->sg);
  879. if (ret < 0)
  880. return ret;
  881. command = ContainerRawIo;
  882. fibsize = sizeof(struct aac_raw_io) +
  883. ((le32_to_cpu(readcmd->sg.count)-1) * sizeof(struct sgentryraw));
  884. }
  885. BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
  886. /*
  887. * Now send the Fib to the adapter
  888. */
  889. return aac_fib_send(command,
  890. fib,
  891. fibsize,
  892. FsaNormal,
  893. 0, 1,
  894. (fib_callback) io_callback,
  895. (void *) cmd);
  896. }
  897. static int aac_read_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
  898. {
  899. u16 fibsize;
  900. struct aac_read64 *readcmd;
  901. long ret;
  902. aac_fib_init(fib);
  903. readcmd = (struct aac_read64 *) fib_data(fib);
  904. readcmd->command = cpu_to_le32(VM_CtHostRead64);
  905. readcmd->cid = cpu_to_le16(scmd_id(cmd));
  906. readcmd->sector_count = cpu_to_le16(count);
  907. readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  908. readcmd->pad = 0;
  909. readcmd->flags = 0;
  910. ret = aac_build_sg64(cmd, &readcmd->sg);
  911. if (ret < 0)
  912. return ret;
  913. fibsize = sizeof(struct aac_read64) +
  914. ((le32_to_cpu(readcmd->sg.count) - 1) *
  915. sizeof (struct sgentry64));
  916. BUG_ON (fibsize > (fib->dev->max_fib_size -
  917. sizeof(struct aac_fibhdr)));
  918. /*
  919. * Now send the Fib to the adapter
  920. */
  921. return aac_fib_send(ContainerCommand64,
  922. fib,
  923. fibsize,
  924. FsaNormal,
  925. 0, 1,
  926. (fib_callback) io_callback,
  927. (void *) cmd);
  928. }
  929. static int aac_read_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count)
  930. {
  931. u16 fibsize;
  932. struct aac_read *readcmd;
  933. long ret;
  934. aac_fib_init(fib);
  935. readcmd = (struct aac_read *) fib_data(fib);
  936. readcmd->command = cpu_to_le32(VM_CtBlockRead);
  937. readcmd->cid = cpu_to_le32(scmd_id(cmd));
  938. readcmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  939. readcmd->count = cpu_to_le32(count * 512);
  940. ret = aac_build_sg(cmd, &readcmd->sg);
  941. if (ret < 0)
  942. return ret;
  943. fibsize = sizeof(struct aac_read) +
  944. ((le32_to_cpu(readcmd->sg.count) - 1) *
  945. sizeof (struct sgentry));
  946. BUG_ON (fibsize > (fib->dev->max_fib_size -
  947. sizeof(struct aac_fibhdr)));
  948. /*
  949. * Now send the Fib to the adapter
  950. */
  951. return aac_fib_send(ContainerCommand,
  952. fib,
  953. fibsize,
  954. FsaNormal,
  955. 0, 1,
  956. (fib_callback) io_callback,
  957. (void *) cmd);
  958. }
  959. static int aac_write_raw_io(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
  960. {
  961. struct aac_dev *dev = fib->dev;
  962. u16 fibsize, command;
  963. long ret;
  964. aac_fib_init(fib);
  965. if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 && !dev->sync_mode) {
  966. struct aac_raw_io2 *writecmd2;
  967. writecmd2 = (struct aac_raw_io2 *) fib_data(fib);
  968. memset(writecmd2, 0, sizeof(struct aac_raw_io2));
  969. writecmd2->blockLow = cpu_to_le32((u32)(lba&0xffffffff));
  970. writecmd2->blockHigh = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  971. writecmd2->byteCount = cpu_to_le32(count<<9);
  972. writecmd2->cid = cpu_to_le16(scmd_id(cmd));
  973. writecmd2->flags = (fua && ((aac_cache & 5) != 1) &&
  974. (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
  975. cpu_to_le16(RIO2_IO_TYPE_WRITE|RIO2_IO_SUREWRITE) :
  976. cpu_to_le16(RIO2_IO_TYPE_WRITE);
  977. ret = aac_build_sgraw2(cmd, writecmd2,
  978. dev->scsi_host_ptr->sg_tablesize);
  979. if (ret < 0)
  980. return ret;
  981. command = ContainerRawIo2;
  982. fibsize = sizeof(struct aac_raw_io2) +
  983. ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212));
  984. } else {
  985. struct aac_raw_io *writecmd;
  986. writecmd = (struct aac_raw_io *) fib_data(fib);
  987. writecmd->block[0] = cpu_to_le32((u32)(lba&0xffffffff));
  988. writecmd->block[1] = cpu_to_le32((u32)((lba&0xffffffff00000000LL)>>32));
  989. writecmd->count = cpu_to_le32(count<<9);
  990. writecmd->cid = cpu_to_le16(scmd_id(cmd));
  991. writecmd->flags = (fua && ((aac_cache & 5) != 1) &&
  992. (((aac_cache & 5) != 5) || !fib->dev->cache_protected)) ?
  993. cpu_to_le16(RIO_TYPE_WRITE|RIO_SUREWRITE) :
  994. cpu_to_le16(RIO_TYPE_WRITE);
  995. writecmd->bpTotal = 0;
  996. writecmd->bpComplete = 0;
  997. ret = aac_build_sgraw(cmd, &writecmd->sg);
  998. if (ret < 0)
  999. return ret;
  1000. command = ContainerRawIo;
  1001. fibsize = sizeof(struct aac_raw_io) +
  1002. ((le32_to_cpu(writecmd->sg.count)-1) * sizeof (struct sgentryraw));
  1003. }
  1004. BUG_ON(fibsize > (fib->dev->max_fib_size - sizeof(struct aac_fibhdr)));
  1005. /*
  1006. * Now send the Fib to the adapter
  1007. */
  1008. return aac_fib_send(command,
  1009. fib,
  1010. fibsize,
  1011. FsaNormal,
  1012. 0, 1,
  1013. (fib_callback) io_callback,
  1014. (void *) cmd);
  1015. }
  1016. static int aac_write_block64(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
  1017. {
  1018. u16 fibsize;
  1019. struct aac_write64 *writecmd;
  1020. long ret;
  1021. aac_fib_init(fib);
  1022. writecmd = (struct aac_write64 *) fib_data(fib);
  1023. writecmd->command = cpu_to_le32(VM_CtHostWrite64);
  1024. writecmd->cid = cpu_to_le16(scmd_id(cmd));
  1025. writecmd->sector_count = cpu_to_le16(count);
  1026. writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  1027. writecmd->pad = 0;
  1028. writecmd->flags = 0;
  1029. ret = aac_build_sg64(cmd, &writecmd->sg);
  1030. if (ret < 0)
  1031. return ret;
  1032. fibsize = sizeof(struct aac_write64) +
  1033. ((le32_to_cpu(writecmd->sg.count) - 1) *
  1034. sizeof (struct sgentry64));
  1035. BUG_ON (fibsize > (fib->dev->max_fib_size -
  1036. sizeof(struct aac_fibhdr)));
  1037. /*
  1038. * Now send the Fib to the adapter
  1039. */
  1040. return aac_fib_send(ContainerCommand64,
  1041. fib,
  1042. fibsize,
  1043. FsaNormal,
  1044. 0, 1,
  1045. (fib_callback) io_callback,
  1046. (void *) cmd);
  1047. }
  1048. static int aac_write_block(struct fib * fib, struct scsi_cmnd * cmd, u64 lba, u32 count, int fua)
  1049. {
  1050. u16 fibsize;
  1051. struct aac_write *writecmd;
  1052. long ret;
  1053. aac_fib_init(fib);
  1054. writecmd = (struct aac_write *) fib_data(fib);
  1055. writecmd->command = cpu_to_le32(VM_CtBlockWrite);
  1056. writecmd->cid = cpu_to_le32(scmd_id(cmd));
  1057. writecmd->block = cpu_to_le32((u32)(lba&0xffffffff));
  1058. writecmd->count = cpu_to_le32(count * 512);
  1059. writecmd->sg.count = cpu_to_le32(1);
  1060. /* ->stable is not used - it did mean which type of write */
  1061. ret = aac_build_sg(cmd, &writecmd->sg);
  1062. if (ret < 0)
  1063. return ret;
  1064. fibsize = sizeof(struct aac_write) +
  1065. ((le32_to_cpu(writecmd->sg.count) - 1) *
  1066. sizeof (struct sgentry));
  1067. BUG_ON (fibsize > (fib->dev->max_fib_size -
  1068. sizeof(struct aac_fibhdr)));
  1069. /*
  1070. * Now send the Fib to the adapter
  1071. */
  1072. return aac_fib_send(ContainerCommand,
  1073. fib,
  1074. fibsize,
  1075. FsaNormal,
  1076. 0, 1,
  1077. (fib_callback) io_callback,
  1078. (void *) cmd);
  1079. }
  1080. static struct aac_srb * aac_scsi_common(struct fib * fib, struct scsi_cmnd * cmd)
  1081. {
  1082. struct aac_srb * srbcmd;
  1083. u32 flag;
  1084. u32 timeout;
  1085. aac_fib_init(fib);
  1086. switch(cmd->sc_data_direction){
  1087. case DMA_TO_DEVICE:
  1088. flag = SRB_DataOut;
  1089. break;
  1090. case DMA_BIDIRECTIONAL:
  1091. flag = SRB_DataIn | SRB_DataOut;
  1092. break;
  1093. case DMA_FROM_DEVICE:
  1094. flag = SRB_DataIn;
  1095. break;
  1096. case DMA_NONE:
  1097. default: /* shuts up some versions of gcc */
  1098. flag = SRB_NoDataXfer;
  1099. break;
  1100. }
  1101. srbcmd = (struct aac_srb*) fib_data(fib);
  1102. srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi);
  1103. srbcmd->channel = cpu_to_le32(aac_logical_to_phys(scmd_channel(cmd)));
  1104. srbcmd->id = cpu_to_le32(scmd_id(cmd));
  1105. srbcmd->lun = cpu_to_le32(cmd->device->lun);
  1106. srbcmd->flags = cpu_to_le32(flag);
  1107. timeout = cmd->request->timeout/HZ;
  1108. if (timeout == 0)
  1109. timeout = 1;
  1110. srbcmd->timeout = cpu_to_le32(timeout); // timeout in seconds
  1111. srbcmd->retry_limit = 0; /* Obsolete parameter */
  1112. srbcmd->cdb_size = cpu_to_le32(cmd->cmd_len);
  1113. return srbcmd;
  1114. }
  1115. static void aac_srb_callback(void *context, struct fib * fibptr);
  1116. static int aac_scsi_64(struct fib * fib, struct scsi_cmnd * cmd)
  1117. {
  1118. u16 fibsize;
  1119. struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
  1120. long ret;
  1121. ret = aac_build_sg64(cmd, (struct sgmap64 *) &srbcmd->sg);
  1122. if (ret < 0)
  1123. return ret;
  1124. srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
  1125. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1126. memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
  1127. /*
  1128. * Build Scatter/Gather list
  1129. */
  1130. fibsize = sizeof (struct aac_srb) - sizeof (struct sgentry) +
  1131. ((le32_to_cpu(srbcmd->sg.count) & 0xff) *
  1132. sizeof (struct sgentry64));
  1133. BUG_ON (fibsize > (fib->dev->max_fib_size -
  1134. sizeof(struct aac_fibhdr)));
  1135. /*
  1136. * Now send the Fib to the adapter
  1137. */
  1138. return aac_fib_send(ScsiPortCommand64, fib,
  1139. fibsize, FsaNormal, 0, 1,
  1140. (fib_callback) aac_srb_callback,
  1141. (void *) cmd);
  1142. }
  1143. static int aac_scsi_32(struct fib * fib, struct scsi_cmnd * cmd)
  1144. {
  1145. u16 fibsize;
  1146. struct aac_srb * srbcmd = aac_scsi_common(fib, cmd);
  1147. long ret;
  1148. ret = aac_build_sg(cmd, (struct sgmap *)&srbcmd->sg);
  1149. if (ret < 0)
  1150. return ret;
  1151. srbcmd->count = cpu_to_le32(scsi_bufflen(cmd));
  1152. memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb));
  1153. memcpy(srbcmd->cdb, cmd->cmnd, cmd->cmd_len);
  1154. /*
  1155. * Build Scatter/Gather list
  1156. */
  1157. fibsize = sizeof (struct aac_srb) +
  1158. (((le32_to_cpu(srbcmd->sg.count) & 0xff) - 1) *
  1159. sizeof (struct sgentry));
  1160. BUG_ON (fibsize > (fib->dev->max_fib_size -
  1161. sizeof(struct aac_fibhdr)));
  1162. /*
  1163. * Now send the Fib to the adapter
  1164. */
  1165. return aac_fib_send(ScsiPortCommand, fib, fibsize, FsaNormal, 0, 1,
  1166. (fib_callback) aac_srb_callback, (void *) cmd);
  1167. }
  1168. static int aac_scsi_32_64(struct fib * fib, struct scsi_cmnd * cmd)
  1169. {
  1170. if ((sizeof(dma_addr_t) > 4) && fib->dev->needs_dac &&
  1171. (fib->dev->adapter_info.options & AAC_OPT_SGMAP_HOST64))
  1172. return FAILED;
  1173. return aac_scsi_32(fib, cmd);
  1174. }
  1175. int aac_get_adapter_info(struct aac_dev* dev)
  1176. {
  1177. struct fib* fibptr;
  1178. int rcode;
  1179. u32 tmp;
  1180. struct aac_adapter_info *info;
  1181. struct aac_bus_info *command;
  1182. struct aac_bus_info_response *bus_info;
  1183. if (!(fibptr = aac_fib_alloc(dev)))
  1184. return -ENOMEM;
  1185. aac_fib_init(fibptr);
  1186. info = (struct aac_adapter_info *) fib_data(fibptr);
  1187. memset(info,0,sizeof(*info));
  1188. rcode = aac_fib_send(RequestAdapterInfo,
  1189. fibptr,
  1190. sizeof(*info),
  1191. FsaNormal,
  1192. -1, 1, /* First `interrupt' command uses special wait */
  1193. NULL,
  1194. NULL);
  1195. if (rcode < 0) {
  1196. /* FIB should be freed only after
  1197. * getting the response from the F/W */
  1198. if (rcode != -ERESTARTSYS) {
  1199. aac_fib_complete(fibptr);
  1200. aac_fib_free(fibptr);
  1201. }
  1202. return rcode;
  1203. }
  1204. memcpy(&dev->adapter_info, info, sizeof(*info));
  1205. if (dev->adapter_info.options & AAC_OPT_SUPPLEMENT_ADAPTER_INFO) {
  1206. struct aac_supplement_adapter_info * sinfo;
  1207. aac_fib_init(fibptr);
  1208. sinfo = (struct aac_supplement_adapter_info *) fib_data(fibptr);
  1209. memset(sinfo,0,sizeof(*sinfo));
  1210. rcode = aac_fib_send(RequestSupplementAdapterInfo,
  1211. fibptr,
  1212. sizeof(*sinfo),
  1213. FsaNormal,
  1214. 1, 1,
  1215. NULL,
  1216. NULL);
  1217. if (rcode >= 0)
  1218. memcpy(&dev->supplement_adapter_info, sinfo, sizeof(*sinfo));
  1219. if (rcode == -ERESTARTSYS) {
  1220. fibptr = aac_fib_alloc(dev);
  1221. if (!fibptr)
  1222. return -ENOMEM;
  1223. }
  1224. }
  1225. /*
  1226. * GetBusInfo
  1227. */
  1228. aac_fib_init(fibptr);
  1229. bus_info = (struct aac_bus_info_response *) fib_data(fibptr);
  1230. memset(bus_info, 0, sizeof(*bus_info));
  1231. command = (struct aac_bus_info *)bus_info;
  1232. command->Command = cpu_to_le32(VM_Ioctl);
  1233. command->ObjType = cpu_to_le32(FT_DRIVE);
  1234. command->MethodId = cpu_to_le32(1);
  1235. command->CtlCmd = cpu_to_le32(GetBusInfo);
  1236. rcode = aac_fib_send(ContainerCommand,
  1237. fibptr,
  1238. sizeof (*bus_info),
  1239. FsaNormal,
  1240. 1, 1,
  1241. NULL, NULL);
  1242. /* reasoned default */
  1243. dev->maximum_num_physicals = 16;
  1244. if (rcode >= 0 && le32_to_cpu(bus_info->Status) == ST_OK) {
  1245. dev->maximum_num_physicals = le32_to_cpu(bus_info->TargetsPerBus);
  1246. dev->maximum_num_channels = le32_to_cpu(bus_info->BusCount);
  1247. }
  1248. if (!dev->in_reset) {
  1249. char buffer[16];
  1250. tmp = le32_to_cpu(dev->adapter_info.kernelrev);
  1251. printk(KERN_INFO "%s%d: kernel %d.%d-%d[%d] %.*s\n",
  1252. dev->name,
  1253. dev->id,
  1254. tmp>>24,
  1255. (tmp>>16)&0xff,
  1256. tmp&0xff,
  1257. le32_to_cpu(dev->adapter_info.kernelbuild),
  1258. (int)sizeof(dev->supplement_adapter_info.BuildDate),
  1259. dev->supplement_adapter_info.BuildDate);
  1260. tmp = le32_to_cpu(dev->adapter_info.monitorrev);
  1261. printk(KERN_INFO "%s%d: monitor %d.%d-%d[%d]\n",
  1262. dev->name, dev->id,
  1263. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  1264. le32_to_cpu(dev->adapter_info.monitorbuild));
  1265. tmp = le32_to_cpu(dev->adapter_info.biosrev);
  1266. printk(KERN_INFO "%s%d: bios %d.%d-%d[%d]\n",
  1267. dev->name, dev->id,
  1268. tmp>>24,(tmp>>16)&0xff,tmp&0xff,
  1269. le32_to_cpu(dev->adapter_info.biosbuild));
  1270. buffer[0] = '\0';
  1271. if (aac_get_serial_number(
  1272. shost_to_class(dev->scsi_host_ptr), buffer))
  1273. printk(KERN_INFO "%s%d: serial %s",
  1274. dev->name, dev->id, buffer);
  1275. if (dev->supplement_adapter_info.VpdInfo.Tsid[0]) {
  1276. printk(KERN_INFO "%s%d: TSID %.*s\n",
  1277. dev->name, dev->id,
  1278. (int)sizeof(dev->supplement_adapter_info.VpdInfo.Tsid),
  1279. dev->supplement_adapter_info.VpdInfo.Tsid);
  1280. }
  1281. if (!aac_check_reset || ((aac_check_reset == 1) &&
  1282. (dev->supplement_adapter_info.SupportedOptions2 &
  1283. AAC_OPTION_IGNORE_RESET))) {
  1284. printk(KERN_INFO "%s%d: Reset Adapter Ignored\n",
  1285. dev->name, dev->id);
  1286. }
  1287. }
  1288. dev->cache_protected = 0;
  1289. dev->jbod = ((dev->supplement_adapter_info.FeatureBits &
  1290. AAC_FEATURE_JBOD) != 0);
  1291. dev->nondasd_support = 0;
  1292. dev->raid_scsi_mode = 0;
  1293. if(dev->adapter_info.options & AAC_OPT_NONDASD)
  1294. dev->nondasd_support = 1;
  1295. /*
  1296. * If the firmware supports ROMB RAID/SCSI mode and we are currently
  1297. * in RAID/SCSI mode, set the flag. For now if in this mode we will
  1298. * force nondasd support on. If we decide to allow the non-dasd flag
  1299. * additional changes changes will have to be made to support
  1300. * RAID/SCSI. the function aac_scsi_cmd in this module will have to be
  1301. * changed to support the new dev->raid_scsi_mode flag instead of
  1302. * leaching off of the dev->nondasd_support flag. Also in linit.c the
  1303. * function aac_detect will have to be modified where it sets up the
  1304. * max number of channels based on the aac->nondasd_support flag only.
  1305. */
  1306. if ((dev->adapter_info.options & AAC_OPT_SCSI_MANAGED) &&
  1307. (dev->adapter_info.options & AAC_OPT_RAID_SCSI_MODE)) {
  1308. dev->nondasd_support = 1;
  1309. dev->raid_scsi_mode = 1;
  1310. }
  1311. if (dev->raid_scsi_mode != 0)
  1312. printk(KERN_INFO "%s%d: ROMB RAID/SCSI mode enabled\n",
  1313. dev->name, dev->id);
  1314. if (nondasd != -1)
  1315. dev->nondasd_support = (nondasd!=0);
  1316. if (dev->nondasd_support && !dev->in_reset)
  1317. printk(KERN_INFO "%s%d: Non-DASD support enabled.\n",dev->name, dev->id);
  1318. if (dma_get_required_mask(&dev->pdev->dev) > DMA_BIT_MASK(32))
  1319. dev->needs_dac = 1;
  1320. dev->dac_support = 0;
  1321. if ((sizeof(dma_addr_t) > 4) && dev->needs_dac &&
  1322. (dev->adapter_info.options & AAC_OPT_SGMAP_HOST64)) {
  1323. if (!dev->in_reset)
  1324. printk(KERN_INFO "%s%d: 64bit support enabled.\n",
  1325. dev->name, dev->id);
  1326. dev->dac_support = 1;
  1327. }
  1328. if(dacmode != -1) {
  1329. dev->dac_support = (dacmode!=0);
  1330. }
  1331. /* avoid problems with AAC_QUIRK_SCSI_32 controllers */
  1332. if (dev->dac_support && (aac_get_driver_ident(dev->cardtype)->quirks
  1333. & AAC_QUIRK_SCSI_32)) {
  1334. dev->nondasd_support = 0;
  1335. dev->jbod = 0;
  1336. expose_physicals = 0;
  1337. }
  1338. if(dev->dac_support != 0) {
  1339. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(64)) &&
  1340. !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(64))) {
  1341. if (!dev->in_reset)
  1342. printk(KERN_INFO"%s%d: 64 Bit DAC enabled\n",
  1343. dev->name, dev->id);
  1344. } else if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(32)) &&
  1345. !pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32))) {
  1346. printk(KERN_INFO"%s%d: DMA mask set failed, 64 Bit DAC disabled\n",
  1347. dev->name, dev->id);
  1348. dev->dac_support = 0;
  1349. } else {
  1350. printk(KERN_WARNING"%s%d: No suitable DMA available.\n",
  1351. dev->name, dev->id);
  1352. rcode = -ENOMEM;
  1353. }
  1354. }
  1355. /*
  1356. * Deal with configuring for the individualized limits of each packet
  1357. * interface.
  1358. */
  1359. dev->a_ops.adapter_scsi = (dev->dac_support)
  1360. ? ((aac_get_driver_ident(dev->cardtype)->quirks & AAC_QUIRK_SCSI_32)
  1361. ? aac_scsi_32_64
  1362. : aac_scsi_64)
  1363. : aac_scsi_32;
  1364. if (dev->raw_io_interface) {
  1365. dev->a_ops.adapter_bounds = (dev->raw_io_64)
  1366. ? aac_bounds_64
  1367. : aac_bounds_32;
  1368. dev->a_ops.adapter_read = aac_read_raw_io;
  1369. dev->a_ops.adapter_write = aac_write_raw_io;
  1370. } else {
  1371. dev->a_ops.adapter_bounds = aac_bounds_32;
  1372. dev->scsi_host_ptr->sg_tablesize = (dev->max_fib_size -
  1373. sizeof(struct aac_fibhdr) -
  1374. sizeof(struct aac_write) + sizeof(struct sgentry)) /
  1375. sizeof(struct sgentry);
  1376. if (dev->dac_support) {
  1377. dev->a_ops.adapter_read = aac_read_block64;
  1378. dev->a_ops.adapter_write = aac_write_block64;
  1379. /*
  1380. * 38 scatter gather elements
  1381. */
  1382. dev->scsi_host_ptr->sg_tablesize =
  1383. (dev->max_fib_size -
  1384. sizeof(struct aac_fibhdr) -
  1385. sizeof(struct aac_write64) +
  1386. sizeof(struct sgentry64)) /
  1387. sizeof(struct sgentry64);
  1388. } else {
  1389. dev->a_ops.adapter_read = aac_read_block;
  1390. dev->a_ops.adapter_write = aac_write_block;
  1391. }
  1392. dev->scsi_host_ptr->max_sectors = AAC_MAX_32BIT_SGBCOUNT;
  1393. if (!(dev->adapter_info.options & AAC_OPT_NEW_COMM)) {
  1394. /*
  1395. * Worst case size that could cause sg overflow when
  1396. * we break up SG elements that are larger than 64KB.
  1397. * Would be nice if we could tell the SCSI layer what
  1398. * the maximum SG element size can be. Worst case is
  1399. * (sg_tablesize-1) 4KB elements with one 64KB
  1400. * element.
  1401. * 32bit -> 468 or 238KB 64bit -> 424 or 212KB
  1402. */
  1403. dev->scsi_host_ptr->max_sectors =
  1404. (dev->scsi_host_ptr->sg_tablesize * 8) + 112;
  1405. }
  1406. }
  1407. /* FIB should be freed only after getting the response from the F/W */
  1408. if (rcode != -ERESTARTSYS) {
  1409. aac_fib_complete(fibptr);
  1410. aac_fib_free(fibptr);
  1411. }
  1412. return rcode;
  1413. }
  1414. static void io_callback(void *context, struct fib * fibptr)
  1415. {
  1416. struct aac_dev *dev;
  1417. struct aac_read_reply *readreply;
  1418. struct scsi_cmnd *scsicmd;
  1419. u32 cid;
  1420. scsicmd = (struct scsi_cmnd *) context;
  1421. if (!aac_valid_context(scsicmd, fibptr))
  1422. return;
  1423. dev = fibptr->dev;
  1424. cid = scmd_id(scsicmd);
  1425. if (nblank(dprintk(x))) {
  1426. u64 lba;
  1427. switch (scsicmd->cmnd[0]) {
  1428. case WRITE_6:
  1429. case READ_6:
  1430. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
  1431. (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  1432. break;
  1433. case WRITE_16:
  1434. case READ_16:
  1435. lba = ((u64)scsicmd->cmnd[2] << 56) |
  1436. ((u64)scsicmd->cmnd[3] << 48) |
  1437. ((u64)scsicmd->cmnd[4] << 40) |
  1438. ((u64)scsicmd->cmnd[5] << 32) |
  1439. ((u64)scsicmd->cmnd[6] << 24) |
  1440. (scsicmd->cmnd[7] << 16) |
  1441. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1442. break;
  1443. case WRITE_12:
  1444. case READ_12:
  1445. lba = ((u64)scsicmd->cmnd[2] << 24) |
  1446. (scsicmd->cmnd[3] << 16) |
  1447. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1448. break;
  1449. default:
  1450. lba = ((u64)scsicmd->cmnd[2] << 24) |
  1451. (scsicmd->cmnd[3] << 16) |
  1452. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1453. break;
  1454. }
  1455. printk(KERN_DEBUG
  1456. "io_callback[cpu %d]: lba = %llu, t = %ld.\n",
  1457. smp_processor_id(), (unsigned long long)lba, jiffies);
  1458. }
  1459. BUG_ON(fibptr == NULL);
  1460. scsi_dma_unmap(scsicmd);
  1461. readreply = (struct aac_read_reply *)fib_data(fibptr);
  1462. switch (le32_to_cpu(readreply->status)) {
  1463. case ST_OK:
  1464. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  1465. SAM_STAT_GOOD;
  1466. dev->fsa_dev[cid].sense_data.sense_key = NO_SENSE;
  1467. break;
  1468. case ST_NOT_READY:
  1469. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  1470. SAM_STAT_CHECK_CONDITION;
  1471. set_sense(&dev->fsa_dev[cid].sense_data, NOT_READY,
  1472. SENCODE_BECOMING_READY, ASENCODE_BECOMING_READY, 0, 0);
  1473. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1474. min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
  1475. SCSI_SENSE_BUFFERSIZE));
  1476. break;
  1477. default:
  1478. #ifdef AAC_DETAILED_STATUS_INFO
  1479. printk(KERN_WARNING "io_callback: io failed, status = %d\n",
  1480. le32_to_cpu(readreply->status));
  1481. #endif
  1482. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  1483. SAM_STAT_CHECK_CONDITION;
  1484. set_sense(&dev->fsa_dev[cid].sense_data,
  1485. HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
  1486. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
  1487. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1488. min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
  1489. SCSI_SENSE_BUFFERSIZE));
  1490. break;
  1491. }
  1492. aac_fib_complete(fibptr);
  1493. aac_fib_free(fibptr);
  1494. scsicmd->scsi_done(scsicmd);
  1495. }
  1496. static int aac_read(struct scsi_cmnd * scsicmd)
  1497. {
  1498. u64 lba;
  1499. u32 count;
  1500. int status;
  1501. struct aac_dev *dev;
  1502. struct fib * cmd_fibcontext;
  1503. int cid;
  1504. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1505. /*
  1506. * Get block address and transfer length
  1507. */
  1508. switch (scsicmd->cmnd[0]) {
  1509. case READ_6:
  1510. dprintk((KERN_DEBUG "aachba: received a read(6) command on id %d.\n", scmd_id(scsicmd)));
  1511. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) |
  1512. (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  1513. count = scsicmd->cmnd[4];
  1514. if (count == 0)
  1515. count = 256;
  1516. break;
  1517. case READ_16:
  1518. dprintk((KERN_DEBUG "aachba: received a read(16) command on id %d.\n", scmd_id(scsicmd)));
  1519. lba = ((u64)scsicmd->cmnd[2] << 56) |
  1520. ((u64)scsicmd->cmnd[3] << 48) |
  1521. ((u64)scsicmd->cmnd[4] << 40) |
  1522. ((u64)scsicmd->cmnd[5] << 32) |
  1523. ((u64)scsicmd->cmnd[6] << 24) |
  1524. (scsicmd->cmnd[7] << 16) |
  1525. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1526. count = (scsicmd->cmnd[10] << 24) |
  1527. (scsicmd->cmnd[11] << 16) |
  1528. (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
  1529. break;
  1530. case READ_12:
  1531. dprintk((KERN_DEBUG "aachba: received a read(12) command on id %d.\n", scmd_id(scsicmd)));
  1532. lba = ((u64)scsicmd->cmnd[2] << 24) |
  1533. (scsicmd->cmnd[3] << 16) |
  1534. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1535. count = (scsicmd->cmnd[6] << 24) |
  1536. (scsicmd->cmnd[7] << 16) |
  1537. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1538. break;
  1539. default:
  1540. dprintk((KERN_DEBUG "aachba: received a read(10) command on id %d.\n", scmd_id(scsicmd)));
  1541. lba = ((u64)scsicmd->cmnd[2] << 24) |
  1542. (scsicmd->cmnd[3] << 16) |
  1543. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1544. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  1545. break;
  1546. }
  1547. if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
  1548. cid = scmd_id(scsicmd);
  1549. dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
  1550. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  1551. SAM_STAT_CHECK_CONDITION;
  1552. set_sense(&dev->fsa_dev[cid].sense_data,
  1553. HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
  1554. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
  1555. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1556. min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
  1557. SCSI_SENSE_BUFFERSIZE));
  1558. scsicmd->scsi_done(scsicmd);
  1559. return 1;
  1560. }
  1561. dprintk((KERN_DEBUG "aac_read[cpu %d]: lba = %llu, t = %ld.\n",
  1562. smp_processor_id(), (unsigned long long)lba, jiffies));
  1563. if (aac_adapter_bounds(dev,scsicmd,lba))
  1564. return 0;
  1565. /*
  1566. * Alocate and initialize a Fib
  1567. */
  1568. if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
  1569. printk(KERN_WARNING "aac_read: fib allocation failed\n");
  1570. return -1;
  1571. }
  1572. status = aac_adapter_read(cmd_fibcontext, scsicmd, lba, count);
  1573. /*
  1574. * Check that the command queued to the controller
  1575. */
  1576. if (status == -EINPROGRESS) {
  1577. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1578. return 0;
  1579. }
  1580. printk(KERN_WARNING "aac_read: aac_fib_send failed with status: %d.\n", status);
  1581. /*
  1582. * For some reason, the Fib didn't queue, return QUEUE_FULL
  1583. */
  1584. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  1585. scsicmd->scsi_done(scsicmd);
  1586. aac_fib_complete(cmd_fibcontext);
  1587. aac_fib_free(cmd_fibcontext);
  1588. return 0;
  1589. }
  1590. static int aac_write(struct scsi_cmnd * scsicmd)
  1591. {
  1592. u64 lba;
  1593. u32 count;
  1594. int fua;
  1595. int status;
  1596. struct aac_dev *dev;
  1597. struct fib * cmd_fibcontext;
  1598. int cid;
  1599. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  1600. /*
  1601. * Get block address and transfer length
  1602. */
  1603. if (scsicmd->cmnd[0] == WRITE_6) /* 6 byte command */
  1604. {
  1605. lba = ((scsicmd->cmnd[1] & 0x1F) << 16) | (scsicmd->cmnd[2] << 8) | scsicmd->cmnd[3];
  1606. count = scsicmd->cmnd[4];
  1607. if (count == 0)
  1608. count = 256;
  1609. fua = 0;
  1610. } else if (scsicmd->cmnd[0] == WRITE_16) { /* 16 byte command */
  1611. dprintk((KERN_DEBUG "aachba: received a write(16) command on id %d.\n", scmd_id(scsicmd)));
  1612. lba = ((u64)scsicmd->cmnd[2] << 56) |
  1613. ((u64)scsicmd->cmnd[3] << 48) |
  1614. ((u64)scsicmd->cmnd[4] << 40) |
  1615. ((u64)scsicmd->cmnd[5] << 32) |
  1616. ((u64)scsicmd->cmnd[6] << 24) |
  1617. (scsicmd->cmnd[7] << 16) |
  1618. (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1619. count = (scsicmd->cmnd[10] << 24) | (scsicmd->cmnd[11] << 16) |
  1620. (scsicmd->cmnd[12] << 8) | scsicmd->cmnd[13];
  1621. fua = scsicmd->cmnd[1] & 0x8;
  1622. } else if (scsicmd->cmnd[0] == WRITE_12) { /* 12 byte command */
  1623. dprintk((KERN_DEBUG "aachba: received a write(12) command on id %d.\n", scmd_id(scsicmd)));
  1624. lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16)
  1625. | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1626. count = (scsicmd->cmnd[6] << 24) | (scsicmd->cmnd[7] << 16)
  1627. | (scsicmd->cmnd[8] << 8) | scsicmd->cmnd[9];
  1628. fua = scsicmd->cmnd[1] & 0x8;
  1629. } else {
  1630. dprintk((KERN_DEBUG "aachba: received a write(10) command on id %d.\n", scmd_id(scsicmd)));
  1631. lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) | (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1632. count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  1633. fua = scsicmd->cmnd[1] & 0x8;
  1634. }
  1635. if ((lba + count) > (dev->fsa_dev[scmd_id(scsicmd)].size)) {
  1636. cid = scmd_id(scsicmd);
  1637. dprintk((KERN_DEBUG "aacraid: Illegal lba\n"));
  1638. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  1639. SAM_STAT_CHECK_CONDITION;
  1640. set_sense(&dev->fsa_dev[cid].sense_data,
  1641. HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
  1642. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
  1643. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1644. min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
  1645. SCSI_SENSE_BUFFERSIZE));
  1646. scsicmd->scsi_done(scsicmd);
  1647. return 1;
  1648. }
  1649. dprintk((KERN_DEBUG "aac_write[cpu %d]: lba = %llu, t = %ld.\n",
  1650. smp_processor_id(), (unsigned long long)lba, jiffies));
  1651. if (aac_adapter_bounds(dev,scsicmd,lba))
  1652. return 0;
  1653. /*
  1654. * Allocate and initialize a Fib then setup a BlockWrite command
  1655. */
  1656. if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
  1657. /* FIB temporarily unavailable,not catastrophic failure */
  1658. /* scsicmd->result = DID_ERROR << 16;
  1659. * scsicmd->scsi_done(scsicmd);
  1660. * return 0;
  1661. */
  1662. printk(KERN_WARNING "aac_write: fib allocation failed\n");
  1663. return -1;
  1664. }
  1665. status = aac_adapter_write(cmd_fibcontext, scsicmd, lba, count, fua);
  1666. /*
  1667. * Check that the command queued to the controller
  1668. */
  1669. if (status == -EINPROGRESS) {
  1670. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1671. return 0;
  1672. }
  1673. printk(KERN_WARNING "aac_write: aac_fib_send failed with status: %d\n", status);
  1674. /*
  1675. * For some reason, the Fib didn't queue, return QUEUE_FULL
  1676. */
  1677. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_TASK_SET_FULL;
  1678. scsicmd->scsi_done(scsicmd);
  1679. aac_fib_complete(cmd_fibcontext);
  1680. aac_fib_free(cmd_fibcontext);
  1681. return 0;
  1682. }
  1683. static void synchronize_callback(void *context, struct fib *fibptr)
  1684. {
  1685. struct aac_synchronize_reply *synchronizereply;
  1686. struct scsi_cmnd *cmd;
  1687. cmd = context;
  1688. if (!aac_valid_context(cmd, fibptr))
  1689. return;
  1690. dprintk((KERN_DEBUG "synchronize_callback[cpu %d]: t = %ld.\n",
  1691. smp_processor_id(), jiffies));
  1692. BUG_ON(fibptr == NULL);
  1693. synchronizereply = fib_data(fibptr);
  1694. if (le32_to_cpu(synchronizereply->status) == CT_OK)
  1695. cmd->result = DID_OK << 16 |
  1696. COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1697. else {
  1698. struct scsi_device *sdev = cmd->device;
  1699. struct aac_dev *dev = fibptr->dev;
  1700. u32 cid = sdev_id(sdev);
  1701. printk(KERN_WARNING
  1702. "synchronize_callback: synchronize failed, status = %d\n",
  1703. le32_to_cpu(synchronizereply->status));
  1704. cmd->result = DID_OK << 16 |
  1705. COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1706. set_sense(&dev->fsa_dev[cid].sense_data,
  1707. HARDWARE_ERROR, SENCODE_INTERNAL_TARGET_FAILURE,
  1708. ASENCODE_INTERNAL_TARGET_FAILURE, 0, 0);
  1709. memcpy(cmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1710. min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
  1711. SCSI_SENSE_BUFFERSIZE));
  1712. }
  1713. aac_fib_complete(fibptr);
  1714. aac_fib_free(fibptr);
  1715. cmd->scsi_done(cmd);
  1716. }
  1717. static int aac_synchronize(struct scsi_cmnd *scsicmd)
  1718. {
  1719. int status;
  1720. struct fib *cmd_fibcontext;
  1721. struct aac_synchronize *synchronizecmd;
  1722. struct scsi_cmnd *cmd;
  1723. struct scsi_device *sdev = scsicmd->device;
  1724. int active = 0;
  1725. struct aac_dev *aac;
  1726. u64 lba = ((u64)scsicmd->cmnd[2] << 24) | (scsicmd->cmnd[3] << 16) |
  1727. (scsicmd->cmnd[4] << 8) | scsicmd->cmnd[5];
  1728. u32 count = (scsicmd->cmnd[7] << 8) | scsicmd->cmnd[8];
  1729. unsigned long flags;
  1730. /*
  1731. * Wait for all outstanding queued commands to complete to this
  1732. * specific target (block).
  1733. */
  1734. spin_lock_irqsave(&sdev->list_lock, flags);
  1735. list_for_each_entry(cmd, &sdev->cmd_list, list)
  1736. if (cmd->SCp.phase == AAC_OWNER_FIRMWARE) {
  1737. u64 cmnd_lba;
  1738. u32 cmnd_count;
  1739. if (cmd->cmnd[0] == WRITE_6) {
  1740. cmnd_lba = ((cmd->cmnd[1] & 0x1F) << 16) |
  1741. (cmd->cmnd[2] << 8) |
  1742. cmd->cmnd[3];
  1743. cmnd_count = cmd->cmnd[4];
  1744. if (cmnd_count == 0)
  1745. cmnd_count = 256;
  1746. } else if (cmd->cmnd[0] == WRITE_16) {
  1747. cmnd_lba = ((u64)cmd->cmnd[2] << 56) |
  1748. ((u64)cmd->cmnd[3] << 48) |
  1749. ((u64)cmd->cmnd[4] << 40) |
  1750. ((u64)cmd->cmnd[5] << 32) |
  1751. ((u64)cmd->cmnd[6] << 24) |
  1752. (cmd->cmnd[7] << 16) |
  1753. (cmd->cmnd[8] << 8) |
  1754. cmd->cmnd[9];
  1755. cmnd_count = (cmd->cmnd[10] << 24) |
  1756. (cmd->cmnd[11] << 16) |
  1757. (cmd->cmnd[12] << 8) |
  1758. cmd->cmnd[13];
  1759. } else if (cmd->cmnd[0] == WRITE_12) {
  1760. cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
  1761. (cmd->cmnd[3] << 16) |
  1762. (cmd->cmnd[4] << 8) |
  1763. cmd->cmnd[5];
  1764. cmnd_count = (cmd->cmnd[6] << 24) |
  1765. (cmd->cmnd[7] << 16) |
  1766. (cmd->cmnd[8] << 8) |
  1767. cmd->cmnd[9];
  1768. } else if (cmd->cmnd[0] == WRITE_10) {
  1769. cmnd_lba = ((u64)cmd->cmnd[2] << 24) |
  1770. (cmd->cmnd[3] << 16) |
  1771. (cmd->cmnd[4] << 8) |
  1772. cmd->cmnd[5];
  1773. cmnd_count = (cmd->cmnd[7] << 8) |
  1774. cmd->cmnd[8];
  1775. } else
  1776. continue;
  1777. if (((cmnd_lba + cmnd_count) < lba) ||
  1778. (count && ((lba + count) < cmnd_lba)))
  1779. continue;
  1780. ++active;
  1781. break;
  1782. }
  1783. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1784. /*
  1785. * Yield the processor (requeue for later)
  1786. */
  1787. if (active)
  1788. return SCSI_MLQUEUE_DEVICE_BUSY;
  1789. aac = (struct aac_dev *)sdev->host->hostdata;
  1790. if (aac->in_reset)
  1791. return SCSI_MLQUEUE_HOST_BUSY;
  1792. /*
  1793. * Allocate and initialize a Fib
  1794. */
  1795. if (!(cmd_fibcontext = aac_fib_alloc(aac)))
  1796. return SCSI_MLQUEUE_HOST_BUSY;
  1797. aac_fib_init(cmd_fibcontext);
  1798. synchronizecmd = fib_data(cmd_fibcontext);
  1799. synchronizecmd->command = cpu_to_le32(VM_ContainerConfig);
  1800. synchronizecmd->type = cpu_to_le32(CT_FLUSH_CACHE);
  1801. synchronizecmd->cid = cpu_to_le32(scmd_id(scsicmd));
  1802. synchronizecmd->count =
  1803. cpu_to_le32(sizeof(((struct aac_synchronize_reply *)NULL)->data));
  1804. /*
  1805. * Now send the Fib to the adapter
  1806. */
  1807. status = aac_fib_send(ContainerCommand,
  1808. cmd_fibcontext,
  1809. sizeof(struct aac_synchronize),
  1810. FsaNormal,
  1811. 0, 1,
  1812. (fib_callback)synchronize_callback,
  1813. (void *)scsicmd);
  1814. /*
  1815. * Check that the command queued to the controller
  1816. */
  1817. if (status == -EINPROGRESS) {
  1818. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1819. return 0;
  1820. }
  1821. printk(KERN_WARNING
  1822. "aac_synchronize: aac_fib_send failed with status: %d.\n", status);
  1823. aac_fib_complete(cmd_fibcontext);
  1824. aac_fib_free(cmd_fibcontext);
  1825. return SCSI_MLQUEUE_HOST_BUSY;
  1826. }
  1827. static void aac_start_stop_callback(void *context, struct fib *fibptr)
  1828. {
  1829. struct scsi_cmnd *scsicmd = context;
  1830. if (!aac_valid_context(scsicmd, fibptr))
  1831. return;
  1832. BUG_ON(fibptr == NULL);
  1833. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1834. aac_fib_complete(fibptr);
  1835. aac_fib_free(fibptr);
  1836. scsicmd->scsi_done(scsicmd);
  1837. }
  1838. static int aac_start_stop(struct scsi_cmnd *scsicmd)
  1839. {
  1840. int status;
  1841. struct fib *cmd_fibcontext;
  1842. struct aac_power_management *pmcmd;
  1843. struct scsi_device *sdev = scsicmd->device;
  1844. struct aac_dev *aac = (struct aac_dev *)sdev->host->hostdata;
  1845. if (!(aac->supplement_adapter_info.SupportedOptions2 &
  1846. AAC_OPTION_POWER_MANAGEMENT)) {
  1847. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  1848. SAM_STAT_GOOD;
  1849. scsicmd->scsi_done(scsicmd);
  1850. return 0;
  1851. }
  1852. if (aac->in_reset)
  1853. return SCSI_MLQUEUE_HOST_BUSY;
  1854. /*
  1855. * Allocate and initialize a Fib
  1856. */
  1857. cmd_fibcontext = aac_fib_alloc(aac);
  1858. if (!cmd_fibcontext)
  1859. return SCSI_MLQUEUE_HOST_BUSY;
  1860. aac_fib_init(cmd_fibcontext);
  1861. pmcmd = fib_data(cmd_fibcontext);
  1862. pmcmd->command = cpu_to_le32(VM_ContainerConfig);
  1863. pmcmd->type = cpu_to_le32(CT_POWER_MANAGEMENT);
  1864. /* Eject bit ignored, not relevant */
  1865. pmcmd->sub = (scsicmd->cmnd[4] & 1) ?
  1866. cpu_to_le32(CT_PM_START_UNIT) : cpu_to_le32(CT_PM_STOP_UNIT);
  1867. pmcmd->cid = cpu_to_le32(sdev_id(sdev));
  1868. pmcmd->parm = (scsicmd->cmnd[1] & 1) ?
  1869. cpu_to_le32(CT_PM_UNIT_IMMEDIATE) : 0;
  1870. /*
  1871. * Now send the Fib to the adapter
  1872. */
  1873. status = aac_fib_send(ContainerCommand,
  1874. cmd_fibcontext,
  1875. sizeof(struct aac_power_management),
  1876. FsaNormal,
  1877. 0, 1,
  1878. (fib_callback)aac_start_stop_callback,
  1879. (void *)scsicmd);
  1880. /*
  1881. * Check that the command queued to the controller
  1882. */
  1883. if (status == -EINPROGRESS) {
  1884. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  1885. return 0;
  1886. }
  1887. aac_fib_complete(cmd_fibcontext);
  1888. aac_fib_free(cmd_fibcontext);
  1889. return SCSI_MLQUEUE_HOST_BUSY;
  1890. }
  1891. /**
  1892. * aac_scsi_cmd() - Process SCSI command
  1893. * @scsicmd: SCSI command block
  1894. *
  1895. * Emulate a SCSI command and queue the required request for the
  1896. * aacraid firmware.
  1897. */
  1898. int aac_scsi_cmd(struct scsi_cmnd * scsicmd)
  1899. {
  1900. u32 cid;
  1901. struct Scsi_Host *host = scsicmd->device->host;
  1902. struct aac_dev *dev = (struct aac_dev *)host->hostdata;
  1903. struct fsa_dev_info *fsa_dev_ptr = dev->fsa_dev;
  1904. if (fsa_dev_ptr == NULL)
  1905. return -1;
  1906. /*
  1907. * If the bus, id or lun is out of range, return fail
  1908. * Test does not apply to ID 16, the pseudo id for the controller
  1909. * itself.
  1910. */
  1911. cid = scmd_id(scsicmd);
  1912. if (cid != host->this_id) {
  1913. if (scmd_channel(scsicmd) == CONTAINER_CHANNEL) {
  1914. if((cid >= dev->maximum_num_containers) ||
  1915. (scsicmd->device->lun != 0)) {
  1916. scsicmd->result = DID_NO_CONNECT << 16;
  1917. scsicmd->scsi_done(scsicmd);
  1918. return 0;
  1919. }
  1920. /*
  1921. * If the target container doesn't exist, it may have
  1922. * been newly created
  1923. */
  1924. if (((fsa_dev_ptr[cid].valid & 1) == 0) ||
  1925. (fsa_dev_ptr[cid].sense_data.sense_key ==
  1926. NOT_READY)) {
  1927. switch (scsicmd->cmnd[0]) {
  1928. case SERVICE_ACTION_IN:
  1929. if (!(dev->raw_io_interface) ||
  1930. !(dev->raw_io_64) ||
  1931. ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
  1932. break;
  1933. case INQUIRY:
  1934. case READ_CAPACITY:
  1935. case TEST_UNIT_READY:
  1936. if (dev->in_reset)
  1937. return -1;
  1938. return _aac_probe_container(scsicmd,
  1939. aac_probe_container_callback2);
  1940. default:
  1941. break;
  1942. }
  1943. }
  1944. } else { /* check for physical non-dasd devices */
  1945. if (dev->nondasd_support || expose_physicals ||
  1946. dev->jbod) {
  1947. if (dev->in_reset)
  1948. return -1;
  1949. return aac_send_srb_fib(scsicmd);
  1950. } else {
  1951. scsicmd->result = DID_NO_CONNECT << 16;
  1952. scsicmd->scsi_done(scsicmd);
  1953. return 0;
  1954. }
  1955. }
  1956. }
  1957. /*
  1958. * else Command for the controller itself
  1959. */
  1960. else if ((scsicmd->cmnd[0] != INQUIRY) && /* only INQUIRY & TUR cmnd supported for controller */
  1961. (scsicmd->cmnd[0] != TEST_UNIT_READY))
  1962. {
  1963. dprintk((KERN_WARNING "Only INQUIRY & TUR command supported for controller, rcvd = 0x%x.\n", scsicmd->cmnd[0]));
  1964. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  1965. set_sense(&dev->fsa_dev[cid].sense_data,
  1966. ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
  1967. ASENCODE_INVALID_COMMAND, 0, 0);
  1968. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  1969. min_t(size_t, sizeof(dev->fsa_dev[cid].sense_data),
  1970. SCSI_SENSE_BUFFERSIZE));
  1971. scsicmd->scsi_done(scsicmd);
  1972. return 0;
  1973. }
  1974. /* Handle commands here that don't really require going out to the adapter */
  1975. switch (scsicmd->cmnd[0]) {
  1976. case INQUIRY:
  1977. {
  1978. struct inquiry_data inq_data;
  1979. dprintk((KERN_DEBUG "INQUIRY command, ID: %d.\n", cid));
  1980. memset(&inq_data, 0, sizeof (struct inquiry_data));
  1981. if ((scsicmd->cmnd[1] & 0x1) && aac_wwn) {
  1982. char *arr = (char *)&inq_data;
  1983. /* EVPD bit set */
  1984. arr[0] = (scmd_id(scsicmd) == host->this_id) ?
  1985. INQD_PDT_PROC : INQD_PDT_DA;
  1986. if (scsicmd->cmnd[2] == 0) {
  1987. /* supported vital product data pages */
  1988. arr[3] = 2;
  1989. arr[4] = 0x0;
  1990. arr[5] = 0x80;
  1991. arr[1] = scsicmd->cmnd[2];
  1992. scsi_sg_copy_from_buffer(scsicmd, &inq_data,
  1993. sizeof(inq_data));
  1994. scsicmd->result = DID_OK << 16 |
  1995. COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  1996. } else if (scsicmd->cmnd[2] == 0x80) {
  1997. /* unit serial number page */
  1998. arr[3] = setinqserial(dev, &arr[4],
  1999. scmd_id(scsicmd));
  2000. arr[1] = scsicmd->cmnd[2];
  2001. scsi_sg_copy_from_buffer(scsicmd, &inq_data,
  2002. sizeof(inq_data));
  2003. if (aac_wwn != 2)
  2004. return aac_get_container_serial(
  2005. scsicmd);
  2006. /* SLES 10 SP1 special */
  2007. scsicmd->result = DID_OK << 16 |
  2008. COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2009. } else {
  2010. /* vpd page not implemented */
  2011. scsicmd->result = DID_OK << 16 |
  2012. COMMAND_COMPLETE << 8 |
  2013. SAM_STAT_CHECK_CONDITION;
  2014. set_sense(&dev->fsa_dev[cid].sense_data,
  2015. ILLEGAL_REQUEST, SENCODE_INVALID_CDB_FIELD,
  2016. ASENCODE_NO_SENSE, 7, 2);
  2017. memcpy(scsicmd->sense_buffer,
  2018. &dev->fsa_dev[cid].sense_data,
  2019. min_t(size_t,
  2020. sizeof(dev->fsa_dev[cid].sense_data),
  2021. SCSI_SENSE_BUFFERSIZE));
  2022. }
  2023. scsicmd->scsi_done(scsicmd);
  2024. return 0;
  2025. }
  2026. inq_data.inqd_ver = 2; /* claim compliance to SCSI-2 */
  2027. 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 */
  2028. inq_data.inqd_len = 31;
  2029. /*Format for "pad2" is RelAdr | WBus32 | WBus16 | Sync | Linked |Reserved| CmdQue | SftRe */
  2030. inq_data.inqd_pad2= 0x32 ; /*WBus16|Sync|CmdQue */
  2031. /*
  2032. * Set the Vendor, Product, and Revision Level
  2033. * see: <vendor>.c i.e. aac.c
  2034. */
  2035. if (cid == host->this_id) {
  2036. setinqstr(dev, (void *) (inq_data.inqd_vid), ARRAY_SIZE(container_types));
  2037. inq_data.inqd_pdt = INQD_PDT_PROC; /* Processor device */
  2038. scsi_sg_copy_from_buffer(scsicmd, &inq_data,
  2039. sizeof(inq_data));
  2040. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2041. scsicmd->scsi_done(scsicmd);
  2042. return 0;
  2043. }
  2044. if (dev->in_reset)
  2045. return -1;
  2046. setinqstr(dev, (void *) (inq_data.inqd_vid), fsa_dev_ptr[cid].type);
  2047. inq_data.inqd_pdt = INQD_PDT_DA; /* Direct/random access device */
  2048. scsi_sg_copy_from_buffer(scsicmd, &inq_data, sizeof(inq_data));
  2049. return aac_get_container_name(scsicmd);
  2050. }
  2051. case SERVICE_ACTION_IN:
  2052. if (!(dev->raw_io_interface) ||
  2053. !(dev->raw_io_64) ||
  2054. ((scsicmd->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16))
  2055. break;
  2056. {
  2057. u64 capacity;
  2058. char cp[13];
  2059. unsigned int alloc_len;
  2060. dprintk((KERN_DEBUG "READ CAPACITY_16 command.\n"));
  2061. capacity = fsa_dev_ptr[cid].size - 1;
  2062. cp[0] = (capacity >> 56) & 0xff;
  2063. cp[1] = (capacity >> 48) & 0xff;
  2064. cp[2] = (capacity >> 40) & 0xff;
  2065. cp[3] = (capacity >> 32) & 0xff;
  2066. cp[4] = (capacity >> 24) & 0xff;
  2067. cp[5] = (capacity >> 16) & 0xff;
  2068. cp[6] = (capacity >> 8) & 0xff;
  2069. cp[7] = (capacity >> 0) & 0xff;
  2070. cp[8] = 0;
  2071. cp[9] = 0;
  2072. cp[10] = 2;
  2073. cp[11] = 0;
  2074. cp[12] = 0;
  2075. alloc_len = ((scsicmd->cmnd[10] << 24)
  2076. + (scsicmd->cmnd[11] << 16)
  2077. + (scsicmd->cmnd[12] << 8) + scsicmd->cmnd[13]);
  2078. alloc_len = min_t(size_t, alloc_len, sizeof(cp));
  2079. scsi_sg_copy_from_buffer(scsicmd, cp, alloc_len);
  2080. if (alloc_len < scsi_bufflen(scsicmd))
  2081. scsi_set_resid(scsicmd,
  2082. scsi_bufflen(scsicmd) - alloc_len);
  2083. /* Do not cache partition table for arrays */
  2084. scsicmd->device->removable = 1;
  2085. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2086. scsicmd->scsi_done(scsicmd);
  2087. return 0;
  2088. }
  2089. case READ_CAPACITY:
  2090. {
  2091. u32 capacity;
  2092. char cp[8];
  2093. dprintk((KERN_DEBUG "READ CAPACITY command.\n"));
  2094. if (fsa_dev_ptr[cid].size <= 0x100000000ULL)
  2095. capacity = fsa_dev_ptr[cid].size - 1;
  2096. else
  2097. capacity = (u32)-1;
  2098. cp[0] = (capacity >> 24) & 0xff;
  2099. cp[1] = (capacity >> 16) & 0xff;
  2100. cp[2] = (capacity >> 8) & 0xff;
  2101. cp[3] = (capacity >> 0) & 0xff;
  2102. cp[4] = 0;
  2103. cp[5] = 0;
  2104. cp[6] = 2;
  2105. cp[7] = 0;
  2106. scsi_sg_copy_from_buffer(scsicmd, cp, sizeof(cp));
  2107. /* Do not cache partition table for arrays */
  2108. scsicmd->device->removable = 1;
  2109. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  2110. SAM_STAT_GOOD;
  2111. scsicmd->scsi_done(scsicmd);
  2112. return 0;
  2113. }
  2114. case MODE_SENSE:
  2115. {
  2116. char mode_buf[7];
  2117. int mode_buf_length = 4;
  2118. dprintk((KERN_DEBUG "MODE SENSE command.\n"));
  2119. mode_buf[0] = 3; /* Mode data length */
  2120. mode_buf[1] = 0; /* Medium type - default */
  2121. mode_buf[2] = 0; /* Device-specific param,
  2122. bit 8: 0/1 = write enabled/protected
  2123. bit 4: 0/1 = FUA enabled */
  2124. if (dev->raw_io_interface && ((aac_cache & 5) != 1))
  2125. mode_buf[2] = 0x10;
  2126. mode_buf[3] = 0; /* Block descriptor length */
  2127. if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
  2128. ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
  2129. mode_buf[0] = 6;
  2130. mode_buf[4] = 8;
  2131. mode_buf[5] = 1;
  2132. mode_buf[6] = ((aac_cache & 6) == 2)
  2133. ? 0 : 0x04; /* WCE */
  2134. mode_buf_length = 7;
  2135. if (mode_buf_length > scsicmd->cmnd[4])
  2136. mode_buf_length = scsicmd->cmnd[4];
  2137. }
  2138. scsi_sg_copy_from_buffer(scsicmd, mode_buf, mode_buf_length);
  2139. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2140. scsicmd->scsi_done(scsicmd);
  2141. return 0;
  2142. }
  2143. case MODE_SENSE_10:
  2144. {
  2145. char mode_buf[11];
  2146. int mode_buf_length = 8;
  2147. dprintk((KERN_DEBUG "MODE SENSE 10 byte command.\n"));
  2148. mode_buf[0] = 0; /* Mode data length (MSB) */
  2149. mode_buf[1] = 6; /* Mode data length (LSB) */
  2150. mode_buf[2] = 0; /* Medium type - default */
  2151. mode_buf[3] = 0; /* Device-specific param,
  2152. bit 8: 0/1 = write enabled/protected
  2153. bit 4: 0/1 = FUA enabled */
  2154. if (dev->raw_io_interface && ((aac_cache & 5) != 1))
  2155. mode_buf[3] = 0x10;
  2156. mode_buf[4] = 0; /* reserved */
  2157. mode_buf[5] = 0; /* reserved */
  2158. mode_buf[6] = 0; /* Block descriptor length (MSB) */
  2159. mode_buf[7] = 0; /* Block descriptor length (LSB) */
  2160. if (((scsicmd->cmnd[2] & 0x3f) == 8) ||
  2161. ((scsicmd->cmnd[2] & 0x3f) == 0x3f)) {
  2162. mode_buf[1] = 9;
  2163. mode_buf[8] = 8;
  2164. mode_buf[9] = 1;
  2165. mode_buf[10] = ((aac_cache & 6) == 2)
  2166. ? 0 : 0x04; /* WCE */
  2167. mode_buf_length = 11;
  2168. if (mode_buf_length > scsicmd->cmnd[8])
  2169. mode_buf_length = scsicmd->cmnd[8];
  2170. }
  2171. scsi_sg_copy_from_buffer(scsicmd, mode_buf, mode_buf_length);
  2172. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2173. scsicmd->scsi_done(scsicmd);
  2174. return 0;
  2175. }
  2176. case REQUEST_SENSE:
  2177. dprintk((KERN_DEBUG "REQUEST SENSE command.\n"));
  2178. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data, sizeof (struct sense_data));
  2179. memset(&dev->fsa_dev[cid].sense_data, 0, sizeof (struct sense_data));
  2180. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2181. scsicmd->scsi_done(scsicmd);
  2182. return 0;
  2183. case ALLOW_MEDIUM_REMOVAL:
  2184. dprintk((KERN_DEBUG "LOCK command.\n"));
  2185. if (scsicmd->cmnd[4])
  2186. fsa_dev_ptr[cid].locked = 1;
  2187. else
  2188. fsa_dev_ptr[cid].locked = 0;
  2189. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2190. scsicmd->scsi_done(scsicmd);
  2191. return 0;
  2192. /*
  2193. * These commands are all No-Ops
  2194. */
  2195. case TEST_UNIT_READY:
  2196. if (fsa_dev_ptr[cid].sense_data.sense_key == NOT_READY) {
  2197. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 |
  2198. SAM_STAT_CHECK_CONDITION;
  2199. set_sense(&dev->fsa_dev[cid].sense_data,
  2200. NOT_READY, SENCODE_BECOMING_READY,
  2201. ASENCODE_BECOMING_READY, 0, 0);
  2202. memcpy(scsicmd->sense_buffer,
  2203. &dev->fsa_dev[cid].sense_data,
  2204. min_t(size_t,
  2205. sizeof(dev->fsa_dev[cid].sense_data),
  2206. SCSI_SENSE_BUFFERSIZE));
  2207. scsicmd->scsi_done(scsicmd);
  2208. return 0;
  2209. }
  2210. /* FALLTHRU */
  2211. case RESERVE:
  2212. case RELEASE:
  2213. case REZERO_UNIT:
  2214. case REASSIGN_BLOCKS:
  2215. case SEEK_10:
  2216. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2217. scsicmd->scsi_done(scsicmd);
  2218. return 0;
  2219. case START_STOP:
  2220. return aac_start_stop(scsicmd);
  2221. }
  2222. switch (scsicmd->cmnd[0])
  2223. {
  2224. case READ_6:
  2225. case READ_10:
  2226. case READ_12:
  2227. case READ_16:
  2228. if (dev->in_reset)
  2229. return -1;
  2230. /*
  2231. * Hack to keep track of ordinal number of the device that
  2232. * corresponds to a container. Needed to convert
  2233. * containers to /dev/sd device names
  2234. */
  2235. if (scsicmd->request->rq_disk)
  2236. strlcpy(fsa_dev_ptr[cid].devname,
  2237. scsicmd->request->rq_disk->disk_name,
  2238. min(sizeof(fsa_dev_ptr[cid].devname),
  2239. sizeof(scsicmd->request->rq_disk->disk_name) + 1));
  2240. return aac_read(scsicmd);
  2241. case WRITE_6:
  2242. case WRITE_10:
  2243. case WRITE_12:
  2244. case WRITE_16:
  2245. if (dev->in_reset)
  2246. return -1;
  2247. return aac_write(scsicmd);
  2248. case SYNCHRONIZE_CACHE:
  2249. if (((aac_cache & 6) == 6) && dev->cache_protected) {
  2250. scsicmd->result = DID_OK << 16 |
  2251. COMMAND_COMPLETE << 8 | SAM_STAT_GOOD;
  2252. scsicmd->scsi_done(scsicmd);
  2253. return 0;
  2254. }
  2255. /* Issue FIB to tell Firmware to flush it's cache */
  2256. if ((aac_cache & 6) != 2)
  2257. return aac_synchronize(scsicmd);
  2258. /* FALLTHRU */
  2259. default:
  2260. /*
  2261. * Unhandled commands
  2262. */
  2263. dprintk((KERN_WARNING "Unhandled SCSI Command: 0x%x.\n", scsicmd->cmnd[0]));
  2264. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  2265. set_sense(&dev->fsa_dev[cid].sense_data,
  2266. ILLEGAL_REQUEST, SENCODE_INVALID_COMMAND,
  2267. ASENCODE_INVALID_COMMAND, 0, 0);
  2268. memcpy(scsicmd->sense_buffer, &dev->fsa_dev[cid].sense_data,
  2269. min_t(size_t,
  2270. sizeof(dev->fsa_dev[cid].sense_data),
  2271. SCSI_SENSE_BUFFERSIZE));
  2272. scsicmd->scsi_done(scsicmd);
  2273. return 0;
  2274. }
  2275. }
  2276. static int query_disk(struct aac_dev *dev, void __user *arg)
  2277. {
  2278. struct aac_query_disk qd;
  2279. struct fsa_dev_info *fsa_dev_ptr;
  2280. fsa_dev_ptr = dev->fsa_dev;
  2281. if (!fsa_dev_ptr)
  2282. return -EBUSY;
  2283. if (copy_from_user(&qd, arg, sizeof (struct aac_query_disk)))
  2284. return -EFAULT;
  2285. if (qd.cnum == -1)
  2286. qd.cnum = qd.id;
  2287. else if ((qd.bus == -1) && (qd.id == -1) && (qd.lun == -1))
  2288. {
  2289. if (qd.cnum < 0 || qd.cnum >= dev->maximum_num_containers)
  2290. return -EINVAL;
  2291. qd.instance = dev->scsi_host_ptr->host_no;
  2292. qd.bus = 0;
  2293. qd.id = CONTAINER_TO_ID(qd.cnum);
  2294. qd.lun = CONTAINER_TO_LUN(qd.cnum);
  2295. }
  2296. else return -EINVAL;
  2297. qd.valid = fsa_dev_ptr[qd.cnum].valid != 0;
  2298. qd.locked = fsa_dev_ptr[qd.cnum].locked;
  2299. qd.deleted = fsa_dev_ptr[qd.cnum].deleted;
  2300. if (fsa_dev_ptr[qd.cnum].devname[0] == '\0')
  2301. qd.unmapped = 1;
  2302. else
  2303. qd.unmapped = 0;
  2304. strlcpy(qd.name, fsa_dev_ptr[qd.cnum].devname,
  2305. min(sizeof(qd.name), sizeof(fsa_dev_ptr[qd.cnum].devname) + 1));
  2306. if (copy_to_user(arg, &qd, sizeof (struct aac_query_disk)))
  2307. return -EFAULT;
  2308. return 0;
  2309. }
  2310. static int force_delete_disk(struct aac_dev *dev, void __user *arg)
  2311. {
  2312. struct aac_delete_disk dd;
  2313. struct fsa_dev_info *fsa_dev_ptr;
  2314. fsa_dev_ptr = dev->fsa_dev;
  2315. if (!fsa_dev_ptr)
  2316. return -EBUSY;
  2317. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  2318. return -EFAULT;
  2319. if (dd.cnum >= dev->maximum_num_containers)
  2320. return -EINVAL;
  2321. /*
  2322. * Mark this container as being deleted.
  2323. */
  2324. fsa_dev_ptr[dd.cnum].deleted = 1;
  2325. /*
  2326. * Mark the container as no longer valid
  2327. */
  2328. fsa_dev_ptr[dd.cnum].valid = 0;
  2329. return 0;
  2330. }
  2331. static int delete_disk(struct aac_dev *dev, void __user *arg)
  2332. {
  2333. struct aac_delete_disk dd;
  2334. struct fsa_dev_info *fsa_dev_ptr;
  2335. fsa_dev_ptr = dev->fsa_dev;
  2336. if (!fsa_dev_ptr)
  2337. return -EBUSY;
  2338. if (copy_from_user(&dd, arg, sizeof (struct aac_delete_disk)))
  2339. return -EFAULT;
  2340. if (dd.cnum >= dev->maximum_num_containers)
  2341. return -EINVAL;
  2342. /*
  2343. * If the container is locked, it can not be deleted by the API.
  2344. */
  2345. if (fsa_dev_ptr[dd.cnum].locked)
  2346. return -EBUSY;
  2347. else {
  2348. /*
  2349. * Mark the container as no longer being valid.
  2350. */
  2351. fsa_dev_ptr[dd.cnum].valid = 0;
  2352. fsa_dev_ptr[dd.cnum].devname[0] = '\0';
  2353. return 0;
  2354. }
  2355. }
  2356. int aac_dev_ioctl(struct aac_dev *dev, int cmd, void __user *arg)
  2357. {
  2358. switch (cmd) {
  2359. case FSACTL_QUERY_DISK:
  2360. return query_disk(dev, arg);
  2361. case FSACTL_DELETE_DISK:
  2362. return delete_disk(dev, arg);
  2363. case FSACTL_FORCE_DELETE_DISK:
  2364. return force_delete_disk(dev, arg);
  2365. case FSACTL_GET_CONTAINERS:
  2366. return aac_get_containers(dev);
  2367. default:
  2368. return -ENOTTY;
  2369. }
  2370. }
  2371. /**
  2372. *
  2373. * aac_srb_callback
  2374. * @context: the context set in the fib - here it is scsi cmd
  2375. * @fibptr: pointer to the fib
  2376. *
  2377. * Handles the completion of a scsi command to a non dasd device
  2378. *
  2379. */
  2380. static void aac_srb_callback(void *context, struct fib * fibptr)
  2381. {
  2382. struct aac_dev *dev;
  2383. struct aac_srb_reply *srbreply;
  2384. struct scsi_cmnd *scsicmd;
  2385. scsicmd = (struct scsi_cmnd *) context;
  2386. if (!aac_valid_context(scsicmd, fibptr))
  2387. return;
  2388. BUG_ON(fibptr == NULL);
  2389. dev = fibptr->dev;
  2390. srbreply = (struct aac_srb_reply *) fib_data(fibptr);
  2391. scsicmd->sense_buffer[0] = '\0'; /* Initialize sense valid flag to false */
  2392. if (fibptr->flags & FIB_CONTEXT_FLAG_FASTRESP) {
  2393. /* fast response */
  2394. srbreply->srb_status = cpu_to_le32(SRB_STATUS_SUCCESS);
  2395. srbreply->scsi_status = cpu_to_le32(SAM_STAT_GOOD);
  2396. } else {
  2397. /*
  2398. * Calculate resid for sg
  2399. */
  2400. scsi_set_resid(scsicmd, scsi_bufflen(scsicmd)
  2401. - le32_to_cpu(srbreply->data_xfer_length));
  2402. }
  2403. scsi_dma_unmap(scsicmd);
  2404. /* expose physical device if expose_physicald flag is on */
  2405. if (scsicmd->cmnd[0] == INQUIRY && !(scsicmd->cmnd[1] & 0x01)
  2406. && expose_physicals > 0)
  2407. aac_expose_phy_device(scsicmd);
  2408. /*
  2409. * First check the fib status
  2410. */
  2411. if (le32_to_cpu(srbreply->status) != ST_OK){
  2412. int len;
  2413. printk(KERN_WARNING "aac_srb_callback: srb failed, status = %d\n", le32_to_cpu(srbreply->status));
  2414. len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
  2415. SCSI_SENSE_BUFFERSIZE);
  2416. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8 | SAM_STAT_CHECK_CONDITION;
  2417. memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
  2418. }
  2419. /*
  2420. * Next check the srb status
  2421. */
  2422. switch( (le32_to_cpu(srbreply->srb_status))&0x3f){
  2423. case SRB_STATUS_ERROR_RECOVERY:
  2424. case SRB_STATUS_PENDING:
  2425. case SRB_STATUS_SUCCESS:
  2426. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  2427. break;
  2428. case SRB_STATUS_DATA_OVERRUN:
  2429. switch(scsicmd->cmnd[0]){
  2430. case READ_6:
  2431. case WRITE_6:
  2432. case READ_10:
  2433. case WRITE_10:
  2434. case READ_12:
  2435. case WRITE_12:
  2436. case READ_16:
  2437. case WRITE_16:
  2438. if (le32_to_cpu(srbreply->data_xfer_length) < scsicmd->underflow) {
  2439. printk(KERN_WARNING"aacraid: SCSI CMD underflow\n");
  2440. } else {
  2441. printk(KERN_WARNING"aacraid: SCSI CMD Data Overrun\n");
  2442. }
  2443. scsicmd->result = DID_ERROR << 16 | COMMAND_COMPLETE << 8;
  2444. break;
  2445. case INQUIRY: {
  2446. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  2447. break;
  2448. }
  2449. default:
  2450. scsicmd->result = DID_OK << 16 | COMMAND_COMPLETE << 8;
  2451. break;
  2452. }
  2453. break;
  2454. case SRB_STATUS_ABORTED:
  2455. scsicmd->result = DID_ABORT << 16 | ABORT << 8;
  2456. break;
  2457. case SRB_STATUS_ABORT_FAILED:
  2458. // Not sure about this one - but assuming the hba was trying to abort for some reason
  2459. scsicmd->result = DID_ERROR << 16 | ABORT << 8;
  2460. break;
  2461. case SRB_STATUS_PARITY_ERROR:
  2462. scsicmd->result = DID_PARITY << 16 | MSG_PARITY_ERROR << 8;
  2463. break;
  2464. case SRB_STATUS_NO_DEVICE:
  2465. case SRB_STATUS_INVALID_PATH_ID:
  2466. case SRB_STATUS_INVALID_TARGET_ID:
  2467. case SRB_STATUS_INVALID_LUN:
  2468. case SRB_STATUS_SELECTION_TIMEOUT:
  2469. scsicmd->result = DID_NO_CONNECT << 16 | COMMAND_COMPLETE << 8;
  2470. break;
  2471. case SRB_STATUS_COMMAND_TIMEOUT:
  2472. case SRB_STATUS_TIMEOUT:
  2473. scsicmd->result = DID_TIME_OUT << 16 | COMMAND_COMPLETE << 8;
  2474. break;
  2475. case SRB_STATUS_BUSY:
  2476. scsicmd->result = DID_BUS_BUSY << 16 | COMMAND_COMPLETE << 8;
  2477. break;
  2478. case SRB_STATUS_BUS_RESET:
  2479. scsicmd->result = DID_RESET << 16 | COMMAND_COMPLETE << 8;
  2480. break;
  2481. case SRB_STATUS_MESSAGE_REJECTED:
  2482. scsicmd->result = DID_ERROR << 16 | MESSAGE_REJECT << 8;
  2483. break;
  2484. case SRB_STATUS_REQUEST_FLUSHED:
  2485. case SRB_STATUS_ERROR:
  2486. case SRB_STATUS_INVALID_REQUEST:
  2487. case SRB_STATUS_REQUEST_SENSE_FAILED:
  2488. case SRB_STATUS_NO_HBA:
  2489. case SRB_STATUS_UNEXPECTED_BUS_FREE:
  2490. case SRB_STATUS_PHASE_SEQUENCE_FAILURE:
  2491. case SRB_STATUS_BAD_SRB_BLOCK_LENGTH:
  2492. case SRB_STATUS_DELAYED_RETRY:
  2493. case SRB_STATUS_BAD_FUNCTION:
  2494. case SRB_STATUS_NOT_STARTED:
  2495. case SRB_STATUS_NOT_IN_USE:
  2496. case SRB_STATUS_FORCE_ABORT:
  2497. case SRB_STATUS_DOMAIN_VALIDATION_FAIL:
  2498. default:
  2499. #ifdef AAC_DETAILED_STATUS_INFO
  2500. printk("aacraid: SRB ERROR(%u) %s scsi cmd 0x%x - scsi status 0x%x\n",
  2501. le32_to_cpu(srbreply->srb_status) & 0x3F,
  2502. aac_get_status_string(
  2503. le32_to_cpu(srbreply->srb_status) & 0x3F),
  2504. scsicmd->cmnd[0],
  2505. le32_to_cpu(srbreply->scsi_status));
  2506. #endif
  2507. if ((scsicmd->cmnd[0] == ATA_12)
  2508. || (scsicmd->cmnd[0] == ATA_16)) {
  2509. if (scsicmd->cmnd[2] & (0x01 << 5)) {
  2510. scsicmd->result = DID_OK << 16
  2511. | COMMAND_COMPLETE << 8;
  2512. break;
  2513. } else {
  2514. scsicmd->result = DID_ERROR << 16
  2515. | COMMAND_COMPLETE << 8;
  2516. break;
  2517. }
  2518. } else {
  2519. scsicmd->result = DID_ERROR << 16
  2520. | COMMAND_COMPLETE << 8;
  2521. break;
  2522. }
  2523. }
  2524. if (le32_to_cpu(srbreply->scsi_status) == SAM_STAT_CHECK_CONDITION) {
  2525. int len;
  2526. scsicmd->result |= SAM_STAT_CHECK_CONDITION;
  2527. len = min_t(u32, le32_to_cpu(srbreply->sense_data_size),
  2528. SCSI_SENSE_BUFFERSIZE);
  2529. #ifdef AAC_DETAILED_STATUS_INFO
  2530. printk(KERN_WARNING "aac_srb_callback: check condition, status = %d len=%d\n",
  2531. le32_to_cpu(srbreply->status), len);
  2532. #endif
  2533. memcpy(scsicmd->sense_buffer, srbreply->sense_data, len);
  2534. }
  2535. /*
  2536. * OR in the scsi status (already shifted up a bit)
  2537. */
  2538. scsicmd->result |= le32_to_cpu(srbreply->scsi_status);
  2539. aac_fib_complete(fibptr);
  2540. aac_fib_free(fibptr);
  2541. scsicmd->scsi_done(scsicmd);
  2542. }
  2543. /**
  2544. *
  2545. * aac_send_scb_fib
  2546. * @scsicmd: the scsi command block
  2547. *
  2548. * This routine will form a FIB and fill in the aac_srb from the
  2549. * scsicmd passed in.
  2550. */
  2551. static int aac_send_srb_fib(struct scsi_cmnd* scsicmd)
  2552. {
  2553. struct fib* cmd_fibcontext;
  2554. struct aac_dev* dev;
  2555. int status;
  2556. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  2557. if (scmd_id(scsicmd) >= dev->maximum_num_physicals ||
  2558. scsicmd->device->lun > 7) {
  2559. scsicmd->result = DID_NO_CONNECT << 16;
  2560. scsicmd->scsi_done(scsicmd);
  2561. return 0;
  2562. }
  2563. /*
  2564. * Allocate and initialize a Fib then setup a BlockWrite command
  2565. */
  2566. if (!(cmd_fibcontext = aac_fib_alloc(dev))) {
  2567. return -1;
  2568. }
  2569. status = aac_adapter_scsi(cmd_fibcontext, scsicmd);
  2570. /*
  2571. * Check that the command queued to the controller
  2572. */
  2573. if (status == -EINPROGRESS) {
  2574. scsicmd->SCp.phase = AAC_OWNER_FIRMWARE;
  2575. return 0;
  2576. }
  2577. printk(KERN_WARNING "aac_srb: aac_fib_send failed with status: %d\n", status);
  2578. aac_fib_complete(cmd_fibcontext);
  2579. aac_fib_free(cmd_fibcontext);
  2580. return -1;
  2581. }
  2582. static long aac_build_sg(struct scsi_cmnd *scsicmd, struct sgmap *psg)
  2583. {
  2584. struct aac_dev *dev;
  2585. unsigned long byte_count = 0;
  2586. int nseg;
  2587. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  2588. // Get rid of old data
  2589. psg->count = 0;
  2590. psg->sg[0].addr = 0;
  2591. psg->sg[0].count = 0;
  2592. nseg = scsi_dma_map(scsicmd);
  2593. if (nseg < 0)
  2594. return nseg;
  2595. if (nseg) {
  2596. struct scatterlist *sg;
  2597. int i;
  2598. psg->count = cpu_to_le32(nseg);
  2599. scsi_for_each_sg(scsicmd, sg, nseg, i) {
  2600. psg->sg[i].addr = cpu_to_le32(sg_dma_address(sg));
  2601. psg->sg[i].count = cpu_to_le32(sg_dma_len(sg));
  2602. byte_count += sg_dma_len(sg);
  2603. }
  2604. /* hba wants the size to be exact */
  2605. if (byte_count > scsi_bufflen(scsicmd)) {
  2606. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2607. (byte_count - scsi_bufflen(scsicmd));
  2608. psg->sg[i-1].count = cpu_to_le32(temp);
  2609. byte_count = scsi_bufflen(scsicmd);
  2610. }
  2611. /* Check for command underflow */
  2612. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2613. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2614. byte_count, scsicmd->underflow);
  2615. }
  2616. }
  2617. return byte_count;
  2618. }
  2619. static long aac_build_sg64(struct scsi_cmnd *scsicmd, struct sgmap64 *psg)
  2620. {
  2621. struct aac_dev *dev;
  2622. unsigned long byte_count = 0;
  2623. u64 addr;
  2624. int nseg;
  2625. dev = (struct aac_dev *)scsicmd->device->host->hostdata;
  2626. // Get rid of old data
  2627. psg->count = 0;
  2628. psg->sg[0].addr[0] = 0;
  2629. psg->sg[0].addr[1] = 0;
  2630. psg->sg[0].count = 0;
  2631. nseg = scsi_dma_map(scsicmd);
  2632. if (nseg < 0)
  2633. return nseg;
  2634. if (nseg) {
  2635. struct scatterlist *sg;
  2636. int i;
  2637. scsi_for_each_sg(scsicmd, sg, nseg, i) {
  2638. int count = sg_dma_len(sg);
  2639. addr = sg_dma_address(sg);
  2640. psg->sg[i].addr[0] = cpu_to_le32(addr & 0xffffffff);
  2641. psg->sg[i].addr[1] = cpu_to_le32(addr>>32);
  2642. psg->sg[i].count = cpu_to_le32(count);
  2643. byte_count += count;
  2644. }
  2645. psg->count = cpu_to_le32(nseg);
  2646. /* hba wants the size to be exact */
  2647. if (byte_count > scsi_bufflen(scsicmd)) {
  2648. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2649. (byte_count - scsi_bufflen(scsicmd));
  2650. psg->sg[i-1].count = cpu_to_le32(temp);
  2651. byte_count = scsi_bufflen(scsicmd);
  2652. }
  2653. /* Check for command underflow */
  2654. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2655. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2656. byte_count, scsicmd->underflow);
  2657. }
  2658. }
  2659. return byte_count;
  2660. }
  2661. static long aac_build_sgraw(struct scsi_cmnd *scsicmd, struct sgmapraw *psg)
  2662. {
  2663. unsigned long byte_count = 0;
  2664. int nseg;
  2665. // Get rid of old data
  2666. psg->count = 0;
  2667. psg->sg[0].next = 0;
  2668. psg->sg[0].prev = 0;
  2669. psg->sg[0].addr[0] = 0;
  2670. psg->sg[0].addr[1] = 0;
  2671. psg->sg[0].count = 0;
  2672. psg->sg[0].flags = 0;
  2673. nseg = scsi_dma_map(scsicmd);
  2674. if (nseg < 0)
  2675. return nseg;
  2676. if (nseg) {
  2677. struct scatterlist *sg;
  2678. int i;
  2679. scsi_for_each_sg(scsicmd, sg, nseg, i) {
  2680. int count = sg_dma_len(sg);
  2681. u64 addr = sg_dma_address(sg);
  2682. psg->sg[i].next = 0;
  2683. psg->sg[i].prev = 0;
  2684. psg->sg[i].addr[1] = cpu_to_le32((u32)(addr>>32));
  2685. psg->sg[i].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff));
  2686. psg->sg[i].count = cpu_to_le32(count);
  2687. psg->sg[i].flags = 0;
  2688. byte_count += count;
  2689. }
  2690. psg->count = cpu_to_le32(nseg);
  2691. /* hba wants the size to be exact */
  2692. if (byte_count > scsi_bufflen(scsicmd)) {
  2693. u32 temp = le32_to_cpu(psg->sg[i-1].count) -
  2694. (byte_count - scsi_bufflen(scsicmd));
  2695. psg->sg[i-1].count = cpu_to_le32(temp);
  2696. byte_count = scsi_bufflen(scsicmd);
  2697. }
  2698. /* Check for command underflow */
  2699. if(scsicmd->underflow && (byte_count < scsicmd->underflow)){
  2700. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2701. byte_count, scsicmd->underflow);
  2702. }
  2703. }
  2704. return byte_count;
  2705. }
  2706. static long aac_build_sgraw2(struct scsi_cmnd *scsicmd,
  2707. struct aac_raw_io2 *rio2, int sg_max)
  2708. {
  2709. unsigned long byte_count = 0;
  2710. int nseg;
  2711. nseg = scsi_dma_map(scsicmd);
  2712. if (nseg < 0)
  2713. return nseg;
  2714. if (nseg) {
  2715. struct scatterlist *sg;
  2716. int i, conformable = 0;
  2717. u32 min_size = PAGE_SIZE, cur_size;
  2718. scsi_for_each_sg(scsicmd, sg, nseg, i) {
  2719. int count = sg_dma_len(sg);
  2720. u64 addr = sg_dma_address(sg);
  2721. BUG_ON(i >= sg_max);
  2722. rio2->sge[i].addrHigh = cpu_to_le32((u32)(addr>>32));
  2723. rio2->sge[i].addrLow = cpu_to_le32((u32)(addr & 0xffffffff));
  2724. cur_size = cpu_to_le32(count);
  2725. rio2->sge[i].length = cur_size;
  2726. rio2->sge[i].flags = 0;
  2727. if (i == 0) {
  2728. conformable = 1;
  2729. rio2->sgeFirstSize = cur_size;
  2730. } else if (i == 1) {
  2731. rio2->sgeNominalSize = cur_size;
  2732. min_size = cur_size;
  2733. } else if ((i+1) < nseg && cur_size != rio2->sgeNominalSize) {
  2734. conformable = 0;
  2735. if (cur_size < min_size)
  2736. min_size = cur_size;
  2737. }
  2738. byte_count += count;
  2739. }
  2740. /* hba wants the size to be exact */
  2741. if (byte_count > scsi_bufflen(scsicmd)) {
  2742. u32 temp = le32_to_cpu(rio2->sge[i-1].length) -
  2743. (byte_count - scsi_bufflen(scsicmd));
  2744. rio2->sge[i-1].length = cpu_to_le32(temp);
  2745. byte_count = scsi_bufflen(scsicmd);
  2746. }
  2747. rio2->sgeCnt = cpu_to_le32(nseg);
  2748. rio2->flags |= cpu_to_le16(RIO2_SG_FORMAT_IEEE1212);
  2749. /* not conformable: evaluate required sg elements */
  2750. if (!conformable) {
  2751. int j, nseg_new = nseg, err_found;
  2752. for (i = min_size / PAGE_SIZE; i >= 1; --i) {
  2753. err_found = 0;
  2754. nseg_new = 2;
  2755. for (j = 1; j < nseg - 1; ++j) {
  2756. if (rio2->sge[j].length % (i*PAGE_SIZE)) {
  2757. err_found = 1;
  2758. break;
  2759. }
  2760. nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
  2761. }
  2762. if (!err_found)
  2763. break;
  2764. }
  2765. if (i > 0 && nseg_new <= sg_max)
  2766. aac_convert_sgraw2(rio2, i, nseg, nseg_new);
  2767. } else
  2768. rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
  2769. /* Check for command underflow */
  2770. if (scsicmd->underflow && (byte_count < scsicmd->underflow)) {
  2771. printk(KERN_WARNING"aacraid: cmd len %08lX cmd underflow %08X\n",
  2772. byte_count, scsicmd->underflow);
  2773. }
  2774. }
  2775. return byte_count;
  2776. }
  2777. static int aac_convert_sgraw2(struct aac_raw_io2 *rio2, int pages, int nseg, int nseg_new)
  2778. {
  2779. struct sge_ieee1212 *sge;
  2780. int i, j, pos;
  2781. u32 addr_low;
  2782. if (aac_convert_sgl == 0)
  2783. return 0;
  2784. sge = kmalloc(nseg_new * sizeof(struct sge_ieee1212), GFP_ATOMIC);
  2785. if (sge == NULL)
  2786. return -1;
  2787. for (i = 1, pos = 1; i < nseg-1; ++i) {
  2788. for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
  2789. addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
  2790. sge[pos].addrLow = addr_low;
  2791. sge[pos].addrHigh = rio2->sge[i].addrHigh;
  2792. if (addr_low < rio2->sge[i].addrLow)
  2793. sge[pos].addrHigh++;
  2794. sge[pos].length = pages * PAGE_SIZE;
  2795. sge[pos].flags = 0;
  2796. pos++;
  2797. }
  2798. }
  2799. sge[pos] = rio2->sge[nseg-1];
  2800. memcpy(&rio2->sge[1], &sge[1], (nseg_new-1)*sizeof(struct sge_ieee1212));
  2801. kfree(sge);
  2802. rio2->sgeCnt = cpu_to_le32(nseg_new);
  2803. rio2->flags |= cpu_to_le16(RIO2_SGL_CONFORMANT);
  2804. rio2->sgeNominalSize = pages * PAGE_SIZE;
  2805. return 0;
  2806. }
  2807. #ifdef AAC_DETAILED_STATUS_INFO
  2808. struct aac_srb_status_info {
  2809. u32 status;
  2810. char *str;
  2811. };
  2812. static struct aac_srb_status_info srb_status_info[] = {
  2813. { SRB_STATUS_PENDING, "Pending Status"},
  2814. { SRB_STATUS_SUCCESS, "Success"},
  2815. { SRB_STATUS_ABORTED, "Aborted Command"},
  2816. { SRB_STATUS_ABORT_FAILED, "Abort Failed"},
  2817. { SRB_STATUS_ERROR, "Error Event"},
  2818. { SRB_STATUS_BUSY, "Device Busy"},
  2819. { SRB_STATUS_INVALID_REQUEST, "Invalid Request"},
  2820. { SRB_STATUS_INVALID_PATH_ID, "Invalid Path ID"},
  2821. { SRB_STATUS_NO_DEVICE, "No Device"},
  2822. { SRB_STATUS_TIMEOUT, "Timeout"},
  2823. { SRB_STATUS_SELECTION_TIMEOUT, "Selection Timeout"},
  2824. { SRB_STATUS_COMMAND_TIMEOUT, "Command Timeout"},
  2825. { SRB_STATUS_MESSAGE_REJECTED, "Message Rejected"},
  2826. { SRB_STATUS_BUS_RESET, "Bus Reset"},
  2827. { SRB_STATUS_PARITY_ERROR, "Parity Error"},
  2828. { SRB_STATUS_REQUEST_SENSE_FAILED,"Request Sense Failed"},
  2829. { SRB_STATUS_NO_HBA, "No HBA"},
  2830. { SRB_STATUS_DATA_OVERRUN, "Data Overrun/Data Underrun"},
  2831. { SRB_STATUS_UNEXPECTED_BUS_FREE,"Unexpected Bus Free"},
  2832. { SRB_STATUS_PHASE_SEQUENCE_FAILURE,"Phase Error"},
  2833. { SRB_STATUS_BAD_SRB_BLOCK_LENGTH,"Bad Srb Block Length"},
  2834. { SRB_STATUS_REQUEST_FLUSHED, "Request Flushed"},
  2835. { SRB_STATUS_DELAYED_RETRY, "Delayed Retry"},
  2836. { SRB_STATUS_INVALID_LUN, "Invalid LUN"},
  2837. { SRB_STATUS_INVALID_TARGET_ID, "Invalid TARGET ID"},
  2838. { SRB_STATUS_BAD_FUNCTION, "Bad Function"},
  2839. { SRB_STATUS_ERROR_RECOVERY, "Error Recovery"},
  2840. { SRB_STATUS_NOT_STARTED, "Not Started"},
  2841. { SRB_STATUS_NOT_IN_USE, "Not In Use"},
  2842. { SRB_STATUS_FORCE_ABORT, "Force Abort"},
  2843. { SRB_STATUS_DOMAIN_VALIDATION_FAIL,"Domain Validation Failure"},
  2844. { 0xff, "Unknown Error"}
  2845. };
  2846. char *aac_get_status_string(u32 status)
  2847. {
  2848. int i;
  2849. for (i = 0; i < ARRAY_SIZE(srb_status_info); i++)
  2850. if (srb_status_info[i].status == status)
  2851. return srb_status_info[i].str;
  2852. return "Bad Status Code";
  2853. }
  2854. #endif