fc_fcp.c 61 KB

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