libata-scsi.c 89 KB

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