scsi_lib.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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 <scsi/scsi.h>
  20. #include <scsi/scsi_cmnd.h>
  21. #include <scsi/scsi_dbg.h>
  22. #include <scsi/scsi_device.h>
  23. #include <scsi/scsi_driver.h>
  24. #include <scsi/scsi_eh.h>
  25. #include <scsi/scsi_host.h>
  26. #include "scsi_priv.h"
  27. #include "scsi_logging.h"
  28. #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
  29. #define SG_MEMPOOL_SIZE 32
  30. struct scsi_host_sg_pool {
  31. size_t size;
  32. char *name;
  33. kmem_cache_t *slab;
  34. mempool_t *pool;
  35. };
  36. #if (SCSI_MAX_PHYS_SEGMENTS < 32)
  37. #error SCSI_MAX_PHYS_SEGMENTS is too small
  38. #endif
  39. #define SP(x) { x, "sgpool-" #x }
  40. static struct scsi_host_sg_pool scsi_sg_pools[] = {
  41. SP(8),
  42. SP(16),
  43. SP(32),
  44. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  45. SP(64),
  46. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  47. SP(128),
  48. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  49. SP(256),
  50. #if (SCSI_MAX_PHYS_SEGMENTS > 256)
  51. #error SCSI_MAX_PHYS_SEGMENTS is too large
  52. #endif
  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->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 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->flags |= flags | REQ_BLOCK_PC | REQ_SPECIAL | REQ_QUIET;
  177. /*
  178. * head injection *required* here otherwise quiesce won't work
  179. */
  180. blk_execute_rq(req->q, NULL, req, 1);
  181. ret = req->errors;
  182. out:
  183. blk_put_request(req);
  184. return ret;
  185. }
  186. EXPORT_SYMBOL(scsi_execute);
  187. int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
  188. int data_direction, void *buffer, unsigned bufflen,
  189. struct scsi_sense_hdr *sshdr, int timeout, int retries)
  190. {
  191. char *sense = NULL;
  192. int result;
  193. if (sshdr) {
  194. sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  195. if (!sense)
  196. return DRIVER_ERROR << 24;
  197. }
  198. result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
  199. sense, timeout, retries, 0);
  200. if (sshdr)
  201. scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
  202. kfree(sense);
  203. return result;
  204. }
  205. EXPORT_SYMBOL(scsi_execute_req);
  206. struct scsi_io_context {
  207. void *data;
  208. void (*done)(void *data, char *sense, int result, int resid);
  209. char sense[SCSI_SENSE_BUFFERSIZE];
  210. };
  211. static kmem_cache_t *scsi_io_context_cache;
  212. static void scsi_end_async(struct request *req, int uptodate)
  213. {
  214. struct scsi_io_context *sioc = req->end_io_data;
  215. if (sioc->done)
  216. sioc->done(sioc->data, sioc->sense, req->errors, req->data_len);
  217. kmem_cache_free(scsi_io_context_cache, sioc);
  218. __blk_put_request(req->q, req);
  219. }
  220. static int scsi_merge_bio(struct request *rq, struct bio *bio)
  221. {
  222. struct request_queue *q = rq->q;
  223. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  224. if (rq_data_dir(rq) == WRITE)
  225. bio->bi_rw |= (1 << BIO_RW);
  226. blk_queue_bounce(q, &bio);
  227. if (!rq->bio)
  228. blk_rq_bio_prep(q, rq, bio);
  229. else if (!q->back_merge_fn(q, rq, bio))
  230. return -EINVAL;
  231. else {
  232. rq->biotail->bi_next = bio;
  233. rq->biotail = bio;
  234. rq->hard_nr_sectors += bio_sectors(bio);
  235. rq->nr_sectors = rq->hard_nr_sectors;
  236. }
  237. return 0;
  238. }
  239. static int scsi_bi_endio(struct bio *bio, unsigned int bytes_done, int error)
  240. {
  241. if (bio->bi_size)
  242. return 1;
  243. bio_put(bio);
  244. return 0;
  245. }
  246. /**
  247. * scsi_req_map_sg - map a scatterlist into a request
  248. * @rq: request to fill
  249. * @sg: scatterlist
  250. * @nsegs: number of elements
  251. * @bufflen: len of buffer
  252. * @gfp: memory allocation flags
  253. *
  254. * scsi_req_map_sg maps a scatterlist into a request so that the
  255. * request can be sent to the block layer. We do not trust the scatterlist
  256. * sent to use, as some ULDs use that struct to only organize the pages.
  257. */
  258. static int scsi_req_map_sg(struct request *rq, struct scatterlist *sgl,
  259. int nsegs, unsigned bufflen, gfp_t gfp)
  260. {
  261. struct request_queue *q = rq->q;
  262. int nr_pages = (bufflen + sgl[0].offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
  263. unsigned int data_len = 0, len, bytes, off;
  264. struct page *page;
  265. struct bio *bio = NULL;
  266. int i, err, nr_vecs = 0;
  267. for (i = 0; i < nsegs; i++) {
  268. page = sgl[i].page;
  269. off = sgl[i].offset;
  270. len = sgl[i].length;
  271. data_len += len;
  272. while (len > 0) {
  273. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  274. if (!bio) {
  275. nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
  276. nr_pages -= nr_vecs;
  277. bio = bio_alloc(gfp, nr_vecs);
  278. if (!bio) {
  279. err = -ENOMEM;
  280. goto free_bios;
  281. }
  282. bio->bi_end_io = scsi_bi_endio;
  283. }
  284. if (bio_add_pc_page(q, bio, page, bytes, off) !=
  285. bytes) {
  286. bio_put(bio);
  287. err = -EINVAL;
  288. goto free_bios;
  289. }
  290. if (bio->bi_vcnt >= nr_vecs) {
  291. err = scsi_merge_bio(rq, bio);
  292. if (err) {
  293. bio_endio(bio, bio->bi_size, 0);
  294. goto free_bios;
  295. }
  296. bio = NULL;
  297. }
  298. page++;
  299. len -= bytes;
  300. off = 0;
  301. }
  302. }
  303. rq->buffer = rq->data = NULL;
  304. rq->data_len = data_len;
  305. return 0;
  306. free_bios:
  307. while ((bio = rq->bio) != NULL) {
  308. rq->bio = bio->bi_next;
  309. /*
  310. * call endio instead of bio_put incase it was bounced
  311. */
  312. bio_endio(bio, bio->bi_size, 0);
  313. }
  314. return err;
  315. }
  316. /**
  317. * scsi_execute_async - insert request
  318. * @sdev: scsi device
  319. * @cmd: scsi command
  320. * @cmd_len: length of scsi cdb
  321. * @data_direction: data direction
  322. * @buffer: data buffer (this can be a kernel buffer or scatterlist)
  323. * @bufflen: len of buffer
  324. * @use_sg: if buffer is a scatterlist this is the number of elements
  325. * @timeout: request timeout in seconds
  326. * @retries: number of times to retry request
  327. * @flags: or into request flags
  328. **/
  329. int scsi_execute_async(struct scsi_device *sdev, const unsigned char *cmd,
  330. int cmd_len, int data_direction, void *buffer, unsigned bufflen,
  331. int use_sg, int timeout, int retries, void *privdata,
  332. void (*done)(void *, char *, int, int), gfp_t gfp)
  333. {
  334. struct request *req;
  335. struct scsi_io_context *sioc;
  336. int err = 0;
  337. int write = (data_direction == DMA_TO_DEVICE);
  338. sioc = kmem_cache_alloc(scsi_io_context_cache, gfp);
  339. if (!sioc)
  340. return DRIVER_ERROR << 24;
  341. memset(sioc, 0, sizeof(*sioc));
  342. req = blk_get_request(sdev->request_queue, write, gfp);
  343. if (!req)
  344. goto free_sense;
  345. req->flags |= REQ_BLOCK_PC | REQ_QUIET;
  346. if (use_sg)
  347. err = scsi_req_map_sg(req, buffer, use_sg, bufflen, gfp);
  348. else if (bufflen)
  349. err = blk_rq_map_kern(req->q, req, buffer, bufflen, gfp);
  350. if (err)
  351. goto free_req;
  352. req->cmd_len = cmd_len;
  353. memcpy(req->cmd, cmd, req->cmd_len);
  354. req->sense = sioc->sense;
  355. req->sense_len = 0;
  356. req->timeout = timeout;
  357. req->retries = retries;
  358. req->end_io_data = sioc;
  359. sioc->data = privdata;
  360. sioc->done = done;
  361. blk_execute_rq_nowait(req->q, NULL, req, 1, scsi_end_async);
  362. return 0;
  363. free_req:
  364. blk_put_request(req);
  365. free_sense:
  366. kfree(sioc);
  367. return DRIVER_ERROR << 24;
  368. }
  369. EXPORT_SYMBOL_GPL(scsi_execute_async);
  370. /*
  371. * Function: scsi_init_cmd_errh()
  372. *
  373. * Purpose: Initialize cmd fields related to error handling.
  374. *
  375. * Arguments: cmd - command that is ready to be queued.
  376. *
  377. * Notes: This function has the job of initializing a number of
  378. * fields related to error handling. Typically this will
  379. * be called once for each command, as required.
  380. */
  381. static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  382. {
  383. cmd->serial_number = 0;
  384. memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
  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 (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. blk_run_queue(sdev->request_queue);
  481. spin_lock_irqsave(shost->host_lock, flags);
  482. if (unlikely(!list_empty(&sdev->starved_entry)))
  483. /*
  484. * sdev lost a race, and was put back on the
  485. * starved list. This is unlikely but without this
  486. * in theory we could loop forever.
  487. */
  488. break;
  489. }
  490. spin_unlock_irqrestore(shost->host_lock, flags);
  491. blk_run_queue(q);
  492. }
  493. /*
  494. * Function: scsi_requeue_command()
  495. *
  496. * Purpose: Handle post-processing of completed commands.
  497. *
  498. * Arguments: q - queue to operate on
  499. * cmd - command that may need to be requeued.
  500. *
  501. * Returns: Nothing
  502. *
  503. * Notes: After command completion, there may be blocks left
  504. * over which weren't finished by the previous command
  505. * this can be for a number of reasons - the main one is
  506. * I/O errors in the middle of the request, in which case
  507. * we need to request the blocks that come after the bad
  508. * sector.
  509. * Notes: Upon return, cmd is a stale pointer.
  510. */
  511. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  512. {
  513. struct request *req = cmd->request;
  514. unsigned long flags;
  515. scsi_unprep_request(req);
  516. spin_lock_irqsave(q->queue_lock, flags);
  517. blk_requeue_request(q, req);
  518. spin_unlock_irqrestore(q->queue_lock, flags);
  519. scsi_run_queue(q);
  520. }
  521. void scsi_next_command(struct scsi_cmnd *cmd)
  522. {
  523. struct scsi_device *sdev = cmd->device;
  524. struct request_queue *q = sdev->request_queue;
  525. /* need to hold a reference on the device before we let go of the cmd */
  526. get_device(&sdev->sdev_gendev);
  527. scsi_put_command(cmd);
  528. scsi_run_queue(q);
  529. /* ok to remove device now */
  530. put_device(&sdev->sdev_gendev);
  531. }
  532. void scsi_run_host_queues(struct Scsi_Host *shost)
  533. {
  534. struct scsi_device *sdev;
  535. shost_for_each_device(sdev, shost)
  536. scsi_run_queue(sdev->request_queue);
  537. }
  538. /*
  539. * Function: scsi_end_request()
  540. *
  541. * Purpose: Post-processing of completed commands (usually invoked at end
  542. * of upper level post-processing and scsi_io_completion).
  543. *
  544. * Arguments: cmd - command that is complete.
  545. * uptodate - 1 if I/O indicates success, <= 0 for I/O error.
  546. * bytes - number of bytes of completed I/O
  547. * requeue - indicates whether we should requeue leftovers.
  548. *
  549. * Lock status: Assumed that lock is not held upon entry.
  550. *
  551. * Returns: cmd if requeue required, NULL otherwise.
  552. *
  553. * Notes: This is called for block device requests in order to
  554. * mark some number of sectors as complete.
  555. *
  556. * We are guaranteeing that the request queue will be goosed
  557. * at some point during this call.
  558. * Notes: If cmd was requeued, upon return it will be a stale pointer.
  559. */
  560. static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
  561. int bytes, int requeue)
  562. {
  563. request_queue_t *q = cmd->device->request_queue;
  564. struct request *req = cmd->request;
  565. unsigned long flags;
  566. /*
  567. * If there are blocks left over at the end, set up the command
  568. * to queue the remainder of them.
  569. */
  570. if (end_that_request_chunk(req, uptodate, bytes)) {
  571. int leftover = (req->hard_nr_sectors << 9);
  572. if (blk_pc_request(req))
  573. leftover = req->data_len;
  574. /* kill remainder if no retrys */
  575. if (!uptodate && blk_noretry_request(req))
  576. end_that_request_chunk(req, 0, leftover);
  577. else {
  578. if (requeue) {
  579. /*
  580. * Bleah. Leftovers again. Stick the
  581. * leftovers in the front of the
  582. * queue, and goose the queue again.
  583. */
  584. scsi_requeue_command(q, cmd);
  585. cmd = NULL;
  586. }
  587. return cmd;
  588. }
  589. }
  590. add_disk_randomness(req->rq_disk);
  591. spin_lock_irqsave(q->queue_lock, flags);
  592. if (blk_rq_tagged(req))
  593. blk_queue_end_tag(q, req);
  594. end_that_request_last(req, uptodate);
  595. spin_unlock_irqrestore(q->queue_lock, flags);
  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. static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  604. {
  605. struct scsi_host_sg_pool *sgp;
  606. struct scatterlist *sgl;
  607. BUG_ON(!cmd->use_sg);
  608. switch (cmd->use_sg) {
  609. case 1 ... 8:
  610. cmd->sglist_len = 0;
  611. break;
  612. case 9 ... 16:
  613. cmd->sglist_len = 1;
  614. break;
  615. case 17 ... 32:
  616. cmd->sglist_len = 2;
  617. break;
  618. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  619. case 33 ... 64:
  620. cmd->sglist_len = 3;
  621. break;
  622. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  623. case 65 ... 128:
  624. cmd->sglist_len = 4;
  625. break;
  626. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  627. case 129 ... 256:
  628. cmd->sglist_len = 5;
  629. break;
  630. #endif
  631. #endif
  632. #endif
  633. default:
  634. return NULL;
  635. }
  636. sgp = scsi_sg_pools + cmd->sglist_len;
  637. sgl = mempool_alloc(sgp->pool, gfp_mask);
  638. return sgl;
  639. }
  640. static void scsi_free_sgtable(struct scatterlist *sgl, int index)
  641. {
  642. struct scsi_host_sg_pool *sgp;
  643. BUG_ON(index >= SG_MEMPOOL_NR);
  644. sgp = scsi_sg_pools + index;
  645. mempool_free(sgl, sgp->pool);
  646. }
  647. /*
  648. * Function: scsi_release_buffers()
  649. *
  650. * Purpose: Completion processing for block device I/O requests.
  651. *
  652. * Arguments: cmd - command that we are bailing.
  653. *
  654. * Lock status: Assumed that no lock is held upon entry.
  655. *
  656. * Returns: Nothing
  657. *
  658. * Notes: In the event that an upper level driver rejects a
  659. * command, we must release resources allocated during
  660. * the __init_io() function. Primarily this would involve
  661. * the scatter-gather table, and potentially any bounce
  662. * buffers.
  663. */
  664. static void scsi_release_buffers(struct scsi_cmnd *cmd)
  665. {
  666. if (cmd->use_sg)
  667. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  668. /*
  669. * Zero these out. They now point to freed memory, and it is
  670. * dangerous to hang onto the pointers.
  671. */
  672. cmd->request_buffer = NULL;
  673. cmd->request_bufflen = 0;
  674. }
  675. /*
  676. * Function: scsi_io_completion()
  677. *
  678. * Purpose: Completion processing for block device I/O requests.
  679. *
  680. * Arguments: cmd - command that is finished.
  681. *
  682. * Lock status: Assumed that no lock is held upon entry.
  683. *
  684. * Returns: Nothing
  685. *
  686. * Notes: This function is matched in terms of capabilities to
  687. * the function that created the scatter-gather list.
  688. * In other words, if there are no bounce buffers
  689. * (the normal case for most drivers), we don't need
  690. * the logic to deal with cleaning up afterwards.
  691. *
  692. * We must do one of several things here:
  693. *
  694. * a) Call scsi_end_request. This will finish off the
  695. * specified number of sectors. If we are done, the
  696. * command block will be released, and the queue
  697. * function will be goosed. If we are not done, then
  698. * scsi_end_request will directly goose the queue.
  699. *
  700. * b) We can just use scsi_requeue_command() here. This would
  701. * be used if we just wanted to retry, for example.
  702. */
  703. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
  704. {
  705. int result = cmd->result;
  706. int this_count = cmd->request_bufflen;
  707. request_queue_t *q = cmd->device->request_queue;
  708. struct request *req = cmd->request;
  709. int clear_errors = 1;
  710. struct scsi_sense_hdr sshdr;
  711. int sense_valid = 0;
  712. int sense_deferred = 0;
  713. scsi_release_buffers(cmd);
  714. if (result) {
  715. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  716. if (sense_valid)
  717. sense_deferred = scsi_sense_is_deferred(&sshdr);
  718. }
  719. if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
  720. req->errors = result;
  721. if (result) {
  722. clear_errors = 0;
  723. if (sense_valid && req->sense) {
  724. /*
  725. * SG_IO wants current and deferred errors
  726. */
  727. int len = 8 + cmd->sense_buffer[7];
  728. if (len > SCSI_SENSE_BUFFERSIZE)
  729. len = SCSI_SENSE_BUFFERSIZE;
  730. memcpy(req->sense, cmd->sense_buffer, len);
  731. req->sense_len = len;
  732. }
  733. } else
  734. req->data_len = cmd->resid;
  735. }
  736. /*
  737. * Next deal with any sectors which we were able to correctly
  738. * handle.
  739. */
  740. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, "
  741. "%d bytes done.\n",
  742. req->nr_sectors, good_bytes));
  743. SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
  744. if (clear_errors)
  745. req->errors = 0;
  746. /* A number of bytes were successfully read. If there
  747. * are leftovers and there is some kind of error
  748. * (result != 0), retry the rest.
  749. */
  750. if (scsi_end_request(cmd, 1, good_bytes, result == 0) == NULL)
  751. return;
  752. /* good_bytes = 0, or (inclusive) there were leftovers and
  753. * result = 0, so scsi_end_request couldn't retry.
  754. */
  755. if (sense_valid && !sense_deferred) {
  756. switch (sshdr.sense_key) {
  757. case UNIT_ATTENTION:
  758. if (cmd->device->removable) {
  759. /* Detected disc change. Set a bit
  760. * and quietly refuse further access.
  761. */
  762. cmd->device->changed = 1;
  763. scsi_end_request(cmd, 0, this_count, 1);
  764. return;
  765. } else {
  766. /* Must have been a power glitch, or a
  767. * bus reset. Could not have been a
  768. * media change, so we just retry the
  769. * request and see what happens.
  770. */
  771. scsi_requeue_command(q, cmd);
  772. return;
  773. }
  774. break;
  775. case ILLEGAL_REQUEST:
  776. /* If we had an ILLEGAL REQUEST returned, then
  777. * we may have performed an unsupported
  778. * command. The only thing this should be
  779. * would be a ten byte read where only a six
  780. * byte read was supported. Also, on a system
  781. * where READ CAPACITY failed, we may have
  782. * read past the end of the disk.
  783. */
  784. if ((cmd->device->use_10_for_rw &&
  785. sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
  786. (cmd->cmnd[0] == READ_10 ||
  787. cmd->cmnd[0] == WRITE_10)) {
  788. cmd->device->use_10_for_rw = 0;
  789. /* This will cause a retry with a
  790. * 6-byte command.
  791. */
  792. scsi_requeue_command(q, cmd);
  793. return;
  794. } else {
  795. scsi_end_request(cmd, 0, this_count, 1);
  796. return;
  797. }
  798. break;
  799. case NOT_READY:
  800. /* If the device is in the process of becoming
  801. * ready, or has a temporary blockage, retry.
  802. */
  803. if (sshdr.asc == 0x04) {
  804. switch (sshdr.ascq) {
  805. case 0x01: /* becoming ready */
  806. case 0x04: /* format in progress */
  807. case 0x05: /* rebuild in progress */
  808. case 0x06: /* recalculation in progress */
  809. case 0x07: /* operation in progress */
  810. case 0x08: /* Long write in progress */
  811. case 0x09: /* self test in progress */
  812. scsi_requeue_command(q, cmd);
  813. return;
  814. default:
  815. break;
  816. }
  817. }
  818. if (!(req->flags & REQ_QUIET)) {
  819. scmd_printk(KERN_INFO, cmd,
  820. "Device not ready: ");
  821. scsi_print_sense_hdr("", &sshdr);
  822. }
  823. scsi_end_request(cmd, 0, this_count, 1);
  824. return;
  825. case VOLUME_OVERFLOW:
  826. if (!(req->flags & REQ_QUIET)) {
  827. scmd_printk(KERN_INFO, cmd,
  828. "Volume overflow, CDB: ");
  829. __scsi_print_command(cmd->cmnd);
  830. scsi_print_sense("", cmd);
  831. }
  832. /* See SSC3rXX or current. */
  833. scsi_end_request(cmd, 0, this_count, 1);
  834. return;
  835. default:
  836. break;
  837. }
  838. }
  839. if (host_byte(result) == DID_RESET) {
  840. /* Third party bus reset or reset for error recovery
  841. * reasons. Just retry the request and see what
  842. * happens.
  843. */
  844. scsi_requeue_command(q, cmd);
  845. return;
  846. }
  847. if (result) {
  848. if (!(req->flags & REQ_QUIET)) {
  849. scmd_printk(KERN_INFO, cmd,
  850. "SCSI error: return code = 0x%08x\n",
  851. result);
  852. if (driver_byte(result) & DRIVER_SENSE)
  853. scsi_print_sense("", cmd);
  854. }
  855. }
  856. scsi_end_request(cmd, 0, this_count, !result);
  857. }
  858. EXPORT_SYMBOL(scsi_io_completion);
  859. /*
  860. * Function: scsi_init_io()
  861. *
  862. * Purpose: SCSI I/O initialize function.
  863. *
  864. * Arguments: cmd - Command descriptor we wish to initialize
  865. *
  866. * Returns: 0 on success
  867. * BLKPREP_DEFER if the failure is retryable
  868. * BLKPREP_KILL if the failure is fatal
  869. */
  870. static int scsi_init_io(struct scsi_cmnd *cmd)
  871. {
  872. struct request *req = cmd->request;
  873. struct scatterlist *sgpnt;
  874. int count;
  875. /*
  876. * if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
  877. */
  878. if ((req->flags & REQ_BLOCK_PC) && !req->bio) {
  879. cmd->request_bufflen = req->data_len;
  880. cmd->request_buffer = req->data;
  881. req->buffer = req->data;
  882. cmd->use_sg = 0;
  883. return 0;
  884. }
  885. /*
  886. * we used to not use scatter-gather for single segment request,
  887. * but now we do (it makes highmem I/O easier to support without
  888. * kmapping pages)
  889. */
  890. cmd->use_sg = req->nr_phys_segments;
  891. /*
  892. * if sg table allocation fails, requeue request later.
  893. */
  894. sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
  895. if (unlikely(!sgpnt)) {
  896. scsi_unprep_request(req);
  897. return BLKPREP_DEFER;
  898. }
  899. cmd->request_buffer = (char *) sgpnt;
  900. cmd->request_bufflen = req->nr_sectors << 9;
  901. if (blk_pc_request(req))
  902. cmd->request_bufflen = req->data_len;
  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, cmd->request_buffer);
  909. /*
  910. * mapped well, send it off
  911. */
  912. if (likely(count <= cmd->use_sg)) {
  913. cmd->use_sg = count;
  914. return 0;
  915. }
  916. printk(KERN_ERR "Incorrect number of segments after building list\n");
  917. printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
  918. printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
  919. req->current_nr_sectors);
  920. /* release the command and kill it */
  921. scsi_release_buffers(cmd);
  922. scsi_put_command(cmd);
  923. return BLKPREP_KILL;
  924. }
  925. static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
  926. sector_t *error_sector)
  927. {
  928. struct scsi_device *sdev = q->queuedata;
  929. struct scsi_driver *drv;
  930. if (sdev->sdev_state != SDEV_RUNNING)
  931. return -ENXIO;
  932. drv = *(struct scsi_driver **) disk->private_data;
  933. if (drv->issue_flush)
  934. return drv->issue_flush(&sdev->sdev_gendev, error_sector);
  935. return -EOPNOTSUPP;
  936. }
  937. static void scsi_blk_pc_done(struct scsi_cmnd *cmd)
  938. {
  939. BUG_ON(!blk_pc_request(cmd->request));
  940. /*
  941. * This will complete the whole command with uptodate=1 so
  942. * as far as the block layer is concerned the command completed
  943. * successfully. Since this is a REQ_BLOCK_PC command the
  944. * caller should check the request's errors value
  945. */
  946. scsi_io_completion(cmd, cmd->request_bufflen);
  947. }
  948. static void scsi_setup_blk_pc_cmnd(struct scsi_cmnd *cmd)
  949. {
  950. struct request *req = cmd->request;
  951. BUG_ON(sizeof(req->cmd) > sizeof(cmd->cmnd));
  952. memcpy(cmd->cmnd, req->cmd, sizeof(cmd->cmnd));
  953. cmd->cmd_len = req->cmd_len;
  954. if (!req->data_len)
  955. cmd->sc_data_direction = DMA_NONE;
  956. else if (rq_data_dir(req) == WRITE)
  957. cmd->sc_data_direction = DMA_TO_DEVICE;
  958. else
  959. cmd->sc_data_direction = DMA_FROM_DEVICE;
  960. cmd->transfersize = req->data_len;
  961. cmd->allowed = req->retries;
  962. cmd->timeout_per_command = req->timeout;
  963. cmd->done = scsi_blk_pc_done;
  964. }
  965. static int scsi_prep_fn(struct request_queue *q, struct request *req)
  966. {
  967. struct scsi_device *sdev = q->queuedata;
  968. struct scsi_cmnd *cmd;
  969. int specials_only = 0;
  970. /*
  971. * Just check to see if the device is online. If it isn't, we
  972. * refuse to process any commands. The device must be brought
  973. * online before trying any recovery commands
  974. */
  975. if (unlikely(!scsi_device_online(sdev))) {
  976. sdev_printk(KERN_ERR, sdev,
  977. "rejecting I/O to offline device\n");
  978. goto kill;
  979. }
  980. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  981. /* OK, we're not in a running state don't prep
  982. * user commands */
  983. if (sdev->sdev_state == SDEV_DEL) {
  984. /* Device is fully deleted, no commands
  985. * at all allowed down */
  986. sdev_printk(KERN_ERR, sdev,
  987. "rejecting I/O to dead device\n");
  988. goto kill;
  989. }
  990. /* OK, we only allow special commands (i.e. not
  991. * user initiated ones */
  992. specials_only = sdev->sdev_state;
  993. }
  994. /*
  995. * Find the actual device driver associated with this command.
  996. * The SPECIAL requests are things like character device or
  997. * ioctls, which did not originate from ll_rw_blk. Note that
  998. * the special field is also used to indicate the cmd for
  999. * the remainder of a partially fulfilled request that can
  1000. * come up when there is a medium error. We have to treat
  1001. * these two cases differently. We differentiate by looking
  1002. * at request->cmd, as this tells us the real story.
  1003. */
  1004. if (req->flags & REQ_SPECIAL && req->special) {
  1005. cmd = req->special;
  1006. } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1007. if(unlikely(specials_only) && !(req->flags & REQ_SPECIAL)) {
  1008. if(specials_only == SDEV_QUIESCE ||
  1009. specials_only == SDEV_BLOCK)
  1010. goto defer;
  1011. sdev_printk(KERN_ERR, sdev,
  1012. "rejecting I/O to device being removed\n");
  1013. goto kill;
  1014. }
  1015. /*
  1016. * Now try and find a command block that we can use.
  1017. */
  1018. if (!req->special) {
  1019. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  1020. if (unlikely(!cmd))
  1021. goto defer;
  1022. } else
  1023. cmd = req->special;
  1024. /* pull a tag out of the request if we have one */
  1025. cmd->tag = req->tag;
  1026. } else {
  1027. blk_dump_rq_flags(req, "SCSI bad req");
  1028. goto kill;
  1029. }
  1030. /* note the overloading of req->special. When the tag
  1031. * is active it always means cmd. If the tag goes
  1032. * back for re-queueing, it may be reset */
  1033. req->special = cmd;
  1034. cmd->request = req;
  1035. /*
  1036. * FIXME: drop the lock here because the functions below
  1037. * expect to be called without the queue lock held. Also,
  1038. * previously, we dequeued the request before dropping the
  1039. * lock. We hope REQ_STARTED prevents anything untoward from
  1040. * happening now.
  1041. */
  1042. if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1043. int ret;
  1044. /*
  1045. * This will do a couple of things:
  1046. * 1) Fill in the actual SCSI command.
  1047. * 2) Fill in any other upper-level specific fields
  1048. * (timeout).
  1049. *
  1050. * If this returns 0, it means that the request failed
  1051. * (reading past end of disk, reading offline device,
  1052. * etc). This won't actually talk to the device, but
  1053. * some kinds of consistency checking may cause the
  1054. * request to be rejected immediately.
  1055. */
  1056. /*
  1057. * This sets up the scatter-gather table (allocating if
  1058. * required).
  1059. */
  1060. ret = scsi_init_io(cmd);
  1061. switch(ret) {
  1062. /* For BLKPREP_KILL/DEFER the cmd was released */
  1063. case BLKPREP_KILL:
  1064. goto kill;
  1065. case BLKPREP_DEFER:
  1066. goto defer;
  1067. }
  1068. /*
  1069. * Initialize the actual SCSI command for this request.
  1070. */
  1071. if (req->flags & REQ_BLOCK_PC) {
  1072. scsi_setup_blk_pc_cmnd(cmd);
  1073. } else if (req->rq_disk) {
  1074. struct scsi_driver *drv;
  1075. drv = *(struct scsi_driver **)req->rq_disk->private_data;
  1076. if (unlikely(!drv->init_command(cmd))) {
  1077. scsi_release_buffers(cmd);
  1078. scsi_put_command(cmd);
  1079. goto kill;
  1080. }
  1081. }
  1082. }
  1083. /*
  1084. * The request is now prepped, no need to come back here
  1085. */
  1086. req->flags |= REQ_DONTPREP;
  1087. return BLKPREP_OK;
  1088. defer:
  1089. /* If we defer, the elv_next_request() returns NULL, but the
  1090. * queue must be restarted, so we plug here if no returning
  1091. * command will automatically do that. */
  1092. if (sdev->device_busy == 0)
  1093. blk_plug_device(q);
  1094. return BLKPREP_DEFER;
  1095. kill:
  1096. req->errors = DID_NO_CONNECT << 16;
  1097. return BLKPREP_KILL;
  1098. }
  1099. /*
  1100. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1101. * return 0.
  1102. *
  1103. * Called with the queue_lock held.
  1104. */
  1105. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1106. struct scsi_device *sdev)
  1107. {
  1108. if (sdev->device_busy >= sdev->queue_depth)
  1109. return 0;
  1110. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1111. /*
  1112. * unblock after device_blocked iterates to zero
  1113. */
  1114. if (--sdev->device_blocked == 0) {
  1115. SCSI_LOG_MLQUEUE(3,
  1116. sdev_printk(KERN_INFO, sdev,
  1117. "unblocking device at zero depth\n"));
  1118. } else {
  1119. blk_plug_device(q);
  1120. return 0;
  1121. }
  1122. }
  1123. if (sdev->device_blocked)
  1124. return 0;
  1125. return 1;
  1126. }
  1127. /*
  1128. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1129. * return 0. We must end up running the queue again whenever 0 is
  1130. * returned, else IO can hang.
  1131. *
  1132. * Called with host_lock held.
  1133. */
  1134. static inline int scsi_host_queue_ready(struct request_queue *q,
  1135. struct Scsi_Host *shost,
  1136. struct scsi_device *sdev)
  1137. {
  1138. if (scsi_host_in_recovery(shost))
  1139. return 0;
  1140. if (shost->host_busy == 0 && shost->host_blocked) {
  1141. /*
  1142. * unblock after host_blocked iterates to zero
  1143. */
  1144. if (--shost->host_blocked == 0) {
  1145. SCSI_LOG_MLQUEUE(3,
  1146. printk("scsi%d unblocking host at zero depth\n",
  1147. shost->host_no));
  1148. } else {
  1149. blk_plug_device(q);
  1150. return 0;
  1151. }
  1152. }
  1153. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1154. shost->host_blocked || shost->host_self_blocked) {
  1155. if (list_empty(&sdev->starved_entry))
  1156. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1157. return 0;
  1158. }
  1159. /* We're OK to process the command, so we can't be starved */
  1160. if (!list_empty(&sdev->starved_entry))
  1161. list_del_init(&sdev->starved_entry);
  1162. return 1;
  1163. }
  1164. /*
  1165. * Kill a request for a dead device
  1166. */
  1167. static void scsi_kill_request(struct request *req, request_queue_t *q)
  1168. {
  1169. struct scsi_cmnd *cmd = req->special;
  1170. struct scsi_device *sdev = cmd->device;
  1171. struct Scsi_Host *shost = sdev->host;
  1172. blkdev_dequeue_request(req);
  1173. if (unlikely(cmd == NULL)) {
  1174. printk(KERN_CRIT "impossible request in %s.\n",
  1175. __FUNCTION__);
  1176. BUG();
  1177. }
  1178. scsi_init_cmd_errh(cmd);
  1179. cmd->result = DID_NO_CONNECT << 16;
  1180. atomic_inc(&cmd->device->iorequest_cnt);
  1181. /*
  1182. * SCSI request completion path will do scsi_device_unbusy(),
  1183. * bump busy counts. To bump the counters, we need to dance
  1184. * with the locks as normal issue path does.
  1185. */
  1186. sdev->device_busy++;
  1187. spin_unlock(sdev->request_queue->queue_lock);
  1188. spin_lock(shost->host_lock);
  1189. shost->host_busy++;
  1190. spin_unlock(shost->host_lock);
  1191. spin_lock(sdev->request_queue->queue_lock);
  1192. __scsi_done(cmd);
  1193. }
  1194. static void scsi_softirq_done(struct request *rq)
  1195. {
  1196. struct scsi_cmnd *cmd = rq->completion_data;
  1197. unsigned long wait_for = (cmd->allowed + 1) * cmd->timeout_per_command;
  1198. int disposition;
  1199. INIT_LIST_HEAD(&cmd->eh_entry);
  1200. disposition = scsi_decide_disposition(cmd);
  1201. if (disposition != SUCCESS &&
  1202. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1203. sdev_printk(KERN_ERR, cmd->device,
  1204. "timing out command, waited %lus\n",
  1205. wait_for/HZ);
  1206. disposition = SUCCESS;
  1207. }
  1208. scsi_log_completion(cmd, disposition);
  1209. switch (disposition) {
  1210. case SUCCESS:
  1211. scsi_finish_command(cmd);
  1212. break;
  1213. case NEEDS_RETRY:
  1214. scsi_retry_command(cmd);
  1215. break;
  1216. case ADD_TO_MLQUEUE:
  1217. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1218. break;
  1219. default:
  1220. if (!scsi_eh_scmd_add(cmd, 0))
  1221. scsi_finish_command(cmd);
  1222. }
  1223. }
  1224. /*
  1225. * Function: scsi_request_fn()
  1226. *
  1227. * Purpose: Main strategy routine for SCSI.
  1228. *
  1229. * Arguments: q - Pointer to actual queue.
  1230. *
  1231. * Returns: Nothing
  1232. *
  1233. * Lock status: IO request lock assumed to be held when called.
  1234. */
  1235. static void scsi_request_fn(struct request_queue *q)
  1236. {
  1237. struct scsi_device *sdev = q->queuedata;
  1238. struct Scsi_Host *shost;
  1239. struct scsi_cmnd *cmd;
  1240. struct request *req;
  1241. if (!sdev) {
  1242. printk("scsi: killing requests for dead queue\n");
  1243. while ((req = elv_next_request(q)) != NULL)
  1244. scsi_kill_request(req, q);
  1245. return;
  1246. }
  1247. if(!get_device(&sdev->sdev_gendev))
  1248. /* We must be tearing the block queue down already */
  1249. return;
  1250. /*
  1251. * To start with, we keep looping until the queue is empty, or until
  1252. * the host is no longer able to accept any more requests.
  1253. */
  1254. shost = sdev->host;
  1255. while (!blk_queue_plugged(q)) {
  1256. int rtn;
  1257. /*
  1258. * get next queueable request. We do this early to make sure
  1259. * that the request is fully prepared even if we cannot
  1260. * accept it.
  1261. */
  1262. req = elv_next_request(q);
  1263. if (!req || !scsi_dev_queue_ready(q, sdev))
  1264. break;
  1265. if (unlikely(!scsi_device_online(sdev))) {
  1266. sdev_printk(KERN_ERR, sdev,
  1267. "rejecting I/O to offline device\n");
  1268. scsi_kill_request(req, q);
  1269. continue;
  1270. }
  1271. /*
  1272. * Remove the request from the request list.
  1273. */
  1274. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1275. blkdev_dequeue_request(req);
  1276. sdev->device_busy++;
  1277. spin_unlock(q->queue_lock);
  1278. cmd = req->special;
  1279. if (unlikely(cmd == NULL)) {
  1280. printk(KERN_CRIT "impossible request in %s.\n"
  1281. "please mail a stack trace to "
  1282. "linux-scsi@vger.kernel.org",
  1283. __FUNCTION__);
  1284. BUG();
  1285. }
  1286. spin_lock(shost->host_lock);
  1287. if (!scsi_host_queue_ready(q, shost, sdev))
  1288. goto not_ready;
  1289. if (sdev->single_lun) {
  1290. if (scsi_target(sdev)->starget_sdev_user &&
  1291. scsi_target(sdev)->starget_sdev_user != sdev)
  1292. goto not_ready;
  1293. scsi_target(sdev)->starget_sdev_user = sdev;
  1294. }
  1295. shost->host_busy++;
  1296. /*
  1297. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1298. * take the lock again.
  1299. */
  1300. spin_unlock_irq(shost->host_lock);
  1301. /*
  1302. * Finally, initialize any error handling parameters, and set up
  1303. * the timers for timeouts.
  1304. */
  1305. scsi_init_cmd_errh(cmd);
  1306. /*
  1307. * Dispatch the command to the low-level driver.
  1308. */
  1309. rtn = scsi_dispatch_cmd(cmd);
  1310. spin_lock_irq(q->queue_lock);
  1311. if(rtn) {
  1312. /* we're refusing the command; because of
  1313. * the way locks get dropped, we need to
  1314. * check here if plugging is required */
  1315. if(sdev->device_busy == 0)
  1316. blk_plug_device(q);
  1317. break;
  1318. }
  1319. }
  1320. goto out;
  1321. not_ready:
  1322. spin_unlock_irq(shost->host_lock);
  1323. /*
  1324. * lock q, handle tag, requeue req, and decrement device_busy. We
  1325. * must return with queue_lock held.
  1326. *
  1327. * Decrementing device_busy without checking it is OK, as all such
  1328. * cases (host limits or settings) should run the queue at some
  1329. * later time.
  1330. */
  1331. spin_lock_irq(q->queue_lock);
  1332. blk_requeue_request(q, req);
  1333. sdev->device_busy--;
  1334. if(sdev->device_busy == 0)
  1335. blk_plug_device(q);
  1336. out:
  1337. /* must be careful here...if we trigger the ->remove() function
  1338. * we cannot be holding the q lock */
  1339. spin_unlock_irq(q->queue_lock);
  1340. put_device(&sdev->sdev_gendev);
  1341. spin_lock_irq(q->queue_lock);
  1342. }
  1343. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1344. {
  1345. struct device *host_dev;
  1346. u64 bounce_limit = 0xffffffff;
  1347. if (shost->unchecked_isa_dma)
  1348. return BLK_BOUNCE_ISA;
  1349. /*
  1350. * Platforms with virtual-DMA translation
  1351. * hardware have no practical limit.
  1352. */
  1353. if (!PCI_DMA_BUS_IS_PHYS)
  1354. return BLK_BOUNCE_ANY;
  1355. host_dev = scsi_get_device(shost);
  1356. if (host_dev && host_dev->dma_mask)
  1357. bounce_limit = *host_dev->dma_mask;
  1358. return bounce_limit;
  1359. }
  1360. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1361. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1362. {
  1363. struct Scsi_Host *shost = sdev->host;
  1364. struct request_queue *q;
  1365. q = blk_init_queue(scsi_request_fn, NULL);
  1366. if (!q)
  1367. return NULL;
  1368. blk_queue_prep_rq(q, scsi_prep_fn);
  1369. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1370. blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
  1371. blk_queue_max_sectors(q, shost->max_sectors);
  1372. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1373. blk_queue_segment_boundary(q, shost->dma_boundary);
  1374. blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
  1375. blk_queue_softirq_done(q, scsi_softirq_done);
  1376. if (!shost->use_clustering)
  1377. clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  1378. return q;
  1379. }
  1380. void scsi_free_queue(struct request_queue *q)
  1381. {
  1382. blk_cleanup_queue(q);
  1383. }
  1384. /*
  1385. * Function: scsi_block_requests()
  1386. *
  1387. * Purpose: Utility function used by low-level drivers to prevent further
  1388. * commands from being queued to the device.
  1389. *
  1390. * Arguments: shost - Host in question
  1391. *
  1392. * Returns: Nothing
  1393. *
  1394. * Lock status: No locks are assumed held.
  1395. *
  1396. * Notes: There is no timer nor any other means by which the requests
  1397. * get unblocked other than the low-level driver calling
  1398. * scsi_unblock_requests().
  1399. */
  1400. void scsi_block_requests(struct Scsi_Host *shost)
  1401. {
  1402. shost->host_self_blocked = 1;
  1403. }
  1404. EXPORT_SYMBOL(scsi_block_requests);
  1405. /*
  1406. * Function: scsi_unblock_requests()
  1407. *
  1408. * Purpose: Utility function used by low-level drivers to allow further
  1409. * commands from being queued to the device.
  1410. *
  1411. * Arguments: shost - Host in question
  1412. *
  1413. * Returns: Nothing
  1414. *
  1415. * Lock status: No locks are assumed held.
  1416. *
  1417. * Notes: There is no timer nor any other means by which the requests
  1418. * get unblocked other than the low-level driver calling
  1419. * scsi_unblock_requests().
  1420. *
  1421. * This is done as an API function so that changes to the
  1422. * internals of the scsi mid-layer won't require wholesale
  1423. * changes to drivers that use this feature.
  1424. */
  1425. void scsi_unblock_requests(struct Scsi_Host *shost)
  1426. {
  1427. shost->host_self_blocked = 0;
  1428. scsi_run_host_queues(shost);
  1429. }
  1430. EXPORT_SYMBOL(scsi_unblock_requests);
  1431. int __init scsi_init_queue(void)
  1432. {
  1433. int i;
  1434. scsi_io_context_cache = kmem_cache_create("scsi_io_context",
  1435. sizeof(struct scsi_io_context),
  1436. 0, 0, NULL, NULL);
  1437. if (!scsi_io_context_cache) {
  1438. printk(KERN_ERR "SCSI: can't init scsi io context cache\n");
  1439. return -ENOMEM;
  1440. }
  1441. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1442. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1443. int size = sgp->size * sizeof(struct scatterlist);
  1444. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1445. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1446. if (!sgp->slab) {
  1447. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1448. sgp->name);
  1449. }
  1450. sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
  1451. sgp->slab);
  1452. if (!sgp->pool) {
  1453. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1454. sgp->name);
  1455. }
  1456. }
  1457. return 0;
  1458. }
  1459. void scsi_exit_queue(void)
  1460. {
  1461. int i;
  1462. kmem_cache_destroy(scsi_io_context_cache);
  1463. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1464. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1465. mempool_destroy(sgp->pool);
  1466. kmem_cache_destroy(sgp->slab);
  1467. }
  1468. }
  1469. /**
  1470. * scsi_mode_select - issue a mode select
  1471. * @sdev: SCSI device to be queried
  1472. * @pf: Page format bit (1 == standard, 0 == vendor specific)
  1473. * @sp: Save page bit (0 == don't save, 1 == save)
  1474. * @modepage: mode page being requested
  1475. * @buffer: request buffer (may not be smaller than eight bytes)
  1476. * @len: length of request buffer.
  1477. * @timeout: command timeout
  1478. * @retries: number of retries before failing
  1479. * @data: returns a structure abstracting the mode header data
  1480. * @sense: place to put sense data (or NULL if no sense to be collected).
  1481. * must be SCSI_SENSE_BUFFERSIZE big.
  1482. *
  1483. * Returns zero if successful; negative error number or scsi
  1484. * status on error
  1485. *
  1486. */
  1487. int
  1488. scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
  1489. unsigned char *buffer, int len, int timeout, int retries,
  1490. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1491. {
  1492. unsigned char cmd[10];
  1493. unsigned char *real_buffer;
  1494. int ret;
  1495. memset(cmd, 0, sizeof(cmd));
  1496. cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
  1497. if (sdev->use_10_for_ms) {
  1498. if (len > 65535)
  1499. return -EINVAL;
  1500. real_buffer = kmalloc(8 + len, GFP_KERNEL);
  1501. if (!real_buffer)
  1502. return -ENOMEM;
  1503. memcpy(real_buffer + 8, buffer, len);
  1504. len += 8;
  1505. real_buffer[0] = 0;
  1506. real_buffer[1] = 0;
  1507. real_buffer[2] = data->medium_type;
  1508. real_buffer[3] = data->device_specific;
  1509. real_buffer[4] = data->longlba ? 0x01 : 0;
  1510. real_buffer[5] = 0;
  1511. real_buffer[6] = data->block_descriptor_length >> 8;
  1512. real_buffer[7] = data->block_descriptor_length;
  1513. cmd[0] = MODE_SELECT_10;
  1514. cmd[7] = len >> 8;
  1515. cmd[8] = len;
  1516. } else {
  1517. if (len > 255 || data->block_descriptor_length > 255 ||
  1518. data->longlba)
  1519. return -EINVAL;
  1520. real_buffer = kmalloc(4 + len, GFP_KERNEL);
  1521. if (!real_buffer)
  1522. return -ENOMEM;
  1523. memcpy(real_buffer + 4, buffer, len);
  1524. len += 4;
  1525. real_buffer[0] = 0;
  1526. real_buffer[1] = data->medium_type;
  1527. real_buffer[2] = data->device_specific;
  1528. real_buffer[3] = data->block_descriptor_length;
  1529. cmd[0] = MODE_SELECT;
  1530. cmd[4] = len;
  1531. }
  1532. ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
  1533. sshdr, timeout, retries);
  1534. kfree(real_buffer);
  1535. return ret;
  1536. }
  1537. EXPORT_SYMBOL_GPL(scsi_mode_select);
  1538. /**
  1539. * scsi_mode_sense - issue a mode sense, falling back from 10 to
  1540. * six bytes if necessary.
  1541. * @sdev: SCSI device to be queried
  1542. * @dbd: set if mode sense will allow block descriptors to be returned
  1543. * @modepage: mode page being requested
  1544. * @buffer: request buffer (may not be smaller than eight bytes)
  1545. * @len: length of request buffer.
  1546. * @timeout: command timeout
  1547. * @retries: number of retries before failing
  1548. * @data: returns a structure abstracting the mode header data
  1549. * @sense: place to put sense data (or NULL if no sense to be collected).
  1550. * must be SCSI_SENSE_BUFFERSIZE big.
  1551. *
  1552. * Returns zero if unsuccessful, or the header offset (either 4
  1553. * or 8 depending on whether a six or ten byte command was
  1554. * issued) if successful.
  1555. **/
  1556. int
  1557. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1558. unsigned char *buffer, int len, int timeout, int retries,
  1559. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1560. {
  1561. unsigned char cmd[12];
  1562. int use_10_for_ms;
  1563. int header_length;
  1564. int result;
  1565. struct scsi_sense_hdr my_sshdr;
  1566. memset(data, 0, sizeof(*data));
  1567. memset(&cmd[0], 0, 12);
  1568. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1569. cmd[2] = modepage;
  1570. /* caller might not be interested in sense, but we need it */
  1571. if (!sshdr)
  1572. sshdr = &my_sshdr;
  1573. retry:
  1574. use_10_for_ms = sdev->use_10_for_ms;
  1575. if (use_10_for_ms) {
  1576. if (len < 8)
  1577. len = 8;
  1578. cmd[0] = MODE_SENSE_10;
  1579. cmd[8] = len;
  1580. header_length = 8;
  1581. } else {
  1582. if (len < 4)
  1583. len = 4;
  1584. cmd[0] = MODE_SENSE;
  1585. cmd[4] = len;
  1586. header_length = 4;
  1587. }
  1588. memset(buffer, 0, len);
  1589. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  1590. sshdr, timeout, retries);
  1591. /* This code looks awful: what it's doing is making sure an
  1592. * ILLEGAL REQUEST sense return identifies the actual command
  1593. * byte as the problem. MODE_SENSE commands can return
  1594. * ILLEGAL REQUEST if the code page isn't supported */
  1595. if (use_10_for_ms && !scsi_status_is_good(result) &&
  1596. (driver_byte(result) & DRIVER_SENSE)) {
  1597. if (scsi_sense_valid(sshdr)) {
  1598. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  1599. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  1600. /*
  1601. * Invalid command operation code
  1602. */
  1603. sdev->use_10_for_ms = 0;
  1604. goto retry;
  1605. }
  1606. }
  1607. }
  1608. if(scsi_status_is_good(result)) {
  1609. if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
  1610. (modepage == 6 || modepage == 8))) {
  1611. /* Initio breakage? */
  1612. header_length = 0;
  1613. data->length = 13;
  1614. data->medium_type = 0;
  1615. data->device_specific = 0;
  1616. data->longlba = 0;
  1617. data->block_descriptor_length = 0;
  1618. } else if(use_10_for_ms) {
  1619. data->length = buffer[0]*256 + buffer[1] + 2;
  1620. data->medium_type = buffer[2];
  1621. data->device_specific = buffer[3];
  1622. data->longlba = buffer[4] & 0x01;
  1623. data->block_descriptor_length = buffer[6]*256
  1624. + buffer[7];
  1625. } else {
  1626. data->length = buffer[0] + 1;
  1627. data->medium_type = buffer[1];
  1628. data->device_specific = buffer[2];
  1629. data->block_descriptor_length = buffer[3];
  1630. }
  1631. data->header_length = header_length;
  1632. }
  1633. return result;
  1634. }
  1635. EXPORT_SYMBOL(scsi_mode_sense);
  1636. int
  1637. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
  1638. {
  1639. char cmd[] = {
  1640. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1641. };
  1642. struct scsi_sense_hdr sshdr;
  1643. int result;
  1644. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, &sshdr,
  1645. timeout, retries);
  1646. if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) {
  1647. if ((scsi_sense_valid(&sshdr)) &&
  1648. ((sshdr.sense_key == UNIT_ATTENTION) ||
  1649. (sshdr.sense_key == NOT_READY))) {
  1650. sdev->changed = 1;
  1651. result = 0;
  1652. }
  1653. }
  1654. return result;
  1655. }
  1656. EXPORT_SYMBOL(scsi_test_unit_ready);
  1657. /**
  1658. * scsi_device_set_state - Take the given device through the device
  1659. * state model.
  1660. * @sdev: scsi device to change the state of.
  1661. * @state: state to change to.
  1662. *
  1663. * Returns zero if unsuccessful or an error if the requested
  1664. * transition is illegal.
  1665. **/
  1666. int
  1667. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1668. {
  1669. enum scsi_device_state oldstate = sdev->sdev_state;
  1670. if (state == oldstate)
  1671. return 0;
  1672. switch (state) {
  1673. case SDEV_CREATED:
  1674. /* There are no legal states that come back to
  1675. * created. This is the manually initialised start
  1676. * state */
  1677. goto illegal;
  1678. case SDEV_RUNNING:
  1679. switch (oldstate) {
  1680. case SDEV_CREATED:
  1681. case SDEV_OFFLINE:
  1682. case SDEV_QUIESCE:
  1683. case SDEV_BLOCK:
  1684. break;
  1685. default:
  1686. goto illegal;
  1687. }
  1688. break;
  1689. case SDEV_QUIESCE:
  1690. switch (oldstate) {
  1691. case SDEV_RUNNING:
  1692. case SDEV_OFFLINE:
  1693. break;
  1694. default:
  1695. goto illegal;
  1696. }
  1697. break;
  1698. case SDEV_OFFLINE:
  1699. switch (oldstate) {
  1700. case SDEV_CREATED:
  1701. case SDEV_RUNNING:
  1702. case SDEV_QUIESCE:
  1703. case SDEV_BLOCK:
  1704. break;
  1705. default:
  1706. goto illegal;
  1707. }
  1708. break;
  1709. case SDEV_BLOCK:
  1710. switch (oldstate) {
  1711. case SDEV_CREATED:
  1712. case SDEV_RUNNING:
  1713. break;
  1714. default:
  1715. goto illegal;
  1716. }
  1717. break;
  1718. case SDEV_CANCEL:
  1719. switch (oldstate) {
  1720. case SDEV_CREATED:
  1721. case SDEV_RUNNING:
  1722. case SDEV_QUIESCE:
  1723. case SDEV_OFFLINE:
  1724. case SDEV_BLOCK:
  1725. break;
  1726. default:
  1727. goto illegal;
  1728. }
  1729. break;
  1730. case SDEV_DEL:
  1731. switch (oldstate) {
  1732. case SDEV_CREATED:
  1733. case SDEV_RUNNING:
  1734. case SDEV_OFFLINE:
  1735. case SDEV_CANCEL:
  1736. break;
  1737. default:
  1738. goto illegal;
  1739. }
  1740. break;
  1741. }
  1742. sdev->sdev_state = state;
  1743. return 0;
  1744. illegal:
  1745. SCSI_LOG_ERROR_RECOVERY(1,
  1746. sdev_printk(KERN_ERR, sdev,
  1747. "Illegal state transition %s->%s\n",
  1748. scsi_device_state_name(oldstate),
  1749. scsi_device_state_name(state))
  1750. );
  1751. return -EINVAL;
  1752. }
  1753. EXPORT_SYMBOL(scsi_device_set_state);
  1754. /**
  1755. * scsi_device_quiesce - Block user issued commands.
  1756. * @sdev: scsi device to quiesce.
  1757. *
  1758. * This works by trying to transition to the SDEV_QUIESCE state
  1759. * (which must be a legal transition). When the device is in this
  1760. * state, only special requests will be accepted, all others will
  1761. * be deferred. Since special requests may also be requeued requests,
  1762. * a successful return doesn't guarantee the device will be
  1763. * totally quiescent.
  1764. *
  1765. * Must be called with user context, may sleep.
  1766. *
  1767. * Returns zero if unsuccessful or an error if not.
  1768. **/
  1769. int
  1770. scsi_device_quiesce(struct scsi_device *sdev)
  1771. {
  1772. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1773. if (err)
  1774. return err;
  1775. scsi_run_queue(sdev->request_queue);
  1776. while (sdev->device_busy) {
  1777. msleep_interruptible(200);
  1778. scsi_run_queue(sdev->request_queue);
  1779. }
  1780. return 0;
  1781. }
  1782. EXPORT_SYMBOL(scsi_device_quiesce);
  1783. /**
  1784. * scsi_device_resume - Restart user issued commands to a quiesced device.
  1785. * @sdev: scsi device to resume.
  1786. *
  1787. * Moves the device from quiesced back to running and restarts the
  1788. * queues.
  1789. *
  1790. * Must be called with user context, may sleep.
  1791. **/
  1792. void
  1793. scsi_device_resume(struct scsi_device *sdev)
  1794. {
  1795. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  1796. return;
  1797. scsi_run_queue(sdev->request_queue);
  1798. }
  1799. EXPORT_SYMBOL(scsi_device_resume);
  1800. static void
  1801. device_quiesce_fn(struct scsi_device *sdev, void *data)
  1802. {
  1803. scsi_device_quiesce(sdev);
  1804. }
  1805. void
  1806. scsi_target_quiesce(struct scsi_target *starget)
  1807. {
  1808. starget_for_each_device(starget, NULL, device_quiesce_fn);
  1809. }
  1810. EXPORT_SYMBOL(scsi_target_quiesce);
  1811. static void
  1812. device_resume_fn(struct scsi_device *sdev, void *data)
  1813. {
  1814. scsi_device_resume(sdev);
  1815. }
  1816. void
  1817. scsi_target_resume(struct scsi_target *starget)
  1818. {
  1819. starget_for_each_device(starget, NULL, device_resume_fn);
  1820. }
  1821. EXPORT_SYMBOL(scsi_target_resume);
  1822. /**
  1823. * scsi_internal_device_block - internal function to put a device
  1824. * temporarily into the SDEV_BLOCK state
  1825. * @sdev: device to block
  1826. *
  1827. * Block request made by scsi lld's to temporarily stop all
  1828. * scsi commands on the specified device. Called from interrupt
  1829. * or normal process context.
  1830. *
  1831. * Returns zero if successful or error if not
  1832. *
  1833. * Notes:
  1834. * This routine transitions the device to the SDEV_BLOCK state
  1835. * (which must be a legal transition). When the device is in this
  1836. * state, all commands are deferred until the scsi lld reenables
  1837. * the device with scsi_device_unblock or device_block_tmo fires.
  1838. * This routine assumes the host_lock is held on entry.
  1839. **/
  1840. int
  1841. scsi_internal_device_block(struct scsi_device *sdev)
  1842. {
  1843. request_queue_t *q = sdev->request_queue;
  1844. unsigned long flags;
  1845. int err = 0;
  1846. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  1847. if (err)
  1848. return err;
  1849. /*
  1850. * The device has transitioned to SDEV_BLOCK. Stop the
  1851. * block layer from calling the midlayer with this device's
  1852. * request queue.
  1853. */
  1854. spin_lock_irqsave(q->queue_lock, flags);
  1855. blk_stop_queue(q);
  1856. spin_unlock_irqrestore(q->queue_lock, flags);
  1857. return 0;
  1858. }
  1859. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  1860. /**
  1861. * scsi_internal_device_unblock - resume a device after a block request
  1862. * @sdev: device to resume
  1863. *
  1864. * Called by scsi lld's or the midlayer to restart the device queue
  1865. * for the previously suspended scsi device. Called from interrupt or
  1866. * normal process context.
  1867. *
  1868. * Returns zero if successful or error if not.
  1869. *
  1870. * Notes:
  1871. * This routine transitions the device to the SDEV_RUNNING state
  1872. * (which must be a legal transition) allowing the midlayer to
  1873. * goose the queue for this device. This routine assumes the
  1874. * host_lock is held upon entry.
  1875. **/
  1876. int
  1877. scsi_internal_device_unblock(struct scsi_device *sdev)
  1878. {
  1879. request_queue_t *q = sdev->request_queue;
  1880. int err;
  1881. unsigned long flags;
  1882. /*
  1883. * Try to transition the scsi device to SDEV_RUNNING
  1884. * and goose the device queue if successful.
  1885. */
  1886. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  1887. if (err)
  1888. return err;
  1889. spin_lock_irqsave(q->queue_lock, flags);
  1890. blk_start_queue(q);
  1891. spin_unlock_irqrestore(q->queue_lock, flags);
  1892. return 0;
  1893. }
  1894. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  1895. static void
  1896. device_block(struct scsi_device *sdev, void *data)
  1897. {
  1898. scsi_internal_device_block(sdev);
  1899. }
  1900. static int
  1901. target_block(struct device *dev, void *data)
  1902. {
  1903. if (scsi_is_target_device(dev))
  1904. starget_for_each_device(to_scsi_target(dev), NULL,
  1905. device_block);
  1906. return 0;
  1907. }
  1908. void
  1909. scsi_target_block(struct device *dev)
  1910. {
  1911. if (scsi_is_target_device(dev))
  1912. starget_for_each_device(to_scsi_target(dev), NULL,
  1913. device_block);
  1914. else
  1915. device_for_each_child(dev, NULL, target_block);
  1916. }
  1917. EXPORT_SYMBOL_GPL(scsi_target_block);
  1918. static void
  1919. device_unblock(struct scsi_device *sdev, void *data)
  1920. {
  1921. scsi_internal_device_unblock(sdev);
  1922. }
  1923. static int
  1924. target_unblock(struct device *dev, void *data)
  1925. {
  1926. if (scsi_is_target_device(dev))
  1927. starget_for_each_device(to_scsi_target(dev), NULL,
  1928. device_unblock);
  1929. return 0;
  1930. }
  1931. void
  1932. scsi_target_unblock(struct device *dev)
  1933. {
  1934. if (scsi_is_target_device(dev))
  1935. starget_for_each_device(to_scsi_target(dev), NULL,
  1936. device_unblock);
  1937. else
  1938. device_for_each_child(dev, NULL, target_unblock);
  1939. }
  1940. EXPORT_SYMBOL_GPL(scsi_target_unblock);
  1941. /**
  1942. * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
  1943. * @sg: scatter-gather list
  1944. * @sg_count: number of segments in sg
  1945. * @offset: offset in bytes into sg, on return offset into the mapped area
  1946. * @len: bytes to map, on return number of bytes mapped
  1947. *
  1948. * Returns virtual address of the start of the mapped page
  1949. */
  1950. void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
  1951. size_t *offset, size_t *len)
  1952. {
  1953. int i;
  1954. size_t sg_len = 0, len_complete = 0;
  1955. struct page *page;
  1956. for (i = 0; i < sg_count; i++) {
  1957. len_complete = sg_len; /* Complete sg-entries */
  1958. sg_len += sg[i].length;
  1959. if (sg_len > *offset)
  1960. break;
  1961. }
  1962. if (unlikely(i == sg_count)) {
  1963. printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
  1964. "elements %d\n",
  1965. __FUNCTION__, sg_len, *offset, sg_count);
  1966. WARN_ON(1);
  1967. return NULL;
  1968. }
  1969. /* Offset starting from the beginning of first page in this sg-entry */
  1970. *offset = *offset - len_complete + sg[i].offset;
  1971. /* Assumption: contiguous pages can be accessed as "page + i" */
  1972. page = nth_page(sg[i].page, (*offset >> PAGE_SHIFT));
  1973. *offset &= ~PAGE_MASK;
  1974. /* Bytes in this sg-entry from *offset to the end of the page */
  1975. sg_len = PAGE_SIZE - *offset;
  1976. if (*len > sg_len)
  1977. *len = sg_len;
  1978. return kmap_atomic(page, KM_BIO_SRC_IRQ);
  1979. }
  1980. EXPORT_SYMBOL(scsi_kmap_atomic_sg);
  1981. /**
  1982. * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously
  1983. * mapped with scsi_kmap_atomic_sg
  1984. * @virt: virtual address to be unmapped
  1985. */
  1986. void scsi_kunmap_atomic_sg(void *virt)
  1987. {
  1988. kunmap_atomic(virt, KM_BIO_SRC_IRQ);
  1989. }
  1990. EXPORT_SYMBOL(scsi_kunmap_atomic_sg);