libata-scsi.c 126 KB

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