libata-scsi.c 104 KB

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