libata-scsi.c 96 KB

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