libata-scsi.c 127 KB

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