libata-scsi.c 98 KB

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