scsi_lib.c 59 KB

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