libata-scsi.c 81 KB

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