libata-scsi.c 93 KB

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