fc_fcp.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/err.h>
  28. #include <linux/crc32.h>
  29. #include <linux/slab.h>
  30. #include <scsi/scsi_tcq.h>
  31. #include <scsi/scsi.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_device.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <scsi/fc/fc_fc2.h>
  36. #include <scsi/libfc.h>
  37. #include <scsi/fc_encode.h>
  38. #include "fc_libfc.h"
  39. static struct kmem_cache *scsi_pkt_cachep;
  40. /* SRB state definitions */
  41. #define FC_SRB_FREE 0 /* cmd is free */
  42. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  43. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  44. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  45. #define FC_SRB_ABORTED (1 << 3) /* abort acknowledged */
  46. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  47. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  48. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  49. #define FC_SRB_READ (1 << 1)
  50. #define FC_SRB_WRITE (1 << 0)
  51. /*
  52. * The SCp.ptr should be tested and set under the scsi_pkt_queue lock
  53. */
  54. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  55. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  56. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  57. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  58. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  59. /**
  60. * struct fc_fcp_internal - FCP layer internal data
  61. * @scsi_pkt_pool: Memory pool to draw FCP packets from
  62. * @scsi_queue_lock: Protects the scsi_pkt_queue
  63. * @scsi_pkt_queue: Current FCP packets
  64. * @last_can_queue_ramp_down_time: ramp down time
  65. * @last_can_queue_ramp_up_time: ramp up time
  66. * @max_can_queue: max can_queue size
  67. */
  68. struct fc_fcp_internal {
  69. mempool_t *scsi_pkt_pool;
  70. spinlock_t scsi_queue_lock;
  71. struct list_head scsi_pkt_queue;
  72. unsigned long last_can_queue_ramp_down_time;
  73. unsigned long last_can_queue_ramp_up_time;
  74. int max_can_queue;
  75. };
  76. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  77. /*
  78. * function prototypes
  79. * FC scsi I/O related functions
  80. */
  81. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  82. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  83. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  84. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  85. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  86. static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
  87. static void fc_fcp_recovery(struct fc_fcp_pkt *, u8 code);
  88. static void fc_fcp_timeout(unsigned long);
  89. static void fc_fcp_rec(struct fc_fcp_pkt *);
  90. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  91. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  92. static void fc_io_compl(struct fc_fcp_pkt *);
  93. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  94. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  95. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  96. /*
  97. * command status codes
  98. */
  99. #define FC_COMPLETE 0
  100. #define FC_CMD_ABORTED 1
  101. #define FC_CMD_RESET 2
  102. #define FC_CMD_PLOGO 3
  103. #define FC_SNS_RCV 4
  104. #define FC_TRANS_ERR 5
  105. #define FC_DATA_OVRRUN 6
  106. #define FC_DATA_UNDRUN 7
  107. #define FC_ERROR 8
  108. #define FC_HRD_ERROR 9
  109. #define FC_CRC_ERROR 10
  110. #define FC_TIMED_OUT 11
  111. /*
  112. * Error recovery timeout values.
  113. */
  114. #define FC_SCSI_TM_TOV (10 * HZ)
  115. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  116. #define FC_CAN_QUEUE_PERIOD (60 * HZ)
  117. #define FC_MAX_ERROR_CNT 5
  118. #define FC_MAX_RECOV_RETRY 3
  119. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  120. /**
  121. * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
  122. * @lport: The local port that the FCP packet is for
  123. * @gfp: GFP flags for allocation
  124. *
  125. * Return value: fcp_pkt structure or null on allocation failure.
  126. * Context: Can be called from process context, no lock is required.
  127. */
  128. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
  129. {
  130. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  131. struct fc_fcp_pkt *fsp;
  132. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  133. if (fsp) {
  134. memset(fsp, 0, sizeof(*fsp));
  135. fsp->lp = lport;
  136. fsp->xfer_ddp = FC_XID_UNKNOWN;
  137. atomic_set(&fsp->ref_cnt, 1);
  138. init_timer(&fsp->timer);
  139. fsp->timer.data = (unsigned long)fsp;
  140. INIT_LIST_HEAD(&fsp->list);
  141. spin_lock_init(&fsp->scsi_pkt_lock);
  142. } else {
  143. per_cpu_ptr(lport->stats, get_cpu())->FcpPktAllocFails++;
  144. put_cpu();
  145. }
  146. return fsp;
  147. }
  148. /**
  149. * fc_fcp_pkt_release() - Release hold on a fcp_pkt
  150. * @fsp: The FCP packet to be released
  151. *
  152. * Context: Can be called from process or interrupt context,
  153. * no lock is required.
  154. */
  155. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  156. {
  157. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  158. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  159. mempool_free(fsp, si->scsi_pkt_pool);
  160. }
  161. }
  162. /**
  163. * fc_fcp_pkt_hold() - Hold a fcp_pkt
  164. * @fsp: The FCP packet to be held
  165. */
  166. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  167. {
  168. atomic_inc(&fsp->ref_cnt);
  169. }
  170. /**
  171. * fc_fcp_pkt_destroy() - Release hold on a fcp_pkt
  172. * @seq: The sequence that the FCP packet is on (required by destructor API)
  173. * @fsp: The FCP packet to be released
  174. *
  175. * This routine is called by a destructor callback in the exch_seq_send()
  176. * routine of the libfc Transport Template. The 'struct fc_seq' is a required
  177. * argument even though it is not used by this routine.
  178. *
  179. * Context: No locking required.
  180. */
  181. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  182. {
  183. fc_fcp_pkt_release(fsp);
  184. }
  185. /**
  186. * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
  187. * @fsp: The FCP packet to be locked and incremented
  188. *
  189. * We should only return error if we return a command to SCSI-ml before
  190. * getting a response. This can happen in cases where we send a abort, but
  191. * do not wait for the response and the abort and command can be passing
  192. * each other on the wire/network-layer.
  193. *
  194. * Note: this function locks the packet and gets a reference to allow
  195. * callers to call the completion function while the lock is held and
  196. * not have to worry about the packets refcount.
  197. *
  198. * TODO: Maybe we should just have callers grab/release the lock and
  199. * have a function that they call to verify the fsp and grab a ref if
  200. * needed.
  201. */
  202. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  203. {
  204. spin_lock_bh(&fsp->scsi_pkt_lock);
  205. if (fsp->state & FC_SRB_COMPL) {
  206. spin_unlock_bh(&fsp->scsi_pkt_lock);
  207. return -EPERM;
  208. }
  209. fc_fcp_pkt_hold(fsp);
  210. return 0;
  211. }
  212. /**
  213. * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
  214. * reference count
  215. * @fsp: The FCP packet to be unlocked and decremented
  216. */
  217. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  218. {
  219. spin_unlock_bh(&fsp->scsi_pkt_lock);
  220. fc_fcp_pkt_release(fsp);
  221. }
  222. /**
  223. * fc_fcp_timer_set() - Start a timer for a fcp_pkt
  224. * @fsp: The FCP packet to start a timer for
  225. * @delay: The timeout period in jiffies
  226. */
  227. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  228. {
  229. if (!(fsp->state & FC_SRB_COMPL))
  230. mod_timer(&fsp->timer, jiffies + delay);
  231. }
  232. /**
  233. * fc_fcp_send_abort() - Send an abort for exchanges associated with a
  234. * fcp_pkt
  235. * @fsp: The FCP packet to abort exchanges on
  236. */
  237. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  238. {
  239. if (!fsp->seq_ptr)
  240. return -EINVAL;
  241. per_cpu_ptr(fsp->lp->stats, get_cpu())->FcpPktAborts++;
  242. put_cpu();
  243. fsp->state |= FC_SRB_ABORT_PENDING;
  244. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  245. }
  246. /**
  247. * fc_fcp_retry_cmd() - Retry a fcp_pkt
  248. * @fsp: The FCP packet to be retried
  249. *
  250. * Sets the status code to be FC_ERROR and then calls
  251. * fc_fcp_complete_locked() which in turn calls fc_io_compl().
  252. * fc_io_compl() will notify the SCSI-ml that the I/O is done.
  253. * The SCSI-ml will retry the command.
  254. */
  255. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  256. {
  257. if (fsp->seq_ptr) {
  258. fsp->lp->tt.exch_done(fsp->seq_ptr);
  259. fsp->seq_ptr = NULL;
  260. }
  261. fsp->state &= ~FC_SRB_ABORT_PENDING;
  262. fsp->io_status = 0;
  263. fsp->status_code = FC_ERROR;
  264. fc_fcp_complete_locked(fsp);
  265. }
  266. /**
  267. * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
  268. * @fsp: The FCP packet that will manage the DDP frames
  269. * @xid: The XID that will be used for the DDP exchange
  270. */
  271. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  272. {
  273. struct fc_lport *lport;
  274. lport = fsp->lp;
  275. if ((fsp->req_flags & FC_SRB_READ) &&
  276. (lport->lro_enabled) && (lport->tt.ddp_setup)) {
  277. if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
  278. scsi_sg_count(fsp->cmd)))
  279. fsp->xfer_ddp = xid;
  280. }
  281. }
  282. /**
  283. * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
  284. * DDP related resources for a fcp_pkt
  285. * @fsp: The FCP packet that DDP had been used on
  286. */
  287. void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  288. {
  289. struct fc_lport *lport;
  290. if (!fsp)
  291. return;
  292. if (fsp->xfer_ddp == FC_XID_UNKNOWN)
  293. return;
  294. lport = fsp->lp;
  295. if (lport->tt.ddp_done) {
  296. fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
  297. fsp->xfer_ddp = FC_XID_UNKNOWN;
  298. }
  299. }
  300. /**
  301. * fc_fcp_can_queue_ramp_up() - increases can_queue
  302. * @lport: lport to ramp up can_queue
  303. */
  304. static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport)
  305. {
  306. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  307. unsigned long flags;
  308. int can_queue;
  309. spin_lock_irqsave(lport->host->host_lock, flags);
  310. if (si->last_can_queue_ramp_up_time &&
  311. (time_before(jiffies, si->last_can_queue_ramp_up_time +
  312. FC_CAN_QUEUE_PERIOD)))
  313. goto unlock;
  314. if (time_before(jiffies, si->last_can_queue_ramp_down_time +
  315. FC_CAN_QUEUE_PERIOD))
  316. goto unlock;
  317. si->last_can_queue_ramp_up_time = jiffies;
  318. can_queue = lport->host->can_queue << 1;
  319. if (can_queue >= si->max_can_queue) {
  320. can_queue = si->max_can_queue;
  321. si->last_can_queue_ramp_down_time = 0;
  322. }
  323. lport->host->can_queue = can_queue;
  324. shost_printk(KERN_ERR, lport->host, "libfc: increased "
  325. "can_queue to %d.\n", can_queue);
  326. unlock:
  327. spin_unlock_irqrestore(lport->host->host_lock, flags);
  328. }
  329. /**
  330. * fc_fcp_can_queue_ramp_down() - reduces can_queue
  331. * @lport: lport to reduce can_queue
  332. *
  333. * If we are getting memory allocation failures, then we may
  334. * be trying to execute too many commands. We let the running
  335. * commands complete or timeout, then try again with a reduced
  336. * can_queue. Eventually we will hit the point where we run
  337. * on all reserved structs.
  338. */
  339. static void fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
  340. {
  341. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  342. unsigned long flags;
  343. int can_queue;
  344. spin_lock_irqsave(lport->host->host_lock, flags);
  345. if (si->last_can_queue_ramp_down_time &&
  346. (time_before(jiffies, si->last_can_queue_ramp_down_time +
  347. FC_CAN_QUEUE_PERIOD)))
  348. goto unlock;
  349. si->last_can_queue_ramp_down_time = jiffies;
  350. can_queue = lport->host->can_queue;
  351. can_queue >>= 1;
  352. if (!can_queue)
  353. can_queue = 1;
  354. lport->host->can_queue = can_queue;
  355. shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
  356. "Reducing can_queue to %d.\n", can_queue);
  357. unlock:
  358. spin_unlock_irqrestore(lport->host->host_lock, flags);
  359. }
  360. /*
  361. * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer.
  362. * @lport: fc lport struct
  363. * @len: payload length
  364. *
  365. * Allocates fc_frame structure and buffer but if fails to allocate
  366. * then reduce can_queue.
  367. */
  368. static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
  369. size_t len)
  370. {
  371. struct fc_frame *fp;
  372. fp = fc_frame_alloc(lport, len);
  373. if (likely(fp))
  374. return fp;
  375. per_cpu_ptr(lport->stats, get_cpu())->FcpFrameAllocFails++;
  376. put_cpu();
  377. /* error case */
  378. fc_fcp_can_queue_ramp_down(lport);
  379. return NULL;
  380. }
  381. /**
  382. * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
  383. * @fsp: The FCP packet the data is on
  384. * @fp: The data frame
  385. */
  386. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  387. {
  388. struct scsi_cmnd *sc = fsp->cmd;
  389. struct fc_lport *lport = fsp->lp;
  390. struct fc_stats *stats;
  391. struct fc_frame_header *fh;
  392. size_t start_offset;
  393. size_t offset;
  394. u32 crc;
  395. u32 copy_len = 0;
  396. size_t len;
  397. void *buf;
  398. struct scatterlist *sg;
  399. u32 nents;
  400. u8 host_bcode = FC_COMPLETE;
  401. fh = fc_frame_header_get(fp);
  402. offset = ntohl(fh->fh_parm_offset);
  403. start_offset = offset;
  404. len = fr_len(fp) - sizeof(*fh);
  405. buf = fc_frame_payload_get(fp, 0);
  406. /*
  407. * if this I/O is ddped then clear it and initiate recovery since data
  408. * frames are expected to be placed directly in that case.
  409. *
  410. * Indicate error to scsi-ml because something went wrong with the
  411. * ddp handling to get us here.
  412. */
  413. if (fsp->xfer_ddp != FC_XID_UNKNOWN) {
  414. fc_fcp_ddp_done(fsp);
  415. FC_FCP_DBG(fsp, "DDP I/O in fc_fcp_recv_data set ERROR\n");
  416. host_bcode = FC_ERROR;
  417. goto err;
  418. }
  419. if (offset + len > fsp->data_len) {
  420. /* this should never happen */
  421. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  422. fc_frame_crc_check(fp))
  423. goto crc_err;
  424. FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
  425. "data_len %x\n", len, offset, fsp->data_len);
  426. /* Data is corrupted indicate scsi-ml should retry */
  427. host_bcode = FC_DATA_OVRRUN;
  428. goto err;
  429. }
  430. if (offset != fsp->xfer_len)
  431. fsp->state |= FC_SRB_DISCONTIG;
  432. sg = scsi_sglist(sc);
  433. nents = scsi_sg_count(sc);
  434. if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
  435. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  436. &offset, NULL);
  437. } else {
  438. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  439. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  440. &offset, &crc);
  441. buf = fc_frame_payload_get(fp, 0);
  442. if (len % 4)
  443. crc = crc32(crc, buf + len, 4 - (len % 4));
  444. if (~crc != le32_to_cpu(fr_crc(fp))) {
  445. crc_err:
  446. stats = per_cpu_ptr(lport->stats, get_cpu());
  447. stats->ErrorFrames++;
  448. /* per cpu count, not total count, but OK for limit */
  449. if (stats->InvalidCRCCount++ < FC_MAX_ERROR_CNT)
  450. printk(KERN_WARNING "libfc: CRC error on data "
  451. "frame for port (%6.6x)\n",
  452. lport->port_id);
  453. put_cpu();
  454. /*
  455. * Assume the frame is total garbage.
  456. * We may have copied it over the good part
  457. * of the buffer.
  458. * If so, we need to retry the entire operation.
  459. * Otherwise, ignore it.
  460. */
  461. if (fsp->state & FC_SRB_DISCONTIG) {
  462. host_bcode = FC_CRC_ERROR;
  463. goto err;
  464. }
  465. return;
  466. }
  467. }
  468. if (fsp->xfer_contig_end == start_offset)
  469. fsp->xfer_contig_end += copy_len;
  470. fsp->xfer_len += copy_len;
  471. /*
  472. * In the very rare event that this data arrived after the response
  473. * and completes the transfer, call the completion handler.
  474. */
  475. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  476. fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
  477. fc_fcp_complete_locked(fsp);
  478. return;
  479. err:
  480. fc_fcp_recovery(fsp, host_bcode);
  481. }
  482. /**
  483. * fc_fcp_send_data() - Send SCSI data to a target
  484. * @fsp: The FCP packet the data is on
  485. * @sp: The sequence the data is to be sent on
  486. * @offset: The starting offset for this data request
  487. * @seq_blen: The burst length for this data request
  488. *
  489. * Called after receiving a Transfer Ready data descriptor.
  490. * If the LLD is capable of sequence offload then send down the
  491. * seq_blen amount of data in single frame, otherwise send
  492. * multiple frames of the maximum frame payload supported by
  493. * the target port.
  494. */
  495. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  496. size_t offset, size_t seq_blen)
  497. {
  498. struct fc_exch *ep;
  499. struct scsi_cmnd *sc;
  500. struct scatterlist *sg;
  501. struct fc_frame *fp = NULL;
  502. struct fc_lport *lport = fsp->lp;
  503. struct page *page;
  504. size_t remaining;
  505. size_t t_blen;
  506. size_t tlen;
  507. size_t sg_bytes;
  508. size_t frame_offset, fh_parm_offset;
  509. size_t off;
  510. int error;
  511. void *data = NULL;
  512. void *page_addr;
  513. int using_sg = lport->sg_supp;
  514. u32 f_ctl;
  515. WARN_ON(seq_blen <= 0);
  516. if (unlikely(offset + seq_blen > fsp->data_len)) {
  517. /* this should never happen */
  518. FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
  519. "offset %zx\n", seq_blen, offset);
  520. fc_fcp_send_abort(fsp);
  521. return 0;
  522. } else if (offset != fsp->xfer_len) {
  523. /* Out of Order Data Request - no problem, but unexpected. */
  524. FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
  525. "seq_blen %zx offset %zx\n", seq_blen, offset);
  526. }
  527. /*
  528. * if LLD is capable of seq_offload then set transport
  529. * burst length (t_blen) to seq_blen, otherwise set t_blen
  530. * to max FC frame payload previously set in fsp->max_payload.
  531. */
  532. t_blen = fsp->max_payload;
  533. if (lport->seq_offload) {
  534. t_blen = min(seq_blen, (size_t)lport->lso_max);
  535. FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  536. fsp, seq_blen, lport->lso_max, t_blen);
  537. }
  538. if (t_blen > 512)
  539. t_blen &= ~(512 - 1); /* round down to block size */
  540. sc = fsp->cmd;
  541. remaining = seq_blen;
  542. fh_parm_offset = frame_offset = offset;
  543. tlen = 0;
  544. seq = lport->tt.seq_start_next(seq);
  545. f_ctl = FC_FC_REL_OFF;
  546. WARN_ON(!seq);
  547. sg = scsi_sglist(sc);
  548. while (remaining > 0 && sg) {
  549. if (offset >= sg->length) {
  550. offset -= sg->length;
  551. sg = sg_next(sg);
  552. continue;
  553. }
  554. if (!fp) {
  555. tlen = min(t_blen, remaining);
  556. /*
  557. * TODO. Temporary workaround. fc_seq_send() can't
  558. * handle odd lengths in non-linear skbs.
  559. * This will be the final fragment only.
  560. */
  561. if (tlen % 4)
  562. using_sg = 0;
  563. fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
  564. if (!fp)
  565. return -ENOMEM;
  566. data = fc_frame_header_get(fp) + 1;
  567. fh_parm_offset = frame_offset;
  568. fr_max_payload(fp) = fsp->max_payload;
  569. }
  570. off = offset + sg->offset;
  571. sg_bytes = min(tlen, sg->length - offset);
  572. sg_bytes = min(sg_bytes,
  573. (size_t) (PAGE_SIZE - (off & ~PAGE_MASK)));
  574. page = sg_page(sg) + (off >> PAGE_SHIFT);
  575. if (using_sg) {
  576. get_page(page);
  577. skb_fill_page_desc(fp_skb(fp),
  578. skb_shinfo(fp_skb(fp))->nr_frags,
  579. page, off & ~PAGE_MASK, sg_bytes);
  580. fp_skb(fp)->data_len += sg_bytes;
  581. fr_len(fp) += sg_bytes;
  582. fp_skb(fp)->truesize += PAGE_SIZE;
  583. } else {
  584. /*
  585. * The scatterlist item may be bigger than PAGE_SIZE,
  586. * but we must not cross pages inside the kmap.
  587. */
  588. page_addr = kmap_atomic(page);
  589. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  590. sg_bytes);
  591. kunmap_atomic(page_addr);
  592. data += sg_bytes;
  593. }
  594. offset += sg_bytes;
  595. frame_offset += sg_bytes;
  596. tlen -= sg_bytes;
  597. remaining -= sg_bytes;
  598. if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
  599. (tlen))
  600. continue;
  601. /*
  602. * Send sequence with transfer sequence initiative in case
  603. * this is last FCP frame of the sequence.
  604. */
  605. if (remaining == 0)
  606. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  607. ep = fc_seq_exch(seq);
  608. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  609. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  610. /*
  611. * send fragment using for a sequence.
  612. */
  613. error = lport->tt.seq_send(lport, seq, fp);
  614. if (error) {
  615. WARN_ON(1); /* send error should be rare */
  616. return error;
  617. }
  618. fp = NULL;
  619. }
  620. fsp->xfer_len += seq_blen; /* premature count? */
  621. return 0;
  622. }
  623. /**
  624. * fc_fcp_abts_resp() - Receive an ABTS response
  625. * @fsp: The FCP packet that is being aborted
  626. * @fp: The response frame
  627. */
  628. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  629. {
  630. int ba_done = 1;
  631. struct fc_ba_rjt *brp;
  632. struct fc_frame_header *fh;
  633. fh = fc_frame_header_get(fp);
  634. switch (fh->fh_r_ctl) {
  635. case FC_RCTL_BA_ACC:
  636. break;
  637. case FC_RCTL_BA_RJT:
  638. brp = fc_frame_payload_get(fp, sizeof(*brp));
  639. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  640. break;
  641. /* fall thru */
  642. default:
  643. /*
  644. * we will let the command timeout
  645. * and scsi-ml recover in this case,
  646. * therefore cleared the ba_done flag.
  647. */
  648. ba_done = 0;
  649. }
  650. if (ba_done) {
  651. fsp->state |= FC_SRB_ABORTED;
  652. fsp->state &= ~FC_SRB_ABORT_PENDING;
  653. if (fsp->wait_for_comp)
  654. complete(&fsp->tm_done);
  655. else
  656. fc_fcp_complete_locked(fsp);
  657. }
  658. }
  659. /**
  660. * fc_fcp_recv() - Receive an FCP frame
  661. * @seq: The sequence the frame is on
  662. * @fp: The received frame
  663. * @arg: The related FCP packet
  664. *
  665. * Context: Called from Soft IRQ context. Can not be called
  666. * holding the FCP packet list lock.
  667. */
  668. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  669. {
  670. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  671. struct fc_lport *lport = fsp->lp;
  672. struct fc_frame_header *fh;
  673. struct fcp_txrdy *dd;
  674. u8 r_ctl;
  675. int rc = 0;
  676. if (IS_ERR(fp)) {
  677. fc_fcp_error(fsp, fp);
  678. return;
  679. }
  680. fh = fc_frame_header_get(fp);
  681. r_ctl = fh->fh_r_ctl;
  682. if (lport->state != LPORT_ST_READY)
  683. goto out;
  684. if (fc_fcp_lock_pkt(fsp))
  685. goto out;
  686. if (fh->fh_type == FC_TYPE_BLS) {
  687. fc_fcp_abts_resp(fsp, fp);
  688. goto unlock;
  689. }
  690. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  691. goto unlock;
  692. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  693. /*
  694. * received XFER RDY from the target
  695. * need to send data to the target
  696. */
  697. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  698. dd = fc_frame_payload_get(fp, sizeof(*dd));
  699. WARN_ON(!dd);
  700. rc = fc_fcp_send_data(fsp, seq,
  701. (size_t) ntohl(dd->ft_data_ro),
  702. (size_t) ntohl(dd->ft_burst_len));
  703. if (!rc)
  704. seq->rec_data = fsp->xfer_len;
  705. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  706. /*
  707. * received a DATA frame
  708. * next we will copy the data to the system buffer
  709. */
  710. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  711. fc_fcp_recv_data(fsp, fp);
  712. seq->rec_data = fsp->xfer_contig_end;
  713. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  714. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  715. fc_fcp_resp(fsp, fp);
  716. } else {
  717. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  718. }
  719. unlock:
  720. fc_fcp_unlock_pkt(fsp);
  721. out:
  722. fc_frame_free(fp);
  723. }
  724. /**
  725. * fc_fcp_resp() - Handler for FCP responses
  726. * @fsp: The FCP packet the response is for
  727. * @fp: The response frame
  728. */
  729. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  730. {
  731. struct fc_frame_header *fh;
  732. struct fcp_resp *fc_rp;
  733. struct fcp_resp_ext *rp_ex;
  734. struct fcp_resp_rsp_info *fc_rp_info;
  735. u32 plen;
  736. u32 expected_len;
  737. u32 respl = 0;
  738. u32 snsl = 0;
  739. u8 flags = 0;
  740. plen = fr_len(fp);
  741. fh = (struct fc_frame_header *)fr_hdr(fp);
  742. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  743. goto len_err;
  744. plen -= sizeof(*fh);
  745. fc_rp = (struct fcp_resp *)(fh + 1);
  746. fsp->cdb_status = fc_rp->fr_status;
  747. flags = fc_rp->fr_flags;
  748. fsp->scsi_comp_flags = flags;
  749. expected_len = fsp->data_len;
  750. /* if ddp, update xfer len */
  751. fc_fcp_ddp_done(fsp);
  752. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  753. rp_ex = (void *)(fc_rp + 1);
  754. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  755. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  756. goto len_err;
  757. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  758. if (flags & FCP_RSP_LEN_VAL) {
  759. respl = ntohl(rp_ex->fr_rsp_len);
  760. if ((respl != FCP_RESP_RSP_INFO_LEN4) &&
  761. (respl != FCP_RESP_RSP_INFO_LEN8))
  762. goto len_err;
  763. if (fsp->wait_for_comp) {
  764. /* Abuse cdb_status for rsp code */
  765. fsp->cdb_status = fc_rp_info->rsp_code;
  766. complete(&fsp->tm_done);
  767. /*
  768. * tmfs will not have any scsi cmd so
  769. * exit here
  770. */
  771. return;
  772. }
  773. }
  774. if (flags & FCP_SNS_LEN_VAL) {
  775. snsl = ntohl(rp_ex->fr_sns_len);
  776. if (snsl > SCSI_SENSE_BUFFERSIZE)
  777. snsl = SCSI_SENSE_BUFFERSIZE;
  778. memcpy(fsp->cmd->sense_buffer,
  779. (char *)fc_rp_info + respl, snsl);
  780. }
  781. }
  782. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  783. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  784. goto len_err;
  785. if (flags & FCP_RESID_UNDER) {
  786. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  787. /*
  788. * The cmnd->underflow is the minimum number of
  789. * bytes that must be transferred for this
  790. * command. Provided a sense condition is not
  791. * present, make sure the actual amount
  792. * transferred is at least the underflow value
  793. * or fail.
  794. */
  795. if (!(flags & FCP_SNS_LEN_VAL) &&
  796. (fc_rp->fr_status == 0) &&
  797. (scsi_bufflen(fsp->cmd) -
  798. fsp->scsi_resid) < fsp->cmd->underflow)
  799. goto err;
  800. expected_len -= fsp->scsi_resid;
  801. } else {
  802. fsp->status_code = FC_ERROR;
  803. }
  804. }
  805. }
  806. fsp->state |= FC_SRB_RCV_STATUS;
  807. /*
  808. * Check for missing or extra data frames.
  809. */
  810. if (unlikely(fsp->cdb_status == SAM_STAT_GOOD &&
  811. fsp->xfer_len != expected_len)) {
  812. if (fsp->xfer_len < expected_len) {
  813. /*
  814. * Some data may be queued locally,
  815. * Wait a at least one jiffy to see if it is delivered.
  816. * If this expires without data, we may do SRR.
  817. */
  818. fc_fcp_timer_set(fsp, 2);
  819. return;
  820. }
  821. fsp->status_code = FC_DATA_OVRRUN;
  822. FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx greater than expected, "
  823. "len %x, data len %x\n",
  824. fsp->rport->port_id,
  825. fsp->xfer_len, expected_len, fsp->data_len);
  826. }
  827. fc_fcp_complete_locked(fsp);
  828. return;
  829. len_err:
  830. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  831. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  832. err:
  833. fsp->status_code = FC_ERROR;
  834. fc_fcp_complete_locked(fsp);
  835. }
  836. /**
  837. * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
  838. * fcp_pkt lock held
  839. * @fsp: The FCP packet to be completed
  840. *
  841. * This function may sleep if a timer is pending. The packet lock must be
  842. * held, and the host lock must not be held.
  843. */
  844. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  845. {
  846. struct fc_lport *lport = fsp->lp;
  847. struct fc_seq *seq;
  848. struct fc_exch *ep;
  849. u32 f_ctl;
  850. if (fsp->state & FC_SRB_ABORT_PENDING)
  851. return;
  852. if (fsp->state & FC_SRB_ABORTED) {
  853. if (!fsp->status_code)
  854. fsp->status_code = FC_CMD_ABORTED;
  855. } else {
  856. /*
  857. * Test for transport underrun, independent of response
  858. * underrun status.
  859. */
  860. if (fsp->cdb_status == SAM_STAT_GOOD &&
  861. fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  862. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  863. fsp->xfer_len < fsp->data_len - fsp->scsi_resid))
  864. fsp->status_code = FC_DATA_UNDRUN;
  865. }
  866. seq = fsp->seq_ptr;
  867. if (seq) {
  868. fsp->seq_ptr = NULL;
  869. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  870. struct fc_frame *conf_frame;
  871. struct fc_seq *csp;
  872. csp = lport->tt.seq_start_next(seq);
  873. conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
  874. if (conf_frame) {
  875. f_ctl = FC_FC_SEQ_INIT;
  876. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  877. ep = fc_seq_exch(seq);
  878. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  879. ep->did, ep->sid,
  880. FC_TYPE_FCP, f_ctl, 0);
  881. lport->tt.seq_send(lport, csp, conf_frame);
  882. }
  883. }
  884. lport->tt.exch_done(seq);
  885. }
  886. /*
  887. * Some resets driven by SCSI are not I/Os and do not have
  888. * SCSI commands associated with the requests. We should not
  889. * call I/O completion if we do not have a SCSI command.
  890. */
  891. if (fsp->cmd)
  892. fc_io_compl(fsp);
  893. }
  894. /**
  895. * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
  896. * @fsp: The FCP packet whose exchanges should be canceled
  897. * @error: The reason for the cancellation
  898. */
  899. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  900. {
  901. struct fc_lport *lport = fsp->lp;
  902. if (fsp->seq_ptr) {
  903. lport->tt.exch_done(fsp->seq_ptr);
  904. fsp->seq_ptr = NULL;
  905. }
  906. fsp->status_code = error;
  907. }
  908. /**
  909. * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
  910. * @lport: The local port whose exchanges should be canceled
  911. * @id: The target's ID
  912. * @lun: The LUN
  913. * @error: The reason for cancellation
  914. *
  915. * If lun or id is -1, they are ignored.
  916. */
  917. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
  918. unsigned int lun, int error)
  919. {
  920. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  921. struct fc_fcp_pkt *fsp;
  922. struct scsi_cmnd *sc_cmd;
  923. unsigned long flags;
  924. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  925. restart:
  926. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  927. sc_cmd = fsp->cmd;
  928. if (id != -1 && scmd_id(sc_cmd) != id)
  929. continue;
  930. if (lun != -1 && sc_cmd->device->lun != lun)
  931. continue;
  932. fc_fcp_pkt_hold(fsp);
  933. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  934. spin_lock_bh(&fsp->scsi_pkt_lock);
  935. if (!(fsp->state & FC_SRB_COMPL)) {
  936. fsp->state |= FC_SRB_COMPL;
  937. /*
  938. * TODO: dropping scsi_pkt_lock and then reacquiring
  939. * again around fc_fcp_cleanup_cmd() is required,
  940. * since fc_fcp_cleanup_cmd() calls into
  941. * fc_seq_set_resp() and that func preempts cpu using
  942. * schedule. May be schedule and related code should be
  943. * removed instead of unlocking here to avoid scheduling
  944. * while atomic bug.
  945. */
  946. spin_unlock_bh(&fsp->scsi_pkt_lock);
  947. fc_fcp_cleanup_cmd(fsp, error);
  948. spin_lock_bh(&fsp->scsi_pkt_lock);
  949. fc_io_compl(fsp);
  950. }
  951. spin_unlock_bh(&fsp->scsi_pkt_lock);
  952. fc_fcp_pkt_release(fsp);
  953. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  954. /*
  955. * while we dropped the lock multiple pkts could
  956. * have been released, so we have to start over.
  957. */
  958. goto restart;
  959. }
  960. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  961. }
  962. /**
  963. * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
  964. * @lport: The local port whose exchanges are to be aborted
  965. */
  966. static void fc_fcp_abort_io(struct fc_lport *lport)
  967. {
  968. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
  969. }
  970. /**
  971. * fc_fcp_pkt_send() - Send a fcp_pkt
  972. * @lport: The local port to send the FCP packet on
  973. * @fsp: The FCP packet to send
  974. *
  975. * Return: Zero for success and -1 for failure
  976. * Locks: Called without locks held
  977. */
  978. static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
  979. {
  980. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  981. unsigned long flags;
  982. int rc;
  983. fsp->cmd->SCp.ptr = (char *)fsp;
  984. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  985. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  986. int_to_scsilun(fsp->cmd->device->lun, &fsp->cdb_cmd.fc_lun);
  987. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  988. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  989. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  990. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  991. rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
  992. if (unlikely(rc)) {
  993. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  994. fsp->cmd->SCp.ptr = NULL;
  995. list_del(&fsp->list);
  996. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  997. }
  998. return rc;
  999. }
  1000. /**
  1001. * get_fsp_rec_tov() - Helper function to get REC_TOV
  1002. * @fsp: the FCP packet
  1003. *
  1004. * Returns rec tov in jiffies as rpriv->e_d_tov + 1 second
  1005. */
  1006. static inline unsigned int get_fsp_rec_tov(struct fc_fcp_pkt *fsp)
  1007. {
  1008. struct fc_rport_libfc_priv *rpriv = fsp->rport->dd_data;
  1009. return msecs_to_jiffies(rpriv->e_d_tov) + HZ;
  1010. }
  1011. /**
  1012. * fc_fcp_cmd_send() - Send a FCP command
  1013. * @lport: The local port to send the command on
  1014. * @fsp: The FCP packet the command is on
  1015. * @resp: The handler for the response
  1016. */
  1017. static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1018. void (*resp)(struct fc_seq *,
  1019. struct fc_frame *fp,
  1020. void *arg))
  1021. {
  1022. struct fc_frame *fp;
  1023. struct fc_seq *seq;
  1024. struct fc_rport *rport;
  1025. struct fc_rport_libfc_priv *rpriv;
  1026. const size_t len = sizeof(fsp->cdb_cmd);
  1027. int rc = 0;
  1028. if (fc_fcp_lock_pkt(fsp))
  1029. return 0;
  1030. fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
  1031. if (!fp) {
  1032. rc = -1;
  1033. goto unlock;
  1034. }
  1035. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  1036. fr_fsp(fp) = fsp;
  1037. rport = fsp->rport;
  1038. fsp->max_payload = rport->maxframe_size;
  1039. rpriv = rport->dd_data;
  1040. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  1041. rpriv->local_port->port_id, FC_TYPE_FCP,
  1042. FC_FCTL_REQ, 0);
  1043. seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
  1044. fsp, 0);
  1045. if (!seq) {
  1046. rc = -1;
  1047. goto unlock;
  1048. }
  1049. fsp->seq_ptr = seq;
  1050. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  1051. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  1052. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1053. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1054. unlock:
  1055. fc_fcp_unlock_pkt(fsp);
  1056. return rc;
  1057. }
  1058. /**
  1059. * fc_fcp_error() - Handler for FCP layer errors
  1060. * @fsp: The FCP packet the error is on
  1061. * @fp: The frame that has errored
  1062. */
  1063. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1064. {
  1065. int error = PTR_ERR(fp);
  1066. if (fc_fcp_lock_pkt(fsp))
  1067. return;
  1068. if (error == -FC_EX_CLOSED) {
  1069. fc_fcp_retry_cmd(fsp);
  1070. goto unlock;
  1071. }
  1072. /*
  1073. * clear abort pending, because the lower layer
  1074. * decided to force completion.
  1075. */
  1076. fsp->state &= ~FC_SRB_ABORT_PENDING;
  1077. fsp->status_code = FC_CMD_PLOGO;
  1078. fc_fcp_complete_locked(fsp);
  1079. unlock:
  1080. fc_fcp_unlock_pkt(fsp);
  1081. }
  1082. /**
  1083. * fc_fcp_pkt_abort() - Abort a fcp_pkt
  1084. * @fsp: The FCP packet to abort on
  1085. *
  1086. * Called to send an abort and then wait for abort completion
  1087. */
  1088. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  1089. {
  1090. int rc = FAILED;
  1091. unsigned long ticks_left;
  1092. if (fc_fcp_send_abort(fsp))
  1093. return FAILED;
  1094. init_completion(&fsp->tm_done);
  1095. fsp->wait_for_comp = 1;
  1096. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1097. ticks_left = wait_for_completion_timeout(&fsp->tm_done,
  1098. FC_SCSI_TM_TOV);
  1099. spin_lock_bh(&fsp->scsi_pkt_lock);
  1100. fsp->wait_for_comp = 0;
  1101. if (!ticks_left) {
  1102. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  1103. } else if (fsp->state & FC_SRB_ABORTED) {
  1104. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  1105. rc = SUCCESS;
  1106. fc_fcp_complete_locked(fsp);
  1107. }
  1108. return rc;
  1109. }
  1110. /**
  1111. * fc_lun_reset_send() - Send LUN reset command
  1112. * @data: The FCP packet that identifies the LUN to be reset
  1113. */
  1114. static void fc_lun_reset_send(unsigned long data)
  1115. {
  1116. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1117. struct fc_lport *lport = fsp->lp;
  1118. if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
  1119. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1120. return;
  1121. if (fc_fcp_lock_pkt(fsp))
  1122. return;
  1123. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1124. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1125. fc_fcp_unlock_pkt(fsp);
  1126. }
  1127. }
  1128. /**
  1129. * fc_lun_reset() - Send a LUN RESET command to a device
  1130. * and wait for the reply
  1131. * @lport: The local port to sent the command on
  1132. * @fsp: The FCP packet that identifies the LUN to be reset
  1133. * @id: The SCSI command ID
  1134. * @lun: The LUN ID to be reset
  1135. */
  1136. static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1137. unsigned int id, unsigned int lun)
  1138. {
  1139. int rc;
  1140. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1141. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1142. int_to_scsilun(lun, &fsp->cdb_cmd.fc_lun);
  1143. fsp->wait_for_comp = 1;
  1144. init_completion(&fsp->tm_done);
  1145. fc_lun_reset_send((unsigned long)fsp);
  1146. /*
  1147. * wait for completion of reset
  1148. * after that make sure all commands are terminated
  1149. */
  1150. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1151. spin_lock_bh(&fsp->scsi_pkt_lock);
  1152. fsp->state |= FC_SRB_COMPL;
  1153. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1154. del_timer_sync(&fsp->timer);
  1155. spin_lock_bh(&fsp->scsi_pkt_lock);
  1156. if (fsp->seq_ptr) {
  1157. lport->tt.exch_done(fsp->seq_ptr);
  1158. fsp->seq_ptr = NULL;
  1159. }
  1160. fsp->wait_for_comp = 0;
  1161. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1162. if (!rc) {
  1163. FC_SCSI_DBG(lport, "lun reset failed\n");
  1164. return FAILED;
  1165. }
  1166. /* cdb_status holds the tmf's rsp code */
  1167. if (fsp->cdb_status != FCP_TMF_CMPL)
  1168. return FAILED;
  1169. FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
  1170. fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
  1171. return SUCCESS;
  1172. }
  1173. /**
  1174. * fc_tm_done() - Task Management response handler
  1175. * @seq: The sequence that the response is on
  1176. * @fp: The response frame
  1177. * @arg: The FCP packet the response is for
  1178. */
  1179. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1180. {
  1181. struct fc_fcp_pkt *fsp = arg;
  1182. struct fc_frame_header *fh;
  1183. if (IS_ERR(fp)) {
  1184. /*
  1185. * If there is an error just let it timeout or wait
  1186. * for TMF to be aborted if it timedout.
  1187. *
  1188. * scsi-eh will escalate for when either happens.
  1189. */
  1190. return;
  1191. }
  1192. if (fc_fcp_lock_pkt(fsp))
  1193. goto out;
  1194. /*
  1195. * raced with eh timeout handler.
  1196. */
  1197. if (!fsp->seq_ptr || !fsp->wait_for_comp)
  1198. goto out_unlock;
  1199. fh = fc_frame_header_get(fp);
  1200. if (fh->fh_type != FC_TYPE_BLS)
  1201. fc_fcp_resp(fsp, fp);
  1202. fsp->seq_ptr = NULL;
  1203. fsp->lp->tt.exch_done(seq);
  1204. out_unlock:
  1205. fc_fcp_unlock_pkt(fsp);
  1206. out:
  1207. fc_frame_free(fp);
  1208. }
  1209. /**
  1210. * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
  1211. * @lport: The local port to be cleaned up
  1212. */
  1213. static void fc_fcp_cleanup(struct fc_lport *lport)
  1214. {
  1215. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
  1216. }
  1217. /**
  1218. * fc_fcp_timeout() - Handler for fcp_pkt timeouts
  1219. * @data: The FCP packet that has timed out
  1220. *
  1221. * If REC is supported then just issue it and return. The REC exchange will
  1222. * complete or time out and recovery can continue at that point. Otherwise,
  1223. * if the response has been received without all the data it has been
  1224. * ER_TIMEOUT since the response was received. If the response has not been
  1225. * received we see if data was received recently. If it has been then we
  1226. * continue waiting, otherwise, we abort the command.
  1227. */
  1228. static void fc_fcp_timeout(unsigned long data)
  1229. {
  1230. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1231. struct fc_rport *rport = fsp->rport;
  1232. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1233. if (fc_fcp_lock_pkt(fsp))
  1234. return;
  1235. if (fsp->cdb_cmd.fc_tm_flags)
  1236. goto unlock;
  1237. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1238. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1239. fc_fcp_rec(fsp);
  1240. else if (fsp->state & FC_SRB_RCV_STATUS)
  1241. fc_fcp_complete_locked(fsp);
  1242. else
  1243. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1244. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1245. unlock:
  1246. fc_fcp_unlock_pkt(fsp);
  1247. }
  1248. /**
  1249. * fc_fcp_rec() - Send a REC ELS request
  1250. * @fsp: The FCP packet to send the REC request on
  1251. */
  1252. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1253. {
  1254. struct fc_lport *lport;
  1255. struct fc_frame *fp;
  1256. struct fc_rport *rport;
  1257. struct fc_rport_libfc_priv *rpriv;
  1258. lport = fsp->lp;
  1259. rport = fsp->rport;
  1260. rpriv = rport->dd_data;
  1261. if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
  1262. fsp->status_code = FC_HRD_ERROR;
  1263. fsp->io_status = 0;
  1264. fc_fcp_complete_locked(fsp);
  1265. return;
  1266. }
  1267. fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
  1268. if (!fp)
  1269. goto retry;
  1270. fr_seq(fp) = fsp->seq_ptr;
  1271. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1272. rpriv->local_port->port_id, FC_TYPE_ELS,
  1273. FC_FCTL_REQ, 0);
  1274. if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
  1275. fc_fcp_rec_resp, fsp,
  1276. 2 * lport->r_a_tov)) {
  1277. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1278. return;
  1279. }
  1280. retry:
  1281. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1282. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1283. else
  1284. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1285. }
  1286. /**
  1287. * fc_fcp_rec_resp() - Handler for REC ELS responses
  1288. * @seq: The sequence the response is on
  1289. * @fp: The response frame
  1290. * @arg: The FCP packet the response is on
  1291. *
  1292. * If the response is a reject then the scsi layer will handle
  1293. * the timeout. If the response is a LS_ACC then if the I/O was not completed
  1294. * set the timeout and return. If the I/O was completed then complete the
  1295. * exchange and tell the SCSI layer.
  1296. */
  1297. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1298. {
  1299. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1300. struct fc_els_rec_acc *recp;
  1301. struct fc_els_ls_rjt *rjt;
  1302. u32 e_stat;
  1303. u8 opcode;
  1304. u32 offset;
  1305. enum dma_data_direction data_dir;
  1306. enum fc_rctl r_ctl;
  1307. struct fc_rport_libfc_priv *rpriv;
  1308. if (IS_ERR(fp)) {
  1309. fc_fcp_rec_error(fsp, fp);
  1310. return;
  1311. }
  1312. if (fc_fcp_lock_pkt(fsp))
  1313. goto out;
  1314. fsp->recov_retry = 0;
  1315. opcode = fc_frame_payload_op(fp);
  1316. if (opcode == ELS_LS_RJT) {
  1317. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1318. switch (rjt->er_reason) {
  1319. default:
  1320. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1321. "reason %d expl %d\n",
  1322. fsp->rport->port_id, rjt->er_reason,
  1323. rjt->er_explan);
  1324. /* fall through */
  1325. case ELS_RJT_UNSUP:
  1326. FC_FCP_DBG(fsp, "device does not support REC\n");
  1327. rpriv = fsp->rport->dd_data;
  1328. /*
  1329. * if we do not spport RECs or got some bogus
  1330. * reason then resetup timer so we check for
  1331. * making progress.
  1332. */
  1333. rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1334. break;
  1335. case ELS_RJT_LOGIC:
  1336. case ELS_RJT_UNAB:
  1337. /*
  1338. * If no data transfer, the command frame got dropped
  1339. * so we just retry. If data was transferred, we
  1340. * lost the response but the target has no record,
  1341. * so we abort and retry.
  1342. */
  1343. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1344. fsp->xfer_len == 0) {
  1345. fc_fcp_retry_cmd(fsp);
  1346. break;
  1347. }
  1348. fc_fcp_recovery(fsp, FC_ERROR);
  1349. break;
  1350. }
  1351. } else if (opcode == ELS_LS_ACC) {
  1352. if (fsp->state & FC_SRB_ABORTED)
  1353. goto unlock_out;
  1354. data_dir = fsp->cmd->sc_data_direction;
  1355. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1356. offset = ntohl(recp->reca_fc4value);
  1357. e_stat = ntohl(recp->reca_e_stat);
  1358. if (e_stat & ESB_ST_COMPLETE) {
  1359. /*
  1360. * The exchange is complete.
  1361. *
  1362. * For output, we must've lost the response.
  1363. * For input, all data must've been sent.
  1364. * We lost may have lost the response
  1365. * (and a confirmation was requested) and maybe
  1366. * some data.
  1367. *
  1368. * If all data received, send SRR
  1369. * asking for response. If partial data received,
  1370. * or gaps, SRR requests data at start of gap.
  1371. * Recovery via SRR relies on in-order-delivery.
  1372. */
  1373. if (data_dir == DMA_TO_DEVICE) {
  1374. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1375. } else if (fsp->xfer_contig_end == offset) {
  1376. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1377. } else {
  1378. offset = fsp->xfer_contig_end;
  1379. r_ctl = FC_RCTL_DD_SOL_DATA;
  1380. }
  1381. fc_fcp_srr(fsp, r_ctl, offset);
  1382. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1383. /*
  1384. * The remote port has the initiative, so just
  1385. * keep waiting for it to complete.
  1386. */
  1387. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1388. } else {
  1389. /*
  1390. * The exchange is incomplete, we have seq. initiative.
  1391. * Lost response with requested confirmation,
  1392. * lost confirmation, lost transfer ready or
  1393. * lost write data.
  1394. *
  1395. * For output, if not all data was received, ask
  1396. * for transfer ready to be repeated.
  1397. *
  1398. * If we received or sent all the data, send SRR to
  1399. * request response.
  1400. *
  1401. * If we lost a response, we may have lost some read
  1402. * data as well.
  1403. */
  1404. r_ctl = FC_RCTL_DD_SOL_DATA;
  1405. if (data_dir == DMA_TO_DEVICE) {
  1406. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1407. if (offset < fsp->data_len)
  1408. r_ctl = FC_RCTL_DD_DATA_DESC;
  1409. } else if (offset == fsp->xfer_contig_end) {
  1410. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1411. } else if (fsp->xfer_contig_end < offset) {
  1412. offset = fsp->xfer_contig_end;
  1413. }
  1414. fc_fcp_srr(fsp, r_ctl, offset);
  1415. }
  1416. }
  1417. unlock_out:
  1418. fc_fcp_unlock_pkt(fsp);
  1419. out:
  1420. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1421. fc_frame_free(fp);
  1422. }
  1423. /**
  1424. * fc_fcp_rec_error() - Handler for REC errors
  1425. * @fsp: The FCP packet the error is on
  1426. * @fp: The REC frame
  1427. */
  1428. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1429. {
  1430. int error = PTR_ERR(fp);
  1431. if (fc_fcp_lock_pkt(fsp))
  1432. goto out;
  1433. switch (error) {
  1434. case -FC_EX_CLOSED:
  1435. fc_fcp_retry_cmd(fsp);
  1436. break;
  1437. default:
  1438. FC_FCP_DBG(fsp, "REC %p fid %6.6x error unexpected error %d\n",
  1439. fsp, fsp->rport->port_id, error);
  1440. fsp->status_code = FC_CMD_PLOGO;
  1441. /* fall through */
  1442. case -FC_EX_TIMEOUT:
  1443. /*
  1444. * Assume REC or LS_ACC was lost.
  1445. * The exchange manager will have aborted REC, so retry.
  1446. */
  1447. FC_FCP_DBG(fsp, "REC fid %6.6x error error %d retry %d/%d\n",
  1448. fsp->rport->port_id, error, fsp->recov_retry,
  1449. FC_MAX_RECOV_RETRY);
  1450. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1451. fc_fcp_rec(fsp);
  1452. else
  1453. fc_fcp_recovery(fsp, FC_ERROR);
  1454. break;
  1455. }
  1456. fc_fcp_unlock_pkt(fsp);
  1457. out:
  1458. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1459. }
  1460. /**
  1461. * fc_fcp_recovery() - Handler for fcp_pkt recovery
  1462. * @fsp: The FCP pkt that needs to be aborted
  1463. */
  1464. static void fc_fcp_recovery(struct fc_fcp_pkt *fsp, u8 code)
  1465. {
  1466. fsp->status_code = code;
  1467. fsp->cdb_status = 0;
  1468. fsp->io_status = 0;
  1469. /*
  1470. * if this fails then we let the scsi command timer fire and
  1471. * scsi-ml escalate.
  1472. */
  1473. fc_fcp_send_abort(fsp);
  1474. }
  1475. /**
  1476. * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
  1477. * @fsp: The FCP packet the SRR is to be sent on
  1478. * @r_ctl: The R_CTL field for the SRR request
  1479. * This is called after receiving status but insufficient data, or
  1480. * when expecting status but the request has timed out.
  1481. */
  1482. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1483. {
  1484. struct fc_lport *lport = fsp->lp;
  1485. struct fc_rport *rport;
  1486. struct fc_rport_libfc_priv *rpriv;
  1487. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1488. struct fc_seq *seq;
  1489. struct fcp_srr *srr;
  1490. struct fc_frame *fp;
  1491. unsigned int rec_tov;
  1492. rport = fsp->rport;
  1493. rpriv = rport->dd_data;
  1494. if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
  1495. rpriv->rp_state != RPORT_ST_READY)
  1496. goto retry; /* shouldn't happen */
  1497. fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
  1498. if (!fp)
  1499. goto retry;
  1500. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1501. memset(srr, 0, sizeof(*srr));
  1502. srr->srr_op = ELS_SRR;
  1503. srr->srr_ox_id = htons(ep->oxid);
  1504. srr->srr_rx_id = htons(ep->rxid);
  1505. srr->srr_r_ctl = r_ctl;
  1506. srr->srr_rel_off = htonl(offset);
  1507. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1508. rpriv->local_port->port_id, FC_TYPE_FCP,
  1509. FC_FCTL_REQ, 0);
  1510. rec_tov = get_fsp_rec_tov(fsp);
  1511. seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp,
  1512. fc_fcp_pkt_destroy,
  1513. fsp, jiffies_to_msecs(rec_tov));
  1514. if (!seq)
  1515. goto retry;
  1516. fsp->recov_seq = seq;
  1517. fsp->xfer_len = offset;
  1518. fsp->xfer_contig_end = offset;
  1519. fsp->state &= ~FC_SRB_RCV_STATUS;
  1520. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1521. return;
  1522. retry:
  1523. fc_fcp_retry_cmd(fsp);
  1524. }
  1525. /**
  1526. * fc_fcp_srr_resp() - Handler for SRR response
  1527. * @seq: The sequence the SRR is on
  1528. * @fp: The SRR frame
  1529. * @arg: The FCP packet the SRR is on
  1530. */
  1531. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1532. {
  1533. struct fc_fcp_pkt *fsp = arg;
  1534. struct fc_frame_header *fh;
  1535. if (IS_ERR(fp)) {
  1536. fc_fcp_srr_error(fsp, fp);
  1537. return;
  1538. }
  1539. if (fc_fcp_lock_pkt(fsp))
  1540. goto out;
  1541. fh = fc_frame_header_get(fp);
  1542. /*
  1543. * BUG? fc_fcp_srr_error calls exch_done which would release
  1544. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1545. * then fc_exch_timeout would be sending an abort. The exch_done
  1546. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1547. * an abort response though.
  1548. */
  1549. if (fh->fh_type == FC_TYPE_BLS) {
  1550. fc_fcp_unlock_pkt(fsp);
  1551. return;
  1552. }
  1553. switch (fc_frame_payload_op(fp)) {
  1554. case ELS_LS_ACC:
  1555. fsp->recov_retry = 0;
  1556. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1557. break;
  1558. case ELS_LS_RJT:
  1559. default:
  1560. fc_fcp_recovery(fsp, FC_ERROR);
  1561. break;
  1562. }
  1563. fc_fcp_unlock_pkt(fsp);
  1564. out:
  1565. fsp->lp->tt.exch_done(seq);
  1566. fc_frame_free(fp);
  1567. }
  1568. /**
  1569. * fc_fcp_srr_error() - Handler for SRR errors
  1570. * @fsp: The FCP packet that the SRR error is on
  1571. * @fp: The SRR frame
  1572. */
  1573. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1574. {
  1575. if (fc_fcp_lock_pkt(fsp))
  1576. goto out;
  1577. switch (PTR_ERR(fp)) {
  1578. case -FC_EX_TIMEOUT:
  1579. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1580. fc_fcp_rec(fsp);
  1581. else
  1582. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1583. break;
  1584. case -FC_EX_CLOSED: /* e.g., link failure */
  1585. /* fall through */
  1586. default:
  1587. fc_fcp_retry_cmd(fsp);
  1588. break;
  1589. }
  1590. fc_fcp_unlock_pkt(fsp);
  1591. out:
  1592. fsp->lp->tt.exch_done(fsp->recov_seq);
  1593. }
  1594. /**
  1595. * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
  1596. * @lport: The local port to be checked
  1597. */
  1598. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
  1599. {
  1600. /* lock ? */
  1601. return (lport->state == LPORT_ST_READY) &&
  1602. lport->link_up && !lport->qfull;
  1603. }
  1604. /**
  1605. * fc_queuecommand() - The queuecommand function of the SCSI template
  1606. * @shost: The Scsi_Host that the command was issued to
  1607. * @cmd: The scsi_cmnd to be executed
  1608. *
  1609. * This is the i/o strategy routine, called by the SCSI layer.
  1610. */
  1611. int fc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc_cmd)
  1612. {
  1613. struct fc_lport *lport = shost_priv(shost);
  1614. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1615. struct fc_fcp_pkt *fsp;
  1616. struct fc_rport_libfc_priv *rpriv;
  1617. int rval;
  1618. int rc = 0;
  1619. struct fc_stats *stats;
  1620. rval = fc_remote_port_chkready(rport);
  1621. if (rval) {
  1622. sc_cmd->result = rval;
  1623. sc_cmd->scsi_done(sc_cmd);
  1624. return 0;
  1625. }
  1626. if (!*(struct fc_remote_port **)rport->dd_data) {
  1627. /*
  1628. * rport is transitioning from blocked/deleted to
  1629. * online
  1630. */
  1631. sc_cmd->result = DID_IMM_RETRY << 16;
  1632. sc_cmd->scsi_done(sc_cmd);
  1633. goto out;
  1634. }
  1635. rpriv = rport->dd_data;
  1636. if (!fc_fcp_lport_queue_ready(lport)) {
  1637. if (lport->qfull)
  1638. fc_fcp_can_queue_ramp_down(lport);
  1639. rc = SCSI_MLQUEUE_HOST_BUSY;
  1640. goto out;
  1641. }
  1642. fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
  1643. if (fsp == NULL) {
  1644. rc = SCSI_MLQUEUE_HOST_BUSY;
  1645. goto out;
  1646. }
  1647. /*
  1648. * build the libfc request pkt
  1649. */
  1650. fsp->cmd = sc_cmd; /* save the cmd */
  1651. fsp->rport = rport; /* set the remote port ptr */
  1652. /*
  1653. * set up the transfer length
  1654. */
  1655. fsp->data_len = scsi_bufflen(sc_cmd);
  1656. fsp->xfer_len = 0;
  1657. /*
  1658. * setup the data direction
  1659. */
  1660. stats = per_cpu_ptr(lport->stats, get_cpu());
  1661. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1662. fsp->req_flags = FC_SRB_READ;
  1663. stats->InputRequests++;
  1664. stats->InputBytes += fsp->data_len;
  1665. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1666. fsp->req_flags = FC_SRB_WRITE;
  1667. stats->OutputRequests++;
  1668. stats->OutputBytes += fsp->data_len;
  1669. } else {
  1670. fsp->req_flags = 0;
  1671. stats->ControlRequests++;
  1672. }
  1673. put_cpu();
  1674. /*
  1675. * send it to the lower layer
  1676. * if we get -1 return then put the request in the pending
  1677. * queue.
  1678. */
  1679. rval = fc_fcp_pkt_send(lport, fsp);
  1680. if (rval != 0) {
  1681. fsp->state = FC_SRB_FREE;
  1682. fc_fcp_pkt_release(fsp);
  1683. rc = SCSI_MLQUEUE_HOST_BUSY;
  1684. }
  1685. out:
  1686. return rc;
  1687. }
  1688. EXPORT_SYMBOL(fc_queuecommand);
  1689. /**
  1690. * fc_io_compl() - Handle responses for completed commands
  1691. * @fsp: The FCP packet that is complete
  1692. *
  1693. * Translates fcp_pkt errors to a Linux SCSI errors.
  1694. * The fcp packet lock must be held when calling.
  1695. */
  1696. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1697. {
  1698. struct fc_fcp_internal *si;
  1699. struct scsi_cmnd *sc_cmd;
  1700. struct fc_lport *lport;
  1701. unsigned long flags;
  1702. /* release outstanding ddp context */
  1703. fc_fcp_ddp_done(fsp);
  1704. fsp->state |= FC_SRB_COMPL;
  1705. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1706. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1707. del_timer_sync(&fsp->timer);
  1708. spin_lock_bh(&fsp->scsi_pkt_lock);
  1709. }
  1710. lport = fsp->lp;
  1711. si = fc_get_scsi_internal(lport);
  1712. /*
  1713. * if can_queue ramp down is done then try can_queue ramp up
  1714. * since commands are completing now.
  1715. */
  1716. if (si->last_can_queue_ramp_down_time)
  1717. fc_fcp_can_queue_ramp_up(lport);
  1718. sc_cmd = fsp->cmd;
  1719. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1720. switch (fsp->status_code) {
  1721. case FC_COMPLETE:
  1722. if (fsp->cdb_status == 0) {
  1723. /*
  1724. * good I/O status
  1725. */
  1726. sc_cmd->result = DID_OK << 16;
  1727. if (fsp->scsi_resid)
  1728. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1729. } else {
  1730. /*
  1731. * transport level I/O was ok but scsi
  1732. * has non zero status
  1733. */
  1734. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1735. }
  1736. break;
  1737. case FC_ERROR:
  1738. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1739. "due to FC_ERROR\n");
  1740. sc_cmd->result = DID_ERROR << 16;
  1741. break;
  1742. case FC_DATA_UNDRUN:
  1743. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1744. /*
  1745. * scsi status is good but transport level
  1746. * underrun.
  1747. */
  1748. if (fsp->state & FC_SRB_RCV_STATUS) {
  1749. sc_cmd->result = DID_OK << 16;
  1750. } else {
  1751. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml"
  1752. " due to FC_DATA_UNDRUN (trans)\n");
  1753. sc_cmd->result = DID_ERROR << 16;
  1754. }
  1755. } else {
  1756. /*
  1757. * scsi got underrun, this is an error
  1758. */
  1759. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1760. "due to FC_DATA_UNDRUN (scsi)\n");
  1761. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1762. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1763. }
  1764. break;
  1765. case FC_DATA_OVRRUN:
  1766. /*
  1767. * overrun is an error
  1768. */
  1769. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1770. "due to FC_DATA_OVRRUN\n");
  1771. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1772. break;
  1773. case FC_CMD_ABORTED:
  1774. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1775. "due to FC_CMD_ABORTED\n");
  1776. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1777. break;
  1778. case FC_CMD_RESET:
  1779. FC_FCP_DBG(fsp, "Returning DID_RESET to scsi-ml "
  1780. "due to FC_CMD_RESET\n");
  1781. sc_cmd->result = (DID_RESET << 16);
  1782. break;
  1783. case FC_HRD_ERROR:
  1784. FC_FCP_DBG(fsp, "Returning DID_NO_CONNECT to scsi-ml "
  1785. "due to FC_HRD_ERROR\n");
  1786. sc_cmd->result = (DID_NO_CONNECT << 16);
  1787. break;
  1788. case FC_CRC_ERROR:
  1789. FC_FCP_DBG(fsp, "Returning DID_PARITY to scsi-ml "
  1790. "due to FC_CRC_ERROR\n");
  1791. sc_cmd->result = (DID_PARITY << 16);
  1792. break;
  1793. case FC_TIMED_OUT:
  1794. FC_FCP_DBG(fsp, "Returning DID_BUS_BUSY to scsi-ml "
  1795. "due to FC_TIMED_OUT\n");
  1796. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1797. break;
  1798. default:
  1799. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1800. "due to unknown error\n");
  1801. sc_cmd->result = (DID_ERROR << 16);
  1802. break;
  1803. }
  1804. if (lport->state != LPORT_ST_READY && fsp->status_code != FC_COMPLETE)
  1805. sc_cmd->result = (DID_TRANSPORT_DISRUPTED << 16);
  1806. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1807. list_del(&fsp->list);
  1808. sc_cmd->SCp.ptr = NULL;
  1809. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1810. sc_cmd->scsi_done(sc_cmd);
  1811. /* release ref from initial allocation in queue command */
  1812. fc_fcp_pkt_release(fsp);
  1813. }
  1814. /**
  1815. * fc_eh_abort() - Abort a command
  1816. * @sc_cmd: The SCSI command to abort
  1817. *
  1818. * From SCSI host template.
  1819. * Send an ABTS to the target device and wait for the response.
  1820. */
  1821. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1822. {
  1823. struct fc_fcp_pkt *fsp;
  1824. struct fc_lport *lport;
  1825. struct fc_fcp_internal *si;
  1826. int rc = FAILED;
  1827. unsigned long flags;
  1828. int rval;
  1829. rval = fc_block_scsi_eh(sc_cmd);
  1830. if (rval)
  1831. return rval;
  1832. lport = shost_priv(sc_cmd->device->host);
  1833. if (lport->state != LPORT_ST_READY)
  1834. return rc;
  1835. else if (!lport->link_up)
  1836. return rc;
  1837. si = fc_get_scsi_internal(lport);
  1838. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1839. fsp = CMD_SP(sc_cmd);
  1840. if (!fsp) {
  1841. /* command completed while scsi eh was setting up */
  1842. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1843. return SUCCESS;
  1844. }
  1845. /* grab a ref so the fsp and sc_cmd cannot be released from under us */
  1846. fc_fcp_pkt_hold(fsp);
  1847. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1848. if (fc_fcp_lock_pkt(fsp)) {
  1849. /* completed while we were waiting for timer to be deleted */
  1850. rc = SUCCESS;
  1851. goto release_pkt;
  1852. }
  1853. rc = fc_fcp_pkt_abort(fsp);
  1854. fc_fcp_unlock_pkt(fsp);
  1855. release_pkt:
  1856. fc_fcp_pkt_release(fsp);
  1857. return rc;
  1858. }
  1859. EXPORT_SYMBOL(fc_eh_abort);
  1860. /**
  1861. * fc_eh_device_reset() - Reset a single LUN
  1862. * @sc_cmd: The SCSI command which identifies the device whose
  1863. * LUN is to be reset
  1864. *
  1865. * Set from SCSI host template.
  1866. */
  1867. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1868. {
  1869. struct fc_lport *lport;
  1870. struct fc_fcp_pkt *fsp;
  1871. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1872. int rc = FAILED;
  1873. int rval;
  1874. rval = fc_block_scsi_eh(sc_cmd);
  1875. if (rval)
  1876. return rval;
  1877. lport = shost_priv(sc_cmd->device->host);
  1878. if (lport->state != LPORT_ST_READY)
  1879. return rc;
  1880. FC_SCSI_DBG(lport, "Resetting rport (%6.6x)\n", rport->port_id);
  1881. fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
  1882. if (fsp == NULL) {
  1883. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1884. goto out;
  1885. }
  1886. /*
  1887. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1888. * the sc passed in is not setup for execution like when sent
  1889. * through the queuecommand callout.
  1890. */
  1891. fsp->rport = rport; /* set the remote port ptr */
  1892. /*
  1893. * flush outstanding commands
  1894. */
  1895. rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1896. fsp->state = FC_SRB_FREE;
  1897. fc_fcp_pkt_release(fsp);
  1898. out:
  1899. return rc;
  1900. }
  1901. EXPORT_SYMBOL(fc_eh_device_reset);
  1902. /**
  1903. * fc_eh_host_reset() - Reset a Scsi_Host.
  1904. * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
  1905. */
  1906. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1907. {
  1908. struct Scsi_Host *shost = sc_cmd->device->host;
  1909. struct fc_lport *lport = shost_priv(shost);
  1910. unsigned long wait_tmo;
  1911. FC_SCSI_DBG(lport, "Resetting host\n");
  1912. fc_block_scsi_eh(sc_cmd);
  1913. lport->tt.lport_reset(lport);
  1914. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1915. while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
  1916. wait_tmo))
  1917. msleep(1000);
  1918. if (fc_fcp_lport_queue_ready(lport)) {
  1919. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1920. "on port (%6.6x)\n", lport->port_id);
  1921. return SUCCESS;
  1922. } else {
  1923. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1924. "port (%6.6x) is not ready.\n",
  1925. lport->port_id);
  1926. return FAILED;
  1927. }
  1928. }
  1929. EXPORT_SYMBOL(fc_eh_host_reset);
  1930. /**
  1931. * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
  1932. * @sdev: The SCSI device that identifies the SCSI host
  1933. *
  1934. * Configures queue depth based on host's cmd_per_len. If not set
  1935. * then we use the libfc default.
  1936. */
  1937. int fc_slave_alloc(struct scsi_device *sdev)
  1938. {
  1939. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1940. if (!rport || fc_remote_port_chkready(rport))
  1941. return -ENXIO;
  1942. scsi_change_queue_depth(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
  1943. return 0;
  1944. }
  1945. EXPORT_SYMBOL(fc_slave_alloc);
  1946. /**
  1947. * fc_fcp_destory() - Tear down the FCP layer for a given local port
  1948. * @lport: The local port that no longer needs the FCP layer
  1949. */
  1950. void fc_fcp_destroy(struct fc_lport *lport)
  1951. {
  1952. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  1953. if (!list_empty(&si->scsi_pkt_queue))
  1954. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  1955. "port (%6.6x)\n", lport->port_id);
  1956. mempool_destroy(si->scsi_pkt_pool);
  1957. kfree(si);
  1958. lport->scsi_priv = NULL;
  1959. }
  1960. EXPORT_SYMBOL(fc_fcp_destroy);
  1961. int fc_setup_fcp(void)
  1962. {
  1963. int rc = 0;
  1964. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1965. sizeof(struct fc_fcp_pkt),
  1966. 0, SLAB_HWCACHE_ALIGN, NULL);
  1967. if (!scsi_pkt_cachep) {
  1968. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  1969. "module load failed!");
  1970. rc = -ENOMEM;
  1971. }
  1972. return rc;
  1973. }
  1974. void fc_destroy_fcp(void)
  1975. {
  1976. if (scsi_pkt_cachep)
  1977. kmem_cache_destroy(scsi_pkt_cachep);
  1978. }
  1979. /**
  1980. * fc_fcp_init() - Initialize the FCP layer for a local port
  1981. * @lport: The local port to initialize the exchange layer for
  1982. */
  1983. int fc_fcp_init(struct fc_lport *lport)
  1984. {
  1985. int rc;
  1986. struct fc_fcp_internal *si;
  1987. if (!lport->tt.fcp_cmd_send)
  1988. lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1989. if (!lport->tt.fcp_cleanup)
  1990. lport->tt.fcp_cleanup = fc_fcp_cleanup;
  1991. if (!lport->tt.fcp_abort_io)
  1992. lport->tt.fcp_abort_io = fc_fcp_abort_io;
  1993. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  1994. if (!si)
  1995. return -ENOMEM;
  1996. lport->scsi_priv = si;
  1997. si->max_can_queue = lport->host->can_queue;
  1998. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  1999. spin_lock_init(&si->scsi_queue_lock);
  2000. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  2001. if (!si->scsi_pkt_pool) {
  2002. rc = -ENOMEM;
  2003. goto free_internal;
  2004. }
  2005. return 0;
  2006. free_internal:
  2007. kfree(si);
  2008. return rc;
  2009. }
  2010. EXPORT_SYMBOL(fc_fcp_init);