scsi_lib.c 57 KB

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