sd.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440
  1. /*
  2. * sd.c Copyright (C) 1992 Drew Eckhardt
  3. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  4. *
  5. * Linux scsi disk driver
  6. * Initial versions: Drew Eckhardt
  7. * Subsequent revisions: Eric Youngdale
  8. * Modification history:
  9. * - Drew Eckhardt <drew@colorado.edu> original
  10. * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
  11. * outstanding request, and other enhancements.
  12. * Support loadable low-level scsi drivers.
  13. * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
  14. * eight major numbers.
  15. * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
  16. * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
  17. * sd_init and cleanups.
  18. * - Alex Davis <letmein@erols.com> Fix problem where partition info
  19. * not being read in sd_open. Fix problem where removable media
  20. * could be ejected after sd_open.
  21. * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
  22. * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
  23. * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
  24. * Support 32k/1M disks.
  25. *
  26. * Logging policy (needs CONFIG_SCSI_LOGGING defined):
  27. * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
  28. * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
  29. * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
  30. * - entering other commands: SCSI_LOG_HLQUEUE level 3
  31. * Note: when the logging level is set by the user, it must be greater
  32. * than the level indicated above to trigger output.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/fs.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mm.h>
  38. #include <linux/bio.h>
  39. #include <linux/genhd.h>
  40. #include <linux/hdreg.h>
  41. #include <linux/errno.h>
  42. #include <linux/idr.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/init.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/blkpg.h>
  47. #include <linux/delay.h>
  48. #include <linux/mutex.h>
  49. #include <linux/string_helpers.h>
  50. #include <linux/async.h>
  51. #include <linux/slab.h>
  52. #include <linux/pm_runtime.h>
  53. #include <linux/pr.h>
  54. #include <linux/t10-pi.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/unaligned.h>
  57. #include <scsi/scsi.h>
  58. #include <scsi/scsi_cmnd.h>
  59. #include <scsi/scsi_dbg.h>
  60. #include <scsi/scsi_device.h>
  61. #include <scsi/scsi_driver.h>
  62. #include <scsi/scsi_eh.h>
  63. #include <scsi/scsi_host.h>
  64. #include <scsi/scsi_ioctl.h>
  65. #include <scsi/scsicam.h>
  66. #include "sd.h"
  67. #include "scsi_priv.h"
  68. #include "scsi_logging.h"
  69. MODULE_AUTHOR("Eric Youngdale");
  70. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  71. MODULE_LICENSE("GPL");
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  80. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  81. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  82. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  83. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  84. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  85. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  86. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  87. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  88. MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
  89. MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
  90. MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
  91. #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
  92. #define SD_MINORS 16
  93. #else
  94. #define SD_MINORS 0
  95. #endif
  96. static void sd_config_discard(struct scsi_disk *, unsigned int);
  97. static void sd_config_write_same(struct scsi_disk *);
  98. static int sd_revalidate_disk(struct gendisk *);
  99. static void sd_unlock_native_capacity(struct gendisk *disk);
  100. static int sd_probe(struct device *);
  101. static int sd_remove(struct device *);
  102. static void sd_shutdown(struct device *);
  103. static int sd_suspend_system(struct device *);
  104. static int sd_suspend_runtime(struct device *);
  105. static int sd_resume(struct device *);
  106. static void sd_rescan(struct device *);
  107. static int sd_init_command(struct scsi_cmnd *SCpnt);
  108. static void sd_uninit_command(struct scsi_cmnd *SCpnt);
  109. static int sd_done(struct scsi_cmnd *);
  110. static int sd_eh_action(struct scsi_cmnd *, int);
  111. static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
  112. static void scsi_disk_release(struct device *cdev);
  113. static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
  114. static void sd_print_result(const struct scsi_disk *, const char *, int);
  115. static DEFINE_SPINLOCK(sd_index_lock);
  116. static DEFINE_IDA(sd_index_ida);
  117. /* This semaphore is used to mediate the 0->1 reference get in the
  118. * face of object destruction (i.e. we can't allow a get on an
  119. * object after last put) */
  120. static DEFINE_MUTEX(sd_ref_mutex);
  121. static struct kmem_cache *sd_cdb_cache;
  122. static mempool_t *sd_cdb_pool;
  123. static const char *sd_cache_types[] = {
  124. "write through", "none", "write back",
  125. "write back, no read (daft)"
  126. };
  127. static void sd_set_flush_flag(struct scsi_disk *sdkp)
  128. {
  129. bool wc = false, fua = false;
  130. if (sdkp->WCE) {
  131. wc = true;
  132. if (sdkp->DPOFUA)
  133. fua = true;
  134. }
  135. blk_queue_write_cache(sdkp->disk->queue, wc, fua);
  136. }
  137. static ssize_t
  138. cache_type_store(struct device *dev, struct device_attribute *attr,
  139. const char *buf, size_t count)
  140. {
  141. int i, ct = -1, rcd, wce, sp;
  142. struct scsi_disk *sdkp = to_scsi_disk(dev);
  143. struct scsi_device *sdp = sdkp->device;
  144. char buffer[64];
  145. char *buffer_data;
  146. struct scsi_mode_data data;
  147. struct scsi_sense_hdr sshdr;
  148. static const char temp[] = "temporary ";
  149. int len;
  150. if (sdp->type != TYPE_DISK)
  151. /* no cache control on RBC devices; theoretically they
  152. * can do it, but there's probably so many exceptions
  153. * it's not worth the risk */
  154. return -EINVAL;
  155. if (strncmp(buf, temp, sizeof(temp) - 1) == 0) {
  156. buf += sizeof(temp) - 1;
  157. sdkp->cache_override = 1;
  158. } else {
  159. sdkp->cache_override = 0;
  160. }
  161. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  162. len = strlen(sd_cache_types[i]);
  163. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  164. buf[len] == '\n') {
  165. ct = i;
  166. break;
  167. }
  168. }
  169. if (ct < 0)
  170. return -EINVAL;
  171. rcd = ct & 0x01 ? 1 : 0;
  172. wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0;
  173. if (sdkp->cache_override) {
  174. sdkp->WCE = wce;
  175. sdkp->RCD = rcd;
  176. sd_set_flush_flag(sdkp);
  177. return count;
  178. }
  179. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  180. SD_MAX_RETRIES, &data, NULL))
  181. return -EINVAL;
  182. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  183. data.block_descriptor_length);
  184. buffer_data = buffer + data.header_length +
  185. data.block_descriptor_length;
  186. buffer_data[2] &= ~0x05;
  187. buffer_data[2] |= wce << 2 | rcd;
  188. sp = buffer_data[0] & 0x80 ? 1 : 0;
  189. buffer_data[0] &= ~0x80;
  190. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  191. SD_MAX_RETRIES, &data, &sshdr)) {
  192. if (scsi_sense_valid(&sshdr))
  193. sd_print_sense_hdr(sdkp, &sshdr);
  194. return -EINVAL;
  195. }
  196. revalidate_disk(sdkp->disk);
  197. return count;
  198. }
  199. static ssize_t
  200. manage_start_stop_show(struct device *dev, struct device_attribute *attr,
  201. char *buf)
  202. {
  203. struct scsi_disk *sdkp = to_scsi_disk(dev);
  204. struct scsi_device *sdp = sdkp->device;
  205. return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
  206. }
  207. static ssize_t
  208. manage_start_stop_store(struct device *dev, struct device_attribute *attr,
  209. const char *buf, size_t count)
  210. {
  211. struct scsi_disk *sdkp = to_scsi_disk(dev);
  212. struct scsi_device *sdp = sdkp->device;
  213. if (!capable(CAP_SYS_ADMIN))
  214. return -EACCES;
  215. sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
  216. return count;
  217. }
  218. static DEVICE_ATTR_RW(manage_start_stop);
  219. static ssize_t
  220. allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf)
  221. {
  222. struct scsi_disk *sdkp = to_scsi_disk(dev);
  223. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  224. }
  225. static ssize_t
  226. allow_restart_store(struct device *dev, struct device_attribute *attr,
  227. const char *buf, size_t count)
  228. {
  229. struct scsi_disk *sdkp = to_scsi_disk(dev);
  230. struct scsi_device *sdp = sdkp->device;
  231. if (!capable(CAP_SYS_ADMIN))
  232. return -EACCES;
  233. if (sdp->type != TYPE_DISK)
  234. return -EINVAL;
  235. sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  236. return count;
  237. }
  238. static DEVICE_ATTR_RW(allow_restart);
  239. static ssize_t
  240. cache_type_show(struct device *dev, struct device_attribute *attr, char *buf)
  241. {
  242. struct scsi_disk *sdkp = to_scsi_disk(dev);
  243. int ct = sdkp->RCD + 2*sdkp->WCE;
  244. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  245. }
  246. static DEVICE_ATTR_RW(cache_type);
  247. static ssize_t
  248. FUA_show(struct device *dev, struct device_attribute *attr, char *buf)
  249. {
  250. struct scsi_disk *sdkp = to_scsi_disk(dev);
  251. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  252. }
  253. static DEVICE_ATTR_RO(FUA);
  254. static ssize_t
  255. protection_type_show(struct device *dev, struct device_attribute *attr,
  256. char *buf)
  257. {
  258. struct scsi_disk *sdkp = to_scsi_disk(dev);
  259. return snprintf(buf, 20, "%u\n", sdkp->protection_type);
  260. }
  261. static ssize_t
  262. protection_type_store(struct device *dev, struct device_attribute *attr,
  263. const char *buf, size_t count)
  264. {
  265. struct scsi_disk *sdkp = to_scsi_disk(dev);
  266. unsigned int val;
  267. int err;
  268. if (!capable(CAP_SYS_ADMIN))
  269. return -EACCES;
  270. err = kstrtouint(buf, 10, &val);
  271. if (err)
  272. return err;
  273. if (val >= 0 && val <= T10_PI_TYPE3_PROTECTION)
  274. sdkp->protection_type = val;
  275. return count;
  276. }
  277. static DEVICE_ATTR_RW(protection_type);
  278. static ssize_t
  279. protection_mode_show(struct device *dev, struct device_attribute *attr,
  280. char *buf)
  281. {
  282. struct scsi_disk *sdkp = to_scsi_disk(dev);
  283. struct scsi_device *sdp = sdkp->device;
  284. unsigned int dif, dix;
  285. dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  286. dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
  287. if (!dix && scsi_host_dix_capable(sdp->host, T10_PI_TYPE0_PROTECTION)) {
  288. dif = 0;
  289. dix = 1;
  290. }
  291. if (!dif && !dix)
  292. return snprintf(buf, 20, "none\n");
  293. return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
  294. }
  295. static DEVICE_ATTR_RO(protection_mode);
  296. static ssize_t
  297. app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf)
  298. {
  299. struct scsi_disk *sdkp = to_scsi_disk(dev);
  300. return snprintf(buf, 20, "%u\n", sdkp->ATO);
  301. }
  302. static DEVICE_ATTR_RO(app_tag_own);
  303. static ssize_t
  304. thin_provisioning_show(struct device *dev, struct device_attribute *attr,
  305. char *buf)
  306. {
  307. struct scsi_disk *sdkp = to_scsi_disk(dev);
  308. return snprintf(buf, 20, "%u\n", sdkp->lbpme);
  309. }
  310. static DEVICE_ATTR_RO(thin_provisioning);
  311. static const char *lbp_mode[] = {
  312. [SD_LBP_FULL] = "full",
  313. [SD_LBP_UNMAP] = "unmap",
  314. [SD_LBP_WS16] = "writesame_16",
  315. [SD_LBP_WS10] = "writesame_10",
  316. [SD_LBP_ZERO] = "writesame_zero",
  317. [SD_LBP_DISABLE] = "disabled",
  318. };
  319. static ssize_t
  320. provisioning_mode_show(struct device *dev, struct device_attribute *attr,
  321. char *buf)
  322. {
  323. struct scsi_disk *sdkp = to_scsi_disk(dev);
  324. return snprintf(buf, 20, "%s\n", lbp_mode[sdkp->provisioning_mode]);
  325. }
  326. static ssize_t
  327. provisioning_mode_store(struct device *dev, struct device_attribute *attr,
  328. const char *buf, size_t count)
  329. {
  330. struct scsi_disk *sdkp = to_scsi_disk(dev);
  331. struct scsi_device *sdp = sdkp->device;
  332. if (!capable(CAP_SYS_ADMIN))
  333. return -EACCES;
  334. if (sdp->type != TYPE_DISK)
  335. return -EINVAL;
  336. if (!strncmp(buf, lbp_mode[SD_LBP_UNMAP], 20))
  337. sd_config_discard(sdkp, SD_LBP_UNMAP);
  338. else if (!strncmp(buf, lbp_mode[SD_LBP_WS16], 20))
  339. sd_config_discard(sdkp, SD_LBP_WS16);
  340. else if (!strncmp(buf, lbp_mode[SD_LBP_WS10], 20))
  341. sd_config_discard(sdkp, SD_LBP_WS10);
  342. else if (!strncmp(buf, lbp_mode[SD_LBP_ZERO], 20))
  343. sd_config_discard(sdkp, SD_LBP_ZERO);
  344. else if (!strncmp(buf, lbp_mode[SD_LBP_DISABLE], 20))
  345. sd_config_discard(sdkp, SD_LBP_DISABLE);
  346. else
  347. return -EINVAL;
  348. return count;
  349. }
  350. static DEVICE_ATTR_RW(provisioning_mode);
  351. static ssize_t
  352. max_medium_access_timeouts_show(struct device *dev,
  353. struct device_attribute *attr, char *buf)
  354. {
  355. struct scsi_disk *sdkp = to_scsi_disk(dev);
  356. return snprintf(buf, 20, "%u\n", sdkp->max_medium_access_timeouts);
  357. }
  358. static ssize_t
  359. max_medium_access_timeouts_store(struct device *dev,
  360. struct device_attribute *attr, const char *buf,
  361. size_t count)
  362. {
  363. struct scsi_disk *sdkp = to_scsi_disk(dev);
  364. int err;
  365. if (!capable(CAP_SYS_ADMIN))
  366. return -EACCES;
  367. err = kstrtouint(buf, 10, &sdkp->max_medium_access_timeouts);
  368. return err ? err : count;
  369. }
  370. static DEVICE_ATTR_RW(max_medium_access_timeouts);
  371. static ssize_t
  372. max_write_same_blocks_show(struct device *dev, struct device_attribute *attr,
  373. char *buf)
  374. {
  375. struct scsi_disk *sdkp = to_scsi_disk(dev);
  376. return snprintf(buf, 20, "%u\n", sdkp->max_ws_blocks);
  377. }
  378. static ssize_t
  379. max_write_same_blocks_store(struct device *dev, struct device_attribute *attr,
  380. const char *buf, size_t count)
  381. {
  382. struct scsi_disk *sdkp = to_scsi_disk(dev);
  383. struct scsi_device *sdp = sdkp->device;
  384. unsigned long max;
  385. int err;
  386. if (!capable(CAP_SYS_ADMIN))
  387. return -EACCES;
  388. if (sdp->type != TYPE_DISK)
  389. return -EINVAL;
  390. err = kstrtoul(buf, 10, &max);
  391. if (err)
  392. return err;
  393. if (max == 0)
  394. sdp->no_write_same = 1;
  395. else if (max <= SD_MAX_WS16_BLOCKS) {
  396. sdp->no_write_same = 0;
  397. sdkp->max_ws_blocks = max;
  398. }
  399. sd_config_write_same(sdkp);
  400. return count;
  401. }
  402. static DEVICE_ATTR_RW(max_write_same_blocks);
  403. static struct attribute *sd_disk_attrs[] = {
  404. &dev_attr_cache_type.attr,
  405. &dev_attr_FUA.attr,
  406. &dev_attr_allow_restart.attr,
  407. &dev_attr_manage_start_stop.attr,
  408. &dev_attr_protection_type.attr,
  409. &dev_attr_protection_mode.attr,
  410. &dev_attr_app_tag_own.attr,
  411. &dev_attr_thin_provisioning.attr,
  412. &dev_attr_provisioning_mode.attr,
  413. &dev_attr_max_write_same_blocks.attr,
  414. &dev_attr_max_medium_access_timeouts.attr,
  415. NULL,
  416. };
  417. ATTRIBUTE_GROUPS(sd_disk);
  418. static struct class sd_disk_class = {
  419. .name = "scsi_disk",
  420. .owner = THIS_MODULE,
  421. .dev_release = scsi_disk_release,
  422. .dev_groups = sd_disk_groups,
  423. };
  424. static const struct dev_pm_ops sd_pm_ops = {
  425. .suspend = sd_suspend_system,
  426. .resume = sd_resume,
  427. .poweroff = sd_suspend_system,
  428. .restore = sd_resume,
  429. .runtime_suspend = sd_suspend_runtime,
  430. .runtime_resume = sd_resume,
  431. };
  432. static struct scsi_driver sd_template = {
  433. .gendrv = {
  434. .name = "sd",
  435. .owner = THIS_MODULE,
  436. .probe = sd_probe,
  437. .remove = sd_remove,
  438. .shutdown = sd_shutdown,
  439. .pm = &sd_pm_ops,
  440. },
  441. .rescan = sd_rescan,
  442. .init_command = sd_init_command,
  443. .uninit_command = sd_uninit_command,
  444. .done = sd_done,
  445. .eh_action = sd_eh_action,
  446. };
  447. /*
  448. * Dummy kobj_map->probe function.
  449. * The default ->probe function will call modprobe, which is
  450. * pointless as this module is already loaded.
  451. */
  452. static struct kobject *sd_default_probe(dev_t devt, int *partno, void *data)
  453. {
  454. return NULL;
  455. }
  456. /*
  457. * Device no to disk mapping:
  458. *
  459. * major disc2 disc p1
  460. * |............|.............|....|....| <- dev_t
  461. * 31 20 19 8 7 4 3 0
  462. *
  463. * Inside a major, we have 16k disks, however mapped non-
  464. * contiguously. The first 16 disks are for major0, the next
  465. * ones with major1, ... Disk 256 is for major0 again, disk 272
  466. * for major1, ...
  467. * As we stay compatible with our numbering scheme, we can reuse
  468. * the well-know SCSI majors 8, 65--71, 136--143.
  469. */
  470. static int sd_major(int major_idx)
  471. {
  472. switch (major_idx) {
  473. case 0:
  474. return SCSI_DISK0_MAJOR;
  475. case 1 ... 7:
  476. return SCSI_DISK1_MAJOR + major_idx - 1;
  477. case 8 ... 15:
  478. return SCSI_DISK8_MAJOR + major_idx - 8;
  479. default:
  480. BUG();
  481. return 0; /* shut up gcc */
  482. }
  483. }
  484. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  485. {
  486. struct scsi_disk *sdkp = NULL;
  487. mutex_lock(&sd_ref_mutex);
  488. if (disk->private_data) {
  489. sdkp = scsi_disk(disk);
  490. if (scsi_device_get(sdkp->device) == 0)
  491. get_device(&sdkp->dev);
  492. else
  493. sdkp = NULL;
  494. }
  495. mutex_unlock(&sd_ref_mutex);
  496. return sdkp;
  497. }
  498. static void scsi_disk_put(struct scsi_disk *sdkp)
  499. {
  500. struct scsi_device *sdev = sdkp->device;
  501. mutex_lock(&sd_ref_mutex);
  502. put_device(&sdkp->dev);
  503. scsi_device_put(sdev);
  504. mutex_unlock(&sd_ref_mutex);
  505. }
  506. static unsigned char sd_setup_protect_cmnd(struct scsi_cmnd *scmd,
  507. unsigned int dix, unsigned int dif)
  508. {
  509. struct bio *bio = scmd->request->bio;
  510. unsigned int prot_op = sd_prot_op(rq_data_dir(scmd->request), dix, dif);
  511. unsigned int protect = 0;
  512. if (dix) { /* DIX Type 0, 1, 2, 3 */
  513. if (bio_integrity_flagged(bio, BIP_IP_CHECKSUM))
  514. scmd->prot_flags |= SCSI_PROT_IP_CHECKSUM;
  515. if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
  516. scmd->prot_flags |= SCSI_PROT_GUARD_CHECK;
  517. }
  518. if (dif != T10_PI_TYPE3_PROTECTION) { /* DIX/DIF Type 0, 1, 2 */
  519. scmd->prot_flags |= SCSI_PROT_REF_INCREMENT;
  520. if (bio_integrity_flagged(bio, BIP_CTRL_NOCHECK) == false)
  521. scmd->prot_flags |= SCSI_PROT_REF_CHECK;
  522. }
  523. if (dif) { /* DIX/DIF Type 1, 2, 3 */
  524. scmd->prot_flags |= SCSI_PROT_TRANSFER_PI;
  525. if (bio_integrity_flagged(bio, BIP_DISK_NOCHECK))
  526. protect = 3 << 5; /* Disable target PI checking */
  527. else
  528. protect = 1 << 5; /* Enable target PI checking */
  529. }
  530. scsi_set_prot_op(scmd, prot_op);
  531. scsi_set_prot_type(scmd, dif);
  532. scmd->prot_flags &= sd_prot_flag_mask(prot_op);
  533. return protect;
  534. }
  535. static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode)
  536. {
  537. struct request_queue *q = sdkp->disk->queue;
  538. unsigned int logical_block_size = sdkp->device->sector_size;
  539. unsigned int max_blocks = 0;
  540. q->limits.discard_zeroes_data = 0;
  541. /*
  542. * When LBPRZ is reported, discard alignment and granularity
  543. * must be fixed to the logical block size. Otherwise the block
  544. * layer will drop misaligned portions of the request which can
  545. * lead to data corruption. If LBPRZ is not set, we honor the
  546. * device preference.
  547. */
  548. if (sdkp->lbprz) {
  549. q->limits.discard_alignment = 0;
  550. q->limits.discard_granularity = logical_block_size;
  551. } else {
  552. q->limits.discard_alignment = sdkp->unmap_alignment *
  553. logical_block_size;
  554. q->limits.discard_granularity =
  555. max(sdkp->physical_block_size,
  556. sdkp->unmap_granularity * logical_block_size);
  557. }
  558. sdkp->provisioning_mode = mode;
  559. switch (mode) {
  560. case SD_LBP_DISABLE:
  561. blk_queue_max_discard_sectors(q, 0);
  562. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  563. return;
  564. case SD_LBP_UNMAP:
  565. max_blocks = min_not_zero(sdkp->max_unmap_blocks,
  566. (u32)SD_MAX_WS16_BLOCKS);
  567. break;
  568. case SD_LBP_WS16:
  569. max_blocks = min_not_zero(sdkp->max_ws_blocks,
  570. (u32)SD_MAX_WS16_BLOCKS);
  571. q->limits.discard_zeroes_data = sdkp->lbprz;
  572. break;
  573. case SD_LBP_WS10:
  574. max_blocks = min_not_zero(sdkp->max_ws_blocks,
  575. (u32)SD_MAX_WS10_BLOCKS);
  576. q->limits.discard_zeroes_data = sdkp->lbprz;
  577. break;
  578. case SD_LBP_ZERO:
  579. max_blocks = min_not_zero(sdkp->max_ws_blocks,
  580. (u32)SD_MAX_WS10_BLOCKS);
  581. q->limits.discard_zeroes_data = 1;
  582. break;
  583. }
  584. blk_queue_max_discard_sectors(q, max_blocks * (logical_block_size >> 9));
  585. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  586. }
  587. /**
  588. * sd_setup_discard_cmnd - unmap blocks on thinly provisioned device
  589. * @sdp: scsi device to operate one
  590. * @rq: Request to prepare
  591. *
  592. * Will issue either UNMAP or WRITE SAME(16) depending on preference
  593. * indicated by target device.
  594. **/
  595. static int sd_setup_discard_cmnd(struct scsi_cmnd *cmd)
  596. {
  597. struct request *rq = cmd->request;
  598. struct scsi_device *sdp = cmd->device;
  599. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  600. sector_t sector = blk_rq_pos(rq);
  601. unsigned int nr_sectors = blk_rq_sectors(rq);
  602. unsigned int nr_bytes = blk_rq_bytes(rq);
  603. unsigned int len;
  604. int ret;
  605. char *buf;
  606. struct page *page;
  607. sector >>= ilog2(sdp->sector_size) - 9;
  608. nr_sectors >>= ilog2(sdp->sector_size) - 9;
  609. page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
  610. if (!page)
  611. return BLKPREP_DEFER;
  612. switch (sdkp->provisioning_mode) {
  613. case SD_LBP_UNMAP:
  614. buf = page_address(page);
  615. cmd->cmd_len = 10;
  616. cmd->cmnd[0] = UNMAP;
  617. cmd->cmnd[8] = 24;
  618. put_unaligned_be16(6 + 16, &buf[0]);
  619. put_unaligned_be16(16, &buf[2]);
  620. put_unaligned_be64(sector, &buf[8]);
  621. put_unaligned_be32(nr_sectors, &buf[16]);
  622. len = 24;
  623. break;
  624. case SD_LBP_WS16:
  625. cmd->cmd_len = 16;
  626. cmd->cmnd[0] = WRITE_SAME_16;
  627. cmd->cmnd[1] = 0x8; /* UNMAP */
  628. put_unaligned_be64(sector, &cmd->cmnd[2]);
  629. put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
  630. len = sdkp->device->sector_size;
  631. break;
  632. case SD_LBP_WS10:
  633. case SD_LBP_ZERO:
  634. cmd->cmd_len = 10;
  635. cmd->cmnd[0] = WRITE_SAME;
  636. if (sdkp->provisioning_mode == SD_LBP_WS10)
  637. cmd->cmnd[1] = 0x8; /* UNMAP */
  638. put_unaligned_be32(sector, &cmd->cmnd[2]);
  639. put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
  640. len = sdkp->device->sector_size;
  641. break;
  642. default:
  643. ret = BLKPREP_INVALID;
  644. goto out;
  645. }
  646. rq->completion_data = page;
  647. rq->timeout = SD_TIMEOUT;
  648. cmd->transfersize = len;
  649. cmd->allowed = SD_MAX_RETRIES;
  650. /*
  651. * Initially __data_len is set to the amount of data that needs to be
  652. * transferred to the target. This amount depends on whether WRITE SAME
  653. * or UNMAP is being used. After the scatterlist has been mapped by
  654. * scsi_init_io() we set __data_len to the size of the area to be
  655. * discarded on disk. This allows us to report completion on the full
  656. * amount of blocks described by the request.
  657. */
  658. blk_add_request_payload(rq, page, 0, len);
  659. ret = scsi_init_io(cmd);
  660. rq->__data_len = nr_bytes;
  661. out:
  662. if (ret != BLKPREP_OK)
  663. __free_page(page);
  664. return ret;
  665. }
  666. static void sd_config_write_same(struct scsi_disk *sdkp)
  667. {
  668. struct request_queue *q = sdkp->disk->queue;
  669. unsigned int logical_block_size = sdkp->device->sector_size;
  670. if (sdkp->device->no_write_same) {
  671. sdkp->max_ws_blocks = 0;
  672. goto out;
  673. }
  674. /* Some devices can not handle block counts above 0xffff despite
  675. * supporting WRITE SAME(16). Consequently we default to 64k
  676. * blocks per I/O unless the device explicitly advertises a
  677. * bigger limit.
  678. */
  679. if (sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)
  680. sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
  681. (u32)SD_MAX_WS16_BLOCKS);
  682. else if (sdkp->ws16 || sdkp->ws10 || sdkp->device->no_report_opcodes)
  683. sdkp->max_ws_blocks = min_not_zero(sdkp->max_ws_blocks,
  684. (u32)SD_MAX_WS10_BLOCKS);
  685. else {
  686. sdkp->device->no_write_same = 1;
  687. sdkp->max_ws_blocks = 0;
  688. }
  689. out:
  690. blk_queue_max_write_same_sectors(q, sdkp->max_ws_blocks *
  691. (logical_block_size >> 9));
  692. }
  693. /**
  694. * sd_setup_write_same_cmnd - write the same data to multiple blocks
  695. * @cmd: command to prepare
  696. *
  697. * Will issue either WRITE SAME(10) or WRITE SAME(16) depending on
  698. * preference indicated by target device.
  699. **/
  700. static int sd_setup_write_same_cmnd(struct scsi_cmnd *cmd)
  701. {
  702. struct request *rq = cmd->request;
  703. struct scsi_device *sdp = cmd->device;
  704. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  705. struct bio *bio = rq->bio;
  706. sector_t sector = blk_rq_pos(rq);
  707. unsigned int nr_sectors = blk_rq_sectors(rq);
  708. unsigned int nr_bytes = blk_rq_bytes(rq);
  709. int ret;
  710. if (sdkp->device->no_write_same)
  711. return BLKPREP_INVALID;
  712. BUG_ON(bio_offset(bio) || bio_iovec(bio).bv_len != sdp->sector_size);
  713. sector >>= ilog2(sdp->sector_size) - 9;
  714. nr_sectors >>= ilog2(sdp->sector_size) - 9;
  715. rq->timeout = SD_WRITE_SAME_TIMEOUT;
  716. if (sdkp->ws16 || sector > 0xffffffff || nr_sectors > 0xffff) {
  717. cmd->cmd_len = 16;
  718. cmd->cmnd[0] = WRITE_SAME_16;
  719. put_unaligned_be64(sector, &cmd->cmnd[2]);
  720. put_unaligned_be32(nr_sectors, &cmd->cmnd[10]);
  721. } else {
  722. cmd->cmd_len = 10;
  723. cmd->cmnd[0] = WRITE_SAME;
  724. put_unaligned_be32(sector, &cmd->cmnd[2]);
  725. put_unaligned_be16(nr_sectors, &cmd->cmnd[7]);
  726. }
  727. cmd->transfersize = sdp->sector_size;
  728. cmd->allowed = SD_MAX_RETRIES;
  729. /*
  730. * For WRITE_SAME the data transferred in the DATA IN buffer is
  731. * different from the amount of data actually written to the target.
  732. *
  733. * We set up __data_len to the amount of data transferred from the
  734. * DATA IN buffer so that blk_rq_map_sg set up the proper S/G list
  735. * to transfer a single sector of data first, but then reset it to
  736. * the amount of data to be written right after so that the I/O path
  737. * knows how much to actually write.
  738. */
  739. rq->__data_len = sdp->sector_size;
  740. ret = scsi_init_io(cmd);
  741. rq->__data_len = nr_bytes;
  742. return ret;
  743. }
  744. static int sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
  745. {
  746. struct request *rq = cmd->request;
  747. /* flush requests don't perform I/O, zero the S/G table */
  748. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  749. cmd->cmnd[0] = SYNCHRONIZE_CACHE;
  750. cmd->cmd_len = 10;
  751. cmd->transfersize = 0;
  752. cmd->allowed = SD_MAX_RETRIES;
  753. rq->timeout = rq->q->rq_timeout * SD_FLUSH_TIMEOUT_MULTIPLIER;
  754. return BLKPREP_OK;
  755. }
  756. static int sd_setup_read_write_cmnd(struct scsi_cmnd *SCpnt)
  757. {
  758. struct request *rq = SCpnt->request;
  759. struct scsi_device *sdp = SCpnt->device;
  760. struct gendisk *disk = rq->rq_disk;
  761. struct scsi_disk *sdkp;
  762. sector_t block = blk_rq_pos(rq);
  763. sector_t threshold;
  764. unsigned int this_count = blk_rq_sectors(rq);
  765. unsigned int dif, dix;
  766. int ret;
  767. unsigned char protect;
  768. ret = scsi_init_io(SCpnt);
  769. if (ret != BLKPREP_OK)
  770. goto out;
  771. SCpnt = rq->special;
  772. sdkp = scsi_disk(disk);
  773. /* from here on until we're complete, any goto out
  774. * is used for a killable error condition */
  775. ret = BLKPREP_KILL;
  776. SCSI_LOG_HLQUEUE(1,
  777. scmd_printk(KERN_INFO, SCpnt,
  778. "%s: block=%llu, count=%d\n",
  779. __func__, (unsigned long long)block, this_count));
  780. if (!sdp || !scsi_device_online(sdp) ||
  781. block + blk_rq_sectors(rq) > get_capacity(disk)) {
  782. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  783. "Finishing %u sectors\n",
  784. blk_rq_sectors(rq)));
  785. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  786. "Retry with 0x%p\n", SCpnt));
  787. goto out;
  788. }
  789. if (sdp->changed) {
  790. /*
  791. * quietly refuse to do anything to a changed disc until
  792. * the changed bit has been reset
  793. */
  794. /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
  795. goto out;
  796. }
  797. /*
  798. * Some SD card readers can't handle multi-sector accesses which touch
  799. * the last one or two hardware sectors. Split accesses as needed.
  800. */
  801. threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
  802. (sdp->sector_size / 512);
  803. if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
  804. if (block < threshold) {
  805. /* Access up to the threshold but not beyond */
  806. this_count = threshold - block;
  807. } else {
  808. /* Access only a single hardware sector */
  809. this_count = sdp->sector_size / 512;
  810. }
  811. }
  812. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
  813. (unsigned long long)block));
  814. /*
  815. * If we have a 1K hardware sectorsize, prevent access to single
  816. * 512 byte sectors. In theory we could handle this - in fact
  817. * the scsi cdrom driver must be able to handle this because
  818. * we typically use 1K blocksizes, and cdroms typically have
  819. * 2K hardware sectorsizes. Of course, things are simpler
  820. * with the cdrom, since it is read-only. For performance
  821. * reasons, the filesystems should be able to handle this
  822. * and not force the scsi disk driver to use bounce buffers
  823. * for this.
  824. */
  825. if (sdp->sector_size == 1024) {
  826. if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
  827. scmd_printk(KERN_ERR, SCpnt,
  828. "Bad block number requested\n");
  829. goto out;
  830. } else {
  831. block = block >> 1;
  832. this_count = this_count >> 1;
  833. }
  834. }
  835. if (sdp->sector_size == 2048) {
  836. if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
  837. scmd_printk(KERN_ERR, SCpnt,
  838. "Bad block number requested\n");
  839. goto out;
  840. } else {
  841. block = block >> 2;
  842. this_count = this_count >> 2;
  843. }
  844. }
  845. if (sdp->sector_size == 4096) {
  846. if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
  847. scmd_printk(KERN_ERR, SCpnt,
  848. "Bad block number requested\n");
  849. goto out;
  850. } else {
  851. block = block >> 3;
  852. this_count = this_count >> 3;
  853. }
  854. }
  855. if (rq_data_dir(rq) == WRITE) {
  856. SCpnt->cmnd[0] = WRITE_6;
  857. if (blk_integrity_rq(rq))
  858. sd_dif_prepare(SCpnt);
  859. } else if (rq_data_dir(rq) == READ) {
  860. SCpnt->cmnd[0] = READ_6;
  861. } else {
  862. scmd_printk(KERN_ERR, SCpnt, "Unknown command %llu,%llx\n",
  863. req_op(rq), (unsigned long long) rq->cmd_flags);
  864. goto out;
  865. }
  866. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  867. "%s %d/%u 512 byte blocks.\n",
  868. (rq_data_dir(rq) == WRITE) ?
  869. "writing" : "reading", this_count,
  870. blk_rq_sectors(rq)));
  871. dix = scsi_prot_sg_count(SCpnt);
  872. dif = scsi_host_dif_capable(SCpnt->device->host, sdkp->protection_type);
  873. if (dif || dix)
  874. protect = sd_setup_protect_cmnd(SCpnt, dix, dif);
  875. else
  876. protect = 0;
  877. if (protect && sdkp->protection_type == T10_PI_TYPE2_PROTECTION) {
  878. SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
  879. if (unlikely(SCpnt->cmnd == NULL)) {
  880. ret = BLKPREP_DEFER;
  881. goto out;
  882. }
  883. SCpnt->cmd_len = SD_EXT_CDB_SIZE;
  884. memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
  885. SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
  886. SCpnt->cmnd[7] = 0x18;
  887. SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
  888. SCpnt->cmnd[10] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  889. /* LBA */
  890. SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  891. SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  892. SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  893. SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  894. SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
  895. SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
  896. SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
  897. SCpnt->cmnd[19] = (unsigned char) block & 0xff;
  898. /* Expected Indirect LBA */
  899. SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
  900. SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
  901. SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
  902. SCpnt->cmnd[23] = (unsigned char) block & 0xff;
  903. /* Transfer length */
  904. SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
  905. SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
  906. SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
  907. SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
  908. } else if (sdp->use_16_for_rw || (this_count > 0xffff)) {
  909. SCpnt->cmnd[0] += READ_16 - READ_6;
  910. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  911. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  912. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  913. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  914. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  915. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  916. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  917. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  918. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  919. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  920. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  921. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  922. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  923. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  924. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  925. scsi_device_protection(SCpnt->device) ||
  926. SCpnt->device->use_10_for_rw) {
  927. SCpnt->cmnd[0] += READ_10 - READ_6;
  928. SCpnt->cmnd[1] = protect | ((rq->cmd_flags & REQ_FUA) ? 0x8 : 0);
  929. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  930. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  931. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  932. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  933. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  934. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  935. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  936. } else {
  937. if (unlikely(rq->cmd_flags & REQ_FUA)) {
  938. /*
  939. * This happens only if this drive failed
  940. * 10byte rw command with ILLEGAL_REQUEST
  941. * during operation and thus turned off
  942. * use_10_for_rw.
  943. */
  944. scmd_printk(KERN_ERR, SCpnt,
  945. "FUA write on READ/WRITE(6) drive\n");
  946. goto out;
  947. }
  948. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  949. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  950. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  951. SCpnt->cmnd[4] = (unsigned char) this_count;
  952. SCpnt->cmnd[5] = 0;
  953. }
  954. SCpnt->sdb.length = this_count * sdp->sector_size;
  955. /*
  956. * We shouldn't disconnect in the middle of a sector, so with a dumb
  957. * host adapter, it's safe to assume that we can at least transfer
  958. * this many bytes between each connect / disconnect.
  959. */
  960. SCpnt->transfersize = sdp->sector_size;
  961. SCpnt->underflow = this_count << 9;
  962. SCpnt->allowed = SD_MAX_RETRIES;
  963. /*
  964. * This indicates that the command is ready from our end to be
  965. * queued.
  966. */
  967. ret = BLKPREP_OK;
  968. out:
  969. return ret;
  970. }
  971. static int sd_init_command(struct scsi_cmnd *cmd)
  972. {
  973. struct request *rq = cmd->request;
  974. switch (req_op(rq)) {
  975. case REQ_OP_DISCARD:
  976. return sd_setup_discard_cmnd(cmd);
  977. case REQ_OP_WRITE_SAME:
  978. return sd_setup_write_same_cmnd(cmd);
  979. case REQ_OP_FLUSH:
  980. return sd_setup_flush_cmnd(cmd);
  981. case REQ_OP_READ:
  982. case REQ_OP_WRITE:
  983. return sd_setup_read_write_cmnd(cmd);
  984. default:
  985. BUG();
  986. }
  987. }
  988. static void sd_uninit_command(struct scsi_cmnd *SCpnt)
  989. {
  990. struct request *rq = SCpnt->request;
  991. if (req_op(rq) == REQ_OP_DISCARD)
  992. __free_page(rq->completion_data);
  993. if (SCpnt->cmnd != rq->cmd) {
  994. mempool_free(SCpnt->cmnd, sd_cdb_pool);
  995. SCpnt->cmnd = NULL;
  996. SCpnt->cmd_len = 0;
  997. }
  998. }
  999. /**
  1000. * sd_open - open a scsi disk device
  1001. * @inode: only i_rdev member may be used
  1002. * @filp: only f_mode and f_flags may be used
  1003. *
  1004. * Returns 0 if successful. Returns a negated errno value in case
  1005. * of error.
  1006. *
  1007. * Note: This can be called from a user context (e.g. fsck(1) )
  1008. * or from within the kernel (e.g. as a result of a mount(1) ).
  1009. * In the latter case @inode and @filp carry an abridged amount
  1010. * of information as noted above.
  1011. *
  1012. * Locking: called with bdev->bd_mutex held.
  1013. **/
  1014. static int sd_open(struct block_device *bdev, fmode_t mode)
  1015. {
  1016. struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
  1017. struct scsi_device *sdev;
  1018. int retval;
  1019. if (!sdkp)
  1020. return -ENXIO;
  1021. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
  1022. sdev = sdkp->device;
  1023. /*
  1024. * If the device is in error recovery, wait until it is done.
  1025. * If the device is offline, then disallow any access to it.
  1026. */
  1027. retval = -ENXIO;
  1028. if (!scsi_block_when_processing_errors(sdev))
  1029. goto error_out;
  1030. if (sdev->removable || sdkp->write_prot)
  1031. check_disk_change(bdev);
  1032. /*
  1033. * If the drive is empty, just let the open fail.
  1034. */
  1035. retval = -ENOMEDIUM;
  1036. if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
  1037. goto error_out;
  1038. /*
  1039. * If the device has the write protect tab set, have the open fail
  1040. * if the user expects to be able to write to the thing.
  1041. */
  1042. retval = -EROFS;
  1043. if (sdkp->write_prot && (mode & FMODE_WRITE))
  1044. goto error_out;
  1045. /*
  1046. * It is possible that the disk changing stuff resulted in
  1047. * the device being taken offline. If this is the case,
  1048. * report this to the user, and don't pretend that the
  1049. * open actually succeeded.
  1050. */
  1051. retval = -ENXIO;
  1052. if (!scsi_device_online(sdev))
  1053. goto error_out;
  1054. if ((atomic_inc_return(&sdkp->openers) == 1) && sdev->removable) {
  1055. if (scsi_block_when_processing_errors(sdev))
  1056. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  1057. }
  1058. return 0;
  1059. error_out:
  1060. scsi_disk_put(sdkp);
  1061. return retval;
  1062. }
  1063. /**
  1064. * sd_release - invoked when the (last) close(2) is called on this
  1065. * scsi disk.
  1066. * @inode: only i_rdev member may be used
  1067. * @filp: only f_mode and f_flags may be used
  1068. *
  1069. * Returns 0.
  1070. *
  1071. * Note: may block (uninterruptible) if error recovery is underway
  1072. * on this disk.
  1073. *
  1074. * Locking: called with bdev->bd_mutex held.
  1075. **/
  1076. static void sd_release(struct gendisk *disk, fmode_t mode)
  1077. {
  1078. struct scsi_disk *sdkp = scsi_disk(disk);
  1079. struct scsi_device *sdev = sdkp->device;
  1080. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
  1081. if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
  1082. if (scsi_block_when_processing_errors(sdev))
  1083. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  1084. }
  1085. /*
  1086. * XXX and what if there are packets in flight and this close()
  1087. * XXX is followed by a "rmmod sd_mod"?
  1088. */
  1089. scsi_disk_put(sdkp);
  1090. }
  1091. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1092. {
  1093. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  1094. struct scsi_device *sdp = sdkp->device;
  1095. struct Scsi_Host *host = sdp->host;
  1096. sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
  1097. int diskinfo[4];
  1098. /* default to most commonly used values */
  1099. diskinfo[0] = 0x40; /* 1 << 6 */
  1100. diskinfo[1] = 0x20; /* 1 << 5 */
  1101. diskinfo[2] = capacity >> 11;
  1102. /* override with calculated, extended default, or driver values */
  1103. if (host->hostt->bios_param)
  1104. host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
  1105. else
  1106. scsicam_bios_param(bdev, capacity, diskinfo);
  1107. geo->heads = diskinfo[0];
  1108. geo->sectors = diskinfo[1];
  1109. geo->cylinders = diskinfo[2];
  1110. return 0;
  1111. }
  1112. /**
  1113. * sd_ioctl - process an ioctl
  1114. * @inode: only i_rdev/i_bdev members may be used
  1115. * @filp: only f_mode and f_flags may be used
  1116. * @cmd: ioctl command number
  1117. * @arg: this is third argument given to ioctl(2) system call.
  1118. * Often contains a pointer.
  1119. *
  1120. * Returns 0 if successful (some ioctls return positive numbers on
  1121. * success as well). Returns a negated errno value in case of error.
  1122. *
  1123. * Note: most ioctls are forward onto the block subsystem or further
  1124. * down in the scsi subsystem.
  1125. **/
  1126. static int sd_ioctl(struct block_device *bdev, fmode_t mode,
  1127. unsigned int cmd, unsigned long arg)
  1128. {
  1129. struct gendisk *disk = bdev->bd_disk;
  1130. struct scsi_disk *sdkp = scsi_disk(disk);
  1131. struct scsi_device *sdp = sdkp->device;
  1132. void __user *p = (void __user *)arg;
  1133. int error;
  1134. SCSI_LOG_IOCTL(1, sd_printk(KERN_INFO, sdkp, "sd_ioctl: disk=%s, "
  1135. "cmd=0x%x\n", disk->disk_name, cmd));
  1136. error = scsi_verify_blk_ioctl(bdev, cmd);
  1137. if (error < 0)
  1138. return error;
  1139. /*
  1140. * If we are in the middle of error recovery, don't let anyone
  1141. * else try and use this device. Also, if error recovery fails, it
  1142. * may try and take the device offline, in which case all further
  1143. * access to the device is prohibited.
  1144. */
  1145. error = scsi_ioctl_block_when_processing_errors(sdp, cmd,
  1146. (mode & FMODE_NDELAY) != 0);
  1147. if (error)
  1148. goto out;
  1149. /*
  1150. * Send SCSI addressing ioctls directly to mid level, send other
  1151. * ioctls to block level and then onto mid level if they can't be
  1152. * resolved.
  1153. */
  1154. switch (cmd) {
  1155. case SCSI_IOCTL_GET_IDLUN:
  1156. case SCSI_IOCTL_GET_BUS_NUMBER:
  1157. error = scsi_ioctl(sdp, cmd, p);
  1158. break;
  1159. default:
  1160. error = scsi_cmd_blk_ioctl(bdev, mode, cmd, p);
  1161. if (error != -ENOTTY)
  1162. break;
  1163. error = scsi_ioctl(sdp, cmd, p);
  1164. break;
  1165. }
  1166. out:
  1167. return error;
  1168. }
  1169. static void set_media_not_present(struct scsi_disk *sdkp)
  1170. {
  1171. if (sdkp->media_present)
  1172. sdkp->device->changed = 1;
  1173. if (sdkp->device->removable) {
  1174. sdkp->media_present = 0;
  1175. sdkp->capacity = 0;
  1176. }
  1177. }
  1178. static int media_not_present(struct scsi_disk *sdkp,
  1179. struct scsi_sense_hdr *sshdr)
  1180. {
  1181. if (!scsi_sense_valid(sshdr))
  1182. return 0;
  1183. /* not invoked for commands that could return deferred errors */
  1184. switch (sshdr->sense_key) {
  1185. case UNIT_ATTENTION:
  1186. case NOT_READY:
  1187. /* medium not present */
  1188. if (sshdr->asc == 0x3A) {
  1189. set_media_not_present(sdkp);
  1190. return 1;
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. /**
  1196. * sd_check_events - check media events
  1197. * @disk: kernel device descriptor
  1198. * @clearing: disk events currently being cleared
  1199. *
  1200. * Returns mask of DISK_EVENT_*.
  1201. *
  1202. * Note: this function is invoked from the block subsystem.
  1203. **/
  1204. static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
  1205. {
  1206. struct scsi_disk *sdkp = scsi_disk_get(disk);
  1207. struct scsi_device *sdp;
  1208. struct scsi_sense_hdr *sshdr = NULL;
  1209. int retval;
  1210. if (!sdkp)
  1211. return 0;
  1212. sdp = sdkp->device;
  1213. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
  1214. /*
  1215. * If the device is offline, don't send any commands - just pretend as
  1216. * if the command failed. If the device ever comes back online, we
  1217. * can deal with it then. It is only because of unrecoverable errors
  1218. * that we would ever take a device offline in the first place.
  1219. */
  1220. if (!scsi_device_online(sdp)) {
  1221. set_media_not_present(sdkp);
  1222. goto out;
  1223. }
  1224. /*
  1225. * Using TEST_UNIT_READY enables differentiation between drive with
  1226. * no cartridge loaded - NOT READY, drive with changed cartridge -
  1227. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  1228. *
  1229. * Drives that auto spin down. eg iomega jaz 1G, will be started
  1230. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  1231. * sd_revalidate() is called.
  1232. */
  1233. retval = -ENODEV;
  1234. if (scsi_block_when_processing_errors(sdp)) {
  1235. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  1236. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
  1237. sshdr);
  1238. }
  1239. /* failed to execute TUR, assume media not present */
  1240. if (host_byte(retval)) {
  1241. set_media_not_present(sdkp);
  1242. goto out;
  1243. }
  1244. if (media_not_present(sdkp, sshdr))
  1245. goto out;
  1246. /*
  1247. * For removable scsi disk we have to recognise the presence
  1248. * of a disk in the drive.
  1249. */
  1250. if (!sdkp->media_present)
  1251. sdp->changed = 1;
  1252. sdkp->media_present = 1;
  1253. out:
  1254. /*
  1255. * sdp->changed is set under the following conditions:
  1256. *
  1257. * Medium present state has changed in either direction.
  1258. * Device has indicated UNIT_ATTENTION.
  1259. */
  1260. kfree(sshdr);
  1261. retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
  1262. sdp->changed = 0;
  1263. scsi_disk_put(sdkp);
  1264. return retval;
  1265. }
  1266. static int sd_sync_cache(struct scsi_disk *sdkp)
  1267. {
  1268. int retries, res;
  1269. struct scsi_device *sdp = sdkp->device;
  1270. const int timeout = sdp->request_queue->rq_timeout
  1271. * SD_FLUSH_TIMEOUT_MULTIPLIER;
  1272. struct scsi_sense_hdr sshdr;
  1273. if (!scsi_device_online(sdp))
  1274. return -ENODEV;
  1275. for (retries = 3; retries > 0; --retries) {
  1276. unsigned char cmd[10] = { 0 };
  1277. cmd[0] = SYNCHRONIZE_CACHE;
  1278. /*
  1279. * Leave the rest of the command zero to indicate
  1280. * flush everything.
  1281. */
  1282. res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0,
  1283. &sshdr, timeout, SD_MAX_RETRIES,
  1284. NULL, REQ_PM);
  1285. if (res == 0)
  1286. break;
  1287. }
  1288. if (res) {
  1289. sd_print_result(sdkp, "Synchronize Cache(10) failed", res);
  1290. if (driver_byte(res) & DRIVER_SENSE)
  1291. sd_print_sense_hdr(sdkp, &sshdr);
  1292. /* we need to evaluate the error return */
  1293. if (scsi_sense_valid(&sshdr) &&
  1294. (sshdr.asc == 0x3a || /* medium not present */
  1295. sshdr.asc == 0x20)) /* invalid command */
  1296. /* this is no error here */
  1297. return 0;
  1298. switch (host_byte(res)) {
  1299. /* ignore errors due to racing a disconnection */
  1300. case DID_BAD_TARGET:
  1301. case DID_NO_CONNECT:
  1302. return 0;
  1303. /* signal the upper layer it might try again */
  1304. case DID_BUS_BUSY:
  1305. case DID_IMM_RETRY:
  1306. case DID_REQUEUE:
  1307. case DID_SOFT_ERROR:
  1308. return -EBUSY;
  1309. default:
  1310. return -EIO;
  1311. }
  1312. }
  1313. return 0;
  1314. }
  1315. static void sd_rescan(struct device *dev)
  1316. {
  1317. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  1318. revalidate_disk(sdkp->disk);
  1319. }
  1320. #ifdef CONFIG_COMPAT
  1321. /*
  1322. * This gets directly called from VFS. When the ioctl
  1323. * is not recognized we go back to the other translation paths.
  1324. */
  1325. static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
  1326. unsigned int cmd, unsigned long arg)
  1327. {
  1328. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  1329. int error;
  1330. error = scsi_ioctl_block_when_processing_errors(sdev, cmd,
  1331. (mode & FMODE_NDELAY) != 0);
  1332. if (error)
  1333. return error;
  1334. /*
  1335. * Let the static ioctl translation table take care of it.
  1336. */
  1337. if (!sdev->host->hostt->compat_ioctl)
  1338. return -ENOIOCTLCMD;
  1339. return sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  1340. }
  1341. #endif
  1342. static char sd_pr_type(enum pr_type type)
  1343. {
  1344. switch (type) {
  1345. case PR_WRITE_EXCLUSIVE:
  1346. return 0x01;
  1347. case PR_EXCLUSIVE_ACCESS:
  1348. return 0x03;
  1349. case PR_WRITE_EXCLUSIVE_REG_ONLY:
  1350. return 0x05;
  1351. case PR_EXCLUSIVE_ACCESS_REG_ONLY:
  1352. return 0x06;
  1353. case PR_WRITE_EXCLUSIVE_ALL_REGS:
  1354. return 0x07;
  1355. case PR_EXCLUSIVE_ACCESS_ALL_REGS:
  1356. return 0x08;
  1357. default:
  1358. return 0;
  1359. }
  1360. };
  1361. static int sd_pr_command(struct block_device *bdev, u8 sa,
  1362. u64 key, u64 sa_key, u8 type, u8 flags)
  1363. {
  1364. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  1365. struct scsi_sense_hdr sshdr;
  1366. int result;
  1367. u8 cmd[16] = { 0, };
  1368. u8 data[24] = { 0, };
  1369. cmd[0] = PERSISTENT_RESERVE_OUT;
  1370. cmd[1] = sa;
  1371. cmd[2] = type;
  1372. put_unaligned_be32(sizeof(data), &cmd[5]);
  1373. put_unaligned_be64(key, &data[0]);
  1374. put_unaligned_be64(sa_key, &data[8]);
  1375. data[20] = flags;
  1376. result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, &data, sizeof(data),
  1377. &sshdr, SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1378. if ((driver_byte(result) & DRIVER_SENSE) &&
  1379. (scsi_sense_valid(&sshdr))) {
  1380. sdev_printk(KERN_INFO, sdev, "PR command failed: %d\n", result);
  1381. scsi_print_sense_hdr(sdev, NULL, &sshdr);
  1382. }
  1383. return result;
  1384. }
  1385. static int sd_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
  1386. u32 flags)
  1387. {
  1388. if (flags & ~PR_FL_IGNORE_KEY)
  1389. return -EOPNOTSUPP;
  1390. return sd_pr_command(bdev, (flags & PR_FL_IGNORE_KEY) ? 0x06 : 0x00,
  1391. old_key, new_key, 0,
  1392. (1 << 0) /* APTPL */);
  1393. }
  1394. static int sd_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
  1395. u32 flags)
  1396. {
  1397. if (flags)
  1398. return -EOPNOTSUPP;
  1399. return sd_pr_command(bdev, 0x01, key, 0, sd_pr_type(type), 0);
  1400. }
  1401. static int sd_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  1402. {
  1403. return sd_pr_command(bdev, 0x02, key, 0, sd_pr_type(type), 0);
  1404. }
  1405. static int sd_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
  1406. enum pr_type type, bool abort)
  1407. {
  1408. return sd_pr_command(bdev, abort ? 0x05 : 0x04, old_key, new_key,
  1409. sd_pr_type(type), 0);
  1410. }
  1411. static int sd_pr_clear(struct block_device *bdev, u64 key)
  1412. {
  1413. return sd_pr_command(bdev, 0x03, key, 0, 0, 0);
  1414. }
  1415. static const struct pr_ops sd_pr_ops = {
  1416. .pr_register = sd_pr_register,
  1417. .pr_reserve = sd_pr_reserve,
  1418. .pr_release = sd_pr_release,
  1419. .pr_preempt = sd_pr_preempt,
  1420. .pr_clear = sd_pr_clear,
  1421. };
  1422. static const struct block_device_operations sd_fops = {
  1423. .owner = THIS_MODULE,
  1424. .open = sd_open,
  1425. .release = sd_release,
  1426. .ioctl = sd_ioctl,
  1427. .getgeo = sd_getgeo,
  1428. #ifdef CONFIG_COMPAT
  1429. .compat_ioctl = sd_compat_ioctl,
  1430. #endif
  1431. .check_events = sd_check_events,
  1432. .revalidate_disk = sd_revalidate_disk,
  1433. .unlock_native_capacity = sd_unlock_native_capacity,
  1434. .pr_ops = &sd_pr_ops,
  1435. };
  1436. /**
  1437. * sd_eh_action - error handling callback
  1438. * @scmd: sd-issued command that has failed
  1439. * @eh_disp: The recovery disposition suggested by the midlayer
  1440. *
  1441. * This function is called by the SCSI midlayer upon completion of an
  1442. * error test command (currently TEST UNIT READY). The result of sending
  1443. * the eh command is passed in eh_disp. We're looking for devices that
  1444. * fail medium access commands but are OK with non access commands like
  1445. * test unit ready (so wrongly see the device as having a successful
  1446. * recovery)
  1447. **/
  1448. static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
  1449. {
  1450. struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk);
  1451. if (!scsi_device_online(scmd->device) ||
  1452. !scsi_medium_access_command(scmd) ||
  1453. host_byte(scmd->result) != DID_TIME_OUT ||
  1454. eh_disp != SUCCESS)
  1455. return eh_disp;
  1456. /*
  1457. * The device has timed out executing a medium access command.
  1458. * However, the TEST UNIT READY command sent during error
  1459. * handling completed successfully. Either the device is in the
  1460. * process of recovering or has it suffered an internal failure
  1461. * that prevents access to the storage medium.
  1462. */
  1463. sdkp->medium_access_timed_out++;
  1464. /*
  1465. * If the device keeps failing read/write commands but TEST UNIT
  1466. * READY always completes successfully we assume that medium
  1467. * access is no longer possible and take the device offline.
  1468. */
  1469. if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) {
  1470. scmd_printk(KERN_ERR, scmd,
  1471. "Medium access timeout failure. Offlining disk!\n");
  1472. scsi_device_set_state(scmd->device, SDEV_OFFLINE);
  1473. return FAILED;
  1474. }
  1475. return eh_disp;
  1476. }
  1477. static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
  1478. {
  1479. u64 start_lba = blk_rq_pos(scmd->request);
  1480. u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
  1481. u64 factor = scmd->device->sector_size / 512;
  1482. u64 bad_lba;
  1483. int info_valid;
  1484. /*
  1485. * resid is optional but mostly filled in. When it's unused,
  1486. * its value is zero, so we assume the whole buffer transferred
  1487. */
  1488. unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
  1489. unsigned int good_bytes;
  1490. if (scmd->request->cmd_type != REQ_TYPE_FS)
  1491. return 0;
  1492. info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
  1493. SCSI_SENSE_BUFFERSIZE,
  1494. &bad_lba);
  1495. if (!info_valid)
  1496. return 0;
  1497. if (scsi_bufflen(scmd) <= scmd->device->sector_size)
  1498. return 0;
  1499. /* be careful ... don't want any overflows */
  1500. do_div(start_lba, factor);
  1501. do_div(end_lba, factor);
  1502. /* The bad lba was reported incorrectly, we have no idea where
  1503. * the error is.
  1504. */
  1505. if (bad_lba < start_lba || bad_lba >= end_lba)
  1506. return 0;
  1507. /* This computation should always be done in terms of
  1508. * the resolution of the device's medium.
  1509. */
  1510. good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
  1511. return min(good_bytes, transferred);
  1512. }
  1513. /**
  1514. * sd_done - bottom half handler: called when the lower level
  1515. * driver has completed (successfully or otherwise) a scsi command.
  1516. * @SCpnt: mid-level's per command structure.
  1517. *
  1518. * Note: potentially run from within an ISR. Must not block.
  1519. **/
  1520. static int sd_done(struct scsi_cmnd *SCpnt)
  1521. {
  1522. int result = SCpnt->result;
  1523. unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
  1524. struct scsi_sense_hdr sshdr;
  1525. struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
  1526. struct request *req = SCpnt->request;
  1527. int sense_valid = 0;
  1528. int sense_deferred = 0;
  1529. unsigned char op = SCpnt->cmnd[0];
  1530. unsigned char unmap = SCpnt->cmnd[1] & 8;
  1531. if (req_op(req) == REQ_OP_DISCARD || req_op(req) == REQ_OP_WRITE_SAME) {
  1532. if (!result) {
  1533. good_bytes = blk_rq_bytes(req);
  1534. scsi_set_resid(SCpnt, 0);
  1535. } else {
  1536. good_bytes = 0;
  1537. scsi_set_resid(SCpnt, blk_rq_bytes(req));
  1538. }
  1539. }
  1540. if (result) {
  1541. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  1542. if (sense_valid)
  1543. sense_deferred = scsi_sense_is_deferred(&sshdr);
  1544. }
  1545. sdkp->medium_access_timed_out = 0;
  1546. if (driver_byte(result) != DRIVER_SENSE &&
  1547. (!sense_valid || sense_deferred))
  1548. goto out;
  1549. switch (sshdr.sense_key) {
  1550. case HARDWARE_ERROR:
  1551. case MEDIUM_ERROR:
  1552. good_bytes = sd_completed_bytes(SCpnt);
  1553. break;
  1554. case RECOVERED_ERROR:
  1555. good_bytes = scsi_bufflen(SCpnt);
  1556. break;
  1557. case NO_SENSE:
  1558. /* This indicates a false check condition, so ignore it. An
  1559. * unknown amount of data was transferred so treat it as an
  1560. * error.
  1561. */
  1562. SCpnt->result = 0;
  1563. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1564. break;
  1565. case ABORTED_COMMAND:
  1566. if (sshdr.asc == 0x10) /* DIF: Target detected corruption */
  1567. good_bytes = sd_completed_bytes(SCpnt);
  1568. break;
  1569. case ILLEGAL_REQUEST:
  1570. if (sshdr.asc == 0x10) /* DIX: Host detected corruption */
  1571. good_bytes = sd_completed_bytes(SCpnt);
  1572. /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
  1573. if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
  1574. switch (op) {
  1575. case UNMAP:
  1576. sd_config_discard(sdkp, SD_LBP_DISABLE);
  1577. break;
  1578. case WRITE_SAME_16:
  1579. case WRITE_SAME:
  1580. if (unmap)
  1581. sd_config_discard(sdkp, SD_LBP_DISABLE);
  1582. else {
  1583. sdkp->device->no_write_same = 1;
  1584. sd_config_write_same(sdkp);
  1585. good_bytes = 0;
  1586. req->__data_len = blk_rq_bytes(req);
  1587. req->cmd_flags |= REQ_QUIET;
  1588. }
  1589. }
  1590. }
  1591. break;
  1592. default:
  1593. break;
  1594. }
  1595. out:
  1596. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
  1597. "sd_done: completed %d of %d bytes\n",
  1598. good_bytes, scsi_bufflen(SCpnt)));
  1599. if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
  1600. sd_dif_complete(SCpnt, good_bytes);
  1601. return good_bytes;
  1602. }
  1603. /*
  1604. * spinup disk - called only in sd_revalidate_disk()
  1605. */
  1606. static void
  1607. sd_spinup_disk(struct scsi_disk *sdkp)
  1608. {
  1609. unsigned char cmd[10];
  1610. unsigned long spintime_expire = 0;
  1611. int retries, spintime;
  1612. unsigned int the_result;
  1613. struct scsi_sense_hdr sshdr;
  1614. int sense_valid = 0;
  1615. spintime = 0;
  1616. /* Spin up drives, as required. Only do this at boot time */
  1617. /* Spinup needs to be done for module loads too. */
  1618. do {
  1619. retries = 0;
  1620. do {
  1621. cmd[0] = TEST_UNIT_READY;
  1622. memset((void *) &cmd[1], 0, 9);
  1623. the_result = scsi_execute_req(sdkp->device, cmd,
  1624. DMA_NONE, NULL, 0,
  1625. &sshdr, SD_TIMEOUT,
  1626. SD_MAX_RETRIES, NULL);
  1627. /*
  1628. * If the drive has indicated to us that it
  1629. * doesn't have any media in it, don't bother
  1630. * with any more polling.
  1631. */
  1632. if (media_not_present(sdkp, &sshdr))
  1633. return;
  1634. if (the_result)
  1635. sense_valid = scsi_sense_valid(&sshdr);
  1636. retries++;
  1637. } while (retries < 3 &&
  1638. (!scsi_status_is_good(the_result) ||
  1639. ((driver_byte(the_result) & DRIVER_SENSE) &&
  1640. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  1641. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  1642. /* no sense, TUR either succeeded or failed
  1643. * with a status error */
  1644. if(!spintime && !scsi_status_is_good(the_result)) {
  1645. sd_print_result(sdkp, "Test Unit Ready failed",
  1646. the_result);
  1647. }
  1648. break;
  1649. }
  1650. /*
  1651. * The device does not want the automatic start to be issued.
  1652. */
  1653. if (sdkp->device->no_start_on_add)
  1654. break;
  1655. if (sense_valid && sshdr.sense_key == NOT_READY) {
  1656. if (sshdr.asc == 4 && sshdr.ascq == 3)
  1657. break; /* manual intervention required */
  1658. if (sshdr.asc == 4 && sshdr.ascq == 0xb)
  1659. break; /* standby */
  1660. if (sshdr.asc == 4 && sshdr.ascq == 0xc)
  1661. break; /* unavailable */
  1662. /*
  1663. * Issue command to spin up drive when not ready
  1664. */
  1665. if (!spintime) {
  1666. sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  1667. cmd[0] = START_STOP;
  1668. cmd[1] = 1; /* Return immediately */
  1669. memset((void *) &cmd[2], 0, 8);
  1670. cmd[4] = 1; /* Start spin cycle */
  1671. if (sdkp->device->start_stop_pwr_cond)
  1672. cmd[4] |= 1 << 4;
  1673. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  1674. NULL, 0, &sshdr,
  1675. SD_TIMEOUT, SD_MAX_RETRIES,
  1676. NULL);
  1677. spintime_expire = jiffies + 100 * HZ;
  1678. spintime = 1;
  1679. }
  1680. /* Wait 1 second for next try */
  1681. msleep(1000);
  1682. printk(".");
  1683. /*
  1684. * Wait for USB flash devices with slow firmware.
  1685. * Yes, this sense key/ASC combination shouldn't
  1686. * occur here. It's characteristic of these devices.
  1687. */
  1688. } else if (sense_valid &&
  1689. sshdr.sense_key == UNIT_ATTENTION &&
  1690. sshdr.asc == 0x28) {
  1691. if (!spintime) {
  1692. spintime_expire = jiffies + 5 * HZ;
  1693. spintime = 1;
  1694. }
  1695. /* Wait 1 second for next try */
  1696. msleep(1000);
  1697. } else {
  1698. /* we don't understand the sense code, so it's
  1699. * probably pointless to loop */
  1700. if(!spintime) {
  1701. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1702. sd_print_sense_hdr(sdkp, &sshdr);
  1703. }
  1704. break;
  1705. }
  1706. } while (spintime && time_before_eq(jiffies, spintime_expire));
  1707. if (spintime) {
  1708. if (scsi_status_is_good(the_result))
  1709. printk("ready\n");
  1710. else
  1711. printk("not responding...\n");
  1712. }
  1713. }
  1714. /*
  1715. * Determine whether disk supports Data Integrity Field.
  1716. */
  1717. static int sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1718. {
  1719. struct scsi_device *sdp = sdkp->device;
  1720. u8 type;
  1721. int ret = 0;
  1722. if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
  1723. return ret;
  1724. type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
  1725. if (type > T10_PI_TYPE3_PROTECTION)
  1726. ret = -ENODEV;
  1727. else if (scsi_host_dif_capable(sdp->host, type))
  1728. ret = 1;
  1729. if (sdkp->first_scan || type != sdkp->protection_type)
  1730. switch (ret) {
  1731. case -ENODEV:
  1732. sd_printk(KERN_ERR, sdkp, "formatted with unsupported" \
  1733. " protection type %u. Disabling disk!\n",
  1734. type);
  1735. break;
  1736. case 1:
  1737. sd_printk(KERN_NOTICE, sdkp,
  1738. "Enabling DIF Type %u protection\n", type);
  1739. break;
  1740. case 0:
  1741. sd_printk(KERN_NOTICE, sdkp,
  1742. "Disabling DIF Type %u protection\n", type);
  1743. break;
  1744. }
  1745. sdkp->protection_type = type;
  1746. return ret;
  1747. }
  1748. static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1749. struct scsi_sense_hdr *sshdr, int sense_valid,
  1750. int the_result)
  1751. {
  1752. if (driver_byte(the_result) & DRIVER_SENSE)
  1753. sd_print_sense_hdr(sdkp, sshdr);
  1754. else
  1755. sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1756. /*
  1757. * Set dirty bit for removable devices if not ready -
  1758. * sometimes drives will not report this properly.
  1759. */
  1760. if (sdp->removable &&
  1761. sense_valid && sshdr->sense_key == NOT_READY)
  1762. set_media_not_present(sdkp);
  1763. /*
  1764. * We used to set media_present to 0 here to indicate no media
  1765. * in the drive, but some drives fail read capacity even with
  1766. * media present, so we can't do that.
  1767. */
  1768. sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1769. }
  1770. #define RC16_LEN 32
  1771. #if RC16_LEN > SD_BUF_SIZE
  1772. #error RC16_LEN must not be more than SD_BUF_SIZE
  1773. #endif
  1774. #define READ_CAPACITY_RETRIES_ON_RESET 10
  1775. /*
  1776. * Ensure that we don't overflow sector_t when CONFIG_LBDAF is not set
  1777. * and the reported logical block size is bigger than 512 bytes. Note
  1778. * that last_sector is a u64 and therefore logical_to_sectors() is not
  1779. * applicable.
  1780. */
  1781. static bool sd_addressable_capacity(u64 lba, unsigned int sector_size)
  1782. {
  1783. u64 last_sector = (lba + 1ULL) << (ilog2(sector_size) - 9);
  1784. if (sizeof(sector_t) == 4 && last_sector > U32_MAX)
  1785. return false;
  1786. return true;
  1787. }
  1788. static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1789. unsigned char *buffer)
  1790. {
  1791. unsigned char cmd[16];
  1792. struct scsi_sense_hdr sshdr;
  1793. int sense_valid = 0;
  1794. int the_result;
  1795. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1796. unsigned int alignment;
  1797. unsigned long long lba;
  1798. unsigned sector_size;
  1799. if (sdp->no_read_capacity_16)
  1800. return -EINVAL;
  1801. do {
  1802. memset(cmd, 0, 16);
  1803. cmd[0] = SERVICE_ACTION_IN_16;
  1804. cmd[1] = SAI_READ_CAPACITY_16;
  1805. cmd[13] = RC16_LEN;
  1806. memset(buffer, 0, RC16_LEN);
  1807. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1808. buffer, RC16_LEN, &sshdr,
  1809. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1810. if (media_not_present(sdkp, &sshdr))
  1811. return -ENODEV;
  1812. if (the_result) {
  1813. sense_valid = scsi_sense_valid(&sshdr);
  1814. if (sense_valid &&
  1815. sshdr.sense_key == ILLEGAL_REQUEST &&
  1816. (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1817. sshdr.ascq == 0x00)
  1818. /* Invalid Command Operation Code or
  1819. * Invalid Field in CDB, just retry
  1820. * silently with RC10 */
  1821. return -EINVAL;
  1822. if (sense_valid &&
  1823. sshdr.sense_key == UNIT_ATTENTION &&
  1824. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1825. /* Device reset might occur several times,
  1826. * give it one more chance */
  1827. if (--reset_retries > 0)
  1828. continue;
  1829. }
  1830. retries--;
  1831. } while (the_result && retries);
  1832. if (the_result) {
  1833. sd_print_result(sdkp, "Read Capacity(16) failed", the_result);
  1834. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1835. return -EINVAL;
  1836. }
  1837. sector_size = get_unaligned_be32(&buffer[8]);
  1838. lba = get_unaligned_be64(&buffer[0]);
  1839. if (sd_read_protection_type(sdkp, buffer) < 0) {
  1840. sdkp->capacity = 0;
  1841. return -ENODEV;
  1842. }
  1843. if (!sd_addressable_capacity(lba, sector_size)) {
  1844. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1845. "kernel compiled with support for large block "
  1846. "devices.\n");
  1847. sdkp->capacity = 0;
  1848. return -EOVERFLOW;
  1849. }
  1850. /* Logical blocks per physical block exponent */
  1851. sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
  1852. /* Lowest aligned logical block */
  1853. alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
  1854. blk_queue_alignment_offset(sdp->request_queue, alignment);
  1855. if (alignment && sdkp->first_scan)
  1856. sd_printk(KERN_NOTICE, sdkp,
  1857. "physical block alignment offset: %u\n", alignment);
  1858. if (buffer[14] & 0x80) { /* LBPME */
  1859. sdkp->lbpme = 1;
  1860. if (buffer[14] & 0x40) /* LBPRZ */
  1861. sdkp->lbprz = 1;
  1862. sd_config_discard(sdkp, SD_LBP_WS16);
  1863. }
  1864. sdkp->capacity = lba + 1;
  1865. return sector_size;
  1866. }
  1867. static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1868. unsigned char *buffer)
  1869. {
  1870. unsigned char cmd[16];
  1871. struct scsi_sense_hdr sshdr;
  1872. int sense_valid = 0;
  1873. int the_result;
  1874. int retries = 3, reset_retries = READ_CAPACITY_RETRIES_ON_RESET;
  1875. sector_t lba;
  1876. unsigned sector_size;
  1877. do {
  1878. cmd[0] = READ_CAPACITY;
  1879. memset(&cmd[1], 0, 9);
  1880. memset(buffer, 0, 8);
  1881. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1882. buffer, 8, &sshdr,
  1883. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1884. if (media_not_present(sdkp, &sshdr))
  1885. return -ENODEV;
  1886. if (the_result) {
  1887. sense_valid = scsi_sense_valid(&sshdr);
  1888. if (sense_valid &&
  1889. sshdr.sense_key == UNIT_ATTENTION &&
  1890. sshdr.asc == 0x29 && sshdr.ascq == 0x00)
  1891. /* Device reset might occur several times,
  1892. * give it one more chance */
  1893. if (--reset_retries > 0)
  1894. continue;
  1895. }
  1896. retries--;
  1897. } while (the_result && retries);
  1898. if (the_result) {
  1899. sd_print_result(sdkp, "Read Capacity(10) failed", the_result);
  1900. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1901. return -EINVAL;
  1902. }
  1903. sector_size = get_unaligned_be32(&buffer[4]);
  1904. lba = get_unaligned_be32(&buffer[0]);
  1905. if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
  1906. /* Some buggy (usb cardreader) devices return an lba of
  1907. 0xffffffff when the want to report a size of 0 (with
  1908. which they really mean no media is present) */
  1909. sdkp->capacity = 0;
  1910. sdkp->physical_block_size = sector_size;
  1911. return sector_size;
  1912. }
  1913. if (!sd_addressable_capacity(lba, sector_size)) {
  1914. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1915. "kernel compiled with support for large block "
  1916. "devices.\n");
  1917. sdkp->capacity = 0;
  1918. return -EOVERFLOW;
  1919. }
  1920. sdkp->capacity = lba + 1;
  1921. sdkp->physical_block_size = sector_size;
  1922. return sector_size;
  1923. }
  1924. static int sd_try_rc16_first(struct scsi_device *sdp)
  1925. {
  1926. if (sdp->host->max_cmd_len < 16)
  1927. return 0;
  1928. if (sdp->try_rc_10_first)
  1929. return 0;
  1930. if (sdp->scsi_level > SCSI_SPC_2)
  1931. return 1;
  1932. if (scsi_device_protection(sdp))
  1933. return 1;
  1934. return 0;
  1935. }
  1936. /*
  1937. * read disk capacity
  1938. */
  1939. static void
  1940. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  1941. {
  1942. int sector_size;
  1943. struct scsi_device *sdp = sdkp->device;
  1944. sector_t old_capacity = sdkp->capacity;
  1945. if (sd_try_rc16_first(sdp)) {
  1946. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1947. if (sector_size == -EOVERFLOW)
  1948. goto got_data;
  1949. if (sector_size == -ENODEV)
  1950. return;
  1951. if (sector_size < 0)
  1952. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1953. if (sector_size < 0)
  1954. return;
  1955. } else {
  1956. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1957. if (sector_size == -EOVERFLOW)
  1958. goto got_data;
  1959. if (sector_size < 0)
  1960. return;
  1961. if ((sizeof(sdkp->capacity) > 4) &&
  1962. (sdkp->capacity > 0xffffffffULL)) {
  1963. int old_sector_size = sector_size;
  1964. sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1965. "Trying to use READ CAPACITY(16).\n");
  1966. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1967. if (sector_size < 0) {
  1968. sd_printk(KERN_NOTICE, sdkp,
  1969. "Using 0xffffffff as device size\n");
  1970. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1971. sector_size = old_sector_size;
  1972. goto got_data;
  1973. }
  1974. }
  1975. }
  1976. /* Some devices are known to return the total number of blocks,
  1977. * not the highest block number. Some devices have versions
  1978. * which do this and others which do not. Some devices we might
  1979. * suspect of doing this but we don't know for certain.
  1980. *
  1981. * If we know the reported capacity is wrong, decrement it. If
  1982. * we can only guess, then assume the number of blocks is even
  1983. * (usually true but not always) and err on the side of lowering
  1984. * the capacity.
  1985. */
  1986. if (sdp->fix_capacity ||
  1987. (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
  1988. sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
  1989. "from its reported value: %llu\n",
  1990. (unsigned long long) sdkp->capacity);
  1991. --sdkp->capacity;
  1992. }
  1993. got_data:
  1994. if (sector_size == 0) {
  1995. sector_size = 512;
  1996. sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  1997. "assuming 512.\n");
  1998. }
  1999. if (sector_size != 512 &&
  2000. sector_size != 1024 &&
  2001. sector_size != 2048 &&
  2002. sector_size != 4096) {
  2003. sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  2004. sector_size);
  2005. /*
  2006. * The user might want to re-format the drive with
  2007. * a supported sectorsize. Once this happens, it
  2008. * would be relatively trivial to set the thing up.
  2009. * For this reason, we leave the thing in the table.
  2010. */
  2011. sdkp->capacity = 0;
  2012. /*
  2013. * set a bogus sector size so the normal read/write
  2014. * logic in the block layer will eventually refuse any
  2015. * request on this device without tripping over power
  2016. * of two sector size assumptions
  2017. */
  2018. sector_size = 512;
  2019. }
  2020. blk_queue_logical_block_size(sdp->request_queue, sector_size);
  2021. {
  2022. char cap_str_2[10], cap_str_10[10];
  2023. string_get_size(sdkp->capacity, sector_size,
  2024. STRING_UNITS_2, cap_str_2, sizeof(cap_str_2));
  2025. string_get_size(sdkp->capacity, sector_size,
  2026. STRING_UNITS_10, cap_str_10,
  2027. sizeof(cap_str_10));
  2028. if (sdkp->first_scan || old_capacity != sdkp->capacity) {
  2029. sd_printk(KERN_NOTICE, sdkp,
  2030. "%llu %d-byte logical blocks: (%s/%s)\n",
  2031. (unsigned long long)sdkp->capacity,
  2032. sector_size, cap_str_10, cap_str_2);
  2033. if (sdkp->physical_block_size != sector_size)
  2034. sd_printk(KERN_NOTICE, sdkp,
  2035. "%u-byte physical blocks\n",
  2036. sdkp->physical_block_size);
  2037. }
  2038. }
  2039. if (sdkp->capacity > 0xffffffff)
  2040. sdp->use_16_for_rw = 1;
  2041. blk_queue_physical_block_size(sdp->request_queue,
  2042. sdkp->physical_block_size);
  2043. sdkp->device->sector_size = sector_size;
  2044. }
  2045. /* called with buffer of length 512 */
  2046. static inline int
  2047. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  2048. unsigned char *buffer, int len, struct scsi_mode_data *data,
  2049. struct scsi_sense_hdr *sshdr)
  2050. {
  2051. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  2052. SD_TIMEOUT, SD_MAX_RETRIES, data,
  2053. sshdr);
  2054. }
  2055. /*
  2056. * read write protect setting, if possible - called only in sd_revalidate_disk()
  2057. * called with buffer of length SD_BUF_SIZE
  2058. */
  2059. static void
  2060. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  2061. {
  2062. int res;
  2063. struct scsi_device *sdp = sdkp->device;
  2064. struct scsi_mode_data data;
  2065. int old_wp = sdkp->write_prot;
  2066. set_disk_ro(sdkp->disk, 0);
  2067. if (sdp->skip_ms_page_3f) {
  2068. sd_first_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  2069. return;
  2070. }
  2071. if (sdp->use_192_bytes_for_3f) {
  2072. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  2073. } else {
  2074. /*
  2075. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  2076. * We have to start carefully: some devices hang if we ask
  2077. * for more than is available.
  2078. */
  2079. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  2080. /*
  2081. * Second attempt: ask for page 0 When only page 0 is
  2082. * implemented, a request for page 3F may return Sense Key
  2083. * 5: Illegal Request, Sense Code 24: Invalid field in
  2084. * CDB.
  2085. */
  2086. if (!scsi_status_is_good(res))
  2087. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  2088. /*
  2089. * Third attempt: ask 255 bytes, as we did earlier.
  2090. */
  2091. if (!scsi_status_is_good(res))
  2092. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  2093. &data, NULL);
  2094. }
  2095. if (!scsi_status_is_good(res)) {
  2096. sd_first_printk(KERN_WARNING, sdkp,
  2097. "Test WP failed, assume Write Enabled\n");
  2098. } else {
  2099. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  2100. set_disk_ro(sdkp->disk, sdkp->write_prot);
  2101. if (sdkp->first_scan || old_wp != sdkp->write_prot) {
  2102. sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  2103. sdkp->write_prot ? "on" : "off");
  2104. sd_printk(KERN_DEBUG, sdkp,
  2105. "Mode Sense: %02x %02x %02x %02x\n",
  2106. buffer[0], buffer[1], buffer[2], buffer[3]);
  2107. }
  2108. }
  2109. }
  2110. /*
  2111. * sd_read_cache_type - called only from sd_revalidate_disk()
  2112. * called with buffer of length SD_BUF_SIZE
  2113. */
  2114. static void
  2115. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  2116. {
  2117. int len = 0, res;
  2118. struct scsi_device *sdp = sdkp->device;
  2119. int dbd;
  2120. int modepage;
  2121. int first_len;
  2122. struct scsi_mode_data data;
  2123. struct scsi_sense_hdr sshdr;
  2124. int old_wce = sdkp->WCE;
  2125. int old_rcd = sdkp->RCD;
  2126. int old_dpofua = sdkp->DPOFUA;
  2127. if (sdkp->cache_override)
  2128. return;
  2129. first_len = 4;
  2130. if (sdp->skip_ms_page_8) {
  2131. if (sdp->type == TYPE_RBC)
  2132. goto defaults;
  2133. else {
  2134. if (sdp->skip_ms_page_3f)
  2135. goto defaults;
  2136. modepage = 0x3F;
  2137. if (sdp->use_192_bytes_for_3f)
  2138. first_len = 192;
  2139. dbd = 0;
  2140. }
  2141. } else if (sdp->type == TYPE_RBC) {
  2142. modepage = 6;
  2143. dbd = 8;
  2144. } else {
  2145. modepage = 8;
  2146. dbd = 0;
  2147. }
  2148. /* cautiously ask */
  2149. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, first_len,
  2150. &data, &sshdr);
  2151. if (!scsi_status_is_good(res))
  2152. goto bad_sense;
  2153. if (!data.header_length) {
  2154. modepage = 6;
  2155. first_len = 0;
  2156. sd_first_printk(KERN_ERR, sdkp,
  2157. "Missing header in MODE_SENSE response\n");
  2158. }
  2159. /* that went OK, now ask for the proper length */
  2160. len = data.length;
  2161. /*
  2162. * We're only interested in the first three bytes, actually.
  2163. * But the data cache page is defined for the first 20.
  2164. */
  2165. if (len < 3)
  2166. goto bad_sense;
  2167. else if (len > SD_BUF_SIZE) {
  2168. sd_first_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
  2169. "data from %d to %d bytes\n", len, SD_BUF_SIZE);
  2170. len = SD_BUF_SIZE;
  2171. }
  2172. if (modepage == 0x3F && sdp->use_192_bytes_for_3f)
  2173. len = 192;
  2174. /* Get the data */
  2175. if (len > first_len)
  2176. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len,
  2177. &data, &sshdr);
  2178. if (scsi_status_is_good(res)) {
  2179. int offset = data.header_length + data.block_descriptor_length;
  2180. while (offset < len) {
  2181. u8 page_code = buffer[offset] & 0x3F;
  2182. u8 spf = buffer[offset] & 0x40;
  2183. if (page_code == 8 || page_code == 6) {
  2184. /* We're interested only in the first 3 bytes.
  2185. */
  2186. if (len - offset <= 2) {
  2187. sd_first_printk(KERN_ERR, sdkp,
  2188. "Incomplete mode parameter "
  2189. "data\n");
  2190. goto defaults;
  2191. } else {
  2192. modepage = page_code;
  2193. goto Page_found;
  2194. }
  2195. } else {
  2196. /* Go to the next page */
  2197. if (spf && len - offset > 3)
  2198. offset += 4 + (buffer[offset+2] << 8) +
  2199. buffer[offset+3];
  2200. else if (!spf && len - offset > 1)
  2201. offset += 2 + buffer[offset+1];
  2202. else {
  2203. sd_first_printk(KERN_ERR, sdkp,
  2204. "Incomplete mode "
  2205. "parameter data\n");
  2206. goto defaults;
  2207. }
  2208. }
  2209. }
  2210. sd_first_printk(KERN_ERR, sdkp, "No Caching mode page found\n");
  2211. goto defaults;
  2212. Page_found:
  2213. if (modepage == 8) {
  2214. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  2215. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  2216. } else {
  2217. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  2218. sdkp->RCD = 0;
  2219. }
  2220. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  2221. if (sdp->broken_fua) {
  2222. sd_first_printk(KERN_NOTICE, sdkp, "Disabling FUA\n");
  2223. sdkp->DPOFUA = 0;
  2224. } else if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw &&
  2225. !sdkp->device->use_16_for_rw) {
  2226. sd_first_printk(KERN_NOTICE, sdkp,
  2227. "Uses READ/WRITE(6), disabling FUA\n");
  2228. sdkp->DPOFUA = 0;
  2229. }
  2230. /* No cache flush allowed for write protected devices */
  2231. if (sdkp->WCE && sdkp->write_prot)
  2232. sdkp->WCE = 0;
  2233. if (sdkp->first_scan || old_wce != sdkp->WCE ||
  2234. old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
  2235. sd_printk(KERN_NOTICE, sdkp,
  2236. "Write cache: %s, read cache: %s, %s\n",
  2237. sdkp->WCE ? "enabled" : "disabled",
  2238. sdkp->RCD ? "disabled" : "enabled",
  2239. sdkp->DPOFUA ? "supports DPO and FUA"
  2240. : "doesn't support DPO or FUA");
  2241. return;
  2242. }
  2243. bad_sense:
  2244. if (scsi_sense_valid(&sshdr) &&
  2245. sshdr.sense_key == ILLEGAL_REQUEST &&
  2246. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  2247. /* Invalid field in CDB */
  2248. sd_first_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  2249. else
  2250. sd_first_printk(KERN_ERR, sdkp,
  2251. "Asking for cache data failed\n");
  2252. defaults:
  2253. if (sdp->wce_default_on) {
  2254. sd_first_printk(KERN_NOTICE, sdkp,
  2255. "Assuming drive cache: write back\n");
  2256. sdkp->WCE = 1;
  2257. } else {
  2258. sd_first_printk(KERN_ERR, sdkp,
  2259. "Assuming drive cache: write through\n");
  2260. sdkp->WCE = 0;
  2261. }
  2262. sdkp->RCD = 0;
  2263. sdkp->DPOFUA = 0;
  2264. }
  2265. /*
  2266. * The ATO bit indicates whether the DIF application tag is available
  2267. * for use by the operating system.
  2268. */
  2269. static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
  2270. {
  2271. int res, offset;
  2272. struct scsi_device *sdp = sdkp->device;
  2273. struct scsi_mode_data data;
  2274. struct scsi_sense_hdr sshdr;
  2275. if (sdp->type != TYPE_DISK)
  2276. return;
  2277. if (sdkp->protection_type == 0)
  2278. return;
  2279. res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
  2280. SD_MAX_RETRIES, &data, &sshdr);
  2281. if (!scsi_status_is_good(res) || !data.header_length ||
  2282. data.length < 6) {
  2283. sd_first_printk(KERN_WARNING, sdkp,
  2284. "getting Control mode page failed, assume no ATO\n");
  2285. if (scsi_sense_valid(&sshdr))
  2286. sd_print_sense_hdr(sdkp, &sshdr);
  2287. return;
  2288. }
  2289. offset = data.header_length + data.block_descriptor_length;
  2290. if ((buffer[offset] & 0x3f) != 0x0a) {
  2291. sd_first_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
  2292. return;
  2293. }
  2294. if ((buffer[offset + 5] & 0x80) == 0)
  2295. return;
  2296. sdkp->ATO = 1;
  2297. return;
  2298. }
  2299. /**
  2300. * sd_read_block_limits - Query disk device for preferred I/O sizes.
  2301. * @disk: disk to query
  2302. */
  2303. static void sd_read_block_limits(struct scsi_disk *sdkp)
  2304. {
  2305. unsigned int sector_sz = sdkp->device->sector_size;
  2306. const int vpd_len = 64;
  2307. unsigned char *buffer = kmalloc(vpd_len, GFP_KERNEL);
  2308. if (!buffer ||
  2309. /* Block Limits VPD */
  2310. scsi_get_vpd_page(sdkp->device, 0xb0, buffer, vpd_len))
  2311. goto out;
  2312. blk_queue_io_min(sdkp->disk->queue,
  2313. get_unaligned_be16(&buffer[6]) * sector_sz);
  2314. sdkp->max_xfer_blocks = get_unaligned_be32(&buffer[8]);
  2315. sdkp->opt_xfer_blocks = get_unaligned_be32(&buffer[12]);
  2316. if (buffer[3] == 0x3c) {
  2317. unsigned int lba_count, desc_count;
  2318. sdkp->max_ws_blocks = (u32)get_unaligned_be64(&buffer[36]);
  2319. if (!sdkp->lbpme)
  2320. goto out;
  2321. lba_count = get_unaligned_be32(&buffer[20]);
  2322. desc_count = get_unaligned_be32(&buffer[24]);
  2323. if (lba_count && desc_count)
  2324. sdkp->max_unmap_blocks = lba_count;
  2325. sdkp->unmap_granularity = get_unaligned_be32(&buffer[28]);
  2326. if (buffer[32] & 0x80)
  2327. sdkp->unmap_alignment =
  2328. get_unaligned_be32(&buffer[32]) & ~(1 << 31);
  2329. if (!sdkp->lbpvpd) { /* LBP VPD page not provided */
  2330. if (sdkp->max_unmap_blocks)
  2331. sd_config_discard(sdkp, SD_LBP_UNMAP);
  2332. else
  2333. sd_config_discard(sdkp, SD_LBP_WS16);
  2334. } else { /* LBP VPD page tells us what to use */
  2335. if (sdkp->lbpu && sdkp->max_unmap_blocks && !sdkp->lbprz)
  2336. sd_config_discard(sdkp, SD_LBP_UNMAP);
  2337. else if (sdkp->lbpws)
  2338. sd_config_discard(sdkp, SD_LBP_WS16);
  2339. else if (sdkp->lbpws10)
  2340. sd_config_discard(sdkp, SD_LBP_WS10);
  2341. else if (sdkp->lbpu && sdkp->max_unmap_blocks)
  2342. sd_config_discard(sdkp, SD_LBP_UNMAP);
  2343. else
  2344. sd_config_discard(sdkp, SD_LBP_DISABLE);
  2345. }
  2346. }
  2347. out:
  2348. kfree(buffer);
  2349. }
  2350. /**
  2351. * sd_read_block_characteristics - Query block dev. characteristics
  2352. * @disk: disk to query
  2353. */
  2354. static void sd_read_block_characteristics(struct scsi_disk *sdkp)
  2355. {
  2356. unsigned char *buffer;
  2357. u16 rot;
  2358. const int vpd_len = 64;
  2359. buffer = kmalloc(vpd_len, GFP_KERNEL);
  2360. if (!buffer ||
  2361. /* Block Device Characteristics VPD */
  2362. scsi_get_vpd_page(sdkp->device, 0xb1, buffer, vpd_len))
  2363. goto out;
  2364. rot = get_unaligned_be16(&buffer[4]);
  2365. if (rot == 1) {
  2366. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
  2367. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, sdkp->disk->queue);
  2368. }
  2369. out:
  2370. kfree(buffer);
  2371. }
  2372. /**
  2373. * sd_read_block_provisioning - Query provisioning VPD page
  2374. * @disk: disk to query
  2375. */
  2376. static void sd_read_block_provisioning(struct scsi_disk *sdkp)
  2377. {
  2378. unsigned char *buffer;
  2379. const int vpd_len = 8;
  2380. if (sdkp->lbpme == 0)
  2381. return;
  2382. buffer = kmalloc(vpd_len, GFP_KERNEL);
  2383. if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
  2384. goto out;
  2385. sdkp->lbpvpd = 1;
  2386. sdkp->lbpu = (buffer[5] >> 7) & 1; /* UNMAP */
  2387. sdkp->lbpws = (buffer[5] >> 6) & 1; /* WRITE SAME(16) with UNMAP */
  2388. sdkp->lbpws10 = (buffer[5] >> 5) & 1; /* WRITE SAME(10) with UNMAP */
  2389. out:
  2390. kfree(buffer);
  2391. }
  2392. static void sd_read_write_same(struct scsi_disk *sdkp, unsigned char *buffer)
  2393. {
  2394. struct scsi_device *sdev = sdkp->device;
  2395. if (sdev->host->no_write_same) {
  2396. sdev->no_write_same = 1;
  2397. return;
  2398. }
  2399. if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, INQUIRY) < 0) {
  2400. /* too large values might cause issues with arcmsr */
  2401. int vpd_buf_len = 64;
  2402. sdev->no_report_opcodes = 1;
  2403. /* Disable WRITE SAME if REPORT SUPPORTED OPERATION
  2404. * CODES is unsupported and the device has an ATA
  2405. * Information VPD page (SAT).
  2406. */
  2407. if (!scsi_get_vpd_page(sdev, 0x89, buffer, vpd_buf_len))
  2408. sdev->no_write_same = 1;
  2409. }
  2410. if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME_16) == 1)
  2411. sdkp->ws16 = 1;
  2412. if (scsi_report_opcode(sdev, buffer, SD_BUF_SIZE, WRITE_SAME) == 1)
  2413. sdkp->ws10 = 1;
  2414. }
  2415. /**
  2416. * sd_revalidate_disk - called the first time a new disk is seen,
  2417. * performs disk spin up, read_capacity, etc.
  2418. * @disk: struct gendisk we care about
  2419. **/
  2420. static int sd_revalidate_disk(struct gendisk *disk)
  2421. {
  2422. struct scsi_disk *sdkp = scsi_disk(disk);
  2423. struct scsi_device *sdp = sdkp->device;
  2424. struct request_queue *q = sdkp->disk->queue;
  2425. unsigned char *buffer;
  2426. unsigned int dev_max, rw_max;
  2427. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
  2428. "sd_revalidate_disk\n"));
  2429. /*
  2430. * If the device is offline, don't try and read capacity or any
  2431. * of the other niceties.
  2432. */
  2433. if (!scsi_device_online(sdp))
  2434. goto out;
  2435. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
  2436. if (!buffer) {
  2437. sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  2438. "allocation failure.\n");
  2439. goto out;
  2440. }
  2441. sd_spinup_disk(sdkp);
  2442. /*
  2443. * Without media there is no reason to ask; moreover, some devices
  2444. * react badly if we do.
  2445. */
  2446. if (sdkp->media_present) {
  2447. sd_read_capacity(sdkp, buffer);
  2448. if (scsi_device_supports_vpd(sdp)) {
  2449. sd_read_block_provisioning(sdkp);
  2450. sd_read_block_limits(sdkp);
  2451. sd_read_block_characteristics(sdkp);
  2452. }
  2453. sd_read_write_protect_flag(sdkp, buffer);
  2454. sd_read_cache_type(sdkp, buffer);
  2455. sd_read_app_tag_own(sdkp, buffer);
  2456. sd_read_write_same(sdkp, buffer);
  2457. }
  2458. /*
  2459. * We now have all cache related info, determine how we deal
  2460. * with flush requests.
  2461. */
  2462. sd_set_flush_flag(sdkp);
  2463. /* Initial block count limit based on CDB TRANSFER LENGTH field size. */
  2464. dev_max = sdp->use_16_for_rw ? SD_MAX_XFER_BLOCKS : SD_DEF_XFER_BLOCKS;
  2465. /* Some devices report a maximum block count for READ/WRITE requests. */
  2466. dev_max = min_not_zero(dev_max, sdkp->max_xfer_blocks);
  2467. q->limits.max_dev_sectors = logical_to_sectors(sdp, dev_max);
  2468. /*
  2469. * Determine the device's preferred I/O size for reads and writes
  2470. * unless the reported value is unreasonably small, large, or
  2471. * garbage.
  2472. */
  2473. if (sdkp->opt_xfer_blocks &&
  2474. sdkp->opt_xfer_blocks <= dev_max &&
  2475. sdkp->opt_xfer_blocks <= SD_DEF_XFER_BLOCKS &&
  2476. logical_to_bytes(sdp, sdkp->opt_xfer_blocks) >= PAGE_SIZE) {
  2477. q->limits.io_opt = logical_to_bytes(sdp, sdkp->opt_xfer_blocks);
  2478. rw_max = logical_to_sectors(sdp, sdkp->opt_xfer_blocks);
  2479. } else
  2480. rw_max = min_not_zero(logical_to_sectors(sdp, dev_max),
  2481. (sector_t)BLK_DEF_MAX_SECTORS);
  2482. /* Do not exceed controller limit */
  2483. rw_max = min(rw_max, queue_max_hw_sectors(q));
  2484. /*
  2485. * Only update max_sectors if previously unset or if the current value
  2486. * exceeds the capabilities of the hardware.
  2487. */
  2488. if (sdkp->first_scan ||
  2489. q->limits.max_sectors > q->limits.max_dev_sectors ||
  2490. q->limits.max_sectors > q->limits.max_hw_sectors)
  2491. q->limits.max_sectors = rw_max;
  2492. sdkp->first_scan = 0;
  2493. set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
  2494. sd_config_write_same(sdkp);
  2495. kfree(buffer);
  2496. out:
  2497. return 0;
  2498. }
  2499. /**
  2500. * sd_unlock_native_capacity - unlock native capacity
  2501. * @disk: struct gendisk to set capacity for
  2502. *
  2503. * Block layer calls this function if it detects that partitions
  2504. * on @disk reach beyond the end of the device. If the SCSI host
  2505. * implements ->unlock_native_capacity() method, it's invoked to
  2506. * give it a chance to adjust the device capacity.
  2507. *
  2508. * CONTEXT:
  2509. * Defined by block layer. Might sleep.
  2510. */
  2511. static void sd_unlock_native_capacity(struct gendisk *disk)
  2512. {
  2513. struct scsi_device *sdev = scsi_disk(disk)->device;
  2514. if (sdev->host->hostt->unlock_native_capacity)
  2515. sdev->host->hostt->unlock_native_capacity(sdev);
  2516. }
  2517. /**
  2518. * sd_format_disk_name - format disk name
  2519. * @prefix: name prefix - ie. "sd" for SCSI disks
  2520. * @index: index of the disk to format name for
  2521. * @buf: output buffer
  2522. * @buflen: length of the output buffer
  2523. *
  2524. * SCSI disk names starts at sda. The 26th device is sdz and the
  2525. * 27th is sdaa. The last one for two lettered suffix is sdzz
  2526. * which is followed by sdaaa.
  2527. *
  2528. * This is basically 26 base counting with one extra 'nil' entry
  2529. * at the beginning from the second digit on and can be
  2530. * determined using similar method as 26 base conversion with the
  2531. * index shifted -1 after each digit is computed.
  2532. *
  2533. * CONTEXT:
  2534. * Don't care.
  2535. *
  2536. * RETURNS:
  2537. * 0 on success, -errno on failure.
  2538. */
  2539. static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  2540. {
  2541. const int base = 'z' - 'a' + 1;
  2542. char *begin = buf + strlen(prefix);
  2543. char *end = buf + buflen;
  2544. char *p;
  2545. int unit;
  2546. p = end - 1;
  2547. *p = '\0';
  2548. unit = base;
  2549. do {
  2550. if (p == begin)
  2551. return -EINVAL;
  2552. *--p = 'a' + (index % unit);
  2553. index = (index / unit) - 1;
  2554. } while (index >= 0);
  2555. memmove(begin, p, end - p);
  2556. memcpy(buf, prefix, strlen(prefix));
  2557. return 0;
  2558. }
  2559. /*
  2560. * The asynchronous part of sd_probe
  2561. */
  2562. static void sd_probe_async(void *data, async_cookie_t cookie)
  2563. {
  2564. struct scsi_disk *sdkp = data;
  2565. struct scsi_device *sdp;
  2566. struct gendisk *gd;
  2567. u32 index;
  2568. struct device *dev;
  2569. sdp = sdkp->device;
  2570. gd = sdkp->disk;
  2571. index = sdkp->index;
  2572. dev = &sdp->sdev_gendev;
  2573. gd->major = sd_major((index & 0xf0) >> 4);
  2574. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  2575. gd->minors = SD_MINORS;
  2576. gd->fops = &sd_fops;
  2577. gd->private_data = &sdkp->driver;
  2578. gd->queue = sdkp->device->request_queue;
  2579. /* defaults, until the device tells us otherwise */
  2580. sdp->sector_size = 512;
  2581. sdkp->capacity = 0;
  2582. sdkp->media_present = 1;
  2583. sdkp->write_prot = 0;
  2584. sdkp->cache_override = 0;
  2585. sdkp->WCE = 0;
  2586. sdkp->RCD = 0;
  2587. sdkp->ATO = 0;
  2588. sdkp->first_scan = 1;
  2589. sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
  2590. sd_revalidate_disk(gd);
  2591. gd->flags = GENHD_FL_EXT_DEVT;
  2592. if (sdp->removable) {
  2593. gd->flags |= GENHD_FL_REMOVABLE;
  2594. gd->events |= DISK_EVENT_MEDIA_CHANGE;
  2595. }
  2596. blk_pm_runtime_init(sdp->request_queue, dev);
  2597. device_add_disk(dev, gd);
  2598. if (sdkp->capacity)
  2599. sd_dif_config_host(sdkp);
  2600. sd_revalidate_disk(gd);
  2601. sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  2602. sdp->removable ? "removable " : "");
  2603. scsi_autopm_put_device(sdp);
  2604. put_device(&sdkp->dev);
  2605. }
  2606. /**
  2607. * sd_probe - called during driver initialization and whenever a
  2608. * new scsi device is attached to the system. It is called once
  2609. * for each scsi device (not just disks) present.
  2610. * @dev: pointer to device object
  2611. *
  2612. * Returns 0 if successful (or not interested in this scsi device
  2613. * (e.g. scanner)); 1 when there is an error.
  2614. *
  2615. * Note: this function is invoked from the scsi mid-level.
  2616. * This function sets up the mapping between a given
  2617. * <host,channel,id,lun> (found in sdp) and new device name
  2618. * (e.g. /dev/sda). More precisely it is the block device major
  2619. * and minor number that is chosen here.
  2620. *
  2621. * Assume sd_probe is not re-entrant (for time being)
  2622. * Also think about sd_probe() and sd_remove() running coincidentally.
  2623. **/
  2624. static int sd_probe(struct device *dev)
  2625. {
  2626. struct scsi_device *sdp = to_scsi_device(dev);
  2627. struct scsi_disk *sdkp;
  2628. struct gendisk *gd;
  2629. int index;
  2630. int error;
  2631. scsi_autopm_get_device(sdp);
  2632. error = -ENODEV;
  2633. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  2634. goto out;
  2635. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  2636. "sd_probe\n"));
  2637. error = -ENOMEM;
  2638. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  2639. if (!sdkp)
  2640. goto out;
  2641. gd = alloc_disk(SD_MINORS);
  2642. if (!gd)
  2643. goto out_free;
  2644. do {
  2645. if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
  2646. goto out_put;
  2647. spin_lock(&sd_index_lock);
  2648. error = ida_get_new(&sd_index_ida, &index);
  2649. spin_unlock(&sd_index_lock);
  2650. } while (error == -EAGAIN);
  2651. if (error) {
  2652. sdev_printk(KERN_WARNING, sdp, "sd_probe: memory exhausted.\n");
  2653. goto out_put;
  2654. }
  2655. error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  2656. if (error) {
  2657. sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name length exceeded.\n");
  2658. goto out_free_index;
  2659. }
  2660. sdkp->device = sdp;
  2661. sdkp->driver = &sd_template;
  2662. sdkp->disk = gd;
  2663. sdkp->index = index;
  2664. atomic_set(&sdkp->openers, 0);
  2665. atomic_set(&sdkp->device->ioerr_cnt, 0);
  2666. if (!sdp->request_queue->rq_timeout) {
  2667. if (sdp->type != TYPE_MOD)
  2668. blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  2669. else
  2670. blk_queue_rq_timeout(sdp->request_queue,
  2671. SD_MOD_TIMEOUT);
  2672. }
  2673. device_initialize(&sdkp->dev);
  2674. sdkp->dev.parent = dev;
  2675. sdkp->dev.class = &sd_disk_class;
  2676. dev_set_name(&sdkp->dev, "%s", dev_name(dev));
  2677. error = device_add(&sdkp->dev);
  2678. if (error)
  2679. goto out_free_index;
  2680. get_device(dev);
  2681. dev_set_drvdata(dev, sdkp);
  2682. get_device(&sdkp->dev); /* prevent release before async_schedule */
  2683. async_schedule_domain(sd_probe_async, sdkp, &scsi_sd_probe_domain);
  2684. return 0;
  2685. out_free_index:
  2686. spin_lock(&sd_index_lock);
  2687. ida_remove(&sd_index_ida, index);
  2688. spin_unlock(&sd_index_lock);
  2689. out_put:
  2690. put_disk(gd);
  2691. out_free:
  2692. kfree(sdkp);
  2693. out:
  2694. scsi_autopm_put_device(sdp);
  2695. return error;
  2696. }
  2697. /**
  2698. * sd_remove - called whenever a scsi disk (previously recognized by
  2699. * sd_probe) is detached from the system. It is called (potentially
  2700. * multiple times) during sd module unload.
  2701. * @sdp: pointer to mid level scsi device object
  2702. *
  2703. * Note: this function is invoked from the scsi mid-level.
  2704. * This function potentially frees up a device name (e.g. /dev/sdc)
  2705. * that could be re-used by a subsequent sd_probe().
  2706. * This function is not called when the built-in sd driver is "exit-ed".
  2707. **/
  2708. static int sd_remove(struct device *dev)
  2709. {
  2710. struct scsi_disk *sdkp;
  2711. dev_t devt;
  2712. sdkp = dev_get_drvdata(dev);
  2713. devt = disk_devt(sdkp->disk);
  2714. scsi_autopm_get_device(sdkp->device);
  2715. async_synchronize_full_domain(&scsi_sd_pm_domain);
  2716. async_synchronize_full_domain(&scsi_sd_probe_domain);
  2717. device_del(&sdkp->dev);
  2718. del_gendisk(sdkp->disk);
  2719. sd_shutdown(dev);
  2720. blk_register_region(devt, SD_MINORS, NULL,
  2721. sd_default_probe, NULL, NULL);
  2722. mutex_lock(&sd_ref_mutex);
  2723. dev_set_drvdata(dev, NULL);
  2724. put_device(&sdkp->dev);
  2725. mutex_unlock(&sd_ref_mutex);
  2726. return 0;
  2727. }
  2728. /**
  2729. * scsi_disk_release - Called to free the scsi_disk structure
  2730. * @dev: pointer to embedded class device
  2731. *
  2732. * sd_ref_mutex must be held entering this routine. Because it is
  2733. * called on last put, you should always use the scsi_disk_get()
  2734. * scsi_disk_put() helpers which manipulate the semaphore directly
  2735. * and never do a direct put_device.
  2736. **/
  2737. static void scsi_disk_release(struct device *dev)
  2738. {
  2739. struct scsi_disk *sdkp = to_scsi_disk(dev);
  2740. struct gendisk *disk = sdkp->disk;
  2741. spin_lock(&sd_index_lock);
  2742. ida_remove(&sd_index_ida, sdkp->index);
  2743. spin_unlock(&sd_index_lock);
  2744. disk->private_data = NULL;
  2745. put_disk(disk);
  2746. put_device(&sdkp->device->sdev_gendev);
  2747. kfree(sdkp);
  2748. }
  2749. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  2750. {
  2751. unsigned char cmd[6] = { START_STOP }; /* START_VALID */
  2752. struct scsi_sense_hdr sshdr;
  2753. struct scsi_device *sdp = sdkp->device;
  2754. int res;
  2755. if (start)
  2756. cmd[4] |= 1; /* START */
  2757. if (sdp->start_stop_pwr_cond)
  2758. cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
  2759. if (!scsi_device_online(sdp))
  2760. return -ENODEV;
  2761. res = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  2762. SD_TIMEOUT, SD_MAX_RETRIES, NULL, REQ_PM);
  2763. if (res) {
  2764. sd_print_result(sdkp, "Start/Stop Unit failed", res);
  2765. if (driver_byte(res) & DRIVER_SENSE)
  2766. sd_print_sense_hdr(sdkp, &sshdr);
  2767. if (scsi_sense_valid(&sshdr) &&
  2768. /* 0x3a is medium not present */
  2769. sshdr.asc == 0x3a)
  2770. res = 0;
  2771. }
  2772. /* SCSI error codes must not go to the generic layer */
  2773. if (res)
  2774. return -EIO;
  2775. return 0;
  2776. }
  2777. /*
  2778. * Send a SYNCHRONIZE CACHE instruction down to the device through
  2779. * the normal SCSI command structure. Wait for the command to
  2780. * complete.
  2781. */
  2782. static void sd_shutdown(struct device *dev)
  2783. {
  2784. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  2785. if (!sdkp)
  2786. return; /* this can happen */
  2787. if (pm_runtime_suspended(dev))
  2788. return;
  2789. if (sdkp->WCE && sdkp->media_present) {
  2790. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2791. sd_sync_cache(sdkp);
  2792. }
  2793. if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  2794. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2795. sd_start_stop_device(sdkp, 0);
  2796. }
  2797. }
  2798. static int sd_suspend_common(struct device *dev, bool ignore_stop_errors)
  2799. {
  2800. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  2801. int ret = 0;
  2802. if (!sdkp) /* E.g.: runtime suspend following sd_remove() */
  2803. return 0;
  2804. if (sdkp->WCE && sdkp->media_present) {
  2805. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2806. ret = sd_sync_cache(sdkp);
  2807. if (ret) {
  2808. /* ignore OFFLINE device */
  2809. if (ret == -ENODEV)
  2810. ret = 0;
  2811. goto done;
  2812. }
  2813. }
  2814. if (sdkp->device->manage_start_stop) {
  2815. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2816. /* an error is not worth aborting a system sleep */
  2817. ret = sd_start_stop_device(sdkp, 0);
  2818. if (ignore_stop_errors)
  2819. ret = 0;
  2820. }
  2821. done:
  2822. return ret;
  2823. }
  2824. static int sd_suspend_system(struct device *dev)
  2825. {
  2826. return sd_suspend_common(dev, true);
  2827. }
  2828. static int sd_suspend_runtime(struct device *dev)
  2829. {
  2830. return sd_suspend_common(dev, false);
  2831. }
  2832. static int sd_resume(struct device *dev)
  2833. {
  2834. struct scsi_disk *sdkp = dev_get_drvdata(dev);
  2835. if (!sdkp) /* E.g.: runtime resume at the start of sd_probe() */
  2836. return 0;
  2837. if (!sdkp->device->manage_start_stop)
  2838. return 0;
  2839. sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  2840. return sd_start_stop_device(sdkp, 1);
  2841. }
  2842. /**
  2843. * init_sd - entry point for this driver (both when built in or when
  2844. * a module).
  2845. *
  2846. * Note: this function registers this driver with the scsi mid-level.
  2847. **/
  2848. static int __init init_sd(void)
  2849. {
  2850. int majors = 0, i, err;
  2851. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  2852. for (i = 0; i < SD_MAJORS; i++) {
  2853. if (register_blkdev(sd_major(i), "sd") != 0)
  2854. continue;
  2855. majors++;
  2856. blk_register_region(sd_major(i), SD_MINORS, NULL,
  2857. sd_default_probe, NULL, NULL);
  2858. }
  2859. if (!majors)
  2860. return -ENODEV;
  2861. err = class_register(&sd_disk_class);
  2862. if (err)
  2863. goto err_out;
  2864. sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  2865. 0, 0, NULL);
  2866. if (!sd_cdb_cache) {
  2867. printk(KERN_ERR "sd: can't init extended cdb cache\n");
  2868. err = -ENOMEM;
  2869. goto err_out_class;
  2870. }
  2871. sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  2872. if (!sd_cdb_pool) {
  2873. printk(KERN_ERR "sd: can't init extended cdb pool\n");
  2874. err = -ENOMEM;
  2875. goto err_out_cache;
  2876. }
  2877. err = scsi_register_driver(&sd_template.gendrv);
  2878. if (err)
  2879. goto err_out_driver;
  2880. return 0;
  2881. err_out_driver:
  2882. mempool_destroy(sd_cdb_pool);
  2883. err_out_cache:
  2884. kmem_cache_destroy(sd_cdb_cache);
  2885. err_out_class:
  2886. class_unregister(&sd_disk_class);
  2887. err_out:
  2888. for (i = 0; i < SD_MAJORS; i++)
  2889. unregister_blkdev(sd_major(i), "sd");
  2890. return err;
  2891. }
  2892. /**
  2893. * exit_sd - exit point for this driver (when it is a module).
  2894. *
  2895. * Note: this function unregisters this driver from the scsi mid-level.
  2896. **/
  2897. static void __exit exit_sd(void)
  2898. {
  2899. int i;
  2900. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  2901. scsi_unregister_driver(&sd_template.gendrv);
  2902. mempool_destroy(sd_cdb_pool);
  2903. kmem_cache_destroy(sd_cdb_cache);
  2904. class_unregister(&sd_disk_class);
  2905. for (i = 0; i < SD_MAJORS; i++) {
  2906. blk_unregister_region(sd_major(i), SD_MINORS);
  2907. unregister_blkdev(sd_major(i), "sd");
  2908. }
  2909. }
  2910. module_init(init_sd);
  2911. module_exit(exit_sd);
  2912. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  2913. struct scsi_sense_hdr *sshdr)
  2914. {
  2915. scsi_print_sense_hdr(sdkp->device,
  2916. sdkp->disk ? sdkp->disk->disk_name : NULL, sshdr);
  2917. }
  2918. static void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
  2919. int result)
  2920. {
  2921. const char *hb_string = scsi_hostbyte_string(result);
  2922. const char *db_string = scsi_driverbyte_string(result);
  2923. if (hb_string || db_string)
  2924. sd_printk(KERN_INFO, sdkp,
  2925. "%s: Result: hostbyte=%s driverbyte=%s\n", msg,
  2926. hb_string ? hb_string : "invalid",
  2927. db_string ? db_string : "invalid");
  2928. else
  2929. sd_printk(KERN_INFO, sdkp,
  2930. "%s: Result: hostbyte=0x%02x driverbyte=0x%02x\n",
  2931. msg, host_byte(result), driver_byte(result));
  2932. }