scsi_lib.c 53 KB

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