scsi_lib.c 67 KB

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