sd.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  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 <asm/uaccess.h>
  52. #include <asm/unaligned.h>
  53. #include <scsi/scsi.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include <scsi/scsi_device.h>
  57. #include <scsi/scsi_driver.h>
  58. #include <scsi/scsi_eh.h>
  59. #include <scsi/scsi_host.h>
  60. #include <scsi/scsi_ioctl.h>
  61. #include <scsi/scsicam.h>
  62. #include "sd.h"
  63. #include "scsi_logging.h"
  64. MODULE_AUTHOR("Eric Youngdale");
  65. MODULE_DESCRIPTION("SCSI disk (sd) driver");
  66. MODULE_LICENSE("GPL");
  67. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
  68. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
  69. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
  70. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
  71. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
  72. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
  73. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
  74. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
  76. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
  77. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
  78. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
  79. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
  80. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
  81. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
  82. MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
  83. MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
  84. MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
  85. MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
  86. #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
  87. #define SD_MINORS 16
  88. #else
  89. #define SD_MINORS 0
  90. #endif
  91. static int sd_revalidate_disk(struct gendisk *);
  92. static int sd_probe(struct device *);
  93. static int sd_remove(struct device *);
  94. static void sd_shutdown(struct device *);
  95. static int sd_suspend(struct device *, pm_message_t state);
  96. static int sd_resume(struct device *);
  97. static void sd_rescan(struct device *);
  98. static int sd_done(struct scsi_cmnd *);
  99. static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
  100. static void scsi_disk_release(struct device *cdev);
  101. static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
  102. static void sd_print_result(struct scsi_disk *, int);
  103. static DEFINE_SPINLOCK(sd_index_lock);
  104. static DEFINE_IDA(sd_index_ida);
  105. /* This semaphore is used to mediate the 0->1 reference get in the
  106. * face of object destruction (i.e. we can't allow a get on an
  107. * object after last put) */
  108. static DEFINE_MUTEX(sd_ref_mutex);
  109. struct kmem_cache *sd_cdb_cache;
  110. mempool_t *sd_cdb_pool;
  111. static const char *sd_cache_types[] = {
  112. "write through", "none", "write back",
  113. "write back, no read (daft)"
  114. };
  115. static ssize_t
  116. sd_store_cache_type(struct device *dev, struct device_attribute *attr,
  117. const char *buf, size_t count)
  118. {
  119. int i, ct = -1, rcd, wce, sp;
  120. struct scsi_disk *sdkp = to_scsi_disk(dev);
  121. struct scsi_device *sdp = sdkp->device;
  122. char buffer[64];
  123. char *buffer_data;
  124. struct scsi_mode_data data;
  125. struct scsi_sense_hdr sshdr;
  126. int len;
  127. if (sdp->type != TYPE_DISK)
  128. /* no cache control on RBC devices; theoretically they
  129. * can do it, but there's probably so many exceptions
  130. * it's not worth the risk */
  131. return -EINVAL;
  132. for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
  133. const int len = strlen(sd_cache_types[i]);
  134. if (strncmp(sd_cache_types[i], buf, len) == 0 &&
  135. buf[len] == '\n') {
  136. ct = i;
  137. break;
  138. }
  139. }
  140. if (ct < 0)
  141. return -EINVAL;
  142. rcd = ct & 0x01 ? 1 : 0;
  143. wce = ct & 0x02 ? 1 : 0;
  144. if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
  145. SD_MAX_RETRIES, &data, NULL))
  146. return -EINVAL;
  147. len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
  148. data.block_descriptor_length);
  149. buffer_data = buffer + data.header_length +
  150. data.block_descriptor_length;
  151. buffer_data[2] &= ~0x05;
  152. buffer_data[2] |= wce << 2 | rcd;
  153. sp = buffer_data[0] & 0x80 ? 1 : 0;
  154. if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
  155. SD_MAX_RETRIES, &data, &sshdr)) {
  156. if (scsi_sense_valid(&sshdr))
  157. sd_print_sense_hdr(sdkp, &sshdr);
  158. return -EINVAL;
  159. }
  160. revalidate_disk(sdkp->disk);
  161. return count;
  162. }
  163. static ssize_t
  164. sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
  165. const char *buf, size_t count)
  166. {
  167. struct scsi_disk *sdkp = to_scsi_disk(dev);
  168. struct scsi_device *sdp = sdkp->device;
  169. if (!capable(CAP_SYS_ADMIN))
  170. return -EACCES;
  171. sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
  172. return count;
  173. }
  174. static ssize_t
  175. sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
  176. const char *buf, size_t count)
  177. {
  178. struct scsi_disk *sdkp = to_scsi_disk(dev);
  179. struct scsi_device *sdp = sdkp->device;
  180. if (!capable(CAP_SYS_ADMIN))
  181. return -EACCES;
  182. if (sdp->type != TYPE_DISK)
  183. return -EINVAL;
  184. sdp->allow_restart = simple_strtoul(buf, NULL, 10);
  185. return count;
  186. }
  187. static ssize_t
  188. sd_show_cache_type(struct device *dev, struct device_attribute *attr,
  189. char *buf)
  190. {
  191. struct scsi_disk *sdkp = to_scsi_disk(dev);
  192. int ct = sdkp->RCD + 2*sdkp->WCE;
  193. return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
  194. }
  195. static ssize_t
  196. sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
  197. {
  198. struct scsi_disk *sdkp = to_scsi_disk(dev);
  199. return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
  200. }
  201. static ssize_t
  202. sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
  203. char *buf)
  204. {
  205. struct scsi_disk *sdkp = to_scsi_disk(dev);
  206. struct scsi_device *sdp = sdkp->device;
  207. return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
  208. }
  209. static ssize_t
  210. sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
  211. char *buf)
  212. {
  213. struct scsi_disk *sdkp = to_scsi_disk(dev);
  214. return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
  215. }
  216. static ssize_t
  217. sd_show_protection_type(struct device *dev, struct device_attribute *attr,
  218. char *buf)
  219. {
  220. struct scsi_disk *sdkp = to_scsi_disk(dev);
  221. return snprintf(buf, 20, "%u\n", sdkp->protection_type);
  222. }
  223. static ssize_t
  224. sd_show_app_tag_own(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->ATO);
  229. }
  230. static ssize_t
  231. sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
  232. char *buf)
  233. {
  234. struct scsi_disk *sdkp = to_scsi_disk(dev);
  235. return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning);
  236. }
  237. static struct device_attribute sd_disk_attrs[] = {
  238. __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
  239. sd_store_cache_type),
  240. __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
  241. __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
  242. sd_store_allow_restart),
  243. __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
  244. sd_store_manage_start_stop),
  245. __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
  246. __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
  247. __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
  248. __ATTR_NULL,
  249. };
  250. static struct class sd_disk_class = {
  251. .name = "scsi_disk",
  252. .owner = THIS_MODULE,
  253. .dev_release = scsi_disk_release,
  254. .dev_attrs = sd_disk_attrs,
  255. };
  256. static struct scsi_driver sd_template = {
  257. .owner = THIS_MODULE,
  258. .gendrv = {
  259. .name = "sd",
  260. .probe = sd_probe,
  261. .remove = sd_remove,
  262. .suspend = sd_suspend,
  263. .resume = sd_resume,
  264. .shutdown = sd_shutdown,
  265. },
  266. .rescan = sd_rescan,
  267. .done = sd_done,
  268. };
  269. /*
  270. * Device no to disk mapping:
  271. *
  272. * major disc2 disc p1
  273. * |............|.............|....|....| <- dev_t
  274. * 31 20 19 8 7 4 3 0
  275. *
  276. * Inside a major, we have 16k disks, however mapped non-
  277. * contiguously. The first 16 disks are for major0, the next
  278. * ones with major1, ... Disk 256 is for major0 again, disk 272
  279. * for major1, ...
  280. * As we stay compatible with our numbering scheme, we can reuse
  281. * the well-know SCSI majors 8, 65--71, 136--143.
  282. */
  283. static int sd_major(int major_idx)
  284. {
  285. switch (major_idx) {
  286. case 0:
  287. return SCSI_DISK0_MAJOR;
  288. case 1 ... 7:
  289. return SCSI_DISK1_MAJOR + major_idx - 1;
  290. case 8 ... 15:
  291. return SCSI_DISK8_MAJOR + major_idx - 8;
  292. default:
  293. BUG();
  294. return 0; /* shut up gcc */
  295. }
  296. }
  297. static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
  298. {
  299. struct scsi_disk *sdkp = NULL;
  300. if (disk->private_data) {
  301. sdkp = scsi_disk(disk);
  302. if (scsi_device_get(sdkp->device) == 0)
  303. get_device(&sdkp->dev);
  304. else
  305. sdkp = NULL;
  306. }
  307. return sdkp;
  308. }
  309. static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
  310. {
  311. struct scsi_disk *sdkp;
  312. mutex_lock(&sd_ref_mutex);
  313. sdkp = __scsi_disk_get(disk);
  314. mutex_unlock(&sd_ref_mutex);
  315. return sdkp;
  316. }
  317. static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
  318. {
  319. struct scsi_disk *sdkp;
  320. mutex_lock(&sd_ref_mutex);
  321. sdkp = dev_get_drvdata(dev);
  322. if (sdkp)
  323. sdkp = __scsi_disk_get(sdkp->disk);
  324. mutex_unlock(&sd_ref_mutex);
  325. return sdkp;
  326. }
  327. static void scsi_disk_put(struct scsi_disk *sdkp)
  328. {
  329. struct scsi_device *sdev = sdkp->device;
  330. mutex_lock(&sd_ref_mutex);
  331. put_device(&sdkp->dev);
  332. scsi_device_put(sdev);
  333. mutex_unlock(&sd_ref_mutex);
  334. }
  335. static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
  336. {
  337. unsigned int prot_op = SCSI_PROT_NORMAL;
  338. unsigned int dix = scsi_prot_sg_count(scmd);
  339. if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
  340. if (dif && dix)
  341. prot_op = SCSI_PROT_READ_PASS;
  342. else if (dif && !dix)
  343. prot_op = SCSI_PROT_READ_STRIP;
  344. else if (!dif && dix)
  345. prot_op = SCSI_PROT_READ_INSERT;
  346. } else {
  347. if (dif && dix)
  348. prot_op = SCSI_PROT_WRITE_PASS;
  349. else if (dif && !dix)
  350. prot_op = SCSI_PROT_WRITE_INSERT;
  351. else if (!dif && dix)
  352. prot_op = SCSI_PROT_WRITE_STRIP;
  353. }
  354. scsi_set_prot_op(scmd, prot_op);
  355. scsi_set_prot_type(scmd, dif);
  356. }
  357. /**
  358. * sd_prepare_discard - unmap blocks on thinly provisioned device
  359. * @rq: Request to prepare
  360. *
  361. * Will issue either UNMAP or WRITE SAME(16) depending on preference
  362. * indicated by target device.
  363. **/
  364. static int sd_prepare_discard(struct request *rq)
  365. {
  366. struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
  367. struct bio *bio = rq->bio;
  368. sector_t sector = bio->bi_sector;
  369. unsigned int num = bio_sectors(bio);
  370. if (sdkp->device->sector_size == 4096) {
  371. sector >>= 3;
  372. num >>= 3;
  373. }
  374. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  375. rq->timeout = SD_TIMEOUT;
  376. memset(rq->cmd, 0, rq->cmd_len);
  377. if (sdkp->unmap) {
  378. char *buf = kmap_atomic(bio_page(bio), KM_USER0);
  379. rq->cmd[0] = UNMAP;
  380. rq->cmd[8] = 24;
  381. rq->cmd_len = 10;
  382. /* Ensure that data length matches payload */
  383. rq->__data_len = bio->bi_size = bio->bi_io_vec->bv_len = 24;
  384. put_unaligned_be16(6 + 16, &buf[0]);
  385. put_unaligned_be16(16, &buf[2]);
  386. put_unaligned_be64(sector, &buf[8]);
  387. put_unaligned_be32(num, &buf[16]);
  388. kunmap_atomic(buf, KM_USER0);
  389. } else {
  390. rq->cmd[0] = WRITE_SAME_16;
  391. rq->cmd[1] = 0x8; /* UNMAP */
  392. put_unaligned_be64(sector, &rq->cmd[2]);
  393. put_unaligned_be32(num, &rq->cmd[10]);
  394. rq->cmd_len = 16;
  395. }
  396. return BLKPREP_OK;
  397. }
  398. /**
  399. * sd_init_command - build a scsi (read or write) command from
  400. * information in the request structure.
  401. * @SCpnt: pointer to mid-level's per scsi command structure that
  402. * contains request and into which the scsi command is written
  403. *
  404. * Returns 1 if successful and 0 if error (or cannot be done now).
  405. **/
  406. static int sd_prep_fn(struct request_queue *q, struct request *rq)
  407. {
  408. struct scsi_cmnd *SCpnt;
  409. struct scsi_device *sdp = q->queuedata;
  410. struct gendisk *disk = rq->rq_disk;
  411. struct scsi_disk *sdkp;
  412. sector_t block = blk_rq_pos(rq);
  413. sector_t threshold;
  414. unsigned int this_count = blk_rq_sectors(rq);
  415. int ret, host_dif;
  416. unsigned char protect;
  417. /*
  418. * Discard request come in as REQ_TYPE_FS but we turn them into
  419. * block PC requests to make life easier.
  420. */
  421. if (blk_discard_rq(rq))
  422. ret = sd_prepare_discard(rq);
  423. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  424. ret = scsi_setup_blk_pc_cmnd(sdp, rq);
  425. goto out;
  426. } else if (rq->cmd_type != REQ_TYPE_FS) {
  427. ret = BLKPREP_KILL;
  428. goto out;
  429. }
  430. ret = scsi_setup_fs_cmnd(sdp, rq);
  431. if (ret != BLKPREP_OK)
  432. goto out;
  433. SCpnt = rq->special;
  434. sdkp = scsi_disk(disk);
  435. /* from here on until we're complete, any goto out
  436. * is used for a killable error condition */
  437. ret = BLKPREP_KILL;
  438. SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
  439. "sd_init_command: block=%llu, "
  440. "count=%d\n",
  441. (unsigned long long)block,
  442. this_count));
  443. if (!sdp || !scsi_device_online(sdp) ||
  444. block + blk_rq_sectors(rq) > get_capacity(disk)) {
  445. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  446. "Finishing %u sectors\n",
  447. blk_rq_sectors(rq)));
  448. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  449. "Retry with 0x%p\n", SCpnt));
  450. goto out;
  451. }
  452. if (sdp->changed) {
  453. /*
  454. * quietly refuse to do anything to a changed disc until
  455. * the changed bit has been reset
  456. */
  457. /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
  458. goto out;
  459. }
  460. /*
  461. * Some SD card readers can't handle multi-sector accesses which touch
  462. * the last one or two hardware sectors. Split accesses as needed.
  463. */
  464. threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
  465. (sdp->sector_size / 512);
  466. if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
  467. if (block < threshold) {
  468. /* Access up to the threshold but not beyond */
  469. this_count = threshold - block;
  470. } else {
  471. /* Access only a single hardware sector */
  472. this_count = sdp->sector_size / 512;
  473. }
  474. }
  475. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
  476. (unsigned long long)block));
  477. /*
  478. * If we have a 1K hardware sectorsize, prevent access to single
  479. * 512 byte sectors. In theory we could handle this - in fact
  480. * the scsi cdrom driver must be able to handle this because
  481. * we typically use 1K blocksizes, and cdroms typically have
  482. * 2K hardware sectorsizes. Of course, things are simpler
  483. * with the cdrom, since it is read-only. For performance
  484. * reasons, the filesystems should be able to handle this
  485. * and not force the scsi disk driver to use bounce buffers
  486. * for this.
  487. */
  488. if (sdp->sector_size == 1024) {
  489. if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
  490. scmd_printk(KERN_ERR, SCpnt,
  491. "Bad block number requested\n");
  492. goto out;
  493. } else {
  494. block = block >> 1;
  495. this_count = this_count >> 1;
  496. }
  497. }
  498. if (sdp->sector_size == 2048) {
  499. if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
  500. scmd_printk(KERN_ERR, SCpnt,
  501. "Bad block number requested\n");
  502. goto out;
  503. } else {
  504. block = block >> 2;
  505. this_count = this_count >> 2;
  506. }
  507. }
  508. if (sdp->sector_size == 4096) {
  509. if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
  510. scmd_printk(KERN_ERR, SCpnt,
  511. "Bad block number requested\n");
  512. goto out;
  513. } else {
  514. block = block >> 3;
  515. this_count = this_count >> 3;
  516. }
  517. }
  518. if (rq_data_dir(rq) == WRITE) {
  519. if (!sdp->writeable) {
  520. goto out;
  521. }
  522. SCpnt->cmnd[0] = WRITE_6;
  523. SCpnt->sc_data_direction = DMA_TO_DEVICE;
  524. if (blk_integrity_rq(rq) &&
  525. sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
  526. goto out;
  527. } else if (rq_data_dir(rq) == READ) {
  528. SCpnt->cmnd[0] = READ_6;
  529. SCpnt->sc_data_direction = DMA_FROM_DEVICE;
  530. } else {
  531. scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
  532. goto out;
  533. }
  534. SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
  535. "%s %d/%u 512 byte blocks.\n",
  536. (rq_data_dir(rq) == WRITE) ?
  537. "writing" : "reading", this_count,
  538. blk_rq_sectors(rq)));
  539. /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
  540. host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
  541. if (host_dif)
  542. protect = 1 << 5;
  543. else
  544. protect = 0;
  545. if (host_dif == SD_DIF_TYPE2_PROTECTION) {
  546. SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
  547. if (unlikely(SCpnt->cmnd == NULL)) {
  548. ret = BLKPREP_DEFER;
  549. goto out;
  550. }
  551. SCpnt->cmd_len = SD_EXT_CDB_SIZE;
  552. memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
  553. SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
  554. SCpnt->cmnd[7] = 0x18;
  555. SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
  556. SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  557. /* LBA */
  558. SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  559. SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  560. SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  561. SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  562. SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
  563. SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
  564. SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
  565. SCpnt->cmnd[19] = (unsigned char) block & 0xff;
  566. /* Expected Indirect LBA */
  567. SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
  568. SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
  569. SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
  570. SCpnt->cmnd[23] = (unsigned char) block & 0xff;
  571. /* Transfer length */
  572. SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
  573. SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
  574. SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
  575. SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
  576. } else if (block > 0xffffffff) {
  577. SCpnt->cmnd[0] += READ_16 - READ_6;
  578. SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  579. SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
  580. SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
  581. SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
  582. SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
  583. SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
  584. SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
  585. SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
  586. SCpnt->cmnd[9] = (unsigned char) block & 0xff;
  587. SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
  588. SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
  589. SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
  590. SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
  591. SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
  592. } else if ((this_count > 0xff) || (block > 0x1fffff) ||
  593. scsi_device_protection(SCpnt->device) ||
  594. SCpnt->device->use_10_for_rw) {
  595. if (this_count > 0xffff)
  596. this_count = 0xffff;
  597. SCpnt->cmnd[0] += READ_10 - READ_6;
  598. SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
  599. SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
  600. SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
  601. SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
  602. SCpnt->cmnd[5] = (unsigned char) block & 0xff;
  603. SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
  604. SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
  605. SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
  606. } else {
  607. if (unlikely(blk_fua_rq(rq))) {
  608. /*
  609. * This happens only if this drive failed
  610. * 10byte rw command with ILLEGAL_REQUEST
  611. * during operation and thus turned off
  612. * use_10_for_rw.
  613. */
  614. scmd_printk(KERN_ERR, SCpnt,
  615. "FUA write on READ/WRITE(6) drive\n");
  616. goto out;
  617. }
  618. SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
  619. SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
  620. SCpnt->cmnd[3] = (unsigned char) block & 0xff;
  621. SCpnt->cmnd[4] = (unsigned char) this_count;
  622. SCpnt->cmnd[5] = 0;
  623. }
  624. SCpnt->sdb.length = this_count * sdp->sector_size;
  625. /* If DIF or DIX is enabled, tell HBA how to handle request */
  626. if (host_dif || scsi_prot_sg_count(SCpnt))
  627. sd_prot_op(SCpnt, host_dif);
  628. /*
  629. * We shouldn't disconnect in the middle of a sector, so with a dumb
  630. * host adapter, it's safe to assume that we can at least transfer
  631. * this many bytes between each connect / disconnect.
  632. */
  633. SCpnt->transfersize = sdp->sector_size;
  634. SCpnt->underflow = this_count << 9;
  635. SCpnt->allowed = SD_MAX_RETRIES;
  636. /*
  637. * This indicates that the command is ready from our end to be
  638. * queued.
  639. */
  640. ret = BLKPREP_OK;
  641. out:
  642. return scsi_prep_return(q, rq, ret);
  643. }
  644. /**
  645. * sd_open - open a scsi disk device
  646. * @inode: only i_rdev member may be used
  647. * @filp: only f_mode and f_flags may be used
  648. *
  649. * Returns 0 if successful. Returns a negated errno value in case
  650. * of error.
  651. *
  652. * Note: This can be called from a user context (e.g. fsck(1) )
  653. * or from within the kernel (e.g. as a result of a mount(1) ).
  654. * In the latter case @inode and @filp carry an abridged amount
  655. * of information as noted above.
  656. **/
  657. static int sd_open(struct block_device *bdev, fmode_t mode)
  658. {
  659. struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
  660. struct scsi_device *sdev;
  661. int retval;
  662. if (!sdkp)
  663. return -ENXIO;
  664. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
  665. sdev = sdkp->device;
  666. /*
  667. * If the device is in error recovery, wait until it is done.
  668. * If the device is offline, then disallow any access to it.
  669. */
  670. retval = -ENXIO;
  671. if (!scsi_block_when_processing_errors(sdev))
  672. goto error_out;
  673. if (sdev->removable || sdkp->write_prot)
  674. check_disk_change(bdev);
  675. /*
  676. * If the drive is empty, just let the open fail.
  677. */
  678. retval = -ENOMEDIUM;
  679. if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
  680. goto error_out;
  681. /*
  682. * If the device has the write protect tab set, have the open fail
  683. * if the user expects to be able to write to the thing.
  684. */
  685. retval = -EROFS;
  686. if (sdkp->write_prot && (mode & FMODE_WRITE))
  687. goto error_out;
  688. /*
  689. * It is possible that the disk changing stuff resulted in
  690. * the device being taken offline. If this is the case,
  691. * report this to the user, and don't pretend that the
  692. * open actually succeeded.
  693. */
  694. retval = -ENXIO;
  695. if (!scsi_device_online(sdev))
  696. goto error_out;
  697. if (!sdkp->openers++ && sdev->removable) {
  698. if (scsi_block_when_processing_errors(sdev))
  699. scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
  700. }
  701. return 0;
  702. error_out:
  703. scsi_disk_put(sdkp);
  704. return retval;
  705. }
  706. /**
  707. * sd_release - invoked when the (last) close(2) is called on this
  708. * scsi disk.
  709. * @inode: only i_rdev member may be used
  710. * @filp: only f_mode and f_flags may be used
  711. *
  712. * Returns 0.
  713. *
  714. * Note: may block (uninterruptible) if error recovery is underway
  715. * on this disk.
  716. **/
  717. static int sd_release(struct gendisk *disk, fmode_t mode)
  718. {
  719. struct scsi_disk *sdkp = scsi_disk(disk);
  720. struct scsi_device *sdev = sdkp->device;
  721. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
  722. if (!--sdkp->openers && sdev->removable) {
  723. if (scsi_block_when_processing_errors(sdev))
  724. scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
  725. }
  726. /*
  727. * XXX and what if there are packets in flight and this close()
  728. * XXX is followed by a "rmmod sd_mod"?
  729. */
  730. scsi_disk_put(sdkp);
  731. return 0;
  732. }
  733. static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  734. {
  735. struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
  736. struct scsi_device *sdp = sdkp->device;
  737. struct Scsi_Host *host = sdp->host;
  738. int diskinfo[4];
  739. /* default to most commonly used values */
  740. diskinfo[0] = 0x40; /* 1 << 6 */
  741. diskinfo[1] = 0x20; /* 1 << 5 */
  742. diskinfo[2] = sdkp->capacity >> 11;
  743. /* override with calculated, extended default, or driver values */
  744. if (host->hostt->bios_param)
  745. host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
  746. else
  747. scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
  748. geo->heads = diskinfo[0];
  749. geo->sectors = diskinfo[1];
  750. geo->cylinders = diskinfo[2];
  751. return 0;
  752. }
  753. /**
  754. * sd_ioctl - process an ioctl
  755. * @inode: only i_rdev/i_bdev members may be used
  756. * @filp: only f_mode and f_flags may be used
  757. * @cmd: ioctl command number
  758. * @arg: this is third argument given to ioctl(2) system call.
  759. * Often contains a pointer.
  760. *
  761. * Returns 0 if successful (some ioctls return postive numbers on
  762. * success as well). Returns a negated errno value in case of error.
  763. *
  764. * Note: most ioctls are forward onto the block subsystem or further
  765. * down in the scsi subsystem.
  766. **/
  767. static int sd_ioctl(struct block_device *bdev, fmode_t mode,
  768. unsigned int cmd, unsigned long arg)
  769. {
  770. struct gendisk *disk = bdev->bd_disk;
  771. struct scsi_device *sdp = scsi_disk(disk)->device;
  772. void __user *p = (void __user *)arg;
  773. int error;
  774. SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
  775. disk->disk_name, cmd));
  776. /*
  777. * If we are in the middle of error recovery, don't let anyone
  778. * else try and use this device. Also, if error recovery fails, it
  779. * may try and take the device offline, in which case all further
  780. * access to the device is prohibited.
  781. */
  782. error = scsi_nonblockable_ioctl(sdp, cmd, p,
  783. (mode & FMODE_NDELAY) != 0);
  784. if (!scsi_block_when_processing_errors(sdp) || !error)
  785. return error;
  786. /*
  787. * Send SCSI addressing ioctls directly to mid level, send other
  788. * ioctls to block level and then onto mid level if they can't be
  789. * resolved.
  790. */
  791. switch (cmd) {
  792. case SCSI_IOCTL_GET_IDLUN:
  793. case SCSI_IOCTL_GET_BUS_NUMBER:
  794. return scsi_ioctl(sdp, cmd, p);
  795. default:
  796. error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
  797. if (error != -ENOTTY)
  798. return error;
  799. }
  800. return scsi_ioctl(sdp, cmd, p);
  801. }
  802. static void set_media_not_present(struct scsi_disk *sdkp)
  803. {
  804. sdkp->media_present = 0;
  805. sdkp->capacity = 0;
  806. sdkp->device->changed = 1;
  807. }
  808. /**
  809. * sd_media_changed - check if our medium changed
  810. * @disk: kernel device descriptor
  811. *
  812. * Returns 0 if not applicable or no change; 1 if change
  813. *
  814. * Note: this function is invoked from the block subsystem.
  815. **/
  816. static int sd_media_changed(struct gendisk *disk)
  817. {
  818. struct scsi_disk *sdkp = scsi_disk(disk);
  819. struct scsi_device *sdp = sdkp->device;
  820. struct scsi_sense_hdr *sshdr = NULL;
  821. int retval;
  822. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
  823. if (!sdp->removable)
  824. return 0;
  825. /*
  826. * If the device is offline, don't send any commands - just pretend as
  827. * if the command failed. If the device ever comes back online, we
  828. * can deal with it then. It is only because of unrecoverable errors
  829. * that we would ever take a device offline in the first place.
  830. */
  831. if (!scsi_device_online(sdp)) {
  832. set_media_not_present(sdkp);
  833. retval = 1;
  834. goto out;
  835. }
  836. /*
  837. * Using TEST_UNIT_READY enables differentiation between drive with
  838. * no cartridge loaded - NOT READY, drive with changed cartridge -
  839. * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
  840. *
  841. * Drives that auto spin down. eg iomega jaz 1G, will be started
  842. * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
  843. * sd_revalidate() is called.
  844. */
  845. retval = -ENODEV;
  846. if (scsi_block_when_processing_errors(sdp)) {
  847. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  848. retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
  849. sshdr);
  850. }
  851. /*
  852. * Unable to test, unit probably not ready. This usually
  853. * means there is no disc in the drive. Mark as changed,
  854. * and we will figure it out later once the drive is
  855. * available again.
  856. */
  857. if (retval || (scsi_sense_valid(sshdr) &&
  858. /* 0x3a is medium not present */
  859. sshdr->asc == 0x3a)) {
  860. set_media_not_present(sdkp);
  861. retval = 1;
  862. goto out;
  863. }
  864. /*
  865. * For removable scsi disk we have to recognise the presence
  866. * of a disk in the drive. This is kept in the struct scsi_disk
  867. * struct and tested at open ! Daniel Roche (dan@lectra.fr)
  868. */
  869. sdkp->media_present = 1;
  870. retval = sdp->changed;
  871. sdp->changed = 0;
  872. out:
  873. if (retval != sdkp->previous_state)
  874. sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
  875. sdkp->previous_state = retval;
  876. kfree(sshdr);
  877. return retval;
  878. }
  879. static int sd_sync_cache(struct scsi_disk *sdkp)
  880. {
  881. int retries, res;
  882. struct scsi_device *sdp = sdkp->device;
  883. struct scsi_sense_hdr sshdr;
  884. if (!scsi_device_online(sdp))
  885. return -ENODEV;
  886. for (retries = 3; retries > 0; --retries) {
  887. unsigned char cmd[10] = { 0 };
  888. cmd[0] = SYNCHRONIZE_CACHE;
  889. /*
  890. * Leave the rest of the command zero to indicate
  891. * flush everything.
  892. */
  893. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  894. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  895. if (res == 0)
  896. break;
  897. }
  898. if (res) {
  899. sd_print_result(sdkp, res);
  900. if (driver_byte(res) & DRIVER_SENSE)
  901. sd_print_sense_hdr(sdkp, &sshdr);
  902. }
  903. if (res)
  904. return -EIO;
  905. return 0;
  906. }
  907. static void sd_prepare_flush(struct request_queue *q, struct request *rq)
  908. {
  909. rq->cmd_type = REQ_TYPE_BLOCK_PC;
  910. rq->timeout = SD_TIMEOUT;
  911. rq->cmd[0] = SYNCHRONIZE_CACHE;
  912. rq->cmd_len = 10;
  913. }
  914. static void sd_rescan(struct device *dev)
  915. {
  916. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  917. if (sdkp) {
  918. revalidate_disk(sdkp->disk);
  919. scsi_disk_put(sdkp);
  920. }
  921. }
  922. #ifdef CONFIG_COMPAT
  923. /*
  924. * This gets directly called from VFS. When the ioctl
  925. * is not recognized we go back to the other translation paths.
  926. */
  927. static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
  928. unsigned int cmd, unsigned long arg)
  929. {
  930. struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
  931. /*
  932. * If we are in the middle of error recovery, don't let anyone
  933. * else try and use this device. Also, if error recovery fails, it
  934. * may try and take the device offline, in which case all further
  935. * access to the device is prohibited.
  936. */
  937. if (!scsi_block_when_processing_errors(sdev))
  938. return -ENODEV;
  939. if (sdev->host->hostt->compat_ioctl) {
  940. int ret;
  941. ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
  942. return ret;
  943. }
  944. /*
  945. * Let the static ioctl translation table take care of it.
  946. */
  947. return -ENOIOCTLCMD;
  948. }
  949. #endif
  950. static const struct block_device_operations sd_fops = {
  951. .owner = THIS_MODULE,
  952. .open = sd_open,
  953. .release = sd_release,
  954. .locked_ioctl = sd_ioctl,
  955. .getgeo = sd_getgeo,
  956. #ifdef CONFIG_COMPAT
  957. .compat_ioctl = sd_compat_ioctl,
  958. #endif
  959. .media_changed = sd_media_changed,
  960. .revalidate_disk = sd_revalidate_disk,
  961. };
  962. static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
  963. {
  964. u64 start_lba = blk_rq_pos(scmd->request);
  965. u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
  966. u64 bad_lba;
  967. int info_valid;
  968. if (!blk_fs_request(scmd->request))
  969. return 0;
  970. info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
  971. SCSI_SENSE_BUFFERSIZE,
  972. &bad_lba);
  973. if (!info_valid)
  974. return 0;
  975. if (scsi_bufflen(scmd) <= scmd->device->sector_size)
  976. return 0;
  977. if (scmd->device->sector_size < 512) {
  978. /* only legitimate sector_size here is 256 */
  979. start_lba <<= 1;
  980. end_lba <<= 1;
  981. } else {
  982. /* be careful ... don't want any overflows */
  983. u64 factor = scmd->device->sector_size / 512;
  984. do_div(start_lba, factor);
  985. do_div(end_lba, factor);
  986. }
  987. /* The bad lba was reported incorrectly, we have no idea where
  988. * the error is.
  989. */
  990. if (bad_lba < start_lba || bad_lba >= end_lba)
  991. return 0;
  992. /* This computation should always be done in terms of
  993. * the resolution of the device's medium.
  994. */
  995. return (bad_lba - start_lba) * scmd->device->sector_size;
  996. }
  997. /**
  998. * sd_done - bottom half handler: called when the lower level
  999. * driver has completed (successfully or otherwise) a scsi command.
  1000. * @SCpnt: mid-level's per command structure.
  1001. *
  1002. * Note: potentially run from within an ISR. Must not block.
  1003. **/
  1004. static int sd_done(struct scsi_cmnd *SCpnt)
  1005. {
  1006. int result = SCpnt->result;
  1007. unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
  1008. struct scsi_sense_hdr sshdr;
  1009. struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
  1010. int sense_valid = 0;
  1011. int sense_deferred = 0;
  1012. if (result) {
  1013. sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
  1014. if (sense_valid)
  1015. sense_deferred = scsi_sense_is_deferred(&sshdr);
  1016. }
  1017. #ifdef CONFIG_SCSI_LOGGING
  1018. SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
  1019. if (sense_valid) {
  1020. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
  1021. "sd_done: sb[respc,sk,asc,"
  1022. "ascq]=%x,%x,%x,%x\n",
  1023. sshdr.response_code,
  1024. sshdr.sense_key, sshdr.asc,
  1025. sshdr.ascq));
  1026. }
  1027. #endif
  1028. if (driver_byte(result) != DRIVER_SENSE &&
  1029. (!sense_valid || sense_deferred))
  1030. goto out;
  1031. switch (sshdr.sense_key) {
  1032. case HARDWARE_ERROR:
  1033. case MEDIUM_ERROR:
  1034. good_bytes = sd_completed_bytes(SCpnt);
  1035. break;
  1036. case RECOVERED_ERROR:
  1037. good_bytes = scsi_bufflen(SCpnt);
  1038. break;
  1039. case NO_SENSE:
  1040. /* This indicates a false check condition, so ignore it. An
  1041. * unknown amount of data was transferred so treat it as an
  1042. * error.
  1043. */
  1044. scsi_print_sense("sd", SCpnt);
  1045. SCpnt->result = 0;
  1046. memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  1047. break;
  1048. case ABORTED_COMMAND:
  1049. if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */
  1050. scsi_print_result(SCpnt);
  1051. scsi_print_sense("sd", SCpnt);
  1052. good_bytes = sd_completed_bytes(SCpnt);
  1053. }
  1054. break;
  1055. case ILLEGAL_REQUEST:
  1056. if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */
  1057. scsi_print_result(SCpnt);
  1058. scsi_print_sense("sd", SCpnt);
  1059. good_bytes = sd_completed_bytes(SCpnt);
  1060. }
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. out:
  1066. if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
  1067. sd_dif_complete(SCpnt, good_bytes);
  1068. if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
  1069. == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd)
  1070. mempool_free(SCpnt->cmnd, sd_cdb_pool);
  1071. return good_bytes;
  1072. }
  1073. static int media_not_present(struct scsi_disk *sdkp,
  1074. struct scsi_sense_hdr *sshdr)
  1075. {
  1076. if (!scsi_sense_valid(sshdr))
  1077. return 0;
  1078. /* not invoked for commands that could return deferred errors */
  1079. if (sshdr->sense_key != NOT_READY &&
  1080. sshdr->sense_key != UNIT_ATTENTION)
  1081. return 0;
  1082. if (sshdr->asc != 0x3A) /* medium not present */
  1083. return 0;
  1084. set_media_not_present(sdkp);
  1085. return 1;
  1086. }
  1087. /*
  1088. * spinup disk - called only in sd_revalidate_disk()
  1089. */
  1090. static void
  1091. sd_spinup_disk(struct scsi_disk *sdkp)
  1092. {
  1093. unsigned char cmd[10];
  1094. unsigned long spintime_expire = 0;
  1095. int retries, spintime;
  1096. unsigned int the_result;
  1097. struct scsi_sense_hdr sshdr;
  1098. int sense_valid = 0;
  1099. spintime = 0;
  1100. /* Spin up drives, as required. Only do this at boot time */
  1101. /* Spinup needs to be done for module loads too. */
  1102. do {
  1103. retries = 0;
  1104. do {
  1105. cmd[0] = TEST_UNIT_READY;
  1106. memset((void *) &cmd[1], 0, 9);
  1107. the_result = scsi_execute_req(sdkp->device, cmd,
  1108. DMA_NONE, NULL, 0,
  1109. &sshdr, SD_TIMEOUT,
  1110. SD_MAX_RETRIES, NULL);
  1111. /*
  1112. * If the drive has indicated to us that it
  1113. * doesn't have any media in it, don't bother
  1114. * with any more polling.
  1115. */
  1116. if (media_not_present(sdkp, &sshdr))
  1117. return;
  1118. if (the_result)
  1119. sense_valid = scsi_sense_valid(&sshdr);
  1120. retries++;
  1121. } while (retries < 3 &&
  1122. (!scsi_status_is_good(the_result) ||
  1123. ((driver_byte(the_result) & DRIVER_SENSE) &&
  1124. sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
  1125. if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
  1126. /* no sense, TUR either succeeded or failed
  1127. * with a status error */
  1128. if(!spintime && !scsi_status_is_good(the_result)) {
  1129. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1130. sd_print_result(sdkp, the_result);
  1131. }
  1132. break;
  1133. }
  1134. /*
  1135. * The device does not want the automatic start to be issued.
  1136. */
  1137. if (sdkp->device->no_start_on_add)
  1138. break;
  1139. if (sense_valid && sshdr.sense_key == NOT_READY) {
  1140. if (sshdr.asc == 4 && sshdr.ascq == 3)
  1141. break; /* manual intervention required */
  1142. if (sshdr.asc == 4 && sshdr.ascq == 0xb)
  1143. break; /* standby */
  1144. if (sshdr.asc == 4 && sshdr.ascq == 0xc)
  1145. break; /* unavailable */
  1146. /*
  1147. * Issue command to spin up drive when not ready
  1148. */
  1149. if (!spintime) {
  1150. sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
  1151. cmd[0] = START_STOP;
  1152. cmd[1] = 1; /* Return immediately */
  1153. memset((void *) &cmd[2], 0, 8);
  1154. cmd[4] = 1; /* Start spin cycle */
  1155. if (sdkp->device->start_stop_pwr_cond)
  1156. cmd[4] |= 1 << 4;
  1157. scsi_execute_req(sdkp->device, cmd, DMA_NONE,
  1158. NULL, 0, &sshdr,
  1159. SD_TIMEOUT, SD_MAX_RETRIES,
  1160. NULL);
  1161. spintime_expire = jiffies + 100 * HZ;
  1162. spintime = 1;
  1163. }
  1164. /* Wait 1 second for next try */
  1165. msleep(1000);
  1166. printk(".");
  1167. /*
  1168. * Wait for USB flash devices with slow firmware.
  1169. * Yes, this sense key/ASC combination shouldn't
  1170. * occur here. It's characteristic of these devices.
  1171. */
  1172. } else if (sense_valid &&
  1173. sshdr.sense_key == UNIT_ATTENTION &&
  1174. sshdr.asc == 0x28) {
  1175. if (!spintime) {
  1176. spintime_expire = jiffies + 5 * HZ;
  1177. spintime = 1;
  1178. }
  1179. /* Wait 1 second for next try */
  1180. msleep(1000);
  1181. } else {
  1182. /* we don't understand the sense code, so it's
  1183. * probably pointless to loop */
  1184. if(!spintime) {
  1185. sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
  1186. sd_print_sense_hdr(sdkp, &sshdr);
  1187. }
  1188. break;
  1189. }
  1190. } while (spintime && time_before_eq(jiffies, spintime_expire));
  1191. if (spintime) {
  1192. if (scsi_status_is_good(the_result))
  1193. printk("ready\n");
  1194. else
  1195. printk("not responding...\n");
  1196. }
  1197. }
  1198. /*
  1199. * Determine whether disk supports Data Integrity Field.
  1200. */
  1201. void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1202. {
  1203. struct scsi_device *sdp = sdkp->device;
  1204. u8 type;
  1205. if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
  1206. return;
  1207. type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
  1208. if (type == sdkp->protection_type || !sdkp->first_scan)
  1209. return;
  1210. sdkp->protection_type = type;
  1211. if (type > SD_DIF_TYPE3_PROTECTION) {
  1212. sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
  1213. "protection type %u. Disabling disk!\n", type);
  1214. sdkp->capacity = 0;
  1215. return;
  1216. }
  1217. if (scsi_host_dif_capable(sdp->host, type))
  1218. sd_printk(KERN_NOTICE, sdkp,
  1219. "Enabling DIF Type %u protection\n", type);
  1220. else
  1221. sd_printk(KERN_NOTICE, sdkp,
  1222. "Disabling DIF Type %u protection\n", type);
  1223. }
  1224. static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1225. struct scsi_sense_hdr *sshdr, int sense_valid,
  1226. int the_result)
  1227. {
  1228. sd_print_result(sdkp, the_result);
  1229. if (driver_byte(the_result) & DRIVER_SENSE)
  1230. sd_print_sense_hdr(sdkp, sshdr);
  1231. else
  1232. sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
  1233. /*
  1234. * Set dirty bit for removable devices if not ready -
  1235. * sometimes drives will not report this properly.
  1236. */
  1237. if (sdp->removable &&
  1238. sense_valid && sshdr->sense_key == NOT_READY)
  1239. sdp->changed = 1;
  1240. /*
  1241. * We used to set media_present to 0 here to indicate no media
  1242. * in the drive, but some drives fail read capacity even with
  1243. * media present, so we can't do that.
  1244. */
  1245. sdkp->capacity = 0; /* unknown mapped to zero - as usual */
  1246. }
  1247. #define RC16_LEN 32
  1248. #if RC16_LEN > SD_BUF_SIZE
  1249. #error RC16_LEN must not be more than SD_BUF_SIZE
  1250. #endif
  1251. static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1252. unsigned char *buffer)
  1253. {
  1254. unsigned char cmd[16];
  1255. struct scsi_sense_hdr sshdr;
  1256. int sense_valid = 0;
  1257. int the_result;
  1258. int retries = 3;
  1259. unsigned int alignment;
  1260. unsigned long long lba;
  1261. unsigned sector_size;
  1262. do {
  1263. memset(cmd, 0, 16);
  1264. cmd[0] = SERVICE_ACTION_IN;
  1265. cmd[1] = SAI_READ_CAPACITY_16;
  1266. cmd[13] = RC16_LEN;
  1267. memset(buffer, 0, RC16_LEN);
  1268. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1269. buffer, RC16_LEN, &sshdr,
  1270. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1271. if (media_not_present(sdkp, &sshdr))
  1272. return -ENODEV;
  1273. if (the_result) {
  1274. sense_valid = scsi_sense_valid(&sshdr);
  1275. if (sense_valid &&
  1276. sshdr.sense_key == ILLEGAL_REQUEST &&
  1277. (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
  1278. sshdr.ascq == 0x00)
  1279. /* Invalid Command Operation Code or
  1280. * Invalid Field in CDB, just retry
  1281. * silently with RC10 */
  1282. return -EINVAL;
  1283. }
  1284. retries--;
  1285. } while (the_result && retries);
  1286. if (the_result) {
  1287. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
  1288. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1289. return -EINVAL;
  1290. }
  1291. sector_size = get_unaligned_be32(&buffer[8]);
  1292. lba = get_unaligned_be64(&buffer[0]);
  1293. sd_read_protection_type(sdkp, buffer);
  1294. if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
  1295. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1296. "kernel compiled with support for large block "
  1297. "devices.\n");
  1298. sdkp->capacity = 0;
  1299. return -EOVERFLOW;
  1300. }
  1301. /* Logical blocks per physical block exponent */
  1302. sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
  1303. /* Lowest aligned logical block */
  1304. alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
  1305. blk_queue_alignment_offset(sdp->request_queue, alignment);
  1306. if (alignment && sdkp->first_scan)
  1307. sd_printk(KERN_NOTICE, sdkp,
  1308. "physical block alignment offset: %u\n", alignment);
  1309. if (buffer[14] & 0x80) { /* TPE */
  1310. struct request_queue *q = sdp->request_queue;
  1311. sdkp->thin_provisioning = 1;
  1312. q->limits.discard_granularity = sdkp->hw_sector_size;
  1313. q->limits.max_discard_sectors = 0xffffffff;
  1314. if (buffer[14] & 0x40) /* TPRZ */
  1315. q->limits.discard_zeroes_data = 1;
  1316. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1317. }
  1318. sdkp->capacity = lba + 1;
  1319. return sector_size;
  1320. }
  1321. static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
  1322. unsigned char *buffer)
  1323. {
  1324. unsigned char cmd[16];
  1325. struct scsi_sense_hdr sshdr;
  1326. int sense_valid = 0;
  1327. int the_result;
  1328. int retries = 3;
  1329. sector_t lba;
  1330. unsigned sector_size;
  1331. do {
  1332. cmd[0] = READ_CAPACITY;
  1333. memset(&cmd[1], 0, 9);
  1334. memset(buffer, 0, 8);
  1335. the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
  1336. buffer, 8, &sshdr,
  1337. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  1338. if (media_not_present(sdkp, &sshdr))
  1339. return -ENODEV;
  1340. if (the_result)
  1341. sense_valid = scsi_sense_valid(&sshdr);
  1342. retries--;
  1343. } while (the_result && retries);
  1344. if (the_result) {
  1345. sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
  1346. read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
  1347. return -EINVAL;
  1348. }
  1349. sector_size = get_unaligned_be32(&buffer[4]);
  1350. lba = get_unaligned_be32(&buffer[0]);
  1351. if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
  1352. sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
  1353. "kernel compiled with support for large block "
  1354. "devices.\n");
  1355. sdkp->capacity = 0;
  1356. return -EOVERFLOW;
  1357. }
  1358. sdkp->capacity = lba + 1;
  1359. sdkp->hw_sector_size = sector_size;
  1360. return sector_size;
  1361. }
  1362. static int sd_try_rc16_first(struct scsi_device *sdp)
  1363. {
  1364. if (sdp->scsi_level > SCSI_SPC_2)
  1365. return 1;
  1366. if (scsi_device_protection(sdp))
  1367. return 1;
  1368. return 0;
  1369. }
  1370. /*
  1371. * read disk capacity
  1372. */
  1373. static void
  1374. sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
  1375. {
  1376. int sector_size;
  1377. struct scsi_device *sdp = sdkp->device;
  1378. sector_t old_capacity = sdkp->capacity;
  1379. if (sd_try_rc16_first(sdp)) {
  1380. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1381. if (sector_size == -EOVERFLOW)
  1382. goto got_data;
  1383. if (sector_size == -ENODEV)
  1384. return;
  1385. if (sector_size < 0)
  1386. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1387. if (sector_size < 0)
  1388. return;
  1389. } else {
  1390. sector_size = read_capacity_10(sdkp, sdp, buffer);
  1391. if (sector_size == -EOVERFLOW)
  1392. goto got_data;
  1393. if (sector_size < 0)
  1394. return;
  1395. if ((sizeof(sdkp->capacity) > 4) &&
  1396. (sdkp->capacity > 0xffffffffULL)) {
  1397. int old_sector_size = sector_size;
  1398. sd_printk(KERN_NOTICE, sdkp, "Very big device. "
  1399. "Trying to use READ CAPACITY(16).\n");
  1400. sector_size = read_capacity_16(sdkp, sdp, buffer);
  1401. if (sector_size < 0) {
  1402. sd_printk(KERN_NOTICE, sdkp,
  1403. "Using 0xffffffff as device size\n");
  1404. sdkp->capacity = 1 + (sector_t) 0xffffffff;
  1405. sector_size = old_sector_size;
  1406. goto got_data;
  1407. }
  1408. }
  1409. }
  1410. /* Some devices are known to return the total number of blocks,
  1411. * not the highest block number. Some devices have versions
  1412. * which do this and others which do not. Some devices we might
  1413. * suspect of doing this but we don't know for certain.
  1414. *
  1415. * If we know the reported capacity is wrong, decrement it. If
  1416. * we can only guess, then assume the number of blocks is even
  1417. * (usually true but not always) and err on the side of lowering
  1418. * the capacity.
  1419. */
  1420. if (sdp->fix_capacity ||
  1421. (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
  1422. sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
  1423. "from its reported value: %llu\n",
  1424. (unsigned long long) sdkp->capacity);
  1425. --sdkp->capacity;
  1426. }
  1427. got_data:
  1428. if (sector_size == 0) {
  1429. sector_size = 512;
  1430. sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
  1431. "assuming 512.\n");
  1432. }
  1433. if (sector_size != 512 &&
  1434. sector_size != 1024 &&
  1435. sector_size != 2048 &&
  1436. sector_size != 4096 &&
  1437. sector_size != 256) {
  1438. sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
  1439. sector_size);
  1440. /*
  1441. * The user might want to re-format the drive with
  1442. * a supported sectorsize. Once this happens, it
  1443. * would be relatively trivial to set the thing up.
  1444. * For this reason, we leave the thing in the table.
  1445. */
  1446. sdkp->capacity = 0;
  1447. /*
  1448. * set a bogus sector size so the normal read/write
  1449. * logic in the block layer will eventually refuse any
  1450. * request on this device without tripping over power
  1451. * of two sector size assumptions
  1452. */
  1453. sector_size = 512;
  1454. }
  1455. blk_queue_logical_block_size(sdp->request_queue, sector_size);
  1456. {
  1457. char cap_str_2[10], cap_str_10[10];
  1458. u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
  1459. string_get_size(sz, STRING_UNITS_2, cap_str_2,
  1460. sizeof(cap_str_2));
  1461. string_get_size(sz, STRING_UNITS_10, cap_str_10,
  1462. sizeof(cap_str_10));
  1463. if (sdkp->first_scan || old_capacity != sdkp->capacity) {
  1464. sd_printk(KERN_NOTICE, sdkp,
  1465. "%llu %d-byte logical blocks: (%s/%s)\n",
  1466. (unsigned long long)sdkp->capacity,
  1467. sector_size, cap_str_10, cap_str_2);
  1468. if (sdkp->hw_sector_size != sector_size)
  1469. sd_printk(KERN_NOTICE, sdkp,
  1470. "%u-byte physical blocks\n",
  1471. sdkp->hw_sector_size);
  1472. }
  1473. }
  1474. /* Rescale capacity to 512-byte units */
  1475. if (sector_size == 4096)
  1476. sdkp->capacity <<= 3;
  1477. else if (sector_size == 2048)
  1478. sdkp->capacity <<= 2;
  1479. else if (sector_size == 1024)
  1480. sdkp->capacity <<= 1;
  1481. else if (sector_size == 256)
  1482. sdkp->capacity >>= 1;
  1483. blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
  1484. sdkp->device->sector_size = sector_size;
  1485. }
  1486. /* called with buffer of length 512 */
  1487. static inline int
  1488. sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
  1489. unsigned char *buffer, int len, struct scsi_mode_data *data,
  1490. struct scsi_sense_hdr *sshdr)
  1491. {
  1492. return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
  1493. SD_TIMEOUT, SD_MAX_RETRIES, data,
  1494. sshdr);
  1495. }
  1496. /*
  1497. * read write protect setting, if possible - called only in sd_revalidate_disk()
  1498. * called with buffer of length SD_BUF_SIZE
  1499. */
  1500. static void
  1501. sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
  1502. {
  1503. int res;
  1504. struct scsi_device *sdp = sdkp->device;
  1505. struct scsi_mode_data data;
  1506. int old_wp = sdkp->write_prot;
  1507. set_disk_ro(sdkp->disk, 0);
  1508. if (sdp->skip_ms_page_3f) {
  1509. sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
  1510. return;
  1511. }
  1512. if (sdp->use_192_bytes_for_3f) {
  1513. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
  1514. } else {
  1515. /*
  1516. * First attempt: ask for all pages (0x3F), but only 4 bytes.
  1517. * We have to start carefully: some devices hang if we ask
  1518. * for more than is available.
  1519. */
  1520. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
  1521. /*
  1522. * Second attempt: ask for page 0 When only page 0 is
  1523. * implemented, a request for page 3F may return Sense Key
  1524. * 5: Illegal Request, Sense Code 24: Invalid field in
  1525. * CDB.
  1526. */
  1527. if (!scsi_status_is_good(res))
  1528. res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
  1529. /*
  1530. * Third attempt: ask 255 bytes, as we did earlier.
  1531. */
  1532. if (!scsi_status_is_good(res))
  1533. res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
  1534. &data, NULL);
  1535. }
  1536. if (!scsi_status_is_good(res)) {
  1537. sd_printk(KERN_WARNING, sdkp,
  1538. "Test WP failed, assume Write Enabled\n");
  1539. } else {
  1540. sdkp->write_prot = ((data.device_specific & 0x80) != 0);
  1541. set_disk_ro(sdkp->disk, sdkp->write_prot);
  1542. if (sdkp->first_scan || old_wp != sdkp->write_prot) {
  1543. sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
  1544. sdkp->write_prot ? "on" : "off");
  1545. sd_printk(KERN_DEBUG, sdkp,
  1546. "Mode Sense: %02x %02x %02x %02x\n",
  1547. buffer[0], buffer[1], buffer[2], buffer[3]);
  1548. }
  1549. }
  1550. }
  1551. /*
  1552. * sd_read_cache_type - called only from sd_revalidate_disk()
  1553. * called with buffer of length SD_BUF_SIZE
  1554. */
  1555. static void
  1556. sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
  1557. {
  1558. int len = 0, res;
  1559. struct scsi_device *sdp = sdkp->device;
  1560. int dbd;
  1561. int modepage;
  1562. struct scsi_mode_data data;
  1563. struct scsi_sense_hdr sshdr;
  1564. int old_wce = sdkp->WCE;
  1565. int old_rcd = sdkp->RCD;
  1566. int old_dpofua = sdkp->DPOFUA;
  1567. if (sdp->skip_ms_page_8)
  1568. goto defaults;
  1569. if (sdp->type == TYPE_RBC) {
  1570. modepage = 6;
  1571. dbd = 8;
  1572. } else {
  1573. modepage = 8;
  1574. dbd = 0;
  1575. }
  1576. /* cautiously ask */
  1577. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
  1578. if (!scsi_status_is_good(res))
  1579. goto bad_sense;
  1580. if (!data.header_length) {
  1581. modepage = 6;
  1582. sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
  1583. }
  1584. /* that went OK, now ask for the proper length */
  1585. len = data.length;
  1586. /*
  1587. * We're only interested in the first three bytes, actually.
  1588. * But the data cache page is defined for the first 20.
  1589. */
  1590. if (len < 3)
  1591. goto bad_sense;
  1592. if (len > 20)
  1593. len = 20;
  1594. /* Take headers and block descriptors into account */
  1595. len += data.header_length + data.block_descriptor_length;
  1596. if (len > SD_BUF_SIZE)
  1597. goto bad_sense;
  1598. /* Get the data */
  1599. res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
  1600. if (scsi_status_is_good(res)) {
  1601. int offset = data.header_length + data.block_descriptor_length;
  1602. if (offset >= SD_BUF_SIZE - 2) {
  1603. sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
  1604. goto defaults;
  1605. }
  1606. if ((buffer[offset] & 0x3f) != modepage) {
  1607. sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
  1608. goto defaults;
  1609. }
  1610. if (modepage == 8) {
  1611. sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
  1612. sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
  1613. } else {
  1614. sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
  1615. sdkp->RCD = 0;
  1616. }
  1617. sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
  1618. if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
  1619. sd_printk(KERN_NOTICE, sdkp,
  1620. "Uses READ/WRITE(6), disabling FUA\n");
  1621. sdkp->DPOFUA = 0;
  1622. }
  1623. if (sdkp->first_scan || old_wce != sdkp->WCE ||
  1624. old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
  1625. sd_printk(KERN_NOTICE, sdkp,
  1626. "Write cache: %s, read cache: %s, %s\n",
  1627. sdkp->WCE ? "enabled" : "disabled",
  1628. sdkp->RCD ? "disabled" : "enabled",
  1629. sdkp->DPOFUA ? "supports DPO and FUA"
  1630. : "doesn't support DPO or FUA");
  1631. return;
  1632. }
  1633. bad_sense:
  1634. if (scsi_sense_valid(&sshdr) &&
  1635. sshdr.sense_key == ILLEGAL_REQUEST &&
  1636. sshdr.asc == 0x24 && sshdr.ascq == 0x0)
  1637. /* Invalid field in CDB */
  1638. sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
  1639. else
  1640. sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
  1641. defaults:
  1642. sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
  1643. sdkp->WCE = 0;
  1644. sdkp->RCD = 0;
  1645. sdkp->DPOFUA = 0;
  1646. }
  1647. /*
  1648. * The ATO bit indicates whether the DIF application tag is available
  1649. * for use by the operating system.
  1650. */
  1651. void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
  1652. {
  1653. int res, offset;
  1654. struct scsi_device *sdp = sdkp->device;
  1655. struct scsi_mode_data data;
  1656. struct scsi_sense_hdr sshdr;
  1657. if (sdp->type != TYPE_DISK)
  1658. return;
  1659. if (sdkp->protection_type == 0)
  1660. return;
  1661. res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
  1662. SD_MAX_RETRIES, &data, &sshdr);
  1663. if (!scsi_status_is_good(res) || !data.header_length ||
  1664. data.length < 6) {
  1665. sd_printk(KERN_WARNING, sdkp,
  1666. "getting Control mode page failed, assume no ATO\n");
  1667. if (scsi_sense_valid(&sshdr))
  1668. sd_print_sense_hdr(sdkp, &sshdr);
  1669. return;
  1670. }
  1671. offset = data.header_length + data.block_descriptor_length;
  1672. if ((buffer[offset] & 0x3f) != 0x0a) {
  1673. sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
  1674. return;
  1675. }
  1676. if ((buffer[offset + 5] & 0x80) == 0)
  1677. return;
  1678. sdkp->ATO = 1;
  1679. return;
  1680. }
  1681. /**
  1682. * sd_read_block_limits - Query disk device for preferred I/O sizes.
  1683. * @disk: disk to query
  1684. */
  1685. static void sd_read_block_limits(struct scsi_disk *sdkp)
  1686. {
  1687. struct request_queue *q = sdkp->disk->queue;
  1688. unsigned int sector_sz = sdkp->device->sector_size;
  1689. char *buffer;
  1690. /* Block Limits VPD */
  1691. buffer = scsi_get_vpd_page(sdkp->device, 0xb0);
  1692. if (buffer == NULL)
  1693. return;
  1694. blk_queue_io_min(sdkp->disk->queue,
  1695. get_unaligned_be16(&buffer[6]) * sector_sz);
  1696. blk_queue_io_opt(sdkp->disk->queue,
  1697. get_unaligned_be32(&buffer[12]) * sector_sz);
  1698. /* Thin provisioning enabled and page length indicates TP support */
  1699. if (sdkp->thin_provisioning && buffer[3] == 0x3c) {
  1700. unsigned int lba_count, desc_count, granularity;
  1701. lba_count = get_unaligned_be32(&buffer[20]);
  1702. desc_count = get_unaligned_be32(&buffer[24]);
  1703. if (lba_count) {
  1704. q->limits.max_discard_sectors =
  1705. lba_count * sector_sz >> 9;
  1706. if (desc_count)
  1707. sdkp->unmap = 1;
  1708. }
  1709. granularity = get_unaligned_be32(&buffer[28]);
  1710. if (granularity)
  1711. q->limits.discard_granularity = granularity * sector_sz;
  1712. if (buffer[32] & 0x80)
  1713. q->limits.discard_alignment =
  1714. get_unaligned_be32(&buffer[32]) & ~(1 << 31);
  1715. }
  1716. kfree(buffer);
  1717. }
  1718. /**
  1719. * sd_read_block_characteristics - Query block dev. characteristics
  1720. * @disk: disk to query
  1721. */
  1722. static void sd_read_block_characteristics(struct scsi_disk *sdkp)
  1723. {
  1724. char *buffer;
  1725. u16 rot;
  1726. /* Block Device Characteristics VPD */
  1727. buffer = scsi_get_vpd_page(sdkp->device, 0xb1);
  1728. if (buffer == NULL)
  1729. return;
  1730. rot = get_unaligned_be16(&buffer[4]);
  1731. if (rot == 1)
  1732. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
  1733. kfree(buffer);
  1734. }
  1735. static int sd_try_extended_inquiry(struct scsi_device *sdp)
  1736. {
  1737. /*
  1738. * Although VPD inquiries can go to SCSI-2 type devices,
  1739. * some USB ones crash on receiving them, and the pages
  1740. * we currently ask for are for SPC-3 and beyond
  1741. */
  1742. if (sdp->scsi_level > SCSI_SPC_2)
  1743. return 1;
  1744. return 0;
  1745. }
  1746. /**
  1747. * sd_revalidate_disk - called the first time a new disk is seen,
  1748. * performs disk spin up, read_capacity, etc.
  1749. * @disk: struct gendisk we care about
  1750. **/
  1751. static int sd_revalidate_disk(struct gendisk *disk)
  1752. {
  1753. struct scsi_disk *sdkp = scsi_disk(disk);
  1754. struct scsi_device *sdp = sdkp->device;
  1755. unsigned char *buffer;
  1756. unsigned ordered;
  1757. SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
  1758. "sd_revalidate_disk\n"));
  1759. /*
  1760. * If the device is offline, don't try and read capacity or any
  1761. * of the other niceties.
  1762. */
  1763. if (!scsi_device_online(sdp))
  1764. goto out;
  1765. buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
  1766. if (!buffer) {
  1767. sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
  1768. "allocation failure.\n");
  1769. goto out;
  1770. }
  1771. sd_spinup_disk(sdkp);
  1772. /*
  1773. * Without media there is no reason to ask; moreover, some devices
  1774. * react badly if we do.
  1775. */
  1776. if (sdkp->media_present) {
  1777. sd_read_capacity(sdkp, buffer);
  1778. if (sd_try_extended_inquiry(sdp)) {
  1779. sd_read_block_limits(sdkp);
  1780. sd_read_block_characteristics(sdkp);
  1781. }
  1782. sd_read_write_protect_flag(sdkp, buffer);
  1783. sd_read_cache_type(sdkp, buffer);
  1784. sd_read_app_tag_own(sdkp, buffer);
  1785. }
  1786. sdkp->first_scan = 0;
  1787. /*
  1788. * We now have all cache related info, determine how we deal
  1789. * with ordered requests. Note that as the current SCSI
  1790. * dispatch function can alter request order, we cannot use
  1791. * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
  1792. */
  1793. if (sdkp->WCE)
  1794. ordered = sdkp->DPOFUA
  1795. ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
  1796. else
  1797. ordered = QUEUE_ORDERED_DRAIN;
  1798. blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
  1799. set_capacity(disk, sdkp->capacity);
  1800. kfree(buffer);
  1801. out:
  1802. return 0;
  1803. }
  1804. /**
  1805. * sd_format_disk_name - format disk name
  1806. * @prefix: name prefix - ie. "sd" for SCSI disks
  1807. * @index: index of the disk to format name for
  1808. * @buf: output buffer
  1809. * @buflen: length of the output buffer
  1810. *
  1811. * SCSI disk names starts at sda. The 26th device is sdz and the
  1812. * 27th is sdaa. The last one for two lettered suffix is sdzz
  1813. * which is followed by sdaaa.
  1814. *
  1815. * This is basically 26 base counting with one extra 'nil' entry
  1816. * at the beggining from the second digit on and can be
  1817. * determined using similar method as 26 base conversion with the
  1818. * index shifted -1 after each digit is computed.
  1819. *
  1820. * CONTEXT:
  1821. * Don't care.
  1822. *
  1823. * RETURNS:
  1824. * 0 on success, -errno on failure.
  1825. */
  1826. static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
  1827. {
  1828. const int base = 'z' - 'a' + 1;
  1829. char *begin = buf + strlen(prefix);
  1830. char *end = buf + buflen;
  1831. char *p;
  1832. int unit;
  1833. p = end - 1;
  1834. *p = '\0';
  1835. unit = base;
  1836. do {
  1837. if (p == begin)
  1838. return -EINVAL;
  1839. *--p = 'a' + (index % unit);
  1840. index = (index / unit) - 1;
  1841. } while (index >= 0);
  1842. memmove(begin, p, end - p);
  1843. memcpy(buf, prefix, strlen(prefix));
  1844. return 0;
  1845. }
  1846. /*
  1847. * The asynchronous part of sd_probe
  1848. */
  1849. static void sd_probe_async(void *data, async_cookie_t cookie)
  1850. {
  1851. struct scsi_disk *sdkp = data;
  1852. struct scsi_device *sdp;
  1853. struct gendisk *gd;
  1854. u32 index;
  1855. struct device *dev;
  1856. sdp = sdkp->device;
  1857. gd = sdkp->disk;
  1858. index = sdkp->index;
  1859. dev = &sdp->sdev_gendev;
  1860. if (index < SD_MAX_DISKS) {
  1861. gd->major = sd_major((index & 0xf0) >> 4);
  1862. gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
  1863. gd->minors = SD_MINORS;
  1864. }
  1865. gd->fops = &sd_fops;
  1866. gd->private_data = &sdkp->driver;
  1867. gd->queue = sdkp->device->request_queue;
  1868. /* defaults, until the device tells us otherwise */
  1869. sdp->sector_size = 512;
  1870. sdkp->capacity = 0;
  1871. sdkp->media_present = 1;
  1872. sdkp->write_prot = 0;
  1873. sdkp->WCE = 0;
  1874. sdkp->RCD = 0;
  1875. sdkp->ATO = 0;
  1876. sdkp->first_scan = 1;
  1877. sd_revalidate_disk(gd);
  1878. blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
  1879. gd->driverfs_dev = &sdp->sdev_gendev;
  1880. gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
  1881. if (sdp->removable)
  1882. gd->flags |= GENHD_FL_REMOVABLE;
  1883. dev_set_drvdata(dev, sdkp);
  1884. add_disk(gd);
  1885. sd_dif_config_host(sdkp);
  1886. sd_revalidate_disk(gd);
  1887. sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
  1888. sdp->removable ? "removable " : "");
  1889. put_device(&sdkp->dev);
  1890. }
  1891. /**
  1892. * sd_probe - called during driver initialization and whenever a
  1893. * new scsi device is attached to the system. It is called once
  1894. * for each scsi device (not just disks) present.
  1895. * @dev: pointer to device object
  1896. *
  1897. * Returns 0 if successful (or not interested in this scsi device
  1898. * (e.g. scanner)); 1 when there is an error.
  1899. *
  1900. * Note: this function is invoked from the scsi mid-level.
  1901. * This function sets up the mapping between a given
  1902. * <host,channel,id,lun> (found in sdp) and new device name
  1903. * (e.g. /dev/sda). More precisely it is the block device major
  1904. * and minor number that is chosen here.
  1905. *
  1906. * Assume sd_attach is not re-entrant (for time being)
  1907. * Also think about sd_attach() and sd_remove() running coincidentally.
  1908. **/
  1909. static int sd_probe(struct device *dev)
  1910. {
  1911. struct scsi_device *sdp = to_scsi_device(dev);
  1912. struct scsi_disk *sdkp;
  1913. struct gendisk *gd;
  1914. u32 index;
  1915. int error;
  1916. error = -ENODEV;
  1917. if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
  1918. goto out;
  1919. SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
  1920. "sd_attach\n"));
  1921. error = -ENOMEM;
  1922. sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
  1923. if (!sdkp)
  1924. goto out;
  1925. gd = alloc_disk(SD_MINORS);
  1926. if (!gd)
  1927. goto out_free;
  1928. do {
  1929. if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
  1930. goto out_put;
  1931. spin_lock(&sd_index_lock);
  1932. error = ida_get_new(&sd_index_ida, &index);
  1933. spin_unlock(&sd_index_lock);
  1934. } while (error == -EAGAIN);
  1935. if (error)
  1936. goto out_put;
  1937. error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
  1938. if (error)
  1939. goto out_free_index;
  1940. sdkp->device = sdp;
  1941. sdkp->driver = &sd_template;
  1942. sdkp->disk = gd;
  1943. sdkp->index = index;
  1944. sdkp->openers = 0;
  1945. sdkp->previous_state = 1;
  1946. if (!sdp->request_queue->rq_timeout) {
  1947. if (sdp->type != TYPE_MOD)
  1948. blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
  1949. else
  1950. blk_queue_rq_timeout(sdp->request_queue,
  1951. SD_MOD_TIMEOUT);
  1952. }
  1953. device_initialize(&sdkp->dev);
  1954. sdkp->dev.parent = &sdp->sdev_gendev;
  1955. sdkp->dev.class = &sd_disk_class;
  1956. dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
  1957. if (device_add(&sdkp->dev))
  1958. goto out_free_index;
  1959. get_device(&sdp->sdev_gendev);
  1960. get_device(&sdkp->dev); /* prevent release before async_schedule */
  1961. async_schedule(sd_probe_async, sdkp);
  1962. return 0;
  1963. out_free_index:
  1964. spin_lock(&sd_index_lock);
  1965. ida_remove(&sd_index_ida, index);
  1966. spin_unlock(&sd_index_lock);
  1967. out_put:
  1968. put_disk(gd);
  1969. out_free:
  1970. kfree(sdkp);
  1971. out:
  1972. return error;
  1973. }
  1974. /**
  1975. * sd_remove - called whenever a scsi disk (previously recognized by
  1976. * sd_probe) is detached from the system. It is called (potentially
  1977. * multiple times) during sd module unload.
  1978. * @sdp: pointer to mid level scsi device object
  1979. *
  1980. * Note: this function is invoked from the scsi mid-level.
  1981. * This function potentially frees up a device name (e.g. /dev/sdc)
  1982. * that could be re-used by a subsequent sd_probe().
  1983. * This function is not called when the built-in sd driver is "exit-ed".
  1984. **/
  1985. static int sd_remove(struct device *dev)
  1986. {
  1987. struct scsi_disk *sdkp;
  1988. async_synchronize_full();
  1989. sdkp = dev_get_drvdata(dev);
  1990. blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
  1991. device_del(&sdkp->dev);
  1992. del_gendisk(sdkp->disk);
  1993. sd_shutdown(dev);
  1994. mutex_lock(&sd_ref_mutex);
  1995. dev_set_drvdata(dev, NULL);
  1996. put_device(&sdkp->dev);
  1997. mutex_unlock(&sd_ref_mutex);
  1998. return 0;
  1999. }
  2000. /**
  2001. * scsi_disk_release - Called to free the scsi_disk structure
  2002. * @dev: pointer to embedded class device
  2003. *
  2004. * sd_ref_mutex must be held entering this routine. Because it is
  2005. * called on last put, you should always use the scsi_disk_get()
  2006. * scsi_disk_put() helpers which manipulate the semaphore directly
  2007. * and never do a direct put_device.
  2008. **/
  2009. static void scsi_disk_release(struct device *dev)
  2010. {
  2011. struct scsi_disk *sdkp = to_scsi_disk(dev);
  2012. struct gendisk *disk = sdkp->disk;
  2013. spin_lock(&sd_index_lock);
  2014. ida_remove(&sd_index_ida, sdkp->index);
  2015. spin_unlock(&sd_index_lock);
  2016. disk->private_data = NULL;
  2017. put_disk(disk);
  2018. put_device(&sdkp->device->sdev_gendev);
  2019. kfree(sdkp);
  2020. }
  2021. static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
  2022. {
  2023. unsigned char cmd[6] = { START_STOP }; /* START_VALID */
  2024. struct scsi_sense_hdr sshdr;
  2025. struct scsi_device *sdp = sdkp->device;
  2026. int res;
  2027. if (start)
  2028. cmd[4] |= 1; /* START */
  2029. if (sdp->start_stop_pwr_cond)
  2030. cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
  2031. if (!scsi_device_online(sdp))
  2032. return -ENODEV;
  2033. res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
  2034. SD_TIMEOUT, SD_MAX_RETRIES, NULL);
  2035. if (res) {
  2036. sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
  2037. sd_print_result(sdkp, res);
  2038. if (driver_byte(res) & DRIVER_SENSE)
  2039. sd_print_sense_hdr(sdkp, &sshdr);
  2040. }
  2041. return res;
  2042. }
  2043. /*
  2044. * Send a SYNCHRONIZE CACHE instruction down to the device through
  2045. * the normal SCSI command structure. Wait for the command to
  2046. * complete.
  2047. */
  2048. static void sd_shutdown(struct device *dev)
  2049. {
  2050. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2051. if (!sdkp)
  2052. return; /* this can happen */
  2053. if (sdkp->WCE) {
  2054. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2055. sd_sync_cache(sdkp);
  2056. }
  2057. if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
  2058. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2059. sd_start_stop_device(sdkp, 0);
  2060. }
  2061. scsi_disk_put(sdkp);
  2062. }
  2063. static int sd_suspend(struct device *dev, pm_message_t mesg)
  2064. {
  2065. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2066. int ret = 0;
  2067. if (!sdkp)
  2068. return 0; /* this can happen */
  2069. if (sdkp->WCE) {
  2070. sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
  2071. ret = sd_sync_cache(sdkp);
  2072. if (ret)
  2073. goto done;
  2074. }
  2075. if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
  2076. sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
  2077. ret = sd_start_stop_device(sdkp, 0);
  2078. }
  2079. done:
  2080. scsi_disk_put(sdkp);
  2081. return ret;
  2082. }
  2083. static int sd_resume(struct device *dev)
  2084. {
  2085. struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
  2086. int ret = 0;
  2087. if (!sdkp->device->manage_start_stop)
  2088. goto done;
  2089. sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
  2090. ret = sd_start_stop_device(sdkp, 1);
  2091. done:
  2092. scsi_disk_put(sdkp);
  2093. return ret;
  2094. }
  2095. /**
  2096. * init_sd - entry point for this driver (both when built in or when
  2097. * a module).
  2098. *
  2099. * Note: this function registers this driver with the scsi mid-level.
  2100. **/
  2101. static int __init init_sd(void)
  2102. {
  2103. int majors = 0, i, err;
  2104. SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
  2105. for (i = 0; i < SD_MAJORS; i++)
  2106. if (register_blkdev(sd_major(i), "sd") == 0)
  2107. majors++;
  2108. if (!majors)
  2109. return -ENODEV;
  2110. err = class_register(&sd_disk_class);
  2111. if (err)
  2112. goto err_out;
  2113. err = scsi_register_driver(&sd_template.gendrv);
  2114. if (err)
  2115. goto err_out_class;
  2116. sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
  2117. 0, 0, NULL);
  2118. if (!sd_cdb_cache) {
  2119. printk(KERN_ERR "sd: can't init extended cdb cache\n");
  2120. goto err_out_class;
  2121. }
  2122. sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
  2123. if (!sd_cdb_pool) {
  2124. printk(KERN_ERR "sd: can't init extended cdb pool\n");
  2125. goto err_out_cache;
  2126. }
  2127. return 0;
  2128. err_out_cache:
  2129. kmem_cache_destroy(sd_cdb_cache);
  2130. err_out_class:
  2131. class_unregister(&sd_disk_class);
  2132. err_out:
  2133. for (i = 0; i < SD_MAJORS; i++)
  2134. unregister_blkdev(sd_major(i), "sd");
  2135. return err;
  2136. }
  2137. /**
  2138. * exit_sd - exit point for this driver (when it is a module).
  2139. *
  2140. * Note: this function unregisters this driver from the scsi mid-level.
  2141. **/
  2142. static void __exit exit_sd(void)
  2143. {
  2144. int i;
  2145. SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
  2146. mempool_destroy(sd_cdb_pool);
  2147. kmem_cache_destroy(sd_cdb_cache);
  2148. scsi_unregister_driver(&sd_template.gendrv);
  2149. class_unregister(&sd_disk_class);
  2150. for (i = 0; i < SD_MAJORS; i++)
  2151. unregister_blkdev(sd_major(i), "sd");
  2152. }
  2153. module_init(init_sd);
  2154. module_exit(exit_sd);
  2155. static void sd_print_sense_hdr(struct scsi_disk *sdkp,
  2156. struct scsi_sense_hdr *sshdr)
  2157. {
  2158. sd_printk(KERN_INFO, sdkp, "");
  2159. scsi_show_sense_hdr(sshdr);
  2160. sd_printk(KERN_INFO, sdkp, "");
  2161. scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
  2162. }
  2163. static void sd_print_result(struct scsi_disk *sdkp, int result)
  2164. {
  2165. sd_printk(KERN_INFO, sdkp, "");
  2166. scsi_show_result(result);
  2167. }