sd.c 91 KB

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