libata-scsi.c 96 KB

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