libata-scsi.c 99 KB

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