libata-scsi.c 83 KB

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