libata-scsi.c 104 KB

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