libata-scsi.c 104 KB

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