sd.c 84 KB

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