libiscsi.c 77 KB

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