scsi_lib.c 66 KB

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