libata-scsi.c 94 KB

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