scsi_lib.c 55 KB

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