scsi_lib.c 61 KB

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