libata-scsi.c 104 KB

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