libata-scsi.c 107 KB

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