libata-scsi.c 128 KB

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