libata-scsi.c 104 KB

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