libata-scsi.c 105 KB

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