libata-scsi.c 97 KB

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