libata-scsi.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150
  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. /**
  1566. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  1567. * @qc: Storage for translated ATA taskfile
  1568. *
  1569. * Converts any of six SCSI read/write commands into the
  1570. * ATA counterpart, including starting sector (LBA),
  1571. * sector count, and taking into account the device's LBA48
  1572. * support.
  1573. *
  1574. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  1575. * %WRITE_16 are currently supported.
  1576. *
  1577. * LOCKING:
  1578. * spin_lock_irqsave(host lock)
  1579. *
  1580. * RETURNS:
  1581. * Zero on success, non-zero on error.
  1582. */
  1583. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
  1584. {
  1585. struct scsi_cmnd *scmd = qc->scsicmd;
  1586. const u8 *cdb = scmd->cmnd;
  1587. struct request *rq = scmd->request;
  1588. int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
  1589. unsigned int tf_flags = 0;
  1590. u64 block;
  1591. u32 n_block;
  1592. int rc;
  1593. u16 fp = 0;
  1594. if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
  1595. tf_flags |= ATA_TFLAG_WRITE;
  1596. /* Calculate the SCSI LBA, transfer length and FUA. */
  1597. switch (cdb[0]) {
  1598. case READ_10:
  1599. case WRITE_10:
  1600. if (unlikely(scmd->cmd_len < 10)) {
  1601. fp = 9;
  1602. goto invalid_fld;
  1603. }
  1604. scsi_10_lba_len(cdb, &block, &n_block);
  1605. if (cdb[1] & (1 << 3))
  1606. tf_flags |= ATA_TFLAG_FUA;
  1607. break;
  1608. case READ_6:
  1609. case WRITE_6:
  1610. if (unlikely(scmd->cmd_len < 6)) {
  1611. fp = 5;
  1612. goto invalid_fld;
  1613. }
  1614. scsi_6_lba_len(cdb, &block, &n_block);
  1615. /* for 6-byte r/w commands, transfer length 0
  1616. * means 256 blocks of data, not 0 block.
  1617. */
  1618. if (!n_block)
  1619. n_block = 256;
  1620. break;
  1621. case READ_16:
  1622. case WRITE_16:
  1623. if (unlikely(scmd->cmd_len < 16)) {
  1624. fp = 15;
  1625. goto invalid_fld;
  1626. }
  1627. scsi_16_lba_len(cdb, &block, &n_block);
  1628. if (cdb[1] & (1 << 3))
  1629. tf_flags |= ATA_TFLAG_FUA;
  1630. break;
  1631. default:
  1632. DPRINTK("no-byte command\n");
  1633. fp = 0;
  1634. goto invalid_fld;
  1635. }
  1636. /* Check and compose ATA command */
  1637. if (!n_block)
  1638. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  1639. * length 0 means transfer 0 block of data.
  1640. * However, for ATA R/W commands, sector count 0 means
  1641. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  1642. *
  1643. * WARNING: one or two older ATA drives treat 0 as 0...
  1644. */
  1645. goto nothing_to_do;
  1646. qc->flags |= ATA_QCFLAG_IO;
  1647. qc->nbytes = n_block * scmd->device->sector_size;
  1648. rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
  1649. qc->hw_tag, class);
  1650. if (likely(rc == 0))
  1651. return 0;
  1652. if (rc == -ERANGE)
  1653. goto out_of_range;
  1654. /* treat all other errors as -EINVAL, fall through */
  1655. invalid_fld:
  1656. ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
  1657. return 1;
  1658. out_of_range:
  1659. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1660. /* "Logical Block Address out of range" */
  1661. return 1;
  1662. nothing_to_do:
  1663. scmd->result = SAM_STAT_GOOD;
  1664. return 1;
  1665. }
  1666. static void ata_qc_done(struct ata_queued_cmd *qc)
  1667. {
  1668. struct scsi_cmnd *cmd = qc->scsicmd;
  1669. void (*done)(struct scsi_cmnd *) = qc->scsidone;
  1670. ata_qc_free(qc);
  1671. done(cmd);
  1672. }
  1673. static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  1674. {
  1675. struct ata_port *ap = qc->ap;
  1676. struct scsi_cmnd *cmd = qc->scsicmd;
  1677. u8 *cdb = cmd->cmnd;
  1678. int need_sense = (qc->err_mask != 0);
  1679. /* For ATA pass thru (SAT) commands, generate a sense block if
  1680. * user mandated it or if there's an error. Note that if we
  1681. * generate because the user forced us to [CK_COND =1], a check
  1682. * condition is generated and the ATA register values are returned
  1683. * whether the command completed successfully or not. If there
  1684. * was no error, we use the following sense data:
  1685. * sk = RECOVERED ERROR
  1686. * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
  1687. */
  1688. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1689. ((cdb[2] & 0x20) || need_sense))
  1690. ata_gen_passthru_sense(qc);
  1691. else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1692. cmd->result = SAM_STAT_CHECK_CONDITION;
  1693. else if (need_sense)
  1694. ata_gen_ata_sense(qc);
  1695. else
  1696. cmd->result = SAM_STAT_GOOD;
  1697. if (need_sense && !ap->ops->error_handler)
  1698. ata_dump_status(ap->print_id, &qc->result_tf);
  1699. ata_qc_done(qc);
  1700. }
  1701. /**
  1702. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1703. * @dev: ATA device to which the command is addressed
  1704. * @cmd: SCSI command to execute
  1705. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1706. *
  1707. * Our ->queuecommand() function has decided that the SCSI
  1708. * command issued can be directly translated into an ATA
  1709. * command, rather than handled internally.
  1710. *
  1711. * This function sets up an ata_queued_cmd structure for the
  1712. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1713. *
  1714. * The xlat_func argument (actor) returns 0 if ready to execute
  1715. * ATA command, else 1 to finish translation. If 1 is returned
  1716. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1717. * to be set reflecting an error condition or clean (early)
  1718. * termination.
  1719. *
  1720. * LOCKING:
  1721. * spin_lock_irqsave(host lock)
  1722. *
  1723. * RETURNS:
  1724. * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
  1725. * needs to be deferred.
  1726. */
  1727. static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
  1728. ata_xlat_func_t xlat_func)
  1729. {
  1730. struct ata_port *ap = dev->link->ap;
  1731. struct ata_queued_cmd *qc;
  1732. int rc;
  1733. VPRINTK("ENTER\n");
  1734. qc = ata_scsi_qc_new(dev, cmd);
  1735. if (!qc)
  1736. goto err_mem;
  1737. /* data is present; dma-map it */
  1738. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1739. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1740. if (unlikely(scsi_bufflen(cmd) < 1)) {
  1741. ata_dev_warn(dev, "WARNING: zero len r/w req\n");
  1742. goto err_did;
  1743. }
  1744. ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
  1745. qc->dma_dir = cmd->sc_data_direction;
  1746. }
  1747. qc->complete_fn = ata_scsi_qc_complete;
  1748. if (xlat_func(qc))
  1749. goto early_finish;
  1750. if (ap->ops->qc_defer) {
  1751. if ((rc = ap->ops->qc_defer(qc)))
  1752. goto defer;
  1753. }
  1754. /* select device, send command to hardware */
  1755. ata_qc_issue(qc);
  1756. VPRINTK("EXIT\n");
  1757. return 0;
  1758. early_finish:
  1759. ata_qc_free(qc);
  1760. cmd->scsi_done(cmd);
  1761. DPRINTK("EXIT - early finish (good or error)\n");
  1762. return 0;
  1763. err_did:
  1764. ata_qc_free(qc);
  1765. cmd->result = (DID_ERROR << 16);
  1766. cmd->scsi_done(cmd);
  1767. err_mem:
  1768. DPRINTK("EXIT - internal\n");
  1769. return 0;
  1770. defer:
  1771. ata_qc_free(qc);
  1772. DPRINTK("EXIT - defer\n");
  1773. if (rc == ATA_DEFER_LINK)
  1774. return SCSI_MLQUEUE_DEVICE_BUSY;
  1775. else
  1776. return SCSI_MLQUEUE_HOST_BUSY;
  1777. }
  1778. struct ata_scsi_args {
  1779. struct ata_device *dev;
  1780. u16 *id;
  1781. struct scsi_cmnd *cmd;
  1782. };
  1783. /**
  1784. * ata_scsi_rbuf_get - Map response buffer.
  1785. * @cmd: SCSI command containing buffer to be mapped.
  1786. * @flags: unsigned long variable to store irq enable status
  1787. * @copy_in: copy in from user buffer
  1788. *
  1789. * Prepare buffer for simulated SCSI commands.
  1790. *
  1791. * LOCKING:
  1792. * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
  1793. *
  1794. * RETURNS:
  1795. * Pointer to response buffer.
  1796. */
  1797. static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
  1798. unsigned long *flags)
  1799. {
  1800. spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
  1801. memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
  1802. if (copy_in)
  1803. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1804. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1805. return ata_scsi_rbuf;
  1806. }
  1807. /**
  1808. * ata_scsi_rbuf_put - Unmap response buffer.
  1809. * @cmd: SCSI command containing buffer to be unmapped.
  1810. * @copy_out: copy out result
  1811. * @flags: @flags passed to ata_scsi_rbuf_get()
  1812. *
  1813. * Returns rbuf buffer. The result is copied to @cmd's buffer if
  1814. * @copy_back is true.
  1815. *
  1816. * LOCKING:
  1817. * Unlocks ata_scsi_rbuf_lock.
  1818. */
  1819. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
  1820. unsigned long *flags)
  1821. {
  1822. if (copy_out)
  1823. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1824. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1825. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
  1826. }
  1827. /**
  1828. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1829. * @args: device IDENTIFY data / SCSI command of interest.
  1830. * @actor: Callback hook for desired SCSI command simulator
  1831. *
  1832. * Takes care of the hard work of simulating a SCSI command...
  1833. * Mapping the response buffer, calling the command's handler,
  1834. * and handling the handler's return value. This return value
  1835. * indicates whether the handler wishes the SCSI command to be
  1836. * completed successfully (0), or not (in which case cmd->result
  1837. * and sense buffer are assumed to be set).
  1838. *
  1839. * LOCKING:
  1840. * spin_lock_irqsave(host lock)
  1841. */
  1842. static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1843. unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
  1844. {
  1845. u8 *rbuf;
  1846. unsigned int rc;
  1847. struct scsi_cmnd *cmd = args->cmd;
  1848. unsigned long flags;
  1849. rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
  1850. rc = actor(args, rbuf);
  1851. ata_scsi_rbuf_put(cmd, rc == 0, &flags);
  1852. if (rc == 0)
  1853. cmd->result = SAM_STAT_GOOD;
  1854. }
  1855. /**
  1856. * ata_scsiop_inq_std - Simulate INQUIRY command
  1857. * @args: device IDENTIFY data / SCSI command of interest.
  1858. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1859. *
  1860. * Returns standard device identification data associated
  1861. * with non-VPD INQUIRY command output.
  1862. *
  1863. * LOCKING:
  1864. * spin_lock_irqsave(host lock)
  1865. */
  1866. static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
  1867. {
  1868. static const u8 versions[] = {
  1869. 0x00,
  1870. 0x60, /* SAM-3 (no version claimed) */
  1871. 0x03,
  1872. 0x20, /* SBC-2 (no version claimed) */
  1873. 0x03,
  1874. 0x00 /* SPC-3 (no version claimed) */
  1875. };
  1876. static const u8 versions_zbc[] = {
  1877. 0x00,
  1878. 0xA0, /* SAM-5 (no version claimed) */
  1879. 0x06,
  1880. 0x00, /* SBC-4 (no version claimed) */
  1881. 0x05,
  1882. 0xC0, /* SPC-5 (no version claimed) */
  1883. 0x60,
  1884. 0x24, /* ZBC r05 */
  1885. };
  1886. u8 hdr[] = {
  1887. TYPE_DISK,
  1888. 0,
  1889. 0x5, /* claim SPC-3 version compatibility */
  1890. 2,
  1891. 95 - 4,
  1892. 0,
  1893. 0,
  1894. 2
  1895. };
  1896. VPRINTK("ENTER\n");
  1897. /* set scsi removable (RMB) bit per ata bit, or if the
  1898. * AHCI port says it's external (Hotplug-capable, eSATA).
  1899. */
  1900. if (ata_id_removable(args->id) ||
  1901. (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
  1902. hdr[1] |= (1 << 7);
  1903. if (args->dev->class == ATA_DEV_ZAC) {
  1904. hdr[0] = TYPE_ZBC;
  1905. hdr[2] = 0x7; /* claim SPC-5 version compatibility */
  1906. }
  1907. memcpy(rbuf, hdr, sizeof(hdr));
  1908. memcpy(&rbuf[8], "ATA ", 8);
  1909. ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
  1910. /* From SAT, use last 2 words from fw rev unless they are spaces */
  1911. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
  1912. if (strncmp(&rbuf[32], " ", 4) == 0)
  1913. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1914. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1915. memcpy(&rbuf[32], "n/a ", 4);
  1916. if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
  1917. memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
  1918. else
  1919. memcpy(rbuf + 58, versions, sizeof(versions));
  1920. return 0;
  1921. }
  1922. /**
  1923. * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  1924. * @args: device IDENTIFY data / SCSI command of interest.
  1925. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1926. *
  1927. * Returns list of inquiry VPD pages available.
  1928. *
  1929. * LOCKING:
  1930. * spin_lock_irqsave(host lock)
  1931. */
  1932. static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
  1933. {
  1934. int num_pages;
  1935. static const u8 pages[] = {
  1936. 0x00, /* page 0x00, this page */
  1937. 0x80, /* page 0x80, unit serial no page */
  1938. 0x83, /* page 0x83, device ident page */
  1939. 0x89, /* page 0x89, ata info page */
  1940. 0xb0, /* page 0xb0, block limits page */
  1941. 0xb1, /* page 0xb1, block device characteristics page */
  1942. 0xb2, /* page 0xb2, thin provisioning page */
  1943. 0xb6, /* page 0xb6, zoned block device characteristics */
  1944. };
  1945. num_pages = sizeof(pages);
  1946. if (!(args->dev->flags & ATA_DFLAG_ZAC))
  1947. num_pages--;
  1948. rbuf[3] = num_pages; /* number of supported VPD pages */
  1949. memcpy(rbuf + 4, pages, num_pages);
  1950. return 0;
  1951. }
  1952. /**
  1953. * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  1954. * @args: device IDENTIFY data / SCSI command of interest.
  1955. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1956. *
  1957. * Returns ATA device serial number.
  1958. *
  1959. * LOCKING:
  1960. * spin_lock_irqsave(host lock)
  1961. */
  1962. static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
  1963. {
  1964. static const u8 hdr[] = {
  1965. 0,
  1966. 0x80, /* this page code */
  1967. 0,
  1968. ATA_ID_SERNO_LEN, /* page len */
  1969. };
  1970. memcpy(rbuf, hdr, sizeof(hdr));
  1971. ata_id_string(args->id, (unsigned char *) &rbuf[4],
  1972. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1973. return 0;
  1974. }
  1975. /**
  1976. * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  1977. * @args: device IDENTIFY data / SCSI command of interest.
  1978. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1979. *
  1980. * Yields two logical unit device identification designators:
  1981. * - vendor specific ASCII containing the ATA serial number
  1982. * - SAT defined "t10 vendor id based" containing ASCII vendor
  1983. * name ("ATA "), model and serial numbers.
  1984. *
  1985. * LOCKING:
  1986. * spin_lock_irqsave(host lock)
  1987. */
  1988. static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
  1989. {
  1990. const int sat_model_serial_desc_len = 68;
  1991. int num;
  1992. rbuf[1] = 0x83; /* this page code */
  1993. num = 4;
  1994. /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
  1995. rbuf[num + 0] = 2;
  1996. rbuf[num + 3] = ATA_ID_SERNO_LEN;
  1997. num += 4;
  1998. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1999. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  2000. num += ATA_ID_SERNO_LEN;
  2001. /* SAT defined lu model and serial numbers descriptor */
  2002. /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
  2003. rbuf[num + 0] = 2;
  2004. rbuf[num + 1] = 1;
  2005. rbuf[num + 3] = sat_model_serial_desc_len;
  2006. num += 4;
  2007. memcpy(rbuf + num, "ATA ", 8);
  2008. num += 8;
  2009. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
  2010. ATA_ID_PROD_LEN);
  2011. num += ATA_ID_PROD_LEN;
  2012. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
  2013. ATA_ID_SERNO_LEN);
  2014. num += ATA_ID_SERNO_LEN;
  2015. if (ata_id_has_wwn(args->id)) {
  2016. /* SAT defined lu world wide name */
  2017. /* piv=0, assoc=lu, code_set=binary, designator=NAA */
  2018. rbuf[num + 0] = 1;
  2019. rbuf[num + 1] = 3;
  2020. rbuf[num + 3] = ATA_ID_WWN_LEN;
  2021. num += 4;
  2022. ata_id_string(args->id, (unsigned char *) rbuf + num,
  2023. ATA_ID_WWN, ATA_ID_WWN_LEN);
  2024. num += ATA_ID_WWN_LEN;
  2025. }
  2026. rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
  2027. return 0;
  2028. }
  2029. /**
  2030. * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
  2031. * @args: device IDENTIFY data / SCSI command of interest.
  2032. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2033. *
  2034. * Yields SAT-specified ATA VPD page.
  2035. *
  2036. * LOCKING:
  2037. * spin_lock_irqsave(host lock)
  2038. */
  2039. static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
  2040. {
  2041. struct ata_taskfile tf;
  2042. memset(&tf, 0, sizeof(tf));
  2043. rbuf[1] = 0x89; /* our page code */
  2044. rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
  2045. rbuf[3] = (0x238 & 0xff);
  2046. memcpy(&rbuf[8], "linux ", 8);
  2047. memcpy(&rbuf[16], "libata ", 16);
  2048. memcpy(&rbuf[32], DRV_VERSION, 4);
  2049. /* we don't store the ATA device signature, so we fake it */
  2050. tf.command = ATA_DRDY; /* really, this is Status reg */
  2051. tf.lbal = 0x1;
  2052. tf.nsect = 0x1;
  2053. ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
  2054. rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
  2055. rbuf[56] = ATA_CMD_ID_ATA;
  2056. memcpy(&rbuf[60], &args->id[0], 512);
  2057. return 0;
  2058. }
  2059. static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
  2060. {
  2061. u16 min_io_sectors;
  2062. rbuf[1] = 0xb0;
  2063. rbuf[3] = 0x3c; /* required VPD size with unmap support */
  2064. /*
  2065. * Optimal transfer length granularity.
  2066. *
  2067. * This is always one physical block, but for disks with a smaller
  2068. * logical than physical sector size we need to figure out what the
  2069. * latter is.
  2070. */
  2071. min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
  2072. put_unaligned_be16(min_io_sectors, &rbuf[6]);
  2073. /*
  2074. * Optimal unmap granularity.
  2075. *
  2076. * The ATA spec doesn't even know about a granularity or alignment
  2077. * for the TRIM command. We can leave away most of the unmap related
  2078. * VPD page entries, but we have specifify a granularity to signal
  2079. * that we support some form of unmap - in thise case via WRITE SAME
  2080. * with the unmap bit set.
  2081. */
  2082. if (ata_id_has_trim(args->id)) {
  2083. put_unaligned_be64(65535 * ATA_MAX_TRIM_RNUM, &rbuf[36]);
  2084. put_unaligned_be32(1, &rbuf[28]);
  2085. }
  2086. return 0;
  2087. }
  2088. static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
  2089. {
  2090. int form_factor = ata_id_form_factor(args->id);
  2091. int media_rotation_rate = ata_id_rotation_rate(args->id);
  2092. u8 zoned = ata_id_zoned_cap(args->id);
  2093. rbuf[1] = 0xb1;
  2094. rbuf[3] = 0x3c;
  2095. rbuf[4] = media_rotation_rate >> 8;
  2096. rbuf[5] = media_rotation_rate;
  2097. rbuf[7] = form_factor;
  2098. if (zoned)
  2099. rbuf[8] = (zoned << 4);
  2100. return 0;
  2101. }
  2102. static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
  2103. {
  2104. /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
  2105. rbuf[1] = 0xb2;
  2106. rbuf[3] = 0x4;
  2107. rbuf[5] = 1 << 6; /* TPWS */
  2108. return 0;
  2109. }
  2110. static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
  2111. {
  2112. /*
  2113. * zbc-r05 SCSI Zoned Block device characteristics VPD page
  2114. */
  2115. rbuf[1] = 0xb6;
  2116. rbuf[3] = 0x3C;
  2117. /*
  2118. * URSWRZ bit is only meaningful for host-managed ZAC drives
  2119. */
  2120. if (args->dev->zac_zoned_cap & 1)
  2121. rbuf[4] |= 1;
  2122. put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
  2123. put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
  2124. put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
  2125. return 0;
  2126. }
  2127. /**
  2128. * modecpy - Prepare response for MODE SENSE
  2129. * @dest: output buffer
  2130. * @src: data being copied
  2131. * @n: length of mode page
  2132. * @changeable: whether changeable parameters are requested
  2133. *
  2134. * Generate a generic MODE SENSE page for either current or changeable
  2135. * parameters.
  2136. *
  2137. * LOCKING:
  2138. * None.
  2139. */
  2140. static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
  2141. {
  2142. if (changeable) {
  2143. memcpy(dest, src, 2);
  2144. memset(dest + 2, 0, n - 2);
  2145. } else {
  2146. memcpy(dest, src, n);
  2147. }
  2148. }
  2149. /**
  2150. * ata_msense_caching - Simulate MODE SENSE caching info page
  2151. * @id: device IDENTIFY data
  2152. * @buf: output buffer
  2153. * @changeable: whether changeable parameters are requested
  2154. *
  2155. * Generate a caching info page, which conditionally indicates
  2156. * write caching to the SCSI layer, depending on device
  2157. * capabilities.
  2158. *
  2159. * LOCKING:
  2160. * None.
  2161. */
  2162. static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
  2163. {
  2164. modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
  2165. if (changeable) {
  2166. buf[2] |= (1 << 2); /* ata_mselect_caching() */
  2167. } else {
  2168. buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
  2169. buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
  2170. }
  2171. return sizeof(def_cache_mpage);
  2172. }
  2173. /**
  2174. * ata_msense_control - Simulate MODE SENSE control mode page
  2175. * @dev: ATA device of interest
  2176. * @buf: output buffer
  2177. * @changeable: whether changeable parameters are requested
  2178. *
  2179. * Generate a generic MODE SENSE control mode page.
  2180. *
  2181. * LOCKING:
  2182. * None.
  2183. */
  2184. static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
  2185. bool changeable)
  2186. {
  2187. modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
  2188. if (changeable) {
  2189. buf[2] |= (1 << 2); /* ata_mselect_control() */
  2190. } else {
  2191. bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
  2192. buf[2] |= (d_sense << 2); /* descriptor format sense data */
  2193. }
  2194. return sizeof(def_control_mpage);
  2195. }
  2196. /**
  2197. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  2198. * @buf: output buffer
  2199. * @changeable: whether changeable parameters are requested
  2200. *
  2201. * Generate a generic MODE SENSE r/w error recovery page.
  2202. *
  2203. * LOCKING:
  2204. * None.
  2205. */
  2206. static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
  2207. {
  2208. modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
  2209. changeable);
  2210. return sizeof(def_rw_recovery_mpage);
  2211. }
  2212. /*
  2213. * We can turn this into a real blacklist if it's needed, for now just
  2214. * blacklist any Maxtor BANC1G10 revision firmware
  2215. */
  2216. static int ata_dev_supports_fua(u16 *id)
  2217. {
  2218. unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
  2219. if (!libata_fua)
  2220. return 0;
  2221. if (!ata_id_has_fua(id))
  2222. return 0;
  2223. ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  2224. ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
  2225. if (strcmp(model, "Maxtor"))
  2226. return 1;
  2227. if (strcmp(fw, "BANC1G10"))
  2228. return 1;
  2229. return 0; /* blacklisted */
  2230. }
  2231. /**
  2232. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  2233. * @args: device IDENTIFY data / SCSI command of interest.
  2234. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2235. *
  2236. * Simulate MODE SENSE commands. Assume this is invoked for direct
  2237. * access devices (e.g. disks) only. There should be no block
  2238. * descriptor for other device types.
  2239. *
  2240. * LOCKING:
  2241. * spin_lock_irqsave(host lock)
  2242. */
  2243. static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
  2244. {
  2245. struct ata_device *dev = args->dev;
  2246. u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
  2247. static const u8 sat_blk_desc[] = {
  2248. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  2249. 0,
  2250. 0, 0x2, 0x0 /* block length: 512 bytes */
  2251. };
  2252. u8 pg, spg;
  2253. unsigned int ebd, page_control, six_byte;
  2254. u8 dpofua, bp = 0xff;
  2255. u16 fp;
  2256. VPRINTK("ENTER\n");
  2257. six_byte = (scsicmd[0] == MODE_SENSE);
  2258. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  2259. /*
  2260. * LLBA bit in msense(10) ignored (compliant)
  2261. */
  2262. page_control = scsicmd[2] >> 6;
  2263. switch (page_control) {
  2264. case 0: /* current */
  2265. case 1: /* changeable */
  2266. case 2: /* defaults */
  2267. break; /* supported */
  2268. case 3: /* saved */
  2269. goto saving_not_supp;
  2270. default:
  2271. fp = 2;
  2272. bp = 6;
  2273. goto invalid_fld;
  2274. }
  2275. if (six_byte)
  2276. p += 4 + (ebd ? 8 : 0);
  2277. else
  2278. p += 8 + (ebd ? 8 : 0);
  2279. pg = scsicmd[2] & 0x3f;
  2280. spg = scsicmd[3];
  2281. /*
  2282. * No mode subpages supported (yet) but asking for _all_
  2283. * subpages may be valid
  2284. */
  2285. if (spg && (spg != ALL_SUB_MPAGES)) {
  2286. fp = 3;
  2287. goto invalid_fld;
  2288. }
  2289. switch(pg) {
  2290. case RW_RECOVERY_MPAGE:
  2291. p += ata_msense_rw_recovery(p, page_control == 1);
  2292. break;
  2293. case CACHE_MPAGE:
  2294. p += ata_msense_caching(args->id, p, page_control == 1);
  2295. break;
  2296. case CONTROL_MPAGE:
  2297. p += ata_msense_control(args->dev, p, page_control == 1);
  2298. break;
  2299. case ALL_MPAGES:
  2300. p += ata_msense_rw_recovery(p, page_control == 1);
  2301. p += ata_msense_caching(args->id, p, page_control == 1);
  2302. p += ata_msense_control(args->dev, p, page_control == 1);
  2303. break;
  2304. default: /* invalid page code */
  2305. fp = 2;
  2306. goto invalid_fld;
  2307. }
  2308. dpofua = 0;
  2309. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  2310. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  2311. dpofua = 1 << 4;
  2312. if (six_byte) {
  2313. rbuf[0] = p - rbuf - 1;
  2314. rbuf[2] |= dpofua;
  2315. if (ebd) {
  2316. rbuf[3] = sizeof(sat_blk_desc);
  2317. memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
  2318. }
  2319. } else {
  2320. unsigned int output_len = p - rbuf - 2;
  2321. rbuf[0] = output_len >> 8;
  2322. rbuf[1] = output_len;
  2323. rbuf[3] |= dpofua;
  2324. if (ebd) {
  2325. rbuf[7] = sizeof(sat_blk_desc);
  2326. memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
  2327. }
  2328. }
  2329. return 0;
  2330. invalid_fld:
  2331. ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
  2332. return 1;
  2333. saving_not_supp:
  2334. ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  2335. /* "Saving parameters not supported" */
  2336. return 1;
  2337. }
  2338. /**
  2339. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  2340. * @args: device IDENTIFY data / SCSI command of interest.
  2341. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2342. *
  2343. * Simulate READ CAPACITY commands.
  2344. *
  2345. * LOCKING:
  2346. * None.
  2347. */
  2348. static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
  2349. {
  2350. struct ata_device *dev = args->dev;
  2351. u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
  2352. u32 sector_size; /* physical sector size in bytes */
  2353. u8 log2_per_phys;
  2354. u16 lowest_aligned;
  2355. sector_size = ata_id_logical_sector_size(dev->id);
  2356. log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
  2357. lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
  2358. VPRINTK("ENTER\n");
  2359. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  2360. if (last_lba >= 0xffffffffULL)
  2361. last_lba = 0xffffffff;
  2362. /* sector count, 32-bit */
  2363. rbuf[0] = last_lba >> (8 * 3);
  2364. rbuf[1] = last_lba >> (8 * 2);
  2365. rbuf[2] = last_lba >> (8 * 1);
  2366. rbuf[3] = last_lba;
  2367. /* sector size */
  2368. rbuf[4] = sector_size >> (8 * 3);
  2369. rbuf[5] = sector_size >> (8 * 2);
  2370. rbuf[6] = sector_size >> (8 * 1);
  2371. rbuf[7] = sector_size;
  2372. } else {
  2373. /* sector count, 64-bit */
  2374. rbuf[0] = last_lba >> (8 * 7);
  2375. rbuf[1] = last_lba >> (8 * 6);
  2376. rbuf[2] = last_lba >> (8 * 5);
  2377. rbuf[3] = last_lba >> (8 * 4);
  2378. rbuf[4] = last_lba >> (8 * 3);
  2379. rbuf[5] = last_lba >> (8 * 2);
  2380. rbuf[6] = last_lba >> (8 * 1);
  2381. rbuf[7] = last_lba;
  2382. /* sector size */
  2383. rbuf[ 8] = sector_size >> (8 * 3);
  2384. rbuf[ 9] = sector_size >> (8 * 2);
  2385. rbuf[10] = sector_size >> (8 * 1);
  2386. rbuf[11] = sector_size;
  2387. rbuf[12] = 0;
  2388. rbuf[13] = log2_per_phys;
  2389. rbuf[14] = (lowest_aligned >> 8) & 0x3f;
  2390. rbuf[15] = lowest_aligned;
  2391. if (ata_id_has_trim(args->id) &&
  2392. !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
  2393. rbuf[14] |= 0x80; /* LBPME */
  2394. if (ata_id_has_zero_after_trim(args->id) &&
  2395. dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
  2396. ata_dev_info(dev, "Enabling discard_zeroes_data\n");
  2397. rbuf[14] |= 0x40; /* LBPRZ */
  2398. }
  2399. }
  2400. if (ata_id_zoned_cap(args->id) ||
  2401. args->dev->class == ATA_DEV_ZAC)
  2402. rbuf[12] = (1 << 4); /* RC_BASIS */
  2403. }
  2404. return 0;
  2405. }
  2406. /**
  2407. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  2408. * @args: device IDENTIFY data / SCSI command of interest.
  2409. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2410. *
  2411. * Simulate REPORT LUNS command.
  2412. *
  2413. * LOCKING:
  2414. * spin_lock_irqsave(host lock)
  2415. */
  2416. static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
  2417. {
  2418. VPRINTK("ENTER\n");
  2419. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  2420. return 0;
  2421. }
  2422. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  2423. {
  2424. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  2425. /* FIXME: not quite right; we don't want the
  2426. * translation of taskfile registers into
  2427. * a sense descriptors, since that's only
  2428. * correct for ATA, not ATAPI
  2429. */
  2430. ata_gen_passthru_sense(qc);
  2431. }
  2432. ata_qc_done(qc);
  2433. }
  2434. /* is it pointless to prefer PIO for "safety reasons"? */
  2435. static inline int ata_pio_use_silly(struct ata_port *ap)
  2436. {
  2437. return (ap->flags & ATA_FLAG_PIO_DMA);
  2438. }
  2439. static void atapi_request_sense(struct ata_queued_cmd *qc)
  2440. {
  2441. struct ata_port *ap = qc->ap;
  2442. struct scsi_cmnd *cmd = qc->scsicmd;
  2443. DPRINTK("ATAPI request sense\n");
  2444. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  2445. #ifdef CONFIG_ATA_SFF
  2446. if (ap->ops->sff_tf_read)
  2447. ap->ops->sff_tf_read(ap, &qc->tf);
  2448. #endif
  2449. /* fill these in, for the case where they are -not- overwritten */
  2450. cmd->sense_buffer[0] = 0x70;
  2451. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  2452. ata_qc_reinit(qc);
  2453. /* setup sg table and init transfer direction */
  2454. sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
  2455. ata_sg_init(qc, &qc->sgent, 1);
  2456. qc->dma_dir = DMA_FROM_DEVICE;
  2457. memset(&qc->cdb, 0, qc->dev->cdb_len);
  2458. qc->cdb[0] = REQUEST_SENSE;
  2459. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2460. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2461. qc->tf.command = ATA_CMD_PACKET;
  2462. if (ata_pio_use_silly(ap)) {
  2463. qc->tf.protocol = ATAPI_PROT_DMA;
  2464. qc->tf.feature |= ATAPI_PKT_DMA;
  2465. } else {
  2466. qc->tf.protocol = ATAPI_PROT_PIO;
  2467. qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
  2468. qc->tf.lbah = 0;
  2469. }
  2470. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  2471. qc->complete_fn = atapi_sense_complete;
  2472. ata_qc_issue(qc);
  2473. DPRINTK("EXIT\n");
  2474. }
  2475. /*
  2476. * ATAPI devices typically report zero for their SCSI version, and sometimes
  2477. * deviate from the spec WRT response data format. If SCSI version is
  2478. * reported as zero like normal, then we make the following fixups:
  2479. * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
  2480. * modern device.
  2481. * 2) Ensure response data format / ATAPI information are always correct.
  2482. */
  2483. static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
  2484. {
  2485. u8 buf[4];
  2486. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
  2487. if (buf[2] == 0) {
  2488. buf[2] = 0x5;
  2489. buf[3] = 0x32;
  2490. }
  2491. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
  2492. }
  2493. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  2494. {
  2495. struct scsi_cmnd *cmd = qc->scsicmd;
  2496. unsigned int err_mask = qc->err_mask;
  2497. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  2498. /* handle completion from new EH */
  2499. if (unlikely(qc->ap->ops->error_handler &&
  2500. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  2501. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  2502. /* FIXME: not quite right; we don't want the
  2503. * translation of taskfile registers into a
  2504. * sense descriptors, since that's only
  2505. * correct for ATA, not ATAPI
  2506. */
  2507. ata_gen_passthru_sense(qc);
  2508. }
  2509. /* SCSI EH automatically locks door if sdev->locked is
  2510. * set. Sometimes door lock request continues to
  2511. * fail, for example, when no media is present. This
  2512. * creates a loop - SCSI EH issues door lock which
  2513. * fails and gets invoked again to acquire sense data
  2514. * for the failed command.
  2515. *
  2516. * If door lock fails, always clear sdev->locked to
  2517. * avoid this infinite loop.
  2518. *
  2519. * This may happen before SCSI scan is complete. Make
  2520. * sure qc->dev->sdev isn't NULL before dereferencing.
  2521. */
  2522. if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
  2523. qc->dev->sdev->locked = 0;
  2524. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  2525. ata_qc_done(qc);
  2526. return;
  2527. }
  2528. /* successful completion or old EH failure path */
  2529. if (unlikely(err_mask & AC_ERR_DEV)) {
  2530. cmd->result = SAM_STAT_CHECK_CONDITION;
  2531. atapi_request_sense(qc);
  2532. return;
  2533. } else if (unlikely(err_mask)) {
  2534. /* FIXME: not quite right; we don't want the
  2535. * translation of taskfile registers into
  2536. * a sense descriptors, since that's only
  2537. * correct for ATA, not ATAPI
  2538. */
  2539. ata_gen_passthru_sense(qc);
  2540. } else {
  2541. if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
  2542. atapi_fixup_inquiry(cmd);
  2543. cmd->result = SAM_STAT_GOOD;
  2544. }
  2545. ata_qc_done(qc);
  2546. }
  2547. /**
  2548. * atapi_xlat - Initialize PACKET taskfile
  2549. * @qc: command structure to be initialized
  2550. *
  2551. * LOCKING:
  2552. * spin_lock_irqsave(host lock)
  2553. *
  2554. * RETURNS:
  2555. * Zero on success, non-zero on failure.
  2556. */
  2557. static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
  2558. {
  2559. struct scsi_cmnd *scmd = qc->scsicmd;
  2560. struct ata_device *dev = qc->dev;
  2561. int nodata = (scmd->sc_data_direction == DMA_NONE);
  2562. int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
  2563. unsigned int nbytes;
  2564. memset(qc->cdb, 0, dev->cdb_len);
  2565. memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
  2566. qc->complete_fn = atapi_qc_complete;
  2567. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2568. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2569. qc->tf.flags |= ATA_TFLAG_WRITE;
  2570. DPRINTK("direction: write\n");
  2571. }
  2572. qc->tf.command = ATA_CMD_PACKET;
  2573. ata_qc_set_pc_nbytes(qc);
  2574. /* check whether ATAPI DMA is safe */
  2575. if (!nodata && !using_pio && atapi_check_dma(qc))
  2576. using_pio = 1;
  2577. /* Some controller variants snoop this value for Packet
  2578. * transfers to do state machine and FIFO management. Thus we
  2579. * want to set it properly, and for DMA where it is
  2580. * effectively meaningless.
  2581. */
  2582. nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
  2583. /* Most ATAPI devices which honor transfer chunk size don't
  2584. * behave according to the spec when odd chunk size which
  2585. * matches the transfer length is specified. If the number of
  2586. * bytes to transfer is 2n+1. According to the spec, what
  2587. * should happen is to indicate that 2n+1 is going to be
  2588. * transferred and transfer 2n+2 bytes where the last byte is
  2589. * padding.
  2590. *
  2591. * In practice, this doesn't happen. ATAPI devices first
  2592. * indicate and transfer 2n bytes and then indicate and
  2593. * transfer 2 bytes where the last byte is padding.
  2594. *
  2595. * This inconsistency confuses several controllers which
  2596. * perform PIO using DMA such as Intel AHCIs and sil3124/32.
  2597. * These controllers use actual number of transferred bytes to
  2598. * update DMA poitner and transfer of 4n+2 bytes make those
  2599. * controller push DMA pointer by 4n+4 bytes because SATA data
  2600. * FISes are aligned to 4 bytes. This causes data corruption
  2601. * and buffer overrun.
  2602. *
  2603. * Always setting nbytes to even number solves this problem
  2604. * because then ATAPI devices don't have to split data at 2n
  2605. * boundaries.
  2606. */
  2607. if (nbytes & 0x1)
  2608. nbytes++;
  2609. qc->tf.lbam = (nbytes & 0xFF);
  2610. qc->tf.lbah = (nbytes >> 8);
  2611. if (nodata)
  2612. qc->tf.protocol = ATAPI_PROT_NODATA;
  2613. else if (using_pio)
  2614. qc->tf.protocol = ATAPI_PROT_PIO;
  2615. else {
  2616. /* DMA data xfer */
  2617. qc->tf.protocol = ATAPI_PROT_DMA;
  2618. qc->tf.feature |= ATAPI_PKT_DMA;
  2619. if ((dev->flags & ATA_DFLAG_DMADIR) &&
  2620. (scmd->sc_data_direction != DMA_TO_DEVICE))
  2621. /* some SATA bridges need us to indicate data xfer direction */
  2622. qc->tf.feature |= ATAPI_DMADIR;
  2623. }
  2624. /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
  2625. as ATAPI tape drives don't get this right otherwise */
  2626. return 0;
  2627. }
  2628. static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
  2629. {
  2630. if (!sata_pmp_attached(ap)) {
  2631. if (likely(devno >= 0 &&
  2632. devno < ata_link_max_devices(&ap->link)))
  2633. return &ap->link.device[devno];
  2634. } else {
  2635. if (likely(devno >= 0 &&
  2636. devno < ap->nr_pmp_links))
  2637. return &ap->pmp_link[devno].device[0];
  2638. }
  2639. return NULL;
  2640. }
  2641. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  2642. const struct scsi_device *scsidev)
  2643. {
  2644. int devno;
  2645. /* skip commands not addressed to targets we simulate */
  2646. if (!sata_pmp_attached(ap)) {
  2647. if (unlikely(scsidev->channel || scsidev->lun))
  2648. return NULL;
  2649. devno = scsidev->id;
  2650. } else {
  2651. if (unlikely(scsidev->id || scsidev->lun))
  2652. return NULL;
  2653. devno = scsidev->channel;
  2654. }
  2655. return ata_find_dev(ap, devno);
  2656. }
  2657. /**
  2658. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2659. * @ap: ATA port to which the device is attached
  2660. * @scsidev: SCSI device from which we derive the ATA device
  2661. *
  2662. * Given various information provided in struct scsi_cmnd,
  2663. * map that onto an ATA bus, and using that mapping
  2664. * determine which ata_device is associated with the
  2665. * SCSI command to be sent.
  2666. *
  2667. * LOCKING:
  2668. * spin_lock_irqsave(host lock)
  2669. *
  2670. * RETURNS:
  2671. * Associated ATA device, or %NULL if not found.
  2672. */
  2673. static struct ata_device *
  2674. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2675. {
  2676. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2677. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2678. return NULL;
  2679. return dev;
  2680. }
  2681. /*
  2682. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2683. * @byte1: Byte 1 from pass-thru CDB.
  2684. *
  2685. * RETURNS:
  2686. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2687. */
  2688. static u8
  2689. ata_scsi_map_proto(u8 byte1)
  2690. {
  2691. switch((byte1 & 0x1e) >> 1) {
  2692. case 3: /* Non-data */
  2693. return ATA_PROT_NODATA;
  2694. case 6: /* DMA */
  2695. case 10: /* UDMA Data-in */
  2696. case 11: /* UDMA Data-Out */
  2697. return ATA_PROT_DMA;
  2698. case 4: /* PIO Data-in */
  2699. case 5: /* PIO Data-out */
  2700. return ATA_PROT_PIO;
  2701. case 12: /* FPDMA */
  2702. return ATA_PROT_NCQ;
  2703. case 0: /* Hard Reset */
  2704. case 1: /* SRST */
  2705. case 8: /* Device Diagnostic */
  2706. case 9: /* Device Reset */
  2707. case 7: /* DMA Queued */
  2708. case 15: /* Return Response Info */
  2709. default: /* Reserved */
  2710. break;
  2711. }
  2712. return ATA_PROT_UNKNOWN;
  2713. }
  2714. /**
  2715. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2716. * @qc: command structure to be initialized
  2717. *
  2718. * Handles either 12, 16, or 32-byte versions of the CDB.
  2719. *
  2720. * RETURNS:
  2721. * Zero on success, non-zero on failure.
  2722. */
  2723. static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
  2724. {
  2725. struct ata_taskfile *tf = &(qc->tf);
  2726. struct scsi_cmnd *scmd = qc->scsicmd;
  2727. struct ata_device *dev = qc->dev;
  2728. const u8 *cdb = scmd->cmnd;
  2729. u16 fp;
  2730. u16 cdb_offset = 0;
  2731. /* 7Fh variable length cmd means a ata pass-thru(32) */
  2732. if (cdb[0] == VARIABLE_LENGTH_CMD)
  2733. cdb_offset = 9;
  2734. tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
  2735. if (tf->protocol == ATA_PROT_UNKNOWN) {
  2736. fp = 1;
  2737. goto invalid_fld;
  2738. }
  2739. if (ata_is_ncq(tf->protocol) && (cdb[2 + cdb_offset] & 0x3) == 0)
  2740. tf->protocol = ATA_PROT_NCQ_NODATA;
  2741. /* enable LBA */
  2742. tf->flags |= ATA_TFLAG_LBA;
  2743. /*
  2744. * 12 and 16 byte CDBs use different offsets to
  2745. * provide the various register values.
  2746. */
  2747. if (cdb[0] == ATA_16) {
  2748. /*
  2749. * 16-byte CDB - may contain extended commands.
  2750. *
  2751. * If that is the case, copy the upper byte register values.
  2752. */
  2753. if (cdb[1] & 0x01) {
  2754. tf->hob_feature = cdb[3];
  2755. tf->hob_nsect = cdb[5];
  2756. tf->hob_lbal = cdb[7];
  2757. tf->hob_lbam = cdb[9];
  2758. tf->hob_lbah = cdb[11];
  2759. tf->flags |= ATA_TFLAG_LBA48;
  2760. } else
  2761. tf->flags &= ~ATA_TFLAG_LBA48;
  2762. /*
  2763. * Always copy low byte, device and command registers.
  2764. */
  2765. tf->feature = cdb[4];
  2766. tf->nsect = cdb[6];
  2767. tf->lbal = cdb[8];
  2768. tf->lbam = cdb[10];
  2769. tf->lbah = cdb[12];
  2770. tf->device = cdb[13];
  2771. tf->command = cdb[14];
  2772. } else if (cdb[0] == ATA_12) {
  2773. /*
  2774. * 12-byte CDB - incapable of extended commands.
  2775. */
  2776. tf->flags &= ~ATA_TFLAG_LBA48;
  2777. tf->feature = cdb[3];
  2778. tf->nsect = cdb[4];
  2779. tf->lbal = cdb[5];
  2780. tf->lbam = cdb[6];
  2781. tf->lbah = cdb[7];
  2782. tf->device = cdb[8];
  2783. tf->command = cdb[9];
  2784. } else {
  2785. /*
  2786. * 32-byte CDB - may contain extended command fields.
  2787. *
  2788. * If that is the case, copy the upper byte register values.
  2789. */
  2790. if (cdb[10] & 0x01) {
  2791. tf->hob_feature = cdb[20];
  2792. tf->hob_nsect = cdb[22];
  2793. tf->hob_lbal = cdb[16];
  2794. tf->hob_lbam = cdb[15];
  2795. tf->hob_lbah = cdb[14];
  2796. tf->flags |= ATA_TFLAG_LBA48;
  2797. } else
  2798. tf->flags &= ~ATA_TFLAG_LBA48;
  2799. tf->feature = cdb[21];
  2800. tf->nsect = cdb[23];
  2801. tf->lbal = cdb[19];
  2802. tf->lbam = cdb[18];
  2803. tf->lbah = cdb[17];
  2804. tf->device = cdb[24];
  2805. tf->command = cdb[25];
  2806. tf->auxiliary = get_unaligned_be32(&cdb[28]);
  2807. }
  2808. /* For NCQ commands copy the tag value */
  2809. if (ata_is_ncq(tf->protocol))
  2810. tf->nsect = qc->hw_tag << 3;
  2811. /* enforce correct master/slave bit */
  2812. tf->device = dev->devno ?
  2813. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2814. switch (tf->command) {
  2815. /* READ/WRITE LONG use a non-standard sect_size */
  2816. case ATA_CMD_READ_LONG:
  2817. case ATA_CMD_READ_LONG_ONCE:
  2818. case ATA_CMD_WRITE_LONG:
  2819. case ATA_CMD_WRITE_LONG_ONCE:
  2820. if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
  2821. fp = 1;
  2822. goto invalid_fld;
  2823. }
  2824. qc->sect_size = scsi_bufflen(scmd);
  2825. break;
  2826. /* commands using reported Logical Block size (e.g. 512 or 4K) */
  2827. case ATA_CMD_CFA_WRITE_NE:
  2828. case ATA_CMD_CFA_TRANS_SECT:
  2829. case ATA_CMD_CFA_WRITE_MULT_NE:
  2830. /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
  2831. case ATA_CMD_READ:
  2832. case ATA_CMD_READ_EXT:
  2833. case ATA_CMD_READ_QUEUED:
  2834. /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
  2835. case ATA_CMD_FPDMA_READ:
  2836. case ATA_CMD_READ_MULTI:
  2837. case ATA_CMD_READ_MULTI_EXT:
  2838. case ATA_CMD_PIO_READ:
  2839. case ATA_CMD_PIO_READ_EXT:
  2840. case ATA_CMD_READ_STREAM_DMA_EXT:
  2841. case ATA_CMD_READ_STREAM_EXT:
  2842. case ATA_CMD_VERIFY:
  2843. case ATA_CMD_VERIFY_EXT:
  2844. case ATA_CMD_WRITE:
  2845. case ATA_CMD_WRITE_EXT:
  2846. case ATA_CMD_WRITE_FUA_EXT:
  2847. case ATA_CMD_WRITE_QUEUED:
  2848. case ATA_CMD_WRITE_QUEUED_FUA_EXT:
  2849. case ATA_CMD_FPDMA_WRITE:
  2850. case ATA_CMD_WRITE_MULTI:
  2851. case ATA_CMD_WRITE_MULTI_EXT:
  2852. case ATA_CMD_WRITE_MULTI_FUA_EXT:
  2853. case ATA_CMD_PIO_WRITE:
  2854. case ATA_CMD_PIO_WRITE_EXT:
  2855. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  2856. case ATA_CMD_WRITE_STREAM_EXT:
  2857. qc->sect_size = scmd->device->sector_size;
  2858. break;
  2859. /* Everything else uses 512 byte "sectors" */
  2860. default:
  2861. qc->sect_size = ATA_SECT_SIZE;
  2862. }
  2863. /*
  2864. * Set flags so that all registers will be written, pass on
  2865. * write indication (used for PIO/DMA setup), result TF is
  2866. * copied back and we don't whine too much about its failure.
  2867. */
  2868. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2869. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2870. tf->flags |= ATA_TFLAG_WRITE;
  2871. qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
  2872. /*
  2873. * Set transfer length.
  2874. *
  2875. * TODO: find out if we need to do more here to
  2876. * cover scatter/gather case.
  2877. */
  2878. ata_qc_set_pc_nbytes(qc);
  2879. /* We may not issue DMA commands if no DMA mode is set */
  2880. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
  2881. fp = 1;
  2882. goto invalid_fld;
  2883. }
  2884. /* We may not issue NCQ commands to devices not supporting NCQ */
  2885. if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
  2886. fp = 1;
  2887. goto invalid_fld;
  2888. }
  2889. /* sanity check for pio multi commands */
  2890. if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
  2891. fp = 1;
  2892. goto invalid_fld;
  2893. }
  2894. if (is_multi_taskfile(tf)) {
  2895. unsigned int multi_count = 1 << (cdb[1] >> 5);
  2896. /* compare the passed through multi_count
  2897. * with the cached multi_count of libata
  2898. */
  2899. if (multi_count != dev->multi_count)
  2900. ata_dev_warn(dev, "invalid multi_count %u ignored\n",
  2901. multi_count);
  2902. }
  2903. /*
  2904. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2905. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2906. * by an update to hardware-specific registers for each
  2907. * controller (i.e. the reason for ->set_piomode(),
  2908. * ->set_dmamode(), and ->post_set_mode() hooks).
  2909. */
  2910. if (tf->command == ATA_CMD_SET_FEATURES &&
  2911. tf->feature == SETFEATURES_XFER) {
  2912. fp = (cdb[0] == ATA_16) ? 4 : 3;
  2913. goto invalid_fld;
  2914. }
  2915. /*
  2916. * Filter TPM commands by default. These provide an
  2917. * essentially uncontrolled encrypted "back door" between
  2918. * applications and the disk. Set libata.allow_tpm=1 if you
  2919. * have a real reason for wanting to use them. This ensures
  2920. * that installed software cannot easily mess stuff up without
  2921. * user intent. DVR type users will probably ship with this enabled
  2922. * for movie content management.
  2923. *
  2924. * Note that for ATA8 we can issue a DCS change and DCS freeze lock
  2925. * for this and should do in future but that it is not sufficient as
  2926. * DCS is an optional feature set. Thus we also do the software filter
  2927. * so that we comply with the TC consortium stated goal that the user
  2928. * can turn off TC features of their system.
  2929. */
  2930. if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
  2931. fp = (cdb[0] == ATA_16) ? 14 : 9;
  2932. goto invalid_fld;
  2933. }
  2934. return 0;
  2935. invalid_fld:
  2936. ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
  2937. return 1;
  2938. }
  2939. /**
  2940. * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
  2941. * @cmd: SCSI command being translated
  2942. * @trmax: Maximum number of entries that will fit in sector_size bytes.
  2943. * @sector: Starting sector
  2944. * @count: Total Range of request in logical sectors
  2945. *
  2946. * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
  2947. * descriptor.
  2948. *
  2949. * Upto 64 entries of the format:
  2950. * 63:48 Range Length
  2951. * 47:0 LBA
  2952. *
  2953. * Range Length of 0 is ignored.
  2954. * LBA's should be sorted order and not overlap.
  2955. *
  2956. * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
  2957. *
  2958. * Return: Number of bytes copied into sglist.
  2959. */
  2960. static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
  2961. u64 sector, u32 count)
  2962. {
  2963. struct scsi_device *sdp = cmd->device;
  2964. size_t len = sdp->sector_size;
  2965. size_t r;
  2966. __le64 *buf;
  2967. u32 i = 0;
  2968. unsigned long flags;
  2969. WARN_ON(len > ATA_SCSI_RBUF_SIZE);
  2970. if (len > ATA_SCSI_RBUF_SIZE)
  2971. len = ATA_SCSI_RBUF_SIZE;
  2972. spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
  2973. buf = ((void *)ata_scsi_rbuf);
  2974. memset(buf, 0, len);
  2975. while (i < trmax) {
  2976. u64 entry = sector |
  2977. ((u64)(count > 0xffff ? 0xffff : count) << 48);
  2978. buf[i++] = __cpu_to_le64(entry);
  2979. if (count <= 0xffff)
  2980. break;
  2981. count -= 0xffff;
  2982. sector += 0xffff;
  2983. }
  2984. r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
  2985. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
  2986. return r;
  2987. }
  2988. /**
  2989. * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
  2990. * @qc: Command to be translated
  2991. *
  2992. * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
  2993. * an SCT Write Same command.
  2994. * Based on WRITE SAME has the UNMAP flag:
  2995. *
  2996. * - When set translate to DSM TRIM
  2997. * - When clear translate to SCT Write Same
  2998. */
  2999. static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
  3000. {
  3001. struct ata_taskfile *tf = &qc->tf;
  3002. struct scsi_cmnd *scmd = qc->scsicmd;
  3003. struct scsi_device *sdp = scmd->device;
  3004. size_t len = sdp->sector_size;
  3005. struct ata_device *dev = qc->dev;
  3006. const u8 *cdb = scmd->cmnd;
  3007. u64 block;
  3008. u32 n_block;
  3009. const u32 trmax = len >> 3;
  3010. u32 size;
  3011. u16 fp;
  3012. u8 bp = 0xff;
  3013. u8 unmap = cdb[1] & 0x8;
  3014. /* we may not issue DMA commands if no DMA mode is set */
  3015. if (unlikely(!dev->dma_mode))
  3016. goto invalid_opcode;
  3017. /*
  3018. * We only allow sending this command through the block layer,
  3019. * as it modifies the DATA OUT buffer, which would corrupt user
  3020. * memory for SG_IO commands.
  3021. */
  3022. if (unlikely(blk_rq_is_passthrough(scmd->request)))
  3023. goto invalid_opcode;
  3024. if (unlikely(scmd->cmd_len < 16)) {
  3025. fp = 15;
  3026. goto invalid_fld;
  3027. }
  3028. scsi_16_lba_len(cdb, &block, &n_block);
  3029. if (!unmap ||
  3030. (dev->horkage & ATA_HORKAGE_NOTRIM) ||
  3031. !ata_id_has_trim(dev->id)) {
  3032. fp = 1;
  3033. bp = 3;
  3034. goto invalid_fld;
  3035. }
  3036. /* If the request is too large the cmd is invalid */
  3037. if (n_block > 0xffff * trmax) {
  3038. fp = 2;
  3039. goto invalid_fld;
  3040. }
  3041. /*
  3042. * WRITE SAME always has a sector sized buffer as payload, this
  3043. * should never be a multiple entry S/G list.
  3044. */
  3045. if (!scsi_sg_count(scmd))
  3046. goto invalid_param_len;
  3047. /*
  3048. * size must match sector size in bytes
  3049. * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
  3050. * is defined as number of 512 byte blocks to be transferred.
  3051. */
  3052. size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
  3053. if (size != len)
  3054. goto invalid_param_len;
  3055. if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
  3056. /* Newer devices support queued TRIM commands */
  3057. tf->protocol = ATA_PROT_NCQ;
  3058. tf->command = ATA_CMD_FPDMA_SEND;
  3059. tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
  3060. tf->nsect = qc->hw_tag << 3;
  3061. tf->hob_feature = (size / 512) >> 8;
  3062. tf->feature = size / 512;
  3063. tf->auxiliary = 1;
  3064. } else {
  3065. tf->protocol = ATA_PROT_DMA;
  3066. tf->hob_feature = 0;
  3067. tf->feature = ATA_DSM_TRIM;
  3068. tf->hob_nsect = (size / 512) >> 8;
  3069. tf->nsect = size / 512;
  3070. tf->command = ATA_CMD_DSM;
  3071. }
  3072. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
  3073. ATA_TFLAG_WRITE;
  3074. ata_qc_set_pc_nbytes(qc);
  3075. return 0;
  3076. invalid_fld:
  3077. ata_scsi_set_invalid_field(dev, scmd, fp, bp);
  3078. return 1;
  3079. invalid_param_len:
  3080. /* "Parameter list length error" */
  3081. ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  3082. return 1;
  3083. invalid_opcode:
  3084. /* "Invalid command operation code" */
  3085. ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
  3086. return 1;
  3087. }
  3088. /**
  3089. * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
  3090. * @args: device MAINTENANCE_IN data / SCSI command of interest.
  3091. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  3092. *
  3093. * Yields a subset to satisfy scsi_report_opcode()
  3094. *
  3095. * LOCKING:
  3096. * spin_lock_irqsave(host lock)
  3097. */
  3098. static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
  3099. {
  3100. struct ata_device *dev = args->dev;
  3101. u8 *cdb = args->cmd->cmnd;
  3102. u8 supported = 0;
  3103. unsigned int err = 0;
  3104. if (cdb[2] != 1) {
  3105. ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
  3106. err = 2;
  3107. goto out;
  3108. }
  3109. switch (cdb[3]) {
  3110. case INQUIRY:
  3111. case MODE_SENSE:
  3112. case MODE_SENSE_10:
  3113. case READ_CAPACITY:
  3114. case SERVICE_ACTION_IN_16:
  3115. case REPORT_LUNS:
  3116. case REQUEST_SENSE:
  3117. case SYNCHRONIZE_CACHE:
  3118. case REZERO_UNIT:
  3119. case SEEK_6:
  3120. case SEEK_10:
  3121. case TEST_UNIT_READY:
  3122. case SEND_DIAGNOSTIC:
  3123. case MAINTENANCE_IN:
  3124. case READ_6:
  3125. case READ_10:
  3126. case READ_16:
  3127. case WRITE_6:
  3128. case WRITE_10:
  3129. case WRITE_16:
  3130. case ATA_12:
  3131. case ATA_16:
  3132. case VERIFY:
  3133. case VERIFY_16:
  3134. case MODE_SELECT:
  3135. case MODE_SELECT_10:
  3136. case START_STOP:
  3137. supported = 3;
  3138. break;
  3139. case ZBC_IN:
  3140. case ZBC_OUT:
  3141. if (ata_id_zoned_cap(dev->id) ||
  3142. dev->class == ATA_DEV_ZAC)
  3143. supported = 3;
  3144. break;
  3145. case SECURITY_PROTOCOL_IN:
  3146. case SECURITY_PROTOCOL_OUT:
  3147. if (dev->flags & ATA_DFLAG_TRUSTED)
  3148. supported = 3;
  3149. break;
  3150. default:
  3151. break;
  3152. }
  3153. out:
  3154. rbuf[1] = supported; /* supported */
  3155. return err;
  3156. }
  3157. /**
  3158. * ata_scsi_report_zones_complete - convert ATA output
  3159. * @qc: command structure returning the data
  3160. *
  3161. * Convert T-13 little-endian field representation into
  3162. * T-10 big-endian field representation.
  3163. * What a mess.
  3164. */
  3165. static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
  3166. {
  3167. struct scsi_cmnd *scmd = qc->scsicmd;
  3168. struct sg_mapping_iter miter;
  3169. unsigned long flags;
  3170. unsigned int bytes = 0;
  3171. sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
  3172. SG_MITER_TO_SG | SG_MITER_ATOMIC);
  3173. local_irq_save(flags);
  3174. while (sg_miter_next(&miter)) {
  3175. unsigned int offset = 0;
  3176. if (bytes == 0) {
  3177. char *hdr;
  3178. u32 list_length;
  3179. u64 max_lba, opt_lba;
  3180. u16 same;
  3181. /* Swizzle header */
  3182. hdr = miter.addr;
  3183. list_length = get_unaligned_le32(&hdr[0]);
  3184. same = get_unaligned_le16(&hdr[4]);
  3185. max_lba = get_unaligned_le64(&hdr[8]);
  3186. opt_lba = get_unaligned_le64(&hdr[16]);
  3187. put_unaligned_be32(list_length, &hdr[0]);
  3188. hdr[4] = same & 0xf;
  3189. put_unaligned_be64(max_lba, &hdr[8]);
  3190. put_unaligned_be64(opt_lba, &hdr[16]);
  3191. offset += 64;
  3192. bytes += 64;
  3193. }
  3194. while (offset < miter.length) {
  3195. char *rec;
  3196. u8 cond, type, non_seq, reset;
  3197. u64 size, start, wp;
  3198. /* Swizzle zone descriptor */
  3199. rec = miter.addr + offset;
  3200. type = rec[0] & 0xf;
  3201. cond = (rec[1] >> 4) & 0xf;
  3202. non_seq = (rec[1] & 2);
  3203. reset = (rec[1] & 1);
  3204. size = get_unaligned_le64(&rec[8]);
  3205. start = get_unaligned_le64(&rec[16]);
  3206. wp = get_unaligned_le64(&rec[24]);
  3207. rec[0] = type;
  3208. rec[1] = (cond << 4) | non_seq | reset;
  3209. put_unaligned_be64(size, &rec[8]);
  3210. put_unaligned_be64(start, &rec[16]);
  3211. put_unaligned_be64(wp, &rec[24]);
  3212. WARN_ON(offset + 64 > miter.length);
  3213. offset += 64;
  3214. bytes += 64;
  3215. }
  3216. }
  3217. sg_miter_stop(&miter);
  3218. local_irq_restore(flags);
  3219. ata_scsi_qc_complete(qc);
  3220. }
  3221. static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
  3222. {
  3223. struct ata_taskfile *tf = &qc->tf;
  3224. struct scsi_cmnd *scmd = qc->scsicmd;
  3225. const u8 *cdb = scmd->cmnd;
  3226. u16 sect, fp = (u16)-1;
  3227. u8 sa, options, bp = 0xff;
  3228. u64 block;
  3229. u32 n_block;
  3230. if (unlikely(scmd->cmd_len < 16)) {
  3231. ata_dev_warn(qc->dev, "invalid cdb length %d\n",
  3232. scmd->cmd_len);
  3233. fp = 15;
  3234. goto invalid_fld;
  3235. }
  3236. scsi_16_lba_len(cdb, &block, &n_block);
  3237. if (n_block != scsi_bufflen(scmd)) {
  3238. ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
  3239. n_block, scsi_bufflen(scmd));
  3240. goto invalid_param_len;
  3241. }
  3242. sa = cdb[1] & 0x1f;
  3243. if (sa != ZI_REPORT_ZONES) {
  3244. ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
  3245. fp = 1;
  3246. goto invalid_fld;
  3247. }
  3248. /*
  3249. * ZAC allows only for transfers in 512 byte blocks,
  3250. * and uses a 16 bit value for the transfer count.
  3251. */
  3252. if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
  3253. ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
  3254. goto invalid_param_len;
  3255. }
  3256. sect = n_block / 512;
  3257. options = cdb[14] & 0xbf;
  3258. if (ata_ncq_enabled(qc->dev) &&
  3259. ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
  3260. tf->protocol = ATA_PROT_NCQ;
  3261. tf->command = ATA_CMD_FPDMA_RECV;
  3262. tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
  3263. tf->nsect = qc->hw_tag << 3;
  3264. tf->feature = sect & 0xff;
  3265. tf->hob_feature = (sect >> 8) & 0xff;
  3266. tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
  3267. } else {
  3268. tf->command = ATA_CMD_ZAC_MGMT_IN;
  3269. tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
  3270. tf->protocol = ATA_PROT_DMA;
  3271. tf->hob_feature = options;
  3272. tf->hob_nsect = (sect >> 8) & 0xff;
  3273. tf->nsect = sect & 0xff;
  3274. }
  3275. tf->device = ATA_LBA;
  3276. tf->lbah = (block >> 16) & 0xff;
  3277. tf->lbam = (block >> 8) & 0xff;
  3278. tf->lbal = block & 0xff;
  3279. tf->hob_lbah = (block >> 40) & 0xff;
  3280. tf->hob_lbam = (block >> 32) & 0xff;
  3281. tf->hob_lbal = (block >> 24) & 0xff;
  3282. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  3283. qc->flags |= ATA_QCFLAG_RESULT_TF;
  3284. ata_qc_set_pc_nbytes(qc);
  3285. qc->complete_fn = ata_scsi_report_zones_complete;
  3286. return 0;
  3287. invalid_fld:
  3288. ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
  3289. return 1;
  3290. invalid_param_len:
  3291. /* "Parameter list length error" */
  3292. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  3293. return 1;
  3294. }
  3295. static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
  3296. {
  3297. struct ata_taskfile *tf = &qc->tf;
  3298. struct scsi_cmnd *scmd = qc->scsicmd;
  3299. struct ata_device *dev = qc->dev;
  3300. const u8 *cdb = scmd->cmnd;
  3301. u8 all, sa;
  3302. u64 block;
  3303. u32 n_block;
  3304. u16 fp = (u16)-1;
  3305. if (unlikely(scmd->cmd_len < 16)) {
  3306. fp = 15;
  3307. goto invalid_fld;
  3308. }
  3309. sa = cdb[1] & 0x1f;
  3310. if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
  3311. (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
  3312. fp = 1;
  3313. goto invalid_fld;
  3314. }
  3315. scsi_16_lba_len(cdb, &block, &n_block);
  3316. if (n_block) {
  3317. /*
  3318. * ZAC MANAGEMENT OUT doesn't define any length
  3319. */
  3320. goto invalid_param_len;
  3321. }
  3322. all = cdb[14] & 0x1;
  3323. if (all) {
  3324. /*
  3325. * Ignore the block address (zone ID) as defined by ZBC.
  3326. */
  3327. block = 0;
  3328. } else if (block >= dev->n_sectors) {
  3329. /*
  3330. * Block must be a valid zone ID (a zone start LBA).
  3331. */
  3332. fp = 2;
  3333. goto invalid_fld;
  3334. }
  3335. if (ata_ncq_enabled(qc->dev) &&
  3336. ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
  3337. tf->protocol = ATA_PROT_NCQ_NODATA;
  3338. tf->command = ATA_CMD_NCQ_NON_DATA;
  3339. tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
  3340. tf->nsect = qc->hw_tag << 3;
  3341. tf->auxiliary = sa | ((u16)all << 8);
  3342. } else {
  3343. tf->protocol = ATA_PROT_NODATA;
  3344. tf->command = ATA_CMD_ZAC_MGMT_OUT;
  3345. tf->feature = sa;
  3346. tf->hob_feature = all;
  3347. }
  3348. tf->lbah = (block >> 16) & 0xff;
  3349. tf->lbam = (block >> 8) & 0xff;
  3350. tf->lbal = block & 0xff;
  3351. tf->hob_lbah = (block >> 40) & 0xff;
  3352. tf->hob_lbam = (block >> 32) & 0xff;
  3353. tf->hob_lbal = (block >> 24) & 0xff;
  3354. tf->device = ATA_LBA;
  3355. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  3356. return 0;
  3357. invalid_fld:
  3358. ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
  3359. return 1;
  3360. invalid_param_len:
  3361. /* "Parameter list length error" */
  3362. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  3363. return 1;
  3364. }
  3365. /**
  3366. * ata_mselect_caching - Simulate MODE SELECT for caching info page
  3367. * @qc: Storage for translated ATA taskfile
  3368. * @buf: input buffer
  3369. * @len: number of valid bytes in the input buffer
  3370. * @fp: out parameter for the failed field on error
  3371. *
  3372. * Prepare a taskfile to modify caching information for the device.
  3373. *
  3374. * LOCKING:
  3375. * None.
  3376. */
  3377. static int ata_mselect_caching(struct ata_queued_cmd *qc,
  3378. const u8 *buf, int len, u16 *fp)
  3379. {
  3380. struct ata_taskfile *tf = &qc->tf;
  3381. struct ata_device *dev = qc->dev;
  3382. u8 mpage[CACHE_MPAGE_LEN];
  3383. u8 wce;
  3384. int i;
  3385. /*
  3386. * The first two bytes of def_cache_mpage are a header, so offsets
  3387. * in mpage are off by 2 compared to buf. Same for len.
  3388. */
  3389. if (len != CACHE_MPAGE_LEN - 2) {
  3390. if (len < CACHE_MPAGE_LEN - 2)
  3391. *fp = len;
  3392. else
  3393. *fp = CACHE_MPAGE_LEN - 2;
  3394. return -EINVAL;
  3395. }
  3396. wce = buf[0] & (1 << 2);
  3397. /*
  3398. * Check that read-only bits are not modified.
  3399. */
  3400. ata_msense_caching(dev->id, mpage, false);
  3401. for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
  3402. if (i == 0)
  3403. continue;
  3404. if (mpage[i + 2] != buf[i]) {
  3405. *fp = i;
  3406. return -EINVAL;
  3407. }
  3408. }
  3409. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  3410. tf->protocol = ATA_PROT_NODATA;
  3411. tf->nsect = 0;
  3412. tf->command = ATA_CMD_SET_FEATURES;
  3413. tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
  3414. return 0;
  3415. }
  3416. /**
  3417. * ata_mselect_control - Simulate MODE SELECT for control page
  3418. * @qc: Storage for translated ATA taskfile
  3419. * @buf: input buffer
  3420. * @len: number of valid bytes in the input buffer
  3421. * @fp: out parameter for the failed field on error
  3422. *
  3423. * Prepare a taskfile to modify caching information for the device.
  3424. *
  3425. * LOCKING:
  3426. * None.
  3427. */
  3428. static int ata_mselect_control(struct ata_queued_cmd *qc,
  3429. const u8 *buf, int len, u16 *fp)
  3430. {
  3431. struct ata_device *dev = qc->dev;
  3432. u8 mpage[CONTROL_MPAGE_LEN];
  3433. u8 d_sense;
  3434. int i;
  3435. /*
  3436. * The first two bytes of def_control_mpage are a header, so offsets
  3437. * in mpage are off by 2 compared to buf. Same for len.
  3438. */
  3439. if (len != CONTROL_MPAGE_LEN - 2) {
  3440. if (len < CONTROL_MPAGE_LEN - 2)
  3441. *fp = len;
  3442. else
  3443. *fp = CONTROL_MPAGE_LEN - 2;
  3444. return -EINVAL;
  3445. }
  3446. d_sense = buf[0] & (1 << 2);
  3447. /*
  3448. * Check that read-only bits are not modified.
  3449. */
  3450. ata_msense_control(dev, mpage, false);
  3451. for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
  3452. if (i == 0)
  3453. continue;
  3454. if (mpage[2 + i] != buf[i]) {
  3455. *fp = i;
  3456. return -EINVAL;
  3457. }
  3458. }
  3459. if (d_sense & (1 << 2))
  3460. dev->flags |= ATA_DFLAG_D_SENSE;
  3461. else
  3462. dev->flags &= ~ATA_DFLAG_D_SENSE;
  3463. return 0;
  3464. }
  3465. /**
  3466. * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
  3467. * @qc: Storage for translated ATA taskfile
  3468. *
  3469. * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
  3470. * Assume this is invoked for direct access devices (e.g. disks) only.
  3471. * There should be no block descriptor for other device types.
  3472. *
  3473. * LOCKING:
  3474. * spin_lock_irqsave(host lock)
  3475. */
  3476. static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
  3477. {
  3478. struct scsi_cmnd *scmd = qc->scsicmd;
  3479. const u8 *cdb = scmd->cmnd;
  3480. const u8 *p;
  3481. u8 pg, spg;
  3482. unsigned six_byte, pg_len, hdr_len, bd_len;
  3483. int len;
  3484. u16 fp = (u16)-1;
  3485. u8 bp = 0xff;
  3486. VPRINTK("ENTER\n");
  3487. six_byte = (cdb[0] == MODE_SELECT);
  3488. if (six_byte) {
  3489. if (scmd->cmd_len < 5) {
  3490. fp = 4;
  3491. goto invalid_fld;
  3492. }
  3493. len = cdb[4];
  3494. hdr_len = 4;
  3495. } else {
  3496. if (scmd->cmd_len < 9) {
  3497. fp = 8;
  3498. goto invalid_fld;
  3499. }
  3500. len = (cdb[7] << 8) + cdb[8];
  3501. hdr_len = 8;
  3502. }
  3503. /* We only support PF=1, SP=0. */
  3504. if ((cdb[1] & 0x11) != 0x10) {
  3505. fp = 1;
  3506. bp = (cdb[1] & 0x01) ? 1 : 5;
  3507. goto invalid_fld;
  3508. }
  3509. /* Test early for possible overrun. */
  3510. if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
  3511. goto invalid_param_len;
  3512. p = page_address(sg_page(scsi_sglist(scmd)));
  3513. /* Move past header and block descriptors. */
  3514. if (len < hdr_len)
  3515. goto invalid_param_len;
  3516. if (six_byte)
  3517. bd_len = p[3];
  3518. else
  3519. bd_len = (p[6] << 8) + p[7];
  3520. len -= hdr_len;
  3521. p += hdr_len;
  3522. if (len < bd_len)
  3523. goto invalid_param_len;
  3524. if (bd_len != 0 && bd_len != 8) {
  3525. fp = (six_byte) ? 3 : 6;
  3526. fp += bd_len + hdr_len;
  3527. goto invalid_param;
  3528. }
  3529. len -= bd_len;
  3530. p += bd_len;
  3531. if (len == 0)
  3532. goto skip;
  3533. /* Parse both possible formats for the mode page headers. */
  3534. pg = p[0] & 0x3f;
  3535. if (p[0] & 0x40) {
  3536. if (len < 4)
  3537. goto invalid_param_len;
  3538. spg = p[1];
  3539. pg_len = (p[2] << 8) | p[3];
  3540. p += 4;
  3541. len -= 4;
  3542. } else {
  3543. if (len < 2)
  3544. goto invalid_param_len;
  3545. spg = 0;
  3546. pg_len = p[1];
  3547. p += 2;
  3548. len -= 2;
  3549. }
  3550. /*
  3551. * No mode subpages supported (yet) but asking for _all_
  3552. * subpages may be valid
  3553. */
  3554. if (spg && (spg != ALL_SUB_MPAGES)) {
  3555. fp = (p[0] & 0x40) ? 1 : 0;
  3556. fp += hdr_len + bd_len;
  3557. goto invalid_param;
  3558. }
  3559. if (pg_len > len)
  3560. goto invalid_param_len;
  3561. switch (pg) {
  3562. case CACHE_MPAGE:
  3563. if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
  3564. fp += hdr_len + bd_len;
  3565. goto invalid_param;
  3566. }
  3567. break;
  3568. case CONTROL_MPAGE:
  3569. if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
  3570. fp += hdr_len + bd_len;
  3571. goto invalid_param;
  3572. } else {
  3573. goto skip; /* No ATA command to send */
  3574. }
  3575. break;
  3576. default: /* invalid page code */
  3577. fp = bd_len + hdr_len;
  3578. goto invalid_param;
  3579. }
  3580. /*
  3581. * Only one page has changeable data, so we only support setting one
  3582. * page at a time.
  3583. */
  3584. if (len > pg_len)
  3585. goto invalid_param;
  3586. return 0;
  3587. invalid_fld:
  3588. ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
  3589. return 1;
  3590. invalid_param:
  3591. ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
  3592. return 1;
  3593. invalid_param_len:
  3594. /* "Parameter list length error" */
  3595. ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
  3596. return 1;
  3597. skip:
  3598. scmd->result = SAM_STAT_GOOD;
  3599. return 1;
  3600. }
  3601. static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
  3602. {
  3603. if (len == 0)
  3604. return ATA_CMD_TRUSTED_NONDATA;
  3605. else if (send)
  3606. return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
  3607. else
  3608. return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
  3609. }
  3610. static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
  3611. {
  3612. struct scsi_cmnd *scmd = qc->scsicmd;
  3613. const u8 *cdb = scmd->cmnd;
  3614. struct ata_taskfile *tf = &qc->tf;
  3615. u8 secp = cdb[1];
  3616. bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
  3617. u16 spsp = get_unaligned_be16(&cdb[2]);
  3618. u32 len = get_unaligned_be32(&cdb[6]);
  3619. bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
  3620. /*
  3621. * We don't support the ATA "security" protocol.
  3622. */
  3623. if (secp == 0xef) {
  3624. ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
  3625. return 1;
  3626. }
  3627. if (cdb[4] & 7) { /* INC_512 */
  3628. if (len > 0xffff) {
  3629. ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
  3630. return 1;
  3631. }
  3632. } else {
  3633. if (len > 0x01fffe00) {
  3634. ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
  3635. return 1;
  3636. }
  3637. /* convert to the sector-based ATA addressing */
  3638. len = (len + 511) / 512;
  3639. }
  3640. tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
  3641. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
  3642. if (send)
  3643. tf->flags |= ATA_TFLAG_WRITE;
  3644. tf->command = ata_scsi_trusted_op(len, send, dma);
  3645. tf->feature = secp;
  3646. tf->lbam = spsp & 0xff;
  3647. tf->lbah = spsp >> 8;
  3648. if (len) {
  3649. tf->nsect = len & 0xff;
  3650. tf->lbal = len >> 8;
  3651. } else {
  3652. if (!send)
  3653. tf->lbah = (1 << 7);
  3654. }
  3655. ata_qc_set_pc_nbytes(qc);
  3656. return 0;
  3657. }
  3658. /**
  3659. * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
  3660. * @qc: Command to be translated
  3661. *
  3662. * Translate a SCSI variable length CDB to specified commands.
  3663. * It checks a service action value in CDB to call corresponding handler.
  3664. *
  3665. * RETURNS:
  3666. * Zero on success, non-zero on failure
  3667. *
  3668. */
  3669. static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
  3670. {
  3671. struct scsi_cmnd *scmd = qc->scsicmd;
  3672. const u8 *cdb = scmd->cmnd;
  3673. const u16 sa = get_unaligned_be16(&cdb[8]);
  3674. /*
  3675. * if service action represents a ata pass-thru(32) command,
  3676. * then pass it to ata_scsi_pass_thru handler.
  3677. */
  3678. if (sa == ATA_32)
  3679. return ata_scsi_pass_thru(qc);
  3680. /* unsupported service action */
  3681. return 1;
  3682. }
  3683. /**
  3684. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  3685. * @dev: ATA device
  3686. * @cmd: SCSI command opcode to consider
  3687. *
  3688. * Look up the SCSI command given, and determine whether the
  3689. * SCSI command is to be translated or simulated.
  3690. *
  3691. * RETURNS:
  3692. * Pointer to translation function if possible, %NULL if not.
  3693. */
  3694. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  3695. {
  3696. switch (cmd) {
  3697. case READ_6:
  3698. case READ_10:
  3699. case READ_16:
  3700. case WRITE_6:
  3701. case WRITE_10:
  3702. case WRITE_16:
  3703. return ata_scsi_rw_xlat;
  3704. case WRITE_SAME_16:
  3705. return ata_scsi_write_same_xlat;
  3706. case SYNCHRONIZE_CACHE:
  3707. if (ata_try_flush_cache(dev))
  3708. return ata_scsi_flush_xlat;
  3709. break;
  3710. case VERIFY:
  3711. case VERIFY_16:
  3712. return ata_scsi_verify_xlat;
  3713. case ATA_12:
  3714. case ATA_16:
  3715. return ata_scsi_pass_thru;
  3716. case VARIABLE_LENGTH_CMD:
  3717. return ata_scsi_var_len_cdb_xlat;
  3718. case MODE_SELECT:
  3719. case MODE_SELECT_10:
  3720. return ata_scsi_mode_select_xlat;
  3721. break;
  3722. case ZBC_IN:
  3723. return ata_scsi_zbc_in_xlat;
  3724. case ZBC_OUT:
  3725. return ata_scsi_zbc_out_xlat;
  3726. case SECURITY_PROTOCOL_IN:
  3727. case SECURITY_PROTOCOL_OUT:
  3728. if (!(dev->flags & ATA_DFLAG_TRUSTED))
  3729. break;
  3730. return ata_scsi_security_inout_xlat;
  3731. case START_STOP:
  3732. return ata_scsi_start_stop_xlat;
  3733. }
  3734. return NULL;
  3735. }
  3736. /**
  3737. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  3738. * @ap: ATA port to which the command was being sent
  3739. * @cmd: SCSI command to dump
  3740. *
  3741. * Prints the contents of a SCSI command via printk().
  3742. */
  3743. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  3744. struct scsi_cmnd *cmd)
  3745. {
  3746. #ifdef ATA_VERBOSE_DEBUG
  3747. struct scsi_device *scsidev = cmd->device;
  3748. VPRINTK("CDB (%u:%d,%d,%lld) %9ph\n",
  3749. ap->print_id,
  3750. scsidev->channel, scsidev->id, scsidev->lun,
  3751. cmd->cmnd);
  3752. #endif
  3753. }
  3754. static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
  3755. struct ata_device *dev)
  3756. {
  3757. u8 scsi_op = scmd->cmnd[0];
  3758. ata_xlat_func_t xlat_func;
  3759. int rc = 0;
  3760. if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
  3761. if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
  3762. goto bad_cdb_len;
  3763. xlat_func = ata_get_xlat_func(dev, scsi_op);
  3764. } else {
  3765. if (unlikely(!scmd->cmd_len))
  3766. goto bad_cdb_len;
  3767. xlat_func = NULL;
  3768. if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
  3769. /* relay SCSI command to ATAPI device */
  3770. int len = COMMAND_SIZE(scsi_op);
  3771. if (unlikely(len > scmd->cmd_len ||
  3772. len > dev->cdb_len ||
  3773. scmd->cmd_len > ATAPI_CDB_LEN))
  3774. goto bad_cdb_len;
  3775. xlat_func = atapi_xlat;
  3776. } else {
  3777. /* ATA_16 passthru, treat as an ATA command */
  3778. if (unlikely(scmd->cmd_len > 16))
  3779. goto bad_cdb_len;
  3780. xlat_func = ata_get_xlat_func(dev, scsi_op);
  3781. }
  3782. }
  3783. if (xlat_func)
  3784. rc = ata_scsi_translate(dev, scmd, xlat_func);
  3785. else
  3786. ata_scsi_simulate(dev, scmd);
  3787. return rc;
  3788. bad_cdb_len:
  3789. DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
  3790. scmd->cmd_len, scsi_op, dev->cdb_len);
  3791. scmd->result = DID_ERROR << 16;
  3792. scmd->scsi_done(scmd);
  3793. return 0;
  3794. }
  3795. /**
  3796. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  3797. * @shost: SCSI host of command to be sent
  3798. * @cmd: SCSI command to be sent
  3799. *
  3800. * In some cases, this function translates SCSI commands into
  3801. * ATA taskfiles, and queues the taskfiles to be sent to
  3802. * hardware. In other cases, this function simulates a
  3803. * SCSI device by evaluating and responding to certain
  3804. * SCSI commands. This creates the overall effect of
  3805. * ATA and ATAPI devices appearing as SCSI devices.
  3806. *
  3807. * LOCKING:
  3808. * ATA host lock
  3809. *
  3810. * RETURNS:
  3811. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3812. * 0 otherwise.
  3813. */
  3814. int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  3815. {
  3816. struct ata_port *ap;
  3817. struct ata_device *dev;
  3818. struct scsi_device *scsidev = cmd->device;
  3819. int rc = 0;
  3820. unsigned long irq_flags;
  3821. ap = ata_shost_to_port(shost);
  3822. spin_lock_irqsave(ap->lock, irq_flags);
  3823. ata_scsi_dump_cdb(ap, cmd);
  3824. dev = ata_scsi_find_dev(ap, scsidev);
  3825. if (likely(dev))
  3826. rc = __ata_scsi_queuecmd(cmd, dev);
  3827. else {
  3828. cmd->result = (DID_BAD_TARGET << 16);
  3829. cmd->scsi_done(cmd);
  3830. }
  3831. spin_unlock_irqrestore(ap->lock, irq_flags);
  3832. return rc;
  3833. }
  3834. /**
  3835. * ata_scsi_simulate - simulate SCSI command on ATA device
  3836. * @dev: the target device
  3837. * @cmd: SCSI command being sent to device.
  3838. *
  3839. * Interprets and directly executes a select list of SCSI commands
  3840. * that can be handled internally.
  3841. *
  3842. * LOCKING:
  3843. * spin_lock_irqsave(host lock)
  3844. */
  3845. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
  3846. {
  3847. struct ata_scsi_args args;
  3848. const u8 *scsicmd = cmd->cmnd;
  3849. u8 tmp8;
  3850. args.dev = dev;
  3851. args.id = dev->id;
  3852. args.cmd = cmd;
  3853. switch(scsicmd[0]) {
  3854. case INQUIRY:
  3855. if (scsicmd[1] & 2) /* is CmdDt set? */
  3856. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3857. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  3858. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  3859. else switch (scsicmd[2]) {
  3860. case 0x00:
  3861. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  3862. break;
  3863. case 0x80:
  3864. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  3865. break;
  3866. case 0x83:
  3867. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  3868. break;
  3869. case 0x89:
  3870. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
  3871. break;
  3872. case 0xb0:
  3873. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
  3874. break;
  3875. case 0xb1:
  3876. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
  3877. break;
  3878. case 0xb2:
  3879. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
  3880. break;
  3881. case 0xb6:
  3882. if (dev->flags & ATA_DFLAG_ZAC) {
  3883. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
  3884. break;
  3885. }
  3886. /* Fallthrough */
  3887. default:
  3888. ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
  3889. break;
  3890. }
  3891. break;
  3892. case MODE_SENSE:
  3893. case MODE_SENSE_10:
  3894. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  3895. break;
  3896. case READ_CAPACITY:
  3897. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3898. break;
  3899. case SERVICE_ACTION_IN_16:
  3900. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  3901. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  3902. else
  3903. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3904. break;
  3905. case REPORT_LUNS:
  3906. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  3907. break;
  3908. case REQUEST_SENSE:
  3909. ata_scsi_set_sense(dev, cmd, 0, 0, 0);
  3910. cmd->result = (DRIVER_SENSE << 24);
  3911. break;
  3912. /* if we reach this, then writeback caching is disabled,
  3913. * turning this into a no-op.
  3914. */
  3915. case SYNCHRONIZE_CACHE:
  3916. /* fall through */
  3917. /* no-op's, complete with success */
  3918. case REZERO_UNIT:
  3919. case SEEK_6:
  3920. case SEEK_10:
  3921. case TEST_UNIT_READY:
  3922. break;
  3923. case SEND_DIAGNOSTIC:
  3924. tmp8 = scsicmd[1] & ~(1 << 3);
  3925. if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
  3926. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3927. break;
  3928. case MAINTENANCE_IN:
  3929. if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
  3930. ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
  3931. else
  3932. ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
  3933. break;
  3934. /* all other commands */
  3935. default:
  3936. ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  3937. /* "Invalid command operation code" */
  3938. break;
  3939. }
  3940. cmd->scsi_done(cmd);
  3941. }
  3942. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
  3943. {
  3944. int i, rc;
  3945. for (i = 0; i < host->n_ports; i++) {
  3946. struct ata_port *ap = host->ports[i];
  3947. struct Scsi_Host *shost;
  3948. rc = -ENOMEM;
  3949. shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
  3950. if (!shost)
  3951. goto err_alloc;
  3952. shost->eh_noresume = 1;
  3953. *(struct ata_port **)&shost->hostdata[0] = ap;
  3954. ap->scsi_host = shost;
  3955. shost->transportt = ata_scsi_transport_template;
  3956. shost->unique_id = ap->print_id;
  3957. shost->max_id = 16;
  3958. shost->max_lun = 1;
  3959. shost->max_channel = 1;
  3960. shost->max_cmd_len = 32;
  3961. /* Schedule policy is determined by ->qc_defer()
  3962. * callback and it needs to see every deferred qc.
  3963. * Set host_blocked to 1 to prevent SCSI midlayer from
  3964. * automatically deferring requests.
  3965. */
  3966. shost->max_host_blocked = 1;
  3967. rc = scsi_add_host_with_dma(ap->scsi_host,
  3968. &ap->tdev, ap->host->dev);
  3969. if (rc)
  3970. goto err_add;
  3971. }
  3972. return 0;
  3973. err_add:
  3974. scsi_host_put(host->ports[i]->scsi_host);
  3975. err_alloc:
  3976. while (--i >= 0) {
  3977. struct Scsi_Host *shost = host->ports[i]->scsi_host;
  3978. scsi_remove_host(shost);
  3979. scsi_host_put(shost);
  3980. }
  3981. return rc;
  3982. }
  3983. void ata_scsi_scan_host(struct ata_port *ap, int sync)
  3984. {
  3985. int tries = 5;
  3986. struct ata_device *last_failed_dev = NULL;
  3987. struct ata_link *link;
  3988. struct ata_device *dev;
  3989. repeat:
  3990. ata_for_each_link(link, ap, EDGE) {
  3991. ata_for_each_dev(dev, link, ENABLED) {
  3992. struct scsi_device *sdev;
  3993. int channel = 0, id = 0;
  3994. if (dev->sdev)
  3995. continue;
  3996. if (ata_is_host_link(link))
  3997. id = dev->devno;
  3998. else
  3999. channel = link->pmp;
  4000. sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
  4001. NULL);
  4002. if (!IS_ERR(sdev)) {
  4003. dev->sdev = sdev;
  4004. scsi_device_put(sdev);
  4005. } else {
  4006. dev->sdev = NULL;
  4007. }
  4008. }
  4009. }
  4010. /* If we scanned while EH was in progress or allocation
  4011. * failure occurred, scan would have failed silently. Check
  4012. * whether all devices are attached.
  4013. */
  4014. ata_for_each_link(link, ap, EDGE) {
  4015. ata_for_each_dev(dev, link, ENABLED) {
  4016. if (!dev->sdev)
  4017. goto exit_loop;
  4018. }
  4019. }
  4020. exit_loop:
  4021. if (!link)
  4022. return;
  4023. /* we're missing some SCSI devices */
  4024. if (sync) {
  4025. /* If caller requested synchrnous scan && we've made
  4026. * any progress, sleep briefly and repeat.
  4027. */
  4028. if (dev != last_failed_dev) {
  4029. msleep(100);
  4030. last_failed_dev = dev;
  4031. goto repeat;
  4032. }
  4033. /* We might be failing to detect boot device, give it
  4034. * a few more chances.
  4035. */
  4036. if (--tries) {
  4037. msleep(100);
  4038. goto repeat;
  4039. }
  4040. ata_port_err(ap,
  4041. "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
  4042. }
  4043. queue_delayed_work(system_long_wq, &ap->hotplug_task,
  4044. round_jiffies_relative(HZ));
  4045. }
  4046. /**
  4047. * ata_scsi_offline_dev - offline attached SCSI device
  4048. * @dev: ATA device to offline attached SCSI device for
  4049. *
  4050. * This function is called from ata_eh_hotplug() and responsible
  4051. * for taking the SCSI device attached to @dev offline. This
  4052. * function is called with host lock which protects dev->sdev
  4053. * against clearing.
  4054. *
  4055. * LOCKING:
  4056. * spin_lock_irqsave(host lock)
  4057. *
  4058. * RETURNS:
  4059. * 1 if attached SCSI device exists, 0 otherwise.
  4060. */
  4061. int ata_scsi_offline_dev(struct ata_device *dev)
  4062. {
  4063. if (dev->sdev) {
  4064. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  4065. return 1;
  4066. }
  4067. return 0;
  4068. }
  4069. /**
  4070. * ata_scsi_remove_dev - remove attached SCSI device
  4071. * @dev: ATA device to remove attached SCSI device for
  4072. *
  4073. * This function is called from ata_eh_scsi_hotplug() and
  4074. * responsible for removing the SCSI device attached to @dev.
  4075. *
  4076. * LOCKING:
  4077. * Kernel thread context (may sleep).
  4078. */
  4079. static void ata_scsi_remove_dev(struct ata_device *dev)
  4080. {
  4081. struct ata_port *ap = dev->link->ap;
  4082. struct scsi_device *sdev;
  4083. unsigned long flags;
  4084. /* Alas, we need to grab scan_mutex to ensure SCSI device
  4085. * state doesn't change underneath us and thus
  4086. * scsi_device_get() always succeeds. The mutex locking can
  4087. * be removed if there is __scsi_device_get() interface which
  4088. * increments reference counts regardless of device state.
  4089. */
  4090. mutex_lock(&ap->scsi_host->scan_mutex);
  4091. spin_lock_irqsave(ap->lock, flags);
  4092. /* clearing dev->sdev is protected by host lock */
  4093. sdev = dev->sdev;
  4094. dev->sdev = NULL;
  4095. if (sdev) {
  4096. /* If user initiated unplug races with us, sdev can go
  4097. * away underneath us after the host lock and
  4098. * scan_mutex are released. Hold onto it.
  4099. */
  4100. if (scsi_device_get(sdev) == 0) {
  4101. /* The following ensures the attached sdev is
  4102. * offline on return from ata_scsi_offline_dev()
  4103. * regardless it wins or loses the race
  4104. * against this function.
  4105. */
  4106. scsi_device_set_state(sdev, SDEV_OFFLINE);
  4107. } else {
  4108. WARN_ON(1);
  4109. sdev = NULL;
  4110. }
  4111. }
  4112. spin_unlock_irqrestore(ap->lock, flags);
  4113. mutex_unlock(&ap->scsi_host->scan_mutex);
  4114. if (sdev) {
  4115. ata_dev_info(dev, "detaching (SCSI %s)\n",
  4116. dev_name(&sdev->sdev_gendev));
  4117. scsi_remove_device(sdev);
  4118. scsi_device_put(sdev);
  4119. }
  4120. }
  4121. static void ata_scsi_handle_link_detach(struct ata_link *link)
  4122. {
  4123. struct ata_port *ap = link->ap;
  4124. struct ata_device *dev;
  4125. ata_for_each_dev(dev, link, ALL) {
  4126. unsigned long flags;
  4127. if (!(dev->flags & ATA_DFLAG_DETACHED))
  4128. continue;
  4129. spin_lock_irqsave(ap->lock, flags);
  4130. dev->flags &= ~ATA_DFLAG_DETACHED;
  4131. spin_unlock_irqrestore(ap->lock, flags);
  4132. if (zpodd_dev_enabled(dev))
  4133. zpodd_exit(dev);
  4134. ata_scsi_remove_dev(dev);
  4135. }
  4136. }
  4137. /**
  4138. * ata_scsi_media_change_notify - send media change event
  4139. * @dev: Pointer to the disk device with media change event
  4140. *
  4141. * Tell the block layer to send a media change notification
  4142. * event.
  4143. *
  4144. * LOCKING:
  4145. * spin_lock_irqsave(host lock)
  4146. */
  4147. void ata_scsi_media_change_notify(struct ata_device *dev)
  4148. {
  4149. if (dev->sdev)
  4150. sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
  4151. GFP_ATOMIC);
  4152. }
  4153. /**
  4154. * ata_scsi_hotplug - SCSI part of hotplug
  4155. * @work: Pointer to ATA port to perform SCSI hotplug on
  4156. *
  4157. * Perform SCSI part of hotplug. It's executed from a separate
  4158. * workqueue after EH completes. This is necessary because SCSI
  4159. * hot plugging requires working EH and hot unplugging is
  4160. * synchronized with hot plugging with a mutex.
  4161. *
  4162. * LOCKING:
  4163. * Kernel thread context (may sleep).
  4164. */
  4165. void ata_scsi_hotplug(struct work_struct *work)
  4166. {
  4167. struct ata_port *ap =
  4168. container_of(work, struct ata_port, hotplug_task.work);
  4169. int i;
  4170. if (ap->pflags & ATA_PFLAG_UNLOADING) {
  4171. DPRINTK("ENTER/EXIT - unloading\n");
  4172. return;
  4173. }
  4174. /*
  4175. * XXX - UGLY HACK
  4176. *
  4177. * The block layer suspend/resume path is fundamentally broken due
  4178. * to freezable kthreads and workqueue and may deadlock if a block
  4179. * device gets removed while resume is in progress. I don't know
  4180. * what the solution is short of removing freezable kthreads and
  4181. * workqueues altogether.
  4182. *
  4183. * The following is an ugly hack to avoid kicking off device
  4184. * removal while freezer is active. This is a joke but does avoid
  4185. * this particular deadlock scenario.
  4186. *
  4187. * https://bugzilla.kernel.org/show_bug.cgi?id=62801
  4188. * http://marc.info/?l=linux-kernel&m=138695698516487
  4189. */
  4190. #ifdef CONFIG_FREEZER
  4191. while (pm_freezing)
  4192. msleep(10);
  4193. #endif
  4194. DPRINTK("ENTER\n");
  4195. mutex_lock(&ap->scsi_scan_mutex);
  4196. /* Unplug detached devices. We cannot use link iterator here
  4197. * because PMP links have to be scanned even if PMP is
  4198. * currently not attached. Iterate manually.
  4199. */
  4200. ata_scsi_handle_link_detach(&ap->link);
  4201. if (ap->pmp_link)
  4202. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  4203. ata_scsi_handle_link_detach(&ap->pmp_link[i]);
  4204. /* scan for new ones */
  4205. ata_scsi_scan_host(ap, 0);
  4206. mutex_unlock(&ap->scsi_scan_mutex);
  4207. DPRINTK("EXIT\n");
  4208. }
  4209. /**
  4210. * ata_scsi_user_scan - indication for user-initiated bus scan
  4211. * @shost: SCSI host to scan
  4212. * @channel: Channel to scan
  4213. * @id: ID to scan
  4214. * @lun: LUN to scan
  4215. *
  4216. * This function is called when user explicitly requests bus
  4217. * scan. Set probe pending flag and invoke EH.
  4218. *
  4219. * LOCKING:
  4220. * SCSI layer (we don't care)
  4221. *
  4222. * RETURNS:
  4223. * Zero.
  4224. */
  4225. int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  4226. unsigned int id, u64 lun)
  4227. {
  4228. struct ata_port *ap = ata_shost_to_port(shost);
  4229. unsigned long flags;
  4230. int devno, rc = 0;
  4231. if (!ap->ops->error_handler)
  4232. return -EOPNOTSUPP;
  4233. if (lun != SCAN_WILD_CARD && lun)
  4234. return -EINVAL;
  4235. if (!sata_pmp_attached(ap)) {
  4236. if (channel != SCAN_WILD_CARD && channel)
  4237. return -EINVAL;
  4238. devno = id;
  4239. } else {
  4240. if (id != SCAN_WILD_CARD && id)
  4241. return -EINVAL;
  4242. devno = channel;
  4243. }
  4244. spin_lock_irqsave(ap->lock, flags);
  4245. if (devno == SCAN_WILD_CARD) {
  4246. struct ata_link *link;
  4247. ata_for_each_link(link, ap, EDGE) {
  4248. struct ata_eh_info *ehi = &link->eh_info;
  4249. ehi->probe_mask |= ATA_ALL_DEVICES;
  4250. ehi->action |= ATA_EH_RESET;
  4251. }
  4252. } else {
  4253. struct ata_device *dev = ata_find_dev(ap, devno);
  4254. if (dev) {
  4255. struct ata_eh_info *ehi = &dev->link->eh_info;
  4256. ehi->probe_mask |= 1 << dev->devno;
  4257. ehi->action |= ATA_EH_RESET;
  4258. } else
  4259. rc = -EINVAL;
  4260. }
  4261. if (rc == 0) {
  4262. ata_port_schedule_eh(ap);
  4263. spin_unlock_irqrestore(ap->lock, flags);
  4264. ata_port_wait_eh(ap);
  4265. } else
  4266. spin_unlock_irqrestore(ap->lock, flags);
  4267. return rc;
  4268. }
  4269. /**
  4270. * ata_scsi_dev_rescan - initiate scsi_rescan_device()
  4271. * @work: Pointer to ATA port to perform scsi_rescan_device()
  4272. *
  4273. * After ATA pass thru (SAT) commands are executed successfully,
  4274. * libata need to propagate the changes to SCSI layer.
  4275. *
  4276. * LOCKING:
  4277. * Kernel thread context (may sleep).
  4278. */
  4279. void ata_scsi_dev_rescan(struct work_struct *work)
  4280. {
  4281. struct ata_port *ap =
  4282. container_of(work, struct ata_port, scsi_rescan_task);
  4283. struct ata_link *link;
  4284. struct ata_device *dev;
  4285. unsigned long flags;
  4286. mutex_lock(&ap->scsi_scan_mutex);
  4287. spin_lock_irqsave(ap->lock, flags);
  4288. ata_for_each_link(link, ap, EDGE) {
  4289. ata_for_each_dev(dev, link, ENABLED) {
  4290. struct scsi_device *sdev = dev->sdev;
  4291. if (!sdev)
  4292. continue;
  4293. if (scsi_device_get(sdev))
  4294. continue;
  4295. spin_unlock_irqrestore(ap->lock, flags);
  4296. scsi_rescan_device(&(sdev->sdev_gendev));
  4297. scsi_device_put(sdev);
  4298. spin_lock_irqsave(ap->lock, flags);
  4299. }
  4300. }
  4301. spin_unlock_irqrestore(ap->lock, flags);
  4302. mutex_unlock(&ap->scsi_scan_mutex);
  4303. }
  4304. /**
  4305. * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
  4306. * @host: ATA host container for all SAS ports
  4307. * @port_info: Information from low-level host driver
  4308. * @shost: SCSI host that the scsi device is attached to
  4309. *
  4310. * LOCKING:
  4311. * PCI/etc. bus probe sem.
  4312. *
  4313. * RETURNS:
  4314. * ata_port pointer on success / NULL on failure.
  4315. */
  4316. struct ata_port *ata_sas_port_alloc(struct ata_host *host,
  4317. struct ata_port_info *port_info,
  4318. struct Scsi_Host *shost)
  4319. {
  4320. struct ata_port *ap;
  4321. ap = ata_port_alloc(host);
  4322. if (!ap)
  4323. return NULL;
  4324. ap->port_no = 0;
  4325. ap->lock = &host->lock;
  4326. ap->pio_mask = port_info->pio_mask;
  4327. ap->mwdma_mask = port_info->mwdma_mask;
  4328. ap->udma_mask = port_info->udma_mask;
  4329. ap->flags |= port_info->flags;
  4330. ap->ops = port_info->port_ops;
  4331. ap->cbl = ATA_CBL_SATA;
  4332. return ap;
  4333. }
  4334. EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  4335. /**
  4336. * ata_sas_port_start - Set port up for dma.
  4337. * @ap: Port to initialize
  4338. *
  4339. * Called just after data structures for each port are
  4340. * initialized.
  4341. *
  4342. * May be used as the port_start() entry in ata_port_operations.
  4343. *
  4344. * LOCKING:
  4345. * Inherited from caller.
  4346. */
  4347. int ata_sas_port_start(struct ata_port *ap)
  4348. {
  4349. /*
  4350. * the port is marked as frozen at allocation time, but if we don't
  4351. * have new eh, we won't thaw it
  4352. */
  4353. if (!ap->ops->error_handler)
  4354. ap->pflags &= ~ATA_PFLAG_FROZEN;
  4355. return 0;
  4356. }
  4357. EXPORT_SYMBOL_GPL(ata_sas_port_start);
  4358. /**
  4359. * ata_port_stop - Undo ata_sas_port_start()
  4360. * @ap: Port to shut down
  4361. *
  4362. * May be used as the port_stop() entry in ata_port_operations.
  4363. *
  4364. * LOCKING:
  4365. * Inherited from caller.
  4366. */
  4367. void ata_sas_port_stop(struct ata_port *ap)
  4368. {
  4369. }
  4370. EXPORT_SYMBOL_GPL(ata_sas_port_stop);
  4371. /**
  4372. * ata_sas_async_probe - simply schedule probing and return
  4373. * @ap: Port to probe
  4374. *
  4375. * For batch scheduling of probe for sas attached ata devices, assumes
  4376. * the port has already been through ata_sas_port_init()
  4377. */
  4378. void ata_sas_async_probe(struct ata_port *ap)
  4379. {
  4380. __ata_port_probe(ap);
  4381. }
  4382. EXPORT_SYMBOL_GPL(ata_sas_async_probe);
  4383. int ata_sas_sync_probe(struct ata_port *ap)
  4384. {
  4385. return ata_port_probe(ap);
  4386. }
  4387. EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
  4388. /**
  4389. * ata_sas_port_init - Initialize a SATA device
  4390. * @ap: SATA port to initialize
  4391. *
  4392. * LOCKING:
  4393. * PCI/etc. bus probe sem.
  4394. *
  4395. * RETURNS:
  4396. * Zero on success, non-zero on error.
  4397. */
  4398. int ata_sas_port_init(struct ata_port *ap)
  4399. {
  4400. int rc = ap->ops->port_start(ap);
  4401. if (rc)
  4402. return rc;
  4403. ap->print_id = atomic_inc_return(&ata_print_id);
  4404. return 0;
  4405. }
  4406. EXPORT_SYMBOL_GPL(ata_sas_port_init);
  4407. int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
  4408. {
  4409. return ata_tport_add(parent, ap);
  4410. }
  4411. EXPORT_SYMBOL_GPL(ata_sas_tport_add);
  4412. void ata_sas_tport_delete(struct ata_port *ap)
  4413. {
  4414. ata_tport_delete(ap);
  4415. }
  4416. EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
  4417. /**
  4418. * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
  4419. * @ap: SATA port to destroy
  4420. *
  4421. */
  4422. void ata_sas_port_destroy(struct ata_port *ap)
  4423. {
  4424. if (ap->ops->port_stop)
  4425. ap->ops->port_stop(ap);
  4426. kfree(ap);
  4427. }
  4428. EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
  4429. /**
  4430. * ata_sas_slave_configure - Default slave_config routine for libata devices
  4431. * @sdev: SCSI device to configure
  4432. * @ap: ATA port to which SCSI device is attached
  4433. *
  4434. * RETURNS:
  4435. * Zero.
  4436. */
  4437. int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
  4438. {
  4439. ata_scsi_sdev_config(sdev);
  4440. ata_scsi_dev_config(sdev, ap->link.device);
  4441. return 0;
  4442. }
  4443. EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
  4444. /**
  4445. * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
  4446. * @cmd: SCSI command to be sent
  4447. * @ap: ATA port to which the command is being sent
  4448. *
  4449. * RETURNS:
  4450. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  4451. * 0 otherwise.
  4452. */
  4453. int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
  4454. {
  4455. int rc = 0;
  4456. ata_scsi_dump_cdb(ap, cmd);
  4457. if (likely(ata_dev_enabled(ap->link.device)))
  4458. rc = __ata_scsi_queuecmd(cmd, ap->link.device);
  4459. else {
  4460. cmd->result = (DID_BAD_TARGET << 16);
  4461. cmd->scsi_done(cmd);
  4462. }
  4463. return rc;
  4464. }
  4465. EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
  4466. int ata_sas_allocate_tag(struct ata_port *ap)
  4467. {
  4468. unsigned int max_queue = ap->host->n_tags;
  4469. unsigned int i, tag;
  4470. for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
  4471. tag = tag < max_queue ? tag : 0;
  4472. /* the last tag is reserved for internal command. */
  4473. if (ata_tag_internal(tag))
  4474. continue;
  4475. if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
  4476. ap->sas_last_tag = tag;
  4477. return tag;
  4478. }
  4479. }
  4480. return -1;
  4481. }
  4482. void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
  4483. {
  4484. clear_bit(tag, &ap->sas_tag_allocated);
  4485. }