libata-scsi.c 97 KB

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