libata-scsi.c 94 KB

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