libiscsi.c 89 KB

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