libata-scsi.c 127 KB

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