scsi_lib.c 59 KB

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