libata-scsi.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * libata-scsi.c - helper library for ATA
  4. *
  5. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  6. * Copyright 2003-2004 Jeff Garzik
  7. *
  8. * libata documentation is available via 'make {ps|pdf}docs',
  9. * as Documentation/driver-api/libata.rst
  10. *
  11. * Hardware documentation available from
  12. * - http://www.t10.org/
  13. * - http://www.t13.org/
  14. */
  15. #include <linux/compat.h>
  16. #include <linux/slab.h>
  17. #include <linux/kernel.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/export.h>
  21. #include <scsi/scsi.h>
  22. #include <scsi/scsi_host.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_eh.h>
  25. #include <scsi/scsi_device.h>
  26. #include <scsi/scsi_tcq.h>
  27. #include <scsi/scsi_transport.h>
  28. #include <linux/libata.h>
  29. #include <linux/hdreg.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/suspend.h>
  32. #include <asm/unaligned.h>
  33. #include <linux/ioprio.h>
  34. #include <linux/of.h>
  35. #include "libata.h"
  36. #include "libata-transport.h"
  37. #define ATA_SCSI_RBUF_SIZE 576
  38. static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
  39. static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
  40. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
  41. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  42. const struct scsi_device *scsidev);
  43. #define RW_RECOVERY_MPAGE 0x1
  44. #define RW_RECOVERY_MPAGE_LEN 12
  45. #define CACHE_MPAGE 0x8
  46. #define CACHE_MPAGE_LEN 20
  47. #define CONTROL_MPAGE 0xa
  48. #define CONTROL_MPAGE_LEN 12
  49. #define ALL_MPAGES 0x3f
  50. #define ALL_SUB_MPAGES 0xff
  51. static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
  52. RW_RECOVERY_MPAGE,
  53. RW_RECOVERY_MPAGE_LEN - 2,
  54. (1 << 7), /* AWRE */
  55. 0, /* read retry count */
  56. 0, 0, 0, 0,
  57. 0, /* write retry count */
  58. 0, 0, 0
  59. };
  60. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  61. CACHE_MPAGE,
  62. CACHE_MPAGE_LEN - 2,
  63. 0, /* contains WCE, needs to be 0 for logic */
  64. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  65. 0, /* contains DRA, needs to be 0 for logic */
  66. 0, 0, 0, 0, 0, 0, 0
  67. };
  68. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  69. CONTROL_MPAGE,
  70. CONTROL_MPAGE_LEN - 2,
  71. 2, /* DSENSE=0, GLTSD=1 */
  72. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  73. 0, 0, 0, 0, 0xff, 0xff,
  74. 0, 30 /* extended self test time, see 05-359r1 */
  75. };
  76. static ssize_t ata_scsi_park_show(struct device *device,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct scsi_device *sdev = to_scsi_device(device);
  80. struct ata_port *ap;
  81. struct ata_link *link;
  82. struct ata_device *dev;
  83. unsigned long now;
  84. unsigned int msecs;
  85. int rc = 0;
  86. ap = ata_shost_to_port(sdev->host);
  87. spin_lock_irq(ap->lock);
  88. dev = ata_scsi_find_dev(ap, sdev);
  89. if (!dev) {
  90. rc = -ENODEV;
  91. goto unlock;
  92. }
  93. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  94. rc = -EOPNOTSUPP;
  95. goto unlock;
  96. }
  97. link = dev->link;
  98. now = jiffies;
  99. if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
  100. link->eh_context.unloaded_mask & (1 << dev->devno) &&
  101. time_after(dev->unpark_deadline, now))
  102. msecs = jiffies_to_msecs(dev->unpark_deadline - now);
  103. else
  104. msecs = 0;
  105. unlock:
  106. spin_unlock_irq(ap->lock);
  107. return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
  108. }
  109. static ssize_t ata_scsi_park_store(struct device *device,
  110. struct device_attribute *attr,
  111. const char *buf, size_t len)
  112. {
  113. struct scsi_device *sdev = to_scsi_device(device);
  114. struct ata_port *ap;
  115. struct ata_device *dev;
  116. long int input;
  117. unsigned long flags;
  118. int rc;
  119. rc = kstrtol(buf, 10, &input);
  120. if (rc)
  121. return rc;
  122. if (input < -2)
  123. return -EINVAL;
  124. if (input > ATA_TMOUT_MAX_PARK) {
  125. rc = -EOVERFLOW;
  126. input = ATA_TMOUT_MAX_PARK;
  127. }
  128. ap = ata_shost_to_port(sdev->host);
  129. spin_lock_irqsave(ap->lock, flags);
  130. dev = ata_scsi_find_dev(ap, sdev);
  131. if (unlikely(!dev)) {
  132. rc = -ENODEV;
  133. goto unlock;
  134. }
  135. if (dev->class != ATA_DEV_ATA &&
  136. dev->class != ATA_DEV_ZAC) {
  137. rc = -EOPNOTSUPP;
  138. goto unlock;
  139. }
  140. if (input >= 0) {
  141. if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
  142. rc = -EOPNOTSUPP;
  143. goto unlock;
  144. }
  145. dev->unpark_deadline = ata_deadline(jiffies, input);
  146. dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
  147. ata_port_schedule_eh(ap);
  148. complete(&ap->park_req_pending);
  149. } else {
  150. switch (input) {
  151. case -1:
  152. dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
  153. break;
  154. case -2:
  155. dev->flags |= ATA_DFLAG_NO_UNLOAD;
  156. break;
  157. }
  158. }
  159. unlock:
  160. spin_unlock_irqrestore(ap->lock, flags);
  161. return rc ? rc : len;
  162. }
  163. DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
  164. ata_scsi_park_show, ata_scsi_park_store);
  165. EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
  166. void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
  167. u8 sk, u8 asc, u8 ascq)
  168. {
  169. bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
  170. if (!cmd)
  171. return;
  172. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  173. scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
  174. }
  175. void ata_scsi_set_sense_information(struct ata_device *dev,
  176. struct scsi_cmnd *cmd,
  177. const struct ata_taskfile *tf)
  178. {
  179. u64 information;
  180. if (!cmd)
  181. return;
  182. information = ata_tf_read_block(tf, dev);
  183. if (information == U64_MAX)
  184. return;
  185. scsi_set_sense_information(cmd->sense_buffer,
  186. SCSI_SENSE_BUFFERSIZE, information);
  187. }
  188. static void ata_scsi_set_invalid_field(struct ata_device *dev,
  189. struct scsi_cmnd *cmd, u16 field, u8 bit)
  190. {
  191. ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  192. /* "Invalid field in CDB" */
  193. scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  194. field, bit, 1);
  195. }
  196. static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
  197. struct scsi_cmnd *cmd, u16 field)
  198. {
  199. /* "Invalid field in parameter list" */
  200. ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
  201. scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
  202. field, 0xff, 0);
  203. }
  204. struct device_attribute *ata_common_sdev_attrs[] = {
  205. &dev_attr_unload_heads,
  206. NULL
  207. };
  208. EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
  209. /**
  210. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  211. * @sdev: SCSI device for which BIOS geometry is to be determined
  212. * @bdev: block device associated with @sdev
  213. * @capacity: capacity of SCSI device
  214. * @geom: location to which geometry will be output
  215. *
  216. * Generic bios head/sector/cylinder calculator
  217. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  218. * mapping. Some situations may arise where the disk is not
  219. * bootable if this is not used.
  220. *
  221. * LOCKING:
  222. * Defined by the SCSI layer. We don't really care.
  223. *
  224. * RETURNS:
  225. * Zero.
  226. */
  227. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  228. sector_t capacity, int geom[])
  229. {
  230. geom[0] = 255;
  231. geom[1] = 63;
  232. sector_div(capacity, 255*63);
  233. geom[2] = capacity;
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(ata_std_bios_param);
  237. /**
  238. * ata_scsi_unlock_native_capacity - unlock native capacity
  239. * @sdev: SCSI device to adjust device capacity for
  240. *
  241. * This function is called if a partition on @sdev extends beyond
  242. * the end of the device. It requests EH to unlock HPA.
  243. *
  244. * LOCKING:
  245. * Defined by the SCSI layer. Might sleep.
  246. */
  247. void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
  248. {
  249. struct ata_port *ap = ata_shost_to_port(sdev->host);
  250. struct ata_device *dev;
  251. unsigned long flags;
  252. spin_lock_irqsave(ap->lock, flags);
  253. dev = ata_scsi_find_dev(ap, sdev);
  254. if (dev && dev->n_sectors < dev->n_native_sectors) {
  255. dev->flags |= ATA_DFLAG_UNLOCK_HPA;
  256. dev->link->eh_info.action |= ATA_EH_RESET;
  257. ata_port_schedule_eh(ap);
  258. }
  259. spin_unlock_irqrestore(ap->lock, flags);
  260. ata_port_wait_eh(ap);
  261. }
  262. EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
  263. /**
  264. * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
  265. * @ap: target port
  266. * @sdev: SCSI device to get identify data for
  267. * @arg: User buffer area for identify data
  268. *
  269. * LOCKING:
  270. * Defined by the SCSI layer. We don't really care.
  271. *
  272. * RETURNS:
  273. * Zero on success, negative errno on error.
  274. */
  275. static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
  276. void __user *arg)
  277. {
  278. struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
  279. u16 __user *dst = arg;
  280. char buf[40];
  281. if (!dev)
  282. return -ENOMSG;
  283. if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
  284. return -EFAULT;
  285. ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
  286. if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
  287. return -EFAULT;
  288. ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
  289. if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
  290. return -EFAULT;
  291. ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  292. if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
  293. return -EFAULT;
  294. return 0;
  295. }
  296. /**
  297. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  298. * @scsidev: Device to which we are issuing command
  299. * @arg: User provided data for issuing command
  300. *
  301. * LOCKING:
  302. * Defined by the SCSI layer. We don't really care.
  303. *
  304. * RETURNS:
  305. * Zero on success, negative errno on error.
  306. */
  307. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  308. {
  309. int rc = 0;
  310. u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
  311. u8 scsi_cmd[MAX_COMMAND_SIZE];
  312. u8 args[4], *argbuf = NULL;
  313. int argsize = 0;
  314. enum dma_data_direction data_dir;
  315. struct scsi_sense_hdr sshdr;
  316. int cmd_result;
  317. if (arg == NULL)
  318. return -EINVAL;
  319. if (copy_from_user(args, arg, sizeof(args)))
  320. return -EFAULT;
  321. memset(sensebuf, 0, sizeof(sensebuf));
  322. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  323. if (args[3]) {
  324. argsize = ATA_SECT_SIZE * args[3];
  325. argbuf = kmalloc(argsize, GFP_KERNEL);
  326. if (argbuf == NULL) {
  327. rc = -ENOMEM;
  328. goto error;
  329. }
  330. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  331. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  332. block count in sector count field */
  333. data_dir = DMA_FROM_DEVICE;
  334. } else {
  335. scsi_cmd[1] = (3 << 1); /* Non-data */
  336. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  337. data_dir = DMA_NONE;
  338. }
  339. scsi_cmd[0] = ATA_16;
  340. scsi_cmd[4] = args[2];
  341. if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
  342. scsi_cmd[6] = args[3];
  343. scsi_cmd[8] = args[1];
  344. scsi_cmd[10] = ATA_SMART_LBAM_PASS;
  345. scsi_cmd[12] = ATA_SMART_LBAH_PASS;
  346. } else {
  347. scsi_cmd[6] = args[1];
  348. }
  349. scsi_cmd[14] = args[0];
  350. /* Good values for timeout and retries? Values below
  351. from scsi_ioctl_send_command() for default case... */
  352. cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
  353. sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
  354. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  355. u8 *desc = sensebuf + 8;
  356. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  357. /* If we set cc then ATA pass-through will cause a
  358. * check condition even if no error. Filter that. */
  359. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  360. if (sshdr.sense_key == RECOVERED_ERROR &&
  361. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  362. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  363. }
  364. /* Send userspace a few ATA registers (same as drivers/ide) */
  365. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  366. desc[0] == 0x09) { /* code is "ATA Descriptor" */
  367. args[0] = desc[13]; /* status */
  368. args[1] = desc[3]; /* error */
  369. args[2] = desc[5]; /* sector count (0:7) */
  370. if (copy_to_user(arg, args, sizeof(args)))
  371. rc = -EFAULT;
  372. }
  373. }
  374. if (cmd_result) {
  375. rc = -EIO;
  376. goto error;
  377. }
  378. if ((argbuf)
  379. && copy_to_user(arg + sizeof(args), argbuf, argsize))
  380. rc = -EFAULT;
  381. error:
  382. kfree(argbuf);
  383. return rc;
  384. }
  385. /**
  386. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  387. * @scsidev: Device to which we are issuing command
  388. * @arg: User provided data for issuing command
  389. *
  390. * LOCKING:
  391. * Defined by the SCSI layer. We don't really care.
  392. *
  393. * RETURNS:
  394. * Zero on success, negative errno on error.
  395. */
  396. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  397. {
  398. int rc = 0;
  399. u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
  400. u8 scsi_cmd[MAX_COMMAND_SIZE];
  401. u8 args[7];
  402. struct scsi_sense_hdr sshdr;
  403. int cmd_result;
  404. if (arg == NULL)
  405. return -EINVAL;
  406. if (copy_from_user(args, arg, sizeof(args)))
  407. return -EFAULT;
  408. memset(sensebuf, 0, sizeof(sensebuf));
  409. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  410. scsi_cmd[0] = ATA_16;
  411. scsi_cmd[1] = (3 << 1); /* Non-data */
  412. scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
  413. scsi_cmd[4] = args[1];
  414. scsi_cmd[6] = args[2];
  415. scsi_cmd[8] = args[3];
  416. scsi_cmd[10] = args[4];
  417. scsi_cmd[12] = args[5];
  418. scsi_cmd[13] = args[6] & 0x4f;
  419. scsi_cmd[14] = args[0];
  420. /* Good values for timeout and retries? Values below
  421. from scsi_ioctl_send_command() for default case... */
  422. cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
  423. sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
  424. if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
  425. u8 *desc = sensebuf + 8;
  426. cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
  427. /* If we set cc then ATA pass-through will cause a
  428. * check condition even if no error. Filter that. */
  429. if (cmd_result & SAM_STAT_CHECK_CONDITION) {
  430. if (sshdr.sense_key == RECOVERED_ERROR &&
  431. sshdr.asc == 0 && sshdr.ascq == 0x1d)
  432. cmd_result &= ~SAM_STAT_CHECK_CONDITION;
  433. }
  434. /* Send userspace ATA registers */
  435. if (sensebuf[0] == 0x72 && /* format is "descriptor" */
  436. desc[0] == 0x09) {/* code is "ATA Descriptor" */
  437. args[0] = desc[13]; /* status */
  438. args[1] = desc[3]; /* error */
  439. args[2] = desc[5]; /* sector count (0:7) */
  440. args[3] = desc[7]; /* lbal */
  441. args[4] = desc[9]; /* lbam */
  442. args[5] = desc[11]; /* lbah */
  443. args[6] = desc[12]; /* select */
  444. if (copy_to_user(arg, args, sizeof(args)))
  445. rc = -EFAULT;
  446. }
  447. }
  448. if (cmd_result) {
  449. rc = -EIO;
  450. goto error;
  451. }
  452. error:
  453. return rc;
  454. }
  455. static int ata_ioc32(struct ata_port *ap)
  456. {
  457. if (ap->flags & ATA_FLAG_PIO_DMA)
  458. return 1;
  459. if (ap->pflags & ATA_PFLAG_PIO32)
  460. return 1;
  461. return 0;
  462. }
  463. /*
  464. * This handles both native and compat commands, so anything added
  465. * here must have a compatible argument, or check in_compat_syscall()
  466. */
  467. int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
  468. unsigned int cmd, void __user *arg)
  469. {
  470. unsigned long val;
  471. int rc = -EINVAL;
  472. unsigned long flags;
  473. switch (cmd) {
  474. case HDIO_GET_32BIT:
  475. spin_lock_irqsave(ap->lock, flags);
  476. val = ata_ioc32(ap);
  477. spin_unlock_irqrestore(ap->lock, flags);
  478. #ifdef CONFIG_COMPAT
  479. if (in_compat_syscall())
  480. return put_user(val, (compat_ulong_t __user *)arg);
  481. #endif
  482. return put_user(val, (unsigned long __user *)arg);
  483. case HDIO_SET_32BIT:
  484. val = (unsigned long) arg;
  485. rc = 0;
  486. spin_lock_irqsave(ap->lock, flags);
  487. if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
  488. if (val)
  489. ap->pflags |= ATA_PFLAG_PIO32;
  490. else
  491. ap->pflags &= ~ATA_PFLAG_PIO32;
  492. } else {
  493. if (val != ata_ioc32(ap))
  494. rc = -EINVAL;
  495. }
  496. spin_unlock_irqrestore(ap->lock, flags);
  497. return rc;
  498. case HDIO_GET_IDENTITY:
  499. return ata_get_identity(ap, scsidev, arg);
  500. case HDIO_DRIVE_CMD:
  501. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  502. return -EACCES;
  503. return ata_cmd_ioctl(scsidev, arg);
  504. case HDIO_DRIVE_TASK:
  505. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  506. return -EACCES;
  507. return ata_task_ioctl(scsidev, arg);
  508. default:
  509. rc = -ENOTTY;
  510. break;
  511. }
  512. return rc;
  513. }
  514. EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
  515. int ata_scsi_ioctl(struct scsi_device *scsidev, unsigned int cmd,
  516. void __user *arg)
  517. {
  518. return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
  519. scsidev, cmd, arg);
  520. }
  521. EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
  522. /**
  523. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  524. * @dev: ATA device to which the new command is attached
  525. * @cmd: SCSI command that originated this ATA command
  526. *
  527. * Obtain a reference to an unused ata_queued_cmd structure,
  528. * which is the basic libata structure representing a single
  529. * ATA command sent to the hardware.
  530. *
  531. * If a command was available, fill in the SCSI-specific
  532. * portions of the structure with information on the
  533. * current command.
  534. *
  535. * LOCKING:
  536. * spin_lock_irqsave(host lock)
  537. *
  538. * RETURNS:
  539. * Command allocated, or %NULL if none available.
  540. */
  541. static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
  542. struct scsi_cmnd *cmd)
  543. {
  544. struct ata_queued_cmd *qc;
  545. qc = ata_qc_new_init(dev, cmd->request->tag);
  546. if (qc) {
  547. qc->scsicmd = cmd;
  548. qc->scsidone = cmd->scsi_done;
  549. qc->sg = scsi_sglist(cmd);
  550. qc->n_elem = scsi_sg_count(cmd);
  551. if (cmd->request->rq_flags & RQF_QUIET)
  552. qc->flags |= ATA_QCFLAG_QUIET;
  553. } else {
  554. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  555. cmd->scsi_done(cmd);
  556. }
  557. return qc;
  558. }
  559. static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
  560. {
  561. struct scsi_cmnd *scmd = qc->scsicmd;
  562. qc->extrabytes = scmd->extra_len;
  563. qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
  564. }
  565. /**
  566. * ata_dump_status - user friendly display of error info
  567. * @id: id of the port in question
  568. * @tf: ptr to filled out taskfile
  569. *
  570. * Decode and dump the ATA error/status registers for the user so
  571. * that they have some idea what really happened at the non
  572. * make-believe layer.
  573. *
  574. * LOCKING:
  575. * inherited from caller
  576. */
  577. static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  578. {
  579. u8 stat = tf->command, err = tf->feature;
  580. pr_warn("ata%u: status=0x%02x { ", id, stat);
  581. if (stat & ATA_BUSY) {
  582. pr_cont("Busy }\n"); /* Data is not valid in this case */
  583. } else {
  584. if (stat & ATA_DRDY) pr_cont("DriveReady ");
  585. if (stat & ATA_DF) pr_cont("DeviceFault ");
  586. if (stat & ATA_DSC) pr_cont("SeekComplete ");
  587. if (stat & ATA_DRQ) pr_cont("DataRequest ");
  588. if (stat & ATA_CORR) pr_cont("CorrectedError ");
  589. if (stat & ATA_SENSE) pr_cont("Sense ");
  590. if (stat & ATA_ERR) pr_cont("Error ");
  591. pr_cont("}\n");
  592. if (err) {
  593. pr_warn("ata%u: error=0x%02x { ", id, err);
  594. if (err & ATA_ABORTED) pr_cont("DriveStatusError ");
  595. if (err & ATA_ICRC) {
  596. if (err & ATA_ABORTED)
  597. pr_cont("BadCRC ");
  598. else pr_cont("Sector ");
  599. }
  600. if (err & ATA_UNC) pr_cont("UncorrectableError ");
  601. if (err & ATA_IDNF) pr_cont("SectorIdNotFound ");
  602. if (err & ATA_TRK0NF) pr_cont("TrackZeroNotFound ");
  603. if (err & ATA_AMNF) pr_cont("AddrMarkNotFound ");
  604. pr_cont("}\n");
  605. }
  606. }
  607. }
  608. /**
  609. * ata_to_sense_error - convert ATA error to SCSI error
  610. * @id: ATA device number
  611. * @drv_stat: value contained in ATA status register
  612. * @drv_err: value contained in ATA error register
  613. * @sk: the sense key we'll fill out
  614. * @asc: the additional sense code we'll fill out
  615. * @ascq: the additional sense code qualifier we'll fill out
  616. * @verbose: be verbose
  617. *
  618. * Converts an ATA error into a SCSI error. Fill out pointers to
  619. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  620. * format sense blocks.
  621. *
  622. * LOCKING:
  623. * spin_lock_irqsave(host lock)
  624. */
  625. static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
  626. u8 *asc, u8 *ascq, int verbose)
  627. {
  628. int i;
  629. /* Based on the 3ware driver translation table */
  630. static const unsigned char sense_table[][4] = {
  631. /* BBD|ECC|ID|MAR */
  632. {0xd1, ABORTED_COMMAND, 0x00, 0x00},
  633. // Device busy Aborted command
  634. /* BBD|ECC|ID */
  635. {0xd0, ABORTED_COMMAND, 0x00, 0x00},
  636. // Device busy Aborted command
  637. /* ECC|MC|MARK */
  638. {0x61, HARDWARE_ERROR, 0x00, 0x00},
  639. // Device fault Hardware error
  640. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  641. {0x84, ABORTED_COMMAND, 0x47, 0x00},
  642. // Data CRC error SCSI parity error
  643. /* MC|ID|ABRT|TRK0|MARK */
  644. {0x37, NOT_READY, 0x04, 0x00},
  645. // Unit offline Not ready
  646. /* MCR|MARK */
  647. {0x09, NOT_READY, 0x04, 0x00},
  648. // Unrecovered disk error Not ready
  649. /* Bad address mark */
  650. {0x01, MEDIUM_ERROR, 0x13, 0x00},
  651. // Address mark not found for data field
  652. /* TRK0 - Track 0 not found */
  653. {0x02, HARDWARE_ERROR, 0x00, 0x00},
  654. // Hardware error
  655. /* Abort: 0x04 is not translated here, see below */
  656. /* Media change request */
  657. {0x08, NOT_READY, 0x04, 0x00},
  658. // FIXME: faking offline
  659. /* SRV/IDNF - ID not found */
  660. {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
  661. // Logical address out of range
  662. /* MC - Media Changed */
  663. {0x20, UNIT_ATTENTION, 0x28, 0x00},
  664. // Not ready to ready change, medium may have changed
  665. /* ECC - Uncorrectable ECC error */
  666. {0x40, MEDIUM_ERROR, 0x11, 0x04},
  667. // Unrecovered read error
  668. /* BBD - block marked bad */
  669. {0x80, MEDIUM_ERROR, 0x11, 0x04},
  670. // Block marked bad Medium error, unrecovered read error
  671. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  672. };
  673. static const unsigned char stat_table[][4] = {
  674. /* Must be first because BUSY means no other bits valid */
  675. {0x80, ABORTED_COMMAND, 0x47, 0x00},
  676. // Busy, fake parity for now
  677. {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
  678. // Device ready, unaligned write command
  679. {0x20, HARDWARE_ERROR, 0x44, 0x00},
  680. // Device fault, internal target failure
  681. {0x08, ABORTED_COMMAND, 0x47, 0x00},
  682. // Timed out in xfer, fake parity for now
  683. {0x04, RECOVERED_ERROR, 0x11, 0x00},
  684. // Recovered ECC error Medium error, recovered
  685. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  686. };
  687. /*
  688. * Is this an error we can process/parse
  689. */
  690. if (drv_stat & ATA_BUSY) {
  691. drv_err = 0; /* Ignore the err bits, they're invalid */
  692. }
  693. if (drv_err) {
  694. /* Look for drv_err */
  695. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  696. /* Look for best matches first */
  697. if ((sense_table[i][0] & drv_err) ==
  698. sense_table[i][0]) {
  699. *sk = sense_table[i][1];
  700. *asc = sense_table[i][2];
  701. *ascq = sense_table[i][3];
  702. goto translate_done;
  703. }
  704. }
  705. }
  706. /*
  707. * Fall back to interpreting status bits. Note that if the drv_err
  708. * has only the ABRT bit set, we decode drv_stat. ABRT by itself
  709. * is not descriptive enough.
  710. */
  711. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  712. if (stat_table[i][0] & drv_stat) {
  713. *sk = stat_table[i][1];
  714. *asc = stat_table[i][2];
  715. *ascq = stat_table[i][3];
  716. goto translate_done;
  717. }
  718. }
  719. /*
  720. * We need a sensible error return here, which is tricky, and one
  721. * that won't cause people to do things like return a disk wrongly.
  722. */
  723. *sk = ABORTED_COMMAND;
  724. *asc = 0x00;
  725. *ascq = 0x00;
  726. translate_done:
  727. if (verbose)
  728. pr_err("ata%u: translated ATA stat/err 0x%02x/%02x to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
  729. id, drv_stat, drv_err, *sk, *asc, *ascq);
  730. return;
  731. }
  732. /*
  733. * ata_gen_passthru_sense - Generate check condition sense block.
  734. * @qc: Command that completed.
  735. *
  736. * This function is specific to the ATA descriptor format sense
  737. * block specified for the ATA pass through commands. Regardless
  738. * of whether the command errored or not, return a sense
  739. * block. Copy all controller registers into the sense
  740. * block. If there was no error, we get the request from an ATA
  741. * passthrough command, so we use the following sense data:
  742. * sk = RECOVERED ERROR
  743. * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
  744. *
  745. *
  746. * LOCKING:
  747. * None.
  748. */
  749. static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
  750. {
  751. struct scsi_cmnd *cmd = qc->scsicmd;
  752. struct ata_taskfile *tf = &qc->result_tf;
  753. unsigned char *sb = cmd->sense_buffer;
  754. unsigned char *desc = sb + 8;
  755. int verbose = qc->ap->ops->error_handler == NULL;
  756. u8 sense_key, asc, ascq;
  757. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  758. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  759. /*
  760. * Use ata_to_sense_error() to map status register bits
  761. * onto sense key, asc & ascq.
  762. */
  763. if (qc->err_mask ||
  764. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  765. ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
  766. &sense_key, &asc, &ascq, verbose);
  767. ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
  768. } else {
  769. /*
  770. * ATA PASS-THROUGH INFORMATION AVAILABLE
  771. * Always in descriptor format sense.
  772. */
  773. scsi_build_sense_buffer(1, cmd->sense_buffer,
  774. RECOVERED_ERROR, 0, 0x1D);
  775. }
  776. if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
  777. u8 len;
  778. /* descriptor format */
  779. len = sb[7];
  780. desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
  781. if (!desc) {
  782. if (SCSI_SENSE_BUFFERSIZE < len + 14)
  783. return;
  784. sb[7] = len + 14;
  785. desc = sb + 8 + len;
  786. }
  787. desc[0] = 9;
  788. desc[1] = 12;
  789. /*
  790. * Copy registers into sense buffer.
  791. */
  792. desc[2] = 0x00;
  793. desc[3] = tf->feature; /* == error reg */
  794. desc[5] = tf->nsect;
  795. desc[7] = tf->lbal;
  796. desc[9] = tf->lbam;
  797. desc[11] = tf->lbah;
  798. desc[12] = tf->device;
  799. desc[13] = tf->command; /* == status reg */
  800. /*
  801. * Fill in Extend bit, and the high order bytes
  802. * if applicable.
  803. */
  804. if (tf->flags & ATA_TFLAG_LBA48) {
  805. desc[2] |= 0x01;
  806. desc[4] = tf->hob_nsect;
  807. desc[6] = tf->hob_lbal;
  808. desc[8] = tf->hob_lbam;
  809. desc[10] = tf->hob_lbah;
  810. }
  811. } else {
  812. /* Fixed sense format */
  813. desc[0] = tf->feature;
  814. desc[1] = tf->command; /* status */
  815. desc[2] = tf->device;
  816. desc[3] = tf->nsect;
  817. desc[7] = 0;
  818. if (tf->flags & ATA_TFLAG_LBA48) {
  819. desc[8] |= 0x80;
  820. if (tf->hob_nsect)
  821. desc[8] |= 0x40;
  822. if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
  823. desc[8] |= 0x20;
  824. }
  825. desc[9] = tf->lbal;
  826. desc[10] = tf->lbam;
  827. desc[11] = tf->lbah;
  828. }
  829. }
  830. /**
  831. * ata_gen_ata_sense - generate a SCSI fixed sense block
  832. * @qc: Command that we are erroring out
  833. *
  834. * Generate sense block for a failed ATA command @qc. Descriptor
  835. * format is used to accommodate LBA48 block address.
  836. *
  837. * LOCKING:
  838. * None.
  839. */
  840. static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
  841. {
  842. struct ata_device *dev = qc->dev;
  843. struct scsi_cmnd *cmd = qc->scsicmd;
  844. struct ata_taskfile *tf = &qc->result_tf;
  845. unsigned char *sb = cmd->sense_buffer;
  846. int verbose = qc->ap->ops->error_handler == NULL;
  847. u64 block;
  848. u8 sense_key, asc, ascq;
  849. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  850. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  851. if (ata_dev_disabled(dev)) {
  852. /* Device disabled after error recovery */
  853. /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
  854. ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
  855. return;
  856. }
  857. /* Use ata_to_sense_error() to map status register bits
  858. * onto sense key, asc & ascq.
  859. */
  860. if (qc->err_mask ||
  861. tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  862. ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
  863. &sense_key, &asc, &ascq, verbose);
  864. ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
  865. } else {
  866. /* Could not decode error */
  867. ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
  868. tf->command, qc->err_mask);
  869. ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
  870. return;
  871. }
  872. block = ata_tf_read_block(&qc->result_tf, dev);
  873. if (block == U64_MAX)
  874. return;
  875. scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
  876. }
  877. void ata_scsi_sdev_config(struct scsi_device *sdev)
  878. {
  879. sdev->use_10_for_rw = 1;
  880. sdev->use_10_for_ms = 1;
  881. sdev->no_write_same = 1;
  882. /* Schedule policy is determined by ->qc_defer() callback and
  883. * it needs to see every deferred qc. Set dev_blocked to 1 to
  884. * prevent SCSI midlayer from automatically deferring
  885. * requests.
  886. */
  887. sdev->max_device_blocked = 1;
  888. }
  889. /**
  890. * ata_scsi_dma_need_drain - Check whether data transfer may overflow
  891. * @rq: request to be checked
  892. *
  893. * ATAPI commands which transfer variable length data to host
  894. * might overflow due to application error or hardware bug. This
  895. * function checks whether overflow should be drained and ignored
  896. * for @request.
  897. *
  898. * LOCKING:
  899. * None.
  900. *
  901. * RETURNS:
  902. * 1 if ; otherwise, 0.
  903. */
  904. bool ata_scsi_dma_need_drain(struct request *rq)
  905. {
  906. return atapi_cmd_type(scsi_req(rq)->cmd[0]) == ATAPI_MISC;
  907. }
  908. EXPORT_SYMBOL_GPL(ata_scsi_dma_need_drain);
  909. int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)
  910. {
  911. struct request_queue *q = sdev->request_queue;
  912. if (!ata_id_has_unload(dev->id))
  913. dev->flags |= ATA_DFLAG_NO_UNLOAD;
  914. /* configure max sectors */
  915. blk_queue_max_hw_sectors(q, dev->max_sectors);
  916. if (dev->class == ATA_DEV_ATAPI) {
  917. sdev->sector_size = ATA_SECT_SIZE;
  918. /* set DMA padding */
  919. blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
  920. /* make room for appending the drain */
  921. blk_queue_max_segments(q, queue_max_segments(q) - 1);
  922. sdev->dma_drain_len = ATAPI_MAX_DRAIN;
  923. sdev->dma_drain_buf = kmalloc(sdev->dma_drain_len,
  924. q->bounce_gfp | GFP_KERNEL);
  925. if (!sdev->dma_drain_buf) {
  926. ata_dev_err(dev, "drain buffer allocation failed\n");
  927. return -ENOMEM;
  928. }
  929. } else {
  930. sdev->sector_size = ata_id_logical_sector_size(dev->id);
  931. sdev->manage_start_stop = 1;
  932. }
  933. /*
  934. * ata_pio_sectors() expects buffer for each sector to not cross
  935. * page boundary. Enforce it by requiring buffers to be sector
  936. * aligned, which works iff sector_size is not larger than
  937. * PAGE_SIZE. ATAPI devices also need the alignment as
  938. * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
  939. */
  940. if (sdev->sector_size > PAGE_SIZE)
  941. ata_dev_warn(dev,
  942. "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
  943. sdev->sector_size);
  944. blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
  945. if (dev->flags & ATA_DFLAG_AN)
  946. set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
  947. if (dev->flags & ATA_DFLAG_NCQ) {
  948. int depth;
  949. depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
  950. depth = min(ATA_MAX_QUEUE, depth);
  951. scsi_change_queue_depth(sdev, depth);
  952. }
  953. if (dev->flags & ATA_DFLAG_TRUSTED)
  954. sdev->security_supported = 1;
  955. dev->sdev = sdev;
  956. return 0;
  957. }
  958. /**
  959. * ata_scsi_slave_config - Set SCSI device attributes
  960. * @sdev: SCSI device to examine
  961. *
  962. * This is called before we actually start reading
  963. * and writing to the device, to configure certain
  964. * SCSI mid-layer behaviors.
  965. *
  966. * LOCKING:
  967. * Defined by SCSI layer. We don't really care.
  968. */
  969. int ata_scsi_slave_config(struct scsi_device *sdev)
  970. {
  971. struct ata_port *ap = ata_shost_to_port(sdev->host);
  972. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  973. int rc = 0;
  974. ata_scsi_sdev_config(sdev);
  975. if (dev)
  976. rc = ata_scsi_dev_config(sdev, dev);
  977. return rc;
  978. }
  979. EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
  980. /**
  981. * ata_scsi_slave_destroy - SCSI device is about to be destroyed
  982. * @sdev: SCSI device to be destroyed
  983. *
  984. * @sdev is about to be destroyed for hot/warm unplugging. If
  985. * this unplugging was initiated by libata as indicated by NULL
  986. * dev->sdev, this function doesn't have to do anything.
  987. * Otherwise, SCSI layer initiated warm-unplug is in progress.
  988. * Clear dev->sdev, schedule the device for ATA detach and invoke
  989. * EH.
  990. *
  991. * LOCKING:
  992. * Defined by SCSI layer. We don't really care.
  993. */
  994. void ata_scsi_slave_destroy(struct scsi_device *sdev)
  995. {
  996. struct ata_port *ap = ata_shost_to_port(sdev->host);
  997. unsigned long flags;
  998. struct ata_device *dev;
  999. if (!ap->ops->error_handler)
  1000. return;
  1001. spin_lock_irqsave(ap->lock, flags);
  1002. dev = __ata_scsi_find_dev(ap, sdev);
  1003. if (dev && dev->sdev) {
  1004. /* SCSI device already in CANCEL state, no need to offline it */
  1005. dev->sdev = NULL;
  1006. dev->flags |= ATA_DFLAG_DETACH;
  1007. ata_port_schedule_eh(ap);
  1008. }
  1009. spin_unlock_irqrestore(ap->lock, flags);
  1010. kfree(sdev->dma_drain_buf);
  1011. }
  1012. EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
  1013. /**
  1014. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  1015. * @qc: Storage for translated ATA taskfile
  1016. *
  1017. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  1018. * (to start). Perhaps these commands should be preceded by
  1019. * CHECK POWER MODE to see what power mode the device is already in.
  1020. * [See SAT revision 5 at www.t10.org]
  1021. *
  1022. * LOCKING:
  1023. * spin_lock_irqsave(host lock)
  1024. *
  1025. * RETURNS:
  1026. * Zero on success, non-zero on error.
  1027. */
  1028. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
  1029. {
  1030. struct scsi_cmnd *scmd = qc->scsicmd;
  1031. struct ata_taskfile *tf = &qc->tf;
  1032. const u8 *cdb = scmd->cmnd;
  1033. u16 fp;
  1034. u8 bp = 0xff;
  1035. if (scmd->cmd_len < 5) {
  1036. fp = 4;
  1037. goto invalid_fld;
  1038. }
  1039. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1040. tf->protocol = ATA_PROT_NODATA;
  1041. if (cdb[1] & 0x1) {
  1042. ; /* ignore IMMED bit, violates sat-r05 */
  1043. }
  1044. if (cdb[4] & 0x2) {
  1045. fp = 4;
  1046. bp = 1;
  1047. goto invalid_fld; /* LOEJ bit set not supported */
  1048. }
  1049. if (((cdb[4] >> 4) & 0xf) != 0) {
  1050. fp = 4;
  1051. bp = 3;
  1052. goto invalid_fld; /* power conditions not supported */
  1053. }
  1054. if (cdb[4] & 0x1) {
  1055. tf->nsect = 1; /* 1 sector, lba=0 */
  1056. if (qc->dev->flags & ATA_DFLAG_LBA) {
  1057. tf->flags |= ATA_TFLAG_LBA;
  1058. tf->lbah = 0x0;
  1059. tf->lbam = 0x0;
  1060. tf->lbal = 0x0;
  1061. tf->device |= ATA_LBA;
  1062. } else {
  1063. /* CHS */
  1064. tf->lbal = 0x1; /* sect */
  1065. tf->lbam = 0x0; /* cyl low */
  1066. tf->lbah = 0x0; /* cyl high */
  1067. }
  1068. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  1069. } else {
  1070. /* Some odd clown BIOSen issue spindown on power off (ACPI S4
  1071. * or S5) causing some drives to spin up and down again.
  1072. */
  1073. if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
  1074. system_state == SYSTEM_POWER_OFF)
  1075. goto skip;
  1076. if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
  1077. system_entering_hibernation())
  1078. goto skip;
  1079. /* Issue ATA STANDBY IMMEDIATE command */
  1080. tf->command = ATA_CMD_STANDBYNOW1;
  1081. }
  1082. /*
  1083. * Standby and Idle condition timers could be implemented but that
  1084. * would require libata to implement the Power condition mode page
  1085. * and allow the user to change it. Changing mode pages requires
  1086. * MODE SELECT to be implemented.
  1087. */
  1088. return 0;
  1089. invalid_fld:
  1090. ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
  1091. return 1;
  1092. skip:
  1093. scmd->result = SAM_STAT_GOOD;
  1094. return 1;
  1095. }
  1096. /**
  1097. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  1098. * @qc: Storage for translated ATA taskfile
  1099. *
  1100. * Sets up an ATA taskfile to issue FLUSH CACHE or
  1101. * FLUSH CACHE EXT.
  1102. *
  1103. * LOCKING:
  1104. * spin_lock_irqsave(host lock)
  1105. *
  1106. * RETURNS:
  1107. * Zero on success, non-zero on error.
  1108. */
  1109. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
  1110. {
  1111. struct ata_taskfile *tf = &qc->tf;
  1112. tf->flags |= ATA_TFLAG_DEVICE;
  1113. tf->protocol = ATA_PROT_NODATA;
  1114. if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
  1115. tf->command = ATA_CMD_FLUSH_EXT;
  1116. else
  1117. tf->command = ATA_CMD_FLUSH;
  1118. /* flush is critical for IO integrity, consider it an IO command */
  1119. qc->flags |= ATA_QCFLAG_IO;
  1120. return 0;
  1121. }
  1122. /**
  1123. * scsi_6_lba_len - Get LBA and transfer length
  1124. * @cdb: SCSI command to translate
  1125. *
  1126. * Calculate LBA and transfer length for 6-byte commands.
  1127. *
  1128. * RETURNS:
  1129. * @plba: the LBA
  1130. * @plen: the transfer length
  1131. */
  1132. static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1133. {
  1134. u64 lba = 0;
  1135. u32 len;
  1136. VPRINTK("six-byte command\n");
  1137. lba |= ((u64)(cdb[1] & 0x1f)) << 16;
  1138. lba |= ((u64)cdb[2]) << 8;
  1139. lba |= ((u64)cdb[3]);
  1140. len = cdb[4];
  1141. *plba = lba;
  1142. *plen = len;
  1143. }
  1144. /**
  1145. * scsi_10_lba_len - Get LBA and transfer length
  1146. * @cdb: SCSI command to translate
  1147. *
  1148. * Calculate LBA and transfer length for 10-byte commands.
  1149. *
  1150. * RETURNS:
  1151. * @plba: the LBA
  1152. * @plen: the transfer length
  1153. */
  1154. static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1155. {
  1156. u64 lba = 0;
  1157. u32 len = 0;
  1158. VPRINTK("ten-byte command\n");
  1159. lba |= ((u64)cdb[2]) << 24;
  1160. lba |= ((u64)cdb[3]) << 16;
  1161. lba |= ((u64)cdb[4]) << 8;
  1162. lba |= ((u64)cdb[5]);
  1163. len |= ((u32)cdb[7]) << 8;
  1164. len |= ((u32)cdb[8]);
  1165. *plba = lba;
  1166. *plen = len;
  1167. }
  1168. /**
  1169. * scsi_16_lba_len - Get LBA and transfer length
  1170. * @cdb: SCSI command to translate
  1171. *
  1172. * Calculate LBA and transfer length for 16-byte commands.
  1173. *
  1174. * RETURNS:
  1175. * @plba: the LBA
  1176. * @plen: the transfer length
  1177. */
  1178. static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1179. {
  1180. u64 lba = 0;
  1181. u32 len = 0;
  1182. VPRINTK("sixteen-byte command\n");
  1183. lba |= ((u64)cdb[2]) << 56;
  1184. lba |= ((u64)cdb[3]) << 48;
  1185. lba |= ((u64)cdb[4]) << 40;
  1186. lba |= ((u64)cdb[5]) << 32;
  1187. lba |= ((u64)cdb[6]) << 24;
  1188. lba |= ((u64)cdb[7]) << 16;
  1189. lba |= ((u64)cdb[8]) << 8;
  1190. lba |= ((u64)cdb[9]);
  1191. len |= ((u32)cdb[10]) << 24;
  1192. len |= ((u32)cdb[11]) << 16;
  1193. len |= ((u32)cdb[12]) << 8;
  1194. len |= ((u32)cdb[13]);
  1195. *plba = lba;
  1196. *plen = len;
  1197. }
  1198. /**
  1199. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  1200. * @qc: Storage for translated ATA taskfile
  1201. *
  1202. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  1203. *
  1204. * LOCKING:
  1205. * spin_lock_irqsave(host lock)
  1206. *
  1207. * RETURNS:
  1208. * Zero on success, non-zero on error.
  1209. */
  1210. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
  1211. {
  1212. struct scsi_cmnd *scmd = qc->scsicmd;
  1213. struct ata_taskfile *tf = &qc->tf;
  1214. struct ata_device *dev = qc->dev;
  1215. u64 dev_sectors = qc->dev->n_sectors;
  1216. const u8 *cdb = scmd->cmnd;
  1217. u64 block;
  1218. u32 n_block;
  1219. u16 fp;
  1220. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1221. tf->protocol = ATA_PROT_NODATA;
  1222. if (cdb[0] == VERIFY) {
  1223. if (scmd->cmd_len < 10) {
  1224. fp = 9;
  1225. goto invalid_fld;
  1226. }
  1227. scsi_10_lba_len(cdb, &block, &n_block);
  1228. } else if (cdb[0] == VERIFY_16) {
  1229. if (scmd->cmd_len < 16) {
  1230. fp = 15;
  1231. goto invalid_fld;
  1232. }
  1233. scsi_16_lba_len(cdb, &block, &n_block);
  1234. } else {
  1235. fp = 0;
  1236. goto invalid_fld;
  1237. }
  1238. if (!n_block)
  1239. goto nothing_to_do;
  1240. if (block >= dev_sectors)
  1241. goto out_of_range;
  1242. if ((block + n_block) > dev_sectors)
  1243. goto out_of_range;
  1244. if (dev->flags & ATA_DFLAG_LBA) {
  1245. tf->flags |= ATA_TFLAG_LBA;
  1246. if (lba_28_ok(block, n_block)) {
  1247. /* use LBA28 */
  1248. tf->command = ATA_CMD_VERIFY;
  1249. tf->device |= (block >> 24) & 0xf;
  1250. } else if (lba_48_ok(block, n_block)) {
  1251. if (!(dev->flags & ATA_DFLAG_LBA48))
  1252. goto out_of_range;
  1253. /* use LBA48 */
  1254. tf->flags |= ATA_TFLAG_LBA48;
  1255. tf->command = ATA_CMD_VERIFY_EXT;
  1256. tf->hob_nsect = (n_block >> 8) & 0xff;
  1257. tf->hob_lbah = (block >> 40) & 0xff;
  1258. tf->hob_lbam = (block >> 32) & 0xff;
  1259. tf->hob_lbal = (block >> 24) & 0xff;
  1260. } else
  1261. /* request too large even for LBA48 */
  1262. goto out_of_range;
  1263. tf->nsect = n_block & 0xff;
  1264. tf->lbah = (block >> 16) & 0xff;
  1265. tf->lbam = (block >> 8) & 0xff;
  1266. tf->lbal = block & 0xff;
  1267. tf->device |= ATA_LBA;
  1268. } else {
  1269. /* CHS */
  1270. u32 sect, head, cyl, track;
  1271. if (!lba_28_ok(block, n_block))
  1272. goto out_of_range;
  1273. /* Convert LBA to CHS */
  1274. track = (u32)block / dev->sectors;
  1275. cyl = track / dev->heads;
  1276. head = track % dev->heads;
  1277. sect = (u32)block % dev->sectors + 1;
  1278. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1279. (u32)block, track, cyl, head, sect);
  1280. /* Check whether the converted CHS can fit.
  1281. Cylinder: 0-65535
  1282. Head: 0-15
  1283. Sector: 1-255*/
  1284. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1285. goto out_of_range;
  1286. tf->command = ATA_CMD_VERIFY;
  1287. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1288. tf->lbal = sect;
  1289. tf->lbam = cyl;
  1290. tf->lbah = cyl >> 8;
  1291. tf->device |= head;
  1292. }
  1293. return 0;
  1294. invalid_fld:
  1295. ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
  1296. return 1;
  1297. out_of_range:
  1298. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1299. /* "Logical Block Address out of range" */
  1300. return 1;
  1301. nothing_to_do:
  1302. scmd->result = SAM_STAT_GOOD;
  1303. return 1;
  1304. }
  1305. static bool ata_check_nblocks(struct scsi_cmnd *scmd, u32 n_blocks)
  1306. {
  1307. struct request *rq = scmd->request;
  1308. u32 req_blocks;
  1309. if (!blk_rq_is_passthrough(rq))
  1310. return true;
  1311. req_blocks = blk_rq_bytes(rq) / scmd->device->sector_size;
  1312. if (n_blocks > req_blocks)
  1313. return false;
  1314. return true;
  1315. }
  1316. /**
  1317. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  1318. * @qc: Storage for translated ATA taskfile
  1319. *
  1320. * Converts any of six SCSI read/write commands into the
  1321. * ATA counterpart, including starting sector (LBA),
  1322. * sector count, and taking into account the device's LBA48
  1323. * support.
  1324. *
  1325. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  1326. * %WRITE_16 are currently supported.
  1327. *
  1328. * LOCKING:
  1329. * spin_lock_irqsave(host lock)
  1330. *
  1331. * RETURNS:
  1332. * Zero on success, non-zero on error.
  1333. */
  1334. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
  1335. {
  1336. struct scsi_cmnd *scmd = qc->scsicmd;
  1337. const u8 *cdb = scmd->cmnd;
  1338. struct request *rq = scmd->request;
  1339. int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
  1340. unsigned int tf_flags = 0;
  1341. u64 block;
  1342. u32 n_block;
  1343. int rc;
  1344. u16 fp = 0;
  1345. if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
  1346. tf_flags |= ATA_TFLAG_WRITE;
  1347. /* Calculate the SCSI LBA, transfer length and FUA. */
  1348. switch (cdb[0]) {
  1349. case READ_10:
  1350. case WRITE_10:
  1351. if (unlikely(scmd->cmd_len < 10)) {
  1352. fp = 9;
  1353. goto invalid_fld;
  1354. }
  1355. scsi_10_lba_len(cdb, &block, &n_block);
  1356. if (cdb[1] & (1 << 3))
  1357. tf_flags |= ATA_TFLAG_FUA;
  1358. if (!ata_check_nblocks(scmd, n_block))
  1359. goto invalid_fld;
  1360. break;
  1361. case READ_6:
  1362. case WRITE_6:
  1363. if (unlikely(scmd->cmd_len < 6)) {
  1364. fp = 5;
  1365. goto invalid_fld;
  1366. }
  1367. scsi_6_lba_len(cdb, &block, &n_block);
  1368. /* for 6-byte r/w commands, transfer length 0
  1369. * means 256 blocks of data, not 0 block.
  1370. */
  1371. if (!n_block)
  1372. n_block = 256;
  1373. if (!ata_check_nblocks(scmd, n_block))
  1374. goto invalid_fld;
  1375. break;
  1376. case READ_16:
  1377. case WRITE_16:
  1378. if (unlikely(scmd->cmd_len < 16)) {
  1379. fp = 15;
  1380. goto invalid_fld;
  1381. }
  1382. scsi_16_lba_len(cdb, &block, &n_block);
  1383. if (cdb[1] & (1 << 3))
  1384. tf_flags |= ATA_TFLAG_FUA;
  1385. if (!ata_check_nblocks(scmd, n_block))
  1386. goto invalid_fld;
  1387. break;
  1388. default:
  1389. DPRINTK("no-byte command\n");
  1390. fp = 0;
  1391. goto invalid_fld;
  1392. }
  1393. /* Check and compose ATA command */
  1394. if (!n_block)
  1395. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  1396. * length 0 means transfer 0 block of data.
  1397. * However, for ATA R/W commands, sector count 0 means
  1398. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  1399. *
  1400. * WARNING: one or two older ATA drives treat 0 as 0...
  1401. */
  1402. goto nothing_to_do;
  1403. qc->flags |= ATA_QCFLAG_IO;
  1404. qc->nbytes = n_block * scmd->device->sector_size;
  1405. rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
  1406. qc->hw_tag, class);
  1407. if (likely(rc == 0))
  1408. return 0;
  1409. if (rc == -ERANGE)
  1410. goto out_of_range;
  1411. /* treat all other errors as -EINVAL, fall through */
  1412. invalid_fld:
  1413. ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
  1414. return 1;
  1415. out_of_range:
  1416. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1417. /* "Logical Block Address out of range" */
  1418. return 1;
  1419. nothing_to_do:
  1420. scmd->result = SAM_STAT_GOOD;
  1421. return 1;
  1422. }
  1423. static void ata_qc_done(struct ata_queued_cmd *qc)
  1424. {
  1425. struct scsi_cmnd *cmd = qc->scsicmd;
  1426. void (*done)(struct scsi_cmnd *) = qc->scsidone;
  1427. ata_qc_free(qc);
  1428. done(cmd);
  1429. }
  1430. static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  1431. {
  1432. struct ata_port *ap = qc->ap;
  1433. struct scsi_cmnd *cmd = qc->scsicmd;
  1434. u8 *cdb = cmd->cmnd;
  1435. int need_sense = (qc->err_mask != 0);
  1436. /* For ATA pass thru (SAT) commands, generate a sense block if
  1437. * user mandated it or if there's an error. Note that if we
  1438. * generate because the user forced us to [CK_COND =1], a check
  1439. * condition is generated and the ATA register values are returned
  1440. * whether the command completed successfully or not. If there
  1441. * was no error, we use the following sense data:
  1442. * sk = RECOVERED ERROR
  1443. * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
  1444. */
  1445. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1446. ((cdb[2] & 0x20) || need_sense))
  1447. ata_gen_passthru_sense(qc);
  1448. else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1449. cmd->result = SAM_STAT_CHECK_CONDITION;
  1450. else if (need_sense)
  1451. ata_gen_ata_sense(qc);
  1452. else
  1453. cmd->result = SAM_STAT_GOOD;
  1454. if (need_sense && !ap->ops->error_handler)
  1455. ata_dump_status(ap->print_id, &qc->result_tf);
  1456. ata_qc_done(qc);
  1457. }
  1458. /**
  1459. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1460. * @dev: ATA device to which the command is addressed
  1461. * @cmd: SCSI command to execute
  1462. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1463. *
  1464. * Our ->queuecommand() function has decided that the SCSI
  1465. * command issued can be directly translated into an ATA
  1466. * command, rather than handled internally.
  1467. *
  1468. * This function sets up an ata_queued_cmd structure for the
  1469. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1470. *
  1471. * The xlat_func argument (actor) returns 0 if ready to execute
  1472. * ATA command, else 1 to finish translation. If 1 is returned
  1473. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1474. * to be set reflecting an error condition or clean (early)
  1475. * termination.
  1476. *
  1477. * LOCKING:
  1478. * spin_lock_irqsave(host lock)
  1479. *
  1480. * RETURNS:
  1481. * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
  1482. * needs to be deferred.
  1483. */
  1484. static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
  1485. ata_xlat_func_t xlat_func)
  1486. {
  1487. struct ata_port *ap = dev->link->ap;
  1488. struct ata_queued_cmd *qc;
  1489. int rc;
  1490. VPRINTK("ENTER\n");
  1491. qc = ata_scsi_qc_new(dev, cmd);
  1492. if (!qc)
  1493. goto err_mem;
  1494. /* data is present; dma-map it */
  1495. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1496. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1497. if (unlikely(scsi_bufflen(cmd) < 1)) {
  1498. ata_dev_warn(dev, "WARNING: zero len r/w req\n");
  1499. goto err_did;
  1500. }
  1501. ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
  1502. qc->dma_dir = cmd->sc_data_direction;
  1503. }
  1504. qc->complete_fn = ata_scsi_qc_complete;
  1505. if (xlat_func(qc))
  1506. goto early_finish;
  1507. if (ap->ops->qc_defer) {
  1508. if ((rc = ap->ops->qc_defer(qc)))
  1509. goto defer;
  1510. }
  1511. /* select device, send command to hardware */
  1512. ata_qc_issue(qc);
  1513. VPRINTK("EXIT\n");
  1514. return 0;
  1515. early_finish:
  1516. ata_qc_free(qc);
  1517. cmd->scsi_done(cmd);
  1518. DPRINTK("EXIT - early finish (good or error)\n");
  1519. return 0;
  1520. err_did:
  1521. ata_qc_free(qc);
  1522. cmd->result = (DID_ERROR << 16);
  1523. cmd->scsi_done(cmd);
  1524. err_mem:
  1525. DPRINTK("EXIT - internal\n");
  1526. return 0;
  1527. defer:
  1528. ata_qc_free(qc);
  1529. DPRINTK("EXIT - defer\n");
  1530. if (rc == ATA_DEFER_LINK)
  1531. return SCSI_MLQUEUE_DEVICE_BUSY;
  1532. else
  1533. return SCSI_MLQUEUE_HOST_BUSY;
  1534. }
  1535. struct ata_scsi_args {
  1536. struct ata_device *dev;
  1537. u16 *id;
  1538. struct scsi_cmnd *cmd;
  1539. };
  1540. /**
  1541. * ata_scsi_rbuf_get - Map response buffer.
  1542. * @cmd: SCSI command containing buffer to be mapped.
  1543. * @flags: unsigned long variable to store irq enable status
  1544. * @copy_in: copy in from user buffer
  1545. *
  1546. * Prepare buffer for simulated SCSI commands.
  1547. *
  1548. * LOCKING:
  1549. * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
  1550. *
  1551. * RETURNS:
  1552. * Pointer to response buffer.
  1553. */
  1554. static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
  1555. unsigned long *flags)
  1556. {
  1557. spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
  1558. memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
  1559. if (copy_in)
  1560. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1561. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1562. return ata_scsi_rbuf;
  1563. }
  1564. /**
  1565. * ata_scsi_rbuf_put - Unmap response buffer.
  1566. * @cmd: SCSI command containing buffer to be unmapped.
  1567. * @copy_out: copy out result
  1568. * @flags: @flags passed to ata_scsi_rbuf_get()
  1569. *
  1570. * Returns rbuf buffer. The result is copied to @cmd's buffer if
  1571. * @copy_back is true.
  1572. *
  1573. * LOCKING:
  1574. * Unlocks ata_scsi_rbuf_lock.
  1575. */
  1576. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
  1577. unsigned long *flags)
  1578. {
  1579. if (copy_out)
  1580. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1581. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1582. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
  1583. }
  1584. /**
  1585. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1586. * @args: device IDENTIFY data / SCSI command of interest.
  1587. * @actor: Callback hook for desired SCSI command simulator
  1588. *
  1589. * Takes care of the hard work of simulating a SCSI command...
  1590. * Mapping the response buffer, calling the command's handler,
  1591. * and handling the handler's return value. This return value
  1592. * indicates whether the handler wishes the SCSI command to be
  1593. * completed successfully (0), or not (in which case cmd->result
  1594. * and sense buffer are assumed to be set).
  1595. *
  1596. * LOCKING:
  1597. * spin_lock_irqsave(host lock)
  1598. */
  1599. static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1600. unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
  1601. {
  1602. u8 *rbuf;
  1603. unsigned int rc;
  1604. struct scsi_cmnd *cmd = args->cmd;
  1605. unsigned long flags;
  1606. rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
  1607. rc = actor(args, rbuf);
  1608. ata_scsi_rbuf_put(cmd, rc == 0, &flags);
  1609. if (rc == 0)
  1610. cmd->result = SAM_STAT_GOOD;
  1611. }
  1612. /**
  1613. * ata_scsiop_inq_std - Simulate INQUIRY command
  1614. * @args: device IDENTIFY data / SCSI command of interest.
  1615. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1616. *
  1617. * Returns standard device identification data associated
  1618. * with non-VPD INQUIRY command output.
  1619. *
  1620. * LOCKING:
  1621. * spin_lock_irqsave(host lock)
  1622. */
  1623. static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
  1624. {
  1625. static const u8 versions[] = {
  1626. 0x00,
  1627. 0x60, /* SAM-3 (no version claimed) */
  1628. 0x03,
  1629. 0x20, /* SBC-2 (no version claimed) */
  1630. 0x03,
  1631. 0x00 /* SPC-3 (no version claimed) */
  1632. };
  1633. static const u8 versions_zbc[] = {
  1634. 0x00,
  1635. 0xA0, /* SAM-5 (no version claimed) */
  1636. 0x06,
  1637. 0x00, /* SBC-4 (no version claimed) */
  1638. 0x05,
  1639. 0xC0, /* SPC-5 (no version claimed) */
  1640. 0x60,
  1641. 0x24, /* ZBC r05 */
  1642. };
  1643. u8 hdr[] = {
  1644. TYPE_DISK,
  1645. 0,
  1646. 0x5, /* claim SPC-3 version compatibility */
  1647. 2,
  1648. 95 - 4,
  1649. 0,
  1650. 0,
  1651. 2
  1652. };
  1653. VPRINTK("ENTER\n");
  1654. /* set scsi removable (RMB) bit per ata bit, or if the
  1655. * AHCI port says it's external (Hotplug-capable, eSATA).
  1656. */
  1657. if (ata_id_removable(args->id) ||
  1658. (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
  1659. hdr[1] |= (1 << 7);
  1660. if (args->dev->class == ATA_DEV_ZAC) {
  1661. hdr[0] = TYPE_ZBC;
  1662. hdr[2] = 0x7; /* claim SPC-5 version compatibility */
  1663. }
  1664. memcpy(rbuf, hdr, sizeof(hdr));
  1665. memcpy(&rbuf[8], "ATA ", 8);
  1666. ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
  1667. /* From SAT, use last 2 words from fw rev unless they are spaces */
  1668. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
  1669. if (strncmp(&rbuf[32], " ", 4) == 0)
  1670. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1671. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1672. memcpy(&rbuf[32], "n/a ", 4);
  1673. if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
  1674. memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
  1675. else
  1676. memcpy(rbuf + 58, versions, sizeof(versions));
  1677. return 0;
  1678. }
  1679. /**
  1680. * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  1681. * @args: device IDENTIFY data / SCSI command of interest.
  1682. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1683. *
  1684. * Returns list of inquiry VPD pages available.
  1685. *
  1686. * LOCKING:
  1687. * spin_lock_irqsave(host lock)
  1688. */
  1689. static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
  1690. {
  1691. int num_pages;
  1692. static const u8 pages[] = {
  1693. 0x00, /* page 0x00, this page */
  1694. 0x80, /* page 0x80, unit serial no page */
  1695. 0x83, /* page 0x83, device ident page */
  1696. 0x89, /* page 0x89, ata info page */
  1697. 0xb0, /* page 0xb0, block limits page */
  1698. 0xb1, /* page 0xb1, block device characteristics page */
  1699. 0xb2, /* page 0xb2, thin provisioning page */
  1700. 0xb6, /* page 0xb6, zoned block device characteristics */
  1701. };
  1702. num_pages = sizeof(pages);
  1703. if (!(args->dev->flags & ATA_DFLAG_ZAC))
  1704. num_pages--;
  1705. rbuf[3] = num_pages; /* number of supported VPD pages */
  1706. memcpy(rbuf + 4, pages, num_pages);
  1707. return 0;
  1708. }
  1709. /**
  1710. * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  1711. * @args: device IDENTIFY data / SCSI command of interest.
  1712. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1713. *
  1714. * Returns ATA device serial number.
  1715. *
  1716. * LOCKING:
  1717. * spin_lock_irqsave(host lock)
  1718. */
  1719. static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
  1720. {
  1721. static const u8 hdr[] = {
  1722. 0,
  1723. 0x80, /* this page code */
  1724. 0,
  1725. ATA_ID_SERNO_LEN, /* page len */
  1726. };
  1727. memcpy(rbuf, hdr, sizeof(hdr));
  1728. ata_id_string(args->id, (unsigned char *) &rbuf[4],
  1729. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1730. return 0;
  1731. }
  1732. /**
  1733. * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  1734. * @args: device IDENTIFY data / SCSI command of interest.
  1735. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1736. *
  1737. * Yields two logical unit device identification designators:
  1738. * - vendor specific ASCII containing the ATA serial number
  1739. * - SAT defined "t10 vendor id based" containing ASCII vendor
  1740. * name ("ATA "), model and serial numbers.
  1741. *
  1742. * LOCKING:
  1743. * spin_lock_irqsave(host lock)
  1744. */
  1745. static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
  1746. {
  1747. const int sat_model_serial_desc_len = 68;
  1748. int num;
  1749. rbuf[1] = 0x83; /* this page code */
  1750. num = 4;
  1751. /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
  1752. rbuf[num + 0] = 2;
  1753. rbuf[num + 3] = ATA_ID_SERNO_LEN;
  1754. num += 4;
  1755. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1756. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1757. num += ATA_ID_SERNO_LEN;
  1758. /* SAT defined lu model and serial numbers descriptor */
  1759. /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
  1760. rbuf[num + 0] = 2;
  1761. rbuf[num + 1] = 1;
  1762. rbuf[num + 3] = sat_model_serial_desc_len;
  1763. num += 4;
  1764. memcpy(rbuf + num, "ATA ", 8);
  1765. num += 8;
  1766. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
  1767. ATA_ID_PROD_LEN);
  1768. num += ATA_ID_PROD_LEN;
  1769. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
  1770. ATA_ID_SERNO_LEN);
  1771. num += ATA_ID_SERNO_LEN;
  1772. if (ata_id_has_wwn(args->id)) {
  1773. /* SAT defined lu world wide name */
  1774. /* piv=0, assoc=lu, code_set=binary, designator=NAA */
  1775. rbuf[num + 0] = 1;
  1776. rbuf[num + 1] = 3;
  1777. rbuf[num + 3] = ATA_ID_WWN_LEN;
  1778. num += 4;
  1779. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1780. ATA_ID_WWN, ATA_ID_WWN_LEN);
  1781. num += ATA_ID_WWN_LEN;
  1782. }
  1783. rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
  1784. return 0;
  1785. }
  1786. /**
  1787. * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
  1788. * @args: device IDENTIFY data / SCSI command of interest.
  1789. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1790. *
  1791. * Yields SAT-specified ATA VPD page.
  1792. *
  1793. * LOCKING:
  1794. * spin_lock_irqsave(host lock)
  1795. */
  1796. static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
  1797. {
  1798. rbuf[1] = 0x89; /* our page code */
  1799. rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
  1800. rbuf[3] = (0x238 & 0xff);
  1801. memcpy(&rbuf[8], "linux ", 8);
  1802. memcpy(&rbuf[16], "libata ", 16);
  1803. memcpy(&rbuf[32], DRV_VERSION, 4);
  1804. rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
  1805. rbuf[37] = (1 << 7); /* bit 7 indicates Command FIS */
  1806. /* TODO: PMP? */
  1807. /* we don't store the ATA device signature, so we fake it */
  1808. rbuf[38] = ATA_DRDY; /* really, this is Status reg */
  1809. rbuf[40] = 0x1;
  1810. rbuf[48] = 0x1;
  1811. rbuf[56] = ATA_CMD_ID_ATA;
  1812. memcpy(&rbuf[60], &args->id[0], 512);
  1813. return 0;
  1814. }
  1815. static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
  1816. {
  1817. struct ata_device *dev = args->dev;
  1818. u16 min_io_sectors;
  1819. rbuf[1] = 0xb0;
  1820. rbuf[3] = 0x3c; /* required VPD size with unmap support */
  1821. /*
  1822. * Optimal transfer length granularity.
  1823. *
  1824. * This is always one physical block, but for disks with a smaller
  1825. * logical than physical sector size we need to figure out what the
  1826. * latter is.
  1827. */
  1828. min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
  1829. put_unaligned_be16(min_io_sectors, &rbuf[6]);
  1830. /*
  1831. * Optimal unmap granularity.
  1832. *
  1833. * The ATA spec doesn't even know about a granularity or alignment
  1834. * for the TRIM command. We can leave away most of the unmap related
  1835. * VPD page entries, but we have specifify a granularity to signal
  1836. * that we support some form of unmap - in thise case via WRITE SAME
  1837. * with the unmap bit set.
  1838. */
  1839. if (ata_id_has_trim(args->id)) {
  1840. u64 max_blocks = 65535 * ATA_MAX_TRIM_RNUM;
  1841. if (dev->horkage & ATA_HORKAGE_MAX_TRIM_128M)
  1842. max_blocks = 128 << (20 - SECTOR_SHIFT);
  1843. put_unaligned_be64(max_blocks, &rbuf[36]);
  1844. put_unaligned_be32(1, &rbuf[28]);
  1845. }
  1846. return 0;
  1847. }
  1848. static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
  1849. {
  1850. int form_factor = ata_id_form_factor(args->id);
  1851. int media_rotation_rate = ata_id_rotation_rate(args->id);
  1852. u8 zoned = ata_id_zoned_cap(args->id);
  1853. rbuf[1] = 0xb1;
  1854. rbuf[3] = 0x3c;
  1855. rbuf[4] = media_rotation_rate >> 8;
  1856. rbuf[5] = media_rotation_rate;
  1857. rbuf[7] = form_factor;
  1858. if (zoned)
  1859. rbuf[8] = (zoned << 4);
  1860. return 0;
  1861. }
  1862. static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
  1863. {
  1864. /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
  1865. rbuf[1] = 0xb2;
  1866. rbuf[3] = 0x4;
  1867. rbuf[5] = 1 << 6; /* TPWS */
  1868. return 0;
  1869. }
  1870. static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
  1871. {
  1872. /*
  1873. * zbc-r05 SCSI Zoned Block device characteristics VPD page
  1874. */
  1875. rbuf[1] = 0xb6;
  1876. rbuf[3] = 0x3C;
  1877. /*
  1878. * URSWRZ bit is only meaningful for host-managed ZAC drives
  1879. */
  1880. if (args->dev->zac_zoned_cap & 1)
  1881. rbuf[4] |= 1;
  1882. put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
  1883. put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
  1884. put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
  1885. return 0;
  1886. }
  1887. /**
  1888. * modecpy - Prepare response for MODE SENSE
  1889. * @dest: output buffer
  1890. * @src: data being copied
  1891. * @n: length of mode page
  1892. * @changeable: whether changeable parameters are requested
  1893. *
  1894. * Generate a generic MODE SENSE page for either current or changeable
  1895. * parameters.
  1896. *
  1897. * LOCKING:
  1898. * None.
  1899. */
  1900. static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
  1901. {
  1902. if (changeable) {
  1903. memcpy(dest, src, 2);
  1904. memset(dest + 2, 0, n - 2);
  1905. } else {
  1906. memcpy(dest, src, n);
  1907. }
  1908. }
  1909. /**
  1910. * ata_msense_caching - Simulate MODE SENSE caching info page
  1911. * @id: device IDENTIFY data
  1912. * @buf: output buffer
  1913. * @changeable: whether changeable parameters are requested
  1914. *
  1915. * Generate a caching info page, which conditionally indicates
  1916. * write caching to the SCSI layer, depending on device
  1917. * capabilities.
  1918. *
  1919. * LOCKING:
  1920. * None.
  1921. */
  1922. static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
  1923. {
  1924. modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
  1925. if (changeable) {
  1926. buf[2] |= (1 << 2); /* ata_mselect_caching() */
  1927. } else {
  1928. buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
  1929. buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
  1930. }
  1931. return sizeof(def_cache_mpage);
  1932. }
  1933. /**
  1934. * ata_msense_control - Simulate MODE SENSE control mode page
  1935. * @dev: ATA device of interest
  1936. * @buf: output buffer
  1937. * @changeable: whether changeable parameters are requested
  1938. *
  1939. * Generate a generic MODE SENSE control mode page.
  1940. *
  1941. * LOCKING:
  1942. * None.
  1943. */
  1944. static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
  1945. bool changeable)
  1946. {
  1947. modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
  1948. if (changeable) {
  1949. buf[2] |= (1 << 2); /* ata_mselect_control() */
  1950. } else {
  1951. bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
  1952. buf[2] |= (d_sense << 2); /* descriptor format sense data */
  1953. }
  1954. return sizeof(def_control_mpage);
  1955. }
  1956. /**
  1957. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1958. * @buf: output buffer
  1959. * @changeable: whether changeable parameters are requested
  1960. *
  1961. * Generate a generic MODE SENSE r/w error recovery page.
  1962. *
  1963. * LOCKING:
  1964. * None.
  1965. */
  1966. static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
  1967. {
  1968. modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
  1969. changeable);
  1970. return sizeof(def_rw_recovery_mpage);
  1971. }
  1972. /*
  1973. * We can turn this into a real blacklist if it's needed, for now just
  1974. * blacklist any Maxtor BANC1G10 revision firmware
  1975. */
  1976. static int ata_dev_supports_fua(u16 *id)
  1977. {
  1978. unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
  1979. if (!libata_fua)
  1980. return 0;
  1981. if (!ata_id_has_fua(id))
  1982. return 0;
  1983. ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  1984. ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
  1985. if (strcmp(model, "Maxtor"))
  1986. return 1;
  1987. if (strcmp(fw, "BANC1G10"))
  1988. return 1;
  1989. return 0; /* blacklisted */
  1990. }
  1991. /**
  1992. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1993. * @args: device IDENTIFY data / SCSI command of interest.
  1994. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1995. *
  1996. * Simulate MODE SENSE commands. Assume this is invoked for direct
  1997. * access devices (e.g. disks) only. There should be no block
  1998. * descriptor for other device types.
  1999. *
  2000. * LOCKING:
  2001. * spin_lock_irqsave(host lock)
  2002. */
  2003. static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
  2004. {
  2005. struct ata_device *dev = args->dev;
  2006. u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
  2007. static const u8 sat_blk_desc[] = {
  2008. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  2009. 0,
  2010. 0, 0x2, 0x0 /* block length: 512 bytes */
  2011. };
  2012. u8 pg, spg;
  2013. unsigned int ebd, page_control, six_byte;
  2014. u8 dpofua, bp = 0xff;
  2015. u16 fp;
  2016. VPRINTK("ENTER\n");
  2017. six_byte = (scsicmd[0] == MODE_SENSE);
  2018. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  2019. /*
  2020. * LLBA bit in msense(10) ignored (compliant)
  2021. */
  2022. page_control = scsicmd[2] >> 6;
  2023. switch (page_control) {
  2024. case 0: /* current */
  2025. case 1: /* changeable */
  2026. case 2: /* defaults */
  2027. break; /* supported */
  2028. case 3: /* saved */
  2029. goto saving_not_supp;
  2030. default:
  2031. fp = 2;
  2032. bp = 6;
  2033. goto invalid_fld;
  2034. }
  2035. if (six_byte)
  2036. p += 4 + (ebd ? 8 : 0);
  2037. else
  2038. p += 8 + (ebd ? 8 : 0);
  2039. pg = scsicmd[2] & 0x3f;
  2040. spg = scsicmd[3];
  2041. /*
  2042. * No mode subpages supported (yet) but asking for _all_
  2043. * subpages may be valid
  2044. */
  2045. if (spg && (spg != ALL_SUB_MPAGES)) {
  2046. fp = 3;
  2047. goto invalid_fld;
  2048. }
  2049. switch(pg) {
  2050. case RW_RECOVERY_MPAGE:
  2051. p += ata_msense_rw_recovery(p, page_control == 1);
  2052. break;
  2053. case CACHE_MPAGE:
  2054. p += ata_msense_caching(args->id, p, page_control == 1);
  2055. break;
  2056. case CONTROL_MPAGE:
  2057. p += ata_msense_control(args->dev, p, page_control == 1);
  2058. break;
  2059. case ALL_MPAGES:
  2060. p += ata_msense_rw_recovery(p, page_control == 1);
  2061. p += ata_msense_caching(args->id, p, page_control == 1);
  2062. p += ata_msense_control(args->dev, p, page_control == 1);
  2063. break;
  2064. default: /* invalid page code */
  2065. fp = 2;
  2066. goto invalid_fld;
  2067. }
  2068. dpofua = 0;
  2069. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  2070. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  2071. dpofua = 1 << 4;
  2072. if (six_byte) {
  2073. rbuf[0] = p - rbuf - 1;
  2074. rbuf[2] |= dpofua;
  2075. if (ebd) {
  2076. rbuf[3] = sizeof(sat_blk_desc);
  2077. memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
  2078. }
  2079. } else {
  2080. unsigned int output_len = p - rbuf - 2;
  2081. rbuf[0] = output_len >> 8;
  2082. rbuf[1] = output_len;
  2083. rbuf[3] |= dpofua;
  2084. if (ebd) {
  2085. rbuf[7] = sizeof(sat_blk_desc);
  2086. memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
  2087. }
  2088. }
  2089. return 0;
  2090. invalid_fld:
  2091. ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
  2092. return 1;
  2093. saving_not_supp:
  2094. ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  2095. /* "Saving parameters not supported" */
  2096. return 1;
  2097. }
  2098. /**
  2099. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  2100. * @args: device IDENTIFY data / SCSI command of interest.
  2101. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2102. *
  2103. * Simulate READ CAPACITY commands.
  2104. *
  2105. * LOCKING:
  2106. * None.
  2107. */
  2108. static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
  2109. {
  2110. struct ata_device *dev = args->dev;
  2111. u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
  2112. u32 sector_size; /* physical sector size in bytes */
  2113. u8 log2_per_phys;
  2114. u16 lowest_aligned;
  2115. sector_size = ata_id_logical_sector_size(dev->id);
  2116. log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
  2117. lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
  2118. VPRINTK("ENTER\n");
  2119. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  2120. if (last_lba >= 0xffffffffULL)
  2121. last_lba = 0xffffffff;
  2122. /* sector count, 32-bit */
  2123. rbuf[0] = last_lba >> (8 * 3);
  2124. rbuf[1] = last_lba >> (8 * 2);
  2125. rbuf[2] = last_lba >> (8 * 1);
  2126. rbuf[3] = last_lba;
  2127. /* sector size */
  2128. rbuf[4] = sector_size >> (8 * 3);
  2129. rbuf[5] = sector_size >> (8 * 2);
  2130. rbuf[6] = sector_size >> (8 * 1);
  2131. rbuf[7] = sector_size;
  2132. } else {
  2133. /* sector count, 64-bit */
  2134. rbuf[0] = last_lba >> (8 * 7);
  2135. rbuf[1] = last_lba >> (8 * 6);
  2136. rbuf[2] = last_lba >> (8 * 5);
  2137. rbuf[3] = last_lba >> (8 * 4);
  2138. rbuf[4] = last_lba >> (8 * 3);
  2139. rbuf[5] = last_lba >> (8 * 2);
  2140. rbuf[6] = last_lba >> (8 * 1);
  2141. rbuf[7] = last_lba;
  2142. /* sector size */
  2143. rbuf[ 8] = sector_size >> (8 * 3);
  2144. rbuf[ 9] = sector_size >> (8 * 2);
  2145. rbuf[10] = sector_size >> (8 * 1);
  2146. rbuf[11] = sector_size;
  2147. rbuf[12] = 0;
  2148. rbuf[13] = log2_per_phys;
  2149. rbuf[14] = (lowest_aligned >> 8) & 0x3f;
  2150. rbuf[15] = lowest_aligned;
  2151. if (ata_id_has_trim(args->id) &&
  2152. !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
  2153. rbuf[14] |= 0x80; /* LBPME */
  2154. if (ata_id_has_zero_after_trim(args->id) &&
  2155. dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
  2156. ata_dev_info(dev, "Enabling discard_zeroes_data\n");
  2157. rbuf[14] |= 0x40; /* LBPRZ */
  2158. }
  2159. }
  2160. if (ata_id_zoned_cap(args->id) ||
  2161. args->dev->class == ATA_DEV_ZAC)
  2162. rbuf[12] = (1 << 4); /* RC_BASIS */
  2163. }
  2164. return 0;
  2165. }
  2166. /**
  2167. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  2168. * @args: device IDENTIFY data / SCSI command of interest.
  2169. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2170. *
  2171. * Simulate REPORT LUNS command.
  2172. *
  2173. * LOCKING:
  2174. * spin_lock_irqsave(host lock)
  2175. */
  2176. static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
  2177. {
  2178. VPRINTK("ENTER\n");
  2179. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  2180. return 0;
  2181. }
  2182. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  2183. {
  2184. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  2185. /* FIXME: not quite right; we don't want the
  2186. * translation of taskfile registers into
  2187. * a sense descriptors, since that's only
  2188. * correct for ATA, not ATAPI
  2189. */
  2190. ata_gen_passthru_sense(qc);
  2191. }
  2192. ata_qc_done(qc);
  2193. }
  2194. /* is it pointless to prefer PIO for "safety reasons"? */
  2195. static inline int ata_pio_use_silly(struct ata_port *ap)
  2196. {
  2197. return (ap->flags & ATA_FLAG_PIO_DMA);
  2198. }
  2199. static void atapi_request_sense(struct ata_queued_cmd *qc)
  2200. {
  2201. struct ata_port *ap = qc->ap;
  2202. struct scsi_cmnd *cmd = qc->scsicmd;
  2203. DPRINTK("ATAPI request sense\n");
  2204. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  2205. #ifdef CONFIG_ATA_SFF
  2206. if (ap->ops->sff_tf_read)
  2207. ap->ops->sff_tf_read(ap, &qc->tf);
  2208. #endif
  2209. /* fill these in, for the case where they are -not- overwritten */
  2210. cmd->sense_buffer[0] = 0x70;
  2211. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  2212. ata_qc_reinit(qc);
  2213. /* setup sg table and init transfer direction */
  2214. sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
  2215. ata_sg_init(qc, &qc->sgent, 1);
  2216. qc->dma_dir = DMA_FROM_DEVICE;
  2217. memset(&qc->cdb, 0, qc->dev->cdb_len);
  2218. qc->cdb[0] = REQUEST_SENSE;
  2219. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2220. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2221. qc->tf.command = ATA_CMD_PACKET;
  2222. if (ata_pio_use_silly(ap)) {
  2223. qc->tf.protocol = ATAPI_PROT_DMA;
  2224. qc->tf.feature |= ATAPI_PKT_DMA;
  2225. } else {
  2226. qc->tf.protocol = ATAPI_PROT_PIO;
  2227. qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
  2228. qc->tf.lbah = 0;
  2229. }
  2230. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  2231. qc->complete_fn = atapi_sense_complete;
  2232. ata_qc_issue(qc);
  2233. DPRINTK("EXIT\n");
  2234. }
  2235. /*
  2236. * ATAPI devices typically report zero for their SCSI version, and sometimes
  2237. * deviate from the spec WRT response data format. If SCSI version is
  2238. * reported as zero like normal, then we make the following fixups:
  2239. * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
  2240. * modern device.
  2241. * 2) Ensure response data format / ATAPI information are always correct.
  2242. */
  2243. static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
  2244. {
  2245. u8 buf[4];
  2246. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
  2247. if (buf[2] == 0) {
  2248. buf[2] = 0x5;
  2249. buf[3] = 0x32;
  2250. }
  2251. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
  2252. }
  2253. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  2254. {
  2255. struct scsi_cmnd *cmd = qc->scsicmd;
  2256. unsigned int err_mask = qc->err_mask;
  2257. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  2258. /* handle completion from new EH */
  2259. if (unlikely(qc->ap->ops->error_handler &&
  2260. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  2261. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  2262. /* FIXME: not quite right; we don't want the
  2263. * translation of taskfile registers into a
  2264. * sense descriptors, since that's only
  2265. * correct for ATA, not ATAPI
  2266. */
  2267. ata_gen_passthru_sense(qc);
  2268. }
  2269. /* SCSI EH automatically locks door if sdev->locked is
  2270. * set. Sometimes door lock request continues to
  2271. * fail, for example, when no media is present. This
  2272. * creates a loop - SCSI EH issues door lock which
  2273. * fails and gets invoked again to acquire sense data
  2274. * for the failed command.
  2275. *
  2276. * If door lock fails, always clear sdev->locked to
  2277. * avoid this infinite loop.
  2278. *
  2279. * This may happen before SCSI scan is complete. Make
  2280. * sure qc->dev->sdev isn't NULL before dereferencing.
  2281. */
  2282. if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
  2283. qc->dev->sdev->locked = 0;
  2284. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  2285. ata_qc_done(qc);
  2286. return;
  2287. }
  2288. /* successful completion or old EH failure path */
  2289. if (unlikely(err_mask & AC_ERR_DEV)) {
  2290. cmd->result = SAM_STAT_CHECK_CONDITION;
  2291. atapi_request_sense(qc);
  2292. return;
  2293. } else if (unlikely(err_mask)) {
  2294. /* FIXME: not quite right; we don't want the
  2295. * translation of taskfile registers into
  2296. * a sense descriptors, since that's only
  2297. * correct for ATA, not ATAPI
  2298. */
  2299. ata_gen_passthru_sense(qc);
  2300. } else {
  2301. if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
  2302. atapi_fixup_inquiry(cmd);
  2303. cmd->result = SAM_STAT_GOOD;
  2304. }
  2305. ata_qc_done(qc);
  2306. }
  2307. /**
  2308. * atapi_xlat - Initialize PACKET taskfile
  2309. * @qc: command structure to be initialized
  2310. *
  2311. * LOCKING:
  2312. * spin_lock_irqsave(host lock)
  2313. *
  2314. * RETURNS:
  2315. * Zero on success, non-zero on failure.
  2316. */
  2317. static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
  2318. {
  2319. struct scsi_cmnd *scmd = qc->scsicmd;
  2320. struct ata_device *dev = qc->dev;
  2321. int nodata = (scmd->sc_data_direction == DMA_NONE);
  2322. int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
  2323. unsigned int nbytes;
  2324. memset(qc->cdb, 0, dev->cdb_len);
  2325. memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
  2326. qc->complete_fn = atapi_qc_complete;
  2327. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2328. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2329. qc->tf.flags |= ATA_TFLAG_WRITE;
  2330. DPRINTK("direction: write\n");
  2331. }
  2332. qc->tf.command = ATA_CMD_PACKET;
  2333. ata_qc_set_pc_nbytes(qc);
  2334. /* check whether ATAPI DMA is safe */
  2335. if (!nodata && !using_pio && atapi_check_dma(qc))
  2336. using_pio = 1;
  2337. /* Some controller variants snoop this value for Packet
  2338. * transfers to do state machine and FIFO management. Thus we
  2339. * want to set it properly, and for DMA where it is
  2340. * effectively meaningless.
  2341. */
  2342. nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
  2343. /* Most ATAPI devices which honor transfer chunk size don't
  2344. * behave according to the spec when odd chunk size which
  2345. * matches the transfer length is specified. If the number of
  2346. * bytes to transfer is 2n+1. According to the spec, what
  2347. * should happen is to indicate that 2n+1 is going to be
  2348. * transferred and transfer 2n+2 bytes where the last byte is
  2349. * padding.
  2350. *
  2351. * In practice, this doesn't happen. ATAPI devices first
  2352. * indicate and transfer 2n bytes and then indicate and
  2353. * transfer 2 bytes where the last byte is padding.
  2354. *
  2355. * This inconsistency confuses several controllers which
  2356. * perform PIO using DMA such as Intel AHCIs and sil3124/32.
  2357. * These controllers use actual number of transferred bytes to
  2358. * update DMA pointer and transfer of 4n+2 bytes make those
  2359. * controller push DMA pointer by 4n+4 bytes because SATA data
  2360. * FISes are aligned to 4 bytes. This causes data corruption
  2361. * and buffer overrun.
  2362. *
  2363. * Always setting nbytes to even number solves this problem
  2364. * because then ATAPI devices don't have to split data at 2n
  2365. * boundaries.
  2366. */
  2367. if (nbytes & 0x1)
  2368. nbytes++;
  2369. qc->tf.lbam = (nbytes & 0xFF);
  2370. qc->tf.lbah = (nbytes >> 8);
  2371. if (nodata)
  2372. qc->tf.protocol = ATAPI_PROT_NODATA;
  2373. else if (using_pio)
  2374. qc->tf.protocol = ATAPI_PROT_PIO;
  2375. else {
  2376. /* DMA data xfer */
  2377. qc->tf.protocol = ATAPI_PROT_DMA;
  2378. qc->tf.feature |= ATAPI_PKT_DMA;
  2379. if ((dev->flags & ATA_DFLAG_DMADIR) &&
  2380. (scmd->sc_data_direction != DMA_TO_DEVICE))
  2381. /* some SATA bridges need us to indicate data xfer direction */
  2382. qc->tf.feature |= ATAPI_DMADIR;
  2383. }
  2384. /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
  2385. as ATAPI tape drives don't get this right otherwise */
  2386. return 0;
  2387. }
  2388. static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
  2389. {
  2390. if (!sata_pmp_attached(ap)) {
  2391. if (likely(devno >= 0 &&
  2392. devno < ata_link_max_devices(&ap->link)))
  2393. return &ap->link.device[devno];
  2394. } else {
  2395. if (likely(devno >= 0 &&
  2396. devno < ap->nr_pmp_links))
  2397. return &ap->pmp_link[devno].device[0];
  2398. }
  2399. return NULL;
  2400. }
  2401. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  2402. const struct scsi_device *scsidev)
  2403. {
  2404. int devno;
  2405. /* skip commands not addressed to targets we simulate */
  2406. if (!sata_pmp_attached(ap)) {
  2407. if (unlikely(scsidev->channel || scsidev->lun))
  2408. return NULL;
  2409. devno = scsidev->id;
  2410. } else {
  2411. if (unlikely(scsidev->id || scsidev->lun))
  2412. return NULL;
  2413. devno = scsidev->channel;
  2414. }
  2415. return ata_find_dev(ap, devno);
  2416. }
  2417. /**
  2418. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2419. * @ap: ATA port to which the device is attached
  2420. * @scsidev: SCSI device from which we derive the ATA device
  2421. *
  2422. * Given various information provided in struct scsi_cmnd,
  2423. * map that onto an ATA bus, and using that mapping
  2424. * determine which ata_device is associated with the
  2425. * SCSI command to be sent.
  2426. *
  2427. * LOCKING:
  2428. * spin_lock_irqsave(host lock)
  2429. *
  2430. * RETURNS:
  2431. * Associated ATA device, or %NULL if not found.
  2432. */
  2433. struct ata_device *
  2434. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2435. {
  2436. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2437. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2438. return NULL;
  2439. return dev;
  2440. }
  2441. /*
  2442. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2443. * @byte1: Byte 1 from pass-thru CDB.
  2444. *
  2445. * RETURNS:
  2446. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2447. */
  2448. static u8
  2449. ata_scsi_map_proto(u8 byte1)
  2450. {
  2451. switch((byte1 & 0x1e) >> 1) {
  2452. case 3: /* Non-data */
  2453. return ATA_PROT_NODATA;
  2454. case 6: /* DMA */
  2455. case 10: /* UDMA Data-in */
  2456. case 11: /* UDMA Data-Out */
  2457. return ATA_PROT_DMA;
  2458. case 4: /* PIO Data-in */
  2459. case 5: /* PIO Data-out */
  2460. return ATA_PROT_PIO;
  2461. case 12: /* FPDMA */
  2462. return ATA_PROT_NCQ;
  2463. case 0: /* Hard Reset */
  2464. case 1: /* SRST */
  2465. case 8: /* Device Diagnostic */
  2466. case 9: /* Device Reset */
  2467. case 7: /* DMA Queued */
  2468. case 15: /* Return Response Info */
  2469. default: /* Reserved */
  2470. break;
  2471. }
  2472. return ATA_PROT_UNKNOWN;
  2473. }
  2474. /**
  2475. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2476. * @qc: command structure to be initialized
  2477. *
  2478. * Handles either 12, 16, or 32-byte versions of the CDB.
  2479. *
  2480. * RETURNS:
  2481. * Zero on success, non-zero on failure.
  2482. */
  2483. static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
  2484. {
  2485. struct ata_taskfile *tf = &(qc->tf);
  2486. struct scsi_cmnd *scmd = qc->scsicmd;
  2487. struct ata_device *dev = qc->dev;
  2488. const u8 *cdb = scmd->cmnd;
  2489. u16 fp;
  2490. u16 cdb_offset = 0;
  2491. /* 7Fh variable length cmd means a ata pass-thru(32) */
  2492. if (cdb[0] == VARIABLE_LENGTH_CMD)
  2493. cdb_offset = 9;
  2494. tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
  2495. if (tf->protocol == ATA_PROT_UNKNOWN) {
  2496. fp = 1;
  2497. goto invalid_fld;
  2498. }
  2499. if ((cdb[2 + cdb_offset] & 0x3) == 0) {
  2500. /*
  2501. * When T_LENGTH is zero (No data is transferred), dir should
  2502. * be DMA_NONE.
  2503. */
  2504. if (scmd->sc_data_direction != DMA_NONE) {
  2505. fp = 2 + cdb_offset;
  2506. goto invalid_fld;
  2507. }
  2508. if (ata_is_ncq(tf->protocol))
  2509. tf->protocol = ATA_PROT_NCQ_NODATA;
  2510. }
  2511. /* enable LBA */
  2512. tf->flags |= ATA_TFLAG_LBA;
  2513. /*
  2514. * 12 and 16 byte CDBs use different offsets to
  2515. * provide the various register values.
  2516. */
  2517. if (cdb[0] == ATA_16) {
  2518. /*
  2519. * 16-byte CDB - may contain extended commands.
  2520. *
  2521. * If that is the case, copy the upper byte register values.
  2522. */
  2523. if (cdb[1] & 0x01) {
  2524. tf->hob_feature = cdb[3];
  2525. tf->hob_nsect = cdb[5];
  2526. tf->hob_lbal = cdb[7];
  2527. tf->hob_lbam = cdb[9];
  2528. tf->hob_lbah = cdb[11];
  2529. tf->flags |= ATA_TFLAG_LBA48;
  2530. } else
  2531. tf->flags &= ~ATA_TFLAG_LBA48;
  2532. /*
  2533. * Always copy low byte, device and command registers.
  2534. */
  2535. tf->feature = cdb[4];
  2536. tf->nsect = cdb[6];
  2537. tf->lbal = cdb[8];
  2538. tf->lbam = cdb[10];
  2539. tf->lbah = cdb[12];
  2540. tf->device = cdb[13];
  2541. tf->command = cdb[14];
  2542. } else if (cdb[0] == ATA_12) {
  2543. /*
  2544. * 12-byte CDB - incapable of extended commands.
  2545. */
  2546. tf->flags &= ~ATA_TFLAG_LBA48;
  2547. tf->feature = cdb[3];
  2548. tf->nsect = cdb[4];
  2549. tf->lbal = cdb[5];
  2550. tf->lbam = cdb[6];
  2551. tf->lbah = cdb[7];
  2552. tf->device = cdb[8];
  2553. tf->command = cdb[9];
  2554. } else {
  2555. /*
  2556. * 32-byte CDB - may contain extended command fields.
  2557. *
  2558. * If that is the case, copy the upper byte register values.
  2559. */
  2560. if (cdb[10] & 0x01) {
  2561. tf->hob_feature = cdb[20];
  2562. tf->hob_nsect = cdb[22];
  2563. tf->hob_lbal = cdb[16];
  2564. tf->hob_lbam = cdb[15];
  2565. tf->hob_lbah = cdb[14];
  2566. tf->flags |= ATA_TFLAG_LBA48;
  2567. } else
  2568. tf->flags &= ~ATA_TFLAG_LBA48;
  2569. tf->feature = cdb[21];
  2570. tf->nsect = cdb[23];
  2571. tf->lbal = cdb[19];
  2572. tf->lbam = cdb[18];
  2573. tf->lbah = cdb[17];
  2574. tf->device = cdb[24];
  2575. tf->command = cdb[25];
  2576. tf->auxiliary = get_unaligned_be32(&cdb[28]);
  2577. }
  2578. /* For NCQ commands copy the tag value */
  2579. if (ata_is_ncq(tf->protocol))
  2580. tf->nsect = qc->hw_tag << 3;
  2581. /* enforce correct master/slave bit */
  2582. tf->device = dev->devno ?
  2583. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2584. switch (tf->command) {
  2585. /* READ/WRITE LONG use a non-standard sect_size */
  2586. case ATA_CMD_READ_LONG:
  2587. case ATA_CMD_READ_LONG_ONCE:
  2588. case ATA_CMD_WRITE_LONG:
  2589. case ATA_CMD_WRITE_LONG_ONCE:
  2590. if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
  2591. fp = 1;
  2592. goto invalid_fld;
  2593. }
  2594. qc->sect_size = scsi_bufflen(scmd);
  2595. break;
  2596. /* commands using reported Logical Block size (e.g. 512 or 4K) */
  2597. case ATA_CMD_CFA_WRITE_NE:
  2598. case ATA_CMD_CFA_TRANS_SECT:
  2599. case ATA_CMD_CFA_WRITE_MULT_NE:
  2600. /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
  2601. case ATA_CMD_READ:
  2602. case ATA_CMD_READ_EXT:
  2603. case ATA_CMD_READ_QUEUED:
  2604. /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
  2605. case ATA_CMD_FPDMA_READ:
  2606. case ATA_CMD_READ_MULTI:
  2607. case ATA_CMD_READ_MULTI_EXT:
  2608. case ATA_CMD_PIO_READ:
  2609. case ATA_CMD_PIO_READ_EXT:
  2610. case ATA_CMD_READ_STREAM_DMA_EXT:
  2611. case ATA_CMD_READ_STREAM_EXT:
  2612. case ATA_CMD_VERIFY:
  2613. case ATA_CMD_VERIFY_EXT:
  2614. case ATA_CMD_WRITE:
  2615. case ATA_CMD_WRITE_EXT:
  2616. case ATA_CMD_WRITE_FUA_EXT:
  2617. case ATA_CMD_WRITE_QUEUED:
  2618. case ATA_CMD_WRITE_QUEUED_FUA_EXT:
  2619. case ATA_CMD_FPDMA_WRITE:
  2620. case ATA_CMD_WRITE_MULTI:
  2621. case ATA_CMD_WRITE_MULTI_EXT:
  2622. case ATA_CMD_WRITE_MULTI_FUA_EXT:
  2623. case ATA_CMD_PIO_WRITE:
  2624. case ATA_CMD_PIO_WRITE_EXT:
  2625. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  2626. case ATA_CMD_WRITE_STREAM_EXT:
  2627. qc->sect_size = scmd->device->sector_size;
  2628. break;
  2629. /* Everything else uses 512 byte "sectors" */
  2630. default:
  2631. qc->sect_size = ATA_SECT_SIZE;
  2632. }
  2633. /*
  2634. * Set flags so that all registers will be written, pass on
  2635. * write indication (used for PIO/DMA setup), result TF is
  2636. * copied back and we don't whine too much about its failure.
  2637. */
  2638. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2639. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2640. tf->flags |= ATA_TFLAG_WRITE;
  2641. qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
  2642. /*
  2643. * Set transfer length.
  2644. *
  2645. * TODO: find out if we need to do more here to
  2646. * cover scatter/gather case.
  2647. */
  2648. ata_qc_set_pc_nbytes(qc);
  2649. /* We may not issue DMA commands if no DMA mode is set */
  2650. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
  2651. fp = 1;
  2652. goto invalid_fld;
  2653. }
  2654. /* We may not issue NCQ commands to devices not supporting NCQ */
  2655. if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
  2656. fp = 1;
  2657. goto invalid_fld;
  2658. }
  2659. /* sanity check for pio multi commands */
  2660. if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
  2661. fp = 1;
  2662. goto invalid_fld;
  2663. }
  2664. if (is_multi_taskfile(tf)) {
  2665. unsigned int multi_count = 1 << (cdb[1] >> 5);
  2666. /* compare the passed through multi_count
  2667. * with the cached multi_count of libata
  2668. */
  2669. if (multi_count != dev->multi_count)
  2670. ata_dev_warn(dev, "invalid multi_count %u ignored\n",
  2671. multi_count);
  2672. }
  2673. /*
  2674. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2675. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2676. * by an update to hardware-specific registers for each
  2677. * controller (i.e. the reason for ->set_piomode(),
  2678. * ->set_dmamode(), and ->post_set_mode() hooks).
  2679. */
  2680. if (tf->command == ATA_CMD_SET_FEATURES &&
  2681. tf->feature == SETFEATURES_XFER) {
  2682. fp = (cdb[0] == ATA_16) ? 4 : 3;
  2683. goto invalid_fld;
  2684. }
  2685. /*
  2686. * Filter TPM commands by default. These provide an
  2687. * essentially uncontrolled encrypted "back door" between
  2688. * applications and the disk. Set libata.allow_tpm=1 if you
  2689. * have a real reason for wanting to use them. This ensures
  2690. * that installed software cannot easily mess stuff up without
  2691. * user intent. DVR type users will probably ship with this enabled
  2692. * for movie content management.
  2693. *
  2694. * Note that for ATA8 we can issue a DCS change and DCS freeze lock
  2695. * for this and should do in future but that it is not sufficient as
  2696. * DCS is an optional feature set. Thus we also do the software filter
  2697. * so that we comply with the TC consortium stated goal that the user
  2698. * can turn off TC features of their system.
  2699. */
  2700. if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
  2701. fp = (cdb[0] == ATA_16) ? 14 : 9;
  2702. goto invalid_fld;
  2703. }
  2704. return 0;
  2705. invalid_fld:
  2706. ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
  2707. return 1;
  2708. }
  2709. /**
  2710. * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
  2711. * @cmd: SCSI command being translated
  2712. * @trmax: Maximum number of entries that will fit in sector_size bytes.
  2713. * @sector: Starting sector
  2714. * @count: Total Range of request in logical sectors
  2715. *
  2716. * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
  2717. * descriptor.
  2718. *
  2719. * Upto 64 entries of the format:
  2720. * 63:48 Range Length
  2721. * 47:0 LBA
  2722. *
  2723. * Range Length of 0 is ignored.
  2724. * LBA's should be sorted order and not overlap.
  2725. *
  2726. * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
  2727. *
  2728. * Return: Number of bytes copied into sglist.
  2729. */
  2730. static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
  2731. u64 sector, u32 count)
  2732. {
  2733. struct scsi_device *sdp = cmd->device;
  2734. size_t len = sdp->sector_size;
  2735. size_t r;
  2736. __le64 *buf;
  2737. u32 i = 0;
  2738. unsigned long flags;
  2739. WARN_ON(len > ATA_SCSI_RBUF_SIZE);
  2740. if (len > ATA_SCSI_RBUF_SIZE)
  2741. len = ATA_SCSI_RBUF_SIZE;
  2742. spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
  2743. buf = ((void *)ata_scsi_rbuf);
  2744. memset(buf, 0, len);
  2745. while (i < trmax) {
  2746. u64 entry = sector |
  2747. ((u64)(count > 0xffff ? 0xffff : count) << 48);
  2748. buf[i++] = __cpu_to_le64(entry);
  2749. if (count <= 0xffff)
  2750. break;
  2751. count -= 0xffff;
  2752. sector += 0xffff;
  2753. }
  2754. r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
  2755. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
  2756. return r;
  2757. }
  2758. /**
  2759. * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
  2760. * @qc: Command to be translated
  2761. *
  2762. * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
  2763. * an SCT Write Same command.
  2764. * Based on WRITE SAME has the UNMAP flag:
  2765. *
  2766. * - When set translate to DSM TRIM
  2767. * - When clear translate to SCT Write Same
  2768. */
  2769. static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
  2770. {
  2771. struct ata_taskfile *tf = &qc->tf;
  2772. struct scsi_cmnd *scmd = qc->scsicmd;
  2773. struct scsi_device *sdp = scmd->device;
  2774. size_t len = sdp->sector_size;
  2775. struct ata_device *dev = qc->dev;
  2776. const u8 *cdb = scmd->cmnd;
  2777. u64 block;
  2778. u32 n_block;
  2779. const u32 trmax = len >> 3;
  2780. u32 size;
  2781. u16 fp;
  2782. u8 bp = 0xff;
  2783. u8 unmap = cdb[1] & 0x8;
  2784. /* we may not issue DMA commands if no DMA mode is set */
  2785. if (unlikely(!dev->dma_mode))
  2786. goto invalid_opcode;
  2787. /*
  2788. * We only allow sending this command through the block layer,
  2789. * as it modifies the DATA OUT buffer, which would corrupt user
  2790. * memory for SG_IO commands.
  2791. */
  2792. if (unlikely(blk_rq_is_passthrough(scmd->request)))
  2793. goto invalid_opcode;
  2794. if (unlikely(scmd->cmd_len < 16)) {
  2795. fp = 15;
  2796. goto invalid_fld;
  2797. }
  2798. scsi_16_lba_len(cdb, &block, &n_block);
  2799. if (!unmap ||
  2800. (dev->horkage & ATA_HORKAGE_NOTRIM) ||
  2801. !ata_id_has_trim(dev->id)) {
  2802. fp = 1;
  2803. bp = 3;
  2804. goto invalid_fld;
  2805. }
  2806. /* If the request is too large the cmd is invalid */
  2807. if (n_block > 0xffff * trmax) {
  2808. fp = 2;
  2809. goto invalid_fld;
  2810. }
  2811. /*
  2812. * WRITE SAME always has a sector sized buffer as payload, this
  2813. * should never be a multiple entry S/G list.
  2814. */
  2815. if (!scsi_sg_count(scmd))
  2816. goto invalid_param_len;
  2817. /*
  2818. * size must match sector size in bytes
  2819. * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
  2820. * is defined as number of 512 byte blocks to be transferred.
  2821. */
  2822. size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
  2823. if (size != len)
  2824. goto invalid_param_len;
  2825. if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
  2826. /* Newer devices support queued TRIM commands */
  2827. tf->protocol = ATA_PROT_NCQ;
  2828. tf->command = ATA_CMD_FPDMA_SEND;
  2829. tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
  2830. tf->nsect = qc->hw_tag << 3;
  2831. tf->hob_feature = (size / 512) >> 8;
  2832. tf->feature = size / 512;
  2833. tf->auxiliary = 1;
  2834. } else {
  2835. tf->protocol = ATA_PROT_DMA;
  2836. tf->hob_feature = 0;
  2837. tf->feature = ATA_DSM_TRIM;
  2838. tf->hob_nsect = (size / 512) >> 8;
  2839. tf->nsect = size / 512;
  2840. tf->command = ATA_CMD_DSM;
  2841. }
  2842. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
  2843. ATA_TFLAG_WRITE;
  2844. ata_qc_set_pc_nbytes(qc);
  2845. return 0;
  2846. invalid_fld:
  2847. ata_scsi_set_invalid_field(dev, scmd, fp, bp);
  2848. return 1;
  2849. invalid_param_len:
  2850. /* "Parameter list length error" */
  2851. ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  2852. return 1;
  2853. invalid_opcode:
  2854. /* "Invalid command operation code" */
  2855. ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
  2856. return 1;
  2857. }
  2858. /**
  2859. * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
  2860. * @args: device MAINTENANCE_IN data / SCSI command of interest.
  2861. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2862. *
  2863. * Yields a subset to satisfy scsi_report_opcode()
  2864. *
  2865. * LOCKING:
  2866. * spin_lock_irqsave(host lock)
  2867. */
  2868. static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
  2869. {
  2870. struct ata_device *dev = args->dev;
  2871. u8 *cdb = args->cmd->cmnd;
  2872. u8 supported = 0;
  2873. unsigned int err = 0;
  2874. if (cdb[2] != 1) {
  2875. ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
  2876. err = 2;
  2877. goto out;
  2878. }
  2879. switch (cdb[3]) {
  2880. case INQUIRY:
  2881. case MODE_SENSE:
  2882. case MODE_SENSE_10:
  2883. case READ_CAPACITY:
  2884. case SERVICE_ACTION_IN_16:
  2885. case REPORT_LUNS:
  2886. case REQUEST_SENSE:
  2887. case SYNCHRONIZE_CACHE:
  2888. case REZERO_UNIT:
  2889. case SEEK_6:
  2890. case SEEK_10:
  2891. case TEST_UNIT_READY:
  2892. case SEND_DIAGNOSTIC:
  2893. case MAINTENANCE_IN:
  2894. case READ_6:
  2895. case READ_10:
  2896. case READ_16:
  2897. case WRITE_6:
  2898. case WRITE_10:
  2899. case WRITE_16:
  2900. case ATA_12:
  2901. case ATA_16:
  2902. case VERIFY:
  2903. case VERIFY_16:
  2904. case MODE_SELECT:
  2905. case MODE_SELECT_10:
  2906. case START_STOP:
  2907. supported = 3;
  2908. break;
  2909. case ZBC_IN:
  2910. case ZBC_OUT:
  2911. if (ata_id_zoned_cap(dev->id) ||
  2912. dev->class == ATA_DEV_ZAC)
  2913. supported = 3;
  2914. break;
  2915. case SECURITY_PROTOCOL_IN:
  2916. case SECURITY_PROTOCOL_OUT:
  2917. if (dev->flags & ATA_DFLAG_TRUSTED)
  2918. supported = 3;
  2919. break;
  2920. default:
  2921. break;
  2922. }
  2923. out:
  2924. rbuf[1] = supported; /* supported */
  2925. return err;
  2926. }
  2927. /**
  2928. * ata_scsi_report_zones_complete - convert ATA output
  2929. * @qc: command structure returning the data
  2930. *
  2931. * Convert T-13 little-endian field representation into
  2932. * T-10 big-endian field representation.
  2933. * What a mess.
  2934. */
  2935. static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
  2936. {
  2937. struct scsi_cmnd *scmd = qc->scsicmd;
  2938. struct sg_mapping_iter miter;
  2939. unsigned long flags;
  2940. unsigned int bytes = 0;
  2941. sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
  2942. SG_MITER_TO_SG | SG_MITER_ATOMIC);
  2943. local_irq_save(flags);
  2944. while (sg_miter_next(&miter)) {
  2945. unsigned int offset = 0;
  2946. if (bytes == 0) {
  2947. char *hdr;
  2948. u32 list_length;
  2949. u64 max_lba, opt_lba;
  2950. u16 same;
  2951. /* Swizzle header */
  2952. hdr = miter.addr;
  2953. list_length = get_unaligned_le32(&hdr[0]);
  2954. same = get_unaligned_le16(&hdr[4]);
  2955. max_lba = get_unaligned_le64(&hdr[8]);
  2956. opt_lba = get_unaligned_le64(&hdr[16]);
  2957. put_unaligned_be32(list_length, &hdr[0]);
  2958. hdr[4] = same & 0xf;
  2959. put_unaligned_be64(max_lba, &hdr[8]);
  2960. put_unaligned_be64(opt_lba, &hdr[16]);
  2961. offset += 64;
  2962. bytes += 64;
  2963. }
  2964. while (offset < miter.length) {
  2965. char *rec;
  2966. u8 cond, type, non_seq, reset;
  2967. u64 size, start, wp;
  2968. /* Swizzle zone descriptor */
  2969. rec = miter.addr + offset;
  2970. type = rec[0] & 0xf;
  2971. cond = (rec[1] >> 4) & 0xf;
  2972. non_seq = (rec[1] & 2);
  2973. reset = (rec[1] & 1);
  2974. size = get_unaligned_le64(&rec[8]);
  2975. start = get_unaligned_le64(&rec[16]);
  2976. wp = get_unaligned_le64(&rec[24]);
  2977. rec[0] = type;
  2978. rec[1] = (cond << 4) | non_seq | reset;
  2979. put_unaligned_be64(size, &rec[8]);
  2980. put_unaligned_be64(start, &rec[16]);
  2981. put_unaligned_be64(wp, &rec[24]);
  2982. WARN_ON(offset + 64 > miter.length);
  2983. offset += 64;
  2984. bytes += 64;
  2985. }
  2986. }
  2987. sg_miter_stop(&miter);
  2988. local_irq_restore(flags);
  2989. ata_scsi_qc_complete(qc);
  2990. }
  2991. static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
  2992. {
  2993. struct ata_taskfile *tf = &qc->tf;
  2994. struct scsi_cmnd *scmd = qc->scsicmd;
  2995. const u8 *cdb = scmd->cmnd;
  2996. u16 sect, fp = (u16)-1;
  2997. u8 sa, options, bp = 0xff;
  2998. u64 block;
  2999. u32 n_block;
  3000. if (unlikely(scmd->cmd_len < 16)) {
  3001. ata_dev_warn(qc->dev, "invalid cdb length %d\n",
  3002. scmd->cmd_len);
  3003. fp = 15;
  3004. goto invalid_fld;
  3005. }
  3006. scsi_16_lba_len(cdb, &block, &n_block);
  3007. if (n_block != scsi_bufflen(scmd)) {
  3008. ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
  3009. n_block, scsi_bufflen(scmd));
  3010. goto invalid_param_len;
  3011. }
  3012. sa = cdb[1] & 0x1f;
  3013. if (sa != ZI_REPORT_ZONES) {
  3014. ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
  3015. fp = 1;
  3016. goto invalid_fld;
  3017. }
  3018. /*
  3019. * ZAC allows only for transfers in 512 byte blocks,
  3020. * and uses a 16 bit value for the transfer count.
  3021. */
  3022. if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
  3023. ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
  3024. goto invalid_param_len;
  3025. }
  3026. sect = n_block / 512;
  3027. options = cdb[14] & 0xbf;
  3028. if (ata_ncq_enabled(qc->dev) &&
  3029. ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
  3030. tf->protocol = ATA_PROT_NCQ;
  3031. tf->command = ATA_CMD_FPDMA_RECV;
  3032. tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
  3033. tf->nsect = qc->hw_tag << 3;
  3034. tf->feature = sect & 0xff;
  3035. tf->hob_feature = (sect >> 8) & 0xff;
  3036. tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
  3037. } else {
  3038. tf->command = ATA_CMD_ZAC_MGMT_IN;
  3039. tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
  3040. tf->protocol = ATA_PROT_DMA;
  3041. tf->hob_feature = options;
  3042. tf->hob_nsect = (sect >> 8) & 0xff;
  3043. tf->nsect = sect & 0xff;
  3044. }
  3045. tf->device = ATA_LBA;
  3046. tf->lbah = (block >> 16) & 0xff;
  3047. tf->lbam = (block >> 8) & 0xff;
  3048. tf->lbal = block & 0xff;
  3049. tf->hob_lbah = (block >> 40) & 0xff;
  3050. tf->hob_lbam = (block >> 32) & 0xff;
  3051. tf->hob_lbal = (block >> 24) & 0xff;
  3052. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  3053. qc->flags |= ATA_QCFLAG_RESULT_TF;
  3054. ata_qc_set_pc_nbytes(qc);
  3055. qc->complete_fn = ata_scsi_report_zones_complete;
  3056. return 0;
  3057. invalid_fld:
  3058. ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
  3059. return 1;
  3060. invalid_param_len:
  3061. /* "Parameter list length error" */
  3062. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  3063. return 1;
  3064. }
  3065. static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
  3066. {
  3067. struct ata_taskfile *tf = &qc->tf;
  3068. struct scsi_cmnd *scmd = qc->scsicmd;
  3069. struct ata_device *dev = qc->dev;
  3070. const u8 *cdb = scmd->cmnd;
  3071. u8 all, sa;
  3072. u64 block;
  3073. u32 n_block;
  3074. u16 fp = (u16)-1;
  3075. if (unlikely(scmd->cmd_len < 16)) {
  3076. fp = 15;
  3077. goto invalid_fld;
  3078. }
  3079. sa = cdb[1] & 0x1f;
  3080. if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
  3081. (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
  3082. fp = 1;
  3083. goto invalid_fld;
  3084. }
  3085. scsi_16_lba_len(cdb, &block, &n_block);
  3086. if (n_block) {
  3087. /*
  3088. * ZAC MANAGEMENT OUT doesn't define any length
  3089. */
  3090. goto invalid_param_len;
  3091. }
  3092. all = cdb[14] & 0x1;
  3093. if (all) {
  3094. /*
  3095. * Ignore the block address (zone ID) as defined by ZBC.
  3096. */
  3097. block = 0;
  3098. } else if (block >= dev->n_sectors) {
  3099. /*
  3100. * Block must be a valid zone ID (a zone start LBA).
  3101. */
  3102. fp = 2;
  3103. goto invalid_fld;
  3104. }
  3105. if (ata_ncq_enabled(qc->dev) &&
  3106. ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
  3107. tf->protocol = ATA_PROT_NCQ_NODATA;
  3108. tf->command = ATA_CMD_NCQ_NON_DATA;
  3109. tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
  3110. tf->nsect = qc->hw_tag << 3;
  3111. tf->auxiliary = sa | ((u16)all << 8);
  3112. } else {
  3113. tf->protocol = ATA_PROT_NODATA;
  3114. tf->command = ATA_CMD_ZAC_MGMT_OUT;
  3115. tf->feature = sa;
  3116. tf->hob_feature = all;
  3117. }
  3118. tf->lbah = (block >> 16) & 0xff;
  3119. tf->lbam = (block >> 8) & 0xff;
  3120. tf->lbal = block & 0xff;
  3121. tf->hob_lbah = (block >> 40) & 0xff;
  3122. tf->hob_lbam = (block >> 32) & 0xff;
  3123. tf->hob_lbal = (block >> 24) & 0xff;
  3124. tf->device = ATA_LBA;
  3125. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  3126. return 0;
  3127. invalid_fld:
  3128. ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
  3129. return 1;
  3130. invalid_param_len:
  3131. /* "Parameter list length error" */
  3132. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  3133. return 1;
  3134. }
  3135. /**
  3136. * ata_mselect_caching - Simulate MODE SELECT for caching info page
  3137. * @qc: Storage for translated ATA taskfile
  3138. * @buf: input buffer
  3139. * @len: number of valid bytes in the input buffer
  3140. * @fp: out parameter for the failed field on error
  3141. *
  3142. * Prepare a taskfile to modify caching information for the device.
  3143. *
  3144. * LOCKING:
  3145. * None.
  3146. */
  3147. static int ata_mselect_caching(struct ata_queued_cmd *qc,
  3148. const u8 *buf, int len, u16 *fp)
  3149. {
  3150. struct ata_taskfile *tf = &qc->tf;
  3151. struct ata_device *dev = qc->dev;
  3152. u8 mpage[CACHE_MPAGE_LEN];
  3153. u8 wce;
  3154. int i;
  3155. /*
  3156. * The first two bytes of def_cache_mpage are a header, so offsets
  3157. * in mpage are off by 2 compared to buf. Same for len.
  3158. */
  3159. if (len != CACHE_MPAGE_LEN - 2) {
  3160. if (len < CACHE_MPAGE_LEN - 2)
  3161. *fp = len;
  3162. else
  3163. *fp = CACHE_MPAGE_LEN - 2;
  3164. return -EINVAL;
  3165. }
  3166. wce = buf[0] & (1 << 2);
  3167. /*
  3168. * Check that read-only bits are not modified.
  3169. */
  3170. ata_msense_caching(dev->id, mpage, false);
  3171. for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
  3172. if (i == 0)
  3173. continue;
  3174. if (mpage[i + 2] != buf[i]) {
  3175. *fp = i;
  3176. return -EINVAL;
  3177. }
  3178. }
  3179. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  3180. tf->protocol = ATA_PROT_NODATA;
  3181. tf->nsect = 0;
  3182. tf->command = ATA_CMD_SET_FEATURES;
  3183. tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
  3184. return 0;
  3185. }
  3186. /**
  3187. * ata_mselect_control - Simulate MODE SELECT for control page
  3188. * @qc: Storage for translated ATA taskfile
  3189. * @buf: input buffer
  3190. * @len: number of valid bytes in the input buffer
  3191. * @fp: out parameter for the failed field on error
  3192. *
  3193. * Prepare a taskfile to modify caching information for the device.
  3194. *
  3195. * LOCKING:
  3196. * None.
  3197. */
  3198. static int ata_mselect_control(struct ata_queued_cmd *qc,
  3199. const u8 *buf, int len, u16 *fp)
  3200. {
  3201. struct ata_device *dev = qc->dev;
  3202. u8 mpage[CONTROL_MPAGE_LEN];
  3203. u8 d_sense;
  3204. int i;
  3205. /*
  3206. * The first two bytes of def_control_mpage are a header, so offsets
  3207. * in mpage are off by 2 compared to buf. Same for len.
  3208. */
  3209. if (len != CONTROL_MPAGE_LEN - 2) {
  3210. if (len < CONTROL_MPAGE_LEN - 2)
  3211. *fp = len;
  3212. else
  3213. *fp = CONTROL_MPAGE_LEN - 2;
  3214. return -EINVAL;
  3215. }
  3216. d_sense = buf[0] & (1 << 2);
  3217. /*
  3218. * Check that read-only bits are not modified.
  3219. */
  3220. ata_msense_control(dev, mpage, false);
  3221. for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
  3222. if (i == 0)
  3223. continue;
  3224. if (mpage[2 + i] != buf[i]) {
  3225. *fp = i;
  3226. return -EINVAL;
  3227. }
  3228. }
  3229. if (d_sense & (1 << 2))
  3230. dev->flags |= ATA_DFLAG_D_SENSE;
  3231. else
  3232. dev->flags &= ~ATA_DFLAG_D_SENSE;
  3233. return 0;
  3234. }
  3235. /**
  3236. * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
  3237. * @qc: Storage for translated ATA taskfile
  3238. *
  3239. * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
  3240. * Assume this is invoked for direct access devices (e.g. disks) only.
  3241. * There should be no block descriptor for other device types.
  3242. *
  3243. * LOCKING:
  3244. * spin_lock_irqsave(host lock)
  3245. */
  3246. static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
  3247. {
  3248. struct scsi_cmnd *scmd = qc->scsicmd;
  3249. const u8 *cdb = scmd->cmnd;
  3250. u8 pg, spg;
  3251. unsigned six_byte, pg_len, hdr_len, bd_len;
  3252. int len;
  3253. u16 fp = (u16)-1;
  3254. u8 bp = 0xff;
  3255. u8 buffer[64];
  3256. const u8 *p = buffer;
  3257. VPRINTK("ENTER\n");
  3258. six_byte = (cdb[0] == MODE_SELECT);
  3259. if (six_byte) {
  3260. if (scmd->cmd_len < 5) {
  3261. fp = 4;
  3262. goto invalid_fld;
  3263. }
  3264. len = cdb[4];
  3265. hdr_len = 4;
  3266. } else {
  3267. if (scmd->cmd_len < 9) {
  3268. fp = 8;
  3269. goto invalid_fld;
  3270. }
  3271. len = (cdb[7] << 8) + cdb[8];
  3272. hdr_len = 8;
  3273. }
  3274. /* We only support PF=1, SP=0. */
  3275. if ((cdb[1] & 0x11) != 0x10) {
  3276. fp = 1;
  3277. bp = (cdb[1] & 0x01) ? 1 : 5;
  3278. goto invalid_fld;
  3279. }
  3280. /* Test early for possible overrun. */
  3281. if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
  3282. goto invalid_param_len;
  3283. /* Move past header and block descriptors. */
  3284. if (len < hdr_len)
  3285. goto invalid_param_len;
  3286. if (!sg_copy_to_buffer(scsi_sglist(scmd), scsi_sg_count(scmd),
  3287. buffer, sizeof(buffer)))
  3288. goto invalid_param_len;
  3289. if (six_byte)
  3290. bd_len = p[3];
  3291. else
  3292. bd_len = (p[6] << 8) + p[7];
  3293. len -= hdr_len;
  3294. p += hdr_len;
  3295. if (len < bd_len)
  3296. goto invalid_param_len;
  3297. if (bd_len != 0 && bd_len != 8) {
  3298. fp = (six_byte) ? 3 : 6;
  3299. fp += bd_len + hdr_len;
  3300. goto invalid_param;
  3301. }
  3302. len -= bd_len;
  3303. p += bd_len;
  3304. if (len == 0)
  3305. goto skip;
  3306. /* Parse both possible formats for the mode page headers. */
  3307. pg = p[0] & 0x3f;
  3308. if (p[0] & 0x40) {
  3309. if (len < 4)
  3310. goto invalid_param_len;
  3311. spg = p[1];
  3312. pg_len = (p[2] << 8) | p[3];
  3313. p += 4;
  3314. len -= 4;
  3315. } else {
  3316. if (len < 2)
  3317. goto invalid_param_len;
  3318. spg = 0;
  3319. pg_len = p[1];
  3320. p += 2;
  3321. len -= 2;
  3322. }
  3323. /*
  3324. * No mode subpages supported (yet) but asking for _all_
  3325. * subpages may be valid
  3326. */
  3327. if (spg && (spg != ALL_SUB_MPAGES)) {
  3328. fp = (p[0] & 0x40) ? 1 : 0;
  3329. fp += hdr_len + bd_len;
  3330. goto invalid_param;
  3331. }
  3332. if (pg_len > len)
  3333. goto invalid_param_len;
  3334. switch (pg) {
  3335. case CACHE_MPAGE:
  3336. if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
  3337. fp += hdr_len + bd_len;
  3338. goto invalid_param;
  3339. }
  3340. break;
  3341. case CONTROL_MPAGE:
  3342. if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
  3343. fp += hdr_len + bd_len;
  3344. goto invalid_param;
  3345. } else {
  3346. goto skip; /* No ATA command to send */
  3347. }
  3348. break;
  3349. default: /* invalid page code */
  3350. fp = bd_len + hdr_len;
  3351. goto invalid_param;
  3352. }
  3353. /*
  3354. * Only one page has changeable data, so we only support setting one
  3355. * page at a time.
  3356. */
  3357. if (len > pg_len)
  3358. goto invalid_param;
  3359. return 0;
  3360. invalid_fld:
  3361. ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
  3362. return 1;
  3363. invalid_param:
  3364. ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
  3365. return 1;
  3366. invalid_param_len:
  3367. /* "Parameter list length error" */
  3368. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  3369. return 1;
  3370. skip:
  3371. scmd->result = SAM_STAT_GOOD;
  3372. return 1;
  3373. }
  3374. static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
  3375. {
  3376. if (len == 0)
  3377. return ATA_CMD_TRUSTED_NONDATA;
  3378. else if (send)
  3379. return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
  3380. else
  3381. return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
  3382. }
  3383. static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
  3384. {
  3385. struct scsi_cmnd *scmd = qc->scsicmd;
  3386. const u8 *cdb = scmd->cmnd;
  3387. struct ata_taskfile *tf = &qc->tf;
  3388. u8 secp = cdb[1];
  3389. bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
  3390. u16 spsp = get_unaligned_be16(&cdb[2]);
  3391. u32 len = get_unaligned_be32(&cdb[6]);
  3392. bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
  3393. /*
  3394. * We don't support the ATA "security" protocol.
  3395. */
  3396. if (secp == 0xef) {
  3397. ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
  3398. return 1;
  3399. }
  3400. if (cdb[4] & 7) { /* INC_512 */
  3401. if (len > 0xffff) {
  3402. ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
  3403. return 1;
  3404. }
  3405. } else {
  3406. if (len > 0x01fffe00) {
  3407. ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
  3408. return 1;
  3409. }
  3410. /* convert to the sector-based ATA addressing */
  3411. len = (len + 511) / 512;
  3412. }
  3413. tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
  3414. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
  3415. if (send)
  3416. tf->flags |= ATA_TFLAG_WRITE;
  3417. tf->command = ata_scsi_trusted_op(len, send, dma);
  3418. tf->feature = secp;
  3419. tf->lbam = spsp & 0xff;
  3420. tf->lbah = spsp >> 8;
  3421. if (len) {
  3422. tf->nsect = len & 0xff;
  3423. tf->lbal = len >> 8;
  3424. } else {
  3425. if (!send)
  3426. tf->lbah = (1 << 7);
  3427. }
  3428. ata_qc_set_pc_nbytes(qc);
  3429. return 0;
  3430. }
  3431. /**
  3432. * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
  3433. * @qc: Command to be translated
  3434. *
  3435. * Translate a SCSI variable length CDB to specified commands.
  3436. * It checks a service action value in CDB to call corresponding handler.
  3437. *
  3438. * RETURNS:
  3439. * Zero on success, non-zero on failure
  3440. *
  3441. */
  3442. static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
  3443. {
  3444. struct scsi_cmnd *scmd = qc->scsicmd;
  3445. const u8 *cdb = scmd->cmnd;
  3446. const u16 sa = get_unaligned_be16(&cdb[8]);
  3447. /*
  3448. * if service action represents a ata pass-thru(32) command,
  3449. * then pass it to ata_scsi_pass_thru handler.
  3450. */
  3451. if (sa == ATA_32)
  3452. return ata_scsi_pass_thru(qc);
  3453. /* unsupported service action */
  3454. return 1;
  3455. }
  3456. /**
  3457. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  3458. * @dev: ATA device
  3459. * @cmd: SCSI command opcode to consider
  3460. *
  3461. * Look up the SCSI command given, and determine whether the
  3462. * SCSI command is to be translated or simulated.
  3463. *
  3464. * RETURNS:
  3465. * Pointer to translation function if possible, %NULL if not.
  3466. */
  3467. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  3468. {
  3469. switch (cmd) {
  3470. case READ_6:
  3471. case READ_10:
  3472. case READ_16:
  3473. case WRITE_6:
  3474. case WRITE_10:
  3475. case WRITE_16:
  3476. return ata_scsi_rw_xlat;
  3477. case WRITE_SAME_16:
  3478. return ata_scsi_write_same_xlat;
  3479. case SYNCHRONIZE_CACHE:
  3480. if (ata_try_flush_cache(dev))
  3481. return ata_scsi_flush_xlat;
  3482. break;
  3483. case VERIFY:
  3484. case VERIFY_16:
  3485. return ata_scsi_verify_xlat;
  3486. case ATA_12:
  3487. case ATA_16:
  3488. return ata_scsi_pass_thru;
  3489. case VARIABLE_LENGTH_CMD:
  3490. return ata_scsi_var_len_cdb_xlat;
  3491. case MODE_SELECT:
  3492. case MODE_SELECT_10:
  3493. return ata_scsi_mode_select_xlat;
  3494. break;
  3495. case ZBC_IN:
  3496. return ata_scsi_zbc_in_xlat;
  3497. case ZBC_OUT:
  3498. return ata_scsi_zbc_out_xlat;
  3499. case SECURITY_PROTOCOL_IN:
  3500. case SECURITY_PROTOCOL_OUT:
  3501. if (!(dev->flags & ATA_DFLAG_TRUSTED))
  3502. break;
  3503. return ata_scsi_security_inout_xlat;
  3504. case START_STOP:
  3505. return ata_scsi_start_stop_xlat;
  3506. }
  3507. return NULL;
  3508. }
  3509. /**
  3510. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  3511. * @ap: ATA port to which the command was being sent
  3512. * @cmd: SCSI command to dump
  3513. *
  3514. * Prints the contents of a SCSI command via printk().
  3515. */
  3516. void ata_scsi_dump_cdb(struct ata_port *ap, struct scsi_cmnd *cmd)
  3517. {
  3518. #ifdef ATA_VERBOSE_DEBUG
  3519. struct scsi_device *scsidev = cmd->device;
  3520. VPRINTK("CDB (%u:%d,%d,%lld) %9ph\n",
  3521. ap->print_id,
  3522. scsidev->channel, scsidev->id, scsidev->lun,
  3523. cmd->cmnd);
  3524. #endif
  3525. }
  3526. int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, struct ata_device *dev)
  3527. {
  3528. u8 scsi_op = scmd->cmnd[0];
  3529. ata_xlat_func_t xlat_func;
  3530. int rc = 0;
  3531. if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
  3532. if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
  3533. goto bad_cdb_len;
  3534. xlat_func = ata_get_xlat_func(dev, scsi_op);
  3535. } else {
  3536. if (unlikely(!scmd->cmd_len))
  3537. goto bad_cdb_len;
  3538. xlat_func = NULL;
  3539. if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
  3540. /* relay SCSI command to ATAPI device */
  3541. int len = COMMAND_SIZE(scsi_op);
  3542. if (unlikely(len > scmd->cmd_len ||
  3543. len > dev->cdb_len ||
  3544. scmd->cmd_len > ATAPI_CDB_LEN))
  3545. goto bad_cdb_len;
  3546. xlat_func = atapi_xlat;
  3547. } else {
  3548. /* ATA_16 passthru, treat as an ATA command */
  3549. if (unlikely(scmd->cmd_len > 16))
  3550. goto bad_cdb_len;
  3551. xlat_func = ata_get_xlat_func(dev, scsi_op);
  3552. }
  3553. }
  3554. if (xlat_func)
  3555. rc = ata_scsi_translate(dev, scmd, xlat_func);
  3556. else
  3557. ata_scsi_simulate(dev, scmd);
  3558. return rc;
  3559. bad_cdb_len:
  3560. DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
  3561. scmd->cmd_len, scsi_op, dev->cdb_len);
  3562. scmd->result = DID_ERROR << 16;
  3563. scmd->scsi_done(scmd);
  3564. return 0;
  3565. }
  3566. /**
  3567. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  3568. * @shost: SCSI host of command to be sent
  3569. * @cmd: SCSI command to be sent
  3570. *
  3571. * In some cases, this function translates SCSI commands into
  3572. * ATA taskfiles, and queues the taskfiles to be sent to
  3573. * hardware. In other cases, this function simulates a
  3574. * SCSI device by evaluating and responding to certain
  3575. * SCSI commands. This creates the overall effect of
  3576. * ATA and ATAPI devices appearing as SCSI devices.
  3577. *
  3578. * LOCKING:
  3579. * ATA host lock
  3580. *
  3581. * RETURNS:
  3582. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3583. * 0 otherwise.
  3584. */
  3585. int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  3586. {
  3587. struct ata_port *ap;
  3588. struct ata_device *dev;
  3589. struct scsi_device *scsidev = cmd->device;
  3590. int rc = 0;
  3591. unsigned long irq_flags;
  3592. ap = ata_shost_to_port(shost);
  3593. spin_lock_irqsave(ap->lock, irq_flags);
  3594. ata_scsi_dump_cdb(ap, cmd);
  3595. dev = ata_scsi_find_dev(ap, scsidev);
  3596. if (likely(dev))
  3597. rc = __ata_scsi_queuecmd(cmd, dev);
  3598. else {
  3599. cmd->result = (DID_BAD_TARGET << 16);
  3600. cmd->scsi_done(cmd);
  3601. }
  3602. spin_unlock_irqrestore(ap->lock, irq_flags);
  3603. return rc;
  3604. }
  3605. EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
  3606. /**
  3607. * ata_scsi_simulate - simulate SCSI command on ATA device
  3608. * @dev: the target device
  3609. * @cmd: SCSI command being sent to device.
  3610. *
  3611. * Interprets and directly executes a select list of SCSI commands
  3612. * that can be handled internally.
  3613. *
  3614. * LOCKING:
  3615. * spin_lock_irqsave(host lock)
  3616. */
  3617. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
  3618. {
  3619. struct ata_scsi_args args;
  3620. const u8 *scsicmd = cmd->cmnd;
  3621. u8 tmp8;
  3622. args.dev = dev;
  3623. args.id = dev->id;
  3624. args.cmd = cmd;
  3625. switch(scsicmd[0]) {
  3626. case INQUIRY:
  3627. if (scsicmd[1] & 2) /* is CmdDt set? */
  3628. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3629. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  3630. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  3631. else switch (scsicmd[2]) {
  3632. case 0x00:
  3633. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  3634. break;
  3635. case 0x80:
  3636. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  3637. break;
  3638. case 0x83:
  3639. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  3640. break;
  3641. case 0x89:
  3642. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
  3643. break;
  3644. case 0xb0:
  3645. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
  3646. break;
  3647. case 0xb1:
  3648. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
  3649. break;
  3650. case 0xb2:
  3651. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
  3652. break;
  3653. case 0xb6:
  3654. if (dev->flags & ATA_DFLAG_ZAC) {
  3655. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
  3656. break;
  3657. }
  3658. fallthrough;
  3659. default:
  3660. ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
  3661. break;
  3662. }
  3663. break;
  3664. case MODE_SENSE:
  3665. case MODE_SENSE_10:
  3666. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  3667. break;
  3668. case READ_CAPACITY:
  3669. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3670. break;
  3671. case SERVICE_ACTION_IN_16:
  3672. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  3673. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3674. else
  3675. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3676. break;
  3677. case REPORT_LUNS:
  3678. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  3679. break;
  3680. case REQUEST_SENSE:
  3681. ata_scsi_set_sense(dev, cmd, 0, 0, 0);
  3682. cmd->result = (DRIVER_SENSE << 24);
  3683. break;
  3684. /* if we reach this, then writeback caching is disabled,
  3685. * turning this into a no-op.
  3686. */
  3687. case SYNCHRONIZE_CACHE:
  3688. fallthrough;
  3689. /* no-op's, complete with success */
  3690. case REZERO_UNIT:
  3691. case SEEK_6:
  3692. case SEEK_10:
  3693. case TEST_UNIT_READY:
  3694. break;
  3695. case SEND_DIAGNOSTIC:
  3696. tmp8 = scsicmd[1] & ~(1 << 3);
  3697. if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
  3698. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3699. break;
  3700. case MAINTENANCE_IN:
  3701. if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
  3702. ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
  3703. else
  3704. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3705. break;
  3706. /* all other commands */
  3707. default:
  3708. ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  3709. /* "Invalid command operation code" */
  3710. break;
  3711. }
  3712. cmd->scsi_done(cmd);
  3713. }
  3714. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
  3715. {
  3716. int i, rc;
  3717. for (i = 0; i < host->n_ports; i++) {
  3718. struct ata_port *ap = host->ports[i];
  3719. struct Scsi_Host *shost;
  3720. rc = -ENOMEM;
  3721. shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
  3722. if (!shost)
  3723. goto err_alloc;
  3724. shost->eh_noresume = 1;
  3725. *(struct ata_port **)&shost->hostdata[0] = ap;
  3726. ap->scsi_host = shost;
  3727. shost->transportt = ata_scsi_transport_template;
  3728. shost->unique_id = ap->print_id;
  3729. shost->max_id = 16;
  3730. shost->max_lun = 1;
  3731. shost->max_channel = 1;
  3732. shost->max_cmd_len = 32;
  3733. /* Schedule policy is determined by ->qc_defer()
  3734. * callback and it needs to see every deferred qc.
  3735. * Set host_blocked to 1 to prevent SCSI midlayer from
  3736. * automatically deferring requests.
  3737. */
  3738. shost->max_host_blocked = 1;
  3739. rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev);
  3740. if (rc)
  3741. goto err_alloc;
  3742. }
  3743. return 0;
  3744. err_alloc:
  3745. while (--i >= 0) {
  3746. struct Scsi_Host *shost = host->ports[i]->scsi_host;
  3747. /* scsi_host_put() is in ata_devres_release() */
  3748. scsi_remove_host(shost);
  3749. }
  3750. return rc;
  3751. }
  3752. #ifdef CONFIG_OF
  3753. static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
  3754. {
  3755. struct scsi_device *sdev = dev->sdev;
  3756. struct device *d = ap->host->dev;
  3757. struct device_node *np = d->of_node;
  3758. struct device_node *child;
  3759. for_each_available_child_of_node(np, child) {
  3760. int ret;
  3761. u32 val;
  3762. ret = of_property_read_u32(child, "reg", &val);
  3763. if (ret)
  3764. continue;
  3765. if (val == dev->devno) {
  3766. dev_dbg(d, "found matching device node\n");
  3767. sdev->sdev_gendev.of_node = child;
  3768. return;
  3769. }
  3770. }
  3771. }
  3772. #else
  3773. static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
  3774. {
  3775. }
  3776. #endif
  3777. void ata_scsi_scan_host(struct ata_port *ap, int sync)
  3778. {
  3779. int tries = 5;
  3780. struct ata_device *last_failed_dev = NULL;
  3781. struct ata_link *link;
  3782. struct ata_device *dev;
  3783. repeat:
  3784. ata_for_each_link(link, ap, EDGE) {
  3785. ata_for_each_dev(dev, link, ENABLED) {
  3786. struct scsi_device *sdev;
  3787. int channel = 0, id = 0;
  3788. if (dev->sdev)
  3789. continue;
  3790. if (ata_is_host_link(link))
  3791. id = dev->devno;
  3792. else
  3793. channel = link->pmp;
  3794. sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
  3795. NULL);
  3796. if (!IS_ERR(sdev)) {
  3797. dev->sdev = sdev;
  3798. ata_scsi_assign_ofnode(dev, ap);
  3799. scsi_device_put(sdev);
  3800. } else {
  3801. dev->sdev = NULL;
  3802. }
  3803. }
  3804. }
  3805. /* If we scanned while EH was in progress or allocation
  3806. * failure occurred, scan would have failed silently. Check
  3807. * whether all devices are attached.
  3808. */
  3809. ata_for_each_link(link, ap, EDGE) {
  3810. ata_for_each_dev(dev, link, ENABLED) {
  3811. if (!dev->sdev)
  3812. goto exit_loop;
  3813. }
  3814. }
  3815. exit_loop:
  3816. if (!link)
  3817. return;
  3818. /* we're missing some SCSI devices */
  3819. if (sync) {
  3820. /* If caller requested synchrnous scan && we've made
  3821. * any progress, sleep briefly and repeat.
  3822. */
  3823. if (dev != last_failed_dev) {
  3824. msleep(100);
  3825. last_failed_dev = dev;
  3826. goto repeat;
  3827. }
  3828. /* We might be failing to detect boot device, give it
  3829. * a few more chances.
  3830. */
  3831. if (--tries) {
  3832. msleep(100);
  3833. goto repeat;
  3834. }
  3835. ata_port_err(ap,
  3836. "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
  3837. }
  3838. queue_delayed_work(system_long_wq, &ap->hotplug_task,
  3839. round_jiffies_relative(HZ));
  3840. }
  3841. /**
  3842. * ata_scsi_offline_dev - offline attached SCSI device
  3843. * @dev: ATA device to offline attached SCSI device for
  3844. *
  3845. * This function is called from ata_eh_hotplug() and responsible
  3846. * for taking the SCSI device attached to @dev offline. This
  3847. * function is called with host lock which protects dev->sdev
  3848. * against clearing.
  3849. *
  3850. * LOCKING:
  3851. * spin_lock_irqsave(host lock)
  3852. *
  3853. * RETURNS:
  3854. * 1 if attached SCSI device exists, 0 otherwise.
  3855. */
  3856. int ata_scsi_offline_dev(struct ata_device *dev)
  3857. {
  3858. if (dev->sdev) {
  3859. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  3860. return 1;
  3861. }
  3862. return 0;
  3863. }
  3864. /**
  3865. * ata_scsi_remove_dev - remove attached SCSI device
  3866. * @dev: ATA device to remove attached SCSI device for
  3867. *
  3868. * This function is called from ata_eh_scsi_hotplug() and
  3869. * responsible for removing the SCSI device attached to @dev.
  3870. *
  3871. * LOCKING:
  3872. * Kernel thread context (may sleep).
  3873. */
  3874. static void ata_scsi_remove_dev(struct ata_device *dev)
  3875. {
  3876. struct ata_port *ap = dev->link->ap;
  3877. struct scsi_device *sdev;
  3878. unsigned long flags;
  3879. /* Alas, we need to grab scan_mutex to ensure SCSI device
  3880. * state doesn't change underneath us and thus
  3881. * scsi_device_get() always succeeds. The mutex locking can
  3882. * be removed if there is __scsi_device_get() interface which
  3883. * increments reference counts regardless of device state.
  3884. */
  3885. mutex_lock(&ap->scsi_host->scan_mutex);
  3886. spin_lock_irqsave(ap->lock, flags);
  3887. /* clearing dev->sdev is protected by host lock */
  3888. sdev = dev->sdev;
  3889. dev->sdev = NULL;
  3890. if (sdev) {
  3891. /* If user initiated unplug races with us, sdev can go
  3892. * away underneath us after the host lock and
  3893. * scan_mutex are released. Hold onto it.
  3894. */
  3895. if (scsi_device_get(sdev) == 0) {
  3896. /* The following ensures the attached sdev is
  3897. * offline on return from ata_scsi_offline_dev()
  3898. * regardless it wins or loses the race
  3899. * against this function.
  3900. */
  3901. scsi_device_set_state(sdev, SDEV_OFFLINE);
  3902. } else {
  3903. WARN_ON(1);
  3904. sdev = NULL;
  3905. }
  3906. }
  3907. spin_unlock_irqrestore(ap->lock, flags);
  3908. mutex_unlock(&ap->scsi_host->scan_mutex);
  3909. if (sdev) {
  3910. ata_dev_info(dev, "detaching (SCSI %s)\n",
  3911. dev_name(&sdev->sdev_gendev));
  3912. scsi_remove_device(sdev);
  3913. scsi_device_put(sdev);
  3914. }
  3915. }
  3916. static void ata_scsi_handle_link_detach(struct ata_link *link)
  3917. {
  3918. struct ata_port *ap = link->ap;
  3919. struct ata_device *dev;
  3920. ata_for_each_dev(dev, link, ALL) {
  3921. unsigned long flags;
  3922. if (!(dev->flags & ATA_DFLAG_DETACHED))
  3923. continue;
  3924. spin_lock_irqsave(ap->lock, flags);
  3925. dev->flags &= ~ATA_DFLAG_DETACHED;
  3926. spin_unlock_irqrestore(ap->lock, flags);
  3927. if (zpodd_dev_enabled(dev))
  3928. zpodd_exit(dev);
  3929. ata_scsi_remove_dev(dev);
  3930. }
  3931. }
  3932. /**
  3933. * ata_scsi_media_change_notify - send media change event
  3934. * @dev: Pointer to the disk device with media change event
  3935. *
  3936. * Tell the block layer to send a media change notification
  3937. * event.
  3938. *
  3939. * LOCKING:
  3940. * spin_lock_irqsave(host lock)
  3941. */
  3942. void ata_scsi_media_change_notify(struct ata_device *dev)
  3943. {
  3944. if (dev->sdev)
  3945. sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
  3946. GFP_ATOMIC);
  3947. }
  3948. /**
  3949. * ata_scsi_hotplug - SCSI part of hotplug
  3950. * @work: Pointer to ATA port to perform SCSI hotplug on
  3951. *
  3952. * Perform SCSI part of hotplug. It's executed from a separate
  3953. * workqueue after EH completes. This is necessary because SCSI
  3954. * hot plugging requires working EH and hot unplugging is
  3955. * synchronized with hot plugging with a mutex.
  3956. *
  3957. * LOCKING:
  3958. * Kernel thread context (may sleep).
  3959. */
  3960. void ata_scsi_hotplug(struct work_struct *work)
  3961. {
  3962. struct ata_port *ap =
  3963. container_of(work, struct ata_port, hotplug_task.work);
  3964. int i;
  3965. if (ap->pflags & ATA_PFLAG_UNLOADING) {
  3966. DPRINTK("ENTER/EXIT - unloading\n");
  3967. return;
  3968. }
  3969. DPRINTK("ENTER\n");
  3970. mutex_lock(&ap->scsi_scan_mutex);
  3971. /* Unplug detached devices. We cannot use link iterator here
  3972. * because PMP links have to be scanned even if PMP is
  3973. * currently not attached. Iterate manually.
  3974. */
  3975. ata_scsi_handle_link_detach(&ap->link);
  3976. if (ap->pmp_link)
  3977. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  3978. ata_scsi_handle_link_detach(&ap->pmp_link[i]);
  3979. /* scan for new ones */
  3980. ata_scsi_scan_host(ap, 0);
  3981. mutex_unlock(&ap->scsi_scan_mutex);
  3982. DPRINTK("EXIT\n");
  3983. }
  3984. /**
  3985. * ata_scsi_user_scan - indication for user-initiated bus scan
  3986. * @shost: SCSI host to scan
  3987. * @channel: Channel to scan
  3988. * @id: ID to scan
  3989. * @lun: LUN to scan
  3990. *
  3991. * This function is called when user explicitly requests bus
  3992. * scan. Set probe pending flag and invoke EH.
  3993. *
  3994. * LOCKING:
  3995. * SCSI layer (we don't care)
  3996. *
  3997. * RETURNS:
  3998. * Zero.
  3999. */
  4000. int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  4001. unsigned int id, u64 lun)
  4002. {
  4003. struct ata_port *ap = ata_shost_to_port(shost);
  4004. unsigned long flags;
  4005. int devno, rc = 0;
  4006. if (!ap->ops->error_handler)
  4007. return -EOPNOTSUPP;
  4008. if (lun != SCAN_WILD_CARD && lun)
  4009. return -EINVAL;
  4010. if (!sata_pmp_attached(ap)) {
  4011. if (channel != SCAN_WILD_CARD && channel)
  4012. return -EINVAL;
  4013. devno = id;
  4014. } else {
  4015. if (id != SCAN_WILD_CARD && id)
  4016. return -EINVAL;
  4017. devno = channel;
  4018. }
  4019. spin_lock_irqsave(ap->lock, flags);
  4020. if (devno == SCAN_WILD_CARD) {
  4021. struct ata_link *link;
  4022. ata_for_each_link(link, ap, EDGE) {
  4023. struct ata_eh_info *ehi = &link->eh_info;
  4024. ehi->probe_mask |= ATA_ALL_DEVICES;
  4025. ehi->action |= ATA_EH_RESET;
  4026. }
  4027. } else {
  4028. struct ata_device *dev = ata_find_dev(ap, devno);
  4029. if (dev) {
  4030. struct ata_eh_info *ehi = &dev->link->eh_info;
  4031. ehi->probe_mask |= 1 << dev->devno;
  4032. ehi->action |= ATA_EH_RESET;
  4033. } else
  4034. rc = -EINVAL;
  4035. }
  4036. if (rc == 0) {
  4037. ata_port_schedule_eh(ap);
  4038. spin_unlock_irqrestore(ap->lock, flags);
  4039. ata_port_wait_eh(ap);
  4040. } else
  4041. spin_unlock_irqrestore(ap->lock, flags);
  4042. return rc;
  4043. }
  4044. /**
  4045. * ata_scsi_dev_rescan - initiate scsi_rescan_device()
  4046. * @work: Pointer to ATA port to perform scsi_rescan_device()
  4047. *
  4048. * After ATA pass thru (SAT) commands are executed successfully,
  4049. * libata need to propagate the changes to SCSI layer.
  4050. *
  4051. * LOCKING:
  4052. * Kernel thread context (may sleep).
  4053. */
  4054. void ata_scsi_dev_rescan(struct work_struct *work)
  4055. {
  4056. struct ata_port *ap =
  4057. container_of(work, struct ata_port, scsi_rescan_task);
  4058. struct ata_link *link;
  4059. struct ata_device *dev;
  4060. unsigned long flags;
  4061. mutex_lock(&ap->scsi_scan_mutex);
  4062. spin_lock_irqsave(ap->lock, flags);
  4063. ata_for_each_link(link, ap, EDGE) {
  4064. ata_for_each_dev(dev, link, ENABLED) {
  4065. struct scsi_device *sdev = dev->sdev;
  4066. if (!sdev)
  4067. continue;
  4068. if (scsi_device_get(sdev))
  4069. continue;
  4070. spin_unlock_irqrestore(ap->lock, flags);
  4071. scsi_rescan_device(&(sdev->sdev_gendev));
  4072. scsi_device_put(sdev);
  4073. spin_lock_irqsave(ap->lock, flags);
  4074. }
  4075. }
  4076. spin_unlock_irqrestore(ap->lock, flags);
  4077. mutex_unlock(&ap->scsi_scan_mutex);
  4078. }