libata-scsi.c 99 KB

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