libata-scsi.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905
  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_printk(dev, KERN_ERR,
  971. "drain buffer allocation failed\n");
  972. return -ENOMEM;
  973. }
  974. blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
  975. } else {
  976. sdev->sector_size = ata_id_logical_sector_size(dev->id);
  977. sdev->manage_start_stop = 1;
  978. }
  979. /*
  980. * ata_pio_sectors() expects buffer for each sector to not cross
  981. * page boundary. Enforce it by requiring buffers to be sector
  982. * aligned, which works iff sector_size is not larger than
  983. * PAGE_SIZE. ATAPI devices also need the alignment as
  984. * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
  985. */
  986. if (sdev->sector_size > PAGE_SIZE)
  987. ata_dev_printk(dev, KERN_WARNING,
  988. "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
  989. sdev->sector_size);
  990. blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
  991. if (dev->flags & ATA_DFLAG_AN)
  992. set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
  993. if (dev->flags & ATA_DFLAG_NCQ) {
  994. int depth;
  995. depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
  996. depth = min(ATA_MAX_QUEUE - 1, depth);
  997. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
  998. }
  999. blk_queue_flush_queueable(q, false);
  1000. dev->sdev = sdev;
  1001. return 0;
  1002. }
  1003. /**
  1004. * ata_scsi_slave_config - Set SCSI device attributes
  1005. * @sdev: SCSI device to examine
  1006. *
  1007. * This is called before we actually start reading
  1008. * and writing to the device, to configure certain
  1009. * SCSI mid-layer behaviors.
  1010. *
  1011. * LOCKING:
  1012. * Defined by SCSI layer. We don't really care.
  1013. */
  1014. int ata_scsi_slave_config(struct scsi_device *sdev)
  1015. {
  1016. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1017. struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
  1018. int rc = 0;
  1019. ata_scsi_sdev_config(sdev);
  1020. if (dev)
  1021. rc = ata_scsi_dev_config(sdev, dev);
  1022. return rc;
  1023. }
  1024. /**
  1025. * ata_scsi_slave_destroy - SCSI device is about to be destroyed
  1026. * @sdev: SCSI device to be destroyed
  1027. *
  1028. * @sdev is about to be destroyed for hot/warm unplugging. If
  1029. * this unplugging was initiated by libata as indicated by NULL
  1030. * dev->sdev, this function doesn't have to do anything.
  1031. * Otherwise, SCSI layer initiated warm-unplug is in progress.
  1032. * Clear dev->sdev, schedule the device for ATA detach and invoke
  1033. * EH.
  1034. *
  1035. * LOCKING:
  1036. * Defined by SCSI layer. We don't really care.
  1037. */
  1038. void ata_scsi_slave_destroy(struct scsi_device *sdev)
  1039. {
  1040. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1041. struct request_queue *q = sdev->request_queue;
  1042. unsigned long flags;
  1043. struct ata_device *dev;
  1044. if (!ap->ops->error_handler)
  1045. return;
  1046. spin_lock_irqsave(ap->lock, flags);
  1047. dev = __ata_scsi_find_dev(ap, sdev);
  1048. if (dev && dev->sdev) {
  1049. /* SCSI device already in CANCEL state, no need to offline it */
  1050. dev->sdev = NULL;
  1051. dev->flags |= ATA_DFLAG_DETACH;
  1052. ata_port_schedule_eh(ap);
  1053. }
  1054. spin_unlock_irqrestore(ap->lock, flags);
  1055. kfree(q->dma_drain_buffer);
  1056. q->dma_drain_buffer = NULL;
  1057. q->dma_drain_size = 0;
  1058. }
  1059. /**
  1060. * ata_scsi_change_queue_depth - SCSI callback for queue depth config
  1061. * @sdev: SCSI device to configure queue depth for
  1062. * @queue_depth: new queue depth
  1063. * @reason: calling context
  1064. *
  1065. * This is libata standard hostt->change_queue_depth callback.
  1066. * SCSI will call into this callback when user tries to set queue
  1067. * depth via sysfs.
  1068. *
  1069. * LOCKING:
  1070. * SCSI layer (we don't care)
  1071. *
  1072. * RETURNS:
  1073. * Newly configured queue depth.
  1074. */
  1075. int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth,
  1076. int reason)
  1077. {
  1078. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1079. struct ata_device *dev;
  1080. unsigned long flags;
  1081. if (reason != SCSI_QDEPTH_DEFAULT)
  1082. return -EOPNOTSUPP;
  1083. if (queue_depth < 1 || queue_depth == sdev->queue_depth)
  1084. return sdev->queue_depth;
  1085. dev = ata_scsi_find_dev(ap, sdev);
  1086. if (!dev || !ata_dev_enabled(dev))
  1087. return sdev->queue_depth;
  1088. /* NCQ enabled? */
  1089. spin_lock_irqsave(ap->lock, flags);
  1090. dev->flags &= ~ATA_DFLAG_NCQ_OFF;
  1091. if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
  1092. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1093. queue_depth = 1;
  1094. }
  1095. spin_unlock_irqrestore(ap->lock, flags);
  1096. /* limit and apply queue depth */
  1097. queue_depth = min(queue_depth, sdev->host->can_queue);
  1098. queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
  1099. queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
  1100. if (sdev->queue_depth == queue_depth)
  1101. return -EINVAL;
  1102. scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
  1103. return queue_depth;
  1104. }
  1105. /**
  1106. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  1107. * @qc: Storage for translated ATA taskfile
  1108. *
  1109. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  1110. * (to start). Perhaps these commands should be preceded by
  1111. * CHECK POWER MODE to see what power mode the device is already in.
  1112. * [See SAT revision 5 at www.t10.org]
  1113. *
  1114. * LOCKING:
  1115. * spin_lock_irqsave(host lock)
  1116. *
  1117. * RETURNS:
  1118. * Zero on success, non-zero on error.
  1119. */
  1120. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
  1121. {
  1122. struct scsi_cmnd *scmd = qc->scsicmd;
  1123. struct ata_taskfile *tf = &qc->tf;
  1124. const u8 *cdb = scmd->cmnd;
  1125. if (scmd->cmd_len < 5)
  1126. goto invalid_fld;
  1127. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  1128. tf->protocol = ATA_PROT_NODATA;
  1129. if (cdb[1] & 0x1) {
  1130. ; /* ignore IMMED bit, violates sat-r05 */
  1131. }
  1132. if (cdb[4] & 0x2)
  1133. goto invalid_fld; /* LOEJ bit set not supported */
  1134. if (((cdb[4] >> 4) & 0xf) != 0)
  1135. goto invalid_fld; /* power conditions not supported */
  1136. if (cdb[4] & 0x1) {
  1137. tf->nsect = 1; /* 1 sector, lba=0 */
  1138. if (qc->dev->flags & ATA_DFLAG_LBA) {
  1139. tf->flags |= ATA_TFLAG_LBA;
  1140. tf->lbah = 0x0;
  1141. tf->lbam = 0x0;
  1142. tf->lbal = 0x0;
  1143. tf->device |= ATA_LBA;
  1144. } else {
  1145. /* CHS */
  1146. tf->lbal = 0x1; /* sect */
  1147. tf->lbam = 0x0; /* cyl low */
  1148. tf->lbah = 0x0; /* cyl high */
  1149. }
  1150. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  1151. } else {
  1152. /* Some odd clown BIOSen issue spindown on power off (ACPI S4
  1153. * or S5) causing some drives to spin up and down again.
  1154. */
  1155. if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
  1156. system_state == SYSTEM_POWER_OFF)
  1157. goto skip;
  1158. if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
  1159. system_entering_hibernation())
  1160. goto skip;
  1161. /* Issue ATA STANDBY IMMEDIATE command */
  1162. tf->command = ATA_CMD_STANDBYNOW1;
  1163. }
  1164. /*
  1165. * Standby and Idle condition timers could be implemented but that
  1166. * would require libata to implement the Power condition mode page
  1167. * and allow the user to change it. Changing mode pages requires
  1168. * MODE SELECT to be implemented.
  1169. */
  1170. return 0;
  1171. invalid_fld:
  1172. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1173. /* "Invalid field in cbd" */
  1174. return 1;
  1175. skip:
  1176. scmd->result = SAM_STAT_GOOD;
  1177. return 1;
  1178. }
  1179. /**
  1180. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  1181. * @qc: Storage for translated ATA taskfile
  1182. *
  1183. * Sets up an ATA taskfile to issue FLUSH CACHE or
  1184. * FLUSH CACHE EXT.
  1185. *
  1186. * LOCKING:
  1187. * spin_lock_irqsave(host lock)
  1188. *
  1189. * RETURNS:
  1190. * Zero on success, non-zero on error.
  1191. */
  1192. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
  1193. {
  1194. struct ata_taskfile *tf = &qc->tf;
  1195. tf->flags |= ATA_TFLAG_DEVICE;
  1196. tf->protocol = ATA_PROT_NODATA;
  1197. if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
  1198. tf->command = ATA_CMD_FLUSH_EXT;
  1199. else
  1200. tf->command = ATA_CMD_FLUSH;
  1201. /* flush is critical for IO integrity, consider it an IO command */
  1202. qc->flags |= ATA_QCFLAG_IO;
  1203. return 0;
  1204. }
  1205. /**
  1206. * scsi_6_lba_len - Get LBA and transfer length
  1207. * @cdb: SCSI command to translate
  1208. *
  1209. * Calculate LBA and transfer length for 6-byte commands.
  1210. *
  1211. * RETURNS:
  1212. * @plba: the LBA
  1213. * @plen: the transfer length
  1214. */
  1215. static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1216. {
  1217. u64 lba = 0;
  1218. u32 len;
  1219. VPRINTK("six-byte command\n");
  1220. lba |= ((u64)(cdb[1] & 0x1f)) << 16;
  1221. lba |= ((u64)cdb[2]) << 8;
  1222. lba |= ((u64)cdb[3]);
  1223. len = cdb[4];
  1224. *plba = lba;
  1225. *plen = len;
  1226. }
  1227. /**
  1228. * scsi_10_lba_len - Get LBA and transfer length
  1229. * @cdb: SCSI command to translate
  1230. *
  1231. * Calculate LBA and transfer length for 10-byte commands.
  1232. *
  1233. * RETURNS:
  1234. * @plba: the LBA
  1235. * @plen: the transfer length
  1236. */
  1237. static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1238. {
  1239. u64 lba = 0;
  1240. u32 len = 0;
  1241. VPRINTK("ten-byte command\n");
  1242. lba |= ((u64)cdb[2]) << 24;
  1243. lba |= ((u64)cdb[3]) << 16;
  1244. lba |= ((u64)cdb[4]) << 8;
  1245. lba |= ((u64)cdb[5]);
  1246. len |= ((u32)cdb[7]) << 8;
  1247. len |= ((u32)cdb[8]);
  1248. *plba = lba;
  1249. *plen = len;
  1250. }
  1251. /**
  1252. * scsi_16_lba_len - Get LBA and transfer length
  1253. * @cdb: SCSI command to translate
  1254. *
  1255. * Calculate LBA and transfer length for 16-byte commands.
  1256. *
  1257. * RETURNS:
  1258. * @plba: the LBA
  1259. * @plen: the transfer length
  1260. */
  1261. static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
  1262. {
  1263. u64 lba = 0;
  1264. u32 len = 0;
  1265. VPRINTK("sixteen-byte command\n");
  1266. lba |= ((u64)cdb[2]) << 56;
  1267. lba |= ((u64)cdb[3]) << 48;
  1268. lba |= ((u64)cdb[4]) << 40;
  1269. lba |= ((u64)cdb[5]) << 32;
  1270. lba |= ((u64)cdb[6]) << 24;
  1271. lba |= ((u64)cdb[7]) << 16;
  1272. lba |= ((u64)cdb[8]) << 8;
  1273. lba |= ((u64)cdb[9]);
  1274. len |= ((u32)cdb[10]) << 24;
  1275. len |= ((u32)cdb[11]) << 16;
  1276. len |= ((u32)cdb[12]) << 8;
  1277. len |= ((u32)cdb[13]);
  1278. *plba = lba;
  1279. *plen = len;
  1280. }
  1281. /**
  1282. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  1283. * @qc: Storage for translated ATA taskfile
  1284. *
  1285. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  1286. *
  1287. * LOCKING:
  1288. * spin_lock_irqsave(host lock)
  1289. *
  1290. * RETURNS:
  1291. * Zero on success, non-zero on error.
  1292. */
  1293. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
  1294. {
  1295. struct scsi_cmnd *scmd = qc->scsicmd;
  1296. struct ata_taskfile *tf = &qc->tf;
  1297. struct ata_device *dev = qc->dev;
  1298. u64 dev_sectors = qc->dev->n_sectors;
  1299. const u8 *cdb = scmd->cmnd;
  1300. u64 block;
  1301. u32 n_block;
  1302. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1303. tf->protocol = ATA_PROT_NODATA;
  1304. if (cdb[0] == VERIFY) {
  1305. if (scmd->cmd_len < 10)
  1306. goto invalid_fld;
  1307. scsi_10_lba_len(cdb, &block, &n_block);
  1308. } else if (cdb[0] == VERIFY_16) {
  1309. if (scmd->cmd_len < 16)
  1310. goto invalid_fld;
  1311. scsi_16_lba_len(cdb, &block, &n_block);
  1312. } else
  1313. goto invalid_fld;
  1314. if (!n_block)
  1315. goto nothing_to_do;
  1316. if (block >= dev_sectors)
  1317. goto out_of_range;
  1318. if ((block + n_block) > dev_sectors)
  1319. goto out_of_range;
  1320. if (dev->flags & ATA_DFLAG_LBA) {
  1321. tf->flags |= ATA_TFLAG_LBA;
  1322. if (lba_28_ok(block, n_block)) {
  1323. /* use LBA28 */
  1324. tf->command = ATA_CMD_VERIFY;
  1325. tf->device |= (block >> 24) & 0xf;
  1326. } else if (lba_48_ok(block, n_block)) {
  1327. if (!(dev->flags & ATA_DFLAG_LBA48))
  1328. goto out_of_range;
  1329. /* use LBA48 */
  1330. tf->flags |= ATA_TFLAG_LBA48;
  1331. tf->command = ATA_CMD_VERIFY_EXT;
  1332. tf->hob_nsect = (n_block >> 8) & 0xff;
  1333. tf->hob_lbah = (block >> 40) & 0xff;
  1334. tf->hob_lbam = (block >> 32) & 0xff;
  1335. tf->hob_lbal = (block >> 24) & 0xff;
  1336. } else
  1337. /* request too large even for LBA48 */
  1338. goto out_of_range;
  1339. tf->nsect = n_block & 0xff;
  1340. tf->lbah = (block >> 16) & 0xff;
  1341. tf->lbam = (block >> 8) & 0xff;
  1342. tf->lbal = block & 0xff;
  1343. tf->device |= ATA_LBA;
  1344. } else {
  1345. /* CHS */
  1346. u32 sect, head, cyl, track;
  1347. if (!lba_28_ok(block, n_block))
  1348. goto out_of_range;
  1349. /* Convert LBA to CHS */
  1350. track = (u32)block / dev->sectors;
  1351. cyl = track / dev->heads;
  1352. head = track % dev->heads;
  1353. sect = (u32)block % dev->sectors + 1;
  1354. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1355. (u32)block, track, cyl, head, sect);
  1356. /* Check whether the converted CHS can fit.
  1357. Cylinder: 0-65535
  1358. Head: 0-15
  1359. Sector: 1-255*/
  1360. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1361. goto out_of_range;
  1362. tf->command = ATA_CMD_VERIFY;
  1363. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1364. tf->lbal = sect;
  1365. tf->lbam = cyl;
  1366. tf->lbah = cyl >> 8;
  1367. tf->device |= head;
  1368. }
  1369. return 0;
  1370. invalid_fld:
  1371. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1372. /* "Invalid field in cbd" */
  1373. return 1;
  1374. out_of_range:
  1375. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1376. /* "Logical Block Address out of range" */
  1377. return 1;
  1378. nothing_to_do:
  1379. scmd->result = SAM_STAT_GOOD;
  1380. return 1;
  1381. }
  1382. /**
  1383. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  1384. * @qc: Storage for translated ATA taskfile
  1385. *
  1386. * Converts any of six SCSI read/write commands into the
  1387. * ATA counterpart, including starting sector (LBA),
  1388. * sector count, and taking into account the device's LBA48
  1389. * support.
  1390. *
  1391. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  1392. * %WRITE_16 are currently supported.
  1393. *
  1394. * LOCKING:
  1395. * spin_lock_irqsave(host lock)
  1396. *
  1397. * RETURNS:
  1398. * Zero on success, non-zero on error.
  1399. */
  1400. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
  1401. {
  1402. struct scsi_cmnd *scmd = qc->scsicmd;
  1403. const u8 *cdb = scmd->cmnd;
  1404. unsigned int tf_flags = 0;
  1405. u64 block;
  1406. u32 n_block;
  1407. int rc;
  1408. if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
  1409. tf_flags |= ATA_TFLAG_WRITE;
  1410. /* Calculate the SCSI LBA, transfer length and FUA. */
  1411. switch (cdb[0]) {
  1412. case READ_10:
  1413. case WRITE_10:
  1414. if (unlikely(scmd->cmd_len < 10))
  1415. goto invalid_fld;
  1416. scsi_10_lba_len(cdb, &block, &n_block);
  1417. if (unlikely(cdb[1] & (1 << 3)))
  1418. tf_flags |= ATA_TFLAG_FUA;
  1419. break;
  1420. case READ_6:
  1421. case WRITE_6:
  1422. if (unlikely(scmd->cmd_len < 6))
  1423. goto invalid_fld;
  1424. scsi_6_lba_len(cdb, &block, &n_block);
  1425. /* for 6-byte r/w commands, transfer length 0
  1426. * means 256 blocks of data, not 0 block.
  1427. */
  1428. if (!n_block)
  1429. n_block = 256;
  1430. break;
  1431. case READ_16:
  1432. case WRITE_16:
  1433. if (unlikely(scmd->cmd_len < 16))
  1434. goto invalid_fld;
  1435. scsi_16_lba_len(cdb, &block, &n_block);
  1436. if (unlikely(cdb[1] & (1 << 3)))
  1437. tf_flags |= ATA_TFLAG_FUA;
  1438. break;
  1439. default:
  1440. DPRINTK("no-byte command\n");
  1441. goto invalid_fld;
  1442. }
  1443. /* Check and compose ATA command */
  1444. if (!n_block)
  1445. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  1446. * length 0 means transfer 0 block of data.
  1447. * However, for ATA R/W commands, sector count 0 means
  1448. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  1449. *
  1450. * WARNING: one or two older ATA drives treat 0 as 0...
  1451. */
  1452. goto nothing_to_do;
  1453. qc->flags |= ATA_QCFLAG_IO;
  1454. qc->nbytes = n_block * scmd->device->sector_size;
  1455. rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
  1456. qc->tag);
  1457. if (likely(rc == 0))
  1458. return 0;
  1459. if (rc == -ERANGE)
  1460. goto out_of_range;
  1461. /* treat all other errors as -EINVAL, fall through */
  1462. invalid_fld:
  1463. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1464. /* "Invalid field in cbd" */
  1465. return 1;
  1466. out_of_range:
  1467. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1468. /* "Logical Block Address out of range" */
  1469. return 1;
  1470. nothing_to_do:
  1471. scmd->result = SAM_STAT_GOOD;
  1472. return 1;
  1473. }
  1474. static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
  1475. {
  1476. struct ata_port *ap = qc->ap;
  1477. struct scsi_cmnd *cmd = qc->scsicmd;
  1478. u8 *cdb = cmd->cmnd;
  1479. int need_sense = (qc->err_mask != 0);
  1480. /* For ATA pass thru (SAT) commands, generate a sense block if
  1481. * user mandated it or if there's an error. Note that if we
  1482. * generate because the user forced us to, a check condition
  1483. * is generated and the ATA register values are returned
  1484. * whether the command completed successfully or not. If there
  1485. * was no error, SK, ASC and ASCQ will all be zero.
  1486. */
  1487. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1488. ((cdb[2] & 0x20) || need_sense)) {
  1489. ata_gen_passthru_sense(qc);
  1490. } else {
  1491. if (!need_sense) {
  1492. cmd->result = SAM_STAT_GOOD;
  1493. } else {
  1494. /* TODO: decide which descriptor format to use
  1495. * for 48b LBA devices and call that here
  1496. * instead of the fixed desc, which is only
  1497. * good for smaller LBA (and maybe CHS?)
  1498. * devices.
  1499. */
  1500. ata_gen_ata_sense(qc);
  1501. }
  1502. }
  1503. if (need_sense && !ap->ops->error_handler)
  1504. ata_dump_status(ap->print_id, &qc->result_tf);
  1505. qc->scsidone(cmd);
  1506. ata_qc_free(qc);
  1507. }
  1508. /**
  1509. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1510. * @dev: ATA device to which the command is addressed
  1511. * @cmd: SCSI command to execute
  1512. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1513. *
  1514. * Our ->queuecommand() function has decided that the SCSI
  1515. * command issued can be directly translated into an ATA
  1516. * command, rather than handled internally.
  1517. *
  1518. * This function sets up an ata_queued_cmd structure for the
  1519. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1520. *
  1521. * The xlat_func argument (actor) returns 0 if ready to execute
  1522. * ATA command, else 1 to finish translation. If 1 is returned
  1523. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1524. * to be set reflecting an error condition or clean (early)
  1525. * termination.
  1526. *
  1527. * LOCKING:
  1528. * spin_lock_irqsave(host lock)
  1529. *
  1530. * RETURNS:
  1531. * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
  1532. * needs to be deferred.
  1533. */
  1534. static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
  1535. ata_xlat_func_t xlat_func)
  1536. {
  1537. struct ata_port *ap = dev->link->ap;
  1538. struct ata_queued_cmd *qc;
  1539. int rc;
  1540. VPRINTK("ENTER\n");
  1541. qc = ata_scsi_qc_new(dev, cmd);
  1542. if (!qc)
  1543. goto err_mem;
  1544. /* data is present; dma-map it */
  1545. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1546. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1547. if (unlikely(scsi_bufflen(cmd) < 1)) {
  1548. ata_dev_printk(dev, KERN_WARNING,
  1549. "WARNING: zero len r/w req\n");
  1550. goto err_did;
  1551. }
  1552. ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
  1553. qc->dma_dir = cmd->sc_data_direction;
  1554. }
  1555. qc->complete_fn = ata_scsi_qc_complete;
  1556. if (xlat_func(qc))
  1557. goto early_finish;
  1558. if (ap->ops->qc_defer) {
  1559. if ((rc = ap->ops->qc_defer(qc)))
  1560. goto defer;
  1561. }
  1562. /* select device, send command to hardware */
  1563. ata_qc_issue(qc);
  1564. VPRINTK("EXIT\n");
  1565. return 0;
  1566. early_finish:
  1567. ata_qc_free(qc);
  1568. cmd->scsi_done(cmd);
  1569. DPRINTK("EXIT - early finish (good or error)\n");
  1570. return 0;
  1571. err_did:
  1572. ata_qc_free(qc);
  1573. cmd->result = (DID_ERROR << 16);
  1574. cmd->scsi_done(cmd);
  1575. err_mem:
  1576. DPRINTK("EXIT - internal\n");
  1577. return 0;
  1578. defer:
  1579. ata_qc_free(qc);
  1580. DPRINTK("EXIT - defer\n");
  1581. if (rc == ATA_DEFER_LINK)
  1582. return SCSI_MLQUEUE_DEVICE_BUSY;
  1583. else
  1584. return SCSI_MLQUEUE_HOST_BUSY;
  1585. }
  1586. /**
  1587. * ata_scsi_rbuf_get - Map response buffer.
  1588. * @cmd: SCSI command containing buffer to be mapped.
  1589. * @flags: unsigned long variable to store irq enable status
  1590. * @copy_in: copy in from user buffer
  1591. *
  1592. * Prepare buffer for simulated SCSI commands.
  1593. *
  1594. * LOCKING:
  1595. * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
  1596. *
  1597. * RETURNS:
  1598. * Pointer to response buffer.
  1599. */
  1600. static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
  1601. unsigned long *flags)
  1602. {
  1603. spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
  1604. memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
  1605. if (copy_in)
  1606. sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1607. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1608. return ata_scsi_rbuf;
  1609. }
  1610. /**
  1611. * ata_scsi_rbuf_put - Unmap response buffer.
  1612. * @cmd: SCSI command containing buffer to be unmapped.
  1613. * @copy_out: copy out result
  1614. * @flags: @flags passed to ata_scsi_rbuf_get()
  1615. *
  1616. * Returns rbuf buffer. The result is copied to @cmd's buffer if
  1617. * @copy_back is true.
  1618. *
  1619. * LOCKING:
  1620. * Unlocks ata_scsi_rbuf_lock.
  1621. */
  1622. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
  1623. unsigned long *flags)
  1624. {
  1625. if (copy_out)
  1626. sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
  1627. ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
  1628. spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
  1629. }
  1630. /**
  1631. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1632. * @args: device IDENTIFY data / SCSI command of interest.
  1633. * @actor: Callback hook for desired SCSI command simulator
  1634. *
  1635. * Takes care of the hard work of simulating a SCSI command...
  1636. * Mapping the response buffer, calling the command's handler,
  1637. * and handling the handler's return value. This return value
  1638. * indicates whether the handler wishes the SCSI command to be
  1639. * completed successfully (0), or not (in which case cmd->result
  1640. * and sense buffer are assumed to be set).
  1641. *
  1642. * LOCKING:
  1643. * spin_lock_irqsave(host lock)
  1644. */
  1645. static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1646. unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
  1647. {
  1648. u8 *rbuf;
  1649. unsigned int rc;
  1650. struct scsi_cmnd *cmd = args->cmd;
  1651. unsigned long flags;
  1652. rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
  1653. rc = actor(args, rbuf);
  1654. ata_scsi_rbuf_put(cmd, rc == 0, &flags);
  1655. if (rc == 0)
  1656. cmd->result = SAM_STAT_GOOD;
  1657. args->done(cmd);
  1658. }
  1659. /**
  1660. * ata_scsiop_inq_std - Simulate INQUIRY command
  1661. * @args: device IDENTIFY data / SCSI command of interest.
  1662. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1663. *
  1664. * Returns standard device identification data associated
  1665. * with non-VPD INQUIRY command output.
  1666. *
  1667. * LOCKING:
  1668. * spin_lock_irqsave(host lock)
  1669. */
  1670. static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
  1671. {
  1672. const u8 versions[] = {
  1673. 0x60, /* SAM-3 (no version claimed) */
  1674. 0x03,
  1675. 0x20, /* SBC-2 (no version claimed) */
  1676. 0x02,
  1677. 0x60 /* SPC-3 (no version claimed) */
  1678. };
  1679. u8 hdr[] = {
  1680. TYPE_DISK,
  1681. 0,
  1682. 0x5, /* claim SPC-3 version compatibility */
  1683. 2,
  1684. 95 - 4
  1685. };
  1686. VPRINTK("ENTER\n");
  1687. /* set scsi removeable (RMB) bit per ata bit */
  1688. if (ata_id_removeable(args->id))
  1689. hdr[1] |= (1 << 7);
  1690. memcpy(rbuf, hdr, sizeof(hdr));
  1691. memcpy(&rbuf[8], "ATA ", 8);
  1692. ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
  1693. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1694. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1695. memcpy(&rbuf[32], "n/a ", 4);
  1696. memcpy(rbuf + 59, versions, sizeof(versions));
  1697. return 0;
  1698. }
  1699. /**
  1700. * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
  1701. * @args: device IDENTIFY data / SCSI command of interest.
  1702. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1703. *
  1704. * Returns list of inquiry VPD pages available.
  1705. *
  1706. * LOCKING:
  1707. * spin_lock_irqsave(host lock)
  1708. */
  1709. static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
  1710. {
  1711. const u8 pages[] = {
  1712. 0x00, /* page 0x00, this page */
  1713. 0x80, /* page 0x80, unit serial no page */
  1714. 0x83, /* page 0x83, device ident page */
  1715. 0x89, /* page 0x89, ata info page */
  1716. 0xb0, /* page 0xb0, block limits page */
  1717. 0xb1, /* page 0xb1, block device characteristics page */
  1718. 0xb2, /* page 0xb2, thin provisioning page */
  1719. };
  1720. rbuf[3] = sizeof(pages); /* number of supported VPD pages */
  1721. memcpy(rbuf + 4, pages, sizeof(pages));
  1722. return 0;
  1723. }
  1724. /**
  1725. * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
  1726. * @args: device IDENTIFY data / SCSI command of interest.
  1727. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1728. *
  1729. * Returns ATA device serial number.
  1730. *
  1731. * LOCKING:
  1732. * spin_lock_irqsave(host lock)
  1733. */
  1734. static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
  1735. {
  1736. const u8 hdr[] = {
  1737. 0,
  1738. 0x80, /* this page code */
  1739. 0,
  1740. ATA_ID_SERNO_LEN, /* page len */
  1741. };
  1742. memcpy(rbuf, hdr, sizeof(hdr));
  1743. ata_id_string(args->id, (unsigned char *) &rbuf[4],
  1744. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1745. return 0;
  1746. }
  1747. /**
  1748. * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
  1749. * @args: device IDENTIFY data / SCSI command of interest.
  1750. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1751. *
  1752. * Yields two logical unit device identification designators:
  1753. * - vendor specific ASCII containing the ATA serial number
  1754. * - SAT defined "t10 vendor id based" containing ASCII vendor
  1755. * name ("ATA "), model and serial numbers.
  1756. *
  1757. * LOCKING:
  1758. * spin_lock_irqsave(host lock)
  1759. */
  1760. static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
  1761. {
  1762. const int sat_model_serial_desc_len = 68;
  1763. int num;
  1764. rbuf[1] = 0x83; /* this page code */
  1765. num = 4;
  1766. /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
  1767. rbuf[num + 0] = 2;
  1768. rbuf[num + 3] = ATA_ID_SERNO_LEN;
  1769. num += 4;
  1770. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1771. ATA_ID_SERNO, ATA_ID_SERNO_LEN);
  1772. num += ATA_ID_SERNO_LEN;
  1773. /* SAT defined lu model and serial numbers descriptor */
  1774. /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
  1775. rbuf[num + 0] = 2;
  1776. rbuf[num + 1] = 1;
  1777. rbuf[num + 3] = sat_model_serial_desc_len;
  1778. num += 4;
  1779. memcpy(rbuf + num, "ATA ", 8);
  1780. num += 8;
  1781. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
  1782. ATA_ID_PROD_LEN);
  1783. num += ATA_ID_PROD_LEN;
  1784. ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
  1785. ATA_ID_SERNO_LEN);
  1786. num += ATA_ID_SERNO_LEN;
  1787. if (ata_id_has_wwn(args->id)) {
  1788. /* SAT defined lu world wide name */
  1789. /* piv=0, assoc=lu, code_set=binary, designator=NAA */
  1790. rbuf[num + 0] = 1;
  1791. rbuf[num + 1] = 3;
  1792. rbuf[num + 3] = ATA_ID_WWN_LEN;
  1793. num += 4;
  1794. ata_id_string(args->id, (unsigned char *) rbuf + num,
  1795. ATA_ID_WWN, ATA_ID_WWN_LEN);
  1796. num += ATA_ID_WWN_LEN;
  1797. }
  1798. rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
  1799. return 0;
  1800. }
  1801. /**
  1802. * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
  1803. * @args: device IDENTIFY data / SCSI command of interest.
  1804. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1805. *
  1806. * Yields SAT-specified ATA VPD page.
  1807. *
  1808. * LOCKING:
  1809. * spin_lock_irqsave(host lock)
  1810. */
  1811. static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
  1812. {
  1813. struct ata_taskfile tf;
  1814. memset(&tf, 0, sizeof(tf));
  1815. rbuf[1] = 0x89; /* our page code */
  1816. rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
  1817. rbuf[3] = (0x238 & 0xff);
  1818. memcpy(&rbuf[8], "linux ", 8);
  1819. memcpy(&rbuf[16], "libata ", 16);
  1820. memcpy(&rbuf[32], DRV_VERSION, 4);
  1821. ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
  1822. /* we don't store the ATA device signature, so we fake it */
  1823. tf.command = ATA_DRDY; /* really, this is Status reg */
  1824. tf.lbal = 0x1;
  1825. tf.nsect = 0x1;
  1826. ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
  1827. rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
  1828. rbuf[56] = ATA_CMD_ID_ATA;
  1829. memcpy(&rbuf[60], &args->id[0], 512);
  1830. return 0;
  1831. }
  1832. static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
  1833. {
  1834. u16 min_io_sectors;
  1835. rbuf[1] = 0xb0;
  1836. rbuf[3] = 0x3c; /* required VPD size with unmap support */
  1837. /*
  1838. * Optimal transfer length granularity.
  1839. *
  1840. * This is always one physical block, but for disks with a smaller
  1841. * logical than physical sector size we need to figure out what the
  1842. * latter is.
  1843. */
  1844. min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
  1845. put_unaligned_be16(min_io_sectors, &rbuf[6]);
  1846. /*
  1847. * Optimal unmap granularity.
  1848. *
  1849. * The ATA spec doesn't even know about a granularity or alignment
  1850. * for the TRIM command. We can leave away most of the unmap related
  1851. * VPD page entries, but we have specifify a granularity to signal
  1852. * that we support some form of unmap - in thise case via WRITE SAME
  1853. * with the unmap bit set.
  1854. */
  1855. if (ata_id_has_trim(args->id)) {
  1856. put_unaligned_be64(65535 * 512 / 8, &rbuf[36]);
  1857. put_unaligned_be32(1, &rbuf[28]);
  1858. }
  1859. return 0;
  1860. }
  1861. static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
  1862. {
  1863. int form_factor = ata_id_form_factor(args->id);
  1864. int media_rotation_rate = ata_id_rotation_rate(args->id);
  1865. rbuf[1] = 0xb1;
  1866. rbuf[3] = 0x3c;
  1867. rbuf[4] = media_rotation_rate >> 8;
  1868. rbuf[5] = media_rotation_rate;
  1869. rbuf[7] = form_factor;
  1870. return 0;
  1871. }
  1872. static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
  1873. {
  1874. /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
  1875. rbuf[1] = 0xb2;
  1876. rbuf[3] = 0x4;
  1877. rbuf[5] = 1 << 6; /* TPWS */
  1878. return 0;
  1879. }
  1880. /**
  1881. * ata_scsiop_noop - Command handler that simply returns success.
  1882. * @args: device IDENTIFY data / SCSI command of interest.
  1883. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1884. *
  1885. * No operation. Simply returns success to caller, to indicate
  1886. * that the caller should successfully complete this SCSI command.
  1887. *
  1888. * LOCKING:
  1889. * spin_lock_irqsave(host lock)
  1890. */
  1891. static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
  1892. {
  1893. VPRINTK("ENTER\n");
  1894. return 0;
  1895. }
  1896. /**
  1897. * ata_msense_caching - Simulate MODE SENSE caching info page
  1898. * @id: device IDENTIFY data
  1899. * @buf: output buffer
  1900. *
  1901. * Generate a caching info page, which conditionally indicates
  1902. * write caching to the SCSI layer, depending on device
  1903. * capabilities.
  1904. *
  1905. * LOCKING:
  1906. * None.
  1907. */
  1908. static unsigned int ata_msense_caching(u16 *id, u8 *buf)
  1909. {
  1910. memcpy(buf, def_cache_mpage, sizeof(def_cache_mpage));
  1911. if (ata_id_wcache_enabled(id))
  1912. buf[2] |= (1 << 2); /* write cache enable */
  1913. if (!ata_id_rahead_enabled(id))
  1914. buf[12] |= (1 << 5); /* disable read ahead */
  1915. return sizeof(def_cache_mpage);
  1916. }
  1917. /**
  1918. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1919. * @buf: output buffer
  1920. *
  1921. * Generate a generic MODE SENSE control mode page.
  1922. *
  1923. * LOCKING:
  1924. * None.
  1925. */
  1926. static unsigned int ata_msense_ctl_mode(u8 *buf)
  1927. {
  1928. memcpy(buf, def_control_mpage, sizeof(def_control_mpage));
  1929. return sizeof(def_control_mpage);
  1930. }
  1931. /**
  1932. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1933. * @buf: output buffer
  1934. *
  1935. * Generate a generic MODE SENSE r/w error recovery page.
  1936. *
  1937. * LOCKING:
  1938. * None.
  1939. */
  1940. static unsigned int ata_msense_rw_recovery(u8 *buf)
  1941. {
  1942. memcpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage));
  1943. return sizeof(def_rw_recovery_mpage);
  1944. }
  1945. /*
  1946. * We can turn this into a real blacklist if it's needed, for now just
  1947. * blacklist any Maxtor BANC1G10 revision firmware
  1948. */
  1949. static int ata_dev_supports_fua(u16 *id)
  1950. {
  1951. unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
  1952. if (!libata_fua)
  1953. return 0;
  1954. if (!ata_id_has_fua(id))
  1955. return 0;
  1956. ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
  1957. ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
  1958. if (strcmp(model, "Maxtor"))
  1959. return 1;
  1960. if (strcmp(fw, "BANC1G10"))
  1961. return 1;
  1962. return 0; /* blacklisted */
  1963. }
  1964. /**
  1965. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1966. * @args: device IDENTIFY data / SCSI command of interest.
  1967. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1968. *
  1969. * Simulate MODE SENSE commands. Assume this is invoked for direct
  1970. * access devices (e.g. disks) only. There should be no block
  1971. * descriptor for other device types.
  1972. *
  1973. * LOCKING:
  1974. * spin_lock_irqsave(host lock)
  1975. */
  1976. static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
  1977. {
  1978. struct ata_device *dev = args->dev;
  1979. u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
  1980. const u8 sat_blk_desc[] = {
  1981. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  1982. 0,
  1983. 0, 0x2, 0x0 /* block length: 512 bytes */
  1984. };
  1985. u8 pg, spg;
  1986. unsigned int ebd, page_control, six_byte;
  1987. u8 dpofua;
  1988. VPRINTK("ENTER\n");
  1989. six_byte = (scsicmd[0] == MODE_SENSE);
  1990. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  1991. /*
  1992. * LLBA bit in msense(10) ignored (compliant)
  1993. */
  1994. page_control = scsicmd[2] >> 6;
  1995. switch (page_control) {
  1996. case 0: /* current */
  1997. break; /* supported */
  1998. case 3: /* saved */
  1999. goto saving_not_supp;
  2000. case 1: /* changeable */
  2001. case 2: /* defaults */
  2002. default:
  2003. goto invalid_fld;
  2004. }
  2005. if (six_byte)
  2006. p += 4 + (ebd ? 8 : 0);
  2007. else
  2008. p += 8 + (ebd ? 8 : 0);
  2009. pg = scsicmd[2] & 0x3f;
  2010. spg = scsicmd[3];
  2011. /*
  2012. * No mode subpages supported (yet) but asking for _all_
  2013. * subpages may be valid
  2014. */
  2015. if (spg && (spg != ALL_SUB_MPAGES))
  2016. goto invalid_fld;
  2017. switch(pg) {
  2018. case RW_RECOVERY_MPAGE:
  2019. p += ata_msense_rw_recovery(p);
  2020. break;
  2021. case CACHE_MPAGE:
  2022. p += ata_msense_caching(args->id, p);
  2023. break;
  2024. case CONTROL_MPAGE:
  2025. p += ata_msense_ctl_mode(p);
  2026. break;
  2027. case ALL_MPAGES:
  2028. p += ata_msense_rw_recovery(p);
  2029. p += ata_msense_caching(args->id, p);
  2030. p += ata_msense_ctl_mode(p);
  2031. break;
  2032. default: /* invalid page code */
  2033. goto invalid_fld;
  2034. }
  2035. dpofua = 0;
  2036. if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
  2037. (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
  2038. dpofua = 1 << 4;
  2039. if (six_byte) {
  2040. rbuf[0] = p - rbuf - 1;
  2041. rbuf[2] |= dpofua;
  2042. if (ebd) {
  2043. rbuf[3] = sizeof(sat_blk_desc);
  2044. memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
  2045. }
  2046. } else {
  2047. unsigned int output_len = p - rbuf - 2;
  2048. rbuf[0] = output_len >> 8;
  2049. rbuf[1] = output_len;
  2050. rbuf[3] |= dpofua;
  2051. if (ebd) {
  2052. rbuf[7] = sizeof(sat_blk_desc);
  2053. memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
  2054. }
  2055. }
  2056. return 0;
  2057. invalid_fld:
  2058. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  2059. /* "Invalid field in cbd" */
  2060. return 1;
  2061. saving_not_supp:
  2062. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  2063. /* "Saving parameters not supported" */
  2064. return 1;
  2065. }
  2066. /**
  2067. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  2068. * @args: device IDENTIFY data / SCSI command of interest.
  2069. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2070. *
  2071. * Simulate READ CAPACITY commands.
  2072. *
  2073. * LOCKING:
  2074. * None.
  2075. */
  2076. static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
  2077. {
  2078. struct ata_device *dev = args->dev;
  2079. u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
  2080. u32 sector_size; /* physical sector size in bytes */
  2081. u8 log2_per_phys;
  2082. u16 lowest_aligned;
  2083. sector_size = ata_id_logical_sector_size(dev->id);
  2084. log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
  2085. lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
  2086. VPRINTK("ENTER\n");
  2087. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  2088. if (last_lba >= 0xffffffffULL)
  2089. last_lba = 0xffffffff;
  2090. /* sector count, 32-bit */
  2091. rbuf[0] = last_lba >> (8 * 3);
  2092. rbuf[1] = last_lba >> (8 * 2);
  2093. rbuf[2] = last_lba >> (8 * 1);
  2094. rbuf[3] = last_lba;
  2095. /* sector size */
  2096. rbuf[4] = sector_size >> (8 * 3);
  2097. rbuf[5] = sector_size >> (8 * 2);
  2098. rbuf[6] = sector_size >> (8 * 1);
  2099. rbuf[7] = sector_size;
  2100. } else {
  2101. /* sector count, 64-bit */
  2102. rbuf[0] = last_lba >> (8 * 7);
  2103. rbuf[1] = last_lba >> (8 * 6);
  2104. rbuf[2] = last_lba >> (8 * 5);
  2105. rbuf[3] = last_lba >> (8 * 4);
  2106. rbuf[4] = last_lba >> (8 * 3);
  2107. rbuf[5] = last_lba >> (8 * 2);
  2108. rbuf[6] = last_lba >> (8 * 1);
  2109. rbuf[7] = last_lba;
  2110. /* sector size */
  2111. rbuf[ 8] = sector_size >> (8 * 3);
  2112. rbuf[ 9] = sector_size >> (8 * 2);
  2113. rbuf[10] = sector_size >> (8 * 1);
  2114. rbuf[11] = sector_size;
  2115. rbuf[12] = 0;
  2116. rbuf[13] = log2_per_phys;
  2117. rbuf[14] = (lowest_aligned >> 8) & 0x3f;
  2118. rbuf[15] = lowest_aligned;
  2119. if (ata_id_has_trim(args->id)) {
  2120. rbuf[14] |= 0x80; /* TPE */
  2121. if (ata_id_has_zero_after_trim(args->id))
  2122. rbuf[14] |= 0x40; /* TPRZ */
  2123. }
  2124. }
  2125. return 0;
  2126. }
  2127. /**
  2128. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  2129. * @args: device IDENTIFY data / SCSI command of interest.
  2130. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  2131. *
  2132. * Simulate REPORT LUNS command.
  2133. *
  2134. * LOCKING:
  2135. * spin_lock_irqsave(host lock)
  2136. */
  2137. static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
  2138. {
  2139. VPRINTK("ENTER\n");
  2140. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  2141. return 0;
  2142. }
  2143. static void atapi_sense_complete(struct ata_queued_cmd *qc)
  2144. {
  2145. if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
  2146. /* FIXME: not quite right; we don't want the
  2147. * translation of taskfile registers into
  2148. * a sense descriptors, since that's only
  2149. * correct for ATA, not ATAPI
  2150. */
  2151. ata_gen_passthru_sense(qc);
  2152. }
  2153. qc->scsidone(qc->scsicmd);
  2154. ata_qc_free(qc);
  2155. }
  2156. /* is it pointless to prefer PIO for "safety reasons"? */
  2157. static inline int ata_pio_use_silly(struct ata_port *ap)
  2158. {
  2159. return (ap->flags & ATA_FLAG_PIO_DMA);
  2160. }
  2161. static void atapi_request_sense(struct ata_queued_cmd *qc)
  2162. {
  2163. struct ata_port *ap = qc->ap;
  2164. struct scsi_cmnd *cmd = qc->scsicmd;
  2165. DPRINTK("ATAPI request sense\n");
  2166. /* FIXME: is this needed? */
  2167. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  2168. #ifdef CONFIG_ATA_SFF
  2169. if (ap->ops->sff_tf_read)
  2170. ap->ops->sff_tf_read(ap, &qc->tf);
  2171. #endif
  2172. /* fill these in, for the case where they are -not- overwritten */
  2173. cmd->sense_buffer[0] = 0x70;
  2174. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  2175. ata_qc_reinit(qc);
  2176. /* setup sg table and init transfer direction */
  2177. sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
  2178. ata_sg_init(qc, &qc->sgent, 1);
  2179. qc->dma_dir = DMA_FROM_DEVICE;
  2180. memset(&qc->cdb, 0, qc->dev->cdb_len);
  2181. qc->cdb[0] = REQUEST_SENSE;
  2182. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2183. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2184. qc->tf.command = ATA_CMD_PACKET;
  2185. if (ata_pio_use_silly(ap)) {
  2186. qc->tf.protocol = ATAPI_PROT_DMA;
  2187. qc->tf.feature |= ATAPI_PKT_DMA;
  2188. } else {
  2189. qc->tf.protocol = ATAPI_PROT_PIO;
  2190. qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
  2191. qc->tf.lbah = 0;
  2192. }
  2193. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  2194. qc->complete_fn = atapi_sense_complete;
  2195. ata_qc_issue(qc);
  2196. DPRINTK("EXIT\n");
  2197. }
  2198. static void atapi_qc_complete(struct ata_queued_cmd *qc)
  2199. {
  2200. struct scsi_cmnd *cmd = qc->scsicmd;
  2201. unsigned int err_mask = qc->err_mask;
  2202. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  2203. /* handle completion from new EH */
  2204. if (unlikely(qc->ap->ops->error_handler &&
  2205. (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
  2206. if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
  2207. /* FIXME: not quite right; we don't want the
  2208. * translation of taskfile registers into a
  2209. * sense descriptors, since that's only
  2210. * correct for ATA, not ATAPI
  2211. */
  2212. ata_gen_passthru_sense(qc);
  2213. }
  2214. /* SCSI EH automatically locks door if sdev->locked is
  2215. * set. Sometimes door lock request continues to
  2216. * fail, for example, when no media is present. This
  2217. * creates a loop - SCSI EH issues door lock which
  2218. * fails and gets invoked again to acquire sense data
  2219. * for the failed command.
  2220. *
  2221. * If door lock fails, always clear sdev->locked to
  2222. * avoid this infinite loop.
  2223. *
  2224. * This may happen before SCSI scan is complete. Make
  2225. * sure qc->dev->sdev isn't NULL before dereferencing.
  2226. */
  2227. if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
  2228. qc->dev->sdev->locked = 0;
  2229. qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
  2230. qc->scsidone(cmd);
  2231. ata_qc_free(qc);
  2232. return;
  2233. }
  2234. /* successful completion or old EH failure path */
  2235. if (unlikely(err_mask & AC_ERR_DEV)) {
  2236. cmd->result = SAM_STAT_CHECK_CONDITION;
  2237. atapi_request_sense(qc);
  2238. return;
  2239. } else if (unlikely(err_mask)) {
  2240. /* FIXME: not quite right; we don't want the
  2241. * translation of taskfile registers into
  2242. * a sense descriptors, since that's only
  2243. * correct for ATA, not ATAPI
  2244. */
  2245. ata_gen_passthru_sense(qc);
  2246. } else {
  2247. u8 *scsicmd = cmd->cmnd;
  2248. if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
  2249. unsigned long flags;
  2250. u8 *buf;
  2251. buf = ata_scsi_rbuf_get(cmd, true, &flags);
  2252. /* ATAPI devices typically report zero for their SCSI version,
  2253. * and sometimes deviate from the spec WRT response data
  2254. * format. If SCSI version is reported as zero like normal,
  2255. * then we make the following fixups: 1) Fake MMC-5 version,
  2256. * to indicate to the Linux scsi midlayer this is a modern
  2257. * device. 2) Ensure response data format / ATAPI information
  2258. * are always correct.
  2259. */
  2260. if (buf[2] == 0) {
  2261. buf[2] = 0x5;
  2262. buf[3] = 0x32;
  2263. }
  2264. ata_scsi_rbuf_put(cmd, true, &flags);
  2265. }
  2266. cmd->result = SAM_STAT_GOOD;
  2267. }
  2268. qc->scsidone(cmd);
  2269. ata_qc_free(qc);
  2270. }
  2271. /**
  2272. * atapi_xlat - Initialize PACKET taskfile
  2273. * @qc: command structure to be initialized
  2274. *
  2275. * LOCKING:
  2276. * spin_lock_irqsave(host lock)
  2277. *
  2278. * RETURNS:
  2279. * Zero on success, non-zero on failure.
  2280. */
  2281. static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
  2282. {
  2283. struct scsi_cmnd *scmd = qc->scsicmd;
  2284. struct ata_device *dev = qc->dev;
  2285. int nodata = (scmd->sc_data_direction == DMA_NONE);
  2286. int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
  2287. unsigned int nbytes;
  2288. memset(qc->cdb, 0, dev->cdb_len);
  2289. memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
  2290. qc->complete_fn = atapi_qc_complete;
  2291. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2292. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  2293. qc->tf.flags |= ATA_TFLAG_WRITE;
  2294. DPRINTK("direction: write\n");
  2295. }
  2296. qc->tf.command = ATA_CMD_PACKET;
  2297. ata_qc_set_pc_nbytes(qc);
  2298. /* check whether ATAPI DMA is safe */
  2299. if (!nodata && !using_pio && atapi_check_dma(qc))
  2300. using_pio = 1;
  2301. /* Some controller variants snoop this value for Packet
  2302. * transfers to do state machine and FIFO management. Thus we
  2303. * want to set it properly, and for DMA where it is
  2304. * effectively meaningless.
  2305. */
  2306. nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
  2307. /* Most ATAPI devices which honor transfer chunk size don't
  2308. * behave according to the spec when odd chunk size which
  2309. * matches the transfer length is specified. If the number of
  2310. * bytes to transfer is 2n+1. According to the spec, what
  2311. * should happen is to indicate that 2n+1 is going to be
  2312. * transferred and transfer 2n+2 bytes where the last byte is
  2313. * padding.
  2314. *
  2315. * In practice, this doesn't happen. ATAPI devices first
  2316. * indicate and transfer 2n bytes and then indicate and
  2317. * transfer 2 bytes where the last byte is padding.
  2318. *
  2319. * This inconsistency confuses several controllers which
  2320. * perform PIO using DMA such as Intel AHCIs and sil3124/32.
  2321. * These controllers use actual number of transferred bytes to
  2322. * update DMA poitner and transfer of 4n+2 bytes make those
  2323. * controller push DMA pointer by 4n+4 bytes because SATA data
  2324. * FISes are aligned to 4 bytes. This causes data corruption
  2325. * and buffer overrun.
  2326. *
  2327. * Always setting nbytes to even number solves this problem
  2328. * because then ATAPI devices don't have to split data at 2n
  2329. * boundaries.
  2330. */
  2331. if (nbytes & 0x1)
  2332. nbytes++;
  2333. qc->tf.lbam = (nbytes & 0xFF);
  2334. qc->tf.lbah = (nbytes >> 8);
  2335. if (nodata)
  2336. qc->tf.protocol = ATAPI_PROT_NODATA;
  2337. else if (using_pio)
  2338. qc->tf.protocol = ATAPI_PROT_PIO;
  2339. else {
  2340. /* DMA data xfer */
  2341. qc->tf.protocol = ATAPI_PROT_DMA;
  2342. qc->tf.feature |= ATAPI_PKT_DMA;
  2343. if ((dev->flags & ATA_DFLAG_DMADIR) &&
  2344. (scmd->sc_data_direction != DMA_TO_DEVICE))
  2345. /* some SATA bridges need us to indicate data xfer direction */
  2346. qc->tf.feature |= ATAPI_DMADIR;
  2347. }
  2348. /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
  2349. as ATAPI tape drives don't get this right otherwise */
  2350. return 0;
  2351. }
  2352. static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
  2353. {
  2354. if (!sata_pmp_attached(ap)) {
  2355. if (likely(devno < ata_link_max_devices(&ap->link)))
  2356. return &ap->link.device[devno];
  2357. } else {
  2358. if (likely(devno < ap->nr_pmp_links))
  2359. return &ap->pmp_link[devno].device[0];
  2360. }
  2361. return NULL;
  2362. }
  2363. static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
  2364. const struct scsi_device *scsidev)
  2365. {
  2366. int devno;
  2367. /* skip commands not addressed to targets we simulate */
  2368. if (!sata_pmp_attached(ap)) {
  2369. if (unlikely(scsidev->channel || scsidev->lun))
  2370. return NULL;
  2371. devno = scsidev->id;
  2372. } else {
  2373. if (unlikely(scsidev->id || scsidev->lun))
  2374. return NULL;
  2375. devno = scsidev->channel;
  2376. }
  2377. return ata_find_dev(ap, devno);
  2378. }
  2379. /**
  2380. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  2381. * @ap: ATA port to which the device is attached
  2382. * @scsidev: SCSI device from which we derive the ATA device
  2383. *
  2384. * Given various information provided in struct scsi_cmnd,
  2385. * map that onto an ATA bus, and using that mapping
  2386. * determine which ata_device is associated with the
  2387. * SCSI command to be sent.
  2388. *
  2389. * LOCKING:
  2390. * spin_lock_irqsave(host lock)
  2391. *
  2392. * RETURNS:
  2393. * Associated ATA device, or %NULL if not found.
  2394. */
  2395. static struct ata_device *
  2396. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  2397. {
  2398. struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
  2399. if (unlikely(!dev || !ata_dev_enabled(dev)))
  2400. return NULL;
  2401. return dev;
  2402. }
  2403. /*
  2404. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  2405. * @byte1: Byte 1 from pass-thru CDB.
  2406. *
  2407. * RETURNS:
  2408. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  2409. */
  2410. static u8
  2411. ata_scsi_map_proto(u8 byte1)
  2412. {
  2413. switch((byte1 & 0x1e) >> 1) {
  2414. case 3: /* Non-data */
  2415. return ATA_PROT_NODATA;
  2416. case 6: /* DMA */
  2417. case 10: /* UDMA Data-in */
  2418. case 11: /* UDMA Data-Out */
  2419. return ATA_PROT_DMA;
  2420. case 4: /* PIO Data-in */
  2421. case 5: /* PIO Data-out */
  2422. return ATA_PROT_PIO;
  2423. case 0: /* Hard Reset */
  2424. case 1: /* SRST */
  2425. case 8: /* Device Diagnostic */
  2426. case 9: /* Device Reset */
  2427. case 7: /* DMA Queued */
  2428. case 12: /* FPDMA */
  2429. case 15: /* Return Response Info */
  2430. default: /* Reserved */
  2431. break;
  2432. }
  2433. return ATA_PROT_UNKNOWN;
  2434. }
  2435. /**
  2436. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  2437. * @qc: command structure to be initialized
  2438. *
  2439. * Handles either 12 or 16-byte versions of the CDB.
  2440. *
  2441. * RETURNS:
  2442. * Zero on success, non-zero on failure.
  2443. */
  2444. static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
  2445. {
  2446. struct ata_taskfile *tf = &(qc->tf);
  2447. struct scsi_cmnd *scmd = qc->scsicmd;
  2448. struct ata_device *dev = qc->dev;
  2449. const u8 *cdb = scmd->cmnd;
  2450. if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
  2451. goto invalid_fld;
  2452. /*
  2453. * 12 and 16 byte CDBs use different offsets to
  2454. * provide the various register values.
  2455. */
  2456. if (cdb[0] == ATA_16) {
  2457. /*
  2458. * 16-byte CDB - may contain extended commands.
  2459. *
  2460. * If that is the case, copy the upper byte register values.
  2461. */
  2462. if (cdb[1] & 0x01) {
  2463. tf->hob_feature = cdb[3];
  2464. tf->hob_nsect = cdb[5];
  2465. tf->hob_lbal = cdb[7];
  2466. tf->hob_lbam = cdb[9];
  2467. tf->hob_lbah = cdb[11];
  2468. tf->flags |= ATA_TFLAG_LBA48;
  2469. } else
  2470. tf->flags &= ~ATA_TFLAG_LBA48;
  2471. /*
  2472. * Always copy low byte, device and command registers.
  2473. */
  2474. tf->feature = cdb[4];
  2475. tf->nsect = cdb[6];
  2476. tf->lbal = cdb[8];
  2477. tf->lbam = cdb[10];
  2478. tf->lbah = cdb[12];
  2479. tf->device = cdb[13];
  2480. tf->command = cdb[14];
  2481. } else {
  2482. /*
  2483. * 12-byte CDB - incapable of extended commands.
  2484. */
  2485. tf->flags &= ~ATA_TFLAG_LBA48;
  2486. tf->feature = cdb[3];
  2487. tf->nsect = cdb[4];
  2488. tf->lbal = cdb[5];
  2489. tf->lbam = cdb[6];
  2490. tf->lbah = cdb[7];
  2491. tf->device = cdb[8];
  2492. tf->command = cdb[9];
  2493. }
  2494. /* enforce correct master/slave bit */
  2495. tf->device = dev->devno ?
  2496. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  2497. switch (tf->command) {
  2498. /* READ/WRITE LONG use a non-standard sect_size */
  2499. case ATA_CMD_READ_LONG:
  2500. case ATA_CMD_READ_LONG_ONCE:
  2501. case ATA_CMD_WRITE_LONG:
  2502. case ATA_CMD_WRITE_LONG_ONCE:
  2503. if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
  2504. goto invalid_fld;
  2505. qc->sect_size = scsi_bufflen(scmd);
  2506. break;
  2507. /* commands using reported Logical Block size (e.g. 512 or 4K) */
  2508. case ATA_CMD_CFA_WRITE_NE:
  2509. case ATA_CMD_CFA_TRANS_SECT:
  2510. case ATA_CMD_CFA_WRITE_MULT_NE:
  2511. /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
  2512. case ATA_CMD_READ:
  2513. case ATA_CMD_READ_EXT:
  2514. case ATA_CMD_READ_QUEUED:
  2515. /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
  2516. case ATA_CMD_FPDMA_READ:
  2517. case ATA_CMD_READ_MULTI:
  2518. case ATA_CMD_READ_MULTI_EXT:
  2519. case ATA_CMD_PIO_READ:
  2520. case ATA_CMD_PIO_READ_EXT:
  2521. case ATA_CMD_READ_STREAM_DMA_EXT:
  2522. case ATA_CMD_READ_STREAM_EXT:
  2523. case ATA_CMD_VERIFY:
  2524. case ATA_CMD_VERIFY_EXT:
  2525. case ATA_CMD_WRITE:
  2526. case ATA_CMD_WRITE_EXT:
  2527. case ATA_CMD_WRITE_FUA_EXT:
  2528. case ATA_CMD_WRITE_QUEUED:
  2529. case ATA_CMD_WRITE_QUEUED_FUA_EXT:
  2530. case ATA_CMD_FPDMA_WRITE:
  2531. case ATA_CMD_WRITE_MULTI:
  2532. case ATA_CMD_WRITE_MULTI_EXT:
  2533. case ATA_CMD_WRITE_MULTI_FUA_EXT:
  2534. case ATA_CMD_PIO_WRITE:
  2535. case ATA_CMD_PIO_WRITE_EXT:
  2536. case ATA_CMD_WRITE_STREAM_DMA_EXT:
  2537. case ATA_CMD_WRITE_STREAM_EXT:
  2538. qc->sect_size = scmd->device->sector_size;
  2539. break;
  2540. /* Everything else uses 512 byte "sectors" */
  2541. default:
  2542. qc->sect_size = ATA_SECT_SIZE;
  2543. }
  2544. /*
  2545. * Set flags so that all registers will be written, pass on
  2546. * write indication (used for PIO/DMA setup), result TF is
  2547. * copied back and we don't whine too much about its failure.
  2548. */
  2549. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  2550. if (scmd->sc_data_direction == DMA_TO_DEVICE)
  2551. tf->flags |= ATA_TFLAG_WRITE;
  2552. qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
  2553. /*
  2554. * Set transfer length.
  2555. *
  2556. * TODO: find out if we need to do more here to
  2557. * cover scatter/gather case.
  2558. */
  2559. ata_qc_set_pc_nbytes(qc);
  2560. /* We may not issue DMA commands if no DMA mode is set */
  2561. if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
  2562. goto invalid_fld;
  2563. /* sanity check for pio multi commands */
  2564. if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
  2565. goto invalid_fld;
  2566. if (is_multi_taskfile(tf)) {
  2567. unsigned int multi_count = 1 << (cdb[1] >> 5);
  2568. /* compare the passed through multi_count
  2569. * with the cached multi_count of libata
  2570. */
  2571. if (multi_count != dev->multi_count)
  2572. ata_dev_printk(dev, KERN_WARNING,
  2573. "invalid multi_count %u ignored\n",
  2574. multi_count);
  2575. }
  2576. /*
  2577. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  2578. * SET_FEATURES - XFER MODE must be preceded/succeeded
  2579. * by an update to hardware-specific registers for each
  2580. * controller (i.e. the reason for ->set_piomode(),
  2581. * ->set_dmamode(), and ->post_set_mode() hooks).
  2582. */
  2583. if (tf->command == ATA_CMD_SET_FEATURES &&
  2584. tf->feature == SETFEATURES_XFER)
  2585. goto invalid_fld;
  2586. /*
  2587. * Filter TPM commands by default. These provide an
  2588. * essentially uncontrolled encrypted "back door" between
  2589. * applications and the disk. Set libata.allow_tpm=1 if you
  2590. * have a real reason for wanting to use them. This ensures
  2591. * that installed software cannot easily mess stuff up without
  2592. * user intent. DVR type users will probably ship with this enabled
  2593. * for movie content management.
  2594. *
  2595. * Note that for ATA8 we can issue a DCS change and DCS freeze lock
  2596. * for this and should do in future but that it is not sufficient as
  2597. * DCS is an optional feature set. Thus we also do the software filter
  2598. * so that we comply with the TC consortium stated goal that the user
  2599. * can turn off TC features of their system.
  2600. */
  2601. if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
  2602. goto invalid_fld;
  2603. return 0;
  2604. invalid_fld:
  2605. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2606. /* "Invalid field in cdb" */
  2607. return 1;
  2608. }
  2609. static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
  2610. {
  2611. struct ata_taskfile *tf = &qc->tf;
  2612. struct scsi_cmnd *scmd = qc->scsicmd;
  2613. struct ata_device *dev = qc->dev;
  2614. const u8 *cdb = scmd->cmnd;
  2615. u64 block;
  2616. u32 n_block;
  2617. u32 size;
  2618. void *buf;
  2619. /* we may not issue DMA commands if no DMA mode is set */
  2620. if (unlikely(!dev->dma_mode))
  2621. goto invalid_fld;
  2622. if (unlikely(scmd->cmd_len < 16))
  2623. goto invalid_fld;
  2624. scsi_16_lba_len(cdb, &block, &n_block);
  2625. /* for now we only support WRITE SAME with the unmap bit set */
  2626. if (unlikely(!(cdb[1] & 0x8)))
  2627. goto invalid_fld;
  2628. /*
  2629. * WRITE SAME always has a sector sized buffer as payload, this
  2630. * should never be a multiple entry S/G list.
  2631. */
  2632. if (!scsi_sg_count(scmd))
  2633. goto invalid_fld;
  2634. buf = page_address(sg_page(scsi_sglist(scmd)));
  2635. size = ata_set_lba_range_entries(buf, 512, block, n_block);
  2636. tf->protocol = ATA_PROT_DMA;
  2637. tf->hob_feature = 0;
  2638. tf->feature = ATA_DSM_TRIM;
  2639. tf->hob_nsect = (size / 512) >> 8;
  2640. tf->nsect = size / 512;
  2641. tf->command = ATA_CMD_DSM;
  2642. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
  2643. ATA_TFLAG_WRITE;
  2644. ata_qc_set_pc_nbytes(qc);
  2645. return 0;
  2646. invalid_fld:
  2647. ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
  2648. /* "Invalid field in cdb" */
  2649. return 1;
  2650. }
  2651. /**
  2652. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  2653. * @dev: ATA device
  2654. * @cmd: SCSI command opcode to consider
  2655. *
  2656. * Look up the SCSI command given, and determine whether the
  2657. * SCSI command is to be translated or simulated.
  2658. *
  2659. * RETURNS:
  2660. * Pointer to translation function if possible, %NULL if not.
  2661. */
  2662. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  2663. {
  2664. switch (cmd) {
  2665. case READ_6:
  2666. case READ_10:
  2667. case READ_16:
  2668. case WRITE_6:
  2669. case WRITE_10:
  2670. case WRITE_16:
  2671. return ata_scsi_rw_xlat;
  2672. case WRITE_SAME_16:
  2673. return ata_scsi_write_same_xlat;
  2674. case SYNCHRONIZE_CACHE:
  2675. if (ata_try_flush_cache(dev))
  2676. return ata_scsi_flush_xlat;
  2677. break;
  2678. case VERIFY:
  2679. case VERIFY_16:
  2680. return ata_scsi_verify_xlat;
  2681. case ATA_12:
  2682. case ATA_16:
  2683. return ata_scsi_pass_thru;
  2684. case START_STOP:
  2685. return ata_scsi_start_stop_xlat;
  2686. }
  2687. return NULL;
  2688. }
  2689. /**
  2690. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2691. * @ap: ATA port to which the command was being sent
  2692. * @cmd: SCSI command to dump
  2693. *
  2694. * Prints the contents of a SCSI command via printk().
  2695. */
  2696. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2697. struct scsi_cmnd *cmd)
  2698. {
  2699. #ifdef ATA_DEBUG
  2700. struct scsi_device *scsidev = cmd->device;
  2701. u8 *scsicmd = cmd->cmnd;
  2702. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2703. ap->print_id,
  2704. scsidev->channel, scsidev->id, scsidev->lun,
  2705. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2706. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2707. scsicmd[8]);
  2708. #endif
  2709. }
  2710. static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
  2711. struct ata_device *dev)
  2712. {
  2713. u8 scsi_op = scmd->cmnd[0];
  2714. ata_xlat_func_t xlat_func;
  2715. int rc = 0;
  2716. if (dev->class == ATA_DEV_ATA) {
  2717. if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
  2718. goto bad_cdb_len;
  2719. xlat_func = ata_get_xlat_func(dev, scsi_op);
  2720. } else {
  2721. if (unlikely(!scmd->cmd_len))
  2722. goto bad_cdb_len;
  2723. xlat_func = NULL;
  2724. if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
  2725. /* relay SCSI command to ATAPI device */
  2726. int len = COMMAND_SIZE(scsi_op);
  2727. if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
  2728. goto bad_cdb_len;
  2729. xlat_func = atapi_xlat;
  2730. } else {
  2731. /* ATA_16 passthru, treat as an ATA command */
  2732. if (unlikely(scmd->cmd_len > 16))
  2733. goto bad_cdb_len;
  2734. xlat_func = ata_get_xlat_func(dev, scsi_op);
  2735. }
  2736. }
  2737. if (xlat_func)
  2738. rc = ata_scsi_translate(dev, scmd, xlat_func);
  2739. else
  2740. ata_scsi_simulate(dev, scmd);
  2741. return rc;
  2742. bad_cdb_len:
  2743. DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
  2744. scmd->cmd_len, scsi_op, dev->cdb_len);
  2745. scmd->result = DID_ERROR << 16;
  2746. scmd->scsi_done(scmd);
  2747. return 0;
  2748. }
  2749. /**
  2750. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  2751. * @shost: SCSI host of command to be sent
  2752. * @cmd: SCSI command to be sent
  2753. *
  2754. * In some cases, this function translates SCSI commands into
  2755. * ATA taskfiles, and queues the taskfiles to be sent to
  2756. * hardware. In other cases, this function simulates a
  2757. * SCSI device by evaluating and responding to certain
  2758. * SCSI commands. This creates the overall effect of
  2759. * ATA and ATAPI devices appearing as SCSI devices.
  2760. *
  2761. * LOCKING:
  2762. * ATA host lock
  2763. *
  2764. * RETURNS:
  2765. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  2766. * 0 otherwise.
  2767. */
  2768. int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  2769. {
  2770. struct ata_port *ap;
  2771. struct ata_device *dev;
  2772. struct scsi_device *scsidev = cmd->device;
  2773. int rc = 0;
  2774. unsigned long irq_flags;
  2775. ap = ata_shost_to_port(shost);
  2776. spin_lock_irqsave(ap->lock, irq_flags);
  2777. ata_scsi_dump_cdb(ap, cmd);
  2778. dev = ata_scsi_find_dev(ap, scsidev);
  2779. if (likely(dev))
  2780. rc = __ata_scsi_queuecmd(cmd, dev);
  2781. else {
  2782. cmd->result = (DID_BAD_TARGET << 16);
  2783. cmd->scsi_done(cmd);
  2784. }
  2785. spin_unlock_irqrestore(ap->lock, irq_flags);
  2786. return rc;
  2787. }
  2788. /**
  2789. * ata_scsi_simulate - simulate SCSI command on ATA device
  2790. * @dev: the target device
  2791. * @cmd: SCSI command being sent to device.
  2792. *
  2793. * Interprets and directly executes a select list of SCSI commands
  2794. * that can be handled internally.
  2795. *
  2796. * LOCKING:
  2797. * spin_lock_irqsave(host lock)
  2798. */
  2799. void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
  2800. {
  2801. struct ata_scsi_args args;
  2802. const u8 *scsicmd = cmd->cmnd;
  2803. u8 tmp8;
  2804. args.dev = dev;
  2805. args.id = dev->id;
  2806. args.cmd = cmd;
  2807. args.done = cmd->scsi_done;
  2808. switch(scsicmd[0]) {
  2809. /* TODO: worth improving? */
  2810. case FORMAT_UNIT:
  2811. ata_scsi_invalid_field(cmd);
  2812. break;
  2813. case INQUIRY:
  2814. if (scsicmd[1] & 2) /* is CmdDt set? */
  2815. ata_scsi_invalid_field(cmd);
  2816. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  2817. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  2818. else switch (scsicmd[2]) {
  2819. case 0x00:
  2820. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  2821. break;
  2822. case 0x80:
  2823. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  2824. break;
  2825. case 0x83:
  2826. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  2827. break;
  2828. case 0x89:
  2829. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
  2830. break;
  2831. case 0xb0:
  2832. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
  2833. break;
  2834. case 0xb1:
  2835. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
  2836. break;
  2837. case 0xb2:
  2838. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
  2839. break;
  2840. default:
  2841. ata_scsi_invalid_field(cmd);
  2842. break;
  2843. }
  2844. break;
  2845. case MODE_SENSE:
  2846. case MODE_SENSE_10:
  2847. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  2848. break;
  2849. case MODE_SELECT: /* unconditionally return */
  2850. case MODE_SELECT_10: /* bad-field-in-cdb */
  2851. ata_scsi_invalid_field(cmd);
  2852. break;
  2853. case READ_CAPACITY:
  2854. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2855. break;
  2856. case SERVICE_ACTION_IN:
  2857. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  2858. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2859. else
  2860. ata_scsi_invalid_field(cmd);
  2861. break;
  2862. case REPORT_LUNS:
  2863. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  2864. break;
  2865. case REQUEST_SENSE:
  2866. ata_scsi_set_sense(cmd, 0, 0, 0);
  2867. cmd->result = (DRIVER_SENSE << 24);
  2868. cmd->scsi_done(cmd);
  2869. break;
  2870. /* if we reach this, then writeback caching is disabled,
  2871. * turning this into a no-op.
  2872. */
  2873. case SYNCHRONIZE_CACHE:
  2874. /* fall through */
  2875. /* no-op's, complete with success */
  2876. case REZERO_UNIT:
  2877. case SEEK_6:
  2878. case SEEK_10:
  2879. case TEST_UNIT_READY:
  2880. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2881. break;
  2882. case SEND_DIAGNOSTIC:
  2883. tmp8 = scsicmd[1] & ~(1 << 3);
  2884. if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
  2885. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2886. else
  2887. ata_scsi_invalid_field(cmd);
  2888. break;
  2889. /* all other commands */
  2890. default:
  2891. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  2892. /* "Invalid command operation code" */
  2893. cmd->scsi_done(cmd);
  2894. break;
  2895. }
  2896. }
  2897. int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
  2898. {
  2899. int i, rc;
  2900. for (i = 0; i < host->n_ports; i++) {
  2901. struct ata_port *ap = host->ports[i];
  2902. struct Scsi_Host *shost;
  2903. rc = -ENOMEM;
  2904. shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
  2905. if (!shost)
  2906. goto err_alloc;
  2907. *(struct ata_port **)&shost->hostdata[0] = ap;
  2908. ap->scsi_host = shost;
  2909. shost->transportt = ata_scsi_transport_template;
  2910. shost->unique_id = ap->print_id;
  2911. shost->max_id = 16;
  2912. shost->max_lun = 1;
  2913. shost->max_channel = 1;
  2914. shost->max_cmd_len = 16;
  2915. /* Schedule policy is determined by ->qc_defer()
  2916. * callback and it needs to see every deferred qc.
  2917. * Set host_blocked to 1 to prevent SCSI midlayer from
  2918. * automatically deferring requests.
  2919. */
  2920. shost->max_host_blocked = 1;
  2921. rc = scsi_add_host(ap->scsi_host, ap->host->dev);
  2922. if (rc)
  2923. goto err_add;
  2924. }
  2925. return 0;
  2926. err_add:
  2927. scsi_host_put(host->ports[i]->scsi_host);
  2928. err_alloc:
  2929. while (--i >= 0) {
  2930. struct Scsi_Host *shost = host->ports[i]->scsi_host;
  2931. scsi_remove_host(shost);
  2932. scsi_host_put(shost);
  2933. }
  2934. return rc;
  2935. }
  2936. void ata_scsi_scan_host(struct ata_port *ap, int sync)
  2937. {
  2938. int tries = 5;
  2939. struct ata_device *last_failed_dev = NULL;
  2940. struct ata_link *link;
  2941. struct ata_device *dev;
  2942. repeat:
  2943. ata_for_each_link(link, ap, EDGE) {
  2944. ata_for_each_dev(dev, link, ENABLED) {
  2945. struct scsi_device *sdev;
  2946. int channel = 0, id = 0;
  2947. if (dev->sdev)
  2948. continue;
  2949. if (ata_is_host_link(link))
  2950. id = dev->devno;
  2951. else
  2952. channel = link->pmp;
  2953. sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
  2954. NULL);
  2955. if (!IS_ERR(sdev)) {
  2956. dev->sdev = sdev;
  2957. scsi_device_put(sdev);
  2958. } else {
  2959. dev->sdev = NULL;
  2960. }
  2961. }
  2962. }
  2963. /* If we scanned while EH was in progress or allocation
  2964. * failure occurred, scan would have failed silently. Check
  2965. * whether all devices are attached.
  2966. */
  2967. ata_for_each_link(link, ap, EDGE) {
  2968. ata_for_each_dev(dev, link, ENABLED) {
  2969. if (!dev->sdev)
  2970. goto exit_loop;
  2971. }
  2972. }
  2973. exit_loop:
  2974. if (!link)
  2975. return;
  2976. /* we're missing some SCSI devices */
  2977. if (sync) {
  2978. /* If caller requested synchrnous scan && we've made
  2979. * any progress, sleep briefly and repeat.
  2980. */
  2981. if (dev != last_failed_dev) {
  2982. msleep(100);
  2983. last_failed_dev = dev;
  2984. goto repeat;
  2985. }
  2986. /* We might be failing to detect boot device, give it
  2987. * a few more chances.
  2988. */
  2989. if (--tries) {
  2990. msleep(100);
  2991. goto repeat;
  2992. }
  2993. ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "
  2994. "failed without making any progress,\n"
  2995. " switching to async\n");
  2996. }
  2997. queue_delayed_work(system_long_wq, &ap->hotplug_task,
  2998. round_jiffies_relative(HZ));
  2999. }
  3000. /**
  3001. * ata_scsi_offline_dev - offline attached SCSI device
  3002. * @dev: ATA device to offline attached SCSI device for
  3003. *
  3004. * This function is called from ata_eh_hotplug() and responsible
  3005. * for taking the SCSI device attached to @dev offline. This
  3006. * function is called with host lock which protects dev->sdev
  3007. * against clearing.
  3008. *
  3009. * LOCKING:
  3010. * spin_lock_irqsave(host lock)
  3011. *
  3012. * RETURNS:
  3013. * 1 if attached SCSI device exists, 0 otherwise.
  3014. */
  3015. int ata_scsi_offline_dev(struct ata_device *dev)
  3016. {
  3017. if (dev->sdev) {
  3018. scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
  3019. return 1;
  3020. }
  3021. return 0;
  3022. }
  3023. /**
  3024. * ata_scsi_remove_dev - remove attached SCSI device
  3025. * @dev: ATA device to remove attached SCSI device for
  3026. *
  3027. * This function is called from ata_eh_scsi_hotplug() and
  3028. * responsible for removing the SCSI device attached to @dev.
  3029. *
  3030. * LOCKING:
  3031. * Kernel thread context (may sleep).
  3032. */
  3033. static void ata_scsi_remove_dev(struct ata_device *dev)
  3034. {
  3035. struct ata_port *ap = dev->link->ap;
  3036. struct scsi_device *sdev;
  3037. unsigned long flags;
  3038. /* Alas, we need to grab scan_mutex to ensure SCSI device
  3039. * state doesn't change underneath us and thus
  3040. * scsi_device_get() always succeeds. The mutex locking can
  3041. * be removed if there is __scsi_device_get() interface which
  3042. * increments reference counts regardless of device state.
  3043. */
  3044. mutex_lock(&ap->scsi_host->scan_mutex);
  3045. spin_lock_irqsave(ap->lock, flags);
  3046. /* clearing dev->sdev is protected by host lock */
  3047. sdev = dev->sdev;
  3048. dev->sdev = NULL;
  3049. if (sdev) {
  3050. /* If user initiated unplug races with us, sdev can go
  3051. * away underneath us after the host lock and
  3052. * scan_mutex are released. Hold onto it.
  3053. */
  3054. if (scsi_device_get(sdev) == 0) {
  3055. /* The following ensures the attached sdev is
  3056. * offline on return from ata_scsi_offline_dev()
  3057. * regardless it wins or loses the race
  3058. * against this function.
  3059. */
  3060. scsi_device_set_state(sdev, SDEV_OFFLINE);
  3061. } else {
  3062. WARN_ON(1);
  3063. sdev = NULL;
  3064. }
  3065. }
  3066. spin_unlock_irqrestore(ap->lock, flags);
  3067. mutex_unlock(&ap->scsi_host->scan_mutex);
  3068. if (sdev) {
  3069. ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
  3070. dev_name(&sdev->sdev_gendev));
  3071. scsi_remove_device(sdev);
  3072. scsi_device_put(sdev);
  3073. }
  3074. }
  3075. static void ata_scsi_handle_link_detach(struct ata_link *link)
  3076. {
  3077. struct ata_port *ap = link->ap;
  3078. struct ata_device *dev;
  3079. ata_for_each_dev(dev, link, ALL) {
  3080. unsigned long flags;
  3081. if (!(dev->flags & ATA_DFLAG_DETACHED))
  3082. continue;
  3083. spin_lock_irqsave(ap->lock, flags);
  3084. dev->flags &= ~ATA_DFLAG_DETACHED;
  3085. spin_unlock_irqrestore(ap->lock, flags);
  3086. ata_scsi_remove_dev(dev);
  3087. }
  3088. }
  3089. /**
  3090. * ata_scsi_media_change_notify - send media change event
  3091. * @dev: Pointer to the disk device with media change event
  3092. *
  3093. * Tell the block layer to send a media change notification
  3094. * event.
  3095. *
  3096. * LOCKING:
  3097. * spin_lock_irqsave(host lock)
  3098. */
  3099. void ata_scsi_media_change_notify(struct ata_device *dev)
  3100. {
  3101. if (dev->sdev)
  3102. sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
  3103. GFP_ATOMIC);
  3104. }
  3105. /**
  3106. * ata_scsi_hotplug - SCSI part of hotplug
  3107. * @work: Pointer to ATA port to perform SCSI hotplug on
  3108. *
  3109. * Perform SCSI part of hotplug. It's executed from a separate
  3110. * workqueue after EH completes. This is necessary because SCSI
  3111. * hot plugging requires working EH and hot unplugging is
  3112. * synchronized with hot plugging with a mutex.
  3113. *
  3114. * LOCKING:
  3115. * Kernel thread context (may sleep).
  3116. */
  3117. void ata_scsi_hotplug(struct work_struct *work)
  3118. {
  3119. struct ata_port *ap =
  3120. container_of(work, struct ata_port, hotplug_task.work);
  3121. int i;
  3122. if (ap->pflags & ATA_PFLAG_UNLOADING) {
  3123. DPRINTK("ENTER/EXIT - unloading\n");
  3124. return;
  3125. }
  3126. DPRINTK("ENTER\n");
  3127. mutex_lock(&ap->scsi_scan_mutex);
  3128. /* Unplug detached devices. We cannot use link iterator here
  3129. * because PMP links have to be scanned even if PMP is
  3130. * currently not attached. Iterate manually.
  3131. */
  3132. ata_scsi_handle_link_detach(&ap->link);
  3133. if (ap->pmp_link)
  3134. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  3135. ata_scsi_handle_link_detach(&ap->pmp_link[i]);
  3136. /* scan for new ones */
  3137. ata_scsi_scan_host(ap, 0);
  3138. mutex_unlock(&ap->scsi_scan_mutex);
  3139. DPRINTK("EXIT\n");
  3140. }
  3141. /**
  3142. * ata_scsi_user_scan - indication for user-initiated bus scan
  3143. * @shost: SCSI host to scan
  3144. * @channel: Channel to scan
  3145. * @id: ID to scan
  3146. * @lun: LUN to scan
  3147. *
  3148. * This function is called when user explicitly requests bus
  3149. * scan. Set probe pending flag and invoke EH.
  3150. *
  3151. * LOCKING:
  3152. * SCSI layer (we don't care)
  3153. *
  3154. * RETURNS:
  3155. * Zero.
  3156. */
  3157. int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
  3158. unsigned int id, unsigned int lun)
  3159. {
  3160. struct ata_port *ap = ata_shost_to_port(shost);
  3161. unsigned long flags;
  3162. int devno, rc = 0;
  3163. if (!ap->ops->error_handler)
  3164. return -EOPNOTSUPP;
  3165. if (lun != SCAN_WILD_CARD && lun)
  3166. return -EINVAL;
  3167. if (!sata_pmp_attached(ap)) {
  3168. if (channel != SCAN_WILD_CARD && channel)
  3169. return -EINVAL;
  3170. devno = id;
  3171. } else {
  3172. if (id != SCAN_WILD_CARD && id)
  3173. return -EINVAL;
  3174. devno = channel;
  3175. }
  3176. spin_lock_irqsave(ap->lock, flags);
  3177. if (devno == SCAN_WILD_CARD) {
  3178. struct ata_link *link;
  3179. ata_for_each_link(link, ap, EDGE) {
  3180. struct ata_eh_info *ehi = &link->eh_info;
  3181. ehi->probe_mask |= ATA_ALL_DEVICES;
  3182. ehi->action |= ATA_EH_RESET;
  3183. }
  3184. } else {
  3185. struct ata_device *dev = ata_find_dev(ap, devno);
  3186. if (dev) {
  3187. struct ata_eh_info *ehi = &dev->link->eh_info;
  3188. ehi->probe_mask |= 1 << dev->devno;
  3189. ehi->action |= ATA_EH_RESET;
  3190. } else
  3191. rc = -EINVAL;
  3192. }
  3193. if (rc == 0) {
  3194. ata_port_schedule_eh(ap);
  3195. spin_unlock_irqrestore(ap->lock, flags);
  3196. ata_port_wait_eh(ap);
  3197. } else
  3198. spin_unlock_irqrestore(ap->lock, flags);
  3199. return rc;
  3200. }
  3201. /**
  3202. * ata_scsi_dev_rescan - initiate scsi_rescan_device()
  3203. * @work: Pointer to ATA port to perform scsi_rescan_device()
  3204. *
  3205. * After ATA pass thru (SAT) commands are executed successfully,
  3206. * libata need to propagate the changes to SCSI layer.
  3207. *
  3208. * LOCKING:
  3209. * Kernel thread context (may sleep).
  3210. */
  3211. void ata_scsi_dev_rescan(struct work_struct *work)
  3212. {
  3213. struct ata_port *ap =
  3214. container_of(work, struct ata_port, scsi_rescan_task);
  3215. struct ata_link *link;
  3216. struct ata_device *dev;
  3217. unsigned long flags;
  3218. mutex_lock(&ap->scsi_scan_mutex);
  3219. spin_lock_irqsave(ap->lock, flags);
  3220. ata_for_each_link(link, ap, EDGE) {
  3221. ata_for_each_dev(dev, link, ENABLED) {
  3222. struct scsi_device *sdev = dev->sdev;
  3223. if (!sdev)
  3224. continue;
  3225. if (scsi_device_get(sdev))
  3226. continue;
  3227. spin_unlock_irqrestore(ap->lock, flags);
  3228. scsi_rescan_device(&(sdev->sdev_gendev));
  3229. scsi_device_put(sdev);
  3230. spin_lock_irqsave(ap->lock, flags);
  3231. }
  3232. }
  3233. spin_unlock_irqrestore(ap->lock, flags);
  3234. mutex_unlock(&ap->scsi_scan_mutex);
  3235. }
  3236. /**
  3237. * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
  3238. * @host: ATA host container for all SAS ports
  3239. * @port_info: Information from low-level host driver
  3240. * @shost: SCSI host that the scsi device is attached to
  3241. *
  3242. * LOCKING:
  3243. * PCI/etc. bus probe sem.
  3244. *
  3245. * RETURNS:
  3246. * ata_port pointer on success / NULL on failure.
  3247. */
  3248. struct ata_port *ata_sas_port_alloc(struct ata_host *host,
  3249. struct ata_port_info *port_info,
  3250. struct Scsi_Host *shost)
  3251. {
  3252. struct ata_port *ap;
  3253. ap = ata_port_alloc(host);
  3254. if (!ap)
  3255. return NULL;
  3256. ap->port_no = 0;
  3257. ap->lock = &host->lock;
  3258. ap->pio_mask = port_info->pio_mask;
  3259. ap->mwdma_mask = port_info->mwdma_mask;
  3260. ap->udma_mask = port_info->udma_mask;
  3261. ap->flags |= port_info->flags;
  3262. ap->ops = port_info->port_ops;
  3263. ap->cbl = ATA_CBL_SATA;
  3264. return ap;
  3265. }
  3266. EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  3267. /**
  3268. * ata_sas_port_start - Set port up for dma.
  3269. * @ap: Port to initialize
  3270. *
  3271. * Called just after data structures for each port are
  3272. * initialized.
  3273. *
  3274. * May be used as the port_start() entry in ata_port_operations.
  3275. *
  3276. * LOCKING:
  3277. * Inherited from caller.
  3278. */
  3279. int ata_sas_port_start(struct ata_port *ap)
  3280. {
  3281. /*
  3282. * the port is marked as frozen at allocation time, but if we don't
  3283. * have new eh, we won't thaw it
  3284. */
  3285. if (!ap->ops->error_handler)
  3286. ap->pflags &= ~ATA_PFLAG_FROZEN;
  3287. return 0;
  3288. }
  3289. EXPORT_SYMBOL_GPL(ata_sas_port_start);
  3290. /**
  3291. * ata_port_stop - Undo ata_sas_port_start()
  3292. * @ap: Port to shut down
  3293. *
  3294. * May be used as the port_stop() entry in ata_port_operations.
  3295. *
  3296. * LOCKING:
  3297. * Inherited from caller.
  3298. */
  3299. void ata_sas_port_stop(struct ata_port *ap)
  3300. {
  3301. }
  3302. EXPORT_SYMBOL_GPL(ata_sas_port_stop);
  3303. /**
  3304. * ata_sas_port_init - Initialize a SATA device
  3305. * @ap: SATA port to initialize
  3306. *
  3307. * LOCKING:
  3308. * PCI/etc. bus probe sem.
  3309. *
  3310. * RETURNS:
  3311. * Zero on success, non-zero on error.
  3312. */
  3313. int ata_sas_port_init(struct ata_port *ap)
  3314. {
  3315. int rc = ap->ops->port_start(ap);
  3316. if (!rc) {
  3317. ap->print_id = ata_print_id++;
  3318. rc = ata_port_probe(ap);
  3319. }
  3320. return rc;
  3321. }
  3322. EXPORT_SYMBOL_GPL(ata_sas_port_init);
  3323. /**
  3324. * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
  3325. * @ap: SATA port to destroy
  3326. *
  3327. */
  3328. void ata_sas_port_destroy(struct ata_port *ap)
  3329. {
  3330. if (ap->ops->port_stop)
  3331. ap->ops->port_stop(ap);
  3332. kfree(ap);
  3333. }
  3334. EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
  3335. /**
  3336. * ata_sas_slave_configure - Default slave_config routine for libata devices
  3337. * @sdev: SCSI device to configure
  3338. * @ap: ATA port to which SCSI device is attached
  3339. *
  3340. * RETURNS:
  3341. * Zero.
  3342. */
  3343. int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
  3344. {
  3345. ata_scsi_sdev_config(sdev);
  3346. ata_scsi_dev_config(sdev, ap->link.device);
  3347. return 0;
  3348. }
  3349. EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
  3350. /**
  3351. * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
  3352. * @cmd: SCSI command to be sent
  3353. * @ap: ATA port to which the command is being sent
  3354. *
  3355. * RETURNS:
  3356. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  3357. * 0 otherwise.
  3358. */
  3359. int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
  3360. {
  3361. int rc = 0;
  3362. ata_scsi_dump_cdb(ap, cmd);
  3363. if (likely(ata_dev_enabled(ap->link.device)))
  3364. rc = __ata_scsi_queuecmd(cmd, ap->link.device);
  3365. else {
  3366. cmd->result = (DID_BAD_TARGET << 16);
  3367. cmd->scsi_done(cmd);
  3368. }
  3369. return rc;
  3370. }
  3371. EXPORT_SYMBOL_GPL(ata_sas_queuecmd);