sd.c 88 KB

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