libata-scsi.c 91 KB

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