libata-scsi.c 93 KB

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