fc_fcp.c 60 KB

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