fc_fcp.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  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. /* constant added to e_d_tov timeout to get rec_tov value */
  52. #define REC_TOV_CONST 1
  53. /*
  54. * The SCp.ptr should be tested and set under the scsi_pkt_queue lock
  55. */
  56. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  57. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  58. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  59. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  60. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  61. /**
  62. * struct fc_fcp_internal - FCP layer internal data
  63. * @scsi_pkt_pool: Memory pool to draw FCP packets from
  64. * @scsi_queue_lock: Protects the scsi_pkt_queue
  65. * @scsi_pkt_queue: Current FCP packets
  66. * @last_can_queue_ramp_down_time: ramp down time
  67. * @last_can_queue_ramp_up_time: ramp up time
  68. * @max_can_queue: max can_queue size
  69. */
  70. struct fc_fcp_internal {
  71. mempool_t *scsi_pkt_pool;
  72. spinlock_t scsi_queue_lock;
  73. struct list_head scsi_pkt_queue;
  74. unsigned long last_can_queue_ramp_down_time;
  75. unsigned long last_can_queue_ramp_up_time;
  76. int max_can_queue;
  77. };
  78. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  79. /*
  80. * function prototypes
  81. * FC scsi I/O related functions
  82. */
  83. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  84. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  85. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  86. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  87. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  88. static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
  89. static void fc_fcp_recovery(struct fc_fcp_pkt *, u8 code);
  90. static void fc_fcp_timeout(unsigned long);
  91. static void fc_fcp_rec(struct fc_fcp_pkt *);
  92. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  93. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  94. static void fc_io_compl(struct fc_fcp_pkt *);
  95. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  96. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  97. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  98. /*
  99. * command status codes
  100. */
  101. #define FC_COMPLETE 0
  102. #define FC_CMD_ABORTED 1
  103. #define FC_CMD_RESET 2
  104. #define FC_CMD_PLOGO 3
  105. #define FC_SNS_RCV 4
  106. #define FC_TRANS_ERR 5
  107. #define FC_DATA_OVRRUN 6
  108. #define FC_DATA_UNDRUN 7
  109. #define FC_ERROR 8
  110. #define FC_HRD_ERROR 9
  111. #define FC_CRC_ERROR 10
  112. #define FC_TIMED_OUT 11
  113. /*
  114. * Error recovery timeout values.
  115. */
  116. #define FC_SCSI_TM_TOV (10 * HZ)
  117. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  118. #define FC_CAN_QUEUE_PERIOD (60 * HZ)
  119. #define FC_MAX_ERROR_CNT 5
  120. #define FC_MAX_RECOV_RETRY 3
  121. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  122. /**
  123. * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
  124. * @lport: The local port that the FCP packet is for
  125. * @gfp: GFP flags for allocation
  126. *
  127. * Return value: fcp_pkt structure or null on allocation failure.
  128. * Context: Can be called from process context, no lock is required.
  129. */
  130. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
  131. {
  132. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  133. struct fc_fcp_pkt *fsp;
  134. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  135. if (fsp) {
  136. memset(fsp, 0, sizeof(*fsp));
  137. fsp->lp = lport;
  138. fsp->xfer_ddp = FC_XID_UNKNOWN;
  139. atomic_set(&fsp->ref_cnt, 1);
  140. init_timer(&fsp->timer);
  141. INIT_LIST_HEAD(&fsp->list);
  142. spin_lock_init(&fsp->scsi_pkt_lock);
  143. }
  144. return fsp;
  145. }
  146. /**
  147. * fc_fcp_pkt_release() - Release hold on a fcp_pkt
  148. * @fsp: The FCP packet to be released
  149. *
  150. * Context: Can be called from process or interrupt context,
  151. * no lock is required.
  152. */
  153. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  154. {
  155. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  156. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  157. mempool_free(fsp, si->scsi_pkt_pool);
  158. }
  159. }
  160. /**
  161. * fc_fcp_pkt_hold() - Hold a fcp_pkt
  162. * @fsp: The FCP packet to be held
  163. */
  164. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  165. {
  166. atomic_inc(&fsp->ref_cnt);
  167. }
  168. /**
  169. * fc_fcp_pkt_destory() - Release hold on a fcp_pkt
  170. * @seq: The sequence that the FCP packet is on (required by destructor API)
  171. * @fsp: The FCP packet to be released
  172. *
  173. * This routine is called by a destructor callback in the exch_seq_send()
  174. * routine of the libfc Transport Template. The 'struct fc_seq' is a required
  175. * argument even though it is not used by this routine.
  176. *
  177. * Context: No locking required.
  178. */
  179. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  180. {
  181. fc_fcp_pkt_release(fsp);
  182. }
  183. /**
  184. * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
  185. * @fsp: The FCP packet to be locked and incremented
  186. *
  187. * We should only return error if we return a command to SCSI-ml before
  188. * getting a response. This can happen in cases where we send a abort, but
  189. * do not wait for the response and the abort and command can be passing
  190. * each other on the wire/network-layer.
  191. *
  192. * Note: this function locks the packet and gets a reference to allow
  193. * callers to call the completion function while the lock is held and
  194. * not have to worry about the packets refcount.
  195. *
  196. * TODO: Maybe we should just have callers grab/release the lock and
  197. * have a function that they call to verify the fsp and grab a ref if
  198. * needed.
  199. */
  200. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  201. {
  202. spin_lock_bh(&fsp->scsi_pkt_lock);
  203. if (fsp->state & FC_SRB_COMPL) {
  204. spin_unlock_bh(&fsp->scsi_pkt_lock);
  205. return -EPERM;
  206. }
  207. fc_fcp_pkt_hold(fsp);
  208. return 0;
  209. }
  210. /**
  211. * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
  212. * reference count
  213. * @fsp: The FCP packet to be unlocked and decremented
  214. */
  215. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  216. {
  217. spin_unlock_bh(&fsp->scsi_pkt_lock);
  218. fc_fcp_pkt_release(fsp);
  219. }
  220. /**
  221. * fc_fcp_timer_set() - Start a timer for a fcp_pkt
  222. * @fsp: The FCP packet to start a timer for
  223. * @delay: The timeout period for the timer
  224. */
  225. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  226. {
  227. if (!(fsp->state & FC_SRB_COMPL))
  228. mod_timer(&fsp->timer, jiffies + delay);
  229. }
  230. /**
  231. * fc_fcp_send_abort() - Send an abort for exchanges associated with a
  232. * fcp_pkt
  233. * @fsp: The FCP packet to abort exchanges on
  234. */
  235. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  236. {
  237. if (!fsp->seq_ptr)
  238. return -EINVAL;
  239. fsp->state |= FC_SRB_ABORT_PENDING;
  240. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  241. }
  242. /**
  243. * fc_fcp_retry_cmd() - Retry a fcp_pkt
  244. * @fsp: The FCP packet to be retried
  245. *
  246. * Sets the status code to be FC_ERROR and then calls
  247. * fc_fcp_complete_locked() which in turn calls fc_io_compl().
  248. * fc_io_compl() will notify the SCSI-ml that the I/O is done.
  249. * The SCSI-ml will retry the command.
  250. */
  251. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  252. {
  253. if (fsp->seq_ptr) {
  254. fsp->lp->tt.exch_done(fsp->seq_ptr);
  255. fsp->seq_ptr = NULL;
  256. }
  257. fsp->state &= ~FC_SRB_ABORT_PENDING;
  258. fsp->io_status = 0;
  259. fsp->status_code = FC_ERROR;
  260. fc_fcp_complete_locked(fsp);
  261. }
  262. /**
  263. * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
  264. * @fsp: The FCP packet that will manage the DDP frames
  265. * @xid: The XID that will be used for the DDP exchange
  266. */
  267. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  268. {
  269. struct fc_lport *lport;
  270. lport = fsp->lp;
  271. if ((fsp->req_flags & FC_SRB_READ) &&
  272. (lport->lro_enabled) && (lport->tt.ddp_setup)) {
  273. if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
  274. scsi_sg_count(fsp->cmd)))
  275. fsp->xfer_ddp = xid;
  276. }
  277. }
  278. /**
  279. * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
  280. * DDP related resources for a fcp_pkt
  281. * @fsp: The FCP packet that DDP had been used on
  282. */
  283. static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  284. {
  285. struct fc_lport *lport;
  286. if (!fsp)
  287. return;
  288. if (fsp->xfer_ddp == FC_XID_UNKNOWN)
  289. return;
  290. lport = fsp->lp;
  291. if (lport->tt.ddp_done) {
  292. fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
  293. fsp->xfer_ddp = FC_XID_UNKNOWN;
  294. }
  295. }
  296. /**
  297. * fc_fcp_can_queue_ramp_up() - increases can_queue
  298. * @lport: lport to ramp up can_queue
  299. *
  300. * Locking notes: Called with Scsi_Host lock held
  301. */
  302. static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport)
  303. {
  304. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  305. int can_queue;
  306. if (si->last_can_queue_ramp_up_time &&
  307. (time_before(jiffies, si->last_can_queue_ramp_up_time +
  308. FC_CAN_QUEUE_PERIOD)))
  309. return;
  310. if (time_before(jiffies, si->last_can_queue_ramp_down_time +
  311. FC_CAN_QUEUE_PERIOD))
  312. return;
  313. si->last_can_queue_ramp_up_time = jiffies;
  314. can_queue = lport->host->can_queue << 1;
  315. if (can_queue >= si->max_can_queue) {
  316. can_queue = si->max_can_queue;
  317. si->last_can_queue_ramp_down_time = 0;
  318. }
  319. lport->host->can_queue = can_queue;
  320. shost_printk(KERN_ERR, lport->host, "libfc: increased "
  321. "can_queue to %d.\n", can_queue);
  322. }
  323. /**
  324. * fc_fcp_can_queue_ramp_down() - reduces can_queue
  325. * @lport: lport to reduce can_queue
  326. *
  327. * If we are getting memory allocation failures, then we may
  328. * be trying to execute too many commands. We let the running
  329. * commands complete or timeout, then try again with a reduced
  330. * can_queue. Eventually we will hit the point where we run
  331. * on all reserved structs.
  332. *
  333. * Locking notes: Called with Scsi_Host lock held
  334. */
  335. static void fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
  336. {
  337. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  338. int can_queue;
  339. if (si->last_can_queue_ramp_down_time &&
  340. (time_before(jiffies, si->last_can_queue_ramp_down_time +
  341. FC_CAN_QUEUE_PERIOD)))
  342. return;
  343. si->last_can_queue_ramp_down_time = jiffies;
  344. can_queue = lport->host->can_queue;
  345. can_queue >>= 1;
  346. if (!can_queue)
  347. can_queue = 1;
  348. lport->host->can_queue = can_queue;
  349. shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
  350. "Reducing can_queue to %d.\n", can_queue);
  351. }
  352. /*
  353. * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer.
  354. * @lport: fc lport struct
  355. * @len: payload length
  356. *
  357. * Allocates fc_frame structure and buffer but if fails to allocate
  358. * then reduce can_queue.
  359. */
  360. static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
  361. size_t len)
  362. {
  363. struct fc_frame *fp;
  364. unsigned long flags;
  365. fp = fc_frame_alloc(lport, len);
  366. if (likely(fp))
  367. return fp;
  368. /* error case */
  369. spin_lock_irqsave(lport->host->host_lock, flags);
  370. fc_fcp_can_queue_ramp_down(lport);
  371. spin_unlock_irqrestore(lport->host->host_lock, flags);
  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, KM_SOFTIRQ0, 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, KM_SOFTIRQ0, &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++ < 5)
  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, KM_SOFTIRQ0);
  582. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  583. sg_bytes);
  584. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  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. fc_fcp_retry_cmd(fsp);
  610. return 0;
  611. }
  612. fp = NULL;
  613. }
  614. fsp->xfer_len += seq_blen; /* premature count? */
  615. return 0;
  616. }
  617. /**
  618. * fc_fcp_abts_resp() - Send an ABTS response
  619. * @fsp: The FCP packet that is being aborted
  620. * @fp: The response frame
  621. */
  622. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  623. {
  624. int ba_done = 1;
  625. struct fc_ba_rjt *brp;
  626. struct fc_frame_header *fh;
  627. fh = fc_frame_header_get(fp);
  628. switch (fh->fh_r_ctl) {
  629. case FC_RCTL_BA_ACC:
  630. break;
  631. case FC_RCTL_BA_RJT:
  632. brp = fc_frame_payload_get(fp, sizeof(*brp));
  633. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  634. break;
  635. /* fall thru */
  636. default:
  637. /*
  638. * we will let the command timeout
  639. * and scsi-ml recover in this case,
  640. * therefore cleared the ba_done flag.
  641. */
  642. ba_done = 0;
  643. }
  644. if (ba_done) {
  645. fsp->state |= FC_SRB_ABORTED;
  646. fsp->state &= ~FC_SRB_ABORT_PENDING;
  647. if (fsp->wait_for_comp)
  648. complete(&fsp->tm_done);
  649. else
  650. fc_fcp_complete_locked(fsp);
  651. }
  652. }
  653. /**
  654. * fc_fcp_recv() - Reveive an FCP frame
  655. * @seq: The sequence the frame is on
  656. * @fp: The received frame
  657. * @arg: The related FCP packet
  658. *
  659. * Context: Called from Soft IRQ context. Can not be called
  660. * holding the FCP packet list lock.
  661. */
  662. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  663. {
  664. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  665. struct fc_lport *lport = fsp->lp;
  666. struct fc_frame_header *fh;
  667. struct fcp_txrdy *dd;
  668. u8 r_ctl;
  669. int rc = 0;
  670. if (IS_ERR(fp)) {
  671. fc_fcp_error(fsp, fp);
  672. return;
  673. }
  674. fh = fc_frame_header_get(fp);
  675. r_ctl = fh->fh_r_ctl;
  676. if (lport->state != LPORT_ST_READY)
  677. goto out;
  678. if (fc_fcp_lock_pkt(fsp))
  679. goto out;
  680. fsp->last_pkt_time = jiffies;
  681. if (fh->fh_type == FC_TYPE_BLS) {
  682. fc_fcp_abts_resp(fsp, fp);
  683. goto unlock;
  684. }
  685. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  686. goto unlock;
  687. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  688. /*
  689. * received XFER RDY from the target
  690. * need to send data to the target
  691. */
  692. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  693. dd = fc_frame_payload_get(fp, sizeof(*dd));
  694. WARN_ON(!dd);
  695. rc = fc_fcp_send_data(fsp, seq,
  696. (size_t) ntohl(dd->ft_data_ro),
  697. (size_t) ntohl(dd->ft_burst_len));
  698. if (!rc)
  699. seq->rec_data = fsp->xfer_len;
  700. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  701. /*
  702. * received a DATA frame
  703. * next we will copy the data to the system buffer
  704. */
  705. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  706. fc_fcp_recv_data(fsp, fp);
  707. seq->rec_data = fsp->xfer_contig_end;
  708. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  709. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  710. fc_fcp_resp(fsp, fp);
  711. } else {
  712. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  713. }
  714. unlock:
  715. fc_fcp_unlock_pkt(fsp);
  716. out:
  717. fc_frame_free(fp);
  718. }
  719. /**
  720. * fc_fcp_resp() - Handler for FCP responses
  721. * @fsp: The FCP packet the response is for
  722. * @fp: The response frame
  723. */
  724. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  725. {
  726. struct fc_frame_header *fh;
  727. struct fcp_resp *fc_rp;
  728. struct fcp_resp_ext *rp_ex;
  729. struct fcp_resp_rsp_info *fc_rp_info;
  730. u32 plen;
  731. u32 expected_len;
  732. u32 respl = 0;
  733. u32 snsl = 0;
  734. u8 flags = 0;
  735. plen = fr_len(fp);
  736. fh = (struct fc_frame_header *)fr_hdr(fp);
  737. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  738. goto len_err;
  739. plen -= sizeof(*fh);
  740. fc_rp = (struct fcp_resp *)(fh + 1);
  741. fsp->cdb_status = fc_rp->fr_status;
  742. flags = fc_rp->fr_flags;
  743. fsp->scsi_comp_flags = flags;
  744. expected_len = fsp->data_len;
  745. /* if ddp, update xfer len */
  746. fc_fcp_ddp_done(fsp);
  747. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  748. rp_ex = (void *)(fc_rp + 1);
  749. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  750. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  751. goto len_err;
  752. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  753. if (flags & FCP_RSP_LEN_VAL) {
  754. respl = ntohl(rp_ex->fr_rsp_len);
  755. if (respl != sizeof(*fc_rp_info))
  756. goto len_err;
  757. if (fsp->wait_for_comp) {
  758. /* Abuse cdb_status for rsp code */
  759. fsp->cdb_status = fc_rp_info->rsp_code;
  760. complete(&fsp->tm_done);
  761. /*
  762. * tmfs will not have any scsi cmd so
  763. * exit here
  764. */
  765. return;
  766. }
  767. }
  768. if (flags & FCP_SNS_LEN_VAL) {
  769. snsl = ntohl(rp_ex->fr_sns_len);
  770. if (snsl > SCSI_SENSE_BUFFERSIZE)
  771. snsl = SCSI_SENSE_BUFFERSIZE;
  772. memcpy(fsp->cmd->sense_buffer,
  773. (char *)fc_rp_info + respl, snsl);
  774. }
  775. }
  776. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  777. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  778. goto len_err;
  779. if (flags & FCP_RESID_UNDER) {
  780. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  781. /*
  782. * The cmnd->underflow is the minimum number of
  783. * bytes that must be transfered for this
  784. * command. Provided a sense condition is not
  785. * present, make sure the actual amount
  786. * transferred is at least the underflow value
  787. * or fail.
  788. */
  789. if (!(flags & FCP_SNS_LEN_VAL) &&
  790. (fc_rp->fr_status == 0) &&
  791. (scsi_bufflen(fsp->cmd) -
  792. fsp->scsi_resid) < fsp->cmd->underflow)
  793. goto err;
  794. expected_len -= fsp->scsi_resid;
  795. } else {
  796. fsp->status_code = FC_ERROR;
  797. }
  798. }
  799. }
  800. fsp->state |= FC_SRB_RCV_STATUS;
  801. /*
  802. * Check for missing or extra data frames.
  803. */
  804. if (unlikely(fsp->xfer_len != expected_len)) {
  805. if (fsp->xfer_len < expected_len) {
  806. /*
  807. * Some data may be queued locally,
  808. * Wait a at least one jiffy to see if it is delivered.
  809. * If this expires without data, we may do SRR.
  810. */
  811. fc_fcp_timer_set(fsp, 2);
  812. return;
  813. }
  814. fsp->status_code = FC_DATA_OVRRUN;
  815. FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx greater than expected, "
  816. "len %x, data len %x\n",
  817. fsp->rport->port_id,
  818. fsp->xfer_len, expected_len, fsp->data_len);
  819. }
  820. fc_fcp_complete_locked(fsp);
  821. return;
  822. len_err:
  823. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  824. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  825. err:
  826. fsp->status_code = FC_ERROR;
  827. fc_fcp_complete_locked(fsp);
  828. }
  829. /**
  830. * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
  831. * fcp_pkt lock held
  832. * @fsp: The FCP packet to be completed
  833. *
  834. * This function may sleep if a timer is pending. The packet lock must be
  835. * held, and the host lock must not be held.
  836. */
  837. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  838. {
  839. struct fc_lport *lport = fsp->lp;
  840. struct fc_seq *seq;
  841. struct fc_exch *ep;
  842. u32 f_ctl;
  843. if (fsp->state & FC_SRB_ABORT_PENDING)
  844. return;
  845. if (fsp->state & FC_SRB_ABORTED) {
  846. if (!fsp->status_code)
  847. fsp->status_code = FC_CMD_ABORTED;
  848. } else {
  849. /*
  850. * Test for transport underrun, independent of response
  851. * underrun status.
  852. */
  853. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  854. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  855. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  856. fsp->status_code = FC_DATA_UNDRUN;
  857. fsp->io_status = 0;
  858. }
  859. }
  860. seq = fsp->seq_ptr;
  861. if (seq) {
  862. fsp->seq_ptr = NULL;
  863. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  864. struct fc_frame *conf_frame;
  865. struct fc_seq *csp;
  866. csp = lport->tt.seq_start_next(seq);
  867. conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
  868. if (conf_frame) {
  869. f_ctl = FC_FC_SEQ_INIT;
  870. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  871. ep = fc_seq_exch(seq);
  872. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  873. ep->did, ep->sid,
  874. FC_TYPE_FCP, f_ctl, 0);
  875. lport->tt.seq_send(lport, csp, conf_frame);
  876. }
  877. }
  878. lport->tt.exch_done(seq);
  879. }
  880. /*
  881. * Some resets driven by SCSI are not I/Os and do not have
  882. * SCSI commands associated with the requests. We should not
  883. * call I/O completion if we do not have a SCSI command.
  884. */
  885. if (fsp->cmd)
  886. fc_io_compl(fsp);
  887. }
  888. /**
  889. * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
  890. * @fsp: The FCP packet whose exchanges should be canceled
  891. * @error: The reason for the cancellation
  892. */
  893. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  894. {
  895. struct fc_lport *lport = fsp->lp;
  896. if (fsp->seq_ptr) {
  897. lport->tt.exch_done(fsp->seq_ptr);
  898. fsp->seq_ptr = NULL;
  899. }
  900. fsp->status_code = error;
  901. }
  902. /**
  903. * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
  904. * @lport: The local port whose exchanges should be canceled
  905. * @id: The target's ID
  906. * @lun: The LUN
  907. * @error: The reason for cancellation
  908. *
  909. * If lun or id is -1, they are ignored.
  910. */
  911. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
  912. unsigned int lun, int error)
  913. {
  914. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  915. struct fc_fcp_pkt *fsp;
  916. struct scsi_cmnd *sc_cmd;
  917. unsigned long flags;
  918. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  919. restart:
  920. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  921. sc_cmd = fsp->cmd;
  922. if (id != -1 && scmd_id(sc_cmd) != id)
  923. continue;
  924. if (lun != -1 && sc_cmd->device->lun != lun)
  925. continue;
  926. fc_fcp_pkt_hold(fsp);
  927. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  928. if (!fc_fcp_lock_pkt(fsp)) {
  929. fc_fcp_cleanup_cmd(fsp, error);
  930. fc_io_compl(fsp);
  931. fc_fcp_unlock_pkt(fsp);
  932. }
  933. fc_fcp_pkt_release(fsp);
  934. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  935. /*
  936. * while we dropped the lock multiple pkts could
  937. * have been released, so we have to start over.
  938. */
  939. goto restart;
  940. }
  941. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  942. }
  943. /**
  944. * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
  945. * @lport: The local port whose exchanges are to be aborted
  946. */
  947. static void fc_fcp_abort_io(struct fc_lport *lport)
  948. {
  949. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
  950. }
  951. /**
  952. * fc_fcp_pkt_send() - Send a fcp_pkt
  953. * @lport: The local port to send the FCP packet on
  954. * @fsp: The FCP packet to send
  955. *
  956. * Return: Zero for success and -1 for failure
  957. * Locks: Called without locks held
  958. */
  959. static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
  960. {
  961. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  962. unsigned long flags;
  963. int rc;
  964. fsp->cmd->SCp.ptr = (char *)fsp;
  965. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  966. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  967. int_to_scsilun(fsp->cmd->device->lun,
  968. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  969. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  970. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  971. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  972. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  973. rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
  974. if (unlikely(rc)) {
  975. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  976. list_del(&fsp->list);
  977. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  978. }
  979. return rc;
  980. }
  981. /**
  982. * get_fsp_rec_tov() - Helper function to get REC_TOV
  983. * @fsp: the FCP packet
  984. */
  985. static inline unsigned int get_fsp_rec_tov(struct fc_fcp_pkt *fsp)
  986. {
  987. struct fc_rport *rport;
  988. struct fc_rport_libfc_priv *rpriv;
  989. rport = fsp->rport;
  990. rpriv = rport->dd_data;
  991. return rpriv->e_d_tov + REC_TOV_CONST;
  992. }
  993. /**
  994. * fc_fcp_cmd_send() - Send a FCP command
  995. * @lport: The local port to send the command on
  996. * @fsp: The FCP packet the command is on
  997. * @resp: The handler for the response
  998. */
  999. static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1000. void (*resp)(struct fc_seq *,
  1001. struct fc_frame *fp,
  1002. void *arg))
  1003. {
  1004. struct fc_frame *fp;
  1005. struct fc_seq *seq;
  1006. struct fc_rport *rport;
  1007. struct fc_rport_libfc_priv *rpriv;
  1008. const size_t len = sizeof(fsp->cdb_cmd);
  1009. int rc = 0;
  1010. unsigned int rec_tov;
  1011. if (fc_fcp_lock_pkt(fsp))
  1012. return 0;
  1013. fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
  1014. if (!fp) {
  1015. rc = -1;
  1016. goto unlock;
  1017. }
  1018. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  1019. fr_fsp(fp) = fsp;
  1020. rport = fsp->rport;
  1021. fsp->max_payload = rport->maxframe_size;
  1022. rpriv = rport->dd_data;
  1023. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  1024. rpriv->local_port->port_id, FC_TYPE_FCP,
  1025. FC_FCTL_REQ, 0);
  1026. seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
  1027. fsp, 0);
  1028. if (!seq) {
  1029. rc = -1;
  1030. goto unlock;
  1031. }
  1032. fsp->last_pkt_time = jiffies;
  1033. fsp->seq_ptr = seq;
  1034. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  1035. rec_tov = get_fsp_rec_tov(fsp);
  1036. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  1037. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1038. fc_fcp_timer_set(fsp, rec_tov);
  1039. unlock:
  1040. fc_fcp_unlock_pkt(fsp);
  1041. return rc;
  1042. }
  1043. /**
  1044. * fc_fcp_error() - Handler for FCP layer errors
  1045. * @fsp: The FCP packet the error is on
  1046. * @fp: The frame that has errored
  1047. */
  1048. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1049. {
  1050. int error = PTR_ERR(fp);
  1051. if (fc_fcp_lock_pkt(fsp))
  1052. return;
  1053. if (error == -FC_EX_CLOSED) {
  1054. fc_fcp_retry_cmd(fsp);
  1055. goto unlock;
  1056. }
  1057. /*
  1058. * clear abort pending, because the lower layer
  1059. * decided to force completion.
  1060. */
  1061. fsp->state &= ~FC_SRB_ABORT_PENDING;
  1062. fsp->status_code = FC_CMD_PLOGO;
  1063. fc_fcp_complete_locked(fsp);
  1064. unlock:
  1065. fc_fcp_unlock_pkt(fsp);
  1066. }
  1067. /**
  1068. * fc_fcp_pkt_abort() - Abort a fcp_pkt
  1069. * @fsp: The FCP packet to abort on
  1070. *
  1071. * Called to send an abort and then wait for abort completion
  1072. */
  1073. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  1074. {
  1075. int rc = FAILED;
  1076. unsigned long ticks_left;
  1077. if (fc_fcp_send_abort(fsp))
  1078. return FAILED;
  1079. init_completion(&fsp->tm_done);
  1080. fsp->wait_for_comp = 1;
  1081. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1082. ticks_left = wait_for_completion_timeout(&fsp->tm_done,
  1083. FC_SCSI_TM_TOV);
  1084. spin_lock_bh(&fsp->scsi_pkt_lock);
  1085. fsp->wait_for_comp = 0;
  1086. if (!ticks_left) {
  1087. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  1088. } else if (fsp->state & FC_SRB_ABORTED) {
  1089. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  1090. rc = SUCCESS;
  1091. fc_fcp_complete_locked(fsp);
  1092. }
  1093. return rc;
  1094. }
  1095. /**
  1096. * fc_lun_reset_send() - Send LUN reset command
  1097. * @data: The FCP packet that identifies the LUN to be reset
  1098. */
  1099. static void fc_lun_reset_send(unsigned long data)
  1100. {
  1101. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1102. struct fc_lport *lport = fsp->lp;
  1103. unsigned int rec_tov;
  1104. if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
  1105. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1106. return;
  1107. if (fc_fcp_lock_pkt(fsp))
  1108. return;
  1109. rec_tov = get_fsp_rec_tov(fsp);
  1110. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1111. fc_fcp_timer_set(fsp, rec_tov);
  1112. fc_fcp_unlock_pkt(fsp);
  1113. }
  1114. }
  1115. /**
  1116. * fc_lun_reset() - Send a LUN RESET command to a device
  1117. * and wait for the reply
  1118. * @lport: The local port to sent the command on
  1119. * @fsp: The FCP packet that identifies the LUN to be reset
  1120. * @id: The SCSI command ID
  1121. * @lun: The LUN ID to be reset
  1122. */
  1123. static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1124. unsigned int id, unsigned int lun)
  1125. {
  1126. int rc;
  1127. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1128. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1129. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  1130. fsp->wait_for_comp = 1;
  1131. init_completion(&fsp->tm_done);
  1132. fc_lun_reset_send((unsigned long)fsp);
  1133. /*
  1134. * wait for completion of reset
  1135. * after that make sure all commands are terminated
  1136. */
  1137. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1138. spin_lock_bh(&fsp->scsi_pkt_lock);
  1139. fsp->state |= FC_SRB_COMPL;
  1140. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1141. del_timer_sync(&fsp->timer);
  1142. spin_lock_bh(&fsp->scsi_pkt_lock);
  1143. if (fsp->seq_ptr) {
  1144. lport->tt.exch_done(fsp->seq_ptr);
  1145. fsp->seq_ptr = NULL;
  1146. }
  1147. fsp->wait_for_comp = 0;
  1148. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1149. if (!rc) {
  1150. FC_SCSI_DBG(lport, "lun reset failed\n");
  1151. return FAILED;
  1152. }
  1153. /* cdb_status holds the tmf's rsp code */
  1154. if (fsp->cdb_status != FCP_TMF_CMPL)
  1155. return FAILED;
  1156. FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
  1157. fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
  1158. return SUCCESS;
  1159. }
  1160. /**
  1161. * fc_tm_done() - Task Managment response handler
  1162. * @seq: The sequence that the response is on
  1163. * @fp: The response frame
  1164. * @arg: The FCP packet the response is for
  1165. */
  1166. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1167. {
  1168. struct fc_fcp_pkt *fsp = arg;
  1169. struct fc_frame_header *fh;
  1170. if (IS_ERR(fp)) {
  1171. /*
  1172. * If there is an error just let it timeout or wait
  1173. * for TMF to be aborted if it timedout.
  1174. *
  1175. * scsi-eh will escalate for when either happens.
  1176. */
  1177. return;
  1178. }
  1179. if (fc_fcp_lock_pkt(fsp))
  1180. goto out;
  1181. /*
  1182. * raced with eh timeout handler.
  1183. */
  1184. if (!fsp->seq_ptr || !fsp->wait_for_comp)
  1185. goto out_unlock;
  1186. fh = fc_frame_header_get(fp);
  1187. if (fh->fh_type != FC_TYPE_BLS)
  1188. fc_fcp_resp(fsp, fp);
  1189. fsp->seq_ptr = NULL;
  1190. fsp->lp->tt.exch_done(seq);
  1191. out_unlock:
  1192. fc_fcp_unlock_pkt(fsp);
  1193. out:
  1194. fc_frame_free(fp);
  1195. }
  1196. /**
  1197. * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
  1198. * @lport: The local port to be cleaned up
  1199. */
  1200. static void fc_fcp_cleanup(struct fc_lport *lport)
  1201. {
  1202. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
  1203. }
  1204. /**
  1205. * fc_fcp_timeout() - Handler for fcp_pkt timeouts
  1206. * @data: The FCP packet that has timed out
  1207. *
  1208. * If REC is supported then just issue it and return. The REC exchange will
  1209. * complete or time out and recovery can continue at that point. Otherwise,
  1210. * if the response has been received without all the data it has been
  1211. * ER_TIMEOUT since the response was received. If the response has not been
  1212. * received we see if data was received recently. If it has been then we
  1213. * continue waiting, otherwise, we abort the command.
  1214. */
  1215. static void fc_fcp_timeout(unsigned long data)
  1216. {
  1217. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1218. struct fc_rport *rport = fsp->rport;
  1219. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1220. if (fc_fcp_lock_pkt(fsp))
  1221. return;
  1222. if (fsp->cdb_cmd.fc_tm_flags)
  1223. goto unlock;
  1224. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1225. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1226. fc_fcp_rec(fsp);
  1227. else if (fsp->state & FC_SRB_RCV_STATUS)
  1228. fc_fcp_complete_locked(fsp);
  1229. else
  1230. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1231. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1232. unlock:
  1233. fc_fcp_unlock_pkt(fsp);
  1234. }
  1235. /**
  1236. * fc_fcp_rec() - Send a REC ELS request
  1237. * @fsp: The FCP packet to send the REC request on
  1238. */
  1239. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1240. {
  1241. struct fc_lport *lport;
  1242. struct fc_frame *fp;
  1243. struct fc_rport *rport;
  1244. struct fc_rport_libfc_priv *rpriv;
  1245. lport = fsp->lp;
  1246. rport = fsp->rport;
  1247. rpriv = rport->dd_data;
  1248. if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
  1249. fsp->status_code = FC_HRD_ERROR;
  1250. fsp->io_status = 0;
  1251. fc_fcp_complete_locked(fsp);
  1252. return;
  1253. }
  1254. fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
  1255. if (!fp)
  1256. goto retry;
  1257. fr_seq(fp) = fsp->seq_ptr;
  1258. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1259. rpriv->local_port->port_id, FC_TYPE_ELS,
  1260. FC_FCTL_REQ, 0);
  1261. if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
  1262. fc_fcp_rec_resp, fsp,
  1263. 2 * lport->r_a_tov)) {
  1264. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1265. return;
  1266. }
  1267. retry:
  1268. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1269. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1270. else
  1271. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1272. }
  1273. /**
  1274. * fc_fcp_rec_resp() - Handler for REC ELS responses
  1275. * @seq: The sequence the response is on
  1276. * @fp: The response frame
  1277. * @arg: The FCP packet the response is on
  1278. *
  1279. * If the response is a reject then the scsi layer will handle
  1280. * the timeout. If the response is a LS_ACC then if the I/O was not completed
  1281. * set the timeout and return. If the I/O was completed then complete the
  1282. * exchange and tell the SCSI layer.
  1283. */
  1284. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1285. {
  1286. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1287. struct fc_els_rec_acc *recp;
  1288. struct fc_els_ls_rjt *rjt;
  1289. u32 e_stat;
  1290. u8 opcode;
  1291. u32 offset;
  1292. enum dma_data_direction data_dir;
  1293. enum fc_rctl r_ctl;
  1294. struct fc_rport_libfc_priv *rpriv;
  1295. if (IS_ERR(fp)) {
  1296. fc_fcp_rec_error(fsp, fp);
  1297. return;
  1298. }
  1299. if (fc_fcp_lock_pkt(fsp))
  1300. goto out;
  1301. fsp->recov_retry = 0;
  1302. opcode = fc_frame_payload_op(fp);
  1303. if (opcode == ELS_LS_RJT) {
  1304. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1305. switch (rjt->er_reason) {
  1306. default:
  1307. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1308. "reason %d expl %d\n",
  1309. fsp->rport->port_id, rjt->er_reason,
  1310. rjt->er_explan);
  1311. /* fall through */
  1312. case ELS_RJT_UNSUP:
  1313. FC_FCP_DBG(fsp, "device does not support REC\n");
  1314. rpriv = fsp->rport->dd_data;
  1315. /*
  1316. * if we do not spport RECs or got some bogus
  1317. * reason then resetup timer so we check for
  1318. * making progress.
  1319. */
  1320. rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1321. break;
  1322. case ELS_RJT_LOGIC:
  1323. case ELS_RJT_UNAB:
  1324. /*
  1325. * If no data transfer, the command frame got dropped
  1326. * so we just retry. If data was transferred, we
  1327. * lost the response but the target has no record,
  1328. * so we abort and retry.
  1329. */
  1330. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1331. fsp->xfer_len == 0) {
  1332. fc_fcp_retry_cmd(fsp);
  1333. break;
  1334. }
  1335. fc_fcp_recovery(fsp, FC_ERROR);
  1336. break;
  1337. }
  1338. } else if (opcode == ELS_LS_ACC) {
  1339. if (fsp->state & FC_SRB_ABORTED)
  1340. goto unlock_out;
  1341. data_dir = fsp->cmd->sc_data_direction;
  1342. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1343. offset = ntohl(recp->reca_fc4value);
  1344. e_stat = ntohl(recp->reca_e_stat);
  1345. if (e_stat & ESB_ST_COMPLETE) {
  1346. /*
  1347. * The exchange is complete.
  1348. *
  1349. * For output, we must've lost the response.
  1350. * For input, all data must've been sent.
  1351. * We lost may have lost the response
  1352. * (and a confirmation was requested) and maybe
  1353. * some data.
  1354. *
  1355. * If all data received, send SRR
  1356. * asking for response. If partial data received,
  1357. * or gaps, SRR requests data at start of gap.
  1358. * Recovery via SRR relies on in-order-delivery.
  1359. */
  1360. if (data_dir == DMA_TO_DEVICE) {
  1361. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1362. } else if (fsp->xfer_contig_end == offset) {
  1363. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1364. } else {
  1365. offset = fsp->xfer_contig_end;
  1366. r_ctl = FC_RCTL_DD_SOL_DATA;
  1367. }
  1368. fc_fcp_srr(fsp, r_ctl, offset);
  1369. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1370. unsigned int rec_tov = get_fsp_rec_tov(fsp);
  1371. /*
  1372. * The remote port has the initiative, so just
  1373. * keep waiting for it to complete.
  1374. */
  1375. fc_fcp_timer_set(fsp, rec_tov);
  1376. } else {
  1377. /*
  1378. * The exchange is incomplete, we have seq. initiative.
  1379. * Lost response with requested confirmation,
  1380. * lost confirmation, lost transfer ready or
  1381. * lost write data.
  1382. *
  1383. * For output, if not all data was received, ask
  1384. * for transfer ready to be repeated.
  1385. *
  1386. * If we received or sent all the data, send SRR to
  1387. * request response.
  1388. *
  1389. * If we lost a response, we may have lost some read
  1390. * data as well.
  1391. */
  1392. r_ctl = FC_RCTL_DD_SOL_DATA;
  1393. if (data_dir == DMA_TO_DEVICE) {
  1394. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1395. if (offset < fsp->data_len)
  1396. r_ctl = FC_RCTL_DD_DATA_DESC;
  1397. } else if (offset == fsp->xfer_contig_end) {
  1398. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1399. } else if (fsp->xfer_contig_end < offset) {
  1400. offset = fsp->xfer_contig_end;
  1401. }
  1402. fc_fcp_srr(fsp, r_ctl, offset);
  1403. }
  1404. }
  1405. unlock_out:
  1406. fc_fcp_unlock_pkt(fsp);
  1407. out:
  1408. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1409. fc_frame_free(fp);
  1410. }
  1411. /**
  1412. * fc_fcp_rec_error() - Handler for REC errors
  1413. * @fsp: The FCP packet the error is on
  1414. * @fp: The REC frame
  1415. */
  1416. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1417. {
  1418. int error = PTR_ERR(fp);
  1419. if (fc_fcp_lock_pkt(fsp))
  1420. goto out;
  1421. switch (error) {
  1422. case -FC_EX_CLOSED:
  1423. fc_fcp_retry_cmd(fsp);
  1424. break;
  1425. default:
  1426. FC_FCP_DBG(fsp, "REC %p fid %6.6x error unexpected error %d\n",
  1427. fsp, fsp->rport->port_id, error);
  1428. fsp->status_code = FC_CMD_PLOGO;
  1429. /* fall through */
  1430. case -FC_EX_TIMEOUT:
  1431. /*
  1432. * Assume REC or LS_ACC was lost.
  1433. * The exchange manager will have aborted REC, so retry.
  1434. */
  1435. FC_FCP_DBG(fsp, "REC fid %6.6x error error %d retry %d/%d\n",
  1436. fsp->rport->port_id, error, fsp->recov_retry,
  1437. FC_MAX_RECOV_RETRY);
  1438. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1439. fc_fcp_rec(fsp);
  1440. else
  1441. fc_fcp_recovery(fsp, FC_ERROR);
  1442. break;
  1443. }
  1444. fc_fcp_unlock_pkt(fsp);
  1445. out:
  1446. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1447. }
  1448. /**
  1449. * fc_fcp_recovery() - Handler for fcp_pkt recovery
  1450. * @fsp: The FCP pkt that needs to be aborted
  1451. */
  1452. static void fc_fcp_recovery(struct fc_fcp_pkt *fsp, u8 code)
  1453. {
  1454. fsp->status_code = code;
  1455. fsp->cdb_status = 0;
  1456. fsp->io_status = 0;
  1457. /*
  1458. * if this fails then we let the scsi command timer fire and
  1459. * scsi-ml escalate.
  1460. */
  1461. fc_fcp_send_abort(fsp);
  1462. }
  1463. /**
  1464. * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
  1465. * @fsp: The FCP packet the SRR is to be sent on
  1466. * @r_ctl: The R_CTL field for the SRR request
  1467. * This is called after receiving status but insufficient data, or
  1468. * when expecting status but the request has timed out.
  1469. */
  1470. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1471. {
  1472. struct fc_lport *lport = fsp->lp;
  1473. struct fc_rport *rport;
  1474. struct fc_rport_libfc_priv *rpriv;
  1475. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1476. struct fc_seq *seq;
  1477. struct fcp_srr *srr;
  1478. struct fc_frame *fp;
  1479. u8 cdb_op;
  1480. unsigned int rec_tov;
  1481. rport = fsp->rport;
  1482. rpriv = rport->dd_data;
  1483. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1484. if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
  1485. rpriv->rp_state != RPORT_ST_READY)
  1486. goto retry; /* shouldn't happen */
  1487. fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
  1488. if (!fp)
  1489. goto retry;
  1490. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1491. memset(srr, 0, sizeof(*srr));
  1492. srr->srr_op = ELS_SRR;
  1493. srr->srr_ox_id = htons(ep->oxid);
  1494. srr->srr_rx_id = htons(ep->rxid);
  1495. srr->srr_r_ctl = r_ctl;
  1496. srr->srr_rel_off = htonl(offset);
  1497. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1498. rpriv->local_port->port_id, FC_TYPE_FCP,
  1499. FC_FCTL_REQ, 0);
  1500. rec_tov = get_fsp_rec_tov(fsp);
  1501. seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
  1502. fsp, jiffies_to_msecs(rec_tov));
  1503. if (!seq)
  1504. goto retry;
  1505. fsp->recov_seq = seq;
  1506. fsp->xfer_len = offset;
  1507. fsp->xfer_contig_end = offset;
  1508. fsp->state &= ~FC_SRB_RCV_STATUS;
  1509. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1510. return;
  1511. retry:
  1512. fc_fcp_retry_cmd(fsp);
  1513. }
  1514. /**
  1515. * fc_fcp_srr_resp() - Handler for SRR response
  1516. * @seq: The sequence the SRR is on
  1517. * @fp: The SRR frame
  1518. * @arg: The FCP packet the SRR is on
  1519. */
  1520. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1521. {
  1522. struct fc_fcp_pkt *fsp = arg;
  1523. struct fc_frame_header *fh;
  1524. unsigned int rec_tov;
  1525. if (IS_ERR(fp)) {
  1526. fc_fcp_srr_error(fsp, fp);
  1527. return;
  1528. }
  1529. if (fc_fcp_lock_pkt(fsp))
  1530. goto out;
  1531. fh = fc_frame_header_get(fp);
  1532. /*
  1533. * BUG? fc_fcp_srr_error calls exch_done which would release
  1534. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1535. * then fc_exch_timeout would be sending an abort. The exch_done
  1536. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1537. * an abort response though.
  1538. */
  1539. if (fh->fh_type == FC_TYPE_BLS) {
  1540. fc_fcp_unlock_pkt(fsp);
  1541. return;
  1542. }
  1543. fsp->recov_seq = NULL;
  1544. switch (fc_frame_payload_op(fp)) {
  1545. case ELS_LS_ACC:
  1546. fsp->recov_retry = 0;
  1547. rec_tov = get_fsp_rec_tov(fsp);
  1548. fc_fcp_timer_set(fsp, rec_tov);
  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. fsp->lp->tt.exch_done(seq);
  1557. out:
  1558. fc_frame_free(fp);
  1559. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1560. }
  1561. /**
  1562. * fc_fcp_srr_error() - Handler for SRR errors
  1563. * @fsp: The FCP packet that the SRR error is on
  1564. * @fp: The SRR frame
  1565. */
  1566. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1567. {
  1568. if (fc_fcp_lock_pkt(fsp))
  1569. goto out;
  1570. fsp->lp->tt.exch_done(fsp->recov_seq);
  1571. fsp->recov_seq = NULL;
  1572. switch (PTR_ERR(fp)) {
  1573. case -FC_EX_TIMEOUT:
  1574. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1575. fc_fcp_rec(fsp);
  1576. else
  1577. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1578. break;
  1579. case -FC_EX_CLOSED: /* e.g., link failure */
  1580. /* fall through */
  1581. default:
  1582. fc_fcp_retry_cmd(fsp);
  1583. break;
  1584. }
  1585. fc_fcp_unlock_pkt(fsp);
  1586. out:
  1587. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1588. }
  1589. /**
  1590. * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
  1591. * @lport: The local port to be checked
  1592. */
  1593. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
  1594. {
  1595. /* lock ? */
  1596. return (lport->state == LPORT_ST_READY) &&
  1597. lport->link_up && !lport->qfull;
  1598. }
  1599. /**
  1600. * fc_queuecommand() - The queuecommand function of the SCSI template
  1601. * @shost: The Scsi_Host that the command was issued to
  1602. * @cmd: The scsi_cmnd to be executed
  1603. *
  1604. * This is the i/o strategy routine, called by the SCSI layer.
  1605. */
  1606. int fc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc_cmd)
  1607. {
  1608. struct fc_lport *lport = shost_priv(shost);
  1609. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1610. struct fc_fcp_pkt *fsp;
  1611. struct fc_rport_libfc_priv *rpriv;
  1612. int rval;
  1613. int rc = 0;
  1614. struct fcoe_dev_stats *stats;
  1615. rval = fc_remote_port_chkready(rport);
  1616. if (rval) {
  1617. sc_cmd->result = rval;
  1618. sc_cmd->scsi_done(sc_cmd);
  1619. return 0;
  1620. }
  1621. if (!*(struct fc_remote_port **)rport->dd_data) {
  1622. /*
  1623. * rport is transitioning from blocked/deleted to
  1624. * online
  1625. */
  1626. sc_cmd->result = DID_IMM_RETRY << 16;
  1627. sc_cmd->scsi_done(sc_cmd);
  1628. goto out;
  1629. }
  1630. rpriv = rport->dd_data;
  1631. if (!fc_fcp_lport_queue_ready(lport)) {
  1632. if (lport->qfull)
  1633. fc_fcp_can_queue_ramp_down(lport);
  1634. rc = SCSI_MLQUEUE_HOST_BUSY;
  1635. goto out;
  1636. }
  1637. fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
  1638. if (fsp == NULL) {
  1639. rc = SCSI_MLQUEUE_HOST_BUSY;
  1640. goto out;
  1641. }
  1642. /*
  1643. * build the libfc request pkt
  1644. */
  1645. fsp->cmd = sc_cmd; /* save the cmd */
  1646. fsp->rport = rport; /* set the remote port ptr */
  1647. /*
  1648. * set up the transfer length
  1649. */
  1650. fsp->data_len = scsi_bufflen(sc_cmd);
  1651. fsp->xfer_len = 0;
  1652. /*
  1653. * setup the data direction
  1654. */
  1655. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1656. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1657. fsp->req_flags = FC_SRB_READ;
  1658. stats->InputRequests++;
  1659. stats->InputBytes += fsp->data_len;
  1660. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1661. fsp->req_flags = FC_SRB_WRITE;
  1662. stats->OutputRequests++;
  1663. stats->OutputBytes += fsp->data_len;
  1664. } else {
  1665. fsp->req_flags = 0;
  1666. stats->ControlRequests++;
  1667. }
  1668. put_cpu();
  1669. init_timer(&fsp->timer);
  1670. fsp->timer.data = (unsigned long)fsp;
  1671. /*
  1672. * send it to the lower layer
  1673. * if we get -1 return then put the request in the pending
  1674. * queue.
  1675. */
  1676. rval = fc_fcp_pkt_send(lport, fsp);
  1677. if (rval != 0) {
  1678. fsp->state = FC_SRB_FREE;
  1679. fc_fcp_pkt_release(fsp);
  1680. rc = SCSI_MLQUEUE_HOST_BUSY;
  1681. }
  1682. out:
  1683. return rc;
  1684. }
  1685. EXPORT_SYMBOL(fc_queuecommand);
  1686. /**
  1687. * fc_io_compl() - Handle responses for completed commands
  1688. * @fsp: The FCP packet that is complete
  1689. *
  1690. * Translates fcp_pkt errors to a Linux SCSI errors.
  1691. * The fcp packet lock must be held when calling.
  1692. */
  1693. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1694. {
  1695. struct fc_fcp_internal *si;
  1696. struct scsi_cmnd *sc_cmd;
  1697. struct fc_lport *lport;
  1698. unsigned long flags;
  1699. /* release outstanding ddp context */
  1700. fc_fcp_ddp_done(fsp);
  1701. fsp->state |= FC_SRB_COMPL;
  1702. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1703. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1704. del_timer_sync(&fsp->timer);
  1705. spin_lock_bh(&fsp->scsi_pkt_lock);
  1706. }
  1707. lport = fsp->lp;
  1708. si = fc_get_scsi_internal(lport);
  1709. /*
  1710. * if can_queue ramp down is done then try can_queue ramp up
  1711. * since commands are completing now.
  1712. */
  1713. if (si->last_can_queue_ramp_down_time)
  1714. fc_fcp_can_queue_ramp_up(lport);
  1715. sc_cmd = fsp->cmd;
  1716. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1717. switch (fsp->status_code) {
  1718. case FC_COMPLETE:
  1719. if (fsp->cdb_status == 0) {
  1720. /*
  1721. * good I/O status
  1722. */
  1723. sc_cmd->result = DID_OK << 16;
  1724. if (fsp->scsi_resid)
  1725. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1726. } else {
  1727. /*
  1728. * transport level I/O was ok but scsi
  1729. * has non zero status
  1730. */
  1731. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1732. }
  1733. break;
  1734. case FC_ERROR:
  1735. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1736. "due to FC_ERROR\n");
  1737. sc_cmd->result = DID_ERROR << 16;
  1738. break;
  1739. case FC_DATA_UNDRUN:
  1740. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1741. /*
  1742. * scsi status is good but transport level
  1743. * underrun.
  1744. */
  1745. if (fsp->state & FC_SRB_RCV_STATUS) {
  1746. sc_cmd->result = DID_OK << 16;
  1747. } else {
  1748. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml"
  1749. " due to FC_DATA_UNDRUN (trans)\n");
  1750. sc_cmd->result = DID_ERROR << 16;
  1751. }
  1752. } else {
  1753. /*
  1754. * scsi got underrun, this is an error
  1755. */
  1756. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1757. "due to FC_DATA_UNDRUN (scsi)\n");
  1758. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1759. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1760. }
  1761. break;
  1762. case FC_DATA_OVRRUN:
  1763. /*
  1764. * overrun is an error
  1765. */
  1766. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1767. "due to FC_DATA_OVRRUN\n");
  1768. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1769. break;
  1770. case FC_CMD_ABORTED:
  1771. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1772. "due to FC_CMD_ABORTED\n");
  1773. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1774. break;
  1775. case FC_CMD_RESET:
  1776. FC_FCP_DBG(fsp, "Returning DID_RESET to scsi-ml "
  1777. "due to FC_CMD_RESET\n");
  1778. sc_cmd->result = (DID_RESET << 16);
  1779. break;
  1780. case FC_HRD_ERROR:
  1781. FC_FCP_DBG(fsp, "Returning DID_NO_CONNECT to scsi-ml "
  1782. "due to FC_HRD_ERROR\n");
  1783. sc_cmd->result = (DID_NO_CONNECT << 16);
  1784. break;
  1785. case FC_CRC_ERROR:
  1786. FC_FCP_DBG(fsp, "Returning DID_PARITY to scsi-ml "
  1787. "due to FC_CRC_ERROR\n");
  1788. sc_cmd->result = (DID_PARITY << 16);
  1789. break;
  1790. case FC_TIMED_OUT:
  1791. FC_FCP_DBG(fsp, "Returning DID_BUS_BUSY to scsi-ml "
  1792. "due to FC_TIMED_OUT\n");
  1793. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1794. break;
  1795. default:
  1796. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1797. "due to unknown error\n");
  1798. sc_cmd->result = (DID_ERROR << 16);
  1799. break;
  1800. }
  1801. if (lport->state != LPORT_ST_READY && fsp->status_code != FC_COMPLETE)
  1802. sc_cmd->result = (DID_TRANSPORT_DISRUPTED << 16);
  1803. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1804. list_del(&fsp->list);
  1805. sc_cmd->SCp.ptr = NULL;
  1806. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1807. sc_cmd->scsi_done(sc_cmd);
  1808. /* release ref from initial allocation in queue command */
  1809. fc_fcp_pkt_release(fsp);
  1810. }
  1811. /**
  1812. * fc_eh_abort() - Abort a command
  1813. * @sc_cmd: The SCSI command to abort
  1814. *
  1815. * From SCSI host template.
  1816. * Send an ABTS to the target device and wait for the response.
  1817. */
  1818. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1819. {
  1820. struct fc_fcp_pkt *fsp;
  1821. struct fc_lport *lport;
  1822. struct fc_fcp_internal *si;
  1823. int rc = FAILED;
  1824. unsigned long flags;
  1825. lport = shost_priv(sc_cmd->device->host);
  1826. if (lport->state != LPORT_ST_READY)
  1827. return rc;
  1828. else if (!lport->link_up)
  1829. return rc;
  1830. si = fc_get_scsi_internal(lport);
  1831. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1832. fsp = CMD_SP(sc_cmd);
  1833. if (!fsp) {
  1834. /* command completed while scsi eh was setting up */
  1835. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1836. return SUCCESS;
  1837. }
  1838. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1839. fc_fcp_pkt_hold(fsp);
  1840. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1841. if (fc_fcp_lock_pkt(fsp)) {
  1842. /* completed while we were waiting for timer to be deleted */
  1843. rc = SUCCESS;
  1844. goto release_pkt;
  1845. }
  1846. rc = fc_fcp_pkt_abort(fsp);
  1847. fc_fcp_unlock_pkt(fsp);
  1848. release_pkt:
  1849. fc_fcp_pkt_release(fsp);
  1850. return rc;
  1851. }
  1852. EXPORT_SYMBOL(fc_eh_abort);
  1853. /**
  1854. * fc_eh_device_reset() - Reset a single LUN
  1855. * @sc_cmd: The SCSI command which identifies the device whose
  1856. * LUN is to be reset
  1857. *
  1858. * Set from SCSI host template.
  1859. */
  1860. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1861. {
  1862. struct fc_lport *lport;
  1863. struct fc_fcp_pkt *fsp;
  1864. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1865. int rc = FAILED;
  1866. int rval;
  1867. rval = fc_remote_port_chkready(rport);
  1868. if (rval)
  1869. goto out;
  1870. lport = shost_priv(sc_cmd->device->host);
  1871. if (lport->state != LPORT_ST_READY)
  1872. return rc;
  1873. FC_SCSI_DBG(lport, "Resetting rport (%6.6x)\n", rport->port_id);
  1874. fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
  1875. if (fsp == NULL) {
  1876. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1877. goto out;
  1878. }
  1879. /*
  1880. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1881. * the sc passed in is not setup for execution like when sent
  1882. * through the queuecommand callout.
  1883. */
  1884. fsp->rport = rport; /* set the remote port ptr */
  1885. /*
  1886. * flush outstanding commands
  1887. */
  1888. rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1889. fsp->state = FC_SRB_FREE;
  1890. fc_fcp_pkt_release(fsp);
  1891. out:
  1892. return rc;
  1893. }
  1894. EXPORT_SYMBOL(fc_eh_device_reset);
  1895. /**
  1896. * fc_eh_host_reset() - Reset a Scsi_Host.
  1897. * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
  1898. */
  1899. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1900. {
  1901. struct Scsi_Host *shost = sc_cmd->device->host;
  1902. struct fc_lport *lport = shost_priv(shost);
  1903. unsigned long wait_tmo;
  1904. FC_SCSI_DBG(lport, "Resetting host\n");
  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);