libata-scsi.c 107 KB

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