libata-scsi.c 124 KB

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