libata-scsi.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784
  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 "libata.h"
  50. #define SECTOR_SIZE 512
  51. #define ATA_SCSI_RBUF_SIZE 4096
  52. static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
  53. static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
  54. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
  55. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  56. const struct scsi_device *scsidev);
  57. static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
  58. const struct scsi_device *scsidev);
  59. static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  60. unsigned int id, unsigned int lun);
  61. #define RW_RECOVERY_MPAGE 0x1
  62. #define RW_RECOVERY_MPAGE_LEN 12
  63. #define CACHE_MPAGE 0x8
  64. #define CACHE_MPAGE_LEN 20
  65. #define CONTROL_MPAGE 0xa
  66. #define CONTROL_MPAGE_LEN 12
  67. #define ALL_MPAGES 0x3f
  68. #define ALL_SUB_MPAGES 0xff
  69. static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
  70. RW_RECOVERY_MPAGE,
  71. RW_RECOVERY_MPAGE_LEN - 2,
  72. (1 << 7), /* AWRE */
  73. 0, /* read retry count */
  74. 0, 0, 0, 0,
  75. 0, /* write retry count */
  76. 0, 0, 0
  77. };
  78. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  79. CACHE_MPAGE,
  80. CACHE_MPAGE_LEN - 2,
  81. 0, /* contains WCE, needs to be 0 for logic */
  82. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, /* contains DRA, needs to be 0 for logic */
  84. 0, 0, 0, 0, 0, 0, 0
  85. };
  86. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  87. CONTROL_MPAGE,
  88. CONTROL_MPAGE_LEN - 2,
  89. 2, /* DSENSE=0, GLTSD=1 */
  90. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  91. 0, 0, 0, 0, 0xff, 0xff,
  92. 0, 30 /* extended self test time, see 05-359r1 */
  93. };
  94. /*
  95. * libata transport template. libata doesn't do real transport stuff.
  96. * It just needs the eh_timed_out hook.
  97. */
  98. static struct scsi_transport_template ata_scsi_transport_template = {
  99. .eh_strategy_handler = ata_scsi_error,
  100. .eh_timed_out = ata_scsi_timed_out,
  101. .user_scan = ata_scsi_user_scan,
  102. };
  103. static const struct {
  104. enum link_pm value;
  105. const char *name;
  106. } link_pm_policy[] = {
  107. { NOT_AVAILABLE, "max_performance" },
  108. { MIN_POWER, "min_power" },
  109. { MAX_PERFORMANCE, "max_performance" },
  110. { MEDIUM_POWER, "medium_power" },
  111. };
  112. static const char *ata_scsi_lpm_get(enum link_pm policy)
  113. {
  114. int i;
  115. for (i = 0; i < ARRAY_SIZE(link_pm_policy); i++)
  116. if (link_pm_policy[i].value == policy)
  117. return link_pm_policy[i].name;
  118. return NULL;
  119. }
  120. static ssize_t ata_scsi_lpm_put(struct device *dev,
  121. struct device_attribute *attr,
  122. const char *buf, size_t count)
  123. {
  124. struct Scsi_Host *shost = class_to_shost(dev);
  125. struct ata_port *ap = ata_shost_to_port(shost);
  126. enum link_pm policy = 0;
  127. int i;
  128. /*
  129. * we are skipping array location 0 on purpose - this
  130. * is because a value of NOT_AVAILABLE is displayed
  131. * to the user as max_performance, but when the user
  132. * writes "max_performance", they actually want the
  133. * value to match MAX_PERFORMANCE.
  134. */
  135. for (i = 1; i < ARRAY_SIZE(link_pm_policy); i++) {
  136. const int len = strlen(link_pm_policy[i].name);
  137. if (strncmp(link_pm_policy[i].name, buf, len) == 0 &&
  138. buf[len] == '\n') {
  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. if (ata_id_is_ssd(dev->id))
  983. queue_flag_set_unlocked(QUEUE_FLAG_NONROT,
  984. sdev->request_queue);
  985. /* ATA devices must be sector aligned */
  986. blk_queue_update_dma_alignment(sdev->request_queue,
  987. ATA_SECT_SIZE - 1);
  988. sdev->manage_start_stop = 1;
  989. }
  990. if (dev->flags & ATA_DFLAG_AN)
  991. set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
  992. if (dev->flags & ATA_DFLAG_NCQ) {
  993. int depth;
  994. depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
  995. depth = min(ATA_MAX_QUEUE - 1, depth);
  996. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
  997. }
  998. return 0;
  999. }
  1000. /**
  1001. * ata_scsi_slave_config - Set SCSI device attributes
  1002. * @sdev: SCSI device to examine
  1003. *
  1004. * This is called before we actually start reading
  1005. * and writing to the device, to configure certain
  1006. * SCSI mid-layer behaviors.
  1007. *
  1008. * LOCKING:
  1009. * Defined by SCSI layer. We don't really care.
  1010. */
  1011. int ata_scsi_slave_config(struct scsi_device *sdev)
  1012. {
  1013. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1014. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  1015. int rc = 0;
  1016. ata_scsi_sdev_config(sdev);
  1017. if (dev)
  1018. rc = ata_scsi_dev_config(sdev, dev);
  1019. return rc;
  1020. }
  1021. /**
  1022. * ata_scsi_slave_destroy - SCSI device is about to be destroyed
  1023. * @sdev: SCSI device to be destroyed
  1024. *
  1025. * @sdev is about to be destroyed for hot/warm unplugging. If
  1026. * this unplugging was initiated by libata as indicated by NULL
  1027. * dev->sdev, this function doesn't have to do anything.
  1028. * Otherwise, SCSI layer initiated warm-unplug is in progress.
  1029. * Clear dev->sdev, schedule the device for ATA detach and invoke
  1030. * EH.
  1031. *
  1032. * LOCKING:
  1033. * Defined by SCSI layer. We don't really care.
  1034. */
  1035. void ata_scsi_slave_destroy(struct scsi_device *sdev)
  1036. {
  1037. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1038. struct request_queue *q = sdev->request_queue;
  1039. unsigned long flags;
  1040. struct ata_device *dev;
  1041. if (!ap->ops->error_handler)
  1042. return;
  1043. spin_lock_irqsave(ap->lock, flags);
  1044. dev = __ata_scsi_find_dev(ap, sdev);
  1045. if (dev && dev->sdev) {
  1046. /* SCSI device already in CANCEL state, no need to offline it */
  1047. dev->sdev = NULL;
  1048. dev->flags |= ATA_DFLAG_DETACH;
  1049. ata_port_schedule_eh(ap);
  1050. }
  1051. spin_unlock_irqrestore(ap->lock, flags);
  1052. kfree(q->dma_drain_buffer);
  1053. q->dma_drain_buffer = NULL;
  1054. q->dma_drain_size = 0;
  1055. }
  1056. /**
  1057. * ata_scsi_change_queue_depth - SCSI callback for queue depth config
  1058. * @sdev: SCSI device to configure queue depth for
  1059. * @queue_depth: new queue depth
  1060. *
  1061. * This is libata standard hostt->change_queue_depth callback.
  1062. * SCSI will call into this callback when user tries to set queue
  1063. * depth via sysfs.
  1064. *
  1065. * LOCKING:
  1066. * SCSI layer (we don't care)
  1067. *
  1068. * RETURNS:
  1069. * Newly configured queue depth.
  1070. */
  1071. int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
  1072. {
  1073. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1074. struct ata_device *dev;
  1075. unsigned long flags;
  1076. if (queue_depth < 1 || queue_depth == sdev->queue_depth)
  1077. return sdev->queue_depth;
  1078. dev = ata_scsi_find_dev(ap, sdev);
  1079. if (!dev || !ata_dev_enabled(dev))
  1080. return sdev->queue_depth;
  1081. /* NCQ enabled? */
  1082. spin_lock_irqsave(ap->lock, flags);
  1083. dev->flags &= ~ATA_DFLAG_NCQ_OFF;
  1084. if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
  1085. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1086. queue_depth = 1;
  1087. }
  1088. spin_unlock_irqrestore(ap->lock, flags);
  1089. /* limit and apply queue depth */
  1090. queue_depth = min(queue_depth, sdev->host->can_queue);
  1091. queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
  1092. queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
  1093. if (sdev->queue_depth == queue_depth)
  1094. return -EINVAL;
  1095. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
  1096. return queue_depth;
  1097. }
  1098. /* XXX: for spindown warning */
  1099. static void ata_delayed_done_timerfn(unsigned long arg)
  1100. {
  1101. struct scsi_cmnd *scmd = (void *)arg;
  1102. scmd->scsi_done(scmd);
  1103. }
  1104. /* XXX: for spindown warning */
  1105. static void ata_delayed_done(struct scsi_cmnd *scmd)
  1106. {
  1107. static struct timer_list timer;
  1108. setup_timer(&timer, ata_delayed_done_timerfn, (unsigned long)scmd);
  1109. mod_timer(&timer, jiffies + 5 * HZ);
  1110. }
  1111. /**
  1112. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  1113. * @qc: Storage for translated ATA taskfile
  1114. *
  1115. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  1116. * (to start). Perhaps these commands should be preceded by
  1117. * CHECK POWER MODE to see what power mode the device is already in.
  1118. * [See SAT revision 5 at www.t10.org]
  1119. *
  1120. * LOCKING:
  1121. * spin_lock_irqsave(host lock)
  1122. *
  1123. * RETURNS:
  1124. * Zero on success, non-zero on error.
  1125. */
  1126. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
  1127. {
  1128. struct scsi_cmnd *scmd = qc->scsicmd;
  1129. struct ata_taskfile *tf = &qc->tf;
  1130. const u8 *cdb = scmd->cmnd;
  1131. if (scmd->cmd_len < 5)
  1132. goto invalid_fld;
  1133. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1134. tf->protocol = ATA_PROT_NODATA;
  1135. if (cdb[1] & 0x1) {
  1136. ; /* ignore IMMED bit, violates sat-r05 */
  1137. }
  1138. if (cdb[4] & 0x2)
  1139. goto invalid_fld; /* LOEJ bit set not supported */
  1140. if (((cdb[4] >> 4) & 0xf) != 0)
  1141. goto invalid_fld; /* power conditions not supported */
  1142. if (cdb[4] & 0x1) {
  1143. tf->nsect = 1; /* 1 sector, lba=0 */
  1144. if (qc->dev->flags & ATA_DFLAG_LBA) {
  1145. tf->flags |= ATA_TFLAG_LBA;
  1146. tf->lbah = 0x0;
  1147. tf->lbam = 0x0;
  1148. tf->lbal = 0x0;
  1149. tf->device |= ATA_LBA;
  1150. } else {
  1151. /* CHS */
  1152. tf->lbal = 0x1; /* sect */
  1153. tf->lbam = 0x0; /* cyl low */
  1154. tf->lbah = 0x0; /* cyl high */
  1155. }
  1156. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  1157. } else {
  1158. /* Some odd clown BIOSen issue spindown on power off (ACPI S4
  1159. * or S5) causing some drives to spin up and down again.
  1160. */
  1161. if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
  1162. system_state == SYSTEM_POWER_OFF)
  1163. goto skip;
  1164. if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
  1165. system_entering_hibernation())
  1166. goto skip;
  1167. /* XXX: This is for backward compatibility, will be
  1168. * removed. Read Documentation/feature-removal-schedule.txt
  1169. * for more info.
  1170. */
  1171. if ((qc->dev->flags & ATA_DFLAG_SPUNDOWN) &&
  1172. (system_state == SYSTEM_HALT ||
  1173. system_state == SYSTEM_POWER_OFF)) {
  1174. static unsigned long warned;
  1175. if (!test_and_set_bit(0, &warned)) {
  1176. ata_dev_printk(qc->dev, KERN_WARNING,
  1177. "DISK MIGHT NOT BE SPUN DOWN PROPERLY. "
  1178. "UPDATE SHUTDOWN UTILITY\n");
  1179. ata_dev_printk(qc->dev, KERN_WARNING,
  1180. "For more info, visit "
  1181. "http://linux-ata.org/shutdown.html\n");
  1182. /* ->scsi_done is not used, use it for
  1183. * delayed completion.
  1184. */
  1185. scmd->scsi_done = qc->scsidone;
  1186. qc->scsidone = ata_delayed_done;
  1187. }
  1188. goto skip;
  1189. }
  1190. /* Issue ATA STANDBY IMMEDIATE command */
  1191. tf->command = ATA_CMD_STANDBYNOW1;
  1192. }
  1193. /*
  1194. * Standby and Idle condition timers could be implemented but that
  1195. * would require libata to implement the Power condition mode page
  1196. * and allow the user to change it. Changing mode pages requires
  1197. * MODE SELECT to be implemented.
  1198. */
  1199. return 0;
  1200. invalid_fld:
  1201. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1202. /* "Invalid field in cbd" */
  1203. return 1;
  1204. skip:
  1205. scmd->result = SAM_STAT_GOOD;
  1206. return 1;
  1207. }
  1208. /**
  1209. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  1210. * @qc: Storage for translated ATA taskfile
  1211. *
  1212. * Sets up an ATA taskfile to issue FLUSH CACHE or
  1213. * FLUSH CACHE EXT.
  1214. *
  1215. * LOCKING:
  1216. * spin_lock_irqsave(host lock)
  1217. *
  1218. * RETURNS:
  1219. * Zero on success, non-zero on error.
  1220. */
  1221. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
  1222. {
  1223. struct ata_taskfile *tf = &qc->tf;
  1224. tf->flags |= ATA_TFLAG_DEVICE;
  1225. tf->protocol = ATA_PROT_NODATA;
  1226. if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
  1227. tf->command = ATA_CMD_FLUSH_EXT;
  1228. else
  1229. tf->command = ATA_CMD_FLUSH;
  1230. /* flush is critical for IO integrity, consider it an IO command */
  1231. qc->flags |= ATA_QCFLAG_IO;
  1232. return 0;
  1233. }
  1234. /**
  1235. * scsi_6_lba_len - Get LBA and transfer length
  1236. * @cdb: SCSI command to translate
  1237. *
  1238. * Calculate LBA and transfer length for 6-byte commands.
  1239. *
  1240. * RETURNS:
  1241. * @plba: the LBA
  1242. * @plen: the transfer length
  1243. */
  1244. static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1245. {
  1246. u64 lba = 0;
  1247. u32 len;
  1248. VPRINTK("six-byte command\n");
  1249. lba |= ((u64)(cdb[1] & 0x1f)) << 16;
  1250. lba |= ((u64)cdb[2]) << 8;
  1251. lba |= ((u64)cdb[3]);
  1252. len = cdb[4];
  1253. *plba = lba;
  1254. *plen = len;
  1255. }
  1256. /**
  1257. * scsi_10_lba_len - Get LBA and transfer length
  1258. * @cdb: SCSI command to translate
  1259. *
  1260. * Calculate LBA and transfer length for 10-byte commands.
  1261. *
  1262. * RETURNS:
  1263. * @plba: the LBA
  1264. * @plen: the transfer length
  1265. */
  1266. static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1267. {
  1268. u64 lba = 0;
  1269. u32 len = 0;
  1270. VPRINTK("ten-byte command\n");
  1271. lba |= ((u64)cdb[2]) << 24;
  1272. lba |= ((u64)cdb[3]) << 16;
  1273. lba |= ((u64)cdb[4]) << 8;
  1274. lba |= ((u64)cdb[5]);
  1275. len |= ((u32)cdb[7]) << 8;
  1276. len |= ((u32)cdb[8]);
  1277. *plba = lba;
  1278. *plen = len;
  1279. }
  1280. /**
  1281. * scsi_16_lba_len - Get LBA and transfer length
  1282. * @cdb: SCSI command to translate
  1283. *
  1284. * Calculate LBA and transfer length for 16-byte commands.
  1285. *
  1286. * RETURNS:
  1287. * @plba: the LBA
  1288. * @plen: the transfer length
  1289. */
  1290. static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1291. {
  1292. u64 lba = 0;
  1293. u32 len = 0;
  1294. VPRINTK("sixteen-byte command\n");
  1295. lba |= ((u64)cdb[2]) << 56;
  1296. lba |= ((u64)cdb[3]) << 48;
  1297. lba |= ((u64)cdb[4]) << 40;
  1298. lba |= ((u64)cdb[5]) << 32;
  1299. lba |= ((u64)cdb[6]) << 24;
  1300. lba |= ((u64)cdb[7]) << 16;
  1301. lba |= ((u64)cdb[8]) << 8;
  1302. lba |= ((u64)cdb[9]);
  1303. len |= ((u32)cdb[10]) << 24;
  1304. len |= ((u32)cdb[11]) << 16;
  1305. len |= ((u32)cdb[12]) << 8;
  1306. len |= ((u32)cdb[13]);
  1307. *plba = lba;
  1308. *plen = len;
  1309. }
  1310. /**
  1311. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  1312. * @qc: Storage for translated ATA taskfile
  1313. *
  1314. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  1315. *
  1316. * LOCKING:
  1317. * spin_lock_irqsave(host lock)
  1318. *
  1319. * RETURNS:
  1320. * Zero on success, non-zero on error.
  1321. */
  1322. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
  1323. {
  1324. struct scsi_cmnd *scmd = qc->scsicmd;
  1325. struct ata_taskfile *tf = &qc->tf;
  1326. struct ata_device *dev = qc->dev;
  1327. u64 dev_sectors = qc->dev->n_sectors;
  1328. const u8 *cdb = scmd->cmnd;
  1329. u64 block;
  1330. u32 n_block;
  1331. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1332. tf->protocol = ATA_PROT_NODATA;
  1333. if (cdb[0] == VERIFY) {
  1334. if (scmd->cmd_len < 10)
  1335. goto invalid_fld;
  1336. scsi_10_lba_len(cdb, &block, &n_block);
  1337. } else if (cdb[0] == VERIFY_16) {
  1338. if (scmd->cmd_len < 16)
  1339. goto invalid_fld;
  1340. scsi_16_lba_len(cdb, &block, &n_block);
  1341. } else
  1342. goto invalid_fld;
  1343. if (!n_block)
  1344. goto nothing_to_do;
  1345. if (block >= dev_sectors)
  1346. goto out_of_range;
  1347. if ((block + n_block) > dev_sectors)
  1348. goto out_of_range;
  1349. if (dev->flags & ATA_DFLAG_LBA) {
  1350. tf->flags |= ATA_TFLAG_LBA;
  1351. if (lba_28_ok(block, n_block)) {
  1352. /* use LBA28 */
  1353. tf->command = ATA_CMD_VERIFY;
  1354. tf->device |= (block >> 24) & 0xf;
  1355. } else if (lba_48_ok(block, n_block)) {
  1356. if (!(dev->flags & ATA_DFLAG_LBA48))
  1357. goto out_of_range;
  1358. /* use LBA48 */
  1359. tf->flags |= ATA_TFLAG_LBA48;
  1360. tf->command = ATA_CMD_VERIFY_EXT;
  1361. tf->hob_nsect = (n_block >> 8) & 0xff;
  1362. tf->hob_lbah = (block >> 40) & 0xff;
  1363. tf->hob_lbam = (block >> 32) & 0xff;
  1364. tf->hob_lbal = (block >> 24) & 0xff;
  1365. } else
  1366. /* request too large even for LBA48 */
  1367. goto out_of_range;
  1368. tf->nsect = n_block & 0xff;
  1369. tf->lbah = (block >> 16) & 0xff;
  1370. tf->lbam = (block >> 8) & 0xff;
  1371. tf->lbal = block & 0xff;
  1372. tf->device |= ATA_LBA;
  1373. } else {
  1374. /* CHS */
  1375. u32 sect, head, cyl, track;
  1376. if (!lba_28_ok(block, n_block))
  1377. goto out_of_range;
  1378. /* Convert LBA to CHS */
  1379. track = (u32)block / dev->sectors;
  1380. cyl = track / dev->heads;
  1381. head = track % dev->heads;
  1382. sect = (u32)block % dev->sectors + 1;
  1383. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1384. (u32)block, track, cyl, head, sect);
  1385. /* Check whether the converted CHS can fit.
  1386. Cylinder: 0-65535
  1387. Head: 0-15
  1388. Sector: 1-255*/
  1389. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1390. goto out_of_range;
  1391. tf->command = ATA_CMD_VERIFY;
  1392. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1393. tf->lbal = sect;
  1394. tf->lbam = cyl;
  1395. tf->lbah = cyl >> 8;
  1396. tf->device |= head;
  1397. }
  1398. return 0;
  1399. invalid_fld:
  1400. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1401. /* "Invalid field in cbd" */
  1402. return 1;
  1403. out_of_range:
  1404. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1405. /* "Logical Block Address out of range" */
  1406. return 1;
  1407. nothing_to_do:
  1408. scmd->result = SAM_STAT_GOOD;
  1409. return 1;
  1410. }
  1411. /**
  1412. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  1413. * @qc: Storage for translated ATA taskfile
  1414. *
  1415. * Converts any of six SCSI read/write commands into the
  1416. * ATA counterpart, including starting sector (LBA),
  1417. * sector count, and taking into account the device's LBA48
  1418. * support.
  1419. *
  1420. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  1421. * %WRITE_16 are currently supported.
  1422. *
  1423. * LOCKING:
  1424. * spin_lock_irqsave(host lock)
  1425. *
  1426. * RETURNS:
  1427. * Zero on success, non-zero on error.
  1428. */
  1429. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
  1430. {
  1431. struct scsi_cmnd *scmd = qc->scsicmd;
  1432. const u8 *cdb = scmd->cmnd;
  1433. unsigned int tf_flags = 0;
  1434. u64 block;
  1435. u32 n_block;
  1436. int rc;
  1437. if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
  1438. tf_flags |= ATA_TFLAG_WRITE;
  1439. /* Calculate the SCSI LBA, transfer length and FUA. */
  1440. switch (cdb[0]) {
  1441. case READ_10:
  1442. case WRITE_10:
  1443. if (unlikely(scmd->cmd_len < 10))
  1444. goto invalid_fld;
  1445. scsi_10_lba_len(cdb, &block, &n_block);
  1446. if (unlikely(cdb[1] & (1 << 3)))
  1447. tf_flags |= ATA_TFLAG_FUA;
  1448. break;
  1449. case READ_6:
  1450. case WRITE_6:
  1451. if (unlikely(scmd->cmd_len < 6))
  1452. goto invalid_fld;
  1453. scsi_6_lba_len(cdb, &block, &n_block);
  1454. /* for 6-byte r/w commands, transfer length 0
  1455. * means 256 blocks of data, not 0 block.
  1456. */
  1457. if (!n_block)
  1458. n_block = 256;
  1459. break;
  1460. case READ_16:
  1461. case WRITE_16:
  1462. if (unlikely(scmd->cmd_len < 16))
  1463. goto invalid_fld;
  1464. scsi_16_lba_len(cdb, &block, &n_block);
  1465. if (unlikely(cdb[1] & (1 << 3)))
  1466. tf_flags |= ATA_TFLAG_FUA;
  1467. break;
  1468. default:
  1469. DPRINTK("no-byte command\n");
  1470. goto invalid_fld;
  1471. }
  1472. /* Check and compose ATA command */
  1473. if (!n_block)
  1474. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  1475. * length 0 means transfer 0 block of data.
  1476. * However, for ATA R/W commands, sector count 0 means
  1477. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  1478. *
  1479. * WARNING: one or two older ATA drives treat 0 as 0...
  1480. */
  1481. goto nothing_to_do;
  1482. qc->flags |= ATA_QCFLAG_IO;
  1483. qc->nbytes = n_block * ATA_SECT_SIZE;
  1484. rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
  1485. qc->tag);
  1486. if (likely(rc == 0))
  1487. return 0;
  1488. if (rc == -ERANGE)
  1489. goto out_of_range;
  1490. /* treat all other errors as -EINVAL, fall through */
  1491. invalid_fld:
  1492. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1493. /* "Invalid field in cbd" */
  1494. return 1;
  1495. out_of_range:
  1496. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1497. /* "Logical Block Address out of range" */
  1498. return 1;
  1499. nothing_to_do:
  1500. scmd->result = SAM_STAT_GOOD;
  1501. return 1;
  1502. }
  1503. static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  1504. {
  1505. struct ata_port *ap = qc->ap;
  1506. struct scsi_cmnd *cmd = qc->scsicmd;
  1507. u8 *cdb = cmd->cmnd;
  1508. int need_sense = (qc->err_mask != 0);
  1509. /* For ATA pass thru (SAT) commands, generate a sense block if
  1510. * user mandated it or if there's an error. Note that if we
  1511. * generate because the user forced us to, a check condition
  1512. * is generated and the ATA register values are returned
  1513. * whether the command completed successfully or not. If there
  1514. * was no error, SK, ASC and ASCQ will all be zero.
  1515. */
  1516. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1517. ((cdb[2] & 0x20) || need_sense)) {
  1518. ata_gen_passthru_sense(qc);
  1519. } else {
  1520. if (!need_sense) {
  1521. cmd->result = SAM_STAT_GOOD;
  1522. } else {
  1523. /* TODO: decide which descriptor format to use
  1524. * for 48b LBA devices and call that here
  1525. * instead of the fixed desc, which is only
  1526. * good for smaller LBA (and maybe CHS?)
  1527. * devices.
  1528. */
  1529. ata_gen_ata_sense(qc);
  1530. }
  1531. }
  1532. /* XXX: track spindown state for spindown skipping and warning */
  1533. if (unlikely(qc->tf.command == ATA_CMD_STANDBY ||
  1534. qc->tf.command == ATA_CMD_STANDBYNOW1))
  1535. qc->dev->flags |= ATA_DFLAG_SPUNDOWN;
  1536. else if (likely(system_state != SYSTEM_HALT &&
  1537. system_state != SYSTEM_POWER_OFF))
  1538. qc->dev->flags &= ~ATA_DFLAG_SPUNDOWN;
  1539. if (need_sense && !ap->ops->error_handler)
  1540. ata_dump_status(ap->print_id, &qc->result_tf);
  1541. qc->scsidone(cmd);
  1542. ata_qc_free(qc);
  1543. }
  1544. /**
  1545. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1546. * @dev: ATA device to which the command is addressed
  1547. * @cmd: SCSI command to execute
  1548. * @done: SCSI command completion function
  1549. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1550. *
  1551. * Our ->queuecommand() function has decided that the SCSI
  1552. * command issued can be directly translated into an ATA
  1553. * command, rather than handled internally.
  1554. *
  1555. * This function sets up an ata_queued_cmd structure for the
  1556. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1557. *
  1558. * The xlat_func argument (actor) returns 0 if ready to execute
  1559. * ATA command, else 1 to finish translation. If 1 is returned
  1560. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1561. * to be set reflecting an error condition or clean (early)
  1562. * termination.
  1563. *
  1564. * LOCKING:
  1565. * spin_lock_irqsave(host lock)
  1566. *
  1567. * RETURNS:
  1568. * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
  1569. * needs to be deferred.
  1570. */
  1571. static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
  1572. void (*done)(struct scsi_cmnd *),
  1573. ata_xlat_func_t xlat_func)
  1574. {
  1575. struct ata_port *ap = dev->link->ap;
  1576. struct ata_queued_cmd *qc;
  1577. int rc;
  1578. VPRINTK("ENTER\n");
  1579. qc = ata_scsi_qc_new(dev, cmd, done);
  1580. if (!qc)
  1581. goto err_mem;
  1582. /* data is present; dma-map it */
  1583. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1584. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1585. if (unlikely(scsi_bufflen(cmd) < 1)) {
  1586. ata_dev_printk(dev, KERN_WARNING,
  1587. "WARNING: zero len r/w req\n");
  1588. goto err_did;
  1589. }
  1590. ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
  1591. qc->dma_dir = cmd->sc_data_direction;
  1592. }
  1593. qc->complete_fn = ata_scsi_qc_complete;
  1594. if (xlat_func(qc))
  1595. goto early_finish;
  1596. if (ap->ops->qc_defer) {
  1597. if ((rc = ap->ops->qc_defer(qc)))
  1598. goto defer;
  1599. }
  1600. /* select device, send command to hardware */
  1601. ata_qc_issue(qc);
  1602. VPRINTK("EXIT\n");
  1603. return 0;
  1604. early_finish:
  1605. ata_qc_free(qc);
  1606. qc->scsidone(cmd);
  1607. DPRINTK("EXIT - early finish (good or error)\n");
  1608. return 0;
  1609. err_did:
  1610. ata_qc_free(qc);
  1611. cmd->result = (DID_ERROR << 16);
  1612. qc->scsidone(cmd);
  1613. err_mem:
  1614. DPRINTK("EXIT - internal\n");
  1615. return 0;
  1616. defer:
  1617. ata_qc_free(qc);
  1618. DPRINTK("EXIT - defer\n");
  1619. if (rc == ATA_DEFER_LINK)
  1620. return SCSI_MLQUEUE_DEVICE_BUSY;
  1621. else
  1622. return SCSI_MLQUEUE_HOST_BUSY;
  1623. }
  1624. /**
  1625. * ata_scsi_rbuf_get - Map response buffer.
  1626. * @cmd: SCSI command containing buffer to be mapped.
  1627. * @flags: unsigned long variable to store irq enable status
  1628. * @copy_in: copy in from user buffer
  1629. *
  1630. * Prepare buffer for simulated SCSI commands.
  1631. *
  1632. * LOCKING:
  1633. * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
  1634. *
  1635. * RETURNS:
  1636. * Pointer to response buffer.
  1637. */
  1638. static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
  1639. unsigned long *flags)
  1640. {
  1641. spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
  1642. memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
  1643. if (copy_in)
  1644. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1645. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1646. return ata_scsi_rbuf;
  1647. }
  1648. /**
  1649. * ata_scsi_rbuf_put - Unmap response buffer.
  1650. * @cmd: SCSI command containing buffer to be unmapped.
  1651. * @copy_out: copy out result
  1652. * @flags: @flags passed to ata_scsi_rbuf_get()
  1653. *
  1654. * Returns rbuf buffer. The result is copied to @cmd's buffer if
  1655. * @copy_back is true.
  1656. *
  1657. * LOCKING:
  1658. * Unlocks ata_scsi_rbuf_lock.
  1659. */
  1660. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
  1661. unsigned long *flags)
  1662. {
  1663. if (copy_out)
  1664. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1665. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1666. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
  1667. }
  1668. /**
  1669. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1670. * @args: device IDENTIFY data / SCSI command of interest.
  1671. * @actor: Callback hook for desired SCSI command simulator
  1672. *
  1673. * Takes care of the hard work of simulating a SCSI command...
  1674. * Mapping the response buffer, calling the command's handler,
  1675. * and handling the handler's return value. This return value
  1676. * indicates whether the handler wishes the SCSI command to be
  1677. * completed successfully (0), or not (in which case cmd->result
  1678. * and sense buffer are assumed to be set).
  1679. *
  1680. * LOCKING:
  1681. * spin_lock_irqsave(host lock)
  1682. */
  1683. static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1684. unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
  1685. {
  1686. u8 *rbuf;
  1687. unsigned int rc;
  1688. struct scsi_cmnd *cmd = args->cmd;
  1689. unsigned long flags;
  1690. rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
  1691. rc = actor(args, rbuf);
  1692. ata_scsi_rbuf_put(cmd, rc == 0, &flags);
  1693. if (rc == 0)
  1694. cmd->result = SAM_STAT_GOOD;
  1695. args->done(cmd);
  1696. }
  1697. /**
  1698. * ata_scsiop_inq_std - Simulate INQUIRY command
  1699. * @args: device IDENTIFY data / SCSI command of interest.
  1700. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1701. *
  1702. * Returns standard device identification data associated
  1703. * with non-VPD INQUIRY command output.
  1704. *
  1705. * LOCKING:
  1706. * spin_lock_irqsave(host lock)
  1707. */
  1708. static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
  1709. {
  1710. const u8 versions[] = {
  1711. 0x60, /* SAM-3 (no version claimed) */
  1712. 0x03,
  1713. 0x20, /* SBC-2 (no version claimed) */
  1714. 0x02,
  1715. 0x60 /* SPC-3 (no version claimed) */
  1716. };
  1717. u8 hdr[] = {
  1718. TYPE_DISK,
  1719. 0,
  1720. 0x5, /* claim SPC-3 version compatibility */
  1721. 2,
  1722. 95 - 4
  1723. };
  1724. VPRINTK("ENTER\n");
  1725. /* set scsi removeable (RMB) bit per ata bit */
  1726. if (ata_id_removeable(args->id))
  1727. hdr[1] |= (1 << 7);
  1728. memcpy(rbuf, hdr, sizeof(hdr));
  1729. memcpy(&rbuf[8], "ATA ", 8);
  1730. ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
  1731. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1732. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1733. memcpy(&rbuf[32], "n/a ", 4);
  1734. memcpy(rbuf + 59, versions, sizeof(versions));
  1735. return 0;
  1736. }
  1737. /**
  1738. * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  1739. * @args: device IDENTIFY data / SCSI command of interest.
  1740. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1741. *
  1742. * Returns list of inquiry VPD pages available.
  1743. *
  1744. * LOCKING:
  1745. * spin_lock_irqsave(host lock)
  1746. */
  1747. static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
  1748. {
  1749. const u8 pages[] = {
  1750. 0x00, /* page 0x00, this page */
  1751. 0x80, /* page 0x80, unit serial no page */
  1752. 0x83, /* page 0x83, device ident page */
  1753. 0x89, /* page 0x89, ata info page */
  1754. 0xb1, /* page 0xb1, block device characteristics page */
  1755. };
  1756. rbuf[3] = sizeof(pages); /* number of supported VPD pages */
  1757. memcpy(rbuf + 4, pages, sizeof(pages));
  1758. return 0;
  1759. }
  1760. /**
  1761. * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  1762. * @args: device IDENTIFY data / SCSI command of interest.
  1763. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1764. *
  1765. * Returns ATA device serial number.
  1766. *
  1767. * LOCKING:
  1768. * spin_lock_irqsave(host lock)
  1769. */
  1770. static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
  1771. {
  1772. const u8 hdr[] = {
  1773. 0,
  1774. 0x80, /* this page code */
  1775. 0,
  1776. ATA_ID_SERNO_LEN, /* page len */
  1777. };
  1778. memcpy(rbuf, hdr, sizeof(hdr));
  1779. ata_id_string(args->id, (unsigned char *) &rbuf[4],
  1780. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1781. return 0;
  1782. }
  1783. /**
  1784. * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  1785. * @args: device IDENTIFY data / SCSI command of interest.
  1786. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1787. *
  1788. * Yields two logical unit device identification designators:
  1789. * - vendor specific ASCII containing the ATA serial number
  1790. * - SAT defined "t10 vendor id based" containing ASCII vendor
  1791. * name ("ATA "), model and serial numbers.
  1792. *
  1793. * LOCKING:
  1794. * spin_lock_irqsave(host lock)
  1795. */
  1796. static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
  1797. {
  1798. const int sat_model_serial_desc_len = 68;
  1799. int num;
  1800. rbuf[1] = 0x83; /* this page code */
  1801. num = 4;
  1802. /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
  1803. rbuf[num + 0] = 2;
  1804. rbuf[num + 3] = ATA_ID_SERNO_LEN;
  1805. num += 4;
  1806. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1807. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1808. num += ATA_ID_SERNO_LEN;
  1809. /* SAT defined lu model and serial numbers descriptor */
  1810. /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
  1811. rbuf[num + 0] = 2;
  1812. rbuf[num + 1] = 1;
  1813. rbuf[num + 3] = sat_model_serial_desc_len;
  1814. num += 4;
  1815. memcpy(rbuf + num, "ATA ", 8);
  1816. num += 8;
  1817. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
  1818. ATA_ID_PROD_LEN);
  1819. num += ATA_ID_PROD_LEN;
  1820. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
  1821. ATA_ID_SERNO_LEN);
  1822. num += ATA_ID_SERNO_LEN;
  1823. rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
  1824. return 0;
  1825. }
  1826. /**
  1827. * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
  1828. * @args: device IDENTIFY data / SCSI command of interest.
  1829. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1830. *
  1831. * Yields SAT-specified ATA VPD page.
  1832. *
  1833. * LOCKING:
  1834. * spin_lock_irqsave(host lock)
  1835. */
  1836. static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
  1837. {
  1838. struct ata_taskfile tf;
  1839. memset(&tf, 0, sizeof(tf));
  1840. rbuf[1] = 0x89; /* our page code */
  1841. rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
  1842. rbuf[3] = (0x238 & 0xff);
  1843. memcpy(&rbuf[8], "linux ", 8);
  1844. memcpy(&rbuf[16], "libata ", 16);
  1845. memcpy(&rbuf[32], DRV_VERSION, 4);
  1846. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1847. /* we don't store the ATA device signature, so we fake it */
  1848. tf.command = ATA_DRDY; /* really, this is Status reg */
  1849. tf.lbal = 0x1;
  1850. tf.nsect = 0x1;
  1851. ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
  1852. rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
  1853. rbuf[56] = ATA_CMD_ID_ATA;
  1854. memcpy(&rbuf[60], &args->id[0], 512);
  1855. return 0;
  1856. }
  1857. static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
  1858. {
  1859. int form_factor = ata_id_form_factor(args->id);
  1860. int media_rotation_rate = ata_id_rotation_rate(args->id);
  1861. rbuf[1] = 0xb1;
  1862. rbuf[3] = 0x3c;
  1863. rbuf[4] = media_rotation_rate >> 8;
  1864. rbuf[5] = media_rotation_rate;
  1865. rbuf[7] = form_factor;
  1866. return 0;
  1867. }
  1868. /**
  1869. * ata_scsiop_noop - Command handler that simply returns success.
  1870. * @args: device IDENTIFY data / SCSI command of interest.
  1871. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1872. *
  1873. * No operation. Simply returns success to caller, to indicate
  1874. * that the caller should successfully complete this SCSI command.
  1875. *
  1876. * LOCKING:
  1877. * spin_lock_irqsave(host lock)
  1878. */
  1879. static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
  1880. {
  1881. VPRINTK("ENTER\n");
  1882. return 0;
  1883. }
  1884. /**
  1885. * ata_msense_caching - Simulate MODE SENSE caching info page
  1886. * @id: device IDENTIFY data
  1887. * @buf: output buffer
  1888. *
  1889. * Generate a caching info page, which conditionally indicates
  1890. * write caching to the SCSI layer, depending on device
  1891. * capabilities.
  1892. *
  1893. * LOCKING:
  1894. * None.
  1895. */
  1896. static unsigned int ata_msense_caching(u16 *id, u8 *buf)
  1897. {
  1898. memcpy(buf, def_cache_mpage, sizeof(def_cache_mpage));
  1899. if (ata_id_wcache_enabled(id))
  1900. buf[2] |= (1 << 2); /* write cache enable */
  1901. if (!ata_id_rahead_enabled(id))
  1902. buf[12] |= (1 << 5); /* disable read ahead */
  1903. return sizeof(def_cache_mpage);
  1904. }
  1905. /**
  1906. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1907. * @buf: output buffer
  1908. *
  1909. * Generate a generic MODE SENSE control mode page.
  1910. *
  1911. * LOCKING:
  1912. * None.
  1913. */
  1914. static unsigned int ata_msense_ctl_mode(u8 *buf)
  1915. {
  1916. memcpy(buf, def_control_mpage, sizeof(def_control_mpage));
  1917. return sizeof(def_control_mpage);
  1918. }
  1919. /**
  1920. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1921. * @buf: output buffer
  1922. *
  1923. * Generate a generic MODE SENSE r/w error recovery page.
  1924. *
  1925. * LOCKING:
  1926. * None.
  1927. */
  1928. static unsigned int ata_msense_rw_recovery(u8 *buf)
  1929. {
  1930. memcpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage));
  1931. return sizeof(def_rw_recovery_mpage);
  1932. }
  1933. /*
  1934. * We can turn this into a real blacklist if it's needed, for now just
  1935. * blacklist any Maxtor BANC1G10 revision firmware
  1936. */
  1937. static int ata_dev_supports_fua(u16 *id)
  1938. {
  1939. unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
  1940. if (!libata_fua)
  1941. return 0;
  1942. if (!ata_id_has_fua(id))
  1943. return 0;
  1944. ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  1945. ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
  1946. if (strcmp(model, "Maxtor"))
  1947. return 1;
  1948. if (strcmp(fw, "BANC1G10"))
  1949. return 1;
  1950. return 0; /* blacklisted */
  1951. }
  1952. /**
  1953. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1954. * @args: device IDENTIFY data / SCSI command of interest.
  1955. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1956. *
  1957. * Simulate MODE SENSE commands. Assume this is invoked for direct
  1958. * access devices (e.g. disks) only. There should be no block
  1959. * descriptor for other device types.
  1960. *
  1961. * LOCKING:
  1962. * spin_lock_irqsave(host lock)
  1963. */
  1964. static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
  1965. {
  1966. struct ata_device *dev = args->dev;
  1967. u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
  1968. const u8 sat_blk_desc[] = {
  1969. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  1970. 0,
  1971. 0, 0x2, 0x0 /* block length: 512 bytes */
  1972. };
  1973. u8 pg, spg;
  1974. unsigned int ebd, page_control, six_byte;
  1975. u8 dpofua;
  1976. VPRINTK("ENTER\n");
  1977. six_byte = (scsicmd[0] == MODE_SENSE);
  1978. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  1979. /*
  1980. * LLBA bit in msense(10) ignored (compliant)
  1981. */
  1982. page_control = scsicmd[2] >> 6;
  1983. switch (page_control) {
  1984. case 0: /* current */
  1985. break; /* supported */
  1986. case 3: /* saved */
  1987. goto saving_not_supp;
  1988. case 1: /* changeable */
  1989. case 2: /* defaults */
  1990. default:
  1991. goto invalid_fld;
  1992. }
  1993. if (six_byte)
  1994. p += 4 + (ebd ? 8 : 0);
  1995. else
  1996. p += 8 + (ebd ? 8 : 0);
  1997. pg = scsicmd[2] & 0x3f;
  1998. spg = scsicmd[3];
  1999. /*
  2000. * No mode subpages supported (yet) but asking for _all_
  2001. * subpages may be valid
  2002. */
  2003. if (spg && (spg != ALL_SUB_MPAGES))
  2004. goto invalid_fld;
  2005. switch(pg) {
  2006. case RW_RECOVERY_MPAGE:
  2007. p += ata_msense_rw_recovery(p);
  2008. break;
  2009. case CACHE_MPAGE:
  2010. p += ata_msense_caching(args->id, p);
  2011. break;
  2012. case CONTROL_MPAGE:
  2013. p += ata_msense_ctl_mode(p);
  2014. break;
  2015. case ALL_MPAGES:
  2016. p += ata_msense_rw_recovery(p);
  2017. p += ata_msense_caching(args->id, p);
  2018. p += ata_msense_ctl_mode(p);
  2019. break;
  2020. default: /* invalid page code */
  2021. goto invalid_fld;
  2022. }
  2023. dpofua = 0;
  2024. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  2025. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  2026. dpofua = 1 << 4;
  2027. if (six_byte) {
  2028. rbuf[0] = p - rbuf - 1;
  2029. rbuf[2] |= dpofua;
  2030. if (ebd) {
  2031. rbuf[3] = sizeof(sat_blk_desc);
  2032. memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
  2033. }
  2034. } else {
  2035. unsigned int output_len = p - rbuf - 2;
  2036. rbuf[0] = output_len >> 8;
  2037. rbuf[1] = output_len;
  2038. rbuf[3] |= dpofua;
  2039. if (ebd) {
  2040. rbuf[7] = sizeof(sat_blk_desc);
  2041. memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
  2042. }
  2043. }
  2044. return 0;
  2045. invalid_fld:
  2046. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2047. /* "Invalid field in cbd" */
  2048. return 1;
  2049. saving_not_supp:
  2050. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  2051. /* "Saving parameters not supported" */
  2052. return 1;
  2053. }
  2054. /**
  2055. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  2056. * @args: device IDENTIFY data / SCSI command of interest.
  2057. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2058. *
  2059. * Simulate READ CAPACITY commands.
  2060. *
  2061. * LOCKING:
  2062. * None.
  2063. */
  2064. static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
  2065. {
  2066. struct ata_device *dev = args->dev;
  2067. u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
  2068. u8 log_per_phys = 0;
  2069. u16 lowest_aligned = 0;
  2070. u16 word_106 = dev->id[106];
  2071. u16 word_209 = dev->id[209];
  2072. if ((word_106 & 0xc000) == 0x4000) {
  2073. /* Number and offset of logical sectors per physical sector */
  2074. if (word_106 & (1 << 13))
  2075. log_per_phys = word_106 & 0xf;
  2076. if ((word_209 & 0xc000) == 0x4000) {
  2077. u16 first = dev->id[209] & 0x3fff;
  2078. if (first > 0)
  2079. lowest_aligned = (1 << log_per_phys) - first;
  2080. }
  2081. }
  2082. VPRINTK("ENTER\n");
  2083. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  2084. if (last_lba >= 0xffffffffULL)
  2085. last_lba = 0xffffffff;
  2086. /* sector count, 32-bit */
  2087. rbuf[0] = last_lba >> (8 * 3);
  2088. rbuf[1] = last_lba >> (8 * 2);
  2089. rbuf[2] = last_lba >> (8 * 1);
  2090. rbuf[3] = last_lba;
  2091. /* sector size */
  2092. rbuf[6] = ATA_SECT_SIZE >> 8;
  2093. rbuf[7] = ATA_SECT_SIZE & 0xff;
  2094. } else {
  2095. /* sector count, 64-bit */
  2096. rbuf[0] = last_lba >> (8 * 7);
  2097. rbuf[1] = last_lba >> (8 * 6);
  2098. rbuf[2] = last_lba >> (8 * 5);
  2099. rbuf[3] = last_lba >> (8 * 4);
  2100. rbuf[4] = last_lba >> (8 * 3);
  2101. rbuf[5] = last_lba >> (8 * 2);
  2102. rbuf[6] = last_lba >> (8 * 1);
  2103. rbuf[7] = last_lba;
  2104. /* sector size */
  2105. rbuf[10] = ATA_SECT_SIZE >> 8;
  2106. rbuf[11] = ATA_SECT_SIZE & 0xff;
  2107. rbuf[12] = 0;
  2108. rbuf[13] = log_per_phys;
  2109. rbuf[14] = (lowest_aligned >> 8) & 0x3f;
  2110. rbuf[15] = lowest_aligned;
  2111. }
  2112. return 0;
  2113. }
  2114. /**
  2115. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  2116. * @args: device IDENTIFY data / SCSI command of interest.
  2117. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2118. *
  2119. * Simulate REPORT LUNS command.
  2120. *
  2121. * LOCKING:
  2122. * spin_lock_irqsave(host lock)
  2123. */
  2124. static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
  2125. {
  2126. VPRINTK("ENTER\n");
  2127. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  2128. return 0;
  2129. }
  2130. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  2131. {
  2132. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  2133. /* FIXME: not quite right; we don't want the
  2134. * translation of taskfile registers into
  2135. * a sense descriptors, since that's only
  2136. * correct for ATA, not ATAPI
  2137. */
  2138. ata_gen_passthru_sense(qc);
  2139. }
  2140. qc->scsidone(qc->scsicmd);
  2141. ata_qc_free(qc);
  2142. }
  2143. /* is it pointless to prefer PIO for "safety reasons"? */
  2144. static inline int ata_pio_use_silly(struct ata_port *ap)
  2145. {
  2146. return (ap->flags & ATA_FLAG_PIO_DMA);
  2147. }
  2148. static void atapi_request_sense(struct ata_queued_cmd *qc)
  2149. {
  2150. struct ata_port *ap = qc->ap;
  2151. struct scsi_cmnd *cmd = qc->scsicmd;
  2152. DPRINTK("ATAPI request sense\n");
  2153. /* FIXME: is this needed? */
  2154. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  2155. #ifdef CONFIG_ATA_SFF
  2156. if (ap->ops->sff_tf_read)
  2157. ap->ops->sff_tf_read(ap, &qc->tf);
  2158. #endif
  2159. /* fill these in, for the case where they are -not- overwritten */
  2160. cmd->sense_buffer[0] = 0x70;
  2161. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  2162. ata_qc_reinit(qc);
  2163. /* setup sg table and init transfer direction */
  2164. sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
  2165. ata_sg_init(qc, &qc->sgent, 1);
  2166. qc->dma_dir = DMA_FROM_DEVICE;
  2167. memset(&qc->cdb, 0, qc->dev->cdb_len);
  2168. qc->cdb[0] = REQUEST_SENSE;
  2169. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2170. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2171. qc->tf.command = ATA_CMD_PACKET;
  2172. if (ata_pio_use_silly(ap)) {
  2173. qc->tf.protocol = ATAPI_PROT_DMA;
  2174. qc->tf.feature |= ATAPI_PKT_DMA;
  2175. } else {
  2176. qc->tf.protocol = ATAPI_PROT_PIO;
  2177. qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
  2178. qc->tf.lbah = 0;
  2179. }
  2180. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  2181. qc->complete_fn = atapi_sense_complete;
  2182. ata_qc_issue(qc);
  2183. DPRINTK("EXIT\n");
  2184. }
  2185. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  2186. {
  2187. struct scsi_cmnd *cmd = qc->scsicmd;
  2188. unsigned int err_mask = qc->err_mask;
  2189. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  2190. /* handle completion from new EH */
  2191. if (unlikely(qc->ap->ops->error_handler &&
  2192. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  2193. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  2194. /* FIXME: not quite right; we don't want the
  2195. * translation of taskfile registers into a
  2196. * sense descriptors, since that's only
  2197. * correct for ATA, not ATAPI
  2198. */
  2199. ata_gen_passthru_sense(qc);
  2200. }
  2201. /* SCSI EH automatically locks door if sdev->locked is
  2202. * set. Sometimes door lock request continues to
  2203. * fail, for example, when no media is present. This
  2204. * creates a loop - SCSI EH issues door lock which
  2205. * fails and gets invoked again to acquire sense data
  2206. * for the failed command.
  2207. *
  2208. * If door lock fails, always clear sdev->locked to
  2209. * avoid this infinite loop.
  2210. */
  2211. if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
  2212. qc->dev->sdev->locked = 0;
  2213. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  2214. qc->scsidone(cmd);
  2215. ata_qc_free(qc);
  2216. return;
  2217. }
  2218. /* successful completion or old EH failure path */
  2219. if (unlikely(err_mask & AC_ERR_DEV)) {
  2220. cmd->result = SAM_STAT_CHECK_CONDITION;
  2221. atapi_request_sense(qc);
  2222. return;
  2223. } else if (unlikely(err_mask)) {
  2224. /* FIXME: not quite right; we don't want the
  2225. * translation of taskfile registers into
  2226. * a sense descriptors, since that's only
  2227. * correct for ATA, not ATAPI
  2228. */
  2229. ata_gen_passthru_sense(qc);
  2230. } else {
  2231. u8 *scsicmd = cmd->cmnd;
  2232. if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
  2233. unsigned long flags;
  2234. u8 *buf;
  2235. buf = ata_scsi_rbuf_get(cmd, true, &flags);
  2236. /* ATAPI devices typically report zero for their SCSI version,
  2237. * and sometimes deviate from the spec WRT response data
  2238. * format. If SCSI version is reported as zero like normal,
  2239. * then we make the following fixups: 1) Fake MMC-5 version,
  2240. * to indicate to the Linux scsi midlayer this is a modern
  2241. * device. 2) Ensure response data format / ATAPI information
  2242. * are always correct.
  2243. */
  2244. if (buf[2] == 0) {
  2245. buf[2] = 0x5;
  2246. buf[3] = 0x32;
  2247. }
  2248. ata_scsi_rbuf_put(cmd, true, &flags);
  2249. }
  2250. cmd->result = SAM_STAT_GOOD;
  2251. }
  2252. qc->scsidone(cmd);
  2253. ata_qc_free(qc);
  2254. }
  2255. /**
  2256. * atapi_xlat - Initialize PACKET taskfile
  2257. * @qc: command structure to be initialized
  2258. *
  2259. * LOCKING:
  2260. * spin_lock_irqsave(host lock)
  2261. *
  2262. * RETURNS:
  2263. * Zero on success, non-zero on failure.
  2264. */
  2265. static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
  2266. {
  2267. struct scsi_cmnd *scmd = qc->scsicmd;
  2268. struct ata_device *dev = qc->dev;
  2269. int nodata = (scmd->sc_data_direction == DMA_NONE);
  2270. int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
  2271. unsigned int nbytes;
  2272. memset(qc->cdb, 0, dev->cdb_len);
  2273. memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
  2274. qc->complete_fn = atapi_qc_complete;
  2275. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2276. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2277. qc->tf.flags |= ATA_TFLAG_WRITE;
  2278. DPRINTK("direction: write\n");
  2279. }
  2280. qc->tf.command = ATA_CMD_PACKET;
  2281. ata_qc_set_pc_nbytes(qc);
  2282. /* check whether ATAPI DMA is safe */
  2283. if (!nodata && !using_pio && atapi_check_dma(qc))
  2284. using_pio = 1;
  2285. /* Some controller variants snoop this value for Packet
  2286. * transfers to do state machine and FIFO management. Thus we
  2287. * want to set it properly, and for DMA where it is
  2288. * effectively meaningless.
  2289. */
  2290. nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
  2291. /* Most ATAPI devices which honor transfer chunk size don't
  2292. * behave according to the spec when odd chunk size which
  2293. * matches the transfer length is specified. If the number of
  2294. * bytes to transfer is 2n+1. According to the spec, what
  2295. * should happen is to indicate that 2n+1 is going to be
  2296. * transferred and transfer 2n+2 bytes where the last byte is
  2297. * padding.
  2298. *
  2299. * In practice, this doesn't happen. ATAPI devices first
  2300. * indicate and transfer 2n bytes and then indicate and
  2301. * transfer 2 bytes where the last byte is padding.
  2302. *
  2303. * This inconsistency confuses several controllers which
  2304. * perform PIO using DMA such as Intel AHCIs and sil3124/32.
  2305. * These controllers use actual number of transferred bytes to
  2306. * update DMA poitner and transfer of 4n+2 bytes make those
  2307. * controller push DMA pointer by 4n+4 bytes because SATA data
  2308. * FISes are aligned to 4 bytes. This causes data corruption
  2309. * and buffer overrun.
  2310. *
  2311. * Always setting nbytes to even number solves this problem
  2312. * because then ATAPI devices don't have to split data at 2n
  2313. * boundaries.
  2314. */
  2315. if (nbytes & 0x1)
  2316. nbytes++;
  2317. qc->tf.lbam = (nbytes & 0xFF);
  2318. qc->tf.lbah = (nbytes >> 8);
  2319. if (nodata)
  2320. qc->tf.protocol = ATAPI_PROT_NODATA;
  2321. else if (using_pio)
  2322. qc->tf.protocol = ATAPI_PROT_PIO;
  2323. else {
  2324. /* DMA data xfer */
  2325. qc->tf.protocol = ATAPI_PROT_DMA;
  2326. qc->tf.feature |= ATAPI_PKT_DMA;
  2327. if ((dev->flags & ATA_DFLAG_DMADIR) &&
  2328. (scmd->sc_data_direction != DMA_TO_DEVICE))
  2329. /* some SATA bridges need us to indicate data xfer direction */
  2330. qc->tf.feature |= ATAPI_DMADIR;
  2331. }
  2332. /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
  2333. as ATAPI tape drives don't get this right otherwise */
  2334. return 0;
  2335. }
  2336. static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
  2337. {
  2338. if (!sata_pmp_attached(ap)) {
  2339. if (likely(devno < ata_link_max_devices(&ap->link)))
  2340. return &ap->link.device[devno];
  2341. } else {
  2342. if (likely(devno < ap->nr_pmp_links))
  2343. return &ap->pmp_link[devno].device[0];
  2344. }
  2345. return NULL;
  2346. }
  2347. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  2348. const struct scsi_device *scsidev)
  2349. {
  2350. int devno;
  2351. /* skip commands not addressed to targets we simulate */
  2352. if (!sata_pmp_attached(ap)) {
  2353. if (unlikely(scsidev->channel || scsidev->lun))
  2354. return NULL;
  2355. devno = scsidev->id;
  2356. } else {
  2357. if (unlikely(scsidev->id || scsidev->lun))
  2358. return NULL;
  2359. devno = scsidev->channel;
  2360. }
  2361. return ata_find_dev(ap, devno);
  2362. }
  2363. /**
  2364. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2365. * @ap: ATA port to which the device is attached
  2366. * @scsidev: SCSI device from which we derive the ATA device
  2367. *
  2368. * Given various information provided in struct scsi_cmnd,
  2369. * map that onto an ATA bus, and using that mapping
  2370. * determine which ata_device is associated with the
  2371. * SCSI command to be sent.
  2372. *
  2373. * LOCKING:
  2374. * spin_lock_irqsave(host lock)
  2375. *
  2376. * RETURNS:
  2377. * Associated ATA device, or %NULL if not found.
  2378. */
  2379. static struct ata_device *
  2380. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2381. {
  2382. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2383. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2384. return NULL;
  2385. return dev;
  2386. }
  2387. /*
  2388. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2389. * @byte1: Byte 1 from pass-thru CDB.
  2390. *
  2391. * RETURNS:
  2392. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2393. */
  2394. static u8
  2395. ata_scsi_map_proto(u8 byte1)
  2396. {
  2397. switch((byte1 & 0x1e) >> 1) {
  2398. case 3: /* Non-data */
  2399. return ATA_PROT_NODATA;
  2400. case 6: /* DMA */
  2401. case 10: /* UDMA Data-in */
  2402. case 11: /* UDMA Data-Out */
  2403. return ATA_PROT_DMA;
  2404. case 4: /* PIO Data-in */
  2405. case 5: /* PIO Data-out */
  2406. return ATA_PROT_PIO;
  2407. case 0: /* Hard Reset */
  2408. case 1: /* SRST */
  2409. case 8: /* Device Diagnostic */
  2410. case 9: /* Device Reset */
  2411. case 7: /* DMA Queued */
  2412. case 12: /* FPDMA */
  2413. case 15: /* Return Response Info */
  2414. default: /* Reserved */
  2415. break;
  2416. }
  2417. return ATA_PROT_UNKNOWN;
  2418. }
  2419. /**
  2420. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2421. * @qc: command structure to be initialized
  2422. *
  2423. * Handles either 12 or 16-byte versions of the CDB.
  2424. *
  2425. * RETURNS:
  2426. * Zero on success, non-zero on failure.
  2427. */
  2428. static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
  2429. {
  2430. struct ata_taskfile *tf = &(qc->tf);
  2431. struct scsi_cmnd *scmd = qc->scsicmd;
  2432. struct ata_device *dev = qc->dev;
  2433. const u8 *cdb = scmd->cmnd;
  2434. if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
  2435. goto invalid_fld;
  2436. /*
  2437. * Filter TPM commands by default. These provide an
  2438. * essentially uncontrolled encrypted "back door" between
  2439. * applications and the disk. Set libata.allow_tpm=1 if you
  2440. * have a real reason for wanting to use them. This ensures
  2441. * that installed software cannot easily mess stuff up without
  2442. * user intent. DVR type users will probably ship with this enabled
  2443. * for movie content management.
  2444. *
  2445. * Note that for ATA8 we can issue a DCS change and DCS freeze lock
  2446. * for this and should do in future but that it is not sufficient as
  2447. * DCS is an optional feature set. Thus we also do the software filter
  2448. * so that we comply with the TC consortium stated goal that the user
  2449. * can turn off TC features of their system.
  2450. */
  2451. if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
  2452. goto invalid_fld;
  2453. /* We may not issue DMA commands if no DMA mode is set */
  2454. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
  2455. goto invalid_fld;
  2456. /*
  2457. * 12 and 16 byte CDBs use different offsets to
  2458. * provide the various register values.
  2459. */
  2460. if (cdb[0] == ATA_16) {
  2461. /*
  2462. * 16-byte CDB - may contain extended commands.
  2463. *
  2464. * If that is the case, copy the upper byte register values.
  2465. */
  2466. if (cdb[1] & 0x01) {
  2467. tf->hob_feature = cdb[3];
  2468. tf->hob_nsect = cdb[5];
  2469. tf->hob_lbal = cdb[7];
  2470. tf->hob_lbam = cdb[9];
  2471. tf->hob_lbah = cdb[11];
  2472. tf->flags |= ATA_TFLAG_LBA48;
  2473. } else
  2474. tf->flags &= ~ATA_TFLAG_LBA48;
  2475. /*
  2476. * Always copy low byte, device and command registers.
  2477. */
  2478. tf->feature = cdb[4];
  2479. tf->nsect = cdb[6];
  2480. tf->lbal = cdb[8];
  2481. tf->lbam = cdb[10];
  2482. tf->lbah = cdb[12];
  2483. tf->device = cdb[13];
  2484. tf->command = cdb[14];
  2485. } else {
  2486. /*
  2487. * 12-byte CDB - incapable of extended commands.
  2488. */
  2489. tf->flags &= ~ATA_TFLAG_LBA48;
  2490. tf->feature = cdb[3];
  2491. tf->nsect = cdb[4];
  2492. tf->lbal = cdb[5];
  2493. tf->lbam = cdb[6];
  2494. tf->lbah = cdb[7];
  2495. tf->device = cdb[8];
  2496. tf->command = cdb[9];
  2497. }
  2498. /* enforce correct master/slave bit */
  2499. tf->device = dev->devno ?
  2500. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2501. /* sanity check for pio multi commands */
  2502. if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
  2503. goto invalid_fld;
  2504. if (is_multi_taskfile(tf)) {
  2505. unsigned int multi_count = 1 << (cdb[1] >> 5);
  2506. /* compare the passed through multi_count
  2507. * with the cached multi_count of libata
  2508. */
  2509. if (multi_count != dev->multi_count)
  2510. ata_dev_printk(dev, KERN_WARNING,
  2511. "invalid multi_count %u ignored\n",
  2512. multi_count);
  2513. }
  2514. /* READ/WRITE LONG use a non-standard sect_size */
  2515. qc->sect_size = ATA_SECT_SIZE;
  2516. switch (tf->command) {
  2517. case ATA_CMD_READ_LONG:
  2518. case ATA_CMD_READ_LONG_ONCE:
  2519. case ATA_CMD_WRITE_LONG:
  2520. case ATA_CMD_WRITE_LONG_ONCE:
  2521. if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
  2522. goto invalid_fld;
  2523. qc->sect_size = scsi_bufflen(scmd);
  2524. }
  2525. /*
  2526. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2527. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2528. * by an update to hardware-specific registers for each
  2529. * controller (i.e. the reason for ->set_piomode(),
  2530. * ->set_dmamode(), and ->post_set_mode() hooks).
  2531. */
  2532. if ((tf->command == ATA_CMD_SET_FEATURES)
  2533. && (tf->feature == SETFEATURES_XFER))
  2534. goto invalid_fld;
  2535. /*
  2536. * Set flags so that all registers will be written,
  2537. * and pass on write indication (used for PIO/DMA
  2538. * setup.)
  2539. */
  2540. tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
  2541. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2542. tf->flags |= ATA_TFLAG_WRITE;
  2543. /*
  2544. * Set transfer length.
  2545. *
  2546. * TODO: find out if we need to do more here to
  2547. * cover scatter/gather case.
  2548. */
  2549. ata_qc_set_pc_nbytes(qc);
  2550. /* request result TF and be quiet about device error */
  2551. qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
  2552. return 0;
  2553. invalid_fld:
  2554. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2555. /* "Invalid field in cdb" */
  2556. return 1;
  2557. }
  2558. /**
  2559. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  2560. * @dev: ATA device
  2561. * @cmd: SCSI command opcode to consider
  2562. *
  2563. * Look up the SCSI command given, and determine whether the
  2564. * SCSI command is to be translated or simulated.
  2565. *
  2566. * RETURNS:
  2567. * Pointer to translation function if possible, %NULL if not.
  2568. */
  2569. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  2570. {
  2571. switch (cmd) {
  2572. case READ_6:
  2573. case READ_10:
  2574. case READ_16:
  2575. case WRITE_6:
  2576. case WRITE_10:
  2577. case WRITE_16:
  2578. return ata_scsi_rw_xlat;
  2579. case SYNCHRONIZE_CACHE:
  2580. if (ata_try_flush_cache(dev))
  2581. return ata_scsi_flush_xlat;
  2582. break;
  2583. case VERIFY:
  2584. case VERIFY_16:
  2585. return ata_scsi_verify_xlat;
  2586. case ATA_12:
  2587. case ATA_16:
  2588. return ata_scsi_pass_thru;
  2589. case START_STOP:
  2590. return ata_scsi_start_stop_xlat;
  2591. }
  2592. return NULL;
  2593. }
  2594. /**
  2595. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2596. * @ap: ATA port to which the command was being sent
  2597. * @cmd: SCSI command to dump
  2598. *
  2599. * Prints the contents of a SCSI command via printk().
  2600. */
  2601. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2602. struct scsi_cmnd *cmd)
  2603. {
  2604. #ifdef ATA_DEBUG
  2605. struct scsi_device *scsidev = cmd->device;
  2606. u8 *scsicmd = cmd->cmnd;
  2607. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2608. ap->print_id,
  2609. scsidev->channel, scsidev->id, scsidev->lun,
  2610. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2611. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2612. scsicmd[8]);
  2613. #endif
  2614. }
  2615. static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
  2616. void (*done)(struct scsi_cmnd *),
  2617. struct ata_device *dev)
  2618. {
  2619. u8 scsi_op = scmd->cmnd[0];
  2620. ata_xlat_func_t xlat_func;
  2621. int rc = 0;
  2622. if (dev->class == ATA_DEV_ATA) {
  2623. if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
  2624. goto bad_cdb_len;
  2625. xlat_func = ata_get_xlat_func(dev, scsi_op);
  2626. } else {
  2627. if (unlikely(!scmd->cmd_len))
  2628. goto bad_cdb_len;
  2629. xlat_func = NULL;
  2630. if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
  2631. /* relay SCSI command to ATAPI device */
  2632. int len = COMMAND_SIZE(scsi_op);
  2633. if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
  2634. goto bad_cdb_len;
  2635. xlat_func = atapi_xlat;
  2636. } else {
  2637. /* ATA_16 passthru, treat as an ATA command */
  2638. if (unlikely(scmd->cmd_len > 16))
  2639. goto bad_cdb_len;
  2640. xlat_func = ata_get_xlat_func(dev, scsi_op);
  2641. }
  2642. }
  2643. if (xlat_func)
  2644. rc = ata_scsi_translate(dev, scmd, done, xlat_func);
  2645. else
  2646. ata_scsi_simulate(dev, scmd, done);
  2647. return rc;
  2648. bad_cdb_len:
  2649. DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
  2650. scmd->cmd_len, scsi_op, dev->cdb_len);
  2651. scmd->result = DID_ERROR << 16;
  2652. done(scmd);
  2653. return 0;
  2654. }
  2655. /**
  2656. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  2657. * @cmd: SCSI command to be sent
  2658. * @done: Completion function, called when command is complete
  2659. *
  2660. * In some cases, this function translates SCSI commands into
  2661. * ATA taskfiles, and queues the taskfiles to be sent to
  2662. * hardware. In other cases, this function simulates a
  2663. * SCSI device by evaluating and responding to certain
  2664. * SCSI commands. This creates the overall effect of
  2665. * ATA and ATAPI devices appearing as SCSI devices.
  2666. *
  2667. * LOCKING:
  2668. * Releases scsi-layer-held lock, and obtains host lock.
  2669. *
  2670. * RETURNS:
  2671. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  2672. * 0 otherwise.
  2673. */
  2674. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  2675. {
  2676. struct ata_port *ap;
  2677. struct ata_device *dev;
  2678. struct scsi_device *scsidev = cmd->device;
  2679. struct Scsi_Host *shost = scsidev->host;
  2680. int rc = 0;
  2681. ap = ata_shost_to_port(shost);
  2682. spin_unlock(shost->host_lock);
  2683. spin_lock(ap->lock);
  2684. ata_scsi_dump_cdb(ap, cmd);
  2685. dev = ata_scsi_find_dev(ap, scsidev);
  2686. if (likely(dev))
  2687. rc = __ata_scsi_queuecmd(cmd, done, dev);
  2688. else {
  2689. cmd->result = (DID_BAD_TARGET << 16);
  2690. done(cmd);
  2691. }
  2692. spin_unlock(ap->lock);
  2693. spin_lock(shost->host_lock);
  2694. return rc;
  2695. }
  2696. /**
  2697. * ata_scsi_simulate - simulate SCSI command on ATA device
  2698. * @dev: the target device
  2699. * @cmd: SCSI command being sent to device.
  2700. * @done: SCSI command completion function.
  2701. *
  2702. * Interprets and directly executes a select list of SCSI commands
  2703. * that can be handled internally.
  2704. *
  2705. * LOCKING:
  2706. * spin_lock_irqsave(host lock)
  2707. */
  2708. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
  2709. void (*done)(struct scsi_cmnd *))
  2710. {
  2711. struct ata_scsi_args args;
  2712. const u8 *scsicmd = cmd->cmnd;
  2713. u8 tmp8;
  2714. args.dev = dev;
  2715. args.id = dev->id;
  2716. args.cmd = cmd;
  2717. args.done = done;
  2718. switch(scsicmd[0]) {
  2719. /* TODO: worth improving? */
  2720. case FORMAT_UNIT:
  2721. ata_scsi_invalid_field(cmd, done);
  2722. break;
  2723. case INQUIRY:
  2724. if (scsicmd[1] & 2) /* is CmdDt set? */
  2725. ata_scsi_invalid_field(cmd, done);
  2726. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  2727. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  2728. else switch (scsicmd[2]) {
  2729. case 0x00:
  2730. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  2731. break;
  2732. case 0x80:
  2733. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  2734. break;
  2735. case 0x83:
  2736. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  2737. break;
  2738. case 0x89:
  2739. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
  2740. break;
  2741. case 0xb1:
  2742. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
  2743. break;
  2744. default:
  2745. ata_scsi_invalid_field(cmd, done);
  2746. break;
  2747. }
  2748. break;
  2749. case MODE_SENSE:
  2750. case MODE_SENSE_10:
  2751. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  2752. break;
  2753. case MODE_SELECT: /* unconditionally return */
  2754. case MODE_SELECT_10: /* bad-field-in-cdb */
  2755. ata_scsi_invalid_field(cmd, done);
  2756. break;
  2757. case READ_CAPACITY:
  2758. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2759. break;
  2760. case SERVICE_ACTION_IN:
  2761. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  2762. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2763. else
  2764. ata_scsi_invalid_field(cmd, done);
  2765. break;
  2766. case REPORT_LUNS:
  2767. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  2768. break;
  2769. case REQUEST_SENSE:
  2770. ata_scsi_set_sense(cmd, 0, 0, 0);
  2771. cmd->result = (DRIVER_SENSE << 24);
  2772. done(cmd);
  2773. break;
  2774. /* if we reach this, then writeback caching is disabled,
  2775. * turning this into a no-op.
  2776. */
  2777. case SYNCHRONIZE_CACHE:
  2778. /* fall through */
  2779. /* no-op's, complete with success */
  2780. case REZERO_UNIT:
  2781. case SEEK_6:
  2782. case SEEK_10:
  2783. case TEST_UNIT_READY:
  2784. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2785. break;
  2786. case SEND_DIAGNOSTIC:
  2787. tmp8 = scsicmd[1] & ~(1 << 3);
  2788. if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
  2789. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2790. else
  2791. ata_scsi_invalid_field(cmd, done);
  2792. break;
  2793. /* all other commands */
  2794. default:
  2795. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  2796. /* "Invalid command operation code" */
  2797. done(cmd);
  2798. break;
  2799. }
  2800. }
  2801. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
  2802. {
  2803. int i, rc;
  2804. for (i = 0; i < host->n_ports; i++) {
  2805. struct ata_port *ap = host->ports[i];
  2806. struct Scsi_Host *shost;
  2807. rc = -ENOMEM;
  2808. shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
  2809. if (!shost)
  2810. goto err_alloc;
  2811. *(struct ata_port **)&shost->hostdata[0] = ap;
  2812. ap->scsi_host = shost;
  2813. shost->transportt = &ata_scsi_transport_template;
  2814. shost->unique_id = ap->print_id;
  2815. shost->max_id = 16;
  2816. shost->max_lun = 1;
  2817. shost->max_channel = 1;
  2818. shost->max_cmd_len = 16;
  2819. /* Schedule policy is determined by ->qc_defer()
  2820. * callback and it needs to see every deferred qc.
  2821. * Set host_blocked to 1 to prevent SCSI midlayer from
  2822. * automatically deferring requests.
  2823. */
  2824. shost->max_host_blocked = 1;
  2825. rc = scsi_add_host(ap->scsi_host, ap->host->dev);
  2826. if (rc)
  2827. goto err_add;
  2828. }
  2829. return 0;
  2830. err_add:
  2831. scsi_host_put(host->ports[i]->scsi_host);
  2832. err_alloc:
  2833. while (--i >= 0) {
  2834. struct Scsi_Host *shost = host->ports[i]->scsi_host;
  2835. scsi_remove_host(shost);
  2836. scsi_host_put(shost);
  2837. }
  2838. return rc;
  2839. }
  2840. void ata_scsi_scan_host(struct ata_port *ap, int sync)
  2841. {
  2842. int tries = 5;
  2843. struct ata_device *last_failed_dev = NULL;
  2844. struct ata_link *link;
  2845. struct ata_device *dev;
  2846. if (ap->flags & ATA_FLAG_DISABLED)
  2847. return;
  2848. repeat:
  2849. ata_for_each_link(link, ap, EDGE) {
  2850. ata_for_each_dev(dev, link, ENABLED) {
  2851. struct scsi_device *sdev;
  2852. int channel = 0, id = 0;
  2853. if (dev->sdev)
  2854. continue;
  2855. if (ata_is_host_link(link))
  2856. id = dev->devno;
  2857. else
  2858. channel = link->pmp;
  2859. sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
  2860. NULL);
  2861. if (!IS_ERR(sdev)) {
  2862. dev->sdev = sdev;
  2863. scsi_device_put(sdev);
  2864. }
  2865. }
  2866. }
  2867. /* If we scanned while EH was in progress or allocation
  2868. * failure occurred, scan would have failed silently. Check
  2869. * whether all devices are attached.
  2870. */
  2871. ata_for_each_link(link, ap, EDGE) {
  2872. ata_for_each_dev(dev, link, ENABLED) {
  2873. if (!dev->sdev)
  2874. goto exit_loop;
  2875. }
  2876. }
  2877. exit_loop:
  2878. if (!link)
  2879. return;
  2880. /* we're missing some SCSI devices */
  2881. if (sync) {
  2882. /* If caller requested synchrnous scan && we've made
  2883. * any progress, sleep briefly and repeat.
  2884. */
  2885. if (dev != last_failed_dev) {
  2886. msleep(100);
  2887. last_failed_dev = dev;
  2888. goto repeat;
  2889. }
  2890. /* We might be failing to detect boot device, give it
  2891. * a few more chances.
  2892. */
  2893. if (--tries) {
  2894. msleep(100);
  2895. goto repeat;
  2896. }
  2897. ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "
  2898. "failed without making any progress,\n"
  2899. " switching to async\n");
  2900. }
  2901. queue_delayed_work(ata_aux_wq, &ap->hotplug_task,
  2902. round_jiffies_relative(HZ));
  2903. }
  2904. /**
  2905. * ata_scsi_offline_dev - offline attached SCSI device
  2906. * @dev: ATA device to offline attached SCSI device for
  2907. *
  2908. * This function is called from ata_eh_hotplug() and responsible
  2909. * for taking the SCSI device attached to @dev offline. This
  2910. * function is called with host lock which protects dev->sdev
  2911. * against clearing.
  2912. *
  2913. * LOCKING:
  2914. * spin_lock_irqsave(host lock)
  2915. *
  2916. * RETURNS:
  2917. * 1 if attached SCSI device exists, 0 otherwise.
  2918. */
  2919. int ata_scsi_offline_dev(struct ata_device *dev)
  2920. {
  2921. if (dev->sdev) {
  2922. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  2923. return 1;
  2924. }
  2925. return 0;
  2926. }
  2927. /**
  2928. * ata_scsi_remove_dev - remove attached SCSI device
  2929. * @dev: ATA device to remove attached SCSI device for
  2930. *
  2931. * This function is called from ata_eh_scsi_hotplug() and
  2932. * responsible for removing the SCSI device attached to @dev.
  2933. *
  2934. * LOCKING:
  2935. * Kernel thread context (may sleep).
  2936. */
  2937. static void ata_scsi_remove_dev(struct ata_device *dev)
  2938. {
  2939. struct ata_port *ap = dev->link->ap;
  2940. struct scsi_device *sdev;
  2941. unsigned long flags;
  2942. /* Alas, we need to grab scan_mutex to ensure SCSI device
  2943. * state doesn't change underneath us and thus
  2944. * scsi_device_get() always succeeds. The mutex locking can
  2945. * be removed if there is __scsi_device_get() interface which
  2946. * increments reference counts regardless of device state.
  2947. */
  2948. mutex_lock(&ap->scsi_host->scan_mutex);
  2949. spin_lock_irqsave(ap->lock, flags);
  2950. /* clearing dev->sdev is protected by host lock */
  2951. sdev = dev->sdev;
  2952. dev->sdev = NULL;
  2953. if (sdev) {
  2954. /* If user initiated unplug races with us, sdev can go
  2955. * away underneath us after the host lock and
  2956. * scan_mutex are released. Hold onto it.
  2957. */
  2958. if (scsi_device_get(sdev) == 0) {
  2959. /* The following ensures the attached sdev is
  2960. * offline on return from ata_scsi_offline_dev()
  2961. * regardless it wins or loses the race
  2962. * against this function.
  2963. */
  2964. scsi_device_set_state(sdev, SDEV_OFFLINE);
  2965. } else {
  2966. WARN_ON(1);
  2967. sdev = NULL;
  2968. }
  2969. }
  2970. spin_unlock_irqrestore(ap->lock, flags);
  2971. mutex_unlock(&ap->scsi_host->scan_mutex);
  2972. if (sdev) {
  2973. ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
  2974. dev_name(&sdev->sdev_gendev));
  2975. scsi_remove_device(sdev);
  2976. scsi_device_put(sdev);
  2977. }
  2978. }
  2979. static void ata_scsi_handle_link_detach(struct ata_link *link)
  2980. {
  2981. struct ata_port *ap = link->ap;
  2982. struct ata_device *dev;
  2983. ata_for_each_dev(dev, link, ALL) {
  2984. unsigned long flags;
  2985. if (!(dev->flags & ATA_DFLAG_DETACHED))
  2986. continue;
  2987. spin_lock_irqsave(ap->lock, flags);
  2988. dev->flags &= ~ATA_DFLAG_DETACHED;
  2989. spin_unlock_irqrestore(ap->lock, flags);
  2990. ata_scsi_remove_dev(dev);
  2991. }
  2992. }
  2993. /**
  2994. * ata_scsi_media_change_notify - send media change event
  2995. * @dev: Pointer to the disk device with media change event
  2996. *
  2997. * Tell the block layer to send a media change notification
  2998. * event.
  2999. *
  3000. * LOCKING:
  3001. * spin_lock_irqsave(host lock)
  3002. */
  3003. void ata_scsi_media_change_notify(struct ata_device *dev)
  3004. {
  3005. if (dev->sdev)
  3006. sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
  3007. GFP_ATOMIC);
  3008. }
  3009. /**
  3010. * ata_scsi_hotplug - SCSI part of hotplug
  3011. * @work: Pointer to ATA port to perform SCSI hotplug on
  3012. *
  3013. * Perform SCSI part of hotplug. It's executed from a separate
  3014. * workqueue after EH completes. This is necessary because SCSI
  3015. * hot plugging requires working EH and hot unplugging is
  3016. * synchronized with hot plugging with a mutex.
  3017. *
  3018. * LOCKING:
  3019. * Kernel thread context (may sleep).
  3020. */
  3021. void ata_scsi_hotplug(struct work_struct *work)
  3022. {
  3023. struct ata_port *ap =
  3024. container_of(work, struct ata_port, hotplug_task.work);
  3025. int i;
  3026. if (ap->pflags & ATA_PFLAG_UNLOADING) {
  3027. DPRINTK("ENTER/EXIT - unloading\n");
  3028. return;
  3029. }
  3030. DPRINTK("ENTER\n");
  3031. /* Unplug detached devices. We cannot use link iterator here
  3032. * because PMP links have to be scanned even if PMP is
  3033. * currently not attached. Iterate manually.
  3034. */
  3035. ata_scsi_handle_link_detach(&ap->link);
  3036. if (ap->pmp_link)
  3037. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  3038. ata_scsi_handle_link_detach(&ap->pmp_link[i]);
  3039. /* scan for new ones */
  3040. ata_scsi_scan_host(ap, 0);
  3041. DPRINTK("EXIT\n");
  3042. }
  3043. /**
  3044. * ata_scsi_user_scan - indication for user-initiated bus scan
  3045. * @shost: SCSI host to scan
  3046. * @channel: Channel to scan
  3047. * @id: ID to scan
  3048. * @lun: LUN to scan
  3049. *
  3050. * This function is called when user explicitly requests bus
  3051. * scan. Set probe pending flag and invoke EH.
  3052. *
  3053. * LOCKING:
  3054. * SCSI layer (we don't care)
  3055. *
  3056. * RETURNS:
  3057. * Zero.
  3058. */
  3059. static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  3060. unsigned int id, unsigned int lun)
  3061. {
  3062. struct ata_port *ap = ata_shost_to_port(shost);
  3063. unsigned long flags;
  3064. int devno, rc = 0;
  3065. if (!ap->ops->error_handler)
  3066. return -EOPNOTSUPP;
  3067. if (lun != SCAN_WILD_CARD && lun)
  3068. return -EINVAL;
  3069. if (!sata_pmp_attached(ap)) {
  3070. if (channel != SCAN_WILD_CARD && channel)
  3071. return -EINVAL;
  3072. devno = id;
  3073. } else {
  3074. if (id != SCAN_WILD_CARD && id)
  3075. return -EINVAL;
  3076. devno = channel;
  3077. }
  3078. spin_lock_irqsave(ap->lock, flags);
  3079. if (devno == SCAN_WILD_CARD) {
  3080. struct ata_link *link;
  3081. ata_for_each_link(link, ap, EDGE) {
  3082. struct ata_eh_info *ehi = &link->eh_info;
  3083. ehi->probe_mask |= ATA_ALL_DEVICES;
  3084. ehi->action |= ATA_EH_RESET;
  3085. }
  3086. } else {
  3087. struct ata_device *dev = ata_find_dev(ap, devno);
  3088. if (dev) {
  3089. struct ata_eh_info *ehi = &dev->link->eh_info;
  3090. ehi->probe_mask |= 1 << dev->devno;
  3091. ehi->action |= ATA_EH_RESET;
  3092. } else
  3093. rc = -EINVAL;
  3094. }
  3095. if (rc == 0) {
  3096. ata_port_schedule_eh(ap);
  3097. spin_unlock_irqrestore(ap->lock, flags);
  3098. ata_port_wait_eh(ap);
  3099. } else
  3100. spin_unlock_irqrestore(ap->lock, flags);
  3101. return rc;
  3102. }
  3103. /**
  3104. * ata_scsi_dev_rescan - initiate scsi_rescan_device()
  3105. * @work: Pointer to ATA port to perform scsi_rescan_device()
  3106. *
  3107. * After ATA pass thru (SAT) commands are executed successfully,
  3108. * libata need to propagate the changes to SCSI layer. This
  3109. * function must be executed from ata_aux_wq such that sdev
  3110. * attach/detach don't race with rescan.
  3111. *
  3112. * LOCKING:
  3113. * Kernel thread context (may sleep).
  3114. */
  3115. void ata_scsi_dev_rescan(struct work_struct *work)
  3116. {
  3117. struct ata_port *ap =
  3118. container_of(work, struct ata_port, scsi_rescan_task);
  3119. struct ata_link *link;
  3120. struct ata_device *dev;
  3121. unsigned long flags;
  3122. spin_lock_irqsave(ap->lock, flags);
  3123. ata_for_each_link(link, ap, EDGE) {
  3124. ata_for_each_dev(dev, link, ENABLED) {
  3125. struct scsi_device *sdev = dev->sdev;
  3126. if (!sdev)
  3127. continue;
  3128. if (scsi_device_get(sdev))
  3129. continue;
  3130. spin_unlock_irqrestore(ap->lock, flags);
  3131. scsi_rescan_device(&(sdev->sdev_gendev));
  3132. scsi_device_put(sdev);
  3133. spin_lock_irqsave(ap->lock, flags);
  3134. }
  3135. }
  3136. spin_unlock_irqrestore(ap->lock, flags);
  3137. }
  3138. /**
  3139. * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
  3140. * @host: ATA host container for all SAS ports
  3141. * @port_info: Information from low-level host driver
  3142. * @shost: SCSI host that the scsi device is attached to
  3143. *
  3144. * LOCKING:
  3145. * PCI/etc. bus probe sem.
  3146. *
  3147. * RETURNS:
  3148. * ata_port pointer on success / NULL on failure.
  3149. */
  3150. struct ata_port *ata_sas_port_alloc(struct ata_host *host,
  3151. struct ata_port_info *port_info,
  3152. struct Scsi_Host *shost)
  3153. {
  3154. struct ata_port *ap;
  3155. ap = ata_port_alloc(host);
  3156. if (!ap)
  3157. return NULL;
  3158. ap->port_no = 0;
  3159. ap->lock = shost->host_lock;
  3160. ap->pio_mask = port_info->pio_mask;
  3161. ap->mwdma_mask = port_info->mwdma_mask;
  3162. ap->udma_mask = port_info->udma_mask;
  3163. ap->flags |= port_info->flags;
  3164. ap->ops = port_info->port_ops;
  3165. ap->cbl = ATA_CBL_SATA;
  3166. return ap;
  3167. }
  3168. EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  3169. /**
  3170. * ata_sas_port_start - Set port up for dma.
  3171. * @ap: Port to initialize
  3172. *
  3173. * Called just after data structures for each port are
  3174. * initialized.
  3175. *
  3176. * May be used as the port_start() entry in ata_port_operations.
  3177. *
  3178. * LOCKING:
  3179. * Inherited from caller.
  3180. */
  3181. int ata_sas_port_start(struct ata_port *ap)
  3182. {
  3183. return 0;
  3184. }
  3185. EXPORT_SYMBOL_GPL(ata_sas_port_start);
  3186. /**
  3187. * ata_port_stop - Undo ata_sas_port_start()
  3188. * @ap: Port to shut down
  3189. *
  3190. * May be used as the port_stop() entry in ata_port_operations.
  3191. *
  3192. * LOCKING:
  3193. * Inherited from caller.
  3194. */
  3195. void ata_sas_port_stop(struct ata_port *ap)
  3196. {
  3197. }
  3198. EXPORT_SYMBOL_GPL(ata_sas_port_stop);
  3199. /**
  3200. * ata_sas_port_init - Initialize a SATA device
  3201. * @ap: SATA port to initialize
  3202. *
  3203. * LOCKING:
  3204. * PCI/etc. bus probe sem.
  3205. *
  3206. * RETURNS:
  3207. * Zero on success, non-zero on error.
  3208. */
  3209. int ata_sas_port_init(struct ata_port *ap)
  3210. {
  3211. int rc = ap->ops->port_start(ap);
  3212. if (!rc) {
  3213. ap->print_id = ata_print_id++;
  3214. rc = ata_bus_probe(ap);
  3215. }
  3216. return rc;
  3217. }
  3218. EXPORT_SYMBOL_GPL(ata_sas_port_init);
  3219. /**
  3220. * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
  3221. * @ap: SATA port to destroy
  3222. *
  3223. */
  3224. void ata_sas_port_destroy(struct ata_port *ap)
  3225. {
  3226. if (ap->ops->port_stop)
  3227. ap->ops->port_stop(ap);
  3228. kfree(ap);
  3229. }
  3230. EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
  3231. /**
  3232. * ata_sas_slave_configure - Default slave_config routine for libata devices
  3233. * @sdev: SCSI device to configure
  3234. * @ap: ATA port to which SCSI device is attached
  3235. *
  3236. * RETURNS:
  3237. * Zero.
  3238. */
  3239. int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
  3240. {
  3241. ata_scsi_sdev_config(sdev);
  3242. ata_scsi_dev_config(sdev, ap->link.device);
  3243. return 0;
  3244. }
  3245. EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
  3246. /**
  3247. * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
  3248. * @cmd: SCSI command to be sent
  3249. * @done: Completion function, called when command is complete
  3250. * @ap: ATA port to which the command is being sent
  3251. *
  3252. * RETURNS:
  3253. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3254. * 0 otherwise.
  3255. */
  3256. int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
  3257. struct ata_port *ap)
  3258. {
  3259. int rc = 0;
  3260. ata_scsi_dump_cdb(ap, cmd);
  3261. if (likely(ata_dev_enabled(ap->link.device)))
  3262. rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
  3263. else {
  3264. cmd->result = (DID_BAD_TARGET << 16);
  3265. done(cmd);
  3266. }
  3267. return rc;
  3268. }
  3269. EXPORT_SYMBOL_GPL(ata_sas_queuecmd);