libata-scsi.c 89 KB

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