libiscsi.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <linux/log2.h>
  28. #include <asm/unaligned.h>
  29. #include <net/tcp.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_host.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/iscsi_proto.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_iscsi.h>
  39. #include <scsi/libiscsi.h>
  40. static int iscsi_dbg_lib;
  41. module_param_named(debug_libiscsi, iscsi_dbg_lib, int, S_IRUGO | S_IWUSR);
  42. MODULE_PARM_DESC(debug_libiscsi, "Turn on debugging for libiscsi module. "
  43. "Set to 1 to turn on, and zero to turn off. Default "
  44. "is off.");
  45. #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
  46. do { \
  47. if (iscsi_dbg_lib) \
  48. iscsi_conn_printk(KERN_INFO, _conn, \
  49. "%s " dbg_fmt, \
  50. __func__, ##arg); \
  51. } while (0);
  52. #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
  53. do { \
  54. if (iscsi_dbg_lib) \
  55. iscsi_session_printk(KERN_INFO, _session, \
  56. "%s " dbg_fmt, \
  57. __func__, ##arg); \
  58. } while (0);
  59. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  60. #define SNA32_CHECK 2147483648UL
  61. static int iscsi_sna_lt(u32 n1, u32 n2)
  62. {
  63. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  64. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  65. }
  66. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  67. static int iscsi_sna_lte(u32 n1, u32 n2)
  68. {
  69. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  70. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  71. }
  72. inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
  73. {
  74. struct Scsi_Host *shost = conn->session->host;
  75. struct iscsi_host *ihost = shost_priv(shost);
  76. if (ihost->workq)
  77. queue_work(ihost->workq, &conn->xmitwork);
  78. }
  79. EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
  80. void
  81. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  82. {
  83. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  84. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  85. /*
  86. * standard specifies this check for when to update expected and
  87. * max sequence numbers
  88. */
  89. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  90. return;
  91. if (exp_cmdsn != session->exp_cmdsn &&
  92. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  93. session->exp_cmdsn = exp_cmdsn;
  94. if (max_cmdsn != session->max_cmdsn &&
  95. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  96. session->max_cmdsn = max_cmdsn;
  97. /*
  98. * if the window closed with IO queued, then kick the
  99. * xmit thread
  100. */
  101. if (!list_empty(&session->leadconn->cmdqueue) ||
  102. !list_empty(&session->leadconn->mgmtqueue))
  103. iscsi_conn_queue_work(session->leadconn);
  104. }
  105. }
  106. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  107. /**
  108. * iscsi_prep_data_out_pdu - initialize Data-Out
  109. * @task: scsi command task
  110. * @r2t: R2T info
  111. * @hdr: iscsi data in pdu
  112. *
  113. * Notes:
  114. * Initialize Data-Out within this R2T sequence and finds
  115. * proper data_offset within this SCSI command.
  116. *
  117. * This function is called with connection lock taken.
  118. **/
  119. void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
  120. struct iscsi_data *hdr)
  121. {
  122. struct iscsi_conn *conn = task->conn;
  123. unsigned int left = r2t->data_length - r2t->sent;
  124. task->hdr_len = sizeof(struct iscsi_data);
  125. memset(hdr, 0, sizeof(struct iscsi_data));
  126. hdr->ttt = r2t->ttt;
  127. hdr->datasn = cpu_to_be32(r2t->datasn);
  128. r2t->datasn++;
  129. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  130. memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
  131. hdr->itt = task->hdr_itt;
  132. hdr->exp_statsn = r2t->exp_statsn;
  133. hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
  134. if (left > conn->max_xmit_dlength) {
  135. hton24(hdr->dlength, conn->max_xmit_dlength);
  136. r2t->data_count = conn->max_xmit_dlength;
  137. hdr->flags = 0;
  138. } else {
  139. hton24(hdr->dlength, left);
  140. r2t->data_count = left;
  141. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  142. }
  143. conn->dataout_pdus_cnt++;
  144. }
  145. EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
  146. static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
  147. {
  148. unsigned exp_len = task->hdr_len + len;
  149. if (exp_len > task->hdr_max) {
  150. WARN_ON(1);
  151. return -EINVAL;
  152. }
  153. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  154. task->hdr_len = exp_len;
  155. return 0;
  156. }
  157. /*
  158. * make an extended cdb AHS
  159. */
  160. static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
  161. {
  162. struct scsi_cmnd *cmd = task->sc;
  163. unsigned rlen, pad_len;
  164. unsigned short ahslength;
  165. struct iscsi_ecdb_ahdr *ecdb_ahdr;
  166. int rc;
  167. ecdb_ahdr = iscsi_next_hdr(task);
  168. rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
  169. BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
  170. ahslength = rlen + sizeof(ecdb_ahdr->reserved);
  171. pad_len = iscsi_padding(rlen);
  172. rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
  173. sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
  174. if (rc)
  175. return rc;
  176. if (pad_len)
  177. memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
  178. ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
  179. ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
  180. ecdb_ahdr->reserved = 0;
  181. memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
  182. ISCSI_DBG_SESSION(task->conn->session,
  183. "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
  184. "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
  185. "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
  186. task->hdr_len);
  187. return 0;
  188. }
  189. static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
  190. {
  191. struct scsi_cmnd *sc = task->sc;
  192. struct iscsi_rlength_ahdr *rlen_ahdr;
  193. int rc;
  194. rlen_ahdr = iscsi_next_hdr(task);
  195. rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
  196. if (rc)
  197. return rc;
  198. rlen_ahdr->ahslength =
  199. cpu_to_be16(sizeof(rlen_ahdr->read_length) +
  200. sizeof(rlen_ahdr->reserved));
  201. rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
  202. rlen_ahdr->reserved = 0;
  203. rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
  204. ISCSI_DBG_SESSION(task->conn->session,
  205. "bidi-in rlen_ahdr->read_length(%d) "
  206. "rlen_ahdr->ahslength(%d)\n",
  207. be32_to_cpu(rlen_ahdr->read_length),
  208. be16_to_cpu(rlen_ahdr->ahslength));
  209. return 0;
  210. }
  211. /**
  212. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  213. * @task: iscsi task
  214. *
  215. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  216. * fields like dlength or final based on how much data it sends
  217. */
  218. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
  219. {
  220. struct iscsi_conn *conn = task->conn;
  221. struct iscsi_session *session = conn->session;
  222. struct scsi_cmnd *sc = task->sc;
  223. struct iscsi_cmd *hdr;
  224. unsigned hdrlength, cmd_len;
  225. itt_t itt;
  226. int rc;
  227. if (conn->session->tt->alloc_pdu) {
  228. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
  229. if (rc)
  230. return rc;
  231. }
  232. hdr = (struct iscsi_cmd *) task->hdr;
  233. itt = hdr->itt;
  234. memset(hdr, 0, sizeof(*hdr));
  235. if (session->tt->parse_pdu_itt)
  236. hdr->itt = task->hdr_itt = itt;
  237. else
  238. hdr->itt = task->hdr_itt = build_itt(task->itt,
  239. task->conn->session->age);
  240. task->hdr_len = 0;
  241. rc = iscsi_add_hdr(task, sizeof(*hdr));
  242. if (rc)
  243. return rc;
  244. hdr->opcode = ISCSI_OP_SCSI_CMD;
  245. hdr->flags = ISCSI_ATTR_SIMPLE;
  246. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  247. memcpy(task->lun, hdr->lun, sizeof(task->lun));
  248. hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
  249. session->cmdsn++;
  250. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  251. cmd_len = sc->cmd_len;
  252. if (cmd_len < ISCSI_CDB_SIZE)
  253. memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
  254. else if (cmd_len > ISCSI_CDB_SIZE) {
  255. rc = iscsi_prep_ecdb_ahs(task);
  256. if (rc)
  257. return rc;
  258. cmd_len = ISCSI_CDB_SIZE;
  259. }
  260. memcpy(hdr->cdb, sc->cmnd, cmd_len);
  261. task->imm_count = 0;
  262. if (scsi_bidi_cmnd(sc)) {
  263. hdr->flags |= ISCSI_FLAG_CMD_READ;
  264. rc = iscsi_prep_bidi_ahs(task);
  265. if (rc)
  266. return rc;
  267. }
  268. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  269. unsigned out_len = scsi_out(sc)->length;
  270. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  271. hdr->data_length = cpu_to_be32(out_len);
  272. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  273. /*
  274. * Write counters:
  275. *
  276. * imm_count bytes to be sent right after
  277. * SCSI PDU Header
  278. *
  279. * unsol_count bytes(as Data-Out) to be sent
  280. * without R2T ack right after
  281. * immediate data
  282. *
  283. * r2t data_length bytes to be sent via R2T ack's
  284. *
  285. * pad_count bytes to be sent as zero-padding
  286. */
  287. memset(r2t, 0, sizeof(*r2t));
  288. if (session->imm_data_en) {
  289. if (out_len >= session->first_burst)
  290. task->imm_count = min(session->first_burst,
  291. conn->max_xmit_dlength);
  292. else
  293. task->imm_count = min(out_len,
  294. conn->max_xmit_dlength);
  295. hton24(hdr->dlength, task->imm_count);
  296. } else
  297. zero_data(hdr->dlength);
  298. if (!session->initial_r2t_en) {
  299. r2t->data_length = min(session->first_burst, out_len) -
  300. task->imm_count;
  301. r2t->data_offset = task->imm_count;
  302. r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  303. r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
  304. }
  305. if (!task->unsol_r2t.data_length)
  306. /* No unsolicit Data-Out's */
  307. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  308. } else {
  309. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  310. zero_data(hdr->dlength);
  311. hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
  312. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  313. hdr->flags |= ISCSI_FLAG_CMD_READ;
  314. }
  315. /* calculate size of additional header segments (AHSs) */
  316. hdrlength = task->hdr_len - sizeof(*hdr);
  317. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  318. hdrlength /= ISCSI_PAD_LEN;
  319. WARN_ON(hdrlength >= 256);
  320. hdr->hlength = hdrlength & 0xFF;
  321. if (session->tt->init_task && session->tt->init_task(task))
  322. return -EIO;
  323. task->state = ISCSI_TASK_RUNNING;
  324. conn->scsicmd_pdus_cnt++;
  325. ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
  326. "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
  327. scsi_bidi_cmnd(sc) ? "bidirectional" :
  328. sc->sc_data_direction == DMA_TO_DEVICE ?
  329. "write" : "read", conn->id, sc, sc->cmnd[0],
  330. task->itt, scsi_bufflen(sc),
  331. scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
  332. session->cmdsn,
  333. session->max_cmdsn - session->exp_cmdsn + 1);
  334. return 0;
  335. }
  336. /**
  337. * iscsi_free_task - free a task
  338. * @task: iscsi cmd task
  339. *
  340. * Must be called with session lock.
  341. * This function returns the scsi command to scsi-ml or cleans
  342. * up mgmt tasks then returns the task to the pool.
  343. */
  344. static void iscsi_free_task(struct iscsi_task *task)
  345. {
  346. struct iscsi_conn *conn = task->conn;
  347. struct iscsi_session *session = conn->session;
  348. struct scsi_cmnd *sc = task->sc;
  349. session->tt->cleanup_task(task);
  350. task->state = ISCSI_TASK_FREE;
  351. task->sc = NULL;
  352. /*
  353. * login task is preallocated so do not free
  354. */
  355. if (conn->login_task == task)
  356. return;
  357. __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
  358. if (sc) {
  359. task->sc = NULL;
  360. /* SCSI eh reuses commands to verify us */
  361. sc->SCp.ptr = NULL;
  362. /*
  363. * queue command may call this to free the task, but
  364. * not have setup the sc callback
  365. */
  366. if (sc->scsi_done)
  367. sc->scsi_done(sc);
  368. }
  369. }
  370. void __iscsi_get_task(struct iscsi_task *task)
  371. {
  372. atomic_inc(&task->refcount);
  373. }
  374. EXPORT_SYMBOL_GPL(__iscsi_get_task);
  375. static void __iscsi_put_task(struct iscsi_task *task)
  376. {
  377. if (atomic_dec_and_test(&task->refcount))
  378. iscsi_free_task(task);
  379. }
  380. void iscsi_put_task(struct iscsi_task *task)
  381. {
  382. struct iscsi_session *session = task->conn->session;
  383. spin_lock_bh(&session->lock);
  384. __iscsi_put_task(task);
  385. spin_unlock_bh(&session->lock);
  386. }
  387. EXPORT_SYMBOL_GPL(iscsi_put_task);
  388. /**
  389. * iscsi_complete_task - finish a task
  390. * @task: iscsi cmd task
  391. * @state: state to complete task with
  392. *
  393. * Must be called with session lock.
  394. */
  395. static void iscsi_complete_task(struct iscsi_task *task, int state)
  396. {
  397. struct iscsi_conn *conn = task->conn;
  398. if (task->state == ISCSI_TASK_COMPLETED ||
  399. task->state == ISCSI_TASK_ABRT_TMF ||
  400. task->state == ISCSI_TASK_ABRT_SESS_RECOV)
  401. return;
  402. WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
  403. task->state = state;
  404. if (!list_empty(&task->running))
  405. list_del_init(&task->running);
  406. if (conn->task == task)
  407. conn->task = NULL;
  408. if (conn->ping_task == task)
  409. conn->ping_task = NULL;
  410. /* release get from queueing */
  411. __iscsi_put_task(task);
  412. }
  413. /*
  414. * session lock must be held and if not called for a task that is
  415. * still pending or from the xmit thread, then xmit thread must
  416. * be suspended.
  417. */
  418. static void fail_scsi_task(struct iscsi_task *task, int err)
  419. {
  420. struct iscsi_conn *conn = task->conn;
  421. struct scsi_cmnd *sc;
  422. int state;
  423. /*
  424. * if a command completes and we get a successful tmf response
  425. * we will hit this because the scsi eh abort code does not take
  426. * a ref to the task.
  427. */
  428. sc = task->sc;
  429. if (!sc)
  430. return;
  431. if (task->state == ISCSI_TASK_PENDING) {
  432. /*
  433. * cmd never made it to the xmit thread, so we should not count
  434. * the cmd in the sequencing
  435. */
  436. conn->session->queued_cmdsn--;
  437. /* it was never sent so just complete like normal */
  438. state = ISCSI_TASK_COMPLETED;
  439. } else if (err == DID_TRANSPORT_DISRUPTED)
  440. state = ISCSI_TASK_ABRT_SESS_RECOV;
  441. else
  442. state = ISCSI_TASK_ABRT_TMF;
  443. sc->result = err << 16;
  444. if (!scsi_bidi_cmnd(sc))
  445. scsi_set_resid(sc, scsi_bufflen(sc));
  446. else {
  447. scsi_out(sc)->resid = scsi_out(sc)->length;
  448. scsi_in(sc)->resid = scsi_in(sc)->length;
  449. }
  450. iscsi_complete_task(task, state);
  451. }
  452. static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
  453. struct iscsi_task *task)
  454. {
  455. struct iscsi_session *session = conn->session;
  456. struct iscsi_hdr *hdr = task->hdr;
  457. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  458. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  459. return -ENOTCONN;
  460. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  461. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  462. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  463. /*
  464. * pre-format CmdSN for outgoing PDU.
  465. */
  466. nop->cmdsn = cpu_to_be32(session->cmdsn);
  467. if (hdr->itt != RESERVED_ITT) {
  468. /*
  469. * TODO: We always use immediate, so we never hit this.
  470. * If we start to send tmfs or nops as non-immediate then
  471. * we should start checking the cmdsn numbers for mgmt tasks.
  472. */
  473. if (conn->c_stage == ISCSI_CONN_STARTED &&
  474. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  475. session->queued_cmdsn++;
  476. session->cmdsn++;
  477. }
  478. }
  479. if (session->tt->init_task && session->tt->init_task(task))
  480. return -EIO;
  481. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  482. session->state = ISCSI_STATE_LOGGING_OUT;
  483. task->state = ISCSI_TASK_RUNNING;
  484. ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
  485. "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
  486. hdr->itt, task->data_count);
  487. return 0;
  488. }
  489. static struct iscsi_task *
  490. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  491. char *data, uint32_t data_size)
  492. {
  493. struct iscsi_session *session = conn->session;
  494. struct iscsi_host *ihost = shost_priv(session->host);
  495. struct iscsi_task *task;
  496. itt_t itt;
  497. if (session->state == ISCSI_STATE_TERMINATE)
  498. return NULL;
  499. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  500. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  501. /*
  502. * Login and Text are sent serially, in
  503. * request-followed-by-response sequence.
  504. * Same task can be used. Same ITT must be used.
  505. * Note that login_task is preallocated at conn_create().
  506. */
  507. task = conn->login_task;
  508. else {
  509. if (session->state != ISCSI_STATE_LOGGED_IN)
  510. return NULL;
  511. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  512. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  513. if (!__kfifo_get(session->cmdpool.queue,
  514. (void*)&task, sizeof(void*)))
  515. return NULL;
  516. }
  517. /*
  518. * released in complete pdu for task we expect a response for, and
  519. * released by the lld when it has transmitted the task for
  520. * pdus we do not expect a response for.
  521. */
  522. atomic_set(&task->refcount, 1);
  523. task->conn = conn;
  524. task->sc = NULL;
  525. INIT_LIST_HEAD(&task->running);
  526. task->state = ISCSI_TASK_PENDING;
  527. if (data_size) {
  528. memcpy(task->data, data, data_size);
  529. task->data_count = data_size;
  530. } else
  531. task->data_count = 0;
  532. if (conn->session->tt->alloc_pdu) {
  533. if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
  534. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
  535. "pdu for mgmt task.\n");
  536. goto free_task;
  537. }
  538. }
  539. itt = task->hdr->itt;
  540. task->hdr_len = sizeof(struct iscsi_hdr);
  541. memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
  542. if (hdr->itt != RESERVED_ITT) {
  543. if (session->tt->parse_pdu_itt)
  544. task->hdr->itt = itt;
  545. else
  546. task->hdr->itt = build_itt(task->itt,
  547. task->conn->session->age);
  548. }
  549. if (!ihost->workq) {
  550. if (iscsi_prep_mgmt_task(conn, task))
  551. goto free_task;
  552. if (session->tt->xmit_task(task))
  553. goto free_task;
  554. } else {
  555. list_add_tail(&task->running, &conn->mgmtqueue);
  556. iscsi_conn_queue_work(conn);
  557. }
  558. return task;
  559. free_task:
  560. __iscsi_put_task(task);
  561. return NULL;
  562. }
  563. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  564. char *data, uint32_t data_size)
  565. {
  566. struct iscsi_conn *conn = cls_conn->dd_data;
  567. struct iscsi_session *session = conn->session;
  568. int err = 0;
  569. spin_lock_bh(&session->lock);
  570. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  571. err = -EPERM;
  572. spin_unlock_bh(&session->lock);
  573. return err;
  574. }
  575. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  576. /**
  577. * iscsi_cmd_rsp - SCSI Command Response processing
  578. * @conn: iscsi connection
  579. * @hdr: iscsi header
  580. * @task: scsi command task
  581. * @data: cmd data buffer
  582. * @datalen: len of buffer
  583. *
  584. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  585. * then completes the command and task.
  586. **/
  587. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  588. struct iscsi_task *task, char *data,
  589. int datalen)
  590. {
  591. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  592. struct iscsi_session *session = conn->session;
  593. struct scsi_cmnd *sc = task->sc;
  594. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  595. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  596. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  597. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  598. sc->result = DID_ERROR << 16;
  599. goto out;
  600. }
  601. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  602. uint16_t senselen;
  603. if (datalen < 2) {
  604. invalid_datalen:
  605. iscsi_conn_printk(KERN_ERR, conn,
  606. "Got CHECK_CONDITION but invalid data "
  607. "buffer size of %d\n", datalen);
  608. sc->result = DID_BAD_TARGET << 16;
  609. goto out;
  610. }
  611. senselen = get_unaligned_be16(data);
  612. if (datalen < senselen)
  613. goto invalid_datalen;
  614. memcpy(sc->sense_buffer, data + 2,
  615. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  616. ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
  617. min_t(uint16_t, senselen,
  618. SCSI_SENSE_BUFFERSIZE));
  619. }
  620. if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  621. ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
  622. int res_count = be32_to_cpu(rhdr->bi_residual_count);
  623. if (scsi_bidi_cmnd(sc) && res_count > 0 &&
  624. (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
  625. res_count <= scsi_in(sc)->length))
  626. scsi_in(sc)->resid = res_count;
  627. else
  628. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  629. }
  630. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  631. ISCSI_FLAG_CMD_OVERFLOW)) {
  632. int res_count = be32_to_cpu(rhdr->residual_count);
  633. if (res_count > 0 &&
  634. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  635. res_count <= scsi_bufflen(sc)))
  636. /* write side for bidi or uni-io set_resid */
  637. scsi_set_resid(sc, res_count);
  638. else
  639. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  640. }
  641. out:
  642. ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
  643. sc, sc->result, task->itt);
  644. conn->scsirsp_pdus_cnt++;
  645. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  646. }
  647. /**
  648. * iscsi_data_in_rsp - SCSI Data-In Response processing
  649. * @conn: iscsi connection
  650. * @hdr: iscsi pdu
  651. * @task: scsi command task
  652. **/
  653. static void
  654. iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  655. struct iscsi_task *task)
  656. {
  657. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
  658. struct scsi_cmnd *sc = task->sc;
  659. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  660. return;
  661. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
  662. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  663. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  664. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  665. ISCSI_FLAG_DATA_OVERFLOW)) {
  666. int res_count = be32_to_cpu(rhdr->residual_count);
  667. if (res_count > 0 &&
  668. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  669. res_count <= scsi_in(sc)->length))
  670. scsi_in(sc)->resid = res_count;
  671. else
  672. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  673. }
  674. ISCSI_DBG_SESSION(conn->session, "data in with status done "
  675. "[sc %p res %d itt 0x%x]\n",
  676. sc, sc->result, task->itt);
  677. conn->scsirsp_pdus_cnt++;
  678. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  679. }
  680. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  681. {
  682. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  683. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  684. conn->tmfrsp_pdus_cnt++;
  685. if (conn->tmf_state != TMF_QUEUED)
  686. return;
  687. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  688. conn->tmf_state = TMF_SUCCESS;
  689. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  690. conn->tmf_state = TMF_NOT_FOUND;
  691. else
  692. conn->tmf_state = TMF_FAILED;
  693. wake_up(&conn->ehwait);
  694. }
  695. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  696. {
  697. struct iscsi_nopout hdr;
  698. struct iscsi_task *task;
  699. if (!rhdr && conn->ping_task)
  700. return;
  701. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  702. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  703. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  704. if (rhdr) {
  705. memcpy(hdr.lun, rhdr->lun, 8);
  706. hdr.ttt = rhdr->ttt;
  707. hdr.itt = RESERVED_ITT;
  708. } else
  709. hdr.ttt = RESERVED_ITT;
  710. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  711. if (!task)
  712. iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
  713. else if (!rhdr) {
  714. /* only track our nops */
  715. conn->ping_task = task;
  716. conn->last_ping = jiffies;
  717. }
  718. }
  719. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  720. char *data, int datalen)
  721. {
  722. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  723. struct iscsi_hdr rejected_pdu;
  724. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  725. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  726. if (ntoh24(reject->dlength) > datalen)
  727. return ISCSI_ERR_PROTO;
  728. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  729. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  730. iscsi_conn_printk(KERN_ERR, conn,
  731. "pdu (op 0x%x) rejected "
  732. "due to DataDigest error.\n",
  733. rejected_pdu.opcode);
  734. }
  735. }
  736. return 0;
  737. }
  738. /**
  739. * iscsi_itt_to_task - look up task by itt
  740. * @conn: iscsi connection
  741. * @itt: itt
  742. *
  743. * This should be used for mgmt tasks like login and nops, or if
  744. * the LDD's itt space does not include the session age.
  745. *
  746. * The session lock must be held.
  747. */
  748. struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
  749. {
  750. struct iscsi_session *session = conn->session;
  751. int i;
  752. if (itt == RESERVED_ITT)
  753. return NULL;
  754. if (session->tt->parse_pdu_itt)
  755. session->tt->parse_pdu_itt(conn, itt, &i, NULL);
  756. else
  757. i = get_itt(itt);
  758. if (i >= session->cmds_max)
  759. return NULL;
  760. return session->cmds[i];
  761. }
  762. EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
  763. /**
  764. * __iscsi_complete_pdu - complete pdu
  765. * @conn: iscsi conn
  766. * @hdr: iscsi header
  767. * @data: data buffer
  768. * @datalen: len of data buffer
  769. *
  770. * Completes pdu processing by freeing any resources allocated at
  771. * queuecommand or send generic. session lock must be held and verify
  772. * itt must have been called.
  773. */
  774. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  775. char *data, int datalen)
  776. {
  777. struct iscsi_session *session = conn->session;
  778. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  779. struct iscsi_task *task;
  780. uint32_t itt;
  781. conn->last_recv = jiffies;
  782. rc = iscsi_verify_itt(conn, hdr->itt);
  783. if (rc)
  784. return rc;
  785. if (hdr->itt != RESERVED_ITT)
  786. itt = get_itt(hdr->itt);
  787. else
  788. itt = ~0U;
  789. ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
  790. opcode, conn->id, itt, datalen);
  791. if (itt == ~0U) {
  792. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  793. switch(opcode) {
  794. case ISCSI_OP_NOOP_IN:
  795. if (datalen) {
  796. rc = ISCSI_ERR_PROTO;
  797. break;
  798. }
  799. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  800. break;
  801. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  802. break;
  803. case ISCSI_OP_REJECT:
  804. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  805. break;
  806. case ISCSI_OP_ASYNC_EVENT:
  807. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  808. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  809. rc = ISCSI_ERR_CONN_FAILED;
  810. break;
  811. default:
  812. rc = ISCSI_ERR_BAD_OPCODE;
  813. break;
  814. }
  815. goto out;
  816. }
  817. switch(opcode) {
  818. case ISCSI_OP_SCSI_CMD_RSP:
  819. case ISCSI_OP_SCSI_DATA_IN:
  820. task = iscsi_itt_to_ctask(conn, hdr->itt);
  821. if (!task)
  822. return ISCSI_ERR_BAD_ITT;
  823. break;
  824. case ISCSI_OP_R2T:
  825. /*
  826. * LLD handles R2Ts if they need to.
  827. */
  828. return 0;
  829. case ISCSI_OP_LOGOUT_RSP:
  830. case ISCSI_OP_LOGIN_RSP:
  831. case ISCSI_OP_TEXT_RSP:
  832. case ISCSI_OP_SCSI_TMFUNC_RSP:
  833. case ISCSI_OP_NOOP_IN:
  834. task = iscsi_itt_to_task(conn, hdr->itt);
  835. if (!task)
  836. return ISCSI_ERR_BAD_ITT;
  837. break;
  838. default:
  839. return ISCSI_ERR_BAD_OPCODE;
  840. }
  841. switch(opcode) {
  842. case ISCSI_OP_SCSI_CMD_RSP:
  843. iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
  844. break;
  845. case ISCSI_OP_SCSI_DATA_IN:
  846. iscsi_data_in_rsp(conn, hdr, task);
  847. break;
  848. case ISCSI_OP_LOGOUT_RSP:
  849. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  850. if (datalen) {
  851. rc = ISCSI_ERR_PROTO;
  852. break;
  853. }
  854. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  855. goto recv_pdu;
  856. case ISCSI_OP_LOGIN_RSP:
  857. case ISCSI_OP_TEXT_RSP:
  858. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  859. /*
  860. * login related PDU's exp_statsn is handled in
  861. * userspace
  862. */
  863. goto recv_pdu;
  864. case ISCSI_OP_SCSI_TMFUNC_RSP:
  865. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  866. if (datalen) {
  867. rc = ISCSI_ERR_PROTO;
  868. break;
  869. }
  870. iscsi_tmf_rsp(conn, hdr);
  871. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  872. break;
  873. case ISCSI_OP_NOOP_IN:
  874. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  875. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  876. rc = ISCSI_ERR_PROTO;
  877. break;
  878. }
  879. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  880. if (conn->ping_task != task)
  881. /*
  882. * If this is not in response to one of our
  883. * nops then it must be from userspace.
  884. */
  885. goto recv_pdu;
  886. mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
  887. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  888. break;
  889. default:
  890. rc = ISCSI_ERR_BAD_OPCODE;
  891. break;
  892. }
  893. out:
  894. return rc;
  895. recv_pdu:
  896. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  897. rc = ISCSI_ERR_CONN_FAILED;
  898. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  899. return rc;
  900. }
  901. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  902. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  903. char *data, int datalen)
  904. {
  905. int rc;
  906. spin_lock(&conn->session->lock);
  907. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  908. spin_unlock(&conn->session->lock);
  909. return rc;
  910. }
  911. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  912. int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
  913. {
  914. struct iscsi_session *session = conn->session;
  915. int age = 0, i = 0;
  916. if (itt == RESERVED_ITT)
  917. return 0;
  918. if (session->tt->parse_pdu_itt)
  919. session->tt->parse_pdu_itt(conn, itt, &i, &age);
  920. else {
  921. i = get_itt(itt);
  922. age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
  923. }
  924. if (age != session->age) {
  925. iscsi_conn_printk(KERN_ERR, conn,
  926. "received itt %x expected session age (%x)\n",
  927. (__force u32)itt, session->age);
  928. return ISCSI_ERR_BAD_ITT;
  929. }
  930. if (i >= session->cmds_max) {
  931. iscsi_conn_printk(KERN_ERR, conn,
  932. "received invalid itt index %u (max cmds "
  933. "%u.\n", i, session->cmds_max);
  934. return ISCSI_ERR_BAD_ITT;
  935. }
  936. return 0;
  937. }
  938. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  939. /**
  940. * iscsi_itt_to_ctask - look up ctask by itt
  941. * @conn: iscsi connection
  942. * @itt: itt
  943. *
  944. * This should be used for cmd tasks.
  945. *
  946. * The session lock must be held.
  947. */
  948. struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
  949. {
  950. struct iscsi_task *task;
  951. if (iscsi_verify_itt(conn, itt))
  952. return NULL;
  953. task = iscsi_itt_to_task(conn, itt);
  954. if (!task || !task->sc)
  955. return NULL;
  956. if (task->sc->SCp.phase != conn->session->age) {
  957. iscsi_session_printk(KERN_ERR, conn->session,
  958. "task's session age %d, expected %d\n",
  959. task->sc->SCp.phase, conn->session->age);
  960. return NULL;
  961. }
  962. return task;
  963. }
  964. EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
  965. void iscsi_session_failure(struct iscsi_session *session,
  966. enum iscsi_err err)
  967. {
  968. struct iscsi_conn *conn;
  969. struct device *dev;
  970. unsigned long flags;
  971. spin_lock_irqsave(&session->lock, flags);
  972. conn = session->leadconn;
  973. if (session->state == ISCSI_STATE_TERMINATE || !conn) {
  974. spin_unlock_irqrestore(&session->lock, flags);
  975. return;
  976. }
  977. dev = get_device(&conn->cls_conn->dev);
  978. spin_unlock_irqrestore(&session->lock, flags);
  979. if (!dev)
  980. return;
  981. /*
  982. * if the host is being removed bypass the connection
  983. * recovery initialization because we are going to kill
  984. * the session.
  985. */
  986. if (err == ISCSI_ERR_INVALID_HOST)
  987. iscsi_conn_error_event(conn->cls_conn, err);
  988. else
  989. iscsi_conn_failure(conn, err);
  990. put_device(dev);
  991. }
  992. EXPORT_SYMBOL_GPL(iscsi_session_failure);
  993. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  994. {
  995. struct iscsi_session *session = conn->session;
  996. unsigned long flags;
  997. spin_lock_irqsave(&session->lock, flags);
  998. if (session->state == ISCSI_STATE_FAILED) {
  999. spin_unlock_irqrestore(&session->lock, flags);
  1000. return;
  1001. }
  1002. if (conn->stop_stage == 0)
  1003. session->state = ISCSI_STATE_FAILED;
  1004. spin_unlock_irqrestore(&session->lock, flags);
  1005. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1006. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1007. iscsi_conn_error_event(conn->cls_conn, err);
  1008. }
  1009. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  1010. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  1011. {
  1012. struct iscsi_session *session = conn->session;
  1013. /*
  1014. * Check for iSCSI window and take care of CmdSN wrap-around
  1015. */
  1016. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  1017. ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
  1018. "%u MaxCmdSN %u CmdSN %u/%u\n",
  1019. session->exp_cmdsn, session->max_cmdsn,
  1020. session->cmdsn, session->queued_cmdsn);
  1021. return -ENOSPC;
  1022. }
  1023. return 0;
  1024. }
  1025. static int iscsi_xmit_task(struct iscsi_conn *conn)
  1026. {
  1027. struct iscsi_task *task = conn->task;
  1028. int rc;
  1029. __iscsi_get_task(task);
  1030. spin_unlock_bh(&conn->session->lock);
  1031. rc = conn->session->tt->xmit_task(task);
  1032. spin_lock_bh(&conn->session->lock);
  1033. __iscsi_put_task(task);
  1034. if (!rc)
  1035. /* done with this task */
  1036. conn->task = NULL;
  1037. return rc;
  1038. }
  1039. /**
  1040. * iscsi_requeue_task - requeue task to run from session workqueue
  1041. * @task: task to requeue
  1042. *
  1043. * LLDs that need to run a task from the session workqueue should call
  1044. * this. The session lock must be held. This should only be called
  1045. * by software drivers.
  1046. */
  1047. void iscsi_requeue_task(struct iscsi_task *task)
  1048. {
  1049. struct iscsi_conn *conn = task->conn;
  1050. /*
  1051. * this may be on the requeue list already if the xmit_task callout
  1052. * is handling the r2ts while we are adding new ones
  1053. */
  1054. if (list_empty(&task->running))
  1055. list_add_tail(&task->running, &conn->requeue);
  1056. iscsi_conn_queue_work(conn);
  1057. }
  1058. EXPORT_SYMBOL_GPL(iscsi_requeue_task);
  1059. /**
  1060. * iscsi_data_xmit - xmit any command into the scheduled connection
  1061. * @conn: iscsi connection
  1062. *
  1063. * Notes:
  1064. * The function can return -EAGAIN in which case the caller must
  1065. * re-schedule it again later or recover. '0' return code means
  1066. * successful xmit.
  1067. **/
  1068. static int iscsi_data_xmit(struct iscsi_conn *conn)
  1069. {
  1070. int rc = 0;
  1071. spin_lock_bh(&conn->session->lock);
  1072. if (unlikely(conn->suspend_tx)) {
  1073. ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
  1074. spin_unlock_bh(&conn->session->lock);
  1075. return -ENODATA;
  1076. }
  1077. if (conn->task) {
  1078. rc = iscsi_xmit_task(conn);
  1079. if (rc)
  1080. goto again;
  1081. }
  1082. /*
  1083. * process mgmt pdus like nops before commands since we should
  1084. * only have one nop-out as a ping from us and targets should not
  1085. * overflow us with nop-ins
  1086. */
  1087. check_mgmt:
  1088. while (!list_empty(&conn->mgmtqueue)) {
  1089. conn->task = list_entry(conn->mgmtqueue.next,
  1090. struct iscsi_task, running);
  1091. list_del_init(&conn->task->running);
  1092. if (iscsi_prep_mgmt_task(conn, conn->task)) {
  1093. __iscsi_put_task(conn->task);
  1094. conn->task = NULL;
  1095. continue;
  1096. }
  1097. rc = iscsi_xmit_task(conn);
  1098. if (rc)
  1099. goto again;
  1100. }
  1101. /* process pending command queue */
  1102. while (!list_empty(&conn->cmdqueue)) {
  1103. if (conn->tmf_state == TMF_QUEUED)
  1104. break;
  1105. conn->task = list_entry(conn->cmdqueue.next,
  1106. struct iscsi_task, running);
  1107. list_del_init(&conn->task->running);
  1108. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  1109. fail_scsi_task(conn->task, DID_IMM_RETRY);
  1110. continue;
  1111. }
  1112. rc = iscsi_prep_scsi_cmd_pdu(conn->task);
  1113. if (rc) {
  1114. if (rc == -ENOMEM) {
  1115. list_add_tail(&conn->task->running,
  1116. &conn->cmdqueue);
  1117. conn->task = NULL;
  1118. goto again;
  1119. } else
  1120. fail_scsi_task(conn->task, DID_ABORT);
  1121. continue;
  1122. }
  1123. rc = iscsi_xmit_task(conn);
  1124. if (rc)
  1125. goto again;
  1126. /*
  1127. * we could continuously get new task requests so
  1128. * we need to check the mgmt queue for nops that need to
  1129. * be sent to aviod starvation
  1130. */
  1131. if (!list_empty(&conn->mgmtqueue))
  1132. goto check_mgmt;
  1133. }
  1134. while (!list_empty(&conn->requeue)) {
  1135. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  1136. break;
  1137. /*
  1138. * we always do fastlogout - conn stop code will clean up.
  1139. */
  1140. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  1141. break;
  1142. conn->task = list_entry(conn->requeue.next,
  1143. struct iscsi_task, running);
  1144. list_del_init(&conn->task->running);
  1145. conn->task->state = ISCSI_TASK_RUNNING;
  1146. rc = iscsi_xmit_task(conn);
  1147. if (rc)
  1148. goto again;
  1149. if (!list_empty(&conn->mgmtqueue))
  1150. goto check_mgmt;
  1151. }
  1152. spin_unlock_bh(&conn->session->lock);
  1153. return -ENODATA;
  1154. again:
  1155. if (unlikely(conn->suspend_tx))
  1156. rc = -ENODATA;
  1157. spin_unlock_bh(&conn->session->lock);
  1158. return rc;
  1159. }
  1160. static void iscsi_xmitworker(struct work_struct *work)
  1161. {
  1162. struct iscsi_conn *conn =
  1163. container_of(work, struct iscsi_conn, xmitwork);
  1164. int rc;
  1165. /*
  1166. * serialize Xmit worker on a per-connection basis.
  1167. */
  1168. do {
  1169. rc = iscsi_data_xmit(conn);
  1170. } while (rc >= 0 || rc == -EAGAIN);
  1171. }
  1172. static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
  1173. struct scsi_cmnd *sc)
  1174. {
  1175. struct iscsi_task *task;
  1176. if (!__kfifo_get(conn->session->cmdpool.queue,
  1177. (void *) &task, sizeof(void *)))
  1178. return NULL;
  1179. sc->SCp.phase = conn->session->age;
  1180. sc->SCp.ptr = (char *) task;
  1181. atomic_set(&task->refcount, 1);
  1182. task->state = ISCSI_TASK_PENDING;
  1183. task->conn = conn;
  1184. task->sc = sc;
  1185. INIT_LIST_HEAD(&task->running);
  1186. return task;
  1187. }
  1188. enum {
  1189. FAILURE_BAD_HOST = 1,
  1190. FAILURE_SESSION_FAILED,
  1191. FAILURE_SESSION_FREED,
  1192. FAILURE_WINDOW_CLOSED,
  1193. FAILURE_OOM,
  1194. FAILURE_SESSION_TERMINATE,
  1195. FAILURE_SESSION_IN_RECOVERY,
  1196. FAILURE_SESSION_RECOVERY_TIMEOUT,
  1197. FAILURE_SESSION_LOGGING_OUT,
  1198. FAILURE_SESSION_NOT_READY,
  1199. };
  1200. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  1201. {
  1202. struct iscsi_cls_session *cls_session;
  1203. struct Scsi_Host *host;
  1204. struct iscsi_host *ihost;
  1205. int reason = 0;
  1206. struct iscsi_session *session;
  1207. struct iscsi_conn *conn;
  1208. struct iscsi_task *task = NULL;
  1209. sc->scsi_done = done;
  1210. sc->result = 0;
  1211. sc->SCp.ptr = NULL;
  1212. host = sc->device->host;
  1213. ihost = shost_priv(host);
  1214. spin_unlock(host->host_lock);
  1215. cls_session = starget_to_session(scsi_target(sc->device));
  1216. session = cls_session->dd_data;
  1217. spin_lock(&session->lock);
  1218. reason = iscsi_session_chkready(cls_session);
  1219. if (reason) {
  1220. sc->result = reason;
  1221. goto fault;
  1222. }
  1223. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1224. /*
  1225. * to handle the race between when we set the recovery state
  1226. * and block the session we requeue here (commands could
  1227. * be entering our queuecommand while a block is starting
  1228. * up because the block code is not locked)
  1229. */
  1230. switch (session->state) {
  1231. case ISCSI_STATE_FAILED:
  1232. case ISCSI_STATE_IN_RECOVERY:
  1233. reason = FAILURE_SESSION_IN_RECOVERY;
  1234. sc->result = DID_IMM_RETRY << 16;
  1235. break;
  1236. case ISCSI_STATE_LOGGING_OUT:
  1237. reason = FAILURE_SESSION_LOGGING_OUT;
  1238. sc->result = DID_IMM_RETRY << 16;
  1239. break;
  1240. case ISCSI_STATE_RECOVERY_FAILED:
  1241. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  1242. sc->result = DID_TRANSPORT_FAILFAST << 16;
  1243. break;
  1244. case ISCSI_STATE_TERMINATE:
  1245. reason = FAILURE_SESSION_TERMINATE;
  1246. sc->result = DID_NO_CONNECT << 16;
  1247. break;
  1248. default:
  1249. reason = FAILURE_SESSION_FREED;
  1250. sc->result = DID_NO_CONNECT << 16;
  1251. }
  1252. goto fault;
  1253. }
  1254. conn = session->leadconn;
  1255. if (!conn) {
  1256. reason = FAILURE_SESSION_FREED;
  1257. sc->result = DID_NO_CONNECT << 16;
  1258. goto fault;
  1259. }
  1260. if (iscsi_check_cmdsn_window_closed(conn)) {
  1261. reason = FAILURE_WINDOW_CLOSED;
  1262. goto reject;
  1263. }
  1264. task = iscsi_alloc_task(conn, sc);
  1265. if (!task) {
  1266. reason = FAILURE_OOM;
  1267. goto reject;
  1268. }
  1269. if (!ihost->workq) {
  1270. reason = iscsi_prep_scsi_cmd_pdu(task);
  1271. if (reason) {
  1272. if (reason == -ENOMEM) {
  1273. reason = FAILURE_OOM;
  1274. goto prepd_reject;
  1275. } else {
  1276. sc->result = DID_ABORT << 16;
  1277. goto prepd_fault;
  1278. }
  1279. }
  1280. if (session->tt->xmit_task(task)) {
  1281. reason = FAILURE_SESSION_NOT_READY;
  1282. goto prepd_reject;
  1283. }
  1284. } else {
  1285. list_add_tail(&task->running, &conn->cmdqueue);
  1286. iscsi_conn_queue_work(conn);
  1287. }
  1288. session->queued_cmdsn++;
  1289. spin_unlock(&session->lock);
  1290. spin_lock(host->host_lock);
  1291. return 0;
  1292. prepd_reject:
  1293. sc->scsi_done = NULL;
  1294. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1295. reject:
  1296. spin_unlock(&session->lock);
  1297. ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
  1298. sc->cmnd[0], reason);
  1299. spin_lock(host->host_lock);
  1300. return SCSI_MLQUEUE_TARGET_BUSY;
  1301. prepd_fault:
  1302. sc->scsi_done = NULL;
  1303. iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
  1304. fault:
  1305. spin_unlock(&session->lock);
  1306. ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
  1307. sc->cmnd[0], reason);
  1308. if (!scsi_bidi_cmnd(sc))
  1309. scsi_set_resid(sc, scsi_bufflen(sc));
  1310. else {
  1311. scsi_out(sc)->resid = scsi_out(sc)->length;
  1312. scsi_in(sc)->resid = scsi_in(sc)->length;
  1313. }
  1314. done(sc);
  1315. spin_lock(host->host_lock);
  1316. return 0;
  1317. }
  1318. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  1319. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  1320. {
  1321. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  1322. return sdev->queue_depth;
  1323. }
  1324. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  1325. int iscsi_target_alloc(struct scsi_target *starget)
  1326. {
  1327. struct iscsi_cls_session *cls_session = starget_to_session(starget);
  1328. struct iscsi_session *session = cls_session->dd_data;
  1329. starget->can_queue = session->scsi_cmds_max;
  1330. return 0;
  1331. }
  1332. EXPORT_SYMBOL_GPL(iscsi_target_alloc);
  1333. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  1334. {
  1335. struct iscsi_session *session = cls_session->dd_data;
  1336. spin_lock_bh(&session->lock);
  1337. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1338. session->state = ISCSI_STATE_RECOVERY_FAILED;
  1339. if (session->leadconn)
  1340. wake_up(&session->leadconn->ehwait);
  1341. }
  1342. spin_unlock_bh(&session->lock);
  1343. }
  1344. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  1345. int iscsi_eh_target_reset(struct scsi_cmnd *sc)
  1346. {
  1347. struct iscsi_cls_session *cls_session;
  1348. struct iscsi_session *session;
  1349. struct iscsi_conn *conn;
  1350. cls_session = starget_to_session(scsi_target(sc->device));
  1351. session = cls_session->dd_data;
  1352. conn = session->leadconn;
  1353. mutex_lock(&session->eh_mutex);
  1354. spin_lock_bh(&session->lock);
  1355. if (session->state == ISCSI_STATE_TERMINATE) {
  1356. failed:
  1357. iscsi_session_printk(KERN_INFO, session,
  1358. "failing target reset: Could not log "
  1359. "back into target [age %d]\n",
  1360. session->age);
  1361. spin_unlock_bh(&session->lock);
  1362. mutex_unlock(&session->eh_mutex);
  1363. return FAILED;
  1364. }
  1365. spin_unlock_bh(&session->lock);
  1366. mutex_unlock(&session->eh_mutex);
  1367. /*
  1368. * we drop the lock here but the leadconn cannot be destoyed while
  1369. * we are in the scsi eh
  1370. */
  1371. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1372. ISCSI_DBG_SESSION(session, "wait for relogin\n");
  1373. wait_event_interruptible(conn->ehwait,
  1374. session->state == ISCSI_STATE_TERMINATE ||
  1375. session->state == ISCSI_STATE_LOGGED_IN ||
  1376. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1377. if (signal_pending(current))
  1378. flush_signals(current);
  1379. mutex_lock(&session->eh_mutex);
  1380. spin_lock_bh(&session->lock);
  1381. if (session->state == ISCSI_STATE_LOGGED_IN)
  1382. iscsi_session_printk(KERN_INFO, session,
  1383. "target reset succeeded\n");
  1384. else
  1385. goto failed;
  1386. spin_unlock_bh(&session->lock);
  1387. mutex_unlock(&session->eh_mutex);
  1388. return SUCCESS;
  1389. }
  1390. EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
  1391. static void iscsi_tmf_timedout(unsigned long data)
  1392. {
  1393. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1394. struct iscsi_session *session = conn->session;
  1395. spin_lock(&session->lock);
  1396. if (conn->tmf_state == TMF_QUEUED) {
  1397. conn->tmf_state = TMF_TIMEDOUT;
  1398. ISCSI_DBG_SESSION(session, "tmf timedout\n");
  1399. /* unblock eh_abort() */
  1400. wake_up(&conn->ehwait);
  1401. }
  1402. spin_unlock(&session->lock);
  1403. }
  1404. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1405. struct iscsi_tm *hdr, int age,
  1406. int timeout)
  1407. {
  1408. struct iscsi_session *session = conn->session;
  1409. struct iscsi_task *task;
  1410. task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1411. NULL, 0);
  1412. if (!task) {
  1413. spin_unlock_bh(&session->lock);
  1414. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1415. spin_lock_bh(&session->lock);
  1416. ISCSI_DBG_SESSION(session, "tmf exec failure\n");
  1417. return -EPERM;
  1418. }
  1419. conn->tmfcmd_pdus_cnt++;
  1420. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1421. conn->tmf_timer.function = iscsi_tmf_timedout;
  1422. conn->tmf_timer.data = (unsigned long)conn;
  1423. add_timer(&conn->tmf_timer);
  1424. ISCSI_DBG_SESSION(session, "tmf set timeout\n");
  1425. spin_unlock_bh(&session->lock);
  1426. mutex_unlock(&session->eh_mutex);
  1427. /*
  1428. * block eh thread until:
  1429. *
  1430. * 1) tmf response
  1431. * 2) tmf timeout
  1432. * 3) session is terminated or restarted or userspace has
  1433. * given up on recovery
  1434. */
  1435. wait_event_interruptible(conn->ehwait, age != session->age ||
  1436. session->state != ISCSI_STATE_LOGGED_IN ||
  1437. conn->tmf_state != TMF_QUEUED);
  1438. if (signal_pending(current))
  1439. flush_signals(current);
  1440. del_timer_sync(&conn->tmf_timer);
  1441. mutex_lock(&session->eh_mutex);
  1442. spin_lock_bh(&session->lock);
  1443. /* if the session drops it will clean up the task */
  1444. if (age != session->age ||
  1445. session->state != ISCSI_STATE_LOGGED_IN)
  1446. return -ENOTCONN;
  1447. return 0;
  1448. }
  1449. /*
  1450. * Fail commands. session lock held and recv side suspended and xmit
  1451. * thread flushed
  1452. */
  1453. static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
  1454. int error)
  1455. {
  1456. struct iscsi_task *task;
  1457. int i;
  1458. for (i = 0; i < conn->session->cmds_max; i++) {
  1459. task = conn->session->cmds[i];
  1460. if (!task->sc || task->state == ISCSI_TASK_FREE)
  1461. continue;
  1462. if (lun != -1 && lun != task->sc->device->lun)
  1463. continue;
  1464. ISCSI_DBG_SESSION(conn->session,
  1465. "failing sc %p itt 0x%x state %d\n",
  1466. task->sc, task->itt, task->state);
  1467. fail_scsi_task(task, error);
  1468. }
  1469. }
  1470. void iscsi_suspend_tx(struct iscsi_conn *conn)
  1471. {
  1472. struct Scsi_Host *shost = conn->session->host;
  1473. struct iscsi_host *ihost = shost_priv(shost);
  1474. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1475. if (ihost->workq)
  1476. flush_workqueue(ihost->workq);
  1477. }
  1478. EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
  1479. static void iscsi_start_tx(struct iscsi_conn *conn)
  1480. {
  1481. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1482. iscsi_conn_queue_work(conn);
  1483. }
  1484. /*
  1485. * We want to make sure a ping is in flight. It has timed out.
  1486. * And we are not busy processing a pdu that is making
  1487. * progress but got started before the ping and is taking a while
  1488. * to complete so the ping is just stuck behind it in a queue.
  1489. */
  1490. static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
  1491. {
  1492. if (conn->ping_task &&
  1493. time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1494. (conn->ping_timeout * HZ), jiffies))
  1495. return 1;
  1496. else
  1497. return 0;
  1498. }
  1499. static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1500. {
  1501. struct iscsi_cls_session *cls_session;
  1502. struct iscsi_session *session;
  1503. struct iscsi_conn *conn;
  1504. enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
  1505. cls_session = starget_to_session(scsi_target(scmd->device));
  1506. session = cls_session->dd_data;
  1507. ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
  1508. spin_lock(&session->lock);
  1509. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1510. /*
  1511. * We are probably in the middle of iscsi recovery so let
  1512. * that complete and handle the error.
  1513. */
  1514. rc = BLK_EH_RESET_TIMER;
  1515. goto done;
  1516. }
  1517. conn = session->leadconn;
  1518. if (!conn) {
  1519. /* In the middle of shuting down */
  1520. rc = BLK_EH_RESET_TIMER;
  1521. goto done;
  1522. }
  1523. if (!conn->recv_timeout && !conn->ping_timeout)
  1524. goto done;
  1525. /*
  1526. * if the ping timedout then we are in the middle of cleaning up
  1527. * and can let the iscsi eh handle it
  1528. */
  1529. if (iscsi_has_ping_timed_out(conn)) {
  1530. rc = BLK_EH_RESET_TIMER;
  1531. goto done;
  1532. }
  1533. /*
  1534. * if we are about to check the transport then give the command
  1535. * more time
  1536. */
  1537. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1538. jiffies)) {
  1539. rc = BLK_EH_RESET_TIMER;
  1540. goto done;
  1541. }
  1542. /* if in the middle of checking the transport then give us more time */
  1543. if (conn->ping_task)
  1544. rc = BLK_EH_RESET_TIMER;
  1545. done:
  1546. spin_unlock(&session->lock);
  1547. ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
  1548. "timer reset" : "nh");
  1549. return rc;
  1550. }
  1551. static void iscsi_check_transport_timeouts(unsigned long data)
  1552. {
  1553. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1554. struct iscsi_session *session = conn->session;
  1555. unsigned long recv_timeout, next_timeout = 0, last_recv;
  1556. spin_lock(&session->lock);
  1557. if (session->state != ISCSI_STATE_LOGGED_IN)
  1558. goto done;
  1559. recv_timeout = conn->recv_timeout;
  1560. if (!recv_timeout)
  1561. goto done;
  1562. recv_timeout *= HZ;
  1563. last_recv = conn->last_recv;
  1564. if (iscsi_has_ping_timed_out(conn)) {
  1565. iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
  1566. "expired, recv timeout %d, last rx %lu, "
  1567. "last ping %lu, now %lu\n",
  1568. conn->ping_timeout, conn->recv_timeout,
  1569. last_recv, conn->last_ping, jiffies);
  1570. spin_unlock(&session->lock);
  1571. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1572. return;
  1573. }
  1574. if (time_before_eq(last_recv + recv_timeout, jiffies)) {
  1575. /* send a ping to try to provoke some traffic */
  1576. ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
  1577. iscsi_send_nopout(conn, NULL);
  1578. next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
  1579. } else
  1580. next_timeout = last_recv + recv_timeout;
  1581. ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
  1582. mod_timer(&conn->transport_timer, next_timeout);
  1583. done:
  1584. spin_unlock(&session->lock);
  1585. }
  1586. static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
  1587. struct iscsi_tm *hdr)
  1588. {
  1589. memset(hdr, 0, sizeof(*hdr));
  1590. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1591. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1592. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1593. memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
  1594. hdr->rtt = task->hdr_itt;
  1595. hdr->refcmdsn = task->cmdsn;
  1596. }
  1597. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1598. {
  1599. struct iscsi_cls_session *cls_session;
  1600. struct iscsi_session *session;
  1601. struct iscsi_conn *conn;
  1602. struct iscsi_task *task;
  1603. struct iscsi_tm *hdr;
  1604. int rc, age;
  1605. cls_session = starget_to_session(scsi_target(sc->device));
  1606. session = cls_session->dd_data;
  1607. mutex_lock(&session->eh_mutex);
  1608. spin_lock_bh(&session->lock);
  1609. /*
  1610. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1611. * got the command.
  1612. */
  1613. if (!sc->SCp.ptr) {
  1614. ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
  1615. "it completed.\n");
  1616. spin_unlock_bh(&session->lock);
  1617. mutex_unlock(&session->eh_mutex);
  1618. return SUCCESS;
  1619. }
  1620. /*
  1621. * If we are not logged in or we have started a new session
  1622. * then let the host reset code handle this
  1623. */
  1624. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1625. sc->SCp.phase != session->age) {
  1626. spin_unlock_bh(&session->lock);
  1627. mutex_unlock(&session->eh_mutex);
  1628. return FAILED;
  1629. }
  1630. conn = session->leadconn;
  1631. conn->eh_abort_cnt++;
  1632. age = session->age;
  1633. task = (struct iscsi_task *)sc->SCp.ptr;
  1634. ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
  1635. sc, task->itt);
  1636. /* task completed before time out */
  1637. if (!task->sc) {
  1638. ISCSI_DBG_SESSION(session, "sc completed while abort in "
  1639. "progress\n");
  1640. goto success;
  1641. }
  1642. if (task->state == ISCSI_TASK_PENDING) {
  1643. fail_scsi_task(task, DID_ABORT);
  1644. goto success;
  1645. }
  1646. /* only have one tmf outstanding at a time */
  1647. if (conn->tmf_state != TMF_INITIAL)
  1648. goto failed;
  1649. conn->tmf_state = TMF_QUEUED;
  1650. hdr = &conn->tmhdr;
  1651. iscsi_prep_abort_task_pdu(task, hdr);
  1652. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1653. rc = FAILED;
  1654. goto failed;
  1655. }
  1656. switch (conn->tmf_state) {
  1657. case TMF_SUCCESS:
  1658. spin_unlock_bh(&session->lock);
  1659. /*
  1660. * stop tx side incase the target had sent a abort rsp but
  1661. * the initiator was still writing out data.
  1662. */
  1663. iscsi_suspend_tx(conn);
  1664. /*
  1665. * we do not stop the recv side because targets have been
  1666. * good and have never sent us a successful tmf response
  1667. * then sent more data for the cmd.
  1668. */
  1669. spin_lock(&session->lock);
  1670. fail_scsi_task(task, DID_ABORT);
  1671. conn->tmf_state = TMF_INITIAL;
  1672. spin_unlock(&session->lock);
  1673. iscsi_start_tx(conn);
  1674. goto success_unlocked;
  1675. case TMF_TIMEDOUT:
  1676. spin_unlock_bh(&session->lock);
  1677. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1678. goto failed_unlocked;
  1679. case TMF_NOT_FOUND:
  1680. if (!sc->SCp.ptr) {
  1681. conn->tmf_state = TMF_INITIAL;
  1682. /* task completed before tmf abort response */
  1683. ISCSI_DBG_SESSION(session, "sc completed while abort "
  1684. "in progress\n");
  1685. goto success;
  1686. }
  1687. /* fall through */
  1688. default:
  1689. conn->tmf_state = TMF_INITIAL;
  1690. goto failed;
  1691. }
  1692. success:
  1693. spin_unlock_bh(&session->lock);
  1694. success_unlocked:
  1695. ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
  1696. sc, task->itt);
  1697. mutex_unlock(&session->eh_mutex);
  1698. return SUCCESS;
  1699. failed:
  1700. spin_unlock_bh(&session->lock);
  1701. failed_unlocked:
  1702. ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
  1703. task ? task->itt : 0);
  1704. mutex_unlock(&session->eh_mutex);
  1705. return FAILED;
  1706. }
  1707. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1708. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1709. {
  1710. memset(hdr, 0, sizeof(*hdr));
  1711. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1712. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1713. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1714. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1715. hdr->rtt = RESERVED_ITT;
  1716. }
  1717. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1718. {
  1719. struct iscsi_cls_session *cls_session;
  1720. struct iscsi_session *session;
  1721. struct iscsi_conn *conn;
  1722. struct iscsi_tm *hdr;
  1723. int rc = FAILED;
  1724. cls_session = starget_to_session(scsi_target(sc->device));
  1725. session = cls_session->dd_data;
  1726. ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
  1727. sc, sc->device->lun);
  1728. mutex_lock(&session->eh_mutex);
  1729. spin_lock_bh(&session->lock);
  1730. /*
  1731. * Just check if we are not logged in. We cannot check for
  1732. * the phase because the reset could come from a ioctl.
  1733. */
  1734. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1735. goto unlock;
  1736. conn = session->leadconn;
  1737. /* only have one tmf outstanding at a time */
  1738. if (conn->tmf_state != TMF_INITIAL)
  1739. goto unlock;
  1740. conn->tmf_state = TMF_QUEUED;
  1741. hdr = &conn->tmhdr;
  1742. iscsi_prep_lun_reset_pdu(sc, hdr);
  1743. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1744. session->lu_reset_timeout)) {
  1745. rc = FAILED;
  1746. goto unlock;
  1747. }
  1748. switch (conn->tmf_state) {
  1749. case TMF_SUCCESS:
  1750. break;
  1751. case TMF_TIMEDOUT:
  1752. spin_unlock_bh(&session->lock);
  1753. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1754. goto done;
  1755. default:
  1756. conn->tmf_state = TMF_INITIAL;
  1757. goto unlock;
  1758. }
  1759. rc = SUCCESS;
  1760. spin_unlock_bh(&session->lock);
  1761. iscsi_suspend_tx(conn);
  1762. spin_lock_bh(&session->lock);
  1763. fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
  1764. conn->tmf_state = TMF_INITIAL;
  1765. spin_unlock_bh(&session->lock);
  1766. iscsi_start_tx(conn);
  1767. goto done;
  1768. unlock:
  1769. spin_unlock_bh(&session->lock);
  1770. done:
  1771. ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
  1772. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1773. mutex_unlock(&session->eh_mutex);
  1774. return rc;
  1775. }
  1776. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1777. /*
  1778. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1779. * should be accessed via kfifo_{get,put} on q->queue.
  1780. * Optionally, the caller can obtain the array of object pointers
  1781. * by passing in a non-NULL @items pointer
  1782. */
  1783. int
  1784. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1785. {
  1786. int i, num_arrays = 1;
  1787. memset(q, 0, sizeof(*q));
  1788. q->max = max;
  1789. /* If the user passed an items pointer, he wants a copy of
  1790. * the array. */
  1791. if (items)
  1792. num_arrays++;
  1793. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1794. if (q->pool == NULL)
  1795. return -ENOMEM;
  1796. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1797. GFP_KERNEL, NULL);
  1798. if (IS_ERR(q->queue)) {
  1799. q->queue = NULL;
  1800. goto enomem;
  1801. }
  1802. for (i = 0; i < max; i++) {
  1803. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1804. if (q->pool[i] == NULL) {
  1805. q->max = i;
  1806. goto enomem;
  1807. }
  1808. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1809. }
  1810. if (items) {
  1811. *items = q->pool + max;
  1812. memcpy(*items, q->pool, max * sizeof(void *));
  1813. }
  1814. return 0;
  1815. enomem:
  1816. iscsi_pool_free(q);
  1817. return -ENOMEM;
  1818. }
  1819. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1820. void iscsi_pool_free(struct iscsi_pool *q)
  1821. {
  1822. int i;
  1823. for (i = 0; i < q->max; i++)
  1824. kfree(q->pool[i]);
  1825. kfree(q->pool);
  1826. kfree(q->queue);
  1827. }
  1828. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1829. /**
  1830. * iscsi_host_add - add host to system
  1831. * @shost: scsi host
  1832. * @pdev: parent device
  1833. *
  1834. * This should be called by partial offload and software iscsi drivers
  1835. * to add a host to the system.
  1836. */
  1837. int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
  1838. {
  1839. if (!shost->can_queue)
  1840. shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
  1841. if (!shost->cmd_per_lun)
  1842. shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
  1843. if (!shost->transportt->eh_timed_out)
  1844. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1845. return scsi_add_host(shost, pdev);
  1846. }
  1847. EXPORT_SYMBOL_GPL(iscsi_host_add);
  1848. /**
  1849. * iscsi_host_alloc - allocate a host and driver data
  1850. * @sht: scsi host template
  1851. * @dd_data_size: driver host data size
  1852. * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
  1853. *
  1854. * This should be called by partial offload and software iscsi drivers.
  1855. * To access the driver specific memory use the iscsi_host_priv() macro.
  1856. */
  1857. struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
  1858. int dd_data_size, bool xmit_can_sleep)
  1859. {
  1860. struct Scsi_Host *shost;
  1861. struct iscsi_host *ihost;
  1862. shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
  1863. if (!shost)
  1864. return NULL;
  1865. ihost = shost_priv(shost);
  1866. if (xmit_can_sleep) {
  1867. snprintf(ihost->workq_name, sizeof(ihost->workq_name),
  1868. "iscsi_q_%d", shost->host_no);
  1869. ihost->workq = create_singlethread_workqueue(ihost->workq_name);
  1870. if (!ihost->workq)
  1871. goto free_host;
  1872. }
  1873. spin_lock_init(&ihost->lock);
  1874. ihost->state = ISCSI_HOST_SETUP;
  1875. ihost->num_sessions = 0;
  1876. init_waitqueue_head(&ihost->session_removal_wq);
  1877. return shost;
  1878. free_host:
  1879. scsi_host_put(shost);
  1880. return NULL;
  1881. }
  1882. EXPORT_SYMBOL_GPL(iscsi_host_alloc);
  1883. static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
  1884. {
  1885. iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
  1886. }
  1887. /**
  1888. * iscsi_host_remove - remove host and sessions
  1889. * @shost: scsi host
  1890. *
  1891. * If there are any sessions left, this will initiate the removal and wait
  1892. * for the completion.
  1893. */
  1894. void iscsi_host_remove(struct Scsi_Host *shost)
  1895. {
  1896. struct iscsi_host *ihost = shost_priv(shost);
  1897. unsigned long flags;
  1898. spin_lock_irqsave(&ihost->lock, flags);
  1899. ihost->state = ISCSI_HOST_REMOVED;
  1900. spin_unlock_irqrestore(&ihost->lock, flags);
  1901. iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
  1902. wait_event_interruptible(ihost->session_removal_wq,
  1903. ihost->num_sessions == 0);
  1904. if (signal_pending(current))
  1905. flush_signals(current);
  1906. scsi_remove_host(shost);
  1907. if (ihost->workq)
  1908. destroy_workqueue(ihost->workq);
  1909. }
  1910. EXPORT_SYMBOL_GPL(iscsi_host_remove);
  1911. void iscsi_host_free(struct Scsi_Host *shost)
  1912. {
  1913. struct iscsi_host *ihost = shost_priv(shost);
  1914. kfree(ihost->netdev);
  1915. kfree(ihost->hwaddress);
  1916. kfree(ihost->initiatorname);
  1917. scsi_host_put(shost);
  1918. }
  1919. EXPORT_SYMBOL_GPL(iscsi_host_free);
  1920. static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
  1921. {
  1922. struct iscsi_host *ihost = shost_priv(shost);
  1923. unsigned long flags;
  1924. shost = scsi_host_get(shost);
  1925. if (!shost) {
  1926. printk(KERN_ERR "Invalid state. Cannot notify host removal "
  1927. "of session teardown event because host already "
  1928. "removed.\n");
  1929. return;
  1930. }
  1931. spin_lock_irqsave(&ihost->lock, flags);
  1932. ihost->num_sessions--;
  1933. if (ihost->num_sessions == 0)
  1934. wake_up(&ihost->session_removal_wq);
  1935. spin_unlock_irqrestore(&ihost->lock, flags);
  1936. scsi_host_put(shost);
  1937. }
  1938. /**
  1939. * iscsi_session_setup - create iscsi cls session and host and session
  1940. * @iscsit: iscsi transport template
  1941. * @shost: scsi host
  1942. * @cmds_max: session can queue
  1943. * @cmd_task_size: LLD task private data size
  1944. * @initial_cmdsn: initial CmdSN
  1945. *
  1946. * This can be used by software iscsi_transports that allocate
  1947. * a session per scsi host.
  1948. *
  1949. * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
  1950. * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
  1951. * for nop handling and login/logout requests.
  1952. */
  1953. struct iscsi_cls_session *
  1954. iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
  1955. uint16_t cmds_max, int cmd_task_size,
  1956. uint32_t initial_cmdsn, unsigned int id)
  1957. {
  1958. struct iscsi_host *ihost = shost_priv(shost);
  1959. struct iscsi_session *session;
  1960. struct iscsi_cls_session *cls_session;
  1961. int cmd_i, scsi_cmds, total_cmds = cmds_max;
  1962. unsigned long flags;
  1963. spin_lock_irqsave(&ihost->lock, flags);
  1964. if (ihost->state == ISCSI_HOST_REMOVED) {
  1965. spin_unlock_irqrestore(&ihost->lock, flags);
  1966. return NULL;
  1967. }
  1968. ihost->num_sessions++;
  1969. spin_unlock_irqrestore(&ihost->lock, flags);
  1970. if (!total_cmds)
  1971. total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
  1972. /*
  1973. * The iscsi layer needs some tasks for nop handling and tmfs,
  1974. * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
  1975. * + 1 command for scsi IO.
  1976. */
  1977. if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
  1978. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  1979. "must be a power of two that is at least %d.\n",
  1980. total_cmds, ISCSI_TOTAL_CMDS_MIN);
  1981. goto dec_session_count;
  1982. }
  1983. if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
  1984. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  1985. "must be a power of 2 less than or equal to %d.\n",
  1986. cmds_max, ISCSI_TOTAL_CMDS_MAX);
  1987. total_cmds = ISCSI_TOTAL_CMDS_MAX;
  1988. }
  1989. if (!is_power_of_2(total_cmds)) {
  1990. printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
  1991. "must be a power of 2.\n", total_cmds);
  1992. total_cmds = rounddown_pow_of_two(total_cmds);
  1993. if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
  1994. return NULL;
  1995. printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
  1996. total_cmds);
  1997. }
  1998. scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
  1999. cls_session = iscsi_alloc_session(shost, iscsit,
  2000. sizeof(struct iscsi_session));
  2001. if (!cls_session)
  2002. goto dec_session_count;
  2003. session = cls_session->dd_data;
  2004. session->cls_session = cls_session;
  2005. session->host = shost;
  2006. session->state = ISCSI_STATE_FREE;
  2007. session->fast_abort = 1;
  2008. session->lu_reset_timeout = 15;
  2009. session->abort_timeout = 10;
  2010. session->scsi_cmds_max = scsi_cmds;
  2011. session->cmds_max = total_cmds;
  2012. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  2013. session->exp_cmdsn = initial_cmdsn + 1;
  2014. session->max_cmdsn = initial_cmdsn + 1;
  2015. session->max_r2t = 1;
  2016. session->tt = iscsit;
  2017. mutex_init(&session->eh_mutex);
  2018. spin_lock_init(&session->lock);
  2019. /* initialize SCSI PDU commands pool */
  2020. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  2021. (void***)&session->cmds,
  2022. cmd_task_size + sizeof(struct iscsi_task)))
  2023. goto cmdpool_alloc_fail;
  2024. /* pre-format cmds pool with ITT */
  2025. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  2026. struct iscsi_task *task = session->cmds[cmd_i];
  2027. if (cmd_task_size)
  2028. task->dd_data = &task[1];
  2029. task->itt = cmd_i;
  2030. task->state = ISCSI_TASK_FREE;
  2031. INIT_LIST_HEAD(&task->running);
  2032. }
  2033. if (!try_module_get(iscsit->owner))
  2034. goto module_get_fail;
  2035. if (iscsi_add_session(cls_session, id))
  2036. goto cls_session_fail;
  2037. return cls_session;
  2038. cls_session_fail:
  2039. module_put(iscsit->owner);
  2040. module_get_fail:
  2041. iscsi_pool_free(&session->cmdpool);
  2042. cmdpool_alloc_fail:
  2043. iscsi_free_session(cls_session);
  2044. dec_session_count:
  2045. iscsi_host_dec_session_cnt(shost);
  2046. return NULL;
  2047. }
  2048. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  2049. /**
  2050. * iscsi_session_teardown - destroy session, host, and cls_session
  2051. * @cls_session: iscsi session
  2052. *
  2053. * The driver must have called iscsi_remove_session before
  2054. * calling this.
  2055. */
  2056. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  2057. {
  2058. struct iscsi_session *session = cls_session->dd_data;
  2059. struct module *owner = cls_session->transport->owner;
  2060. struct Scsi_Host *shost = session->host;
  2061. iscsi_pool_free(&session->cmdpool);
  2062. kfree(session->password);
  2063. kfree(session->password_in);
  2064. kfree(session->username);
  2065. kfree(session->username_in);
  2066. kfree(session->targetname);
  2067. kfree(session->initiatorname);
  2068. kfree(session->ifacename);
  2069. iscsi_destroy_session(cls_session);
  2070. iscsi_host_dec_session_cnt(shost);
  2071. module_put(owner);
  2072. }
  2073. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  2074. /**
  2075. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  2076. * @cls_session: iscsi_cls_session
  2077. * @dd_size: private driver data size
  2078. * @conn_idx: cid
  2079. */
  2080. struct iscsi_cls_conn *
  2081. iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
  2082. uint32_t conn_idx)
  2083. {
  2084. struct iscsi_session *session = cls_session->dd_data;
  2085. struct iscsi_conn *conn;
  2086. struct iscsi_cls_conn *cls_conn;
  2087. char *data;
  2088. cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
  2089. conn_idx);
  2090. if (!cls_conn)
  2091. return NULL;
  2092. conn = cls_conn->dd_data;
  2093. memset(conn, 0, sizeof(*conn) + dd_size);
  2094. conn->dd_data = cls_conn->dd_data + sizeof(*conn);
  2095. conn->session = session;
  2096. conn->cls_conn = cls_conn;
  2097. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  2098. conn->id = conn_idx;
  2099. conn->exp_statsn = 0;
  2100. conn->tmf_state = TMF_INITIAL;
  2101. init_timer(&conn->transport_timer);
  2102. conn->transport_timer.data = (unsigned long)conn;
  2103. conn->transport_timer.function = iscsi_check_transport_timeouts;
  2104. INIT_LIST_HEAD(&conn->mgmtqueue);
  2105. INIT_LIST_HEAD(&conn->cmdqueue);
  2106. INIT_LIST_HEAD(&conn->requeue);
  2107. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  2108. /* allocate login_task used for the login/text sequences */
  2109. spin_lock_bh(&session->lock);
  2110. if (!__kfifo_get(session->cmdpool.queue,
  2111. (void*)&conn->login_task,
  2112. sizeof(void*))) {
  2113. spin_unlock_bh(&session->lock);
  2114. goto login_task_alloc_fail;
  2115. }
  2116. spin_unlock_bh(&session->lock);
  2117. data = (char *) __get_free_pages(GFP_KERNEL,
  2118. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2119. if (!data)
  2120. goto login_task_data_alloc_fail;
  2121. conn->login_task->data = conn->data = data;
  2122. init_timer(&conn->tmf_timer);
  2123. init_waitqueue_head(&conn->ehwait);
  2124. return cls_conn;
  2125. login_task_data_alloc_fail:
  2126. __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
  2127. sizeof(void*));
  2128. login_task_alloc_fail:
  2129. iscsi_destroy_conn(cls_conn);
  2130. return NULL;
  2131. }
  2132. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  2133. /**
  2134. * iscsi_conn_teardown - teardown iscsi connection
  2135. * cls_conn: iscsi class connection
  2136. *
  2137. * TODO: we may need to make this into a two step process
  2138. * like scsi-mls remove + put host
  2139. */
  2140. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  2141. {
  2142. struct iscsi_conn *conn = cls_conn->dd_data;
  2143. struct iscsi_session *session = conn->session;
  2144. unsigned long flags;
  2145. del_timer_sync(&conn->transport_timer);
  2146. spin_lock_bh(&session->lock);
  2147. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  2148. if (session->leadconn == conn) {
  2149. /*
  2150. * leading connection? then give up on recovery.
  2151. */
  2152. session->state = ISCSI_STATE_TERMINATE;
  2153. wake_up(&conn->ehwait);
  2154. }
  2155. spin_unlock_bh(&session->lock);
  2156. /*
  2157. * Block until all in-progress commands for this connection
  2158. * time out or fail.
  2159. */
  2160. for (;;) {
  2161. spin_lock_irqsave(session->host->host_lock, flags);
  2162. if (!session->host->host_busy) { /* OK for ERL == 0 */
  2163. spin_unlock_irqrestore(session->host->host_lock, flags);
  2164. break;
  2165. }
  2166. spin_unlock_irqrestore(session->host->host_lock, flags);
  2167. msleep_interruptible(500);
  2168. iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
  2169. "host_busy %d host_failed %d\n",
  2170. session->host->host_busy,
  2171. session->host->host_failed);
  2172. /*
  2173. * force eh_abort() to unblock
  2174. */
  2175. wake_up(&conn->ehwait);
  2176. }
  2177. /* flush queued up work because we free the connection below */
  2178. iscsi_suspend_tx(conn);
  2179. spin_lock_bh(&session->lock);
  2180. free_pages((unsigned long) conn->data,
  2181. get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
  2182. kfree(conn->persistent_address);
  2183. __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
  2184. sizeof(void*));
  2185. if (session->leadconn == conn)
  2186. session->leadconn = NULL;
  2187. spin_unlock_bh(&session->lock);
  2188. iscsi_destroy_conn(cls_conn);
  2189. }
  2190. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  2191. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  2192. {
  2193. struct iscsi_conn *conn = cls_conn->dd_data;
  2194. struct iscsi_session *session = conn->session;
  2195. if (!session) {
  2196. iscsi_conn_printk(KERN_ERR, conn,
  2197. "can't start unbound connection\n");
  2198. return -EPERM;
  2199. }
  2200. if ((session->imm_data_en || !session->initial_r2t_en) &&
  2201. session->first_burst > session->max_burst) {
  2202. iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
  2203. "first_burst %d max_burst %d\n",
  2204. session->first_burst, session->max_burst);
  2205. return -EINVAL;
  2206. }
  2207. if (conn->ping_timeout && !conn->recv_timeout) {
  2208. iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
  2209. "zero. Using 5 seconds\n.");
  2210. conn->recv_timeout = 5;
  2211. }
  2212. if (conn->recv_timeout && !conn->ping_timeout) {
  2213. iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
  2214. "zero. Using 5 seconds.\n");
  2215. conn->ping_timeout = 5;
  2216. }
  2217. spin_lock_bh(&session->lock);
  2218. conn->c_stage = ISCSI_CONN_STARTED;
  2219. session->state = ISCSI_STATE_LOGGED_IN;
  2220. session->queued_cmdsn = session->cmdsn;
  2221. conn->last_recv = jiffies;
  2222. conn->last_ping = jiffies;
  2223. if (conn->recv_timeout && conn->ping_timeout)
  2224. mod_timer(&conn->transport_timer,
  2225. jiffies + (conn->recv_timeout * HZ));
  2226. switch(conn->stop_stage) {
  2227. case STOP_CONN_RECOVER:
  2228. /*
  2229. * unblock eh_abort() if it is blocked. re-try all
  2230. * commands after successful recovery
  2231. */
  2232. conn->stop_stage = 0;
  2233. conn->tmf_state = TMF_INITIAL;
  2234. session->age++;
  2235. if (session->age == 16)
  2236. session->age = 0;
  2237. break;
  2238. case STOP_CONN_TERM:
  2239. conn->stop_stage = 0;
  2240. break;
  2241. default:
  2242. break;
  2243. }
  2244. spin_unlock_bh(&session->lock);
  2245. iscsi_unblock_session(session->cls_session);
  2246. wake_up(&conn->ehwait);
  2247. return 0;
  2248. }
  2249. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  2250. static void
  2251. fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
  2252. {
  2253. struct iscsi_task *task;
  2254. int i, state;
  2255. for (i = 0; i < conn->session->cmds_max; i++) {
  2256. task = conn->session->cmds[i];
  2257. if (task->sc)
  2258. continue;
  2259. if (task->state == ISCSI_TASK_FREE)
  2260. continue;
  2261. ISCSI_DBG_SESSION(conn->session,
  2262. "failing mgmt itt 0x%x state %d\n",
  2263. task->itt, task->state);
  2264. state = ISCSI_TASK_ABRT_SESS_RECOV;
  2265. if (task->state == ISCSI_TASK_PENDING)
  2266. state = ISCSI_TASK_COMPLETED;
  2267. iscsi_complete_task(task, state);
  2268. }
  2269. }
  2270. static void iscsi_start_session_recovery(struct iscsi_session *session,
  2271. struct iscsi_conn *conn, int flag)
  2272. {
  2273. int old_stop_stage;
  2274. mutex_lock(&session->eh_mutex);
  2275. spin_lock_bh(&session->lock);
  2276. if (conn->stop_stage == STOP_CONN_TERM) {
  2277. spin_unlock_bh(&session->lock);
  2278. mutex_unlock(&session->eh_mutex);
  2279. return;
  2280. }
  2281. /*
  2282. * When this is called for the in_login state, we only want to clean
  2283. * up the login task and connection. We do not need to block and set
  2284. * the recovery state again
  2285. */
  2286. if (flag == STOP_CONN_TERM)
  2287. session->state = ISCSI_STATE_TERMINATE;
  2288. else if (conn->stop_stage != STOP_CONN_RECOVER)
  2289. session->state = ISCSI_STATE_IN_RECOVERY;
  2290. spin_unlock_bh(&session->lock);
  2291. del_timer_sync(&conn->transport_timer);
  2292. iscsi_suspend_tx(conn);
  2293. spin_lock_bh(&session->lock);
  2294. old_stop_stage = conn->stop_stage;
  2295. conn->stop_stage = flag;
  2296. conn->c_stage = ISCSI_CONN_STOPPED;
  2297. spin_unlock_bh(&session->lock);
  2298. /*
  2299. * for connection level recovery we should not calculate
  2300. * header digest. conn->hdr_size used for optimization
  2301. * in hdr_extract() and will be re-negotiated at
  2302. * set_param() time.
  2303. */
  2304. if (flag == STOP_CONN_RECOVER) {
  2305. conn->hdrdgst_en = 0;
  2306. conn->datadgst_en = 0;
  2307. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  2308. old_stop_stage != STOP_CONN_RECOVER) {
  2309. ISCSI_DBG_SESSION(session, "blocking session\n");
  2310. iscsi_block_session(session->cls_session);
  2311. }
  2312. }
  2313. /*
  2314. * flush queues.
  2315. */
  2316. spin_lock_bh(&session->lock);
  2317. fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
  2318. fail_mgmt_tasks(session, conn);
  2319. spin_unlock_bh(&session->lock);
  2320. mutex_unlock(&session->eh_mutex);
  2321. }
  2322. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  2323. {
  2324. struct iscsi_conn *conn = cls_conn->dd_data;
  2325. struct iscsi_session *session = conn->session;
  2326. switch (flag) {
  2327. case STOP_CONN_RECOVER:
  2328. case STOP_CONN_TERM:
  2329. iscsi_start_session_recovery(session, conn, flag);
  2330. break;
  2331. default:
  2332. iscsi_conn_printk(KERN_ERR, conn,
  2333. "invalid stop flag %d\n", flag);
  2334. }
  2335. }
  2336. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  2337. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  2338. struct iscsi_cls_conn *cls_conn, int is_leading)
  2339. {
  2340. struct iscsi_session *session = cls_session->dd_data;
  2341. struct iscsi_conn *conn = cls_conn->dd_data;
  2342. spin_lock_bh(&session->lock);
  2343. if (is_leading)
  2344. session->leadconn = conn;
  2345. spin_unlock_bh(&session->lock);
  2346. /*
  2347. * Unblock xmitworker(), Login Phase will pass through.
  2348. */
  2349. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  2350. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  2351. return 0;
  2352. }
  2353. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  2354. static int iscsi_switch_str_param(char **param, char *new_val_buf)
  2355. {
  2356. char *new_val;
  2357. if (*param) {
  2358. if (!strcmp(*param, new_val_buf))
  2359. return 0;
  2360. }
  2361. new_val = kstrdup(new_val_buf, GFP_NOIO);
  2362. if (!new_val)
  2363. return -ENOMEM;
  2364. kfree(*param);
  2365. *param = new_val;
  2366. return 0;
  2367. }
  2368. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  2369. enum iscsi_param param, char *buf, int buflen)
  2370. {
  2371. struct iscsi_conn *conn = cls_conn->dd_data;
  2372. struct iscsi_session *session = conn->session;
  2373. uint32_t value;
  2374. switch(param) {
  2375. case ISCSI_PARAM_FAST_ABORT:
  2376. sscanf(buf, "%d", &session->fast_abort);
  2377. break;
  2378. case ISCSI_PARAM_ABORT_TMO:
  2379. sscanf(buf, "%d", &session->abort_timeout);
  2380. break;
  2381. case ISCSI_PARAM_LU_RESET_TMO:
  2382. sscanf(buf, "%d", &session->lu_reset_timeout);
  2383. break;
  2384. case ISCSI_PARAM_PING_TMO:
  2385. sscanf(buf, "%d", &conn->ping_timeout);
  2386. break;
  2387. case ISCSI_PARAM_RECV_TMO:
  2388. sscanf(buf, "%d", &conn->recv_timeout);
  2389. break;
  2390. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2391. sscanf(buf, "%d", &conn->max_recv_dlength);
  2392. break;
  2393. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2394. sscanf(buf, "%d", &conn->max_xmit_dlength);
  2395. break;
  2396. case ISCSI_PARAM_HDRDGST_EN:
  2397. sscanf(buf, "%d", &conn->hdrdgst_en);
  2398. break;
  2399. case ISCSI_PARAM_DATADGST_EN:
  2400. sscanf(buf, "%d", &conn->datadgst_en);
  2401. break;
  2402. case ISCSI_PARAM_INITIAL_R2T_EN:
  2403. sscanf(buf, "%d", &session->initial_r2t_en);
  2404. break;
  2405. case ISCSI_PARAM_MAX_R2T:
  2406. sscanf(buf, "%d", &session->max_r2t);
  2407. break;
  2408. case ISCSI_PARAM_IMM_DATA_EN:
  2409. sscanf(buf, "%d", &session->imm_data_en);
  2410. break;
  2411. case ISCSI_PARAM_FIRST_BURST:
  2412. sscanf(buf, "%d", &session->first_burst);
  2413. break;
  2414. case ISCSI_PARAM_MAX_BURST:
  2415. sscanf(buf, "%d", &session->max_burst);
  2416. break;
  2417. case ISCSI_PARAM_PDU_INORDER_EN:
  2418. sscanf(buf, "%d", &session->pdu_inorder_en);
  2419. break;
  2420. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2421. sscanf(buf, "%d", &session->dataseq_inorder_en);
  2422. break;
  2423. case ISCSI_PARAM_ERL:
  2424. sscanf(buf, "%d", &session->erl);
  2425. break;
  2426. case ISCSI_PARAM_IFMARKER_EN:
  2427. sscanf(buf, "%d", &value);
  2428. BUG_ON(value);
  2429. break;
  2430. case ISCSI_PARAM_OFMARKER_EN:
  2431. sscanf(buf, "%d", &value);
  2432. BUG_ON(value);
  2433. break;
  2434. case ISCSI_PARAM_EXP_STATSN:
  2435. sscanf(buf, "%u", &conn->exp_statsn);
  2436. break;
  2437. case ISCSI_PARAM_USERNAME:
  2438. return iscsi_switch_str_param(&session->username, buf);
  2439. case ISCSI_PARAM_USERNAME_IN:
  2440. return iscsi_switch_str_param(&session->username_in, buf);
  2441. case ISCSI_PARAM_PASSWORD:
  2442. return iscsi_switch_str_param(&session->password, buf);
  2443. case ISCSI_PARAM_PASSWORD_IN:
  2444. return iscsi_switch_str_param(&session->password_in, buf);
  2445. case ISCSI_PARAM_TARGET_NAME:
  2446. return iscsi_switch_str_param(&session->targetname, buf);
  2447. case ISCSI_PARAM_TPGT:
  2448. sscanf(buf, "%d", &session->tpgt);
  2449. break;
  2450. case ISCSI_PARAM_PERSISTENT_PORT:
  2451. sscanf(buf, "%d", &conn->persistent_port);
  2452. break;
  2453. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2454. return iscsi_switch_str_param(&conn->persistent_address, buf);
  2455. case ISCSI_PARAM_IFACE_NAME:
  2456. return iscsi_switch_str_param(&session->ifacename, buf);
  2457. case ISCSI_PARAM_INITIATOR_NAME:
  2458. return iscsi_switch_str_param(&session->initiatorname, buf);
  2459. default:
  2460. return -ENOSYS;
  2461. }
  2462. return 0;
  2463. }
  2464. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2465. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2466. enum iscsi_param param, char *buf)
  2467. {
  2468. struct iscsi_session *session = cls_session->dd_data;
  2469. int len;
  2470. switch(param) {
  2471. case ISCSI_PARAM_FAST_ABORT:
  2472. len = sprintf(buf, "%d\n", session->fast_abort);
  2473. break;
  2474. case ISCSI_PARAM_ABORT_TMO:
  2475. len = sprintf(buf, "%d\n", session->abort_timeout);
  2476. break;
  2477. case ISCSI_PARAM_LU_RESET_TMO:
  2478. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2479. break;
  2480. case ISCSI_PARAM_INITIAL_R2T_EN:
  2481. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2482. break;
  2483. case ISCSI_PARAM_MAX_R2T:
  2484. len = sprintf(buf, "%hu\n", session->max_r2t);
  2485. break;
  2486. case ISCSI_PARAM_IMM_DATA_EN:
  2487. len = sprintf(buf, "%d\n", session->imm_data_en);
  2488. break;
  2489. case ISCSI_PARAM_FIRST_BURST:
  2490. len = sprintf(buf, "%u\n", session->first_burst);
  2491. break;
  2492. case ISCSI_PARAM_MAX_BURST:
  2493. len = sprintf(buf, "%u\n", session->max_burst);
  2494. break;
  2495. case ISCSI_PARAM_PDU_INORDER_EN:
  2496. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2497. break;
  2498. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2499. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2500. break;
  2501. case ISCSI_PARAM_ERL:
  2502. len = sprintf(buf, "%d\n", session->erl);
  2503. break;
  2504. case ISCSI_PARAM_TARGET_NAME:
  2505. len = sprintf(buf, "%s\n", session->targetname);
  2506. break;
  2507. case ISCSI_PARAM_TPGT:
  2508. len = sprintf(buf, "%d\n", session->tpgt);
  2509. break;
  2510. case ISCSI_PARAM_USERNAME:
  2511. len = sprintf(buf, "%s\n", session->username);
  2512. break;
  2513. case ISCSI_PARAM_USERNAME_IN:
  2514. len = sprintf(buf, "%s\n", session->username_in);
  2515. break;
  2516. case ISCSI_PARAM_PASSWORD:
  2517. len = sprintf(buf, "%s\n", session->password);
  2518. break;
  2519. case ISCSI_PARAM_PASSWORD_IN:
  2520. len = sprintf(buf, "%s\n", session->password_in);
  2521. break;
  2522. case ISCSI_PARAM_IFACE_NAME:
  2523. len = sprintf(buf, "%s\n", session->ifacename);
  2524. break;
  2525. case ISCSI_PARAM_INITIATOR_NAME:
  2526. len = sprintf(buf, "%s\n", session->initiatorname);
  2527. break;
  2528. default:
  2529. return -ENOSYS;
  2530. }
  2531. return len;
  2532. }
  2533. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2534. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2535. enum iscsi_param param, char *buf)
  2536. {
  2537. struct iscsi_conn *conn = cls_conn->dd_data;
  2538. int len;
  2539. switch(param) {
  2540. case ISCSI_PARAM_PING_TMO:
  2541. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2542. break;
  2543. case ISCSI_PARAM_RECV_TMO:
  2544. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2545. break;
  2546. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2547. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2548. break;
  2549. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2550. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2551. break;
  2552. case ISCSI_PARAM_HDRDGST_EN:
  2553. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2554. break;
  2555. case ISCSI_PARAM_DATADGST_EN:
  2556. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2557. break;
  2558. case ISCSI_PARAM_IFMARKER_EN:
  2559. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2560. break;
  2561. case ISCSI_PARAM_OFMARKER_EN:
  2562. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2563. break;
  2564. case ISCSI_PARAM_EXP_STATSN:
  2565. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2566. break;
  2567. case ISCSI_PARAM_PERSISTENT_PORT:
  2568. len = sprintf(buf, "%d\n", conn->persistent_port);
  2569. break;
  2570. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2571. len = sprintf(buf, "%s\n", conn->persistent_address);
  2572. break;
  2573. default:
  2574. return -ENOSYS;
  2575. }
  2576. return len;
  2577. }
  2578. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2579. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2580. char *buf)
  2581. {
  2582. struct iscsi_host *ihost = shost_priv(shost);
  2583. int len;
  2584. switch (param) {
  2585. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2586. len = sprintf(buf, "%s\n", ihost->netdev);
  2587. break;
  2588. case ISCSI_HOST_PARAM_HWADDRESS:
  2589. len = sprintf(buf, "%s\n", ihost->hwaddress);
  2590. break;
  2591. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2592. len = sprintf(buf, "%s\n", ihost->initiatorname);
  2593. break;
  2594. case ISCSI_HOST_PARAM_IPADDRESS:
  2595. len = sprintf(buf, "%s\n", ihost->local_address);
  2596. break;
  2597. default:
  2598. return -ENOSYS;
  2599. }
  2600. return len;
  2601. }
  2602. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2603. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2604. char *buf, int buflen)
  2605. {
  2606. struct iscsi_host *ihost = shost_priv(shost);
  2607. switch (param) {
  2608. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2609. return iscsi_switch_str_param(&ihost->netdev, buf);
  2610. case ISCSI_HOST_PARAM_HWADDRESS:
  2611. return iscsi_switch_str_param(&ihost->hwaddress, buf);
  2612. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2613. return iscsi_switch_str_param(&ihost->initiatorname, buf);
  2614. default:
  2615. return -ENOSYS;
  2616. }
  2617. return 0;
  2618. }
  2619. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2620. MODULE_AUTHOR("Mike Christie");
  2621. MODULE_DESCRIPTION("iSCSI library functions");
  2622. MODULE_LICENSE("GPL");