libata-scsi.c 94 KB

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