scsi_lib.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  1. /*
  2. * scsi_lib.c Copyright (C) 1999 Eric Youngdale
  3. *
  4. * SCSI queueing library.
  5. * Initial versions: Eric Youngdale (eric@andante.org).
  6. * Based upon conversations with large numbers
  7. * of people at Linux Expo.
  8. */
  9. #include <linux/bio.h>
  10. #include <linux/bitops.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/completion.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mempool.h>
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/hardirq.h>
  20. #include <linux/scatterlist.h>
  21. #include <scsi/scsi.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <scsi/scsi_dbg.h>
  24. #include <scsi/scsi_device.h>
  25. #include <scsi/scsi_driver.h>
  26. #include <scsi/scsi_eh.h>
  27. #include <scsi/scsi_host.h>
  28. #include "scsi_priv.h"
  29. #include "scsi_logging.h"
  30. #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
  31. #define SG_MEMPOOL_SIZE 2
  32. struct scsi_host_sg_pool {
  33. size_t size;
  34. char *name;
  35. struct kmem_cache *slab;
  36. mempool_t *pool;
  37. };
  38. #define SP(x) { x, "sgpool-" __stringify(x) }
  39. #if (SCSI_MAX_SG_SEGMENTS < 32)
  40. #error SCSI_MAX_SG_SEGMENTS is too small (must be 32 or greater)
  41. #endif
  42. static struct scsi_host_sg_pool scsi_sg_pools[] = {
  43. SP(8),
  44. SP(16),
  45. #if (SCSI_MAX_SG_SEGMENTS > 32)
  46. SP(32),
  47. #if (SCSI_MAX_SG_SEGMENTS > 64)
  48. SP(64),
  49. #if (SCSI_MAX_SG_SEGMENTS > 128)
  50. SP(128),
  51. #if (SCSI_MAX_SG_SEGMENTS > 256)
  52. #error SCSI_MAX_SG_SEGMENTS is too large (256 MAX)
  53. #endif
  54. #endif
  55. #endif
  56. #endif
  57. SP(SCSI_MAX_SG_SEGMENTS)
  58. };
  59. #undef SP
  60. static struct kmem_cache *scsi_sdb_cache;
  61. static void scsi_run_queue(struct request_queue *q);
  62. /*
  63. * Function: scsi_unprep_request()
  64. *
  65. * Purpose: Remove all preparation done for a request, including its
  66. * associated scsi_cmnd, so that it can be requeued.
  67. *
  68. * Arguments: req - request to unprepare
  69. *
  70. * Lock status: Assumed that no locks are held upon entry.
  71. *
  72. * Returns: Nothing.
  73. */
  74. static void scsi_unprep_request(struct request *req)
  75. {
  76. struct scsi_cmnd *cmd = req->special;
  77. req->cmd_flags &= ~REQ_DONTPREP;
  78. req->special = NULL;
  79. scsi_put_command(cmd);
  80. }
  81. /*
  82. * Function: scsi_queue_insert()
  83. *
  84. * Purpose: Insert a command in the midlevel queue.
  85. *
  86. * Arguments: cmd - command that we are adding to queue.
  87. * reason - why we are inserting command to queue.
  88. *
  89. * Lock status: Assumed that lock is not held upon entry.
  90. *
  91. * Returns: Nothing.
  92. *
  93. * Notes: We do this for one of two cases. Either the host is busy
  94. * and it cannot accept any more commands for the time being,
  95. * or the device returned QUEUE_FULL and can accept no more
  96. * commands.
  97. * Notes: This could be called either from an interrupt context or a
  98. * normal process context.
  99. */
  100. int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  101. {
  102. struct Scsi_Host *host = cmd->device->host;
  103. struct scsi_device *device = cmd->device;
  104. struct request_queue *q = device->request_queue;
  105. unsigned long flags;
  106. SCSI_LOG_MLQUEUE(1,
  107. printk("Inserting command %p into mlqueue\n", cmd));
  108. /*
  109. * Set the appropriate busy bit for the device/host.
  110. *
  111. * If the host/device isn't busy, assume that something actually
  112. * completed, and that we should be able to queue a command now.
  113. *
  114. * Note that the prior mid-layer assumption that any host could
  115. * always queue at least one command is now broken. The mid-layer
  116. * will implement a user specifiable stall (see
  117. * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
  118. * if a command is requeued with no other commands outstanding
  119. * either for the device or for the host.
  120. */
  121. if (reason == SCSI_MLQUEUE_HOST_BUSY)
  122. host->host_blocked = host->max_host_blocked;
  123. else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
  124. device->device_blocked = device->max_device_blocked;
  125. /*
  126. * Decrement the counters, since these commands are no longer
  127. * active on the host/device.
  128. */
  129. scsi_device_unbusy(device);
  130. /*
  131. * Requeue this command. It will go before all other commands
  132. * that are already in the queue.
  133. *
  134. * NOTE: there is magic here about the way the queue is plugged if
  135. * we have no outstanding commands.
  136. *
  137. * Although we *don't* plug the queue, we call the request
  138. * function. The SCSI request function detects the blocked condition
  139. * and plugs the queue appropriately.
  140. */
  141. spin_lock_irqsave(q->queue_lock, flags);
  142. blk_requeue_request(q, cmd->request);
  143. spin_unlock_irqrestore(q->queue_lock, flags);
  144. scsi_run_queue(q);
  145. return 0;
  146. }
  147. /**
  148. * scsi_execute - insert request and wait for the result
  149. * @sdev: scsi device
  150. * @cmd: scsi command
  151. * @data_direction: data direction
  152. * @buffer: data buffer
  153. * @bufflen: len of buffer
  154. * @sense: optional sense buffer
  155. * @timeout: request timeout in seconds
  156. * @retries: number of times to retry request
  157. * @flags: or into request flags;
  158. *
  159. * returns the req->errors value which is the scsi_cmnd result
  160. * field.
  161. */
  162. int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
  163. int data_direction, void *buffer, unsigned bufflen,
  164. unsigned char *sense, int timeout, int retries, int flags)
  165. {
  166. struct request *req;
  167. int write = (data_direction == DMA_TO_DEVICE);
  168. int ret = DRIVER_ERROR << 24;
  169. req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
  170. if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
  171. buffer, bufflen, __GFP_WAIT))
  172. goto out;
  173. req->cmd_len = COMMAND_SIZE(cmd[0]);
  174. memcpy(req->cmd, cmd, req->cmd_len);
  175. req->sense = sense;
  176. req->sense_len = 0;
  177. req->retries = retries;
  178. req->timeout = timeout;
  179. req->cmd_type = REQ_TYPE_BLOCK_PC;
  180. req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT;
  181. /*
  182. * head injection *required* here otherwise quiesce won't work
  183. */
  184. blk_execute_rq(req->q, NULL, req, 1);
  185. /*
  186. * Some devices (USB mass-storage in particular) may transfer
  187. * garbage data together with a residue indicating that the data
  188. * is invalid. Prevent the garbage from being misinterpreted
  189. * and prevent security leaks by zeroing out the excess data.
  190. */
  191. if (unlikely(req->data_len > 0 && req->data_len <= bufflen))
  192. memset(buffer + (bufflen - req->data_len), 0, req->data_len);
  193. ret = req->errors;
  194. out:
  195. blk_put_request(req);
  196. return ret;
  197. }
  198. EXPORT_SYMBOL(scsi_execute);
  199. int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
  200. int data_direction, void *buffer, unsigned bufflen,
  201. struct scsi_sense_hdr *sshdr, int timeout, int retries)
  202. {
  203. char *sense = NULL;
  204. int result;
  205. if (sshdr) {
  206. sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  207. if (!sense)
  208. return DRIVER_ERROR << 24;
  209. }
  210. result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
  211. sense, timeout, retries, 0);
  212. if (sshdr)
  213. scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
  214. kfree(sense);
  215. return result;
  216. }
  217. EXPORT_SYMBOL(scsi_execute_req);
  218. struct scsi_io_context {
  219. void *data;
  220. void (*done)(void *data, char *sense, int result, int resid);
  221. char sense[SCSI_SENSE_BUFFERSIZE];
  222. };
  223. static struct kmem_cache *scsi_io_context_cache;
  224. static void scsi_end_async(struct request *req, int uptodate)
  225. {
  226. struct scsi_io_context *sioc = req->end_io_data;
  227. if (sioc->done)
  228. sioc->done(sioc->data, sioc->sense, req->errors, req->data_len);
  229. kmem_cache_free(scsi_io_context_cache, sioc);
  230. __blk_put_request(req->q, req);
  231. }
  232. static int scsi_merge_bio(struct request *rq, struct bio *bio)
  233. {
  234. struct request_queue *q = rq->q;
  235. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  236. if (rq_data_dir(rq) == WRITE)
  237. bio->bi_rw |= (1 << BIO_RW);
  238. blk_queue_bounce(q, &bio);
  239. return blk_rq_append_bio(q, rq, bio);
  240. }
  241. static void scsi_bi_endio(struct bio *bio, int error)
  242. {
  243. bio_put(bio);
  244. }
  245. /**
  246. * scsi_req_map_sg - map a scatterlist into a request
  247. * @rq: request to fill
  248. * @sgl: scatterlist
  249. * @nsegs: number of elements
  250. * @bufflen: len of buffer
  251. * @gfp: memory allocation flags
  252. *
  253. * scsi_req_map_sg maps a scatterlist into a request so that the
  254. * request can be sent to the block layer. We do not trust the scatterlist
  255. * sent to use, as some ULDs use that struct to only organize the pages.
  256. */
  257. static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,
  258. int nsegs, unsigned bufflen, gfp_t gfp)
  259. {
  260. struct request_queue *q = rq->q;
  261. int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
  262. unsigned int data_len = bufflen, len, bytes, off;
  263. struct scatterlist *sg;
  264. struct page *page;
  265. struct bio *bio = NULL;
  266. int i, err, nr_vecs = 0;
  267. for_each_sg(sgl, sg, nsegs, i) {
  268. page = sg_page(sg);
  269. off = sg->offset;
  270. len = sg->length;
  271. while (len > 0 && data_len > 0) {
  272. /*
  273. * sg sends a scatterlist that is larger than
  274. * the data_len it wants transferred for certain
  275. * IO sizes
  276. */
  277. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  278. bytes = min(bytes, data_len);
  279. if (!bio) {
  280. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  281. nr_pages -= nr_vecs;
  282. bio = bio_alloc(gfp, nr_vecs);
  283. if (!bio) {
  284. err = -ENOMEM;
  285. goto free_bios;
  286. }
  287. bio->bi_end_io = scsi_bi_endio;
  288. }
  289. if (bio_add_pc_page(q, bio, page, bytes, off) !=
  290. bytes) {
  291. bio_put(bio);
  292. err = -EINVAL;
  293. goto free_bios;
  294. }
  295. if (bio->bi_vcnt >= nr_vecs) {
  296. err = scsi_merge_bio(rq, bio);
  297. if (err) {
  298. bio_endio(bio, 0);
  299. goto free_bios;
  300. }
  301. bio = NULL;
  302. }
  303. page++;
  304. len -= bytes;
  305. data_len -=bytes;
  306. off = 0;
  307. }
  308. }
  309. rq->buffer = rq->data = NULL;
  310. rq->data_len = bufflen;
  311. return 0;
  312. free_bios:
  313. while ((bio = rq->bio) != NULL) {
  314. rq->bio = bio->bi_next;
  315. /*
  316. * call endio instead of bio_put incase it was bounced
  317. */
  318. bio_endio(bio, 0);
  319. }
  320. return err;
  321. }
  322. /**
  323. * scsi_execute_async - insert request
  324. * @sdev: scsi device
  325. * @cmd: scsi command
  326. * @cmd_len: length of scsi cdb
  327. * @data_direction: DMA_TO_DEVICE, DMA_FROM_DEVICE, or DMA_NONE
  328. * @buffer: data buffer (this can be a kernel buffer or scatterlist)
  329. * @bufflen: len of buffer
  330. * @use_sg: if buffer is a scatterlist this is the number of elements
  331. * @timeout: request timeout in seconds
  332. * @retries: number of times to retry request
  333. * @privdata: data passed to done()
  334. * @done: callback function when done
  335. * @gfp: memory allocation flags
  336. */
  337. int scsi_execute_async(struct scsi_device *sdev, const unsigned char *cmd,
  338. int cmd_len, int data_direction, void *buffer, unsigned bufflen,
  339. int use_sg, int timeout, int retries, void *privdata,
  340. void (*done)(void *, char *, int, int), gfp_t gfp)
  341. {
  342. struct request *req;
  343. struct scsi_io_context *sioc;
  344. int err = 0;
  345. int write = (data_direction == DMA_TO_DEVICE);
  346. sioc = kmem_cache_zalloc(scsi_io_context_cache, gfp);
  347. if (!sioc)
  348. return DRIVER_ERROR << 24;
  349. req = blk_get_request(sdev->request_queue, write, gfp);
  350. if (!req)
  351. goto free_sense;
  352. req->cmd_type = REQ_TYPE_BLOCK_PC;
  353. req->cmd_flags |= REQ_QUIET;
  354. if (use_sg)
  355. err = scsi_req_map_sg(req, buffer, use_sg, bufflen, gfp);
  356. else if (bufflen)
  357. err = blk_rq_map_kern(req->q, req, buffer, bufflen, gfp);
  358. if (err)
  359. goto free_req;
  360. req->cmd_len = cmd_len;
  361. memset(req->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
  362. memcpy(req->cmd, cmd, req->cmd_len);
  363. req->sense = sioc->sense;
  364. req->sense_len = 0;
  365. req->timeout = timeout;
  366. req->retries = retries;
  367. req->end_io_data = sioc;
  368. sioc->data = privdata;
  369. sioc->done = done;
  370. blk_execute_rq_nowait(req->q, NULL, req, 1, scsi_end_async);
  371. return 0;
  372. free_req:
  373. blk_put_request(req);
  374. free_sense:
  375. kmem_cache_free(scsi_io_context_cache, sioc);
  376. return DRIVER_ERROR << 24;
  377. }
  378. EXPORT_SYMBOL_GPL(scsi_execute_async);
  379. /*
  380. * Function: scsi_init_cmd_errh()
  381. *
  382. * Purpose: Initialize cmd fields related to error handling.
  383. *
  384. * Arguments: cmd - command that is ready to be queued.
  385. *
  386. * Notes: This function has the job of initializing a number of
  387. * fields related to error handling. Typically this will
  388. * be called once for each command, as required.
  389. */
  390. static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  391. {
  392. cmd->serial_number = 0;
  393. scsi_set_resid(cmd, 0);
  394. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  395. if (cmd->cmd_len == 0)
  396. cmd->cmd_len = scsi_command_size(cmd->cmnd);
  397. }
  398. void scsi_device_unbusy(struct scsi_device *sdev)
  399. {
  400. struct Scsi_Host *shost = sdev->host;
  401. unsigned long flags;
  402. spin_lock_irqsave(shost->host_lock, flags);
  403. shost->host_busy--;
  404. if (unlikely(scsi_host_in_recovery(shost) &&
  405. (shost->host_failed || shost->host_eh_scheduled)))
  406. scsi_eh_wakeup(shost);
  407. spin_unlock(shost->host_lock);
  408. spin_lock(sdev->request_queue->queue_lock);
  409. sdev->device_busy--;
  410. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  411. }
  412. /*
  413. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  414. * and call blk_run_queue for all the scsi_devices on the target -
  415. * including current_sdev first.
  416. *
  417. * Called with *no* scsi locks held.
  418. */
  419. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  420. {
  421. struct Scsi_Host *shost = current_sdev->host;
  422. struct scsi_device *sdev, *tmp;
  423. struct scsi_target *starget = scsi_target(current_sdev);
  424. unsigned long flags;
  425. spin_lock_irqsave(shost->host_lock, flags);
  426. starget->starget_sdev_user = NULL;
  427. spin_unlock_irqrestore(shost->host_lock, flags);
  428. /*
  429. * Call blk_run_queue for all LUNs on the target, starting with
  430. * current_sdev. We race with others (to set starget_sdev_user),
  431. * but in most cases, we will be first. Ideally, each LU on the
  432. * target would get some limited time or requests on the target.
  433. */
  434. blk_run_queue(current_sdev->request_queue);
  435. spin_lock_irqsave(shost->host_lock, flags);
  436. if (starget->starget_sdev_user)
  437. goto out;
  438. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  439. same_target_siblings) {
  440. if (sdev == current_sdev)
  441. continue;
  442. if (scsi_device_get(sdev))
  443. continue;
  444. spin_unlock_irqrestore(shost->host_lock, flags);
  445. blk_run_queue(sdev->request_queue);
  446. spin_lock_irqsave(shost->host_lock, flags);
  447. scsi_device_put(sdev);
  448. }
  449. out:
  450. spin_unlock_irqrestore(shost->host_lock, flags);
  451. }
  452. /*
  453. * Function: scsi_run_queue()
  454. *
  455. * Purpose: Select a proper request queue to serve next
  456. *
  457. * Arguments: q - last request's queue
  458. *
  459. * Returns: Nothing
  460. *
  461. * Notes: The previous command was completely finished, start
  462. * a new one if possible.
  463. */
  464. static void scsi_run_queue(struct request_queue *q)
  465. {
  466. struct scsi_device *sdev = q->queuedata;
  467. struct Scsi_Host *shost = sdev->host;
  468. unsigned long flags;
  469. if (scsi_target(sdev)->single_lun)
  470. scsi_single_lun_run(sdev);
  471. spin_lock_irqsave(shost->host_lock, flags);
  472. while (!list_empty(&shost->starved_list) &&
  473. !shost->host_blocked && !shost->host_self_blocked &&
  474. !((shost->can_queue > 0) &&
  475. (shost->host_busy >= shost->can_queue))) {
  476. int flagset;
  477. /*
  478. * As long as shost is accepting commands and we have
  479. * starved queues, call blk_run_queue. scsi_request_fn
  480. * drops the queue_lock and can add us back to the
  481. * starved_list.
  482. *
  483. * host_lock protects the starved_list and starved_entry.
  484. * scsi_request_fn must get the host_lock before checking
  485. * or modifying starved_list or starved_entry.
  486. */
  487. sdev = list_entry(shost->starved_list.next,
  488. struct scsi_device, starved_entry);
  489. list_del_init(&sdev->starved_entry);
  490. spin_unlock(shost->host_lock);
  491. spin_lock(sdev->request_queue->queue_lock);
  492. flagset = test_bit(QUEUE_FLAG_REENTER, &q->queue_flags) &&
  493. !test_bit(QUEUE_FLAG_REENTER,
  494. &sdev->request_queue->queue_flags);
  495. if (flagset)
  496. queue_flag_set(QUEUE_FLAG_REENTER, sdev->request_queue);
  497. __blk_run_queue(sdev->request_queue);
  498. if (flagset)
  499. queue_flag_clear(QUEUE_FLAG_REENTER, sdev->request_queue);
  500. spin_unlock(sdev->request_queue->queue_lock);
  501. spin_lock(shost->host_lock);
  502. if (unlikely(!list_empty(&sdev->starved_entry)))
  503. /*
  504. * sdev lost a race, and was put back on the
  505. * starved list. This is unlikely but without this
  506. * in theory we could loop forever.
  507. */
  508. break;
  509. }
  510. spin_unlock_irqrestore(shost->host_lock, flags);
  511. blk_run_queue(q);
  512. }
  513. /*
  514. * Function: scsi_requeue_command()
  515. *
  516. * Purpose: Handle post-processing of completed commands.
  517. *
  518. * Arguments: q - queue to operate on
  519. * cmd - command that may need to be requeued.
  520. *
  521. * Returns: Nothing
  522. *
  523. * Notes: After command completion, there may be blocks left
  524. * over which weren't finished by the previous command
  525. * this can be for a number of reasons - the main one is
  526. * I/O errors in the middle of the request, in which case
  527. * we need to request the blocks that come after the bad
  528. * sector.
  529. * Notes: Upon return, cmd is a stale pointer.
  530. */
  531. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  532. {
  533. struct request *req = cmd->request;
  534. unsigned long flags;
  535. scsi_unprep_request(req);
  536. spin_lock_irqsave(q->queue_lock, flags);
  537. blk_requeue_request(q, req);
  538. spin_unlock_irqrestore(q->queue_lock, flags);
  539. scsi_run_queue(q);
  540. }
  541. void scsi_next_command(struct scsi_cmnd *cmd)
  542. {
  543. struct scsi_device *sdev = cmd->device;
  544. struct request_queue *q = sdev->request_queue;
  545. /* need to hold a reference on the device before we let go of the cmd */
  546. get_device(&sdev->sdev_gendev);
  547. scsi_put_command(cmd);
  548. scsi_run_queue(q);
  549. /* ok to remove device now */
  550. put_device(&sdev->sdev_gendev);
  551. }
  552. void scsi_run_host_queues(struct Scsi_Host *shost)
  553. {
  554. struct scsi_device *sdev;
  555. shost_for_each_device(sdev, shost)
  556. scsi_run_queue(sdev->request_queue);
  557. }
  558. /*
  559. * Function: scsi_end_request()
  560. *
  561. * Purpose: Post-processing of completed commands (usually invoked at end
  562. * of upper level post-processing and scsi_io_completion).
  563. *
  564. * Arguments: cmd - command that is complete.
  565. * error - 0 if I/O indicates success, < 0 for I/O error.
  566. * bytes - number of bytes of completed I/O
  567. * requeue - indicates whether we should requeue leftovers.
  568. *
  569. * Lock status: Assumed that lock is not held upon entry.
  570. *
  571. * Returns: cmd if requeue required, NULL otherwise.
  572. *
  573. * Notes: This is called for block device requests in order to
  574. * mark some number of sectors as complete.
  575. *
  576. * We are guaranteeing that the request queue will be goosed
  577. * at some point during this call.
  578. * Notes: If cmd was requeued, upon return it will be a stale pointer.
  579. */
  580. static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int error,
  581. int bytes, int requeue)
  582. {
  583. struct request_queue *q = cmd->device->request_queue;
  584. struct request *req = cmd->request;
  585. /*
  586. * If there are blocks left over at the end, set up the command
  587. * to queue the remainder of them.
  588. */
  589. if (blk_end_request(req, error, bytes)) {
  590. int leftover = (req->hard_nr_sectors << 9);
  591. if (blk_pc_request(req))
  592. leftover = req->data_len;
  593. /* kill remainder if no retrys */
  594. if (error && blk_noretry_request(req))
  595. blk_end_request(req, error, leftover);
  596. else {
  597. if (requeue) {
  598. /*
  599. * Bleah. Leftovers again. Stick the
  600. * leftovers in the front of the
  601. * queue, and goose the queue again.
  602. */
  603. scsi_requeue_command(q, cmd);
  604. cmd = NULL;
  605. }
  606. return cmd;
  607. }
  608. }
  609. /*
  610. * This will goose the queue request function at the end, so we don't
  611. * need to worry about launching another command.
  612. */
  613. scsi_next_command(cmd);
  614. return NULL;
  615. }
  616. static inline unsigned int scsi_sgtable_index(unsigned short nents)
  617. {
  618. unsigned int index;
  619. BUG_ON(nents > SCSI_MAX_SG_SEGMENTS);
  620. if (nents <= 8)
  621. index = 0;
  622. else
  623. index = get_count_order(nents) - 3;
  624. return index;
  625. }
  626. static void scsi_sg_free(struct scatterlist *sgl, unsigned int nents)
  627. {
  628. struct scsi_host_sg_pool *sgp;
  629. sgp = scsi_sg_pools + scsi_sgtable_index(nents);
  630. mempool_free(sgl, sgp->pool);
  631. }
  632. static struct scatterlist *scsi_sg_alloc(unsigned int nents, gfp_t gfp_mask)
  633. {
  634. struct scsi_host_sg_pool *sgp;
  635. sgp = scsi_sg_pools + scsi_sgtable_index(nents);
  636. return mempool_alloc(sgp->pool, gfp_mask);
  637. }
  638. static int scsi_alloc_sgtable(struct scsi_data_buffer *sdb, int nents,
  639. gfp_t gfp_mask)
  640. {
  641. int ret;
  642. BUG_ON(!nents);
  643. ret = __sg_alloc_table(&sdb->table, nents, SCSI_MAX_SG_SEGMENTS,
  644. gfp_mask, scsi_sg_alloc);
  645. if (unlikely(ret))
  646. __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS,
  647. scsi_sg_free);
  648. return ret;
  649. }
  650. static void scsi_free_sgtable(struct scsi_data_buffer *sdb)
  651. {
  652. __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS, scsi_sg_free);
  653. }
  654. /*
  655. * Function: scsi_release_buffers()
  656. *
  657. * Purpose: Completion processing for block device I/O requests.
  658. *
  659. * Arguments: cmd - command that we are bailing.
  660. *
  661. * Lock status: Assumed that no lock is held upon entry.
  662. *
  663. * Returns: Nothing
  664. *
  665. * Notes: In the event that an upper level driver rejects a
  666. * command, we must release resources allocated during
  667. * the __init_io() function. Primarily this would involve
  668. * the scatter-gather table, and potentially any bounce
  669. * buffers.
  670. */
  671. void scsi_release_buffers(struct scsi_cmnd *cmd)
  672. {
  673. if (cmd->sdb.table.nents)
  674. scsi_free_sgtable(&cmd->sdb);
  675. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  676. if (scsi_bidi_cmnd(cmd)) {
  677. struct scsi_data_buffer *bidi_sdb =
  678. cmd->request->next_rq->special;
  679. scsi_free_sgtable(bidi_sdb);
  680. kmem_cache_free(scsi_sdb_cache, bidi_sdb);
  681. cmd->request->next_rq->special = NULL;
  682. }
  683. }
  684. EXPORT_SYMBOL(scsi_release_buffers);
  685. /*
  686. * Bidi commands Must be complete as a whole, both sides at once.
  687. * If part of the bytes were written and lld returned
  688. * scsi_in()->resid and/or scsi_out()->resid this information will be left
  689. * in req->data_len and req->next_rq->data_len. The upper-layer driver can
  690. * decide what to do with this information.
  691. */
  692. static void scsi_end_bidi_request(struct scsi_cmnd *cmd)
  693. {
  694. struct request *req = cmd->request;
  695. unsigned int dlen = req->data_len;
  696. unsigned int next_dlen = req->next_rq->data_len;
  697. req->data_len = scsi_out(cmd)->resid;
  698. req->next_rq->data_len = scsi_in(cmd)->resid;
  699. /* The req and req->next_rq have not been completed */
  700. BUG_ON(blk_end_bidi_request(req, 0, dlen, next_dlen));
  701. scsi_release_buffers(cmd);
  702. /*
  703. * This will goose the queue request function at the end, so we don't
  704. * need to worry about launching another command.
  705. */
  706. scsi_next_command(cmd);
  707. }
  708. /*
  709. * Function: scsi_io_completion()
  710. *
  711. * Purpose: Completion processing for block device I/O requests.
  712. *
  713. * Arguments: cmd - command that is finished.
  714. *
  715. * Lock status: Assumed that no lock is held upon entry.
  716. *
  717. * Returns: Nothing
  718. *
  719. * Notes: This function is matched in terms of capabilities to
  720. * the function that created the scatter-gather list.
  721. * In other words, if there are no bounce buffers
  722. * (the normal case for most drivers), we don't need
  723. * the logic to deal with cleaning up afterwards.
  724. *
  725. * We must do one of several things here:
  726. *
  727. * a) Call scsi_end_request. This will finish off the
  728. * specified number of sectors. If we are done, the
  729. * command block will be released, and the queue
  730. * function will be goosed. If we are not done, then
  731. * scsi_end_request will directly goose the queue.
  732. *
  733. * b) We can just use scsi_requeue_command() here. This would
  734. * be used if we just wanted to retry, for example.
  735. */
  736. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
  737. {
  738. int result = cmd->result;
  739. int this_count = scsi_bufflen(cmd);
  740. struct request_queue *q = cmd->device->request_queue;
  741. struct request *req = cmd->request;
  742. int error = 0;
  743. struct scsi_sense_hdr sshdr;
  744. int sense_valid = 0;
  745. int sense_deferred = 0;
  746. if (result) {
  747. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  748. if (sense_valid)
  749. sense_deferred = scsi_sense_is_deferred(&sshdr);
  750. }
  751. if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
  752. req->errors = result;
  753. if (result) {
  754. if (sense_valid && req->sense) {
  755. /*
  756. * SG_IO wants current and deferred errors
  757. */
  758. int len = 8 + cmd->sense_buffer[7];
  759. if (len > SCSI_SENSE_BUFFERSIZE)
  760. len = SCSI_SENSE_BUFFERSIZE;
  761. memcpy(req->sense, cmd->sense_buffer, len);
  762. req->sense_len = len;
  763. }
  764. if (!sense_deferred)
  765. error = -EIO;
  766. }
  767. if (scsi_bidi_cmnd(cmd)) {
  768. /* will also release_buffers */
  769. scsi_end_bidi_request(cmd);
  770. return;
  771. }
  772. req->data_len = scsi_get_resid(cmd);
  773. }
  774. BUG_ON(blk_bidi_rq(req)); /* bidi not support for !blk_pc_request yet */
  775. scsi_release_buffers(cmd);
  776. /*
  777. * Next deal with any sectors which we were able to correctly
  778. * handle.
  779. */
  780. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, "
  781. "%d bytes done.\n",
  782. req->nr_sectors, good_bytes));
  783. /* A number of bytes were successfully read. If there
  784. * are leftovers and there is some kind of error
  785. * (result != 0), retry the rest.
  786. */
  787. if (scsi_end_request(cmd, error, good_bytes, result == 0) == NULL)
  788. return;
  789. /* good_bytes = 0, or (inclusive) there were leftovers and
  790. * result = 0, so scsi_end_request couldn't retry.
  791. */
  792. if (sense_valid && !sense_deferred) {
  793. switch (sshdr.sense_key) {
  794. case UNIT_ATTENTION:
  795. if (cmd->device->removable) {
  796. /* Detected disc change. Set a bit
  797. * and quietly refuse further access.
  798. */
  799. cmd->device->changed = 1;
  800. scsi_end_request(cmd, -EIO, this_count, 1);
  801. return;
  802. } else {
  803. /* Must have been a power glitch, or a
  804. * bus reset. Could not have been a
  805. * media change, so we just retry the
  806. * request and see what happens.
  807. */
  808. scsi_requeue_command(q, cmd);
  809. return;
  810. }
  811. break;
  812. case ILLEGAL_REQUEST:
  813. /* If we had an ILLEGAL REQUEST returned, then
  814. * we may have performed an unsupported
  815. * command. The only thing this should be
  816. * would be a ten byte read where only a six
  817. * byte read was supported. Also, on a system
  818. * where READ CAPACITY failed, we may have
  819. * read past the end of the disk.
  820. */
  821. if ((cmd->device->use_10_for_rw &&
  822. sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
  823. (cmd->cmnd[0] == READ_10 ||
  824. cmd->cmnd[0] == WRITE_10)) {
  825. cmd->device->use_10_for_rw = 0;
  826. /* This will cause a retry with a
  827. * 6-byte command.
  828. */
  829. scsi_requeue_command(q, cmd);
  830. return;
  831. } else {
  832. scsi_end_request(cmd, -EIO, this_count, 1);
  833. return;
  834. }
  835. break;
  836. case NOT_READY:
  837. /* If the device is in the process of becoming
  838. * ready, or has a temporary blockage, retry.
  839. */
  840. if (sshdr.asc == 0x04) {
  841. switch (sshdr.ascq) {
  842. case 0x01: /* becoming ready */
  843. case 0x04: /* format in progress */
  844. case 0x05: /* rebuild in progress */
  845. case 0x06: /* recalculation in progress */
  846. case 0x07: /* operation in progress */
  847. case 0x08: /* Long write in progress */
  848. case 0x09: /* self test in progress */
  849. scsi_requeue_command(q, cmd);
  850. return;
  851. default:
  852. break;
  853. }
  854. }
  855. if (!(req->cmd_flags & REQ_QUIET))
  856. scsi_cmd_print_sense_hdr(cmd,
  857. "Device not ready",
  858. &sshdr);
  859. scsi_end_request(cmd, -EIO, this_count, 1);
  860. return;
  861. case VOLUME_OVERFLOW:
  862. if (!(req->cmd_flags & REQ_QUIET)) {
  863. scmd_printk(KERN_INFO, cmd,
  864. "Volume overflow, CDB: ");
  865. __scsi_print_command(cmd->cmnd);
  866. scsi_print_sense("", cmd);
  867. }
  868. /* See SSC3rXX or current. */
  869. scsi_end_request(cmd, -EIO, this_count, 1);
  870. return;
  871. default:
  872. break;
  873. }
  874. }
  875. if (host_byte(result) == DID_RESET) {
  876. /* Third party bus reset or reset for error recovery
  877. * reasons. Just retry the request and see what
  878. * happens.
  879. */
  880. scsi_requeue_command(q, cmd);
  881. return;
  882. }
  883. if (result) {
  884. if (!(req->cmd_flags & REQ_QUIET)) {
  885. scsi_print_result(cmd);
  886. if (driver_byte(result) & DRIVER_SENSE)
  887. scsi_print_sense("", cmd);
  888. }
  889. }
  890. scsi_end_request(cmd, -EIO, this_count, !result);
  891. }
  892. static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb,
  893. gfp_t gfp_mask)
  894. {
  895. int count;
  896. /*
  897. * If sg table allocation fails, requeue request later.
  898. */
  899. if (unlikely(scsi_alloc_sgtable(sdb, req->nr_phys_segments,
  900. gfp_mask))) {
  901. return BLKPREP_DEFER;
  902. }
  903. req->buffer = NULL;
  904. /*
  905. * Next, walk the list, and fill in the addresses and sizes of
  906. * each segment.
  907. */
  908. count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
  909. BUG_ON(count > sdb->table.nents);
  910. sdb->table.nents = count;
  911. if (blk_pc_request(req))
  912. sdb->length = req->data_len;
  913. else
  914. sdb->length = req->nr_sectors << 9;
  915. return BLKPREP_OK;
  916. }
  917. /*
  918. * Function: scsi_init_io()
  919. *
  920. * Purpose: SCSI I/O initialize function.
  921. *
  922. * Arguments: cmd - Command descriptor we wish to initialize
  923. *
  924. * Returns: 0 on success
  925. * BLKPREP_DEFER if the failure is retryable
  926. * BLKPREP_KILL if the failure is fatal
  927. */
  928. int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  929. {
  930. int error = scsi_init_sgtable(cmd->request, &cmd->sdb, gfp_mask);
  931. if (error)
  932. goto err_exit;
  933. if (blk_bidi_rq(cmd->request)) {
  934. struct scsi_data_buffer *bidi_sdb = kmem_cache_zalloc(
  935. scsi_sdb_cache, GFP_ATOMIC);
  936. if (!bidi_sdb) {
  937. error = BLKPREP_DEFER;
  938. goto err_exit;
  939. }
  940. cmd->request->next_rq->special = bidi_sdb;
  941. error = scsi_init_sgtable(cmd->request->next_rq, bidi_sdb,
  942. GFP_ATOMIC);
  943. if (error)
  944. goto err_exit;
  945. }
  946. return BLKPREP_OK ;
  947. err_exit:
  948. scsi_release_buffers(cmd);
  949. if (error == BLKPREP_KILL)
  950. scsi_put_command(cmd);
  951. else /* BLKPREP_DEFER */
  952. scsi_unprep_request(cmd->request);
  953. return error;
  954. }
  955. EXPORT_SYMBOL(scsi_init_io);
  956. static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
  957. struct request *req)
  958. {
  959. struct scsi_cmnd *cmd;
  960. if (!req->special) {
  961. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  962. if (unlikely(!cmd))
  963. return NULL;
  964. req->special = cmd;
  965. } else {
  966. cmd = req->special;
  967. }
  968. /* pull a tag out of the request if we have one */
  969. cmd->tag = req->tag;
  970. cmd->request = req;
  971. cmd->cmnd = req->cmd;
  972. return cmd;
  973. }
  974. int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
  975. {
  976. struct scsi_cmnd *cmd;
  977. int ret = scsi_prep_state_check(sdev, req);
  978. if (ret != BLKPREP_OK)
  979. return ret;
  980. cmd = scsi_get_cmd_from_req(sdev, req);
  981. if (unlikely(!cmd))
  982. return BLKPREP_DEFER;
  983. /*
  984. * BLOCK_PC requests may transfer data, in which case they must
  985. * a bio attached to them. Or they might contain a SCSI command
  986. * that does not transfer data, in which case they may optionally
  987. * submit a request without an attached bio.
  988. */
  989. if (req->bio) {
  990. int ret;
  991. BUG_ON(!req->nr_phys_segments);
  992. ret = scsi_init_io(cmd, GFP_ATOMIC);
  993. if (unlikely(ret))
  994. return ret;
  995. } else {
  996. BUG_ON(req->data_len);
  997. BUG_ON(req->data);
  998. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  999. req->buffer = NULL;
  1000. }
  1001. cmd->cmd_len = req->cmd_len;
  1002. if (!req->data_len)
  1003. cmd->sc_data_direction = DMA_NONE;
  1004. else if (rq_data_dir(req) == WRITE)
  1005. cmd->sc_data_direction = DMA_TO_DEVICE;
  1006. else
  1007. cmd->sc_data_direction = DMA_FROM_DEVICE;
  1008. cmd->transfersize = req->data_len;
  1009. cmd->allowed = req->retries;
  1010. cmd->timeout_per_command = req->timeout;
  1011. return BLKPREP_OK;
  1012. }
  1013. EXPORT_SYMBOL(scsi_setup_blk_pc_cmnd);
  1014. /*
  1015. * Setup a REQ_TYPE_FS command. These are simple read/write request
  1016. * from filesystems that still need to be translated to SCSI CDBs from
  1017. * the ULD.
  1018. */
  1019. int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
  1020. {
  1021. struct scsi_cmnd *cmd;
  1022. int ret = scsi_prep_state_check(sdev, req);
  1023. if (ret != BLKPREP_OK)
  1024. return ret;
  1025. if (unlikely(sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh
  1026. && sdev->scsi_dh_data->scsi_dh->prep_fn)) {
  1027. ret = sdev->scsi_dh_data->scsi_dh->prep_fn(sdev, req);
  1028. if (ret != BLKPREP_OK)
  1029. return ret;
  1030. }
  1031. /*
  1032. * Filesystem requests must transfer data.
  1033. */
  1034. BUG_ON(!req->nr_phys_segments);
  1035. cmd = scsi_get_cmd_from_req(sdev, req);
  1036. if (unlikely(!cmd))
  1037. return BLKPREP_DEFER;
  1038. memset(cmd->cmnd, 0, BLK_MAX_CDB);
  1039. return scsi_init_io(cmd, GFP_ATOMIC);
  1040. }
  1041. EXPORT_SYMBOL(scsi_setup_fs_cmnd);
  1042. int scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
  1043. {
  1044. int ret = BLKPREP_OK;
  1045. /*
  1046. * If the device is not in running state we will reject some
  1047. * or all commands.
  1048. */
  1049. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  1050. switch (sdev->sdev_state) {
  1051. case SDEV_OFFLINE:
  1052. /*
  1053. * If the device is offline we refuse to process any
  1054. * commands. The device must be brought online
  1055. * before trying any recovery commands.
  1056. */
  1057. sdev_printk(KERN_ERR, sdev,
  1058. "rejecting I/O to offline device\n");
  1059. ret = BLKPREP_KILL;
  1060. break;
  1061. case SDEV_DEL:
  1062. /*
  1063. * If the device is fully deleted, we refuse to
  1064. * process any commands as well.
  1065. */
  1066. sdev_printk(KERN_ERR, sdev,
  1067. "rejecting I/O to dead device\n");
  1068. ret = BLKPREP_KILL;
  1069. break;
  1070. case SDEV_QUIESCE:
  1071. case SDEV_BLOCK:
  1072. /*
  1073. * If the devices is blocked we defer normal commands.
  1074. */
  1075. if (!(req->cmd_flags & REQ_PREEMPT))
  1076. ret = BLKPREP_DEFER;
  1077. break;
  1078. default:
  1079. /*
  1080. * For any other not fully online state we only allow
  1081. * special commands. In particular any user initiated
  1082. * command is not allowed.
  1083. */
  1084. if (!(req->cmd_flags & REQ_PREEMPT))
  1085. ret = BLKPREP_KILL;
  1086. break;
  1087. }
  1088. }
  1089. return ret;
  1090. }
  1091. EXPORT_SYMBOL(scsi_prep_state_check);
  1092. int scsi_prep_return(struct request_queue *q, struct request *req, int ret)
  1093. {
  1094. struct scsi_device *sdev = q->queuedata;
  1095. switch (ret) {
  1096. case BLKPREP_KILL:
  1097. req->errors = DID_NO_CONNECT << 16;
  1098. /* release the command and kill it */
  1099. if (req->special) {
  1100. struct scsi_cmnd *cmd = req->special;
  1101. scsi_release_buffers(cmd);
  1102. scsi_put_command(cmd);
  1103. req->special = NULL;
  1104. }
  1105. break;
  1106. case BLKPREP_DEFER:
  1107. /*
  1108. * If we defer, the elv_next_request() returns NULL, but the
  1109. * queue must be restarted, so we plug here if no returning
  1110. * command will automatically do that.
  1111. */
  1112. if (sdev->device_busy == 0)
  1113. blk_plug_device(q);
  1114. break;
  1115. default:
  1116. req->cmd_flags |= REQ_DONTPREP;
  1117. }
  1118. return ret;
  1119. }
  1120. EXPORT_SYMBOL(scsi_prep_return);
  1121. int scsi_prep_fn(struct request_queue *q, struct request *req)
  1122. {
  1123. struct scsi_device *sdev = q->queuedata;
  1124. int ret = BLKPREP_KILL;
  1125. if (req->cmd_type == REQ_TYPE_BLOCK_PC)
  1126. ret = scsi_setup_blk_pc_cmnd(sdev, req);
  1127. return scsi_prep_return(q, req, ret);
  1128. }
  1129. /*
  1130. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1131. * return 0.
  1132. *
  1133. * Called with the queue_lock held.
  1134. */
  1135. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1136. struct scsi_device *sdev)
  1137. {
  1138. if (sdev->device_busy >= sdev->queue_depth)
  1139. return 0;
  1140. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1141. /*
  1142. * unblock after device_blocked iterates to zero
  1143. */
  1144. if (--sdev->device_blocked == 0) {
  1145. SCSI_LOG_MLQUEUE(3,
  1146. sdev_printk(KERN_INFO, sdev,
  1147. "unblocking device at zero depth\n"));
  1148. } else {
  1149. blk_plug_device(q);
  1150. return 0;
  1151. }
  1152. }
  1153. if (sdev->device_blocked)
  1154. return 0;
  1155. return 1;
  1156. }
  1157. /*
  1158. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1159. * return 0. We must end up running the queue again whenever 0 is
  1160. * returned, else IO can hang.
  1161. *
  1162. * Called with host_lock held.
  1163. */
  1164. static inline int scsi_host_queue_ready(struct request_queue *q,
  1165. struct Scsi_Host *shost,
  1166. struct scsi_device *sdev)
  1167. {
  1168. if (scsi_host_in_recovery(shost))
  1169. return 0;
  1170. if (shost->host_busy == 0 && shost->host_blocked) {
  1171. /*
  1172. * unblock after host_blocked iterates to zero
  1173. */
  1174. if (--shost->host_blocked == 0) {
  1175. SCSI_LOG_MLQUEUE(3,
  1176. printk("scsi%d unblocking host at zero depth\n",
  1177. shost->host_no));
  1178. } else {
  1179. return 0;
  1180. }
  1181. }
  1182. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1183. shost->host_blocked || shost->host_self_blocked) {
  1184. if (list_empty(&sdev->starved_entry))
  1185. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1186. return 0;
  1187. }
  1188. /* We're OK to process the command, so we can't be starved */
  1189. if (!list_empty(&sdev->starved_entry))
  1190. list_del_init(&sdev->starved_entry);
  1191. return 1;
  1192. }
  1193. /*
  1194. * Kill a request for a dead device
  1195. */
  1196. static void scsi_kill_request(struct request *req, struct request_queue *q)
  1197. {
  1198. struct scsi_cmnd *cmd = req->special;
  1199. struct scsi_device *sdev = cmd->device;
  1200. struct Scsi_Host *shost = sdev->host;
  1201. blkdev_dequeue_request(req);
  1202. if (unlikely(cmd == NULL)) {
  1203. printk(KERN_CRIT "impossible request in %s.\n",
  1204. __FUNCTION__);
  1205. BUG();
  1206. }
  1207. scsi_init_cmd_errh(cmd);
  1208. cmd->result = DID_NO_CONNECT << 16;
  1209. atomic_inc(&cmd->device->iorequest_cnt);
  1210. /*
  1211. * SCSI request completion path will do scsi_device_unbusy(),
  1212. * bump busy counts. To bump the counters, we need to dance
  1213. * with the locks as normal issue path does.
  1214. */
  1215. sdev->device_busy++;
  1216. spin_unlock(sdev->request_queue->queue_lock);
  1217. spin_lock(shost->host_lock);
  1218. shost->host_busy++;
  1219. spin_unlock(shost->host_lock);
  1220. spin_lock(sdev->request_queue->queue_lock);
  1221. __scsi_done(cmd);
  1222. }
  1223. static void scsi_softirq_done(struct request *rq)
  1224. {
  1225. struct scsi_cmnd *cmd = rq->completion_data;
  1226. unsigned long wait_for = (cmd->allowed + 1) * cmd->timeout_per_command;
  1227. int disposition;
  1228. INIT_LIST_HEAD(&cmd->eh_entry);
  1229. disposition = scsi_decide_disposition(cmd);
  1230. if (disposition != SUCCESS &&
  1231. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1232. sdev_printk(KERN_ERR, cmd->device,
  1233. "timing out command, waited %lus\n",
  1234. wait_for/HZ);
  1235. disposition = SUCCESS;
  1236. }
  1237. scsi_log_completion(cmd, disposition);
  1238. switch (disposition) {
  1239. case SUCCESS:
  1240. scsi_finish_command(cmd);
  1241. break;
  1242. case NEEDS_RETRY:
  1243. scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  1244. break;
  1245. case ADD_TO_MLQUEUE:
  1246. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1247. break;
  1248. default:
  1249. if (!scsi_eh_scmd_add(cmd, 0))
  1250. scsi_finish_command(cmd);
  1251. }
  1252. }
  1253. /*
  1254. * Function: scsi_request_fn()
  1255. *
  1256. * Purpose: Main strategy routine for SCSI.
  1257. *
  1258. * Arguments: q - Pointer to actual queue.
  1259. *
  1260. * Returns: Nothing
  1261. *
  1262. * Lock status: IO request lock assumed to be held when called.
  1263. */
  1264. static void scsi_request_fn(struct request_queue *q)
  1265. {
  1266. struct scsi_device *sdev = q->queuedata;
  1267. struct Scsi_Host *shost;
  1268. struct scsi_cmnd *cmd;
  1269. struct request *req;
  1270. if (!sdev) {
  1271. printk("scsi: killing requests for dead queue\n");
  1272. while ((req = elv_next_request(q)) != NULL)
  1273. scsi_kill_request(req, q);
  1274. return;
  1275. }
  1276. if(!get_device(&sdev->sdev_gendev))
  1277. /* We must be tearing the block queue down already */
  1278. return;
  1279. /*
  1280. * To start with, we keep looping until the queue is empty, or until
  1281. * the host is no longer able to accept any more requests.
  1282. */
  1283. shost = sdev->host;
  1284. while (!blk_queue_plugged(q)) {
  1285. int rtn;
  1286. /*
  1287. * get next queueable request. We do this early to make sure
  1288. * that the request is fully prepared even if we cannot
  1289. * accept it.
  1290. */
  1291. req = elv_next_request(q);
  1292. if (!req || !scsi_dev_queue_ready(q, sdev))
  1293. break;
  1294. if (unlikely(!scsi_device_online(sdev))) {
  1295. sdev_printk(KERN_ERR, sdev,
  1296. "rejecting I/O to offline device\n");
  1297. scsi_kill_request(req, q);
  1298. continue;
  1299. }
  1300. /*
  1301. * Remove the request from the request list.
  1302. */
  1303. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1304. blkdev_dequeue_request(req);
  1305. sdev->device_busy++;
  1306. spin_unlock(q->queue_lock);
  1307. cmd = req->special;
  1308. if (unlikely(cmd == NULL)) {
  1309. printk(KERN_CRIT "impossible request in %s.\n"
  1310. "please mail a stack trace to "
  1311. "linux-scsi@vger.kernel.org\n",
  1312. __FUNCTION__);
  1313. blk_dump_rq_flags(req, "foo");
  1314. BUG();
  1315. }
  1316. spin_lock(shost->host_lock);
  1317. if (!scsi_host_queue_ready(q, shost, sdev))
  1318. goto not_ready;
  1319. if (scsi_target(sdev)->single_lun) {
  1320. if (scsi_target(sdev)->starget_sdev_user &&
  1321. scsi_target(sdev)->starget_sdev_user != sdev)
  1322. goto not_ready;
  1323. scsi_target(sdev)->starget_sdev_user = sdev;
  1324. }
  1325. shost->host_busy++;
  1326. /*
  1327. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1328. * take the lock again.
  1329. */
  1330. spin_unlock_irq(shost->host_lock);
  1331. /*
  1332. * Finally, initialize any error handling parameters, and set up
  1333. * the timers for timeouts.
  1334. */
  1335. scsi_init_cmd_errh(cmd);
  1336. /*
  1337. * Dispatch the command to the low-level driver.
  1338. */
  1339. rtn = scsi_dispatch_cmd(cmd);
  1340. spin_lock_irq(q->queue_lock);
  1341. if(rtn) {
  1342. /* we're refusing the command; because of
  1343. * the way locks get dropped, we need to
  1344. * check here if plugging is required */
  1345. if(sdev->device_busy == 0)
  1346. blk_plug_device(q);
  1347. break;
  1348. }
  1349. }
  1350. goto out;
  1351. not_ready:
  1352. spin_unlock_irq(shost->host_lock);
  1353. /*
  1354. * lock q, handle tag, requeue req, and decrement device_busy. We
  1355. * must return with queue_lock held.
  1356. *
  1357. * Decrementing device_busy without checking it is OK, as all such
  1358. * cases (host limits or settings) should run the queue at some
  1359. * later time.
  1360. */
  1361. spin_lock_irq(q->queue_lock);
  1362. blk_requeue_request(q, req);
  1363. sdev->device_busy--;
  1364. if(sdev->device_busy == 0)
  1365. blk_plug_device(q);
  1366. out:
  1367. /* must be careful here...if we trigger the ->remove() function
  1368. * we cannot be holding the q lock */
  1369. spin_unlock_irq(q->queue_lock);
  1370. put_device(&sdev->sdev_gendev);
  1371. spin_lock_irq(q->queue_lock);
  1372. }
  1373. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1374. {
  1375. struct device *host_dev;
  1376. u64 bounce_limit = 0xffffffff;
  1377. if (shost->unchecked_isa_dma)
  1378. return BLK_BOUNCE_ISA;
  1379. /*
  1380. * Platforms with virtual-DMA translation
  1381. * hardware have no practical limit.
  1382. */
  1383. if (!PCI_DMA_BUS_IS_PHYS)
  1384. return BLK_BOUNCE_ANY;
  1385. host_dev = scsi_get_device(shost);
  1386. if (host_dev && host_dev->dma_mask)
  1387. bounce_limit = *host_dev->dma_mask;
  1388. return bounce_limit;
  1389. }
  1390. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1391. struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
  1392. request_fn_proc *request_fn)
  1393. {
  1394. struct request_queue *q;
  1395. struct device *dev = shost->shost_gendev.parent;
  1396. q = blk_init_queue(request_fn, NULL);
  1397. if (!q)
  1398. return NULL;
  1399. /*
  1400. * this limit is imposed by hardware restrictions
  1401. */
  1402. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1403. blk_queue_max_phys_segments(q, SCSI_MAX_SG_CHAIN_SEGMENTS);
  1404. blk_queue_max_sectors(q, shost->max_sectors);
  1405. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1406. blk_queue_segment_boundary(q, shost->dma_boundary);
  1407. dma_set_seg_boundary(dev, shost->dma_boundary);
  1408. blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
  1409. /* New queue, no concurrency on queue_flags */
  1410. if (!shost->use_clustering)
  1411. queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
  1412. /*
  1413. * set a reasonable default alignment on word boundaries: the
  1414. * host and device may alter it using
  1415. * blk_queue_update_dma_alignment() later.
  1416. */
  1417. blk_queue_dma_alignment(q, 0x03);
  1418. return q;
  1419. }
  1420. EXPORT_SYMBOL(__scsi_alloc_queue);
  1421. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1422. {
  1423. struct request_queue *q;
  1424. q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
  1425. if (!q)
  1426. return NULL;
  1427. blk_queue_prep_rq(q, scsi_prep_fn);
  1428. blk_queue_softirq_done(q, scsi_softirq_done);
  1429. return q;
  1430. }
  1431. void scsi_free_queue(struct request_queue *q)
  1432. {
  1433. blk_cleanup_queue(q);
  1434. }
  1435. /*
  1436. * Function: scsi_block_requests()
  1437. *
  1438. * Purpose: Utility function used by low-level drivers to prevent further
  1439. * commands from being queued to the device.
  1440. *
  1441. * Arguments: shost - Host in question
  1442. *
  1443. * Returns: Nothing
  1444. *
  1445. * Lock status: No locks are assumed held.
  1446. *
  1447. * Notes: There is no timer nor any other means by which the requests
  1448. * get unblocked other than the low-level driver calling
  1449. * scsi_unblock_requests().
  1450. */
  1451. void scsi_block_requests(struct Scsi_Host *shost)
  1452. {
  1453. shost->host_self_blocked = 1;
  1454. }
  1455. EXPORT_SYMBOL(scsi_block_requests);
  1456. /*
  1457. * Function: scsi_unblock_requests()
  1458. *
  1459. * Purpose: Utility function used by low-level drivers to allow further
  1460. * commands from being queued to the device.
  1461. *
  1462. * Arguments: shost - Host in question
  1463. *
  1464. * Returns: Nothing
  1465. *
  1466. * Lock status: No locks are assumed held.
  1467. *
  1468. * Notes: There is no timer nor any other means by which the requests
  1469. * get unblocked other than the low-level driver calling
  1470. * scsi_unblock_requests().
  1471. *
  1472. * This is done as an API function so that changes to the
  1473. * internals of the scsi mid-layer won't require wholesale
  1474. * changes to drivers that use this feature.
  1475. */
  1476. void scsi_unblock_requests(struct Scsi_Host *shost)
  1477. {
  1478. shost->host_self_blocked = 0;
  1479. scsi_run_host_queues(shost);
  1480. }
  1481. EXPORT_SYMBOL(scsi_unblock_requests);
  1482. int __init scsi_init_queue(void)
  1483. {
  1484. int i;
  1485. scsi_io_context_cache = kmem_cache_create("scsi_io_context",
  1486. sizeof(struct scsi_io_context),
  1487. 0, 0, NULL);
  1488. if (!scsi_io_context_cache) {
  1489. printk(KERN_ERR "SCSI: can't init scsi io context cache\n");
  1490. return -ENOMEM;
  1491. }
  1492. scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
  1493. sizeof(struct scsi_data_buffer),
  1494. 0, 0, NULL);
  1495. if (!scsi_sdb_cache) {
  1496. printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
  1497. goto cleanup_io_context;
  1498. }
  1499. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1500. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1501. int size = sgp->size * sizeof(struct scatterlist);
  1502. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1503. SLAB_HWCACHE_ALIGN, NULL);
  1504. if (!sgp->slab) {
  1505. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1506. sgp->name);
  1507. goto cleanup_sdb;
  1508. }
  1509. sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
  1510. sgp->slab);
  1511. if (!sgp->pool) {
  1512. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1513. sgp->name);
  1514. goto cleanup_sdb;
  1515. }
  1516. }
  1517. return 0;
  1518. cleanup_sdb:
  1519. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1520. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1521. if (sgp->pool)
  1522. mempool_destroy(sgp->pool);
  1523. if (sgp->slab)
  1524. kmem_cache_destroy(sgp->slab);
  1525. }
  1526. kmem_cache_destroy(scsi_sdb_cache);
  1527. cleanup_io_context:
  1528. kmem_cache_destroy(scsi_io_context_cache);
  1529. return -ENOMEM;
  1530. }
  1531. void scsi_exit_queue(void)
  1532. {
  1533. int i;
  1534. kmem_cache_destroy(scsi_io_context_cache);
  1535. kmem_cache_destroy(scsi_sdb_cache);
  1536. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1537. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1538. mempool_destroy(sgp->pool);
  1539. kmem_cache_destroy(sgp->slab);
  1540. }
  1541. }
  1542. /**
  1543. * scsi_mode_select - issue a mode select
  1544. * @sdev: SCSI device to be queried
  1545. * @pf: Page format bit (1 == standard, 0 == vendor specific)
  1546. * @sp: Save page bit (0 == don't save, 1 == save)
  1547. * @modepage: mode page being requested
  1548. * @buffer: request buffer (may not be smaller than eight bytes)
  1549. * @len: length of request buffer.
  1550. * @timeout: command timeout
  1551. * @retries: number of retries before failing
  1552. * @data: returns a structure abstracting the mode header data
  1553. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  1554. * must be SCSI_SENSE_BUFFERSIZE big.
  1555. *
  1556. * Returns zero if successful; negative error number or scsi
  1557. * status on error
  1558. *
  1559. */
  1560. int
  1561. scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
  1562. unsigned char *buffer, int len, int timeout, int retries,
  1563. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1564. {
  1565. unsigned char cmd[10];
  1566. unsigned char *real_buffer;
  1567. int ret;
  1568. memset(cmd, 0, sizeof(cmd));
  1569. cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
  1570. if (sdev->use_10_for_ms) {
  1571. if (len > 65535)
  1572. return -EINVAL;
  1573. real_buffer = kmalloc(8 + len, GFP_KERNEL);
  1574. if (!real_buffer)
  1575. return -ENOMEM;
  1576. memcpy(real_buffer + 8, buffer, len);
  1577. len += 8;
  1578. real_buffer[0] = 0;
  1579. real_buffer[1] = 0;
  1580. real_buffer[2] = data->medium_type;
  1581. real_buffer[3] = data->device_specific;
  1582. real_buffer[4] = data->longlba ? 0x01 : 0;
  1583. real_buffer[5] = 0;
  1584. real_buffer[6] = data->block_descriptor_length >> 8;
  1585. real_buffer[7] = data->block_descriptor_length;
  1586. cmd[0] = MODE_SELECT_10;
  1587. cmd[7] = len >> 8;
  1588. cmd[8] = len;
  1589. } else {
  1590. if (len > 255 || data->block_descriptor_length > 255 ||
  1591. data->longlba)
  1592. return -EINVAL;
  1593. real_buffer = kmalloc(4 + len, GFP_KERNEL);
  1594. if (!real_buffer)
  1595. return -ENOMEM;
  1596. memcpy(real_buffer + 4, buffer, len);
  1597. len += 4;
  1598. real_buffer[0] = 0;
  1599. real_buffer[1] = data->medium_type;
  1600. real_buffer[2] = data->device_specific;
  1601. real_buffer[3] = data->block_descriptor_length;
  1602. cmd[0] = MODE_SELECT;
  1603. cmd[4] = len;
  1604. }
  1605. ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
  1606. sshdr, timeout, retries);
  1607. kfree(real_buffer);
  1608. return ret;
  1609. }
  1610. EXPORT_SYMBOL_GPL(scsi_mode_select);
  1611. /**
  1612. * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
  1613. * @sdev: SCSI device to be queried
  1614. * @dbd: set if mode sense will allow block descriptors to be returned
  1615. * @modepage: mode page being requested
  1616. * @buffer: request buffer (may not be smaller than eight bytes)
  1617. * @len: length of request buffer.
  1618. * @timeout: command timeout
  1619. * @retries: number of retries before failing
  1620. * @data: returns a structure abstracting the mode header data
  1621. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  1622. * must be SCSI_SENSE_BUFFERSIZE big.
  1623. *
  1624. * Returns zero if unsuccessful, or the header offset (either 4
  1625. * or 8 depending on whether a six or ten byte command was
  1626. * issued) if successful.
  1627. */
  1628. int
  1629. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1630. unsigned char *buffer, int len, int timeout, int retries,
  1631. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1632. {
  1633. unsigned char cmd[12];
  1634. int use_10_for_ms;
  1635. int header_length;
  1636. int result;
  1637. struct scsi_sense_hdr my_sshdr;
  1638. memset(data, 0, sizeof(*data));
  1639. memset(&cmd[0], 0, 12);
  1640. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1641. cmd[2] = modepage;
  1642. /* caller might not be interested in sense, but we need it */
  1643. if (!sshdr)
  1644. sshdr = &my_sshdr;
  1645. retry:
  1646. use_10_for_ms = sdev->use_10_for_ms;
  1647. if (use_10_for_ms) {
  1648. if (len < 8)
  1649. len = 8;
  1650. cmd[0] = MODE_SENSE_10;
  1651. cmd[8] = len;
  1652. header_length = 8;
  1653. } else {
  1654. if (len < 4)
  1655. len = 4;
  1656. cmd[0] = MODE_SENSE;
  1657. cmd[4] = len;
  1658. header_length = 4;
  1659. }
  1660. memset(buffer, 0, len);
  1661. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  1662. sshdr, timeout, retries);
  1663. /* This code looks awful: what it's doing is making sure an
  1664. * ILLEGAL REQUEST sense return identifies the actual command
  1665. * byte as the problem. MODE_SENSE commands can return
  1666. * ILLEGAL REQUEST if the code page isn't supported */
  1667. if (use_10_for_ms && !scsi_status_is_good(result) &&
  1668. (driver_byte(result) & DRIVER_SENSE)) {
  1669. if (scsi_sense_valid(sshdr)) {
  1670. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  1671. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  1672. /*
  1673. * Invalid command operation code
  1674. */
  1675. sdev->use_10_for_ms = 0;
  1676. goto retry;
  1677. }
  1678. }
  1679. }
  1680. if(scsi_status_is_good(result)) {
  1681. if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
  1682. (modepage == 6 || modepage == 8))) {
  1683. /* Initio breakage? */
  1684. header_length = 0;
  1685. data->length = 13;
  1686. data->medium_type = 0;
  1687. data->device_specific = 0;
  1688. data->longlba = 0;
  1689. data->block_descriptor_length = 0;
  1690. } else if(use_10_for_ms) {
  1691. data->length = buffer[0]*256 + buffer[1] + 2;
  1692. data->medium_type = buffer[2];
  1693. data->device_specific = buffer[3];
  1694. data->longlba = buffer[4] & 0x01;
  1695. data->block_descriptor_length = buffer[6]*256
  1696. + buffer[7];
  1697. } else {
  1698. data->length = buffer[0] + 1;
  1699. data->medium_type = buffer[1];
  1700. data->device_specific = buffer[2];
  1701. data->block_descriptor_length = buffer[3];
  1702. }
  1703. data->header_length = header_length;
  1704. }
  1705. return result;
  1706. }
  1707. EXPORT_SYMBOL(scsi_mode_sense);
  1708. /**
  1709. * scsi_test_unit_ready - test if unit is ready
  1710. * @sdev: scsi device to change the state of.
  1711. * @timeout: command timeout
  1712. * @retries: number of retries before failing
  1713. * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
  1714. * returning sense. Make sure that this is cleared before passing
  1715. * in.
  1716. *
  1717. * Returns zero if unsuccessful or an error if TUR failed. For
  1718. * removable media, a return of NOT_READY or UNIT_ATTENTION is
  1719. * translated to success, with the ->changed flag updated.
  1720. **/
  1721. int
  1722. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
  1723. struct scsi_sense_hdr *sshdr_external)
  1724. {
  1725. char cmd[] = {
  1726. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1727. };
  1728. struct scsi_sense_hdr *sshdr;
  1729. int result;
  1730. if (!sshdr_external)
  1731. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  1732. else
  1733. sshdr = sshdr_external;
  1734. /* try to eat the UNIT_ATTENTION if there are enough retries */
  1735. do {
  1736. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
  1737. timeout, retries);
  1738. } while ((driver_byte(result) & DRIVER_SENSE) &&
  1739. sshdr && sshdr->sense_key == UNIT_ATTENTION &&
  1740. --retries);
  1741. if (!sshdr)
  1742. /* could not allocate sense buffer, so can't process it */
  1743. return result;
  1744. if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
  1745. if ((scsi_sense_valid(sshdr)) &&
  1746. ((sshdr->sense_key == UNIT_ATTENTION) ||
  1747. (sshdr->sense_key == NOT_READY))) {
  1748. sdev->changed = 1;
  1749. result = 0;
  1750. }
  1751. }
  1752. if (!sshdr_external)
  1753. kfree(sshdr);
  1754. return result;
  1755. }
  1756. EXPORT_SYMBOL(scsi_test_unit_ready);
  1757. /**
  1758. * scsi_device_set_state - Take the given device through the device state model.
  1759. * @sdev: scsi device to change the state of.
  1760. * @state: state to change to.
  1761. *
  1762. * Returns zero if unsuccessful or an error if the requested
  1763. * transition is illegal.
  1764. */
  1765. int
  1766. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1767. {
  1768. enum scsi_device_state oldstate = sdev->sdev_state;
  1769. if (state == oldstate)
  1770. return 0;
  1771. switch (state) {
  1772. case SDEV_CREATED:
  1773. /* There are no legal states that come back to
  1774. * created. This is the manually initialised start
  1775. * state */
  1776. goto illegal;
  1777. case SDEV_RUNNING:
  1778. switch (oldstate) {
  1779. case SDEV_CREATED:
  1780. case SDEV_OFFLINE:
  1781. case SDEV_QUIESCE:
  1782. case SDEV_BLOCK:
  1783. break;
  1784. default:
  1785. goto illegal;
  1786. }
  1787. break;
  1788. case SDEV_QUIESCE:
  1789. switch (oldstate) {
  1790. case SDEV_RUNNING:
  1791. case SDEV_OFFLINE:
  1792. break;
  1793. default:
  1794. goto illegal;
  1795. }
  1796. break;
  1797. case SDEV_OFFLINE:
  1798. switch (oldstate) {
  1799. case SDEV_CREATED:
  1800. case SDEV_RUNNING:
  1801. case SDEV_QUIESCE:
  1802. case SDEV_BLOCK:
  1803. break;
  1804. default:
  1805. goto illegal;
  1806. }
  1807. break;
  1808. case SDEV_BLOCK:
  1809. switch (oldstate) {
  1810. case SDEV_CREATED:
  1811. case SDEV_RUNNING:
  1812. break;
  1813. default:
  1814. goto illegal;
  1815. }
  1816. break;
  1817. case SDEV_CANCEL:
  1818. switch (oldstate) {
  1819. case SDEV_CREATED:
  1820. case SDEV_RUNNING:
  1821. case SDEV_QUIESCE:
  1822. case SDEV_OFFLINE:
  1823. case SDEV_BLOCK:
  1824. break;
  1825. default:
  1826. goto illegal;
  1827. }
  1828. break;
  1829. case SDEV_DEL:
  1830. switch (oldstate) {
  1831. case SDEV_CREATED:
  1832. case SDEV_RUNNING:
  1833. case SDEV_OFFLINE:
  1834. case SDEV_CANCEL:
  1835. break;
  1836. default:
  1837. goto illegal;
  1838. }
  1839. break;
  1840. }
  1841. sdev->sdev_state = state;
  1842. return 0;
  1843. illegal:
  1844. SCSI_LOG_ERROR_RECOVERY(1,
  1845. sdev_printk(KERN_ERR, sdev,
  1846. "Illegal state transition %s->%s\n",
  1847. scsi_device_state_name(oldstate),
  1848. scsi_device_state_name(state))
  1849. );
  1850. return -EINVAL;
  1851. }
  1852. EXPORT_SYMBOL(scsi_device_set_state);
  1853. /**
  1854. * sdev_evt_emit - emit a single SCSI device uevent
  1855. * @sdev: associated SCSI device
  1856. * @evt: event to emit
  1857. *
  1858. * Send a single uevent (scsi_event) to the associated scsi_device.
  1859. */
  1860. static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
  1861. {
  1862. int idx = 0;
  1863. char *envp[3];
  1864. switch (evt->evt_type) {
  1865. case SDEV_EVT_MEDIA_CHANGE:
  1866. envp[idx++] = "SDEV_MEDIA_CHANGE=1";
  1867. break;
  1868. default:
  1869. /* do nothing */
  1870. break;
  1871. }
  1872. envp[idx++] = NULL;
  1873. kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
  1874. }
  1875. /**
  1876. * sdev_evt_thread - send a uevent for each scsi event
  1877. * @work: work struct for scsi_device
  1878. *
  1879. * Dispatch queued events to their associated scsi_device kobjects
  1880. * as uevents.
  1881. */
  1882. void scsi_evt_thread(struct work_struct *work)
  1883. {
  1884. struct scsi_device *sdev;
  1885. LIST_HEAD(event_list);
  1886. sdev = container_of(work, struct scsi_device, event_work);
  1887. while (1) {
  1888. struct scsi_event *evt;
  1889. struct list_head *this, *tmp;
  1890. unsigned long flags;
  1891. spin_lock_irqsave(&sdev->list_lock, flags);
  1892. list_splice_init(&sdev->event_list, &event_list);
  1893. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1894. if (list_empty(&event_list))
  1895. break;
  1896. list_for_each_safe(this, tmp, &event_list) {
  1897. evt = list_entry(this, struct scsi_event, node);
  1898. list_del(&evt->node);
  1899. scsi_evt_emit(sdev, evt);
  1900. kfree(evt);
  1901. }
  1902. }
  1903. }
  1904. /**
  1905. * sdev_evt_send - send asserted event to uevent thread
  1906. * @sdev: scsi_device event occurred on
  1907. * @evt: event to send
  1908. *
  1909. * Assert scsi device event asynchronously.
  1910. */
  1911. void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
  1912. {
  1913. unsigned long flags;
  1914. #if 0
  1915. /* FIXME: currently this check eliminates all media change events
  1916. * for polled devices. Need to update to discriminate between AN
  1917. * and polled events */
  1918. if (!test_bit(evt->evt_type, sdev->supported_events)) {
  1919. kfree(evt);
  1920. return;
  1921. }
  1922. #endif
  1923. spin_lock_irqsave(&sdev->list_lock, flags);
  1924. list_add_tail(&evt->node, &sdev->event_list);
  1925. schedule_work(&sdev->event_work);
  1926. spin_unlock_irqrestore(&sdev->list_lock, flags);
  1927. }
  1928. EXPORT_SYMBOL_GPL(sdev_evt_send);
  1929. /**
  1930. * sdev_evt_alloc - allocate a new scsi event
  1931. * @evt_type: type of event to allocate
  1932. * @gfpflags: GFP flags for allocation
  1933. *
  1934. * Allocates and returns a new scsi_event.
  1935. */
  1936. struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
  1937. gfp_t gfpflags)
  1938. {
  1939. struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
  1940. if (!evt)
  1941. return NULL;
  1942. evt->evt_type = evt_type;
  1943. INIT_LIST_HEAD(&evt->node);
  1944. /* evt_type-specific initialization, if any */
  1945. switch (evt_type) {
  1946. case SDEV_EVT_MEDIA_CHANGE:
  1947. default:
  1948. /* do nothing */
  1949. break;
  1950. }
  1951. return evt;
  1952. }
  1953. EXPORT_SYMBOL_GPL(sdev_evt_alloc);
  1954. /**
  1955. * sdev_evt_send_simple - send asserted event to uevent thread
  1956. * @sdev: scsi_device event occurred on
  1957. * @evt_type: type of event to send
  1958. * @gfpflags: GFP flags for allocation
  1959. *
  1960. * Assert scsi device event asynchronously, given an event type.
  1961. */
  1962. void sdev_evt_send_simple(struct scsi_device *sdev,
  1963. enum scsi_device_event evt_type, gfp_t gfpflags)
  1964. {
  1965. struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
  1966. if (!evt) {
  1967. sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
  1968. evt_type);
  1969. return;
  1970. }
  1971. sdev_evt_send(sdev, evt);
  1972. }
  1973. EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
  1974. /**
  1975. * scsi_device_quiesce - Block user issued commands.
  1976. * @sdev: scsi device to quiesce.
  1977. *
  1978. * This works by trying to transition to the SDEV_QUIESCE state
  1979. * (which must be a legal transition). When the device is in this
  1980. * state, only special requests will be accepted, all others will
  1981. * be deferred. Since special requests may also be requeued requests,
  1982. * a successful return doesn't guarantee the device will be
  1983. * totally quiescent.
  1984. *
  1985. * Must be called with user context, may sleep.
  1986. *
  1987. * Returns zero if unsuccessful or an error if not.
  1988. */
  1989. int
  1990. scsi_device_quiesce(struct scsi_device *sdev)
  1991. {
  1992. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1993. if (err)
  1994. return err;
  1995. scsi_run_queue(sdev->request_queue);
  1996. while (sdev->device_busy) {
  1997. msleep_interruptible(200);
  1998. scsi_run_queue(sdev->request_queue);
  1999. }
  2000. return 0;
  2001. }
  2002. EXPORT_SYMBOL(scsi_device_quiesce);
  2003. /**
  2004. * scsi_device_resume - Restart user issued commands to a quiesced device.
  2005. * @sdev: scsi device to resume.
  2006. *
  2007. * Moves the device from quiesced back to running and restarts the
  2008. * queues.
  2009. *
  2010. * Must be called with user context, may sleep.
  2011. */
  2012. void
  2013. scsi_device_resume(struct scsi_device *sdev)
  2014. {
  2015. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  2016. return;
  2017. scsi_run_queue(sdev->request_queue);
  2018. }
  2019. EXPORT_SYMBOL(scsi_device_resume);
  2020. static void
  2021. device_quiesce_fn(struct scsi_device *sdev, void *data)
  2022. {
  2023. scsi_device_quiesce(sdev);
  2024. }
  2025. void
  2026. scsi_target_quiesce(struct scsi_target *starget)
  2027. {
  2028. starget_for_each_device(starget, NULL, device_quiesce_fn);
  2029. }
  2030. EXPORT_SYMBOL(scsi_target_quiesce);
  2031. static void
  2032. device_resume_fn(struct scsi_device *sdev, void *data)
  2033. {
  2034. scsi_device_resume(sdev);
  2035. }
  2036. void
  2037. scsi_target_resume(struct scsi_target *starget)
  2038. {
  2039. starget_for_each_device(starget, NULL, device_resume_fn);
  2040. }
  2041. EXPORT_SYMBOL(scsi_target_resume);
  2042. /**
  2043. * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
  2044. * @sdev: device to block
  2045. *
  2046. * Block request made by scsi lld's to temporarily stop all
  2047. * scsi commands on the specified device. Called from interrupt
  2048. * or normal process context.
  2049. *
  2050. * Returns zero if successful or error if not
  2051. *
  2052. * Notes:
  2053. * This routine transitions the device to the SDEV_BLOCK state
  2054. * (which must be a legal transition). When the device is in this
  2055. * state, all commands are deferred until the scsi lld reenables
  2056. * the device with scsi_device_unblock or device_block_tmo fires.
  2057. * This routine assumes the host_lock is held on entry.
  2058. */
  2059. int
  2060. scsi_internal_device_block(struct scsi_device *sdev)
  2061. {
  2062. struct request_queue *q = sdev->request_queue;
  2063. unsigned long flags;
  2064. int err = 0;
  2065. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  2066. if (err)
  2067. return err;
  2068. /*
  2069. * The device has transitioned to SDEV_BLOCK. Stop the
  2070. * block layer from calling the midlayer with this device's
  2071. * request queue.
  2072. */
  2073. spin_lock_irqsave(q->queue_lock, flags);
  2074. blk_stop_queue(q);
  2075. spin_unlock_irqrestore(q->queue_lock, flags);
  2076. return 0;
  2077. }
  2078. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  2079. /**
  2080. * scsi_internal_device_unblock - resume a device after a block request
  2081. * @sdev: device to resume
  2082. *
  2083. * Called by scsi lld's or the midlayer to restart the device queue
  2084. * for the previously suspended scsi device. Called from interrupt or
  2085. * normal process context.
  2086. *
  2087. * Returns zero if successful or error if not.
  2088. *
  2089. * Notes:
  2090. * This routine transitions the device to the SDEV_RUNNING state
  2091. * (which must be a legal transition) allowing the midlayer to
  2092. * goose the queue for this device. This routine assumes the
  2093. * host_lock is held upon entry.
  2094. */
  2095. int
  2096. scsi_internal_device_unblock(struct scsi_device *sdev)
  2097. {
  2098. struct request_queue *q = sdev->request_queue;
  2099. int err;
  2100. unsigned long flags;
  2101. /*
  2102. * Try to transition the scsi device to SDEV_RUNNING
  2103. * and goose the device queue if successful.
  2104. */
  2105. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  2106. if (err)
  2107. return err;
  2108. spin_lock_irqsave(q->queue_lock, flags);
  2109. blk_start_queue(q);
  2110. spin_unlock_irqrestore(q->queue_lock, flags);
  2111. return 0;
  2112. }
  2113. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  2114. static void
  2115. device_block(struct scsi_device *sdev, void *data)
  2116. {
  2117. scsi_internal_device_block(sdev);
  2118. }
  2119. static int
  2120. target_block(struct device *dev, void *data)
  2121. {
  2122. if (scsi_is_target_device(dev))
  2123. starget_for_each_device(to_scsi_target(dev), NULL,
  2124. device_block);
  2125. return 0;
  2126. }
  2127. void
  2128. scsi_target_block(struct device *dev)
  2129. {
  2130. if (scsi_is_target_device(dev))
  2131. starget_for_each_device(to_scsi_target(dev), NULL,
  2132. device_block);
  2133. else
  2134. device_for_each_child(dev, NULL, target_block);
  2135. }
  2136. EXPORT_SYMBOL_GPL(scsi_target_block);
  2137. static void
  2138. device_unblock(struct scsi_device *sdev, void *data)
  2139. {
  2140. scsi_internal_device_unblock(sdev);
  2141. }
  2142. static int
  2143. target_unblock(struct device *dev, void *data)
  2144. {
  2145. if (scsi_is_target_device(dev))
  2146. starget_for_each_device(to_scsi_target(dev), NULL,
  2147. device_unblock);
  2148. return 0;
  2149. }
  2150. void
  2151. scsi_target_unblock(struct device *dev)
  2152. {
  2153. if (scsi_is_target_device(dev))
  2154. starget_for_each_device(to_scsi_target(dev), NULL,
  2155. device_unblock);
  2156. else
  2157. device_for_each_child(dev, NULL, target_unblock);
  2158. }
  2159. EXPORT_SYMBOL_GPL(scsi_target_unblock);
  2160. /**
  2161. * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
  2162. * @sgl: scatter-gather list
  2163. * @sg_count: number of segments in sg
  2164. * @offset: offset in bytes into sg, on return offset into the mapped area
  2165. * @len: bytes to map, on return number of bytes mapped
  2166. *
  2167. * Returns virtual address of the start of the mapped page
  2168. */
  2169. void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
  2170. size_t *offset, size_t *len)
  2171. {
  2172. int i;
  2173. size_t sg_len = 0, len_complete = 0;
  2174. struct scatterlist *sg;
  2175. struct page *page;
  2176. WARN_ON(!irqs_disabled());
  2177. for_each_sg(sgl, sg, sg_count, i) {
  2178. len_complete = sg_len; /* Complete sg-entries */
  2179. sg_len += sg->length;
  2180. if (sg_len > *offset)
  2181. break;
  2182. }
  2183. if (unlikely(i == sg_count)) {
  2184. printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
  2185. "elements %d\n",
  2186. __FUNCTION__, sg_len, *offset, sg_count);
  2187. WARN_ON(1);
  2188. return NULL;
  2189. }
  2190. /* Offset starting from the beginning of first page in this sg-entry */
  2191. *offset = *offset - len_complete + sg->offset;
  2192. /* Assumption: contiguous pages can be accessed as "page + i" */
  2193. page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
  2194. *offset &= ~PAGE_MASK;
  2195. /* Bytes in this sg-entry from *offset to the end of the page */
  2196. sg_len = PAGE_SIZE - *offset;
  2197. if (*len > sg_len)
  2198. *len = sg_len;
  2199. return kmap_atomic(page, KM_BIO_SRC_IRQ);
  2200. }
  2201. EXPORT_SYMBOL(scsi_kmap_atomic_sg);
  2202. /**
  2203. * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
  2204. * @virt: virtual address to be unmapped
  2205. */
  2206. void scsi_kunmap_atomic_sg(void *virt)
  2207. {
  2208. kunmap_atomic(virt, KM_BIO_SRC_IRQ);
  2209. }
  2210. EXPORT_SYMBOL(scsi_kunmap_atomic_sg);