libata-scsi.c 116 KB

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