libata-scsi.c 93 KB

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