fc_fcp.c 59 KB

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