libata-scsi.c 119 KB

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