scsi_lib.c 61 KB

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